Skip to content

Simple Context Deduplication for Autocomplete #6055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions core/autocomplete/templating/filtering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SnippetPayload } from "../snippets";
import {
AutocompleteCodeSnippet,
AutocompleteSnippet,
AutocompleteSnippetType,
} from "../snippets/types";
import { HelperVars } from "../util/HelperVars";
import { formatOpenedFilesContext } from "./formatOpenedFilesContext";
Expand Down Expand Up @@ -130,21 +131,16 @@ export const getSnippets = (
const finalSnippets = [];
let remainingTokenCount = getRemainingTokenCount(helper);

// tracks already added filepaths for deduplication
const addedFilepaths = new Set<string>();

// Process snippets in priority order
for (const { key } of snippetOrder) {
// Special handling for recentlyOpenedFiles
if (key === "recentlyOpenedFiles" && helper.options.useRecentlyOpened) {
const recentlyOpenedFilesSnippets =
payload.recentlyOpenedFileSnippets.filter(
(snippet) =>
!(snippet as AutocompleteCodeSnippet).filepath?.startsWith(
"output:extension-output-Continue.continue",
),
);

// Custom trimming
const processedSnippets = formatOpenedFilesContext(
recentlyOpenedFilesSnippets,
payload.recentlyOpenedFileSnippets,
remainingTokenCount,
helper,
finalSnippets,
Expand All @@ -160,18 +156,18 @@ export const getSnippets = (

if (remainingTokenCount >= snippetSize) {
finalSnippets.push(snippet);
addedFilepaths.add(snippet.filepath);
remainingTokenCount -= snippetSize;
} else {
break; // Out of tokens
continue; // Not enough tokens, try again with next snippet
}
}
} else {
// Normal processing for other snippet types
const snippetsToProcess = snippets[key].filter(
(snippet) =>
!(snippet as AutocompleteCodeSnippet).filepath?.startsWith(
"output:extension-output-Continue.continue",
),
snippet.type !== AutocompleteSnippetType.Code ||
!addedFilepaths.has(snippet.filepath),
);

for (const snippet of snippetsToProcess) {
Expand All @@ -182,9 +178,14 @@ export const getSnippets = (

if (remainingTokenCount >= snippetSize) {
finalSnippets.push(snippet);

if ((snippet as AutocompleteCodeSnippet).filepath) {
addedFilepaths.add((snippet as AutocompleteCodeSnippet).filepath);
}

remainingTokenCount -= snippetSize;
} else {
break; // Out of tokens
continue; // Not enough tokens, try again with next snippet
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion core/autocomplete/templating/formatOpenedFilesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { countTokens, pruneStringFromBottom } from "../../llm/countTokens";
import {
AutocompleteCodeSnippet,
AutocompleteSnippet,
AutocompleteSnippetType,
} from "../snippets/types";
import { HelperVars } from "../util/HelperVars";

Expand All @@ -20,13 +21,23 @@ export function formatOpenedFilesContext(
recentlyOpenedFilesSnippets: AutocompleteCodeSnippet[],
remainingTokenCount: number,
helper: HelperVars,
alreadyAddedSnippets: AutocompleteSnippet[], // TODO use this to deduplicate context
alreadyAddedSnippets: AutocompleteSnippet[],
TOKEN_BUFFER: number,
): AutocompleteCodeSnippet[] {
if (recentlyOpenedFilesSnippets.length === 0) {
return [];
}

// deduplication; if a snippet is already added, don't include it here
for (const snippet of alreadyAddedSnippets) {
if (snippet.type !== AutocompleteSnippetType.Code) {
continue;
}
recentlyOpenedFilesSnippets = recentlyOpenedFilesSnippets.filter(
(s) => s.filepath !== snippet.filepath,
);
}

// Calculate how many full snippets would fit within the remaining token count
let numSnippetsThatFit = 0;
let totalTokens = 0;
Expand Down
9 changes: 9 additions & 0 deletions core/autocomplete/templating/validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AutocompleteClipboardSnippet,
AutocompleteCodeSnippet,
AutocompleteSnippet,
AutocompleteSnippetType,
} from "../snippets/types";
Expand All @@ -25,5 +26,13 @@ export const isValidSnippet = (snippet: AutocompleteSnippet): boolean => {
return isValidClipboardSnippet(snippet);
}

if (
(snippet as AutocompleteCodeSnippet).filepath?.startsWith(
"output:extension-output-Continue.continue",
)
) {
return false;
}

return true;
};
Loading