Changesets · Branches · Tags · Compare
| Changeset | 475fa2c6bca873cff660732e93ba1b892320fc9e74edc2d977c56fb169a3d83c |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-09-02 21:07 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-09-02 21:07 -07:00 |
| Parents | 88f9397dc3b6 |
10 files changed
deleted extensions/cli/spec/bash-mode.md +0 −75
| @@ -1,75 +0,0 @@ | ||
| 1 | -Bash Mode (CLI TUI) | |
| 2 | - | |
| 3 | -Overview | |
| 4 | - | |
| 5 | -- Bash 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 | -- Bash mode is activated when the current input (trimmed) starts with ! | |
| 11 | - - Example: "!git status" or " !ls -la" both activate Bash 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 bash mode". | |
| 16 | - | |
| 17 | -Deactivation / Exiting Bash Mode | |
| 18 | - | |
| 19 | -- Pressing Enter to submit the input exits bash mode immediately after submission, regardless of the command result. | |
| 20 | -- Pressing Esc when the input (trimmed) is exactly ! clears the input and exits bash mode. | |
| 21 | -- Editing the input so it no longer starts with ! also exits bash mode and restores normal input behavior. | |
| 22 | - | |
| 23 | -Interaction with other input helpers | |
| 24 | - | |
| 25 | -- When in bash 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 bash-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 Bash tool call, with status set to calling, so users can see that the command is in progress. | |
| 36 | - - The bash 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 from the current working directory of the CLI process. | |
| 41 | -- The command runs in the user’s shell as a login shell: | |
| 42 | - - Unix/macOS: SHELL (or /bin/bash) invoked with -l -c "<command>". | |
| 43 | - - Windows: powershell.exe -NoLogo -ExecutionPolicy Bypass -Command "<command>". | |
| 44 | -- The child process stdin is detached (not connected to the TTY). This prevents the TUI from being suspended by job control. Consequences: | |
| 45 | - - Interactive commands that require stdin will not work. | |
| 46 | -- Environment variables are inherited from the parent process. Aliases/functions availability depends on the user’s shell configuration and is not guaranteed. | |
| 47 | - | |
| 48 | -Output handling | |
| 49 | - | |
| 50 | -- Stdout is streamed into memory; Stderr is captured and appended as a trailing "Stderr: ..." section on success. | |
| 51 | -- If the process exits non-zero and Stderr contains content, the tool call is marked as error and the error text is shown. | |
| 52 | -- Output is truncated to the first 5000 lines if exceeded. | |
| 53 | -- Timeout behavior: If no output is received for 30 seconds (configurable in tests), the process is terminated and the result includes a note like: | |
| 54 | - "[Command timed out after 30 seconds of no output]". | |
| 55 | - | |
| 56 | -Keyboard behaviors (summary) | |
| 57 | - | |
| 58 | -- Enter: submit input. If in bash mode, exits bash mode after submission and shows the pending Bash tool call immediately. | |
| 59 | -- Shift+Enter: new line. | |
| 60 | -- Backslash (\) at end-of-line: inserts a new line (line continuation) as usual. | |
| 61 | -- Esc: if only ! (trimmed) is present, clears input and exits bash mode; otherwise cancels streaming or closes suggestions depending on context. | |
| 62 | - | |
| 63 | -Scope / Modes | |
| 64 | - | |
| 65 | -- Bash mode applies to interactive (TUI/standard) CLI usage. It is not part of headless (-p/--print) processing. | |
| 66 | - | |
| 67 | -Error handling | |
| 68 | - | |
| 69 | -- Command execution errors are captured and surfaced in the tool call as status error with human-readable error text (including Stderr when available). | |
| 70 | - | |
| 71 | -Examples | |
| 72 | - | |
| 73 | -- "!git status" → shows a Bash tool call immediately, then populates with the git status output. | |
| 74 | -- "!echo hello" → shows a Bash tool call immediately, then output "hello". | |
| 75 | -- "!some-unknown-cmd" → shows a Bash tool call immediately, then sets status to error with an error message. |
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 +1 −1
| @@ -28,7 +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("!")} Bash mode - run shell commands`, | |
| 31 | + ` ${chalk.cyan("!")} Shell mode - run shell commands`, | |
| 32 | 32 | "", |
| 33 | 33 | chalk.white("Controls:"), |
| 34 | 34 | ` ${chalk.cyan("Ctrl+C")} Clear input`, |
modified extensions/cli/src/tools/runTerminalCommand.ts +2 −7
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | args: ["-NoLogo", "-ExecutionPolicy", "Bypass", "-Command", command], |
| 18 | 18 | }; |
| 19 | 19 | } else { |
| 20 | - // Unix/macOS: Just run the user's shell as a login shell | |
| 20 | + // Unix/macOS: Use login shell to source .bashrc/.zshrc etc. | |
| 21 | 21 | const userShell = process.env.SHELL || "/bin/bash"; |
| 22 | 22 | return { shell: userShell, args: ["-l", "-c", command] }; |
| 23 | 23 | } |
| @@ -60,12 +60,7 @@ | ||
| 60 | 60 | return new Promise((resolve, reject) => { |
| 61 | 61 | // Use same shell logic as core implementation |
| 62 | 62 | const { shell, args } = getShellCommand(command); |
| 63 | - const child = spawn(shell, args, { | |
| 64 | - cwd: process.cwd(), | |
| 65 | - env: process.env, | |
| 66 | - stdio: ["ignore", "pipe", "pipe"], // prevent child from reading TTY input | |
| 67 | - detached: false, | |
| 68 | - }); | |
| 63 | + const child = spawn(shell, args); | |
| 69 | 64 | let stdout = ""; |
| 70 | 65 | let stderr = ""; |
| 71 | 66 | let timeoutId: NodeJS.Timeout; |
modified extensions/cli/src/ui/UserInput.tsx +6 −6
| @@ -190,13 +190,13 @@ | ||
| 190 | 190 | return nextMode; |
| 191 | 191 | }; |
| 192 | 192 | |
| 193 | - // Update bash mode state based on input | |
| 193 | + // Update shell mode state based on input | |
| 194 | 194 | const updateBashModeState = (text: string) => { |
| 195 | 195 | const trimmed = text.trimStart(); |
| 196 | 196 | const isBash = trimmed.startsWith("!"); |
| 197 | 197 | setShowBashMode(isBash); |
| 198 | 198 | if (isBash) { |
| 199 | - // Disable other helpers while in bash mode | |
| 199 | + // Disable other helpers while in shell mode | |
| 200 | 200 | setShowSlashCommands(false); |
| 201 | 201 | setShowFileSearch(false); |
| 202 | 202 | } |
| @@ -275,7 +275,7 @@ | ||
| 275 | 275 | return; |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | - // Disable file search if we're in bash mode or slash command mode | |
| 278 | + // Disable file search if we're in shell mode or slash command mode | |
| 279 | 279 | const trimmed = text.trimStart(); |
| 280 | 280 | if (trimmed.startsWith("!") || trimmed.startsWith("/")) { |
| 281 | 281 | setShowFileSearch(false); |
| @@ -551,7 +551,7 @@ | ||
| 551 | 551 | const handleEscapeKey = (key: any): boolean => { |
| 552 | 552 | if (!key.escape) return false; |
| 553 | 553 | |
| 554 | - // If only "!" is present, clear bash mode | |
| 554 | + // If only "!" is present, clear shell mode | |
| 555 | 555 | if (inputMode && showBashMode && inputText.trim() === "!") { |
| 556 | 556 | textBuffer.clear(); |
| 557 | 557 | setInputText(""); |
| @@ -688,9 +688,9 @@ | ||
| 688 | 688 | |
| 689 | 689 | const renderInputText = () => { |
| 690 | 690 | const placeholderText = isRemoteMode |
| 691 | - ? "Ask anything, / for slash commands, ! for bash mode" | |
| 691 | + ? "Ask anything, / for slash commands, ! for shell mode" | |
| 692 | 692 | : placeholder || |
| 693 | - "Ask anything, @ for context, / for slash commands, ! for bash mode"; | |
| 693 | + "Ask anything, @ for context, / for slash commands, ! for shell mode"; | |
| 694 | 694 | if (inputText.length === 0) { |
| 695 | 695 | return ( |
| 696 | 696 | <> |
deleted extensions/cli/src/ui/hooks/useChat.bashMode.test.ts +0 −110
| @@ -1,110 +0,0 @@ | ||
| 1 | -import { act } from "@testing-library/react"; | |
| 2 | -import { describe, it, expect, vi, beforeEach } from "vitest"; | |
| 3 | - | |
| 4 | -import { createMinimalTestContext } from "../../test-helpers/ui-test-context.js"; | |
| 5 | -import { runTerminalCommandTool } from "../../tools/runTerminalCommand.js"; | |
| 6 | - | |
| 7 | -import { useChat } from "./useChat.js"; | |
| 8 | - | |
| 9 | -// Mock the runTerminalCommandTool | |
| 10 | -vi.mock("../../tools/runTerminalCommand.js", () => ({ | |
| 11 | - runTerminalCommandTool: { | |
| 12 | - run: vi.fn(), | |
| 13 | - }, | |
| 14 | -})); | |
| 15 | - | |
| 16 | -describe("useChat - Bash Mode", () => { | |
| 17 | - beforeEach(() => { | |
| 18 | - vi.clearAllMocks(); | |
| 19 | - }); | |
| 20 | - | |
| 21 | - it("should detect bash mode when message starts with !", async () => { | |
| 22 | - const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 23 | - mockRun.mockResolvedValue( | |
| 24 | - "total 5\ndrwxr-xr-x 2 user user 4096 Jan 1 12:00 .\ndrwxr-xr-x 3 user user 4096 Jan 1 12:00 ..", | |
| 25 | - ); | |
| 26 | - | |
| 27 | - const ctx = createMinimalTestContext(); | |
| 28 | - const chat = useChat({ | |
| 29 | - assistant: { name: "test" } as any, | |
| 30 | - model: { name: "test-model" } as any, | |
| 31 | - llmApi: {} as any, | |
| 32 | - } as any); | |
| 33 | - | |
| 34 | - await act(async () => { | |
| 35 | - await chat.handleUserMessage("!ls -la"); | |
| 36 | - }); | |
| 37 | - | |
| 38 | - expect(mockRun).toHaveBeenCalledWith({ command: "ls -la" }); | |
| 39 | - | |
| 40 | - // Since history is managed by service, we just verify no exception and tool called | |
| 41 | - ctx.cleanup(); | |
| 42 | - }); | |
| 43 | - | |
| 44 | - it("should handle bash command errors", async () => { | |
| 45 | - const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 46 | - mockRun.mockRejectedValue(new Error("Command not found")); | |
| 47 | - | |
| 48 | - const chat = useChat({ | |
| 49 | - assistant: { name: "test" } as any, | |
| 50 | - model: { name: "test-model" } as any, | |
| 51 | - llmApi: {} as any, | |
| 52 | - } as any); | |
| 53 | - | |
| 54 | - await act(async () => { | |
| 55 | - await chat.handleUserMessage("!invalidcommand"); | |
| 56 | - }); | |
| 57 | - | |
| 58 | - expect(mockRun).toHaveBeenCalledWith({ command: "invalidcommand" }); | |
| 59 | - }); | |
| 60 | - | |
| 61 | - it("should not trigger bash mode for regular messages", async () => { | |
| 62 | - const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 63 | - | |
| 64 | - const chat = useChat({ | |
| 65 | - assistant: { name: "test" } as any, | |
| 66 | - model: { name: "test-model" } as any, | |
| 67 | - llmApi: {} as any, | |
| 68 | - } as any); | |
| 69 | - | |
| 70 | - await act(async () => { | |
| 71 | - await chat.handleUserMessage("Hello! How are you?"); | |
| 72 | - }); | |
| 73 | - | |
| 74 | - expect(mockRun).not.toHaveBeenCalled(); | |
| 75 | - }); | |
| 76 | - | |
| 77 | - it("should handle empty bash command", async () => { | |
| 78 | - const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 79 | - | |
| 80 | - const chat = useChat({ | |
| 81 | - assistant: { name: "test" } as any, | |
| 82 | - model: { name: "test-model" } as any, | |
| 83 | - llmApi: {} as any, | |
| 84 | - } as any); | |
| 85 | - | |
| 86 | - await act(async () => { | |
| 87 | - await chat.handleUserMessage("!"); | |
| 88 | - }); | |
| 89 | - | |
| 90 | - // Should not call the tool for empty command | |
| 91 | - expect(mockRun).not.toHaveBeenCalled(); | |
| 92 | - }); | |
| 93 | - | |
| 94 | - it("should handle bash command with leading whitespace", async () => { | |
| 95 | - const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 96 | - mockRun.mockResolvedValue("echo test output"); | |
| 97 | - | |
| 98 | - const chat = useChat({ | |
| 99 | - assistant: { name: "test" } as any, | |
| 100 | - model: { name: "test-model" } as any, | |
| 101 | - llmApi: {} as any, | |
| 102 | - } as any); | |
| 103 | - | |
| 104 | - await act(async () => { | |
| 105 | - await chat.handleUserMessage(" !echo hello"); | |
| 106 | - }); | |
| 107 | - | |
| 108 | - expect(mockRun).toHaveBeenCalledWith({ command: "echo hello" }); | |
| 109 | - }); | |
| 110 | -}); |
deleted extensions/cli/src/ui/hooks/useChat.bashMode.ts +0 −51
| @@ -1,51 +0,0 @@ | ||
| 1 | -import { services } from "../../services/index.js"; | |
| 2 | - | |
| 3 | -export const handleBashModeProcessing = async ( | |
| 4 | - message: string, | |
| 5 | -): Promise<string | null> => { | |
| 6 | - // Handle bash mode commands (starting with !) | |
| 7 | - if (!message.trimStart().startsWith("!")) { | |
| 8 | - return message; | |
| 9 | - } | |
| 10 | - | |
| 11 | - // Extract the command after the ! | |
| 12 | - const bashCommand = message.trimStart().slice(1).trim(); | |
| 13 | - | |
| 14 | - if (!bashCommand) { | |
| 15 | - return message; // No command provided, treat as regular message | |
| 16 | - } | |
| 17 | - | |
| 18 | - // Show tool call immediately, then run asynchronously and populate result | |
| 19 | - const toolCallId = `bash-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; | |
| 20 | - const toolCall = { | |
| 21 | - id: toolCallId, | |
| 22 | - type: "function" as const, | |
| 23 | - function: { | |
| 24 | - name: "Bash", | |
| 25 | - arguments: JSON.stringify({ command: bashCommand }), | |
| 26 | - }, | |
| 27 | - }; | |
| 28 | - | |
| 29 | - // Add assistant message with tool call immediately | |
| 30 | - services.chatHistory.addAssistantMessage("", [toolCall as any]); | |
| 31 | - // Mark as calling for immediate UI feedback | |
| 32 | - services.chatHistory.updateToolStatus(toolCallId, "calling"); | |
| 33 | - | |
| 34 | - // Execute the bash command asynchronously - don't block UI | |
| 35 | - (async () => { | |
| 36 | - try { | |
| 37 | - const { runTerminalCommandTool } = await import( | |
| 38 | - "../../tools/runTerminalCommand.js" | |
| 39 | - ); | |
| 40 | - const result = await runTerminalCommandTool.run({ | |
| 41 | - command: bashCommand, | |
| 42 | - }); | |
| 43 | - services.chatHistory.addToolResult(toolCallId, result, "done"); | |
| 44 | - } catch (error: any) { | |
| 45 | - const errorMessage = `Bash command failed: ${error?.message || String(error)}`; | |
| 46 | - services.chatHistory.addToolResult(toolCallId, errorMessage, "errored"); | |
| 47 | - } | |
| 48 | - })(); | |
| 49 | - | |
| 50 | - return null; // Processed, no further message handling needed | |
| 51 | -}; |
added extensions/cli/src/ui/hooks/useChat.shellMode.test.ts +110 −0
| @@ -0,0 +1,110 @@ | ||
| 1 | +import { act } from "@testing-library/react"; | |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; | |
| 3 | + | |
| 4 | +import { createMinimalTestContext } from "../../test-helpers/ui-test-context.js"; | |
| 5 | +import { runTerminalCommandTool } from "../../tools/runTerminalCommand.js"; | |
| 6 | + | |
| 7 | +import { useChat } from "./useChat.js"; | |
| 8 | + | |
| 9 | +// Mock the runTerminalCommandTool | |
| 10 | +vi.mock("../../tools/runTerminalCommand.js", () => ({ | |
| 11 | + runTerminalCommandTool: { | |
| 12 | + run: vi.fn(), | |
| 13 | + }, | |
| 14 | +})); | |
| 15 | + | |
| 16 | +describe("useChat - Bash Mode", () => { | |
| 17 | + beforeEach(() => { | |
| 18 | + vi.clearAllMocks(); | |
| 19 | + }); | |
| 20 | + | |
| 21 | + it("should detect bash mode when message starts with !", async () => { | |
| 22 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 23 | + mockRun.mockResolvedValue( | |
| 24 | + "total 5\ndrwxr-xr-x 2 user user 4096 Jan 1 12:00 .\ndrwxr-xr-x 3 user user 4096 Jan 1 12:00 ..", | |
| 25 | + ); | |
| 26 | + | |
| 27 | + const ctx = createMinimalTestContext(); | |
| 28 | + const chat = useChat({ | |
| 29 | + assistant: { name: "test" } as any, | |
| 30 | + model: { name: "test-model" } as any, | |
| 31 | + llmApi: {} as any, | |
| 32 | + } as any); | |
| 33 | + | |
| 34 | + await act(async () => { | |
| 35 | + await chat.handleUserMessage("!ls -la"); | |
| 36 | + }); | |
| 37 | + | |
| 38 | + expect(mockRun).toHaveBeenCalledWith({ command: "ls -la" }); | |
| 39 | + | |
| 40 | + // Since history is managed by service, we just verify no exception and tool called | |
| 41 | + ctx.cleanup(); | |
| 42 | + }); | |
| 43 | + | |
| 44 | + it("should handle bash command errors", async () => { | |
| 45 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 46 | + mockRun.mockRejectedValue(new Error("Command not found")); | |
| 47 | + | |
| 48 | + const chat = useChat({ | |
| 49 | + assistant: { name: "test" } as any, | |
| 50 | + model: { name: "test-model" } as any, | |
| 51 | + llmApi: {} as any, | |
| 52 | + } as any); | |
| 53 | + | |
| 54 | + await act(async () => { | |
| 55 | + await chat.handleUserMessage("!invalidcommand"); | |
| 56 | + }); | |
| 57 | + | |
| 58 | + expect(mockRun).toHaveBeenCalledWith({ command: "invalidcommand" }); | |
| 59 | + }); | |
| 60 | + | |
| 61 | + it("should not trigger bash mode for regular messages", async () => { | |
| 62 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 63 | + | |
| 64 | + const chat = useChat({ | |
| 65 | + assistant: { name: "test" } as any, | |
| 66 | + model: { name: "test-model" } as any, | |
| 67 | + llmApi: {} as any, | |
| 68 | + } as any); | |
| 69 | + | |
| 70 | + await act(async () => { | |
| 71 | + await chat.handleUserMessage("Hello! How are you?"); | |
| 72 | + }); | |
| 73 | + | |
| 74 | + expect(mockRun).not.toHaveBeenCalled(); | |
| 75 | + }); | |
| 76 | + | |
| 77 | + it("should handle empty bash command", async () => { | |
| 78 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 79 | + | |
| 80 | + const chat = useChat({ | |
| 81 | + assistant: { name: "test" } as any, | |
| 82 | + model: { name: "test-model" } as any, | |
| 83 | + llmApi: {} as any, | |
| 84 | + } as any); | |
| 85 | + | |
| 86 | + await act(async () => { | |
| 87 | + await chat.handleUserMessage("!"); | |
| 88 | + }); | |
| 89 | + | |
| 90 | + // Should not call the tool for empty command | |
| 91 | + expect(mockRun).not.toHaveBeenCalled(); | |
| 92 | + }); | |
| 93 | + | |
| 94 | + it("should handle bash command with leading whitespace", async () => { | |
| 95 | + const mockRun = vi.mocked(runTerminalCommandTool.run); | |
| 96 | + mockRun.mockResolvedValue("echo test output"); | |
| 97 | + | |
| 98 | + const chat = useChat({ | |
| 99 | + assistant: { name: "test" } as any, | |
| 100 | + model: { name: "test-model" } as any, | |
| 101 | + llmApi: {} as any, | |
| 102 | + } as any); | |
| 103 | + | |
| 104 | + await act(async () => { | |
| 105 | + await chat.handleUserMessage(" !echo hello"); | |
| 106 | + }); | |
| 107 | + | |
| 108 | + expect(mockRun).toHaveBeenCalledWith({ command: "echo hello" }); | |
| 109 | + }); | |
| 110 | +}); |
added extensions/cli/src/ui/hooks/useChat.shellMode.ts +50 −0
| @@ -0,0 +1,50 @@ | ||
| 1 | +import { runTerminalCommandTool } from "src/tools/runTerminalCommand.js"; | |
| 2 | +import { getErrorString } from "src/util/error.js"; | |
| 3 | +import { services } from "../../services/index.js"; | |
| 4 | + | |
| 5 | +export const handleBashModeProcessing = async ( | |
| 6 | + message: string, | |
| 7 | +): Promise<string | null> => { | |
| 8 | + // Handle shell mode commands (starting with !) | |
| 9 | + if (!message.trimStart().startsWith("!")) { | |
| 10 | + return message; | |
| 11 | + } | |
| 12 | + | |
| 13 | + // Extract the command after the ! | |
| 14 | + const bashCommand = message.trimStart().slice(1).trim(); | |
| 15 | + | |
| 16 | + if (!bashCommand) { | |
| 17 | + return message; // No command provided, treat as regular message | |
| 18 | + } | |
| 19 | + | |
| 20 | + // Show tool call immediately, then run asynchronously and populate result | |
| 21 | + const toolCallId = `shell-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; | |
| 22 | + const toolCall = { | |
| 23 | + id: toolCallId, | |
| 24 | + type: "function" as const, | |
| 25 | + function: { | |
| 26 | + name: "Bash", | |
| 27 | + arguments: JSON.stringify({ command: bashCommand }), | |
| 28 | + }, | |
| 29 | + }; | |
| 30 | + | |
| 31 | + // Add assistant message with tool call immediately | |
| 32 | + services.chatHistory.addAssistantMessage("", [toolCall]); | |
| 33 | + // Mark as calling for immediate UI feedback | |
| 34 | + services.chatHistory.updateToolStatus(toolCallId, "calling"); | |
| 35 | + | |
| 36 | + // Execute the bash command asynchronously - don't block UI | |
| 37 | + void runTerminalCommandTool | |
| 38 | + .run({ | |
| 39 | + command: bashCommand, | |
| 40 | + }) | |
| 41 | + .then((result) => { | |
| 42 | + services.chatHistory.addToolResult(toolCallId, result, "done"); | |
| 43 | + }) | |
| 44 | + .catch((error) => { | |
| 45 | + const errorMessage = `Bash command failed: ${getErrorString(error)}`; | |
| 46 | + services.chatHistory.addToolResult(toolCallId, errorMessage, "errored"); | |
| 47 | + }); | |
| 48 | + | |
| 49 | + return null; // Processed, no further message handling needed | |
| 50 | +}; |
modified extensions/cli/src/ui/hooks/useChat.ts +2 −2
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | import { formatError } from "../../util/formatError.js"; |
| 16 | 16 | import { logger } from "../../util/logger.js"; |
| 17 | 17 | |
| 18 | -import { handleBashModeProcessing } from "./useChat.bashMode.js"; | |
| 19 | 18 | import { |
| 20 | 19 | handleAutoCompaction, |
| 21 | 20 | handleCompactCommand, |
| @@ -31,6 +30,7 @@ | ||
| 31 | 30 | handleRemoteMessage, |
| 32 | 31 | setupRemotePolling, |
| 33 | 32 | } from "./useChat.remote.helpers.js"; |
| 33 | +import { handleBashModeProcessing } from "./useChat.shellMode.js"; | |
| 34 | 34 | import { |
| 35 | 35 | createStreamCallbacks, |
| 36 | 36 | executeStreaming, |
| @@ -424,7 +424,7 @@ | ||
| 424 | 424 | |
| 425 | 425 | if (handled) return; |
| 426 | 426 | |
| 427 | - // Handle bash mode commands (before slash commands) | |
| 427 | + // Handle shell mode commands (before slash commands) | |
| 428 | 428 | const bashProcessedMessage = await handleBashModeProcessing(message); |
| 429 | 429 | if (bashProcessedMessage === null) { |
| 430 | 430 | return; // Bash command was handled and no further processing needed |