Elffuss BraidHub

kikocis/continue public

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