diff --git a/bin/antigravity-direct.mjs b/bin/antigravity-direct.mjs index 7e00708baf..705a550f3a 100755 --- a/bin/antigravity-direct.mjs +++ b/bin/antigravity-direct.mjs @@ -519,6 +519,7 @@ const toolChoiceConfig = (toolChoice, declaredNames) => { const buildV1Request = (body) => { const { base, fullName, effort } = resolveModelAndEffort(body.model, body.reasoning_effort); + const anthropic = base.startsWith('claude'); const messages = Array.isArray(body.messages) ? body.messages : []; const systemParts = []; const contents = []; @@ -559,7 +560,9 @@ const buildV1Request = (body) => { // may be sent (same code#2825 shape rule as functionResponse). if (parsedArgs && typeof parsedArgs === 'object' && !Array.isArray(parsedArgs)) args = parsedArgs; } catch {} - const part = { functionCall: { name: tc.function && tc.function.name, args } }; + const call = { name: tc.function && tc.function.name, args }; + if (anthropic && tc.id) call.id = tc.id; + const part = { functionCall: call }; const sig = lookupSignature(tc.id); if (sig) part.thoughtSignature = sig; parts.push(part); @@ -579,7 +582,11 @@ const buildV1Request = (body) => { const parsed = JSON.parse(result); response = (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) ? parsed : { result: parsed }; } catch { response = { result }; } - contents.push({ role: 'user', parts: [{ functionResponse: { name, response } }] }); + const fr = { name, response }; + // Same interop as the functionCall id above: for Claude targets the + // tool_result block must name its tool_use (opencode#1550). + if (anthropic && msg.tool_call_id) fr.id = msg.tool_call_id; + contents.push({ role: 'user', parts: [{ functionResponse: fr }] }); } } const request = { contents }; @@ -591,7 +598,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: {} }, base.startsWith('claude')) }); + decls.push({ name: fn.name, description: fn.description || '', parameters: convertSchema(fn.parameters || { type: 'object', properties: {} }, anthropic) }); declaredNames.push(fn.name); } } @@ -751,7 +758,7 @@ const consumeSse = async (stream, onEvent) => { onEvent({ [part.thought ? 'thought' : 'text']: part.text }); } if (part.functionCall) { - const call = { id: 'call_' + crypto.randomUUID().replace(/-/g, ''), name: part.functionCall.name, args: part.functionCall.args || {} }; + const call = { id: (typeof part.functionCall.id === 'string' && part.functionCall.id) || 'call_' + crypto.randomUUID().replace(/-/g, ''), name: part.functionCall.name, args: part.functionCall.args || {} }; if (part.thoughtSignature) call.signature = part.thoughtSignature; emittedCalls.push(call); onEvent({ toolCall: call }); diff --git a/bin/antigravity-direct.test.mjs b/bin/antigravity-direct.test.mjs index d0d12832cd..757dbf244b 100644 --- a/bin/antigravity-direct.test.mjs +++ b/bin/antigravity-direct.test.mjs @@ -280,6 +280,55 @@ test('anyOf tool schemas stay anyOf for gemini models (opencode#1550)', async () assert.deepEqual(props.pick, { oneOf: [{ type: 'STRING' }, { type: 'NUMBER' }] }); }); +// --- opencode#1550 (part 2): tool-call ids ride the history parts -------- +// For Claude models the upstream's v1internal->Anthropic conversion emits +// a tool_use block per history functionCall part and a tool_result per +// functionResponse part, and Anthropic requires both ids +// ("messages.N.content.M.tool_use.id: Field required", then +// "tool_result.tool_use_id: Field required") -- which 400'd every Claude +// session continuation carrying a tool call. Conversion for Claude +// targets must carry the client's tool_call ids on both part kinds; +// Gemini targets keep the historic id-less parts. + +const HISTORY = [ + { role: 'user', content: 'list the files' }, + { role: 'assistant', content: '', tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'get_weather', arguments: '{"city":"Oslo"}' } }] }, + { role: 'tool', tool_call_id: 'call_1', name: 'get_weather', content: '{"temp": 3}' }, + { role: 'user', content: 'thanks' } +]; + +test('history tool calls carry their ids for claude models (opencode#1550)', async () => { + const res = await fetch('http://127.0.0.1:' + proxyPort + '/v1/chat/completions', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ model: 'claude-sonnet-5-5', messages: HISTORY, tools: TOOLS }) + }); + assert.equal(res.status, 200); + await res.json(); + const calls = generateCalls(); + const contents = calls[calls.length - 1].body.request.contents; + const callPart = contents.flatMap(c => c.parts).find(p => p.functionCall); + const respPart = contents.flatMap(c => c.parts).find(p => p.functionResponse); + assert.equal(callPart.functionCall.id, 'call_1'); + assert.equal(respPart.functionResponse.id, 'call_1'); +}); + +test('history tool calls stay id-less for gemini models (opencode#1550)', async () => { + const res = await fetch('http://127.0.0.1:' + proxyPort + '/v1/chat/completions', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ model: 'gemini-3.8-flash', messages: HISTORY, tools: TOOLS }) + }); + assert.equal(res.status, 200); + await res.json(); + const calls = generateCalls(); + const contents = calls[calls.length - 1].body.request.contents; + const callPart = contents.flatMap(c => c.parts).find(p => p.functionCall); + const respPart = contents.flatMap(c => c.parts).find(p => p.functionResponse); + assert.ok(!('id' in callPart.functionCall), 'gemini functionCall must not carry id'); + assert.ok(!('id' in respPart.functionResponse), 'gemini functionResponse must not carry id'); +}); + // --- #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