Repository navigation
Expand file tree
/
Copy pathterminal-manager.ts
More file actions
332 lines (291 loc) · 7.89 KB
/
Copy pathterminal-manager.ts
File metadata and controls
332 lines (291 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import crypto from "node:crypto";
import { stripVTControlCharacters } from "node:util";
import type { Terminal as XtermTerminalType } from "@xterm/headless";
import xterm from "@xterm/headless";
import * as pty from "node-pty";
class WriteQueue {
private queue = Promise.resolve();
enqueue(writeFn: () => Promise<void> | void): void {
this.queue = this.queue
.then(() => writeFn())
.catch((error) => {
console.error("WriteQueue error:", error);
});
}
async drain(): Promise<void> {
await this.queue;
}
}
export interface ManagedTerminal {
id: string;
command: string;
cwd: string;
process: pty.IPty;
terminal: XtermTerminalType;
startedAt: Date;
rawOutput: string;
lastStreamReadPosition: number;
terminalWriteQueue: WriteQueue;
ptyWriteQueue: WriteQueue;
running: boolean;
exitCode?: number;
}
export class TerminalManager {
private processes = new Map<string, ManagedTerminal>();
private outputHandlers = new Map<string, (sessionId: string, data: string) => void>();
/**
* Start a new process with virtual terminal
*/
async start(command: string, options?: { cwd?: string; name?: string }): Promise<string> {
const id = options?.name || `proc-${crypto.randomBytes(6).toString("hex")}`;
if (this.processes.has(id)) {
throw new Error(`Session '${id}' already exists`);
}
const terminal = new xterm.Terminal({
cols: 80,
rows: 24,
scrollback: 10000,
allowProposedApi: true,
convertEol: true,
});
const proc = pty.spawn(process.env.SHELL || "/bin/bash", ["-c", command], {
name: "xterm-256color",
cols: 80,
rows: 24,
cwd: options?.cwd || process.cwd(),
env: {
...process.env,
TERM: "xterm-256color",
COLORTERM: "truecolor",
FORCE_COLOR: "1",
} as { [key: string]: string },
});
const processEntry: ManagedTerminal = {
id,
command,
cwd: options?.cwd || process.cwd(),
process: proc,
terminal,
startedAt: new Date(),
rawOutput: "",
lastStreamReadPosition: 0,
terminalWriteQueue: new WriteQueue(),
ptyWriteQueue: new WriteQueue(),
running: true,
};
proc.onData((data) => {
processEntry.terminalWriteQueue.enqueue(async () => {
processEntry.rawOutput += data;
await new Promise<void>((resolve) => {
terminal.write(data, () => resolve());
});
const handler = this.outputHandlers.get(id);
if (handler) {
handler(id, data);
}
});
});
proc.onExit((exitCode) => {
const code = exitCode.exitCode;
const signal = exitCode.signal;
const exitMsg = `\n[Process exited with code ${code}${signal ? ` (signal: ${signal})` : ""}]\n`;
// Mark process as not running
processEntry.running = false;
processEntry.exitCode = code;
processEntry.terminalWriteQueue.enqueue(async () => {
processEntry.rawOutput += exitMsg;
await new Promise<void>((resolve) => {
terminal.write(exitMsg, () => resolve());
});
});
});
this.processes.set(id, processEntry);
return id;
}
/**
* Stop a process
*/
async stop(id: string): Promise<void> {
const proc = this.processes.get(id);
if (!proc) {
throw new Error(`Process not found: ${id}`);
}
proc.process.kill();
this.processes.delete(id);
}
/**
* Stop all processes
*/
async stopAll(): Promise<void> {
const ids = Array.from(this.processes.keys());
for (const id of ids) {
await this.stop(id);
}
}
/**
* Send input to a process
*/
async sendInput(id: string, data: string): Promise<void> {
const proc = this.processes.get(id);
if (!proc) {
throw new Error(`Session not found: ${id}`);
}
if (!proc.running) {
throw new Error(
`Session ${id} is not running (pid: ${proc.process.pid}, exit code: ${proc.exitCode ?? "unknown"}). Check stdout or stream.`,
);
}
// Scan through string and handle \r specially (unless it's part of \r\n)
// This helps with some TUI apps that need \r sent separately
let buffer = "";
for (let i = 0; i < data.length; i++) {
if (data[i] === "\r") {
// Check if next character is \n (Windows line ending)
if (i + 1 < data.length && data[i + 1] === "\n") {
// It's \r\n, keep them together
buffer += "\r\n";
i++; // Skip the \n since we already added it
} else {
// It's a standalone \r, send buffer then \r separately
if (buffer) {
const bufferCopy = buffer; // Capture buffer value for closure
proc.ptyWriteQueue.enqueue(() => {
proc.process.write(bufferCopy);
});
buffer = "";
}
// Send \r separately with a small delay
proc.ptyWriteQueue.enqueue(async () => {
// Small delay helps some TUIs recognize \r as Enter
await new Promise((resolve) => setTimeout(resolve, 200));
proc.process.write("\r");
});
}
} else {
buffer += data[i];
}
}
// Send any remaining buffer
if (buffer) {
const bufferCopy = buffer; // Capture buffer value for closure
proc.ptyWriteQueue.enqueue(() => {
proc.process.write(bufferCopy);
});
}
await proc.ptyWriteQueue.drain();
}
/**
* Get terminal output (rendered)
*/
async getOutput(id: string, options?: { lines?: number }): Promise<string> {
const proc = this.processes.get(id);
if (!proc) {
throw new Error(`Process not found: ${id}`);
}
// Ensure terminal writes are complete
await proc.terminalWriteQueue.drain();
const buffer = proc.terminal.buffer.active;
const lines = [];
const endRow = buffer.length;
// Get all lines first
for (let i = 0; i < endRow; i++) {
const line = buffer.getLine(i);
if (line) {
lines.push(line.translateToString(true));
}
}
// Remove trailing empty lines
while (lines.length > 0 && lines[lines.length - 1].trim() === "") {
lines.pop();
}
// Apply line limit after removing empty lines
if (options?.lines && lines.length > options.lines) {
return lines.slice(-options.lines).join("\n");
}
return lines.join("\n");
}
/**
* Get raw output stream
*/
async getStream(id: string, options?: { since_last?: boolean; strip_ansi?: boolean }): Promise<string> {
const proc = this.processes.get(id);
if (!proc) {
throw new Error(`Process not found: ${id}`);
}
let output: string;
if (options?.since_last) {
output = proc.rawOutput.substring(proc.lastStreamReadPosition);
proc.lastStreamReadPosition = proc.rawOutput.length;
} else {
output = proc.rawOutput;
}
// Strip ANSI codes by default (can be disabled by setting strip_ansi: false)
if (options?.strip_ansi !== false && output) {
output = stripVTControlCharacters(output);
}
return output;
}
/**
* Get terminal size
*/
getTerminalSize(id: string): { rows: number; cols: number; scrollback_lines: number } {
const proc = this.processes.get(id);
if (!proc) {
throw new Error(`Process not found: ${id}`);
}
return {
rows: proc.terminal.rows,
cols: proc.terminal.cols,
scrollback_lines: proc.terminal.buffer.active.length,
};
}
/**
* Resize terminal
*/
resizeTerminal(id: string, cols: number, rows: number): void {
const proc = this.processes.get(id);
if (!proc) {
throw new Error(`Process not found: ${id}`);
}
proc.process.resize(cols, rows);
proc.terminal.resize(cols, rows);
}
/**
* List all processes
*/
listProcesses(): Array<{
id: string;
command: string;
cwd: string;
startedAt: string;
running: boolean;
pid?: number;
}> {
return Array.from(this.processes.values()).map((p) => ({
id: p.id,
command: p.command,
cwd: p.cwd,
startedAt: p.startedAt.toISOString(),
running: p.running,
pid: p.process.pid,
}));
}
/**
* Get a specific process
*/
getProcess(id: string): ManagedTerminal | undefined {
return this.processes.get(id);
}
/**
* Register an output handler
*/
onOutput(sessionId: string, handler: (sessionId: string, data: string) => void): void {
this.outputHandlers.set(sessionId, handler);
}
/**
* Unregister an output handler
*/
offOutput(sessionId: string): void {
this.outputHandlers.delete(sessionId);
}
}