fix(tui): show context window usage in footer (#5679)
* fix(tui): show context window usage in footer * refactor(tui): simplify context footer usage
This commit is contained in:
1 parent
192e2e90dc
commit
5eb9039f0e
4 files changed
+33
-110
No files matched your search
+15
-59
@@ -1,7 +1,5 @@
|
||||
import { RGBA, StyledText, TextAttributes, type TextChunk } from "@opentui/core"
|
||||
|
||||
import type { TokenUsage } from "./protocol"
|
||||
|
||||
export interface FooterHint {
|
||||
key: string
|
||||
label: string
|
||||
@@ -37,66 +35,24 @@ export function contextualFooterHints(
|
||||
return footerHints(hintsFor(mode, width), theme)
|
||||
}
|
||||
|
||||
/** Last-turn model telemetry. Passive chrome reports the system, not its manual. */
|
||||
/** Latest measured request's context-window occupancy. */
|
||||
export function footerTelemetry(
|
||||
usage: TokenUsage | null,
|
||||
width: number,
|
||||
contextTokens: number | null,
|
||||
contextWindowTokens: number | null,
|
||||
theme: FooterHintTheme,
|
||||
): StyledText {
|
||||
if (!usage) return new StyledText([])
|
||||
const parts: string[] = []
|
||||
const duration = usage.generation_ms
|
||||
const measured = usage.measured_completion_tokens
|
||||
if (typeof duration === "number" && duration > 0 && typeof measured === "number") {
|
||||
const rate = measured * 1000 / duration
|
||||
const value = rate < 10 ? rate.toFixed(1) : String(Math.round(rate))
|
||||
const estimated = (usage.estimated_tokens || 0) > 0 ? "~" : ""
|
||||
parts.push(`${estimated}${value} tok/s`)
|
||||
}
|
||||
const cacheHitRate = (
|
||||
typeof usage.cached_tokens === "number"
|
||||
&& typeof usage.prompt_tokens === "number"
|
||||
&& usage.prompt_tokens > 0
|
||||
)
|
||||
? Math.min(100, Math.max(0, Math.round(
|
||||
usage.cached_tokens * 100 / usage.prompt_tokens,
|
||||
)))
|
||||
: null
|
||||
if (width >= 72) {
|
||||
const prompt = usage.prompt_tokens
|
||||
const completion = usage.completion_tokens
|
||||
if (typeof prompt === "number" || typeof completion === "number") {
|
||||
const cache = cacheHitRate === null ? "" : ` (${cacheHitRate}% cached)`
|
||||
parts.push(`${formatTelemetryTokens(prompt || 0)} in${cache}`)
|
||||
parts.push(`${formatTelemetryTokens(completion || 0)} out`)
|
||||
}
|
||||
} else if (cacheHitRate !== null) {
|
||||
parts.push(`${cacheHitRate}% cached`)
|
||||
}
|
||||
if (width >= 128 && typeof usage.cost_usd === "number" && usage.cost_usd > 0) {
|
||||
parts.push(`$${usage.cost_usd < 0.01 ? usage.cost_usd.toFixed(4) : usage.cost_usd.toFixed(2)}`)
|
||||
}
|
||||
return footerMetrics(parts, theme)
|
||||
}
|
||||
|
||||
function formatTelemetryTokens(value: number): string {
|
||||
const count = Math.max(0, value)
|
||||
for (const [threshold, suffix] of [[1_000_000_000, "B"], [1_000_000, "M"], [1_000, "K"]] as const) {
|
||||
if (count < threshold) continue
|
||||
const scaled = count / threshold
|
||||
const compact = scaled >= 10 ? Math.round(scaled) : Math.round(scaled * 10) / 10
|
||||
return `${compact}${suffix}`
|
||||
}
|
||||
return String(Math.round(count))
|
||||
}
|
||||
|
||||
function footerMetrics(parts: readonly string[], theme: FooterHintTheme): StyledText {
|
||||
const chunks: TextChunk[] = []
|
||||
parts.forEach((text, index) => {
|
||||
if (index) chunks.push(chunk(" · ", theme.separator))
|
||||
chunks.push(chunk(text, index === 0 ? theme.accent : theme.muted, index === 0))
|
||||
})
|
||||
return new StyledText(chunks)
|
||||
if (
|
||||
typeof contextTokens !== "number"
|
||||
|| !Number.isFinite(contextTokens)
|
||||
|| contextTokens < 0
|
||||
|| typeof contextWindowTokens !== "number"
|
||||
|| !Number.isFinite(contextWindowTokens)
|
||||
|| contextWindowTokens <= 0
|
||||
) return new StyledText([])
|
||||
const percentage = Math.min(100, Math.round(
|
||||
contextTokens * 100 / contextWindowTokens,
|
||||
))
|
||||
return new StyledText([chunk(`${percentage}% context`, theme.accent, true)])
|
||||
}
|
||||
|
||||
/** Give shortcuts visual hierarchy without turning the footer into a toolbar. */
|
||||
|
||||
Reference in new issue
Block a user