feat(cli): Prevent queuing of slash and shell commands (#11094)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
Jainam M
2025-10-15 22:32:50 +05:30
committed by GitHub
parent b8df8b2ab8
commit 4f17eae5cc
7 changed files with 215 additions and 2 deletions

View File

@@ -89,6 +89,8 @@ export const Composer = () => {
</Text>
) : uiState.showEscapePrompt ? (
<Text color={theme.text.secondary}>Press Esc again to clear.</Text>
) : uiState.queueErrorMessage ? (
<Text color={theme.status.error}>{uiState.queueErrorMessage}</Text>
) : (
!settings.merged.ui?.hideContextSummary && (
<ContextSummaryDisplay
@@ -149,6 +151,8 @@ export const Composer = () => {
? " Press 'i' for INSERT mode and 'Esc' for NORMAL mode."
: ' Type your message or @path/to/file'
}
setQueueErrorMessage={uiActions.setQueueErrorMessage}
streamingState={uiState.streamingState}
/>
)}

View File

@@ -28,6 +28,7 @@ import { useKittyKeyboardProtocol } from '../hooks/useKittyKeyboardProtocol.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
import stripAnsi from 'strip-ansi';
import chalk from 'chalk';
import { StreamingState } from '../types.js';
vi.mock('../hooks/useShellHistory.js');
vi.mock('../hooks/useCommandCompletion.js');
@@ -2167,7 +2168,58 @@ describe('InputPrompt', () => {
expect(mockBuffer.handleInput).toHaveBeenCalled();
unmount();
});
it('should prevent slash commands from being queued while streaming', async () => {
props.onSubmit = vi.fn();
props.buffer.text = '/help';
props.setQueueErrorMessage = vi.fn();
props.streamingState = StreamingState.Responding;
const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();
stdin.write('/help');
stdin.write('\r');
await wait();
expect(props.onSubmit).not.toHaveBeenCalled();
expect(props.setQueueErrorMessage).toHaveBeenCalledWith(
'Slash commands cannot be queued',
);
unmount();
});
it('should prevent shell commands from being queued while streaming', async () => {
props.onSubmit = vi.fn();
props.buffer.text = 'ls';
props.setQueueErrorMessage = vi.fn();
props.streamingState = StreamingState.Responding;
props.shellModeActive = true;
const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();
stdin.write('ls');
stdin.write('\r');
await wait();
expect(props.onSubmit).not.toHaveBeenCalled();
expect(props.setQueueErrorMessage).toHaveBeenCalledWith(
'Shell commands cannot be queued',
);
unmount();
});
it('should allow regular messages to be queued while streaming', async () => {
props.onSubmit = vi.fn();
props.buffer.text = 'regular message';
props.setQueueErrorMessage = vi.fn();
props.streamingState = StreamingState.Responding;
const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();
stdin.write('regular message');
stdin.write('\r');
await wait();
expect(props.onSubmit).toHaveBeenCalledWith('regular message');
expect(props.setQueueErrorMessage).not.toHaveBeenCalled();
unmount();
});
});
function clean(str: string | undefined): string {
if (!str) return '';
// Remove ANSI escape codes and trim whitespace

View File

@@ -38,6 +38,8 @@ import * as path from 'node:path';
import { SCREEN_READER_USER_PREFIX } from '../textConstants.js';
import { useShellFocusState } from '../contexts/ShellFocusContext.js';
import { useUIState } from '../contexts/UIStateContext.js';
import { StreamingState } from '../types.js';
import { isSlashCommand } from '../utils/commandUtils.js';
/**
* Returns if the terminal can be trusted to handle paste events atomically
@@ -71,6 +73,8 @@ export interface InputPromptProps {
onEscapePromptChange?: (showPrompt: boolean) => void;
vimHandleInput?: (key: Key) => boolean;
isEmbeddedShellFocused?: boolean;
setQueueErrorMessage: (message: string | null) => void;
streamingState: StreamingState;
}
// The input content, input container, and input suggestions list may have different widths
@@ -107,6 +111,8 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
onEscapePromptChange,
vimHandleInput,
isEmbeddedShellFocused,
setQueueErrorMessage,
streamingState,
}) => {
const kittyProtocol = useKittyKeyboardProtocol();
const isShellFocused = useShellFocusState();
@@ -221,6 +227,31 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
],
);
const handleSubmit = useCallback(
(submittedValue: string) => {
const trimmedMessage = submittedValue.trim();
const isSlash = isSlashCommand(trimmedMessage);
const isShell = shellModeActive;
if (
(isSlash || isShell) &&
streamingState === StreamingState.Responding
) {
setQueueErrorMessage(
`${isShell ? 'Shell' : 'Slash'} commands cannot be queued`,
);
return;
}
handleSubmitAndClear(trimmedMessage);
},
[
handleSubmitAndClear,
shellModeActive,
streamingState,
setQueueErrorMessage,
],
);
const customSetTextAndResetCompletionSignal = useCallback(
(newText: string) => {
buffer.setText(newText);
@@ -514,7 +545,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
// If the command is a perfect match, pressing enter should execute it.
if (completion.isPerfectMatch && keyMatchers[Command.RETURN](key)) {
handleSubmitAndClear(buffer.text);
handleSubmit(buffer.text);
return;
}
@@ -625,7 +656,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
buffer.backspace();
buffer.newline();
} else {
handleSubmitAndClear(buffer.text);
handleSubmit(buffer.text);
}
}
return;
@@ -706,6 +737,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
onClearScreen,
inputHistory,
handleSubmitAndClear,
handleSubmit,
shellHistory,
reverseSearchCompletion,
handleClipboardImage,