@mastra/libsql 1.0.0-beta.11 → 1.0.0-beta.13
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 +154 -0
- package/dist/docs/README.md +2 -2
- package/dist/docs/SKILL.md +2 -2
- package/dist/docs/SOURCE_MAP.json +1 -1
- package/dist/docs/agents/01-agent-memory.md +6 -0
- package/dist/docs/agents/02-networks.md +56 -0
- package/dist/docs/agents/03-agent-approval.md +66 -6
- package/dist/docs/agents/04-network-approval.md +274 -0
- package/dist/docs/core/01-reference.md +7 -8
- package/dist/docs/memory/02-storage.md +77 -25
- package/dist/docs/memory/03-working-memory.md +10 -6
- package/dist/docs/memory/04-semantic-recall.md +2 -4
- package/dist/docs/memory/05-memory-processors.md +2 -3
- package/dist/docs/memory/06-reference.md +3 -5
- package/dist/docs/rag/01-retrieval.md +5 -6
- package/dist/docs/storage/01-reference.md +106 -15
- package/dist/docs/vectors/01-reference.md +3 -5
- package/dist/index.cjs +165 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +165 -71
- package/dist/index.js.map +1 -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 +2 -2
- package/dist/storage/domains/memory/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/vector/index.d.ts +6 -2
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,159 @@
|
|
|
1
1
|
# @mastra/libsql
|
|
2
2
|
|
|
3
|
+
## 1.0.0-beta.13
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Added new `listThreads` method for flexible thread filtering across all storage adapters. ([#11832](https://github.com/mastra-ai/mastra/pull/11832))
|
|
8
|
+
|
|
9
|
+
**New Features**
|
|
10
|
+
- Filter threads by `resourceId`, `metadata`, or both (with AND logic for metadata key-value pairs)
|
|
11
|
+
- All filter parameters are optional, allowing you to list all threads or filter as needed
|
|
12
|
+
- Full pagination and sorting support
|
|
13
|
+
|
|
14
|
+
**Example Usage**
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
// List all threads
|
|
18
|
+
const allThreads = await memory.listThreads({});
|
|
19
|
+
|
|
20
|
+
// Filter by resourceId only
|
|
21
|
+
const userThreads = await memory.listThreads({
|
|
22
|
+
filter: { resourceId: 'user-123' },
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Filter by metadata only
|
|
26
|
+
const supportThreads = await memory.listThreads({
|
|
27
|
+
filter: { metadata: { category: 'support' } },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Filter by both with pagination
|
|
31
|
+
const filteredThreads = await memory.listThreads({
|
|
32
|
+
filter: {
|
|
33
|
+
resourceId: 'user-123',
|
|
34
|
+
metadata: { priority: 'high', status: 'open' },
|
|
35
|
+
},
|
|
36
|
+
orderBy: { field: 'updatedAt', direction: 'DESC' },
|
|
37
|
+
page: 0,
|
|
38
|
+
perPage: 20,
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Security Improvements**
|
|
43
|
+
- Added validation to prevent SQL injection via malicious metadata keys
|
|
44
|
+
- Added pagination parameter validation to prevent integer overflow attacks
|
|
45
|
+
|
|
46
|
+
- Updated dependencies [[`ed3e3dd`](https://github.com/mastra-ai/mastra/commit/ed3e3ddec69d564fe2b125e083437f76331f1283), [`6833c69`](https://github.com/mastra-ai/mastra/commit/6833c69607418d257750bbcdd84638993d343539), [`47b1c16`](https://github.com/mastra-ai/mastra/commit/47b1c16a01c7ffb6765fe1e499b49092f8b7eba3), [`3a76a80`](https://github.com/mastra-ai/mastra/commit/3a76a80284cb71a0faa975abb3d4b2a9631e60cd), [`8538a0d`](https://github.com/mastra-ai/mastra/commit/8538a0d232619bf55dad7ddc2a8b0ca77c679a87), [`9312dcd`](https://github.com/mastra-ai/mastra/commit/9312dcd1c6f5b321929e7d382e763d95fdc030f5)]:
|
|
47
|
+
- @mastra/core@1.0.0-beta.25
|
|
48
|
+
|
|
49
|
+
## 1.0.0-beta.12
|
|
50
|
+
|
|
51
|
+
### Minor Changes
|
|
52
|
+
|
|
53
|
+
- Changed JSON columns from TEXT to JSONB in `mastra_threads` and `mastra_workflow_snapshot` tables. ([#11853](https://github.com/mastra-ai/mastra/pull/11853))
|
|
54
|
+
|
|
55
|
+
**Why this change?**
|
|
56
|
+
|
|
57
|
+
These were the last remaining columns storing JSON as TEXT. This change aligns them with other tables that already use JSONB, enabling native JSON operators and improved performance. See [#8978](https://github.com/mastra-ai/mastra/issues/8978) for details.
|
|
58
|
+
|
|
59
|
+
**Columns Changed:**
|
|
60
|
+
- `mastra_threads.metadata` - Thread metadata
|
|
61
|
+
- `mastra_workflow_snapshot.snapshot` - Workflow run state
|
|
62
|
+
|
|
63
|
+
**PostgreSQL**
|
|
64
|
+
|
|
65
|
+
Migration Required - PostgreSQL enforces column types, so existing tables must be migrated. Note: Migration will fail if existing column values contain invalid JSON.
|
|
66
|
+
|
|
67
|
+
```sql
|
|
68
|
+
ALTER TABLE mastra_threads
|
|
69
|
+
ALTER COLUMN metadata TYPE jsonb
|
|
70
|
+
USING metadata::jsonb;
|
|
71
|
+
|
|
72
|
+
ALTER TABLE mastra_workflow_snapshot
|
|
73
|
+
ALTER COLUMN snapshot TYPE jsonb
|
|
74
|
+
USING snapshot::jsonb;
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**LibSQL**
|
|
78
|
+
|
|
79
|
+
No Migration Required - LibSQL now uses native SQLite JSONB format (added in SQLite 3.45) for ~3x performance improvement on JSON operations. The changes are fully backwards compatible:
|
|
80
|
+
- Existing TEXT JSON data continues to work
|
|
81
|
+
- New data is stored in binary JSONB format
|
|
82
|
+
- Both formats can coexist in the same table
|
|
83
|
+
- All JSON functions (`json_extract`, etc.) work on both formats
|
|
84
|
+
|
|
85
|
+
New installations automatically use JSONB. Existing applications continue to work without any changes.
|
|
86
|
+
|
|
87
|
+
- Aligned vector store configuration with underlying library APIs, giving you access to all library options directly. ([#11742](https://github.com/mastra-ai/mastra/pull/11742))
|
|
88
|
+
|
|
89
|
+
**Why this change?**
|
|
90
|
+
|
|
91
|
+
Previously, each vector store defined its own configuration types that only exposed a subset of the underlying library's options. This meant users couldn't access advanced features like authentication, SSL, compression, or custom headers without creating their own client instances. Now, the configuration types extend the library types directly, so all options are available.
|
|
92
|
+
|
|
93
|
+
**@mastra/libsql** (Breaking)
|
|
94
|
+
|
|
95
|
+
Renamed `connectionUrl` to `url` to match the `@libsql/client` API and align with LibSQLStorage.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// Before
|
|
99
|
+
new LibSQLVector({ id: 'my-vector', connectionUrl: 'file:./db.sqlite' });
|
|
100
|
+
|
|
101
|
+
// After
|
|
102
|
+
new LibSQLVector({ id: 'my-vector', url: 'file:./db.sqlite' });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**@mastra/opensearch** (Breaking)
|
|
106
|
+
|
|
107
|
+
Renamed `url` to `node` and added support for all OpenSearch `ClientOptions` including authentication, SSL, and compression.
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Before
|
|
111
|
+
new OpenSearchVector({ id: 'my-vector', url: 'http://localhost:9200' });
|
|
112
|
+
|
|
113
|
+
// After
|
|
114
|
+
new OpenSearchVector({ id: 'my-vector', node: 'http://localhost:9200' });
|
|
115
|
+
|
|
116
|
+
// With authentication (now possible)
|
|
117
|
+
new OpenSearchVector({
|
|
118
|
+
id: 'my-vector',
|
|
119
|
+
node: 'https://localhost:9200',
|
|
120
|
+
auth: { username: 'admin', password: 'admin' },
|
|
121
|
+
ssl: { rejectUnauthorized: false },
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**@mastra/pinecone** (Breaking)
|
|
126
|
+
|
|
127
|
+
Removed `environment` parameter. Use `controllerHostUrl` instead (the actual Pinecone SDK field name). Added support for all `PineconeConfiguration` options.
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
// Before
|
|
131
|
+
new PineconeVector({ id: 'my-vector', apiKey: '...', environment: '...' });
|
|
132
|
+
|
|
133
|
+
// After
|
|
134
|
+
new PineconeVector({ id: 'my-vector', apiKey: '...' });
|
|
135
|
+
|
|
136
|
+
// With custom controller host (if needed)
|
|
137
|
+
new PineconeVector({ id: 'my-vector', apiKey: '...', controllerHostUrl: '...' });
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**@mastra/clickhouse**
|
|
141
|
+
|
|
142
|
+
Added support for all `ClickHouseClientConfigOptions` like `request_timeout`, `compression`, `keep_alive`, and `database`. Existing configurations continue to work unchanged.
|
|
143
|
+
|
|
144
|
+
**@mastra/cloudflare, @mastra/cloudflare-d1, @mastra/lance, @mastra/libsql, @mastra/mongodb, @mastra/pg, @mastra/upstash**
|
|
145
|
+
|
|
146
|
+
Improved logging by replacing `console.warn` with structured logger in workflow storage domains.
|
|
147
|
+
|
|
148
|
+
**@mastra/deployer-cloud**
|
|
149
|
+
|
|
150
|
+
Updated internal LibSQLVector configuration for compatibility with the new API.
|
|
151
|
+
|
|
152
|
+
### Patch Changes
|
|
153
|
+
|
|
154
|
+
- Updated dependencies [[`ebae12a`](https://github.com/mastra-ai/mastra/commit/ebae12a2dd0212e75478981053b148a2c246962d), [`c61a0a5`](https://github.com/mastra-ai/mastra/commit/c61a0a5de4904c88fd8b3718bc26d1be1c2ec6e7), [`69136e7`](https://github.com/mastra-ai/mastra/commit/69136e748e32f57297728a4e0f9a75988462f1a7), [`449aed2`](https://github.com/mastra-ai/mastra/commit/449aed2ba9d507b75bf93d427646ea94f734dfd1), [`eb648a2`](https://github.com/mastra-ai/mastra/commit/eb648a2cc1728f7678768dd70cd77619b448dab9), [`0131105`](https://github.com/mastra-ai/mastra/commit/0131105532e83bdcbb73352fc7d0879eebf140dc), [`9d5059e`](https://github.com/mastra-ai/mastra/commit/9d5059eae810829935fb08e81a9bb7ecd5b144a7), [`ef756c6`](https://github.com/mastra-ai/mastra/commit/ef756c65f82d16531c43f49a27290a416611e526), [`b00ccd3`](https://github.com/mastra-ai/mastra/commit/b00ccd325ebd5d9e37e34dd0a105caae67eb568f), [`3bdfa75`](https://github.com/mastra-ai/mastra/commit/3bdfa7507a91db66f176ba8221aa28dd546e464a), [`e770de9`](https://github.com/mastra-ai/mastra/commit/e770de941a287a49b1964d44db5a5763d19890a6), [`52e2716`](https://github.com/mastra-ai/mastra/commit/52e2716b42df6eff443de72360ae83e86ec23993), [`27b4040`](https://github.com/mastra-ai/mastra/commit/27b4040bfa1a95d92546f420a02a626b1419a1d6), [`610a70b`](https://github.com/mastra-ai/mastra/commit/610a70bdad282079f0c630e0d7bb284578f20151), [`8dc7f55`](https://github.com/mastra-ai/mastra/commit/8dc7f55900395771da851dc7d78d53ae84fe34ec), [`8379099`](https://github.com/mastra-ai/mastra/commit/8379099fc467af6bef54dd7f80c9bd75bf8bbddf), [`8c0ec25`](https://github.com/mastra-ai/mastra/commit/8c0ec25646c8a7df253ed1e5ff4863a0d3f1316c), [`ff4d9a6`](https://github.com/mastra-ai/mastra/commit/ff4d9a6704fc87b31a380a76ed22736fdedbba5a), [`69821ef`](https://github.com/mastra-ai/mastra/commit/69821ef806482e2c44e2197ac0b050c3fe3a5285), [`1ed5716`](https://github.com/mastra-ai/mastra/commit/1ed5716830867b3774c4a1b43cc0d82935f32b96), [`4186bdd`](https://github.com/mastra-ai/mastra/commit/4186bdd00731305726fa06adba0b076a1d50b49f), [`7aaf973`](https://github.com/mastra-ai/mastra/commit/7aaf973f83fbbe9521f1f9e7a4fd99b8de464617)]:
|
|
155
|
+
- @mastra/core@1.0.0-beta.22
|
|
156
|
+
|
|
3
157
|
## 1.0.0-beta.11
|
|
4
158
|
|
|
5
159
|
### Patch Changes
|
package/dist/docs/README.md
CHANGED
|
@@ -22,7 +22,7 @@ docs/
|
|
|
22
22
|
├── SKILL.md # Entry point
|
|
23
23
|
├── README.md # This file
|
|
24
24
|
├── SOURCE_MAP.json # Export index
|
|
25
|
-
├── agents/ (
|
|
25
|
+
├── agents/ (4 files)
|
|
26
26
|
├── core/ (3 files)
|
|
27
27
|
├── guides/ (1 files)
|
|
28
28
|
├── memory/ (6 files)
|
|
@@ -36,4 +36,4 @@ docs/
|
|
|
36
36
|
## Version
|
|
37
37
|
|
|
38
38
|
Package: @mastra/libsql
|
|
39
|
-
Version: 1.0.0-beta.
|
|
39
|
+
Version: 1.0.0-beta.13
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -5,7 +5,7 @@ description: Documentation for @mastra/libsql. Includes links to type definition
|
|
|
5
5
|
|
|
6
6
|
# @mastra/libsql Documentation
|
|
7
7
|
|
|
8
|
-
> **Version**: 1.0.0-beta.
|
|
8
|
+
> **Version**: 1.0.0-beta.13
|
|
9
9
|
> **Package**: @mastra/libsql
|
|
10
10
|
|
|
11
11
|
## Quick Navigation
|
|
@@ -29,7 +29,7 @@ See SOURCE_MAP.json for the complete list.
|
|
|
29
29
|
|
|
30
30
|
## Available Topics
|
|
31
31
|
|
|
32
|
-
- [Agents](agents/) -
|
|
32
|
+
- [Agents](agents/) - 4 file(s)
|
|
33
33
|
- [Core](core/) - 3 file(s)
|
|
34
34
|
- [Guides](guides/) - 1 file(s)
|
|
35
35
|
- [Memory](memory/) - 6 file(s)
|
|
@@ -90,6 +90,9 @@ export const memoryAgent = new Agent({
|
|
|
90
90
|
});
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
+
> **Mastra Cloud Store limitation**
|
|
94
|
+
Agent-level storage is not supported when using [Mastra Cloud Store](https://mastra.ai/docs/v1/mastra-cloud/deployment#using-mastra-cloud-store). If you use Mastra Cloud Store, configure storage on the Mastra instance instead. This limitation does not apply if you bring your own database.
|
|
95
|
+
|
|
93
96
|
## Message history
|
|
94
97
|
|
|
95
98
|
Include a `memory` object with both `resource` and `thread` to track message history during agent calls.
|
|
@@ -122,6 +125,9 @@ const response = await memoryAgent.generate("What's my favorite color?", {
|
|
|
122
125
|
});
|
|
123
126
|
```
|
|
124
127
|
|
|
128
|
+
> **Note:**
|
|
129
|
+
Each thread has an owner (`resourceId`) that cannot be changed after creation. Avoid reusing the same thread ID for threads with different owners, as this will cause errors when querying.
|
|
130
|
+
|
|
125
131
|
To learn more about memory see the [Memory](../memory/overview) documentation.
|
|
126
132
|
|
|
127
133
|
## Using `RequestContext`
|
|
@@ -228,6 +228,62 @@ tool-execution-end
|
|
|
228
228
|
network-execution-event-step-finish
|
|
229
229
|
```
|
|
230
230
|
|
|
231
|
+
## Structured output
|
|
232
|
+
|
|
233
|
+
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.
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
import { z } from "zod";
|
|
237
|
+
|
|
238
|
+
const resultSchema = z.object({
|
|
239
|
+
summary: z.string().describe("A brief summary of the findings"),
|
|
240
|
+
recommendations: z.array(z.string()).describe("List of recommendations"),
|
|
241
|
+
confidence: z.number().min(0).max(1).describe("Confidence score"),
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
const stream = await routingAgent.network("Research AI trends", {
|
|
245
|
+
structuredOutput: {
|
|
246
|
+
schema: resultSchema,
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Consume the stream
|
|
251
|
+
for await (const chunk of stream) {
|
|
252
|
+
if (chunk.type === "network-object") {
|
|
253
|
+
// Partial object during generation
|
|
254
|
+
console.log("Partial:", chunk.payload.object);
|
|
255
|
+
}
|
|
256
|
+
if (chunk.type === "network-object-result") {
|
|
257
|
+
// Final structured object
|
|
258
|
+
console.log("Final:", chunk.payload.object);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Get the typed result
|
|
263
|
+
const result = await stream.object;
|
|
264
|
+
console.log(result?.summary);
|
|
265
|
+
console.log(result?.recommendations);
|
|
266
|
+
console.log(result?.confidence);
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Streaming partial objects
|
|
270
|
+
|
|
271
|
+
For real-time updates during structured output generation, use `objectStream`:
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
const stream = await routingAgent.network("Analyze market data", {
|
|
275
|
+
structuredOutput: { schema: resultSchema },
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// Stream partial objects as they're generated
|
|
279
|
+
for await (const partial of stream.objectStream) {
|
|
280
|
+
console.log("Building result:", partial);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Get the final typed result
|
|
284
|
+
const final = await stream.object;
|
|
285
|
+
```
|
|
286
|
+
|
|
231
287
|
## Related
|
|
232
288
|
|
|
233
289
|
- [Agent Memory](./agent-memory)
|
|
@@ -64,6 +64,67 @@ const handleDecline = async () => {
|
|
|
64
64
|
};
|
|
65
65
|
```
|
|
66
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
|
+
|
|
67
128
|
## Tool-level approval
|
|
68
129
|
|
|
69
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.
|
|
@@ -85,8 +146,8 @@ export const testTool = createTool({
|
|
|
85
146
|
resumeSchema: z.object({
|
|
86
147
|
approved: z.boolean()
|
|
87
148
|
}),
|
|
88
|
-
execute: async (
|
|
89
|
-
const response = await fetch(`https://wttr.in/${location}?format=3`);
|
|
149
|
+
execute: async (inputData) => {
|
|
150
|
+
const response = await fetch(`https://wttr.in/${inputData.location}?format=3`);
|
|
90
151
|
const weather = await response.text();
|
|
91
152
|
|
|
92
153
|
return { weather };
|
|
@@ -121,7 +182,6 @@ const handleResume = async () => {
|
|
|
121
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.
|
|
122
183
|
|
|
123
184
|
```typescript
|
|
124
|
-
|
|
125
185
|
export const testToolB = createTool({
|
|
126
186
|
id: "test-tool-b",
|
|
127
187
|
description: "Fetches weather for a location",
|
|
@@ -137,14 +197,14 @@ export const testToolB = createTool({
|
|
|
137
197
|
suspendSchema: z.object({
|
|
138
198
|
reason: z.string()
|
|
139
199
|
}),
|
|
140
|
-
execute: async (
|
|
141
|
-
const { resumeData: { approved } = {}, suspend } = agent ?? {};
|
|
200
|
+
execute: async (inputData, context) => {
|
|
201
|
+
const { resumeData: { approved } = {}, suspend } = context?.agent ?? {};
|
|
142
202
|
|
|
143
203
|
if (!approved) {
|
|
144
204
|
return suspend?.({ reason: "Approval required." });
|
|
145
205
|
}
|
|
146
206
|
|
|
147
|
-
const response = await fetch(`https://wttr.in/${location}?format=3`);
|
|
207
|
+
const response = await fetch(`https://wttr.in/${inputData.location}?format=3`);
|
|
148
208
|
const weather = await response.text();
|
|
149
209
|
|
|
150
210
|
return { weather };
|
|
@@ -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)
|