Open History to see the last commit
1import { type ChildProcess, spawn } from "node:child_process";
2import { randomUUID } from "node:crypto";
3import { EventEmitter } from "node:events";
4
5import {
6 type AgentConfig,
7 type AgentExit,
8 type OutputChunk,
9 agentConfigSchema,
10} from "@acme/contracts";
11import type { IPty } from "node-pty";
12
13export interface ProcessManagerEvents {
14 output: [chunk: OutputChunk];
15 exit: [exit: AgentExit];
16}
17
18export class ProcessManager extends EventEmitter<ProcessManagerEvents> {
19 private sessions = new Map<string, ChildProcess>();
20 private ptySessions = new Map<string, IPty>();
21
22 spawn(raw: AgentConfig): string {
23 const config = agentConfigSchema.parse(raw);
24 const sessionId = randomUUID();
25
26 if (config.usePty) {
27 return this.spawnPty(sessionId, config);
28 }
29
30 return this.spawnProcess(sessionId, config);
31 }
32
33 private spawnProcess(sessionId: string, config: AgentConfig): string {
34 const child = spawn(config.command, config.args, {
35 cwd: config.cwd,
36 env: config.env ? { ...process.env, ...config.env } : process.env,
37 stdio: ["pipe", "pipe", "pipe"],
38 });
39
40 this.sessions.set(sessionId, child);
41
42 child.stdout?.on("data", (data: Buffer) => {
43 this.emit("output", {
44 sessionId,
45 stream: "stdout",
46 data: data.toString(),
47 });
48 });
49
50 child.stderr?.on("data", (data: Buffer) => {
51 this.emit("output", {
52 sessionId,
53 stream: "stderr",
54 data: data.toString(),
55 });
56 });
57
58 child.on("exit", (code, signal) => {
59 this.sessions.delete(sessionId);
60 this.emit("exit", {
61 sessionId,
62 code: code ?? null,
63 signal: signal ?? null,
64 });
65 });
66
67 return sessionId;
68 }
69
70 private spawnPty(sessionId: string, config: AgentConfig): string {
71 const pty = require("node-pty") as typeof import("node-pty");
72
73 const ptyProcess = pty.spawn(config.command, config.args, {
74 name: "xterm-256color",
75 cols: 120,
76 rows: 30,
77 cwd: config.cwd ?? process.cwd(),
78 env: (config.env ? { ...process.env, ...config.env } : process.env) as Record<string, string>,
79 });
80
81 this.ptySessions.set(sessionId, ptyProcess);
82
83 ptyProcess.onData((data) => {
84 this.emit("output", {
85 sessionId,
86 stream: "stdout",
87 data,
88 });
89 });
90
91 ptyProcess.onExit(({ exitCode, signal }) => {
92 this.ptySessions.delete(sessionId);
93 this.emit("exit", {
94 sessionId,
95 code: exitCode,
96 signal: signal !== undefined ? String(signal) : null,
97 });
98 });
99
100 return sessionId;
101 }
102
103 write(sessionId: string, data: string): void {
104 const child = this.sessions.get(sessionId);
105 if (child) {
106 child.stdin?.write(data);
107 return;
108 }
109
110 const pty = this.ptySessions.get(sessionId);
111 if (pty) {
112 pty.write(data);
113 return;
114 }
115
116 throw new Error(`No session: ${sessionId}`);
117 }
118
119 kill(sessionId: string): void {
120 const child = this.sessions.get(sessionId);
121 if (child) {
122 child.kill();
123 return;
124 }
125
126 const pty = this.ptySessions.get(sessionId);
127 if (pty) {
128 pty.kill();
129 return;
130 }
131 }
132
133 killAll(): void {
134 for (const child of this.sessions.values()) {
135 child.kill();
136 }
137 this.sessions.clear();
138
139 for (const pty of this.ptySessions.values()) {
140 pty.kill();
141 }
142 this.ptySessions.clear();
143 }
144}