Skip to content

Commit 658de61

Browse files
catrielmullerdaniel-lxs
authored andcommitted
refactor: implement yielding to avoid block the main thread
1 parent 8206c81 commit 658de61

File tree

1 file changed

+3
-18
lines changed

1 file changed

+3
-18
lines changed

src/core/task/Task.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,26 +1446,10 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
14461446
}
14471447
}
14481448

1449-
/**
1450-
* Iteratively processes Cline requests using a stack-based approach to avoid recursion.
1451-
* This method handles the main request-response loop with the AI assistant, processing
1452-
* user content, making API calls, and handling tool usage responses.
1453-
*
1454-
* @param userContent - The content blocks to send to the AI assistant
1455-
* @param includeFileDetails - Whether to include detailed file information in the first request
1456-
* @returns Promise<boolean> - Returns true if the loop should end, false to continue
1457-
*
1458-
* @remarks
1459-
* This method was converted from a recursive implementation to an iterative one using
1460-
* a stack data structure to eliminate potential stack overflow issues and improve
1461-
* performance while maintaining exact same functionality and behavior.
1462-
*/
14631449
public async recursivelyMakeClineRequests(
14641450
userContent: Anthropic.Messages.ContentBlockParam[],
14651451
includeFileDetails: boolean = false,
14661452
): Promise<boolean> {
1467-
// Use a stack to manage the iterative processing to eliminate recursion
1468-
// Each stack item contains the user content and whether to include file details
14691453
interface StackItem {
14701454
userContent: Anthropic.Messages.ContentBlockParam[]
14711455
includeFileDetails: boolean
@@ -2058,13 +2042,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
20582042
this.consecutiveMistakeCount++
20592043
}
20602044

2061-
// ITERATIVE CHANGE: Instead of recursive call, push to stack for next iteration
2062-
// This replaces: const recDidEndLoop = await this.recursivelyMakeClineRequests(this.userMessageContent)
20632045
if (this.userMessageContent.length > 0) {
20642046
stack.push({
20652047
userContent: [...this.userMessageContent], // Create a copy to avoid mutation issues
20662048
includeFileDetails: false, // Subsequent iterations don't need file details
20672049
})
2050+
2051+
// Add periodic yielding to prevent blocking
2052+
await new Promise((resolve) => setImmediate(resolve))
20682053
}
20692054
// Continue to next iteration instead of setting didEndLoop from recursive call
20702055
continue

0 commit comments

Comments
 (0)