| 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 | }; |