fix(frontend): preserve streamed message prefix on reconnect#3318
Open
LittleChenLiya wants to merge 2 commits into
Open
fix(frontend): preserve streamed message prefix on reconnect#3318LittleChenLiya wants to merge 2 commits into
LittleChenLiya wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR improves mergeMessages behavior to preserve already-persisted AI text when a streaming connection reconnects and resumes with only new deltas, avoiding duplicated tails and retaining live metadata.
Changes:
- Added multiple unit tests covering AI-prefix preservation, overlap deduplication, and non-stitching behaviors.
- Extended
mergeMessageswith optional partial-overlap stitching for AI messages (including array-based text content). - Updated
useThreadStreamto enable the new stitching mode only for messages withstreamMetadatawhile loading.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| frontend/tests/unit/core/threads/message-merge.test.ts | Adds tests for reconnect/stream delta merge behavior and overlap deduplication. |
| frontend/src/core/threads/hooks.ts | Implements AI text overlap detection + stitching and wires it into mergeMessages/useThreadStream. |
Comment on lines
+150
to
+172
| function shouldJoinPartialAiText( | ||
| historyText: string, | ||
| threadText: string, | ||
| allowDisjointText: boolean, | ||
| ) { | ||
| if (!historyText || !threadText) { | ||
| return false; | ||
| } | ||
| if (threadText.startsWith(historyText)) { | ||
| return false; | ||
| } | ||
| if (historyText.endsWith(threadText)) { | ||
| return true; | ||
| } | ||
| const overlap = longestTextOverlap(historyText, threadText); | ||
| if (overlap > 0) { | ||
| return true; | ||
| } | ||
| if (allowDisjointText) { | ||
| return true; | ||
| } | ||
| return /\s$/.test(historyText) || /^\s/.test(threadText); | ||
| } |
Comment on lines
282
to
+286
| export function mergeMessages( | ||
| historyMessages: Message[], | ||
| threadMessages: Message[], | ||
| optimisticMessages: Message[], | ||
| preservePartialAiOverlap: boolean | PartialAiOverlapPredicate = false, |
Comment on lines
+140
to
+148
| function longestTextOverlap(left: string, right: string): number { | ||
| const maxLength = Math.min(left.length, right.length); | ||
| for (let length = maxLength; length > 0; length--) { | ||
| if (left.endsWith(right.slice(0, length))) { | ||
| return length; | ||
| } | ||
| } | ||
| return 0; | ||
| } |
Comment on lines
+161
to
+177
| test("mergeMessages does not stitch unrelated live AI replacement text while streaming", () => { | ||
| const persistedAi = { | ||
| id: "ai-1", | ||
| type: "ai", | ||
| content: "old answer", | ||
| } as Message; | ||
| const liveReplacementAi = { | ||
| id: "ai-1", | ||
| type: "ai", | ||
| content: "new answer", | ||
| response_metadata: { model: "test-model" }, | ||
| } as Message; | ||
|
|
||
| expect(mergeMessages([persistedAi], [liveReplacementAi], [], true)).toEqual([ | ||
| liveReplacementAi, | ||
| ]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题原因
流式回答过程中切换到其他对话再返回时,前端会同时合并已持久化的历史消息和重连后的 live stream 消息。对于同一个 AI 消息 ID,现有合并逻辑总是让 live stream 消息覆盖历史消息;如果重连后的流只包含后续增量文本,已显示并持久化的前缀就会在流式过程中从页面上消失。
修改内容
关联 issue
Closes #3279
Problem Cause
When users leave a chat during streaming and then return, the frontend merges persisted history messages with the reconnected live stream messages. For the same AI message ID, the previous merge logic always let the live stream message replace the history message. If the reconnected stream only contains later delta text, the already rendered and persisted prefix disappears from the page while streaming continues.
Changes
Related Issue
Closes #3279