fix(chat/wechat): messages order by created_at,id (sub-second DATETIME(3)); container+MySQL TZ->Asia/Shanghai; correct UTC timestamps to CST; rebuild WeXin Bot history

This commit is contained in:
qyyfw committed 2026-07-10 10:50:06 +08:00
1 parent 7b7871a14d
commit 774f1648dc
2 files changed
+8 -5

No files matched your search

+2 -2
View File
@@ -802,7 +802,7 @@ def get_messages(
messages = ( messages = (
db.query(Message) db.query(Message)
.filter(Message.conversation_id == conv_id) .filter(Message.conversation_id == conv_id)
.order_by(asc(Message.created_at)) .order_by(asc(Message.created_at), asc(Message.id))
.all() .all()
) )
@@ -1227,7 +1227,7 @@ def convert_to_knowledge_graph(
messages = ( messages = (
db.query(Message) db.query(Message)
.filter(Message.conversation_id == conv_id) .filter(Message.conversation_id == conv_id)
.order_by(asc(Message.created_at)) .order_by(asc(Message.created_at), asc(Message.id))
.all() .all()
) )
+6 -3
View File
@@ -17,7 +17,7 @@ import json
import random import random
import threading import threading
import time import time
from datetime import datetime from datetime import datetime, timedelta
from typing import Optional from typing import Optional
import requests import requests
@@ -310,8 +310,11 @@ def _mirror_to_ai_chat(user_id: str, user_content: str, assistant_content: str):
try: try:
db = SessionLocal() db = SessionLocal()
cid = _get_or_create_wechat_conv(db, user_id) cid = _get_or_create_wechat_conv(db, user_id)
_append_ai_message(db, cid, "user", user_content) now = datetime.now()
_append_ai_message(db, cid, "assistant", assistant_content) _append_ai_message(db, cid, "user", user_content, created=now)
# assistant 比 user 晚 50ms,保证排序严格递增,避免同秒时 ORDER BY 乱序
_append_ai_message(db, cid, "assistant", assistant_content,
created=now + timedelta(milliseconds=50))
_execute_raw(db, text("UPDATE conversations SET updated_at=:u WHERE id=:id"), _execute_raw(db, text("UPDATE conversations SET updated_at=:u WHERE id=:id"),
{"u": datetime.now(), "id": cid}) {"u": datetime.now(), "id": cid})
except Exception: except Exception: