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

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 (65) hide show
  1. package/README.md +115 -90
  2. package/SKILL.md +96 -32
  3. package/dist/index.d.ts +52 -15
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +677 -1195
  6. package/dist/index.js.map +1 -1
  7. package/dist/openclaw.plugin.json +157 -5
  8. package/dist/src/dedup/three_stage_deduplicator.d.ts +25 -0
  9. package/dist/src/dedup/three_stage_deduplicator.d.ts.map +1 -0
  10. package/dist/src/dedup/three_stage_deduplicator.js +225 -0
  11. package/dist/src/dedup/three_stage_deduplicator.js.map +1 -0
  12. package/dist/src/engine/memory_engine.d.ts +2 -1
  13. package/dist/src/engine/memory_engine.d.ts.map +1 -1
  14. package/dist/src/engine/ts_engine.d.ts +95 -0
  15. package/dist/src/engine/ts_engine.d.ts.map +1 -1
  16. package/dist/src/engine/ts_engine.js +918 -38
  17. package/dist/src/engine/ts_engine.js.map +1 -1
  18. package/dist/src/engine/types.d.ts +11 -0
  19. package/dist/src/engine/types.d.ts.map +1 -1
  20. package/dist/src/graph/ontology.d.ts +53 -0
  21. package/dist/src/graph/ontology.d.ts.map +1 -0
  22. package/dist/src/graph/ontology.js +252 -0
  23. package/dist/src/graph/ontology.js.map +1 -0
  24. package/dist/src/reflect/reflector.d.ts +7 -0
  25. package/dist/src/reflect/reflector.d.ts.map +1 -1
  26. package/dist/src/reflect/reflector.js +75 -1
  27. package/dist/src/reflect/reflector.js.map +1 -1
  28. package/dist/src/session/session_end.d.ts +56 -0
  29. package/dist/src/session/session_end.d.ts.map +1 -1
  30. package/dist/src/session/session_end.js +270 -55
  31. package/dist/src/session/session_end.js.map +1 -1
  32. package/dist/src/store/archive_store.d.ts +115 -0
  33. package/dist/src/store/archive_store.d.ts.map +1 -0
  34. package/dist/src/store/archive_store.js +446 -0
  35. package/dist/src/store/archive_store.js.map +1 -0
  36. package/dist/src/store/embedding_utils.d.ts +32 -0
  37. package/dist/src/store/embedding_utils.d.ts.map +1 -0
  38. package/dist/src/store/embedding_utils.js +173 -0
  39. package/dist/src/store/embedding_utils.js.map +1 -0
  40. package/dist/src/store/read_store.d.ts +59 -0
  41. package/dist/src/store/read_store.d.ts.map +1 -1
  42. package/dist/src/store/read_store.js +1114 -17
  43. package/dist/src/store/read_store.js.map +1 -1
  44. package/dist/src/store/vector_store.d.ts +43 -0
  45. package/dist/src/store/vector_store.d.ts.map +1 -0
  46. package/dist/src/store/vector_store.js +200 -0
  47. package/dist/src/store/vector_store.js.map +1 -0
  48. package/dist/src/store/write_store.d.ts +45 -0
  49. package/dist/src/store/write_store.d.ts.map +1 -1
  50. package/dist/src/store/write_store.js +230 -0
  51. package/dist/src/store/write_store.js.map +1 -1
  52. package/dist/src/sync/session_sync.d.ts +52 -2
  53. package/dist/src/sync/session_sync.d.ts.map +1 -1
  54. package/dist/src/sync/session_sync.js +474 -22
  55. package/dist/src/sync/session_sync.js.map +1 -1
  56. package/dist/src/utils/runtime_env.d.ts +4 -0
  57. package/dist/src/utils/runtime_env.d.ts.map +1 -0
  58. package/dist/src/utils/runtime_env.js +51 -0
  59. package/dist/src/utils/runtime_env.js.map +1 -0
  60. package/openclaw.plugin.json +157 -5
  61. package/package.json +21 -6
  62. package/scripts/cli.js +19 -14
  63. package/scripts/uninstall.js +22 -5
  64. package/index.ts +0 -2092
  65. package/scripts/install.js +0 -27
package/README.md CHANGED
@@ -1,85 +1,114 @@
1
1
  # OpenClaw Cortex Memory
2
2
 
