| 1 | import { RangeInFileWithContents } from "../../../"; |
| 2 | import { countTokens } from "../../../llm/countTokens"; |
| 3 | import { AutocompleteSnippetDeprecated } from "../../types"; |
| 4 | import { HelperVars } from "../../util/HelperVars"; |
| 5 | |
| 6 | const rx = /[\s.,\/#!$%\^&\*;:{}=\-_`~()\[\]]/g; |
| 7 | export function getSymbolsForSnippet(snippet: string): Set<string> { |
| 8 | const symbols = snippet |
| 9 | .split(rx) |
| 10 | .map((s) => s.trim()) |
| 11 | .filter((s) => s !== ""); |
| 12 | return new Set(symbols); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Calculate similarity as number of shared symbols divided by total number of unique symbols between both. |
| 17 | */ |
| 18 | function jaccardSimilarity(a: string, b: string): number { |
| 19 | const aSet = getSymbolsForSnippet(a); |
| 20 | const bSet = getSymbolsForSnippet(b); |
| 21 | const union = new Set([...aSet, ...bSet]).size; |
| 22 | |
| 23 | // Avoid division by zero |
| 24 | if (union === 0) { |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | let intersection = 0; |
| 29 | for (const symbol of aSet) { |
| 30 | if (bSet.has(symbol)) { |
| 31 | intersection++; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return intersection / union; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Rank code snippets to be used in tab-autocomplete prompt. Returns a sorted version of the snippet array. |
| 40 | */ |
| 41 | export function rankAndOrderSnippets( |
| 42 | ranges: AutocompleteSnippetDeprecated[], |
| 43 | helper: HelperVars, |
| 44 | ): Required<AutocompleteSnippetDeprecated>[] { |
| 45 | const windowAroundCursor = |
| 46 | helper.fullPrefix.slice( |
| 47 | -helper.options.slidingWindowSize * |
| 48 | helper.options.slidingWindowPrefixPercentage, |
| 49 | ) + |
| 50 | helper.fullSuffix.slice( |
| 51 | helper.options.slidingWindowSize * |
| 52 | (1 - helper.options.slidingWindowPrefixPercentage), |
| 53 | ); |
| 54 | |
| 55 | const snippets: Required<AutocompleteSnippetDeprecated>[] = ranges.map( |
| 56 | (snippet) => ({ |
| 57 | score: |
| 58 | snippet.score ?? |
| 59 | jaccardSimilarity(snippet.contents, windowAroundCursor), |
| 60 | ...snippet, |
| 61 | }), |
| 62 | ); |
| 63 | const uniqueSnippets = deduplicateSnippets(snippets); |
| 64 | return uniqueSnippets.sort((a, b) => a.score - b.score); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Deduplicate code snippets by merging overlapping ranges into a single range. |
| 69 | */ |
| 70 | function deduplicateSnippets( |
| 71 | snippets: Required<AutocompleteSnippetDeprecated>[], |
| 72 | ): Required<AutocompleteSnippetDeprecated>[] { |
| 73 | // Group by file |
| 74 | const fileGroups: { |
| 75 | [key: string]: Required<AutocompleteSnippetDeprecated>[]; |
| 76 | } = {}; |
| 77 | for (const snippet of snippets) { |
| 78 | if (!fileGroups[snippet.filepath]) { |
| 79 | fileGroups[snippet.filepath] = []; |
| 80 | } |
| 81 | fileGroups[snippet.filepath].push(snippet); |
| 82 | } |
| 83 | |
| 84 | // Merge overlapping ranges |
| 85 | const allRanges = []; |
| 86 | for (const file of Object.keys(fileGroups)) { |
| 87 | allRanges.push(...mergeSnippetsByRange(fileGroups[file])); |
| 88 | } |
| 89 | return allRanges; |
| 90 | } |
| 91 | |
| 92 | function mergeSnippetsByRange( |
| 93 | snippets: Required<AutocompleteSnippetDeprecated>[], |
| 94 | ): Required<AutocompleteSnippetDeprecated>[] { |
| 95 | if (snippets.length <= 1) { |
| 96 | return snippets; |
| 97 | } |
| 98 | |
| 99 | const sorted = snippets.sort( |
| 100 | (a, b) => a.range.start.line - b.range.start.line, |
| 101 | ); |
| 102 | const merged: Required<AutocompleteSnippetDeprecated>[] = []; |
| 103 | |
| 104 | while (sorted.length > 0) { |
| 105 | const next = sorted.shift()!; |
| 106 | const last = merged[merged.length - 1]; |
| 107 | if (merged.length > 0 && last.range.end.line >= next.range.start.line) { |
| 108 | // Merge with previous snippet |
| 109 | last.score = Math.max(last.score, next.score); |
| 110 | try { |
| 111 | last.range.end = next.range.end; |
| 112 | } catch (e) { |
| 113 | console.log("Error merging ranges", e); |
| 114 | } |
| 115 | last.contents = mergeOverlappingRangeContents(last, next); |
| 116 | } else { |
| 117 | merged.push(next); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return merged; |
| 122 | } |
| 123 | |
| 124 | function mergeOverlappingRangeContents( |
| 125 | first: RangeInFileWithContents, |
| 126 | second: RangeInFileWithContents, |
| 127 | ): string { |
| 128 | const firstLines = first.contents.split("\n"); |
| 129 | const numOverlapping = first.range.end.line - second.range.start.line; |
| 130 | return `${firstLines.slice(-numOverlapping).join("\n")}\n${second.contents}`; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Fill the allowed space with snippets. |
| 135 | * It is assumed that the snippets are sorted by score. |
| 136 | */ |
| 137 | export function fillPromptWithSnippets( |
| 138 | snippets: Required<AutocompleteSnippetDeprecated>[], |
| 139 | maxSnippetTokens: number, |
| 140 | modelName: string, |
| 141 | ): Required<AutocompleteSnippetDeprecated>[] { |
| 142 | let tokensRemaining = maxSnippetTokens; |
| 143 | const keptSnippets: Required<AutocompleteSnippetDeprecated>[] = []; |
| 144 | for (let i = 0; i < snippets.length; i++) { |
| 145 | const snippet = snippets[i]; |
| 146 | const tokenCount = countTokens(snippet.contents, modelName); |
| 147 | if (tokensRemaining - tokenCount >= 0) { |
| 148 | tokensRemaining -= tokenCount; |
| 149 | keptSnippets.push(snippet); |
| 150 | } else { |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return keptSnippets; |
| 155 | } |