{"version":3,"file":"output_parser.cjs","names":["OutputParserException","AgentMultiActionOutputParser","isBaseMessage"],"sources":["../../../src/agents/tool_calling/output_parser.ts"],"sourcesContent":["import { AgentAction, AgentFinish, AgentStep } from \"@langchain/core/agents\";\nimport {\n  AIMessage,\n  BaseMessage,\n  isBaseMessage,\n} from \"@langchain/core/messages\";\nimport { OutputParserException } from \"@langchain/core/output_parsers\";\nimport { ChatGeneration } from \"@langchain/core/outputs\";\nimport { ToolCall } from \"@langchain/core/messages/tool\";\nimport { AgentMultiActionOutputParser } from \"../types.js\";\n\n/**\n * Type that represents an agent action with an optional message log.\n */\nexport type ToolsAgentAction = AgentAction & {\n  toolCallId: string;\n  messageLog?: BaseMessage[];\n};\n\nexport type ToolsAgentStep = AgentStep & {\n  action: ToolsAgentAction;\n};\n\nexport function parseAIMessageToToolAction(\n  message: AIMessage\n): ToolsAgentAction[] | AgentFinish {\n  const stringifiedMessageContent =\n    typeof message.content === \"string\"\n      ? message.content\n      : JSON.stringify(message.content);\n  let toolCalls: ToolCall[] = [];\n  if (message.tool_calls !== undefined && message.tool_calls.length > 0) {\n    toolCalls = message.tool_calls;\n  } else {\n    if (\n      !message.additional_kwargs.tool_calls ||\n      message.additional_kwargs.tool_calls.length === 0\n    ) {\n      return {\n        returnValues: { output: message.content },\n        log: stringifiedMessageContent,\n      };\n    }\n    // Best effort parsing\n    for (const toolCall of message.additional_kwargs.tool_calls ?? []) {\n      const functionName = toolCall.function?.name;\n      try {\n        const args = JSON.parse(toolCall.function.arguments);\n        toolCalls.push({ name: functionName, args, id: toolCall.id });\n        // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n      } catch (e: any) {\n        throw new OutputParserException(\n          `Failed to parse tool arguments from chat model response. Text: \"${JSON.stringify(\n            toolCalls\n          )}\". ${e}`\n        );\n      }\n    }\n  }\n  return toolCalls.map((toolCall, i) => {\n    const messageLog = i === 0 ? [message] : [];\n    const log = `Invoking \"${toolCall.name}\" with ${JSON.stringify(\n      toolCall.args ?? {}\n    )}\\n${stringifiedMessageContent}`;\n    return {\n      tool: toolCall.name as string,\n      toolInput: toolCall.args,\n      toolCallId: toolCall.id ?? \"\",\n      log,\n      messageLog,\n    };\n  });\n}\n\nexport class ToolCallingAgentOutputParser extends AgentMultiActionOutputParser {\n  lc_namespace = [\"langchain\", \"agents\", \"tool_calling\"];\n\n  static lc_name() {\n    return \"ToolCallingAgentOutputParser\";\n  }\n\n  async parse(text: string): Promise<AgentAction[] | AgentFinish> {\n    throw new Error(\n      `ToolCallingAgentOutputParser can only parse messages.\\nPassed input: ${text}`\n    );\n  }\n\n  async parseResult(generations: ChatGeneration[]) {\n    if (\"message\" in generations[0] && isBaseMessage(generations[0].message)) {\n      return parseAIMessageToToolAction(generations[0].message);\n    }\n    throw new Error(\n      \"parseResult on ToolCallingAgentOutputParser only works on ChatGeneration output\"\n    );\n  }\n\n  getFormatInstructions(): string {\n    throw new Error(\n      \"getFormatInstructions not implemented inside ToolCallingAgentOutputParser.\"\n    );\n  }\n}\n"],"mappings":";;;;AAuBA,SAAgB,2BACd,SACkC;CAClC,MAAM,4BACJ,OAAO,QAAQ,YAAY,WACvB,QAAQ,UACR,KAAK,UAAU,QAAQ,OAAO;CACpC,IAAI,YAAwB,CAAC;CAC7B,IAAI,QAAQ,eAAe,KAAA,KAAa,QAAQ,WAAW,SAAS,GAClE,YAAY,QAAQ;MACf;EACL,IACE,CAAC,QAAQ,kBAAkB,cAC3B,QAAQ,kBAAkB,WAAW,WAAW,GAEhD,OAAO;GACL,cAAc,EAAE,QAAQ,QAAQ,QAAQ;GACxC,KAAK;EACP;EAGF,KAAK,MAAM,YAAY,QAAQ,kBAAkB,cAAc,CAAC,GAAG;GACjE,MAAM,eAAe,SAAS,UAAU;GACxC,IAAI;IACF,MAAM,OAAO,KAAK,MAAM,SAAS,SAAS,SAAS;IACnD,UAAU,KAAK;KAAE,MAAM;KAAc;KAAM,IAAI,SAAS;IAAG,CAAC;GAE9D,SAAS,GAAQ;IACf,MAA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