Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/services/ghost/GhostStreamingParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ function mapNormalizedToOriginalIndex(
export class GhostStreamingParser {
public buffer: string = ""
private completedChanges: ParsedChange[] = []
private lastProcessedIndex: number = 0
private context: GhostSuggestionContext | null = null
private streamFinished: boolean = false

Expand All @@ -223,7 +222,6 @@ export class GhostStreamingParser {
public reset(): void {
this.buffer = ""
this.completedChanges = []
this.lastProcessedIndex = 0
this.streamFinished = false
}

Expand All @@ -237,6 +235,16 @@ export class GhostStreamingParser {

// Add chunk to buffer
this.buffer += chunk
this.generateSuggestions(new Array<ParsedChange>())
}

/**
* Process a new chunk of text and return any newly completed suggestions
*/
public processResult(): StreamingParseResult {
if (!this.context) {
throw new Error("Parser not initialized. Call initialize() first.")
}

// Extract any newly completed changes from the current buffer
const newChanges = this.extractCompletedChanges()
Expand Down Expand Up @@ -280,7 +288,7 @@ export class GhostStreamingParser {
*/
public finishStream(): StreamingParseResult {
this.streamFinished = true
return this.processChunk("")
return this.processResult()
}

/**
Expand All @@ -290,7 +298,7 @@ export class GhostStreamingParser {
const newChanges: ParsedChange[] = []

// Look for complete <change> blocks starting from where we left off
const searchText = this.buffer.substring(this.lastProcessedIndex)
const searchText = this.buffer

// Updated regex to handle both single-line XML format and traditional format with whitespace
const changeRegex =
Expand All @@ -315,11 +323,6 @@ export class GhostStreamingParser {
lastMatchEnd = match.index + match[0].length
}

// Update our processed index to avoid re-processing the same content
if (lastMatchEnd > 0) {
this.lastProcessedIndex += lastMatchEnd
}

return newChanges
}

Expand Down
Loading