{"version":3,"file":"toolError.cjs","names":["createMiddleware","ToolMessage"],"sources":["../../../src/agents/middleware/toolError.ts"],"sourcesContent":["/**\n * Tool error middleware for agents.\n */\nimport { ToolMessage, type MessageContent } from \"@langchain/core/messages\";\nimport type { ClientTool, ServerTool } from \"@langchain/core/tools\";\nimport { isGraphBubbleUp } from \"@langchain/langgraph\";\n\nimport { createMiddleware } from \"../middleware.js\";\nimport type { ToolCallRequest } from \"./types.js\";\n\n/**\n * Handler called when tool execution throws.\n *\n * Return content to surface the error to the model as an error `ToolMessage`.\n * Return nothing to propagate the original error.\n */\nexport type ToolErrorHandler = (\n  error: unknown,\n  request: ToolCallRequest\n) => MessageContent | void | Promise<MessageContent | void>;\n\n/**\n * Configuration for {@link toolErrorMiddleware}.\n */\nexport interface ToolErrorMiddlewareConfig {\n  /**\n   * Handler called when tool execution throws. Return content to convert the\n   * error into an error `ToolMessage`, or return nothing to propagate it.\n   */\n  onError: ToolErrorHandler;\n\n  /**\n   * Optional tools or tool names to handle errors for. Handles all tools when\n   * omitted.\n   */\n  tools?: Array<ClientTool | ServerTool | string>;\n}\n\n/**\n * Converts selected tool execution errors into error `ToolMessage`s.\n *\n * Handling is opt-in: {@link ToolErrorMiddlewareConfig.onError} must return\n * content for an error to be sent to the model. Returning nothing propagates\n * the original error unchanged. LangGraph control-flow signals always\n * propagate and are never passed to `onError`.\n *\n * Prefer returning content that identifies the error type without including\n * the raw error message, which may contain sensitive or internal details.\n *\n * This middleware does not retry. To retry before handling an error, place\n * `toolRetryMiddleware({ onFailure: \"error\" })` after this middleware.\n *\n * @example\n * ```ts\n * import { createAgent, toolErrorMiddleware } from \"langchain\";\n *\n * const agent = createAgent({\n *   model,\n *   tools: [searchTool],\n *   middleware: [\n *     toolErrorMiddleware({\n *       onError: (error, request) => {\n *         if (error instanceof TypeError) {\n *           return `Tool '${request.toolCall.name}' failed with TypeError.`;\n *         }\n *       },\n *     }),\n *   ],\n * });\n * ```\n */\nexport function toolErrorMiddleware(config: ToolErrorMiddlewareConfig) {\n  const toolFilter = config.tools?.map((tool) =>\n    typeof tool === \"string\" ? tool : tool.name\n  );\n\n  return createMiddleware({\n    name: \"toolErrorMiddleware\",\n    wrapToolCall: async (request, handler) => {\n      const toolName = request.tool?.name ?? request.toolCall.name;\n\n      if (toolFilter !== undefined && !toolFilter.includes(toolName)) {\n        return handler(request);\n      }\n\n      try {\n        return await handler(request);\n      } catch (error: unknown) {\n        if (isGraphBubbleUp(error)) {\n          throw error;\n        }\n\n        const content = await config.onError(error, request);\n        if (content === undefined) {\n          throw error;\n        }\n\n        return new ToolMessage({\n          content,\n          tool_call_id: request.toolCall.id ?? \"\",\n          ...(typeof toolName === \"string\" ? { name: toolName } : {}),\n          status: \"error\",\n        });\n      }\n    },\n  });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,SAAgB,oBAAoB,QAAmC;CACrE,MAAM,aAAa,OAAO,OAAO,KAAK,SACpC,OAAO,SAAS,WAAW,OAAO,KAAK,IACzC;CAEA,OAAOA,mBAAAA,iBAAiB;EACtB,MAAM;EACN,cAAc,OAAO,SAAS,YAAY;GACxC,MAAM,WAAW,QAAQ,MAAM,QAAQ,QAAQ,SAAS;GAExD,IAAI,eAAe,KAAA,KAAa,CAAC,WAAW,SAAS,QAAQ,GAC3D,OAAO,QAAQ,OAAO;GAGxB,IAAI;IACF,OAAO,MAAM,QAAQ,OAAO;GAC9B,SAAS,OAAgB;IACvB,KAAA,GAAA,qBAAA,gBAAA,CAAoB,KAAK,GACvB,MAAM;IAGR,MAAM,UAAU,MAAM,OAAO,QAAQ,OAAO,OAAO;IACnD,IAAI,YAAY,KAAA,GACd,MAAM;IAGR,OAAO,IAAIC,yBAAAA,YAAY;KACrB;KACA,cAAc,QAAQ,SAAS,MAAM;KACrC,GAAI,OAAO,aAAa,WAAW,EAAE,MAAM,SAAS,IAAI,CAAC;KACzD,QAAQ;IACV,CAAC;GACH;EACF;CACF,CAAC;AACH"}