openclaw-cortex-memory 0.1.0-Alpha.20 → 0.1.0-Alpha.22

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 (60) hide show
  1. package/README.md +181 -228
  2. package/SKILL.md +71 -332
  3. package/dist/index.d.ts +36 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +194 -6
  6. package/dist/index.js.map +1 -1
  7. package/dist/openclaw.plugin.json +208 -12
  8. package/dist/src/dedup/three_stage_deduplicator.d.ts.map +1 -1
  9. package/dist/src/dedup/three_stage_deduplicator.js +1 -2
  10. package/dist/src/dedup/three_stage_deduplicator.js.map +1 -1
  11. package/dist/src/engine/ts_engine.d.ts +31 -0
  12. package/dist/src/engine/ts_engine.d.ts.map +1 -1
  13. package/dist/src/engine/ts_engine.js +262 -14
  14. package/dist/src/engine/ts_engine.js.map +1 -1
  15. package/dist/src/engine/types.d.ts +1 -0
  16. package/dist/src/engine/types.d.ts.map +1 -1
  17. package/dist/src/graph/ontology.d.ts +65 -15
  18. package/dist/src/graph/ontology.d.ts.map +1 -1
  19. package/dist/src/graph/ontology.js +316 -4
  20. package/dist/src/graph/ontology.js.map +1 -1
  21. package/dist/src/quality/llm_output_validator.d.ts +48 -0
  22. package/dist/src/quality/llm_output_validator.d.ts.map +1 -0
  23. package/dist/src/quality/llm_output_validator.js +404 -0
  24. package/dist/src/quality/llm_output_validator.js.map +1 -0
  25. package/dist/src/reflect/reflector.d.ts.map +1 -1
  26. package/dist/src/reflect/reflector.js +284 -8
  27. package/dist/src/reflect/reflector.js.map +1 -1
  28. package/dist/src/rules/rule_store.d.ts.map +1 -1
  29. package/dist/src/rules/rule_store.js +75 -16
  30. package/dist/src/rules/rule_store.js.map +1 -1
  31. package/dist/src/session/session_end.d.ts +20 -43
  32. package/dist/src/session/session_end.d.ts.map +1 -1
  33. package/dist/src/session/session_end.js +21 -233
  34. package/dist/src/session/session_end.js.map +1 -1
  35. package/dist/src/store/archive_store.d.ts +20 -7
  36. package/dist/src/store/archive_store.d.ts.map +1 -1
  37. package/dist/src/store/archive_store.js +96 -61
  38. package/dist/src/store/archive_store.js.map +1 -1
  39. package/dist/src/store/graph_memory_store.d.ts +44 -0
  40. package/dist/src/store/graph_memory_store.d.ts.map +1 -0
  41. package/dist/src/store/graph_memory_store.js +168 -0
  42. package/dist/src/store/graph_memory_store.js.map +1 -0
  43. package/dist/src/store/read_store.d.ts +27 -0
  44. package/dist/src/store/read_store.d.ts.map +1 -1
  45. package/dist/src/store/read_store.js +653 -94
  46. package/dist/src/store/read_store.js.map +1 -1
  47. package/dist/src/store/vector_store.d.ts +1 -0
  48. package/dist/src/store/vector_store.d.ts.map +1 -1
  49. package/dist/src/store/vector_store.js +1 -0
  50. package/dist/src/store/vector_store.js.map +1 -1
  51. package/dist/src/store/write_store.d.ts +7 -0
  52. package/dist/src/store/write_store.d.ts.map +1 -1
  53. package/dist/src/store/write_store.js +15 -3
  54. package/dist/src/store/write_store.js.map +1 -1
  55. package/dist/src/sync/session_sync.d.ts +48 -0
  56. package/dist/src/sync/session_sync.d.ts.map +1 -1
  57. package/dist/src/sync/session_sync.js +277 -78
  58. package/dist/src/sync/session_sync.js.map +1 -1
  59. package/openclaw.plugin.json +208 -12
  60. package/package.json +22 -18
