diff --git a/packages/zen-free/tool-response.mjs b/packages/zen-free/tool-response.mjs index b0dc678..5b984dc 100644 --- a/packages/zen-free/tool-response.mjs +++ b/packages/zen-free/tool-response.mjs @@ -250,6 +250,8 @@ function appendFunction(target, fragment) { if (!object(fragment)) throw new GuardError("Invalid upstream function delta"); for (const [key, value] of Object.entries(fragment)) { if (key === "name" || key === "arguments") { + // Nullable stream fields carry no new bytes; keep prior fragments intact. + if (value == null) continue; if (typeof value !== "string") throw new GuardError("Invalid upstream function delta"); target[key] = (target[key] ?? "") + value; } else { diff --git a/packages/zen-free/tool-response.test.mjs b/packages/zen-free/tool-response.test.mjs index a1aa6c1..cefe293 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 ); }); +for (const legacy of [false, true]) { + const kind = legacy ? "function_call" : "tool_calls"; + const delta = (fragment) => legacy + ? { function_call: fragment } + : { tool_calls: [{ index: 0, id: "call-0", type: "function", function: fragment }] }; + const functions = (values) => values + .flatMap((value) => value.choices ?? []) + .flatMap((choice) => legacy + ? (choice.delta?.function_call ? [choice.delta.function_call] : []) + : (choice.delta?.tool_calls ?? []).map((value) => value.function)); + + test(`chat: ${kind} ignores null fields while preserving name and argument fragments`, async () => { + const fragments = [ + { name: null, arguments: null }, + { name: "Re", arguments: null }, + { name: null, arguments: '{"path":' }, + { name: "ad", arguments: null }, + { name: null, arguments: '"example.txt"}' }, + { name: null, arguments: null }, + ]; + const result = await guardToolResponse( + sse(fragments.map((fragment) => event(chat(delta(fragment)))).join("") + + event(chat({}, kind)) + terminal("chat"), { chunkSize: 1 }), + "chat", + policy, + ); + const values = parse(await result.text()).map((frame) => frame.data); + assert.ok(!values.some((value) => value.error)); + assert.deepEqual(functions(values), [{ name: "Read", arguments: '{"path":"example.txt"}' }]); + }); + + test(`chat: ${kind} still drops calls whose name remains null`, async () => { + const result = await guardToolResponse( + sse(event(chat(delta({ name: null, arguments: "{}" }), kind)) + terminal("chat")), + "chat", + policy, + ); + const values = parse(await result.text()).map((frame) => frame.data); + assert.ok(!values.some((value) => value.error)); + assert.deepEqual(functions(values), []); + assert.equal(textIn("chat", values), droppedNote("")); + }); + + for (const field of ["name", "arguments"]) { + for (const invalid of [42, false, [], {}]) { + test(`chat: ${kind} rejects ${field} delta ${JSON.stringify(invalid)}`, async () => { + const result = await guardToolResponse( + sse(event(chat(delta({ name: "Read", arguments: "" }))) + + event(chat(delta({ [field]: invalid }), kind)) + terminal("chat")), + "chat", + policy, + ); + const values = parse(await result.text()).map((frame) => frame.data); + assert.deepEqual(functions(values), []); + assertError("chat", values[0]); + assert.equal(values[0].error.message, "Invalid upstream function delta"); + }); + } + } +} + test("chat: legacy fragments flush at DONE, but truncated pending calls never escape", async () => { const prefix = event(chat({ function_call: { arguments: "{}" } })) +