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).
This commit is contained in:
yetone
2026-11-12 22:50:15 +08:00
parent 4b35c34c34
commit 507dbcc459
3 changed files with 25 additions and 1 deletions
+15
View File
@@ -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) -----------------------------
/**
+9
View File
@@ -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)
+1 -1
View File
@@ -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",