Changesets · Branches · Tags · Compare
| Changeset | af695458de9dac865a7252688b8bb58337134e005e76885708b75182eac2997a |
|---|---|
| Author | Ting-Wai <40179554+tingwai@users.noreply.github.com> · 2025-09-05 13:40 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-05 13:40 -07:00 |
| Parents | ef50d56d248f, 3f8d78b26826 |
7 files changed
added extensions/cli/spec/shell-mode.md +138 −0
| @@ -0,0 +1,138 @@ | ||
| 1 | +shell Mode (CLI TUI) | |
| 2 | + | |
| 3 | +Overview | |
| 4 | + | |
| 5 | +- shell mode lets users run shell commands directly from the chat input by starting their input with an exclamation mark (!). | |
| 6 | +- It is intended for quick terminal command execution without leaving the TUI. | |
| 7 | + | |
| 8 | +Activation | |
| 9 | + | |
| 10 | +- shell mode is activated when the current input (trimmed) starts with ! | |
| 11 | + - Example: "!git status" or " !ls -la" both activate shell mode. | |
| 12 | +- Visual indicator: | |
| 13 | + - Input border color changes to yellow. | |
| 14 | + - The input prompt indicator changes to a yellow "$". | |
| 15 | + - The input placeholder includes "! for shell mode". | |
| 16 | + | |
| 17 | +Deactivation / Exiting shell Mode | |
| 18 | + | |
| 19 | +- Pressing Enter to submit the input exits shell mode immediately after submission, regardless of the command result. | |
| 20 | +- Pressing Esc when the input (trimmed) is exactly ! clears the input and exits shell mode. | |
| 21 | +- Editing the input so it no longer starts with ! also exits shell mode and restores normal input behavior. | |
| 22 | + | |
| 23 | +Interaction with other input helpers | |
| 24 | + | |
| 25 | +- When in shell mode (input starts with !): | |
| 26 | + - "@" file search suggestions are disabled. | |
| 27 | + - "/" slash command UI is disabled. | |
| 28 | +- When in slash command mode (input starts with /): | |
| 29 | + - "@" file search suggestions are disabled. | |
| 30 | + | |
| 31 | +Submission behavior | |
| 32 | + | |
| 33 | +- On submit (Enter) with a shell-mode input: | |
| 34 | + - The leading ! is removed and the remainder is treated as the shell command to run. | |
| 35 | + - The TUI immediately appends an assistant message representing a shell tool call, with status set to calling, so users can see that the command is in progress. | |
| 36 | + - The shell command is executed asynchronously; when it completes, the tool call status is updated to done (or error) and the output is populated. | |
| 37 | + | |
| 38 | +Execution semantics | |
| 39 | + | |
| 40 | +- The command is executed in the same way as terminal tool commands, minus permissions | |
| 41 | + | |
| 42 | +Output handling | |
| 43 | + | |
| 44 | +- Stdout is streamed into memory; Stderr is captured and appended as a trailing "Stderr: ..." section on success. | |
| 45 | +- If the process exits non-zero and Stderr contains content, the tool call is marked as error and the error text is shown. | |
| 46 | +- Output is truncated to the first 5000 lines if exceeded. | |
| 47 | +- Timeout behavior: If no output is received for 30 seconds (configurable in tests), the process is terminated and the result includes a note like: | |
| 48 | + "[Command timed out after 30 seconds of no output]". | |
| 49 | + | |
| 50 | +Keyboard behaviors (summary) | |
| 51 | + | |
| 52 | +- Enter: submit input. If in shell mode, exits shell mode after submission and shows the pending shell tool call immediately. | |
| 53 | +- Shift+Enter: new line. | |
| 54 | +- Backslash (\) at end-of-line: inserts a new line (line continuation) as usual. | |
| 55 | +- Esc: if only ! (trimmed) is present, clears input and exits shell mode; otherwise cancels streaming or closes suggestions depending on context. | |
| 56 | + | |
| 57 | +Scope / Modes | |
| 58 | + | |
| 59 | +- shell mode applies to interactive (TUI/standard) CLI usage. It is not part of headless (-p/--print) processing. | |
| 60 | + | |
| 61 | +Error handling | |
| 62 | + | |
| 63 | +- Command execution errors are captured and surfaced in the tool call as status error with human-readable error text (including Stderr when available). | |
| 64 | + | |
| 65 | +Examples | |
| 66 | + | |
| 67 | +- "!git status" → shows a shell tool call immediately, then populates with the git status output. | |
| 68 | +- "!echo hello" → shows a shell tool call immediately, then output "hello". | |
| 69 | +- "!some-unknown-cmd" → shows a shell tool call immediately, then sets status to error with an error message. | |
| 70 | + Shell Mode (CLI TUI) | |
| 71 | + | |
| 72 | +Overview | |
| 73 | + | |
| 74 | +- Shell mode lets users run shell commands directly from the chat input by starting their input with an exclamation mark (!). | |
| 75 | +- It is intended for quick terminal command execution without leaving the TUI. | |
| 76 | + | |
| 77 | +Activation | |
| 78 | + | |
| 79 | +- Shell mode is activated when the current input (trimmed) starts with ! | |
| 80 | + - Example: "!git status" or " !ls -la" both activate Shell mode. | |
| 81 | +- Visual indicator: | |
| 82 | + - Input border color changes to yellow. | |
| 83 | + - The input prompt indicator changes to a yellow "$". | |
| 84 | + - The input placeholder includes "! for shell mode". | |
| 85 | + | |
| 86 | +Deactivation / Exiting Shell Mode | |
| 87 | + | |
| 88 | +- Pressing Enter to submit the input exits shell mode immediately after submission, regardless of the command result. | |
| 89 | +- Pressing Esc when the input (trimmed) is exactly ! clears the input and exits shell mode. | |
| 90 | +- Editing the input so it no longer starts with ! also exits shell mode and restores normal input behavior. | |
| 91 | + | |
| 92 | +Interaction with other input helpers | |
| 93 | + | |
| 94 | +- When in shell mode (input starts with !): | |
| 95 | + - "@" file search suggestions are disabled. | |
| 96 | + - "/" slash command UI is disabled. | |
| 97 | +- When in slash command mode (input starts with /): | |
| 98 | + - "@" file search suggestions are disabled. | |
| 99 | + | |
| 100 | +Submission behavior | |
| 101 | + | |
| 102 | +- On submit (Enter) with a shell-mode input: | |
| 103 | + - The leading ! is removed and the remainder is treated as the shell command to run. | |
| 104 | + - The TUI immediately appends an assistant message representing a Shell tool call, with status set to calling, so users can see that the command is in progress. | |
| 105 | + - The shell command is executed asynchronously; when it completes, the tool call status is updated to done (or error) and the output is populated. | |
| 106 | + | |
| 107 | +Execution semantics | |
| 108 | + | |
| 109 | +- The command is executed in the same way as terminal tool commands, minus permissions, and inserted into the chat history the same. | |
| 110 | + | |
| 111 | +Output handling | |
| 112 | + | |
| 113 | +- Stdout is streamed into memory; Stderr is captured and appended as a trailing "Stderr: ..." section on success. | |
| 114 | +- If the process exits non-zero and Stderr contains content, the tool call is marked as error and the error text is shown. | |
| 115 | +- Output is truncated to the first 5000 lines if exceeded. | |
| 116 | +- Timeout behavior: If no output is received for 30 seconds (configurable in tests), the process is terminated and the result includes a note like: | |
| 117 | + "[Command timed out after 30 seconds of no output]". | |
| 118 | + | |
| 119 | +Keyboard behaviors (summary) | |
| 120 | + | |
| 121 | +- Enter: submit input. If in shell mode, exits shell mode after submission and shows the pending Shell tool call immediately. | |
| 122 | +- Shift+Enter: new line. | |
| 123 | +- Backslash (\) at end-of-line: inserts a new line (line continuation) as usual. | |
| 124 | +- Esc: if only ! (trimmed) is present, clears input and exits shell mode; otherwise cancels streaming or closes suggestions depending on context. | |
| 125 | + | |
| 126 | +Scope / Modes | |
| 127 | + | |
| 128 | +- Shell mode applies to interactive (TUI/standard) CLI usage. It is not part of headless (-p/--print) processing. | |
| 129 | + | |
| 130 | +Error handling | |
| 131 | + | |
| 132 | +- Command execution errors are captured and surfaced in the tool call as status error with human-readable error text (including Stderr when available). | |
| 133 | + | |
| 134 | +Examples | |
| 135 | + | |
| 136 | +- "!git status" → shows a Shell tool call immediately, then populates with the git status output. | |
| 137 | +- "!echo hello" → shows a Shell tool call immediately, then output "hello". | |
| 138 | +- "!some-unknown-cmd" → shows a Shell tool call immediately, then sets status to error with an error message. |
modified extensions/cli/src/slashCommands.ts +3 −0
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | ` ${chalk.cyan("Enter")} Submit message`, |
| 29 | 29 | ` ${chalk.cyan("Shift+Enter")} New line`, |
| 30 | 30 | ` ${chalk.cyan("\\")} Line continuation (at end of line)`, |
| 31 | + ` ${chalk.cyan("!")} Shell mode - run shell commands`, | |
| 31 | 32 | "", |
| 32 | 33 | chalk.white("Controls:"), |
| 33 | 34 | ` ${chalk.cyan("Ctrl+C")} Clear input`, |
| @@ -39,9 +40,11 @@ | ||
| 39 | 40 | chalk.white("Special Characters:"), |
| 40 | 41 | ` ${chalk.cyan("@")} Search and attach files for context`, |
| 41 | 42 | ` ${chalk.cyan("/")} Access slash commands`, |
| 43 | + ` ${chalk.cyan("!")} Execute bash commands directly`, | |
| 42 | 44 | "", |
| 43 | 45 | chalk.white("Available Commands:"), |
| 44 | 46 | ` Type ${chalk.cyan("/")} to see available slash commands`, |
| 47 | + ` Type ${chalk.cyan("!")} followed by a command to execute bash directly`, | |
| 45 | 48 | ].join("\n"); |
| 46 | 49 | posthogService.capture("useSlashCommand", { name: "help" }); |
| 47 | 50 | return { output: helpMessage }; |
modified extensions/cli/src/ui/UserInput.tsx +140 −35
| @@ -20,6 +20,77 @@ | ||
| 20 | 20 | import { SlashCommandUI } from "./SlashCommandUI.js"; |
| 21 | 21 | import { TextBuffer } from "./TextBuffer.js"; |
| 22 | 22 | |
| 23 | +// Small presentational helpers to reduce cyclomatic complexity in UserInput | |
| 24 | +const InterruptedBanner: React.FC<{ wasInterrupted: boolean }> = ({ | |
| 25 | + wasInterrupted, | |
| 26 | +}) => { | |
| 27 | + if (!wasInterrupted) return null; | |
| 28 | + return ( | |
| 29 | + <Box paddingX={1} marginBottom={0}> | |
| 30 | + <Text color="yellow">⚠ Interrupted by user - Press enter to resume</Text> | |
| 31 | + </Box> | |
| 32 | + ); | |
| 33 | +}; | |
| 34 | + | |
| 35 | +const SlashCommandsMaybe: React.FC<{ | |
| 36 | + show: boolean; | |
| 37 | + inputMode: boolean; | |
| 38 | + hideNormalUI: boolean; | |
| 39 | + isRemoteMode: boolean; | |
| 40 | + assistant?: AssistantConfig; | |
| 41 | + filter: string; | |
| 42 | + selectedIndex: number; | |
| 43 | +}> = ({ | |
| 44 | + show, | |
| 45 | + inputMode, | |
| 46 | + hideNormalUI, | |
| 47 | + isRemoteMode, | |
| 48 | + assistant, | |
| 49 | + filter, | |
| 50 | + selectedIndex, | |
| 51 | +}) => { | |
| 52 | + if (!show || !inputMode || hideNormalUI || !(isRemoteMode || assistant)) | |
| 53 | + return null; | |
| 54 | + return ( | |
| 55 | + <SlashCommandUI | |
| 56 | + assistant={assistant} | |
| 57 | + filter={filter} | |
| 58 | + selectedIndex={selectedIndex} | |
| 59 | + isRemoteMode={isRemoteMode} | |
| 60 | + /> | |
| 61 | + ); | |
| 62 | +}; | |
| 63 | + | |
| 64 | +const FileSearchMaybe: React.FC<{ | |
| 65 | + show: boolean; | |
| 66 | + inputMode: boolean; | |
| 67 | + hideNormalUI: boolean; | |
| 68 | + isRemoteMode: boolean; | |
| 69 | + filter: string; | |
| 70 | + selectedIndex: number; | |
| 71 | + onSelect: (path: string) => void | Promise<void>; | |
| 72 | + onFilesUpdated: (files: Array<{ path: string; displayName: string }>) => void; | |
| 73 | +}> = ({ | |
| 74 | + show, | |
| 75 | + inputMode, | |
| 76 | + hideNormalUI, | |
| 77 | + isRemoteMode, | |
| 78 | + filter, | |
| 79 | + selectedIndex, | |
| 80 | + onSelect, | |
| 81 | + onFilesUpdated, | |
| 82 | +}) => { | |
| 83 | + if (!show || !inputMode || hideNormalUI || isRemoteMode) return null; | |
| 84 | + return ( | |
| 85 | + <FileSearchUI | |
| 86 | + filter={filter} | |
| 87 | + selectedIndex={selectedIndex} | |
| 88 | + onSelect={onSelect} | |
| 89 | + onFilesUpdated={onFilesUpdated} | |
| 90 | + /> | |
| 91 | + ); | |
| 92 | +}; | |
| 93 | + | |
| 23 | 94 | interface UserInputProps { |
| 24 | 95 | onSubmit: (message: string, imageMap?: Map<string, Buffer>) => void; |
| 25 | 96 | isWaitingForResponse: boolean; |
| @@ -78,10 +149,13 @@ | ||
| 78 | 149 | setCursorPosition(0); |
| 79 | 150 | setShowSlashCommands(false); |
| 80 | 151 | setShowFileSearch(false); |
| 152 | + setShowBashMode(false); | |
| 81 | 153 | inputHistory.resetNavigation(); |
| 82 | 154 | }, [textBuffer]); |
| 155 | + | |
| 83 | 156 | const [slashCommandFilter, setSlashCommandFilter] = useState(""); |
| 84 | 157 | const [selectedCommandIndex, setSelectedCommandIndex] = useState(0); |
| 158 | + const [showBashMode, setShowBashMode] = useState(false); | |
| 85 | 159 | const [showFileSearch, setShowFileSearch] = useState(false); |
| 86 | 160 | const [fileSearchFilter, setFileSearchFilter] = useState(""); |
| 87 | 161 | const [selectedFileIndex, setSelectedFileIndex] = useState(0); |
| @@ -131,6 +205,18 @@ | ||
| 131 | 205 | return nextMode; |
| 132 | 206 | }; |
| 133 | 207 | |
| 208 | + // Update shell mode state based on input | |
| 209 | + const updateBashModeState = (text: string) => { | |
| 210 | + const trimmed = text.trimStart(); | |
| 211 | + const isBash = trimmed.startsWith("!"); | |
| 212 | + setShowBashMode(isBash); | |
| 213 | + if (isBash) { | |
| 214 | + // Disable other helpers while in shell mode | |
| 215 | + setShowSlashCommands(false); | |
| 216 | + setShowFileSearch(false); | |
| 217 | + } | |
| 218 | + }; | |
| 219 | + | |
| 134 | 220 | // Update slash command UI state based on input |
| 135 | 221 | const updateSlashCommandState = (text: string, cursor: number) => { |
| 136 | 222 | // Only show slash commands if slash is the very first character |
| @@ -139,6 +225,9 @@ | ||
| 139 | 225 | return; |
| 140 | 226 | } |
| 141 | 227 | |
| 228 | + // When using slash commands, disable file search | |
| 229 | + setShowFileSearch(false); | |
| 230 | + | |
| 142 | 231 | const trimmedText = text.trimStart(); |
| 143 | 232 | const beforeCursor = trimmedText.slice( |
| 144 | 233 | 0, |
| @@ -196,6 +285,13 @@ | ||
| 196 | 285 | return; |
| 197 | 286 | } |
| 198 | 287 | |
| 288 | + // Disable file search if we're in shell mode or slash command mode | |
| 289 | + const trimmed = text.trimStart(); | |
| 290 | + if (trimmed.startsWith("!") || trimmed.startsWith("/")) { | |
| 291 | + setShowFileSearch(false); | |
| 292 | + return; | |
| 293 | + } | |
| 294 | + | |
| 199 | 295 | // Check if we're in a file search context |
| 200 | 296 | const beforeCursor = text.slice(0, cursor); |
| 201 | 297 | const lastAtIndex = beforeCursor.lastIndexOf("@"); |
| @@ -234,6 +330,7 @@ | ||
| 234 | 330 | setFileSearchFilter(afterAt); |
| 235 | 331 | setSelectedFileIndex(0); |
| 236 | 332 | setShowSlashCommands(false); |
| 333 | + setShowBashMode(false); | |
| 237 | 334 | } |
| 238 | 335 | } |
| 239 | 336 | }; |
| @@ -278,6 +375,7 @@ | ||
| 278 | 375 | setInputText(newText); |
| 279 | 376 | setCursorPosition(newCursorPos); |
| 280 | 377 | setShowSlashCommands(false); |
| 378 | + setShowBashMode(false); | |
| 281 | 379 | } |
| 282 | 380 | }; |
| 283 | 381 | |
| @@ -437,6 +535,7 @@ | ||
| 437 | 535 | setInputText(""); |
| 438 | 536 | setCursorPosition(0); |
| 439 | 537 | setShowSlashCommands(false); |
| 538 | + setShowBashMode(false); | |
| 440 | 539 | } |
| 441 | 540 | return true; |
| 442 | 541 | } |
| @@ -462,6 +561,15 @@ | ||
| 462 | 561 | const handleEscapeKey = (key: any): boolean => { |
| 463 | 562 | if (!key.escape) return false; |
| 464 | 563 | |
| 564 | + // If only "!" is present, clear shell mode | |
| 565 | + if (inputMode && showBashMode && inputText.trim() === "!") { | |
| 566 | + textBuffer.clear(); | |
| 567 | + setInputText(""); | |
| 568 | + setCursorPosition(0); | |
| 569 | + setShowBashMode(false); | |
| 570 | + return true; | |
| 571 | + } | |
| 572 | + | |
| 465 | 573 | // Handle escape key to interrupt streaming |
| 466 | 574 | if (isWaitingForResponse && onInterrupt) { |
| 467 | 575 | onInterrupt(); |
| @@ -491,6 +599,7 @@ | ||
| 491 | 599 | setCursorPosition(newCursor); |
| 492 | 600 | updateSlashCommandState(newText, newCursor); |
| 493 | 601 | updateFileSearchState(newText, newCursor); |
| 602 | + updateBashModeState(newText); | |
| 494 | 603 | inputHistory.resetNavigation(); |
| 495 | 604 | }; |
| 496 | 605 | |
| @@ -587,14 +696,16 @@ | ||
| 587 | 696 | setCursorPosition, |
| 588 | 697 | updateSlashCommandState, |
| 589 | 698 | updateFileSearchState, |
| 699 | + updateBashModeState, | |
| 590 | 700 | inputHistory, |
| 591 | 701 | }); |
| 592 | 702 | }); |
| 593 | 703 | |
| 594 | 704 | const renderInputText = () => { |
| 595 | 705 | const placeholderText = isRemoteMode |
| 596 | - ? "Ask anything, / for slash commands" | |
| 597 | - : placeholder || "Ask anything, @ for context, / for slash commands"; | |
| 706 | + ? "Ask anything, / for slash commands, ! for shell mode" | |
| 707 | + : placeholder || | |
| 708 | + "Ask anything, @ for context, / for slash commands, ! for shell mode"; | |
| 598 | 709 | if (inputText.length === 0) { |
| 599 | 710 | return ( |
| 600 | 711 | <> |
| @@ -677,50 +788,44 @@ | ||
| 677 | 788 | |
| 678 | 789 | return ( |
| 679 | 790 | <Box flexDirection="column"> |
| 680 | - {/* Interruption message - shown just above the input box */} | |
| 681 | - {wasInterrupted && ( | |
| 682 | - <Box paddingX={1} marginBottom={0}> | |
| 683 | - <Text color="yellow"> | |
| 684 | - ⚠ Interrupted by user - Press enter to resume | |
| 685 | - </Text> | |
| 686 | - </Box> | |
| 687 | - )} | |
| 791 | + <InterruptedBanner wasInterrupted={!!wasInterrupted} /> | |
| 688 | 792 | |
| 689 | 793 | {/* Input box */} |
| 690 | 794 | <Box |
| 691 | 795 | borderStyle="round" |
| 692 | 796 | borderTop={true} |
| 693 | 797 | paddingX={1} |
| 694 | - borderColor={isRemoteMode ? "cyan" : "gray"} | |
| 798 | + borderColor={showBashMode ? "yellow" : isRemoteMode ? "cyan" : "gray"} | |
| 695 | 799 | > |
| 696 | - <Text color={isRemoteMode ? "cyan" : "blue"} bold> | |
| 697 | - {isRemoteMode ? "◉" : "●"}{" "} | |
| 800 | + <Text | |
| 801 | + color={showBashMode ? "yellow" : isRemoteMode ? "cyan" : "blue"} | |
| 802 | + bold | |
| 803 | + > | |
| 804 | + {showBashMode ? "$" : isRemoteMode ? "◉" : "●"} | |
| 698 | 805 | </Text> |
| 699 | 806 | {renderInputText()} |
| 700 | 807 | </Box> |
| 701 | 808 | |
| 702 | - {/* Slash command UI - show in remote mode OR when assistant is available */} | |
| 703 | - {showSlashCommands && | |
| 704 | - inputMode && | |
| 705 | - !hideNormalUI && | |
| 706 | - (isRemoteMode || assistant) && ( | |
| 707 | - <SlashCommandUI | |
| 708 | - assistant={assistant} | |
| 709 | - filter={slashCommandFilter} | |
| 710 | - selectedIndex={selectedCommandIndex} | |
| 711 | - isRemoteMode={isRemoteMode} | |
| 712 | - /> | |
| 713 | - )} | |
| 714 | - | |
| 715 | - {/* File search UI - only show in local mode */} | |
| 716 | - {showFileSearch && inputMode && !hideNormalUI && !isRemoteMode && ( | |
| 717 | - <FileSearchUI | |
| 718 | - filter={fileSearchFilter} | |
| 719 | - selectedIndex={selectedFileIndex} | |
| 720 | - onSelect={selectFile} | |
| 721 | - onFilesUpdated={setCurrentFiles} | |
| 722 | - /> | |
| 723 | - )} | |
| 809 | + <SlashCommandsMaybe | |
| 810 | + show={showSlashCommands} | |
| 811 | + inputMode={inputMode} | |
| 812 | + hideNormalUI={!!hideNormalUI} | |
| 813 | + isRemoteMode={!!isRemoteMode} | |
| 814 | + assistant={assistant} | |
| 815 | + filter={slashCommandFilter} | |
| 816 | + selectedIndex={selectedCommandIndex} | |
| 817 | + /> | |
| 818 | + | |
| 819 | + <FileSearchMaybe | |
| 820 | + show={showFileSearch} | |
| 821 | + inputMode={inputMode} | |
| 822 | + hideNormalUI={!!hideNormalUI} | |
| 823 | + isRemoteMode={!!isRemoteMode} | |
| 824 | + filter={fileSearchFilter} | |
| 825 | + selectedIndex={selectedFileIndex} | |
| 826 | + onSelect={selectFile} | |
| 827 | + onFilesUpdated={setCurrentFiles} | |
| 828 | + /> | |
| 724 | 829 | </Box> |
| 725 | 830 | ); |
| 726 | 831 | }; |
added extensions/cli/src/ui/hooks/useChat.shellMode.test.ts +148 −0
| @@ -0,0 +1,148 @@ | ||
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; | |
| 2 | + | |
| 3 | +import { runTerminalCommandTool } from "src/tools/runTerminalCommand.js"; | |
| 4 | + | |
| 5 | +import { services } from "../../services/index.js"; | |
| 6 | + | |
| 7 | +import { handleBashModeProcessing } from "./useChat.shellMode.js"; | |
| 8 | + | |
| 9 | +// Mock the runTerminalCommandTool with the correct path for the import in shellMode.ts | |
| 10 | +vi.mock("src/tools/runTerminalCommand.js", () => ({ | |
| 11 | + runTerminalCommandTool: { | |
| 12 | + run: vi.fn(), | |
| 13 | + }, | |
| 14 | +})); | |
| 15 | + | |
| 16 | +// Mock the services | |
| 17 | +vi.mock("../../services/index.js", () => ({ | |
| 18 | + services: { | |
| 19 | + chatHistory: { | |
| 20 | + addAssistantMessage: vi.fn(), | |
| 21 | + updateToolStatus: vi.fn(), | |
| 22 | + addToolResult: vi.fn(), | |
| 23 | + }, | |
| 24 | + }, | |
| 25 | +})); | |
| 26 | + | |
| 27 | +describe("handleBashModeProcessing", () => { | |
| 28 | + beforeEach(() => { | |
| 29 | + vi.clearAllMocks(); | |
| 30 | + }); | |
| 31 | + | |
| 32 | + it("should detect bash mode when message starts with !", async () => { | |
| 33 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 34 | + mockRun.mockResolvedValue( | |
| 35 | + "total 5\ndrwxr-xr-x 2 user user 4096 Jan 1 12:00 .\ndrwxr-xr-x 3 user user 4096 Jan 1 12:00 ..", | |
| 36 | + ); | |
| 37 | + | |
| 38 | + const result = await handleBashModeProcessing("!ls -la"); | |
| 39 | + | |
| 40 | + expect(result).toBe(null); // Processed, no further handling needed | |
| 41 | + expect(services.chatHistory.addAssistantMessage).toHaveBeenCalled(); | |
| 42 | + expect(services.chatHistory.updateToolStatus).toHaveBeenCalledWith( | |
| 43 | + expect.any(String), | |
| 44 | + "calling", | |
| 45 | + ); | |
| 46 | + | |
| 47 | + // Use vi.waitFor to wait for async operations in the void expression | |
| 48 | + await vi.waitFor( | |
| 49 | + () => { | |
| 50 | + expect(mockRun).toHaveBeenCalledWith({ command: "ls -la" }); | |
| 51 | + }, | |
| 52 | + { timeout: 1000 }, | |
| 53 | + ); | |
| 54 | + | |
| 55 | + await vi.waitFor( | |
| 56 | + () => { | |
| 57 | + expect(services.chatHistory.addToolResult).toHaveBeenCalledWith( | |
| 58 | + expect.any(String), | |
| 59 | + "total 5\ndrwxr-xr-x 2 user user 4096 Jan 1 12:00 .\ndrwxr-xr-x 3 user user 4096 Jan 1 12:00 ..", | |
| 60 | + "done", | |
| 61 | + ); | |
| 62 | + }, | |
| 63 | + { timeout: 1000 }, | |
| 64 | + ); | |
| 65 | + }); | |
| 66 | + | |
| 67 | + it("should handle bash command errors", async () => { | |
| 68 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 69 | + mockRun.mockRejectedValue(new Error("Command not found")); | |
| 70 | + | |
| 71 | + const result = await handleBashModeProcessing("!invalidcommand"); | |
| 72 | + | |
| 73 | + expect(result).toBe(null); // Processed, no further handling needed | |
| 74 | + expect(services.chatHistory.addAssistantMessage).toHaveBeenCalled(); | |
| 75 | + expect(services.chatHistory.updateToolStatus).toHaveBeenCalledWith( | |
| 76 | + expect.any(String), | |
| 77 | + "calling", | |
| 78 | + ); | |
| 79 | + | |
| 80 | + // Use vi.waitFor to wait for async operations in the void expression | |
| 81 | + await vi.waitFor( | |
| 82 | + () => { | |
| 83 | + expect(mockRun).toHaveBeenCalledWith({ command: "invalidcommand" }); | |
| 84 | + }, | |
| 85 | + { timeout: 1000 }, | |
| 86 | + ); | |
| 87 | + | |
| 88 | + await vi.waitFor( | |
| 89 | + () => { | |
| 90 | + expect(services.chatHistory.addToolResult).toHaveBeenCalledWith( | |
| 91 | + expect.any(String), | |
| 92 | + "Bash command failed: Command not found", | |
| 93 | + "errored", | |
| 94 | + ); | |
| 95 | + }, | |
| 96 | + { timeout: 1000 }, | |
| 97 | + ); | |
| 98 | + }); | |
| 99 | + | |
| 100 | + it("should not trigger bash mode for regular messages", async () => { | |
| 101 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 102 | + | |
| 103 | + const result = await handleBashModeProcessing("Hello! How are you?"); | |
| 104 | + | |
| 105 | + expect(result).toBe("Hello! How are you?"); // Pass through unchanged | |
| 106 | + expect(mockRun).not.toHaveBeenCalled(); | |
| 107 | + expect(services.chatHistory.addAssistantMessage).not.toHaveBeenCalled(); | |
| 108 | + }); | |
| 109 | + | |
| 110 | + it("should handle empty bash command", async () => { | |
| 111 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 112 | + | |
| 113 | + const result = await handleBashModeProcessing("!"); | |
| 114 | + | |
| 115 | + expect(result).toBe("!"); // Pass through unchanged for empty command | |
| 116 | + expect(mockRun).not.toHaveBeenCalled(); | |
| 117 | + expect(services.chatHistory.addAssistantMessage).not.toHaveBeenCalled(); | |
| 118 | + }); | |
| 119 | + | |
| 120 | + it("should handle bash command with leading whitespace", async () => { | |
| 121 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 122 | + mockRun.mockResolvedValue("echo test output"); | |
| 123 | + | |
| 124 | + const result = await handleBashModeProcessing(" !echo hello"); | |
| 125 | + | |
| 126 | + expect(result).toBe(null); // Processed, no further handling needed | |
| 127 | + expect(services.chatHistory.addAssistantMessage).toHaveBeenCalled(); | |
| 128 | + | |
| 129 | + // Use vi.waitFor to wait for async operations in the void expression | |
| 130 | + await vi.waitFor( | |
| 131 | + () => { | |
| 132 | + expect(mockRun).toHaveBeenCalledWith({ command: "echo hello" }); | |
| 133 | + }, | |
| 134 | + { timeout: 1000 }, | |
| 135 | + ); | |
| 136 | + | |
| 137 | + await vi.waitFor( | |
| 138 | + () => { | |
| 139 | + expect(services.chatHistory.addToolResult).toHaveBeenCalledWith( | |
| 140 | + expect.any(String), | |
| 141 | + "echo test output", | |
| 142 | + "done", | |
| 143 | + ); | |
| 144 | + }, | |
| 145 | + { timeout: 1000 }, | |
| 146 | + ); | |
| 147 | + }); | |
| 148 | +}); |
added extensions/cli/src/ui/hooks/useChat.shellMode.ts +51 −0
| @@ -0,0 +1,51 @@ | ||
| 1 | +import { runTerminalCommandTool } from "src/tools/runTerminalCommand.js"; | |
| 2 | +import { getErrorString } from "src/util/error.js"; | |
| 3 | + | |
| 4 | +import { services } from "../../services/index.js"; | |
| 5 | + | |
| 6 | +export const handleBashModeProcessing = async ( | |
| 7 | + message: string, | |
| 8 | +): Promise<string | null> => { | |
| 9 | + // Handle shell mode commands (starting with !) | |
| 10 | + if (!message.trimStart().startsWith("!")) { | |
| 11 | + return message; | |
| 12 | + } | |
| 13 | + | |
| 14 | + // Extract the command after the ! | |
| 15 | + const bashCommand = message.trimStart().slice(1).trim(); | |
| 16 | + | |
| 17 | + if (!bashCommand) { | |
| 18 | + return message; // No command provided, treat as regular message | |
| 19 | + } | |
| 20 | + | |
| 21 | + // Show tool call immediately, then run asynchronously and populate result | |
| 22 | + const toolCallId = `shell-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; | |
| 23 | + const toolCall = { | |
| 24 | + id: toolCallId, | |
| 25 | + type: "function" as const, | |
| 26 | + function: { | |
| 27 | + name: "Bash", | |
| 28 | + arguments: JSON.stringify({ command: bashCommand }), | |
| 29 | + }, | |
| 30 | + }; | |
| 31 | + | |
| 32 | + // Add assistant message with tool call immediately | |
| 33 | + services.chatHistory.addAssistantMessage("", [toolCall]); | |
| 34 | + // Mark as calling for immediate UI feedback | |
| 35 | + services.chatHistory.updateToolStatus(toolCallId, "calling"); | |
| 36 | + | |
| 37 | + // Execute the bash command asynchronously - don't block UI | |
| 38 | + void runTerminalCommandTool | |
| 39 | + .run({ | |
| 40 | + command: bashCommand, | |
| 41 | + }) | |
| 42 | + .then((result) => { | |
| 43 | + services.chatHistory.addToolResult(toolCallId, result, "done"); | |
| 44 | + }) | |
| 45 | + .catch((error) => { | |
| 46 | + const errorMessage = `Bash command failed: ${getErrorString(error)}`; | |
| 47 | + services.chatHistory.addToolResult(toolCallId, errorMessage, "errored"); | |
| 48 | + }); | |
| 49 | + | |
| 50 | + return null; // Processed, no further message handling needed | |
| 51 | +}; |
modified extensions/cli/src/ui/hooks/useChat.ts +8 −0
| @@ -30,6 +30,7 @@ | ||
| 30 | 30 | handleRemoteMessage, |
| 31 | 31 | setupRemotePolling, |
| 32 | 32 | } from "./useChat.remote.helpers.js"; |
| 33 | +import { handleBashModeProcessing } from "./useChat.shellMode.js"; | |
| 33 | 34 | import { |
| 34 | 35 | createStreamCallbacks, |
| 35 | 36 | executeStreaming, |
| @@ -423,6 +424,13 @@ | ||
| 423 | 424 | |
| 424 | 425 | if (handled) return; |
| 425 | 426 | |
| 427 | + // Handle shell mode commands (before slash commands) | |
| 428 | + const bashProcessedMessage = await handleBashModeProcessing(message); | |
| 429 | + if (bashProcessedMessage === null) { | |
| 430 | + return; // Bash command was handled and no further processing needed | |
| 431 | + } | |
| 432 | + message = bashProcessedMessage; | |
| 433 | + | |
| 426 | 434 | // Handle slash commands |
| 427 | 435 | const processedMessage = await handleSlashCommandProcessing(message); |
| 428 | 436 | if (processedMessage === null) { |
modified extensions/cli/src/ui/hooks/useUserInput.ts +3 −0
| @@ -104,6 +104,7 @@ | ||
| 104 | 104 | setCursorPosition: (pos: number) => void; |
| 105 | 105 | updateSlashCommandState: (text: string, cursor: number) => void; |
| 106 | 106 | updateFileSearchState: (text: string, cursor: number) => void; |
| 107 | + updateBashModeState: (text: string) => void; | |
| 107 | 108 | inputHistory: any; |
| 108 | 109 | } |
| 109 | 110 | |
| @@ -115,6 +116,7 @@ | ||
| 115 | 116 | setCursorPosition, |
| 116 | 117 | updateSlashCommandState, |
| 117 | 118 | updateFileSearchState, |
| 119 | + updateBashModeState, | |
| 118 | 120 | inputHistory, |
| 119 | 121 | } = options; |
| 120 | 122 | |
| @@ -126,6 +128,7 @@ | ||
| 126 | 128 | setCursorPosition(newCursor); |
| 127 | 129 | updateSlashCommandState(newText, newCursor); |
| 128 | 130 | updateFileSearchState(newText, newCursor); |
| 131 | + updateBashModeState(newText); | |
| 129 | 132 | inputHistory.resetNavigation(); |
| 130 | 133 | } |
| 131 | 134 | } |