@weitc/dfy-awesome-sidebar 1.0.1 → 1.0.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/client.js +1 -1
- package/index.js +2049 -2014
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,2015 +1,2050 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Host half of @weitc/dfy-awesome-sidebar (v2)。
|
|
3
|
-
*
|
|
4
|
-
* 只做两件事,都围绕"工作区/会话在侧栏里的位置":
|
|
5
|
-
* 1. 状态:唯一置顶工作区 + 多个星标工作区(按星标先后)+ 星标会话,持久化到
|
|
6
|
-
* %DSH_HOME%\workspace-pin.json(原子写,UTF-8 无 BOM);
|
|
7
|
-
* 2. 排序:把注册表顺序收敛成「置顶(唯一) > 星标(按星标先后) > 其余(保持当前相对顺序)」,
|
|
8
|
-
* 一律经 `workspaceRegistry.insertBefore()`(DSH 官方、串行、原子写回 workspace.json)。
|
|
9
|
-
*
|
|
10
|
-
* 另外挂一个 `domain/changed` 监听:任何来源(包括用户手动拖拽)改动了工作区顺序,
|
|
11
|
-
* 只要违反上面这条不变式,就立刻重排回去——这就是置顶/星标项的"拖拽回弹"。
|
|
12
|
-
*/
|
|
13
|
-
import { spawn } from 'node:child_process'
|
|
14
|
-
import { copyFile, mkdir, readdir, readFile, rename, stat, unlink, writeFile, appendFile } from 'node:fs/promises'
|
|
15
|
-
import { existsSync, mkdirSync, appendFileSync } from 'node:fs'
|
|
16
|
-
import { homedir } from 'node:os'
|
|
17
|
-
import { basename, dirname, join } from 'node:path'
|
|
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
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
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
|
-
const
|
|
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
|
-
out
|
|
144
|
-
return out
|
|
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
|
-
settings.bgMode =
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
settings.
|
|
185
|
-
|
|
186
|
-
settings.
|
|
187
|
-
//
|
|
188
|
-
settings.
|
|
189
|
-
|
|
190
|
-
settings.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (Number.isFinite(Number(raw.
|
|
207
|
-
settings.
|
|
208
|
-
}
|
|
209
|
-
//
|
|
210
|
-
if (raw.
|
|
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
|
-
return
|
|
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
|
-
if (
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
state
|
|
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
|
-
return
|
|
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
|
-
const
|
|
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
|
-
function
|
|
402
|
-
return
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
const
|
|
416
|
-
const
|
|
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
|
-
error
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
error
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
error
|
|
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
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
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
|
-
if (starred)
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
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
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
}
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
*
|
|
877
|
-
*
|
|
878
|
-
*
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
error
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
}
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
const
|
|
923
|
-
await
|
|
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
|
-
if (
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
//
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
out
|
|
981
|
-
}
|
|
982
|
-
const
|
|
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
|
-
return
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
const
|
|
1059
|
-
if (!
|
|
1060
|
-
const
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
const
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
new
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
}
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
*
|
|
1162
|
-
*
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
*
|
|
1225
|
-
*
|
|
1226
|
-
*
|
|
1227
|
-
* -
|
|
1228
|
-
*
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
}
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
*
|
|
1274
|
-
*
|
|
1275
|
-
*
|
|
1276
|
-
*
|
|
1277
|
-
*
|
|
1278
|
-
*
|
|
1279
|
-
*
|
|
1280
|
-
*
|
|
1281
|
-
*
|
|
1282
|
-
*
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
const
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
}
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
const
|
|
1374
|
-
const
|
|
1375
|
-
const
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
baseline = { balance:
|
|
1402
|
-
baselineSource = '
|
|
1403
|
-
}
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
} else {
|
|
1422
|
-
// v0.22.
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
}
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
//
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
const
|
|
1502
|
-
const
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
res.
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
res.
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
res.
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
res.
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
if (req.method
|
|
1681
|
-
res
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
res.
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
'
|
|
1702
|
-
'
|
|
1703
|
-
'
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
res.
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
error
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
const
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
res.
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
res
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
res.
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
if (
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
const
|
|
1880
|
-
sendJson(res, 200,
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
}
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
}
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Host half of @weitc/dfy-awesome-sidebar (v2)。
|
|
3
|
+
*
|
|
4
|
+
* 只做两件事,都围绕"工作区/会话在侧栏里的位置":
|
|
5
|
+
* 1. 状态:唯一置顶工作区 + 多个星标工作区(按星标先后)+ 星标会话,持久化到
|
|
6
|
+
* %DSH_HOME%\workspace-pin.json(原子写,UTF-8 无 BOM);
|
|
7
|
+
* 2. 排序:把注册表顺序收敛成「置顶(唯一) > 星标(按星标先后) > 其余(保持当前相对顺序)」,
|
|
8
|
+
* 一律经 `workspaceRegistry.insertBefore()`(DSH 官方、串行、原子写回 workspace.json)。
|
|
9
|
+
*
|
|
10
|
+
* 另外挂一个 `domain/changed` 监听:任何来源(包括用户手动拖拽)改动了工作区顺序,
|
|
11
|
+
* 只要违反上面这条不变式,就立刻重排回去——这就是置顶/星标项的"拖拽回弹"。
|
|
12
|
+
*/
|
|
13
|
+
import { spawn } from 'node:child_process'
|
|
14
|
+
import { copyFile, mkdir, readdir, readFile, rename, stat, unlink, writeFile, appendFile } from 'node:fs/promises'
|
|
15
|
+
import { existsSync, mkdirSync, appendFileSync } from 'node:fs'
|
|
16
|
+
import { homedir } from 'node:os'
|
|
17
|
+
import { basename, dirname, join } from 'node:path'
|
|
18
|
+
import { createRequire } from 'node:module'
|
|
19
|
+
/** 打包时写死的兜底版本(v0.22.480):npm 安装时 import.meta.url / require.resolve 都可能拿不到 package.json,
|
|
20
|
+
* 这一级保证 /version 永远不会返回 unknown(客户端版本自检依赖它)。 */
|
|
21
|
+
const BUNDLE_VERSION = '1.0.2'
|
|
22
|
+
|
|
23
|
+
export const name = 'workspace-pin'
|
|
24
|
+
|
|
25
|
+
/** 只依赖官方服务:HTTP 路由表 + 工作区注册表;domain/changed 是 Cordis 事件。 */
|
|
26
|
+
export const inject = ['webServer', 'workspaceRegistry', 'deepseekAccount']
|
|
27
|
+
/**
|
|
28
|
+
* T-20261130(lead 指正):启动自检 —— 代码里用到的每个 ctx 服务都必须在 inject 里声明。
|
|
29
|
+
* Cordis 的 proxy 访问未声明服务会抛 "cannot get property X without inject",
|
|
30
|
+
* 之前这个错误被 try/catch 兜底吞掉,表现为"删了还在、要重启"。
|
|
31
|
+
*/
|
|
32
|
+
const REQUIRED_CTX_SERVICES = ['webServer', 'workspaceRegistry', 'deepseekAccount']
|
|
33
|
+
function assertInjectedServices() {
|
|
34
|
+
const missing = []
|
|
35
|
+
for (const name of REQUIRED_CTX_SERVICES) {
|
|
36
|
+
if (!inject.includes(name)) missing.push(name + '(未声明)')
|
|
37
|
+
}
|
|
38
|
+
if (missing.length > 0) {
|
|
39
|
+
console.error('[workspace-pin] inject 声明缺失:' + missing.join(', '))
|
|
40
|
+
}
|
|
41
|
+
return missing
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const ROUTE_STATE = '/workspace-pin/state'
|
|
45
|
+
const ROUTE_PIN = '/workspace-pin/pin'
|
|
46
|
+
const ROUTE_STAR = '/workspace-pin/star'
|
|
47
|
+
const ROUTE_ORDER = '/workspace-pin/order'
|
|
48
|
+
const ROUTE_TOGGLE = '/workspace-pin/toggle' // v1 兼容
|
|
49
|
+
const ROUTE_SESSION_PIN = '/workspace-pin/session-pin'
|
|
50
|
+
const ROUTE_DEBUG = '/workspace-pin/debug'
|
|
51
|
+
const ROUTE_VERSION = '/workspace-pin/version'
|
|
52
|
+
/** BUG-6 观察用:每次工作区置顶/取消追加一行,便于定位偶发"写入没发生"。 */
|
|
53
|
+
const ROUTE_SETTING = '/workspace-pin/setting'
|
|
54
|
+
const ROUTE_ASSET = '/workspace-pin/asset'
|
|
55
|
+
/** T-20261127:插件包内自带的默认背景图(不走用户素材目录)。 */
|
|
56
|
+
const ROUTE_DEFAULT_BG = '/workspace-pin/default-bg'
|
|
57
|
+
const ROUTE_ACCOUNT = '/workspace-pin/account'
|
|
58
|
+
const ROUTE_ACTIVITY = '/workspace-pin/activity'
|
|
59
|
+
const MAX_BODY_BYTES = 64 * 1024
|
|
60
|
+
/** 素材(裁切后的背景图 / 头像)走这条通道,单独放宽;其它写请求仍是 64KB。 */
|
|
61
|
+
const MAX_ASSET_BYTES = 6 * 1024 * 1024
|
|
62
|
+
const STATE_VERSION = 4
|
|
63
|
+
|
|
64
|
+
/** 素材目录名(放在 DSH home 下):图片一律落盘成文件,settings 里只存路径。 */
|
|
65
|
+
const ASSET_DIR_NAME = 'workspace-pin-assets'
|
|
66
|
+
/** 只认我们自己写出来的名字,天然挡掉路径穿越与任意文件读取。 */
|
|
67
|
+
const ASSET_NAME = /^(sidebar|ws|se|profile)-[A-Za-z0-9_-]{1,80}\.(png|jpg)$/
|
|
68
|
+
const ASSET_MIME = { png: 'image/png', jpg: 'image/jpeg' }
|
|
69
|
+
/** 头像裁切输出边长(px);背景图按侧栏真实比例裁,宽度上限见 ASSET_MAX_WIDTH。 */
|
|
70
|
+
const AVATAR_SIZE = 128
|
|
71
|
+
const ASSET_MAX_WIDTH = 720
|
|
72
|
+
const DATA_URL = /^data:image\/(png|jpeg);base64,([A-Za-z0-9+/=]+)$/
|
|
73
|
+
/** 余额流水:**我们自己写**(见 sampleBalanceOnce),默认路径 DATA_DIR\\balance-log.jsonl。 */
|
|
74
|
+
/** 可移植根目录(T-20261127):所有"我们会写文件"的地方都落在 DATA_DIR 下,
|
|
75
|
+
* 不再依赖任何人的绝对路径。可用 WP_DATA_DIR 覆盖。 */
|
|
76
|
+
const DATA_DIR = process.env.WP_DATA_DIR || join(process.env.DSH_HOME || join(homedir(), '.dsh'), 'workspace-pin')
|
|
77
|
+
const DEBUG_FLAG = join(DATA_DIR, 'debug-menu.flag')
|
|
78
|
+
const PIN_LOG = join(DATA_DIR, 'pin-actions.log')
|
|
79
|
+
const DEBUG_OUT = join(DATA_DIR, 'menu-dump.html')
|
|
80
|
+
const UPLOAD_TRACE = join(DATA_DIR, 'upload-trace.md')
|
|
81
|
+
/** 余额流水:**只作为兜底**——首选直接问 DSH 账号服务(跨机器可用)。
|
|
82
|
+
* 账本路径全部走环境变量或 DATA_DIR,**包内不出现任何本机绝对路径**。
|
|
83
|
+
* ① 环境变量 WP_BALANCE_LOG(测试/换源用)
|
|
84
|
+
* ② DATA_DIR\balance-log.jsonl(我们自己可能写的那份) */
|
|
85
|
+
const LEGACY_BALANCE_LOG = join(DATA_DIR, 'balance-log.jsonl')
|
|
86
|
+
const BALANCE_CACHE_MS = 60 * 1000
|
|
87
|
+
|
|
88
|
+
/** 可配置项与默认值;写入前逐项校验。 */
|
|
89
|
+
const SETTINGS_DEFAULTS = {
|
|
90
|
+
nativeSessionPin: 'plugin',
|
|
91
|
+
workspaceStarColor: '#E5484D',
|
|
92
|
+
sessionStarColor: '#E8B93B',
|
|
93
|
+
sessionTitleChars: 7,
|
|
94
|
+
theme: 'metal',
|
|
95
|
+
themeEnabled: true, // T-20261202:是否套用我们的样式主题;false = 侧栏回 DSH 原生外观
|
|
96
|
+
// —— v0.20 新增 ——
|
|
97
|
+
settingsTab: 1, // 设置面板停留在第几个 Tab(1|2|3)
|
|
98
|
+
areaOpacity: 50, // 会话区背景不透明度(0–100);工作区标题栏由它 +20 推出
|
|
99
|
+
bgMode: 'default', // 'default' 包内默认图 | 'custom' 用户上传 | 'none' 无背景(T-20261127)
|
|
100
|
+
sidebarBackground: '', // 侧栏铺底图的绝对路径,''=无
|
|
101
|
+
showTokenBalance: false, // 底部账号区是否显示余额 / 今日 token
|
|
102
|
+
showBalanceTime: true, // v0.22.134:余额后面是否带 @HH:mm 采样时间(与 showTokenBalance 联动)
|
|
103
|
+
costBaseline: null, // { balance, at, day }:今日消费的当日基线(快速路径;日志优先)
|
|
104
|
+
dailyLimit: 0, // 每日消费上限(元);0 = 不限制
|
|
105
|
+
limitAlert: true, // 消费限额告警开关(≥90% 触发)
|
|
106
|
+
balanceAlert: true, // 余额告急告警开关
|
|
107
|
+
balanceAlertThreshold: 20, // 余额告急阈值(元)
|
|
108
|
+
paused: false, // 暂停:告警触发时置 true,**只能用户手动解除**
|
|
109
|
+
pausedReason: '', // 暂停原因(横幅显示)
|
|
110
|
+
pausedBroadcast: '', // 上次广播结果:sent / failed / disabled / busy(只记状态)
|
|
111
|
+
inactiveHours: 24, // 工作区「全部长期无响应」的判定阈值(小时)
|
|
112
|
+
useGlowAlert: true, // 光效提醒总开关(跑马灯 / 浅绿 / 三态背光);头像开启时强制生效
|
|
113
|
+
pausedTempReleaseAt: 0, // T-05 ⑥:「临时解除」时刻(ms 时间戳);0=未临时解除
|
|
114
|
+
userProfile: { nickname: '', avatar: '' }, // T-05 ⑪:用户资料(头像存文件名,不塞 base64)
|
|
115
|
+
avatar: { enabled: false, workspace: {}, session: {} }, // 头像总开关 + 两份 id→路径映射
|
|
116
|
+
}
|
|
117
|
+
const NATIVE_PIN_MODES = ['plugin', 'native']
|
|
118
|
+
/** 五套视觉主题;unknown 值一律拒绝,读盘时回落到 metal。 */
|
|
119
|
+
const THEMES = ['metal', 'glass', 'neon', 'minimal', 'ocean', 'oldday', 'pop', 'vaporwave']
|
|
120
|
+
const HEX_COLOR = /^#[0-9a-fA-F]{6}$/
|
|
121
|
+
const TABS = [1, 2, 3]
|
|
122
|
+
|
|
123
|
+
/** 默认设置的深拷贝:avatar 是嵌套对象,浅拷贝会让多个状态共享同一个 map。 */
|
|
124
|
+
function defaultSettings() {
|
|
125
|
+
return {
|
|
126
|
+
...SETTINGS_DEFAULTS,
|
|
127
|
+
avatar: { enabled: false, workspace: {}, session: {} },
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function normalizeAvatarMap(raw) {
|
|
132
|
+
const out = {}
|
|
133
|
+
if (!raw || typeof raw !== 'object') return out
|
|
134
|
+
for (const [id, value] of Object.entries(raw)) {
|
|
135
|
+
if (typeof id !== 'string' || id === '') continue
|
|
136
|
+
if (typeof value !== 'string' || !isAssetPath(value)) continue
|
|
137
|
+
out[id] = value
|
|
138
|
+
}
|
|
139
|
+
return out
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function normalizeAvatar(raw) {
|
|
143
|
+
const out = { enabled: false, workspace: {}, session: {} }
|
|
144
|
+
if (!raw || typeof raw !== 'object') return out
|
|
145
|
+
out.enabled = raw.enabled === true
|
|
146
|
+
out.workspace = normalizeAvatarMap(raw.workspace)
|
|
147
|
+
out.session = normalizeAvatarMap(raw.session)
|
|
148
|
+
return out
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function normalizeSettings(raw) {
|
|
152
|
+
const settings = defaultSettings()
|
|
153
|
+
if (raw && typeof raw === 'object') {
|
|
154
|
+
if (NATIVE_PIN_MODES.includes(raw.nativeSessionPin)) settings.nativeSessionPin = raw.nativeSessionPin
|
|
155
|
+
if (typeof raw.workspaceStarColor === 'string' && HEX_COLOR.test(raw.workspaceStarColor)) settings.workspaceStarColor = raw.workspaceStarColor
|
|
156
|
+
if (typeof raw.sessionStarColor === 'string' && HEX_COLOR.test(raw.sessionStarColor)) settings.sessionStarColor = raw.sessionStarColor
|
|
157
|
+
if (Number.isInteger(raw.sessionTitleChars)) {
|
|
158
|
+
const chars = raw.sessionTitleChars
|
|
159
|
+
// 0 = 不限制;2–12 = 截断字数;13+ 视为不限制(兼容旧版写下的 12/13)
|
|
160
|
+
if (chars === 0 || (chars >= 2 && chars <= 12)) settings.sessionTitleChars = chars
|
|
161
|
+
else if (chars >= 13) settings.sessionTitleChars = 0
|
|
162
|
+
}
|
|
163
|
+
if (THEMES.includes(raw.theme)) settings.theme = raw.theme
|
|
164
|
+
settings.themeEnabled = raw.themeEnabled !== false
|
|
165
|
+
// —— v0.20 ——
|
|
166
|
+
if (TABS.includes(raw.settingsTab)) settings.settingsTab = raw.settingsTab
|
|
167
|
+
// —— v0.20.7:areaOpacity(语义=会话区);旧 workspaceOpacity 一次性迁移过来 ——
|
|
168
|
+
// 迁移策略:新键优先;只有新键缺失时才吃旧值,写回时旧键自然消失(writeState 只写
|
|
169
|
+
// normalizeSettings 的结果),所以"迁移 + 删旧键"一次完成。
|
|
170
|
+
const areaRaw = Number.isInteger(raw.areaOpacity) ? raw.areaOpacity : (Number.isInteger(raw.workspaceOpacity) ? raw.workspaceOpacity : undefined)
|
|
171
|
+
if (areaRaw !== undefined && areaRaw >= 0 && areaRaw <= 100) settings.areaOpacity = areaRaw
|
|
172
|
+
// T-20261127(CommanderT:默认背景 + 返回默认背景按钮):三种状态 default | custom | none。
|
|
173
|
+
// 迁移:老数据没有 bgMode 时 —— 有自定义图 → 'custom'(绝不让老用户的图被换掉);没有 → 'none'(保持原样)。
|
|
174
|
+
if (raw.bgMode === 'default' || raw.bgMode === 'custom' || raw.bgMode === 'none') {
|
|
175
|
+
settings.bgMode = raw.bgMode
|
|
176
|
+
} else if (typeof raw.sidebarBackground === 'string' && raw.sidebarBackground !== '') {
|
|
177
|
+
settings.bgMode = 'custom'
|
|
178
|
+
} else {
|
|
179
|
+
settings.bgMode = 'none'
|
|
180
|
+
}
|
|
181
|
+
if (typeof raw.sidebarBackground === 'string' && (raw.sidebarBackground === '' || isAssetPath(raw.sidebarBackground))) {
|
|
182
|
+
settings.sidebarBackground = raw.sidebarBackground
|
|
183
|
+
}
|
|
184
|
+
settings.showTokenBalance = raw.showTokenBalance === true
|
|
185
|
+
// v0.22.134:缺省 true(老状态文件没有该键时保持显示)
|
|
186
|
+
settings.showBalanceTime = raw.showBalanceTime !== false
|
|
187
|
+
// 暂停状态:只认 true;原因截断保存
|
|
188
|
+
settings.paused = raw.paused === true
|
|
189
|
+
settings.pausedReason = typeof raw.pausedReason === 'string' ? raw.pausedReason.slice(0, 120) : ''
|
|
190
|
+
settings.pausedBroadcast = typeof raw.pausedBroadcast === 'string' ? raw.pausedBroadcast.slice(0, 20) : ''
|
|
191
|
+
// 光效提醒:只认 false 才关;头像强制在客户端算,这里永远存用户原值
|
|
192
|
+
settings.useGlowAlert = raw.useGlowAlert !== false
|
|
193
|
+
const tra = Number(raw.pausedTempReleaseAt)
|
|
194
|
+
settings.pausedTempReleaseAt = Number.isFinite(tra) && tra > 0 ? tra : 0
|
|
195
|
+
if (raw.userProfile && typeof raw.userProfile === 'object') {
|
|
196
|
+
settings.userProfile = {
|
|
197
|
+
nickname: typeof raw.userProfile.nickname === 'string' ? raw.userProfile.nickname.slice(0, 12) : '',
|
|
198
|
+
avatar: typeof raw.userProfile.avatar === 'string' ? raw.userProfile.avatar.slice(0, 200) : '',
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const ih = Number(raw.inactiveHours)
|
|
202
|
+
settings.inactiveHours = Number.isFinite(ih) && ih > 0 ? Math.min(24 * 365, ih) : 24
|
|
203
|
+
// 告警开关 + 余额阈值
|
|
204
|
+
settings.limitAlert = raw.limitAlert !== false
|
|
205
|
+
settings.balanceAlert = raw.balanceAlert !== false
|
|
206
|
+
if (Number.isFinite(Number(raw.balanceAlertThreshold)) && Number(raw.balanceAlertThreshold) >= 0) {
|
|
207
|
+
settings.balanceAlertThreshold = Math.round(Number(raw.balanceAlertThreshold) * 100) / 100
|
|
208
|
+
}
|
|
209
|
+
// 每日限额:非负数字,最多 2 位小数;0 = 不限
|
|
210
|
+
if (Number.isFinite(Number(raw.dailyLimit)) && Number(raw.dailyLimit) >= 0) {
|
|
211
|
+
settings.dailyLimit = Math.round(Number(raw.dailyLimit) * 100) / 100
|
|
212
|
+
}
|
|
213
|
+
// 今日消费基线:{ balance:number, at:ISO字符串, day:YYYY-MM-DD }
|
|
214
|
+
if (raw.costBaseline && typeof raw.costBaseline === 'object') {
|
|
215
|
+
const bal = Number(raw.costBaseline.balance)
|
|
216
|
+
const at = raw.costBaseline.at
|
|
217
|
+
const day = raw.costBaseline.day
|
|
218
|
+
if (Number.isFinite(bal) && typeof at === 'string' && at !== '' && typeof day === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(day)) {
|
|
219
|
+
settings.costBaseline = { balance: bal, at, day }
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
settings.avatar = normalizeAvatar(raw.avatar)
|
|
223
|
+
}
|
|
224
|
+
return settings
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** DSH home:状态文件与素材都放这里,避免跟着端口 / 浏览器 origin 跑。 */
|
|
228
|
+
function dshHome() {
|
|
229
|
+
return process.env.DSH_HOME || join(homedir(), '.dsh')
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** 状态文件路径。 */
|
|
233
|
+
function statePath() {
|
|
234
|
+
return join(dshHome(), 'workspace-pin.json')
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** 素材目录:图片落盘在这里,settings 里只存路径。 */
|
|
238
|
+
function assetDir() {
|
|
239
|
+
return join(dshHome(), ASSET_DIR_NAME)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** 只接受「素材目录里的、由本插件命名的」文件,其它一律当无效值丢弃。 */
|
|
243
|
+
function isAssetPath(value) {
|
|
244
|
+
if (typeof value !== 'string' || value === '') return false
|
|
245
|
+
const name = basename(value)
|
|
246
|
+
if (!ASSET_NAME.test(name)) return false
|
|
247
|
+
try {
|
|
248
|
+
return dirname(value) === assetDir()
|
|
249
|
+
} catch {
|
|
250
|
+
return false
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function uniqueIds(value) {
|
|
255
|
+
const out = []
|
|
256
|
+
const seen = new Set()
|
|
257
|
+
for (const raw of Array.isArray(value) ? value : []) {
|
|
258
|
+
if (typeof raw !== 'string' || raw === '' || seen.has(raw)) continue
|
|
259
|
+
seen.add(raw)
|
|
260
|
+
out.push(raw)
|
|
261
|
+
}
|
|
262
|
+
return out
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function emptyState() {
|
|
266
|
+
return {
|
|
267
|
+
pinnedWorkspaceId: undefined,
|
|
268
|
+
pinnedPrev: undefined,
|
|
269
|
+
starredWorkspaces: [],
|
|
270
|
+
starredSessions: [],
|
|
271
|
+
starPrev: {},
|
|
272
|
+
pinnedSessionByWorkspace: {},
|
|
273
|
+
settings: defaultSettings(),
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 把落盘内容规范成 v3 形状,并平滑迁移 v1/v2:
|
|
279
|
+
* - v1 `{ pinned: [ids] }`:取第一个作为唯一置顶;
|
|
280
|
+
* - v2 `{ records: [{ id, prev, pinned }] }`:取第一条 pinned 记录,其余置顶丢弃(v2 允许多个,v3 只留一个)。
|
|
281
|
+
*/
|
|
282
|
+
function normalizeState(parsed) {
|
|
283
|
+
const state = emptyState()
|
|
284
|
+
if (!parsed || typeof parsed !== 'object') return state
|
|
285
|
+
|
|
286
|
+
if (Array.isArray(parsed.pinned) || Array.isArray(parsed.records)) {
|
|
287
|
+
const legacyPinned = Array.isArray(parsed.pinned)
|
|
288
|
+
? parsed.pinned.filter(id => typeof id === 'string' && id !== '')
|
|
289
|
+
: parsed.records
|
|
290
|
+
.filter(record => record && record.pinned === true && typeof record.id === 'string' && record.id !== '')
|
|
291
|
+
.map(record => record.id)
|
|
292
|
+
if (legacyPinned.length > 0) state.pinnedWorkspaceId = legacyPinned[0]
|
|
293
|
+
const legacyPrev = Array.isArray(parsed.records)
|
|
294
|
+
? parsed.records.find(record => record && record.id === state.pinnedWorkspaceId)?.prev
|
|
295
|
+
: undefined
|
|
296
|
+
if (typeof legacyPrev === 'string' && legacyPrev !== '') state.pinnedPrev = legacyPrev
|
|
297
|
+
return state
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (typeof parsed.pinnedWorkspaceId === 'string' && parsed.pinnedWorkspaceId !== '') {
|
|
301
|
+
state.pinnedWorkspaceId = parsed.pinnedWorkspaceId
|
|
302
|
+
}
|
|
303
|
+
if (typeof parsed.pinnedPrev === 'string' && parsed.pinnedPrev !== '') state.pinnedPrev = parsed.pinnedPrev
|
|
304
|
+
state.starredWorkspaces = uniqueIds(parsed.starredWorkspaces)
|
|
305
|
+
state.starredSessions = uniqueIds(parsed.starredSessions)
|
|
306
|
+
if (parsed.pinnedSessionByWorkspace && typeof parsed.pinnedSessionByWorkspace === 'object') {
|
|
307
|
+
for (const [workspaceId, sessionId] of Object.entries(parsed.pinnedSessionByWorkspace)) {
|
|
308
|
+
if (typeof sessionId === 'string' && sessionId !== '') state.pinnedSessionByWorkspace[workspaceId] = sessionId
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (parsed.starPrev && typeof parsed.starPrev === 'object') {
|
|
312
|
+
for (const [key, value] of Object.entries(parsed.starPrev)) {
|
|
313
|
+
if (typeof value === 'string' && value !== '' && value !== key) state.starPrev[key] = value
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// v3 及更早没有 settings:缺省即默认值。
|
|
317
|
+
state.settings = normalizeSettings(parsed.settings)
|
|
318
|
+
// 注意:置顶项允许同时留在星标集合里(置顶优先级由 layout 处理)。
|
|
319
|
+
// 这里不能把 pinnedWorkspaceId 从 star 集合里剔掉,否则"取消置顶后回星标区"会丢记忆。
|
|
320
|
+
return state
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** 读状态;文件缺失或损坏时退化为空状态,绝不阻断 DSH。 */
|
|
324
|
+
async function readState() {
|
|
325
|
+
const file = statePath()
|
|
326
|
+
try {
|
|
327
|
+
return normalizeState(JSON.parse(await readFile(file, 'utf8')))
|
|
328
|
+
} catch (error) {
|
|
329
|
+
if (error && error.code === 'ENOENT') return emptyState()
|
|
330
|
+
console.warn(`[workspace-pin] 状态文件不可用(${file}):${error?.message ?? error}`)
|
|
331
|
+
return emptyState()
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** 原子写:先写同目录临时文件再 rename,避免半截 JSON;UTF-8 无 BOM。
|
|
336
|
+
* Windows 上杀软/索引器可能短暂占用目标文件,rename 会偶发 EPERM/EBUSY;
|
|
337
|
+
* 这里做三件事:同进程内串行化写、临时文件/rename 带退避重试、失败时清理残留 temp。 */
|
|
338
|
+
let stateWriteChain = Promise.resolve()
|
|
339
|
+
const RETRYABLE_CODES = new Set(['EPERM', 'EACCES', 'EBUSY', 'ENOTEMPTY', 'EEXIST'])
|
|
340
|
+
async function withRetry(fn, attempts = 6, baseDelay = 10) {
|
|
341
|
+
let lastError = null
|
|
342
|
+
for (let i = 0; i < attempts; i += 1) {
|
|
343
|
+
try {
|
|
344
|
+
return await fn()
|
|
345
|
+
} catch (error) {
|
|
346
|
+
lastError = error
|
|
347
|
+
if (!error || !RETRYABLE_CODES.has(error.code)) throw error
|
|
348
|
+
if (i < attempts - 1) await new Promise(resolve => setTimeout(resolve, baseDelay * (i + 1)))
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
throw lastError
|
|
352
|
+
}
|
|
353
|
+
async function writeState(state) {
|
|
354
|
+
const run = stateWriteChain.then(() => writeStateOnce(state), () => writeStateOnce(state))
|
|
355
|
+
stateWriteChain = run.then(() => undefined, () => undefined)
|
|
356
|
+
return run
|
|
357
|
+
}
|
|
358
|
+
async function writeStateOnce(state) {
|
|
359
|
+
const file = statePath()
|
|
360
|
+
await mkdir(dirname(file), { recursive: true })
|
|
361
|
+
// 唯一化:同一个 pid 内并发写若共用固定 temp 名,第二个 rename 会 ENOENT(实测 paused 连写踩过)
|
|
362
|
+
const temporary = `${file}.${process.pid}.${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}.tmp`
|
|
363
|
+
const body = `${JSON.stringify({
|
|
364
|
+
version: STATE_VERSION,
|
|
365
|
+
pinnedWorkspaceId: state.pinnedWorkspaceId ?? null,
|
|
366
|
+
pinnedPrev: state.pinnedPrev ?? null,
|
|
367
|
+
starredWorkspaces: state.starredWorkspaces,
|
|
368
|
+
starredSessions: state.starredSessions,
|
|
369
|
+
starPrev: state.starPrev,
|
|
370
|
+
pinnedSessionByWorkspace: state.pinnedSessionByWorkspace,
|
|
371
|
+
settings: normalizeSettings(state.settings),
|
|
372
|
+
updatedAt: new Date().toISOString(),
|
|
373
|
+
}, null, 2)}\n`
|
|
374
|
+
try {
|
|
375
|
+
await withRetry(() => writeFile(temporary, body, 'utf8'))
|
|
376
|
+
// 写之前把上一版留成 .prev:万一写出空状态,能一条命令找回。
|
|
377
|
+
try {
|
|
378
|
+
await copyFile(file, `${file}.prev`)
|
|
379
|
+
} catch (error) {
|
|
380
|
+
if (!error || error.code !== 'ENOENT') console.warn(`[workspace-pin] 备份上一版失败:${error?.message ?? error}`)
|
|
381
|
+
}
|
|
382
|
+
await withRetry(() => rename(temporary, file), 8, 15)
|
|
383
|
+
} catch (error) {
|
|
384
|
+
// 兜底:rename 连续失败时,尝试先删目标再改名(Windows 上占用解除后通常能过)
|
|
385
|
+
if (error && RETRYABLE_CODES.has(error.code)) {
|
|
386
|
+
try {
|
|
387
|
+
await unlink(file)
|
|
388
|
+
await rename(temporary, file)
|
|
389
|
+
return
|
|
390
|
+
} catch (inner) {
|
|
391
|
+
try { await unlink(temporary) } catch { /* ignore */ }
|
|
392
|
+
throw inner
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
try { await unlink(temporary) } catch { /* ignore */ }
|
|
396
|
+
throw error
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** 当前注册表顺序(权威,来自内存缓存,不读盘)。 */
|
|
401
|
+
function currentOrder(ctx) {
|
|
402
|
+
return ctx.workspaceRegistry.list().map(workspace => workspace.id)
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function sameOrder(left, right) {
|
|
406
|
+
return left.length === right.length && left.every((id, index) => id === right[index])
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* 目标顺序 = 置顶(唯一) + 星标 + 其余。
|
|
411
|
+
* 星标区/普通区**各自保持当前顺序的先后**——用户在星标区内部拖动排序必须能保留;
|
|
412
|
+
* `restore`(可选)把刚被取消置顶/星标的工作区放回 `prev` 之后(一次性)。
|
|
413
|
+
*/
|
|
414
|
+
function layout(order, state, restore) {
|
|
415
|
+
const known = new Set(order)
|
|
416
|
+
const pinned = state.pinnedWorkspaceId !== undefined && known.has(state.pinnedWorkspaceId)
|
|
417
|
+
? [state.pinnedWorkspaceId]
|
|
418
|
+
: []
|
|
419
|
+
const pinnedSet = new Set(pinned)
|
|
420
|
+
const starredSet = new Set(state.starredWorkspaces.filter(id => known.has(id) && !pinnedSet.has(id)))
|
|
421
|
+
const starred = order.filter(id => starredSet.has(id))
|
|
422
|
+
const rest = order.filter(id => !pinnedSet.has(id) && !starredSet.has(id))
|
|
423
|
+
if (restore !== undefined && restore !== null && typeof restore.id === 'string') {
|
|
424
|
+
const bucket = starredSet.has(restore.id) ? starred : rest
|
|
425
|
+
const from = bucket.indexOf(restore.id)
|
|
426
|
+
if (from !== -1) {
|
|
427
|
+
bucket.splice(from, 1)
|
|
428
|
+
const anchor = typeof restore.prev === 'string' ? bucket.indexOf(restore.prev) : -1
|
|
429
|
+
bucket.splice(anchor === -1 ? 0 : anchor + 1, 0, restore.id)
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return [...pinned, ...starred, ...rest]
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* 把注册表顺序收敛到 target:每次 insertBefore(目标[i], 当前[i]) 再重读,逐步逼近。
|
|
437
|
+
* 由于插入会触发 domain/changed,调用方需要保证不会与自身打架(后者靠相等判断直接跳过)。
|
|
438
|
+
*/
|
|
439
|
+
async function applyOrder(ctx, target) {
|
|
440
|
+
for (let index = 0; index < target.length; index += 1) {
|
|
441
|
+
const order = currentOrder(ctx)
|
|
442
|
+
if (order[index] === target[index]) continue
|
|
443
|
+
if (!order.includes(target[index])) continue
|
|
444
|
+
await ctx.workspaceRegistry.insertBefore(target[index], order[index])
|
|
445
|
+
}
|
|
446
|
+
return currentOrder(ctx)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** 丢掉已删除工作区/会话的陈旧记录。 */
|
|
450
|
+
function prune(state, order) {
|
|
451
|
+
const known = new Set(order)
|
|
452
|
+
const next = {
|
|
453
|
+
...state,
|
|
454
|
+
starredWorkspaces: state.starredWorkspaces.filter(id => known.has(id)),
|
|
455
|
+
starPrev: Object.fromEntries(Object.entries(state.starPrev).filter(([id]) => known.has(id))),
|
|
456
|
+
}
|
|
457
|
+
if (next.pinnedWorkspaceId !== undefined && !known.has(next.pinnedWorkspaceId)) {
|
|
458
|
+
next.pinnedWorkspaceId = undefined
|
|
459
|
+
next.pinnedPrev = undefined
|
|
460
|
+
}
|
|
461
|
+
return next
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** T-20261202(彩蛋骨架):头像"文件真实存在"的计数(工作区 + 会话),只读。 */
|
|
465
|
+
function countAvatarFiles(settings) {
|
|
466
|
+
try {
|
|
467
|
+
const dir = assetDir()
|
|
468
|
+
let total = 0
|
|
469
|
+
for (const map of [settings && settings.avatar ? settings.avatar.workspace : null, settings && settings.avatar ? settings.avatar.session : null]) {
|
|
470
|
+
if (!map || typeof map !== 'object') continue
|
|
471
|
+
for (const key of Object.keys(map)) {
|
|
472
|
+
const value = String(map[key] || '')
|
|
473
|
+
if (value === '') continue
|
|
474
|
+
const file = join(dir, value.split(/[\\/]/).pop())
|
|
475
|
+
if (existsSync(file)) total += 1
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return total
|
|
479
|
+
} catch (error) {
|
|
480
|
+
return 0
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function snapshot(ctx, state, order) {
|
|
485
|
+
return {
|
|
486
|
+
pinnedWorkspaceId: state.pinnedWorkspaceId ?? null,
|
|
487
|
+
starredWorkspaces: state.starredWorkspaces,
|
|
488
|
+
starredSessions: state.starredSessions,
|
|
489
|
+
pinnedSessionByWorkspace: state.pinnedSessionByWorkspace,
|
|
490
|
+
workspaceIds: order,
|
|
491
|
+
settings: normalizeSettings(state.settings),
|
|
492
|
+
avatarFileCount: countAvatarFiles(normalizeSettings(state.settings)),
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/** 改一个设置项(校验后写盘),返回最新快照。 */
|
|
497
|
+
async function setSetting(ctx, key, value) {
|
|
498
|
+
const wasPaused = key === 'paused' ? (normalizeSettings((await readState()).settings).paused === true) : false
|
|
499
|
+
// v0.20.7:旧键当别名**要在未知键检查之前**映射,否则老前端刷新前的写入会被 400 挡掉
|
|
500
|
+
if (key === 'workspaceOpacity') key = 'areaOpacity'
|
|
501
|
+
if (!(key in SETTINGS_DEFAULTS)) {
|
|
502
|
+
const error = new Error(`unknown setting '${key}'`)
|
|
503
|
+
error.statusCode = 400
|
|
504
|
+
throw error
|
|
505
|
+
}
|
|
506
|
+
if (key === 'nativeSessionPin' && !NATIVE_PIN_MODES.includes(value)) {
|
|
507
|
+
const error = new Error("nativeSessionPin must be 'plugin' or 'native'")
|
|
508
|
+
error.statusCode = 400
|
|
509
|
+
throw error
|
|
510
|
+
}
|
|
511
|
+
if (key === 'themeEnabled' && typeof value !== 'boolean') {
|
|
512
|
+
const error = new Error("themeEnabled must be a boolean")
|
|
513
|
+
error.statusCode = 400
|
|
514
|
+
throw error
|
|
515
|
+
}
|
|
516
|
+
if (key === 'theme' && !THEMES.includes(value)) {
|
|
517
|
+
const error = new Error(`theme must be one of ${THEMES.join(' | ')}`)
|
|
518
|
+
error.statusCode = 400
|
|
519
|
+
throw error
|
|
520
|
+
}
|
|
521
|
+
if (key === 'sessionTitleChars') {
|
|
522
|
+
const n = Number(value)
|
|
523
|
+
// v0.17:2–12 字截断;0 或 13+ = 不限制(13+ 归一成 0)。
|
|
524
|
+
if (!Number.isInteger(n) || (n !== 0 && n < 2)) {
|
|
525
|
+
const error = new Error('sessionTitleChars must be 0 (unlimited), 2–12, or ≥13 (unlimited)')
|
|
526
|
+
error.statusCode = 400
|
|
527
|
+
throw error
|
|
528
|
+
}
|
|
529
|
+
value = n >= 13 ? 0 : n
|
|
530
|
+
}
|
|
531
|
+
if ((key === 'workspaceStarColor' || key === 'sessionStarColor') && !(typeof value === 'string' && HEX_COLOR.test(value))) {
|
|
532
|
+
const error = new Error(`${key} must be a #RRGGBB color`)
|
|
533
|
+
error.statusCode = 400
|
|
534
|
+
throw error
|
|
535
|
+
}
|
|
536
|
+
// —— v0.20:设置分页 / 透明度 / 背景图 / 余额开关 / 头像 ——
|
|
537
|
+
if (key === 'settingsTab') {
|
|
538
|
+
const n = Number(value)
|
|
539
|
+
if (!TABS.includes(n)) {
|
|
540
|
+
const error = new Error('settingsTab must be 1, 2 or 3')
|
|
541
|
+
error.statusCode = 400
|
|
542
|
+
throw error
|
|
543
|
+
}
|
|
544
|
+
value = n
|
|
545
|
+
}
|
|
546
|
+
if (key === 'areaOpacity') {
|
|
547
|
+
const n = Number(value)
|
|
548
|
+
if (!Number.isInteger(n) || n < 0 || n > 100) {
|
|
549
|
+
const error = new Error('areaOpacity must be an integer 0–100')
|
|
550
|
+
error.statusCode = 400
|
|
551
|
+
throw error
|
|
552
|
+
}
|
|
553
|
+
value = n
|
|
554
|
+
}
|
|
555
|
+
const pausing = key === 'paused' && value === true && wasPaused === false
|
|
556
|
+
if (key === 'userProfile') {
|
|
557
|
+
if (!value || typeof value !== 'object') {
|
|
558
|
+
const error = new Error('userProfile must be an object')
|
|
559
|
+
error.statusCode = 400
|
|
560
|
+
throw error
|
|
561
|
+
}
|
|
562
|
+
value = {
|
|
563
|
+
nickname: typeof value.nickname === 'string' ? value.nickname.slice(0, 32) : '',
|
|
564
|
+
avatar: typeof value.avatar === 'string' ? value.avatar.slice(0, 200) : '',
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (key === 'pausedTempReleaseAt') {
|
|
568
|
+
const n = Number(value)
|
|
569
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
570
|
+
const error = new Error('pausedTempReleaseAt must be a non-negative number (ms)')
|
|
571
|
+
error.statusCode = 400
|
|
572
|
+
throw error
|
|
573
|
+
}
|
|
574
|
+
value = Math.round(n)
|
|
575
|
+
}
|
|
576
|
+
if (key === 'useGlowAlert') {
|
|
577
|
+
if (value !== true && value !== false) {
|
|
578
|
+
const error = new Error('useGlowAlert must be a boolean')
|
|
579
|
+
error.statusCode = 400
|
|
580
|
+
throw error
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (key === 'inactiveHours') {
|
|
584
|
+
const n = Number(value)
|
|
585
|
+
if (!Number.isFinite(n) || n <= 0) {
|
|
586
|
+
const error = new Error('inactiveHours must be a positive number')
|
|
587
|
+
error.statusCode = 400
|
|
588
|
+
throw error
|
|
589
|
+
}
|
|
590
|
+
value = Math.min(24 * 365, n)
|
|
591
|
+
}
|
|
592
|
+
if (key === 'paused') {
|
|
593
|
+
if (value !== true && value !== false) {
|
|
594
|
+
const error = new Error('paused must be a boolean')
|
|
595
|
+
error.statusCode = 400
|
|
596
|
+
throw error
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
if (key === 'pausedReason') {
|
|
600
|
+
if (typeof value !== 'string') {
|
|
601
|
+
const error = new Error('pausedReason must be a string')
|
|
602
|
+
error.statusCode = 400
|
|
603
|
+
throw error
|
|
604
|
+
}
|
|
605
|
+
value = value.slice(0, 120)
|
|
606
|
+
}
|
|
607
|
+
if (key === 'limitAlert' || key === 'balanceAlert' || key === 'showTokenBalance') {
|
|
608
|
+
if (value !== true && value !== false) {
|
|
609
|
+
const error = new Error(key + ' must be a boolean')
|
|
610
|
+
error.statusCode = 400
|
|
611
|
+
throw error
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (key === 'balanceAlertThreshold') {
|
|
615
|
+
const n = Number(value)
|
|
616
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
617
|
+
const error = new Error('balanceAlertThreshold must be a non-negative number')
|
|
618
|
+
error.statusCode = 400
|
|
619
|
+
throw error
|
|
620
|
+
}
|
|
621
|
+
value = Math.round(n * 100) / 100
|
|
622
|
+
}
|
|
623
|
+
if (key === 'dailyLimit') {
|
|
624
|
+
const n = Number(value)
|
|
625
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
626
|
+
const error = new Error('dailyLimit must be a non-negative number (0 = unlimited)')
|
|
627
|
+
error.statusCode = 400
|
|
628
|
+
throw error
|
|
629
|
+
}
|
|
630
|
+
// 最多两位小数
|
|
631
|
+
if (Math.abs(n * 100 - Math.round(n * 100)) > 1e-9) {
|
|
632
|
+
const error = new Error('dailyLimit must have at most 2 decimals')
|
|
633
|
+
error.statusCode = 400
|
|
634
|
+
throw error
|
|
635
|
+
}
|
|
636
|
+
value = Math.round(n * 100) / 100
|
|
637
|
+
}
|
|
638
|
+
if (key === 'costBaseline') {
|
|
639
|
+
if (value === null) {
|
|
640
|
+
/* 允许清空 */
|
|
641
|
+
} else if (value && typeof value === 'object') {
|
|
642
|
+
const bal = Number(value.balance)
|
|
643
|
+
if (!Number.isFinite(bal) || typeof value.at !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(String(value.day || ''))) {
|
|
644
|
+
const error = new Error('costBaseline must be null or { balance:number, at:string, day:YYYY-MM-DD }')
|
|
645
|
+
error.statusCode = 400
|
|
646
|
+
throw error
|
|
647
|
+
}
|
|
648
|
+
value = { balance: bal, at: value.at, day: value.day }
|
|
649
|
+
} else {
|
|
650
|
+
const error = new Error('costBaseline must be null or an object')
|
|
651
|
+
error.statusCode = 400
|
|
652
|
+
throw error
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
if (key === 'bgMode') {
|
|
656
|
+
if (value !== 'default' && value !== 'custom' && value !== 'none') {
|
|
657
|
+
const error = new Error("bgMode must be 'default' | 'custom' | 'none'")
|
|
658
|
+
error.statusCode = 400
|
|
659
|
+
throw error
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (key === 'sidebarBackground') {
|
|
663
|
+
if (typeof value !== 'string' || (value !== '' && !isAssetPath(value))) {
|
|
664
|
+
const error = new Error('sidebarBackground must be "" or a path inside the asset directory')
|
|
665
|
+
error.statusCode = 400
|
|
666
|
+
throw error
|
|
667
|
+
}
|
|
668
|
+
// 指向不存在的文件 = 看不见的空白背景,宁可直接拒掉
|
|
669
|
+
if (value !== '' && !existsSync(value)) {
|
|
670
|
+
const error = new Error('sidebarBackground file does not exist')
|
|
671
|
+
error.statusCode = 400
|
|
672
|
+
throw error
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
if (key === 'showTokenBalance' || key === 'showBalanceTime') {
|
|
676
|
+
if (value !== true && value !== false) {
|
|
677
|
+
const error = new Error(key + ' must be a boolean')
|
|
678
|
+
error.statusCode = 400
|
|
679
|
+
throw error
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
if (key === 'avatar') {
|
|
683
|
+
if (!value || typeof value !== 'object') {
|
|
684
|
+
const error = new Error('avatar must be { enabled, workspace, session }')
|
|
685
|
+
error.statusCode = 400
|
|
686
|
+
throw error
|
|
687
|
+
}
|
|
688
|
+
// T-20261127(体检/CommanderT:工作区头像消失):**不再按 existsSync 剪条目**。
|
|
689
|
+
// 旧行为:任何一次设置写入时,文件不存在的头像条目会被永久丢弃 —— 一次误删资产就永远丢失配置。
|
|
690
|
+
// 新行为:条目原样保留;渲染层找不到文件时画占位(由客户端 paintAvatar 处理),配置不再被静默删除。
|
|
691
|
+
const normalized = normalizeAvatar(value)
|
|
692
|
+
value = { enabled: normalized.enabled, workspace: { ...normalized.workspace }, session: { ...normalized.session } }
|
|
693
|
+
}
|
|
694
|
+
const order = currentOrder(ctx)
|
|
695
|
+
const state = prune(await readState(), order)
|
|
696
|
+
const next = { ...state, settings: { ...normalizeSettings(state.settings), [key]: value } }
|
|
697
|
+
await writeState(next)
|
|
698
|
+
// 暂停刚被置起:尽力广播一次(不阻塞、失败不影响写入结果)
|
|
699
|
+
if (pausing) {
|
|
700
|
+
const reason = normalizeSettings(next.settings).pausedReason || '消费告警'
|
|
701
|
+
// 注意:**不要**在锁外再写一次盘(与 serialize 抢写会 ENOENT)。
|
|
702
|
+
// 广播结果只记日志 + 通过 snapshot 之外的诊断查看,不落 settings。
|
|
703
|
+
void broadcastPause(reason).then((result) => {
|
|
704
|
+
if (result !== 'disabled') console.log('[workspace-pin] 暂停广播结果:' + result)
|
|
705
|
+
})
|
|
706
|
+
}
|
|
707
|
+
return snapshot(ctx, next, order)
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/** 追加一行置顶动作日志(BUG-6 观察项;失败不影响主流程)。 */
|
|
711
|
+
async function logPinAction(action, target, before, after, result) {
|
|
712
|
+
try {
|
|
713
|
+
await mkdir(dirname(PIN_LOG), { recursive: true })
|
|
714
|
+
const line = `${new Date().toISOString()} | ${action} | ${target ?? '-'} | ${before ?? 'null'} | ${after ?? 'null'} | ${result}\n`
|
|
715
|
+
await appendFile(PIN_LOG, line, 'utf8')
|
|
716
|
+
} catch (error) {
|
|
717
|
+
console.warn(`[workspace-pin] 置顶日志写入失败:${error?.message ?? error}`)
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/** 设置/清除唯一置顶(workspaceId 为 null 表示清除)。 */
|
|
722
|
+
async function setPin(ctx, workspaceId) {
|
|
723
|
+
const order = currentOrder(ctx)
|
|
724
|
+
if (workspaceId !== null && !order.includes(workspaceId)) {
|
|
725
|
+
const error = new Error(`unknown workspace '${workspaceId}'`)
|
|
726
|
+
error.statusCode = 404
|
|
727
|
+
throw error
|
|
728
|
+
}
|
|
729
|
+
let state = prune(await readState(), order)
|
|
730
|
+
if (state.pinnedWorkspaceId === (workspaceId ?? undefined)) {
|
|
731
|
+
await logPinAction(workspaceId === null ? 'unpin' : 'pin', workspaceId, state.pinnedWorkspaceId, state.pinnedWorkspaceId, 'noop')
|
|
732
|
+
return snapshot(ctx, state, order)
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
let restore
|
|
736
|
+
if (state.pinnedWorkspaceId !== undefined && state.pinnedWorkspaceId !== workspaceId) {
|
|
737
|
+
// 被顶替的旧置顶项:先放回它原来记下的位置(如果有),再让新项占最上。
|
|
738
|
+
restore = { id: state.pinnedWorkspaceId, prev: state.pinnedPrev }
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
let next
|
|
742
|
+
if (workspaceId === null) {
|
|
743
|
+
next = { ...state, pinnedWorkspaceId: undefined, pinnedPrev: undefined }
|
|
744
|
+
} else {
|
|
745
|
+
const position = order.indexOf(workspaceId)
|
|
746
|
+
const prev = position > 0 ? order[position - 1] : undefined
|
|
747
|
+
// 置顶优先于星标:置顶项若本来星标着,仍留在星标集合里,取消置顶后回到它的星标位置。
|
|
748
|
+
next = { ...state, pinnedWorkspaceId: workspaceId, pinnedPrev: prev }
|
|
749
|
+
}
|
|
750
|
+
if (restore === undefined && next.pinnedWorkspaceId === undefined) {
|
|
751
|
+
restore = { id: state.pinnedWorkspaceId, prev: state.pinnedPrev }
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
await writeState(next)
|
|
755
|
+
const workspaceIds = await applyOrder(ctx, layout(order, next, restore))
|
|
756
|
+
await logPinAction(workspaceId === null ? 'unpin' : 'pin', workspaceId, state.pinnedWorkspaceId, next.pinnedWorkspaceId, 'ok')
|
|
757
|
+
return snapshot(ctx, next, workspaceIds)
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/** 设置/清除星标(工作区或会话)。星标工作区进入星标区末尾(= 星标时间顺序)。 */
|
|
761
|
+
async function setStar(ctx, kind, id, starred) {
|
|
762
|
+
if (kind !== 'workspace' && kind !== 'session') {
|
|
763
|
+
const error = new Error("kind must be 'workspace' or 'session'")
|
|
764
|
+
error.statusCode = 400
|
|
765
|
+
throw error
|
|
766
|
+
}
|
|
767
|
+
const order = currentOrder(ctx)
|
|
768
|
+
let state = prune(await readState(), order)
|
|
769
|
+
|
|
770
|
+
if (kind === 'session') {
|
|
771
|
+
const before = state.starredSessions
|
|
772
|
+
if (starred === before.includes(id)) return snapshot(ctx, state, order)
|
|
773
|
+
const after = starred
|
|
774
|
+
? [...before, id]
|
|
775
|
+
: before.filter(candidate => candidate !== id)
|
|
776
|
+
const next = { ...state, starredSessions: after }
|
|
777
|
+
await writeState(next)
|
|
778
|
+
return snapshot(ctx, next, order)
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
if (!order.includes(id)) {
|
|
782
|
+
const error = new Error(`unknown workspace '${id}'`)
|
|
783
|
+
error.statusCode = 404
|
|
784
|
+
throw error
|
|
785
|
+
}
|
|
786
|
+
if (starred && state.starredWorkspaces.includes(id)) return snapshot(ctx, state, order)
|
|
787
|
+
if (!starred && !state.starredWorkspaces.includes(id)) return snapshot(ctx, state, order)
|
|
788
|
+
|
|
789
|
+
let next
|
|
790
|
+
let restore
|
|
791
|
+
if (starred) {
|
|
792
|
+
const position = order.indexOf(id)
|
|
793
|
+
const prev = position > 0 ? order[position - 1] : undefined
|
|
794
|
+
const starPrev = { ...state.starPrev }
|
|
795
|
+
if (prev !== undefined) starPrev[id] = prev
|
|
796
|
+
else delete starPrev[id]
|
|
797
|
+
next = {
|
|
798
|
+
...state,
|
|
799
|
+
starredWorkspaces: [...state.starredWorkspaces, id],
|
|
800
|
+
starPrev,
|
|
801
|
+
// 置顶项被星标时保留置顶身份(置顶优先级绝对),但仍记进星标集合。
|
|
802
|
+
}
|
|
803
|
+
} else {
|
|
804
|
+
const prev = state.starPrev[id]
|
|
805
|
+
const starPrev = { ...state.starPrev }
|
|
806
|
+
delete starPrev[id]
|
|
807
|
+
next = { ...state, starredWorkspaces: state.starredWorkspaces.filter(candidate => candidate !== id), starPrev }
|
|
808
|
+
restore = { id, prev }
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
await writeState(next)
|
|
812
|
+
const workspaceIds = await applyOrder(ctx, layout(order, next, restore))
|
|
813
|
+
return snapshot(ctx, next, workspaceIds)
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
function sendJson(res, statusCode, value) {
|
|
817
|
+
const body = `${JSON.stringify(value)}\n`
|
|
818
|
+
res.writeHead(statusCode, {
|
|
819
|
+
'content-type': 'application/json; charset=utf-8',
|
|
820
|
+
'cache-control': 'no-store',
|
|
821
|
+
'content-length': Buffer.byteLength(body),
|
|
822
|
+
})
|
|
823
|
+
res.end(body)
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/** 只接受同 host 的写请求,降低被任意网页 CSRF 的风险。 */
|
|
827
|
+
|
|
828
|
+
/** v0.22.51:Host 侧请求日志(同步写,日志本身绝不成为挂起点)。按天切文件。 */
|
|
829
|
+
function logHostRequest(req, res, started) {
|
|
830
|
+
try {
|
|
831
|
+
const dir = DATA_DIR
|
|
832
|
+
mkdirSync(dir, { recursive: true })
|
|
833
|
+
const day = new Date().toISOString().slice(0, 10)
|
|
834
|
+
const line = new Date().toISOString()
|
|
835
|
+
+ ' ' + String(req.method || '?')
|
|
836
|
+
+ ' ' + String(req.url || '?')
|
|
837
|
+
+ ' cl=' + String(req.headers['content-length'] || '?')
|
|
838
|
+
+ ' status=' + String(res.statusCode || 0)
|
|
839
|
+
+ ' dur=' + (Date.now() - started) + 'ms\n'
|
|
840
|
+
appendFileSync(join(dir, 'host-requests-' + day + '.log'), line, 'utf8')
|
|
841
|
+
} catch { /* 日志失败不影响响应 */ }
|
|
842
|
+
}
|
|
843
|
+
function originAllowed(req) {
|
|
844
|
+
const origin = req.headers.origin
|
|
845
|
+
if (origin === undefined || origin === '') return true
|
|
846
|
+
try {
|
|
847
|
+
return new URL(origin).host === req.headers.host
|
|
848
|
+
} catch {
|
|
849
|
+
return false
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
async function readBody(req, limit = MAX_BODY_BYTES) {
|
|
854
|
+
const chunks = []
|
|
855
|
+
let total = 0
|
|
856
|
+
for await (const chunk of req) {
|
|
857
|
+
total += chunk.length
|
|
858
|
+
if (total > limit) {
|
|
859
|
+
const error = new Error('request body too large')
|
|
860
|
+
error.statusCode = 413
|
|
861
|
+
throw error
|
|
862
|
+
}
|
|
863
|
+
chunks.push(chunk)
|
|
864
|
+
}
|
|
865
|
+
if (total === 0) return {}
|
|
866
|
+
try {
|
|
867
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8'))
|
|
868
|
+
} catch {
|
|
869
|
+
const error = new Error('request body is not valid JSON')
|
|
870
|
+
error.statusCode = 400
|
|
871
|
+
throw error
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* 把浏览器 canvas 裁好的图落盘。
|
|
877
|
+
* **每次上传都用一个新文件名**(带时间戳 token):
|
|
878
|
+
* 素材路由带 `immutable` 长缓存,若沿用 `ws-<id>.png` 这种固定名,
|
|
879
|
+
* 换头像只是覆盖同名文件,浏览器会一直拿缓存里第一次那张图 —— 这就是
|
|
880
|
+
* 「换头像后变成纯蓝圆/旧图」的根因。新名字 = 新 URL,缓存自然失效。
|
|
881
|
+
* kind=sidebar → sidebar-<token>.<ext>;kind=ws/se → ws-<id>-<token>.<ext>。
|
|
882
|
+
* 只接受 png / jpeg 的 data URL,且大小受 MAX_ASSET_BYTES 约束。
|
|
883
|
+
*/
|
|
884
|
+
async function saveAsset(kind, id, dataUrl) {
|
|
885
|
+
const match = typeof dataUrl === 'string' ? DATA_URL.exec(dataUrl) : null
|
|
886
|
+
if (!match) {
|
|
887
|
+
const error = new Error('dataUrl must be a base64 data:image/png|jpeg URL')
|
|
888
|
+
error.statusCode = 400
|
|
889
|
+
throw error
|
|
890
|
+
}
|
|
891
|
+
const ext = match[1] === 'jpeg' ? 'jpg' : 'png'
|
|
892
|
+
const body = Buffer.from(match[2], 'base64')
|
|
893
|
+
if (body.length === 0) {
|
|
894
|
+
const error = new Error('empty image payload')
|
|
895
|
+
error.statusCode = 400
|
|
896
|
+
throw error
|
|
897
|
+
}
|
|
898
|
+
if (body.length > MAX_ASSET_BYTES) {
|
|
899
|
+
const error = new Error('image too large (limit 6MB after cropping)')
|
|
900
|
+
error.statusCode = 413
|
|
901
|
+
throw error
|
|
902
|
+
}
|
|
903
|
+
const token = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`
|
|
904
|
+
let name
|
|
905
|
+
let prefix = ''
|
|
906
|
+
if (kind === 'sidebar') {
|
|
907
|
+
name = `sidebar-${token}.${ext}`
|
|
908
|
+
} else {
|
|
909
|
+
if (typeof id !== 'string' || !/^[A-Za-z0-9_-]{1,80}$/.test(id)) {
|
|
910
|
+
const error = new Error('id must be a workspace/session id')
|
|
911
|
+
error.statusCode = 400
|
|
912
|
+
throw error
|
|
913
|
+
}
|
|
914
|
+
prefix = kind === 'profile' ? 'profile-user' : `${kind === 'ws' ? 'ws' : 'se'}-${id}`
|
|
915
|
+
name = `${prefix}-${token}.${ext}`
|
|
916
|
+
}
|
|
917
|
+
if (!ASSET_NAME.test(name)) {
|
|
918
|
+
const error = new Error('refusing to write an unexpected asset name')
|
|
919
|
+
error.statusCode = 400
|
|
920
|
+
throw error
|
|
921
|
+
}
|
|
922
|
+
const dir = assetDir()
|
|
923
|
+
await mkdir(dir, { recursive: true })
|
|
924
|
+
const file = join(dir, name)
|
|
925
|
+
// 唯一化:同一个 pid 内并发写若共用固定 temp 名,第二个 rename 会 ENOENT(实测 paused 连写踩过)
|
|
926
|
+
const temporary = `${file}.${process.pid}.${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}.tmp`
|
|
927
|
+
await writeFile(temporary, body)
|
|
928
|
+
await rename(temporary, file)
|
|
929
|
+
// 写成功再清旧图;失败也仍然返回(新图已经在盘上了)
|
|
930
|
+
if (prefix !== '') await pruneOldAssets(dir, prefix, name)
|
|
931
|
+
return { name, path: file, bytes: body.length }
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
/**
|
|
935
|
+
* 删掉同一个工作区/会话的旧素材(含 v0.20.3 之前的固定名 ws-<id>.png)。
|
|
936
|
+
* 两道保险,绝不误删:
|
|
937
|
+
* 1. 只删「过得了 ASSET_NAME 白名单 + 前缀完全匹配」的文件;
|
|
938
|
+
* 2. **先确认该路径没有被 settings 里任何键引用**(avatar.workspace / avatar.session /
|
|
939
|
+
* sidebarBackground)—— 被引用的文件一律跳过。
|
|
940
|
+
*/
|
|
941
|
+
async function pruneOldAssets(dir, prefix, keepName) {
|
|
942
|
+
let names = []
|
|
943
|
+
try {
|
|
944
|
+
names = await readdir(dir)
|
|
945
|
+
} catch {
|
|
946
|
+
return
|
|
947
|
+
}
|
|
948
|
+
const referenced = await referencedAssetPaths()
|
|
949
|
+
for (const name of names) {
|
|
950
|
+
if (name === keepName) continue
|
|
951
|
+
if (!ASSET_NAME.test(name)) continue
|
|
952
|
+
const body = name.slice(0, name.lastIndexOf('.'))
|
|
953
|
+
// 旧固定名:ws-<id> ;新名:ws-<id>-<token>
|
|
954
|
+
if (body !== prefix && body.indexOf(`${prefix}-`) !== 0) continue
|
|
955
|
+
const full = join(dir, name)
|
|
956
|
+
if (referenced.has(full)) continue // 还有别的键指着它 → 不删
|
|
957
|
+
// v0.22.57:**10 分钟宽限期**——刚写完、客户端还没把 name 写进 settings 的新文件,
|
|
958
|
+
// 在这个时间窗里一删就会变成"文件没了、头像也显示不出来"(实测踩过)。
|
|
959
|
+
try {
|
|
960
|
+
const info = await stat(full)
|
|
961
|
+
if (Date.now() - info.mtimeMs < 10 * 60 * 1000) continue
|
|
962
|
+
} catch {
|
|
963
|
+
continue
|
|
964
|
+
}
|
|
965
|
+
try {
|
|
966
|
+
await unlink(full)
|
|
967
|
+
} catch {
|
|
968
|
+
// 删不掉不影响主流程
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
/** 当前 settings 里引用到的素材绝对路径集合(删文件前用它做保护检查)。 */
|
|
974
|
+
async function referencedAssetPaths() {
|
|
975
|
+
const out = new Set()
|
|
976
|
+
let state = null
|
|
977
|
+
try {
|
|
978
|
+
state = await readState()
|
|
979
|
+
} catch {
|
|
980
|
+
return out
|
|
981
|
+
}
|
|
982
|
+
const settings = (state && state.settings) || {}
|
|
983
|
+
if (typeof settings.sidebarBackground === 'string' && settings.sidebarBackground !== '') {
|
|
984
|
+
out.add(settings.sidebarBackground)
|
|
985
|
+
}
|
|
986
|
+
const avatar = settings.avatar || {}
|
|
987
|
+
for (const kind of ['workspace', 'session']) {
|
|
988
|
+
const map = avatar[kind]
|
|
989
|
+
if (!map || typeof map !== 'object') continue
|
|
990
|
+
for (const value of Object.values(map)) {
|
|
991
|
+
if (typeof value === 'string' && value !== '') out.add(value)
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
// v0.22.44:用户资料头像也要保护,否则"保存新头像 → 剪掉旧文件"会把 current 那张删掉
|
|
995
|
+
const profileAvatar = settings.userProfile && settings.userProfile.avatar
|
|
996
|
+
if (typeof profileAvatar === 'string' && profileAvatar !== '') {
|
|
997
|
+
out.add(join(assetDir(), profileAvatar.split(/[\\/]/).pop()))
|
|
998
|
+
}
|
|
999
|
+
return out
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/** 读素材文件并直接回写响应;文件名先过 ASSET_NAME 白名单,挡掉路径穿越。 */
|
|
1003
|
+
async function sendAsset(res, name) {
|
|
1004
|
+
if (typeof name !== 'string' || !ASSET_NAME.test(name)) {
|
|
1005
|
+
res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' })
|
|
1006
|
+
res.end('bad asset name')
|
|
1007
|
+
return
|
|
1008
|
+
}
|
|
1009
|
+
const file = join(assetDir(), name)
|
|
1010
|
+
let body
|
|
1011
|
+
try {
|
|
1012
|
+
body = await readFile(file)
|
|
1013
|
+
} catch (error) {
|
|
1014
|
+
if (error && error.code === 'ENOENT') {
|
|
1015
|
+
res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' })
|
|
1016
|
+
res.end('not found')
|
|
1017
|
+
return
|
|
1018
|
+
}
|
|
1019
|
+
throw error
|
|
1020
|
+
}
|
|
1021
|
+
const ext = name.slice(name.lastIndexOf('.') + 1)
|
|
1022
|
+
res.writeHead(200, {
|
|
1023
|
+
'content-type': ASSET_MIME[ext] || 'application/octet-stream',
|
|
1024
|
+
// 名字带时间戳/ID,内容不会变;长缓存减少每次渲染的请求。
|
|
1025
|
+
'cache-control': 'private, max-age=31536000, immutable',
|
|
1026
|
+
'content-length': body.length,
|
|
1027
|
+
})
|
|
1028
|
+
res.end(body)
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/** 余额 / 今日 token:读不到就返回 null,**绝不编数字**(前端显示 --)。 */
|
|
1032
|
+
let balanceCache = { at: 0, key: '', value: null }
|
|
1033
|
+
|
|
1034
|
+
/** 每次请求时读环境变量,便于测试与排障时换流水文件。
|
|
1035
|
+
/** 每次请求时读环境变量,便于测试与排障时换流水文件。
|
|
1036
|
+
* v0.22.158:账本只作为兜底(首选 DSH 账号服务),路径不再含任何本机绝对地址。 */
|
|
1037
|
+
function balanceLogPath() {
|
|
1038
|
+
const env = process.env.WP_BALANCE_LOG
|
|
1039
|
+
if (env) return env
|
|
1040
|
+
try {
|
|
1041
|
+
if (existsSync(LEGACY_BALANCE_LOG)) return LEGACY_BALANCE_LOG
|
|
1042
|
+
} catch { /* 忽略 */ }
|
|
1043
|
+
return LEGACY_BALANCE_LOG
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/** v0.22.159:**直接问 DSH 账号服务取余额**(跨机器可用,不依赖任何本地账本)。
|
|
1047
|
+
* 解析逻辑照 token-balance 那套:兼容 `{ok, value}` 信封、value 可能是数组、字段三种写法。
|
|
1048
|
+
* 任何一步失败都返回 null,绝不抛、不阻塞 /account。 */
|
|
1049
|
+
export function parseAccountBalance(payload) {
|
|
1050
|
+
let body = payload
|
|
1051
|
+
let guard = 0
|
|
1052
|
+
while (body && typeof body === 'object' && 'ok' in body && 'value' in body && guard < 3) {
|
|
1053
|
+
body = body.value
|
|
1054
|
+
guard += 1
|
|
1055
|
+
}
|
|
1056
|
+
if (!body || typeof body !== 'object') return null
|
|
1057
|
+
const raw = body.value !== undefined ? body.value : body
|
|
1058
|
+
const main = Array.isArray(raw) ? raw[0] : raw
|
|
1059
|
+
if (!main || typeof main !== 'object') return null
|
|
1060
|
+
const amount = main.balance ?? main.totalBalance ?? main.total_balance ?? null
|
|
1061
|
+
if (amount === null || amount === undefined) return null
|
|
1062
|
+
const n = Number(amount)
|
|
1063
|
+
if (!Number.isFinite(n)) return null
|
|
1064
|
+
const bonusWallet = Array.isArray(body.bonusWallets) ? body.bonusWallets[0] : null
|
|
1065
|
+
// v0.22.243:分项钱包(充值钱包 / 赠送钱包)。赠送到账也会让总余额跳升,
|
|
1066
|
+
// 但**不该**让日支作废 —— 充值判据只认 normal 钱包。取不到就不写该字段。
|
|
1067
|
+
const normalArr = Array.isArray(body.normal_wallets) ? body.normal_wallets
|
|
1068
|
+
: (Array.isArray(body.normalWallets) ? body.normalWallets : null)
|
|
1069
|
+
const bonusArr = Array.isArray(body.bonus_wallets) ? body.bonus_wallets
|
|
1070
|
+
: (Array.isArray(body.bonusWallets) ? body.bonusWallets : null)
|
|
1071
|
+
const sumWallets = (arr) => {
|
|
1072
|
+
if (!Array.isArray(arr)) return null
|
|
1073
|
+
let sum = 0
|
|
1074
|
+
for (const wallet of arr) {
|
|
1075
|
+
const value = Number(wallet && wallet.balance)
|
|
1076
|
+
if (!Number.isFinite(value)) continue
|
|
1077
|
+
sum += value
|
|
1078
|
+
}
|
|
1079
|
+
return Math.round(sum * 100) / 100
|
|
1080
|
+
}
|
|
1081
|
+
const normalSum = sumWallets(normalArr)
|
|
1082
|
+
const bonusSum = sumWallets(bonusArr)
|
|
1083
|
+
return {
|
|
1084
|
+
amount: n.toFixed(2),
|
|
1085
|
+
currency: String(main.currency ?? 'CNY'),
|
|
1086
|
+
bonus: bonusWallet ? String(bonusWallet.balance ?? '') : null,
|
|
1087
|
+
normalBalance: normalSum === null ? null : normalSum.toFixed(2),
|
|
1088
|
+
bonusBalance: bonusSum === null ? null : bonusSum.toFixed(2),
|
|
1089
|
+
at: new Date().toISOString(),
|
|
1090
|
+
source: 'account',
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
/** 3 秒超时保护:账号服务异常时不能让 /account 挂住。 */
|
|
1095
|
+
async function balanceFromAccount(ctx) {
|
|
1096
|
+
try {
|
|
1097
|
+
let acct = null
|
|
1098
|
+
try { acct = ctx.deepseekAccount } catch (error) { return null }
|
|
1099
|
+
if (!acct || typeof acct.getBalance !== 'function') return null
|
|
1100
|
+
const meta = {
|
|
1101
|
+
version: '0.2.2-rc.5',
|
|
1102
|
+
locale: 'zh-CN',
|
|
1103
|
+
timezoneOffsetSeconds: -new Date().getTimezoneOffset() * 60,
|
|
1104
|
+
}
|
|
1105
|
+
const payload = await Promise.race([
|
|
1106
|
+
Promise.resolve(acct.getBalance(meta)),
|
|
1107
|
+
new Promise((resolve) => setTimeout(() => resolve(null), 3000)),
|
|
1108
|
+
])
|
|
1109
|
+
if (!payload) return null
|
|
1110
|
+
return parseAccountBalance(payload)
|
|
1111
|
+
} catch (error) {
|
|
1112
|
+
return null
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
/** 余额日志是否可用(文件存在才算):用于前端显示「未配置」而不是「--」。 */
|
|
1117
|
+
async function balanceLogConfigured() {
|
|
1118
|
+
try {
|
|
1119
|
+
const st = await stat(balanceLogPath())
|
|
1120
|
+
return !!(st && st.isFile())
|
|
1121
|
+
} catch {
|
|
1122
|
+
return false
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
async function readBalance() {
|
|
1127
|
+
const file = balanceLogPath()
|
|
1128
|
+
const now = Date.now()
|
|
1129
|
+
if (balanceCache.value !== null && balanceCache.key === file && now - balanceCache.at < BALANCE_CACHE_MS) {
|
|
1130
|
+
return balanceCache.value
|
|
1131
|
+
}
|
|
1132
|
+
let value = null
|
|
1133
|
+
try {
|
|
1134
|
+
const text = await readFile(file, 'utf8')
|
|
1135
|
+
const lines = text.split('\n').filter(line => line.trim() !== '')
|
|
1136
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
1137
|
+
try {
|
|
1138
|
+
const row = JSON.parse(lines[index])
|
|
1139
|
+
const amount = Number(row?.balance)
|
|
1140
|
+
if (!Number.isFinite(amount)) continue
|
|
1141
|
+
value = {
|
|
1142
|
+
amount: amount.toFixed(2),
|
|
1143
|
+
currency: typeof row.currency === 'string' && row.currency !== '' ? row.currency : 'CNY',
|
|
1144
|
+
at: typeof row.at === 'string' ? row.at : null,
|
|
1145
|
+
source: 'log',
|
|
1146
|
+
}
|
|
1147
|
+
break
|
|
1148
|
+
} catch {
|
|
1149
|
+
// 某一行坏掉就往前找,不影响其它行
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
} catch {
|
|
1153
|
+
value = null
|
|
1154
|
+
}
|
|
1155
|
+
// null 不缓存:文件还没生成时,下一次请求应该再试。
|
|
1156
|
+
balanceCache = { at: now, key: file, value }
|
|
1157
|
+
return value
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
/* ============================================================
|
|
1161
|
+
* T-20261125-02 · 会话最后活动时间(给工作区「全部长期无响应」判定用)
|
|
1162
|
+
*
|
|
1163
|
+
* 数据来源:`~/.dsh/sessions/<工作区编码>/<sessionId>/` 下所有文件的**最新 mtime**。
|
|
1164
|
+
* 只读、不解析内容;30s 缓存,避免每次轮询都扫盘。
|
|
1165
|
+
* 不参与任何写操作,失败一律降级为"没有活动时间"(前端就不判 stale)。
|
|
1166
|
+
* ============================================================ */
|
|
1167
|
+
const ACTIVITY_CACHE_MS = 30 * 1000
|
|
1168
|
+
let activityCache = { at: 0, map: {} }
|
|
1169
|
+
|
|
1170
|
+
async function readSessionActivity() {
|
|
1171
|
+
const now = Date.now()
|
|
1172
|
+
if (now - activityCache.at < ACTIVITY_CACHE_MS) return activityCache
|
|
1173
|
+
const map = {}
|
|
1174
|
+
const root = join(dshHome(), 'sessions')
|
|
1175
|
+
try {
|
|
1176
|
+
const groups = await readdir(root, { withFileTypes: true })
|
|
1177
|
+
for (const group of groups) {
|
|
1178
|
+
if (!group.isDirectory()) continue
|
|
1179
|
+
const groupPath = join(root, group.name)
|
|
1180
|
+
let sessions = []
|
|
1181
|
+
try {
|
|
1182
|
+
sessions = await readdir(groupPath, { withFileTypes: true })
|
|
1183
|
+
} catch {
|
|
1184
|
+
continue
|
|
1185
|
+
}
|
|
1186
|
+
for (const session of sessions) {
|
|
1187
|
+
if (!session.isDirectory()) continue
|
|
1188
|
+
const sessionPath = join(groupPath, session.name)
|
|
1189
|
+
let files = []
|
|
1190
|
+
try {
|
|
1191
|
+
files = await readdir(sessionPath)
|
|
1192
|
+
} catch {
|
|
1193
|
+
continue
|
|
1194
|
+
}
|
|
1195
|
+
let latest = 0
|
|
1196
|
+
for (const file of files) {
|
|
1197
|
+
try {
|
|
1198
|
+
const info = await stat(join(sessionPath, file))
|
|
1199
|
+
if (info.mtimeMs > latest) latest = info.mtimeMs
|
|
1200
|
+
} catch {
|
|
1201
|
+
// 单个文件读不到就跳过
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
if (latest > 0 && !(map[session.name] > latest)) map[session.name] = Math.round(latest)
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
} catch {
|
|
1208
|
+
// 目录不存在等情况:返回空表
|
|
1209
|
+
}
|
|
1210
|
+
activityCache = { at: now, map }
|
|
1211
|
+
return activityCache
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
/**
|
|
1215
|
+
* 今日消耗 token:本机没有任何可信来源 —— token-balance 只写余额流水,
|
|
1216
|
+
* DSH 的 session 日志里不含 usage 字段(27 个会话文件全量扫过,0 命中)。
|
|
1217
|
+
* 所以恒为 null,由前端显示 `--`;这里保留字段与 source,等将来有真数据时直接填。
|
|
1218
|
+
*/
|
|
1219
|
+
function todayTokens() {
|
|
1220
|
+
return { tokens: null, source: 'unavailable' }
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/* ============================================================
|
|
1224
|
+
* T-20261125-01 · 暂停的「尽力停止」第三步:向所有活动会话广播一条暂停提示
|
|
1225
|
+
*
|
|
1226
|
+
* 边界(CommanderT 明确):
|
|
1227
|
+
* - **绝不 kill 正在运行的会话**;已经启用的工作不受影响
|
|
1228
|
+
* - 只做:阻止新建 + 显著横幅 + 广播提示
|
|
1229
|
+
* 安全:
|
|
1230
|
+
* - 默认**不开启**(WP_PAUSE_BROADCAST=on 才发),因为一次广播会唤醒全场会话
|
|
1231
|
+
* - 每次暂停只发一次;失败不影响主流程,结果写进 settings.pausedBroadcast
|
|
1232
|
+
* ============================================================ */
|
|
1233
|
+
const BROADCAST_FLAG = () => (process.env.WP_PAUSE_BROADCAST || '') === 'on'
|
|
1234
|
+
const BUS_CMD = process.env.WP_BUS_CMD || ''
|
|
1235
|
+
const BROADCAST_PREFIX = process.env.WP_BROADCAST_PREFIX || ''
|
|
1236
|
+
let broadcastInFlight = false
|
|
1237
|
+
|
|
1238
|
+
/** 返回 'sent' | 'failed' | 'disabled' | 'busy',绝不抛。 */
|
|
1239
|
+
async function broadcastPause(reason) {
|
|
1240
|
+
// 可移植版:没配总线(BUS_CMD / 前缀为空)或没开开关 → 静默禁用,不报错、不重试、不打日志
|
|
1241
|
+
if (BUS_CMD === '' || BROADCAST_PREFIX === '') return 'disabled'
|
|
1242
|
+
if (!BROADCAST_FLAG()) return 'disabled'
|
|
1243
|
+
if (broadcastInFlight) return 'busy'
|
|
1244
|
+
broadcastInFlight = true
|
|
1245
|
+
const text = '⏸ 暂停提示:' + (reason || '消费告警') + ' —— 请不要再新建任务,手上正在跑的工作继续,不要中断。'
|
|
1246
|
+
try {
|
|
1247
|
+
const code = await new Promise((resolve) => {
|
|
1248
|
+
let settled = false
|
|
1249
|
+
const done = (value) => { if (!settled) { settled = true; resolve(value) } }
|
|
1250
|
+
let child
|
|
1251
|
+
try {
|
|
1252
|
+
child = spawn(BUS_CMD, ['broadcast', '--prefix', BROADCAST_PREFIX, '--no-focus', text], {
|
|
1253
|
+
windowsHide: true,
|
|
1254
|
+
stdio: 'ignore',
|
|
1255
|
+
})
|
|
1256
|
+
} catch {
|
|
1257
|
+
done(-1)
|
|
1258
|
+
return
|
|
1259
|
+
}
|
|
1260
|
+
const timer = setTimeout(() => { try { child.kill() } catch {} ; done(-2) }, 20000)
|
|
1261
|
+
child.on('error', () => { clearTimeout(timer); done(-1) })
|
|
1262
|
+
child.on('exit', (exitCode) => { clearTimeout(timer); done(exitCode === 0 ? 0 : 1) })
|
|
1263
|
+
})
|
|
1264
|
+
return code === 0 ? 'sent' : 'failed'
|
|
1265
|
+
} catch {
|
|
1266
|
+
return 'failed'
|
|
1267
|
+
} finally {
|
|
1268
|
+
broadcastInFlight = false
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/* ============================================================
|
|
1273
|
+
* T-20261125-01 · 今日消费(余额差值)
|
|
1274
|
+
*
|
|
1275
|
+
* 口径(CommanderT 定):
|
|
1276
|
+
* 今日消费 = |当日基线余额 − 当前余额|,基线按**自然日**取。
|
|
1277
|
+
* 余额变大(充值)→ 不显示负数,标"本周期内有充值"。
|
|
1278
|
+
*
|
|
1279
|
+
* 基线来源优先级:
|
|
1280
|
+
* 1) balance-log.jsonl 里**当天首条采样**(最可靠,跨重启也不丢)
|
|
1281
|
+
* 2) settings.costBaseline(快速路径;同一天才认)
|
|
1282
|
+
* 3) 当天还没有任何采样 → 立刻用当前余额当场写一条基线
|
|
1283
|
+
* 两者不一致时**以日志为准**,并把日志的值回写 settings。
|
|
1284
|
+
*
|
|
1285
|
+
* 注意:tokenMeter / /usage 都不是"今日消耗",不要用来冒充 token 数。
|
|
1286
|
+
* ============================================================ */
|
|
1287
|
+
const SAMPLE_INTERVAL_MS = 5 * 60 * 1000 // 自己采样:5 分钟一条(日支才有分辨率)
|
|
1288
|
+
|
|
1289
|
+
/** 本地自然日 YYYY-MM-DD(at 是 UTC ISO,按本机时区归日)。 */
|
|
1290
|
+
function localDay(iso) {
|
|
1291
|
+
const d = iso ? new Date(iso) : new Date()
|
|
1292
|
+
if (Number.isNaN(d.getTime())) return ''
|
|
1293
|
+
try {
|
|
1294
|
+
return d.toLocaleDateString('en-CA')
|
|
1295
|
+
} catch {
|
|
1296
|
+
return d.toISOString().slice(0, 10)
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
/** 读整条余额流水,按时间升序,坏行跳过。 */
|
|
1301
|
+
async function readBalanceRows() {
|
|
1302
|
+
const file = balanceLogPath()
|
|
1303
|
+
let text = ''
|
|
1304
|
+
try {
|
|
1305
|
+
text = await readFile(file, 'utf8')
|
|
1306
|
+
} catch {
|
|
1307
|
+
return []
|
|
1308
|
+
}
|
|
1309
|
+
const rows = []
|
|
1310
|
+
for (const line of text.split('\n')) {
|
|
1311
|
+
if (line.trim() === '') continue
|
|
1312
|
+
try {
|
|
1313
|
+
const row = JSON.parse(line)
|
|
1314
|
+
const balance = Number(row?.balance)
|
|
1315
|
+
if (!Number.isFinite(balance)) continue
|
|
1316
|
+
const at = typeof row.at === 'string' ? row.at : ''
|
|
1317
|
+
if (at === '') continue
|
|
1318
|
+
const normal = Number(row?.normalBalance)
|
|
1319
|
+
const bonus = Number(row?.bonusBalance)
|
|
1320
|
+
rows.push({
|
|
1321
|
+
at,
|
|
1322
|
+
balance,
|
|
1323
|
+
currency: typeof row.currency === 'string' ? row.currency : 'CNY',
|
|
1324
|
+
// v0.22.243:分项钱包(旧行没有 → NaN,判定时回退总余额)
|
|
1325
|
+
normalBalance: Number.isFinite(normal) ? normal : NaN,
|
|
1326
|
+
bonusBalance: Number.isFinite(bonus) ? bonus : NaN,
|
|
1327
|
+
})
|
|
1328
|
+
} catch {
|
|
1329
|
+
// 坏行跳过,不影响其它行
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
rows.sort((a, b) => (a.at < b.at ? -1 : a.at > b.at ? 1 : 0))
|
|
1333
|
+
return rows
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
/** v0.22.161:**自己采样**——问 DSH 账号服务,把读数追加到**自己的**账本
|
|
1337
|
+
* (DATA_DIR/balance-log.jsonl),不读任何其它插件的文件。
|
|
1338
|
+
* 采样失败静默跳过,绝不写脏行、绝不抛。 */
|
|
1339
|
+
async function sampleBalanceOnce(ctx) {
|
|
1340
|
+
try {
|
|
1341
|
+
const parsed = await balanceFromAccount(ctx)
|
|
1342
|
+
if (!parsed) return null
|
|
1343
|
+
// v0.22.163:明显不合理的读数不落盘(余额 ≤0 或非有限值),避免脏行成为当日基线
|
|
1344
|
+
const amountNum = Number(parsed.amount)
|
|
1345
|
+
if (!Number.isFinite(amountNum) || amountNum <= 0) return null
|
|
1346
|
+
const file = balanceLogPath()
|
|
1347
|
+
await mkdir(dirname(file), { recursive: true })
|
|
1348
|
+
await appendFile(file, `${JSON.stringify({
|
|
1349
|
+
at: parsed.at,
|
|
1350
|
+
balance: parsed.amount,
|
|
1351
|
+
currency: parsed.currency || 'CNY',
|
|
1352
|
+
bonus: parsed.bonus || '',
|
|
1353
|
+
// v0.22.243:分项钱包(可选);旧行没有这两个字段时按总余额降级判定
|
|
1354
|
+
...(parsed.normalBalance !== null && parsed.normalBalance !== undefined
|
|
1355
|
+
? { normalBalance: parsed.normalBalance } : {}),
|
|
1356
|
+
...(parsed.bonusBalance !== null && parsed.bonusBalance !== undefined
|
|
1357
|
+
? { bonusBalance: parsed.bonusBalance } : {}),
|
|
1358
|
+
source: 'workspace-pin',
|
|
1359
|
+
})}\n`, 'utf8')
|
|
1360
|
+
// 采样写入后清缓存,让 /account 立刻看到新值
|
|
1361
|
+
balanceCache = { at: 0, key: '', value: null }
|
|
1362
|
+
return parsed
|
|
1363
|
+
} catch (error) {
|
|
1364
|
+
return null
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
/**
|
|
1369
|
+
* 算出消费快照。
|
|
1370
|
+
* @returns {{baseline, baselineSource, logFirst, logLatest, current, spent, recharge, reason, persist}}
|
|
1371
|
+
*/
|
|
1372
|
+
async function computeCost(state) {
|
|
1373
|
+
const today = localDay()
|
|
1374
|
+
const settings = normalizeSettings(state && state.settings)
|
|
1375
|
+
const stored = settings.costBaseline
|
|
1376
|
+
const rows = await readBalanceRows()
|
|
1377
|
+
const todays = rows.filter(row => localDay(row.at) === today)
|
|
1378
|
+
const logFirst = todays.length > 0 ? todays[0] : null
|
|
1379
|
+
const logLatest = todays.length > 0 ? todays[todays.length - 1] : null
|
|
1380
|
+
|
|
1381
|
+
// v0.22.243:充值判据 = **正常钱包**余额上升(赠送钱包到账不算充值)。
|
|
1382
|
+
// 一次充多次 → 取**最后一次**上升点作新基线;旧数据没有分项字段 → 回退"总余额上升"。
|
|
1383
|
+
// 余额上升是"充值"信号,不是消费;所以重置基线,而不是把当天作废。
|
|
1384
|
+
const walletOf = (row) => (Number.isFinite(row.normalBalance) ? row.normalBalance : row.balance)
|
|
1385
|
+
let baseIdx = 0
|
|
1386
|
+
let rechargeResets = 0
|
|
1387
|
+
for (let i = 1; i < todays.length; i += 1) {
|
|
1388
|
+
if (walletOf(todays[i]) - walletOf(todays[i - 1]) > 0.005) {
|
|
1389
|
+
baseIdx = i
|
|
1390
|
+
rechargeResets += 1
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
let baseline = null
|
|
1395
|
+
let baselineSource = 'none'
|
|
1396
|
+
if (todays.length > 0) {
|
|
1397
|
+
const anchor = todays[baseIdx]
|
|
1398
|
+
baseline = { balance: anchor.balance, at: anchor.at, day: today }
|
|
1399
|
+
baselineSource = baseIdx > 0 ? 'recharge-reset' : 'log-first'
|
|
1400
|
+
} else if (stored && stored.day === today && Number.isFinite(stored.balance)) {
|
|
1401
|
+
baseline = { balance: stored.balance, at: stored.at, day: stored.day }
|
|
1402
|
+
baselineSource = 'settings'
|
|
1403
|
+
} else if (logLatest) {
|
|
1404
|
+
// 当天只有历史读数(例如跨零点后的第一条昨晚记录):拿它当基线,不再写别人的账本
|
|
1405
|
+
baseline = { balance: logLatest.balance, at: logLatest.at, day: today }
|
|
1406
|
+
baselineSource = 'bootstrap'
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
// 需要回写 settings 的情形:没有 / 跨天 / 与日志基线不一致(日志优先)
|
|
1410
|
+
let persist = null
|
|
1411
|
+
if (baseline && (!stored || stored.day !== baseline.day || Math.abs(stored.balance - baseline.balance) > 0.005)) {
|
|
1412
|
+
persist = baseline
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
const current = logLatest ? { balance: logLatest.balance, at: logLatest.at, currency: logLatest.currency } : null
|
|
1416
|
+
let spent = null
|
|
1417
|
+
let recharge = false
|
|
1418
|
+
let reason = 'ok'
|
|
1419
|
+
if (!baseline || !current) {
|
|
1420
|
+
reason = 'no-data'
|
|
1421
|
+
} else if (todays.length <= 1) {
|
|
1422
|
+
// v0.22.161:当天只有 1 条自采记录 → 有基线但没有对照,日支按 0.00 显示(不是"未配置")
|
|
1423
|
+
reason = 'single-sample'
|
|
1424
|
+
spent = 0
|
|
1425
|
+
} else {
|
|
1426
|
+
// v0.22.243:日支 = 基线(充值后第一条)− 最新;负数一律取 0,绝不把充值算成负消费
|
|
1427
|
+
spent = Math.max(0, Math.round((baseline.balance - current.balance) * 100) / 100)
|
|
1428
|
+
recharge = rechargeResets > 0
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
return {
|
|
1432
|
+
cost: {
|
|
1433
|
+
baseline,
|
|
1434
|
+
baselineSource,
|
|
1435
|
+
logFirst: logFirst ? { balance: logFirst.balance, at: logFirst.at } : null,
|
|
1436
|
+
logLatest: logLatest ? { balance: logLatest.balance, at: logLatest.at } : null,
|
|
1437
|
+
current,
|
|
1438
|
+
spent,
|
|
1439
|
+
recharge,
|
|
1440
|
+
rechargeResets, // v0.22.243:当天检测到的充值重置次数(0 = 无充值)
|
|
1441
|
+
reason,
|
|
1442
|
+
day: today,
|
|
1443
|
+
tokensToday: null, // 无官方口径,前端显示 -- 并注明
|
|
1444
|
+
},
|
|
1445
|
+
persist,
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
|
|
1450
|
+
function fail(res, error) {
|
|
1451
|
+
const statusCode = typeof error?.statusCode === 'number' ? error.statusCode : 500
|
|
1452
|
+
if (statusCode >= 500) console.warn(`[workspace-pin] ${error?.stack ?? error}`)
|
|
1453
|
+
sendJson(res, statusCode, { error: error?.message ?? String(error) })
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
/** 到期清理:只在启动跑一次 + 24h 定时器(绝不挂进每轮 pass)。 */
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
export function apply(ctx) {
|
|
1462
|
+
// T-20261130:启动自检(缺声明立刻在 console 暴露,不等用户撞)
|
|
1463
|
+
assertInjectedServices()
|
|
1464
|
+
// T-20261127:启动时把被误剪掉的头像配置按磁盘文件补回(老版本 pruning 造成的丢失)
|
|
1465
|
+
repairAvatarEntriesFromDisk().catch(() => {})
|
|
1466
|
+
// v0.22.161:**自己采样**(启动立刻一次 + 每 5 分钟一次),写自己的 DATA_DIR 账本。
|
|
1467
|
+
// 不读任何其它插件的文件;没有账号服务时静默跳过(前端显示"未配置",不报错)。
|
|
1468
|
+
ctx.effect(() => {
|
|
1469
|
+
let stopped = false
|
|
1470
|
+
let ok = false
|
|
1471
|
+
const timers = []
|
|
1472
|
+
const attempt = async () => {
|
|
1473
|
+
if (stopped || ok) return
|
|
1474
|
+
const parsed = await sampleBalanceOnce(ctx)
|
|
1475
|
+
if (parsed) ok = true
|
|
1476
|
+
}
|
|
1477
|
+
// v0.22.164:冷启动竞态——apply 时 deepseekAccount 可能还没就绪,先密集重试几次
|
|
1478
|
+
void attempt()
|
|
1479
|
+
for (const delay of [5000, 15000, 45000]) {
|
|
1480
|
+
timers.push(setTimeout(() => { void attempt() }, delay))
|
|
1481
|
+
}
|
|
1482
|
+
timers.push(setInterval(() => { void sampleBalanceOnce(ctx) }, SAMPLE_INTERVAL_MS))
|
|
1483
|
+
return () => { stopped = true; for (const t of timers) { clearTimeout(t); clearInterval(t) } }
|
|
1484
|
+
}, 'workspace-pin: balance sampler')
|
|
1485
|
+
// 所有写操作串行化:读状态 → 写状态 → 重排,避免两个点击交错写坏状态文件。
|
|
1486
|
+
let tail = Promise.resolve()
|
|
1487
|
+
const serialize = (task) => {
|
|
1488
|
+
const run = tail.then(task, task)
|
|
1489
|
+
tail = run.then(() => undefined, () => undefined)
|
|
1490
|
+
return run
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
// 拖拽回弹:任何来源把顺序改坏了,都在下一个串行任务里纠正回去。
|
|
1494
|
+
let normalizeQueued = false
|
|
1495
|
+
const scheduleNormalize = () => {
|
|
1496
|
+
if (normalizeQueued) return
|
|
1497
|
+
normalizeQueued = true
|
|
1498
|
+
serialize(async () => {
|
|
1499
|
+
normalizeQueued = false
|
|
1500
|
+
let order = currentOrder(ctx)
|
|
1501
|
+
const state = prune(await readState(), order)
|
|
1502
|
+
const target = layout(order, state)
|
|
1503
|
+
if (!sameOrder(order, target)) order = await applyOrder(ctx, target)
|
|
1504
|
+
// 星标区内部的顺序(用户可能拖动过)回写状态,保证下一次置顶/星标不会打回旧顺序。
|
|
1505
|
+
const pinnedId = state.pinnedWorkspaceId
|
|
1506
|
+
const observed = order.filter(id => state.starredWorkspaces.includes(id) && id !== pinnedId)
|
|
1507
|
+
const recorded = state.starredWorkspaces.filter(id => id !== pinnedId && order.includes(id))
|
|
1508
|
+
if (!sameOrder(observed, recorded)) {
|
|
1509
|
+
const pinnedWasStarred = pinnedId !== undefined && state.starredWorkspaces.includes(pinnedId)
|
|
1510
|
+
await writeState({
|
|
1511
|
+
...state,
|
|
1512
|
+
starredWorkspaces: pinnedWasStarred ? [...observed, pinnedId] : observed,
|
|
1513
|
+
})
|
|
1514
|
+
}
|
|
1515
|
+
}).catch(error => console.warn(`[workspace-pin] normalize failed: ${error?.message ?? error}`))
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
ctx.on('domain/changed', (change) => {
|
|
1519
|
+
if (!change || change.domain !== 'workspace') return
|
|
1520
|
+
if (change.table !== '' || change.operation !== 'put') return
|
|
1521
|
+
scheduleNormalize()
|
|
1522
|
+
})
|
|
1523
|
+
|
|
1524
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1525
|
+
kind: 'exact',
|
|
1526
|
+
path: ROUTE_STATE,
|
|
1527
|
+
handler: async (req, res) => {
|
|
1528
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
1529
|
+
res.writeHead(405, { allow: 'GET, HEAD' })
|
|
1530
|
+
res.end()
|
|
1531
|
+
return
|
|
1532
|
+
}
|
|
1533
|
+
try {
|
|
1534
|
+
const order = currentOrder(ctx)
|
|
1535
|
+
const state = prune(await readState(), order)
|
|
1536
|
+
const payload = snapshot(ctx, state, order)
|
|
1537
|
+
sendJson(res, 200, payload)
|
|
1538
|
+
} catch (error) {
|
|
1539
|
+
fail(res, error)
|
|
1540
|
+
}
|
|
1541
|
+
},
|
|
1542
|
+
}), 'workspace-pin: state route')
|
|
1543
|
+
|
|
1544
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1545
|
+
kind: 'exact',
|
|
1546
|
+
path: ROUTE_PIN,
|
|
1547
|
+
handler: async (req, res) => {
|
|
1548
|
+
if (req.method !== 'POST') {
|
|
1549
|
+
res.writeHead(405, { allow: 'POST' })
|
|
1550
|
+
res.end()
|
|
1551
|
+
return
|
|
1552
|
+
}
|
|
1553
|
+
if (!originAllowed(req)) {
|
|
1554
|
+
res.writeHead(403)
|
|
1555
|
+
res.end('forbidden')
|
|
1556
|
+
return
|
|
1557
|
+
}
|
|
1558
|
+
try {
|
|
1559
|
+
const body = await readBody(req)
|
|
1560
|
+
const raw = body.workspaceId
|
|
1561
|
+
if (raw !== null && raw !== undefined && typeof raw !== 'string') {
|
|
1562
|
+
const error = new Error('expected { workspaceId: string | null }')
|
|
1563
|
+
error.statusCode = 400
|
|
1564
|
+
throw error
|
|
1565
|
+
}
|
|
1566
|
+
const workspaceId = typeof raw === 'string' && raw !== '' ? raw : null
|
|
1567
|
+
sendJson(res, 200, await serialize(() => setPin(ctx, workspaceId)))
|
|
1568
|
+
} catch (error) {
|
|
1569
|
+
fail(res, error)
|
|
1570
|
+
}
|
|
1571
|
+
},
|
|
1572
|
+
}), 'workspace-pin: pin route')
|
|
1573
|
+
|
|
1574
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1575
|
+
kind: 'exact',
|
|
1576
|
+
path: ROUTE_STAR,
|
|
1577
|
+
handler: async (req, res) => {
|
|
1578
|
+
if (req.method !== 'POST') {
|
|
1579
|
+
res.writeHead(405, { allow: 'POST' })
|
|
1580
|
+
res.end()
|
|
1581
|
+
return
|
|
1582
|
+
}
|
|
1583
|
+
if (!originAllowed(req)) {
|
|
1584
|
+
res.writeHead(403)
|
|
1585
|
+
res.end('forbidden')
|
|
1586
|
+
return
|
|
1587
|
+
}
|
|
1588
|
+
try {
|
|
1589
|
+
const body = await readBody(req)
|
|
1590
|
+
if (typeof body.id !== 'string' || body.id === '' || typeof body.starred !== 'boolean') {
|
|
1591
|
+
const error = new Error("expected { kind, id: string, starred: boolean }")
|
|
1592
|
+
error.statusCode = 400
|
|
1593
|
+
throw error
|
|
1594
|
+
}
|
|
1595
|
+
sendJson(res, 200, await serialize(() => setStar(ctx, body.kind, body.id, body.starred)))
|
|
1596
|
+
} catch (error) {
|
|
1597
|
+
fail(res, error)
|
|
1598
|
+
}
|
|
1599
|
+
},
|
|
1600
|
+
}), 'workspace-pin: star route')
|
|
1601
|
+
|
|
1602
|
+
// 排障/脚本入口:按显式顺序重排。违反「置顶/星标」不变式的顺序会被上面的监听纠正回去。
|
|
1603
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1604
|
+
kind: 'exact',
|
|
1605
|
+
path: ROUTE_ORDER,
|
|
1606
|
+
handler: async (req, res) => {
|
|
1607
|
+
if (req.method !== 'POST') {
|
|
1608
|
+
res.writeHead(405, { allow: 'POST' })
|
|
1609
|
+
res.end()
|
|
1610
|
+
return
|
|
1611
|
+
}
|
|
1612
|
+
if (!originAllowed(req)) {
|
|
1613
|
+
res.writeHead(403)
|
|
1614
|
+
res.end('forbidden')
|
|
1615
|
+
return
|
|
1616
|
+
}
|
|
1617
|
+
try {
|
|
1618
|
+
const body = await readBody(req)
|
|
1619
|
+
const requested = Array.isArray(body.workspaceIds) ? body.workspaceIds : null
|
|
1620
|
+
if (requested === null || requested.some(id => typeof id !== 'string')) {
|
|
1621
|
+
const error = new Error('expected { workspaceIds: string[] }')
|
|
1622
|
+
error.statusCode = 400
|
|
1623
|
+
throw error
|
|
1624
|
+
}
|
|
1625
|
+
const result = await serialize(async () => {
|
|
1626
|
+
const order = currentOrder(ctx)
|
|
1627
|
+
const same = requested.length === order.length
|
|
1628
|
+
&& new Set(requested).size === requested.length
|
|
1629
|
+
&& requested.every(id => order.includes(id))
|
|
1630
|
+
if (!same) {
|
|
1631
|
+
const error = new Error('workspaceIds must be a permutation of the current order')
|
|
1632
|
+
error.statusCode = 400
|
|
1633
|
+
throw error
|
|
1634
|
+
}
|
|
1635
|
+
return { workspaceIds: await applyOrder(ctx, requested) }
|
|
1636
|
+
})
|
|
1637
|
+
sendJson(res, 200, result)
|
|
1638
|
+
} catch (error) {
|
|
1639
|
+
fail(res, error)
|
|
1640
|
+
}
|
|
1641
|
+
},
|
|
1642
|
+
}), 'workspace-pin: order route')
|
|
1643
|
+
|
|
1644
|
+
// v1 兼容:老页面若还发 /toggle,等价于 /pin 的置顶/取消。
|
|
1645
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1646
|
+
kind: 'exact',
|
|
1647
|
+
path: ROUTE_TOGGLE,
|
|
1648
|
+
handler: async (req, res) => {
|
|
1649
|
+
if (req.method !== 'POST') {
|
|
1650
|
+
res.writeHead(405, { allow: 'POST' })
|
|
1651
|
+
res.end()
|
|
1652
|
+
return
|
|
1653
|
+
}
|
|
1654
|
+
if (!originAllowed(req)) {
|
|
1655
|
+
res.writeHead(403)
|
|
1656
|
+
res.end('forbidden')
|
|
1657
|
+
return
|
|
1658
|
+
}
|
|
1659
|
+
try {
|
|
1660
|
+
const body = await readBody(req)
|
|
1661
|
+
if (typeof body.workspaceId !== 'string' || body.workspaceId === '' || typeof body.pinned !== 'boolean') {
|
|
1662
|
+
const error = new Error('expected { workspaceId: string, pinned: boolean }')
|
|
1663
|
+
error.statusCode = 400
|
|
1664
|
+
throw error
|
|
1665
|
+
}
|
|
1666
|
+
const workspaceId = body.pinned ? body.workspaceId : null
|
|
1667
|
+
sendJson(res, 200, await serialize(() => setPin(ctx, workspaceId)))
|
|
1668
|
+
} catch (error) {
|
|
1669
|
+
fail(res, error)
|
|
1670
|
+
}
|
|
1671
|
+
},
|
|
1672
|
+
}), 'workspace-pin: legacy toggle route')
|
|
1673
|
+
|
|
1674
|
+
// 调试开关:queue/debug-menu.flag 存在时,浏览器端把菜单结构 dump 到 queue/menu-dump.html。
|
|
1675
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1676
|
+
kind: 'exact',
|
|
1677
|
+
path: ROUTE_DEBUG,
|
|
1678
|
+
handler: async (req, res) => {
|
|
1679
|
+
try {
|
|
1680
|
+
if (req.method === 'GET') {
|
|
1681
|
+
sendJson(res, 200, { enabled: existsSync(DEBUG_FLAG), flag: DEBUG_FLAG, out: DEBUG_OUT })
|
|
1682
|
+
return
|
|
1683
|
+
}
|
|
1684
|
+
if (req.method !== 'POST') {
|
|
1685
|
+
res.writeHead(405, { allow: 'GET, POST' })
|
|
1686
|
+
res.end()
|
|
1687
|
+
return
|
|
1688
|
+
}
|
|
1689
|
+
if (!originAllowed(req)) {
|
|
1690
|
+
res.writeHead(403)
|
|
1691
|
+
res.end('forbidden')
|
|
1692
|
+
return
|
|
1693
|
+
}
|
|
1694
|
+
const body = await readBody(req)
|
|
1695
|
+
if (typeof body.html !== 'string') {
|
|
1696
|
+
const error = new Error('expected { html: string }')
|
|
1697
|
+
error.statusCode = 400
|
|
1698
|
+
throw error
|
|
1699
|
+
}
|
|
1700
|
+
const header = [
|
|
1701
|
+
'<!-- menu dump',
|
|
1702
|
+
'version: ' + String(body.version || 'unknown'),
|
|
1703
|
+
'time: ' + new Date().toISOString(),
|
|
1704
|
+
'mode: ' + String(body.mode || 'unknown'),
|
|
1705
|
+
'items: ' + String(body.items || 0),
|
|
1706
|
+
'url: ' + String(body.url || ''),
|
|
1707
|
+
'-->',
|
|
1708
|
+
].join(' | ')
|
|
1709
|
+
const list = Array.isArray(body.list) ? body.list.join('\n') : String(body.list || '')
|
|
1710
|
+
// 每个候选弹层的摘要:哪几个根、各有多少项、最终选了哪个(排查"抓错节点"用)。
|
|
1711
|
+
const roots = Array.isArray(body.roots) ? body.roots.join('\n') : String(body.roots || '')
|
|
1712
|
+
await mkdir(DATA_DIR, { recursive: true })
|
|
1713
|
+
await writeFile(DEBUG_OUT, header + '\n<!-- #roots -->\n' + roots + '\n<!-- outerHTML(选中的弹层根) -->\n' + body.html + '\n<!-- #items -->\n' + list + '\n', 'utf8')
|
|
1714
|
+
sendJson(res, 200, { written: DEBUG_OUT })
|
|
1715
|
+
} catch (error) {
|
|
1716
|
+
fail(res, error)
|
|
1717
|
+
}
|
|
1718
|
+
},
|
|
1719
|
+
}), 'workspace-pin: debug dump route')
|
|
1720
|
+
// 会话置顶(按工作区唯一):只写插件自己的 map,原生的 pinSession/unpinSession 由浏览器侧同步。
|
|
1721
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1722
|
+
kind: 'exact',
|
|
1723
|
+
path: ROUTE_SESSION_PIN,
|
|
1724
|
+
handler: async (req, res) => {
|
|
1725
|
+
if (req.method !== 'POST') {
|
|
1726
|
+
res.writeHead(405, { allow: 'POST' })
|
|
1727
|
+
res.end()
|
|
1728
|
+
return
|
|
1729
|
+
}
|
|
1730
|
+
if (!originAllowed(req)) {
|
|
1731
|
+
res.writeHead(403)
|
|
1732
|
+
res.end('forbidden')
|
|
1733
|
+
return
|
|
1734
|
+
}
|
|
1735
|
+
try {
|
|
1736
|
+
const body = await readBody(req)
|
|
1737
|
+
if (typeof body.workspaceId !== 'string' || body.workspaceId === '') {
|
|
1738
|
+
const error = new Error('expected { workspaceId: string, sessionId: string | null }')
|
|
1739
|
+
error.statusCode = 400
|
|
1740
|
+
throw error
|
|
1741
|
+
}
|
|
1742
|
+
if (body.sessionId !== null && body.sessionId !== undefined && typeof body.sessionId !== 'string') {
|
|
1743
|
+
const error = new Error('sessionId must be a string or null')
|
|
1744
|
+
error.statusCode = 400
|
|
1745
|
+
throw error
|
|
1746
|
+
}
|
|
1747
|
+
sendJson(res, 200, await serialize(async () => {
|
|
1748
|
+
const order = currentOrder(ctx)
|
|
1749
|
+
if (!order.includes(body.workspaceId)) {
|
|
1750
|
+
const error = new Error(`unknown workspace '${body.workspaceId}'`)
|
|
1751
|
+
error.statusCode = 404
|
|
1752
|
+
throw error
|
|
1753
|
+
}
|
|
1754
|
+
const state = prune(await readState(), order)
|
|
1755
|
+
const map = { ...state.pinnedSessionByWorkspace }
|
|
1756
|
+
if (typeof body.sessionId === 'string' && body.sessionId !== '') map[body.workspaceId] = body.sessionId
|
|
1757
|
+
else delete map[body.workspaceId]
|
|
1758
|
+
const next = { ...state, pinnedSessionByWorkspace: map }
|
|
1759
|
+
await writeState(next)
|
|
1760
|
+
return snapshot(ctx, next, order)
|
|
1761
|
+
}))
|
|
1762
|
+
} catch (error) {
|
|
1763
|
+
fail(res, error)
|
|
1764
|
+
}
|
|
1765
|
+
},
|
|
1766
|
+
}), 'workspace-pin: session pin route')
|
|
1767
|
+
// 设置项:nativeSessionPin / workspaceStarColor / sessionStarColor。
|
|
1768
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1769
|
+
kind: 'exact',
|
|
1770
|
+
path: ROUTE_SETTING,
|
|
1771
|
+
handler: async (req, res) => {
|
|
1772
|
+
if (req.method !== 'POST') {
|
|
1773
|
+
res.writeHead(405, { allow: 'POST' })
|
|
1774
|
+
res.end()
|
|
1775
|
+
return
|
|
1776
|
+
}
|
|
1777
|
+
if (!originAllowed(req)) {
|
|
1778
|
+
res.writeHead(403)
|
|
1779
|
+
res.end('forbidden')
|
|
1780
|
+
return
|
|
1781
|
+
}
|
|
1782
|
+
try {
|
|
1783
|
+
const body = await readBody(req)
|
|
1784
|
+
if (typeof body.key !== 'string' || body.key === '') {
|
|
1785
|
+
const error = new Error('expected { key: string, value: string }')
|
|
1786
|
+
error.statusCode = 400
|
|
1787
|
+
throw error
|
|
1788
|
+
}
|
|
1789
|
+
sendJson(res, 200, await serialize(() => setSetting(ctx, body.key, body.value)))
|
|
1790
|
+
} catch (error) {
|
|
1791
|
+
fail(res, error)
|
|
1792
|
+
}
|
|
1793
|
+
},
|
|
1794
|
+
}), 'workspace-pin: setting route')
|
|
1795
|
+
|
|
1796
|
+
// 素材:GET ?name=xxx 取图(<img>/background 用);POST 收裁切后的 data URL 落盘。
|
|
1797
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1798
|
+
kind: 'exact',
|
|
1799
|
+
path: ROUTE_ASSET,
|
|
1800
|
+
handler: async (req, res) => {
|
|
1801
|
+
try {
|
|
1802
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
1803
|
+
let name = ''
|
|
1804
|
+
try {
|
|
1805
|
+
name = new URL(req.url || '/', 'http://127.0.0.1').searchParams.get('name') || ''
|
|
1806
|
+
} catch {
|
|
1807
|
+
name = ''
|
|
1808
|
+
}
|
|
1809
|
+
await sendAsset(res, name)
|
|
1810
|
+
return
|
|
1811
|
+
}
|
|
1812
|
+
if (req.method !== 'POST') {
|
|
1813
|
+
res.writeHead(405, { allow: 'GET, HEAD, POST' })
|
|
1814
|
+
res.end()
|
|
1815
|
+
return
|
|
1816
|
+
}
|
|
1817
|
+
if (!originAllowed(req)) {
|
|
1818
|
+
res.writeHead(403)
|
|
1819
|
+
res.end('forbidden')
|
|
1820
|
+
return
|
|
1821
|
+
}
|
|
1822
|
+
const body = await readBody(req, MAX_ASSET_BYTES + MAX_BODY_BYTES)
|
|
1823
|
+
if (['sidebar', 'ws', 'se', 'profile'].indexOf(body.kind) === -1) {
|
|
1824
|
+
const error = new Error("expected kind 'sidebar' | 'ws' | 'se'")
|
|
1825
|
+
error.statusCode = 400
|
|
1826
|
+
throw error
|
|
1827
|
+
}
|
|
1828
|
+
const saved = await serialize(() => saveAsset(body.kind, body.id, body.dataUrl))
|
|
1829
|
+
sendJson(res, 200, saved)
|
|
1830
|
+
} catch (error) {
|
|
1831
|
+
fail(res, error)
|
|
1832
|
+
}
|
|
1833
|
+
},
|
|
1834
|
+
}), 'workspace-pin: asset route')
|
|
1835
|
+
|
|
1836
|
+
// T-20261127(CommanderT:把当前背景设为默认):包内自带的默认侧栏背景,新装用户与「返回默认背景」都走这里。
|
|
1837
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1838
|
+
kind: 'exact',
|
|
1839
|
+
path: ROUTE_DEFAULT_BG,
|
|
1840
|
+
handler: async (req, res) => {
|
|
1841
|
+
try {
|
|
1842
|
+
const file = new URL('./assets/backgrounds/sidebar-default.jpg', import.meta.url)
|
|
1843
|
+
const body = await readFile(file)
|
|
1844
|
+
res.writeHead(200, { 'content-type': 'image/jpeg', 'cache-control': 'public, max-age=86400' })
|
|
1845
|
+
res.end(body)
|
|
1846
|
+
} catch (error) {
|
|
1847
|
+
fail(res, error)
|
|
1848
|
+
}
|
|
1849
|
+
},
|
|
1850
|
+
}), 'workspace-pin: default background')
|
|
1851
|
+
|
|
1852
|
+
|
|
1853
|
+
// v0.22.43:分块上传。dsh-app:// 自定义协议下**大请求体的响应回不来**(请求到、盘写成功、fetch 挂起),
|
|
1854
|
+
// 但小 body(如 /setting)正常 → 把 dataUrl 切成 <2KB 的片段逐段 POST,最后一片落盘并返回 {name,path}。
|
|
1855
|
+
const uploads = new Map()
|
|
1856
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1857
|
+
kind: 'exact',
|
|
1858
|
+
path: '/workspace-pin/asset-chunk',
|
|
1859
|
+
handler: async (req, res) => {
|
|
1860
|
+
const started = Date.now()
|
|
1861
|
+
try {
|
|
1862
|
+
if (req.method !== 'POST') { res.writeHead(405); res.end(); return }
|
|
1863
|
+
const body = await readBody(req, MAX_BODY_BYTES)
|
|
1864
|
+
const id = typeof body.uploadId === 'string' ? body.uploadId : ''
|
|
1865
|
+
const seq = Number(body.seq)
|
|
1866
|
+
const total = Number(body.total)
|
|
1867
|
+
if (id === '' || !Number.isInteger(seq) || !Number.isInteger(total) || seq < 0 || seq >= total) {
|
|
1868
|
+
const error = new Error('expected { uploadId, seq, total, chunk }')
|
|
1869
|
+
error.statusCode = 400
|
|
1870
|
+
throw error
|
|
1871
|
+
}
|
|
1872
|
+
let entry = uploads.get(id)
|
|
1873
|
+
if (!entry) {
|
|
1874
|
+
entry = { parts: new Array(total).fill(''), received: 0, at: Date.now(), kind: body.kind, assetId: body.id, mime: body.mime }
|
|
1875
|
+
uploads.set(id, entry)
|
|
1876
|
+
}
|
|
1877
|
+
if (typeof body.chunk === 'string') { entry.parts[seq] = body.chunk; entry.received += 1 }
|
|
1878
|
+
entry.at = Date.now()
|
|
1879
|
+
for (const [key, value] of uploads) { if (Date.now() - value.at > 10 * 60 * 1000) uploads.delete(key) }
|
|
1880
|
+
if (entry.received < total) { sendJson(res, 200, { received: entry.received, total }); return }
|
|
1881
|
+
uploads.delete(id)
|
|
1882
|
+
const dataUrl = 'data:' + (entry.mime || 'image/png') + ';base64,' + entry.parts.join('')
|
|
1883
|
+
const saved = await serialize(() => saveAsset(entry.kind, entry.assetId, dataUrl))
|
|
1884
|
+
sendJson(res, 200, saved)
|
|
1885
|
+
} catch (error) {
|
|
1886
|
+
fail(res, error)
|
|
1887
|
+
} finally {
|
|
1888
|
+
logHostRequest(req, res, started)
|
|
1889
|
+
}
|
|
1890
|
+
},
|
|
1891
|
+
}), 'workspace-pin: asset chunk route')
|
|
1892
|
+
// v0.22.44:版本自检——页面拿磁盘版本和自身 CLIENT_VERSION 比较,不一致就自动重载一次
|
|
1893
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1894
|
+
kind: 'exact',
|
|
1895
|
+
path: ROUTE_VERSION,
|
|
1896
|
+
handler: async (req, res) => {
|
|
1897
|
+
try {
|
|
1898
|
+
if (req.method !== 'GET') { res.writeHead(405); res.end(); return }
|
|
1899
|
+
// v0.22.480:四级兜底解析版本(npm 安装后 import.meta.url 可能读不到 package.json)
|
|
1900
|
+
let version = 'unknown'
|
|
1901
|
+
let source = 'none'
|
|
1902
|
+
const tried = []
|
|
1903
|
+
// a) 与 index.js 同目录的 package.json
|
|
1904
|
+
try {
|
|
1905
|
+
const pkgPath = new URL('./package.json', import.meta.url)
|
|
1906
|
+
tried.push(String(pkgPath))
|
|
1907
|
+
const v = JSON.parse(await readFile(pkgPath, 'utf8')).version
|
|
1908
|
+
if (v) { version = String(v); source = 'pkg' }
|
|
1909
|
+
} catch (error) { tried.push('a-fail:' + String(error && error.code || error && error.message)) }
|
|
1910
|
+
// b) require.resolve(被 exports 拦时退一步:解析 index.js 再拼 package.json)
|
|
1911
|
+
if (source === 'none') {
|
|
1912
|
+
try {
|
|
1913
|
+
const require2 = createRequire(import.meta.url)
|
|
1914
|
+
let p = null
|
|
1915
|
+
try { p = require2.resolve('@weitc/dfy-awesome-sidebar/package.json') } catch (e) { p = null }
|
|
1916
|
+
if (!p) { p = join(dirname(require2.resolve('@weitc/dfy-awesome-sidebar')), 'package.json') }
|
|
1917
|
+
tried.push(String(p))
|
|
1918
|
+
const v = JSON.parse(await readFile(p, 'utf8')).version
|
|
1919
|
+
if (v) { version = String(v); source = 'require' }
|
|
1920
|
+
} catch (error) { tried.push('b-fail:' + String(error && error.code || error && error.message)) }
|
|
1921
|
+
}
|
|
1922
|
+
// c) npm/pnpm 注入的环境变量
|
|
1923
|
+
if (source === 'none' && process.env.npm_package_version) {
|
|
1924
|
+
version = String(process.env.npm_package_version)
|
|
1925
|
+
source = 'env'
|
|
1926
|
+
}
|
|
1927
|
+
// d) 打包时常量
|
|
1928
|
+
if (source === 'none' && BUNDLE_VERSION && BUNDLE_VERSION !== '') {
|
|
1929
|
+
version = BUNDLE_VERSION
|
|
1930
|
+
source = 'const'
|
|
1931
|
+
}
|
|
1932
|
+
if (source === 'none') {
|
|
1933
|
+
try { console.warn('[workspace-pin] version resolve failed, tried: ' + tried.join(' | ')) } catch (e) { /* 忽略 */ }
|
|
1934
|
+
}
|
|
1935
|
+
sendJson(res, 200, { version, source, at: new Date().toISOString() })
|
|
1936
|
+
} catch (error) {
|
|
1937
|
+
fail(res, error)
|
|
1938
|
+
}
|
|
1939
|
+
},
|
|
1940
|
+
}), 'workspace-pin: version route')
|
|
1941
|
+
// v0.22.45:upload-trace 独立出口(不与 opacity-diag/menu-dump 共用文件,避免互相覆盖)
|
|
1942
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1943
|
+
kind: 'exact',
|
|
1944
|
+
path: '/workspace-pin/trace',
|
|
1945
|
+
handler: async (req, res) => {
|
|
1946
|
+
try {
|
|
1947
|
+
if (req.method !== 'POST') { res.writeHead(405); res.end(); return }
|
|
1948
|
+
const body = await readBody(req)
|
|
1949
|
+
const list = Array.isArray(body.list) ? body.list.join('\n') : String(body.list || '')
|
|
1950
|
+
const header = '<!-- upload trace | time: ' + new Date().toISOString() + ' | version: ' + String(body.version || 'unknown') + ' -->'
|
|
1951
|
+
await mkdir(DATA_DIR, { recursive: true })
|
|
1952
|
+
await writeFile(UPLOAD_TRACE, header + '\n\n' + list + '\n', 'utf8')
|
|
1953
|
+
sendJson(res, 200, { written: UPLOAD_TRACE })
|
|
1954
|
+
} catch (error) {
|
|
1955
|
+
fail(res, error)
|
|
1956
|
+
}
|
|
1957
|
+
},
|
|
1958
|
+
}), 'workspace-pin: trace route')
|
|
1959
|
+
|
|
1960
|
+
// 底部账号区右侧的余额 / 今日 token(读不到就返回 null,前端显示 --)
|
|
1961
|
+
ctx.effect(() => ctx.webServer.register({
|
|
1962
|
+
kind: 'exact',
|
|
1963
|
+
path: ROUTE_ACCOUNT,
|
|
1964
|
+
handler: async (req, res) => {
|
|
1965
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
1966
|
+
res.writeHead(405, { allow: 'GET, HEAD' })
|
|
1967
|
+
res.end()
|
|
1968
|
+
return
|
|
1969
|
+
}
|
|
1970
|
+
try {
|
|
1971
|
+
// T-01:余额 + 今日消费(余额差值,基线按自然日、日志优先)
|
|
1972
|
+
const order = currentOrder(ctx)
|
|
1973
|
+
const result = await serialize(async () => {
|
|
1974
|
+
const state = prune(await readState(), order)
|
|
1975
|
+
const { cost, persist } = await computeCost(state)
|
|
1976
|
+
let next = cost
|
|
1977
|
+
if (persist) {
|
|
1978
|
+
const nextState = {
|
|
1979
|
+
...state,
|
|
1980
|
+
settings: { ...normalizeSettings(state.settings), costBaseline: persist },
|
|
1981
|
+
}
|
|
1982
|
+
await writeState(nextState)
|
|
1983
|
+
}
|
|
1984
|
+
return next
|
|
1985
|
+
})
|
|
1986
|
+
// v0.22.159:优先账号服务(跨机器可用),失败再退本地账本;两者都没有 → null
|
|
1987
|
+
let balance = await balanceFromAccount(ctx)
|
|
1988
|
+
if (!balance) balance = await readBalance()
|
|
1989
|
+
// T-20261127:附带「余额日志是否配置」,前端据此显示「未配置」而不是「--」
|
|
1990
|
+
const logConfigured = await balanceLogConfigured()
|
|
1991
|
+
sendJson(res, 200, { balance, balanceSource: balance ? balance.source : null, tokensToday: todayTokens(), cost: result, logConfigured })
|
|
1992
|
+
} catch (error) {
|
|
1993
|
+
fail(res, error)
|
|
1994
|
+
}
|
|
1995
|
+
},
|
|
1996
|
+
}), 'workspace-pin: account route')
|
|
1997
|
+
|
|
1998
|
+
// 会话最后活动时间(给工作区 all-stale 判定;只读 + 30s 缓存)
|
|
1999
|
+
ctx.effect(() => ctx.webServer.register({
|
|
2000
|
+
kind: 'exact',
|
|
2001
|
+
path: ROUTE_ACTIVITY,
|
|
2002
|
+
handler: async (req, res) => {
|
|
2003
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
2004
|
+
res.writeHead(405, { allow: 'GET, HEAD' })
|
|
2005
|
+
res.end()
|
|
2006
|
+
return
|
|
2007
|
+
}
|
|
2008
|
+
try {
|
|
2009
|
+
const cache = await readSessionActivity()
|
|
2010
|
+
sendJson(res, 200, { at: cache.at, sessions: cache.map })
|
|
2011
|
+
} catch (error) {
|
|
2012
|
+
fail(res, error)
|
|
2013
|
+
}
|
|
2014
|
+
},
|
|
2015
|
+
}), 'workspace-pin: activity route')
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
/**
|
|
2019
|
+
* T-20261127:把「被旧版剪掉的头像配置」按磁盘上仍存在的文件补回来。
|
|
2020
|
+
* 旧版写设置时按 existsSync 剪条目;资产一旦被误删,配置就永久丢失,这里按资产目录重建。
|
|
2021
|
+
* 只在某些 id 当前没有条目时补,不覆盖用户后来新设置的。
|
|
2022
|
+
*/
|
|
2023
|
+
async function repairAvatarEntriesFromDisk() {
|
|
2024
|
+
try {
|
|
2025
|
+
// 与保存/取图共用同一个 assetDir()(尊重 DSH_HOME),不要在这里另拼路径
|
|
2026
|
+
const dir = assetDir()
|
|
2027
|
+
if (!existsSync(dir)) return
|
|
2028
|
+
const names = await readdir(dir)
|
|
2029
|
+
const wsMap = {}
|
|
2030
|
+
const seMap = {}
|
|
2031
|
+
for (const item of names) {
|
|
2032
|
+
const name = String(item)
|
|
2033
|
+
const raw = name.match(/^(ws|se)-([0-9a-fA-F-]{36})(?:-[a-z0-9]+)?\.(png|jpg)$/)
|
|
2034
|
+
if (!raw) continue
|
|
2035
|
+
const full = join(dir, name)
|
|
2036
|
+
try { const st = await stat(full); if (!st.isFile()) continue } catch { continue }
|
|
2037
|
+
const target = raw[1].toLowerCase() === 'ws' ? wsMap : seMap
|
|
2038
|
+
target[raw[2]] = full
|
|
2039
|
+
}
|
|
2040
|
+
if (Object.keys(wsMap).length === 0 && Object.keys(seMap).length === 0) return
|
|
2041
|
+
const state = await readState()
|
|
2042
|
+
const avatar = normalizeAvatar(state.settings && state.settings.avatar)
|
|
2043
|
+
let changed = false
|
|
2044
|
+
for (const id of Object.keys(wsMap)) { if (!avatar.workspace[id]) { avatar.workspace[id] = wsMap[id]; changed = true } }
|
|
2045
|
+
for (const id of Object.keys(seMap)) { if (!avatar.session[id]) { avatar.session[id] = seMap[id]; changed = true } }
|
|
2046
|
+
if (!changed) return
|
|
2047
|
+
const next = { ...state, settings: { ...normalizeSettings(state.settings), avatar } }
|
|
2048
|
+
await writeState(next)
|
|
2049
|
+
} catch (error) { /* 修复失败不影响启动 */ }
|
|
2015
2050
|
}
|