Merge pull request #1796 from QwenLM/feat/support-cancel-execute-pr
Some checks are pending
Qwen Code CI / Lint (push) Waiting to run
Qwen Code CI / Test (push) Blocked by required conditions
Qwen Code CI / Test-1 (push) Blocked by required conditions
Qwen Code CI / Test-2 (push) Blocked by required conditions
Qwen Code CI / Test-3 (push) Blocked by required conditions
Qwen Code CI / Test-4 (push) Blocked by required conditions
Qwen Code CI / Test-5 (push) Blocked by required conditions
Qwen Code CI / Test-6 (push) Blocked by required conditions
Qwen Code CI / Test-7 (push) Blocked by required conditions
Qwen Code CI / Test-8 (push) Blocked by required conditions
Qwen Code CI / Post Coverage Comment (push) Blocked by required conditions
Qwen Code CI / CodeQL (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:docker (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:none (push) Waiting to run
E2E Tests / E2E Test - macOS (push) Waiting to run

fix: unblock input after ESC cancel
This commit is contained in:
tanzhenxin 2026-02-27 21:32:13 +08:00 committed by GitHub
commit e93b287329
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 131 additions and 19 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();
addItem(
{
type: MessageType.INFO,
text: 'Command cancelled.',
},
Date.now(),
);
setPendingItem(null);
setIsProcessing(false);
}, [addItem, setIsProcessing]);
useKeypress(
(key) => {
if (key.name === 'escape') {
cancelSlashCommand();
}
},
{ isActive: isProcessing },
);
const pendingHistoryItems = useMemo(() => {
const items: HistoryItemWithoutId[] = [];
if (pendingItem != null) {
@ -324,6 +354,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 },
@ -357,6 +391,7 @@ export const useSlashCommandProcessor = (
args,
},
overwriteConfirmed,
abortSignal: abortController.signal,
};
// If a one-time list is provided for a "Proceed" action, temporarily
@ -370,10 +405,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) {
@ -566,6 +618,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({