add necessary filter to lance_db_cache

This commit is contained in:
Nate Sesti 2024-07-05 23:18:11 -07:00
parent 4c5210622b
commit e5393c76d0
4 changed files with 38 additions and 10 deletions

View file

@ -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 {

View file

@ -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<number[][]>;

View file

@ -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[]) {

View file

@ -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();
}
}