diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 3f5744c..7afe670 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -1200,7 +1200,18 @@ function reasoningOf(m) { // thinking before the call said, and said it had none without it. A turn // with thinking and nothing else is still left out, as that client leaves // it. -function qoderMessages(msgs) { +// Kimi on QwenWork (kimi-k3.1-pro) answers 400 "Error in upstream response" +// to an assistant turn with tool_calls and an empty content, which is how +// every OpenAI client records a call-only turn (nanfei892, #1213: 9-token +// bodies, DeepSeek, GLM and Qwen on the same account fine, " " served). +// QwenWork's own client always writes text beside a call. A Kimi model's +// call-only turn carries one space; other models get the turn as written. +function kimiModel(m) { + const key = String(m?.key ?? "") + return /^kmodel/i.test(key) || /kimi/i.test(key + " " + (m?.config?.display_name ?? "") + " " + (m?.name ?? "")) +} + +function qoderMessages(msgs, opts = {}) { const out = [] const names = {} let seen = [] @@ -1259,7 +1270,7 @@ function qoderMessages(msgs) { return { id, type: "function", function: { name: c.function?.name, arguments: typeof args === "string" ? args : JSON.stringify(args ?? {}) } } }) const think = reasoningOf(m) - out.push({ role: "assistant", content: textOf(m.content), ...(think ? { reasoning_content: think } : {}), tool_calls: calls }) + out.push({ role: "assistant", content: textOf(m.content) || (opts.callText ?? ""), ...(think ? { reasoning_content: think } : {}), tool_calls: calls }) const open = calls.map((c) => c.id) const kept = new Set() for (let j = i + 1; j < msgs.length && msgs[j].role === "tool"; j++) { @@ -1522,7 +1533,7 @@ function qoderBody(chat, m, named = "", task, site) { session_type: site?.sessionType ?? "app", model_config: modelConfig(m, thinking), system: [sys], - messages: [{ role: "system", content: [sys] }, ...cacheMarked(qoderMessages(chat.messages ?? []))], + messages: [{ role: "system", content: [sys] }, ...cacheMarked(qoderMessages(chat.messages ?? [], { callText: kimiModel(m) ? " " : "" }))], } if (tools.length) body.tools = tools.map((t) => ({ @@ -4216,4 +4227,4 @@ export async function QwenWorkEnterpriseAuthPlugin({ client }) { export const protocol = { makePlugin, deviceSignIn, openapi, siteOf } // for tests -export const _internal = { spentOf, OUTPUT_CAPS, LEARNED_CAPS, outputCap, fetchListing, accountIdentity, deviceRefreshBody, qwenworkClientHeaders, freeView, maskPhone, phoneLabelOf, realEmailOf, chatHeaders, machineOS, machineHostname, cosyHeaders, typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, dedicatedOrigin, dedicatedHost, dedicatedSite, lingmaRoots, lingmaDecode, lingmaSite, lingmaHeaders, LINGMA_MODELS, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState } +export const _internal = { spentOf, OUTPUT_CAPS, LEARNED_CAPS, outputCap, fetchListing, accountIdentity, deviceRefreshBody, qwenworkClientHeaders, freeView, maskPhone, phoneLabelOf, realEmailOf, chatHeaders, machineOS, machineHostname, cosyHeaders, typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, dedicatedOrigin, dedicatedHost, dedicatedSite, lingmaRoots, lingmaDecode, lingmaSite, lingmaHeaders, LINGMA_MODELS, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, kimiModel, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState } diff --git a/packages/qoder/kimi-call-text.test.mjs b/packages/qoder/kimi-call-text.test.mjs new file mode 100644 index 0000000..e402812 --- /dev/null +++ b/packages/qoder/kimi-call-text.test.mjs @@ -0,0 +1,38 @@ +// #1213 (nanfei892): QwenWork's kimi-k3.1-pro answered 400 "Error in +// upstream response" (502 here) to every request whose history held a +// call-only assistant turn, content null or "", from the second turn of any +// agent on; one space served it, and other models took the empty turn. + +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const { qoderBody, kimiModel } = _internal + +const turns = (content) => [ + { role: "system", content: "you are a coding agent" }, + { role: "user", content: "read it" }, + { role: "assistant", content, tool_calls: [{ id: "call_1", type: "function", function: { name: "read_file", arguments: "{}" } }] }, + { role: "tool", tool_call_id: "call_1", content: "ok" }, +] +const model = (key, name) => ({ key, name, config: { key, display_name: name } }) +const callTurn = (body) => body.messages.find((x) => x.role === "assistant" && x.tool_calls) + +test("a Kimi model's call-only turn carries text, for null and empty content", () => { + for (const m of [model("kimi-k3.1-pro", "Kimi-K3.1-Pro"), model("kmodel", "Kimi-K2.8-Preview"), model("kmodel_latest", "Kimi-K3")]) + for (const content of [null, "", undefined]) { + const turn = callTurn(qoderBody({ messages: turns(content) }, m)) + expect(turn.content).toBe(" ") + expect(turn.tool_calls[0].id).toBe("call_1") + } +}) + +test("a Kimi call turn that has text keeps it as written", () => { + expect(callTurn(qoderBody({ messages: turns("reading it") }, model("kimi-k3.1-pro", "Kimi-K3.1-Pro"))).content).toBe("reading it") +}) + +test("other models get the call-only turn as written", () => { + for (const m of [model("deepseek-flash", "DeepSeek-Flash"), model("glm-5.3", "GLM-5.3"), model("qwen4-max", "Qwen4-Max"), model("dfmodel", "DeepSeek")]) { + expect(kimiModel(m)).toBe(false) + expect(callTurn(qoderBody({ messages: turns(null) }, m)).content).toBe("") + } +}) diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 58d185e..58c4c90 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.68", + "version": "0.2.69", "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": "./opencode.mjs",