Skip to content

Commit dba4454

Browse files
committed
refactor: implement yielding to avoid block the main thread
1 parent c780c77 commit dba4454

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
@@ -1437,26 +1437,10 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
14371437
}
14381438
}
14391439

1440-
/**
1441-
* Iteratively processes Cline requests using a stack-based approach to avoid recursion.
1442-
* This method handles the main request-response loop with the AI assistant, processing
1443-
* user content, making API calls, and handling tool usage responses.
1444-
*
1445-
* @param userContent - The content blocks to send to the AI assistant
1446-
* @param includeFileDetails - Whether to include detailed file information in the first request
1447-
* @returns Promise<boolean> - Returns true if the loop should end, false to continue
1448-
*
1449-
* @remarks
1450-
* This method was converted from a recursive implementation to an iterative one using
1451-
* a stack data structure to eliminate potential stack overflow issues and improve
1452-
* performance while maintaining exact same functionality and behavior.
1453-
*/
14541440
public async recursivelyMakeClineRequests(
14551441
userContent: Anthropic.Messages.ContentBlockParam[],
14561442
includeFileDetails: boolean = false,
14571443
): Promise<boolean> {
1458-
// Use a stack to manage the iterative processing to eliminate recursion
1459-
// Each stack item contains the user content and whether to include file details
14601444
interface StackItem {
14611445
userContent: Anthropic.Messages.ContentBlockParam[]
14621446
includeFileDetails: boolean
@@ -2049,13 +2033,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
20492033
this.consecutiveMistakeCount++
20502034
}
20512035

2052-
// ITERATIVE CHANGE: Instead of recursive call, push to stack for next iteration
2053-
// This replaces: const recDidEndLoop = await this.recursivelyMakeClineRequests(this.userMessageContent)
20542036
if (this.userMessageContent.length > 0) {
20552037
stack.push({
20562038
userContent: [...this.userMessageContent], // Create a copy to avoid mutation issues
20572039
includeFileDetails: false, // Subsequent iterations don't need file details
20582040
})
2041+
2042+
// Add periodic yielding to prevent blocking
2043+
await new Promise((resolve) => setImmediate(resolve))
20592044
}
20602045
// Continue to next iteration instead of setting didEndLoop from recursive call
20612046
continue

0 commit comments

Comments
 (0)