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",