translation/translated/documents/course/04-workflows/20-building-conditional-wor...

61 lines
1.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 构建条件工作流
现在您将创建一个使用条件分支通过两条不同处理路径路由内容的工作流。
## 创建条件工作流
将此工作流添加到您的文件中:
```typescript
export const conditionalWorkflow = createWorkflow({
id: "conditional-workflow",
description: "Content processing with conditional branching",
inputSchema: z.object({
content: z.string(),
type: z.enum(["article", "blog", "social"]).default("article"),
}),
outputSchema: z.object({
processedContent: z.string(),
processingType: z.string(),
recommendations: z.array(z.string()),
}),
})
.then(assessContentStep)
.branch([
// Branch 1: Short and simple content
[
async ({ inputData }) =>
inputData.category === "short" && inputData.complexity === "simple",
quickProcessingStep,
],
// Branch 2: Everything else
[
async ({ inputData }) =>
!(inputData.category === "short" && inputData.complexity === "simple"),
generalProcessingStep,
],
])
.commit();
```
## 理解条件
1. **短+简单**:最少的建议进行快速处理
2. **其他所有内容**:更多建议的通用处理
## 多个条件
您可以使用逻辑运算符组合条件:
- **`&&`**AND - 两个条件都必须为true
- **`||`**OR - 任何一个条件可以为true
- **`!`**NOT - 条件必须为false
## 条件评估
- 条件按顺序检查
- 多个条件可以为true步骤并行运行
- 如果没有条件匹配,则跳过分支
接下来,您将使用不同类型的内容测试此条件工作流!