diff --git a/packages/qoder/context.test.mjs b/packages/qoder/context.test.mjs index 1d762fa..3f4a8e4 100644 --- a/packages/qoder/context.test.mjs +++ b/packages/qoder/context.test.mjs @@ -49,3 +49,15 @@ test("a request is sent the default window while it fits, a larger one when it d expect(sent(700000 * 3)).toBe(1000000) expect(sent(1200000 * 3)).toBe(1000000) }) + +// a window holds the reply too: a prompt that fits 200K alone but not with +// the max_tokens asked for the reply is sent the next window (yetone/magpie#700) +test("the reply's max_tokens is counted in the window a request needs", () => { + const m = { ..._internal.modelInfo(flash), config: flash } + const ctx = (max_tokens) => + _internal.qoderBody({ max_tokens, messages: [{ role: "user", content: "a".repeat(180000 * 3) }] }, m).parameters.context_length + expect(ctx(1000)).toBe(200000) + expect(ctx(32000)).toBe(400000) + // none asked: the 32000 Qoder is sent counts + expect(_internal.qoderBody({ messages: [{ role: "user", content: "a".repeat(180000 * 3) }] }, m).parameters.context_length).toBe(400000) +}) diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index a14988a..b6a5833 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -576,11 +576,14 @@ function qoderMessages(msgs) { // (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) { +// raised a little early rather than late, and the reply's max_tokens is +// counted in it: a window holds the prompt and the reply, so a +// conversation of 180K asking 32K for the reply is sent 400K, not the 200K +// it would overflow while magpie says the model takes 1M (yetone/magpie#700). +function windowFor(chat, m, reply = 0) { 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) + const need = Math.ceil(Buffer.byteLength(JSON.stringify({ m: chat.messages ?? [], t: chat.tools ?? [] })) / 3) + Math.max(0, reply) if (base > 0 && need <= base) return base return m.windows.find((w) => w >= need) ?? m.windows[m.windows.length - 1] } @@ -596,7 +599,7 @@ 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 - const window = windowFor(chat, m) + const window = windowFor(chat, m, params.max_tokens) if (window > 0) params.context_length = window const body = { parameters: params, diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 47e29e7..31d6da8 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.5", + "version": "0.2.6", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs",