diff --git a/core/context/retrieval/pipelines/BaseRetrievalPipeline.ts b/core/context/retrieval/pipelines/BaseRetrievalPipeline.ts index 80328decb..f5d8a89f6 100644 --- a/core/context/retrieval/pipelines/BaseRetrievalPipeline.ts +++ b/core/context/retrieval/pipelines/BaseRetrievalPipeline.ts @@ -60,12 +60,13 @@ export interface IRetrievalPipeline { export default class BaseRetrievalPipeline implements IRetrievalPipeline { private ftsIndex = new FullTextSearchCodebaseIndex(); private lanceDbIndex: LanceDbIndex | null = null; + private lanceDbInitPromise: Promise | null = null; constructor(protected readonly options: RetrievalPipelineOptions) { void this.initLanceDb(); } - private async initLanceDb() { + protected async initLanceDb() { const embedModel = this.options.config.selectedModelByRole.embed; if (!embedModel) { @@ -77,6 +78,23 @@ export default class BaseRetrievalPipeline implements IRetrievalPipeline { ); } + protected async ensureLanceDbInitialized(): Promise { + if (this.lanceDbIndex) { + return true; + } + + if (this.lanceDbInitPromise) { + await this.lanceDbInitPromise; + return this.lanceDbIndex !== null; + } + + this.lanceDbInitPromise = this.initLanceDb(); + await this.lanceDbInitPromise; + this.lanceDbInitPromise = null; // clear after init + + return this.lanceDbIndex !== null; + } + private getCleanedTrigrams( query: RetrievalPipelineRunArguments["query"], ): string[] { @@ -163,7 +181,9 @@ export default class BaseRetrievalPipeline implements IRetrievalPipeline { input: string, n: number, ): Promise { - if (!this.lanceDbIndex) { + const initialized = await this.ensureLanceDbInitialized(); + + if (!initialized || !this.lanceDbIndex) { console.warn( "LanceDB index not available, skipping embeddings retrieval", );