s05
Agent 编排
智能层子 Agent 生成与多智能体协作
AgentTool 分叉独立上下文 + TeamCreate 并行工作每个子 Agent 拥有独立消息历史和工具集,通过 SendMessage 跨 Agent 通信
子 Agent 生成与多智能体协作
AgentTool 分叉独立上下文 + TeamCreate 并行工作每个子 Agent 拥有独立消息历史和工具集,通过 SendMessage 跨 Agent 通信
复杂编程任务(如大规模重构)超出单个 LLM 调用的能力范围。Claude Code 通过 AgentTool 生成子 Agent,每个子 Agent 拥有独立的消息历史和工具集。TeamCreateTool 更进一步,允许创建并行工作的 Agent 团队。核心挑战是:如何让多个 Agent 高效协作而不互相干扰?
子 Agent 运行逻辑 — 分叉独立上下文并启动查询循环
// runAgent 核心流程:
// 1. forkSubagent 从父 Agent 克隆上下文(但创建独立消息历史)
// 2. 构建子 Agent 的系统提示词(包含任务描述和可用工具限制)
// 3. 启动独立的 QueryEngine 循环
// 4. 子 Agent 完成后返回摘要给父 Agent
// 5. 可选:通过 SendMessage 与其他 Agent 通信内置 Agent 类型定义 — 预配置的专用 Agent
// 内置 Agent 类型包括:
// - general-purpose: 通用 Agent,可使用所有工具
// - Explore: 快速代码探索,限制为只读工具
// - Plan: 架构规划,限制为读和搜索工具
// - claude-code-guide: Claude Code 使用指南
// 每种类型预配置了:工具白名单、系统提示词、推荐模型