terminal-pilot 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -0
- package/dist/ansi.js +68 -63
- package/dist/ansi.js.map +7 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +1807 -0
- package/dist/cli.js.map +7 -0
- package/dist/commands/close-session.d.ts +12 -0
- package/dist/commands/close-session.js +1264 -0
- package/dist/commands/close-session.js.map +7 -0
- package/dist/commands/create-session.d.ts +26 -0
- package/dist/commands/create-session.js +1271 -0
- package/dist/commands/create-session.js.map +7 -0
- package/dist/commands/fill.d.ts +10 -0
- package/dist/commands/fill.js +1267 -0
- package/dist/commands/fill.js.map +7 -0
- package/dist/commands/get-session.d.ts +18 -0
- package/dist/commands/get-session.js +1269 -0
- package/dist/commands/get-session.js.map +7 -0
- package/dist/commands/index.d.ts +242 -0
- package/dist/commands/index.js +1781 -0
- package/dist/commands/index.js.map +7 -0
- package/dist/commands/install.d.ts +20 -0
- package/dist/commands/install.js +119 -0
- package/dist/commands/install.js.map +7 -0
- package/dist/commands/installer.d.ts +43 -0
- package/dist/commands/installer.js +123 -0
- package/dist/commands/installer.js.map +7 -0
- package/dist/commands/list-sessions.d.ts +16 -0
- package/dist/commands/list-sessions.js +1268 -0
- package/dist/commands/list-sessions.js.map +7 -0
- package/dist/commands/press-key.d.ts +10 -0
- package/dist/commands/press-key.js +1267 -0
- package/dist/commands/press-key.js.map +7 -0
- package/dist/commands/read-history.d.ts +16 -0
- package/dist/commands/read-history.js +1266 -0
- package/dist/commands/read-history.js.map +7 -0
- package/dist/commands/read-screen.d.ts +21 -0
- package/dist/commands/read-screen.js +1270 -0
- package/dist/commands/read-screen.js.map +7 -0
- package/dist/commands/resize.d.ts +12 -0
- package/dist/commands/resize.js +1267 -0
- package/dist/commands/resize.js.map +7 -0
- package/dist/commands/runtime.d.ts +36 -0
- package/dist/commands/runtime.js +1256 -0
- package/dist/commands/runtime.js.map +7 -0
- package/dist/commands/screenshot.d.ts +14 -0
- package/dist/commands/screenshot.js +1274 -0
- package/dist/commands/screenshot.js.map +7 -0
- package/dist/commands/send-signal.d.ts +10 -0
- package/dist/commands/send-signal.js +1267 -0
- package/dist/commands/send-signal.js.map +7 -0
- package/dist/commands/type.d.ts +10 -0
- package/dist/commands/type.js +1267 -0
- package/dist/commands/type.js.map +7 -0
- package/dist/commands/uninstall.d.ts +14 -0
- package/dist/commands/uninstall.js +124 -0
- package/dist/commands/uninstall.js.map +7 -0
- package/dist/commands/wait-for-exit.d.ts +14 -0
- package/dist/commands/wait-for-exit.js +1268 -0
- package/dist/commands/wait-for-exit.js.map +7 -0
- package/dist/commands/wait-for.d.ts +20 -0
- package/dist/commands/wait-for.js +1275 -0
- package/dist/commands/wait-for.js.map +7 -0
- package/dist/exports.compile-check.js +1 -1
- package/dist/exports.compile-check.js.map +7 -0
- package/dist/index.js +1118 -5
- package/dist/index.js.map +7 -0
- package/dist/keys.js +58 -49
- package/dist/keys.js.map +7 -0
- package/dist/templates/terminal-pilot.md +45 -0
- package/dist/terminal-buffer.d.ts +9 -1
- package/dist/terminal-buffer.js +525 -425
- package/dist/terminal-buffer.js.map +7 -0
- package/dist/terminal-pilot.js +1105 -35
- package/dist/terminal-pilot.js.map +7 -0
- package/dist/terminal-screen.js +108 -26
- package/dist/terminal-screen.js.map +7 -0
- package/dist/terminal-session.js +1020 -292
- package/dist/terminal-session.js.map +7 -0
- package/dist/testing/cli-repl.d.ts +15 -0
- package/dist/testing/cli-repl.js +1911 -0
- package/dist/testing/cli-repl.js.map +7 -0
- package/dist/testing/qa-cli.d.ts +1 -0
- package/dist/testing/qa-cli.js +2828 -0
- package/dist/testing/qa-cli.js.map +7 -0
- package/package.json +26 -5
|
@@ -0,0 +1,1264 @@
|
|
|
1
|
+
// src/commands/close-session.ts
|
|
2
|
+
import { defineCommand, S } from "@poe-code/cmdkit";
|
|
3
|
+
|
|
4
|
+
// src/commands/runtime.ts
|
|
5
|
+
import { UserError } from "@poe-code/cmdkit";
|
|
6
|
+
|
|
7
|
+
// src/terminal-pilot.ts
|
|
8
|
+
import { randomUUID } from "node:crypto";
|
|
9
|
+
|
|
10
|
+
// src/terminal-session.ts
|
|
11
|
+
import { spawn as spawnChildProcess } from "node:child_process";
|
|
12
|
+
import { EventEmitter } from "node:events";
|
|
13
|
+
import * as nodePty from "node-pty";
|
|
14
|
+
|
|
15
|
+
// src/ansi.ts
|
|
16
|
+
var ESC = 27;
|
|
17
|
+
var BEL = 7;
|
|
18
|
+
var ST = 156;
|
|
19
|
+
var CSI = 155;
|
|
20
|
+
var OSC = 157;
|
|
21
|
+
var DCS = 144;
|
|
22
|
+
var SOS = 152;
|
|
23
|
+
var PM = 158;
|
|
24
|
+
var APC = 159;
|
|
25
|
+
function stripAnsi(input) {
|
|
26
|
+
let output = "";
|
|
27
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
28
|
+
const code = input.charCodeAt(index);
|
|
29
|
+
if (code === ESC) {
|
|
30
|
+
const nextCode = input.charCodeAt(index + 1);
|
|
31
|
+
if (nextCode === 91) {
|
|
32
|
+
index = consumeCsi(input, index + 2);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (nextCode === 93) {
|
|
36
|
+
index = consumeTerminatedString(input, index + 2, true);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (nextCode === 80 || nextCode === 88 || nextCode === 94 || nextCode === 95) {
|
|
40
|
+
index = consumeTerminatedString(input, index + 2, false);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (!Number.isNaN(nextCode)) {
|
|
44
|
+
index += 1;
|
|
45
|
+
}
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (code === CSI) {
|
|
49
|
+
index = consumeCsi(input, index + 1);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (code === OSC) {
|
|
53
|
+
index = consumeTerminatedString(input, index + 1, true);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (code === DCS || code === SOS || code === PM || code === APC) {
|
|
57
|
+
index = consumeTerminatedString(input, index + 1, false);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
output += input[index];
|
|
61
|
+
}
|
|
62
|
+
return output;
|
|
63
|
+
}
|
|
64
|
+
function consumeCsi(input, index) {
|
|
65
|
+
while (index < input.length) {
|
|
66
|
+
const code = input.charCodeAt(index);
|
|
67
|
+
if (code >= 64 && code <= 126) {
|
|
68
|
+
return index;
|
|
69
|
+
}
|
|
70
|
+
index += 1;
|
|
71
|
+
}
|
|
72
|
+
return input.length;
|
|
73
|
+
}
|
|
74
|
+
function consumeTerminatedString(input, index, allowBellTerminator) {
|
|
75
|
+
while (index < input.length) {
|
|
76
|
+
const code = input.charCodeAt(index);
|
|
77
|
+
if (code === ST || allowBellTerminator && code === BEL) {
|
|
78
|
+
return index;
|
|
79
|
+
}
|
|
80
|
+
if (code === ESC && input.charCodeAt(index + 1) === 92) {
|
|
81
|
+
return index + 1;
|
|
82
|
+
}
|
|
83
|
+
index += 1;
|
|
84
|
+
}
|
|
85
|
+
return input.length;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/terminal-buffer.ts
|
|
89
|
+
var RESET_SGR = "\x1B[0m";
|
|
90
|
+
var TerminalBuffer = class {
|
|
91
|
+
_cols;
|
|
92
|
+
_rows;
|
|
93
|
+
_screen;
|
|
94
|
+
_cursorX = 0;
|
|
95
|
+
_cursorY = 0;
|
|
96
|
+
_savedCursor = { x: 0, y: 0 };
|
|
97
|
+
_scrollTop = 0;
|
|
98
|
+
_scrollBottom;
|
|
99
|
+
_state = 0 /* Normal */;
|
|
100
|
+
_csiParams = "";
|
|
101
|
+
_csiPrivate = "";
|
|
102
|
+
_style = createDefaultStyleState();
|
|
103
|
+
_styleSequence = "";
|
|
104
|
+
displayBuffer;
|
|
105
|
+
constructor(cols, rows) {
|
|
106
|
+
this._cols = cols;
|
|
107
|
+
this._rows = rows;
|
|
108
|
+
this._scrollBottom = rows - 1;
|
|
109
|
+
this._screen = this._makeScreen(cols, rows);
|
|
110
|
+
this.displayBuffer = Object.defineProperties(
|
|
111
|
+
{},
|
|
112
|
+
{
|
|
113
|
+
cursorX: { get: () => this._cursorX, enumerable: true },
|
|
114
|
+
cursorY: { get: () => this._cursorY, enumerable: true },
|
|
115
|
+
data: { get: () => this._screen, enumerable: true }
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
write(data) {
|
|
120
|
+
for (const ch of data) {
|
|
121
|
+
this._feed(ch);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
renderLine(row) {
|
|
125
|
+
const cells = this._screen[row] ?? [];
|
|
126
|
+
let lastVisibleCell = -1;
|
|
127
|
+
for (let index = cells.length - 1; index >= 0; index -= 1) {
|
|
128
|
+
if (cells[index] !== null) {
|
|
129
|
+
lastVisibleCell = index;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (lastVisibleCell === -1) {
|
|
134
|
+
return "";
|
|
135
|
+
}
|
|
136
|
+
let line = "";
|
|
137
|
+
let activeStyle = "";
|
|
138
|
+
for (let index = 0; index <= lastVisibleCell; index += 1) {
|
|
139
|
+
const cell = cells[index];
|
|
140
|
+
const cellStyle = cell?.style ?? "";
|
|
141
|
+
if (cellStyle !== activeStyle) {
|
|
142
|
+
line += cellStyle.length > 0 ? cellStyle : RESET_SGR;
|
|
143
|
+
activeStyle = cellStyle;
|
|
144
|
+
}
|
|
145
|
+
line += cell?.[1] ?? " ";
|
|
146
|
+
}
|
|
147
|
+
if (activeStyle.length > 0) {
|
|
148
|
+
line += RESET_SGR;
|
|
149
|
+
}
|
|
150
|
+
return line;
|
|
151
|
+
}
|
|
152
|
+
resize(cols, rows) {
|
|
153
|
+
while (this._screen.length < rows) {
|
|
154
|
+
this._screen.push(this._makeRow(cols));
|
|
155
|
+
}
|
|
156
|
+
this._screen.length = rows;
|
|
157
|
+
for (let y = 0; y < rows; y++) {
|
|
158
|
+
const row = this._screen[y] ?? this._makeRow(cols);
|
|
159
|
+
while (row.length < cols) row.push(null);
|
|
160
|
+
row.length = cols;
|
|
161
|
+
this._screen[y] = row;
|
|
162
|
+
}
|
|
163
|
+
this._cols = cols;
|
|
164
|
+
this._rows = rows;
|
|
165
|
+
this._scrollTop = 0;
|
|
166
|
+
this._scrollBottom = rows - 1;
|
|
167
|
+
this._cursorX = this._clamp(this._cursorX, 0, cols - 1);
|
|
168
|
+
this._cursorY = this._clamp(this._cursorY, 0, rows - 1);
|
|
169
|
+
}
|
|
170
|
+
_makeScreen(cols, rows) {
|
|
171
|
+
return Array.from({ length: rows }, () => this._makeRow(cols));
|
|
172
|
+
}
|
|
173
|
+
_makeRow(cols) {
|
|
174
|
+
return Array(cols).fill(null);
|
|
175
|
+
}
|
|
176
|
+
_clamp(value, min, max) {
|
|
177
|
+
return Math.max(min, Math.min(max, value));
|
|
178
|
+
}
|
|
179
|
+
_setChar(y, x, ch) {
|
|
180
|
+
const row = this._screen[y];
|
|
181
|
+
if (row && x >= 0 && x < this._cols) {
|
|
182
|
+
const cell = [ch.charCodeAt(0), ch];
|
|
183
|
+
if (this._styleSequence.length > 0) {
|
|
184
|
+
Object.defineProperty(cell, "style", {
|
|
185
|
+
value: this._styleSequence,
|
|
186
|
+
writable: true,
|
|
187
|
+
configurable: true
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
row[x] = cell;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
_eraseLine(y, fromX, toX) {
|
|
194
|
+
const row = this._screen[y];
|
|
195
|
+
if (!row) return;
|
|
196
|
+
for (let x = fromX; x <= toX && x < this._cols; x++) {
|
|
197
|
+
row[x] = null;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
_scrollUp(count) {
|
|
201
|
+
for (let i = 0; i < count; i++) {
|
|
202
|
+
this._screen.splice(this._scrollTop, 1);
|
|
203
|
+
this._screen.splice(this._scrollBottom, 0, this._makeRow(this._cols));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
_scrollDown(count) {
|
|
207
|
+
for (let i = 0; i < count; i++) {
|
|
208
|
+
this._screen.splice(this._scrollBottom, 1);
|
|
209
|
+
this._screen.splice(this._scrollTop, 0, this._makeRow(this._cols));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
_newline() {
|
|
213
|
+
if (this._cursorY === this._scrollBottom) {
|
|
214
|
+
this._scrollUp(1);
|
|
215
|
+
} else {
|
|
216
|
+
this._cursorY = Math.min(this._cursorY + 1, this._rows - 1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
_parseCsiParams() {
|
|
220
|
+
if (!this._csiParams) return [];
|
|
221
|
+
return this._csiParams.split(";").map((s) => s === "" ? 0 : parseInt(s, 10));
|
|
222
|
+
}
|
|
223
|
+
_execCsi(final) {
|
|
224
|
+
const params2 = this._parseCsiParams();
|
|
225
|
+
const p0 = params2[0] ?? 0;
|
|
226
|
+
const p1 = params2[1] ?? 0;
|
|
227
|
+
if (this._csiPrivate === "?") {
|
|
228
|
+
if (final === "h" || final === "l") {
|
|
229
|
+
if (params2.includes(1049)) {
|
|
230
|
+
if (final === "h") {
|
|
231
|
+
this._screen = this._makeScreen(this._cols, this._rows);
|
|
232
|
+
this._cursorX = 0;
|
|
233
|
+
this._cursorY = 0;
|
|
234
|
+
} else {
|
|
235
|
+
this._screen = this._makeScreen(this._cols, this._rows);
|
|
236
|
+
this._cursorX = 0;
|
|
237
|
+
this._cursorY = 0;
|
|
238
|
+
}
|
|
239
|
+
this._resetStyle();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
switch (final) {
|
|
245
|
+
case "A":
|
|
246
|
+
this._cursorY = this._clamp(this._cursorY - Math.max(1, p0), 0, this._rows - 1);
|
|
247
|
+
break;
|
|
248
|
+
case "B":
|
|
249
|
+
this._cursorY = this._clamp(this._cursorY + Math.max(1, p0), 0, this._rows - 1);
|
|
250
|
+
break;
|
|
251
|
+
case "C":
|
|
252
|
+
// cursor forward
|
|
253
|
+
case "a":
|
|
254
|
+
this._cursorX = this._clamp(this._cursorX + Math.max(1, p0), 0, this._cols - 1);
|
|
255
|
+
break;
|
|
256
|
+
case "D":
|
|
257
|
+
this._cursorX = this._clamp(this._cursorX - Math.max(1, p0), 0, this._cols - 1);
|
|
258
|
+
break;
|
|
259
|
+
case "E":
|
|
260
|
+
this._cursorY = this._clamp(this._cursorY + Math.max(1, p0), 0, this._rows - 1);
|
|
261
|
+
this._cursorX = 0;
|
|
262
|
+
break;
|
|
263
|
+
case "F":
|
|
264
|
+
this._cursorY = this._clamp(this._cursorY - Math.max(1, p0), 0, this._rows - 1);
|
|
265
|
+
this._cursorX = 0;
|
|
266
|
+
break;
|
|
267
|
+
case "G":
|
|
268
|
+
// cursor horizontal absolute
|
|
269
|
+
case "`":
|
|
270
|
+
this._cursorX = this._clamp(Math.max(1, p0) - 1, 0, this._cols - 1);
|
|
271
|
+
break;
|
|
272
|
+
case "H":
|
|
273
|
+
// cursor position
|
|
274
|
+
case "f":
|
|
275
|
+
this._cursorY = this._clamp(Math.max(1, p0) - 1, 0, this._rows - 1);
|
|
276
|
+
this._cursorX = this._clamp(Math.max(1, p1) - 1, 0, this._cols - 1);
|
|
277
|
+
break;
|
|
278
|
+
case "I":
|
|
279
|
+
for (let i = 0; i < Math.max(1, p0); i++) {
|
|
280
|
+
this._cursorX = Math.min(this._cols - 1, (Math.floor(this._cursorX / 8) + 1) * 8);
|
|
281
|
+
}
|
|
282
|
+
break;
|
|
283
|
+
case "J":
|
|
284
|
+
if (p0 === 0) {
|
|
285
|
+
this._eraseLine(this._cursorY, this._cursorX, this._cols - 1);
|
|
286
|
+
for (let y = this._cursorY + 1; y < this._rows; y++) this._eraseLine(y, 0, this._cols - 1);
|
|
287
|
+
} else if (p0 === 1) {
|
|
288
|
+
for (let y = 0; y < this._cursorY; y++) this._eraseLine(y, 0, this._cols - 1);
|
|
289
|
+
this._eraseLine(this._cursorY, 0, this._cursorX);
|
|
290
|
+
} else if (p0 === 2 || p0 === 3) {
|
|
291
|
+
for (let y = 0; y < this._rows; y++) this._eraseLine(y, 0, this._cols - 1);
|
|
292
|
+
}
|
|
293
|
+
break;
|
|
294
|
+
case "K":
|
|
295
|
+
if (p0 === 0) this._eraseLine(this._cursorY, this._cursorX, this._cols - 1);
|
|
296
|
+
else if (p0 === 1) this._eraseLine(this._cursorY, 0, this._cursorX);
|
|
297
|
+
else if (p0 === 2) this._eraseLine(this._cursorY, 0, this._cols - 1);
|
|
298
|
+
break;
|
|
299
|
+
case "X":
|
|
300
|
+
this._eraseLine(this._cursorY, this._cursorX, this._cursorX + Math.max(1, p0) - 1);
|
|
301
|
+
break;
|
|
302
|
+
case "L": {
|
|
303
|
+
const n = Math.max(1, p0);
|
|
304
|
+
for (let i = 0; i < n; i++) {
|
|
305
|
+
this._screen.splice(this._scrollBottom, 1);
|
|
306
|
+
this._screen.splice(this._cursorY, 0, this._makeRow(this._cols));
|
|
307
|
+
}
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
case "M": {
|
|
311
|
+
const n = Math.max(1, p0);
|
|
312
|
+
for (let i = 0; i < n; i++) {
|
|
313
|
+
this._screen.splice(this._cursorY, 1);
|
|
314
|
+
this._screen.splice(this._scrollBottom, 0, this._makeRow(this._cols));
|
|
315
|
+
}
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
case "P": {
|
|
319
|
+
const row = this._screen[this._cursorY];
|
|
320
|
+
if (row) {
|
|
321
|
+
const n = Math.max(1, p0);
|
|
322
|
+
row.splice(this._cursorX, n);
|
|
323
|
+
while (row.length < this._cols) row.push(null);
|
|
324
|
+
}
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
case "@": {
|
|
328
|
+
const row = this._screen[this._cursorY];
|
|
329
|
+
if (row) {
|
|
330
|
+
const n = Math.max(1, p0);
|
|
331
|
+
for (let i = 0; i < n; i++) row.splice(this._cursorX, 0, null);
|
|
332
|
+
row.splice(this._cols);
|
|
333
|
+
}
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
case "S":
|
|
337
|
+
this._scrollUp(Math.max(1, p0));
|
|
338
|
+
break;
|
|
339
|
+
case "T":
|
|
340
|
+
if (params2.length <= 1) this._scrollDown(Math.max(1, p0));
|
|
341
|
+
break;
|
|
342
|
+
case "Z": {
|
|
343
|
+
const n = Math.max(1, p0);
|
|
344
|
+
for (let i = 0; i < n; i++) {
|
|
345
|
+
this._cursorX = Math.max(0, (Math.ceil(this._cursorX / 8) - 1) * 8);
|
|
346
|
+
}
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
case "d":
|
|
350
|
+
this._cursorY = this._clamp(Math.max(1, p0) - 1, 0, this._rows - 1);
|
|
351
|
+
break;
|
|
352
|
+
case "e":
|
|
353
|
+
this._cursorY = this._clamp(this._cursorY + Math.max(1, p0), 0, this._rows - 1);
|
|
354
|
+
break;
|
|
355
|
+
case "r": {
|
|
356
|
+
const top = this._clamp(Math.max(1, p0) - 1, 0, this._rows - 1);
|
|
357
|
+
const bottom = this._clamp((p1 === 0 ? this._rows : p1) - 1, 0, this._rows - 1);
|
|
358
|
+
if (top < bottom) {
|
|
359
|
+
this._scrollTop = top;
|
|
360
|
+
this._scrollBottom = bottom;
|
|
361
|
+
}
|
|
362
|
+
this._cursorX = 0;
|
|
363
|
+
this._cursorY = 0;
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
case "s":
|
|
367
|
+
this._savedCursor = { x: this._cursorX, y: this._cursorY };
|
|
368
|
+
break;
|
|
369
|
+
case "u":
|
|
370
|
+
this._cursorX = this._clamp(this._savedCursor.x, 0, this._cols - 1);
|
|
371
|
+
this._cursorY = this._clamp(this._savedCursor.y, 0, this._rows - 1);
|
|
372
|
+
break;
|
|
373
|
+
case "m":
|
|
374
|
+
this._applySgr(params2);
|
|
375
|
+
break;
|
|
376
|
+
default:
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
_feed(ch) {
|
|
381
|
+
const code = ch.charCodeAt(0);
|
|
382
|
+
switch (this._state) {
|
|
383
|
+
case 0 /* Normal */:
|
|
384
|
+
this._feedNormal(ch, code);
|
|
385
|
+
break;
|
|
386
|
+
case 1 /* Escape */:
|
|
387
|
+
this._feedEscape(ch, code);
|
|
388
|
+
break;
|
|
389
|
+
case 2 /* Csi */:
|
|
390
|
+
this._feedCsi(ch, code);
|
|
391
|
+
break;
|
|
392
|
+
case 3 /* Osc */:
|
|
393
|
+
if (code === 7 || code === 156) {
|
|
394
|
+
this._state = 0 /* Normal */;
|
|
395
|
+
} else if (code === 27) {
|
|
396
|
+
this._state = 0 /* Normal */;
|
|
397
|
+
}
|
|
398
|
+
break;
|
|
399
|
+
case 4 /* Str */:
|
|
400
|
+
if (code === 156 || code === 7) {
|
|
401
|
+
this._state = 0 /* Normal */;
|
|
402
|
+
} else if (code === 27) {
|
|
403
|
+
this._state = 0 /* Normal */;
|
|
404
|
+
}
|
|
405
|
+
break;
|
|
406
|
+
case 5 /* EscCharset */:
|
|
407
|
+
this._state = 0 /* Normal */;
|
|
408
|
+
break;
|
|
409
|
+
case 6 /* EscHash */:
|
|
410
|
+
this._state = 0 /* Normal */;
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
_feedNormal(ch, code) {
|
|
415
|
+
if (code === 27) {
|
|
416
|
+
this._state = 1 /* Escape */;
|
|
417
|
+
} else if (code === 155) {
|
|
418
|
+
this._csiParams = "";
|
|
419
|
+
this._csiPrivate = "";
|
|
420
|
+
this._state = 2 /* Csi */;
|
|
421
|
+
} else if (code === 157) {
|
|
422
|
+
this._state = 3 /* Osc */;
|
|
423
|
+
} else if (code === 144 || code === 152 || code === 158 || code === 159) {
|
|
424
|
+
this._state = 4 /* Str */;
|
|
425
|
+
} else if (code === 7 || code === 5 || code === 6) {
|
|
426
|
+
} else if (code === 8) {
|
|
427
|
+
if (this._cursorX > 0) this._cursorX--;
|
|
428
|
+
} else if (code === 127) {
|
|
429
|
+
if (this._cursorX > 0) {
|
|
430
|
+
this._cursorX--;
|
|
431
|
+
this._setChar(this._cursorY, this._cursorX, " ");
|
|
432
|
+
}
|
|
433
|
+
} else if (code === 9) {
|
|
434
|
+
this._cursorX = Math.min(this._cols - 1, (Math.floor(this._cursorX / 8) + 1) * 8);
|
|
435
|
+
} else if (code === 10 || code === 11 || code === 12) {
|
|
436
|
+
this._newline();
|
|
437
|
+
} else if (code === 13) {
|
|
438
|
+
this._cursorX = 0;
|
|
439
|
+
} else if (code === 14 || code === 15) {
|
|
440
|
+
} else if (code >= 32 && code !== 127) {
|
|
441
|
+
this._setChar(this._cursorY, this._cursorX, ch);
|
|
442
|
+
this._cursorX++;
|
|
443
|
+
if (this._cursorX >= this._cols) {
|
|
444
|
+
this._cursorX = 0;
|
|
445
|
+
this._newline();
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
_feedEscape(ch, code) {
|
|
450
|
+
this._state = 0 /* Normal */;
|
|
451
|
+
if (code === 91) {
|
|
452
|
+
this._csiParams = "";
|
|
453
|
+
this._csiPrivate = "";
|
|
454
|
+
this._state = 2 /* Csi */;
|
|
455
|
+
} else if (code === 93) {
|
|
456
|
+
this._state = 3 /* Osc */;
|
|
457
|
+
} else if (code === 80 || code === 88 || code === 94 || code === 95) {
|
|
458
|
+
this._state = 4 /* Str */;
|
|
459
|
+
} else if (code === 40 || code === 41 || code === 42 || code === 43 || code === 45 || code === 46) {
|
|
460
|
+
this._state = 5 /* EscCharset */;
|
|
461
|
+
} else if (code === 35) {
|
|
462
|
+
this._state = 6 /* EscHash */;
|
|
463
|
+
} else if (code === 55) {
|
|
464
|
+
this._savedCursor = { x: this._cursorX, y: this._cursorY };
|
|
465
|
+
} else if (code === 56) {
|
|
466
|
+
this._cursorX = this._clamp(this._savedCursor.x, 0, this._cols - 1);
|
|
467
|
+
this._cursorY = this._clamp(this._savedCursor.y, 0, this._rows - 1);
|
|
468
|
+
} else if (code === 68) {
|
|
469
|
+
this._newline();
|
|
470
|
+
} else if (code === 69) {
|
|
471
|
+
this._cursorX = 0;
|
|
472
|
+
this._newline();
|
|
473
|
+
} else if (code === 77) {
|
|
474
|
+
if (this._cursorY === this._scrollTop) {
|
|
475
|
+
this._scrollDown(1);
|
|
476
|
+
} else {
|
|
477
|
+
this._cursorY = Math.max(0, this._cursorY - 1);
|
|
478
|
+
}
|
|
479
|
+
} else if (code === 72) {
|
|
480
|
+
} else if (code === 99) {
|
|
481
|
+
this._screen = this._makeScreen(this._cols, this._rows);
|
|
482
|
+
this._cursorX = 0;
|
|
483
|
+
this._cursorY = 0;
|
|
484
|
+
this._savedCursor = { x: 0, y: 0 };
|
|
485
|
+
this._scrollTop = 0;
|
|
486
|
+
this._scrollBottom = this._rows - 1;
|
|
487
|
+
this._resetStyle();
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
_feedCsi(ch, code) {
|
|
491
|
+
if (code >= 64 && code <= 126) {
|
|
492
|
+
this._execCsi(ch);
|
|
493
|
+
this._state = 0 /* Normal */;
|
|
494
|
+
} else if (code === 63 || code === 33 || code === 62 || code === 32) {
|
|
495
|
+
this._csiPrivate = ch;
|
|
496
|
+
} else if (code >= 48 && code <= 57 || code === 59) {
|
|
497
|
+
this._csiParams += ch;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
_resetStyle() {
|
|
501
|
+
this._style = createDefaultStyleState();
|
|
502
|
+
this._styleSequence = "";
|
|
503
|
+
}
|
|
504
|
+
_applySgr(params2) {
|
|
505
|
+
const normalizedParams = params2.length === 0 ? [0] : params2;
|
|
506
|
+
for (let index = 0; index < normalizedParams.length; index += 1) {
|
|
507
|
+
const value = normalizedParams[index] ?? 0;
|
|
508
|
+
switch (value) {
|
|
509
|
+
case 0:
|
|
510
|
+
this._resetStyle();
|
|
511
|
+
break;
|
|
512
|
+
case 1:
|
|
513
|
+
this._style.bold = true;
|
|
514
|
+
break;
|
|
515
|
+
case 2:
|
|
516
|
+
this._style.dim = true;
|
|
517
|
+
break;
|
|
518
|
+
case 3:
|
|
519
|
+
this._style.italic = true;
|
|
520
|
+
break;
|
|
521
|
+
case 4:
|
|
522
|
+
this._style.underline = true;
|
|
523
|
+
break;
|
|
524
|
+
case 7:
|
|
525
|
+
this._style.inverse = true;
|
|
526
|
+
break;
|
|
527
|
+
case 9:
|
|
528
|
+
this._style.strikethrough = true;
|
|
529
|
+
break;
|
|
530
|
+
case 21:
|
|
531
|
+
case 22:
|
|
532
|
+
this._style.bold = false;
|
|
533
|
+
this._style.dim = false;
|
|
534
|
+
break;
|
|
535
|
+
case 23:
|
|
536
|
+
this._style.italic = false;
|
|
537
|
+
break;
|
|
538
|
+
case 24:
|
|
539
|
+
this._style.underline = false;
|
|
540
|
+
break;
|
|
541
|
+
case 27:
|
|
542
|
+
this._style.inverse = false;
|
|
543
|
+
break;
|
|
544
|
+
case 29:
|
|
545
|
+
this._style.strikethrough = false;
|
|
546
|
+
break;
|
|
547
|
+
case 39:
|
|
548
|
+
this._style.fg = void 0;
|
|
549
|
+
break;
|
|
550
|
+
case 49:
|
|
551
|
+
this._style.bg = void 0;
|
|
552
|
+
break;
|
|
553
|
+
case 38:
|
|
554
|
+
case 48:
|
|
555
|
+
index = this._applyExtendedColor(value, normalizedParams, index);
|
|
556
|
+
break;
|
|
557
|
+
default:
|
|
558
|
+
if (value >= 30 && value <= 37 || value >= 90 && value <= 97) {
|
|
559
|
+
this._style.fg = [value];
|
|
560
|
+
} else if (value >= 40 && value <= 47 || value >= 100 && value <= 107) {
|
|
561
|
+
this._style.bg = [value];
|
|
562
|
+
}
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
this._styleSequence = serializeStyleState(this._style);
|
|
567
|
+
}
|
|
568
|
+
_applyExtendedColor(control, params2, index) {
|
|
569
|
+
const mode = params2[index + 1];
|
|
570
|
+
const target = control === 38 ? "fg" : "bg";
|
|
571
|
+
if (mode === 5) {
|
|
572
|
+
const paletteIndex = params2[index + 2];
|
|
573
|
+
if (paletteIndex !== void 0) {
|
|
574
|
+
this._style[target] = [control, 5, paletteIndex];
|
|
575
|
+
return index + 2;
|
|
576
|
+
}
|
|
577
|
+
return index;
|
|
578
|
+
}
|
|
579
|
+
if (mode === 2) {
|
|
580
|
+
const red = params2[index + 2];
|
|
581
|
+
const green = params2[index + 3];
|
|
582
|
+
const blue = params2[index + 4];
|
|
583
|
+
if (red !== void 0 && green !== void 0 && blue !== void 0) {
|
|
584
|
+
this._style[target] = [control, 2, red, green, blue];
|
|
585
|
+
return index + 4;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return index;
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
function createDefaultStyleState() {
|
|
592
|
+
return {
|
|
593
|
+
bold: false,
|
|
594
|
+
dim: false,
|
|
595
|
+
italic: false,
|
|
596
|
+
underline: false,
|
|
597
|
+
inverse: false,
|
|
598
|
+
strikethrough: false
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
function serializeStyleState(state) {
|
|
602
|
+
const codes = [];
|
|
603
|
+
if (state.bold) {
|
|
604
|
+
codes.push(1);
|
|
605
|
+
}
|
|
606
|
+
if (state.dim) {
|
|
607
|
+
codes.push(2);
|
|
608
|
+
}
|
|
609
|
+
if (state.italic) {
|
|
610
|
+
codes.push(3);
|
|
611
|
+
}
|
|
612
|
+
if (state.underline) {
|
|
613
|
+
codes.push(4);
|
|
614
|
+
}
|
|
615
|
+
if (state.inverse) {
|
|
616
|
+
codes.push(7);
|
|
617
|
+
}
|
|
618
|
+
if (state.strikethrough) {
|
|
619
|
+
codes.push(9);
|
|
620
|
+
}
|
|
621
|
+
if (state.fg !== void 0) {
|
|
622
|
+
codes.push(...state.fg);
|
|
623
|
+
}
|
|
624
|
+
if (state.bg !== void 0) {
|
|
625
|
+
codes.push(...state.bg);
|
|
626
|
+
}
|
|
627
|
+
return codes.length === 0 ? "" : `\x1B[${codes.join(";")}m`;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// src/keys.ts
|
|
631
|
+
var NAMED_KEY_SEQUENCES = {
|
|
632
|
+
Enter: "\r",
|
|
633
|
+
Tab: " ",
|
|
634
|
+
Escape: "\x1B",
|
|
635
|
+
Backspace: "\x7F",
|
|
636
|
+
Delete: "\x1B[3~",
|
|
637
|
+
ArrowUp: "\x1B[A",
|
|
638
|
+
ArrowDown: "\x1B[B",
|
|
639
|
+
ArrowRight: "\x1B[C",
|
|
640
|
+
ArrowLeft: "\x1B[D",
|
|
641
|
+
Home: "\x1B[H",
|
|
642
|
+
End: "\x1B[F",
|
|
643
|
+
PageUp: "\x1B[5~",
|
|
644
|
+
PageDown: "\x1B[6~",
|
|
645
|
+
Space: " "
|
|
646
|
+
};
|
|
647
|
+
var NAMED_KEY_LOWER = new Map(
|
|
648
|
+
Object.entries(NAMED_KEY_SEQUENCES).map(([k, v]) => [k.toLowerCase(), v])
|
|
649
|
+
);
|
|
650
|
+
var VALID_KEYS_HINT = `Valid keys: ${Object.keys(NAMED_KEY_SEQUENCES).join(", ")}, Control+<letter>, Alt+<key>`;
|
|
651
|
+
function unknownKeyError(key) {
|
|
652
|
+
return new Error(`Unknown terminal key: ${key}. ${VALID_KEYS_HINT}`);
|
|
653
|
+
}
|
|
654
|
+
function keyToSequence(key) {
|
|
655
|
+
const lowerKey = key.toLowerCase();
|
|
656
|
+
const namedSequence = NAMED_KEY_LOWER.get(lowerKey);
|
|
657
|
+
if (namedSequence !== void 0) {
|
|
658
|
+
return namedSequence;
|
|
659
|
+
}
|
|
660
|
+
if (lowerKey.startsWith("control+")) {
|
|
661
|
+
return controlKeyToSequence(key.slice("control+".length));
|
|
662
|
+
}
|
|
663
|
+
if (lowerKey.startsWith("alt+")) {
|
|
664
|
+
const nestedKey = key.slice("alt+".length);
|
|
665
|
+
if (nestedKey.length === 0) {
|
|
666
|
+
throw unknownKeyError(key);
|
|
667
|
+
}
|
|
668
|
+
if (nestedKey.length === 1) {
|
|
669
|
+
return "\x1B" + nestedKey;
|
|
670
|
+
}
|
|
671
|
+
try {
|
|
672
|
+
return "\x1B" + keyToSequence(nestedKey);
|
|
673
|
+
} catch {
|
|
674
|
+
throw unknownKeyError(key);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if (key.length === 1) {
|
|
678
|
+
return key;
|
|
679
|
+
}
|
|
680
|
+
throw unknownKeyError(key);
|
|
681
|
+
}
|
|
682
|
+
function controlKeyToSequence(controlKey) {
|
|
683
|
+
if (controlKey.length !== 1) {
|
|
684
|
+
throw unknownKeyError(`Control+${controlKey}`);
|
|
685
|
+
}
|
|
686
|
+
const uppercaseLetter = controlKey.toUpperCase();
|
|
687
|
+
const charCode = uppercaseLetter.charCodeAt(0);
|
|
688
|
+
if (charCode < 65 || charCode > 90) {
|
|
689
|
+
throw unknownKeyError(`Control+${controlKey}`);
|
|
690
|
+
}
|
|
691
|
+
return String.fromCharCode(charCode - 64);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// src/terminal-screen.ts
|
|
695
|
+
var TerminalScreen = class {
|
|
696
|
+
lines;
|
|
697
|
+
rawLines;
|
|
698
|
+
cursor;
|
|
699
|
+
size;
|
|
700
|
+
constructor({
|
|
701
|
+
lines,
|
|
702
|
+
rawLines,
|
|
703
|
+
cursor,
|
|
704
|
+
size
|
|
705
|
+
}) {
|
|
706
|
+
this.lines = Object.freeze(lines.map((line) => stripAnsi(line)));
|
|
707
|
+
this.rawLines = Object.freeze([...rawLines]);
|
|
708
|
+
this.cursor = Object.freeze({ ...cursor });
|
|
709
|
+
this.size = Object.freeze({ ...size });
|
|
710
|
+
Object.freeze(this);
|
|
711
|
+
}
|
|
712
|
+
get text() {
|
|
713
|
+
return this.lines.join("\n");
|
|
714
|
+
}
|
|
715
|
+
contains(substring) {
|
|
716
|
+
return this.text.includes(substring);
|
|
717
|
+
}
|
|
718
|
+
line(index) {
|
|
719
|
+
const normalizedIndex = index < 0 ? this.lines.length + index : index;
|
|
720
|
+
const line = this.lines[normalizedIndex];
|
|
721
|
+
if (line === void 0) {
|
|
722
|
+
throw new RangeError(`Line index out of bounds: ${index}`);
|
|
723
|
+
}
|
|
724
|
+
return line;
|
|
725
|
+
}
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
// src/terminal-session.ts
|
|
729
|
+
var DEFAULT_COLS = 120;
|
|
730
|
+
var DEFAULT_ROWS = 40;
|
|
731
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
732
|
+
var WAIT_FOR_POLL_MS = 10;
|
|
733
|
+
var TYPE_DELAY_MS = 15;
|
|
734
|
+
var CLOSE_AFTER_SIGNAL_GRACE_MS = 250;
|
|
735
|
+
var CLOSE_AFTER_SIGTERM_MS = 1e3;
|
|
736
|
+
var TerminalSession = class {
|
|
737
|
+
id;
|
|
738
|
+
command;
|
|
739
|
+
pid;
|
|
740
|
+
exitCode = null;
|
|
741
|
+
pty;
|
|
742
|
+
terminal;
|
|
743
|
+
emitter = new EventEmitter();
|
|
744
|
+
exitPromise;
|
|
745
|
+
rawBuffer = "";
|
|
746
|
+
lastDataAt = Date.now();
|
|
747
|
+
currentCols;
|
|
748
|
+
currentRows;
|
|
749
|
+
closeRequested = false;
|
|
750
|
+
signalRequested = false;
|
|
751
|
+
constructor({
|
|
752
|
+
id,
|
|
753
|
+
command,
|
|
754
|
+
args = [],
|
|
755
|
+
cwd = process.cwd(),
|
|
756
|
+
env = process.env,
|
|
757
|
+
cols = DEFAULT_COLS,
|
|
758
|
+
rows = DEFAULT_ROWS,
|
|
759
|
+
observe = false
|
|
760
|
+
}) {
|
|
761
|
+
this.id = id;
|
|
762
|
+
this.command = command;
|
|
763
|
+
this.currentCols = cols;
|
|
764
|
+
this.currentRows = rows;
|
|
765
|
+
this.terminal = new TerminalBuffer(cols, rows);
|
|
766
|
+
this.pty = createPtyProcess({ command, args, cwd, env, cols, rows });
|
|
767
|
+
this.pid = this.pty.pid;
|
|
768
|
+
const dataSubscription = this.pty.onData((chunk) => {
|
|
769
|
+
this.rawBuffer += chunk;
|
|
770
|
+
this.lastDataAt = Date.now();
|
|
771
|
+
this.terminal.write(chunk);
|
|
772
|
+
if (observe) {
|
|
773
|
+
process.stderr.write(chunk);
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
let exitSubscription;
|
|
777
|
+
this.exitPromise = new Promise((resolve) => {
|
|
778
|
+
exitSubscription = this.pty.onExit(({ exitCode }) => {
|
|
779
|
+
if (this.exitCode !== null) {
|
|
780
|
+
resolve(this.exitCode);
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
this.exitCode = exitCode;
|
|
784
|
+
dataSubscription.dispose();
|
|
785
|
+
exitSubscription?.dispose();
|
|
786
|
+
this.emitter.emit("exit", exitCode);
|
|
787
|
+
resolve(exitCode);
|
|
788
|
+
});
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
async type(text) {
|
|
792
|
+
for (const character of text) {
|
|
793
|
+
await this.send(character);
|
|
794
|
+
await sleep(TYPE_DELAY_MS);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
async fill(text) {
|
|
798
|
+
await this.send(text.replace(/\r?\n/g, "\r"));
|
|
799
|
+
}
|
|
800
|
+
async press(key) {
|
|
801
|
+
await this.send(keyToSequence(key));
|
|
802
|
+
}
|
|
803
|
+
async send(raw) {
|
|
804
|
+
if (this.exitCode !== null) {
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
this.pty.write(raw);
|
|
808
|
+
}
|
|
809
|
+
async signal(sig) {
|
|
810
|
+
if (this.exitCode !== null) {
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
this.signalRequested = true;
|
|
814
|
+
this.pty.kill(sig);
|
|
815
|
+
}
|
|
816
|
+
async waitFor(pattern, opts) {
|
|
817
|
+
const timeout = opts?.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
818
|
+
const startedAt = Date.now();
|
|
819
|
+
while (Date.now() - startedAt <= timeout) {
|
|
820
|
+
const matched = matchPattern(this.rawBuffer, pattern);
|
|
821
|
+
if (matched !== null) {
|
|
822
|
+
return matched;
|
|
823
|
+
}
|
|
824
|
+
await sleep(WAIT_FOR_POLL_MS);
|
|
825
|
+
}
|
|
826
|
+
throw new Error(`Timed out waiting for pattern after ${timeout}ms: ${String(pattern)}`);
|
|
827
|
+
}
|
|
828
|
+
async waitForQuiet(ms) {
|
|
829
|
+
while (true) {
|
|
830
|
+
const remaining = ms - (Date.now() - this.lastDataAt);
|
|
831
|
+
if (remaining <= 0) {
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
await sleep(remaining);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
async screen() {
|
|
838
|
+
const rawLines = [];
|
|
839
|
+
for (let row = 0; row < this.currentRows; row += 1) {
|
|
840
|
+
rawLines.push(this.terminal.renderLine(row));
|
|
841
|
+
}
|
|
842
|
+
return new TerminalScreen({
|
|
843
|
+
lines: rawLines,
|
|
844
|
+
rawLines,
|
|
845
|
+
cursor: {
|
|
846
|
+
row: this.terminal.displayBuffer.cursorY,
|
|
847
|
+
col: this.terminal.displayBuffer.cursorX
|
|
848
|
+
},
|
|
849
|
+
size: {
|
|
850
|
+
rows: this.currentRows,
|
|
851
|
+
cols: this.currentCols
|
|
852
|
+
}
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
async history(opts) {
|
|
856
|
+
const normalized = normalizeHistoryBuffer(stripAnsi(this.rawBuffer));
|
|
857
|
+
const lines = splitHistoryLines(normalized);
|
|
858
|
+
if (opts?.last === void 0) {
|
|
859
|
+
return lines;
|
|
860
|
+
}
|
|
861
|
+
const start = Math.max(0, lines.length - opts.last);
|
|
862
|
+
return lines.slice(start);
|
|
863
|
+
}
|
|
864
|
+
async resize(cols, rows) {
|
|
865
|
+
this.currentCols = cols;
|
|
866
|
+
this.currentRows = rows;
|
|
867
|
+
if (this.exitCode === null) {
|
|
868
|
+
this.pty.resize(cols, rows);
|
|
869
|
+
}
|
|
870
|
+
this.terminal.resize(cols, rows);
|
|
871
|
+
}
|
|
872
|
+
async waitForExit(opts) {
|
|
873
|
+
if (this.exitCode !== null) {
|
|
874
|
+
return this.exitCode;
|
|
875
|
+
}
|
|
876
|
+
if (opts?.timeout !== void 0) {
|
|
877
|
+
const result = await waitForExit(this.exitPromise, opts.timeout);
|
|
878
|
+
if (result === null) {
|
|
879
|
+
throw new Error(`Timed out waiting for process to exit after ${opts.timeout}ms`);
|
|
880
|
+
}
|
|
881
|
+
return result;
|
|
882
|
+
}
|
|
883
|
+
return this.exitPromise;
|
|
884
|
+
}
|
|
885
|
+
async close() {
|
|
886
|
+
if (this.exitCode !== null) {
|
|
887
|
+
return this.exitCode;
|
|
888
|
+
}
|
|
889
|
+
if (!this.closeRequested) {
|
|
890
|
+
this.closeRequested = true;
|
|
891
|
+
const gracefulExitCode = await waitForExit(this.exitPromise, CLOSE_AFTER_SIGNAL_GRACE_MS);
|
|
892
|
+
if (gracefulExitCode !== null) {
|
|
893
|
+
return gracefulExitCode;
|
|
894
|
+
}
|
|
895
|
+
if (this.signalRequested) {
|
|
896
|
+
return this.exitPromise;
|
|
897
|
+
}
|
|
898
|
+
if (this.exitCode === null) {
|
|
899
|
+
this.pty.kill("SIGTERM");
|
|
900
|
+
const afterSigterm = await waitForExit(this.exitPromise, CLOSE_AFTER_SIGTERM_MS);
|
|
901
|
+
if (afterSigterm !== null) {
|
|
902
|
+
return afterSigterm;
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
if (this.exitCode === null) {
|
|
906
|
+
this.pty.kill("SIGKILL");
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
return this.exitPromise;
|
|
910
|
+
}
|
|
911
|
+
on(event, cb) {
|
|
912
|
+
this.emitter.on(event, cb);
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
function createPtyProcess({
|
|
916
|
+
command,
|
|
917
|
+
args,
|
|
918
|
+
cwd,
|
|
919
|
+
env,
|
|
920
|
+
cols,
|
|
921
|
+
rows
|
|
922
|
+
}) {
|
|
923
|
+
try {
|
|
924
|
+
return nodePty.spawn(command, args, {
|
|
925
|
+
cwd,
|
|
926
|
+
env,
|
|
927
|
+
cols,
|
|
928
|
+
rows,
|
|
929
|
+
encoding: "utf8"
|
|
930
|
+
});
|
|
931
|
+
} catch {
|
|
932
|
+
return createChildProcessFallback({ command, args, cwd, env });
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
function createChildProcessFallback({
|
|
936
|
+
command,
|
|
937
|
+
args,
|
|
938
|
+
cwd,
|
|
939
|
+
env
|
|
940
|
+
}) {
|
|
941
|
+
const child = spawnChildProcess(command, args, {
|
|
942
|
+
cwd,
|
|
943
|
+
env,
|
|
944
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
945
|
+
});
|
|
946
|
+
return new ChildProcessFallback(child);
|
|
947
|
+
}
|
|
948
|
+
var ChildProcessFallback = class {
|
|
949
|
+
pid;
|
|
950
|
+
child;
|
|
951
|
+
dataEmitter = new EventEmitter();
|
|
952
|
+
exitEmitter = new EventEmitter();
|
|
953
|
+
constructor(child) {
|
|
954
|
+
this.child = child;
|
|
955
|
+
this.pid = child.pid ?? -1;
|
|
956
|
+
child.stdout.setEncoding("utf8");
|
|
957
|
+
child.stderr.setEncoding("utf8");
|
|
958
|
+
child.stdout.on("data", this.handleData);
|
|
959
|
+
child.stderr.on("data", this.handleData);
|
|
960
|
+
child.on("exit", (exitCode, signal) => {
|
|
961
|
+
this.exitEmitter.emit("exit", {
|
|
962
|
+
exitCode: exitCode ?? signalToExitCode(signal),
|
|
963
|
+
signal: void 0
|
|
964
|
+
});
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
write(data) {
|
|
968
|
+
this.child.stdin.write(data);
|
|
969
|
+
}
|
|
970
|
+
resize() {
|
|
971
|
+
}
|
|
972
|
+
kill(signal) {
|
|
973
|
+
this.child.kill(signal);
|
|
974
|
+
}
|
|
975
|
+
onData(listener) {
|
|
976
|
+
this.dataEmitter.on("data", listener);
|
|
977
|
+
return {
|
|
978
|
+
dispose: () => {
|
|
979
|
+
this.dataEmitter.off("data", listener);
|
|
980
|
+
}
|
|
981
|
+
};
|
|
982
|
+
}
|
|
983
|
+
onExit(listener) {
|
|
984
|
+
this.exitEmitter.on("exit", listener);
|
|
985
|
+
return {
|
|
986
|
+
dispose: () => {
|
|
987
|
+
this.exitEmitter.off("exit", listener);
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
handleData = (chunk) => {
|
|
992
|
+
this.dataEmitter.emit("data", String(chunk));
|
|
993
|
+
};
|
|
994
|
+
};
|
|
995
|
+
function signalToExitCode(signal) {
|
|
996
|
+
if (signal === null) {
|
|
997
|
+
return 0;
|
|
998
|
+
}
|
|
999
|
+
const signalNumbers = {
|
|
1000
|
+
SIGTERM: 15,
|
|
1001
|
+
SIGINT: 2,
|
|
1002
|
+
SIGHUP: 1,
|
|
1003
|
+
SIGKILL: 9
|
|
1004
|
+
};
|
|
1005
|
+
const signalNumber = signalNumbers[signal];
|
|
1006
|
+
if (signalNumber === void 0) {
|
|
1007
|
+
return 1;
|
|
1008
|
+
}
|
|
1009
|
+
return 128 + signalNumber;
|
|
1010
|
+
}
|
|
1011
|
+
function matchPattern(buffer, pattern) {
|
|
1012
|
+
const clean = normalizeHistoryBuffer(stripAnsi(buffer));
|
|
1013
|
+
for (const line of clean.split("\n")) {
|
|
1014
|
+
if (typeof pattern === "string") {
|
|
1015
|
+
if (line.includes(pattern)) return line;
|
|
1016
|
+
} else {
|
|
1017
|
+
const flags = removeCharacter(pattern.flags, "g");
|
|
1018
|
+
if (new RegExp(pattern.source, flags).test(line)) return line;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
return null;
|
|
1022
|
+
}
|
|
1023
|
+
function removeCharacter(input, charToRemove) {
|
|
1024
|
+
let output = "";
|
|
1025
|
+
for (const character of input) {
|
|
1026
|
+
if (character !== charToRemove) {
|
|
1027
|
+
output += character;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
return output;
|
|
1031
|
+
}
|
|
1032
|
+
function splitHistoryLines(input) {
|
|
1033
|
+
const lines = input.split("\n");
|
|
1034
|
+
if (lines.length > 0 && lines[lines.length - 1] === "") {
|
|
1035
|
+
lines.pop();
|
|
1036
|
+
}
|
|
1037
|
+
return lines;
|
|
1038
|
+
}
|
|
1039
|
+
function normalizeHistoryBuffer(input) {
|
|
1040
|
+
let output = "";
|
|
1041
|
+
for (const character of input) {
|
|
1042
|
+
if (character === "\r" || character === "\b") {
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
1045
|
+
output += character;
|
|
1046
|
+
}
|
|
1047
|
+
return output;
|
|
1048
|
+
}
|
|
1049
|
+
function sleep(ms) {
|
|
1050
|
+
return new Promise((resolve) => {
|
|
1051
|
+
setTimeout(resolve, ms);
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
async function waitForExit(exitPromise, timeout) {
|
|
1055
|
+
return new Promise((resolve) => {
|
|
1056
|
+
let settled = false;
|
|
1057
|
+
const timer = setTimeout(() => {
|
|
1058
|
+
if (settled) {
|
|
1059
|
+
return;
|
|
1060
|
+
}
|
|
1061
|
+
settled = true;
|
|
1062
|
+
resolve(null);
|
|
1063
|
+
}, timeout);
|
|
1064
|
+
void exitPromise.then((code) => {
|
|
1065
|
+
if (settled) {
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
settled = true;
|
|
1069
|
+
clearTimeout(timer);
|
|
1070
|
+
resolve(code);
|
|
1071
|
+
});
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
// src/terminal-pilot.ts
|
|
1076
|
+
var DEFAULT_COLS2 = 120;
|
|
1077
|
+
var DEFAULT_ROWS2 = 40;
|
|
1078
|
+
var TerminalPilot = class _TerminalPilot {
|
|
1079
|
+
sessionMap = /* @__PURE__ */ new Map();
|
|
1080
|
+
static async launch() {
|
|
1081
|
+
return new _TerminalPilot();
|
|
1082
|
+
}
|
|
1083
|
+
async newSession(opts) {
|
|
1084
|
+
const session = new TerminalSession({
|
|
1085
|
+
id: randomUUID(),
|
|
1086
|
+
command: opts.command,
|
|
1087
|
+
args: opts.args,
|
|
1088
|
+
cwd: opts.cwd,
|
|
1089
|
+
env: opts.env,
|
|
1090
|
+
cols: opts.cols ?? DEFAULT_COLS2,
|
|
1091
|
+
rows: opts.rows ?? DEFAULT_ROWS2,
|
|
1092
|
+
observe: opts.observe ?? false
|
|
1093
|
+
});
|
|
1094
|
+
this.sessionMap.set(session.id, session);
|
|
1095
|
+
return session;
|
|
1096
|
+
}
|
|
1097
|
+
getSession(id) {
|
|
1098
|
+
const session = this.sessionMap.get(id);
|
|
1099
|
+
if (session === void 0) {
|
|
1100
|
+
throw new Error(`Session not found: ${id}`);
|
|
1101
|
+
}
|
|
1102
|
+
return session;
|
|
1103
|
+
}
|
|
1104
|
+
deleteSession(id) {
|
|
1105
|
+
this.sessionMap.delete(id);
|
|
1106
|
+
}
|
|
1107
|
+
sessions() {
|
|
1108
|
+
return [...this.sessionMap.values()].filter((s) => s.exitCode === null);
|
|
1109
|
+
}
|
|
1110
|
+
async close() {
|
|
1111
|
+
const sessions = [...this.sessionMap.values()];
|
|
1112
|
+
try {
|
|
1113
|
+
await Promise.all(sessions.map((session) => session.close()));
|
|
1114
|
+
} finally {
|
|
1115
|
+
this.sessionMap.clear();
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
// src/commands/runtime.ts
|
|
1121
|
+
var SESSION_ENV_VAR = "TERMINAL_PILOT_SESSION";
|
|
1122
|
+
var sharedRuntime;
|
|
1123
|
+
function getTerminalPilotRuntime(runtime) {
|
|
1124
|
+
if (runtime !== void 0) {
|
|
1125
|
+
return runtime;
|
|
1126
|
+
}
|
|
1127
|
+
sharedRuntime ??= createTerminalPilotRuntime();
|
|
1128
|
+
return sharedRuntime;
|
|
1129
|
+
}
|
|
1130
|
+
function createTerminalPilotRuntime(options = {}) {
|
|
1131
|
+
const launchPilot = options.launchPilot ?? TerminalPilot.launch;
|
|
1132
|
+
const nameToId = /* @__PURE__ */ new Map();
|
|
1133
|
+
const idToName = /* @__PURE__ */ new Map();
|
|
1134
|
+
let pilotPromise;
|
|
1135
|
+
function getRequestedName(name, env) {
|
|
1136
|
+
return name ?? env?.get(SESSION_ENV_VAR);
|
|
1137
|
+
}
|
|
1138
|
+
async function getPilot() {
|
|
1139
|
+
pilotPromise ??= launchPilot();
|
|
1140
|
+
return pilotPromise;
|
|
1141
|
+
}
|
|
1142
|
+
function nextSessionName() {
|
|
1143
|
+
let index = 1;
|
|
1144
|
+
while (nameToId.has(`s${index}`)) {
|
|
1145
|
+
index += 1;
|
|
1146
|
+
}
|
|
1147
|
+
return `s${index}`;
|
|
1148
|
+
}
|
|
1149
|
+
function rememberSession(name, session) {
|
|
1150
|
+
nameToId.set(name, session.id);
|
|
1151
|
+
idToName.set(session.id, name);
|
|
1152
|
+
return { name, session };
|
|
1153
|
+
}
|
|
1154
|
+
function forgetSession(name, sessionId) {
|
|
1155
|
+
nameToId.delete(name);
|
|
1156
|
+
idToName.delete(sessionId);
|
|
1157
|
+
}
|
|
1158
|
+
function formatAvailableSessions(names) {
|
|
1159
|
+
if (names.length === 0) {
|
|
1160
|
+
return "No active sessions are available.";
|
|
1161
|
+
}
|
|
1162
|
+
return `Available sessions: ${names.join(", ")}.`;
|
|
1163
|
+
}
|
|
1164
|
+
async function lookupNamedSession(name) {
|
|
1165
|
+
const sessionId = nameToId.get(name);
|
|
1166
|
+
if (sessionId === void 0) {
|
|
1167
|
+
const active = await listSessions();
|
|
1168
|
+
throw new UserError(`Session "${name}" was not found. ${formatAvailableSessions(active.map((entry) => entry.name))}`);
|
|
1169
|
+
}
|
|
1170
|
+
const pilot = await getPilot();
|
|
1171
|
+
try {
|
|
1172
|
+
return { name, session: pilot.getSession(sessionId) };
|
|
1173
|
+
} catch {
|
|
1174
|
+
forgetSession(name, sessionId);
|
|
1175
|
+
const active = await listSessions();
|
|
1176
|
+
throw new UserError(`Session "${name}" was not found. ${formatAvailableSessions(active.map((entry) => entry.name))}`);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
async function listSessions() {
|
|
1180
|
+
const pilot = await getPilot();
|
|
1181
|
+
return pilot.sessions().flatMap((session) => {
|
|
1182
|
+
const name = idToName.get(session.id);
|
|
1183
|
+
if (name === void 0) {
|
|
1184
|
+
return [];
|
|
1185
|
+
}
|
|
1186
|
+
return [{ name, session }];
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
return {
|
|
1190
|
+
async createSession(params2, env) {
|
|
1191
|
+
const requestedName = getRequestedName(params2.session, env) ?? nextSessionName();
|
|
1192
|
+
if (nameToId.has(requestedName)) {
|
|
1193
|
+
throw new UserError(`Session "${requestedName}" already exists.`);
|
|
1194
|
+
}
|
|
1195
|
+
const pilot = await getPilot();
|
|
1196
|
+
const session = await pilot.newSession({
|
|
1197
|
+
command: params2.command,
|
|
1198
|
+
args: params2.args,
|
|
1199
|
+
cwd: params2.cwd,
|
|
1200
|
+
cols: params2.cols,
|
|
1201
|
+
rows: params2.rows,
|
|
1202
|
+
observe: params2.observe
|
|
1203
|
+
});
|
|
1204
|
+
return rememberSession(requestedName, session);
|
|
1205
|
+
},
|
|
1206
|
+
async resolveSession(name, env) {
|
|
1207
|
+
const requestedName = getRequestedName(name, env);
|
|
1208
|
+
if (requestedName !== void 0) {
|
|
1209
|
+
return lookupNamedSession(requestedName);
|
|
1210
|
+
}
|
|
1211
|
+
const active = await listSessions();
|
|
1212
|
+
if (active.length === 1) {
|
|
1213
|
+
return active[0];
|
|
1214
|
+
}
|
|
1215
|
+
if (active.length === 0) {
|
|
1216
|
+
throw new UserError("No active sessions. Create one with create-session.");
|
|
1217
|
+
}
|
|
1218
|
+
throw new UserError(
|
|
1219
|
+
`Multiple active sessions require an explicit session name. Pass --session or set ${SESSION_ENV_VAR}. ${formatAvailableSessions(active.map((entry) => entry.name))}`
|
|
1220
|
+
);
|
|
1221
|
+
},
|
|
1222
|
+
async closeSession(name, env) {
|
|
1223
|
+
const namedSession = await this.resolveSession(name, env);
|
|
1224
|
+
const exitCode = await namedSession.session.close();
|
|
1225
|
+
const pilot = await getPilot();
|
|
1226
|
+
pilot.deleteSession(namedSession.session.id);
|
|
1227
|
+
forgetSession(namedSession.name, namedSession.session.id);
|
|
1228
|
+
return {
|
|
1229
|
+
exitCode,
|
|
1230
|
+
name: namedSession.name
|
|
1231
|
+
};
|
|
1232
|
+
},
|
|
1233
|
+
listSessions,
|
|
1234
|
+
async close() {
|
|
1235
|
+
if (pilotPromise === void 0) {
|
|
1236
|
+
return;
|
|
1237
|
+
}
|
|
1238
|
+
const pilot = await pilotPromise;
|
|
1239
|
+
await pilot.close();
|
|
1240
|
+
pilotPromise = void 0;
|
|
1241
|
+
nameToId.clear();
|
|
1242
|
+
idToName.clear();
|
|
1243
|
+
}
|
|
1244
|
+
};
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
// src/commands/close-session.ts
|
|
1248
|
+
var params = S.Object({
|
|
1249
|
+
session: S.Optional(S.String({ short: "s", description: "Session name" }))
|
|
1250
|
+
});
|
|
1251
|
+
var closeSession = defineCommand({
|
|
1252
|
+
name: "close-session",
|
|
1253
|
+
description: "Close an active terminal session",
|
|
1254
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1255
|
+
params,
|
|
1256
|
+
handler: async ({ params: params2, env, terminalPilotRuntime }) => {
|
|
1257
|
+
const { exitCode } = await getTerminalPilotRuntime(terminalPilotRuntime).closeSession(params2.session, env);
|
|
1258
|
+
return { exitCode };
|
|
1259
|
+
}
|
|
1260
|
+
});
|
|
1261
|
+
export {
|
|
1262
|
+
closeSession
|
|
1263
|
+
};
|
|
1264
|
+
//# sourceMappingURL=close-session.js.map
|