Repository navigation
fix(chat): preserve in-flight SSE stream when switching conversations - #18037
Conversation
Switching conversations no longer aborts the in-flight completion request, which could cancel server-side persistence of the just-asked question. The old SSE stream finishes in the background instead. - SingleChatBox ignores backend conversation.messages sync while a stream is active for that conversation, so local streaming state isn't wiped. - useSendMessage keeps a per-conversation message cache, restores it on switch, and routes streamed answers to the conversation they belong to (via answer.conversationId) rather than the currently displayed one. Co-Authored-By: Claude Opus 4.6 <[email protected]>
📝 WalkthroughWalkthroughThe chat flow now caches messages per conversation and tracks active SSE streams. Questions and answers are routed to their target conversations. Conversation switching no longer aborts the previous request, and active streams are protected from backend message replacement. ChangesConversation stream preservation
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant User
participant useSendMessage
participant SSE
participant messagesCacheRef
participant SingleChatBox
User->>useSendMessage: send message for conversation
useSendMessage->>SSE: start SSE stream
useSendMessage->>messagesCacheRef: cache question and placeholder
SSE-->>useSendMessage: stream answer with conversationId
useSendMessage->>messagesCacheRef: update target conversation
SingleChatBox->>useSendMessage: read active stream state
SingleChatBox-->>User: preserve local streamed messages
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@web/src/pages/next-chats/hooks/use-click-card.ts`:
- Around line 18-24: Remove the setController(new AbortController()) call from
the navigation flow in use-click-card.ts so the active stream controller remains
reachable by stopOutputMessage. Preserve the existing behavior of letting the
previous SSE request finish without aborting it during conversation switching.
In `@web/src/pages/next-chats/hooks/use-send-chat-message.ts`:
- Around line 55-60: Update the assistant placeholder construction in
useSendChatMessage so its id is generated independently rather than via
buildMessageUuid with the original message, ensuring it cannot reuse
questionMessage.id. Preserve the existing role, empty content, and
conversationId assignments.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: db31ec79-bf71-4428-9a14-4bde7ca34ad0
📒 Files selected for processing (3)
web/src/pages/next-chats/chat/chat-box/single-chat-box.tsxweb/src/pages/next-chats/hooks/use-click-card.tsweb/src/pages/next-chats/hooks/use-send-chat-message.ts
| // Switch to a fresh controller WITHOUT aborting the previous one. | ||
| // Aborting the in-flight completion request can cancel server-side | ||
| // persistence of the just-asked question, so switching conversations | ||
| // right after asking would lose the question. Let the old SSE finish | ||
| // in the background; the unmounted SingleChatBox's setState becomes | ||
| // a no-op and the server keeps the message. | ||
| setController(new AbortController()); |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Keep the active stream controller reachable by stopOutputMessage.
The active SSE request retains the controller that useSendMessage received before the switch. Line 24 replaces it. A later stopOutputMessage call aborts the new controller and leaves the prior stream running.
Do not replace the controller on navigation, or track controllers by active stream and abort the controller that owns the selected stream.
Small local fix
- setController(new AbortController());🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@web/src/pages/next-chats/hooks/use-click-card.ts` around lines 18 - 24,
Remove the setController(new AbortController()) call from the navigation flow in
use-click-card.ts so the active stream controller remains reachable by
stopOutputMessage. Preserve the existing behavior of letting the previous SSE
request finish without aborting it during conversation switching.
| { | ||
| role: MessageType.Assistant, | ||
| content: '', | ||
| conversationId: message.conversationId, | ||
| id: buildMessageUuid({ ...message, role: MessageType.Assistant }), | ||
| }, |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Give the assistant placeholder a distinct ID.
questionMessage always has an ID at Line 246. buildMessageUuid preserves that ID. Line 59 therefore assigns the user message ID to the assistant placeholder. This makes ID-based message removal and file reconciliation ambiguous until an answer replaces the placeholder.
Proposed fix
{
role: MessageType.Assistant,
content: '',
conversationId: message.conversationId,
- id: buildMessageUuid({ ...message, role: MessageType.Assistant }),
+ id: buildMessageUuid({ role: MessageType.Assistant }),
},📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| { | |
| role: MessageType.Assistant, | |
| content: '', | |
| conversationId: message.conversationId, | |
| id: buildMessageUuid({ ...message, role: MessageType.Assistant }), | |
| }, | |
| { | |
| role: MessageType.Assistant, | |
| content: '', | |
| conversationId: message.conversationId, | |
| id: buildMessageUuid({ role: MessageType.Assistant }), | |
| }, |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@web/src/pages/next-chats/hooks/use-send-chat-message.ts` around lines 55 -
60, Update the assistant placeholder construction in useSendChatMessage so its
id is generated independently rather than via buildMessageUuid with the original
message, ensuring it cannot reuse questionMessage.id. Preserve the existing
role, empty content, and conversationId assignments.
wangq8
left a comment
There was a problem hiding this comment.
Nice work on this PR — keeping SSE streams alive with per-conversation caching is a clean approach.
Observations (non-blocking):
-
messagesCacheRef grows unbounded for the tab lifetime. Consider an LRU cap (20-30 conversations) for long sessions to avoid memory pressure.
-
appendAnswerToMessages duplicates the message-building logic from addNewestAnswer in logic-hooks.ts. Extracting a shared helper would prevent drift if fields change upstream.
-
appendAnswerToMessages assumes the last message is always the assistant placeholder — safe today given addNewestQuestion pattern, but worth a comment for future readers.
Edge cases around messagesConversationIdRef ordering and targetConversationId routing are correctly handled. LGTM overall.
Summary
fix(chat): preserve in-flight SSE stream when switching conversations
Switching conversations no longer aborts the in-flight completion request, which could cancel server-side persistence of the just-asked question. The old SSE stream finishes in the background instead.