diff --git a/packages/qoder/README.md b/packages/qoder/README.md index 6749314..668d195 100644 --- a/packages/qoder/README.md +++ b/packages/qoder/README.md @@ -138,7 +138,10 @@ itself. - The model's own configuration from Qoder's list goes with it. - It picks the reasoning effort from the model's own levels: - The nearest level to the one asked, the higher one on a tie. - - The model's default when none is asked. + - The model's default when none is asked, unless the request's + `max_tokens` is 1024 or less: that is asked as "none" (since 0.2.55, + plugins#643), since thinking would spend so small a budget on + reasoning alone. - 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 diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 05e8a17..d2b91ce 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -898,12 +898,24 @@ function fitEffort(want, levels) { return best } +// SMALL_BUDGET is the largest max_tokens a request can name and still be +// one no client meant to think in: Anthropic's API takes thinking only with +// a budget of at least 1024 tokens and a max_tokens above it, and a client's +// own short asks (a session title, a classification) name such budgets. +// Thinking by default spends them on reasoning: on WorkBuddy, a title asked +// at 64 tokens ended "length" with 64 reasoning tokens and no text +// (plugins#643, itspig). +const SMALL_BUDGET = 1024 + // effortFor is whether to think and at which of the model's efforts ("" for // none): asked for none, a model that always thinks thinks at its lowest; -// asked for nothing, or a level Qoder doesn't name, at its own default. -function effortFor(asked, m) { +// asked for nothing, or a level Qoder doesn't name, at its own default — +// unless the request's own budget (max_tokens, 0 when it named none) is +// SMALL_BUDGET or less, which is asked as none. +function effortFor(asked, m, budget = 0) { let want = String(asked ?? "").toLowerCase() if (!EFFORT_RANK.includes(want)) want = "" + if (want === "" && budget > 0 && budget <= SMALL_BUDGET) want = "none" if (!m.thinks || (want === "none" && !m.alwaysThinks)) return [false, ""] if (!m.efforts.length) return [true, ""] if (want === "") return [true, m.defaultEffort] @@ -1268,7 +1280,8 @@ function qoderBody(chat, m, named = "", task, site) { let sysText = system ? baseSystem + "\n\n" + system : baseSystem if (chat.tool_choice === "required" && tools.length) sysText += "\nYou must call an available function in this response." const sys = { type: "text", text: sysText } - const [thinking, effort] = effortFor(chat.reasoning_effort, m) + const budget = chat.max_completion_tokens || chat.max_tokens || 0 + const [thinking, effort] = effortFor(chat.reasoning_effort, m, typeof budget === "number" ? budget : 0) 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, params.max_tokens) diff --git a/packages/qoder/package.json b/packages/qoder/package.json index cf56812..2020a0b 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.54", + "version": "0.2.55", "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship, VPC and dedicated domains", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/small-budget.test.mjs b/packages/qoder/small-budget.test.mjs new file mode 100644 index 0000000..cff2c79 --- /dev/null +++ b/packages/qoder/small-budget.test.mjs @@ -0,0 +1,30 @@ +// A request whose own max_tokens is small asks for no thinking: the default +// effort would spend its whole output budget on reasoning, as WorkBuddy's +// did on a 64-token title (plugins#643, itspig). Qoder's qoderBody had the +// same default, so it gets the same rule. +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const { qoderBody, modelInfo } = _internal +const tc = (disabled) => ({ enabled: { efforts: { low: {}, medium: {}, high: { is_default: true } } }, ...(disabled ? { disabled: {} } : {}) }) +const optional = modelInfo({ key: "think-opt", is_reasoning: true, thinking_config: tc(true) }) +const always = modelInfo({ key: "think-always", is_reasoning: true, thinking_config: tc(false) }) +const chat = (extra) => ({ messages: [{ role: "user", content: "Title this chat." }], ...extra }) +const params = (m, extra) => qoderBody(chat(extra), m).parameters + +test("a small max_tokens with no effort asked does not think at the default", () => { + for (const extra of [{ max_tokens: 64 }, { max_tokens: 1024 }, { max_completion_tokens: 64 }]) { + expect(params(optional, extra).enable_thinking).toBe(false) + expect(params(optional, extra).reasoning_effort).toBeUndefined() + // A model that always thinks thinks at its lowest, as a request for none does. + expect(params(always, extra)).toMatchObject({ enable_thinking: true, reasoning_effort: "low" }) + } + expect(params(optional, { max_tokens: 64 }).max_tokens).toBe(64) +}) + +test("an asked effort, a larger budget or none named keeps what it had", () => { + expect(params(optional, { max_tokens: 64, reasoning_effort: "high" })).toMatchObject({ enable_thinking: true, reasoning_effort: "high" }) + for (const extra of [{ max_tokens: 1025 }, { max_completion_tokens: 32000 }, {}]) { + expect(params(optional, extra)).toMatchObject({ enable_thinking: true, reasoning_effort: "high" }) + } +})