{"version":3,"file":"index.cjs","names":["Runnable","ensureConfig","parseCallbackConfigArg","CallbackManager"],"sources":["../../src/retrievers/index.ts"],"sourcesContent":["import {\n  CallbackManager,\n  CallbackManagerForRetrieverRun,\n  Callbacks,\n  parseCallbackConfigArg,\n} from \"../callbacks/manager.js\";\nimport type { DocumentInterface } from \"../documents/document.js\";\nimport { Runnable, type RunnableInterface } from \"../runnables/base.js\";\nimport { RunnableConfig, ensureConfig } from \"../runnables/config.js\";\n\n/**\n * Input configuration options for initializing a retriever that extends\n * the `BaseRetriever` class. This interface provides base properties\n * common to all retrievers, allowing customization of callback functions,\n * tagging, metadata, and logging verbosity.\n *\n * Fields:\n * - `callbacks` (optional): An array of callback functions that handle various\n *   events during retrieval, such as logging, error handling, or progress updates.\n *\n * - `tags` (optional): An array of strings used to add contextual tags to\n *   retrieval operations, allowing for easier categorization and tracking.\n *\n * - `metadata` (optional): A record of key-value pairs to store additional\n *   contextual information for retrieval operations, which can be useful\n *   for logging or auditing purposes.\n *\n * - `verbose` (optional): A boolean flag that, if set to `true`, enables\n *   detailed logging and output during the retrieval process. Defaults to `false`.\n */\nexport interface BaseRetrieverInput {\n  callbacks?: Callbacks;\n  tags?: string[];\n  metadata?: Record<string, unknown>;\n  verbose?: boolean;\n}\n\n/**\n * Interface for a base retriever that defines core functionality for\n * retrieving relevant documents from a source based on a query.\n *\n * The `BaseRetrieverInterface` standardizes the `getRelevantDocuments` method,\n * enabling retrieval of documents that match the query criteria.\n *\n * @template Metadata - The type of metadata associated with each document,\n *                      defaulting to `Record<string, any>`.\n */\nexport interface BaseRetrieverInterface<\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  Metadata extends Record<string, any> = Record<string, any>,\n> extends RunnableInterface<string, DocumentInterface<Metadata>[]> {}\n\n/**\n * Abstract base class for a document retrieval system, designed to\n * process string queries and return the most relevant documents from a source.\n *\n * `BaseRetriever` provides common properties and methods for derived retrievers,\n * such as callbacks, tagging, and verbose logging. Custom retrieval systems\n * should extend this class and implement `_getRelevantDocuments` to define\n * the specific retrieval logic.\n *\n * @template Metadata - The type of metadata associated with each document,\n *                      defaulting to `Record<string, any>`.\n */\nexport abstract class BaseRetriever<\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  Metadata extends Record<string, any> = Record<string, any>,\n>\n  extends Runnable<string, DocumentInterface<Metadata>[]>\n  implements BaseRetrieverInterface\n{\n  /**\n   * Optional callbacks to handle various events in the retrieval process.\n   */\n  callbacks?: Callbacks;\n\n  /**\n   * Tags to label or categorize the retrieval operation.\n   */\n  tags?: string[];\n\n  /**\n   * Metadata to provide additional context or information about the retrieval\n   * operation.\n   */\n  metadata?: Record<string, unknown>;\n\n  /**\n   * If set to `true`, enables verbose logging for the retrieval process.\n   */\n  verbose?: boolean;\n\n  /**\n   * Constructs a new `BaseRetriever` instance with optional configuration fields.\n   *\n   * @param fields - Optional input configuration that can include `callbacks`,\n   *                 `tags`, `metadata`, and `verbose` settings for custom retriever behavior.\n   */\n  constructor(fields?: BaseRetrieverInput) {\n    super(fields);\n    this.callbacks = fields?.callbacks;\n    this.tags = fields?.tags ?? [];\n    this.metadata = fields?.metadata ?? {};\n    this.verbose = fields?.verbose ?? false;\n  }\n\n  /**\n   * TODO: This should be an abstract method, but we'd like to avoid breaking\n   * changes to people currently using subclassed custom retrievers.\n   * Change it on next major release.\n   */\n  /**\n   * Placeholder method for retrieving relevant documents based on a query.\n   *\n   * This method is intended to be implemented by subclasses and will be\n   * converted to an abstract method in the next major release. Currently, it\n   * throws an error if not implemented, ensuring that custom retrievers define\n   * the specific retrieval logic.\n   *\n   * @param _query - The query string used to search for relevant documents.\n   * @param _callbacks - (optional) Callback manager for managing callbacks\n   *                     during retrieval.\n   * @returns A promise resolving to an array of `DocumentInterface` instances relevant to the query.\n   * @throws {Error} Throws an error indicating the method is not implemented.\n   */\n  _getRelevantDocuments(\n    _query: string,\n    _callbacks?: CallbackManagerForRetrieverRun\n  ): Promise<DocumentInterface<Metadata>[]> {\n    throw new Error(\"Not implemented!\");\n  }\n\n  /**\n   * Executes a retrieval operation.\n   *\n   * @param input - The query string used to search for relevant documents.\n   * @param options - (optional) Configuration options for the retrieval run,\n   *                  which may include callbacks, tags, and metadata.\n   * @returns A promise that resolves to an array of `DocumentInterface` instances\n   *          representing the most relevant documents to the query.\n   */\n  async invoke(\n    input: string,\n    options?: RunnableConfig\n  ): Promise<DocumentInterface<Metadata>[]> {\n    const parsedConfig = ensureConfig(parseCallbackConfigArg(options));\n    const callbackManager_ = await CallbackManager.configure(\n      parsedConfig.callbacks,\n      this.callbacks,\n      parsedConfig.tags,\n      this.tags,\n      parsedConfig.metadata,\n      this.metadata,\n      { verbose: this.verbose }\n    );\n    const runManager = await callbackManager_?.handleRetrieverStart(\n      this.toJSON(),\n      input,\n      parsedConfig.runId,\n      undefined,\n      undefined,\n      undefined,\n      parsedConfig.runName\n    );\n    try {\n      const results = await this._getRelevantDocuments(input, runManager);\n      await runManager?.handleRetrieverEnd(results);\n      return results;\n    } catch (error) {\n      await runManager?.handleRetrieverError(error);\n      throw error;\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAgEA,IAAsB,gBAAtB,cAIUA,aAAAA,SAEV;;;;CAIE;;;;CAKA;;;;;CAMA;;;;CAKA;;;;;;;CAQA,YAAY,QAA6B;AACvC,QAAM,OAAO;AACb,OAAK,YAAY,QAAQ;AACzB,OAAK,OAAO,QAAQ,QAAQ,EAAE;AAC9B,OAAK,WAAW,QAAQ,YAAY,EAAE;AACtC,OAAK,UAAU,QAAQ,WAAW;;;;;;;;;;;;;;;;;;;;;CAsBpC,sBACE,QACA,YACwC;AACxC,QAAM,IAAI,MAAM,mBAAmB;;;;;;;;;;;CAYrC,MAAM,OACJ,OACA,SACwC;EACxC,MAAM,eAAeC,eAAAA,aAAaC,0BAAAA,uBAAuB,QAAQ,CAAC;EAUlE,MAAM,aAAa,OATM,MAAMC,0BAAAA,gBAAgB,UAC7C,aAAa,WACb,KAAK,WACL,aAAa,MACb,KAAK,MACL,aAAa,UACb,KAAK,UACL,EAAE,SAAS,KAAK,SAAS,CAC1B,GAC0C,qBACzC,KAAK,QAAQ,EACb,OACA,aAAa,OACb,KAAA,GACA,KAAA,GACA,KAAA,GACA,aAAa,QACd;AACD,MAAI;GACF,MAAM,UAAU,MAAM,KAAK,sBAAsB,OAAO,WAAW;AACnE,SAAM,YAAY,mBAAmB,QAAQ;AAC7C,UAAO;WACA,OAAO;AACd,SAAM,YAAY,qBAAqB,MAAM;AAC7C,SAAM"}