mcpField
S
swarm

swarm

github.com/langchain-aiAgent 工作流30.8k98 次安装1 天前更新
代码仓库

关于 swarm

swarm 是用于将独立任务分派给多个子 Agent 并发处理的技能,适合批量摘要、分类和研究。它限制并发数,并按输入顺序收集结果,且单项失败不会中止其他任务。

主要功能

  1. 01并发分派多个任务
  2. 02限制并发数,最高为 10
  3. 03按输入顺序收集结果
  4. 04隔离单项任务失败

适用场景

  1. 01并行摘要多份文件
  2. 02批量分类工单
  3. 03并行研究多个主题

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/langchain-ai/deepagents --skill swarm

安装后在 Claude Code 中输入/swarm确认已加载。

附带文件scripts/index.ts

详细介绍

Swarm

将任务列表分发给子 Agent 并限制并发数,收集结果后返回紧凑的摘要。

加载

REPL 的 eval 工具支持从 @/skills/* 进行 ES 模块导入。 使用 await import("@/skills/swarm") 加载此技能,REPL 会安装一个自定义模块加载器,将这些路径解析到 skills 后端。请不要把 index.ts 内联到 eval 代码中。 导入是受支持且经过测试的方式;复制源码属于反模式,会重复逻辑,并在技能更新后产生偏差。

适用场景

你有许多相互独立的任务(例如“总结这 20 个文件中的每一个”“对这 50 张工单进行分类”“研究这 15 个主题”),并希望它们并发运行而不是顺序执行。

用法

runSwarm(...) 返回一个摘要对象,而不是数组。请解构 .results 以获取每个任务的输出;摘要对象本身不可迭代。

const { runSwarm } = await import("@/skills/swarm");

const { results, completed, failed } = await runSwarm({
  tasks: [
    { description: "Summarize /notes/alpha.md" },
    { description: "Summarize /notes/beta.md" },
    { description: "Summarize /notes/gamma.md" },
  ],
  concurrency: 3,           // optional, defaults to 5, capped at 10
  subagentType: "general-purpose",  // optional; per-task override wins
});

console.log(`${completed} ok, ${failed} failed`);
for (const r of results) {
  console.log(r.id, r.status, r.output ?? r.error);
}

约定

runSwarm(opts) 函数接受以下参数:

  • tasks(必填):{ description: string, subagentType?: string } 组成的数组。
  • concurrency(可选,默认 5,上限 10):子 Agent 的最大并行调用数。
  • subagentType(可选,默认 "general-purpose"):分发每个任务时使用的默认子 Agent。任务自身的 subagentType 优先。

返回一个摘要对象:

{
  total: number;          // tasks.length
  completed: number;      // subagents that returned a result
  failed: number;         // subagents that threw
  results: {              // one entry per task, in input order
    id: number;           // 0-indexed position in `tasks`
    status: "completed" | "failed";
    output?: string;      // on success — subagent's final message
    error?: string;       // on failure — error message
  }[];
}

设计说明

  • 分发通过 tools.task 进行,REPL 的 PTC 层为我们暴露了该接口。本技能本身不注册任何子 Agent,它只是构建在 Agent 已有能力之上的扇出模式。
  • 失败在每个任务内单独捕获:一个子 Agent 失败不会中止整个 Swarm。请检查 failed 计数和每个任务的 status。
  • 并发通过信号量式的池进行限制,而不是对所有任务使用 Promise.all。对于 100 个任务且 concurrency=5 的情况,这样可以使内存占用和工具调用速率保持可预测。

评论

支持 Markdown