|
| 1 | +import { TerminalRegistry } from "../../integrations/terminal/TerminalRegistry" |
| 2 | +import { Terminal } from "../../integrations/terminal/Terminal" |
| 3 | +import { formatResponse } from "../prompts/responses" |
| 4 | +import { Task } from "../task/Task" |
| 5 | +import { ToolUse, AskApproval, HandleError, PushToolResult, RemoveClosingTag } from "../../shared/tools" |
| 6 | + |
| 7 | +export async function terminalCtrlTool( |
| 8 | + task: Task, |
| 9 | + block: ToolUse, |
| 10 | + askApproval: AskApproval, |
| 11 | + handleError: HandleError, |
| 12 | + pushToolResult: PushToolResult, |
| 13 | + removeClosingTag: RemoveClosingTag, |
| 14 | +) { |
| 15 | + const action: string | undefined = block.params.action |
| 16 | + const terminalId: string | undefined = block.params.terminal_id |
| 17 | + |
| 18 | + try { |
| 19 | + if (block.partial) { |
| 20 | + await task.ask("command", removeClosingTag("action", action), block.partial).catch(() => {}) |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + if (!action) { |
| 25 | + task.consecutiveMistakeCount++ |
| 26 | + task.recordToolError("terminal_ctrl") |
| 27 | + pushToolResult(await task.sayAndCreateMissingParamError("terminal_ctrl", "action")) |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + if (!terminalId) { |
| 32 | + task.consecutiveMistakeCount++ |
| 33 | + task.recordToolError("terminal_ctrl") |
| 34 | + pushToolResult(await task.sayAndCreateMissingParamError("terminal_ctrl", "terminal_id")) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + // Only support kill action for MVP |
| 39 | + if (action !== "kill") { |
| 40 | + task.consecutiveMistakeCount++ |
| 41 | + task.recordToolError("terminal_ctrl") |
| 42 | + pushToolResult(formatResponse.toolError(`Invalid action "${action}". Only "kill" is supported.`)) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + // Get approval for the action |
| 47 | + const didApprove = await askApproval("command", `Kill process in terminal ${terminalId}`) |
| 48 | + if (!didApprove) { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const result = await killTerminalProcess(parseInt(terminalId)) |
| 54 | + pushToolResult(formatResponse.toolResult(result)) |
| 55 | + } catch (error) { |
| 56 | + await handleError("killing terminal process", error) |
| 57 | + } |
| 58 | + } catch (error) { |
| 59 | + await handleError("terminal control operation", error) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Kills a process running in a specific terminal by sending Ctrl+C |
| 65 | + * @param terminalId The terminal ID containing the process to kill |
| 66 | + * @returns Promise<string> Result message |
| 67 | + */ |
| 68 | +async function killTerminalProcess(terminalId: number): Promise<string> { |
| 69 | + const targetTerminal = findTerminal(terminalId) |
| 70 | + if (!targetTerminal) { |
| 71 | + return getTerminalNotFoundMessage(terminalId) |
| 72 | + } |
| 73 | + |
| 74 | + if (!targetTerminal.busy && !targetTerminal.process) { |
| 75 | + return `Terminal ${terminalId} is not running any process.` |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + if (targetTerminal instanceof Terminal) { |
| 80 | + // For VSCode terminals, send Ctrl+C |
| 81 | + targetTerminal.terminal.sendText("\x03") |
| 82 | + return `Sent Ctrl+C to terminal ${terminalId}. Process should terminate shortly.` |
| 83 | + } else { |
| 84 | + // For ExecaTerminal, use the abort method |
| 85 | + if (targetTerminal.process) { |
| 86 | + targetTerminal.process.abort() |
| 87 | + return `Terminated process in terminal ${terminalId}.` |
| 88 | + } else { |
| 89 | + return `No active process found in terminal ${terminalId}.` |
| 90 | + } |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + throw new Error( |
| 94 | + `Failed to kill process in terminal ${terminalId}: ${error instanceof Error ? error.message : String(error)}`, |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Helper function to find a terminal by ID |
| 101 | + */ |
| 102 | +function findTerminal(terminalId: number) { |
| 103 | + const busyTerminals = TerminalRegistry.getTerminals(true) |
| 104 | + const allTerminals = TerminalRegistry.getTerminals(false) |
| 105 | + const allTerminalsList = [...busyTerminals, ...allTerminals] |
| 106 | + |
| 107 | + return allTerminalsList.find((t) => t.id === terminalId) |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Helper function to get terminal not found message |
| 112 | + */ |
| 113 | +function getTerminalNotFoundMessage(terminalId: number): string { |
| 114 | + const busyTerminals = TerminalRegistry.getTerminals(true) |
| 115 | + const allTerminals = TerminalRegistry.getTerminals(false) |
| 116 | + const allTerminalsList = [...busyTerminals, ...allTerminals] |
| 117 | + |
| 118 | + return `Terminal ${terminalId} not found. Available terminals: ${allTerminalsList.map((t) => t.id).join(", ")}` |
| 119 | +} |
0 commit comments