danny-avila / LibreChat

Enhanced ChatGPT Clone: Features Anthropic, AWS, OpenAI, Assistants API, Azure, Groq, o1, GPT-4o, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, langchain, DALL-E-3, ChatGPT Plugins, OpenAI Functions, Secure Multi-User System, Presets, completely open-source for self-hosting. Actively in public development.
https://librechat.ai/
MIT License
19.33k stars 3.23k forks source link

fix: forking a long conversation breaks chat structure #4777

Closed xyqyear closed 3 days ago

xyqyear commented 3 days ago

ref: PR https://github.com/danny-avila/LibreChat/pull/4772 and issue #4761

I think I've found the culprit.

This issue is due to the forking doesn't copy over the createdAt attribute of the messages, so the message list queried might be our of order, hence a messed up tree.

The main change is here:

https://github.com/xyqyear/LibreChat/blob/cb98456ba034a86857adaeaa564d2ac7f17cebc8/api/models/Message.js#L80-L86

If timestamps is true, then mongoose will override createdAt with the current time, which is the default if timestamps is set to true for the schema.

Testing

I've modified exiting tests and the api tests passed.

However, this PR should not be merged right away and should be further investigated to make sure importing doesn't break since importing use the same mechanics.

The current behavior for importing I believe is to just use the default createdAt time that mongoose auto inserts. So imports a conversations with more than 16 messages should be having the same issue (needs testing).

danny-avila commented 3 days ago

Adding my changes here, added some tests to simulate the behavior, since I can't directly reproduce it with mongodb memory server. https://github.com/danny-avila/LibreChat/pull/4778