{"version":3,"file":"output_parsers.cjs","names":["BaseLLMOutputParser","OutputParserException"],"sources":["../src/output_parsers.ts"],"sourcesContent":["import {\n  BaseLLMOutputParser,\n  OutputParserException,\n} from \"@langchain/core/output_parsers\";\nimport {\n  JsonOutputKeyToolsParserParamsInterop,\n  JsonOutputKeyToolsParserParamsSerializable,\n} from \"@langchain/core/output_parsers/openai_tools\";\nimport { ChatGeneration } from \"@langchain/core/outputs\";\nimport { ToolCall } from \"@langchain/core/messages/tool\";\nimport {\n  interopSafeParseAsync,\n  InteropZodType,\n} from \"@langchain/core/utils/types\";\nimport { SerializableSchema } from \"@langchain/core/utils/standard_schema\";\n\ninterface AnthropicToolsOutputParserParams<\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  T extends Record<string, any>,\n>\n  extends\n    JsonOutputKeyToolsParserParamsInterop<T>,\n    JsonOutputKeyToolsParserParamsSerializable<T> {}\n\nexport class AnthropicToolsOutputParser<\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  T extends Record<string, any> = Record<string, any>,\n> extends BaseLLMOutputParser<T> {\n  static lc_name() {\n    return \"AnthropicToolsOutputParser\";\n  }\n\n  lc_namespace = [\"langchain\", \"anthropic\", \"output_parsers\"];\n\n  returnId = false;\n\n  /** The type of tool calls to return. */\n  keyName: string;\n\n  /** Whether to return only the first tool call. */\n  returnSingle = false;\n\n  zodSchema?: InteropZodType<T>;\n\n  serializableSchema?: SerializableSchema<T>;\n\n  constructor(params: AnthropicToolsOutputParserParams<T>) {\n    super(params);\n    this.keyName = params.keyName;\n    this.returnSingle = params.returnSingle ?? this.returnSingle;\n    this.zodSchema = params.zodSchema;\n    this.serializableSchema = params.serializableSchema;\n  }\n\n  protected async _validateResult(result: unknown): Promise<T> {\n    let parsedResult = result;\n    if (typeof result === \"string\") {\n      try {\n        parsedResult = JSON.parse(result);\n        // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n      } catch (e: any) {\n        throw new OutputParserException(\n          `Failed to parse. Text: \"${JSON.stringify(\n            result,\n            null,\n            2\n          )}\". Error: ${JSON.stringify(e.message)}`,\n          result\n        );\n      }\n    } else {\n      parsedResult = result;\n    }\n\n    if (this.serializableSchema !== undefined) {\n      const validated =\n        await this.serializableSchema[\"~standard\"].validate(parsedResult);\n      if (validated.issues) {\n        throw new OutputParserException(\n          `Failed to parse. Text: \"${JSON.stringify(\n            parsedResult,\n            null,\n            2\n          )}\". Error: ${JSON.stringify(validated.issues)}`,\n          JSON.stringify(parsedResult, null, 2)\n        );\n      }\n      return validated.value as T;\n    }\n\n    if (this.zodSchema === undefined) {\n      return parsedResult as T;\n    }\n    const zodParsedResult = await interopSafeParseAsync(\n      this.zodSchema,\n      parsedResult\n    );\n    if (zodParsedResult.success) {\n      return zodParsedResult.data;\n    } else {\n      throw new OutputParserException(\n        `Failed to parse. Text: \"${JSON.stringify(\n          result,\n          null,\n          2\n        )}\". Error: ${JSON.stringify(zodParsedResult.error.issues)}`,\n        JSON.stringify(parsedResult, null, 2)\n      );\n    }\n  }\n\n  async parseResult(generations: ChatGeneration[]): Promise<T> {\n    const tools = generations.flatMap((generation) => {\n      const { message } = generation;\n      if (!Array.isArray(message.content)) {\n        return [];\n      }\n      const tool = extractToolCalls(message.content)[0];\n      return tool;\n    });\n    if (tools[0] === undefined) {\n      throw new Error(\n        \"No parseable tool calls provided to AnthropicToolsOutputParser.\"\n      );\n    }\n    const [tool] = tools;\n    const validatedResult = await this._validateResult(tool.args);\n    return validatedResult;\n  }\n}\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport function extractToolCalls(content: Record<string, any>[]) {\n  const toolCalls: ToolCall[] = [];\n\n  for (const block of content) {\n    if (block.type === \"tool_use\") {\n      toolCalls.push({\n        name: block.name,\n        args: block.input,\n        id: block.id,\n        type: \"tool_call\",\n      });\n    }\n  }\n\n  return toolCalls;\n}\n"],"mappings":";;;AAwBA,IAAa,6BAAb,cAGUA,+BAAAA,oBAAuB;CAC/B,OAAO,UAAU;EACf,OA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