章节 01
导读 / 主楼:AgentFlow DSL:用声明式语法构建多智能体工作流的新范式
AgentFlow DSL 是一种专为多智能体 AI 工作流设计的声明式语言,开发者只需编写简洁的 .aflow 文件即可定义复杂的多智能体协作流程,无需编写集成代码即可直接转换为 MCP 工具在 Claude Code 中使用。
正文
AgentFlow DSL 是一种专为多智能体 AI 工作流设计的声明式语言,开发者只需编写简洁的 .aflow 文件即可定义复杂的多智能体协作流程,无需编写集成代码即可直接转换为 MCP 工具在 Claude Code 中使用。
章节 01
AgentFlow DSL 是一种专为多智能体 AI 工作流设计的声明式语言,开发者只需编写简洁的 .aflow 文件即可定义复杂的多智能体协作流程,无需编写集成代码即可直接转换为 MCP 工具在 Claude Code 中使用。
章节 02
章节 03
原作者与来源
aflow\nagent writer\n model: \"local-fast\"\n mode: focused\n must_produce:\n - draft\n - word_count: int\n\n\n支持的智能体模式包括:\n- focused:专注模式,适合执行具体任务\n- adversarial:对抗模式,适合审查和批评\n- reliable:可靠模式,追求稳定输出\n- precise:精确模式,注重准确性\n- strict:严格模式,遵循规则执行\n- patient:耐心模式,适合研究性任务\n- objective:客观模式,追求中立评估\n\n阶段(Phases)\n\n阶段定义了工作流的执行步骤,每个阶段指定由哪个智能体执行、输入什么、输出什么:\n\naflow\nphase research\n agent: researcher\n input: [trigger.topic]\n output: [outline, key_points]\n\n\n循环(Loops)\n\n循环机制支持迭代优化,直到满足特定条件为止:\n\naflow\nloop revision_cycle\n phases: [write, edit]\n repeat_while: edit.verdict == \"needs_work\"\n max_iterations: 3\n on_each_iteration:\n send_to: writer\n payload: edit.suggestions\n\n\n典型应用场景\n\nAgentFlow DSL 适用于多种需要多智能体协作的场景:\n\n内容创作工作流\n\n以博客文章生成为例,工作流可以设计为:研究者 → 写作者 → 编辑者,并在编辑不通过时循环迭代:\n\naflow\nworkflow blog_post\n description: \"Generate and refine a blog post\"\n version: \"1.0.0\"\n\n agents:\n agent researcher\n mode: patient\n must_produce:\n - outline\n - key_points\n\n agent writer\n mode: focused\n must_produce:\n - draft\n - word_count: int\n\n agent editor\n mode: adversarial\n must_produce:\n - verdict\n - suggestions\n - confidence: float\n\n phases:\n phase research\n agent: researcher\n input: [trigger.topic]\n output: [outline, key_points]\n\n phase write\n agent: writer\n input: [research.outline, research.key_points]\n output: [draft, word_count]\n\n phase edit\n agent: editor\n input: [write.draft]\n output: [verdict, suggestions, confidence]\n\n loop revision_cycle\n phases: [write, edit]\n repeat_while: edit.verdict == \"needs_work\"\n max_iterations: 3\n on_each_iteration:\n send_to: writer\n payload: edit.suggestions\n\n done when: edit.confidence >= 0.8 and edit.verdict == \"approved\"\n\n\n代码质量审查\n\n代码审查工作流可以设计为:写作者 → 测试者 → 批评者,直到质量达标:\n\naflow\nworkflow code_quality\n description: \"Iterative code review with writer, tester, and critic\"\n version: \"1.0.0\"\n\n agents:\n agent writer → model: \"local-fast\"\n agent tester → model: \"openrouter-smart\"\n agent critic → model: \"claude-sonnet\"\n\n loop quality_gate\n phases: [write, test, review]\n repeat_while: review.verdict == \"needs_work\"\n max_iterations: 5\n\n done when: review.confidence >= 0.85\n\n\n支持的模型提供商\n\nAgentFlow 目前支持三大主流模型提供商:\n\n| 提供商 | 状态 | 说明 |\n|--------|------|------|\n| Claude (Anthropic) | ✅ | 原生 SDK 支持,支持多轮工具调用 |\n| OpenRouter | ✅ | 支持 315+ 模型,自动路由提供商 |\n| Ollama | ✅ | 本地执行,无需 API 密钥 |\n\n开发者可以配置模型别名来实现成本优化策略,例如使用便宜模型进行初稿生成,使用前沿模型进行最终审查:\n\njson\n{\n \"models\": {\n \"local-fast\": { \"provider\": \"ollama\", \"model\": \"qwen3:8b\" },\n \"openrouter-smart\": { \"provider\": \"openrouter\", \"model\": \"google/gemini-2.5-flash\" },\n \"claude-sonnet\": { \"provider\": \"claude\", \"model\": \"claude-sonnet-4-5\" }\n }\n}\n\n\n技术架构与实现\n\nAgentFlow 的技术架构清晰分层,从 .aflow 文件到最终执行经历了以下流程:\n\n\n.aflow file\n │\n ▼\n Tokenizer ──► Parser ──► Compiler (AST → IR)\n │\n ┌─────────▼──────────┐\n │ Validator (S1-S10) │\n └─────────┬──────────┘\n │\n ┌─────────▼──────────┐\n │ WorkflowRunner │\n │ ┌───────────────┐ │\n │ │ ExecutorResolver│ │\n │ │ ┌─────────────┐│ │\n │ │ │ Claude ││ │\n │ │ │ OpenRouter ││ │\n │ │ │ Ollama ││ │\n │ │ └─────────────┘│ │\n │ └───────────────┘ │\n └─────────┬──────────┘\n │\n ┌─────────▼──────────┐\n │ MCP Server │\n │ (stdio JSON-RPC) │\n └─────────┬──────────┘\n │\n Claude Code / Cursor\n\n\n这种架构设计使得 AgentFlow 具有良好的扩展性,未来可以方便地添加新的执行器和提供商支持。\n\n使用方式与工具链\n\nAgentFlow 提供了完整的 CLI 工具链:\n\nbash\nagentflow init 交互式配置向导\nagentflow check <file> 验证工作流并生成摘要\nagentflow run <file> --input '…' 使用真实 LLM 执行\nagentflow run <file> --mock 使用模拟智能体执行(无需 API 密钥)\nagentflow compile <file> 编译为中间表示 JSON\nagentflow validate <file> 仅验证工作流\nagentflow mcp-config 输出 Claude Code 的 MCP 配置\nagentflow models 列出已配置的模型及连接状态\nagentflow resume <file> --instance <uuid> 恢复中断的工作流\n\n\n未来展望\n\nAgentFlow 项目有着清晰的路线图:\n\n- v1.1:VS Code 扩展(语法高亮、LSP 支持)\n- v1.2:并行阶段执行\n- v1.3:工作流注册表与分享机制\n- v2.0:Web 可视化编辑器、CI/CD 集成\n\n总结与思考\n\nAgentFlow DSL 代表了多智能体工作流定义方式的一种新范式。它通过声明式语法降低了多智能体系统的开发门槛,使得更多开发者能够利用多智能体协作来构建复杂的 AI 应用。\n\n这种设计思路的价值在于:将工作流的"做什么"与"怎么做"分离,让开发者专注于业务逻辑本身,而非底层实现细节。同时,自动 MCP 集成特性使得工作流可以无缝嵌入到 Claude Code 等 AI 辅助编程环境中,进一步提升了开发体验。\n\n对于正在探索多智能体系统的开发者来说,AgentFlow 提供了一个轻量级但功能完整的选择,值得尝试和关注。