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/query.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { searchMem, traceExact, timeline, neighbors, bySrc, clip, persona, activeRoleCard, chains } from './db.js';
|
|
2
|
+
import { semanticSearch, queryVector, vectorStatus, vectorCoverage } from './vector.js';
|
|
3
|
+
import { effectiveWeight, touch, decayStats, FLOOR } from './decay.js';
|
|
4
|
+
import { SRC_LABEL, LIMITS } from './contract.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 读的一侧:把「以前要调四个工具」压成一次。
|
|
8
|
+
*
|
|
9
|
+
* 2026-11-22 的教训:四个检索入口时我记不住哪把刀切什么,猜错就空手回来,
|
|
10
|
+
* 然后误报「没有」。现在一张库,一次查,结果按来源分组摊开——
|
|
11
|
+
* 空手就是真没有,不是「这一路没查」。
|
|
12
|
+
*
|
|
13
|
+
* 2026-11-22 晚补三样(同日整合时丢掉的):
|
|
14
|
+
* ① 语义那一路(bge-small):意思像但措辞不像也能命中
|
|
15
|
+
* ② 激活衰减:三周前的闲聊不该和昨天定的规矩排在一起
|
|
16
|
+
* ③ 三路融合打分:相似 0.55 + 字面 0.25 + 激活 0.20
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const W_SEMANTIC = 0.55;
|
|
20
|
+
const W_LEXICAL = 0.25;
|
|
21
|
+
const W_ACTIVATION = 0.20;
|
|
22
|
+
|
|
23
|
+
function label(src) {
|
|
24
|
+
return SRC_LABEL[src] ?? src;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function line(row, n = 320) {
|
|
28
|
+
const bits = [];
|
|
29
|
+
if (row.speaker) bits.push(row.speaker);
|
|
30
|
+
if (row.ts) bits.push(String(row.ts).slice(0, 16));
|
|
31
|
+
if (row.zone) bits.push(`zone=${row.zone}`);
|
|
32
|
+
if (row.session) bits.push(String(row.session).slice(0, 24));
|
|
33
|
+
// 分数让人看得出「这条为什么排前面」,不是黑箱
|
|
34
|
+
const score = [];
|
|
35
|
+
if (row._sim != null && row._sim > 0) score.push(`相似${row._sim.toFixed(2)}`);
|
|
36
|
+
if (row._w != null) score.push(`激活${row._w.toFixed(2)}`);
|
|
37
|
+
const head = `[${row.origin ?? row.id}]${bits.length ? ` (${bits.join(' ')})` : ''}${score.length ? ` · ${score.join('·')}` : ''}`;
|
|
38
|
+
return `${head}\n ${clip(row.content, n)}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 三路融合检索:词面 + 语义 + 激活。async 因为要算 query 向量。
|
|
43
|
+
*/
|
|
44
|
+
export async function queryPlan(query, { top = LIMITS.topDefault, only = null, zone = null, semantic = true } = {}) {
|
|
45
|
+
const q = String(query ?? '').trim();
|
|
46
|
+
if (!q) return { query: q, groups: {}, counts: {}, empty: true, total: 0, mode: 'none' };
|
|
47
|
+
|
|
48
|
+
const now = Date.now();
|
|
49
|
+
const pool = new Map();
|
|
50
|
+
|
|
51
|
+
// ① 词面(trigram 短语 → 分词 OR → LIKE)
|
|
52
|
+
const lex = searchMem(q, { top: top * 4, zone });
|
|
53
|
+
for (const r of lex) pool.set(r.id, { ...r, _lex: 1 });
|
|
54
|
+
|
|
55
|
+
// ② 语义(bge-small;失败就只剩词面,不报错)
|
|
56
|
+
let semCount = 0;
|
|
57
|
+
if (semantic && vectorStatus().available) {
|
|
58
|
+
try {
|
|
59
|
+
const qv = await queryVector(q);
|
|
60
|
+
if (qv) {
|
|
61
|
+
const sem = semanticSearch(qv, { top: top * 5, zone, minSim: 0.3 });
|
|
62
|
+
semCount = sem.length;
|
|
63
|
+
for (const r of sem) {
|
|
64
|
+
const prev = pool.get(r.id);
|
|
65
|
+
if (prev) prev._sim = r.sim;
|
|
66
|
+
else pool.set(r.id, { ...r, _lex: 0, _sim: r.sim });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
/* 向量路挂了不影响词面那路 */
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ③ 三路打分
|
|
75
|
+
const scored = [];
|
|
76
|
+
for (const r of pool.values()) {
|
|
77
|
+
if (only && !only.includes(r.src)) continue;
|
|
78
|
+
const sim = r._sim ?? 0;
|
|
79
|
+
const lexHit = r._lex ?? 0;
|
|
80
|
+
const w = effectiveWeight(r, now);
|
|
81
|
+
scored.push({ ...r, _w: w, _score: W_SEMANTIC * sim + W_LEXICAL * lexHit + W_ACTIVATION * w });
|
|
82
|
+
}
|
|
83
|
+
scored.sort((a, b) => b._score - a._score);
|
|
84
|
+
|
|
85
|
+
const groups = {};
|
|
86
|
+
for (const r of scored) (groups[r.src] ??= []).push(r);
|
|
87
|
+
|
|
88
|
+
const trimmed = {};
|
|
89
|
+
for (const [src, rows] of Object.entries(groups)) trimmed[src] = rows.slice(0, top);
|
|
90
|
+
|
|
91
|
+
const mode = semCount ? (lex.length ? '语义+词面+激活' : '语义+激活') : (lex.length ? '词面+激活' : '空');
|
|
92
|
+
|
|
93
|
+
// 命中的顶几条「想起一次」——每路前 2 条,别整页升级
|
|
94
|
+
const toTouch = [];
|
|
95
|
+
for (const rows of Object.values(trimmed)) for (const r of rows.slice(0, 2)) toTouch.push(r.id);
|
|
96
|
+
if (toTouch.length) {
|
|
97
|
+
try {
|
|
98
|
+
touch(toTouch);
|
|
99
|
+
} catch {
|
|
100
|
+
/* 加权失败不影响返回 */
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
query: q,
|
|
106
|
+
groups: trimmed,
|
|
107
|
+
counts: Object.fromEntries(Object.entries(groups).map(([k, v]) => [k, v.length])),
|
|
108
|
+
total: scored.length,
|
|
109
|
+
empty: scored.length === 0,
|
|
110
|
+
truncated: Object.entries(groups).some(([k, v]) => v.length > (trimmed[k]?.length ?? 0)),
|
|
111
|
+
mode,
|
|
112
|
+
semCount,
|
|
113
|
+
lexCount: lex.length,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 图扩展:命中的 soul 节点顺着边再捞一层。只认 kind=node,别拿 span 当节点。 */
|
|
118
|
+
export function graphExpand(query, { top = 3 } = {}) {
|
|
119
|
+
const hits = searchMem(query, { src: 'soul', kind: 'node', top });
|
|
120
|
+
const out = [];
|
|
121
|
+
for (const h of hits) {
|
|
122
|
+
const { edges, nodes } = neighbors(h.id, { limit: 6 });
|
|
123
|
+
if (!edges.length && !nodes.length) continue;
|
|
124
|
+
out.push({ from: h, edges, nodes });
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 渲染成给模型看的文本。规矩:必须带实际内容,不能只写「命中 N 条」。 */
|
|
130
|
+
export function renderPlan(plan, { withGraph = false, graph = [] } = {}) {
|
|
131
|
+
const lines = [];
|
|
132
|
+
const q = plan.query;
|
|
133
|
+
|
|
134
|
+
if (plan.empty) {
|
|
135
|
+
lines.push(`查「${q}」:全库 0 条命中(词面 + 语义 + 激活三路都查过)。`);
|
|
136
|
+
lines.push('(可以说「查不到」。换一种说法再试一次也值得——语义那路对措辞不敏感,换个角度问会有不同结果。)');
|
|
137
|
+
return lines.join('\n');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
lines.push(`查「${q}」:命中 ${plan.total} 条${plan.truncated ? '(每路只列前几条)' : ''} 路径=${plan.mode}`);
|
|
141
|
+
for (const [src, rows] of Object.entries(plan.groups)) {
|
|
142
|
+
lines.push('');
|
|
143
|
+
lines.push(`── ${label(src)} · ${plan.counts[src]} 条 ──`);
|
|
144
|
+
for (const r of rows) lines.push(line(r));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (withGraph && graph.length) {
|
|
148
|
+
lines.push('');
|
|
149
|
+
lines.push('── 顺边联想(图谱)──');
|
|
150
|
+
for (const g of graph.slice(0, 3)) {
|
|
151
|
+
lines.push(` 从「${clip(g.from.content, 40)}」出发:`);
|
|
152
|
+
for (const e of g.edges.slice(0, 4)) {
|
|
153
|
+
lines.push(` ${e.rel} (w=${Number(e.w).toFixed(2)}) → ${e.src === g.from.src_id ? e.dst : e.src}`);
|
|
154
|
+
}
|
|
155
|
+
for (const n of g.nodes.slice(0, 3)) lines.push(` · ${clip(n.content, 90)}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return lines.join('\n');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** 逐字查的渲染。出处必须完整:会话 + 事件 + 时间。 */
|
|
162
|
+
export function renderTrace(phrase, rows) {
|
|
163
|
+
if (!rows.length) {
|
|
164
|
+
return `逐字查「${phrase}」:0 条。全库原文里没有这串字(精确匹配,不是相似匹配)。\n想找「意思像」的用 mem_query,那条路走向量。`;
|
|
165
|
+
}
|
|
166
|
+
const lines = [`逐字查「${phrase}」:${rows.length} 条`];
|
|
167
|
+
for (const r of rows) {
|
|
168
|
+
const prov = [
|
|
169
|
+
r.src,
|
|
170
|
+
r.session ? `会话 ${r.session}` : null,
|
|
171
|
+
r.origin_event ? `事件 ${r.origin_event}` : null,
|
|
172
|
+
r.ts ? String(r.ts) : null,
|
|
173
|
+
].filter(Boolean).join(' · ');
|
|
174
|
+
lines.push('');
|
|
175
|
+
lines.push(` ${r.origin ?? r.id}`);
|
|
176
|
+
lines.push(` 出处:${prov}`);
|
|
177
|
+
lines.push(` 原文:${clip(r.content, 500)}`);
|
|
178
|
+
}
|
|
179
|
+
return lines.join('\n');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function renderTimeline(rows, { from, to } = {}) {
|
|
183
|
+
const head = `时间线 ${from ?? '最早'} → ${to ?? '现在'}`;
|
|
184
|
+
if (!rows.length) return `${head}:这段时间没有记录。`;
|
|
185
|
+
const now = Date.now();
|
|
186
|
+
const lines = [head, `共 ${rows.length} 条`];
|
|
187
|
+
for (const r of rows) {
|
|
188
|
+
const w = effectiveWeight(r, now);
|
|
189
|
+
lines.push(` ${String(r.ts).slice(0, 16)} [${r.src}/${r.kind}${r.speaker ? ` ${r.speaker}` : ''}·w${w.toFixed(2)}] ${clip(r.content, 110)}`);
|
|
190
|
+
}
|
|
191
|
+
return lines.join('\n');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** 身份层渲染:角色卡 + 核心本能 + 本能数据库。 */
|
|
195
|
+
export function renderIdentity() {
|
|
196
|
+
const role = activeRoleCard();
|
|
197
|
+
const core = persona('core')[0];
|
|
198
|
+
const instinct = persona('instinct')[0];
|
|
199
|
+
const lines = [];
|
|
200
|
+
if (role) lines.push(`### 角色卡(用户提供 · v${role.version})\n${role.content}`);
|
|
201
|
+
if (core) lines.push(`### 核心本能\n${core.content}`);
|
|
202
|
+
if (instinct) lines.push(`### 本能数据库\n${instinct.content}`);
|
|
203
|
+
return lines.join('\n\n');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function renderStatus() {
|
|
207
|
+
const counts = bySrc();
|
|
208
|
+
const total = Object.values(counts).reduce((a, b) => a + b, 0);
|
|
209
|
+
const role = activeRoleCard();
|
|
210
|
+
const chainsAll = chains({ limit: 50 });
|
|
211
|
+
const vec = vectorCoverage();
|
|
212
|
+
const vs = vectorStatus();
|
|
213
|
+
const dec = decayStats();
|
|
214
|
+
const lines = [
|
|
215
|
+
'记忆系统概况(一张库)',
|
|
216
|
+
` 库里共 ${total} 条`,
|
|
217
|
+
...Object.entries(counts).map(([k, v]) => ` ${k.padEnd(6)} ${v}`),
|
|
218
|
+
` 角色卡 ${role ? `v${role.version} active(来源 ${role.source ?? '?'})` : '无'}`,
|
|
219
|
+
` H 链 ${chainsAll.length} 条(H1 ${chainsAll.filter((c) => c.status === 'H1').length} / H2 ${chainsAll.filter((c) => c.status === 'H2').length} / H3 ${chainsAll.filter((c) => c.status === 'H3').length})`,
|
|
220
|
+
` 向量 ${vs.available ? `可用 ${vs.dims} 维 · 覆盖 ${vec.embedded}/${vec.total}${vec.missing ? `(缺 ${vec.missing},跑 backfill-vector.mjs)` : ''}` : `不可用:${vs.error}`}`,
|
|
221
|
+
' 激活衰减',
|
|
222
|
+
...Object.entries(dec).map(([cls, s]) => ` ${cls.padEnd(6)} ${s.n} 条 · 均值 ${s.avg} · 区间 ${s.min}~${s.max}`),
|
|
223
|
+
` 沉底(<${FLOOR}) 仍在库,只是排最后,不删`,
|
|
224
|
+
];
|
|
225
|
+
return lines.join('\n');
|
|
226
|
+
}
|