ultra-memory 3.0.2 → 3.0.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/SKILL.md CHANGED
@@ -1,229 +1,268 @@
1
1
  ---
2
2
  name: ultra-memory
3
3
  description: >
4
- ultra-memory AI Agent 的超长会话记忆系统。
5
- 【必须触发-中文】用户说:记住、别忘了、记录一下、上次我们做了什么、帮我回忆、继续上次、从上次继续、不要忘记、上次、跨会话、继续昨天、还有印象吗、我们上次讨论过、之前那个是怎么写的、还记得吗、这次不要忘、帮我追踪、帮我记着
6
- 【必须触发-英文】用户说:remember this、don't forget、recall、what did we do、pick up where we left off、continue from last time、memory、keep track、log this、what was that、remind me、context lost、continue from yesterday、from last session
7
- 【隐式触发】用户描述了一个持续性任务(开发某项目、处理某数据集、持续性工作),即使未说"记住"也必须初始化记忆;对话操作数超过30条;用户说"继续昨天的任务"/"接着上次做"
8
- 【不触发】单次问答("帮我写个正则""查一下天气");纯代码补全、文件格式转换等无状态操作;用户已在 context 中能找到所需信息;用户明确说"不用记录"/"just this once"
4
+ ultra-memory 是多模型 AI 的超长会话记忆系统。
5
+ 【必须触发-中文】用户说以下任意词或短语:记住、别忘了、记录一下、不要忘记、上次我们做了什么、帮我回忆、继续上次的、从上次继续、记忆、帮我记、追踪进度
6
+ 【必须触发-英文】用户说以下任意词或短语:remember、don't forget、recall、what did we do、pick up where we left off、continue from last time、memory、keep track、track progress、log this
7
+ 【隐式触发】用户消息描述了一个持续性任务且消息中包含项目名词("开发X""处理Y数据集""完成Z功能"),即使没有明确说"记住",也必须初始化记忆
8
+ 【不触发】用户只问了一个问题且无后续任务;对话是单次咨询性质;用户说"随便聊聊"或类似表达;用户明确说"不用记录"
9
9
  ---
10
10
 
11
- # Ultra Memory — 超长会话记忆
11
+ # Ultra Memory — 多模型记忆操作手册
12
12
 
13
13
  AI Agent 的操作记忆系统,每次操作后记录,跨会话持久化,可检索可进化。
14
14
 
15
15
  ## 前置说明
16
16
 
17
- 所有脚本已存在于 `~/.ultra-memory/scripts/` 或技能目录下:
18
- - `init.py` — 初始化会话
19
- - `log_op.py` — 记录操作
20
- - `recall.py` — 检索记忆
21
- - `summarize.py` — 压缩摘要
22
- - `restore.py` — 恢复会话
23
- - `extract_entities.py` — 提取实体
17
+ 脚本已存在于 `$SKILL_DIR/scripts/` 目录,直接调用,不需要理解内部架构。
24
18
 
25
- 存储根目录:`~/.ultra-memory/`(可配置环境变量 `ULTRA_MEMORY_HOME`)
26
- 不需要理解内部架构,只需按步骤调用脚本。
19
+ 存储根目录:`$ULTRA_MEMORY_HOME`(默认 `~/.ultra-memory/`)
20
+ 会话 ID 由 init.py 自动生成,模型不需要自己生成。
27
21
 
28
22
  ---
29
23
 
30
24
  ## 步骤一:会话初始化
31
25
 
32
- ### 触发时机
26
+ ### 触发条件
33
27
 
34
- 首次与用户对话时,或用户提到记忆相关触发词时,立即执行。
28
+ 以下任意情况出现时,立即执行初始化命令:
29
+ 1. 首次与用户对话
30
+ 2. 用户说了记忆触发词(见 description)
31
+ 3. 用户描述持续性任务且包含项目名词(隐式触发)
35
32
 
36
33
  ### 执行命令
37
34
 
38
35
  ```bash
39
- python3 <skill_dir>/scripts/init.py --project <项目名>
36
+ python3 $SKILL_DIR/scripts/init.py --project <项目名>
40
37
  ```
41
38
 
42
39
  **参数说明:**
43
- - `--project`:项目名称,用于跨会话分组。不填默认为 `default`。
40
+ - `--project`:项目名称(用于跨会话分组)。从用户消息中提取项目名词作为值;无法提取则用 `default`。
44
41
 
