✨ shared indexing

This commit is contained in:
Nate Sesti 2024-03-19 23:07:28 -07:00
parent 224089b74a
commit e6ddab32a2
4 changed files with 160 additions and 38 deletions

View file

@ -0,0 +1,27 @@
export interface EmbeddingsCacheChunk {
vector: number[];
startLine: number;
endLine: number;
contents: string;
}
export interface EmbeddingsCacheResponse {
files: { [cacheKey: string]: EmbeddingsCacheChunk[] };
}
export class ContinueServerClient {
constructor(
private readonly serverUrl: string,
private readonly userToken: Promise<string | undefined>,
) {}
public async getConfig(): Promise<{ configJson: string; configJs: string }> {
throw new Error("Not Implemented");
}
public async getFromIndexCache(
keys: string[],
artifactId: string,
): Promise<EmbeddingsCacheResponse> {
return { files: {} };
}
}

View file

@ -1,11 +1,13 @@
// NOTE: vectordb requirement must be listed in extensions/vscode to avoid error
import { v4 as uuidv4 } from "uuid";
import { Table } from "vectordb";
import {
Chunk,
EmbeddingsProvider,
IndexTag,
IndexingProgressUpdate,
} from "..";
import { ContinueServerClient } from "../continueServer/client";
import { MAX_CHUNK_SIZE } from "../llm/constants";
import { getBasename } from "../util";
import { getLanceDbPath } from "../util/paths";
@ -34,16 +36,11 @@ export class LanceDbIndex implements CodebaseIndex {
static MAX_CHUNK_SIZE = MAX_CHUNK_SIZE;
embeddingsProvider: EmbeddingsProvider;
readFile: (filepath: string) => Promise<string>;
constructor(
embeddingsProvider: EmbeddingsProvider,
readFile: (filepath: string) => Promise<string>,
) {
this.embeddingsProvider = embeddingsProvider;
this.readFile = readFile;
}
private readonly embeddingsProvider: EmbeddingsProvider,
private readonly readFile: (filepath: string) => Promise<string>,
private readonly continueServerClient?: ContinueServerClient,
) {}
private tableNameForTag(tag: IndexTag) {
return tagToString(tag)
@ -145,11 +142,91 @@ export class LanceDbIndex implements CodebaseIndex {
await this.createSqliteCacheTable(sqlite);
// Compute
let table = undefined;
let table: Table<number[]> | undefined = undefined;
let needToCreateTable = true;
const existingTables = await db.tableNames();
let computedRows: LanceDbRow[] = [];
const addComputedLanceDbRows = async (
pathAndCacheKey: PathAndCacheKey,
computedRows: LanceDbRow[],
) => {
// Create table if needed, add computed rows
if (table) {
if (computedRows.length > 0) {
await table.add(computedRows);
}
} else if (existingTables.includes(tableName)) {
table = await db.openTable(tableName);
needToCreateTable = false;
if (computedRows.length > 0) {
await table.add(computedRows);
}
} else if (computedRows.length > 0) {
table = await db.createTable(tableName, computedRows);
needToCreateTable = false;
}
// Mark item complete
markComplete([pathAndCacheKey], IndexResultType.Compute);
};
// Check remote cache
if (this.continueServerClient !== undefined) {
try {
const keys = results.compute.map(({ cacheKey }) => cacheKey);
const resp = await this.continueServerClient.getFromIndexCache(
keys,
"chunks",
);
for (const [cacheKey, chunks] of Object.entries(resp.files)) {
// Get path for cacheKey
const path = results.compute.find(
(item) => item.cacheKey === cacheKey,
)?.path;
if (!path) {
console.warn(
"Continue server sent a cacheKey that wasn't requested",
cacheKey,
);
continue;
}
// Build LanceDbRow objects
const rows: LanceDbRow[] = [];
for (const chunk of chunks) {
const row = {
path,
cachekey: cacheKey,
uuid: uuidv4(),
vector: chunk.vector,
};
rows.push(row);
await sqlite.run(
"INSERT INTO lance_db_cache (uuid, cacheKey, path, vector, startLine, endLine, contents) VALUES (?, ?, ?, ?, ?, ?, ?)",
row.uuid,
row.cachekey,
row.path,
JSON.stringify(row.vector),
chunk.startLine,
chunk.endLine,
chunk.contents,
);
}
await addComputedLanceDbRows({ cacheKey, path }, rows);
}
// Remove items that don't need to be recomputed
results.compute = results.compute.filter(
(item) => !resp.files[item.cacheKey],
);
} catch (e) {
console.log("Error checking remote cache: ", e);
}
}
let computedRows: LanceDbRow[] = [];
for await (const update of this.computeChunks(results.compute)) {
if (Array.isArray(update)) {
const [progress, row, data, desc] = update;
@ -169,26 +246,8 @@ export class LanceDbIndex implements CodebaseIndex {
yield { progress, desc };
} else {
// Create table if needed, add computed rows
if (table) {
if (computedRows.length > 0) {
await table.add(computedRows);
}
} else if (existingTables.includes(tableName)) {
table = await db.openTable(tableName);
needToCreateTable = false;
if (computedRows.length > 0) {
await table.add(computedRows);
}
} else if (computedRows.length > 0) {
table = await db.createTable(tableName, computedRows);
needToCreateTable = false;
}
await addComputedLanceDbRows(update, computedRows);
computedRows = [];
// Mark item complete
markComplete([update], IndexResultType.Compute);
}
}

View file

@ -1,5 +1,6 @@
import { IDE, IndexTag, IndexingProgressUpdate } from "..";
import { ConfigHandler } from "../config/handler";
import { ContinueServerClient } from "../continueServer/client";
import { CodeSnippetsCodebaseIndex } from "./CodeSnippetsIndex";
import { FullTextSearchCodebaseIndex } from "./FullTextSearch";
import { LanceDbIndex } from "./LanceDbIndex";
@ -20,14 +21,20 @@ export class PauseToken {
}
export class CodebaseIndexer {
configHandler: ConfigHandler;
ide: IDE;
pauseToken: PauseToken;
constructor(configHandler: ConfigHandler, ide: IDE, pauseToken: PauseToken) {
this.configHandler = configHandler;
this.ide = ide;
this.pauseToken = pauseToken;
private continueServerClient?: ContinueServerClient;
constructor(
private readonly configHandler: ConfigHandler,
private readonly ide: IDE,
private readonly pauseToken: PauseToken,
private readonly continueServerUrl: string | undefined,
private readonly userToken: Promise<string | undefined>,
) {
if (continueServerUrl) {
this.continueServerClient = new ContinueServerClient(
continueServerUrl,
userToken,
);
}
}
private async getIndexesToBuild(): Promise<CodebaseIndex[]> {
@ -38,6 +45,7 @@ export class CodebaseIndexer {
new LanceDbIndex(
config.embeddingsProvider,
this.ide.readFile.bind(this.ide),
this.continueServerClient,
),
new FullTextSearchCodebaseIndex(),
new CodeSnippetsCodebaseIndex(this.ide),

View file

@ -13,6 +13,7 @@ import { VerticalPerLineDiffManager } from "../diff/verticalPerLine/manager";
import { VsCodeIde } from "../ideProtocol";
import { registerAllCodeLensProviders } from "../lang-server/codeLens";
import { setupRemoteConfigSync } from "../stubs/activation";
import { getUserToken } from "../stubs/auth";
import { TabAutocompleteModel } from "../util/loadAutocompleteModel";
import { VsCodeWebviewProtocol } from "../webviewProtocol";
@ -106,12 +107,39 @@ export class VsCodeExtension {
});
this.diffManager.webviewProtocol = this.webviewProtocol;
const userTokenPromise: Promise<string | undefined> = new Promise(
async (resolve) => {
if (
remoteConfigServerUrl === null ||
remoteConfigServerUrl === undefined ||
remoteConfigServerUrl.trim() === ""
) {
resolve(undefined);
return;
}
const token = await getUserToken();
resolve(token);
},
);
this.indexer = new CodebaseIndexer(
this.configHandler,
this.ide,
indexingPauseToken,
ideSettings.remoteConfigServerUrl,
userTokenPromise,
);
if (
!(
remoteConfigServerUrl === null ||
remoteConfigServerUrl === undefined ||
remoteConfigServerUrl.trim() === ""
)
) {
getUserToken().then((token) => {});
}
// CodeLens
registerAllCodeLensProviders(
context,