@easynet/agent-memory 1.0.46 → 1.0.47
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/README.md +21 -201
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,92 +1,20 @@
|
|
|
1
1
|
# @easynet/agent-memory
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
默认入口:`createAgentMemory()`。
|
|
3
|
+
## Introduction
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
`@easynet/agent-memory` adds memory storage and recall for agents. It supports common scopes such as `thread`, `cross_thread`, and `knowledge`, and registers the memory client into the default `AgentContext`.
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
import { createAgentMemory } from "@easynet/agent-memory";
|
|
10
|
-
|
|
11
|
-
const memory = await createAgentMemory();
|
|
12
|
-
|
|
13
|
-
// 写入:memorize
|
|
14
|
-
await memory.memorize({
|
|
15
|
-
namespace: "user:alice",
|
|
16
|
-
type: "cross_thread",
|
|
17
|
-
content: "用户偏好中文回答,尽量简洁。",
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
// 检索:recall
|
|
21
|
-
const { injectedText } = await memory.recall({
|
|
22
|
-
namespace: "user:alice",
|
|
23
|
-
query: "用户偏好",
|
|
24
|
-
topK: 5,
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
console.log(injectedText);
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Memory Type 关键字
|
|
31
|
-
|
|
32
|
-
- Canonical keyword:`thread`、`cross_thread`、`knowledge`
|
|
33
|
-
- 推荐始终写 canonical keyword
|
|
7
|
+
## API Reference
|
|
34
8
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
## 最简单示例(带详细注释)
|
|
39
|
-
|
|
40
|
-
```ts
|
|
41
|
-
import { createAgentMemory } from "@easynet/agent-memory";
|
|
9
|
+
| API | What it does | Minimal usage |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| `createAgentMemory` | Create a memory client and register it into `AgentContext`. | `const memory = await createAgentMemory({ configPath: "./memory.yaml" })` |
|
|
42
12
|
|
|
43
|
-
|
|
44
|
-
// 1) 创建 memory 实例:
|
|
45
|
-
// - 不传参数时,会尝试读取 ./config/memory.yaml
|
|
46
|
-
// - 如果文件不存在,自动退回内存版 in_memory provider
|
|
47
|
-
const memory = await createAgentMemory();
|
|
13
|
+
## Usage
|
|
48
14
|
|
|
49
|
-
|
|
50
|
-
// namespace 是逻辑隔离维度,建议固定命名规范(如 user:{id})
|
|
51
|
-
await memory.memorize({
|
|
52
|
-
namespace: "user:alice",
|
|
53
|
-
type: "cross_thread",
|
|
54
|
-
content: "用户希望所有输出优先中文。",
|
|
55
|
-
metadata: {
|
|
56
|
-
source: "chat",
|
|
57
|
-
tags: ["preference", "language"],
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// 3) 写入知识(knowledge):
|
|
62
|
-
await memory.memorize({
|
|
63
|
-
namespace: "project:demo",
|
|
64
|
-
type: "knowledge",
|
|
65
|
-
content: "Demo API 统一使用 Bearer Token 鉴权。",
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// 4) 检索上下文:
|
|
69
|
-
// recall 会返回可直接拼进 prompt 的 injectedText
|
|
70
|
-
const result = await memory.recall({
|
|
71
|
-
namespace: "project:demo",
|
|
72
|
-
query: "这个项目如何鉴权?",
|
|
73
|
-
topK: 3,
|
|
74
|
-
budgetTokens: 400,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
console.log("traceId:", result.traceId);
|
|
78
|
-
console.log("context:\\n", result.injectedText);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
main().catch(console.error);
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
## 最简单 YAML(推荐先用这个)
|
|
85
|
-
|
|
86
|
-
`config/memory.yaml`
|
|
15
|
+
Create `config/memory.yaml`:
|
|
87
16
|
|
|
88
17
|
```yaml
|
|
89
|
-
# 最小配置:单一 in_memory provider(进程内存,重启后数据丢失)
|
|
90
18
|
providers:
|
|
91
19
|
- id: default
|
|
92
20
|
type: in_memory
|
|
@@ -97,132 +25,24 @@ memoryTypes:
|
|
|
97
25
|
thread: { providerId: default, allowWrite: true, maxItems: 20 }
|
|
98
26
|
cross_thread: { providerId: default, allowWrite: true, maxItems: 20 }
|
|
99
27
|
knowledge: { providerId: default, allowWrite: true, maxItems: 20 }
|
|
100
|
-
|
|
101
|
-
observability:
|
|
102
|
-
# 是否记录 recall trace(建议开发期开,生产按需)
|
|
103
|
-
trace: false
|
|
104
|
-
|
|
105
|
-
privacy:
|
|
106
|
-
# 写入前移除敏感 metadata 字段
|
|
107
|
-
forbiddenMetadataKeys: [password, api_key, secret, token]
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
## SQLite YAML(本地持久化)
|
|
111
|
-
|
|
112
|
-
```yaml
|
|
113
|
-
providers:
|
|
114
|
-
- id: default
|
|
115
|
-
type: sqlite
|
|
116
|
-
options:
|
|
117
|
-
# SQLite 文件路径;相对路径以该 YAML 所在目录为基准
|
|
118
|
-
dbPath: ./data/agent-memory.db
|
|
119
|
-
|
|
120
|
-
defaultProvider: default
|
|
121
|
-
|
|
122
|
-
memoryTypes:
|
|
123
|
-
thread: { providerId: default, allowWrite: true, maxItems: 50 }
|
|
124
|
-
cross_thread: { providerId: default, allowWrite: true, maxItems: 50 }
|
|
125
|
-
knowledge: { providerId: default, allowWrite: true, maxItems: 50 }
|
|
126
|
-
|
|
127
|
-
observability:
|
|
128
|
-
trace: true
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
## 分层配置(按 memory type)
|
|
132
|
-
|
|
133
|
-
当你希望 `thread / cross_thread / knowledge` 各自有独立设置时,推荐使用 `memoryTypes`:
|
|
134
|
-
|
|
135
|
-
```yaml
|
|
136
|
-
providers:
|
|
137
|
-
- id: default
|
|
138
|
-
type: in_memory
|
|
139
|
-
- id: docs
|
|
140
|
-
type: sqlite
|
|
141
|
-
options:
|
|
142
|
-
dbPath: ./data/docs.db
|
|
143
|
-
|
|
144
|
-
defaultProvider: default
|
|
145
|
-
|
|
146
|
-
memoryTypes:
|
|
147
|
-
thread:
|
|
148
|
-
allowWrite: false
|
|
149
|
-
cross_thread:
|
|
150
|
-
maxItems: 20
|
|
151
|
-
budgetTokens: 500
|
|
152
|
-
knowledge:
|
|
153
|
-
providerId: docs
|
|
154
|
-
maxItems: 8
|
|
155
|
-
budgetTokens: 900
|
|
156
28
|
```
|
|
157
29
|
|
|
158
|
-
|
|
159
|
-
- `memoryTypes.*.providerId` 决定该类型路由到哪个 provider
|
|
160
|
-
- `memoryTypes.*.allowWrite` 控制该类型是否允许写入
|
|
161
|
-
- `memoryTypes.*.maxItems / budgetTokens` 控制该类型召回裁剪预算
|
|
162
|
-
|
|
163
|
-
## LlamaIndex / Sqlite-Vec YAML(内置类型)
|
|
30
|
+
Initialize and use memory:
|
|
164
31
|
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
- id: knowledge
|
|
168
|
-
type: llamaindex
|
|
169
|
-
options: {}
|
|
170
|
-
- id: longterm
|
|
171
|
-
type: sqlite_vec
|
|
172
|
-
options:
|
|
173
|
-
dbPath: ./data/long-term.sqlite
|
|
174
|
-
dimensions: 1024
|
|
175
|
-
|
|
176
|
-
defaultProvider: knowledge
|
|
177
|
-
|
|
178
|
-
memoryTypes:
|
|
179
|
-
thread:
|
|
180
|
-
providerId: knowledge
|
|
181
|
-
cross_thread:
|
|
182
|
-
providerId: longterm
|
|
183
|
-
knowledge:
|
|
184
|
-
providerId: knowledge
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
依赖说明:
|
|
188
|
-
- `type: sqlite` 需要安装 `better-sqlite3`
|
|
189
|
-
- `type: sqlite_vec` 需要安装 `better-sqlite3`,并提供 provider ctor(推荐:`overrides.providerCtors.sqliteVec`,或在 `stores[].config` 设置 `package` + `exportName`)
|
|
190
|
-
- `type: llamaindex` 需要安装 `llamaindex`,并提供 provider ctor(推荐:`overrides.providerCtors.llamaindex`,或在 `stores[].config` 设置 `package` + `exportName`)
|
|
191
|
-
- URL 摄入 PDF 需要安装 `pdf-parse`
|
|
192
|
-
- `type: mem0` 需要安装 `mem0ai`
|
|
193
|
-
|
|
194
|
-
## 扩展 Provider(extension)
|
|
195
|
-
|
|
196
|
-
支持从外部 npm 包或本地模块加载自定义 provider。
|
|
197
|
-
也可以直接复用 `@easynet/agent-memory` 已导出的 provider(如 `InMemoryStoreProvider` / `SqliteStoreProvider` / `RAGProvider` / `Mem0Provider`)。
|
|
32
|
+
```ts
|
|
33
|
+
import { createAgentMemory } from "@easynet/agent-memory";
|
|
198
34
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
- id: builtin
|
|
203
|
-
type: extension
|
|
204
|
-
options:
|
|
205
|
-
package: "@easynet/agent-memory"
|
|
206
|
-
exportName: InMemoryStoreProvider
|
|
35
|
+
const memory = await createAgentMemory({
|
|
36
|
+
configPath: "./config/memory.yaml",
|
|
37
|
+
});
|
|
207
38
|
|
|
208
|
-
|
|
209
|
-
- id: ext
|
|
210
|
-
type: extension
|
|
211
|
-
options:
|
|
212
|
-
package: "@your-scope/agent-memory-provider-foo" # 也可用 ./relative/path/to/provider.mjs
|
|
213
|
-
exportName: createMemoryProvider # 可选,默认 createMemoryProvider
|
|
214
|
-
options:
|
|
215
|
-
anyKey: anyValue
|
|
39
|
+
await memory.memorize("user:alice", "cross_thread", "Prefers concise English answers.");
|
|
216
40
|
|
|
217
|
-
|
|
41
|
+
const result = await memory.recall({
|
|
42
|
+
namespace: "user:alice",
|
|
43
|
+
query: "language preference",
|
|
44
|
+
topK: 5,
|
|
45
|
+
});
|
|
218
46
|
|
|
219
|
-
|
|
220
|
-
thread: { providerId: ext }
|
|
221
|
-
cross_thread: { providerId: ext }
|
|
222
|
-
knowledge: { providerId: ext }
|
|
47
|
+
console.log(result.injectedText);
|
|
223
48
|
```
|
|
224
|
-
|
|
225
|
-
扩展导出约定:
|
|
226
|
-
- 函数导出:`createMemoryProvider({ providerId, options }) => MemoryProvider`
|
|
227
|
-
- 或默认导出 class,构造参数同上
|
|
228
|
-
- 返回对象需实现 `write/query/delete`
|