3
- OpenClaw 长期记忆插件,提供跨会话检索、事件存储、规则反思、增量同步与运行诊断能力。
4
- 可作为 OpenClaw 的 memory slot 直接接入,支持从历史会话持续沉淀可复用记忆。
3
+ OpenClaw 长期记忆插件 - 专为 OpenClaw AI 助手设计的智能记忆系统
5
4
 
6
- ## 功能特性
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)
7
8
 
8
- | 特性 | 说明 |
9
- |------|------|
10
- | 语义检索 | `search_memory` 支持 query + top_k |
11
- | 事件存储 | `store_event` 将摘要写入归档 |
12
- | 上下文注入 | `get_hot_context` / `get_auto_context` |
13
- | 增量同步 | `sync_memory` 按状态文件增量导入 |
14
- | 规则演进 | `reflect_memory` 更新 `CORTEX_RULES.md` |
15
- | 运行诊断 | `diagnostics` 检查本地存储状态 |
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` 供后续任务复用
16
34
 
17
- ## 安装
35
+ ### 🔧 运维诊断
36
+ - **系统诊断**: `diagnostics` 检查本地存储状态与 API 连通性
37
+ - **向量回填**: `backfill_embeddings` 支持 incremental / vector_only / full 三种模式重建向量层
38
+ - **可观测性**: 通过状态文件快速定位数据目录、同步与反思异常
39
+
40
+ ---
41
+
42
+ ## 🚀 快速开始
18
43
 
19
44
  ### 前置要求
20
45
 
21
46
  - Node.js 22+
22
47
  - OpenAI API Key(或其他兼容 API)
23
48
 
24
- ### 安装步骤
49
+ ### 安装
25
50
 
26
- 快速安装(推荐,npm 发布版):
51
+ **快速安装(推荐):**
27
52
 
28
53
  ```bash
29
54
  cd ~/openclaw
30
- pnpm openclaw plugins install openclaw-cortex-memory@alpha
31
- pnpm openclaw plugins enable openclaw-cortex-memory
32
- pnpm openclaw gateway restart
55
+ openclaw plugins install clawhub:openclaw-cortex-memory
56
+ openclaw plugins enable openclaw-cortex-memory
33
57
  ```
34
58
 
35
- 后续更新:
59
+ **npm 安装方式:**
36
60
 
37
61
  ```bash
38
62
  cd ~/openclaw
39
- pnpm openclaw plugins install openclaw-cortex-memory@alpha
40
- pnpm openclaw gateway restart
63
+ openclaw plugins install openclaw-cortex-memory@alpha
64
+ openclaw plugins enable openclaw-cortex-memory
41
65
  ```
42
-
43
- 安装前如果 `openclaw.json` 已经提前写了 `allow/slots/entries` 指向本插件,先临时移除,安装后再加回,避免安装前校验报 `plugin not found`。
44
-
45
- ### 本地打包安装(源码模式)
66
+ **本地包安装方式(当 ClawHub/npm 解析受网络影响时):**
46
67
 
47
68
  ```bash
48
- git clone https://github.com/deki18/openclaw-cortex-memory.git ~/openclaw-cortex-memory-src
49
- cd ~/openclaw-cortex-memory-src
50
- npm install && npm run build && npm pack
69
+ curl -L -o /tmp/cortex.tgz https://registry.npmjs.org/openclaw-cortex-memory/-/openclaw-cortex-memory-0.1.0-Alpha.20.tgz
51
70
  cd ~/openclaw
52
- pnpm openclaw plugins install ~/openclaw-cortex-memory-src/openclaw-cortex-memory-0.1.0-Alpha.1.tgz
53
- pnpm openclaw gateway restart
71
+ openclaw plugins install /tmp/cortex.tgz
72
+ openclaw plugins enable openclaw-cortex-memory
73
+ rm -f /tmp/cortex.tgz
54
74
  ```
55
75
 
56
- ### 本地开发模式(无安装记录)
76
+ **本地开发模式:**
57
77
 
58
78
  ```bash
59
79
  cd ~/.openclaw/extensions
60
80
  git clone https://github.com/deki18/openclaw-cortex-memory.git
61
81
  cd openclaw-cortex-memory
62
- npm install
82
+ npm install && npm run build
63
83
  ```
64
84
 
65
- `npm install` 会自动执行 TypeScript 构建并生成 `dist/`,但这种方式默认不写 OpenClaw 安装记录。
66
-
67
- ## 配置
85
+ ### 配置
68
86
 
69
87
  在 `openclaw.json` 中添加:
70
88
 
71
89
  ```json
