From 507dbcc4590edd15ebf15ca89fb66ca600c36430 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 12 Nov 2026 22:50:15 +0800 Subject: [PATCH] codearts 0.3.15: stop goes to the gateway as a string, an empty one not at all (Abandon332 on X, magpie#6342) Huawei's gateway takes `stop` as a string only. Any array, [] too, came back as HTTP 200 with InferHub.001001005.400 "The request param is invalid", which magpie read as a plain 400 and rested the account for (182 times in the report). prepareChatBody now sends the first non-empty string of a list, and drops an empty or blank one or a value of another kind. Run: bun test packages/codearts (204 pass). Broke it by hand: with the normalizeStop call commented out the new test is red. Not tried: a real CodeArts account (the reporter's table is the evidence). --- packages/codearts/index.mjs | 15 +++++++++++++++ packages/codearts/index.test.mjs | 9 +++++++++ packages/codearts/package.json | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/codearts/index.mjs b/packages/codearts/index.mjs index 27fdd5c..027b2f3 100644 --- a/packages/codearts/index.mjs +++ b/packages/codearts/index.mjs @@ -1079,11 +1079,26 @@ function prepareChatBody(parsed, { thinking = true } = {}) { for (const k of ["max_tokens", "max_completion_tokens"]) { if (typeof body[k] === "number" && body[k] > MAX_OUTPUT_TOKENS) body[k] = MAX_OUTPUT_TOKENS } + normalizeStop(body) const fold = body.stream !== true body.stream = true return { body, fold, injected, names } } +/** + * The gateway takes `stop` as a string only: an array, an empty one too, + * comes back as HTTP 200 with `InferHub.001001005.400` "The request param is + * invalid" (magpie#6342, measured with every model). A list goes as its + * first non-empty string, the one the gateway can take; an empty or blank + * one, or a value of another kind, is left out. + */ +function normalizeStop(body) { + if (!("stop" in body)) return + const v = Array.isArray(body.stop) ? body.stop.find((x) => typeof x === "string" && x !== "") : body.stop + if (typeof v === "string" && v !== "") body.stop = v + else delete body.stop +} + // ---- tool names over the gateway's limit (#432) ----------------------------- /** diff --git a/packages/codearts/index.test.mjs b/packages/codearts/index.test.mjs index df746c1..f90310a 100644 --- a/packages/codearts/index.test.mjs +++ b/packages/codearts/index.test.mjs @@ -244,6 +244,15 @@ test("a dated model only the agent lists keeps its date, named or not (#677)", a assert.equal(names["deepseek-v4.1-flash-0930"], "Deepseek V4.1 Flash 0930") }) +test("stop goes as the string the gateway takes, and an empty one not at all (magpie#6342)", () => { + const stop = (v) => x.prepareChatBody({ model: "GLM-5.2", messages: [], stop: v }).body + assert.equal(stop(["\n\n"]).stop, "\n\n") + assert.equal(stop(["", "###", "Observation:"]).stop, "###") + assert.equal(stop("###").stop, "###") + for (const v of [[], [""], [null, 3], "", null, 7]) assert.equal("stop" in stop(v), false, JSON.stringify(v)) + assert.equal("stop" in x.prepareChatBody({ model: "GLM-5.2", messages: [] }).body, false) +}) + test("prepareChatBody reshapes a chat into what the gateway accepts and meters", () => { const out = x.prepareChatBody({ model: "codearts/GLM-5.2", messages: [], max_tokens: 128000 }) assert.equal(out.fold, true) diff --git a/packages/codearts/package.json b/packages/codearts/package.json index 6549c81..5c98b5b 100644 --- a/packages/codearts/package.json +++ b/packages/codearts/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-codearts-auth", - "version": "0.3.14", + "version": "0.3.15", "description": "Huawei Cloud in OpenCode and magpie: CodeArts (码道) and OfficeAce (AgentArts), each from its own Huawei Cloud OAuth sign-in — models, credit rates, credit usage and the daily check-in (unofficial)", "type": "module", "main": "./opencode.mjs",