58 lines
1.5 KiB
Markdown
58 lines
1.5 KiB
Markdown
# 创建您的第一个步骤
|
|
|
|
让我们创建您的第一个工作流步骤!我们将构建一个验证文本内容的步骤。
|
|
|
|
## 设置
|
|
|
|
首先,在 `src/mastra/workflows` 目录中为工作流创建一个新文件。让我们将此文件命名为 `content-workflow.ts`
|
|
|
|
## 创建验证步骤
|
|
|
|
将此代码添加到您的工作流文件中:
|
|
|
|
```typescript
|
|
import { createStep } from "@mastra/core/workflows";
|
|
import { z } from "zod";
|
|
|
|
const validateContentStep = createStep({
|
|
id: "validate-content",
|
|
description: "Validates incoming text content",
|
|
inputSchema: z.object({
|
|
content: z.string().min(1, "Content cannot be empty"),
|
|
type: z.enum(["article", "blog", "social"]).default("article"),
|
|
}),
|
|
outputSchema: z.object({
|
|
content: z.string(),
|
|
type: z.string(),
|
|
wordCount: z.number(),
|
|
isValid: z.boolean(),
|
|
}),
|
|
execute: async ({ inputData }) => {
|
|
const { content, type } = inputData;
|
|
|
|
const wordCount = content.trim().split(/\s+/).length;
|
|
const isValid = wordCount >= 5; // Minimum 5 words
|
|
|
|
if (!isValid) {
|
|
throw new Error(`Content too short: ${wordCount} words`);
|
|
}
|
|
|
|
return {
|
|
content: content.trim(),
|
|
type,
|
|
wordCount,
|
|
isValid,
|
|
};
|
|
},
|
|
});
|
|
```
|
|
|
|
## 理解代码
|
|
|
|
- **ID**:此步骤的唯一标识符
|
|
- **输入模式**:期望 `content`(字符串)和可选的 `type`
|
|
- **输出模式**:返回内容、类型、字数和验证状态
|
|
- **执行**:包含验证逻辑
|
|
|
|
您的第一个步骤已准备就绪!接下来,您将测试它以确保它正常工作。
|