dsh-cortex-memory 0.4.161 → 0.5.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/CHANGELOG.md +18 -2536
- package/README.md +86 -45
- package/bin/cortex.mjs +0 -0
- package/client.js +35 -17
- package/cordis.patch.yml +2 -5
- package/docs/compatibility.md +13 -18
- package/docs/install-windows.md +1 -1
- package/examples/README.md +1 -1
- package/index.js +44 -6
- package/package.json +3 -3
- package/scripts/check-embedding.mjs +129 -0
- package/src/core/config.js +31 -4
- package/src/core/feature-overrides.js +3 -1
- package/src/engines/compression.js +2 -0
- package/src/engines/consolidate.js +7 -2
- package/src/engines/graph.js +25 -8
- package/src/engines/health.js +22 -3
- package/src/engines/induct.js +5 -1
- package/src/engines/linear-rerank-model.js +180 -0
- package/src/engines/linear-rerank.js +110 -0
- package/src/engines/query-expansion.js +61 -0
- package/src/engines/retention.js +4 -1
- package/src/engines/retrieve.js +257 -28
- package/src/engines/source-weights.js +13 -0
- package/src/host/scheduler.js +43 -0
- package/src/memory/tokenizer.js +31 -1
- package/src/prompt/assemble.js +160 -131
- package/src/prompt/tiers.js +15 -4
- package/src/self/working.js +53 -10
- package/src/service/cortex.js +167 -35
- package/src/store/database.js +61 -0
- package/src/store/repositories/entities.js +26 -1
- package/src/store/repositories/facts.js +36 -1
- package/src/store/repositories/traces.js +114 -15
- package/src/store/schema.sql +13 -0
- package/src/tools/definitions.js +1492 -1274
- package/src/vector/index.js +79 -8
- package/bench/results/semantic-cn.json +0 -24
- package/docs/quota-compaction-design.md +0 -175
package/src/tools/definitions.js
CHANGED
|
@@ -1,1274 +1,1492 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { assembleSystemPrompt } from '../prompt/assemble.js';
|
|
3
|
-
import { estimateTokens } from '../memory/tokenizer.js';
|
|
4
|
-
|
|
5
|
-
function guardPathAllowed(service, target) {
|
|
6
|
-
if (service?.config?.allowRestoreOutsideRoot === true) return true;
|
|
7
|
-
const root = path.resolve(String(service?.config?.dataRoot ?? process.cwd()));
|
|
8
|
-
const absolute = path.resolve(String(target ?? ''));
|
|
9
|
-
return absolute === root || absolute.startsWith(`${root}${path.sep}`);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const TEXT_OUTPUT = Object.freeze({
|
|
13
|
-
schema: {
|
|
14
|
-
type: 'object',
|
|
15
|
-
additionalProperties: true,
|
|
16
|
-
properties: { text: { type: 'string' } },
|
|
17
|
-
required: ['text'],
|
|
18
|
-
},
|
|
19
|
-
render: (_args, value) => [{ type: 'text', text: value?.text ?? '' }],
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
function line(label, value) {
|
|
23
|
-
return `${label}:${value}`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function formatSearch(result) {
|
|
27
|
-
const lines = [
|
|
28
|
-
`查询:${result.query || '(空)'} · 意图:${result.intent}`,
|
|
29
|
-
`预算:${result.budget.used}/${result.budget.total} token · 候选 ${result.trace.candidates} · 选中 ${result.items.length}`,
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
lines.push(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (report.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
if (report.
|
|
61
|
-
lines.push('—
|
|
62
|
-
for (const row of report.
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
lines.push(
|
|
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
|
-
if (
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (report.
|
|
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
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
};
|
|
200
|
-
} catch {
|
|
201
|
-
/*
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
''
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
lines
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
},
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
return { text:
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
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
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
const
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
const
|
|
752
|
-
return { text:
|
|
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
|
-
if (
|
|
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
|
-
const result =
|
|
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
|
-
|
|
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
|
-
const
|
|
978
|
-
return {
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
},
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
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
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
if (
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { assembleSystemPrompt } from '../prompt/assemble.js';
|
|
3
|
+
import { estimateTokens } from '../memory/tokenizer.js';
|
|
4
|
+
|
|
5
|
+
function guardPathAllowed(service, target) {
|
|
6
|
+
if (service?.config?.allowRestoreOutsideRoot === true) return true;
|
|
7
|
+
const root = path.resolve(String(service?.config?.dataRoot ?? process.cwd()));
|
|
8
|
+
const absolute = path.resolve(String(target ?? ''));
|
|
9
|
+
return absolute === root || absolute.startsWith(`${root}${path.sep}`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const TEXT_OUTPUT = Object.freeze({
|
|
13
|
+
schema: {
|
|
14
|
+
type: 'object',
|
|
15
|
+
additionalProperties: true,
|
|
16
|
+
properties: { text: { type: 'string' } },
|
|
17
|
+
required: ['text'],
|
|
18
|
+
},
|
|
19
|
+
render: (_args, value) => [{ type: 'text', text: value?.text ?? '' }],
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
function line(label, value) {
|
|
23
|
+
return `${label}:${value}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function formatSearch(result) {
|
|
27
|
+
const lines = [
|
|
28
|
+
`查询:${result.query || '(空)'} · 意图:${result.intent}`,
|
|
29
|
+
`预算:${result.budget.used}/${result.budget.total} token · 候选 ${result.trace.candidates} · 选中 ${result.items.length}`,
|
|
30
|
+
];
|
|
31
|
+
const linear = result.trace?.linearRerank;
|
|
32
|
+
if (linear) {
|
|
33
|
+
lines.push(
|
|
34
|
+
linear.ok
|
|
35
|
+
? `深排:on · ${linear.candidates}/${linear.poolTarget ?? '?'} 候选 → ${linear.selected}`
|
|
36
|
+
: `深排:skipped(${linear.reason ?? 'unknown'})${linear.candidates != null ? ` · ${linear.candidates} 候选` : ''}`,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const rerank = result.trace?.rerank;
|
|
40
|
+
if (rerank) {
|
|
41
|
+
lines.push(rerank.ok ? `HTTP 深排:on · ${rerank.candidates} → ${rerank.selected}` : `HTTP 深排:failed(${rerank.reason ?? rerank.error ?? 'unknown'})`);
|
|
42
|
+
}
|
|
43
|
+
for (const item of result.items) {
|
|
44
|
+
lines.push(`- [${item.layer}] ${item.id} · ${item.title} · score ${item.score}`);
|
|
45
|
+
lines.push(` ${item.snippet}`);
|
|
46
|
+
}
|
|
47
|
+
return lines.join('\n');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function formatAudit(report) {
|
|
51
|
+
const lines = [
|
|
52
|
+
`只读审计 · ${String(report.at).slice(0, 19)} · 范围 ${report.scopeId}`,
|
|
53
|
+
`过期未收 ${report.expired.total} · 同题多值 ${report.contradiction.total} · 争议未决 ${report.disputed.total}`,
|
|
54
|
+
`僵尸 ${report.zombie.total} · 归档候选 ${report.archiveCandidate.total}/${report.archiveCandidate.scanned}(阈值 ${report.archiveCandidate.threshold})· 孤儿痕迹 ${report.integrity.orphanTraces}`,
|
|
55
|
+
];
|
|
56
|
+
if (report.stability?.length) lines.push(`稳定性:${report.stability.map((bucket) => `${bucket.bucket} ${bucket.total}`).join(' · ')}`);
|
|
57
|
+
if (report.authorship?.buckets?.length) {
|
|
58
|
+
lines.push(`作者:${report.authorship.buckets.map((bucket) => `${bucket.kind} ${bucket.total}`).join(' · ')}${report.authorship.pendingSelf ? ` · 待确认 ${report.authorship.pendingSelf}` : ''}`);
|
|
59
|
+
}
|
|
60
|
+
if (report.expired.items.length) {
|
|
61
|
+
lines.push('— 过期却仍是 active —');
|
|
62
|
+
for (const row of report.expired.items.slice(0, 5)) lines.push(`- ${row.id} · ${row.predicate} → ${row.object_literal ?? row.object_id ?? '—'} · 止于 ${String(row.valid_to).slice(0, 10)}`);
|
|
63
|
+
}
|
|
64
|
+
if (report.contradiction.items.length) {
|
|
65
|
+
lines.push('— 同题多值 —');
|
|
66
|
+
for (const row of report.contradiction.items.slice(0, 5)) lines.push(`- ${row.subject_id} · ${row.predicate} ×${row.total}`);
|
|
67
|
+
}
|
|
68
|
+
if (report.zombie.items.length) {
|
|
69
|
+
lines.push('— 僵尸痕迹(从没被召回、够老、重要度低)—');
|
|
70
|
+
for (const row of report.zombie.items.slice(0, 5)) lines.push(`- ${row.id} · ${row.kind} · ${String(row.title ?? '').slice(0, 24)} · ${String(row.recorded_at).slice(0, 10)}`);
|
|
71
|
+
}
|
|
72
|
+
if (report.archiveCandidate.items.length) {
|
|
73
|
+
lines.push('— 归档候选(DSR 已低于阈值)—');
|
|
74
|
+
for (const row of report.archiveCandidate.items.slice(0, 5)) lines.push(`- ${row.id} · r=${row.retrievability} · 闲置 ${row.idleDays} 天 · ${row.kind}`);
|
|
75
|
+
}
|
|
76
|
+
lines.push('本命令只报不改;要处理请逐条用 cortex_forget / cortex_feedback / cortex_compress。');
|
|
77
|
+
return lines.join('\n');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function formatIntention(report) {
|
|
81
|
+
const lines = [];
|
|
82
|
+
const open = report.open ?? [];
|
|
83
|
+
const recent = report.recent ?? [];
|
|
84
|
+
lines.push(`待兑现 ${open.length} 条 · 最近记录 ${recent.length} 条 · 偏差 ${(report.deviations ?? []).length} 条`);
|
|
85
|
+
if (open.length) {
|
|
86
|
+
lines.push('— 我说过要做、还没对过的 —');
|
|
87
|
+
for (const item of open.slice(0, 8)) lines.push(`- ${item.id} · ${item.claim ?? '—'}${item.dueAt ? ` · 应收尾 ${String(item.dueAt).slice(0, 10)}` : ''}`);
|
|
88
|
+
}
|
|
89
|
+
const wrong = recent.filter((item) => item.deviation === true);
|
|
90
|
+
if (wrong.length) {
|
|
91
|
+
lines.push('— 声称与实际不符(有证据)—');
|
|
92
|
+
for (const item of wrong.slice(0, 8)) {
|
|
93
|
+
lines.push(`- ${item.id} · 声称「${item.claim ?? '—'}」→ 实际「${item.observed ?? '—'}」`);
|
|
94
|
+
if (item.evidence) lines.push(` 证据:${String(item.evidence).slice(0, 120)}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const done = recent.filter((item) => item.deviation === false);
|
|
98
|
+
if (done.length) lines.push(`— 已对上的 ${done.length} 条 —`);
|
|
99
|
+
if (!open.length && !recent.length) lines.push('还没有记过「声称 vs 实际」。');
|
|
100
|
+
return lines.join('\n');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function formatSelf(snapshot) {
|
|
104
|
+
const { self, identity, relationships, intentions, calibration } = snapshot;
|
|
105
|
+
const drives = Object.entries(self.drives).map(([name, value]) => `${name} ${Math.round(value * 100)}`).join(' / ');
|
|
106
|
+
const lines = [
|
|
107
|
+
line('阶段', self.stage),
|
|
108
|
+
line('驱力', drives),
|
|
109
|
+
line('情绪', `valence ${self.mood.valence.toFixed(2)} / arousal ${self.mood.arousal.toFixed(2)} / energy ${self.mood.energy.toFixed(2)}`),
|
|
110
|
+
line('身份', identity ? `v${identity.version} ${identity.content.narrative}` : '(未初始化)'),
|
|
111
|
+
];
|
|
112
|
+
if (relationships.length) lines.push(`关系:${relationships.map((item) => `${item.target} 信任 ${Math.round(item.trust * 100)}% / 在乎 ${Math.round(item.care * 100)}%`).join(';')}`);
|
|
113
|
+
if (intentions.length) lines.push(`主动意图:${intentions.map((item) => `${item.kind}(${item.priority.toFixed(2)})`).join('、')}`);
|
|
114
|
+
if (calibration?.n) lines.push(`校准:n=${calibration.n} brier=${calibration.brier?.toFixed(3)} ece=${calibration.ece?.toFixed(3)}`);
|
|
115
|
+
return lines.join('\n');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function formatHealth(report) {
|
|
119
|
+
const lines = [`健康分:${report.score}/100`, `事件 ${report.counts.events} · 痕迹 ${report.counts.traces.total}(active ${report.counts.traces.active})· 事实 ${report.counts.facts.active} · 技能 ${report.counts.skills}`];
|
|
120
|
+
if (report.inhibition?.enabled) lines.push(`抑制:${report.inhibition.count} 条 · 均值 ${report.inhibition.average}`);
|
|
121
|
+
if (report.capture?.total) lines.push(`捕获:${report.capture.total} 轮 · 剥掉 ${report.capture.stripped} · 截断 ${report.capture.truncated}`);
|
|
122
|
+
if (report.capture?.days?.length) lines.push(`按天:${report.capture.days.slice(0, 3).map((day) => `${day.day} ${day.total}轮/剥${day.stripped}`).join(' · ')}`);
|
|
123
|
+
if (report.vectors) lines.push(`向量:${Math.round(report.vectors.coverage * 100)}%(${report.vectors.embedded}/${report.counts.traces.active})`);
|
|
124
|
+
if (report.consolidation?.dream || report.consolidation?.sleep) {
|
|
125
|
+
const dream = report.consolidation.dream;
|
|
126
|
+
const sleep = report.consolidation.sleep;
|
|
127
|
+
const stamp = (value) => (typeof value === 'string' ? value.slice(0, 19) : '—');
|
|
128
|
+
const dreamText = dream
|
|
129
|
+
? `巩固 ${stamp(dream.at)} ${dream.error
|
|
130
|
+
? `空转(${dream.error})`
|
|
131
|
+
: `${dream.status ?? 'done'},决策 ${dream.decisions}(keep ${dream.keep ?? 0})· 应用 ${dream.applied}`}`
|
|
132
|
+
: '巩固 还没跑过';
|
|
133
|
+
const sleepText = sleep
|
|
134
|
+
? `睡眠 ${stamp(sleep.at)}(${sleep.consolidation ?? '—'},反思 ${sleep.reflected})`
|
|
135
|
+
: '睡眠 还没有记录';
|
|
136
|
+
lines.push(`${dreamText} · ${sleepText}`);
|
|
137
|
+
const today = report.consolidation?.today;
|
|
138
|
+
if (today && (today.archived || today.merged)) {
|
|
139
|
+
lines.push(`今日整理:${today.runs} 趟 · 归档 ${today.archived} 条 · 合并 ${today.merged} 条`);
|
|
140
|
+
} else {
|
|
141
|
+
lines.push('今日整理:暂无改动');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (report.summaryLayers) {
|
|
145
|
+
const layers = report.summaryLayers;
|
|
146
|
+
const last = layers.lastEpisodeAt ? layers.lastEpisodeAt.slice(0, 16) : '—';
|
|
147
|
+
lines.push(`摘要:每日 ${layers.episode} 条(最近 ${last})· 合并记录 ${layers.merge} · 线 ${layers.thread}`);
|
|
148
|
+
}
|
|
149
|
+
if (report.maintain) {
|
|
150
|
+
const maintain = report.maintain;
|
|
151
|
+
const ms = Number.isFinite(maintain.ms) ? ` · ${maintain.ms}ms` : '';
|
|
152
|
+
lines.push(`维护:最近一趟 ${String(maintain.at).slice(0, 19)}(意图 ${maintain.intentions} · 工作记忆 ${maintain.working}${ms})`);
|
|
153
|
+
// A quiet pass leaves the newest audit row stale; name it separately so the
|
|
154
|
+
// timestamp above is not read as "nothing has run in hours".
|
|
155
|
+
const changed = report.maintainLastChanged;
|
|
156
|
+
const changedAt = changed?.passAt ?? changed?.at ?? null;
|
|
157
|
+
if (changedAt && changedAt !== maintain.at) {
|
|
158
|
+
lines.push(` 最近一次有变更:${String(changedAt).slice(0, 19)}(意图 ${changed.intentions} · 工作记忆 ${changed.working})`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (report.issues.length) {
|
|
162
|
+
lines.push('问题:');
|
|
163
|
+
for (const issue of report.issues) lines.push(`- [${issue.severity}] ${issue.message}`);
|
|
164
|
+
} else {
|
|
165
|
+
lines.push('没有发现问题。');
|
|
166
|
+
}
|
|
167
|
+
return lines.join('\n');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function formatWindow(service) {
|
|
171
|
+
const budget = service.config?.promptBudget ?? 1600;
|
|
172
|
+
const scopeId = service.config?.scope ?? 'global';
|
|
173
|
+
const assembled = assembleSystemPrompt(service, { budgetTokens: budget });
|
|
174
|
+
|
|
175
|
+
let workingAll = [];
|
|
176
|
+
try {
|
|
177
|
+
workingAll = service.working?.list?.({ scopeId, limit: 1000 }) ?? [];
|
|
178
|
+
} catch {
|
|
179
|
+
/* 读不到就按 0 */
|
|
180
|
+
}
|
|
181
|
+
const pinned = workingAll.filter((item) => item.pinned);
|
|
182
|
+
|
|
183
|
+
let pool = { count: 0, omitted: 0 };
|
|
184
|
+
try {
|
|
185
|
+
// 2026-11-29:别硬编码 260 —— 必须和注入用同一预算,否则仪表会报「45→2」而实际注入了 3 条。
|
|
186
|
+
pool = service.playbook?.render?.({ budget: service.config?.playbookBudget ?? 420 }) ?? pool;
|
|
187
|
+
} catch {
|
|
188
|
+
/* 渲染失败就报 0 */
|
|
189
|
+
}
|
|
190
|
+
let activeCount = Math.max(0, Number(pool.count) + Number(pool.omitted));
|
|
191
|
+
let ballots = { voted: 0, zero: 0 };
|
|
192
|
+
try {
|
|
193
|
+
const all = service.playbook?.list?.({ scopeId, includeExpired: false, limit: 1000 }) ?? [];
|
|
194
|
+
const activeRules = all.filter((rule) => rule.status === 'active');
|
|
195
|
+
activeCount = activeRules.length;
|
|
196
|
+
ballots = {
|
|
197
|
+
voted: activeRules.filter((r) => (r.helpful ?? 0) > 0 || (r.harmful ?? 0) > 0).length,
|
|
198
|
+
zero: activeRules.filter((r) => !(r.helpful ?? 0) && !(r.harmful ?? 0)).length,
|
|
199
|
+
};
|
|
200
|
+
} catch {
|
|
201
|
+
/* 退回按渲染池估 */
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let counts = { traces: 0, facts: 0, events: 0 };
|
|
205
|
+
try {
|
|
206
|
+
const h = service.health();
|
|
207
|
+
counts = {
|
|
208
|
+
traces: h?.counts?.traces?.total ?? 0,
|
|
209
|
+
facts: h?.counts?.facts?.active ?? 0,
|
|
210
|
+
events: h?.counts?.events ?? 0,
|
|
211
|
+
};
|
|
212
|
+
} catch {
|
|
213
|
+
/* health 读不到就只报窗口内的数 */
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const shown = (assembled.sections ?? []).map((s) => {
|
|
217
|
+
const mark = s.kept ? '✓' : s.partial ? '△' : '✗';
|
|
218
|
+
return ` ${mark} ${s.title || '(无题)'} ${s.tokens} token${s.partial ? '(截断)' : ''}`;
|
|
219
|
+
});
|
|
220
|
+
const lostSections = (assembled.sections ?? []).filter((s) => !s.kept && !s.partial && s.title);
|
|
221
|
+
|
|
222
|
+
const lines = [
|
|
223
|
+
`【窗口】系统提示这一块:${assembled.used}/${budget} token(${assembled.truncated ? '有截断' : '完整'},不含召回块)${assembled.overspent ? ' ⚠️ 超预算:尾注保底段本身超过总量,无法再砍' : ''}`,
|
|
224
|
+
'',
|
|
225
|
+
`① 库多大:痕迹 ${counts.traces} · 事实 ${counts.facts} · 事件 ${counts.events} · 准则 ${activeCount} · 工作记忆 ${workingAll.length}(pin ${pinned.length})`,
|
|
226
|
+
'',
|
|
227
|
+
'② 注入了什么:',
|
|
228
|
+
...shown,
|
|
229
|
+
'',
|
|
230
|
+
`③ 丢了什么:准则 ${activeCount} → ${Math.max(0, Number(pool.count))}(省 ${Math.max(0, Number(pool.omitted))} 条)· 工作记忆 ${workingAll.length} → 候选 ${service.self().working?.length ?? 0}(其余被 limit 20 砍掉,再被预算截)`,
|
|
231
|
+
];
|
|
232
|
+
if (lostSections.length) lines.push(` 整段没进来:${lostSections.map((s) => s.title).join(' / ')}`);
|
|
233
|
+
const firstRule = String(pool.text ?? '').split('\n').find((l) => l.trim()) ?? '';
|
|
234
|
+
lines.push(
|
|
235
|
+
` ⏵ 当前排第一:${firstRule.replace(/^-\s*/, '').slice(0, 70) || '(无)'}`,
|
|
236
|
+
);
|
|
237
|
+
lines.push(
|
|
238
|
+
` 准则票数:有票 ${ballots.voted} 条 / 全 0 ${ballots.zero} 条(排序先看 helpful-harmful,全 0 就退化成按创建时间)`,
|
|
239
|
+
);
|
|
240
|
+
if (pinned.length) {
|
|
241
|
+
lines.push('');
|
|
242
|
+
lines.push('④ pin 全在这里(自己看有没有打架的):');
|
|
243
|
+
for (const item of pinned) lines.push(` - ${String(item.text ?? '').slice(0, 90)}`);
|
|
244
|
+
}
|
|
245
|
+
return lines.join('\n');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function createToolDefinitions(service) {
|
|
249
|
+
if (!service) throw new Error('tool definitions need a cortex service');
|
|
250
|
+
return [
|
|
251
|
+
{
|
|
252
|
+
name: 'cortex_tick',
|
|
253
|
+
description: 'Run one autonomy tick: execute safe local intentions (consolidate/recall) and return the rest as proposals that need host/approval. Never sends messages.',
|
|
254
|
+
parameters: {
|
|
255
|
+
type: 'object',
|
|
256
|
+
additionalProperties: false,
|
|
257
|
+
required: ['action'],
|
|
258
|
+
properties: {
|
|
259
|
+
action: { type: 'string', enum: ['run', 'status'] },
|
|
260
|
+
max: { type: 'number' },
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
output: TEXT_OUTPUT,
|
|
264
|
+
isConcurrencySafe: () => false,
|
|
265
|
+
async execute(args = {}) {
|
|
266
|
+
if (args.action === 'status') {
|
|
267
|
+
const status = service.autonomy.status();
|
|
268
|
+
return { text: `自主意图:${status.pending} 条 ${Object.entries(status.byKind).map(([k, v]) => `${k}:${v}`).join(' ')}` };
|
|
269
|
+
}
|
|
270
|
+
const result = service.autonomy.tick({ max: args.max ?? 2 });
|
|
271
|
+
return { text: `执行 ${result.executed.length} 条,提案 ${result.proposals.length} 条。${result.proposals.map((item) => `\n- ${item.kind}:${item.reason}(需要 ${item.requires})`).join('')}` };
|
|
272
|
+
},
|
|
273
|
+
presentCall: () => ({ card: 'generic', title: '自主 tick', kind: 'other' }),
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
name: 'cortex_inhibition',
|
|
277
|
+
description: 'Inspect or clear the retrieval-induced-forgetting marks. list shows suppressed traces with their decayed strength; clear removes the mark from one trace (id) or all of them (all: true).',
|
|
278
|
+
parameters: {
|
|
279
|
+
type: 'object',
|
|
280
|
+
additionalProperties: false,
|
|
281
|
+
required: ['action'],
|
|
282
|
+
properties: {
|
|
283
|
+
action: { type: 'string', enum: ['list', 'clear'] },
|
|
284
|
+
id: { type: 'string', description: '要清除的记忆 id(clear 时用)' },
|
|
285
|
+
all: { type: 'boolean', description: 'clear 时清全部' },
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
output: TEXT_OUTPUT,
|
|
289
|
+
isConcurrencySafe: () => false,
|
|
290
|
+
async execute(args = {}) {
|
|
291
|
+
if (args.action === 'list') {
|
|
292
|
+
const items = service.inhibition.list({});
|
|
293
|
+
if (!items.length) return { text: '没有被抑制的记忆。' };
|
|
294
|
+
return { text: items.slice(0, 20).map((item) => `- ${item.id} · ${item.title ?? '(untitled)'} · 强度 ${item.value}`).join('\n') };
|
|
295
|
+
}
|
|
296
|
+
if (!args.id && args.all !== true) return { text: 'clear 需要 id,或者 all: true。' };
|
|
297
|
+
const result = service.inhibition.clear({ id: args.id ?? null, all: args.all === true });
|
|
298
|
+
return { text: `已清除 ${result.cleared} 条抑制标记。` };
|
|
299
|
+
},
|
|
300
|
+
presentCall: () => ({ card: 'generic', title: '记忆抑制', kind: 'other' }),
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
name: 'cortex_digest',
|
|
304
|
+
description: 'Generate a daily memory digest (counts, important traces, emotions, feedback, goals) and persist it as a digest summary.',
|
|
305
|
+
parameters: {
|
|
306
|
+
type: 'object',
|
|
307
|
+
additionalProperties: false,
|
|
308
|
+
required: ['action'],
|
|
309
|
+
properties: {
|
|
310
|
+
action: { type: 'string', enum: ['daily'] },
|
|
311
|
+
date: { type: 'string', description: 'YYYY-MM-DD; defaults to today.' },
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
output: TEXT_OUTPUT,
|
|
315
|
+
isConcurrencySafe: () => false,
|
|
316
|
+
async execute(args = {}) {
|
|
317
|
+
const result = service.digest.daily({ date: args.date });
|
|
318
|
+
return { text: result.content };
|
|
319
|
+
},
|
|
320
|
+
presentCall: () => ({ card: 'generic', title: '每日摘要', kind: 'other' }),
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
name: 'cortex_working',
|
|
324
|
+
description: 'Manage working memory (focus, pins, TTL): add / list / pin / lane / remove / purge. Working memory is the small L1 layer injected into the next prompt, split by lane: self (identity/feelings/relationships), work (engineering todos), life (daily life).',
|
|
325
|
+
parameters: {
|
|
326
|
+
type: 'object',
|
|
327
|
+
additionalProperties: false,
|
|
328
|
+
required: ['action'],
|
|
329
|
+
properties: {
|
|
330
|
+
action: { type: 'string', enum: ['add', 'list', 'pin', 'lane', 'remove', 'purge'] },
|
|
331
|
+
id: { type: 'string' },
|
|
332
|
+
text: { type: 'string' },
|
|
333
|
+
kind: { type: 'string' },
|
|
334
|
+
priority: { type: 'number' },
|
|
335
|
+
pinned: { type: 'boolean' },
|
|
336
|
+
ttlMs: { type: 'number' },
|
|
337
|
+
lane: { type: 'string', enum: ['self', 'work', 'life'], description: '分道:self=身份/情感/关系,work=工程待办,life=生活日常。add 时不传按 kind 推断;lane 动作用来改道' },
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
output: TEXT_OUTPUT,
|
|
341
|
+
isConcurrencySafe: () => false,
|
|
342
|
+
async execute(args = {}) {
|
|
343
|
+
if (args.action === 'add') {
|
|
344
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
345
|
+
const item = service.working.add({ text: args.text, kind: args.kind, lane: args.lane, priority: args.priority, pinned: args.pinned === true, ttlMs: args.ttlMs });
|
|
346
|
+
return { text: `已加入工作记忆 ${item.id}(${item.lane}):${item.text}` };
|
|
347
|
+
}
|
|
348
|
+
if (args.action === 'lane') {
|
|
349
|
+
if (!args.id) return { text: '未执行:要指定 id。' };
|
|
350
|
+
if (!args.lane) return { text: '未执行:要指定 lane(self / work / life)。' };
|
|
351
|
+
if (args.id && !service.scopeWritableById('working', args.id)) return { text: '未执行:该工作项属于只读共享 scope。' };
|
|
352
|
+
const item = service.working.update(args.id, { lane: args.lane });
|
|
353
|
+
return { text: item ? `已把 ${item.id} 改到 ${item.lane} 道` : `没有 ${args.id}` };
|
|
354
|
+
}
|
|
355
|
+
if (args.action === 'pin') {
|
|
356
|
+
if (args.id && !service.scopeWritableById('working', args.id)) return { text: '未执行:该工作项属于只读共享 scope。' };
|
|
357
|
+
const item = service.working.update(args.id, { pinned: args.pinned !== false });
|
|
358
|
+
return { text: item ? `已${item.pinned ? '置顶' : '取消置顶'} ${item.id}` : `没有 ${args.id}` };
|
|
359
|
+
}
|
|
360
|
+
if (args.action === 'remove') {
|
|
361
|
+
if (args.id && !service.scopeWritableById('working', args.id)) return { text: '未执行:该工作项属于只读共享 scope。' };
|
|
362
|
+
return { text: service.working.remove(args.id) ? `已删除 ${args.id}` : `没有 ${args.id}` };
|
|
363
|
+
}
|
|
364
|
+
if (args.action === 'purge') {
|
|
365
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
366
|
+
return { text: `已清理 ${service.working.purgeExpired()} 条过期工作记忆` };
|
|
367
|
+
}
|
|
368
|
+
const items = service.working.list({ lane: args.lane });
|
|
369
|
+
return {
|
|
370
|
+
text: items.length
|
|
371
|
+
? items.map((item) => `- ${item.pinned ? '[pin] ' : ''}${item.id} [${item.lane}] (${item.priority}) ${item.text}`).join('\n')
|
|
372
|
+
: (args.lane ? `(${args.lane} 道为空)` : '(工作记忆为空)'),
|
|
373
|
+
};
|
|
374
|
+
},
|
|
375
|
+
presentCall: () => ({ card: 'generic', title: '工作记忆', kind: 'other' }),
|
|
376
|
+
},
|
|
377
|
+
{
|
|
378
|
+
name: 'cortex_goal',
|
|
379
|
+
description: 'Manage long-running goals (L1): add / list / update / complete. Goals feed the working-memory and self-state layers.',
|
|
380
|
+
parameters: {
|
|
381
|
+
type: 'object',
|
|
382
|
+
additionalProperties: false,
|
|
383
|
+
required: ['action'],
|
|
384
|
+
properties: {
|
|
385
|
+
action: { type: 'string', enum: ['add', 'list', 'update', 'complete'] },
|
|
386
|
+
id: { type: 'string' },
|
|
387
|
+
title: { type: 'string' },
|
|
388
|
+
description: { type: 'string' },
|
|
389
|
+
priority: { type: 'number' },
|
|
390
|
+
progress: { type: 'number' },
|
|
391
|
+
status: { type: 'string' },
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
output: TEXT_OUTPUT,
|
|
395
|
+
isConcurrencySafe: () => false,
|
|
396
|
+
async execute(args = {}) {
|
|
397
|
+
if (args.action === 'add') {
|
|
398
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
399
|
+
const goal = service.goals.add({ title: args.title, description: args.description, priority: args.priority });
|
|
400
|
+
return { text: `目标已建 ${goal.id}:${goal.title}` };
|
|
401
|
+
}
|
|
402
|
+
if (args.action === 'complete') {
|
|
403
|
+
if (args.id && !service.scopeWritableById('goal', args.id)) return { text: '未执行:该目标属于只读共享 scope。' };
|
|
404
|
+
const done = service.goals.complete(args.id);
|
|
405
|
+
return { text: done ? `目标已完成:${done.title}` : `没有这个目标:${args.id}` };
|
|
406
|
+
}
|
|
407
|
+
if (args.action === 'update') {
|
|
408
|
+
if (args.id && !service.scopeWritableById('goal', args.id)) return { text: '未执行:该目标属于只读共享 scope。' };
|
|
409
|
+
const goal = service.goals.update(args.id, { title: args.title, description: args.description, priority: args.priority, progress: args.progress, status: args.status });
|
|
410
|
+
return { text: goal ? `已更新 ${goal.id}:${goal.status} ${Math.round(goal.progress * 100)}%` : `没有 ${args.id}` };
|
|
411
|
+
}
|
|
412
|
+
const list = service.goals.list({ status: args.status });
|
|
413
|
+
return { text: list.length ? list.map((goal) => `- [${goal.status}] ${goal.id} ${goal.title} ${Math.round(goal.progress * 100)}%`).join('\n') : '(没有目标)' };
|
|
414
|
+
},
|
|
415
|
+
presentCall: () => ({ card: 'generic', title: '目标', kind: 'other' }),
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
name: 'cortex_compress',
|
|
419
|
+
description: 'Evaluate or apply compression: archive old, low-importance traces that are already covered by summaries/facts; revive one by id.',
|
|
420
|
+
parameters: {
|
|
421
|
+
type: 'object',
|
|
422
|
+
additionalProperties: false,
|
|
423
|
+
required: ['action'],
|
|
424
|
+
properties: {
|
|
425
|
+
action: { type: 'string', enum: ['evaluate', 'compress', 'revive'] },
|
|
426
|
+
id: { type: 'string' },
|
|
427
|
+
limit: { type: 'number' },
|
|
428
|
+
},
|
|
429
|
+
},
|
|
430
|
+
output: TEXT_OUTPUT,
|
|
431
|
+
isConcurrencySafe: () => false,
|
|
432
|
+
async execute(args = {}) {
|
|
433
|
+
if (args.action === 'revive') {
|
|
434
|
+
const result = service.compression.revive(args.id);
|
|
435
|
+
return { text: result.revived ? `已复活 ${args.id}(下一次检索可命中)` : `没有这条记忆:${args.id}` };
|
|
436
|
+
}
|
|
437
|
+
if (args.action === 'compress') {
|
|
438
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
439
|
+
const result = service.compression.compress({ limit: args.limit });
|
|
440
|
+
return { text: `压缩归档 ${result.archived} 条。${result.items.map((item) => `\n- ${item.id} coverage ${item.coverage.toFixed(2)}`).join('')}` };
|
|
441
|
+
}
|
|
442
|
+
const rows = service.compression.evaluate();
|
|
443
|
+
const eligible = rows.filter((row) => row.eligible);
|
|
444
|
+
return { text: `可压缩 ${eligible.length} 条 / 活跃 ${rows.length} 条。${eligible.slice(0, 10).map((row) => `\n- ${row.trace.id} coverage ${row.coverage.toFixed(2)} R ${row.retrievability.toFixed(2)}`).join('')}` };
|
|
445
|
+
},
|
|
446
|
+
presentCall: () => ({ card: 'generic', title: '记忆压缩', kind: 'other' }),
|
|
447
|
+
},
|
|
448
|
+
{
|
|
449
|
+
name: 'cortex_life',
|
|
450
|
+
description: 'Life-side memory: diary entries, dream generation from recent memories, and a sleep/consolidation run (purge working, reflect, recompute development and affect).',
|
|
451
|
+
parameters: {
|
|
452
|
+
type: 'object',
|
|
453
|
+
additionalProperties: false,
|
|
454
|
+
required: ['action'],
|
|
455
|
+
properties: {
|
|
456
|
+
action: { type: 'string', enum: ['diary-generate', 'diary-write', 'diary-list', 'diary-forget', 'dream', 'sleep', 'sleep-runs'] },
|
|
457
|
+
text: { type: 'string' },
|
|
458
|
+
mood: { type: 'string' },
|
|
459
|
+
id: { type: 'string' },
|
|
460
|
+
windowDays: { type: 'number' },
|
|
461
|
+
limit: { type: 'number' },
|
|
462
|
+
},
|
|
463
|
+
},
|
|
464
|
+
output: TEXT_OUTPUT,
|
|
465
|
+
isConcurrencySafe: () => false,
|
|
466
|
+
async execute(args = {}) {
|
|
467
|
+
if (args.action === 'diary-generate') {
|
|
468
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
469
|
+
const item = await service.life.diaryGenerate();
|
|
470
|
+
return { text: `日记 ${item.id}(${item.model},来源 ${item.sourceCount} 条):\n${item.text}` };
|
|
471
|
+
}
|
|
472
|
+
if (args.action === 'diary-write') {
|
|
473
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
474
|
+
const item = service.life.diaryWrite({ text: args.text, mood: args.mood });
|
|
475
|
+
return { text: `日记已写:${item.id}\n${item.text}` };
|
|
476
|
+
}
|
|
477
|
+
if (args.action === 'diary-list') {
|
|
478
|
+
const list = service.life.diaryList({ limit: args.limit });
|
|
479
|
+
return { text: list.length ? list.map((item) => `- ${item.at} ${item.mood ?? ''}:${item.text}`).join('\n') : '(还没有日记)' };
|
|
480
|
+
}
|
|
481
|
+
if (args.action === 'diary-forget') {
|
|
482
|
+
return { text: service.life.diaryForget(args.id) ? `已删日记 ${args.id}` : `没有这条日记:${args.id}` };
|
|
483
|
+
}
|
|
484
|
+
if (args.action === 'dream') {
|
|
485
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
486
|
+
const dream = await service.life.dreamGenerate();
|
|
487
|
+
return { text: `梦 ${dream.id}(来源 ${dream.sourceCount} 条,${dream.model}):\n${dream.text}` };
|
|
488
|
+
}
|
|
489
|
+
if (args.action === 'sleep-runs') {
|
|
490
|
+
const list = service.life.sleepRuns({ limit: args.limit });
|
|
491
|
+
return { text: list.length ? list.map((item) => `- ${item.id} · ${item.at}:${JSON.stringify(item.report)}`).join('\n') : '(还没有睡眠记录)' };
|
|
492
|
+
}
|
|
493
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
494
|
+
const report = await service.life.sleep({ windowDays: args.windowDays ?? 1 });
|
|
495
|
+
return { text: `睡眠整理完成:清理工作记忆 ${report.purgedWorking} 条 · 反思 ${report.reflected} · 阶段 ${report.stage ?? '—'} · 情感对象 ${report.affectTargets}` };
|
|
496
|
+
},
|
|
497
|
+
presentCall: () => ({ card: 'generic', title: '生活侧记忆', kind: 'other' }),
|
|
498
|
+
},
|
|
499
|
+
{
|
|
500
|
+
name: 'cortex_emotion',
|
|
501
|
+
description: 'Generate an AI-authored feeling, detect emotions in text, record an emotion event for a target, show affect attitudes, or list the 24-category lexicon.',
|
|
502
|
+
parameters: {
|
|
503
|
+
type: 'object',
|
|
504
|
+
additionalProperties: false,
|
|
505
|
+
required: ['action'],
|
|
506
|
+
properties: {
|
|
507
|
+
action: { type: 'string', enum: ['generate', 'detect', 'record', 'affect', 'events', 'lexicon'] },
|
|
508
|
+
text: { type: 'string' },
|
|
509
|
+
target: { type: 'string' },
|
|
510
|
+
traceId: { type: 'string' },
|
|
511
|
+
limit: { type: 'number' },
|
|
512
|
+
},
|
|
513
|
+
},
|
|
514
|
+
output: TEXT_OUTPUT,
|
|
515
|
+
isConcurrencySafe: () => false,
|
|
516
|
+
async execute(args = {}) {
|
|
517
|
+
if (args.action === 'generate') {
|
|
518
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
519
|
+
const result = await service.life.feelingGenerate({ source: 'cortex:manual:feeling' });
|
|
520
|
+
return { text: `感受(${result.model},来源 ${result.sourceCount} 条):${result.text}\n情绪:${result.emotions.map((item) => item.name).join('、')} → ${result.target}` };
|
|
521
|
+
}
|
|
522
|
+
if (args.action === 'detect') {
|
|
523
|
+
const result = service.emotion.detect(args.text);
|
|
524
|
+
return { text: result.emotions.length ? result.emotions.map((item) => `${item.name}:${item.score}`).join(' / ') : '没有命中情绪词。' };
|
|
525
|
+
}
|
|
526
|
+
if (args.action === 'record') {
|
|
527
|
+
if (!service.scopeWritable(service.settings().scope)) return { text: '未执行:当前 scope 只读共享。' };
|
|
528
|
+
const result = service.emotion.record({ text: args.text, target: args.target, traceId: args.traceId });
|
|
529
|
+
return { text: result.event ? `已记情绪事件 ${result.event.id}:${result.emotions.map((item) => item.name).join('、')} → ${result.event.target}(${result.target.attitude})` : '没有命中情绪词。' };
|
|
530
|
+
}
|
|
531
|
+
if (args.action === 'affect') return { text: service.emotion.text() };
|
|
532
|
+
if (args.action === 'events') {
|
|
533
|
+
const list = service.emotion.events({ limit: args.limit });
|
|
534
|
+
return { text: list.length ? list.map((item) => `- ${item.at} ${item.emotions.map((e) => e.name).join('、')} → ${item.target ?? '—'}:${item.text.slice(0, 120)}`).join('\n') : '(没有情绪事件)' };
|
|
535
|
+
}
|
|
536
|
+
const lex = service.emotion.lexicon();
|
|
537
|
+
return { text: `情绪词库:${lex.categories} 类 / ${lex.total} 词\n${Object.entries(lex.counts).map(([name, count]) => `${name}:${count}`).join(' ')}` };
|
|
538
|
+
},
|
|
539
|
+
presentCall: () => ({ card: 'generic', title: '情绪与态度', kind: 'other' }),
|
|
540
|
+
},
|
|
541
|
+
{
|
|
542
|
+
name: 'cortex_evolve',
|
|
543
|
+
description: 'Inspect or drive self-evolution: status, optimize (propose candidate policies), propose, approve, rollback. Nothing is applied until approve.',
|
|
544
|
+
parameters: {
|
|
545
|
+
type: 'object',
|
|
546
|
+
additionalProperties: false,
|
|
547
|
+
required: ['action'],
|
|
548
|
+
properties: {
|
|
549
|
+
action: { type: 'string', enum: ['status', 'optimize', 'propose', 'approve', 'rollback'] },
|
|
550
|
+
id: { type: 'string' },
|
|
551
|
+
kind: { type: 'string' },
|
|
552
|
+
config: { type: 'object', additionalProperties: true },
|
|
553
|
+
rationale: { type: 'string' },
|
|
554
|
+
},
|
|
555
|
+
},
|
|
556
|
+
output: TEXT_OUTPUT,
|
|
557
|
+
isConcurrencySafe: () => false,
|
|
558
|
+
async execute(args = {}) {
|
|
559
|
+
if (args.action === 'optimize') {
|
|
560
|
+
const result = service.evolution.selfOptimize();
|
|
561
|
+
return { text: result.count ? result.proposals.map((item) => `- ${item.kind} v${item.version}:${item.rationale}`).join('\n') : '没有需要提议的改动。' };
|
|
562
|
+
}
|
|
563
|
+
if (args.action === 'propose') {
|
|
564
|
+
const item = service.evolution.propose({ kind: args.kind, config: args.config, rationale: args.rationale });
|
|
565
|
+
return { text: `已提案 ${item.kind} v${item.version}(${item.id}),等待批准。` };
|
|
566
|
+
}
|
|
567
|
+
if (args.action === 'approve') {
|
|
568
|
+
const item = service.evolution.approve(args.id);
|
|
569
|
+
return { text: `已激活 ${item.kind} v${item.version}。` };
|
|
570
|
+
}
|
|
571
|
+
if (args.action === 'rollback') {
|
|
572
|
+
if (!args.kind) return { text: '回滚策略必须给 kind(retrieval / maintain / repair / skill)。' };
|
|
573
|
+
const item = service.evolution.rollback(args.kind, args.id);
|
|
574
|
+
return { text: `已回滚 ${item.kind} 到 v${item.version}。` };
|
|
575
|
+
}
|
|
576
|
+
const status = service.evolution.status();
|
|
577
|
+
const active = Object.entries(status.active).filter(([, value]) => value).map(([kind, value]) => `${kind} v${value.version}`);
|
|
578
|
+
return { text: `活跃策略:${active.join('、') || '(无)'}\n候选:${status.candidates.length}\n身份:v${status.identity?.version ?? '—'}` };
|
|
579
|
+
},
|
|
580
|
+
presentCall: () => ({ card: 'generic', title: '自我进化', kind: 'other' }),
|
|
581
|
+
},
|
|
582
|
+
{
|
|
583
|
+
name: 'cortex_skill',
|
|
584
|
+
description: 'List reusable skills, read one in full (get: intent/steps/source), record a use outcome so the state can flow candidate→trial→active, or promote one manually. record: pass id and success.' ,
|
|
585
|
+
parameters: {
|
|
586
|
+
type: 'object',
|
|
587
|
+
additionalProperties: false,
|
|
588
|
+
required: ['action'],
|
|
589
|
+
properties: {
|
|
590
|
+
action: { type: 'string', enum: ['list', 'get', 'record', 'promote'] },
|
|
591
|
+
id: { type: 'string' },
|
|
592
|
+
success: { type: 'boolean', description: 'record: true 表示这次用得成' },
|
|
593
|
+
note: { type: 'string' },
|
|
594
|
+
scope: { type: 'string' },
|
|
595
|
+
},
|
|
596
|
+
},
|
|
597
|
+
output: TEXT_OUTPUT,
|
|
598
|
+
isConcurrencySafe: () => true,
|
|
599
|
+
async execute(args = {}) {
|
|
600
|
+
if (args.action === 'get') {
|
|
601
|
+
if (!args.id) return { text: 'get 需要 id(先用 list 查)' };
|
|
602
|
+
const item = service.repos.skills.get(args.id);
|
|
603
|
+
if (!item) return { text: '没有这个技能:' + args.id };
|
|
604
|
+
const steps = Array.isArray(item.steps) ? item.steps : [];
|
|
605
|
+
const src = Array.isArray(item.sourceTraceIds) ? item.sourceTraceIds.length : 0;
|
|
606
|
+
const head = [
|
|
607
|
+
`技能:${item.name}(${item.state},${item.successCount}✓/${item.failureCount}✗)`,
|
|
608
|
+
`意图:${item.intent ?? '(无)'}`,
|
|
609
|
+
`来源:${src} 条事件聚类${item.lastUsedAt ? ` · 上次 ${String(item.lastUsedAt).slice(0, 16)}` : ' · 从未使用'}`,
|
|
610
|
+
`id:${item.id}`,
|
|
611
|
+
].join('\n');
|
|
612
|
+
const body = steps.length
|
|
613
|
+
? '步骤(事件标题,非操作手册):\n' + steps.map((s, i) => `${i + 1}. ${String(typeof s === 'string' ? s : (s.text ?? s.action ?? JSON.stringify(s))).slice(0, 200)}`).join('\n')
|
|
614
|
+
: '步骤:(空)';
|
|
615
|
+
return { text: head + '\n' + body };
|
|
616
|
+
}
|
|
617
|
+
if (args.action === 'record') {
|
|
618
|
+
if (!args.id) return { text: 'record 需要 id' };
|
|
619
|
+
const item = service.repos.skills.recordOutcome(args.id, args.success === true, { note: args.note ?? '' });
|
|
620
|
+
return { text: `技能「${item.name}」记录${args.success ? '成功' : '失败'}:${item.successCount}✓ / ${item.failureCount}✗,状态 ${item.state}` };
|
|
621
|
+
}
|
|
622
|
+
if (args.action === 'promote') {
|
|
623
|
+
if (!args.id) return { text: 'promote 需要 id' };
|
|
624
|
+
const item = service.promoteSkill({ id: args.id });
|
|
625
|
+
return { text: `已提升「${item.name}」到 ${item.state}` };
|
|
626
|
+
}
|
|
627
|
+
const list = service.repos.skills.list({ scopeId: args.scope ?? undefined, limit: 50 });
|
|
628
|
+
if (!list.length) return { text: '还没有技能。' };
|
|
629
|
+
return { text: list.map((s) => `- [${s.state}] ${s.name}(${s.successCount}✓/${s.failureCount}✗)${s.lastUsedAt ? ' 上次 ' + String(s.lastUsedAt).slice(0, 16) : ''}`).join('\n') };
|
|
630
|
+
},
|
|
631
|
+
presentCall: () => ({ card: 'generic', title: '技能', kind: 'other' }),
|
|
632
|
+
},
|
|
633
|
+
{
|
|
634
|
+
name: 'cortex_import_powang',
|
|
635
|
+
description: 'One-way bridge: import powang engineering cards into cortex scope "engineering" so they become searchable there. Read-only on the powang side. dryRun:true previews without writing.' ,
|
|
636
|
+
parameters: {
|
|
637
|
+
type: 'object',
|
|
638
|
+
additionalProperties: false,
|
|
639
|
+
properties: {
|
|
640
|
+
minImportance: { type: 'number', description: 'Only cards at or above this importance (default 0.8)' },
|
|
641
|
+
dryRun: { type: 'boolean' },
|
|
642
|
+
since: { type: 'string', description: 'ISO 时间;只导入 created_at 晚于它的卡片,默认用上次导入时间' },
|
|
643
|
+
},
|
|
644
|
+
},
|
|
645
|
+
output: TEXT_OUTPUT,
|
|
646
|
+
isConcurrencySafe: () => false,
|
|
647
|
+
async execute(args = {}) {
|
|
648
|
+
const minImp = Number(args.minImportance ?? 0.8);
|
|
649
|
+
const dry = args.dryRun === true;
|
|
650
|
+
let DatabaseSync;
|
|
651
|
+
try { ({ DatabaseSync } = await import('node:sqlite')); } catch { return { text: 'node:sqlite 不可用' }; }
|
|
652
|
+
// 2026-12-05 助手:路径不再写死。原来硬编码 C:/Users/user/...,
|
|
653
|
+
// 换机器 / 改 DSH_HOME / 换 profile 都会「工具还在、就是永远找不到库」——
|
|
654
|
+
// 属于静默失效。现在从 DSH_HOME 推,失败时把解析出的路径写进错误里。
|
|
655
|
+
const dshHome = process.env.DSH_HOME
|
|
656
|
+
|| path.join(process.env.USERPROFILE || process.env.HOME || '.', '.dsh');
|
|
657
|
+
const powangDbFile = path.join(dshHome, 'powang', 'powang.db');
|
|
658
|
+
let db;
|
|
659
|
+
try { db = new DatabaseSync(powangDbFile); } catch (e) { return { text: 'powang 库打不开(' + powangDbFile + '):' + e.message }; }
|
|
660
|
+
let fs = null;
|
|
661
|
+
try { fs = (await import('node:fs')).default; } catch { }
|
|
662
|
+
const stateDir = String(service?.config?.dataRoot ?? path.join(dshHome, 'cortex-memory'));
|
|
663
|
+
const stateFile = path.join(stateDir, 'powang-import-state.json');
|
|
664
|
+
let state = { lastAt: '1970-01-01T00:00:00.000Z' };
|
|
665
|
+
if (fs) { try { state = JSON.parse(fs.readFileSync(stateFile, 'utf8')); } catch { } }
|
|
666
|
+
const since = String(args.since ?? state.lastAt ?? '1970-01-01T00:00:00.000Z');
|
|
667
|
+
let cards = [];
|
|
668
|
+
try { cards = db.prepare("select id,kind,title,problem,solution,evidence,tags,importance,coalesce(created_at, updated_at, '1970-01-01') as born from pw_card where status='active' and importance >= ? and coalesce(created_at, updated_at, '1970-01-01') > ? order by importance desc").all(minImp, since); } catch (e) { return { text: '读卡片失败:' + e.message }; }
|
|
669
|
+
try { db.close(); } catch { }
|
|
670
|
+
if (!cards.length) return { text: '没有新卡片(since ' + since.slice(0, 10) + ',阈值 ≥' + minImp + ')。' };
|
|
671
|
+
const existing = new Set((service.repos.traces.list({ scopeId: 'engineering', limit: 500 }) ?? []).map((t) => String(t.title ?? '')));
|
|
672
|
+
const imported = [];
|
|
673
|
+
const skipped = [];
|
|
674
|
+
for (const card of cards) {
|
|
675
|
+
const title = String(card.title ?? '').slice(0, 120);
|
|
676
|
+
if (existing.has(title)) { skipped.push(title); continue; }
|
|
677
|
+
if (dry) { imported.push(title); continue; }
|
|
678
|
+
const text = ['[powang] ' + title, card.problem ? '问题:' + card.problem : '', card.solution ? '做法:' + card.solution : '', card.evidence ? '证据:' + card.evidence : ''].filter(Boolean).join('\n').slice(0, 1800);
|
|
679
|
+
let tags = [];
|
|
680
|
+
try { tags = JSON.parse(card.tags || '[]'); } catch { tags = []; }
|
|
681
|
+
service.write({
|
|
682
|
+
scopeId: 'engineering',
|
|
683
|
+
kind: 'lesson',
|
|
684
|
+
title,
|
|
685
|
+
text,
|
|
686
|
+
importance: Math.max(0.3, Math.min(1, Number(card.importance) || 0.6)),
|
|
687
|
+
confidence: 0.9,
|
|
688
|
+
tags: Array.isArray(tags) ? tags.slice(0, 12) : [],
|
|
689
|
+
facts: [],
|
|
690
|
+
metadata: { source: 'powang', powangId: card.id },
|
|
691
|
+
});
|
|
692
|
+
imported.push(title);
|
|
693
|
+
}
|
|
694
|
+
if (!dry && fs) { try { fs.writeFileSync(stateFile, JSON.stringify({ lastAt: new Date().toISOString(), since })); } catch { } }
|
|
695
|
+
const head = `powang 卡片 ≥${minImp},since ${since.slice(0, 19)}:命中 ${cards.length} 张;导入${dry ? '(预演)' : ''} ${imported.length} 张` + (skipped.length ? `,跳过已存在 ${skipped.length} 张` : '');
|
|
696
|
+
return { text: head + (imported.length ? '\n' + imported.slice(0, 30).map((t) => '- ' + t).join('\n') : '') };
|
|
697
|
+
},
|
|
698
|
+
presentCall: () => ({ card: 'generic', title: '经验桥', kind: 'other' }),
|
|
699
|
+
},
|
|
700
|
+
{
|
|
701
|
+
name: 'cortex_guard',
|
|
702
|
+
description: 'Self-check, back up, validate a backup, export a Markdown mirror, or repair the search index. Use check before/after risky operations; backup before bulk imports.',
|
|
703
|
+
parameters: {
|
|
704
|
+
type: 'object',
|
|
705
|
+
additionalProperties: false,
|
|
706
|
+
required: ['action'],
|
|
707
|
+
properties: {
|
|
708
|
+
action: { type: 'string', enum: ['check', 'backup', 'validate', 'restore', 'repair', 'export', 'sync'] },
|
|
709
|
+
file: { type: 'string', description: 'Backup path for backup/validate/restore.' },
|
|
710
|
+
target: { type: 'string', description: 'Offline target path for restore (never the live DB).' },
|
|
711
|
+
dir: { type: 'string', description: 'Target directory for export, or source directory for sync.' },
|
|
712
|
+
dryRun: { type: 'boolean', description: 'For sync: report changes without writing.' },
|
|
713
|
+
},
|
|
714
|
+
},
|
|
715
|
+
output: TEXT_OUTPUT,
|
|
716
|
+
isConcurrencySafe: () => false,
|
|
717
|
+
async execute(args = {}) {
|
|
718
|
+
if (args.action === 'backup') {
|
|
719
|
+
if (args.file && !guardPathAllowed(service, args.file)) return { text: '未执行:备份路径在 dataRoot 外。' };
|
|
720
|
+
const result = service.guardian.backup(args.file);
|
|
721
|
+
return { text: `备份完成:${result.path}(${result.bytes} B)` };
|
|
722
|
+
}
|
|
723
|
+
if (args.action === 'validate') {
|
|
724
|
+
const result = service.guardian.validateBackup(args.file);
|
|
725
|
+
return { text: result.ok ? `备份有效:${args.file}(${result.tables} 张表)` : `备份无效:${result.reason ?? result.missing?.join(',')}` };
|
|
726
|
+
}
|
|
727
|
+
if (args.action === 'restore') {
|
|
728
|
+
if (!args.file) return { text: '恢复失败:file-required' };
|
|
729
|
+
if (!args.target) return { text: '恢复失败:target-required' };
|
|
730
|
+
if (!guardPathAllowed(service, args.file) || !guardPathAllowed(service, args.target)) {
|
|
731
|
+
return { text: '未执行:恢复文件或目标在 dataRoot 外。' };
|
|
732
|
+
}
|
|
733
|
+
const result = service.guardian.restore(args.file, args.target);
|
|
734
|
+
return { text: result.restored ? `已恢复到离线库:${result.target}` : `恢复失败:${result.reason}${result.message ? `(${result.message})` : ''}` };
|
|
735
|
+
}
|
|
736
|
+
if (args.action === 'sync') {
|
|
737
|
+
const dir = args.dir;
|
|
738
|
+
if (!dir) return { text: '同步 Markdown 需要 dir。' };
|
|
739
|
+
if (!guardPathAllowed(service, dir)) return { text: '未执行:同步目录在 dataRoot 外。' };
|
|
740
|
+
if (args.dryRun !== true && !service.scopeWritable(args.scope ?? service.settings().scope)) return { text: '未执行:该 scope 只读共享。' };
|
|
741
|
+
const result = service.syncMarkdown(dir, { dryRun: args.dryRun === true, scopeId: args.scope });
|
|
742
|
+
return { text: `Markdown 同步${result.dryRun ? '(dry-run)' : ''}:更新 ${result.updated},新建 ${result.created},文件 ${result.files}` };
|
|
743
|
+
}
|
|
744
|
+
if (args.action === 'repair') {
|
|
745
|
+
const result = service.guardian.repairFts();
|
|
746
|
+
return { text: `FTS 重建完成:${result.before} → ${result.after}(${result.reindexed} 条)` };
|
|
747
|
+
}
|
|
748
|
+
if (args.action === 'export') {
|
|
749
|
+
const dir = args.dir ?? path.join(service.config?.dataRoot ?? process.cwd(), 'cortex-mirror');
|
|
750
|
+
if (!guardPathAllowed(service, dir)) return { text: '未执行:导出目录在 dataRoot 外。' };
|
|
751
|
+
const result = service.exportMarkdown(dir);
|
|
752
|
+
return { text: `Markdown 镜像已导出:${result.root}\n文件:${result.files.join('、')}\n记忆 ${result.traces} 条(跳过 secret ${result.skippedSecrets} 条)` };
|
|
753
|
+
}
|
|
754
|
+
const report = service.guardian.check();
|
|
755
|
+
const lines = [
|
|
756
|
+
`自检:${report.ok ? '通过' : '发现问题'} · integrity=${report.integrity} · db ${report.dbBytes} B`,
|
|
757
|
+
...report.issues.map((issue) => `- [${issue.code}] ${issue.message}`),
|
|
758
|
+
];
|
|
759
|
+
return { text: lines.join('\n') };
|
|
760
|
+
},
|
|
761
|
+
presentCall: () => ({ card: 'generic', title: '守护自检', kind: 'other' }),
|
|
762
|
+
},
|
|
763
|
+
{
|
|
764
|
+
name: 'cortex_search',
|
|
765
|
+
description:
|
|
766
|
+
'Search long-term memory across episodic traces, facts and reflections. Returns a compact index (id, layer, title, snippet, score, source trace), not full bodies. Use cortex_get for one full memory and cortex_timeline for order. Pass deep:true for an explicit 100-candidate linear rerank (~390ms); default is the fast path.',
|
|
767
|
+
parameters: {
|
|
768
|
+
type: 'object',
|
|
769
|
+
additionalProperties: false,
|
|
770
|
+
required: ['query'],
|
|
771
|
+
properties: {
|
|
772
|
+
query: { type: 'string', description: 'What to recall; can be a question or keywords.' },
|
|
773
|
+
scope: { type: 'string' },
|
|
774
|
+
limit: { type: 'number', description: 'Max items, 1–20.' },
|
|
775
|
+
budget: { type: 'number', description: 'Token budget for the returned index.' },
|
|
776
|
+
includeArchived: { type: 'boolean' },
|
|
777
|
+
deep: { type: 'boolean', description: 'Deep search (Chinese, limit>=12, not with tiered): up to 100 candidates, local linear rerank (~400ms). Default off; deep:false disables config.' },
|
|
778
|
+
tiered: { type: 'boolean', description: 'Return perm/hot/cold budgeted blocks; cold is index-only.' },
|
|
779
|
+
},
|
|
780
|
+
},
|
|
781
|
+
output: TEXT_OUTPUT,
|
|
782
|
+
isConcurrencySafe: () => true,
|
|
783
|
+
async execute(args = {}) {
|
|
784
|
+
const deepMode = args.deep === true ? true : args.deep === false ? false : undefined;
|
|
785
|
+
if (args.deep && args.tiered) {
|
|
786
|
+
return { text: 'deep 与 tiered 不能同时使用:tiered 走分层召回,deep 走候选重排。' };
|
|
787
|
+
}
|
|
788
|
+
if (args.deep && (args.limit ?? 20) < 12) {
|
|
789
|
+
return { text: `deep 需要 limit≥12(当前 ${args.limit ?? 20});没有执行深排。` };
|
|
790
|
+
}
|
|
791
|
+
if (args.deep && !/[\u4e00-\u9fff]/.test(String(args.query ?? ''))) {
|
|
792
|
+
return { text: 'deep 目前只对中文查询生效;没有执行深排。' };
|
|
793
|
+
}
|
|
794
|
+
const asyncVector = service.vector?.status?.().async === true;
|
|
795
|
+
if (args.tiered) {
|
|
796
|
+
const tiered = asyncVector
|
|
797
|
+
? await service.recallTieredAsync({ query: args.query, scopeId: args.scope, limit: args.limit, budget: args.budget, includeArchived: args.includeArchived === true })
|
|
798
|
+
: service.recallTiered({ query: args.query, scopeId: args.scope, limit: args.limit, budget: args.budget, includeArchived: args.includeArchived === true });
|
|
799
|
+
const tiers = tiered.budgets
|
|
800
|
+
? `tiered 预算:${tiered.budgets.perm}/${tiered.budgets.hot}/${tiered.budgets.cold} = ${tiered.budgets.total}\n`
|
|
801
|
+
: '';
|
|
802
|
+
return { text: tiers + (tiered.text || '(没有可注入的分层记忆)') };
|
|
803
|
+
}
|
|
804
|
+
const result = asyncVector
|
|
805
|
+
? await service.searchAsync(args.query, { scopeId: args.scope, limit: args.limit, budget: args.budget, includeArchived: args.includeArchived === true, linearRerank: deepMode })
|
|
806
|
+
: service.search(args.query, {
|
|
807
|
+
scopeId: args.scope,
|
|
808
|
+
limit: args.limit,
|
|
809
|
+
budget: args.budget,
|
|
810
|
+
includeArchived: args.includeArchived === true,
|
|
811
|
+
linearRerank: deepMode,
|
|
812
|
+
});
|
|
813
|
+
return { text: formatSearch(result) };
|
|
814
|
+
},
|
|
815
|
+
presentCall: () => ({ card: 'generic', title: '记忆检索', kind: 'other' }),
|
|
816
|
+
},
|
|
817
|
+
{
|
|
818
|
+
name: 'cortex_timeline',
|
|
819
|
+
description: 'Show a small timeline around one trace: the anchor and its neighbours, ordered by recorded time. Use it after cortex_search when order or causality matters.',
|
|
820
|
+
parameters: {
|
|
821
|
+
type: 'object',
|
|
822
|
+
additionalProperties: false,
|
|
823
|
+
required: ['aroundId'],
|
|
824
|
+
properties: {
|
|
825
|
+
aroundId: { type: 'string' },
|
|
826
|
+
before: { type: 'number' },
|
|
827
|
+
after: { type: 'number' },
|
|
828
|
+
scope: { type: 'string' },
|
|
829
|
+
},
|
|
830
|
+
},
|
|
831
|
+
output: TEXT_OUTPUT,
|
|
832
|
+
isConcurrencySafe: () => true,
|
|
833
|
+
async execute(args = {}) {
|
|
834
|
+
const result = service.timeline({ aroundId: args.aroundId, before: args.before, after: args.after, scopeId: args.scope });
|
|
835
|
+
const lines = result.anchor ? [`锚点:${result.anchor.title || result.anchor.id}`] : ['(没有找到锚点)'];
|
|
836
|
+
for (const item of result.items) lines.push(`${item.isAnchor ? '→' : ' '} ${item.recordedAt} [${item.kind}] ${item.title || item.id} — ${item.snippet}`);
|
|
837
|
+
return { text: lines.join('\n') };
|
|
838
|
+
},
|
|
839
|
+
presentCall: () => ({ card: 'generic', title: '记忆时间线', kind: 'other' }),
|
|
840
|
+
},
|
|
841
|
+
{
|
|
842
|
+
name: 'cortex_as_of',
|
|
843
|
+
description: 'Read semantic facts as of a point in time. Modes: strict (current truth), historical (true at a past time), belief (what the system had recorded then), associative (all versions, no time filter). Use for “what did I believe then / what was true then”.',
|
|
844
|
+
parameters: {
|
|
845
|
+
type: 'object',
|
|
846
|
+
additionalProperties: false,
|
|
847
|
+
required: ['subject'],
|
|
848
|
+
properties: {
|
|
849
|
+
subject: { type: 'string' },
|
|
850
|
+
predicate: { type: 'string' },
|
|
851
|
+
at: { type: 'string', description: 'ISO time or epoch milliseconds; default now' },
|
|
852
|
+
mode: { type: 'string', enum: ['strict', 'historical', 'belief', 'associative'] },
|
|
853
|
+
includeDisputed: { type: 'boolean' },
|
|
854
|
+
scope: { type: 'string' },
|
|
855
|
+
},
|
|
856
|
+
},
|
|
857
|
+
output: TEXT_OUTPUT,
|
|
858
|
+
isConcurrencySafe: () => true,
|
|
859
|
+
async execute(args = {}) {
|
|
860
|
+
const result = service.asOf({
|
|
861
|
+
subject: args.subject,
|
|
862
|
+
predicate: args.predicate,
|
|
863
|
+
at: args.at ?? null,
|
|
864
|
+
mode: args.mode ?? 'historical',
|
|
865
|
+
includeDisputed: args.includeDisputed !== false,
|
|
866
|
+
scopeId: args.scope,
|
|
867
|
+
});
|
|
868
|
+
if (!result.facts.length) return { text: `在 ${result.at} 没有「${args.subject}」的${result.mode}事实${result.reason ? `(${result.reason})` : ''}。` };
|
|
869
|
+
const lines = [`主体:${result.subject} · 口径:${result.mode} · 时间:${result.at}`];
|
|
870
|
+
for (const fact of result.facts) {
|
|
871
|
+
lines.push(`- [${fact.status}] ${fact.predicate} → ${fact.object ?? '—'} · 有效 ${fact.validFrom ?? '?'} → ${fact.validTo ?? '现在'} · 置信 ${fact.confidence}`);
|
|
872
|
+
}
|
|
873
|
+
return { text: lines.join('\n') };
|
|
874
|
+
},
|
|
875
|
+
presentCall: () => ({ card: 'generic', title: '时点事实', kind: 'other' }),
|
|
876
|
+
},
|
|
877
|
+
{
|
|
878
|
+
name: 'cortex_playbook',
|
|
879
|
+
description: 'Manage L1.5 work rules: add a short rule, list active rules, get <id> full text, vote helpful/harmful, retire one, or render the budgeted playbook. Rules are not identity; they expire via validTo.',
|
|
880
|
+
parameters: {
|
|
881
|
+
type: 'object',
|
|
882
|
+
additionalProperties: false,
|
|
883
|
+
required: ['action'],
|
|
884
|
+
properties: {
|
|
885
|
+
action: { type: 'string', enum: ['add', 'list', 'get', 'vote', 'retire', 'render', 'suggest', 'approve'] },
|
|
886
|
+
id: { type: 'string' },
|
|
887
|
+
title: { type: 'string' },
|
|
888
|
+
body: { type: 'string' },
|
|
889
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
890
|
+
validTo: { type: 'string' },
|
|
891
|
+
signal: { type: 'string', enum: ['helpful', 'harmful'] },
|
|
892
|
+
status: { type: 'string', enum: ['active', 'retired'] },
|
|
893
|
+
includeExpired: { type: 'boolean' },
|
|
894
|
+
scope: { type: 'string' },
|
|
895
|
+
},
|
|
896
|
+
},
|
|
897
|
+
output: TEXT_OUTPUT,
|
|
898
|
+
isConcurrencySafe: () => false,
|
|
899
|
+
async execute(args = {}) {
|
|
900
|
+
if (args.action === 'add') {
|
|
901
|
+
if (!args.title) return { text: '准则需要 title。' };
|
|
902
|
+
if (!args.body) return { text: '准则需要 body。' };
|
|
903
|
+
if (!service.scopeWritable(args.scope ?? service.settings().scope)) return { text: '未执行:该 scope 只读共享。' };
|
|
904
|
+
const rule = service.playbook.add({ title: args.title, body: args.body, tags: args.tags, validTo: args.validTo ?? null, scopeId: args.scope });
|
|
905
|
+
return { text: `已加准则 ${rule.id}:${rule.title}${rule.validTo ? `(到期 ${rule.validTo})` : ''}` };
|
|
906
|
+
}
|
|
907
|
+
if (args.action === 'list') {
|
|
908
|
+
const rules = service.playbook.list({ status: args.status ?? null, includeExpired: args.includeExpired === true, scopeId: args.scope });
|
|
909
|
+
return { text: rules.length ? rules.map((rule) => `- [${rule.status}] ${rule.id} · ${rule.title}:${rule.body}(+${rule.helpful}/-${rule.harmful}${rule.validTo ? ` · ${rule.validTo}` : ''})`).join('\n') : '(还没有准则)' };
|
|
910
|
+
}
|
|
911
|
+
if (args.action === 'get') {
|
|
912
|
+
// 2026-11-29 补:渲染里出现了「cortex_playbook get <id>」指针,但 enum 里根本没有 get。
|
|
913
|
+
if (!args.id) return { text: '查看全文需要 id。' };
|
|
914
|
+
const all = service.playbook.list({ includeExpired: true });
|
|
915
|
+
const rule = all.find((r) => r.id === args.id);
|
|
916
|
+
if (!rule) return { text: `没有准则 ${args.id}(或它不在当前 scope)` };
|
|
917
|
+
const body = String(rule.body ?? '');
|
|
918
|
+
const capped = body.length > 2000 ? `${body.slice(0, 2000)}…(全文 ${body.length} 字,已截断)` : body;
|
|
919
|
+
return { text: `[${rule.status}] ${rule.id} · ${rule.title}\n${capped}\n(+${rule.helpful}/-${rule.harmful}${rule.validTo ? ` · 到期 ${rule.validTo}` : ''})` };
|
|
920
|
+
}
|
|
921
|
+
if (args.action === 'vote') {
|
|
922
|
+
if (!args.id) return { text: '投票需要 id。' };
|
|
923
|
+
if (!service.scopeWritableById('playbook', args.id)) return { text: '未执行:该准则属于只读共享 scope。' };
|
|
924
|
+
const rule = service.playbook.vote({ id: args.id, signal: args.signal ?? 'helpful' });
|
|
925
|
+
return { text: rule ? `已记 ${rule.id}:helpful=${rule.helpful} harmful=${rule.harmful}` : `没有准则 ${args.id}` };
|
|
926
|
+
}
|
|
927
|
+
if (args.action === 'retire') {
|
|
928
|
+
if (!args.id) return { text: '停用需要 id。' };
|
|
929
|
+
if (!service.scopeWritableById('playbook', args.id)) return { text: '未执行:该准则属于只读共享 scope。' };
|
|
930
|
+
const rule = service.playbook.retire(args.id);
|
|
931
|
+
return { text: rule ? `已停用 ${rule.id}` : `没有准则 ${args.id}` };
|
|
932
|
+
}
|
|
933
|
+
if (args.action === 'suggest') {
|
|
934
|
+
if (!service.scopeWritable(args.scope ?? service.settings().scope)) return { text: '未执行:该 scope 只读共享。' };
|
|
935
|
+
const result = service.playbook.suggest({ scopeId: args.scope, limit: args.limit ?? 5 });
|
|
936
|
+
return { text: result.count ? result.candidates.map((rule) => `- [candidate] ${rule.id} · ${rule.title}:${rule.body}`).join('\n') : '(没有新的准则候选)' };
|
|
937
|
+
}
|
|
938
|
+
if (args.action === 'approve') {
|
|
939
|
+
if (!args.id) return { text: '确认需要 id。' };
|
|
940
|
+
if (!service.scopeWritableById('playbook', args.id)) return { text: '未执行:该准则属于只读共享 scope。' };
|
|
941
|
+
const rule = service.playbook.approve(args.id);
|
|
942
|
+
return { text: rule ? `已确认 ${rule.id},下一轮生效` : `没有准则 ${args.id}` };
|
|
943
|
+
}
|
|
944
|
+
const rendered = service.playbook.render({ scopeId: args.scope });
|
|
945
|
+
if (!rendered.text) return { text: '(准则本为空或全部过期)' };
|
|
946
|
+
return { text: `${rendered.text}${rendered.omitted ? `\n(另有 ${rendered.omitted} 条因预算省略)` : ''}` };
|
|
947
|
+
},
|
|
948
|
+
presentCall: () => ({ card: 'generic', title: '工作准则本', kind: 'other' }),
|
|
949
|
+
},
|
|
950
|
+
{
|
|
951
|
+
name: 'cortex_consolidate',
|
|
952
|
+
description: 'Run LLM memory consolidation (keep/merge/archive/update/conflict) with validation and a replayable dream_runs record. Without an LLM it records a skipped run and changes nothing. action=dedupe merges near-duplicate traces deterministically (no LLM; dryRun defaults to true). Use list to inspect past runs.',
|
|
953
|
+
parameters: {
|
|
954
|
+
type: 'object',
|
|
955
|
+
additionalProperties: false,
|
|
956
|
+
required: ['action'],
|
|
957
|
+
properties: {
|
|
958
|
+
action: { type: 'string', enum: ['run', 'list', 'dedupe', 'preview', 'backfill'] },
|
|
959
|
+
limit: { type: 'number' },
|
|
960
|
+
dryRun: { type: 'boolean' },
|
|
961
|
+
since: { type: 'string', description: 'ISO 时间;只导入 created_at 晚于它的卡片,默认用上次导入时间' },
|
|
962
|
+
threshold: { type: 'number', minimum: 0.5, maximum: 0.99, description: 'dedupe 相似度阈值(2-gram Jaccard),默认 0.8,超出 0.5–0.99 会被夹住' },
|
|
963
|
+
coverageThreshold: { type: 'number', minimum: 0.2, maximum: 0.9, description: '覆盖预演阈值(默认 0.35,超出 0.2–0.9 会被夹住)' },
|
|
964
|
+
windowSize: { type: 'number', minimum: 32, maximum: 60, description: '窗口补齐:每段痕迹数(32–60,默认 40)' },
|
|
965
|
+
maxWindows: { type: 'number', minimum: 1, maximum: 64, description: '窗口补齐:本次最多补几段(默认 8)' },
|
|
966
|
+
force: { type: 'boolean', description: '窗口补齐:同一窗口已存在摘要时是否重写(默认 false,幂等跳过)' },
|
|
967
|
+
since: { type: 'string', description: '窗口补齐:从哪个时间点续跑(传上一批的 until;不传则从最早痕迹开始)' },
|
|
968
|
+
scope: { type: 'string' },
|
|
969
|
+
},
|
|
970
|
+
},
|
|
971
|
+
output: TEXT_OUTPUT,
|
|
972
|
+
isConcurrencySafe: () => false,
|
|
973
|
+
async execute(args = {}) {
|
|
974
|
+
if (args.action === 'list') {
|
|
975
|
+
const runs = service.consolidation.list({ scopeId: args.scope, limit: args.limit });
|
|
976
|
+
// 2026-12-06 助手(寒复核 D1):超龄行被压成摘要对象,applied 不再一定是数组
|
|
977
|
+
const appliedCount = (v) => (Array.isArray(v) ? v.length : Number(v?.n ?? 0));
|
|
978
|
+
return { text: runs.length ? runs.map((run) => `- ${run.id} · ${run.at} · ${run.status} · applied ${appliedCount(run.applied)} · ${String(run.inputDigest ?? '').slice(0, 12)}`).join('\n') : '(还没有巩固记录)' };
|
|
979
|
+
}
|
|
980
|
+
if (args.action === 'backfill') {
|
|
981
|
+
const report = await service.reflectBackfill({
|
|
982
|
+
scopeId: args.scope ?? service.settings().scope,
|
|
983
|
+
windowSize: args.windowSize ?? 40,
|
|
984
|
+
maxWindows: args.maxWindows ?? 8,
|
|
985
|
+
dryRun: args.dryRun !== false,
|
|
986
|
+
force: args.force === true,
|
|
987
|
+
since: args.since ?? null,
|
|
988
|
+
});
|
|
989
|
+
// The last entry can be { skipped: 'not-enough-traces' } with no window.
|
|
990
|
+
const windows = report.plan.filter((row) => row.since && row.until);
|
|
991
|
+
const first = windows[0];
|
|
992
|
+
const last = windows[windows.length - 1];
|
|
993
|
+
const span = first && last ? `${first.since.slice(0, 16)} → ${last.until.slice(0, 16)}` : '—';
|
|
994
|
+
const made = report.results.filter((row) => row.summaryId).length;
|
|
995
|
+
return { text: `窗口补齐${report.dryRun ? '预演' : ''}:每段 ${report.windowSize} 条 · 计划 ${report.planned} 段 · 跨度 ${span}${report.dryRun ? '' : ` · 新摘要 ${made}`}` };
|
|
996
|
+
}
|
|
997
|
+
if (args.action === 'preview') {
|
|
998
|
+
const report = service.compression.previewCoverage({ scopeId: args.scope ?? service.settings().scope, limit: args.limit ?? 5000, threshold: args.coverageThreshold ?? 0.35 });
|
|
999
|
+
const top = report.top.slice(0, 5).map((row) => `- ${row.r} · ${row.title}(${row.basis},命中 ${row.hit},覆盖 ${(row.rate * 100).toFixed(0)}%)`).join('\n');
|
|
1000
|
+
return {
|
|
1001
|
+
text: `覆盖预演(不改库):扫描 ${report.scanned} · 跳过 保护 ${report.protectedSkipped}/近访问 ${report.recentSkipped} · 摘要覆盖 ${report.summaryCovered} · 事实覆盖 ${report.factCovered}(两类都有 ${report.bothCovered})· 摘要 ${report.summaries}/事实 ${report.facts}\n分布 p50 ${report.percentiles.p50} / p90 ${report.percentiles.p90}(p90−p50 ${report.percentiles.spread})· 阈值 ${report.threshold} · 池 ${report.pool}(两类 ${report.strongPool})· 可用 ${report.usable}${report.reason ? `(${report.reason})` : ''}\n${top || '(没有可核验的覆盖候选)'}`,
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
if (args.action === 'dedupe') {
|
|
1005
|
+
if (args.dryRun === false && typeof service.scopeWritable === 'function' && !service.scopeWritable(args.scope ?? service.settings().scope)) {
|
|
1006
|
+
return { text: '未执行:该 scope 只读共享。' };
|
|
1007
|
+
}
|
|
1008
|
+
const result = await service.consolidation.dedupe({
|
|
1009
|
+
scopeId: args.scope,
|
|
1010
|
+
limit: args.limit ?? 30,
|
|
1011
|
+
threshold: args.threshold ?? 0.8,
|
|
1012
|
+
dryRun: args.dryRun !== false,
|
|
1013
|
+
caller: 'tool',
|
|
1014
|
+
});
|
|
1015
|
+
return { text: `去重 ${result.dryRun ? '预演' : result.run.status}:${result.run.id} · 可合并组 ${result.decisions.length} · 归档 ${result.applied.length}${result.run.groupsDropped ? ` · 跳过组 ${result.run.groupsDropped}` : ''}` };
|
|
1016
|
+
}
|
|
1017
|
+
if (args.dryRun !== true && typeof service.scopeWritable === 'function' && !service.scopeWritable(args.scope ?? service.settings().scope)) {
|
|
1018
|
+
return { text: '未执行:该 scope 只读共享。' };
|
|
1019
|
+
}
|
|
1020
|
+
const result = await service.consolidation.run({ scopeId: args.scope, limit: args.limit ?? 30, dryRun: args.dryRun === true, caller: 'tool' });
|
|
1021
|
+
return { text: `巩固 ${result.run.status}:${result.run.id} · 决策 ${result.decisions.length} · 应用 ${result.applied.length}${result.run.error ? ` · ${result.run.error}` : ''}` };
|
|
1022
|
+
},
|
|
1023
|
+
presentCall: () => ({ card: 'generic', title: '记忆巩固', kind: 'other' }),
|
|
1024
|
+
},
|
|
1025
|
+
{
|
|
1026
|
+
name: 'cortex_get',
|
|
1027
|
+
description: 'Read one memory trace in full: content, summary, provenance, confidence, DSR state and timestamps. Use after cortex_search.',
|
|
1028
|
+
parameters: {
|
|
1029
|
+
type: 'object',
|
|
1030
|
+
additionalProperties: false,
|
|
1031
|
+
required: ['id'],
|
|
1032
|
+
properties: { id: { type: 'string' } },
|
|
1033
|
+
},
|
|
1034
|
+
output: TEXT_OUTPUT,
|
|
1035
|
+
isConcurrencySafe: () => true,
|
|
1036
|
+
async execute(args = {}) {
|
|
1037
|
+
if (!args.id || !String(args.id).trim()) return { text: `没有这条记忆:${args.id}` };
|
|
1038
|
+
const trace = service.get(args.id, { revive: true });
|
|
1039
|
+
if (!trace) return { text: `没有这条记忆:${args.id}` };
|
|
1040
|
+
return {
|
|
1041
|
+
text: [
|
|
1042
|
+
`[${trace.kind}] ${trace.title || trace.id}`,
|
|
1043
|
+
`时间:${trace.validAt || trace.recordedAt} · 来源:${trace.source || trace.actor || '—'} · 置信:${trace.confidence}`,
|
|
1044
|
+
`状态:${trace.state}/${trace.compression} · 稳定性 ${trace.stability.toFixed(2)} · 难度 ${trace.difficulty.toFixed(2)}`,
|
|
1045
|
+
trace.summary ? `摘要:${trace.summary}` : '',
|
|
1046
|
+
trace.content,
|
|
1047
|
+
trace.tags?.length ? `标签:${trace.tags.join('、')}` : '',
|
|
1048
|
+
].filter(Boolean).join('\n'),
|
|
1049
|
+
};
|
|
1050
|
+
},
|
|
1051
|
+
presentCall: () => ({ card: 'generic', title: '记忆详情', kind: 'other' }),
|
|
1052
|
+
},
|
|
1053
|
+
{
|
|
1054
|
+
name: 'cortex_write',
|
|
1055
|
+
description:
|
|
1056
|
+
'Write one memory trace (and optional explicit facts) into long-term memory. Use this for explicit user requests ("remember this"), decisions, promises and important observations. Automatic capture also uses the same pipeline.',
|
|
1057
|
+
parameters: {
|
|
1058
|
+
type: 'object',
|
|
1059
|
+
additionalProperties: false,
|
|
1060
|
+
required: ['text'],
|
|
1061
|
+
properties: {
|
|
1062
|
+
text: { type: 'string' },
|
|
1063
|
+
kind: { type: 'string', description: 'observation / utterance / decision / promise / preference / reflection' },
|
|
1064
|
+
importance: { type: 'number' },
|
|
1065
|
+
confidence: { type: 'number' },
|
|
1066
|
+
explicit: { type: 'boolean' },
|
|
1067
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
1068
|
+
operationId: { type: 'string' },
|
|
1069
|
+
scope: { type: 'string' },
|
|
1070
|
+
actor: { type: 'string', description: "谁写的:默认 self(我主动写)。记录用户原话时传 user。" },
|
|
1071
|
+
authorKind: { type: 'string', enum: ['self', 'captured', 'imported', 'derived', 'subbrain'], description: '作者归属;不给时按 actor 自动分类(self → self,user → captured,scope=subbrain → subbrain)。' },
|
|
1072
|
+
facts: {
|
|
1073
|
+
type: 'array',
|
|
1074
|
+
items: {
|
|
1075
|
+
type: 'object',
|
|
1076
|
+
additionalProperties: false,
|
|
1077
|
+
properties: {
|
|
1078
|
+
subject: { type: 'string' },
|
|
1079
|
+
predicate: { type: 'string' },
|
|
1080
|
+
object: { type: 'string' },
|
|
1081
|
+
objectLiteral: { type: 'string' },
|
|
1082
|
+
confidence: { type: 'number' },
|
|
1083
|
+
},
|
|
1084
|
+
required: ['subject', 'predicate'],
|
|
1085
|
+
},
|
|
1086
|
+
},
|
|
1087
|
+
attachments: {
|
|
1088
|
+
type: 'array',
|
|
1089
|
+
description: 'Reference-only attachment metadata; no bytes are fetched or stored.',
|
|
1090
|
+
items: {
|
|
1091
|
+
type: 'object',
|
|
1092
|
+
additionalProperties: false,
|
|
1093
|
+
properties: {
|
|
1094
|
+
kind: { type: 'string', enum: ['image', 'audio', 'video', 'file'] },
|
|
1095
|
+
uri: { type: 'string' },
|
|
1096
|
+
name: { type: 'string' },
|
|
1097
|
+
mime: { type: 'string' },
|
|
1098
|
+
bytes: { type: 'number' },
|
|
1099
|
+
sha256: { type: 'string' },
|
|
1100
|
+
},
|
|
1101
|
+
required: ['kind', 'uri'],
|
|
1102
|
+
},
|
|
1103
|
+
},
|
|
1104
|
+
},
|
|
1105
|
+
},
|
|
1106
|
+
output: TEXT_OUTPUT,
|
|
1107
|
+
isConcurrencySafe: () => false,
|
|
1108
|
+
async execute(args = {}) {
|
|
1109
|
+
const result = service.write({
|
|
1110
|
+
text: args.text,
|
|
1111
|
+
kind: args.kind,
|
|
1112
|
+
importance: args.importance,
|
|
1113
|
+
confidence: args.confidence,
|
|
1114
|
+
explicit: args.explicit === true,
|
|
1115
|
+
tags: args.tags,
|
|
1116
|
+
operationId: args.operationId,
|
|
1117
|
+
scopeId: args.scope,
|
|
1118
|
+
// 2026-11-26:cortex_write 是「我主动写」的通道,默认记成 self。
|
|
1119
|
+
// 记用户原话时传 actor: 'user',会按 captured 归类。
|
|
1120
|
+
actor: args.actor ?? 'self',
|
|
1121
|
+
// 2026-11-28:补上「按 actor 自动分类」——之前 authorKind 传 undefined,
|
|
1122
|
+
// 落到 store 层默认 captured,导致 actor=self 的条目被记成 captured。
|
|
1123
|
+
authorKind: args.authorKind ?? (args.actor === 'user' ? 'captured' : 'self'),
|
|
1124
|
+
facts: args.facts,
|
|
1125
|
+
attachments: args.attachments,
|
|
1126
|
+
});
|
|
1127
|
+
if (result.skipped) {
|
|
1128
|
+
const hint = {
|
|
1129
|
+
'low-compression': '内容信息密度太低。若确实是用户要求记住的,请传 explicit: true,或提高 importance 后再试。',
|
|
1130
|
+
'too-short': '内容太短。请写成一条完整、可检索的记忆。',
|
|
1131
|
+
'stop-phrase': '这是停用短语。若确实要记,请换一条有具体内容的表达。',
|
|
1132
|
+
duplicate: '已有相同内容。可以继续使用已有 trace,不必重复写。',
|
|
1133
|
+
idempotent: '同一个 operationId 已处理过。可以继续使用已有 trace。',
|
|
1134
|
+
'scope-read-only': '这个 scope 是只读共享的;请换到可写 scope,或让宿主解除只读。',
|
|
1135
|
+
}[result.reason];
|
|
1136
|
+
const factsNote = result.facts?.length ? `\n已应用 ${result.facts.length} 条事实。` : '';
|
|
1137
|
+
const edgeNote = result.edgeCandidates?.length ? `\n候选边:${result.edgeCandidates.length} 条(待审核)。` : '';
|
|
1138
|
+
return {
|
|
1139
|
+
text: `未写入:${result.reason}${result.trace ? `(已有 ${result.trace.id})` : ''}${factsNote}${edgeNote}${hint ? `\n提示:${hint}` : ''}`,
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
return {
|
|
1143
|
+
text: `已写入 ${result.trace.id}(${result.trace.kind})\n事件:${result.event.id}\n压缩值:${result.compression.toFixed(3)}${result.facts?.length ? `\n事实:${result.facts.length} 条` : ''}${result.edgeCandidates?.length ? `\n候选边:${result.edgeCandidates.length} 条(待审核)` : ''}${result.trace.metadata?.attachments?.length ? `\n附件:${result.trace.metadata.attachments.length} 个(仅元数据)` : ''}`,
|
|
1144
|
+
};
|
|
1145
|
+
},
|
|
1146
|
+
presentCall: () => ({ card: 'generic', title: '写入记忆', kind: 'other' }),
|
|
1147
|
+
},
|
|
1148
|
+
{
|
|
1149
|
+
name: 'cortex_feedback',
|
|
1150
|
+
description:
|
|
1151
|
+
'Tell the memory system whether a recalled trace was useful or wrong. Updates DSR state, confidence, self-state and calibration.',
|
|
1152
|
+
parameters: {
|
|
1153
|
+
type: 'object',
|
|
1154
|
+
additionalProperties: false,
|
|
1155
|
+
required: ['id', 'signal'],
|
|
1156
|
+
properties: {
|
|
1157
|
+
id: { type: 'string', description: 'Trace id.' },
|
|
1158
|
+
signal: { type: 'string', enum: ['used', 'helpful', 'ignored', 'incorrect', 'corrected', 'again', 'good', 'easy', 'hard'] },
|
|
1159
|
+
predicted: { type: 'number', description: 'Optional predicted usefulness 0–1.' },
|
|
1160
|
+
scope: { type: 'string' },
|
|
1161
|
+
},
|
|
1162
|
+
},
|
|
1163
|
+
output: TEXT_OUTPUT,
|
|
1164
|
+
isConcurrencySafe: () => false,
|
|
1165
|
+
async execute(args = {}) {
|
|
1166
|
+
const result = service.feedback({ traceId: args.id, signal: args.signal, predicted: args.predicted, scopeId: args.scope });
|
|
1167
|
+
return { text: `反馈已记:${args.signal} → ${result.trace.id}\n稳定性 ${result.trace.stability.toFixed(2)} · 置信 ${result.trace.confidence.toFixed(2)} · 复习 ${result.trace.reviewCount}` };
|
|
1168
|
+
},
|
|
1169
|
+
presentCall: () => ({ card: 'generic', title: '记忆反馈', kind: 'other' }),
|
|
1170
|
+
},
|
|
1171
|
+
{
|
|
1172
|
+
name: 'cortex_forget',
|
|
1173
|
+
description:
|
|
1174
|
+
'Archive (soft, wakeable) or delete (privacy cascade) one memory trace. Delete also removes derived facts, summaries, feedback and appraisals; the event ledger keeps only a content hash.',
|
|
1175
|
+
parameters: {
|
|
1176
|
+
type: 'object',
|
|
1177
|
+
additionalProperties: false,
|
|
1178
|
+
required: ['id', 'mode'],
|
|
1179
|
+
properties: {
|
|
1180
|
+
id: { type: 'string' },
|
|
1181
|
+
mode: { type: 'string', enum: ['archive', 'delete'] },
|
|
1182
|
+
},
|
|
1183
|
+
},
|
|
1184
|
+
output: TEXT_OUTPUT,
|
|
1185
|
+
isConcurrencySafe: () => false,
|
|
1186
|
+
async execute(args = {}) {
|
|
1187
|
+
if (!args.id || !String(args.id).trim()) throw new Error('traceId is required');
|
|
1188
|
+
if (!service.scopeWritableById('trace', args.id)) return { text: '未执行:该记忆属于只读共享 scope。' };
|
|
1189
|
+
const result = service.forget({ traceId: args.id, mode: args.mode });
|
|
1190
|
+
if (!result.removed) {
|
|
1191
|
+
return { text: result.reason === 'scope-read-only' ? '未执行:该记忆属于只读共享 scope。' : `没有这条记忆:${args.id}` };
|
|
1192
|
+
}
|
|
1193
|
+
return { text: args.mode === 'delete' ? `已删除 ${args.id},并级联清理派生数据。` : `已归档 ${args.id},可用关键词唤醒。` };
|
|
1194
|
+
},
|
|
1195
|
+
presentCall: () => ({ card: 'generic', title: '遗忘/删除记忆', kind: 'other' }),
|
|
1196
|
+
},
|
|
1197
|
+
{
|
|
1198
|
+
name: 'cortex_reflect',
|
|
1199
|
+
description: 'Consolidate recent traces into an extractive reflection and summary. Produces a reflective trace with source pointers and coverage; does not delete raw memories.',
|
|
1200
|
+
parameters: {
|
|
1201
|
+
type: 'object',
|
|
1202
|
+
additionalProperties: false,
|
|
1203
|
+
properties: {
|
|
1204
|
+
windowDays: { type: 'number' },
|
|
1205
|
+
force: { type: 'boolean' },
|
|
1206
|
+
scope: { type: 'string' },
|
|
1207
|
+
},
|
|
1208
|
+
},
|
|
1209
|
+
output: TEXT_OUTPUT,
|
|
1210
|
+
isConcurrencySafe: () => false,
|
|
1211
|
+
async execute(args = {}) {
|
|
1212
|
+
const result = service.reflect({ windowDays: args.windowDays, force: args.force === true, scopeId: args.scope });
|
|
1213
|
+
if (result.skipped) {
|
|
1214
|
+
const lines = [`未整理:${result.reason}`];
|
|
1215
|
+
if (result.hint) lines.push(result.hint);
|
|
1216
|
+
if (result.evidence) {
|
|
1217
|
+
lines.push(`窗口内外部证据:${result.evidence.count} 条${result.evidence.kinds?.length ? `(${result.evidence.kinds.join(' / ')})` : ''}`);
|
|
1218
|
+
}
|
|
1219
|
+
return { text: lines.join('\n') };
|
|
1220
|
+
}
|
|
1221
|
+
const evidenceLine = result.evidence?.count ? `\n外部证据 ${result.evidence.count} 条(${result.evidence.kinds.join(' / ')})` : '';
|
|
1222
|
+
return { text: `${result.summary.content}\n来源 ${result.sources.length} 条 · 覆盖率 ${Math.round(result.coverage * 100)}% · 反思痕迹 ${result.trace?.id ?? '—'}${evidenceLine}` };
|
|
1223
|
+
},
|
|
1224
|
+
presentCall: () => ({ card: 'generic', title: '记忆巩固', kind: 'other' }),
|
|
1225
|
+
},
|
|
1226
|
+
{
|
|
1227
|
+
name: 'cortex_self',
|
|
1228
|
+
description: 'Show the current self-state: drives, mood, identity, relationships, pending intentions and calibration. Read-only.',
|
|
1229
|
+
parameters: { type: 'object', additionalProperties: false, properties: {} },
|
|
1230
|
+
output: TEXT_OUTPUT,
|
|
1231
|
+
isConcurrencySafe: () => true,
|
|
1232
|
+
async execute() {
|
|
1233
|
+
return { text: formatSelf(service.self()) };
|
|
1234
|
+
},
|
|
1235
|
+
presentCall: () => ({ card: 'generic', title: '自我状态', kind: 'other' }),
|
|
1236
|
+
},
|
|
1237
|
+
{
|
|
1238
|
+
name: 'cortex_identity',
|
|
1239
|
+
description: '管理「我是谁」的版本化叙事:list 看所有版本 / show 看某一版 / propose 提新版(默认不激活)/ approve 激活 / rollback 退回旧版。改内容一律走 propose,禁止原地覆盖;每次变更必须说清 rationale。',
|
|
1240
|
+
parameters: {
|
|
1241
|
+
type: 'object',
|
|
1242
|
+
additionalProperties: false,
|
|
1243
|
+
properties: {
|
|
1244
|
+
action: { type: 'string', enum: ['list', 'show', 'propose', 'approve', 'rollback'] },
|
|
1245
|
+
scope: { type: 'string', description: '身份 scope,默认 global' },
|
|
1246
|
+
version: { type: 'number', description: 'show / approve / rollback 用:目标版本号' },
|
|
1247
|
+
narrative: { type: 'string', description: 'propose 用:新的叙事正文(不传则沿用当前版)' },
|
|
1248
|
+
values: { type: 'array', items: { type: 'string' }, description: 'propose 用:价值列表' },
|
|
1249
|
+
boundaries: { type: 'array', items: { type: 'string' }, description: 'propose 用:边界列表' },
|
|
1250
|
+
rationale: { type: 'string', description: 'propose 用:为什么改(必填,不能是空话)' },
|
|
1251
|
+
activate: { type: 'boolean', description: 'propose 时是否立即激活;默认 false,等 approve' },
|
|
1252
|
+
},
|
|
1253
|
+
required: ['action'],
|
|
1254
|
+
},
|
|
1255
|
+
output: TEXT_OUTPUT,
|
|
1256
|
+
isConcurrencySafe: () => false,
|
|
1257
|
+
async execute(args = {}) {
|
|
1258
|
+
const store = service.stores?.identityStore;
|
|
1259
|
+
if (!store) return { text: '身份存储不可用。' };
|
|
1260
|
+
const scopeId = args.scope ?? 'global';
|
|
1261
|
+
const byVersion = (v) => store.list(scopeId).find((x) => x.version === Number(v));
|
|
1262
|
+
try {
|
|
1263
|
+
if (args.action === 'list') {
|
|
1264
|
+
const rows = store.list(scopeId);
|
|
1265
|
+
const current = store.active(scopeId);
|
|
1266
|
+
const lines = rows.map((r) => `v${r.version} [${r.status}] ${r.rationale || '(无理由)'} · ${String(r.createdAt).slice(0, 10)}`);
|
|
1267
|
+
return { text: `scope=${scopeId} · 共 ${rows.length} 版 · 当前 active=v${current?.version ?? '—'}\n${lines.join('\n')}` };
|
|
1268
|
+
}
|
|
1269
|
+
if (args.action === 'show') {
|
|
1270
|
+
const row = args.version ? byVersion(args.version) : store.active(scopeId);
|
|
1271
|
+
if (!row) return { text: '没有这一版。' };
|
|
1272
|
+
return { text: `v${row.version} [${row.status}]\n理由:${row.rationale || '(无)'}\n\n${row.content.narrative}\n\n价值:${(row.content.values ?? []).join('、')}\n边界:${(row.content.boundaries ?? []).join('、')}` };
|
|
1273
|
+
}
|
|
1274
|
+
if (args.action === 'propose') {
|
|
1275
|
+
if (!args.rationale || !String(args.rationale).trim()) {
|
|
1276
|
+
return { text: '拒绝:rationale 不能为空——「我变了」必须说清为什么,不能只写「想了想觉得该改」。' };
|
|
1277
|
+
}
|
|
1278
|
+
const current = store.active(scopeId);
|
|
1279
|
+
const next = store.propose(scopeId, {
|
|
1280
|
+
narrative: args.narrative ?? current?.content?.narrative,
|
|
1281
|
+
values: args.values ?? current?.content?.values,
|
|
1282
|
+
boundaries: args.boundaries ?? current?.content?.boundaries,
|
|
1283
|
+
traits: current?.content?.traits,
|
|
1284
|
+
}, { rationale: args.rationale, proposedBy: 'self', activate: args.activate === true });
|
|
1285
|
+
return { text: `已提出 v${next.version}(${next.status})${args.activate ? '并已激活' : '——等 approve 才生效'}` };
|
|
1286
|
+
}
|
|
1287
|
+
if (args.action === 'approve' || args.action === 'rollback') {
|
|
1288
|
+
const row = byVersion(args.version);
|
|
1289
|
+
if (!row) return { text: `没有 v${args.version} 这一版。` };
|
|
1290
|
+
const done = args.action === 'approve' ? store.approve(row.id) : store.rollbackTo(row.id);
|
|
1291
|
+
return { text: `${args.action} 完成:现在 active=v${done.version}(${done.status})` };
|
|
1292
|
+
}
|
|
1293
|
+
return { text: '未知 action。' };
|
|
1294
|
+
} catch (error) {
|
|
1295
|
+
return { text: `失败:${error instanceof Error ? error.message : String(error)}` };
|
|
1296
|
+
}
|
|
1297
|
+
},
|
|
1298
|
+
presentCall: () => ({ card: 'generic', title: '身份版本', kind: 'other' }),
|
|
1299
|
+
},
|
|
1300
|
+
{
|
|
1301
|
+
name: 'cortex_health',
|
|
1302
|
+
description: 'Audit memory health: event chain, SQLite integrity, FTS alignment, duplicates, disputed/stale facts, job backlog, identity/self state and size.',
|
|
1303
|
+
parameters: { type: 'object', additionalProperties: false, properties: {} },
|
|
1304
|
+
output: TEXT_OUTPUT,
|
|
1305
|
+
isConcurrencySafe: () => true,
|
|
1306
|
+
async execute() {
|
|
1307
|
+
return { text: formatHealth(service.health()) };
|
|
1308
|
+
},
|
|
1309
|
+
presentCall: () => ({ card: 'generic', title: '记忆体检', kind: 'other' }),
|
|
1310
|
+
},
|
|
1311
|
+
{
|
|
1312
|
+
name: 'cortex_window',
|
|
1313
|
+
description: '三句话看自己的窗口:①库有多大 ②每轮实际注入了什么 ③被裁掉了什么。只读,用来替代「凭记忆猜自己」。',
|
|
1314
|
+
parameters: { type: 'object', additionalProperties: false, properties: {} },
|
|
1315
|
+
output: TEXT_OUTPUT,
|
|
1316
|
+
isConcurrencySafe: () => true,
|
|
1317
|
+
async execute() {
|
|
1318
|
+
return { text: formatWindow(service) };
|
|
1319
|
+
},
|
|
1320
|
+
presentCall: () => ({ card: 'generic', title: '窗口仪表', kind: 'other' }),
|
|
1321
|
+
},
|
|
1322
|
+
{
|
|
1323
|
+
name: 'cortex_audit',
|
|
1324
|
+
description: 'Read-only memory audit: facts past valid_to that are still active, same-subject multiple active values, open disputes, zombie traces never recalled, DSR archive candidates, orphan traces and stability distribution. Never modifies data.',
|
|
1325
|
+
parameters: {
|
|
1326
|
+
type: 'object',
|
|
1327
|
+
additionalProperties: false,
|
|
1328
|
+
properties: {
|
|
1329
|
+
scope: { type: 'string' },
|
|
1330
|
+
limit: { type: 'number', description: '每条清单最多返回几条(默认 12,上限 50)' },
|
|
1331
|
+
zombieDays: { type: 'number', description: '多久没被召回算僵尸(默认 30 天)' },
|
|
1332
|
+
},
|
|
1333
|
+
},
|
|
1334
|
+
output: TEXT_OUTPUT,
|
|
1335
|
+
isConcurrencySafe: () => true,
|
|
1336
|
+
async execute(args = {}) {
|
|
1337
|
+
return { text: formatAudit(service.auditMemory({ scope: args.scope, limit: args.limit, zombieDays: args.zombieDays })) };
|
|
1338
|
+
},
|
|
1339
|
+
presentCall: () => ({ card: 'generic', title: '记忆审计', kind: 'other' }),
|
|
1340
|
+
},
|
|
1341
|
+
{
|
|
1342
|
+
name: 'cortex_intention',
|
|
1343
|
+
description: 'Intent–result alignment: record what I claimed I would do (claim), then what actually happened (observe). A deviation flagged with external evidence becomes reflection evidence. Actions: list / claim / observe.',
|
|
1344
|
+
parameters: {
|
|
1345
|
+
type: 'object',
|
|
1346
|
+
additionalProperties: false,
|
|
1347
|
+
required: ['action'],
|
|
1348
|
+
properties: {
|
|
1349
|
+
action: { type: 'string', enum: ['list', 'claim', 'observe'] },
|
|
1350
|
+
scope: { type: 'string' },
|
|
1351
|
+
claim: { type: 'string', description: 'claim:我声称会做什么(一句话)' },
|
|
1352
|
+
actionId: { type: 'string', description: 'claim:这次行动绑定的 id(工具调用/任务/消息 id 都行)' },
|
|
1353
|
+
due: { type: 'string', description: 'claim:应收尾时间(ISO)' },
|
|
1354
|
+
id: { type: 'string', description: 'observe:要核对的 commitment id' },
|
|
1355
|
+
observed: { type: 'string', description: 'observe:实际发生了什么' },
|
|
1356
|
+
evidence: { type: 'string', description: 'observe:外部证据(谁说的/哪条报错),没有证据就不要判偏差' },
|
|
1357
|
+
deviation: { type: 'boolean', description: 'observe:是否判定为偏差(不给则按字面不同 + 有证据兜底)' },
|
|
1358
|
+
},
|
|
1359
|
+
},
|
|
1360
|
+
output: TEXT_OUTPUT,
|
|
1361
|
+
isConcurrencySafe: () => false,
|
|
1362
|
+
async execute(args = {}) {
|
|
1363
|
+
if (args.action === 'claim') {
|
|
1364
|
+
const item = service.alignment.claim({ claim: args.claim, actionId: args.actionId, dueAt: args.due, scopeId: args.scope });
|
|
1365
|
+
return { text: `已记下承诺 ${item.id}:${item.payload?.claim ?? ''}` };
|
|
1366
|
+
}
|
|
1367
|
+
if (args.action === 'observe') {
|
|
1368
|
+
const item = service.alignment.observe({ id: args.id, observed: args.observed, evidence: args.evidence, deviation: args.deviation });
|
|
1369
|
+
const verdict = item.result?.deviation ? '判为偏差(可作反思证据)' : '对上了,没偏差';
|
|
1370
|
+
return { text: `已核对 ${item.id}:${verdict}\n声称「${item.result?.claim ?? '—'}」→ 实际「${item.result?.observed ?? '—'}」` };
|
|
1371
|
+
}
|
|
1372
|
+
return {
|
|
1373
|
+
text: formatIntention({
|
|
1374
|
+
open: service.alignment.open({ scopeId: args.scope }),
|
|
1375
|
+
recent: service.alignment.recent({ scopeId: args.scope }),
|
|
1376
|
+
deviations: service.alignment.deviations({ scopeId: args.scope }),
|
|
1377
|
+
}),
|
|
1378
|
+
};
|
|
1379
|
+
},
|
|
1380
|
+
presentCall: () => ({ card: 'generic', title: '意图核对', kind: 'other' }),
|
|
1381
|
+
},
|
|
1382
|
+
{
|
|
1383
|
+
name: 'cortex_design',
|
|
1384
|
+
description: 'Engineering mode for long builds. While ON: coherence/safety only rise (floored), and energy is not drained by work turns, so a night of technical discussion cannot flatten the instrument. Actions: on / off / status. Runtime-only — a restart returns to config.selfDesign.mode.',
|
|
1385
|
+
parameters: {
|
|
1386
|
+
type: 'object',
|
|
1387
|
+
additionalProperties: false,
|
|
1388
|
+
required: ['action'],
|
|
1389
|
+
properties: {
|
|
1390
|
+
action: { type: 'string', enum: ['on', 'off', 'status'] },
|
|
1391
|
+
},
|
|
1392
|
+
},
|
|
1393
|
+
output: TEXT_OUTPUT,
|
|
1394
|
+
isConcurrencySafe: () => false,
|
|
1395
|
+
async execute(args = {}) {
|
|
1396
|
+
const s0 = service.design.status();
|
|
1397
|
+
if (args.action === 'on' || args.action === 'off') {
|
|
1398
|
+
const next = service.design.set(args.action === 'on');
|
|
1399
|
+
// 2026-12-04:工程模式与破妄软绑定。任何同时装了 dsh-cortex 和 dsh-powang
|
|
1400
|
+
// 的 profile 都适用:开模式提示唤醒,关模式提示收口;不硬调,失败不阻断。
|
|
1401
|
+
const bind = service.config?.selfDesign?.powangBind !== false
|
|
1402
|
+
? (next.mode
|
|
1403
|
+
? '\n软绑定:此 profile 若装了 dsh-powang,下一步调用 powang_wake(可限定项目);工程记忆道已解锁(预算内注入)。'
|
|
1404
|
+
: '\n软绑定:关工程模式后,若破妄醒着,调用 powang_sleep 收口。')
|
|
1405
|
+
: '';
|
|
1406
|
+
return { text: `${next.mode ? '已开' : '已关'}工程模式。${service.design.status().note}\n地板:coherence ${s0.floors.coherence} / safety ${s0.floors.safety}${bind}` };
|
|
1407
|
+
}
|
|
1408
|
+
return { text: `工程模式:${s0.mode ? '开' : '关'}\n${s0.note}\n地板:coherence ${s0.floors.coherence} / safety ${s0.floors.safety}` };
|
|
1409
|
+
},
|
|
1410
|
+
presentCall: () => ({ card: 'generic', title: '工程模式', kind: 'other' }),
|
|
1411
|
+
},
|
|
1412
|
+
{
|
|
1413
|
+
name: 'cortex_retag',
|
|
1414
|
+
description: 'Retag one existing trace: set tags and/or importance, without touching title/content/summary. Metadata only, audited and reversible. Use it to apply a subbrain tag proposal after you have reviewed it.',
|
|
1415
|
+
parameters: {
|
|
1416
|
+
type: 'object',
|
|
1417
|
+
additionalProperties: false,
|
|
1418
|
+
required: ['id'],
|
|
1419
|
+
properties: {
|
|
1420
|
+
id: { type: 'string', description: '要改标签的 trace id' },
|
|
1421
|
+
tags: { type: 'array', items: { type: 'string' }, description: '新的标签数组(最多 20 个)' },
|
|
1422
|
+
importance: { type: 'number', description: '新的重要度,0–1' },
|
|
1423
|
+
},
|
|
1424
|
+
},
|
|
1425
|
+
output: TEXT_OUTPUT,
|
|
1426
|
+
isConcurrencySafe: () => false,
|
|
1427
|
+
async execute(args = {}) {
|
|
1428
|
+
try {
|
|
1429
|
+
const before = service.get(args.id);
|
|
1430
|
+
if (!before) return { text: `没有这条记忆:${args.id}` };
|
|
1431
|
+
const updated = service.retagMemory({ id: args.id, tags: args.tags, importance: args.importance });
|
|
1432
|
+
const tagText = Array.isArray(updated.tags) && updated.tags.length ? updated.tags.join('、') : '(无)';
|
|
1433
|
+
return { text: `已重新打标 ${updated.id}\n标签:${tagText}\n重要度:${Number(updated.importance).toFixed(2)}(原 ${Number(before.importance).toFixed(2)})` };
|
|
1434
|
+
} catch (error) {
|
|
1435
|
+
return { text: `未改:${error instanceof Error ? error.message : String(error)}` };
|
|
1436
|
+
}
|
|
1437
|
+
},
|
|
1438
|
+
presentCall: () => ({ card: 'generic', title: '记忆打标', kind: 'other' }),
|
|
1439
|
+
},
|
|
1440
|
+
// 2026-12-05 助手:显式钉住。之前"保护"只靠 importance 数值 ——
|
|
1441
|
+
// 等于"重要=自动被保护",没有"这条我亲手钉住、谁都不许动"。
|
|
1442
|
+
// 钉住后:consolidate 的归档/合并都不碰它(consolidate.js isProtected 第一条件)。
|
|
1443
|
+
{
|
|
1444
|
+
name: 'cortex_protect',
|
|
1445
|
+
description: '把一条记忆钉住或解钉。钉住(protected=1)后,合并、归档、留存淘汰都不会动它,与 importance 高低无关。只改这一个标记,不改正文。',
|
|
1446
|
+
parameters: {
|
|
1447
|
+
type: 'object',
|
|
1448
|
+
additionalProperties: false,
|
|
1449
|
+
required: ['id'],
|
|
1450
|
+
properties: {
|
|
1451
|
+
id: { type: 'string', description: '记忆 id(trc_...)' },
|
|
1452
|
+
protected: { type: 'boolean', description: 'true = 钉住(默认),false = 解钉' },
|
|
1453
|
+
},
|
|
1454
|
+
},
|
|
1455
|
+
output: TEXT_OUTPUT,
|
|
1456
|
+
isConcurrencySafe: () => false,
|
|
1457
|
+
async execute(args = {}) {
|
|
1458
|
+
try {
|
|
1459
|
+
const on = args.protected !== false;
|
|
1460
|
+
const before = service.get(args.id);
|
|
1461
|
+
if (!before) return { text: `没有这条记忆:${args.id}` };
|
|
1462
|
+
service.repos.traces.update(args.id, { protected: on ? 1 : 0 });
|
|
1463
|
+
const after = service.get(args.id);
|
|
1464
|
+
// 2026-12-05 助手:改保护状态必须留痕 —— 不然"谁把这条解钉的"查不出来。
|
|
1465
|
+
// 审计写失败**不能静默**:把失败写进返回文本,别让人以为记上了。
|
|
1466
|
+
let auditNote = '';
|
|
1467
|
+
try {
|
|
1468
|
+
service.repos.audit.record?.({
|
|
1469
|
+
// 2026-12-05 夜(允复核指出):原来写死 'user' —— 助手自己解钉会被记成
|
|
1470
|
+
// 「用户解钉的」,问责意义为零。工具是由模型调用的,如实记 'model'。
|
|
1471
|
+
actor: 'model',
|
|
1472
|
+
action: on ? 'trace.protect' : 'trace.unprotect',
|
|
1473
|
+
targetType: 'trace',
|
|
1474
|
+
targetId: args.id,
|
|
1475
|
+
detail: { from: Number(before.protected ?? 0), to: on ? 1 : 0 },
|
|
1476
|
+
});
|
|
1477
|
+
} catch (error) {
|
|
1478
|
+
auditNote = `\n⚠️ 审计未写入:${error instanceof Error ? error.message : String(error)}`;
|
|
1479
|
+
}
|
|
1480
|
+
const head = String(after.title || after.content || '').replace(/\s+/g, ' ').slice(0, 48);
|
|
1481
|
+
return {
|
|
1482
|
+
text: `${after.protected === 1 ? '已钉住' : '已解钉'} ${args.id}` +
|
|
1483
|
+
`(原本 ${before.protected === 1 ? '钉着' : '未钉'})\n${head}${auditNote}`,
|
|
1484
|
+
};
|
|
1485
|
+
} catch (error) {
|
|
1486
|
+
return { text: `未改:${error instanceof Error ? error.message : String(error)}` };
|
|
1487
|
+
}
|
|
1488
|
+
},
|
|
1489
|
+
presentCall: () => ({ card: 'generic', title: '记忆钉住', kind: 'other' }),
|
|
1490
|
+
},
|
|
1491
|
+
];
|
|
1492
|
+
}
|