Elffuss BraidHub

kikocis/continue public

Merge pull request #6055 from continuedev/adarsh/enhancement/deduplication

Simple Context Deduplication for Autocomplete
Changeset0795258f043e3529ad9d886c6f3195c0841a728e4563ca0fabcf79d3bdf2e920
AuthorAdarsh Iyer <76015889+adarshramiyer@users.noreply.github.com> · 2025-06-11 18:42 -07:00
CommitterGitHub <noreply@github.com> · 2025-06-11 18:42 -07:00
Parentsdc2ca87101a3, a665520662d1

Browse the files at this changeset

3 files changed

modified core/autocomplete/templating/filtering.ts +15 −14
@@ -3,6 +3,7 @@
33 import {
44 AutocompleteCodeSnippet,
55 AutocompleteSnippet,
6+ AutocompleteSnippetType,
67 } from "../snippets/types";
78 import { HelperVars } from "../util/HelperVars";
89 import { formatOpenedFilesContext } from "./formatOpenedFilesContext";
@@ -130,21 +131,16 @@
130131 const finalSnippets = [];
131132 let remainingTokenCount = getRemainingTokenCount(helper);
132133
134+ // tracks already added filepaths for deduplication
135+ const addedFilepaths = new Set<string>();
136+
133137 // Process snippets in priority order
134138 for (const { key } of snippetOrder) {
135139 // Special handling for recentlyOpenedFiles
136140 if (key === "recentlyOpenedFiles" && helper.options.useRecentlyOpened) {
137- const recentlyOpenedFilesSnippets =
138- payload.recentlyOpenedFileSnippets.filter(
139- (snippet) =>
140- !(snippet as AutocompleteCodeSnippet).filepath?.startsWith(
141- "output:extension-output-Continue.continue",
142- ),
143- );
144-
145141 // Custom trimming
146142 const processedSnippets = formatOpenedFilesContext(
147- recentlyOpenedFilesSnippets,
143+ payload.recentlyOpenedFileSnippets,
148144 remainingTokenCount,
149145 helper,
150146 finalSnippets,
@@ -160,18 +156,18 @@
160156
161157 if (remainingTokenCount >= snippetSize) {
162158 finalSnippets.push(snippet);
159+ addedFilepaths.add(snippet.filepath);
163160 remainingTokenCount -= snippetSize;
164161 } else {
165- break; // Out of tokens
162+ continue; // Not enough tokens, try again with next snippet
166163 }
167164 }
168165 } else {
169166 // Normal processing for other snippet types
170167 const snippetsToProcess = snippets[key].filter(
171168 (snippet) =>
172- !(snippet as AutocompleteCodeSnippet).filepath?.startsWith(
173- "output:extension-output-Continue.continue",
174- ),
169+ snippet.type !== AutocompleteSnippetType.Code ||
170+ !addedFilepaths.has(snippet.filepath),
175171 );
176172
177173 for (const snippet of snippetsToProcess) {
@@ -182,9 +178,14 @@
182178
183179 if (remainingTokenCount >= snippetSize) {
184180 finalSnippets.push(snippet);
181+
182+ if ((snippet as AutocompleteCodeSnippet).filepath) {
183+ addedFilepaths.add((snippet as AutocompleteCodeSnippet).filepath);
184+ }
185+
185186 remainingTokenCount -= snippetSize;
186187 } else {
187- break; // Out of tokens
188+ continue; // Not enough tokens, try again with next snippet
188189 }
189190 }
190191 }
modified core/autocomplete/templating/formatOpenedFilesContext.ts +12 −1
@@ -2,6 +2,7 @@
22 import {
33 AutocompleteCodeSnippet,
44 AutocompleteSnippet,
5+ AutocompleteSnippetType,
56 } from "../snippets/types";
67 import { HelperVars } from "../util/HelperVars";
78
@@ -20,13 +21,23 @@
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;
modified core/autocomplete/templating/validation.ts +9 −0
@@ -1,5 +1,6 @@
11 import {
22 AutocompleteClipboardSnippet,
3+ AutocompleteCodeSnippet,
34 AutocompleteSnippet,
45 AutocompleteSnippetType,
56 } from "../snippets/types";
@@ -25,5 +26,13 @@
2526 return isValidClipboardSnippet(snippet);
2627 }
2728
29+ if (
30+ (snippet as AutocompleteCodeSnippet).filepath?.startsWith(
31+ "output:extension-output-Continue.continue",
32+ )
33+ ) {
34+ return false;
35+ }
36+
2837 return true;
2938 };