fix: branching and forking sometimes break conversation structure #4772
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.
Summary
As reported in #4761 and #3813, conversations will seemingly randomly collapse when regenerating or forking. I believe this is because in buildTree.ts, the function assumes messages are in the correct order when building the tree (parents are before their children). But for some reason, this is only the case when there are less than 17 messages in the conversation.
If the messages are not in correct order, when looking for a message's parent in
messageMap
, the parent might not have put into the map yet resulting in the current message becoming a root message.What I've done is to populate
messageMap
first in one loop, and then in another loop look for a message's parent. This way, the parent should be found wherever it is in the message list.I don't know if messages should be in the correct order when queried from the api given the previous assumption in
buildTree.ts
but this change should resolve the issue anyway.Change Type
Testing
npm run test:client
is passing.I have not introduced any changes to the api.
I've tested my changes with a conversation described in #4761 (comment)
Before the change: conversatios with more than 16 messages will collapse after forking. Conversations with more than 16 messages and include any branching will collapse.
After the changes: all is good.
Checklist
Please delete any irrelevant options.