Skip to content

Commit 3987303

Browse files
committed
implemented very simple priority-based deduplication
1 parent 95abe0c commit 3987303

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

core/autocomplete/templating/filtering.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ export const getSnippets = (
130130
const finalSnippets = [];
131131
let remainingTokenCount = getRemainingTokenCount(helper);
132132

133+
// tracks already added filepaths for deduplication
134+
const addedFilepaths = new Set<string>();
135+
133136
// Process snippets in priority order
134137
for (const { key } of snippetOrder) {
135138
// Special handling for recentlyOpenedFiles
@@ -160,9 +163,10 @@ export const getSnippets = (
160163

161164
if (remainingTokenCount >= snippetSize) {
162165
finalSnippets.push(snippet);
166+
addedFilepaths.add(snippet.filepath);
163167
remainingTokenCount -= snippetSize;
164168
} else {
165-
break; // Out of tokens
169+
continue; // Not enough tokens, try again with next snippet
166170
}
167171
}
168172
} else {
@@ -171,7 +175,9 @@ export const getSnippets = (
171175
(snippet) =>
172176
!(snippet as AutocompleteCodeSnippet).filepath?.startsWith(
173177
"output:extension-output-Continue.continue",
174-
),
178+
) &&
179+
((snippet as AutocompleteCodeSnippet).filepath === undefined ||
180+
!addedFilepaths.has((snippet as AutocompleteCodeSnippet).filepath)),
175181
);
176182

177183
for (const snippet of snippetsToProcess) {
@@ -182,9 +188,14 @@ export const getSnippets = (
182188

183189
if (remainingTokenCount >= snippetSize) {
184190
finalSnippets.push(snippet);
191+
192+
if ((snippet as AutocompleteCodeSnippet).filepath) {
193+
addedFilepaths.add((snippet as AutocompleteCodeSnippet).filepath);
194+
}
195+
185196
remainingTokenCount -= snippetSize;
186197
} else {
187-
break; // Out of tokens
198+
continue; // Not enough tokens, try again with next snippet
188199
}
189200
}
190201
}

core/autocomplete/templating/formatOpenedFilesContext.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { countTokens, pruneStringFromBottom } from "../../llm/countTokens";
22
import {
33
AutocompleteCodeSnippet,
44
AutocompleteSnippet,
5+
AutocompleteSnippetType,
56
} from "../snippets/types";
67
import { HelperVars } from "../util/HelperVars";
78

@@ -20,13 +21,23 @@ export function formatOpenedFilesContext(
2021
recentlyOpenedFilesSnippets: AutocompleteCodeSnippet[],
2122
remainingTokenCount: number,
2223
helper: HelperVars,
23-
alreadyAddedSnippets: AutocompleteSnippet[], // TODO use this to deduplicate context
24+
alreadyAddedSnippets: AutocompleteSnippet[],
2425
TOKEN_BUFFER: number,
2526
): AutocompleteCodeSnippet[] {
2627
if (recentlyOpenedFilesSnippets.length === 0) {
2728
return [];
2829
}
2930

31+
// deduplication; if a snippet is already added, don't include it here
32+
for (const snippet of alreadyAddedSnippets) {
33+
if (snippet.type !== AutocompleteSnippetType.Code) {
34+
continue;
35+
}
36+
recentlyOpenedFilesSnippets = recentlyOpenedFilesSnippets.filter(
37+
(s) => s.filepath !== snippet.filepath,
38+
);
39+
}
40+
3041
// Calculate how many full snippets would fit within the remaining token count
3142
let numSnippetsThatFit = 0;
3243
let totalTokens = 0;

0 commit comments

Comments
 (0)