Generated banner for Swarm
Generated logo for Swarm

Swarm

LangChainOfficialPublished by LangChain

  • Skill
  • Tool integration
  • Automation and integrations
  • MIT
  • Low risk
  • Has scripts

From the author

Dispatches many independent items in parallel: create a table, fan out to subagents, aggregate results. One row = one unit of work.
— the description by LangChain

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

Swarm provides a structured way to process large volumes of independent items in parallel. By organizing data into tables, it allows users to fan out instructions to subagents or direct model calls, aggregate results, and perform targeted retries on failed items. It is designed to handle batching automatically, making it efficient for tasks ranging from simple data classification to complex, multi-step agentic workflows.

What it does

  • Creates tabular data structures from files, glob patterns, or custom record sets.
  • Dispatches instructions across rows in parallel using subagents or direct model calls.
  • Supports automatic and custom batching to optimize throughput and cost.
  • Enables chaining of multiple passes to accumulate data on existing rows.
  • Provides filtering capabilities for targeted processing and error recovery.
  • Allows for local JavaScript-based aggregation of results after processing.

When to use it

  • When you need to perform the same task on a large number of independent files or records.
  • When you want to classify, extract, or label data using structured output.
  • When a task requires complex, multi-step reasoning or tool usage across many items.
  • When you need to retry only the failed portions of a large batch operation.

Advantages

  • Significantly faster and cheaper for simple tasks by using direct model calls.
  • Handles large datasets by managing batching and concurrency automatically.
  • Maintains state across multiple passes, allowing for iterative data enrichment.
  • Flexible source handling supports both file-based and custom record-based inputs.

Usage examples

“Classify the sentiment of every JSON object in data.jsonl using the swarm skill.”
The agent creates a table from the records, runs a classification instruction across all rows, and returns a summary of the sentiment distribution.
“Use swarm to review all TypeScript files in src/ for security issues.”
The agent identifies files via glob, dispatches a 'reviewer' subagent to each file, and compiles a table of findings.

Install

  1. Download the skill as a ZIP:

    https://autogineer.com/agent-skills/langchain-ai/swarm/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip swarm.zip -d .agents/skills

Documentation by LangChain

Swarm

Process many independent items in parallel. create builds a table handle; run fans work out across rows and merges results back. One row = one unit of work — swarm handles batching automatically.

Flow

  1. Create. Build a table from a source — files, a glob pattern, or pre-parsed records. One row per item. Returns a handle.

  2. Run. Dispatch an instruction template across rows. Results are merged back into the table. Returns { completed, failed, skipped, failures }.

  3. Aggregate. Use rows() and plain JS to count, filter, or summarize. Do not spawn additional subagents for aggregation.

  4. Retry. Re-run with filter: { column: "<col>", exists: false } to reprocess only failed rows.

Choosing a source

glob / filePaths — one file = one row. Use when each file is an independent unit of work. Each row gets { id, file }; the subagent reads the file itself via the {file} placeholder.

tasks — pass pre-built records directly. Use when the data lives inside a file (JSONL, CSV, JSON array). Read and parse the file first inside eval, then pass the records. One record = one row — do not group multiple items into a single row.

For small files (under ~500 lines), parse and create in one block:

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:

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 eval call, split across two blocks. Only the block that calls swarm functions needs the import:

// 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`);
// 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 filePaths: ["/data.jsonl"] would produce a table with one row pointing at the file — not one row per record inside it.

When to use subagentType

Omit subagentType 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.

Set subagentType when the task requires tools, file access, or multi-step reasoning. Each dispatch runs a full agentic loop with the named subagent.

// 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"] },
});

Instruction + context

instruction is a per-item template with {column} placeholders. Placeholders are resolved by the framework — your column names appear in prompts as references to the values listed alongside, never as raw template syntax. Subagents do the work — do not process items yourself in JS and write the results into rows.

context is free-form prose prepended to every subagent prompt. Use it for shared background: domain terms, classification rules, examples, etc.

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: [...] }

Structured output

responseSchema is required. Schema properties become top-level columns on each row and constrain what subagents can return.

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" }

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 batchSize to control grouping:

  • Number — uniform batch size for all rows. batchSize: 1 forces per-row dispatch; batchSize: 20 groups in twenties.

  • Function — (row, rowCount) => number. 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.

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.

Aggregation

After run(), use rows() and plain JS — no additional subagents needed.

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

run updates the table in place — chain calls to accumulate columns.

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 exists: false filter still works for retries.

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 },
});

Filtering

{ 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] }

Technical notes

  • Only import @/skills/swarm in blocks where you call swarm functions. Data preparation (reading files, parsing, storing in globalThis) does not need the import. Destructure only what you use: { create }, { run }, { create, run }, etc.

  • Console output is capped at ~5 KB. Never log raw file contents — log only counts and short samples.

  • readFile inside eval returns raw content — no line-number prefixes. Request at most 500 lines per call. For files with more than 500 lines, loop with incrementing offset.

  • When building a table from a file, read it inside eval. Data read inside the sandbox stays there; it never enters the agent's context window.

  • Never write to .swarm/ directly. Always use create().

  • Everything the subagent needs must be in instruction + context. Subagents can't see the agent's context.

  • Row ids must be unique. create() rejects sources that produce duplicate ids. For tasks, that's a caller-side responsibility; for glob / filePaths, ids are auto-disambiguated by parent directory.

  • Unknown columns fail fast. If instruction references {foo} and no matched row provides foo, run() throws before any subagent is dispatched.

API Reference

create(source)

Create a table. Returns a handle { id, count, columns }.

SourceDescription
{ glob: "src/**/*.ts" } or { glob: ["src/**/*.ts", "lib/**/*.ts"] }Match files by one or more patterns. Columns: id, file
{ filePaths: ["a.ts", "b.ts"] }Explicit file list. Columns: id, file
{ tasks: [{ id: "t1", text: "..." }] }Custom rows. Each must have id
run(tableId, options)

Dispatch work across rows. Returns { completed, failed, skipped, failures }.

OptionDefaultDescription
instruction(required)Template with {column} placeholders
responseSchema(required)JSON Schema (type: "object") — properties become row columns
context—Prose prepended to every subagent prompt
filter—Only dispatch matching rows
subagentType—Name of subagent to dispatch to. When set, runs a full agentic loop. When omitted, runs a direct model call
batchSizeautoNumber or (row, rowCount) => number. Auto caps dispatches at 10; 1 = per-row; function = per-row sizing
concurrency10Max concurrent subagent dispatches (clamped to 1–10)
rows(tableId, options?)

Retrieve rows. Use for inspection and JS-based aggregation.

OptionDescription
filterOnly return matching rows
columnsProject to specific columns
limitMax rows returned

Files

scripts/table.ts

import type { CreateSource, SwarmHandle } from "./types.js";

/**
 * PTC tool declarations for file operations.
 *
 * At runtime in QuickJS, `tools` is an ambient global injected by the
 * PTC layer. For vitest, set up `globalThis.tools` in `beforeEach`.
 */
declare const tools: {
  glob?: (args: { pattern: string }) => Promise<string>;
  readFile?: (args: { file_path: string }) => Promise<string>;
  writeFile?: (args: { file_path: string; content: string }) => Promise<string>;
  editFile?: (args: {
    file_path: string;
    old_string: string;
    new_string: string;
  }) => Promise<string>;
};

/**
 * Session ID injected by the QuickJS middleware as a global.
 * Scopes table files to the current conversation thread.
 */
declare const __sessionId__: string | undefined;

/**
 * Sanitize a session ID for use as a directory name component.
 * Replaces any character that isn't alphanumeric, hyphen, or underscore
 * with an underscore, and caps length to prevent excessively long paths.
 */
function sanitizeSessionId(id: string): string {
  return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
}

