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