Skip to content

Commit 169fe04

Browse files
committed
fix(tui): address catalog cost estimate review
1 parent 1a082e6 commit 169fe04

6 files changed

Lines changed: 128 additions & 55 deletions

File tree

‎packages/tui/src/feature-plugins/prompt/footer.tsx‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,26 @@ export function PromptFooter(props: {
4646
session.revert?.messageID,
4747
)
4848
const cost = props.context.data.session.cost(props.sessionID)
49-
const estimate = enabled()
50-
? costEstimate(
51-
session.parentID
52-
? [session]
53-
: props.context.data.session
54-
.family(props.sessionID)
55-
.map((id) => props.context.data.session.get(id))
56-
.filter((info) => info !== undefined),
57-
props.context.data.location.model.list(session.location),
58-
(id) => props.context.data.session.message.list(id),
59-
)
60-
: 0
49+
const estimate =
50+
cost <= 0 && enabled()
51+
? costEstimate(
52+
session.parentID
53+
? [session]
54+
: props.context.data.session
55+
.family(props.sessionID)
56+
.map((id) => props.context.data.session.get(id))
57+
.filter((info) => info !== undefined),
58+
props.context.data.location.model.list(session.location),
59+
(id) => props.context.data.session.message.list(id),
60+
)
61+
: 0
6162
return [
6263
usage ? formatContextUsage(usage.tokens, usage.percent) : undefined,
63-
cost > 0 ? money.format(cost) : estimate > 0 ? `≈ ${money.format(estimate)}` : undefined,
64+
cost > 0
65+
? money.format(cost)
66+
: estimate !== undefined && estimate > 0
67+
? `≈ ${estimate < 0.01 ? "<$0.01" : money.format(estimate)}`
68+
: undefined,
6469
].filter((item): item is string => Boolean(item))
6570
})
6671
const live = createMemo(() => Boolean(subagents() || shells()))

