Skip to content

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.

fix(gateway): keep streamed Chat tool calls apart when they carry no index

Alex Castillo37d641021186
diff --git a/apps/daemon/src/gateway/chat-responses.ts b/apps/daemon/src/gateway/chat-responses.ts
index f768a1f1..17512875 100644
--- a/apps/daemon/src/gateway/chat-responses.ts
+++ b/apps/daemon/src/gateway/chat-responses.ts
@@ -84,6 +84,16 @@ function usage(value: Item | undefined) {
input_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 {
+ 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;
+}
+
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 });
@@ -134,8 +144,9 @@ export async function chatToResponses(response: Response, tools: Map<string, Too
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 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);
diff --git a/apps/daemon/test/unit/gateway-eighty-eight-adapters.test.ts b/apps/daemon/test/unit/gateway-eighty-eight-adapters.test.ts
index 22aa28bc..e64b9df6 100644
--- 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 @@ it('bridges a non-streaming model probe before submitting and replays the Respon
expect(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"}' }
+ ]);
+});