@mastra/mcp-docs-server 0.0.0-commonjs-20250414101718
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/.docs/organized/changelogs/%40mastra%2Fastra.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +161 -0
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +110 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fgithub.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +26 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmem0.md +196 -0
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Frag.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fragie.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-azure.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-deepgram.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-elevenlabs.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-google.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-ibm.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-murf.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-openai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-playai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-replicate.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-speechify.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +250 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +250 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +302 -0
- package/.docs/organized/changelogs/create-mastra.md +302 -0
- package/.docs/organized/changelogs/mastra.md +302 -0
- package/.docs/organized/code-examples/agent-network.md +282 -0
- package/.docs/organized/code-examples/agent.md +388 -0
- package/.docs/organized/code-examples/ai-sdk-useChat.md +378 -0
- package/.docs/organized/code-examples/assistant-ui.md +37 -0
- package/.docs/organized/code-examples/bird-checker-with-express.md +235 -0
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +360 -0
- package/.docs/organized/code-examples/bird-checker-with-nextjs.md +250 -0
- package/.docs/organized/code-examples/client-side-tools.md +69 -0
- package/.docs/organized/code-examples/crypto-chatbot.md +96 -0
- package/.docs/organized/code-examples/fireworks-r1.md +159 -0
- package/.docs/organized/code-examples/mcp-registry-registry.md +63 -0
- package/.docs/organized/code-examples/memory-todo-agent.md +164 -0
- package/.docs/organized/code-examples/memory-with-context.md +167 -0
- package/.docs/organized/code-examples/memory-with-libsql.md +204 -0
- package/.docs/organized/code-examples/memory-with-mem0.md +121 -0
- package/.docs/organized/code-examples/memory-with-pg.md +224 -0
- package/.docs/organized/code-examples/memory-with-upstash.md +268 -0
- package/.docs/organized/code-examples/quick-start.md +129 -0
- package/.docs/organized/code-examples/stock-price-tool.md +124 -0
- package/.docs/organized/code-examples/weather-agent.md +353 -0
- package/.docs/organized/code-examples/workflow-ai-recruiter.md +159 -0
- package/.docs/organized/code-examples/workflow-with-inline-steps.md +111 -0
- package/.docs/organized/code-examples/workflow-with-memory.md +393 -0
- package/.docs/organized/code-examples/workflow-with-separate-steps.md +131 -0
- package/.docs/raw/agents/adding-tools.mdx +317 -0
- package/.docs/raw/agents/adding-voice.mdx +175 -0
- package/.docs/raw/agents/agent-memory.mdx +62 -0
- package/.docs/raw/agents/mcp-guide.mdx +215 -0
- package/.docs/raw/agents/overview.mdx +303 -0
- package/.docs/raw/community/discord.mdx +12 -0
- package/.docs/raw/community/licensing.mdx +63 -0
- package/.docs/raw/deployment/client.mdx +120 -0
- package/.docs/raw/deployment/deployment.mdx +127 -0
- package/.docs/raw/deployment/server.mdx +282 -0
- package/.docs/raw/evals/custom-eval.mdx +22 -0
- package/.docs/raw/evals/overview.mdx +95 -0
- package/.docs/raw/evals/running-in-ci.mdx +81 -0
- package/.docs/raw/evals/textual-evals.mdx +54 -0
- package/.docs/raw/faq/index.mdx +63 -0
- package/.docs/raw/frameworks/ai-sdk.mdx +296 -0
- package/.docs/raw/frameworks/next-js.mdx +238 -0
- package/.docs/raw/getting-started/installation.mdx +442 -0
- package/.docs/raw/getting-started/mcp-docs-server.mdx +141 -0
- package/.docs/raw/getting-started/project-structure.mdx +80 -0
- package/.docs/raw/index.mdx +22 -0
- package/.docs/raw/integrations/index.mdx +213 -0
- package/.docs/raw/local-dev/add-to-existing-project.mdx +48 -0
- package/.docs/raw/local-dev/creating-a-new-project.mdx +54 -0
- package/.docs/raw/local-dev/mastra-dev.mdx +108 -0
- package/.docs/raw/memory/memory-processors.mdx +131 -0
- package/.docs/raw/memory/overview.mdx +119 -0
- package/.docs/raw/memory/semantic-recall.mdx +122 -0
- package/.docs/raw/memory/working-memory.mdx +87 -0
- package/.docs/raw/observability/logging.mdx +38 -0
- package/.docs/raw/observability/nextjs-tracing.mdx +108 -0
- package/.docs/raw/observability/tracing.mdx +115 -0
- package/.docs/raw/rag/chunking-and-embedding.mdx +156 -0
- package/.docs/raw/rag/overview.mdx +85 -0
- package/.docs/raw/rag/retrieval.mdx +365 -0
- package/.docs/raw/rag/vector-databases.mdx +340 -0
- package/.docs/raw/reference/agents/createTool.mdx +229 -0
- package/.docs/raw/reference/agents/generate.mdx +334 -0
- package/.docs/raw/reference/agents/getAgent.mdx +54 -0
- package/.docs/raw/reference/agents/stream.mdx +369 -0
- package/.docs/raw/reference/cli/build.mdx +55 -0
- package/.docs/raw/reference/cli/dev.mdx +134 -0
- package/.docs/raw/reference/cli/init.mdx +43 -0
- package/.docs/raw/reference/client-js/agents.mdx +107 -0
- package/.docs/raw/reference/client-js/error-handling.mdx +38 -0
- package/.docs/raw/reference/client-js/logs.mdx +24 -0
- package/.docs/raw/reference/client-js/memory.mdx +97 -0
- package/.docs/raw/reference/client-js/telemetry.mdx +20 -0
- package/.docs/raw/reference/client-js/tools.mdx +44 -0
- package/.docs/raw/reference/client-js/vectors.mdx +79 -0
- package/.docs/raw/reference/client-js/workflows.mdx +136 -0
- package/.docs/raw/reference/core/mastra-class.mdx +232 -0
- package/.docs/raw/reference/deployer/cloudflare.mdx +207 -0
- package/.docs/raw/reference/deployer/deployer.mdx +159 -0
- package/.docs/raw/reference/deployer/netlify.mdx +109 -0
- package/.docs/raw/reference/deployer/vercel.mdx +117 -0
- package/.docs/raw/reference/evals/answer-relevancy.mdx +186 -0
- package/.docs/raw/reference/evals/bias.mdx +186 -0
- package/.docs/raw/reference/evals/completeness.mdx +174 -0
- package/.docs/raw/reference/evals/content-similarity.mdx +183 -0
- package/.docs/raw/reference/evals/context-position.mdx +190 -0
- package/.docs/raw/reference/evals/context-precision.mdx +189 -0
- package/.docs/raw/reference/evals/context-relevancy.mdx +188 -0
- package/.docs/raw/reference/evals/contextual-recall.mdx +191 -0
- package/.docs/raw/reference/evals/faithfulness.mdx +193 -0
- package/.docs/raw/reference/evals/hallucination.mdx +219 -0
- package/.docs/raw/reference/evals/keyword-coverage.mdx +176 -0
- package/.docs/raw/reference/evals/prompt-alignment.mdx +238 -0
- package/.docs/raw/reference/evals/summarization.mdx +205 -0
- package/.docs/raw/reference/evals/textual-difference.mdx +161 -0
- package/.docs/raw/reference/evals/tone-consistency.mdx +181 -0
- package/.docs/raw/reference/evals/toxicity.mdx +165 -0
- package/.docs/raw/reference/index.mdx +12 -0
- package/.docs/raw/reference/memory/Memory.mdx +212 -0
- package/.docs/raw/reference/memory/createThread.mdx +95 -0
- package/.docs/raw/reference/memory/getThreadById.mdx +46 -0
- package/.docs/raw/reference/memory/getThreadsByResourceId.mdx +48 -0
- package/.docs/raw/reference/memory/query.mdx +167 -0
- package/.docs/raw/reference/networks/agent-network.mdx +159 -0
- package/.docs/raw/reference/observability/create-logger.mdx +106 -0
- package/.docs/raw/reference/observability/logger.mdx +55 -0
- package/.docs/raw/reference/observability/otel-config.mdx +120 -0
- package/.docs/raw/reference/observability/providers/braintrust.mdx +40 -0
- package/.docs/raw/reference/observability/providers/dash0.mdx +40 -0
- package/.docs/raw/reference/observability/providers/index.mdx +16 -0
- package/.docs/raw/reference/observability/providers/laminar.mdx +41 -0
- package/.docs/raw/reference/observability/providers/langfuse.mdx +51 -0
- package/.docs/raw/reference/observability/providers/langsmith.mdx +48 -0
- package/.docs/raw/reference/observability/providers/langwatch.mdx +45 -0
- package/.docs/raw/reference/observability/providers/new-relic.mdx +40 -0
- package/.docs/raw/reference/observability/providers/signoz.mdx +40 -0
- package/.docs/raw/reference/observability/providers/traceloop.mdx +40 -0
- package/.docs/raw/reference/rag/astra.mdx +258 -0
- package/.docs/raw/reference/rag/chroma.mdx +281 -0
- package/.docs/raw/reference/rag/chunk.mdx +235 -0
- package/.docs/raw/reference/rag/document.mdx +127 -0
- package/.docs/raw/reference/rag/embeddings.mdx +160 -0
- package/.docs/raw/reference/rag/extract-params.mdx +226 -0
- package/.docs/raw/reference/rag/graph-rag.mdx +182 -0
- package/.docs/raw/reference/rag/libsql.mdx +357 -0
- package/.docs/raw/reference/rag/metadata-filters.mdx +298 -0
- package/.docs/raw/reference/rag/pg.mdx +477 -0
- package/.docs/raw/reference/rag/pinecone.mdx +281 -0
- package/.docs/raw/reference/rag/qdrant.mdx +236 -0
- package/.docs/raw/reference/rag/rerank.mdx +212 -0
- package/.docs/raw/reference/rag/turbopuffer.mdx +249 -0
- package/.docs/raw/reference/rag/upstash.mdx +247 -0
- package/.docs/raw/reference/rag/vectorize.mdx +298 -0
- package/.docs/raw/reference/storage/libsql.mdx +74 -0
- package/.docs/raw/reference/storage/postgresql.mdx +48 -0
- package/.docs/raw/reference/storage/upstash.mdx +86 -0
- package/.docs/raw/reference/tools/client.mdx +207 -0
- package/.docs/raw/reference/tools/document-chunker-tool.mdx +141 -0
- package/.docs/raw/reference/tools/graph-rag-tool.mdx +154 -0
- package/.docs/raw/reference/tools/mcp-configuration.mdx +206 -0
- package/.docs/raw/reference/tools/vector-query-tool.mdx +212 -0
- package/.docs/raw/reference/voice/composite-voice.mdx +140 -0
- package/.docs/raw/reference/voice/deepgram.mdx +164 -0
- package/.docs/raw/reference/voice/elevenlabs.mdx +216 -0
- package/.docs/raw/reference/voice/google.mdx +198 -0
- package/.docs/raw/reference/voice/mastra-voice.mdx +394 -0
- package/.docs/raw/reference/voice/murf.mdx +251 -0
- package/.docs/raw/reference/voice/openai-realtime.mdx +431 -0
- package/.docs/raw/reference/voice/openai.mdx +168 -0
- package/.docs/raw/reference/voice/playai.mdx +159 -0
- package/.docs/raw/reference/voice/sarvam.mdx +260 -0
- package/.docs/raw/reference/voice/speechify.mdx +145 -0
- package/.docs/raw/reference/voice/voice.answer.mdx +122 -0
- package/.docs/raw/reference/voice/voice.connect.mdx +124 -0
- package/.docs/raw/reference/voice/voice.listen.mdx +195 -0
- package/.docs/raw/reference/voice/voice.on.mdx +189 -0
- package/.docs/raw/reference/voice/voice.send.mdx +118 -0
- package/.docs/raw/reference/voice/voice.speak.mdx +203 -0
- package/.docs/raw/reference/workflows/after.mdx +88 -0
- package/.docs/raw/reference/workflows/afterEvent.mdx +76 -0
- package/.docs/raw/reference/workflows/commit.mdx +37 -0
- package/.docs/raw/reference/workflows/createRun.mdx +77 -0
- package/.docs/raw/reference/workflows/else.mdx +72 -0
- package/.docs/raw/reference/workflows/events.mdx +305 -0
- package/.docs/raw/reference/workflows/execute.mdx +110 -0
- package/.docs/raw/reference/workflows/if.mdx +107 -0
- package/.docs/raw/reference/workflows/resume.mdx +155 -0
- package/.docs/raw/reference/workflows/resumeWithEvent.mdx +133 -0
- package/.docs/raw/reference/workflows/snapshots.mdx +207 -0
- package/.docs/raw/reference/workflows/start.mdx +84 -0
- package/.docs/raw/reference/workflows/step-class.mdx +100 -0
- package/.docs/raw/reference/workflows/step-condition.mdx +134 -0
- package/.docs/raw/reference/workflows/step-function.mdx +92 -0
- package/.docs/raw/reference/workflows/step-options.mdx +69 -0
- package/.docs/raw/reference/workflows/step-retries.mdx +203 -0
- package/.docs/raw/reference/workflows/suspend.mdx +70 -0
- package/.docs/raw/reference/workflows/then.mdx +74 -0
- package/.docs/raw/reference/workflows/until.mdx +165 -0
- package/.docs/raw/reference/workflows/watch.mdx +118 -0
- package/.docs/raw/reference/workflows/while.mdx +168 -0
- package/.docs/raw/reference/workflows/workflow.mdx +233 -0
- package/.docs/raw/storage/overview.mdx +378 -0
- package/.docs/raw/voice/overview.mdx +135 -0
- package/.docs/raw/voice/speech-to-text.mdx +45 -0
- package/.docs/raw/voice/text-to-speech.mdx +52 -0
- package/.docs/raw/voice/voice-to-voice.mdx +310 -0
- package/.docs/raw/workflows/control-flow.mdx +778 -0
- package/.docs/raw/workflows/dynamic-workflows.mdx +236 -0
- package/.docs/raw/workflows/error-handling.mdx +183 -0
- package/.docs/raw/workflows/nested-workflows.mdx +352 -0
- package/.docs/raw/workflows/overview.mdx +203 -0
- package/.docs/raw/workflows/steps.mdx +108 -0
- package/.docs/raw/workflows/suspend-and-resume.mdx +404 -0
- package/.docs/raw/workflows/variables.mdx +313 -0
- package/LICENSE.md +46 -0
- package/README.md +129 -0
- package/dist/_tsup-dts-rollup.d.ts +149 -0
- package/dist/chunk-QWYMT5LP.js +194 -0
- package/dist/prepare-docs/prepare.d.ts +1 -0
- package/dist/prepare-docs/prepare.js +1 -0
- package/dist/stdio.d.ts +1 -0
- package/dist/stdio.js +518 -0
- package/package.json +60 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Suspend & Resume Workflows | Human-in-the-Loop | Mastra Docs"
|
|
3
|
+
description: "Suspend and resume in Mastra workflows allows you to pause execution while waiting for external input or resources."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Suspend and Resume in Workflows
|
|
7
|
+
|
|
8
|
+
Complex workflows often need to pause execution while waiting for external input or resources.
|
|
9
|
+
|
|
10
|
+
Mastra's suspend and resume features let you pause workflow execution at any step, persist the workflow snapshot to storage, and resume execution from the saved snapshot when ready.
|
|
11
|
+
This entire process is automatically managed by Mastra. No config needed, or manual step required from the user.
|
|
12
|
+
|
|
13
|
+
Storing the workflow snapshot to storage (LibSQL by default) means that the workflow state is permanently preserved across sessions, deployments, and server restarts. This persistence is crucial for workflows that might remain suspended for minutes, hours, or even days while waiting for external input or resources.
|
|
14
|
+
|
|
15
|
+
## When to Use Suspend/Resume
|
|
16
|
+
|
|
17
|
+
Common scenarios for suspending workflows include:
|
|
18
|
+
|
|
19
|
+
- Waiting for human approval or input
|
|
20
|
+
- Pausing until external API resources become available
|
|
21
|
+
- Collecting additional data needed for later steps
|
|
22
|
+
- Rate limiting or throttling expensive operations
|
|
23
|
+
- Handling event-driven processes with external triggers
|
|
24
|
+
|
|
25
|
+
## Basic Suspend Example
|
|
26
|
+
|
|
27
|
+
Here's a simple workflow that suspends when a value is too low and resumes when given a higher value:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
const stepTwo = new Step({
|
|
31
|
+
id: "stepTwo",
|
|
32
|
+
outputSchema: z.object({
|
|
33
|
+
incrementedValue: z.number(),
|
|
34
|
+
}),
|
|
35
|
+
execute: async ({ context, suspend }) => {
|
|
36
|
+
if (context.steps.stepOne.status !== "success") {
|
|
37
|
+
return { incrementedValue: 0 };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const currentValue = context.steps.stepOne.output.doubledValue;
|
|
41
|
+
|
|
42
|
+
if (currentValue < 100) {
|
|
43
|
+
await suspend();
|
|
44
|
+
return { incrementedValue: 0 };
|
|
45
|
+
}
|
|
46
|
+
return { incrementedValue: currentValue + 1 };
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Async/Await Based Flow
|
|
52
|
+
|
|
53
|
+
The suspend and resume mechanism in Mastra uses an async/await pattern that makes it intuitive to implement complex workflows with suspension points. The code structure naturally reflects the execution flow.
|
|
54
|
+
|
|
55
|
+
### How It Works
|
|
56
|
+
|
|
57
|
+
1. A step's execution function receives a `suspend` function in its parameters
|
|
58
|
+
2. When called with `await suspend()`, the workflow pauses at that point
|
|
59
|
+
3. The workflow state is persisted
|
|
60
|
+
4. Later, the workflow can be resumed by calling `workflow.resume()` with the appropriate parameters
|
|
61
|
+
5. Execution continues from the point after the `suspend()` call
|
|
62
|
+
|
|
63
|
+
### Example with Multiple Suspension Points
|
|
64
|
+
|
|
65
|
+
Here's an example of a workflow with multiple steps that can suspend:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// Define steps with suspend capability
|
|
69
|
+
const promptAgentStep = new Step({
|
|
70
|
+
id: "promptAgent",
|
|
71
|
+
execute: async ({ context, suspend }) => {
|
|
72
|
+
// Some condition that determines if we need to suspend
|
|
73
|
+
if (needHumanInput) {
|
|
74
|
+
// Optionally pass payload data that will be stored with suspended state
|
|
75
|
+
await suspend({ requestReason: "Need human input for prompt" });
|
|
76
|
+
// Code after suspend() will execute when the step is resumed
|
|
77
|
+
return { modelOutput: context.userInput };
|
|
78
|
+
}
|
|
79
|
+
return { modelOutput: "AI generated output" };
|
|
80
|
+
},
|
|
81
|
+
outputSchema: z.object({ modelOutput: z.string() }),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const improveResponseStep = new Step({
|
|
85
|
+
id: "improveResponse",
|
|
86
|
+
execute: async ({ context, suspend }) => {
|
|
87
|
+
// Another condition for suspension
|
|
88
|
+
if (needFurtherRefinement) {
|
|
89
|
+
await suspend();
|
|
90
|
+
return { improvedOutput: context.refinedOutput };
|
|
91
|
+
}
|
|
92
|
+
return { improvedOutput: "Improved output" };
|
|
93
|
+
},
|
|
94
|
+
outputSchema: z.object({ improvedOutput: z.string() }),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Build the workflow
|
|
98
|
+
const workflow = new Workflow({
|
|
99
|
+
name: "multi-suspend-workflow",
|
|
100
|
+
triggerSchema: z.object({ input: z.string() }),
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
workflow
|
|
104
|
+
.step(getUserInput)
|
|
105
|
+
.then(promptAgentStep)
|
|
106
|
+
.then(evaluateTone)
|
|
107
|
+
.then(improveResponseStep)
|
|
108
|
+
.then(evaluateImproved)
|
|
109
|
+
.commit();
|
|
110
|
+
|
|
111
|
+
// Register the workflow with Mastra
|
|
112
|
+
export const mastra = new Mastra({
|
|
113
|
+
workflows: { workflow },
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Starting and Resuming the Workflow
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// Get the workflow and create a run
|
|
121
|
+
const wf = mastra.getWorkflow("multi-suspend-workflow");
|
|
122
|
+
const run = wf.createRun();
|
|
123
|
+
|
|
124
|
+
// Start the workflow
|
|
125
|
+
const initialResult = await run.start({
|
|
126
|
+
triggerData: { input: "initial input" },
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
let promptAgentStepResult = initialResult.activePaths.get("promptAgent");
|
|
130
|
+
let promptAgentResumeResult = undefined;
|
|
131
|
+
|
|
132
|
+
// Check if a step is suspended
|
|
133
|
+
if (promptAgentStepResult?.status === "suspended") {
|
|
134
|
+
console.log("Workflow suspended at promptAgent step");
|
|
135
|
+
|
|
136
|
+
// Resume the workflow with new context
|
|
137
|
+
const resumeResult = await run.resume({
|
|
138
|
+
stepId: "promptAgent",
|
|
139
|
+
context: { userInput: "Human provided input" },
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
promptAgentResumeResult = resumeResult;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const improveResponseStepResult =
|
|
146
|
+
promptAgentResumeResult?.activePaths.get("improveResponse");
|
|
147
|
+
|
|
148
|
+
if (improveResponseStepResult?.status === "suspended") {
|
|
149
|
+
console.log("Workflow suspended at improveResponse step");
|
|
150
|
+
|
|
151
|
+
// Resume again with different context
|
|
152
|
+
const finalResult = await run.resume({
|
|
153
|
+
stepId: "improveResponse",
|
|
154
|
+
context: { refinedOutput: "Human refined output" },
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
console.log("Workflow completed:", finalResult?.results);
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Event-Based Suspension and Resumption
|
|
162
|
+
|
|
163
|
+
In addition to manually suspending steps, Mastra provides event-based suspension through the `afterEvent` method. This allows workflows to automatically suspend and wait for a specific event to occur before continuing.
|
|
164
|
+
|
|
165
|
+
### Using afterEvent and resumeWithEvent
|
|
166
|
+
|
|
167
|
+
The `afterEvent` method automatically creates a suspension point in your workflow that waits for a specific event to occur. When the event happens, you can use `resumeWithEvent` to continue the workflow with the event data.
|
|
168
|
+
|
|
169
|
+
Here's how it works:
|
|
170
|
+
|
|
171
|
+
1. Define events in your workflow configuration
|
|
172
|
+
2. Use `afterEvent` to create a suspension point waiting for that event
|
|
173
|
+
3. When the event occurs, call `resumeWithEvent` with the event name and data
|
|
174
|
+
|
|
175
|
+
### Example: Event-Based Workflow
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
// Define steps
|
|
179
|
+
const getUserInput = new Step({
|
|
180
|
+
id: "getUserInput",
|
|
181
|
+
execute: async () => ({ userInput: "initial input" }),
|
|
182
|
+
outputSchema: z.object({ userInput: z.string() }),
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const processApproval = new Step({
|
|
186
|
+
id: "processApproval",
|
|
187
|
+
execute: async ({ context }) => {
|
|
188
|
+
// Access the event data from the context
|
|
189
|
+
const approvalData = context.inputData?.resumedEvent;
|
|
190
|
+
return {
|
|
191
|
+
approved: approvalData?.approved,
|
|
192
|
+
approvedBy: approvalData?.approverName,
|
|
193
|
+
};
|
|
194
|
+
},
|
|
195
|
+
outputSchema: z.object({
|
|
196
|
+
approved: z.boolean(),
|
|
197
|
+
approvedBy: z.string(),
|
|
198
|
+
}),
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// Create workflow with event definition
|
|
202
|
+
const approvalWorkflow = new Workflow({
|
|
203
|
+
name: "approval-workflow",
|
|
204
|
+
triggerSchema: z.object({ requestId: z.string() }),
|
|
205
|
+
events: {
|
|
206
|
+
approvalReceived: {
|
|
207
|
+
schema: z.object({
|
|
208
|
+
approved: z.boolean(),
|
|
209
|
+
approverName: z.string(),
|
|
210
|
+
}),
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// Build workflow with event-based suspension
|
|
216
|
+
approvalWorkflow
|
|
217
|
+
.step(getUserInput)
|
|
218
|
+
.afterEvent("approvalReceived") // Workflow will automatically suspend here
|
|
219
|
+
.step(processApproval) // This step runs after the event is received
|
|
220
|
+
.commit();
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Running an Event-Based Workflow
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
// Get the workflow
|
|
227
|
+
const workflow = mastra.getWorkflow("approval-workflow");
|
|
228
|
+
const run = workflow.createRun();
|
|
229
|
+
|
|
230
|
+
// Start the workflow
|
|
231
|
+
const initialResult = await run.start({
|
|
232
|
+
triggerData: { requestId: "request-123" },
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
console.log("Workflow started, waiting for approval event");
|
|
236
|
+
console.log(initialResult.results);
|
|
237
|
+
// Output will show the workflow is suspended at the event step:
|
|
238
|
+
// {
|
|
239
|
+
// getUserInput: { status: 'success', output: { userInput: 'initial input' } },
|
|
240
|
+
// __approvalReceived_event: { status: 'suspended' }
|
|
241
|
+
// }
|
|
242
|
+
|
|
243
|
+
// Later, when the approval event occurs:
|
|
244
|
+
const resumeResult = await run.resumeWithEvent("approvalReceived", {
|
|
245
|
+
approved: true,
|
|
246
|
+
approverName: "Jane Doe",
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
console.log("Workflow resumed with event data:", resumeResult.results);
|
|
250
|
+
// Output will show the completed workflow:
|
|
251
|
+
// {
|
|
252
|
+
// getUserInput: { status: 'success', output: { userInput: 'initial input' } },
|
|
253
|
+
// __approvalReceived_event: { status: 'success', output: { executed: true, resumedEvent: { approved: true, approverName: 'Jane Doe' } } },
|
|
254
|
+
// processApproval: { status: 'success', output: { approved: true, approvedBy: 'Jane Doe' } }
|
|
255
|
+
// }
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Key Points About Event-Based Workflows
|
|
259
|
+
|
|
260
|
+
- The `suspend()` function can optionally take a payload object that will be stored with the suspended state
|
|
261
|
+
- Code after the `await suspend()` call will not execute until the step is resumed
|
|
262
|
+
- When a step is suspended, its status becomes `'suspended'` in the workflow results
|
|
263
|
+
- When resumed, the step's status changes from `'suspended'` to `'success'` once completed
|
|
264
|
+
- The `resume()` method requires the `stepId` to identify which suspended step to resume
|
|
265
|
+
- You can provide new context data when resuming that will be merged with existing step results
|
|
266
|
+
|
|
267
|
+
- Events must be defined in the workflow configuration with a schema
|
|
268
|
+
- The `afterEvent` method creates a special suspended step that waits for the event
|
|
269
|
+
- The event step is automatically named `__eventName_event` (e.g., `__approvalReceived_event`)
|
|
270
|
+
- Use `resumeWithEvent` to provide event data and continue the workflow
|
|
271
|
+
- Event data is validated against the schema defined for that event
|
|
272
|
+
- The event data is available in the context as `inputData.resumedEvent`
|
|
273
|
+
|
|
274
|
+
## Storage for Suspend and Resume
|
|
275
|
+
|
|
276
|
+
When a workflow is suspended using `await suspend()`, Mastra automatically persists the entire workflow state to storage. This is essential for workflows that might remain suspended for extended periods, as it ensures the state is preserved across application restarts or server instances.
|
|
277
|
+
|
|
278
|
+
### Default Storage: LibSQL
|
|
279
|
+
|
|
280
|
+
By default, Mastra uses LibSQL as its storage engine:
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
284
|
+
import { DefaultStorage } from "@mastra/core/storage/libsql";
|
|
285
|
+
|
|
286
|
+
const mastra = new Mastra({
|
|
287
|
+
storage: new DefaultStorage({
|
|
288
|
+
config: {
|
|
289
|
+
url: "file:storage.db", // Local file-based database for development
|
|
290
|
+
// For production, use a persistent URL:
|
|
291
|
+
// url: process.env.DATABASE_URL,
|
|
292
|
+
// authToken: process.env.DATABASE_AUTH_TOKEN, // Optional for authenticated connections
|
|
293
|
+
},
|
|
294
|
+
}),
|
|
295
|
+
});
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
The LibSQL storage can be configured in different modes:
|
|
299
|
+
|
|
300
|
+
- In-memory database (testing): `:memory:`
|
|
301
|
+
- File-based database (development): `file:storage.db`
|
|
302
|
+
- Remote database (production): URLs like `libsql://your-database.turso.io`
|
|
303
|
+
|
|
304
|
+
### Alternative Storage Options
|
|
305
|
+
|
|
306
|
+
#### Upstash (Redis-Compatible)
|
|
307
|
+
|
|
308
|
+
For serverless applications or environments where Redis is preferred:
|
|
309
|
+
|
|
310
|
+
```bash
|
|
311
|
+
npm install @mastra/upstash
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
316
|
+
import { UpstashStore } from "@mastra/upstash";
|
|
317
|
+
|
|
318
|
+
const mastra = new Mastra({
|
|
319
|
+
storage: new UpstashStore({
|
|
320
|
+
url: process.env.UPSTASH_URL,
|
|
321
|
+
token: process.env.UPSTASH_TOKEN,
|
|
322
|
+
}),
|
|
323
|
+
});
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### Storage Considerations
|
|
327
|
+
|
|
328
|
+
- All storage options support suspend and resume functionality identically
|
|
329
|
+
- The workflow state is automatically serialized and saved when suspended
|
|
330
|
+
- No additional configuration is needed for suspend/resume to work with storage
|
|
331
|
+
- Choose your storage option based on your infrastructure, scaling needs, and existing technology stack
|
|
332
|
+
|
|
333
|
+
## Watching and Resuming
|
|
334
|
+
|
|
335
|
+
To handle suspended workflows, use the `watch` method to monitor workflow status per run and `resume` to continue execution:
|
|
336
|
+
|
|
337
|
+
```typescript
|
|
338
|
+
import { mastra } from "./index";
|
|
339
|
+
|
|
340
|
+
// Get the workflow
|
|
341
|
+
const myWorkflow = mastra.getWorkflow("myWorkflow");
|
|
342
|
+
const { start, watch, resume } = myWorkflow.createRun();
|
|
343
|
+
|
|
344
|
+
// Start watching the workflow before executing it
|
|
345
|
+
watch(async ({ activePaths }) => {
|
|
346
|
+
const isStepTwoSuspended = activePaths.get("stepTwo")?.status === "suspended";
|
|
347
|
+
if (isStepTwoSuspended) {
|
|
348
|
+
console.log("Workflow suspended, resuming with new value");
|
|
349
|
+
|
|
350
|
+
// Resume the workflow with new context
|
|
351
|
+
await resume({
|
|
352
|
+
stepId: "stepTwo",
|
|
353
|
+
context: { secondValue: 100 },
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
// Start the workflow execution
|
|
359
|
+
await start({ triggerData: { inputValue: 45 } });
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
### Watching and Resuming Event-Based Workflows
|
|
363
|
+
|
|
364
|
+
You can use the same watching pattern with event-based workflows:
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
const { start, watch, resumeWithEvent } = workflow.createRun();
|
|
368
|
+
|
|
369
|
+
// Watch for suspended event steps
|
|
370
|
+
watch(async ({ activePaths }) => {
|
|
371
|
+
const isApprovalReceivedSuspended =
|
|
372
|
+
activePaths.get("__approvalReceived_event")?.status === "suspended";
|
|
373
|
+
if (isApprovalReceivedSuspended) {
|
|
374
|
+
console.log("Workflow waiting for approval event");
|
|
375
|
+
|
|
376
|
+
// In a real scenario, you would wait for the actual event to occur
|
|
377
|
+
// For example, this could be triggered by a webhook or user interaction
|
|
378
|
+
setTimeout(async () => {
|
|
379
|
+
await resumeWithEvent("approvalReceived", {
|
|
380
|
+
approved: true,
|
|
381
|
+
approverName: "Auto Approver",
|
|
382
|
+
});
|
|
383
|
+
}, 5000); // Simulate event after 5 seconds
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// Start the workflow
|
|
388
|
+
await start({ triggerData: { requestId: "auto-123" } });
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
## Further Reading
|
|
392
|
+
|
|
393
|
+
For a deeper understanding of how suspend and resume works under the hood:
|
|
394
|
+
|
|
395
|
+
- [Understanding Snapshots in Mastra Workflows](../../reference/workflows/snapshots.mdx) - Learn about the snapshot mechanism that powers suspend and resume functionality
|
|
396
|
+
- [Step Configuration Guide](./steps.mdx) - Learn more about configuring steps in your workflows
|
|
397
|
+
- [Control Flow Guide](./control-flow.mdx) - Advanced workflow control patterns
|
|
398
|
+
- [Event-Driven Workflows](../../reference/workflows/events.mdx) - Detailed reference for event-based workflows
|
|
399
|
+
|
|
400
|
+
## Related Resources
|
|
401
|
+
|
|
402
|
+
- See the [Suspend and Resume Example](../../examples/workflows/suspend-and-resume.mdx) for a complete working example
|
|
403
|
+
- Check the [Step Class Reference](../../reference/workflows/step-class.mdx) for suspend/resume API details
|
|
404
|
+
- Review [Workflow Observability](../../reference/observability/otel-config.mdx) for monitoring suspended workflows
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Data Mapping with Workflow Variables | Mastra Docs"
|
|
3
|
+
description: "Learn how to use workflow variables to map data between steps and create dynamic data flows in your Mastra workflows."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Data Mapping with Workflow Variables
|
|
7
|
+
|
|
8
|
+
Workflow variables in Mastra provide a powerful mechanism for mapping data between steps, allowing you to create dynamic data flows and pass information from one step to another.
|
|
9
|
+
|
|
10
|
+
## Understanding Workflow Variables
|
|
11
|
+
|
|
12
|
+
In Mastra workflows, variables serve as a way to:
|
|
13
|
+
|
|
14
|
+
- Map data from trigger inputs to step inputs
|
|
15
|
+
- Pass outputs from one step to inputs of another step
|
|
16
|
+
- Access nested properties within step outputs
|
|
17
|
+
- Create more flexible and reusable workflow steps
|
|
18
|
+
|
|
19
|
+
## Using Variables for Data Mapping
|
|
20
|
+
|
|
21
|
+
### Basic Variable Mapping
|
|
22
|
+
|
|
23
|
+
You can map data between steps using the `variables` property when adding a step to your workflow:
|
|
24
|
+
|
|
25
|
+
```typescript showLineNumbers filename="src/mastra/workflows/index.ts" copy
|
|
26
|
+
const workflow = new Workflow({
|
|
27
|
+
name: 'data-mapping-workflow',
|
|
28
|
+
triggerSchema: z.object({
|
|
29
|
+
inputData: z.string(),
|
|
30
|
+
}),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
workflow
|
|
34
|
+
.step(step1, {
|
|
35
|
+
variables: {
|
|
36
|
+
// Map trigger data to step input
|
|
37
|
+
inputData: { step: 'trigger', path: 'inputData' }
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
.then(step2, {
|
|
41
|
+
variables: {
|
|
42
|
+
// Map output from step1 to input for step2
|
|
43
|
+
previousValue: { step: step1, path: 'outputField' }
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
.commit();
|
|
47
|
+
|
|
48
|
+
// Register the workflow with Mastra
|
|
49
|
+
export const mastra = new Mastra({
|
|
50
|
+
workflows: { workflow },
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Accessing Nested Properties
|
|
55
|
+
|
|
56
|
+
You can access nested properties using dot notation in the `path` field:
|
|
57
|
+
|
|
58
|
+
```typescript showLineNumbers filename="src/mastra/workflows/index.ts" copy
|
|
59
|
+
workflow
|
|
60
|
+
.step(step1)
|
|
61
|
+
.then(step2, {
|
|
62
|
+
variables: {
|
|
63
|
+
// Access a nested property from step1's output
|
|
64
|
+
nestedValue: { step: step1, path: 'nested.deeply.value' }
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
.commit();
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Mapping Entire Objects
|
|
71
|
+
|
|
72
|
+
You can map an entire object by using `.` as the path:
|
|
73
|
+
|
|
74
|
+
```typescript showLineNumbers filename="src/mastra/workflows/index.ts" copy
|
|
75
|
+
workflow
|
|
76
|
+
.step(step1, {
|
|
77
|
+
variables: {
|
|
78
|
+
// Map the entire trigger data object
|
|
79
|
+
triggerData: { step: 'trigger', path: '.' }
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
.commit();
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Variables in Loops
|
|
86
|
+
|
|
87
|
+
Variables can also be passed to `while` and `until` loops. This is useful for passing data between iterations or from outside steps:
|
|
88
|
+
|
|
89
|
+
```typescript showLineNumbers filename="src/mastra/workflows/loop-variables.ts" copy
|
|
90
|
+
// Step that increments a counter
|
|
91
|
+
const incrementStep = new Step({
|
|
92
|
+
id: 'increment',
|
|
93
|
+
inputSchema: z.object({
|
|
94
|
+
// Previous value from last iteration
|
|
95
|
+
prevValue: z.number().optional(),
|
|
96
|
+
}),
|
|
97
|
+
outputSchema: z.object({
|
|
98
|
+
// Updated counter value
|
|
99
|
+
updatedCounter: z.number(),
|
|
100
|
+
}),
|
|
101
|
+
execute: async ({ context }) => {
|
|
102
|
+
const { prevValue = 0 } = context.inputData;
|
|
103
|
+
return { updatedCounter: prevValue + 1 };
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const workflow = new Workflow({
|
|
108
|
+
name: 'counter'
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
workflow
|
|
112
|
+
.step(incrementStep)
|
|
113
|
+
.while(
|
|
114
|
+
async ({ context }) => {
|
|
115
|
+
// Continue while counter is less than 10
|
|
116
|
+
const result = context.getStepResult(incrementStep);
|
|
117
|
+
return (result?.updatedCounter ?? 0) < 10;
|
|
118
|
+
},
|
|
119
|
+
incrementStep,
|
|
120
|
+
{
|
|
121
|
+
// Pass previous value to next iteration
|
|
122
|
+
prevValue: {
|
|
123
|
+
step: incrementStep,
|
|
124
|
+
path: 'updatedCounter'
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Variable Resolution
|
|
131
|
+
|
|
132
|
+
When a workflow executes, Mastra resolves variables at runtime by:
|
|
133
|
+
|
|
134
|
+
1. Identifying the source step specified in the `step` property
|
|
135
|
+
2. Retrieving the output from that step
|
|
136
|
+
3. Navigating to the specified property using the `path`
|
|
137
|
+
4. Injecting the resolved value into the target step's context as the `inputData` property
|
|
138
|
+
|
|
139
|
+
## Examples
|
|
140
|
+
|
|
141
|
+
### Mapping from Trigger Data
|
|
142
|
+
|
|
143
|
+
This example shows how to map data from the workflow trigger to a step:
|
|
144
|
+
|
|
145
|
+
```typescript showLineNumbers filename="src/mastra/workflows/trigger-mapping.ts" copy
|
|
146
|
+
import { Step, Workflow, Mastra } from "@mastra/core";
|
|
147
|
+
import { z } from "zod";
|
|
148
|
+
|
|
149
|
+
// Define a step that needs user input
|
|
150
|
+
const processUserInput = new Step({
|
|
151
|
+
id: "processUserInput",
|
|
152
|
+
execute: async ({ context }) => {
|
|
153
|
+
// The inputData will be available in context because of the variable mapping
|
|
154
|
+
const { inputData } = context.inputData;
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
processedData: `Processed: ${inputData}`
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Create the workflow
|
|
163
|
+
const workflow = new Workflow({
|
|
164
|
+
name: "trigger-mapping",
|
|
165
|
+
triggerSchema: z.object({
|
|
166
|
+
inputData: z.string(),
|
|
167
|
+
}),
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Map the trigger data to the step
|
|
171
|
+
workflow
|
|
172
|
+
.step(processUserInput, {
|
|
173
|
+
variables: {
|
|
174
|
+
inputData: { step: 'trigger', path: 'inputData' },
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
.commit();
|
|
178
|
+
|
|
179
|
+
// Register the workflow with Mastra
|
|
180
|
+
export const mastra = new Mastra({
|
|
181
|
+
workflows: { workflow },
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Mapping Between Steps
|
|
186
|
+
|
|
187
|
+
This example demonstrates mapping data from one step to another:
|
|
188
|
+
|
|
189
|
+
```typescript showLineNumbers filename="src/mastra/workflows/step-mapping.ts" copy
|
|
190
|
+
import { Step, Workflow, Mastra } from "@mastra/core";
|
|
191
|
+
import { z } from "zod";
|
|
192
|
+
|
|
193
|
+
// Step 1: Generate data
|
|
194
|
+
const generateData = new Step({
|
|
195
|
+
id: "generateData",
|
|
196
|
+
outputSchema: z.object({
|
|
197
|
+
nested: z.object({
|
|
198
|
+
value: z.string(),
|
|
199
|
+
}),
|
|
200
|
+
}),
|
|
201
|
+
execute: async () => {
|
|
202
|
+
return {
|
|
203
|
+
nested: {
|
|
204
|
+
value: "step1-data"
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// Step 2: Process the data from step 1
|
|
211
|
+
const processData = new Step({
|
|
212
|
+
id: "processData",
|
|
213
|
+
inputSchema: z.object({
|
|
214
|
+
previousValue: z.string(),
|
|
215
|
+
}),
|
|
216
|
+
execute: async ({ context }) => {
|
|
217
|
+
// previousValue will be available because of the variable mapping
|
|
218
|
+
const { previousValue } = context.inputData;
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
result: `Processed: ${previousValue}`
|
|
222
|
+
};
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// Create the workflow
|
|
227
|
+
const workflow = new Workflow({
|
|
228
|
+
name: "step-mapping",
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// Map data from step1 to step2
|
|
232
|
+
workflow
|
|
233
|
+
.step(generateData)
|
|
234
|
+
.then(processData, {
|
|
235
|
+
variables: {
|
|
236
|
+
// Map the nested.value property from generateData's output
|
|
237
|
+
previousValue: { step: generateData, path: 'nested.value' },
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
.commit();
|
|
241
|
+
|
|
242
|
+
// Register the workflow with Mastra
|
|
243
|
+
export const mastra = new Mastra({
|
|
244
|
+
workflows: { workflow },
|
|
245
|
+
});
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## Type Safety
|
|
249
|
+
|
|
250
|
+
Mastra provides type safety for variable mappings when using TypeScript:
|
|
251
|
+
|
|
252
|
+
```typescript showLineNumbers filename="src/mastra/workflows/type-safe.ts" copy
|
|
253
|
+
import { Step, Workflow, Mastra } from "@mastra/core";
|
|
254
|
+
import { z } from "zod";
|
|
255
|
+
|
|
256
|
+
// Define schemas for better type safety
|
|
257
|
+
const triggerSchema = z.object({
|
|
258
|
+
inputValue: z.string(),
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
type TriggerType = z.infer<typeof triggerSchema>;
|
|
262
|
+
|
|
263
|
+
// Step with typed context
|
|
264
|
+
const step1 = new Step({
|
|
265
|
+
id: "step1",
|
|
266
|
+
outputSchema: z.object({
|
|
267
|
+
nested: z.object({
|
|
268
|
+
value: z.string(),
|
|
269
|
+
}),
|
|
270
|
+
}),
|
|
271
|
+
execute: async ({ context }) => {
|
|
272
|
+
// TypeScript knows the shape of triggerData
|
|
273
|
+
const triggerData = context.getStepResult<TriggerType>('trigger');
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
nested: {
|
|
277
|
+
value: `processed-${triggerData?.inputValue}`
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// Create the workflow with the schema
|
|
284
|
+
const workflow = new Workflow({
|
|
285
|
+
name: "type-safe-workflow",
|
|
286
|
+
triggerSchema,
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
workflow.step(step1).commit();
|
|
290
|
+
|
|
291
|
+
// Register the workflow with Mastra
|
|
292
|
+
export const mastra = new Mastra({
|
|
293
|
+
workflows: { workflow },
|
|
294
|
+
});
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
## Best Practices
|
|
298
|
+
|
|
299
|
+
1. **Validate Inputs and Outputs**: Use `inputSchema` and `outputSchema` to ensure data consistency.
|
|
300
|
+
|
|
301
|
+
2. **Keep Mappings Simple**: Avoid overly complex nested paths when possible.
|
|
302
|
+
|
|
303
|
+
3. **Consider Default Values**: Handle cases where mapped data might be undefined.
|
|
304
|
+
|
|
305
|
+
## Comparison with Direct Context Access
|
|
306
|
+
|
|
307
|
+
While you can access previous step results directly via `context.steps`, using variable mappings offers several advantages:
|
|
308
|
+
|
|
309
|
+
| Feature | Variable Mapping | Direct Context Access |
|
|
310
|
+
| ------- | --------------- | --------------------- |
|
|
311
|
+
| Clarity | Explicit data dependencies | Implicit dependencies |
|
|
312
|
+
| Reusability | Steps can be reused with different mappings | Steps are tightly coupled |
|
|
313
|
+
| Type Safety | Better TypeScript integration | Requires manual type assertions |
|