From 0e07ebd1b2e24dde1093264bca6633ecbdc483ef Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 26 Sep 2026 19:16:31 +0800 Subject: [PATCH] =?UTF-8?q?providers:=20the=20context=20window=20can=20be?= =?UTF-8?q?=20set=20in=20the=20app=20=E2=80=94=20a=20provider's=20editor?= =?UTF-8?q?=20has=20a=20Context=20window=20field,=20128k=20for=20all=20its?= =?UTF-8?q?=20models=20or=20gpt-6=3D1m=20for=20one,=20comma=20separated,?= =?UTF-8?q?=20which=20agents=20are=20told=20over=20what=20the=20vendor=20o?= =?UTF-8?q?r=20models.dev=20says=20(as=20magpie=20provider=20set=20?= =?UTF-8?q?=E2=80=A6=20context=3D=20already=20did);=20empty=20leaves=20it?= =?UTF-8?q?=20to=20them=20again.=20Asked=20on=20X?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gui/assets/app.js | 37 ++++++++++++++++++++++++++++++++++++- internal/gui/assets/i18n.js | 4 ++++ internal/gui/providers.go | 7 ++++--- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 491219ba4..7284c3603 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -2105,7 +2105,7 @@ function renderEditor(p, presetID) { // more provider of it, under a name and id of its own const another = isNew && !!pr?.added; draft = draft || (p - ? { id: p.id, name: p.name, preset: p.preset, chat: p.chat, responses: p.responses, anthropic: p.anthropic, catalog: p.catalog, key: "", api: p.chat ? "openai" : p.anthropic ? "anthropic" : p.responses ? "responses" : "openai", chosen: p.models.filter((m) => m.on).map((m) => m.id), extra: [], headers: headerRows(p.headers), icon: p.icon || "", fallback: [...(p.fallback || [])], unlisted: !!p.unlisted, balanceURL: p.balanceURL || "", balancePath: p.balancePath || "", modelsURL: p.modelsURL || "" } + ? { id: p.id, name: p.name, preset: p.preset, chat: p.chat, responses: p.responses, anthropic: p.anthropic, catalog: p.catalog, key: "", api: p.chat ? "openai" : p.anthropic ? "anthropic" : p.responses ? "responses" : "openai", chosen: p.models.filter((m) => m.on).map((m) => m.id), extra: [], headers: headerRows(p.headers), icon: p.icon || "", fallback: [...(p.fallback || [])], unlisted: !!p.unlisted, balanceURL: p.balanceURL || "", balancePath: p.balancePath || "", modelsURL: p.modelsURL || "", contexts: contextsText(p.contexts) } : pr ? { id: pr.id, name: pr.name, preset: pr.id, key: "", chosen: [], extra: [], headers: [] } : { id: "", name: "", preset: "", chat: "", responses: "", anthropic: "", catalog: "", key: "", api: "openai", chosen: [], extra: [], headers: [], icon: "" }); @@ -2293,6 +2293,13 @@ function renderEditor(p, presetID) { } if (p) ed.append(...field(t("Models"), renderModels(p), "")); + { + // the window agents are told a model has, over what the vendor or + // models.dev says: one for all of them, and model=size for one + const cx = input(draft.contexts || "", t("e.g. 128k · or gpt-6=1m, comma separated")); + cx.oninput = () => { draft.contexts = cx.value; }; + ed.append(...field(t("Context window"), cx, t("How long a request the models take, told to the agents; empty leaves it to the vendor and models.dev"))); + } if (p) ed.append(...field(t("Fallback"), renderFallback(p), fallbackHint(p))); else if (custom) { const ex = input(draft.extra.join(", "), t("model ids, comma separated · e.g. gpt-5.5, claude-sonnet-5")); @@ -2370,6 +2377,9 @@ function renderEditor(p, presetID) { const body = { id: draft.id, name: draft.name, preset: draft.preset, key: draft.key || "", chat: draft.chat, responses: draft.responses, anthropic: draft.anthropic, catalog: draft.catalog, models: p ? draft.chosen : draft.extra, headers: headersOf(draft.headers), new: isNew }; if (custom) { body.icon = draft.icon || "generic"; body.balanceURL = (draft.balanceURL || "").trim(); body.balancePath = (draft.balancePath || "").trim(); body.modelsURL = (draft.modelsURL || "").trim(); } if (p) { body.fallback = draft.fallback; body.unlisted = draft.unlisted; } + const cx = parseContexts(draft.contexts || ""); + if (cx.error) return editorError(t("Context window: {v} is not a length like 128k or 1m", { v: cx.error }), "warn"); + body.contexts = cx.map; if (draft.balanceToken) body.balanceToken = draft.balanceToken; else if (draft.clearBalanceToken) body.clearBalanceToken = true; if (isNew && custom && !body.name) { name.focus(); return editorError(t("Give it a name"), "warn"); } @@ -2385,6 +2395,31 @@ function renderEditor(p, presetID) { return ed; } +// contextsText is a provider's contexts as the editor shows them: the one +// for all its models first, then model=size. +function contextsText(cx) { + if (!cx) return ""; + const size = (n) => n % 1e6 === 0 ? n / 1e6 + "m" : n % 1e3 === 0 ? n / 1e3 + "k" : String(n); + const out = cx["*"] ? [size(cx["*"])] : []; + for (const [id, n] of Object.entries(cx).sort()) if (id !== "*") out.push(id + "=" + size(n)); + return out.join(", "); +} + +// parseContexts reads "128k, gpt-6=1m" back: sizes by model id, "*" for +// all; error is the first part that isn't a size. +function parseContexts(text) { + const map = {}; + for (const part of text.split(/[,,\n]/).map((x) => x.trim()).filter(Boolean)) { + const i = part.lastIndexOf("="); + const id = i < 0 ? "*" : part.slice(0, i).trim(), v = (i < 0 ? part : part.slice(i + 1)).trim().toLowerCase().replace(/_/g, ""); + const m = /^(\d+(?:\.\d+)?)([km]?)$/.exec(v); + if (!m || !id) return { error: part }; + const n = Math.round(parseFloat(m[1]) * (m[2] === "m" ? 1e6 : m[2] === "k" ? 1e3 : 1)); + if (n > 0) map[id] = n; + } + return { map }; +} + // fetchImportIcon asks the server to download the vendor's own logo, named // by the link. It swaps the header mark when it lands; a failure is silent // (the generic outline stays), since the icon is decoration, not the deal. diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 896a47340..dfcdd90cc 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -180,6 +180,10 @@ const I18N = { "Account": "账号", "Accounts": "账号", "Fallback": "备用", + "Context window": "上下文窗口", + "e.g. 128k · or gpt-6=1m, comma separated": "如 128k · 或 gpt-6=1m,逗号分隔", + "How long a request the models take, told to the agents; empty leaves it to the vendor and models.dev": "模型能接受的上下文长度,会告诉各个 Agent;留空则按厂商和 models.dev 的", + "Context window: {v} is not a length like 128k or 1m": "上下文窗口:{v} 不是 128k、1m 这样的长度", "add a model: filter, or type provider/model…": "添加模型:筛选,或输入 provider/model…", "No provider serves {id} now; it is skipped": "现在没有供应商提供 {id},会跳过它", "Up": "上移", diff --git a/internal/gui/providers.go b/internal/gui/providers.go index cf22e0715..8dc3e0738 100644 --- a/internal/gui/providers.go +++ b/internal/gui/providers.go @@ -63,6 +63,7 @@ type providerJSON struct { Models []modelJSON `json:"models"` // everything the vendor lists, exposed ones flagged Exposed int `json:"exposed"` // how many reach the agents Unlisted bool `json:"unlisted"` // its models serve only through routing groups + Contexts map[string]int `json:"contexts,omitempty"` // the windows the user set, "*" for all its models Fetched string `json:"fetched"` // "3h ago" when the list came from the vendor Agents []providerAgent `json:"agents"` // detected agents, current ones flagged Sponsored bool `json:"sponsored"` @@ -152,7 +153,7 @@ func providerInfo(p provider.Provider, agents []*agent.Agent) providerJSON { Catalog: p.Catalog, Website: p.Website, KeysURL: p.KeysURL, Headers: p.Headers, BalanceURL: p.BalanceURL, BalancePath: p.BalancePath, ModelsURL: p.ModelsURL, Ready: p.Ready(), Chosen: p.Models, Models: []modelJSON{}, Agents: []providerAgent{}, - Fallback: p.Fallback, Routing: p.Routing, Affinity: p.Affinity, Unlisted: p.Unlisted, + Fallback: p.Fallback, Routing: p.Routing, Affinity: p.Affinity, Unlisted: p.Unlisted, Contexts: p.Contexts, } if out.Fallback == nil { out.Fallback = []string{} @@ -352,7 +353,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { // a preset needs nothing but the key; a saved provider keeps // its key when the form left it blank if pr, err := provider.FromPreset(in.Preset); err == nil && in.Chat == "" && in.Responses == "" && in.Anthropic == "" { - pr.Key, pr.Models, pr.Fallback, pr.Headers, pr.BalanceToken = in.Key, in.Models, in.Fallback, in.Headers, in.BalanceToken + pr.Key, pr.Models, pr.Fallback, pr.Headers, pr.BalanceToken, pr.Contexts = in.Key, in.Models, in.Fallback, in.Headers, in.BalanceToken, in.Contexts if in.Name != "" { pr.Name = in.Name } @@ -384,7 +385,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { in.Keys = old.Keys in.Routing = old.Routing // set on its own, with route if in.Contexts == nil { - in.Contexts = old.Contexts // set from the terminal + in.Contexts = old.Contexts // a save that doesn't say } if in.Key == old.Key { in.KeyName, in.KeyProtocol = old.KeyName, old.KeyProtocol