swarm
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSwarm
Swarm
Process many independent items in parallel. builds a table handle;
fans work out across rows and merges results back. One row = one unit
of work — swarm handles batching automatically.
createrun并行处理多个独立任务。 方法创建表格句柄; 方法将任务分发至各行并合并结果。一行代表一个工作单元——Swarm 会自动处理批量任务。
createrunFlow
流程
- Create. Build a table from a source — files, a glob pattern, or pre-parsed records. One row per item. Returns a handle.
- Run. Dispatch an template across rows. Results are merged back into the table. Returns
instruction.{ completed, failed, skipped, failures } - Aggregate. Use and plain JS to count, filter, or summarize. Do not spawn additional subagents for aggregation.
rows() - Retry. Re-run with to reprocess only failed rows.
filter: { column: "<col>", exists: false }
- 创建:从源数据(文件、通配符模式或预解析记录)构建表格,每行对应一个任务项,返回句柄。
- 运行:将模板分发至各行,结果合并回表格,返回
instruction。{ completed, failed, skipped, failures } - 汇总:使用和原生JS进行计数、过滤或总结,汇总时不要生成额外的子代理。
rows() - 重试:使用重新运行,仅处理失败的行。
filter: { column: "<col>", exists: false }
Choosing a source
选择数据源
globfilePaths{ id, file }{file}tasksevalFor small files (under ~500 lines), parse and create in one block:
javascript
const { create } = await import("@/skills/swarm");
const raw = await tools.readFile({ file_path: "/data.jsonl" });
const records = raw.trim().split("\n").map(l => JSON.parse(l));
const table = await create({ tasks: records });
console.log(table);For large files, read in chunks of 500 lines to avoid truncation:
javascript
const { create } = await import("@/skills/swarm");
let records = [];
let offset = 0;
while (true) {
const chunk = await tools.readFile({ file_path: "/data.txt", offset, limit: 500 });
const lines = chunk.split("\n").filter(l => l.trim());
for (const l of lines) { records.push({ id: `r${records.length}`, text: l }); }
if (lines.length < 500) break;
offset += 500;
}
const table = await create({ tasks: records });
console.log(table);When the file is too large to parse and dispatch in one call, split
across two blocks. Only the block that calls swarm functions needs the import:
evaljavascript
// eval 1: parse only — no swarm import needed
const raw = await tools.readFile({ file_path: "/data.jsonl" });
globalThis.records = raw.trim().split("\n").map(l => JSON.parse(l));
console.log(`Parsed ${globalThis.records.length} records`);javascript
// eval 2: create and dispatch
const { create, run } = await import("@/skills/swarm");
const table = await create({ tasks: globalThis.records });
const result = await run(table.id, {
instruction: "Classify {text}",
responseSchema: {
type: "object",
properties: { label: { type: "string" } },
required: ["label"],
},
});
console.log(result);Passing would produce a table with one row
pointing at the file — not one row per record inside it.
filePaths: ["/data.jsonl"]globfilePaths{ id, file }{file}taskseval对于小文件(约500行以内),可在一个代码块中完成解析和创建:
javascript
const { create } = await import("@/skills/swarm");
const raw = await tools.readFile({ file_path: "/data.jsonl" });
const records = raw.trim().split("\n").map(l => JSON.parse(l));
const table = await create({ tasks: records });
console.log(table);对于大文件,按500行分块读取以避免截断:
javascript
const { create } = await import("@/skills/swarm");
let records = [];
let offset = 0;
while (true) {
const chunk = await tools.readFile({ file_path: "/data.txt", offset, limit: 500 });
const lines = chunk.split("\n").filter(l => l.trim());
for (const l of lines) { records.push({ id: `r${records.length}`, text: l }); }
if (lines.length < 500) break;
offset += 500;
}
const table = await create({ tasks: records });
console.log(table);当文件过大无法在一次调用中完成解析和分发时,拆分为两个代码块。只有调用Swarm函数的代码块需要导入:
evaljavascript
// eval 1: 仅解析——无需导入Swarm
const raw = await tools.readFile({ file_path: "/data.jsonl" });
globalThis.records = raw.trim().split("\n").map(l => JSON.parse(l));
console.log(`已解析 ${globalThis.records.length} 条记录`);javascript
// eval 2: 创建并分发
const { create, run } = await import("@/skills/swarm");
const table = await create({ tasks: globalThis.records });
const result = await run(table.id, {
instruction: "Classify {text}",
responseSchema: {
type: "object",
properties: { label: { type: "string" } },
required: ["label"],
},
});
console.log(result);传入会生成一个仅包含一行的表格(指向该文件)——而非文件内每条记录对应一行。
filePaths: ["/data.jsonl"]When to use subagentType
subagentType何时使用subagentType
subagentTypeOmit for classification, extraction, labeling, and any task
where a single model call with structured output is sufficient. This is the
default and is significantly cheaper and faster — each dispatch is a direct
model call, no tools, no iteration.
subagentTypeSet when the task requires tools, file access, or multi-step
reasoning. Each dispatch runs a full agentic loop with the named subagent.
subagentTypejavascript
// Direct model call — classification, no tools needed
await run(table.id, {
instruction: "Classify {text}",
responseSchema: { type: "object", properties: { label: { type: "string" } }, required: ["label"] },
});
// Subagent — needs to read files and reason over multiple steps
await run(table.id, {
subagentType: "reviewer",
instruction: "Review {file} for security issues.",
responseSchema: { type: "object", properties: { finding: { type: "string" } }, required: ["finding"] },
});对于分类、提取、标注等仅需单次模型调用并返回结构化输出的任务,无需设置。这是默认模式,成本更低、速度更快——每次调度都是直接的模型调用,无需工具,无需迭代。
subagentType当任务需要工具、文件访问或多步推理时,设置。每次调度会运行指定子代理的完整智能体循环。
subagentTypejavascript
// 直接模型调用——分类任务,无需工具
await run(table.id, {
instruction: "Classify {text}",
responseSchema: { type: "object", properties: { label: { type: "string" } }, required: ["label"] },
});
// 子代理——需要读取文件并进行多步推理
await run(table.id, {
subagentType: "reviewer",
instruction: "Review {file} for security issues.",
responseSchema: { type: "object", properties: { finding: { type: "string" } }, required: ["finding"] },
});Instruction + context
指令 + 上下文
instruction{column}contextjavascript
const { create, run } = await import("@/skills/swarm");
const table = await create({ glob: "src/**/*.ts" });
const r = await run(table.id, {
subagentType: "reviewer",
instruction: "Review {file} for security issues. List findings or write 'no issues'.",
context: "TypeScript Express backend using Prisma ORM. Focus on injection, auth bypass, path traversal.",
responseSchema: {
type: "object",
properties: { review: { type: "string" } },
required: ["review"],
},
});
console.log(r);
// → { completed: 45, failed: 2, skipped: 0, failures: [...] }instruction{column}contextjavascript
const { create, run } = await import("@/skills/swarm");
const table = await create({ glob: "src/**/*.ts" });
const r = await run(table.id, {
subagentType: "reviewer",
instruction: "Review {file} for security issues. List findings or write 'no issues'.",
context: "TypeScript Express后端,使用Prisma ORM。重点关注注入、权限绕过、路径遍历问题。",
responseSchema: {
type: "object",
properties: { review: { type: "string" } },
required: ["review"],
},
});
console.log(r);
// → { completed: 45, failed: 2, skipped: 0, failures: [...] }Structured output
结构化输出
responseSchemajavascript
const { run } = await import("@/skills/swarm");
await run(table.id, {
instruction: "Classify: {text}",
responseSchema: {
type: "object",
properties: {
sentiment: { type: "string", enum: ["positive", "negative", "neutral"] },
},
required: ["sentiment"],
},
});
// Row after: { id: "r1", text: "...", sentiment: "positive" }responseSchemajavascript
const { run } = await import("@/skills/swarm");
await run(table.id, {
instruction: "Classify: {text}",
responseSchema: {
type: "object",
properties: {
sentiment: { type: "string", enum: ["positive", "negative", "neutral"] },
},
required: ["sentiment"],
},
});
// 处理后的数据行:{ id: "r1", text: "...", sentiment: "positive" }Batching
批量处理
By default, swarm auto-batches to keep total dispatches under 10. For small
tables (≤10 rows) each row gets its own subagent call. For larger tables,
rows are grouped automatically.
Set to control grouping:
batchSize- Number — uniform batch size for all rows. forces per-row dispatch;
batchSize: 1groups in twenties.batchSize: 20 - Function — . Returns the desired batch size for each row. Rows with the same batch size are grouped together, then chunked. Allows mixed dispatch where some rows go solo and others batch.
(row, rowCount) => number
javascript
const { create, run } = await import("@/skills/swarm");
const table = await create({ tasks: items });
// Complex items get individual attention; simple ones batch together
await run(table.id, {
instruction: "Analyze {text}",
responseSchema: {
type: "object",
properties: { analysis: { type: "string" } },
required: ["analysis"],
},
batchSize: (row) => (row.token_count > 1000 ? 1 : 10),
});Batch sizes are clamped to [1, 50] after evaluation.
默认情况下,Swarm会自动批量处理,使总调度次数保持在10次以内。对于小型表格(≤10行),每行单独调用子代理;对于大型表格,行将自动分组。
设置可控制分组方式:
batchSize- 数字——所有行使用统一的批量大小。强制逐行调度;
batchSize: 1每20行一组。batchSize: 20 - 函数——。返回每行所需的批量大小。具有相同批量大小的行会被分组,然后拆分。支持混合调度,部分行单独处理,部分行批量处理。
(row, rowCount) => number
javascript
const { create, run } = await import("@/skills/swarm");
const table = await create({ tasks: items });
// 复杂任务项单独处理;简单任务项批量处理
await run(table.id, {
instruction: "Analyze {text}",
responseSchema: {
type: "object",
properties: { analysis: { type: "string" } },
required: ["analysis"],
},
batchSize: (row) => (row.token_count > 1000 ? 1 : 10),
});批量大小在计算后会被限制在[1, 50]范围内。
Aggregation
汇总
After , use and plain JS — no additional subagents needed.
run()rows()javascript
const { rows } = await import("@/skills/swarm");
const data = await rows(table.id, { columns: ["sentiment"] });
const counts = {};
data.forEach(r => { counts[r.sentiment] = (counts[r.sentiment] || 0) + 1 });
console.log(counts);
// → { positive: 120, negative: 45, neutral: 35 }调用后,使用和原生JS进行汇总——无需额外子代理。
run()rows()javascript
const { rows } = await import("@/skills/swarm");
const data = await rows(table.id, { columns: ["sentiment"] });
const counts = {};
data.forEach(r => { counts[r.sentiment] = (counts[r.sentiment] || 0) + 1 });
console.log(counts);
// → { positive: 120, negative: 45, neutral: 35 }Chaining passes
链式调用
runjavascript
const { create, run } = await import("@/skills/swarm");
const table = await create({ tasks: interviews });
await run(table.id, {
instruction: "Classify sentiment of {text}",
responseSchema: {
type: "object",
properties: { sentiment: { type: "string", enum: ["positive", "negative", "neutral"] } },
required: ["sentiment"],
},
});
await run(table.id, {
filter: { column: "sentiment", equals: "negative" },
instruction: "Summarize why {text} had negative sentiment.",
responseSchema: {
type: "object",
properties: { summary: { type: "string" } },
required: ["summary"],
},
});runjavascript
const { create, run } = await import("@/skills/swarm");
const table = await create({ tasks: interviews });
await run(table.id, {
instruction: "Classify sentiment of {text}",
responseSchema: {
type: "object",
properties: { sentiment: { type: "string", enum: ["positive", "negative", "neutral"] } },
required: ["sentiment"],
},
});
await run(table.id, {
filter: { column: "sentiment", equals: "negative" },
instruction: "Summarize why {text} had negative sentiment.",
responseSchema: {
type: "object",
properties: { summary: { type: "string" } },
required: ["summary"],
},
});Action-only tasks
仅执行动作的任务
When subagents perform actions (write a file, apply a fix) rather than return
data, use a simple schema with a status or marker field. The
filter still works for retries.
exists: falsejavascript
const { create, run } = await import("@/skills/swarm");
const fixedSchema = {
type: "object",
properties: { fixed: { type: "string" } },
required: ["fixed"],
};
const table = await create({ glob: "src/**/*.ts" });
await run(table.id, {
subagentType: "fixer",
instruction: "Add missing JSDoc to all exported functions in {file}.",
responseSchema: fixedSchema,
});
// retry any that failed
await run(table.id, {
subagentType: "fixer",
instruction: "Add missing JSDoc to all exported functions in {file}.",
responseSchema: fixedSchema,
filter: { column: "fixed", exists: false },
});当子代理执行动作(写入文件、修复问题)而非返回数据时,使用包含状态或标记字段的简单Schema。过滤器仍可用于重试。
exists: falsejavascript
const { create, run } = await import("@/skills/swarm");
const fixedSchema = {
type: "object",
properties: { fixed: { type: "string" } },
required: ["fixed"],
};
const table = await create({ glob: "src/**/*.ts" });
await run(table.id, {
subagentType: "fixer",
instruction: "Add missing JSDoc to all exported functions in {file}.",
responseSchema: fixedSchema,
});
// 重试失败的任务
await run(table.id, {
subagentType: "fixer",
instruction: "Add missing JSDoc to all exported functions in {file}.",
responseSchema: fixedSchema,
filter: { column: "fixed", exists: false },
});Filtering
过滤规则
javascript
{ column: "status", equals: "done" }
{ column: "status", notEquals: "done" }
{ column: "category", in: ["A", "B"] }
{ column: "result", exists: false } // not yet processed
{ and: [filter1, filter2] }
{ or: [filter1, filter2] }javascript
{ column: "status", equals: "done" }
{ column: "status", notEquals: "done" }
{ column: "category", in: ["A", "B"] }
{ column: "result", exists: false } // 未处理
{ and: [filter1, filter2] }
{ or: [filter1, filter2] }Technical notes
技术说明
- Only import in blocks where you call swarm functions. Data preparation (reading files, parsing, storing in
@/skills/swarm) does not need the import. Destructure only what you use:globalThis,{ create },{ run }, etc.{ create, run } - Console output is capped at ~5 KB. Never log raw file contents — log only counts and short samples.
- inside
readFilereturns raw content — no line-number prefixes. Request at most 500 lines per call. For files with more than 500 lines, loop with incrementingeval.offset - When building a table from a file, read it inside . Data read inside the sandbox stays there; it never enters the agent's context window.
eval - Never write to directly. Always use
.swarm/.create() - Everything the subagent needs must be in +
instruction. Subagents can't see the agent's context.context - Row ids must be unique. rejects sources that produce duplicate ids. For
create(), that's a caller-side responsibility; fortasks/glob, ids are auto-disambiguated by parent directory.filePaths - Unknown columns fail fast. If references
instructionand no matched row provides{foo},foothrows before any subagent is dispatched.run()
- 仅在调用Swarm函数的代码块中导入。数据准备(读取文件、解析、存储到
@/skills/swarm)无需导入。仅解构所需的方法:globalThis、{ create }、{ run }等。{ create, run } - 控制台输出上限约为5 KB。切勿记录原始文件内容——仅记录计数和简短样本。
- 内的
eval返回原始内容——无行号前缀。每次调用最多请求500行。对于超过500行的文件,使用递增的readFile循环读取。offset - 从文件构建表格时,在内读取文件。沙箱内读取的数据会保留在沙箱中;不会进入智能体的上下文窗口。
eval - 切勿直接写入目录。始终使用
.swarm/方法。create() - 子代理所需的所有信息必须包含在+
instruction中。子代理无法查看智能体的上下文。context - 行ID必须唯一。会拒绝生成重复ID的数据源。对于
create(),这是调用方的责任;对于tasks/glob,ID会通过父目录自动去重。filePaths - 未知列会快速失败。如果引用
instruction但没有匹配的行提供{foo},foo会在调度任何子代理前抛出错误。run()
API Reference
API参考
create(source)
create(source)create(source)
create(source)Create a table. Returns a handle .
{ id, count, columns }| Source | Description |
|---|---|
| Match files by one or more patterns. Columns: |
| Explicit file list. Columns: |
| Custom rows. Each must have |
创建表格。返回句柄。
{ id, count, columns }| 数据源 | 描述 |
|---|---|
| 通过一个或多个模式匹配文件。列: |
| 明确的文件列表。列: |
| 自定义行数据。每行必须包含 |
run(tableId, options)
run(tableId, options)run(tableId, options)
run(tableId, options)Dispatch work across rows. Returns .
{ completed, failed, skipped, failures }| Option | Default | Description |
|---|---|---|
| (required) | Template with |
| (required) | JSON Schema ( |
| — | Prose prepended to every subagent prompt |
| — | Only dispatch matching rows |
| — | Name of subagent to dispatch to. When set, runs a full agentic loop. When omitted, runs a direct model call |
| auto | Number or |
| | Max concurrent subagent dispatches (clamped to 1–10) |
将任务分发至各行。返回。
{ completed, failed, skipped, failures }| 选项 | 默认值 | 描述 |
|---|---|---|
| 必填 | 带有 |
| 必填 | JSON Schema( |
| — | 添加到每个子代理提示词开头的文本 |
| — | 仅调度匹配条件的行 |
| — | 要调度的子代理名称。设置后会运行完整的智能体循环;未设置时运行直接模型调用 |
| 自动 | 数字或 |
| | 最大并发子代理调度数(限制在1–10之间) |
rows(tableId, options?)
rows(tableId, options?)rows(tableId, options?)
rows(tableId, options?)Retrieve rows. Use for inspection and JS-based aggregation.
| Option | Description |
|---|---|
| Only return matching rows |
| Project to specific columns |
| Max rows returned |
获取行数据。用于检查和基于JS的汇总。
| 选项 | 描述 |
|---|---|
| 仅返回匹配条件的行 |
| 仅返回指定列 |
| 返回的最大行数 |