// What the context meter says, as the terminal footer's does: how full the // context is, how much of the prompt a request would read from cache, and // how the prepared context splits across system, messages, tool output, and media. import type { components } from './api.gen'; export type Usage = components['schemas']['Usage']; export type ContextSummary = components['schemas']['ContextSummary']; export type HistoryEntry = components['schemas']['HistoryEntry']; export type Size = components['schemas']['Size']; export type MediaTokens = components['schemas']['ContextMediaTokens']; export type EffortTone = 'none' | 'low' | 'medium' | 'high' | 'xhigh'; /** Maps a provider effort identifier to one of the five visual effort tiers. */ export function effortTone( level: string | null | undefined, ): EffortTone | null { switch (level) { case 'none': case 'off': return 'none'; case 'minimal': case 'extra-low': case 'low': return 'low'; case 'medium': return 'medium'; case 'high': return 'high'; case 'xhigh': case 'max': return 'xhigh'; default: return null; } } export type SegmentKey = 'sys' | 'msg' | 'tool' | 'img' | 'audio' | 'video'; export interface ContextSegment { key: SegmentKey; label: string; tokens: number; color: string; /** Width share (0..100) inside the used portion, with a gentle perceptual curve (`tokens^0.58`). */ perceptualPct: number; } interface EntryCost { msgBytes: number; toolBytes: number; media: MediaTokens; } const entryCostCache = new WeakMap(); const entryIdCostCache = new Map(); /** What the context holds after a reply: the total, or the prompt and completion when no total was reported. */ export function contextTokens(usage: Usage): number | null { if (usage.total_tokens !== null && usage.total_tokens >= 0) return usage.total_tokens; if (usage.prompt_tokens === null || usage.prompt_tokens < 0) return null; return usage.prompt_tokens + Math.max(0, usage.completion_tokens ?? 0); } /** What a request would read from cache at `now`: the measured count until the cache starts to fade, then each step it has reached. */ export function cachedNow(usage: Usage, now: number): number | null { let cached = usage.cached_prompt_tokens; for (const step of usage.cache_fade) { if (Date.parse(step.at) > now) break; cached = step.cached_tokens; } return cached !== null && cached >= 0 ? cached : null; } /** When the cache next fades, so a view re-renders then and not on a timer; null when it has settled. */ export function nextFade(usage: Usage, now: number): number | null { for (const step of usage.cache_fade) { const at = Date.parse(step.at); if (at > now) return at; } return null; } /** When the prompt cache completely expires (`cached_tokens === 0` or `null`), if a fade schedule was recorded. */ export function cacheExpiryAt(usage: Usage): number | null { let expiry: number | null = null; for (const step of usage.cache_fade) { if (step.cached_tokens === null || step.cached_tokens <= 0) { const at = Date.parse(step.at); if (!Number.isNaN(at)) { expiry = at; break; } } } return expiry; } /** Whether the prompt cache is still warm at `now`. */ export function isCacheWarm(usage: Usage, now: number): boolean { const current = cachedNow(usage, now); if (current !== null && current > 0) return true; if (!usage.cache_fade.length) return ( (usage.cached_prompt_tokens ?? 0) > 0 || (usage.cache_write_tokens ?? 0) > 0 ); return false; } /** Compact countdown string (`3m 50s`, `45s`, `18m`) until `targetMs`. */ export function formatCountdown(targetMs: number, now: number): string { const sec = Math.max(0, Math.ceil((targetMs - now) / 1000)); if (sec < 60) return `${sec}s`; const min = Math.floor(sec / 60); const rem = sec % 60; if (min >= 60) { const hr = Math.floor(min / 60); const m = min % 60; return m > 0 ? `${hr}h ${m}m` : `${hr}h`; } return rem > 0 ? `${min}m ${rem}s` : `${min}m`; } /** Provider tokens per thousand estimated ones, as ×1.31. */ function factor(perMille: number): string { return `×${(perMille / 1000).toFixed(2)}`; } /** * What a size is made of, in the words a person needs to trust it: the part a * provider measured, said as counted or reported, and the part that was only * estimated, with the correction it used. The daemon sends the two parts * apart; this only phrases them. */ export function sizeParts(size: Size): string[] { const parts: string[] = []; if (size.measured) { parts.push( `${shortCount(size.measured.tokens)} ${ size.measured.source === 'count_endpoint' ? 'counted by the provider' : 'from reported usage' }`, ); } if (size.estimated) { const correction = size.estimated.correction; const how = !correction ? '' : correction.state === 'calibrated' ? `, corrected ${factor(correction.per_mille)} from reported usage` : `, provisional correction ${factor(correction.per_mille)} after ${correction.reports ?? 0} report${correction.reports === 1 ? '' : 's'}`; parts.push(`${shortCount(size.estimated.tokens)} estimated${how}`); } return parts; } /** The percent of the window the context fills. */ export function share(tokens: number, window: number): number { return Math.round((100 * tokens) / window); } /** Usage a different model reported says nothing about the model in use now. */ export function stale(usage: Usage, model: string | null): boolean { return usage.model !== null && model !== null && usage.model !== model; } /** Token counts to about three digits: 812, 4.2k, 392k, 1.2m. */ export function shortCount(n: number): string { if (n < 1_000) return String(n); if (n < 10_000) return `${(n / 1_000).toFixed(1).replace(/\.0$/, '')}k`; if (n < 999_500) return `${Math.round(n / 1_000)}k`; return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, '')}m`; } /** * An amount in the smallest form that keeps its meaning. From one dollar up it * shows two decimals, from one cent up three decimals, and from a ten-thousandth * of a dollar up four. A smaller amount shows as "<$0.0001". This is the rule of * the daemon's `FormatCharge`. */ export function formatCharge(usd: number): string { if (usd >= 1) return `$${usd.toFixed(2)}`; if (usd >= 0.01) return `$${usd.toFixed(3)}`; if (usd >= 0.0001) return `$${usd.toFixed(4)}`; return usd > 0 ? '<$0.0001' : '$0'; } /** The statement that a session's spend belongs to: money charged, a plan * allowance drawn down, or the list price of calls that cost nothing. The daemon * derives the amount and the basis. This map gives only the text that a client * shows. */ export const costBasis: Record = { charged: 'charged to this session', counted: 'counted against your plan', listed: 'list price of these calls', }; /** Same 512px tile formula as `albedo_sdk/compaction.image_tokens`. */ function estimateImageTokens(width: number, height: number): number { const wTiles = Math.max(1, Math.ceil(width / 512)); const hTiles = Math.max(1, Math.ceil(height / 512)); return 85 + 170 * wTiles * hTiles; } /** Approximate byte length of a decoded JSON value without allocating a serialized string. */ function jsonValueBytes(val: unknown): number { if (typeof val === 'string') return val.length; if (val === null || val === undefined) return 4; if (typeof val === 'number' || typeof val === 'boolean') return 6; if (Array.isArray(val)) { let sum = 2; for (const item of val) sum += jsonValueBytes(item) + 1; return sum; } if (typeof val === 'object') { let sum = 2; for (const [k, v] of Object.entries(val as Record)) { sum += k.length + 4 + jsonValueBytes(v); } return sum; } return 8; } function shellCommandBytes(value: unknown): number | null { if (!value || typeof value !== 'object') return null; const bytes = (value as Record).model_text_bytes; return typeof bytes === 'number' ? bytes : null; } function measureEntry(entry: HistoryEntry): EntryCost { const incompleteShell = entry.kind === 'shell_command' && entry.content.some( (part) => part.kind === 'reference' && part.reference.field === 'shell_command', ); const byRef = incompleteShell ? undefined : entryCostCache.get(entry); if (byRef) return byRef; if (!incompleteShell && entry.id) { const byId = entryIdCostCache.get(entry.id); if (byId) { entryCostCache.set(entry, byId); return byId; } } let msgBytes = 0; let toolBytes = 0; const media: MediaTokens = { images: 0, audio: 0, video: 0 }; const isTool = entry.kind === 'tool_call' || entry.kind === 'tool_result'; const isMsg = entry.kind === 'user' || entry.kind === 'assistant' || entry.kind === 'thinking' || entry.kind === 'compaction' || entry.kind === 'continuation' || entry.kind === 'shell_command'; if (isTool || isMsg) { for (const part of entry.content) { if (part.kind === 'image') { // only a daemon without media_tokens leaves this to the browser, which // cannot read how long a clip plays. const kind = part.image.mime_type.split('/')[0]; if (kind === 'audio' || kind === 'video') { media[kind] += Math.floor(part.image.original_bytes / 400) + 20; } else { media.images += estimateImageTokens(part.image.width, part.image.height) + 20; } } else if (part.kind === 'reference') { if ( entry.kind === 'shell_command' && part.reference.field === 'shell_command' ) { // Shell receipts are selectively hydrated by transcript loading. Their // reference byte count is serialized receipt size, not model preview size. } else if (part.reference.field.startsWith('image-')) { media.images += 765; } else if (part.reference.field !== 'trace') { if (isTool) toolBytes += part.reference.bytes; else msgBytes += part.reference.bytes; } } else if (part.kind === 'text') { if (isTool) toolBytes += part.text.length; else msgBytes += part.text.length; } else if (part.kind === 'json') { const len = entry.kind === 'shell_command' && part.field === 'shell_command' ? (shellCommandBytes(part.value) ?? jsonValueBytes(part.value)) : jsonValueBytes(part.value); if (isTool) toolBytes += len; else msgBytes += len; } } } const cost: EntryCost = { msgBytes, toolBytes, media }; if (!incompleteShell) entryCostCache.set(entry, cost); if (!incompleteShell && entry.id) { if (entryIdCostCache.size >= 4000) entryIdCostCache.clear(); entryIdCostCache.set(entry.id, cost); } return cost; } /** * Splits `totalTokens` into `system`, `messages`, `tool output`, `images`, * `audio`, and `video` in prompt order (`system` first), keeping `system` * anchored to the prepared instructions + tool schemas byte count and * splitting the remaining history tokens across the rest. Media is weighed by * the tokens the daemon priced for the prepared request when it sent them. */ export function computeSegments( totalTokens: number, summary: ContextSummary | null, entries: HistoryEntry[], ): ContextSegment[] { if (totalTokens <= 0) return []; let sysEst = 0; if (summary && summary.state === 'ready') { for (const sec of summary.sections) { if ( sec.kind === 'instructions' || sec.kind === 'tools' || sec.kind === 'extension_context' ) { sysEst += Math.ceil(sec.utf8_bytes / 4); } } } if (sysEst === 0) { sysEst = Math.min(Math.round(totalTokens * 0.18), 18_000); } const sysTokens = Math.min( sysEst, Math.max(1, Math.round(totalTokens * 0.85)), ); const historyBudget = Math.max(0, totalTokens - sysTokens); let msgBytes = 0; let toolBytes = 0; const estimated: MediaTokens = { images: 0, audio: 0, video: 0 }; for (const entry of entries) { const c = measureEntry(entry); msgBytes += c.msgBytes; toolBytes += c.toolBytes; estimated.images += c.media.images; estimated.audio += c.media.audio; estimated.video += c.media.video; } const media = summary?.state === 'ready' && summary.media_tokens ? summary.media_tokens : estimated; let msgRaw = Math.ceil(msgBytes / 4); const toolRaw = Math.ceil(toolBytes / 4); const mediaRaw = media.images + media.audio + media.video; if (msgRaw === 0 && toolRaw === 0 && mediaRaw === 0 && historyBudget > 0) { msgRaw = historyBudget; } const histRawSum = msgRaw + toolRaw + mediaRaw || 1; const scaled = (raw: number) => historyBudget > 0 && raw > 0 ? Math.max(1, Math.round((raw / histRawSum) * historyBudget)) : 0; const rawList: { key: SegmentKey; label: string; tokens: number; color: string; }[] = [ { key: 'sys', label: 'system', tokens: sysTokens, color: 'var(--c-sys)' }, { key: 'msg', label: 'messages', tokens: scaled(msgRaw), color: 'var(--c-msg)', }, { key: 'tool', label: 'tool output', tokens: scaled(toolRaw), color: 'var(--c-tool)', }, { key: 'img', label: 'images', tokens: scaled(media.images), color: 'var(--c-img)', }, { key: 'audio', label: 'audio', tokens: scaled(media.audio), color: 'var(--c-audio)', }, { key: 'video', label: 'video', tokens: scaled(media.video), color: 'var(--c-video)', }, ]; const nonZero = rawList.filter((item) => item.tokens > 0); if (!nonZero.length) return []; const rawWeights = nonZero.map((item) => Math.pow(item.tokens, 0.58)); const rawWSum = rawWeights.reduce((acc, w) => acc + w, 0) || 1; // Guarantee every non-zero category at least 8% of the used bar so even a 1k // message segment beside 200k of tool output is a clear, hoverable pill. const floored = rawWeights.map((w) => Math.max(8, (w / rawWSum) * 100)); const flooredSum = floored.reduce((acc, w) => acc + w, 0) || 1; return nonZero.map((item, i) => ({ key: item.key, label: item.label, tokens: item.tokens, color: item.color, perceptualPct: (floored[i] / flooredSum) * 100, })); }