@mastra/memory 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 +839 -0
- package/dist/docs/README.md +36 -0
- package/dist/docs/SKILL.md +42 -0
- package/dist/docs/SOURCE_MAP.json +31 -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 +114 -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 +687 -0
- package/dist/docs/processors/01-reference.md +100 -0
- package/dist/docs/storage/01-reference.md +1139 -0
- package/dist/docs/vectors/01-reference.md +942 -0
- package/dist/index.cjs +339 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +145 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +339 -19
- package/dist/index.js.map +1 -1
- package/dist/tools/working-memory.d.ts +10 -13
- package/dist/tools/working-memory.d.ts.map +1 -1
- package/package.json +15 -14
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @mastra/memory Documentation
|
|
2
|
+
|
|
3
|
+
> Embedded documentation for coding agents
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Read the skill overview
|
|
9
|
+
cat docs/SKILL.md
|
|
10
|
+
|
|
11
|
+
# Get the source map
|
|
12
|
+
cat docs/SOURCE_MAP.json
|
|
13
|
+
|
|
14
|
+
# Read topic documentation
|
|
15
|
+
cat docs/<topic>/01-overview.md
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Structure
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
docs/
|
|
22
|
+
├── SKILL.md # Entry point
|
|
23
|
+
├── README.md # This file
|
|
24
|
+
├── SOURCE_MAP.json # Export index
|
|
25
|
+
├── agents/ (4 files)
|
|
26
|
+
├── core/ (2 files)
|
|
27
|
+
├── memory/ (11 files)
|
|
28
|
+
├── processors/ (1 files)
|
|
29
|
+
├── storage/ (5 files)
|
|
30
|
+
├── vectors/ (4 files)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Version
|
|
34
|
+
|
|
35
|
+
Package: @mastra/memory
|
|
36
|
+
Version: 1.0.0
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mastra-memory-docs
|
|
3
|
+
description: Documentation for @mastra/memory. Includes links to type definitions and readable implementation code in dist/.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# @mastra/memory Documentation
|
|
7
|
+
|
|
8
|
+
> **Version**: 1.0.0
|
|
9
|
+
> **Package**: @mastra/memory
|
|
10
|
+
|
|
11
|
+
## Quick Navigation
|
|
12
|
+
|
|
13
|
+
Use SOURCE_MAP.json to find any export:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cat docs/SOURCE_MAP.json
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Each export maps to:
|
|
20
|
+
- **types**: `.d.ts` file with JSDoc and API signatures
|
|
21
|
+
- **implementation**: `.js` chunk file with readable source
|
|
22
|
+
- **docs**: Conceptual documentation in `docs/`
|
|
23
|
+
|
|
24
|
+
## Top Exports
|
|
25
|
+
|
|
26
|
+
- extractWorkingMemoryContent: dist/index.d.ts
|
|
27
|
+
- extractWorkingMemoryTags: dist/index.d.ts
|
|
28
|
+
- removeWorkingMemoryTags: dist/index.d.ts
|
|
29
|
+
- MessageHistory: dist/index.d.ts
|
|
30
|
+
- SemanticRecall: dist/index.d.ts
|
|
31
|
+
- WorkingMemory: dist/index.d.ts
|
|
32
|
+
|
|
33
|
+
See SOURCE_MAP.json for the complete list.
|
|
34
|
+
|
|
35
|
+
## Available Topics
|
|
36
|
+
|
|
37
|
+
- [Agents](agents/) - 4 file(s)
|
|
38
|
+
- [Core](core/) - 2 file(s)
|
|
39
|
+
- [Memory](memory/) - 11 file(s)
|
|
40
|
+
- [Processors](processors/) - 1 file(s)
|
|
41
|
+
- [Storage](storage/) - 5 file(s)
|
|
42
|
+
- [Vectors](vectors/) - 4 file(s)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "1.0.0",
|
|
3
|
+
"package": "@mastra/memory",
|
|
4
|
+
"exports": {
|
|
5
|
+
"extractWorkingMemoryContent": {
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"implementation": "dist/memory"
|
|
8
|
+
},
|
|
9
|
+
"extractWorkingMemoryTags": {
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"implementation": "dist/memory"
|
|
12
|
+
},
|
|
13
|
+
"removeWorkingMemoryTags": {
|
|
14
|
+
"types": "dist/index.d.ts",
|
|
15
|
+
"implementation": "dist/memory"
|
|
16
|
+
},
|
|
17
|
+
"MessageHistory": {
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"implementation": "dist/processors"
|
|
20
|
+
},
|
|
21
|
+
"SemanticRecall": {
|
|
22
|
+
"types": "dist/index.d.ts",
|
|
23
|
+
"implementation": "dist/processors"
|
|
24
|
+
},
|
|
25
|
+
"WorkingMemory": {
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"implementation": "dist/processors"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"modules": {}
|
|
31
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
> Learn how to add memory to agents to store message history and maintain context across interactions.
|
|
2
|
+
|
|
3
|
+
# Agent memory
|
|
4
|
+
|
|
5
|
+
Agents use memory to maintain context across interactions. LLMs are stateless and don't retain information between calls, so agents need memory to track message history and recall relevant information.
|
|
6
|
+
|
|
7
|
+
Mastra agents can be configured to store message history, with optional [working memory](../memory/working-memory) to maintain recent context or [semantic recall](../memory/semantic-recall) to retrieve past messages based on meaning.
|
|
8
|
+
|
|
9
|
+
## When to use memory
|
|
10
|
+
|
|
11
|
+
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.
|
|
12
|
+
|
|
13
|
+
## Setting up memory
|
|
14
|
+
|
|
15
|
+
To enable memory in Mastra, install the `@mastra/memory` package along with a storage provider.
|
|
16
|
+
|
|
17
|
+
```bash npm2yarn
|
|
18
|
+
npm install @mastra/memory@beta @mastra/libsql@beta
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Storage providers
|
|
22
|
+
|
|
23
|
+
Memory requires a storage provider to persist message history, including user messages and agent responses. For more details on available providers and how storage works in Mastra, see the [Storage](https://mastra.ai/docs/v1/memory/storage) documentation.
|
|
24
|
+
|
|
25
|
+
## Configuring memory
|
|
26
|
+
|
|
27
|
+
### Step
|
|
28
|
+
|
|
29
|
+
Enable memory by creating a `Memory` instance and passing it to the agent’s `memory` option.
|
|
30
|
+
|
|
31
|
+
```typescript {7-11} title="src/mastra/agents/memory-agent.ts"
|
|
32
|
+
import { Agent } from "@mastra/core/agent";
|
|
33
|
+
import { Memory } from "@mastra/memory";
|
|
34
|
+
|
|
35
|
+
export const memoryAgent = new Agent({
|
|
36
|
+
id: 'memory-agent',
|
|
37
|
+
name: 'Memory Agent',
|
|
38
|
+
memory: new Memory({
|
|
39
|
+
options: {
|
|
40
|
+
lastMessages: 20,
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> **Note:**
|
|
47
|
+
|
|
48
|
+
Visit [Memory Class](https://mastra.ai/reference/v1/memory/memory-class) for a full list of configuration options.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
### Step
|
|
53
|
+
|
|
54
|
+
Add a storage provider to your main Mastra instance to enable memory across all configured agents.
|
|
55
|
+
|
|
56
|
+
```typescript {5-8} title="src/mastra/index.ts"
|
|
57
|
+
import { Mastra } from "@mastra/core";
|
|
58
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
59
|
+
|
|
60
|
+
export const mastra = new Mastra({
|
|
61
|
+
storage: new LibSQLStore({
|
|
62
|
+
id: 'mastra-storage',
|
|
63
|
+
url: ":memory:",
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
> **Note:**
|
|
69
|
+
|
|
70
|
+
Visit [libSQL Storage](https://mastra.ai/reference/v1/storage/libsql) for a full list of configuration options.
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
Alternatively, add storage directly to an agent’s memory to keep data separate or use different providers per agent.
|
|
75
|
+
|
|
76
|
+
```typescript {9-12} title="src/mastra/agents/memory-agent.ts"
|
|
77
|
+
import { Agent } from "@mastra/core/agent";
|
|
78
|
+
import { Memory } from "@mastra/memory";
|
|
79
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
80
|
+
|
|
81
|
+
export const memoryAgent = new Agent({
|
|
82
|
+
id: 'memory-agent',
|
|
83
|
+
name: 'Memory Agent',
|
|
84
|
+
memory: new Memory({
|
|
85
|
+
storage: new LibSQLStore({
|
|
86
|
+
id: 'mastra-storage',
|
|
87
|
+
url: ":memory:",
|
|
88
|
+
}),
|
|
89
|
+
}),
|
|
90
|
+
});
|
|
91
|
+
```
|
|
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
|
+
|
|
96
|
+
## Message history
|
|
97
|
+
|
|
98
|
+
Include a `memory` object with both `resource` and `thread` to track message history during agent calls.
|
|
99
|
+
|
|
100
|
+
- `resource`: A stable identifier for the user or entity.
|
|
101
|
+
- `thread`: An ID that isolates a specific conversation or session.
|
|
102
|
+
|
|
103
|
+
These fields tell the agent where to store and retrieve context, enabling persistent, thread-aware memory across a conversation.
|
|
104
|
+
|
|
105
|
+
```typescript {4-7}
|
|
106
|
+
const response = await memoryAgent.generate(
|
|
107
|
+
"Remember my favorite color is blue.",
|
|
108
|
+
{
|
|
109
|
+
memory: {
|
|
110
|
+
resource: "user-123",
|
|
111
|
+
thread: "conversation-123",
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
To recall information stored in memory, call the agent with the same `resource` and `thread` values used in the original conversation.
|
|
118
|
+
|
|
119
|
+
```typescript {2-5}
|
|
120
|
+
const response = await memoryAgent.generate("What's my favorite color?", {
|
|
121
|
+
memory: {
|
|
122
|
+
resource: "user-123",
|
|
123
|
+
thread: "conversation-123",
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
```
|
|
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
|
+
|
|
131
|
+
To learn more about memory see the [Memory](../memory/overview) documentation.
|
|
132
|
+
|
|
133
|
+
## Using `RequestContext`
|
|
134
|
+
|
|
135
|
+
Use [RequestContext](https://mastra.ai/docs/v1/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.
|
|
136
|
+
|
|
137
|
+
```typescript title="src/mastra/agents/memory-agent.ts"
|
|
138
|
+
export type UserTier = {
|
|
139
|
+
"user-tier": "enterprise" | "pro";
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const premiumMemory = new Memory();
|
|
143
|
+
|
|
144
|
+
const standardMemory = new Memory();
|
|
145
|
+
|
|
146
|
+
export const memoryAgent = new Agent({
|
|
147
|
+
id: 'memory-agent',
|
|
148
|
+
name: 'Memory Agent',
|
|
149
|
+
memory: ({ requestContext }) => {
|
|
150
|
+
const userTier = requestContext.get("user-tier") as UserTier["user-tier"];
|
|
151
|
+
|
|
152
|
+
return userTier === "enterprise" ? premiumMemory : standardMemory;
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
> **Note:**
|
|
158
|
+
|
|
159
|
+
Visit [Request Context](https://mastra.ai/docs/v1/server/request-context) for more information.
|
|
160
|
+
|
|
161
|
+
## Related
|
|
162
|
+
|
|
163
|
+
- [Working Memory](../memory/working-memory)
|
|
164
|
+
- [Semantic Recall](../memory/semantic-recall)
|
|
165
|
+
- [Storage](../memory/storage)
|
|
166
|
+
- [Request Context](https://mastra.ai/docs/v1/server/request-context)
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
> Learn how to coordinate multiple agents, workflows, and tools using agent networks for complex, non-deterministic task execution.
|
|
2
|
+
|
|
3
|
+
# Agent Networks
|
|
4
|
+
|
|
5
|
+
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 (sub-agents, workflows, or tools) to call, in what order, and with what data.
|
|
6
|
+
|
|
7
|
+
## When to use networks
|
|
8
|
+
|
|
9
|
+
Use networks for complex tasks that require coordination across multiple primitives. Unlike workflows, which follow a predefined sequence, networks rely on LLM reasoning to interpret the request and decide what to run.
|
|
10
|
+
|
|
11
|
+
## Core principles
|
|
12
|
+
|
|
13
|
+
Mastra agent networks operate using these principles:
|
|
14
|
+
|
|
15
|
+
- Memory is required when using `.network()` and is used to store task history and determine when a task is complete.
|
|
16
|
+
- 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.
|
|
17
|
+
- 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.
|
|
18
|
+
|
|
19
|
+
## Creating an agent network
|
|
20
|
+
|
|
21
|
+
An agent network is built around a top-level routing agent that delegates tasks to agents, 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.
|
|
22
|
+
|
|
23
|
+
```typescript {22-23,26,29} title="src/mastra/agents/routing-agent.ts"
|
|
24
|
+
import { Agent } from "@mastra/core/agent";
|
|
25
|
+
import { Memory } from "@mastra/memory";
|
|
26
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
27
|
+
|
|
28
|
+
import { researchAgent } from "./research-agent";
|
|
29
|
+
import { writingAgent } from "./writing-agent";
|
|
30
|
+
|
|
31
|
+
import { cityWorkflow } from "../workflows/city-workflow";
|
|
32
|
+
import { weatherTool } from "../tools/weather-tool";
|
|
33
|
+
|
|
34
|
+
export const routingAgent = new Agent({
|
|
35
|
+
id: "routing-agent",
|
|
36
|
+
name: "Routing Agent",
|
|
37
|
+
instructions: `
|
|
38
|
+
You are a network of writers and researchers.
|
|
39
|
+
The user will ask you to research a topic.
|
|
40
|
+
Always respond with a complete report—no bullet points.
|
|
41
|
+
Write in full paragraphs, like a blog post.
|
|
42
|
+
Do not answer with incomplete or uncertain information.`,
|
|
43
|
+
model: "openai/gpt-5.1",
|
|
44
|
+
agents: {
|
|
45
|
+
researchAgent,
|
|
46
|
+
writingAgent,
|
|
47
|
+
},
|
|
48
|
+
workflows: {
|
|
49
|
+
cityWorkflow,
|
|
50
|
+
},
|
|
51
|
+
tools: {
|
|
52
|
+
weatherTool,
|
|
53
|
+
},
|
|
54
|
+
memory: new Memory({
|
|
55
|
+
storage: new LibSQLStore({
|
|
56
|
+
id: 'mastra-storage',
|
|
57
|
+
url: "file:../mastra.db",
|
|
58
|
+
}),
|
|
59
|
+
}),
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Writing descriptions for network primitives
|
|
64
|
+
|
|
65
|
+
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.
|
|
66
|
+
|
|
67
|
+
#### Agent descriptions
|
|
68
|
+
|
|
69
|
+
Each agent in a network should include a clear `description` that explains what the agent does.
|
|
70
|
+
|
|
71
|
+
```typescript title="src/mastra/agents/research-agent.ts"
|
|
72
|
+
export const researchAgent = new Agent({
|
|
73
|
+
id: "research-agent",
|
|
74
|
+
name: "Research Agent",
|
|
75
|
+
description: `This agent gathers concise research insights in bullet-point form.
|
|
76
|
+
It's designed to extract key facts without generating full
|
|
77
|
+
responses or narrative content.`,
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```typescript title="src/mastra/agents/writing-agent.ts"
|
|
82
|
+
export const writingAgent = new Agent({
|
|
83
|
+
id: "writing-agent",
|
|
84
|
+
name: "Writing Agent",
|
|
85
|
+
description: `This agent turns researched material into well-structured
|
|
86
|
+
written content. It produces full-paragraph reports with no bullet points,
|
|
87
|
+
suitable for use in articles, summaries, or blog posts.`,
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### Workflow descriptions
|
|
92
|
+
|
|
93
|
+
Workflows in a network should include a `description` to explain their purpose, along with `inputSchema` and `outputSchema` to describe the expected data.
|
|
94
|
+
|
|
95
|
+
```typescript title="src/mastra/workflows/city-workflow.ts"
|
|
96
|
+
export const cityWorkflow = createWorkflow({
|
|
97
|
+
id: "city-workflow",
|
|
98
|
+
description: `This workflow handles city-specific research tasks.
|
|
99
|
+
It first gathers factual information about the city, then synthesizes
|
|
100
|
+
that research into a full written report. Use it when the user input
|
|
101
|
+
includes a city to be researched.`,
|
|
102
|
+
inputSchema: z.object({
|
|
103
|
+
city: z.string(),
|
|
104
|
+
}),
|
|
105
|
+
outputSchema: z.object({
|
|
106
|
+
text: z.string(),
|
|
107
|
+
}),
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### Tool descriptions
|
|
112
|
+
|
|
113
|
+
Tools in a network should include a `description` to explain their purpose, along with `inputSchema` and `outputSchema` to describe the expected data.
|
|
114
|
+
|
|
115
|
+
```typescript title="src/mastra/tools/weather-tool.ts"
|
|
116
|
+
export const weatherTool = createTool({
|
|
117
|
+
id: "weather-tool",
|
|
118
|
+
description: ` Retrieves current weather information using the wttr.in API.
|
|
119
|
+
Accepts a city or location name as input and returns a short weather summary.
|
|
120
|
+
Use this tool whenever up-to-date weather data is requested.
|
|
121
|
+
`,
|
|
122
|
+
inputSchema: z.object({
|
|
123
|
+
location: z.string(),
|
|
124
|
+
}),
|
|
125
|
+
outputSchema: z.object({
|
|
126
|
+
weather: z.string(),
|
|
127
|
+
}),
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Calling agent networks
|
|
132
|
+
|
|
133
|
+
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.
|
|
134
|
+
|
|
135
|
+
### Agent example
|
|
136
|
+
|
|
137
|
+
In this example, the network interprets the message and would route the request to both the `researchAgent` and `writingAgent` to generate a complete response.
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
const result = await routingAgent.network(
|
|
141
|
+
"Tell me three cool ways to use Mastra",
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
for await (const chunk of result) {
|
|
145
|
+
console.log(chunk.type);
|
|
146
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
147
|
+
console.log(chunk.payload.result);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
#### Agent output
|
|
153
|
+
|
|
154
|
+
The following `chunk.type` events are emitted during this request:
|
|
155
|
+
|
|
156
|
+
```text
|
|
157
|
+
routing-agent-start
|
|
158
|
+
routing-agent-end
|
|
159
|
+
agent-execution-start
|
|
160
|
+
agent-execution-event-start
|
|
161
|
+
agent-execution-event-step-start
|
|
162
|
+
agent-execution-event-text-start
|
|
163
|
+
agent-execution-event-text-delta
|
|
164
|
+
agent-execution-event-text-end
|
|
165
|
+
agent-execution-event-step-finish
|
|
166
|
+
agent-execution-event-finish
|
|
167
|
+
agent-execution-end
|
|
168
|
+
network-execution-event-step-finish
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Workflow example
|
|
172
|
+
|
|
173
|
+
In this example, the routing agent recognizes the city name in the message and runs the `cityWorkflow`. The workflow defines steps that call the `researchAgent` to gather facts, then the `writingAgent` to generate the final text.
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
const result = await routingAgent.network(
|
|
177
|
+
"Tell me some historical facts about London",
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
for await (const chunk of result) {
|
|
181
|
+
console.log(chunk.type);
|
|
182
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
183
|
+
console.log(chunk.payload.result);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
#### Workflow output
|
|
189
|
+
|
|
190
|
+
The following `chunk.type` events are emitted during this request:
|
|
191
|
+
|
|
192
|
+
```text
|
|
193
|
+
routing-agent-end
|
|
194
|
+
workflow-execution-start
|
|
195
|
+
workflow-execution-event-workflow-start
|
|
196
|
+
workflow-execution-event-workflow-step-start
|
|
197
|
+
workflow-execution-event-workflow-step-result
|
|
198
|
+
workflow-execution-event-workflow-finish
|
|
199
|
+
workflow-execution-end
|
|
200
|
+
routing-agent-start
|
|
201
|
+
network-execution-event-step-finish
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Tool example
|
|
205
|
+
|
|
206
|
+
In this example, the routing agent skips the `researchAgent`, `writingAgent`, and `cityWorkflow`, and calls the `weatherTool` directly to complete the task.
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
const result = await routingAgent.network("What's the weather in London?");
|
|
210
|
+
|
|
211
|
+
for await (const chunk of result) {
|
|
212
|
+
console.log(chunk.type);
|
|
213
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
214
|
+
console.log(chunk.payload.result);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
#### Tool output
|
|
220
|
+
|
|
221
|
+
The following `chunk.type` events are emitted during this request:
|
|
222
|
+
|
|
223
|
+
```text
|
|
224
|
+
routing-agent-start
|
|
225
|
+
routing-agent-end
|
|
226
|
+
tool-execution-start
|
|
227
|
+
tool-execution-end
|
|
228
|
+
network-execution-event-step-finish
|
|
229
|
+
```
|
|
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
|
+
|
|
287
|
+
## Related
|
|
288
|
+
|
|
289
|
+
- [Agent Memory](./agent-memory)
|
|
290
|
+
- [Workflows Overview](../workflows/overview)
|
|
291
|
+
- [Request Context](https://mastra.ai/docs/v1/server/request-context)
|
|
292
|
+
- [Supervisor example](https://github.com/mastra-ai/mastra/tree/main/examples/supervisor-agent)
|