Elffuss BraidHub

kikocis/continue public

3ff29c567dd0 · 3.4 KiBRawPermalink
1import { ChildProcess } from "child_process";
2
3// Track which processes have been backgrounded
4const processTerminalBackgroundStates = new Map<string, boolean>();
5
6// Track which foreground processes are currently running
7interface RunningProcessInfo {
8 process: ChildProcess;
9 onPartialOutput?: (params: {
10 toolCallId: string;
11 contextItems: any[];
12 }) => void;
13 currentOutput: string;
14}
15
16const processTerminalForegroundStates = new Map<string, RunningProcessInfo>();
17
18// Background process functions (existing)
19export function markProcessAsBackgrounded(toolCallId: string): void {
20 processTerminalBackgroundStates.set(toolCallId, true);
21}
22
23export function isProcessBackgrounded(toolCallId: string): boolean {
24 return processTerminalBackgroundStates.has(toolCallId);
25}
26
27export function removeBackgroundedProcess(toolCallId: string): void {
28 processTerminalBackgroundStates.delete(toolCallId);
29}
30
31// Foreground process functions (new)
32export 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
48export function isProcessRunning(toolCallId: string): boolean {
49 return processTerminalForegroundStates.has(toolCallId);
50}
51
52export function getRunningProcess(
53 toolCallId: string,
54): ChildProcess | undefined {
55 const info = processTerminalForegroundStates.get(toolCallId);
56 return info?.process;
57}
58
59export function updateProcessOutput(toolCallId: string, output: string): void {
60 const info = processTerminalForegroundStates.get(toolCallId);
61 if (info) {
62 info.currentOutput = output;
63 }
64}
65
66export function removeRunningProcess(toolCallId: string): void {
67 processTerminalForegroundStates.delete(toolCallId);
68}
69
70export 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
89export 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
99export 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
108export function getAllRunningProcessIds(): string[] {
109 return Array.from(processTerminalForegroundStates.keys());
110}
111
112export function getAllBackgroundedProcessIds(): string[] {
113 return Array.from(processTerminalBackgroundStates.keys());
114}
115
116// Utility function for testing - clears all background process states
117export function clearAllBackgroundProcesses(): void {
118 processTerminalBackgroundStates.clear();
119}