From a5d37bacdb17ecc82c30ffac195489767b40a7cc Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Mon, 29 Jan 2024 20:04:39 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20improvements=20to=20codeba?= =?UTF-8?q?se=20retrieval=20with=20FTS5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/commands/util.ts | 4 +- core/context/retrieval.ts | 38 +++++++++++++------ core/ide/filesystem.ts | 7 ++++ core/ide/index.ts | 8 ++++ core/index.d.ts | 5 +++ core/indexing/FullTextSearch.ts | 19 +++++++--- extensions/vscode/src/continueIdeClient.ts | 4 +- extensions/vscode/src/debugPanel.ts | 24 +++--------- extensions/vscode/src/ideProtocol.ts | 22 ++++++++++- .../components/mainInput/ContextItemsPeek.tsx | 8 +++- .../markdown/CodeSnippetPreview.tsx | 11 +++--- .../markdown/StyledMarkdownPreview.tsx | 5 ++- 12 files changed, 107 insertions(+), 48 deletions(-) diff --git a/core/commands/util.ts b/core/commands/util.ts index 17e0151f0..e9ec5943d 100644 --- a/core/commands/util.ts +++ b/core/commands/util.ts @@ -15,10 +15,10 @@ export function contextItemToRangeInFileWithContents( const lines = item.name.split("(")[1].split(")")[0].split("-"); const rif: RangeInFileWithContents = { - filepath: item.description, + filepath: item.description.split(" (")[0], range: { start: { - line: parseInt(lines[0]) - 1, + line: parseInt(lines[0]), character: 0, }, end: { diff --git a/core/context/retrieval.ts b/core/context/retrieval.ts index 26bdc4e6b..53efd5579 100644 --- a/core/context/retrieval.ts +++ b/core/context/retrieval.ts @@ -136,18 +136,11 @@ export async function retrieveContextItemsFromEmbeddings( return []; } - const nRetrieve = options?.nRetrieve || 20; - const nFinal = options?.nFinal || 8; + const nFinal = options?.nFinal || 10; const useReranking = llmCanGenerateInParallel(extras.llm) && (options?.useReranking === undefined ? false : options?.useReranking); - - // Similarity search - let results = await extras.ide.retrieveChunks( - extras.fullInput, - useReranking === false ? nFinal : nRetrieve, - filterDirectory - ); + const nRetrieve = useReranking === false ? nFinal : options?.nRetrieve || 20; const ftsIndex = new FullTextSearchCodebaseIndex(); const workspaceDirs = await extras.ide.getWorkspaceDirs(); @@ -159,15 +152,38 @@ export async function retrieveContextItemsFromEmbeddings( branch: branches[i], artifactId: ChunkCodebaseIndex.artifactId, })); + let ftsResults = await ftsIndex.retrieve( tags, extras.fullInput.trim().split(" ").join(" OR "), + nRetrieve / 2, + filterDirectory, + undefined + ); + + let vecResults = await extras.ide.retrieveChunks( + extras.fullInput, nRetrieve, filterDirectory ); - console.log("SIM RESULTS: ", results); - console.log("FTS RESULTS: ", ftsResults); + // Now combine these (de-duplicate) and re-rank + let results = [...ftsResults]; + for (const vecResult of vecResults) { + if (results.length >= nRetrieve) { + break; + } + if ( + !ftsResults.find( + (r) => + r.filepath === vecResult.filepath && + r.startLine === vecResult.startLine && + r.endLine === vecResult.endLine + ) + ) { + results.push(vecResult); + } + } // Re-ranking if (useReranking) { diff --git a/core/ide/filesystem.ts b/core/ide/filesystem.ts index 06d4e8c76..ad79c7e1f 100644 --- a/core/ide/filesystem.ts +++ b/core/ide/filesystem.ts @@ -28,6 +28,13 @@ class FileSystemIde implements IDE { getTerminalContents(): Promise { return Promise.resolve(""); } + showLines( + filepath: string, + startLine: number, + endLine: number + ): Promise { + return Promise.resolve(); + } listWorkspaceContents(): Promise { return new Promise((resolve, reject) => { fs.readdir("/tmp/continue", (err, files) => { diff --git a/core/ide/index.ts b/core/ide/index.ts index 58647bfbf..02c808634 100644 --- a/core/ide/index.ts +++ b/core/ide/index.ts @@ -30,6 +30,14 @@ export class ExtensionIde implements IDE { return await r("getWorkspaceDirs"); } + async showLines( + filepath: string, + startLine: number, + endLine: number + ): Promise { + return await r("showLines", { filepath, startLine, endLine }); + } + async listFolders(): Promise { return await r("listFolders"); } diff --git a/core/index.d.ts b/core/index.d.ts index d22a3c9a5..40a577a04 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -300,6 +300,11 @@ export interface IDE { runCommand(command: string): Promise; saveFile(filepath: string): Promise; readFile(filepath: string): Promise; + showLines( + filepath: string, + startLine: number, + endLine: number + ): Promise; showDiff( filepath: string, newContents: string, diff --git a/core/indexing/FullTextSearch.ts b/core/indexing/FullTextSearch.ts index 6688174ac..c7720bbf5 100644 --- a/core/indexing/FullTextSearch.ts +++ b/core/indexing/FullTextSearch.ts @@ -89,7 +89,8 @@ export class FullTextSearchCodebaseIndex implements CodebaseIndex { tags: IndexTag[], text: string, n: number, - directory: string | undefined + directory: string | undefined, + filterPaths: string[] | undefined ): Promise { const db = await SqliteDb.get(); const tagStrings = tags.map(tagToString); @@ -99,12 +100,20 @@ export class FullTextSearchCodebaseIndex implements CodebaseIndex { 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}' AND chunk_tags.tag IN (${tagStrings - .map(() => "?") - .join(",")}) + 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 ?`, - [...tagStrings, n] + [...tagStrings, ...(filterPaths || []), n] ); const chunks = await db.all( diff --git a/extensions/vscode/src/continueIdeClient.ts b/extensions/vscode/src/continueIdeClient.ts index 2f083f3bd..38cb966cc 100644 --- a/extensions/vscode/src/continueIdeClient.ts +++ b/extensions/vscode/src/continueIdeClient.ts @@ -233,9 +233,9 @@ class IdeProtocolClient { panel.webview.html = getSidebarContent(panel, "/monaco", edits); } - openFile(filepath: string) { + openFile(filepath: string, range?: vscode.Range) { // vscode has a builtin open/get open files - openEditorAndRevealRange(filepath, undefined, vscode.ViewColumn.One); + return openEditorAndRevealRange(filepath, range, vscode.ViewColumn.One); } async fileExists(filepath: string): Promise { diff --git a/extensions/vscode/src/debugPanel.ts b/extensions/vscode/src/debugPanel.ts index 71fb09053..ccb28c435 100644 --- a/extensions/vscode/src/debugPanel.ts +++ b/extensions/vscode/src/debugPanel.ts @@ -211,25 +211,6 @@ export function getSidebarContent( }); break; } - case "showLines": { - ideProtocolClient.highlightCode( - { - filepath: data.filepath, - range: { - start: { - line: data.start, - character: 0, - }, - end: { - line: data.end, - character: 0, - }, - }, - }, - "#00ff0022" - ); - break; - } case "toggleDevTools": { vscode.commands.executeCommand("workbench.action.toggleDevTools"); vscode.commands.executeCommand("continue.viewLogs"); @@ -441,6 +422,11 @@ export function getSidebarContent( respond(await ide.getPinnedFiles()); break; } + case "showLines": { + const { filepath, startLine, endLine } = data.message; + respond(await ide.showLines(filepath, startLine, endLine)); + break; + } // Other case "errorPopup": { vscode.window diff --git a/extensions/vscode/src/ideProtocol.ts b/extensions/vscode/src/ideProtocol.ts index 83c63f6b7..7c6f38916 100644 --- a/extensions/vscode/src/ideProtocol.ts +++ b/extensions/vscode/src/ideProtocol.ts @@ -31,7 +31,7 @@ import { verticalPerLineDiffManager } from "./diff/verticalPerLine/manager"; import { configHandler } from "./loadConfig"; import mergeJson from "./util/merge"; import { traverseDirectory } from "./util/traverseDirectory"; -import { getExtensionUri } from "./util/vscode"; +import { getExtensionUri, openEditorAndRevealRange } from "./util/vscode"; async function buildConfigTs(browser: boolean) { if (!fs.existsSync(getConfigTsPath())) { @@ -295,6 +295,26 @@ class VsCodeIde implements IDE { ideProtocolClient.openFile(path); } + async showLines( + filepath: string, + startLine: number, + endLine: number + ): Promise { + const range = new vscode.Range( + new vscode.Position(startLine, 0), + new vscode.Position(endLine, 0) + ); + openEditorAndRevealRange(filepath, range).then(() => { + ideProtocolClient.highlightCode( + { + filepath, + range, + }, + "#fff1" + ); + }); + } + async runCommand(command: string): Promise { await ideProtocolClient.runCommand(command); } diff --git a/gui/src/components/mainInput/ContextItemsPeek.tsx b/gui/src/components/mainInput/ContextItemsPeek.tsx index bdf99d708..466700495 100644 --- a/gui/src/components/mainInput/ContextItemsPeek.tsx +++ b/gui/src/components/mainInput/ContextItemsPeek.tsx @@ -1,5 +1,6 @@ import { ChevronDownIcon, ChevronUpIcon } from "@heroicons/react/24/outline"; import { ContextItemWithId } from "core"; +import { contextItemToRangeInFileWithContents } from "core/commands/util"; import { ExtensionIde } from "core/ide"; import React from "react"; import styled from "styled-components"; @@ -46,8 +47,11 @@ const ContextItemsPeek = (props: ContextItemsPeekProps) => { contextItem.description.startsWith("/") || contextItem.description.startsWith("\\") ) { - new ExtensionIde().openFile( - contextItem.description.split(" ").shift() || "" + const rif = contextItemToRangeInFileWithContents(contextItem); + new ExtensionIde().showLines( + rif.filepath, + rif.range.start.line, + rif.range.end.line ); } else { new ExtensionIde().showVirtualFile(contextItem.name, contextItem.content); diff --git a/gui/src/components/markdown/CodeSnippetPreview.tsx b/gui/src/components/markdown/CodeSnippetPreview.tsx index 646764b5e..3f2a2adfa 100644 --- a/gui/src/components/markdown/CodeSnippetPreview.tsx +++ b/gui/src/components/markdown/CodeSnippetPreview.tsx @@ -5,6 +5,7 @@ import { XMarkIcon, } from "@heroicons/react/24/outline"; import { ContextItemWithId } from "core"; +import { ExtensionIde } from "core/ide"; import { getMarkdownLanguageTagForFile } from "core/util"; import React from "react"; import { useDispatch } from "react-redux"; @@ -97,11 +98,11 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) { .split("(")[1] .split(")")[0] .split("-"); - postToIde("showLines", { - filepath: props.item.description, - start: parseInt(lines[0]) - 1, - end: parseInt(lines[1]) - 1, - }); + new ExtensionIde().showLines( + props.item.description, + parseInt(lines[0]) - 1, + parseInt(lines[1]) - 1 + ); } else { postToIde("showVirtualFile", { name: props.item.name, diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 6e7b3333d..ec26ba2ed 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -74,7 +74,10 @@ const StyledMarkdown = styled.div<{ padding-right: 8px; color: ${vscForeground}; - p { + p, + li, + ol, + ul { line-height: 1.5; } `;