nebula-memory-mcp 1.3.1 → 1.4.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 CHANGED
@@ -153,7 +153,11 @@ agent-memory(引擎真源,9 个模块) ──人工对齐──▶ 本
153
153
 
154
154
  | 上游 `agent-memory` | **本仓库** | `nebula-workbuddy-mcp` | 备注 |
155
155
  |---|---|---|---|
156
- | **2.4.0**(+ Sleep Cycle) | **1.3.1** | 1.0.10(待同步) | 引擎与上游 2.4.0 对齐 |
156
+ | **2.8.1**(+ 拼音索引瘦身) | **1.4.1** | 1.1.1(已同步) | `index.json` 不再写 py token;总数据增量约 +22MB(+37%),长文本预算更公平 |
157
+ | **2.8.0**(+ 本地拼音检索) | **1.4.0** | 1.1.0(已同步) | 中文记忆可按全拼/声母/模糊音检索(`pinyinIndex` 可选开启),引擎与上游 2.8.0 对齐 |
158
+ | **2.7.1**(+ 蒸馏压测修复) | **1.3.4** | 1.0.13(已同步) | 高频词写入 O(N²)、并发快照 EPERM 修复,引擎与上游 2.7.1 对齐 |
159
+ | **2.7.0**(+ 可解释召回) | **1.3.3** | 1.0.12(已同步) | `recall{strategy}` + `explain_recall`,引擎与上游 2.7.0 对齐 |
160
+ | **2.5.0**(+ 冲突裁决) | **1.3.2** | 1.0.11(已同步) | 引擎与上游 2.5.0 对齐 |
157
161
 
158
162
  ## License
159
163
 
