@morningljn/mnemo 0.1.3 → 0.1.4
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 +43 -14
- package/dist/init.js +16 -8
- package/dist/init.js.map +1 -1
- package/dist/refine.d.ts +14 -0
- package/dist/refine.js +115 -0
- package/dist/refine.js.map +1 -0
- package/dist/resources.d.ts +27 -0
- package/dist/resources.js +56 -0
- package/dist/resources.js.map +1 -0
- package/dist/retriever.d.ts +3 -1
- package/dist/retriever.js +38 -26
- package/dist/retriever.js.map +1 -1
- package/dist/server.js +7 -0
- package/dist/server.js.map +1 -1
- package/docs/superpowers/plans/2026-05-15-mnemo-mcp.md +1154 -0
- package/docs/superpowers/plans/2026-05-16-mnemo-query-cache.md +613 -0
- package/docs/superpowers/plans/2026-05-16-retrieval-and-injection-optimization.md +770 -0
- package/openspec/changes/archive/2026-05-15-mnemo-mcp/.openspec.yaml +2 -0
- package/openspec/changes/archive/2026-05-15-mnemo-mcp/design.md +83 -0
- package/openspec/changes/archive/2026-05-15-mnemo-mcp/proposal.md +32 -0
- package/openspec/changes/archive/2026-05-15-mnemo-mcp/specs/fact-retrieval/spec.md +75 -0
- package/openspec/changes/archive/2026-05-15-mnemo-mcp/specs/fact-store/spec.md +83 -0
- package/openspec/changes/archive/2026-05-15-mnemo-mcp/specs/mcp-server/spec.md +34 -0
- package/openspec/changes/archive/2026-05-15-mnemo-mcp/specs/security/spec.md +37 -0
- package/openspec/changes/archive/2026-05-15-mnemo-mcp/tasks.md +44 -0
- package/openspec/changes/archive/2026-05-16-mnemo-query-cache/.openspec.yaml +2 -0
- package/openspec/changes/archive/2026-05-16-mnemo-query-cache/design.md +96 -0
- package/openspec/changes/archive/2026-05-16-mnemo-query-cache/proposal.md +29 -0
- package/openspec/changes/archive/2026-05-16-mnemo-query-cache/specs/batch-operations/spec.md +42 -0
- package/openspec/changes/archive/2026-05-16-mnemo-query-cache/specs/perf-metrics/spec.md +55 -0
- package/openspec/changes/archive/2026-05-16-mnemo-query-cache/specs/query-cache/spec.md +65 -0
- package/openspec/changes/archive/2026-05-16-mnemo-query-cache/tasks.md +45 -0
- package/openspec/changes/retrieval-and-injection-optimization/.openspec.yaml +2 -0
- package/openspec/changes/retrieval-and-injection-optimization/design.md +117 -0
- package/openspec/changes/retrieval-and-injection-optimization/proposal.md +30 -0
- package/openspec/changes/retrieval-and-injection-optimization/specs/adaptive-scoring/spec.md +43 -0
- package/openspec/changes/retrieval-and-injection-optimization/specs/injection-protocol/spec.md +48 -0
- package/openspec/changes/retrieval-and-injection-optimization/specs/mcp-resources/spec.md +39 -0
- package/openspec/changes/retrieval-and-injection-optimization/specs/query-refinement/spec.md +39 -0
- package/openspec/changes/retrieval-and-injection-optimization/tasks.md +33 -0
- package/openspec/config.yaml +20 -0
- package/package.json +1 -1
- package/src/init.ts +17 -9
- package/src/refine.ts +127 -0
- package/src/resources.ts +78 -0
- package/src/retriever.ts +40 -26
- package/src/server.ts +8 -0
- package/tests/refine.test.ts +52 -0
- package/tests/resource.test.ts +62 -0
package/README.md
CHANGED
|
@@ -15,6 +15,8 @@ AI coding assistants forget everything between sessions. `CLAUDE.md` stores stat
|
|
|
15
15
|
mnemo gives your AI assistant a **searchable, structured memory layer** that persists across sessions:
|
|
16
16
|
|
|
17
17
|
- **Search by meaning** — FTS5 full-text search + Jaccard reranking + bilingual expansion
|
|
18
|
+
- **Session warmup** — MCP Resources auto-inject top facts at session start, zero tool calls
|
|
19
|
+
- **Query refinement** — strips action words and noise tokens before memory search
|
|
18
20
|
- **Trust scoring** — facts gain or lose trust over time based on feedback and decay
|
|
19
21
|
- **Entity graph** — automatic entity extraction with multi-hop relationship queries
|
|
20
22
|
- **Contradiction detection** — finds conflicting facts and demotes the older one
|
|
@@ -49,15 +51,26 @@ claude mcp add mnemo -- mnemo
|
|
|
49
51
|
|
|
50
52
|
You have mnemo memory tools (fact_store / fact_feedback). Rules:
|
|
51
53
|
|
|
52
|
-
## Rule 1:
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
## Rule 1: Session warmup (automatic)
|
|
55
|
+
mnemo MCP Resources auto-inject global memory into system context at session start.
|
|
56
|
+
You do NOT need to call fact_store(search) for high-frequency memories.
|
|
55
57
|
|
|
56
|
-
## Rule 2:
|
|
58
|
+
## Rule 2: On-demand search
|
|
59
|
+
Only call fact_store(action="search") when:
|
|
60
|
+
- User message involves personal preferences/habits/tool choices not covered by warmup
|
|
61
|
+
- User explicitly queries memory ("what did I say before", "per my habits")
|
|
62
|
+
- Technical decisions need user preference confirmation
|
|
63
|
+
|
|
64
|
+
Do NOT trigger search for:
|
|
65
|
+
- Pure operations ("run tests", "git commit")
|
|
66
|
+
- General tech questions ("how to use Promise")
|
|
67
|
+
- Code review/explanation requests
|
|
68
|
+
|
|
69
|
+
## Rule 3: Write on request
|
|
57
70
|
When user says "remember", call `fact_store(action="add", content="...", category="...")`.
|
|
58
71
|
Search first to avoid duplicates. Categories: identity / coding_style / tool_pref / workflow / general.
|
|
59
72
|
|
|
60
|
-
## Rule
|
|
73
|
+
## Rule 4: Feedback
|
|
61
74
|
When a memory was useful, call `fact_feedback(action="helpful", fact_id=...)`.
|
|
62
75
|
```
|
|
63
76
|
|
|
@@ -115,21 +128,37 @@ Rate a fact after use. Good facts rise, bad facts decay.
|
|
|
115
128
|
| `helpful` | +0.05 trust |
|
|
116
129
|
| `unhelpful` | -0.10 trust |
|
|
117
130
|
|
|
131
|
+
## MCP Resources
|
|
132
|
+
|
|
133
|
+
mnemo exposes 5 global category resources for **zero-cost session warmup**:
|
|
134
|
+
|
|
135
|
+
| Resource URI | Description |
|
|
136
|
+
|-------------|-------------|
|
|
137
|
+
| `mnemo://global/identity` | Identity facts (top 10 by trust) |
|
|
138
|
+
| `mnemo://global/coding_style` | Coding style preferences |
|
|
139
|
+
| `mnemo://global/tool_pref` | Tool preferences |
|
|
140
|
+
| `mnemo://global/workflow` | Workflow preferences |
|
|
141
|
+
| `mnemo://global/general` | General facts |
|
|
142
|
+
|
|
143
|
+
MCP clients (Claude Code, Codex) automatically fetch these resources at session start, injecting memory into system context without any tool calls. This eliminates the need for "search every message" patterns.
|
|
144
|
+
|
|
118
145
|
## Architecture
|
|
119
146
|
|
|
120
147
|
```
|
|
121
148
|
┌───────────────────┐ stdio ┌────────────┐ SQLite ┌─────────────────────┐
|
|
122
149
|
│ MCP Client │◄─────────►│ mnemo │◄───────────►│ ~/.mnemo/facts.db │
|
|
123
150
|
│ (Claude / Codex) │ JSON │ server │ │ │
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
151
|
+
│ │ └─────┬──────┘ │ Tables: │
|
|
152
|
+
│ Auto-fetch: │ │ │ facts │
|
|
153
|
+
│ mnemo://global/* │ ┌──────────┼──────────┐ │ entities │
|
|
154
|
+
│ (session warmup) │ │ │ │ │ fact_entities │
|
|
155
|
+
└───────────────────┘ │ │ │ │ Indexes: │
|
|
156
|
+
│ │ │ │ facts_fts (FTS5) │
|
|
157
|
+
Resources Retriever Security │ idx_facts_trust │
|
|
158
|
+
(warmup, (search, (PII scan, │ idx_facts_category│
|
|
159
|
+
cache) probe, injection └─────────────────────┘
|
|
160
|
+
reason) detection)
|
|
161
|
+
└──────────────────┘
|
|
133
162
|
```
|
|
134
163
|
|
|
135
164
|
## Categories
|
package/dist/init.js
CHANGED
|
@@ -19,19 +19,27 @@ const MEMORY_RULES = `
|
|
|
19
19
|
|
|
20
20
|
你有 mnemo 记忆工具(fact_store / fact_feedback),必须按以下规则使用:
|
|
21
21
|
|
|
22
|
-
## 规则 1
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
- 用户说"我叫什么" → query="姓名 名字 叫什么"
|
|
26
|
-
- 用户说"我喜欢什么语言" → query="编程语言 偏好 喜欢"
|
|
27
|
-
搜索后根据结果自然回复,不要说"根据记忆"。
|
|
22
|
+
## 规则 1:会话预热(自动)
|
|
23
|
+
会话启动时,mnemo-mcp 的 MCP Resource 会自动注入全局记忆到 system context。
|
|
24
|
+
你不需要主动调用 fact_store(search) 来获取高频记忆。
|
|
28
25
|
|
|
29
|
-
## 规则 2
|
|
26
|
+
## 规则 2:按需补充查询
|
|
27
|
+
仅在以下情况调用 \`fact_store(action="search")\`:
|
|
28
|
+
- 用户消息涉及个人偏好/习惯/工具选择且预热中未覆盖
|
|
29
|
+
- 用户明确查询记忆("我之前说过什么""按我的习惯")
|
|
30
|
+
- 技术选型时需要确认用户偏好
|
|
31
|
+
|
|
32
|
+
不触发查询的情况:
|
|
33
|
+
- 纯操作指令("运行测试""git commit")
|
|
34
|
+
- 通用技术问题("Promise 怎么用")
|
|
35
|
+
- 代码审查/解释请求
|
|
36
|
+
|
|
37
|
+
## 规则 3:写入记忆
|
|
30
38
|
用户说"记住"、"记下来"时,调用 \`fact_store(action="add", content="...", category="...")\`。
|
|
31
39
|
- 先 search 检查是否已有相似事实,有则 update
|
|
32
40
|
- category:identity / coding_style / tool_pref / workflow / general
|
|
33
41
|
|
|
34
|
-
## 规则
|
|
42
|
+
## 规则 4:反馈强化
|
|
35
43
|
成功使用某条记忆时,调用 \`fact_feedback(action="helpful", fact_id=...)\`。
|
|
36
44
|
`;
|
|
37
45
|
const MCP_TOOLS = [
|
package/dist/init.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.js","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":";AAEA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAC5E,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAEjC,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE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I,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;IACtE,EAAE,CAAC,OAAO,KAAK,WAAW,aAAa,EAAE,CAAC,CAAA;AAC5C,CAAC;AAED,aAAa;AACb,SAAS,IAAI;IACX,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IAClC,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAA;IAE7D,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IACf,OAAO,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAA;IACpD,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IAEf,WAAW,CAAC,KAAK,CAAC,CAAA;IAClB,aAAa,EAAE,CAAA;IACf,iBAAiB,EAAE,CAAA;IAEnB,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IACf,EAAE,CAAC,+BAA+B,CAAC,CAAA;IACnC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;AACjB,CAAC;AAED,IAAI,EAAE,CAAA"}
|
package/dist/refine.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query refinement: strip noise tokens from user messages before memory search.
|
|
3
|
+
* Pure function — no side effects, no DB access.
|
|
4
|
+
*/
|
|
5
|
+
export interface RefineResult {
|
|
6
|
+
query: string | null;
|
|
7
|
+
tokens: string[];
|
|
8
|
+
entityTokens: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Refine a raw user message into memory-searchable keywords.
|
|
12
|
+
* Returns null if the message is a pure operation command with no memory relevance.
|
|
13
|
+
*/
|
|
14
|
+
export declare function refineQuery(raw: string): RefineResult | null;
|
package/dist/refine.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query refinement: strip noise tokens from user messages before memory search.
|
|
3
|
+
* Pure function — no side effects, no DB access.
|
|
4
|
+
*/
|
|
5
|
+
// Action words / helper phrases to strip (Chinese)
|
|
6
|
+
const ACTION_WORDS = [
|
|
7
|
+
'帮我看看', '能不能帮我', '给我看看',
|
|
8
|
+
'帮我', '看看', '看一下', '做一下', '能不能', '为什么', '怎么',
|
|
9
|
+
'是什么', '如何', '请', '麻烦', '可以', '给我',
|
|
10
|
+
'给我做', '给我写', '给我查', '给我找', '给我说', '给我讲',
|
|
11
|
+
'告诉我', '跟我说', '跟我讲', '给我解释', '给我说明', '给我介绍',
|
|
12
|
+
'运行', '执行', '启动', '停止', '创建', '删除', '修改', '更新', '查看',
|
|
13
|
+
'检查', '测试', '提交', '推送', '拉取', '合并', '切换', '重置', '重构',
|
|
14
|
+
'运行测试', '创建文件',
|
|
15
|
+
];
|
|
16
|
+
// Common CLI commands / low-signal English tokens to filter
|
|
17
|
+
const NOISE_WORDS = new Set([
|
|
18
|
+
'git', 'npm', 'npx', 'yarn', 'pnpm', 'status', 'log', 'diff', 'add',
|
|
19
|
+
'commit', 'push', 'pull', 'merge', 'checkout', 'branch', 'stash',
|
|
20
|
+
'install', 'build', 'run', 'start', 'stop', 'test', 'lint', 'format',
|
|
21
|
+
]);
|
|
22
|
+
// Sort by length descending so longer phrases match first during replacement
|
|
23
|
+
const ACTION_WORDS_SORTED = [...ACTION_WORDS].sort((a, b) => b.length - a.length);
|
|
24
|
+
const ACTION_WORDS_SET = new Set(ACTION_WORDS);
|
|
25
|
+
// Reuse existing stop words from retriever
|
|
26
|
+
const CN_STOP_WORDS = new Set([
|
|
27
|
+
'的', '了', '是', '在', '有', '和', '就', '不', '人', '都',
|
|
28
|
+
'一', '个', '上', '也', '很', '到', '说', '要', '去', '你',
|
|
29
|
+
'会', '着', '没', '看', '好', '自', '这', '他', '她', '它',
|
|
30
|
+
'那', '些', '用', '对', '下', '为', '从', '被', '把', '能',
|
|
31
|
+
'可', '以', '所', '而', '又', '与', '但', '或', '等', '中',
|
|
32
|
+
'大', '小', '多', '少', '其', '之', '做', '让', '给', '已',
|
|
33
|
+
'还', '来', '地', '得', '过', '时', '里', '后', '前', '当',
|
|
34
|
+
]);
|
|
35
|
+
/**
|
|
36
|
+
* Refine a raw user message into memory-searchable keywords.
|
|
37
|
+
* Returns null if the message is a pure operation command with no memory relevance.
|
|
38
|
+
*/
|
|
39
|
+
export function refineQuery(raw) {
|
|
40
|
+
const trimmed = raw.trim();
|
|
41
|
+
if (!trimmed)
|
|
42
|
+
return null;
|
|
43
|
+
// Extract high-signal tokens first: quoted content, book titles, capitalized phrases
|
|
44
|
+
const entityTokens = [];
|
|
45
|
+
// Chinese quotes: 「深色主题」 or "深色主题" or '深色主题'
|
|
46
|
+
for (const m of trimmed.matchAll(/[「""'']([^「""''」]{2,20})[」""'']/g)) {
|
|
47
|
+
entityTokens.push(m[1]);
|
|
48
|
+
}
|
|
49
|
+
// Book titles: 《记忆系统》
|
|
50
|
+
for (const m of trimmed.matchAll(/《([^》]+)》/g)) {
|
|
51
|
+
entityTokens.push(m[1]);
|
|
52
|
+
}
|
|
53
|
+
// Capitalized English phrases: "TypeScript", "Visual Studio Code"
|
|
54
|
+
for (const m of trimmed.matchAll(/\b([A-Z][a-zA-Z]*(?:\s+[A-Z][a-zA-Z]*)+)\b/g)) {
|
|
55
|
+
entityTokens.push(m[1]);
|
|
56
|
+
}
|
|
57
|
+
// Tokenize: split by spaces and Chinese character boundaries
|
|
58
|
+
const tokens = [];
|
|
59
|
+
const parts = trimmed.split(/\s+/);
|
|
60
|
+
for (const part of parts) {
|
|
61
|
+
// English words
|
|
62
|
+
for (const word of part.match(/[a-zA-Z0-9_\-.]+/g) ?? []) {
|
|
63
|
+
if (word.length >= 2)
|
|
64
|
+
tokens.push(word);
|
|
65
|
+
}
|
|
66
|
+
// For Chinese: strip action words first, then extract remaining chars
|
|
67
|
+
let cnText = part.replace(/[\u4e00-\u9fff]+/g, (seg) => {
|
|
68
|
+
let result = seg;
|
|
69
|
+
for (const aw of ACTION_WORDS_SORTED) {
|
|
70
|
+
result = result.replaceAll(aw, '');
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
});
|
|
74
|
+
const cnChars = cnText.match(/[\u4e00-\u9fff]/g) ?? [];
|
|
75
|
+
for (const c of cnChars) {
|
|
76
|
+
if (!CN_STOP_WORDS.has(c))
|
|
77
|
+
tokens.push(c);
|
|
78
|
+
}
|
|
79
|
+
// Chinese 2-grams for better matching
|
|
80
|
+
for (let i = 0; i < cnChars.length - 1; i++) {
|
|
81
|
+
const bigram = cnChars[i] + cnChars[i + 1];
|
|
82
|
+
tokens.push(bigram);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Filter stop words, noise, and short tokens
|
|
86
|
+
const filtered = tokens.filter(t => {
|
|
87
|
+
if (ACTION_WORDS_SET.has(t))
|
|
88
|
+
return false;
|
|
89
|
+
if (CN_STOP_WORDS.has(t))
|
|
90
|
+
return false;
|
|
91
|
+
if (NOISE_WORDS.has(t.toLowerCase()))
|
|
92
|
+
return false;
|
|
93
|
+
if (t.length < 2)
|
|
94
|
+
return false;
|
|
95
|
+
return true;
|
|
96
|
+
});
|
|
97
|
+
// Deduplicate while preserving order
|
|
98
|
+
const seen = new Set();
|
|
99
|
+
const deduped = [];
|
|
100
|
+
for (const t of filtered) {
|
|
101
|
+
if (!seen.has(t)) {
|
|
102
|
+
seen.add(t);
|
|
103
|
+
deduped.push(t);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// If nothing left after filtering, check if we have entity tokens
|
|
107
|
+
if (deduped.length === 0 && entityTokens.length === 0) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
// Combine: entity tokens first (higher signal), then deduped tokens
|
|
111
|
+
const allTokens = [...entityTokens, ...deduped.filter(t => !entityTokens.includes(t))];
|
|
112
|
+
const query = allTokens.join(' ');
|
|
113
|
+
return { query, tokens: deduped, entityTokens };
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=refine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refine.js","sourceRoot":"","sources":["../src/refine.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,mDAAmD;AACnD,MAAM,YAAY,GAAG;IACnB,MAAM,EAAE,OAAO,EAAE,MAAM;IACvB,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI;IAC5C,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IAClC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK;IACxC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAC3C,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IACpD,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IACpD,MAAM,EAAE,MAAM;CACf,CAAA;AAED,4DAA4D;AAC5D,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC;IAC1B,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK;IACnE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO;IAChE,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ;CACrE,CAAC,CAAA;AACF,6EAA6E;AAC7E,MAAM,mBAAmB,GAAG,CAAC,GAAG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C,CAAC,CAAC,EAAE,CAAC;YACjB,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;YACX,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACjB,CAAC;IACH,CAAC;IAED,kEAAkE;IAClE,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,oEAAoE;IACpE,MAAM,SAAS,GAAG,CAAC,GAAG,YAAY,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACtF,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IAEjC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,CAAA;AACjD,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Resource manager for mnemo-mcp.
|
|
3
|
+
* Exposes per-category memory summaries as MCP Resources for session warmup injection.
|
|
4
|
+
*/
|
|
5
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
6
|
+
import type { MemoryStore } from './store.js';
|
|
7
|
+
import type { FactCategory } from './types.js';
|
|
8
|
+
export interface ResourceFact {
|
|
9
|
+
fact_id: number;
|
|
10
|
+
content: string;
|
|
11
|
+
trust_score: number;
|
|
12
|
+
}
|
|
13
|
+
export declare class ResourceManager {
|
|
14
|
+
private store;
|
|
15
|
+
private cache;
|
|
16
|
+
constructor(store: MemoryStore);
|
|
17
|
+
/** Register all category resources with the MCP server */
|
|
18
|
+
registerResources(server: McpServer): void;
|
|
19
|
+
/** Read handler for a specific category */
|
|
20
|
+
private readCategory;
|
|
21
|
+
/** Get facts for a category — with caching */
|
|
22
|
+
getFacts(category: FactCategory): ResourceFact[];
|
|
23
|
+
/** Invalidate all caches — call after any write operation */
|
|
24
|
+
invalidate(): void;
|
|
25
|
+
/** Get cache entry count for debugging */
|
|
26
|
+
cacheSize(): number;
|
|
27
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Resource manager for mnemo-mcp.
|
|
3
|
+
* Exposes per-category memory summaries as MCP Resources for session warmup injection.
|
|
4
|
+
*/
|
|
5
|
+
const CATEGORIES = ['identity', 'coding_style', 'tool_pref', 'workflow', 'general'];
|
|
6
|
+
const RESOURCE_LIMIT = 10;
|
|
7
|
+
export class ResourceManager {
|
|
8
|
+
store;
|
|
9
|
+
cache = new Map();
|
|
10
|
+
constructor(store) {
|
|
11
|
+
this.store = store;
|
|
12
|
+
}
|
|
13
|
+
/** Register all category resources with the MCP server */
|
|
14
|
+
registerResources(server) {
|
|
15
|
+
for (const category of CATEGORIES) {
|
|
16
|
+
const uri = `mnemo://global/${category}`;
|
|
17
|
+
server.registerResource(`mnemo-global-${category}`, uri, {
|
|
18
|
+
description: `${category} category global facts (top ${RESOURCE_LIMIT} by trust)`,
|
|
19
|
+
mimeType: 'application/json',
|
|
20
|
+
}, async () => this.readCategory(category));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Read handler for a specific category */
|
|
24
|
+
readCategory(category) {
|
|
25
|
+
const facts = this.getFacts(category);
|
|
26
|
+
return {
|
|
27
|
+
contents: [{
|
|
28
|
+
uri: `mnemo://global/${category}`,
|
|
29
|
+
mimeType: 'application/json',
|
|
30
|
+
text: JSON.stringify(facts, null, 2),
|
|
31
|
+
}],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Get facts for a category — with caching */
|
|
35
|
+
getFacts(category) {
|
|
36
|
+
const cached = this.cache.get(category);
|
|
37
|
+
if (cached)
|
|
38
|
+
return cached;
|
|
39
|
+
const facts = this.store.listFacts(category, 0.0, RESOURCE_LIMIT).map(f => ({
|
|
40
|
+
fact_id: f.factId,
|
|
41
|
+
content: f.content,
|
|
42
|
+
trust_score: f.trustScore,
|
|
43
|
+
}));
|
|
44
|
+
this.cache.set(category, facts);
|
|
45
|
+
return facts;
|
|
46
|
+
}
|
|
47
|
+
/** Invalidate all caches — call after any write operation */
|
|
48
|
+
invalidate() {
|
|
49
|
+
this.cache.clear();
|
|
50
|
+
}
|
|
51
|
+
/** Get cache entry count for debugging */
|
|
52
|
+
cacheSize() {
|
|
53
|
+
return this.cache.size;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=resources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.js","sourceRoot":"","sources":["../src/resources.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH,MAAM,UAAU,GAAmB,CAAC,UAAU,EAAE,cAAc,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,CAAC,CAAA;AACnG,MAAM,cAAc,GAAG,EAAE,CAAA;AAQzB,MAAM,OAAO,eAAe;IAIhB;IAHF,KAAK,GAAG,IAAI,GAAG,EAAgC,CAAA;IAEvD,YACU,KAAkB;QAAlB,UAAK,GAAL,KAAK,CAAa;IACzB,CAAC;IAEJ,0DAA0D;IAC1D,iBAAiB,CAAC,MAAiB;QACjC,KAAK,MAAM,QAAQ,IAAI,UAAU,EAAE,CAAC;YAClC,MAAM,GAAG,GAAG,kBAAkB,QAAQ,EAAE,CAAA;YACxC,MAAM,CAAC,gBAAgB,CACrB,gBAAgB,QAAQ,EAAE,EAC1B,GAAG,EACH;gBACE,WAAW,EAAE,GAAG,QAAQ,+BAA+B,cAAc,YAAY;gBACjF,QAAQ,EAAE,kBAAkB;aAC7B,EACD,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CACxC,CAAA;QACH,CAAC;IACH,CAAC;IAED,2CAA2C;IACnC,YAAY,CAAC,QAAsB;QACzC,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;QACrC,OAAO;YACL,QAAQ,EAAE,CAAC;oBACT,GAAG,EAAE,kBAAkB,QAAQ,EAAE;oBACjC,QAAQ,EAAE,kBAAkB;oBAC5B,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;iBACrC,CAAC;SACH,CAAA;IACH,CAAC;IAED,8CAA8C;IAC9C,QAAQ,CAAC,QAAsB;QAC7B,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QACvC,IAAI,MAAM;YAAE,OAAO,MAAM,CAAA;QAEzB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,QAAQ,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YAC1E,OAAO,EAAE,CAAC,CAAC,MAAM;YACjB,OAAO,EAAE,CAAC,CAAC,OAAO;YAClB,WAAW,EAAE,CAAC,CAAC,UAAU;SAC1B,CAAC,CAAC,CAAA;QAEH,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;QAC/B,OAAO,KAAK,CAAA;IACd,CAAC;IAED,6DAA6D;IAC7D,UAAU;QACR,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;IACpB,CAAC;IAED,0CAA0C;IAC1C,SAAS;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAA;IACxB,CAAC;CACF"}
|
package/dist/retriever.d.ts
CHANGED
|
@@ -30,7 +30,9 @@ export declare class FactRetriever {
|
|
|
30
30
|
/** 获取性能指标实例(供调试接口使用) */
|
|
31
31
|
getMetrics(): PerfMetrics;
|
|
32
32
|
/** 主搜索:FTS5 → LIKE → 字符交叉 → 分类推断 → Jaccard → 信任评分 → 时间衰减 */
|
|
33
|
-
search(query: string, options?: SearchOptions
|
|
33
|
+
search(query: string, options?: SearchOptions & {
|
|
34
|
+
skipRefine?: boolean;
|
|
35
|
+
}): ScoredFact[];
|
|
34
36
|
/** 实体探测:查询某实体关联的所有事实 */
|
|
35
37
|
probe(entity: string, options?: SearchOptions): ScoredFact[];
|
|
36
38
|
/** 实体关联:查找与某实体共享上下文的其他事实 */
|
package/dist/retriever.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { QueryCache } from './cache.js';
|
|
10
10
|
import { PerfMetrics } from './metrics.js';
|
|
11
|
+
import { refineQuery } from './refine.js';
|
|
11
12
|
// 中文字符级匹配的虚词集合(这些单字太常见,不参与字符交叉匹配)
|
|
12
13
|
const CN_OVERLAP_STOP = new Set([
|
|
13
14
|
'的', '了', '是', '在', '有', '和', '就', '不', '人', '都',
|
|
@@ -55,15 +56,23 @@ export class FactRetriever {
|
|
|
55
56
|
const minTrust = options?.minTrust ?? 0.3;
|
|
56
57
|
const limit = options?.limit ?? 10;
|
|
57
58
|
const category = options?.category;
|
|
59
|
+
// 查询提炼(除非显式跳过)
|
|
60
|
+
let searchQuery = query;
|
|
61
|
+
if (!options?.skipRefine) {
|
|
62
|
+
const refined = refineQuery(query);
|
|
63
|
+
if (refined?.query) {
|
|
64
|
+
searchQuery = refined.query;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
58
67
|
// 缓存检查
|
|
59
|
-
const cacheKey = this.cache.makeKey({ action: 'search', query, category, minTrust, limit });
|
|
68
|
+
const cacheKey = this.cache.makeKey({ action: 'search', query: searchQuery, category, minTrust, limit });
|
|
60
69
|
const cached = this.cache.get(cacheKey);
|
|
61
70
|
if (cached) {
|
|
62
71
|
this.metrics.record({ action: 'search', durationMs: performance.now() - startTime, resultCount: cached.length, cacheHit: true });
|
|
63
72
|
return cached;
|
|
64
73
|
}
|
|
65
74
|
// 查询双语扩展:中文术语追加英文,英文术语追加中文
|
|
66
|
-
const expandedQuery = this.expandQueryBilingually(
|
|
75
|
+
const expandedQuery = this.expandQueryBilingually(searchQuery);
|
|
67
76
|
// Stage 1: FTS5 候选集,空时逐级 fallback(使用双语扩展后的查询)
|
|
68
77
|
let candidates = this.ftsCandidates(expandedQuery, category, minTrust, limit * 3);
|
|
69
78
|
if (candidates.length === 0) {
|
|
@@ -75,18 +84,22 @@ export class FactRetriever {
|
|
|
75
84
|
if (candidates.length === 0) {
|
|
76
85
|
// 分类推断 fallback(仅无 category 过滤时生效)
|
|
77
86
|
if (!category) {
|
|
78
|
-
const inferred = this.categoryInferFallback(
|
|
87
|
+
const inferred = this.categoryInferFallback(searchQuery, minTrust, limit);
|
|
79
88
|
if (inferred.length > 0)
|
|
80
89
|
return inferred;
|
|
81
90
|
}
|
|
82
91
|
// 个人/身份相关的短查询触发 trust fallback
|
|
83
|
-
if (this.isPersonalQuery(
|
|
92
|
+
if (this.isPersonalQuery(searchQuery)) {
|
|
84
93
|
return this.trustFallback(category, minTrust, limit);
|
|
85
94
|
}
|
|
86
95
|
return [];
|
|
87
96
|
}
|
|
88
97
|
// Stage 2-4: Jaccard 重排序 + 信任评分 + 时间衰减
|
|
89
|
-
|
|
98
|
+
// 动态权重:短查询偏 FTS,长查询偏 Jaccard
|
|
99
|
+
const queryTokens = this.tokenize(searchQuery);
|
|
100
|
+
const tokenCount = queryTokens.size;
|
|
101
|
+
const ftsWeight = tokenCount <= 3 ? 0.7 : 0.3;
|
|
102
|
+
const jaccardWeight = tokenCount <= 3 ? 0.3 : 0.7;
|
|
90
103
|
const scored = [];
|
|
91
104
|
for (const fact of candidates) {
|
|
92
105
|
const contentTokens = this.tokenize(fact.content);
|
|
@@ -99,7 +112,7 @@ export class FactRetriever {
|
|
|
99
112
|
const similarity = 0.3 * jaccard + 0.7 * qInF;
|
|
100
113
|
const ftsScore = fact.ftsRank;
|
|
101
114
|
// 综合评分
|
|
102
|
-
const relevance =
|
|
115
|
+
const relevance = ftsWeight * ftsScore + jaccardWeight * similarity;
|
|
103
116
|
let score = relevance * fact.trustScore;
|
|
104
117
|
// 时间衰减
|
|
105
118
|
if (this.halfLifeDays > 0) {
|
|
@@ -108,29 +121,28 @@ export class FactRetriever {
|
|
|
108
121
|
scored.push({ ...fact, score });
|
|
109
122
|
}
|
|
110
123
|
scored.sort((a, b) => b.score - a.score);
|
|
111
|
-
//
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
for (const s of scored) {
|
|
126
|
-
if (!diverseIds.has(s.factId)) {
|
|
127
|
-
diverse.push(s);
|
|
128
|
-
if (diverse.length >= limit)
|
|
129
|
-
break;
|
|
124
|
+
// 相关性门控:过滤低相关性结果
|
|
125
|
+
const RELEVANCE_THRESHOLD = 0.15;
|
|
126
|
+
const gated = scored.filter(s => s.score >= RELEVANCE_THRESHOLD);
|
|
127
|
+
const pool = gated.length > 0 ? gated : scored;
|
|
128
|
+
// 内容去重:Jaccard > 0.7 的只保留高分
|
|
129
|
+
const results = [];
|
|
130
|
+
for (const candidate of pool) {
|
|
131
|
+
let isDuplicate = false;
|
|
132
|
+
const candidateTokens = this.tokenize(candidate.content);
|
|
133
|
+
for (const kept of results) {
|
|
134
|
+
const keptTokens = this.tokenize(kept.content);
|
|
135
|
+
if (this.jaccardSimilarity(candidateTokens, keptTokens) > 0.7) {
|
|
136
|
+
isDuplicate = true;
|
|
137
|
+
break;
|
|
130
138
|
}
|
|
131
139
|
}
|
|
140
|
+
if (!isDuplicate) {
|
|
141
|
+
results.push(candidate);
|
|
142
|
+
if (results.length >= limit)
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
132
145
|
}
|
|
133
|
-
const results = diverse;
|
|
134
146
|
// 检索追踪:递增 retrieval_count + top3 信任刷新
|
|
135
147
|
if (results.length > 0) {
|
|
136
148
|
this.trackRetrieval(results);
|