@@ -37,8 +37,9 @@ import { usePromptStash } from "../../prompt/stash"
3737import { DialogStash } from "../dialog-stash"
3838import { type AutocompleteRef , Autocomplete } from "./autocomplete"
3939import { useRenderer , useTerminalDimensions , type JSX } from "@opentui/solid"
40- import type { AssistantMessage , FilePart , UserMessage } from "@opencode-ai/sdk/v2"
40+ import type { FilePart , UserMessage } from "@opencode-ai/sdk/v2"
4141import { Locale } from "../../util/locale"
42+ import { contextUsage } from "../../util/context-usage"
4243import { errorMessage } from "../../util/error"
4344import { formatDuration } from "../../util/format"
4445import { createColors , createFrames } from "../../ui/spinner"
@@ -266,19 +267,13 @@ export function Prompt(props: PromptProps) {
266267 const usage = createMemo ( ( ) => {
267268 if ( ! props . sessionID ) return
268269 const session = sync . session . get ( props . sessionID )
269- const msg = sync . data . message [ props . sessionID ] ?? [ ]
270- const last = msg . findLast ( ( item ) : item is AssistantMessage => item . role === "assistant" && item . tokens . output > 0 )
271- if ( ! last ) return
270+ const found = contextUsage ( sync . data . message [ props . sessionID ] ?? [ ] , sync . data . provider )
271+ if ( ! found || found . tokens <= 0 ) return
272272
273- const tokens =
274- last . tokens . input + last . tokens . output + last . tokens . reasoning + last . tokens . cache . read + last . tokens . cache . write
275- if ( tokens <= 0 ) return
276-
277- const model = sync . data . provider . find ( ( item ) => item . id === last . providerID ) ?. models [ last . modelID ]
278- const pct = model ?. limit . context ? `${ Math . round ( ( tokens / model . limit . context ) * 100 ) } %` : undefined
273+ const pct = found . percent === null ? undefined : `${ found . percent } %`
279274 const cost = session ?. cost ?? 0
280275 return {
281- context : pct ? `${ Locale . number ( tokens ) } (${ pct } )` : Locale . number ( tokens ) ,
276+ context : pct ? `${ Locale . number ( found . tokens ) } (${ pct } )` : Locale . number ( found . tokens ) ,
282277 cost : cost > 0 ? money . format ( cost ) : undefined ,
283278 }
284279 } )
0 commit comments