dsh-shiori 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -304
- package/package.json +1 -1
- package/src/host/seat.mjs +44 -3
- package/src/host/tools.mjs +598 -582
- package/src/sparks/delivery.mjs +4 -2
package/src/host/tools.mjs
CHANGED
|
@@ -1,582 +1,598 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shiori host bridge — 兼容工具(P0)。
|
|
3
|
-
*
|
|
4
|
-
* 覆盖 CONTRACTS.md 要求的最小集合:soul_read / soul_update、memory_read / search /
|
|
5
|
-
* append / rewrite、core_write、topic_append、memory_list_topics。
|
|
6
|
-
*
|
|
7
|
-
* 硬边界:
|
|
8
|
-
* - 读 payload 一律不接受 scope 参数(scope 只能来自宿主可信来源注入 service 的 factory);
|
|
9
|
-
* - 所有写都委托 service.mutate(即 B 的 commit),携带 expectedRevision 做 CAS,绝不绕过;
|
|
10
|
-
* - 旧工具(soul_update / memory_append / memory_rewrite / topic_append)没有 expectedRevision
|
|
11
|
-
* 入参:执行时先经 service 读当前版本,再以读到的真实 revision 发起 CAS(read-then-CAS),
|
|
12
|
-
* 不假造 revision。读与 CAS 之间的窗口由 B 的跨进程锁 + CAS 兜底(冲突即 REVISION_CONFLICT)。
|
|
13
|
-
* - persona 的独立 CAS 持久化由后续集成 service 实现;本模块只以 kind:"persona" 委托并
|
|
14
|
-
* 用测试 stub 验证(已知依赖,见 REPORT.md)。
|
|
15
|
-
* - 工具回执的投影状态恒为 "written-pending-projection"(已落盘、待下一次请求投影):
|
|
16
|
-
* 桥不预测下一次请求,不提前回报「已生效」。
|
|
17
|
-
* - 不静默截断:memory_read 返回全文,无 truncated 逻辑(与 soul-md 的 20000 字符截断不同,
|
|
18
|
-
* 属有意分歧,见 REPORT.md)。
|
|
19
|
-
*/
|
|
20
|
-
import { randomUUID } from "node:crypto";
|
|
21
|
-
import { INVALID_INPUT, UNSUPPORTED } from "./errors.mjs";
|
|
22
|
-
import { TOOL_OUTPUT_SCHEMAS, renderToolOutput } from "../plugin/tool-schemas.mjs";
|
|
23
|
-
import { assertNotHostLocked, validateReadonlyGuard } from "./readonly-guard.mjs";
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
},
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
const
|
|
323
|
-
const
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
const
|
|
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
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Shiori host bridge — 兼容工具(P0)。
|
|
3
|
+
*
|
|
4
|
+
* 覆盖 CONTRACTS.md 要求的最小集合:soul_read / soul_update、memory_read / search /
|
|
5
|
+
* append / rewrite、core_write、topic_append、memory_list_topics。
|
|
6
|
+
*
|
|
7
|
+
* 硬边界:
|
|
8
|
+
* - 读 payload 一律不接受 scope 参数(scope 只能来自宿主可信来源注入 service 的 factory);
|
|
9
|
+
* - 所有写都委托 service.mutate(即 B 的 commit),携带 expectedRevision 做 CAS,绝不绕过;
|
|
10
|
+
* - 旧工具(soul_update / memory_append / memory_rewrite / topic_append)没有 expectedRevision
|
|
11
|
+
* 入参:执行时先经 service 读当前版本,再以读到的真实 revision 发起 CAS(read-then-CAS),
|
|
12
|
+
* 不假造 revision。读与 CAS 之间的窗口由 B 的跨进程锁 + CAS 兜底(冲突即 REVISION_CONFLICT)。
|
|
13
|
+
* - persona 的独立 CAS 持久化由后续集成 service 实现;本模块只以 kind:"persona" 委托并
|
|
14
|
+
* 用测试 stub 验证(已知依赖,见 REPORT.md)。
|
|
15
|
+
* - 工具回执的投影状态恒为 "written-pending-projection"(已落盘、待下一次请求投影):
|
|
16
|
+
* 桥不预测下一次请求,不提前回报「已生效」。
|
|
17
|
+
* - 不静默截断:memory_read 返回全文,无 truncated 逻辑(与 soul-md 的 20000 字符截断不同,
|
|
18
|
+
* 属有意分歧,见 REPORT.md)。
|
|
19
|
+
*/
|
|
20
|
+
import { randomUUID } from "node:crypto";
|
|
21
|
+
import { INVALID_INPUT, UNSUPPORTED } from "./errors.mjs";
|
|
22
|
+
import { TOOL_OUTPUT_SCHEMAS, renderToolOutput } from "../plugin/tool-schemas.mjs";
|
|
23
|
+
import { assertNotHostLocked, validateReadonlyGuard } from "./readonly-guard.mjs";
|
|
24
|
+
|
|
25
|
+
/** 派生索引标题(= src/core/render.mjs 的 INDEX_HEADER)。正文与索引在读取时拼接,正文里不得自带。 */
|
|
26
|
+
const DERIVED_INDEX_HEADER = "## 主题记忆索引";
|
|
27
|
+
|
|
28
|
+
/** core 正文禁止自带派生索引:否则读取渲染时会再拼一份,索引重复。 */
|
|
29
|
+
function rejectDerivedIndex(content, toolName) {
|
|
30
|
+
if (typeof content === "string" && content.includes(DERIVED_INDEX_HEADER)) {
|
|
31
|
+
throw INVALID_INPUT(
|
|
32
|
+
`${toolName}: core body must not contain the derived topic index (found "${DERIVED_INDEX_HEADER}"); pass only the body, the index is generated on read`,
|
|
33
|
+
{ header: DERIVED_INDEX_HEADER },
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 宿主 system 的 section 名(与 soul-md 的 soul:persona/soul:memory 不同名,便于唯一写者判定)。 */
|
|
39
|
+
export const SECTION_PERSONA = "shiori:persona";
|
|
40
|
+
export const SECTION_CORE = "shiori:core";
|
|
41
|
+
|
|
42
|
+
/** config 缺省时的 persona 上限兜底;正式值以注入的已校验 config 为准。 */
|
|
43
|
+
const FALLBACK_PERSONA_MAX_CHARS = 12000;
|
|
44
|
+
|
|
45
|
+
function requireObject(args) {
|
|
46
|
+
if (args === undefined || args === null) return {};
|
|
47
|
+
if (typeof args !== "object" || Array.isArray(args)) {
|
|
48
|
+
throw INVALID_INPUT("tool arguments must be an object", { received: typeof args });
|
|
49
|
+
}
|
|
50
|
+
return args;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 读 payload 不接受任意 scope:schema 不暴露,payload 混入也拒绝。 */
|
|
54
|
+
function rejectScope(args) {
|
|
55
|
+
if (args.scope !== undefined) {
|
|
56
|
+
throw INVALID_INPUT("scope cannot be passed in tool payloads; scope comes from the trusted host factory", {});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function requireString(args, field, { allowEmpty = false } = {}) {
|
|
61
|
+
const value = args[field];
|
|
62
|
+
if (typeof value !== "string" || (!allowEmpty && value.trim().length === 0)) {
|
|
63
|
+
throw INVALID_INPUT(`\`${field}\` must be a non-empty string`, { field });
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function optionalString(args, field) {
|
|
69
|
+
const value = args[field];
|
|
70
|
+
if (value === undefined || value === null) return undefined;
|
|
71
|
+
if (typeof value !== "string") {
|
|
72
|
+
throw INVALID_INPUT(`\`${field}\` must be a string when present`, { field });
|
|
73
|
+
}
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function byteLen(text) {
|
|
78
|
+
return Buffer.byteLength(text, "utf8");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 旧 soul-md 工具的 topic 参数解析(与 memory-layout.js:27-31 normalizeMemoryTopic 对齐):
|
|
83
|
+
* 缺省/空 → core;`core` / `core.md`(大小写不敏感,trim 后)→ core;其余为 topic key。
|
|
84
|
+
* DESIGN.md §10:core、core.md 是保留别名,真实 topic key 不得与之冲突。
|
|
85
|
+
*/
|
|
86
|
+
export function resolveTopicArg(value) {
|
|
87
|
+
const topic = String(value ?? "").trim();
|
|
88
|
+
if (!topic || /^(core|core\.md)$/i.test(topic)) return null; // null = core 路径
|
|
89
|
+
return topic;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 与 soul-md memory_append 一致的日期标题格式。 */
|
|
93
|
+
export function datedHeading(section, date) {
|
|
94
|
+
const d = date ?? new Date();
|
|
95
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
96
|
+
const stamp = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
97
|
+
return `## ${stamp} — ${section}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 拼接追加块;空正文不加前导换行。 */
|
|
101
|
+
export function appendBlock(existingText, section, content, date) {
|
|
102
|
+
const block = `\n${datedHeading(section, date)}\n\n${content}\n`;
|
|
103
|
+
return existingText.length === 0 ? block.replace(/^\n/, "") : existingText + block;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 统一从 mutate 回执构造工具回执。
|
|
108
|
+
* 投影状态恒为 written-pending-projection:已落盘、待投影,绝不提前声称已生效。
|
|
109
|
+
*/
|
|
110
|
+
function writeReceipt(mutateResult, scopeFallback) {
|
|
111
|
+
const changes = mutateResult.changes ?? [];
|
|
112
|
+
const first = changes[0] ?? {};
|
|
113
|
+
return {
|
|
114
|
+
operationId: mutateResult.operationId,
|
|
115
|
+
generation: mutateResult.generation,
|
|
116
|
+
revision: first.afterRevision ?? null,
|
|
117
|
+
scope: first.scope ?? scopeFallback ?? null,
|
|
118
|
+
projection: "written-pending-projection",
|
|
119
|
+
changes,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 构造兼容工具定义。
|
|
125
|
+
* @param {object} deps
|
|
126
|
+
* @param {object} deps.service 见 docs/host/README.md 的 service 契约
|
|
127
|
+
* @param {() => {sessionId:string|null, scopeId:string|null}} [deps.getSessionInfo]
|
|
128
|
+
* @param {object} [deps.config] 已校验配置(persona.maxChars 等)
|
|
129
|
+
* @param {() => Date} [deps.now] 时钟注入
|
|
130
|
+
* @param {object} [deps.readonlyGuard] F7 只读守卫(createReadonlyGuard 产物;可选,
|
|
131
|
+
* 缺省时写行为与 0.1 完全一致)。外部写锁持续持有进入只读态后,本工厂的写工具
|
|
132
|
+
* (soul_update / memory_append / memory_rewrite / core_write / topic_append)
|
|
133
|
+
* 抛 code="HOST_LOCKED";读工具与 L0/L1 注入不受影响。
|
|
134
|
+
*/
|
|
135
|
+
export function createCompatTools({ service, getSessionInfo, config, now, readonlyGuard } = {}) {
|
|
136
|
+
if (!service || typeof service.mutate !== "function") {
|
|
137
|
+
throw INVALID_INPUT("service with mutate() is required", {});
|
|
138
|
+
}
|
|
139
|
+
validateReadonlyGuard(readonlyGuard);
|
|
140
|
+
const personaMaxChars = Number.isInteger(config?.persona?.maxChars) && config.persona.maxChars > 0
|
|
141
|
+
? config.persona.maxChars
|
|
142
|
+
: FALLBACK_PERSONA_MAX_CHARS;
|
|
143
|
+
const clock = now ?? (() => new Date());
|
|
144
|
+
|
|
145
|
+
async function scopeIdOf() {
|
|
146
|
+
try {
|
|
147
|
+
return getSessionInfo?.()?.scopeId ?? null;
|
|
148
|
+
} catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 核心写路径需要磁盘正文视图;渲染视图(含派生索引)绝不能回写。 */
|
|
154
|
+
async function readDiskCore() {
|
|
155
|
+
if (typeof service.readCore !== "function") {
|
|
156
|
+
throw UNSUPPORTED(
|
|
157
|
+
"core write paths require service.readCore() (disk body without the derived index); "
|
|
158
|
+
+ "the injected service does not provide it. Rendered views must never be written back.",
|
|
159
|
+
{},
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
return service.readCore();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const tools = [
|
|
166
|
+
// ── L0 persona ────────────────────────────────────────────────────────
|
|
167
|
+
{
|
|
168
|
+
name: "soul_read",
|
|
169
|
+
description: "Read the persona card active for the current scope, exactly as stored (Shiori bridge; delegates to the injected memory service).",
|
|
170
|
+
inputSchema: {},
|
|
171
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["soul_read"],
|
|
172
|
+
outputRender: renderToolOutput,
|
|
173
|
+
async execute(args) {
|
|
174
|
+
const a = requireObject(args);
|
|
175
|
+
rejectScope(a);
|
|
176
|
+
const persona = await service.readPersona();
|
|
177
|
+
return {
|
|
178
|
+
exists: persona?.name != null && persona?.text != null,
|
|
179
|
+
persona: persona?.name ?? "none",
|
|
180
|
+
content: persona?.text ?? "",
|
|
181
|
+
revision: persona?.revision ?? null,
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: "soul_update",
|
|
187
|
+
description: "Replace the active persona card with new content. CAS-protected: the bridge reads the current revision and commits through service.mutate; concurrent modifications are rejected instead of overwritten.",
|
|
188
|
+
inputSchema: {
|
|
189
|
+
content: { type: "string", required: true, description: "The complete new persona card (markdown)." },
|
|
190
|
+
operationId: { type: "string", description: "Idempotency key; reuse it verbatim on retry (P5)." },
|
|
191
|
+
},
|
|
192
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["soul_update"],
|
|
193
|
+
outputRender: renderToolOutput,
|
|
194
|
+
async execute(args) {
|
|
195
|
+
const a = requireObject(args);
|
|
196
|
+
rejectScope(a);
|
|
197
|
+
assertNotHostLocked(readonlyGuard);
|
|
198
|
+
const content = requireString(a, "content");
|
|
199
|
+
// DESIGN.md:110:L0 上限按 UTF-16 code units(content.length)计,UTF-8 bytes 仅作诊断。
|
|
200
|
+
if (!content.isWellFormed()) {
|
|
201
|
+
throw INVALID_INPUT("soul_update: content contains unpaired surrogates and would not survive lossless UTF-8 encoding", {});
|
|
202
|
+
}
|
|
203
|
+
if (content.length > personaMaxChars) {
|
|
204
|
+
throw INVALID_INPUT(`soul_update: content exceeds persona.maxChars (${personaMaxChars} UTF-16 chars)`, {
|
|
205
|
+
chars: content.length,
|
|
206
|
+
bytes: byteLen(content),
|
|
207
|
+
limit: personaMaxChars,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
const current = await service.readPersona();
|
|
211
|
+
const result = await service.mutate({
|
|
212
|
+
operationId: typeof a.operationId === "string" && a.operationId.length > 0 ? a.operationId : randomUUID(),
|
|
213
|
+
changes: [{
|
|
214
|
+
kind: "persona",
|
|
215
|
+
text: content,
|
|
216
|
+
expectedRevision: current?.revision ?? null,
|
|
217
|
+
}],
|
|
218
|
+
});
|
|
219
|
+
return writeReceipt(result, await scopeIdOf());
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
|
|
223
|
+
// ── L1 core 读取 ──────────────────────────────────────────────────────
|
|
224
|
+
{
|
|
225
|
+
name: "memory_read",
|
|
226
|
+
description: "Read long-term memory. Without arguments returns the complete rendered L1 view (core body + derived topic index). With a topic key returns that topic's full body. Scope is fixed by the host; it cannot be passed.",
|
|
227
|
+
inputSchema: {
|
|
228
|
+
topic: { type: "string", description: "Optional topic key. Omit or pass core/core.md for the rendered core view (legacy alias)." },
|
|
229
|
+
},
|
|
230
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["memory_read"],
|
|
231
|
+
outputRender: renderToolOutput,
|
|
232
|
+
async execute(args) {
|
|
233
|
+
const a = requireObject(args);
|
|
234
|
+
rejectScope(a);
|
|
235
|
+
const topicArg = optionalString(a, "topic");
|
|
236
|
+
const topic = resolveTopicArg(topicArg); // core / core.md / 空 → core 路径
|
|
237
|
+
if (topic === null) {
|
|
238
|
+
const view = await service.readCoreView();
|
|
239
|
+
const text = view?.text ?? "";
|
|
240
|
+
return {
|
|
241
|
+
exists: text.length > 0,
|
|
242
|
+
bytes: byteLen(text),
|
|
243
|
+
truncated: false,
|
|
244
|
+
source: view?.scopeId ?? null,
|
|
245
|
+
topic: "",
|
|
246
|
+
content: text,
|
|
247
|
+
revision: view?.revision ?? null,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
const t = await service.readTopic(topic);
|
|
251
|
+
return {
|
|
252
|
+
exists: true,
|
|
253
|
+
bytes: byteLen(t.body ?? ""),
|
|
254
|
+
truncated: false,
|
|
255
|
+
source: t.scopeId,
|
|
256
|
+
topic: t.key,
|
|
257
|
+
content: t.body ?? "",
|
|
258
|
+
revision: t.revision,
|
|
259
|
+
};
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
// ── L2/L3 检索 ────────────────────────────────────────────────────────
|
|
264
|
+
{
|
|
265
|
+
name: "memory_search",
|
|
266
|
+
description: "Keyword search over visible topics; archived (cold) topics are included by default but return summary-level entries only (state='archived', score=0, listed after active hits) — never full text; use memory_read(topic) for full text. Returns keys, titles and summaries only. Lexical search, not semantic matching.",
|
|
267
|
+
inputSchema: {
|
|
268
|
+
query: { type: "string", required: true, description: "Keywords or a short question." },
|
|
269
|
+
limit: { type: "integer", description: "Maximum results, 1–20 (default 5)." },
|
|
270
|
+
includeArchived: { type: "boolean", description: "Explicitly include archived (cold) spark cards; only affects spark cold cards — topic archives are always included as summary entries. Always read-only; archived cards are never revived or re-matched automatically." },
|
|
271
|
+
},
|
|
272
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["memory_search"],
|
|
273
|
+
outputRender: renderToolOutput,
|
|
274
|
+
async execute(args) {
|
|
275
|
+
const a = requireObject(args);
|
|
276
|
+
rejectScope(a);
|
|
277
|
+
const query = requireString(a, "query");
|
|
278
|
+
let limit = 5;
|
|
279
|
+
if (a.limit !== undefined) {
|
|
280
|
+
if (!Number.isInteger(a.limit) || a.limit < 1 || a.limit > 20) {
|
|
281
|
+
throw INVALID_INPUT("`limit` must be an integer between 1 and 20", { limit: a.limit });
|
|
282
|
+
}
|
|
283
|
+
limit = a.limit;
|
|
284
|
+
}
|
|
285
|
+
const matches = await service.search(query);
|
|
286
|
+
const result = { matches: Array.isArray(matches) ? matches.slice(0, limit) : [] };
|
|
287
|
+
if (a.includeArchived === true) {
|
|
288
|
+
if (typeof service.searchArchived !== "function") {
|
|
289
|
+
throw UNSUPPORTED(
|
|
290
|
+
"memory_search includeArchived requires service.searchArchived() (cold-store reader); "
|
|
291
|
+
+ "the injected service does not provide it. Archived cards are never auto-searched.",
|
|
292
|
+
{},
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
const archived = await service.searchArchived(query);
|
|
296
|
+
const merged = [
|
|
297
|
+
...result.matches,
|
|
298
|
+
...(Array.isArray(archived) ? archived : []).map((m) => ({ score: 0, ...m, state: "archived" })),
|
|
299
|
+
];
|
|
300
|
+
result.matches = merged.slice(0, limit);
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
|
|
306
|
+
// ── 追加 / 重写(旧工具:read-then-CAS)────────────────────────────────
|
|
307
|
+
{
|
|
308
|
+
name: "memory_append",
|
|
309
|
+
description: "Append a dated Markdown block to memory in the CURRENT scope. Omit topic for the always-visible core body; pass a topic key for that topic file. The append is committed through service.mutate with CAS.",
|
|
310
|
+
inputSchema: {
|
|
311
|
+
section: { type: "string", required: true, description: "Short heading for the entry." },
|
|
312
|
+
content: { type: "string", required: true, description: "The markdown text to remember." },
|
|
313
|
+
topic: { type: "string", description: "Optional topic key. Omit or pass core/core.md for the core body (legacy alias)." },
|
|
314
|
+
operationId: { type: "string", description: "Idempotency key; reuse it verbatim on retry (P5)." },
|
|
315
|
+
},
|
|
316
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["memory_append"],
|
|
317
|
+
outputRender: renderToolOutput,
|
|
318
|
+
async execute(args) {
|
|
319
|
+
const a = requireObject(args);
|
|
320
|
+
rejectScope(a);
|
|
321
|
+
assertNotHostLocked(readonlyGuard);
|
|
322
|
+
const section = requireString(a, "section");
|
|
323
|
+
const content = requireString(a, "content");
|
|
324
|
+
const topic = resolveTopicArg(optionalString(a, "topic")); // core / core.md / 空 → core 路径
|
|
325
|
+
const operationId = typeof a.operationId === "string" && a.operationId.length > 0 ? a.operationId : randomUUID();
|
|
326
|
+
if (topic === null) {
|
|
327
|
+
rejectDerivedIndex(content, "memory_append");
|
|
328
|
+
const current = await readDiskCore();
|
|
329
|
+
const candidate = appendBlock(current.text ?? "", section, content, clock());
|
|
330
|
+
const result = await service.mutate({
|
|
331
|
+
operationId,
|
|
332
|
+
changes: [{ kind: "core", text: candidate, expectedRevision: current.revision }],
|
|
333
|
+
});
|
|
334
|
+
return writeReceipt(result, current.scopeId);
|
|
335
|
+
}
|
|
336
|
+
const current = await service.readTopic(topic);
|
|
337
|
+
const candidate = appendBlock(current.body ?? "", section, content, clock());
|
|
338
|
+
const result = await service.mutate({
|
|
339
|
+
operationId,
|
|
340
|
+
changes: [{
|
|
341
|
+
kind: "topic",
|
|
342
|
+
key: current.key,
|
|
343
|
+
text: candidate,
|
|
344
|
+
expectedRevision: current.revision,
|
|
345
|
+
topic: { title: current.title, summary: current.summary },
|
|
346
|
+
}],
|
|
347
|
+
});
|
|
348
|
+
return writeReceipt(result, current.scopeId);
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
name: "memory_rewrite",
|
|
353
|
+
description: "Replace one memory body in the CURRENT scope: omit topic for the core body, or pass a topic key to replace that topic's body (topic front-matter metadata is preserved by the service). Build the new content from memory_read output unless you deliberately drop entries.",
|
|
354
|
+
inputSchema: {
|
|
355
|
+
content: { type: "string", required: true, description: "The new full body content (markdown)." },
|
|
356
|
+
topic: { type: "string", description: "Optional topic key. Omit or pass core/core.md for the core body (legacy alias)." },
|
|
357
|
+
operationId: { type: "string", description: "Idempotency key; reuse it verbatim on retry (P5)." },
|
|
358
|
+
},
|
|
359
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["memory_rewrite"],
|
|
360
|
+
outputRender: renderToolOutput,
|
|
361
|
+
async execute(args) {
|
|
362
|
+
const a = requireObject(args);
|
|
363
|
+
rejectScope(a);
|
|
364
|
+
assertNotHostLocked(readonlyGuard);
|
|
365
|
+
const content = requireString(a, "content", { allowEmpty: true });
|
|
366
|
+
const topic = resolveTopicArg(optionalString(a, "topic")); // core / core.md / 空 → core 路径
|
|
367
|
+
const operationId = typeof a.operationId === "string" && a.operationId.length > 0 ? a.operationId : randomUUID();
|
|
368
|
+
if (topic === null) {
|
|
369
|
+
rejectDerivedIndex(content, "memory_rewrite");
|
|
370
|
+
const current = await readDiskCore();
|
|
371
|
+
const result = await service.mutate({
|
|
372
|
+
operationId,
|
|
373
|
+
changes: [{ kind: "core", text: content, expectedRevision: current.revision }],
|
|
374
|
+
});
|
|
375
|
+
return writeReceipt(result, current.scopeId);
|
|
376
|
+
}
|
|
377
|
+
const current = await service.readTopic(topic);
|
|
378
|
+
const result = await service.mutate({
|
|
379
|
+
operationId,
|
|
380
|
+
changes: [{
|
|
381
|
+
kind: "topic",
|
|
382
|
+
key: current.key,
|
|
383
|
+
text: content,
|
|
384
|
+
expectedRevision: current.revision,
|
|
385
|
+
topic: { title: current.title, summary: current.summary },
|
|
386
|
+
}],
|
|
387
|
+
});
|
|
388
|
+
return writeReceipt(result, current.scopeId);
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
|
|
392
|
+
// ── 新式写工具(expectedRevision 必须显式给出)─────────────────────────
|
|
393
|
+
{
|
|
394
|
+
name: "core_write",
|
|
395
|
+
description: "Replace the disk core body (L1, without the derived index). Pass the opaque revision from memory_read(); null only when no visible core exists. Global fallback is validated before an active-scope copy is created. The service enforces the complete rendered L1 budget.",
|
|
396
|
+
inputSchema: {
|
|
397
|
+
content: { type: "string", required: true, description: "The new core body (markdown)." },
|
|
398
|
+
expectedRevision: {
|
|
399
|
+
oneOf: [{ type: "string" }, { type: "null" }],
|
|
400
|
+
required: true,
|
|
401
|
+
description: "Opaque revision returned by memory_read() or the service. Do not parse or construct it. Null is allowed only when no visible core exists.",
|
|
402
|
+
},
|
|
403
|
+
operationId: { type: "string", description: "Client-supplied operation id for idempotent retry." },
|
|
404
|
+
section: { type: "string", description: "NOT supported in P0; must be omitted." },
|
|
405
|
+
},
|
|
406
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["core_write"],
|
|
407
|
+
outputRender: renderToolOutput,
|
|
408
|
+
async execute(args) {
|
|
409
|
+
const a = requireObject(args);
|
|
410
|
+
rejectScope(a);
|
|
411
|
+
assertNotHostLocked(readonlyGuard);
|
|
412
|
+
const content = requireString(a, "content", { allowEmpty: true });
|
|
413
|
+
const section = optionalString(a, "section");
|
|
414
|
+
if (section !== undefined && section.length > 0) {
|
|
415
|
+
throw UNSUPPORTED("core_write(section) is not supported in P0; rewrite the whole body instead", { section });
|
|
416
|
+
}
|
|
417
|
+
rejectDerivedIndex(content, "core_write");
|
|
418
|
+
if (!("expectedRevision" in a)) {
|
|
419
|
+
throw INVALID_INPUT("core_write requires explicit expectedRevision (string, or null for first creation)", {});
|
|
420
|
+
}
|
|
421
|
+
const expectedRevision = a.expectedRevision;
|
|
422
|
+
if (expectedRevision !== null && typeof expectedRevision !== "string") {
|
|
423
|
+
throw INVALID_INPUT("`expectedRevision` must be a string or null", { expectedRevision });
|
|
424
|
+
}
|
|
425
|
+
const operationId = typeof a.operationId === "string" && a.operationId.length > 0 ? a.operationId : randomUUID();
|
|
426
|
+
const result = await service.mutate({
|
|
427
|
+
operationId,
|
|
428
|
+
changes: [{ kind: "core", text: content, expectedRevision }],
|
|
429
|
+
});
|
|
430
|
+
return writeReceipt(result, await scopeIdOf());
|
|
431
|
+
},
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
name: "topic_append",
|
|
435
|
+
description: "Append a dated section to an L3 topic body. The topic must already exist (P0 does not create topics). Committed through service.mutate with CAS.",
|
|
436
|
+
inputSchema: {
|
|
437
|
+
topic: { type: "string", required: true, description: "Topic key." },
|
|
438
|
+
section: { type: "string", required: true, description: "Short heading for the appended section." },
|
|
439
|
+
content: { type: "string", required: true, description: "The markdown text to append." },
|
|
440
|
+
operationId: { type: "string", description: "Idempotency key; reuse it verbatim on retry (P5)." },
|
|
441
|
+
},
|
|
442
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["topic_append"],
|
|
443
|
+
outputRender: renderToolOutput,
|
|
444
|
+
async execute(args) {
|
|
445
|
+
const a = requireObject(args);
|
|
446
|
+
rejectScope(a);
|
|
447
|
+
const topicArg = requireString(a, "topic");
|
|
448
|
+
if (resolveTopicArg(topicArg) === null) {
|
|
449
|
+
throw INVALID_INPUT(
|
|
450
|
+
`topic_append: "${topicArg}" is a reserved core alias; use core_write or memory_append/rewrite without a topic for the core body`,
|
|
451
|
+
{ topic: topicArg },
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
const topic = topicArg;
|
|
455
|
+
const section = requireString(a, "section");
|
|
456
|
+
const content = requireString(a, "content");
|
|
457
|
+
assertNotHostLocked(readonlyGuard);
|
|
458
|
+
const current = await service.readTopic(topic);
|
|
459
|
+
const candidate = appendBlock(current.body ?? "", section, content, clock());
|
|
460
|
+
const result = await service.mutate({
|
|
461
|
+
operationId: typeof a.operationId === "string" && a.operationId.length > 0 ? a.operationId : randomUUID(),
|
|
462
|
+
changes: [{
|
|
463
|
+
kind: "topic",
|
|
464
|
+
key: current.key,
|
|
465
|
+
text: candidate,
|
|
466
|
+
expectedRevision: current.revision,
|
|
467
|
+
topic: { title: current.title, summary: current.summary },
|
|
468
|
+
}],
|
|
469
|
+
});
|
|
470
|
+
return writeReceipt(result, current.scopeId);
|
|
471
|
+
},
|
|
472
|
+
},
|
|
473
|
+
|
|
474
|
+
// ── L3 目录枚举 ───────────────────────────────────────────────────────
|
|
475
|
+
{
|
|
476
|
+
name: "memory_list_topics",
|
|
477
|
+
description: "List visible topics with size (characters and UTF-8 bytes of the body) for consolidation workflows. Does not return bodies; use memory_read(topic).",
|
|
478
|
+
inputSchema: {},
|
|
479
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["memory_list_topics"],
|
|
480
|
+
outputRender: renderToolOutput,
|
|
481
|
+
async execute(args) {
|
|
482
|
+
const a = requireObject(args);
|
|
483
|
+
rejectScope(a);
|
|
484
|
+
const topics = await service.listTopics();
|
|
485
|
+
return {
|
|
486
|
+
topics: (Array.isArray(topics) ? topics : []).map((t) => ({
|
|
487
|
+
key: t.key,
|
|
488
|
+
title: t.title,
|
|
489
|
+
summary: t.summary,
|
|
490
|
+
scopeId: t.scopeId,
|
|
491
|
+
revision: t.revision,
|
|
492
|
+
updated: t.updated,
|
|
493
|
+
chars: (t.body ?? "").length,
|
|
494
|
+
bytes: byteLen(t.body ?? ""),
|
|
495
|
+
})),
|
|
496
|
+
};
|
|
497
|
+
},
|
|
498
|
+
},
|
|
499
|
+
];
|
|
500
|
+
|
|
501
|
+
return tools;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* P5 topic_create 工具(独立工厂:保持既有 createCompatTools 恒为 9 个兼容工具,
|
|
506
|
+
* controller 负责把 topic_create 与 6 个 Spark 工具一起追加成 16 工具全集)。
|
|
507
|
+
* 创建走 service.mutate(kind:topic + expectedRevision null):派生目录 / 索引
|
|
508
|
+
* 增长 / 完整 L1 预算由底层 policy 在候选快照上统一裁决,绝不绕 policy 直接 fs 写。
|
|
509
|
+
*
|
|
510
|
+
* @param {object} deps
|
|
511
|
+
* @param {object} deps.service 见 docs/host/README.md 的 service 契约
|
|
512
|
+
* @param {() => {sessionId:string|null, scopeId:string|null}} [deps.getSessionInfo]
|
|
513
|
+
* @param {() => Date} [deps.now] 时钟注入
|
|
514
|
+
* @param {object} [deps.readonlyGuard] F7 只读守卫(可选;只读态下 topic_create 抛 HOST_LOCKED)
|
|
515
|
+
*/
|
|
516
|
+
export function createTopicCreateTool({ service, getSessionInfo, now, readonlyGuard } = {}) {
|
|
517
|
+
if (!service || typeof service.mutate !== "function") {
|
|
518
|
+
throw INVALID_INPUT("service with mutate() is required", {});
|
|
519
|
+
}
|
|
520
|
+
validateReadonlyGuard(readonlyGuard);
|
|
521
|
+
const clock = now ?? (() => new Date());
|
|
522
|
+
|
|
523
|
+
async function scopeIdOf() {
|
|
524
|
+
try {
|
|
525
|
+
return getSessionInfo?.()?.scopeId ?? null;
|
|
526
|
+
} catch {
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return {
|
|
532
|
+
name: "topic_create",
|
|
533
|
+
description: "Create a new L3 topic in the CURRENT scope with title and summary (front-matter metadata; the body starts empty). If a topic with the same key exists in the active scope this is a conflict; if it only exists in global fallback an active-scope shadow is created so the active card gets its own copy. The complete visible L1 budget (index growth) is validated by the service before commit.",
|
|
534
|
+
inputSchema: {
|
|
535
|
+
topic: { type: "string", required: true, description: "Topic key (must be a safe filename; 'core' is reserved)." },
|
|
536
|
+
title: { type: "string", required: true, description: "Human-readable topic title." },
|
|
537
|
+
summary: { type: "string", required: true, description: "One-line summary for the derived L1 index (single line)." },
|
|
538
|
+
operationId: { type: "string", description: "Idempotency key; reuse it verbatim on retry." },
|
|
539
|
+
},
|
|
540
|
+
outputSchema: TOOL_OUTPUT_SCHEMAS["topic_create"],
|
|
541
|
+
outputRender: renderToolOutput,
|
|
542
|
+
async execute(args) {
|
|
543
|
+
const a = requireObject(args);
|
|
544
|
+
rejectScope(a);
|
|
545
|
+
assertNotHostLocked(readonlyGuard);
|
|
546
|
+
const topicArg = requireString(a, "topic");
|
|
547
|
+
if (resolveTopicArg(topicArg) === null) {
|
|
548
|
+
throw INVALID_INPUT(
|
|
549
|
+
`topic_create: "${topicArg}" is a reserved core alias; core is not a creatable topic`,
|
|
550
|
+
{ topic: topicArg },
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
const title = requireString(a, "title");
|
|
554
|
+
const summary = requireString(a, "summary");
|
|
555
|
+
if (summary.includes("\n") || summary.includes("\r")) {
|
|
556
|
+
throw INVALID_INPUT("topic_create: `summary` must be a single line (it feeds the derived L1 index)", {});
|
|
557
|
+
}
|
|
558
|
+
if (title.length > 200) {
|
|
559
|
+
throw INVALID_INPUT("topic_create: `title` must be at most 200 UTF-16 chars", { length: title.length });
|
|
560
|
+
}
|
|
561
|
+
const operationId = typeof a.operationId === "string" && a.operationId.length > 0 ? a.operationId : randomUUID();
|
|
562
|
+
// 可见性探测:active 已存在 → conflict;仅 global 同 key → active shadow;
|
|
563
|
+
// 都不存在 → 新建。创建走 service.mutate(kind:topic + expectedRevision null),
|
|
564
|
+
// 派生目录 / 索引增长 / 完整 L1 预算由底层 policy 在候选快照上统一裁决。
|
|
565
|
+
let visible = null;
|
|
566
|
+
try {
|
|
567
|
+
visible = await service.readTopic(topicArg);
|
|
568
|
+
} catch (error) {
|
|
569
|
+
if (error?.code !== "NOT_FOUND") throw error;
|
|
570
|
+
}
|
|
571
|
+
const alreadyActive = visible !== null && visible.scopeId === (await scopeIdOf());
|
|
572
|
+
if (alreadyActive) {
|
|
573
|
+
throw INVALID_INPUT(
|
|
574
|
+
`topic_create: topic "${topicArg}" already exists in the active scope (use topic_append / memory_rewrite instead)`,
|
|
575
|
+
{ topic: topicArg, scope: visible.scopeId },
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
const updated = clock().toISOString();
|
|
579
|
+
const result = await service.mutate({
|
|
580
|
+
operationId,
|
|
581
|
+
changes: [{
|
|
582
|
+
kind: "topic",
|
|
583
|
+
key: topicArg,
|
|
584
|
+
expectedRevision: null, // active scope 内创建(store 对新文件执行 null CAS)
|
|
585
|
+
text: "",
|
|
586
|
+
topic: { title, summary, updated },
|
|
587
|
+
}],
|
|
588
|
+
});
|
|
589
|
+
const receipt = writeReceipt(result, await scopeIdOf());
|
|
590
|
+
return {
|
|
591
|
+
...receipt,
|
|
592
|
+
created: true,
|
|
593
|
+
alreadyExists: false,
|
|
594
|
+
topic: topicArg,
|
|
595
|
+
};
|
|
596
|
+
},
|
|
597
|
+
};
|
|
598
|
+
};
|