72
90
  {
73
91
  "plugins": {
92
+ "allow": ["openclaw-cortex-memory"],
74
93
  "slots": { "memory": "openclaw-cortex-memory" },
75
94
  "entries": {
76
95
  "openclaw-cortex-memory": {
77
96
  "enabled": true,
78
97
  "config": {
79
98
  "engineMode": "ts",
80
- "dbPath": "<optional-memory-dir>",
81
99
  "autoSync": true,
82
- "autoReflect": 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
+ },
83
112
  "embedding": {
84
113
  "provider": "api",
85
114
  "model": "text-embedding-3-large",
@@ -106,68 +135,33 @@ npm install
106
135
  }
107
136
  ```
108
137
 
109
- ### 配置项说明
110
-
111
- | 配置项 | 必填 | 说明 |
112
- |--------|------|------|
113
- | `embedding.provider` | 是 | 建议使用 `api`(统一第三方接口模式) |
114
- | `embedding.model` | 是 | 嵌入模型名称 |
115
- | `embedding.dimensions` | 否 | 向量维度,需与模型匹配 |
116
- | `embedding.apiKey` | 是 | Embedding API Key(建议 `${EMBEDDING_API_KEY}`) |
117
- | `embedding.baseURL` | 是 | Embedding API 端点 |
118
- | `llm.provider` | 是 | 建议使用 `api` |
119
- | `llm.model` | 是 | LLM 模型名称 |
120
- | `llm.apiKey` | 是 | LLM API Key(建议 `${LLM_API_KEY}`) |
121
- | `llm.baseURL` | 是 | LLM API 端点 |
122
- | `reranker.provider` | 否 | 建议使用 `api` |
123
- | `reranker.model` | 是 | Reranker 模型名称 |
124
- | `reranker.apiKey` | 是 | Reranker API Key(建议 `${RERANKER_API_KEY}`) |
125
- | `reranker.baseURL` | 是 | Reranker API 端点 |
126
- | `engineMode` | 否 | 固定 `ts` |
127
- | `dbPath` | 否 | 数据目录,默认 `<plugin-dir>/data/memory` |
128
- | `autoSync` | 否 | 会话结束时自动同步历史记录,默认 `true` |
129
- | `autoReflect` | 否 | 自动触发记忆反思,默认 `false` |
130
-
131
138
  ### 启动
132
139
 
133
140
  ```bash
134
- pnpm openclaw config validate
135
- pnpm openclaw gateway restart
141
+ openclaw config validate
142
+ openclaw gateway restart
136
143
  ```
137
144
 
138
- ### 主 Agent 注入说明
139
-
140
- 首次接入后,建议把下面这段发给主 Agent,确保其按记忆工作流执行:
141
-
142
- ```text
143
- 你已接入 Cortex Memory。请遵循以下规则:
144
- 1) 当用户询问历史对话、偏好、项目上下文时,先调用 search_memory 再回答。
145
- 2) 需要当前会话热上下文时调用 get_hot_context。
146
- 3) 需要自动召回相关记忆时调用 get_auto_context。
147
- 4) 在一件重要事情结束并形成明确结果后,再调用 store_event 记录(不要在过程进行中频繁记录)。
148
- 5) 需要实体关联关系时调用 query_graph。
149
- 6) 当任务经历“失败→调整→最终成功”时,优先用 store_event 记录失败原因与成功方案,再调用 reflect_memory 沉淀可复用规则。
150
- 7) 需要导入历史会话时调用 sync_memory。
151
- 8) 出现配置校验失败、记忆读写异常、检索结果异常或数据目录问题时,优先调用 diagnostics。
152
- 9) 同一任务内不要反复调用 store_event 或 reflect_memory;仅在关键节点或任务收尾时触发一次。
153
- 10) 不要臆造历史事实;无法确认时必须先检索。
154
- ```
145
+ ---
155
146
 
156
- ## 可用工具
147
+ ## 🛠️ 可用工具
157
148
 
158
149
  | 工具 | 说明 |
159
150
  |------|------|
160
- | `search_memory` | 语义搜索记忆库,支持 top_k 参数 |
151
+ | `search_memory` | 语义搜索记忆库,支持 query + top_k |
161
152
  | `store_event` | 存储事件,可包含实体和关系 |
162
- | `query_graph` | 查询实体关系图谱 |
153
+ | `query_graph` | 查询实体关系图谱,支持关系过滤、方向过滤与路径查询 |
163
154
  | `get_hot_context` | 获取热上下文(CORTEX_RULES.md + 近期会话) |
164
155
  | `get_auto_context` | 自动检索相关记忆 + 热上下文 |
165
156
  | `reflect_memory` | 触发记忆反思,将事件转化为规则 |
166
- | `sync_memory` | 同步 OpenClaw 历史会话(增量) |
157
+ | `sync_memory` | 同步 OpenClaw 历史会话(增量),并通过 LLM 判定后写入事件/图谱 |
158
+ | `backfill_embeddings` | 向量回填/重建(支持 `incremental`、`vector_only`、`full`) |
167
159
  | `delete_memory` | 删除指定记忆 |
168
- | `diagnostics` | 系统诊断 |
160
+ | `diagnostics` | 系统诊断(含 embedding/LLM/reranker 连通性检查) |
169
161
 
170
- ## CLI 命令
162
+ ---
163
+
164
+ ## 💻 CLI 命令
171
165
 
172
166
  CLI 命令需在插件目录运行:
173
167
 
@@ -180,7 +174,9 @@ npx cortex-memory uninstall --keep-data # 卸载但保留数据
180
174
  npx cortex-memory help # 查看命令帮助
181
175
  ```
