Skip to content

Commit 5f4e20e

Browse files
feat(tui): estimate spend in the mini footer
1 parent 6ae8e17 commit 5f4e20e

13 files changed

Lines changed: 415 additions & 23 deletions

‎packages/tui/src/mini/catalog.shared.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ export async function loadRunReferences(
9898
return result.data.filter((reference) => !reference.hidden)
9999
}
100100

101-
export async function loadRunProviders(
101+
export async function loadRunCatalog(
102102
sdk: OpenCodeClient,
103103
ref: LocationRef,
104104
signal?: AbortSignal,
105-
): Promise<RunProvider[]> {
105+
): Promise<{ providers: RunProvider[]; models: ModelListOutput["data"] }> {
106106
const [providers, models] = await Promise.all([
107107
sdk.provider.list(location(ref), ...requestOptions(signal)),
108108
sdk.model.list(location(ref), ...requestOptions(signal)),
109109
])
110-
return runProviders([...providers.data], [...models.data])
110+
return { providers: runProviders([...providers.data], [...models.data]), models: [...models.data] }
111111
}
112112

113113
export function requestOptions(signal?: AbortSignal): [] | [{ signal: AbortSignal }] {

‎packages/tui/src/mini/footer.view.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,13 @@ export function RunFooterView(props: RunFooterViewProps) {
214214
const current = usage()
215215
return current && current.tokens > 0 ? formatContextUsage(current.tokens, current.percent) : ""
216216
})
217-
const cost = createMemo(() => (usage()?.cost ? money.format(usage()!.cost!) : ""))
217+
const cost = createMemo(() => {
218+
const current = usage()
219+
if (!current) return ""
220+
if (current.cost) return money.format(current.cost)
221+
if (!current.estimate) return ""
222+
return `≈ ${current.estimate < 0.01 ? "<$0.01" : money.format(current.estimate)}`
223+
})
218224
const takeover = createMemo(() => exiting() || (busy() && armed()) || !!props.state().notice.trim())
219225
const footerDetails = createMemo(() => props.miniSettings().footer === "show" && !takeover())
220226
const interruptLabel = createMemo(() => {

‎packages/tui/src/mini/runtime.boot.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,32 @@
44
// needs before the first frame: TUI keymap config, model catalog, and session history for the prompt
55
// history ring. All are async because they read config or hit the SDK, but
66
// none block each other.
7-
import type { LocationRef } from "@opencode/client/promise"
7+
import type { LocationRef, ModelListOutput, SessionMessageInfo } from "@opencode/client/promise"
88
import { resolve } from "../config"
9-
import { loadRunProviders } from "./catalog.shared"
9+
import type { CostSession } from "../util/session"
10+
import { loadRunCatalog } from "./catalog.shared"
1011
import { resolveCurrentSession, sessionHistory } from "./session.shared"
1112
import type { MiniSettings, RunInput, RunPrompt, RunProvider, RunTuiConfig } from "./types"
1213
import { pickVariant } from "./variant.shared"
1314

1415
export type ModelInfo = {
1516
providers: RunProvider[]
17+
models: ModelListOutput["data"]
1618
}
1719

1820
export type SessionInfo = {
1921
first: boolean
2022
history: RunPrompt[]
2123
model?: NonNullable<RunInput["model"]>
2224
variant: string | undefined
25+
session?: CostSession
26+
messages?: SessionMessageInfo[]
2327
}
2428

2529
function emptyModelInfo(): ModelInfo {
2630
return {
2731
providers: [],
32+
models: [],
2833
}
2934
}
3035

@@ -41,7 +46,7 @@ function defaultRunTuiConfig(platform: NodeJS.Platform): RunTuiConfig {
4146
}
4247

4348
async function loadModelInfo(sdk: RunInput["sdk"], location: LocationRef, signal?: AbortSignal): Promise<ModelInfo> {
44-
return { providers: await loadRunProviders(sdk, location, signal) }
49+
return loadRunCatalog(sdk, location, signal)
4550
}
4651

4752
// Fetches available providers and variants.
@@ -70,6 +75,8 @@ export async function resolveSessionInfo(
7075
history: sessionHistory(session),
7176
model: session.model,
7277
variant: pickVariant(model ?? session.model, session),
78+
session: session.session,
79+
messages: session.messages,
7380
}))
7481
.catch(() => emptySessionInfo())
7582
}

