@synth-coder/memhub 0.2.1 → 0.2.3

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.
Files changed (71) hide show
  1. package/.eslintrc.cjs +45 -45
  2. package/.factory/commands/opsx-apply.md +150 -0
  3. package/.factory/commands/opsx-archive.md +155 -0
  4. package/.factory/commands/opsx-explore.md +171 -0
  5. package/.factory/commands/opsx-propose.md +104 -0
  6. package/.factory/skills/openspec-apply-change/SKILL.md +156 -0
  7. package/.factory/skills/openspec-archive-change/SKILL.md +114 -0
  8. package/.factory/skills/openspec-explore/SKILL.md +288 -0
  9. package/.factory/skills/openspec-propose/SKILL.md +110 -0
  10. package/.github/workflows/ci.yml +74 -74
  11. package/.iflow/commands/opsx-apply.md +152 -152
  12. package/.iflow/commands/opsx-archive.md +157 -157
  13. package/.iflow/commands/opsx-explore.md +173 -173
  14. package/.iflow/commands/opsx-propose.md +106 -106
  15. package/.iflow/skills/openspec-apply-change/SKILL.md +156 -156
  16. package/.iflow/skills/openspec-archive-change/SKILL.md +114 -114
  17. package/.iflow/skills/openspec-explore/SKILL.md +288 -288
  18. package/.iflow/skills/openspec-propose/SKILL.md +110 -110
  19. package/.prettierrc +11 -11
  20. package/AGENTS.md +169 -26
  21. package/README.md +195 -195
  22. package/README.zh-CN.md +193 -193
  23. package/dist/src/contracts/mcp.js +34 -34
  24. package/dist/src/server/mcp-server.d.ts +8 -0
  25. package/dist/src/server/mcp-server.d.ts.map +1 -1
  26. package/dist/src/server/mcp-server.js +23 -2
  27. package/dist/src/server/mcp-server.js.map +1 -1
  28. package/dist/src/services/memory-service.d.ts +1 -0
  29. package/dist/src/services/memory-service.d.ts.map +1 -1
  30. package/dist/src/services/memory-service.js +125 -82
  31. package/dist/src/services/memory-service.js.map +1 -1
  32. package/docs/architecture-diagrams.md +368 -0
  33. package/docs/architecture.md +381 -349
  34. package/docs/contracts.md +190 -119
  35. package/docs/prompt-template.md +33 -79
  36. package/docs/proposals/mcp-typescript-sdk-refactor.md +568 -568
  37. package/docs/proposals/proposal-close-gates.md +58 -58
  38. package/docs/tool-calling-policy.md +101 -107
  39. package/docs/vector-search.md +306 -0
  40. package/package.json +59 -58
  41. package/src/contracts/index.ts +12 -12
  42. package/src/contracts/mcp.ts +222 -222
  43. package/src/contracts/schemas.ts +307 -307
  44. package/src/contracts/types.ts +410 -410
  45. package/src/index.ts +8 -8
  46. package/src/server/index.ts +5 -5
  47. package/src/server/mcp-server.ts +185 -161
  48. package/src/services/embedding-service.ts +114 -114
  49. package/src/services/index.ts +5 -5
  50. package/src/services/memory-service.ts +663 -621
  51. package/src/storage/frontmatter-parser.ts +243 -243
  52. package/src/storage/index.ts +6 -6
  53. package/src/storage/markdown-storage.ts +236 -236
  54. package/src/storage/vector-index.ts +160 -160
  55. package/src/utils/index.ts +5 -5
  56. package/src/utils/slugify.ts +63 -63
  57. package/test/contracts/schemas.test.ts +313 -313
  58. package/test/contracts/types.test.ts +21 -21
  59. package/test/frontmatter-parser-more.test.ts +94 -94
  60. package/test/server/mcp-server.test.ts +210 -169
  61. package/test/services/memory-service-edge.test.ts +248 -248
  62. package/test/services/memory-service.test.ts +278 -278
  63. package/test/storage/frontmatter-parser.test.ts +222 -222
  64. package/test/storage/markdown-storage.test.ts +216 -216
  65. package/test/storage/storage-edge.test.ts +238 -238
  66. package/test/storage/vector-index.test.ts +153 -153
  67. package/test/utils/slugify-edge.test.ts +94 -94
  68. package/test/utils/slugify.test.ts +68 -68
  69. package/tsconfig.json +25 -25
  70. package/tsconfig.test.json +8 -8
  71. package/vitest.config.ts +29 -29
