@wwwfz/dsh-agent-bridge 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -99
- package/THIRD_PARTY_NOTICES.md +5 -63
- package/package.json +2 -1
- package/src/assets.js +32 -19
- package/src/catalog.js +1139 -906
- package/src/client.js +1218 -623
- package/src/conversation-readers.js +198 -0
- package/src/conversation-search.js +55 -0
- package/src/conversation-store.js +747 -0
- package/src/export.js +35 -2
- package/src/handoff.js +149 -0
- package/src/history-attachment.js +41 -0
- package/src/index.js +153 -7
- package/src/native-transcript.js +217 -0
- package/src/project-archive.js +50 -0
- package/src/projects.js +877 -149
- package/src/session-import.js +65 -19
- package/src/session-trace.js +1 -1
- package/src/skill-cli.js +287 -0
- package/src/skill-history.js +129 -0
- package/src/skill-output.js +19 -0
- package/src/skill-package.js +303 -321
- package/src/skill-provider.js +155 -193
- package/src/watch.js +11 -1
- package/web/inline-process.css +22 -0
- package/web/panel.css +547 -607
- package/web/panel.html +298 -277
- package/web/panel.js +3339 -2334
- package/web/reader.css +311 -0
- package/web/relay-categories.js +3 -0
- package/web/share-redaction.js +136 -0
- package/web/share-review.js +204 -0
package/src/catalog.js
CHANGED
|
@@ -1,906 +1,1139 @@
|
|
|
1
|
-
import { execFile } from 'node:child_process'
|
|
2
|
-
import {
|
|
3
|
-
import { realpath, stat } from 'node:fs/promises'
|
|
4
|
-
import path
|
|
5
|
-
import { promisify } from 'node:util'
|
|
6
|
-
import { authorizeProjectReference, listProjectAssets, readAsset, verifyAssetSource, verifySourceAuthorization } from './assets.js'
|
|
7
|
-
import { dateMillis,
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from './
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
const
|
|
307
|
-
if (
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
await
|
|
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
|
-
if (
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
const current = await
|
|
405
|
-
const
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
const
|
|
497
|
-
if (
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
}
|
|
562
|
-
if (
|
|
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
|
-
if (!
|
|
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
|
-
const
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
const
|
|
694
|
-
const
|
|
695
|
-
const
|
|
696
|
-
if (
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
})
|
|
700
|
-
if (!
|
|
701
|
-
const
|
|
702
|
-
const
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
const
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
const
|
|
735
|
-
if (!
|
|
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
|
-
|
|
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
|
-
if (
|
|
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
|
-
if (
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
if (!ticket
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
1
|
+
import { execFile } from 'node:child_process'
|
|
2
|
+
import { randomUUID } from 'node:crypto'
|
|
3
|
+
import { realpath, stat } from 'node:fs/promises'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { promisify } from 'node:util'
|
|
6
|
+
import { authorizeProjectReference, listProjectAssets, readAsset, verifyAssetSource, verifySourceAuthorization } from './assets.js'
|
|
7
|
+
import { dateMillis, probeConversation, readProjectHistory, readSourceEvents, scanProjects } from './projects.js'
|
|
8
|
+
import {
|
|
9
|
+
approveConversation, discardConversation, finishConversation, firstContinuationMessage, locateHistoryMessage, locateRelayExcerpt,
|
|
10
|
+
readCompletedConversationImports, readContextExcerpts, readConversationSnapshot, readHistoryPage,
|
|
11
|
+
readPendingConversation, readRetainedRelaySnapshot, rebindConversation, setConversationAttempt, stageConversation, withApprovedImportLock,
|
|
12
|
+
} from './conversation-store.js'
|
|
13
|
+
import { searchConversations } from './conversation-search.js'
|
|
14
|
+
import { createSourceMonitor } from './watch.js'
|
|
15
|
+
import { makeExport } from './export.js'
|
|
16
|
+
import { readConversationAttachment } from './history-attachment.js'
|
|
17
|
+
import {
|
|
18
|
+
canReadConversation, conversationIds, conversationTitle, keyFor, makePrompt, mergeProjects, pathKey, redact,
|
|
19
|
+
} from './handoff.js'
|
|
20
|
+
import { countTrace, isSessionId, normalizeTrace } from './session-trace.js'
|
|
21
|
+
import { isMarkdownFile, relativeMarkdownLinks } from '../web/markdown.js'
|
|
22
|
+
import { relayCategories } from '../web/relay-categories.js'
|
|
23
|
+
import {
|
|
24
|
+
ProjectArchiveError, readProjectSelection, readProjectState, writeProjectArchive,
|
|
25
|
+
writeProjectContinuation, writeProjectPin, writeProjectSelection,
|
|
26
|
+
readProjectRelay, changeProjectRelay, saveCodexAliases,
|
|
27
|
+
} from './project-archive.js'
|
|
28
|
+
|
|
29
|
+
export { canReadConversation, conversationId, conversationTitle, makePrompt, mergeProjects, pathKey, redact } from './handoff.js'
|
|
30
|
+
|
|
31
|
+
const run = promisify(execFile)
|
|
32
|
+
const fallbackScanInterval = 15_000
|
|
33
|
+
const ticketLifetime = 10 * 60_000
|
|
34
|
+
const catalogId = /^[0-9a-f]{24}$/
|
|
35
|
+
const traceBudget = { items: 200, bytes: 48 * 1024, resultBytes: 4 * 1024 }
|
|
36
|
+
|
|
37
|
+
class RequestError extends Error {
|
|
38
|
+
constructor(message, status = 400) {
|
|
39
|
+
super(message)
|
|
40
|
+
this.status = status
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function gitStatus(cwd) {
|
|
45
|
+
try {
|
|
46
|
+
const { stdout } = await run('git', [
|
|
47
|
+
'--no-optional-locks', '-c', 'core.fsmonitor=false', '-C', cwd, 'status', '--short',
|
|
48
|
+
'--branch', '--untracked-files=normal', '--ignore-submodules=all',
|
|
49
|
+
], { timeout: 2500, maxBuffer: 128 * 1024, windowsHide: true, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' } })
|
|
50
|
+
return stdout.trim()
|
|
51
|
+
} catch {
|
|
52
|
+
return ''
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function publicProject({ archives, ...project }) {
|
|
57
|
+
return { ...project, sessions: archives.length, hasConversations: archives.some(canReadConversation) }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Source trace is shown to the user and stored with the imported session, so
|
|
61
|
+
// it is bounded and redacted exactly once here. Results are shortened before
|
|
62
|
+
// items are dropped so a long build log cannot hide the surrounding calls.
|
|
63
|
+
function limitUtf8(text, bytes) {
|
|
64
|
+
const data = Buffer.from(text, 'utf8')
|
|
65
|
+
if (data.length <= bytes) return text
|
|
66
|
+
while ((data[bytes] & 0xc0) === 0x80) bytes--
|
|
67
|
+
return data.toString('utf8', 0, bytes)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function conversationTrace(trace, messageCount, offset = 0) {
|
|
71
|
+
const items = []
|
|
72
|
+
const seen = new Set()
|
|
73
|
+
let bytes = 2 // JSON array brackets
|
|
74
|
+
let truncated = false
|
|
75
|
+
const indexOf = new Map()
|
|
76
|
+
for (const item of [...trace].reverse()) {
|
|
77
|
+
const index = Number.isInteger(item?.messageIndex) ? item.messageIndex - offset : undefined
|
|
78
|
+
// Records are shown under the question they belong to. One without a
|
|
79
|
+
// retained message, or whose message fell out of the window, cannot be
|
|
80
|
+
// placed anywhere the user can see, so it is dropped.
|
|
81
|
+
if (index === undefined || index < 0 || index >= messageCount) {
|
|
82
|
+
truncated = true
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
const text = redact(item?.text ?? '')
|
|
86
|
+
const bounded = limitUtf8(text, item?.kind === 'tool_result' ? traceBudget.resultBytes : 2000)
|
|
87
|
+
const [entry] = normalizeTrace([{
|
|
88
|
+
...item,
|
|
89
|
+
name: item.name ? redact(item.name) : undefined,
|
|
90
|
+
text: bounded,
|
|
91
|
+
truncated: text !== bounded || item.truncated === true,
|
|
92
|
+
messageIndex: index,
|
|
93
|
+
}])
|
|
94
|
+
if (!entry || seen.has(entry.id)) continue
|
|
95
|
+
const cost = Buffer.byteLength(JSON.stringify(entry)) + (items.length ? 1 : 0)
|
|
96
|
+
if (items.length >= traceBudget.items || bytes + cost > traceBudget.bytes) {
|
|
97
|
+
truncated = true
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
seen.add(entry.id)
|
|
101
|
+
bytes += cost
|
|
102
|
+
truncated ||= entry.truncated === true
|
|
103
|
+
items.unshift(entry)
|
|
104
|
+
indexOf.set(index, [entry.id, ...(indexOf.get(index) ?? [])])
|
|
105
|
+
}
|
|
106
|
+
return { trace: items, indexOf, truncated }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function currentDirectory(cwd) {
|
|
110
|
+
try {
|
|
111
|
+
const canonical = await realpath(cwd)
|
|
112
|
+
if ((await stat(canonical)).isDirectory()) return canonical
|
|
113
|
+
} catch {
|
|
114
|
+
// Historical sources may point to an offline drive or a removed worktree.
|
|
115
|
+
}
|
|
116
|
+
return null
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function conversationDirectory(cwd) {
|
|
120
|
+
if (typeof cwd !== 'string' || cwd.length > 4096 || !path.isAbsolute(cwd) || /[\u0000-\u001f]/u.test(cwd)) return
|
|
121
|
+
const canonical = await currentDirectory(cwd)
|
|
122
|
+
if (!canonical || canonical === path.parse(canonical).root) return
|
|
123
|
+
const info = await stat(canonical, { bigint: true }).catch(() => undefined)
|
|
124
|
+
if (info?.isDirectory()) return { path: canonical, identity: `${info.dev}:${info.ino}` }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function hasUnwrittenAttempt(record, state) {
|
|
128
|
+
return record?.state === 'approved' && !record.seed && record.importAttempt === state
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function sortProjects(a, b) {
|
|
132
|
+
return Number(b.online) - Number(a.online)
|
|
133
|
+
|| (b.updatedAt ?? '').localeCompare(a.updatedAt ?? '')
|
|
134
|
+
|| a.name.localeCompare(b.name, 'zh-CN')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function applyProjectState(projects, { archivedIds, preferences }) {
|
|
138
|
+
const visibleState = []
|
|
139
|
+
for (const project of projects) {
|
|
140
|
+
const preference = preferences.get(project.id)
|
|
141
|
+
project.archived = archivedIds.has(project.id)
|
|
142
|
+
project.pinned = preference?.pinned ?? false
|
|
143
|
+
project.continuedAt = preference?.continuedAt ?? null
|
|
144
|
+
if (project.archived || project.pinned || project.continuedAt !== null) {
|
|
145
|
+
visibleState.push([project.id, project.archived, project.pinned, project.continuedAt])
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return keyFor(visibleState.sort((a, b) => a[0].localeCompare(b[0])))
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function storageFailure(operation, error, subject = '项目归档记录') {
|
|
152
|
+
const sqliteCode = error.errcode & 0xff
|
|
153
|
+
let detail = '请检查 agent-bridge-archive.sqlite 及其所在目录。'
|
|
154
|
+
if (error instanceof ProjectArchiveError) detail = error.message
|
|
155
|
+
else if ([5, 6].includes(sqliteCode)) detail = '归档数据库正忙,请重试。'
|
|
156
|
+
else if ([11, 26].includes(sqliteCode)) detail = '归档文件已损坏或格式无效。'
|
|
157
|
+
else if ([3, 8, 14].includes(sqliteCode) || ['EACCES', 'EPERM', 'EROFS'].includes(error.code)) {
|
|
158
|
+
detail = '请检查归档文件和目录的访问权限。'
|
|
159
|
+
}
|
|
160
|
+
return new RequestError(`无法${operation}${subject}。${detail}`, 503)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function loadProjectState(config) {
|
|
164
|
+
try { return await readProjectState(config) }
|
|
165
|
+
catch (error) { throw storageFailure('读取', error, '项目归档和偏好记录') }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function isCatalogId(id) {
|
|
169
|
+
return typeof id === 'string' && catalogId.test(id)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function requireProject(current, projectId) {
|
|
173
|
+
const project = current.projects.find(item => item.id === projectId)
|
|
174
|
+
if (!project) throw new RequestError('项目目录已刷新,请重新选择项目。', 404)
|
|
175
|
+
return project
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function createCatalog(loadConfig, { importConversation, canOpenConversation = () => true, skillHandoff, loadSourceRecord } = {}) {
|
|
179
|
+
let snapshot
|
|
180
|
+
let scanning
|
|
181
|
+
let revision = 0
|
|
182
|
+
let epoch = 0
|
|
183
|
+
let disposed = false
|
|
184
|
+
const details = new Map()
|
|
185
|
+
const signatures = new Map()
|
|
186
|
+
const skillDetailRevisions = new Map()
|
|
187
|
+
const tickets = new Map()
|
|
188
|
+
const openReferences = new Map()
|
|
189
|
+
const conversationTickets = new Map()
|
|
190
|
+
const sourceIndexes = new WeakMap()
|
|
191
|
+
const watched = new Map()
|
|
192
|
+
const listeners = new Set()
|
|
193
|
+
const monitor = createSourceMonitor({ onChange() {
|
|
194
|
+
epoch++
|
|
195
|
+
wake()
|
|
196
|
+
} })
|
|
197
|
+
|
|
198
|
+
function wake() {
|
|
199
|
+
for (const listener of listeners) listener()
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function waitForChange(knownRevision) {
|
|
203
|
+
if (knownRevision !== revision || snapshot?.epoch !== epoch || listeners.size >= 32 || disposed) return
|
|
204
|
+
return new Promise(resolve => {
|
|
205
|
+
function done() {
|
|
206
|
+
clearTimeout(timer)
|
|
207
|
+
listeners.delete(done)
|
|
208
|
+
resolve()
|
|
209
|
+
}
|
|
210
|
+
const timer = setTimeout(done, 5_000)
|
|
211
|
+
listeners.add(done)
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function synchronizeProjectState() {
|
|
216
|
+
if (scanning) await scanning
|
|
217
|
+
const current = snapshot
|
|
218
|
+
const state = await loadProjectState(current.config)
|
|
219
|
+
if (scanning || snapshot !== current) return synchronizeProjectState()
|
|
220
|
+
const stateFingerprint = applyProjectState(current.projects, state)
|
|
221
|
+
if (current.stateFingerprint !== stateFingerprint) {
|
|
222
|
+
current.stateFingerprint = stateFingerprint
|
|
223
|
+
revision++
|
|
224
|
+
wake()
|
|
225
|
+
}
|
|
226
|
+
return current
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async function scan(refresh = false) {
|
|
230
|
+
if (scanning) return scanning
|
|
231
|
+
if (!refresh && snapshot?.epoch === epoch && Date.now() - snapshot.scannedAt < fallbackScanInterval) {
|
|
232
|
+
return synchronizeProjectState()
|
|
233
|
+
}
|
|
234
|
+
scanning = (async () => {
|
|
235
|
+
const scanEpoch = epoch
|
|
236
|
+
const config = await loadConfig()
|
|
237
|
+
const result = await scanProjects(config)
|
|
238
|
+
const resolved = new Map()
|
|
239
|
+
const aliases = [...new Set(result.projects.map(item => item.cwd))]
|
|
240
|
+
for (let i = 0; i < aliases.length; i += 64) {
|
|
241
|
+
await Promise.all(aliases.slice(i, i + 64).map(async cwd => resolved.set(cwd, await currentDirectory(cwd))))
|
|
242
|
+
}
|
|
243
|
+
const projects = mergeProjects(result.projects.map(item => ({ ...item, cwd: resolved.get(item.cwd) || item.cwd })))
|
|
244
|
+
const online = new Set([...resolved.values()].filter(Boolean).map(value => pathKey(value)))
|
|
245
|
+
for (const project of projects) {
|
|
246
|
+
project.online = online.has(pathKey(project.cwd))
|
|
247
|
+
if (watched.has(project.cwd)) watched.set(project.cwd, { cwd: project.cwd, projectIds: project.projectIds, archives: project.archives })
|
|
248
|
+
}
|
|
249
|
+
projects.sort(sortProjects)
|
|
250
|
+
const fingerprint = keyFor(projects, result.sources)
|
|
251
|
+
const stateFingerprint = applyProjectState(projects, await loadProjectState(config))
|
|
252
|
+
const changed = refresh || snapshot?.epoch !== scanEpoch || snapshot?.fingerprint !== fingerprint
|
|
253
|
+
|| snapshot?.stateFingerprint !== stateFingerprint
|
|
254
|
+
details.clear()
|
|
255
|
+
snapshot = { config, projects, sources: result.sources, scannedAt: Date.now(), fingerprint, stateFingerprint, epoch: scanEpoch, conversationLists: new Map() }
|
|
256
|
+
await monitor.update(config, [...watched.values()])
|
|
257
|
+
if (changed) {
|
|
258
|
+
revision++
|
|
259
|
+
wake()
|
|
260
|
+
}
|
|
261
|
+
return snapshot
|
|
262
|
+
})().finally(() => { scanning = undefined })
|
|
263
|
+
return scanning
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async function setProjectArchive(input) {
|
|
267
|
+
if (typeof input.archived !== 'boolean' || !Array.isArray(input.projectIds) || !input.projectIds.length
|
|
268
|
+
|| !input.projectIds.every(isCatalogId)) {
|
|
269
|
+
throw new RequestError('归档请求格式无效,请重新选择项目。')
|
|
270
|
+
}
|
|
271
|
+
const projectIds = [...new Set(input.projectIds)]
|
|
272
|
+
const current = await scan()
|
|
273
|
+
const known = new Set(current.projects.map(project => project.id))
|
|
274
|
+
if (projectIds.some(id => !known.has(id))) throw new RequestError('有项目已不在当前目录中,请刷新后重试。', 404)
|
|
275
|
+
let changedProjectIds
|
|
276
|
+
try { changedProjectIds = await writeProjectArchive(current.config, projectIds, input.archived) }
|
|
277
|
+
catch (error) { throw storageFailure('保存', error) }
|
|
278
|
+
await synchronizeProjectState()
|
|
279
|
+
return { projectIds, changedProjectIds, archived: input.archived, revision }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function setProjectPin({ projectId, pinned }) {
|
|
283
|
+
if (!isCatalogId(projectId) || typeof pinned !== 'boolean') throw new RequestError('置顶请求格式无效。')
|
|
284
|
+
const current = await scan()
|
|
285
|
+
requireProject(current, projectId)
|
|
286
|
+
try { await writeProjectPin(current.config, projectId, pinned) }
|
|
287
|
+
catch (error) { throw storageFailure('保存', error, '项目置顶') }
|
|
288
|
+
await synchronizeProjectState()
|
|
289
|
+
return { projectId, pinned, revision }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async function setProjectSelection({ projectId, assetIds, history }) {
|
|
293
|
+
if (!isCatalogId(projectId) || typeof history !== 'boolean'
|
|
294
|
+
|| !Array.isArray(assetIds) || assetIds.length > 24 || !assetIds.every(isCatalogId)) {
|
|
295
|
+
throw new RequestError('资料选择格式无效,每个项目最多记住 24 项。')
|
|
296
|
+
}
|
|
297
|
+
const current = snapshot ?? await scan()
|
|
298
|
+
requireProject(current, projectId)
|
|
299
|
+
const selection = { assetIds: [...new Set(assetIds)], history }
|
|
300
|
+
try { await writeProjectSelection(current.config, projectId, selection) }
|
|
301
|
+
catch (error) { throw storageFailure('保存', error, '项目资料选择') }
|
|
302
|
+
return { projectId, selection }
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
async function recordProjectContinuation(id) {
|
|
306
|
+
const ticket = getTicket(id)
|
|
307
|
+
if (!ticket.opened) throw new RequestError('请先打开交接草稿,再记录接续。', 409)
|
|
308
|
+
ticket.continuation ??= writeProjectContinuation(ticket.config, ticket.projectId).catch(error => {
|
|
309
|
+
delete ticket.continuation
|
|
310
|
+
throw storageFailure('保存', error, '项目接续记录')
|
|
311
|
+
})
|
|
312
|
+
const continuedAt = await ticket.continuation
|
|
313
|
+
await synchronizeProjectState()
|
|
314
|
+
return { projectId: ticket.projectId, continuedAt, revision }
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function detail(id) {
|
|
318
|
+
const current = await scan()
|
|
319
|
+
const { config } = current
|
|
320
|
+
const project = requireProject(current, id)
|
|
321
|
+
if (skillHandoff?.revision) {
|
|
322
|
+
try {
|
|
323
|
+
const skillRevision = await skillHandoff.revision()
|
|
324
|
+
if (skillDetailRevisions.get(id) !== skillRevision) {
|
|
325
|
+
skillDetailRevisions.set(id, skillRevision)
|
|
326
|
+
if (details.has(id)) {
|
|
327
|
+
details.delete(id)
|
|
328
|
+
signatures.delete(id)
|
|
329
|
+
revision++
|
|
330
|
+
wake()
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
} catch { /* The text-only path remains available without a skill service. */ }
|
|
334
|
+
}
|
|
335
|
+
if (details.has(id)) return details.get(id)
|
|
336
|
+
const pending = (async () => {
|
|
337
|
+
if (!watched.has(project.cwd)) {
|
|
338
|
+
if (watched.size >= 4) watched.delete(watched.keys().next().value)
|
|
339
|
+
watched.set(project.cwd, { cwd: project.cwd, projectIds: project.projectIds, archives: project.archives })
|
|
340
|
+
await monitor.update(config, [...watched.values()])
|
|
341
|
+
}
|
|
342
|
+
const [{ memories, skills }, history] = await Promise.all([
|
|
343
|
+
listProjectAssets(project, config),
|
|
344
|
+
readProjectHistory(project, config),
|
|
345
|
+
])
|
|
346
|
+
const used = new Set(history.usedSkills.map(name => name.toLowerCase()))
|
|
347
|
+
const normalize = (item, kind) => ({
|
|
348
|
+
...item, kind, id: keyFor(kind, item.file, item.agent, item.scope),
|
|
349
|
+
used: kind === 'skill' && used.has(item.name.toLowerCase()),
|
|
350
|
+
})
|
|
351
|
+
const normalizedSkills = skills.map(item => normalize(item, 'skill'))
|
|
352
|
+
const inspected = skillHandoff
|
|
353
|
+
? await skillHandoff.inspect(project, normalizedSkills)
|
|
354
|
+
: {
|
|
355
|
+
available: false,
|
|
356
|
+
projectRoot: undefined,
|
|
357
|
+
skills: Object.fromEntries(normalizedSkills.map(item => [item.id, {
|
|
358
|
+
label: '仅可带入说明',
|
|
359
|
+
state: 'handoff',
|
|
360
|
+
action: 'view',
|
|
361
|
+
allowed: false,
|
|
362
|
+
current: false,
|
|
363
|
+
sourceChanged: false,
|
|
364
|
+
reason: '当前 DSH 未提供原生技能服务。',
|
|
365
|
+
}])),
|
|
366
|
+
}
|
|
367
|
+
for (const item of normalizedSkills) {
|
|
368
|
+
if (inspected.skills[item.id]) item.handoff = inspected.skills[item.id]
|
|
369
|
+
}
|
|
370
|
+
const result = {
|
|
371
|
+
project, history, config,
|
|
372
|
+
memories: memories.map(item => normalize(item, 'memory')),
|
|
373
|
+
skills: normalizedSkills,
|
|
374
|
+
skillNativeAvailable: inspected.available,
|
|
375
|
+
skillProjectRoot: inspected.projectRoot,
|
|
376
|
+
managedSkills: inspected.managedSkills ?? [],
|
|
377
|
+
}
|
|
378
|
+
if (current === snapshot) {
|
|
379
|
+
const assets = [...result.memories, ...result.skills].map(item => [publicAsset(item), item.revision])
|
|
380
|
+
const signature = keyFor(assets, result.managedSkills, history)
|
|
381
|
+
if (signatures.has(id) && signatures.get(id) !== signature) {
|
|
382
|
+
revision++
|
|
383
|
+
wake()
|
|
384
|
+
}
|
|
385
|
+
if (!signatures.has(id) && signatures.size >= 24) signatures.delete(signatures.keys().next().value)
|
|
386
|
+
signatures.set(id, signature)
|
|
387
|
+
}
|
|
388
|
+
return result
|
|
389
|
+
})().catch(error => {
|
|
390
|
+
if (details.get(id) === pending) details.delete(id)
|
|
391
|
+
throw error
|
|
392
|
+
})
|
|
393
|
+
if (details.size >= 24) details.delete(details.keys().next().value)
|
|
394
|
+
details.set(id, pending)
|
|
395
|
+
return pending
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function publicAsset({ id, name, agent, scope, detail, file, used, handoff }) {
|
|
399
|
+
return { id, name, agent, scope, detail, file, used, ...(handoff ? { handoff } : {}) }
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
async function prepare(input) {
|
|
403
|
+
const entry = await detail(input.projectId)
|
|
404
|
+
const current = await currentDirectory(entry.project.cwd)
|
|
405
|
+
const canImport = Boolean(current && pathKey(current) === pathKey(entry.project.cwd))
|
|
406
|
+
if (!Array.isArray(input.assetIds) || input.assetIds.length > 24 || input.assetIds.some(id => typeof id !== 'string')) {
|
|
407
|
+
throw new RequestError('每次交接最多选择 24 项记忆和 Skills。')
|
|
408
|
+
}
|
|
409
|
+
const selected = new Set(input.assetIds)
|
|
410
|
+
const assets = [...entry.memories, ...entry.skills].filter(item => selected.has(item.id))
|
|
411
|
+
if (assets.length !== selected.size) throw new RequestError('所选资料已变化,请刷新项目。', 409)
|
|
412
|
+
if (input.nativeSkillIds !== undefined && (!Array.isArray(input.nativeSkillIds)
|
|
413
|
+
|| input.nativeSkillIds.length > 8
|
|
414
|
+
|| input.nativeSkillIds.some(id => typeof id !== 'string' || !selected.has(id)))) {
|
|
415
|
+
throw new RequestError('已启用技能选择格式无效。')
|
|
416
|
+
}
|
|
417
|
+
const nativeSkillIds = new Set(input.nativeSkillIds ?? [])
|
|
418
|
+
const nativeAssets = assets.filter(item => item.kind === 'skill' && nativeSkillIds.has(item.id))
|
|
419
|
+
if (nativeAssets.length !== nativeSkillIds.size) {
|
|
420
|
+
throw new RequestError('已启用技能必须同时属于本次资料选择。')
|
|
421
|
+
}
|
|
422
|
+
let nativeApprovals = []
|
|
423
|
+
if (nativeAssets.length) {
|
|
424
|
+
if (!skillHandoff?.authorizeTextOmission) {
|
|
425
|
+
throw new RequestError('当前 DSH 无法确认技能的原生登记状态。', 409)
|
|
426
|
+
}
|
|
427
|
+
nativeApprovals = await skillHandoff.authorizeTextOmission(entry.project, nativeAssets)
|
|
428
|
+
}
|
|
429
|
+
const sectionAssets = assets.filter(item => !(item.kind === 'skill' && nativeSkillIds.has(item.id)))
|
|
430
|
+
const sections = await Promise.all(sectionAssets.map(async item => {
|
|
431
|
+
const result = await readAsset(item).catch(() => {
|
|
432
|
+
throw new RequestError(`无法读取「${item.name}」。请刷新后重试。`, 409)
|
|
433
|
+
})
|
|
434
|
+
return {
|
|
435
|
+
label: `${item.kind === 'memory' ? '记忆' : 'Skill'} · ${item.name}(${item.agent})`,
|
|
436
|
+
file: item.file, assetId: item.id, ...result,
|
|
437
|
+
handoff: assets.length === 1 && (item.agent === 'export' || item.detail === '项目交接'),
|
|
438
|
+
}
|
|
439
|
+
}))
|
|
440
|
+
if (nativeAssets.length) {
|
|
441
|
+
const approved = new Map(nativeApprovals.map(item => [item.assetId, item]))
|
|
442
|
+
sections.unshift({
|
|
443
|
+
label: '已启用 Skill',
|
|
444
|
+
text: `${nativeAssets.map(item => {
|
|
445
|
+
const name = item.handoff?.dshName || item.name
|
|
446
|
+
return approved.get(item.id)?.invocation?.modelInvocable === false ? `${name}(仅手动)` : name
|
|
447
|
+
}).join('、')} 已由 DSH 原生技能目录提供;按需调用,不在此重复正文。`,
|
|
448
|
+
})
|
|
449
|
+
}
|
|
450
|
+
let historyTrace
|
|
451
|
+
if (input.history === true) {
|
|
452
|
+
const retained = entry.history.messages.slice(-8)
|
|
453
|
+
const offset = entry.history.messages.length - retained.length
|
|
454
|
+
for (const message of retained) {
|
|
455
|
+
if (message.role !== 'user' && message.role !== 'assistant') continue
|
|
456
|
+
const author = message.role === 'user' ? '用户' : '助手'
|
|
457
|
+
sections.push({
|
|
458
|
+
label: [`近期会话 · ${author}`, message.agent].filter(Boolean).join(' · '),
|
|
459
|
+
text: message.text, conversation: true, markdown: message.role === 'assistant',
|
|
460
|
+
})
|
|
461
|
+
}
|
|
462
|
+
// The draft only carries the last few turns, so the trace is trimmed to
|
|
463
|
+
// the same window and re-indexed before it reaches the export preview.
|
|
464
|
+
historyTrace = conversationTrace(entry.history.trace ?? [], retained.length, offset).trace
|
|
465
|
+
}
|
|
466
|
+
const { prompt, truncated, preview } = makePrompt(entry.project, sections, canImport ? await gitStatus(current) : '')
|
|
467
|
+
for (const section of preview.sections) {
|
|
468
|
+
if (!isMarkdownFile(section.file)) continue
|
|
469
|
+
const asset = sectionAssets.find(item => item.id === section.assetId)
|
|
470
|
+
if (asset) section.references = await markdownReferences(entry, asset, section.text)
|
|
471
|
+
}
|
|
472
|
+
if (historyTrace?.length) preview.trace = historyTrace
|
|
473
|
+
for (const [key, value] of tickets) {
|
|
474
|
+
if (Date.now() - value.created > ticketLifetime) tickets.delete(key)
|
|
475
|
+
}
|
|
476
|
+
if (tickets.size >= 40) tickets.delete(tickets.keys().next().value)
|
|
477
|
+
const ticket = randomUUID()
|
|
478
|
+
tickets.set(ticket, {
|
|
479
|
+
created: Date.now(), path: canImport ? current : null, prompt, truncated, title: entry.project.name,
|
|
480
|
+
projectId: entry.project.id, config: entry.config, preview,
|
|
481
|
+
})
|
|
482
|
+
const skillCandidates = entry.skills.filter(item => selected.has(item.id)).map(item => ({
|
|
483
|
+
assetId: item.id,
|
|
484
|
+
...item.handoff,
|
|
485
|
+
}))
|
|
486
|
+
return { ticket, prompt, truncated, canImport, preview, skillCandidates }
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function getTicket(id) {
|
|
490
|
+
const ticket = tickets.get(id)
|
|
491
|
+
if (!ticket || Date.now() - ticket.created > ticketLifetime) throw new RequestError('预览已过期,请重新生成。', 409)
|
|
492
|
+
return ticket
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
async function markdownReferences(entry, asset, text) {
|
|
496
|
+
const hrefs = relativeMarkdownLinks(text).slice(0, 128)
|
|
497
|
+
if (!hrefs.length) return []
|
|
498
|
+
const root = await currentDirectory(entry.project.cwd)
|
|
499
|
+
if (!root || pathKey(root) !== pathKey(entry.project.cwd)) return []
|
|
500
|
+
const references = await Promise.all(hrefs.map(async href => {
|
|
501
|
+
const { path: target, authorization, error, revealOnly } = await authorizeProjectReference(root, asset.file, href)
|
|
502
|
+
if (!authorization) return { href, path: target, error }
|
|
503
|
+
const id = randomUUID()
|
|
504
|
+
if (openReferences.size >= 2048) openReferences.delete(openReferences.keys().next().value)
|
|
505
|
+
openReferences.set(id, { projectId: entry.project.id, asset, authorization, revealOnly, created: Date.now() })
|
|
506
|
+
return { href, path: target, id }
|
|
507
|
+
}))
|
|
508
|
+
return references
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function getOpenReference(id, projectId) {
|
|
512
|
+
const reference = openReferences.get(id)
|
|
513
|
+
if (!reference || reference.projectId !== projectId || Date.now() - reference.created > ticketLifetime) {
|
|
514
|
+
throw new RequestError('文件链接已失效,请重新打开预览。', 409)
|
|
515
|
+
}
|
|
516
|
+
return reference
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
async function verifyOpenReference(reference, current) {
|
|
520
|
+
if (!current || pathKey(current) !== pathKey(reference.authorization.root)) {
|
|
521
|
+
throw new RequestError('项目目录已移动或不可访问。', 409)
|
|
522
|
+
}
|
|
523
|
+
try {
|
|
524
|
+
await verifyAssetSource(reference.asset)
|
|
525
|
+
const target = await verifySourceAuthorization(reference.authorization)
|
|
526
|
+
return { path: target.file, kind: 'file', name: path.basename(target.file), revealOnly: reference.revealOnly }
|
|
527
|
+
} catch {
|
|
528
|
+
throw new RequestError('链接文件已移动、被替换或不在项目范围内。', 409)
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
async function exportPreview(ticket) {
|
|
533
|
+
const current = ticket.path && await currentDirectory(ticket.path)
|
|
534
|
+
if (!current) return ticket.preview
|
|
535
|
+
const sections = await Promise.all(ticket.preview.sections.map(async section => {
|
|
536
|
+
if (!section.references?.length) return section
|
|
537
|
+
const references = await Promise.all(section.references.map(async reference => {
|
|
538
|
+
if (!reference.id) return reference
|
|
539
|
+
try {
|
|
540
|
+
const grant = getOpenReference(reference.id, ticket.projectId)
|
|
541
|
+
const target = await verifyOpenReference(grant, current)
|
|
542
|
+
if (!target.revealOnly) return { ...reference, verifiedFile: target.path }
|
|
543
|
+
} catch { /* A stale reference is exported as text. */ }
|
|
544
|
+
return reference
|
|
545
|
+
}))
|
|
546
|
+
return { ...section, references }
|
|
547
|
+
}))
|
|
548
|
+
return { ...ticket.preview, sections }
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// The panel never supplies a free-form path for opening. It names an asset
|
|
552
|
+
// that is currently listed for the project (or the project directory
|
|
553
|
+
// itself), and the host re-reads the recorded authorization so a replaced
|
|
554
|
+
// file, a junction, or a moved source is refused instead of opened.
|
|
555
|
+
async function resolveOpenPath(input) {
|
|
556
|
+
const entry = await detail(input.projectId)
|
|
557
|
+
if (input.referenceId !== undefined) {
|
|
558
|
+
const reference = getOpenReference(input.referenceId, entry.project.id)
|
|
559
|
+
const current = await currentDirectory(entry.project.cwd)
|
|
560
|
+
return verifyOpenReference(reference, current)
|
|
561
|
+
}
|
|
562
|
+
if (input.directory === true) {
|
|
563
|
+
const current = await currentDirectory(entry.project.cwd)
|
|
564
|
+
if (!current || pathKey(current) !== pathKey(entry.project.cwd)) {
|
|
565
|
+
throw new RequestError('项目目录已移动或不可访问。', 409)
|
|
566
|
+
}
|
|
567
|
+
return { path: current, kind: 'directory', name: entry.project.name }
|
|
568
|
+
}
|
|
569
|
+
if (typeof input.assetId !== 'string' || !isCatalogId(input.assetId)) {
|
|
570
|
+
throw new RequestError('资料标识无效。')
|
|
571
|
+
}
|
|
572
|
+
const item = [...entry.memories, ...entry.skills].find(asset => asset.id === input.assetId)
|
|
573
|
+
if (!item) throw new RequestError('找不到所选资料,请刷新项目。', 404)
|
|
574
|
+
let source
|
|
575
|
+
try {
|
|
576
|
+
source = await verifyAssetSource(item)
|
|
577
|
+
} catch {
|
|
578
|
+
throw new RequestError('资料已移动或内容已变化,请刷新项目后重试。', 409)
|
|
579
|
+
}
|
|
580
|
+
return { path: source.file, kind: 'file', name: item.name }
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function conversationSources(project) {
|
|
584
|
+
if (sourceIndexes.has(project)) return sourceIndexes.get(project)
|
|
585
|
+
const archives = project.archives
|
|
586
|
+
.filter(canReadConversation)
|
|
587
|
+
.sort((a, b) => dateMillis(b.date) - dateMillis(a.date))
|
|
588
|
+
const current = new Map()
|
|
589
|
+
const aliases = new Map()
|
|
590
|
+
for (const archive of archives) {
|
|
591
|
+
const [id, ...oldIds] = conversationIds(archive)
|
|
592
|
+
if (!current.has(id)) current.set(id, { id, ids: [id], archive })
|
|
593
|
+
const source = current.get(id)
|
|
594
|
+
for (const alias of oldIds) {
|
|
595
|
+
if (!aliases.has(alias)) aliases.set(alias, source)
|
|
596
|
+
else if (aliases.get(alias) !== source) aliases.set(alias, null)
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
const byId = new Map(current)
|
|
600
|
+
for (const [alias, source] of aliases) {
|
|
601
|
+
if (!source || byId.has(alias)) continue
|
|
602
|
+
source.ids.push(alias)
|
|
603
|
+
byId.set(alias, source)
|
|
604
|
+
}
|
|
605
|
+
const index = { current: [...current.values()], byId }
|
|
606
|
+
sourceIndexes.set(project, index)
|
|
607
|
+
return index
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
function sourceFor(project, id) {
|
|
611
|
+
return (project && conversationSources(project).byId.get(id)) ?? { id, ids: [id] }
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
async function rememberLegacyLinks(config, projectId, links) {
|
|
615
|
+
const verified = links.filter(([source, alias]) =>
|
|
616
|
+
source.archive?.profile && source.id !== alias && !source.archive.savedIds?.includes(alias))
|
|
617
|
+
if (!verified.length) return
|
|
618
|
+
// A read-only installation can still use the currently verified filesystem links.
|
|
619
|
+
try { await saveCodexAliases(config, projectId, verified) } catch { return }
|
|
620
|
+
for (const [source, alias] of verified) {
|
|
621
|
+
const saved = source.archive.savedIds ??= []
|
|
622
|
+
if (!saved.includes(alias)) saved.push(alias)
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
async function readableConversations(project, config) {
|
|
627
|
+
const candidates = conversationSources(project).current
|
|
628
|
+
const sessions = []
|
|
629
|
+
for (let offset = 0; offset < candidates.length; offset += 8) {
|
|
630
|
+
const batch = await Promise.all(candidates.slice(offset, offset + 8).map(async ({ id, archive }) => {
|
|
631
|
+
const probe = await probeConversation(archive, { sourceCwd: project.cwd, config })
|
|
632
|
+
if (probe.state === 'empty') return undefined
|
|
633
|
+
return {
|
|
634
|
+
id, agent: archive.agent ?? archive.kind, date: dateMillis(archive.date),
|
|
635
|
+
title: conversationTitle(archive.title), archivedSource: archive.kind === 'agentsview',
|
|
636
|
+
...(probe.state === 'unknown' ? { unavailable: true, reason: probe.reason } : {}),
|
|
637
|
+
}
|
|
638
|
+
}))
|
|
639
|
+
sessions.push(...batch.filter(Boolean))
|
|
640
|
+
}
|
|
641
|
+
return sessions
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
async function loadImportStates(config, projectId, sources) {
|
|
645
|
+
const owners = new Map()
|
|
646
|
+
for (const source of sources) for (const alias of source.ids) owners.set(alias, source)
|
|
647
|
+
const grouped = new Map()
|
|
648
|
+
const entries = await readCompletedConversationImports(config, projectId, [...owners.keys()])
|
|
649
|
+
await rememberLegacyLinks(config, projectId, entries.map(entry => [owners.get(entry.sourceId), entry.sourceId]))
|
|
650
|
+
for (const entry of entries) {
|
|
651
|
+
const { id } = owners.get(entry.sourceId)
|
|
652
|
+
if (!grouped.has(id)) grouped.set(id, [])
|
|
653
|
+
grouped.get(id).push(entry)
|
|
654
|
+
}
|
|
655
|
+
return new Map(await Promise.all([...grouped].map(async ([sourceId, entries]) => {
|
|
656
|
+
const [latest] = entries
|
|
657
|
+
const unavailableImports = []
|
|
658
|
+
let sessionId = null
|
|
659
|
+
for (const entry of entries) {
|
|
660
|
+
if (await canOpenConversation(entry.sessionId)) {
|
|
661
|
+
sessionId = entry.sessionId
|
|
662
|
+
break
|
|
663
|
+
}
|
|
664
|
+
unavailableImports.push(entry.sessionId)
|
|
665
|
+
}
|
|
666
|
+
return [sourceId, { unavailableImports, lastImport: {
|
|
667
|
+
sessionId, importedAt: latest.importedAt, approximate: Boolean(latest.approximate),
|
|
668
|
+
...(sessionId && sessionId !== latest.sessionId ? { older: true } : {}),
|
|
669
|
+
} }]
|
|
670
|
+
})))
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
async function withImportStates(config, project, sessions) {
|
|
674
|
+
const sources = [...new Set(sessions.map(session => session.id))].map(id => sourceFor(project, id))
|
|
675
|
+
const states = await loadImportStates(config, project.id, sources)
|
|
676
|
+
return sessions.map(session => {
|
|
677
|
+
const state = states.get(session.id)
|
|
678
|
+
return state ? { ...session, lastImport: state.lastImport } : session
|
|
679
|
+
})
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
async function importStatus(config, projectId, source) {
|
|
683
|
+
return (await loadImportStates(config, projectId, [source])).get(source.id)
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
async function listConversations(projectId, recent, cursor) {
|
|
687
|
+
const current = await scan()
|
|
688
|
+
const project = requireProject(current, projectId)
|
|
689
|
+
const { conversationLists, config } = current
|
|
690
|
+
if (!conversationLists.has(projectId)) {
|
|
691
|
+
conversationLists.set(projectId, readableConversations(project, config))
|
|
692
|
+
}
|
|
693
|
+
const sessions = await conversationLists.get(projectId)
|
|
694
|
+
const ready = sessions.filter(session => !session.unavailable)
|
|
695
|
+
const unavailable = sessions.filter(session => session.unavailable)
|
|
696
|
+
if (recent) return { sessions: await withImportStates(config, project, ready.slice(0, 3)), total: ready.length,
|
|
697
|
+
...(unavailable.length ? { unavailable: unavailable.slice(0, 20) } : {}) }
|
|
698
|
+
const marker = keyFor(ready.map(({ id }) => id))
|
|
699
|
+
const parsed = typeof cursor === 'string' && /^([0-9a-f]{24}):(\d{1,9})$/.exec(cursor)
|
|
700
|
+
if (cursor && (!parsed || parsed[1] !== marker)) throw new RequestError('会话列表已更新,请从第一页重新打开。', 409)
|
|
701
|
+
const offset = parsed ? Number(parsed[2]) : 0
|
|
702
|
+
const end = offset + 100
|
|
703
|
+
return { sessions: await withImportStates(config, project, ready.slice(offset, end)),
|
|
704
|
+
...(end < ready.length ? { nextCursor: `${marker}:${end}` } : {}),
|
|
705
|
+
...(!offset && unavailable.length ? { unavailable: unavailable.slice(0, 100) } : {}) }
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
async function previewConversation({ projectId, sessionId, targetCwd, snapshotId, searchHit, relayId, inspectSource, duplicate, signal }, recover = false) {
|
|
709
|
+
if (!isCatalogId(sessionId)) throw new RequestError('会话标识无效。')
|
|
710
|
+
if (snapshotId !== undefined && !isSessionId(snapshotId)) throw new RequestError('会话预览凭据无效,请重新打开。', 409)
|
|
711
|
+
if (searchHit && (!Number.isSafeInteger(searchHit.turn) || searchHit.turn < 1
|
|
712
|
+
|| !Number.isSafeInteger(searchHit.offset) || searchHit.offset < 0 || !isCatalogId(searchHit.hash))) {
|
|
713
|
+
throw new RequestError('搜索结果位置无效。')
|
|
714
|
+
}
|
|
715
|
+
const current = await scan()
|
|
716
|
+
const project = requireProject(current, projectId)
|
|
717
|
+
const { config } = current
|
|
718
|
+
const source = sourceFor(project, sessionId)
|
|
719
|
+
sessionId = source.id
|
|
720
|
+
const relay = relayId && (await readProjectRelay(config, projectId)).find(item =>
|
|
721
|
+
item.id === relayId && source.ids.includes(item.source.id))
|
|
722
|
+
if (relayId && !relay) throw new RequestError('接力条目已移除。', 404)
|
|
723
|
+
if (relay) await rememberLegacyLinks(config, projectId, [[source, relay.source.id]])
|
|
724
|
+
const archive = source.archive
|
|
725
|
+
const { lastImport } = await importStatus(config, projectId, source) ?? {}
|
|
726
|
+
if (lastImport?.sessionId && !snapshotId && duplicate !== true && inspectSource !== true && !searchHit && !relay && targetCwd === undefined) {
|
|
727
|
+
return { openExisting: true, lastImport, title: conversationTitle(archive?.title, 80) }
|
|
728
|
+
}
|
|
729
|
+
const saved = snapshotId && await readConversationSnapshot(config, snapshotId)
|
|
730
|
+
if (recover && (targetCwd === undefined || !hasUnwrittenAttempt(saved, 'unwritten'))) {
|
|
731
|
+
throw new RequestError('这次迁入无法改选目录,请从原凭据重试。', 409)
|
|
732
|
+
}
|
|
733
|
+
const previous = saved?.state === 'preview' || hasUnwrittenAttempt(saved, 'review') || recover ? saved : undefined
|
|
734
|
+
const sameSource = saved && (source.ids.includes(saved.sourceId) || (!archive && saved.sourceIds?.includes(sessionId)))
|
|
735
|
+
if ((snapshotId && (!saved || saved.projectId !== projectId || !sameSource))
|
|
736
|
+
|| (targetCwd !== undefined && snapshotId && !previous)) {
|
|
737
|
+
throw new RequestError('会话预览已变化,请重新打开。', 409)
|
|
738
|
+
}
|
|
739
|
+
// An offline alternate directory still has a pending approval. A supplied snapshot or
|
|
740
|
+
// explicit target must never select another receipt simply because it has the same source.
|
|
741
|
+
let pending = saved?.state === 'approved' && !previous ? saved : undefined
|
|
742
|
+
if (!snapshotId && targetCwd === undefined) {
|
|
743
|
+
pending = await readPendingConversation(config, projectId, source.ids, project.cwd)
|
|
744
|
+
?? await readPendingConversation(config, projectId, source.ids)
|
|
745
|
+
}
|
|
746
|
+
const target = await conversationDirectory(targetCwd !== undefined ? targetCwd : pending?.targetCwd ?? saved?.targetCwd ?? project.cwd)
|
|
747
|
+
if (targetCwd !== undefined && !target) throw new RequestError('目标目录不可访问,请选择本机工作目录。', 409)
|
|
748
|
+
if (!snapshotId && targetCwd !== undefined) pending = await readPendingConversation(config, projectId, source.ids, target.path)
|
|
749
|
+
const retained = !archive && relay && !snapshotId && await readRetainedRelaySnapshot(config, projectId, relay.source)
|
|
750
|
+
if (!archive && !previous && !pending && !retained) throw new RequestError('找不到这条会话,请刷新项目。', 404)
|
|
751
|
+
const metadata = {
|
|
752
|
+
id: randomUUID(), sessionId: randomUUID(), projectId, sourceId: !archive && previous ? previous.sourceId : sessionId,
|
|
753
|
+
sourceIds: [...new Set([...source.ids, ...(previous?.sourceIds ?? [])])],
|
|
754
|
+
sourceCwd: previous?.sourceCwd ?? project.cwd, targetCwd: target?.path, targetIdentity: target?.identity,
|
|
755
|
+
title: previous?.title ?? conversationTitle(archive?.title, 80),
|
|
756
|
+
}
|
|
757
|
+
let record = retained || pending || previous
|
|
758
|
+
if (previous && targetCwd !== undefined) {
|
|
759
|
+
if (previous.state === 'approved') {
|
|
760
|
+
record = await withApprovedImportLock(config, previous.id, async () => {
|
|
761
|
+
const rebound = await rebindConversation(config, previous, metadata, { approvedAttempt: previous.importAttempt })
|
|
762
|
+
if (!rebound) throw new RequestError('迁入凭据已变化,请重新打开预览。', 409)
|
|
763
|
+
return rebound
|
|
764
|
+
})
|
|
765
|
+
} else record = await rebindConversation(config, previous, metadata)
|
|
766
|
+
} else if (!record) {
|
|
767
|
+
record = await stageConversation(config, metadata, readSourceEvents(archive, { config, sourceCwd: project.cwd, signal }), redact,
|
|
768
|
+
item => readConversationAttachment(project, archive, item))
|
|
769
|
+
}
|
|
770
|
+
if (!record) throw new RequestError('会话预览已变化,请重新打开。', 409)
|
|
771
|
+
const messages = record.messages.map(({ role, text, time }) => ({ role, text, time }))
|
|
772
|
+
if (messages.length) messages[0].text = firstContinuationMessage(record)
|
|
773
|
+
const historyLocation = relay ? await locateRelayExcerpt(config, record.id, relay.source)
|
|
774
|
+
: searchHit && await locateHistoryMessage(config, record.id, searchHit)
|
|
775
|
+
const { trace, indexOf, truncated: traceTruncated } = conversationTrace(record.trace, messages.length)
|
|
776
|
+
for (let index = 0; index < messages.length; index++) {
|
|
777
|
+
if (indexOf.has(index)) messages[index].traceIds = indexOf.get(index)
|
|
778
|
+
}
|
|
779
|
+
const traceCounts = countTrace(trace)
|
|
780
|
+
const { sourceStats, history } = record
|
|
781
|
+
const title = record.title || conversationTitle(messages[0]?.text, 80) || '迁入的 Agent 会话'
|
|
782
|
+
const canImport = Boolean(!retained && target && record.targetCwd && pathKey(record.targetCwd) === pathKey(target.path)
|
|
783
|
+
&& messages.length && record.targetIdentity === target.identity)
|
|
784
|
+
const skillCandidates = skillHandoff
|
|
785
|
+
&& (!target || pathKey(target.path) === pathKey(project.cwd))
|
|
786
|
+
? await skillHandoff.conversationCandidates(project, record.usedSkills, (await detail(projectId)).skills)
|
|
787
|
+
: []
|
|
788
|
+
return {
|
|
789
|
+
title, messages, ticket: canImport ? record.id : null, canImport, cwd: project.cwd,
|
|
790
|
+
sourceCwd: record.sourceCwd ?? project.cwd, targetCwd: record.targetCwd, history,
|
|
791
|
+
resumePending: !retained && record.state === 'approved' && !hasUnwrittenAttempt(record, 'review'),
|
|
792
|
+
...(!retained && hasUnwrittenAttempt(record, 'review') ? { continuationLocked: true } : {}),
|
|
793
|
+
...(!retained && hasUnwrittenAttempt(record, 'unwritten') ? { recoveryTicket: record.id } : {}),
|
|
794
|
+
truncated: record.truncated || traceTruncated, skillCandidates, trace, traceCounts, sourceStats,
|
|
795
|
+
...(historyLocation ? { historyLocation } : searchHit ? { searchHitStale: true } : {}),
|
|
796
|
+
...(record.continuation ? { continuation: record.continuation } : {}),
|
|
797
|
+
...(lastImport ? { lastImport } : {}),
|
|
798
|
+
...(relay && (!historyLocation || historyLocation.changed) ? { relayLocationChanged: true } : {}),
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
async function projectRelay(action, input) {
|
|
803
|
+
const current = await scan()
|
|
804
|
+
const { config } = current
|
|
805
|
+
const project = requireProject(current, input.projectId)
|
|
806
|
+
if (action === 'project-relay') {
|
|
807
|
+
const items = await readProjectRelay(config, project.id)
|
|
808
|
+
await rememberLegacyLinks(config, project.id, items.map(item => [sourceFor(project, item.source.id), item.source.id]))
|
|
809
|
+
return { items }
|
|
810
|
+
}
|
|
811
|
+
if (action === 'export-relay') {
|
|
812
|
+
const items = await readProjectRelay(config, project.id)
|
|
813
|
+
if (!items.length) throw new RequestError('请先添加接力条目。', 409)
|
|
814
|
+
const sections = Object.entries(relayCategories).map(([key, label]) => ({
|
|
815
|
+
label, text: items.filter(item => item.category === key).map(({ text, source }) =>
|
|
816
|
+
[`- 来源:${source.agent} · ${source.title} · 第 ${source.turn} 轮(会话 ${source.id})`,
|
|
817
|
+
'', ...text.split(/\r\n|\n|\r/).map(line => '> ' + line)].join('\n')).join('\n\n') || '暂无条目。',
|
|
818
|
+
}))
|
|
819
|
+
return makeExport({ title: redact(project.name) + ' · 项目接力单', format: 'share',
|
|
820
|
+
preview: { cwd: project.cwd, sources: [...new Set(items.map(item => item.source.agent))], sections } })
|
|
821
|
+
}
|
|
822
|
+
if (action === 'remove-relay-item') {
|
|
823
|
+
if (!isCatalogId(input.id)) throw new RequestError('接力条目标识无效。')
|
|
824
|
+
await changeProjectRelay(config, project.id, input.id, () => null)
|
|
825
|
+
return {}
|
|
826
|
+
}
|
|
827
|
+
if (!Object.hasOwn(relayCategories, input.category)) throw new RequestError('请选择接力分类。')
|
|
828
|
+
if (action === 'update-relay-item') {
|
|
829
|
+
if (!isCatalogId(input.id) || typeof input.text !== 'string' || !input.text.trim() || input.text.length > 8000) {
|
|
830
|
+
throw new RequestError('接力正文须为 1 至 8,000 字符。')
|
|
831
|
+
}
|
|
832
|
+
return changeProjectRelay(config, project.id, input.id, previous => {
|
|
833
|
+
if (!previous) throw new RequestError('接力条目已移除。', 404)
|
|
834
|
+
return { ...previous, category: input.category, text: redact(input.text.trim()) }
|
|
835
|
+
})
|
|
836
|
+
}
|
|
837
|
+
const { snapshotId, seq, offset } = input
|
|
838
|
+
if (!isSessionId(snapshotId) || !Number.isSafeInteger(seq) || seq < 1 || !Number.isSafeInteger(offset) || offset < 0) {
|
|
839
|
+
throw new RequestError('来源摘录无效。')
|
|
840
|
+
}
|
|
841
|
+
const [excerpt] = await readContextExcerpts(config, project.id, [{ snapshotId, seq, offset }]) ?? []
|
|
842
|
+
const origin = excerpt && sourceFor(project, excerpt.sourceId)
|
|
843
|
+
if (!origin?.archive || !excerpt.turn) throw new RequestError('来源摘录已过期或没有可定位的回合。', 409)
|
|
844
|
+
const source = { id: origin.id, title: excerpt.title || '未命名对话', agent: origin.archive.agent ?? origin.archive.kind,
|
|
845
|
+
turn: excerpt.turn, kind: excerpt.kind, seq, offset, hash: keyFor(excerpt.text) }
|
|
846
|
+
const [id, ...previousIds] = origin.ids.map(alias => keyFor(alias, source.turn, seq, offset, source.hash))
|
|
847
|
+
return changeProjectRelay(config, project.id, id, previous => previous ?? {
|
|
848
|
+
id, category: input.category, text: excerpt.text, source,
|
|
849
|
+
}, previousIds)
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
async function reviewContinuation(record, config, excerpts = []) {
|
|
853
|
+
if (!Array.isArray(excerpts) || excerpts.length > 8 || !excerpts.every(excerpt =>
|
|
854
|
+
isSessionId(excerpt?.snapshotId) && Number.isSafeInteger(excerpt.seq) && excerpt.seq > 0
|
|
855
|
+
&& Number.isSafeInteger(excerpt.offset) && excerpt.offset >= 0)) {
|
|
856
|
+
throw new RequestError('每次最多带入 8 段有效的来源摘录。')
|
|
857
|
+
}
|
|
858
|
+
const unique = [...new Map(excerpts.map(({ snapshotId, seq, offset }) =>
|
|
859
|
+
[`${snapshotId}:${seq}:${offset}`, { snapshotId, seq, offset }])).values()]
|
|
860
|
+
const items = unique.length ? await readContextExcerpts(config, record.projectId, unique) : []
|
|
861
|
+
if (!items || items.some(item => !item)) throw new RequestError('部分摘录已过期或不属于当前项目,请重新勾选。', 409)
|
|
862
|
+
const labels = { user: '用户', assistant: '助手', tool_call: '工具调用', tool_result: '工具结果', summary: '摘要' }
|
|
863
|
+
const quoted = items.map(item => {
|
|
864
|
+
const source = [item.title, item.turn && `第 ${item.turn} 轮`, labels[item.role ?? item.kind]].filter(Boolean).join(' · ')
|
|
865
|
+
return `> ${source}\n>\n${item.text.split(/\r\n|\n|\r/u).map(line => `> ${line}`).join('\n')}`
|
|
866
|
+
}).join('\n\n')
|
|
867
|
+
if (quoted.length > 16_000) throw new RequestError('摘录合计超过 16,000 字符,请减少所选内容。')
|
|
868
|
+
const firstMessage = record.messages[0].text + (quoted
|
|
869
|
+
? `\n\n以下是用户选入的历史摘录,请结合当前任务核对后使用。\n\n${quoted}` : '')
|
|
870
|
+
return { contextKey: keyFor(record.id, unique, firstMessage), firstMessage, items }
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
async function previewContinuation({ ticket, excerpts }) {
|
|
874
|
+
const config = await loadConfig()
|
|
875
|
+
const record = await readConversationSnapshot(config, ticket)
|
|
876
|
+
if (record?.state !== 'preview' || !record.messages.length) throw new RequestError('会话预览已过期,请重新打开。', 409)
|
|
877
|
+
return reviewContinuation(record, config, excerpts)
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
async function writeApprovedConversation(config, ticket) {
|
|
881
|
+
return withApprovedImportLock(config, ticket, async () => {
|
|
882
|
+
const record = await readConversationSnapshot(config, ticket)
|
|
883
|
+
if (record?.state === 'done') return { sessionId: record.sessionId }
|
|
884
|
+
if (record?.state !== 'approved') throw new RequestError('迁入凭据已变化,请重新打开预览。', 409)
|
|
885
|
+
const previouslyUnwritten = !record.seed && ['ready', 'unwritten', 'review'].includes(record.importAttempt)
|
|
886
|
+
const target = await conversationDirectory(record.targetCwd)
|
|
887
|
+
record.config = config
|
|
888
|
+
record.title ||= conversationTitle(record.messages[0].text, 80) || '迁入的 Agent 会话'
|
|
889
|
+
record.trace = conversationTrace(record.trace, record.messages.length).trace
|
|
890
|
+
let result
|
|
891
|
+
try {
|
|
892
|
+
if (!target || pathKey(target.path) !== pathKey(record.targetCwd) || target.identity !== record.targetIdentity) {
|
|
893
|
+
throw Object.assign(new RequestError('目标目录已变化,请核对原迁入目录。', 409), { nativeSessionUnwritten: true })
|
|
894
|
+
}
|
|
895
|
+
// Persist uncertainty before dispatch: a crash, timeout or older receipt must
|
|
896
|
+
// never let a later pre-write failure authorize a second DSH session.
|
|
897
|
+
if (!await setConversationAttempt(config, ticket, 'unknown')) throw new RequestError('迁入凭据已变化,请重新打开预览。', 409)
|
|
898
|
+
result = await importConversation(record, target.path)
|
|
899
|
+
await finishConversation(config, ticket, record.nativeTimeline)
|
|
900
|
+
} catch (error) {
|
|
901
|
+
const unwritten = previouslyUnwritten && error.nativeSessionUnwritten === true
|
|
902
|
+
const message = unwritten ? 'DSH 尚未写入这条会话,可重试或改选目录。' : record.stored
|
|
903
|
+
? '会话历史已写入,暂时无法加入工作区。可重试打开。' : '无法确认 DSH 会话的写入结果,请在原目录重试。'
|
|
904
|
+
const failure = error.status ? error : new RequestError(message, 503)
|
|
905
|
+
if (unwritten && await setConversationAttempt(config, ticket, 'unwritten')) failure.recoveryTicket = ticket
|
|
906
|
+
throw failure
|
|
907
|
+
}
|
|
908
|
+
try {
|
|
909
|
+
await writeProjectContinuation(config, record.projectId)
|
|
910
|
+
await synchronizeProjectState()
|
|
911
|
+
} catch { /* A saved session remains usable even if the recents index cannot be updated. */ }
|
|
912
|
+
return result
|
|
913
|
+
})
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
async function commitConversation({ ticket, excerpts, contextKey, duplicate }) {
|
|
917
|
+
if (!isSessionId(ticket)) throw new RequestError('会话预览已过期,请重新打开。', 409)
|
|
918
|
+
const copy = duplicate === true
|
|
919
|
+
const requestKey = keyFor(excerpts, contextKey, copy)
|
|
920
|
+
const reuse = lastImport => ({ sessionId: lastImport.sessionId, reused: true, lastImport })
|
|
921
|
+
const active = conversationTickets.get(ticket)
|
|
922
|
+
if (active) {
|
|
923
|
+
if (active.requestKey !== requestKey) throw new RequestError('正在迁入另一份续聊预览,请等待当前操作结束。', 409)
|
|
924
|
+
return active.pending
|
|
925
|
+
}
|
|
926
|
+
const pending = (async () => {
|
|
927
|
+
const config = await loadConfig()
|
|
928
|
+
const record = await readConversationSnapshot(config, ticket)
|
|
929
|
+
if (!record?.targetCwd || !record.messages.length) throw new RequestError('会话预览已过期或不能迁入,请重新打开。', 409)
|
|
930
|
+
if (record.state !== 'preview' && (contextKey !== undefined || excerpts !== undefined)
|
|
931
|
+
&& contextKey !== record.continuation?.contextKey) {
|
|
932
|
+
throw new RequestError('续聊内容已确定,请重新打开迁入预览。', 409)
|
|
933
|
+
}
|
|
934
|
+
if (record.state === 'done') return { sessionId: record.sessionId }
|
|
935
|
+
const project = (snapshot ?? await scan()).projects.find(item => item.id === record.projectId)
|
|
936
|
+
const source = sourceFor(project, record.sourceId)
|
|
937
|
+
const sourceIds = [...new Set([...source.ids, ...(record.sourceIds ?? [])])]
|
|
938
|
+
const getStatus = () => importStatus(config, record.projectId, { ...source, ids: sourceIds })
|
|
939
|
+
const prior = record.state === 'preview' && !copy ? await getStatus() : undefined
|
|
940
|
+
if (prior?.lastImport.sessionId) return reuse(prior.lastImport)
|
|
941
|
+
if (record.state === 'preview') {
|
|
942
|
+
const target = await conversationDirectory(record.targetCwd)
|
|
943
|
+
if (!target || pathKey(target.path) !== pathKey(record.targetCwd) || target.identity !== record.targetIdentity) {
|
|
944
|
+
throw new RequestError('目标目录已变化,请重新选择目录。', 409)
|
|
945
|
+
}
|
|
946
|
+
const continuation = excerpts !== undefined || contextKey !== undefined
|
|
947
|
+
? await reviewContinuation(record, config, excerpts) : null
|
|
948
|
+
if (continuation && continuation.contextKey !== contextKey) {
|
|
949
|
+
throw new RequestError('续聊内容已变化,请重新核对预览。', 409)
|
|
950
|
+
}
|
|
951
|
+
// Recheck receipts atomically with approval, including receipts completed after the host lookup.
|
|
952
|
+
const unavailableImports = copy ? undefined : prior?.unavailableImports ?? []
|
|
953
|
+
if (!await approveConversation(config, ticket, continuation, { unavailableImports, sourceIds })) {
|
|
954
|
+
const completed = !copy && (await getStatus())?.lastImport
|
|
955
|
+
if (completed?.sessionId) return reuse(completed)
|
|
956
|
+
throw new RequestError('会话预览已过期或正在迁入,请重新打开。', 409)
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
return writeApprovedConversation(config, ticket)
|
|
960
|
+
})().finally(() => conversationTickets.delete(ticket))
|
|
961
|
+
conversationTickets.set(ticket, { pending, requestKey })
|
|
962
|
+
return pending
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
async function exportSession({ conversation, format, includeTrace, sessionId }) {
|
|
966
|
+
if (!conversation || typeof conversation.title !== 'string' || conversation.title.length > 500
|
|
967
|
+
|| typeof conversation.path !== 'string' || conversation.path.length > 4096
|
|
968
|
+
|| !path.isAbsolute(conversation.path) || /[\u0000-\u001f]/u.test(conversation.path)
|
|
969
|
+
|| !Array.isArray(conversation.messages) || conversation.messages.length > 24) {
|
|
970
|
+
throw new RequestError('DSH 会话资料格式无效。')
|
|
971
|
+
}
|
|
972
|
+
const sections = conversation.messages
|
|
973
|
+
.filter(message => (message?.role === 'user' || message?.role === 'assistant')
|
|
974
|
+
&& typeof message.text === 'string' && message.text.trim())
|
|
975
|
+
.map(message => ({ label: message.role === 'user' ? '用户' : 'DSH 助手', text: message.text,
|
|
976
|
+
conversation: true, markdown: message.role === 'assistant' }))
|
|
977
|
+
if (!sections.length) throw new RequestError('这段会话暂无可导出的消息。', 409)
|
|
978
|
+
const title = redact(conversation.title.trim()) || 'DSH 会话'
|
|
979
|
+
const { truncated, preview } = makePrompt({ name: title, cwd: conversation.path, agents: ['DSH'] }, sections, '')
|
|
980
|
+
const saved = includeTrace === true ? await loadSourceRecord?.(sessionId) : undefined
|
|
981
|
+
return makeExport({
|
|
982
|
+
title, origin: 'DSH 会话', format,
|
|
983
|
+
preview: { ...preview, truncated: truncated || conversation.truncated === true, trace: saved?.trace },
|
|
984
|
+
includeTrace: includeTrace === true,
|
|
985
|
+
})
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
return {
|
|
989
|
+
dispose() {
|
|
990
|
+
disposed = true
|
|
991
|
+
wake()
|
|
992
|
+
monitor.dispose()
|
|
993
|
+
tickets.clear()
|
|
994
|
+
openReferences.clear()
|
|
995
|
+
conversationTickets.clear()
|
|
996
|
+
},
|
|
997
|
+
async execute(action, input) {
|
|
998
|
+
if (['project-relay', 'add-relay-item', 'update-relay-item', 'remove-relay-item', 'export-relay'].includes(action)) {
|
|
999
|
+
return projectRelay(action, input)
|
|
1000
|
+
}
|
|
1001
|
+
if (action === 'catalog') {
|
|
1002
|
+
const { projects, sources, scannedAt } = await scan(input.refresh === true)
|
|
1003
|
+
return { projects: projects.map(publicProject), sources, scannedAt, revision }
|
|
1004
|
+
}
|
|
1005
|
+
if (action === 'set-project-archive') return setProjectArchive(input)
|
|
1006
|
+
if (action === 'set-project-pin') return setProjectPin(input)
|
|
1007
|
+
if (action === 'set-project-selection') return setProjectSelection(input)
|
|
1008
|
+
if (action === 'record-project-continuation') return recordProjectContinuation(input.ticket)
|
|
1009
|
+
if (action === 'conversation-history' || action === 'discard-conversation-preview') {
|
|
1010
|
+
const config = await loadConfig()
|
|
1011
|
+
const id = input.snapshotId ?? input.ticket
|
|
1012
|
+
if (action === 'discard-conversation-preview') {
|
|
1013
|
+
await discardConversation(config, id)
|
|
1014
|
+
return {}
|
|
1015
|
+
}
|
|
1016
|
+
const page = await readHistoryPage(config, id, input.cursor, input.turn)
|
|
1017
|
+
if (!page) throw new RequestError('来源历史已过期,请重新打开预览。', 404)
|
|
1018
|
+
return page
|
|
1019
|
+
}
|
|
1020
|
+
if (action === 'preview-skill-activation') {
|
|
1021
|
+
if (!skillHandoff) throw new RequestError('当前 DSH 未提供原生技能启用服务。', 409)
|
|
1022
|
+
if (!isCatalogId(input.projectId) || !Array.isArray(input.assetIds)
|
|
1023
|
+
|| !input.assetIds.length || input.assetIds.length > 8 || !input.assetIds.every(isCatalogId)) {
|
|
1024
|
+
throw new RequestError('技能启用请求格式无效。')
|
|
1025
|
+
}
|
|
1026
|
+
const current = snapshot ?? await scan()
|
|
1027
|
+
const project = requireProject(current, input.projectId)
|
|
1028
|
+
const entry = await detail(project.id)
|
|
1029
|
+
const wanted = new Set(input.assetIds)
|
|
1030
|
+
const assets = entry.skills.filter(item => wanted.has(item.id))
|
|
1031
|
+
if (assets.length !== wanted.size) throw new RequestError('所选技能已变化,请刷新项目。', 409)
|
|
1032
|
+
return await skillHandoff.preview(project, assets)
|
|
1033
|
+
}
|
|
1034
|
+
if (action === 'apply-skill-activation') {
|
|
1035
|
+
if (!skillHandoff) throw new RequestError('当前 DSH 未提供原生技能启用服务。', 409)
|
|
1036
|
+
if (typeof input.ticket !== 'string' || input.ticket.length > 128) {
|
|
1037
|
+
throw new RequestError('技能预览凭证无效。')
|
|
1038
|
+
}
|
|
1039
|
+
const result = await skillHandoff.apply(input.ticket)
|
|
1040
|
+
details.clear()
|
|
1041
|
+
revision++
|
|
1042
|
+
wake()
|
|
1043
|
+
return { ...result, revision }
|
|
1044
|
+
}
|
|
1045
|
+
if (action === 'deactivate-skill-activation' || action === 'undo-skill-activation') {
|
|
1046
|
+
if (!skillHandoff) throw new RequestError('当前 DSH 未提供原生技能启用服务。', 409)
|
|
1047
|
+
if (!isCatalogId(input.projectId) || !isCatalogId(input.activationId)) {
|
|
1048
|
+
throw new RequestError('技能启用记录格式无效。')
|
|
1049
|
+
}
|
|
1050
|
+
const current = snapshot ?? await scan()
|
|
1051
|
+
const project = requireProject(current, input.projectId)
|
|
1052
|
+
const result = action === 'undo-skill-activation'
|
|
1053
|
+
? await skillHandoff.undo(project, input.activationId)
|
|
1054
|
+
: await skillHandoff.deactivate(project, input.activationId)
|
|
1055
|
+
details.clear()
|
|
1056
|
+
revision++
|
|
1057
|
+
wake()
|
|
1058
|
+
return { ...result, revision }
|
|
1059
|
+
}
|
|
1060
|
+
if (['list-conversations', 'search-conversations', 'conversation-import-status',
|
|
1061
|
+
'preview-conversation', 'preview-conversation-context', 'retarget-failed-conversation', 'import-conversation'].includes(action)) {
|
|
1062
|
+
if (!importConversation) throw new RequestError('当前 DSH 未提供会话迁入服务。', 409)
|
|
1063
|
+
if (action === 'list-conversations') return listConversations(input.projectId, input.recent === true, input.cursor)
|
|
1064
|
+
if (action === 'search-conversations') {
|
|
1065
|
+
const current = await scan()
|
|
1066
|
+
const project = requireProject(current, input.projectId)
|
|
1067
|
+
const result = await searchConversations(project, current.config, input)
|
|
1068
|
+
return { ...result, matches: await withImportStates(current.config, project, result.matches) }
|
|
1069
|
+
}
|
|
1070
|
+
if (action === 'conversation-import-status') {
|
|
1071
|
+
if (!isCatalogId(input.sessionId)) throw new RequestError('会话标识无效。')
|
|
1072
|
+
const current = await scan()
|
|
1073
|
+
const project = requireProject(current, input.projectId)
|
|
1074
|
+
return { lastImport: (await importStatus(current.config, project.id, sourceFor(project, input.sessionId)))?.lastImport ?? null }
|
|
1075
|
+
}
|
|
1076
|
+
if (action === 'preview-conversation') return previewConversation(input)
|
|
1077
|
+
if (action === 'retarget-failed-conversation') return previewConversation({
|
|
1078
|
+
projectId: input.projectId, sessionId: input.sessionId, snapshotId: input.ticket, targetCwd: input.targetCwd,
|
|
1079
|
+
}, true)
|
|
1080
|
+
if (action === 'preview-conversation-context') return previewContinuation(input)
|
|
1081
|
+
return commitConversation(input)
|
|
1082
|
+
}
|
|
1083
|
+
if (action === 'changes') {
|
|
1084
|
+
const inspect = async () => {
|
|
1085
|
+
const current = await scan()
|
|
1086
|
+
if (current.projects.some(item => item.id === input.projectId)) await detail(input.projectId)
|
|
1087
|
+
}
|
|
1088
|
+
await inspect()
|
|
1089
|
+
if (input.wait === true && input.revision === revision) {
|
|
1090
|
+
await waitForChange(input.revision)
|
|
1091
|
+
if (!disposed) await inspect()
|
|
1092
|
+
}
|
|
1093
|
+
return { revision, changed: input.revision !== revision, scannedAt: snapshot.scannedAt }
|
|
1094
|
+
}
|
|
1095
|
+
if (action === 'detail') {
|
|
1096
|
+
const {
|
|
1097
|
+
project, history, memories, skills, config, skillNativeAvailable, skillProjectRoot, managedSkills,
|
|
1098
|
+
} = await detail(input.projectId)
|
|
1099
|
+
let selection
|
|
1100
|
+
try { selection = await readProjectSelection(config, project.id) }
|
|
1101
|
+
catch (error) { throw storageFailure('读取', error, '项目资料选择') }
|
|
1102
|
+
return {
|
|
1103
|
+
project: publicProject(project), memories: memories.map(publicAsset), skills: skills.map(publicAsset),
|
|
1104
|
+
usedSkills: history.usedSkills, historyCount: history.messages.length, selection,
|
|
1105
|
+
skillNativeAvailable, skillProjectRoot, managedSkills,
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
if (action === 'asset') {
|
|
1109
|
+
const entry = await detail(input.projectId)
|
|
1110
|
+
const item = [...entry.memories, ...entry.skills].find(asset => asset.id === input.assetId)
|
|
1111
|
+
if (!item) throw new RequestError('找不到所选资料。', 404)
|
|
1112
|
+
const result = await readAsset(item).catch(() => { throw new RequestError('资料已移动或不可读。', 409) })
|
|
1113
|
+
const text = redact(result.text)
|
|
1114
|
+
const references = isMarkdownFile(item.file) ? await markdownReferences(entry, item, text) : []
|
|
1115
|
+
return { ...result, text, references }
|
|
1116
|
+
}
|
|
1117
|
+
if (action === 'preview') return prepare(input)
|
|
1118
|
+
if (action === 'resolve-open-path') return resolveOpenPath(input)
|
|
1119
|
+
if (action === 'export') {
|
|
1120
|
+
const ticket = getTicket(input.ticket)
|
|
1121
|
+
return makeExport({
|
|
1122
|
+
title: redact(ticket.title), origin: '项目交接', format: input.format,
|
|
1123
|
+
preview: await exportPreview(ticket), date: new Date(ticket.created),
|
|
1124
|
+
includeTrace: input.includeTrace === true,
|
|
1125
|
+
})
|
|
1126
|
+
}
|
|
1127
|
+
if (action === 'export-session') return exportSession(input)
|
|
1128
|
+
if (action === 'open') {
|
|
1129
|
+
const ticket = getTicket(input.ticket)
|
|
1130
|
+
if (!ticket.path) throw new RequestError('原工作目录不可访问。可导出交接。', 409)
|
|
1131
|
+
const current = await currentDirectory(ticket.path)
|
|
1132
|
+
if (!current || pathKey(current) !== pathKey(ticket.path)) throw new RequestError('原工作目录已变化。', 409)
|
|
1133
|
+
ticket.opened = true
|
|
1134
|
+
return { path: current, prompt: ticket.prompt }
|
|
1135
|
+
}
|
|
1136
|
+
throw new RequestError('找不到该操作。', 404)
|
|
1137
|
+
},
|
|
1138
|
+
}
|
|
1139
|
+
}
|