forked from nexu-io/open-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxTokens.ts
More file actions
50 lines (43 loc) · 2 KB
/
maxTokens.ts
File metadata and controls
50 lines (43 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { AppConfig } from '../types';
import litellmData from './litellm-models.json';
// Per-model output cap, used to default `max_tokens` so users on supported
// models don't have to find Settings to avoid mid-stream truncation.
//
// Source of truth: vendored slice of BerriAI/litellm's
// model_prices_and_context_window.json (MIT). Regenerate with:
// node --experimental-strip-types scripts/sync-litellm-models.ts
//
// Anything LiteLLM doesn't track (or where its value is wrong for our
// usage) goes in OVERRIDES; unknown models fall through to FALLBACK.
export const FALLBACK_MAX_TOKENS = 8192;
// Bounds the user can express via the Settings override. Source of truth
// for both the UI input attributes and runtime validation in
// `effectiveMaxTokens`, so a stale or hand-edited localStorage value
// can't sneak past the UI's promise.
export const MIN_MAX_TOKENS = 1024;
export const MAX_MAX_TOKENS = 200000;
const LITELLM_MODELS = litellmData.models as Record<string, number>;
const OVERRIDES: Record<string, number> = {
// LiteLLM lists MiMo via OpenRouter and Novita aliases (16k / 32k) but
// not the canonical `mimo-v2.5-pro` id we hand to Xiaomi's direct API.
// 32k matches what issue #29 reports as the working ceiling.
'mimo-v2.5-pro': 32768,
};
export function modelMaxTokensDefault(model: string): number {
return OVERRIDES[model] ?? LITELLM_MODELS[model] ?? FALLBACK_MAX_TOKENS;
}
function isValidOverride(value: number | undefined): value is number {
return (
typeof value === 'number' &&
Number.isInteger(value) &&
value >= MIN_MAX_TOKENS &&
value <= MAX_MAX_TOKENS
);
}
export function effectiveMaxTokens(cfg: Pick<AppConfig, 'maxTokens' | 'model'>): number {
// Out-of-range or non-integer overrides (stale localStorage, hand-edited
// config, future schema drift) fall back to the model default rather
// than silently shipping an invalid `max_tokens` upstream.
if (isValidOverride(cfg.maxTokens)) return cfg.maxTokens;
return modelMaxTokensDefault(cfg.model);
}