/**
 * Directory prefix for all table JSONL files, scoped to the session.
 */
function getTableDir(): string {
  const id = typeof __sessionId__ !== "undefined" ? __sessionId__ : "default";
  return `/tmp/.swarm/${sanitizeSessionId(id)}`;
}

/**
 * Maximum number of tables before oldest are evicted.
 */
const MAX_TABLES = 5;

/**
 * A table's rows and backend file path, cached in memory to avoid
 * redundant PTC reads within the same session.
 */
interface CachedTable {
  /**
   * The table's row data. Mutated in place during `run()`.
   */
  rows: Record<string, unknown>[];

  /**
   * Backend file path (e.g. `".swarm/003-t_a1b2c3.jsonl"`).
   */
  path: string;

  /**
   * The JSONL content from the most recent successful write.
   * Used as `old_string` when falling back to editFile for overwrites.
   */
  lastWritten: string;
}

/**
 * In-memory table cache keyed by table ID.
 */
const cache = new Map<string, CachedTable>();

/**
 * Monotonic counter for table file sequence numbers.
 */
let sequenceCounter = 0;

/**
 * Reset all module-level state for testing.
 *
 * Clears the in-memory cache and resets the sequence counter.
 */
export function _resetForTesting(): void {
  cache.clear();
  sequenceCounter = 0;
}

/**
 * Generate a random 6-hex-char table ID prefixed with `t_`.
 *
 * @returns A string like `"t_a1b2c3"`.
 */
export function generateId(): string {
  const hex = Math.floor(Math.random() * 0xffffff)
    .toString(16)
    .padStart(6, "0");
  return `t_${hex}`;
}

/**
 * Build the backend file path for a table.
 *
 * @param sequence - Zero-padded monotonic sequence number.
 * @param id - Table ID (e.g. `"t_a1b2c3"`).
 * @returns Path like `".swarm/003-t_a1b2c3.jsonl"`.
 */
export function tablePath(sequence: number, id: string): string {
  const padded = String(sequence).padStart(3, "0");
  return `${getTableDir()}/${padded}-${id}.jsonl`;
}

/**
 * Serialize an array of row objects to JSONL format.
 * One JSON object per line, no trailing newline.
 *
 * @param rows - Array of row objects to serialize.
 * @returns JSONL string.
 */
export function serializeJsonl(rows: Record<string, unknown>[]): string {
  return rows.map((r) => JSON.stringify(r)).join("\n");
}

/**
 * Parse a JSONL string into an array of row objects.
 * Validates that each line parses to a non-null, non-array object.
 *
 * @param content - Raw JSONL content from the backend.
 * @returns Array of parsed row objects.
 * @throws Error with line number if any line is malformed.
 */
export function parseJsonl(content: string): Record<string, unknown>[] {
  if (!content.trim()) {
    return [];
  }

  const parseLine = (line: string, idx: number): Record<string, unknown> => {
    try {
      const parsed = JSON.parse(line);
      if (
        typeof parsed !== "object" ||
        parsed === null ||
        Array.isArray(parsed)
      ) {
        throw new Error(`expected object`);
      }
      return parsed as Record<string, unknown>;
    } catch (e) {
      throw new Error(
        `JSONL parse error at line ${idx + 1}: ${(e as Error).message}`,
        { cause: e },
      );
    }
  };

  return content
    .split("\n")
    .filter((line) => line.trim() !== "")
    .map(parseLine);
}

/**
 * Extract a table ID from a `.swarm/NNN-t_XXXXXX.jsonl` filename.
 *
 * @param filePath - Full path to a table JSONL file.
 * @returns The table ID (e.g. `"t_a1b2c3"`), or `undefined` if the
 *          filename doesn't match the expected pattern.
 */
export function extractIdFromPath(filePath: string): string | undefined {
  const filename = filePath.split("/").pop() || "";
  const match = filename.match(/^\d+-(t_[a-f0-9]+)\.jsonl$/);
  return match ? match[1] : undefined;
}

