@anusornneal/chat-relay 0.7.2 → 0.8.1
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 +353 -320
- package/agent/capability-scheduler.mjs +121 -0
- package/agent/connection-state.mjs +141 -0
- package/agent/desktop-manager.mjs +220 -115
- package/agent/file-manager.mjs +527 -382
- package/agent/lifecycle.mjs +101 -0
- package/agent/local-agent.mjs +494 -242
- package/agent/macos-desktop-helper.js +243 -0
- package/agent/macos-desktop-runner.mjs +292 -0
- package/agent/process-manager.mjs +110 -26
- package/agent/protocol.mjs +67 -0
- package/agent/terminal-manager.mjs +587 -550
- package/agent/windows-desktop-runner.mjs +109 -0
- package/agent/windows-desktop-worker.ps1 +215 -13
- package/cli/config.mjs +114 -114
- package/cli/device-login.mjs +170 -170
- package/cli/index.mjs +241 -201
- package/cli/remote.mjs +139 -94
- package/package.json +76 -75
- package/agent/windows-desktop.ps1 +0 -288
|
@@ -1,550 +1,587 @@
|
|
|
1
|
-
import { execFile, spawn } from "node:child_process";
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
3
|
-
import os from "node:os";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
/\b(
|
|
23
|
-
/\b(
|
|
24
|
-
/\b(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
this.#cleanup();
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
this
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
this.
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
batch.
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
#
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
.
|
|
545
|
-
.
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
const MAX_EXEC_BUFFER = 48 * 1024;
|
|
7
|
+
const MAX_SESSION_BUFFER = 256 * 1024;
|
|
8
|
+
const MAX_READ_CHARS = 24 * 1024;
|
|
9
|
+
const MAX_INPUT_CHARS = 8 * 1024;
|
|
10
|
+
const MAX_SESSIONS = 8;
|
|
11
|
+
const MAX_BATCH_SIZE = 20;
|
|
12
|
+
const MAX_BATCH_CONCURRENCY = 8;
|
|
13
|
+
const DEFAULT_BATCH_CONCURRENCY = 4;
|
|
14
|
+
const DEFAULT_MAX_QUEUED_JOBS = 64;
|
|
15
|
+
const MAX_QUEUED_JOBS = 256;
|
|
16
|
+
const MAX_BATCH_READ_CHARS = 24 * 1024;
|
|
17
|
+
const MAX_JOB_READ_CHARS = 8 * 1024;
|
|
18
|
+
const MAX_RETAINED_BATCHES = 64;
|
|
19
|
+
const ENDED_TTL_MS = 30 * 60 * 1000;
|
|
20
|
+
|
|
21
|
+
const BLOCKED_COMMANDS = [
|
|
22
|
+
/\b(mkfs|format|mount|umount|fdisk|dd|parted|diskpart|sudo|su|passwd)\b/i,
|
|
23
|
+
/\b(adduser|useradd|usermod|groupadd|chsh|visudo|shutdown|reboot|halt)\b/i,
|
|
24
|
+
/\b(poweroff|init|iptables|firewall|netsh|sfc|bcdedit|runas|cipher|takeown)\b/i,
|
|
25
|
+
/\b(reg|sc)\.exe\b/i,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
function validateCommand(command) {
|
|
29
|
+
if (typeof command !== "string" || command.length === 0 || command.length > 4000) {
|
|
30
|
+
throw new Error("invalid_command");
|
|
31
|
+
}
|
|
32
|
+
if (BLOCKED_COMMANDS.some((pattern) => pattern.test(command))) {
|
|
33
|
+
throw new Error("command_blocked");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function normalizeTimeout(timeoutMs, fallback = 15000, max = 20000) {
|
|
38
|
+
const value = Number(timeoutMs);
|
|
39
|
+
if (!Number.isFinite(value)) return fallback;
|
|
40
|
+
return Math.min(Math.max(Math.trunc(value), 1000), max);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function normalizeInteger(value, fallback, min, max) {
|
|
44
|
+
const number = Number(value);
|
|
45
|
+
if (!Number.isFinite(number)) return fallback;
|
|
46
|
+
return Math.min(Math.max(Math.trunc(number), min), max);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function safeCwd(cwd) {
|
|
50
|
+
if (cwd === undefined || cwd === null || cwd === "") return process.cwd();
|
|
51
|
+
if (typeof cwd !== "string" || cwd.length > 1024) throw new Error("invalid_cwd");
|
|
52
|
+
return cwd;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function shellForPlatform(platform, preferredShell) {
|
|
56
|
+
if (platform === "win32") {
|
|
57
|
+
return {
|
|
58
|
+
file: "powershell.exe",
|
|
59
|
+
displayName: "powershell",
|
|
60
|
+
commandArgs: (command) => ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", command],
|
|
61
|
+
interactiveArgs: ["-NoLogo", "-NoProfile", "-NoExit", "-Command", "-"],
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const file = preferredShell || (platform === "darwin" ? "/bin/zsh" : "/bin/sh");
|
|
65
|
+
return {
|
|
66
|
+
file,
|
|
67
|
+
displayName: path.basename(file),
|
|
68
|
+
commandArgs: (command) => ["-lc", command],
|
|
69
|
+
interactiveArgs: ["-l"],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function terminateProcessTree(platform, pid) {
|
|
74
|
+
if (platform === "win32") {
|
|
75
|
+
return new Promise((resolve) => {
|
|
76
|
+
execFile("taskkill.exe", ["/PID", String(pid), "/T", "/F"], () => resolve());
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return new Promise((resolve) => {
|
|
80
|
+
try { process.kill(-pid, "SIGTERM"); } catch {
|
|
81
|
+
try { process.kill(pid, "SIGTERM"); } catch {}
|
|
82
|
+
}
|
|
83
|
+
setTimeout(() => {
|
|
84
|
+
try { process.kill(-pid, "SIGKILL"); } catch {
|
|
85
|
+
try { process.kill(pid, "SIGKILL"); } catch {}
|
|
86
|
+
}
|
|
87
|
+
resolve();
|
|
88
|
+
}, 150);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function clipText(value, limit) {
|
|
92
|
+
const text = typeof value === "string" ? value : "";
|
|
93
|
+
if (text.length <= limit) return { text, truncated: false };
|
|
94
|
+
return { text: text.slice(0, limit), truncated: true };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function clipJobOutput(stdout, stderr, limit = MAX_JOB_READ_CHARS) {
|
|
98
|
+
const rawStdout = typeof stdout === "string" ? stdout : "";
|
|
99
|
+
const rawStderr = typeof stderr === "string" ? stderr : "";
|
|
100
|
+
const stdoutBudget = rawStderr ? Math.floor(limit / 2) : limit;
|
|
101
|
+
const clippedStdout = clipText(rawStdout, stdoutBudget);
|
|
102
|
+
const stderrBudget = Math.max(0, limit - clippedStdout.text.length);
|
|
103
|
+
const clippedStderr = clipText(rawStderr, stderrBudget);
|
|
104
|
+
return {
|
|
105
|
+
stdout: clippedStdout.text,
|
|
106
|
+
stderr: clippedStderr.text,
|
|
107
|
+
truncated: clippedStdout.truncated || clippedStderr.truncated,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export class TerminalManager {
|
|
112
|
+
constructor(options = {}) {
|
|
113
|
+
this.platform = options.platform ?? process.platform;
|
|
114
|
+
this.shell = shellForPlatform(this.platform, options.shell);
|
|
115
|
+
this.sessions = new Map();
|
|
116
|
+
this.batches = new Map();
|
|
117
|
+
this.batchQueue = [];
|
|
118
|
+
this.activeBatchJobs = 0;
|
|
119
|
+
this.batchConcurrency = normalizeInteger(
|
|
120
|
+
options.batchConcurrency,
|
|
121
|
+
DEFAULT_BATCH_CONCURRENCY,
|
|
122
|
+
1,
|
|
123
|
+
MAX_BATCH_CONCURRENCY,
|
|
124
|
+
);
|
|
125
|
+
this.maxQueuedJobs = normalizeInteger(
|
|
126
|
+
options.maxQueuedJobs,
|
|
127
|
+
DEFAULT_MAX_QUEUED_JOBS,
|
|
128
|
+
1,
|
|
129
|
+
MAX_QUEUED_JOBS,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async exec(command, cwd, timeoutMs) {
|
|
134
|
+
validateCommand(command);
|
|
135
|
+
const resolvedCwd = safeCwd(cwd);
|
|
136
|
+
const timeout = normalizeTimeout(timeoutMs);
|
|
137
|
+
|
|
138
|
+
return new Promise((resolve) => {
|
|
139
|
+
execFile(
|
|
140
|
+
this.shell.file,
|
|
141
|
+
this.shell.commandArgs(command),
|
|
142
|
+
{
|
|
143
|
+
cwd: resolvedCwd,
|
|
144
|
+
timeout,
|
|
145
|
+
windowsHide: true,
|
|
146
|
+
maxBuffer: MAX_EXEC_BUFFER,
|
|
147
|
+
},
|
|
148
|
+
(error, stdout, stderr) => {
|
|
149
|
+
resolve({
|
|
150
|
+
ok: !error,
|
|
151
|
+
exitCode: typeof error?.code === "number" ? error.code : (error ? 1 : 0),
|
|
152
|
+
stdout: stdout ?? "",
|
|
153
|
+
stderr: stderr ?? "",
|
|
154
|
+
error: error ? error.message : null,
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
start(command, cwd, observability) {
|
|
162
|
+
validateCommand(command);
|
|
163
|
+
return this.#spawnSession({
|
|
164
|
+
type: "command",
|
|
165
|
+
command,
|
|
166
|
+
cwd: safeCwd(cwd),
|
|
167
|
+
args: this.shell.commandArgs(command),
|
|
168
|
+
observability,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
startShell(cwd, observability) {
|
|
173
|
+
return this.#spawnSession({
|
|
174
|
+
type: "shell",
|
|
175
|
+
command: this.shell.displayName,
|
|
176
|
+
cwd: safeCwd(cwd),
|
|
177
|
+
args: this.shell.interactiveArgs,
|
|
178
|
+
observability,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
list() {
|
|
183
|
+
this.#cleanup();
|
|
184
|
+
return [...this.sessions.values()].map((session) => this.#summary(session));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
observability() {
|
|
188
|
+
this.#cleanup();
|
|
189
|
+
return {
|
|
190
|
+
ok: true,
|
|
191
|
+
sessions: [...this.sessions.values()].map((session) => ({
|
|
192
|
+
sessionId: session.id,
|
|
193
|
+
type: session.type,
|
|
194
|
+
status: session.status,
|
|
195
|
+
exitCode: session.exitCode,
|
|
196
|
+
startedAt: session.startedAt,
|
|
197
|
+
endedAt: session.endedAt,
|
|
198
|
+
...(session.observability?.userId ? { userId: session.observability.userId } : {}),
|
|
199
|
+
...(session.observability?.activityId ? { activityId: session.observability.activityId } : {}),
|
|
200
|
+
...(session.observability?.toolCallId ? { toolCallId: session.observability.toolCallId } : {}),
|
|
201
|
+
})),
|
|
202
|
+
batches: [...this.batches.values()].map((batch) => ({
|
|
203
|
+
batchId: batch.batchId,
|
|
204
|
+
createdAt: batch.createdAt,
|
|
205
|
+
endedAt: batch.endedAt,
|
|
206
|
+
cancelled: batch.cancelled,
|
|
207
|
+
...(batch.observability?.userId ? { userId: batch.observability.userId } : {}),
|
|
208
|
+
...(batch.observability?.activityId ? { activityId: batch.observability.activityId } : {}),
|
|
209
|
+
...(batch.observability?.toolCallId ? { toolCallId: batch.observability.toolCallId } : {}),
|
|
210
|
+
counts: this.#batchSummary(batch).counts,
|
|
211
|
+
jobs: batch.jobs.map((job) => ({
|
|
212
|
+
jobId: job.jobId,
|
|
213
|
+
status: job.status,
|
|
214
|
+
queuedAt: job.queuedAt,
|
|
215
|
+
startedAt: job.startedAt,
|
|
216
|
+
endedAt: job.endedAt,
|
|
217
|
+
durationMs: job.durationMs,
|
|
218
|
+
exitCode: job.exitCode,
|
|
219
|
+
})),
|
|
220
|
+
})),
|
|
221
|
+
activeExecJobs: this.activeBatchJobs,
|
|
222
|
+
queuedJobs: this.batchQueue.length,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
read(sessionId, afterSeq = 0, maxChars = MAX_READ_CHARS) {
|
|
227
|
+
this.#cleanup();
|
|
228
|
+
const session = this.#get(sessionId);
|
|
229
|
+
const limit = Math.min(Math.max(Number(maxChars) || MAX_READ_CHARS, 1024), MAX_READ_CHARS);
|
|
230
|
+
const oldestSeq = session.chunks[0]?.seq ?? session.nextSeq;
|
|
231
|
+
const chunks = [];
|
|
232
|
+
let chars = 0;
|
|
233
|
+
|
|
234
|
+
for (const chunk of session.chunks) {
|
|
235
|
+
if (chunk.seq <= afterSeq) continue;
|
|
236
|
+
if (chunks.length > 0 && chars + chunk.text.length > limit) break;
|
|
237
|
+
chunks.push(chunk);
|
|
238
|
+
chars += chunk.text.length;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
...this.#summary(session),
|
|
243
|
+
chunks,
|
|
244
|
+
oldestSeq,
|
|
245
|
+
nextSeq: chunks.at(-1)?.seq ?? afterSeq,
|
|
246
|
+
truncated: afterSeq < oldestSeq - 1,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
write(sessionId, input, appendNewline = true) {
|
|
251
|
+
const session = this.#get(sessionId);
|
|
252
|
+
if (session.status !== "running") throw new Error("session_not_running");
|
|
253
|
+
if (typeof input !== "string" || input.length === 0 || input.length > MAX_INPUT_CHARS) {
|
|
254
|
+
throw new Error("invalid_input");
|
|
255
|
+
}
|
|
256
|
+
session.process.stdin.write(input + (appendNewline ? os.EOL : ""));
|
|
257
|
+
return this.#summary(session);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async kill(sessionId) {
|
|
261
|
+
const session = this.#get(sessionId);
|
|
262
|
+
if (session.status !== "running") return this.#summary(session);
|
|
263
|
+
|
|
264
|
+
await terminateProcessTree(this.platform, session.pid);
|
|
265
|
+
return this.#summary(session);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
batchStart(jobs, options = {}) {
|
|
269
|
+
this.#cleanup();
|
|
270
|
+
if (!Array.isArray(jobs) || jobs.length < 2 || jobs.length > MAX_BATCH_SIZE) {
|
|
271
|
+
throw new Error("invalid_batch_size");
|
|
272
|
+
}
|
|
273
|
+
const defaultCwd = safeCwd(options.cwd);
|
|
274
|
+
const defaultTimeoutMs = normalizeTimeout(options.timeoutMs);
|
|
275
|
+
const requestedConcurrency = normalizeInteger(
|
|
276
|
+
options.concurrency,
|
|
277
|
+
this.batchConcurrency,
|
|
278
|
+
1,
|
|
279
|
+
this.batchConcurrency,
|
|
280
|
+
);
|
|
281
|
+
const availableSlots = Math.max(0, this.batchConcurrency - this.activeBatchJobs);
|
|
282
|
+
const immediatelyRunnable = this.batchQueue.length === 0
|
|
283
|
+
? Math.min(jobs.length, availableSlots, requestedConcurrency)
|
|
284
|
+
: 0;
|
|
285
|
+
const projectedQueuedJobs = this.batchQueue.length + jobs.length - immediatelyRunnable;
|
|
286
|
+
if (projectedQueuedJobs > this.maxQueuedJobs) {
|
|
287
|
+
throw new Error("queue_full");
|
|
288
|
+
}
|
|
289
|
+
const batchId = randomUUID();
|
|
290
|
+
const createdAt = new Date().toISOString();
|
|
291
|
+
const batch = {
|
|
292
|
+
batchId,
|
|
293
|
+
createdAt,
|
|
294
|
+
endedAt: null,
|
|
295
|
+
requestedConcurrency,
|
|
296
|
+
cancelled: false,
|
|
297
|
+
observability: options.observability ?? null,
|
|
298
|
+
jobs: jobs.map((input, index) => {
|
|
299
|
+
const item = typeof input === "string" ? { command: input } : input;
|
|
300
|
+
validateCommand(item?.command);
|
|
301
|
+
return {
|
|
302
|
+
jobId: randomUUID(),
|
|
303
|
+
index,
|
|
304
|
+
command: item.command,
|
|
305
|
+
cwd: safeCwd(item.cwd ?? defaultCwd),
|
|
306
|
+
timeoutMs: normalizeTimeout(item.timeoutMs, defaultTimeoutMs),
|
|
307
|
+
status: "queued",
|
|
308
|
+
queuedAt: createdAt,
|
|
309
|
+
startedAt: null,
|
|
310
|
+
endedAt: null,
|
|
311
|
+
queueWaitMs: null,
|
|
312
|
+
durationMs: null,
|
|
313
|
+
exitCode: null,
|
|
314
|
+
stdout: "",
|
|
315
|
+
stderr: "",
|
|
316
|
+
outputTruncated: false,
|
|
317
|
+
error: null,
|
|
318
|
+
process: null,
|
|
319
|
+
};
|
|
320
|
+
}),
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
this.batches.set(batchId, batch);
|
|
324
|
+
for (const job of batch.jobs) this.batchQueue.push({ batchId, jobId: job.jobId });
|
|
325
|
+
this.#drainBatchQueue();
|
|
326
|
+
|
|
327
|
+
return this.#batchSummary(batch);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
batchStatus(batchId) {
|
|
331
|
+
this.#cleanup();
|
|
332
|
+
const batch = this.#getBatch(batchId);
|
|
333
|
+
return this.#batchSummary(batch);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
batchRead(batchId, maxChars = MAX_BATCH_READ_CHARS) {
|
|
337
|
+
this.#cleanup();
|
|
338
|
+
const batch = this.#getBatch(batchId);
|
|
339
|
+
const aggregateLimit = normalizeInteger(maxChars, MAX_BATCH_READ_CHARS, 1024, MAX_BATCH_READ_CHARS);
|
|
340
|
+
let remaining = aggregateLimit;
|
|
341
|
+
let truncated = false;
|
|
342
|
+
const jobs = batch.jobs.map((job) => {
|
|
343
|
+
const perJobLimit = Math.min(MAX_JOB_READ_CHARS, remaining);
|
|
344
|
+
const output = clipJobOutput(job.stdout, job.stderr, perJobLimit);
|
|
345
|
+
remaining = Math.max(0, remaining - output.stdout.length - output.stderr.length);
|
|
346
|
+
truncated ||= job.outputTruncated || output.truncated ||
|
|
347
|
+
((job.stdout.length + job.stderr.length) > (output.stdout.length + output.stderr.length));
|
|
348
|
+
return {
|
|
349
|
+
...this.#jobSummary(job),
|
|
350
|
+
stdout: output.stdout,
|
|
351
|
+
stderr: output.stderr,
|
|
352
|
+
outputTruncated: job.outputTruncated || output.truncated,
|
|
353
|
+
};
|
|
354
|
+
});
|
|
355
|
+
return { ...this.#batchSummary(batch), jobs, truncated, maxChars: aggregateLimit };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async batchCancel(batchId) {
|
|
359
|
+
this.#cleanup();
|
|
360
|
+
const batch = this.#getBatch(batchId);
|
|
361
|
+
batch.cancelled = true;
|
|
362
|
+
const cancelledAt = new Date().toISOString();
|
|
363
|
+
this.batchQueue = this.batchQueue.filter((queued) => {
|
|
364
|
+
if (queued.batchId !== batchId) return true;
|
|
365
|
+
const job = batch.jobs.find((item) => item.jobId === queued.jobId);
|
|
366
|
+
if (job && job.status === "queued") {
|
|
367
|
+
job.status = "cancelled";
|
|
368
|
+
job.endedAt = cancelledAt;
|
|
369
|
+
}
|
|
370
|
+
return false;
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
const running = batch.jobs.filter((job) => job.status === "running" && job.process?.pid);
|
|
374
|
+
await Promise.all(running.map((job) => terminateProcessTree(this.platform, job.process.pid)));
|
|
375
|
+
|
|
376
|
+
this.#finishBatchIfDone(batch);
|
|
377
|
+
this.#drainBatchQueue();
|
|
378
|
+
return this.#batchSummary(batch);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
getBatchConfig() {
|
|
382
|
+
return {
|
|
383
|
+
maxBatchSize: MAX_BATCH_SIZE,
|
|
384
|
+
batchConcurrency: this.batchConcurrency,
|
|
385
|
+
maxQueuedJobs: this.maxQueuedJobs,
|
|
386
|
+
activeBatchJobs: this.activeBatchJobs,
|
|
387
|
+
queuedBatchJobs: this.batchQueue.length,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
#spawnSession({ type, command, cwd, args, observability }) {
|
|
392
|
+
this.#cleanup();
|
|
393
|
+
const running = [...this.sessions.values()].filter((session) => session.status === "running").length;
|
|
394
|
+
if (running >= MAX_SESSIONS) throw new Error("too_many_sessions");
|
|
395
|
+
|
|
396
|
+
const child = spawn(this.shell.file, args, {
|
|
397
|
+
cwd,
|
|
398
|
+
windowsHide: true,
|
|
399
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
400
|
+
detached: this.platform !== "win32",
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
const session = {
|
|
404
|
+
id: randomUUID(),
|
|
405
|
+
type,
|
|
406
|
+
command,
|
|
407
|
+
cwd,
|
|
408
|
+
pid: child.pid,
|
|
409
|
+
process: child,
|
|
410
|
+
status: "running",
|
|
411
|
+
exitCode: null,
|
|
412
|
+
signal: null,
|
|
413
|
+
startedAt: new Date().toISOString(),
|
|
414
|
+
endedAt: null,
|
|
415
|
+
chunks: [],
|
|
416
|
+
nextSeq: 1,
|
|
417
|
+
totalChars: 0,
|
|
418
|
+
observability: observability ?? null,
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
this.sessions.set(session.id, session);
|
|
422
|
+
child.stdout.on("data", (data) => this.#append(session, "stdout", data));
|
|
423
|
+
child.stderr.on("data", (data) => this.#append(session, "stderr", data));
|
|
424
|
+
child.on("error", (error) => this.#append(session, "stderr", error.message + os.EOL));
|
|
425
|
+
child.on("exit", (code, signal) => {
|
|
426
|
+
session.status = "exited";
|
|
427
|
+
session.exitCode = code;
|
|
428
|
+
session.signal = signal;
|
|
429
|
+
session.endedAt = new Date().toISOString();
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
return this.#summary(session);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
#drainBatchQueue() {
|
|
436
|
+
while (this.activeBatchJobs < this.batchConcurrency && this.batchQueue.length > 0) {
|
|
437
|
+
const next = this.batchQueue.findIndex((entry) => {
|
|
438
|
+
const batch = this.batches.get(entry.batchId);
|
|
439
|
+
if (!batch || batch.cancelled) return false;
|
|
440
|
+
const runningForBatch = batch.jobs.filter((job) => job.status === "running").length;
|
|
441
|
+
return runningForBatch < batch.requestedConcurrency;
|
|
442
|
+
});
|
|
443
|
+
if (next < 0) break;
|
|
444
|
+
|
|
445
|
+
const [{ batchId, jobId }] = this.batchQueue.splice(next, 1);
|
|
446
|
+
const batch = this.batches.get(batchId);
|
|
447
|
+
const job = batch?.jobs.find((item) => item.jobId === jobId);
|
|
448
|
+
if (!batch || !job || job.status !== "queued") continue;
|
|
449
|
+
this.#runBatchJob(batch, job);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
#runBatchJob(batch, job) {
|
|
454
|
+
this.activeBatchJobs += 1;
|
|
455
|
+
job.status = "running";
|
|
456
|
+
job.startedAt = new Date().toISOString();
|
|
457
|
+
job.queueWaitMs = Date.now() - Date.parse(job.queuedAt);
|
|
458
|
+
|
|
459
|
+
const started = Date.now();
|
|
460
|
+
const child = execFile(
|
|
461
|
+
this.shell.file,
|
|
462
|
+
this.shell.commandArgs(job.command),
|
|
463
|
+
{
|
|
464
|
+
cwd: job.cwd,
|
|
465
|
+
timeout: job.timeoutMs,
|
|
466
|
+
windowsHide: true,
|
|
467
|
+
maxBuffer: MAX_EXEC_BUFFER,
|
|
468
|
+
detached: this.platform !== "win32",
|
|
469
|
+
},
|
|
470
|
+
(error, stdout, stderr) => {
|
|
471
|
+
job.process = null;
|
|
472
|
+
const clippedOutput = clipJobOutput(stdout, stderr);
|
|
473
|
+
job.stdout = clippedOutput.stdout;
|
|
474
|
+
job.stderr = clippedOutput.stderr;
|
|
475
|
+
job.outputTruncated = clippedOutput.truncated;
|
|
476
|
+
job.exitCode = typeof error?.code === "number" ? error.code : (error ? 1 : 0);
|
|
477
|
+
job.error = error ? error.message : null;
|
|
478
|
+
job.status = batch.cancelled ? "cancelled" : (error ? "failed" : "completed");
|
|
479
|
+
job.endedAt = new Date().toISOString();
|
|
480
|
+
job.durationMs = Date.now() - started;
|
|
481
|
+
this.activeBatchJobs = Math.max(0, this.activeBatchJobs - 1);
|
|
482
|
+
this.#finishBatchIfDone(batch);
|
|
483
|
+
this.#drainBatchQueue();
|
|
484
|
+
},
|
|
485
|
+
);
|
|
486
|
+
job.process = child;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
#finishBatchIfDone(batch) {
|
|
490
|
+
if (batch.jobs.every((job) => ["completed", "failed", "cancelled"].includes(job.status))) {
|
|
491
|
+
batch.endedAt ||= new Date().toISOString();
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
#jobSummary(job) {
|
|
496
|
+
return {
|
|
497
|
+
jobId: job.jobId,
|
|
498
|
+
index: job.index,
|
|
499
|
+
status: job.status,
|
|
500
|
+
queuedAt: job.queuedAt,
|
|
501
|
+
startedAt: job.startedAt,
|
|
502
|
+
endedAt: job.endedAt,
|
|
503
|
+
queueWaitMs: job.queueWaitMs,
|
|
504
|
+
durationMs: job.durationMs,
|
|
505
|
+
exitCode: job.exitCode,
|
|
506
|
+
error: job.error,
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
#batchSummary(batch) {
|
|
511
|
+
const counts = { queued: 0, running: 0, completed: 0, failed: 0, cancelled: 0 };
|
|
512
|
+
for (const job of batch.jobs) counts[job.status] = (counts[job.status] || 0) + 1;
|
|
513
|
+
return {
|
|
514
|
+
ok: true,
|
|
515
|
+
batchId: batch.batchId,
|
|
516
|
+
createdAt: batch.createdAt,
|
|
517
|
+
endedAt: batch.endedAt,
|
|
518
|
+
requestedConcurrency: batch.requestedConcurrency,
|
|
519
|
+
cancelled: batch.cancelled,
|
|
520
|
+
counts,
|
|
521
|
+
jobs: batch.jobs.map((job) => this.#jobSummary(job)),
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
#append(session, stream, data) {
|
|
526
|
+
const text = data.toString();
|
|
527
|
+
for (let offset = 0; offset < text.length; offset += 4096) {
|
|
528
|
+
const part = text.slice(offset, offset + 4096);
|
|
529
|
+
session.chunks.push({ seq: session.nextSeq++, stream, text: part });
|
|
530
|
+
session.totalChars += part.length;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
while (session.totalChars > MAX_SESSION_BUFFER && session.chunks.length > 1) {
|
|
534
|
+
const removed = session.chunks.shift();
|
|
535
|
+
session.totalChars -= removed.text.length;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
#summary(session) {
|
|
540
|
+
return {
|
|
541
|
+
sessionId: session.id,
|
|
542
|
+
type: session.type,
|
|
543
|
+
command: session.command,
|
|
544
|
+
cwd: session.cwd,
|
|
545
|
+
pid: session.pid,
|
|
546
|
+
status: session.status,
|
|
547
|
+
exitCode: session.exitCode,
|
|
548
|
+
signal: session.signal,
|
|
549
|
+
startedAt: session.startedAt,
|
|
550
|
+
endedAt: session.endedAt,
|
|
551
|
+
nextSeq: session.nextSeq - 1,
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
#get(sessionId) {
|
|
556
|
+
if (typeof sessionId !== "string") throw new Error("invalid_session_id");
|
|
557
|
+
const session = this.sessions.get(sessionId);
|
|
558
|
+
if (!session) throw new Error("session_not_found");
|
|
559
|
+
return session;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
#getBatch(batchId) {
|
|
563
|
+
if (typeof batchId !== "string") throw new Error("invalid_batch_id");
|
|
564
|
+
const batch = this.batches.get(batchId);
|
|
565
|
+
if (!batch) throw new Error("batch_not_found");
|
|
566
|
+
return batch;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
#cleanup() {
|
|
570
|
+
const now = Date.now();
|
|
571
|
+
for (const [id, session] of this.sessions) {
|
|
572
|
+
if (session.status === "running" || !session.endedAt) continue;
|
|
573
|
+
if (now - Date.parse(session.endedAt) > ENDED_TTL_MS) this.sessions.delete(id);
|
|
574
|
+
}
|
|
575
|
+
for (const [id, batch] of this.batches) {
|
|
576
|
+
if (!batch.endedAt) continue;
|
|
577
|
+
if (now - Date.parse(batch.endedAt) > ENDED_TTL_MS) this.batches.delete(id);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
const retainedCompleted = [...this.batches.entries()]
|
|
581
|
+
.filter(([, batch]) => Boolean(batch.endedAt))
|
|
582
|
+
.sort((a, b) => Date.parse(b[1].endedAt) - Date.parse(a[1].endedAt));
|
|
583
|
+
for (const [id] of retainedCompleted.slice(MAX_RETAINED_BATCHES)) {
|
|
584
|
+
this.batches.delete(id);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|