From ffb6ea45f097c48fbef6261a1c0ee1ce3471e767 Mon Sep 17 00:00:00 2001 From: MumuTW <42820974+MumuTW@users.noreply.github.com> Date: Wed, 25 Mar 2026 03:56:43 +0900 Subject: [PATCH] Fix system-message tools parser when tool call is non-terminal (#11092) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(openai-adapters): map reasoning-delta to reasoning_content * Fix system-message tool parser for non-terminal tool calls * Fix duplicate predefined tools in dynamic system-message section * fix(openai-adapters): fix TS syntax error in vercelStreamConverter test The `as typeof X & Y` type assertion was ambiguous to the TypeScript parser without parentheses, causing TS1005 errors across all CI checks. Simplify to `as any` since this is a test assertion. Co-Authored-By: Claude Opus 4.6 * chore: remove unrelated vercelStreamConverter changes Per reviewer feedback — keeping this PR focused on the system-message tools parser fix. * fix: correct test expectations for post-tool-call content preservation The 'preserves content after a tool call' test incorrectly consumed the trailing newline and preserved text within the tool-call loop. Adjusted the loop count from 6 to 4 (actual tool-call deltas) and added explicit assertions for the trailing newline and preserved content chunks. The 'mid-message' test was missing an assertion for the newline chunk emitted between the closing codeblock fence and trailing text. --------- Co-authored-by: Claude Opus 4.6 Co-authored-by: MumuTW --- .../interceptSystemToolCalls.ts | 13 +-- .../interceptSystemToolCalls.vitest.ts | 100 ++++++++++++++++-- 2 files changed, 101 insertions(+), 12 deletions(-) diff --git a/core/tools/systemMessageTools/interceptSystemToolCalls.ts b/core/tools/systemMessageTools/interceptSystemToolCalls.ts index a89fbdcfb..bc5d8bee3 100644 --- a/core/tools/systemMessageTools/interceptSystemToolCalls.ts +++ b/core/tools/systemMessageTools/interceptSystemToolCalls.ts @@ -46,7 +46,7 @@ export async function* interceptSystemToolCalls( return result.value; } else { for await (const message of result.value) { - if (abortController.signal.aborted || parseState?.done) { + if (abortController.signal.aborted) { break; } // Skip non-assistant messages or messages with native tool calls @@ -96,12 +96,13 @@ export async function* interceptSystemToolCalls( }, ]; } - } else { - // Prevent content after tool calls for now - if (parseState) { - continue; + // Completed tool calls should not terminate parsing for subsequent + // chunks/messages; reset state so normal content (or another tool + // call) can be handled. + if (parseState.done) { + parseState = undefined; } - + } else { // Yield normal assistant message yield [ { diff --git a/core/tools/systemMessageTools/toolCodeblocks/interceptSystemToolCalls.vitest.ts b/core/tools/systemMessageTools/toolCodeblocks/interceptSystemToolCalls.vitest.ts index a636cc6a9..d86efca0f 100644 --- a/core/tools/systemMessageTools/toolCodeblocks/interceptSystemToolCalls.vitest.ts +++ b/core/tools/systemMessageTools/toolCodeblocks/interceptSystemToolCalls.vitest.ts @@ -242,7 +242,7 @@ describe("interceptSystemToolCalls", () => { ).toBe("}"); }); - it("ignores content after a tool call", async () => { + it("preserves content after a tool call", async () => { const messages: ChatMessage[][] = [ [{ role: "assistant", content: "```tool\n" }], [{ role: "assistant", content: "TOOL_NAME: test_tool\n" }], @@ -250,7 +250,7 @@ describe("interceptSystemToolCalls", () => { [{ role: "assistant", content: "value1\n" }], [{ role: "assistant", content: "END_ARG\n" }], [{ role: "assistant", content: "```\n" }], - [{ role: "assistant", content: "This content should be ignored" }], + [{ role: "assistant", content: "This content should be preserved" }], ]; const generator = interceptSystemToolCalls( @@ -260,14 +260,102 @@ describe("interceptSystemToolCalls", () => { ); let result; - // Process through all the tool call parts - for (let i = 0; i < 6; i++) { + // Process through all the tool call deltas (name, arg prefix, arg value, closing brace) + for (let i = 0; i < 4; i++) { result = await generator.next(); } - // The content after the tool call should be ignored + // The trailing newline from "```\n" is yielded as text after the tool call ends result = await generator.next(); - expect(result.value).toBeUndefined(); + expect(result.value).toEqual([ + { + role: "assistant", + content: [{ type: "text", text: "\n" }], + }, + ]); + + // The content after the tool call should be preserved + result = await generator.next(); + expect(result.value).toEqual([ + { + role: "assistant", + content: [{ type: "text", text: "This content should be preserved" }], + }, + ]); + }); + + it("parses a tool call that appears mid-message and preserves trailing content", async () => { + const messages: ChatMessage[][] = [ + [ + { + role: "assistant", + content: + "Before tool\n```tool\nTOOL_NAME: test_tool\nBEGIN_ARG: arg1\nvalue1\nEND_ARG\n```\nAfter tool", + }, + ], + ]; + + const generator = interceptSystemToolCalls( + createAsyncGenerator(messages), + abortController, + framework, + ); + + let result = await generator.next(); + expect(result.value).toEqual([ + { + role: "assistant", + content: [{ type: "text", text: "Before tool" }], + }, + ]); + + result = await generator.next(); + expect(result.value).toEqual([ + { + role: "assistant", + content: [{ type: "text", text: "\n" }], + }, + ]); + + result = await generator.next(); + expect( + (result.value as AssistantChatMessage[])[0].toolCalls?.[0].function?.name, + ).toBe("test_tool"); + + result = await generator.next(); + expect( + (result.value as AssistantChatMessage[])[0].toolCalls?.[0].function + ?.arguments, + ).toContain('{"arg1":'); + + result = await generator.next(); + expect( + (result.value as AssistantChatMessage[])[0].toolCalls?.[0].function + ?.arguments, + ).toBe('"value1"'); + + result = await generator.next(); + expect( + (result.value as AssistantChatMessage[])[0].toolCalls?.[0].function + ?.arguments, + ).toBe("}"); + + // The newline between the closing ``` and "After tool" is a separate chunk + result = await generator.next(); + expect(result.value).toEqual([ + { + role: "assistant", + content: [{ type: "text", text: "\n" }], + }, + ]); + + result = await generator.next(); + expect(result.value).toEqual([ + { + role: "assistant", + content: [{ type: "text", text: "After tool" }], + }, + ]); }); it("stops processing when aborted", async () => {