182
176
 
183
- ## 数据存储
177
+ ---
178
+
179
+ ## 📁 数据存储结构
184
180
 
185
181
  | 路径 | 说明 |
186
182
  |------|------|
@@ -188,17 +184,46 @@ npx cortex-memory help # 查看命令帮助
188
184
  | `<dbPath>/CORTEX_RULES.md` | 规则文件 |
189
185
  | `<dbPath>/sessions/active/sessions.jsonl` | 活跃会话 |
190
186
  | `<dbPath>/sessions/archive/sessions.jsonl` | 归档事件 |
187
+ | `<dbPath>/vector/lancedb` | LanceDB 向量表(可用时) |
188
+ | `<dbPath>/vector/lancedb_events.jsonl` | 向量回退存储(LanceDB不可用时) |
191
189
  | `<dbPath>/.sync_state.json` | 同步增量状态 |
192
190
  | `<dbPath>/.session_end_state.json` | session_end 幂等状态 |
193
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` 持续把会话经验转化为长期可复用记忆
194
206
 
195
- ## 注意事项
207
+ ---
196
208
 
197
- 1. **API Key 安全**:使用环境变量 `${OPENAI_API_KEY}` 而非硬编码
198
- 2. **向量维度**:必须与嵌入模型匹配,如 `text-embedding-3-large` 为 3072
199
- 3. **重排序**:可选配置 `reranker` 以提升检索精度
200
- 4. **单栈运行**:当前版本为纯 TS,无 Python 运行时依赖
209
+ ## ⚠️ 注意事项
201
210
 
202
- ## 许可证
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
+ ## �📄 许可证
203
226
 
204
227
  MIT License
228
+
229
+ 发布签名见 [SIGNATURE.md](SIGNATURE.md)
package/SKILL.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: cortex-memory
3
- description: 长期记忆系统(纯 TypeScript)。Use when user asks about past conversations, preferences, project history, or needs to remember information across sessions.
3
+ description: 长期记忆系统(纯 TypeScript)。适用于历史对话回溯、偏好记忆、项目上下文延续与跨会话信息复用场景。
4
4
  homepage: https://github.com/deki18/openclaw-cortex-memory
5
5
  metadata:
6
6
  {
@@ -8,27 +8,27 @@ metadata:
8
8
  "emoji": "🧠",
9
9
  "os": ["darwin", "linux", "win32"],
10
10
  "requires": {
11
- "env": ["OPENAI_API_KEY"]
11
+ "env": ["EMBEDDING_API_KEY", "LLM_API_KEY", "RERANKER_API_KEY"]
12
12
  },
13
- "primaryEnv": "OPENAI_API_KEY"
13
+ "primaryEnv": "EMBEDDING_API_KEY"
14
14
  }
15
15
  }
16
16
  ---
17
17
 
18
- # Cortex Memory - 长期记忆系统
18
+ # Cortex Memory · 长期记忆系统
19
19
 
20
- 为 OpenClaw Agent 提供持久化记忆能力,当前为纯 TypeScript 单栈实现。
20
+ 为 OpenClaw Agent 提供稳定的跨会话记忆能力。
21
21
 
22
22
  ## 使用场景
23
23
 
24
- **USE when:**
24
+ **建议使用场景(USE when):**
25
25
  - 用户询问过去的对话内容或决策
26
26
  - 需要记住用户偏好、项目信息
27
27
  - 跨会话保持上下文
28
28
  - 查询实体关系(人物、项目、技术)
29
29
  - 存储重要事件或里程碑
30
30
 
31
- **DON'T use when:**
31
+ **不建议使用场景(DON'T use when):**
32
32
  - 仅需当前会话的临时信息
33
33
  - 查询实时数据(天气、新闻等)
34
34
 
@@ -36,36 +36,54 @@ metadata:
36
36
 
37
37
  ### 安装
38
38
 
39
- 快速安装(推荐,npm 发布版):
39
+ 命令前缀说明:
40
+ - 若你是全局安装 OpenClaw,请直接使用 `openclaw ...`
41
+ - 若你使用源码安装的 OpenClaw ,请使用 `pnpm openclaw ...`
42
+
43
+ 快速安装(推荐,显式来源):
40
44
 
41
45
  ```bash
