diff options
| author | Brandon Keiji <[email protected]> | 2025-05-22 09:51:07 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-05-22 02:51:07 -0700 |
| commit | a8bfdf2d5603e9e2fb01e12d6c5499662dccaa85 (patch) | |
| tree | 5c191574d4d1ae3d2ebc7a75139ca47e7f96b915 /packages/cli/src/ui/hooks/useToolScheduler.ts | |
| parent | 174fdce7d8230a12f879874cf7121698d91e678c (diff) | |
fix: synchronization between executed tools and turn loops (#488)
Diffstat (limited to 'packages/cli/src/ui/hooks/useToolScheduler.ts')
| -rw-r--r-- | packages/cli/src/ui/hooks/useToolScheduler.ts | 70 |
1 files changed, 32 insertions, 38 deletions
diff --git a/packages/cli/src/ui/hooks/useToolScheduler.ts b/packages/cli/src/ui/hooks/useToolScheduler.ts index fde632df..e14241b6 100644 --- a/packages/cli/src/ui/hooks/useToolScheduler.ts +++ b/packages/cli/src/ui/hooks/useToolScheduler.ts @@ -184,61 +184,55 @@ export function useToolScheduler( useEffect(() => { // effect for executing scheduled tool calls - const scheduledCalls = toolCalls.filter((t) => t.status === 'scheduled'); - const awaitingConfirmation = toolCalls.some( - (t) => t.status === 'awaiting_approval', - ); - if (!awaitingConfirmation && scheduledCalls.length) { - scheduledCalls.forEach(async (c) => { + if (toolCalls.every((t) => t.status === 'scheduled')) { + toolCalls.forEach((c) => { const callId = c.request.callId; - try { - setToolCalls(setStatus(c.request.callId, 'executing')); - const result = await c.tool.execute( - c.request.args, - abortController.signal, - ); - const functionResponse: Part = { - functionResponse: { - name: c.request.name, - id: callId, - response: { output: result.llmContent }, - }, - }; - const response: ToolCallResponseInfo = { - callId, - responsePart: functionResponse, - resultDisplay: result.returnDisplay, - error: undefined, - }; - setToolCalls(setStatus(callId, 'success', response)); - } catch (e: unknown) { - setToolCalls( - setStatus( + setToolCalls(setStatus(c.request.callId, 'executing')); + c.tool + .execute(c.request.args, abortController.signal) + .then((result) => { + const functionResponse: Part = { + functionResponse: { + name: c.request.name, + id: callId, + response: { output: result.llmContent }, + }, + }; + const response: ToolCallResponseInfo = { callId, - 'error', - toolErrorResponse( - c.request, - e instanceof Error ? e : new Error(String(e)), + responsePart: functionResponse, + resultDisplay: result.returnDisplay, + error: undefined, + }; + setToolCalls(setStatus(callId, 'success', response)); + }) + .catch((e) => + setToolCalls( + setStatus( + callId, + 'error', + toolErrorResponse( + c.request, + e instanceof Error ? e : new Error(String(e)), + ), ), ), ); - } }); } }, [toolCalls, toolRegistry, abortController.signal]); useEffect(() => { - const completedTools = toolCalls.filter( + const allDone = toolCalls.every( (t) => t.status === 'success' || t.status === 'error' || t.status === 'cancelled', ); - const allDone = completedTools.length === toolCalls.length; if (toolCalls.length && allDone) { - onComplete(completedTools); setToolCalls([]); - setAbortController(new AbortController()); + onComplete(toolCalls); + setAbortController(() => new AbortController()); } }, [toolCalls, onComplete]); |
