fix(webui): allow custom context window token budgets (#5901)

* fix(webui): allow custom context window token budgets

* fix(i18n): translate context window hints in all locales

* fix(webui): accept context window K and M shorthand

* fix(webui): shorten context window hints

* refactor(webui): simplify context window validation
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-24 18:26:03 +08:00
1 parent fdee134afb
commit eb3fc07087
17 files changed
+135 -49

No files matched your search

@@ -18,6 +18,8 @@ import {
ProviderPicker,
ProviderPickerIcon,
formatContextWindow,
formatContextWindowInput,
parseContextWindowTokens,
formatModelContextWindow,
normalizeContextWindowTokens,
settingsProviderConfigured,
@@ -39,7 +41,6 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { SegmentedControl } from "@/components/ui/segmented-control";
import { cn } from "@/lib/utils";
import type { SettingsPayload } from "@/lib/types";
@@ -55,8 +56,6 @@ export interface AgentSettingsDraft {
toolHintMaxLength: number;
}
const CONTEXT_WINDOW_TOKEN_OPTIONS = [65_536, 200_000, 262_144, 500_000, 1_048_576] as const;
function modelPresetValue(payload: SettingsPayload): string {
return (
payload.model_call_order?.[0] ??
@@ -517,7 +516,7 @@ export function ModelsSettings({
"settings.models.advancedSummary",
"Context {{context}} · Max {{max}} tokens",
{
context: formatModelContextWindow(form.contextWindowTokens),
context: Number.isFinite(form.contextWindowTokens) ? formatModelContextWindow(form.contextWindowTokens) : "—",
max: formatContextWindow(form.maxTokens),
},
)}
@@ -897,9 +896,10 @@ function ModelAdvancedFields({
}) {
const { t } = useTranslation();
const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
const contextWindowOptions = Array.from(
new Set([...CONTEXT_WINDOW_TOKEN_OPTIONS, contextWindowTokens]),
).sort((left, right) => left - right);
const [contextDraft, setContextDraft] = useState<{ text: string; tokens: number } | null>(null);
const contextWindowInputId = useId();
const contextWindowHintId = `${contextWindowInputId}-hint`;
const contextWindowValid = Number.isSafeInteger(contextWindowTokens) && contextWindowTokens > 0;
return (
<div className="space-y-4">
<div className="grid gap-4 sm:grid-cols-2">
@@ -938,20 +938,32 @@ function ModelAdvancedFields({
</label>
</div>
<div>
<span className="mb-2 block text-[12px] font-medium text-muted-foreground">
<label htmlFor={contextWindowInputId} className="mb-1.5 block text-[12px] font-medium text-muted-foreground">
{tx("settings.rows.contextWindow", "Context window")}
</span>
<SegmentedControl
value={String(contextWindowTokens)}
animateIndicator={false}
options={contextWindowOptions.map((tokens) => ({
value: String(tokens),
label: formatModelContextWindow(tokens),
}))}
onChange={(value) =>
onChange({ contextWindowTokens: normalizeContextWindowTokens(Number(value)) })
}
</label>
<Input
id={contextWindowInputId}
type="text"
autoCapitalize="none"
spellCheck={false}
required
value={contextDraft && Object.is(contextDraft.tokens, contextWindowTokens)
? contextDraft.text : formatContextWindowInput(contextWindowTokens)}
onChange={(event) => {
const text = event.target.value;
const tokens = parseContextWindowTokens(text);
setContextDraft({ text, tokens });
onChange({ contextWindowTokens: tokens });
}}
aria-invalid={!contextWindowValid}
aria-describedby={contextWindowHintId}
className="h-9 text-[13px]"
/>
<p id={contextWindowHintId} className={cn("mt-1.5 text-[12px]", contextWindowValid ? "text-muted-foreground" : "text-destructive")}>
{contextWindowValid
? t("settings.models.contextWindowHint", { tokens: contextWindowTokens.toLocaleString(), defaultValue: "{{tokens}} tokens" })
: tx("settings.models.contextWindowError", "Enter a positive token count, such as 200k, 1m, or 131072.")}
</p>
</div>
<label className="block">
<span className="mb-1.5 block text-[12px] font-medium text-muted-foreground">
@@ -159,7 +159,9 @@ export function useModelSettingsActions({
!settings ||
saving ||
modelCallOrderSaving ||
modelConfigurationSaving
modelConfigurationSaving ||
!Number.isSafeInteger(form.contextWindowTokens) ||
form.contextWindowTokens <= 0
) {
return;
}
@@ -173,7 +175,6 @@ export function useModelSettingsActions({
!provider ||
!model ||
form.maxTokens <= 0 ||
form.contextWindowTokens <= 0 ||
form.temperature < 0 ||
form.temperature > 2
) {
@@ -60,6 +60,21 @@ export function normalizeContextWindowTokens(value: number | null | undefined):
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 200_000;
}
export function parseContextWindowTokens(value: string): number {
const match = /^(\d+(?:\.\d+)?)\s*([km]?)$/i.exec(value.trim());
if (!match) return NaN;
const exponent = { k: 3, m: 6 }[match[2].toLowerCase()] ?? 0;
const tokens = Number(`${match[1]}e${exponent}`);
return Number.isSafeInteger(tokens) && tokens > 0 ? tokens : NaN;
}
export function formatContextWindowInput(tokens: number): string {
if (!Number.isFinite(tokens)) return "";
if (tokens % 1_000_000 === 0) return `${tokens / 1_000_000}m`;
if (tokens % 1_000 === 0) return `${tokens / 1_000}k`;
return String(tokens);
}
function settingsProviderRow(
payload: SettingsPayload,
provider: string | null | undefined,