codearts: stop goes as a list on the international station (#998)
0.3.15 turned every stop into one string, because the mainland gateway refuses a list (magpie#6342). AyakaChen1 found the international station (snap-access.ap-southeast-1) is the other way round: a string is refused and a list works, so the normalisation broke international requests that used to pass. normalizeStop now takes the account's station: mainland still gets the first non-empty string, international gets every non-empty string as a list. Empty or non-string entries are dropped on both. Run: bun test packages/codearts on WSL with a temp HOME, 204 pass. bun scripts/check.mjs codearts ok. Broken by hand: with 0.3.15's index.mjs and the new tests, the stop test and the international station test fail (2 fail). Not tried: a real international account; the list shape comes from the reporter's measurement. Reported-by: AyakaChen1
This commit is contained in:
+14
-12
@@ -1041,7 +1041,7 @@ function isRegularModel(model, state) {
|
||||
* agent's own, to put back in the answer; null when none was cut).
|
||||
* A body that is not a chat (no `model` + `messages`) gives null.
|
||||
*/
|
||||
function prepareChatBody(parsed, { thinking = true } = {}) {
|
||||
function prepareChatBody(parsed, { thinking = true, station = "mainland" } = {}) {
|
||||
if (!parsed || typeof parsed.model !== "string" || !Array.isArray(parsed.messages)) return null
|
||||
const body = { ...parsed, model: upstreamModelId(parsed.model) }
|
||||
const names = shortenToolNames(body)
|
||||
@@ -1079,24 +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)
|
||||
normalizeStop(body, station)
|
||||
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.
|
||||
* `stop` in the shape the credential's station takes; the other shape comes
|
||||
* back as HTTP 200 with `InferHub.001001005.400` "The request param is
|
||||
* invalid". The mainland gateway takes a string only (magpie#6342, measured
|
||||
* with every model), so a list goes as its first non-empty string. The
|
||||
* international one takes a list only (#998, GLM-5.1: `["zzz"]` answered,
|
||||
* `"zzz"` refused), so a string goes as a list of one. An empty or blank
|
||||
* value, or one of another kind, is left out on both.
|
||||
*/
|
||||
function normalizeStop(body) {
|
||||
function normalizeStop(body, station = "mainland") {
|
||||
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
|
||||
const all = (Array.isArray(body.stop) ? body.stop : [body.stop]).filter((x) => typeof x === "string" && x !== "")
|
||||
if (!all.length) delete body.stop
|
||||
else body.stop = station === "international" ? all : all[0]
|
||||
}
|
||||
|
||||
// ---- tool names over the gateway's limit (#432) -----------------------------
|
||||
@@ -1827,7 +1829,7 @@ async function signedFetch(cred, input, init, context) {
|
||||
else body = new Uint8Array()
|
||||
// The body is part of the signature, so it is reshaped before signing.
|
||||
const parsed = body.length > 0 ? safeJson(new TextDecoder().decode(body)) : null
|
||||
const chat = prepareChatBody(parsed, context)
|
||||
const chat = prepareChatBody(parsed, { ...context, station: stationOf(cred).id })
|
||||
if (chat) body = utf8(JSON.stringify(chat.body))
|
||||
const model = upstreamModelId(parsed?.model)
|
||||
let extra = isBenefitModel(model, context.state) ? { maas_type: "benefit" } : undefined
|
||||
|
||||
@@ -251,6 +251,11 @@ test("stop goes as the string the gateway takes, and an empty one not at all (ma
|
||||
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)
|
||||
// the international station takes a list only (#998)
|
||||
const intl = (v) => x.prepareChatBody({ model: "GLM-5.1", messages: [], stop: v }, { station: "international" }).body
|
||||
assert.deepEqual(intl("zzz").stop, ["zzz"])
|
||||
assert.deepEqual(intl(["", "###", 3, "Observation:"]).stop, ["###", "Observation:"])
|
||||
for (const v of [[], [""], [null, 3], "", null, 7]) assert.equal("stop" in intl(v), false, JSON.stringify(v))
|
||||
})
|
||||
|
||||
test("prepareChatBody reshapes a chat into what the gateway accepts and meters", () => {
|
||||
@@ -4618,6 +4623,8 @@ test("an international account chats, reads and renews on its own station only",
|
||||
// the first goes with a credential the vendor calls expired
|
||||
if (chats === 1) return new Response(JSON.stringify({ error_code: "APIG.0602", error_msg: "the security token has expired" }), { status: 400 })
|
||||
assert.match(auth, /Access=IDEAK2,/)
|
||||
// the station's own shape of stop: a list (#998)
|
||||
assert.deepEqual(JSON.parse(typeof init.body === "string" ? init.body : new TextDecoder().decode(init.body)).stop, ["zzz"])
|
||||
return sseResponse(okStream)
|
||||
}
|
||||
if (u.includes("/statistics/plugin")) return new Response(JSON.stringify(intlStatistics), { status: 200 })
|
||||
@@ -4631,7 +4638,7 @@ test("an international account chats, reads and renews on its own station only",
|
||||
assert.equal(loaded.baseURL, `${INTL}/api/v2`)
|
||||
await rewriteIde(fx.file, ideValue(ideOrigin({ accessKey: "IDEAK2", securityToken: "IDETOKEN2" })))
|
||||
// the config's mainland `api` is taken to the account's station
|
||||
const res = await loaded.fetch(CHAT_URL, chatInit({ model: "GLM-5.2", messages: [], stream: true }))
|
||||
const res = await loaded.fetch(CHAT_URL, chatInit({ model: "GLM-5.2", messages: [], stream: true, stop: "zzz" }))
|
||||
assert.equal(res.status, 200)
|
||||
assert.equal(chats, 2)
|
||||
assert.equal(JSON.parse(sets.at(-1).body.access).access_key_id, "IDEAK2")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-codearts-auth",
|
||||
"version": "0.3.15",
|
||||
"version": "0.3.16",
|
||||
"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",
|
||||
|
||||
Reference in New Issue
Block a user