Skip to content

Commit 71e5c02

Browse files
authored
Merge pull request #3428 from Kilo-Org/feature/cache-failed-ghost-lookups
feat: cache failed Ghost autocomplete lookups to prevent redundant LL…
2 parents d7ae72c + b3c0e10 commit 71e5c02

File tree

3 files changed

+361
-208
lines changed

3 files changed

+361
-208
lines changed

.changeset/good-horses-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"kilo-code": patch
3+
---
4+
5+
Do less requests for autocomplete when no completion could be found

src/services/ghost/classic-auto-complete/GhostInlineCompletionProvider.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ export function findMatchingSuggestion(
4040
return fillInAtCursor.text
4141
}
4242

43-
// If no exact match, check for partial typing
43+
// If no exact match, but suggestion is available, check for partial typing
4444
// The user may have started typing the suggested text
45-
if (prefix.startsWith(fillInAtCursor.prefix) && suffix === fillInAtCursor.suffix) {
45+
if (
46+
fillInAtCursor.text !== "" &&
47+
prefix.startsWith(fillInAtCursor.prefix) &&
48+
suffix === fillInAtCursor.suffix
49+
) {
4650
// Extract what the user has typed between the original prefix and current position
4751
const typedContent = prefix.substring(fillInAtCursor.prefix.length)
4852

@@ -91,23 +95,7 @@ export class GhostInlineCompletionProvider implements vscode.InlineCompletionIte
9195
this.autoTriggerStrategy = new AutoTriggerStrategy()
9296
}
9397

94-
/**
95-
* Check if a cached suggestion is available for the given prefix and suffix
96-
* @param prefix - The text before the cursor position
97-
* @param suffix - The text after the cursor position
98-
* @returns True if a matching suggestion exists, false otherwise
99-
*/
100-
public cachedSuggestionAvailable(prefix: string, suffix: string): boolean {
101-
return findMatchingSuggestion(prefix, suffix, this.suggestionsHistory) !== null
102-
}
103-
104-
public updateSuggestions(suggestions: GhostSuggestionsState): void {
105-
const fillInAtCursor = suggestions.getFillInAtCursor()
106-
107-
if (!fillInAtCursor) {
108-
return
109-
}
110-
98+
public updateSuggestions(fillInAtCursor: FillInAtCursorSuggestion): void {
11199
const isDuplicate = this.suggestionsHistory.some(
112100
(existing) =>
113101
existing.text === fillInAtCursor.text &&
@@ -143,9 +131,10 @@ export class GhostInlineCompletionProvider implements vscode.InlineCompletionIte
143131
const { prefix, suffix } = extractPrefixSuffix(context.document, position)
144132
const languageId = context.document.languageId
145133

146-
// Check cache before making API call
147-
if (this.cachedSuggestionAvailable(prefix, suffix)) {
148-
// Return empty result if cached suggestion is available
134+
// Check cache before making API call (includes both successful and failed lookups)
135+
const cachedResult = findMatchingSuggestion(prefix, suffix, this.suggestionsHistory)
136+
if (cachedResult !== null) {
137+
// Return empty result if we have a cached entry (either success or failure)
149138
return {
150139
suggestions: new GhostSuggestionsState(),
151140
cost: 0,
@@ -254,6 +243,10 @@ export class GhostInlineCompletionProvider implements vscode.InlineCompletionIte
254243
const matchingText = findMatchingSuggestion(prefix, suffix, this.suggestionsHistory)
255244

256245
if (matchingText !== null) {
246+
if (matchingText === "") {
247+
return []
248+
}
249+
257250
const item: vscode.InlineCompletionItem = {
258251
insertText: matchingText,
259252
range: new vscode.Range(position, position),
@@ -278,8 +271,7 @@ export class GhostInlineCompletionProvider implements vscode.InlineCompletionIte
278271
// Hide cursor animation after generation
279272
this.cursorAnimation.hide()
280273

281-
// Track costs
282-
if (this.costTrackingCallback) {
274+
if (this.costTrackingCallback && result.cost > 0) {
283275
this.costTrackingCallback(
284276
result.cost,
285277
result.inputTokens,
@@ -289,17 +281,20 @@ export class GhostInlineCompletionProvider implements vscode.InlineCompletionIte
289281
)
290282
}
291283

292-
// Update suggestions history
293-
this.updateSuggestions(result.suggestions)
294-
295-
// Return the new suggestion if available
296284
const fillInAtCursor = result.suggestions.getFillInAtCursor()
285+
297286
if (fillInAtCursor) {
287+
this.updateSuggestions(fillInAtCursor)
298288
const item: vscode.InlineCompletionItem = {
299289
insertText: fillInAtCursor.text,
300290
range: new vscode.Range(position, position),
301291
}
302292
return [item]
293+
} else {
294+
// Store empty suggestion to prevent re-requesting
295+
this.updateSuggestions({ text: "", prefix, suffix })
296+
297+
return []
303298
}
304299
} catch (error) {
305300
this.cursorAnimation.hide()

0 commit comments

Comments
 (0)