qoder 0.2.35: a oneOf of objects with no type reaches kmodel typed object (#378)

congee949 found that qoder-cn's kmodel_latest (Kimi K3.1 Pro) fails with
502 on dsh's REA tools because of a oneOf with no type whose branches are
all objects. With "type": "object" beside each such oneOf, the same
requests got 200 in a real dsh run. qoderBody now sends each tool's
parameters through typedUnions, which types such a oneOf object when each
branch is typed object, directly or through local $refs. Other unions,
anyOf, and missing, external or circular $refs are left alone. The work is
done on a copy, so the client's schema is not changed. This is the same
rule workbuddy 0.1.33 applies.

Run: `bun test` in packages/qoder with a sandbox HOME/TMPDIR and a dead
HTTPS_PROXY: 225 pass, 0 fail. `bun scripts/check.mjs qoder`: ok.
Broken by hand: with qoderBody sending parameters unchanged,
schema.test.mjs goes 1 red. Not tried: a real Qoder account.
This commit is contained in:
yetone
2026-10-25 14:54:25 +08:00
parent def6a83584
commit ebe3555b30
3 changed files with 77 additions and 3 deletions
+38 -2
View File
@@ -951,6 +951,42 @@ function taskOf(sid, chat, now = Date.now()) {
return t
}
// Qoder CN's kmodel (Kimi K3.1 Pro) fails a chat whose tools hold a oneOf
// with no type, even of objects only, and takes it with "type": "object"
// beside it (congee949, #378: dsh's REA tools, a 502 here). typedUnions is
// the schema with each such oneOf typed object when each branch is typed
// object itself or through a chain of local $refs; nothing else changes,
// and the caller's own schema is not touched.
const SUB_ONE = ["items", "additionalItems", "unevaluatedItems", "contains", "not", "if", "then", "else", "propertyNames", "additionalProperties", "unevaluatedProperties"]
const SUB_LIST = ["anyOf", "oneOf", "allOf", "prefixItems"]
const SUB_MAP = ["properties", "patternProperties", "$defs", "definitions", "dependentSchemas"]
function typedUnions(schema) {
if (!schema || typeof schema !== "object") return schema
const root = structuredClone(schema)
const isObj = (v) => v && typeof v === "object" && !Array.isArray(v)
const target = (ref) => {
if (!ref.startsWith("#/")) return undefined
let at = root
for (const k of ref.slice(2).split("/")) at = at?.[k.replaceAll("~1", "/").replaceAll("~0", "~")]
return isObj(at) ? at : undefined
}
const object = (b, seen = []) => {
if (!isObj(b)) return false
if ("type" in b) return b.type === "object"
if (typeof b.$ref !== "string" || seen.includes(b.$ref) || seen.length > 16) return false
return object(target(b.$ref), [...seen, b.$ref])
}
const walk = (s, depth) => {
if (!isObj(s) || depth > 64) return
if (!("type" in s) && Array.isArray(s.oneOf) && s.oneOf.length && s.oneOf.every((b) => object(b))) s.type = "object"
for (const k of SUB_ONE) walk(s[k], depth + 1)
for (const k of SUB_LIST) for (const v of Array.isArray(s[k]) ? s[k] : []) walk(v, depth + 1)
for (const k of SUB_MAP) if (isObj(s[k])) for (const v of Object.values(s[k])) walk(v, depth + 1)
}
walk(root, 0)
return root
}
// qoderBody is the plaintext agent_chat_generation takes for a chat
// completion request, for model m, in the session the agent named (named,
// "" when none) and the task it is a call of (taskOf's, a new one when
@@ -988,7 +1024,7 @@ function qoderBody(chat, m, named = "", task, site) {
if (tools.length)
body.tools = tools.map((t) => ({
type: "function",
function: { name: t.function.name, description: t.function.description ?? "", parameters: t.function.parameters ?? { type: "object", properties: {} } },
function: { name: t.function.name, description: t.function.description ?? "", parameters: typedUnions(t.function.parameters) ?? { type: "object", properties: {} } },
}))
return body
}
@@ -2585,4 +2621,4 @@ export async function QwenWorkEnterpriseAuthPlugin({ client }) {
export const protocol = { makePlugin, deviceSignIn, openapi, siteOf }
// for tests
export const _internal = { campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, 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 = { typedUnions, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-qoder-auth",
"version": "0.2.34",
"version": "0.2.35",
"description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship (VPC)",
"type": "module",
"main": "./index.mjs",
+38
View File
@@ -0,0 +1,38 @@
// Qoder CN's kmodel_latest (Kimi K3.1 Pro) fails a chat whose tools hold a
// oneOf with no type, even of objects only, and takes it with "type":
// "object" beside it (congee949, #378). The parameters are the issue's own.
import { expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const { qoderBody, typedUnions } = _internal
const m = { key: "kmodel_latest", config: { key: "kmodel_latest" }, context: 128_000 }
const ab = [
{ type: "object", properties: { mode: { type: "string", const: "a" } } },
{ type: "object", properties: { mode: { type: "string", const: "b" } } },
]
const nested = { type: "object", properties: { x: { $ref: "#/$defs/X" } }, $defs: { X: { type: "object", properties: { y: { oneOf: ab } }, required: ["y"] } } }
test("a oneOf of objects with no type goes typed object, the client's tool untouched", () => {
const parameters = { type: "object", properties: { x: { $ref: "#/$defs/X" } }, $defs: { X: { oneOf: ab } } }
const chat = { messages: [{ role: "user", content: "Do not call tools. Reply exactly OK." }], tools: [{ type: "function", function: { name: "probe", description: "Diagnostic placeholder.", parameters } }] }
const before = structuredClone(chat)
const sent = qoderBody(chat, m).tools[0].function.parameters
expect(sent.$defs.X).toEqual({ oneOf: ab, type: "object" })
expect(sent.properties.x).toEqual({ $ref: "#/$defs/X" })
expect(chat).toEqual(before)
expect(typedUnions(nested).$defs.X.properties.y.type).toBe("object")
})
test("a oneOf whose branches are $refs to objects is typed; mixed, anyOf, missing, external and circular are left", () => {
const refs = { type: "object", properties: { s: { $ref: "#/$defs/__schema0" } }, $defs: { __schema0: { oneOf: [{ $ref: "#/$defs/__schema1" }, { $ref: "#/$defs/__schema3" }] }, __schema1: { type: "object" }, __schema3: { $ref: "#/$defs/__schema4" }, __schema4: { type: "object" } } }
expect(typedUnions(refs).$defs.__schema0.type).toBe("object")
const left = { type: "object", properties: {
mixed: { oneOf: [ab[0], { type: "string" }] },
any: { anyOf: ab },
missing: { oneOf: [ab[0], { $ref: "#/$defs/Nope" }] },
external: { oneOf: [ab[0], { $ref: "other.json#/X" }] },
loop: { oneOf: [ab[0], { $ref: "#/$defs/A" }] },
examples: { type: "array", examples: [{ oneOf: ab }] },
}, $defs: { A: { $ref: "#/$defs/B" }, B: { $ref: "#/$defs/A" } } }
expect(typedUnions(left)).toEqual(left)
})