{"version":3,"file":"output_parser.cjs","names":["AgentActionOutputParser","OutputParserException","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;EAAQ;CAE/C;CAEA,YAAY,QAAiC;AAC3C,QAAM,GAAG,UAAU;AACnB,OAAK,YAAY,OAAO;;;;;;;;CAS1B,MAAM,MAAM,MAAkD;EAC5D,MAAM,iBAAiB,KAAK,SAAS,oBAAoB;EAGzD,MAAM,cAAc,KAAK,MADvB,yEACmC;AACrC,MAAI,aAAa;AACf,OAAI,eACF,OAAM,IAAIC,+BAAAA,sBACR,GAAG,+CAA+C,IAAI,OACvD;AAOH,UAAO;IACL,MALa,YAAY;IAMzB,WALkB,YAAY,GACF,MAAM,CAAC,QAAQ,UAAU,GAAG;IAKxD,KAAK;IACN;;AAGH,MAAI,eAEF,QAAO;GACL,cAAc,EACZ,QAHoB,KAAK,MAAM,oBAAoB,CAAC,GAAG,MAAM,EAI9D;GACD,KAAK;GACN;AAGH,QAAM,IAAIA,+BAAAA,sBAAsB,+BAA+B,OAAO;;;;;;;;CASxE,wBAAgC;AAC9B,UAAA,GAAA,wBAAA,gBAAsBC,eAAAA,qBAAqB,YAAY,EACrD,YAAY,KAAK,UAAU,KAAK,KAAK,EACtC,CAAC"}