continue-custom/core/context/retrieval/retrieval.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

167 lines
5.1 KiB
TypeScript

import {
BranchAndDir,
Chunk,
ContextItem,
ContextProviderExtras,
} from "../../index.js";
import { LanceDbIndex } from "../../indexing/LanceDbIndex.js";
import { deduplicateArray, getBasename } from "../../util/index.js";
import { RETRIEVAL_PARAMS } from "../../util/parameters.js";
import { retrieveFts } from "./fullTextSearch.js";
function deduplicateChunks(chunks: Chunk[]): Chunk[] {
return deduplicateArray(chunks, (a, b) => {
return (
a.filepath === b.filepath &&
a.startLine === b.startLine &&
a.endLine === b.endLine
);
});
}
export async function retrieveContextItemsFromEmbeddings(
extras: ContextProviderExtras,
options: any | undefined,
filterDirectory: string | undefined,
): Promise<ContextItem[]> {
if (!extras.embeddingsProvider) {
return [];
}
// transformers.js not supported in JetBrains IDEs right now
if (
extras.embeddingsProvider.id === "all-MiniLM-L6-v2" &&
(await extras.ide.getIdeInfo()).ideType === "jetbrains"
) {
throw new Error(
"The transformers.js context provider is not currently supported in JetBrains. For now, you can use Ollama to set up local embeddings, or use our 'free-trial' embeddings provider. See here to learn more: https://docs.continue.dev/walkthroughs/codebase-embeddings#embeddings-providers",
);
}
const nFinal = options?.nFinal || RETRIEVAL_PARAMS.nFinal;
const useReranking = extras.reranker !== undefined;
const nRetrieve =
useReranking === false
? nFinal
: options?.nRetrieve || RETRIEVAL_PARAMS.nRetrieve;
// Get tags to retrieve for
const workspaceDirs = await extras.ide.getWorkspaceDirs();
if (workspaceDirs.length === 0) {
throw new Error("No workspace directories found");
}
const branches = (await Promise.race([
Promise.all(workspaceDirs.map((dir) => extras.ide.getBranch(dir))),
new Promise((resolve) => {
setTimeout(() => {
resolve(["NONE"]);
}, 500);
}),
])) as string[];
const tags: BranchAndDir[] = workspaceDirs.map((directory, i) => ({
directory,
branch: branches[i],
}));
// Get all retrieval results
const retrievalResults: Chunk[] = [];
// Source: Full-text search
const ftsResults = await retrieveFts(
extras.fullInput,
nRetrieve / 2,
tags,
filterDirectory,
);
retrievalResults.push(...ftsResults);
// Source: expansion with code graph
// consider doing this after reranking? Or just having a lower reranking threshold
// This is VS Code only until we use PSI for JetBrains or build our own general solution
// TODO: Need to pass in the expandSnippet function as a function argument
// because this import causes `tsc` to fail
// if ((await extras.ide.getIdeInfo()).ideType === "vscode") {
// const { expandSnippet } = await import(
// "../../../extensions/vscode/src/util/expandSnippet"
// );
// let expansionResults = (
// await Promise.all(
// extras.selectedCode.map(async (rif) => {
// return expandSnippet(
// rif.filepath,
// rif.range.start.line,
// rif.range.end.line,
// extras.ide,
// );
// }),
// )
// ).flat() as Chunk[];
// retrievalResults.push(...expansionResults);
// }
// Source: Open file exact match
// Source: Class/function name exact match
// Source: Embeddings
const lanceDbIndex = new LanceDbIndex(extras.embeddingsProvider, (path) =>
extras.ide.readFile(path),
);
const vecResults = await lanceDbIndex.retrieve(
extras.fullInput,
nRetrieve,
tags,
filterDirectory,
);
retrievalResults.push(...vecResults);
// De-duplicate
let results: Chunk[] = deduplicateChunks(retrievalResults);
// Re-rank
if (useReranking && extras.reranker) {
let scores: number[] = await extras.reranker.rerank(
extras.fullInput,
results,
);
// Filter out low-scoring results
results = results.filter(
(_, i) => scores[i] >= RETRIEVAL_PARAMS.rerankThreshold,
);
scores = scores.filter(
(score) => score >= RETRIEVAL_PARAMS.rerankThreshold,
);
results.sort(
(a, b) => scores[results.indexOf(a)] - scores[results.indexOf(b)],
);
results = results.slice(-nFinal);
}
if (results.length === 0) {
throw new Error(
"Warning: No results found for @codebase context provider.",
);
}
return [
...results.map((r) => {
const name = `${getBasename(r.filepath)} (${r.startLine}-${r.endLine})`;
const description = `${r.filepath} (${r.startLine}-${r.endLine})`;
return {
name,
description,
content: `\`\`\`${name}\n${r.content}\n\`\`\``,
};
}),
{
name: "Instructions",
description: "Instructions",
content:
"Use the above code to answer the following question. You should not reference any files outside of what is shown, unless they are commonly known files, like a .gitignore or package.json. Reference the filenames whenever possible. If there isn't enough information to answer the question, suggest where the user might look to learn more.",
},
];
}