pi-wechat-session-hub 0.1.0 → 0.1.2
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/CHANGELOG.md +95 -0
- package/README.md +420 -365
- package/docs/handoff-next-session.md +4 -4
- package/docs/implementation-spec.md +150 -147
- package/extensions/daemon-entry.mjs +31 -0
- package/extensions/wechat-session-hub.ts +12 -12
- package/package.json +72 -67
- package/scripts/probe-wt-capability.mjs +26 -5
- package/scripts/release-preflight.mjs +229 -0
- package/src/gateway/command-parser.ts +25 -9
- package/src/gateway/command-router.ts +1790 -1449
- package/src/gateway/durable-message-queue.ts +1154 -1043
- package/src/gateway/gateway-control-ipc.ts +705 -687
- package/src/gateway/turn-health.ts +305 -0
- package/src/gateway/ui-worker-extension-bridge.ts +1092 -1070
- package/src/gateway/ui-worker-ipc.ts +1517 -1483
- package/src/gateway/wechat-ilink/api-client.ts +386 -390
- package/src/runtime/gateway-lifecycle-operation.ts +67 -2
- package/src/runtime/gateway-service-controller.ts +341 -339
- package/src/runtime/ui-worker-manager.ts +1216 -1186
- package/src/runtime/windows-logon-autostart-manager.ts +401 -0
- package/src/runtime/windows-logon-task-manager.ts +0 -212
|
@@ -1,1070 +1,1092 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
private
|
|
59
|
-
private
|
|
60
|
-
private
|
|
61
|
-
private
|
|
62
|
-
private
|
|
63
|
-
private
|
|
64
|
-
private
|
|
65
|
-
private
|
|
66
|
-
private
|
|
67
|
-
private
|
|
68
|
-
private
|
|
69
|
-
private
|
|
70
|
-
private
|
|
71
|
-
private
|
|
72
|
-
private
|
|
73
|
-
private
|
|
74
|
-
private
|
|
75
|
-
private readonly
|
|
76
|
-
private readonly
|
|
77
|
-
private readonly
|
|
78
|
-
private readonly
|
|
79
|
-
private
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* @param
|
|
86
|
-
* @param
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
this.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
this.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* @
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* @
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
this.
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
* @param
|
|
147
|
-
* @
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
this.
|
|
158
|
-
this.
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
this.
|
|
176
|
-
this.
|
|
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
|
-
this.managerDirectory,
|
|
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
|
-
value.pid
|
|
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
|
-
this.
|
|
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
|
-
this.
|
|
332
|
-
this.
|
|
333
|
-
this.
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
* @param
|
|
341
|
-
* @
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
this.
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
* @
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
this.pendingPrompt
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
this.
|
|
372
|
-
|
|
373
|
-
this.client
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
this.
|
|
377
|
-
this.
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
this.
|
|
382
|
-
this.
|
|
383
|
-
this.
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
* @param
|
|
403
|
-
* @
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
const
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
const
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
*
|
|
436
|
-
*
|
|
437
|
-
* @param
|
|
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
|
-
const
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
* @param
|
|
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
|
-
const
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
this.pendingPrompt
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
*
|
|
551
|
-
*
|
|
552
|
-
* @
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
this.
|
|
561
|
-
this.
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
this.
|
|
567
|
-
|
|
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
|
-
this.
|
|
612
|
-
this.
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
this.
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
*
|
|
622
|
-
*
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
this.launchRecord?.
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
this.registration
|
|
646
|
-
this.registration.
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
this.
|
|
653
|
-
this.
|
|
654
|
-
this.
|
|
655
|
-
this.
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
this.
|
|
664
|
-
this.
|
|
665
|
-
this.
|
|
666
|
-
this.
|
|
667
|
-
this.
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
this.
|
|
674
|
-
this.
|
|
675
|
-
this.
|
|
676
|
-
this.
|
|
677
|
-
this.
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
this.
|
|
685
|
-
this.
|
|
686
|
-
this.
|
|
687
|
-
this.
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
*
|
|
694
|
-
*
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
this.
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
*
|
|
712
|
-
*
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
this.reconnectTimer
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
this.recoveryTimer
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
*
|
|
733
|
-
*
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
this.heartbeatTimer
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
*
|
|
745
|
-
*
|
|
746
|
-
* @
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
*
|
|
757
|
-
*
|
|
758
|
-
* @
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
*
|
|
783
|
-
*
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
*
|
|
792
|
-
*
|
|
793
|
-
* @param
|
|
794
|
-
* @
|
|
795
|
-
* @
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
*
|
|
811
|
-
*
|
|
812
|
-
* @param
|
|
813
|
-
* @
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
const
|
|
839
|
-
const
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
*
|
|
859
|
-
*
|
|
860
|
-
* @param
|
|
861
|
-
* @param
|
|
862
|
-
* @
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
let
|
|
871
|
-
|
|
872
|
-
const
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
bridge = new UiWorkerExtensionBridge(
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
pi.on("
|
|
971
|
-
pi.on("
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
*
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
*
|
|
1005
|
-
*
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
.
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
*
|
|
1027
|
-
*
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
.
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
*
|
|
1049
|
-
*
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
*
|
|
1059
|
-
*
|
|
1060
|
-
* @
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { lstat, readFile, realpath } from "node:fs/promises";
|
|
4
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
FooterComponent,
|
|
7
|
+
type AgentSession,
|
|
8
|
+
type ExtensionAPI,
|
|
9
|
+
type ExtensionContext,
|
|
10
|
+
} from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import { isProcessAlive } from "../system/process-liveness.ts";
|
|
12
|
+
import { ProjectService, ProjectServiceError } from "../projects/project-service.ts";
|
|
13
|
+
import {
|
|
14
|
+
createStableNamedPipe,
|
|
15
|
+
readOrCreateUiWorkerAttachToken,
|
|
16
|
+
readUiWorkerAttachToken,
|
|
17
|
+
UiWorkerIpcClient,
|
|
18
|
+
UiWorkerIpcError,
|
|
19
|
+
readUiWorkerLaunchRecord,
|
|
20
|
+
} from "./ui-worker-ipc.ts";
|
|
21
|
+
import type { UiWorkerLaunchRecord, UiWorkerRegistration } from "./ui-worker-types.ts";
|
|
22
|
+
|
|
23
|
+
interface PendingUiPrompt {
|
|
24
|
+
expectedText: string;
|
|
25
|
+
remoteTurnStarted: boolean;
|
|
26
|
+
resolve: (result: unknown) => void;
|
|
27
|
+
reject: (error: Error) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type UiWorkerConnectionState = "connected" | "connecting" | "disconnected";
|
|
31
|
+
|
|
32
|
+
type UiWorkerStatusSink = (state: UiWorkerConnectionState) => void;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 可供本机诊断展示的 UI worker bridge 状态快照。
|
|
36
|
+
*/
|
|
37
|
+
export interface UiWorkerBridgeDiagnostics {
|
|
38
|
+
instanceId: string;
|
|
39
|
+
sessionId: string | undefined;
|
|
40
|
+
state: UiWorkerConnectionState;
|
|
41
|
+
reconnectAttempts: number;
|
|
42
|
+
lastConnectedAt: string | undefined;
|
|
43
|
+
lastDisconnectReason: string | undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface UiWorkerBridgeProcessState extends NodeJS.Process {
|
|
47
|
+
piWechatUiWorkerBridge?: UiWorkerExtensionBridge;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const bridgeProcess = process as UiWorkerBridgeProcessState;
|
|
51
|
+
const PROCESS_BIRTH_ID = `${process.pid}:${Math.floor(Date.now() - process.uptime() * 1000)}`;
|
|
52
|
+
const RECOVERY_CHECK_INTERVAL_MS = 15000;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 在一个 Pi TUI extension host 内运行 session owner named pipe bridge。
|
|
56
|
+
*/
|
|
57
|
+
export class UiWorkerExtensionBridge {
|
|
58
|
+
private pi?: ExtensionAPI;
|
|
59
|
+
private context?: ExtensionContext;
|
|
60
|
+
private client?: UiWorkerIpcClient;
|
|
61
|
+
private registration?: UiWorkerRegistration;
|
|
62
|
+
private connectionTask?: Promise<void>;
|
|
63
|
+
private reconnectTimer?: NodeJS.Timeout;
|
|
64
|
+
private recoveryTimer?: NodeJS.Timeout;
|
|
65
|
+
private heartbeatTimer?: NodeJS.Timeout;
|
|
66
|
+
private reconnectDelayMs = 500;
|
|
67
|
+
private closing = false;
|
|
68
|
+
private connectionState: UiWorkerConnectionState = "disconnected";
|
|
69
|
+
private reconnectAttempts = 0;
|
|
70
|
+
private lastConnectedAt: string | undefined;
|
|
71
|
+
private lastDisconnectReason: string | undefined;
|
|
72
|
+
private initialSessionId?: string;
|
|
73
|
+
private latestAssistantText = "";
|
|
74
|
+
private pendingPrompt?: PendingUiPrompt;
|
|
75
|
+
private readonly idleWaiters = new Set<() => void>();
|
|
76
|
+
private readonly launchId: string;
|
|
77
|
+
private readonly instanceId: string;
|
|
78
|
+
private readonly managerDirectory: string;
|
|
79
|
+
private readonly launchRecord?: UiWorkerLaunchRecord;
|
|
80
|
+
private statusSink?: UiWorkerStatusSink;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 创建可见 Pi session 的 Gateway bridge, 支持 Gateway 启动的 TUI 与普通 Pi TUI。
|
|
84
|
+
*
|
|
85
|
+
* @param launchRecord 可选的一次性 UI worker 启动记录。
|
|
86
|
+
* @param projectService 用于重新解析当前 session cwd 与项目 alias。
|
|
87
|
+
* @param managerDirectory 普通 Pi TUI 使用的 Hub 数据目录。
|
|
88
|
+
*/
|
|
89
|
+
constructor(
|
|
90
|
+
launchRecord: UiWorkerLaunchRecord | undefined,
|
|
91
|
+
private readonly projectService: ProjectService,
|
|
92
|
+
managerDirectory?: string,
|
|
93
|
+
) {
|
|
94
|
+
this.launchRecord = launchRecord;
|
|
95
|
+
this.managerDirectory = launchRecord?.managerDirectory ?? managerDirectory ?? "";
|
|
96
|
+
if (!this.managerDirectory) {
|
|
97
|
+
throw new UiWorkerIpcError("handshake_rejected", "普通 Pi TUI bridge 缺少 Hub manager directory。 ");
|
|
98
|
+
}
|
|
99
|
+
this.launchId = launchRecord?.launchId ?? randomUUID();
|
|
100
|
+
this.instanceId = launchRecord?.instanceId ?? randomUUID();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 设置当前 TUI footer 状态回调, 用于显示 Hub 是否已接入.
|
|
105
|
+
*
|
|
106
|
+
* @param sink 状态回调. 传入 undefined 时移除回调.
|
|
107
|
+
* @returns 无返回值.
|
|
108
|
+
*/
|
|
109
|
+
setStatusSink(sink: UiWorkerStatusSink | undefined): void {
|
|
110
|
+
this.statusSink = sink;
|
|
111
|
+
if (sink) {
|
|
112
|
+
sink(this.client ? "connected" : "disconnected");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 向当前 TUI 通知 UI worker 连接状态.
|
|
118
|
+
*
|
|
119
|
+
* @param state 当前连接状态.
|
|
120
|
+
* @returns 无返回值.
|
|
121
|
+
*/
|
|
122
|
+
private notifyConnectionState(state: UiWorkerConnectionState): void {
|
|
123
|
+
this.connectionState = state;
|
|
124
|
+
this.statusSink?.(state);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 获取当前 bridge 连接诊断快照, 供 `/wx status` 展示真实连接状态。
|
|
129
|
+
*
|
|
130
|
+
* @returns 不包含凭证与消息正文的诊断信息。
|
|
131
|
+
*/
|
|
132
|
+
getDiagnostics(): UiWorkerBridgeDiagnostics {
|
|
133
|
+
return {
|
|
134
|
+
instanceId: this.instanceId,
|
|
135
|
+
sessionId: this.registration?.sessionId,
|
|
136
|
+
state: this.connectionState,
|
|
137
|
+
reconnectAttempts: this.reconnectAttempts,
|
|
138
|
+
lastConnectedAt: this.lastConnectedAt,
|
|
139
|
+
lastDisconnectReason: this.lastDisconnectReason,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 绑定 Pi 当前 session context 并注册或更新 Gateway session owner。
|
|
145
|
+
*
|
|
146
|
+
* @param pi 当前 Pi extension API。
|
|
147
|
+
* @param context 当前 session 的只读 context。
|
|
148
|
+
* @returns 注册和重连任务开始后正常结束。
|
|
149
|
+
*/
|
|
150
|
+
async attach(pi: ExtensionAPI, context: ExtensionContext): Promise<void> {
|
|
151
|
+
const isFirstSession = this.initialSessionId === undefined;
|
|
152
|
+
if (isFirstSession && this.launchRecord?.sessionName) {
|
|
153
|
+
pi.setSessionName(this.launchRecord.sessionName);
|
|
154
|
+
}
|
|
155
|
+
const registration = await this.createRegistration(context, isFirstSession);
|
|
156
|
+
const previous = this.registration;
|
|
157
|
+
this.pi = pi;
|
|
158
|
+
this.context = context;
|
|
159
|
+
this.registration = registration;
|
|
160
|
+
if (!this.initialSessionId) {
|
|
161
|
+
this.initialSessionId = registration.sessionId;
|
|
162
|
+
}
|
|
163
|
+
if (this.client) {
|
|
164
|
+
if (previous && previous.sessionId !== registration.sessionId) {
|
|
165
|
+
this.client.notify("session_changed", { registration });
|
|
166
|
+
} else if (previous && previous.name !== registration.name) {
|
|
167
|
+
this.client.notify("session_info_changed", {
|
|
168
|
+
registration,
|
|
169
|
+
clearName: registration.name === undefined,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
this.notifyConnectionState("connected");
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
this.reconnectDelayMs = 500;
|
|
176
|
+
this.notifyConnectionState("connecting");
|
|
177
|
+
this.scheduleConnect(0);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 在 Pi session switch 前向 Gateway 检查目标 session owner。
|
|
182
|
+
*
|
|
183
|
+
* @param sessionFile 用户选择的 Pi session transcript 文件。
|
|
184
|
+
* @returns Gateway 确认无 writer 冲突时返回 true。
|
|
185
|
+
*/
|
|
186
|
+
async canSwitchSession(sessionFile: string): Promise<boolean> {
|
|
187
|
+
if (!isAbsolute(sessionFile)) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
if (this.client) {
|
|
191
|
+
try {
|
|
192
|
+
const result = await this.client.request("can_switch_session", { sessionFile }, 10000);
|
|
193
|
+
return isRecord(result) && result.allowed === true;
|
|
194
|
+
} catch {
|
|
195
|
+
return !(await this.hasLiveSessionOwner(sessionFile));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
let controlClient: UiWorkerIpcClient | undefined;
|
|
200
|
+
try {
|
|
201
|
+
const managerToken = await readUiWorkerAttachToken(this.managerDirectory);
|
|
202
|
+
if (!managerToken) {
|
|
203
|
+
return !(await this.hasLiveSessionOwner(sessionFile));
|
|
204
|
+
}
|
|
205
|
+
controlClient = await UiWorkerIpcClient.connectControl(
|
|
206
|
+
createStableNamedPipe(this.managerDirectory),
|
|
207
|
+
this.managerDirectory,
|
|
208
|
+
managerToken,
|
|
209
|
+
{ timeoutMs: 3000 },
|
|
210
|
+
);
|
|
211
|
+
const result = await controlClient.request("can_switch_session", { sessionFile }, 5000);
|
|
212
|
+
return isRecord(result) && result.allowed === true;
|
|
213
|
+
} catch {
|
|
214
|
+
return !(await this.hasLiveSessionOwner(sessionFile));
|
|
215
|
+
} finally {
|
|
216
|
+
controlClient?.close();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 在 Gateway IPC 不可用时检查目标 transcript 是否仍有存活 registry owner。
|
|
222
|
+
*
|
|
223
|
+
* @param sessionFile 用户选择的 Pi session transcript 文件。
|
|
224
|
+
* @returns 存在活跃/状态不明 owner 时返回 true。
|
|
225
|
+
*/
|
|
226
|
+
private async hasLiveSessionOwner(sessionFile: string): Promise<boolean> {
|
|
227
|
+
let managerInfo;
|
|
228
|
+
try {
|
|
229
|
+
managerInfo = await lstat(this.managerDirectory);
|
|
230
|
+
} catch (error) {
|
|
231
|
+
return !(error instanceof Error && "code" in error && error.code === "ENOENT");
|
|
232
|
+
}
|
|
233
|
+
if (!managerInfo.isDirectory() || managerInfo.isSymbolicLink()) {
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
const instanceFile = join(this.managerDirectory, "instances.json");
|
|
237
|
+
let instanceFileInfo;
|
|
238
|
+
try {
|
|
239
|
+
instanceFileInfo = await lstat(instanceFile);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
return !(error instanceof Error && "code" in error && error.code === "ENOENT");
|
|
242
|
+
}
|
|
243
|
+
if (!instanceFileInfo.isFile() || instanceFileInfo.isSymbolicLink()) {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
let records: unknown;
|
|
247
|
+
try {
|
|
248
|
+
records = JSON.parse(await readFile(instanceFile, "utf8")) as unknown;
|
|
249
|
+
} catch {
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
if (!isRecord(records)) {
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
for (const value of Object.values(records)) {
|
|
256
|
+
if (!isRecord(value) || typeof value.sessionFile !== "string" || !samePath(value.sessionFile, sessionFile)) {
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (value.instanceId === this.instanceId) {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (
|
|
263
|
+
typeof value.pid !== "number" ||
|
|
264
|
+
!Number.isSafeInteger(value.pid) ||
|
|
265
|
+
value.pid <= 0 ||
|
|
266
|
+
isProcessAlive(value.pid)
|
|
267
|
+
) {
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 通知 Gateway 当前 UI worker 已开始 agent turn。
|
|
276
|
+
*
|
|
277
|
+
* @returns 无返回值。
|
|
278
|
+
*/
|
|
279
|
+
onAgentStart(): void {
|
|
280
|
+
if (this.pendingPrompt?.remoteTurnStarted) {
|
|
281
|
+
this.latestAssistantText = "";
|
|
282
|
+
}
|
|
283
|
+
this.sendState("running");
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 收集当前 turn 的最终 assistant 普通文本, 不转发 thinking 或工具参数。
|
|
288
|
+
*
|
|
289
|
+
* @param event Pi message end event。
|
|
290
|
+
* @returns 无返回值。
|
|
291
|
+
*/
|
|
292
|
+
onMessageEnd(event: { message: unknown }): void {
|
|
293
|
+
if (!isRecord(event.message)) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (
|
|
297
|
+
event.message.role === "user" &&
|
|
298
|
+
this.pendingPrompt &&
|
|
299
|
+
!this.pendingPrompt.remoteTurnStarted &&
|
|
300
|
+
extractMessageText(event.message).trim() === this.pendingPrompt.expectedText.trim()
|
|
301
|
+
) {
|
|
302
|
+
this.pendingPrompt.remoteTurnStarted = true;
|
|
303
|
+
this.latestAssistantText = "";
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const text = extractAssistantText(event.message);
|
|
307
|
+
if (this.pendingPrompt?.remoteTurnStarted && event.message.role === "assistant" && text) {
|
|
308
|
+
this.latestAssistantText = text;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 在 Pi 完成自动重试、compaction 和 follow-up 后回传 UI worker 状态与最终回复。
|
|
314
|
+
*
|
|
315
|
+
* @param _event Pi agent settled event。
|
|
316
|
+
* @returns 无返回值。
|
|
317
|
+
*/
|
|
318
|
+
onAgentSettled(): void {
|
|
319
|
+
const pending = this.pendingPrompt;
|
|
320
|
+
if (pending?.remoteTurnStarted) {
|
|
321
|
+
this.pendingPrompt = undefined;
|
|
322
|
+
if (this.latestAssistantText) {
|
|
323
|
+
pending.resolve({ reply: this.latestAssistantText });
|
|
324
|
+
} else {
|
|
325
|
+
pending.reject(new UiWorkerIpcError("worker_disconnected", "Pi turn settled, 但没有最终 assistant 文本。 "));
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
for (const resolveIdle of this.idleWaiters) {
|
|
329
|
+
resolveIdle();
|
|
330
|
+
}
|
|
331
|
+
this.idleWaiters.clear();
|
|
332
|
+
this.sendState("idle");
|
|
333
|
+
this.client?.notify("turn_settled", { state: "idle" });
|
|
334
|
+
this.latestAssistantText = "";
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 处理 Pi session 名称变化。
|
|
339
|
+
*
|
|
340
|
+
* @param event Session info changed event。
|
|
341
|
+
* @param context 当前 Pi context。
|
|
342
|
+
* @returns 无返回值。
|
|
343
|
+
*/
|
|
344
|
+
async onSessionInfoChanged(event: { name: string | undefined }, context: ExtensionContext): Promise<void> {
|
|
345
|
+
if (!this.registration) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const registration: UiWorkerRegistration = {
|
|
349
|
+
...this.registration,
|
|
350
|
+
name: event.name,
|
|
351
|
+
};
|
|
352
|
+
this.registration = registration;
|
|
353
|
+
this.client?.notify("session_info_changed", {
|
|
354
|
+
registration,
|
|
355
|
+
cwd: context.sessionManager.getCwd(),
|
|
356
|
+
clearName: event.name === undefined,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* 关闭当前 TUI owner 时通知 Gateway 并释放 pipe reconnect timer。
|
|
362
|
+
*
|
|
363
|
+
* @param event Pi session shutdown 事件。
|
|
364
|
+
* @returns Gateway 仍在线时等待 worker owner unregister。
|
|
365
|
+
*/
|
|
366
|
+
async onSessionShutdown(event: { reason: "quit" | "reload" | "new" | "resume" | "fork" }): Promise<void> {
|
|
367
|
+
const pending = this.pendingPrompt;
|
|
368
|
+
this.pendingPrompt = undefined;
|
|
369
|
+
pending?.reject(new UiWorkerIpcError("worker_disconnected", "Pi session 正在关闭或切换, remote prompt 未完成。 "));
|
|
370
|
+
if (event.reason !== "quit") {
|
|
371
|
+
this.registration = undefined;
|
|
372
|
+
this.stopHeartbeat();
|
|
373
|
+
const previousClient = this.client;
|
|
374
|
+
this.client = undefined;
|
|
375
|
+
previousClient?.close();
|
|
376
|
+
this.lastDisconnectReason = `session ${event.reason} 切换中`;
|
|
377
|
+
this.notifyConnectionState("disconnected");
|
|
378
|
+
this.scheduleRecoveryCheck();
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
this.pi = undefined;
|
|
382
|
+
this.context = undefined;
|
|
383
|
+
this.closing = true;
|
|
384
|
+
this.clearTimers();
|
|
385
|
+
const client = this.client;
|
|
386
|
+
this.client = undefined;
|
|
387
|
+
if (client) {
|
|
388
|
+
await client.request("worker_shutdown", {
|
|
389
|
+
registration: this.registration,
|
|
390
|
+
}, 5000).catch(() => undefined);
|
|
391
|
+
client.close();
|
|
392
|
+
}
|
|
393
|
+
if (bridgeProcess.piWechatUiWorkerBridge === this) {
|
|
394
|
+
delete bridgeProcess.piWechatUiWorkerBridge;
|
|
395
|
+
}
|
|
396
|
+
this.notifyConnectionState("disconnected");
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* 生成最新的 Pi session registration 并检查 launch cwd/project。
|
|
401
|
+
*
|
|
402
|
+
* @param context 当前 Pi context。
|
|
403
|
+
* @param isFirstSession 是否为初次 launch handshake。
|
|
404
|
+
* @returns UI worker registration。
|
|
405
|
+
*/
|
|
406
|
+
private async createRegistration(
|
|
407
|
+
context: ExtensionContext,
|
|
408
|
+
isFirstSession: boolean,
|
|
409
|
+
): Promise<UiWorkerRegistration> {
|
|
410
|
+
const sessionId = context.sessionManager.getSessionId();
|
|
411
|
+
const sessionFile = context.sessionManager.getSessionFile();
|
|
412
|
+
const cwd = context.sessionManager.getCwd();
|
|
413
|
+
if (!sessionId || !sessionFile || !isAbsolute(sessionFile) || !isAbsolute(cwd)) {
|
|
414
|
+
throw new UiWorkerIpcError("handshake_rejected", "Pi UI session 缺少 sessionId、sessionFile 或绝对 cwd。 ");
|
|
415
|
+
}
|
|
416
|
+
const projectAlias = await this.resolveProjectAlias(cwd, isFirstSession);
|
|
417
|
+
const sessionName = isFirstSession
|
|
418
|
+
? this.launchRecord?.sessionName ?? context.sessionManager.getSessionName()
|
|
419
|
+
: context.sessionManager.getSessionName();
|
|
420
|
+
return {
|
|
421
|
+
launchId: this.launchId,
|
|
422
|
+
instanceId: this.instanceId,
|
|
423
|
+
pid: process.pid,
|
|
424
|
+
processBirthId: PROCESS_BIRTH_ID,
|
|
425
|
+
sessionId,
|
|
426
|
+
sessionFile,
|
|
427
|
+
cwd,
|
|
428
|
+
projectAlias,
|
|
429
|
+
state: context.isIdle() ? "idle" : "running",
|
|
430
|
+
...(sessionName ? { name: sessionName } : {}),
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* 从已登记项目中解析当前 TUI cwd, 不允许把任意路径登记为 route。
|
|
436
|
+
*
|
|
437
|
+
* @param cwd Pi session cwd。
|
|
438
|
+
* @param isFirstSession 是否验证 launch 中的预期项目。
|
|
439
|
+
* @returns 唯一匹配项目 alias。
|
|
440
|
+
*/
|
|
441
|
+
private async resolveProjectAlias(cwd: string, isFirstSession: boolean): Promise<string> {
|
|
442
|
+
let canonicalCwd: string;
|
|
443
|
+
try {
|
|
444
|
+
canonicalCwd = await realpath(cwd);
|
|
445
|
+
} catch {
|
|
446
|
+
if (this.launchRecord) {
|
|
447
|
+
throw new UiWorkerIpcError("handshake_rejected", "UI worker 项目目录不可用。 ");
|
|
448
|
+
}
|
|
449
|
+
throw new UiWorkerIpcError(
|
|
450
|
+
"project_not_registered",
|
|
451
|
+
"当前 Pi TUI 工作目录不可用, 本机会话保持可用, 但未接入 Hub。",
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
if (
|
|
455
|
+
this.launchRecord &&
|
|
456
|
+
isFirstSession &&
|
|
457
|
+
samePath(canonicalCwd, this.launchRecord.projectPath)
|
|
458
|
+
) {
|
|
459
|
+
await this.projectService.resolveSession(this.launchRecord.projectAlias, canonicalCwd);
|
|
460
|
+
return this.launchRecord.projectAlias;
|
|
461
|
+
}
|
|
462
|
+
try {
|
|
463
|
+
return (await this.projectService.ensureProjectForCwd(canonicalCwd)).alias;
|
|
464
|
+
} catch (error) {
|
|
465
|
+
if (this.launchRecord || !(error instanceof ProjectServiceError)) {
|
|
466
|
+
throw error;
|
|
467
|
+
}
|
|
468
|
+
if (error.code !== "project_path_denied") {
|
|
469
|
+
throw new UiWorkerIpcError("project_not_registered", error.message);
|
|
470
|
+
}
|
|
471
|
+
const { config } = await this.projectService.list();
|
|
472
|
+
const notice = config.allowedProjectRoots.length === 0
|
|
473
|
+
? "尚未初始化 Hub 项目授权. 执行 /wx start 可将当前目录设为授权根, 或执行 /wx roots add-current 手动授权。"
|
|
474
|
+
: "当前目录不在 Hub 授权根内. 本机会话保持可用, 但未接入 Hub. 执行 /wx roots add-current 可授权当前目录及其子目录。";
|
|
475
|
+
throw new UiWorkerIpcError("project_not_registered", notice);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* 处理 Gateway 通过 named pipe 发来的受控请求。
|
|
481
|
+
*
|
|
482
|
+
* @param method 方法名称。
|
|
483
|
+
* @param params 参数对象。
|
|
484
|
+
* @returns Pi 最终输出或 shutdown acknowledgement。
|
|
485
|
+
*/
|
|
486
|
+
private async handleGatewayRequest(method: string, params: Record<string, unknown>): Promise<unknown> {
|
|
487
|
+
if (method === "prompt") {
|
|
488
|
+
return this.handlePrompt(params);
|
|
489
|
+
}
|
|
490
|
+
if (method === "abort") {
|
|
491
|
+
this.context?.abort();
|
|
492
|
+
return { accepted: true };
|
|
493
|
+
}
|
|
494
|
+
if (method === "shutdown") {
|
|
495
|
+
if (!this.context) {
|
|
496
|
+
throw new UiWorkerIpcError("worker_disconnected", "UI worker session context 不可用。 ");
|
|
497
|
+
}
|
|
498
|
+
if (!this.context.isIdle()) {
|
|
499
|
+
if (params.abortRunning !== true) {
|
|
500
|
+
throw new UiWorkerIpcError("worker_disconnected", "UI Pi turn 正在运行; 未经显式授权不执行 abort。 ");
|
|
501
|
+
}
|
|
502
|
+
this.context.abort();
|
|
503
|
+
await this.waitForIdle(60000);
|
|
504
|
+
}
|
|
505
|
+
const context = this.context;
|
|
506
|
+
setImmediate(() => context.shutdown());
|
|
507
|
+
return { accepted: true };
|
|
508
|
+
}
|
|
509
|
+
throw new UiWorkerIpcError("invalid_frame", "Gateway 调用了未知 UI worker 方法。 ");
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* 将微信 prompt 作为 Pi 原生 user message 注入当前 TUI session。
|
|
514
|
+
*
|
|
515
|
+
* @param params Gateway prompt 参数。
|
|
516
|
+
* @returns Pi agent settled 后的最终 assistant 文本。
|
|
517
|
+
*/
|
|
518
|
+
private async handlePrompt(params: Record<string, unknown>): Promise<unknown> {
|
|
519
|
+
if (!this.context || !this.pi || typeof params.message !== "string" || !params.message.trim()) {
|
|
520
|
+
throw new UiWorkerIpcError("invalid_frame", "Gateway prompt 参数缺少文本正文。 ");
|
|
521
|
+
}
|
|
522
|
+
if (this.pendingPrompt) {
|
|
523
|
+
throw new UiWorkerIpcError("worker_disconnected", "UI worker 已有一条尚未结算的 Gateway prompt。 ");
|
|
524
|
+
}
|
|
525
|
+
const pi = this.pi;
|
|
526
|
+
const agentWasIdle = this.context.isIdle();
|
|
527
|
+
this.latestAssistantText = "";
|
|
528
|
+
const response = new Promise<unknown>((resolveResponse, rejectResponse) => {
|
|
529
|
+
this.pendingPrompt = {
|
|
530
|
+
expectedText: params.message as string,
|
|
531
|
+
remoteTurnStarted: agentWasIdle,
|
|
532
|
+
resolve: resolveResponse,
|
|
533
|
+
reject: rejectResponse,
|
|
534
|
+
};
|
|
535
|
+
});
|
|
536
|
+
try {
|
|
537
|
+
pi.sendUserMessage(
|
|
538
|
+
params.message,
|
|
539
|
+
agentWasIdle ? undefined : { deliverAs: "followUp" },
|
|
540
|
+
);
|
|
541
|
+
} catch (error) {
|
|
542
|
+
const pending = this.pendingPrompt as PendingUiPrompt | undefined;
|
|
543
|
+
this.pendingPrompt = undefined;
|
|
544
|
+
pending?.reject(error instanceof Error ? error : new Error("pi.sendUserMessage failed"));
|
|
545
|
+
}
|
|
546
|
+
return response;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* 断线后以稳定 pipe endpoint 进行限速重连。
|
|
551
|
+
*
|
|
552
|
+
* @param delayMs 首次延迟。
|
|
553
|
+
* @returns 无返回值。
|
|
554
|
+
*/
|
|
555
|
+
private scheduleConnect(delayMs: number): void {
|
|
556
|
+
if (this.closing || this.client || this.connectionTask || this.reconnectTimer) {
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
if (!this.registration) {
|
|
560
|
+
this.lastDisconnectReason ??= "缺少 session registration, 等待 session 重新 attach";
|
|
561
|
+
this.notifyConnectionState("disconnected");
|
|
562
|
+
this.scheduleRecoveryCheck();
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
this.reconnectTimer = setTimeout(() => {
|
|
566
|
+
this.reconnectTimer = undefined;
|
|
567
|
+
this.connectionTask = this.connectOnce().finally(() => {
|
|
568
|
+
this.connectionTask = undefined;
|
|
569
|
+
if (!this.client && !this.closing) {
|
|
570
|
+
this.scheduleConnect(this.reconnectDelayMs);
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
void this.connectionTask;
|
|
574
|
+
}, delayMs);
|
|
575
|
+
this.reconnectTimer.unref();
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* 在缺少 registration 时定期重试恢复, 避免 footer 永久停在“连接中”。
|
|
580
|
+
*
|
|
581
|
+
* @returns 无返回值。
|
|
582
|
+
*/
|
|
583
|
+
private scheduleRecoveryCheck(): void {
|
|
584
|
+
if (this.closing || this.client || this.recoveryTimer) {
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
this.recoveryTimer = setTimeout(() => {
|
|
588
|
+
this.recoveryTimer = undefined;
|
|
589
|
+
void this.recoverRegistration();
|
|
590
|
+
}, RECOVERY_CHECK_INTERVAL_MS);
|
|
591
|
+
this.recoveryTimer.unref();
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* 尝试用当前 Pi context 重建 registration 并恢复 IPC 连接。
|
|
596
|
+
*
|
|
597
|
+
* @returns 无返回值。
|
|
598
|
+
*/
|
|
599
|
+
private async recoverRegistration(): Promise<void> {
|
|
600
|
+
if (this.closing || this.client) {
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
if (!this.registration && this.pi && this.context) {
|
|
604
|
+
try {
|
|
605
|
+
this.registration = await this.createRegistration(this.context, false);
|
|
606
|
+
} catch (error) {
|
|
607
|
+
this.lastDisconnectReason = error instanceof Error ? error.message : "重建 registration 失败";
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (this.registration) {
|
|
611
|
+
this.reconnectDelayMs = 500;
|
|
612
|
+
this.notifyConnectionState("connecting");
|
|
613
|
+
this.scheduleConnect(0);
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
this.notifyConnectionState("disconnected");
|
|
617
|
+
this.scheduleRecoveryCheck();
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* 建立一次 authenticated pipe connection。
|
|
622
|
+
*
|
|
623
|
+
* @returns 握手成功后开始 heartbeat。
|
|
624
|
+
*/
|
|
625
|
+
private async connectOnce(): Promise<void> {
|
|
626
|
+
const registration = this.registration;
|
|
627
|
+
if (!registration || this.closing) {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
try {
|
|
631
|
+
const managerToken = this.launchRecord
|
|
632
|
+
? undefined
|
|
633
|
+
: await readOrCreateUiWorkerAttachToken(this.managerDirectory);
|
|
634
|
+
const client = await UiWorkerIpcClient.connect(
|
|
635
|
+
this.launchRecord?.pipeEndpoint ?? createStableNamedPipe(this.managerDirectory),
|
|
636
|
+
this.launchRecord?.ticket ?? "",
|
|
637
|
+
registration,
|
|
638
|
+
{
|
|
639
|
+
...(managerToken ? { managerToken } : {}),
|
|
640
|
+
requestHandler: (method, params) => this.handleGatewayRequest(method, params),
|
|
641
|
+
},
|
|
642
|
+
);
|
|
643
|
+
if (
|
|
644
|
+
this.closing ||
|
|
645
|
+
!this.registration ||
|
|
646
|
+
this.registration.instanceId !== registration.instanceId ||
|
|
647
|
+
this.registration.sessionId !== registration.sessionId
|
|
648
|
+
) {
|
|
649
|
+
client.close();
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
this.client = client;
|
|
653
|
+
this.reconnectAttempts = 0;
|
|
654
|
+
this.lastConnectedAt = new Date().toISOString();
|
|
655
|
+
this.lastDisconnectReason = undefined;
|
|
656
|
+
this.notifyConnectionState("connected");
|
|
657
|
+
if (this.registration !== registration) {
|
|
658
|
+
client.notify("session_changed", { registration: this.registration });
|
|
659
|
+
}
|
|
660
|
+
this.reconnectDelayMs = 500;
|
|
661
|
+
client.on("close", () => {
|
|
662
|
+
if (this.client === client) {
|
|
663
|
+
this.client = undefined;
|
|
664
|
+
this.stopHeartbeat();
|
|
665
|
+
this.lastDisconnectReason = "IPC 连接已关闭";
|
|
666
|
+
this.notifyConnectionState("connecting");
|
|
667
|
+
this.scheduleConnect(this.reconnectDelayMs);
|
|
668
|
+
this.reconnectDelayMs = Math.min(this.reconnectDelayMs * 2, 15000);
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
client.on("transport_error", (error: Error) => {
|
|
672
|
+
if (this.client === client) {
|
|
673
|
+
this.client = undefined;
|
|
674
|
+
this.stopHeartbeat();
|
|
675
|
+
this.lastDisconnectReason = `IPC 传输错误: ${error.message}`;
|
|
676
|
+
this.notifyConnectionState("connecting");
|
|
677
|
+
this.scheduleConnect(this.reconnectDelayMs);
|
|
678
|
+
this.reconnectDelayMs = Math.min(this.reconnectDelayMs * 2, 15000);
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
this.startHeartbeat();
|
|
682
|
+
client.notify("heartbeat", { state: registration.state });
|
|
683
|
+
} catch (error) {
|
|
684
|
+
this.reconnectAttempts += 1;
|
|
685
|
+
this.lastDisconnectReason = error instanceof Error ? error.message : "UI worker IPC 连接失败";
|
|
686
|
+
this.notifyConnectionState("connecting");
|
|
687
|
+
this.scheduleConnect(this.reconnectDelayMs);
|
|
688
|
+
this.reconnectDelayMs = Math.min(this.reconnectDelayMs * 2, 15000);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* 定期向 Gateway 发送不含消息正文的 worker heartbeat。
|
|
694
|
+
*
|
|
695
|
+
* @returns 无返回值。
|
|
696
|
+
*/
|
|
697
|
+
private startHeartbeat(): void {
|
|
698
|
+
if (this.heartbeatTimer) {
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
this.heartbeatTimer = setInterval(() => {
|
|
702
|
+
if (this.client && this.registration) {
|
|
703
|
+
const state = this.context?.isIdle() ? "idle" : "running";
|
|
704
|
+
this.client.notify("heartbeat", { state });
|
|
705
|
+
}
|
|
706
|
+
}, 5000);
|
|
707
|
+
this.heartbeatTimer.unref();
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* 清除重连/心跳定时器并唤醒所有等待中的 idle waiter。
|
|
712
|
+
*
|
|
713
|
+
* @returns 无返回值。
|
|
714
|
+
*/
|
|
715
|
+
private clearTimers(): void {
|
|
716
|
+
if (this.reconnectTimer) {
|
|
717
|
+
clearTimeout(this.reconnectTimer);
|
|
718
|
+
this.reconnectTimer = undefined;
|
|
719
|
+
}
|
|
720
|
+
if (this.recoveryTimer) {
|
|
721
|
+
clearTimeout(this.recoveryTimer);
|
|
722
|
+
this.recoveryTimer = undefined;
|
|
723
|
+
}
|
|
724
|
+
this.stopHeartbeat();
|
|
725
|
+
for (const resolveIdle of this.idleWaiters) {
|
|
726
|
+
resolveIdle();
|
|
727
|
+
}
|
|
728
|
+
this.idleWaiters.clear();
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* 停止 heartbeat timer。
|
|
733
|
+
*
|
|
734
|
+
* @returns 无返回值。
|
|
735
|
+
*/
|
|
736
|
+
private stopHeartbeat(): void {
|
|
737
|
+
if (this.heartbeatTimer) {
|
|
738
|
+
clearInterval(this.heartbeatTimer);
|
|
739
|
+
this.heartbeatTimer = undefined;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* 将 UI session 名称和当前 agent state 写回 registration event。
|
|
745
|
+
*
|
|
746
|
+
* @param state worker 当前状态。
|
|
747
|
+
* @returns 无返回值。
|
|
748
|
+
*/
|
|
749
|
+
private sendState(state: "idle" | "running"): void {
|
|
750
|
+
if (this.client) {
|
|
751
|
+
this.client.notify("heartbeat", { state });
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* 等待当前 agent turn settled。
|
|
757
|
+
*
|
|
758
|
+
* @param timeoutMs 最大等待时长。
|
|
759
|
+
* @returns agent_settled event 到达时结束。
|
|
760
|
+
*/
|
|
761
|
+
private waitForIdle(timeoutMs: number): Promise<void> {
|
|
762
|
+
if (this.context?.isIdle()) {
|
|
763
|
+
return Promise.resolve();
|
|
764
|
+
}
|
|
765
|
+
return new Promise<void>((resolveIdle, rejectIdle) => {
|
|
766
|
+
const wake = (): void => {
|
|
767
|
+
clearTimeout(timer);
|
|
768
|
+
this.idleWaiters.delete(wake);
|
|
769
|
+
resolveIdle();
|
|
770
|
+
};
|
|
771
|
+
const timer = setTimeout(() => {
|
|
772
|
+
this.idleWaiters.delete(wake);
|
|
773
|
+
rejectIdle(new UiWorkerIpcError("request_timeout", "等待 UI Pi turn 结束超时。 "));
|
|
774
|
+
}, timeoutMs);
|
|
775
|
+
timer.unref();
|
|
776
|
+
this.idleWaiters.add(wake);
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* 判断当前 Pi extension host 是否已经为可见 TUI session 创建了 Hub bridge。
|
|
783
|
+
*
|
|
784
|
+
* @returns 当前进程持有可见 TUI bridge 时返回 true。
|
|
785
|
+
*/
|
|
786
|
+
export function isUiWorkerBridgeActive(): boolean {
|
|
787
|
+
return bridgeProcess.piWechatUiWorkerBridge !== undefined;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* 在 Pi 本机命令修改授权配置后, 重新尝试接入当前可见 session.
|
|
792
|
+
*
|
|
793
|
+
* @param pi 当前 Pi extension API.
|
|
794
|
+
* @param context 当前本机 session context.
|
|
795
|
+
* @returns session 已通过授权验证并开始注册时正常结束.
|
|
796
|
+
* @throws {UiWorkerIpcError} 当前进程尚未建立 UI worker bridge 时抛出.
|
|
797
|
+
*/
|
|
798
|
+
export async function attachCurrentUiWorkerSession(
|
|
799
|
+
pi: ExtensionAPI,
|
|
800
|
+
context: ExtensionContext,
|
|
801
|
+
): Promise<void> {
|
|
802
|
+
const bridge = bridgeProcess.piWechatUiWorkerBridge;
|
|
803
|
+
if (!bridge) {
|
|
804
|
+
throw new UiWorkerIpcError("worker_disconnected", "当前 Pi TUI 尚未建立 Hub session bridge, 请重新打开 session 后重试。");
|
|
805
|
+
}
|
|
806
|
+
await bridge.attach(pi, context);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* 安装保留 Pi 默认 footer 布局的 Hub 状态渲染器.
|
|
811
|
+
*
|
|
812
|
+
* @param context 当前 TUI session context.
|
|
813
|
+
* @param statusKey Hub footer 状态 key.
|
|
814
|
+
* @returns 无返回值.
|
|
815
|
+
*/
|
|
816
|
+
function installHubFooter(context: ExtensionContext, statusKey: string): void {
|
|
817
|
+
if (context.mode !== "tui") {
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
const sessionLike = {
|
|
821
|
+
get state() {
|
|
822
|
+
return {
|
|
823
|
+
model: context.model,
|
|
824
|
+
thinkingLevel: context.thinkingLevel ?? "off",
|
|
825
|
+
};
|
|
826
|
+
},
|
|
827
|
+
sessionManager: context.sessionManager,
|
|
828
|
+
getContextUsage: () => context.getContextUsage(),
|
|
829
|
+
modelRuntime: {
|
|
830
|
+
isUsingSubscription: (): boolean => false,
|
|
831
|
+
},
|
|
832
|
+
} as unknown as AgentSession;
|
|
833
|
+
|
|
834
|
+
context.ui.setFooter((_tui, theme, footerData) => {
|
|
835
|
+
const defaultFooter = new FooterComponent(sessionLike, footerData);
|
|
836
|
+
return {
|
|
837
|
+
render(width: number): string[] {
|
|
838
|
+
const lines = defaultFooter.render(width);
|
|
839
|
+
const statusText = footerData.getExtensionStatuses().get(statusKey);
|
|
840
|
+
const lastLine = lines.at(-1);
|
|
841
|
+
if (statusText && lastLine) {
|
|
842
|
+
const color = statusText.startsWith("🟢") ? "success" : "warning";
|
|
843
|
+
lines[lines.length - 1] = lastLine.replace(statusText, theme.fg(color, statusText));
|
|
844
|
+
}
|
|
845
|
+
return lines;
|
|
846
|
+
},
|
|
847
|
+
invalidate(): void {
|
|
848
|
+
defaultFooter.invalidate();
|
|
849
|
+
},
|
|
850
|
+
dispose(): void {
|
|
851
|
+
defaultFooter.dispose();
|
|
852
|
+
},
|
|
853
|
+
};
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* 注册所有可见 Pi TUI 的 Hub session owner bridge。
|
|
859
|
+
*
|
|
860
|
+
* @param pi Pi extension API。
|
|
861
|
+
* @param projectService 已登记项目路径校验服务。
|
|
862
|
+
* @param managerDirectory manager 私有数据目录。
|
|
863
|
+
* @returns 无返回值。
|
|
864
|
+
*/
|
|
865
|
+
export function registerUiWorkerBridgeHooks(
|
|
866
|
+
pi: ExtensionAPI,
|
|
867
|
+
projectService: ProjectService,
|
|
868
|
+
managerDirectory: string,
|
|
869
|
+
): void {
|
|
870
|
+
let bridge = bridgeProcess.piWechatUiWorkerBridge;
|
|
871
|
+
let statusContext: ExtensionContext | undefined;
|
|
872
|
+
const statusKey = "wechat-session-hub";
|
|
873
|
+
const updateStatus = (state: UiWorkerConnectionState): void => {
|
|
874
|
+
const ui = statusContext?.ui;
|
|
875
|
+
if (!ui) {
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
const text = state === "connected"
|
|
879
|
+
? "🟢 微信: 已接入"
|
|
880
|
+
: state === "connecting"
|
|
881
|
+
? "🟡 微信: 连接中"
|
|
882
|
+
: undefined;
|
|
883
|
+
ui.setStatus(statusKey, text);
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
pi.on("session_start", async (event, sessionContext) => {
|
|
887
|
+
if (sessionContext.mode !== "tui") {
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
if (bridge && typeof bridge.setStatusSink !== "function") {
|
|
891
|
+
if (bridgeProcess.piWechatUiWorkerBridge === bridge) {
|
|
892
|
+
delete bridgeProcess.piWechatUiWorkerBridge;
|
|
893
|
+
}
|
|
894
|
+
bridge = undefined;
|
|
895
|
+
}
|
|
896
|
+
// launch record 首次注册后会被消费, reload 时仅移除旧 UI 回调并保留进程级 bridge。
|
|
897
|
+
if (event.reason === "reload" && bridge) {
|
|
898
|
+
bridge.setStatusSink(undefined);
|
|
899
|
+
}
|
|
900
|
+
statusContext = sessionContext;
|
|
901
|
+
sessionContext.ui.setStatus(statusKey, undefined);
|
|
902
|
+
installHubFooter(sessionContext, statusKey);
|
|
903
|
+
const configuredManagerDirectory = process.env.PI_WECHAT_MANAGER_DIR;
|
|
904
|
+
if (configuredManagerDirectory && !samePath(configuredManagerDirectory, managerDirectory)) {
|
|
905
|
+
sessionContext.ui.notify("UI worker managerDirectory 与当前 Pi agent 配置不一致, 已拒绝注册。", "error");
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
if (!bridge) {
|
|
909
|
+
const launchId = process.env.PI_WECHAT_MANAGER_LAUNCH_ID;
|
|
910
|
+
try {
|
|
911
|
+
if (launchId) {
|
|
912
|
+
const launchRecord = await readUiWorkerLaunchRecord(managerDirectory, launchId);
|
|
913
|
+
const endpoint = process.env.PI_WECHAT_MANAGER_PIPE;
|
|
914
|
+
if (endpoint && endpoint !== launchRecord.pipeEndpoint) {
|
|
915
|
+
throw new UiWorkerIpcError("launch_record_invalid", "UI worker pipe endpoint 与 launch record 不一致。 ");
|
|
916
|
+
}
|
|
917
|
+
bridge = new UiWorkerExtensionBridge(launchRecord, projectService);
|
|
918
|
+
} else {
|
|
919
|
+
bridge = new UiWorkerExtensionBridge(undefined, projectService, managerDirectory);
|
|
920
|
+
}
|
|
921
|
+
bridgeProcess.piWechatUiWorkerBridge = bridge;
|
|
922
|
+
} catch (error) {
|
|
923
|
+
sessionContext.ui.notify(
|
|
924
|
+
error instanceof UiWorkerIpcError ? error.message : "UI worker launch record 验证失败。",
|
|
925
|
+
"error",
|
|
926
|
+
);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
bridge.setStatusSink(updateStatus);
|
|
931
|
+
try {
|
|
932
|
+
await bridge.attach(pi, sessionContext);
|
|
933
|
+
} catch (error) {
|
|
934
|
+
if (error instanceof UiWorkerIpcError && error.code === "project_not_registered") {
|
|
935
|
+
sessionContext.ui.notify(error.message, "warning");
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
sessionContext.ui.notify(
|
|
939
|
+
`无法准备当前 Pi TUI session registration; Hub route 未建立. ${error instanceof Error ? error.message : "原因未知."}`,
|
|
940
|
+
"error",
|
|
941
|
+
);
|
|
942
|
+
}
|
|
943
|
+
});
|
|
944
|
+
|
|
945
|
+
pi.on("session_before_switch", async (event, sessionContext) => {
|
|
946
|
+
if (!bridge) {
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
if (!event.targetSessionFile) {
|
|
950
|
+
if (event.reason === "resume") {
|
|
951
|
+
sessionContext.ui.notify("Gateway 无法确认目标 session, 已拒绝恢复以避免双 writer。", "warning");
|
|
952
|
+
return { cancel: true };
|
|
953
|
+
}
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
const allowed = await bridge.canSwitchSession(event.targetSessionFile);
|
|
957
|
+
if (!allowed) {
|
|
958
|
+
sessionContext.ui.notify(
|
|
959
|
+
"Gateway 检测到目标 session 仍由其他 Pi runtime 持有或 owner 状态不明, 为避免双 writer 已拒绝恢复。",
|
|
960
|
+
"warning",
|
|
961
|
+
);
|
|
962
|
+
return { cancel: true };
|
|
963
|
+
}
|
|
964
|
+
});
|
|
965
|
+
pi.on("session_info_changed", async (event, sessionContext) => {
|
|
966
|
+
await bridge?.onSessionInfoChanged(event, sessionContext);
|
|
967
|
+
});
|
|
968
|
+
pi.on("agent_start", () => bridge?.onAgentStart());
|
|
969
|
+
pi.on("message_end", (event) => bridge?.onMessageEnd(event));
|
|
970
|
+
pi.on("agent_settled", () => bridge?.onAgentSettled());
|
|
971
|
+
pi.on("session_shutdown", async (event) => {
|
|
972
|
+
if (!bridge) {
|
|
973
|
+
statusContext?.ui.setStatus(statusKey, undefined);
|
|
974
|
+
if (event.reason === "quit") {
|
|
975
|
+
statusContext = undefined;
|
|
976
|
+
}
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
await bridge.onSessionShutdown(event);
|
|
980
|
+
if (event.reason === "quit" && bridgeProcess.piWechatUiWorkerBridge === bridge) {
|
|
981
|
+
if (typeof bridge.setStatusSink === "function") {
|
|
982
|
+
bridge.setStatusSink(undefined);
|
|
983
|
+
}
|
|
984
|
+
delete bridgeProcess.piWechatUiWorkerBridge;
|
|
985
|
+
bridge = undefined;
|
|
986
|
+
statusContext?.ui.setStatus(statusKey, undefined);
|
|
987
|
+
statusContext = undefined;
|
|
988
|
+
}
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* 获取当前进程内 UI worker bridge 的诊断快照。
|
|
994
|
+
*
|
|
995
|
+
* @returns 当前 TUI 的连接诊断信息; 本进程未建立 bridge 时返回 undefined。
|
|
996
|
+
*/
|
|
997
|
+
export function getCurrentUiWorkerBridgeDiagnostics(): UiWorkerBridgeDiagnostics | undefined {
|
|
998
|
+
return bridgeProcess.piWechatUiWorkerBridge?.getDiagnostics();
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
/**
|
|
1002
|
+
* 从 Pi user message 中提取普通文本块。
|
|
1003
|
+
*
|
|
1004
|
+
* @param message Pi message。
|
|
1005
|
+
* @returns 拼接后的文本。
|
|
1006
|
+
*/
|
|
1007
|
+
function extractMessageText(message: unknown): string {
|
|
1008
|
+
if (!isRecord(message)) {
|
|
1009
|
+
return typeof message === "string" ? message : "";
|
|
1010
|
+
}
|
|
1011
|
+
if (typeof message.content === "string") {
|
|
1012
|
+
return message.content;
|
|
1013
|
+
}
|
|
1014
|
+
if (!Array.isArray(message.content)) {
|
|
1015
|
+
return "";
|
|
1016
|
+
}
|
|
1017
|
+
return message.content
|
|
1018
|
+
.filter((item) => isRecord(item) && item.type === "text" && typeof item.text === "string")
|
|
1019
|
+
.map((item) => (item as { text: string }).text)
|
|
1020
|
+
.join("");
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* 将 Pi message content 中的普通文本块拼接为可回传回复。
|
|
1025
|
+
*
|
|
1026
|
+
* @param message Pi agent message。
|
|
1027
|
+
* @returns 普通文本, 不包含 thinking 或 tool call 参数。
|
|
1028
|
+
*/
|
|
1029
|
+
function extractAssistantText(message: unknown): string {
|
|
1030
|
+
if (!isRecord(message) || message.role !== "assistant") {
|
|
1031
|
+
return "";
|
|
1032
|
+
}
|
|
1033
|
+
if (typeof message.content === "string") {
|
|
1034
|
+
return message.content;
|
|
1035
|
+
}
|
|
1036
|
+
if (!Array.isArray(message.content)) {
|
|
1037
|
+
return "";
|
|
1038
|
+
}
|
|
1039
|
+
return message.content
|
|
1040
|
+
.filter((item) => isRecord(item) && item.type === "text" && typeof item.text === "string")
|
|
1041
|
+
.map((item) => (item as { text: string }).text)
|
|
1042
|
+
.join("");
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* 判断未知值是否为非数组 object。
|
|
1047
|
+
*
|
|
1048
|
+
* @param value 待检查的值。
|
|
1049
|
+
* @returns 值可安全作为记录对象读取时返回 true。
|
|
1050
|
+
*/
|
|
1051
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
1052
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
/**
|
|
1056
|
+
* 比较两个项目 cwd 路径, Windows 上忽略大小写。
|
|
1057
|
+
*
|
|
1058
|
+
* @param left 路径一。
|
|
1059
|
+
* @param right 路径二。
|
|
1060
|
+
* @returns 路径相同时返回 true。
|
|
1061
|
+
*/
|
|
1062
|
+
function samePath(left: string, right: string): boolean {
|
|
1063
|
+
const resolvedLeft = normalizeComparablePath(left);
|
|
1064
|
+
const resolvedRight = normalizeComparablePath(right);
|
|
1065
|
+
return process.platform === "win32"
|
|
1066
|
+
? resolvedLeft.toLowerCase() === resolvedRight.toLowerCase()
|
|
1067
|
+
: resolvedLeft === resolvedRight;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* 将路径归一化为可比较形式, 尽量消解 Windows 8.3 短名与符号链接差异。
|
|
1072
|
+
*
|
|
1073
|
+
* GitHub Windows runner 的临时目录会是 `RUNNER~1`, 而 `realpath` 返回
|
|
1074
|
+
* `runneradmin`。两侧若一侧已规范化、另一侧未规范化, 纯字符串比较会
|
|
1075
|
+
* 误判为不同路径。这里尽力对两侧同时做 `realpathSync.native`; 任一侧
|
|
1076
|
+
* 无法解析(例如文件尚未创建)时回退到 `resolve` 结果。
|
|
1077
|
+
*
|
|
1078
|
+
* @param value 待归一化路径。
|
|
1079
|
+
* @returns 用于相等比较的路径字符串。
|
|
1080
|
+
*/
|
|
1081
|
+
function normalizeComparablePath(value: string): string {
|
|
1082
|
+
const resolved = resolve(value);
|
|
1083
|
+
if (process.platform !== "win32") {
|
|
1084
|
+
return resolved;
|
|
1085
|
+
}
|
|
1086
|
+
try {
|
|
1087
|
+
return realpathSync.native(resolved);
|
|
1088
|
+
} catch {
|
|
1089
|
+
return resolved;
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
|