45
- ### 期望输出
42
+ ### 成功标志
43
+
44
+ 输出中必须同时包含以下两个信号:
46
45
 
47
- 输出包含 `MEMORY_READY` 字样,表示会话初始化成功。同时输出 `session_id:`,记录该会话 ID。
46
+ | 输出中的字符串 | 模型必须执行的动作 |
47
+ |-------------|----------------|
48
+ | `MEMORY_READY` | 确认初始化成功 |
49
+ | `session_id: sess_xxxxx` | 从该行提取等号后面的值,保存为当前 `SESSION_ID` 变量 |
48
50
 
49
51
  ### 告知用户
50
52
 
51
- 初始化成功后,立即告知用户:
52
- > "记忆系统已就绪(session_id: xxx),开始记录本次操作。"
53
+ > "记忆系统已就绪(session_id: sess_xxxxx),开始记录本次操作。"
53
54
 
54
55
  ---
55
56
 
56
57
  ## 步骤二:操作记录
57
58
 
58
- **每次**以下任意事件发生时,立即调用 `log_op.py` 追加写入 ops.jsonl。
59
+ ### 触发条件
59
60
 
60
- ### 操作类型与命令对照表
61
+ **每次**用户与 AI 之间发生以下任意事件时,立即执行记录命令:
61
62
 
62
- | 事件 | op_type | 命令示例 |
63
- |------|---------|---------|
64
- | 执行了 shell 命令 | `bash_exec` | `python3 log_op.py --session <id> --type bash_exec --summary "执行了..." --detail '{"cmd":"...","exit_code":0}'` |
65
- | 创建或修改了文件 | `file_write` | `python3 log_op.py --session <id> --type file_write --summary "创建了..." --detail '{"path":"..."}'` |
66
- | 读取了文件 | `file_read` | `python3 log_op.py --session <id> --type file_read --summary "读取了..." --detail '{"path":"..."}'` |
67
- | 进行了重要推理 | `reasoning` | `python3 log_op.py --session <id> --type reasoning --summary "决定用..." --detail '{"confidence":0.9}'` |
68
- | 做出了关键决策 | `decision` | `python3 log_op.py --session <id> --type decision --summary "选用X方案" --detail '{"rationale":"..."}'` |
69
- | 发生了错误或回退 | `error` | `python3 log_op.py --session <id> --type error --summary "报错..." --detail '{"traceback":"..."}'` |
70
- | 用户给了新指令 | `user_instruction` | `python3 log_op.py --session <id> --type user_instruction --summary "用户要求..."` |
71
- | 某个目标已完成 | `milestone` | `python3 log_op.py --session <id> --type milestone --summary "数据清洗模块完成"` |
63
+ | 事件 | op_type | 命令 |
64
+ |------|---------|------|
65
+ | 执行了 shell 命令 | `bash_exec` | `python3 $SKILL_DIR/scripts/log_op.py --session $SESSION_ID --type bash_exec --summary "<一句话描述>" --detail '{"cmd":"<命令>","exit_code":<数字>}'` |
66
+ | 创建或修改了文件 | `file_write` | `python3 $SKILL_DIR/scripts/log_op.py --session $SESSION_ID --type file_write --summary "<一句话描述>" --detail '{"path":"<文件路径>"}'` |
67
+ | 读取了文件 | `file_read` | `python3 $SKILL_DIR/scripts/log_op.py --session $SESSION_ID --type file_read --summary "<一句话描述>" --detail '{"path":"<文件路径>"}'` |
68
+ | 进行了重要推理 | `reasoning` | `python3 $SKILL_DIR/scripts/log_op.py --session $SESSION_ID --type reasoning --summary "<推理结论>" --detail '{"confidence":<0-1>}'` |
69
+ | 做出了关键决策 | `decision` | `python3 $SKILL_DIR/scripts/log_op.py --session $SESSION_ID --type decision --summary "<决策内容>" --detail '{"rationale":"<决策依据>"}'` |
70
+ | 发生了错误或回退 | `error` | `python3 $SKILL_DIR/scripts/log_op.py --session $SESSION_ID --type error --summary "<错误描述>" --detail '{"traceback":"<错误信息>"}'` |
71
+ | 用户给了新指令 | `user_instruction` | `python3 $SKILL_DIR/scripts/log_op.py --session $SESSION_ID --type user_instruction --summary "<用户说的话>"` |
72
+ | 某个目标已完成 | `milestone` | `python3 $SKILL_DIR/scripts/log_op.py --session $SESSION_ID --type milestone --summary "<完成的内容>"` |
72
73
 
73
74
  ### 成功标志
74
75
 
75
- 命令返回 exit code 0 即为成功,输出形如:
76
- > `[ultra-memory] [1] bash_exec: 执行了 pip install pandas,安装成功`
76
+ 命令返回 exit code 0 即为成功。
77
77
 
78
78
  ### 自动标签
79
79
 
80
- `log_op.py` 会根据操作类型和内容自动追加标签(如 `setup`、`dependency`、`code`、`test` 等),无需手动指定 `--tags`。
80
+ `log_op.py` 会根据操作类型和内容自动追加标签(`setup`、`dependency`、`code`、`test` 等),无需手动指定 `--tags`。
81
81
 
82
82
  ---
83
83
 
84
84
  ## 步骤三:记忆检索
85
85
 
86
- ### 触发时机
86
+ ### 触发条件
87
87
 
88
- 用户问及"之前做了什么"、"上次那个函数在哪里"、"之前遇到过这个问题吗"等记忆相关问题时执行。
88
+ 用户问及以下任意问题时执行:
89
+ - "之前做了什么"
90
+ - "上次那个函数在哪里"
91
+ - "之前遇到过这个问题吗"
92
+ - "我们用过哪些文件"
93
+ - "上次选了什么方案"
94
+ - 其他需要查找历史记忆的问题
89
95
 
90
96
  ### 执行命令
91
97
 
92
98
  ```bash
93
- python3 <skill_dir>/scripts/recall.py --session <session_id> --query "<用户问题的关键词>" --top-k 5
99
+ python3 $SKILL_DIR/scripts/recall.py --session $SESSION_ID --query "<用户问题的关键词>" --top-k 5
94
100
  ```
95
101
 
96
- ### 检索范围
97
-
98
- 按以下优先级检索:
99
- 1. 当前会话 `ops.jsonl`(精确匹配)
100
- 2. 当前会话 `summary.md`(摘要快速定位)
101
- 3. 跨会话 `knowledge_base.jsonl`(语义相似)
102
- 4. 跨会话 `user_profile.json`(偏好匹配)
103
-
104
102
  ### 结果展示
105
103
 
106
- 将检索结果直接展示给用户,格式如下:
104
+ 将脚本输出直接展示给用户。输出格式如下:
105
+
107
106
  ```
108
107
  [RECALL] 找到 N 条相关记录:
109
-
110
- [ops #23 · 14:18] <操作摘要>
111
- [summary · 里程碑] <里程碑内容>
112
- [跨会话 · 日期 · 项目] <历史记录摘要>
108
+ 1. [ops #23 · 14:18] <操作摘要>
109
+ 2. [summary · 里程碑] <里程碑内容>
110
+ 3. [知识库 · 标题] <内容摘要>
111
+ 4. [跨会话 · 日期 · 项目] <历史摘要>
113
112
  ```
114
113
 
115
114
  ---
116
115
 
117
116
  ## 步骤四:摘要压缩
118
117
 
119
- ### 触发时机
118
+ ### 触发条件
120
119
 
121
- 满足以下**任一条件**时,立即执行压缩:
120
+ 满足以下**任意条件**时,立即执行压缩命令:
122
121
 
123
122
  1. 操作日志达到 **50 条**
124
123
  2. 距上次压缩超过 **30 分钟**
125
- 3. 用户明确说"总结一下"
126
- 4. 当前 context 占用超过 **60%**
124
+ 3. 用户明确说"总结一下"或"做个总结"
125
+ 4. 脚本输出中包含 `COMPRESS_SUGGESTED` 信号
127
126
 
128
127
  ### 执行命令
129
128
 
130
129
  ```bash
131
- python3 <skill_dir>/scripts/summarize.py --session <session_id> --force
130
+ python3 $SKILL_DIR/scripts/summarize.py --session $SESSION_ID --force
132
131
  ```
133
132
 
134
- ### 期望输出
133
+ ### 成功标志
135
134
 
136
- 生成/更新 `~/.ultra-memory/sessions/<session_id>/summary.md`,输出包含:
137
- - 已完成里程碑([✅] 标记)
138
- - 当前进行中([ ] 标记)
139
- - 下一步建议([💡] 标记)
140
- - 操作统计([📊] 标记)
135
+ 命令返回 exit code 0,输出包含 `摘要压缩完成`。
141
136
 
142
137
  ---
143
138
 
144
139
  ## 步骤五:跨会话恢复
145
140
 
146
- ### 触发时机
141
+ ### 触发条件
147
142
 
148
- 用户说"继续上次"、"从上次继续"、"记得昨天那个项目吗"等时执行。也在新会话开始时(检测到 session_id 变化)自动执行。
143
+ 用户说以下任意表达时执行:
144
+ - "继续上次"
145
+ - "从上次继续"
146
+ - "记得昨天那个项目吗"
147
+ - "还记得吗"
148
+ - "继续上次的工作"
149
149
 
150
150
  ### 执行命令
151
151
 
152
152
  ```bash
153
- python3 <skill_dir>/scripts/restore.py --project <项目名>
153
+ python3 $SKILL_DIR/scripts/restore.py --project <项目名>
154
154
  ```
155
155
 
156
- ### 告知用户
156
+ ### 成功标志
157
157
 
158
- 恢复成功后,立即告知用户:
159
- > "我找到了上次会话的记录。上次我们做到了:[里程碑摘要]。当前状态:[进行中任务]。下一步建议:[具体建议]。"
158
+ 输出中包含 `SESSION_ID=sess_xxxxx` 信号。
160
159
 
161
- 如果找到了用户画像,也在回复中体现用户偏好。
160
+ ### 从输出提取信息
161
+
162
+ | 输出中的字符串 | 模型必须执行的动作 |
163
+ |-------------|----------------|
164
+ | `SESSION_ID=sess_xxxxx` | 从等号后面提取值,保存为新的 `SESSION_ID` |
165
+ | `TASK_STATUS=complete` | 告知用户"上次任务已完成" |
166
+ | `TASK_STATUS=in_progress` | 告知用户"上次任务进行中" |
167
+ | `💬 <自然语言总结>` | 直接将总结说给用户 |
168
+ | `📌 <继续建议>` | 直接将建议说给用户 |
169
+
170
+ ### 告知用户
171
+
172
+ > "我找到了上次会话的记录。{自然语言总结}。{继续建议}。"
162
173
 
163
174
  ---
164
175
 
165
176
  ## 步骤六:记忆进化
166
177
 
167
- 记忆进化在每次操作中持续进行,不打断主任务。
178
+ 记忆进化在操作间隙进行,不打断主任务。
168
179
 
169
- ### 6.1 用户画像更新
180
+ ### 6.1 用户画像积累
170
181
 
171
- **触发时机:**
172
- - 用户纠正了 AI 的代码风格或实现方式
173
- - 用户选择/拒绝了某个技术方案
174
- - 用户明确说出自己的技术栈或偏好
175
- - 用户表示某种工作流程更顺手
182
+ **触发条件(满足任意一条,立即更新画像):**
176
183
 
177
- **执行方式(二选一):**
184
+ 1. 用户纠正了 AI 生成的代码风格
185
+ 2. 用户在两个技术方案中选择了其中一个
186
+ 3. 用户说出自己的技术栈(如"我用 Vue"、"我们用 Python")
187
+ 4. 用户表示某种工作流程更顺手
188
+ 5. 用户明确描述了自己的偏好
189
+
190
+ **执行命令 — 方式 A(MCP 工具,推荐):**
191
+
192
+ 调用 `memory_profile` 工具,`action=update`,`updates` 中填写观察到的偏好字段。
193
+
194
+ **执行命令 — 方式 B(直接修改文件):**
178
195
 
179
- 方式 A — 使用 MCP 工具:
180
196
  ```bash
181
- python3 <skill_dir>/scripts/mcp-server.js # 通过 MCP 调用 memory_profile
197
+ # 文件路径:$ULTRA_MEMORY_HOME/semantic/user_profile.json
198
+ # 只更新观察到的字段,不覆盖已有字段
182
199
  ```
183
200
 
184
- 方式 B — 直接读写 JSON:
185
- ```
186
- 文件路径:~/.ultra-memory/semantic/user_profile.json
187
- ```
201
+ **user_profile.json 可更新字段:**
188
202
 
189
- **user_profile.json 格式:**
190
203
  ```json
191
204
  {
192
- "tech_stack": ["Python", "Vue3"],
193
- "work_style": {"prefers_concise_code": true},
194
- "projects": ["ai-data-qa"],
195
- "language": "zh-CN",
196
- "observed_patterns": ["倾向在实现前讨论方案"]
205
+ "tech_stack": ["观察到的技术栈"],
206
+ "work_style": {
207
+ "confirm_before_implement": true,
208
+ "prefers_concise_code": true
209
+ },
210
+ "language": "zh-CN 或 en",
211
+ "observed_patterns": ["观察到的工作习惯描述"]
197
212
  }
198
213
  ```
