@mastra/libsql 0.0.0-structured-output-issue-20260227214155 → 0.0.0-structured-output-errors-20260409185629
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 +344 -3
- package/LICENSE.md +15 -0
- package/dist/docs/SKILL.md +15 -19
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-agents-agent-approval.md +136 -185
- package/dist/docs/references/docs-agents-networks.md +90 -207
- package/dist/docs/references/docs-memory-memory-processors.md +15 -15
- package/dist/docs/references/docs-memory-message-history.md +10 -8
- package/dist/docs/references/docs-memory-overview.md +219 -24
- package/dist/docs/references/docs-memory-semantic-recall.md +54 -29
- package/dist/docs/references/docs-memory-storage.md +14 -16
- package/dist/docs/references/docs-memory-working-memory.md +22 -22
- package/dist/docs/references/docs-rag-retrieval.md +16 -16
- package/dist/docs/references/docs-workflows-snapshots.md +1 -1
- package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +3 -3
- package/dist/docs/references/reference-core-getMemory.md +4 -5
- package/dist/docs/references/reference-core-listMemory.md +3 -4
- package/dist/docs/references/reference-core-mastra-class.md +18 -18
- package/dist/docs/references/reference-memory-memory-class.md +16 -18
- package/dist/docs/references/reference-storage-composite.md +19 -11
- package/dist/docs/references/reference-storage-dynamodb.md +16 -16
- package/dist/docs/references/reference-storage-libsql.md +3 -3
- package/dist/docs/references/reference-vectors-libsql.md +47 -47
- package/dist/index.cjs +512 -82
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +513 -83
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +12 -0
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/domains/datasets/index.d.ts.map +1 -1
- package/dist/storage/domains/experiments/index.d.ts +3 -1
- package/dist/storage/domains/experiments/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +5 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/package.json +8 -8
- package/dist/docs/references/docs-agents-agent-memory.md +0 -209
- 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 -209
|
@@ -1,32 +1,16 @@
|
|
|
1
|
-
# Agent
|
|
1
|
+
# Agent networks
|
|
2
2
|
|
|
3
|
-
> **
|
|
3
|
+
> **Deprecated — Use supervisor agents:** Agent networks are deprecated and will be removed in a future major release. [Supervisor agents](https://mastra.ai/docs/agents/supervisor-agents) using `agent.stream()` or `agent.generate()` are now the recommended approach. It provides the same multi-agent coordination with better control, a simpler API, and easier debugging.
|
|
4
4
|
>
|
|
5
|
-
>
|
|
6
|
-
> - **Simpler API**: Uses familiar `stream()` and `generate()` methods instead of a separate `.network()` API
|
|
7
|
-
> - **More flexible**: Stop execution early, modify delegations, filter context, and provide feedback to guide the agent
|
|
8
|
-
> - **Type-safe**: Full TypeScript support for all hooks and callbacks
|
|
9
|
-
> - **Easier debugging**: Monitor progress with `onIterationComplete`, track delegations with `onDelegationStart`/`onDelegationComplete`
|
|
10
|
-
>
|
|
11
|
-
> See the [migration guide](https://mastra.ai/guides/migrations/network-to-supervisor) to upgrade from `.network()`.
|
|
12
|
-
|
|
13
|
-
Agent networks in Mastra coordinate multiple agents, workflows, and tools to handle tasks that aren't clearly defined upfront but can be inferred from the user's message or context. A top-level **routing agent** (a Mastra agent with other agents, workflows, and tools configured) uses an LLM to interpret the request and decide which primitives (subagents, workflows, or tools) to call, in what order, and with what data.
|
|
14
|
-
|
|
15
|
-
## When to use networks
|
|
5
|
+
> See the [migration guide](https://mastra.ai/guides/migrations/network-to-supervisor) to upgrade.
|
|
16
6
|
|
|
17
|
-
|
|
7
|
+
A **routing agent** uses an LLM to interpret a request and decide which primitives (subagents, workflows, or tools) to call, in what order, and with what data.
|
|
18
8
|
|
|
19
|
-
##
|
|
9
|
+
## Create an agent network
|
|
20
10
|
|
|
21
|
-
|
|
11
|
+
Configure a routing agent with `agents`, `workflows`, and `tools`. Memory is required as `.network()` uses it to store task history and determine when a task is complete.
|
|
22
12
|
|
|
23
|
-
|
|
24
|
-
- Primitives are selected based on their descriptions. Clear, specific descriptions improve routing. For workflows and tools, the input schema helps determine the right inputs at runtime.
|
|
25
|
-
- If multiple primitives have overlapping functionality, the agent favors the more specific one, using a combination of schema and descriptions to decide which to run.
|
|
26
|
-
|
|
27
|
-
## Creating an agent network
|
|
28
|
-
|
|
29
|
-
An agent network is built around a top-level routing agent that delegates tasks to subagents, workflows, and tools defined in its configuration. Memory is configured on the routing agent using the `memory` option, and `instructions` define the agent's routing behavior.
|
|
13
|
+
Each primitive needs a clear `description` so the routing agent can decide which to use. For workflows and tools, `inputSchema` and `outputSchema` also help the router determine the right inputs.
|
|
30
14
|
|
|
31
15
|
```typescript
|
|
32
16
|
import { Agent } from '@mastra/core/agent'
|
|
@@ -35,7 +19,6 @@ import { LibSQLStore } from '@mastra/libsql'
|
|
|
35
19
|
|
|
36
20
|
import { researchAgent } from './research-agent'
|
|
37
21
|
import { writingAgent } from './writing-agent'
|
|
38
|
-
|
|
39
22
|
import { cityWorkflow } from '../workflows/city-workflow'
|
|
40
23
|
import { weatherTool } from '../tools/weather-tool'
|
|
41
24
|
|
|
@@ -43,12 +26,8 @@ export const routingAgent = new Agent({
|
|
|
43
26
|
id: 'routing-agent',
|
|
44
27
|
name: 'Routing Agent',
|
|
45
28
|
instructions: `
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
Always respond with a complete report—no bullet points.
|
|
49
|
-
Write in full paragraphs, like a blog post.
|
|
50
|
-
Do not answer with incomplete or uncertain information.`,
|
|
51
|
-
model: 'openai/gpt-5.1',
|
|
29
|
+
You are a network of writers and researchers. The user will ask you to research a topic. Always respond with a complete report—no bullet points. Write in full paragraphs, like a blog post. Do not answer with incomplete or uncertain information.`,
|
|
30
|
+
model: 'openai/gpt-5.4',
|
|
52
31
|
agents: {
|
|
53
32
|
researchAgent,
|
|
54
33
|
writingAgent,
|
|
@@ -68,81 +47,11 @@ export const routingAgent = new Agent({
|
|
|
68
47
|
})
|
|
69
48
|
```
|
|
70
49
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
When configuring a Mastra agent network, each primitive (agent, workflow, or tool) needs a clear description to help the routing agent decide which to use. The routing agent uses each primitive's description and schema to determine what it does and how to use it. Clear descriptions and well-defined input and output schemas improve routing accuracy.
|
|
74
|
-
|
|
75
|
-
#### Agent descriptions
|
|
76
|
-
|
|
77
|
-
Each subagent in a network should include a clear `description` that explains what the agent does.
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
export const researchAgent = new Agent({
|
|
81
|
-
id: 'research-agent',
|
|
82
|
-
name: 'Research Agent',
|
|
83
|
-
description: `This agent gathers concise research insights in bullet-point form.
|
|
84
|
-
It's designed to extract key facts without generating full
|
|
85
|
-
responses or narrative content.`,
|
|
86
|
-
})
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
export const writingAgent = new Agent({
|
|
91
|
-
id: 'writing-agent',
|
|
92
|
-
name: 'Writing Agent',
|
|
93
|
-
description: `This agent turns researched material into well-structured
|
|
94
|
-
written content. It produces full-paragraph reports with no bullet points,
|
|
95
|
-
suitable for use in articles, summaries, or blog posts.`,
|
|
96
|
-
})
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
#### Workflow descriptions
|
|
100
|
-
|
|
101
|
-
Workflows in a network should include a `description` to explain their purpose, along with `inputSchema` and `outputSchema` to describe the expected data.
|
|
102
|
-
|
|
103
|
-
```typescript
|
|
104
|
-
export const cityWorkflow = createWorkflow({
|
|
105
|
-
id: 'city-workflow',
|
|
106
|
-
description: `This workflow handles city-specific research tasks.
|
|
107
|
-
It first gathers factual information about the city, then synthesizes
|
|
108
|
-
that research into a full written report. Use it when the user input
|
|
109
|
-
includes a city to be researched.`,
|
|
110
|
-
inputSchema: z.object({
|
|
111
|
-
city: z.string(),
|
|
112
|
-
}),
|
|
113
|
-
outputSchema: z.object({
|
|
114
|
-
text: z.string(),
|
|
115
|
-
}),
|
|
116
|
-
})
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
#### Tool descriptions
|
|
120
|
-
|
|
121
|
-
Tools in a network should include a `description` to explain their purpose, along with `inputSchema` and `outputSchema` to describe the expected data.
|
|
122
|
-
|
|
123
|
-
```typescript
|
|
124
|
-
export const weatherTool = createTool({
|
|
125
|
-
id: 'weather-tool',
|
|
126
|
-
description: ` Retrieves current weather information using the wttr.in API.
|
|
127
|
-
Accepts a city or location name as input and returns a short weather summary.
|
|
128
|
-
Use this tool whenever up-to-date weather data is requested.
|
|
129
|
-
`,
|
|
130
|
-
inputSchema: z.object({
|
|
131
|
-
location: z.string(),
|
|
132
|
-
}),
|
|
133
|
-
outputSchema: z.object({
|
|
134
|
-
weather: z.string(),
|
|
135
|
-
}),
|
|
136
|
-
})
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
## Calling agent networks
|
|
140
|
-
|
|
141
|
-
Call a Mastra agent network using `.network()` with a user message. The method returns a stream of events that you can iterate over to track execution progress and retrieve the final result.
|
|
50
|
+
> **Note:** Subagents need a `description` on the `Agent` instance. Workflows and tools need a `description` plus `inputSchema` and `outputSchema` on `createWorkflow()` or `createTool()`.
|
|
142
51
|
|
|
143
|
-
|
|
52
|
+
## Call the network
|
|
144
53
|
|
|
145
|
-
|
|
54
|
+
Call `.network()` with a user message. The method returns a stream of events you can iterate over.
|
|
146
55
|
|
|
147
56
|
```typescript
|
|
148
57
|
const result = await routingAgent.network('Tell me three cool ways to use Mastra')
|
|
@@ -155,145 +64,119 @@ for await (const chunk of result) {
|
|
|
155
64
|
}
|
|
156
65
|
```
|
|
157
66
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
The following `chunk.type` events are emitted during this request:
|
|
161
|
-
|
|
162
|
-
```text
|
|
163
|
-
routing-agent-start
|
|
164
|
-
routing-agent-end
|
|
165
|
-
agent-execution-start
|
|
166
|
-
agent-execution-event-start
|
|
167
|
-
agent-execution-event-step-start
|
|
168
|
-
agent-execution-event-text-start
|
|
169
|
-
agent-execution-event-text-delta
|
|
170
|
-
agent-execution-event-text-end
|
|
171
|
-
agent-execution-event-step-finish
|
|
172
|
-
agent-execution-event-finish
|
|
173
|
-
agent-execution-end
|
|
174
|
-
network-execution-event-step-finish
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
## Workflow example
|
|
67
|
+
## Structured output
|
|
178
68
|
|
|
179
|
-
|
|
69
|
+
Pass `structuredOutput` to get typed, validated results. Use `objectStream` for partial objects as they generate.
|
|
180
70
|
|
|
181
71
|
```typescript
|
|
182
|
-
|
|
72
|
+
import { z } from 'zod'
|
|
183
73
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
```
|
|
74
|
+
const resultSchema = z.object({
|
|
75
|
+
summary: z.string().describe('A brief summary of the findings'),
|
|
76
|
+
recommendations: z.array(z.string()).describe('List of recommendations'),
|
|
77
|
+
confidence: z.number().min(0).max(1).describe('Confidence score'),
|
|
78
|
+
})
|
|
191
79
|
|
|
192
|
-
|
|
80
|
+
const stream = await routingAgent.network('Research AI trends', {
|
|
81
|
+
structuredOutput: { schema: resultSchema },
|
|
82
|
+
})
|
|
193
83
|
|
|
194
|
-
|
|
84
|
+
for await (const partial of stream.objectStream) {
|
|
85
|
+
console.log('Building result:', partial)
|
|
86
|
+
}
|
|
195
87
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
workflow-execution-start
|
|
199
|
-
workflow-execution-event-workflow-start
|
|
200
|
-
workflow-execution-event-workflow-step-start
|
|
201
|
-
workflow-execution-event-workflow-step-result
|
|
202
|
-
workflow-execution-event-workflow-finish
|
|
203
|
-
workflow-execution-end
|
|
204
|
-
routing-agent-start
|
|
205
|
-
network-execution-event-step-finish
|
|
88
|
+
const final = await stream.object
|
|
89
|
+
console.log(final?.summary)
|
|
206
90
|
```
|
|
207
91
|
|
|
208
|
-
|
|
92
|
+
## Approve and decline tool calls
|
|
93
|
+
|
|
94
|
+
When a primitive requires approval, the stream emits an `agent-execution-approval` or `tool-execution-approval` chunk. Use `approveNetworkToolCall()` or `declineNetworkToolCall()` to respond.
|
|
209
95
|
|
|
210
|
-
|
|
96
|
+
Network approval uses snapshots to capture execution state. Ensure a [storage provider](https://mastra.ai/docs/memory/storage) is enabled in your Mastra instance.
|
|
211
97
|
|
|
212
98
|
```typescript
|
|
213
|
-
const
|
|
99
|
+
const stream = await routingAgent.network('Perform some sensitive action', {
|
|
100
|
+
memory: {
|
|
101
|
+
thread: 'user-123',
|
|
102
|
+
resource: 'my-app',
|
|
103
|
+
},
|
|
104
|
+
})
|
|
214
105
|
|
|
215
|
-
for await (const chunk of
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
106
|
+
for await (const chunk of stream) {
|
|
107
|
+
if (chunk.type === 'agent-execution-approval' || chunk.type === 'tool-execution-approval') {
|
|
108
|
+
// Approve
|
|
109
|
+
const approvedStream = await routingAgent.approveNetworkToolCall(chunk.payload.toolCallId, {
|
|
110
|
+
runId: stream.runId,
|
|
111
|
+
memory: { thread: 'user-123', resource: 'my-app' },
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
for await (const c of approvedStream) {
|
|
115
|
+
if (c.type === 'network-execution-event-step-finish') {
|
|
116
|
+
console.log(c.payload.result)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
219
119
|
}
|
|
220
120
|
}
|
|
221
121
|
```
|
|
222
122
|
|
|
223
|
-
|
|
123
|
+
To decline instead, call `declineNetworkToolCall()` with the same arguments.
|
|
224
124
|
|
|
225
|
-
|
|
125
|
+
## Suspend and resume
|
|
226
126
|
|
|
227
|
-
|
|
228
|
-
routing-agent-start
|
|
229
|
-
routing-agent-end
|
|
230
|
-
tool-execution-start
|
|
231
|
-
tool-execution-end
|
|
232
|
-
network-execution-event-step-finish
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
## Structured output
|
|
236
|
-
|
|
237
|
-
When you need typed, validated results from a network, use the `structuredOutput` option. After the network completes its task, it generates a structured response matching your schema.
|
|
127
|
+
When a primitive calls `suspend()`, the stream emits a suspension chunk (e.g., `tool-execution-suspended`). Use `resumeNetwork()` to provide the requested data and continue execution.
|
|
238
128
|
|
|
239
129
|
```typescript
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
const resultSchema = z.object({
|
|
243
|
-
summary: z.string().describe('A brief summary of the findings'),
|
|
244
|
-
recommendations: z.array(z.string()).describe('List of recommendations'),
|
|
245
|
-
confidence: z.number().min(0).max(1).describe('Confidence score'),
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
const stream = await routingAgent.network('Research AI trends', {
|
|
249
|
-
structuredOutput: {
|
|
250
|
-
schema: resultSchema,
|
|
251
|
-
},
|
|
130
|
+
const stream = await routingAgent.network('Delete the old records', {
|
|
131
|
+
memory: { thread: 'user-123', resource: 'my-app' },
|
|
252
132
|
})
|
|
253
133
|
|
|
254
|
-
// Consume the stream
|
|
255
134
|
for await (const chunk of stream) {
|
|
256
|
-
if (chunk.type === '
|
|
257
|
-
|
|
258
|
-
console.log('Partial:', chunk.payload.object)
|
|
259
|
-
}
|
|
260
|
-
if (chunk.type === 'network-object-result') {
|
|
261
|
-
// Final structured object
|
|
262
|
-
console.log('Final:', chunk.payload.object)
|
|
135
|
+
if (chunk.type === 'workflow-execution-suspended') {
|
|
136
|
+
console.log(chunk.payload.suspendPayload)
|
|
263
137
|
}
|
|
264
138
|
}
|
|
265
139
|
|
|
266
|
-
//
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
140
|
+
// Resume with user confirmation
|
|
141
|
+
const resumedStream = await routingAgent.resumeNetwork(
|
|
142
|
+
{ confirmed: true },
|
|
143
|
+
{
|
|
144
|
+
runId: stream.runId,
|
|
145
|
+
memory: { thread: 'user-123', resource: 'my-app' },
|
|
146
|
+
},
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
for await (const chunk of resumedStream) {
|
|
150
|
+
if (chunk.type === 'network-execution-event-step-finish') {
|
|
151
|
+
console.log(chunk.payload.result)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
271
154
|
```
|
|
272
155
|
|
|
273
|
-
###
|
|
156
|
+
### Automatic resumption
|
|
274
157
|
|
|
275
|
-
|
|
158
|
+
Set `autoResumeSuspendedTools` to `true` so the network resumes suspended primitives based on the user's next message. This creates a conversational flow where users provide the required information naturally.
|
|
276
159
|
|
|
277
160
|
```typescript
|
|
278
|
-
const stream = await routingAgent.network('
|
|
279
|
-
|
|
161
|
+
const stream = await routingAgent.network('Delete the old records', {
|
|
162
|
+
autoResumeSuspendedTools: true,
|
|
163
|
+
memory: { thread: 'user-123', resource: 'my-app' },
|
|
280
164
|
})
|
|
165
|
+
```
|
|
281
166
|
|
|
282
|
-
|
|
283
|
-
for await (const partial of stream.objectStream) {
|
|
284
|
-
console.log('Building result:', partial)
|
|
285
|
-
}
|
|
167
|
+
Requirements for automatic resumption:
|
|
286
168
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
169
|
+
- **Memory configured**: The agent needs memory to track suspended tools across messages.
|
|
170
|
+
- **Same thread**: The follow-up message must use the same `thread` and `resource` identifiers.
|
|
171
|
+
- **`resumeSchema` defined**: The tool must define a `resumeSchema` so the network can extract data from the user's message.
|
|
172
|
+
|
|
173
|
+
| | Manual (`resumeNetwork`) | Automatic (`autoResumeSuspendedTools`) |
|
|
174
|
+
| -------- | ---------------------------------------------- | ----------------------------------------- |
|
|
175
|
+
| Best for | Custom UIs with approval buttons | Chat-style interfaces |
|
|
176
|
+
| Control | Full control over resume timing and data | Network extracts data from user's message |
|
|
177
|
+
| Setup | Handle suspension chunks, call `resumeNetwork` | Set flag, define `resumeSchema` on tools |
|
|
290
178
|
|
|
291
179
|
## Related
|
|
292
180
|
|
|
293
|
-
- [Supervisor
|
|
294
|
-
- [Migration:
|
|
295
|
-
- [Guide: Research Coordinator](https://mastra.ai/guides/guide/research-coordinator)
|
|
296
|
-
- [Agent Memory](https://mastra.ai/docs/agents/agent-memory)
|
|
297
|
-
- [Agent Approval](https://mastra.ai/docs/agents/agent-approval)
|
|
298
|
-
- [Workflows Overview](https://mastra.ai/docs/workflows/overview)
|
|
299
|
-
- [Request Context](https://mastra.ai/docs/server/request-context)
|
|
181
|
+
- [Supervisor agents](https://mastra.ai/docs/agents/supervisor-agents)
|
|
182
|
+
- [Migration: `.network()` to supervisor agents](https://mastra.ai/guides/migrations/network-to-supervisor)
|
|
@@ -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
|
|
|
@@ -45,7 +45,7 @@ const agent = new Agent({
|
|
|
45
45
|
id: 'test-agent',
|
|
46
46
|
name: 'Test Agent',
|
|
47
47
|
instructions: 'You are a helpful assistant',
|
|
48
|
-
model: 'openai/gpt-
|
|
48
|
+
model: 'openai/gpt-5.4',
|
|
49
49
|
memory: new Memory({
|
|
50
50
|
storage: new LibSQLStore({
|
|
51
51
|
id: 'memory-store',
|
|
@@ -56,7 +56,7 @@ const agent = new Agent({
|
|
|
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
|
|
|
@@ -95,7 +95,7 @@ import { openai } from '@ai-sdk/openai'
|
|
|
95
95
|
const agent = new Agent({
|
|
96
96
|
name: 'semantic-agent',
|
|
97
97
|
instructions: 'You are a helpful assistant with semantic memory',
|
|
98
|
-
model: 'openai/gpt-
|
|
98
|
+
model: 'openai/gpt-5.4',
|
|
99
99
|
memory: new Memory({
|
|
100
100
|
storage: new LibSQLStore({
|
|
101
101
|
id: 'memory-store',
|
|
@@ -114,7 +114,7 @@ const agent = new Agent({
|
|
|
114
114
|
})
|
|
115
115
|
```
|
|
116
116
|
|
|
117
|
-
### WorkingMemory
|
|
117
|
+
### `WorkingMemory`
|
|
118
118
|
|
|
119
119
|
Manages working memory state across conversations.
|
|
120
120
|
|
|
@@ -148,7 +148,7 @@ import { openai } from '@ai-sdk/openai'
|
|
|
148
148
|
const agent = new Agent({
|
|
149
149
|
name: 'working-memory-agent',
|
|
150
150
|
instructions: 'You are an assistant with working memory',
|
|
151
|
-
model: 'openai/gpt-
|
|
151
|
+
model: 'openai/gpt-5.4',
|
|
152
152
|
memory: new Memory({
|
|
153
153
|
storage: new LibSQLStore({
|
|
154
154
|
id: 'memory-store',
|
|
@@ -159,9 +159,9 @@ const agent = new Agent({
|
|
|
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
167
|
import { Agent } from '@mastra/core/agent'
|
|
@@ -180,7 +180,7 @@ const customMessageHistory = new MessageHistory({
|
|
|
180
180
|
const agent = new Agent({
|
|
181
181
|
name: 'custom-memory-agent',
|
|
182
182
|
instructions: 'You are a helpful assistant',
|
|
183
|
-
model: 'openai/gpt-
|
|
183
|
+
model: 'openai/gpt-5.4',
|
|
184
184
|
memory: new Memory({
|
|
185
185
|
storage: new LibSQLStore({ id: 'memory-store', url: 'file:memory.db' }),
|
|
186
186
|
lastMessages: 10, // This would normally add MessageHistory(10)
|
|
@@ -192,7 +192,7 @@ const agent = new Agent({
|
|
|
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
|
|
|
@@ -250,7 +250,7 @@ const contentBlocker = {
|
|
|
250
250
|
const agent = new Agent({
|
|
251
251
|
name: 'safe-agent',
|
|
252
252
|
instructions: 'You are a helpful assistant',
|
|
253
|
-
model: 'openai/gpt-
|
|
253
|
+
model: 'openai/gpt-5.4',
|
|
254
254
|
memory: new Memory({ lastMessages: 10 }),
|
|
255
255
|
// Your guardrail runs BEFORE memory saves
|
|
256
256
|
outputProcessors: [contentBlocker],
|
|
@@ -289,7 +289,7 @@ const inputValidator = {
|
|
|
289
289
|
const agent = new Agent({
|
|
290
290
|
name: 'validated-agent',
|
|
291
291
|
instructions: 'You are a helpful assistant',
|
|
292
|
-
model: 'openai/gpt-
|
|
292
|
+
model: 'openai/gpt-5.4',
|
|
293
293
|
memory: new Memory({ lastMessages: 10 }),
|
|
294
294
|
// Your guardrail runs AFTER memory loads history
|
|
295
295
|
inputProcessors: [inputValidator],
|
|
@@ -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
|
|
|
@@ -48,7 +48,7 @@ export const mastra = new Mastra({
|
|
|
48
48
|
})
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
Instantiate a [`Memory`](https://mastra.ai/reference/memory/memory-class) instance in your agent:
|
|
52
52
|
|
|
53
53
|
```typescript
|
|
54
54
|
import { Memory } from '@mastra/memory'
|
|
@@ -66,7 +66,7 @@ export const agent = new Agent({
|
|
|
66
66
|
|
|
67
67
|
When you call the agent, messages are automatically saved to the database. You can specify a `threadId`, `resourceId`, and optional `metadata`:
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
**.generate()**:
|
|
70
70
|
|
|
71
71
|
```typescript
|
|
72
72
|
await agent.generate('Hello', {
|
|
@@ -81,7 +81,7 @@ await agent.generate('Hello', {
|
|
|
81
81
|
})
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
**.stream()**:
|
|
85
85
|
|
|
86
86
|
```typescript
|
|
87
87
|
await agent.stream('Hello', {
|
|
@@ -98,17 +98,19 @@ await agent.stream('Hello', {
|
|
|
98
98
|
|
|
99
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
100
|
|
|
101
|
-
|
|
101
|
+
You can use this history in two ways:
|
|
102
102
|
|
|
103
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
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
105
|
|
|
106
|
-
|
|
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
|
|
107
109
|
|
|
108
110
|
To access memory functions for querying, cloning, or deleting threads and messages, call `getMemory()` on an agent:
|
|
109
111
|
|
|
110
112
|
```typescript
|
|
111
|
-
const agent = mastra.
|
|
113
|
+
const agent = mastra.getAgentById('test-agent')
|
|
112
114
|
const memory = await agent.getMemory()
|
|
113
115
|
```
|
|
114
116
|
|
|
@@ -118,7 +120,7 @@ The `Memory` instance gives you access to functions for listing threads, recalli
|
|
|
118
120
|
|
|
119
121
|
Use these methods to fetch threads and messages for displaying conversation history in your UI or for custom memory retrieval logic.
|
|
120
122
|
|
|
121
|
-
> **Warning:** The memory system
|
|
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.
|
|
122
124
|
|
|
123
125
|
### Threads
|
|
124
126
|
|