@@ -20,7 +20,7 @@ import AutocompleteLruCache from "./util/AutocompleteLruCache.js";
2020import { HelperVars } from "./util/HelperVars.js" ;
2121import { AutocompleteInput , AutocompleteOutcome } from "./util/types.js" ;
2222
23- const autocompleteCache = AutocompleteLruCache . get ( ) ;
23+ const autocompleteCachePromise = AutocompleteLruCache . get ( ) ;
2424
2525// Errors that can be expected on occasion even during normal functioning should not be shown.
2626// Not worth disrupting the user to tell them that a single autocomplete request didn't go through
@@ -31,7 +31,7 @@ const ERRORS_TO_IGNORE = [
3131] ;
3232
3333export class CompletionProvider {
34- private autocompleteCache = AutocompleteLruCache . get ( ) ;
34+ private autocompleteCache ?: AutocompleteLruCache ;
3535 public errorsShown : Set < string > = new Set ( ) ;
3636 private bracketMatchingService = new BracketMatchingService ( ) ;
3737 private debouncer = new AutocompleteDebouncer ( ) ;
@@ -48,6 +48,22 @@ export class CompletionProvider {
4848 ) {
4949 this . completionStreamer = new CompletionStreamer ( this . onError . bind ( this ) ) ;
5050 this . contextRetrievalService = new ContextRetrievalService ( this . ide ) ;
51+ void this . initCache ( ) ;
52+ }
53+
54+ private async initCache ( ) {
55+ try {
56+ this . autocompleteCache = await autocompleteCachePromise ;
57+ } catch ( e ) {
58+ console . error ( "Failed to initialize autocomplete cache:" , e ) ;
59+ }
60+ }
61+
62+ private async getCache ( ) : Promise < AutocompleteLruCache > {
63+ if ( ! this . autocompleteCache ) {
64+ this . autocompleteCache = await autocompleteCachePromise ;
65+ }
66+ return this . autocompleteCache ;
5167 }
5268
5369 private async _prepareLlm ( ) : Promise < ILLM | undefined > {
@@ -201,14 +217,12 @@ export class CompletionProvider {
201217
202218 // Completion
203219 let completion : string | undefined = "" ;
204-
205- const cache = await autocompleteCache ;
220+ const cache = await this . getCache ( ) ;
206221 const cachedCompletion = helper . options . useCache
207222 ? await cache . get ( helper . prunedPrefix )
208223 : undefined ;
209224 let cacheHit = false ;
210225 if ( cachedCompletion ) {
211- // Cache
212226 cacheHit = true ;
213227 completion = cachedCompletion ;
214228 } else {
@@ -277,16 +291,12 @@ export class CompletionProvider {
277291 outcome . enabledStaticContextualization = true ;
278292 }
279293
280- //////////
281-
282- // Save to cache
283294 if ( ! outcome . cacheHit && helper . options . useCache ) {
284- ( await this . autocompleteCache )
295+ void cache
285296 . put ( outcome . prefix , outcome . completion )
286297 . catch ( ( e ) => console . warn ( `Failed to save to cache: ${ e . message } ` ) ) ;
287298 }
288299
289- // When using the JetBrains extension, Mark as displayed
290300 const ideType = ( await this . ide . getIdeInfo ( ) ) . ideType ;
291301 if ( ideType === "jetbrains" ) {
292302 this . markDisplayed ( input . completionId , outcome ) ;
@@ -299,4 +309,10 @@ export class CompletionProvider {
299309 this . loggingService . deleteAbortController ( input . completionId ) ;
300310 }
301311 }
312+
313+ public async dispose ( ) {
314+ if ( this . autocompleteCache ) {
315+ await this . autocompleteCache . close ( ) ;
316+ }
317+ }
302318}
0 commit comments