diff --git a/src/services/ghost/GhostStreamingParser.ts b/src/services/ghost/GhostStreamingParser.ts index 349256cb58c..ad8c6354cc7 100644 --- a/src/services/ghost/GhostStreamingParser.ts +++ b/src/services/ghost/GhostStreamingParser.ts @@ -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 @@ -223,7 +222,6 @@ export class GhostStreamingParser { public reset(): void { this.buffer = "" this.completedChanges = [] - this.lastProcessedIndex = 0 this.streamFinished = false } @@ -237,6 +235,16 @@ export class GhostStreamingParser { // Add chunk to buffer this.buffer += chunk + this.generateSuggestions(new Array()) + } + + /** + * 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() @@ -280,7 +288,7 @@ export class GhostStreamingParser { */ public finishStream(): StreamingParseResult { this.streamFinished = true - return this.processChunk("") + return this.processResult() } /** @@ -290,7 +298,7 @@ export class GhostStreamingParser { const newChanges: ParsedChange[] = [] // Look for complete 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 = @@ -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 }