Skip to content

Commit 76386fe

Browse files
Keep large streamed tool arguments responsive (#2726)
Throttle partial JSON parsing for large arguments while preserving final parsing and ordinary partial previews. Refs #2725
1 parent 993c967 commit 76386fe

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/core/assistant-message/NativeToolCallParser.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class NativeToolCallParser {
5656
// Note: name is string to accommodate dynamic MCP tools (mcp--serverName--toolName)
5757
private static streamingToolCallsByScope = new WeakMap<
5858
object,
59-
Map<string, { id: string; name: string; argumentsAccumulator: string }>
59+
Map<string, { id: string; name: string; argumentsAccumulator: string; nextPartialParseLength: number }>
6060
>()
6161

6262
/**
@@ -259,6 +259,7 @@ export class NativeToolCallParser {
259259
id,
260260
name,
261261
argumentsAccumulator: "",
262+
nextPartialParseLength: 64 * 1024,
262263
})
263264
}
264265

@@ -328,6 +329,17 @@ export class NativeToolCallParser {
328329
return null
329330
}
330331

332+
// Re-parsing the entire JSON value for every streamed fragment becomes
333+
// quadratic for large write_to_file arguments. Keep small tool calls fully
334+
// responsive, then update the preview at exponentially spaced sizes.
335+
const length = toolCall.argumentsAccumulator.length
336+
if (length > 32 * 1024 && length < toolCall.nextPartialParseLength) {
337+
return null
338+
}
339+
if (length >= toolCall.nextPartialParseLength) {
340+
toolCall.nextPartialParseLength = length * 2
341+
}
342+
331343
// Parse whatever we can from the incomplete JSON!
332344
// partial-json-parser extracts partial values (strings, arrays, objects) immediately
333345
try {

‎src/core/assistant-message/__tests__/NativeToolCallParser.spec.ts‎

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)