nebula-memory-mcp 1.2.0 → 1.2.2

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.
@@ -0,0 +1,109 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * profile-recall.cjs — 画像反哺查询(个人化问题的语义补偿)
5
+ *
6
+ * 问题:用户问「我孩子多大了?」,答案在画像记忆里(「孩子上二年级」),
7
+ * 但 query 与记忆的词面几乎不重叠,bigram 召不回。
8
+ *
9
+ * 解法:**用已有的画像/摘要节点反哺 query** ——
10
+ * 1. 找出库里的 profile 层节点(kind=profile 或 layer=summary)
11
+ * 2. 检查 query 是否命中「个人化询问」模式(我/我的/孩子/偏好/习惯…)
12
+ * 3. 命中则把画像节点的关键词拼进检索 token,让相关记忆浮上来
13
+ *
14
+ * 实测依据(2026-11-23):far 档(纯语义)本地只有 33.3%,
15
+ * 其中「我孩子多大了」「我平时喝咖啡有什么讲究」这类个人问题占比高。
16
+ *
17
+ * 设计原则:
18
+ * - 只对**个人化提问**生效,避免污染普通查询
19
+ * - 反哺词有上限(默认 12 个),防止 query 被画像淹没
20
+ * - 可关闭:options.profileBoost === false
21
+ */
22
+
23
+ /** 个人化询问的触发模式(问「关于我」的事) */
24
+ const PERSONAL_PATTERNS = [
25
+ /我(的)?(孩子|儿子|女儿|小朋友|娃|小孩)/,
26
+ /我(平时|平常|周末|日常)/,
27
+ /我(喜欢|偏好|习惯|爱好)/,
28
+ /我(用|在|住|去)/,
29
+ /我(的)?(账号|密码|凭据|密钥|key)/,
30
+ /我(有|要|得)(什么|哪些)/,
31
+ /(孩子|儿子|女儿|小朋友|娃)(多大|几岁|在哪|喜欢什么)/,
32
+ /(我的)?(偏好|习惯|爱好)(是什么|有哪些)/,
33
+ /(我家|公司|团队)(的)?/,
34
+ ];
35
+
36
+ /** 判断 query 是否是个人化提问 */
37
+ function isPersonalQuery(query) {
38
+ if (!query) return false;
39
+ const q = String(query);
40
+ return PERSONAL_PATTERNS.some((re) => re.test(q));
41
+ }
42
+
43
+ /**
44
+ * 收集库里的画像/摘要节点文本(最多 N 条,按 importance 降序)。
45
+ * @param {any} mem NebulaMemory 实例
46
+ * @param {number} [maxNodes=3]
47
+ */
48
+ function collectProfileNodes(mem, maxNodes = 3) {
49
+ const out = [];
50
+ for (const node of mem.nodes.values()) {
51
+ if (!node || node.deleted) continue;
52
+ const isProfile = node.kind === 'profile' || node.layer === 'summary'
53
+ || (node.meta && node.meta.summary)
54
+ || (node.kind === 'fact' && Number(node.importance) >= 0.9);
55
+ if (!isProfile) continue;
56
+ out.push(node);
57
+ }
58
+ out.sort((a, b) => Number(b.importance || 0) - Number(a.importance || 0) || b.ts - a.ts);
59
+ return out.slice(0, maxNodes);
60
+ }
61
+
62
+ /**
63
+ * 为个人化 query 生成反哺词。
64
+ *
65
+ * @param {any} mem
66
+ * @param {string} query
67
+ * @param {{maxTokens?:number, maxNodes?:number, enabled?:number}} [opts]
68
+ * @returns {{boosted:boolean, reason:string, tokens:string[]}}
69
+ */
70
+ function boostPersonalQuery(mem, query, opts = {}) {
71
+ const enabled = opts.enabled !== false;
72
+ const maxTokens = Math.max(1, Math.floor(opts.maxTokens == null ? 12 : opts.maxTokens));
73
+ const maxNodes = Math.max(1, Math.floor(opts.maxNodes == null ? 3 : opts.maxNodes));
74
+
75
+ if (!enabled) return { boosted: false, reason: 'disabled', tokens: [] };
76
+ if (!isPersonalQuery(query)) return { boosted: false, reason: 'not-personal', tokens: [] };
77
+
78
+ const nodes = collectProfileNodes(mem, maxNodes);
79
+ if (!nodes.length) return { boosted: false, reason: 'no-profile', tokens: [] };
80
+
81
+ // 从画像节点文本里抽关键词(用简单的二元切分 + 过滤停用词)
82
+ const STOP = new Set(['的', '了', '是', '在', '有', '和', '与', '我', '你', '他', '她', '这', '那', '就', '也', '都', '很', '个', '上', '下', '中', '为', '对', '从', '到', '把', '被', '让', '给', '但', '而', '或', '如果', '因为', '所以', '一个', '我们', '你们', '他们']);
83
+ const tokens = [];
84
+ const seen = new Set();
85
+ for (const node of nodes) {
86
+ const text = String(node.text || '');
87
+ // 中文按 2-4 字滑窗取词组(比单字有区分度,比整句更可匹配)
88
+ for (const seg of text.split(/[,。;:、\s\n|()()\[\]【】"'"'']+/)) {
89
+ if (seg.length < 2) continue;
90
+ // 取 2 字、3 字、4 字片段(去重)
91
+ for (const len of [4, 3, 2]) {
92
+ for (let i = 0; i + len <= seg.length; i++) {
93
+ const tk = seg.slice(i, i + len);
94
+ if (STOP.has(tk) || seen.has(tk)) continue;
95
+ // 过滤纯数字/纯标点
96
+ if (!/[\u4e00-\u9fa5A-Za-z]/.test(tk)) continue;
97
+ seen.add(tk);
98
+ tokens.push(tk);
99
+ if (tokens.length >= maxTokens) {
100
+ return { boosted: true, reason: 'personal', tokens };
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+ return { boosted: tokens.length > 0, reason: 'personal', tokens };
107
+ }
108
+
109
+ module.exports = { isPersonalQuery, collectProfileNodes, boostPersonalQuery, PERSONAL_PATTERNS };