{"version":3,"file":"base.cjs","names":["BaseRetriever","AsyncCaller"],"sources":["../../../src/retrievers/remote/base.ts"],"sourcesContent":["import {\n  BaseRetriever,\n  type BaseRetrieverInput,\n} from \"@langchain/core/retrievers\";\nimport {\n  AsyncCaller,\n  type AsyncCallerParams,\n} from \"@langchain/core/utils/async_caller\";\nimport type { DocumentInterface } from \"@langchain/core/documents\";\n\n/**\n * Type for the authentication method used by the RemoteRetriever. It can\n * either be false (no authentication) or an object with a bearer token.\n */\nexport type RemoteRetrieverAuth = false | { bearer: string };\n\n/**\n * Type for the JSON response values from the remote server.\n */\n// oxlint-disable-next-line typescript/no-explicit-any\nexport type RemoteRetrieverValues = Record<string, any>;\n\n/**\n * Interface for the parameters required to initialize a RemoteRetriever\n * instance.\n */\nexport interface RemoteRetrieverParams\n  extends AsyncCallerParams, BaseRetrieverInput {\n  /**\n   * The URL of the remote retriever server\n   */\n  url: string;\n\n  /**\n   * The authentication method to use, currently implemented is\n   * - false: no authentication\n   * - { bearer: string }: Bearer token authentication\n   */\n  auth: RemoteRetrieverAuth;\n}\n\n/**\n * Abstract class for interacting with a remote server to retrieve\n * relevant documents based on a given query.\n */\nexport abstract class RemoteRetriever\n  extends BaseRetriever\n  implements RemoteRetrieverParams\n{\n  get lc_secrets(): { [key: string]: string } | undefined {\n    return {\n      \"auth.bearer\": \"REMOTE_RETRIEVER_AUTH_BEARER\",\n    };\n  }\n\n  url: string;\n\n  auth: RemoteRetrieverAuth;\n\n  headers: Record<string, string>;\n\n  asyncCaller: AsyncCaller;\n\n  constructor(fields: RemoteRetrieverParams) {\n    super(fields);\n    const { url, auth, ...rest } = fields;\n    this.url = url;\n    this.auth = auth;\n    this.headers = {\n      Accept: \"application/json\",\n      \"Content-Type\": \"application/json\",\n      ...(this.auth && this.auth.bearer\n        ? { Authorization: `Bearer ${this.auth.bearer}` }\n        : {}),\n    };\n    this.asyncCaller = new AsyncCaller(rest);\n  }\n\n  /**\n   * Abstract method that should be implemented by subclasses to create the\n   * JSON body of the request based on the given query.\n   * @param query The query based on which the JSON body of the request is created.\n   * @returns The JSON body of the request.\n   */\n  abstract createJsonBody(query: string): RemoteRetrieverValues;\n\n  /**\n   * Abstract method that should be implemented by subclasses to process the\n   * JSON response from the server and convert it into an array of Document\n   * instances.\n   * @param json The JSON response from the server.\n   * @returns An array of Document instances.\n   */\n  abstract processJsonResponse(\n    json: RemoteRetrieverValues\n  ): DocumentInterface[];\n\n  async _getRelevantDocuments(query: string): Promise<DocumentInterface[]> {\n    const body = this.createJsonBody(query);\n    const response = await this.asyncCaller.call(() =>\n      fetch(this.url, {\n        method: \"POST\",\n        headers: this.headers,\n        body: JSON.stringify(body),\n      })\n    );\n    if (!response.ok) {\n      throw new Error(\n        `Failed to retrieve documents from ${this.url}: ${response.status} ${response.statusText}`\n      );\n    }\n    const json = await response.json();\n    return this.processJsonResponse(json);\n  }\n}\n"],"mappings":";;;;;;;;AA6CA,IAAsB,kBAAtB,cACUA,2BAAAA,cAEV;CACE,IAAI,aAAoD;AACtD,SAAO,EACL,eAAe,gCAChB;;CAGH;CAEA;CAEA;CAEA;CAEA,YAAY,QAA+B;AACzC,QAAM,OAAO;EACb,MAAM,EAAE,KAAK,MAAM,GAAG,SAAS;AAC/B,OAAK,MAAM;AACX,OAAK,OAAO;AACZ,OAAK,UAAU;GACb,QAAQ;GACR,gBAAgB;GAChB,GAAI,KAAK,QAAQ,KAAK,KAAK,SACvB,EAAE,eAAe,UAAU,KAAK,KAAK,UAAU,GAC/C,EAAE;GACP;AACD,OAAK,cAAc,IAAIC,mCAAAA,YAAY,KAAK;;CAsB1C,MAAM,sBAAsB,OAA6C;EACvE,MAAM,OAAO,KAAK,eAAe,MAAM;EACvC,MAAM,WAAW,MAAM,KAAK,YAAY,WACtC,MAAM,KAAK,KAAK;GACd,QAAQ;GACR,SAAS,KAAK;GACd,MAAM,KAAK,UAAU,KAAK;GAC3B,CAAC,CACH;AACD,MAAI,CAAC,SAAS,GACZ,OAAM,IAAI,MACR,qCAAqC,KAAK,IAAI,IAAI,SAAS,OAAO,GAAG,SAAS,aAC/E;EAEH,MAAM,OAAO,MAAM,SAAS,MAAM;AAClC,SAAO,KAAK,oBAAoB,KAAK"}