fix: unblock input after ESC cancel, suppress abort errors, add hint

- Use Promise.race in handleSlashCommand so ESC abort immediately
  unblocks the submitQuery await chain (fixes /compress blocking input)
- Suppress abort error messages in /compress and /summary when
  cancelled via ESC (cancelSlashCommand already shows "Command cancelled")
- Add "(esc to cancel)" hint below pending slash command items
- Add i18n translations for the new hint in all 6 locales
This commit is contained in:
DragonnZhang 2026-02-11 11:10:40 +08:00
parent 66f754e203
commit 5376ca5873
13 changed files with 106 additions and 7 deletions

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { useCallback, useMemo, useEffect, useState } from 'react';
import { useCallback, useMemo, useEffect, useRef, useState } from 'react';
import { type PartListUnion } from '@google/genai';
import type { UseHistoryManagerReturn } from './useHistoryManager.js';
import {
@ -35,6 +35,7 @@ import { FileCommandLoader } from '../../services/FileCommandLoader.js';
import { McpPromptLoader } from '../../services/McpPromptLoader.js';
import { parseSlashCommand } from '../../utils/commands.js';
import { clearScreen } from '../../utils/stdioHelpers.js';
import { useKeypress } from './useKeypress.js';
import {
type ExtensionUpdateAction,
type ExtensionUpdateStatus,
@ -90,6 +91,7 @@ export const useSlashCommandProcessor = (
loadHistory: UseHistoryManagerReturn['loadHistory'],
refreshStatic: () => void,
toggleVimEnabled: () => Promise<boolean>,
isProcessing: boolean,
setIsProcessing: (isProcessing: boolean) => void,
setGeminiMdFileCount: (count: number) => void,
actions: SlashCommandProcessorActions,
@ -131,6 +133,34 @@ export const useSlashCommandProcessor = (
null,
);
// AbortController for cancelling async slash commands via ESC
const abortControllerRef = useRef<AbortController | null>(null);
const cancelSlashCommand = useCallback(() => {
if (!abortControllerRef.current) {
return;
}
abortControllerRef.current.abort();
setPendingItem(null);
addItem(
{
type: MessageType.INFO,
text: 'Command cancelled.',
},
Date.now(),
);
setIsProcessing(false);
}, [addItem, setIsProcessing]);
useKeypress(
(key) => {
if (key.name === 'escape') {
cancelSlashCommand();
}
},
{ isActive: isProcessing },
);
const pendingHistoryItems = useMemo(() => {
const items: HistoryItemWithoutId[] = [];
if (pendingItem != null) {
@ -319,6 +349,10 @@ export const useSlashCommandProcessor = (
setIsProcessing(true);
// Create a new AbortController for this command execution
const abortController = new AbortController();
abortControllerRef.current = abortController;
const userMessageTimestamp = Date.now();
addItemWithRecording(
{ type: MessageType.USER, text: trimmed },
@ -352,6 +386,7 @@ export const useSlashCommandProcessor = (
args,
},
overwriteConfirmed,
abortSignal: abortController.signal,
};
// If a one-time list is provided for a "Proceed" action, temporarily
@ -365,10 +400,27 @@ export const useSlashCommandProcessor = (
]),
};
}
const result = await commandToExecute.action(
fullCommandContext,
args,
);
// Race the command action against the abort signal so that
// ESC cancellation immediately unblocks the await chain.
// Without this, commands like /compress whose underlying
// operation (tryCompressChat) doesn't accept an AbortSignal
// would keep submitQuery stuck until the operation completes.
const abortPromise = new Promise<undefined>((resolve) => {
abortController.signal.addEventListener(
'abort',
() => resolve(undefined),
{ once: true },
);
});
const result = await Promise.race([
commandToExecute.action(fullCommandContext, args),
abortPromise,
]);
// If the command was cancelled via ESC while executing, skip result processing
if (abortController.signal.aborted) {
return { type: 'handled' };
}
if (result) {
switch (result.type) {
@ -561,6 +613,10 @@ export const useSlashCommandProcessor = (
return { type: 'handled' };
} catch (e: unknown) {
// If cancelled via ESC, the cancelSlashCommand callback already handled cleanup
if (abortController.signal.aborted) {
return { type: 'handled' };
}
hasError = true;
if (config) {
const event = makeSlashCommandEvent({