@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
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# Multi-user threads
|
|
2
|
+
|
|
3
|
+
A single Mastra thread can be shared by multiple users, each with their own name and functional role. You carry speaker identity in the message body so the agent can tell users apart while reading from a single shared thread.
|
|
4
|
+
|
|
5
|
+
## When to use multi-user threads
|
|
6
|
+
|
|
7
|
+
Use multi-user threads when several people collaborate on the same subject through one agent:
|
|
8
|
+
|
|
9
|
+
- Collaborative documents with editors, reviewers, and approvers
|
|
10
|
+
- Group chats where one assistant serves many participants
|
|
11
|
+
- Multi-stakeholder reviews where different roles have different authority
|
|
12
|
+
|
|
13
|
+
## Share one `resourceId` across all participants
|
|
14
|
+
|
|
15
|
+
A thread belongs to exactly one `resourceId`, so all participants on a shared thread need to pass the same value. Instead of using a user id (the default for single-user apps), key `resourceId` on the conversation itself — for example `doc_${docId}` for a shared document, or `room_${roomId}` for a group chat. With everyone pointing at the same `resourceId`, they read and write the same history.
|
|
16
|
+
|
|
17
|
+
## Tag each user message with the speaker's identity
|
|
18
|
+
|
|
19
|
+
The model needs to know who's talking on every turn. Since the message body is the one place that survives into history and back into context, wrap each user message in a small `<turn>` tag with the speaker's id, name, and role. The tag stays attached to the message, so when prior turns are recalled the model still sees who said what.
|
|
20
|
+
|
|
21
|
+
Build the tag with a small helper. The example below is one way to do it — copy it into your project and adapt it to your shape of user data:
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
export type Speaker = {
|
|
25
|
+
id: string
|
|
26
|
+
name: string
|
|
27
|
+
role: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function escapeAttr(value: string) {
|
|
31
|
+
return value
|
|
32
|
+
.replace(/&/g, '&')
|
|
33
|
+
.replace(/"/g, '"')
|
|
34
|
+
.replace(/</g, '<')
|
|
35
|
+
.replace(/>/g, '>')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function asUserTurn(speaker: Speaker, text: string) {
|
|
39
|
+
const id = escapeAttr(speaker.id)
|
|
40
|
+
const name = escapeAttr(speaker.name)
|
|
41
|
+
const role = escapeAttr(speaker.role)
|
|
42
|
+
return {
|
|
43
|
+
role: 'user' as const,
|
|
44
|
+
content: `<turn author_id="${id}" author_name="${name}" functional_role="${role}">
|
|
45
|
+
${text}
|
|
46
|
+
</turn>`,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Teach the agent how to read the `<turn>` tag in its instructions. The agent must have `memory` configured so it can be called with a `thread` and `resource`:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Agent } from '@mastra/core/agent'
|
|
55
|
+
import { Memory } from '@mastra/memory'
|
|
56
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
57
|
+
|
|
58
|
+
const memory = new Memory({
|
|
59
|
+
storage: new LibSQLStore({ url: 'file:./collab.db' }),
|
|
60
|
+
options: {
|
|
61
|
+
lastMessages: 20,
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
export const collabAgent = new Agent({
|
|
66
|
+
id: 'collab',
|
|
67
|
+
name: 'CollabAgent',
|
|
68
|
+
model: '__OPENAI_MODEL_MINI__',
|
|
69
|
+
memory,
|
|
70
|
+
instructions: `
|
|
71
|
+
You are a collaborative document assistant. Multiple users talk to you in the SAME thread.
|
|
72
|
+
|
|
73
|
+
Every user message is wrapped in a <turn> tag carrying the user's identity:
|
|
74
|
+
|
|
75
|
+
<turn author_id="u_alice" author_name="Alice" functional_role="editor">
|
|
76
|
+
...message text...
|
|
77
|
+
</turn>
|
|
78
|
+
|
|
79
|
+
Rules:
|
|
80
|
+
1. Address users by their author_name.
|
|
81
|
+
2. Respect functional_role: editors propose changes, reviewers approve.
|
|
82
|
+
3. When attributing past statements, read author_name from the surrounding <turn> tag.
|
|
83
|
+
4. Do not echo the <turn> tags back at users.
|
|
84
|
+
`.trim(),
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Call the agent with the wrapped message. Every participant shares the same `thread` and `resource`:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { asUserTurn } from './identity'
|
|
92
|
+
|
|
93
|
+
const docResourceId = 'doc_42'
|
|
94
|
+
const docThreadId = 'doc_42'
|
|
95
|
+
|
|
96
|
+
const alice = { id: 'u_alice', name: 'Alice', role: 'editor' }
|
|
97
|
+
const bob = { id: 'u_bob', name: 'Bob', role: 'reviewer' }
|
|
98
|
+
|
|
99
|
+
await collabAgent.generate([asUserTurn(alice, 'My favorite color is teal.')], {
|
|
100
|
+
memory: { thread: docThreadId, resource: docResourceId },
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
await collabAgent.generate([asUserTurn(bob, 'I want QA sign-off before publish.')], {
|
|
104
|
+
memory: { thread: docThreadId, resource: docResourceId },
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The `<turn>` tag persists in the message body, so when history is recalled on later turns the model still sees who said what.
|
|
109
|
+
|
|
110
|
+
## Combining with memory layers
|
|
111
|
+
|
|
112
|
+
The user-tagging pattern composes with every memory layer. Pick the layer based on how long the conversation needs to remember per-user facts:
|
|
113
|
+
|
|
114
|
+
- **Short conversations** (a single session, or a thread small enough to fit in `lastMessages`), or when you need a verbatim record of who said what: use [message history alone](#message-history-alone). The user tags in history are enough; no extra memory layer needed.
|
|
115
|
+
- **Long-running threads** (conversations that outgrow `lastMessages`, where you need per-user facts to survive history eviction): use [observational memory](#with-observational-memory-recommended).
|
|
116
|
+
- **Need a structured participants list, or your storage adapter doesn't support OM** (OM requires LibSQL, PG, or MongoDB): use [working memory](#with-working-memory).
|
|
117
|
+
|
|
118
|
+
We recommend using observational memory or working memory, not both — they cover overlapping needs, and running both at once adds latency and token cost without much benefit.
|
|
119
|
+
|
|
120
|
+
### Message history alone
|
|
121
|
+
|
|
122
|
+
For short conversations, or when you need a verbatim record of who said what, the user tags in history are enough. `lastMessages` brings prior turns back into context with their attribution intact:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { Memory } from '@mastra/memory'
|
|
126
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
127
|
+
|
|
128
|
+
const memory = new Memory({
|
|
129
|
+
storage: new LibSQLStore({ url: 'file:./collab.db' }),
|
|
130
|
+
options: {
|
|
131
|
+
lastMessages: 20,
|
|
132
|
+
},
|
|
133
|
+
})
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The model reads identity from the `<turn>` tag on the current message and from prior tagged messages brought back through `lastMessages`.
|
|
137
|
+
|
|
138
|
+
### With observational memory (recommended)
|
|
139
|
+
|
|
140
|
+
[Observational Memory](https://mastra.ai/docs/memory/observational-memory) (OM) extracts per-user facts into a background log without burning the agent's tool budget. The default Observer model reads `<turn>` tags natively and produces named attribution like `Alice stated her favorite color is teal.` and `Bob asked for QA sign-off before publish.`
|
|
141
|
+
|
|
142
|
+
Prefer OM over working memory for multi-user threads when your storage supports it. OM extracts facts automatically, scales to any number of participants, and doesn't need template upkeep. Enable it with no overrides:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
import { Memory } from '@mastra/memory'
|
|
146
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
147
|
+
|
|
148
|
+
const memory = new Memory({
|
|
149
|
+
storage: new LibSQLStore({ url: 'file:./collab.db' }),
|
|
150
|
+
options: {
|
|
151
|
+
lastMessages: 20,
|
|
152
|
+
observationalMemory: true,
|
|
153
|
+
},
|
|
154
|
+
})
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
OM requires a storage adapter that supports it: `@mastra/libsql`, `@mastra/pg`, or `@mastra/mongodb`.
|
|
158
|
+
|
|
159
|
+
> **Note:** If you switch the Observer to a weaker model and see facts collapse to a generic `User`, use [`observation.instruction`](https://mastra.ai/reference/memory/observational-memory) to teach the Observer how to read the `<turn>` tag.
|
|
160
|
+
|
|
161
|
+
### With working memory
|
|
162
|
+
|
|
163
|
+
Use working memory when OM isn't an option — for example, when your storage adapter doesn't support OM, or when you need a structured, deterministic participants list the agent can read and write on every turn.
|
|
164
|
+
|
|
165
|
+
The default [working memory](https://mastra.ai/docs/memory/working-memory) template assumes one user per thread ("First Name", "Last Name", etc.). For multi-user threads, provide a template with a participants list:
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { Memory } from '@mastra/memory'
|
|
169
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
170
|
+
|
|
171
|
+
const memory = new Memory({
|
|
172
|
+
storage: new LibSQLStore({ url: 'file:./collab.db' }),
|
|
173
|
+
options: {
|
|
174
|
+
lastMessages: 20,
|
|
175
|
+
workingMemory: {
|
|
176
|
+
enabled: true,
|
|
177
|
+
scope: 'thread',
|
|
178
|
+
template: `# Document Collaboration State
|
|
179
|
+
|
|
180
|
+
## Participants
|
|
181
|
+
<!-- One entry per known collaborator. Use author_id as the stable key. -->
|
|
182
|
+
<!-- - **<author_name>** (<author_id>, <functional_role>): <their position> -->
|
|
183
|
+
|
|
184
|
+
## Open Questions
|
|
185
|
+
|
|
186
|
+
## Decisions
|
|
187
|
+
`,
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Set `scope: 'thread'` so the participants list belongs to the document, not to any individual user. Add one instruction telling the agent to append new participants to the list whenever a new `author_id` shows up in a `<turn>`.
|
|
194
|
+
|
|
195
|
+
For more on templates, see [Custom templates](https://mastra.ai/docs/memory/working-memory).
|
|
196
|
+
|
|
197
|
+
## Security
|
|
198
|
+
|
|
199
|
+
Set the `speaker` from your authenticated request context, never from the request body. If a client can choose its own `author_id`, one user can impersonate another. Use [Request Context](https://mastra.ai/docs/server/request-context) to read the verified user from your auth layer and build the `<turn>` tag on the server before calling the agent.
|
|
200
|
+
|
|
201
|
+
## Related
|
|
202
|
+
|
|
203
|
+
- [Working memory](https://mastra.ai/docs/memory/working-memory)
|
|
204
|
+
- [Observational memory](https://mastra.ai/docs/memory/observational-memory)
|
|
205
|
+
- [Share memory between agents](https://mastra.ai/docs/memory/overview)
|
|
206
|
+
- [`Memory` reference](https://mastra.ai/reference/memory/memory-class)
|
|
@@ -2,44 +2,241 @@
|
|
|
2
2
|
|
|
3
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
4
|
|
|
5
|
-
Mastra
|
|
5
|
+
Mastra agents can be configured to store [message history](https://mastra.ai/docs/memory/message-history). Additionally, you can enable:
|
|
6
6
|
|
|
7
|
-
- [
|
|
8
|
-
- [
|
|
9
|
-
- [
|
|
10
|
-
- [
|
|
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
|
+
- [Multi-user threads](https://mastra.ai/docs/memory/multi-user-threads): Share one thread between multiple users.
|
|
11
11
|
|
|
12
12
|
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.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
Memory results will be stored in one or more of your configured [storage providers](https://mastra.ai/docs/memory/storage).
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
## When to use memory
|
|
17
17
|
|
|
18
|
-
-
|
|
19
|
-
- [Working memory](https://mastra.ai/docs/memory/working-memory)
|
|
20
|
-
- [Semantic recall](https://mastra.ai/docs/memory/semantic-recall)
|
|
21
|
-
- [Observational memory](https://mastra.ai/docs/memory/observational-memory)
|
|
18
|
+
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.
|
|
22
19
|
|
|
23
|
-
##
|
|
20
|
+
## Quickstart
|
|
24
21
|
|
|
25
|
-
|
|
22
|
+
1. Install the `@mastra/memory` package.
|
|
26
23
|
|
|
27
|
-
|
|
24
|
+
**npm**:
|
|
28
25
|
|
|
29
|
-
|
|
26
|
+
```bash
|
|
27
|
+
npm install @mastra/memory@latest
|
|
28
|
+
```
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
**pnpm**:
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
```bash
|
|
33
|
+
pnpm add @mastra/memory@latest
|
|
34
|
+
```
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
**Yarn**:
|
|
36
37
|
|
|
37
|
-
|
|
38
|
+
```bash
|
|
39
|
+
yarn add @mastra/memory@latest
|
|
40
|
+
```
|
|
38
41
|
|
|
39
|
-
|
|
42
|
+
**Bun**:
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
```bash
|
|
45
|
+
bun add @mastra/memory@latest
|
|
46
|
+
```
|
|
42
47
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
2. Memory **requires** a storage provider to persist message history, including user messages and agent responses.
|
|
49
|
+
|
|
50
|
+
For the purposes of this quickstart, use `@mastra/libsql`.
|
|
51
|
+
|
|
52
|
+
**npm**:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npm install @mastra/libsql@latest
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**pnpm**:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pnpm add @mastra/libsql@latest
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Yarn**:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
yarn add @mastra/libsql@latest
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Bun**:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
bun add @mastra/libsql@latest
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
> **Note:** For more details on available providers and how storage works in Mastra, visit the [storage](https://mastra.ai/docs/memory/storage) documentation.
|
|
77
|
+
|
|
78
|
+
3. Add the storage provider to your main Mastra instance to enable memory across all configured agents.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { Mastra } from '@mastra/core'
|
|
82
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
83
|
+
|
|
84
|
+
export const mastra = new Mastra({
|
|
85
|
+
storage: new LibSQLStore({
|
|
86
|
+
id: 'mastra-storage',
|
|
87
|
+
url: ':memory:',
|
|
88
|
+
}),
|
|
89
|
+
})
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
4. Create a `Memory` instance and pass it to the agent's `memory` option.
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { Agent } from '@mastra/core/agent'
|
|
96
|
+
import { Memory } from '@mastra/memory'
|
|
97
|
+
|
|
98
|
+
export const memoryAgent = new Agent({
|
|
99
|
+
id: 'memory-agent',
|
|
100
|
+
name: 'Memory Agent',
|
|
101
|
+
memory: new Memory({
|
|
102
|
+
options: {
|
|
103
|
+
lastMessages: 20,
|
|
104
|
+
},
|
|
105
|
+
}),
|
|
106
|
+
})
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
> **Note:** Visit [Memory Class](https://mastra.ai/reference/memory/memory-class) for a full list of configuration options.
|
|
110
|
+
|
|
111
|
+
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.
|
|
112
|
+
|
|
113
|
+
## Message history
|
|
114
|
+
|
|
115
|
+
Pass a `memory` object with `resource` and `thread` to track message history.
|
|
116
|
+
|
|
117
|
+
- `resource`: A stable identifier for the user or entity.
|
|
118
|
+
- `thread`: An ID that isolates a specific conversation or session.
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
const response = await memoryAgent.generate('Remember my favorite color is blue.', {
|
|
122
|
+
memory: {
|
|
123
|
+
resource: 'user-123',
|
|
124
|
+
thread: 'conversation-123',
|
|
125
|
+
},
|
|
126
|
+
})
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
To recall information stored in memory, call the agent with the same `resource` and `thread` values used in the original conversation.
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
const response = await memoryAgent.generate("What's my favorite color?", {
|
|
133
|
+
memory: {
|
|
134
|
+
resource: 'user-123',
|
|
135
|
+
thread: 'conversation-123',
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
// Response: "Your favorite color is blue."
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
> **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.
|
|
143
|
+
|
|
144
|
+
To list all threads for a resource, or retrieve a specific thread, [use the memory API directly](https://mastra.ai/docs/memory/message-history).
|
|
145
|
+
|
|
146
|
+
## Observational Memory
|
|
147
|
+
|
|
148
|
+
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.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { Agent } from '@mastra/core/agent'
|
|
152
|
+
import { Memory } from '@mastra/memory'
|
|
153
|
+
|
|
154
|
+
export const memoryAgent = new Agent({
|
|
155
|
+
id: 'memory-agent',
|
|
156
|
+
name: 'Memory Agent',
|
|
157
|
+
memory: new Memory({
|
|
158
|
+
options: {
|
|
159
|
+
observationalMemory: true,
|
|
160
|
+
},
|
|
161
|
+
}),
|
|
162
|
+
})
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
> **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.
|
|
166
|
+
|
|
167
|
+
## Memory in multi-agent systems
|
|
168
|
+
|
|
169
|
+
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.
|
|
170
|
+
|
|
171
|
+
### How delegation scopes memory
|
|
172
|
+
|
|
173
|
+
Each delegation creates a fresh `threadId` and a deterministic `resourceId` for the subagent:
|
|
174
|
+
|
|
175
|
+
- **Thread ID**: Unique per delegation. The subagent starts with a clean message history every time it's called.
|
|
176
|
+
- **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.
|
|
177
|
+
- **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.
|
|
178
|
+
|
|
179
|
+
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.
|
|
180
|
+
|
|
181
|
+
> **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.
|
|
182
|
+
|
|
183
|
+
To go beyond this default isolation, you can share memory between agents by passing matching identifiers when you call them directly.
|
|
184
|
+
|
|
185
|
+
### Share memory between agents
|
|
186
|
+
|
|
187
|
+
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.
|
|
188
|
+
|
|
189
|
+
**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:
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
// Both agents share the same resource-scoped memory
|
|
193
|
+
await researcher.generate('Find information about quantum computing.', {
|
|
194
|
+
memory: { resource: 'project-42', thread: 'research-session' },
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
await writer.generate('Write a summary from the research notes.', {
|
|
198
|
+
memory: { resource: 'project-42', thread: 'writing-session' },
|
|
199
|
+
})
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
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.
|
|
203
|
+
|
|
204
|
+
**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.
|
|
205
|
+
|
|
206
|
+
## Observability
|
|
207
|
+
|
|
208
|
+
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.
|
|
209
|
+
|
|
210
|
+
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.
|
|
211
|
+
|
|
212
|
+
## Switch memory per request
|
|
213
|
+
|
|
214
|
+
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.
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
export type UserTier = {
|
|
218
|
+
'user-tier': 'enterprise' | 'pro'
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const premiumMemory = new Memory()
|
|
222
|
+
const standardMemory = new Memory()
|
|
223
|
+
|
|
224
|
+
export const memoryAgent = new Agent({
|
|
225
|
+
id: 'memory-agent',
|
|
226
|
+
name: 'Memory Agent',
|
|
227
|
+
memory: ({ requestContext }) => {
|
|
228
|
+
const userTier = requestContext.get('user-tier') as UserTier['user-tier']
|
|
229
|
+
|
|
230
|
+
return userTier === 'enterprise' ? premiumMemory : standardMemory
|
|
231
|
+
},
|
|
232
|
+
})
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
> **Note:** Visit [Request Context](https://mastra.ai/docs/server/request-context) for more information.
|
|
236
|
+
|
|
237
|
+
## Related
|
|
238
|
+
|
|
239
|
+
- [`Memory` reference](https://mastra.ai/reference/memory/memory-class)
|
|
240
|
+
- [Tracing](https://mastra.ai/docs/observability/tracing/overview)
|
|
241
|
+
- [Request Context](https://mastra.ai/docs/server/request-context)
|
|
242
|
+
- [Mastra Code](https://code.mastra.ai/): A coding agent using Mastra's memory system
|