From ef4b32975d052f5e768613c185c8a486e8fc4a75 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 31 Oct 2024 16:41:03 -0700 Subject: [PATCH] refactor context retrieval service --- core/autocomplete/constructPrompt.ts | 2 +- .../context/ContextRetrievalService.ts | 150 +++++++++++------- 2 files changed, 90 insertions(+), 62 deletions(-) diff --git a/core/autocomplete/constructPrompt.ts b/core/autocomplete/constructPrompt.ts index 08bde361c..fdf16abad 100644 --- a/core/autocomplete/constructPrompt.ts +++ b/core/autocomplete/constructPrompt.ts @@ -88,7 +88,7 @@ export async function constructAutocompletePrompt( // Prune prefix/suffix based on token budgets const { prunedPrefix, prunedSuffix } = prunePrefixSuffix(helper); - let snippets = await contextRetrievalService.retrieve( + let snippets = await contextRetrievalService.retrieveCandidateSnippets( prunedPrefix, helper, extraSnippets, diff --git a/core/autocomplete/context/ContextRetrievalService.ts b/core/autocomplete/context/ContextRetrievalService.ts index 21e9eddf4..b8484421d 100644 --- a/core/autocomplete/context/ContextRetrievalService.ts +++ b/core/autocomplete/context/ContextRetrievalService.ts @@ -5,17 +5,6 @@ import { ImportDefinitionsService } from "./ImportDefinitionsService"; import { AutocompleteSnippet, getSymbolsForSnippet } from "./ranking"; import { RootPathContextService } from "./RootPathContextService"; -export function findMatchingRange( - recentlyEditedRanges: RecentlyEditedRange[], - linePrefix: string, -): RecentlyEditedRange | undefined { - return recentlyEditedRanges.find((recentlyEditedRange) => { - return recentlyEditedRange.lines.some((line) => - line.startsWith(linePrefix), - ); - }); -} - export class ContextRetrievalService { private importDefinitionsService: ImportDefinitionsService; private rootPathContextService: RootPathContextService; @@ -28,69 +17,108 @@ export class ContextRetrievalService { ); } - public async retrieve( + private getSnippetsFromRecentlyEditedRanges( + prunedPrefix: string, + helper: HelperVars, + ): AutocompleteSnippet[] { + if (helper.options.useRecentlyEdited === false) { + return []; + } + + const currentLinePrefix = prunedPrefix.trim().split("\n").slice(-1)[0]; + if ( + currentLinePrefix?.length > helper.options.recentLinePrefixMatchMinLength + ) { + const matchingRange = this.findMatchingRange( + helper.input.recentlyEditedRanges, + currentLinePrefix, + ); + + if (matchingRange) { + return [ + { + ...matchingRange, + contents: matchingRange.lines.join("\n"), + score: 0.8, + }, + ]; + } + } + + return []; + } + + private async getSnippetsFromImportDefinitions( + helper: HelperVars, + ): Promise { + if (helper.options.useImports === false) { + return []; + } + + const importSnippets = []; + const fileInfo = this.importDefinitionsService.get(helper.filepath); + if (fileInfo) { + const { imports } = fileInfo; + // Look for imports of any symbols around the current range + const textAroundCursor = + helper.fullPrefix.split("\n").slice(-5).join("\n") + + helper.fullSuffix.split("\n").slice(0, 3).join("\n"); + const symbols = Array.from(getSymbolsForSnippet(textAroundCursor)).filter( + (symbol) => !helper.lang.topLevelKeywords.includes(symbol), + ); + for (const symbol of symbols) { + const rifs = imports[symbol]; + if (Array.isArray(rifs)) { + importSnippets.push(...rifs); + } + } + } + return importSnippets; + } + + public async retrieveCandidateSnippets( prunedPrefix: string, helper: HelperVars, extraSnippets: AutocompleteSnippet[], ) { - // Find external snippets + if (helper.options.useOtherFiles === false) { + return []; + } + let snippets: AutocompleteSnippet[] = []; - if (helper.options.useOtherFiles) { - snippets.push(...extraSnippets); + // Snippets injected by the IDE for IDE-specific reasons + snippets.push(...extraSnippets); - if (helper.options.useRecentlyEdited) { - const currentLinePrefix = prunedPrefix.trim().split("\n").slice(-1)[0]; - if ( - currentLinePrefix?.length > - helper.options.recentLinePrefixMatchMinLength - ) { - const matchingRange = findMatchingRange( - helper.input.recentlyEditedRanges, - currentLinePrefix, - ); - if (matchingRange) { - snippets.push({ - ...matchingRange, - contents: matchingRange.lines.join("\n"), - score: 0.8, - }); - } - } - } + // If a recently edited range has a line that is a perfect match with the start of the current line + snippets.push( + ...this.getSnippetsFromRecentlyEditedRanges(prunedPrefix, helper), + ); - // Use imports - if (helper.options.useImports) { - const importSnippets = []; - const fileInfo = this.importDefinitionsService.get(helper.filepath); - if (fileInfo) { - const { imports } = fileInfo; - // Look for imports of any symbols around the current range - const textAroundCursor = - helper.fullPrefix.split("\n").slice(-5).join("\n") + - helper.fullSuffix.split("\n").slice(0, 3).join("\n"); - const symbols = Array.from( - getSymbolsForSnippet(textAroundCursor), - ).filter((symbol) => !helper.lang.topLevelKeywords.includes(symbol)); - for (const symbol of symbols) { - const rifs = imports[symbol]; - if (Array.isArray(rifs)) { - importSnippets.push(...rifs); - } - } - } - snippets.push(...importSnippets); - } + // Import definitions of any symbols in near range of the caret + snippets.push(...(await this.getSnippetsFromImportDefinitions(helper))); - if (helper.options.useRootPathContext && helper.treePath) { - const ctx = await this.rootPathContextService.getContextForPath( + // Root path context https://blog.continue.dev/root-path-context-the-secret-ingredient-in-continues-autocomplete-prompt/ + if (helper.options.useRootPathContext && helper.treePath) { + snippets.push( + ...(await this.rootPathContextService.getContextForPath( helper.filepath, helper.treePath, - ); - snippets.push(...ctx); - } + )), + ); } return snippets; } + + findMatchingRange( + recentlyEditedRanges: RecentlyEditedRange[], + linePrefix: string, + ): RecentlyEditedRange | undefined { + return recentlyEditedRanges.find((recentlyEditedRange) => { + return recentlyEditedRange.lines.some((line) => + line.startsWith(linePrefix), + ); + }); + } }