@mastra/memory 1.0.0-beta.10 → 1.0.0-beta.11
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 +87 -0
- package/dist/docs/README.md +36 -0
- package/dist/docs/SKILL.md +42 -0
- package/dist/docs/SOURCE_MAP.json +31 -0
- package/dist/docs/agents/01-agent-memory.md +160 -0
- package/dist/docs/agents/02-networks.md +236 -0
- package/dist/docs/agents/03-agent-approval.md +317 -0
- package/dist/docs/core/01-reference.md +114 -0
- package/dist/docs/memory/01-overview.md +76 -0
- package/dist/docs/memory/02-storage.md +181 -0
- package/dist/docs/memory/03-working-memory.md +386 -0
- package/dist/docs/memory/04-semantic-recall.md +235 -0
- package/dist/docs/memory/05-memory-processors.md +319 -0
- package/dist/docs/memory/06-reference.md +617 -0
- package/dist/docs/processors/01-reference.md +81 -0
- package/dist/docs/storage/01-reference.md +972 -0
- package/dist/docs/vectors/01-reference.md +929 -0
- package/dist/index.cjs +247 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +134 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +247 -14
- package/dist/index.js.map +1 -1
- package/dist/tools/working-memory.d.ts +1 -1
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +11 -10
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
> Learn how to use memory processors in Mastra to filter, trim, and transform messages before they
|
|
2
|
+
|
|
3
|
+
# Memory Processors
|
|
4
|
+
|
|
5
|
+
Memory processors transform and filter messages as they pass through an agent with memory enabled. They manage context window limits, remove unnecessary content, and optimize the information sent to the language model.
|
|
6
|
+
|
|
7
|
+
When memory is enabled on an agent, Mastra adds memory processors to the agent's processor pipeline. These processors retrieve message history, working memory, and semantically relevant messages, then persist new messages after the model responds.
|
|
8
|
+
|
|
9
|
+
Memory processors are [processors](https://mastra.ai/docs/v1/agents/processors) that operate specifically on memory-related messages and state.
|
|
10
|
+
|
|
11
|
+
## Built-in Memory Processors
|
|
12
|
+
|
|
13
|
+
Mastra automatically adds these processors when memory is enabled:
|
|
14
|
+
|
|
15
|
+
### MessageHistory
|
|
16
|
+
|
|
17
|
+
Retrieves message history and persists new messages.
|
|
18
|
+
|
|
19
|
+
**When you configure:**
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
memory: new Memory({
|
|
23
|
+
lastMessages: 10,
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Mastra internally:**
|
|
28
|
+
|
|
29
|
+
1. Creates a `MessageHistory` processor with `limit: 10`
|
|
30
|
+
2. Adds it to the agent's input processors (runs before the LLM)
|
|
31
|
+
3. Adds it to the agent's output processors (runs after the LLM)
|
|
32
|
+
|
|
33
|
+
**What it does:**
|
|
34
|
+
|
|
35
|
+
- **Input**: Fetches the last 10 messages from storage and prepends them to the conversation
|
|
36
|
+
- **Output**: Persists new messages to storage after the model responds
|
|
37
|
+
|
|
38
|
+
**Example:**
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { Agent } from "@mastra/core/agent";
|
|
42
|
+
import { Memory } from "@mastra/memory";
|
|
43
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
44
|
+
import { openai } from "@ai-sdk/openai";
|
|
45
|
+
|
|
46
|
+
const agent = new Agent({
|
|
47
|
+
id: "test-agent",
|
|
48
|
+
name: "Test Agent",
|
|
49
|
+
instructions: "You are a helpful assistant",
|
|
50
|
+
model: 'openai/gpt-4o',
|
|
51
|
+
memory: new Memory({
|
|
52
|
+
storage: new LibSQLStore({
|
|
53
|
+
id: "memory-store",
|
|
54
|
+
url: "file:memory.db",
|
|
55
|
+
}),
|
|
56
|
+
lastMessages: 10, // MessageHistory processor automatically added
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### SemanticRecall
|
|
62
|
+
|
|
63
|
+
Retrieves semantically relevant messages based on the current input and creates embeddings for new messages.
|
|
64
|
+
|
|
65
|
+
**When you configure:**
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
memory: new Memory({
|
|
69
|
+
semanticRecall: { enabled: true },
|
|
70
|
+
vector: myVectorStore,
|
|
71
|
+
embedder: myEmbedder,
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**Mastra internally:**
|
|
76
|
+
|
|
77
|
+
1. Creates a `SemanticRecall` processor
|
|
78
|
+
2. Adds it to the agent's input processors (runs before the LLM)
|
|
79
|
+
3. Adds it to the agent's output processors (runs after the LLM)
|
|
80
|
+
4. Requires both a vector store and embedder to be configured
|
|
81
|
+
|
|
82
|
+
**What it does:**
|
|
83
|
+
|
|
84
|
+
- **Input**: Performs vector similarity search to find relevant past messages and prepends them to the conversation
|
|
85
|
+
- **Output**: Creates embeddings for new messages and stores them in the vector store for future retrieval
|
|
86
|
+
|
|
87
|
+
**Example:**
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { Agent } from "@mastra/core/agent";
|
|
91
|
+
import { Memory } from "@mastra/memory";
|
|
92
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
93
|
+
import { PineconeVector } from "@mastra/pinecone";
|
|
94
|
+
import { OpenAIEmbedder } from "@mastra/openai";
|
|
95
|
+
import { openai } from "@ai-sdk/openai";
|
|
96
|
+
|
|
97
|
+
const agent = new Agent({
|
|
98
|
+
name: "semantic-agent",
|
|
99
|
+
instructions: "You are a helpful assistant with semantic memory",
|
|
100
|
+
model: 'openai/gpt-4o',
|
|
101
|
+
memory: new Memory({
|
|
102
|
+
storage: new LibSQLStore({
|
|
103
|
+
id: "memory-store",
|
|
104
|
+
url: "file:memory.db",
|
|
105
|
+
}),
|
|
106
|
+
vector: new PineconeVector({
|
|
107
|
+
id: "memory-vector",
|
|
108
|
+
apiKey: process.env.PINECONE_API_KEY!,
|
|
109
|
+
environment: "us-east-1",
|
|
110
|
+
}),
|
|
111
|
+
embedder: new OpenAIEmbedder({
|
|
112
|
+
model: "text-embedding-3-small",
|
|
113
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
114
|
+
}),
|
|
115
|
+
semanticRecall: { enabled: true }, // SemanticRecall processor automatically added
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### WorkingMemory
|
|
121
|
+
|
|
122
|
+
Manages working memory state across conversations.
|
|
123
|
+
|
|
124
|
+
**When you configure:**
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
memory: new Memory({
|
|
128
|
+
workingMemory: { enabled: true },
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**Mastra internally:**
|
|
133
|
+
|
|
134
|
+
1. Creates a `WorkingMemory` processor
|
|
135
|
+
2. Adds it to the agent's input processors (runs before the LLM)
|
|
136
|
+
3. Requires a storage adapter to be configured
|
|
137
|
+
|
|
138
|
+
**What it does:**
|
|
139
|
+
|
|
140
|
+
- **Input**: Retrieves working memory state for the current thread and prepends it to the conversation
|
|
141
|
+
- **Output**: No output processing
|
|
142
|
+
|
|
143
|
+
**Example:**
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
import { Agent } from "@mastra/core/agent";
|
|
147
|
+
import { Memory } from "@mastra/memory";
|
|
148
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
149
|
+
import { openai } from "@ai-sdk/openai";
|
|
150
|
+
|
|
151
|
+
const agent = new Agent({
|
|
152
|
+
name: "working-memory-agent",
|
|
153
|
+
instructions: "You are an assistant with working memory",
|
|
154
|
+
model: 'openai/gpt-4o',
|
|
155
|
+
memory: new Memory({
|
|
156
|
+
storage: new LibSQLStore({
|
|
157
|
+
id: "memory-store",
|
|
158
|
+
url: "file:memory.db",
|
|
159
|
+
}),
|
|
160
|
+
workingMemory: { enabled: true }, // WorkingMemory processor automatically added
|
|
161
|
+
}),
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Manual Control and Deduplication
|
|
166
|
+
|
|
167
|
+
If you manually add a memory processor to `inputProcessors` or `outputProcessors`, Mastra will **not** automatically add it. This gives you full control over processor ordering:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { Agent } from "@mastra/core/agent";
|
|
171
|
+
import { Memory } from "@mastra/memory";
|
|
172
|
+
import { MessageHistory } from "@mastra/memory/processors";
|
|
173
|
+
import { TokenLimiter } from "@mastra/core/processors";
|
|
174
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
175
|
+
import { openai } from "@ai-sdk/openai";
|
|
176
|
+
|
|
177
|
+
// Custom MessageHistory with different configuration
|
|
178
|
+
const customMessageHistory = new MessageHistory({
|
|
179
|
+
storage: new LibSQLStore({ id: "memory-store", url: "file:memory.db" }),
|
|
180
|
+
lastMessages: 20,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const agent = new Agent({
|
|
184
|
+
name: "custom-memory-agent",
|
|
185
|
+
instructions: "You are a helpful assistant",
|
|
186
|
+
model: 'openai/gpt-4o',
|
|
187
|
+
memory: new Memory({
|
|
188
|
+
storage: new LibSQLStore({ id: "memory-store", url: "file:memory.db" }),
|
|
189
|
+
lastMessages: 10, // This would normally add MessageHistory(10)
|
|
190
|
+
}),
|
|
191
|
+
inputProcessors: [
|
|
192
|
+
customMessageHistory, // Your custom one is used instead
|
|
193
|
+
new TokenLimiter({ limit: 4000 }), // Runs after your custom MessageHistory
|
|
194
|
+
],
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Processor Execution Order
|
|
199
|
+
|
|
200
|
+
Understanding the execution order is important when combining guardrails with memory:
|
|
201
|
+
|
|
202
|
+
### Input Processors
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
[Memory Processors] → [Your inputProcessors]
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
1. **Memory processors run FIRST**: `WorkingMemory`, `MessageHistory`, `SemanticRecall`
|
|
209
|
+
2. **Your input processors run AFTER**: guardrails, filters, validators
|
|
210
|
+
|
|
211
|
+
This means memory loads message history before your processors can validate or filter the input.
|
|
212
|
+
|
|
213
|
+
### Output Processors
|
|
214
|
+
|
|
215
|
+
```
|
|
216
|
+
[Your outputProcessors] → [Memory Processors]
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
1. **Your output processors run FIRST**: guardrails, filters, validators
|
|
220
|
+
2. **Memory processors run AFTER**: `SemanticRecall` (embeddings), `MessageHistory` (persistence)
|
|
221
|
+
|
|
222
|
+
This ordering is designed to be **safe by default**: if your output guardrail calls `abort()`, the memory processors never run and **no messages are saved**.
|
|
223
|
+
|
|
224
|
+
## Guardrails and Memory
|
|
225
|
+
|
|
226
|
+
The default execution order provides safe guardrail behavior:
|
|
227
|
+
|
|
228
|
+
### Output guardrails (recommended)
|
|
229
|
+
|
|
230
|
+
Output guardrails run **before** memory processors save messages. If a guardrail aborts:
|
|
231
|
+
|
|
232
|
+
- The tripwire is triggered
|
|
233
|
+
- Memory processors are skipped
|
|
234
|
+
- **No messages are persisted to storage**
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
import { Agent } from "@mastra/core/agent";
|
|
238
|
+
import { Memory } from "@mastra/memory";
|
|
239
|
+
import { openai } from "@ai-sdk/openai";
|
|
240
|
+
|
|
241
|
+
// Output guardrail that blocks inappropriate content
|
|
242
|
+
const contentBlocker = {
|
|
243
|
+
id: "content-blocker",
|
|
244
|
+
processOutputResult: async ({ messages, abort }) => {
|
|
245
|
+
const hasInappropriateContent = messages.some((msg) =>
|
|
246
|
+
containsBadContent(msg)
|
|
247
|
+
);
|
|
248
|
+
if (hasInappropriateContent) {
|
|
249
|
+
abort("Content blocked by guardrail");
|
|
250
|
+
}
|
|
251
|
+
return messages;
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const agent = new Agent({
|
|
256
|
+
name: "safe-agent",
|
|
257
|
+
instructions: "You are a helpful assistant",
|
|
258
|
+
model: 'openai/gpt-4o',
|
|
259
|
+
memory: new Memory({ lastMessages: 10 }),
|
|
260
|
+
// Your guardrail runs BEFORE memory saves
|
|
261
|
+
outputProcessors: [contentBlocker],
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// If the guardrail aborts, nothing is saved to memory
|
|
265
|
+
const result = await agent.generate("Hello");
|
|
266
|
+
if (result.tripwire) {
|
|
267
|
+
console.log("Blocked:", result.tripwireReason);
|
|
268
|
+
// Memory is empty - no messages were persisted
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### Input guardrails
|
|
273
|
+
|
|
274
|
+
Input guardrails run **after** memory processors load history. If a guardrail aborts:
|
|
275
|
+
|
|
276
|
+
- The tripwire is triggered
|
|
277
|
+
- The LLM is never called
|
|
278
|
+
- Output processors (including memory persistence) are skipped
|
|
279
|
+
- **No messages are persisted to storage**
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
// Input guardrail that validates user input
|
|
283
|
+
const inputValidator = {
|
|
284
|
+
id: "input-validator",
|
|
285
|
+
processInput: async ({ messages, abort }) => {
|
|
286
|
+
const lastUserMessage = messages.findLast((m) => m.role === "user");
|
|
287
|
+
if (isInvalidInput(lastUserMessage)) {
|
|
288
|
+
abort("Invalid input detected");
|
|
289
|
+
}
|
|
290
|
+
return messages;
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const agent = new Agent({
|
|
295
|
+
name: "validated-agent",
|
|
296
|
+
instructions: "You are a helpful assistant",
|
|
297
|
+
model: 'openai/gpt-4o',
|
|
298
|
+
memory: new Memory({ lastMessages: 10 }),
|
|
299
|
+
// Your guardrail runs AFTER memory loads history
|
|
300
|
+
inputProcessors: [inputValidator],
|
|
301
|
+
});
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Summary
|
|
305
|
+
|
|
306
|
+
| Guardrail Type | When it runs | If it aborts |
|
|
307
|
+
| -------------- | ------------ | ------------ |
|
|
308
|
+
| Input | After memory loads history | LLM not called, nothing saved |
|
|
309
|
+
| Output | Before memory saves | Nothing saved to storage |
|
|
310
|
+
|
|
311
|
+
Both scenarios are safe - guardrails prevent inappropriate content from being persisted to memory
|
|
312
|
+
|
|
313
|
+
## Related documentation
|
|
314
|
+
|
|
315
|
+
- [Processors](https://mastra.ai/docs/v1/agents/processors) - General processor concepts and custom processor creation
|
|
316
|
+
- [Guardrails](https://mastra.ai/docs/v1/agents/guardrails) - Security and validation processors
|
|
317
|
+
- [Memory Overview](https://mastra.ai/docs/v1/memory/overview) - Memory types and configuration
|
|
318
|
+
|
|
319
|
+
When creating custom processors avoid mutating the input `messages` array or its objects directly.
|