UNPKG

@openai/agents-core

Version:

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.

88 lines • 3.05 kB
import { spawn } from 'node:child_process'; const DEFAULT_MAX_OUTPUT_BYTES = 1024 * 1024; const FORCE_KILL_DELAY_MS = 5_000; export async function runSandboxProcess(command, args, options = {}) { return new Promise((resolve) => { const stdoutChunks = []; const stderrChunks = []; let stdoutBytes = 0; let stderrBytes = 0; let error; let timedOut = false; let closed = false; let settled = false; let timeout; let forceKillTimeout; const maxOutputBytes = options.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES; const child = spawn(command, args, { cwd: options.cwd, env: options.env, uid: options.uid, gid: options.gid, stdio: ['ignore', 'pipe', 'pipe'], }); const captureChunk = (chunks, currentBytes, chunk) => { const remainingBytes = maxOutputBytes - currentBytes; if (remainingBytes <= 0) { return currentBytes; } const captured = chunk.length > remainingBytes ? chunk.subarray(0, remainingBytes) : chunk; chunks.push(captured); return currentBytes + captured.length; }; const finish = (status, signal) => { if (settled) { return; } settled = true; closed = true; if (timeout) { clearTimeout(timeout); } if (forceKillTimeout) { clearTimeout(forceKillTimeout); } resolve({ status, signal, stdout: Buffer.concat(stdoutChunks).toString('utf8'), stderr: Buffer.concat(stderrChunks).toString('utf8'), error, timedOut, }); }; child.stdout.on('data', (chunk) => { stdoutBytes = captureChunk(stdoutChunks, stdoutBytes, chunk); }); child.stderr.on('data', (chunk) => { stderrBytes = captureChunk(stderrChunks, stderrBytes, chunk); }); child.on('error', (childError) => { error = childError; finish(null, null); }); if (options.timeoutMs !== undefined) { timeout = setTimeout(() => { timedOut = true; child.kill('SIGTERM'); forceKillTimeout = setTimeout(() => { if (!closed) { child.kill('SIGKILL'); } }, FORCE_KILL_DELAY_MS); }, options.timeoutMs); } child.on('close', (status, signal) => { finish(status, signal); }); }); } export function formatSandboxProcessError(result) { if (result.timedOut) { return 'process timed out'; } return (result.stderr || result.stdout || result.error?.message || 'process failed'); } //# sourceMappingURL=runProcess.mjs.map