message files are displayed in random order,when browsing history conversation in the logs and annotations page.
Self Checks
- [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
- [x] I have searched for existing issues search for existing issues, including closed ones.
- [x] I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
- [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
- [x] Please do not modify this template :) and fill in all the required fields.
Dify version
0.15.3
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
I need the pictures to be displayed in the dialog box in the order they were uploaded when the message was sent.
api\models\model.py,This is the code in the dify project source code that involves getting message files.
@property
def message_files(self):
from factories import file_factory
message_files = db.session.query(MessageFile).filter(MessageFile.message_id == self.id).all()
current_app = db.session.query(App).filter(App.id == self.app_id).first()
api\core\app\apps\message_based_app_generator.py,This is the part of the dify project source code that involves storing message_file.
for file in application_generate_entity.files:
message_file = MessageFile(
message_id=message.id,
type=file.type,
transfer_method=file.transfer_method,
belongs_to="user",
url=file.remote_url,
upload_file_id=file.related_id,
created_by_role=(CreatedByRole.ACCOUNT if account_id else CreatedByRole.END_USER),
created_by=account_id or end_user_id or ""
)
db.session.add(message_file)
db.session.commit()
When a group of files (such as 5 pictures) are sent as a message file, and create_at field(the field is accurate to seconds) value is the same, that is, the creation time is the same.This may happen: the message file records inserted into the database are out of order.
When querying message files, there is no field that can represent the order of message files.
Solution:
When the message file is stored in the database, I assign created_at with microsecond accuracy. When querying, I use created_at to sort.
✔️ Expected Behavior
The order in which I uploaded.
❌ Actual Behavior
Disordered situation.There is a disorder. At the same time, the default order of records in the database is also like this.
To address the issue of message files being displayed in a random order, you can implement a solution by ensuring that the created_at field in the MessageFile model is assigned with microsecond accuracy. This will help maintain the order of files as they are uploaded.
Here's a step-by-step approach to implement this solution:
-
Modify the
created_atField: Ensure that thecreated_atfield in theMessageFilemodel is set with microsecond precision. You can achieve this by modifying the code where theMessageFileinstances are created to include microsecond precision for thecreated_attimestamp. -
Update the Query Logic: When retrieving message files, sort them by the
created_atfield to maintain the order in which they were uploaded. You can modify the query in themessage_filesproperty to include anorder_byclause:message_files = db.session.query(MessageFile).filter(MessageFile.message_id == self.id).order_by(MessageFile.created_at).all() -
Test the Changes: After implementing these changes, test the application to ensure that the message files are displayed in the correct order based on their upload time.
By following these steps, you should be able to maintain the correct order of message files when they are displayed in the logs and annotations page.
To continue talking to Dosu, mention @dosu.
Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other