/**
 * Extract the sequence number from a `.swarm/NNN-t_XXXXXX.jsonl` filename.
 *
 * @param filePath - Full path to a table JSONL file.
 * @returns The sequence number, or `0` if the filename doesn't match.
 */
export function extractSeqFromPath(filePath: string): number {
  const filename = filePath.split("/").pop() || "";
  const match = filename.match(/^(\d+)-/);
  return match ? parseInt(match[1], 10) : 0;
}

/**
 * Build `{ id, file }` rows from a list of file paths.
 *
 * Uses the basename (last path segment) as the row ID. When multiple
 * paths share the same basename, disambiguates by prepending the
 * parent directory name (e.g. `"routes-index.ts"` vs `"handlers-index.ts"`).
 *
 * @param paths - List of file paths.
 * @returns Array of `{ id, file }` row objects.
 */
export function pathsToRows(
  paths: string[],
): Array<{ id: string; file: string }> {
  const basenames = paths.map((p) => {
    const parts = p.split("/");
    return parts[parts.length - 1] || p;
  });

  const counts = new Map<string, number>();
  for (const basename of basenames) {
    counts.set(basename, (counts.get(basename) ?? 0) + 1);
  }

  return paths.map((filePath, idx) => {
    let id = basenames[idx];
    if ((counts.get(id) ?? 0) > 1) {
      const parts = filePath.split("/");
      if (parts.length >= 2) {
        id = `${parts[parts.length - 2]}-${id}`;
      }
    }
    return { id, file: filePath };
  });
}

/**
 * Find duplicate `id` values in a row array.
 *
 * @param rows - Row objects to scan.
 * @returns Array of duplicate ids, in first-seen order, deduplicated.
 */
function findDuplicateIds(rows: Record<string, unknown>[]): string[] {
  const seen = new Set<string>();
  const dupes = new Set<string>();
  for (const row of rows) {
    const id = String(row.id);
    if (seen.has(id)) {
      dupes.add(id);
    } else {
      seen.add(id);
    }
  }
  return [...dupes];
}

/**
 * Resolve a glob pattern to a list of file paths via the PTC `glob` tool.
 *
 * Handles both `string[]` and `{ path: string }[]` return formats
 * from different glob tool implementations.
 *
 * @param pattern - Glob pattern to resolve.
 * @returns Array of matching file paths.
 * @throws Error if the `glob` PTC tool is not configured.
 *
 * @internal
 */
export async function globFiles(pattern: string): Promise<string[]> {
  if (typeof tools.glob !== "function") {
    throw new Error(`Swarm requires a 'glob' tool in the PTC configuration`);
  }

  const raw = await tools.glob({ pattern });
  const parsed = JSON.parse(raw);
  if (!Array.isArray(parsed)) {
    return [];
  }

  const paths: string[] = [];
  for (const item of parsed) {
    if (typeof item === "string") {
      paths.push(item);
    } else if (item && typeof item.path === "string") {
      paths.push(item.path);
    }
  }

  return paths;
}

/**
 * Read a file's content from the backend via the PTC `readFile` tool.
 *
 * @param path - Backend file path.
 * @returns The file content as a string.
 * @throws Error if the `readFile` PTC tool is not configured.
 *
 * @internal
 */
export async function readFile(path: string): Promise<string> {
  if (typeof tools.readFile !== "function") {
    throw new Error(
      `Swarm requires a 'readFile' tool in the PTC configuration`,
    );
  }
  return tools.readFile({ file_path: path });
}

/**
 * Write string content to a backend file via the PTC `writeFile` tool.
 *
 * If the file already exists, falls back to `editFile` for a full
 * replacement — the backend's `write` rejects overwrites by design.
 * When `previousContent` is provided it is used as the `old_string`
 * for the edit, avoiding an unreliable round-trip through readFile.
 *
 * @param path - Backend file path. Created if it doesn't exist.
 * @param content - String content to write.
 * @param previousContent - The last-known content of the file, used
 *   as `old_string` for the editFile fallback.
 * @throws Error if the `writeFile` PTC tool is not configured.
 *
 * @internal
 */
