diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index 4f399eb..19f327f 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -988,6 +988,74 @@ function toolDescriptions(tools) { const joinNonEmpty = (...s) => s.filter(Boolean).join("\n\n") +// objectRoot is a tool's parameters as a plain object at the root, with no +// anyOf, oneOf or allOf there: Devin's Claude models turn away a request +// offering a tool whose schema has one ("There is an issue with this +// request, please try a different model", magpie#1196; Codex desktop's +// codex_app automation_update has a root oneOf), as Anthropic does behind +// Factory (magpie#646). It folds the schema as magpie's built-in does +// (provider.ObjectRoot): allOf's branches are all merged, properties and +// required alike; of anyOf's and oneOf's object branches the properties are +// merged, a field each of them requires stays required, and the other +// branches go. Branches may be local $refs. A schema already a plain object +// is returned as it came; the caller's own is never changed. +function objectRoot(ps) { + if (!ps || typeof ps !== "object" || Array.isArray(ps)) return ps + const has = (k) => Object.hasOwn(ps, k) + if (ps.type === "object" && !has("anyOf") && !has("oneOf") && !has("allOf")) return ps + const out = { ...ps } + const props = { ...(isObj(ps.properties) ? ps.properties : {}) } + let required = Array.isArray(ps.required) ? [...ps.required] : [] + const ref = (b) => { + if (!isObj(b)) return null + if (typeof b.$ref !== "string" || !b.$ref) return b + for (const k of ["$defs", "definitions"]) { + const pre = "#/" + k + "/" + if (b.$ref.startsWith(pre)) { + const d = ps[k]?.[b.$ref.slice(pre.length)] + return isObj(d) ? d : null + } + } + return null + } + const merge = (b) => { + if (isObj(b.properties)) for (const [k, v] of Object.entries(b.properties)) if (!Object.hasOwn(props, k)) props[k] = v + } + for (const b of Array.isArray(ps.allOf) ? ps.allOf : []) { + const bm = ref(b) + if (!bm) continue + merge(bm) + if (Array.isArray(bm.required)) required.push(...bm.required) + } + delete out.allOf + const branches = [] + for (const k of ["anyOf", "oneOf"]) { + for (const b of Array.isArray(ps[k]) ? ps[k] : []) { + const bm = ref(b) + if (!bm || (bm.type !== "object" && !Object.hasOwn(bm, "properties"))) continue + branches.push(bm) + } + delete out[k] + } + // a field every branch requires stays required, beside the root's and + // allOf's own, which no branch can drop + let common = null + for (const b of branches) { + merge(b) + const br = Array.isArray(b.required) ? b.required : [] + common = common === null ? [...br] : common.filter((r) => br.includes(r)) + } + for (const r of common ?? []) if (!required.includes(r)) required.push(r) + required = [...new Set(required)] + out.type = "object" + out.properties = props + if (required.length) out.required = required + else delete out.required + return out +} + +const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v) + const osName = () => (process.platform === "win32" ? "windows" : process.platform) // metadata is what each request says of the client: the CLI's own (with @@ -1061,7 +1129,7 @@ function build(chat, uid, key) { // describe themselves, and the same words in a message go through let tools = (chat.tools ?? []) .filter((t) => (!t?.type || t.type === "function") && t.function?.name) - .map((t) => ({ name: t.function.name, description: t.function.description ?? "", schema: t.function.parameters })) + .map((t) => ({ name: t.function.name, description: t.function.description ?? "", schema: objectRoot(t.function.parameters) })) if (chat.tool_choice === "none") tools = [] const instructions = joinNonEmpty(system.join("\n\n"), toolDescriptions(tools)) if (instructions) { @@ -1460,4 +1528,4 @@ export async function DevinAuthPlugin() { } // for tests -export const _internal = { seesImages, imagesFor, forgetSaid: () => said.clear(), parseModelConfigs, SNAPSHOT_IMAGES, withImages, forgetImages: () => (seen = null), effortOf, runtimeModel, configModel, live, build, failure, complete, parseFamilies, listed, variantFor, familiesOf, SNAPSHOT, parseStatus, readsHome, tierName, whoByKey, success, familiesFor, readCredentials, credentials, fields, frame, PB, events, frames } +export const _internal = { objectRoot, seesImages, imagesFor, forgetSaid: () => said.clear(), parseModelConfigs, SNAPSHOT_IMAGES, withImages, forgetImages: () => (seen = null), effortOf, runtimeModel, configModel, live, build, failure, complete, parseFamilies, listed, variantFor, familiesOf, SNAPSHOT, parseStatus, readsHome, tierName, whoByKey, success, familiesFor, readCredentials, credentials, fields, frame, PB, events, frames } diff --git a/packages/devin/package.json b/packages/devin/package.json index 5df73b6..ccf3afc 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.8", + "version": "0.1.9", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/devin/schema.test.mjs b/packages/devin/schema.test.mjs new file mode 100644 index 0000000..2cf928b --- /dev/null +++ b/packages/devin/schema.test.mjs @@ -0,0 +1,108 @@ +// A tool whose parameters have anyOf, oneOf or allOf at the root goes to +// Devin as a plain object (magpie#1196): Devin's Claude models answered +// every request offering one with 502 "There is an issue with this request, +// please try a different model", and Codex desktop offers one in codex_app's +// automation_update. The schema is folded as magpie's built-in folds it for +// Anthropic (provider.ObjectRoot, magpie#646). +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const dec = new TextDecoder() + +// sent is each tool sent with the request: name -> its schema, parsed. +const sent = (b) => + Object.fromEntries( + _internal + .fields(b) + .filter((f) => f.num === 10 && f.wire === 2) + .map((f) => { + const g = _internal.fields(f.data) + return [dec.decode(g.find((h) => h.num === 1).data), JSON.parse(dec.decode(g.find((h) => h.num === 3).data))] + }), + ) + +// the parameters of the issue's minimal reproduction, byte for byte +const ISSUE = `{ + "type": "object", + "properties": {"a": {"type": "string"}}, + "oneOf": [{"properties": {"a": {"type": "string"}}}, {"properties": {"b": {"type": "string"}}}] + }` + +const chatWith = (parameters) => ({ + messages: [{ role: "user", content: "ok" }], + tools: [{ type: "function", function: { name: "t", description: "t", parameters } }], +}) + +for (const k of ["oneOf", "anyOf", "allOf"]) { + test(`a root ${k} goes to Devin as a plain object`, () => { + const parameters = JSON.parse(ISSUE.replace('"oneOf"', `"${k}"`)) + const before = JSON.stringify(parameters) + const got = sent(_internal.build(chatWith(parameters), "claude-opus-5-5-high", "k")).t + expect(got).toEqual({ type: "object", properties: { a: { type: "string" }, b: { type: "string" } } }) + // the caller's own schema is left as it came + expect(JSON.stringify(parameters)).toBe(before) + }) +} + +test("a schema with no root combinator is sent as it came", () => { + const parameters = { + type: "object", + properties: { a: { anyOf: [{ type: "string" }, { type: "null" }] }, n: { type: "integer" } }, + required: ["n"], + $defs: { x: { type: "string" } }, + } + const got = sent(_internal.build(chatWith(parameters), "claude-opus-5-5-high", "k")).t + expect(got).toEqual(parameters) +}) + +test("allOf's branches are all merged, required and all", () => { + expect( + _internal.objectRoot({ + type: "object", + properties: { id: { type: "string" } }, + required: ["id"], + allOf: [{ $ref: "#/definitions/when" }, { properties: { id: { type: "integer" } } }], + definitions: { when: { properties: { when: { type: "string" } }, required: ["when"] } }, + }), + ).toEqual({ + type: "object", + properties: { id: { type: "string" }, when: { type: "string" } }, + required: ["id", "when"], + definitions: { when: { properties: { when: { type: "string" } }, required: ["when"] } }, + }) +}) + +test("anyOf's object branches are merged, a field every one requires stays required, $refs are followed", () => { + const byName = { type: "object", properties: { name: { type: "string" }, when: { type: "string" } }, required: ["name", "when"] } + expect( + _internal.objectRoot({ + anyOf: [ + { $ref: "#/$defs/byName" }, + { type: "object", properties: { name: { type: "string" }, tag: { type: "string" } }, required: ["name", "tag"] }, + { type: "string" }, + { $ref: "#/$defs/missing" }, + ], + $defs: { byName }, + }), + ).toEqual({ + type: "object", + properties: { name: { type: "string" }, when: { type: "string" }, tag: { type: "string" } }, + required: ["name"], + $defs: { byName }, + }) +}) + +test("objectRoot keeps the root's own required beside anyOf's branches", () => { + const { objectRoot } = _internal + const out = objectRoot({ + type: "object", + properties: { id: { type: "string" } }, + required: ["id"], + anyOf: [ + { type: "object", properties: { a: { type: "string" } }, required: ["a"] }, + { type: "object", properties: { b: { type: "string" } }, required: ["b"] }, + ], + }) + expect(out.required).toEqual(["id"]) + expect(Object.keys(out.properties).sort()).toEqual(["a", "b", "id"]) +})