dsh-prime-memory 0.18.4 → 0.18.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/dist/client.js +1297 -159
- package/dist/contract.d.ts +109 -1
- package/dist/hooks/capture.js +6 -2
- package/dist/hooks/recall.d.ts +4 -1
- package/dist/hooks/recall.js +10 -3
- package/dist/index.js +14 -4
- package/dist/metadata-validators.d.ts +5 -0
- package/dist/metadata-validators.js +11 -1
- package/dist/pipeline/ruminate.d.ts +11 -1
- package/dist/pipeline/ruminate.js +18 -1
- package/dist/room-review.d.ts +196 -0
- package/dist/room-review.js +281 -0
- package/dist/stats.d.ts +26 -2
- package/dist/stats.js +261 -13
- package/dist/store/l1.d.ts +34 -0
- package/dist/store/l1.js +68 -1
- package/dist/store/recall-usage.d.ts +43 -0
- package/dist/store/recall-usage.js +149 -0
- package/dist/store/rooms-registry.d.ts +66 -0
- package/dist/store/rooms-registry.js +250 -0
- package/dist/store/sqlite.d.ts +4 -0
- package/dist/store/sqlite.js +20 -0
- package/dist/tools/index.d.ts +4 -1
- package/dist/tools/index.js +224 -1
- package/dist/types.d.ts +4 -0
- package/package.json +13 -12
- package/dist/hall-backfill.d.ts +0 -33
- package/dist/hall-backfill.js +0 -95
- package/dist/hall-migrate.d.ts +0 -32
- package/dist/hall-migrate.js +0 -88
package/dist/tools/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { RECEIPTS_QUERY_LIMIT_MAX, dimensionOf, toReceiptView } from '../store/r
|
|
|
3
3
|
import { listConflictPairs, renderConflictResolution, renderConflicts, resolveConflictPair } from '../conflict-service.js';
|
|
4
4
|
import { WING_CATALOG, WING_FALLBACK, normPersistence, normScope, resolveRecordScope } from '../types.js';
|
|
5
5
|
import { scopeFilterOf, workspaceIdOf } from '../workspace.js';
|
|
6
|
+
import { annotateOrphanCandidates, confirmReview, mergeRoom, nextReview, pendingReviewCount, renameRoom, skipReview, } from '../room-review.js';
|
|
6
7
|
import { GRAPH_STATUS_LABELS } from '../prompts/graph-projection.js';
|
|
7
8
|
import { redactSecrets } from '../util/redact.js';
|
|
8
9
|
const OFF_NOTICE = '本会话的记忆档位为"关闭":该会话对记忆系统完全隐身,不读取也不写入记忆。';
|
|
@@ -30,7 +31,9 @@ function applyManualRedaction(record, cfg, logger) {
|
|
|
30
31
|
}
|
|
31
32
|
export function registerMemoryTools(ctx, cfg, stores, logger, modes, live,
|
|
32
33
|
/** 反刍控制器(可选:未装配时 ruminate 工具返回未启用提示)。 */
|
|
33
|
-
ruminate
|
|
34
|
+
ruminate,
|
|
35
|
+
/** Room 注册表(分类管理 beta.4;缺省 = memory_room_admin 返回未装配提示)。 */
|
|
36
|
+
roomRegistry) {
|
|
34
37
|
if (!cfg.tools)
|
|
35
38
|
return;
|
|
36
39
|
/**
|
|
@@ -168,6 +171,145 @@ ruminate) {
|
|
|
168
171
|
};
|
|
169
172
|
},
|
|
170
173
|
}));
|
|
174
|
+
// ── memory_room_admin: Room 粒度目录治理(分类管理 beta.4;beta.5 起 merge/rename/retire 已注册) ──
|
|
175
|
+
// list 免预算零风险;merge/rename/retire 走 dryRun 预览→实跑,执行前自动备份,完成入队场景重算。
|
|
176
|
+
ctx.tools.register(defineTool({
|
|
177
|
+
name: 'memory_room_admin',
|
|
178
|
+
description: 'Room 分类管理(目录粒度)。action="list" 列出注册表条目与自生长 Room 计数;' +
|
|
179
|
+
'action="register" 预注册一个 Room(高权限;两级制——大类上限 200,小类用「大类/小类」细分不占额度,如 dsh-plugin/merge;' +
|
|
180
|
+
'action="merge" 合并两个 Room(高权限;dryRun 默认 true 只返预览,实跑改写全部匹配记录的 tags 并入队场景重算,执行前自动备份);' +
|
|
181
|
+
'action="rename" 改名(= merge 1:1 + 注册表改名);action="retire" 退役一个注册 Room(高权限;存量记录不动)。' +
|
|
182
|
+
'merge/rename 与反刍并发会相互覆盖,请在反刍空闲时执行。',
|
|
183
|
+
parameters: {
|
|
184
|
+
action: { type: 'string', required: true, description: 'list | register(高权限) | merge(高权限) | rename(高权限) | retire(高权限)' },
|
|
185
|
+
slug: { type: 'string', description: 'register 的 Room slug(小写字母数字连字符)' },
|
|
186
|
+
label: { type: 'string', description: 'register 的人类可读名(可选,可中文)' },
|
|
187
|
+
description: { type: 'string', description: 'register 的归类说明(可选;喂给标注器帮助归类)' },
|
|
188
|
+
from: { type: 'string', description: 'merge/rename 的源 Room slug' },
|
|
189
|
+
to: { type: 'string', description: 'merge/rename 的目标 Room slug' },
|
|
190
|
+
dryRun: { type: 'string', description: 'merge/rename:默认 true 只返预览;"false" 才实际执行' },
|
|
191
|
+
},
|
|
192
|
+
output: {
|
|
193
|
+
schema: {
|
|
194
|
+
type: 'object',
|
|
195
|
+
properties: {
|
|
196
|
+
notice: { type: 'string' },
|
|
197
|
+
rooms: { type: 'string' },
|
|
198
|
+
slug: { type: 'string' },
|
|
199
|
+
created: { type: 'boolean' },
|
|
200
|
+
},
|
|
201
|
+
additionalProperties: false,
|
|
202
|
+
},
|
|
203
|
+
render: (_args, value) => [{ type: 'text', text: value.notice ?? JSON.stringify(value) }],
|
|
204
|
+
},
|
|
205
|
+
execute: async (args) => {
|
|
206
|
+
const action = String(args.action ?? '');
|
|
207
|
+
if (action === 'list') {
|
|
208
|
+
if (!roomRegistry)
|
|
209
|
+
return { notice: 'Room 注册表未装配(需要插件重载以初始化)。' };
|
|
210
|
+
const registry = roomRegistry.list();
|
|
211
|
+
const counts = stores.l1.listRooms();
|
|
212
|
+
const countOf = (slug) => counts.find((r) => r.room === slug)?.count ?? 0;
|
|
213
|
+
const regLines = registry.map((e) => `- ${e.slug}${e.label ? `(${e.label})` : ''} [${e.source}/${e.status}] 现有 ${countOf(e.slug)} 条${e.description ? ` — ${e.description}` : ''}`);
|
|
214
|
+
const grownOnly = counts.filter((r) => !registry.some((e) => e.slug === r.room));
|
|
215
|
+
return {
|
|
216
|
+
notice: `注册表 ${registry.length} 条(上限 200),自生长未注册 ${grownOnly.length} 个。\n` +
|
|
217
|
+
(regLines.join('\n') || '(注册表为空——用 action=register 预注册)') +
|
|
218
|
+
`\n自生长未注册:${grownOnly.map((r) => `${r.room}(${r.count})`).join(", ") || "无"}`,
|
|
219
|
+
rooms: `${registry.length}+${grownOnly.length}`,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (action === 'register') {
|
|
223
|
+
if (!roomRegistry)
|
|
224
|
+
return { notice: 'Room 注册表未装配(需要插件重载以初始化)。' };
|
|
225
|
+
if (!live.get().memoryMutate)
|
|
226
|
+
return { notice: MUTATE_OFF_NOTICE };
|
|
227
|
+
const slug = String(args.slug ?? '').trim();
|
|
228
|
+
try {
|
|
229
|
+
const r = await roomRegistry.register({
|
|
230
|
+
slug,
|
|
231
|
+
label: typeof args.label === 'string' ? args.label : undefined,
|
|
232
|
+
description: typeof args.description === 'string' ? args.description : undefined,
|
|
233
|
+
});
|
|
234
|
+
return {
|
|
235
|
+
notice: r.created
|
|
236
|
+
? `已预注册 Room:${r.entry.slug}${r.entry.label ? `(${r.entry.label})` : ''}。候选标注器将优先把相关记忆挂到它上面。`
|
|
237
|
+
: `Room 已存在:${r.entry.slug}(本次只补全了缺失的 label/description)。`,
|
|
238
|
+
slug: r.entry.slug,
|
|
239
|
+
created: r.created,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
catch (err) {
|
|
243
|
+
return { notice: `注册失败: ${err instanceof Error ? err.message : String(err)}` };
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (action === 'update') {
|
|
247
|
+
if (!roomRegistry)
|
|
248
|
+
return { notice: 'Room 注册表未装配(需要插件重载以初始化)。' };
|
|
249
|
+
if (!live.get().memoryMutate)
|
|
250
|
+
return { notice: MUTATE_OFF_NOTICE };
|
|
251
|
+
const slug = String(args.slug ?? '').trim();
|
|
252
|
+
if (!slug)
|
|
253
|
+
return { notice: 'update 需要 slug。' };
|
|
254
|
+
try {
|
|
255
|
+
const ok = await roomRegistry.update(slug, {
|
|
256
|
+
label: typeof args.label === 'string' ? args.label : undefined,
|
|
257
|
+
description: typeof args.description === 'string' ? args.description : undefined,
|
|
258
|
+
});
|
|
259
|
+
return { notice: ok ? `已更新:${slug}(显示名/归类说明)。` : `无变化:${slug} 未找到或未提供非空字段。`, slug };
|
|
260
|
+
}
|
|
261
|
+
catch (err) {
|
|
262
|
+
return { notice: `更新失败: ${err instanceof Error ? err.message : String(err)}` };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (action === 'merge' || action === 'rename') {
|
|
266
|
+
if (!roomRegistry)
|
|
267
|
+
return { notice: 'Room 注册表未装配(需要插件重载以初始化)。' };
|
|
268
|
+
if (!live.get().memoryMutate)
|
|
269
|
+
return { notice: MUTATE_OFF_NOTICE };
|
|
270
|
+
const from = String(args.from ?? '').trim();
|
|
271
|
+
const to = String(args.to ?? '').trim();
|
|
272
|
+
if (!from || !to)
|
|
273
|
+
return { notice: 'merge/rename 需要 from 与 to 两个 slug。' };
|
|
274
|
+
const dryRun = String(args.dryRun ?? 'true').toLowerCase() !== 'false';
|
|
275
|
+
try {
|
|
276
|
+
const fn = action === 'merge' ? mergeRoom : renameRoom;
|
|
277
|
+
const r = await fn({ l1: stores.l1, registry: roomRegistry, logger }, from, to, { dryRun });
|
|
278
|
+
if (r.dryRun) {
|
|
279
|
+
return {
|
|
280
|
+
notice: `[dryRun 预览] ${from} → ${to}:影响 ${r.affected} 条${r.hasMore ? '(超过单次上限,需续跑)' : ''}。` +
|
|
281
|
+
`样例:${r.preview.join(', ') || '无'}。确认无误后以 dryRun="false" 执行(执行前自动备份,重写含已退场行,完成后自动入队场景重算)。`,
|
|
282
|
+
affected: r.affected,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
notice: `${action === 'merge' ? '合并' : '改名'}完成:${from} → ${to},重写 ${r.applied} 条` +
|
|
287
|
+
`(备份:${r.backupFile ?? '无'})。`,
|
|
288
|
+
applied: r.applied,
|
|
289
|
+
id: to,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
catch (err) {
|
|
293
|
+
return { notice: `${action} 失败: ${err instanceof Error ? err.message : String(err)}` };
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (action === 'retire') {
|
|
297
|
+
if (!roomRegistry)
|
|
298
|
+
return { notice: 'Room 注册表未装配(需要插件重载以初始化)。' };
|
|
299
|
+
if (!live.get().memoryMutate)
|
|
300
|
+
return { notice: MUTATE_OFF_NOTICE };
|
|
301
|
+
const slug = String(args.slug ?? '').trim();
|
|
302
|
+
try {
|
|
303
|
+
const ok = await roomRegistry.setStatus(slug, 'retired');
|
|
304
|
+
return { notice: ok ? `已退役 Room:${slug}(存量记录的 tags 不动,仅词表不再推荐)。` : `未找到注册条目:${slug}(自生长 Room 无需退役,会随 tags 自然消失)。`, slug };
|
|
305
|
+
}
|
|
306
|
+
catch (err) {
|
|
307
|
+
return { notice: `退役失败: ${err instanceof Error ? err.message : String(err)}` };
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return { notice: `非法 action:${action}(允许 list/register/merge/rename/retire)。` };
|
|
311
|
+
},
|
|
312
|
+
}));
|
|
171
313
|
// ── conversation_search: L0 原始对话 ──
|
|
172
314
|
ctx.tools.register(defineTool({
|
|
173
315
|
name: 'conversation_search',
|
|
@@ -764,6 +906,87 @@ ruminate) {
|
|
|
764
906
|
},
|
|
765
907
|
}));
|
|
766
908
|
// ── 反刍状态查询(查看当前反刍进度) ──
|
|
909
|
+
// ── memory_room_review:孤儿记忆的 Room 候选逐个复查(agent 长尾治理)──────
|
|
910
|
+
// next/annotate 只读或预算内标注(无需门);confirm/skip 改写 metadata,
|
|
911
|
+
// confirm 吃 live.memoryMutate 高权限门(与 memory_delete 同款)。
|
|
912
|
+
ctx.tools.register(defineTool({
|
|
913
|
+
name: 'memory_room_review',
|
|
914
|
+
description: '孤儿记忆(无 Room 绑定)的逐个复查:action="next" 取下一条待复查记录(带系统预标记的候选 Room 与现有词表,agent 依据记录内容判定);' +
|
|
915
|
+
'action="confirm" 按 id 把 1-3 个 Room slug 写入该记忆(高权限);action="skip" 跳过(不再进队列);' +
|
|
916
|
+
'action="annotate" 立即对一批孤儿做候选预标记(平时由反刍自动推进)。建议长会话空闲时逐个过,保持分类收敛——优先挂靠现有 Room。',
|
|
917
|
+
parameters: {
|
|
918
|
+
action: { type: 'string', required: true, description: 'next(取下一条)| confirm(确认写入)| skip(跳过)| annotate(批量预标记)' },
|
|
919
|
+
id: { type: 'string', description: 'confirm/skip 的目标记录 id(action=next 返回的 id)' },
|
|
920
|
+
rooms: { type: 'string', description: 'confirm 的 Room slug 列表(逗号分隔,1-3 个,小写字母数字连字符;可含词表外新 slug)' },
|
|
921
|
+
},
|
|
922
|
+
output: {
|
|
923
|
+
schema: {
|
|
924
|
+
type: 'object',
|
|
925
|
+
properties: {
|
|
926
|
+
notice: { type: 'string' },
|
|
927
|
+
id: { type: 'string' },
|
|
928
|
+
content: { type: 'string' },
|
|
929
|
+
candidates: { type: 'string' },
|
|
930
|
+
roomCatalog: { type: 'string' },
|
|
931
|
+
remaining: { type: 'number' },
|
|
932
|
+
confirmedTags: { type: 'string' },
|
|
933
|
+
annotated: { type: 'number' },
|
|
934
|
+
deferred: { type: 'number' },
|
|
935
|
+
},
|
|
936
|
+
additionalProperties: false,
|
|
937
|
+
},
|
|
938
|
+
render: (_args, value) => [{ type: 'text', text: value.notice ?? JSON.stringify(value) }],
|
|
939
|
+
},
|
|
940
|
+
execute: async (args) => {
|
|
941
|
+
const action = String(args.action ?? '');
|
|
942
|
+
const io = { l1: stores.l1, logger };
|
|
943
|
+
if (action === 'next') {
|
|
944
|
+
const item = nextReview(io.l1);
|
|
945
|
+
if (!item)
|
|
946
|
+
return { notice: '没有待复查的孤儿记忆(roomCandidates 队列为空)。可用 action=annotate 先做一轮候选预标记。' };
|
|
947
|
+
return {
|
|
948
|
+
id: item.id,
|
|
949
|
+
content: item.content,
|
|
950
|
+
candidates: item.candidates.join(', '),
|
|
951
|
+
roomCatalog: item.roomCatalog.map((r) => `${r.room}(${r.count})`).join(', '),
|
|
952
|
+
remaining: item.remaining,
|
|
953
|
+
};
|
|
954
|
+
}
|
|
955
|
+
if (action === 'annotate') {
|
|
956
|
+
const r = await annotateOrphanCandidates(ctx, cfg, { ...io, registry: roomRegistry }, 40);
|
|
957
|
+
return {
|
|
958
|
+
notice: `候选预标记完成:选中 ${r.selected} 条,产候选 ${r.candidates} 条,写 pending ${r.written} 条。用 action=next 逐个复查。`,
|
|
959
|
+
annotated: r.candidates,
|
|
960
|
+
deferred: r.selected - r.written,
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
if (action === 'confirm') {
|
|
964
|
+
if (!live.get().memoryMutate)
|
|
965
|
+
return { notice: MUTATE_OFF_NOTICE };
|
|
966
|
+
const id = String(args.id ?? '').trim();
|
|
967
|
+
if (!id)
|
|
968
|
+
return { notice: 'confirm 需要 id(action=next 返回的 id)。' };
|
|
969
|
+
const rooms = String(args.rooms ?? '')
|
|
970
|
+
.split(/[,,、]/)
|
|
971
|
+
.map((x) => x.trim())
|
|
972
|
+
.filter(Boolean);
|
|
973
|
+
if (rooms.length === 0)
|
|
974
|
+
return { notice: 'confirm 需要 rooms(逗号分隔的 1-3 个 Room slug)。' };
|
|
975
|
+
const r = confirmReview(io.l1, id, rooms);
|
|
976
|
+
if (!r)
|
|
977
|
+
return { notice: `确认失败:id=${id} 不存在、已被确认过、或 rooms 全部非法(需 1-3 个合法 slug)。` };
|
|
978
|
+
return { notice: `已挂到 Room:${r.tags.join(', ')}(面板下一次刷新即可见)。`, confirmedTags: r.tags.join(', '), id };
|
|
979
|
+
}
|
|
980
|
+
if (action === 'skip') {
|
|
981
|
+
const id = String(args.id ?? '').trim();
|
|
982
|
+
if (!id)
|
|
983
|
+
return { notice: 'skip 需要 id。' };
|
|
984
|
+
const ok = skipReview(io.l1, id);
|
|
985
|
+
return { notice: ok ? `已跳过 ${id}(不再进复查队列)。` : `跳过失败:id=${id} 不存在。`, id };
|
|
986
|
+
}
|
|
987
|
+
return { notice: `非法 action:${action}(允许 next/confirm/skip/annotate)。剩余待复查 ${pendingReviewCount(stores.l1)} 条。` };
|
|
988
|
+
},
|
|
989
|
+
}));
|
|
767
990
|
ctx.tools.register(defineTool({
|
|
768
991
|
name: 'memory_ruminate_status',
|
|
769
992
|
description: '查询当前记忆反刍的状态与进度(是否运行中、当前阶段、已完成/总会话数等)。\n\n注意:在 DSH Web GUI 的对话中无法直接调用此工具,因为模型可调用工具列表不包含 ruminate_status。如需查询进度,请在记忆库面板的蒸馏面板查看。',
|
package/dist/types.d.ts
CHANGED
|
@@ -41,6 +41,10 @@ export interface RoomCount {
|
|
|
41
41
|
room: string;
|
|
42
42
|
/** 该 Room 下的记录数(与 `wingL1Counts()` 同口径:含 retired 行)。 */
|
|
43
43
|
count: number;
|
|
44
|
+
/** 来源标示(rooms-get 端点层合并注册表后附带;'grown' = 自生长,可缺省)。 */
|
|
45
|
+
source?: 'pre-registered' | 'grown';
|
|
46
|
+
/** 人类可读名(注册表条目才有;面板 chip 可用 label 展示)。 */
|
|
47
|
+
label?: string;
|
|
44
48
|
}
|
|
45
49
|
/** 记录族标签推断:work_* 前缀 → work,其余(含 auto 档兜底)→ chat。 */
|
|
46
50
|
export declare function familyForType(type: string): MemoryFamily;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-prime-memory",
|
|
3
|
-
"version": "0.18.
|
|
3
|
+
"version": "0.18.5",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org",
|
|
6
6
|
"access": "public",
|
|
@@ -50,14 +50,14 @@
|
|
|
50
50
|
"ENGINEERING-NOTES.md"
|
|
51
51
|
],
|
|
52
52
|
"scripts": {
|
|
53
|
-
"build": "tsc -p tsconfig.json && node scripts/build-client.mjs && node scripts/copy-client.mjs",
|
|
53
|
+
"build": "node -e \"fs.rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json && node scripts/build-client.mjs && node scripts/copy-client.mjs",
|
|
54
54
|
"smoke": "npm run build:smoke && node dist-smoke/smoke.js",
|
|
55
55
|
"verify-catalog": "npm run build && node scripts/verify-catalog.mjs",
|
|
56
56
|
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.client.json --noEmit && tsc -p tsconfig.test.json",
|
|
57
57
|
"build:smoke": "tsc src/smoke.ts --outDir dist-smoke --module nodenext --moduleResolution nodenext --target es2022 --strict --skipLibCheck --esModuleInterop",
|
|
58
58
|
"lint": "eslint src client/src scripts tests",
|
|
59
59
|
"test": "vitest run",
|
|
60
|
-
"
|
|
60
|
+
"wing-migrate": "npm run build && node scripts/wing-migrate.mjs"
|
|
61
61
|
},
|
|
62
62
|
"engines": {
|
|
63
63
|
"node": ">=22.16.0",
|
|
@@ -125,15 +125,16 @@
|
|
|
125
125
|
"devDependencies": {
|
|
126
126
|
"@deepseek-ai/cordis": "^4.0.4",
|
|
127
127
|
"@deepseek-ai/cordis-plugin-loader": "^1.0.5",
|
|
128
|
-
"@deepseek-ai/dsh-agent": "0.1.
|
|
129
|
-
"@deepseek-ai/dsh-agent-default-model": "0.1.
|
|
130
|
-
"@deepseek-ai/dsh-client-connection": "0.1.
|
|
131
|
-
"@deepseek-ai/dsh-
|
|
132
|
-
"@deepseek-ai/dsh-
|
|
133
|
-
"@deepseek-ai/dsh-
|
|
134
|
-
"@deepseek-ai/dsh-
|
|
135
|
-
"@deepseek-ai/dsh-
|
|
136
|
-
"@deepseek-ai/dsh-
|
|
128
|
+
"@deepseek-ai/dsh-agent": "0.1.7-rc.2",
|
|
129
|
+
"@deepseek-ai/dsh-agent-default-model": "0.1.7-rc.2",
|
|
130
|
+
"@deepseek-ai/dsh-client-connection": "0.1.7-rc.2",
|
|
131
|
+
"@deepseek-ai/dsh-compaction": "0.1.7-rc.2",
|
|
132
|
+
"@deepseek-ai/dsh-home-paths": "0.1.7-rc.2",
|
|
133
|
+
"@deepseek-ai/dsh-llm": "0.1.7-rc.2",
|
|
134
|
+
"@deepseek-ai/dsh-session": "0.1.7-rc.2",
|
|
135
|
+
"@deepseek-ai/dsh-settings": "0.1.7-rc.2",
|
|
136
|
+
"@deepseek-ai/dsh-system-prompt": "0.1.7-rc.2",
|
|
137
|
+
"@deepseek-ai/dsh-tools": "0.1.7-rc.2",
|
|
137
138
|
"@deepseek-ai/schemastery": "^3.18.4",
|
|
138
139
|
"@eslint/js": "^10.0.1",
|
|
139
140
|
"@types/node": "^22.0.0",
|
package/dist/hall-backfill.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 存量未打标记录的一键回填(task_15):从 HallWheel"未打标 M"入口触发,
|
|
3
|
-
* 对 metadata 无 hall 的 L1 记录按 8 角词表批量补打标签(复用 L1 抽取的打标路径:
|
|
4
|
-
* 同一候选词表与措辞,经既有蒸馏 LLM 路由)。
|
|
5
|
-
*
|
|
6
|
-
* 安全边界:
|
|
7
|
-
* - **单飞**:同时最多一个回填任务,重复触发直接返回 running(不排队不叠加);
|
|
8
|
-
* - **后台执行**:端点立即返回,逐块推进(每块 20 条),不阻塞 RPC/管线;
|
|
9
|
-
* - **失败可退**:LLM 失败/输出不合法只跳过当前块,原记录零改动(标签只写成功的);
|
|
10
|
-
* - **有界**:单次任务最多处理 MAX_RECORDS 条,防止一次跑飞;其余可再次触发。
|
|
11
|
-
*/
|
|
12
|
-
import type { Context } from '@deepseek-ai/cordis';
|
|
13
|
-
import { type MemoryLogger } from './types.js';
|
|
14
|
-
import type { L1Store } from './store/l1.js';
|
|
15
|
-
import type { MemoryConfig } from './config.js';
|
|
16
|
-
export interface HallBackfillDeps {
|
|
17
|
-
ctx: Context;
|
|
18
|
-
cfg: MemoryConfig;
|
|
19
|
-
l1: L1Store;
|
|
20
|
-
logger: MemoryLogger;
|
|
21
|
-
}
|
|
22
|
-
export interface HallBackfillState {
|
|
23
|
-
running: boolean;
|
|
24
|
-
/** 本进程累计成功打标条数(重启归零;仅作观测,不作账)。 */
|
|
25
|
-
updated: number;
|
|
26
|
-
failed: number;
|
|
27
|
-
}
|
|
28
|
-
export declare function hallBackfillState(): HallBackfillState;
|
|
29
|
-
/** 启动后台回填;已在运行返回 false(单飞)。 */
|
|
30
|
-
export declare function startHallBackfill(deps: HallBackfillDeps): {
|
|
31
|
-
started: boolean;
|
|
32
|
-
running: boolean;
|
|
33
|
-
};
|
package/dist/hall-backfill.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { callLLM } from './llm.js';
|
|
2
|
-
import { normHallEnabled } from './config.js';
|
|
3
|
-
import { HALL_FALLBACK } from './types.js';
|
|
4
|
-
import { parseJsonLogged } from './llm.js';
|
|
5
|
-
/** 单次任务处理上限(524 条存量一次跑完约为 LLM 成本大头;分批触发,可重复执行)。 */
|
|
6
|
-
const MAX_RECORDS = 300;
|
|
7
|
-
/** 每块条数(一次 LLM 调用判定的记录数)。 */
|
|
8
|
-
const CHUNK = 20;
|
|
9
|
-
const state = { running: false, updated: 0, failed: 0 };
|
|
10
|
-
export function hallBackfillState() {
|
|
11
|
-
return { ...state };
|
|
12
|
-
}
|
|
13
|
-
const SYSTEM_PROMPT = '你是记忆库的域标注器。给你若干条记忆(带 id),为每条判断其内容属于哪个 Hall 域,输出 JSON 数组:' +
|
|
14
|
-
'[{"id":"<原id>","hall":"<域id>"}]。hall 只能从给定候选列表中选;横跨多域或确实无法归入任何角时选 general。' +
|
|
15
|
-
'只输出 JSON,不要多余文字。';
|
|
16
|
-
/** 启动后台回填;已在运行返回 false(单飞)。 */
|
|
17
|
-
export function startHallBackfill(deps) {
|
|
18
|
-
if (state.running)
|
|
19
|
-
return { started: false, running: true };
|
|
20
|
-
state.running = true;
|
|
21
|
-
state.updated = 0;
|
|
22
|
-
state.failed = 0;
|
|
23
|
-
void run(deps).finally(() => {
|
|
24
|
-
state.running = false;
|
|
25
|
-
deps.logger.info(`[memory] hall 回填结束:成功 ${state.updated} 条,失败 ${state.failed} 条`);
|
|
26
|
-
});
|
|
27
|
-
return { started: true, running: true };
|
|
28
|
-
}
|
|
29
|
-
async function run(deps) {
|
|
30
|
-
const { ctx, cfg, l1, logger } = deps;
|
|
31
|
-
const halls = normHallEnabled(cfg.hall?.enabled);
|
|
32
|
-
if (halls.length === 0) {
|
|
33
|
-
logger.warn('[memory] hall 回填跳过:hall.enabled 为空(打标功能已关闭)');
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
const candidates = [...halls, HALL_FALLBACK].join(' / ');
|
|
37
|
-
// 未打标集:主表全量里的 null hall(含 retired 行——restore 后标签已补,不重扫)
|
|
38
|
-
const all = l1.all();
|
|
39
|
-
const pending = all.filter((r) => !(r.metadata && typeof r.metadata.hall === 'string' && r.metadata.hall !== ''));
|
|
40
|
-
let budget = Math.min(pending.length, MAX_RECORDS);
|
|
41
|
-
logger.info(`[memory] hall 回填启动:未打标 ${pending.length} 条,本次上限 ${budget} 条(候选 ${candidates})`);
|
|
42
|
-
for (let offset = 0; offset < pending.length && budget > 0; offset += CHUNK) {
|
|
43
|
-
const chunk = pending.slice(offset, offset + CHUNK).filter(pickChunk);
|
|
44
|
-
if (chunk.length === 0)
|
|
45
|
-
continue;
|
|
46
|
-
budget -= chunk.length;
|
|
47
|
-
const labeled = await labelChunk(ctx, cfg, logger, chunk, candidates);
|
|
48
|
-
for (const { record, hall } of labeled) {
|
|
49
|
-
// 原记录最小改动:metadata.hall 单键写入,其余字段原样 upsert(同 id 版本不变语义)
|
|
50
|
-
const next = { ...record, metadata: { ...(record.metadata ?? {}), hall } };
|
|
51
|
-
try {
|
|
52
|
-
await l1.upsert(next);
|
|
53
|
-
state.updated++;
|
|
54
|
-
}
|
|
55
|
-
catch {
|
|
56
|
-
state.failed++;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
/** 回填只处理活跃记录可安全加速,但 retired 行的 metadata_json 同样保留且 restore 后可见——
|
|
62
|
-
* upsert 对 retired 行的行为由存储层保证(同 id 覆盖不复活),这里不做活性过滤,口径与迁移一致(全量)。 */
|
|
63
|
-
function pickChunk(_r) {
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
async function labelChunk(ctx, cfg, logger, chunk, candidates) {
|
|
67
|
-
const list = chunk.map((r, i) => `${i + 1}. id=${r.id}\n${r.content.slice(0, 300)}`).join('\n\n');
|
|
68
|
-
const user = `候选 Hall:${candidates}\n\n记忆列表:\n${list}`;
|
|
69
|
-
try {
|
|
70
|
-
const raw = await callLLM(ctx, cfg, {
|
|
71
|
-
system: SYSTEM_PROMPT,
|
|
72
|
-
user,
|
|
73
|
-
maxTokens: 4096,
|
|
74
|
-
layer: 'l1-extract',
|
|
75
|
-
logger,
|
|
76
|
-
});
|
|
77
|
-
const parsed = parseJsonLogged(raw, 'hall 回填', logger);
|
|
78
|
-
if (!Array.isArray(parsed))
|
|
79
|
-
return [];
|
|
80
|
-
const byId = new Map(chunk.map((r) => [r.id, r]));
|
|
81
|
-
const out = [];
|
|
82
|
-
for (const item of parsed) {
|
|
83
|
-
const record = typeof item?.id === 'string' ? byId.get(item.id) : undefined;
|
|
84
|
-
const hall = typeof item?.hall === 'string' ? item.hall.trim() : '';
|
|
85
|
-
if (record && hall)
|
|
86
|
-
out.push({ record, hall });
|
|
87
|
-
}
|
|
88
|
-
return out;
|
|
89
|
-
}
|
|
90
|
-
catch (err) {
|
|
91
|
-
logger.warn(`[memory] hall 回填块失败(跳过 ${chunk.length} 条,原记录未改): ${err instanceof Error ? err.message : String(err)}`);
|
|
92
|
-
state.failed += chunk.length;
|
|
93
|
-
return [];
|
|
94
|
-
}
|
|
95
|
-
}
|
package/dist/hall-migrate.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { MemoryLogger } from './types.js';
|
|
2
|
-
/** 新词表下的全部合法 hall 值(8 角 + 跨域兜底)。 */
|
|
3
|
-
export declare const HALL_VALID_VALUES: readonly string[];
|
|
4
|
-
export interface HallMigrationReport {
|
|
5
|
-
dbPath: string;
|
|
6
|
-
dryRun: boolean;
|
|
7
|
-
/** 主表全量行数(含 retired;与 hallL1Counts 的扫描口径一致)。 */
|
|
8
|
-
total: number;
|
|
9
|
-
/** retired 行数(valid_to 非空或带退场标记——迁移**不加过滤**,全量扫描自动覆盖)。 */
|
|
10
|
-
retired: number;
|
|
11
|
-
active: number;
|
|
12
|
-
/** 逐值计数(含 retired 行;null 单列)。 */
|
|
13
|
-
byHall: Record<string, number>;
|
|
14
|
-
unlabeled: number;
|
|
15
|
-
/** 不在新词表内的历史值(按 R10 不删已有标签,仅报告)。 */
|
|
16
|
-
unknownValues: Record<string, number>;
|
|
17
|
-
/** metadata 解析失败的行数(备份仍写入原始 JSON)。 */
|
|
18
|
-
corruptMetadata: number;
|
|
19
|
-
/** apply 模式写出的备份文件路径(dry-run 为 null)。 */
|
|
20
|
-
backupPath: string | null;
|
|
21
|
-
/** 结论:expected = 全部值合法(迁移无需改写);unknown = 存在历史遗留值(已报告,保留)。 */
|
|
22
|
-
verdict: 'expected' | 'unknown-values';
|
|
23
|
-
}
|
|
24
|
-
export interface HallMigrationOptions {
|
|
25
|
-
dbPath: string;
|
|
26
|
-
/** true = 只读报告,不写备份;false = 先备份 metadata_json 再出报告。 */
|
|
27
|
-
dryRun?: boolean;
|
|
28
|
-
/** 备份输出目录(缺省 = db 同目录 hall-migrate-backup/)。 */
|
|
29
|
-
backupDir?: string;
|
|
30
|
-
logger?: MemoryLogger;
|
|
31
|
-
}
|
|
32
|
-
export declare function runHallMigration(opts: HallMigrationOptions): HallMigrationReport;
|
package/dist/hall-migrate.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* hall 词表迁移(task_14):v0.12 5 值词表 → 8 角 + general 跨域兜底的**有界 targeted pass**。
|
|
3
|
-
*
|
|
4
|
-
* 关键事实(决定本迁移的形态):
|
|
5
|
-
* - 4 个保留 id(work/relationships/finance/journey)在新词表下仍是合法角,**存量标签零改动**;
|
|
6
|
-
* - `general` 保留为跨域兜底值(不重写、不删除);
|
|
7
|
-
* - 4 个新角无存量。因此本迁移**不改任何行的 hall 值**——它做的是:
|
|
8
|
-
* ① 备份 metadata_json(apply 模式);② **扫 L1 主表全量(含 retired 行)**逐行核对;
|
|
9
|
-
* ③ 落报告:逐值计数(含 null)/活性与 retired 拆分/未知值清单。
|
|
10
|
-
*
|
|
11
|
-
* 幂等:连续执行两次报告一致(扫描是纯读)。共享扫描函数 = `MemoryDb.scanL1Metadata`
|
|
12
|
-
* (单一所有者;计划 B 的引用门禁复用同一函数,不写第二份扫描)。
|
|
13
|
-
*/
|
|
14
|
-
import { openSync, closeSync, writeSync, existsSync, mkdirSync } from 'node:fs';
|
|
15
|
-
import * as path from 'node:path';
|
|
16
|
-
import { MemoryDb } from './store/sqlite.js';
|
|
17
|
-
import { HALL_CATALOG, HALL_FALLBACK } from './types.js';
|
|
18
|
-
/** 新词表下的全部合法 hall 值(8 角 + 跨域兜底)。 */
|
|
19
|
-
export const HALL_VALID_VALUES = [...HALL_CATALOG.map((h) => h.id), HALL_FALLBACK];
|
|
20
|
-
export function runHallMigration(opts) {
|
|
21
|
-
const db = new MemoryDb(opts.dbPath, 0, opts.logger);
|
|
22
|
-
// 开库即建表/补列(CREATE TABLE IF NOT EXISTS 惯例);未 init 则句柄未打开,扫描恒 0
|
|
23
|
-
db.init();
|
|
24
|
-
try {
|
|
25
|
-
const report = {
|
|
26
|
-
dbPath: opts.dbPath,
|
|
27
|
-
dryRun: !!opts.dryRun,
|
|
28
|
-
total: 0,
|
|
29
|
-
retired: 0,
|
|
30
|
-
active: 0,
|
|
31
|
-
byHall: {},
|
|
32
|
-
unlabeled: 0,
|
|
33
|
-
unknownValues: {},
|
|
34
|
-
corruptMetadata: 0,
|
|
35
|
-
backupPath: null,
|
|
36
|
-
verdict: 'expected',
|
|
37
|
-
};
|
|
38
|
-
let backupFd = null;
|
|
39
|
-
if (!report.dryRun) {
|
|
40
|
-
const dir = opts.backupDir ?? path.join(path.dirname(opts.dbPath), 'hall-migrate-backup');
|
|
41
|
-
if (!existsSync(dir))
|
|
42
|
-
mkdirSync(dir, { recursive: true });
|
|
43
|
-
const backupPath = path.join(dir, `metadata-backup-${new Date().toISOString().replace(/[:.]/g, '-')}.jsonl`);
|
|
44
|
-
backupFd = openSync(backupPath, 'w');
|
|
45
|
-
report.backupPath = backupPath;
|
|
46
|
-
}
|
|
47
|
-
try {
|
|
48
|
-
report.total = db.scanL1Metadata((recordId, metadata) => {
|
|
49
|
-
const hall = metadata && typeof metadata.hall === 'string' && metadata.hall !== '' ? metadata.hall : null;
|
|
50
|
-
// metadata 缺失/解析失败与"无 hall"同样计入未打标(存量 524 null 的主口径)
|
|
51
|
-
// 备份:原始 metadata_json 原样落盘(apply 模式;dry-run 不写任何文件)
|
|
52
|
-
if (backupFd !== null) {
|
|
53
|
-
writeSync(backupFd, JSON.stringify({ recordId, metadata }) + '\n');
|
|
54
|
-
}
|
|
55
|
-
if (hall === null) {
|
|
56
|
-
report.unlabeled++;
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
report.byHall[hall] = (report.byHall[hall] ?? 0) + 1;
|
|
60
|
-
if (!HALL_VALID_VALUES.includes(hall)) {
|
|
61
|
-
report.unknownValues[hall] = (report.unknownValues[hall] ?? 0) + 1;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
// retired 拆分:退场判定与存储层同口径(valid_to 非空 ∨ dsh_superseded 标记)。
|
|
66
|
-
// 扫描本身**不加过滤**(全表 SELECT 自动覆盖 retired),这里仅分类计数。
|
|
67
|
-
report.active = report.total - countRetired(db);
|
|
68
|
-
report.retired = report.total - report.active;
|
|
69
|
-
}
|
|
70
|
-
finally {
|
|
71
|
-
if (backupFd !== null)
|
|
72
|
-
closeSync(backupFd);
|
|
73
|
-
}
|
|
74
|
-
report.verdict = Object.keys(report.unknownValues).length > 0 ? 'unknown-values' : 'expected';
|
|
75
|
-
return report;
|
|
76
|
-
}
|
|
77
|
-
finally {
|
|
78
|
-
db.close();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
/** retired 行计数(与 sqlite 层退场判定同口径;用于验证"全量扫描不漏 retired")。 */
|
|
82
|
-
function countRetired(db) {
|
|
83
|
-
// 借道 scanL1Metadata 之外的轻量路径不可得(retired 判定在行级字段),
|
|
84
|
-
// 这里直接用 hallL1Counts 的同族 SQL:valid_to 非空 ∨ metadata 带 dsh_superseded。
|
|
85
|
-
// 为保持"单一所有者",经由 scanL1Metadata 之外不再开第二条 SQL——改为返回占位 0,
|
|
86
|
-
// 由 retire 拆分调用方(db.retiredL1Count)提供。
|
|
87
|
-
return db.retiredL1Count();
|
|
88
|
-
}
|