package/README.md CHANGED
@@ -1,229 +1,182 @@
1
- # OpenClaw Cortex Memory
2
-
3
- OpenClaw 长期记忆插件 - 专为 OpenClaw AI 助手设计的智能记忆系统
4
-
5
- [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
6
- [![Node.js: 22+](https://img.shields.io/badge/Node.js-22+-green.svg)](https://nodejs.org/)
7
- [![OpenClaw: Plugin](https://img.shields.io/badge/OpenClaw-Plugin-orange.svg)](https://github.com/openclaw)
8
-
9
- 面向 OpenClaw 的长期记忆插件,集成多路检索、事件归档、图谱关系、向量化与反衰减排序,支持历史增量导入、规则反思和可观测诊断,帮助 Agent 在跨会话中持续积累并稳定调用高价值记忆。
10
-
11
- ---
12
-
13
- ## ✨ 特性
14
-
15
- ### 🔍 语义检索
16
- - **多路召回**: 关键词/BM25 + 向量召回统一融合重排
17
- - **智能排序**: 支持长度归一化防止长文本天然占优
18
- - **反衰减机制**: 命中频次反衰减,避免高价值老记忆被过度遗忘
19
-
20
- ### 📝 事件存储
21
- - **分层记忆**: 原始会话、结构化事件、抽象规则拆分存储
22
- - **全文分块向量化**: Active 与 Archive 统一按 `chunk_size/chunk_overlap` 做全文分块向量化
23
- - **增量同步**: 按状态文件增量导入,避免全量重复扫描
24
-
25
- ### 🕸️ 图谱关系
26
- - **实体关系层**: 基于归档事件中的 `entities` / `relations` 字段动态构图
27
- - **图谱治理**: 写入前按 `schema/graph.schema.yaml` 做规范化与关系规则校验
28
- - **关系查询**: 支撑 `query_graph` 的实体共现与关系查询
29
-
30
- ### ⚙️ 规则演进
31
- - **自动反思**: `reflect_memory` 自动将事件转化为规则
32
- - **规则去重**: 对规则写入做签名判重,减少重复沉淀
33
- - **可复用规则**: 存储于 `CORTEX_RULES.md` 供后续任务复用
34
-
35
- ### 🔧 运维诊断
36
- - **系统诊断**: `diagnostics` 检查本地存储状态与 API 连通性
37
- - **向量回填**: `backfill_embeddings` 支持 incremental / vector_only / full 三种模式重建向量层
38
- - **可观测性**: 通过状态文件快速定位数据目录、同步与反思异常
39
-
40
- ---
41
-
42
- ## 🚀 快速开始
43
-
44
- ### 前置要求
45
-
46
- - Node.js 22+
47
- - OpenAI API Key(或其他兼容 API)
48
-
49
- ### 安装
50
-
51
- **快速安装(推荐):**
52
-
53
- ```bash
54
- cd ~/openclaw
55
- openclaw plugins install clawhub:openclaw-cortex-memory
56
- openclaw plugins enable openclaw-cortex-memory
57
- ```
58
-
59
- **npm 安装方式:**
60
-
61
- ```bash
62
- cd ~/openclaw
63
- openclaw plugins install openclaw-cortex-memory@alpha
64
- openclaw plugins enable openclaw-cortex-memory
65
- ```
66
- **本地包安装方式(当 ClawHub/npm 解析受网络影响时):**
67
-
68
- ```bash
69
- curl -L -o /tmp/cortex.tgz https://registry.npmjs.org/openclaw-cortex-memory/-/openclaw-cortex-memory-0.1.0-Alpha.20.tgz
70
- cd ~/openclaw
71
- openclaw plugins install /tmp/cortex.tgz
72
- openclaw plugins enable openclaw-cortex-memory
73
- rm -f /tmp/cortex.tgz
74
- ```
75
-
76
- **本地开发模式:**
77
-
78
- ```bash
79
- cd ~/.openclaw/extensions
80
- git clone https://github.com/deki18/openclaw-cortex-memory.git
81
- cd openclaw-cortex-memory
82
- npm install && npm run build
83
- ```
84
-
85
- ### 配置
86
-
87
- `openclaw.json` 中添加:
88
-
89
- ```json
90
- {
91
- "plugins": {
92
- "allow": ["openclaw-cortex-memory"],
93
- "slots": { "memory": "openclaw-cortex-memory" },
94
- "entries": {
95
- "openclaw-cortex-memory": {
96
- "enabled": true,
97
- "config": {
98
- "engineMode": "ts",
99
- "autoSync": true,
100
- "autoReflect": false,
101
- "autoReflectIntervalMinutes": 30,
102
- "readFusion": {
103
- "enabled": true,
104
- "authoritative": true
105
- },
106
- "memoryDecay": {
107
- "enabled": true,
108
- "antiDecay": {
109
- "enabled": true
110
- }
111
- },
112
- "embedding": {
113
- "provider": "api",
114
- "model": "text-embedding-3-large",
115
- "apiKey": "${EMBEDDING_API_KEY}",
116
- "baseURL": "https://your-embedding-endpoint/v1",
117
- "dimensions": 3072
118
- },
119
- "llm": {
120
- "provider": "api",
121
- "model": "gpt-4",
122
- "apiKey": "${LLM_API_KEY}",
123
- "baseURL": "https://your-llm-endpoint/v1"
124
- },
125
- "reranker": {
126
- "provider": "api",
127
- "model": "BAAI/bge-reranker-v2-m3",
128
- "apiKey": "${RERANKER_API_KEY}",
129
- "baseURL": "https://your-reranker-endpoint/v1/rerank"
130
- }
131
- }
132
- }
133
- }
134
- }
135
- }
136
- ```
137
-
138
- ### 启动
139
-
140
- ```bash
141
- openclaw config validate
142
- openclaw gateway restart
143
- ```
144
-
145
- ---
146
-
147
- ## 🛠️ 可用工具
148
-
149
- | 工具 | 说明 |
150
- |------|------|
151
- | `search_memory` | 语义搜索记忆库,支持 query + top_k |
152
- | `store_event` | 存储事件,可包含实体和关系 |
153
- | `query_graph` | 查询实体关系图谱,支持关系过滤、方向过滤与路径查询 |
154
- | `get_hot_context` | 获取热上下文(CORTEX_RULES.md + 近期会话) |
155
- | `get_auto_context` | 自动检索相关记忆 + 热上下文 |
156
- | `reflect_memory` | 触发记忆反思,将事件转化为规则 |
157
- | `sync_memory` | 同步 OpenClaw 历史会话(增量),并通过 LLM 判定后写入事件/图谱 |
158
- | `backfill_embeddings` | 向量回填/重建(支持 `incremental`、`vector_only`、`full`) |
159
- | `delete_memory` | 删除指定记忆 |
160
- | `diagnostics` | 系统诊断(含 embedding/LLM/reranker 连通性检查) |
161
-
162
- ---
163
-
164
- ## 💻 CLI 命令
165
-
166
- CLI 命令需在插件目录运行:
167
-
168
- ```bash
169
- npx cortex-memory status # 查看插件状态
170
- npx cortex-memory enable # 启用插件
171
- npx cortex-memory disable # 禁用插件(回退到内置记忆)
172
- npx cortex-memory uninstall # 卸载插件
173
- npx cortex-memory uninstall --keep-data # 卸载但保留数据
174
- npx cortex-memory help # 查看命令帮助
175
- ```
176
-
177
- ---
178
-
179
- ## 📁 数据存储结构
180
-
181
- | 路径 | 说明 |
182
- |------|------|
183
- | `<dbPath>/MEMORY.md` | 记忆说明 |
184
- | `<dbPath>/CORTEX_RULES.md` | 规则文件 |
185
- | `<dbPath>/sessions/active/sessions.jsonl` | 活跃会话 |
186
- | `<dbPath>/sessions/archive/sessions.jsonl` | 归档事件 |
187
- | `<dbPath>/vector/lancedb` | LanceDB 向量表(可用时) |
188
- | `<dbPath>/vector/lancedb_events.jsonl` | 向量回退存储(LanceDB不可用时) |
189
- | `<dbPath>/.sync_state.json` | 同步增量状态 |
190
- | `<dbPath>/.session_end_state.json` | session_end 幂等状态 |
191
- | `<dbPath>/.rule_store_state.json` | 规则去重状态 |
192
- | `<dbPath>/.dedup_index.json` | 三阶段去重索引 |
193
- | `<dbPath>/.read_hit_stats.json` | 检索命中频次统计(用于反衰减增益) |
194
- | `<dbPath>/graph/mutation_log.jsonl` | 图谱写入审计日志 |
195
-
196
- ---
197
-
198
- ## 📋 核心特点
199
-
200
- - **分层记忆**: 把"原始会话、结构化事件、抽象规则"拆分存储,便于分别检索与治理
201
- - **图谱派生**: 实体关系来自事件层的 `entities` / `relations`,查询时动态生成图结构
202
- - **会话结束写入**: message hook 默认仅缓存,会在 `session_end` 后批量抽取并落盘
203
- - **严格导入门禁**: 历史导入内容必须经 LLM 提取判定后才写入事件层与图谱层
204
- - **三阶段去重**: SimHash 预过滤 → MinHash 精比对 → 向量余弦相似度
205
- - **自动演进**: 可结合 `autoSync`、`autoReflect` 持续把会话经验转化为长期可复用记忆
206
-
207
- ---
208
-
209
- ## ⚠️ 注意事项
210
-
211
- 1. **API Key 安全**: 使用 `${EMBEDDING_API_KEY}`、`${LLM_API_KEY}`、`${RERANKER_API_KEY}` 等环境变量而非硬编码
212
- 2. **向量维度**: 必须与嵌入模型匹配,如 `text-embedding-3-large` 为 3072
213
- 3. **重排序**: 可选配置 `reranker` 以提升检索精度
214
- 4. **外部传输**: 检索与反思会调用你配置的 embedding/llm/reranker endpoint,请使用可信服务并最小化密钥权限
215
- 5. **会话数据**: 启用 `autoSync` 时会读取 OpenClaw 会话文件并写入本地记忆目录,生产环境建议先小范围验证
216
-
217
- ---
218
-
219
- ## � 致谢
220
-
221
- 感谢 [memory-lancedb-pro](https://github.com/CortexReach/memory-lancedb-pro) 项目作者,本插件的核心想法借鉴了该项目。
222
-
223
- ---
224
-
225
- ## �📄 许可证
226
-
1
+ # OpenClaw Cortex Memory
2
+
3
+ OpenClaw 长期记忆插件 - 专为 OpenClaw AI 助手设计的智能记忆系统
4
+
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
6
+ [![Node.js: 22+](https://img.shields.io/badge/Node.js-22+-green.svg)](https://nodejs.org/)
7
+ [![OpenClaw: Plugin](https://img.shields.io/badge/OpenClaw-Plugin-orange.svg)](https://github.com/openclaw)
8
+
9
+ 面向 OpenClaw 的长期记忆插件,集成多路检索、事件归档、图谱关系、向量化与反衰减排序,支持历史增量导入、规则反思和可观测诊断,帮助 Agent 在跨会话中持续积累并稳定调用高价值记忆。
10
+
11
+ ---
12
+
13
+ ## 核心能力
14
+
15
+ ### 1) 语义检索
16
+ - 多路召回:`keyword / BM25 / vector / graph` 混合检索
17
+ - 排序融合:加权打分 + RRF + 可选 reranker
18
+ - 结果融合:可选 `readFusion`,支持权威融合返回
19
+ - 时序建模:`memoryDecay` + 命中反衰减(anti-decay)
20
+
21
+ ### 2) 事件存储
22
+ - 分层写入:`active`(会话)与 `archive`(结构化事件)
23
+ - 摘要优先:归档记录保留 `summary` 与 `source_text`
24
+ - 向量分块:支持 summary/evidence 双通道向量记录
25
+ - 增量同步:按状态文件增量导入历史会话
26
+
27
+ ### 3) 图谱关系
28
+ - 图谱独立层:`graph/memory.jsonl` 独立于 archive 文本层
29
+ - 关系追溯:每条关系可追溯 `source_event_id`
30
+ - 关系查询:`query_graph` 支持方向、关系类型、路径搜索
31
+ - 质量门禁:`graphQualityMode` 支持 `off/warn/strict`
32
+
33
+ ### 4) 规则演进
34
+ - 反思沉淀:`reflect_memory` 将事件抽象为规则
35
+ - 去重治理:规则与事件均有去重控制,避免污染
36
+ - 规则复用:规则写入 `CORTEX_RULES.md` 供后续任务复用
37
+
38
+ ### 5) 运维诊断
39
+ - `diagnostics`:模型连通、层级状态、字段对齐检查
40
+ - `backfill_embeddings`:支持 `incremental / vector_only / full`
41
+ - 完整状态文件:便于快速定位同步、回填、质量问题
42
+
43
+ ---
44
+
45
+ ## 已注册工具(与当前代码一致)
46
+
47
+ - `search_memory`
48
+ - `store_event`
49
+ - `query_graph`
50
+ - `get_hot_context`
51
+ - `get_auto_context`
52
+ - `reflect_memory`
53
+ - `sync_memory`
54
+ - `backfill_embeddings`
55
+ - `delete_memory`
56
+ - `diagnostics`
57
+
58
+ ### 工具参数速览
59
+
60
+ | 工具 | 关键参数 |
61
+ |------|------|
62
+ | `search_memory` | `query`, `top_k` |
63
+ | `store_event` | `summary`, `entities`, `entity_types`, `outcome`, `relations` |
64
+ | `query_graph` | `entity`, `rel`, `dir`, `path_to`, `max_depth` |
65
+ | `get_hot_context` | `limit` |
66
+ | `get_auto_context` | `include_hot` |
67
+ | `backfill_embeddings` | `layer`, `batch_size`, `max_retries`, `retry_failed_only`, `rebuild_mode` |
68
+ | `delete_memory` | `memory_id` |
69
+
70
+ ---
71
+
72
+ ## 快速开始
73
+
74
+ ### 安装
75
+
76
+ ```bash
77
+ cd ~/openclaw
78
+ openclaw plugins install clawhub:openclaw-cortex-memory
79
+ openclaw plugins enable openclaw-cortex-memory
80
+ openclaw gateway restart
81
+ ```
82
+
83
+ ### 卸载
84
+
85
+ ```bash
86
+ cd ~/openclaw
87
+ openclaw plugins disable openclaw-cortex-memory
88
+ openclaw plugins uninstall openclaw-cortex-memory
89
+ openclaw gateway restart
90
+ ```
91
+
92
+ 如需卸载插件但保留记忆数据:
93
+
94
+ ```bash
95
+ cd ~/openclaw
96
+ openclaw plugins disable openclaw-cortex-memory
97
+ openclaw plugins uninstall openclaw-cortex-memory --keep-data
98
+ openclaw gateway restart
99
+ ```
100
+
101
+ ### 最小配置(推荐先跑起来)
102
+
103
+ ```json
104
+ {
105
+ "plugins": {
106
+ "allow": ["openclaw-cortex-memory"],
107
+ "slots": { "memory": "openclaw-cortex-memory" },
108
+ "entries": {
109
+ "openclaw-cortex-memory": {
110
+ "enabled": true,
111
+ "config": {
112
+ "autoSync": true,
113
+ "autoReflect": false,
114
+ "graphQualityMode": "warn",
115
+ "embedding": {
116
+ "provider": "api",
117
+ "model": "text-embedding-3-large",
118
+ "apiKey": "${EMBEDDING_API_KEY}",
119
+ "baseURL": "https://your-embedding-endpoint/v1",
120
+ "dimensions": 3072
121
+ },
122
+ "llm": {
123
+ "provider": "api",
124
+ "model": "gpt-4",
125
+ "apiKey": "${LLM_API_KEY}",
126
+ "baseURL": "https://your-llm-endpoint/v1"
127
+ },
128
+ "reranker": {
129
+ "provider": "api",
130
+ "model": "BAAI/bge-reranker-v2-m3",
131
+ "apiKey": "${RERANKER_API_KEY}",
132
+ "baseURL": "https://your-reranker-endpoint/v1/rerank"
133
+ }
134
+ }
135
+ }
136
+ }
137
+ }
138
+ }
139
+ ```
140
+
141
+ <details>
142
+ <summary>高级配置(默认已内置,不懂可以不改)</summary>
143
+
144
+ - `readFusion`:融合候选数、通道权重、通道 TopK、最小 lexical/semantic 命中、长度归一
145
+ - `memoryDecay`:最小衰减地板、默认半衰期、事件类型半衰期、anti-decay 参数
146
+ - `vectorChunking`:分块大小、重叠、evidence 最大分块数
147
+ - `writePolicy`:archive/active 质量阈值与文本长度限制
148
+ - `readTuning`:打分权重、RRF 参数、recency 分桶、auto-context 轻量检索
149
+
150
+ </details>
151
+
152
+ ---
153
+
154
+ ## 数据目录
155
+
156
+ - `<dbPath>/MEMORY.md`
157
+ - `<dbPath>/CORTEX_RULES.md`
158
+ - `<dbPath>/sessions/active/sessions.jsonl`
159
+ - `<dbPath>/sessions/archive/sessions.jsonl`
160
+ - `<dbPath>/graph/memory.jsonl`
161
+ - `<dbPath>/graph/mutation_log.jsonl`
162
+ - `<dbPath>/vector/lancedb`
163
+ - `<dbPath>/vector/lancedb_events.jsonl`
164
+ - `<dbPath>/.sync_state.json`
165
+ - `<dbPath>/.session_end_state.json`
166
+ - `<dbPath>/.rule_store_state.json`
167
+ - `<dbPath>/.dedup_index.json`
168
+ - `<dbPath>/.read_hit_stats.json`
169
+
170
+ ---
171
+
172
+ ## 常用命令
173
+
174
+ ```bash
175
+ npm run typecheck
176
+ npm run test:graph-quality
177
+ npm run test:graph-quality-zh
178
+ ```
179
+
180
+ ---
181
+
227
182
  MIT License
228
-
229
- 发布签名见 [SIGNATURE.md](SIGNATURE.md)