@rind-ai/cli 0.4.1 → 0.6.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/bin/rind.js +5 -5
- package/lib/assistant-renderer.js +179 -265
- package/lib/choice-menu-state.js +46 -46
- package/lib/cli-input-actions.js +548 -0
- package/lib/cli-output-controller.js +460 -0
- package/lib/cli-runtime-controller.js +350 -0
- package/lib/cli-state-store.js +32 -0
- package/lib/cli-state.js +41 -0
- package/lib/command-controller.js +159 -126
- package/lib/compact-context-state.js +22 -22
- package/lib/components/assistant-message.js +169 -0
- package/lib/components/composer-area.js +25 -0
- package/lib/components/dynamic-block.js +20 -0
- package/lib/components/monitor-stack.js +35 -0
- package/lib/components/text-block.js +47 -0
- package/lib/components/tool-block.js +122 -0
- package/lib/composer-terminal.js +224 -203
- package/lib/event-controller.js +243 -242
- package/lib/frontend-cli-implementation.js +656 -1111
- package/lib/input-controller.js +75 -94
- package/lib/input-errors.js +3 -3
- package/lib/interrupt-state.js +9 -9
- package/lib/line-editor.js +541 -541
- package/lib/local-slash-commands.js +217 -0
- package/lib/markdown-lines.js +103 -0
- package/lib/model-menu-state.js +50 -50
- package/lib/one-shot-progress.js +145 -0
- package/lib/one-shot.js +228 -0
- package/lib/question-menu-state.js +61 -0
- package/lib/rendering.js +1295 -1037
- package/lib/runtime-client.js +241 -193
- package/lib/runtime-env.js +21 -21
- package/lib/runtime-protocol.js +122 -15
- package/lib/slash-command-mode.js +16 -27
- package/lib/slash-menu-state.js +59 -59
- package/lib/{background-controller.js → task-monitor-controller.js} +411 -289
- package/lib/terminal-key.js +97 -97
- package/lib/text-width.js +335 -151
- package/lib/theme-menu-state.js +31 -0
- package/lib/theme.js +134 -0
- package/lib/tool-display.js +680 -0
- package/lib/tui/component.js +55 -0
- package/lib/tui/cursor.js +29 -0
- package/lib/tui/input-buffer.js +172 -0
- package/lib/tui/tui.js +591 -0
- package/lib/turn-controller.js +68 -78
- package/package.json +28 -28
- package/lib/assistant-stream-buffer.js +0 -25
- package/lib/terminal-ui.js +0 -581
|
@@ -1,1111 +1,656 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { createInterface } from "node:readline";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
6
|
-
import { createRequire } from "node:module";
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
} from "./rendering.js";
|
|
48
|
-
|
|
49
|
-
export async function runFrontendCliApp(cliArgs = process.argv.slice(2)) {
|
|
50
|
-
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
51
|
-
const repoRoot = path.resolve(scriptDir, "..", "..");
|
|
52
|
-
const python = process.env.RIND_PYTHON || "python";
|
|
53
|
-
const runtimePath = process.env.RIND_RUNTIME_PATH || resolveInstalledRuntime();
|
|
54
|
-
|
|
55
|
-
function resolveInstalledRuntime() {
|
|
56
|
-
const packageNames = {
|
|
57
|
-
win32: "@rind-ai/runtime-win32-x64",
|
|
58
|
-
linux: "@rind-ai/runtime-linux-x64",
|
|
59
|
-
darwin: process.arch === "arm64" ? "@rind-ai/runtime-darwin-arm64" : "@rind-ai/runtime-darwin-x64",
|
|
60
|
-
};
|
|
61
|
-
const packageName = packageNames[process.platform];
|
|
62
|
-
if (!packageName) return "";
|
|
63
|
-
try {
|
|
64
|
-
const packageRoot = path.dirname(createRequire(import.meta.url).resolve(`${packageName}/package.json`));
|
|
65
|
-
return path.join(packageRoot, "bin", process.platform === "win32" ? "rind-runtime.exe" : "rind-runtime");
|
|
66
|
-
} catch {
|
|
67
|
-
return "";
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (cliArgs.some((arg) => arg === "--
|
|
72
|
-
process.
|
|
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
|
-
const
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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
|
-
const
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
if (
|
|
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
|
-
const
|
|
471
|
-
if (
|
|
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
|
-
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
});
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
function
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
function ask(prompt, placeholder = "") {
|
|
658
|
-
if (!terminalUi) {
|
|
659
|
-
if (!input) {
|
|
660
|
-
return Promise.reject(new Error("Input is not available"));
|
|
661
|
-
}
|
|
662
|
-
return askLine(promptValue(prompt));
|
|
663
|
-
}
|
|
664
|
-
return askTtyInput(prompt, placeholder);
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
function askLine(prompt) {
|
|
668
|
-
return new Promise((resolve, reject) => {
|
|
669
|
-
const cleanup = () => {
|
|
670
|
-
inputActive = false;
|
|
671
|
-
input.off("close", onClose);
|
|
672
|
-
if (cancelActiveInput === onCancel) {
|
|
673
|
-
cancelActiveInput = null;
|
|
674
|
-
}
|
|
675
|
-
};
|
|
676
|
-
const onClose = () => {
|
|
677
|
-
cleanup();
|
|
678
|
-
reject(new Error("Input closed"));
|
|
679
|
-
};
|
|
680
|
-
const onCancel = () => {
|
|
681
|
-
cleanup();
|
|
682
|
-
resolve("");
|
|
683
|
-
};
|
|
684
|
-
input.once("close", onClose);
|
|
685
|
-
cancelActiveInput = onCancel;
|
|
686
|
-
inputActive = true;
|
|
687
|
-
input.question(prompt, (answer) => {
|
|
688
|
-
cleanup();
|
|
689
|
-
resolve(answer);
|
|
690
|
-
});
|
|
691
|
-
applyInputPrefill();
|
|
692
|
-
});
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
function askTtyInput(prompt, placeholder) {
|
|
696
|
-
return new Promise((resolve) => {
|
|
697
|
-
clearAssistantLineForInput();
|
|
698
|
-
const mode = placeholder && placeholder !== answerPlaceholderText() ? "prompt" : "line";
|
|
699
|
-
const initialInput = pendingInputPrefill;
|
|
700
|
-
const editor = mode === "prompt" ? promptEditor : createLineEditor(initialInput);
|
|
701
|
-
if (mode === "prompt") {
|
|
702
|
-
editor.setInput(initialInput);
|
|
703
|
-
}
|
|
704
|
-
editor.setViewportWidth(process.stdout.columns || 80);
|
|
705
|
-
const menuState = mode === "prompt" ? createSlashMenuState(slashCommands) : null;
|
|
706
|
-
pendingInputPrefill = "";
|
|
707
|
-
const session = { mode, prompt, placeholder, editor, menuState, resolve };
|
|
708
|
-
activeInputSession = session;
|
|
709
|
-
inputActive = true;
|
|
710
|
-
cancelActiveInput = () => completeTtyInput(session, "", false);
|
|
711
|
-
redrawInput(true);
|
|
712
|
-
});
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
function clearAssistantLineForInput() {
|
|
716
|
-
if (!assistantOutputLineOpen) {
|
|
717
|
-
return;
|
|
718
|
-
}
|
|
719
|
-
if (terminalUi) {
|
|
720
|
-
terminalUi.withSuspended(closeOpenAssistantOutputLine, { render: false });
|
|
721
|
-
} else {
|
|
722
|
-
closeOpenAssistantOutputLine();
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
function renderActiveInput(width = process.stdout.columns || 80) {
|
|
727
|
-
if (backgroundController.isMonitoring()) {
|
|
728
|
-
return backgroundController.frame(width);
|
|
729
|
-
}
|
|
730
|
-
const session = activeInputSession;
|
|
731
|
-
if (!session) {
|
|
732
|
-
return { lines: [], cursorRow: 0, cursorColumn: 0 };
|
|
733
|
-
}
|
|
734
|
-
if (session.mode === "model") {
|
|
735
|
-
return prepareComposerFrame({
|
|
736
|
-
prompt: mainPromptText(),
|
|
737
|
-
inputText: session.inputText,
|
|
738
|
-
cursor: { line: 0, column: session.inputText.length },
|
|
739
|
-
menuText: modelMenuText(session.modelState.items(), session.modelState.selectedIndex()).trimEnd(),
|
|
740
|
-
}, width);
|
|
741
|
-
}
|
|
742
|
-
if (session.mode === "choice") {
|
|
743
|
-
return prepareComposerFrame({
|
|
744
|
-
prompt: mainPromptText(),
|
|
745
|
-
inputText: session.question,
|
|
746
|
-
cursor: { line: 0, column: session.question.length },
|
|
747
|
-
menuText: choiceMenuText(session.choiceState.options(), session.choiceState.selectedIndex(), session.recommended).trimEnd(),
|
|
748
|
-
}, width);
|
|
749
|
-
}
|
|
750
|
-
if (session.mode === "sessions") {
|
|
751
|
-
return prepareComposerFrame({
|
|
752
|
-
prompt: mainPromptText(),
|
|
753
|
-
inputText: session.inputText,
|
|
754
|
-
cursor: { line: 0, column: session.inputText.length },
|
|
755
|
-
menuText: sessionMenuText(session.choiceState.options(), session.choiceState.selectedIndex()).trimEnd(),
|
|
756
|
-
}, width);
|
|
757
|
-
}
|
|
758
|
-
session.editor.setViewportWidth(width);
|
|
759
|
-
const matches = session.menuState ? syncSlashMenu(session) : [];
|
|
760
|
-
return prepareComposerFrame({
|
|
761
|
-
prompt: promptValue(session.prompt),
|
|
762
|
-
inputText: session.editor.input(),
|
|
763
|
-
cursor: session.editor.cursorPosition(),
|
|
764
|
-
placeholder: session.mode === "prompt" ? inputHintText(session.placeholder) : "",
|
|
765
|
-
menuText: session.menuState ? slashMenuText(matches, session.menuState.selectedIndex()).trimEnd() : "",
|
|
766
|
-
}, width);
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
function syncSlashMenu(session) {
|
|
770
|
-
session.menuState.setInput(session.editor.input());
|
|
771
|
-
return session.menuState.matches();
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
function handleTerminalInput(raw = "") {
|
|
775
|
-
const event = parseTerminalKey(raw);
|
|
776
|
-
if (!event) {
|
|
777
|
-
return;
|
|
778
|
-
}
|
|
779
|
-
if (event.ctrl && event.name === "c") {
|
|
780
|
-
handleSigint();
|
|
781
|
-
return;
|
|
782
|
-
}
|
|
783
|
-
if (backgroundController.isMonitoring()) {
|
|
784
|
-
backgroundController.handleInput(event);
|
|
785
|
-
return;
|
|
786
|
-
}
|
|
787
|
-
if (event.ctrl && event.name === "b") {
|
|
788
|
-
backgroundController.enterMonitor();
|
|
789
|
-
return;
|
|
790
|
-
}
|
|
791
|
-
const session = activeInputSession;
|
|
792
|
-
if (!session) {
|
|
793
|
-
return;
|
|
794
|
-
}
|
|
795
|
-
if (session.mode === "model") {
|
|
796
|
-
handleModelInput(session, event);
|
|
797
|
-
return;
|
|
798
|
-
}
|
|
799
|
-
if (session.mode === "choice") {
|
|
800
|
-
handleChoiceInput(session, event);
|
|
801
|
-
return;
|
|
802
|
-
}
|
|
803
|
-
if (session.mode === "sessions") {
|
|
804
|
-
handleSessionInput(session, event);
|
|
805
|
-
return;
|
|
806
|
-
}
|
|
807
|
-
const key = event;
|
|
808
|
-
const matches = session.menuState ? syncSlashMenu(session) : [];
|
|
809
|
-
const menuKey = !key.ctrl && !key.alt && !key.shift && ["escape", "up", "down"].includes(key.name);
|
|
810
|
-
if (session.menuState && matches.length && menuKey) {
|
|
811
|
-
if (session.menuState.handleKey("", key)) {
|
|
812
|
-
redrawInput();
|
|
813
|
-
return;
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
const result = session.editor.handleInput(key);
|
|
817
|
-
if (result === "submit") {
|
|
818
|
-
submitTtyInput(session);
|
|
819
|
-
return;
|
|
820
|
-
}
|
|
821
|
-
if (result) {
|
|
822
|
-
redrawInput();
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
function handleTerminalPaste(text) {
|
|
827
|
-
if (backgroundController.isMonitoring()) {
|
|
828
|
-
return;
|
|
829
|
-
}
|
|
830
|
-
const session = activeInputSession;
|
|
831
|
-
if (!session || session.mode === "model" || session.mode === "choice" || session.mode === "sessions") {
|
|
832
|
-
return;
|
|
833
|
-
}
|
|
834
|
-
session.editor.handleInput({ kind: "paste", text });
|
|
835
|
-
redrawInput();
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
function handleModelInput(session, key) {
|
|
839
|
-
const modified = key.ctrl || key.alt || key.shift;
|
|
840
|
-
if (!modified && (key.name === "enter" || key.name === "return")) {
|
|
841
|
-
const model = session.modelState.selectedModel()?.name || "";
|
|
842
|
-
completeTtyInput(session, model, Boolean(model), "", model ? `/model set ${model}` : "");
|
|
843
|
-
return;
|
|
844
|
-
}
|
|
845
|
-
if (!modified && key.name === "escape") {
|
|
846
|
-
completeTtyInput(session, "", false);
|
|
847
|
-
return;
|
|
848
|
-
}
|
|
849
|
-
if (!modified && session.modelState.handleKey(key)) {
|
|
850
|
-
redrawInput();
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
function submitTtyInput(session) {
|
|
855
|
-
if (session.menuState) {
|
|
856
|
-
syncSlashMenu(session);
|
|
857
|
-
}
|
|
858
|
-
const command = session.menuState?.selectedCommand();
|
|
859
|
-
const value = command ? `/${command.name}` : session.editor.input();
|
|
860
|
-
if (session.mode === "prompt") {
|
|
861
|
-
session.editor.addToHistory(value);
|
|
862
|
-
}
|
|
863
|
-
completeTtyInput(session, value, session.mode === "prompt", session.mode === "line" ? "\n" : "");
|
|
864
|
-
}
|
|
865
|
-
|
|
866
|
-
function completeTtyInput(session, value, writeUser, lineText = "", displayValue = value) {
|
|
867
|
-
if (activeInputSession !== session) {
|
|
868
|
-
return;
|
|
869
|
-
}
|
|
870
|
-
activeInputSession = null;
|
|
871
|
-
inputActive = false;
|
|
872
|
-
if (cancelActiveInput) {
|
|
873
|
-
cancelActiveInput = null;
|
|
874
|
-
}
|
|
875
|
-
const writeAction = () => {
|
|
876
|
-
if (writeUser) {
|
|
877
|
-
writeUserInput(displayValue);
|
|
878
|
-
} else if (lineText && String(value || "").trim()) {
|
|
879
|
-
process.stdout.write("\n");
|
|
880
|
-
}
|
|
881
|
-
};
|
|
882
|
-
if (terminalUi) {
|
|
883
|
-
terminalUi.withSuspended(writeAction, { render: false });
|
|
884
|
-
} else {
|
|
885
|
-
writeAction();
|
|
886
|
-
}
|
|
887
|
-
session.resolve(value);
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
function askModelMenu(models, currentModel) {
|
|
891
|
-
return new Promise((resolve) => {
|
|
892
|
-
clearAssistantLineForInput();
|
|
893
|
-
const state = createModelMenuState(models, currentModel);
|
|
894
|
-
if (!state.items().length) {
|
|
895
|
-
resolve("");
|
|
896
|
-
return;
|
|
897
|
-
}
|
|
898
|
-
const session = {
|
|
899
|
-
mode: "model",
|
|
900
|
-
inputText: "/model",
|
|
901
|
-
modelState: state,
|
|
902
|
-
resolve,
|
|
903
|
-
};
|
|
904
|
-
activeInputSession = session;
|
|
905
|
-
inputActive = true;
|
|
906
|
-
cancelActiveInput = () => completeTtyInput(session, "", false);
|
|
907
|
-
redrawInput(true);
|
|
908
|
-
});
|
|
909
|
-
}
|
|
910
|
-
|
|
911
|
-
function askChoiceMenu(event) {
|
|
912
|
-
return new Promise((resolve) => {
|
|
913
|
-
clearAssistantLineForInput();
|
|
914
|
-
const state = createChoiceMenuState(event.options, event.recommended);
|
|
915
|
-
const session = {
|
|
916
|
-
mode: "choice",
|
|
917
|
-
question: String(event.question || "Input required"),
|
|
918
|
-
choiceState: state,
|
|
919
|
-
recommended: event.recommended || "",
|
|
920
|
-
resolve,
|
|
921
|
-
};
|
|
922
|
-
activeInputSession = session;
|
|
923
|
-
inputActive = true;
|
|
924
|
-
cancelActiveInput = () => completeTtyInput(session, "", false);
|
|
925
|
-
redrawInput(true);
|
|
926
|
-
});
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
function askSessionMenu(options, sessions, currentIndex) {
|
|
930
|
-
return new Promise((resolve) => {
|
|
931
|
-
clearAssistantLineForInput();
|
|
932
|
-
const state = createChoiceMenuState(options, options[currentIndex] || "");
|
|
933
|
-
const session = {
|
|
934
|
-
mode: "sessions",
|
|
935
|
-
inputText: "/sessions",
|
|
936
|
-
choiceState: state,
|
|
937
|
-
sessions,
|
|
938
|
-
resolve,
|
|
939
|
-
};
|
|
940
|
-
activeInputSession = session;
|
|
941
|
-
inputActive = true;
|
|
942
|
-
cancelActiveInput = () => completeTtyInput(session, null, false);
|
|
943
|
-
redrawInput(true);
|
|
944
|
-
});
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
function handleChoiceInput(session, key) {
|
|
948
|
-
const modified = key.ctrl || key.alt || key.shift;
|
|
949
|
-
if (!modified && (key.name === "enter" || key.name === "return")) {
|
|
950
|
-
completeTtyInput(session, session.choiceState.selectedOption(), false);
|
|
951
|
-
return;
|
|
952
|
-
}
|
|
953
|
-
if (!modified && key.name === "escape") {
|
|
954
|
-
completeTtyInput(session, "", false);
|
|
955
|
-
return;
|
|
956
|
-
}
|
|
957
|
-
if (!modified && session.choiceState.handleKey(key)) {
|
|
958
|
-
redrawInput();
|
|
959
|
-
}
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
function handleSessionInput(session, key) {
|
|
963
|
-
const modified = key.ctrl || key.alt || key.shift;
|
|
964
|
-
if (!modified && (key.name === "enter" || key.name === "return")) {
|
|
965
|
-
const index = session.choiceState.selectedIndex();
|
|
966
|
-
completeTtyInput(session, session.sessions[index] || null, false);
|
|
967
|
-
return;
|
|
968
|
-
}
|
|
969
|
-
if (!modified && key.name === "escape") {
|
|
970
|
-
completeTtyInput(session, null, false);
|
|
971
|
-
return;
|
|
972
|
-
}
|
|
973
|
-
if (!modified && session.choiceState.handleKey(key)) {
|
|
974
|
-
redrawInput();
|
|
975
|
-
}
|
|
976
|
-
}
|
|
977
|
-
|
|
978
|
-
function singleLineText(value) {
|
|
979
|
-
return String(value || "").replace(/\s+/g, " ").trim();
|
|
980
|
-
}
|
|
981
|
-
|
|
982
|
-
function pausePrompt() {
|
|
983
|
-
inputController.pause();
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
function resumePrompt() {
|
|
987
|
-
inputController.resume();
|
|
988
|
-
}
|
|
989
|
-
|
|
990
|
-
function applyInputPrefill() {
|
|
991
|
-
if (!pendingInputPrefill) {
|
|
992
|
-
return;
|
|
993
|
-
}
|
|
994
|
-
const text = pendingInputPrefill;
|
|
995
|
-
pendingInputPrefill = "";
|
|
996
|
-
input.write(text);
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
function promptValue(prompt) {
|
|
1000
|
-
return typeof prompt === "function" ? prompt() : prompt;
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
function handleSigint() {
|
|
1006
|
-
const action = sigintAction({ activeTurn: activeTurn || activeCompact, interruptRequested, runtimeClosing });
|
|
1007
|
-
if (action === "interrupt") {
|
|
1008
|
-
interruptTurn();
|
|
1009
|
-
} else {
|
|
1010
|
-
exitFromSignal();
|
|
1011
|
-
}
|
|
1012
|
-
}
|
|
1013
|
-
|
|
1014
|
-
function interruptTurn() {
|
|
1015
|
-
turnController.interrupt();
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
function handleStdinData(chunk) {
|
|
1019
|
-
if (Buffer.from(chunk).includes(3)) {
|
|
1020
|
-
handleSigint();
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
function exitFromSignal() {
|
|
1025
|
-
if (runtimeClosing) {
|
|
1026
|
-
forceCloseRuntime();
|
|
1027
|
-
scheduleProcessExit(0, 0);
|
|
1028
|
-
return;
|
|
1029
|
-
}
|
|
1030
|
-
void shutdownRuntime();
|
|
1031
|
-
}
|
|
1032
|
-
|
|
1033
|
-
function closeAssistant() {
|
|
1034
|
-
assistantRenderer.finish();
|
|
1035
|
-
flushAssistantText(assistantStreamBuffer.flush());
|
|
1036
|
-
assistantHeaderShown = false;
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
function closeRuntime() {
|
|
1040
|
-
if (runtimeClosing) {
|
|
1041
|
-
return;
|
|
1042
|
-
}
|
|
1043
|
-
runtimeClosing = true;
|
|
1044
|
-
clearActivityTimer();
|
|
1045
|
-
backgroundController.stop();
|
|
1046
|
-
void runtimeClient.shutdown();
|
|
1047
|
-
closeInput();
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
function forceCloseRuntime() {
|
|
1051
|
-
runtimeClosing = true;
|
|
1052
|
-
clearActivityTimer();
|
|
1053
|
-
backgroundController.stop();
|
|
1054
|
-
closeInput();
|
|
1055
|
-
runtimeClient.forceShutdown();
|
|
1056
|
-
}
|
|
1057
|
-
|
|
1058
|
-
function cancelInput() {
|
|
1059
|
-
const cancel = cancelActiveInput;
|
|
1060
|
-
cancelActiveInput = null;
|
|
1061
|
-
cancel?.();
|
|
1062
|
-
}
|
|
1063
|
-
|
|
1064
|
-
async function shutdownRuntime() {
|
|
1065
|
-
if (runtimeClosing) {
|
|
1066
|
-
return;
|
|
1067
|
-
}
|
|
1068
|
-
runtimeClosing = true;
|
|
1069
|
-
clearActivityTimer();
|
|
1070
|
-
backgroundController.stop();
|
|
1071
|
-
try {
|
|
1072
|
-
await runtimeClient.shutdown();
|
|
1073
|
-
} catch {
|
|
1074
|
-
runtimeClient.forceShutdown();
|
|
1075
|
-
} finally {
|
|
1076
|
-
runtimeClient.closeInput();
|
|
1077
|
-
closeInput();
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
function scheduleProcessExit(code, delayMs) {
|
|
1082
|
-
if (processExitTimer) {
|
|
1083
|
-
return;
|
|
1084
|
-
}
|
|
1085
|
-
process.exitCode = code;
|
|
1086
|
-
processExitTimer = setTimeout(() => {
|
|
1087
|
-
try {
|
|
1088
|
-
closeInput();
|
|
1089
|
-
} finally {
|
|
1090
|
-
process.exit(code);
|
|
1091
|
-
}
|
|
1092
|
-
}, delayMs);
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
function closeInput() {
|
|
1096
|
-
inputController.close();
|
|
1097
|
-
process.stdin.off("data", handleStdinData);
|
|
1098
|
-
if (input) {
|
|
1099
|
-
const current = input;
|
|
1100
|
-
input = null;
|
|
1101
|
-
try {
|
|
1102
|
-
current.close();
|
|
1103
|
-
} catch {
|
|
1104
|
-
// Ignore readline close races during signal shutdown.
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
if (!terminalUi) {
|
|
1108
|
-
process.stdin.pause();
|
|
1109
|
-
}
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { createInterface } from "node:readline";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
|
|
8
|
+
import { createCompactContextState } from "./compact-context-state.js";
|
|
9
|
+
import { createRuntimeClient, runHelpVersion } from "./runtime-client.js";
|
|
10
|
+
import {
|
|
11
|
+
requireRuntimeInitialization,
|
|
12
|
+
runtimeMethods,
|
|
13
|
+
sessionScopedMethods,
|
|
14
|
+
turnScopedMethods,
|
|
15
|
+
isRuntimeEventForTurn,
|
|
16
|
+
} from "./runtime-protocol.js";
|
|
17
|
+
import { executeLocalSlashCommand, loadLocalSettings } from "./local-slash-commands.js";
|
|
18
|
+
import { loadCliState, saveCliState } from "./cli-state-store.js";
|
|
19
|
+
import { setTheme } from "./theme.js";
|
|
20
|
+
import { createTurnController } from "./turn-controller.js";
|
|
21
|
+
import { createCommandController } from "./command-controller.js";
|
|
22
|
+
import { createTaskMonitorController } from "./task-monitor-controller.js";
|
|
23
|
+
import { createEventController } from "./event-controller.js";
|
|
24
|
+
import { createInputController } from "./input-controller.js";
|
|
25
|
+
import { isInputClosed } from "./input-errors.js";
|
|
26
|
+
import { sigintAction } from "./interrupt-state.js";
|
|
27
|
+
import { CUSTOM_ANSWER_LABEL } from "./question-menu-state.js";
|
|
28
|
+
import { createCliState } from "./cli-state.js";
|
|
29
|
+
import { createCliRuntimeController } from "./cli-runtime-controller.js";
|
|
30
|
+
import { createCliOutputController } from "./cli-output-controller.js";
|
|
31
|
+
import { createCliInputActions } from "./cli-input-actions.js";
|
|
32
|
+
import { cliHelp, oneShotHelp, runOneShot } from "./one-shot.js";
|
|
33
|
+
import { createTui } from "./tui/tui.js";
|
|
34
|
+
import { Container } from "./tui/component.js";
|
|
35
|
+
import { ComposerArea } from "./components/composer-area.js";
|
|
36
|
+
import { MonitorStack } from "./components/monitor-stack.js";
|
|
37
|
+
import {
|
|
38
|
+
inputHintText,
|
|
39
|
+
interruptText,
|
|
40
|
+
modelMenuText,
|
|
41
|
+
themeMenuText,
|
|
42
|
+
questionMenuFrame,
|
|
43
|
+
sessionMenuText,
|
|
44
|
+
promptPlaceholderText,
|
|
45
|
+
slashMenuText,
|
|
46
|
+
startupText,
|
|
47
|
+
} from "./rendering.js";
|
|
48
|
+
|
|
49
|
+
export async function runFrontendCliApp(cliArgs = process.argv.slice(2)) {
|
|
50
|
+
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
51
|
+
const repoRoot = path.resolve(scriptDir, "..", "..");
|
|
52
|
+
const python = process.env.RIND_PYTHON || "python";
|
|
53
|
+
const runtimePath = process.env.RIND_RUNTIME_PATH || resolveInstalledRuntime();
|
|
54
|
+
|
|
55
|
+
function resolveInstalledRuntime() {
|
|
56
|
+
const packageNames = {
|
|
57
|
+
win32: "@rind-ai/runtime-win32-x64",
|
|
58
|
+
linux: "@rind-ai/runtime-linux-x64",
|
|
59
|
+
darwin: process.arch === "arm64" ? "@rind-ai/runtime-darwin-arm64" : "@rind-ai/runtime-darwin-x64",
|
|
60
|
+
};
|
|
61
|
+
const packageName = packageNames[process.platform];
|
|
62
|
+
if (!packageName) return "";
|
|
63
|
+
try {
|
|
64
|
+
const packageRoot = path.dirname(createRequire(import.meta.url).resolve(`${packageName}/package.json`));
|
|
65
|
+
return path.join(packageRoot, "bin", process.platform === "win32" ? "rind-runtime.exe" : "rind-runtime");
|
|
66
|
+
} catch {
|
|
67
|
+
return "";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (cliArgs[0] === "run" && cliArgs.some((arg) => arg === "--help" || arg === "-h")) {
|
|
72
|
+
process.stdout.write(`${oneShotHelp}\n`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (cliArgs.some((arg) => arg === "--version" || arg === "--help" || arg === "-h")) {
|
|
76
|
+
if (cliArgs.includes("--help") || cliArgs.includes("-h")) {
|
|
77
|
+
process.stdout.write(`${cliHelp}\n\n`);
|
|
78
|
+
}
|
|
79
|
+
process.exit(runHelpVersion({ python, repoRoot, runtimePath, cliArgs }));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (cliArgs[0] === "run") {
|
|
83
|
+
try {
|
|
84
|
+
await runOneShot({
|
|
85
|
+
args: cliArgs,
|
|
86
|
+
python,
|
|
87
|
+
repoRoot,
|
|
88
|
+
runtimePath,
|
|
89
|
+
stderr: (text) => process.stderr.write(text),
|
|
90
|
+
stdout: (text) => process.stdout.write(`${text}${String(text).endsWith("\n") ? "" : "\n"}`),
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
94
|
+
process.exitCode = 2;
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const cliState = createCliState();
|
|
100
|
+
const runtimeState = cliState.runtime;
|
|
101
|
+
const sessionState = cliState.session;
|
|
102
|
+
const turnStateData = cliState.turn;
|
|
103
|
+
const inputStateData = cliState.input;
|
|
104
|
+
const displayState = cliState.display;
|
|
105
|
+
let input = null;
|
|
106
|
+
const compactContextState = createCompactContextState();
|
|
107
|
+
const isTty = Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
108
|
+
const tui = isTty
|
|
109
|
+
? createTui({ input: process.stdin, output: process.stdout })
|
|
110
|
+
: null;
|
|
111
|
+
const transcriptContainer = new Container();
|
|
112
|
+
const composerArea = new ComposerArea((width) => composeFrame(width));
|
|
113
|
+
const monitorStack = new MonitorStack({
|
|
114
|
+
composer: composerArea,
|
|
115
|
+
monitor: {
|
|
116
|
+
isMonitoring: () => Boolean(taskMonitorController?.isMonitoring()),
|
|
117
|
+
frame: (width) => taskMonitorController?.frame(width),
|
|
118
|
+
},
|
|
119
|
+
rows: () => (tui ? tui.rows : 24),
|
|
120
|
+
});
|
|
121
|
+
if (tui) {
|
|
122
|
+
tui.addChild(transcriptContainer);
|
|
123
|
+
tui.addChild(monitorStack);
|
|
124
|
+
}
|
|
125
|
+
let inputActions;
|
|
126
|
+
let inputController;
|
|
127
|
+
let eventProcessing = Promise.resolve();
|
|
128
|
+
|
|
129
|
+
tui?.onData((sequence) => inputActions?.handleTerminalInput(sequence));
|
|
130
|
+
tui?.onPaste((text) => inputActions?.handleTerminalPaste(text));
|
|
131
|
+
|
|
132
|
+
const outputController = createCliOutputController({
|
|
133
|
+
state: cliState,
|
|
134
|
+
terminalUi: tui,
|
|
135
|
+
transcript: transcriptContainer,
|
|
136
|
+
});
|
|
137
|
+
const {
|
|
138
|
+
redraw: redrawInput,
|
|
139
|
+
refreshInputState,
|
|
140
|
+
clearActivityTimer,
|
|
141
|
+
mainPromptText,
|
|
142
|
+
log: logOutput,
|
|
143
|
+
writeError: writeErrorOutput,
|
|
144
|
+
closeAssistant,
|
|
145
|
+
renderHistory,
|
|
146
|
+
} = outputController;
|
|
147
|
+
|
|
148
|
+
const runtimeClient = createRuntimeClient({
|
|
149
|
+
python,
|
|
150
|
+
repoRoot,
|
|
151
|
+
runtimePath,
|
|
152
|
+
cliArgs,
|
|
153
|
+
onMessage: (message) => {
|
|
154
|
+
eventProcessing = eventProcessing
|
|
155
|
+
.then(() => renderEvent(message))
|
|
156
|
+
.catch((error) => {
|
|
157
|
+
if (runtimeState.status !== "closing") {
|
|
158
|
+
writeErrorOutput(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
},
|
|
162
|
+
onStderr: (chunk) => writeErrorOutput(chunk),
|
|
163
|
+
onExit: (code, signal, { error }) => {
|
|
164
|
+
const wasClosing = runtimeState.status === "closing";
|
|
165
|
+
runtimeState.status = "failed";
|
|
166
|
+
runtimeState.initialization = null;
|
|
167
|
+
turnStateData.id = "";
|
|
168
|
+
displayState.lastEventSequence = 0;
|
|
169
|
+
turnStateData.active = false;
|
|
170
|
+
turnStateData.interruptRequested = false;
|
|
171
|
+
inputActions?.clearPendingInputs();
|
|
172
|
+
if (!wasClosing) {
|
|
173
|
+
runtimeState.failure = error;
|
|
174
|
+
writeErrorOutput(`Runtime stopped (${signal || (code ?? "startup failure")}): ${error.message}. Runtime commands are unavailable until it restarts.\n`);
|
|
175
|
+
} else {
|
|
176
|
+
process.exitCode = 0;
|
|
177
|
+
scheduleProcessExit(0, 0);
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
const turnState = {
|
|
182
|
+
get activeTurn() {
|
|
183
|
+
return turnStateData.active;
|
|
184
|
+
},
|
|
185
|
+
set activeTurn(value) {
|
|
186
|
+
turnStateData.active = Boolean(value);
|
|
187
|
+
},
|
|
188
|
+
get interruptRequested() {
|
|
189
|
+
return turnStateData.interruptRequested;
|
|
190
|
+
},
|
|
191
|
+
set interruptRequested(value) {
|
|
192
|
+
turnStateData.interruptRequested = Boolean(value);
|
|
193
|
+
},
|
|
194
|
+
get runtimeClosing() {
|
|
195
|
+
return runtimeState.status === "closing";
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
let turnController;
|
|
199
|
+
let commandController;
|
|
200
|
+
let taskMonitorController;
|
|
201
|
+
const runtimeController = createCliRuntimeController({
|
|
202
|
+
client: runtimeClient,
|
|
203
|
+
methods: runtimeMethods,
|
|
204
|
+
sessionScopedMethods,
|
|
205
|
+
turnScopedMethods,
|
|
206
|
+
requireInitialization: requireRuntimeInitialization,
|
|
207
|
+
state: cliState,
|
|
208
|
+
getCommands: () => commandController,
|
|
209
|
+
getTurnController: () => turnController,
|
|
210
|
+
getTaskMonitor: () => taskMonitorController,
|
|
211
|
+
getCompactContextState: () => compactContextState,
|
|
212
|
+
askModelMenu: (...args) => inputActions.askModelMenu(...args),
|
|
213
|
+
askEffortMenu: (...args) => inputActions.askEffortMenu(...args),
|
|
214
|
+
askSessionMenu: (...args) => inputActions.askSessionMenu(...args),
|
|
215
|
+
askTeamBlueprint: (...args) => inputActions.askTeamBlueprint(...args),
|
|
216
|
+
restoreLiveTurn,
|
|
217
|
+
renderHistory,
|
|
218
|
+
clearPendingInputs: (...args) => inputActions.clearPendingInputs(...args),
|
|
219
|
+
closeAssistant,
|
|
220
|
+
refreshInputState,
|
|
221
|
+
updateGoalState,
|
|
222
|
+
log: logOutput,
|
|
223
|
+
writeError: writeErrorOutput,
|
|
224
|
+
redraw: redrawInput,
|
|
225
|
+
});
|
|
226
|
+
const request = runtimeController.request;
|
|
227
|
+
turnController = createTurnController({
|
|
228
|
+
request,
|
|
229
|
+
state: turnState,
|
|
230
|
+
refreshGoalState: runtimeController.refreshGoalState,
|
|
231
|
+
onTurnStart: () => {
|
|
232
|
+
displayState.assistantHeaderShown = false;
|
|
233
|
+
},
|
|
234
|
+
output: {
|
|
235
|
+
queueInput: (...args) => inputActions.addPendingInput(...args),
|
|
236
|
+
restoreInputText: (...args) => inputActions.restoreInputText(...args),
|
|
237
|
+
writeError: (text) => writeErrorOutput(`${text}\n`),
|
|
238
|
+
refreshInputState,
|
|
239
|
+
closeAssistant,
|
|
240
|
+
cancelInput,
|
|
241
|
+
logInterrupt: () => logOutput(() => interruptText()),
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
commandController = createCommandController({
|
|
245
|
+
request,
|
|
246
|
+
turn: turnController,
|
|
247
|
+
input: {
|
|
248
|
+
isTerminal: Boolean(tui),
|
|
249
|
+
runGoalCommand: runtimeController.runGoalCommand,
|
|
250
|
+
runModelSelector: runtimeController.runModelSelector,
|
|
251
|
+
runEffortCommand: (value) => runtimeController.runEffortCommand(value),
|
|
252
|
+
runThemeSelector: async () => {
|
|
253
|
+
const selected = await inputActions.askThemeMenu();
|
|
254
|
+
if (selected) {
|
|
255
|
+
await commandController.handle(`/theme ${selected}`);
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
startCompactCommand: runtimeController.startCompactCommand,
|
|
259
|
+
runSessionsSelector: runtimeController.runSessionsSelector,
|
|
260
|
+
runLocalCommand: async (text) => {
|
|
261
|
+
if (!Object.keys(sessionState.settings).length) {
|
|
262
|
+
sessionState.settings = await loadLocalSettings(undefined, sessionState.info.workspace_root || sessionState.info.cwd || process.cwd());
|
|
263
|
+
}
|
|
264
|
+
const result = await executeLocalSlashCommand(text, {
|
|
265
|
+
settings: sessionState.settings,
|
|
266
|
+
sessionInfo: sessionState.info,
|
|
267
|
+
cwd: sessionState.info.workspace_root || sessionState.info.cwd || process.cwd(),
|
|
268
|
+
runtimeStarted: runtimeState.status === "starting" || runtimeState.status === "ready",
|
|
269
|
+
runtimeInitialized: runtimeState.status === "ready",
|
|
270
|
+
interactive: Boolean(tui),
|
|
271
|
+
commands: sessionState.commands,
|
|
272
|
+
persistTheme: (name) => saveCliState({ theme: name }),
|
|
273
|
+
});
|
|
274
|
+
if (result?.display?.type === "theme" && result.display.changed) {
|
|
275
|
+
outputController.replayAll();
|
|
276
|
+
}
|
|
277
|
+
return result;
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
state: {
|
|
281
|
+
get slashCommands() {
|
|
282
|
+
return sessionState.commands;
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
output: {
|
|
286
|
+
log: logOutput,
|
|
287
|
+
setInputPrefill: (value) => {
|
|
288
|
+
inputStateData.prefill = String(value || "");
|
|
289
|
+
},
|
|
290
|
+
shutdown: shutdownRuntime,
|
|
291
|
+
exit: () => process.exit(0),
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
taskMonitorController = createTaskMonitorController({
|
|
295
|
+
request,
|
|
296
|
+
terminalUi: Boolean(tui),
|
|
297
|
+
state: {
|
|
298
|
+
get runtimeClosing() {
|
|
299
|
+
return runtimeState.status === "closing";
|
|
300
|
+
},
|
|
301
|
+
get sessionInfo() {
|
|
302
|
+
return sessionState.info;
|
|
303
|
+
},
|
|
304
|
+
set sessionInfo(value) {
|
|
305
|
+
sessionState.info = value;
|
|
306
|
+
},
|
|
307
|
+
get inputActive() {
|
|
308
|
+
return inputStateData.active;
|
|
309
|
+
},
|
|
310
|
+
set inputActive(value) {
|
|
311
|
+
inputStateData.active = Boolean(value);
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
redraw: redrawInput,
|
|
315
|
+
log: logOutput,
|
|
316
|
+
});
|
|
317
|
+
const eventController = createEventController({
|
|
318
|
+
state: {
|
|
319
|
+
get runtimeClosing() {
|
|
320
|
+
return runtimeState.status === "closing";
|
|
321
|
+
},
|
|
322
|
+
get activeTurn() {
|
|
323
|
+
return turnStateData.active;
|
|
324
|
+
},
|
|
325
|
+
debug: cliArgs.includes("--debug"),
|
|
326
|
+
},
|
|
327
|
+
input: { answerQuestion: (...args) => inputActions.answerQuestion(...args) },
|
|
328
|
+
monitor: taskMonitorController,
|
|
329
|
+
output: {
|
|
330
|
+
assistantAppend: outputController.assistantAppend,
|
|
331
|
+
beginTool: (...args) => outputController.beginTool(...args),
|
|
332
|
+
updateToolProgress: (...args) => outputController.updateToolProgress(...args),
|
|
333
|
+
finishTool: (...args) => outputController.finishTool(...args),
|
|
334
|
+
handleContextBuilt: (event) => compactContextState.handleContextBuilt(event),
|
|
335
|
+
resetContextUsage,
|
|
336
|
+
closeAssistant,
|
|
337
|
+
log: logOutput,
|
|
338
|
+
debug: (text) => writeErrorOutput(`${text}\n`),
|
|
339
|
+
updateGoal: updateGoalState,
|
|
340
|
+
setGoalChasing: (enabled) => {
|
|
341
|
+
displayState.goalChasing = Boolean(enabled);
|
|
342
|
+
},
|
|
343
|
+
setStats: (stats) => {
|
|
344
|
+
displayState.stats = stats;
|
|
345
|
+
},
|
|
346
|
+
redraw: redrawInput,
|
|
347
|
+
clearCompactContext: () => compactContextState.clear(),
|
|
348
|
+
deliverQueuedInput: (...args) => inputActions.deliverQueuedInput(...args),
|
|
349
|
+
clearQueuedInputs: (...args) => inputActions.clearPendingInputs(...args),
|
|
350
|
+
},
|
|
351
|
+
});
|
|
352
|
+
inputActions = createCliInputActions({
|
|
353
|
+
state: cliState,
|
|
354
|
+
request,
|
|
355
|
+
output: outputController,
|
|
356
|
+
getTurnController: () => turnController,
|
|
357
|
+
getTaskMonitor: () => taskMonitorController,
|
|
358
|
+
getLineInput: () => input,
|
|
359
|
+
pausePrompt: () => inputController.pause(),
|
|
360
|
+
resumePrompt: () => inputController.resume(),
|
|
361
|
+
handleSigint,
|
|
362
|
+
});
|
|
363
|
+
inputController = createInputController({
|
|
364
|
+
terminalUi: tui,
|
|
365
|
+
state: {
|
|
366
|
+
get runtimeClosing() {
|
|
367
|
+
return runtimeState.status === "closing";
|
|
368
|
+
},
|
|
369
|
+
get promptPaused() {
|
|
370
|
+
return inputStateData.paused;
|
|
371
|
+
},
|
|
372
|
+
set promptPaused(value) {
|
|
373
|
+
inputStateData.paused = Boolean(value);
|
|
374
|
+
},
|
|
375
|
+
},
|
|
376
|
+
askInput: (...args) => inputActions.ask(...args),
|
|
377
|
+
onSubmit: (text) => turnController.submit(text),
|
|
378
|
+
onCommand: (text) => commandController.handle(text),
|
|
379
|
+
onPaste: (...args) => inputActions.handleTerminalPaste(...args),
|
|
380
|
+
onInput: (...args) => inputActions.handleTerminalInput(...args),
|
|
381
|
+
cancelInput,
|
|
382
|
+
prompt: mainPromptText,
|
|
383
|
+
placeholder: promptPlaceholderText,
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
process.on("SIGINT", handleSigint);
|
|
387
|
+
|
|
388
|
+
try {
|
|
389
|
+
const persistedState = loadCliState();
|
|
390
|
+
if (persistedState.theme) {
|
|
391
|
+
setTheme(persistedState.theme);
|
|
392
|
+
}
|
|
393
|
+
sessionState.settings = await loadLocalSettings(undefined, process.cwd());
|
|
394
|
+
sessionState.info = { cwd: process.cwd(), model: sessionState.settings.model };
|
|
395
|
+
sessionState.commands = commandController.localCommands();
|
|
396
|
+
await runtimeController.ensureRuntime();
|
|
397
|
+
const startupInfo = { ...sessionState.info, resume_preview: "" };
|
|
398
|
+
if (tui) {
|
|
399
|
+
outputController.showStartup(startupInfo);
|
|
400
|
+
} else {
|
|
401
|
+
logOutput(startupText(startupInfo));
|
|
402
|
+
}
|
|
403
|
+
await runtimeController.restoreSession();
|
|
404
|
+
if (tui) {
|
|
405
|
+
inputController.start();
|
|
406
|
+
} else {
|
|
407
|
+
input = createInterface({
|
|
408
|
+
input: process.stdin,
|
|
409
|
+
output: process.stdout,
|
|
410
|
+
historySize: 100,
|
|
411
|
+
removeHistoryDuplicates: true,
|
|
412
|
+
});
|
|
413
|
+
process.stdin.on("data", handleStdinData);
|
|
414
|
+
}
|
|
415
|
+
await inputController.promptLoop();
|
|
416
|
+
} catch (error) {
|
|
417
|
+
closeAssistant();
|
|
418
|
+
if (!isInputClosed(error)) {
|
|
419
|
+
writeErrorOutput(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
420
|
+
process.exitCode = 1;
|
|
421
|
+
}
|
|
422
|
+
} finally {
|
|
423
|
+
closeRuntime();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function updateGoalState(goal) {
|
|
427
|
+
sessionState.info = { ...sessionState.info, goal: goal && typeof goal === "object" ? goal : null };
|
|
428
|
+
redrawInput();
|
|
429
|
+
}
|
|
430
|
+
function resetContextUsage() {
|
|
431
|
+
displayState.stats = { context_usage_percent: 0 };
|
|
432
|
+
redrawInput();
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
async function renderEvent(message) {
|
|
436
|
+
const sequence = Number(message?.sequence);
|
|
437
|
+
if (!Number.isInteger(sequence) || sequence <= displayState.lastEventSequence) {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
displayState.lastEventSequence = sequence;
|
|
441
|
+
if (!isRuntimeEventForTurn(message, sessionState.info.session_id, turnStateData.id)) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (message?.event?.type === "turn_started") {
|
|
445
|
+
if (turnStateData.id && String(message.turn_id || "") !== turnStateData.id) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
turnStateData.id = String(message.turn_id || "");
|
|
449
|
+
turnStateData.active = Boolean(turnStateData.id);
|
|
450
|
+
}
|
|
451
|
+
const result = await eventController.handle(message);
|
|
452
|
+
if (["turn_completed", "turn_failed", "turn_cancelled"].includes(message?.event?.type)) {
|
|
453
|
+
turnStateData.id = "";
|
|
454
|
+
}
|
|
455
|
+
return result;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function restoreLiveTurn(value) {
|
|
459
|
+
if (!value || typeof value !== "object") return;
|
|
460
|
+
const turnId = String(value.turn_id || "");
|
|
461
|
+
if (!turnId) return;
|
|
462
|
+
turnStateData.id = turnId;
|
|
463
|
+
turnStateData.active = true;
|
|
464
|
+
displayState.assistantHeaderShown = false;
|
|
465
|
+
const text = String(value.assistant_text || "");
|
|
466
|
+
if (text) outputController.assistantAppend(text);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function composeFrame(width = process.stdout.columns || 80) {
|
|
470
|
+
const session = inputStateData.session;
|
|
471
|
+
if (!session) {
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
const choiceMenu = ["model", "theme", "sessions", "team-blueprints"].includes(session.mode);
|
|
475
|
+
if (session.mode === "prompt" && session.menuState) {
|
|
476
|
+
session.menuState.setInput(session.editor.input());
|
|
477
|
+
}
|
|
478
|
+
const slashMenuOpen = session.mode === "prompt"
|
|
479
|
+
&& session.menuState
|
|
480
|
+
&& session.menuState.matches().length > 0;
|
|
481
|
+
const showCaret = (!displayState.activeCompact && !choiceMenu && !slashMenuOpen)
|
|
482
|
+
|| (session.mode === "question" && session.questionState.isEditing());
|
|
483
|
+
if (session.mode === "model") {
|
|
484
|
+
return {
|
|
485
|
+
showCaret,
|
|
486
|
+
prompt: mainPromptText(width),
|
|
487
|
+
inputText: session.inputText,
|
|
488
|
+
cursor: { line: 0, column: session.inputText.length },
|
|
489
|
+
menuText: modelMenuText(session.modelState.items(), session.modelState.selectedIndex()).trimEnd(),
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
if (session.mode === "theme") {
|
|
493
|
+
return {
|
|
494
|
+
showCaret,
|
|
495
|
+
prompt: mainPromptText(width),
|
|
496
|
+
inputText: session.inputText,
|
|
497
|
+
cursor: { line: 0, column: session.inputText.length },
|
|
498
|
+
menuText: themeMenuText(session.themeState.items(), session.themeState.selectedIndex()).trimEnd(),
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
if (session.mode === "question") {
|
|
502
|
+
const editing = session.questionState.isEditing();
|
|
503
|
+
const menu = questionMenuFrame(
|
|
504
|
+
session.questionState.options(),
|
|
505
|
+
session.questionState.selectedIndex(),
|
|
506
|
+
editing ? session.editor.input() : "",
|
|
507
|
+
editing,
|
|
508
|
+
CUSTOM_ANSWER_LABEL,
|
|
509
|
+
width,
|
|
510
|
+
);
|
|
511
|
+
return {
|
|
512
|
+
showCaret,
|
|
513
|
+
prompt: mainPromptText(width),
|
|
514
|
+
inputText: session.question,
|
|
515
|
+
cursor: editing ? session.editor.cursorPosition() : { line: 0, column: session.question.length },
|
|
516
|
+
menuText: menu.text.trimEnd(),
|
|
517
|
+
menuCursor: editing ? menu.cursor : null,
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
if (session.mode === "sessions") {
|
|
521
|
+
return {
|
|
522
|
+
showCaret,
|
|
523
|
+
prompt: mainPromptText(width),
|
|
524
|
+
inputText: session.inputText,
|
|
525
|
+
cursor: { line: 0, column: session.inputText.length },
|
|
526
|
+
menuText: sessionMenuText(session.choiceState.options(), session.choiceState.selectedIndex()).trimEnd(),
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
if (session.mode === "team-blueprints") {
|
|
530
|
+
return {
|
|
531
|
+
showCaret,
|
|
532
|
+
prompt: mainPromptText(width),
|
|
533
|
+
inputText: session.inputText,
|
|
534
|
+
cursor: { line: 0, column: session.inputText.length },
|
|
535
|
+
menuText: sessionMenuText(session.choiceState.options(), session.choiceState.selectedIndex()).trimEnd(),
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
const matches = session.menuState
|
|
539
|
+
? (session.menuState.setInput(session.editor.input()), session.menuState.matches())
|
|
540
|
+
: [];
|
|
541
|
+
if (typeof session.editor.setViewportWidth === "function") {
|
|
542
|
+
session.editor.setViewportWidth(width);
|
|
543
|
+
}
|
|
544
|
+
return {
|
|
545
|
+
showCaret,
|
|
546
|
+
prompt: typeof session.prompt === "function" ? session.prompt(width) : session.prompt,
|
|
547
|
+
inputText: session.editor.input(),
|
|
548
|
+
cursor: session.editor.cursorPosition(),
|
|
549
|
+
placeholder: session.mode === "prompt" ? inputHintText(session.placeholder) : "",
|
|
550
|
+
menuText: session.menuState ? slashMenuText(matches, session.menuState.selectedIndex()).trimEnd() : "",
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function handleSigint() {
|
|
555
|
+
const action = sigintAction({
|
|
556
|
+
activeTurn: turnStateData.active || displayState.activeCompact,
|
|
557
|
+
interruptRequested: turnStateData.interruptRequested,
|
|
558
|
+
runtimeClosing: runtimeState.status === "closing",
|
|
559
|
+
});
|
|
560
|
+
if (action === "interrupt") {
|
|
561
|
+
interruptTurn();
|
|
562
|
+
} else {
|
|
563
|
+
exitFromSignal();
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
function interruptTurn() {
|
|
568
|
+
turnController.interrupt();
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
function handleStdinData(chunk) {
|
|
572
|
+
if (Buffer.from(chunk).includes(3)) {
|
|
573
|
+
handleSigint();
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function exitFromSignal() {
|
|
578
|
+
if (runtimeState.status === "closing") {
|
|
579
|
+
forceCloseRuntime();
|
|
580
|
+
scheduleProcessExit(0, 0);
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
void shutdownRuntime();
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function closeRuntime() {
|
|
587
|
+
if (runtimeState.status === "closing") {
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
runtimeState.status = "closing";
|
|
591
|
+
clearActivityTimer();
|
|
592
|
+
taskMonitorController.stop();
|
|
593
|
+
void runtimeClient.shutdown();
|
|
594
|
+
closeInput();
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function forceCloseRuntime() {
|
|
598
|
+
runtimeState.status = "closing";
|
|
599
|
+
clearActivityTimer();
|
|
600
|
+
taskMonitorController.stop();
|
|
601
|
+
closeInput();
|
|
602
|
+
runtimeClient.forceShutdown();
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
function cancelInput() {
|
|
606
|
+
inputActions?.cancel();
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
async function shutdownRuntime() {
|
|
610
|
+
if (runtimeState.status === "closing") {
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
runtimeState.status = "closing";
|
|
614
|
+
clearActivityTimer();
|
|
615
|
+
taskMonitorController.stop();
|
|
616
|
+
try {
|
|
617
|
+
await runtimeClient.shutdown();
|
|
618
|
+
} catch {
|
|
619
|
+
runtimeClient.forceShutdown();
|
|
620
|
+
} finally {
|
|
621
|
+
runtimeClient.closeInput();
|
|
622
|
+
closeInput();
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function scheduleProcessExit(code, delayMs) {
|
|
627
|
+
if (displayState.processExitTimer) {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
process.exitCode = code;
|
|
631
|
+
displayState.processExitTimer = setTimeout(() => {
|
|
632
|
+
try {
|
|
633
|
+
closeInput();
|
|
634
|
+
} finally {
|
|
635
|
+
process.exit(code);
|
|
636
|
+
}
|
|
637
|
+
}, delayMs);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
function closeInput() {
|
|
641
|
+
inputController.close();
|
|
642
|
+
process.stdin.off("data", handleStdinData);
|
|
643
|
+
if (input) {
|
|
644
|
+
const current = input;
|
|
645
|
+
input = null;
|
|
646
|
+
try {
|
|
647
|
+
current.close();
|
|
648
|
+
} catch {
|
|
649
|
+
// Ignore readline close races during signal shutdown.
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
if (!tui) {
|
|
653
|
+
process.stdin.pause();
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|