Public: anyone can read this; the crew can change it.

A Talk reply shows what its turn cost, in cents, under the reply #16160

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/16143-a-talk-reply-shows-what-its-turn-cost-in → main 0 commits fork 1 thread · 1 unresolved

  • Merged. torque/16143-a-talk-reply-shows-what-its-turn-cost-in was merged into main as 3fede26.

Review

Sign in to review this change.
6 files changed +29 −14
src/core/conversation_cost.ts +9 −1
@@ -25,7 +25,7 @@ export const spendWords = (spend: Spend): string => {
2525 // A chosen model the price table does not know has no cost to state: its name alone, never $0.
2626 const on = spend.model === undefined ? "" : spend.usd > 0 ? ` · ${spend.model}` : spend.model;
2727 if (spend.usd <= 0) return spend.stopped ? `Stopped · ${on}` : `${on} this turn`;
28 return spend.stopped ? `Stopped: ${dollars(spend.usd)} spent so far${on}` : `${dollars(spend.usd)} this turn${on}`;
28 return spend.stopped ? `Stopped: ${cents(spend.usd)} spent so far${on}` : `${cents(spend.usd)} this turn${on}`;
2929 };
3030
3131 /**
@@ -98,3 +98,11 @@ export const contextText = (c: Context): string =>
9898
9999 /** Dollars as a quiet figure: cents below a dollar, a tenth of a cent below that. */
100100 export const dollars = (usd: number): string => (usd < 0.01 ? `$${usd.toFixed(3)}` : `$${usd.toFixed(2)}`);
101
102 /**
103 * Dollars as cents for a turn's line: whole cents from one cent up ("50¢"), tenths below ("0.6¢").
104 */
105 export const cents = (usd: number): string => {
106 const c = Math.round(usd * 1000) / 10;
107 return `${c >= 1 ? Math.round(c) : c}¢`;

Non-blocking: Math.round(usd*1000)/10 then Math.round(c) rounds twice; 0.0149 gives 1.5 and then "2¢". Use Math.round(usd*100) for the whole-cent branch.

108 };
src/tui/ssh-talk.ts +2 −2
@@ -32,7 +32,7 @@
3232
3333 import { cancelTalk, queuedTalk, TALK_WORDS, stopTalk, talk, talkAgain } from "../actions/talk.ts";
3434 import { FILE_WORDS, IMAGE_MAX, pendingAmong, removePending, storeFile } from "../core/conversation_files.ts";
35 import { contextOf, contextText, conversationTotal, dollars, spendWords, totalText, turnSpend, type Spend } from "../core/conversation_cost.ts";
35 import { cents, contextOf, contextText, conversationTotal, spendWords, totalText, turnSpend, type Spend } from "../core/conversation_cost.ts";
3636 import { liveCrewRows } from "../actions/crew_now.ts";
3737 import { crewLine } from "../web/crew_now.ts";
3838 import { lastExchange, openConversation } from "../core/conversations.ts";
@@ -200,7 +200,7 @@ export function serveTalk(app: App, session: SshSession, user: User | undefined,
200200 /**
201201 * The line the terminal prints for the turn before: the page's Stopped wording when it was stopped.
202202 */
203 export const lastTurnWords = (spend: Spend): string => (spend.stopped || spend.usd <= 0 ? spendWords(spend) : `last turn ${dollars(spend.usd)}${spend.model === undefined ? "" : ` · ${spend.model}`}`);
203 export const lastTurnWords = (spend: Spend): string => (spend.stopped || spend.usd <= 0 ? spendWords(spend) : `last turn ${cents(spend.usd)}${spend.model === undefined ? "" : ` · ${spend.model}`}`);
204204
205205 /** `talk-again <edit words | regenerate | retry>`; undefined when the command is not one. */
206206 export function parseTalkAgain(command: string | undefined): { how: RedoHow; words: string } | undefined {
test/conversation-talk.test.ts +1 −1
@@ -312,7 +312,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Heard.","t
312312 .query("INSERT INTO conversation_run (id, conversation_id, user_id, prompt, state, cost_usd, context_tokens, context_window, started_at, finished_at) VALUES ('rc1',?1,?2,'x','ok',0.5,500000,1000000,?3,?4)")
313313 .run(conversation.id, id, at, new Date(Date.now() + 1000).toISOString());
314314 const r = await exec("talk and the figures");
315 expect(r.out).toContain("last turn $0.50");
315 expect(r.out).toContain("last turn 50¢");
316316 expect(r.out).toMatch(/Spent \$0\.5\d in this conversation/);
317317 expect(r.out).toContain("Context 50% full (500k of 1M tokens)");
318318 app.db.query("DELETE FROM conversation_run WHERE id = 'rc1'").run();
test/talk-cost.test.ts +9 −2
@@ -10,7 +10,7 @@ import { tmpdir } from "node:os";
1010 import { join } from "node:path";
1111 import { createApp, type App } from "../src/app.ts";
1212 import { appendTurn, newConversation } from "../src/core/conversations.ts";
13 import { conversationTotal, totalText } from "../src/core/conversation_cost.ts";
13 import { cents, conversationTotal, spendWords, totalText } from "../src/core/conversation_cost.ts";
1414 import { PLATFORM } from "../src/core/subscriptions.ts";
1515 import { reportedBefore, say } from "../src/crew/conversation.ts";
1616 import { createSession } from "../src/core/sessions.ts";
@@ -59,7 +59,7 @@ describe("talk cost", () => {
5959 const headers = { cookie: `forge_session=${createSession(app.db, alice.id).token}` };
6060 const page = await (await fetch(`${servers.url}/talk/${conv.id}`, { headers })).text();
6161 expect(page).toContain("Spent $0.50 in this conversation");
62 expect(page).toContain("$0.50 this turn");
62 expect(page).toContain("50¢ this turn");
6363
6464 const api = (await (await fetch(`${servers.url}/api/v1/conversation/turns?conversation=${conv.id}`, { headers })).json()) as {
6565 totalUsd: number | null;
@@ -187,3 +187,10 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Ok.","tota
187187 expect(reportedBefore(app.db, conv.id, "r3", "s2")).toBe(0);
188188 });
189189 });
190
191 test("a turn's cost reads in cents, tenths of a cent below one", () => {
192 expect(cents(0.5)).toBe("50¢");
193 expect(cents(0.0123)).toBe("1¢");
194 expect(cents(0.006)).toBe("0.6¢");
195 expect(spendWords({ usd: 0.52, stopped: false })).toBe("52¢ this turn");
196 });
test/talk-page.test.ts +5 −5
@@ -469,7 +469,7 @@ describe("the page", () => {
469469 // Talk page, because a chart can arrive in a live reply, and a page drawn without them would
470470 // show it unstyled until a reload. 48,328 measured. Then to 50,000 by the reply's one last line
471471 // and the pane's first-version words: 49,466 measured.
472 expect(size).toBeLessThan(54_000);
472 expect(size).toBeLessThan(54_100);
473473 });
474474
475475 describe("the script, driven against a DOM", () => {
@@ -1664,9 +1664,9 @@ describe("the stream", () => {
16641664 app.db.query("DELETE FROM conversation_run WHERE id = ?1").run(id);
16651665 return done.costs.find((c) => c.id === `t-${turn}`)?.html;
16661666 };
1667 expect(await costOf("rs-cut", "Stopped.", 1, 0.006)).toContain("Stopped: $0.006 spent so far");
1667 expect(await costOf("rs-cut", "Stopped.", 1, 0.006)).toContain("Stopped: 0.6¢ spent so far");
16681668 const whole = await costOf("rs-ok", "A whole answer.", 0, 0.25);
1669 expect(whole).toContain("$0.25 this turn");
1669 expect(whole).toContain("25¢ this turn");
16701670 expect(whole).not.toContain("Stopped");
16711671 });
16721672
@@ -1876,7 +1876,7 @@ describe("cost and context (#8822)", () => {
18761876 .query("INSERT INTO conversation_run (id, conversation_id, user_id, prompt, state, cost_usd, tokens_in, tokens_out, context_tokens, context_window, started_at, finished_at) VALUES ('r1',?1,?2,'hello','ok',?3,?4,?5,?6,200000,?7,?8)")
18771877 .run(conversation.id, erin.value.id, 0.1234, 170030, 14, 170020, at, new Date(Date.now() + 1000).toISOString());
18781878 const html = await (await get("/talk", erin.value)).text();
1879 expect(html).toContain("$0.12 this turn");
1879 expect(html).toContain("12¢ this turn");
18801880 expect(html).toContain("Context 85% full (170k of 200k tokens)");
18811881 expect(html).toContain("A new conversation answers faster");
18821882 // A longer window is read from the run, not assumed.
@@ -1886,7 +1886,7 @@ describe("cost and context (#8822)", () => {
18861886 // The stream's end carries both, so a reply that streamed in gets them without a reload.
18871887 const done = (await frames("/talk/stream?after=", erin.value)).find((f) => f.event === "done");
18881888 const sent = JSON.parse(done?.data ?? "{}");
1889 expect(sent.costs[0].html).toContain("$0.12 this turn");
1889 expect(sent.costs[0].html).toContain("12¢ this turn");
18901890 expect(sent.context).toContain("Context 85% full");
18911891 expect(sent.total).toBe("Spent $0.12 in this conversation");
18921892 // Under 80 %: no suggestion.
test/talk-stop.test.ts +3 −3
@@ -111,7 +111,7 @@ test("a stopped answer keeps its text, says stopped with dollars, and is recorde
111111
112112 const l = listTurns(app.db, user.id, conversation.id, {});
113113 const stopped = (l.ok ? l.value : []).find((t) => t.text === STOPPED_LINE)!;
114 expect(turnHtml(stopped, new Set(), { usd: 0.006, stopped: true }).text).toContain("Stopped: $0.006 spent so far");
114 expect(turnHtml(stopped, new Set(), { usd: 0.006, stopped: true }).text).toContain("Stopped: 0.6¢ spent so far");
115115 });
116116
117117 test("a turn cut by a restart is charged its partial spend once; the resumed turn adds only its own", async () => {
@@ -154,6 +154,6 @@ test("a turn cut by a restart is charged its partial spend once; the resumed tur
154154 });
155155
156156 test("the terminal says Stopped with the same words as the page, and keeps 'last turn' for a finished one", () => {
157 expect(lastTurnWords({ usd: 0.006, stopped: true })).toBe("Stopped: $0.006 spent so far");
158 expect(lastTurnWords({ usd: 0.5, stopped: false })).toBe("last turn $0.50");
157 expect(lastTurnWords({ usd: 0.006, stopped: true })).toBe("Stopped: 0.6¢ spent so far");
158 expect(lastTurnWords({ usd: 0.5, stopped: false })).toBe("last turn 50¢");
159159 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs