@smyslenny/agent-memory 4.0.0-alpha.1 → 4.2.0
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/CHANGELOG.md +38 -0
- package/dist/bin/agent-memory.js +1171 -886
- package/dist/bin/agent-memory.js.map +1 -1
- package/dist/index.d.ts +52 -7
- package/dist/index.js +159 -16
- package/dist/index.js.map +1 -1
- package/dist/mcp/server.js +175 -29
- package/dist/mcp/server.js.map +1 -1
- package/docs/design/.next-id +1 -0
- package/docs/design/0016-v41-warm-boot-surface-emotion.md +228 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
17
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# DD-0016: AgentMemory v4.1 — Warm Boot + Surface CLI + Emotion Tags
|
|
2
|
+
|
|
3
|
+
**Status:** Approved
|
|
4
|
+
**Author:** Noah
|
|
5
|
+
**Date:** 2026-03-10
|
|
6
|
+
**Repo:** /tmp/agent-memory-dd
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 1. Background / 背景
|
|
11
|
+
|
|
12
|
+
v4.0 完成了核心重构(向量检索、语义去重、HTTP API、文档泛化),但缺少三个常被请求的能力:
|
|
13
|
+
|
|
14
|
+
1. `boot` 只返回 JSON 数组——agent 醒来没有"记忆的温度"。
|
|
15
|
+
2. 没有 CLI 命令直接输出 Markdown 用于 `RECENT.md` 自动注入。
|
|
16
|
+
3. emotion 类型记忆只有 `emotion_val` 数值,没有可检索的情感标签。
|
|
17
|
+
|
|
18
|
+
这三个功能共享一些底层改动(列记忆时的分层过滤、schema 升级、MCP/CLI 参数扩展),适合合在一个 DD 里做。
|
|
19
|
+
|
|
20
|
+
## 2. Goals / 目标
|
|
21
|
+
|
|
22
|
+
- G1: `boot` 支持 `format=narrative`,输出分层叙事 Markdown(纯模板,不依赖 LLM)
|
|
23
|
+
- G2: 新增 `agent-memory surface` CLI 命令,输出 Markdown 并支持 `--out FILE`
|
|
24
|
+
- G3: memories 表新增 `emotion_tag TEXT` 列(schema v6),贯通 remember/recall/surface/MCP/CLI
|
|
25
|
+
|
|
26
|
+
## 3. Non-Goals / 非目标
|
|
27
|
+
|
|
28
|
+
- 不做 LLM 驱动的叙事生成
|
|
29
|
+
- 不做自动关联(links 自动建边留 v4.2)
|
|
30
|
+
- 不改 surface 的评分公式
|
|
31
|
+
- 不做统一搜索入口(qmd + recall 合并留后续)
|
|
32
|
+
|
|
33
|
+
## 4. Proposal / 方案
|
|
34
|
+
|
|
35
|
+
### 4.1 Feature 1: Warm Boot
|
|
36
|
+
|
|
37
|
+
**改动文件:** `src/sleep/boot.ts`, `src/mcp/server.ts`, `src/bin/agent-memory.ts`
|
|
38
|
+
|
|
39
|
+
#### 接口变更
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
export interface WarmBootOptions {
|
|
43
|
+
agent_id?: string;
|
|
44
|
+
corePaths?: string[];
|
|
45
|
+
format?: "json" | "narrative"; // 新增,默认 "json"
|
|
46
|
+
agent_name?: string; // narrative 模板用,默认 "Agent"
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface WarmBootResult extends BootResult {
|
|
50
|
+
narrative?: string; // format=narrative 时填充
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#### 分层拉取策略
|
|
55
|
+
|
|
56
|
+
| 层 | 类型 | 拉取策略 | Markdown 标题 |
|
|
57
|
+
|----|------|----------|-------------|
|
|
58
|
+
| 核心身份 | identity (P0) | 全部 | `## 我是谁` |
|
|
59
|
+
| 近期情感 | emotion (P1) | 最近 5 条 by updated_at | `## 最近的心情` |
|
|
60
|
+
| 近期事件 | event (P3) | 最近 7 条 by updated_at | `## 最近发生的事` |
|
|
61
|
+
| 鲜活知识 | knowledge (P2) | vitality > 0.5, top 10 | `## 还记得的知识` |
|
|
62
|
+
|
|
63
|
+
#### 时间格式化
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
function relativeTime(isoDate: string): string {
|
|
67
|
+
const diffMs = Date.now() - new Date(isoDate).getTime();
|
|
68
|
+
const diffDays = Math.floor(diffMs / 86_400_000);
|
|
69
|
+
if (diffDays === 0) return "今天";
|
|
70
|
+
if (diffDays === 1) return "昨天";
|
|
71
|
+
if (diffDays <= 7) return `${diffDays}天前`;
|
|
72
|
+
return isoDate.slice(0, 10); // YYYY-MM-DD
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### Narrative 模板
|
|
77
|
+
|
|
78
|
+
```markdown
|
|
79
|
+
# {agent_name}的回忆
|
|
80
|
+
|
|
81
|
+
## 我是谁
|
|
82
|
+
- {identity_content_1}
|
|
83
|
+
- {identity_content_2}
|
|
84
|
+
|
|
85
|
+
## 最近的心情
|
|
86
|
+
- {emotion_content} ({emotion_tag}, {relative_time})
|
|
87
|
+
|
|
88
|
+
## 最近发生的事
|
|
89
|
+
- {event_content} ({relative_time})
|
|
90
|
+
|
|
91
|
+
## 还记得的知识
|
|
92
|
+
- {knowledge_content}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
emotion 条目如果有 `emotion_tag` 则显示 `(标签, 时间)`,否则只显示 `(时间)`。
|
|
96
|
+
|
|
97
|
+
#### MCP / CLI
|
|
98
|
+
|
|
99
|
+
- MCP `boot` tool: 新增 `format` 参数(枚举 `json|narrative`)和 `agent_name` 参数
|
|
100
|
+
- CLI: `agent-memory boot --format narrative --agent-name Noah`
|
|
101
|
+
|
|
102
|
+
### 4.2 Feature 2: Surface CLI Command
|
|
103
|
+
|
|
104
|
+
**改动文件:** `src/bin/agent-memory.ts`(主要),复用 `src/app/surface.ts` 里已有的 `surfaceMemories()`
|
|
105
|
+
|
|
106
|
+
#### CLI 接口
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
agent-memory surface [--out FILE] [--days N] [--limit N] [--min-vitality F] [--types T1,T2]
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
| 参数 | 默认值 | 说明 |
|
|
113
|
+
|------|--------|------|
|
|
114
|
+
| `--out` | stdout | 输出到文件路径 |
|
|
115
|
+
| `--days` | 7 | 时间窗口(天)|
|
|
116
|
+
| `--limit` | 50 | 最大条目数 |
|
|
117
|
+
| `--min-vitality` | 0.1 | vitality 下限 |
|
|
118
|
+
| `--types` | 全部 | 逗号分隔的类型过滤 |
|
|
119
|
+
|
|
120
|
+
#### 输出 Markdown 格式
|
|
121
|
+
|
|
122
|
+
```markdown
|
|
123
|
+
# Recent Memories
|
|
124
|
+
|
|
125
|
+
> Auto-generated by AgentMemory surface. Last updated: {ISO_DATE}
|
|
126
|
+
|
|
127
|
+
## Identity
|
|
128
|
+
- {content} (vitality: {v})
|
|
129
|
+
|
|
130
|
+
## Emotion
|
|
131
|
+
- {content} ({emotion_tag}, {relative_time})
|
|
132
|
+
|
|
133
|
+
## Knowledge
|
|
134
|
+
- {content} ({relative_time}, vitality: {v})
|
|
135
|
+
|
|
136
|
+
## Events
|
|
137
|
+
- {content} ({relative_time})
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
按 type 分组,每组内按 score 降序。需要先构造一个 surface 调用——使用 `task: "context loading"` + `intent: "temporal"` 作为默认参数。
|
|
141
|
+
|
|
142
|
+
**对于 `--days` 过滤:** surface 本身不支持时间窗口,在 surface 返回后按 `memory.updated_at` 过滤。
|
|
143
|
+
|
|
144
|
+
#### MCP tool
|
|
145
|
+
|
|
146
|
+
MCP 也新增 `surface` tool(当前只有 HTTP endpoint):
|
|
147
|
+
- 参数:`days`, `limit`, `min_vitality`, `types`, `format` (`json|markdown`)
|
|
148
|
+
- `json` 返回现有 SurfaceResponse
|
|
149
|
+
- `markdown` 返回格式化的 Markdown 文本
|
|
150
|
+
|
|
151
|
+
### 4.3 Feature 3: Emotion Tags
|
|
152
|
+
|
|
153
|
+
**改动文件:** `src/core/db.ts` (schema), `src/core/memory.ts` (CRUD), `src/app/remember.ts`, `src/app/recall.ts`, `src/app/surface.ts`, `src/mcp/server.ts`, `src/bin/agent-memory.ts`, `src/transports/http.ts`
|
|
154
|
+
|
|
155
|
+
#### Schema v6 迁移
|
|
156
|
+
|
|
157
|
+
```sql
|
|
158
|
+
ALTER TABLE memories ADD COLUMN emotion_tag TEXT;
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
- 向后兼容,不破坏已有数据
|
|
162
|
+
- `SCHEMA_VERSION` 从 5 → 6
|
|
163
|
+
- 迁移逻辑加在 `initDatabase()` 的 migration 链里
|
|
164
|
+
|
|
165
|
+
#### Memory 接口扩展
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
export interface Memory {
|
|
169
|
+
// ... existing fields ...
|
|
170
|
+
emotion_tag: string | null; // 新增
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export interface CreateMemoryInput {
|
|
174
|
+
// ... existing fields ...
|
|
175
|
+
emotion_tag?: string; // 新增
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### 过滤支持
|
|
180
|
+
|
|
181
|
+
- `listMemories()`: 新增可选 `emotion_tag` 过滤
|
|
182
|
+
- `searchBM25()`: 不变(emotion_tag 不进 FTS)
|
|
183
|
+
- `surfaceMemories()`: SurfaceInput 新增可选 `emotion_tag` 过滤
|
|
184
|
+
- `recall`: RecallInput 新增可选 `emotion_tag` 过滤
|
|
185
|
+
|
|
186
|
+
#### MCP / CLI / HTTP
|
|
187
|
+
|
|
188
|
+
- MCP `remember`: 新增 `emotion_tag` 参数
|
|
189
|
+
- MCP `recall`: 新增 `emotion_tag` 过滤参数
|
|
190
|
+
- CLI `remember`: 新增 `--emotion-tag` 选项
|
|
191
|
+
- CLI `recall`: 新增 `--emotion-tag` 过滤选项
|
|
192
|
+
- HTTP POST `/v1/memories`: body 接受 `emotion_tag`
|
|
193
|
+
- HTTP POST `/v1/recall`: body 接受 `emotion_tag` 过滤
|
|
194
|
+
- HTTP POST `/v1/surface`: body 接受 `emotion_tag` 过滤
|
|
195
|
+
|
|
196
|
+
## 5. Risks / 风险
|
|
197
|
+
|
|
198
|
+
| 风险 | 影响 | 缓解措施 |
|
|
199
|
+
|------|------|----------|
|
|
200
|
+
| Schema 迁移失败 | DB 无法打开 | ALTER TABLE ADD COLUMN 是 SQLite 最安全的迁移之一,只追加不修改 |
|
|
201
|
+
| narrative 模板过长 | 上下文爆炸 | identity 全取但 content 截断到 200 字;其他层有 limit |
|
|
202
|
+
| surface CLI 慢(大 DB) | 超时 | 利用已有 BM25/vitality 索引,结果限制到 50 条 |
|
|
203
|
+
|
|
204
|
+
## 6. Test Plan / 测试方案
|
|
205
|
+
|
|
206
|
+
- [ ] `tests/sleep/boot.test.ts`: narrative format 输出、分层拉取正确性、时间格式化、空 DB 边界
|
|
207
|
+
- [ ] `tests/app/surface-cli.test.ts` 或合并到已有 surface 测试: surface markdown 输出格式、--days 过滤、--out 文件写入
|
|
208
|
+
- [ ] `tests/core/db.test.ts`: schema v5→v6 迁移、emotion_tag 写入/读取
|
|
209
|
+
- [ ] `tests/core/memory.test.ts`: CreateMemoryInput 带 emotion_tag、listMemories 按 emotion_tag 过滤
|
|
210
|
+
- [ ] `tests/app/recall.test.ts`: recall 按 emotion_tag 过滤
|
|
211
|
+
- [ ] `tests/app/surface.test.ts`: surface 按 emotion_tag 过滤
|
|
212
|
+
- [ ] 手动验证: `agent-memory boot --format narrative` + `agent-memory surface --out /tmp/test.md`
|
|
213
|
+
|
|
214
|
+
## 7. Rollback Plan / 回滚方案
|
|
215
|
+
|
|
216
|
+
- Schema v6 只追加列,不删不改,回滚到 v5 代码时 `emotion_tag` 列被忽略
|
|
217
|
+
- narrative boot 是新参数,默认 format=json 不影响已有行为
|
|
218
|
+
- surface CLI 是新命令,不影响已有命令
|
|
219
|
+
|
|
220
|
+
## 8. Decision Log / 决策变更记录
|
|
221
|
+
|
|
222
|
+
| 日期 | 变更 | 原因 |
|
|
223
|
+
|------|------|------|
|
|
224
|
+
| | | |
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
_Generated by DD workflow · Noah_
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smyslenny/agent-memory",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Agent-native memory layer with lifecycle management for AI agents — SQLite-first, Write Guard, hybrid recall, MCP, CLI, and HTTP API.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|