@mastra/libsql 0.0.0-workflow-timeout-issue-20260220185702 → 0.0.0-workflow-graph-crash-20260527191009
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 +1085 -3
- package/LICENSE.md +15 -0
- package/dist/docs/SKILL.md +18 -19
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-agent-builder-deploying.md +121 -0
- package/dist/docs/references/docs-agent-builder-overview.md +97 -0
- package/dist/docs/references/docs-agents-agent-approval.md +336 -202
- package/dist/docs/references/docs-agents-networks.md +107 -215
- package/dist/docs/references/docs-memory-memory-processors.md +86 -88
- package/dist/docs/references/docs-memory-message-history.md +71 -63
- package/dist/docs/references/docs-memory-multi-user-threads.md +206 -0
- package/dist/docs/references/docs-memory-overview.md +221 -24
- package/dist/docs/references/docs-memory-semantic-recall.md +162 -75
- package/dist/docs/references/docs-memory-storage.md +72 -73
- package/dist/docs/references/docs-memory-working-memory.md +62 -62
- package/dist/docs/references/docs-rag-retrieval.md +132 -135
- package/dist/docs/references/docs-workflows-snapshots.md +31 -31
- package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +36 -36
- package/dist/docs/references/reference-core-getMemory.md +15 -16
- package/dist/docs/references/reference-core-listMemory.md +17 -18
- package/dist/docs/references/reference-core-mastra-class.md +37 -27
- package/dist/docs/references/reference-memory-memory-class.md +41 -43
- package/dist/docs/references/reference-storage-composite.md +65 -55
- package/dist/docs/references/reference-storage-dynamodb.md +36 -36
- package/dist/docs/references/reference-storage-libsql.md +27 -27
- package/dist/docs/references/reference-vectors-libsql.md +72 -72
- package/dist/index.cjs +7896 -3257
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +7888 -3259
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +14 -1
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/db/utils.d.ts +16 -0
- package/dist/storage/db/utils.d.ts.map +1 -1
- package/dist/storage/domains/agents/index.d.ts.map +1 -1
- package/dist/storage/domains/background-tasks/index.d.ts +18 -0
- package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/storage/domains/blobs/index.d.ts +17 -0
- package/dist/storage/domains/blobs/index.d.ts.map +1 -0
- package/dist/storage/domains/channels/index.d.ts +20 -0
- package/dist/storage/domains/channels/index.d.ts.map +1 -0
- package/dist/storage/domains/datasets/index.d.ts +43 -0
- package/dist/storage/domains/datasets/index.d.ts.map +1 -0
- package/dist/storage/domains/experiments/index.d.ts +31 -0
- package/dist/storage/domains/experiments/index.d.ts.map +1 -0
- package/dist/storage/domains/favorites/index.d.ts +17 -0
- package/dist/storage/domains/favorites/index.d.ts.map +1 -0
- package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -1
- package/dist/storage/domains/mcp-servers/index.d.ts +26 -0
- package/dist/storage/domains/mcp-servers/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +7 -3
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +2 -1
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -1
- package/dist/storage/domains/schedules/index.d.ts +19 -0
- package/dist/storage/domains/schedules/index.d.ts.map +1 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -1
- package/dist/storage/domains/skills/index.d.ts +26 -0
- package/dist/storage/domains/skills/index.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +1 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/domains/workspaces/index.d.ts +26 -0
- package/dist/storage/domains/workspaces/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +38 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +14 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +12 -12
- package/dist/docs/references/docs-agents-agent-memory.md +0 -212
- package/dist/docs/references/docs-agents-network-approval.md +0 -275
- package/dist/docs/references/docs-observability-overview.md +0 -70
- package/dist/docs/references/docs-observability-tracing-exporters-default.md +0 -211
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Memory
|
|
1
|
+
# Memory processors
|
|
2
2
|
|
|
3
3
|
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.
|
|
4
4
|
|
|
@@ -6,11 +6,11 @@ When memory is enabled on an agent, Mastra adds memory processors to the agent's
|
|
|
6
6
|
|
|
7
7
|
Memory processors are [processors](https://mastra.ai/docs/agents/processors) that operate specifically on memory-related messages and state.
|
|
8
8
|
|
|
9
|
-
## Built-in
|
|
9
|
+
## Built-in memory processors
|
|
10
10
|
|
|
11
11
|
Mastra automatically adds these processors when memory is enabled:
|
|
12
12
|
|
|
13
|
-
### MessageHistory
|
|
13
|
+
### `MessageHistory`
|
|
14
14
|
|
|
15
15
|
Retrieves message history and persists new messages.
|
|
16
16
|
|
|
@@ -19,7 +19,7 @@ Retrieves message history and persists new messages.
|
|
|
19
19
|
```typescript
|
|
20
20
|
memory: new Memory({
|
|
21
21
|
lastMessages: 10,
|
|
22
|
-
})
|
|
22
|
+
})
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
**Mastra internally:**
|
|
@@ -36,27 +36,27 @@ memory: new Memory({
|
|
|
36
36
|
**Example:**
|
|
37
37
|
|
|
38
38
|
```typescript
|
|
39
|
-
import { Agent } from
|
|
40
|
-
import { Memory } from
|
|
41
|
-
import { LibSQLStore } from
|
|
42
|
-
import { openai } from
|
|
39
|
+
import { Agent } from '@mastra/core/agent'
|
|
40
|
+
import { Memory } from '@mastra/memory'
|
|
41
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
42
|
+
import { openai } from '@ai-sdk/openai'
|
|
43
43
|
|
|
44
44
|
const agent = new Agent({
|
|
45
|
-
id:
|
|
46
|
-
name:
|
|
47
|
-
instructions:
|
|
48
|
-
model: 'openai/gpt-
|
|
45
|
+
id: 'test-agent',
|
|
46
|
+
name: 'Test Agent',
|
|
47
|
+
instructions: 'You are a helpful assistant',
|
|
48
|
+
model: 'openai/gpt-5.4',
|
|
49
49
|
memory: new Memory({
|
|
50
50
|
storage: new LibSQLStore({
|
|
51
|
-
id:
|
|
52
|
-
url:
|
|
51
|
+
id: 'memory-store',
|
|
52
|
+
url: 'file:memory.db',
|
|
53
53
|
}),
|
|
54
54
|
lastMessages: 10, // MessageHistory processor automatically added
|
|
55
55
|
}),
|
|
56
|
-
})
|
|
56
|
+
})
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
### SemanticRecall
|
|
59
|
+
### `SemanticRecall`
|
|
60
60
|
|
|
61
61
|
Retrieves semantically relevant messages based on the current input and creates embeddings for new messages.
|
|
62
62
|
|
|
@@ -67,7 +67,7 @@ memory: new Memory({
|
|
|
67
67
|
semanticRecall: { enabled: true },
|
|
68
68
|
vector: myVectorStore,
|
|
69
69
|
embedder: myEmbedder,
|
|
70
|
-
})
|
|
70
|
+
})
|
|
71
71
|
```
|
|
72
72
|
|
|
73
73
|
**Mastra internally:**
|
|
@@ -85,36 +85,36 @@ memory: new Memory({
|
|
|
85
85
|
**Example:**
|
|
86
86
|
|
|
87
87
|
```typescript
|
|
88
|
-
import { Agent } from
|
|
89
|
-
import { Memory } from
|
|
90
|
-
import { LibSQLStore } from
|
|
91
|
-
import { PineconeVector } from
|
|
92
|
-
import { OpenAIEmbedder } from
|
|
93
|
-
import { openai } from
|
|
88
|
+
import { Agent } from '@mastra/core/agent'
|
|
89
|
+
import { Memory } from '@mastra/memory'
|
|
90
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
91
|
+
import { PineconeVector } from '@mastra/pinecone'
|
|
92
|
+
import { OpenAIEmbedder } from '@mastra/openai'
|
|
93
|
+
import { openai } from '@ai-sdk/openai'
|
|
94
94
|
|
|
95
95
|
const agent = new Agent({
|
|
96
|
-
name:
|
|
97
|
-
instructions:
|
|
98
|
-
model: 'openai/gpt-
|
|
96
|
+
name: 'semantic-agent',
|
|
97
|
+
instructions: 'You are a helpful assistant with semantic memory',
|
|
98
|
+
model: 'openai/gpt-5.4',
|
|
99
99
|
memory: new Memory({
|
|
100
100
|
storage: new LibSQLStore({
|
|
101
|
-
id:
|
|
102
|
-
url:
|
|
101
|
+
id: 'memory-store',
|
|
102
|
+
url: 'file:memory.db',
|
|
103
103
|
}),
|
|
104
104
|
vector: new PineconeVector({
|
|
105
|
-
id:
|
|
105
|
+
id: 'memory-vector',
|
|
106
106
|
apiKey: process.env.PINECONE_API_KEY!,
|
|
107
107
|
}),
|
|
108
108
|
embedder: new OpenAIEmbedder({
|
|
109
|
-
model:
|
|
109
|
+
model: 'text-embedding-3-small',
|
|
110
110
|
apiKey: process.env.OPENAI_API_KEY!,
|
|
111
111
|
}),
|
|
112
112
|
semanticRecall: { enabled: true }, // SemanticRecall processor automatically added
|
|
113
113
|
}),
|
|
114
|
-
})
|
|
114
|
+
})
|
|
115
115
|
```
|
|
116
116
|
|
|
117
|
-
### WorkingMemory
|
|
117
|
+
### `WorkingMemory`
|
|
118
118
|
|
|
119
119
|
Manages working memory state across conversations.
|
|
120
120
|
|
|
@@ -123,7 +123,7 @@ Manages working memory state across conversations.
|
|
|
123
123
|
```typescript
|
|
124
124
|
memory: new Memory({
|
|
125
125
|
workingMemory: { enabled: true },
|
|
126
|
-
})
|
|
126
|
+
})
|
|
127
127
|
```
|
|
128
128
|
|
|
129
129
|
**Mastra internally:**
|
|
@@ -140,59 +140,59 @@ memory: new Memory({
|
|
|
140
140
|
**Example:**
|
|
141
141
|
|
|
142
142
|
```typescript
|
|
143
|
-
import { Agent } from
|
|
144
|
-
import { Memory } from
|
|
145
|
-
import { LibSQLStore } from
|
|
146
|
-
import { openai } from
|
|
143
|
+
import { Agent } from '@mastra/core/agent'
|
|
144
|
+
import { Memory } from '@mastra/memory'
|
|
145
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
146
|
+
import { openai } from '@ai-sdk/openai'
|
|
147
147
|
|
|
148
148
|
const agent = new Agent({
|
|
149
|
-
name:
|
|
150
|
-
instructions:
|
|
151
|
-
model: 'openai/gpt-
|
|
149
|
+
name: 'working-memory-agent',
|
|
150
|
+
instructions: 'You are an assistant with working memory',
|
|
151
|
+
model: 'openai/gpt-5.4',
|
|
152
152
|
memory: new Memory({
|
|
153
153
|
storage: new LibSQLStore({
|
|
154
|
-
id:
|
|
155
|
-
url:
|
|
154
|
+
id: 'memory-store',
|
|
155
|
+
url: 'file:memory.db',
|
|
156
156
|
}),
|
|
157
157
|
workingMemory: { enabled: true }, // WorkingMemory processor automatically added
|
|
158
158
|
}),
|
|
159
|
-
})
|
|
159
|
+
})
|
|
160
160
|
```
|
|
161
161
|
|
|
162
|
-
## Manual
|
|
162
|
+
## Manual control and deduplication
|
|
163
163
|
|
|
164
|
-
If you manually add a memory processor to `inputProcessors` or `outputProcessors`, Mastra
|
|
164
|
+
If you manually add a memory processor to `inputProcessors` or `outputProcessors`, Mastra **won't** automatically add it. This gives you full control over processor ordering:
|
|
165
165
|
|
|
166
166
|
```typescript
|
|
167
|
-
import { Agent } from
|
|
168
|
-
import { Memory } from
|
|
169
|
-
import { MessageHistory } from
|
|
170
|
-
import { TokenLimiter } from
|
|
171
|
-
import { LibSQLStore } from
|
|
172
|
-
import { openai } from
|
|
167
|
+
import { Agent } from '@mastra/core/agent'
|
|
168
|
+
import { Memory } from '@mastra/memory'
|
|
169
|
+
import { MessageHistory } from '@mastra/core/processors'
|
|
170
|
+
import { TokenLimiter } from '@mastra/core/processors'
|
|
171
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
172
|
+
import { openai } from '@ai-sdk/openai'
|
|
173
173
|
|
|
174
174
|
// Custom MessageHistory with different configuration
|
|
175
175
|
const customMessageHistory = new MessageHistory({
|
|
176
|
-
storage: new LibSQLStore({ id:
|
|
176
|
+
storage: new LibSQLStore({ id: 'memory-store', url: 'file:memory.db' }),
|
|
177
177
|
lastMessages: 20,
|
|
178
|
-
})
|
|
178
|
+
})
|
|
179
179
|
|
|
180
180
|
const agent = new Agent({
|
|
181
|
-
name:
|
|
182
|
-
instructions:
|
|
183
|
-
model: 'openai/gpt-
|
|
181
|
+
name: 'custom-memory-agent',
|
|
182
|
+
instructions: 'You are a helpful assistant',
|
|
183
|
+
model: 'openai/gpt-5.4',
|
|
184
184
|
memory: new Memory({
|
|
185
|
-
storage: new LibSQLStore({ id:
|
|
185
|
+
storage: new LibSQLStore({ id: 'memory-store', url: 'file:memory.db' }),
|
|
186
186
|
lastMessages: 10, // This would normally add MessageHistory(10)
|
|
187
187
|
}),
|
|
188
188
|
inputProcessors: [
|
|
189
189
|
customMessageHistory, // Your custom one is used instead
|
|
190
190
|
new TokenLimiter({ limit: 4000 }), // Runs after your custom MessageHistory
|
|
191
191
|
],
|
|
192
|
-
})
|
|
192
|
+
})
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
-
## Processor
|
|
195
|
+
## Processor execution order
|
|
196
196
|
|
|
197
197
|
Understanding the execution order is important when combining guardrails with memory:
|
|
198
198
|
|
|
@@ -218,7 +218,7 @@ This means memory loads message history before your processors can validate or f
|
|
|
218
218
|
|
|
219
219
|
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**.
|
|
220
220
|
|
|
221
|
-
## Guardrails and
|
|
221
|
+
## Guardrails and memory
|
|
222
222
|
|
|
223
223
|
The default execution order provides safe guardrail behavior:
|
|
224
224
|
|
|
@@ -231,37 +231,35 @@ Output guardrails run **before** memory processors save messages. If a guardrail
|
|
|
231
231
|
- **No messages are persisted to storage**
|
|
232
232
|
|
|
233
233
|
```typescript
|
|
234
|
-
import { Agent } from
|
|
235
|
-
import { Memory } from
|
|
236
|
-
import { openai } from
|
|
234
|
+
import { Agent } from '@mastra/core/agent'
|
|
235
|
+
import { Memory } from '@mastra/memory'
|
|
236
|
+
import { openai } from '@ai-sdk/openai'
|
|
237
237
|
|
|
238
238
|
// Output guardrail that blocks inappropriate content
|
|
239
239
|
const contentBlocker = {
|
|
240
|
-
id:
|
|
240
|
+
id: 'content-blocker',
|
|
241
241
|
processOutputResult: async ({ messages, abort }) => {
|
|
242
|
-
const hasInappropriateContent = messages.some((msg)
|
|
243
|
-
containsBadContent(msg)
|
|
244
|
-
);
|
|
242
|
+
const hasInappropriateContent = messages.some(msg => containsBadContent(msg))
|
|
245
243
|
if (hasInappropriateContent) {
|
|
246
|
-
abort(
|
|
244
|
+
abort('Content blocked by guardrail')
|
|
247
245
|
}
|
|
248
|
-
return messages
|
|
246
|
+
return messages
|
|
249
247
|
},
|
|
250
|
-
}
|
|
248
|
+
}
|
|
251
249
|
|
|
252
250
|
const agent = new Agent({
|
|
253
|
-
name:
|
|
254
|
-
instructions:
|
|
255
|
-
model: 'openai/gpt-
|
|
251
|
+
name: 'safe-agent',
|
|
252
|
+
instructions: 'You are a helpful assistant',
|
|
253
|
+
model: 'openai/gpt-5.4',
|
|
256
254
|
memory: new Memory({ lastMessages: 10 }),
|
|
257
255
|
// Your guardrail runs BEFORE memory saves
|
|
258
256
|
outputProcessors: [contentBlocker],
|
|
259
|
-
})
|
|
257
|
+
})
|
|
260
258
|
|
|
261
259
|
// If the guardrail aborts, nothing is saved to memory
|
|
262
|
-
const result = await agent.generate(
|
|
260
|
+
const result = await agent.generate('Hello')
|
|
263
261
|
if (result.tripwire) {
|
|
264
|
-
console.log(
|
|
262
|
+
console.log('Blocked:', result.tripwire.reason)
|
|
265
263
|
// Memory is empty - no messages were persisted
|
|
266
264
|
}
|
|
267
265
|
```
|
|
@@ -278,24 +276,24 @@ Input guardrails run **after** memory processors load history. If a guardrail ab
|
|
|
278
276
|
```typescript
|
|
279
277
|
// Input guardrail that validates user input
|
|
280
278
|
const inputValidator = {
|
|
281
|
-
id:
|
|
279
|
+
id: 'input-validator',
|
|
282
280
|
processInput: async ({ messages, abort }) => {
|
|
283
|
-
const lastUserMessage = messages.findLast(
|
|
281
|
+
const lastUserMessage = messages.findLast(m => m.role === 'user')
|
|
284
282
|
if (isInvalidInput(lastUserMessage)) {
|
|
285
|
-
abort(
|
|
283
|
+
abort('Invalid input detected')
|
|
286
284
|
}
|
|
287
|
-
return messages
|
|
285
|
+
return messages
|
|
288
286
|
},
|
|
289
|
-
}
|
|
287
|
+
}
|
|
290
288
|
|
|
291
289
|
const agent = new Agent({
|
|
292
|
-
name:
|
|
293
|
-
instructions:
|
|
294
|
-
model: 'openai/gpt-
|
|
290
|
+
name: 'validated-agent',
|
|
291
|
+
instructions: 'You are a helpful assistant',
|
|
292
|
+
model: 'openai/gpt-5.4',
|
|
295
293
|
memory: new Memory({ lastMessages: 10 }),
|
|
296
294
|
// Your guardrail runs AFTER memory loads history
|
|
297
295
|
inputProcessors: [inputValidator],
|
|
298
|
-
})
|
|
296
|
+
})
|
|
299
297
|
```
|
|
300
298
|
|
|
301
299
|
### Summary
|
|
@@ -309,8 +307,8 @@ Both scenarios are safe - guardrails prevent inappropriate content from being pe
|
|
|
309
307
|
|
|
310
308
|
## Related documentation
|
|
311
309
|
|
|
312
|
-
- [Processors](https://mastra.ai/docs/agents/processors)
|
|
313
|
-
- [Guardrails](https://mastra.ai/docs/agents/guardrails)
|
|
314
|
-
- [Memory Overview](https://mastra.ai/docs/memory/overview)
|
|
310
|
+
- [Processors](https://mastra.ai/docs/agents/processors): General processor concepts and custom processor creation
|
|
311
|
+
- [Guardrails](https://mastra.ai/docs/agents/guardrails): Security and validation processors
|
|
312
|
+
- [Memory Overview](https://mastra.ai/docs/memory/overview): Memory types and configuration
|
|
315
313
|
|
|
316
314
|
When creating custom processors avoid mutating the input `messages` array or its objects directly.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Message
|
|
1
|
+
# Message history
|
|
2
2
|
|
|
3
3
|
Message history is the most basic and important form of memory. It gives the LLM a view of recent messages in the context window, enabling your agent to reference earlier exchanges and respond coherently.
|
|
4
4
|
|
|
@@ -6,6 +6,12 @@ You can also retrieve message history to display past conversations in your UI.
|
|
|
6
6
|
|
|
7
7
|
> **Info:** Each message belongs to a thread (the conversation) and a resource (the user or entity it's associated with). See [Threads and resources](https://mastra.ai/docs/memory/storage) for more detail.
|
|
8
8
|
|
|
9
|
+
> **Warning:** When you use memory with a client application, send **only the new message** from the client instead of the full conversation history.
|
|
10
|
+
>
|
|
11
|
+
> Sending the full history is redundant because Mastra loads messages from storage, and it can cause message ordering bugs when client-side timestamps conflict with stored timestamps.
|
|
12
|
+
>
|
|
13
|
+
> For an AI SDK example, see [Using Mastra Memory](https://mastra.ai/guides/build-your-ui/ai-sdk-ui).
|
|
14
|
+
|
|
9
15
|
## Getting started
|
|
10
16
|
|
|
11
17
|
Install the Mastra memory module along with a [storage adapter](https://mastra.ai/docs/memory/storage) for your database. The examples below use `@mastra/libsql`, which stores data locally in a `mastra.db` file.
|
|
@@ -37,79 +43,81 @@ bun add @mastra/memory@latest @mastra/libsql@latest
|
|
|
37
43
|
Message history requires a storage adapter to persist conversations. Configure storage on your Mastra instance if you haven't already:
|
|
38
44
|
|
|
39
45
|
```typescript
|
|
40
|
-
import { Mastra } from
|
|
41
|
-
import { LibSQLStore } from
|
|
46
|
+
import { Mastra } from '@mastra/core'
|
|
47
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
42
48
|
|
|
43
49
|
export const mastra = new Mastra({
|
|
44
50
|
storage: new LibSQLStore({
|
|
45
51
|
id: 'mastra-storage',
|
|
46
|
-
url:
|
|
52
|
+
url: 'file:./mastra.db',
|
|
47
53
|
}),
|
|
48
|
-
})
|
|
54
|
+
})
|
|
49
55
|
```
|
|
50
56
|
|
|
51
|
-
|
|
57
|
+
Instantiate a [`Memory`](https://mastra.ai/reference/memory/memory-class) instance in your agent:
|
|
52
58
|
|
|
53
59
|
```typescript
|
|
54
|
-
import { Memory } from
|
|
55
|
-
import { Agent } from
|
|
60
|
+
import { Memory } from '@mastra/memory'
|
|
61
|
+
import { Agent } from '@mastra/core/agent'
|
|
56
62
|
|
|
57
63
|
export const agent = new Agent({
|
|
58
|
-
id:
|
|
64
|
+
id: 'test-agent',
|
|
59
65
|
memory: new Memory({
|
|
60
66
|
options: {
|
|
61
67
|
lastMessages: 10,
|
|
62
68
|
},
|
|
63
69
|
}),
|
|
64
|
-
})
|
|
70
|
+
})
|
|
65
71
|
```
|
|
66
72
|
|
|
67
73
|
When you call the agent, messages are automatically saved to the database. You can specify a `threadId`, `resourceId`, and optional `metadata`:
|
|
68
74
|
|
|
69
|
-
|
|
75
|
+
**.generate()**:
|
|
70
76
|
|
|
71
77
|
```typescript
|
|
72
|
-
await agent.generate(
|
|
78
|
+
await agent.generate('Hello', {
|
|
73
79
|
memory: {
|
|
74
80
|
thread: {
|
|
75
|
-
id:
|
|
76
|
-
title:
|
|
77
|
-
metadata: { category:
|
|
81
|
+
id: 'thread-123',
|
|
82
|
+
title: 'Support conversation',
|
|
83
|
+
metadata: { category: 'billing' },
|
|
78
84
|
},
|
|
79
|
-
resource:
|
|
85
|
+
resource: 'user-456',
|
|
80
86
|
},
|
|
81
|
-
})
|
|
87
|
+
})
|
|
82
88
|
```
|
|
83
89
|
|
|
84
|
-
|
|
90
|
+
**.stream()**:
|
|
85
91
|
|
|
86
92
|
```typescript
|
|
87
|
-
await agent.stream(
|
|
93
|
+
await agent.stream('Hello', {
|
|
88
94
|
memory: {
|
|
89
95
|
thread: {
|
|
90
|
-
id:
|
|
91
|
-
title:
|
|
92
|
-
metadata: { category:
|
|
96
|
+
id: 'thread-123',
|
|
97
|
+
title: 'Support conversation',
|
|
98
|
+
metadata: { category: 'billing' },
|
|
93
99
|
},
|
|
94
|
-
resource:
|
|
100
|
+
resource: 'user-456',
|
|
95
101
|
},
|
|
96
|
-
})
|
|
102
|
+
})
|
|
97
103
|
```
|
|
98
104
|
|
|
99
105
|
> **Info:** Threads and messages are created automatically when you call `agent.generate()` or `agent.stream()`, but you can also create them manually with [`createThread()`](https://mastra.ai/reference/memory/createThread) and [`saveMessages()`](https://mastra.ai/reference/memory/memory-class).
|
|
100
106
|
|
|
101
|
-
|
|
107
|
+
You can use this history in two ways:
|
|
108
|
+
|
|
109
|
+
- **Automatic inclusion**: Mastra automatically fetches and includes recent messages in the context window. By default, it includes the last 10 messages, keeping agents grounded in the conversation. You can adjust this number with `lastMessages`, but in most cases you don't need to think about it.
|
|
110
|
+
- [**Manual querying**](#querying): For more control, use the `recall()` function to query threads and messages directly. This lets you choose exactly which memories are included in the context window, or fetch messages to render conversation history in your UI.
|
|
102
111
|
|
|
103
|
-
|
|
104
|
-
- [**Manual querying**](#querying) - For more control, use the `recall()` function to query threads and messages directly. This lets you choose exactly which memories are included in the context window, or fetch messages to render conversation history in your UI.
|
|
112
|
+
> **Tip:** When memory is enabled, [Studio](https://mastra.ai/docs/studio/overview) uses message history to display past conversations in the chat sidebar.
|
|
105
113
|
|
|
106
|
-
## Accessing
|
|
114
|
+
## Accessing memory
|
|
107
115
|
|
|
108
116
|
To access memory functions for querying, cloning, or deleting threads and messages, call `getMemory()` on an agent:
|
|
109
117
|
|
|
110
118
|
```typescript
|
|
111
|
-
const agent = mastra.
|
|
112
|
-
const memory = await agent.getMemory()
|
|
119
|
+
const agent = mastra.getAgentById('test-agent')
|
|
120
|
+
const memory = await agent.getMemory()
|
|
113
121
|
```
|
|
114
122
|
|
|
115
123
|
The `Memory` instance gives you access to functions for listing threads, recalling messages, cloning conversations, and more.
|
|
@@ -118,7 +126,7 @@ The `Memory` instance gives you access to functions for listing threads, recalli
|
|
|
118
126
|
|
|
119
127
|
Use these methods to fetch threads and messages for displaying conversation history in your UI or for custom memory retrieval logic.
|
|
120
128
|
|
|
121
|
-
> **Warning:** The memory system
|
|
129
|
+
> **Warning:** The memory system doesn't enforce access control. Before running any query, verify in your application logic that the current user is authorized to access the `resourceId` being queried.
|
|
122
130
|
|
|
123
131
|
### Threads
|
|
124
132
|
|
|
@@ -126,22 +134,22 @@ Use [`listThreads()`](https://mastra.ai/reference/memory/listThreads) to retriev
|
|
|
126
134
|
|
|
127
135
|
```typescript
|
|
128
136
|
const result = await memory.listThreads({
|
|
129
|
-
filter: { resourceId:
|
|
137
|
+
filter: { resourceId: 'user-123' },
|
|
130
138
|
perPage: false,
|
|
131
|
-
})
|
|
139
|
+
})
|
|
132
140
|
```
|
|
133
141
|
|
|
134
142
|
Paginate through threads:
|
|
135
143
|
|
|
136
144
|
```typescript
|
|
137
145
|
const result = await memory.listThreads({
|
|
138
|
-
filter: { resourceId:
|
|
146
|
+
filter: { resourceId: 'user-123' },
|
|
139
147
|
page: 0,
|
|
140
148
|
perPage: 10,
|
|
141
|
-
})
|
|
149
|
+
})
|
|
142
150
|
|
|
143
|
-
console.log(result.threads)
|
|
144
|
-
console.log(result.hasMore)
|
|
151
|
+
console.log(result.threads) // thread objects
|
|
152
|
+
console.log(result.hasMore) // more pages available?
|
|
145
153
|
```
|
|
146
154
|
|
|
147
155
|
You can also filter by metadata and control sort order:
|
|
@@ -149,17 +157,17 @@ You can also filter by metadata and control sort order:
|
|
|
149
157
|
```typescript
|
|
150
158
|
const result = await memory.listThreads({
|
|
151
159
|
filter: {
|
|
152
|
-
resourceId:
|
|
153
|
-
metadata: { status:
|
|
160
|
+
resourceId: 'user-123',
|
|
161
|
+
metadata: { status: 'active' },
|
|
154
162
|
},
|
|
155
|
-
orderBy: { field:
|
|
156
|
-
})
|
|
163
|
+
orderBy: { field: 'createdAt', direction: 'DESC' },
|
|
164
|
+
})
|
|
157
165
|
```
|
|
158
166
|
|
|
159
167
|
To fetch a single thread by ID, use [`getThreadById()`](https://mastra.ai/reference/memory/getThreadById):
|
|
160
168
|
|
|
161
169
|
```typescript
|
|
162
|
-
const thread = await memory.getThreadById({ threadId:
|
|
170
|
+
const thread = await memory.getThreadById({ threadId: 'thread-123' })
|
|
163
171
|
```
|
|
164
172
|
|
|
165
173
|
### Messages
|
|
@@ -170,70 +178,70 @@ Basic recall returns all messages from a thread:
|
|
|
170
178
|
|
|
171
179
|
```typescript
|
|
172
180
|
const { messages } = await memory.recall({
|
|
173
|
-
threadId:
|
|
181
|
+
threadId: 'thread-123',
|
|
174
182
|
perPage: false,
|
|
175
|
-
})
|
|
183
|
+
})
|
|
176
184
|
```
|
|
177
185
|
|
|
178
186
|
Paginate through messages:
|
|
179
187
|
|
|
180
188
|
```typescript
|
|
181
189
|
const { messages } = await memory.recall({
|
|
182
|
-
threadId:
|
|
190
|
+
threadId: 'thread-123',
|
|
183
191
|
page: 0,
|
|
184
192
|
perPage: 50,
|
|
185
|
-
})
|
|
193
|
+
})
|
|
186
194
|
```
|
|
187
195
|
|
|
188
196
|
Filter by date range:
|
|
189
197
|
|
|
190
198
|
```typescript
|
|
191
199
|
const { messages } = await memory.recall({
|
|
192
|
-
threadId:
|
|
200
|
+
threadId: 'thread-123',
|
|
193
201
|
filter: {
|
|
194
202
|
dateRange: {
|
|
195
|
-
start: new Date(
|
|
196
|
-
end: new Date(
|
|
203
|
+
start: new Date('2025-01-01'),
|
|
204
|
+
end: new Date('2025-06-01'),
|
|
197
205
|
},
|
|
198
206
|
},
|
|
199
|
-
})
|
|
207
|
+
})
|
|
200
208
|
```
|
|
201
209
|
|
|
202
210
|
Fetch a single message by ID:
|
|
203
211
|
|
|
204
212
|
```typescript
|
|
205
213
|
const { messages } = await memory.recall({
|
|
206
|
-
threadId:
|
|
207
|
-
include: [{ id:
|
|
208
|
-
})
|
|
214
|
+
threadId: 'thread-123',
|
|
215
|
+
include: [{ id: 'msg-123' }],
|
|
216
|
+
})
|
|
209
217
|
```
|
|
210
218
|
|
|
211
219
|
Fetch multiple messages by ID with surrounding context:
|
|
212
220
|
|
|
213
221
|
```typescript
|
|
214
222
|
const { messages } = await memory.recall({
|
|
215
|
-
threadId:
|
|
223
|
+
threadId: 'thread-123',
|
|
216
224
|
include: [
|
|
217
|
-
{ id:
|
|
225
|
+
{ id: 'msg-123' },
|
|
218
226
|
{
|
|
219
|
-
id:
|
|
227
|
+
id: 'msg-456',
|
|
220
228
|
withPreviousMessages: 3,
|
|
221
229
|
withNextMessages: 1,
|
|
222
230
|
},
|
|
223
231
|
],
|
|
224
|
-
})
|
|
232
|
+
})
|
|
225
233
|
```
|
|
226
234
|
|
|
227
235
|
Search by meaning (see [Semantic recall](https://mastra.ai/docs/memory/semantic-recall) for setup):
|
|
228
236
|
|
|
229
237
|
```typescript
|
|
230
238
|
const { messages } = await memory.recall({
|
|
231
|
-
threadId:
|
|
232
|
-
vectorSearchString:
|
|
239
|
+
threadId: 'thread-123',
|
|
240
|
+
vectorSearchString: 'project deadline discussion',
|
|
233
241
|
threadConfig: {
|
|
234
242
|
semanticRecall: true,
|
|
235
243
|
},
|
|
236
|
-
})
|
|
244
|
+
})
|
|
237
245
|
```
|
|
238
246
|
|
|
239
247
|
### UI format
|
|
@@ -246,9 +254,9 @@ Thread cloning creates a copy of an existing thread with its messages. This is u
|
|
|
246
254
|
|
|
247
255
|
```typescript
|
|
248
256
|
const { thread, clonedMessages } = await memory.cloneThread({
|
|
249
|
-
sourceThreadId:
|
|
250
|
-
title:
|
|
251
|
-
})
|
|
257
|
+
sourceThreadId: 'thread-123',
|
|
258
|
+
title: 'Branched conversation',
|
|
259
|
+
})
|
|
252
260
|
```
|
|
253
261
|
|
|
254
262
|
You can filter which messages get cloned (by count or date range), specify custom thread IDs, and use utility methods to inspect clone relationships.
|