@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,352 @@
|
|
|
1
|
+
# Nested Workflows
|
|
2
|
+
|
|
3
|
+
Mastra allows you to use workflows as steps within other workflows, enabling you to create modular and reusable workflow components. This feature helps in organizing complex workflows into smaller, manageable pieces and promotes code reuse.
|
|
4
|
+
|
|
5
|
+
It is also visually easier to understand the flow of a workflow when you can see the nested workflows as steps in the parent workflow.
|
|
6
|
+
|
|
7
|
+
## Basic Usage
|
|
8
|
+
|
|
9
|
+
You can use a workflow as a step directly in another workflow using the `step()` method:
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
// Create a nested workflow
|
|
13
|
+
const nestedWorkflow = new Workflow({ name: "nested-workflow" })
|
|
14
|
+
.step(stepA)
|
|
15
|
+
.then(stepB)
|
|
16
|
+
.commit();
|
|
17
|
+
|
|
18
|
+
// Use the nested workflow in a parent workflow
|
|
19
|
+
const parentWorkflow = new Workflow({ name: "parent-workflow" })
|
|
20
|
+
.step(nestedWorkflow, {
|
|
21
|
+
variables: {
|
|
22
|
+
city: {
|
|
23
|
+
step: "trigger",
|
|
24
|
+
path: "myTriggerInput",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
.then(stepC)
|
|
29
|
+
.commit();
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
When a workflow is used as a step:
|
|
33
|
+
|
|
34
|
+
- It is automatically converted to a step using the workflow's name as the step ID
|
|
35
|
+
- The workflow's results are available in the parent workflow's context
|
|
36
|
+
- The nested workflow's steps are executed in their defined order
|
|
37
|
+
|
|
38
|
+
## Accessing Results
|
|
39
|
+
|
|
40
|
+
Results from a nested workflow are available in the parent workflow's context under the nested workflow's name. The results include all step outputs from the nested workflow:
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
const { results } = await parentWorkflow.start();
|
|
44
|
+
// Access nested workflow results
|
|
45
|
+
const nestedWorkflowResult = results["nested-workflow"];
|
|
46
|
+
if (nestedWorkflowResult.status === "success") {
|
|
47
|
+
const nestedResults = nestedWorkflowResult.output.results;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Control Flow with Nested Workflows
|
|
52
|
+
|
|
53
|
+
Nested workflows support all the control flow features available to regular steps:
|
|
54
|
+
|
|
55
|
+
### Parallel Execution
|
|
56
|
+
|
|
57
|
+
Multiple nested workflows can be executed in parallel:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
parentWorkflow
|
|
61
|
+
.step(nestedWorkflowA)
|
|
62
|
+
.step(nestedWorkflowB)
|
|
63
|
+
.after([nestedWorkflowA, nestedWorkflowB])
|
|
64
|
+
.step(finalStep);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Or using `step()` with an array of workflows:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
parentWorkflow.step([nestedWorkflowA, nestedWorkflowB]).then(finalStep);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
In this case, `then()` will implicitly wait for all the workflows to finish before executing the final step.
|
|
74
|
+
|
|
75
|
+
### If-Else Branching
|
|
76
|
+
|
|
77
|
+
Nested workflows can be used in if-else branches using the new syntax that accepts both branches as arguments:
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// Create nested workflows for different paths
|
|
81
|
+
const workflowA = new Workflow({ name: "workflow-a" })
|
|
82
|
+
.step(stepA1)
|
|
83
|
+
.then(stepA2)
|
|
84
|
+
.commit();
|
|
85
|
+
|
|
86
|
+
const workflowB = new Workflow({ name: "workflow-b" })
|
|
87
|
+
.step(stepB1)
|
|
88
|
+
.then(stepB2)
|
|
89
|
+
.commit();
|
|
90
|
+
|
|
91
|
+
// Use the new if-else syntax with nested workflows
|
|
92
|
+
parentWorkflow
|
|
93
|
+
.step(initialStep)
|
|
94
|
+
.if(
|
|
95
|
+
async ({ context }) => {
|
|
96
|
+
// Your condition here
|
|
97
|
+
return someCondition;
|
|
98
|
+
},
|
|
99
|
+
workflowA, // if branch
|
|
100
|
+
workflowB, // else branch
|
|
101
|
+
)
|
|
102
|
+
.then(finalStep)
|
|
103
|
+
.commit();
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The new syntax is more concise and clearer when working with nested workflows. When the condition is:
|
|
107
|
+
|
|
108
|
+
- `true`: The first workflow (if branch) is executed
|
|
109
|
+
- `false`: The second workflow (else branch) is executed
|
|
110
|
+
|
|
111
|
+
The skipped workflow will have a status of `skipped` in the results:
|
|
112
|
+
|
|
113
|
+
The `.then(finalStep)` call following the if-else block will merge the if and else branches back into a single execution path.
|
|
114
|
+
|
|
115
|
+
### Looping
|
|
116
|
+
|
|
117
|
+
Nested workflows can use `.until()` and `.while()` loops same as any other step. One interesting new pattern is to pass a workflow directly as the loop-back argument to keep executing that nested workflow until something is true about its results:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
parentWorkflow
|
|
121
|
+
.step(firstStep)
|
|
122
|
+
.while(
|
|
123
|
+
({ context }) =>
|
|
124
|
+
context.getStepResult("nested-workflow").output.results.someField ===
|
|
125
|
+
"someValue",
|
|
126
|
+
nestedWorkflow,
|
|
127
|
+
)
|
|
128
|
+
.step(finalStep)
|
|
129
|
+
.commit();
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Watching Nested Workflows
|
|
133
|
+
|
|
134
|
+
You can watch the state changes of nested workflows using the `watch` method on the parent workflow. This is useful for monitoring the progress and state transitions of complex workflows:
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
const parentWorkflow = new Workflow({ name: "parent-workflow" })
|
|
138
|
+
.step([nestedWorkflowA, nestedWorkflowB])
|
|
139
|
+
.then(finalStep)
|
|
140
|
+
.commit();
|
|
141
|
+
|
|
142
|
+
const run = parentWorkflow.createRun();
|
|
143
|
+
const unwatch = parentWorkflow.watch((state) => {
|
|
144
|
+
console.log("Current state:", state.value);
|
|
145
|
+
// Access nested workflow states in state.context
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
await run.start();
|
|
149
|
+
unwatch(); // Stop watching when done
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Suspending and Resuming
|
|
153
|
+
|
|
154
|
+
Nested workflows support suspension and resumption, allowing you to pause and continue workflow execution at specific points. You can suspend either the entire nested workflow or specific steps within it:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
// Define a step that may need to suspend
|
|
158
|
+
const suspendableStep = new Step({
|
|
159
|
+
id: "other",
|
|
160
|
+
description: "Step that may need to suspend",
|
|
161
|
+
execute: async ({ context, suspend }) => {
|
|
162
|
+
if (!wasSuspended) {
|
|
163
|
+
wasSuspended = true;
|
|
164
|
+
await suspend();
|
|
165
|
+
}
|
|
166
|
+
return { other: 26 };
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Create a nested workflow with suspendable steps
|
|
171
|
+
const nestedWorkflow = new Workflow({ name: "nested-workflow-a" })
|
|
172
|
+
.step(startStep)
|
|
173
|
+
.then(suspendableStep)
|
|
174
|
+
.then(finalStep)
|
|
175
|
+
.commit();
|
|
176
|
+
|
|
177
|
+
// Use in parent workflow
|
|
178
|
+
const parentWorkflow = new Workflow({ name: "parent-workflow" })
|
|
179
|
+
.step(beginStep)
|
|
180
|
+
.then(nestedWorkflow)
|
|
181
|
+
.then(lastStep)
|
|
182
|
+
.commit();
|
|
183
|
+
|
|
184
|
+
// Start the workflow
|
|
185
|
+
const run = parentWorkflow.createRun();
|
|
186
|
+
const { runId, results } = await run.start({ triggerData: { startValue: 1 } });
|
|
187
|
+
|
|
188
|
+
// Check if a specific step in the nested workflow is suspended
|
|
189
|
+
if (results["nested-workflow-a"].output.results.other.status === "suspended") {
|
|
190
|
+
// Resume the specific suspended step using dot notation
|
|
191
|
+
const resumedResults = await run.resume({
|
|
192
|
+
stepId: "nested-workflow-a.other",
|
|
193
|
+
context: { startValue: 1 },
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// The resumed results will contain the completed nested workflow
|
|
197
|
+
expect(resumedResults.results["nested-workflow-a"].output.results).toEqual({
|
|
198
|
+
start: { output: { newValue: 1 }, status: "success" },
|
|
199
|
+
other: { output: { other: 26 }, status: "success" },
|
|
200
|
+
final: { output: { finalValue: 27 }, status: "success" },
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
When resuming a nested workflow:
|
|
206
|
+
|
|
207
|
+
- Use the nested workflow's name as the `stepId` when calling `resume()` to resume the entire workflow
|
|
208
|
+
- Use dot notation (`nested-workflow.step-name`) to resume a specific step within the nested workflow
|
|
209
|
+
- The nested workflow will continue from the suspended step with the provided context
|
|
210
|
+
- You can check the status of specific steps in the nested workflow's results using `results["nested-workflow"].output.results`
|
|
211
|
+
|
|
212
|
+
## Result Schemas and Mapping
|
|
213
|
+
|
|
214
|
+
Nested workflows can define their result schema and mapping, which helps in type safety and data transformation. This is particularly useful when you want to ensure the nested workflow's output matches a specific structure or when you need to transform the results before they're used in the parent workflow.
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
// Create a nested workflow with result schema and mapping
|
|
218
|
+
const nestedWorkflow = new Workflow({
|
|
219
|
+
name: "nested-workflow",
|
|
220
|
+
result: {
|
|
221
|
+
schema: z.object({
|
|
222
|
+
total: z.number(),
|
|
223
|
+
items: z.array(
|
|
224
|
+
z.object({
|
|
225
|
+
id: z.string(),
|
|
226
|
+
value: z.number(),
|
|
227
|
+
}),
|
|
228
|
+
),
|
|
229
|
+
}),
|
|
230
|
+
mapping: {
|
|
231
|
+
// Map values from step results using variables syntax
|
|
232
|
+
total: { step: "step-a", path: "count" },
|
|
233
|
+
items: { step: "step-b", path: "items" },
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
})
|
|
237
|
+
.step(stepA)
|
|
238
|
+
.then(stepB)
|
|
239
|
+
.commit();
|
|
240
|
+
|
|
241
|
+
// Use in parent workflow with type-safe results
|
|
242
|
+
const parentWorkflow = new Workflow({ name: "parent-workflow" })
|
|
243
|
+
.step(nestedWorkflow)
|
|
244
|
+
.then(async ({ context }) => {
|
|
245
|
+
const result = context.getStepResult("nested-workflow");
|
|
246
|
+
// TypeScript knows the structure of result
|
|
247
|
+
console.log(result.total); // number
|
|
248
|
+
console.log(result.items); // Array<{ id: string, value: number }>
|
|
249
|
+
return { success: true };
|
|
250
|
+
})
|
|
251
|
+
.commit();
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Best Practices
|
|
255
|
+
|
|
256
|
+
1. **Modularity**: Use nested workflows to encapsulate related steps and create reusable workflow components.
|
|
257
|
+
2. **Naming**: Give nested workflows descriptive names as they will be used as step IDs in the parent workflow.
|
|
258
|
+
3. **Error Handling**: Nested workflows propagate their errors to the parent workflow, so handle errors appropriately.
|
|
259
|
+
4. **State Management**: Each nested workflow maintains its own state but can access the parent workflow's context.
|
|
260
|
+
5. **Suspension**: When using suspension in nested workflows, consider the entire workflow's state and handle resumption appropriately.
|
|
261
|
+
|
|
262
|
+
## Example
|
|
263
|
+
|
|
264
|
+
Here's a complete example showing various features of nested workflows:
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
const workflowA = new Workflow({
|
|
268
|
+
name: "workflow-a",
|
|
269
|
+
result: {
|
|
270
|
+
schema: z.object({
|
|
271
|
+
activities: z.string(),
|
|
272
|
+
}),
|
|
273
|
+
mapping: {
|
|
274
|
+
activities: {
|
|
275
|
+
step: planActivities,
|
|
276
|
+
path: "activities",
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
})
|
|
281
|
+
.step(fetchWeather)
|
|
282
|
+
.then(planActivities)
|
|
283
|
+
.commit();
|
|
284
|
+
|
|
285
|
+
const workflowB = new Workflow({
|
|
286
|
+
name: "workflow-b",
|
|
287
|
+
result: {
|
|
288
|
+
schema: z.object({
|
|
289
|
+
activities: z.string(),
|
|
290
|
+
}),
|
|
291
|
+
mapping: {
|
|
292
|
+
activities: {
|
|
293
|
+
step: planActivities,
|
|
294
|
+
path: "activities",
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
})
|
|
299
|
+
.step(fetchWeather)
|
|
300
|
+
.then(planActivities)
|
|
301
|
+
.commit();
|
|
302
|
+
|
|
303
|
+
const weatherWorkflow = new Workflow({
|
|
304
|
+
name: "weather-workflow",
|
|
305
|
+
triggerSchema: z.object({
|
|
306
|
+
cityA: z.string().describe("The city to get the weather for"),
|
|
307
|
+
cityB: z.string().describe("The city to get the weather for"),
|
|
308
|
+
}),
|
|
309
|
+
result: {
|
|
310
|
+
schema: z.object({
|
|
311
|
+
activitiesA: z.string(),
|
|
312
|
+
activitiesB: z.string(),
|
|
313
|
+
}),
|
|
314
|
+
mapping: {
|
|
315
|
+
activitiesA: {
|
|
316
|
+
step: workflowA,
|
|
317
|
+
path: "result.activities",
|
|
318
|
+
},
|
|
319
|
+
activitiesB: {
|
|
320
|
+
step: workflowB,
|
|
321
|
+
path: "result.activities",
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
})
|
|
326
|
+
.step(workflowA, {
|
|
327
|
+
variables: {
|
|
328
|
+
city: {
|
|
329
|
+
step: "trigger",
|
|
330
|
+
path: "cityA",
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
})
|
|
334
|
+
.step(workflowB, {
|
|
335
|
+
variables: {
|
|
336
|
+
city: {
|
|
337
|
+
step: "trigger",
|
|
338
|
+
path: "cityB",
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
weatherWorkflow.commit();
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
In this example:
|
|
347
|
+
|
|
348
|
+
1. We define schemas for type safety across all workflows
|
|
349
|
+
2. Each step has proper input and output schemas
|
|
350
|
+
3. The nested workflows have their own trigger schemas and result mappings
|
|
351
|
+
4. Data is passed through using variables syntax in the `.step()` calls
|
|
352
|
+
5. The main workflow combines data from both nested workflows
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Handling Complex LLM Operations | Workflows | Mastra"
|
|
3
|
+
description: "Workflows in Mastra help you orchestrate complex sequences of operations with features like branching, parallel execution, resource suspension, and more."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Handling Complex LLM Operations with Workflows
|
|
7
|
+
|
|
8
|
+
Workflows in Mastra help you orchestrate complex sequences of operations with features like branching, parallel execution, resource suspension, and more.
|
|
9
|
+
|
|
10
|
+
## When to use workflows
|
|
11
|
+
|
|
12
|
+
Most AI applications need more than a single call to a language model. You may want to run multiple steps, conditionally skip certain paths, or even pause execution altogether until you receive user input. Sometimes your agent tool calling is not accurate enough.
|
|
13
|
+
|
|
14
|
+
Mastra's workflow system provides:
|
|
15
|
+
|
|
16
|
+
- A standardized way to define steps and link them together.
|
|
17
|
+
- Support for both simple (linear) and advanced (branching, parallel) paths.
|
|
18
|
+
- Debugging and observability features to track each workflow run.
|
|
19
|
+
|
|
20
|
+
## Example
|
|
21
|
+
|
|
22
|
+
To create a workflow, you define one or more steps, link them, and then commit the workflow before starting it.
|
|
23
|
+
|
|
24
|
+
### Breaking Down the Workflow
|
|
25
|
+
|
|
26
|
+
Let's examine each part of the workflow creation process:
|
|
27
|
+
|
|
28
|
+
#### 1. Creating the Workflow
|
|
29
|
+
|
|
30
|
+
Here's how you define a workflow in Mastra. The `name` field determines the workflow's API endpoint (`/workflows/$NAME/`), while the `triggerSchema` defines the structure of the workflow's trigger data:
|
|
31
|
+
|
|
32
|
+
```ts filename="src/mastra/workflow/index.ts"
|
|
33
|
+
const myWorkflow = new Workflow({
|
|
34
|
+
name: "my-workflow",
|
|
35
|
+
triggerSchema: z.object({
|
|
36
|
+
inputValue: z.number(),
|
|
37
|
+
}),
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
#### 2. Defining Steps
|
|
42
|
+
|
|
43
|
+
Now, we'll define the workflow's steps. Each step can have its own input and output schemas. Here, `stepOne` doubles an input value, and `stepTwo` increments that result if `stepOne` was successful. (To keep things simple, we aren't making any LLM calls in this example):
|
|
44
|
+
|
|
45
|
+
```ts filename="src/mastra/workflow/index.ts"
|
|
46
|
+
const stepOne = new Step({
|
|
47
|
+
id: "stepOne",
|
|
48
|
+
outputSchema: z.object({
|
|
49
|
+
doubledValue: z.number(),
|
|
50
|
+
}),
|
|
51
|
+
execute: async ({ context }) => {
|
|
52
|
+
const doubledValue = context.triggerData.inputValue * 2;
|
|
53
|
+
return { doubledValue };
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const stepTwo = new Step({
|
|
58
|
+
id: "stepTwo",
|
|
59
|
+
execute: async ({ context }) => {
|
|
60
|
+
const doubledValue = context.getStepResult(stepOne)?.doubledValue;
|
|
61
|
+
if (!doubledValue) {
|
|
62
|
+
return { incrementedValue: 0 };
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
incrementedValue: doubledValue + 1,
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### 3. Linking Steps
|
|
72
|
+
|
|
73
|
+
Now, let's create the control flow, and "commit" (finalize the workflow). In this case, `stepOne` runs first and is followed by `stepTwo`.
|
|
74
|
+
|
|
75
|
+
```ts filename="src/mastra/workflow/index.ts"
|
|
76
|
+
myWorkflow.step(stepOne).then(stepTwo).commit();
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Register the Workflow
|
|
80
|
+
|
|
81
|
+
Register your workflow with Mastra to enable logging and telemetry:
|
|
82
|
+
|
|
83
|
+
```ts showLineNumbers filename="src/mastra/index.ts"
|
|
84
|
+
import { Mastra } from "@mastra/core";
|
|
85
|
+
|
|
86
|
+
export const mastra = new Mastra({
|
|
87
|
+
workflows: { myWorkflow },
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The workflow can also have the mastra instance injected into the context in the case where you need to create dynamic workflows:
|
|
92
|
+
|
|
93
|
+
```ts filename="src/mastra/workflow/index.ts"
|
|
94
|
+
import { Mastra } from "@mastra/core";
|
|
95
|
+
|
|
96
|
+
const mastra = new Mastra();
|
|
97
|
+
|
|
98
|
+
const myWorkflow = new Workflow({
|
|
99
|
+
name: "my-workflow",
|
|
100
|
+
mastra,
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Executing the Workflow
|
|
105
|
+
|
|
106
|
+
Execute your workflow programmatically or via API:
|
|
107
|
+
|
|
108
|
+
```ts showLineNumbers filename="src/mastra/run-workflow.ts" copy
|
|
109
|
+
import { mastra } from "./index";
|
|
110
|
+
|
|
111
|
+
// Get the workflow
|
|
112
|
+
const myWorkflow = mastra.getWorkflow("myWorkflow");
|
|
113
|
+
const { runId, start } = myWorkflow.createRun();
|
|
114
|
+
|
|
115
|
+
// Start the workflow execution
|
|
116
|
+
await start({ triggerData: { inputValue: 45 } });
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Or use the API (requires running `mastra dev`):
|
|
120
|
+
|
|
121
|
+
// Create workflow run
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
curl --location 'http://localhost:4111/api/workflows/myWorkflow/start-async' \
|
|
125
|
+
--header 'Content-Type: application/json' \
|
|
126
|
+
--data '{
|
|
127
|
+
"inputValue": 45
|
|
128
|
+
}'
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
This example shows the essentials: define your workflow, add steps, commit the workflow, then execute it.
|
|
132
|
+
|
|
133
|
+
## Defining Steps
|
|
134
|
+
|
|
135
|
+
The basic building block of a workflow [is a step](./steps.mdx). Steps are defined using schemas for inputs and outputs, and can fetch prior step results.
|
|
136
|
+
|
|
137
|
+
## Control Flow
|
|
138
|
+
|
|
139
|
+
Workflows let you define a [control flow](./control-flow.mdx) to chain steps together in with parallel steps, branching paths, and more.
|
|
140
|
+
|
|
141
|
+
## Workflow Variables
|
|
142
|
+
|
|
143
|
+
When you need to map data between steps or create dynamic data flows, [workflow variables](./variables.mdx) provide a powerful mechanism for passing information from one step to another and accessing nested properties within step outputs.
|
|
144
|
+
|
|
145
|
+
## Suspend and Resume
|
|
146
|
+
|
|
147
|
+
When you need to pause execution for external data, user input, or asynchronous events, Mastra [supports suspension at any step](./suspend-and-resume.mdx), persisting the state of the workflow so you can resume it later.
|
|
148
|
+
|
|
149
|
+
## Observability and Debugging
|
|
150
|
+
|
|
151
|
+
Mastra workflows automatically [log the input and output of each step within a workflow run](../../reference/observability/otel-config.mdx), allowing you to send this data to your preferred logging, telemetry, or observability tools.
|
|
152
|
+
|
|
153
|
+
You can:
|
|
154
|
+
|
|
155
|
+
- Track the status of each step (e.g., `success`, `error`, or `suspended`).
|
|
156
|
+
- Store run-specific metadata for analysis.
|
|
157
|
+
- Integrate with third-party observability platforms like Datadog or New Relic by forwarding logs.
|
|
158
|
+
|
|
159
|
+
## Injecting Request/User-Specific Variables
|
|
160
|
+
|
|
161
|
+
We support dependency injection for tools and workflows. You can pass a container directly to your `start` or `resume` function calls, or inject it using [server middleware](/docs/deployment/server#Middleware).
|
|
162
|
+
|
|
163
|
+
```ts showLineNumbers filename="src/mastra/run-workflow.ts" copy
|
|
164
|
+
import { mastra } from "./index";
|
|
165
|
+
|
|
166
|
+
const stepTwo = new Step({
|
|
167
|
+
id: "stepTwo",
|
|
168
|
+
execute: async ({ context, container }) => {
|
|
169
|
+
const multiplier = (container.get("multiplier");
|
|
170
|
+
const doubledValue = context.getStepResult(stepOne)?.doubledValue;
|
|
171
|
+
if (!doubledValue) {
|
|
172
|
+
return { incrementedValue: 0 };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
incrementedValue: doubledValue * multiplier,
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Get the workflow
|
|
182
|
+
const myWorkflow = mastra.getWorkflow("myWorkflow");
|
|
183
|
+
const { runId, start, resume } = myWorkflow.createRun();
|
|
184
|
+
|
|
185
|
+
type MyContainer = { multiplier; number };
|
|
186
|
+
|
|
187
|
+
const container = new Container<MyContainer>();
|
|
188
|
+
container.set("multiplier", 5);
|
|
189
|
+
|
|
190
|
+
// Start the workflow execution
|
|
191
|
+
await start({ triggerData: { inputValue: 45 }, container });
|
|
192
|
+
await resume({ stepId: "stepTwo", container });
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## More Resources
|
|
196
|
+
|
|
197
|
+
- The [Workflow Guide](../guides/ai-recruiter.mdx) in the Guides section is a tutorial that covers the main concepts.
|
|
198
|
+
- [Sequential Steps workflow example](../../examples/workflows/sequential-steps.mdx)
|
|
199
|
+
- [Parallel Steps workflow example](../../examples/workflows/parallel-steps.mdx)
|
|
200
|
+
- [Branching Paths workflow example](../../examples/workflows/branching-paths.mdx)
|
|
201
|
+
- [Workflow Variables example](../../examples/workflows/workflow-variables.mdx)
|
|
202
|
+
- [Cyclical Dependencies workflow example](../../examples/workflows/cyclical-dependencies.mdx)
|
|
203
|
+
- [Suspend and Resume workflow example](../../examples/workflows/suspend-and-resume.mdx)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Creating Steps and Adding to Workflows | Mastra Docs"
|
|
3
|
+
description: "Steps in Mastra workflows provide a structured way to manage operations by defining inputs, outputs, and execution logic."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Defining Steps in a Workflow
|
|
7
|
+
|
|
8
|
+
When you build a workflow, you typically break down operations into smaller tasks that can be linked and reused. Steps provide a structured way to manage these tasks by defining inputs, outputs, and execution logic.
|
|
9
|
+
|
|
10
|
+
The code below shows how to define these steps inline or separately.
|
|
11
|
+
|
|
12
|
+
## Inline Step Creation
|
|
13
|
+
|
|
14
|
+
You can create steps directly within your workflow using `.step()` and `.then()`. This code shows how to define, link, and execute two steps in sequence.
|
|
15
|
+
|
|
16
|
+
```typescript showLineNumbers filename="src/mastra/workflows/index.ts" copy
|
|
17
|
+
import { Step, Workflow, Mastra } from "@mastra/core";
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
|
|
20
|
+
export const myWorkflow = new Workflow({
|
|
21
|
+
name: "my-workflow",
|
|
22
|
+
triggerSchema: z.object({
|
|
23
|
+
inputValue: z.number(),
|
|
24
|
+
}),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
myWorkflow
|
|
28
|
+
.step(
|
|
29
|
+
new Step({
|
|
30
|
+
id: "stepOne",
|
|
31
|
+
outputSchema: z.object({
|
|
32
|
+
doubledValue: z.number(),
|
|
33
|
+
}),
|
|
34
|
+
execute: async ({ context }) => ({
|
|
35
|
+
doubledValue: context.triggerData.inputValue * 2,
|
|
36
|
+
}),
|
|
37
|
+
}),
|
|
38
|
+
)
|
|
39
|
+
.then(
|
|
40
|
+
new Step({
|
|
41
|
+
id: "stepTwo",
|
|
42
|
+
outputSchema: z.object({
|
|
43
|
+
incrementedValue: z.number(),
|
|
44
|
+
}),
|
|
45
|
+
execute: async ({ context }) => {
|
|
46
|
+
if (context.steps.stepOne.status !== "success") {
|
|
47
|
+
return { incrementedValue: 0 };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { incrementedValue: context.steps.stepOne.output.doubledValue + 1 };
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
53
|
+
).commit();
|
|
54
|
+
|
|
55
|
+
// Register the workflow with Mastra
|
|
56
|
+
export const mastra = new Mastra({
|
|
57
|
+
workflows: { myWorkflow },
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Creating Steps Separately
|
|
62
|
+
|
|
63
|
+
If you prefer to manage your step logic in separate entities, you can define steps outside and then add them to your workflow. This code shows how to define steps independently and link them afterward.
|
|
64
|
+
|
|
65
|
+
```typescript showLineNumbers filename="src/mastra/workflows/index.ts" copy
|
|
66
|
+
import { Step, Workflow, Mastra } from "@mastra/core";
|
|
67
|
+
import { z } from "zod";
|
|
68
|
+
|
|
69
|
+
// Define steps separately
|
|
70
|
+
const stepOne = new Step({
|
|
71
|
+
id: "stepOne",
|
|
72
|
+
outputSchema: z.object({
|
|
73
|
+
doubledValue: z.number(),
|
|
74
|
+
}),
|
|
75
|
+
execute: async ({ context }) => ({
|
|
76
|
+
doubledValue: context.triggerData.inputValue * 2,
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const stepTwo = new Step({
|
|
81
|
+
id: "stepTwo",
|
|
82
|
+
outputSchema: z.object({
|
|
83
|
+
incrementedValue: z.number(),
|
|
84
|
+
}),
|
|
85
|
+
execute: async ({ context }) => {
|
|
86
|
+
if (context.steps.stepOne.status !== "success") {
|
|
87
|
+
return { incrementedValue: 0 };
|
|
88
|
+
}
|
|
89
|
+
return { incrementedValue: context.steps.stepOne.output.doubledValue + 1 };
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Build the workflow
|
|
94
|
+
const myWorkflow = new Workflow({
|
|
95
|
+
name: "my-workflow",
|
|
96
|
+
triggerSchema: z.object({
|
|
97
|
+
inputValue: z.number(),
|
|
98
|
+
}),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
myWorkflow.step(stepOne).then(stepTwo);
|
|
102
|
+
myWorkflow.commit();
|
|
103
|
+
|
|
104
|
+
// Register the workflow with Mastra
|
|
105
|
+
export const mastra = new Mastra({
|
|
106
|
+
workflows: { myWorkflow },
|
|
107
|
+
});
|
|
108
|
+
```
|