* 🎨 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
243 lines
6 KiB
TypeScript
243 lines
6 KiB
TypeScript
import { RangeInFileWithContents } from "../commands/util.js";
|
|
import { Range } from "../index.js";
|
|
import { countTokens } from "../llm/countTokens.js";
|
|
|
|
export type AutocompleteSnippet = RangeInFileWithContents & {
|
|
score?: number;
|
|
};
|
|
|
|
const rx = /[\s.,\/#!$%\^&\*;:{}=\-_`~()\[\]]/g;
|
|
export function getSymbolsForSnippet(snippet: string): Set<string> {
|
|
const symbols = snippet
|
|
.split(rx)
|
|
.map((s) => s.trim())
|
|
.filter((s) => s !== "");
|
|
return new Set(symbols);
|
|
}
|
|
|
|
/**
|
|
* Calculate similarity as number of shared symbols divided by total number of unique symbols between both.
|
|
*/
|
|
export function jaccardSimilarity(a: string, b: string): number {
|
|
const aSet = getSymbolsForSnippet(a);
|
|
const bSet = getSymbolsForSnippet(b);
|
|
const union = new Set([...aSet, ...bSet]).size;
|
|
|
|
// Avoid division by zero
|
|
if (union === 0) {
|
|
return 0;
|
|
}
|
|
|
|
let intersection = 0;
|
|
for (const symbol of aSet) {
|
|
if (bSet.has(symbol)) {
|
|
intersection++;
|
|
}
|
|
}
|
|
|
|
return intersection / union;
|
|
}
|
|
|
|
/**
|
|
* Rank code snippets to be used in tab-autocomplete prompt. Returns a sorted version of the snippet array.
|
|
*/
|
|
export function rankSnippets(
|
|
ranges: AutocompleteSnippet[],
|
|
windowAroundCursor: string,
|
|
): Required<AutocompleteSnippet>[] {
|
|
const snippets: Required<AutocompleteSnippet>[] = ranges.map((snippet) => ({
|
|
score:
|
|
snippet.score ?? jaccardSimilarity(snippet.contents, windowAroundCursor),
|
|
...snippet,
|
|
}));
|
|
const uniqueSnippets = deduplicateSnippets(snippets);
|
|
return uniqueSnippets.sort((a, b) => a.score - b.score);
|
|
}
|
|
|
|
/**
|
|
* Deduplicate code snippets by merging overlapping ranges into a single range.
|
|
*/
|
|
export function deduplicateSnippets(
|
|
snippets: Required<AutocompleteSnippet>[],
|
|
): Required<AutocompleteSnippet>[] {
|
|
// Group by file
|
|
const fileGroups: { [key: string]: Required<AutocompleteSnippet>[] } = {};
|
|
for (const snippet of snippets) {
|
|
if (!fileGroups[snippet.filepath]) {
|
|
fileGroups[snippet.filepath] = [];
|
|
}
|
|
fileGroups[snippet.filepath].push(snippet);
|
|
}
|
|
|
|
// Merge overlapping ranges
|
|
const allRanges = [];
|
|
for (const file of Object.keys(fileGroups)) {
|
|
allRanges.push(...mergeSnippetsByRange(fileGroups[file]));
|
|
}
|
|
return allRanges;
|
|
}
|
|
|
|
function mergeSnippetsByRange(
|
|
snippets: Required<AutocompleteSnippet>[],
|
|
): Required<AutocompleteSnippet>[] {
|
|
if (snippets.length === 0) {
|
|
return snippets;
|
|
}
|
|
|
|
const sorted = snippets.sort(
|
|
(a, b) => a.range.start.line - b.range.start.line,
|
|
);
|
|
const merged: Required<AutocompleteSnippet>[] = [];
|
|
|
|
while (sorted.length > 0) {
|
|
const next = sorted.shift()!;
|
|
const last = merged[merged.length - 1];
|
|
if (merged.length > 0 && last.range.end.line >= next.range.start.line) {
|
|
// Merge with previous snippet
|
|
last.score = Math.max(last.score, next.score);
|
|
try {
|
|
last.range.end = next.range.end;
|
|
} catch (e) {
|
|
console.log("Error merging ranges", e);
|
|
}
|
|
last.contents = mergeOverlappingRangeContents(last, next);
|
|
} else {
|
|
merged.push(next);
|
|
}
|
|
}
|
|
|
|
return merged;
|
|
}
|
|
|
|
function mergeOverlappingRangeContents(
|
|
first: RangeInFileWithContents,
|
|
second: RangeInFileWithContents,
|
|
): string {
|
|
const firstLines = first.contents.split("\n");
|
|
const numOverlapping = first.range.end.line - second.range.start.line;
|
|
return `${firstLines.slice(-numOverlapping).join("\n")}\n${second.contents}`;
|
|
}
|
|
|
|
/**
|
|
* Fill the allowed space with snippets
|
|
*/
|
|
export function fillPromptWithSnippets(
|
|
snippets: Required<AutocompleteSnippet>[],
|
|
maxSnippetTokens: number,
|
|
modelName: string,
|
|
): Required<AutocompleteSnippet>[] {
|
|
let tokensRemaining = maxSnippetTokens;
|
|
const keptSnippets: Required<AutocompleteSnippet>[] = [];
|
|
for (let i = 0; i < snippets.length; i++) {
|
|
const snippet = snippets[i];
|
|
const tokenCount = countTokens(snippet.contents, modelName);
|
|
if (tokensRemaining - tokenCount >= 0) {
|
|
tokensRemaining -= tokenCount;
|
|
keptSnippets.push(snippet);
|
|
} else {
|
|
}
|
|
}
|
|
|
|
return keptSnippets;
|
|
}
|
|
|
|
function rangeIntersectionByLines(a: Range, b: Range): Range | null {
|
|
const startLine = Math.max(a.start.line, b.start.line);
|
|
const endLine = Math.min(a.end.line, b.end.line);
|
|
if (startLine >= endLine) {
|
|
return null;
|
|
}
|
|
return {
|
|
start: {
|
|
line: startLine,
|
|
character: 0,
|
|
},
|
|
end: {
|
|
line: endLine,
|
|
character: 0,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Remove one range from another range, which may lead to returning two disjoint ranges
|
|
*/
|
|
function rangeDifferenceByLines(orig: Range, remove: Range): Range[] {
|
|
if (
|
|
orig.start.line >= remove.start.line &&
|
|
orig.end.line <= remove.end.line
|
|
) {
|
|
// / | | /
|
|
return [];
|
|
}
|
|
if (
|
|
orig.start.line <= remove.start.line &&
|
|
orig.end.line >= remove.end.line
|
|
) {
|
|
// | / / |
|
|
// Splits the range
|
|
return [
|
|
{
|
|
start: orig.start,
|
|
end: remove.start,
|
|
},
|
|
{
|
|
start: remove.end,
|
|
end: orig.end,
|
|
},
|
|
];
|
|
}
|
|
if (
|
|
orig.start.line >= remove.start.line &&
|
|
orig.end.line >= remove.end.line
|
|
) {
|
|
// \ | / |
|
|
return [
|
|
{
|
|
start: remove.end,
|
|
end: orig.end,
|
|
},
|
|
];
|
|
}
|
|
if (
|
|
orig.start.line <= remove.start.line &&
|
|
orig.end.line <= remove.end.line
|
|
) {
|
|
// | / | /
|
|
return [
|
|
{
|
|
start: orig.start,
|
|
end: remove.start,
|
|
},
|
|
];
|
|
}
|
|
return [orig];
|
|
}
|
|
|
|
export function removeRangeFromSnippets(
|
|
snippets: Required<AutocompleteSnippet>[],
|
|
filepath: string,
|
|
range: Range,
|
|
): Required<AutocompleteSnippet>[] {
|
|
const finalSnippets: Required<AutocompleteSnippet>[] = [];
|
|
for (const snippet of snippets) {
|
|
if (snippet.filepath !== filepath) {
|
|
finalSnippets.push(snippet);
|
|
continue;
|
|
}
|
|
|
|
const intersection = rangeIntersectionByLines(range, snippet.range);
|
|
if (!intersection) {
|
|
finalSnippets.push(snippet);
|
|
} else {
|
|
finalSnippets.push(
|
|
...rangeDifferenceByLines(snippet.range, intersection).map((range) => ({
|
|
...snippet,
|
|
range,
|
|
})),
|
|
);
|
|
}
|
|
}
|
|
|
|
return finalSnippets;
|
|
}
|