Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
3.5 kB · 114 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115/** * Bash command execution with streaming support and cancellation. * * This module provides a unified bash execution implementation used by: * - AgentSession.executeBash() for interactive and RPC modes * - Direct calls from modes that need bash execution */
import stripAnsi from "strip-ansi";import { sanitizeBinaryOutput } from "../utils/shell.js";import type { BashOperations } from "./tools/bash.js";import { OutputSpill } from "./tools/output-accumulator.js";import { DEFAULT_MAX_BYTES, truncateTail } from "./tools/truncate.js";export interface BashExecutorOptions { /** Callback for streaming output chunks (already sanitized) */ onChunk?: (chunk: string) => void; /** AbortSignal for cancellation */ signal?: AbortSignal;}
export interface BashResult { /** Combined stdout + stderr output (sanitized, possibly truncated) */ output: string; /** Process exit code (undefined if killed/cancelled) */ exitCode: number | undefined; /** Whether the command was cancelled via signal */ cancelled: boolean; /** Whether the output was truncated */ truncated: boolean; /** Path to temp file containing full output (if output exceeded truncation threshold) */ fullOutputPath?: string;}/** * Execute a bash command using custom BashOperations. * Used for remote execution (SSH, containers, etc.). */export async function executeBashWithOperations( command: string, cwd: string, operations: BashOperations, options?: BashExecutorOptions,): Promise<BashResult> { const outputChunks: string[] = []; let outputBytes = 0; const maxOutputBytes = DEFAULT_MAX_BYTES * 2;
const spill = new OutputSpill("pi-bash"); let totalBytes = 0;
const decoder = new TextDecoder();
const onData = (data: Buffer) => { totalBytes += data.length; const text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\r/g, ""); if (totalBytes > DEFAULT_MAX_BYTES) { spill.open(outputChunks); }
spill.write(text); outputChunks.push(text); outputBytes += text.length; while (outputBytes > maxOutputBytes && outputChunks.length > 1) { const removed = outputChunks.shift()!; outputBytes -= removed.length; } if (options?.onChunk) { options.onChunk(text); } };
try { const result = await operations.exec(command, cwd, { onData, signal: options?.signal, });
const fullOutput = outputChunks.join(""); const truncationResult = truncateTail(fullOutput); if (truncationResult.truncated) { spill.open(outputChunks); } // Settled before advertising: the path refers to the COMPLETE file or is undefined. const fullOutputPath = await spill.finalize(); const cancelled = options?.signal?.aborted ?? false;
return { output: truncationResult.truncated ? truncationResult.content : fullOutput, exitCode: cancelled ? undefined : (result.exitCode ?? undefined), cancelled, truncated: truncationResult.truncated, fullOutputPath, }; } catch (err) { if (options?.signal?.aborted) { const fullOutput = outputChunks.join(""); const truncationResult = truncateTail(fullOutput); if (truncationResult.truncated) { spill.open(outputChunks); } const fullOutputPath = await spill.finalize(); return { output: truncationResult.truncated ? truncationResult.content : fullOutput, exitCode: undefined, cancelled: true, truncated: truncationResult.truncated, fullOutputPath, }; }
void spill.finalize();
throw err; }}