feat: add model-aware context budget calculation for status bar warnings

This commit is contained in:
weishu
2025-12-18 08:50:07 +08:00
parent 7f07bdc796
commit f75a7d46bb
3 changed files with 42 additions and 10 deletions
+28
View File
@@ -0,0 +1,28 @@
import type { ModelMode } from '@/types/api'
/**
* Context windows vary by model/provider and may change over time.
*
* The UI only needs this to compute a conservative "context remaining" warning.
* We intentionally keep a headroom budget to avoid false confidence near the limit
* (system prompts, tool overhead, and other hidden tokens can consume extra space).
*
* If/when the server provides an explicit per-session context limit, prefer that
* and use this only as a fallback.
*/
const CONTEXT_HEADROOM_TOKENS = 10_000
const MODEL_CONTEXT_WINDOWS: Record<NonNullable<ModelMode>, number> = {
// Claude Code modes used in this app; currently treated as ~200k context.
default: 200_000,
sonnet: 200_000,
opus: 200_000
}
export function getContextBudgetTokens(modelMode: ModelMode): number | null {
const mode: NonNullable<ModelMode> = modelMode ?? 'default'
const windowTokens = MODEL_CONTEXT_WINDOWS[mode]
if (!windowTokens) return null
return Math.max(1, windowTokens - CONTEXT_HEADROOM_TOKENS)
}