From ec884d09dcfabb419a1e427b20da960a038354ba Mon Sep 17 00:00:00 2001 From: zhengguangzhuo <100548634+zhengguangzhuo@users.noreply.github.com> Date: Tue, 20 Oct 2026 15:56:03 +0800 Subject: [PATCH] zen-free: a later null or empty tool-call id keeps the id already streamed (#188, #259) When a Chat Completions stream sends a call's id first and then id: null or id: "" in a later delta for the same index, the buffered call kept the later value, so the client got null/"" ids and a later request failed with "Duplicate value for 'tool_call_id'" (#188). A later null or empty id now keeps the id already buffered; an id that first arrives in a later delta is still taken. Run at the reviewed head 8f19ba1e6bf09f105a47e4fb2f7233de22a99b06 (base 5b68df0, main): - packages/zen-free: bun test (bun 1.4.2, temp HOME): 195 pass, 0 fail. - bun scripts/check.mjs: passes. - #188's literal repro script (node 22.22.0): prints call_0/call_1 for both null and "" and exits 0; on main's tool-response.mjs it prints [null,null] and ["",""] and fails its assert. - The PR's new test with main's tool-response.mjs: red (null, '' instead of call_0, call_1); green with the fix. --- packages/zen-free/tool-response.mjs | 8 ++++ packages/zen-free/tool-response.test.mjs | 61 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/packages/zen-free/tool-response.mjs b/packages/zen-free/tool-response.mjs index 5b984dc..6d08377 100644 --- a/packages/zen-free/tool-response.mjs +++ b/packages/zen-free/tool-response.mjs @@ -345,6 +345,14 @@ function chatBuffer(checkName) { const index = position(fragment.index); const previous = state.calls.get(index) ?? { index, function: {} }; const value = { ...previous, ...fragment, index, function: previous.function }; + // 空 ID 片段不应抹掉已缓存的有效 ID,同时允许首次 ID 后续到达。 + if ( + typeof previous.id === "string" && + previous.id.length > 0 && + (fragment.id == null || fragment.id === "") + ) { + value.id = previous.id; + } if (fragment.function != null) appendFunction(value.function, fragment.function); state.calls.set(index, value); } diff --git a/packages/zen-free/tool-response.test.mjs b/packages/zen-free/tool-response.test.mjs index cefe293..6db3097 100644 --- a/packages/zen-free/tool-response.test.mjs +++ b/packages/zen-free/tool-response.test.mjs @@ -994,6 +994,67 @@ test("chat: buffers names and arguments by choice/index, including arguments bef ); }); +test("chat: later null or empty tool IDs do not replace buffered IDs", async () => { + const text = [ + event( + chat( + { + tool_calls: [ + { index: 0, id: "call_0", type: "function", function: { name: "read", arguments: "{}" } }, + { index: 1, id: "call_1", type: "function", function: { name: "read", arguments: "{}" } }, + ], + }, + null, + 0, + ), + ), + event( + chat( + { tool_calls: [{ index: 0, id: null }, { index: 1, id: "" }] }, + null, + 0, + ), + ), + event(chat({ tool_calls: [{ index: 2, function: { name: "read", arguments: "{}" } }] }, null, 0)), + event(chat({ tool_calls: [{ index: 2, id: "call_late" }] }, null, 0)), + event( + chat( + { + tool_calls: [ + { + index: 0, + id: "choice_1_call", + type: "function", + function: { name: "read", arguments: "{}" }, + }, + ], + }, + null, + 1, + ), + ), + event(chat({ tool_calls: [{ index: 0, id: null }] }, null, 1)), + event(chat({}, "tool_calls", 0)), + event(chat({}, "tool_calls", 1)), + terminal("chat"), + ].join(""); + const response = await guardToolResponse(sse(text, { chunkSize: 1 }), "chat", policy); + const values = frameData(await response.text()); + assert.ok(!values.some((value) => value.error)); + + const choices = values.flatMap((value) => value.choices ?? []); + const callsFor = (index) => + choices + .filter((choice) => choice.index === index) + .flatMap((choice) => choice.delta?.tool_calls ?? []); + assert.deepEqual( + callsFor(0).map((value) => value.id), + ["call_0", "call_1", "call_late"], + ); + assert.deepEqual(callsFor(1).map((value) => value.id), ["choice_1_call"]); + assert.deepEqual(callsFor(0).map((value) => value.function.name), ["Read", "Read", "Read"]); +}); + for (const legacy of [false, true]) { const kind = legacy ? "function_call" : "tool_calls"; const delta = (fragment) => legacy