42
46
  cd ~/openclaw
43
- pnpm openclaw plugins install openclaw-cortex-memory@alpha
44
- pnpm openclaw plugins enable openclaw-cortex-memory
45
- pnpm openclaw gateway restart
47
+ openclaw plugins install clawhub:openclaw-cortex-memory
48
+ openclaw plugins enable openclaw-cortex-memory
49
+ openclaw gateway restart
46
50
  ```
47
51
 
48
- ### 后续更新
52
+ npm 安装方式:
49
53
 
50
54
  ```bash
51
55
  cd ~/openclaw
52
- pnpm openclaw plugins install openclaw-cortex-memory@alpha
53
- pnpm openclaw gateway restart
56
+ openclaw plugins install openclaw-cortex-memory@alpha
57
+ openclaw plugins enable openclaw-cortex-memory
58
+ openclaw gateway restart
54
59
  ```
55
60
 
56
- 安装前如果 `openclaw.json` 已经提前写了 `allow/slots/entries` 指向本插件,先临时移除,安装后再加回,避免安装前校验报 `plugin not found`。
61
+ 第三种安装方式(当 ClawHub/npm 解析受网络影响时):
57
62
 
58
- ### 本地打包安装(源码模式)
63
+ ```bash
64
+ cd ~/openclaw
65
+ curl -L -o /tmp/cortex.tgz https://registry.npmjs.org/openclaw-cortex-memory/-/openclaw-cortex-memory-0.1.0-Alpha.20.tgz
66
+ openclaw plugins install /tmp/cortex.tgz
67
+ openclaw plugins enable openclaw-cortex-memory
68
+ rm -f /tmp/cortex.tgz
69
+ ```
70
+
71
+ ### 后续更新
59
72
 
60
73
  ```bash
61
- git clone https://github.com/deki18/openclaw-cortex-memory.git ~/openclaw-cortex-memory-src
62
- cd ~/openclaw-cortex-memory-src
63
- npm install && npm run build && npm pack
64
74
  cd ~/openclaw
65
- pnpm openclaw plugins install ~/openclaw-cortex-memory-src/openclaw-cortex-memory-0.1.0-Alpha.1.tgz
66
- pnpm openclaw gateway restart
75
+ rm -rf ~/.openclaw/extensions/openclaw-cortex-memory
76
+ openclaw plugins install clawhub:openclaw-cortex-memory
77
+ openclaw plugins enable openclaw-cortex-memory
78
+ openclaw gateway restart
67
79
  ```
68
80
 
81
+ 说明:
82
+ - 推荐显式安装来源,减少 ClawHub-first 时代的来源歧义。
83
+ - 使用 `plugins install` 的安装记录方式,避免 `loaded without install/load-path provenance`。
84
+ - 保持 `plugins.allow` 显式包含 `openclaw-cortex-memory`,避免运行时把插件判定为未绑定信任源。
85
+ - 若 `plugins install openclaw-cortex-memory` 在 ClawHub 解析阶段失败,可使用上述 tgz 方式直接安装。
86
+
69
87
  ### 本地开发模式(无安装记录)
70
88
 
71
89
  ```bash
