Files
oh-my-pi/packages/coding-agent/src/exec/exec.ts
T
can1357 6260ad6216 refactor(pi-utils): simplified ptree API and consolidated spawn variants into single function
- Simplified ptree API by removing spawnGroup and spawnAttached variants, consolidating into single spawn function.
- Replaced AsyncQueue class with simpler pushStream utility function for stream management.
- Refactored ChildProcess class to use AbortController instead of callback-based signal handling.
- Renamed captureText method to wait and execText function to exec for clearer API semantics.
- Removed mode parameter from exec function, eliminating distinction between spawn modes.
- Updated all call sites across coding-agent to use simplified ptree API.
2026-01-28 23:49:15 +01:00

54 lines
1.0 KiB
TypeScript

/**
* Shared command execution utilities for hooks and custom tools.
*/
import { ptree } from "@oh-my-pi/pi-utils";
/**
* Options for executing shell commands.
*/
export interface ExecOptions {
/** AbortSignal to cancel the command */
signal?: AbortSignal;
/** Timeout in milliseconds */
timeout?: number;
/** Working directory */
cwd?: string;
}
/**
* Result of executing a shell command.
*/
export interface ExecResult {
stdout: string;
stderr: string;
code: number;
killed: boolean;
}
/**
* Execute a shell command and return stdout/stderr/code.
* Supports timeout and abort signal.
*/
export async function execCommand(
command: string,
args: string[],
cwd: string,
options?: ExecOptions,
): Promise<ExecResult> {
const result = await ptree.exec([command, ...args], {
cwd,
signal: options?.signal,
timeout: options?.timeout,
allowNonZero: true,
allowAbort: true,
stderr: "full",
});
return {
stdout: result.stdout,
stderr: result.stderr,
code: result.exitCode ?? 0,
killed: Boolean(result.exitError?.aborted),
};
}