UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

62 lines • 1.67 kB
import { Channel, JobConsole } from "../shell/console/index.js"; import { BytePipe, CAPACITY } from "../io/pipe.js"; import { PipeClosed } from "../io/errors.js"; import { OutputStream } from "../io/stream.js"; export class ProcessInput { pipe; closed = false; bytesRead = 0; constructor(capacity = CAPACITY) { this.pipe = new BytePipe(capacity); } async write(data) { if (data.byteLength === 0) return; if (this.closed) throw new PipeClosed(); await this.pipe.write(data); } close() { this.closed = true; this.pipe.end(); } stop() { this.close(); this.pipe.closeReader(); } async *read() { for await (const chunk of this.pipe.stream()) { this.bytesRead += chunk.byteLength; yield chunk; } } stream() { return new OutputStream(this.read(), () => { this.stop(); return Promise.resolve(); }); } } export class ProcessOutput extends JobConsole { mergeStderr; stdout; stderr; constructor(mergeStderr = false, bufferBytes = CAPACITY) { super(); this.mergeStderr = mergeStderr; this.stdout = new ProcessInput(bufferBytes); this.stderr = new ProcessInput(bufferBytes); } async emit(channel, data) { await (channel === Channel.STDERR && !this.mergeStderr ? this.stderr : this.stdout).write(data); } end() { this.stdout.close(); this.stderr.close(); } stop() { this.stdout.stop(); this.stderr.stop(); } } //# sourceMappingURL=stdio.js.map