continue-custom/core/indexing/FullTextSearch.ts
Nate Sesti 53b61288ec
🎨 Refactor core (#1281)
* 🎨 refactor out react context

* 🎨 refactor local storage

* 🎨 refactor IdeMessenger

* 🔥 removing unused files

* 🚚 move scripts

* ✅ setup VSCode integration testing

* 🎨 fix test-related stuff

* 🚧 testing

* 🎨 tweak terminal cmd+L

* 🧑‍💻 install biome in core

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 run biome check in binary

* 🧑‍💻 global biome config

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 fix a few biome warnings

* 🚧 WIP on protocols

* 🚧 progress, organizing, about to remove webviewCore protocol

* 🚧 now running

* 🚧 move indexing code to core

* 🚧 WIP on JetBrains indexing implementation

* 🎨 finish listDir

* 🚧 gui fixes

* 🏷️ update IMessenger.on return type

* 🐛 fix jetbrains types + IDE detection

* 🧑‍💻 set up debugging for binary with TcpMessenger

* 👷 fix prepackage.js

* 🧑‍💻 turn off debug mode for intellij

* 🐛 merge fixes

* 🐛 fixes after refactor

* 🐛 merge fixes

* 💄 increase font size in JB

* 🔀 merge changes

* 🩹 small fixes

* 🐛 fix jetbrains build

* 🩹 more jetbrains fixes

* 🎨 jetbrains improvements

* 🐛 fix lancedb prob in jetbrains build

* 👷 update jetbrains build process

* ✨ intellij problems context provider

* 👷 add script utils file

* 💚 fix jetbrains build regression

* 🎨 dynamic import transformers.js

* 🩹 small jetbrains updates

* ✨ folder context provider in jetbrains

* 🎨 many more jetbrains improvements

* 🔀 merge fixes

* ⚡️ small improvements

* 🎨 tell users transformers.js not supported

* 🎨 trial updates

* 📝 update jetbrains readmej

* ⚡️ only use smaller context for local autocomplete models

* ⚡️ improve bracket filter

* ✨ global .continue/.prompts folder

* 💄 improved model setup process

* 🥅 display VSCode req errs
2024-05-24 19:38:13 -07:00

151 lines
4.2 KiB
TypeScript

import {
BranchAndDir,
Chunk,
IndexTag,
IndexingProgressUpdate,
} from "../index.js";
import { RETRIEVAL_PARAMS } from "../util/parameters.js";
import { ChunkCodebaseIndex } from "./chunk/ChunkCodebaseIndex.js";
import { DatabaseConnection, SqliteDb, tagToString } from "./refreshIndex.js";
import {
IndexResultType,
MarkCompleteCallback,
RefreshIndexResults,
type CodebaseIndex,
} from "./types.js";
export class FullTextSearchCodebaseIndex implements CodebaseIndex {
artifactId = "sqliteFts";
private async _createTables(db: DatabaseConnection) {
await db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS fts USING fts5(
path,
content,
tokenize = 'trigram'
)`);
await db.exec(`CREATE TABLE IF NOT EXISTS fts_metadata (
id INTEGER PRIMARY KEY,
path TEXT NOT NULL,
cacheKey TEXT NOT NULL,
chunkId INTEGER NOT NULL,
FOREIGN KEY (chunkId) REFERENCES chunks (id),
FOREIGN KEY (id) REFERENCES fts (rowid)
)`);
}
async *update(
tag: IndexTag,
results: RefreshIndexResults,
markComplete: MarkCompleteCallback,
repoName: string | undefined,
): AsyncGenerator<IndexingProgressUpdate, any, unknown> {
const db = await SqliteDb.get();
await this._createTables(db);
for (let i = 0; i < results.compute.length; i++) {
const item = results.compute[i];
// Insert chunks
const chunks = await db.all(
"SELECT * FROM chunks WHERE path = ? AND cacheKey = ?",
[item.path, item.cacheKey],
);
for (const chunk of chunks) {
const { lastID } = await db.run(
"INSERT INTO fts (path, content) VALUES (?, ?)",
[item.path, chunk.content],
);
await db.run(
"INSERT INTO fts_metadata (id, path, cacheKey, chunkId) VALUES (?, ?, ?, ?)",
[lastID, item.path, item.cacheKey, chunk.id],
);
}
yield {
progress: i / results.compute.length,
desc: `Indexing ${item.path}`,
status: "indexing",
};
markComplete([item], IndexResultType.Compute);
}
// Add tag
for (const item of results.addTag) {
markComplete([item], IndexResultType.AddTag);
}
// Remove tag
for (const item of results.removeTag) {
markComplete([item], IndexResultType.RemoveTag);
}
// Delete
for (const item of results.del) {
const { lastID } = await db.run(
"DELETE FROM fts_metadata WHERE path = ? AND cacheKey = ?",
[item.path, item.cacheKey],
);
await db.run("DELETE FROM fts WHERE rowid = ?", [lastID]);
markComplete([item], IndexResultType.Delete);
}
}
async retrieve(
tags: BranchAndDir[],
text: string,
n: number,
directory: string | undefined,
filterPaths: string[] | undefined,
bm25Threshold: number = RETRIEVAL_PARAMS.bm25Threshold,
): Promise<Chunk[]> {
const db = await SqliteDb.get();
// Notice that the "chunks" artifactId is used because of linking between tables
const tagStrings = tags.map((tag) => {
return tagToString({ ...tag, artifactId: ChunkCodebaseIndex.artifactId });
});
const query = `SELECT fts_metadata.chunkId, fts_metadata.path, fts.content, rank
FROM fts
JOIN fts_metadata ON fts.rowid = fts_metadata.id
JOIN chunk_tags ON fts_metadata.chunkId = chunk_tags.chunkId
WHERE fts MATCH '${text.replace(
/\?/g,
"",
)}' AND chunk_tags.tag IN (${tagStrings.map(() => "?").join(",")})
${
filterPaths
? `AND fts_metadata.path IN (${filterPaths.map(() => "?").join(",")})`
: ""
}
ORDER BY rank
LIMIT ?`;
let results = await db.all(query, [
...tagStrings,
...(filterPaths || []),
Math.ceil(n),
]);
results = results.filter((result) => result.rank <= bm25Threshold);
const chunks = await db.all(
`SELECT * FROM chunks WHERE id IN (${results.map(() => "?").join(",")})`,
results.map((result) => result.chunkId),
);
return chunks.map((chunk) => {
return {
filepath: chunk.path,
index: chunk.index,
startLine: chunk.startLine,
endLine: chunk.endLine,
content: chunk.content,
digest: chunk.cacheKey,
};
});
}
}