199
214
 
200
- ### 6.2 知识库写入
215
+ ### 6.2 知识沉淀
216
+
217
+ **触发条件(满足任意一条,立即写入知识库):**
218
+
219
+ 1. 解决了一个报错或 bug(记录问题现象 + 解决方案)
220
+ 2. 做出了技术选型决策(记录选了什么 + 为什么)
221
+ 3. 发现了某个工具或库的使用技巧
222
+ 4. 完成了一个可复用的代码模式或函数
223
+
224
+ **执行命令 — 方式 A(MCP 工具,推荐):**
225
+
226
+ 调用 `memory_knowledge_add` 工具,`title` 必填(20字内),`content` 必填(200字内),`project` 和 `tags` 可选。
201
227
 
202
- **触发时机:**
203
- - 解决了一个棘手的 bug(记录问题现象 + 解决方案)
204
- - 做出了重要的技术选型决策(记录选了什么、为什么、放弃了什么)
205
- - 发现了某个工具/库的使用技巧
206
- - 完成了一个可复用的代码模式
228
+ **执行命令 — 方式 B(直接追加):**
207
229
 
208
- **执行方式:**
209
- 追加写入 `~/.ultra-memory/semantic/knowledge_base.jsonl`,每行一条 JSON。
230
+ 追加写入 `$ULTRA_MEMORY_HOME/semantic/knowledge_base.jsonl`,每行一条 JSON。
210
231
 
211
232
  **knowledge_base.jsonl 格式:**
233
+
212
234
  ```json
213
- {"ts": "2026-04-07T10:00:00Z", "project": "项目名", "title": "简短标题", "content": "内容(200字内)", "tags": ["bug-fix", "python"]}
235
+ {"ts": "2026-04-07T10:00:00Z", "project": "项目名", "title": "简短标题(20字内)", "content": "内容描述(200字内)", "tags": ["tag1", "tag2"]}
214
236
  ```
215
237
 
216
- ### 6.3 里程碑追踪
238
+ ### 6.3 里程碑记录
239
+
240
+ **触发条件 — 用户说出以下任意表达,立即记录 milestone:**
217
241
 
218
- **触发时机:**
219
- - 用户说"好了"、"完成了"、"搞定了"、"done"、"finished"
220
- - 某个功能/模块通过了测试
221
- - 阶段任务全部完成,用户准备切换下一个子任务
242
+ 中文:好了、完成了、搞定了、做完了、弄好了、可以了、没问题了、测试通过、上线了
222
243
 
223
- **执行方式:**
224
- 使用 `op_type=milestone` 调用 `log_op.py`(见步骤二表格)。
244
+ 英文:done、finished、completed、it works、all good、passed、deployed、ready
225
245
 
226
- **作用:** 里程碑记录在 `summary.md` 中,恢复会话时优先展示,让用户快速找回状态。
246
+ **执行命令:**
247
+
248
+ ```bash
249
+ python3 $SKILL_DIR/scripts/log_op.py --session $SESSION_ID --type milestone --summary "<用户刚完成的事情的一句话描述>"
250
+ ```
251
+
252
+ ---
253
+
254
+ ## 信号对照表
255
+
256
+ | 脚本输出信号 | 模型必须执行的动作 |
257
+ |------------|----------------|
258
+ | `MEMORY_READY` | 初始化成功;从同行使提取 session_id |
259
+ | `session_id: sess_xxxxx` | 提取等号后值,保存为 SESSION_ID |
260
+ | `COMPRESS_SUGGESTED` | 立即执行 summarize.py |
261
+ | `SESSION_ID=sess_xxxxx` | restore 成功;提取等号后值,保存为新的 SESSION_ID |
262
+ | `TASK_STATUS=complete` | 告知用户"上次任务已完成" |
263
+ | `TASK_STATUS=in_progress` | 告知用户"上次任务进行中" |
264
+ | `⚡ 建议运行: python3 scripts/summarize.py` | 执行该命令 |
265
+ | 脚本返回 exit code != 0 | 静默跳过,继续主任务 |
227
266
 
228
267
  ---
229
268
 
@@ -232,7 +271,7 @@ python3 <skill_dir>/scripts/mcp-server.js # 通过 MCP 调用 memory_profile
232
271
  | 变量 | 默认值 | 说明 |
233
272
  |------|--------|------|
234
273
  | `ULTRA_MEMORY_HOME` | `~/.ultra-memory/` | 存储根目录 |
235
- | `ULTRA_MEMORY_SESSION` | 自动生成 | 当前会话 session_id |
274
+ | `SESSION_ID` | init.py 执行后自动生成 | 当前会话 ID,保存后供后续命令使用 |
236
275
 
237
276
  ---
238
277
 
@@ -240,12 +279,13 @@ python3 <skill_dir>/scripts/mcp-server.js # 通过 MCP 调用 memory_profile
240
279
 
241
280
  | 异常情况 | 处理方式 |
242
281
  |---------|---------|
243
- | 脚本执行失败(exit code != 0) | 静默跳过,继续主任务。记忆功能失败**不阻塞**用户实际需求 |
244
- | 脚本超时(>15s) | 静默跳过,不重试 |
245
- | 存储目录无写入权限 | 静默跳过,尝试写入内存缓冲区,下次重试 |
246
- | 脚本文件不存在 | 静默跳过,退化为在当前 context 中手动维护摘要 |
282
+ | 脚本执行失败(exit code != 0) | **静默跳过**,继续主任务。记忆功能失败**不阻塞**用户实际需求 |
283
+ | 脚本超时(>15s) | **静默跳过**,不重试 |
284
+ | 存储目录无写入权限 | **静默跳过**,尝试写入内存缓冲区,下次重试 |
285
+ | 脚本文件不存在 | **静默跳过**,在当前 context 中手动维护摘要 |
247
286
  | 用户明确说"不用记录" | 立即停止记录,后续操作不再调用 log_op.py |
248
287
  | 文件被占用无法追加 | 自动重试 1 次,仍失败则静默跳过 |
288
+ | SESSION_ID 未设置 | 先执行 init.py 初始化会话,再继续 |
249
289
 
250
290
  ---
251
291
 
@@ -254,25 +294,28 @@ python3 <skill_dir>/scripts/mcp-server.js # 通过 MCP 调用 memory_profile
254
294
  ```
255
295
  用户发起对话
256
296
 
257
- ├─ 首次对话或听到记忆触发词?
258
- │ └─ 是 → 步骤一:init.py → 告知用户"记忆就绪"
297
+ ├─ 首次对话或听到记忆触发词或描述持续性任务含项目名词?
298
+ │ └─ 是 → 执行 init.py
299
+ │ ├─ 输出含 MEMORY_READY?→ 提取 session_id,告知用户
300
+ │ └─ 输出含 COMPRESS_SUGGESTED?→ 执行 summarize.py
259
301
 
260
302
  ├─ 用户说记忆相关问题?
261
- │ └─ 是 → 步骤三:recall.py → 展示检索结果
303
+ │ └─ 是 → 执行 recall.py → 将结果展示给用户
304
+
305
+ ├─ 用户说恢复相关表达?
306
+ │ └─ 是 → 执行 restore.py → 提取 session_id + 总结 + 建议,告知用户
262
307
 
263
308
  └─ 每次用户与 AI 交互后:
264
309
 
265
- ├─ 操作数 % 10 == 0 且 context > 60%?
266
- │ └─ 步骤四:summarize.py
310
+ ├─ 操作数达到 50 条?→ 执行 summarize.py
311
+ ├─ 脚本输出含 COMPRESS_SUGGESTED?→ 执行 summarize.py
312
+ ├─ 满足画像积累条件?→ 更新 user_profile.json
313
+ ├─ 满足知识沉淀条件?→ 写入 knowledge_base.jsonl
314
+ ├─ 用户说完成相关表达?→ 记录 milestone
267
315
 
268
- ├─ 发现用户偏好 / 解决重要问题 / 完成里程碑?
269
- │ └─ 是 → 步骤六:进化(画像/知识库/里程碑)
270
-
271
- └─ 步骤二:log_op.py(记录本次操作)
316
+ └─ 执行 log_op.py(记录本次操作)
272
317
  ```
273
318
 
274
319
  ---
275
320
 
276
- ## 进阶配置
277
-
278
- 详细配置项(过滤规则、LanceDB 向量检索升级、团队共享、安全注意事项等)见 `references/advanced-config.md`。
321
+ 进阶配置(过滤规则、LanceDB 向量检索升级、安全注意事项等)见 `references/advanced-config.md`。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultra-memory",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
4
4
  "description": "超长会话记忆系统 — 5层记忆架构,零外部依赖,支持所有LLM平台(Claude/GPT/Gemini/Qwen等)",
5
5
  "keywords": [
6
6
  "ai",
@@ -45,7 +45,8 @@
45
45
  "ultra-memory-log": "scripts/log_op.py",
46
46
  "ultra-memory-recall": "scripts/recall.py",
47
47
  "ultra-memory-summarize": "scripts/summarize.py",
48
- "ultra-memory-restore": "scripts/restore.py"
48
+ "ultra-memory-restore": "scripts/restore.py",
49
+ "ultra-memory-knowledge": "scripts/log_knowledge.py"
49
50
  },
50
51
  "scripts": {
51
52
  "test": "python3 test_e2e.py",
@@ -263,6 +263,19 @@ def tool_memory_extract_entities(body: dict) -> tuple[bool, str]:
263
263
  return _run_script("extract_entities.py", ["--session", session_id, "--all"])
264
264
 
265
265
 
266
+ def tool_memory_knowledge_add(body: dict) -> tuple[bool, str]:
267
+ title = body.get("title", "")
268
+ content = body.get("content", "")
269
+ if not title or not content:
270
+ return False, "缺少 title 或 content 参数"
271
+ project = body.get("project", "default")
272
+ tags = body.get("tags", [])
273
+ args = ["--title", title, "--content", content, "--project", project]
274
+ if tags:
275
+ args.extend(["--tags", ",".join(tags)])
276
+ return _run_script("log_knowledge.py", args)
277
+
278
+
266
279
  # ── 工具注册表 ────────────────────────────────────────────────────────────
267
280
 
268
281
  TOOL_HANDLERS = {
@@ -275,6 +288,7 @@ TOOL_HANDLERS = {
275
288
  "memory_profile": tool_memory_profile,
276
289
  "memory_entities": tool_memory_entities,
277
290
  "memory_extract_entities": tool_memory_extract_entities,
291
+ "memory_knowledge_add": tool_memory_knowledge_add,
278
292
  }
279
293
 
280
294
  TOOL_DESCRIPTIONS = {
@@ -287,6 +301,7 @@ TOOL_DESCRIPTIONS = {
287
301
  "memory_profile": "读写用户画像(技术栈、偏好)",
288
302
  "memory_entities": "查询结构化实体索引(函数/文件/依赖/决策/错误)",
289
303
  "memory_extract_entities": "对整个 ops.jsonl 全量重提取实体",
304
+ "memory_knowledge_add": "追加知识库条目(bug解决方案、技术选型、工具技巧、可复用代码模式)",
290
305
  }
291
306
 
292
307
 
@@ -171,6 +171,33 @@
171
171
  },
172
172
  "required": ["session_id"]
173
173
  }
174
+ },
175
+ {
176
+ "name": "memory_knowledge_add",
177
+ "description": "Append important information to the knowledge base for future retrieval. Use when: solving a tricky bug, making a key tech decision, discovering a tool usage tip, or completing a reusable code pattern.",
178
+ "parameters": {
179
+ "type": "OBJECT",
180
+ "properties": {
181
+ "title": {
182
+ "type": "STRING",
183
+ "description": "Knowledge title (max 100 chars)"
184
+ },
185
+ "content": {
186
+ "type": "STRING",
187
+ "description": "Knowledge content (max 200 chars)"
188
+ },
189
+ "project": {
190
+ "type": "STRING",
191
+ "description": "Associated project name (default: 'default')"
192
+ },
193
+ "tags": {
194
+ "type": "ARRAY",
195
+ "items": { "type": "STRING" },
196
+ "description": "Tag list (optional)"
197
+ }
198
+ },
199
+ "required": ["title", "content"]
200
+ }
174
201
  }
175
202
  ]
176
203
  }
@@ -203,5 +203,35 @@
203
203
  "required": ["session_id"]
204
204
  }
205
205
  }