export async function writeFile(
  path: string,
  content: string,
  previousContent?: string,
): Promise<void> {
  if (typeof tools.writeFile !== "function") {
    throw new Error(
      `Swarm requires a 'writeFile' tool in the PTC configuration`,
    );
  }
  const result = await tools.writeFile({ file_path: path, content });
  if (typeof result === "string" && result.includes("already exists")) {
    if (typeof tools.editFile !== "function") {
      throw new Error(
        "Swarm requires an 'edit_file' PTC tool to update existing tables",
      );
    }
    if (previousContent == null) {
      throw new Error(
        `Cannot overwrite ${path}: file already exists and no previous content available`,
      );
    }
    await tools.editFile({
      file_path: path,
      old_string: previousContent,
      new_string: content,
    });
  }
}

/**
 * List all table JSONL files in the `.swarm/` directory, sorted by
 * filename (which encodes creation order via the sequence prefix).
 *
 * @returns Sorted array of file paths, or empty array on failure.
 */
async function listTableFiles(): Promise<string[]> {
  try {
    const files = await globFiles(`${getTableDir()}/*.jsonl`);
    return files.sort();
  } catch {
    return [];
  }
}

/**
 * Evict the oldest tables when the count meets or exceeds `MAX_TABLES`.
 *
 * Clears evicted entries from the in-memory cache and overwrites
 * backend files with empty content (no delete_file tool available).
 * Empty files are treated as evicted by `loadTable`.
 */
async function evict(): Promise<void> {
  const files = await listTableFiles();
  if (files.length < MAX_TABLES) {
    return;
  }

  const toEvict = files.slice(0, files.length - MAX_TABLES + 1);
  for (const filePath of toEvict) {
    const id = extractIdFromPath(filePath);
    const prev = id ? cache.get(id)?.lastWritten : undefined;
    if (id) {
      cache.delete(id);
    }
    try {
      await writeFile(filePath, "", prev);
    } catch {
      // Best-effort eviction — non-fatal if overwrite fails
    }
  }
}

/**
 * Determine the next sequence number for a new table file.
 *
 * Reads existing files on the backend to avoid sequence collisions
 * across runs (same thread, new session). The counter only advances
 * forward — it never reuses a sequence number.
 *
 * @returns The next available sequence number.
 */
async function nextSequence(): Promise<number> {
  const files = await listTableFiles();
  if (files.length > 0) {
    const lastSequence = extractSeqFromPath(files[files.length - 1]);
    if (lastSequence >= sequenceCounter) {
      sequenceCounter = lastSequence + 1;
    }
  }
  return sequenceCounter++;
}

/**
 * Resolve one or more glob patterns into a deduplicated, sorted list
 * of file paths.
 *
 * @param pattern - A single glob string or array of glob strings.
 * @returns Sorted, deduplicated array of matching file paths.
 * @throws Error if no files match any of the provided patterns.
 */
async function resolveGlob(pattern: string | string[]): Promise<string[]> {
  const patterns = Array.isArray(pattern) ? pattern : [pattern];
  const allPaths: string[] = [];
  for (const p of patterns) {
    const paths = await globFiles(p);
    allPaths.push(...paths);
  }

  const unique = [...new Set(allPaths)].sort();
  if (unique.length === 0) {
    throw new Error(`No files matched pattern: ${JSON.stringify(pattern)}`);
  }

  return unique;
}

/**
 * Create a table from a source spec.
 *
 * Validates the source, builds rows, runs eviction if the table count
 * is at capacity, persists the new table to the backend as JSONL, and
 * returns a lightweight handle.
 *
 * @param source - Exactly one of `glob`, `filePaths`, or `tasks`.
 * @returns A handle with the table's ID, row count, and column names.
 * @throws Error if the source is invalid, empty, or missing required PTC tools.
 */
