{"version":3,"file":"output_parser.cjs","names":["AgentActionOutputParser","OutputParserException","renderTemplate","FORMAT_INSTRUCTIONS"],"sources":["../../../src/agents/react/output_parser.ts"],"sourcesContent":["import { AgentAction, AgentFinish } from \"@langchain/core/agents\";\nimport { renderTemplate } from \"@langchain/core/prompts\";\nimport { OutputParserException } from \"@langchain/core/output_parsers\";\nimport { AgentActionOutputParser } from \"../types.js\";\nimport { FORMAT_INSTRUCTIONS } from \"./prompt.js\";\n\nconst FINAL_ANSWER_ACTION = \"Final Answer:\";\nconst FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE =\n  \"Parsing LLM output produced both a final answer and a parse-able action:\";\n\n/**\n * Parses ReAct-style LLM calls that have a single tool input.\n *\n * Expects output to be in one of two formats.\n *\n * If the output signals that an action should be taken,\n * should be in the below format. This will result in an AgentAction\n * being returned.\n *\n * ```\n * Thought: agent thought here\n * Action: search\n * Action Input: what is the temperature in SF?\n * ```\n *\n * If the output signals that a final answer should be given,\n * should be in the below format. This will result in an AgentFinish\n * being returned.\n *\n * ```\n * Thought: agent thought here\n * Final Answer: The temperature is 100 degrees\n * ```\n * @example\n * ```typescript\n *\n * const runnableAgent = RunnableSequence.from([\n *   ...rest of runnable\n *   new ReActSingleInputOutputParser({ toolNames: [\"SerpAPI\", \"Calculator\"] }),\n * ]);\n * const agent = AgentExecutor.fromAgentAndTools({\n *   agent: runnableAgent,\n *   tools: [new SerpAPI(), new Calculator()],\n * });\n * const result = await agent.invoke({\n *   input: \"whats the weather in pomfret?\",\n * });\n * ```\n */\nexport class ReActSingleInputOutputParser extends AgentActionOutputParser {\n  lc_namespace = [\"langchain\", \"agents\", \"react\"];\n\n  private toolNames: string[];\n\n  constructor(fields: { toolNames: string[] }) {\n    super(...arguments);\n    this.toolNames = fields.toolNames;\n  }\n\n  /**\n   * Parses the given text into an AgentAction or AgentFinish object. If an\n   * output fixing parser is defined, uses it to parse the text.\n   * @param text Text to parse.\n   * @returns Promise that resolves to an AgentAction or AgentFinish object.\n   */\n  async parse(text: string): Promise<AgentAction | AgentFinish> {\n    const includesAnswer = text.includes(FINAL_ANSWER_ACTION);\n    const regex =\n      /Action\\s*\\d*\\s*:[\\s]*(.*?)[\\s]*Action\\s*\\d*\\s*Input\\s*\\d*\\s*:[\\s]*(.*)/;\n    const actionMatch = text.match(regex);\n    if (actionMatch) {\n      if (includesAnswer) {\n        throw new OutputParserException(\n          `${FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE}: ${text}`\n        );\n      }\n\n      const action = actionMatch[1];\n      const actionInput = actionMatch[2];\n      const toolInput = actionInput.trim().replace(/^\"|\"$/g, \"\");\n\n      return {\n        tool: action,\n        toolInput,\n        log: text,\n      };\n    }\n\n    if (includesAnswer) {\n      const finalAnswerText = text.split(FINAL_ANSWER_ACTION)[1].trim();\n      return {\n        returnValues: {\n          output: finalAnswerText,\n        },\n        log: text,\n      };\n    }\n\n    throw new OutputParserException(`Could not parse LLM output: ${text}`);\n  }\n\n  /**\n   * Returns the format instructions as a string. If the 'raw' option is\n   * true, returns the raw FORMAT_INSTRUCTIONS.\n   * @param options Options for getting the format instructions.\n   * @returns Format instructions as a string.\n   */\n  getFormatInstructions(): string {\n    return renderTemplate(FORMAT_INSTRUCTIONS, \"f-string\", {\n      tool_names: this.toolNames.join(\", \"),\n    });\n  }\n}\n"],"mappings":";;;;;;;;AAMA,MAAM,sBAAsB;AAC5B,MAAM,iDACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCF,IAAa,+BAAb,cAAkDA,cAAAA,wBAAwB;CACxE,eAAe;EAAC;EAAa;EAAU;CAAO;CAE9C;CAEA,YAAY,QAAiC;EAC3C,MAAM,GAAG,SAAS;EAClB,KAAK,YAAY,OAAO;CAC1B;;;;;;;CAQA,MAAM,MAAM,MAAkD;EAC5D,MAAM,iBAAiB,KAAK,SAAS,mBAAmB;EAGxD,MAAM,cAAc,KAAK,MAAM,wEAAK;EACpC,IAAI,aAAa;GACf,IAAI,gBACF,MAAM,IAAIC,+BAAAA,sBACR,GAAG,+CAA+C,IAAI,MACxD;GAOF,OAAO;IACL,MALa,YAAY;IAMzB,WALkB,YAAY,EACH,CAAC,KAAK,CAAC,CAAC,QAAQ,UAAU,EAI7C;IACR,KAAK;GACP;EACF;EAEA,IAAI,gBAEF,OAAO;GACL,cAAc,EACZ,QAHoB,KAAK,MAAM,mBAAmB,CAAC,CAAC,EAAE,CAAC,KAGjC,EACxB;GACA,KAAK;EACP;EAGF,MAAM,IAAIA,+BAAAA,sBAAsB,+BAA+B,MAAM;CACvE;;;;;;;CAQA,wBAAgC;EAC9B,QAAA,GAAOC,wBAAAA,eAAAA,CAAeC,eAAAA,qBAAqB,YAAY,EACrD,YAAY,KAAK,UAAU,KAAK,IAAI,EACtC,CAAC;CACH;AACF"}