-
Notifications
You must be signed in to change notification settings - Fork 56
Description
Description:
When starting a new chat, the frontend waits for the backend to return a chatId before updating the URL and tracking the chat session. This works fine for a single chat, but if the user starts multiple chats quickly (either in the same tab or across multiple tabs) before receiving any chatId from the backend, the UI can get out of sync and responses may appear in the wrong chat window.
Steps to Reproduce:
- Open the chat screen and start a new chat (send the first message).
- Before the backend responds with a chatId, start another new chat in the same tab or a different tab and send a message.
- Wait for the backend to respond with chatIds for each pending chat (they may arrive out of order).
Expected Behavior:
Each chat window (or tab) should only update and redirect when its own chatId arrives from the backend. Other chats should not be affected, and chat history/sidebar should remain consistent.
Actual Behavior:
- The UI may redirect to the wrong chat window when a chatId arrives.
- Responses can appear in the wrong chat or tab.
- There is no synchronization between pending chats, leading to a race condition and potentially confusing user experience.
Root Cause:
Currently, the frontend does not generate or track any local chat ID or tab ID. It relies entirely on the backend chatId, which is not available until after the first message is sent and the backend responds. This causes ambiguity when multiple chats are pending at the same time.
Proposed Solution:
- On creating a new chat, generate a localChatId (UUID) on the frontend and send it with the first message to the backend.
- Store a tabId in sessionStorage so each tab is uniquely identified.
- Maintain a temporary mapping of
{ localChatId, tabId, status }
(pending/completed) in sessionStorage or localStorage. - When the backend responds, match the response’s localChatId and tabId to the currently active chat in the tab:
- If both match, update the UI and redirect.
- If not, just update the sidebar/history.
- Clean up the mapping as soon as the chat is resolved (i.e., when the backend chatId is received).