From a5f3321af3a9f2378d2d1dcead004180475d2efa Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 4 Oct 2026 06:50:17 +0800 Subject: [PATCH] =?UTF-8?q?qoder=200.2.5:=20a=20model's=20context=20is=20t?= =?UTF-8?q?he=20largest=20window=20Qoder=20offers=20it,=20not=20max=5Finpu?= =?UTF-8?q?t=5Ftokens=20(yetone/magpie#722,=20Moody-Sin:=20qoder=20?= =?UTF-8?q?=E7=9A=84=20qwen=203.8=20flash=20=E5=8F=AA=E6=9C=89180K?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87)=20=E2=80=94=20modelInfo=20took=20t?= =?UTF-8?q?he=20context=20from=20max=5Finput=5Ftokens,=20180000=20on=20Qwe?= =?UTF-8?q?n3.8-Flash=20(and=20Qwen3.8-Max,=20Kimi-K3,=20GLM-5.3,=20MiniMa?= =?UTF-8?q?x-M3,=20Sonus,=20Cantus),=20so=20magpie=20and=20every=20agent?= =?UTF-8?q?=20behind=20it=20were=20told=20180K,=20and=20each=20request=20w?= =?UTF-8?q?as=20sent=20context=5Flength=20180000.=20Qoder's=20client=20rea?= =?UTF-8?q?ds=20the=20windows=20a=20model=20offers=20from=20the=20listing'?= =?UTF-8?q?s=20context=5Fconfig=20({name:=20{token=5Fcount,=20is=5Fdefault?= =?UTF-8?q?}};=20else=20available=5Fcontext=5Fwindows=20/=20default=5Fcont?= =?UTF-8?q?ext=5Fwindow),=20as=20qodercli's=20catalog=20does=20(YT/Yai=20i?= =?UTF-8?q?n=20the=20Qoder.app=200.4.3=20worker),=20and=20its=20Context=20?= =?UTF-8?q?setting=20offers=20200K,=20400K=20and=201M=20on=20Qwen3.8-Flash?= =?UTF-8?q?=20and=20twelve=20others=20(docs.qoder.com,=20model=20tier=20se?= =?UTF-8?q?lector);=20max=5Finput=5Ftokens=20is=20only=20what=20a=20reques?= =?UTF-8?q?t=20naming=20no=20window=20gets.=20windowsOf=20reads=20them=20t?= =?UTF-8?q?he=20same=20way;=20a=20model's=20context=20is=20now=20its=20lar?= =?UTF-8?q?gest=20window=20(max=5Finput=5Ftokens=20when=20it=20lists=20non?= =?UTF-8?q?e),=20and=20windowFor=20sends=20the=20default=20window=20while?= =?UTF-8?q?=20the=20request=20fits=20it,=20else=20the=20smallest=20that=20?= =?UTF-8?q?holds=20it,=20else=20the=20largest=20=E2=80=94=20a=20larger=20w?= =?UTF-8?q?indow=20may=20change=20the=20rate,=20Qoder's=20docs=20say,=20so?= =?UTF-8?q?=20it=20is=20asked=20for=20only=20when=20the=20conversation=20n?= =?UTF-8?q?eeds=20it;=20the=20request=20is=20counted=20generously,=20a=20t?= =?UTF-8?q?oken=20per=203=20bytes=20of=20its=20JSON.=20context.test.mjs=20?= =?UTF-8?q?(windows=20read=20from=20context=5Fconfig=20and=20available=5Fc?= =?UTF-8?q?ontext=5Fwindows,=20a=20model=20without=20them=20keeping=201800?= =?UTF-8?q?00,=20the=20default=20window=20sent=20for=20a=20short=20request?= =?UTF-8?q?=20and=20400K/1M=20for=20longer=20ones)=20fails=20on=20the=20ol?= =?UTF-8?q?d=20code=20and=20passes;=20bun=20test=20(48=20pass)=20and=20scr?= =?UTF-8?q?ipts/check.mjs=20qoder=20under=20a=20temp=20HOME=20pass.=20Not?= =?UTF-8?q?=20tried=20with=20a=20real=20Qoder=20account:=20the=20listing's?= =?UTF-8?q?=20context=5Fconfig=20for=20qfmodel=20is=20read=20from=20Qoder'?= =?UTF-8?q?s=20client=20code=20and=20docs,=20not=20from=20a=20live=20listi?= =?UTF-8?q?ng.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/qoder/README.md | 10 +++++- packages/qoder/context.test.mjs | 51 +++++++++++++++++++++++++++ packages/qoder/index.mjs | 61 ++++++++++++++++++++++++++++++--- packages/qoder/package.json | 2 +- 4 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 packages/qoder/context.test.mjs diff --git a/packages/qoder/README.md b/packages/qoder/README.md index ad3e084..ebe8e7e 100644 --- a/packages/qoder/README.md +++ b/packages/qoder/README.md @@ -62,6 +62,10 @@ itself. - The nearest level to the one asked, the higher one on a tie. - The model's default when none is asked. - The lowest level for "none", when the model can't turn thinking off. +- It picks the context window (`context_length`) from the model's own + windows (Qoder's Context setting: 200K, 400K, 1M): the default while the + request fits it, else the smallest that holds it, else the largest. A + larger window may cost more, so it is asked for only when needed. - It encodes the body with the client's codec and signs the call with the client's COSY envelope. The envelope carries: - the account, AES-encrypted, with the key wrapped by Qoder's RSA key; @@ -89,7 +93,11 @@ itself. The `provider.models` hook reads the account's own list, as Qoder's client asks for it (`/algo/api/v2/model/list`). It keeps the enabled chat models and leaves out "auto" and "default", which route inside Qoder. It takes -each model's reasoning levels from its `thinking_config`. +each model's reasoning levels from its `thinking_config`, and its context +from its `context_config`: the largest window it offers (Qoder's docs +give Qwen3.8-Flash 200K, 400K and 1M), not `max_input_tokens` (180K +there), which is only what a request naming no window gets. A model listed +with no windows keeps `max_input_tokens`. The `config` hook declares the list as it was on 2026-09-30: diff --git a/packages/qoder/context.test.mjs b/packages/qoder/context.test.mjs new file mode 100644 index 0000000..1d762fa --- /dev/null +++ b/packages/qoder/context.test.mjs @@ -0,0 +1,51 @@ +// A model's context is the largest window Qoder offers it (context_config, +// Qoder's Context setting: 200K / 400K / 1M), not max_input_tokens, which +// is 180K on Qwen3.8-Flash (yetone/magpie#722); a request is sent the +// default window while it fits, a larger one only when it needs it. +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const flash = { + key: "qfmodel", + enable: true, + display_name: "Qwen3.8-Flash", + max_input_tokens: 180000, + is_reasoning: true, + context_config: { + "1m": { token_count: 1000000 }, + "200k": { token_count: 200000, is_default: true }, + "400k": { token_count: 400000 }, + }, +} + +test("the context is the largest window context_config offers", () => { + const m = _internal.modelInfo(flash) + expect(m.context).toBe(1000000) + expect(m.windows).toEqual([200000, 400000, 1000000]) + expect(m.defaultWindow).toBe(200000) +}) + +test("available_context_windows is read without context_config", () => { + const m = _internal.modelInfo({ key: "x", max_input_tokens: 180000, available_context_windows: [400000, 200000], default_context_window: 200000 }) + expect(m.context).toBe(400000) + expect(m.defaultWindow).toBe(200000) +}) + +test("a model with no windows keeps max_input_tokens", () => { + const m = _internal.modelInfo({ key: "y", max_input_tokens: 180000 }) + expect(m.context).toBe(180000) + const body = _internal.qoderBody({ messages: [{ role: "user", content: "hi" }] }, { ...m, config: { key: "y" } }) + expect(body.parameters.context_length).toBe(180000) +}) + +const sent = (chars) => { + const m = { ..._internal.modelInfo(flash), config: flash } + return _internal.qoderBody({ messages: [{ role: "user", content: "a".repeat(chars) }] }, m).parameters.context_length +} + +test("a request is sent the default window while it fits, a larger one when it doesn't", () => { + expect(sent(10)).toBe(200000) + expect(sent(300000 * 3)).toBe(400000) + expect(sent(700000 * 3)).toBe(1000000) + expect(sent(1200000 * 3)).toBe(1000000) +}) diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index e4b2f4c..a14988a 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -340,15 +340,51 @@ function rateOf(raw) { return { rate, rateWas } } +// windowsOf reads the context windows a listing entry offers, smallest +// first, and the one Qoder's client picks unless asked for another: +// context_config's entries ({"200k": {token_count: 200000, is_default: +// true}, "1m": {token_count: 1000000}, …}), else available_context_windows, +// as Qoder's client reads them (qodercli's catalog). Qoder's Context +// setting (200K / 400K / 1M) offers them on Qwen3.8-Flash and the rest; +// max_input_tokens (180K there) is only what a request that names none +// gets. +function windowsOf(raw) { + const n = (v) => (Number.isInteger(v) && v > 0 ? v : 0) + const windows = [] + let def = 0 + const cc = raw?.context_config ?? raw?.contextConfig + if (cc && typeof cc === "object" && !Array.isArray(cc)) { + for (const e of Object.values(cc)) { + const t = n(e?.token_count) + if (!t) continue + if (!windows.includes(t)) windows.push(t) + if (e.is_default === true || e.is_default === 1 || e.is_default === "1") def = t + } + } + if (!windows.length) { + const list = raw?.available_context_windows ?? raw?.availableContextWindows + for (const v of Array.isArray(list) ? list : []) if (n(v) && !windows.includes(v)) windows.push(v) + def = n(raw?.default_context_window ?? raw?.defaultContextWindow) + if (!windows.includes(def)) def = 0 + } + windows.sort((a, b) => a - b) + return { windows, defaultWindow: def } +} + // modelInfo reads one entry of the listing's "chat" array: its efforts and -// default from thinking_config (is_reasoning alone when it has none). +// default from thinking_config (is_reasoning alone when it has none), and +// its context: the largest window it offers (windowsOf), since a request +// that needs more than the default is sent a larger one (windowFor). function modelInfo(raw) { + const { windows, defaultWindow } = windowsOf(raw) const m = { key: raw.key, source: raw.source ?? "", name: raw.display_name || raw.key, images: !!raw.is_vl, - context: raw.max_input_tokens || 0, + context: windows.length ? windows[windows.length - 1] : raw.max_input_tokens || 0, + windows, + defaultWindow, thinks: !!raw.is_reasoning, alwaysThinks: false, efforts: Array.isArray(raw.reasoning_efforts) ? [...raw.reasoning_efforts] : [], @@ -533,6 +569,22 @@ function qoderMessages(msgs) { return out } +// windowFor is the context window a request is sent (context_length) on a +// model that offers several: the one Qoder's client picks by default +// (max_input_tokens without one) while the request fits it, else the +// smallest that holds it, else the largest. A larger window may cost more +// (Qoder: changing the Context "may change the rate"), so it is asked for +// only when the conversation needs it. The request's size is counted +// generously, a token for every 3 bytes of its UTF-8 JSON, so a window is +// raised a little early rather than late. +function windowFor(chat, m) { + const base = m.defaultWindow || (m.config?.max_input_tokens > 0 ? m.config.max_input_tokens : 0) || m.context + if (!m.windows?.length) return m.context + const need = Math.ceil(Buffer.byteLength(JSON.stringify({ m: chat.messages ?? [], t: chat.tools ?? [] })) / 3) + if (base > 0 && need <= base) return base + return m.windows.find((w) => w >= need) ?? m.windows[m.windows.length - 1] +} + // qoderBody is the plaintext agent_chat_generation takes for a chat // completion request, for model m. function qoderBody(chat, m) { @@ -544,7 +596,8 @@ function qoderBody(chat, m) { const [thinking, effort] = effortFor(chat.reasoning_effort, m) const params = { enable_thinking: thinking, max_tokens: chat.max_completion_tokens || chat.max_tokens || 32000 } if (effort) params.reasoning_effort = effort - if (m.context > 0) params.context_length = m.context + const window = windowFor(chat, m) + if (window > 0) params.context_length = window const body = { parameters: params, business: { product: "app", version: COSY_VERSION, type: "agent", id: hexID(), name: "magpie session", begin_at: Date.now(), stage: "start" }, @@ -1253,4 +1306,4 @@ export const QoderAuthPlugin = makePlugin(SITES.qoder) export const QoderCNAuthPlugin = makePlugin(SITES["qoder-cn"]) // for tests -export const _internal = { parseUsage, gfmt, when, failure, modelInfo, SITES, decodeBody, events } +export const _internal = { parseUsage, gfmt, when, failure, modelInfo, qoderBody, SITES, decodeBody, events } diff --git a/packages/qoder/package.json b/packages/qoder/package.json index ca2a99c..47e29e7 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.4", + "version": "0.2.5", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs",