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