{"version":3,"file":"chaindesk.cjs","names":["BaseRetriever","AsyncCaller","query: string","Document"],"sources":["../../src/retrievers/chaindesk.ts"],"sourcesContent":["import {\n  BaseRetriever,\n  type BaseRetrieverInput,\n} from \"@langchain/core/retrievers\";\nimport { Document } from \"@langchain/core/documents\";\nimport {\n  AsyncCaller,\n  type AsyncCallerParams,\n} from \"@langchain/core/utils/async_caller\";\n\nexport interface ChaindeskRetrieverArgs\n  extends AsyncCallerParams,\n    BaseRetrieverInput {\n  datastoreId: string;\n  topK?: number;\n  filter?: Record<string, unknown>;\n  apiKey?: string;\n}\n\ninterface Berry {\n  text: string;\n  score: number;\n  source?: string;\n  [key: string]: unknown;\n}\n\n/**\n * @example\n * ```typescript\n * const retriever = new ChaindeskRetriever({\n *   datastoreId: \"DATASTORE_ID\",\n *   apiKey: \"CHAINDESK_API_KEY\",\n *   topK: 8,\n * });\n * const docs = await retriever.getRelevantDocuments(\"hello\");\n * ```\n */\nexport class ChaindeskRetriever extends BaseRetriever {\n  static lc_name() {\n    return \"ChaindeskRetriever\";\n  }\n\n  lc_namespace = [\"langchain\", \"retrievers\", \"chaindesk\"];\n\n  caller: AsyncCaller;\n\n  datastoreId: string;\n\n  topK?: number;\n\n  filter?: Record<string, unknown>;\n\n  apiKey?: string;\n\n  constructor({\n    datastoreId,\n    apiKey,\n    topK,\n    filter,\n    ...rest\n  }: ChaindeskRetrieverArgs) {\n    super();\n\n    this.caller = new AsyncCaller(rest);\n    this.datastoreId = datastoreId;\n    this.apiKey = apiKey;\n    this.topK = topK;\n    this.filter = filter;\n  }\n\n  async getRelevantDocuments(query: string): Promise<Document[]> {\n    const r = await this.caller.call(\n      fetch,\n      `https://app.chaindesk.ai/api/datastores/${this.datastoreId}/query`,\n      {\n        method: \"POST\",\n        body: JSON.stringify({\n          query,\n          ...(this.topK ? { topK: this.topK } : {}),\n          ...(this.filter ? { filters: this.filter } : {}),\n        }),\n        headers: {\n          \"Content-Type\": \"application/json\",\n          ...(this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {}),\n        },\n      }\n    );\n\n    const { results } = (await r.json()) as { results: Berry[] };\n\n    return results.map(\n      ({ text, score, source, ...rest }) =>\n        new Document({\n          pageContent: text,\n          metadata: {\n            score,\n            source,\n            ...rest,\n          },\n        })\n    );\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAqCA,IAAa,qBAAb,cAAwCA,0CAAc;CACpD,OAAO,UAAU;AACf,SAAO;CACR;CAED,eAAe;EAAC;EAAa;EAAc;CAAY;CAEvD;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,EACV,aACA,QACA,MACA,OACA,GAAG,MACoB,EAAE;EACzB,OAAO;EAEP,KAAK,SAAS,IAAIC,gDAAY;EAC9B,KAAK,cAAc;EACnB,KAAK,SAAS;EACd,KAAK,OAAO;EACZ,KAAK,SAAS;CACf;CAED,MAAM,qBAAqBC,OAAoC;EAC7D,MAAM,IAAI,MAAM,KAAK,OAAO,KAC1B,OACA,CAAC,wCAAwC,EAAE,KAAK,YAAY,MAAM,CAAC,EACnE;GACE,QAAQ;GACR,MAAM,KAAK,UAAU;IACnB;IACA,GAAI,KAAK,OAAO,EAAE,MAAM,KAAK,KAAM,IAAG,CAAE;IACxC,GAAI,KAAK,SAAS,EAAE,SAAS,KAAK,OAAQ,IAAG,CAAE;GAChD,EAAC;GACF,SAAS;IACP,gBAAgB;IAChB,GAAI,KAAK,SAAS,EAAE,eAAe,CAAC,OAAO,EAAE,KAAK,QAAQ,CAAE,IAAG,CAAE;GAClE;EACF,EACF;EAED,MAAM,EAAE,SAAS,GAAI,MAAM,EAAE,MAAM;AAEnC,SAAO,QAAQ,IACb,CAAC,EAAE,MAAM,OAAO,OAAQ,GAAG,MAAM,KAC/B,IAAIC,oCAAS;GACX,aAAa;GACb,UAAU;IACR;IACA;IACA,GAAG;GACJ;EACF,GACJ;CACF;AACF"}