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