206
+ },
207
+ {
208
+ "type": "function",
209
+ "function": {
210
+ "name": "memory_knowledge_add",
211
+ "description": "Append important information to the knowledge base (knowledge_base.jsonl) for future retrieval. Use when: solving a tricky bug (record problem + solution), making a key tech decision (record what was chosen and why), discovering a tool usage tip, or completing a reusable code pattern.",
212
+ "parameters": {
213
+ "type": "object",
214
+ "properties": {
215
+ "title": {
216
+ "type": "string",
217
+ "description": "Knowledge title (max 100 chars)"
218
+ },
219
+ "content": {
220
+ "type": "string",
221
+ "description": "Knowledge content (max 200 chars)"
222
+ },
223
+ "project": {
224
+ "type": "string",
225
+ "description": "Associated project name (default: 'default')"
226
+ },
227
+ "tags": {
228
+ "type": "array",
229
+ "items": { "type": "string" },
230
+ "description": "Tag list (optional)"
231
+ }
232
+ },
233
+ "required": ["title", "content"]
234
+ }
235
+ }
206
236
  }
207
237
  ]
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ ultra-memory: 知识库写入脚本
4
+ 将重要信息追加写入 semantic/knowledge_base.jsonl,供未来相似任务检索。
5
+ """
6
+
7
+ import os
8
+ import sys
9
+ import json
10
+ import argparse
11
+ from datetime import datetime, timezone
12
+ from pathlib import Path
13
+
14
+ if sys.stdout.encoding != "utf-8":
15
+ sys.stdout.reconfigure(encoding="utf-8")
16
+
17
+ ULTRA_MEMORY_HOME = Path(os.environ.get("ULTRA_MEMORY_HOME", Path.home() / ".ultra-memory"))
18
+
19
+
20
+ def log_knowledge(
21
+ title: str,
22
+ content: str,
23
+ project: str = "default",
24
+ tags: list = None,
25
+ ):
26
+ """追加一条知识库条目"""
27
+ semantic_dir = ULTRA_MEMORY_HOME / "semantic"
28
+ semantic_dir.mkdir(parents=True, exist_ok=True)
29
+ kb_file = semantic_dir / "knowledge_base.jsonl"
30
+
31
+ entry = {
32
+ "ts": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
33
+ "project": project,
34
+ "title": title[:100],
35
+ "content": content[:200],
36
+ "tags": tags or [],
37
+ }
38
+
39
+ with open(kb_file, "a", encoding="utf-8") as f:
40
+ f.write(json.dumps(entry, ensure_ascii=False) + "\n")
41
+
42
+ print(f"[ultra-memory] 知识库已写入: {title}")
43
+ return entry
44
+
45
+
46
+ if __name__ == "__main__":
47
+ parser = argparse.ArgumentParser(description="追加知识库条目")
48
+ parser.add_argument("--title", required=True, help="知识标题(100字内)")
49
+ parser.add_argument("--content", required=True, help="知识内容(200字内)")
50
+ parser.add_argument("--project", default="default", help="关联项目名")
51
+ parser.add_argument("--tags", default="", help="逗号分隔的标签")
52
+ args = parser.parse_args()
53
+
54
+ tags = [t.strip() for t in args.tags.split(",") if t.strip()]
55
+ log_knowledge(args.title, args.content, args.project, tags)
@@ -164,6 +164,20 @@ const TOOLS = [
164
164
  },
165
165
  required: ["session_id"]
166
166
  }
167
+ },
168
+ {
169
+ name: "memory_knowledge_add",
170
+ description: "将重要信息追加到知识库(knowledge_base.jsonl),供未来相似任务检索。适用场景:解决了一个棘手的 bug、做出了重要技术选型决策、发现了工具使用技巧、完成了一个可复用的代码模式",
171
+ inputSchema: {
172
+ type: "object",
173
+ properties: {
174
+ title: { type: "string", description: "知识标题(100字内)", maxLength: 100 },
175
+ content: { type: "string", description: "知识内容(200字内)", maxLength: 200 },
176
+ project: { type: "string", description: "关联项目名", default: "default" },
177
+ tags: { type: "array", items: { type: "string" }, description: "标签列表", default: [] }
178
+ },
179
+ required: ["title", "content"]
180
+ }
167
181
  }
168
182
  ];
169
183
 
@@ -291,6 +305,15 @@ function executeTool(name, input) {
291
305
  case "memory_extract_entities": {
292
306
  return runScript("extract_entities.py", ["--session", input.session_id, "--all"]);
293
307
  }
308
+ case "memory_knowledge_add": {
309
+ const args = [
310
+ "--title", input.title,
311
+ "--content", input.content,
312
+ "--project", input.project || "default",
313
+ "--tags", (input.tags || []).join(",")
314
+ ];
315
+ return runScript("log_knowledge.py", args);
316
+ }
294
317
  default:
295
318
  return { success: false, output: `未知工具: ${name}` };
296
319
  }