UNPKG

@langchain/community

Version:
1 lines 3.29 kB
{"version":3,"file":"aiplugin.cjs","names":["Tool"],"sources":["../../src/tools/aiplugin.ts"],"sourcesContent":["import { Tool, type ToolParams } from \"@langchain/core/tools\";\n\n/**\n * Interface for parameters required to create an instance of\n * AIPluginTool.\n */\nexport interface AIPluginToolParams extends ToolParams {\n name: string;\n description: string;\n apiSpec: string;\n}\n\n/**\n * Class for creating instances of AI tools from plugins. It extends the\n * Tool class and implements the AIPluginToolParams interface.\n */\nexport class AIPluginTool extends Tool implements AIPluginToolParams {\n static lc_name() {\n return \"AIPluginTool\";\n }\n\n private _name: string;\n\n private _description: string;\n\n apiSpec: string;\n\n get name() {\n return this._name;\n }\n\n get description() {\n return this._description;\n }\n\n constructor(params: AIPluginToolParams) {\n super(params);\n this._name = params.name;\n this._description = params.description;\n this.apiSpec = params.apiSpec;\n }\n\n /** @ignore */\n async _call(_input: string) {\n return this.apiSpec;\n }\n\n /**\n * Static method that creates an instance of AIPluginTool from a given\n * plugin URL. It fetches the plugin and its API specification from the\n * provided URL and returns a new instance of AIPluginTool with the\n * fetched data.\n * @param url The URL of the AI plugin.\n * @returns A new instance of AIPluginTool.\n */\n static async fromPluginUrl(url: string) {\n const aiPluginRes = await fetch(url);\n if (!aiPluginRes.ok) {\n throw new Error(\n `Failed to fetch plugin from ${url} with status ${aiPluginRes.status}`\n );\n }\n const aiPluginJson = await aiPluginRes.json();\n\n const apiUrlRes = await fetch(aiPluginJson.api.url);\n if (!apiUrlRes.ok) {\n throw new Error(\n `Failed to fetch API spec from ${aiPluginJson.api.url} with status ${apiUrlRes.status}`\n );\n }\n const apiUrlJson = await apiUrlRes.text();\n\n return new AIPluginTool({\n name: aiPluginJson.name_for_model,\n description: `Call this tool to get the OpenAPI spec (and usage guide) for interacting with the ${aiPluginJson.name_for_human} API. You should only call this ONCE! What is the ${aiPluginJson.name_for_human} API useful for? ${aiPluginJson.description_for_human}`,\n apiSpec: `Usage Guide: ${aiPluginJson.description_for_model}\n\nOpenAPI Spec in JSON or YAML format:\\n${apiUrlJson}`,\n });\n }\n}\n"],"mappings":";;;;;;;;;AAgBA,IAAa,eAAb,MAAa,qBAAqBA,sBAAAA,KAAmC;CACnE,OAAO,UAAU;AACf,SAAO;;CAGT;CAEA;CAEA;CAEA,IAAI,OAAO;AACT,SAAO,KAAK;;CAGd,IAAI,cAAc;AAChB,SAAO,KAAK;;CAGd,YAAY,QAA4B;AACtC,QAAM,OAAO;AACb,OAAK,QAAQ,OAAO;AACpB,OAAK,eAAe,OAAO;AAC3B,OAAK,UAAU,OAAO;;;CAIxB,MAAM,MAAM,QAAgB;AAC1B,SAAO,KAAK;;;;;;;;;;CAWd,aAAa,cAAc,KAAa;EACtC,MAAM,cAAc,MAAM,MAAM,IAAI;AACpC,MAAI,CAAC,YAAY,GACf,OAAM,IAAI,MACR,+BAA+B,IAAI,eAAe,YAAY,SAC/D;EAEH,MAAM,eAAe,MAAM,YAAY,MAAM;EAE7C,MAAM,YAAY,MAAM,MAAM,aAAa,IAAI,IAAI;AACnD,MAAI,CAAC,UAAU,GACb,OAAM,IAAI,MACR,iCAAiC,aAAa,IAAI,IAAI,eAAe,UAAU,SAChF;EAEH,MAAM,aAAa,MAAM,UAAU,MAAM;AAEzC,SAAO,IAAI,aAAa;GACtB,MAAM,aAAa;GACnB,aAAa,qFAAqF,aAAa,eAAe,oDAAoD,aAAa,eAAe,mBAAmB,aAAa;GAC9O,SAAS,gBAAgB,aAAa,sBAAsB;;wCAE1B;GACnC,CAAC"}