@@ -82,15 +100,26 @@ npm install
82
100
  ```json
83
101
  {
84
102
  "plugins": {
103
+ "allow": ["openclaw-cortex-memory"],
85
104
  "slots": { "memory": "openclaw-cortex-memory" },
86
105
  "entries": {
87
106
  "openclaw-cortex-memory": {
88
107
  "enabled": true,
89
108
  "config": {
90
109
  "engineMode": "ts",
91
- "dbPath": "<optional-memory-dir>",
92
110
  "autoSync": true,
93
- "autoReflect": true,
111
+ "autoReflect": false,
112
+ "autoReflectIntervalMinutes": 30,
113
+ "readFusion": {
114
+ "enabled": true,
115
+ "authoritative": true
116
+ },
117
+ "memoryDecay": {
118
+ "enabled": true,
119
+ "antiDecay": {
120
+ "enabled": true
121
+ }
122
+ },
94
123
  "embedding": {
95
124
  "provider": "api",
96
125
  "model": "text-embedding-3-large",
@@ -117,6 +146,14 @@ npm install
117
146
  }
118
147
  ```
119
148
 
149
+ 关键功能开关(建议显式配置):
150
+
151
+ - `readFusion.enabled`:开启多路召回后的 LLM 融合
152
+ - `readFusion.authoritative`:仅返回融合后的权威记忆包
153
+ - `memoryDecay.enabled`:开启按事件类型的半衰期衰减
154
+ - `memoryDecay.antiDecay.enabled`:开启命中频次反衰减
155
+ - `autoReflect`:建议验证稳定后再开启(默认 false)
156
+
120
157
  ### 启动
121
158
 
122
159
  ```bash
@@ -126,7 +163,7 @@ openclaw gateway restart
126
163
 
127
164
  ### 主Agent注入说明
128
165
 
129
- 首次接入后,建议把下面这段发给主 Agent,确保它会主动使用记忆工具:
166
+ 首次接入后,建议把下面这段发给主 Agent,确保其按统一记忆工作流调用工具:
130
167
 
131
168
  ```text
132
169
  你已接入 Cortex Memory。请遵循以下规则:
@@ -137,9 +174,10 @@ openclaw gateway restart
137
174
  5) 需要实体关联关系时调用 query_graph。
138
175
  6) 当任务经历“失败→调整→最终成功”时,优先用 store_event 记录失败原因与成功方案,再调用 reflect_memory 沉淀可复用规则。
139
176
  7) 需要导入历史会话时调用 sync_memory。
