@mastra/libsql 0.0.0-error-handler-fix-20251020202607 → 0.0.0-everlasting-joggers-20260331153923
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 +3120 -3
- package/LICENSE.md +15 -0
- package/README.md +30 -20
- package/dist/docs/SKILL.md +46 -0
- package/dist/docs/assets/SOURCE_MAP.json +6 -0
- package/dist/docs/references/docs-agents-agent-approval.md +509 -0
- package/dist/docs/references/docs-agents-networks.md +182 -0
- package/dist/docs/references/docs-memory-memory-processors.md +314 -0
- package/dist/docs/references/docs-memory-message-history.md +262 -0
- package/dist/docs/references/docs-memory-overview.md +240 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +297 -0
- package/dist/docs/references/docs-memory-storage.md +261 -0
- package/dist/docs/references/docs-memory-working-memory.md +400 -0
- package/dist/docs/references/docs-rag-retrieval.md +515 -0
- package/dist/docs/references/docs-workflows-snapshots.md +238 -0
- package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +140 -0
- package/dist/docs/references/reference-core-getMemory.md +49 -0
- package/dist/docs/references/reference-core-listMemory.md +55 -0
- package/dist/docs/references/reference-core-mastra-class.md +66 -0
- package/dist/docs/references/reference-memory-memory-class.md +145 -0
- package/dist/docs/references/reference-storage-composite.md +243 -0
- package/dist/docs/references/reference-storage-dynamodb.md +282 -0
- package/dist/docs/references/reference-storage-libsql.md +135 -0
- package/dist/docs/references/reference-vectors-libsql.md +305 -0
- package/dist/index.cjs +9006 -1972
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +8993 -1973
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +318 -0
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/{domains → db}/utils.d.ts +21 -13
- package/dist/storage/db/utils.d.ts.map +1 -0
- package/dist/storage/domains/agents/index.d.ts +30 -0
- package/dist/storage/domains/agents/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/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/mcp-clients/index.d.ts +26 -0
- package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -0
- 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 +41 -53
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +41 -29
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/prompt-blocks/index.d.ts +25 -0
- package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts +26 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +19 -31
- package/dist/storage/domains/scores/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 +20 -32
- 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 +68 -259
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +12 -4
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/sql-builder.d.ts.map +1 -1
- package/package.json +17 -13
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -18
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/operations/index.d.ts +0 -110
- package/dist/storage/domains/operations/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -21
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
- package/dist/storage/domains/utils.d.ts.map +0 -1
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# Message history
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
You can also retrieve message history to display past conversations in your UI.
|
|
6
|
+
|
|
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
|
+
|
|
9
|
+
## Getting started
|
|
10
|
+
|
|
11
|
+
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.
|
|
12
|
+
|
|
13
|
+
**npm**:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @mastra/memory@latest @mastra/libsql@latest
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**pnpm**:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm add @mastra/memory@latest @mastra/libsql@latest
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Yarn**:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
yarn add @mastra/memory@latest @mastra/libsql@latest
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**Bun**:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
bun add @mastra/memory@latest @mastra/libsql@latest
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Message history requires a storage adapter to persist conversations. Configure storage on your Mastra instance if you haven't already:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { Mastra } from '@mastra/core'
|
|
41
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
42
|
+
|
|
43
|
+
export const mastra = new Mastra({
|
|
44
|
+
storage: new LibSQLStore({
|
|
45
|
+
id: 'mastra-storage',
|
|
46
|
+
url: 'file:./mastra.db',
|
|
47
|
+
}),
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Instantiate a [`Memory`](https://mastra.ai/reference/memory/memory-class) instance in your agent:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Memory } from '@mastra/memory'
|
|
55
|
+
import { Agent } from '@mastra/core/agent'
|
|
56
|
+
|
|
57
|
+
export const agent = new Agent({
|
|
58
|
+
id: 'test-agent',
|
|
59
|
+
memory: new Memory({
|
|
60
|
+
options: {
|
|
61
|
+
lastMessages: 10,
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
When you call the agent, messages are automatically saved to the database. You can specify a `threadId`, `resourceId`, and optional `metadata`:
|
|
68
|
+
|
|
69
|
+
**.generate()**:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
await agent.generate('Hello', {
|
|
73
|
+
memory: {
|
|
74
|
+
thread: {
|
|
75
|
+
id: 'thread-123',
|
|
76
|
+
title: 'Support conversation',
|
|
77
|
+
metadata: { category: 'billing' },
|
|
78
|
+
},
|
|
79
|
+
resource: 'user-456',
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**.stream()**:
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
await agent.stream('Hello', {
|
|
88
|
+
memory: {
|
|
89
|
+
thread: {
|
|
90
|
+
id: 'thread-123',
|
|
91
|
+
title: 'Support conversation',
|
|
92
|
+
metadata: { category: 'billing' },
|
|
93
|
+
},
|
|
94
|
+
resource: 'user-456',
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
> **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
|
+
|
|
101
|
+
You can use this history in two ways:
|
|
102
|
+
|
|
103
|
+
- **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.
|
|
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.
|
|
105
|
+
|
|
106
|
+
> **Tip:** When memory is enabled, [Studio](https://mastra.ai/docs/studio/overview) uses message history to display past conversations in the chat sidebar.
|
|
107
|
+
|
|
108
|
+
## Accessing memory
|
|
109
|
+
|
|
110
|
+
To access memory functions for querying, cloning, or deleting threads and messages, call `getMemory()` on an agent:
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const agent = mastra.getAgentById('test-agent')
|
|
114
|
+
const memory = await agent.getMemory()
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The `Memory` instance gives you access to functions for listing threads, recalling messages, cloning conversations, and more.
|
|
118
|
+
|
|
119
|
+
## Querying
|
|
120
|
+
|
|
121
|
+
Use these methods to fetch threads and messages for displaying conversation history in your UI or for custom memory retrieval logic.
|
|
122
|
+
|
|
123
|
+
> **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.
|
|
124
|
+
|
|
125
|
+
### Threads
|
|
126
|
+
|
|
127
|
+
Use [`listThreads()`](https://mastra.ai/reference/memory/listThreads) to retrieve threads for a resource:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
const result = await memory.listThreads({
|
|
131
|
+
filter: { resourceId: 'user-123' },
|
|
132
|
+
perPage: false,
|
|
133
|
+
})
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Paginate through threads:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
const result = await memory.listThreads({
|
|
140
|
+
filter: { resourceId: 'user-123' },
|
|
141
|
+
page: 0,
|
|
142
|
+
perPage: 10,
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
console.log(result.threads) // thread objects
|
|
146
|
+
console.log(result.hasMore) // more pages available?
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
You can also filter by metadata and control sort order:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
const result = await memory.listThreads({
|
|
153
|
+
filter: {
|
|
154
|
+
resourceId: 'user-123',
|
|
155
|
+
metadata: { status: 'active' },
|
|
156
|
+
},
|
|
157
|
+
orderBy: { field: 'createdAt', direction: 'DESC' },
|
|
158
|
+
})
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
To fetch a single thread by ID, use [`getThreadById()`](https://mastra.ai/reference/memory/getThreadById):
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
const thread = await memory.getThreadById({ threadId: 'thread-123' })
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Messages
|
|
168
|
+
|
|
169
|
+
Once you have a thread, use [`recall()`](https://mastra.ai/reference/memory/recall) to retrieve its messages. It supports pagination, date filtering, and [semantic search](https://mastra.ai/docs/memory/semantic-recall).
|
|
170
|
+
|
|
171
|
+
Basic recall returns all messages from a thread:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
const { messages } = await memory.recall({
|
|
175
|
+
threadId: 'thread-123',
|
|
176
|
+
perPage: false,
|
|
177
|
+
})
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Paginate through messages:
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
const { messages } = await memory.recall({
|
|
184
|
+
threadId: 'thread-123',
|
|
185
|
+
page: 0,
|
|
186
|
+
perPage: 50,
|
|
187
|
+
})
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Filter by date range:
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
const { messages } = await memory.recall({
|
|
194
|
+
threadId: 'thread-123',
|
|
195
|
+
filter: {
|
|
196
|
+
dateRange: {
|
|
197
|
+
start: new Date('2025-01-01'),
|
|
198
|
+
end: new Date('2025-06-01'),
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
})
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Fetch a single message by ID:
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
const { messages } = await memory.recall({
|
|
208
|
+
threadId: 'thread-123',
|
|
209
|
+
include: [{ id: 'msg-123' }],
|
|
210
|
+
})
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Fetch multiple messages by ID with surrounding context:
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
const { messages } = await memory.recall({
|
|
217
|
+
threadId: 'thread-123',
|
|
218
|
+
include: [
|
|
219
|
+
{ id: 'msg-123' },
|
|
220
|
+
{
|
|
221
|
+
id: 'msg-456',
|
|
222
|
+
withPreviousMessages: 3,
|
|
223
|
+
withNextMessages: 1,
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
})
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Search by meaning (see [Semantic recall](https://mastra.ai/docs/memory/semantic-recall) for setup):
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
const { messages } = await memory.recall({
|
|
233
|
+
threadId: 'thread-123',
|
|
234
|
+
vectorSearchString: 'project deadline discussion',
|
|
235
|
+
threadConfig: {
|
|
236
|
+
semanticRecall: true,
|
|
237
|
+
},
|
|
238
|
+
})
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### UI format
|
|
242
|
+
|
|
243
|
+
Message queries return `MastraDBMessage[]` format. To display messages in a frontend, you may need to convert them to a format your UI library expects. For example, [`toAISdkV5Messages`](https://mastra.ai/reference/ai-sdk/to-ai-sdk-v5-messages) converts messages to AI SDK UI format.
|
|
244
|
+
|
|
245
|
+
## Thread cloning
|
|
246
|
+
|
|
247
|
+
Thread cloning creates a copy of an existing thread with its messages. This is useful for branching conversations, creating checkpoints before a potentially destructive operation, or testing variations of a conversation.
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
const { thread, clonedMessages } = await memory.cloneThread({
|
|
251
|
+
sourceThreadId: 'thread-123',
|
|
252
|
+
title: 'Branched conversation',
|
|
253
|
+
})
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
You can filter which messages get cloned (by count or date range), specify custom thread IDs, and use utility methods to inspect clone relationships.
|
|
257
|
+
|
|
258
|
+
See [`cloneThread()`](https://mastra.ai/reference/memory/cloneThread) and [clone utilities](https://mastra.ai/reference/memory/clone-utilities) for the full API.
|
|
259
|
+
|
|
260
|
+
## Deleting messages
|
|
261
|
+
|
|
262
|
+
To remove messages from a thread, use [`deleteMessages()`](https://mastra.ai/reference/memory/deleteMessages). You can delete by message ID or clear all messages from a thread.
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# Memory
|
|
2
|
+
|
|
3
|
+
Memory enables your agent to remember user messages, agent replies, and tool results across interactions, giving it the context it needs to stay consistent, maintain conversation flow, and produce better answers over time.
|
|
4
|
+
|
|
5
|
+
Mastra agents can be configured to store [message history](https://mastra.ai/docs/memory/message-history). Additionally, you can enable:
|
|
6
|
+
|
|
7
|
+
- [Observational Memory](https://mastra.ai/docs/memory/observational-memory) (Recommended): Uses background agents to maintain a dense observation log that replaces raw message history as it grows. This keeps the context window small while preserving long-term memory.
|
|
8
|
+
- [Working memory](https://mastra.ai/docs/memory/working-memory): Stores persistent, structured user data such as names, preferences, and goals.
|
|
9
|
+
- [Semantic recall](https://mastra.ai/docs/memory/semantic-recall): Retrieves relevant past messages based on semantic meaning rather than exact keywords.
|
|
10
|
+
|
|
11
|
+
If the combined memory exceeds the model's context limit, [memory processors](https://mastra.ai/docs/memory/memory-processors) can filter, trim, or prioritize content so the most relevant information is preserved.
|
|
12
|
+
|
|
13
|
+
Memory results will be stored in one or more of your configured [storage providers](https://mastra.ai/docs/memory/storage).
|
|
14
|
+
|
|
15
|
+
## When to use memory
|
|
16
|
+
|
|
17
|
+
Use memory when your agent needs to maintain multi-turn conversations that reference prior exchanges, recall user preferences or facts from earlier in a session, or build context over time within a conversation thread. Skip memory for single-turn requests where each interaction is independent.
|
|
18
|
+
|
|
19
|
+
## Quickstart
|
|
20
|
+
|
|
21
|
+
1. Install the `@mastra/memory` package.
|
|
22
|
+
|
|
23
|
+
**npm**:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @mastra/memory@latest
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**pnpm**:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pnpm add @mastra/memory@latest
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Yarn**:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
yarn add @mastra/memory@latest
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Bun**:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
bun add @mastra/memory@latest
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
2. Memory **requires** a storage provider to persist message history, including user messages and agent responses.
|
|
48
|
+
|
|
49
|
+
For the purposes of this quickstart, use `@mastra/libsql`.
|
|
50
|
+
|
|
51
|
+
**npm**:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install @mastra/libsql@latest
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**pnpm**:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pnpm add @mastra/libsql@latest
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**Yarn**:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
yarn add @mastra/libsql@latest
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Bun**:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
bun add @mastra/libsql@latest
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
> **Note:** For more details on available providers and how storage works in Mastra, visit the [storage](https://mastra.ai/docs/memory/storage) documentation.
|
|
76
|
+
|
|
77
|
+
3. Add the storage provider to your main Mastra instance to enable memory across all configured agents.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { Mastra } from '@mastra/core'
|
|
81
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
82
|
+
|
|
83
|
+
export const mastra = new Mastra({
|
|
84
|
+
storage: new LibSQLStore({
|
|
85
|
+
id: 'mastra-storage',
|
|
86
|
+
url: ':memory:',
|
|
87
|
+
}),
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
4. Create a `Memory` instance and pass it to the agent's `memory` option.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { Agent } from '@mastra/core/agent'
|
|
95
|
+
import { Memory } from '@mastra/memory'
|
|
96
|
+
|
|
97
|
+
export const memoryAgent = new Agent({
|
|
98
|
+
id: 'memory-agent',
|
|
99
|
+
name: 'Memory Agent',
|
|
100
|
+
memory: new Memory({
|
|
101
|
+
options: {
|
|
102
|
+
lastMessages: 20,
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
> **Note:** Visit [Memory Class](https://mastra.ai/reference/memory/memory-class) for a full list of configuration options.
|
|
109
|
+
|
|
110
|
+
5. Call your agent, for example in [Studio](https://mastra.ai/docs/studio/overview). Inside Studio, start a new chat with your agent and take a look at the right sidebar. It'll now display various memory-related information.
|
|
111
|
+
|
|
112
|
+
## Message history
|
|
113
|
+
|
|
114
|
+
Pass a `memory` object with `resource` and `thread` to track message history.
|
|
115
|
+
|
|
116
|
+
- `resource`: A stable identifier for the user or entity.
|
|
117
|
+
- `thread`: An ID that isolates a specific conversation or session.
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
const response = await memoryAgent.generate('Remember my favorite color is blue.', {
|
|
121
|
+
memory: {
|
|
122
|
+
resource: 'user-123',
|
|
123
|
+
thread: 'conversation-123',
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
To recall information stored in memory, call the agent with the same `resource` and `thread` values used in the original conversation.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const response = await memoryAgent.generate("What's my favorite color?", {
|
|
132
|
+
memory: {
|
|
133
|
+
resource: 'user-123',
|
|
134
|
+
thread: 'conversation-123',
|
|
135
|
+
},
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
// Response: "Your favorite color is blue."
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
> **Warning:** Each thread has an owner (`resourceId`) that can't be changed after creation. Avoid reusing the same thread ID for threads with different owners, as this will cause errors when querying.
|
|
142
|
+
|
|
143
|
+
To list all threads for a resource, or retrieve a specific thread, [use the memory API directly](https://mastra.ai/docs/memory/message-history).
|
|
144
|
+
|
|
145
|
+
## Observational Memory
|
|
146
|
+
|
|
147
|
+
For long-running conversations, raw message history grows until it fills the context window, degrading agent performance. [Observational Memory](https://mastra.ai/docs/memory/observational-memory) solves this by running background agents that compress old messages into dense observations, keeping the context window small while preserving long-term memory.
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { Agent } from '@mastra/core/agent'
|
|
151
|
+
import { Memory } from '@mastra/memory'
|
|
152
|
+
|
|
153
|
+
export const memoryAgent = new Agent({
|
|
154
|
+
id: 'memory-agent',
|
|
155
|
+
name: 'Memory Agent',
|
|
156
|
+
memory: new Memory({
|
|
157
|
+
options: {
|
|
158
|
+
observationalMemory: true,
|
|
159
|
+
},
|
|
160
|
+
}),
|
|
161
|
+
})
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
> **Note:** See [Observational Memory](https://mastra.ai/docs/memory/observational-memory) for details on how observations and reflections work, and [the reference](https://mastra.ai/reference/memory/observational-memory) for all configuration options.
|
|
165
|
+
|
|
166
|
+
## Memory in multi-agent systems
|
|
167
|
+
|
|
168
|
+
When a [supervisor agent](https://mastra.ai/docs/agents/supervisor-agents) delegates to a subagent, Mastra isolates subagent memory automatically. No flag enables this as it happens on every delegation. Understanding how this scoping works lets you decide what stays private and what to share intentionally.
|
|
169
|
+
|
|
170
|
+
### How delegation scopes memory
|
|
171
|
+
|
|
172
|
+
Each delegation creates a fresh `threadId` and a deterministic `resourceId` for the subagent:
|
|
173
|
+
|
|
174
|
+
- **Thread ID**: Unique per delegation. The subagent starts with a clean message history every time it's called.
|
|
175
|
+
- **Resource ID**: Derived as `{parentResourceId}-{agentName}`. Because the resource ID is stable across delegations, resource-scoped memory persists between calls. A subagent remembers facts from previous delegations by the same user.
|
|
176
|
+
- **Memory instance**: If a subagent has no memory configured, it inherits the supervisor's `Memory` instance. If the subagent defines its own, that takes precedence.
|
|
177
|
+
|
|
178
|
+
The supervisor forwards its conversation context to the subagent so it has enough background to complete the task. Only the delegation prompt and the subagent's response are saved — the full parent conversation isn't stored. You can control which messages reach the subagent with the [`messageFilter`](https://mastra.ai/docs/agents/supervisor-agents) callback.
|
|
179
|
+
|
|
180
|
+
> **Note:** Subagent resource IDs are always suffixed with the agent name (`{parentResourceId}-{agentName}`). Two different subagents under the same supervisor never share a resource ID through delegation.
|
|
181
|
+
|
|
182
|
+
To go beyond this default isolation, you can share memory between agents by passing matching identifiers when you call them directly.
|
|
183
|
+
|
|
184
|
+
### Share memory between agents
|
|
185
|
+
|
|
186
|
+
When you call agents directly (outside the delegation flow), memory sharing is controlled by two identifiers: `resourceId` and `threadId`. Agents that use the same values read and write to the same data. This is useful when agents collaborate on a shared context — for example, a researcher that saves notes and a writer that reads them.
|
|
187
|
+
|
|
188
|
+
**Resource-scoped sharing** is the most common pattern. [Working memory](https://mastra.ai/docs/memory/working-memory) and [semantic recall](https://mastra.ai/docs/memory/semantic-recall) default to `scope: 'resource'`. If two agents share a `resourceId`, they share observations, working memory, and embeddings — even across different threads:
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
// Both agents share the same resource-scoped memory
|
|
192
|
+
await researcher.generate('Find information about quantum computing.', {
|
|
193
|
+
memory: { resource: 'project-42', thread: 'research-session' },
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
await writer.generate('Write a summary from the research notes.', {
|
|
197
|
+
memory: { resource: 'project-42', thread: 'writing-session' },
|
|
198
|
+
})
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Because both calls use `resource: 'project-42'`, the writer can access the researcher's observations, working memory, and semantic embeddings. Each agent still has its own thread, so message histories stay separate.
|
|
202
|
+
|
|
203
|
+
**Thread-scoped sharing** gives tighter coupling. [Observational Memory](https://mastra.ai/docs/memory/observational-memory) uses `scope: 'thread'` by default. If two agents use the same `resource` _and_ `thread`, they share the full message history. Each agent sees every message the other has written. This is useful when agents need to build on each other's exact outputs.
|
|
204
|
+
|
|
205
|
+
## Observability
|
|
206
|
+
|
|
207
|
+
Enable [Tracing](https://mastra.ai/docs/observability/tracing/overview) to monitor and debug memory in action. Traces show you exactly which messages and observations the agent included in its context for each request, helping you understand agent behavior and verify that memory retrieval is working as expected.
|
|
208
|
+
|
|
209
|
+
Open [Studio](https://mastra.ai/docs/studio/overview) and select the **Observability** tab in the sidebar. Open the trace of a recent agent request, then look for spans of LLMs calls.
|
|
210
|
+
|
|
211
|
+
## Switch memory per request
|
|
212
|
+
|
|
213
|
+
Use [`RequestContext`](https://mastra.ai/docs/server/request-context) to access request-specific values. This lets you conditionally select different memory or storage configurations based on the context of the request.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
export type UserTier = {
|
|
217
|
+
'user-tier': 'enterprise' | 'pro'
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const premiumMemory = new Memory()
|
|
221
|
+
const standardMemory = new Memory()
|
|
222
|
+
|
|
223
|
+
export const memoryAgent = new Agent({
|
|
224
|
+
id: 'memory-agent',
|
|
225
|
+
name: 'Memory Agent',
|
|
226
|
+
memory: ({ requestContext }) => {
|
|
227
|
+
const userTier = requestContext.get('user-tier') as UserTier['user-tier']
|
|
228
|
+
|
|
229
|
+
return userTier === 'enterprise' ? premiumMemory : standardMemory
|
|
230
|
+
},
|
|
231
|
+
})
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
> **Note:** Visit [Request Context](https://mastra.ai/docs/server/request-context) for more information.
|
|
235
|
+
|
|
236
|
+
## Related
|
|
237
|
+
|
|
238
|
+
- [`Memory` reference](https://mastra.ai/reference/memory/memory-class)
|
|
239
|
+
- [Tracing](https://mastra.ai/docs/observability/tracing/overview)
|
|
240
|
+
- [Request Context](https://mastra.ai/docs/server/request-context)
|