{"version":3,"file":"handoff.cjs","names":["ToolMessage","Command","z","AIMessage"],"sources":["../src/handoff.ts"],"sourcesContent":["import { v4 as uuidv4 } from \"@langchain/core/utils/uuid\";\nimport { z } from \"zod\";\nimport { AIMessage, ToolMessage } from \"@langchain/core/messages\";\nimport { tool } from \"@langchain/core/tools\";\nimport {\n  Command,\n  MessagesAnnotation,\n  getCurrentTaskInput,\n} from \"@langchain/langgraph\";\n\nconst WHITESPACE_RE = /\\s+/g;\n\nfunction _normalizeAgentName(agentName: string): string {\n  /**\n   * Normalize an agent name to be used inside the tool name.\n   */\n  return agentName.trim().replace(WHITESPACE_RE, \"_\").toLowerCase();\n}\n\nconst createHandoffTool = ({\n  agentName,\n  description,\n  agentDescription,\n  addHandoffMessages = true,\n}: {\n  agentName: string;\n  description?: string;\n  /**\n   * @deprecated Use `description` instead.\n   */\n  agentDescription?: string;\n  addHandoffMessages?: boolean;\n}) => {\n  /**\n   * Create a tool that can handoff control to the requested agent.\n   *\n   * @param agentName - The name of the agent to handoff control to, i.e.\n   *   the name of the agent node in the multi-agent graph.\n   *   Agent names should be simple, clear and unique, preferably in snake_case,\n   *   although you are only limited to the names accepted by LangGraph\n   *   nodes as well as the tool names accepted by LLM providers\n   *   (the tool name will look like this: `transfer_to_<agent_name>`).\n   * @param description - Optional description for the handoff tool.\n   * @param agentDescription - Deprecated. Use `description` instead.\n   * @param addHandoffMessages - Whether to add the handoff messages to the\n   *   message history forwarded to the expert agent. If `false`, the\n   *   supervisor `AIMessage` containing the handoff tool call and the handoff\n   *   `ToolMessage` are omitted from the expert agent's message history.\n   *   Defaults to `true`.\n   */\n  const toolName = `transfer_to_${_normalizeAgentName(agentName)}`;\n\n  const handoffTool = tool(\n    async (_, config) => {\n      /**\n       * Ask another agent for help.\n       */\n      // inject the current agent state\n      const state =\n        getCurrentTaskInput() as (typeof MessagesAnnotation)[\"State\"];\n\n      let messages = state.messages;\n      if (addHandoffMessages) {\n        const toolMessage = new ToolMessage({\n          content: `Successfully transferred to ${agentName}`,\n          name: toolName,\n          tool_call_id: config.toolCall.id,\n        });\n        messages = messages.concat(toolMessage);\n      } else {\n        // omit the supervisor AIMessage containing the handoff tool call\n        // (and the handoff ToolMessage) from the expert agent's history\n        messages = messages.slice(0, -1);\n      }\n\n      return new Command({\n        goto: agentName,\n        graph: Command.PARENT,\n        update: { messages },\n      });\n    },\n    {\n      name: toolName,\n      schema: z.object({}),\n      description:\n        description ?? agentDescription ?? \"Ask another agent for help.\",\n    }\n  );\n  return handoffTool;\n};\n\nfunction createHandoffBackMessages(\n  agentName: string,\n  supervisorName: string\n): [AIMessage, ToolMessage] {\n  /**\n   * Create a pair of (AIMessage, ToolMessage) to add to the message history when returning control to the supervisor.\n   *\n   * @deprecated This helper is intended for supervisor internals. Prefer configuring handoff behavior through `createSupervisor`.\n   */\n  const toolCallId = uuidv4();\n  const toolName = `transfer_back_to_${_normalizeAgentName(supervisorName)}`;\n  const toolCalls = [{ name: toolName, args: {}, id: toolCallId }];\n\n  return [\n    new AIMessage({\n      content: `Transferring back to ${supervisorName}`,\n      tool_calls: toolCalls,\n      name: agentName,\n    }),\n    new ToolMessage({\n      content: `Successfully transferred back to ${supervisorName}`,\n      name: toolName,\n      tool_call_id: toolCallId,\n    }),\n  ];\n}\n\nexport { createHandoffTool, createHandoffBackMessages };\n"],"mappings":";;;;;;AAUA,MAAM,gBAAgB;AAEtB,SAAS,oBAAoB,WAA2B;;;;AAItD,QAAO,UAAU,MAAM,CAAC,QAAQ,eAAe,IAAI,CAAC,aAAa;;AAGnE,MAAM,qBAAqB,EACzB,WACA,aACA,kBACA,qBAAqB,WASjB;;;;;;;;;;;;;;;;;;CAkBJ,MAAM,WAAW,eAAe,oBAAoB,UAAU;AAsC9D,SAAA,GAAA,sBAAA,MAnCE,OAAO,GAAG,WAAW;EAQnB,IAAI,YAAA,GAAA,qBAAA,sBAFmB,CAEF;AACrB,MAAI,oBAAoB;GACtB,MAAM,cAAc,IAAIA,yBAAAA,YAAY;IAClC,SAAS,+BAA+B;IACxC,MAAM;IACN,cAAc,OAAO,SAAS;IAC/B,CAAC;AACF,cAAW,SAAS,OAAO,YAAY;QAIvC,YAAW,SAAS,MAAM,GAAG,GAAG;AAGlC,SAAO,IAAIC,qBAAAA,QAAQ;GACjB,MAAM;GACN,OAAOA,qBAAAA,QAAQ;GACf,QAAQ,EAAE,UAAU;GACrB,CAAC;IAEJ;EACE,MAAM;EACN,QAAQC,IAAAA,EAAE,OAAO,EAAE,CAAC;EACpB,aACE,eAAe,oBAAoB;EACtC,CACF;;AAIH,SAAS,0BACP,WACA,gBAC0B;;;;;;CAM1B,MAAM,cAAA,GAAA,2BAAA,KAAqB;CAC3B,MAAM,WAAW,oBAAoB,oBAAoB,eAAe;CACxE,MAAM,YAAY,CAAC;EAAE,MAAM;EAAU,MAAM,EAAE;EAAE,IAAI;EAAY,CAAC;AAEhE,QAAO,CACL,IAAIC,yBAAAA,UAAU;EACZ,SAAS,wBAAwB;EACjC,YAAY;EACZ,MAAM;EACP,CAAC,EACF,IAAIH,yBAAAA,YAAY;EACd,SAAS,oCAAoC;EAC7C,MAAM;EACN,cAAc;EACf,CAAC,CACH"}