Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
Merge PR #361 from fix/gateway-unindexed-tool-calls
Leggett565deecbceda
diff --cc apps/daemon/src/gateway/chat-responses.tsindex f768a1f1,17512875..d76e0b63--- a/apps/daemon/src/gateway/chat-responses.ts+++ b/apps/daemon/src/gateway/chat-responses.ts@@@ -84,6 -84,16 +84,16 @@@ function usage(value: Item | undefinedinput_tokens_details: { cached_tokens: value?.prompt_tokens_details?.cached_tokens ?? 0 }, output_tokens_details: { reasoning_tokens: value?.completion_tokens_details?.reasoning_tokens ?? 0 } };}-// Gemini's OpenAI-compatible endpoint streams every parallel tool call whole and without an index. A delta-// without one starts a new call when it brings an id other than the latest call's, or a name without an id;-// anything else (a repeated id, a bare argument fragment) continues the latest call.-function unindexedCallSlot(calls: Map<number, Item>, call: Item): number {++// Unindexed fragments with an ID resume that call even when parallel calls interleave.++// A new ID or a name without an ID starts a call; bare arguments resume the last updated call.++function unindexedCallSlot(calls: Map<number, Item>, call: Item, lastIndex: number): number {++ if (call.id) {++ for (const [index, current] of calls) if (current.id === call.id) return index;++ }+ if (!calls.size) return 0;- const latest = Math.max(...calls.keys());- const startsNew = call.id ? call.id !== calls.get(latest)!.id : Boolean(call.function?.name);- return startsNew ? latest + 1 : latest;++ return call.id || call.function?.name ? Math.max(...calls.keys()) + 1 : lastIndex;+ }+export async function chatToResponses(response: Response, tools: Map<string, Tool>, model: unknown): Promise<Response> {const id = `resp_${randomUUID()}`; const created = Math.floor(Date.now() / 1000);const envelope = (output: Item[], status = 'completed', tokenUsage?: Item) => ({ id, object: 'response', created_at: created, status, model, output, usage: usage(tokenUsage), error: null });@@@ -100,6 -110,6 +110,7 @@@if (!response.body) throw new GatewayError('invalid_gateway_response');const reader = response.body.getReader(); const decoder = new TextDecoder(); const encoder = new TextEncoder();const output: Item[] = []; const calls = new Map<number, Item>();++ let lastCallIndex = 0;let buffer = ''; let text = ''; let message: Item | undefined; let tokenUsage: Item | undefined; let finish: string | undefined; let sequence = 0; let receivedBytes = 0;const event = (type: string, fields: Item) => encoder.encode(`event: ${type}\ndata: ${JSON.stringify({ type, sequence_number: sequence++, ...fields })}\n\n`);async function* events() {@@@ -134,11 -144,12 +145,13 @@@yield event('response.output_text.delta', { item_id: message.id, output_index: 0, content_index: 0, delta: delta.content });}for (const call of delta.tool_calls ?? []) {- const index = call.index ?? 0; const current = calls.get(index) ?? { id: '', function: { name: '', arguments: '' } };- if (call.id) current.id += call.id;- const index = typeof call.index === 'number' ? call.index : unindexedCallSlot(calls, call);++ const index = typeof call.index === 'number' ? call.index : unindexedCallSlot(calls, call, lastCallIndex);+ const current = calls.get(index) ?? { id: '', function: { name: '', arguments: '' } };+ if (call.id && call.id !== current.id) current.id += call.id;if (call.function?.name) current.function.name += call.function.name;if (call.function?.arguments) current.function.arguments += call.function.arguments;calls.set(index, current);++ lastCallIndex = index;}if (choice.finish_reason) finish = choice.finish_reason;}diff --cc apps/daemon/test/integration/gateway-runtime-protocol.test.tsindex 40a5a0ee,40a5a0ee..1dd79c74--- a/apps/daemon/test/integration/gateway-runtime-protocol.test.ts+++ b/apps/daemon/test/integration/gateway-runtime-protocol.test.ts@@@ -211,6 -211,6 +211,44 @@@ describe('bundled Runtime official gateexpect(f.requests.every(request => request.headers.authorization === 'Bearer test-official-key-a')).toBe(true);if (toolType === 'custom') expect(readFileSync(join(f.root, 'chat-bridge.txt'), 'utf8')).toBe('bridge ready\n');}, 30_000);++ it.each(['function', 'custom'])('executes interleaved unindexed %s calls through the Chat bridge', async toolType => {++ const f = await fixture((res, requests) => {++ res.writeHead(200, { 'Content-Type': 'text/event-stream' });++ const name = toolType === 'custom' ? 'apply_patch' : 'exec_command';++ const args = ['a', 'b'].map(suffix => toolType === 'custom'++ ? JSON.stringify({ input: `*** Begin Patch\n*** Add File: ${join(f.root, `interleaved-${suffix}.txt`)}\n+bridge ${suffix}\n*** End Patch` })++ : JSON.stringify({ cmd: 'pwd', max_output_tokens: 100 }));++ const split = Math.floor(args[0]!.length / 2);++ const calls = [++ { id: 'call_a', function: { name, arguments: args[0]!.slice(0, split) } },++ { id: 'call_b', function: { name, arguments: args[1]!.slice(0, split) } },++ { id: 'call_a', function: { arguments: args[0]!.slice(split, split + 1) } },++ { function: { arguments: args[0]!.slice(split + 1) } },++ { id: 'call_b', function: { arguments: args[1]!.slice(split) } }++ ];++ const choices = requests.length === 1++ ? [...calls.map(call => ({ delta: { tool_calls: [call] } })), { delta: {}, finish_reason: 'tool_calls' }]++ : [{ delta: { content: 'gateway ready' }, finish_reason: 'stop' }];++ res.end(choices.map(choice => `data: ${JSON.stringify({ choices: [choice] })}\n\n`).join('') + 'data: [DONE]\n\n');++ }, true, 'https://api.88api.ai/v1', true);++ expect((await f.run().result).turnStatus).toBe('completed');++ expect(f.requests).toHaveLength(2);++ const messages = f.requests[1]!.body.messages as Array<{++ role: string; tool_call_id?: string; content?: string;++ tool_calls?: Array<{ id: string; function: { name: string; arguments: string } }>;++ }>;++ const calls = messages.flatMap(message => message.tool_calls ?? []);++ expect(calls.map(call => ({ id: call.id, name: call.function.name }))).toEqual([++ { id: 'call_a', name: toolType === 'custom' ? 'apply_patch' : 'exec_command' },++ { id: 'call_b', name: toolType === 'custom' ? 'apply_patch' : 'exec_command' }++ ]);++ expect(calls.every(call => typeof JSON.parse(call.function.arguments) === 'object')).toBe(true);++ const results = messages.filter(message => message.role === 'tool');++ expect(results.map(result => result.tool_call_id).sort()).toEqual(['call_a', 'call_b']);++ if (toolType === 'custom') {++ for (const suffix of ['a', 'b']) expect(readFileSync(join(f.root, `interleaved-${suffix}.txt`), 'utf8')).toBe(`bridge ${suffix}\n`);++ } else expect(results.every(result => result.content?.includes(f.root))).toBe(true);++ }, 30_000);it('uses only the official key for Responses streaming and leaves no auth.json', async () => {const f = await fixture(res => complete(res, [message]));const result = await f.run().result;diff --cc apps/daemon/test/unit/gateway-eighty-eight-adapters.test.tsindex 22aa28bc,e64b9df6..d496ae35--- a/apps/daemon/test/unit/gateway-eighty-eight-adapters.test.ts+++ b/apps/daemon/test/unit/gateway-eighty-eight-adapters.test.ts@@@ -174,3 -174,36 +174,54 @@@ it('bridges a non-streaming model probexpect(fetcher).toHaveBeenCalledOnce(); expect(fetcher.mock.lastCall?.[0]).toBe(`${baseUrl}/chat/completions`);expect(success).toHaveBeenCalledWith(model.id, 'text', 'responses');});++ async function chatToolCalls(frames: unknown[]) {+ const bridge = responsesToChat({ model: 'gemini-3.7-flash', tools: [{ type: 'function', name: 'get_weather', parameters: { type: 'object', properties: { city: { type: 'string' } } } }],+ input: [{ role: 'user', content: [{ type: 'input_text', text: 'weather in Paris and London' }] }], stream: true });+ const body = frames.map(p => `data: ${JSON.stringify(p)}\n\n`).join('') + 'data: [DONE]\n\n';+ const response = await chatToResponses(new Response(body, { headers: { 'content-type': 'text/event-stream' } }), bridge.tools, 'gemini-3.7-flash');+ const events = (await response.text()).split('\n\n').filter(Boolean).map(frame => JSON.parse(frame.split('\n').find(l => l.startsWith('data:'))!.slice(5)));+ return events.at(-1).response.output.map((item: { call_id: string; name: string; arguments: string }) => ({ id: item.call_id, name: item.name, arguments: item.arguments }));+ }++ it('keeps parallel Chat tool calls apart when the stream carries no index', async () => {+ // Gemini's OpenAI-compatible endpoint sends every parallel call whole, in its own chunk, without an index.+ expect(await chatToolCalls([+ { choices: [{ delta: { tool_calls: [{ id: 'call_paris', type: 'function', function: { name: 'get_weather', arguments: '{"city":"Paris"}' } }] } }] },+ { choices: [{ delta: { tool_calls: [{ id: 'call_london', type: 'function', function: { name: 'get_weather', arguments: '{"city":"London"}' } }] } }] },+ { choices: [{ delta: {}, finish_reason: 'tool_calls' }] }+ ])).toEqual([+ { id: 'call_paris', name: 'get_weather', arguments: '{"city":"Paris"}' },+ { id: 'call_london', name: 'get_weather', arguments: '{"city":"London"}' }+ ]);+ });++ it('continues the last Chat tool call for argument fragments without an index', async () => {+ expect(await chatToolCalls([+ { choices: [{ delta: { tool_calls: [{ id: 'call_paris', type: 'function', function: { name: 'get_weather', arguments: '{"city":' } }] } }] },+ { choices: [{ delta: { tool_calls: [{ id: 'call_paris', function: { arguments: '"Paris"}' } }] } }] },+ { choices: [{ delta: { tool_calls: [{ id: 'call_london', type: 'function', function: { name: 'get_weather', arguments: '{"ci' } }] } }] },+ { choices: [{ delta: { tool_calls: [{ function: { arguments: 'ty":"London"}' } }] }, finish_reason: 'tool_calls' }] }+ ])).toEqual([+ { id: 'call_paris', name: 'get_weather', arguments: '{"city":"Paris"}' },+ { id: 'call_london', name: 'get_weather', arguments: '{"city":"London"}' }+ ]);+ });++++it.each([false, true])('reassembles interleaved Chat tool calls with explicit indices: %s', async indexed => {++ const calls = [++ { index: 0, id: 'call_paris', function: { name: 'get_weather', arguments: '{"ci' } },++ { index: 1, id: 'call_london', function: { name: 'get_weather', arguments: '{"ci' } },++ { index: 0, id: 'call_paris', function: { arguments: 'ty":' } },++ { index: 0, function: { arguments: '"Paris"}' } },++ { index: 1, id: 'call_london', function: { arguments: 'ty":"London"}' } }++ ];++ const frames = calls.map(({ index, ...call }) => ({ choices: [{ delta: { tool_calls: [indexed ? { ...call, index } : call] } }] }));++ expect(await chatToolCalls([++ ...frames,++ { choices: [{ delta: {}, finish_reason: 'tool_calls' }] }++ ])).toEqual([++ { id: 'call_paris', name: 'get_weather', arguments: '{"city":"Paris"}' },++ { id: 'call_london', name: 'get_weather', arguments: '{"city":"London"}' }++ ]);++});