package/mcp-server.mjs CHANGED
@@ -45,7 +45,7 @@ const DATA_DIR = path.resolve(
45
45
  const SERVER_INFO = {
46
46
  name: 'agent-memory',
47
47
  // 与 package.json 的 version 保持一致(发版时必须一起改,见 README「引擎来历与同步」)
48
- version: '1.3.1',
48
+ version: '1.4.1',
49
49
  }
50
50
 
51
51
  // MCP 协议版本(当前规范)
@@ -207,17 +207,35 @@ const TOOLS = [
207
207
  },
208
208
  {
209
209
  name: 'recall',
210
- description: '召回与 query 相关的记忆。本地四臂检索,返回按相关度排序的记忆列表。',
210
+ description: '召回与 query 相关的记忆。本地四臂检索,返回按相关度排序的记忆列表。默认不返回「已被新陈述取代」的旧记忆(include_superseded=true 可查历史)。strategy 可选:lex=纯词法、graph=词法×图、hybrid=四臂融合(默认);explain=true 时返回打分拆解与命中词。',
211
211
  inputSchema: {
212
212
  type: 'object',
213
213
  properties: {
214
214
  query: { type: 'string', description: '要检索的内容' },
215
215
  limit: { type: 'number', description: '最多返回条数,默认 6' },
216
+ include_superseded: { type: 'boolean', description: '是否包含已被新陈述取代的旧记忆(默认 false)' },
217
+ strategy: { type: 'string', enum: ['lex', 'graph', 'hybrid'], description: '召回策略,默认 hybrid(四臂融合)' },
218
+ explain: { type: 'boolean', description: 'true = 返回打分拆解(parts)与命中词(matchedTerms),默认 false' },
216
219
  namespace: { type: 'string', description: '目标记忆库(可选)。不填=默认单库;填了只在该子库内检索。' },
217
220
  },
218
221
  required: ['query'],
219
222
  },
220
223
  },
224
+ {
225
+ name: 'explain_recall',
226
+ description: '解释召回:返回 query 命中的记忆,以及每条为什么被召回——词法得分、时间衰减、重要性、图权重各自贡献多少,命中了哪些词。可选 id 只看某一条。',
227
+ inputSchema: {
228
+ type: 'object',
229
+ properties: {
230
+ query: { type: 'string', description: '要检索的内容' },
231
+ id: { type: 'string', description: '只看这一条记忆(可选)' },
232
+ limit: { type: 'number', description: '最多分析条数,默认 6' },
233
+ strategy: { type: 'string', enum: ['lex', 'graph', 'hybrid'], description: '按哪种策略解释,默认 hybrid' },
234
+ namespace: { type: 'string', description: '目标记忆库(可选)。不填=默认单库。' },
235
+ },
236
+ required: ['query'],
237
+ },
238
+ },
221
239
  {
222
240
  name: 'stats',
223
241
  description: '返回当前记忆库的统计信息(节点数、边数等)。',
@@ -255,6 +273,26 @@ const TOOLS = [
255
273
 
256
274
  // ── 工具执行 ─────────────────────────────────────────────────────────────
257
275
  // defaultMem:默认单库实例;args.namespace 传了就切到对应子库。
276
+
277
+ function normalizeStrategy(v) {
278
+ if (v == null) return 'hybrid'
279
+ const s = String(v).toLowerCase()
280
+ if (!['lex', 'graph', 'hybrid'].includes(s)) {
281
+ throw new Error(`invalid strategy: ${v}(支持 lex | graph | hybrid)`)
282
+ }
283
+ return s
284
+ }
285
+
286
+ function explainLine(h, i) {
287
+ const p = h.parts || {}
288
+ const terms = (h.matchedTerms || []).map((t) => `${t.term}(${t.weight})`).join(', ')
289
+ return [
290
+ `${i + 1}. [${h.kind}] ${h.text} (score ${h.score})`,
291
+ ` 词法 ${p.lexical} × 衰减 ${p.decay} × 重要性 ${p.importance} × 图 ${p.graph}`,
292
+ ` 命中词: ${terms || '(无)'}`,
293
+ ].join('\n')
294
+ }
295
+
258
296
  async function callTool(name, args, defaultMem) {
259
297
  const { ns, from } = resolveNamespace(args.namespace)
260
298
  const mem = ns == null ? defaultMem : await getMemory(ns)
@@ -275,12 +313,48 @@ async function callTool(name, args, defaultMem) {
275
313
  case 'recall': {
276
314
  const query = String(args.query || '')
277
315
  const limit = Math.max(1, Math.min(50, Number(args.limit) || 6))
278
- const hits = await mem.recall(query, { limit })
279
- const text = hits.length
280
- ? hits.map((h, i) => `${i + 1}. [${h.kind}] ${h.text} (score ${h.score})`).join('\n')
281
- : '(无匹配记忆)'
316
+ const strategy = normalizeStrategy(args.strategy)
317
+ const explain = args.explain === true
318
+ const hits = await mem.recall(query, {
319
+ limit,
320
+ strategy,
321
+ debug: explain,
322
+ includeSuperseded: args.include_superseded === true,
323
+ })
324
+ if (!hits.length) return { content: [{ type: 'text', text: '(无匹配记忆)' }] }
325
+ if (explain) {
326
+ const payload = hits.map((h) => ({
327
+ id: h.id,
328
+ kind: h.kind,
329
+ text: h.text,
330
+ score: h.score,
331
+ strategy: h.strategy,
332
+ parts: h.parts,
333
+ matchedTerms: h.matchedTerms,
334
+ }))
335
+ return { content: [{ type: 'text', text: JSON.stringify(payload) }] }
336
+ }
337
+ const text = hits.map((h, i) => `${i + 1}. [${h.kind}] ${h.text} (score ${h.score})`).join('\n')
282
338
  return { content: [{ type: 'text', text }] }
283
339
  }
340
+ case 'explain_recall': {
341
+ const query = String(args.query || '')
342
+ const limit = Math.max(1, Math.min(50, Number(args.limit) || 6))
343
+ const strategy = normalizeStrategy(args.strategy)
344
+ let hits = await mem.recall(query, { limit, strategy, debug: true })
345
+ if (args.id) hits = hits.filter((h) => h.id === String(args.id))
346
+ if (!hits.length) {
347
+ return { content: [{ type: 'text', text: '(没有命中:query 与记忆无共同词,或候选都因 dormant/archived/冲突未召回)' }] }
348
+ }
349
+ const note = strategy === 'lex'
350
+ ? '(lex:仅词法参与打分)'
351
+ : strategy === 'graph'
352
+ ? '(graph:词法 × 图权重,衰减/重要性不参与)'
353
+ : '(hybrid:四臂融合)'
354
+ const lines = [`查询 "${query}" strategy=${strategy}${note},命中 ${hits.length} 条:`, '']
355
+ hits.forEach((h, i) => { lines.push(explainLine(h, i), '') })
356
+ return { content: [{ type: 'text', text: lines.join('\n').trimEnd() }] }
357
+ }
284
358
  case 'stats': {
285
359
  const s = await mem.stats()
286
360
  return { content: [{ type: 'text', text: JSON.stringify({ ...s, namespace: ns || '(default)', namespaceFrom: from }) }] }
@@ -0,0 +1,121 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * conflict.cjs —— 谓词级冲突识别(纯规则,零依赖)
5
+ *
6
+ * 解决的是"记忆打架":昨天说「我喜欢 TypeScript」,今天说「我讨厌 TypeScript」,
7
+ * 两条都留在库里,召回时 AI 会精神分裂。
8
+ *
9
+ * 做法:从正文里抽一个极窄的「(主体, 谓词类, 极性)」三元组;只有
10
+ * - 主体归一化后完全相同
11
+ * - 谓词类相同
12
+ * - 极性相反(或新句是「改用/换成」这类更新语义)
13
+ * 才判定为冲突。**宁可漏判,不可误判** —— 误判会让一条真实记忆被静默降权。
14
+ *
15
+ * 已知不处理的(按设计):
16
+ * - 否定套否定(「我不是不喜欢」):TRAP 直接放弃整句
17
+ * - 无主语 / 主语是代词(「我喜欢它」):不提 claim
18
+ * - 中文之外的语言:词表只覆盖中英文常见极性表达
19
+ */
20
+
21
+ /** 否定套否定:这类句子语义歧义,直接不解析 */
22
+ const TRAP = /不是不|并非不|没有不|算不上不|不能说.*不/;
23
+
24
+ /**
25
+ * 抽取规则。顺序敏感:负极性必须在正极性之前匹配
26
+ * (否则「不喜欢」会被「喜欢」吃掉);「改用/换成」优先于泛化的「用」。
27
+ */
28
+ const RULES = [
29
+ {
30
+ p: 'preference',
31
+ pol: '-',
32
+ re: /我(?:很|挺|更|最|特别|一直|现在|平时|确实)?(?:不喜欢|不爱|讨厌|反感|受不了|不习惯|不常用)\s*(.+)/,
33
+ },
34
+ {
35
+ p: 'preference',
36
+ pol: '+',
37
+ re: /我(?:很|挺|更|最|特别|一直|现在|平时|确实)?(?:喜欢|偏爱|爱用|习惯用|习惯|常用|倾向于|支持)\s*(.+)/,
38
+ },
39
+ {
40
+ p: 'usage',
41
+ pol: 'upd',
42
+ // 只认「我把 X 改成 Y」这种**宾语在动词前**的写法(主体是 X)。
43
+ // 「我现在用 Y」「改用 Y」不认:宾语在动词后,整句会被当成主体(实测线上库误判 297 条)。
44
+ re: /我把\s*([^\s,。;,.;`]{1,20})\s*(?:改用|换成|改成|换为|改为)/,
45
+ },
46
+ {
47
+ p: 'usage',
48
+ pol: '-',
49
+ re: /我(?:不|不再|别)(?:用|使用)\s*(.+)/,
50
+ },
51
+ {
52
+ p: 'usage',
53
+ pol: '+',
54
+ re: /我(?:一直|平时|现在|通常|一般)?(?:用|使用)\s*(.+)/,
55
+ },
56
+ ];
57
+
58
+ /** 主体清洗:去引号/反引号、去尾部语气词与标点 */
59
+ function cleanSubject(raw) {
60
+ let s = String(raw || '')
61
+ .split(/[,。;!?、,.!?;::\n]/)[0]
62
+ .trim()
63
+ .replace(/^[`"'“”‘’《》「」【】\s]+/, '')
64
+ .replace(/[`"'“”‘’《》「」【】\s]+$/, '');
65
+ // 去掉尾部常见语气/助词;去掉开头误吞的「了/的/过」
66
+ s = s.replace(/(?:了|的|吧|呢|吗|啊|呀|哦|嘛)+$/, '').trim();
67
+ s = s.replace(/^[了的过]+/, '').trim();
68
+ return s;
69
+ }
70
+
71
+ /** 主体归一化:比较用,不用于展示 */
72
+ function norm(s) {
73
+ return String(s || '')
74
+ .toLowerCase()
75
+ .replace(/[\s`"'“”‘’《》「」【】]/g, '')
76
+ .replace(/(?:了|的)$/, '');
77
+ }
78
+
79
+ /** 主体是否可信:1~24 字,不能是代词/空壳 */
80
+ const BAD_SUBJECTS = new Set(['它', '他', '她', '这个', '那个', '这些', '那些', '这样', '那样', '这种', '那种']);
81
+
82
+ /**
83
+ * 从一条正文里抽 claim。
84
+ * @param {string} text
85
+ * @returns {{s:string, p:string, pol:string}|null}
86
+ */
87
+ function parseClaim(text) {
88
+ const t = String(text || '').trim();
89
+ if (!t || t.length > 400) return null;
90
+ if (TRAP.test(t)) return null;
91
+ for (const rule of RULES) {
92
+ const m = t.match(rule.re);
93
+ if (!m) continue;
94
+ const s = cleanSubject(m[1]);
95
+ // 时长/数量词不当主体(「我用了三天才搞定」不是在使用某个工具)
96
+ if (/^[0-9一二三四五六七八九十百千万两]+(?:天|日|小时|分钟|秒|周|星期|月|年|次|遍|个|位|岁)/.test(s)) continue;
97
+ if (!s || s.length > 24) continue;
98
+ if (BAD_SUBJECTS.has(s)) continue;
99
+ // 主体里必须至少有一个字母/数字/汉字,纯标点不算
100
+ if (!/[A-Za-z0-9\u4e00-\u9fa5]/.test(s)) continue;
101
+ // 引号残留 / 疑问词 / 情态词:说明抓到的是一段话而不是一个「东西」
102
+ if (/[」』》】"”’]/.test(s)) continue;
103
+ if (/(?:什么|怎么|为什么|是否|可以|应该|要不要|能|会|吗|呢)/.test(s)) continue;
104
+ return { s, p: rule.p, pol: rule.pol };
105
+ }
106
+ return null;
107
+ }
108
+
109
+ /**
110
+ * 两条 claim 是否冲突。
111
+ * @returns {boolean}
112
+ */
113
+ function isConflict(oldClaim, newClaim) {
114
+ if (!oldClaim || !newClaim) return false;
115
+ if (oldClaim.p !== newClaim.p) return false;
116
+ if (norm(oldClaim.s) !== norm(newClaim.s)) return false;
117
+ if (oldClaim.pol === newClaim.pol) return false;
118
+ return true;
119
+ }
120
+
121
+ module.exports = { parseClaim, isConflict, norm, cleanSubject, TRAP };