export async function createTable(source: CreateSource): Promise<SwarmHandle> {
  const sourceCount = [source.glob, source.filePaths, source.tasks].filter(
    (s) => s != null,
  ).length;

  if (sourceCount === 0) {
    throw new Error(
      "create() requires exactly one source: glob, filePaths, or tasks",
    );
  }

  if (sourceCount > 1) {
    throw new Error("create() accepts only one source type at a time");
  }

  let rows: Record<string, unknown>[];

  if (source.glob != null) {
    const paths = await resolveGlob(source.glob);
    rows = pathsToRows(paths);
  } else if (source.filePaths != null) {
    if (source.filePaths.length === 0) {
      throw new Error("filePaths array is empty");
    }
    rows = pathsToRows(source.filePaths);
  } else {
    const tasks = source.tasks ?? [];
    if (tasks.length === 0) {
      throw new Error("tasks array is empty");
    }

    for (let idx = 0; idx < tasks.length; idx++) {
      if (typeof tasks[idx].id !== "string") {
        throw new Error(`tasks[${idx}] is missing string 'id' field`);
      }
    }

    rows = tasks;
  }

  const dupes = findDuplicateIds(rows);
  if (dupes.length > 0) {
    throw new Error(`create() received duplicate row ids: ${dupes.join(", ")}`);
  }

  await evict();

  const id = generateId();
  const seq = await nextSequence();
  const path = tablePath(seq, id);

  const content = serializeJsonl(rows);
  await writeFile(path, content);
  cache.set(id, { rows, path, lastWritten: content });

  return {
    id,
    count: rows.length,
    columns: Object.keys(rows[0] ?? {}),
  };
}

/**
 * Load a table's rows by ID.
 *
 * Checks the in-memory cache first. On a cache miss (e.g. cross-run
 * resume), globs the backend to locate the JSONL file, reads and
 * parses it, and populates the cache.
 *
 * @param id - The table ID from a `SwarmHandle`.
 * @returns The table's row array (by reference — mutations are visible).
 * @throws Error if the table is not found (evicted or never created).
 */
export async function loadTable(
  id: string,
): Promise<Record<string, unknown>[]> {
  const cached = cache.get(id);
  if (cached) {
    return cached.rows;
  }

  const files = await listTableFiles();
  const match = files.find((f) => f.endsWith(`-${id}.jsonl`));
  if (!match) {
    throw new Error(`Table "${id}" not found. It may have been evicted`);
  }

  const content = await readFile(match);
  if (!content.trim()) {
    throw new Error(`Table "${id}" not found. It may have been evicted`);
  }

  const rows = parseJsonl(content);
  cache.set(id, { rows, path: match, lastWritten: serializeJsonl(rows) });

  return rows;
}

/**
 * Persist a table's current rows to the backend.
 *
 * Updates both the in-memory cache and the backend JSONL file.
 * The table must have been previously loaded via `loadTable` so
 * that its backend file path is known.
 *
 * @param id - The table ID from a `SwarmHandle`.
 * @param rows - The updated row array to persist.
 * @throws Error if the table has not been loaded into cache.
 */
export async function saveTable(
  id: string,
  rows: Record<string, unknown>[],
): Promise<void> {
  const cached = cache.get(id);
  if (!cached) {
    throw new Error(`Table "${id}" is not loaded - call loadTable first`);
  }
  cached.rows = rows;
  const content = serializeJsonl(rows);
  await writeFile(cached.path, content, cached.lastWritten);
  cached.lastWritten = content;
}

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Needs: Needs Node.js

The author says: Requires @langchain/quickjs code interpreter with swarm_task PTC tool

Security

What our scanner noticed· Low risk

  • Scripts that your agent may run

This skill includes scripts that your agent may run. Read them before you use it.

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Published by
LangChain
Publisher
LangChain (Official)
Source
langchain-ai/langchain-skills · config/skills/swarm · commit 126c384
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by LangChain or LangChain.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download swarm.zip