{"version":3,"file":"output_parser.cjs","names":["OutputParserException","AgentMultiActionOutputParser"],"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,QAAQ;CACrC,IAAI,YAAwB,EAAE;AAC9B,KAAI,QAAQ,eAAe,KAAA,KAAa,QAAQ,WAAW,SAAS,EAClE,aAAY,QAAQ;MACf;AACL,MACE,CAAC,QAAQ,kBAAkB,cAC3B,QAAQ,kBAAkB,WAAW,WAAW,EAEhD,QAAO;GACL,cAAc,EAAE,QAAQ,QAAQ,SAAS;GACzC,KAAK;GACN;AAGH,OAAK,MAAM,YAAY,QAAQ,kBAAkB,cAAc,EAAE,EAAE;GACjE,MAAM,eAAe,SAAS,UAAU;AACxC,OAAI;IACF,MAAM,OAAO,KAAK,MAAM,SAAS,SAAS,UAAU;AACpD,cAAU,KAAK;KAAE,MAAM;KAAc;KAAM,IAAI,SAAS;KAAI,CAAC;YAEtD,GAAQ;AACf,UAAM,IAAIA,+BAA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