diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 3c75e89..f16ca63 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -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 } diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 81669a8..f281d4e 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -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", diff --git a/packages/qoder/schema.test.mjs b/packages/qoder/schema.test.mjs new file mode 100644 index 0000000..2b370c8 --- /dev/null +++ b/packages/qoder/schema.test.mjs @@ -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) +})