codearts: tell the host the reasoning levels, and honour the caller's (#680)

Tells the host a CodeArts model's reasoning levels (model row `variants`, per model, measured 2026-11-04 by Gyl1219) and honours the level a caller names: passed on as is with no injected `thinking: enabled`, a level the model does not take is dropped, none/off/disabled send `thinking: {type: "disabled"}`. Version 0.3.2 -> 0.3.3, README updated.

Run at the reviewed head 95acc1e3 on mote (Mac mini): `bun scripts/check.mjs codearts` exit 0; `bun test packages/codearts` 169 pass / 0 fail. Broken by hand, each one turned a test red: the drop-unknown-level branch made unconditional, the `spoken === ""` condition removed from `injected`, the `thinking: disabled` assignment removed, `variants` left off the model row, a caller's own variants overwritten. Tests use the injected fetch, none reach the network.

Not tried: a real CodeArts account (I took the author's 2026-11-04 measurements on trust). Open question left on the PR: kimi-k3.1-pro is declared without `medium` though the author's table shows it answered.
This commit is contained in:
Gyl1219
2026-11-04 04:10:06 +08:00
committed by GitHub
parent 016a869062
commit fae4bc2e13
4 changed files with 198 additions and 3 deletions
+10
View File
@@ -94,6 +94,16 @@ rate), shown as `DeepSeek-V4.1-Flash-1017`, not folded into `DeepSeek-V4.1-Flash
back into one `chat.completion` when the caller asked for JSON.
- `thinking: { type: "enabled" }` is sent unless the caller set it, so the reasoning lands in
`reasoning_content` and `content` is only the reply.
- The reasoning level a caller names in `reasoning_effort` is sent as it is, and the plugin asks
for no thinking of its own then (`thinking: enabled` would overrule the level). A level the model
does not take is dropped rather than left to turn the request away — one it does not take is a
`400` ("reasoning_effort must be none or low or medium or high or xhigh or max"). "No thinking"
(`none`, `off`) is sent as `thinking: { type: "disabled" }`, which is what actually stops it:
`reasoning_effort: none` alone still thinks.
- The levels each model takes are declared in the model row's `variants` (measured 2026-11-04;
they differ per model — `glm-5.3-flash` turns `medium` and `xhigh` away, and the two Kimis think
at `none` anyway, so they take no "no thinking" level), so a host offers the levels that work
and shows the thinking as its own row instead of as the answer.
- Errors the gateway puts inside a `200` stream (spent quota, unknown model) come back as
`429` / `400` / `502` with the vendor's words. The `codearts/` prefix is stripped and output
budgets are clamped to 65536.
+115 -2
View File
@@ -192,6 +192,63 @@ const MAX_OUTPUT_TOKENS = 65_536
// caller shows the thinking as the answer; asked for explicitly it goes to
// `reasoning_content` and `content` is the reply only.
const ENABLE_THINKING = { type: "enabled" }
// The way to stop the thinking altogether (measured 2026-11-03: three runs in
// a row, no reasoning at all). `reasoning_effort: none` alone does not: the
// model thinks anyway (89, 91 and 135 characters of reasoning in three runs).
const DISABLE_THINKING = { type: "disabled" }
// The gateway's own word for "no thinking" is `none`; a host that says `off`
// (or `disabled`) means the same thing.
const NO_THINKING = new Set(["none", "off", "disabled"])
// The reasoning levels each model takes, measured 2026-11-04 against the
// gateway (the account of the report that opened this): a level the model does
// not take is a 400
// "parameter check failed, reasoning_effort must be none or low or medium or
// high or xhigh or max (InferHub.ModelArts.81001.400)"
// — and which of those it takes differs per model, so the list is per model
// and not one list for all.
//
// The levels are the gateway's own words, which is what a host sends: magpie
// reads a plugin's model row's `variants` (its keys are the levels, internal/
// provider/plugins.go: `c.Efforts, c.Reasoning = m.Variants, true`) and writes
// them into the DSH route as `reasoningEfforts`, the value sent being the
// model's own name for the level.
//
// "none" is the level for no thinking; a model that does not take it is left
// without it (the host then offers no way to turn the thinking off, which is
// the truth for that model).
//
// The keys are lower-cased: the agent's own list names a model "GLM-5.2", the
// gateway's list "glm-5.2", and they are one model.
const REASONING_LEVELS = {
"deepseek-v4.1-flash-1017": ["none", "low", "medium", "high", "xhigh", "max"],
"deepseek-v4.1-flash": ["none", "low", "medium", "high", "xhigh", "max"],
"glm-5.3-flash": ["none", "low", "high", "max"],
"glm-5.3": ["none", "low", "medium", "high", "xhigh", "max"],
"glm-5.2": ["none", "low", "medium", "high", "xhigh", "max"],
"glm-5.2-sft-harmony": ["none", "low", "medium", "high", "xhigh", "max"],
"openpangu-2.0-pro": ["none", "low", "medium", "high", "xhigh", "max"],
"openpangu-2.0-flash": ["none", "low", "medium", "high", "xhigh", "max"],
"speed": ["none", "low", "medium", "high", "xhigh", "max"],
// The two Kimis take no "none": they think at it anyway (82 and 290
// characters of reasoning at `none`, 22 and 332 at `thinking: disabled`), so
// there is no level that turns their thinking off — offering one would be
// saying what the model does not do.
"kimi-k3": ["low", "medium", "high", "xhigh", "max"],
"kimi-k3.1-pro": ["low", "high", "xhigh", "max"],
}
// The levels of a model, or none when it does not think (or takes none that
// were measured).
const reasoningLevelsOf = (model) => REASONING_LEVELS[String(model ?? "").toLowerCase()] ?? []
/**
* A model's levels as the host is told of them: an object keyed by the level,
* each with the effort sent for it — the shape every plugin in this repo uses
* (grok, qoder, workbuddy, zen-free).
*/
const reasoningVariants = (levels) =>
Object.fromEntries(levels.map((level) => [level, { reasoningEffort: level }]))
// Benefits (free-quota) models require `maas_type: benefit` as a *signed*
// header, or the backend answers InferHub.002002009.404 "model is not
@@ -968,7 +1025,35 @@ function prepareChatBody(parsed, { thinking = true } = {}) {
const body = { ...parsed, model: upstreamModelId(parsed.model) }
const names = shortenToolNames(body)
// `injected`: ours to take back should a model refuse the parameter.
const injected = thinking && body.thinking === undefined
//
// The caller's level is the gateway's own word for it (measured 2026-11-04:
// a level the model does not take is a 400 — "reasoning_effort must be none
// or low or medium or high or xhigh or max"), and which levels a model takes
// differs per model, so a level this model does not take is dropped rather
// than left to turn the whole request away.
//
// A level that the caller named is passed on as it is and the plugin asks for
// no thinking of its own: `thinking: enabled` would overrule the level.
//
// "No thinking" is the one thing the level alone does not do (measured:
// `reasoning_effort: none` still thought, 89 to 135 characters of it), so it
// is the switch that is used: `thinking: disabled` is the gateway's own way
// of not thinking at all.
const spoken = typeof body.reasoning_effort === "string" ? body.reasoning_effort.trim().toLowerCase() : ""
const levels = reasoningLevelsOf(body.model)
if (spoken !== "") {
if (NO_THINKING.has(spoken)) {
delete body.reasoning_effort
if (body.thinking === undefined) body.thinking = DISABLE_THINKING
} else if (levels.length === 0 || levels.includes(spoken)) {
body.reasoning_effort = spoken
} else {
// a level this model does not take: the request goes as it was written,
// without the level, so the model serves it its own way
delete body.reasoning_effort
}
}
const injected = thinking && spoken === "" && body.thinking === undefined
if (injected) body.thinking = ENABLE_THINKING
for (const k of ["max_tokens", "max_completion_tokens"]) {
if (typeof body[k] === "number" && body[k] > MAX_OUTPUT_TOKENS) body[k] = MAX_OUTPUT_TOKENS
@@ -2515,6 +2600,24 @@ export const CodeArtsAuthPlugin = async ({ client } = {}, options = {}) => {
modalities: { input: ["text"], output: ["text"] },
})
/**
* The reasoning levels a host is told about.
*
* ⚠️ Without them the host does not treat the model as one that reasons
* (measured 2026-11-03): the thinking arrives in `reasoning_content`, the host
* has no row for it, and the reasoning text is rendered as the answer — the
* caller sees its own thinking where the reply should be.
*
* They go in the model row's `variants`, whose keys are the levels (magpie
* reads them as the model's levels: internal/provider/plugins.go, `c.Efforts,
* c.Reasoning = m.Variants, true`) — the shape every plugin in this repo
* uses. The value sent for a level is the model's own word for it.
*
* Which levels a model takes differs per model (measured 2026-11-04), so
* they are per model; a model that takes none is not said to think at all.
*/
const reasoningVariantsOf = (model) => reasoningVariants(reasoningLevelsOf(model))
const modelOf = (provider, live, state) => {
const base = provider.models?.[live.id] ?? {}
const capabilities = {
@@ -2529,13 +2632,23 @@ export const CodeArtsAuthPlugin = async ({ client } = {}, options = {}) => {
// as free while billed as paid) and `rate` (the credits a request costs,
// as a multiple: the IDE's 「消耗系数」).
const free = live.benefit === true && !state?.noBenefit.has(live.id)
// A model that reasons is told so, with the levels it takes: without them
// the host has no row for the thinking and renders it as the answer (see
// reasoningVariantsOf). A caller's own variants stand.
const variants = base.variants ? undefined : reasoningVariantsOf(live.id)
const levels = variants ? Object.keys(variants) : []
const thinks = levels.length > 0 || live.reasoning === true
return {
...base,
id: live.id,
name: live.name || base.name || live.id,
limit: { context: live.context ?? base.limit?.context ?? 0, output: live.output ?? MAX_OUTPUT_TOKENS },
capabilities,
capabilities: {
...capabilities,
...(thinks ? { reasoning: true } : {}),
},
free,
...(levels.length > 0 ? { variants } : {}),
...(!free && live.rate > 0 ? { rate: live.rate } : {}),
}
}
+72
View File
@@ -254,6 +254,42 @@ test("prepareChatBody reshapes a chat into what the gateway accepts and meters",
assert.equal(x.prepareChatBody(null), null)
})
// The caller's level is the gateway's own (measured 2026-11-03: none, low,
// medium, high, xhigh and max are accepted, every other value is a 400), and a
// level that was named is passed on as it is: `thinking: enabled` would
// overrule it. "No thinking" is the switch, not the level: `reasoning_effort:
// none` alone still thought (89 to 135 characters of it).
test("the caller's reasoning level is the model's own", () => {
// A level the model takes is passed on as it is: `thinking: enabled` would
// overrule it.
for (const level of ["low", "medium", "high", "xhigh", "max"]) {
const out = x.prepareChatBody({ model: "deepseek-v4.1-flash-1017", messages: [], reasoning_effort: level })
assert.equal(out.body.reasoning_effort, level)
assert.equal(out.body.thinking, undefined, level + " is the level; nothing else is asked")
}
// A host's word for no thinking is the gateway's way of not thinking.
for (const word of ["off", "disabled", "none"]) {
const out = x.prepareChatBody({ model: "deepseek-v4.1-flash-1017", messages: [], reasoning_effort: word })
assert.deepEqual(out.body.thinking, { type: "disabled" }, word + " means no thinking")
assert.equal("reasoning_effort" in out.body, false, "the level would only be a hint")
}
// A level the model does not take would turn the whole request away (a 400),
// so it is dropped and the request goes as the model serves it.
const notTaken = x.prepareChatBody({ model: "glm-5.3-flash", messages: [], reasoning_effort: "medium" })
assert.equal("reasoning_effort" in notTaken.body, false, "glm-5.3-flash takes no medium")
assert.deepEqual(x.prepareChatBody({ model: "glm-5.3-flash", messages: [], reasoning_effort: "high" }).body.reasoning_effort, "high")
// A word no model takes is dropped as well.
assert.equal("reasoning_effort" in x.prepareChatBody({ model: "deepseek-v4.1-flash-1017", messages: [], reasoning_effort: "loud" }).body, false)
// A caller that said nothing still gets the thinking, as before.
assert.deepEqual(x.prepareChatBody({ model: "deepseek-v4.1-flash-1017", messages: [] }).body.thinking, { type: "enabled" })
// A caller that said so itself is not overruled.
assert.deepEqual(x.prepareChatBody({ model: "deepseek-v4.1-flash-1017", messages: [], thinking: { type: "disabled" } }).body.thinking, { type: "disabled" })
})
// The free model's TPM limit, word for word as the gateway sent it in a 200.
const tpmEvent = 'data:{"text":"[DONE]","error_code":"InferHub.ModelArts.81111.429","error_msg":"The model TPM limit has been significantly exceeded. Please reduce the request rate and retry later."}'
@@ -1977,6 +2013,42 @@ test("provider.models: free models once claimed, rates and parameters from the C
assert.equal(seen.filter((s) => s.url === x.AGENT_LIST_URL).length, 1)
})
// A model that reasons is told so, with the levels it takes: without them the
// host has no row for the thinking and renders the reasoning text as the answer
// (measured 2026-11-03 on a DSH host: the caller's own thinking showed up
// where the reply should have been).
//
// The levels go in the model row's `variants` — their keys are the levels, and
// magpie reads them as the model's levels (internal/provider/plugins.go:
// `c.Efforts, c.Reasoning = m.Variants, true`).
test("provider.models: a model that reasons is given the levels it takes", async () => {
const cred = await makeCred()
const plugin = await CodeArtsAuthPlugin({ client: {} }, { fetch: modelsFetcher([], { agents: agentList, detail: agentDetail }) })
const models = await plugin.provider.models({ models: {} }, { auth: makeAuth(cred) })
const thinking = models["GLM-5.2"]
assert.equal(thinking.capabilities.reasoning, true)
assert.deepEqual(Object.keys(thinking.variants), ["none", "low", "medium", "high", "xhigh", "max"])
assert.deepEqual(thinking.variants.low, { reasoningEffort: "low" })
// A model measured to think is said to, whatever the vendor's own list says
// of it (the vendor names no level for any of them).
assert.deepEqual(Object.keys(models["glm-5.3"].variants), ["none", "low", "medium", "high", "xhigh", "max"])
// A model of no measured level offers none: the host then asks for no
// thinking at all.
assert.equal(models["deepseek-v4-flash-0731"].capabilities.reasoning, undefined)
assert.equal(models["deepseek-v4-flash-0731"].variants, undefined)
})
test("provider.models: a caller's own variants stand", async () => {
const cred = await makeCred()
const plugin = await CodeArtsAuthPlugin({ client: {} }, { fetch: modelsFetcher([], { agents: agentList, detail: agentDetail }) })
const provider = { models: { "GLM-5.2": { variants: { low: { reasoningEffort: "low" }, high: { reasoningEffort: "high" } } } } }
const models = await plugin.provider.models(provider, { auth: makeAuth(cred) })
assert.deepEqual(Object.keys(models["GLM-5.2"].variants), ["low", "high"])
})
test("the model list comes from the main agent: original_id build, then CodeAgent, then the IDE's id", async () => {
const cred = { access_key_id: "AK", secret_access_key: "SK", security_token: "ST" }
const detailOf = (models) => new Response(JSON.stringify({ gpts: { models } }), { status: 200 })
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-codearts-auth",
"version": "0.3.2",
"version": "0.3.3",
"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",