@mastra/libsql 1.0.0-beta.9 → 1.0.0
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 +1164 -0
- package/dist/docs/README.md +39 -0
- package/dist/docs/SKILL.md +40 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/agents/01-agent-memory.md +166 -0
- package/dist/docs/agents/02-networks.md +292 -0
- package/dist/docs/agents/03-agent-approval.md +377 -0
- package/dist/docs/agents/04-network-approval.md +274 -0
- package/dist/docs/core/01-reference.md +151 -0
- package/dist/docs/guides/01-ai-sdk.md +141 -0
- package/dist/docs/memory/01-overview.md +76 -0
- package/dist/docs/memory/02-storage.md +233 -0
- package/dist/docs/memory/03-working-memory.md +390 -0
- package/dist/docs/memory/04-semantic-recall.md +233 -0
- package/dist/docs/memory/05-memory-processors.md +318 -0
- package/dist/docs/memory/06-reference.md +133 -0
- package/dist/docs/observability/01-overview.md +64 -0
- package/dist/docs/observability/02-default.md +177 -0
- package/dist/docs/rag/01-retrieval.md +548 -0
- package/dist/docs/storage/01-reference.md +542 -0
- package/dist/docs/vectors/01-reference.md +213 -0
- package/dist/docs/workflows/01-snapshots.md +240 -0
- package/dist/index.cjs +546 -107
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +543 -109
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +42 -1
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/db/utils.d.ts +16 -1
- package/dist/storage/db/utils.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +3 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +23 -0
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +0 -1
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +1 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +10 -4
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +6 -2
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/sql-builder.d.ts.map +1 -1
- package/package.json +9 -8
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
> Learn how to require approvals, suspend tool execution, and automatically resume suspended tools while keeping humans in control of agent workflows.
|
|
2
|
+
|
|
3
|
+
# Agent Approval
|
|
4
|
+
|
|
5
|
+
Agents sometimes require the same [human-in-the-loop](https://mastra.ai/docs/v1/workflows/human-in-the-loop) oversight used in workflows when calling tools that handle sensitive operations, like deleting resources or performing running long processes. With agent approval you can suspend a tool call and provide feedback to the user, or approve or decline a tool call based on targeted application conditions.
|
|
6
|
+
|
|
7
|
+
## Tool call approval
|
|
8
|
+
|
|
9
|
+
Tool call approval can be enabled at the agent level and apply to every tool the agent uses, or at the tool level providing more granular control over individual tool calls.
|
|
10
|
+
|
|
11
|
+
### Storage
|
|
12
|
+
|
|
13
|
+
Agent approval uses a snapshot to capture the state of the request. Ensure you've enabled a storage provider in your main Mastra instance. If storage isn't enabled you'll see an error relating to snapshot not found.
|
|
14
|
+
|
|
15
|
+
```typescript title="src/mastra/index.ts"
|
|
16
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
17
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
18
|
+
|
|
19
|
+
export const mastra = new Mastra({
|
|
20
|
+
storage: new LibSQLStore({
|
|
21
|
+
id: "mastra-storage",
|
|
22
|
+
url: ":memory:"
|
|
23
|
+
})
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Agent-level approval
|
|
28
|
+
|
|
29
|
+
When calling an agent using `.stream()` set `requireToolApproval` to `true` which will prevent the agent from calling any of the tools defined in its configuration.
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
const stream = await agent.stream("What's the weather in London?", {
|
|
33
|
+
requireToolApproval: true
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Approving tool calls
|
|
38
|
+
|
|
39
|
+
To approve a tool call, access `approveToolCall` from the `agent`, passing in the `runId` of the stream. This will let the agent know its now OK to call its tools.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
const handleApproval = async () => {
|
|
43
|
+
const approvedStream = await agent.approveToolCall({ runId: stream.runId });
|
|
44
|
+
|
|
45
|
+
for await (const chunk of approvedStream.textStream) {
|
|
46
|
+
process.stdout.write(chunk);
|
|
47
|
+
}
|
|
48
|
+
process.stdout.write("\n");
|
|
49
|
+
};
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Declining tool calls
|
|
53
|
+
|
|
54
|
+
To decline a tool call, access the `declineToolCall` from the `agent`. You will see the streamed response from the agent, but it won't call its tools.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
const handleDecline = async () => {
|
|
58
|
+
const declinedStream = await agent.declineToolCall({ runId: stream.runId });
|
|
59
|
+
|
|
60
|
+
for await (const chunk of declinedStream.textStream) {
|
|
61
|
+
process.stdout.write(chunk);
|
|
62
|
+
}
|
|
63
|
+
process.stdout.write("\n");
|
|
64
|
+
};
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Tool approval with generate()
|
|
68
|
+
|
|
69
|
+
Tool approval also works with the `generate()` method for non-streaming use cases. When using `generate()` with `requireToolApproval: true`, the method returns immediately when a tool requires approval instead of executing it.
|
|
70
|
+
|
|
71
|
+
### How it works
|
|
72
|
+
|
|
73
|
+
When a tool requires approval during a `generate()` call, the response includes:
|
|
74
|
+
|
|
75
|
+
- `finishReason: 'suspended'` - indicates the agent is waiting for approval
|
|
76
|
+
- `suspendPayload` - contains tool call details (`toolCallId`, `toolName`, `args`)
|
|
77
|
+
- `runId` - needed to approve or decline the tool call
|
|
78
|
+
|
|
79
|
+
### Approving tool calls
|
|
80
|
+
|
|
81
|
+
To approve a tool call with `generate()`, use the `approveToolCallGenerate` method:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
const output = await agent.generate("Find user John", {
|
|
85
|
+
requireToolApproval: true,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
if (output.finishReason === "suspended") {
|
|
89
|
+
console.log("Tool requires approval:", output.suspendPayload.toolName);
|
|
90
|
+
console.log("Arguments:", output.suspendPayload.args);
|
|
91
|
+
|
|
92
|
+
// Approve the tool call and get the final result
|
|
93
|
+
const result = await agent.approveToolCallGenerate({
|
|
94
|
+
runId: output.runId,
|
|
95
|
+
toolCallId: output.suspendPayload.toolCallId,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
console.log("Final result:", result.text);
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Declining tool calls
|
|
103
|
+
|
|
104
|
+
To decline a tool call, use the `declineToolCallGenerate` method:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
if (output.finishReason === "suspended") {
|
|
108
|
+
const result = await agent.declineToolCallGenerate({
|
|
109
|
+
runId: output.runId,
|
|
110
|
+
toolCallId: output.suspendPayload.toolCallId,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Agent will respond acknowledging the declined tool
|
|
114
|
+
console.log(result.text);
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Stream vs Generate comparison
|
|
119
|
+
|
|
120
|
+
| Aspect | `stream()` | `generate()` |
|
|
121
|
+
|--------|-----------|--------------|
|
|
122
|
+
| Response type | Streaming chunks | Complete response |
|
|
123
|
+
| Approval detection | `tool-call-approval` chunk | `finishReason: 'suspended'` |
|
|
124
|
+
| Approve method | `approveToolCall({ runId })` | `approveToolCallGenerate({ runId, toolCallId })` |
|
|
125
|
+
| Decline method | `declineToolCall({ runId })` | `declineToolCallGenerate({ runId, toolCallId })` |
|
|
126
|
+
| Result | Stream to iterate | Full output object |
|
|
127
|
+
|
|
128
|
+
## Tool-level approval
|
|
129
|
+
|
|
130
|
+
There are two types of tool call approval. The first uses `requireApproval`, which is a property on the tool definition, while `requireToolApproval` is a parameter passed to `agent.stream()`. The second uses `suspend` and lets the agent provide context or confirmation prompts so the user can decide whether the tool call should continue.
|
|
131
|
+
|
|
132
|
+
### Tool approval using `requireToolApproval`
|
|
133
|
+
|
|
134
|
+
In this approach, `requireApproval` is configured on the tool definition (shown below) rather than on the agent.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
export const testTool = createTool({
|
|
138
|
+
id: "test-tool",
|
|
139
|
+
description: "Fetches weather for a location",
|
|
140
|
+
inputSchema: z.object({
|
|
141
|
+
location: z.string()
|
|
142
|
+
}),
|
|
143
|
+
outputSchema: z.object({
|
|
144
|
+
weather: z.string()
|
|
145
|
+
}),
|
|
146
|
+
resumeSchema: z.object({
|
|
147
|
+
approved: z.boolean()
|
|
148
|
+
}),
|
|
149
|
+
execute: async (inputData) => {
|
|
150
|
+
const response = await fetch(`https://wttr.in/${inputData.location}?format=3`);
|
|
151
|
+
const weather = await response.text();
|
|
152
|
+
|
|
153
|
+
return { weather };
|
|
154
|
+
},
|
|
155
|
+
requireApproval: true
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
When `requireApproval` is true for a tool, the stream will include chunks of type `tool-call-approval` to indicate that the call is paused. To continue the call, invoke `resumeStream` with the required `resumeSchema` and the `runId`.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
const stream = await agent.stream("What's the weather in London?");
|
|
163
|
+
|
|
164
|
+
for await (const chunk of stream.fullStream) {
|
|
165
|
+
if (chunk.type === "tool-call-approval") {
|
|
166
|
+
console.log("Approval required.");
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const handleResume = async () => {
|
|
171
|
+
const resumedStream = await agent.resumeStream({ approved: true }, { runId: stream.runId });
|
|
172
|
+
|
|
173
|
+
for await (const chunk of resumedStream.textStream) {
|
|
174
|
+
process.stdout.write(chunk);
|
|
175
|
+
}
|
|
176
|
+
process.stdout.write("\n");
|
|
177
|
+
};
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Tool approval using `suspend`
|
|
181
|
+
|
|
182
|
+
With this approach, neither the agent nor the tool uses `requireApproval`. Instead, the tool implementation calls `suspend` to pause execution and return context or confirmation prompts to the user.
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
export const testToolB = createTool({
|
|
186
|
+
id: "test-tool-b",
|
|
187
|
+
description: "Fetches weather for a location",
|
|
188
|
+
inputSchema: z.object({
|
|
189
|
+
location: z.string()
|
|
190
|
+
}),
|
|
191
|
+
outputSchema: z.object({
|
|
192
|
+
weather: z.string()
|
|
193
|
+
}),
|
|
194
|
+
resumeSchema: z.object({
|
|
195
|
+
approved: z.boolean()
|
|
196
|
+
}),
|
|
197
|
+
suspendSchema: z.object({
|
|
198
|
+
reason: z.string()
|
|
199
|
+
}),
|
|
200
|
+
execute: async (inputData, context) => {
|
|
201
|
+
const { resumeData: { approved } = {}, suspend } = context?.agent ?? {};
|
|
202
|
+
|
|
203
|
+
if (!approved) {
|
|
204
|
+
return suspend?.({ reason: "Approval required." });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const response = await fetch(`https://wttr.in/${inputData.location}?format=3`);
|
|
208
|
+
const weather = await response.text();
|
|
209
|
+
|
|
210
|
+
return { weather };
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
With this approach the stream will include a `tool-call-suspended` chunk, and the `suspendPayload` will contain the `reason` defined by the tool's `suspendSchema`. To continue the call, invoke `resumeStream` with the required `resumeSchema` and the `runId`.
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
const stream = await agent.stream("What's the weather in London?");
|
|
219
|
+
|
|
220
|
+
for await (const chunk of stream.fullStream) {
|
|
221
|
+
if (chunk.type === "tool-call-suspended") {
|
|
222
|
+
console.log(chunk.payload.suspendPayload);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const handleResume = async () => {
|
|
227
|
+
const resumedStream = await agent.resumeStream({ approved: true }, { runId: stream.runId });
|
|
228
|
+
|
|
229
|
+
for await (const chunk of resumedStream.textStream) {
|
|
230
|
+
process.stdout.write(chunk);
|
|
231
|
+
}
|
|
232
|
+
process.stdout.write("\n");
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Automatic tool resumption
|
|
238
|
+
|
|
239
|
+
When using tools that call `suspend()`, you can enable automatic resumption so the agent resumes suspended tools based on the user's next message. This creates a conversational flow where users provide the required information naturally, without your application needing to call `resumeStream()` explicitly.
|
|
240
|
+
|
|
241
|
+
### Enabling auto-resume
|
|
242
|
+
|
|
243
|
+
Set `autoResumeSuspendedTools` to `true` in the agent's default options or when calling `stream()`:
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import { Agent } from "@mastra/core/agent";
|
|
247
|
+
import { Memory } from "@mastra/memory";
|
|
248
|
+
|
|
249
|
+
// Option 1: In agent configuration
|
|
250
|
+
const agent = new Agent({
|
|
251
|
+
id: "my-agent",
|
|
252
|
+
name: "My Agent",
|
|
253
|
+
instructions: "You are a helpful assistant",
|
|
254
|
+
model: "openai/gpt-4o-mini",
|
|
255
|
+
tools: { weatherTool },
|
|
256
|
+
memory: new Memory(),
|
|
257
|
+
defaultOptions: {
|
|
258
|
+
autoResumeSuspendedTools: true,
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// Option 2: Per-request
|
|
263
|
+
const stream = await agent.stream("What's the weather?", {
|
|
264
|
+
autoResumeSuspendedTools: true,
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### How it works
|
|
269
|
+
|
|
270
|
+
When `autoResumeSuspendedTools` is enabled:
|
|
271
|
+
|
|
272
|
+
1. A tool suspends execution by calling `suspend()` with a payload (e.g., requesting more information)
|
|
273
|
+
2. The suspension is persisted to memory along with the conversation
|
|
274
|
+
3. When the user sends their next message on the same thread, the agent:
|
|
275
|
+
- Detects the suspended tool from message history
|
|
276
|
+
- Extracts `resumeData` from the user's message based on the tool's `resumeSchema`
|
|
277
|
+
- Automatically resumes the tool with the extracted data
|
|
278
|
+
|
|
279
|
+
### Example
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
import { createTool } from "@mastra/core/tools";
|
|
283
|
+
import { z } from "zod";
|
|
284
|
+
|
|
285
|
+
export const weatherTool = createTool({
|
|
286
|
+
id: "weather-info",
|
|
287
|
+
description: "Fetches weather information for a city",
|
|
288
|
+
suspendSchema: z.object({
|
|
289
|
+
message: z.string(),
|
|
290
|
+
}),
|
|
291
|
+
resumeSchema: z.object({
|
|
292
|
+
city: z.string(),
|
|
293
|
+
}),
|
|
294
|
+
execute: async (_inputData, context) => {
|
|
295
|
+
// Check if this is a resume with data
|
|
296
|
+
if (!context?.agent?.resumeData) {
|
|
297
|
+
// First call - suspend and ask for the city
|
|
298
|
+
return context?.agent?.suspend({
|
|
299
|
+
message: "What city do you want to know the weather for?",
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Resume call - city was extracted from user's message
|
|
304
|
+
const { city } = context.agent.resumeData;
|
|
305
|
+
const response = await fetch(`https://wttr.in/${city}?format=3`);
|
|
306
|
+
const weather = await response.text();
|
|
307
|
+
|
|
308
|
+
return { city, weather };
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
const agent = new Agent({
|
|
313
|
+
id: "my-agent",
|
|
314
|
+
name: "My Agent",
|
|
315
|
+
instructions: "You are a helpful assistant",
|
|
316
|
+
model: "openai/gpt-4o-mini",
|
|
317
|
+
tools: { weatherTool },
|
|
318
|
+
memory: new Memory(),
|
|
319
|
+
defaultOptions: {
|
|
320
|
+
autoResumeSuspendedTools: true,
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
const stream = await agent.stream("What's the weather like?");
|
|
325
|
+
|
|
326
|
+
for await (const chunk of stream.fullStream) {
|
|
327
|
+
if (chunk.type === "tool-call-suspended") {
|
|
328
|
+
console.log(chunk.payload.suspendPayload);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const handleResume = async () => {
|
|
333
|
+
const resumedStream = await agent.stream("San Francisco");
|
|
334
|
+
|
|
335
|
+
for await (const chunk of resumedStream.textStream) {
|
|
336
|
+
process.stdout.write(chunk);
|
|
337
|
+
}
|
|
338
|
+
process.stdout.write("\n");
|
|
339
|
+
};
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
**Conversation flow:**
|
|
343
|
+
|
|
344
|
+
```
|
|
345
|
+
User: "What's the weather like?"
|
|
346
|
+
Agent: "What city do you want to know the weather for?"
|
|
347
|
+
|
|
348
|
+
User: "San Francisco"
|
|
349
|
+
Agent: "The weather in San Francisco is: San Francisco: ☀️ +72°F"
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
The second message automatically resumes the suspended tool - the agent extracts `{ city: "San Francisco" }` from the user's message and passes it as `resumeData`.
|
|
353
|
+
|
|
354
|
+
### Requirements
|
|
355
|
+
|
|
356
|
+
For automatic tool resumption to work:
|
|
357
|
+
|
|
358
|
+
- **Memory configured**: The agent needs memory to track suspended tools across messages
|
|
359
|
+
- **Same thread**: The follow-up message must use the same memory thread and resource identifiers
|
|
360
|
+
- **`resumeSchema` defined**: The tool must define a `resumeSchema` so the agent knows what data structure to extract from the user's message
|
|
361
|
+
|
|
362
|
+
### Manual vs automatic resumption
|
|
363
|
+
|
|
364
|
+
| Approach | Use case |
|
|
365
|
+
|----------|----------|
|
|
366
|
+
| Manual (`resumeStream()`) | Programmatic control, webhooks, button clicks, external triggers |
|
|
367
|
+
| Automatic (`autoResumeSuspendedTools`) | Conversational flows where users provide resume data in natural language |
|
|
368
|
+
|
|
369
|
+
Both approaches work with the same tool definitions. Automatic resumption triggers only when suspended tools exist in the message history and the user sends a new message on the same thread.
|
|
370
|
+
|
|
371
|
+
## Related
|
|
372
|
+
|
|
373
|
+
- [Using Tools](./using-tools)
|
|
374
|
+
- [Agent Overview](./overview)
|
|
375
|
+
- [Tools Overview](../mcp/overview)
|
|
376
|
+
- [Agent Memory](./agent-memory)
|
|
377
|
+
- [Request Context](https://mastra.ai/docs/v1/server/request-context)
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
> Learn how to require approvals, suspend execution, and resume suspended networks while keeping humans in control of agent network workflows.
|
|
2
|
+
|
|
3
|
+
# Network Approval
|
|
4
|
+
|
|
5
|
+
Agent networks can require the same [human-in-the-loop](https://mastra.ai/docs/v1/workflows/human-in-the-loop) oversight used in individual agents and workflows. When a tool, sub-agent, or workflow within a network requires approval or suspends execution, the network pauses and emits events that allow your application to collect user input before resuming.
|
|
6
|
+
|
|
7
|
+
## Storage
|
|
8
|
+
|
|
9
|
+
Network approval uses snapshots to capture execution state. Ensure you've enabled a storage provider in your Mastra instance. If storage isn't enabled you'll see an error relating to snapshot not found.
|
|
10
|
+
|
|
11
|
+
```typescript title="src/mastra/index.ts"
|
|
12
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
13
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
14
|
+
|
|
15
|
+
export const mastra = new Mastra({
|
|
16
|
+
storage: new LibSQLStore({
|
|
17
|
+
id: "mastra-storage",
|
|
18
|
+
url: ":memory:"
|
|
19
|
+
})
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Approving network tool calls
|
|
24
|
+
|
|
25
|
+
When a tool within a network has `requireApproval: true`, the network stream emits an `agent-execution-approval` chunk and pauses. To allow the tool to execute, call `approveNetworkToolCall` with the `runId`.
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
const stream = await routingAgent.network("Process this query", {
|
|
29
|
+
memory: {
|
|
30
|
+
thread: "user-123",
|
|
31
|
+
resource: "my-app"
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
let runId: string;
|
|
36
|
+
|
|
37
|
+
for await (const chunk of stream) {
|
|
38
|
+
runId = stream.runId;
|
|
39
|
+
// if the requirApproval is in a tool inside a subAgent or the subAgent has requireToolApproval set to true
|
|
40
|
+
if (chunk.type === "agent-execution-approval") {
|
|
41
|
+
console.log("Tool requires approval:", chunk.payload);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// if the requirApproval is in a tool directly in the network agent
|
|
45
|
+
if (chunk.type === "tool-execution-approval") {
|
|
46
|
+
console.log("Tool requires approval:", chunk.payload);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Approve and resume execution
|
|
51
|
+
const approvedStream = await routingAgent.approveNetworkToolCall({
|
|
52
|
+
runId,
|
|
53
|
+
memory: {
|
|
54
|
+
thread: "user-123",
|
|
55
|
+
resource: "my-app"
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
for await (const chunk of approvedStream) {
|
|
60
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
61
|
+
console.log(chunk.payload.result);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Declining network tool calls
|
|
67
|
+
|
|
68
|
+
To decline a pending tool call and prevent execution, call `declineNetworkToolCall`. The network continues without executing the tool.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
const declinedStream = await routingAgent.declineNetworkToolCall({
|
|
72
|
+
runId,
|
|
73
|
+
memory: {
|
|
74
|
+
thread: "user-123",
|
|
75
|
+
resource: "my-app"
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
for await (const chunk of declinedStream) {
|
|
80
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
81
|
+
console.log(chunk.payload.result);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Resuming suspended networks
|
|
87
|
+
|
|
88
|
+
When a primitive in the network calls `suspend()`, the stream emits an `agent-execution-suspended`/`tool-execution-suspended`/`workflow-execution-suspended` chunk with a `suspendPayload` containing context from the primitive. Use `resumeNetwork` to provide the data requested by the primitive and continue execution.
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { createTool } from "@mastra/core/tools";
|
|
92
|
+
import { z } from "zod";
|
|
93
|
+
|
|
94
|
+
const confirmationTool = createTool({
|
|
95
|
+
id: "confirmation-tool",
|
|
96
|
+
description: "Requests user confirmation before proceeding",
|
|
97
|
+
inputSchema: z.object({
|
|
98
|
+
action: z.string()
|
|
99
|
+
}),
|
|
100
|
+
outputSchema: z.object({
|
|
101
|
+
confirmed: z.boolean(),
|
|
102
|
+
action: z.string()
|
|
103
|
+
}),
|
|
104
|
+
suspendSchema: z.object({
|
|
105
|
+
message: z.string(),
|
|
106
|
+
action: z.string()
|
|
107
|
+
}),
|
|
108
|
+
resumeSchema: z.object({
|
|
109
|
+
confirmed: z.boolean()
|
|
110
|
+
}),
|
|
111
|
+
execute: async (inputData, context) => {
|
|
112
|
+
const { resumeData, suspend } = context?.agent ?? {};
|
|
113
|
+
|
|
114
|
+
if (!resumeData?.confirmed) {
|
|
115
|
+
return suspend?.({
|
|
116
|
+
message: `Please confirm: ${inputData.action}`,
|
|
117
|
+
action: inputData.action
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return { confirmed: true, action: inputData.action };
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Handle the suspension and resume with user-provided data:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
const stream = await routingAgent.network("Delete the old records", {
|
|
130
|
+
memory: {
|
|
131
|
+
thread: "user-123",
|
|
132
|
+
resource: "my-app"
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
for await (const chunk of stream) {
|
|
137
|
+
if (chunk.type === "workflow-execution-suspended") {
|
|
138
|
+
console.log(chunk.payload.suspendPayload);
|
|
139
|
+
// { message: "Please confirm: delete old records", action: "delete old records" }
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Resume with user confirmation
|
|
144
|
+
const resumedStream = await routingAgent.resumeNetwork(
|
|
145
|
+
{ confirmed: true },
|
|
146
|
+
{
|
|
147
|
+
runId: stream.runId,
|
|
148
|
+
memory: {
|
|
149
|
+
thread: "user-123",
|
|
150
|
+
resource: "my-app"
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
for await (const chunk of resumedStream) {
|
|
156
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
157
|
+
console.log(chunk.payload.result);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Automatic primitive resumption
|
|
163
|
+
|
|
164
|
+
When using primitives that call `suspend()`, you can enable automatic resumption 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.
|
|
165
|
+
|
|
166
|
+
### Enabling auto-resume
|
|
167
|
+
|
|
168
|
+
Set `autoResumeSuspendedTools` to `true` in the agent's `defaultNetworkOptions` or when calling `network()`:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { Agent } from "@mastra/core/agent";
|
|
172
|
+
import { Memory } from "@mastra/memory";
|
|
173
|
+
|
|
174
|
+
// Option 1: In agent configuration
|
|
175
|
+
const routingAgent = new Agent({
|
|
176
|
+
id: "routing-agent",
|
|
177
|
+
name: "Routing Agent",
|
|
178
|
+
instructions: "You coordinate tasks across multiple agents",
|
|
179
|
+
model: "openai/gpt-4o-mini",
|
|
180
|
+
tools: { confirmationTool },
|
|
181
|
+
memory: new Memory(),
|
|
182
|
+
defaultNetworkOptions: {
|
|
183
|
+
autoResumeSuspendedTools: true,
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// Option 2: Per-request
|
|
188
|
+
const stream = await routingAgent.network("Process this request", {
|
|
189
|
+
autoResumeSuspendedTools: true,
|
|
190
|
+
memory: {
|
|
191
|
+
thread: "user-123",
|
|
192
|
+
resource: "my-app"
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### How it works
|
|
198
|
+
|
|
199
|
+
When `autoResumeSuspendedTools` is enabled:
|
|
200
|
+
|
|
201
|
+
1. A primitive suspends execution by calling `suspend()` with a payload
|
|
202
|
+
2. The suspension is persisted to memory along with the conversation
|
|
203
|
+
3. When the user sends their next message on the same thread, the network:
|
|
204
|
+
- Detects the suspended primitive from message history
|
|
205
|
+
- Extracts `resumeData` from the user's message based on the tool's `resumeSchema`
|
|
206
|
+
- Automatically resumes the primitive with the extracted data
|
|
207
|
+
|
|
208
|
+
### Example
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
const stream = await routingAgent.network("Delete the old records", {
|
|
212
|
+
autoResumeSuspendedTools: true,
|
|
213
|
+
memory: {
|
|
214
|
+
thread: "user-123",
|
|
215
|
+
resource: "my-app"
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
for await (const chunk of stream) {
|
|
220
|
+
if (chunk.type === "workflow-execution-suspended") {
|
|
221
|
+
console.log(chunk.payload.suspendPayload);
|
|
222
|
+
// { message: "Please confirm: delete old records", action: "delete old records" }
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// User provides confirmation in their next message
|
|
227
|
+
const resumedStream = await routingAgent.network("Yes, confirmed", {
|
|
228
|
+
autoResumeSuspendedTools: true,
|
|
229
|
+
memory: {
|
|
230
|
+
thread: "user-123",
|
|
231
|
+
resource: "my-app"
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
for await (const chunk of resumedStream) {
|
|
236
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
237
|
+
console.log(chunk.payload.result);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
**Conversation flow:**
|
|
243
|
+
|
|
244
|
+
```
|
|
245
|
+
User: "Delete the old records"
|
|
246
|
+
Agent: "Please confirm: delete old records"
|
|
247
|
+
|
|
248
|
+
User: "Yes, confirmed"
|
|
249
|
+
Agent: "Records deleted successfully"
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Requirements
|
|
253
|
+
|
|
254
|
+
For automatic tool resumption to work:
|
|
255
|
+
|
|
256
|
+
- **Memory configured**: The agent needs memory to track suspended tools across messages
|
|
257
|
+
- **Same thread**: The follow-up message must use the same memory thread and resource identifiers
|
|
258
|
+
- **`resumeSchema` defined**: The tool (either directly in the network agent or in a subAgent) / workflow (step that gets suspended) must define a `resumeSchema` so the agent knows what data to extract from the user's message
|
|
259
|
+
|
|
260
|
+
### Manual vs automatic resumption
|
|
261
|
+
|
|
262
|
+
| Approach | Use case |
|
|
263
|
+
|----------|----------|
|
|
264
|
+
| Manual (`resumeNetwork()`) | Programmatic control, webhooks, button clicks, external triggers |
|
|
265
|
+
| Automatic (`autoResumeSuspendedTools`) | Conversational flows where users provide resume data in natural language |
|
|
266
|
+
|
|
267
|
+
Both approaches work with the same tool definitions. Automatic resumption triggers only when suspended tools exist in the message history and the user sends a new message on the same thread.
|
|
268
|
+
|
|
269
|
+
## Related
|
|
270
|
+
|
|
271
|
+
- [Agent Networks](./networks)
|
|
272
|
+
- [Agent Approval](./agent-approval)
|
|
273
|
+
- [Human-in-the-Loop](https://mastra.ai/docs/v1/workflows/human-in-the-loop)
|
|
274
|
+
- [Agent Memory](./agent-memory)
|