dsh-gallery 0.7.2 → 0.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +104 -0
- package/client.js +482 -15
- package/cordis.patch.yml +5 -1
- package/index.js +1531 -1187
- package/package.json +3 -2
- package/session-index.js +187 -0
package/index.js
CHANGED
|
@@ -1,1187 +1,1531 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 产出画廊 —— Host 半部。
|
|
3
|
-
*
|
|
4
|
-
* 负责三件事:
|
|
5
|
-
* 1. 解析「产出根目录」:运行时配置 > YAML 配置 > 自动探测(三级优先,见 resolveOptions);
|
|
6
|
-
* 2. 扫描根目录下最近 N 小时的图片 / 文件(深度受限、跳过批量目录、有扫描预算);
|
|
7
|
-
* 3. 通过 /api 的 exact Fetch 路由把「列表」「原始字节」「设置」交给浏览器半部。
|
|
8
|
-
*
|
|
9
|
-
* 缩略图压缩在客户端用 canvas 完成(本文件不生成缩略图,只给原始字节)。
|
|
10
|
-
*/
|
|
11
|
-
import { promises as fsp, readFileSync, statSync } from 'node:fs'
|
|
12
|
-
import os from 'node:os'
|
|
13
|
-
import path from 'node:path'
|
|
14
|
-
import { zstdDecompressSync } from 'node:zlib'
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
'
|
|
45
|
-
'
|
|
46
|
-
'
|
|
47
|
-
'
|
|
48
|
-
'
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
path.join(home, '
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
'
|
|
141
|
-
'
|
|
142
|
-
'.
|
|
143
|
-
'.
|
|
144
|
-
'
|
|
145
|
-
'
|
|
146
|
-
'
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
'
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
function
|
|
290
|
-
if (buf.length <
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (
|
|
303
|
-
p +=
|
|
304
|
-
continue
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
const width =
|
|
336
|
-
const height =
|
|
337
|
-
return width > 0 && height > 0 ? { width, height } : null
|
|
338
|
-
}
|
|
339
|
-
if (chunk === '
|
|
340
|
-
const
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
*/
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
const
|
|
468
|
-
if (
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
const
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
if (
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
try {
|
|
558
|
-
const
|
|
559
|
-
if (
|
|
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
|
-
const
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
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
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
const
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
}
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
}
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
if (
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 产出画廊 —— Host 半部。
|
|
3
|
+
*
|
|
4
|
+
* 负责三件事:
|
|
5
|
+
* 1. 解析「产出根目录」:运行时配置 > YAML 配置 > 自动探测(三级优先,见 resolveOptions);
|
|
6
|
+
* 2. 扫描根目录下最近 N 小时的图片 / 文件(深度受限、跳过批量目录、有扫描预算);
|
|
7
|
+
* 3. 通过 /api 的 exact Fetch 路由把「列表」「原始字节」「设置」交给浏览器半部。
|
|
8
|
+
*
|
|
9
|
+
* 缩略图压缩在客户端用 canvas 完成(本文件不生成缩略图,只给原始字节)。
|
|
10
|
+
*/
|
|
11
|
+
import { promises as fsp, readFileSync, statSync } from 'node:fs'
|
|
12
|
+
import os from 'node:os'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
import { zstdDecompressSync } from 'node:zlib'
|
|
15
|
+
import { buildSessionIndex, clearSessionIndexCache } from './session-index.js'
|
|
16
|
+
|
|
17
|
+
/** 路由:一个 POST 通道,op 区分 list / raw / reveal / settings。exact Fetch 路由必须以 / 开头。 */
|
|
18
|
+
export const ROUTE = '/api/dsh-gallery'
|
|
19
|
+
|
|
20
|
+
/** DSH 主目录:优先环境变量,回退 ~/.dsh。 */
|
|
21
|
+
const DSH_HOME = process.env.DSH_HOME ?? path.join(os.homedir(), '.dsh')
|
|
22
|
+
|
|
23
|
+
/** 会话日志根目录(DSH 主目录下的 sessions)。 */
|
|
24
|
+
const DEFAULT_SESSIONS_DIR = path.join(DSH_HOME, 'sessions')
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 运行时配置文件的默认位置。
|
|
28
|
+
* 用户可在面板内直接改产出根目录,改动写这里;
|
|
29
|
+
* 它的优先级高于 cordis.patch.yml,低于用户显式传入的 root。
|
|
30
|
+
*/
|
|
31
|
+
const DEFAULT_SETTINGS_FILE = path.join(DSH_HOME, 'dsh-gallery.json')
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 会话日志里,哪些工具调用的参数算「本对话的产出」。
|
|
35
|
+
*
|
|
36
|
+
* 分三类:
|
|
37
|
+
* 1. **直接写文件**:write / edit / present 等 —— 路径一定是产出。
|
|
38
|
+
* 2. **脚本产出**:Python/命令行脚本跑出来的图,不在 write 调用里,
|
|
39
|
+
* 但 Agent 一定会用 read_image 打开它看效果 —— 所以「看过的图」也算产出。
|
|
40
|
+
* 这是「过程中即时可见」的关键:不然脚本生成的中间图和成品图都看不见。
|
|
41
|
+
* 3. **不看文件**:read / grep / glob 等纯读取,不算产出(避免把别人的文件算进来)。
|
|
42
|
+
*/
|
|
43
|
+
const OUTPUT_TOOLS = new Set([
|
|
44
|
+
'write',
|
|
45
|
+
'edit',
|
|
46
|
+
'present',
|
|
47
|
+
'dsh_im_return_file',
|
|
48
|
+
'ops_write_file',
|
|
49
|
+
'ops_edit_file',
|
|
50
|
+
])
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 「Agent 看过的图片」也算本对话产出。
|
|
54
|
+
* 只对图片生效(read_image 的参数就是图片路径),风险低:
|
|
55
|
+
* Agent 不会无缘无故打开一张与任务无关的图。
|
|
56
|
+
*/
|
|
57
|
+
const VIEW_TOOLS = new Set(['read_image'])
|
|
58
|
+
|
|
59
|
+
/** 只从 VIEW_TOOLS 里接受的图片扩展名。 */
|
|
60
|
+
const VIEW_IMAGE_EXTS = new Set(['.png', '.jpg', '.jpeg', '.webp', '.bmp', '.gif', '.avif', '.tif', '.tiff'])
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 命令执行类工具。它参数里的路径只作为「弱证据」:
|
|
64
|
+
* 出现在命令文本里 → 该文件是被处理过的,不算「纯输入」。
|
|
65
|
+
*/
|
|
66
|
+
const BASH_TOOLS = new Set(['ops_bash', 'pwsh', 'bash'])
|
|
67
|
+
|
|
68
|
+
/** zstd 帧起始魔数(小端 0x28B52FFD)。一个 .zstd 文件可能由多帧拼接而成。 */
|
|
69
|
+
const ZSTD_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd])
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 自动探测产出根目录(当用户既没配 root、也没有运行时配置时使用)。
|
|
73
|
+
*
|
|
74
|
+
* 策略:先找相对用户主目录的常见位置,再遍历可用盘符找 \DSH产出。
|
|
75
|
+
* 关键点:**代码里不出现任何写死的盘符**(是用循环遍历 A-Z 探测),
|
|
76
|
+
* 所以发布到别人机器上不会指向一个必然存在的错误路径,同时又能自动找到
|
|
77
|
+
* 装在其他盘上的产出目录。
|
|
78
|
+
*
|
|
79
|
+
* 探测结果会缓存 —— detectDefaultRoot 在每次请求的 resolveOptions 里都会被调用,
|
|
80
|
+
* 而遍历盘符涉及多次 stat,没必要每次都做。
|
|
81
|
+
*/
|
|
82
|
+
let defaultRootCache = null
|
|
83
|
+
|
|
84
|
+
function detectDefaultRoot() {
|
|
85
|
+
if (defaultRootCache !== null) return defaultRootCache
|
|
86
|
+
|
|
87
|
+
const home = os.homedir()
|
|
88
|
+
const candidates = [
|
|
89
|
+
// 1. 用户主目录下
|
|
90
|
+
path.join(home, 'DSH产出'),
|
|
91
|
+
path.join(home, 'DSH-outputs'),
|
|
92
|
+
// 2. 文档下
|
|
93
|
+
path.join(home, 'Documents', 'DSH产出'),
|
|
94
|
+
path.join(home, 'Documents', 'DSH-outputs'),
|
|
95
|
+
// 3. 桌面下
|
|
96
|
+
path.join(home, 'Desktop', 'DSH产出'),
|
|
97
|
+
// 4. 下载目录下
|
|
98
|
+
path.join(home, 'Downloads', 'DSH产出'),
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
// 5. 遍历可用盘符(Windows):C:\DSH产出、D:\DSH产出 …… 不写死任何字母
|
|
102
|
+
if (process.platform === 'win32') {
|
|
103
|
+
for (let code = 67; code <= 90; code += 1) { // C(67) → Z(90)
|
|
104
|
+
const drive = String.fromCharCode(code) + ':'
|
|
105
|
+
candidates.push(path.join(drive + path.sep, 'DSH产出'))
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
for (const candidate of candidates) {
|
|
110
|
+
try {
|
|
111
|
+
if (statSync(candidate).isDirectory()) {
|
|
112
|
+
defaultRootCache = candidate
|
|
113
|
+
return candidate
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
// 不存在就试下一个
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 全都不存在:返回第一个候选,让面板显示「目录不存在」并给出修改入口
|
|
121
|
+
defaultRootCache = candidates[0]
|
|
122
|
+
return defaultRootCache
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const DEFAULTS = {
|
|
126
|
+
// root 不在默认值里写死,由 resolveOptions 三级优先推导
|
|
127
|
+
// 扫描时间窗口(小时)。默认一年:全局资产要能覆盖历史对话,
|
|
128
|
+
// 否则「按对话分组」看不到旧成果。24 小时会把历史全滤掉。
|
|
129
|
+
windowHours: 24 * 365,
|
|
130
|
+
maxItems: 60,
|
|
131
|
+
maxDepth: 3,
|
|
132
|
+
extensions: ['.png', '.jpg', '.jpeg', '.webp'],
|
|
133
|
+
/**
|
|
134
|
+
* 文件画廊:只放「产出文件」——文档、代码、数据、压缩包、链接。
|
|
135
|
+
* 不收任何图片格式(图片归图片画廊),也不收音视频(体积大且非文档产出)。
|
|
136
|
+
*/
|
|
137
|
+
fileExtensions: [
|
|
138
|
+
'.md', '.markdown', '.txt', '.log',
|
|
139
|
+
'.json', '.jsonl', '.csv', '.tsv', '.yaml', '.yml', '.xml', '.toml',
|
|
140
|
+
'.pdf', '.docx', '.doc', '.xlsx', '.xls', '.pptx', '.ppt', '.rtf', '.odt',
|
|
141
|
+
'.html', '.htm', '.mht',
|
|
142
|
+
'.zip', '.7z', '.rar', '.tar', '.gz',
|
|
143
|
+
'.py', '.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx',
|
|
144
|
+
'.ps1', '.sh', '.bat', '.cmd', '.sql', '.ipynb',
|
|
145
|
+
'.url', '.lnk', '.webloc',
|
|
146
|
+
'.dxf', '.dwg', '.skp', '.3dm',
|
|
147
|
+
],
|
|
148
|
+
skipDirs: [
|
|
149
|
+
'library_merged',
|
|
150
|
+
'node_modules',
|
|
151
|
+
'.git',
|
|
152
|
+
'.svn',
|
|
153
|
+
'$RECYCLE.BIN',
|
|
154
|
+
'System Volume Information',
|
|
155
|
+
'__pycache__',
|
|
156
|
+
'.venv',
|
|
157
|
+
'venv',
|
|
158
|
+
'.cache',
|
|
159
|
+
],
|
|
160
|
+
maxDirectories: 4000,
|
|
161
|
+
maxEntries: 60000,
|
|
162
|
+
maxImageBytes: 64 * 1024 * 1024,
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** 读取运行时配置文件(<DSH_HOME>/dsh-gallery.json);不存在或损坏返回空对象。 */
|
|
166
|
+
function readSettingsFile(file) {
|
|
167
|
+
try {
|
|
168
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'))
|
|
169
|
+
return parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {}
|
|
170
|
+
} catch {
|
|
171
|
+
return {}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 读取「已隐藏」的相对路径集合。
|
|
177
|
+
* 隐藏只记录路径,不移动/删除文件;列表扫描时按此过滤。
|
|
178
|
+
* @param settingsFile - 配置文件路径。
|
|
179
|
+
* @returns 规范化(正斜杠)后的相对路径数组。
|
|
180
|
+
*/
|
|
181
|
+
async function readHiddenList(settingsFile) {
|
|
182
|
+
const stored = readSettingsFile(settingsFile)
|
|
183
|
+
if (!Array.isArray(stored.hidden)) return []
|
|
184
|
+
return stored.hidden
|
|
185
|
+
.filter((p) => typeof p === 'string' && p !== '')
|
|
186
|
+
.map((p) => p.replace(/\\/g, '/'))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 解析最终配置。扫描目录(roots)的优先级(高 → 低):
|
|
191
|
+
* 1. 运行时配置文件的 roots 数组(面板内可加多个目录)
|
|
192
|
+
* 2. 运行时配置文件的 root(旧版单目录写法,兼容保留)
|
|
193
|
+
* 3. cordis.patch.yml 里配的 root / roots
|
|
194
|
+
* 4. 自动探测(detectDefaultRoot)
|
|
195
|
+
*
|
|
196
|
+
* 为什么支持多目录:DSH 一个会话一个 cwd,产出可能写在任意工作区下
|
|
197
|
+
* (例如 G:\DSH任务文件\xxx),不一定落在 G:\DSH产出。只配一个目录必然漏。
|
|
198
|
+
*/
|
|
199
|
+
function resolveOptions(config) {
|
|
200
|
+
const merged = { ...DEFAULTS, ...(config ?? {}) }
|
|
201
|
+
const settingsFile = String(merged.settingsFile ?? DEFAULT_SETTINGS_FILE)
|
|
202
|
+
const stored = readSettingsFile(settingsFile)
|
|
203
|
+
|
|
204
|
+
// 收集所有候选目录
|
|
205
|
+
const collected = []
|
|
206
|
+
const pushRoot = (value) => {
|
|
207
|
+
if (typeof value === 'string' && value.trim() !== '') collected.push(value.trim())
|
|
208
|
+
}
|
|
209
|
+
const pushList = (value) => {
|
|
210
|
+
if (Array.isArray(value)) for (const v of value) pushRoot(v)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
pushList(stored.roots) // 1. 设置里的多目录
|
|
214
|
+
pushRoot(stored.root) // 2. 设置里的单目录(旧格式)
|
|
215
|
+
pushList(merged.roots) // 3. YAML 多目录
|
|
216
|
+
pushRoot(merged.root) // 4. YAML 单目录
|
|
217
|
+
|
|
218
|
+
// 去重(Windows 路径大小写不敏感)
|
|
219
|
+
const deduped = []
|
|
220
|
+
const seenRoots = new Set()
|
|
221
|
+
const candidates = collected.length > 0 ? collected : [detectDefaultRoot()]
|
|
222
|
+
for (const r of candidates) {
|
|
223
|
+
const abs = path.resolve(r)
|
|
224
|
+
const key = abs.toLowerCase()
|
|
225
|
+
if (seenRoots.has(key)) continue
|
|
226
|
+
seenRoots.add(key)
|
|
227
|
+
deduped.push(abs)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const resolvedRoots = deduped.length > 0 ? deduped : [path.resolve(detectDefaultRoot())]
|
|
231
|
+
const rootSource = collected.length === 0
|
|
232
|
+
? 'auto-detect'
|
|
233
|
+
: (typeof stored.root === 'string' || (Array.isArray(stored.roots) && stored.roots.length > 0) ? 'settings' : 'config')
|
|
234
|
+
|
|
235
|
+
return {
|
|
236
|
+
root: resolvedRoots[0],
|
|
237
|
+
roots: resolvedRoots,
|
|
238
|
+
rootSource,
|
|
239
|
+
settingsFile,
|
|
240
|
+
windowHours: numberOr(merged.windowHours, DEFAULTS.windowHours, 1, 24 * 366),
|
|
241
|
+
maxItems: numberOr(merged.maxItems, DEFAULTS.maxItems, 1, 500),
|
|
242
|
+
maxDepth: numberOr(merged.maxDepth, DEFAULTS.maxDepth, 0, 8),
|
|
243
|
+
extensions: arrayOfStrings(merged.extensions, DEFAULTS.extensions).map((e) => e.toLowerCase()),
|
|
244
|
+
fileExtensions: arrayOfStrings(merged.fileExtensions, DEFAULTS.fileExtensions).map((e) => e.toLowerCase()),
|
|
245
|
+
skipDirs: arrayOfStrings(merged.skipDirs, DEFAULTS.skipDirs).map((e) => e.toLowerCase()),
|
|
246
|
+
maxDirectories: numberOr(merged.maxDirectories, DEFAULTS.maxDirectories, 1, 200000),
|
|
247
|
+
maxEntries: numberOr(merged.maxEntries, DEFAULTS.maxEntries, 1, 1000000),
|
|
248
|
+
maxImageBytes: numberOr(merged.maxImageBytes, DEFAULTS.maxImageBytes, 1, 512 * 1024 * 1024),
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function numberOr(value, fallback, min, max) {
|
|
253
|
+
const n = typeof value === 'number' ? value : Number(value)
|
|
254
|
+
if (!Number.isFinite(n)) return fallback
|
|
255
|
+
return Math.min(Math.max(Math.trunc(n), min), max)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function arrayOfStrings(value, fallback) {
|
|
259
|
+
if (!Array.isArray(value)) return [...fallback]
|
|
260
|
+
const cleaned = value.filter((entry) => typeof entry === 'string' && entry.trim() !== '').map((entry) => entry.trim())
|
|
261
|
+
return cleaned.length > 0 ? cleaned : [...fallback]
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Windows 路径比较统一小写;用于「必须在根目录内」的判定。 */
|
|
265
|
+
function isInside(root, target) {
|
|
266
|
+
const a = root.toLowerCase().replace(/[\\/]+$/, '')
|
|
267
|
+
const b = target.toLowerCase().replace(/[\\/]+$/, '')
|
|
268
|
+
return b === a || b.startsWith(a + '\\') || b.startsWith(a + '/')
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** 只读图片头部若干字节,用于解析尺寸;失败返回 null。 */
|
|
272
|
+
async function readHead(fullPath, byteLimit) {
|
|
273
|
+
let handle
|
|
274
|
+
try {
|
|
275
|
+
handle = await fsp.open(fullPath, 'r')
|
|
276
|
+
const size = (await handle.stat()).size
|
|
277
|
+
const want = Math.min(byteLimit, size)
|
|
278
|
+
const buffer = Buffer.alloc(want)
|
|
279
|
+
const { bytesRead } = await handle.read(buffer, 0, want, 0)
|
|
280
|
+
return buffer.subarray(0, bytesRead)
|
|
281
|
+
} catch {
|
|
282
|
+
return null
|
|
283
|
+
} finally {
|
|
284
|
+
if (handle !== undefined) await handle.close().catch(() => {})
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** PNG:IHDR 在固定偏移。 */
|
|
289
|
+
function pngSize(buf) {
|
|
290
|
+
if (buf.length < 24) return null
|
|
291
|
+
if (buf[0] !== 0x89 || buf[1] !== 0x50 || buf[2] !== 0x4e || buf[3] !== 0x47) return null
|
|
292
|
+
const width = buf.readUInt32BE(16)
|
|
293
|
+
const height = buf.readUInt32BE(20)
|
|
294
|
+
return width > 0 && height > 0 ? { width, height } : null
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** JPEG:顺序找 SOF 段。 */
|
|
298
|
+
function jpegSize(buf) {
|
|
299
|
+
if (buf.length < 4 || buf[0] !== 0xff || buf[1] !== 0xd8) return null
|
|
300
|
+
let p = 2
|
|
301
|
+
while (p + 9 < buf.length) {
|
|
302
|
+
if (buf[p] !== 0xff) {
|
|
303
|
+
p += 1
|
|
304
|
+
continue
|
|
305
|
+
}
|
|
306
|
+
let marker = buf[p + 1]
|
|
307
|
+
while (marker === 0xff && p + 1 < buf.length) {
|
|
308
|
+
p += 1
|
|
309
|
+
marker = buf[p + 1]
|
|
310
|
+
}
|
|
311
|
+
if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
|
|
312
|
+
p += 2
|
|
313
|
+
continue
|
|
314
|
+
}
|
|
315
|
+
if (marker === 0xd9 || marker === 0xda) return null
|
|
316
|
+
const length = buf.readUInt16BE(p + 2)
|
|
317
|
+
if (length < 2) return null
|
|
318
|
+
const isSof = marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc
|
|
319
|
+
if (isSof) {
|
|
320
|
+
const height = buf.readUInt16BE(p + 5)
|
|
321
|
+
const width = buf.readUInt16BE(p + 7)
|
|
322
|
+
return width > 0 && height > 0 ? { width, height } : null
|
|
323
|
+
}
|
|
324
|
+
p += 2 + length
|
|
325
|
+
}
|
|
326
|
+
return null
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** WebP:RIFF 容器,VP8 / VP8L / VP8X 三种尺寸布局。 */
|
|
330
|
+
function webpSize(buf) {
|
|
331
|
+
if (buf.length < 32) return null
|
|
332
|
+
if (buf.toString('ascii', 0, 4) !== 'RIFF' || buf.toString('ascii', 8, 12) !== 'WEBP') return null
|
|
333
|
+
const chunk = buf.toString('ascii', 12, 16)
|
|
334
|
+
if (chunk === 'VP8 ') {
|
|
335
|
+
const width = buf.readUInt16LE(26) & 0x3fff
|
|
336
|
+
const height = buf.readUInt16LE(28) & 0x3fff
|
|
337
|
+
return width > 0 && height > 0 ? { width, height } : null
|
|
338
|
+
}
|
|
339
|
+
if (chunk === 'VP8L') {
|
|
340
|
+
const b0 = buf[21]
|
|
341
|
+
const b1 = buf[22]
|
|
342
|
+
const b2 = buf[23]
|
|
343
|
+
const b3 = buf[24]
|
|
344
|
+
const width = 1 + (((b0 & 0xff) | ((b1 & 0x3f) << 8)) & 0x3fff)
|
|
345
|
+
const height = 1 + ((((b1 & 0xc0) >> 6) | (b2 << 2) | ((b3 & 0x0f) << 10)) & 0x3fff)
|
|
346
|
+
return width > 0 && height > 0 ? { width, height } : null
|
|
347
|
+
}
|
|
348
|
+
if (chunk === 'VP8X') {
|
|
349
|
+
const width = 1 + (buf[24] | (buf[25] << 8) | (buf[26] << 16))
|
|
350
|
+
const height = 1 + (buf[27] | (buf[28] << 8) | (buf[29] << 16))
|
|
351
|
+
return width > 0 && height > 0 ? { width, height } : null
|
|
352
|
+
}
|
|
353
|
+
return null
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** 按扩展名挑选解析器;返回 {width,height} 或 null。 */
|
|
357
|
+
function measureImage(buf, ext) {
|
|
358
|
+
try {
|
|
359
|
+
if (buf === null) return null
|
|
360
|
+
if (ext === '.png') return pngSize(buf)
|
|
361
|
+
if (ext === '.jpg' || ext === '.jpeg') return jpegSize(buf)
|
|
362
|
+
if (ext === '.webp') return webpSize(buf)
|
|
363
|
+
return pngSize(buf) ?? jpegSize(buf) ?? webpSize(buf)
|
|
364
|
+
} catch {
|
|
365
|
+
return null
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* 校验用户想设的产出根目录:必须是绝对路径、必须存在、必须是目录。
|
|
371
|
+
* @param candidate - 用户输入的路径。
|
|
372
|
+
* @returns { ok, root?, reason? } —— 通过时 root 是规范化后的绝对路径。
|
|
373
|
+
*/
|
|
374
|
+
export async function validateRootCandidate(candidate) {
|
|
375
|
+
if (typeof candidate !== 'string' || candidate.trim() === '') {
|
|
376
|
+
return { ok: false, reason: 'empty' }
|
|
377
|
+
}
|
|
378
|
+
const raw = candidate.trim()
|
|
379
|
+
// 只接受绝对路径(盘符或 UNC),相对路径无法确定含义
|
|
380
|
+
if (!path.isAbsolute(raw)) return { ok: false, reason: 'not-absolute' }
|
|
381
|
+
const resolved = path.resolve(raw)
|
|
382
|
+
try {
|
|
383
|
+
const st = await fsp.stat(resolved)
|
|
384
|
+
if (!st.isDirectory()) return { ok: false, reason: 'not-directory' }
|
|
385
|
+
} catch {
|
|
386
|
+
return { ok: false, reason: 'not-found' }
|
|
387
|
+
}
|
|
388
|
+
return { ok: true, root: resolved }
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* 把设置写进运行时配置文件。
|
|
393
|
+
* 只覆盖传入的字段,其余保留——避免把 windowHours 之类顺手清掉。
|
|
394
|
+
* @param settingsFile - 配置文件路径。
|
|
395
|
+
* @param patch - 要合并进去的部分设置。
|
|
396
|
+
*/
|
|
397
|
+
export async function writeSettingsFile(settingsFile, patch) {
|
|
398
|
+
const current = readSettingsFile(settingsFile)
|
|
399
|
+
const next = { ...current, ...patch }
|
|
400
|
+
await fsp.mkdir(path.dirname(settingsFile), { recursive: true })
|
|
401
|
+
await fsp.writeFile(settingsFile, JSON.stringify(next, null, 2), 'utf8')
|
|
402
|
+
return next
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* 返回设置面板需要的全部信息。
|
|
407
|
+
* @param config - 插件配置。
|
|
408
|
+
*/
|
|
409
|
+
async function readSettingsPayload(config) {
|
|
410
|
+
const opts = resolveOptions(config)
|
|
411
|
+
// 逐个检查每个 root 是否存在
|
|
412
|
+
const rootsInfo = []
|
|
413
|
+
for (const r of opts.roots) {
|
|
414
|
+
let exists = false
|
|
415
|
+
try {
|
|
416
|
+
exists = (await fsp.stat(r)).isDirectory()
|
|
417
|
+
} catch {
|
|
418
|
+
exists = false
|
|
419
|
+
}
|
|
420
|
+
rootsInfo.push({ path: r, exists })
|
|
421
|
+
}
|
|
422
|
+
const exists = rootsInfo.length > 0 ? rootsInfo[0].exists : false
|
|
423
|
+
// 已隐藏条数:供设置面板显示「已隐藏 N 个 · 全部恢复」
|
|
424
|
+
const hidden = await readHiddenList(opts.settingsFile)
|
|
425
|
+
return {
|
|
426
|
+
root: opts.root,
|
|
427
|
+
roots: opts.roots,
|
|
428
|
+
rootsInfo,
|
|
429
|
+
rootSource: opts.rootSource,
|
|
430
|
+
rootExists: exists,
|
|
431
|
+
settingsFile: opts.settingsFile,
|
|
432
|
+
windowHours: opts.windowHours,
|
|
433
|
+
maxItems: opts.maxItems,
|
|
434
|
+
maxDepth: opts.maxDepth,
|
|
435
|
+
hiddenCount: hidden.length,
|
|
436
|
+
// 前若干条隐藏项,供面板预览(不返回全部,避免配置很大时拖慢)
|
|
437
|
+
hiddenSample: hidden.slice(0, 10),
|
|
438
|
+
defaults: {
|
|
439
|
+
// 给前端「恢复默认」用:自动探测的结果
|
|
440
|
+
root: path.resolve(detectDefaultRoot()),
|
|
441
|
+
},
|
|
442
|
+
home: os.homedir(),
|
|
443
|
+
platform: process.platform,
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* 会话日志解压结果的缓存。
|
|
449
|
+
* 自动刷新每 5 秒一次,而一个活跃会话的日志解压后有 9MB 以上;
|
|
450
|
+
* 用「路径 + mtime + size」做键,日志没变就复用上次的解析结果。
|
|
451
|
+
*/
|
|
452
|
+
const sessionLogCache = new Map()
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* 解压一个会话日志。
|
|
456
|
+
* DSH 的 session.v4.jsonl.zstd 是「多帧 zstd 拼接」(每次追加写一帧),
|
|
457
|
+
* 单次 zstdDecompressSync 只能解第一帧,所以必须按魔数切开逐帧解。
|
|
458
|
+
* @param fullPath - 日志文件绝对路径。
|
|
459
|
+
* @returns 解压后的完整文本;失败返回空串。
|
|
460
|
+
*/
|
|
461
|
+
async function readSessionLogText(fullPath) {
|
|
462
|
+
let stat
|
|
463
|
+
let buf
|
|
464
|
+
try {
|
|
465
|
+
stat = await fsp.stat(fullPath)
|
|
466
|
+
const cacheKey = `${fullPath}|${stat.mtimeMs}|${stat.size}`
|
|
467
|
+
const cached = sessionLogCache.get(fullPath)
|
|
468
|
+
if (cached !== undefined && cached.key === cacheKey) return cached.text
|
|
469
|
+
buf = await fsp.readFile(fullPath)
|
|
470
|
+
} catch {
|
|
471
|
+
return ''
|
|
472
|
+
}
|
|
473
|
+
const offsets = []
|
|
474
|
+
let cursor = 0
|
|
475
|
+
while (cursor < buf.length) {
|
|
476
|
+
const hit = buf.indexOf(ZSTD_MAGIC, cursor)
|
|
477
|
+
if (hit < 0) break
|
|
478
|
+
offsets.push(hit)
|
|
479
|
+
cursor = hit + ZSTD_MAGIC.length
|
|
480
|
+
}
|
|
481
|
+
if (offsets.length === 0) return ''
|
|
482
|
+
const parts = []
|
|
483
|
+
for (const offset of offsets) {
|
|
484
|
+
try {
|
|
485
|
+
parts.push(zstdDecompressSync(buf.subarray(offset)))
|
|
486
|
+
} catch {
|
|
487
|
+
// 单帧损坏就跳过,不影响其余帧
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
const text = Buffer.concat(parts).toString('utf8')
|
|
491
|
+
sessionLogCache.set(fullPath, { key: `${fullPath}|${stat.mtimeMs}|${stat.size}`, text })
|
|
492
|
+
// 缓存只留最近若干条,避免长跑后堆积
|
|
493
|
+
if (sessionLogCache.size > 8) {
|
|
494
|
+
const oldest = sessionLogCache.keys().next().value
|
|
495
|
+
if (oldest !== undefined) sessionLogCache.delete(oldest)
|
|
496
|
+
}
|
|
497
|
+
return text
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/** 在 ~/.dsh/sessions/<工作区分组>/<sessionId>/ 里定位某个会话目录。 */
|
|
501
|
+
async function locateSessionDir(sessionsDir, sessionId) {
|
|
502
|
+
if (typeof sessionId !== 'string' || sessionId === '') return null
|
|
503
|
+
// 防御:会话 id 只可能是 [A-Za-z0-9._-],不许出现路径分隔符
|
|
504
|
+
if (/[\\/]/.test(sessionId) || sessionId.includes('..')) return null
|
|
505
|
+
let groups
|
|
506
|
+
try {
|
|
507
|
+
groups = await fsp.readdir(sessionsDir, { withFileTypes: true })
|
|
508
|
+
} catch {
|
|
509
|
+
return null
|
|
510
|
+
}
|
|
511
|
+
for (const group of groups) {
|
|
512
|
+
if (!group.isDirectory()) continue
|
|
513
|
+
const candidate = path.join(sessionsDir, String(group.name), sessionId)
|
|
514
|
+
try {
|
|
515
|
+
const stat = await fsp.stat(candidate)
|
|
516
|
+
if (stat.isDirectory()) return candidate
|
|
517
|
+
} catch {
|
|
518
|
+
// 继续找下一个分组
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return null
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* 找出某个会话的全部**子会话**目录(子智能体)。
|
|
526
|
+
*
|
|
527
|
+
* 修法 C:DSH 里子智能体是独立会话,有自己的 session 日志目录,
|
|
528
|
+
* 它的 write/present 调用**不出现在主会话日志里**。
|
|
529
|
+
* 所以「当前对话」必须把子会话的产出也算进去。
|
|
530
|
+
*
|
|
531
|
+
* 判定依据:子会话日志首行的 `parentSession` 字段指向主会话 id。
|
|
532
|
+
*
|
|
533
|
+
* @param sessionsDir - 会话根目录。
|
|
534
|
+
* @param parentSessionId - 主会话 id。
|
|
535
|
+
* @returns 子会话日志的绝对路径数组。
|
|
536
|
+
*/
|
|
537
|
+
async function findSubSessionLogs(sessionsDir, parentSessionId) {
|
|
538
|
+
const result = []
|
|
539
|
+
if (typeof parentSessionId !== 'string' || parentSessionId === '') return result
|
|
540
|
+
let groups
|
|
541
|
+
try {
|
|
542
|
+
groups = await fsp.readdir(sessionsDir, { withFileTypes: true })
|
|
543
|
+
} catch {
|
|
544
|
+
return result
|
|
545
|
+
}
|
|
546
|
+
for (const group of groups) {
|
|
547
|
+
if (!group.isDirectory()) continue
|
|
548
|
+
let sessions
|
|
549
|
+
try {
|
|
550
|
+
sessions = await fsp.readdir(path.join(sessionsDir, String(group.name)), { withFileTypes: true })
|
|
551
|
+
} catch {
|
|
552
|
+
continue
|
|
553
|
+
}
|
|
554
|
+
for (const s of sessions) {
|
|
555
|
+
if (!s.isDirectory()) continue
|
|
556
|
+
const logPath = path.join(sessionsDir, String(group.name), String(s.name), 'session.v4.jsonl.zstd')
|
|
557
|
+
try {
|
|
558
|
+
const st = await fsp.stat(logPath)
|
|
559
|
+
if (!st.isFile()) continue
|
|
560
|
+
} catch {
|
|
561
|
+
continue
|
|
562
|
+
}
|
|
563
|
+
// 读首行判断 parentSession;日志可能很大,只解第一帧就够(首行一定在第一帧)
|
|
564
|
+
const headText = await readSessionLogHead(logPath)
|
|
565
|
+
if (headText === '') continue
|
|
566
|
+
try {
|
|
567
|
+
const first = JSON.parse(headText.split('\n')[0])
|
|
568
|
+
if (first?.type === 'session' && first?.parentSession === parentSessionId) {
|
|
569
|
+
result.push(logPath)
|
|
570
|
+
}
|
|
571
|
+
} catch {
|
|
572
|
+
// 首行解析失败就跳过
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
return result
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* 只解压会话日志的第一帧,取首行(判断 parentSession 用,避免读整个 9MB 日志)。
|
|
581
|
+
* @param fullPath - 日志路径。
|
|
582
|
+
* @returns 首帧解出的文本;失败返回空串。
|
|
583
|
+
*/
|
|
584
|
+
async function readSessionLogHead(fullPath) {
|
|
585
|
+
let buf
|
|
586
|
+
try {
|
|
587
|
+
buf = await fsp.readFile(fullPath)
|
|
588
|
+
} catch {
|
|
589
|
+
return ''
|
|
590
|
+
}
|
|
591
|
+
const hit = buf.indexOf(ZSTD_MAGIC, 0)
|
|
592
|
+
if (hit < 0) return ''
|
|
593
|
+
try {
|
|
594
|
+
return zstdDecompressSync(buf.subarray(hit)).toString('utf8')
|
|
595
|
+
} catch {
|
|
596
|
+
return ''
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/** 递归收集 JSON 节点里所有以盘符开头的字符串(工具的 path 参数)。 */
|
|
601
|
+
function collectAbsolutePaths(node, sink) {
|
|
602
|
+
if (typeof node === 'string') {
|
|
603
|
+
if (/^[A-Za-z]:[\\/]/.test(node)) sink.push(node)
|
|
604
|
+
return
|
|
605
|
+
}
|
|
606
|
+
if (Array.isArray(node)) {
|
|
607
|
+
for (const entry of node) collectAbsolutePaths(entry, sink)
|
|
608
|
+
return
|
|
609
|
+
}
|
|
610
|
+
if (node !== null && typeof node === 'object') {
|
|
611
|
+
for (const key of Object.keys(node)) collectAbsolutePaths(node[key], sink)
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* 资产分类常量。
|
|
617
|
+
*
|
|
618
|
+
* 分类判据(按优先级,从高到低):
|
|
619
|
+
* 1. 被 `present` 声明过 → **最终资产**(正式交付物)
|
|
620
|
+
* 2. 只被 `read_image` 看过、从未被写入 → **输入资产**(用户给的参考图/资料)
|
|
621
|
+
* 3. 其余(write/edit/脚本产出/bash 提到) → **中间资产**
|
|
622
|
+
* 4. 会话日志里查不到任何记录 → **未归类**(历史文件,不能冒充「输入」)
|
|
623
|
+
*
|
|
624
|
+
* 为什么要有 UNKNOWN:早先版本把查不到归属的文件兜底成 input,
|
|
625
|
+
* 结果 4995 个历史文件全被标成「输入」,把真正的输入资产淹没了。
|
|
626
|
+
* 「不知道」就老老实实说不知道,不要瞎猜。
|
|
627
|
+
*/
|
|
628
|
+
const ASSET_CATEGORY = {
|
|
629
|
+
INPUT: 'input',
|
|
630
|
+
INTERMEDIATE: 'intermediate',
|
|
631
|
+
FINAL: 'final',
|
|
632
|
+
UNKNOWN: 'unknown',
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/** 中文/英文标签映射(客户端也可自行翻译,这里只给分类值)。 */
|
|
636
|
+
const CATEGORY_ORDER = ['final', 'input', 'intermediate']
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* 从会话日志里读出「本对话产出过的绝对路径 + 分类」。
|
|
640
|
+
*
|
|
641
|
+
* 覆盖两类来源:
|
|
642
|
+
* 1. `write` / `edit` / `present` 等直接写文件的调用 —— 路径必是产出。
|
|
643
|
+
* 2. `read_image` 打开过的图片 —— 脚本(Python 等)生成的图不在 write 调用里,
|
|
644
|
+
* 但 Agent 一定会打开看效果。不纳入这一条,「过程中生成的图」在
|
|
645
|
+
* 「当前对话」里就完全看不见(只有全局资产能看到)。
|
|
646
|
+
*
|
|
647
|
+
* @param logText - 解压后的日志文本(每行一个 JSON)。
|
|
648
|
+
* @param sessionStartMs - 本会话开始时间(毫秒时间戳)。
|
|
649
|
+
* 用于区分「用户提供的素材」和「会话期间生成的产物」:
|
|
650
|
+
* 前者必定早于会话开始就存在。传 0 表示拿不到,此时不做输入判定。
|
|
651
|
+
* @returns Map<绝对路径, { category, presented, written, viewed, bashMentioned }>
|
|
652
|
+
*/
|
|
653
|
+
function extractAssetInfo(logText, sessionStartMs = 0) {
|
|
654
|
+
const assets = new Map()
|
|
655
|
+
if (typeof logText !== 'string' || logText === '') return assets
|
|
656
|
+
|
|
657
|
+
const ensure = (abs) => {
|
|
658
|
+
if (!assets.has(abs)) {
|
|
659
|
+
assets.set(abs, { presented: false, written: false, viewed: false, bashMentioned: false })
|
|
660
|
+
}
|
|
661
|
+
return assets.get(abs)
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
for (const line of logText.split('\n')) {
|
|
665
|
+
if (line === '') continue
|
|
666
|
+
// 快速预筛:只处理含 tool/call 的行,避免全量 JSON.parse
|
|
667
|
+
if (!line.includes('tool/call')) continue
|
|
668
|
+
let record
|
|
669
|
+
try {
|
|
670
|
+
record = JSON.parse(line)
|
|
671
|
+
} catch {
|
|
672
|
+
continue
|
|
673
|
+
}
|
|
674
|
+
if (record?.type !== 'tool/call') continue
|
|
675
|
+
const name = record?.data?.name
|
|
676
|
+
if (typeof name !== 'string') continue
|
|
677
|
+
|
|
678
|
+
const rawArgs = record?.data?.arguments
|
|
679
|
+
if (typeof rawArgs !== 'string') continue
|
|
680
|
+
let args
|
|
681
|
+
try {
|
|
682
|
+
args = JSON.parse(rawArgs)
|
|
683
|
+
} catch {
|
|
684
|
+
continue
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// ---- bash / pwsh:命令文本里提到的路径,只作为「非输入」的弱证据 ----
|
|
688
|
+
if (BASH_TOOLS.has(name)) {
|
|
689
|
+
const cmdText = `${args?.command ?? ''} ${args?.script_path ?? ''}`
|
|
690
|
+
const re = /[A-Za-z]:[\\/][^\s"'|&;<>()*?]+/g
|
|
691
|
+
for (const m of cmdText.matchAll(re)) {
|
|
692
|
+
const candidate = m[0].replace(/[\\/]+$/, '')
|
|
693
|
+
const ext = path.extname(candidate).toLowerCase()
|
|
694
|
+
// 只接受看起来像文件的路径(有扩展名)
|
|
695
|
+
if (ext === '') continue
|
|
696
|
+
try {
|
|
697
|
+
const info = ensure(path.resolve(candidate))
|
|
698
|
+
info.bashMentioned = true
|
|
699
|
+
} catch {
|
|
700
|
+
// 解析失败就跳过
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
continue
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
const isWriter = OUTPUT_TOOLS.has(name)
|
|
707
|
+
const isViewer = VIEW_TOOLS.has(name)
|
|
708
|
+
const isPresenter = name === 'present'
|
|
709
|
+
if (!isWriter && !isViewer && !isPresenter) continue
|
|
710
|
+
|
|
711
|
+
// present 的参数结构是 { files: [{ path, description }]}
|
|
712
|
+
let found = []
|
|
713
|
+
if (isPresenter && Array.isArray(args?.files)) {
|
|
714
|
+
found = args.files.map((f) => f?.path).filter((p) => typeof p === 'string')
|
|
715
|
+
} else {
|
|
716
|
+
collectAbsolutePaths(args, found)
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
for (const entry of found) {
|
|
720
|
+
if (isViewer) {
|
|
721
|
+
// 查看类工具只认图片:避免把 read_image 参数里偶尔出现的
|
|
722
|
+
// 目录、配置等非图片路径当成产出
|
|
723
|
+
const ext = path.extname(entry).toLowerCase()
|
|
724
|
+
if (!VIEW_IMAGE_EXTS.has(ext)) continue
|
|
725
|
+
}
|
|
726
|
+
const info = ensure(path.resolve(entry))
|
|
727
|
+
if (isPresenter) info.presented = true
|
|
728
|
+
else if (isViewer) info.viewed = true
|
|
729
|
+
else info.written = true
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// ---- 归类 ----
|
|
734
|
+
for (const [abs, info] of assets) {
|
|
735
|
+
classifyAsset(abs, info, sessionStartMs)
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
return assets
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* 从会话日志首行解析会话开始时间(createdAt)。
|
|
743
|
+
* @param logText - 解压后的日志文本。
|
|
744
|
+
* @returns 毫秒时间戳;解析失败返回 0。
|
|
745
|
+
*/
|
|
746
|
+
function parseSessionStartMs(logText) {
|
|
747
|
+
if (typeof logText !== 'string' || logText === '') return 0
|
|
748
|
+
const firstLine = logText.split('\n')[0]
|
|
749
|
+
try {
|
|
750
|
+
const first = JSON.parse(firstLine)
|
|
751
|
+
return typeof first?.createdAt === 'number' ? first.createdAt : 0
|
|
752
|
+
} catch {
|
|
753
|
+
return 0
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* 给单个资产定分类。**唯一**的分类入口,别在别处复制这套判据。
|
|
759
|
+
*
|
|
760
|
+
* 规则(按优先级):
|
|
761
|
+
* 1. present 过 → 最终
|
|
762
|
+
* 2. 「只被 read_image 看过」是候选输入,但必须能证明它早于会话开始就存在:
|
|
763
|
+
* - mtime < 会话开始 → 输入(用户预先提供的素材)
|
|
764
|
+
* - mtime >= 会话开始 → 中间(会话期间生成的,脚本产出常见这种)
|
|
765
|
+
* 3. 其余 → 中间
|
|
766
|
+
*
|
|
767
|
+
* 为什么必须看时间:「被 read_image 看过」既可能是用户素材,也可能是脚本产出的图,
|
|
768
|
+
* Agent 两者都会打开看。实测反例:
|
|
769
|
+
* - architecture.png:write 写的是脚本,file_path 不是它,只在 read_image 出现
|
|
770
|
+
* - 缩略图尺寸对比.png:文件名写在 bash 的 python 代码里,不是绝对路径
|
|
771
|
+
* 这两类旧判据都会误判成「输入」。
|
|
772
|
+
*
|
|
773
|
+
* @param abs - 文件绝对路径。
|
|
774
|
+
* @param info - extractAssetInfo 收集到的动作标记(会被写入 category)。
|
|
775
|
+
* @param sessionStartMs - 会话开始时间;0 表示未知(此时不做输入判定)。
|
|
776
|
+
*/
|
|
777
|
+
function classifyAsset(abs, info, sessionStartMs) {
|
|
778
|
+
info.path = abs
|
|
779
|
+
|
|
780
|
+
if (info.presented) {
|
|
781
|
+
info.category = ASSET_CATEGORY.FINAL
|
|
782
|
+
return
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
const looksLikeInput = info.viewed === true && info.written !== true && info.bashMentioned !== true
|
|
786
|
+
if (!looksLikeInput) {
|
|
787
|
+
info.category = ASSET_CATEGORY.INTERMEDIATE
|
|
788
|
+
return
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
let mtimeMs = null
|
|
792
|
+
try {
|
|
793
|
+
mtimeMs = statSync(abs).mtimeMs
|
|
794
|
+
} catch {
|
|
795
|
+
mtimeMs = null
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
if (mtimeMs !== null && sessionStartMs > 0 && mtimeMs < sessionStartMs) {
|
|
799
|
+
info.category = ASSET_CATEGORY.INPUT
|
|
800
|
+
} else {
|
|
801
|
+
// 会话开始后才出现(或文件已不存在)→ 归中间,不冒充用户素材
|
|
802
|
+
info.category = ASSET_CATEGORY.INTERMEDIATE
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* 从会话日志里读出「本对话产出过的绝对路径」。
|
|
808
|
+
* @param logText - 解压后的日志文本(每行一个 JSON)。
|
|
809
|
+
* @returns 绝对路径的 Set。
|
|
810
|
+
*/
|
|
811
|
+
function extractOutputPaths(logText) {
|
|
812
|
+
return new Set(extractAssetInfo(logText).keys())
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/**
|
|
816
|
+
* 算出「本对话」产出过的路径与任务目录。
|
|
817
|
+
*
|
|
818
|
+
* 支持多扫描目录(roots):逐个判断路径落在哪个 root 下。
|
|
819
|
+
* 修法 B:文件**直接躺在 root 根目录下**(没有子目录)时也要能匹配,
|
|
820
|
+
* 原实现只取「第一层目录」并要求至少两段,导致根目录文件全部判为 0。
|
|
821
|
+
*
|
|
822
|
+
* @param config - 用户配置。
|
|
823
|
+
* @param sessionId - 当前会话 id。
|
|
824
|
+
* @param allRoots - 已解析的扫描目录列表(不传则内部再解析一次)。
|
|
825
|
+
* @returns { ok, reason, taskDirs, paths, relPaths }
|
|
826
|
+
*/
|
|
827
|
+
async function resolveSessionScope(config, sessionId, allRoots) {
|
|
828
|
+
const opts = resolveOptions(config)
|
|
829
|
+
const roots = Array.isArray(allRoots) && allRoots.length > 0 ? allRoots : opts.roots
|
|
830
|
+
const sessionsDir = typeof config?.sessionsDir === 'string' && config.sessionsDir !== ''
|
|
831
|
+
? path.resolve(config.sessionsDir)
|
|
832
|
+
: DEFAULT_SESSIONS_DIR
|
|
833
|
+
const sessionDir = await locateSessionDir(sessionsDir, sessionId)
|
|
834
|
+
if (sessionDir === null) {
|
|
835
|
+
return { ok: false, reason: 'session-not-found', taskDirs: [], paths: [], relPaths: [], subSessions: 0 }
|
|
836
|
+
}
|
|
837
|
+
const logPath = path.join(sessionDir, 'session.v4.jsonl.zstd')
|
|
838
|
+
const logText = await readSessionLogText(logPath)
|
|
839
|
+
if (logText === '') {
|
|
840
|
+
return { ok: false, reason: 'log-empty', taskDirs: [], paths: [], relPaths: [], subSessions: 0 }
|
|
841
|
+
}
|
|
842
|
+
// 会话开始时间:用于区分「用户素材」与「会话期间产出」
|
|
843
|
+
const sessionStartMs = parseSessionStartMs(logText)
|
|
844
|
+
const allAssets = extractAssetInfo(logText, sessionStartMs)
|
|
845
|
+
|
|
846
|
+
// 修法 C:合并所有子会话(子智能体)的产出路径
|
|
847
|
+
const subLogs = await findSubSessionLogs(sessionsDir, sessionId)
|
|
848
|
+
for (const subLog of subLogs) {
|
|
849
|
+
const subText = await readSessionLogText(subLog)
|
|
850
|
+
if (subText === '') continue
|
|
851
|
+
for (const [abs, info] of extractAssetInfo(subText, sessionStartMs)) {
|
|
852
|
+
const existing = allAssets.get(abs)
|
|
853
|
+
if (existing) {
|
|
854
|
+
// 合并标记:任一处 present 就算最终
|
|
855
|
+
existing.presented = existing.presented || info.presented
|
|
856
|
+
existing.written = existing.written || info.written
|
|
857
|
+
existing.viewed = existing.viewed || info.viewed
|
|
858
|
+
existing.bashMentioned = existing.bashMentioned || info.bashMentioned
|
|
859
|
+
} else {
|
|
860
|
+
allAssets.set(abs, info)
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
// 合并后重新归类(因为 presented/written 可能变了)。
|
|
866
|
+
// 统一走 classifyAsset,避免两处判据分叉 —— v0.8.1 就是这里漏改导致分类不准。
|
|
867
|
+
for (const [abs, info] of allAssets) {
|
|
868
|
+
classifyAsset(abs, info, sessionStartMs)
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
const normalized = []
|
|
872
|
+
const relPaths = []
|
|
873
|
+
const taskDirs = new Set()
|
|
874
|
+
// relPath(小写) → 分类,供扫描时标注
|
|
875
|
+
const categoryByRel = new Map()
|
|
876
|
+
|
|
877
|
+
for (const [abs, info] of allAssets) {
|
|
878
|
+
const absolute = path.resolve(abs)
|
|
879
|
+
// 找出该路径属于哪个 root
|
|
880
|
+
let ownerRoot = null
|
|
881
|
+
for (const r of roots) {
|
|
882
|
+
if (isInside(r, absolute)) { ownerRoot = r; break }
|
|
883
|
+
}
|
|
884
|
+
if (ownerRoot === null) continue
|
|
885
|
+
normalized.push(absolute)
|
|
886
|
+
|
|
887
|
+
const rel = path.relative(ownerRoot, absolute)
|
|
888
|
+
const segs = rel.split(path.sep).filter((s) => s !== '')
|
|
889
|
+
const relPath = rel.split(path.sep).join('/')
|
|
890
|
+
relPaths.push(relPath)
|
|
891
|
+
categoryByRel.set(relPath.toLowerCase(), info.category)
|
|
892
|
+
// 修法 B:有子目录时取第一层;没有子目录(文件直接在 root 下)时,
|
|
893
|
+
// 用空字符串占位,表示「只在根目录层匹配」,不再直接丢弃
|
|
894
|
+
if (segs.length >= 2) taskDirs.add(segs[0])
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
return {
|
|
898
|
+
ok: true,
|
|
899
|
+
reason: 'ok',
|
|
900
|
+
taskDirs: [...taskDirs],
|
|
901
|
+
paths: normalized,
|
|
902
|
+
relPaths,
|
|
903
|
+
categoryByRel,
|
|
904
|
+
subSessions: subLogs.length,
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* 扫描画廊候选:广度优先、深度受限、跳过批量目录、有目录/条目预算。
|
|
910
|
+
* 结果按修改时间倒序,只回传 maxItems 条,另外给出 total 供「还有 N 张」。
|
|
911
|
+
*/
|
|
912
|
+
export async function scanGallery(config, kind = 'image', scope = 'global', sessionId = '', page = 1) {
|
|
913
|
+
const opts = resolveOptions(config)
|
|
914
|
+
const now = Date.now()
|
|
915
|
+
const cutoff = now - opts.windowHours * 3600_000
|
|
916
|
+
const isFile = kind === 'file'
|
|
917
|
+
const isSession = scope === 'session'
|
|
918
|
+
// 页码从 1 开始;非法值一律回到第 1 页
|
|
919
|
+
const currentPage = Math.max(1, Math.trunc(Number(page) || 1))
|
|
920
|
+
// 图片格式永远排除在文件画廊之外;防止用户配置误把图片塞进来
|
|
921
|
+
const extraImageEx = new Set(['.png', '.jpg', '.jpeg', '.webp', '.bmp', '.tif', '.tiff', '.heic', '.avif', '.ico'])
|
|
922
|
+
const rawExts = isFile ? opts.fileExtensions : opts.extensions
|
|
923
|
+
const extSet = new Set(rawExts.filter((e) => !(isFile && extraImageEx.has(e))))
|
|
924
|
+
const skipSet = new Set(opts.skipDirs)
|
|
925
|
+
|
|
926
|
+
// 当前对话范围:先解析会话日志(含子智能体会话),得到「本对话产出过的路径」
|
|
927
|
+
let scopeInfo = { ok: true, reason: 'global', taskDirs: [], paths: [], relPaths: [], subSessions: 0 }
|
|
928
|
+
let sessionRelSet = null
|
|
929
|
+
if (isSession) {
|
|
930
|
+
scopeInfo = await resolveSessionScope(config, sessionId, opts.roots)
|
|
931
|
+
// 修法 B:用「精确相对路径」集合判定,而不是只靠第一层目录名。
|
|
932
|
+
// 这样既能处理有子目录的情况,也能处理「文件直接躺在 root 根目录下」。
|
|
933
|
+
sessionRelSet = new Set((scopeInfo.relPaths ?? []).map((p) => p.toLowerCase()))
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
// 会话索引:建立「文件 → 所属对话 + 分类」的映射,供全局按对话分组用
|
|
937
|
+
const sessionsDir = typeof config?.sessionsDir === 'string' && config.sessionsDir !== ''
|
|
938
|
+
? path.resolve(config.sessionsDir)
|
|
939
|
+
: DEFAULT_SESSIONS_DIR
|
|
940
|
+
let sessionIndex = null
|
|
941
|
+
// 全局模式才需要索引(当前对话模式已有自己的 scopeInfo)
|
|
942
|
+
if (!isSession) {
|
|
943
|
+
sessionIndex = await buildSessionIndex(sessionsDir, opts, {
|
|
944
|
+
readSessionLogText,
|
|
945
|
+
extractAssetInfo,
|
|
946
|
+
})
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
// 文件绝对路径 → { sessionId, title, category }
|
|
950
|
+
const ownerByAbs = new Map()
|
|
951
|
+
if (sessionIndex !== null) {
|
|
952
|
+
for (const [sid, entry] of sessionIndex) {
|
|
953
|
+
for (const [abs, cat] of entry.files) {
|
|
954
|
+
// 同一个文件被多个对话产出时,取最近的那个对话
|
|
955
|
+
const prev = ownerByAbs.get(abs)
|
|
956
|
+
if (prev === undefined || entry.createdAt > prev.createdAt) {
|
|
957
|
+
ownerByAbs.set(abs, {
|
|
958
|
+
sessionId: sid,
|
|
959
|
+
title: entry.title || '',
|
|
960
|
+
createdAt: entry.createdAt,
|
|
961
|
+
category: cat,
|
|
962
|
+
})
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
// 已隐藏的路径:扫描时直接跳过(不移动文件,只从列表里拿掉)
|
|
969
|
+
const hiddenSet = new Set(await readHiddenList(opts.settingsFile))
|
|
970
|
+
|
|
971
|
+
const matches = []
|
|
972
|
+
let directories = 0
|
|
973
|
+
let entries = 0
|
|
974
|
+
let capped = false
|
|
975
|
+
let anyRootExists = false
|
|
976
|
+
const missingRoots = []
|
|
977
|
+
|
|
978
|
+
// 遍历所有扫描目录(多目录支持)
|
|
979
|
+
for (const rootDir of opts.roots) {
|
|
980
|
+
if (directories >= opts.maxDirectories || entries >= opts.maxEntries) {
|
|
981
|
+
capped = true
|
|
982
|
+
break
|
|
983
|
+
}
|
|
984
|
+
let rootOk = false
|
|
985
|
+
try {
|
|
986
|
+
const st = await fsp.stat(rootDir)
|
|
987
|
+
rootOk = st.isDirectory()
|
|
988
|
+
} catch {
|
|
989
|
+
rootOk = false
|
|
990
|
+
}
|
|
991
|
+
if (!rootOk) {
|
|
992
|
+
missingRoots.push(rootDir)
|
|
993
|
+
continue
|
|
994
|
+
}
|
|
995
|
+
anyRootExists = true
|
|
996
|
+
|
|
997
|
+
const dirQueue = [{ dir: rootDir, depth: 0 }]
|
|
998
|
+
while (dirQueue.length > 0) {
|
|
999
|
+
if (directories >= opts.maxDirectories || entries >= opts.maxEntries) {
|
|
1000
|
+
capped = true
|
|
1001
|
+
break
|
|
1002
|
+
}
|
|
1003
|
+
const { dir, depth } = dirQueue.shift()
|
|
1004
|
+
directories += 1
|
|
1005
|
+
let dirents
|
|
1006
|
+
try {
|
|
1007
|
+
dirents = await fsp.readdir(dir, { withFileTypes: true })
|
|
1008
|
+
} catch {
|
|
1009
|
+
continue
|
|
1010
|
+
}
|
|
1011
|
+
for (const dirent of dirents) {
|
|
1012
|
+
entries += 1
|
|
1013
|
+
if (entries >= opts.maxEntries) {
|
|
1014
|
+
capped = true
|
|
1015
|
+
break
|
|
1016
|
+
}
|
|
1017
|
+
const entryName = String(dirent.name)
|
|
1018
|
+
const fullPath = path.join(dir, entryName)
|
|
1019
|
+
if (dirent.isDirectory()) {
|
|
1020
|
+
if (depth + 1 > opts.maxDepth) continue
|
|
1021
|
+
if (skipSet.has(entryName.toLowerCase())) continue
|
|
1022
|
+
dirQueue.push({ dir: fullPath, depth: depth + 1 })
|
|
1023
|
+
continue
|
|
1024
|
+
}
|
|
1025
|
+
if (!dirent.isFile()) continue
|
|
1026
|
+
const ext = path.extname(entryName).toLowerCase()
|
|
1027
|
+
if (!extSet.has(ext)) continue
|
|
1028
|
+
// 当前对话范围:按精确相对路径判定(支持根目录文件与多层子目录)
|
|
1029
|
+
const relPath = path.relative(rootDir, fullPath).split(path.sep).join('/')
|
|
1030
|
+
if (sessionRelSet !== null && !sessionRelSet.has(relPath.toLowerCase())) continue
|
|
1031
|
+
let stat
|
|
1032
|
+
try {
|
|
1033
|
+
stat = await fsp.stat(fullPath)
|
|
1034
|
+
} catch {
|
|
1035
|
+
continue
|
|
1036
|
+
}
|
|
1037
|
+
if (stat.size <= 0 || stat.mtimeMs < cutoff) continue
|
|
1038
|
+
// 被隐藏的直接跳过(隐藏只影响列表展示,文件仍在磁盘上)
|
|
1039
|
+
if (hiddenSet.has(relPath)) continue
|
|
1040
|
+
matches.push({
|
|
1041
|
+
relPath,
|
|
1042
|
+
relKey: `${opts.roots.indexOf(rootDir)}:${relPath}`,
|
|
1043
|
+
rootDir,
|
|
1044
|
+
rootIndex: opts.roots.indexOf(rootDir),
|
|
1045
|
+
name: entryName,
|
|
1046
|
+
size: stat.size,
|
|
1047
|
+
mtime: stat.mtimeMs,
|
|
1048
|
+
extension: ext,
|
|
1049
|
+
})
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
const rootMissing = !anyRootExists
|
|
1055
|
+
|
|
1056
|
+
// 给每个条目附加「分类」和「所属对话」
|
|
1057
|
+
for (const item of matches) {
|
|
1058
|
+
const abs = path.join(item.rootDir, ...item.relPath.split('/'))
|
|
1059
|
+
const owner = ownerByAbs.get(abs)
|
|
1060
|
+
item.absPath = abs
|
|
1061
|
+
if (isSession) {
|
|
1062
|
+
item.category = (scopeInfo.categoryByRel?.get(item.relPath.toLowerCase())) ?? null
|
|
1063
|
+
item.sessionId = sessionId
|
|
1064
|
+
} else if (owner !== undefined) {
|
|
1065
|
+
item.category = owner.category
|
|
1066
|
+
item.sessionId = owner.sessionId
|
|
1067
|
+
item.sessionTitle = owner.title
|
|
1068
|
+
item.sessionCreatedAt = owner.createdAt
|
|
1069
|
+
} else {
|
|
1070
|
+
// 不属于任何已知对话的文件:归到「历史资产」,分类记为**未归类**
|
|
1071
|
+
// 不能兜底成 input —— 那会把 4995 个历史文件全标成「输入」
|
|
1072
|
+
item.category = ASSET_CATEGORY.UNKNOWN
|
|
1073
|
+
item.sessionId = null
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
matches.sort((a, b) => b.mtime - a.mtime)
|
|
1078
|
+
const total = matches.length
|
|
1079
|
+
// 分页:每页 maxItems 条,保留 total 供页码条使用
|
|
1080
|
+
// 全局分组模式下不分页(客户端自己分组折叠),所有条目都返回摘要
|
|
1081
|
+
const pageCount = isSession
|
|
1082
|
+
? Math.max(1, Math.ceil(total / opts.maxItems))
|
|
1083
|
+
: Math.max(1, Math.ceil(total / opts.maxItems))
|
|
1084
|
+
const safePage = Math.min(currentPage, pageCount)
|
|
1085
|
+
const offset = (safePage - 1) * opts.maxItems
|
|
1086
|
+
const visible = matches.slice(offset, offset + opts.maxItems)
|
|
1087
|
+
|
|
1088
|
+
await Promise.all(visible.map(async (item) => {
|
|
1089
|
+
if (isFile) {
|
|
1090
|
+
// 文件画廊不解析图片尺寸
|
|
1091
|
+
item.width = null
|
|
1092
|
+
item.height = null
|
|
1093
|
+
return
|
|
1094
|
+
}
|
|
1095
|
+
// 多目录:用该条目自己所属的 rootDir 拼绝对路径
|
|
1096
|
+
const measured = measureImage(
|
|
1097
|
+
await readHead(path.join(item.rootDir, ...item.relPath.split('/')), 65536),
|
|
1098
|
+
item.extension,
|
|
1099
|
+
)
|
|
1100
|
+
item.width = measured?.width ?? null
|
|
1101
|
+
item.height = measured?.height ?? null
|
|
1102
|
+
}))
|
|
1103
|
+
|
|
1104
|
+
// 全量统计:供「全局按对话分组」使用(不受分页限制)
|
|
1105
|
+
const allItemsSummary = []
|
|
1106
|
+
if (!isSession) {
|
|
1107
|
+
for (const item of matches) {
|
|
1108
|
+
allItemsSummary.push({
|
|
1109
|
+
absPath: item.absPath,
|
|
1110
|
+
relPath: item.relPath,
|
|
1111
|
+
sessionId: item.sessionId,
|
|
1112
|
+
sessionTitle: item.sessionTitle ?? '',
|
|
1113
|
+
sessionCreatedAt: item.sessionCreatedAt ?? 0,
|
|
1114
|
+
category: item.category,
|
|
1115
|
+
mtime: item.mtime,
|
|
1116
|
+
width: item.width ?? null,
|
|
1117
|
+
height: item.height ?? null,
|
|
1118
|
+
size: item.size,
|
|
1119
|
+
name: item.name,
|
|
1120
|
+
extension: item.extension,
|
|
1121
|
+
})
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
return {
|
|
1126
|
+
kind,
|
|
1127
|
+
scope: isSession ? 'session' : 'global',
|
|
1128
|
+
scopeInfo: {
|
|
1129
|
+
ok: scopeInfo.ok,
|
|
1130
|
+
reason: scopeInfo.reason,
|
|
1131
|
+
taskDirs: scopeInfo.taskDirs.length,
|
|
1132
|
+
paths: scopeInfo.paths.length,
|
|
1133
|
+
relPaths: (scopeInfo.relPaths ?? []).length,
|
|
1134
|
+
// 子智能体会话数:> 0 表示已纳入它们的产出
|
|
1135
|
+
subSessions: scopeInfo.subSessions ?? 0,
|
|
1136
|
+
},
|
|
1137
|
+
root: opts.root,
|
|
1138
|
+
roots: opts.roots,
|
|
1139
|
+
rootSource: opts.rootSource,
|
|
1140
|
+
missingRoots,
|
|
1141
|
+
windowHours: opts.windowHours,
|
|
1142
|
+
maxItems: opts.maxItems,
|
|
1143
|
+
maxDepth: opts.maxDepth,
|
|
1144
|
+
generatedAt: now,
|
|
1145
|
+
items: visible,
|
|
1146
|
+
total,
|
|
1147
|
+
// 分页信息,客户端据此渲染页码条
|
|
1148
|
+
page: safePage,
|
|
1149
|
+
pageCount,
|
|
1150
|
+
pageSize: opts.maxItems,
|
|
1151
|
+
// 「还有 N 张」= 当前页之外的总数(不再只是「被截掉的」)
|
|
1152
|
+
hidden: Math.max(0, total - visible.length),
|
|
1153
|
+
rootMissing,
|
|
1154
|
+
scan: { directories, entries, capped },
|
|
1155
|
+
// 全局模式额外返回:按对话分组的数据(客户端做折叠展示用)
|
|
1156
|
+
groups: isSession ? null : buildGroups(allItemsSummary, 120, sessionId),
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
/**
|
|
1161
|
+
* 把扁平的资产列表按「所属对话」分组。
|
|
1162
|
+
*
|
|
1163
|
+
* 分组规则:
|
|
1164
|
+
* - 属于某个已知对话的文件 → 归到该对话(标题用会话标题)
|
|
1165
|
+
* - 其余(日志里查不到归属的历史文件)→ 统一归到「历史资产」组
|
|
1166
|
+
*
|
|
1167
|
+
* 排序(v0.8.2 调整):
|
|
1168
|
+
* 1. **当前对话永远置顶** —— 用户打开「全局资产」最想先看自己这次做了什么,
|
|
1169
|
+
* 按时间倒序会把它压到中间(当前对话可能很早创建、但一直在用)。
|
|
1170
|
+
* 2. 其余对话按创建时间倒序。
|
|
1171
|
+
* 3. 「历史资产」永远最后。
|
|
1172
|
+
*
|
|
1173
|
+
* ⚠️ 体积控制:每组只返回前 maxPerGroup 条明细 + 完整总数。
|
|
1174
|
+
* 为什么必须截断:历史组有 4995 个文件,全量返回会让单次响应达到 1.3 MB,
|
|
1175
|
+
* 前端解析/渲染都会卡,面板看起来「只显示两组」。
|
|
1176
|
+
*
|
|
1177
|
+
* @param summary - 扁平资产摘要数组。
|
|
1178
|
+
* @param maxPerGroup - 每组最多返回多少条明细(默认 120)。
|
|
1179
|
+
* @param currentSessionId - 当前打开的会话 id;匹配到的组会标记 isCurrent 并置顶。
|
|
1180
|
+
* @returns 分组数组。
|
|
1181
|
+
*/
|
|
1182
|
+
function buildGroups(summary, maxPerGroup = 120, currentSessionId = '') {
|
|
1183
|
+
if (!Array.isArray(summary) || summary.length === 0) return []
|
|
1184
|
+
|
|
1185
|
+
const groups = new Map()
|
|
1186
|
+
for (const item of summary) {
|
|
1187
|
+
const key = item.sessionId ?? '__unassigned__'
|
|
1188
|
+
if (!groups.has(key)) {
|
|
1189
|
+
groups.set(key, {
|
|
1190
|
+
sessionId: item.sessionId,
|
|
1191
|
+
title: item.sessionId === null ? '' : (item.sessionTitle || item.sessionId.slice(0, 8)),
|
|
1192
|
+
createdAt: item.sessionCreatedAt || 0,
|
|
1193
|
+
files: [],
|
|
1194
|
+
})
|
|
1195
|
+
}
|
|
1196
|
+
const g = groups.get(key)
|
|
1197
|
+
g.files.push({
|
|
1198
|
+
absPath: item.absPath,
|
|
1199
|
+
relPath: item.relPath,
|
|
1200
|
+
category: item.category,
|
|
1201
|
+
mtime: item.mtime,
|
|
1202
|
+
width: item.width ?? null,
|
|
1203
|
+
height: item.height ?? null,
|
|
1204
|
+
size: item.size ?? 0,
|
|
1205
|
+
name: item.name ?? item.relPath.split('/').pop(),
|
|
1206
|
+
})
|
|
1207
|
+
if (item.sessionId === null && item.mtime > g.createdAt) g.createdAt = item.mtime
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
const out = []
|
|
1211
|
+
for (const g of groups.values()) {
|
|
1212
|
+
g.files.sort((a, b) => b.mtime - a.mtime)
|
|
1213
|
+
const counts = { final: 0, input: 0, intermediate: 0, unknown: 0 }
|
|
1214
|
+
for (const f of g.files) {
|
|
1215
|
+
if (counts[f.category] !== undefined) counts[f.category] += 1
|
|
1216
|
+
}
|
|
1217
|
+
// 只回传前 maxPerGroup 条明细(客户端滚动到底再按需请求)
|
|
1218
|
+
const returned = g.files.slice(0, maxPerGroup)
|
|
1219
|
+
out.push({
|
|
1220
|
+
sessionId: g.sessionId,
|
|
1221
|
+
isHistory: g.sessionId === null,
|
|
1222
|
+
// 当前打开的会话:客户端据此置顶并默认展开
|
|
1223
|
+
isCurrent: currentSessionId !== '' && g.sessionId === currentSessionId,
|
|
1224
|
+
title: g.title,
|
|
1225
|
+
createdAt: g.createdAt,
|
|
1226
|
+
fileCount: g.files.length,
|
|
1227
|
+
returnedCount: returned.length,
|
|
1228
|
+
truncated: g.files.length > returned.length,
|
|
1229
|
+
counts,
|
|
1230
|
+
files: returned,
|
|
1231
|
+
latestMtime: g.files.length > 0 ? g.files[0].mtime : 0,
|
|
1232
|
+
})
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
// 组排序:
|
|
1236
|
+
// 1. 当前对话永远第一(按时间倒序会把它压到中间)
|
|
1237
|
+
// 2. 其余对话按创建时间倒序
|
|
1238
|
+
// 3. 「历史资产」永远最后
|
|
1239
|
+
out.sort((a, b) => {
|
|
1240
|
+
if (a.isCurrent !== b.isCurrent) return a.isCurrent ? -1 : 1
|
|
1241
|
+
if (a.isHistory && !b.isHistory) return 1
|
|
1242
|
+
if (b.isHistory && !a.isHistory) return -1
|
|
1243
|
+
return b.createdAt - a.createdAt
|
|
1244
|
+
})
|
|
1245
|
+
|
|
1246
|
+
return out
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
/**
|
|
1250
|
+
* 把一个相对路径解析成根目录内的绝对路径;越界或不存在返回 null。
|
|
1251
|
+
* 多目录支持:第一个 root 找不到就逐个试其余 root。
|
|
1252
|
+
* @param roots - 扫描目录列表(或单个 root 字符串,兼容旧调用)。
|
|
1253
|
+
*/
|
|
1254
|
+
async function resolveReadable(roots, relPath, opts) {
|
|
1255
|
+
const list = Array.isArray(roots) ? roots : [roots]
|
|
1256
|
+
if (typeof relPath !== 'string' || relPath === '') return null
|
|
1257
|
+
const normalized = relPath.replace(/\\/g, '/')
|
|
1258
|
+
if (normalized.split('/').some((segment) => segment === '..' || segment === '')) return null
|
|
1259
|
+
for (const root of list) {
|
|
1260
|
+
const absolute = path.resolve(root, ...normalized.split('/'))
|
|
1261
|
+
if (!isInside(root, absolute)) continue
|
|
1262
|
+
try {
|
|
1263
|
+
const stat = await fsp.stat(absolute)
|
|
1264
|
+
if (!stat.isFile() || stat.size <= 0 || stat.size > opts.maxImageBytes) continue
|
|
1265
|
+
return { absolute, stat, root }
|
|
1266
|
+
} catch {
|
|
1267
|
+
// 换下一个 root
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
return null
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
function contentTypeFor(ext) {
|
|
1274
|
+
if (ext === '.png') return 'image/png'
|
|
1275
|
+
if (ext === '.jpg' || ext === '.jpeg') return 'image/jpeg'
|
|
1276
|
+
if (ext === '.webp') return 'image/webp'
|
|
1277
|
+
return 'application/octet-stream'
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
/** 统一 JSON 响应。 */
|
|
1281
|
+
function json(body, status) {
|
|
1282
|
+
return new Response(JSON.stringify(body), {
|
|
1283
|
+
status: status ?? 200,
|
|
1284
|
+
headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' },
|
|
1285
|
+
})
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/**
|
|
1289
|
+
* Host 插件入口。仅依赖 connection(exact Fetch 路由);缺失时插件不激活而不是抛错。
|
|
1290
|
+
*/
|
|
1291
|
+
export const inject = ['connection']
|
|
1292
|
+
|
|
1293
|
+
export function apply(ctx, config) {
|
|
1294
|
+
ctx.effect(
|
|
1295
|
+
() =>
|
|
1296
|
+
ctx.connection.fetch.register({
|
|
1297
|
+
path: ROUTE,
|
|
1298
|
+
methods: ['POST'],
|
|
1299
|
+
requestBody: 'buffered',
|
|
1300
|
+
fetch: async (request) => {
|
|
1301
|
+
let body
|
|
1302
|
+
try {
|
|
1303
|
+
body = await request.json()
|
|
1304
|
+
} catch {
|
|
1305
|
+
body = {}
|
|
1306
|
+
}
|
|
1307
|
+
const op = typeof body?.op === 'string' ? body.op : ''
|
|
1308
|
+
// 每次请求都重新解析配置:这样用户在面板里改 root 后无需重启即生效
|
|
1309
|
+
const resolved = resolveOptions(config)
|
|
1310
|
+
|
|
1311
|
+
if (op === 'list') {
|
|
1312
|
+
try {
|
|
1313
|
+
const kind = body?.kind === 'file' ? 'file' : 'image'
|
|
1314
|
+
const scope = body?.scope === 'session' ? 'session' : 'global'
|
|
1315
|
+
const sessionId = typeof body?.sessionId === 'string' ? body.sessionId : ''
|
|
1316
|
+
return json(await scanGallery(config, kind, scope, sessionId, body?.page))
|
|
1317
|
+
} catch (error) {
|
|
1318
|
+
return json({ ok: false, error: { code: 'scan-failed', message: String(error?.message ?? error) } }, 500)
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
// 读取当前设置(含 root 来源、是否存在)
|
|
1323
|
+
if (op === 'settings') {
|
|
1324
|
+
try {
|
|
1325
|
+
return json({ ok: true, settings: await readSettingsPayload(config) })
|
|
1326
|
+
} catch (error) {
|
|
1327
|
+
return json({ ok: false, error: { code: 'settings-failed', message: String(error?.message ?? error) } }, 500)
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
// 修改产出根目录(单值,保留兼容):会覆盖 roots,只留这一个
|
|
1332
|
+
if (op === 'setRoot') {
|
|
1333
|
+
const candidate = body?.root
|
|
1334
|
+
const check = await validateRootCandidate(candidate)
|
|
1335
|
+
if (check.ok !== true) {
|
|
1336
|
+
const messages = {
|
|
1337
|
+
empty: '目录不能为空',
|
|
1338
|
+
'not-absolute': '必须是绝对路径,例如 /Users/you/outputs 或 D:\\产出',
|
|
1339
|
+
'not-directory': '该路径不是目录',
|
|
1340
|
+
'not-found': '目录不存在',
|
|
1341
|
+
}
|
|
1342
|
+
return json({
|
|
1343
|
+
ok: false,
|
|
1344
|
+
error: { code: check.reason, message: messages[check.reason] ?? '目录无效' },
|
|
1345
|
+
}, 400)
|
|
1346
|
+
}
|
|
1347
|
+
try {
|
|
1348
|
+
const opts = resolveOptions(config)
|
|
1349
|
+
// 写 roots 数组,并把旧的单值 root 清掉,避免重复
|
|
1350
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
1351
|
+
delete current.root
|
|
1352
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
1353
|
+
await fsp.writeFile(
|
|
1354
|
+
opts.settingsFile,
|
|
1355
|
+
JSON.stringify({ ...current, roots: [check.root] }, null, 2),
|
|
1356
|
+
'utf8',
|
|
1357
|
+
)
|
|
1358
|
+
return json({ ok: true, settings: await readSettingsPayload(config) })
|
|
1359
|
+
} catch (error) {
|
|
1360
|
+
return json({ ok: false, error: { code: 'write-failed', message: String(error?.message ?? error) } }, 500)
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
// 追加一个扫描目录(多目录支持)
|
|
1365
|
+
if (op === 'addRoot') {
|
|
1366
|
+
const check = await validateRootCandidate(body?.root)
|
|
1367
|
+
if (check.ok !== true) {
|
|
1368
|
+
const messages = {
|
|
1369
|
+
empty: '目录不能为空',
|
|
1370
|
+
'not-absolute': '必须是绝对路径',
|
|
1371
|
+
'not-directory': '该路径不是目录',
|
|
1372
|
+
'not-found': '目录不存在',
|
|
1373
|
+
}
|
|
1374
|
+
return json({ ok: false, error: { code: check.reason, message: messages[check.reason] ?? '目录无效' } }, 400)
|
|
1375
|
+
}
|
|
1376
|
+
try {
|
|
1377
|
+
const opts = resolveOptions(config)
|
|
1378
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
1379
|
+
const list = Array.isArray(current.roots) && current.roots.length > 0
|
|
1380
|
+
? current.roots.slice()
|
|
1381
|
+
: opts.roots.slice()
|
|
1382
|
+
// 去重(大小写不敏感)
|
|
1383
|
+
if (!list.some((r) => path.resolve(r).toLowerCase() === check.root.toLowerCase())) {
|
|
1384
|
+
list.push(check.root)
|
|
1385
|
+
}
|
|
1386
|
+
delete current.root
|
|
1387
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
1388
|
+
await fsp.writeFile(opts.settingsFile, JSON.stringify({ ...current, roots: list }, null, 2), 'utf8')
|
|
1389
|
+
return json({ ok: true, settings: await readSettingsPayload(config) })
|
|
1390
|
+
} catch (error) {
|
|
1391
|
+
return json({ ok: false, error: { code: 'write-failed', message: String(error?.message ?? error) } }, 500)
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
// 移除一个扫描目录
|
|
1396
|
+
if (op === 'removeRoot') {
|
|
1397
|
+
const target = typeof body?.root === 'string' ? body.root.trim() : ''
|
|
1398
|
+
if (target === '') {
|
|
1399
|
+
return json({ ok: false, error: { code: 'empty', message: '缺少要移除的目录' } }, 400)
|
|
1400
|
+
}
|
|
1401
|
+
try {
|
|
1402
|
+
const opts = resolveOptions(config)
|
|
1403
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
1404
|
+
const list = (Array.isArray(current.roots) && current.roots.length > 0
|
|
1405
|
+
? current.roots.slice()
|
|
1406
|
+
: opts.roots.slice()
|
|
1407
|
+
).filter((r) => path.resolve(r).toLowerCase() !== path.resolve(target).toLowerCase())
|
|
1408
|
+
if (list.length === 0) {
|
|
1409
|
+
return json({ ok: false, error: { code: 'last-root', message: '至少要保留一个扫描目录' } }, 400)
|
|
1410
|
+
}
|
|
1411
|
+
delete current.root
|
|
1412
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
1413
|
+
await fsp.writeFile(opts.settingsFile, JSON.stringify({ ...current, roots: list }, null, 2), 'utf8')
|
|
1414
|
+
return json({ ok: true, settings: await readSettingsPayload(config) })
|
|
1415
|
+
} catch (error) {
|
|
1416
|
+
return json({ ok: false, error: { code: 'write-failed', message: String(error?.message ?? error) } }, 500)
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
if (op === 'raw') {
|
|
1421
|
+
const found = await resolveReadable(resolved.roots, body?.path, resolved)
|
|
1422
|
+
if (found === null) return json({ ok: false, error: { code: 'not-readable', message: '文件不存在或不可读' } }, 404)
|
|
1423
|
+
let bytes
|
|
1424
|
+
try {
|
|
1425
|
+
bytes = await fsp.readFile(found.absolute)
|
|
1426
|
+
} catch (error) {
|
|
1427
|
+
return json({ ok: false, error: { code: 'read-failed', message: String(error?.message ?? error) } }, 500)
|
|
1428
|
+
}
|
|
1429
|
+
return new Response(bytes, {
|
|
1430
|
+
status: 200,
|
|
1431
|
+
headers: {
|
|
1432
|
+
'content-type': contentTypeFor(path.extname(found.absolute).toLowerCase()),
|
|
1433
|
+
'content-length': String(bytes.byteLength),
|
|
1434
|
+
'cache-control': 'no-store',
|
|
1435
|
+
},
|
|
1436
|
+
})
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
// 在文件管理器中定位该文件(仅 Windows)
|
|
1440
|
+
if (op === 'reveal') {
|
|
1441
|
+
const found = await resolveReadable(resolved.roots, body?.path, resolved)
|
|
1442
|
+
if (found === null) return json({ ok: false, error: { code: 'not-readable', message: '文件不存在或不可读' } }, 404)
|
|
1443
|
+
try {
|
|
1444
|
+
const { spawn } = await import('node:child_process')
|
|
1445
|
+
if (process.platform === 'win32') {
|
|
1446
|
+
spawn('explorer.exe', [`/select,${found.absolute}`], { detached: true, stdio: 'ignore' }).unref()
|
|
1447
|
+
} else {
|
|
1448
|
+
spawn('xdg-open', [path.dirname(found.absolute)], { detached: true, stdio: 'ignore' }).unref()
|
|
1449
|
+
}
|
|
1450
|
+
return json({ ok: true })
|
|
1451
|
+
} catch (error) {
|
|
1452
|
+
return json({ ok: false, error: { code: 'reveal-failed', message: String(error?.message ?? error) } }, 500)
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
// ---------- 隐藏 / 取消隐藏 ----------
|
|
1457
|
+
// 隐藏只记路径,不动文件;列表扫描时过滤掉。
|
|
1458
|
+
if (op === 'hide' || op === 'unhide') {
|
|
1459
|
+
const rel = body?.path
|
|
1460
|
+
if (typeof rel !== 'string' || rel === '') {
|
|
1461
|
+
return json({ ok: false, error: { code: 'invalid-path', message: '缺少文件路径' } }, 400)
|
|
1462
|
+
}
|
|
1463
|
+
const normalized = rel.replace(/\\/g, '/')
|
|
1464
|
+
if (normalized.split('/').some((s) => s === '..' || s === '')) {
|
|
1465
|
+
return json({ ok: false, error: { code: 'invalid-path', message: '路径非法' } }, 400)
|
|
1466
|
+
}
|
|
1467
|
+
try {
|
|
1468
|
+
const opts = resolveOptions(config)
|
|
1469
|
+
const hidden = new Set(await readHiddenList(opts.settingsFile))
|
|
1470
|
+
if (op === 'hide') hidden.add(normalized)
|
|
1471
|
+
else hidden.delete(normalized)
|
|
1472
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
1473
|
+
// 只改 hidden 字段,其余设置保留
|
|
1474
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
1475
|
+
const next = { ...current, hidden: [...hidden] }
|
|
1476
|
+
await fsp.writeFile(opts.settingsFile, JSON.stringify(next, null, 2), 'utf8')
|
|
1477
|
+
return json({ ok: true, hidden: hidden.size })
|
|
1478
|
+
} catch (error) {
|
|
1479
|
+
return json({ ok: false, error: { code: 'hide-failed', message: String(error?.message ?? error) } }, 500)
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
// ---------- 全部恢复(清空隐藏列表)----------
|
|
1484
|
+
if (op === 'clearHidden') {
|
|
1485
|
+
try {
|
|
1486
|
+
const opts = resolveOptions(config)
|
|
1487
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
1488
|
+
const before = Array.isArray(current.hidden) ? current.hidden.length : 0
|
|
1489
|
+
await fsp.mkdir(path.dirname(opts.settingsFile), { recursive: true })
|
|
1490
|
+
await fsp.writeFile(
|
|
1491
|
+
opts.settingsFile,
|
|
1492
|
+
JSON.stringify({ ...current, hidden: [] }, null, 2),
|
|
1493
|
+
'utf8',
|
|
1494
|
+
)
|
|
1495
|
+
return json({ ok: true, cleared: before })
|
|
1496
|
+
} catch (error) {
|
|
1497
|
+
return json({ ok: false, error: { code: 'clear-failed', message: String(error?.message ?? error) } }, 500)
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
// ---------- 删除文件(不可逆,前端必须二次确认)----------
|
|
1502
|
+
if (op === 'delete') {
|
|
1503
|
+
const found = await resolveReadable(resolved.roots, body?.path, resolved)
|
|
1504
|
+
if (found === null) return json({ ok: false, error: { code: 'not-readable', message: '文件不存在或不可读' } }, 404)
|
|
1505
|
+
// 双保险:前端未明确确认则拒绝执行
|
|
1506
|
+
if (body?.confirm !== true) {
|
|
1507
|
+
return json({ ok: false, error: { code: 'need-confirm', message: '需要确认后才能删除' } }, 400)
|
|
1508
|
+
}
|
|
1509
|
+
try {
|
|
1510
|
+
await fsp.rm(found.absolute, { force: true })
|
|
1511
|
+
// 删除后从隐藏列表里清掉,避免残留
|
|
1512
|
+
const opts = resolveOptions(config)
|
|
1513
|
+
const hidden = new Set(await readHiddenList(opts.settingsFile))
|
|
1514
|
+
const rel = body?.path.replace(/\\/g, '/')
|
|
1515
|
+
if (hidden.has(rel)) {
|
|
1516
|
+
hidden.delete(rel)
|
|
1517
|
+
const current = readSettingsFile(opts.settingsFile)
|
|
1518
|
+
await fsp.writeFile(opts.settingsFile, JSON.stringify({ ...current, hidden: [...hidden] }, null, 2), 'utf8')
|
|
1519
|
+
}
|
|
1520
|
+
return json({ ok: true, deleted: found.absolute })
|
|
1521
|
+
} catch (error) {
|
|
1522
|
+
return json({ ok: false, error: { code: 'delete-failed', message: String(error?.message ?? error) } }, 500)
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
return json({ ok: false, error: { code: 'unknown-op', message: `未知操作:${op}` } }, 400)
|
|
1527
|
+
},
|
|
1528
|
+
}),
|
|
1529
|
+
'dsh-gallery: api route',
|
|
1530
|
+
)
|
|
1531
|
+
}
|