| 1 | import { ChildProcess } from "child_process"; |
| 2 | |
| 3 | // Track which processes have been backgrounded |
| 4 | const processTerminalBackgroundStates = new Map<string, boolean>(); |
| 5 | |
| 6 | // Track which foreground processes are currently running |
| 7 | interface RunningProcessInfo { |
| 8 | process: ChildProcess; |
| 9 | onPartialOutput?: (params: { |
| 10 | toolCallId: string; |
| 11 | contextItems: any[]; |
| 12 | }) => void; |
| 13 | currentOutput: string; |
| 14 | } |
| 15 | |
| 16 | const processTerminalForegroundStates = new Map<string, RunningProcessInfo>(); |
| 17 | |
| 18 | // Background process functions (existing) |
| 19 | export function markProcessAsBackgrounded(toolCallId: string): void { |
| 20 | processTerminalBackgroundStates.set(toolCallId, true); |
| 21 | } |
| 22 | |
| 23 | export function isProcessBackgrounded(toolCallId: string): boolean { |
| 24 | return processTerminalBackgroundStates.has(toolCallId); |
| 25 | } |
| 26 | |
| 27 | export function removeBackgroundedProcess(toolCallId: string): void { |
| 28 | processTerminalBackgroundStates.delete(toolCallId); |
| 29 | } |
| 30 | |
| 31 | // Foreground process functions (new) |
| 32 | export function markProcessAsRunning( |
| 33 | toolCallId: string, |
| 34 | process: ChildProcess, |
| 35 | onPartialOutput?: (params: { |
| 36 | toolCallId: string; |
| 37 | contextItems: any[]; |
| 38 | }) => void, |
| 39 | currentOutput: string = "", |
| 40 | ): void { |
| 41 | processTerminalForegroundStates.set(toolCallId, { |
| 42 | process, |
| 43 | onPartialOutput, |
| 44 | currentOutput, |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | export function isProcessRunning(toolCallId: string): boolean { |
| 49 | return processTerminalForegroundStates.has(toolCallId); |
| 50 | } |
| 51 | |
| 52 | export function getRunningProcess( |
| 53 | toolCallId: string, |
| 54 | ): ChildProcess | undefined { |
| 55 | const info = processTerminalForegroundStates.get(toolCallId); |
| 56 | return info?.process; |
| 57 | } |
| 58 | |
| 59 | export function updateProcessOutput(toolCallId: string, output: string): void { |
| 60 | const info = processTerminalForegroundStates.get(toolCallId); |
| 61 | if (info) { |
| 62 | info.currentOutput = output; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | export function removeRunningProcess(toolCallId: string): void { |
| 67 | processTerminalForegroundStates.delete(toolCallId); |
| 68 | } |
| 69 | |
| 70 | export async function killTerminalProcess(toolCallId: string): Promise<void> { |
| 71 | const processInfo = processTerminalForegroundStates.get(toolCallId); |
| 72 | if (processInfo && !processInfo.process.killed) { |
| 73 | const { process } = processInfo; |
| 74 | |
| 75 | process.kill("SIGTERM"); |
| 76 | |
| 77 | // Force kill after 5 seconds if still running |
| 78 | setTimeout(() => { |
| 79 | if (!process.killed) { |
| 80 | process.kill("SIGKILL"); |
| 81 | } |
| 82 | }, 5000); |
| 83 | |
| 84 | processTerminalForegroundStates.delete(toolCallId); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Function to cancel multiple terminal commands at once |
| 89 | export async function killMultipleTerminalProcesses( |
| 90 | toolCallIds: string[], |
| 91 | ): Promise<void> { |
| 92 | const cancelPromises = toolCallIds.map((toolCallId) => |
| 93 | killTerminalProcess(toolCallId), |
| 94 | ); |
| 95 | await Promise.all(cancelPromises); |
| 96 | } |
| 97 | |
| 98 | // Function to cancel ALL currently running terminal commands |
| 99 | export async function killAllRunningTerminalProcesses(): Promise<string[]> { |
| 100 | const runningIds = getAllRunningProcessIds(); |
| 101 | if (runningIds.length > 0) { |
| 102 | await killMultipleTerminalProcesses(runningIds); |
| 103 | } |
| 104 | return runningIds; // Return the IDs that were cancelled |
| 105 | } |
| 106 | |
| 107 | // Utility functions |
| 108 | export function getAllRunningProcessIds(): string[] { |
| 109 | return Array.from(processTerminalForegroundStates.keys()); |
| 110 | } |
| 111 | |
| 112 | export function getAllBackgroundedProcessIds(): string[] { |
| 113 | return Array.from(processTerminalBackgroundStates.keys()); |
| 114 | } |
| 115 | |
| 116 | // Utility function for testing - clears all background process states |
| 117 | export function clearAllBackgroundProcesses(): void { |
| 118 | processTerminalBackgroundStates.clear(); |
| 119 | } |