Skip to content

Commit 87b41a1

Browse files
authored
Merge pull request #3331 from Kilo-Org/mark/stateless-parser
make the autocomplete parser stateless
2 parents 5272bd1 + b8711ce commit 87b41a1

File tree

6 files changed

+234
-327
lines changed

6 files changed

+234
-327
lines changed

src/services/ghost/GhostProvider.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import crypto from "crypto"
22
import * as vscode from "vscode"
33
import { t } from "../../i18n"
44
import { GhostDocumentStore } from "./GhostDocumentStore"
5-
import { GhostStreamingParser } from "./classic-auto-complete/GhostStreamingParser"
5+
import { parseGhostResponse } from "./classic-auto-complete/GhostStreamingParser"
66
import { AutoTriggerStrategy } from "./classic-auto-complete/AutoTriggerStrategy"
77
import { GhostModel } from "./GhostModel"
88
import { GhostSuggestionContext, contextToAutocompleteInput, extractPrefixSuffix } from "./types"
@@ -24,7 +24,6 @@ export class GhostProvider {
2424
private static instance: GhostProvider | null = null
2525
private documentStore: GhostDocumentStore
2626
private model: GhostModel
27-
private streamingParser: GhostStreamingParser
2827
private autoTriggerStrategy: AutoTriggerStrategy
2928
private suggestions: GhostSuggestionsState = new GhostSuggestionsState()
3029
private cline: ClineProvider
@@ -58,7 +57,6 @@ export class GhostProvider {
5857

5958
// Register Internal Components
6059
this.documentStore = new GhostDocumentStore()
61-
this.streamingParser = new GhostStreamingParser()
6260
this.autoTriggerStrategy = new AutoTriggerStrategy()
6361
this.providerSettingsManager = new ProviderSettingsManager(context)
6462
this.model = new GhostModel()
@@ -313,9 +311,6 @@ export class GhostProvider {
313311
await this.load()
314312
}
315313

316-
// Initialize the streaming parser
317-
this.streamingParser.initialize(context)
318-
319314
let hasShownFirstSuggestion = false
320315
let cost = 0
321316
let inputTokens = 0
@@ -368,7 +363,7 @@ export class GhostProvider {
368363
}
369364

370365
// Finish the streaming parser to apply sanitization if needed
371-
const finalParseResult = this.streamingParser.parseResponse(response, prefix, suffix)
366+
const finalParseResult = parseGhostResponse(response, prefix, suffix, context.document, context.range)
372367

373368
if (finalParseResult.suggestions.getFillInAtCursor()) {
374369
console.info("Final suggestion:", finalParseResult.suggestions.getFillInAtCursor())

src/services/ghost/__tests__/GhostProvider.spec.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, expect, beforeEach, vi } from "vitest"
22
import { MockWorkspace } from "./MockWorkspace"
33
import * as vscode from "vscode"
4-
import { GhostStreamingParser } from "../classic-auto-complete/GhostStreamingParser"
4+
import { parseGhostResponse } from "../classic-auto-complete/GhostStreamingParser"
55
import { GhostSuggestionContext } from "../types"
66

77
vi.mock("vscode", () => ({
@@ -65,11 +65,9 @@ vi.mock("vscode", () => ({
6565

6666
describe("GhostProvider", () => {
6767
let mockWorkspace: MockWorkspace
68-
let streamingParser: GhostStreamingParser
6968

7069
beforeEach(() => {
7170
vi.clearAllMocks()
72-
streamingParser = new GhostStreamingParser()
7371
mockWorkspace = new MockWorkspace()
7472

7573
vi.mocked(vscode.workspace.openTextDocument).mockImplementation(async (uri: any) => {
@@ -107,8 +105,7 @@ describe("GhostProvider", () => {
107105
const { context } = await setupTestDocument("empty.js", initialContent)
108106

109107
// Test empty response
110-
streamingParser.initialize(context)
111-
const result = streamingParser.parseResponse("", "", "")
108+
const result = parseGhostResponse("", "", "", context.document, context.range)
112109
expect(result.suggestions.hasSuggestions()).toBe(false)
113110
})
114111

@@ -118,8 +115,7 @@ describe("GhostProvider", () => {
118115

119116
// Test invalid XML format
120117
const invalidXML = "This is not a valid XML format"
121-
streamingParser.initialize(context)
122-
const result = streamingParser.parseResponse(invalidXML, "", "")
118+
const result = parseGhostResponse(invalidXML, "", "", context.document, context.range)
123119
expect(result.suggestions.hasSuggestions()).toBe(false)
124120
})
125121

@@ -137,8 +133,7 @@ describe("GhostProvider", () => {
137133
const xmlResponse = `<change><search><![CDATA[console.log('test');]]></search><replace><![CDATA[// Added comment
138134
console.log('test');]]></replace></change>`
139135

140-
streamingParser.initialize(context)
141-
const result = streamingParser.parseResponse(xmlResponse, "", "")
136+
const result = parseGhostResponse(xmlResponse, "", "", context.document, context.range)
142137
// Should work with the XML format
143138
expect(result.suggestions.hasSuggestions()).toBe(true)
144139
})

0 commit comments

Comments
 (0)