-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathExecuteCommandTool.ts
More file actions
709 lines (605 loc) · 25.1 KB
/
Copy pathExecuteCommandTool.ts
File metadata and controls
709 lines (605 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import fs from "fs/promises"
import * as path from "path"
import * as vscode from "vscode"
import delay from "delay"
import { CommandExecutionStatus, DEFAULT_TERMINAL_OUTPUT_PREVIEW_SIZE, PersistedCommandOutput } from "@roo-code/types"
import { TelemetryService } from "@roo-code/telemetry"
import { Task } from "../task/Task"
import { ToolUse, ToolResponse } from "../../shared/tools"
import { formatResponse } from "../prompts/responses"
import { unescapeHtmlEntities } from "../../utils/text-normalization"
import { parseCommand } from "../../shared/parse-command"
import {
ExitCodeDetails,
RooTerminalCallbacks,
RooTerminalProvider,
RooTerminalProcess,
ShellIntegrationError,
ShellIntegrationErrorDetails,
} from "../../integrations/terminal/types"
import { TerminalRegistry } from "../../integrations/terminal/TerminalRegistry"
import { Terminal } from "../../integrations/terminal/Terminal"
import { OutputInterceptor } from "../../integrations/terminal/OutputInterceptor"
import { Package } from "../../shared/package"
import { t } from "../../i18n"
import { getTaskDirectoryPath } from "../../utils/storage"
import { BaseTool, ToolCallbacks } from "./BaseTool"
export { ShellIntegrationError } from "../../integrations/terminal/types"
export function canRetryShellIntegrationError(error: unknown): error is ShellIntegrationError {
return error instanceof ShellIntegrationError && !error.commandSubmitted
}
export function getTerminalProviderForExecution(terminalShellIntegrationDisabled: boolean): {
terminalProvider: RooTerminalProvider
isCmdExeFallback: boolean
} {
const isCmdExeFallback = !terminalShellIntegrationDisabled && Terminal.isActiveShellCmdExe()
const terminalProvider = terminalShellIntegrationDisabled || isCmdExeFallback ? "execa" : "vscode"
return { terminalProvider, isCmdExeFallback }
}
interface ExecuteCommandParams {
command: string
cwd?: string
timeout?: number | null
}
export function resolveAgentTimeoutMs(timeoutSeconds: number | null | undefined): number {
const requestedAgentTimeout = typeof timeoutSeconds === "number" && timeoutSeconds > 0 ? timeoutSeconds * 1000 : 0
// In CLI runtime, stdin harnesses expect command lifetime to be governed
// solely by commandExecutionTimeout (user setting), not model-provided
// background timeouts.
return process.env.ROO_CLI_RUNTIME === "1" ? 0 : requestedAgentTimeout
}
export class ExecuteCommandTool extends BaseTool<"execute_command"> {
readonly name = "execute_command" as const
async execute(params: ExecuteCommandParams, task: Task, callbacks: ToolCallbacks): Promise<void> {
const { command, cwd: customCwd, timeout: timeoutSeconds } = params
const { handleError, pushToolResult, askApproval } = callbacks
try {
if (!command) {
task.consecutiveMistakeCount++
task.recordToolError("execute_command")
pushToolResult(await task.sayAndCreateMissingParamError("execute_command", "command"))
return
}
const canonicalCommand = unescapeHtmlEntities(command)
const ignoredFileAttemptedToAccess = task.rooIgnoreController?.validateCommand(canonicalCommand)
if (ignoredFileAttemptedToAccess) {
await task.say("rooignore_error", ignoredFileAttemptedToAccess)
pushToolResult(formatResponse.rooIgnoreError(ignoredFileAttemptedToAccess))
return
}
// Clear any prior in-flight ask state before proceeding with execution.
// This prevents a stale ask from a previous command invocation (e.g., a
// shell integration fallback) from racing with the fresh approval prompt
// in the current invocation.
task.supersedePendingAsk()
task.consecutiveMistakeCount = 0
// Detect shell syntax errors (unterminated quotes, unclosed heredocs) before
// presenting the command for approval. Surfacing this as a tool error gives
// the agent a precise, actionable message so it can retry with a corrected
// command, rather than receiving a generic denial from the approval dialog.
const { parseError } = parseCommand(canonicalCommand)
if (parseError !== null) {
const executionId = task.lastMessageTs?.toString() ?? Date.now().toString()
const provider = await task.providerRef.deref()
const errorStatus: CommandExecutionStatus = {
executionId,
status: "error",
message: parseError.message,
}
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(errorStatus) })
task.didToolFailInCurrentTurn = true
pushToolResult(formatResponse.toolError(parseError.message))
return
}
const didApprove = await askApproval("command", canonicalCommand)
if (!didApprove) {
return
}
const executionId = task.lastMessageTs?.toString() ?? Date.now().toString()
const provider = await task.providerRef.deref()
const providerState = await provider?.getState()
const { terminalShellIntegrationDisabled = true } = providerState ?? {}
// Get command execution timeout from VSCode configuration (in seconds)
const commandExecutionTimeoutSeconds = vscode.workspace
.getConfiguration(Package.name)
.get<number>("commandExecutionTimeout", 0)
// Get command timeout allowlist from VSCode configuration
const commandTimeoutAllowlist = vscode.workspace
.getConfiguration(Package.name)
.get<string[]>("commandTimeoutAllowlist", [])
// Check if command matches any prefix in the allowlist
const isCommandAllowlisted = commandTimeoutAllowlist.some((prefix) =>
canonicalCommand.startsWith(prefix.trim()),
)
// Convert seconds to milliseconds for internal use, but skip timeout if command is allowlisted
const commandExecutionTimeout = isCommandAllowlisted ? 0 : commandExecutionTimeoutSeconds * 1000
// Convert agent-specified timeout from seconds to milliseconds
const agentTimeout = resolveAgentTimeoutMs(timeoutSeconds)
const options: ExecuteCommandOptions = {
executionId,
command: canonicalCommand,
customCwd,
terminalShellIntegrationDisabled,
commandExecutionTimeout,
agentTimeout,
}
try {
const [rejected, result] = await executeCommandInTerminal(task, options)
} catch (error: unknown) {
// Invalidate pending ask from first execution to prevent race condition
task.supersedePendingAsk()
if (canRetryShellIntegrationError(error)) {
// Silent retry via execa — shell startup race, command was not submitted.
const status: CommandExecutionStatus = { executionId, status: "fallback" }
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
const [rejected, result] = await executeCommandInTerminal(task, {
...options,
terminalShellIntegrationDisabled: true,
})
if (rejected) {
task.didRejectTool = true
}
// Fix: Mark the first execution result as consumed so we don't send
// duplicate pushToolResult calls. The shell integration retry below
// will produce the single authoritative result.
// Also await onCompletedPromise to avoid a race where the first
// command's onCompleted fires after we've already started the retry,
// corrupting shared state (completed, persistedResult, etc.).
if (!rejected && !runInBackground) {
await onCompletedPromise
}
pushToolResult(result)
} catch (error: unknown) {
// Invalidate pending ask from first execution to prevent race condition
task.supersedePendingAsk()
if (canRetryShellIntegrationError(error)) {
// Silent retry via execa — shell startup race, command was not submitted.
const status: CommandExecutionStatus = { executionId, status: "fallback" }
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
// Fix: When retrying with shell integration fallback, ensure we use a
// fresh terminal rather than the VSCode terminal that may have a stale
// shell integration state. This prevents double-execution where the
// command runs both in the VSCode terminal AND via execa.
const retryOptions = {
...options,
terminalShellIntegrationDisabled: true,
forceNewTerminal: true,
}
const [rejected, result] = await executeCommandInTerminal(task, retryOptions)
if (rejected) {
task.didRejectTool = true
}
pushToolResult(result)
} else {
// Command was submitted but shell integration lost track of it — show warning.
await task.say("shell_integration_warning")
if (error instanceof ShellIntegrationError) {
pushToolResult(
formatResponse.toolError(
"Command was submitted in the terminal, but its output and completion status could not be tracked due to a shell integration error. The command may still be running. Do NOT re-run the command automatically — inspect the terminal manually and report the result.",
),
)
} else {
pushToolResult(
formatResponse.toolError(
`Command failed to execute in terminal: ${error instanceof Error ? error.message : String(error)}. Check the terminal state before retrying.`,
),
)
}
}
}
return
} catch (error) {
await handleError("executing command", error as Error)
return
}
}
override async handlePartial(task: Task, block: ToolUse<"execute_command">): Promise<void> {
const command = block.params.command
await task.ask("command", command ?? "", block.partial).catch(() => {})
}
}
export type ExecuteCommandOptions = {
executionId: string
command: string
customCwd?: string
terminalShellIntegrationDisabled?: boolean
commandExecutionTimeout?: number
agentTimeout?: number
/** When true, forces creation of a fresh terminal even for retries. */
forceNewTerminal?: boolean
}
export async function executeCommandInTerminal(
task: Task,
{
executionId,
command,
customCwd,
terminalShellIntegrationDisabled = true,
commandExecutionTimeout = 0,
agentTimeout = 0,
}: ExecuteCommandOptions,
): Promise<[boolean, ToolResponse]> {
// Convert milliseconds back to seconds for display purposes.
const commandExecutionTimeoutSeconds = commandExecutionTimeout / 1000
let workingDir: string
if (!customCwd) {
workingDir = task.cwd
} else if (path.isAbsolute(customCwd)) {
workingDir = customCwd
} else {
workingDir = path.resolve(task.cwd, customCwd)
}
try {
await fs.access(workingDir)
} catch (error) {
return [false, `Working directory '${workingDir}' does not exist.`]
}
let message: { text?: string; images?: string[] } | undefined
let runInBackground = false
let completed = false
let result: string = ""
let persistedResult: PersistedCommandOutput | undefined
let exitDetails: ExitCodeDetails | undefined
let shellIntegrationError: ShellIntegrationError | undefined
let hasAskedForCommandOutput = false
const { terminalProvider, isCmdExeFallback } = getTerminalProviderForExecution(terminalShellIntegrationDisabled)
const provider = await task.providerRef.deref()
// cmd.exe can't use shell integration — tell the webview to expand the output
// panel immediately (same effect as the retry-fallback path).
if (isCmdExeFallback) {
const status: CommandExecutionStatus = { executionId, status: "fallback" }
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
}
// Get global storage path for persisted output artifacts
const globalStoragePath = provider?.context?.globalStorageUri?.fsPath
let interceptor: OutputInterceptor | undefined
// Create OutputInterceptor if we have storage available
if (globalStoragePath) {
const taskDir = await getTaskDirectoryPath(globalStoragePath, task.taskId)
const storageDir = path.join(taskDir, "command-output")
const providerState = await provider?.getState()
const terminalOutputPreviewSize =
providerState?.terminalOutputPreviewSize ?? DEFAULT_TERMINAL_OUTPUT_PREVIEW_SIZE
interceptor = new OutputInterceptor({
executionId,
taskId: task.taskId,
command,
storageDir,
previewSize: terminalOutputPreviewSize,
})
}
let accumulatedOutput = ""
// Bound accumulated output buffer size to prevent unbounded memory growth for long-running commands.
// The interceptor preserves full output; this buffer is only for UI display (100KB limit).
const maxAccumulatedOutputSize = 100_000
const commandOutputStreamThrottleMs = 150
let latestCompressedOutput = ""
let lastQueuedCommandOutput = ""
let lastCommandOutputEmitAt = 0
let pendingCommandOutputEmitTimer: NodeJS.Timeout | undefined
let commandOutputSayChain: Promise<void> = Promise.resolve()
const queueCommandOutputMessage = (text: string, partial: boolean, force = false): Promise<void> => {
if (!force && text === lastQueuedCommandOutput) {
return commandOutputSayChain
}
lastQueuedCommandOutput = text
commandOutputSayChain = commandOutputSayChain
.then(async () => {
await task.say("command_output", text, undefined, partial, undefined, undefined, {
isNonInteractive: true,
})
})
.catch((error) => {
console.error("[ExecuteCommandTool] Failed to publish command output:", error)
})
return commandOutputSayChain
}
const schedulePartialCommandOutputUpdate = () => {
if (!latestCompressedOutput || completed) {
return
}
const emitUpdate = () => {
pendingCommandOutputEmitTimer = undefined
lastCommandOutputEmitAt = Date.now()
void queueCommandOutputMessage(latestCompressedOutput, true)
}
const elapsed = Date.now() - lastCommandOutputEmitAt
if (elapsed >= commandOutputStreamThrottleMs) {
emitUpdate()
return
}
if (!pendingCommandOutputEmitTimer) {
pendingCommandOutputEmitTimer = setTimeout(emitUpdate, commandOutputStreamThrottleMs - elapsed)
}
}
// Track when onCompleted callback finishes to avoid race condition.
// The callback is async but Terminal/ExecaTerminal don't await it, so we track completion
// explicitly to ensure persistedResult is set before we use it.
let resolveOnCompleted: (() => void) | undefined
const onCompletedPromise = new Promise<void>((resolve) => {
resolveOnCompleted = resolve
})
const callbacks: RooTerminalCallbacks = {
onLine: async (lines: string, process: RooTerminalProcess) => {
accumulatedOutput += lines
// Trim accumulated output to prevent unbounded memory growth
if (accumulatedOutput.length > maxAccumulatedOutputSize) {
accumulatedOutput = accumulatedOutput.slice(-maxAccumulatedOutputSize)
}
// Write to interceptor for persisted output
interceptor?.write(lines)
// Continue sending compressed output to webview for UI display (unchanged behavior)
const compressedOutput = Terminal.compressTerminalOutput(accumulatedOutput)
latestCompressedOutput = compressedOutput
const status: CommandExecutionStatus = { executionId, status: "output", output: compressedOutput }
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
schedulePartialCommandOutputUpdate()
if (runInBackground || hasAskedForCommandOutput) {
return
}
// Mark that we've asked to prevent multiple concurrent asks
hasAskedForCommandOutput = true
try {
const { response, text, images } = await task.ask("command_output", "")
runInBackground = true
if (response === "messageResponse") {
message = { text, images }
process.continue()
}
} catch (_error) {
// Silently handle ask errors (e.g., "Current ask promise was ignored")
}
},
onCompleted: async (output: string | undefined) => {
clearTimeout(pendingCommandOutputEmitTimer)
pendingCommandOutputEmitTimer = undefined
try {
// Finalize interceptor and get persisted result.
// We await finalize() to ensure the artifact file is fully flushed
// before we advertise the artifact_id to the LLM.
if (interceptor) {
persistedResult = await interceptor.finalize()
}
} catch (error) {
console.error("[ExecuteCommandTool] interceptor.finalize() failed:", error)
}
// Continue using compressed output for UI display
result = Terminal.compressTerminalOutput(output ?? "")
latestCompressedOutput = result
completed = true
// Unblock the main code path: persistedResult, result, and completed are
// all set now. Resolve before draining the UI say chain so that a stalled
// or slow webview update cannot prevent the tool result from being returned.
resolveOnCompleted?.()
// Preserve order: wait for queued partial updates, then emit the final
// non-partial command_output update. Fire-and-forget from the main path —
// errors here are UI-only and must not surface to the tool result.
commandOutputSayChain
.then(() => queueCommandOutputMessage(result, false, true))
.catch((error) => {
console.error("[ExecuteCommandTool] Failed to flush final command_output:", error)
})
},
onShellExecutionStarted: (pid: number | undefined) => {
const status: CommandExecutionStatus = { executionId, status: "started", pid, command }
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
},
onShellExecutionComplete: (details: ExitCodeDetails) => {
const status: CommandExecutionStatus = { executionId, status: "exited", exitCode: details.exitCode }
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
exitDetails = details
},
}
if (terminalProvider === "vscode") {
callbacks.onNoShellIntegration = async (details: ShellIntegrationErrorDetails) => {
TelemetryService.instance.captureShellIntegrationError(task.taskId)
shellIntegrationError = new ShellIntegrationError(details.message, details.commandSubmitted)
}
}
// Fix: Use getOrCreateCommandTerminal to ensure each execute_command gets a
// dedicated terminal, preventing output interleaving when multiple commands
// run sequentially on the same working directory.
const terminal = await TerminalRegistry.getOrCreateCommandTerminal(workingDir, task.taskId, terminalProvider)
if (terminal instanceof Terminal) {
terminal.terminal.show(true)
// Update the working directory in case the terminal we asked for has
// a different working directory so that the model will know where the
// command actually executed.
workingDir = terminal.getCurrentWorkingDirectory()
}
const process = terminal.runCommand(command, callbacks)
task.terminalProcess = process
// Dual-timeout logic:
// - Agent timeout: transitions the command to background (continues running)
// - User timeout: aborts the command (kills it)
// Both timers run independently — the user timeout remains active as a safety net
// even after the agent timeout moves the command to the background.
let agentTimeoutId: NodeJS.Timeout | undefined
let userTimeoutId: NodeJS.Timeout | undefined
let isUserTimedOut = false
try {
const racers: Promise<void>[] = [process]
// Agent timeout: transition to background (command keeps running)
if (agentTimeout > 0) {
racers.push(
new Promise<void>((resolve) => {
agentTimeoutId = setTimeout(() => {
runInBackground = true
process.continue()
task.supersedePendingAsk()
resolve()
}, agentTimeout)
}),
)
}
// User timeout: abort the command (existing behavior)
if (commandExecutionTimeout > 0) {
racers.push(
new Promise<void>((_, reject) => {
userTimeoutId = setTimeout(() => {
// Fix timeout race: Only fire the timeout if the command hasn't
// already completed. Check `completed` under a microtask to avoid
// a race where onCompleted sets completed=true between the timeout
// firing and this check.
if (completed) {
resolve()
return
}
isUserTimedOut = true
task.terminalProcess?.abort()
reject(new Error(`Command execution timed out after ${commandExecutionTimeout}ms`))
}, commandExecutionTimeout)
}),
)
}
// Fix timeout race: After Promise.race resolves (either process completed,
// agent timeout, or user timeout), ensure the other timeout is cleared
// immediately. Without this, a user timeout could fire AFTER the command
// has already completed, incorrectly aborting it.
clearTimeout(agentTimeoutId)
agentTimeoutId = undefined
clearTimeout(userTimeoutId)
userTimeoutId = undefined
} catch (error) {
if (isUserTimedOut) {
const status: CommandExecutionStatus = { executionId, status: "timeout" }
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
await task.say("error", t("common:errors:command_timeout", { seconds: commandExecutionTimeoutSeconds }))
task.didToolFailInCurrentTurn = true
task.terminalProcess = undefined
return [
false,
`The command was terminated after exceeding a user-configured ${commandExecutionTimeoutSeconds}s timeout. Do not try to re-run the command.`,
]
}
throw error
} finally {
clearTimeout(agentTimeoutId)
clearTimeout(userTimeoutId)
clearTimeout(pendingCommandOutputEmitTimer)
task.terminalProcess = undefined
}
if (shellIntegrationError) {
throw shellIntegrationError
}
// Wait for a short delay to ensure all messages are sent to the webview.
// This delay allows time for non-awaited promises to be created and
// for their associated messages to be sent to the webview, maintaining
// the correct order of messages (although the webview is smart about
// grouping command_output messages despite any gaps anyways).
await delay(50)
// Wait for onCompleted callback to finish. onCompleted is async and sets
// `completed` and `persistedResult`; we must not read them until it resolves.
// Skip when returning a background result: the command is still running and
// onCompleted will fire later — awaiting it here would block until real completion,
// defeating the purpose of the agent-timeout background transition.
if (!runInBackground) {
await onCompletedPromise
}
// If the command completed during the onCompleted wait (e.g., a very fast
// command that finished before the timeout was set), make sure the timeout
// race fix above didn't leave a dangling timeout that could fire later.
clearTimeout(agentTimeoutId)
clearTimeout(userTimeoutId)
if (message) {
const { text, images } = message
await task.say("user_feedback", text, images)
return [
true,
formatResponse.toolResult(
[
`Command is still running in terminal from '${terminal.getCurrentWorkingDirectory().toPosix()}'.`,
result.length > 0 ? `Here's the output so far:\n${result}\n` : "\n",
`<user_message>\n${text}\n</user_message>`,
].join("\n"),
images,
),
]
} else if (completed || exitDetails) {
const currentWorkingDir = terminal.getCurrentWorkingDirectory().toPosix()
// Use persisted output format when output was truncated and spilled to disk
if (persistedResult?.truncated) {
return [false, formatPersistedOutput(persistedResult, exitDetails, currentWorkingDir)]
}
// Use inline format for small outputs (original behavior with exit status).
if (exitDetails === undefined) {
result += "<VSCE exitDetails == undefined: terminal output and command execution status is unknown.>"
} else if (!exitDetails.signalName && exitDetails.exitCode === undefined) {
result += "<VSCE exit code is undefined: terminal output and command execution status is unknown.>"
}
const exitStatus = formatExitStatus(exitDetails)
return [
false,
`Command executed in terminal within working directory '${currentWorkingDir}'. ${exitStatus}\nOutput:\n${result}`,
]
} else {
return [
false,
[
`Command is still running in terminal ${workingDir ? ` from '${workingDir.toPosix()}'` : ""}.`,
result.length > 0 ? `Here's the output so far:\n${result}\n` : "\n",
"You will be updated on the terminal status and new output in the future.",
].join("\n"),
]
}
}
/**
* Format exit status from ExitCodeDetails
*/
function formatExitStatus(exitDetails: ExitCodeDetails | undefined): string {
if (exitDetails === undefined) {
return "Exit code: <undefined, notify user>"
}
if (exitDetails.signalName) {
let status = `Process terminated by signal ${exitDetails.signalName}`
if (exitDetails.coreDumpPossible) {
status += " - core dump possible"
}
return status
}
if (exitDetails.exitCode === undefined) {
return "Exit code: <undefined, notify user>"
}
let status = ""
if (exitDetails.exitCode !== 0) {
status += "Command execution was not successful, inspect the cause and adjust as needed.\n"
}
status += `Exit code: ${exitDetails.exitCode}`
return status
}
/**
* Format persisted output result for tool response when output was truncated
*/
function formatPersistedOutput(
result: PersistedCommandOutput,
exitDetails: ExitCodeDetails | undefined,
workingDir: string,
): string {
const exitStatus = formatExitStatus(exitDetails)
const sizeStr = formatBytes(result.totalBytes)
const artifactId = result.artifactPath ? path.basename(result.artifactPath) : ""
return [
`Command executed in '${workingDir}'. ${exitStatus}`,
"",
`Output (${sizeStr}) persisted. Artifact ID: ${artifactId}`,
"",
"Preview:",
result.preview,
"",
"Use read_command_output tool to view full output if needed.",
].join("\n")
}
/**
* Format bytes to human-readable string
*/
function formatBytes(bytes: number): string {
if (bytes < 1024) {
return `${bytes}B`
}
if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(1)}KB`
}
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`
}
export const executeCommandTool = new ExecuteCommandTool()