{"version":3,"file":"langserve.cjs","names":[],"sources":["../../../src/service-adapters/langchain/langserve.ts"],"sourcesContent":["import { Parameter, Action } from \"@copilotkit/shared\";\n\nexport interface RemoteChainParameters {\n  name: string;\n  description: string;\n  chainUrl: string;\n  parameters?: Parameter[];\n  parameterType?: \"single\" | \"multi\";\n}\n\nexport class RemoteChain {\n  name: string;\n  description: string;\n  chainUrl: string;\n  parameters?: Parameter[];\n  parameterType: \"single\" | \"multi\";\n\n  constructor(options: RemoteChainParameters) {\n    this.name = options.name;\n    this.description = options.description;\n    this.chainUrl = options.chainUrl;\n    this.parameters = options.parameters;\n    this.parameterType = options.parameterType || \"multi\";\n  }\n\n  async toAction(): Promise<Action<any>> {\n    if (!this.parameters) {\n      await this.inferLangServeParameters();\n    }\n\n    return {\n      name: this.name,\n      description: this.description,\n      parameters: this.parameters!,\n      handler: async (args: any) => {\n        // eslint-disable-next-line @typescript-eslint/no-var-requires\n        const { RemoteRunnable } = require(\"langchain/runnables/remote\");\n        const runnable = new RemoteRunnable({ url: this.chainUrl });\n        let input: any;\n        if (this.parameterType === \"single\") {\n          input = args[Object.keys(args)[0]];\n        } else {\n          input = args;\n        }\n        return await runnable.invoke(input);\n      },\n    };\n  }\n\n  async inferLangServeParameters() {\n    const supportedTypes = [\"string\", \"number\", \"boolean\"];\n\n    let schemaUrl = this.chainUrl.replace(/\\/+$/, \"\") + \"/input_schema\";\n    let schema = await fetch(schemaUrl)\n      .then((res) => res.json())\n      .catch(() => {\n        throw new Error(\"Failed to fetch langserve schema at \" + schemaUrl);\n      });\n    // for now, don't use json schema, just do a simple conversion\n\n    if (supportedTypes.includes(schema.type)) {\n      this.parameterType = \"single\";\n      this.parameters = [\n        {\n          name: \"input\",\n          type: schema.type,\n          description: \"The input to the chain\",\n        },\n      ];\n    } else if (schema.type === \"object\") {\n      this.parameterType = \"multi\";\n      this.parameters = Object.keys(schema.properties).map((key) => {\n        let property = schema.properties[key];\n        if (!supportedTypes.includes(property.type)) {\n          throw new Error(\"Unsupported schema type\");\n        }\n        return {\n          name: key,\n          type: property.type,\n          description: property.description || \"\",\n          required: schema.required?.includes(key) || false,\n        };\n      });\n    } else {\n      throw new Error(\"Unsupported schema type\");\n    }\n  }\n}\n"],"mappings":";;;AAUA,IAAa,cAAb,MAAyB;CAOvB,YAAY,SAAgC;AAC1C,OAAK,OAAO,QAAQ;AACpB,OAAK,cAAc,QAAQ;AAC3B,OAAK,WAAW,QAAQ;AACxB,OAAK,aAAa,QAAQ;AAC1B,OAAK,gBAAgB,QAAQ,iBAAiB;;CAGhD,MAAM,WAAiC;AACrC,MAAI,CAAC,KAAK,WACR,OAAM,KAAK,0BAA0B;AAGvC,SAAO;GACL,MAAM,KAAK;GACX,aAAa,KAAK;GAClB,YAAY,KAAK;GACjB,SAAS,OAAO,SAAc;IAE5B,MAAM,EAAE,mBAAmB,QAAQ,6BAA6B;IAChE,MAAM,WAAW,IAAI,eAAe,EAAE,KAAK,KAAK,UAAU,CAAC;IAC3D,IAAI;AACJ,QAAI,KAAK,kBAAkB,SACzB,SAAQ,KAAK,OAAO,KAAK,KAAK,CAAC;QAE/B,SAAQ;AAEV,WAAO,MAAM,SAAS,OAAO,MAAM;;GAEtC;;CAGH,MAAM,2BAA2B;EAC/B,MAAM,iBAAiB;GAAC;GAAU;GAAU;GAAU;EAEtD,IAAI,YAAY,KAAK,SAAS,QAAQ,QAAQ,GAAG,GAAG;EACpD,IAAI,SAAS,MAAM,MAAM,UAAU,CAChC,MAAM,QAAQ,IAAI,MAAM,CAAC,CACzB,YAAY;AACX,SAAM,IAAI,MAAM,yCAAyC,UAAU;IACnE;AAGJ,MAAI,eAAe,SAAS,OAAO,KAAK,EAAE;AACxC,QAAK,gBAAgB;AACrB,QAAK,aAAa,CAChB;IACE,MAAM;IACN,MAAM,OAAO;IACb,aAAa;IACd,CACF;aACQ,OAAO,SAAS,UAAU;AACnC,QAAK,gBAAgB;AACrB,QAAK,aAAa,OAAO,KAAK,OAAO,WAAW,CAAC,KAAK,QAAQ;IAC5D,IAAI,WAAW,OAAO,WAAW;AACjC,QAAI,CAAC,eAAe,SAAS,SAAS,KAAK,CACzC,OAAM,IAAI,MAAM,0BAA0B;AAE5C,WAAO;KACL,MAAM;KACN,MAAM,SAAS;KACf,aAAa,SAAS,eAAe;KACrC,UAAU,OAAO,UAAU,SAAS,IAAI,IAAI;KAC7C;KACD;QAEF,OAAM,IAAI,MAAM,0BAA0B"}