From 3e1eca6c885f744c28c5f070512f784ca5f01f2d Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 31 Oct 2024 16:17:25 -0700 Subject: [PATCH] refactor multi-line decision --- core/autocomplete/CompletionProvider.ts | 12 ++- .../classification/shouldCompleteMultiline.ts | 77 ++++++------------- core/autocomplete/constructPrompt.ts | 8 +- core/autocomplete/context/ranking/index.ts | 3 +- core/autocomplete/templating/index.ts | 12 +-- 5 files changed, 35 insertions(+), 77 deletions(-) diff --git a/core/autocomplete/CompletionProvider.ts b/core/autocomplete/CompletionProvider.ts index 50629e30e..ab8df6629 100644 --- a/core/autocomplete/CompletionProvider.ts +++ b/core/autocomplete/CompletionProvider.ts @@ -173,12 +173,11 @@ export class CompletionProvider { // or they might separately track recently edited ranges) const extraSnippets = await this._getExtraSnippets(helper); - let { prefix, suffix, completeMultiline, snippets } = - await constructAutocompletePrompt( - helper, - extraSnippets, - this.contextRetrievalService, - ); + let { prefix, suffix, snippets } = await constructAutocompletePrompt( + helper, + extraSnippets, + this.contextRetrievalService, + ); // If prefix is manually passed if (helper.input.manuallyPassPrefix) { @@ -191,7 +190,6 @@ export class CompletionProvider { suffix, snippets, await this.ide.getWorkspaceDirs(), - completeMultiline, helper, ); diff --git a/core/autocomplete/classification/shouldCompleteMultiline.ts b/core/autocomplete/classification/shouldCompleteMultiline.ts index d8df8aeb0..1c999fe01 100644 --- a/core/autocomplete/classification/shouldCompleteMultiline.ts +++ b/core/autocomplete/classification/shouldCompleteMultiline.ts @@ -1,17 +1,33 @@ -import { TabAutocompleteOptions } from "@continuedev/config-types"; import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo"; import { HelperVars } from "../HelperVars"; -import { AutocompleteInput } from "../types"; - -const BLOCK_TYPES = ["body", "statement_block"]; function isMidlineCompletion(prefix: string, suffix: string): boolean { return !suffix.startsWith("\n"); } -export async function shouldCompleteMultiline( +function shouldCompleteMultilineBasedOnLanguage( + language: AutocompleteLanguageInfo, + prefix: string, + suffix: string, +) { + let langMultilineDecision = language.useMultiline?.({ prefix, suffix }); + return langMultilineDecision; +} + +export function decideMultilineEarly( helper: HelperVars, -): Promise { + prefix: string, + suffix: string, +) { + switch (helper.options.multilineCompletions) { + case "always": + return true; + case "never": + return false; + default: + break; + } + // Don't complete multi-line if you are mid-line if (isMidlineCompletion(helper.fullPrefix, helper.fullSuffix)) { return false; @@ -28,56 +44,11 @@ export async function shouldCompleteMultiline( return false; } - return true; -} - -function shouldCompleteMultilineBasedOnLanguage( - language: AutocompleteLanguageInfo, - prefix: string, - suffix: string, -) { - let langMultilineDecision = language.useMultiline?.({ prefix, suffix }); - return langMultilineDecision; -} - -function shouldCompleteMultilineBasedOnSelectedCompletion( - selectedCompletionInfo: AutocompleteInput["selectedCompletionInfo"], - multilineCompletions: TabAutocompleteOptions["multilineCompletions"], - completeMultiline: boolean, -) { - return ( - !selectedCompletionInfo && // Only ever single-line if using intellisense selected value - multilineCompletions !== "never" && - (multilineCompletions === "always" || completeMultiline) - ); -} - -export function decideMultilineEarly({ - language, - prefix, - suffix, - selectedCompletionInfo, - multilineCompletions, - completeMultiline, -}: { - language: AutocompleteLanguageInfo; - prefix: string; - suffix: string; - selectedCompletionInfo: AutocompleteInput["selectedCompletionInfo"]; - multilineCompletions: TabAutocompleteOptions["multilineCompletions"]; - completeMultiline: boolean; -}) { - if (shouldCompleteMultilineBasedOnLanguage(language, prefix, suffix)) { + if (shouldCompleteMultilineBasedOnLanguage(helper.lang, prefix, suffix)) { return true; } - if ( - shouldCompleteMultilineBasedOnSelectedCompletion( - selectedCompletionInfo, - multilineCompletions, - completeMultiline, - ) - ) { + if (helper.input.selectedCompletionInfo) { return true; } diff --git a/core/autocomplete/constructPrompt.ts b/core/autocomplete/constructPrompt.ts index 7c7d898e6..62c28543e 100644 --- a/core/autocomplete/constructPrompt.ts +++ b/core/autocomplete/constructPrompt.ts @@ -4,7 +4,6 @@ import { pruneLinesFromBottom, pruneLinesFromTop, } from "../llm/countTokens.js"; -import { shouldCompleteMultiline } from "./classification/shouldCompleteMultiline.js"; import { ContextRetrievalService } from "./context/ContextRetrievalService.js"; import { fillPromptWithSnippets, @@ -84,8 +83,6 @@ export async function constructAutocompletePrompt( ): Promise<{ prefix: string; suffix: string; - useFim: boolean; - completeMultiline: boolean; snippets: AutocompleteSnippet[]; }> { // Prune prefix/suffix based on token budgets @@ -104,13 +101,14 @@ export async function constructAutocompletePrompt( const scoredSnippets = rankAndOrderSnippets(snippets, helper); + // This might be redundant with filterSnippetsAlreadyInCaretWindow let finalSnippets = removeRangeFromSnippets( scoredSnippets, helper.filepath.split("://").slice(-1)[0], getRangeOfPrefixAndSuffixWithBuffer(prunedPrefix, prunedSuffix, helper.pos), ); - // Filter snippets for those with best scores (must be above threshold) + // Filter snippets below similarity threshold finalSnippets = finalSnippets.filter( (snippet) => snippet.score >= helper.options.recentlyEditedSimilarityThreshold, @@ -127,8 +125,6 @@ export async function constructAutocompletePrompt( return { prefix: prunedPrefix, suffix: prunedSuffix, - useFim: true, - completeMultiline: await shouldCompleteMultiline(helper), snippets, }; } diff --git a/core/autocomplete/context/ranking/index.ts b/core/autocomplete/context/ranking/index.ts index 58e97cc04..aba29d7aa 100644 --- a/core/autocomplete/context/ranking/index.ts +++ b/core/autocomplete/context/ranking/index.ts @@ -130,7 +130,8 @@ function mergeOverlappingRangeContents( } /** - * Fill the allowed space with snippets + * Fill the allowed space with snippets. + * It is assumed that the snippets are sorted by score. */ export function fillPromptWithSnippets( snippets: Required[], diff --git a/core/autocomplete/templating/index.ts b/core/autocomplete/templating/index.ts index ad9dc7391..dea0f5885 100644 --- a/core/autocomplete/templating/index.ts +++ b/core/autocomplete/templating/index.ts @@ -1,6 +1,6 @@ import { CompletionOptions } from "../.."; import { getBasename, getLastNPathParts } from "../../util"; -import { decideMultilineEarly } from "../classification/shouldCompleteMultiline"; +import { decideMultilineEarly as shouldCompleteMultiline } from "../classification/shouldCompleteMultiline"; import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo"; import { AutocompleteSnippet } from "../context/ranking"; import { HelperVars } from "../HelperVars"; @@ -65,7 +65,6 @@ export function renderPrompt( suffix: string, snippets: AutocompleteSnippet[], workspaceDirs: string[], - completeMultiline: boolean, helper: HelperVars, ): [string, Partial | undefined, boolean] { let { @@ -115,14 +114,7 @@ export function renderPrompt( const multiline = !helper.options.transform || - decideMultilineEarly({ - multilineCompletions: helper.options.multilineCompletions, - language: helper.lang, - selectedCompletionInfo: helper.input.selectedCompletionInfo, - prefix, - suffix, - completeMultiline, - }); + shouldCompleteMultiline(helper, prefix, suffix); const stopTokens = getStopTokens( completionOptions,