|
| 1 | +import assert from 'assert' |
| 2 | +import { createWriteStream, mkdirSync, WriteStream } from 'fs' |
| 3 | +import * as os from 'os' |
| 4 | +import path, { dirname } from 'path' |
| 5 | + |
| 6 | +import { stripColors } from 'common/util/string' |
| 7 | +import { green } from 'picocolors' |
| 8 | + |
| 9 | +import { |
| 10 | + backgroundProcesses, |
| 11 | + BackgroundProcessInfo, |
| 12 | + spawnAndTrack, |
| 13 | +} from '../background-process-manager' |
| 14 | + |
| 15 | +export function runBackgroundCommand( |
| 16 | + options: { |
| 17 | + toolCallId: string |
| 18 | + command: string |
| 19 | + mode: 'user' | 'assistant' | 'manager' |
| 20 | + cwd: string |
| 21 | + stdoutFile?: string |
| 22 | + stderrFile?: string |
| 23 | + }, |
| 24 | + resolveCommand: (value: { |
| 25 | + result: string |
| 26 | + stdout: string |
| 27 | + exitCode: number | null |
| 28 | + }) => void |
| 29 | +): void { |
| 30 | + const { toolCallId, command, mode, cwd, stdoutFile, stderrFile } = options |
| 31 | + const isWindows = os.platform() === 'win32' |
| 32 | + const shell = isWindows ? 'cmd.exe' : 'bash' |
| 33 | + const shellArgs = isWindows ? ['/c'] : ['-c'] |
| 34 | + |
| 35 | + if (mode === 'assistant') { |
| 36 | + console.log(green(`Running background process...\n> ${command}`)) |
| 37 | + } |
| 38 | + |
| 39 | + const initialStdout = '' |
| 40 | + const initialStderr = '' |
| 41 | + |
| 42 | + try { |
| 43 | + const childProcess = spawnAndTrack(shell, [...shellArgs, command], { |
| 44 | + cwd, |
| 45 | + env: { ...process.env, FORCE_COLOR: '1' }, |
| 46 | + // Ensure detached is always false to link child lifetime to parent |
| 47 | + detached: false, |
| 48 | + stdio: 'pipe', |
| 49 | + }) |
| 50 | + |
| 51 | + // An error should have been thrown when we called `spawn` |
| 52 | + assert( |
| 53 | + childProcess.pid !== undefined, |
| 54 | + 'Failed to spawn process: no PID assigned.' |
| 55 | + ) |
| 56 | + |
| 57 | + const processId = childProcess.pid |
| 58 | + const processInfo: BackgroundProcessInfo = { |
| 59 | + pid: processId, |
| 60 | + toolCallId, |
| 61 | + command, |
| 62 | + process: childProcess, |
| 63 | + stdoutBuffer: [], |
| 64 | + stderrBuffer: [], |
| 65 | + status: 'running', |
| 66 | + startTime: Date.now(), |
| 67 | + endTime: null, |
| 68 | + lastReportedStdoutLength: 0, |
| 69 | + lastReportedStderrLength: 0, |
| 70 | + lastReportedStatus: null, |
| 71 | + stdoutFile, |
| 72 | + stderrFile, |
| 73 | + } |
| 74 | + backgroundProcesses.set(processId, processInfo) |
| 75 | + |
| 76 | + // Set up file streams if paths are provided |
| 77 | + let stdoutStream: WriteStream | undefined |
| 78 | + let stderrStream: WriteStream | undefined |
| 79 | + |
| 80 | + if (stdoutFile) { |
| 81 | + const stdoutAbs = path.isAbsolute(stdoutFile) |
| 82 | + ? stdoutFile |
| 83 | + : path.join(cwd, stdoutFile) |
| 84 | + mkdirSync(dirname(stdoutAbs), { recursive: true }) |
| 85 | + stdoutStream = createWriteStream(stdoutAbs) |
| 86 | + } |
| 87 | + |
| 88 | + const realStderrFile = stderrFile || stdoutFile |
| 89 | + if (realStderrFile) { |
| 90 | + const stderrAbs = path.isAbsolute(realStderrFile) |
| 91 | + ? realStderrFile |
| 92 | + : path.join(cwd, realStderrFile) |
| 93 | + mkdirSync(dirname(stderrAbs), { recursive: true }) |
| 94 | + stderrStream = createWriteStream(stderrAbs) |
| 95 | + } |
| 96 | + |
| 97 | + childProcess.stdout.on('data', (data: Buffer) => { |
| 98 | + const output = stripColors(data.toString()) |
| 99 | + processInfo.stdoutBuffer.push(output) |
| 100 | + |
| 101 | + // Write to file if stream exists |
| 102 | + if (stdoutStream) { |
| 103 | + stdoutStream.write(output) |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + childProcess.stderr.on('data', (data: Buffer) => { |
| 108 | + const output = stripColors(data.toString()) |
| 109 | + processInfo.stderrBuffer.push(output) |
| 110 | + |
| 111 | + // Write to file if stream exists |
| 112 | + if (stderrStream) { |
| 113 | + stderrStream.write(output) |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + childProcess.on('error', (error) => { |
| 118 | + processInfo.status = 'error' |
| 119 | + processInfo.stderrBuffer.push( |
| 120 | + `\nError spawning command: ${error.message}` |
| 121 | + ) |
| 122 | + processInfo.endTime = Date.now() |
| 123 | + |
| 124 | + // Close file streams |
| 125 | + stdoutStream?.end() |
| 126 | + stderrStream?.end() |
| 127 | + }) |
| 128 | + |
| 129 | + let exitCode = null |
| 130 | + |
| 131 | + childProcess.on('close', (code) => { |
| 132 | + exitCode = code |
| 133 | + processInfo.status = code === 0 ? 'completed' : 'error' |
| 134 | + processInfo.endTime = Date.now() |
| 135 | + |
| 136 | + // Close file streams |
| 137 | + stdoutStream?.end() |
| 138 | + stderrStream?.end() |
| 139 | + }) |
| 140 | + |
| 141 | + // Unreference the process so the parent can exit independently IF the child is the only thing keeping it alive. |
| 142 | + childProcess.unref() |
| 143 | + |
| 144 | + const resultMessage = `<background_process> |
| 145 | +<process_id>${processId}</process_id> |
| 146 | +<command>${command}</command> |
| 147 | +<status>${processInfo.status}</status> |
| 148 | +</background_process>` |
| 149 | + resolveCommand({ |
| 150 | + result: resultMessage, |
| 151 | + stdout: initialStdout + initialStderr, |
| 152 | + exitCode, |
| 153 | + }) |
| 154 | + } catch (error: any) { |
| 155 | + const errorMessage = `<background_process>\n<command>${command}</command>\n<error>${error.message}</error>\n</background_process>` |
| 156 | + resolveCommand({ |
| 157 | + result: errorMessage, |
| 158 | + stdout: error.message, |
| 159 | + exitCode: null, |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
0 commit comments