continuum-memory 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +185 -2
- package/config.schema.json +57 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +21 -0
- package/index.js +10 -0
- package/manifest.json +68 -0
- package/package.json +54 -3
- package/src/capture.js +177 -0
- package/src/config.js +134 -0
- package/src/contract.js +33 -0
- package/src/db.js +486 -0
- package/src/decay.js +131 -0
- package/src/inject.js +213 -0
- package/src/plugin.js +175 -0
- package/src/query.js +226 -0
- package/src/tools.js +467 -0
- package/src/vector.js +205 -0
package/src/tools.js
ADDED
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import {
|
|
2
|
+
searchMem, traceExact, timeline, neighbors, persona, activeRoleCard, upsertPersona,
|
|
3
|
+
insertMem, forgetMem, chains, tableCounts, bySrc, dbSizeMb, legacyPresence, bindingOf, clip, db,
|
|
4
|
+
} from './db.js';
|
|
5
|
+
import { remember as rememberInto } from './capture.js';
|
|
6
|
+
import { queryPlan, renderPlan, graphExpand, renderTrace, renderTimeline, renderStatus } from './query.js';
|
|
7
|
+
import { refreshActivation, sinking, decayStats, FLOOR, HALF_LIFE_DAYS } from './decay.js';
|
|
8
|
+
import { vectorCoverage, vectorStatus, embedOne } from './vector.js';
|
|
9
|
+
import { LIMITS, ZONES } from './contract.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 一个插件的全部工具面。以前这些活分在五个插件里:
|
|
13
|
+
* mem_query / mem_timeline ← soul_recall + memory_search + recall + hcc_memory
|
|
14
|
+
* mem_trace ← trace(认承诺只认它)
|
|
15
|
+
* mem_remember / mem_forget ← hcc_memory remember/forget
|
|
16
|
+
* mem_persona ← hcc_character + persona_edit
|
|
17
|
+
* mem_chain ← hcc_chain
|
|
18
|
+
* mem_status ← hcc_ops status + mem_status
|
|
19
|
+
*
|
|
20
|
+
* 一条硬规矩沿用 HCC 的教训:message 里必须放实际内容。
|
|
21
|
+
* 只写「命中 N 条」= 等于没查,模型看不到正文就会答「查到了但说不清」。
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const str = (description, extra = {}) => ({ type: 'string', description, ...extra });
|
|
25
|
+
const int = (description, extra = {}) => ({ type: 'integer', description, ...extra });
|
|
26
|
+
const bool = (description, extra = {}) => ({ type: 'boolean', description, ...extra });
|
|
27
|
+
|
|
28
|
+
/** data 里可能塞了整行记录,序列化要能扛住循环引用和超长。 */
|
|
29
|
+
function stringifyForDisplay(value) {
|
|
30
|
+
if (value == null) return '';
|
|
31
|
+
try {
|
|
32
|
+
const s = JSON.stringify(value, null, 2);
|
|
33
|
+
if (typeof s !== 'string') return String(value);
|
|
34
|
+
return s.length > 4000 ? `${s.slice(0, 4000)}\n…(data 共 ${s.length} 字符)` : s;
|
|
35
|
+
} catch {
|
|
36
|
+
return '';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* DSH 的工具输出契约(2026-11-22 补)。
|
|
42
|
+
* 缺这个字段时插件会 fiberPhase: failed,诊断是
|
|
43
|
+
* tool "mem_query" must declare output { schema, render, presentationMeta? }
|
|
44
|
+
* 形状照抄 HCC 的 genericOutput:message 必填、data 可选,render 把两者拼成文本。
|
|
45
|
+
*/
|
|
46
|
+
function genericOutput(render) {
|
|
47
|
+
return {
|
|
48
|
+
schema: {
|
|
49
|
+
type: 'object',
|
|
50
|
+
additionalProperties: true,
|
|
51
|
+
required: ['message'],
|
|
52
|
+
properties: {
|
|
53
|
+
message: { type: 'string' },
|
|
54
|
+
data: { type: 'object', additionalProperties: true },
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
render:
|
|
58
|
+
render
|
|
59
|
+
?? ((_args, value) => {
|
|
60
|
+
const head = String(value?.message ?? '');
|
|
61
|
+
const payload = stringifyForDisplay(value?.data);
|
|
62
|
+
const text = payload ? (head ? `${head}\n${payload}` : payload) : head || JSON.stringify(value);
|
|
63
|
+
return [{ type: 'text', text }];
|
|
64
|
+
}),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function clampMessage(text) {
|
|
69
|
+
const s = String(text ?? '');
|
|
70
|
+
if (s.length <= LIMITS.maxMessageChars) return s;
|
|
71
|
+
return `${s.slice(0, LIMITS.maxMessageChars)}\n\n…(共 ${s.length} 字,已截断;要更多用 top 或指定 only)`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function createContinuumTools() {
|
|
75
|
+
return [
|
|
76
|
+
// ---------------------------------------------------------------- 查
|
|
77
|
+
{
|
|
78
|
+
name: 'mem_query',
|
|
79
|
+
description:
|
|
80
|
+
'记忆检索(唯一入口,一张库)。三路融合:词面(trigram/LIKE,字面准)+ 语义(bge-small 向量,意思准)+ 激活(遗忘衰减,记得住的排前面)。'
|
|
81
|
+
+ '库里有五个来源的全部原文与出处:soul 图谱节点/片段、hcc 分区记忆、hippocampus 会话原文、五本 md 逐行、Continuum 自写。'
|
|
82
|
+
+ '一次调用把命中按来源分组摊开,每条都带出处(谁/哪个会话/什么时候)和一个分数(相似/激活)。'
|
|
83
|
+
+ '用途:想知道「以前说过什么、记过什么、我在某个情境怎么反应」时先跑这个。'
|
|
84
|
+
+ '查不到就是全库没有——可以放心说「忘记了」,不是某一路没查。'
|
|
85
|
+
+ '措辞不像但意思像也能命中(走向量那路);要精确字面用 mem_trace。',
|
|
86
|
+
parameters: {
|
|
87
|
+
query: str('要查的内容:一句话、关键词、或编号(如「纪律第15条」)', { required: true }),
|
|
88
|
+
top: int('每路返回条数', { minimum: 1, maximum: LIMITS.topMax }),
|
|
89
|
+
only: str('只查某几个来源,逗号分隔:soul,hcc,hippo,books,gate。省略=全查'),
|
|
90
|
+
graph: bool('顺边联想:对命中的 soul 节点顺着图谱边再捞一层'),
|
|
91
|
+
zone: str('只看某个分区:A-F'),
|
|
92
|
+
semantic: bool('走不走语义那一路(bge-small)。默认 true;关掉只查字面,快一点'),
|
|
93
|
+
},
|
|
94
|
+
output: genericOutput(),
|
|
95
|
+
isConcurrencySafe: () => true,
|
|
96
|
+
async execute(args) {
|
|
97
|
+
const query = String(args?.query ?? '').trim();
|
|
98
|
+
if (!query) return { message: '缺少 query。给我一句要查的话。', data: { error: 'missing_query' } };
|
|
99
|
+
|
|
100
|
+
const top = Math.min(Math.max(Number(args?.top) || LIMITS.topDefault, 1), LIMITS.topMax);
|
|
101
|
+
const only = typeof args?.only === 'string' && args.only.trim()
|
|
102
|
+
? args.only.split(',').map((s) => s.trim()).filter(Boolean)
|
|
103
|
+
: null;
|
|
104
|
+
const zone = typeof args?.zone === 'string' && ZONES.includes(args.zone.trim().toUpperCase())
|
|
105
|
+
? args.zone.trim().toUpperCase()
|
|
106
|
+
: null;
|
|
107
|
+
|
|
108
|
+
const plan = await queryPlan(query, { top, only, zone, semantic: args?.semantic !== false });
|
|
109
|
+
let graph = [];
|
|
110
|
+
if (args?.graph && !plan.empty) {
|
|
111
|
+
try {
|
|
112
|
+
graph = graphExpand(query, { top: 3 });
|
|
113
|
+
} catch {
|
|
114
|
+
graph = [];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
message: clampMessage(renderPlan(plan, { withGraph: Boolean(args?.graph), graph })),
|
|
119
|
+
data: { query, counts: plan.counts, total: plan.total, mode: plan.mode },
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: 'mem_trace',
|
|
125
|
+
description:
|
|
126
|
+
'逐字精确查:整串字必须原样出现在原文里,返回出处(会话 + 事件 + 时间)与完整原文。'
|
|
127
|
+
+ '用途:确认「某人是否说过某句话」、认承诺、核对有没有编。相似检索用 mem_query,这个只认字面。',
|
|
128
|
+
parameters: {
|
|
129
|
+
phrase: str('要逐字确认的原话片段(越长越准)', { required: true }),
|
|
130
|
+
top: int('返回条数', { minimum: 1, maximum: 30 }),
|
|
131
|
+
},
|
|
132
|
+
output: genericOutput(),
|
|
133
|
+
isConcurrencySafe: () => true,
|
|
134
|
+
async execute(args) {
|
|
135
|
+
const phrase = String(args?.phrase ?? '').trim();
|
|
136
|
+
if (!phrase) return { message: '缺少 phrase。', data: { error: 'missing_phrase' } };
|
|
137
|
+
const rows = traceExact(phrase, { top: Math.min(Math.max(Number(args?.top) || 8, 1), 30) });
|
|
138
|
+
return { message: clampMessage(renderTrace(phrase, rows)), data: { phrase, count: rows.length } };
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
name: 'mem_timeline',
|
|
143
|
+
description:
|
|
144
|
+
'按时间翻某一段时间聊了什么(会话原文流)。用途:问「昨天/上周/某天说了什么」时用,'
|
|
145
|
+
+ '不要凭印象。',
|
|
146
|
+
parameters: {
|
|
147
|
+
from: str('起始时间,ISO 或 YYYY-MM-DD'),
|
|
148
|
+
to: str('结束时间,ISO 或 YYYY-MM-DD'),
|
|
149
|
+
limit: int('返回条数', { minimum: 1, maximum: 100 }),
|
|
150
|
+
},
|
|
151
|
+
output: genericOutput(),
|
|
152
|
+
isConcurrencySafe: () => true,
|
|
153
|
+
async execute(args) {
|
|
154
|
+
const from = args?.from ? String(args.from) : null;
|
|
155
|
+
const to = args?.to ? String(args.to) : null;
|
|
156
|
+
const rows = timeline({ from, to, limit: Math.min(Math.max(Number(args?.limit) || 30, 1), 100) });
|
|
157
|
+
return { message: clampMessage(renderTimeline(rows, { from, to })), data: { count: rows.length } };
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
name: 'mem_graph',
|
|
162
|
+
description:
|
|
163
|
+
'顺藤摸瓜:给一个节点或关键词,返回它在知识图谱上的邻居(关系 + 权重)与邻居内容。'
|
|
164
|
+
+ '用途:想让一个词牵出一串相关记忆时用。',
|
|
165
|
+
parameters: {
|
|
166
|
+
query: str('关键词或节点 id(soul:node:xxx 也行)', { required: true }),
|
|
167
|
+
limit: int('边数上限', { minimum: 1, maximum: 30 }),
|
|
168
|
+
},
|
|
169
|
+
output: genericOutput(),
|
|
170
|
+
isConcurrencySafe: () => true,
|
|
171
|
+
async execute(args) {
|
|
172
|
+
const q = String(args?.query ?? '').trim();
|
|
173
|
+
if (!q) return { message: '缺少 query。', data: { error: 'missing_query' } };
|
|
174
|
+
const limit = Math.min(Math.max(Number(args?.limit) || 10, 1), 30);
|
|
175
|
+
let seed = null;
|
|
176
|
+
if (q.startsWith('soul:node:')) seed = q;
|
|
177
|
+
else {
|
|
178
|
+
// 只认图谱节点;span 是原文片段,不是节点,拿它查边必然空手
|
|
179
|
+
const hits = searchMem(q, { src: 'soul', kind: 'node', top: 1 });
|
|
180
|
+
seed = hits[0]?.id ?? null;
|
|
181
|
+
}
|
|
182
|
+
if (!seed) return { message: `「${q}」在图谱里没有对应节点。`, data: { query: q, nodes: 0 } };
|
|
183
|
+
const { edges, nodes } = neighbors(seed, { limit });
|
|
184
|
+
if (!edges.length && !nodes.length) {
|
|
185
|
+
return { message: `节点 ${seed} 没有连边(孤立节点)。`, data: { query: q, edges: 0 } };
|
|
186
|
+
}
|
|
187
|
+
const lines = [`图扩展 ${seed}`, `边 ${edges.length} 条,邻居 ${nodes.length} 个`];
|
|
188
|
+
for (const e of edges) {
|
|
189
|
+
lines.push(` 边 ${e.rel} (w=${Number(e.w).toFixed(2)}) ${e.src} → ${e.dst}`);
|
|
190
|
+
}
|
|
191
|
+
for (const n of nodes) lines.push(` 邻居 ${n.origin ?? n.id}\n ${clip(n.content, 200)}`);
|
|
192
|
+
return { message: clampMessage(lines.join('\n')), data: { query: q, edges: edges.length, nodes: nodes.length } };
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
// ---------------------------------------------------------------- 写
|
|
197
|
+
{
|
|
198
|
+
name: 'mem_remember',
|
|
199
|
+
description:
|
|
200
|
+
'记住一条内容(写入同一张库)。只在用户明确要求记住、或需要长期保留的事实时用。'
|
|
201
|
+
+ '分区:来源明确就按来源给 A-F;判不准就留空,会落 F(来源不明区)。'
|
|
202
|
+
+ '原文会全文保存,不做摘要替代。',
|
|
203
|
+
parameters: {
|
|
204
|
+
content: str('要记住的原文', { required: true }),
|
|
205
|
+
summary: str('可选摘要(不替代原文)'),
|
|
206
|
+
zone: str('分区 A-F,省略按会话绑定,再不行落 F'),
|
|
207
|
+
tags: str('标签,逗号分隔'),
|
|
208
|
+
speaker: str('谁说的:user / assistant / system'),
|
|
209
|
+
},
|
|
210
|
+
output: genericOutput(),
|
|
211
|
+
isConcurrencySafe: () => false,
|
|
212
|
+
async execute(args) {
|
|
213
|
+
const content = String(args?.content ?? '').trim();
|
|
214
|
+
if (!content) return { message: 'content 为空。', data: { error: 'empty' } };
|
|
215
|
+
const zone = args?.zone && ZONES.includes(String(args.zone).toUpperCase())
|
|
216
|
+
? String(args.zone).toUpperCase() : null;
|
|
217
|
+
const tags = typeof args?.tags === 'string' && args.tags.trim()
|
|
218
|
+
? args.tags.split(',').map((s) => s.trim()).filter(Boolean) : null;
|
|
219
|
+
const row = rememberInto({
|
|
220
|
+
speaker: args?.speaker || 'user',
|
|
221
|
+
content,
|
|
222
|
+
zone,
|
|
223
|
+
tags,
|
|
224
|
+
summary: args?.summary ?? null,
|
|
225
|
+
});
|
|
226
|
+
// 顺手算向量,让它下一轮就能被语义检索命中。
|
|
227
|
+
// 不 await:向量算 20ms 也要等模型加载,别让写入卡在这儿。
|
|
228
|
+
let vecState = '略过';
|
|
229
|
+
if (vectorStatus().available) {
|
|
230
|
+
embedOne(row.id, content)
|
|
231
|
+
.then((ok) => {
|
|
232
|
+
if (!ok) return;
|
|
233
|
+
})
|
|
234
|
+
.catch(() => {});
|
|
235
|
+
vecState = '计算中';
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
message: `已记住 ${row.id}(zone=${row.zone},${content.length} 字原文入库,向量${vecState})。`,
|
|
239
|
+
data: { id: row.id, zone: row.zone, vector: vecState },
|
|
240
|
+
};
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
name: 'mem_forget',
|
|
245
|
+
description:
|
|
246
|
+
'删除一条记忆(软删 + 从检索索引摘除 + 级联清掉它的归档索引)。'
|
|
247
|
+
+ '只在用户明确要求删时用,必须 confirm=true。',
|
|
248
|
+
parameters: {
|
|
249
|
+
id: str('记忆 id(mem_query 返回的 [xxx] 里那个)', { required: true }),
|
|
250
|
+
confirm: bool('必须为 true 才执行', { required: true }),
|
|
251
|
+
},
|
|
252
|
+
output: genericOutput(),
|
|
253
|
+
isConcurrencySafe: () => false,
|
|
254
|
+
async execute(args) {
|
|
255
|
+
if (args?.confirm !== true) return { message: '需要 confirm=true 才删。', data: { error: 'no_confirm' } };
|
|
256
|
+
const id = String(args?.id ?? '').trim();
|
|
257
|
+
if (!id) return { message: '缺少 id。', data: { error: 'missing_id' } };
|
|
258
|
+
const r = forgetMem(id);
|
|
259
|
+
return {
|
|
260
|
+
message: r.changed ? `已删 ${id}(同时清了 ${r.l2} 条归档索引)。` : `${id} 不存在或已删。`,
|
|
261
|
+
data: r,
|
|
262
|
+
};
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
|
|
266
|
+
// -------------------------------------------------------------- 身份
|
|
267
|
+
{
|
|
268
|
+
name: 'mem_persona',
|
|
269
|
+
description:
|
|
270
|
+
'身份层读写:角色卡 / 核心本能 / 本能数据库 / 后天训练区。'
|
|
271
|
+
+ 'get 读取(默认全部);set 写入新版本——**写入必须是用户明确提供的原文**,'
|
|
272
|
+
+ '模型不得自行编造人格,所以 set 要 confirmed=true 且内容来自用户。'
|
|
273
|
+
+ '角色卡每轮自动注入,写入立刻生效,不用重启。',
|
|
274
|
+
parameters: {
|
|
275
|
+
action: str('get | set', { required: true, enum: ['get', 'set'] }),
|
|
276
|
+
kind: str('role_card | core | instinct | post_training', { enum: ['role_card', 'core', 'instinct', 'post_training'] }),
|
|
277
|
+
content: str('set 时的原文(必须是用户提供的内容)'),
|
|
278
|
+
source: str('来源说明,例如 user:chat'),
|
|
279
|
+
note: str('版本说明'),
|
|
280
|
+
confirmed: bool('set 时必须为 true,表示内容由用户明确提供'),
|
|
281
|
+
},
|
|
282
|
+
output: genericOutput(),
|
|
283
|
+
isConcurrencySafe: () => false,
|
|
284
|
+
async execute(args) {
|
|
285
|
+
const action = String(args?.action ?? 'get').toLowerCase();
|
|
286
|
+
if (action === 'get') {
|
|
287
|
+
const kinds = args?.kind ? [String(args.kind)] : ['role_card', 'core', 'instinct', 'post_training'];
|
|
288
|
+
const lines = ['身份层(一张库)'];
|
|
289
|
+
for (const k of kinds) {
|
|
290
|
+
const rows = persona(k);
|
|
291
|
+
if (!rows.length) {
|
|
292
|
+
lines.push(`\n── ${k} ──(无)`);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
lines.push(`\n── ${k} · v${rows[0].version} · ${rows[0].active ? 'active' : 'inactive'} · 来源 ${rows[0].source ?? '?'} ──`);
|
|
296
|
+
lines.push(rows[0].content);
|
|
297
|
+
}
|
|
298
|
+
return { message: clampMessage(lines.join('\n')), data: { kinds } };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (args?.confirmed !== true) {
|
|
302
|
+
return {
|
|
303
|
+
message: '写入身份层需要 confirmed=true,且内容必须是用户明确提供的原文。模型不自造人格。',
|
|
304
|
+
data: { error: 'not_confirmed' },
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
const kind = String(args?.kind ?? '').trim();
|
|
308
|
+
const content = String(args?.content ?? '').trim();
|
|
309
|
+
if (!['role_card', 'core', 'instinct', 'post_training'].includes(kind)) {
|
|
310
|
+
return { message: `kind 必须是 role_card/core/instinct/post_training。`, data: { error: 'bad_kind' } };
|
|
311
|
+
}
|
|
312
|
+
if (!content) return { message: 'content 为空。', data: { error: 'empty' } };
|
|
313
|
+
const r = upsertPersona({
|
|
314
|
+
kind, content, source: args?.source ?? 'user', note: args?.note ?? null, actor: 'model+user', active: 1,
|
|
315
|
+
});
|
|
316
|
+
return {
|
|
317
|
+
message: `已写入 ${kind} v${r.version}(${content.length} 字),下一轮注入生效。`,
|
|
318
|
+
data: r,
|
|
319
|
+
};
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
|
|
323
|
+
// ---------------------------------------------------------------- 链
|
|
324
|
+
{
|
|
325
|
+
name: 'mem_chain',
|
|
326
|
+
description:
|
|
327
|
+
'H 反应链:读自己「遇到某种情境会怎么做」的链条。'
|
|
328
|
+
+ 'list 列出(可按 H1/H2/H3 筛);get 看单条完整内容;feedback 记录一次验证结果(会改权重)。',
|
|
329
|
+
parameters: {
|
|
330
|
+
action: str('list | get | feedback', { required: true, enum: ['list', 'get', 'feedback'] }),
|
|
331
|
+
status: str('list 时筛选:H1 / H2 / H3'),
|
|
332
|
+
chainId: str('get / feedback 时的链 id'),
|
|
333
|
+
result: str('feedback 时的结果:success / failure', { enum: ['success', 'failure'] }),
|
|
334
|
+
note: str('feedback 说明'),
|
|
335
|
+
limit: int('list 条数', { minimum: 1, maximum: 50 }),
|
|
336
|
+
},
|
|
337
|
+
output: genericOutput(),
|
|
338
|
+
isConcurrencySafe: () => true,
|
|
339
|
+
async execute(args) {
|
|
340
|
+
const action = String(args?.action ?? 'list').toLowerCase();
|
|
341
|
+
if (action === 'list') {
|
|
342
|
+
const rows = chains({
|
|
343
|
+
status: args?.status ? String(args.status).toUpperCase() : null,
|
|
344
|
+
limit: Math.min(Math.max(Number(args?.limit) || 20, 1), 50),
|
|
345
|
+
});
|
|
346
|
+
if (!rows.length) return { message: '没有链条。', data: { count: 0 } };
|
|
347
|
+
const lines = [`H 链 ${rows.length} 条`];
|
|
348
|
+
for (const c of rows) {
|
|
349
|
+
let intent = '';
|
|
350
|
+
try {
|
|
351
|
+
intent = JSON.parse(c.trigger_json || '{}').intent ?? '';
|
|
352
|
+
} catch {
|
|
353
|
+
intent = '';
|
|
354
|
+
}
|
|
355
|
+
lines.push(` [${c.id}] ${c.status}${c.frozen ? ' 冻结' : ''} 试${c.attempts ?? 0}/成${c.success ?? 0}`);
|
|
356
|
+
if (intent) lines.push(` 触发:${intent}`);
|
|
357
|
+
lines.push(` 动作:${clip(c.action, 160)}`);
|
|
358
|
+
}
|
|
359
|
+
return { message: clampMessage(lines.join('\n')), data: { count: rows.length } };
|
|
360
|
+
}
|
|
361
|
+
if (action === 'get') {
|
|
362
|
+
const id = String(args?.chainId ?? '').trim();
|
|
363
|
+
if (!id) return { message: '缺少 chainId。', data: { error: 'missing_id' } };
|
|
364
|
+
const row = chains({ limit: 200 }).find((c) => c.id === id);
|
|
365
|
+
if (!row) return { message: `没有 ${id}。`, data: { error: 'not_found' } };
|
|
366
|
+
const lines = [
|
|
367
|
+
`${row.id} · ${row.status} · 尝试 ${row.attempts}/${row.success}`,
|
|
368
|
+
`触发:${row.trigger_json}`,
|
|
369
|
+
`动作:${row.action}`,
|
|
370
|
+
`约束:${row.constraints_json}`,
|
|
371
|
+
];
|
|
372
|
+
return { message: clampMessage(lines.join('\n')), data: row };
|
|
373
|
+
}
|
|
374
|
+
// feedback:只改计数与最近结果,不动内容(H1/H2 内容锁定)
|
|
375
|
+
const id = String(args?.chainId ?? '').trim();
|
|
376
|
+
const result = String(args?.result ?? '').trim();
|
|
377
|
+
if (!id || !['success', 'failure'].includes(result)) {
|
|
378
|
+
return { message: 'feedback 需要 chainId 与 result=success|failure。', data: { error: 'bad_args' } };
|
|
379
|
+
}
|
|
380
|
+
const d = db();
|
|
381
|
+
const row = d.prepare('SELECT * FROM chain WHERE id = ?').get(id);
|
|
382
|
+
if (!row) return { message: `没有 ${id}。`, data: { error: 'not_found' } };
|
|
383
|
+
let recent = [];
|
|
384
|
+
try {
|
|
385
|
+
recent = JSON.parse(row.recent_json || '[]');
|
|
386
|
+
} catch {
|
|
387
|
+
recent = [];
|
|
388
|
+
}
|
|
389
|
+
recent.push(result);
|
|
390
|
+
const attempts = Number(row.attempts ?? 0) + 1;
|
|
391
|
+
const success = Number(row.success ?? 0) + (result === 'success' ? 1 : 0);
|
|
392
|
+
const now = new Date().toISOString();
|
|
393
|
+
d.prepare(
|
|
394
|
+
'UPDATE chain SET attempts=?, success=?, recent_json=?, last_used=?, updated_at=? WHERE id=?',
|
|
395
|
+
).run(attempts, success, JSON.stringify(recent.slice(-12)), now, now, id);
|
|
396
|
+
return {
|
|
397
|
+
message: `已记下 ${id} 一次 ${result}(累计 ${attempts} 次,成 ${success} 次)。`,
|
|
398
|
+
data: { id, attempts, success },
|
|
399
|
+
};
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
|
|
403
|
+
// -------------------------------------------------------------- 衰减
|
|
404
|
+
{
|
|
405
|
+
name: 'mem_decay',
|
|
406
|
+
description:
|
|
407
|
+
'遗忘曲线 / 激活衰减(只降权,不删记忆)。'
|
|
408
|
+
+ `公式:weight = exp(−Δdays / (halfLife · strength)),strength = 1 + 0.5·ln(1+use_count)。`
|
|
409
|
+
+ `半衰期:frozen=永不 · slow=${HALF_LIFE_DAYS.slow}天 · normal=${HALF_LIFE_DAYS.normal}天。`
|
|
410
|
+
+ `低于 ${FLOOR} 算沉底:还在库里、还能被检索到,但注入时不占预算。`
|
|
411
|
+
+ 'stats 看分布;sinking 看沉底清单(可以据此决定要不要追认某条);refresh 重算激活。',
|
|
412
|
+
parameters: {
|
|
413
|
+
action: str('stats | sinking | refresh', { required: true, enum: ['stats', 'sinking', 'refresh'] }),
|
|
414
|
+
full: bool('refresh 时全量重算(默认只算没算过的)'),
|
|
415
|
+
limit: int('sinking 返回条数', { minimum: 1, maximum: 50 }),
|
|
416
|
+
},
|
|
417
|
+
output: genericOutput(),
|
|
418
|
+
isConcurrencySafe: () => false,
|
|
419
|
+
async execute(args) {
|
|
420
|
+
const action = String(args?.action ?? 'stats').toLowerCase();
|
|
421
|
+
if (action === 'refresh') {
|
|
422
|
+
const r = refreshActivation({ full: Boolean(args?.full) });
|
|
423
|
+
return { message: `已重算 ${r.updated} 条激活${r.full ? '(全量)' : '(增量)'}。`, data: r };
|
|
424
|
+
}
|
|
425
|
+
if (action === 'sinking') {
|
|
426
|
+
const limit = Math.min(Math.max(Number(args?.limit) || 15, 1), 50);
|
|
427
|
+
const rows = sinking({ limit });
|
|
428
|
+
if (!rows.length) return { message: `没有沉底的(都在 ${FLOOR} 以上)。`, data: { count: 0 } };
|
|
429
|
+
const lines = [`沉底清单(权重 < ${FLOOR},${rows.length} 条,从最轻的排起)`];
|
|
430
|
+
lines.push(' 这些还在库里、mem_query 仍能查到,只是注入时不占预算。要追认就用 mem_query 命中它一次(会 +激活)。');
|
|
431
|
+
for (const r of rows) {
|
|
432
|
+
lines.push(` w${Number(r.activation).toFixed(3)} [${r.src}/${r.kind}] ${String(r.ts ?? '').slice(0, 10)} ${clip(r.peek, 60)}`);
|
|
433
|
+
}
|
|
434
|
+
return { message: clampMessage(lines.join('\n')), data: { count: rows.length } };
|
|
435
|
+
}
|
|
436
|
+
const st = decayStats();
|
|
437
|
+
const vec = vectorCoverage();
|
|
438
|
+
const lines = ['激活衰减分布'];
|
|
439
|
+
for (const [cls, s] of Object.entries(st)) {
|
|
440
|
+
const hl = HALF_LIFE_DAYS[cls] === Infinity ? '永不' : `${HALF_LIFE_DAYS[cls]}天`;
|
|
441
|
+
lines.push(` ${cls.padEnd(7)} ${String(s.n).padStart(5)} 条 · 均值 ${s.avg} · 区间 ${s.min}~${s.max} · 半衰期 ${hl}`);
|
|
442
|
+
}
|
|
443
|
+
lines.push(` 向量 ${vec.embedded}/${vec.total} 条有向量${vec.missing ? `(缺 ${vec.missing})` : '(全覆盖)'}`);
|
|
444
|
+
return { message: clampMessage(lines.join('\n')), data: { stats: st, vector: vec } };
|
|
445
|
+
},
|
|
446
|
+
},
|
|
447
|
+
|
|
448
|
+
// -------------------------------------------------------------- 状态
|
|
449
|
+
{
|
|
450
|
+
name: 'mem_status',
|
|
451
|
+
description:
|
|
452
|
+
'记忆系统概况:一张库的条数、分来源、身份层、H 链、以及旧库还在不在(迁移是否完成)。'
|
|
453
|
+
+ '回答「记忆有多少/有没有丢/在哪」时先跑这个拿数字,不要凭印象。',
|
|
454
|
+
parameters: {},
|
|
455
|
+
output: genericOutput(),
|
|
456
|
+
isConcurrencySafe: () => true,
|
|
457
|
+
async execute() {
|
|
458
|
+
const counts = tableCounts();
|
|
459
|
+
const sizes = dbSizeMb();
|
|
460
|
+
const legacy = legacyPresence();
|
|
461
|
+
const lines = [renderStatus(), '', ` 库体积 ${sizes} MB`, ` 结构化 edge=${counts.edge} mention=${counts.mention} chain=${counts.chain} l2=${counts.index_l2} binding=${counts.binding}`];
|
|
462
|
+
lines.push(` 旧库存在 soul=${legacy.soul ? '在' : '无'} hcc=${legacy.hcc ? '在' : '无'} hippo=${legacy.hippo ? '在' : '无'}(迁移源,不再被读写)`);
|
|
463
|
+
return { message: clampMessage(lines.join('\n')), data: { counts, sizesMb: sizes, legacy } };
|
|
464
|
+
},
|
|
465
|
+
},
|
|
466
|
+
];
|
|
467
|
+
}
|
package/src/vector.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { db, getMeta, setMeta } from './db.js';
|
|
4
|
+
import { config } from './config.js';
|
|
5
|
+
|
|
6
|
+
const MODEL_NAME = 'Xenova/bge-small-zh-v1.5';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 向量层:本地 bge-small-zh-v1.5(512 维,22.9 MB quantized)。
|
|
10
|
+
*
|
|
11
|
+
* 2026-11-22 加。理由:整合时把 memory-keeper 的向量检索丢了,
|
|
12
|
+
* 结果「意思像但措辞不像」的查询会空手——查不到原文里没有那几个字的对话。
|
|
13
|
+
* 字面检索救不了这种问题,只有向量能。
|
|
14
|
+
*
|
|
15
|
+
* 三条设计取舍:
|
|
16
|
+
* 1. 懒加载。模型 200ms 就绪,但那是热缓存;冷启动别让插件启动等它。
|
|
17
|
+
* 2. 失败即降级。任何一步抛错都返回 null,调用方回退词面检索,不抛进 prompt。
|
|
18
|
+
* 3. 模型从哪来由 config 决定:本地目录 / 远程下载 / 不可用。代码里不写死任何路径。
|
|
19
|
+
*
|
|
20
|
+
* 2026-11-22 晚:删掉了「开发机兜底路径」。
|
|
21
|
+
* 那段代码是为了让 link 包能解析到 @xenova/transformers,代价是发布物里会带一条
|
|
22
|
+
* 写死的本机绝对路径 —— 等于把自己的目录结构发出去。现在改成在包目录里建 node_modules
|
|
23
|
+
* 软链(不进包),裸 import 自然解析,代码干净了。
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export const DIMS = 512;
|
|
27
|
+
|
|
28
|
+
let pipePromise = null;
|
|
29
|
+
let unavailable = '';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 诚实报告可用性。
|
|
33
|
+
*
|
|
34
|
+
* 之前写的是 `available: !unavailable`——意思是「还没失败过」,
|
|
35
|
+
* 结果模型目录根本不存在时它照样报 true,测试里就是被这个骗过去一次。
|
|
36
|
+
* 现在按「模型从哪来」报,没有来源就是 false。
|
|
37
|
+
*/
|
|
38
|
+
export function vectorStatus() {
|
|
39
|
+
const c = config();
|
|
40
|
+
const cfgDir = c.modelDir || null;
|
|
41
|
+
const cfgHas = Boolean(cfgDir && fs.existsSync(cfgDir));
|
|
42
|
+
|
|
43
|
+
const base = { model: MODEL_NAME, dims: DIMS, error: unavailable || null };
|
|
44
|
+
if (unavailable) return { ...base, available: false, source: 'failed', dir: null };
|
|
45
|
+
if (cfgHas) return { ...base, available: true, source: 'config', dir: cfgDir, remote: false };
|
|
46
|
+
if (c.allowRemoteModel) return { ...base, available: true, source: 'remote', dir: null, remote: true };
|
|
47
|
+
return {
|
|
48
|
+
...base,
|
|
49
|
+
available: false,
|
|
50
|
+
source: 'none',
|
|
51
|
+
dir: null,
|
|
52
|
+
error: '没有本地模型,且 allowRemoteModel=false —— 语义检索不可用,字面那路照常',
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function getPipe() {
|
|
57
|
+
if (pipePromise) return pipePromise;
|
|
58
|
+
pipePromise = (async () => {
|
|
59
|
+
const c = config();
|
|
60
|
+
const { env, pipeline } = await import('@xenova/transformers');
|
|
61
|
+
env.remoteHost = c.modelMirror;
|
|
62
|
+
env.allowRemoteModels = c.allowRemoteModel;
|
|
63
|
+
|
|
64
|
+
const cfgDir = c.modelDir;
|
|
65
|
+
if (cfgDir && fs.existsSync(cfgDir)) {
|
|
66
|
+
env.cacheDir = cfgDir;
|
|
67
|
+
env.localModelPath = cfgDir + path.sep;
|
|
68
|
+
} else if (c.allowRemoteModel) {
|
|
69
|
+
// 没本地模型:下到 DSH data 目录,别乱丢到用户目录根下
|
|
70
|
+
env.cacheDir = path.join(c.dataDir, 'models');
|
|
71
|
+
fs.mkdirSync(env.cacheDir, { recursive: true });
|
|
72
|
+
} else {
|
|
73
|
+
throw new Error('没有本地模型,且 allowRemoteModel=false');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return pipeline('feature-extraction', MODEL_NAME, { quantized: true });
|
|
77
|
+
})().catch((e) => {
|
|
78
|
+
unavailable = e?.message || String(e);
|
|
79
|
+
throw e;
|
|
80
|
+
});
|
|
81
|
+
return pipePromise;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 把一批文本算成向量;不可用时返回 null(调用方回退词面)。 */
|
|
85
|
+
export async function embed(texts) {
|
|
86
|
+
if (unavailable) return null;
|
|
87
|
+
const list = (Array.isArray(texts) ? texts : [texts]).map((t) => String(t ?? '').slice(0, 2000));
|
|
88
|
+
if (!list.length) return [];
|
|
89
|
+
try {
|
|
90
|
+
const pipe = await getPipe();
|
|
91
|
+
const out = [];
|
|
92
|
+
const BATCH = 16;
|
|
93
|
+
for (let i = 0; i < list.length; i += BATCH) {
|
|
94
|
+
const r = await pipe(list.slice(i, i + BATCH), { pooling: 'mean', normalize: true });
|
|
95
|
+
out.push(...r.tolist());
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
} catch (e) {
|
|
99
|
+
unavailable = e?.message || String(e);
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 向量已归一化,点积即余弦。 */
|
|
105
|
+
export function cos(a, b) {
|
|
106
|
+
if (!a || !b || a.length !== b.length) return 0;
|
|
107
|
+
let s = 0;
|
|
108
|
+
for (let i = 0; i < a.length; i++) s += a[i] * b[i];
|
|
109
|
+
return s;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function toBlob(vec) {
|
|
113
|
+
return Buffer.from(new Float32Array(vec).buffer);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function fromBlob(buf) {
|
|
117
|
+
if (!buf) return null;
|
|
118
|
+
const b = Buffer.isBuffer(buf) ? buf : Buffer.from(buf);
|
|
119
|
+
// node:sqlite 可能给 Uint8Array;按 byteOffset 对齐后再转
|
|
120
|
+
const ab = b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
|
|
121
|
+
return new Float32Array(ab);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 缓存 query 向量:同一句话在一次对话里会被查多次。 */
|
|
125
|
+
const queryCache = new Map();
|
|
126
|
+
export async function queryVector(text) {
|
|
127
|
+
const key = String(text ?? '').trim();
|
|
128
|
+
if (!key) return null;
|
|
129
|
+
if (queryCache.has(key)) return queryCache.get(key);
|
|
130
|
+
const v = await embed([key]);
|
|
131
|
+
const one = v?.[0] ?? null;
|
|
132
|
+
if (one) {
|
|
133
|
+
if (queryCache.size > 64) queryCache.clear();
|
|
134
|
+
queryCache.set(key, one);
|
|
135
|
+
}
|
|
136
|
+
return one;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 语义检索:全库扫向量算余弦。
|
|
141
|
+
* 七千多条 × 512 维 ≈ 15 MB 内存 + 一次矩阵乘,实测几十毫秒,够用。
|
|
142
|
+
* 再涨两个数量级才需要 ANN 索引——现在上那个是浪费。
|
|
143
|
+
*/
|
|
144
|
+
export function semanticSearch(qvec, { top = 8, src = null, zone = null, minSim = 0.35 } = {}) {
|
|
145
|
+
if (!qvec) return [];
|
|
146
|
+
const d = db();
|
|
147
|
+
const where = ['deleted = 0', 'vector IS NOT NULL'];
|
|
148
|
+
const args = [];
|
|
149
|
+
if (src) {
|
|
150
|
+
where.push('src = ?');
|
|
151
|
+
args.push(src);
|
|
152
|
+
}
|
|
153
|
+
if (zone) {
|
|
154
|
+
where.push('zone = ?');
|
|
155
|
+
args.push(zone);
|
|
156
|
+
}
|
|
157
|
+
const rows = d
|
|
158
|
+
.prepare(
|
|
159
|
+
`SELECT id, src, kind, speaker, zone, ts, session, content, origin, vector
|
|
160
|
+
FROM mem WHERE ${where.join(' AND ')}`,
|
|
161
|
+
)
|
|
162
|
+
.all(...args);
|
|
163
|
+
const scored = [];
|
|
164
|
+
for (const r of rows) {
|
|
165
|
+
const v = fromBlob(r.vector);
|
|
166
|
+
if (!v) continue;
|
|
167
|
+
const sim = cos(qvec, v);
|
|
168
|
+
if (sim >= minSim) {
|
|
169
|
+
const { vector, ...rest } = r;
|
|
170
|
+
scored.push({ ...rest, sim });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
scored.sort((a, b) => b.sim - a.sim);
|
|
174
|
+
return scored.slice(0, top);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function vectorCoverage() {
|
|
178
|
+
const d = db();
|
|
179
|
+
const total = d.prepare('SELECT COUNT(*) n FROM mem WHERE deleted = 0').get().n;
|
|
180
|
+
const have = d
|
|
181
|
+
.prepare('SELECT COUNT(*) n FROM mem WHERE deleted = 0 AND vector IS NOT NULL')
|
|
182
|
+
.get().n;
|
|
183
|
+
return {
|
|
184
|
+
total: Number(total),
|
|
185
|
+
embedded: Number(have),
|
|
186
|
+
missing: Number(total) - Number(have),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** 给一条记忆补向量(写入时调用)。 */
|
|
191
|
+
export async function embedOne(id, content) {
|
|
192
|
+
const v = await embed([content]);
|
|
193
|
+
if (!v?.[0]) return false;
|
|
194
|
+
db().prepare('UPDATE mem SET vector = ? WHERE id = ?').run(toBlob(v[0]), id);
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function markVectorBuild(at, count) {
|
|
199
|
+
setMeta('vector_built_at', at);
|
|
200
|
+
setMeta('vector_count', String(count));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function vectorBuiltAt() {
|
|
204
|
+
return getMeta('vector_built_at');
|
|
205
|
+
}
|