fix(ai): strip partialJson from responses tool calls

Mutate persisted tool-call blocks in place on function_call completion,
remove partialJson, and emit the same reference on toolcall_end.
Add regression coverage for persisted block cleanup and event identity.

fixes #3078
This commit is contained in:
Armin Ronacher
2026-04-14 18:37:06 +02:00
parent 64a0a74d37
commit e2b40dfc81
9 changed files with 155 additions and 11 deletions

View File

@@ -384,7 +384,9 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
});
} else if (block.type === "toolCall") {
block.arguments = parseStreamingJson(block.partialJson);
delete (block as any).partialJson;
// Finalize in-place and strip the scratch buffer so replay only
// carries parsed arguments.
delete (block as { partialJson?: string }).partialJson;
stream.push({
type: "toolcall_end",
contentIndex: index,
@@ -429,7 +431,11 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
stream.push({ type: "done", reason: output.stopReason, message: output });
stream.end();
} catch (error) {
for (const block of output.content) delete (block as any).index;
for (const block of output.content) {
delete (block as { index?: number }).index;
// partialJson is only a streaming scratch buffer; never persist it.
delete (block as { partialJson?: string }).partialJson;
}
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
stream.push({ type: "error", reason: output.stopReason, error: output });