LibreChat
LibreChat copied to clipboard
🚑 fix(export): Export Issue with New Chat
Summary
When starting a new chat, sending a few messages, and then exporting the conversation, the exported text only includes the first message. This issue is reproducible across all output file formats.
This issue occurs in a rare use case, so it might be unimportant. However, I investigated it to learn the code. If you plan to refactor or fix this differently, please feel free to close this PR.
Issue
How to Reproduce
- Start a new chat.
- Send more than one message.
- Export the conversation.
- The exported text only includes the first message.
Cause
Premise
- When a message is sent, it is added to the react-query cache using the
setMessages
function fromuseChatHelpers
, with conversationId as the key.
Flow
- When a message is sent in a new chat, it is added to the react-query cache with the key 'new' because the actual conversationId has yet to be obtained.
- After the first message is successfully sent, an actual conversationId is assigned to the conversation.
- The useGetMessagesByConvoId function in
useChatHelpers
fetches messages from the API using the actual conversationId and sets them in the cache with the actual conversationId as the key. At this point, only one message exists for this conversationId. - Subsequent messages in the new chat are added to the cache with the key 'new', but they are not added to the cache with the actual conversationId. As a result, only the first message remains under the actual conversationId.
- During export, the actual conversationId is used, and react-query returns the cache of that key, so only the first message (stored under the actual conversationId) is included in the exported text.
(Please let me know if my understanding is incorrect)
Fix
There are several options to fix this
- Disable export for new chats (for /c/new): Cons: This is very simple but the export icon might be inconsistently displayed for chats, which could confuse users.
- Remove useGetMessagesByConvoId from
useChatHelpers
to avoid caching the first message Cons: The reason for fetching messages here is unclear(these messages might not be used anywhere in the hook), and removing them might cause side effects. - Set
refetchOnMount
totrue
in useGetMessagesByConvoId withinuseExportConversation
to ensure the latest conversation, Cons: This might cause unnecessary network access every time the export modal is opened.
I chose the third option. Although it might cause unnecessary network access, it ensures that the latest conversation is always exported.
Again, if you choose a different option, do not worry about it and close this PR.
Change Type
- [x] Bug fix (non-breaking change which fixes an issue)
Testing
I verified that I could export correctly in the new chat by doing the reproduction steps above.
Checklist
- [x] My code adheres to this project's style guidelines
- [x] I have performed a self-review of my own code
- [x] I have commented in any complex areas of my code
- [x] Local unit tests pass with my changes