工作流
工作流是一种将工作分发到多个独立子智能体的编排模式,用于研究、审计、迁移或多角度验证等任务。与普通的子智能体委托不同,工作流允许你编写一个脚本来协调许多智能体,具有阶段和结构化结果。
工作流与子智能体
工作流和子智能体都涉及委托工作,但适用于不同的用例:
**子智能体(task()/subagent())**适用于:
- 一两个委托
- 简单的父子关系
- 顺序或少量并行执行
- 直接在智能体函数中编写的逻辑
工作流适用于:
- 许多并行任务
- 多阶段流水线
- 需要脚本化编排的复杂协调
- 具有进度跟踪的结构化结果
如果你的工作可以分解为一两个独立任务,请使用子智能体。如果你需要编排一个涉及许多智能体、阶段或并行路径的复杂过程,请使用工作流。
创建工作流
使用 workflow() hook 创建工作流。在你的智能体函数内部调用它:
import { workflow } from '@flue/runtime';
export function Auditor() {
const result = workflow({
name: 'codebase-audit',
description: 'Audit the codebase for security, performance, and style issues.',
phases: [
{ title: 'Security audit' },
{ title: 'Performance audit' },
{ title: 'Style audit' },
],
script: async ({ agent, parallel, phase }) => {
phase('Security audit');
const securityIssues = await agent('Find security vulnerabilities in the codebase.');
phase('Performance audit');
const perfIssues = await agent('Find performance bottlenecks in the codebase.');
phase('Style audit');
const styleIssues = await agent('Find style inconsistencies in the codebase.');
return { securityIssues, perfIssues, styleIssues };
},
});
return 'Audit complete. Review the findings in the workspace.';
}
工作流脚本
工作流脚本是一个 JavaScript 函数,它接收一个包含以下工具的对象:
agent(prompt, opts?) —— 运行一个子智能体到完成。返回智能体的最终文本,或使用 opts.schema 进行结构化输出。
parallel(thunks) —— 并行运行零参数函数并等待所有完成。
pipeline(items, ...stages) —— 独立地将每个项目通过各个阶段运行。
phase(title) —— 开始一个进度阶段。
log(message) —— 记录进度消息。
args —— 工具调用的 args 输入,原样传递。
使用智能体
agent() 函数运行一个子智能体到完成:
const result = await agent('Analyze the security of this codebase.');
对于结构化输出,传递一个 JSON Schema:
const vulnerabilities = await agent(
'Find security vulnerabilities.',
{
schema: {
type: 'object',
properties: {
issues: {
type: 'array',
items: { type: 'string' },
},
},
required: ['issues'],
},
}
);
// vulnerabilities 是一个对象 { issues: string[] }
使用 label 选项为智能体提供显示名称:
const result = await agent('Analyze code quality.', {
label: 'Code quality analyzer',
});
并行执行
parallel() 函数并行运行多个任务:
const [security, performance, style] = await parallel([
() => agent('Find security issues.'),
() => agent('Find performance issues.'),
() => agent('Find style issues.'),
]);
所有智能体同时启动,parallel() 在所有完成时返回。如果一个智能体失败,它返回 null。
流水线处理
pipeline() 函数将每个项目独立地通过各个阶段:
const files = ['auth.ts', 'api.ts', 'db.ts'];
const results = await pipeline(
files,
async (prev, file, index) => agent(`Analyze ${file} for security issues.`),
async (prev, file, index) => agent(`Based on ${prev}, suggest improvements for ${file}.`),
);
每个文件独立地通过阶段,没有阶段之间的屏障。如果一个项目在某个阶段失败,该项目被丢弃并跳过其剩余阶段。
阶段和进度
使用 phase() 来组织工作流进度:
script: async ({ agent, phase, log }) => {
phase('Research');
log('Gathering data...');
const data = await agent('Research the topic.');
phase('Analysis');
log('Analyzing findings...');
const analysis = await agent('Analyze the data.');
phase('Report');
log('Generating report...');
await agent('Write a report based on the analysis.');
return { data, analysis };
};
阶段在进度 UI 中分组显示。在阶段内使用 log() 来提供详细更新。
错误处理
工作流脚本中的错误会杀死整个工作流。单个智能体失败返回 null(使用 .filter(Boolean) 过滤它们)。
const results = await parallel([
() => agent('Task 1.'),
() => agent('Task 2.'),
() => agent('Task 3.'),
]);
// 过滤掉失败的智能体
const successful = results.filter(Boolean);
工作流结果
工作流脚本必须返回一个 JSON 可序列化的值。这成为工作流的结果:
script: async ({ agent }) => {
const issues = await agent('Find all issues.');
const suggestions = await agent('Suggest improvements.');
return {
issues,
suggestions,
timestamp: new Date().toISOString(),
};
};
工作流选项
workflow() hook 接受以下选项:
name —— 工作流的短横线名称(必需)。
description —— 工作流的一行描述(必需)。
phases —— 阶段声明数组,phase() 调用按精确字符串匹配。
args —— 可选的 JSON 输入,作为 args 全局变量暴露给脚本。
whenToUse —— 可选的指导,说明何时使用此工作流。
工作流模式
多角度研究。 从不同角度研究一个主题(例如,技术、商业、法律),然后综合结果。
代码审计。 并行审计代码库的不同方面(安全、性能、风格)。
数据处理。 流水线处理文件列表,每个文件经过多个阶段(提取、转换、加载)。
文档生成。 并行生成文档的多个部分,然后组合它们。
测试生成。 为多个模块并行生成测试,然后运行它们。
限制和注意事项
并发限制。 工作流受并发和总智能体上限的约束。过多的并行智能体可能达到平台限制。
无文件系统访问。 工作流脚本不能直接访问文件系统;智能体执行文件操作。
无网络访问。 工作流脚本不能进行网络请求;智能体使用工具进行网络访问。
JSON 输出。 工作流结果必须是 JSON 可序列化的。