agent tool

AgentTool 实现了子 Agent 调用机制,支持任务委派和多 Agent 协作。

设计理念

为什么需要子 Agent?

单个 AI Agent 面临的挑战:

  1. 上下文限制 - 复杂任务需要大量上下文

  2. 专业化 - 不同任务需要不同的专业知识

  3. 并行化 - 某些任务可以并行执行

子 Agent 解决方案:

  • 每个子 Agent 有独立的上下文

  • 可以使用专门的 System Prompt

  • 支持同步和异步执行

核心实现

输入 Schema

const AgentInputSchema = z.object({
  name: z.string(),  // Agent 名称
  prompt: z.string(), // 任务描述
  explanation: z.string(), // 为什么调用这个 Agent
  contextFiles: z.array(z.object({
    path: z.string(),
    startLine: z.number().optional(),
    endLine: z.number().optional(),
  })).optional(),
  preset: z.string().optional(), // Agent 预设
});

执行流程

Agent 类型

内置 Agent

自定义 Agent

同步执行

实现

特点

  • 阻塞执行,等待完成

  • 返回完整结果

  • 适合需要结果的任务

异步执行

实现

特点

  • 非阻塞执行

  • 使用 worktree 隔离

  • 适合长时间运行的任务

Worktree 隔离

创建 Worktree

清理 Worktree

Context Gatherer

专门的代码分析 Agent

使用示例

Agent 通信

消息传递

结果聚合

下一步