| 1 | import childProcess from "node:child_process"; |
| 2 | import util from "node:util"; |
| 3 | |
| 4 | import { fileURLToPath } from "node:url"; |
| 5 | import { ToolImpl } from "."; |
| 6 | import { |
| 7 | isProcessBackgrounded, |
| 8 | removeBackgroundedProcess, |
| 9 | } from "../../util/processTerminalBackgroundStates"; |
| 10 | |
| 11 | const asyncExec = util.promisify(childProcess.exec); |
| 12 | |
| 13 | export const runTerminalCommandImpl: ToolImpl = async (args, extras) => { |
| 14 | // Default to waiting for completion if not specified |
| 15 | const waitForCompletion = args.waitForCompletion !== false; |
| 16 | const ideInfo = await extras.ide.getIdeInfo(); |
| 17 | const toolCallId = extras.toolCallId || ""; |
| 18 | |
| 19 | if (ideInfo.remoteName === "local" || ideInfo.remoteName === "") { |
| 20 | // For streaming output |
| 21 | if (extras.onPartialOutput) { |
| 22 | return new Promise((resolve, reject) => { |
| 23 | try { |
| 24 | const getWorkspaceDirsPromise = extras.ide.getWorkspaceDirs(); |
| 25 | getWorkspaceDirsPromise |
| 26 | .then((workspaceDirs) => { |
| 27 | const cwd = fileURLToPath(workspaceDirs[0]); |
| 28 | let terminalOutput = ""; |
| 29 | |
| 30 | if (!waitForCompletion) { |
| 31 | const status = "Command is running in the background..."; |
| 32 | if (extras.onPartialOutput) { |
| 33 | extras.onPartialOutput({ |
| 34 | toolCallId, |
| 35 | contextItems: [ |
| 36 | { |
| 37 | name: "Terminal", |
| 38 | description: "Terminal command output", |
| 39 | content: "", |
| 40 | status: status, |
| 41 | }, |
| 42 | ], |
| 43 | }); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Use spawn instead of exec to get streaming output |
| 48 | const childProc = childProcess.spawn(args.command, { |
| 49 | cwd, |
| 50 | shell: true, |
| 51 | }); |
| 52 | |
| 53 | childProc.stdout?.on("data", (data) => { |
| 54 | // Skip if this process has been backgrounded |
| 55 | if (isProcessBackgrounded(toolCallId)) return; |
| 56 | |
| 57 | const newOutput = data.toString(); |
| 58 | terminalOutput += newOutput; |
| 59 | |
| 60 | // Send partial output to UI |
| 61 | if (extras.onPartialOutput) { |
| 62 | const status = waitForCompletion |
| 63 | ? "" |
| 64 | : "Command is running in the background..."; |
| 65 | extras.onPartialOutput({ |
| 66 | toolCallId, |
| 67 | contextItems: [ |
| 68 | { |
| 69 | name: "Terminal", |
| 70 | description: "Terminal command output", |
| 71 | content: terminalOutput, |
| 72 | status: status, |
| 73 | }, |
| 74 | ], |
| 75 | }); |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | childProc.stderr?.on("data", (data) => { |
| 80 | // Skip if this process has been backgrounded |
| 81 | if (isProcessBackgrounded(toolCallId)) return; |
| 82 | |
| 83 | const newOutput = data.toString(); |
| 84 | terminalOutput += newOutput; |
| 85 | |
| 86 | // Send partial output to UI, status is not required |
| 87 | if (extras.onPartialOutput) { |
| 88 | extras.onPartialOutput({ |
| 89 | toolCallId, |
| 90 | contextItems: [ |
| 91 | { |
| 92 | name: "Terminal", |
| 93 | description: "Terminal command output", |
| 94 | content: terminalOutput, |
| 95 | }, |
| 96 | ], |
| 97 | }); |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | // If we don't need to wait for completion, resolve immediately |
| 102 | if (!waitForCompletion) { |
| 103 | const status = "Command is running in the background..."; |
| 104 | resolve([ |
| 105 | { |
| 106 | name: "Terminal", |
| 107 | description: "Terminal command output", |
| 108 | content: terminalOutput, |
| 109 | status: status, |
| 110 | }, |
| 111 | ]); |
| 112 | } |
| 113 | |
| 114 | childProc.on("close", (code) => { |
| 115 | // If this process has been backgrounded, clean it up from the map and return |
| 116 | if (isProcessBackgrounded(toolCallId)) { |
| 117 | removeBackgroundedProcess(toolCallId); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | if (!waitForCompletion) { |
| 122 | // Already resolved, just update the UI with final output |
| 123 | if (extras.onPartialOutput) { |
| 124 | const status = |
| 125 | code === 0 || !code |
| 126 | ? "\nBackground command completed" |
| 127 | : `\nBackground command failed with exit code ${code}`; |
| 128 | extras.onPartialOutput({ |
| 129 | toolCallId, |
| 130 | contextItems: [ |
| 131 | { |
| 132 | name: "Terminal", |
| 133 | description: "Terminal command output", |
| 134 | content: terminalOutput, |
| 135 | status: status, |
| 136 | }, |
| 137 | ], |
| 138 | }); |
| 139 | } |
| 140 | } else { |
| 141 | // Normal completion, resolve now |
| 142 | if (code === 0) { |
| 143 | const status = "Command completed"; |
| 144 | resolve([ |
| 145 | { |
| 146 | name: "Terminal", |
| 147 | description: "Terminal command output", |
| 148 | content: terminalOutput, |
| 149 | status: status, |
| 150 | }, |
| 151 | ]); |
| 152 | } else { |
| 153 | const status = `Command failed with exit code ${code}`; |
| 154 | resolve([ |
| 155 | { |
| 156 | name: "Terminal", |
| 157 | description: "Terminal command output", |
| 158 | content: terminalOutput, |
| 159 | status: status, |
| 160 | }, |
| 161 | ]); |
| 162 | } |
| 163 | } |
| 164 | }); |
| 165 | |
| 166 | childProc.on("error", (error) => { |
| 167 | // If this process has been backgrounded, clean it up from the map and return |
| 168 | if (isProcessBackgrounded(toolCallId)) { |
| 169 | removeBackgroundedProcess(toolCallId); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | reject(error); |
| 174 | }); |
| 175 | }) |
| 176 | .catch((error) => { |
| 177 | reject(error); |
| 178 | }); |
| 179 | } catch (error: any) { |
| 180 | reject(error); |
| 181 | } |
| 182 | }); |
| 183 | } else { |
| 184 | // Fallback to non-streaming for older clients |
| 185 | const workspaceDirs = await extras.ide.getWorkspaceDirs(); |
| 186 | const cwd = fileURLToPath(workspaceDirs[0]); |
| 187 | |
| 188 | if (!waitForCompletion) { |
| 189 | // For non-streaming but also not waiting for completion, use spawn |
| 190 | // but don't attach any listeners other than error |
| 191 | try { |
| 192 | // Use spawn instead of exec but don't wait |
| 193 | const childProc = childProcess.spawn(args.command, { |
| 194 | cwd, |
| 195 | shell: true, |
| 196 | // Detach the process so it's not tied to the parent |
| 197 | detached: true, |
| 198 | // Redirect to /dev/null equivalent (works cross-platform) |
| 199 | stdio: "ignore", |
| 200 | }); |
| 201 | |
| 202 | // Even for detached processes, add event handlers to clean up the background process map |
| 203 | childProc.on("close", () => { |
| 204 | if (isProcessBackgrounded(toolCallId)) { |
| 205 | removeBackgroundedProcess(toolCallId); |
| 206 | } |
| 207 | }); |
| 208 | |
| 209 | childProc.on("error", () => { |
| 210 | if (isProcessBackgrounded(toolCallId)) { |
| 211 | removeBackgroundedProcess(toolCallId); |
| 212 | } |
| 213 | }); |
| 214 | |
| 215 | // Unref the child to allow the Node.js process to exit |
| 216 | childProc.unref(); |
| 217 | const status = "Command is running in the background..."; |
| 218 | return [ |
| 219 | { |
| 220 | name: "Terminal", |
| 221 | description: "Terminal command output", |
| 222 | content: status, |
| 223 | status: status, |
| 224 | }, |
| 225 | ]; |
| 226 | } catch (error: any) { |
| 227 | const status = `Command failed with: ${error.message || error.toString()}`; |
| 228 | return [ |
| 229 | { |
| 230 | name: "Terminal", |
| 231 | description: "Terminal command output", |
| 232 | content: status, |
| 233 | status: status, |
| 234 | }, |
| 235 | ]; |
| 236 | } |
| 237 | } else { |
| 238 | // Standard execution, waiting for completion |
| 239 | try { |
| 240 | const output = await asyncExec(args.command, { cwd }); |
| 241 | const status = "Command completed"; |
| 242 | return [ |
| 243 | { |
| 244 | name: "Terminal", |
| 245 | description: "Terminal command output", |
| 246 | content: output.stdout ?? "", |
| 247 | status: status, |
| 248 | }, |
| 249 | ]; |
| 250 | } catch (error: any) { |
| 251 | const status = `Command failed with: ${error.message || error.toString()}`; |
| 252 | return [ |
| 253 | { |
| 254 | name: "Terminal", |
| 255 | description: "Terminal command output", |
| 256 | content: error.stderr ?? error.toString(), |
| 257 | status: status, |
| 258 | }, |
| 259 | ]; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // For remote environments, just run the command |
| 266 | // Note: waitForCompletion is not supported in remote environments yet |
| 267 | await extras.ide.runCommand(args.command); |
| 268 | return [ |
| 269 | { |
| 270 | name: "Terminal", |
| 271 | description: "Terminal command output", |
| 272 | content: |
| 273 | "Terminal output not available. This is only available in local development environments and not in SSH environments for example.", |
| 274 | status: "Command failed", |
| 275 | }, |
| 276 | ]; |
| 277 | }; |