‎packages/tui/src/feature-plugins/sidebar/context.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function SidebarContext(props: { context: Plugin.Context; sessionID: stri
2020
)
2121

2222
const estimate = createMemo(() => {
23-
if (!enabled()) return 0
23+
if (cost() > 0 || !enabled()) return 0
2424
const info = session()
2525
if (!info) return 0
2626
const data = props.context.data.session
@@ -39,7 +39,8 @@ export function SidebarContext(props: { context: Plugin.Context; sessionID: stri
3939
const spent = createMemo(() => {
4040
if (cost() > 0) return `${money.format(cost())} spent`
4141
// The tilde marks the catalog-price figure as an estimate; the recorded cost stays authoritative.
42-
return estimate() > 0 ? `≈ ${money.format(estimate())} spent` : undefined
42+
const value = estimate()
43+
return value !== undefined && value > 0 ? `≈ ${value < 0.01 ? "<$0.01" : money.format(value)} spent` : undefined
4344
})
4445

4546
return (

‎packages/tui/src/util/session.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ModelInfo, SessionInfo, SessionMessageInfo } from "@opencode/client"
22
import { lastAssistantWithUsage } from "@opencode/client/solid"
33
import { Model } from "@opencode/schema/model"
4+
import { Money } from "@opencode/schema/money"
45
import { Locale } from "./locale"
56

67
type SessionNode = {
@@ -71,21 +72,24 @@ export function costEstimate(
7172
models: ReadonlyArray<ModelInfo> | undefined,
7273
messages: (sessionID: string) => ReadonlyArray<SessionMessageInfo>,
7374
) {
74-
return sessions.reduce((total, session) => {
75+
return sessions.reduce<Money.USD | undefined>((total, session) => {
76+
if (total === undefined) return
7577
const model = models?.find(
7678
(candidate) =>
7779
session.model && candidate.providerID === session.model.providerID && candidate.id === session.model.id,
7880
)
79-
if (!model) return total
81+
if (!model || model.cost.length === 0) return
8082
const base = model.cost.filter((cost) => cost.tier === undefined)
81-
const estimate = total + Model.calculateCost(base, session.tokens)
83+
const estimate = Money.USD.make(total + Model.calculateCost(base, session.tokens))
8284
if (!model.cost.some((cost) => cost.tier !== undefined)) return estimate
8385
// Only assistant messages retain individual call usage. Title, compaction, and unloaded usage stay at base rates.
8486
return messages(session.id).reduce((sum, message) => {
8587
if (message.type !== "assistant" || !message.tokens) return sum
8688
// Forks copy parent timestamps and messages, but start with zero recorded usage.
8789
if (session.fork && message.time.created <= session.time.created) return sum
88-
return sum + Model.calculateCost(model.cost, message.tokens) - Model.calculateCost(base, message.tokens)
90+
return Money.USD.make(
91+
sum + Model.calculateCost(model.cost, message.tokens) - Model.calculateCost(base, message.tokens),
92+
)
8993
}, estimate)
90-
}, 0)
94+
}, Money.USD.zero)
9195
}

‎packages/tui/test/feature-plugins/prompt-footer.test.tsx‎

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,16 @@ test("prompt footer shows the session's goal without its objective and opens it
241241
}
242242
})
243243

244-
test.each([
245-
{ sessionID: "session", expected: "≈ $12.60" },
246-
{ sessionID: "child", expected: "≈ $1.50" },
247-
])("prompt footer estimates $sessionID spend", async ({ sessionID, expected }) => {
244+
test.each(
245+
[
246+
{ sessionID: "session", expected: "≈ $12.60" },
247+
{ sessionID: "child", expected: "≈ $1.50" },
248+
{ sessionID: "session", scale: 0.0001, expected: "≈ <$0.01" },
249+
{ sessionID: "session", cost: 1, expected: "$1.00" },
250+
{ sessionID: "session", experiment: false, expected: undefined },
251+
{ sessionID: "session", unpriced: true, expected: undefined },
252+
].map((options) => ({ cost: 0, experiment: true, scale: 1, unpriced: false, ...options })),
253+
)("prompt footer spend: $expected ($sessionID)", async ({ sessionID, expected, cost, experiment, scale, unpriced }) => {
248254
const color = RGBA.fromInts(200, 200, 200)
249255
const context = {
250256
location: { directory: "/workspace" },
@@ -256,18 +262,31 @@ test.each([
256262
session: {
257263
family: () => ["session", "child", "sibling"],
258264
status: () => "idle",
259-
get: (id: string) => ({
260-
id,
261-
parentID: id === "session" ? undefined : "session",
262-
location: { directory: "/workspace" },
263-
model: { providerID: "provider", id: "model" },
264-
tokens:
265-
id === "session"
266-
? { input: 1_000_000, output: 100_000, reasoning: 0, cache: { read: 2_000_000, write: 0 } }
267-
: { input: id === "child" ? 500_000 : 2_000_000, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
268-
cost: 0,
269-
}),
270-
cost: () => 0,
265+
get: (id: string) => {
266+
if (id !== sessionID && (cost > 0 || !experiment)) throw new Error("Unused estimate reads family sessions")
267+
return {
268+
id,
269+
parentID: id === "session" ? undefined : "session",
270+
location: { directory: "/workspace" },
271+
model: { providerID: "provider", id: unpriced && id === "child" ? "removed" : "model" },
272+
tokens:
273+
id === "session"
274+
? {
275+
input: 1_000_000 * scale,
276+
output: 100_000 * scale,
277+
reasoning: 0,
278+
cache: { read: 2_000_000 * scale, write: 0 },
279+
}
280+
: {
281+
input: (id === "child" ? 500_000 : 2_000_000) * scale,
282+
output: 0,
283+
reasoning: 0,
284+
cache: { read: 0, write: 0 },
285+
},
286+
cost: 0,
287+
}
288+
},
289+
cost: () => cost,
271290
message: { list: () => [] },
272291
},
273292
shell: { list: () => [] },
@@ -286,7 +305,7 @@ test.each([
286305
} as unknown as Context
287306
const app = await testRender(
288307
() => (
289-
<ConfigProvider config={createTuiResolvedConfig({ experimental: { "cost-estimate": true } })}>
308+
<ConfigProvider config={createTuiResolvedConfig({ experimental: { "cost-estimate": experiment } })}>
290309
<PromptFooter context={context} sessionID={sessionID} mode="normal" showDetails={true} />
291310
</ConfigProvider>
292311
),
@@ -298,7 +317,10 @@ test.each([
298317

299318
try {
300319
await app.renderOnce()
301-
expect(app.captureCharFrame()).toContain(expected)
320+
const frame = app.captureCharFrame()
321+
if (expected) expect(frame).toContain(expected)
322+
if (!expected) expect(frame).not.toContain("$")
323+
if (cost > 0) expect(frame).not.toContain("≈")
302324
} finally {
303325
app.renderer.destroy()
304326
}

‎packages/tui/test/feature-plugins/sidebar-context.test.tsx‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,27 @@ test("sidebar shows available context usage", async () => {
101101
}
102102
})
103103

104-
test("sidebar estimates spend from catalog prices when the provider reports $0", async () => {
105-
const app = await render(
106-
context({
107-
experiment: true,
108-
family: [{ tokens: usage(1_000_000, 100_000, 2_000_000) }, { parentID: "session", tokens: usage(0, 10_000) }],
109-
}),
110-
)
104+
test.each([
105+
{ input: 1_000_000, output: 100_000, read: 2_000_000, childOutput: 10_000, expected: "≈ $5.25 spent" },
106+
{ input: 100, output: 0, read: 0, childOutput: 0, expected: "≈ <$0.01 spent" },
107+
])(
108+
"sidebar estimates $expected when the provider reports $0",
109+
async ({ input, output, read, childOutput, expected }) => {
110+
const app = await render(
111+
context({
112+
experiment: true,
113+
family: [{ tokens: usage(input, output, read) }, { parentID: "session", tokens: usage(0, childOutput) }],
114+
}),
115+
)
111116

112-
try {
113-
await app.renderOnce()
114-
expect(app.captureCharFrame()).toContain("≈ $5.25 spent")
115-
} finally {
116-
app.renderer.destroy()
117-
}
118-
})
117+
try {
118+
await app.renderOnce()
119+
expect(app.captureCharFrame()).toContain(expected)
120+
} finally {
121+
app.renderer.destroy()
122+
}
123+
},
124+
)
119125

120126
test("sidebar keeps the reported cost over the estimate", async () => {
121127
const app = await render(context({ experiment: true, cost: 1, family: [{ tokens: usage(1_000_000) }] }))

‎packages/tui/test/util/session.test.ts‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, test } from "bun:test"
22
import type { ModelInfo, SessionInfo, SessionMessageInfo } from "@opencode/client"
3+
import { Money } from "@opencode/schema/money"
34
import { costEstimate, sessionFamily } from "../../src/util/session"
45

56
const ref = { providerID: "provider", id: "model" }
@@ -20,6 +21,7 @@ const tiered: ModelInfo = {
2021
cost: [
2122
...pricing.cost,
2223
{ tier: { type: "context", size: 200_000 }, input: 6, output: 22.5, cache: { read: 0.6, write: 7.5 } },
24+
{ tier: { type: "context", size: 400_000 }, input: 9, output: 30, cache: { read: 0.9, write: 11.25 } },
2325
],
2426
}
2527

@@ -132,9 +134,42 @@ describe("util.session", () => {
132134
).toBeCloseTo(1.8)
133135
})
134136

135-
test("skips sessions without matching catalog pricing", () => {
137+
test.each([
138+
{ tokens: 200_000, expected: 0.6 },
139+
{ tokens: 200_001, expected: 1.200006 },
140+
{ tokens: 400_000, expected: 2.4 },
141+
{ tokens: 400_001, expected: 3.600009 },
142+
])("selects the largest matching tier for $tokens input tokens", ({ tokens, expected }) => {
143+
expect(
144+
costEstimate([session("root", input(tokens / 1_000_000))], [tiered], () => [message(input(tokens / 1_000_000))]),
145+
).toBeCloseTo(expected, 8)
146+
})
147+
148+
test("prices each family member with its own provider and model", () => {
149+
const other = { ...pricing, providerID: "other", id: "other-model", cost: [{ ...pricing.cost[0]!, input: 10 }] }
150+
expect(
151+
costEstimate(
152+
[session("root", input(1)), { ...session("child", input(0.5)), model: other }],
153+
[pricing, other],
154+
() => [],
155+
),
156+
).toBe(Money.USD.make(8))
157+
})
158+
159+
test("omits estimates when any scoped session lacks catalog pricing", () => {
136160
const sessions = [session("root", input(1))]
137-
expect(costEstimate(sessions, undefined, () => [])).toBe(0)
138-
expect(costEstimate(sessions, [{ ...pricing, providerID: "other" }], () => [])).toBe(0)
161+
expect(costEstimate(sessions, undefined, () => [])).toBeUndefined()
162+
expect(costEstimate(sessions, [{ ...pricing, providerID: "other" }], () => [])).toBeUndefined()
163+
expect(costEstimate(sessions, [{ ...pricing, cost: [] }], () => [])).toBeUndefined()
164+
expect(
165+
costEstimate(
166+
[...sessions, { ...session("child", input(0.5)), model: { ...ref, id: "removed" } }],
167+
[pricing],
168+
() => [],
169+
),
170+
).toBeUndefined()
171+
expect(
172+
costEstimate(sessions, [{ ...pricing, cost: [{ input: 0, output: 0, cache: { read: 0, write: 0 } }] }], () => []),
173+
).toBe(Money.USD.zero)
139174
})
140175
})

0 commit comments

Comments
 (0)