⚡️ improvements to codebase retrieval with FTS5

This commit is contained in:
Nate Sesti 2024-01-29 20:04:39 -08:00
parent b71b2b05aa
commit a5d37bacdb
12 changed files with 107 additions and 48 deletions

View file

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

View file

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

View file

@ -28,6 +28,13 @@ class FileSystemIde implements IDE {
getTerminalContents(): Promise<string> {
return Promise.resolve("");
}
showLines(
filepath: string,
startLine: number,
endLine: number
): Promise<void> {
return Promise.resolve();
}
listWorkspaceContents(): Promise<string[]> {
return new Promise((resolve, reject) => {
fs.readdir("/tmp/continue", (err, files) => {

View file

@ -30,6 +30,14 @@ export class ExtensionIde implements IDE {
return await r("getWorkspaceDirs");
}
async showLines(
filepath: string,
startLine: number,
endLine: number
): Promise<void> {
return await r("showLines", { filepath, startLine, endLine });
}
async listFolders(): Promise<string[]> {
return await r("listFolders");
}

5
core/index.d.ts vendored
View file

@ -300,6 +300,11 @@ export interface IDE {
runCommand(command: string): Promise<void>;
saveFile(filepath: string): Promise<void>;
readFile(filepath: string): Promise<string>;
showLines(
filepath: string,
startLine: number,
endLine: number
): Promise<void>;
showDiff(
filepath: string,
newContents: string,

View file

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

View file

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

View file

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

View file

@ -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<void> {
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<void> {
await ideProtocolClient.runCommand(command);
}

View file

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

View file

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

View file

@ -74,7 +74,10 @@ const StyledMarkdown = styled.div<{
padding-right: 8px;
color: ${vscForeground};
p {
p,
li,
ol,
ul {
line-height: 1.5;
}
`;