diff --git a/core/indexing/LanceDbIndex.ts b/core/indexing/LanceDbIndex.ts index d7e9899ef..1f6ea709e 100644 --- a/core/indexing/LanceDbIndex.ts +++ b/core/indexing/LanceDbIndex.ts @@ -11,7 +11,7 @@ import { } from "../index.js"; import { MAX_CHUNK_SIZE } from "../llm/constants.js"; import { getBasename } from "../util/index.js"; -import { getLanceDbPath } from "../util/paths.js"; +import { getLanceDbPath, migrate } from "../util/paths.js"; import { chunkDocument } from "./chunk/chunk.js"; import { DatabaseConnection, SqliteDb, tagToString } from "./refreshIndex.js"; import { @@ -53,11 +53,25 @@ export class LanceDbIndex implements CodebaseIndex { uuid TEXT PRIMARY KEY, cacheKey TEXT NOT NULL, path TEXT NOT NULL, + artifact_id TEXT NOT NULL, vector TEXT NOT NULL, startLine INTEGER NOT NULL, endLine INTEGER NOT NULL, contents TEXT NOT NULL )`); + + await new Promise((resolve) => + migrate( + "lancedb_sqlite_artifact_id_column", + async () => { + await db.exec( + `ALTER TABLE lance_db_cache ADD COLUMN artifact_id TEXT NOT NULL DEFAULT 'UNDEFINED'`, + ); + resolve(undefined); + }, + () => resolve(undefined), + ), + ); } private async *computeChunks( @@ -231,10 +245,11 @@ export class LanceDbIndex implements CodebaseIndex { rows.push(row); await sqlite.run( - "INSERT INTO lance_db_cache (uuid, cacheKey, path, vector, startLine, endLine, contents) VALUES (?, ?, ?, ?, ?, ?, ?)", + "INSERT INTO lance_db_cache (uuid, cacheKey, path, artifact_id, vector, startLine, endLine, contents) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", row.uuid, row.cachekey, row.path, + this.artifactId, JSON.stringify(row.vector), chunk.startLine, chunk.endLine, @@ -265,10 +280,11 @@ export class LanceDbIndex implements CodebaseIndex { // Add the computed row to the cache await sqlite.run( - "INSERT INTO lance_db_cache (uuid, cacheKey, path, vector, startLine, endLine, contents) VALUES (?, ?, ?, ?, ?, ?, ?)", + "INSERT INTO lance_db_cache (uuid, cacheKey, path, artifact_id, vector, startLine, endLine, contents) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", row.uuid, row.cachekey, row.path, + this.artifactId, JSON.stringify(row.vector), data.startLine, data.endLine, @@ -290,9 +306,10 @@ export class LanceDbIndex implements CodebaseIndex { // Add tag - retrieve the computed info from lance sqlite cache for (const { path, cacheKey } of results.addTag) { const stmt = await sqlite.prepare( - "SELECT * FROM lance_db_cache WHERE cacheKey = ? AND path = ?", + "SELECT * FROM lance_db_cache WHERE cacheKey = ? AND path = ? AND artifact_id = ?", cacheKey, path, + this.artifactId, ); const cachedItems = await stmt.all(); @@ -347,9 +364,10 @@ export class LanceDbIndex implements CodebaseIndex { // Delete - also remove from sqlite cache for (const { path, cacheKey } of results.del) { await sqlite.run( - "DELETE FROM lance_db_cache WHERE cacheKey = ? AND path = ?", + "DELETE FROM lance_db_cache WHERE cacheKey = ? AND path = ? AND artifact_id = ?", cacheKey, path, + this.artifactId, ); accumulatedProgress += 1 / results.del.length / 3; yield { diff --git a/core/indexing/embeddings/BaseEmbeddingsProvider.ts b/core/indexing/embeddings/BaseEmbeddingsProvider.ts index 22de70fb8..cd67a001a 100644 --- a/core/indexing/embeddings/BaseEmbeddingsProvider.ts +++ b/core/indexing/embeddings/BaseEmbeddingsProvider.ts @@ -29,7 +29,7 @@ abstract class BaseEmbeddingsProvider implements IBaseEmbeddingsProvider { ...options, }; this.fetch = fetch; - this.id = this.options.model || this.constructor.name; + this.id = `${this.constructor.name}::${this.options.model}`; } abstract embed(chunks: string[]): Promise; diff --git a/core/indexing/embeddings/FreeTrialEmbeddingsProvider.ts b/core/indexing/embeddings/FreeTrialEmbeddingsProvider.ts index 8ba07a728..6d356cef9 100644 --- a/core/indexing/embeddings/FreeTrialEmbeddingsProvider.ts +++ b/core/indexing/embeddings/FreeTrialEmbeddingsProvider.ts @@ -15,7 +15,7 @@ class FreeTrialEmbeddingsProvider extends BaseEmbeddingsProvider { constructor(options: EmbedOptions, fetch: FetchFunction) { super(options, fetch); this.options.model = FreeTrialEmbeddingsProvider.defaultOptions?.model; - this.id = this.options.model || this.constructor.name; + this.id = `${this.constructor.name}::${this.options.model}`; } async embed(chunks: string[]) { diff --git a/core/util/paths.ts b/core/util/paths.ts index 1cb76c47a..f7c7fc1d3 100644 --- a/core/util/paths.ts +++ b/core/util/paths.ts @@ -177,12 +177,22 @@ function getMigrationsFolderPath(): string { return migrationsPath; } -export function migrate(id: string, callback: () => void) { +export function migrate( + id: string, + callback: () => void, + onAlreadyComplete?: () => void, +) { const migrationsPath = getMigrationsFolderPath(); const migrationPath = path.join(migrationsPath, id); if (!fs.existsSync(migrationPath)) { - fs.writeFileSync(migrationPath, ""); - callback(); + try { + callback(); + fs.writeFileSync(migrationPath, ""); + } catch (e) { + console.error(`Migration ${id} failed`, e); + } + } else if (onAlreadyComplete) { + onAlreadyComplete(); } }