Public: anyone can read this; the crew can change it.
A Talk reply shows what its turn cost, in cents, under the reply #16160
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/16143-a-talk-reply-shows-what-its-turn-cost-in → main 0 commits fork 1 thread · 1 unresolved
6 files
changed +29
−14
src/core/conversation_cost.ts +9 −1
| @@ -25,7 +25,7 @@ export const spendWords = (spend: Spend): string => { | ||
| 25 | 25 | // A chosen model the price table does not know has no cost to state: its name alone, never $0. |
| 26 | 26 | const on = spend.model === undefined ? "" : spend.usd > 0 ? ` · ${spend.model}` : spend.model; |
| 27 | 27 | 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}`; | |
| 29 | 29 | }; |
| 30 | 30 | |
| 31 | 31 | /** |
| @@ -98,3 +98,11 @@ export const contextText = (c: Context): string => | ||
| 98 | 98 | |
| 99 | 99 | /** Dollars as a quiet figure: cents below a dollar, a tenth of a cent below that. */ |
| 100 | 100 | 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}¢`; | |
| 108 | }; |
src/tui/ssh-talk.ts +2 −2
| @@ -32,7 +32,7 @@ | ||
| 32 | 32 | |
| 33 | 33 | import { cancelTalk, queuedTalk, TALK_WORDS, stopTalk, talk, talkAgain } from "../actions/talk.ts"; |
| 34 | 34 | 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"; | |
| 36 | 36 | import { liveCrewRows } from "../actions/crew_now.ts"; |
| 37 | 37 | import { crewLine } from "../web/crew_now.ts"; |
| 38 | 38 | import { lastExchange, openConversation } from "../core/conversations.ts"; |
| @@ -200,7 +200,7 @@ export function serveTalk(app: App, session: SshSession, user: User | undefined, | ||
| 200 | 200 | /** |
| 201 | 201 | * The line the terminal prints for the turn before: the page's Stopped wording when it was stopped. |
| 202 | 202 | */ |
| 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}`}`); | |
| 204 | 204 | |
| 205 | 205 | /** `talk-again <edit words | regenerate | retry>`; undefined when the command is not one. */ |
| 206 | 206 | 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 | ||
| 312 | 312 | .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)") |
| 313 | 313 | .run(conversation.id, id, at, new Date(Date.now() + 1000).toISOString()); |
| 314 | 314 | 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¢"); | |
| 316 | 316 | expect(r.out).toMatch(/Spent \$0\.5\d in this conversation/); |
| 317 | 317 | expect(r.out).toContain("Context 50% full (500k of 1M tokens)"); |
| 318 | 318 | 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"; | ||
| 10 | 10 | import { join } from "node:path"; |
| 11 | 11 | import { createApp, type App } from "../src/app.ts"; |
| 12 | 12 | 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"; | |
| 14 | 14 | import { PLATFORM } from "../src/core/subscriptions.ts"; |
| 15 | 15 | import { reportedBefore, say } from "../src/crew/conversation.ts"; |
| 16 | 16 | import { createSession } from "../src/core/sessions.ts"; |
| @@ -59,7 +59,7 @@ describe("talk cost", () => { | ||
| 59 | 59 | const headers = { cookie: `forge_session=${createSession(app.db, alice.id).token}` }; |
| 60 | 60 | const page = await (await fetch(`${servers.url}/talk/${conv.id}`, { headers })).text(); |
| 61 | 61 | expect(page).toContain("Spent $0.50 in this conversation"); |
| 62 | expect(page).toContain("$0.50 this turn"); | |
| 62 | expect(page).toContain("50¢ this turn"); | |
| 63 | 63 | |
| 64 | 64 | const api = (await (await fetch(`${servers.url}/api/v1/conversation/turns?conversation=${conv.id}`, { headers })).json()) as { |
| 65 | 65 | totalUsd: number | null; |
| @@ -187,3 +187,10 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"Ok.","tota | ||
| 187 | 187 | expect(reportedBefore(app.db, conv.id, "r3", "s2")).toBe(0); |
| 188 | 188 | }); |
| 189 | 189 | }); |
| 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", () => { | ||
| 469 | 469 | // Talk page, because a chart can arrive in a live reply, and a page drawn without them would |
| 470 | 470 | // show it unstyled until a reload. 48,328 measured. Then to 50,000 by the reply's one last line |
| 471 | 471 | // and the pane's first-version words: 49,466 measured. |
| 472 | expect(size).toBeLessThan(54_000); | |
| 472 | expect(size).toBeLessThan(54_100); | |
| 473 | 473 | }); |
| 474 | 474 | |
| 475 | 475 | describe("the script, driven against a DOM", () => { |
| @@ -1664,9 +1664,9 @@ describe("the stream", () => { | ||
| 1664 | 1664 | app.db.query("DELETE FROM conversation_run WHERE id = ?1").run(id); |
| 1665 | 1665 | return done.costs.find((c) => c.id === `t-${turn}`)?.html; |
| 1666 | 1666 | }; |
| 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"); | |
| 1668 | 1668 | 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"); | |
| 1670 | 1670 | expect(whole).not.toContain("Stopped"); |
| 1671 | 1671 | }); |
| 1672 | 1672 | |
| @@ -1876,7 +1876,7 @@ describe("cost and context (#8822)", () => { | ||
| 1876 | 1876 | .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)") |
| 1877 | 1877 | .run(conversation.id, erin.value.id, 0.1234, 170030, 14, 170020, at, new Date(Date.now() + 1000).toISOString()); |
| 1878 | 1878 | const html = await (await get("/talk", erin.value)).text(); |
| 1879 | expect(html).toContain("$0.12 this turn"); | |
| 1879 | expect(html).toContain("12¢ this turn"); | |
| 1880 | 1880 | expect(html).toContain("Context 85% full (170k of 200k tokens)"); |
| 1881 | 1881 | expect(html).toContain("A new conversation answers faster"); |
| 1882 | 1882 | // A longer window is read from the run, not assumed. |
| @@ -1886,7 +1886,7 @@ describe("cost and context (#8822)", () => { | ||
| 1886 | 1886 | // The stream's end carries both, so a reply that streamed in gets them without a reload. |
| 1887 | 1887 | const done = (await frames("/talk/stream?after=", erin.value)).find((f) => f.event === "done"); |
| 1888 | 1888 | 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"); | |
| 1890 | 1890 | expect(sent.context).toContain("Context 85% full"); |
| 1891 | 1891 | expect(sent.total).toBe("Spent $0.12 in this conversation"); |
| 1892 | 1892 | // 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 | ||
| 111 | 111 | |
| 112 | 112 | const l = listTurns(app.db, user.id, conversation.id, {}); |
| 113 | 113 | 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"); | |
| 115 | 115 | }); |
| 116 | 116 | |
| 117 | 117 | 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 | ||
| 154 | 154 | }); |
| 155 | 155 | |
| 156 | 156 | 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¢"); | |
| 159 | 159 | }); |
new above
offline
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.