Fix system-message tools parser when tool call is non-terminal (#11092)

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>
Co-authored-by: MumuTW <mumu@novel98.com>
This commit is contained in:
MumuTW 2026-03-25 03:56:43 +09:00 • committed by GitHub
parent cbbe3ef11a
commit ffb6ea45f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 101 additions and 12 deletions

View file

@ -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 [
{

View file

@ -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 () => {