tiletopia/src/lib/usage.ts
megaproxy c01a4decbf Context bar: show absolute tokens, assume 1M window (% wasn't reliable)
The transcript doesn't record the 200k-vs-1M window (model id is bare,
e.g. claude-opus-4-7; the [1m] in /context is display-only), so the
<200k→200k guess overstated the % for 1M users (a 42k session read 21%
instead of 4%). Fix: the indicator label now shows the absolute token
count (accurate regardless of window), and the fill bar assumes 1M (the
common case here; a 200k-only user would just see the bar read low while
the token number stays correct).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-28 23:09:15 +01:00

47 lines
1.8 KiB
TypeScript

// Helpers for the per-pane context-fill indicator. Context occupancy (token
// count) comes from the backend (src-tauri/src/usage.rs, get_pane_context); this
// turns it into a window %, a colour, and a human label.
import type { SessionContext } from "../ipc";
const WINDOW_LARGE = 1_000_000;
/**
* Assumed context window. The transcript does NOT record whether a session
* runs the 200k or 1M window (the model id is bare, e.g. `claude-opus-4-7` —
* the `[1m]` that claude's /context shows is display-only), so the % can't be
* computed reliably. We assume 1M (the common case here) for the fill bar, and
* the indicator LABEL shows the absolute token count, which is accurate
* regardless of the real window — that's the figure to trust.
*/
export function contextWindow(_contextTokens: number): number {
return WINDOW_LARGE;
}
/** Fraction (0..1) of the inferred window currently occupied. */
export function contextFraction(s: SessionContext): number {
const w = contextWindow(s.contextTokens);
return w > 0 ? Math.min(1, s.contextTokens / w) : 0;
}
export function contextPercent(s: SessionContext): number {
return Math.round(contextFraction(s) * 100);
}
/** Green → amber → red ramp as the window fills. */
export function contextColor(fraction: number): string {
if (fraction >= 0.85) return "#d65a5a";
if (fraction >= 0.6) return "#d6a23a";
return "#5aa84a";
}
export function formatTokens(n: number): string {
if (n >= 1_000_000) return (n / 1_000_000).toFixed(1) + "M";
if (n >= 1_000) return Math.round(n / 1_000) + "k";
return String(n);
}
/** e.g. "274k tokens · ~27% of 1M (last turn)" for a tooltip. */
export function contextLabel(s: SessionContext): string {
return `${formatTokens(s.contextTokens)} tokens · ~${contextPercent(s)}% of 1M (last turn)`;
}