‎packages/tui/src/mini/runtime.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import type { LocationRef } from "@opencode/client/promise"
1313
import type { Config } from "../config"
1414
import { newSessionLocation } from "../config/new-session-location"
1515
import { errorMessage } from "../util/error"
16+
import type { CostMessage, CostSession } from "../util/session"
1617
import { loadRunAgents, loadRunCommands, loadRunReferences } from "./catalog.shared"
1718
import {
1819
resolveMiniSettings,
1920
resolveModelInfo,
2021
resolveModelInfoStrict,
2122
resolveRunTuiConfig,
2223
resolveSessionInfo,
24+
type ModelInfo,
2325
} from "./runtime.boot"
2426
import { createRuntimeLifecycle } from "./runtime.lifecycle"
2527
import { cycleVariant, formatModelLabel, resolveVariant } from "./variant.shared"
@@ -118,6 +120,10 @@ type RuntimeState = {
118120
model: RunInput["model"]
119121
defaultModel: RunInput["model"]
120122
providers: RunProvider[]
123+
models: ModelInfo["models"]
124+
sessionInfo?: CostSession
125+
/** Assistant steps observed this run, priced per call by the spend estimate. */
126+
estimateMessages: CostMessage[]
121127
variants: string[]
122128
activeVariant: string | undefined
123129
sessionID: string
@@ -195,8 +201,10 @@ async function runInteractiveRuntime(input: RunRuntimeInput, deps: RunRuntimeDep
195201
const log = input.host.diagnostics.trace
196202
const config = input.config
197203
const configState: { current: MiniSettings } = { current: resolveMiniSettings() }
204+
const experiment = { costEstimate: false }
198205
const tuiConfigTask = resolveRunTuiConfig(input.tuiConfig, input.host.platform).then((tuiConfig) => {
199206
configState.current = resolveMiniSettings(tuiConfig)
207+
experiment.costEstimate = tuiConfig.experimental?.["cost-estimate"] === true
200208
return tuiConfig
201209
})
202210
const ctx = await input.boot()
@@ -214,6 +222,8 @@ async function runInteractiveRuntime(input: RunRuntimeInput, deps: RunRuntimeDep
214222
model: ctx.model ?? session.model,
215223
defaultModel: undefined,
216224
providers: [],
225+
models: [],
226+
estimateMessages: [],
217227
variants: [],
218228
activeVariant: resolveVariant(ctx.variant, session.variant, savedVariant, []),
219229
sessionID: "",
@@ -261,6 +271,7 @@ async function runInteractiveRuntime(input: RunRuntimeInput, deps: RunRuntimeDep
261271
draft.mini[change.key] = change.value
262272
})
263273
configState.current = resolveMiniSettings(info)
274+
experiment.costEstimate = info.experimental?.["cost-estimate"] === true
264275
return configState.current
265276
}
266277
: undefined,
@@ -478,6 +489,8 @@ async function runInteractiveRuntime(input: RunRuntimeInput, deps: RunRuntimeDep
478489
session.history = resumed.history
479490
session.model = resumed.model
480491
session.variant = resumed.variant
492+
state.sessionInfo = resumed.session
493+
state.estimateMessages = resumed.messages ?? []
481494
state.shown = !resumed.first
482495
state.history = [...resumed.history]
483496
state.model = next.model ?? resumed.model
@@ -619,6 +632,7 @@ async function runInteractiveRuntime(input: RunRuntimeInput, deps: RunRuntimeDep
619632
) {
620633
if (!currentClient(attempt)) return
621634
state.providers = info.providers
635+
state.models = info.models
622636
const model = state.model ?? state.defaultModel
623637
state.variants = variantsFor(state.providers, model)
624638
state.activeVariant = boot
@@ -812,6 +826,20 @@ async function runInteractiveRuntime(input: RunRuntimeInput, deps: RunRuntimeDep
812826
onCatalogRefresh: requestCatalogRefresh,
813827
contextLimit: (model) =>
814828
state.providers.find((provider) => provider.id === model.providerID)?.models[model.modelID]?.limit?.context,
829+
spend: {
830+
enabled: experiment.costEstimate,
831+
// A fresh session has no SessionInfo yet; its model still prices the estimate.
832+
session:
833+
state.sessionInfo ??
834+
(state.model && {
835+
id: state.sessionID,
836+
model: { providerID: state.model.providerID, id: state.model.modelID },
837+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
838+
time: { created: 0 },
839+
}),
840+
models: () => state.models,
841+
messages: state.estimateMessages,
842+
},
815843
})
816844
if (footer.isClosed) {
817845
await handle.close()
@@ -956,6 +984,8 @@ async function runInteractiveRuntime(input: RunRuntimeInput, deps: RunRuntimeDep
956984
state.selectSubagent = undefined
957985
state.shown = false
958986
state.sessionID = created.sessionID
987+
state.sessionInfo = undefined
988+
state.estimateMessages = []
959989
state.sessionTitle = created.sessionTitle
960990
shell.setTitle(state.sessionTitle)
961991
state.agent = created.agent ?? state.agent

‎packages/tui/src/mini/session.shared.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SessionMessageInfo, SessionMessageUser } from "@opencode/client/promise"
22
import { projectedPromptInput } from "../prompt/codec"
3+
import type { CostSession } from "../util/session"
34
import { requestOptions } from "./catalog.shared"
45
import { promptCopy, promptSame } from "./prompt.shared"
56
import type { RunInput, RunPrompt } from "./types"
@@ -20,6 +21,9 @@ export type RunSession = {
2021
turns: Turn[]
2122
model?: NonNullable<RunInput["model"]>
2223
variant?: string
24+
/** Session totals and model, priced by the footer spend estimate. */
25+
session?: CostSession
26+
messages?: SessionMessageInfo[]
2327
}
2428

2529
export function messagePrompt(message: Pick<SessionMessageUser, "text" | "files" | "agents" | "skills">): RunPrompt {
@@ -67,6 +71,7 @@ export function createSession(messages: SessionMessages): RunSession {
6771
? [{ prompt: messagePrompt(message), provider: undefined, model: undefined, variant: undefined }]
6872
: [],
6973
),
74+
messages,
7075
}
7176
}
7277

@@ -89,6 +94,14 @@ export async function resolveCurrentSession(
8994
model: session.model?.id,
9095
variant: session.model?.variant,
9196
})),
97+
session: {
98+
id: session.id,
99+
model: session.model && { providerID: session.model.providerID, id: session.model.id },
100+
tokens: session.tokens,
101+
cost: session.cost,
102+
fork: session.fork,
103+
time: { created: session.time.created },
104+
},
92105
...(session.model && {
93106
model: { providerID: session.model.providerID, modelID: session.model.id },
94107
variant: session.model.variant,

‎packages/tui/src/mini/spend.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { CostMessage, CostModel, CostSession } from "../util/session"
2+
import { costEstimate } from "../util/session"
3+
4+
export type SessionSpend = { estimate?: number }
5+
6+
/**
7+
* Catalog-price estimate for the mini footer status line. The opt-in experiment
8+
* only fills in sessions with no recorded cost; a reported cost stays authoritative.
9+
*/
10+
export function sessionSpend(input: {
11+
enabled: boolean
12+
session: CostSession | undefined
13+
models: ReadonlyArray<CostModel> | undefined
14+
messages: ReadonlyArray<CostMessage>
15+
}): SessionSpend {
16+
if (!input.enabled) return {}
17+
const session = input.session
18+
if (!session) return {}
19+
if ((session.cost ?? 0) > 0) return {}
20+
const estimate = costEstimate([session], input.models, () => input.messages)
21+
if (estimate === undefined || estimate <= 0) return {}
22+
return { estimate }
23+
}

‎packages/tui/src/mini/stream-v2.transport.ts‎

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import { messagePrompt } from "./session.shared"
2020
import { createSubagentTracker, toolCommit, toolFinalPhase } from "./stream-v2.subagent"
2121
import { normalizeTool, toolOutputText } from "./tool"
2222
import { toolDisplayContent } from "../util/tool-display"
23+
import { sessionSpend } from "./spend"
24+
import type { CostMessage, CostModel, CostSession } from "../util/session"
2325
import type {
2426
FooterApi,
2527
FooterPatch,
28+
FooterState,
2629
FooterView,
2730
LocalReplayRow,
2831
MiniPermissionRequest,
@@ -58,6 +61,16 @@ type StreamInput = {
5861
signal?: AbortSignal
5962
onCatalogRefresh?: (signal?: AbortSignal) => unknown | Promise<unknown>
6063
contextLimit?: (model: NonNullable<RunInput["model"]>) => number | undefined
64+
/**
65+
* Session-level spend for the status line. When the experiment is enabled and
66+
* the provider reports zero, the catalog estimate replaces the per-step cost.
67+
*/
68+
spend?: {
69+
enabled: boolean
70+
session?: CostSession
71+
models?: () => ReadonlyArray<CostModel> | undefined
72+
messages?: ReadonlyArray<CostMessage>
73+
}
6174
}
6275

6376
export type SessionTurnInput = {
@@ -163,6 +176,16 @@ type State = {
163176
admitted: Set<string>
164177
stepModel: RunInput["model"]
165178
activeCompaction?: string
179+
spend?: {
180+
enabled: boolean
181+
session?: CostSession
182+
models?: () => ReadonlyArray<CostModel> | undefined
183+
messages: CostMessage[]
184+
/** Step event IDs already priced, so a replayed step cannot add its tier twice. */
185+
steps: Set<string>
186+
}
187+
/** Tokens, context percent, and reported cost of the most recent step. */
188+
lastStep?: { tokens: number; percent?: number; cost?: number }
166189
}
167190

168191
export function formatUnknownError(error: unknown): string {
@@ -511,6 +534,7 @@ export async function createSessionTransport(input: StreamInput): Promise<Sessio
511534
pending: new Map(),
512535
admitted: new Set(),
513536
stepModel: undefined,
537+
spend: input.spend && { ...input.spend, messages: [...(input.spend.messages ?? [])], steps: new Set() },
514538
}
515539
let readyResolve!: () => void
516540
let readyReject!: (error: unknown) => void
@@ -580,6 +604,20 @@ export async function createSessionTransport(input: StreamInput): Promise<Sessio
580604
)
581605
}
582606

607+
const usageFor = (): FooterState["usage"] => {
608+
const step = state.lastStep
609+
if (!step) return undefined
610+
const spend = state.spend
611+
if (!spend || step.cost !== undefined) return step
612+
const current = sessionSpend({
613+
enabled: spend.enabled,
614+
session: spend.session,
615+
models: spend.models?.(),
616+
messages: spend.messages,
617+
})
618+
return { ...step, estimate: current.estimate }
619+
}
620+
583621
let syncedPending: string[] | undefined
584622
const syncPending = () => {
585623
const prompts = [...state.pending.values()]
@@ -1376,6 +1414,23 @@ export async function createSessionTransport(input: StreamInput): Promise<Sessio
13761414
syncBlockers()
13771415
return
13781416
}
1417+
if (event.type === "session.usage.updated") {
1418+
const spend = state.spend
1419+
if (!spend?.session) return
1420+
spend.session = { ...spend.session, cost: event.data.cost, tokens: event.data.tokens }
1421+
if (state.lastStep) write([], { usage: usageFor() })
1422+
return
1423+
}
1424+
if (event.type === "session.model.selected") {
1425+
const spend = state.spend
1426+
if (!spend?.session) return
1427+
spend.session = {
1428+
...spend.session,
1429+
model: { providerID: event.data.model.providerID, id: event.data.model.id },
1430+
}
1431+
if (state.lastStep) write([], { usage: usageFor() })
1432+
return
1433+
}
13791434
if (event.type === "session.step.ended") {
13801435
const total =
13811436
event.data.tokens.input +
@@ -1386,16 +1441,19 @@ export async function createSessionTransport(input: StreamInput): Promise<Sessio
13861441
const limit = state.stepModel ? input.contextLimit?.(state.stepModel) : undefined
13871442
state.stepModel = undefined
13881443
if (!showTools()) flushQuietText(event.data.assistantMessageID)
1389-
write([], {
1390-
usage:
1391-
total > 0 || event.data.cost
1392-
? {
1393-
tokens: total,
1394-
percent: limit ? Math.round((total / limit) * 100) : undefined,
1395-
cost: event.data.cost || undefined,
1396-
}
1397-
: undefined,
1398-
})
1444+
state.lastStep =
1445+
total > 0 || event.data.cost
1446+
? {
1447+
tokens: total,
1448+
percent: limit ? Math.round((total / limit) * 100) : undefined,
1449+
cost: event.data.cost || undefined,
1450+
}
1451+
: undefined
1452+
if (state.spend && !state.spend.steps.has(event.id)) {
1453+
state.spend.steps.add(event.id)
1454+
state.spend.messages.push({ type: "assistant", tokens: event.data.tokens, time: { created: event.created } })
1455+
}
1456+
write([], { usage: usageFor() })
13991457
return
14001458
}
14011459
if (event.type === "session.step.failed") {

0 commit comments

Comments
 (0)