From 90e9933c9757eae0d6dd71fce431cb451af21502 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Wed, 27 Mar 2024 20:32:52 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20add=20getRepoName=20to=20IDE,=20?= =?UTF-8?q?use=20for=20indexing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/continueServer/stubs/client.ts | 1 + core/index.d.ts | 1 + core/indexing/CodeSnippetsIndex.ts | 1 + core/indexing/FullTextSearch.ts | 1 + core/indexing/LanceDbIndex.ts | 2 ++ core/indexing/chunk/ChunkCodebaseIndex.ts | 2 ++ core/indexing/indexCodebase.ts | 4 +++ core/indexing/refreshIndex.ts | 9 ++++- core/indexing/types.ts | 1 + core/util/filesystem.ts | 42 +++++++++++++++++++--- core/util/messageIde.ts | 29 ++++++++++++--- extensions/vscode/src/ideProtocol.ts | 44 +++++++++++++++-------- 12 files changed, 112 insertions(+), 25 deletions(-) diff --git a/core/continueServer/stubs/client.ts b/core/continueServer/stubs/client.ts index ea0871cb6..fa7ae0359 100644 --- a/core/continueServer/stubs/client.ts +++ b/core/continueServer/stubs/client.ts @@ -13,6 +13,7 @@ export class ContinueServerClient { public async getFromIndexCache( keys: string[], artifactId: T, + repoName: string | undefined, ): Promise> { return { files: {} }; } diff --git a/core/index.d.ts b/core/index.d.ts index b5b157aaa..03f58f4bd 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -399,6 +399,7 @@ export interface IDE { getBranch(dir: string): Promise; getStats(directory: string): Promise<{ [path: string]: number }>; getTags(artifactId: string): Promise; + getRepoName(dir: string): Promise; } // Slash Commands diff --git a/core/indexing/CodeSnippetsIndex.ts b/core/indexing/CodeSnippetsIndex.ts index f8f92443f..884411491 100644 --- a/core/indexing/CodeSnippetsIndex.ts +++ b/core/indexing/CodeSnippetsIndex.ts @@ -96,6 +96,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { tag: IndexTag, results: RefreshIndexResults, markComplete: MarkCompleteCallback, + repoName: string | undefined, ): AsyncGenerator { const db = await SqliteDb.get(); await CodeSnippetsCodebaseIndex._createTables(db); diff --git a/core/indexing/FullTextSearch.ts b/core/indexing/FullTextSearch.ts index 883b73a51..465587ad1 100644 --- a/core/indexing/FullTextSearch.ts +++ b/core/indexing/FullTextSearch.ts @@ -31,6 +31,7 @@ export class FullTextSearchCodebaseIndex implements CodebaseIndex { tag: IndexTag, results: RefreshIndexResults, markComplete: MarkCompleteCallback, + repoName: string | undefined, ): AsyncGenerator { const db = await SqliteDb.get(); await this._createTables(db); diff --git a/core/indexing/LanceDbIndex.ts b/core/indexing/LanceDbIndex.ts index e57db4e94..40db28ec2 100644 --- a/core/indexing/LanceDbIndex.ts +++ b/core/indexing/LanceDbIndex.ts @@ -133,6 +133,7 @@ export class LanceDbIndex implements CodebaseIndex { items: PathAndCacheKey[], resultType: IndexResultType, ) => void, + repoName: string | undefined, ): AsyncGenerator { const lancedb = await import("vectordb"); const tableName = this.tableNameForTag(tag); @@ -177,6 +178,7 @@ export class LanceDbIndex implements CodebaseIndex { const resp = await this.continueServerClient.getFromIndexCache( keys, "embeddings", + repoName, ); for (const [cacheKey, chunks] of Object.entries(resp.files)) { // Get path for cacheKey diff --git a/core/indexing/chunk/ChunkCodebaseIndex.ts b/core/indexing/chunk/ChunkCodebaseIndex.ts index 3f2a592bb..4601ef5c7 100644 --- a/core/indexing/chunk/ChunkCodebaseIndex.ts +++ b/core/indexing/chunk/ChunkCodebaseIndex.ts @@ -45,6 +45,7 @@ export class ChunkCodebaseIndex implements CodebaseIndex { tag: IndexTag, results: RefreshIndexResults, markComplete: MarkCompleteCallback, + repoName: string | undefined, ): AsyncGenerator { const db = await SqliteDb.get(); await this._createTables(db); @@ -76,6 +77,7 @@ export class ChunkCodebaseIndex implements CodebaseIndex { const resp = await this.continueServerClient.getFromIndexCache( keys, "chunks", + repoName, ); for (const [cacheKey, chunks] of Object.entries(resp.files)) { diff --git a/core/indexing/indexCodebase.ts b/core/indexing/indexCodebase.ts index 582db907d..fca83486f 100644 --- a/core/indexing/indexCodebase.ts +++ b/core/indexing/indexCodebase.ts @@ -77,10 +77,12 @@ export class CodebaseIndexer { for (let directory of workspaceDirs) { const stats = await this.ide.getStats(directory); const branch = await this.ide.getBranch(directory); + const repoName = await this.ide.getRepoName(directory); let completedIndexes = 0; try { for (let codebaseIndex of indexesToBuild) { + // TODO: IndexTag type should use repoName rather than directory const tag: IndexTag = { directory, branch, @@ -90,12 +92,14 @@ export class CodebaseIndexer { tag, { ...stats }, (filepath) => this.ide.readFile(filepath), + repoName, ); for await (let { progress, desc } of codebaseIndex.update( tag, results, markComplete, + repoName, )) { // Handle pausing in this loop because it's the only one really taking time while (this.pauseToken.paused) { diff --git a/core/indexing/refreshIndex.ts b/core/indexing/refreshIndex.ts index 6f6ec1406..a52b07c64 100644 --- a/core/indexing/refreshIndex.ts +++ b/core/indexing/refreshIndex.ts @@ -259,6 +259,7 @@ export async function getComputeDeleteAddRemove( tag: IndexTag, currentFiles: LastModifiedMap, readFile: (path: string) => Promise, + repoName: string | undefined, ): Promise<[RefreshIndexResults, MarkCompleteCallback]> { const [add, remove, markComplete] = await getAddRemoveForTag( tag, @@ -316,7 +317,12 @@ export async function getComputeDeleteAddRemove( removeTag: [], }; results[resultType] = items; - for await (let _ of globalCacheIndex.update(tag, results, () => {})) { + for await (let _ of globalCacheIndex.update( + tag, + results, + () => {}, + repoName, + )) { } }, ]; @@ -338,6 +344,7 @@ export class GlobalCacheCodeBaseIndex implements CodebaseIndex { tag: IndexTag, results: RefreshIndexResults, _: MarkCompleteCallback, + repoName: string | undefined, ): AsyncGenerator { const add = [...results.compute, ...results.addTag]; const remove = [...results.del, ...results.removeTag]; diff --git a/core/indexing/types.ts b/core/indexing/types.ts index 64df4a618..6715413ad 100644 --- a/core/indexing/types.ts +++ b/core/indexing/types.ts @@ -18,6 +18,7 @@ export interface CodebaseIndex { tag: IndexTag, results: RefreshIndexResults, markComplete: MarkCompleteCallback, + repoName: string | undefined, ): AsyncGenerator; } diff --git a/core/util/filesystem.ts b/core/util/filesystem.ts index 3753c35d9..eb85e5529 100644 --- a/core/util/filesystem.ts +++ b/core/util/filesystem.ts @@ -1,12 +1,25 @@ import * as fs from "fs"; -import { ContinueRcJson, IDE, IdeInfo, IndexTag, Problem, Range, Thread } from ".."; +import { + ContinueRcJson, + IDE, + IdeInfo, + IndexTag, + Problem, + Range, + Thread, +} from ".."; import { getContinueGlobalPath } from "./paths"; class FileSystemIde implements IDE { + getRepoName(dir: string): Promise { + return Promise.resolve(undefined); + } + getTags(artifactId: string): Promise { return Promise.resolve([]); } + getIdeInfo(): Promise { return Promise.resolve({ ideType: "vscode", @@ -15,18 +28,23 @@ class FileSystemIde implements IDE { remoteName: "na", }); } + readRangeInFile(filepath: string, range: Range): Promise { return Promise.resolve(""); } + getStats(directory: string): Promise<{ [path: string]: number }> { return Promise.resolve({}); } + isTelemetryEnabled(): Promise { return Promise.resolve(false); } + getUniqueId(): Promise { return Promise.resolve("NOT_UNIQUE"); } + getWorkspaceConfigs(): Promise { return Promise.resolve([]); } @@ -34,28 +52,34 @@ class FileSystemIde implements IDE { getDiff(): Promise { return Promise.resolve(""); } + getTerminalContents(): Promise { return Promise.resolve(""); } + async getDebugLocals(threadIndex: number): Promise { return Promise.resolve(""); } + async getTopLevelCallStackSources( threadIndex: number, - stackDepth: number + stackDepth: number, ): Promise { return Promise.resolve([]); } + async getAvailableThreads(): Promise { return Promise.resolve([]); } + showLines( filepath: string, startLine: number, - endLine: number + endLine: number, ): Promise { return Promise.resolve(); } + listWorkspaceContents(): Promise { return new Promise((resolve, reject) => { fs.readdir("/tmp/continue", (err, files) => { @@ -66,6 +90,7 @@ class FileSystemIde implements IDE { }); }); } + getWorkspaceDirs(): Promise { return new Promise((resolve, reject) => { fs.mkdtemp("/tmp/continue", (err, folder) => { @@ -76,9 +101,11 @@ class FileSystemIde implements IDE { }); }); } + listFolders(): Promise { return Promise.resolve([]); } + writeFile(path: string, contents: string): Promise { return new Promise((resolve, reject) => { fs.writeFile(path, contents, (err) => { @@ -89,21 +116,27 @@ class FileSystemIde implements IDE { }); }); } + showVirtualFile(title: string, contents: string): Promise { return Promise.resolve(); } + getContinueDir(): Promise { return Promise.resolve(getContinueGlobalPath()); } + openFile(path: string): Promise { return Promise.resolve(); } + runCommand(command: string): Promise { return Promise.resolve(); } + saveFile(filepath: string): Promise { return Promise.resolve(); } + readFile(filepath: string): Promise { return new Promise((resolve, reject) => { fs.readFile(filepath, "utf8", (err, contents) => { @@ -114,10 +147,11 @@ class FileSystemIde implements IDE { }); }); } + showDiff( filepath: string, newContents: string, - stepIndex: number + stepIndex: number, ): Promise { return Promise.resolve(); } diff --git a/core/util/messageIde.ts b/core/util/messageIde.ts index cfcaded5a..31f9e34d2 100644 --- a/core/util/messageIde.ts +++ b/core/util/messageIde.ts @@ -1,27 +1,44 @@ -import { ContinueRcJson, IDE, IdeInfo, IndexTag, Problem, Range, Thread } from ".."; +import { + ContinueRcJson, + IDE, + IdeInfo, + IndexTag, + Problem, + Range, + Thread, +} from ".."; export class MessageIde implements IDE { constructor( - private readonly request: (messageType: string, data: any) => Promise + private readonly request: (messageType: string, data: any) => Promise, ) {} + + getRepoName(dir: string): Promise { + return this.request("getRepoName", { dir }); + } + getDebugLocals(threadIndex: number): Promise { return this.request("getDebugLocals", { threadIndex }); } + getTopLevelCallStackSources( threadIndex: number, - stackDepth: number + stackDepth: number, ): Promise { return this.request("getTopLevelCallStackSources", { threadIndex, stackDepth, }); } + getAvailableThreads(): Promise { return this.request("getAvailableThreads", undefined); } + getTags(artifactId: string): Promise { return this.request("getTags", artifactId); } + getIdeInfo(): Promise { return this.request("getIdeInfo", undefined); } @@ -29,9 +46,11 @@ export class MessageIde implements IDE { readRangeInFile(filepath: string, range: Range): Promise { return this.request("readRangeInFile", { filepath, range }); } + getStats(directory: string): Promise<{ [path: string]: number }> { throw new Error("Method not implemented."); } + isTelemetryEnabled(): Promise { return this.request("isTelemetryEnabled", undefined); } @@ -63,7 +82,7 @@ export class MessageIde implements IDE { async showLines( filepath: string, startLine: number, - endLine: number + endLine: number, ): Promise { return await this.request("showLines", { filepath, startLine, endLine }); } @@ -108,7 +127,7 @@ export class MessageIde implements IDE { async showDiff( filepath: string, newContents: string, - stepIndex: number + stepIndex: number, ): Promise { await this.request("showDiff", { filepath, newContents, stepIndex }); } diff --git a/extensions/vscode/src/ideProtocol.ts b/extensions/vscode/src/ideProtocol.ts index 0727e3477..fd71d21f7 100644 --- a/extensions/vscode/src/ideProtocol.ts +++ b/extensions/vscode/src/ideProtocol.ts @@ -1,6 +1,14 @@ import * as child_process from "child_process"; import { exec } from "child_process"; -import { ContinueRcJson, IDE, IdeInfo, IndexTag, Problem, Range, Thread } from "core"; +import { + ContinueRcJson, + IDE, + IdeInfo, + IndexTag, + Problem, + Range, + Thread, +} from "core"; import { getContinueGlobalPath } from "core/util/paths"; import * as path from "path"; import * as vscode from "vscode"; @@ -19,6 +27,12 @@ class VsCodeIde implements IDE { constructor(private readonly diffManager: DiffManager) { this.ideUtils = new VsCodeIdeUtils(); } + + async getRepoName(dir: string): Promise { + const repo = await this.getRepo(vscode.Uri.file(dir)); + return repo?.state.HEAD?.name; + } + async getTags(artifactId: string): Promise { const workspaceDirs = await this.getWorkspaceDirs(); @@ -47,8 +61,8 @@ class VsCodeIde implements IDE { filepath, new vscode.Range( new vscode.Position(range.start.line, range.start.character), - new vscode.Position(range.end.line, range.end.character) - ) + new vscode.Position(range.end.line, range.end.character), + ), ); } @@ -60,7 +74,7 @@ class VsCodeIde implements IDE { files.map(async (file) => { let stat = await vscode.workspace.fs.stat(uriFromFilePath(file)); pathToLastModified[file] = stat.mtime; - }) + }), ); return pathToLastModified; @@ -95,11 +109,11 @@ class VsCodeIde implements IDE { async getTopLevelCallStackSources( threadIndex: number, - stackDepth: number + stackDepth: number, ): Promise { return await this.ideUtils.getTopLevelCallStackSources( threadIndex, - stackDepth + stackDepth, ); } async getAvailableThreads(): Promise { @@ -113,7 +127,7 @@ class VsCodeIde implements IDE { const contents = await Promise.all( this.ideUtils .getWorkspaceDirectories() - .map((dir) => this.ideUtils.getDirectoryContents(dir, true)) + .map((dir) => this.ideUtils.getDirectoryContents(dir, true)), ); return contents.flat(); } @@ -128,7 +142,7 @@ class VsCodeIde implements IDE { for (const [filename, type] of files) { if (type === vscode.FileType.File && filename === ".continuerc.json") { const contents = await this.ideUtils.readFile( - vscode.Uri.joinPath(workspaceDir, filename).fsPath + vscode.Uri.joinPath(workspaceDir, filename).fsPath, ); configs.push(JSON.parse(contents)); } @@ -146,7 +160,7 @@ class VsCodeIde implements IDE { directory, [], false, - undefined + undefined, )) { allDirs.push(dir); } @@ -166,7 +180,7 @@ class VsCodeIde implements IDE { async writeFile(path: string, contents: string): Promise { await vscode.workspace.fs.writeFile( vscode.Uri.file(path), - Buffer.from(contents) + Buffer.from(contents), ); } @@ -181,11 +195,11 @@ class VsCodeIde implements IDE { async showLines( filepath: string, startLine: number, - endLine: number + endLine: number, ): Promise { const range = new vscode.Range( new vscode.Position(startLine, 0), - new vscode.Position(endLine, 0) + new vscode.Position(endLine, 0), ); openEditorAndRevealRange(filepath, range).then(() => { // TODO: Highlight lines @@ -219,7 +233,7 @@ class VsCodeIde implements IDE { async showDiff( filepath: string, newContents: string, - stepIndex: number + stepIndex: number, ): Promise { await this.diffManager.writeDiff(filepath, newContents, stepIndex); } @@ -245,11 +259,11 @@ class VsCodeIde implements IDE { "@vscode", "ripgrep", "bin", - "rg" + "rg", ), ["-i", "-C", "2", "--", `${query}`, "."], //no regex //["-i", "-C", "2", "-e", `${query}`, "."], //use regex - { cwd: dir } + { cwd: dir }, ); let output = "";