子代理工具 — 在独立进程中启动子 Agent 执行委托任务。
利用 OTP 进程实现真正的隔离与并发: 子 Agent crash 不影响父 Agent,多个子代理可同时运行。
调用示例
%{
"name" => "task",
"arguments" => %{
"description" => "搜索项目中所有 TODO 注释并整理成清单",
"subagent_type" => "general-purpose"
}
}执行流程
- 解析
subagent_type→ 匹配Options.subagents中的SubAgent规格 SubAgent.to_options/2合并父 Agent 配置(nil 字段继承父级)- 启动子 Agent 进程(
CMDC.Agent.start_link/1) - 发送 HumanMessage(任务描述),子 Agent 独立运行
- 通过 EventBus 订阅子 Agent 事件,收集流式输出与工具调用日志
- 子 Agent 完成后,取最后一条 AI 回复文本作为 ToolMessage 返回父 Agent
- 广播
:subagent_start/:subagent_end事件 - 终止子 Agent 进程
深度限制
子代理的工具列表自动排除 CMDC.Tool.Task,防止递归生成。
上下文隔离
- 继承:working_dir、sandbox、model(若未覆盖)、工具集(若未覆盖)、user_data(若未覆盖)
- 不继承:messages、todos、memory_contents、tool_call_hashes
- 输入:单条 HumanMessage(任务描述)
- 输出:最后一条 AI 回复文本 → ToolMessage