diff options
| author | JAYADITYA <[email protected]> | 2025-08-12 09:43:57 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-12 04:13:57 +0000 |
| commit | 2d1a6af890da1e9437cd1a1774e2c7fc7ad32957 (patch) | |
| tree | 841a9d59013793e2f6485d8e2fe87a2e7328373b /packages/cli/src/ui/hooks | |
| parent | f9efb2e24f41d8738d6ea8d1b8e8be2dff3bb83b (diff) | |
feat(cli): support single Ctrl+C to cancel streaming, preserving double Ctrl+C to exit (#5838)
Diffstat (limited to 'packages/cli/src/ui/hooks')
| -rw-r--r-- | packages/cli/src/ui/hooks/useGeminiStream.ts | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/packages/cli/src/ui/hooks/useGeminiStream.ts b/packages/cli/src/ui/hooks/useGeminiStream.ts index 58bec431..6385d267 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.ts +++ b/packages/cli/src/ui/hooks/useGeminiStream.ts @@ -183,26 +183,39 @@ export const useGeminiStream = ( return StreamingState.Idle; }, [isResponding, toolCalls]); + const cancelOngoingRequest = useCallback(() => { + if (streamingState !== StreamingState.Responding) { + return; + } + if (turnCancelledRef.current) { + return; + } + turnCancelledRef.current = true; + abortControllerRef.current?.abort(); + if (pendingHistoryItemRef.current) { + addItem(pendingHistoryItemRef.current, Date.now()); + } + addItem( + { + type: MessageType.INFO, + text: 'Request cancelled.', + }, + Date.now(), + ); + setPendingHistoryItem(null); + onCancelSubmit(); + setIsResponding(false); + }, [ + streamingState, + addItem, + setPendingHistoryItem, + onCancelSubmit, + pendingHistoryItemRef, + ]); + useInput((_input, key) => { - if (streamingState === StreamingState.Responding && key.escape) { - if (turnCancelledRef.current) { - return; - } - turnCancelledRef.current = true; - abortControllerRef.current?.abort(); - if (pendingHistoryItemRef.current) { - addItem(pendingHistoryItemRef.current, Date.now()); - } - addItem( - { - type: MessageType.INFO, - text: 'Request cancelled.', - }, - Date.now(), - ); - setPendingHistoryItem(null); - onCancelSubmit(); - setIsResponding(false); + if (key.escape) { + cancelOngoingRequest(); } }); @@ -954,5 +967,6 @@ export const useGeminiStream = ( initError, pendingHistoryItems, thought, + cancelOngoingRequest, }; }; |
