{"version":3,"file":"swarm.cjs","names":["Annotation","MessagesAnnotation","START","StateGraph","getHandoffDestinations"],"sources":["../src/swarm.ts"],"sourcesContent":["import {\n  START,\n  StateGraph,\n  CompiledStateGraph,\n  AnnotationRoot,\n  MessagesAnnotation,\n  Annotation,\n} from \"@langchain/langgraph\";\nimport { getHandoffDestinations } from \"./handoff.js\";\n\n/**\n * State schema for the multi-agent swarm.\n */\nconst SwarmState = Annotation.Root({\n  ...MessagesAnnotation.spec,\n  activeAgent: Annotation<string>,\n});\n\n/**\n * Add a router to the currently active agent to the StateGraph.\n *\n * @param builder The graph builder (StateGraph) to add the router to.\n * @param params Parameters for the router.\n * @returns StateGraph with the router added.\n */\nconst addActiveAgentRouter = <\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  AnnotationRootT extends AnnotationRoot<any> = typeof SwarmState,\n>(\n  /** The graph builder (StateGraph) to add the router to. */\n  builder: StateGraph<\n    AnnotationRootT[\"spec\"],\n    AnnotationRootT[\"State\"],\n    AnnotationRootT[\"Update\"],\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    any,\n    AnnotationRootT[\"spec\"],\n    AnnotationRootT[\"spec\"]\n  >,\n  {\n    routeTo,\n    defaultActiveAgent,\n  }: {\n    /** A list of agent (node) names to route to. */\n    routeTo: string[];\n    /** Name of the agent to route to by default (if no agents are currently active). */\n    defaultActiveAgent: string;\n  }\n): StateGraph<\n  AnnotationRootT[\"spec\"],\n  AnnotationRootT[\"State\"],\n  AnnotationRootT[\"Update\"],\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  any,\n  AnnotationRootT[\"spec\"],\n  AnnotationRootT[\"spec\"]\n> => {\n  if (!routeTo.includes(defaultActiveAgent)) {\n    throw new Error(\n      `Default active agent '${defaultActiveAgent}' not found in routes ${routeTo}`\n    );\n  }\n\n  const routeToActiveAgent = (state: typeof SwarmState.State) => {\n    return state.activeAgent || defaultActiveAgent;\n  };\n\n  builder.addConditionalEdges(START, routeToActiveAgent, routeTo);\n  return builder;\n};\n\n/** @inline */\nexport type CreateSwarmParams<\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  AnnotationRootT extends AnnotationRoot<any> = typeof SwarmState,\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  AgentAnnotationRootT extends AnnotationRoot<any> = typeof MessagesAnnotation,\n> = {\n  /** List of agents to add to the swarm */\n  agents: CompiledStateGraph<\n    AgentAnnotationRootT[\"State\"],\n    AgentAnnotationRootT[\"Update\"],\n    string,\n    AgentAnnotationRootT[\"spec\"],\n    AgentAnnotationRootT[\"spec\"]\n  >[];\n\n  /** Name of the agent to route to by default (if no agents are currently active) */\n  defaultActiveAgent: string;\n\n  /** State schema to use for the multi-agent graph */\n  stateSchema?: AnnotationRootT;\n};\n\n/**\n * Create a multi-agent swarm.\n *\n * @param params Parameters for the multi-agent swarm.\n * @returns A multi-agent swarm StateGraph.\n */\nconst createSwarm = <\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  AnnotationRootT extends AnnotationRoot<any> = typeof SwarmState,\n>({\n  agents,\n  defaultActiveAgent,\n  stateSchema,\n}: CreateSwarmParams<AnnotationRootT>): StateGraph<\n  AnnotationRootT[\"spec\"],\n  AnnotationRootT[\"State\"],\n  AnnotationRootT[\"Update\"],\n  string,\n  AnnotationRootT[\"spec\"],\n  AnnotationRootT[\"spec\"]\n> => {\n  if (stateSchema && !(\"activeAgent\" in stateSchema.spec)) {\n    throw new Error(\"Missing required key 'activeAgent' in stateSchema\");\n  }\n\n  const agentNames = new Set<string>();\n\n  for (const agent of agents) {\n    if (!agent.name || agent.name === \"LangGraph\") {\n      throw new Error(\n        \"Please specify a name when you create your agent, either via `createReactAgent({ ..., name: agentName })` \" +\n          \"or via `graph.compile({ name: agentName })`.\"\n      );\n    }\n\n    if (agentNames.has(agent.name)) {\n      throw new Error(\n        `Agent with name '${agent.name}' already exists. Agent names must be unique.`\n      );\n    }\n\n    agentNames.add(agent.name);\n  }\n\n  const builder = new StateGraph(stateSchema ?? SwarmState);\n  addActiveAgentRouter(builder, {\n    routeTo: [...agentNames],\n    defaultActiveAgent,\n  });\n\n  for (const agent of agents) {\n    builder.addNode(agent.name!, agent, {\n      ends: getHandoffDestinations(agent),\n      subgraphs: [agent],\n    });\n  }\n\n  return builder as StateGraph<\n    AnnotationRootT[\"spec\"],\n    AnnotationRootT[\"State\"],\n    AnnotationRootT[\"Update\"],\n    string,\n    AnnotationRootT[\"spec\"],\n    AnnotationRootT[\"spec\"]\n  >;\n};\n\nexport { createSwarm, addActiveAgentRouter, SwarmState };\n"],"mappings":";;;;;;AAaA,MAAM,aAAaA,qBAAAA,WAAW,KAAK;CACjC,GAAGC,qBAAAA,mBAAmB;CACtB,aAAaD,qBAAAA;CACd,CAAC;;;;;;;;AASF,MAAM,wBAKJ,SASA,EACE,SACA,yBAeC;AACH,KAAI,CAAC,QAAQ,SAAS,mBAAmB,CACvC,OAAM,IAAI,MACR,yBAAyB,mBAAmB,wBAAwB,UACrE;CAGH,MAAM,sBAAsB,UAAmC;AAC7D,SAAO,MAAM,eAAe;;AAG9B,SAAQ,oBAAoBE,qBAAAA,OAAO,oBAAoB,QAAQ;AAC/D,QAAO;;;;;;;;AAgCT,MAAM,eAGJ,EACA,QACA,oBACA,kBAQG;AACH,KAAI,eAAe,EAAE,iBAAiB,YAAY,MAChD,OAAM,IAAI,MAAM,oDAAoD;CAGtE,MAAM,6BAAa,IAAI,KAAa;AAEpC,MAAK,MAAM,SAAS,QAAQ;AAC1B,MAAI,CAAC,MAAM,QAAQ,MAAM,SAAS,YAChC,OAAM,IAAI,MACR,yJAED;AAGH,MAAI,WAAW,IAAI,MAAM,KAAK,CAC5B,OAAM,IAAI,MACR,oBAAoB,MAAM,KAAK,+CAChC;AAGH,aAAW,IAAI,MAAM,KAAK;;CAG5B,MAAM,UAAU,IAAIC,qBAAAA,WAAW,eAAe,WAAW;AACzD,sBAAqB,SAAS;EAC5B,SAAS,CAAC,GAAG,WAAW;EACxB;EACD,CAAC;AAEF,MAAK,MAAM,SAAS,OAClB,SAAQ,QAAQ,MAAM,MAAO,OAAO;EAClC,MAAMC,gBAAAA,uBAAuB,MAAM;EACnC,WAAW,CAAC,MAAM;EACnB,CAAC;AAGJ,QAAO"}