dsh-notify-yimit 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +2 -2
- package/README.md +4 -2
- package/cordis.patch.yml +6 -6
- package/lib/client.js +1127 -789
- package/lib/index.js +973 -716
- package/lib/toast-host.ps1 +289 -330
- package/lib/typert.host.js +193 -157
- package/package.json +56 -50
package/lib/index.js
CHANGED
|
@@ -1,716 +1,973 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-notify-yimit-yimit 宿主插件。
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
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
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
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
|
-
|
|
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
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-notify-yimit-yimit 宿主插件。
|
|
3
|
+
* 优化点:内存LRU控制、队列头指针优化、浏览器探测缓存、宿主崩溃退避、全面多语言支持。
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
z
|
|
7
|
+
} from 'zod'
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import {
|
|
11
|
+
spawn
|
|
12
|
+
} from 'node:child_process'
|
|
13
|
+
import {
|
|
14
|
+
fileURLToPath
|
|
15
|
+
} from 'node:url'
|
|
16
|
+
import {
|
|
17
|
+
resolveDshHome
|
|
18
|
+
} from '@deepseek-ai/dsh-home-paths'
|
|
19
|
+
|
|
20
|
+
export const name = 'dsh-notify-yimit'
|
|
21
|
+
|
|
22
|
+
const KIND_KEYS = Object.freeze(['completed', 'error', 'running', 'approval', 'question'])
|
|
23
|
+
|
|
24
|
+
const DEFAULT_CONFIG = Object.freeze({
|
|
25
|
+
enabled: false,
|
|
26
|
+
mode: 'off',
|
|
27
|
+
maxVisible: 3,
|
|
28
|
+
durationMs: 8000,
|
|
29
|
+
browser: '',
|
|
30
|
+
bgColor: '#20272f',
|
|
31
|
+
textColor: '#e6edf3',
|
|
32
|
+
locale: 'en',
|
|
33
|
+
colors: {
|
|
34
|
+
completed: {
|
|
35
|
+
bg: '#1f5138',
|
|
36
|
+
fg: '#e6f4ec'
|
|
37
|
+
},
|
|
38
|
+
error: {
|
|
39
|
+
bg: '#5c1f24',
|
|
40
|
+
fg: '#fbe9ea'
|
|
41
|
+
},
|
|
42
|
+
running: {
|
|
43
|
+
bg: '#203a5c',
|
|
44
|
+
fg: '#e8f0fb'
|
|
45
|
+
},
|
|
46
|
+
approval: {
|
|
47
|
+
bg: '#4a3a12',
|
|
48
|
+
fg: '#f7efd8'
|
|
49
|
+
},
|
|
50
|
+
question: {
|
|
51
|
+
bg: '#3d2a55',
|
|
52
|
+
fg: '#f0e8fa'
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const ACTIVITY_LABELS = {
|
|
58
|
+
zh: {
|
|
59
|
+
start: '开始处理',
|
|
60
|
+
thinking: '正在思考…',
|
|
61
|
+
generating: '正在生成回复…',
|
|
62
|
+
executing: '正在执行',
|
|
63
|
+
continuing: '继续处理…',
|
|
64
|
+
processing: '正在处理…',
|
|
65
|
+
completed: '任务已完成',
|
|
66
|
+
errorPrefix: '任务出错:',
|
|
67
|
+
errorFallback: '任务出错',
|
|
68
|
+
approvalPrefix: '等待批准执行',
|
|
69
|
+
approvalFallback: '等待批准',
|
|
70
|
+
},
|
|
71
|
+
en: {
|
|
72
|
+
start: 'Started processing',
|
|
73
|
+
thinking: 'Thinking…',
|
|
74
|
+
generating: 'Generating response…',
|
|
75
|
+
executing: 'Executing',
|
|
76
|
+
continuing: 'Continuing…',
|
|
77
|
+
processing: 'Processing...',
|
|
78
|
+
completed: 'Task completed',
|
|
79
|
+
errorPrefix: 'Task failed: ',
|
|
80
|
+
errorFallback: 'Task failed',
|
|
81
|
+
approvalPrefix: 'Awaiting approval to execute',
|
|
82
|
+
approvalFallback: 'Awaiting approval',
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const TOAST_LABELS = {
|
|
87
|
+
zh: {
|
|
88
|
+
ignore: '忽略',
|
|
89
|
+
jump: '跳转会话',
|
|
90
|
+
unnamed: '(未命名会话)'
|
|
91
|
+
},
|
|
92
|
+
en: {
|
|
93
|
+
ignore: 'Ignore',
|
|
94
|
+
jump: 'Open',
|
|
95
|
+
unnamed: '(Unnamed session)'
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** 标题未生成时的占位文本(中/英):session/title 到达后原地替换。 */
|
|
100
|
+
const UNTITLED_NAMES = new Set([TOAST_LABELS.zh.unnamed, TOAST_LABELS.en.unnamed])
|
|
101
|
+
|
|
102
|
+
const HEX_RE = /^#[0-9a-fA-F]{3,8}$/
|
|
103
|
+
const isHex = (v) => typeof v === 'string' && HEX_RE.test(v)
|
|
104
|
+
|
|
105
|
+
function normalizeConfig(input) {
|
|
106
|
+
const src = input !== null && typeof input === 'object' && !Array.isArray(input) ? input : {}
|
|
107
|
+
const out = {
|
|
108
|
+
...DEFAULT_CONFIG
|
|
109
|
+
}
|
|
110
|
+
if (typeof src.enabled === 'boolean') out.enabled = src.enabled
|
|
111
|
+
if (src.mode === 'system' || src.mode === 'custom' || src.mode === 'off') out.mode = src.mode
|
|
112
|
+
if (typeof src.maxVisible === 'number' && Number.isFinite(src.maxVisible)) out.maxVisible = Math.max(1, Math.min(20,
|
|
113
|
+
Math.floor(src.maxVisible)))
|
|
114
|
+
if (typeof src.durationMs === 'number' && Number.isFinite(src.durationMs)) out.durationMs = Math.max(1000, Math.min(
|
|
115
|
+
300000, Math.floor(src.durationMs)))
|
|
116
|
+
if (typeof src.browser === 'string') out.browser = src.browser
|
|
117
|
+
if (isHex(src.bgColor)) out.bgColor = src.bgColor
|
|
118
|
+
if (isHex(src.textColor)) out.textColor = src.textColor
|
|
119
|
+
if (src.locale === 'zh' || src.locale === 'en') out.locale = src.locale
|
|
120
|
+
|
|
121
|
+
const srcColors = src.colors !== null && typeof src.colors === 'object' && !Array.isArray(src.colors) ? src.colors :
|
|
122
|
+
{}
|
|
123
|
+
out.colors = {}
|
|
124
|
+
for (const kind of KIND_KEYS) {
|
|
125
|
+
const entry = srcColors[kind]
|
|
126
|
+
const def = DEFAULT_CONFIG.colors[kind]
|
|
127
|
+
out.colors[kind] = {
|
|
128
|
+
bg: isHex(entry?.bg) ? entry.bg : def.bg,
|
|
129
|
+
fg: isHex(entry?.fg) ? entry.fg : def.fg,
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return out
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function migrateLegacyColors(input) {
|
|
136
|
+
if (input === null || typeof input !== 'object' || Array.isArray(input)) return input
|
|
137
|
+
if (input.colors !== null && typeof input.colors === 'object' && !Array.isArray(input.colors)) return input
|
|
138
|
+
if (!isHex(input.bgColor) && !isHex(input.textColor)) return input
|
|
139
|
+
const colors = {}
|
|
140
|
+
for (const kind of KIND_KEYS) colors[kind] = {
|
|
141
|
+
bg: input.bgColor,
|
|
142
|
+
fg: input.textColor
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
...input,
|
|
146
|
+
colors
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function mergeConfigPatch(base, patch) {
|
|
151
|
+
const out = {
|
|
152
|
+
...base,
|
|
153
|
+
...patch
|
|
154
|
+
}
|
|
155
|
+
if (patch !== null && typeof patch === 'object' && patch.colors !== null && typeof patch.colors === 'object' && !
|
|
156
|
+
Array.isArray(patch.colors)) {
|
|
157
|
+
const baseColors = base.colors !== null && typeof base.colors === 'object' ? base.colors : {}
|
|
158
|
+
const next = {}
|
|
159
|
+
for (const kind of KIND_KEYS) next[kind] = {
|
|
160
|
+
...(baseColors[kind] ?? {}),
|
|
161
|
+
...(patch.colors[kind] ?? {})
|
|
162
|
+
}
|
|
163
|
+
out.colors = next
|
|
164
|
+
}
|
|
165
|
+
return out
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function openConfig() {
|
|
169
|
+
const home = resolveDshHome()
|
|
170
|
+
const dir = path.join(home, 'storages', 'dsh-notify-yimit')
|
|
171
|
+
const file = path.join(dir, 'config.json')
|
|
172
|
+
let loaded = null
|
|
173
|
+
try {
|
|
174
|
+
loaded = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
175
|
+
} catch {
|
|
176
|
+
loaded = null
|
|
177
|
+
}
|
|
178
|
+
let config = normalizeConfig(migrateLegacyColors(loaded))
|
|
179
|
+
let timer = null
|
|
180
|
+
|
|
181
|
+
const persist = () => {
|
|
182
|
+
if (timer !== null) return
|
|
183
|
+
timer = setTimeout(() => {
|
|
184
|
+
timer = null
|
|
185
|
+
try {
|
|
186
|
+
fs.mkdirSync(dir, {
|
|
187
|
+
recursive: true
|
|
188
|
+
})
|
|
189
|
+
const tmp = `${file}.${process.pid}.tmp`
|
|
190
|
+
fs.writeFileSync(tmp, JSON.stringify(config, null, 2), 'utf8')
|
|
191
|
+
fs.renameSync(tmp, file)
|
|
192
|
+
} catch (error) {
|
|
193
|
+
console.warn(`[dsh-notify-yimit] Failed to persist config: ${String(error)}`)
|
|
194
|
+
}
|
|
195
|
+
}, 200)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
get: () => config,
|
|
200
|
+
set(patch) {
|
|
201
|
+
config = normalizeConfig(mergeConfigPatch(config, patch))
|
|
202
|
+
persist()
|
|
203
|
+
return config
|
|
204
|
+
},
|
|
205
|
+
close() {
|
|
206
|
+
if (timer !== null) {
|
|
207
|
+
clearTimeout(timer);
|
|
208
|
+
timer = null
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
fs.mkdirSync(dir, {
|
|
212
|
+
recursive: true
|
|
213
|
+
})
|
|
214
|
+
const tmp = `${file}.${process.pid}.tmp`
|
|
215
|
+
fs.writeFileSync(tmp, JSON.stringify(config, null, 2), 'utf8')
|
|
216
|
+
fs.renameSync(tmp, file)
|
|
217
|
+
} catch {
|
|
218
|
+
/* 静默 */ }
|
|
219
|
+
},
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const MAX_QUEUE = 500
|
|
224
|
+
const RUNNING_THROTTLE_MS = 400
|
|
225
|
+
const KIND_DEBOUNCE_MS = 2000
|
|
226
|
+
const TOAST_GAP = 12
|
|
227
|
+
const TEXT_MAX = 300
|
|
228
|
+
|
|
229
|
+
function clip(text) {
|
|
230
|
+
const s = String(text ?? '')
|
|
231
|
+
return s.length > TEXT_MAX ? `${s.slice(0, TEXT_MAX)}…` : s
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function titleOf(session) {
|
|
235
|
+
const events = session?.events
|
|
236
|
+
if (Array.isArray(events)) {
|
|
237
|
+
// 全量扫描:标题事件通常在会话早期(seq 小)生成,长会话/恢复会话里它可能在
|
|
238
|
+
// 最后 50 条之外——只扫尾部会漏掉,导致回退到占位/目录名(恢复会话不会重放
|
|
239
|
+
// 种子事件,event 流也补不上)。
|
|
240
|
+
for (let i = events.length - 1; i >= 0; i -= 1) {
|
|
241
|
+
const event = events[i]
|
|
242
|
+
if (event?.type === 'session/title' && typeof event.data?.title === 'string' && event.data.title.length > 0)
|
|
243
|
+
return event.data.title
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// 标题尚未生成(异步 LLM/摘录,首轮事件都先于 session/title 到达):
|
|
247
|
+
// 不回退到 cwd 目录名(工作区名),返回空由调用方按语言显示占位;
|
|
248
|
+
// session/title 事件到达后,已弹浮窗标题经 title: 命令原地更新。
|
|
249
|
+
return ''
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
let cachedBrowsers = null
|
|
253
|
+
|
|
254
|
+
function browserCandidates() {
|
|
255
|
+
const pf = process.env.ProgramFiles ?? 'C:\\Program Files'
|
|
256
|
+
const pfx86 = process.env['ProgramFiles(x86)'] ?? 'C:\\Program Files (x86)'
|
|
257
|
+
const local = process.env.LOCALAPPDATA ?? ''
|
|
258
|
+
return [{
|
|
259
|
+
id: 'edge',
|
|
260
|
+
name: 'Microsoft Edge',
|
|
261
|
+
paths: [`${pfx86}\\Microsoft\\Edge\\Application\\msedge.exe`,
|
|
262
|
+
`${pf}\\Microsoft\\Edge\\Application\\msedge.exe`
|
|
263
|
+
]
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
id: 'chrome',
|
|
267
|
+
name: 'Google Chrome',
|
|
268
|
+
paths: [`${pf}\\Google\\Chrome\\Application\\chrome.exe`,
|
|
269
|
+
`${pfx86}\\Google\\Chrome\\Application\\chrome.exe`, local ?
|
|
270
|
+
`${local}\\Google\\Chrome\\Application\\chrome.exe` : ''
|
|
271
|
+
]
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
id: 'firefox',
|
|
275
|
+
name: 'Mozilla Firefox',
|
|
276
|
+
paths: [`${pf}\\Mozilla Firefox\\firefox.exe`, `${pfx86}\\Mozilla Firefox\\firefox.exe`]
|
|
277
|
+
},
|
|
278
|
+
{
|
|
279
|
+
id: 'brave',
|
|
280
|
+
name: 'Brave',
|
|
281
|
+
paths: [`${pf}\\BraveSoftware\\Brave-Browser\\Application\\brave.exe`, local ?
|
|
282
|
+
`${local}\\BraveSoftware\\Brave-Browser\\Application\\brave.exe` : ''
|
|
283
|
+
]
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
id: 'opera',
|
|
287
|
+
name: 'Opera',
|
|
288
|
+
paths: [local ? `${local}\\Programs\\Opera\\opera.exe` : '', `${pf}\\Opera\\launcher.exe`]
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
id: 'vivaldi',
|
|
292
|
+
name: 'Vivaldi',
|
|
293
|
+
paths: [local ? `${local}\\Vivaldi\\Application\\vivaldi.exe` : '',
|
|
294
|
+
`${pf}\\Vivaldi\\Application\\vivaldi.exe`
|
|
295
|
+
]
|
|
296
|
+
},
|
|
297
|
+
{
|
|
298
|
+
id: '360se',
|
|
299
|
+
name: '360 Safe Browser',
|
|
300
|
+
paths: [`${pfx86}\\360\\360se6\\Application\\360se.exe`]
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
id: '360chrome',
|
|
304
|
+
name: 'Chrome Browser',
|
|
305
|
+
paths: [`${pfx86}\\360\\360Chrome\\Chrome\\Application\\360chrome.exe`]
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
id: 'qqbrowser',
|
|
309
|
+
name: 'QQ Browser',
|
|
310
|
+
paths: [`${pfx86}\\Tencent\\QQBrowser\\QQBrowser.exe`, `${pf}\\Tencent\\QQBrowser\\QQBrowser.exe`]
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
id: 'sogou',
|
|
314
|
+
name: 'Sogou Browser',
|
|
315
|
+
paths: [`${pfx86}\\SogouExplorer\\SogouExplorer.exe`]
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
id: 'liebao',
|
|
319
|
+
name: 'Cheetah Browser',
|
|
320
|
+
paths: [`${pfx86}\\liebao\\liebao.exe`]
|
|
321
|
+
},
|
|
322
|
+
]
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function detectBrowsers() {
|
|
326
|
+
if (cachedBrowsers !== null) return cachedBrowsers
|
|
327
|
+
const found = []
|
|
328
|
+
for (const candidate of browserCandidates()) {
|
|
329
|
+
const path = (candidate.paths ?? []).find((p) => typeof p === 'string' && p.length > 0 && fs.existsSync(p))
|
|
330
|
+
if (path !== undefined) found.push({
|
|
331
|
+
id: candidate.id,
|
|
332
|
+
name: candidate.name,
|
|
333
|
+
path
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
cachedBrowsers = found
|
|
337
|
+
return found
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function apply(ctx) {
|
|
341
|
+
const store = openConfig()
|
|
342
|
+
ctx.effect(() => () => store.close(), 'dsh-notify-yimit: config close')
|
|
343
|
+
|
|
344
|
+
const MAX_SESSIONS = 200
|
|
345
|
+
const states = new Map()
|
|
346
|
+
const stateOf = (sessionId) => {
|
|
347
|
+
let st = states.get(sessionId)
|
|
348
|
+
if (st === undefined) {
|
|
349
|
+
st = {
|
|
350
|
+
title: '',
|
|
351
|
+
status: 'idle',
|
|
352
|
+
lastRunningId: null,
|
|
353
|
+
lastRunningAt: 0,
|
|
354
|
+
lastRunningText: null,
|
|
355
|
+
waitingKind: null,
|
|
356
|
+
lastFired: {}
|
|
357
|
+
}
|
|
358
|
+
states.set(sessionId, st)
|
|
359
|
+
if (states.size > MAX_SESSIONS) states.delete(states.keys().next().value)
|
|
360
|
+
}
|
|
361
|
+
return st
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
let queue = []
|
|
365
|
+
let queueHead = 0
|
|
366
|
+
|
|
367
|
+
function trim() {
|
|
368
|
+
while (queue.length - queueHead > MAX_QUEUE) queueHead++
|
|
369
|
+
if (queueHead > MAX_QUEUE) {
|
|
370
|
+
queue = queue.slice(queueHead);
|
|
371
|
+
queueHead = 0
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const TOAST_HOST_SCRIPT = fileURLToPath(new URL('./toast-host.ps1',
|
|
376
|
+
import.meta.url))
|
|
377
|
+
const activeToasts = new Map()
|
|
378
|
+
const toastOrder = []
|
|
379
|
+
const pendingClose = new Set()
|
|
380
|
+
|
|
381
|
+
let globalWorkBottom = null
|
|
382
|
+
let toastSeq = 1
|
|
383
|
+
let host = null
|
|
384
|
+
let hostAlive = false
|
|
385
|
+
let hostRestartAttempts = 0
|
|
386
|
+
let hostRestartUntil = 0
|
|
387
|
+
|
|
388
|
+
function spawnHost() {
|
|
389
|
+
try {
|
|
390
|
+
const spawnFn = typeof globalThis.__DSH_NOTIFY_TEST_SPAWN__ === 'function' ? globalThis
|
|
391
|
+
.__DSH_NOTIFY_TEST_SPAWN__ : spawn
|
|
392
|
+
host = spawnFn('powershell', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', TOAST_HOST_SCRIPT], {
|
|
393
|
+
windowsHide: true,
|
|
394
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
395
|
+
})
|
|
396
|
+
} catch (error) {
|
|
397
|
+
console.warn(`[dsh-notify-yimit] Toast host spawn failed: ${String(error)}`)
|
|
398
|
+
host = null;
|
|
399
|
+
hostAlive = false;
|
|
400
|
+
return
|
|
401
|
+
}
|
|
402
|
+
hostAlive = true
|
|
403
|
+
host.stdin.on('error', (err) => {
|
|
404
|
+
if (process.env.DSH_DEBUG) console.warn(`[dsh-notify-yimit] stdin error: ${err}`)
|
|
405
|
+
})
|
|
406
|
+
host.stdout.on('error', () => {})
|
|
407
|
+
host.on('error', () => {
|
|
408
|
+
hostAlive = false
|
|
409
|
+
})
|
|
410
|
+
host.stdout.setEncoding('utf8')
|
|
411
|
+
|
|
412
|
+
let buf = ''
|
|
413
|
+
host.stdout.on('data', (d) => {
|
|
414
|
+
buf += String(d)
|
|
415
|
+
let nl
|
|
416
|
+
while ((nl = buf.indexOf('\n')) !== -1) {
|
|
417
|
+
const line = buf.slice(0, nl).trim()
|
|
418
|
+
buf = buf.slice(nl + 1)
|
|
419
|
+
if (line.length === 0) continue
|
|
420
|
+
let msg
|
|
421
|
+
try {
|
|
422
|
+
msg = JSON.parse(line)
|
|
423
|
+
} catch {
|
|
424
|
+
continue
|
|
425
|
+
}
|
|
426
|
+
if (msg === null || typeof msg !== 'object') continue
|
|
427
|
+
if (msg.type === 'pos' && typeof msg.key === 'string') {
|
|
428
|
+
const rec = activeToasts.get(msg.key)
|
|
429
|
+
if (rec !== undefined && rec.instance === msg.instance) {
|
|
430
|
+
setToastMeta(rec, {
|
|
431
|
+
left: msg.left,
|
|
432
|
+
top: msg.top,
|
|
433
|
+
height: msg.height,
|
|
434
|
+
hwnd: msg.hwnd,
|
|
435
|
+
workBottom: msg.workBottom
|
|
436
|
+
})
|
|
437
|
+
reflowToasts()
|
|
438
|
+
}
|
|
439
|
+
} else if (msg.type === 'exit' && typeof msg.key === 'string') {
|
|
440
|
+
const rec = activeToasts.get(msg.key)
|
|
441
|
+
if (rec !== undefined && rec.instance === msg.instance) {
|
|
442
|
+
pendingClose.delete(msg.key)
|
|
443
|
+
activeToasts.delete(msg.key)
|
|
444
|
+
const idx = toastOrder.indexOf(msg.key)
|
|
445
|
+
if (idx !== -1) toastOrder.splice(idx, 1)
|
|
446
|
+
reflowToasts()
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
host.on('exit', () => {
|
|
453
|
+
hostAlive = false
|
|
454
|
+
activeToasts.clear()
|
|
455
|
+
toastOrder.length = 0
|
|
456
|
+
pendingClose.clear()
|
|
457
|
+
})
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function sendCmd(payload) {
|
|
461
|
+
if (host === null || !hostAlive) return false
|
|
462
|
+
try {
|
|
463
|
+
host.stdin.write(`${JSON.stringify(payload)}\n`);
|
|
464
|
+
return true
|
|
465
|
+
} catch {
|
|
466
|
+
return false
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function ensureHost() {
|
|
471
|
+
if (host !== null && hostAlive) return true
|
|
472
|
+
if (Date.now() < hostRestartUntil) return false
|
|
473
|
+
spawnHost()
|
|
474
|
+
if (!hostAlive) {
|
|
475
|
+
hostRestartAttempts++
|
|
476
|
+
hostRestartUntil = Date.now() + Math.min(30000, 1000 * (2 ** hostRestartAttempts))
|
|
477
|
+
} else {
|
|
478
|
+
hostRestartAttempts = 0
|
|
479
|
+
}
|
|
480
|
+
return host !== null && hostAlive
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function dismissToast(key) {
|
|
484
|
+
if (pendingClose.has(key)) return
|
|
485
|
+
const rec = activeToasts.get(key)
|
|
486
|
+
if (rec === undefined) return
|
|
487
|
+
pendingClose.add(key)
|
|
488
|
+
sendCmd({
|
|
489
|
+
cmd: 'close',
|
|
490
|
+
key
|
|
491
|
+
})
|
|
492
|
+
setTimeout(() => {
|
|
493
|
+
if (activeToasts.get(key) === rec) {
|
|
494
|
+
sendCmd({
|
|
495
|
+
cmd: 'close',
|
|
496
|
+
key
|
|
497
|
+
})
|
|
498
|
+
pendingClose.delete(key)
|
|
499
|
+
activeToasts.delete(key)
|
|
500
|
+
const idx = toastOrder.indexOf(key)
|
|
501
|
+
if (idx !== -1) toastOrder.splice(idx, 1)
|
|
502
|
+
reflowToasts()
|
|
503
|
+
} else {
|
|
504
|
+
pendingClose.delete(key)
|
|
505
|
+
}
|
|
506
|
+
}, 1500).unref?.()
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function reflowToasts() {
|
|
510
|
+
if (globalWorkBottom === null) return
|
|
511
|
+
let anchor = globalWorkBottom - 20
|
|
512
|
+
for (let i = 0; i < toastOrder.length; i += 1) {
|
|
513
|
+
const rec = activeToasts.get(toastOrder[i])
|
|
514
|
+
if (rec === undefined) continue
|
|
515
|
+
const height = rec.meta?.height ?? 130
|
|
516
|
+
const newTop = Math.round(anchor - height)
|
|
517
|
+
if (rec.meta !== null && newTop !== rec.meta.top) {
|
|
518
|
+
rec.meta.top = newTop
|
|
519
|
+
sendCmd({
|
|
520
|
+
cmd: 'move',
|
|
521
|
+
key: rec.key,
|
|
522
|
+
top: newTop
|
|
523
|
+
})
|
|
524
|
+
}
|
|
525
|
+
anchor = newTop - TOAST_GAP
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function setToastMeta(rec, meta) {
|
|
530
|
+
rec.meta = meta
|
|
531
|
+
if (Number.isFinite(meta.workBottom)) globalWorkBottom = meta.workBottom
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
const baseUrlOf = () => {
|
|
535
|
+
const ws = ctx.get('webServer')
|
|
536
|
+
return `http://127.0.0.1:${ws !== null && ws !== undefined && typeof ws.port === 'number' ? ws.port : 3080}`
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function showDesktopToast(cfg, ev) {
|
|
540
|
+
const maxVisible = Math.max(1, Math.min(20, Math.floor(Number(cfg.maxVisible) || 3)))
|
|
541
|
+
const stickyKind = ev.kind === 'running' || ev.kind === 'approval' || ev.kind === 'question'
|
|
542
|
+
const key = stickyKind ? `${ev.kind}:${ev.sessionId}` : `${ev.kind}:${ev.sessionId}:${ev.at}`
|
|
543
|
+
|
|
544
|
+
let replaceIndex = -1
|
|
545
|
+
if (stickyKind) {
|
|
546
|
+
const prev = activeToasts.get(key)
|
|
547
|
+
if (prev !== undefined && !pendingClose.has(key)) {
|
|
548
|
+
sendCmd({
|
|
549
|
+
cmd: 'text',
|
|
550
|
+
key,
|
|
551
|
+
text: clip(ev.text)
|
|
552
|
+
})
|
|
553
|
+
return
|
|
554
|
+
}
|
|
555
|
+
if (prev !== undefined) {
|
|
556
|
+
replaceIndex = toastOrder.indexOf(key)
|
|
557
|
+
if (replaceIndex !== -1) toastOrder.splice(replaceIndex, 1)
|
|
558
|
+
activeToasts.delete(key)
|
|
559
|
+
pendingClose.delete(key)
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
let guard = 0
|
|
564
|
+
while (activeToasts.size - pendingClose.size >= maxVisible && guard++ < 64) {
|
|
565
|
+
let first = null
|
|
566
|
+
for (const k of activeToasts.keys()) {
|
|
567
|
+
if (pendingClose.has(k)) continue
|
|
568
|
+
if (stickyKind && !/^(running|approval|question):/.test(k)) {
|
|
569
|
+
first = k;
|
|
570
|
+
break
|
|
571
|
+
}
|
|
572
|
+
if (first === null) first = k
|
|
573
|
+
}
|
|
574
|
+
if (first === null) break
|
|
575
|
+
dismissToast(first)
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const colors = cfg.colors !== null && typeof cfg.colors === 'object' ? cfg.colors[ev.kind] : undefined
|
|
579
|
+
if (replaceIndex === -1) {
|
|
580
|
+
toastOrder.unshift(key)
|
|
581
|
+
} else {
|
|
582
|
+
toastOrder.splice(replaceIndex, 0, key)
|
|
583
|
+
}
|
|
584
|
+
const insertIndex = replaceIndex !== -1 ? replaceIndex : 0
|
|
585
|
+
let offsetY = 0
|
|
586
|
+
for (let i = 0; i < insertIndex; i += 1) {
|
|
587
|
+
const r = activeToasts.get(toastOrder[i])
|
|
588
|
+
offsetY += (r?.meta?.height ?? 130) + TOAST_GAP
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
const lang = cfg.locale === 'zh' ? 'zh' : 'en'
|
|
592
|
+
const l = TOAST_LABELS[lang]
|
|
593
|
+
|
|
594
|
+
const payload = {
|
|
595
|
+
cmd: 'show',
|
|
596
|
+
key,
|
|
597
|
+
instance: toastSeq++,
|
|
598
|
+
title: typeof ev.title === 'string' && ev.title.length > 0 ? ev.title : l.unnamed,
|
|
599
|
+
text: clip(ev.text),
|
|
600
|
+
bg: colors?.bg ?? cfg.bgColor ?? '#20272f',
|
|
601
|
+
fg: colors?.fg ?? cfg.textColor ?? '#e6edf3',
|
|
602
|
+
durationSec: Math.max(1, Math.round((Number(cfg.durationMs) || 8000) / 1000)),
|
|
603
|
+
sticky: stickyKind,
|
|
604
|
+
offsetY,
|
|
605
|
+
sessionId: String(ev.sessionId),
|
|
606
|
+
baseUrl: baseUrlOf(),
|
|
607
|
+
winTitle: `dsh-notify-yimit-${key}`,
|
|
608
|
+
ignoreLabel: l.ignore,
|
|
609
|
+
jumpLabel: l.jump,
|
|
610
|
+
unnamedLabel: l.unnamed,
|
|
611
|
+
}
|
|
612
|
+
if (typeof cfg.browser === 'string' && cfg.browser.length > 0) payload.browserPath = cfg.browser
|
|
613
|
+
|
|
614
|
+
if (!ensureHost() || !sendCmd(payload)) {
|
|
615
|
+
const entry = {
|
|
616
|
+
id: String(nextId++),
|
|
617
|
+
sessionId: String(ev.sessionId),
|
|
618
|
+
title: typeof ev.title === 'string' && ev.title.length > 0 ? ev.title : l.unnamed,
|
|
619
|
+
kind: ev.kind,
|
|
620
|
+
text: clip(ev.text),
|
|
621
|
+
at: ev.at
|
|
622
|
+
}
|
|
623
|
+
queue.push(entry)
|
|
624
|
+
trim()
|
|
625
|
+
return
|
|
626
|
+
}
|
|
627
|
+
activeToasts.set(key, {
|
|
628
|
+
key,
|
|
629
|
+
instance: payload.instance,
|
|
630
|
+
meta: null,
|
|
631
|
+
offsetY,
|
|
632
|
+
sessionId: String(ev.sessionId)
|
|
633
|
+
})
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function closeToast(kind, sessionId) {
|
|
637
|
+
dismissToast(`${kind}:${sessionId}`)
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/** 会话标题到达后原地更新已弹浮窗的标题(宿主 title: 命令,不重弹、不闪烁)。 */
|
|
641
|
+
function updateToastTitle(sessionId, title) {
|
|
642
|
+
if (typeof title !== 'string' || title.length === 0) return
|
|
643
|
+
for (const rec of activeToasts.values()) {
|
|
644
|
+
if (rec.sessionId === sessionId) {
|
|
645
|
+
sendCmd({ cmd: 'title', key: rec.key, title })
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
ctx.effect(() => () => {
|
|
651
|
+
if (host !== null) {
|
|
652
|
+
try {
|
|
653
|
+
sendCmd({
|
|
654
|
+
cmd: 'shutdown'
|
|
655
|
+
})
|
|
656
|
+
} catch {
|
|
657
|
+
/* 忽略 */ }
|
|
658
|
+
try {
|
|
659
|
+
host.kill()
|
|
660
|
+
} catch {
|
|
661
|
+
/* 已退出 */ }
|
|
662
|
+
}
|
|
663
|
+
activeToasts.clear()
|
|
664
|
+
toastOrder.length = 0
|
|
665
|
+
pendingClose.clear()
|
|
666
|
+
}, 'dsh-notify-yimit: toast cleanup')
|
|
667
|
+
|
|
668
|
+
spawnHost()
|
|
669
|
+
let nextId = 1
|
|
670
|
+
|
|
671
|
+
const push = (sessionId, kind, text) => {
|
|
672
|
+
const st = states.get(sessionId)
|
|
673
|
+
if (st === undefined) return
|
|
674
|
+
const at = Date.now()
|
|
675
|
+
const cfg = store.get()
|
|
676
|
+
const desktop = cfg.enabled === true && cfg.mode === 'custom'
|
|
677
|
+
const lang = cfg.locale === 'zh' ? 'zh' : 'en'
|
|
678
|
+
const l = TOAST_LABELS[lang]
|
|
679
|
+
const entryTitle = typeof st.title === 'string' && st.title.length > 0 ? st.title : l.unnamed
|
|
680
|
+
|
|
681
|
+
if (kind === 'running') {
|
|
682
|
+
if (st.lastRunningAt !== 0 && at - st.lastRunningAt < RUNNING_THROTTLE_MS && st.lastRunningId !==
|
|
683
|
+
null) {
|
|
684
|
+
if (desktop) {
|
|
685
|
+
if (st.lastRunningText !== clip(text)) {
|
|
686
|
+
st.lastRunningText = clip(text)
|
|
687
|
+
showDesktopToast(cfg, {
|
|
688
|
+
sessionId,
|
|
689
|
+
title: st.title,
|
|
690
|
+
kind,
|
|
691
|
+
text: clip(text),
|
|
692
|
+
at
|
|
693
|
+
})
|
|
694
|
+
}
|
|
695
|
+
return
|
|
696
|
+
}
|
|
697
|
+
for (let i = queueHead; i < queue.length; i++) {
|
|
698
|
+
if (queue[i].id === st.lastRunningId) {
|
|
699
|
+
queue[i].text = clip(text);
|
|
700
|
+
queue[i].at = at;
|
|
701
|
+
break
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
return
|
|
705
|
+
}
|
|
706
|
+
st.lastRunningAt = at
|
|
707
|
+
st.lastRunningText = clip(text)
|
|
708
|
+
if (desktop) {
|
|
709
|
+
showDesktopToast(cfg, {
|
|
710
|
+
sessionId,
|
|
711
|
+
title: st.title,
|
|
712
|
+
kind,
|
|
713
|
+
text: clip(text),
|
|
714
|
+
at
|
|
715
|
+
})
|
|
716
|
+
st.lastRunningId = `desktop:${at}`
|
|
717
|
+
return
|
|
718
|
+
}
|
|
719
|
+
const entry = {
|
|
720
|
+
id: String(nextId++),
|
|
721
|
+
sessionId,
|
|
722
|
+
title: entryTitle,
|
|
723
|
+
kind,
|
|
724
|
+
text: clip(text),
|
|
725
|
+
activity: clip(text),
|
|
726
|
+
at
|
|
727
|
+
}
|
|
728
|
+
st.lastRunningId = entry.id
|
|
729
|
+
queue.push(entry)
|
|
730
|
+
trim()
|
|
731
|
+
return
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
if (st.lastFired === undefined) st.lastFired = {}
|
|
735
|
+
const isSticky = kind === 'approval' || kind === 'question'
|
|
736
|
+
if (!isSticky && at - (st.lastFired[kind] ?? 0) < KIND_DEBOUNCE_MS) return
|
|
737
|
+
st.lastFired[kind] = at
|
|
738
|
+
|
|
739
|
+
if (desktop) {
|
|
740
|
+
showDesktopToast(cfg, {
|
|
741
|
+
sessionId,
|
|
742
|
+
title: st.title,
|
|
743
|
+
kind,
|
|
744
|
+
text: clip(text),
|
|
745
|
+
at
|
|
746
|
+
})
|
|
747
|
+
return
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
let stale = -1
|
|
751
|
+
for (let i = queueHead; i < queue.length; i++) {
|
|
752
|
+
if (queue[i].sessionId === sessionId && queue[i].kind === kind) {
|
|
753
|
+
stale = i;
|
|
754
|
+
break
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
if (stale !== -1) queue.splice(stale, 1)
|
|
758
|
+
|
|
759
|
+
const entry = {
|
|
760
|
+
id: String(nextId++),
|
|
761
|
+
sessionId,
|
|
762
|
+
title: entryTitle,
|
|
763
|
+
kind,
|
|
764
|
+
text: clip(text),
|
|
765
|
+
at
|
|
766
|
+
}
|
|
767
|
+
queue.push(entry)
|
|
768
|
+
trim()
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
ctx.on('session/event', (session, event) => {
|
|
772
|
+
if (session === undefined || session === null || event === undefined || event === null) return
|
|
773
|
+
const id = session.id
|
|
774
|
+
const st = stateOf(id)
|
|
775
|
+
if (st.title.length === 0) st.title = titleOf(session)
|
|
776
|
+
|
|
777
|
+
const lang = store.get().locale === 'zh' ? 'zh' : 'en'
|
|
778
|
+
const L = ACTIVITY_LABELS[lang]
|
|
779
|
+
|
|
780
|
+
switch (event.type) {
|
|
781
|
+
case 'session/title': {
|
|
782
|
+
if (typeof event.data?.title === 'string' && event.data.title.length > 0) {
|
|
783
|
+
st.title = event.data.title
|
|
784
|
+
for (let i = queueHead; i < queue.length; i++) {
|
|
785
|
+
if (queue[i].sessionId === id && (queue[i].title.length === 0 || UNTITLED_NAMES.has(queue[i].title)))
|
|
786
|
+
queue[i].title = st.title
|
|
787
|
+
}
|
|
788
|
+
// 已弹浮窗(运行中/待审批/待回答等)标题原地更新,不再一直顶着占位/旧标题。
|
|
789
|
+
updateToastTitle(id, st.title)
|
|
790
|
+
}
|
|
791
|
+
return
|
|
792
|
+
}
|
|
793
|
+
case 'turn/start': {
|
|
794
|
+
st.status = 'running'
|
|
795
|
+
st.activity = L.start
|
|
796
|
+
push(id, 'running', st.activity)
|
|
797
|
+
return
|
|
798
|
+
}
|
|
799
|
+
case 'step/start': {
|
|
800
|
+
st.status = 'running'
|
|
801
|
+
st.activity = L.thinking
|
|
802
|
+
push(id, 'running', st.activity)
|
|
803
|
+
return
|
|
804
|
+
}
|
|
805
|
+
case 'assistant/chunk': {
|
|
806
|
+
const chunk = event.data?.chunk
|
|
807
|
+
if (chunk === null || typeof chunk !== 'object') return
|
|
808
|
+
if (chunk.type === 'reasoning' && typeof chunk.text === 'string' && chunk.text.length > 0) {
|
|
809
|
+
st.activity = L.thinking
|
|
810
|
+
push(id, 'running', st.activity)
|
|
811
|
+
} else if (chunk.type === 'text' && typeof chunk.text === 'string' && chunk.text.length > 0) {
|
|
812
|
+
st.activity = L.generating
|
|
813
|
+
push(id, 'running', st.activity)
|
|
814
|
+
}
|
|
815
|
+
return
|
|
816
|
+
}
|
|
817
|
+
case 'tool/call': {
|
|
818
|
+
const data = event.data
|
|
819
|
+
if (data === null || typeof data !== 'object') return
|
|
820
|
+
const toolName = typeof data.name === 'string' ? data.name : ''
|
|
821
|
+
st.activity = `${L.executing} ${toolName}`
|
|
822
|
+
push(id, 'running', st.activity)
|
|
823
|
+
if (toolName === 'ask_user_question' && typeof data.arguments === 'string') {
|
|
824
|
+
try {
|
|
825
|
+
const args = JSON.parse(data.arguments)
|
|
826
|
+
const questions = Array.isArray(args?.questions) ? args.questions : []
|
|
827
|
+
const first = questions.find((q) => q !== null && typeof q === 'object' && typeof q
|
|
828
|
+
.question === 'string')
|
|
829
|
+
if (first !== undefined) {
|
|
830
|
+
st.status = 'waiting'
|
|
831
|
+
st.waitingKind = 'question'
|
|
832
|
+
push(id, 'question', first.question)
|
|
833
|
+
}
|
|
834
|
+
} catch {
|
|
835
|
+
/* arguments 非 JSON */ }
|
|
836
|
+
}
|
|
837
|
+
return
|
|
838
|
+
}
|
|
839
|
+
case 'tool/result': {
|
|
840
|
+
if (st.waitingKind === 'question') {
|
|
841
|
+
closeToast('question', id)
|
|
842
|
+
st.waitingKind = null
|
|
843
|
+
st.status = 'running'
|
|
844
|
+
st.activity = L.continuing
|
|
845
|
+
push(id, 'running', st.activity)
|
|
846
|
+
} else if (st.status === 'waiting') {
|
|
847
|
+
st.status = 'running'
|
|
848
|
+
st.activity = L.continuing
|
|
849
|
+
push(id, 'running', st.activity)
|
|
850
|
+
}
|
|
851
|
+
return
|
|
852
|
+
}
|
|
853
|
+
case 'approval/decided': {
|
|
854
|
+
closeToast('approval', id)
|
|
855
|
+
if (st.waitingKind === 'approval') st.waitingKind = null
|
|
856
|
+
return
|
|
857
|
+
}
|
|
858
|
+
case 'turn/end': {
|
|
859
|
+
const reason = event.data?.reason
|
|
860
|
+
const kind = reason !== null && typeof reason === 'object' ? reason.kind : undefined
|
|
861
|
+
st.status = 'idle'
|
|
862
|
+
st.activity = ''
|
|
863
|
+
st.lastRunningAt = 0
|
|
864
|
+
st.lastRunningId = null
|
|
865
|
+
st.lastRunningText = null
|
|
866
|
+
st.waitingKind = null
|
|
867
|
+
closeToast('running', id)
|
|
868
|
+
if (kind === 'completed') {
|
|
869
|
+
push(id, 'completed', L.completed)
|
|
870
|
+
} else if (kind === 'error') {
|
|
871
|
+
const message = reason !== null && typeof reason === 'object' && reason.error !== null &&
|
|
872
|
+
typeof reason.error === 'object' ? (typeof reason.error.message === 'string' ? reason
|
|
873
|
+
.error.message : '') : ''
|
|
874
|
+
push(id, 'error', message.length > 0 ? `${L.errorPrefix}${message}` : L.errorFallback)
|
|
875
|
+
}
|
|
876
|
+
return
|
|
877
|
+
}
|
|
878
|
+
default:
|
|
879
|
+
return
|
|
880
|
+
}
|
|
881
|
+
})
|
|
882
|
+
|
|
883
|
+
ctx.on('agent/status', ({
|
|
884
|
+
agent,
|
|
885
|
+
status
|
|
886
|
+
}) => {
|
|
887
|
+
if (agent === undefined || agent === null) return
|
|
888
|
+
const sessionId = agent.session?.id ?? agent.id
|
|
889
|
+
if (typeof sessionId !== 'string' || sessionId.length === 0) return
|
|
890
|
+
const st = stateOf(sessionId)
|
|
891
|
+
const lang = store.get().locale === 'zh' ? 'zh' : 'en'
|
|
892
|
+
const L = ACTIVITY_LABELS[lang]
|
|
893
|
+
if (status === 'running' && st.status !== 'running') {
|
|
894
|
+
st.status = 'running'
|
|
895
|
+
st.activity = L.processing
|
|
896
|
+
push(sessionId, 'running', st.activity)
|
|
897
|
+
} else if (status === 'idle' && st.status === 'running') {
|
|
898
|
+
st.status = 'idle'
|
|
899
|
+
st.lastRunningAt = 0
|
|
900
|
+
st.lastRunningId = null
|
|
901
|
+
}
|
|
902
|
+
})
|
|
903
|
+
|
|
904
|
+
ctx.on('approval/request', (req, next) => {
|
|
905
|
+
try {
|
|
906
|
+
const sid = req?.agent?.session?.id
|
|
907
|
+
const sessionId = (typeof sid === 'string' && sid.length > 0) ? sid : req?.agent?.id
|
|
908
|
+
if (typeof sessionId === 'string' && sessionId.length > 0 && req !== null && typeof req ===
|
|
909
|
+
'object') {
|
|
910
|
+
const st = stateOf(sessionId)
|
|
911
|
+
st.status = 'waiting'
|
|
912
|
+
st.waitingKind = 'approval'
|
|
913
|
+
const toolName = typeof req.toolName === 'string' ? req.toolName : ''
|
|
914
|
+
const reason = typeof req.reason === 'string' && req.reason.length > 0 ? req.reason : ''
|
|
915
|
+
|
|
916
|
+
const lang = store.get().locale === 'zh' ? 'zh' : 'en'
|
|
917
|
+
const L = ACTIVITY_LABELS[lang]
|
|
918
|
+
push(sessionId, 'approval', reason.length > 0 ? reason : (toolName.length > 0 ?
|
|
919
|
+
`${L.approvalPrefix} ${toolName}` : L.approvalFallback))
|
|
920
|
+
}
|
|
921
|
+
} catch {
|
|
922
|
+
/* 观察失败不影响审批链路 */ }
|
|
923
|
+
return next()
|
|
924
|
+
})
|
|
925
|
+
|
|
926
|
+
const service = {
|
|
927
|
+
getState() {
|
|
928
|
+
return {
|
|
929
|
+
config: store.get(),
|
|
930
|
+
events: queue.slice(queueHead).map((item) => ({
|
|
931
|
+
...item
|
|
932
|
+
}))
|
|
933
|
+
}
|
|
934
|
+
},
|
|
935
|
+
updateConfig(patch) {
|
|
936
|
+
return store.set(patch)
|
|
937
|
+
},
|
|
938
|
+
ackEvents(ids) {
|
|
939
|
+
if (Array.isArray(ids)) {
|
|
940
|
+
const acked = new Set(ids.map((x) => String(x)))
|
|
941
|
+
for (let i = queue.length - 1; i >= queueHead; i -= 1) {
|
|
942
|
+
if (acked.has(queue[i].id)) queue.splice(i, 1)
|
|
943
|
+
}
|
|
944
|
+
if (queueHead > MAX_QUEUE / 2) {
|
|
945
|
+
queue = queue.slice(queueHead);
|
|
946
|
+
queueHead = 0
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
return {
|
|
950
|
+
ok: true
|
|
951
|
+
}
|
|
952
|
+
},
|
|
953
|
+
listBrowsers() {
|
|
954
|
+
return {
|
|
955
|
+
browsers: detectBrowsers()
|
|
956
|
+
}
|
|
957
|
+
},
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
Object.defineProperty(service, 'typertRemote', {
|
|
961
|
+
configurable: false,
|
|
962
|
+
enumerable: false,
|
|
963
|
+
writable: false,
|
|
964
|
+
value: {
|
|
965
|
+
service,
|
|
966
|
+
serviceKey: 'notify',
|
|
967
|
+
namespace: 'notify'
|
|
968
|
+
},
|
|
969
|
+
})
|
|
970
|
+
|
|
971
|
+
ctx.provide('notify', service)
|
|
972
|
+
console.log('[dsh-notify-yimit] Loaded (disabled by default; please enable in Settings -> Notifications)')
|
|
973
|
+
}
|