codebuff 1.0.316 → 1.0.317
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli-definitions.js +2 -2
- package/dist/cli-definitions.js.map +1 -1
- package/dist/cli-handlers/checkpoint.d.ts +1 -0
- package/dist/cli-handlers/checkpoint.js +11 -7
- package/dist/cli-handlers/checkpoint.js.map +1 -1
- package/dist/cli.js +7 -17
- package/dist/cli.js.map +1 -1
- package/dist/client.d.ts +1 -0
- package/dist/client.js +36 -21
- package/dist/client.js.map +1 -1
- package/dist/common/constants.d.ts +7 -10
- package/dist/common/constants.js +3 -4
- package/dist/common/constants.js.map +1 -1
- package/dist/common/db/schema.d.ts +4 -4
- package/dist/common/db/schema.js +2 -2
- package/dist/common/db/schema.js.map +1 -1
- package/dist/common/util/string.d.ts +17 -0
- package/dist/common/util/string.js +31 -1
- package/dist/common/util/string.js.map +1 -1
- package/dist/dev-process-manager.js +2 -2
- package/dist/dev-process-manager.js.map +1 -1
- package/dist/index.js +20 -2
- package/dist/index.js.map +1 -1
- package/dist/json-config/hooks.js +2 -2
- package/dist/json-config/hooks.js.map +1 -1
- package/dist/terminal/background.d.ts +12 -0
- package/dist/terminal/background.js +148 -0
- package/dist/terminal/background.js.map +1 -0
- package/dist/terminal/base.d.ts +47 -0
- package/dist/terminal/base.js +671 -0
- package/dist/terminal/base.js.map +1 -0
- package/dist/tool-handlers.js +3 -3
- package/dist/tool-handlers.js.map +1 -1
- package/dist/utils/git.js +0 -1
- package/dist/utils/git.js.map +1 -1
- package/dist/utils/terminal.js +2 -7
- package/dist/utils/terminal.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,671 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.readNewTerminalOutput = exports.runCommandPtyManager = exports.runCommandPty = exports.runTerminalCommand = exports.resetShell = exports.recreateShell = exports.isCommandRunning = exports.persistentProcess = void 0;
|
|
30
|
+
exports.killAndResetPersistentProcess = killAndResetPersistentProcess;
|
|
31
|
+
exports.clearScreen = clearScreen;
|
|
32
|
+
const child_process_1 = require("child_process");
|
|
33
|
+
const os = __importStar(require("os"));
|
|
34
|
+
const path_1 = __importDefault(require("path"));
|
|
35
|
+
const analytics_events_1 = require("../common/constants/analytics-events");
|
|
36
|
+
const array_1 = require("../common/util/array");
|
|
37
|
+
const string_1 = require("../common/util/string");
|
|
38
|
+
const picocolors_1 = require("picocolors");
|
|
39
|
+
const project_files_1 = require("../project-files");
|
|
40
|
+
const analytics_1 = require("../utils/analytics");
|
|
41
|
+
const detect_shell_1 = require("../utils/detect-shell");
|
|
42
|
+
const logger_1 = require("../utils/logger");
|
|
43
|
+
const background_1 = require("./background");
|
|
44
|
+
let pty;
|
|
45
|
+
const tempConsoleError = console.error;
|
|
46
|
+
console.error = () => { };
|
|
47
|
+
try {
|
|
48
|
+
pty = require('@homebridge/node-pty-prebuilt-multiarch');
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
console.error = tempConsoleError;
|
|
54
|
+
}
|
|
55
|
+
const COMMAND_OUTPUT_LIMIT = 10_000;
|
|
56
|
+
const promptIdentifier = '@36261@';
|
|
57
|
+
const needleIdentifier = '@76593@';
|
|
58
|
+
const createPersistantProcess = (dir, forceChildProcess = false) => {
|
|
59
|
+
if (pty && process.env.NODE_ENV !== 'test' && !forceChildProcess) {
|
|
60
|
+
const isWindows = os.platform() === 'win32';
|
|
61
|
+
const currShell = (0, detect_shell_1.detectShell)();
|
|
62
|
+
const shell = isWindows
|
|
63
|
+
? currShell === 'powershell'
|
|
64
|
+
? 'powershell.exe'
|
|
65
|
+
: 'cmd.exe'
|
|
66
|
+
: 'bash';
|
|
67
|
+
const shellWithoutExe = shell.split('.')[0];
|
|
68
|
+
// Prepare shell init commands
|
|
69
|
+
let shellInitCommands = '';
|
|
70
|
+
if (!isWindows) {
|
|
71
|
+
// Source all relevant config files based on shell type
|
|
72
|
+
if (currShell === 'zsh') {
|
|
73
|
+
shellInitCommands = `
|
|
74
|
+
source ~/.zshenv 2>/dev/null || true
|
|
75
|
+
source ~/.zprofile 2>/dev/null || true
|
|
76
|
+
source ~/.zshrc 2>/dev/null || true
|
|
77
|
+
source ~/.zlogin 2>/dev/null || true
|
|
78
|
+
`;
|
|
79
|
+
}
|
|
80
|
+
else if (currShell === 'fish') {
|
|
81
|
+
shellInitCommands = `
|
|
82
|
+
source ~/.config/fish/config.fish 2>/dev/null || true
|
|
83
|
+
`;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// Bash - source both profile and rc files
|
|
87
|
+
shellInitCommands = `
|
|
88
|
+
source ~/.bash_profile 2>/dev/null || true
|
|
89
|
+
source ~/.profile 2>/dev/null || true
|
|
90
|
+
source ~/.bashrc 2>/dev/null || true
|
|
91
|
+
`;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else if (currShell === 'powershell') {
|
|
95
|
+
// Try to source all possible PowerShell profile locations
|
|
96
|
+
shellInitCommands = `
|
|
97
|
+
$profiles = @(
|
|
98
|
+
$PROFILE.AllUsersAllHosts,
|
|
99
|
+
$PROFILE.AllUsersCurrentHost,
|
|
100
|
+
$PROFILE.CurrentUserAllHosts,
|
|
101
|
+
$PROFILE.CurrentUserCurrentHost
|
|
102
|
+
)
|
|
103
|
+
foreach ($prof in $profiles) {
|
|
104
|
+
if (Test-Path $prof) { . $prof }
|
|
105
|
+
}
|
|
106
|
+
`;
|
|
107
|
+
}
|
|
108
|
+
const persistentPty = pty.spawn(shell, isWindows ? [] : ['--login'], {
|
|
109
|
+
name: 'xterm-256color',
|
|
110
|
+
cols: process.stdout.columns || 80,
|
|
111
|
+
rows: process.stdout.rows || 24,
|
|
112
|
+
cwd: dir,
|
|
113
|
+
env: {
|
|
114
|
+
...process.env,
|
|
115
|
+
PAGER: 'cat',
|
|
116
|
+
GIT_PAGER: 'cat',
|
|
117
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
118
|
+
...(isWindows
|
|
119
|
+
? {
|
|
120
|
+
TERM: 'cygwin',
|
|
121
|
+
ANSICON: '1',
|
|
122
|
+
PROMPT: promptIdentifier,
|
|
123
|
+
}
|
|
124
|
+
: {
|
|
125
|
+
TERM: 'xterm-256color',
|
|
126
|
+
// Preserve important environment variables
|
|
127
|
+
PATH: process.env.PATH,
|
|
128
|
+
HOME: process.env.HOME,
|
|
129
|
+
USER: process.env.USER,
|
|
130
|
+
SHELL: shellWithoutExe,
|
|
131
|
+
}),
|
|
132
|
+
LESS: '-FRX',
|
|
133
|
+
TERM_PROGRAM: 'mintty',
|
|
134
|
+
FORCE_COLOR: '1',
|
|
135
|
+
// Locale settings for consistent output
|
|
136
|
+
LANG: 'en_US.UTF-8',
|
|
137
|
+
LC_ALL: 'en_US.UTF-8',
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
// Source the shell config files
|
|
141
|
+
if (shellInitCommands) {
|
|
142
|
+
persistentPty.write(shellInitCommands);
|
|
143
|
+
}
|
|
144
|
+
// Set prompt for Unix shells after sourcing config
|
|
145
|
+
if (!isWindows) {
|
|
146
|
+
persistentPty.write(`PS1=${promptIdentifier} && PS2=${promptIdentifier}\n`);
|
|
147
|
+
}
|
|
148
|
+
const persistentProcessInfo = {
|
|
149
|
+
type: 'pty',
|
|
150
|
+
shell,
|
|
151
|
+
pty: persistentPty,
|
|
152
|
+
timerId: null,
|
|
153
|
+
globalOutputBuffer: '',
|
|
154
|
+
globalOutputLastReadLength: 0,
|
|
155
|
+
};
|
|
156
|
+
// Add a persistent listener to capture all output for manager mode
|
|
157
|
+
persistentPty.onData((data) => {
|
|
158
|
+
if (persistentProcessInfo.type === 'pty') {
|
|
159
|
+
persistentProcessInfo.globalOutputBuffer += data.toString(); // Should we use stripColors(...)?
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
return persistentProcessInfo;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
// Fallback to child_process
|
|
166
|
+
const isWindows = os.platform() === 'win32';
|
|
167
|
+
const currShell = (0, detect_shell_1.detectShell)();
|
|
168
|
+
const shell = isWindows
|
|
169
|
+
? currShell === 'powershell'
|
|
170
|
+
? 'powershell.exe'
|
|
171
|
+
: 'cmd.exe'
|
|
172
|
+
: 'bash';
|
|
173
|
+
const childProcess = null;
|
|
174
|
+
return {
|
|
175
|
+
type: 'process',
|
|
176
|
+
shell,
|
|
177
|
+
childProcess,
|
|
178
|
+
timerId: null,
|
|
179
|
+
globalOutputBuffer: '',
|
|
180
|
+
globalOutputLastReadLength: 0,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
exports.persistentProcess = null;
|
|
185
|
+
process.stdout.on('resize', () => {
|
|
186
|
+
if (!exports.persistentProcess)
|
|
187
|
+
return;
|
|
188
|
+
if (exports.persistentProcess.type === 'pty') {
|
|
189
|
+
exports.persistentProcess.pty.resize(process.stdout.columns, process.stdout.rows);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
let commandIsRunning = false;
|
|
193
|
+
const isCommandRunning = () => {
|
|
194
|
+
return commandIsRunning;
|
|
195
|
+
};
|
|
196
|
+
exports.isCommandRunning = isCommandRunning;
|
|
197
|
+
const recreateShell = (cwd, forceChildProcess = false) => {
|
|
198
|
+
exports.persistentProcess = createPersistantProcess(cwd, forceChildProcess);
|
|
199
|
+
};
|
|
200
|
+
exports.recreateShell = recreateShell;
|
|
201
|
+
const resetShell = (cwd) => {
|
|
202
|
+
commandIsRunning = false;
|
|
203
|
+
if (exports.persistentProcess) {
|
|
204
|
+
if (exports.persistentProcess.timerId) {
|
|
205
|
+
clearTimeout(exports.persistentProcess.timerId);
|
|
206
|
+
exports.persistentProcess.timerId = null;
|
|
207
|
+
}
|
|
208
|
+
if (exports.persistentProcess.type === 'pty') {
|
|
209
|
+
exports.persistentProcess.pty.kill();
|
|
210
|
+
(0, exports.recreateShell)(cwd);
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
exports.persistentProcess.childProcess?.kill();
|
|
214
|
+
exports.persistentProcess = {
|
|
215
|
+
...exports.persistentProcess,
|
|
216
|
+
childProcess: null,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
exports.resetShell = resetShell;
|
|
222
|
+
function formatResult(command, stdout, status) {
|
|
223
|
+
return (0, array_1.buildArray)(`<command>${command}</command>`, '<terminal_command_result>', stdout &&
|
|
224
|
+
`<output>${(0, string_1.truncateStringWithMessage)({ str: (0, string_1.stripColors)(stdout), maxLength: COMMAND_OUTPUT_LIMIT, remove: 'MIDDLE' })}</output>`, `<status>${status}</status>`, '</terminal_command_result>').join('\n');
|
|
225
|
+
}
|
|
226
|
+
const runTerminalCommand = async (toolCallId, command, mode, processType, timeoutSeconds, cwd, stdoutFile, stderrFile) => {
|
|
227
|
+
const maybeTimeoutSeconds = timeoutSeconds < 0 ? null : timeoutSeconds;
|
|
228
|
+
cwd = cwd || (mode === 'assistant' ? (0, project_files_1.getProjectRoot)() : (0, project_files_1.getWorkingDirectory)());
|
|
229
|
+
return new Promise((resolve) => {
|
|
230
|
+
if (!exports.persistentProcess) {
|
|
231
|
+
throw new Error('Shell not initialized');
|
|
232
|
+
}
|
|
233
|
+
if (commandIsRunning) {
|
|
234
|
+
(0, exports.resetShell)(cwd);
|
|
235
|
+
}
|
|
236
|
+
commandIsRunning = true;
|
|
237
|
+
// Add special case for git log to limit output
|
|
238
|
+
const modifiedCommand = command.trim() === 'git log' ? 'git log -n 5' : command;
|
|
239
|
+
const resolveCommand = (value) => {
|
|
240
|
+
commandIsRunning = false;
|
|
241
|
+
(0, analytics_1.trackEvent)(analytics_events_1.AnalyticsEvent.TERMINAL_COMMAND_COMPLETED, {
|
|
242
|
+
command,
|
|
243
|
+
result: value.result,
|
|
244
|
+
stdout: value.stdout,
|
|
245
|
+
exitCode: value.exitCode,
|
|
246
|
+
mode,
|
|
247
|
+
processType,
|
|
248
|
+
});
|
|
249
|
+
resolve(value);
|
|
250
|
+
};
|
|
251
|
+
if (processType === 'BACKGROUND') {
|
|
252
|
+
(0, background_1.runBackgroundCommand)({
|
|
253
|
+
toolCallId,
|
|
254
|
+
command: modifiedCommand,
|
|
255
|
+
mode,
|
|
256
|
+
cwd,
|
|
257
|
+
stdoutFile,
|
|
258
|
+
stderrFile,
|
|
259
|
+
}, resolveCommand);
|
|
260
|
+
}
|
|
261
|
+
else if (exports.persistentProcess.type === 'pty') {
|
|
262
|
+
if (mode === 'manager') {
|
|
263
|
+
(0, exports.runCommandPtyManager)(exports.persistentProcess, modifiedCommand, cwd, maybeTimeoutSeconds, resolveCommand);
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
(0, exports.runCommandPty)(exports.persistentProcess, modifiedCommand, mode, cwd, maybeTimeoutSeconds, resolveCommand);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
// Fallback to child_process implementation
|
|
271
|
+
runCommandChildProcess(exports.persistentProcess, modifiedCommand, mode, cwd, maybeTimeoutSeconds, resolveCommand);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
exports.runTerminalCommand = runTerminalCommand;
|
|
276
|
+
const echoLinePattern = new RegExp(`${promptIdentifier}[^\n]*\n`, 'g');
|
|
277
|
+
const getNeedlePatternCache = {};
|
|
278
|
+
function getNeedlePattern(middlePattern = '.*') {
|
|
279
|
+
if (!(middlePattern in getNeedlePatternCache)) {
|
|
280
|
+
getNeedlePatternCache[middlePattern] = new RegExp(`${needleIdentifier}(${middlePattern})${needleIdentifier}`);
|
|
281
|
+
}
|
|
282
|
+
return getNeedlePatternCache[middlePattern];
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Executes a single command in a PTY process and returns the result when complete.
|
|
286
|
+
*
|
|
287
|
+
* This function handles the low-level details of running a command in a pseudo-terminal,
|
|
288
|
+
* including parsing the output to separate command echoes from actual output, detecting
|
|
289
|
+
* command completion
|
|
290
|
+
*
|
|
291
|
+
* @param ptyProcess - The IPty instance to execute the command in
|
|
292
|
+
* @param command - The shell command to execute
|
|
293
|
+
* @param onChunk - Callback function called for each chunk of output as it's received
|
|
294
|
+
*
|
|
295
|
+
* @returns Promise that resolves with:
|
|
296
|
+
* - The complete output from the command (excluding echo lines)
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* const result = await runSinglePtyCommand(
|
|
301
|
+
* ptyProcess,
|
|
302
|
+
* 'ls -la',
|
|
303
|
+
* process.stdout.write
|
|
304
|
+
* );
|
|
305
|
+
* ```
|
|
306
|
+
*
|
|
307
|
+
* @internal This is a low-level utility function used by other terminal command runners.
|
|
308
|
+
* It handles platform-specific differences between Windows and Unix-like systems.
|
|
309
|
+
*
|
|
310
|
+
* The function works by:
|
|
311
|
+
* 1. Setting up a data listener on the PTY process
|
|
312
|
+
* 2. Filtering out command echo lines (the command being typed)
|
|
313
|
+
* 3. Detecting command completion markers (promptIdentifier)
|
|
314
|
+
*/
|
|
315
|
+
function runSinglePtyCommand(ptyProcess, command, onChunk) {
|
|
316
|
+
const isWindows = os.platform() === 'win32';
|
|
317
|
+
let commandOutput = '';
|
|
318
|
+
let buffer = promptIdentifier;
|
|
319
|
+
let echoLinesRemaining = isWindows ? 1 : command.split('\n').length;
|
|
320
|
+
const resultPromise = new Promise((resolve) => {
|
|
321
|
+
const dataDisposable = ptyProcess.onData((data) => {
|
|
322
|
+
buffer += data;
|
|
323
|
+
// Wait for pending promptIdentifier
|
|
324
|
+
const suffix = (0, string_1.suffixPrefixOverlap)(buffer, promptIdentifier);
|
|
325
|
+
let toProcess = buffer.slice(0, buffer.length - suffix.length);
|
|
326
|
+
buffer = suffix;
|
|
327
|
+
// Remove echo lines from the output
|
|
328
|
+
const matches = toProcess.match(echoLinePattern);
|
|
329
|
+
if (matches) {
|
|
330
|
+
for (let i = 0; i < matches.length && echoLinesRemaining > 0; i++) {
|
|
331
|
+
echoLinesRemaining = Math.max(echoLinesRemaining - 1, 0);
|
|
332
|
+
toProcess = toProcess.replace(echoLinePattern, '');
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
// Do not process anything after a promptIdentifier (pending line)
|
|
336
|
+
const promptIdentifierIndex = toProcess.indexOf(promptIdentifier);
|
|
337
|
+
if (promptIdentifierIndex !== -1) {
|
|
338
|
+
buffer = toProcess.slice(promptIdentifierIndex) + buffer;
|
|
339
|
+
toProcess = toProcess.slice(0, promptIdentifierIndex);
|
|
340
|
+
}
|
|
341
|
+
onChunk(toProcess);
|
|
342
|
+
commandOutput += toProcess;
|
|
343
|
+
const commandDone = buffer.startsWith(promptIdentifier);
|
|
344
|
+
if (commandDone && echoLinesRemaining === 0) {
|
|
345
|
+
// Command is done
|
|
346
|
+
dataDisposable.dispose();
|
|
347
|
+
resolve(commandOutput);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
// Write the command
|
|
352
|
+
ptyProcess.write(`${command}\r`);
|
|
353
|
+
return resultPromise;
|
|
354
|
+
}
|
|
355
|
+
const runCommandPty = (persistentProcess, command, mode, cwd, maybeTimeoutSeconds, resolve) => {
|
|
356
|
+
const ptyProcess = persistentProcess.pty;
|
|
357
|
+
if (command.trim() === 'clear') {
|
|
358
|
+
// `clear` needs access to the main process stdout. This is a workaround.
|
|
359
|
+
(0, child_process_1.execSync)('clear', { stdio: 'inherit' });
|
|
360
|
+
resolve({
|
|
361
|
+
result: formatResult(command, '', `Complete`),
|
|
362
|
+
stdout: '',
|
|
363
|
+
exitCode: 0,
|
|
364
|
+
});
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
const projectRoot = (0, project_files_1.getProjectRoot)();
|
|
368
|
+
const isWindows = os.platform() === 'win32';
|
|
369
|
+
if (mode === 'assistant') {
|
|
370
|
+
const displayDirectory = path_1.default.join(path_1.default.parse(projectRoot).base, path_1.default.relative(projectRoot, path_1.default.resolve(projectRoot, cwd)));
|
|
371
|
+
console.log((0, picocolors_1.green)(`${displayDirectory} > ${command}`));
|
|
372
|
+
}
|
|
373
|
+
let commandOutput = '';
|
|
374
|
+
let timer = null;
|
|
375
|
+
if (maybeTimeoutSeconds !== null) {
|
|
376
|
+
timer = setTimeout(() => {
|
|
377
|
+
if (mode === 'assistant') {
|
|
378
|
+
// Kill and recreate PTY
|
|
379
|
+
(0, exports.resetShell)(cwd);
|
|
380
|
+
resolve({
|
|
381
|
+
result: formatResult(command, commandOutput, `Command timed out after ${maybeTimeoutSeconds} seconds and was terminated. Shell has been restarted.`),
|
|
382
|
+
stdout: commandOutput,
|
|
383
|
+
exitCode: 124,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
}, maybeTimeoutSeconds * 1000);
|
|
387
|
+
}
|
|
388
|
+
persistentProcess.timerId = timer;
|
|
389
|
+
new Promise(async () => {
|
|
390
|
+
await runSinglePtyCommand(ptyProcess, `cd ${cwd}`, () => { });
|
|
391
|
+
await runSinglePtyCommand(ptyProcess, command, (data) => {
|
|
392
|
+
commandOutput += data;
|
|
393
|
+
process.stdout.write(data);
|
|
394
|
+
});
|
|
395
|
+
const exitCodeHaystack = await runSinglePtyCommand(ptyProcess, persistentProcess.shell === 'cmd.exe'
|
|
396
|
+
? `echo ${needleIdentifier}%ERRORLEVEL%${needleIdentifier}`
|
|
397
|
+
: `echo ${needleIdentifier}$?${needleIdentifier}`, () => { });
|
|
398
|
+
let exitCode = null;
|
|
399
|
+
const exitCodeMatch = exitCodeHaystack.match(getNeedlePattern('\\d+'));
|
|
400
|
+
if (exitCodeMatch) {
|
|
401
|
+
exitCode = parseInt(exitCodeMatch[1].trim());
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
logger_1.logger.error({ exitCodeHaystack }, 'Could not find exitCode in output');
|
|
405
|
+
}
|
|
406
|
+
const cwdHaystack = await runSinglePtyCommand(ptyProcess, isWindows
|
|
407
|
+
? `echo ${needleIdentifier}%cd%${needleIdentifier}`
|
|
408
|
+
: `echo ${needleIdentifier}$(pwd)${needleIdentifier}`, () => { });
|
|
409
|
+
const m = cwdHaystack.match(getNeedlePattern());
|
|
410
|
+
let newWorkingDirectory;
|
|
411
|
+
if (m) {
|
|
412
|
+
newWorkingDirectory = m[1].trim();
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
logger_1.logger.error({ cwdHaystack }, 'Could not find cwd in output');
|
|
416
|
+
newWorkingDirectory = cwd;
|
|
417
|
+
}
|
|
418
|
+
const statusMessage = exitCode === null
|
|
419
|
+
? ''
|
|
420
|
+
: exitCode === 0
|
|
421
|
+
? 'Complete'
|
|
422
|
+
: `Failed with exit code: ${exitCode}`;
|
|
423
|
+
if (timer) {
|
|
424
|
+
clearTimeout(timer);
|
|
425
|
+
}
|
|
426
|
+
if (mode === 'assistant') {
|
|
427
|
+
await runSinglePtyCommand(ptyProcess, `cd ${(0, project_files_1.getWorkingDirectory)()}`, () => { });
|
|
428
|
+
resolve({
|
|
429
|
+
result: formatResult(command, commandOutput, (0, array_1.buildArray)([
|
|
430
|
+
`cwd: ${path_1.default.resolve(projectRoot, cwd)}`,
|
|
431
|
+
statusMessage,
|
|
432
|
+
]).join('\n\n')),
|
|
433
|
+
stdout: commandOutput,
|
|
434
|
+
exitCode,
|
|
435
|
+
});
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
let outsideProject = false;
|
|
439
|
+
const currentWorkingDirectory = (0, project_files_1.getWorkingDirectory)();
|
|
440
|
+
let finalCwd = currentWorkingDirectory;
|
|
441
|
+
if (newWorkingDirectory !== currentWorkingDirectory) {
|
|
442
|
+
(0, analytics_1.trackEvent)(analytics_events_1.AnalyticsEvent.CHANGE_DIRECTORY, {
|
|
443
|
+
from: currentWorkingDirectory,
|
|
444
|
+
to: newWorkingDirectory,
|
|
445
|
+
isSubdir: (0, project_files_1.isSubdir)(currentWorkingDirectory, newWorkingDirectory),
|
|
446
|
+
});
|
|
447
|
+
if (path_1.default.relative(projectRoot, newWorkingDirectory).startsWith('..')) {
|
|
448
|
+
outsideProject = true;
|
|
449
|
+
console.log(`
|
|
450
|
+
Unable to cd outside of the project root (${projectRoot})
|
|
451
|
+
|
|
452
|
+
If you want to change the project root:
|
|
453
|
+
1. Exit Codebuff (type "exit")
|
|
454
|
+
2. Navigate into the target directory (type "cd ${newWorkingDirectory}")
|
|
455
|
+
3. Restart Codebuff`);
|
|
456
|
+
await runSinglePtyCommand(ptyProcess, `cd ${currentWorkingDirectory}`, () => { });
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
(0, project_files_1.setWorkingDirectory)(newWorkingDirectory);
|
|
460
|
+
finalCwd = newWorkingDirectory;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
resolve({
|
|
464
|
+
result: formatResult(command, commandOutput, (0, array_1.buildArray)([
|
|
465
|
+
`cwd: ${currentWorkingDirectory}`,
|
|
466
|
+
`${statusMessage}\n`,
|
|
467
|
+
outsideProject &&
|
|
468
|
+
`Detected final cwd outside project root. Reset cwd to ${currentWorkingDirectory}`,
|
|
469
|
+
`Final **user** cwd: ${finalCwd} (Assistant's cwd is still project root)`,
|
|
470
|
+
]).join('\n')),
|
|
471
|
+
stdout: commandOutput,
|
|
472
|
+
exitCode,
|
|
473
|
+
});
|
|
474
|
+
});
|
|
475
|
+
};
|
|
476
|
+
exports.runCommandPty = runCommandPty;
|
|
477
|
+
const runCommandChildProcess = (persistentProcess, command, mode, cwd, maybeTimeoutSeconds, resolve) => {
|
|
478
|
+
const isWindows = os.platform() === 'win32';
|
|
479
|
+
let commandOutput = '';
|
|
480
|
+
if (mode === 'assistant') {
|
|
481
|
+
console.log((0, picocolors_1.green)(`> ${command}`));
|
|
482
|
+
}
|
|
483
|
+
const childProcess = (0, child_process_1.spawn)(persistentProcess.shell, [isWindows ? '/c' : '-c', command], {
|
|
484
|
+
cwd,
|
|
485
|
+
env: {
|
|
486
|
+
...process.env,
|
|
487
|
+
PAGER: 'cat',
|
|
488
|
+
GIT_PAGER: 'cat',
|
|
489
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
490
|
+
LESS: '-FRX',
|
|
491
|
+
},
|
|
492
|
+
});
|
|
493
|
+
persistentProcess = {
|
|
494
|
+
...persistentProcess,
|
|
495
|
+
childProcess,
|
|
496
|
+
};
|
|
497
|
+
let timer = null;
|
|
498
|
+
if (maybeTimeoutSeconds !== null) {
|
|
499
|
+
timer = setTimeout(() => {
|
|
500
|
+
(0, exports.resetShell)(cwd);
|
|
501
|
+
if (mode === 'assistant') {
|
|
502
|
+
resolve({
|
|
503
|
+
result: formatResult(command, commandOutput, `Command timed out after ${maybeTimeoutSeconds} seconds and was terminated.`),
|
|
504
|
+
stdout: commandOutput,
|
|
505
|
+
exitCode: 124,
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
}, maybeTimeoutSeconds * 1000);
|
|
509
|
+
}
|
|
510
|
+
persistentProcess.timerId = timer;
|
|
511
|
+
childProcess.stdout.on('data', (data) => {
|
|
512
|
+
const output = data.toString();
|
|
513
|
+
process.stdout.write(output);
|
|
514
|
+
commandOutput += output;
|
|
515
|
+
});
|
|
516
|
+
childProcess.stderr.on('data', (data) => {
|
|
517
|
+
const output = data.toString();
|
|
518
|
+
process.stdout.write(output);
|
|
519
|
+
commandOutput += output;
|
|
520
|
+
});
|
|
521
|
+
childProcess.on('close', (code) => {
|
|
522
|
+
if (timer) {
|
|
523
|
+
clearTimeout(timer);
|
|
524
|
+
}
|
|
525
|
+
if (command.startsWith('cd ') && mode === 'user') {
|
|
526
|
+
const newWorkingDirectory = command.split(' ')[1];
|
|
527
|
+
cwd = (0, project_files_1.setWorkingDirectory)(path_1.default.join(cwd, newWorkingDirectory));
|
|
528
|
+
}
|
|
529
|
+
if (mode === 'assistant') {
|
|
530
|
+
console.log((0, picocolors_1.green)(`Command completed`));
|
|
531
|
+
}
|
|
532
|
+
resolve({
|
|
533
|
+
result: formatResult(command, commandOutput, `complete`),
|
|
534
|
+
stdout: commandOutput,
|
|
535
|
+
exitCode: childProcess.exitCode,
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
};
|
|
539
|
+
function killAndResetPersistentProcess() {
|
|
540
|
+
if (exports.persistentProcess?.type === 'pty') {
|
|
541
|
+
exports.persistentProcess.pty.kill();
|
|
542
|
+
exports.persistentProcess = null;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
function clearScreen() {
|
|
546
|
+
process.stdout.write('\u001b[2J\u001b[0;0H');
|
|
547
|
+
}
|
|
548
|
+
// New function specifically for manager mode with settling behavior
|
|
549
|
+
const runCommandPtyManager = (persistentProcess, command, cwd, maybeTimeoutSeconds, resolve) => {
|
|
550
|
+
const ptyProcess = persistentProcess.pty;
|
|
551
|
+
if (command.trim() === 'clear') {
|
|
552
|
+
// `clear` needs access to the main process stdout. This is a workaround.
|
|
553
|
+
(0, child_process_1.execSync)('clear', { stdio: 'inherit' });
|
|
554
|
+
resolve({
|
|
555
|
+
result: formatResult(command, '', `Complete`),
|
|
556
|
+
stdout: '',
|
|
557
|
+
exitCode: 0,
|
|
558
|
+
});
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
const projectRoot = (0, project_files_1.getProjectRoot)();
|
|
562
|
+
const isWindows = os.platform() === 'win32';
|
|
563
|
+
console.log((0, picocolors_1.green)(`${cwd} > ${command}`));
|
|
564
|
+
let commandOutput = '';
|
|
565
|
+
let buffer = promptIdentifier;
|
|
566
|
+
let echoLinesRemaining = isWindows ? 1 : command.split('\n').length;
|
|
567
|
+
let timer = null;
|
|
568
|
+
let settleTimer = null;
|
|
569
|
+
// Use the provided timeout or default to 30 seconds for manager mode
|
|
570
|
+
const managerTimeoutMs = maybeTimeoutSeconds !== null ? maybeTimeoutSeconds * 1000 : 30000;
|
|
571
|
+
if (maybeTimeoutSeconds !== null) {
|
|
572
|
+
timer = setTimeout(() => {
|
|
573
|
+
// In manager mode, don't kill the terminal - just report what we have
|
|
574
|
+
if (timer) {
|
|
575
|
+
clearTimeout(timer);
|
|
576
|
+
}
|
|
577
|
+
if (settleTimer) {
|
|
578
|
+
clearTimeout(settleTimer);
|
|
579
|
+
}
|
|
580
|
+
dataDisposable.dispose();
|
|
581
|
+
resolve({
|
|
582
|
+
result: formatResult(command, commandOutput, `Command timed out after ${managerTimeoutMs / 1000} seconds. Output captured so far. Terminal is still running.`),
|
|
583
|
+
stdout: commandOutput,
|
|
584
|
+
exitCode: null, // null indicates timeout, not failure
|
|
585
|
+
});
|
|
586
|
+
}, managerTimeoutMs);
|
|
587
|
+
}
|
|
588
|
+
persistentProcess.timerId = timer;
|
|
589
|
+
const finishCommand = (exitCode = null) => {
|
|
590
|
+
if (timer) {
|
|
591
|
+
clearTimeout(timer);
|
|
592
|
+
}
|
|
593
|
+
if (settleTimer) {
|
|
594
|
+
clearTimeout(settleTimer);
|
|
595
|
+
}
|
|
596
|
+
dataDisposable.dispose();
|
|
597
|
+
const statusMessage = exitCode === 0
|
|
598
|
+
? 'Complete'
|
|
599
|
+
: exitCode === null
|
|
600
|
+
? 'Comand started'
|
|
601
|
+
: `Failed with exit code: ${exitCode}`;
|
|
602
|
+
resolve({
|
|
603
|
+
result: formatResult(command, undefined, `cwd: ${path_1.default.resolve(projectRoot, cwd)}\n\n${statusMessage}`),
|
|
604
|
+
stdout: commandOutput,
|
|
605
|
+
exitCode,
|
|
606
|
+
});
|
|
607
|
+
};
|
|
608
|
+
const dataDisposable = ptyProcess.onData((data) => {
|
|
609
|
+
buffer += data;
|
|
610
|
+
const suffix = (0, string_1.suffixPrefixOverlap)(buffer, promptIdentifier);
|
|
611
|
+
let toProcess = buffer.slice(0, buffer.length - suffix.length);
|
|
612
|
+
buffer = suffix;
|
|
613
|
+
const matches = toProcess.match(echoLinePattern);
|
|
614
|
+
if (matches) {
|
|
615
|
+
for (let i = 0; i < matches.length && echoLinesRemaining > 0; i++) {
|
|
616
|
+
echoLinesRemaining = Math.max(echoLinesRemaining - 1, 0);
|
|
617
|
+
// Process normal output line
|
|
618
|
+
toProcess = toProcess.replace(echoLinePattern, '');
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
const indexOfPromptIdentifier = toProcess.indexOf(promptIdentifier);
|
|
622
|
+
if (indexOfPromptIdentifier !== -1) {
|
|
623
|
+
buffer = toProcess.slice(indexOfPromptIdentifier) + buffer;
|
|
624
|
+
toProcess = toProcess.slice(0, indexOfPromptIdentifier);
|
|
625
|
+
}
|
|
626
|
+
process.stdout.write(toProcess);
|
|
627
|
+
commandOutput += toProcess;
|
|
628
|
+
// Reset settle timer whenever we get new output
|
|
629
|
+
if (settleTimer) {
|
|
630
|
+
clearTimeout(settleTimer);
|
|
631
|
+
}
|
|
632
|
+
// Set settle timer for 3000ms - if no new output comes, finish the command
|
|
633
|
+
settleTimer = setTimeout(() => {
|
|
634
|
+
finishCommand();
|
|
635
|
+
}, 3000);
|
|
636
|
+
const commandDone = buffer.startsWith(promptIdentifier);
|
|
637
|
+
if (commandDone && echoLinesRemaining === 0) {
|
|
638
|
+
// Command is done
|
|
639
|
+
const exitCode = buffer.includes('Command completed')
|
|
640
|
+
? 0
|
|
641
|
+
: (() => {
|
|
642
|
+
const match = buffer.match(/Command failed with exit code (\d+)\./);
|
|
643
|
+
return match ? parseInt(match[1]) : null;
|
|
644
|
+
})();
|
|
645
|
+
finishCommand(exitCode);
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
ptyProcess.write(`${command}`);
|
|
650
|
+
setTimeout(() => {
|
|
651
|
+
ptyProcess.write('\r');
|
|
652
|
+
}, 50);
|
|
653
|
+
};
|
|
654
|
+
exports.runCommandPtyManager = runCommandPtyManager;
|
|
655
|
+
// Add a function to get new terminal output since last read
|
|
656
|
+
const readNewTerminalOutput = (options = { maxLength: COMMAND_OUTPUT_LIMIT }) => {
|
|
657
|
+
if (!exports.persistentProcess) {
|
|
658
|
+
return '';
|
|
659
|
+
}
|
|
660
|
+
const currentLength = exports.persistentProcess.globalOutputBuffer.length;
|
|
661
|
+
const newOutput = exports.persistentProcess.globalOutputBuffer.slice(exports.persistentProcess.globalOutputLastReadLength);
|
|
662
|
+
// Update the last read position
|
|
663
|
+
exports.persistentProcess.globalOutputLastReadLength = currentLength;
|
|
664
|
+
return (0, string_1.truncateStringWithMessage)({
|
|
665
|
+
str: newOutput,
|
|
666
|
+
maxLength: options.maxLength,
|
|
667
|
+
remove: 'MIDDLE',
|
|
668
|
+
});
|
|
669
|
+
};
|
|
670
|
+
exports.readNewTerminalOutput = readNewTerminalOutput;
|
|
671
|
+
//# sourceMappingURL=base.js.map
|