Skip to content

Commit 048c673

Browse files
committed
do not request if we have a completion
1 parent 5655831 commit 048c673

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/services/ghost/GhostInlineCompletionProvider.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ export function findMatchingSuggestion(
4545
export class GhostInlineCompletionProvider implements vscode.InlineCompletionItemProvider {
4646
private suggestionsHistory: FillInAtCursorSuggestion[] = []
4747

48+
/**
49+
* Check if a cached suggestion is available for the given prefix and suffix
50+
* @param prefix - The text before the cursor position
51+
* @param suffix - The text after the cursor position
52+
* @returns True if a matching suggestion exists, false otherwise
53+
*/
54+
public cachedSuggestionAvailable(prefix: string, suffix: string): boolean {
55+
return findMatchingSuggestion(prefix, suffix, this.suggestionsHistory) !== null
56+
}
57+
4858
public updateSuggestions(suggestions: GhostSuggestionsState): void {
4959
const fillInAtCursor = suggestions.getFillInAtCursor()
5060

src/services/ghost/GhostProvider.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ export class GhostProvider {
293293
const { prefix, suffix } = extractPrefixSuffix(context.document, position)
294294
const languageId = context.document.languageId
295295

296+
// Check cache before making API call
297+
if (this.inlineCompletionProvider.cachedSuggestionAvailable(prefix, suffix)) {
298+
return
299+
}
300+
296301
const { systemPrompt, userPrompt } = this.autoTriggerStrategy.getPrompts(
297302
autocompleteInput,
298303
prefix,

src/services/ghost/__tests__/GhostInlineCompletionProvider.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,69 @@ describe("GhostInlineCompletionProvider", () => {
728728
})
729729
})
730730

731+
describe("cachedSuggestionAvailable", () => {
732+
it("should return true when prefix and suffix match", () => {
733+
const suggestions = new GhostSuggestionsState()
734+
suggestions.setFillInAtCursor({
735+
text: "console.log('cached');",
736+
prefix: "const x = 1",
737+
suffix: "\nconst y = 2",
738+
})
739+
provider.updateSuggestions(suggestions)
740+
741+
const result = provider.cachedSuggestionAvailable("const x = 1", "\nconst y = 2")
742+
expect(result).toBe(true)
743+
})
744+
745+
it("should return false when no matching suggestion exists", () => {
746+
const suggestions = new GhostSuggestionsState()
747+
suggestions.setFillInAtCursor({
748+
text: "console.log('test');",
749+
prefix: "const x = 1",
750+
suffix: "\nconst y = 2",
751+
})
752+
provider.updateSuggestions(suggestions)
753+
754+
const result = provider.cachedSuggestionAvailable("different prefix", "different suffix")
755+
expect(result).toBe(false)
756+
})
757+
758+
it("should return true for partial typing", () => {
759+
const suggestions = new GhostSuggestionsState()
760+
suggestions.setFillInAtCursor({
761+
text: "console.log('test');",
762+
prefix: "const x = 1",
763+
suffix: "\nconst y = 2",
764+
})
765+
provider.updateSuggestions(suggestions)
766+
767+
// User typed "cons" after the prefix
768+
const result = provider.cachedSuggestionAvailable("const x = 1cons", "\nconst y = 2")
769+
expect(result).toBe(true)
770+
})
771+
772+
it("should return true when most recent matching suggestion exists", () => {
773+
const suggestions1 = new GhostSuggestionsState()
774+
suggestions1.setFillInAtCursor({
775+
text: "first",
776+
prefix: "const x = 1",
777+
suffix: "\nconst y = 2",
778+
})
779+
provider.updateSuggestions(suggestions1)
780+
781+
const suggestions2 = new GhostSuggestionsState()
782+
suggestions2.setFillInAtCursor({
783+
text: "second",
784+
prefix: "const x = 1",
785+
suffix: "\nconst y = 2",
786+
})
787+
provider.updateSuggestions(suggestions2)
788+
789+
const result = provider.cachedSuggestionAvailable("const x = 1", "\nconst y = 2")
790+
expect(result).toBe(true)
791+
})
792+
})
793+
731794
describe("updateSuggestions", () => {
732795
it("should accept new suggestions state", () => {
733796
const suggestions = new GhostSuggestionsState()

0 commit comments

Comments
 (0)