From 4b15d00802fbcc7376a5edf194a09435efac2eac Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 31 Oct 2024 14:15:00 -0700 Subject: [PATCH] more refactoring --- core/autocomplete/CompletionProvider.ts | 277 +++++++----------- .../generation/CompletionStreamer.ts | 67 +++++ 2 files changed, 178 insertions(+), 166 deletions(-) create mode 100644 core/autocomplete/generation/CompletionStreamer.ts diff --git a/core/autocomplete/CompletionProvider.ts b/core/autocomplete/CompletionProvider.ts index 5ec075c6d..665e88513 100644 --- a/core/autocomplete/CompletionProvider.ts +++ b/core/autocomplete/CompletionProvider.ts @@ -11,7 +11,6 @@ import { AutocompleteLanguageInfo } from "./constants/AutocompleteLanguageInfo.j import { constructAutocompletePrompt } from "./constructPrompt.js"; import { AutocompleteSnippet } from "./context/ranking/index.js"; import { RootPathContextService } from "./context/RootPathContextService.js"; -import { GeneratorReuseManager } from "./generation/GeneratorReuseManager.js"; import { postprocessCompletion } from "./postprocessing/index.js"; // @prettier-ignore import { TRIAL_FIM_MODEL } from "../config/onboarding.js"; @@ -20,7 +19,7 @@ import { AutocompleteDebouncer } from "./AutocompleteDebouncer.js"; import AutocompleteLruCache from "./AutocompleteLruCache.js"; import { ImportDefinitionsService } from "./context/ImportDefinitionsService.js"; import { BracketMatchingService } from "./filtering/BracketMatchingService.js"; -import { StreamTransformPipeline } from "./filtering/streamTransforms/StreamTransformPipeline.js"; +import { CompletionStreamer } from "./generation/CompletionStreamer.js"; import { HelperVars } from "./HelperVars.js"; import { shouldPrefilter } from "./prefiltering/index.js"; import { constructInitialPrefixSuffix } from "./templating/constructPrefixSuffix.js"; @@ -47,13 +46,11 @@ export type GetLspDefinitionsFunction = ( export class CompletionProvider { private importDefinitionsService: ImportDefinitionsService; private rootPathContextService: RootPathContextService; - private generatorReuseManager: GeneratorReuseManager; private autocompleteCache = AutocompleteLruCache.get(); public errorsShown: Set = new Set(); private bracketMatchingService = new BracketMatchingService(); private debouncer = new AutocompleteDebouncer(); - private streamTransformPipeline = new StreamTransformPipeline(); - // private nearbyDefinitionsService = new NearbyDefinitionsService(); + private completionStreamer: CompletionStreamer; constructor( private readonly configHandler: ConfigHandler, @@ -62,9 +59,7 @@ export class CompletionProvider { private readonly _onError: (e: any) => void, private readonly getDefinitionsFromLsp: GetLspDefinitionsFunction, ) { - this.generatorReuseManager = new GeneratorReuseManager( - this.onError.bind(this), - ); + this.completionStreamer = new CompletionStreamer(this.onError.bind(this)); this.importDefinitionsService = new ImportDefinitionsService(this.ide); this.rootPathContextService = new RootPathContextService( this.importDefinitionsService, @@ -103,7 +98,6 @@ export class CompletionProvider { } private onError(e: any) { - console.warn("Error generating autocompletion: ", e); if ( ERRORS_TO_IGNORE.some((err) => typeof e === "string" ? e.includes(err) : e?.message?.includes(err), @@ -111,6 +105,8 @@ export class CompletionProvider { ) { return; } + + console.warn("Error generating autocompletion: ", e); if (!this.errorsShown.has(e.message)) { this.errorsShown.add(e.message); this._onError(e); @@ -197,6 +193,8 @@ export class CompletionProvider { token: AbortSignal | undefined, ): Promise { try { + const startTime = Date.now(); + const llm = await this._prepareLlm(); if (!llm) { return undefined; @@ -214,8 +212,6 @@ export class CompletionProvider { return undefined; } - // Get completion - // Create abort signal if not given if (!token) { const controller = new AbortController(); @@ -223,12 +219,114 @@ export class CompletionProvider { this._abortControllers.set(input.completionId, controller); } - const outcome = await this.getTabCompletion(token, llm, helper); + ////////// - if (!outcome?.completion) { + // Construct full prefix/suffix (a few edge cases handled in here) + const { prefix: fullPrefix, suffix: fullSuffix } = + await constructInitialPrefixSuffix(helper.input, this.ide); + + // Some IDEs might have special ways of finding snippets (e.g. JetBrains and VS Code have different "LSP-equivalent" systems, + // or they might separately track recently edited ranges) + const extraSnippets = await this._getExtraSnippets( + helper, + fullPrefix, + fullSuffix, + ); + + let { prefix, suffix, completeMultiline, snippets } = + await constructAutocompletePrompt( + fullPrefix, + fullSuffix, + helper, + extraSnippets, + this.importDefinitionsService, + this.rootPathContextService, + ); + + // If prefix is manually passed + if (helper.input.manuallyPassPrefix) { + prefix = helper.input.manuallyPassPrefix; + suffix = ""; + } + + const [prompt, completionOptions, multiline] = renderPrompt( + prefix, + suffix, + snippets, + await this.ide.getWorkspaceDirs(), + completeMultiline, + helper, + ); + + // Completion + let completion: string | undefined = ""; + + const cache = await autocompleteCache; + const cachedCompletion = helper.options.useCache + ? await cache.get(prefix) + : undefined; + let cacheHit = false; + if (cachedCompletion) { + // Cache + cacheHit = true; + completion = cachedCompletion; + } else { + const completionStream = + this.completionStreamer.streamCompletionWithFilters( + token, + llm, + prefix, + suffix, + prompt, + multiline, + completionOptions, + helper, + ); + + for await (const update of completionStream) { + completion += update; + } + + // Don't postprocess if aborted + if (token.aborted) { + return undefined; + } + + const processedCompletion = helper.options.transform + ? postprocessCompletion({ + completion, + prefix, + suffix, + llm, + }) + : completion; + + completion = processedCompletion; + } + + if (!completion) { return undefined; } + const outcome: AutocompleteOutcome = { + time: Date.now() - startTime, + completion, + prefix, + suffix, + prompt, + modelProvider: llm.providerName, + modelName: llm.model, + completionOptions, + cacheHit, + filepath: helper.filepath, + completionId: helper.input.completionId, + gitRepo: await this.ide.getRepoName(helper.filepath), + uniqueId: await this.ide.getUniqueId(), + ...helper.options, + }; + + ////////// + // Do some stuff later so as not to block return. Latency matters const completionToCache = outcome.completion; setTimeout(async () => { @@ -316,157 +414,4 @@ export class CompletionProvider { return extraSnippets; } - - async getTabCompletion( - token: AbortSignal, - llm: ILLM, - helper: HelperVars, - ): Promise { - const startTime = Date.now(); - - // Construct full prefix/suffix (a few edge cases handled in here) - const { prefix: fullPrefix, suffix: fullSuffix } = - await constructInitialPrefixSuffix(helper.input, this.ide); - - // Some IDEs might have special ways of finding snippets (e.g. JetBrains and VS Code have different "LSP-equivalent" systems, - // or they might separately track recently edited ranges) - const extraSnippets = await this._getExtraSnippets( - helper, - fullPrefix, - fullSuffix, - ); - - let { prefix, suffix, completeMultiline, snippets } = - await constructAutocompletePrompt( - fullPrefix, - fullSuffix, - helper, - extraSnippets, - this.importDefinitionsService, - this.rootPathContextService, - ); - - // If prefix is manually passed - if (helper.input.manuallyPassPrefix) { - prefix = helper.input.manuallyPassPrefix; - suffix = ""; - } - - const [prompt, completionOptions, multiline] = renderPrompt( - prefix, - suffix, - snippets, - await this.ide.getWorkspaceDirs(), - completeMultiline, - helper, - ); - - // Completion - let completion: string | undefined = ""; - - const cache = await autocompleteCache; - const cachedCompletion = helper.options.useCache - ? await cache.get(prefix) - : undefined; - let cacheHit = false; - if (cachedCompletion) { - // Cache - cacheHit = true; - completion = cachedCompletion; - } else { - // Try to reuse pending requests if what the user typed matches start of completion - const generator = this.generatorReuseManager.getGenerator( - prefix, - () => - llm.supportsFim() - ? llm.streamFim(prefix, suffix, completionOptions) - : llm.streamComplete(prompt, { - ...completionOptions, - raw: true, - }), - multiline, - ); - - // Full stop means to stop the LLM's generation, instead of just truncating the displayed completion - const fullStop = () => - this.generatorReuseManager.currentGenerator?.cancel(); - - // LLM - let cancelled = false; - const generatorWithCancellation = async function* () { - for await (const update of generator) { - if (token.aborted) { - cancelled = true; - return; - } - yield update; - } - }; - - const initialGenerator = generatorWithCancellation(); - const finalGenerator = helper.options.transform - ? this.streamTransformPipeline.transform( - initialGenerator, - prefix, - suffix, - helper.filepath, - multiline, - helper.pos, - helper.fileLines, - completionOptions?.stop || [], - helper.lang, - fullStop, - ) - : initialGenerator; - - try { - for await (const update of finalGenerator) { - completion += update; - } - } catch (e: any) { - if (ERRORS_TO_IGNORE.some((err) => e.includes(err))) { - return undefined; - } - throw e; - } - - if (cancelled) { - return undefined; - } - - const processedCompletion = helper.options.transform - ? postprocessCompletion({ - completion, - prefix, - suffix, - llm, - }) - : completion; - - completion = processedCompletion; - } - - const time = Date.now() - startTime; - - if (!completion) { - return undefined; - } - - return { - time, - completion, - prefix, - suffix, - prompt, - modelProvider: llm.providerName, - modelName: llm.model, - completionOptions, - cacheHit, - filepath: helper.filepath, - completionId: helper.input.completionId, - gitRepo: await this.ide.getRepoName(helper.filepath), - uniqueId: await this.ide.getUniqueId(), - ...helper.options, - }; - } } diff --git a/core/autocomplete/generation/CompletionStreamer.ts b/core/autocomplete/generation/CompletionStreamer.ts new file mode 100644 index 000000000..803420be9 --- /dev/null +++ b/core/autocomplete/generation/CompletionStreamer.ts @@ -0,0 +1,67 @@ +import { CompletionOptions, ILLM } from "../.."; +import { StreamTransformPipeline } from "../filtering/streamTransforms/StreamTransformPipeline"; +import { HelperVars } from "../HelperVars"; +import { GeneratorReuseManager } from "./GeneratorReuseManager"; + +export class CompletionStreamer { + private streamTransformPipeline = new StreamTransformPipeline(); + private generatorReuseManager: GeneratorReuseManager; + + constructor(private readonly onError: (err: any) => void) { + this.generatorReuseManager = new GeneratorReuseManager(onError); + } + + async *streamCompletionWithFilters( + token: AbortSignal, + llm: ILLM, + prefix: string, + suffix: string, + prompt: string, + multiline: boolean, + completionOptions: Partial | undefined, + helper: HelperVars, + ) { + // Try to reuse pending requests if what the user typed matches start of completion + const generator = this.generatorReuseManager.getGenerator( + prefix, + () => + llm.supportsFim() + ? llm.streamFim(prefix, suffix, completionOptions) + : llm.streamComplete(prompt, { + ...completionOptions, + raw: true, + }), + multiline, + ); + + // Full stop means to stop the LLM's generation, instead of just truncating the displayed completion + const fullStop = () => + this.generatorReuseManager.currentGenerator?.cancel(); + + // LLM + const generatorWithCancellation = async function* () { + for await (const update of generator) { + if (token.aborted) { + return; + } + yield update; + } + }; + + const initialGenerator = generatorWithCancellation(); + const finalGenerator = helper.options.transform + ? this.streamTransformPipeline.transform( + initialGenerator, + prefix, + suffix, + helper.filepath, + multiline, + helper.pos, + helper.fileLines, + completionOptions?.stop || [], + helper.lang, + fullStop, + ) + : initialGenerator; + } +}