140
- 8) 出现配置校验失败、记忆读写异常、检索结果异常或数据目录问题时,优先调用 diagnostics
141
- 9) 同一任务内不要反复调用 store_event 或 reflect_memory;仅在关键节点或任务收尾时触发一次。
142
- 10) 不要臆造历史事实;无法确认时必须先检索。
177
+ 8) diagnostics 显示 active/archive 存在未向量化记录,或迁移后需要重建向量层时,调用 backfill_embeddings(按需选择 incremental/vector_only/full)。
178
+ 9) 出现配置校验失败、记忆读写异常、检索结果异常或数据目录问题时,优先调用 diagnostics。
179
+ 10) 同一任务内不要反复调用 store_event 或 reflect_memory;仅在关键节点或任务收尾时触发一次。
180
+ 11) 不要臆造历史事实;无法确认时必须先检索。
143
181
  ```
144
182
 
145
183
  ## 可用工具
@@ -168,12 +206,16 @@ openclaw gateway restart
168
206
 
169
207
  ### query_graph
170
208
 
171
- 查询归档事件中的实体共现关系。
209
+ 查询归档事件中的实体关系(relations 优先,共现关系回退)。
172
210
 
173
211
  **参数:**
174
212
  | 参数 | 类型 | 必需 | 说明 |
175
213
  |------|------|------|------|
176
214
  | entity | string | 是 | 实体名称 |
215
+ | rel | string | 否 | 关系类型过滤(如 `depends_on`) |
216
+ | dir | string | 否 | 方向过滤:`incoming` / `outgoing` / `both` |
217
+ | path_to | string | 否 | 查询从 `entity` 到目标实体的路径 |
218
+ | max_depth | number | 否 | 路径最大深度(2~4) |
177
219
 
178
220
  ### get_hot_context
179
221
 
@@ -199,7 +241,20 @@ openclaw gateway restart
199
241
 
200
242
  ### sync_memory
201
243
 
202
- 增量同步会话记录到本地记忆(无参数)。
244
+ 增量同步会话记录,且必须经 LLM 提取判定后才写入事件/图谱记忆(无参数)。
245
+
246
+ ### backfill_embeddings
247
+
248
+ 按层回填或重建向量,支持全文分块向量重建。
249
+
250
+ **参数:**
251
+ | 参数 | 类型 | 必需 | 说明 |
252
+ |------|------|------|------|
253
+ | layer | string | 否 | `active` / `archive` / `all` |
254
+ | batch_size | number | 否 | 每批处理数量 |
255
+ | max_retries | number | 否 | 失败重试上限 |
256
+ | retry_failed_only | boolean | 否 | 仅处理失败记录 |
257
+ | rebuild_mode | string | 否 | `incremental` / `vector_only` / `full` |
203
258
 
204
259
  ### delete_memory
205
260
 
@@ -212,7 +267,7 @@ openclaw gateway restart
212
267
 
213
268
  ### diagnostics
214
269
 
215
- 运行本地系统诊断,检查数据目录与基础状态。
270
+ 运行本地系统诊断,检查数据目录、分层状态与 embedding/LLM/reranker 连通性。
216
271
 
217
272
  ## 配置选项
218
273
 
@@ -220,7 +275,6 @@ openclaw gateway restart
220
275
  |------|------|--------|------|
221
276
  | embedding.provider | 是 | - | `api`(推荐) |
222
277
  | embedding.model | 是 | - | Embedding 模型名称 |
223
- | embedding.dimensions | 否 | 3072 | 向量维度 |
224
278
  | embedding.apiKey | 是 | ${EMBEDDING_API_KEY} | Embedding API Key |
225
279
  | embedding.baseURL | 是 | - | Embedding API 端点 |
226
280
  | llm.provider | 是 | - | `api`(推荐) |
@@ -235,6 +289,11 @@ openclaw gateway restart
235
289
  | engineMode | 否 | `ts` | 固定为 TS 引擎 |
236
290
  | autoSync | 否 | true | 会话结束自动同步 |
237
291
  | autoReflect | 否 | false | 自动触发反思 |
292
+ | autoReflectIntervalMinutes | 否 | 30 | 自动反思扫描间隔(分钟) |
293
+ | readFusion.enabled | 否 | true | 启用检索重排后的 LLM 融合 |
294
+ | readFusion.authoritative | 否 | true | 仅返回融合权威记忆包 |
295
+ | memoryDecay.enabled | 否 | true | 启用按事件类型半衰期衰减 |
296
+ | memoryDecay.antiDecay.enabled | 否 | true | 启用命中频次反衰减 |
238
297
 
239
298
  ## 数据文件
240
299
 
@@ -244,9 +303,14 @@ openclaw gateway restart
244
303
  | `<dbPath>/CORTEX_RULES.md` | 规则文件 |
245
304
  | `<dbPath>/sessions/active/sessions.jsonl` | 活跃会话记忆 |
246
305
  | `<dbPath>/sessions/archive/sessions.jsonl` | 归档事件 |
306
+ | `<dbPath>/vector/lancedb` | LanceDB 向量表(可用时) |
307
+ | `<dbPath>/vector/lancedb_events.jsonl` | 向量回退存储(LanceDB 不可用时) |
247
308
  | `<dbPath>/.sync_state.json` | 同步增量状态 |
248
309
  | `<dbPath>/.session_end_state.json` | session_end 幂等状态 |
249
310
  | `<dbPath>/.rule_store_state.json` | 规则去重状态 |
311
+ | `<dbPath>/.dedup_index.json` | 三阶段去重索引 |
312
+ | `<dbPath>/.read_hit_stats.json` | 检索命中频次统计(反衰减) |
313
+ | `<dbPath>/graph/mutation_log.jsonl` | 图谱变更审计日志 |
250
314
 
251
315
  ## 错误处理
252
316
 
@@ -265,4 +329,4 @@ openclaw gateway restart
265
329
  ## 依赖
266
330
 
267
331
  - Node.js >= 22
268
- - OPENAI_API_KEY(或兼容 API 配置)
332
+ - EMBEDDING_API_KEY、LLM_API_KEY、RERANKER_API_KEY(或兼容 API 配置)
package/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { EngineMode } from "./src/engine/types";
2
1
  interface EmbeddingConfig {
3
2
  provider: string;
4
3
  model: string;
@@ -6,6 +5,8 @@ interface EmbeddingConfig {
6
5
  baseURL?: string;
7
6
  baseUrl?: string;
8
7
  dimensions?: number;
8
+ timeoutMs?: number;
9
+ maxRetries?: number;
9
10
  }
10
11
  interface LLMConfig {
11
12
  provider: string;
@@ -23,15 +24,56 @@ interface RerankerConfig {
23
24
  }
24
25
  interface CortexMemoryConfig {
25
26
  enabled?: boolean;
27
+ fallbackToBuiltin?: boolean;
26
28
  embedding: EmbeddingConfig;
27
29
  llm: LLMConfig;
28
30
  reranker: RerankerConfig;
29
31
  dbPath?: string;
30
32
  autoSync?: boolean;
33
+ llmRequiredForWrite?: boolean;
31
34
  autoReflect?: boolean;
32
- fallbackToBuiltin?: boolean;
33
- apiUrl?: string;
34
- engineMode?: EngineMode;
35
+ autoReflectIntervalMinutes?: number;
36
+ readFusion?: {
37
+ enabled?: boolean;
38
+ maxCandidates?: number;
39
+ authoritative?: boolean;
40
+ channelWeights?: {
41
+ rules?: number;
42
+ archive?: number;
43
+ vector?: number;
44
+ graph?: number;
45
+ };
46
+ channelTopK?: {
47
+ rules?: number;
48
+ archive?: number;
49
+ vector?: number;
50
+ graph?: number;
51
+ };
52
+ minLexicalHits?: number;
53
+ minSemanticHits?: number;
54
+ lengthNorm?: {
55
+ enabled?: boolean;
56
+ pivotChars?: number;
57
+ strength?: number;
58
+ minFactor?: number;
59
+ };
60
+ };
61
+ vectorChunking?: {
62
+ chunkSize?: number;
63
+ chunkOverlap?: number;
64
+ };
65
+ memoryDecay?: {
66
+ enabled?: boolean;
67
+ minFloor?: number;
68
+ defaultHalfLifeDays?: number;
69
+ halfLifeByEventType?: Record<string, number>;
70
+ antiDecay?: {
71
+ enabled?: boolean;
72
+ maxBoost?: number;
73
+ hitWeight?: number;
74
+ recentWindowDays?: number;
75
+ };
76
+ };
35
77
  }
36
78
  interface ToolContext {
37
79
  agentId: string;
@@ -44,11 +86,6 @@ interface ToolResult {
44
86
  error?: string;
45
87
  errorCode?: string;
46
88
  }
47
- interface BuiltinMemory {
48
- search: (query: string, limit?: number) => Promise<unknown[]>;
49
- store: (content: string, metadata?: Record<string, unknown>) => Promise<string>;
50
- delete: (id: string) => Promise<boolean>;
51
- }
52
89
  interface Logger {
53
90
  debug: (message: string, ...args: unknown[]) => void;
54
91
  info: (message: string, ...args: unknown[]) => void;
@@ -64,10 +101,7 @@ interface OpenClawPluginApi {
64
101
  args?: Record<string, unknown>;
65
102
  context: ToolContext;
66
103
  }) => Promise<ToolResult>;
67
- handler?: (params: {
68
- args?: Record<string, unknown>;
69
- context: ToolContext;
70
- }) => Promise<ToolResult>;
104
+ handler?: (...params: unknown[]) => Promise<ToolResult>;
71
105
  }): void;
72
106
  unregisterTool?(name: string): void;
73
107
  registerHook(hook: {
@@ -80,13 +114,16 @@ interface OpenClawPluginApi {
80
114
  off?(event: string, handler: (payload: unknown, context: ToolContext) => Promise<void> | void): void;
81
115
  unregisterHook?(event: string): void;
82
116
  getLogger?(): Logger;
83
- getBuiltinMemory?(): BuiltinMemory;
117
+ getBuiltinMemory?(): {
118
+ search: (query: string, topK?: number) => Promise<unknown[]>;
119
+ store: (text: string, metadata?: Record<string, unknown>) => Promise<string>;
120
+ delete: (id: string) => Promise<boolean>;
121
+ } | null;
84
122
  }
85
123
  export declare function enable(): Promise<void>;
86
124
  export declare function disable(): Promise<void>;
87
125
  export declare function getStatus(): {
88
126
  enabled: boolean;
89
- serviceRunning: boolean;
90
127
  };
91
128
  export declare function unregister(): Promise<void>;
92
129
  export declare function register(pluginApi: OpenClawPluginApi, userConfig?: Partial<CortexMemoryConfig>): void;