* Initialize the storage provider at the given path. * @param storagePath - Directory path for LanceDB storage * @param options - Optional configuration * @param options.expectExisting - If true, throws IndexCorruptedError if table doesn't exist * @param options.expectedDimensions - If s
(
storagePath: string,
options?: { expectExisting?: boolean; expectedDimensions?: number }
)
| 44 | * @param options.expectedDimensions - If set, verifies the stored vector column has this many dimensions |
| 45 | */ |
| 46 | async initialize( |
| 47 | storagePath: string, |
| 48 | options?: { expectExisting?: boolean; expectedDimensions?: number } |
| 49 | ): Promise<void> { |
| 50 | if (this.initialized) return; |
| 51 | |
| 52 | try { |
| 53 | this.storagePath = storagePath; |
| 54 | await fs.mkdir(storagePath, { recursive: true }); |
| 55 | |
| 56 | const lancedb = await import('@lancedb/lancedb'); |
| 57 | this.db = await lancedb.connect(storagePath); |
| 58 | |
| 59 | // Check if table exists and validate schema |
| 60 | const tableNames = await this.db.tableNames(); |
| 61 | if (tableNames.includes('code_chunks')) { |
| 62 | this.table = await this.db.openTable('code_chunks'); |
| 63 | |
| 64 | const schema = await this.table.schema(); |
| 65 | const hasVectorColumn = schema.fields.some((f: { name: string }) => f.name === 'vector'); |
| 66 | |
| 67 | if (!hasVectorColumn) { |
| 68 | throw new IndexCorruptedError('LanceDB index corrupted: missing vector column'); |
| 69 | } |
| 70 | |
| 71 | // Check vector dimensions if caller specifies expected dims (e.g. switching providers) |
| 72 | if (options?.expectedDimensions !== undefined) { |
| 73 | const vectorField = schema.fields.find((f: { name: string }) => f.name === 'vector'); |
| 74 | const storedDims = (vectorField?.type as { listSize?: number } | undefined)?.listSize; |
| 75 | if (storedDims !== undefined && storedDims !== options.expectedDimensions) { |
| 76 | throw new IndexCorruptedError( |
| 77 | `LanceDB dimension mismatch: stored=${storedDims}, expected=${options.expectedDimensions} (rebuild required)` |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (process.env.CODEBASE_CONTEXT_DEBUG) console.error('Opened existing LanceDB table'); |
| 83 | } else if (options?.expectExisting) { |
| 84 | throw new IndexCorruptedError( |
| 85 | `LanceDB index missing: no code_chunks table found at ${storagePath}` |
| 86 | ); |
| 87 | } else { |
| 88 | this.table = null; |
| 89 | } |
| 90 | |
| 91 | this.initialized = true; |
| 92 | console.error(`LanceDB initialized at: ${storagePath}`); |
| 93 | } catch (error) { |
| 94 | if (error instanceof IndexCorruptedError) { |
| 95 | throw error; |
| 96 | } |
| 97 | console.error('Failed to initialize LanceDB:', error); |
| 98 | // Wrap connection/open failures as corruption errors for fail-closed behavior |
| 99 | throw new IndexCorruptedError( |
| 100 | `LanceDB initialization failed: ${error instanceof Error ? error.message : String(error)}` |
| 101 | ); |
| 102 | } |
| 103 | } |
no outgoing calls
no test coverage detected