From f2f2ecf9d83224778e5fc38cfcc4a1edddf9f7d4 Mon Sep 17 00:00:00 2001 From: Taylor Mullen Date: Tue, 27 May 2025 23:40:25 -0700 Subject: feat: Allow cancellation of in-progress Gemini requests and pre-execution checks - Implements cancellation for Gemini requests while they are actively being processed by the model. - Extends cancellation support to the logic within tools. This allows users to cancel operations during the phase where the system is determining if a tool execution requires user confirmation, which can include potentially long-running pre-flight checks or LLM-based corrections. - Underlying LLM calls for edit corrections (within and ) and next speaker checks can now also be cancelled. - Previously, cancellation of the main request was not possible until text started streaming, and pre-execution checks were not cancellable. - This change leverages the updated SDK's ability to accept an abort token and threads s throughout the request, tool execution, and pre-execution check lifecycle. Fixes https://github.com/google-gemini/gemini-cli/issues/531 --- packages/cli/src/gemini.tsx | 8 +++++--- packages/cli/src/ui/hooks/useToolScheduler.ts | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'packages/cli/src') diff --git a/packages/cli/src/gemini.tsx b/packages/cli/src/gemini.tsx index 11875593..9cfaef37 100644 --- a/packages/cli/src/gemini.tsx +++ b/packages/cli/src/gemini.tsx @@ -95,9 +95,11 @@ async function main() { const geminiClient = new GeminiClient(config); const chat = await geminiClient.startChat(); try { - for await (const event of geminiClient.sendMessageStream(chat, [ - { text: input }, - ])) { + for await (const event of geminiClient.sendMessageStream( + chat, + [{ text: input }], + new AbortController().signal, + )) { if (event.type === 'content') { process.stdout.write(event.value); } diff --git a/packages/cli/src/ui/hooks/useToolScheduler.ts b/packages/cli/src/ui/hooks/useToolScheduler.ts index f1eee9fd..7d8cfbe4 100644 --- a/packages/cli/src/ui/hooks/useToolScheduler.ts +++ b/packages/cli/src/ui/hooks/useToolScheduler.ts @@ -142,7 +142,10 @@ export function useToolScheduler( const { request: r, tool } = initialCall; try { - const userApproval = await tool.shouldConfirmExecute(r.args); + const userApproval = await tool.shouldConfirmExecute( + r.args, + abortController.signal, + ); if (userApproval) { // Confirmation is needed. Update status to 'awaiting_approval'. setToolCalls( @@ -183,7 +186,7 @@ export function useToolScheduler( } }); }, - [isRunning, setToolCalls, toolRegistry], + [isRunning, setToolCalls, toolRegistry, abortController.signal], ); const cancel = useCallback( -- cgit v1.2.3