{"version":3,"file":"openapi.cjs","names":["BaseToolkit","RequestsGetTool","RequestsPostTool","createJsonAgent","JsonToolkit","DynamicTool","JSON_EXPLORER_DESCRIPTION","OPENAPI_PREFIX","OPENAPI_SUFFIX","ZeroShotAgent","LLMChain","AgentExecutor"],"sources":["../../../../src/agents/toolkits/openapi/openapi.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { ToolInterface } from \"@langchain/core/tools\";\nimport { DynamicTool, BaseToolkit } from \"@langchain/core/tools\";\nimport { JsonSpec } from \"../../../tools/json.js\";\nimport { AgentExecutor } from \"../../executor.js\";\nimport {\n  OPENAPI_PREFIX,\n  OPENAPI_SUFFIX,\n  JSON_EXPLORER_DESCRIPTION,\n} from \"./prompt.js\";\nimport { LLMChain } from \"../../../chains/llm_chain.js\";\nimport { ZeroShotCreatePromptArgs, ZeroShotAgent } from \"../../mrkl/index.js\";\nimport {\n  Headers,\n  RequestsGetTool,\n  RequestsPostTool,\n} from \"../../../tools/requests.js\";\nimport { createJsonAgent, JsonToolkit } from \"../json/json.js\";\n\n/**\n * Represents a toolkit for making HTTP requests. It initializes the\n * request tools based on the provided headers.\n */\nexport class RequestsToolkit extends BaseToolkit {\n  tools: ToolInterface[];\n\n  constructor(headers?: Headers) {\n    super();\n    this.tools = [new RequestsGetTool(headers), new RequestsPostTool(headers)];\n  }\n}\n\n/**\n * Extends the `RequestsToolkit` class and adds a dynamic tool for\n * exploring JSON data. It creates a JSON agent using the `JsonToolkit`\n * and the provided language model, and adds the JSON explorer tool to the\n * toolkit.\n * @example\n * ```typescript\n * const toolkit = new OpenApiToolkit(\n *   new JsonSpec({\n *   }),\n *   new ChatOpenAI({ model: \"gpt-4o-mini\", temperature: 0 }),\n *   {\n *     \"Content-Type\": \"application/json\",\n *     Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,\n *   },\n * );\n *\n * const result = await toolkit.invoke({\n *   input:\n *     \"Make a POST request to openai /completions. The prompt should be 'tell me a joke.'\",\n * });\n * console.log(`Got output ${result.output}`);\n * ```\n */\nexport class OpenApiToolkit extends RequestsToolkit {\n  constructor(\n    jsonSpec: JsonSpec,\n    llm: BaseLanguageModelInterface,\n    headers?: Headers\n  ) {\n    super(headers);\n    const jsonAgent = createJsonAgent(llm, new JsonToolkit(jsonSpec));\n    this.tools = [\n      ...this.tools,\n      new DynamicTool({\n        name: \"json_explorer\",\n        func: async (input: string) => {\n          const result = await jsonAgent.call({ input });\n          return result.output as string;\n        },\n        description: JSON_EXPLORER_DESCRIPTION,\n      }),\n    ];\n  }\n}\n\n/**\n * Creates an OpenAPI agent using a language model, an OpenAPI toolkit,\n * and optional prompt arguments. It creates a prompt for the agent using\n * the OpenAPI tools and the provided prefix and suffix. It then creates a\n * ZeroShotAgent with the prompt and the OpenAPI tools, and returns an\n * AgentExecutor for executing the agent with the tools.\n * @param llm The language model to use.\n * @param openApiToolkit The OpenAPI toolkit to use.\n * @param args Optional arguments for creating the prompt.\n * @returns An AgentExecutor for executing the agent with the tools.\n *\n * @security **Security Notice** This agent provides access to external APIs.\n * Use with caution as this agent can make API calls with arbitrary headers.\n * Exposing this agent to users could lead to security vulnerabilities. Consider\n * limiting access to what endpoints it can hit, what actions can be taken, and\n * more.\n *\n * @link See https://js.langchain.com/docs/security for more information.\n */\nexport function createOpenApiAgent(\n  llm: BaseLanguageModelInterface,\n  openApiToolkit: OpenApiToolkit,\n  args?: ZeroShotCreatePromptArgs\n) {\n  const {\n    prefix = OPENAPI_PREFIX,\n    suffix = OPENAPI_SUFFIX,\n    inputVariables = [\"input\", \"agent_scratchpad\"],\n  } = args ?? {};\n\n  const { tools } = openApiToolkit;\n  const prompt = ZeroShotAgent.createPrompt(tools, {\n    prefix,\n    suffix,\n    inputVariables,\n  });\n  const chain = new LLMChain({\n    prompt,\n    llm,\n  });\n  const toolNames = tools.map((tool) => tool.name);\n  const agent = new ZeroShotAgent({ llmChain: chain, allowedTools: toolNames });\n  return AgentExecutor.fromAgentAndTools({\n    agent,\n    tools,\n    returnIntermediateSteps: true,\n  });\n}\n"],"mappings":";;;;;;;;;;;;;AAuBA,IAAa,kBAAb,cAAqCA,sBAAAA,YAAY;CAC/C;CAEA,YAAY,SAAmB;AAC7B,SAAO;AACP,OAAK,QAAQ,CAAC,IAAIC,iBAAAA,gBAAgB,QAAQ,EAAE,IAAIC,iBAAAA,iBAAiB,QAAQ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4B9E,IAAa,iBAAb,cAAoC,gBAAgB;CAClD,YACE,UACA,KACA,SACA;AACA,QAAM,QAAQ;EACd,MAAM,YAAYC,aAAAA,gBAAgB,KAAK,IAAIC,aAAAA,YAAY,SAAS,CAAC;AACjE,OAAK,QAAQ,CACX,GAAG,KAAK,OACR,IAAIC,sBAAAA,YAAY;GACd,MAAM;GACN,MAAM,OAAO,UAAkB;AAE7B,YADe,MAAM,UAAU,KAAK,EAAE,OAAO,CAAC,EAChC;;GAEhB,aAAaC,eAAAA;GACd,CAAC,CACH;;;;;;;;;;;;;;;;;;;;;;AAuBL,SAAgB,mBACd,KACA,gBACA,MACA;CACA,MAAM,EACJ,SAASC,eAAAA,gBACT,SAASC,eAAAA,gBACT,iBAAiB,CAAC,SAAS,mBAAmB,KAC5C,QAAQ,EAAE;CAEd,MAAM,EAAE,UAAU;CAWlB,MAAM,QAAQ,IAAIC,cAAAA,cAAc;EAAE,UALpB,IAAIC,kBAAAA,SAAS;GACzB,QANaD,cAAAA,cAAc,aAAa,OAAO;IAC/C;IACA;IACA;IACD,CAAC;GAGA;GACD,CAAC;EAEiD,cADjC,MAAM,KAAK,SAAS,KAAK,KAAK;EAC4B,CAAC;AAC7E,QAAOE,iBAAAA,cAAc,kBAAkB;EACrC;EACA;EACA,yBAAyB;EAC1B,CAAC"}