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