Skip to content

Commit 88c8930

Browse files
committed
fix: address PR review comments for streaming tool calls
- Add clearAllStreamingToolCalls() to prevent memory leak when streams are interrupted - Add hasActiveStreamingToolCalls() for debugging/testing - Add comment clarifying the intentional difference between partial and complete validation logic for insert_content (partial uses OR to show progress incrementally) - Call clearAllStreamingToolCalls() in Task.ts when resetting streaming state
1 parent 67d8b8d commit 88c8930

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/core/assistant-message/NativeToolCallParser.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ export class NativeToolCallParser {
4040
})
4141
}
4242

43+
/**
44+
* Clear all streaming tool call state.
45+
* Should be called when a new API request starts to prevent memory leaks
46+
* from interrupted streams.
47+
*/
48+
public static clearAllStreamingToolCalls(): void {
49+
this.streamingToolCalls.clear()
50+
}
51+
52+
/**
53+
* Check if there are any active streaming tool calls.
54+
* Useful for debugging and testing.
55+
*/
56+
public static hasActiveStreamingToolCalls(): boolean {
57+
return this.streamingToolCalls.size > 0
58+
}
59+
4360
/**
4461
* Process a chunk of JSON arguments for a streaming tool call.
4562
* Uses partial-json-parser to extract values from incomplete JSON immediately.
@@ -145,6 +162,9 @@ export class NativeToolCallParser {
145162
break
146163

147164
case "insert_content":
165+
// For partial tool calls, we build nativeArgs incrementally as fields arrive.
166+
// Unlike parseToolCall which validates all required fields, partial parsing
167+
// needs to show progress as each field streams in.
148168
if (
149169
partialArgs.path !== undefined ||
150170
partialArgs.line !== undefined ||
@@ -155,7 +175,7 @@ export class NativeToolCallParser {
155175
line:
156176
typeof partialArgs.line === "number"
157177
? partialArgs.line
158-
: partialArgs.line
178+
: partialArgs.line !== undefined
159179
? parseInt(String(partialArgs.line), 10)
160180
: undefined,
161181
content: partialArgs.content,

src/core/task/Task.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
22532253
this.presentAssistantMessageHasPendingUpdates = false
22542254
this.assistantMessageParser?.reset()
22552255
this.streamingToolCallIndices.clear()
2256+
// Clear any leftover streaming tool call state from previous interrupted streams
2257+
NativeToolCallParser.clearAllStreamingToolCalls()
22562258

22572259
await this.diffViewProvider.reset()
22582260

0 commit comments

Comments
 (0)