@mastra/mcp-docs-server 0.13.2-alpha.2 → 0.13.2-alpha.3
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%2Fdynamodb.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +8 -8
- 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 +20 -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 +56 -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 +58 -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 +1 -1
|
@@ -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.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Using Agent in Workflow
|
|
2
|
+
|
|
3
|
+
Now you'll create a workflow step that uses your AI agent to provide intelligent content analysis.
|
|
4
|
+
|
|
5
|
+
In each step, in the execute function, you have access to the `mastra` class which provides you the ability to access Agents, Tools, and even other Workflows. In this case, we use the `mastra` class to get our agent and call that agent's `generate()` function.
|
|
6
|
+
|
|
7
|
+
## Creating an AI Analysis Step
|
|
8
|
+
|
|
9
|
+
Add this step to your workflow file:
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
const aiAnalysisStep = createStep({
|
|
13
|
+
id: "ai-analysis",
|
|
14
|
+
description: "AI-powered content analysis",
|
|
15
|
+
inputSchema: z.object({
|
|
16
|
+
content: z.string(),
|
|
17
|
+
type: z.string(),
|
|
18
|
+
wordCount: z.number(),
|
|
19
|
+
metadata: z.object({
|
|
20
|
+
readingTime: z.number(),
|
|
21
|
+
difficulty: z.enum(["easy", "medium", "hard"]),
|
|
22
|
+
processedAt: z.string(),
|
|
23
|
+
}),
|
|
24
|
+
summary: z.string(),
|
|
25
|
+
}),
|
|
26
|
+
outputSchema: z.object({
|
|
27
|
+
content: z.string(),
|
|
28
|
+
type: z.string(),
|
|
29
|
+
wordCount: z.number(),
|
|
30
|
+
metadata: z.object({
|
|
31
|
+
readingTime: z.number(),
|
|
32
|
+
difficulty: z.enum(["easy", "medium", "hard"]),
|
|
33
|
+
processedAt: z.string(),
|
|
34
|
+
}),
|
|
35
|
+
summary: z.string(),
|
|
36
|
+
aiAnalysis: z.object({
|
|
37
|
+
score: z.number(),
|
|
38
|
+
feedback: z.string(),
|
|
39
|
+
}),
|
|
40
|
+
}),
|
|
41
|
+
execute: async ({ inputData, mastra }) => {
|
|
42
|
+
const { content, type, wordCount, metadata, summary } = inputData;
|
|
43
|
+
|
|
44
|
+
// Create prompt for the AI agent
|
|
45
|
+
const prompt = `
|
|
46
|
+
Analyze this ${type} content:
|
|
47
|
+
|
|
48
|
+
Content: "${content}"
|
|
49
|
+
Word count: ${wordCount}
|
|
50
|
+
Reading time: ${metadata.readingTime} minutes
|
|
51
|
+
Difficulty: ${metadata.difficulty}
|
|
52
|
+
|
|
53
|
+
Please provide:
|
|
54
|
+
1. A quality score from 1-10
|
|
55
|
+
2. Brief feedback on strengths and areas for improvement
|
|
56
|
+
|
|
57
|
+
Format as JSON: {"score": number, "feedback": "your feedback here"}
|
|
58
|
+
`;
|
|
59
|
+
|
|
60
|
+
// Get the contentAgent from the mastra instance.
|
|
61
|
+
const contentAgent = mastra.getAgent("contentAgent");
|
|
62
|
+
const { text } = await contentAgent.generate([
|
|
63
|
+
{ role: "user", content: prompt },
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
// Parse AI response (with fallback)
|
|
67
|
+
let aiAnalysis;
|
|
68
|
+
try {
|
|
69
|
+
aiAnalysis = JSON.parse(text);
|
|
70
|
+
} catch {
|
|
71
|
+
aiAnalysis = {
|
|
72
|
+
score: 7,
|
|
73
|
+
feedback: "AI analysis completed. " + text,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
console.log(`🤖 AI Score: ${aiAnalysis.score}/10`);
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
content,
|
|
81
|
+
type,
|
|
82
|
+
wordCount,
|
|
83
|
+
metadata,
|
|
84
|
+
summary,
|
|
85
|
+
aiAnalysis,
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Your agent-powered step is ready! Next, you'll add it to your workflow for complete AI-enhanced content processing.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Creating AI-Enhanced Workflow
|
|
2
|
+
|
|
3
|
+
Now you'll create a new workflow that includes agent analysis alongside your existing content processing steps.
|
|
4
|
+
|
|
5
|
+
## Creating the Enhanced Workflow
|
|
6
|
+
|
|
7
|
+
Add this new workflow to your file:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
export const aiContentWorkflow = createWorkflow({
|
|
11
|
+
id: "ai-content-workflow",
|
|
12
|
+
description: "AI-enhanced content processing with analysis",
|
|
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
|
+
aiAnalysis: z.object({
|
|
28
|
+
score: z.number(),
|
|
29
|
+
feedback: z.string(),
|
|
30
|
+
}),
|
|
31
|
+
}),
|
|
32
|
+
})
|
|
33
|
+
.then(validateContentStep)
|
|
34
|
+
.then(enhanceContentStep)
|
|
35
|
+
.then(generateSummaryStep)
|
|
36
|
+
.then(aiAnalysisStep)
|
|
37
|
+
.commit();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Registering the New Workflow
|
|
41
|
+
|
|
42
|
+
Update your Mastra configuration to include both workflows and ensure the contentAgent has been added.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
// In src/mastra/index.ts
|
|
46
|
+
import {
|
|
47
|
+
contentWorkflow,
|
|
48
|
+
aiContentWorkflow,
|
|
49
|
+
} from "./workflows/content-workflow";
|
|
50
|
+
import { contentAgent } from "./agents/content-agent";
|
|
51
|
+
|
|
52
|
+
export const mastra = new Mastra({
|
|
53
|
+
workflows: {
|
|
54
|
+
contentWorkflow,
|
|
55
|
+
aiContentWorkflow, // Add the AI-enhanced version
|
|
56
|
+
},
|
|
57
|
+
agents: { contentAgent },
|
|
58
|
+
// ... rest of configuration
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Testing the Agent-Enhanced Workflow
|
|
63
|
+
|
|
64
|
+
You can now access this new Workflow inside the Mastra playground. Select this new `ai-content-workflow` workflow from the Workflows tab and run a test to validate it works as expected.
|
|
65
|
+
|
|
66
|
+
## The Complete AI Pipeline
|
|
67
|
+
|
|
68
|
+
Your AI-enhanced workflow now:
|
|
69
|
+
|
|
70
|
+
1. **Validates** content and counts words
|
|
71
|
+
2. **Enhances** with metadata
|
|
72
|
+
3. **Summarizes** the content
|
|
73
|
+
4. **Analyzes** with AI for quality scoring and feedback
|
|
74
|
+
|
|
75
|
+
This creates a comprehensive, AI-powered content processing system! Next, you'll learn about parallel execution.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Understanding Parallel Execution
|
|
2
|
+
|
|
3
|
+
Learn how to run multiple workflow steps simultaneously to improve performance when steps don't depend on each other.
|
|
4
|
+
|
|
5
|
+
## When to Use Parallel Execution
|
|
6
|
+
|
|
7
|
+
Use parallel execution when you have steps that:
|
|
8
|
+
|
|
9
|
+
- **Don't depend on each other**: Can run independently
|
|
10
|
+
- **Take time**: Network requests, AI calls, or heavy computations
|
|
11
|
+
- **Process the same input**: Multiple analyses of the same data
|
|
12
|
+
|
|
13
|
+
## Example Scenario
|
|
14
|
+
|
|
15
|
+
Imagine you want to analyze content in three different ways:
|
|
16
|
+
|
|
17
|
+
1. SEO analysis
|
|
18
|
+
2. Readability analysis
|
|
19
|
+
3. Sentiment analysis
|
|
20
|
+
|
|
21
|
+
These can all run at the same time since they don't depend on each other!
|
|
22
|
+
|
|
23
|
+
## Creating Parallel Steps
|
|
24
|
+
|
|
25
|
+
The .parallel() method on a workflow executes multiple steps in parallel.
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
workflow.parallel([stepOne, stepTwo]);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Performance Benefits
|
|
32
|
+
|
|
33
|
+
Running steps in parallel:
|
|
34
|
+
|
|
35
|
+
- **Faster execution**: Steps run simultaneously instead of waiting
|
|
36
|
+
- **Improved user experience**: Shorter wait times
|
|
37
|
+
|
|
38
|
+
Next, you'll create the other parallel steps and see how to combine them!
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# Creating Parallel Steps
|
|
2
|
+
|
|
3
|
+
Let's create three analysis steps that can run simultaneously to analyze different aspects of content.
|
|
4
|
+
|
|
5
|
+
## Creating the Analysis Steps
|
|
6
|
+
|
|
7
|
+
Add these three steps to your workflow file:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
// SEO Analysis
|
|
11
|
+
const seoAnalysisStep = createStep({
|
|
12
|
+
id: "seo-analysis",
|
|
13
|
+
description: "SEO optimization analysis",
|
|
14
|
+
inputSchema: z.object({
|
|
15
|
+
content: z.string(),
|
|
16
|
+
type: z.enum(["article", "blog", "social"]).default("article"),
|
|
17
|
+
}),
|
|
18
|
+
outputSchema: z.object({
|
|
19
|
+
seoScore: z.number(),
|
|
20
|
+
keywords: z.array(z.string()),
|
|
21
|
+
}),
|
|
22
|
+
execute: async ({ inputData }) => {
|
|
23
|
+
console.log("🔍 Running SEO analysis...");
|
|
24
|
+
await new Promise((resolve) => setTimeout(resolve, 800));
|
|
25
|
+
|
|
26
|
+
const words = inputData.content.toLowerCase().split(/\s+/);
|
|
27
|
+
const keywords = words.filter((word) => word.length > 4).slice(0, 3);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
seoScore: Math.floor(Math.random() * 40) + 60,
|
|
31
|
+
keywords,
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Readability Analysis
|
|
37
|
+
const readabilityStep = createStep({
|
|
38
|
+
id: "readability-analysis",
|
|
39
|
+
description: "Content readability analysis",
|
|
40
|
+
inputSchema: z.object({
|
|
41
|
+
content: z.string(),
|
|
42
|
+
type: z.enum(["article", "blog", "social"]).default("article"),
|
|
43
|
+
}),
|
|
44
|
+
outputSchema: z.object({
|
|
45
|
+
readabilityScore: z.number(),
|
|
46
|
+
gradeLevel: z.string(),
|
|
47
|
+
}),
|
|
48
|
+
execute: async ({ inputData }) => {
|
|
49
|
+
console.log("📖 Running readability analysis...");
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, 600));
|
|
51
|
+
|
|
52
|
+
const sentences = inputData.content.split(/[.!?]+/).length;
|
|
53
|
+
const words = inputData.content.split(/\s+/).length;
|
|
54
|
+
const avgWordsPerSentence = words / sentences;
|
|
55
|
+
|
|
56
|
+
const score = Math.max(0, 100 - avgWordsPerSentence * 3);
|
|
57
|
+
const gradeLevel = score > 80 ? "Easy" : score > 60 ? "Medium" : "Hard";
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
readabilityScore: Math.floor(score),
|
|
61
|
+
gradeLevel,
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Sentiment Analysis
|
|
67
|
+
const sentimentStep = createStep({
|
|
68
|
+
id: "sentiment-analysis",
|
|
69
|
+
description: "Content sentiment analysis",
|
|
70
|
+
inputSchema: z.object({
|
|
71
|
+
content: z.string(),
|
|
72
|
+
type: z.enum(["article", "blog", "social"]).default("article"),
|
|
73
|
+
}),
|
|
74
|
+
outputSchema: z.object({
|
|
75
|
+
sentiment: z.enum(["positive", "neutral", "negative"]),
|
|
76
|
+
confidence: z.number(),
|
|
77
|
+
}),
|
|
78
|
+
execute: async ({ inputData }) => {
|
|
79
|
+
console.log("😊 Running sentiment analysis...");
|
|
80
|
+
await new Promise((resolve) => setTimeout(resolve, 700));
|
|
81
|
+
|
|
82
|
+
const content = inputData.content.toLowerCase();
|
|
83
|
+
const positiveWords = ["good", "great", "excellent", "amazing"];
|
|
84
|
+
const negativeWords = ["bad", "terrible", "awful", "horrible"];
|
|
85
|
+
|
|
86
|
+
const positive = positiveWords.filter((word) =>
|
|
87
|
+
content.includes(word),
|
|
88
|
+
).length;
|
|
89
|
+
const negative = negativeWords.filter((word) =>
|
|
90
|
+
content.includes(word),
|
|
91
|
+
).length;
|
|
92
|
+
|
|
93
|
+
let sentiment: "positive" | "neutral" | "negative" = "neutral";
|
|
94
|
+
if (positive > negative) sentiment = "positive";
|
|
95
|
+
if (negative > positive) sentiment = "negative";
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
sentiment,
|
|
99
|
+
confidence: Math.random() * 0.3 + 0.7, // 0.7-1.0
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Notice the Timing
|
|
106
|
+
|
|
107
|
+
Each step has a different simulated processing time:
|
|
108
|
+
|
|
109
|
+
- SEO: 800ms
|
|
110
|
+
- Readability: 600ms
|
|
111
|
+
- Sentiment: 700ms
|
|
112
|
+
|
|
113
|
+
When run sequentially, total time would be ~2.2 seconds. When run in parallel, total time will be ~800ms (the longest step)!
|
|
114
|
+
|
|
115
|
+
Next, you'll learn how to run these steps in parallel using the `.parallel()` method.
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# Building Parallel Workflow
|
|
2
|
+
|
|
3
|
+
Now you'll create a workflow that runs your analysis steps in parallel for maximum performance.
|
|
4
|
+
|
|
5
|
+
## Creating the Parallel Workflow
|
|
6
|
+
|
|
7
|
+
Add this workflow to your file:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
export const parallelAnalysisWorkflow = createWorkflow({
|
|
11
|
+
id: "parallel-analysis-workflow",
|
|
12
|
+
description: "Run multiple content analyses in parallel",
|
|
13
|
+
inputSchema: z.object({
|
|
14
|
+
content: z.string(),
|
|
15
|
+
type: z.enum(["article", "blog", "social"]).default("article"),
|
|
16
|
+
}),
|
|
17
|
+
outputSchema: z.object({
|
|
18
|
+
results: z.object({
|
|
19
|
+
seo: z.object({
|
|
20
|
+
seoScore: z.number(),
|
|
21
|
+
keywords: z.array(z.string()),
|
|
22
|
+
}),
|
|
23
|
+
readability: z.object({
|
|
24
|
+
readabilityScore: z.number(),
|
|
25
|
+
gradeLevel: z.string(),
|
|
26
|
+
}),
|
|
27
|
+
sentiment: z.object({
|
|
28
|
+
sentiment: z.enum(["positive", "neutral", "negative"]),
|
|
29
|
+
confidence: z.number(),
|
|
30
|
+
}),
|
|
31
|
+
}),
|
|
32
|
+
}),
|
|
33
|
+
})
|
|
34
|
+
.parallel([seoAnalysisStep, readabilityStep, sentimentStep])
|
|
35
|
+
.then(
|
|
36
|
+
createStep({
|
|
37
|
+
id: "combine-results",
|
|
38
|
+
description: "Combines parallel analysis results",
|
|
39
|
+
inputSchema: z.object({
|
|
40
|
+
"seo-analysis": z.object({
|
|
41
|
+
seoScore: z.number(),
|
|
42
|
+
keywords: z.array(z.string()),
|
|
43
|
+
}),
|
|
44
|
+
"readability-analysis": z.object({
|
|
45
|
+
readabilityScore: z.number(),
|
|
46
|
+
gradeLevel: z.string(),
|
|
47
|
+
}),
|
|
48
|
+
"sentiment-analysis": z.object({
|
|
49
|
+
sentiment: z.enum(["positive", "neutral", "negative"]),
|
|
50
|
+
confidence: z.number(),
|
|
51
|
+
}),
|
|
52
|
+
}),
|
|
53
|
+
outputSchema: z.object({
|
|
54
|
+
results: z.object({
|
|
55
|
+
seo: z.object({
|
|
56
|
+
seoScore: z.number(),
|
|
57
|
+
keywords: z.array(z.string()),
|
|
58
|
+
}),
|
|
59
|
+
readability: z.object({
|
|
60
|
+
readabilityScore: z.number(),
|
|
61
|
+
gradeLevel: z.string(),
|
|
62
|
+
}),
|
|
63
|
+
sentiment: z.object({
|
|
64
|
+
sentiment: z.enum(["positive", "neutral", "negative"]),
|
|
65
|
+
confidence: z.number(),
|
|
66
|
+
}),
|
|
67
|
+
}),
|
|
68
|
+
}),
|
|
69
|
+
execute: async ({ inputData }) => {
|
|
70
|
+
console.log("🔄 Combining parallel results...");
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
results: {
|
|
74
|
+
seo: inputData["seo-analysis"],
|
|
75
|
+
readability: inputData["readability-analysis"],
|
|
76
|
+
sentiment: inputData["sentiment-analysis"],
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
}),
|
|
81
|
+
)
|
|
82
|
+
.commit();
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Understanding Parallel Data Flow
|
|
86
|
+
|
|
87
|
+
When steps run in parallel:
|
|
88
|
+
|
|
89
|
+
1. Each step receives the same input data
|
|
90
|
+
2. Steps execute simultaneously
|
|
91
|
+
3. Results are collected into an object with step IDs as keys
|
|
92
|
+
4. The next step receives all parallel results
|
|
93
|
+
|
|
94
|
+
## Key Points
|
|
95
|
+
|
|
96
|
+
- **`.parallel([step1, step2, step3])`**: Runs all steps simultaneously
|
|
97
|
+
- **Result object keys**: Use the step IDs (e.g., "seo-analysis")
|
|
98
|
+
- **Combine step**: Processes all parallel results together
|
|
99
|
+
|
|
100
|
+
Next, you'll test this parallel workflow and see the performance improvement!
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Testing Parallel Workflow
|
|
2
|
+
|
|
3
|
+
Let's test your parallel workflow.
|
|
4
|
+
|
|
5
|
+
## Registering the New Workflow
|
|
6
|
+
|
|
7
|
+
Update your Mastra configuration to include your new workflow workflows:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
// In src/mastra/index.ts
|
|
11
|
+
import {
|
|
12
|
+
contentWorkflow,
|
|
13
|
+
aiContentWorkflow,
|
|
14
|
+
parallelAnalysisWorkflow,
|
|
15
|
+
} from "./workflows/content-workflow";
|
|
16
|
+
|
|
17
|
+
export const mastra = new Mastra({
|
|
18
|
+
workflows: {
|
|
19
|
+
contentWorkflow,
|
|
20
|
+
aiContentWorkflow,
|
|
21
|
+
parallelAnalysisWorkflow, // Add the parallel workflow
|
|
22
|
+
},
|
|
23
|
+
// ... rest of configuration
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Testing the Parallel Workflow
|
|
28
|
+
|
|
29
|
+
You can now test this new workflow in the Playground. You will notice that it processes the three analysis steps in parallel speeding up execution time.
|
|
30
|
+
|
|
31
|
+
## When to Use Parallel Execution
|
|
32
|
+
|
|
33
|
+
Use parallel execution when:
|
|
34
|
+
|
|
35
|
+
- Steps don't depend on each other's outputs
|
|
36
|
+
- Steps involve I/O operations (API calls, database queries)
|
|
37
|
+
- You want to maximize performance
|
|
38
|
+
- Steps process the same input data
|
|
39
|
+
|
|
40
|
+
Register your parallel workflow with Mastra to use it in the playground! Next, you'll learn about conditional branching.
|