Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌿 fix: forking a long conversation breaks chat structure #4778

Merged
merged 4 commits into from
Nov 22, 2024

Conversation

danny-avila
Copy link
Owner

ref: PR #4772

Closes #4761

Originally #4777

@danny-avila danny-avila merged commit c87a51e into main Nov 22, 2024
4 checks passed
@danny-avila danny-avila deleted the xyqyear-fix-fork branch November 22, 2024 21:11
@xyqyear
Copy link
Contributor

xyqyear commented Nov 23, 2024

Is it possible to reference the same buildTree function in the tests instead of duplicating the code? Changes made to buildTree would have to be reflected in two different places.

Another thing is, this change would make imported (from file) messages not having createdAt field, which mighe become problematic when in the future we call
something like "deletedOlderThan". We might need to add createdAt manually when importing from files (adding incremental createdAt field when not existed or converting from original timestamp.).

@danny-avila

@danny-avila
Copy link
Owner Author

@xyqyear

Is it possible to reference the same buildTree function in the tests instead of duplicating the code? Changes made to buildTree would have to be reflected in two different places.

I don't plan on changing this function, and it's really not needed in the backend except for this test with the database logic, so it's not worth it to me.

Another thing is, this change would make imported (from file) messages not having createdAt field, which mighe become problematic when in the future we call
something like "deletedOlderThan". We might need to add createdAt manually when importing from files (adding incremental createdAt field when not existed or converting from original timestamp.).

can you elaborate more on this? I think this would be simple to handle the edge case here then:

async saveBatch() {
try {
await bulkSaveConvos(this.conversations);
await bulkSaveMessages(this.messages, true);
logger.debug(
`user: ${this.requestUserId} | Added ${this.conversations.length} conversations and ${this.messages.length} messages to the DB.`,
);
} catch (error) {
logger.error('Error saving batch', error);
throw error;
}
}

to this:

  async saveBatch() {
    try {
      // Set default timestamp for messages that don't have one
      const defaultTimestamp = new Date();
      this.messages = this.messages.map(message => ({
        ...message,
        createdAt: message.createdAt || defaultTimestamp,
        updatedAt: message.updatedAt || defaultTimestamp
      }));

      await bulkSaveConvos(this.conversations);
      // Keep overrideTimestamp as true to use our explicitly set timestamps
      await bulkSaveMessages(this.messages, true);
      logger.debug(
        `user: ${this.requestUserId} | Added ${this.conversations.length} conversations and ${this.messages.length} messages to the DB.`,
      );
    } catch (error) {
      logger.error('Error saving batch', error);
      throw error;
    }
  }
}

@danny-avila
Copy link
Owner Author

also maybe the above needs to be adjusted to account for child messages not being earlier than parent messages

@xyqyear
Copy link
Contributor

xyqyear commented Nov 24, 2024

@danny-avila

Absolutely! Changing saveBatch like that to account for edge cases is the best way to do it for now imo.

also maybe the above needs to be adjusted to account for child messages not being earlier than parent messages

This could be accomplished by d38953b However, through testing, this will make the page unresponsive for some reason. But if it's done correctly, the previously broken conversations could be fixed as well.

owengo pushed a commit to openwengo/LibreChat that referenced this pull request Nov 26, 2024
…#4778)

* fix: branching and forking sometimes break conversation structure

* fix test for forking.

* chore: message type issues

* test: add conversation structure tests for message handling

---------

Co-authored-by: xyqyear <[email protected]>
BertKiv pushed a commit to BertKiv/LibreChat that referenced this pull request Dec 10, 2024
…#4778)

* fix: branching and forking sometimes break conversation structure

* fix test for forking.

* chore: message type issues

* test: add conversation structure tests for message handling

---------

Co-authored-by: xyqyear <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Saved conversation structure collapse
2 participants