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