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,1911 @@
|
|
|
1
|
+
// src/ansi.ts
|
|
2
|
+
var ESC = 27;
|
|
3
|
+
var BEL = 7;
|
|
4
|
+
var ST = 156;
|
|
5
|
+
var CSI = 155;
|
|
6
|
+
var OSC = 157;
|
|
7
|
+
var DCS = 144;
|
|
8
|
+
var SOS = 152;
|
|
9
|
+
var PM = 158;
|
|
10
|
+
var APC = 159;
|
|
11
|
+
function stripAnsi(input) {
|
|
12
|
+
let output = "";
|
|
13
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
14
|
+
const code = input.charCodeAt(index);
|
|
15
|
+
if (code === ESC) {
|
|
16
|
+
const nextCode = input.charCodeAt(index + 1);
|
|
17
|
+
if (nextCode === 91) {
|
|
18
|
+
index = consumeCsi(input, index + 2);
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (nextCode === 93) {
|
|
22
|
+
index = consumeTerminatedString(input, index + 2, true);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (nextCode === 80 || nextCode === 88 || nextCode === 94 || nextCode === 95) {
|
|
26
|
+
index = consumeTerminatedString(input, index + 2, false);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (!Number.isNaN(nextCode)) {
|
|
30
|
+
index += 1;
|
|
31
|
+
}
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (code === CSI) {
|
|
35
|
+
index = consumeCsi(input, index + 1);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (code === OSC) {
|
|
39
|
+
index = consumeTerminatedString(input, index + 1, true);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (code === DCS || code === SOS || code === PM || code === APC) {
|
|
43
|
+
index = consumeTerminatedString(input, index + 1, false);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
output += input[index];
|
|
47
|
+
}
|
|
48
|
+
return output;
|
|
49
|
+
}
|
|
50
|
+
function consumeCsi(input, index) {
|
|
51
|
+
while (index < input.length) {
|
|
52
|
+
const code = input.charCodeAt(index);
|
|
53
|
+
if (code >= 64 && code <= 126) {
|
|
54
|
+
return index;
|
|
55
|
+
}
|
|
56
|
+
index += 1;
|
|
57
|
+
}
|
|
58
|
+
return input.length;
|
|
59
|
+
}
|
|
60
|
+
function consumeTerminatedString(input, index, allowBellTerminator) {
|
|
61
|
+
while (index < input.length) {
|
|
62
|
+
const code = input.charCodeAt(index);
|
|
63
|
+
if (code === ST || allowBellTerminator && code === BEL) {
|
|
64
|
+
return index;
|
|
65
|
+
}
|
|
66
|
+
if (code === ESC && input.charCodeAt(index + 1) === 92) {
|
|
67
|
+
return index + 1;
|
|
68
|
+
}
|
|
69
|
+
index += 1;
|
|
70
|
+
}
|
|
71
|
+
return input.length;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/cli.ts
|
|
75
|
+
import { realpath } from "node:fs/promises";
|
|
76
|
+
import path2 from "node:path";
|
|
77
|
+
import { fileURLToPath } from "node:url";
|
|
78
|
+
import { runCLI } from "@poe-code/cmdkit/cli";
|
|
79
|
+
|
|
80
|
+
// src/commands/index.ts
|
|
81
|
+
import { defineGroup } from "@poe-code/cmdkit";
|
|
82
|
+
|
|
83
|
+
// src/commands/close-session.ts
|
|
84
|
+
import { defineCommand, S } from "@poe-code/cmdkit";
|
|
85
|
+
|
|
86
|
+
// src/commands/runtime.ts
|
|
87
|
+
import { UserError } from "@poe-code/cmdkit";
|
|
88
|
+
|
|
89
|
+
// src/terminal-pilot.ts
|
|
90
|
+
import { randomUUID } from "node:crypto";
|
|
91
|
+
|
|
92
|
+
// src/terminal-session.ts
|
|
93
|
+
import { spawn as spawnChildProcess } from "node:child_process";
|
|
94
|
+
import { EventEmitter } from "node:events";
|
|
95
|
+
import * as nodePty from "node-pty";
|
|
96
|
+
|
|
97
|
+
// src/terminal-buffer.ts
|
|
98
|
+
var RESET_SGR = "\x1B[0m";
|
|
99
|
+
var TerminalBuffer = class {
|
|
100
|
+
_cols;
|
|
101
|
+
_rows;
|
|
102
|
+
_screen;
|
|
103
|
+
_cursorX = 0;
|
|
104
|
+
_cursorY = 0;
|
|
105
|
+
_savedCursor = { x: 0, y: 0 };
|
|
106
|
+
_scrollTop = 0;
|
|
107
|
+
_scrollBottom;
|
|
108
|
+
_state = 0 /* Normal */;
|
|
109
|
+
_csiParams = "";
|
|
110
|
+
_csiPrivate = "";
|
|
111
|
+
_style = createDefaultStyleState();
|
|
112
|
+
_styleSequence = "";
|
|
113
|
+
displayBuffer;
|
|
114
|
+
constructor(cols, rows) {
|
|
115
|
+
this._cols = cols;
|
|
116
|
+
this._rows = rows;
|
|
117
|
+
this._scrollBottom = rows - 1;
|
|
118
|
+
this._screen = this._makeScreen(cols, rows);
|
|
119
|
+
this.displayBuffer = Object.defineProperties(
|
|
120
|
+
{},
|
|
121
|
+
{
|
|
122
|
+
cursorX: { get: () => this._cursorX, enumerable: true },
|
|
123
|
+
cursorY: { get: () => this._cursorY, enumerable: true },
|
|
124
|
+
data: { get: () => this._screen, enumerable: true }
|
|
125
|
+
}
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
write(data) {
|
|
129
|
+
for (const ch of data) {
|
|
130
|
+
this._feed(ch);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
renderLine(row) {
|
|
134
|
+
const cells = this._screen[row] ?? [];
|
|
135
|
+
let lastVisibleCell = -1;
|
|
136
|
+
for (let index = cells.length - 1; index >= 0; index -= 1) {
|
|
137
|
+
if (cells[index] !== null) {
|
|
138
|
+
lastVisibleCell = index;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (lastVisibleCell === -1) {
|
|
143
|
+
return "";
|
|
144
|
+
}
|
|
145
|
+
let line = "";
|
|
146
|
+
let activeStyle = "";
|
|
147
|
+
for (let index = 0; index <= lastVisibleCell; index += 1) {
|
|
148
|
+
const cell = cells[index];
|
|
149
|
+
const cellStyle = cell?.style ?? "";
|
|
150
|
+
if (cellStyle !== activeStyle) {
|
|
151
|
+
line += cellStyle.length > 0 ? cellStyle : RESET_SGR;
|
|
152
|
+
activeStyle = cellStyle;
|
|
153
|
+
}
|
|
154
|
+
line += cell?.[1] ?? " ";
|
|
155
|
+
}
|
|
156
|
+
if (activeStyle.length > 0) {
|
|
157
|
+
line += RESET_SGR;
|
|
158
|
+
}
|
|
159
|
+
return line;
|
|
160
|
+
}
|
|
161
|
+
resize(cols, rows) {
|
|
162
|
+
while (this._screen.length < rows) {
|
|
163
|
+
this._screen.push(this._makeRow(cols));
|
|
164
|
+
}
|
|
165
|
+
this._screen.length = rows;
|
|
166
|
+
for (let y = 0; y < rows; y++) {
|
|
167
|
+
const row = this._screen[y] ?? this._makeRow(cols);
|
|
168
|
+
while (row.length < cols) row.push(null);
|
|
169
|
+
row.length = cols;
|
|
170
|
+
this._screen[y] = row;
|
|
171
|
+
}
|
|
172
|
+
this._cols = cols;
|
|
173
|
+
this._rows = rows;
|
|
174
|
+
this._scrollTop = 0;
|
|
175
|
+
this._scrollBottom = rows - 1;
|
|
176
|
+
this._cursorX = this._clamp(this._cursorX, 0, cols - 1);
|
|
177
|
+
this._cursorY = this._clamp(this._cursorY, 0, rows - 1);
|
|
178
|
+
}
|
|
179
|
+
_makeScreen(cols, rows) {
|
|
180
|
+
return Array.from({ length: rows }, () => this._makeRow(cols));
|
|
181
|
+
}
|
|
182
|
+
_makeRow(cols) {
|
|
183
|
+
return Array(cols).fill(null);
|
|
184
|
+
}
|
|
185
|
+
_clamp(value, min, max) {
|
|
186
|
+
return Math.max(min, Math.min(max, value));
|
|
187
|
+
}
|
|
188
|
+
_setChar(y, x, ch) {
|
|
189
|
+
const row = this._screen[y];
|
|
190
|
+
if (row && x >= 0 && x < this._cols) {
|
|
191
|
+
const cell = [ch.charCodeAt(0), ch];
|
|
192
|
+
if (this._styleSequence.length > 0) {
|
|
193
|
+
Object.defineProperty(cell, "style", {
|
|
194
|
+
value: this._styleSequence,
|
|
195
|
+
writable: true,
|
|
196
|
+
configurable: true
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
row[x] = cell;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
_eraseLine(y, fromX, toX) {
|
|
203
|
+
const row = this._screen[y];
|
|
204
|
+
if (!row) return;
|
|
205
|
+
for (let x = fromX; x <= toX && x < this._cols; x++) {
|
|
206
|
+
row[x] = null;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
_scrollUp(count) {
|
|
210
|
+
for (let i = 0; i < count; i++) {
|
|
211
|
+
this._screen.splice(this._scrollTop, 1);
|
|
212
|
+
this._screen.splice(this._scrollBottom, 0, this._makeRow(this._cols));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
_scrollDown(count) {
|
|
216
|
+
for (let i = 0; i < count; i++) {
|
|
217
|
+
this._screen.splice(this._scrollBottom, 1);
|
|
218
|
+
this._screen.splice(this._scrollTop, 0, this._makeRow(this._cols));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
_newline() {
|
|
222
|
+
if (this._cursorY === this._scrollBottom) {
|
|
223
|
+
this._scrollUp(1);
|
|
224
|
+
} else {
|
|
225
|
+
this._cursorY = Math.min(this._cursorY + 1, this._rows - 1);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
_parseCsiParams() {
|
|
229
|
+
if (!this._csiParams) return [];
|
|
230
|
+
return this._csiParams.split(";").map((s) => s === "" ? 0 : parseInt(s, 10));
|
|
231
|
+
}
|
|
232
|
+
_execCsi(final) {
|
|
233
|
+
const params17 = this._parseCsiParams();
|
|
234
|
+
const p0 = params17[0] ?? 0;
|
|
235
|
+
const p1 = params17[1] ?? 0;
|
|
236
|
+
if (this._csiPrivate === "?") {
|
|
237
|
+
if (final === "h" || final === "l") {
|
|
238
|
+
if (params17.includes(1049)) {
|
|
239
|
+
if (final === "h") {
|
|
240
|
+
this._screen = this._makeScreen(this._cols, this._rows);
|
|
241
|
+
this._cursorX = 0;
|
|
242
|
+
this._cursorY = 0;
|
|
243
|
+
} else {
|
|
244
|
+
this._screen = this._makeScreen(this._cols, this._rows);
|
|
245
|
+
this._cursorX = 0;
|
|
246
|
+
this._cursorY = 0;
|
|
247
|
+
}
|
|
248
|
+
this._resetStyle();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
switch (final) {
|
|
254
|
+
case "A":
|
|
255
|
+
this._cursorY = this._clamp(this._cursorY - Math.max(1, p0), 0, this._rows - 1);
|
|
256
|
+
break;
|
|
257
|
+
case "B":
|
|
258
|
+
this._cursorY = this._clamp(this._cursorY + Math.max(1, p0), 0, this._rows - 1);
|
|
259
|
+
break;
|
|
260
|
+
case "C":
|
|
261
|
+
// cursor forward
|
|
262
|
+
case "a":
|
|
263
|
+
this._cursorX = this._clamp(this._cursorX + Math.max(1, p0), 0, this._cols - 1);
|
|
264
|
+
break;
|
|
265
|
+
case "D":
|
|
266
|
+
this._cursorX = this._clamp(this._cursorX - Math.max(1, p0), 0, this._cols - 1);
|
|
267
|
+
break;
|
|
268
|
+
case "E":
|
|
269
|
+
this._cursorY = this._clamp(this._cursorY + Math.max(1, p0), 0, this._rows - 1);
|
|
270
|
+
this._cursorX = 0;
|
|
271
|
+
break;
|
|
272
|
+
case "F":
|
|
273
|
+
this._cursorY = this._clamp(this._cursorY - Math.max(1, p0), 0, this._rows - 1);
|
|
274
|
+
this._cursorX = 0;
|
|
275
|
+
break;
|
|
276
|
+
case "G":
|
|
277
|
+
// cursor horizontal absolute
|
|
278
|
+
case "`":
|
|
279
|
+
this._cursorX = this._clamp(Math.max(1, p0) - 1, 0, this._cols - 1);
|
|
280
|
+
break;
|
|
281
|
+
case "H":
|
|
282
|
+
// cursor position
|
|
283
|
+
case "f":
|
|
284
|
+
this._cursorY = this._clamp(Math.max(1, p0) - 1, 0, this._rows - 1);
|
|
285
|
+
this._cursorX = this._clamp(Math.max(1, p1) - 1, 0, this._cols - 1);
|
|
286
|
+
break;
|
|
287
|
+
case "I":
|
|
288
|
+
for (let i = 0; i < Math.max(1, p0); i++) {
|
|
289
|
+
this._cursorX = Math.min(this._cols - 1, (Math.floor(this._cursorX / 8) + 1) * 8);
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
case "J":
|
|
293
|
+
if (p0 === 0) {
|
|
294
|
+
this._eraseLine(this._cursorY, this._cursorX, this._cols - 1);
|
|
295
|
+
for (let y = this._cursorY + 1; y < this._rows; y++) this._eraseLine(y, 0, this._cols - 1);
|
|
296
|
+
} else if (p0 === 1) {
|
|
297
|
+
for (let y = 0; y < this._cursorY; y++) this._eraseLine(y, 0, this._cols - 1);
|
|
298
|
+
this._eraseLine(this._cursorY, 0, this._cursorX);
|
|
299
|
+
} else if (p0 === 2 || p0 === 3) {
|
|
300
|
+
for (let y = 0; y < this._rows; y++) this._eraseLine(y, 0, this._cols - 1);
|
|
301
|
+
}
|
|
302
|
+
break;
|
|
303
|
+
case "K":
|
|
304
|
+
if (p0 === 0) this._eraseLine(this._cursorY, this._cursorX, this._cols - 1);
|
|
305
|
+
else if (p0 === 1) this._eraseLine(this._cursorY, 0, this._cursorX);
|
|
306
|
+
else if (p0 === 2) this._eraseLine(this._cursorY, 0, this._cols - 1);
|
|
307
|
+
break;
|
|
308
|
+
case "X":
|
|
309
|
+
this._eraseLine(this._cursorY, this._cursorX, this._cursorX + Math.max(1, p0) - 1);
|
|
310
|
+
break;
|
|
311
|
+
case "L": {
|
|
312
|
+
const n = Math.max(1, p0);
|
|
313
|
+
for (let i = 0; i < n; i++) {
|
|
314
|
+
this._screen.splice(this._scrollBottom, 1);
|
|
315
|
+
this._screen.splice(this._cursorY, 0, this._makeRow(this._cols));
|
|
316
|
+
}
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
case "M": {
|
|
320
|
+
const n = Math.max(1, p0);
|
|
321
|
+
for (let i = 0; i < n; i++) {
|
|
322
|
+
this._screen.splice(this._cursorY, 1);
|
|
323
|
+
this._screen.splice(this._scrollBottom, 0, this._makeRow(this._cols));
|
|
324
|
+
}
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
case "P": {
|
|
328
|
+
const row = this._screen[this._cursorY];
|
|
329
|
+
if (row) {
|
|
330
|
+
const n = Math.max(1, p0);
|
|
331
|
+
row.splice(this._cursorX, n);
|
|
332
|
+
while (row.length < this._cols) row.push(null);
|
|
333
|
+
}
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
case "@": {
|
|
337
|
+
const row = this._screen[this._cursorY];
|
|
338
|
+
if (row) {
|
|
339
|
+
const n = Math.max(1, p0);
|
|
340
|
+
for (let i = 0; i < n; i++) row.splice(this._cursorX, 0, null);
|
|
341
|
+
row.splice(this._cols);
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
case "S":
|
|
346
|
+
this._scrollUp(Math.max(1, p0));
|
|
347
|
+
break;
|
|
348
|
+
case "T":
|
|
349
|
+
if (params17.length <= 1) this._scrollDown(Math.max(1, p0));
|
|
350
|
+
break;
|
|
351
|
+
case "Z": {
|
|
352
|
+
const n = Math.max(1, p0);
|
|
353
|
+
for (let i = 0; i < n; i++) {
|
|
354
|
+
this._cursorX = Math.max(0, (Math.ceil(this._cursorX / 8) - 1) * 8);
|
|
355
|
+
}
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
case "d":
|
|
359
|
+
this._cursorY = this._clamp(Math.max(1, p0) - 1, 0, this._rows - 1);
|
|
360
|
+
break;
|
|
361
|
+
case "e":
|
|
362
|
+
this._cursorY = this._clamp(this._cursorY + Math.max(1, p0), 0, this._rows - 1);
|
|
363
|
+
break;
|
|
364
|
+
case "r": {
|
|
365
|
+
const top = this._clamp(Math.max(1, p0) - 1, 0, this._rows - 1);
|
|
366
|
+
const bottom = this._clamp((p1 === 0 ? this._rows : p1) - 1, 0, this._rows - 1);
|
|
367
|
+
if (top < bottom) {
|
|
368
|
+
this._scrollTop = top;
|
|
369
|
+
this._scrollBottom = bottom;
|
|
370
|
+
}
|
|
371
|
+
this._cursorX = 0;
|
|
372
|
+
this._cursorY = 0;
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
case "s":
|
|
376
|
+
this._savedCursor = { x: this._cursorX, y: this._cursorY };
|
|
377
|
+
break;
|
|
378
|
+
case "u":
|
|
379
|
+
this._cursorX = this._clamp(this._savedCursor.x, 0, this._cols - 1);
|
|
380
|
+
this._cursorY = this._clamp(this._savedCursor.y, 0, this._rows - 1);
|
|
381
|
+
break;
|
|
382
|
+
case "m":
|
|
383
|
+
this._applySgr(params17);
|
|
384
|
+
break;
|
|
385
|
+
default:
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
_feed(ch) {
|
|
390
|
+
const code = ch.charCodeAt(0);
|
|
391
|
+
switch (this._state) {
|
|
392
|
+
case 0 /* Normal */:
|
|
393
|
+
this._feedNormal(ch, code);
|
|
394
|
+
break;
|
|
395
|
+
case 1 /* Escape */:
|
|
396
|
+
this._feedEscape(ch, code);
|
|
397
|
+
break;
|
|
398
|
+
case 2 /* Csi */:
|
|
399
|
+
this._feedCsi(ch, code);
|
|
400
|
+
break;
|
|
401
|
+
case 3 /* Osc */:
|
|
402
|
+
if (code === 7 || code === 156) {
|
|
403
|
+
this._state = 0 /* Normal */;
|
|
404
|
+
} else if (code === 27) {
|
|
405
|
+
this._state = 0 /* Normal */;
|
|
406
|
+
}
|
|
407
|
+
break;
|
|
408
|
+
case 4 /* Str */:
|
|
409
|
+
if (code === 156 || code === 7) {
|
|
410
|
+
this._state = 0 /* Normal */;
|
|
411
|
+
} else if (code === 27) {
|
|
412
|
+
this._state = 0 /* Normal */;
|
|
413
|
+
}
|
|
414
|
+
break;
|
|
415
|
+
case 5 /* EscCharset */:
|
|
416
|
+
this._state = 0 /* Normal */;
|
|
417
|
+
break;
|
|
418
|
+
case 6 /* EscHash */:
|
|
419
|
+
this._state = 0 /* Normal */;
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
_feedNormal(ch, code) {
|
|
424
|
+
if (code === 27) {
|
|
425
|
+
this._state = 1 /* Escape */;
|
|
426
|
+
} else if (code === 155) {
|
|
427
|
+
this._csiParams = "";
|
|
428
|
+
this._csiPrivate = "";
|
|
429
|
+
this._state = 2 /* Csi */;
|
|
430
|
+
} else if (code === 157) {
|
|
431
|
+
this._state = 3 /* Osc */;
|
|
432
|
+
} else if (code === 144 || code === 152 || code === 158 || code === 159) {
|
|
433
|
+
this._state = 4 /* Str */;
|
|
434
|
+
} else if (code === 7 || code === 5 || code === 6) {
|
|
435
|
+
} else if (code === 8) {
|
|
436
|
+
if (this._cursorX > 0) this._cursorX--;
|
|
437
|
+
} else if (code === 127) {
|
|
438
|
+
if (this._cursorX > 0) {
|
|
439
|
+
this._cursorX--;
|
|
440
|
+
this._setChar(this._cursorY, this._cursorX, " ");
|
|
441
|
+
}
|
|
442
|
+
} else if (code === 9) {
|
|
443
|
+
this._cursorX = Math.min(this._cols - 1, (Math.floor(this._cursorX / 8) + 1) * 8);
|
|
444
|
+
} else if (code === 10 || code === 11 || code === 12) {
|
|
445
|
+
this._newline();
|
|
446
|
+
} else if (code === 13) {
|
|
447
|
+
this._cursorX = 0;
|
|
448
|
+
} else if (code === 14 || code === 15) {
|
|
449
|
+
} else if (code >= 32 && code !== 127) {
|
|
450
|
+
this._setChar(this._cursorY, this._cursorX, ch);
|
|
451
|
+
this._cursorX++;
|
|
452
|
+
if (this._cursorX >= this._cols) {
|
|
453
|
+
this._cursorX = 0;
|
|
454
|
+
this._newline();
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
_feedEscape(ch, code) {
|
|
459
|
+
this._state = 0 /* Normal */;
|
|
460
|
+
if (code === 91) {
|
|
461
|
+
this._csiParams = "";
|
|
462
|
+
this._csiPrivate = "";
|
|
463
|
+
this._state = 2 /* Csi */;
|
|
464
|
+
} else if (code === 93) {
|
|
465
|
+
this._state = 3 /* Osc */;
|
|
466
|
+
} else if (code === 80 || code === 88 || code === 94 || code === 95) {
|
|
467
|
+
this._state = 4 /* Str */;
|
|
468
|
+
} else if (code === 40 || code === 41 || code === 42 || code === 43 || code === 45 || code === 46) {
|
|
469
|
+
this._state = 5 /* EscCharset */;
|
|
470
|
+
} else if (code === 35) {
|
|
471
|
+
this._state = 6 /* EscHash */;
|
|
472
|
+
} else if (code === 55) {
|
|
473
|
+
this._savedCursor = { x: this._cursorX, y: this._cursorY };
|
|
474
|
+
} else if (code === 56) {
|
|
475
|
+
this._cursorX = this._clamp(this._savedCursor.x, 0, this._cols - 1);
|
|
476
|
+
this._cursorY = this._clamp(this._savedCursor.y, 0, this._rows - 1);
|
|
477
|
+
} else if (code === 68) {
|
|
478
|
+
this._newline();
|
|
479
|
+
} else if (code === 69) {
|
|
480
|
+
this._cursorX = 0;
|
|
481
|
+
this._newline();
|
|
482
|
+
} else if (code === 77) {
|
|
483
|
+
if (this._cursorY === this._scrollTop) {
|
|
484
|
+
this._scrollDown(1);
|
|
485
|
+
} else {
|
|
486
|
+
this._cursorY = Math.max(0, this._cursorY - 1);
|
|
487
|
+
}
|
|
488
|
+
} else if (code === 72) {
|
|
489
|
+
} else if (code === 99) {
|
|
490
|
+
this._screen = this._makeScreen(this._cols, this._rows);
|
|
491
|
+
this._cursorX = 0;
|
|
492
|
+
this._cursorY = 0;
|
|
493
|
+
this._savedCursor = { x: 0, y: 0 };
|
|
494
|
+
this._scrollTop = 0;
|
|
495
|
+
this._scrollBottom = this._rows - 1;
|
|
496
|
+
this._resetStyle();
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
_feedCsi(ch, code) {
|
|
500
|
+
if (code >= 64 && code <= 126) {
|
|
501
|
+
this._execCsi(ch);
|
|
502
|
+
this._state = 0 /* Normal */;
|
|
503
|
+
} else if (code === 63 || code === 33 || code === 62 || code === 32) {
|
|
504
|
+
this._csiPrivate = ch;
|
|
505
|
+
} else if (code >= 48 && code <= 57 || code === 59) {
|
|
506
|
+
this._csiParams += ch;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
_resetStyle() {
|
|
510
|
+
this._style = createDefaultStyleState();
|
|
511
|
+
this._styleSequence = "";
|
|
512
|
+
}
|
|
513
|
+
_applySgr(params17) {
|
|
514
|
+
const normalizedParams = params17.length === 0 ? [0] : params17;
|
|
515
|
+
for (let index = 0; index < normalizedParams.length; index += 1) {
|
|
516
|
+
const value = normalizedParams[index] ?? 0;
|
|
517
|
+
switch (value) {
|
|
518
|
+
case 0:
|
|
519
|
+
this._resetStyle();
|
|
520
|
+
break;
|
|
521
|
+
case 1:
|
|
522
|
+
this._style.bold = true;
|
|
523
|
+
break;
|
|
524
|
+
case 2:
|
|
525
|
+
this._style.dim = true;
|
|
526
|
+
break;
|
|
527
|
+
case 3:
|
|
528
|
+
this._style.italic = true;
|
|
529
|
+
break;
|
|
530
|
+
case 4:
|
|
531
|
+
this._style.underline = true;
|
|
532
|
+
break;
|
|
533
|
+
case 7:
|
|
534
|
+
this._style.inverse = true;
|
|
535
|
+
break;
|
|
536
|
+
case 9:
|
|
537
|
+
this._style.strikethrough = true;
|
|
538
|
+
break;
|
|
539
|
+
case 21:
|
|
540
|
+
case 22:
|
|
541
|
+
this._style.bold = false;
|
|
542
|
+
this._style.dim = false;
|
|
543
|
+
break;
|
|
544
|
+
case 23:
|
|
545
|
+
this._style.italic = false;
|
|
546
|
+
break;
|
|
547
|
+
case 24:
|
|
548
|
+
this._style.underline = false;
|
|
549
|
+
break;
|
|
550
|
+
case 27:
|
|
551
|
+
this._style.inverse = false;
|
|
552
|
+
break;
|
|
553
|
+
case 29:
|
|
554
|
+
this._style.strikethrough = false;
|
|
555
|
+
break;
|
|
556
|
+
case 39:
|
|
557
|
+
this._style.fg = void 0;
|
|
558
|
+
break;
|
|
559
|
+
case 49:
|
|
560
|
+
this._style.bg = void 0;
|
|
561
|
+
break;
|
|
562
|
+
case 38:
|
|
563
|
+
case 48:
|
|
564
|
+
index = this._applyExtendedColor(value, normalizedParams, index);
|
|
565
|
+
break;
|
|
566
|
+
default:
|
|
567
|
+
if (value >= 30 && value <= 37 || value >= 90 && value <= 97) {
|
|
568
|
+
this._style.fg = [value];
|
|
569
|
+
} else if (value >= 40 && value <= 47 || value >= 100 && value <= 107) {
|
|
570
|
+
this._style.bg = [value];
|
|
571
|
+
}
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
this._styleSequence = serializeStyleState(this._style);
|
|
576
|
+
}
|
|
577
|
+
_applyExtendedColor(control, params17, index) {
|
|
578
|
+
const mode = params17[index + 1];
|
|
579
|
+
const target = control === 38 ? "fg" : "bg";
|
|
580
|
+
if (mode === 5) {
|
|
581
|
+
const paletteIndex = params17[index + 2];
|
|
582
|
+
if (paletteIndex !== void 0) {
|
|
583
|
+
this._style[target] = [control, 5, paletteIndex];
|
|
584
|
+
return index + 2;
|
|
585
|
+
}
|
|
586
|
+
return index;
|
|
587
|
+
}
|
|
588
|
+
if (mode === 2) {
|
|
589
|
+
const red = params17[index + 2];
|
|
590
|
+
const green = params17[index + 3];
|
|
591
|
+
const blue = params17[index + 4];
|
|
592
|
+
if (red !== void 0 && green !== void 0 && blue !== void 0) {
|
|
593
|
+
this._style[target] = [control, 2, red, green, blue];
|
|
594
|
+
return index + 4;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return index;
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
function createDefaultStyleState() {
|
|
601
|
+
return {
|
|
602
|
+
bold: false,
|
|
603
|
+
dim: false,
|
|
604
|
+
italic: false,
|
|
605
|
+
underline: false,
|
|
606
|
+
inverse: false,
|
|
607
|
+
strikethrough: false
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
function serializeStyleState(state) {
|
|
611
|
+
const codes = [];
|
|
612
|
+
if (state.bold) {
|
|
613
|
+
codes.push(1);
|
|
614
|
+
}
|
|
615
|
+
if (state.dim) {
|
|
616
|
+
codes.push(2);
|
|
617
|
+
}
|
|
618
|
+
if (state.italic) {
|
|
619
|
+
codes.push(3);
|
|
620
|
+
}
|
|
621
|
+
if (state.underline) {
|
|
622
|
+
codes.push(4);
|
|
623
|
+
}
|
|
624
|
+
if (state.inverse) {
|
|
625
|
+
codes.push(7);
|
|
626
|
+
}
|
|
627
|
+
if (state.strikethrough) {
|
|
628
|
+
codes.push(9);
|
|
629
|
+
}
|
|
630
|
+
if (state.fg !== void 0) {
|
|
631
|
+
codes.push(...state.fg);
|
|
632
|
+
}
|
|
633
|
+
if (state.bg !== void 0) {
|
|
634
|
+
codes.push(...state.bg);
|
|
635
|
+
}
|
|
636
|
+
return codes.length === 0 ? "" : `\x1B[${codes.join(";")}m`;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// src/keys.ts
|
|
640
|
+
var NAMED_KEY_SEQUENCES = {
|
|
641
|
+
Enter: "\r",
|
|
642
|
+
Tab: " ",
|
|
643
|
+
Escape: "\x1B",
|
|
644
|
+
Backspace: "\x7F",
|
|
645
|
+
Delete: "\x1B[3~",
|
|
646
|
+
ArrowUp: "\x1B[A",
|
|
647
|
+
ArrowDown: "\x1B[B",
|
|
648
|
+
ArrowRight: "\x1B[C",
|
|
649
|
+
ArrowLeft: "\x1B[D",
|
|
650
|
+
Home: "\x1B[H",
|
|
651
|
+
End: "\x1B[F",
|
|
652
|
+
PageUp: "\x1B[5~",
|
|
653
|
+
PageDown: "\x1B[6~",
|
|
654
|
+
Space: " "
|
|
655
|
+
};
|
|
656
|
+
var NAMED_KEY_LOWER = new Map(
|
|
657
|
+
Object.entries(NAMED_KEY_SEQUENCES).map(([k, v]) => [k.toLowerCase(), v])
|
|
658
|
+
);
|
|
659
|
+
var VALID_KEYS_HINT = `Valid keys: ${Object.keys(NAMED_KEY_SEQUENCES).join(", ")}, Control+<letter>, Alt+<key>`;
|
|
660
|
+
function unknownKeyError(key) {
|
|
661
|
+
return new Error(`Unknown terminal key: ${key}. ${VALID_KEYS_HINT}`);
|
|
662
|
+
}
|
|
663
|
+
function keyToSequence(key) {
|
|
664
|
+
const lowerKey = key.toLowerCase();
|
|
665
|
+
const namedSequence = NAMED_KEY_LOWER.get(lowerKey);
|
|
666
|
+
if (namedSequence !== void 0) {
|
|
667
|
+
return namedSequence;
|
|
668
|
+
}
|
|
669
|
+
if (lowerKey.startsWith("control+")) {
|
|
670
|
+
return controlKeyToSequence(key.slice("control+".length));
|
|
671
|
+
}
|
|
672
|
+
if (lowerKey.startsWith("alt+")) {
|
|
673
|
+
const nestedKey = key.slice("alt+".length);
|
|
674
|
+
if (nestedKey.length === 0) {
|
|
675
|
+
throw unknownKeyError(key);
|
|
676
|
+
}
|
|
677
|
+
if (nestedKey.length === 1) {
|
|
678
|
+
return "\x1B" + nestedKey;
|
|
679
|
+
}
|
|
680
|
+
try {
|
|
681
|
+
return "\x1B" + keyToSequence(nestedKey);
|
|
682
|
+
} catch {
|
|
683
|
+
throw unknownKeyError(key);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
if (key.length === 1) {
|
|
687
|
+
return key;
|
|
688
|
+
}
|
|
689
|
+
throw unknownKeyError(key);
|
|
690
|
+
}
|
|
691
|
+
function controlKeyToSequence(controlKey) {
|
|
692
|
+
if (controlKey.length !== 1) {
|
|
693
|
+
throw unknownKeyError(`Control+${controlKey}`);
|
|
694
|
+
}
|
|
695
|
+
const uppercaseLetter = controlKey.toUpperCase();
|
|
696
|
+
const charCode = uppercaseLetter.charCodeAt(0);
|
|
697
|
+
if (charCode < 65 || charCode > 90) {
|
|
698
|
+
throw unknownKeyError(`Control+${controlKey}`);
|
|
699
|
+
}
|
|
700
|
+
return String.fromCharCode(charCode - 64);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// src/terminal-screen.ts
|
|
704
|
+
var TerminalScreen = class {
|
|
705
|
+
lines;
|
|
706
|
+
rawLines;
|
|
707
|
+
cursor;
|
|
708
|
+
size;
|
|
709
|
+
constructor({
|
|
710
|
+
lines,
|
|
711
|
+
rawLines,
|
|
712
|
+
cursor,
|
|
713
|
+
size
|
|
714
|
+
}) {
|
|
715
|
+
this.lines = Object.freeze(lines.map((line) => stripAnsi(line)));
|
|
716
|
+
this.rawLines = Object.freeze([...rawLines]);
|
|
717
|
+
this.cursor = Object.freeze({ ...cursor });
|
|
718
|
+
this.size = Object.freeze({ ...size });
|
|
719
|
+
Object.freeze(this);
|
|
720
|
+
}
|
|
721
|
+
get text() {
|
|
722
|
+
return this.lines.join("\n");
|
|
723
|
+
}
|
|
724
|
+
contains(substring) {
|
|
725
|
+
return this.text.includes(substring);
|
|
726
|
+
}
|
|
727
|
+
line(index) {
|
|
728
|
+
const normalizedIndex = index < 0 ? this.lines.length + index : index;
|
|
729
|
+
const line = this.lines[normalizedIndex];
|
|
730
|
+
if (line === void 0) {
|
|
731
|
+
throw new RangeError(`Line index out of bounds: ${index}`);
|
|
732
|
+
}
|
|
733
|
+
return line;
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
// src/terminal-session.ts
|
|
738
|
+
var DEFAULT_COLS = 120;
|
|
739
|
+
var DEFAULT_ROWS = 40;
|
|
740
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
741
|
+
var WAIT_FOR_POLL_MS = 10;
|
|
742
|
+
var TYPE_DELAY_MS = 15;
|
|
743
|
+
var CLOSE_AFTER_SIGNAL_GRACE_MS = 250;
|
|
744
|
+
var CLOSE_AFTER_SIGTERM_MS = 1e3;
|
|
745
|
+
var TerminalSession = class {
|
|
746
|
+
id;
|
|
747
|
+
command;
|
|
748
|
+
pid;
|
|
749
|
+
exitCode = null;
|
|
750
|
+
pty;
|
|
751
|
+
terminal;
|
|
752
|
+
emitter = new EventEmitter();
|
|
753
|
+
exitPromise;
|
|
754
|
+
rawBuffer = "";
|
|
755
|
+
lastDataAt = Date.now();
|
|
756
|
+
currentCols;
|
|
757
|
+
currentRows;
|
|
758
|
+
closeRequested = false;
|
|
759
|
+
signalRequested = false;
|
|
760
|
+
constructor({
|
|
761
|
+
id,
|
|
762
|
+
command,
|
|
763
|
+
args = [],
|
|
764
|
+
cwd = process.cwd(),
|
|
765
|
+
env = process.env,
|
|
766
|
+
cols = DEFAULT_COLS,
|
|
767
|
+
rows = DEFAULT_ROWS,
|
|
768
|
+
observe = false
|
|
769
|
+
}) {
|
|
770
|
+
this.id = id;
|
|
771
|
+
this.command = command;
|
|
772
|
+
this.currentCols = cols;
|
|
773
|
+
this.currentRows = rows;
|
|
774
|
+
this.terminal = new TerminalBuffer(cols, rows);
|
|
775
|
+
this.pty = createPtyProcess({ command, args, cwd, env, cols, rows });
|
|
776
|
+
this.pid = this.pty.pid;
|
|
777
|
+
const dataSubscription = this.pty.onData((chunk) => {
|
|
778
|
+
this.rawBuffer += chunk;
|
|
779
|
+
this.lastDataAt = Date.now();
|
|
780
|
+
this.terminal.write(chunk);
|
|
781
|
+
if (observe) {
|
|
782
|
+
process.stderr.write(chunk);
|
|
783
|
+
}
|
|
784
|
+
});
|
|
785
|
+
let exitSubscription;
|
|
786
|
+
this.exitPromise = new Promise((resolve) => {
|
|
787
|
+
exitSubscription = this.pty.onExit(({ exitCode }) => {
|
|
788
|
+
if (this.exitCode !== null) {
|
|
789
|
+
resolve(this.exitCode);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
this.exitCode = exitCode;
|
|
793
|
+
dataSubscription.dispose();
|
|
794
|
+
exitSubscription?.dispose();
|
|
795
|
+
this.emitter.emit("exit", exitCode);
|
|
796
|
+
resolve(exitCode);
|
|
797
|
+
});
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
async type(text) {
|
|
801
|
+
for (const character of text) {
|
|
802
|
+
await this.send(character);
|
|
803
|
+
await sleep(TYPE_DELAY_MS);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
async fill(text) {
|
|
807
|
+
await this.send(text.replace(/\r?\n/g, "\r"));
|
|
808
|
+
}
|
|
809
|
+
async press(key) {
|
|
810
|
+
await this.send(keyToSequence(key));
|
|
811
|
+
}
|
|
812
|
+
async send(raw) {
|
|
813
|
+
if (this.exitCode !== null) {
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
this.pty.write(raw);
|
|
817
|
+
}
|
|
818
|
+
async signal(sig) {
|
|
819
|
+
if (this.exitCode !== null) {
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
this.signalRequested = true;
|
|
823
|
+
this.pty.kill(sig);
|
|
824
|
+
}
|
|
825
|
+
async waitFor(pattern, opts) {
|
|
826
|
+
const timeout = opts?.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
827
|
+
const startedAt = Date.now();
|
|
828
|
+
while (Date.now() - startedAt <= timeout) {
|
|
829
|
+
const matched = matchPattern(this.rawBuffer, pattern);
|
|
830
|
+
if (matched !== null) {
|
|
831
|
+
return matched;
|
|
832
|
+
}
|
|
833
|
+
await sleep(WAIT_FOR_POLL_MS);
|
|
834
|
+
}
|
|
835
|
+
throw new Error(`Timed out waiting for pattern after ${timeout}ms: ${String(pattern)}`);
|
|
836
|
+
}
|
|
837
|
+
async waitForQuiet(ms) {
|
|
838
|
+
while (true) {
|
|
839
|
+
const remaining = ms - (Date.now() - this.lastDataAt);
|
|
840
|
+
if (remaining <= 0) {
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
await sleep(remaining);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
async screen() {
|
|
847
|
+
const rawLines = [];
|
|
848
|
+
for (let row = 0; row < this.currentRows; row += 1) {
|
|
849
|
+
rawLines.push(this.terminal.renderLine(row));
|
|
850
|
+
}
|
|
851
|
+
return new TerminalScreen({
|
|
852
|
+
lines: rawLines,
|
|
853
|
+
rawLines,
|
|
854
|
+
cursor: {
|
|
855
|
+
row: this.terminal.displayBuffer.cursorY,
|
|
856
|
+
col: this.terminal.displayBuffer.cursorX
|
|
857
|
+
},
|
|
858
|
+
size: {
|
|
859
|
+
rows: this.currentRows,
|
|
860
|
+
cols: this.currentCols
|
|
861
|
+
}
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
async history(opts) {
|
|
865
|
+
const normalized = normalizeHistoryBuffer(stripAnsi(this.rawBuffer));
|
|
866
|
+
const lines = splitHistoryLines(normalized);
|
|
867
|
+
if (opts?.last === void 0) {
|
|
868
|
+
return lines;
|
|
869
|
+
}
|
|
870
|
+
const start = Math.max(0, lines.length - opts.last);
|
|
871
|
+
return lines.slice(start);
|
|
872
|
+
}
|
|
873
|
+
async resize(cols, rows) {
|
|
874
|
+
this.currentCols = cols;
|
|
875
|
+
this.currentRows = rows;
|
|
876
|
+
if (this.exitCode === null) {
|
|
877
|
+
this.pty.resize(cols, rows);
|
|
878
|
+
}
|
|
879
|
+
this.terminal.resize(cols, rows);
|
|
880
|
+
}
|
|
881
|
+
async waitForExit(opts) {
|
|
882
|
+
if (this.exitCode !== null) {
|
|
883
|
+
return this.exitCode;
|
|
884
|
+
}
|
|
885
|
+
if (opts?.timeout !== void 0) {
|
|
886
|
+
const result = await waitForExit(this.exitPromise, opts.timeout);
|
|
887
|
+
if (result === null) {
|
|
888
|
+
throw new Error(`Timed out waiting for process to exit after ${opts.timeout}ms`);
|
|
889
|
+
}
|
|
890
|
+
return result;
|
|
891
|
+
}
|
|
892
|
+
return this.exitPromise;
|
|
893
|
+
}
|
|
894
|
+
async close() {
|
|
895
|
+
if (this.exitCode !== null) {
|
|
896
|
+
return this.exitCode;
|
|
897
|
+
}
|
|
898
|
+
if (!this.closeRequested) {
|
|
899
|
+
this.closeRequested = true;
|
|
900
|
+
const gracefulExitCode = await waitForExit(this.exitPromise, CLOSE_AFTER_SIGNAL_GRACE_MS);
|
|
901
|
+
if (gracefulExitCode !== null) {
|
|
902
|
+
return gracefulExitCode;
|
|
903
|
+
}
|
|
904
|
+
if (this.signalRequested) {
|
|
905
|
+
return this.exitPromise;
|
|
906
|
+
}
|
|
907
|
+
if (this.exitCode === null) {
|
|
908
|
+
this.pty.kill("SIGTERM");
|
|
909
|
+
const afterSigterm = await waitForExit(this.exitPromise, CLOSE_AFTER_SIGTERM_MS);
|
|
910
|
+
if (afterSigterm !== null) {
|
|
911
|
+
return afterSigterm;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
if (this.exitCode === null) {
|
|
915
|
+
this.pty.kill("SIGKILL");
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
return this.exitPromise;
|
|
919
|
+
}
|
|
920
|
+
on(event, cb) {
|
|
921
|
+
this.emitter.on(event, cb);
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
function createPtyProcess({
|
|
925
|
+
command,
|
|
926
|
+
args,
|
|
927
|
+
cwd,
|
|
928
|
+
env,
|
|
929
|
+
cols,
|
|
930
|
+
rows
|
|
931
|
+
}) {
|
|
932
|
+
try {
|
|
933
|
+
return nodePty.spawn(command, args, {
|
|
934
|
+
cwd,
|
|
935
|
+
env,
|
|
936
|
+
cols,
|
|
937
|
+
rows,
|
|
938
|
+
encoding: "utf8"
|
|
939
|
+
});
|
|
940
|
+
} catch {
|
|
941
|
+
return createChildProcessFallback({ command, args, cwd, env });
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
function createChildProcessFallback({
|
|
945
|
+
command,
|
|
946
|
+
args,
|
|
947
|
+
cwd,
|
|
948
|
+
env
|
|
949
|
+
}) {
|
|
950
|
+
const child = spawnChildProcess(command, args, {
|
|
951
|
+
cwd,
|
|
952
|
+
env,
|
|
953
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
954
|
+
});
|
|
955
|
+
return new ChildProcessFallback(child);
|
|
956
|
+
}
|
|
957
|
+
var ChildProcessFallback = class {
|
|
958
|
+
pid;
|
|
959
|
+
child;
|
|
960
|
+
dataEmitter = new EventEmitter();
|
|
961
|
+
exitEmitter = new EventEmitter();
|
|
962
|
+
constructor(child) {
|
|
963
|
+
this.child = child;
|
|
964
|
+
this.pid = child.pid ?? -1;
|
|
965
|
+
child.stdout.setEncoding("utf8");
|
|
966
|
+
child.stderr.setEncoding("utf8");
|
|
967
|
+
child.stdout.on("data", this.handleData);
|
|
968
|
+
child.stderr.on("data", this.handleData);
|
|
969
|
+
child.on("exit", (exitCode, signal) => {
|
|
970
|
+
this.exitEmitter.emit("exit", {
|
|
971
|
+
exitCode: exitCode ?? signalToExitCode(signal),
|
|
972
|
+
signal: void 0
|
|
973
|
+
});
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
write(data) {
|
|
977
|
+
this.child.stdin.write(data);
|
|
978
|
+
}
|
|
979
|
+
resize() {
|
|
980
|
+
}
|
|
981
|
+
kill(signal) {
|
|
982
|
+
this.child.kill(signal);
|
|
983
|
+
}
|
|
984
|
+
onData(listener) {
|
|
985
|
+
this.dataEmitter.on("data", listener);
|
|
986
|
+
return {
|
|
987
|
+
dispose: () => {
|
|
988
|
+
this.dataEmitter.off("data", listener);
|
|
989
|
+
}
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
onExit(listener) {
|
|
993
|
+
this.exitEmitter.on("exit", listener);
|
|
994
|
+
return {
|
|
995
|
+
dispose: () => {
|
|
996
|
+
this.exitEmitter.off("exit", listener);
|
|
997
|
+
}
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
handleData = (chunk) => {
|
|
1001
|
+
this.dataEmitter.emit("data", String(chunk));
|
|
1002
|
+
};
|
|
1003
|
+
};
|
|
1004
|
+
function signalToExitCode(signal) {
|
|
1005
|
+
if (signal === null) {
|
|
1006
|
+
return 0;
|
|
1007
|
+
}
|
|
1008
|
+
const signalNumbers = {
|
|
1009
|
+
SIGTERM: 15,
|
|
1010
|
+
SIGINT: 2,
|
|
1011
|
+
SIGHUP: 1,
|
|
1012
|
+
SIGKILL: 9
|
|
1013
|
+
};
|
|
1014
|
+
const signalNumber = signalNumbers[signal];
|
|
1015
|
+
if (signalNumber === void 0) {
|
|
1016
|
+
return 1;
|
|
1017
|
+
}
|
|
1018
|
+
return 128 + signalNumber;
|
|
1019
|
+
}
|
|
1020
|
+
function matchPattern(buffer, pattern) {
|
|
1021
|
+
const clean = normalizeHistoryBuffer(stripAnsi(buffer));
|
|
1022
|
+
for (const line of clean.split("\n")) {
|
|
1023
|
+
if (typeof pattern === "string") {
|
|
1024
|
+
if (line.includes(pattern)) return line;
|
|
1025
|
+
} else {
|
|
1026
|
+
const flags = removeCharacter(pattern.flags, "g");
|
|
1027
|
+
if (new RegExp(pattern.source, flags).test(line)) return line;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
return null;
|
|
1031
|
+
}
|
|
1032
|
+
function removeCharacter(input, charToRemove) {
|
|
1033
|
+
let output = "";
|
|
1034
|
+
for (const character of input) {
|
|
1035
|
+
if (character !== charToRemove) {
|
|
1036
|
+
output += character;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
return output;
|
|
1040
|
+
}
|
|
1041
|
+
function splitHistoryLines(input) {
|
|
1042
|
+
const lines = input.split("\n");
|
|
1043
|
+
if (lines.length > 0 && lines[lines.length - 1] === "") {
|
|
1044
|
+
lines.pop();
|
|
1045
|
+
}
|
|
1046
|
+
return lines;
|
|
1047
|
+
}
|
|
1048
|
+
function normalizeHistoryBuffer(input) {
|
|
1049
|
+
let output = "";
|
|
1050
|
+
for (const character of input) {
|
|
1051
|
+
if (character === "\r" || character === "\b") {
|
|
1052
|
+
continue;
|
|
1053
|
+
}
|
|
1054
|
+
output += character;
|
|
1055
|
+
}
|
|
1056
|
+
return output;
|
|
1057
|
+
}
|
|
1058
|
+
function sleep(ms) {
|
|
1059
|
+
return new Promise((resolve) => {
|
|
1060
|
+
setTimeout(resolve, ms);
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
async function waitForExit(exitPromise, timeout) {
|
|
1064
|
+
return new Promise((resolve) => {
|
|
1065
|
+
let settled = false;
|
|
1066
|
+
const timer = setTimeout(() => {
|
|
1067
|
+
if (settled) {
|
|
1068
|
+
return;
|
|
1069
|
+
}
|
|
1070
|
+
settled = true;
|
|
1071
|
+
resolve(null);
|
|
1072
|
+
}, timeout);
|
|
1073
|
+
void exitPromise.then((code) => {
|
|
1074
|
+
if (settled) {
|
|
1075
|
+
return;
|
|
1076
|
+
}
|
|
1077
|
+
settled = true;
|
|
1078
|
+
clearTimeout(timer);
|
|
1079
|
+
resolve(code);
|
|
1080
|
+
});
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
// src/terminal-pilot.ts
|
|
1085
|
+
var DEFAULT_COLS2 = 120;
|
|
1086
|
+
var DEFAULT_ROWS2 = 40;
|
|
1087
|
+
var TerminalPilot = class _TerminalPilot {
|
|
1088
|
+
sessionMap = /* @__PURE__ */ new Map();
|
|
1089
|
+
static async launch() {
|
|
1090
|
+
return new _TerminalPilot();
|
|
1091
|
+
}
|
|
1092
|
+
async newSession(opts) {
|
|
1093
|
+
const session = new TerminalSession({
|
|
1094
|
+
id: randomUUID(),
|
|
1095
|
+
command: opts.command,
|
|
1096
|
+
args: opts.args,
|
|
1097
|
+
cwd: opts.cwd,
|
|
1098
|
+
env: opts.env,
|
|
1099
|
+
cols: opts.cols ?? DEFAULT_COLS2,
|
|
1100
|
+
rows: opts.rows ?? DEFAULT_ROWS2,
|
|
1101
|
+
observe: opts.observe ?? false
|
|
1102
|
+
});
|
|
1103
|
+
this.sessionMap.set(session.id, session);
|
|
1104
|
+
return session;
|
|
1105
|
+
}
|
|
1106
|
+
getSession(id) {
|
|
1107
|
+
const session = this.sessionMap.get(id);
|
|
1108
|
+
if (session === void 0) {
|
|
1109
|
+
throw new Error(`Session not found: ${id}`);
|
|
1110
|
+
}
|
|
1111
|
+
return session;
|
|
1112
|
+
}
|
|
1113
|
+
deleteSession(id) {
|
|
1114
|
+
this.sessionMap.delete(id);
|
|
1115
|
+
}
|
|
1116
|
+
sessions() {
|
|
1117
|
+
return [...this.sessionMap.values()].filter((s) => s.exitCode === null);
|
|
1118
|
+
}
|
|
1119
|
+
async close() {
|
|
1120
|
+
const sessions = [...this.sessionMap.values()];
|
|
1121
|
+
try {
|
|
1122
|
+
await Promise.all(sessions.map((session) => session.close()));
|
|
1123
|
+
} finally {
|
|
1124
|
+
this.sessionMap.clear();
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1129
|
+
// src/commands/runtime.ts
|
|
1130
|
+
var SESSION_ENV_VAR = "TERMINAL_PILOT_SESSION";
|
|
1131
|
+
var sharedRuntime;
|
|
1132
|
+
function getTerminalPilotRuntime(runtime) {
|
|
1133
|
+
if (runtime !== void 0) {
|
|
1134
|
+
return runtime;
|
|
1135
|
+
}
|
|
1136
|
+
sharedRuntime ??= createTerminalPilotRuntime();
|
|
1137
|
+
return sharedRuntime;
|
|
1138
|
+
}
|
|
1139
|
+
function createTerminalPilotRuntime(options = {}) {
|
|
1140
|
+
const launchPilot = options.launchPilot ?? TerminalPilot.launch;
|
|
1141
|
+
const nameToId = /* @__PURE__ */ new Map();
|
|
1142
|
+
const idToName = /* @__PURE__ */ new Map();
|
|
1143
|
+
let pilotPromise;
|
|
1144
|
+
function getRequestedName(name, env) {
|
|
1145
|
+
return name ?? env?.get(SESSION_ENV_VAR);
|
|
1146
|
+
}
|
|
1147
|
+
async function getPilot() {
|
|
1148
|
+
pilotPromise ??= launchPilot();
|
|
1149
|
+
return pilotPromise;
|
|
1150
|
+
}
|
|
1151
|
+
function nextSessionName() {
|
|
1152
|
+
let index = 1;
|
|
1153
|
+
while (nameToId.has(`s${index}`)) {
|
|
1154
|
+
index += 1;
|
|
1155
|
+
}
|
|
1156
|
+
return `s${index}`;
|
|
1157
|
+
}
|
|
1158
|
+
function rememberSession(name, session) {
|
|
1159
|
+
nameToId.set(name, session.id);
|
|
1160
|
+
idToName.set(session.id, name);
|
|
1161
|
+
return { name, session };
|
|
1162
|
+
}
|
|
1163
|
+
function forgetSession(name, sessionId) {
|
|
1164
|
+
nameToId.delete(name);
|
|
1165
|
+
idToName.delete(sessionId);
|
|
1166
|
+
}
|
|
1167
|
+
function formatAvailableSessions(names) {
|
|
1168
|
+
if (names.length === 0) {
|
|
1169
|
+
return "No active sessions are available.";
|
|
1170
|
+
}
|
|
1171
|
+
return `Available sessions: ${names.join(", ")}.`;
|
|
1172
|
+
}
|
|
1173
|
+
async function lookupNamedSession(name) {
|
|
1174
|
+
const sessionId = nameToId.get(name);
|
|
1175
|
+
if (sessionId === void 0) {
|
|
1176
|
+
const active = await listSessions2();
|
|
1177
|
+
throw new UserError(`Session "${name}" was not found. ${formatAvailableSessions(active.map((entry) => entry.name))}`);
|
|
1178
|
+
}
|
|
1179
|
+
const pilot = await getPilot();
|
|
1180
|
+
try {
|
|
1181
|
+
return { name, session: pilot.getSession(sessionId) };
|
|
1182
|
+
} catch {
|
|
1183
|
+
forgetSession(name, sessionId);
|
|
1184
|
+
const active = await listSessions2();
|
|
1185
|
+
throw new UserError(`Session "${name}" was not found. ${formatAvailableSessions(active.map((entry) => entry.name))}`);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
async function listSessions2() {
|
|
1189
|
+
const pilot = await getPilot();
|
|
1190
|
+
return pilot.sessions().flatMap((session) => {
|
|
1191
|
+
const name = idToName.get(session.id);
|
|
1192
|
+
if (name === void 0) {
|
|
1193
|
+
return [];
|
|
1194
|
+
}
|
|
1195
|
+
return [{ name, session }];
|
|
1196
|
+
});
|
|
1197
|
+
}
|
|
1198
|
+
return {
|
|
1199
|
+
async createSession(params17, env) {
|
|
1200
|
+
const requestedName = getRequestedName(params17.session, env) ?? nextSessionName();
|
|
1201
|
+
if (nameToId.has(requestedName)) {
|
|
1202
|
+
throw new UserError(`Session "${requestedName}" already exists.`);
|
|
1203
|
+
}
|
|
1204
|
+
const pilot = await getPilot();
|
|
1205
|
+
const session = await pilot.newSession({
|
|
1206
|
+
command: params17.command,
|
|
1207
|
+
args: params17.args,
|
|
1208
|
+
cwd: params17.cwd,
|
|
1209
|
+
cols: params17.cols,
|
|
1210
|
+
rows: params17.rows,
|
|
1211
|
+
observe: params17.observe
|
|
1212
|
+
});
|
|
1213
|
+
return rememberSession(requestedName, session);
|
|
1214
|
+
},
|
|
1215
|
+
async resolveSession(name, env) {
|
|
1216
|
+
const requestedName = getRequestedName(name, env);
|
|
1217
|
+
if (requestedName !== void 0) {
|
|
1218
|
+
return lookupNamedSession(requestedName);
|
|
1219
|
+
}
|
|
1220
|
+
const active = await listSessions2();
|
|
1221
|
+
if (active.length === 1) {
|
|
1222
|
+
return active[0];
|
|
1223
|
+
}
|
|
1224
|
+
if (active.length === 0) {
|
|
1225
|
+
throw new UserError("No active sessions. Create one with create-session.");
|
|
1226
|
+
}
|
|
1227
|
+
throw new UserError(
|
|
1228
|
+
`Multiple active sessions require an explicit session name. Pass --session or set ${SESSION_ENV_VAR}. ${formatAvailableSessions(active.map((entry) => entry.name))}`
|
|
1229
|
+
);
|
|
1230
|
+
},
|
|
1231
|
+
async closeSession(name, env) {
|
|
1232
|
+
const namedSession = await this.resolveSession(name, env);
|
|
1233
|
+
const exitCode = await namedSession.session.close();
|
|
1234
|
+
const pilot = await getPilot();
|
|
1235
|
+
pilot.deleteSession(namedSession.session.id);
|
|
1236
|
+
forgetSession(namedSession.name, namedSession.session.id);
|
|
1237
|
+
return {
|
|
1238
|
+
exitCode,
|
|
1239
|
+
name: namedSession.name
|
|
1240
|
+
};
|
|
1241
|
+
},
|
|
1242
|
+
listSessions: listSessions2,
|
|
1243
|
+
async close() {
|
|
1244
|
+
if (pilotPromise === void 0) {
|
|
1245
|
+
return;
|
|
1246
|
+
}
|
|
1247
|
+
const pilot = await pilotPromise;
|
|
1248
|
+
await pilot.close();
|
|
1249
|
+
pilotPromise = void 0;
|
|
1250
|
+
nameToId.clear();
|
|
1251
|
+
idToName.clear();
|
|
1252
|
+
}
|
|
1253
|
+
};
|
|
1254
|
+
}
|
|
1255
|
+
async function closeSharedTerminalPilotRuntime() {
|
|
1256
|
+
if (sharedRuntime === void 0) {
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
await sharedRuntime.close();
|
|
1260
|
+
sharedRuntime = void 0;
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
// src/commands/close-session.ts
|
|
1264
|
+
var params = S.Object({
|
|
1265
|
+
session: S.Optional(S.String({ short: "s", description: "Session name" }))
|
|
1266
|
+
});
|
|
1267
|
+
var closeSession = defineCommand({
|
|
1268
|
+
name: "close-session",
|
|
1269
|
+
description: "Close an active terminal session",
|
|
1270
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1271
|
+
params,
|
|
1272
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1273
|
+
const { exitCode } = await getTerminalPilotRuntime(terminalPilotRuntime).closeSession(params17.session, env);
|
|
1274
|
+
return { exitCode };
|
|
1275
|
+
}
|
|
1276
|
+
});
|
|
1277
|
+
|
|
1278
|
+
// src/commands/create-session.ts
|
|
1279
|
+
import { defineCommand as defineCommand2, S as S2 } from "@poe-code/cmdkit";
|
|
1280
|
+
var params2 = S2.Object({
|
|
1281
|
+
command: S2.String({ description: "Command to execute" }),
|
|
1282
|
+
args: S2.Optional(S2.Array(S2.String(), { description: "Command arguments" })),
|
|
1283
|
+
session: S2.Optional(S2.String({ short: "s", description: "Session name" })),
|
|
1284
|
+
cwd: S2.Optional(S2.String({ description: "Working directory" })),
|
|
1285
|
+
cols: S2.Optional(S2.Number({ description: "Terminal width in columns" })),
|
|
1286
|
+
rows: S2.Optional(S2.Number({ description: "Terminal height in rows" })),
|
|
1287
|
+
observe: S2.Optional(S2.Boolean({ description: "Mirror PTY output to stderr" }))
|
|
1288
|
+
});
|
|
1289
|
+
var createSession = defineCommand2({
|
|
1290
|
+
name: "create-session",
|
|
1291
|
+
description: "Spawn an interactive CLI in a PTY",
|
|
1292
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1293
|
+
positional: ["command", "args"],
|
|
1294
|
+
params: params2,
|
|
1295
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1296
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).createSession(params17, env);
|
|
1297
|
+
return { session: namedSession.name, pid: namedSession.session.pid };
|
|
1298
|
+
}
|
|
1299
|
+
});
|
|
1300
|
+
|
|
1301
|
+
// src/commands/fill.ts
|
|
1302
|
+
import { defineCommand as defineCommand3, S as S3 } from "@poe-code/cmdkit";
|
|
1303
|
+
var params3 = S3.Object({
|
|
1304
|
+
text: S3.String({ description: "Text to write to the session" }),
|
|
1305
|
+
session: S3.Optional(S3.String({ short: "s", description: "Session name" }))
|
|
1306
|
+
});
|
|
1307
|
+
var fill = defineCommand3({
|
|
1308
|
+
name: "fill",
|
|
1309
|
+
description: "Write text to an active terminal session all at once (replaces \\n with \\r)",
|
|
1310
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1311
|
+
positional: ["text"],
|
|
1312
|
+
params: params3,
|
|
1313
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1314
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1315
|
+
await namedSession.session.fill(params17.text);
|
|
1316
|
+
return void 0;
|
|
1317
|
+
}
|
|
1318
|
+
});
|
|
1319
|
+
|
|
1320
|
+
// src/commands/get-session.ts
|
|
1321
|
+
import { defineCommand as defineCommand4, S as S4 } from "@poe-code/cmdkit";
|
|
1322
|
+
var params4 = S4.Object({
|
|
1323
|
+
session: S4.Optional(S4.String({ short: "s", description: "Session name" }))
|
|
1324
|
+
});
|
|
1325
|
+
var getSession = defineCommand4({
|
|
1326
|
+
name: "get-session",
|
|
1327
|
+
description: "Get session metadata (name, pid, command, exitCode)",
|
|
1328
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1329
|
+
params: params4,
|
|
1330
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1331
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1332
|
+
return {
|
|
1333
|
+
session: namedSession.name,
|
|
1334
|
+
pid: namedSession.session.pid,
|
|
1335
|
+
command: namedSession.session.command,
|
|
1336
|
+
exitCode: namedSession.session.exitCode
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
});
|
|
1340
|
+
|
|
1341
|
+
// src/commands/install.ts
|
|
1342
|
+
import { installSkill } from "@poe-code/agent-skill-config";
|
|
1343
|
+
import { defineCommand as defineCommand5, S as S5 } from "@poe-code/cmdkit";
|
|
1344
|
+
|
|
1345
|
+
// src/commands/installer.ts
|
|
1346
|
+
import os from "node:os";
|
|
1347
|
+
import path from "node:path";
|
|
1348
|
+
import * as nodeFs from "node:fs/promises";
|
|
1349
|
+
import { readFile } from "node:fs/promises";
|
|
1350
|
+
import { UserError as UserError2 } from "@poe-code/cmdkit";
|
|
1351
|
+
import {
|
|
1352
|
+
getAgentConfig,
|
|
1353
|
+
resolveAgentSupport as resolveSkillAgentSupport,
|
|
1354
|
+
supportedAgents as skillSupportedAgents
|
|
1355
|
+
} from "@poe-code/agent-skill-config";
|
|
1356
|
+
var DEFAULT_INSTALL_AGENT = "claude-code";
|
|
1357
|
+
var DEFAULT_INSTALL_SCOPE = "local";
|
|
1358
|
+
var TERMINAL_PILOT_SKILL_NAME = "terminal-pilot";
|
|
1359
|
+
var installableAgents = skillSupportedAgents;
|
|
1360
|
+
function isNotFoundError(error) {
|
|
1361
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
1362
|
+
}
|
|
1363
|
+
function resolveInstallerServices(installer) {
|
|
1364
|
+
return {
|
|
1365
|
+
fs: installer?.fs ?? nodeFs,
|
|
1366
|
+
cwd: installer?.cwd ?? process.cwd(),
|
|
1367
|
+
homeDir: installer?.homeDir ?? os.homedir(),
|
|
1368
|
+
platform: installer?.platform ?? process.platform
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1371
|
+
function throwUnsupportedAgent(agent) {
|
|
1372
|
+
throw new UserError2(`Unsupported agent: ${agent}`);
|
|
1373
|
+
}
|
|
1374
|
+
function resolveInstallableAgent(agent) {
|
|
1375
|
+
const skillSupport = resolveSkillAgentSupport(agent);
|
|
1376
|
+
if (skillSupport.status !== "supported" || !skillSupport.id) {
|
|
1377
|
+
throwUnsupportedAgent(agent);
|
|
1378
|
+
}
|
|
1379
|
+
return skillSupport.id;
|
|
1380
|
+
}
|
|
1381
|
+
function resolveInstallScope(input) {
|
|
1382
|
+
if (input.local && input.global) {
|
|
1383
|
+
throw new UserError2("Use either --local or --global, not both.");
|
|
1384
|
+
}
|
|
1385
|
+
if (input.local) {
|
|
1386
|
+
return "local";
|
|
1387
|
+
}
|
|
1388
|
+
if (input.global) {
|
|
1389
|
+
return "global";
|
|
1390
|
+
}
|
|
1391
|
+
return DEFAULT_INSTALL_SCOPE;
|
|
1392
|
+
}
|
|
1393
|
+
var terminalPilotTemplateCache;
|
|
1394
|
+
async function loadTerminalPilotTemplate() {
|
|
1395
|
+
if (terminalPilotTemplateCache !== void 0) {
|
|
1396
|
+
return terminalPilotTemplateCache;
|
|
1397
|
+
}
|
|
1398
|
+
const candidates = [
|
|
1399
|
+
new URL("./templates/terminal-pilot.md", import.meta.url),
|
|
1400
|
+
new URL("../templates/terminal-pilot.md", import.meta.url),
|
|
1401
|
+
new URL("../../../agent-skill-config/src/templates/terminal-pilot.md", import.meta.url)
|
|
1402
|
+
];
|
|
1403
|
+
for (const candidate of candidates) {
|
|
1404
|
+
try {
|
|
1405
|
+
terminalPilotTemplateCache = await readFile(candidate, "utf8");
|
|
1406
|
+
return terminalPilotTemplateCache;
|
|
1407
|
+
} catch (error) {
|
|
1408
|
+
if (!isNotFoundError(error)) {
|
|
1409
|
+
throw error;
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
throw new UserError2("terminal-pilot skill template is missing.");
|
|
1414
|
+
}
|
|
1415
|
+
function resolveHomeRelativePath(targetPath, homeDir) {
|
|
1416
|
+
if (targetPath === "~") {
|
|
1417
|
+
return homeDir;
|
|
1418
|
+
}
|
|
1419
|
+
if (targetPath.startsWith("~/")) {
|
|
1420
|
+
return path.join(homeDir, targetPath.slice(2));
|
|
1421
|
+
}
|
|
1422
|
+
return targetPath;
|
|
1423
|
+
}
|
|
1424
|
+
function getSkillFolderWithHome(agent, scope, cwd, homeDir) {
|
|
1425
|
+
const config = getAgentConfig(agent);
|
|
1426
|
+
if (!config) {
|
|
1427
|
+
throwUnsupportedAgent(agent);
|
|
1428
|
+
}
|
|
1429
|
+
return {
|
|
1430
|
+
displayPath: path.join(
|
|
1431
|
+
scope === "global" ? config.globalSkillDir : config.localSkillDir,
|
|
1432
|
+
TERMINAL_PILOT_SKILL_NAME
|
|
1433
|
+
),
|
|
1434
|
+
fullPath: path.join(
|
|
1435
|
+
scope === "global" ? resolveHomeRelativePath(config.globalSkillDir, homeDir) : path.resolve(cwd, config.localSkillDir),
|
|
1436
|
+
TERMINAL_PILOT_SKILL_NAME
|
|
1437
|
+
)
|
|
1438
|
+
};
|
|
1439
|
+
}
|
|
1440
|
+
async function removeSkillFolder(fs, folderPath) {
|
|
1441
|
+
try {
|
|
1442
|
+
await fs.stat(folderPath);
|
|
1443
|
+
} catch (error) {
|
|
1444
|
+
if (isNotFoundError(error)) {
|
|
1445
|
+
return false;
|
|
1446
|
+
}
|
|
1447
|
+
throw error;
|
|
1448
|
+
}
|
|
1449
|
+
if (typeof fs.rm !== "function") {
|
|
1450
|
+
throw new UserError2("The configured filesystem does not support removing directories.");
|
|
1451
|
+
}
|
|
1452
|
+
await fs.rm(folderPath, { recursive: true, force: true });
|
|
1453
|
+
return true;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
// src/commands/install.ts
|
|
1457
|
+
var params5 = S5.Object({
|
|
1458
|
+
agent: S5.Enum(installableAgents, {
|
|
1459
|
+
description: "Agent to install terminal-pilot for",
|
|
1460
|
+
default: DEFAULT_INSTALL_AGENT
|
|
1461
|
+
}),
|
|
1462
|
+
local: S5.Optional(S5.Boolean({ description: "Install the skill in the current project" })),
|
|
1463
|
+
global: S5.Optional(S5.Boolean({ description: "Install the skill in the user home directory" }))
|
|
1464
|
+
});
|
|
1465
|
+
var install = defineCommand5({
|
|
1466
|
+
name: "install",
|
|
1467
|
+
description: "Install the terminal-pilot CLI skill.",
|
|
1468
|
+
scope: ["cli"],
|
|
1469
|
+
positional: ["agent"],
|
|
1470
|
+
params: params5,
|
|
1471
|
+
handler: async ({ params: params17, terminalPilotInstaller }) => {
|
|
1472
|
+
const services = resolveInstallerServices(terminalPilotInstaller);
|
|
1473
|
+
const agent = resolveInstallableAgent(params17.agent);
|
|
1474
|
+
const scope = resolveInstallScope(params17);
|
|
1475
|
+
const template = await loadTerminalPilotTemplate();
|
|
1476
|
+
const skillResult = await installSkill(
|
|
1477
|
+
agent,
|
|
1478
|
+
{
|
|
1479
|
+
name: TERMINAL_PILOT_SKILL_NAME,
|
|
1480
|
+
content: template
|
|
1481
|
+
},
|
|
1482
|
+
{
|
|
1483
|
+
fs: services.fs,
|
|
1484
|
+
cwd: services.cwd,
|
|
1485
|
+
homeDir: services.homeDir,
|
|
1486
|
+
scope
|
|
1487
|
+
}
|
|
1488
|
+
);
|
|
1489
|
+
return {
|
|
1490
|
+
agent,
|
|
1491
|
+
scope,
|
|
1492
|
+
skillPath: skillResult.displayPath
|
|
1493
|
+
};
|
|
1494
|
+
}
|
|
1495
|
+
});
|
|
1496
|
+
|
|
1497
|
+
// src/commands/list-sessions.ts
|
|
1498
|
+
import { defineCommand as defineCommand6, S as S6 } from "@poe-code/cmdkit";
|
|
1499
|
+
var params6 = S6.Object({});
|
|
1500
|
+
var listSessions = defineCommand6({
|
|
1501
|
+
name: "list-sessions",
|
|
1502
|
+
description: "List active terminal sessions",
|
|
1503
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1504
|
+
params: params6,
|
|
1505
|
+
handler: async ({ terminalPilotRuntime }) => {
|
|
1506
|
+
const sessions = await getTerminalPilotRuntime(terminalPilotRuntime).listSessions();
|
|
1507
|
+
return {
|
|
1508
|
+
sessions: sessions.map((namedSession) => ({
|
|
1509
|
+
session: namedSession.name,
|
|
1510
|
+
command: namedSession.session.command,
|
|
1511
|
+
pid: namedSession.session.pid
|
|
1512
|
+
}))
|
|
1513
|
+
};
|
|
1514
|
+
}
|
|
1515
|
+
});
|
|
1516
|
+
|
|
1517
|
+
// src/commands/press-key.ts
|
|
1518
|
+
import { defineCommand as defineCommand7, S as S7 } from "@poe-code/cmdkit";
|
|
1519
|
+
var params7 = S7.Object({
|
|
1520
|
+
key: S7.String({ description: "Named key to press" }),
|
|
1521
|
+
session: S7.Optional(S7.String({ short: "s", description: "Session name" }))
|
|
1522
|
+
});
|
|
1523
|
+
var pressKey = defineCommand7({
|
|
1524
|
+
name: "press-key",
|
|
1525
|
+
description: "Send a named key press to an active terminal session",
|
|
1526
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1527
|
+
positional: ["key"],
|
|
1528
|
+
params: params7,
|
|
1529
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1530
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1531
|
+
await namedSession.session.press(params17.key);
|
|
1532
|
+
return void 0;
|
|
1533
|
+
}
|
|
1534
|
+
});
|
|
1535
|
+
|
|
1536
|
+
// src/commands/read-history.ts
|
|
1537
|
+
import { defineCommand as defineCommand8, S as S8 } from "@poe-code/cmdkit";
|
|
1538
|
+
var params8 = S8.Object({
|
|
1539
|
+
session: S8.Optional(S8.String({ short: "s", description: "Session name" })),
|
|
1540
|
+
last: S8.Optional(S8.Number({ short: "n", description: "Return only the last N lines" }))
|
|
1541
|
+
});
|
|
1542
|
+
var readHistory = defineCommand8({
|
|
1543
|
+
name: "read-history",
|
|
1544
|
+
description: "Read terminal output history",
|
|
1545
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1546
|
+
params: params8,
|
|
1547
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1548
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1549
|
+
const lines = await namedSession.session.history({ last: params17.last });
|
|
1550
|
+
return { lines, exitCode: namedSession.session.exitCode };
|
|
1551
|
+
}
|
|
1552
|
+
});
|
|
1553
|
+
|
|
1554
|
+
// src/commands/read-screen.ts
|
|
1555
|
+
import { defineCommand as defineCommand9, S as S9 } from "@poe-code/cmdkit";
|
|
1556
|
+
var params9 = S9.Object({
|
|
1557
|
+
session: S9.Optional(S9.String({ short: "s", description: "Session name" }))
|
|
1558
|
+
});
|
|
1559
|
+
var readScreen = defineCommand9({
|
|
1560
|
+
name: "read-screen",
|
|
1561
|
+
description: "Read the current visible terminal screen",
|
|
1562
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1563
|
+
params: params9,
|
|
1564
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1565
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1566
|
+
const screen = await namedSession.session.screen();
|
|
1567
|
+
return {
|
|
1568
|
+
lines: [...screen.lines],
|
|
1569
|
+
cursor: { ...screen.cursor },
|
|
1570
|
+
size: { ...screen.size },
|
|
1571
|
+
exitCode: namedSession.session.exitCode
|
|
1572
|
+
};
|
|
1573
|
+
}
|
|
1574
|
+
});
|
|
1575
|
+
|
|
1576
|
+
// src/commands/resize.ts
|
|
1577
|
+
import { defineCommand as defineCommand10, S as S10 } from "@poe-code/cmdkit";
|
|
1578
|
+
var params10 = S10.Object({
|
|
1579
|
+
cols: S10.Number({ description: "Terminal width in columns" }),
|
|
1580
|
+
rows: S10.Number({ description: "Terminal height in rows" }),
|
|
1581
|
+
session: S10.Optional(S10.String({ short: "s", description: "Session name" }))
|
|
1582
|
+
});
|
|
1583
|
+
var resize = defineCommand10({
|
|
1584
|
+
name: "resize",
|
|
1585
|
+
description: "Resize an active terminal session",
|
|
1586
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1587
|
+
params: params10,
|
|
1588
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1589
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1590
|
+
await namedSession.session.resize(params17.cols, params17.rows);
|
|
1591
|
+
return void 0;
|
|
1592
|
+
}
|
|
1593
|
+
});
|
|
1594
|
+
|
|
1595
|
+
// src/commands/screenshot.ts
|
|
1596
|
+
import { defineCommand as defineCommand11, S as S11 } from "@poe-code/cmdkit";
|
|
1597
|
+
import { renderTerminalPng } from "terminal-png";
|
|
1598
|
+
var params11 = S11.Object({
|
|
1599
|
+
session: S11.Optional(S11.String({ short: "s", description: "Session name" })),
|
|
1600
|
+
output: S11.String({ short: "o", description: "Path to the output PNG file" }),
|
|
1601
|
+
window: S11.Optional(S11.Boolean({ description: "Include terminal window chrome", default: true })),
|
|
1602
|
+
padding: S11.Optional(S11.Number({ short: "p", description: "Padding around terminal content" }))
|
|
1603
|
+
});
|
|
1604
|
+
var screenshot = defineCommand11({
|
|
1605
|
+
name: "screenshot",
|
|
1606
|
+
description: "Capture the current terminal screen as a PNG image",
|
|
1607
|
+
scope: ["cli"],
|
|
1608
|
+
params: params11,
|
|
1609
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1610
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1611
|
+
const screen = await namedSession.session.screen();
|
|
1612
|
+
await renderTerminalPng(screen.rawLines.join("\n"), {
|
|
1613
|
+
output: params17.output,
|
|
1614
|
+
window: params17.window,
|
|
1615
|
+
padding: params17.padding
|
|
1616
|
+
});
|
|
1617
|
+
return void 0;
|
|
1618
|
+
}
|
|
1619
|
+
});
|
|
1620
|
+
|
|
1621
|
+
// src/commands/send-signal.ts
|
|
1622
|
+
import { defineCommand as defineCommand12, S as S12 } from "@poe-code/cmdkit";
|
|
1623
|
+
var params12 = S12.Object({
|
|
1624
|
+
signal: S12.String({ description: "Signal to send to the session process" }),
|
|
1625
|
+
session: S12.Optional(S12.String({ short: "s", description: "Session name" }))
|
|
1626
|
+
});
|
|
1627
|
+
var sendSignal = defineCommand12({
|
|
1628
|
+
name: "send-signal",
|
|
1629
|
+
description: "Send a process signal to an active terminal session",
|
|
1630
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1631
|
+
positional: ["signal"],
|
|
1632
|
+
params: params12,
|
|
1633
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1634
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1635
|
+
await namedSession.session.signal(params17.signal);
|
|
1636
|
+
return void 0;
|
|
1637
|
+
}
|
|
1638
|
+
});
|
|
1639
|
+
|
|
1640
|
+
// src/commands/type.ts
|
|
1641
|
+
import { defineCommand as defineCommand13, S as S13 } from "@poe-code/cmdkit";
|
|
1642
|
+
var params13 = S13.Object({
|
|
1643
|
+
text: S13.String({ description: "Text to write to the session" }),
|
|
1644
|
+
session: S13.Optional(S13.String({ short: "s", description: "Session name" }))
|
|
1645
|
+
});
|
|
1646
|
+
var type = defineCommand13({
|
|
1647
|
+
name: "type",
|
|
1648
|
+
description: "Write text to an active terminal session character-by-character with delay",
|
|
1649
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1650
|
+
positional: ["text"],
|
|
1651
|
+
params: params13,
|
|
1652
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1653
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1654
|
+
await namedSession.session.type(params17.text);
|
|
1655
|
+
return void 0;
|
|
1656
|
+
}
|
|
1657
|
+
});
|
|
1658
|
+
|
|
1659
|
+
// src/commands/uninstall.ts
|
|
1660
|
+
import { defineCommand as defineCommand14, S as S14 } from "@poe-code/cmdkit";
|
|
1661
|
+
var params14 = S14.Object({
|
|
1662
|
+
agent: S14.Enum(installableAgents, {
|
|
1663
|
+
description: "Agent to uninstall terminal-pilot from",
|
|
1664
|
+
default: DEFAULT_INSTALL_AGENT
|
|
1665
|
+
})
|
|
1666
|
+
});
|
|
1667
|
+
var uninstall = defineCommand14({
|
|
1668
|
+
name: "uninstall",
|
|
1669
|
+
description: "Remove the terminal-pilot CLI skill.",
|
|
1670
|
+
scope: ["cli"],
|
|
1671
|
+
positional: ["agent"],
|
|
1672
|
+
params: params14,
|
|
1673
|
+
handler: async ({ params: params17, terminalPilotInstaller }) => {
|
|
1674
|
+
const services = resolveInstallerServices(terminalPilotInstaller);
|
|
1675
|
+
const agent = resolveInstallableAgent(params17.agent);
|
|
1676
|
+
const localSkill = getSkillFolderWithHome(
|
|
1677
|
+
agent,
|
|
1678
|
+
"local",
|
|
1679
|
+
services.cwd,
|
|
1680
|
+
services.homeDir
|
|
1681
|
+
);
|
|
1682
|
+
const globalSkill = getSkillFolderWithHome(
|
|
1683
|
+
agent,
|
|
1684
|
+
"global",
|
|
1685
|
+
services.cwd,
|
|
1686
|
+
services.homeDir
|
|
1687
|
+
);
|
|
1688
|
+
const removedSkillPaths = [];
|
|
1689
|
+
if (await removeSkillFolder(services.fs, localSkill.fullPath)) {
|
|
1690
|
+
removedSkillPaths.push(localSkill.displayPath);
|
|
1691
|
+
}
|
|
1692
|
+
if (await removeSkillFolder(services.fs, globalSkill.fullPath)) {
|
|
1693
|
+
removedSkillPaths.push(globalSkill.displayPath);
|
|
1694
|
+
}
|
|
1695
|
+
return {
|
|
1696
|
+
agent,
|
|
1697
|
+
removedSkillPaths
|
|
1698
|
+
};
|
|
1699
|
+
}
|
|
1700
|
+
});
|
|
1701
|
+
|
|
1702
|
+
// src/commands/wait-for.ts
|
|
1703
|
+
import { defineCommand as defineCommand15, S as S15 } from "@poe-code/cmdkit";
|
|
1704
|
+
var params15 = S15.Object({
|
|
1705
|
+
pattern: S15.String({ description: "Regular expression pattern to wait for" }),
|
|
1706
|
+
session: S15.Optional(S15.String({ short: "s", description: "Session name" })),
|
|
1707
|
+
timeout: S15.Optional(S15.Number({ short: "t", description: "Maximum wait time in milliseconds" })),
|
|
1708
|
+
literal: S15.Optional(
|
|
1709
|
+
S15.Boolean({
|
|
1710
|
+
short: "l",
|
|
1711
|
+
description: "When true, treat pattern as a literal string instead of a regex"
|
|
1712
|
+
})
|
|
1713
|
+
)
|
|
1714
|
+
});
|
|
1715
|
+
var waitFor = defineCommand15({
|
|
1716
|
+
name: "wait-for",
|
|
1717
|
+
description: "Wait for terminal output to match a pattern",
|
|
1718
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1719
|
+
positional: ["pattern"],
|
|
1720
|
+
params: params15,
|
|
1721
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1722
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1723
|
+
const pattern = params17.literal === true ? params17.pattern : new RegExp(params17.pattern);
|
|
1724
|
+
const line = params17.timeout === void 0 ? await namedSession.session.waitFor(pattern) : await namedSession.session.waitFor(pattern, { timeout: params17.timeout });
|
|
1725
|
+
return { matched: true, line };
|
|
1726
|
+
}
|
|
1727
|
+
});
|
|
1728
|
+
|
|
1729
|
+
// src/commands/wait-for-exit.ts
|
|
1730
|
+
import { defineCommand as defineCommand16, S as S16 } from "@poe-code/cmdkit";
|
|
1731
|
+
var params16 = S16.Object({
|
|
1732
|
+
session: S16.Optional(S16.String({ short: "s", description: "Session name" })),
|
|
1733
|
+
timeout: S16.Optional(S16.Number({ short: "t", description: "Maximum wait time in milliseconds" }))
|
|
1734
|
+
});
|
|
1735
|
+
var waitForExit2 = defineCommand16({
|
|
1736
|
+
name: "wait-for-exit",
|
|
1737
|
+
description: "Wait for a terminal session process to finish",
|
|
1738
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1739
|
+
params: params16,
|
|
1740
|
+
handler: async ({ params: params17, env, terminalPilotRuntime }) => {
|
|
1741
|
+
const namedSession = await getTerminalPilotRuntime(terminalPilotRuntime).resolveSession(params17.session, env);
|
|
1742
|
+
const exitCode = await namedSession.session.waitForExit(
|
|
1743
|
+
params17.timeout === void 0 ? void 0 : { timeout: params17.timeout }
|
|
1744
|
+
);
|
|
1745
|
+
return { exitCode };
|
|
1746
|
+
}
|
|
1747
|
+
});
|
|
1748
|
+
|
|
1749
|
+
// src/commands/index.ts
|
|
1750
|
+
var children = [
|
|
1751
|
+
install,
|
|
1752
|
+
uninstall,
|
|
1753
|
+
createSession,
|
|
1754
|
+
fill,
|
|
1755
|
+
type,
|
|
1756
|
+
pressKey,
|
|
1757
|
+
sendSignal,
|
|
1758
|
+
waitFor,
|
|
1759
|
+
waitForExit2,
|
|
1760
|
+
readScreen,
|
|
1761
|
+
screenshot,
|
|
1762
|
+
readHistory,
|
|
1763
|
+
resize,
|
|
1764
|
+
closeSession,
|
|
1765
|
+
getSession,
|
|
1766
|
+
listSessions
|
|
1767
|
+
];
|
|
1768
|
+
var terminalPilotGroup = defineGroup({
|
|
1769
|
+
name: "terminal-pilot",
|
|
1770
|
+
scope: ["cli", "mcp", "sdk"],
|
|
1771
|
+
children
|
|
1772
|
+
});
|
|
1773
|
+
|
|
1774
|
+
// src/cli.ts
|
|
1775
|
+
function normalizeArgv(argv) {
|
|
1776
|
+
if (argv.includes("--json") && !argv.some((argument) => argument === "--output" || argument.startsWith("--output="))) {
|
|
1777
|
+
return argv.flatMap((argument) => argument === "--json" ? ["--output", "json"] : [argument]);
|
|
1778
|
+
}
|
|
1779
|
+
return argv;
|
|
1780
|
+
}
|
|
1781
|
+
async function main(argv = process.argv) {
|
|
1782
|
+
const originalArgv = process.argv;
|
|
1783
|
+
process.argv = normalizeArgv(argv);
|
|
1784
|
+
try {
|
|
1785
|
+
await runCLI(terminalPilotGroup);
|
|
1786
|
+
} finally {
|
|
1787
|
+
process.argv = originalArgv;
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
async function isDirectExecution(argv) {
|
|
1791
|
+
const entryPoint = argv[1];
|
|
1792
|
+
if (typeof entryPoint !== "string" || entryPoint.length === 0) {
|
|
1793
|
+
return false;
|
|
1794
|
+
}
|
|
1795
|
+
try {
|
|
1796
|
+
const modulePath = fileURLToPath(import.meta.url);
|
|
1797
|
+
const [resolvedEntryPoint, resolvedModulePath] = await Promise.all([
|
|
1798
|
+
realpath(path2.resolve(entryPoint)),
|
|
1799
|
+
realpath(modulePath)
|
|
1800
|
+
]);
|
|
1801
|
+
return resolvedEntryPoint === resolvedModulePath;
|
|
1802
|
+
} catch {
|
|
1803
|
+
return false;
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
if (await isDirectExecution(process.argv)) {
|
|
1807
|
+
await main();
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
// src/testing/cli-repl.ts
|
|
1811
|
+
function appendJsonOutputFlag(args) {
|
|
1812
|
+
if (args.includes("--json") || args.includes("--output") || args.some((arg) => arg.startsWith("--output="))) {
|
|
1813
|
+
return [...args];
|
|
1814
|
+
}
|
|
1815
|
+
const ddashIndex = args.indexOf("--");
|
|
1816
|
+
if (ddashIndex === -1) {
|
|
1817
|
+
return [...args, "--output", "json"];
|
|
1818
|
+
}
|
|
1819
|
+
return [...args.slice(0, ddashIndex), "--output", "json", "--", ...args.slice(ddashIndex + 1)];
|
|
1820
|
+
}
|
|
1821
|
+
function toText(chunk) {
|
|
1822
|
+
if (typeof chunk === "string") {
|
|
1823
|
+
return chunk;
|
|
1824
|
+
}
|
|
1825
|
+
if (chunk instanceof Uint8Array) {
|
|
1826
|
+
return Buffer.from(chunk).toString("utf8");
|
|
1827
|
+
}
|
|
1828
|
+
return String(chunk);
|
|
1829
|
+
}
|
|
1830
|
+
function normalizeOutput(text) {
|
|
1831
|
+
return stripAnsi(text).replaceAll("\r", "").trim();
|
|
1832
|
+
}
|
|
1833
|
+
function canParseJson(text) {
|
|
1834
|
+
if (text.length === 0) {
|
|
1835
|
+
return false;
|
|
1836
|
+
}
|
|
1837
|
+
try {
|
|
1838
|
+
JSON.parse(text);
|
|
1839
|
+
return true;
|
|
1840
|
+
} catch {
|
|
1841
|
+
return false;
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
function createTerminalPilotCliRepl() {
|
|
1845
|
+
return {
|
|
1846
|
+
async run(args) {
|
|
1847
|
+
const stdoutChunks = [];
|
|
1848
|
+
const stderrChunks = [];
|
|
1849
|
+
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
|
|
1850
|
+
const originalStderrWrite = process.stderr.write.bind(process.stderr);
|
|
1851
|
+
const previousExitCode = process.exitCode;
|
|
1852
|
+
process.exitCode = 0;
|
|
1853
|
+
process.stdout.write = ((chunk, ...rest) => {
|
|
1854
|
+
stdoutChunks.push(toText(chunk));
|
|
1855
|
+
const callback = typeof rest.at(-1) === "function" ? rest.at(-1) : void 0;
|
|
1856
|
+
callback?.();
|
|
1857
|
+
return true;
|
|
1858
|
+
});
|
|
1859
|
+
process.stderr.write = ((chunk, ...rest) => {
|
|
1860
|
+
stderrChunks.push(toText(chunk));
|
|
1861
|
+
const callback = typeof rest.at(-1) === "function" ? rest.at(-1) : void 0;
|
|
1862
|
+
callback?.();
|
|
1863
|
+
return true;
|
|
1864
|
+
});
|
|
1865
|
+
try {
|
|
1866
|
+
await main(["node", "terminal-pilot", ...args]);
|
|
1867
|
+
} finally {
|
|
1868
|
+
process.stdout.write = originalStdoutWrite;
|
|
1869
|
+
process.stderr.write = originalStderrWrite;
|
|
1870
|
+
}
|
|
1871
|
+
const exitCode = process.exitCode ?? 0;
|
|
1872
|
+
process.exitCode = previousExitCode;
|
|
1873
|
+
let stdout = normalizeOutput(stdoutChunks.join(""));
|
|
1874
|
+
let stderr = normalizeOutput(stderrChunks.join(""));
|
|
1875
|
+
if (exitCode !== 0 && stderr.length === 0 && !canParseJson(stdout)) {
|
|
1876
|
+
stderr = stdout;
|
|
1877
|
+
stdout = "";
|
|
1878
|
+
}
|
|
1879
|
+
return {
|
|
1880
|
+
exitCode,
|
|
1881
|
+
stdout,
|
|
1882
|
+
stderr
|
|
1883
|
+
};
|
|
1884
|
+
},
|
|
1885
|
+
async runJson(args) {
|
|
1886
|
+
const result = await this.run(appendJsonOutputFlag(args));
|
|
1887
|
+
if (result.exitCode !== 0) {
|
|
1888
|
+
return {
|
|
1889
|
+
exitCode: result.exitCode,
|
|
1890
|
+
stdout: void 0,
|
|
1891
|
+
stderr: result.stderr
|
|
1892
|
+
};
|
|
1893
|
+
}
|
|
1894
|
+
if (result.stdout.length === 0) {
|
|
1895
|
+
throw new Error(`Expected JSON output for terminal-pilot ${args.join(" ")}, but stdout was empty.`);
|
|
1896
|
+
}
|
|
1897
|
+
return {
|
|
1898
|
+
exitCode: result.exitCode,
|
|
1899
|
+
stdout: JSON.parse(result.stdout),
|
|
1900
|
+
stderr: result.stderr
|
|
1901
|
+
};
|
|
1902
|
+
},
|
|
1903
|
+
async close() {
|
|
1904
|
+
await closeSharedTerminalPilotRuntime();
|
|
1905
|
+
}
|
|
1906
|
+
};
|
|
1907
|
+
}
|
|
1908
|
+
export {
|
|
1909
|
+
createTerminalPilotCliRepl
|
|
1910
|
+
};
|
|
1911
|
+
//# sourceMappingURL=cli-repl.js.map
|