diff --git a/bin/antigravity-direct.mjs b/bin/antigravity-direct.mjs index 5256353c38..7e00708baf 100755 --- a/bin/antigravity-direct.mjs +++ b/bin/antigravity-direct.mjs @@ -314,6 +314,16 @@ const resolveModelAndEffort = (requestedModel, requestedEffort) => { // Probed 2026-11-03 (opencode#1548): an ARRAY schema with neither items // nor prefixItems is likewise rejected ("items: missing field"), so a // bare {type: "array"} gets an unconstrained items synthesized below. +// Probed 2026-11-03 (opencode#1550): for Claude models the upstream +// re-serializes the proto Schema into an Anthropic input_schema, and +// Anthropic's validator rejects `anyOf` outright -- any branch count, any +// position, root or nested ("tools.N.custom.input_schema: JSON schema is +// invalid. It must match JSON Schema draft 2020-12") -- while accepting +// oneOf and allOf in every probed shape. Gemini models accept anyOf +// verbatim. So for Claude targets anyOf is emitted as oneOf below: tool +// unions are disjoint alternatives in practice (type unions, nullable +// encodings), where exactly-one is the faithful reading, and oneOf is +// the only union keyword the Anthropic side takes. // --------------------------------------------------------------------------- const SCHEMA_FIELDS = new Set([ @@ -376,7 +386,7 @@ const resolveSchemaRefs = (root) => { return walkSchema(root, []); }; -const convertSchemaNode = (node) => { +const convertSchemaNode = (node, anthropic = false) => { // Boolean schemas and stray values become an unconstrained object schema. if (!isSchemaRecord(node)) return {}; const out = {}; @@ -407,23 +417,31 @@ const convertSchemaNode = (node) => { if (k === 'properties') { if (isSchemaRecord(v)) { const props = {}; - for (const [name, sub] of Object.entries(v)) defineValue(props, name, convertSchemaNode(sub)); + for (const [name, sub] of Object.entries(v)) defineValue(props, name, convertSchemaNode(sub, anthropic)); out.properties = props; } continue; } if (k === 'anyOf' || k === 'oneOf' || k === 'allOf') { - if (Array.isArray(v)) out[k] = v.map(convertSchemaNode); + if (Array.isArray(v)) { + const branches = v.map(sub => convertSchemaNode(sub, anthropic)); + if (k === 'allOf') out.allOf = branches; + // anyOf folds into oneOf for Anthropic targets (see the header + // probe note); a node carrying both keywords merges both branch + // sets into the single oneOf the Anthropic side accepts. + else if (k === 'oneOf' || anthropic) out.oneOf = out.oneOf ? [...out.oneOf, ...branches] : branches; + else out.anyOf = branches; + } continue; } - if (k === 'not') { out.not = convertSchemaNode(v); continue; } + if (k === 'not') { out.not = convertSchemaNode(v, anthropic); continue; } if (k === 'type' || k === 'required' || k === 'minimum' || k === 'maximum' || k === 'items' || k === 'prefixItems') continue; // handled outside the loop out[k] = v; // scalar constraint fields: format, title, pattern, ... } if (minimum !== undefined) out.minimum = minimum; if (maximum !== undefined) out.maximum = maximum; - if (items !== undefined) out.items = convertSchemaNode(items); - if (prefixItems !== undefined) out.prefixItems = (Array.isArray(prefixItems) ? prefixItems : [prefixItems]).map(convertSchemaNode); + if (items !== undefined) out.items = convertSchemaNode(items, anthropic); + if (prefixItems !== undefined) out.prefixItems = (Array.isArray(prefixItems) ? prefixItems : [prefixItems]).map(sub => convertSchemaNode(sub, anthropic)); // Type: uppercase it; collapse an array of types ("null" -> nullable, // first concrete type wins); infer OBJECT when properties are present // (the proto allows properties only for OBJECT). @@ -455,8 +473,8 @@ const convertSchemaNode = (node) => { return out; }; -const convertSchema = (schema) => { - const converted = convertSchemaNode(resolveSchemaRefs(isSchemaRecord(schema) ? schema : {})); +const convertSchema = (schema, anthropic = false) => { + const converted = convertSchemaNode(resolveSchemaRefs(isSchemaRecord(schema) ? schema : {}), anthropic); // The parameters root must declare OBJECT (tool-schema.ts's objectRoot: // providers reject a tool schema whose root does not declare it). if (converted.type === undefined) converted.type = 'OBJECT'; @@ -500,7 +518,7 @@ const toolChoiceConfig = (toolChoice, declaredNames) => { }; const buildV1Request = (body) => { - const { fullName, effort } = resolveModelAndEffort(body.model, body.reasoning_effort); + const { base, fullName, effort } = resolveModelAndEffort(body.model, body.reasoning_effort); const messages = Array.isArray(body.messages) ? body.messages : []; const systemParts = []; const contents = []; @@ -573,7 +591,7 @@ const buildV1Request = (body) => { for (const t of body.tools) { const fn = t && t.type === 'function' ? t.function : t; if (fn && fn.name) { - decls.push({ name: fn.name, description: fn.description || '', parameters: convertSchema(fn.parameters || { type: 'object', properties: {} }) }); + decls.push({ name: fn.name, description: fn.description || '', parameters: convertSchema(fn.parameters || { type: 'object', properties: {} }, base.startsWith('claude')) }); declaredNames.push(fn.name); } } diff --git a/bin/antigravity-direct.test.mjs b/bin/antigravity-direct.test.mjs index 7c4d88e885..d0d12832cd 100644 --- a/bin/antigravity-direct.test.mjs +++ b/bin/antigravity-direct.test.mjs @@ -223,6 +223,63 @@ test('bare array tool properties reach upstream with synthesized items (opencode assertArraysCarryItems(decls[0].parameters, 'parameters'); }); +// --- opencode#1550: anyOf folds to oneOf for Claude targets --------------- +// For Claude models the upstream re-serializes the proto Schema into an +// Anthropic input_schema, and Anthropic's validator rejects anyOf in any +// position ("tools.N.custom.input_schema: JSON schema is invalid. It must +// match JSON Schema draft 2020-12"), which 400'd workhorse sessions on +// the Claude leaf all day 2026-11-03 (any tool with a zod-union or +// nullable-anyOf property). Conversion for Claude targets must emit +// oneOf instead; Gemini targets keep anyOf verbatim. + +const assertNoKey = (node, key, where) => { + if (Array.isArray(node)) { node.forEach((v, i) => assertNoKey(v, key, `${where}[${i}]`)); return; } + if (!node || typeof node !== 'object') return; + assert.ok(!(key in node), `${where}: unexpected ${key}`); + for (const [k, v] of Object.entries(node)) assertNoKey(v, key, `${where}.${k}`); +}; + +const UNION_TOOLS = [{ + type: 'function', + function: { + name: 'probe_union', + description: 'union-shaped parameters', + parameters: { + type: 'object', + properties: { + nullable_note: { anyOf: [{ type: 'string' }, { type: 'null' }] }, + either: { anyOf: [{ type: 'string' }, { type: 'number' }] }, + listed: { type: 'array', items: { anyOf: [{ type: 'string' }, { type: 'boolean' }] } }, + pick: { oneOf: [{ type: 'string' }, { type: 'number' }] } + } + } + } +}]; + +test('anyOf tool schemas fold to oneOf for claude models (opencode#1550)', async () => { + const { status } = await chat({ model: 'claude-sonnet-5-5', tools: UNION_TOOLS }); + assert.equal(status, 200); + const decls = lastFunctionDeclarations(); + assert.equal(decls.length, 1); + const props = decls[0].parameters.properties; + assert.deepEqual(props.nullable_note, { oneOf: [{ type: 'STRING' }, { type: 'NULL' }] }); + assert.deepEqual(props.either, { oneOf: [{ type: 'STRING' }, { type: 'NUMBER' }] }); + assert.deepEqual(props.listed, { type: 'ARRAY', items: { oneOf: [{ type: 'STRING' }, { type: 'BOOLEAN' }] } }); + assert.deepEqual(props.pick, { oneOf: [{ type: 'STRING' }, { type: 'NUMBER' }] }); + assertNoKey(decls[0].parameters, 'anyOf', 'parameters'); +}); + +test('anyOf tool schemas stay anyOf for gemini models (opencode#1550)', async () => { + const { status } = await chat({ tools: UNION_TOOLS }); + assert.equal(status, 200); + const decls = lastFunctionDeclarations(); + const props = decls[0].parameters.properties; + assert.deepEqual(props.nullable_note, { anyOf: [{ type: 'STRING' }, { type: 'NULL' }] }); + assert.deepEqual(props.either, { anyOf: [{ type: 'STRING' }, { type: 'NUMBER' }] }); + assert.deepEqual(props.listed, { type: 'ARRAY', items: { anyOf: [{ type: 'STRING' }, { type: 'BOOLEAN' }] } }); + assert.deepEqual(props.pick, { oneOf: [{ type: 'STRING' }, { type: 'NUMBER' }] }); +}); + // --- #2823: env parsing is strict ----------------------------------------- // A bad ANTIGRAVITY_DIRECT_PORT used to reach listen() as an anonymous // ERR_SOCKET_BAD_PORT (or bind 0 / a hex-decoded port), and a bad