Skip to content

Commit 37c7de0

Browse files
committed
refactor(ai): remove redundant tool argument finalization
1 parent 27f2a45 commit 37c7de0

6 files changed

Lines changed: 3 additions & 33 deletions

File tree

‎extensions/amazon-bedrock/stream.runtime.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,6 @@ function handleContentBlockStop(
596596
});
597597
break;
598598
case "toolCall":
599-
block.arguments = parseStreamingJson(block.partialJson);
600599
// Finalize in-place and strip the scratch buffer so replay only
601600
// carries parsed arguments.
602601
delete (block as Block).partialJson;

‎packages/ai/src/providers/mistral.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,6 @@ async function consumeChatStream(
771771
continue;
772772
}
773773
const toolBlock = block as ToolCall & { partialArgs?: string };
774-
toolBlock.arguments = parseStreamingJson(toolBlock.partialArgs);
775774
// Finalize in-place and strip the scratch buffer so replay only
776775
// carries parsed arguments.
777776
delete toolBlock.partialArgs;

‎packages/ai/src/providers/openai-completions.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ export const streamOpenAICompletions: StreamFunction<
244244
partial: output,
245245
});
246246
} else if (block.type === "toolCall") {
247-
block.arguments = parseStreamingJson(block.partialArgs);
248247
// Finalize in-place and strip the scratch buffers so replay only
249248
// carries parsed arguments.
250249
delete block.partialArgs;

‎packages/ai/src/utils/json-parse.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ export function repairJson(json: string): string {
105105
}
106106

107107
export function parseJsonWithRepair(json: string): unknown {
108-
const repairedJson = repairJson(json);
109-
if (repairedJson !== json) {
110-
return JSON.parse(repairedJson) as unknown;
111-
}
112-
return JSON.parse(json) as unknown;
108+
return JSON.parse(repairJson(json)) as unknown;
113109
}
114110

115111
function looksLikeWindowsPathPrefix(prefix: string): boolean {
@@ -139,12 +135,10 @@ export function parseStreamingJson(partialJson: string | undefined): Record<stri
139135
return asStreamingJsonRecord(parseJsonWithRepair(partialJson));
140136
} catch {
141137
try {
142-
const result = partialParse(partialJson);
143-
return asStreamingJsonRecord(result);
138+
return asStreamingJsonRecord(partialParse(partialJson));
144139
} catch {
145140
try {
146-
const result = partialParse(repairJson(partialJson));
147-
return asStreamingJsonRecord(result);
141+
return asStreamingJsonRecord(partialParse(repairJson(partialJson)));
148142
} catch {
149143
return {};
150144
}

‎src/agents/anthropic-transport-stream.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,9 +1750,6 @@ export function createAnthropicMessagesTransportStreamFn(): StreamFn {
17501750
continue;
17511751
}
17521752
if (block.type === "toolCall") {
1753-
if (typeof block.partialJson === "string" && block.partialJson.length > 0) {
1754-
block.arguments = parseAnthropicToolCallArguments(block.partialJson);
1755-
}
17561753
delete block.partialJson;
17571754
eventSink.push({
17581755
type: "toolcall_end",

‎src/agents/openai-completions-transport.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,6 @@ async function processOpenAICompletionsStream(
398398
chunkPushedEvent = true;
399399
stream.push(event);
400400
};
401-
const finishCurrentBlock = () => {
402-
if (!currentBlock) {
403-
return;
404-
}
405-
if (currentBlock.type === "toolCall") {
406-
currentBlock.arguments = parseStreamingJson(currentBlock.partialArgs);
407-
}
408-
};
409-
const finishAllToolCallBlocks = () => {
410-
for (const block of toolCallBlocksByIndex.values()) {
411-
block.arguments = parseStreamingJson(block.partialArgs);
412-
}
413-
};
414401
const queuePostToolCallDelta = (next: CompletionsReasoningDelta) => {
415402
const nextBytes = measureUtf8Bytes(next.text);
416403
if (pendingPostToolCallBytes + nextBytes > MAX_POST_TOOL_CALL_BUFFER_BYTES) {
@@ -434,7 +421,6 @@ async function processOpenAICompletionsStream(
434421
};
435422
const appendThinkingDeltaInternal = (reasoningDelta: { signature: string; text: string }) => {
436423
if (!currentBlock || currentBlock.type !== "thinking") {
437-
finishCurrentBlock();
438424
currentBlock = {
439425
type: "thinking",
440426
thinking: "",
@@ -453,7 +439,6 @@ async function processOpenAICompletionsStream(
453439
};
454440
const appendTextDeltaInternal = (text: string) => {
455441
if (!currentBlock || currentBlock.type !== "text") {
456-
finishCurrentBlock();
457442
currentBlock = { type: "text", text: "" };
458443
output.content.push(currentBlock);
459444
pushStreamEvent({ type: "text_start", contentIndex: blockIndex(), partial: output });
@@ -506,7 +491,6 @@ async function processOpenAICompletionsStream(
506491
};
507492
const appendRecoveredToolCall = (toolCall: RecoveredDeepSeekDsmlToolCall) => {
508493
const switchingToolCall = currentBlock?.type === "toolCall";
509-
finishCurrentBlock();
510494
if (switchingToolCall) {
511495
currentBlock = null;
512496
flushPendingPostToolCallDeltas();
@@ -731,7 +715,6 @@ async function processOpenAICompletionsStream(
731715
}
732716
if (!block) {
733717
const switchingToolCall = currentBlock?.type === "toolCall";
734-
finishCurrentBlock();
735718
if (switchingToolCall) {
736719
currentBlock = null;
737720
flushPendingPostToolCallDeltas();
@@ -793,7 +776,6 @@ async function processOpenAICompletionsStream(
793776
flushReasoningTagTextPartitionerAtEnd();
794777
flushDeepSeekToolCallRecovererAtEnd();
795778
flushDeepSeekTextFilterAtEnd();
796-
finishAllToolCallBlocks();
797779
currentBlock = null;
798780
flushPendingPostToolCallDeltas();
799781
const hasToolCalls = output.content.some((block) => block.type === "toolCall");

0 commit comments

Comments
 (0)