| 1 | import { spawn } from "child_process"; |
| 2 | |
| 3 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 4 | import { |
| 5 | isGitCommitCommand, |
| 6 | isPullRequestCommand, |
| 7 | } from "../telemetry/utils.js"; |
| 8 | |
| 9 | import { Tool } from "./types.js"; |
| 10 | |
| 11 | export const runTerminalCommandTool: Tool = { |
| 12 | name: "Bash", |
| 13 | displayName: "Bash", |
| 14 | description: `Executes a terminal command and returns the output |
| 15 | |
| 16 | The command will be executed from the current working directory: ${process.cwd()} |
| 17 | `, |
| 18 | parameters: { |
| 19 | command: { |
| 20 | type: "string", |
| 21 | description: "The command to execute in the terminal.", |
| 22 | required: true, |
| 23 | }, |
| 24 | }, |
| 25 | readonly: false, |
| 26 | isBuiltIn: true, |
| 27 | preprocess: async (args) => { |
| 28 | const command = args.command; |
| 29 | if (!command || typeof command !== "string") { |
| 30 | throw new Error("command arg is required and must be a non-empty string"); |
| 31 | } |
| 32 | const truncatedCmd = |
| 33 | command.length > 60 ? command.substring(0, 60) + "..." : command; |
| 34 | return { |
| 35 | args, |
| 36 | preview: [ |
| 37 | { |
| 38 | type: "text", |
| 39 | content: `Will run: ${truncatedCmd}`, |
| 40 | }, |
| 41 | ], |
| 42 | }; |
| 43 | }, |
| 44 | run: async ({ command }: { command: string }): Promise<string> => { |
| 45 | return new Promise((resolve, reject) => { |
| 46 | const child = spawn("sh", ["-c", command]); |
| 47 | let stdout = ""; |
| 48 | let stderr = ""; |
| 49 | let timeoutId: NodeJS.Timeout; |
| 50 | let isResolved = false; |
| 51 | |
| 52 | const TIMEOUT_MS = |
| 53 | process.env.NODE_ENV === "test" && process.env.TEST_TERMINAL_TIMEOUT |
| 54 | ? parseInt(process.env.TEST_TERMINAL_TIMEOUT, 10) |
| 55 | : 30000; // 30 seconds default, configurable for tests |
| 56 | |
| 57 | const resetTimeout = () => { |
| 58 | if (timeoutId) { |
| 59 | clearTimeout(timeoutId); |
| 60 | } |
| 61 | timeoutId = setTimeout(() => { |
| 62 | if (isResolved) return; |
| 63 | isResolved = true; |
| 64 | child.kill(); |
| 65 | let output = stdout + (stderr ? `\nStderr: ${stderr}` : ""); |
| 66 | output += `\n\n[Command timed out after ${TIMEOUT_MS / 1000} seconds of no output]`; |
| 67 | |
| 68 | // Truncate output if it has too many lines |
| 69 | const lines = output.split("\n"); |
| 70 | if (lines.length > 5000) { |
| 71 | const truncatedOutput = lines.slice(0, 5000).join("\n"); |
| 72 | resolve( |
| 73 | truncatedOutput + |
| 74 | `\n\n[Output truncated to first 5000 lines of ${lines.length} total]`, |
| 75 | ); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | resolve(output); |
| 80 | }, TIMEOUT_MS); |
| 81 | }; |
| 82 | |
| 83 | // Start the initial timeout |
| 84 | resetTimeout(); |
| 85 | |
| 86 | child.stdout.on("data", (data) => { |
| 87 | stdout += data.toString(); |
| 88 | resetTimeout(); |
| 89 | }); |
| 90 | |
| 91 | child.stderr.on("data", (data) => { |
| 92 | stderr += data.toString(); |
| 93 | resetTimeout(); |
| 94 | }); |
| 95 | |
| 96 | child.on("close", (code) => { |
| 97 | if (isResolved) return; |
| 98 | isResolved = true; |
| 99 | |
| 100 | if (timeoutId) { |
| 101 | clearTimeout(timeoutId); |
| 102 | } |
| 103 | |
| 104 | // Only reject on non-zero exit code if there's also stderr |
| 105 | if (code !== 0 && stderr) { |
| 106 | reject(`Error (exit code ${code}): ${stderr}`); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Track specific git operations only after successful execution |
| 111 | if (code === 0) { |
| 112 | if (isGitCommitCommand(command)) { |
| 113 | telemetryService.recordCommitCreated(); |
| 114 | } else if (isPullRequestCommand(command)) { |
| 115 | telemetryService.recordPullRequestCreated(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | let output = stdout; |
| 120 | if (stderr) { |
| 121 | output = stdout + `\nStderr: ${stderr}`; |
| 122 | } |
| 123 | |
| 124 | // Truncate output if it has too many lines |
| 125 | const lines = output.split("\n"); |
| 126 | if (lines.length > 5000) { |
| 127 | const truncatedOutput = lines.slice(0, 5000).join("\n"); |
| 128 | resolve( |
| 129 | truncatedOutput + |
| 130 | `\n\n[Output truncated to first 5000 lines of ${lines.length} total]`, |
| 131 | ); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | resolve(output); |
| 136 | }); |
| 137 | |
| 138 | child.on("error", (error) => { |
| 139 | if (isResolved) return; |
| 140 | isResolved = true; |
| 141 | |
| 142 | if (timeoutId) { |
| 143 | clearTimeout(timeoutId); |
| 144 | } |
| 145 | reject(`Error: ${error.message}`); |
| 146 | }); |
| 147 | }); |
| 148 | }, |
| 149 | }; |