@@ -1,349 +1,381 @@
1
- # MemHub 架构设计文档
2
-
3
- ## 概述
4
-
5
- MemHub 是一个基于 Git 友好的记忆存储系统,使用 Markdown 格式存储记忆条目,通过 YAML Front Matter 存储元数据。它实现了 MCP (Model Context Protocol) Server,通过 stdio 进行通信。
6
-
7
- ## 设计原则
8
-
9
- 1. **Git 友好**: 所有数据以纯文本 Markdown 文件存储,天然支持版本控制
10
- 2. **人类可读**: 记忆条目可以直接用文本编辑器打开阅读
11
- 3. **简单优先**: 不使用数据库,避免复杂的迁移和锁定问题
12
- 4. **契约先行**: 接口和类型定义先于实现
13
- 5. **测试驱动**: 严格遵循 TDD (红-绿-重构)
14
-
15
- ## 系统架构
16
-
17
- ```
18
- ┌─────────────────────────────────────────────────────────────┐
19
- │ MCP Client │
20
- │ (Claude Desktop, etc.) │
21
- └──────────────────────┬──────────────────────────────────────┘
22
- stdio
23
-
24
- ┌─────────────────────────────────────────────────────────────┐
25
- │ MemHub MCP Server │
26
- │ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
27
- MCP Router │ Services │ │ Storage Layer │ │
28
- │ │ │ │ │ │
29
- │ │ - list_tools │ │ - create │ │ - read memory │ │
30
- │ │ - call_tool │ │ - read │ │ - write memory │ │
31
- │ │ │ │ - update │ │ - search index │ │
32
- │ │ │ │ - delete │ - list memories │ │
33
- │ │ │ │ - search │ │ │ │
34
- └──────────────┘ └──────────────┘ └──────────────────┘
35
- └──────────────────────┬──────────────────────────────────────┘
36
-
37
-
38
- ┌─────────────────────────────────────────────────────────────┐
39
- │ Markdown Storage │
40
- ┌─────────────────────────────────────────────────────┐
41
- YYYY-MM-DD-title-slug.md
42
- │ │ ───────────────────────── │ │
43
- │ │ --- │ │
44
- │ │ id: uuid-v4
45
- │ │ created_at: ISO8601
46
- │ │ updated_at: ISO8601
47
- │ │ tags: [tag1, tag2]
48
- │ │ category: string
49
- importance: 1-5
50
- │ │ --- │ │
51
- │ │ │
52
- │ │ # Title │ │
53
- │ │ │ │
54
- │ Markdown content body...
55
- │ │
56
- └─────────────────────────────────────────────────────┘
57
- └─────────────────────────────────────────────────────────────┘
58
- ```
59
-
60
- ## 核心组件
61
-
62
- ### 1. MCP Server (src/server/mcp-server.ts)
63
-
64
- - 入口点,处理 stdio 通信
65
- - 实现 MCP 协议的生命周期管理
66
- - 路由工具调用到对应的服务
67
-
68
- ### 2. Services (src/services/)
69
-
70
- 业务逻辑层,包含:
71
-
72
- - **MemoryService**: 记忆 CRUD 操作
73
- - **SearchService**: 搜索和过滤
74
- - **IndexService**: 索引管理(可选,用于性能)
75
-
76
- ### 3. Storage Layer (src/storage/)
77
-
78
- 数据持久化层:
79
-
80
- - **MarkdownStorage**: Markdown 文件读写
81
- - **FrontMatterParser**: YAML Front Matter 解析
82
- - **FileSystem**: 文件系统抽象(便于测试)
83
-
84
- ### 4. Contracts (src/contracts/)
85
-
86
- 类型定义和 Zod 契约:
87
-
88
- - **types.ts**: TypeScript 类型
89
- - **schemas.ts**: Zod 验证模式
90
- - **mcp.ts**: MCP 协议相关类型
91
-
92
- ## 数据模型
93
-
94
- ### Memory Entry (记忆条目)
95
-
96
- ```typescript
97
- interface Memory {
98
- // Identity
99
- id: string; // UUID v4
100
-
101
- // Metadata (YAML Front Matter)
102
- createdAt: string; // ISO 8601
103
- updatedAt: string; // ISO 8601
104
- tags: string[]; // 标签数组
105
- category: string; // 分类
106
- importance: number; // 1-5 重要性等级
107
-
108
- // Content (Markdown Body)
109
- title: string; // Markdown H1 标题
110
- content: string; // Markdown 正文
111
- }
112
- ```
113
-
114
- ### File Naming Convention
115
-
116
- ```
117
- {YYYY-MM-DD}-{title-slug}.md
118
-
119
- Examples:
120
- - 2024-03-15-project-kickoff.md
121
- - 2024-03-20-meeting-notes.md
122
- ```
123
-
124
- ### Markdown Format
125
-
126
- ```markdown
127
- ---
128
- id: '550e8400-e29b-41d4-a716-446655440000'
129
- created_at: '2024-03-15T10:30:00Z'
130
- updated_at: '2024-03-15T14:20:00Z'
131
- tags:
132
- - project
133
- - planning
134
- category: 'work'
135
- importance: 4
136
- ---
137
-
138
- # Project Kickoff
139
-
140
- Today we started the new project...
141
-
142
- ## Key Decisions
143
-
144
- - Decision 1
145
- - Decision 2
146
- ```
147
-
148
- ## MCP Tools
149
-
150
- ### 1. `memory_create`
151
-
152
- 创建新记忆条目。
153
-
154
- **Input:**
155
-
156
- - title: string (required)
157
- - content: string (required)
158
- - tags?: string[]
159
- - category?: string
160
- - importance?: number (1-5, default: 3)
161
-
162
- **Output:**
163
-
164
- - id: string
165
- - filePath: string
166
-
167
- ### 2. `memory_read`
168
-
169
- 读取记忆条目。
170
-
171
- **Input:**
172
-
173
- - id: string (required)
174
-
175
- **Output:**
176
-
177
- - Memory object
178
-
179
- ### 3. `memory_update`
180
-
181
- 更新记忆条目。
182
-
183
- **Input:**
184
-
185
- - id: string (required)
186
- - title?: string
187
- - content?: string
188
- - tags?: string[]
189
- - category?: string
190
- - importance?: number
191
-
192
- **Output:**
193
-
194
- - updated Memory object
195
-
196
- ### 4. `memory_delete`
197
-
198
- 删除记忆条目。
199
-
200
- **Input:**
201
-
202
- - id: string (required)
203
-
204
- **Output:**
205
-
206
- - success: boolean
207
-
208
- ### 5. `memory_list`
209
-
210
- 列出记忆条目(支持过滤和分页)。
211
-
212
- **Input:**
213
-
214
- - category?: string
215
- - tags?: string[]
216
- - fromDate?: string (ISO 8601)
217
- - toDate?: string (ISO 8601)
218
- - limit?: number (default: 20, max: 100)
219
- - offset?: number (default: 0)
220
-
221
- **Output:**
222
-
223
- - memories: Memory[]
224
- - total: number
225
- - hasMore: boolean
226
-
227
- ### 6. `memory_search`
228
-
229
- 全文搜索记忆。
230
-
231
- **Input:**
232
-
233
- - query: string (required)
234
- - limit?: number (default: 10)
235
-
236
- **Output:**
237
-
238
- - results: SearchResult[]
239
- - memory: Memory
240
- - score: number
241
- - matches: string[]
242
-
243
- ### 7. `memory_get_categories`
244
-
245
- 获取所有分类。
246
-
247
- **Output:**
248
-
249
- - categories: string[]
250
-
251
- ### 8. `memory_get_tags`
252
-
253
- 获取所有标签。
254
-
255
- **Output:**
256
-
257
- - tags: string[]
258
-
259
- ## 错误处理
260
-
261
- 所有错误使用标准 MCP 错误格式:
262
-
263
- ```typescript
264
- interface McpError {
265
- code: number; // MCP error code
266
- message: string; // Human readable message
267
- data?: unknown; // Additional context
268
- }
269
- ```
270
-
271
- 错误码定义:
272
-
273
- - `INVALID_PARAMS`: 参数验证失败
274
- - `NOT_FOUND`: 记忆条目不存在
275
- - `STORAGE_ERROR`: 存储操作失败
276
- - `INTERNAL_ERROR`: 内部错误
277
-
278
- ## 配置
279
-
280
- 通过环境变量配置:
281
-
282
- ```bash
283
- MEMHUB_STORAGE_PATH=/path/to/memories # 存储目录,默认: ./memories
284
- MEMHUB_LOG_LEVEL=info # 日志级别: debug, info, warn, error
285
- ```
286
-
287
- ## 技术栈
288
-
289
- - **Runtime**: Node.js 18+
290
- - **Language**: TypeScript 5.3+
291
- - **Protocol**: MCP (Model Context Protocol)
292
- - **Transport**: stdio
293
- - **Validation**: Zod
294
- - **Testing**: Vitest (覆盖率 >= 80%)
295
- - **Linting**: ESLint + Prettier
296
-
297
- ## 目录结构
298
-
299
- ```
300
- memhub/
301
- ├── docs/
302
- │ ├── architecture.md # 本文档
303
- │ └── contracts.md # 契约文档
304
- ├── src/
305
- │ ├── contracts/ # 类型和契约
306
- │ │ ├── types.ts
307
- │ │ ├── schemas.ts
308
- │ │ └── mcp.ts
309
- │ ├── server/ # MCP Server
310
- │ │ └── mcp-server.ts
311
- │ ├── services/ # 业务逻辑
312
- │ │ ├── memory-service.ts
313
- │ │ └── search-service.ts
314
- │ ├── storage/ # 存储层
315
- │ │ ├── markdown-storage.ts
316
- │ │ └── frontmatter-parser.ts
317
- │ └── utils/ # 工具函数
318
- │ └── file-system.ts
319
- ├── test/ # 测试
320
- │ ├── unit/
321
- │ ├── integration/
322
- │ └── fixtures/
323
- ├── .github/
324
- │ └── workflows/
325
- │ └── ci.yml # CI 配置
326
- ├── package.json
327
- ├── tsconfig.json
328
- ├── vitest.config.ts
329
- ├── .eslintrc.cjs
330
- ├── .prettierrc
331
- └── README.md
332
- ```
333
-
334
- ## 开发流程
335
-
336
- 1. **设计**: 先写架构文档和契约
337
- 2. **契约**: 定义 TypeScript 类型和 Zod Schema
338
- 3. **测试**: 编写红色测试(先失败)
339
- 4. **实现**: 编写最小实现使测试通过(绿色)
340
- 5. **重构**: 优化代码结构
341
- 6. **重复**: 循环 3-5 步
342
-
343
- ## 质量门禁
344
-
345
- - ✅ ESLint 无错误
346
- - Prettier 格式化检查通过
347
- - ✅ TypeScript 严格类型检查通过
348
- - ✅ 单元测试全部通过
349
- - ✅ 代码覆盖率 >= 80%
1
+ # MemHub 架构设计文档
2
+
3
+ ## 概述
4
+
5
+ MemHub 是一个基于 Git 友好的记忆存储系统,使用 Markdown 格式存储记忆条目,通过 YAML Front Matter 存储元数据。它实现了 MCP (Model Context Protocol) Server,通过 stdio 进行通信,并集成了向量语义搜索功能以提供更智能的记忆检索。
6
+
7
+ ## 设计原则
8
+
9
+ 1. **Git 友好**: 所有数据以纯文本 Markdown 文件存储,天然支持版本控制
10
+ 2. **人类可读**: 记忆条目可以直接用文本编辑器打开阅读
11
+ 3. **简单优先**: Markdown 作为唯一数据源,向量索引仅为搜索缓存
12
+ 4. **契约先行**: 接口和类型定义先于实现
13
+ 5. **测试驱动**: 严格遵循 TDD (红-绿-重构)
14
+ 6. **混合搜索**: 支持基于元数据的过滤和基于向量的语义搜索
15
+ 7. **懒加载**: 向量模型和索引按需加载,降低启动成本
16
+
17
+ ## 系统架构
18
+
19
+ ```
20
+ ┌─────────────────────────────────────────────────────────────┐
21
+ │ MCP Client │
22
+ (Claude Desktop, etc.) │
23
+ └──────────────────────┬──────────────────────────────────────┘
24
+ │ stdio
25
+
26
+ ┌─────────────────────────────────────────────────────────────┐
27
+ MemHub MCP Server
28
+ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐
29
+ │ │ MCP Router │ │ Services │ │ Storage Layer │ │
30
+ │ │ │ │ │ │ │ │
31
+ │ │ - list_tools │ │ - memoryLoad │ │ - MarkdownStorage│ │
32
+ │ │ - call_tool │ │ - memoryUpdate │ - FrontMatter │ │
33
+ │ │ │ │ │ │ - VectorIndex │ │
34
+ │ │ │ │ │ - EmbeddingSvc │
35
+ │ └──────────────┘ └──────────────┘ └──────────────────┘ │
36
+ └──────────────────────┬──────────────────────────────────────┘
37
+
38
+
39
+ ┌─────────────────────────────────────────────────────────────┐
40
+ Storage Layer
41
+ ┌────────────────────┐ ┌────────────────────────┐
42
+ │ │ Markdown Storage Vector Search Cache
43
+ │ │ (Source of Truth) (LanceDB)
44
+ │ │ │ │ │
45
+ │ │ YYYY-MM-DD/ .lancedb/ │ │
46
+ │ │ session_uuid/ │◄──────►│ - 384-dim vectors
47
+ │ │ title-slug.md │ │ - Cosine distance
48
+ │ │ │ │ - Rebuildable
49
+ └────────────────────┘ └────────────────────────┘
50
+ └─────────────────────────────────────────────────────────────┘
51
+
52
+
53
+ ┌─────────────────────────────────────────────────────────────┐
54
+ Embedding Service
55
+ ┌─────────────────────────────────────────────────────┐
56
+ │ ONNX Model (all-MiniLM-L6-v2) │
57
+ │ │ - 384-dimension output │ │
58
+ │ │ - Cached at ~/.cache/huggingface │ │
59
+ │ │ - Lazy initialization │ │
60
+ │ └─────────────────────────────────────────────────────┘ │
61
+ └─────────────────────────────────────────────────────────────┘
62
+ ```
63
+
64
+ ## 核心组件
65
+
66
+ ### 1. MCP Server (src/server/mcp-server.ts)
67
+
68
+ - 入口点,处理 stdio 通信
69
+ - 实现 MCP 协议的生命周期管理
70
+ - 路由工具调用到 MemoryService
71
+ - 支持 `MEMHUB_STORAGE_PATH` 和 `MEMHUB_VECTOR_SEARCH` 环境变量
72
+
73
+ ### 2. Services (src/services/)
74
+
75
+ 业务逻辑层,包含:
76
+
77
+ - **MemoryService**: 统一的记忆管理服务
78
+ - `memoryLoad()`: 加载记忆(支持向量搜索和元数据过滤)
79
+ - `memoryUpdate()`: 创建或更新记忆(自动维护向量索引)
80
+ - **EmbeddingService**: 文本嵌入服务(单例模式)
81
+ - 基于 `@xenova/transformers` ONNX 模型
82
+ - 输出 384 维归一化向量
83
+ - 首次使用时下载并缓存到 `~/.cache/huggingface`
84
+
85
+ ### 3. Storage Layer (src/storage/)
86
+
87
+ 数据持久化层:
88
+
89
+ - **MarkdownStorage**: Markdown 文件读写(唯一数据源)
90
+ - **FrontMatterParser**: YAML Front Matter 解析和序列化
91
+ - **VectorIndex**: LanceDB 向量索引(搜索缓存,可重建)
92
+
93
+ ### 4. Contracts (src/contracts/)
94
+
95
+ 类型定义和 Zod 契约:
96
+
97
+ - **types.ts**: TypeScript 类型定义
98
+ - **schemas.ts**: Zod 验证模式
99
+ - **mcp.ts**: MCP 工具定义和描述
100
+
101
+ ## 数据模型
102
+
103
+ ### Memory Entry (记忆条目)
104
+
105
+ ```typescript
106
+ interface Memory {
107
+ // Identity
108
+ id: string; // UUID v4
109
+
110
+ // Metadata (YAML Front Matter)
111
+ createdAt: string; // ISO 8601
112
+ updatedAt: string; // ISO 8601
113
+ sessionId?: string; // 会话 UUID(并发隔离)
114
+ entryType?: EntryType; // 记忆类型
115
+ tags: string[]; // 标签数组
116
+ category: string; // 分类
117
+ importance: number; // 1-5 重要性等级
118
+
119
+ // Content (Markdown Body)
120
+ title: string; // Markdown H1 标题
121
+ content: string; // Markdown 正文
122
+ }
123
+
124
+ type EntryType = 'preference' | 'decision' | 'context' | 'fact';
125
+ ```
126
+
127
+ ### File Naming Convention
128
+
129
+ ```
130
+ memories/YYYY-MM-DD/session_uuid/timestamp-title-slug.md
131
+
132
+ Examples:
133
+ - memories/2026-03-03/550e8400-e29b-41d4-a716-446655440111/2026-03-03T16-40-12-123Z-task-summary.md
134
+ - memories/2026-03-04/a1b2c3d4-e5f6-7890-abcd-ef1234567890/2026-03-04T10-15-30-456Z-architecture-decision.md
135
+ ```
136
+
137
+ **并发安全设计:**
138
+
139
+ - 同一天按 `session_uuid` 分桶,避免多 CLI 写入冲突
140
+ - 每条记录独立文件,支持并发读写
141
+ - 文件路径包含时间戳,保证唯一性
142
+
143
+ ### Markdown Format
144
+
145
+ ```markdown
146
+ ---
147
+ id: '550e8400-e29b-41d4-a716-446655440000'
148
+ created_at: '2026-03-03T08:00:00.000Z'
149
+ updated_at: '2026-03-03T08:00:00.000Z'
150
+ session_id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890'
151
+ entry_type: 'decision'
152
+ tags:
153
+ - architecture
154
+ - vector-search
155
+ category: 'engineering'
156
+ importance: 4
157
+ ---
158
+
159
+ # Integrate Vector Semantic Search
160
+
161
+ Add LanceDB-based vector search with local ONNX embedding model.
162
+
163
+ ## Key Decisions
164
+
165
+ - Use all-MiniLM-L6-v2 model (384-dim output)
166
+ - Store vectors in `.lancedb/` alongside markdown files
167
+ - Vector index is rebuildable from markdown source
168
+ ```
169
+
170
+ ## MCP Tools
171
+
172
+ MemHub 使用简化的 2-tool 接口,形成「先加载 STM,再回写 STM」的闭环。
173
+
174
+ ### 1. `memory_load`
175
+
176
+ 统一记忆加载接口,用于用户输入后第一轮主动加载短期记忆。
177
+
178
+ **Input:**
179
+
180
+ ```typescript
181
+ interface MemoryLoadInput {
182
+ id?: string; // 指定单条记忆 ID
183
+ query?: string; // 语义搜索查询(启用向量搜索)
184
+ category?: string; // 分类过滤
185
+ tags?: string[]; // 标签过滤
186
+ limit?: number; // 返回数量限制(默认 20)
187
+ }
188
+ ```
189
+
190
+ **Output:**
191
+
192
+ ```typescript
193
+ interface MemoryLoadOutput {
194
+ items: Memory[]; // 记忆条目数组
195
+ total: number; // 总数
196
+ }
197
+ ```
198
+
199
+ **行为特性:**
200
+
201
+ - 当提供 `query` 时,使用向量语义搜索(需要 `MEMHUB_VECTOR_SEARCH=true`)
202
+ - 当提供 `tags`、`category`、`date` 时,使用元数据过滤
203
+ - 支持组合条件(先向量搜索再过滤)
204
+ - 向量搜索失败时自动降级为文本搜索
205
+
206
+ ### 2. `memory_update`
207
+
208
+ 统一记忆回写接口,用于本轮结束前主动回写记忆。
209
+
210
+ **Input:**
211
+
212
+ ```typescript
213
+ type EntryType = 'preference' | 'decision' | 'context' | 'fact';
214
+
215
+ interface MemoryUpdateInput {
216
+ id?: string; // 有则更新,无则创建
217
+ sessionId?: string; // 无则服务端自动生成并返回
218
+ mode?: 'append' | 'upsert'; // 默认 append
219
+ entryType?: EntryType; // 记忆类型
220
+ title?: string; // 标题(Markdown H1)
221
+ content: string; // 内容(必填,Markdown)
222
+ tags?: string[]; // 标签
223
+ category?: string; // 分类
224
+ importance?: number; // 重要性 1-5(默认 3)
225
+ }
226
+ ```
227
+
228
+ **Output:**
229
+
230
+ ```typescript
231
+ interface MemoryUpdateOutput {
232
+ id: string; // 记忆 ID
233
+ sessionId: string; // 会话 ID
234
+ filePath: string; // 存储路径
235
+ created: boolean; // 是否新建
236
+ updated: boolean; // 是否更新
237
+ memory: Memory; // 完整记忆对象
238
+ }
239
+ ```
240
+
241
+ **行为特性:**
242
+
243
+ - 自动生成 UUID 和时间戳
244
+ - 自动维护向量索引(后台异步)
245
+ - 支持并发安全:按 `YYYY-MM-DD/session_uuid/` 分目录存储
246
+ - 向量索引失败不影响 Markdown 写入
247
+
248
+ ## 错误处理
249
+
250
+ 所有错误使用标准 MCP 错误格式:
251
+
252
+ ```typescript
253
+ interface McpError {
254
+ code: number; // MCP error code
255
+ message: string; // Human readable message
256
+ data?: unknown; // Additional context
257
+ }
258
+ ```
259
+
260
+ 错误码定义:
261
+
262
+ - `INVALID_PARAMS`: 参数验证失败
263
+ - `NOT_FOUND`: 记忆条目不存在
264
+ - `STORAGE_ERROR`: 存储操作失败
265
+ - `INTERNAL_ERROR`: 内部错误
266
+
267
+ ## 配置
268
+
269
+ 通过环境变量配置:
270
+
271
+ ```bash
272
+ MEMHUB_STORAGE_PATH=/path/to/memories # 存储目录,默认: ./memories
273
+ MEMHUB_LOG_LEVEL=info # 日志级别: debug, info, warn, error
274
+ MEMHUB_VECTOR_SEARCH=true # 启用向量搜索,默认: true
275
+ ```
276
+
277
+ ## 向量搜索架构
278
+
279
+ ### 设计原则
280
+
281
+ 1. **Markdown 为准**: Markdown 文件是唯一数据源,向量索引仅为搜索缓存
282
+ 2. **可重建性**: 向量索引可随时从 Markdown 文件重建
283
+ 3. **懒加载**: ONNX 模型和 LanceDB 按需加载,降低启动成本
284
+ 4. **降级友好**: 向量搜索失败不影响基本功能
285
+
286
+ ### Embedding 服务
287
+
288
+ - **模型**: Xenova/all-MiniLM-L6-v2 (~23MB)
289
+ - **输出维度**: 384
290
+ - **缓存位置**: `~/.cache/huggingface/`
291
+ - **单例模式**: 全局共享一个模型实例
292
+
293
+ ### Vector Index
294
+
295
+ - **存储**: LanceDB (`.lancedb/` 目录)
296
+ - **距离度量**: Cosine distance
297
+ - **索引字段**:
298
+ - `id`: 记忆 UUID
299
+ - `vector`: 384 维浮点向量
300
+ - `title`, `category`, `tags`, `importance`: 元数据过滤
301
+
302
+ ### 搜索流程
303
+
304
+ 1. **Query Embedding**: 将查询文本转换为 384 维向量
305
+ 2. **Vector Search**: 在 LanceDB 中查找最相似的向量
306
+ 3. **Metadata Filter**: 可选地按 tags/category/date 过滤
307
+ 4. **Ranking**: 按距离排序(距离越小越相似)
308
+ 5. **Result Loading**: 从 Markdown 文件加载完整记忆内容
309
+
310
+ ## 技术栈
311
+
312
+ - **Runtime**: Node.js 18+
313
+ - **Language**: TypeScript 5.9+
314
+ - **Protocol**: MCP (Model Context Protocol) via @modelcontextprotocol/sdk
315
+ - **Transport**: stdio
316
+ - **Storage**:
317
+ - **Markdown**: 纯文本存储(唯一数据源)
318
+ - **Vector DB**: LanceDB (基于 Apache Arrow)
319
+ - **AI/ML**:
320
+ - **Embedding**: @xenova/transformers (all-MiniLM-L6-v2 ONNX 模型)
321
+ - **Vector Dimension**: 384
322
+ - **Validation**: Zod 3.25+
323
+ - **Testing**: Vitest (覆盖率 >= 80%)
324
+ - **Linting**: ESLint + Prettier
325
+
326
+ ## 目录结构
327
+
328
+ ```
329
+ memhub/
330
+ ├── docs/
331
+ │ ├── architecture.md # 本文档
332
+ │ ├── architecture-diagrams.md # 架构图(Mermaid)
333
+ │ ├── contracts.md # 契约文档
334
+ │ ├── tool-calling-policy.md # 工具调用策略
335
+ │ └── prompt-template.md # Agent Prompt 模板
336
+ ├── src/
337
+ │ ├── contracts/ # 类型和契约
338
+ │ │ ├── types.ts
339
+ │ │ ├── schemas.ts
340
+ │ │ └── mcp.ts
341
+ │ ├── server/ # MCP Server
342
+ │ │ └── mcp-server.ts
343
+ │ ├── services/ # 业务逻辑
344
+ │ │ ├── memory-service.ts
345
+ │ │ └── embedding-service.ts
346
+ │ ├── storage/ # 存储层
347
+ │ │ ├── markdown-storage.ts
348
+ │ │ ├── frontmatter-parser.ts
349
+ │ │ └── vector-index.ts
350
+ │ └── utils/ # 工具函数
351
+ │ └── slugify.ts
352
+ ├── test/ # 测试
353
+ │ ├── unit/
354
+ │ └── integration/
355
+ ├── .github/
356
+ │ └── workflows/
357
+ │ └── ci.yml # CI 配置
358
+ ├── package.json
359
+ ├── tsconfig.json
360
+ ├── vitest.config.ts
361
+ ├── .eslintrc.cjs
362
+ ├── .prettierrc
363
+ └── README.md
364
+ ```
365
+
366
+ ## 开发流程
367
+
368
+ 1. **设计**: 先写架构文档和契约
369
+ 2. **契约**: 定义 TypeScript 类型和 Zod Schema
370
+ 3. **测试**: 编写红色测试(先失败)
371
+ 4. **实现**: 编写最小实现使测试通过(绿色)
372
+ 5. **重构**: 优化代码结构
373
+ 6. **重复**: 循环 3-5 步
374
+
375
+ ## 质量门禁
376
+
377
+ - ✅ ESLint 无错误
378
+ - ✅ Prettier 格式化检查通过
379
+ - ✅ TypeScript 严格类型检查通过
380
+ - ✅ 单元测试全部通过
381
+ - ✅ 代码覆盖率 >= 80%