@mastra/mcp-docs-server 0.13.2-alpha.2 → 0.13.2
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%2Fclient-js.md +15 -15
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +15 -15
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +7 -7
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +8 -8
- package/.docs/organized/changelogs/create-mastra.md +7 -7
- package/.docs/organized/changelogs/mastra.md +10 -10
- package/.docs/raw/course/01-first-agent/04-project-structure.md +8 -3
- package/.docs/raw/course/01-first-agent/07-creating-your-agent.md +5 -3
- package/.docs/raw/course/01-first-agent/08-exporting-your-agent.md +21 -6
- package/.docs/raw/course/01-first-agent/11-creating-transactions-tool.md +5 -3
- package/.docs/raw/course/01-first-agent/12-connecting-tool-to-agent.md +2 -2
- package/.docs/raw/course/04-workflows/01-introduction-to-workflows.md +44 -0
- package/.docs/raw/course/04-workflows/02-understanding-steps.md +53 -0
- package/.docs/raw/course/04-workflows/03-creating-your-first-step.md +57 -0
- package/.docs/raw/course/04-workflows/04-creating-a-second-step.md +58 -0
- package/.docs/raw/course/04-workflows/05-chaining-steps-together.md +57 -0
- package/.docs/raw/course/04-workflows/06-registering-with-mastra.md +24 -0
- package/.docs/raw/course/04-workflows/07-using-playground.md +58 -0
- package/.docs/raw/course/04-workflows/08-running-workflows-programmatically.md +77 -0
- package/.docs/raw/course/04-workflows/09-adding-a-third-step.md +70 -0
- package/.docs/raw/course/04-workflows/10-updating-the-workflow.md +55 -0
- package/.docs/raw/course/04-workflows/11-creating-an-ai-agent.md +67 -0
- package/.docs/raw/course/04-workflows/12-using-agent-in-workflow.md +91 -0
- package/.docs/raw/course/04-workflows/13-creating-ai-enhanced-workflow.md +75 -0
- package/.docs/raw/course/04-workflows/14-understanding-parallel-execution.md +38 -0
- package/.docs/raw/course/04-workflows/15-creating-parallel-steps.md +115 -0
- package/.docs/raw/course/04-workflows/16-building-parallel-workflow.md +100 -0
- package/.docs/raw/course/04-workflows/17-testing-parallel-performance.md +40 -0
- package/.docs/raw/course/04-workflows/18-understanding-conditional-branching.md +61 -0
- package/.docs/raw/course/04-workflows/19-creating-conditional-steps.md +128 -0
- package/.docs/raw/course/04-workflows/20-building-conditional-workflow.md +60 -0
- package/.docs/raw/course/04-workflows/21-testing-conditional-logic.md +58 -0
- package/.docs/raw/course/04-workflows/22-conclusion.md +58 -0
- package/package.json +4 -4
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Introduction to Workflows
|
|
2
|
+
|
|
3
|
+
Welcome to the fourth lesson of the Mastra course! In this lesson, you'll learn about Mastra Workflows - a powerful way to orchestrate complex sequences of operations.
|
|
4
|
+
|
|
5
|
+
## What are Workflows?
|
|
6
|
+
|
|
7
|
+
Workflows in Mastra let you chain together multiple operations in a predictable, type-safe manner. Think of them as a recipe that breaks down complex tasks into smaller, manageable steps.
|
|
8
|
+
|
|
9
|
+
Instead of writing one big function that does everything, workflows let you:
|
|
10
|
+
|
|
11
|
+
- Break complex operations into smaller, reusable steps
|
|
12
|
+
- Define clear inputs and outputs for each step
|
|
13
|
+
- Chain steps together with automatic data validation
|
|
14
|
+
- Handle errors gracefully at each step
|
|
15
|
+
|
|
16
|
+
## Simple Example
|
|
17
|
+
|
|
18
|
+
Without workflows, you might write:
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
async function processContent(text: string) {
|
|
22
|
+
// All logic in one function - hard to test and reuse
|
|
23
|
+
const validated = validateText(text);
|
|
24
|
+
const enhanced = enhanceText(validated);
|
|
25
|
+
const summarized = summarizeText(enhanced);
|
|
26
|
+
return summarized;
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
With workflows, the same logic becomes modular and reusable with tracing built in at every step.
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
export const contentWorkflow = createWorkflow({...})
|
|
34
|
+
.then(validateStep)
|
|
35
|
+
.then(enhanceStep)
|
|
36
|
+
.then(summarizeStep)
|
|
37
|
+
.commit();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## What You'll Build
|
|
41
|
+
|
|
42
|
+
In this lesson, you'll create a content processing workflow that validates, enhances, and summarizes text content using multiple connected steps.
|
|
43
|
+
|
|
44
|
+
Let's start by understanding the basic building blocks!
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Understanding Steps
|
|
2
|
+
|
|
3
|
+
Steps are the building blocks of workflows. Each step is a self-contained unit that takes some input, processes it, and produces an output.
|
|
4
|
+
|
|
5
|
+
## What is a Step?
|
|
6
|
+
|
|
7
|
+
A step has three main parts:
|
|
8
|
+
|
|
9
|
+
1. **Input Schema** - what data it expects to receive
|
|
10
|
+
2. **Output Schema** - what data it will produce
|
|
11
|
+
3. **Execute Function** - the logic that transforms input to output
|
|
12
|
+
|
|
13
|
+
## Step Structure
|
|
14
|
+
|
|
15
|
+
Every step follows this pattern:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
const myStep = createStep({
|
|
19
|
+
id: "unique-step-name",
|
|
20
|
+
description: "What this step does",
|
|
21
|
+
inputSchema: z.object({
|
|
22
|
+
// Define expected input structure
|
|
23
|
+
}),
|
|
24
|
+
outputSchema: z.object({
|
|
25
|
+
// Define output structure
|
|
26
|
+
}),
|
|
27
|
+
execute: async ({ inputData }) => {
|
|
28
|
+
// Your logic here
|
|
29
|
+
return {
|
|
30
|
+
// Return data matching output schema
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Why Use Schemas?
|
|
37
|
+
|
|
38
|
+
Schemas provide several benefits:
|
|
39
|
+
|
|
40
|
+
- **Type Safety**: TypeScript knows exactly what data flows between steps
|
|
41
|
+
- **Runtime Validation**: Invalid data is caught immediately with helpful error messages
|
|
42
|
+
- **Documentation**: Schemas serve as living documentation of your workflow
|
|
43
|
+
- **Debugging**: Clear contracts make it easy to identify issues
|
|
44
|
+
|
|
45
|
+
## Key Benefits
|
|
46
|
+
|
|
47
|
+
- **Reusable**: Steps can be used in multiple workflows
|
|
48
|
+
- **Testable**: Each step can be tested in isolation
|
|
49
|
+
- **Composable**: Steps can be combined in different ways
|
|
50
|
+
- **Reliable**: Schemas catch data flow issues early
|
|
51
|
+
- **Traceable**: Every step is traced so you can see the flow of data
|
|
52
|
+
|
|
53
|
+
Next, you'll create your first step!
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Creating Your First Step
|
|
2
|
+
|
|
3
|
+
Let's create your first workflow step! We'll build a step that validates text content.
|
|
4
|
+
|
|
5
|
+
## Setting Up
|
|
6
|
+
|
|
7
|
+
First, create a new file for your workflow in the `src/mastra/workflows` directory. Let's name this file `content-workflow.ts`
|
|
8
|
+
|
|
9
|
+
## Creating a Validation Step
|
|
10
|
+
|
|
11
|
+
Add this code to your workflow file:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createStep } from "@mastra/core/workflows";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
|
|
17
|
+
const validateContentStep = createStep({
|
|
18
|
+
id: "validate-content",
|
|
19
|
+
description: "Validates incoming text content",
|
|
20
|
+
inputSchema: z.object({
|
|
21
|
+
content: z.string().min(1, "Content cannot be empty"),
|
|
22
|
+
type: z.enum(["article", "blog", "social"]).default("article"),
|
|
23
|
+
}),
|
|
24
|
+
outputSchema: z.object({
|
|
25
|
+
content: z.string(),
|
|
26
|
+
type: z.string(),
|
|
27
|
+
wordCount: z.number(),
|
|
28
|
+
isValid: z.boolean(),
|
|
29
|
+
}),
|
|
30
|
+
execute: async ({ inputData }) => {
|
|
31
|
+
const { content, type } = inputData;
|
|
32
|
+
|
|
33
|
+
const wordCount = content.trim().split(/\s+/).length;
|
|
34
|
+
const isValid = wordCount >= 5; // Minimum 5 words
|
|
35
|
+
|
|
36
|
+
if (!isValid) {
|
|
37
|
+
throw new Error(`Content too short: ${wordCount} words`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
content: content.trim(),
|
|
42
|
+
type,
|
|
43
|
+
wordCount,
|
|
44
|
+
isValid,
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Understanding the Code
|
|
51
|
+
|
|
52
|
+
- **ID**: Unique identifier for this step
|
|
53
|
+
- **Input Schema**: Expects `content` (string) and optional `type`
|
|
54
|
+
- **Output Schema**: Returns content, type, word count, and validation status
|
|
55
|
+
- **Execute**: Contains the validation logic
|
|
56
|
+
|
|
57
|
+
Your first step is ready! Next, you'll test it to make sure it works correctly.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Creating a Second Step
|
|
2
|
+
|
|
3
|
+
Now let's create a second step that enhances the validated content with metadata.
|
|
4
|
+
|
|
5
|
+
## The Enhancement Step
|
|
6
|
+
|
|
7
|
+
Add this step to your workflow file:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
const enhanceContentStep = createStep({
|
|
11
|
+
id: "enhance-content",
|
|
12
|
+
description: "Adds metadata to validated content",
|
|
13
|
+
inputSchema: z.object({
|
|
14
|
+
content: z.string(),
|
|
15
|
+
type: z.string(),
|
|
16
|
+
wordCount: z.number(),
|
|
17
|
+
isValid: z.boolean(),
|
|
18
|
+
}),
|
|
19
|
+
outputSchema: z.object({
|
|
20
|
+
content: z.string(),
|
|
21
|
+
type: z.string(),
|
|
22
|
+
wordCount: z.number(),
|
|
23
|
+
metadata: z.object({
|
|
24
|
+
readingTime: z.number(),
|
|
25
|
+
difficulty: z.enum(["easy", "medium", "hard"]),
|
|
26
|
+
processedAt: z.string(),
|
|
27
|
+
}),
|
|
28
|
+
}),
|
|
29
|
+
execute: async ({ inputData }) => {
|
|
30
|
+
const { content, type, wordCount } = inputData;
|
|
31
|
+
|
|
32
|
+
// Calculate reading time (200 words per minute)
|
|
33
|
+
const readingTime = Math.ceil(wordCount / 200);
|
|
34
|
+
|
|
35
|
+
// Determine difficulty based on word count
|
|
36
|
+
let difficulty: "easy" | "medium" | "hard" = "easy";
|
|
37
|
+
if (wordCount > 100) difficulty = "medium";
|
|
38
|
+
if (wordCount > 300) difficulty = "hard";
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
content,
|
|
42
|
+
type,
|
|
43
|
+
wordCount,
|
|
44
|
+
metadata: {
|
|
45
|
+
readingTime,
|
|
46
|
+
difficulty,
|
|
47
|
+
processedAt: new Date().toISOString(),
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Notice the Input Schema
|
|
55
|
+
|
|
56
|
+
The input schema of this step matches the output schema of the previous step. This is important for chaining steps together!
|
|
57
|
+
|
|
58
|
+
Your second step is ready! Next, you'll learn how to chain these steps together into a complete workflow.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Chaining Steps Together
|
|
2
|
+
|
|
3
|
+
Now you'll learn how to chain your steps together to create a complete workflow.
|
|
4
|
+
|
|
5
|
+
## Creating the Workflow
|
|
6
|
+
|
|
7
|
+
Add this workflow definition to your file:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { createWorkflow } from "@mastra/core/workflows";
|
|
11
|
+
|
|
12
|
+
export const contentWorkflow = createWorkflow({
|
|
13
|
+
id: "content-processing-workflow",
|
|
14
|
+
description: "Validates and enhances content",
|
|
15
|
+
inputSchema: z.object({
|
|
16
|
+
content: z.string(),
|
|
17
|
+
type: z.enum(["article", "blog", "social"]).default("article"),
|
|
18
|
+
}),
|
|
19
|
+
outputSchema: z.object({
|
|
20
|
+
content: z.string(),
|
|
21
|
+
type: z.string(),
|
|
22
|
+
wordCount: z.number(),
|
|
23
|
+
metadata: z.object({
|
|
24
|
+
readingTime: z.number(),
|
|
25
|
+
difficulty: z.enum(["easy", "medium", "hard"]),
|
|
26
|
+
processedAt: z.string(),
|
|
27
|
+
}),
|
|
28
|
+
}),
|
|
29
|
+
})
|
|
30
|
+
.then(validateContentStep)
|
|
31
|
+
.then(enhanceContentStep)
|
|
32
|
+
.commit();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Understanding the Workflow
|
|
36
|
+
|
|
37
|
+
- **Input Schema**: Defines what data the workflow expects
|
|
38
|
+
- **Output Schema**: Defines what the workflow will return
|
|
39
|
+
- **Steps**: Chained using `.then()` in the order they should execute
|
|
40
|
+
- **Commit**: Finalizes the workflow definition
|
|
41
|
+
|
|
42
|
+
## How Data Flows
|
|
43
|
+
|
|
44
|
+
1. Workflow receives input matching the input schema
|
|
45
|
+
2. First step processes input and outputs validated data
|
|
46
|
+
3. Second step receives the first step's output as its input
|
|
47
|
+
4. Workflow returns the final step's output
|
|
48
|
+
|
|
49
|
+
## Schema Validation
|
|
50
|
+
|
|
51
|
+
The workflow automatically validates:
|
|
52
|
+
|
|
53
|
+
- Input data matches the workflow's input schema
|
|
54
|
+
- Each step's output matches the next step's input schema
|
|
55
|
+
- Final output matches the workflow's output schema
|
|
56
|
+
|
|
57
|
+
Your workflow is now ready to run! Next, you'll test the complete workflow.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Registering with Mastra
|
|
2
|
+
|
|
3
|
+
Now you'll register your workflow with the main Mastra instance so you can use it alongside agents and tools.
|
|
4
|
+
|
|
5
|
+
## Updating Your Mastra Configuration
|
|
6
|
+
|
|
7
|
+
Open your `src/mastra/index.ts` file and add your workflow:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
// Import your workflow
|
|
11
|
+
import { contentWorkflow } from "./workflows/content-workflow";
|
|
12
|
+
|
|
13
|
+
export const mastra = new Mastra({
|
|
14
|
+
// Register your workflow here
|
|
15
|
+
workflows: {
|
|
16
|
+
contentWorkflow,
|
|
17
|
+
},
|
|
18
|
+
// ...Existing code
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Note: You may already have workflows registered, in which case, this workflow should be added to the workflows object.
|
|
23
|
+
|
|
24
|
+
Your workflow is now registered with Mastra! Next, you'll learn how to use it in the playground.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Using the Playground
|
|
2
|
+
|
|
3
|
+
The Mastra Playground provides a visual interface to test and run your workflows. Let's see your workflow in action!
|
|
4
|
+
|
|
5
|
+
## Starting the Development Server
|
|
6
|
+
|
|
7
|
+
Start your Mastra development server using the `mastra dev` command. If you are using npm the command is:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm run dev
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
You should see output like:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
🚀 Mastra Dev Server starting...
|
|
17
|
+
📊 Playground available at: http://localhost:4111
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Accessing Workflows in Playground
|
|
21
|
+
|
|
22
|
+
1. Open your browser and go to `http://localhost:4111`
|
|
23
|
+
2. Click on "Workflows" in the navigation
|
|
24
|
+
3. You should see your `contentWorkflow` listed
|
|
25
|
+
|
|
26
|
+
## Testing Your Workflow
|
|
27
|
+
|
|
28
|
+
1. Click on your `contentWorkflow`
|
|
29
|
+
2. You'll see an input form based on your workflow's input schema
|
|
30
|
+
3. Enter some test content in the form:
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
{
|
|
34
|
+
"content": "Machine learning is revolutionizing healthcare by enabling faster diagnoses and personalized treatments.",
|
|
35
|
+
"type": "article"
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
4. Click "Run Workflow"
|
|
40
|
+
5. Watch the execution progress and see the results
|
|
41
|
+
|
|
42
|
+
## Understanding the Interface
|
|
43
|
+
|
|
44
|
+
The playground shows you:
|
|
45
|
+
|
|
46
|
+
- **Input Schema**: What data your workflow expects
|
|
47
|
+
- **Execution Progress**: Real-time updates as steps complete
|
|
48
|
+
- **Output**: The final result from your workflow
|
|
49
|
+
- **Execution Time**: How long the workflow took to run
|
|
50
|
+
|
|
51
|
+
## Benefits of the Playground
|
|
52
|
+
|
|
53
|
+
- **Visual Testing**: Easy way to test workflows without writing code
|
|
54
|
+
- **Schema Validation**: Automatic form generation from your schemas
|
|
55
|
+
- **Real-time Feedback**: See exactly what's happening during execution
|
|
56
|
+
- **Easy Debugging**: Quickly test different inputs and view traces from workflow runs
|
|
57
|
+
|
|
58
|
+
Great! You can now visually test your workflow. Next, you'll learn how to run workflows programmatically.
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Running Workflows Programmatically
|
|
2
|
+
|
|
3
|
+
Learn how to execute your workflows from code, which is essential for integrating them into applications.
|
|
4
|
+
|
|
5
|
+
## Creating a Workflow Runner
|
|
6
|
+
|
|
7
|
+
Create a new file to test programmatic execution:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
// src/run-workflow.ts
|
|
11
|
+
import { mastra } from "./mastra";
|
|
12
|
+
|
|
13
|
+
async function runContentWorkflow() {
|
|
14
|
+
console.log("🚀 Running workflow programmatically...\n");
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
// Get the workflow instance
|
|
18
|
+
const workflow = mastra.getWorkflow("contentWorkflow");
|
|
19
|
+
|
|
20
|
+
if (!workflow) {
|
|
21
|
+
throw new Error("Workflow not found");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Create a run instance
|
|
25
|
+
const run = workflow.createRun();
|
|
26
|
+
|
|
27
|
+
// Execute with test data
|
|
28
|
+
const result = await run.start({
|
|
29
|
+
inputData: {
|
|
30
|
+
content:
|
|
31
|
+
"Climate change is one of the most pressing challenges of our time, requiring immediate action from governments, businesses, and individuals worldwide.",
|
|
32
|
+
type: "blog",
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (result.status === "success") {
|
|
37
|
+
console.log("✅ Success!");
|
|
38
|
+
console.log(
|
|
39
|
+
"📊 Reading time:",
|
|
40
|
+
result.result.metadata.readingTime,
|
|
41
|
+
"minutes",
|
|
42
|
+
);
|
|
43
|
+
console.log("🎯 Difficulty:", result.result.metadata.difficulty);
|
|
44
|
+
console.log("📅 Processed at:", result.result.metadata.processedAt);
|
|
45
|
+
}
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.error("❌ Error:", (error as Error).message);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Run the workflow
|
|
52
|
+
runContentWorkflow();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Running the Code
|
|
56
|
+
|
|
57
|
+
Execute your workflow runner:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npx tsx src/run-workflow.ts
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Key Methods
|
|
64
|
+
|
|
65
|
+
- **`mastra.getWorkflow(id)`**: Gets a registered workflow by ID
|
|
66
|
+
- **`workflow.createRun()`**: Creates a new execution instance
|
|
67
|
+
- **`run.start(inputData)`**: Executes the workflow with provided data
|
|
68
|
+
|
|
69
|
+
## Return Value
|
|
70
|
+
|
|
71
|
+
The `start()` method returns:
|
|
72
|
+
|
|
73
|
+
- **`success`**: Boolean indicating if workflow completed successfully
|
|
74
|
+
- **`result`**: The final output from the workflow
|
|
75
|
+
- **`executionTime`**: How long the workflow took to run
|
|
76
|
+
|
|
77
|
+
Your workflow can now be run from anywhere in your application! Next, you'll learn about error handling.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Adding a Third Step
|
|
2
|
+
|
|
3
|
+
Let's extend your workflow by adding a third step that generates a summary of the content.
|
|
4
|
+
|
|
5
|
+
## Creating the Summary Step
|
|
6
|
+
|
|
7
|
+
Add this new step to your workflow file:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
const generateSummaryStep = createStep({
|
|
11
|
+
id: "generate-summary",
|
|
12
|
+
description: "Creates a summary of the content",
|
|
13
|
+
inputSchema: z.object({
|
|
14
|
+
content: z.string(),
|
|
15
|
+
type: z.string(),
|
|
16
|
+
wordCount: z.number(),
|
|
17
|
+
metadata: z.object({
|
|
18
|
+
readingTime: z.number(),
|
|
19
|
+
difficulty: z.enum(["easy", "medium", "hard"]),
|
|
20
|
+
processedAt: z.string(),
|
|
21
|
+
}),
|
|
22
|
+
}),
|
|
23
|
+
outputSchema: z.object({
|
|
24
|
+
content: z.string(),
|
|
25
|
+
type: z.string(),
|
|
26
|
+
wordCount: z.number(),
|
|
27
|
+
metadata: z.object({
|
|
28
|
+
readingTime: z.number(),
|
|
29
|
+
difficulty: z.enum(["easy", "medium", "hard"]),
|
|
30
|
+
processedAt: z.string(),
|
|
31
|
+
}),
|
|
32
|
+
summary: z.string(),
|
|
33
|
+
}),
|
|
34
|
+
execute: async ({ inputData }) => {
|
|
35
|
+
const { content, type, wordCount, metadata } = inputData;
|
|
36
|
+
|
|
37
|
+
// Create a simple summary from first sentence
|
|
38
|
+
const sentences = content
|
|
39
|
+
.split(/[.!?]+/)
|
|
40
|
+
.filter((s) => s.trim().length > 0);
|
|
41
|
+
const firstSentence = sentences[0]?.trim() + ".";
|
|
42
|
+
|
|
43
|
+
// Generate summary based on content length
|
|
44
|
+
let summary = firstSentence;
|
|
45
|
+
if (wordCount > 50) {
|
|
46
|
+
summary += ` This ${type} contains ${wordCount} words and takes approximately ${metadata.readingTime} minute(s) to read.`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
console.log(`📝 Generated summary: ${summary.length} characters`);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
content,
|
|
53
|
+
type,
|
|
54
|
+
wordCount,
|
|
55
|
+
metadata,
|
|
56
|
+
summary,
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Understanding the Pattern
|
|
63
|
+
|
|
64
|
+
Notice how this step:
|
|
65
|
+
|
|
66
|
+
- Takes the output from the previous step as input
|
|
67
|
+
- Adds new data (`summary`) while preserving existing data
|
|
68
|
+
- Follows the same structure as other steps
|
|
69
|
+
|
|
70
|
+
Your third step is ready! Next, you'll update the workflow to include all three steps.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Updating the Workflow
|
|
2
|
+
|
|
3
|
+
Now you'll update your workflow to include all three steps: validate, enhance, and summarize.
|
|
4
|
+
|
|
5
|
+
## Updating the Workflow Definition
|
|
6
|
+
|
|
7
|
+
Replace your existing workflow with this updated version:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
export const contentWorkflow = createWorkflow({
|
|
11
|
+
id: "content-processing-workflow",
|
|
12
|
+
description: "Validates, enhances, and summarizes content",
|
|
13
|
+
inputSchema: z.object({
|
|
14
|
+
content: z.string(),
|
|
15
|
+
type: z.enum(["article", "blog", "social"]).default("article"),
|
|
16
|
+
}),
|
|
17
|
+
outputSchema: z.object({
|
|
18
|
+
content: z.string(),
|
|
19
|
+
type: z.string(),
|
|
20
|
+
wordCount: z.number(),
|
|
21
|
+
metadata: z.object({
|
|
22
|
+
readingTime: z.number(),
|
|
23
|
+
difficulty: z.enum(["easy", "medium", "hard"]),
|
|
24
|
+
processedAt: z.string(),
|
|
25
|
+
}),
|
|
26
|
+
summary: z.string(),
|
|
27
|
+
}),
|
|
28
|
+
})
|
|
29
|
+
.then(validateContentStep)
|
|
30
|
+
.then(enhanceContentStep)
|
|
31
|
+
.then(generateSummaryStep)
|
|
32
|
+
.commit();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## What Changed
|
|
36
|
+
|
|
37
|
+
- **Description**: Updated to reflect the new functionality
|
|
38
|
+
- **Output Schema**: Now includes the `summary` field
|
|
39
|
+
- **Steps**: Added the third step to the chain
|
|
40
|
+
|
|
41
|
+
## Testing the Updated Workflow
|
|
42
|
+
|
|
43
|
+
You can now test this workflow in the playground to validate it works as expected.
|
|
44
|
+
|
|
45
|
+
## The Complete Flow
|
|
46
|
+
|
|
47
|
+
Your workflow now:
|
|
48
|
+
|
|
49
|
+
1. **Validates** content and counts words
|
|
50
|
+
2. **Enhances** with metadata like reading time and difficulty
|
|
51
|
+
3. **Summarizes** the content for quick understanding
|
|
52
|
+
|
|
53
|
+
Each step builds on the previous one, creating a comprehensive content processing pipeline!
|
|
54
|
+
|
|
55
|
+
Next, you'll learn about using workflows with agents.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Creating an AI Agent
|
|
2
|
+
|
|
3
|
+
Learn how to create an Mastra agent that can be used within your workflows for more intelligent content processing.
|
|
4
|
+
|
|
5
|
+
## Creating a Content Analysis Agent
|
|
6
|
+
|
|
7
|
+
Create a new file for your agent in the `src/mastra/agents` directory. Use `content-agent.ts` as the name of the file with the following contents:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
// src/mastra/agents/content-agent.ts
|
|
11
|
+
import { openai } from "@ai-sdk/openai";
|
|
12
|
+
import { Agent } from "@mastra/core/agent";
|
|
13
|
+
|
|
14
|
+
export const contentAgent = new Agent({
|
|
15
|
+
name: "Content Agent",
|
|
16
|
+
description: "AI agent for analyzing and improving content",
|
|
17
|
+
instructions: `
|
|
18
|
+
You are a professional content analyst. Your role is to:
|
|
19
|
+
1. Analyze content for clarity and engagement
|
|
20
|
+
2. Identify the main themes and topics
|
|
21
|
+
3. Provide a quality score from 1-10
|
|
22
|
+
4. Suggest specific improvements
|
|
23
|
+
|
|
24
|
+
Always provide constructive, actionable feedback.
|
|
25
|
+
`,
|
|
26
|
+
model: openai("gpt-4o-mini"),
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Understanding the Agent
|
|
31
|
+
|
|
32
|
+
- **Name**: Unique identifier for the agent
|
|
33
|
+
- **Description**: What the agent does
|
|
34
|
+
- **Instructions**: Detailed prompts that guide the AI's behavior
|
|
35
|
+
- **Model**: Which AI model to use (GPT-4o-mini is fast and cost-effective)
|
|
36
|
+
|
|
37
|
+
## Registering and Testing Your Agent
|
|
38
|
+
|
|
39
|
+
Open your `src/mastra/index.ts` file and add your agent (you may need to append it to the agents object in the Mastra class):
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
// Import your workflow
|
|
43
|
+
import { contentAgent } from "./agents/content-agent";
|
|
44
|
+
|
|
45
|
+
export const mastra = new Mastra({
|
|
46
|
+
// Register your agent here
|
|
47
|
+
agents: {
|
|
48
|
+
contentAgent,
|
|
49
|
+
},
|
|
50
|
+
// ...Existing code
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
You can test this agent in the Playground by navigating to the Agents tab and selecting `content-agent`. Use the chat interface to validate the agent is working.
|
|
55
|
+
|
|
56
|
+
The agent should provide analysis of the content, including themes, quality assessment, and improvement suggestions.
|
|
57
|
+
|
|
58
|
+
## Why Use Agents in Workflows?
|
|
59
|
+
|
|
60
|
+
Agents add intelligence to workflows by:
|
|
61
|
+
|
|
62
|
+
- **Understanding context**: AI can interpret meaning, not just process data
|
|
63
|
+
- **Generating insights**: Provide analysis that simple logic cannot
|
|
64
|
+
- **Adapting responses**: Give different feedback based on content type
|
|
65
|
+
- **Natural language output**: Communicate results in human-readable form
|
|
66
|
+
|
|
67
|
+
Your AI agent is ready! Next, you'll learn how to integrate it into a workflow step.
|