From 398730364f591de0c8b8e5bfd7de1dc69950b23b Mon Sep 17 00:00:00 2001 From: adarshramiyer Date: Mon, 9 Jun 2025 15:39:21 -0700 Subject: [PATCH] implemented very simple priority-based deduplication --- core/autocomplete/templating/filtering.ts | 17 ++++++++++++++--- .../templating/formatOpenedFilesContext.ts | 13 ++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/core/autocomplete/templating/filtering.ts b/core/autocomplete/templating/filtering.ts index 6d321aba7..6b5c95fab 100644 --- a/core/autocomplete/templating/filtering.ts +++ b/core/autocomplete/templating/filtering.ts @@ -130,6 +130,9 @@ export const getSnippets = ( const finalSnippets = []; let remainingTokenCount = getRemainingTokenCount(helper); + // tracks already added filepaths for deduplication + const addedFilepaths = new Set(); + // Process snippets in priority order for (const { key } of snippetOrder) { // Special handling for recentlyOpenedFiles @@ -160,9 +163,10 @@ 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 { @@ -171,7 +175,9 @@ export const getSnippets = ( (snippet) => !(snippet as AutocompleteCodeSnippet).filepath?.startsWith( "output:extension-output-Continue.continue", - ), + ) && + ((snippet as AutocompleteCodeSnippet).filepath === undefined || + !addedFilepaths.has((snippet as AutocompleteCodeSnippet).filepath)), ); for (const snippet of snippetsToProcess) { @@ -182,9 +188,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 } } } diff --git a/core/autocomplete/templating/formatOpenedFilesContext.ts b/core/autocomplete/templating/formatOpenedFilesContext.ts index c114e63d1..68f9ae907 100644 --- a/core/autocomplete/templating/formatOpenedFilesContext.ts +++ b/core/autocomplete/templating/formatOpenedFilesContext.ts @@ -2,6 +2,7 @@ import { countTokens, pruneStringFromBottom } from "../../llm/countTokens"; import { AutocompleteCodeSnippet, AutocompleteSnippet, + AutocompleteSnippetType, } from "../snippets/types"; import { HelperVars } from "../util/HelperVars"; @@ -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;