From 4bc9a7010f9e24564f9417ba2e5468d2e3b89d33 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Sun, 12 Oct 2025 21:12:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E9=80=BB=E8=BE=91=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E4=BF=9D=E5=AD=98=E7=94=A8=E6=88=B7=E6=B6=88?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E7=AE=80=E5=8C=96=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/routers/chat_router.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/server/routers/chat_router.py b/server/routers/chat_router.py index bbd5157f..4867bcc0 100644 --- a/server/routers/chat_router.py +++ b/server/routers/chat_router.py @@ -163,17 +163,13 @@ async def chat_agent( # 获取已保存的消息数量,避免重复保存 existing_messages = conv_mgr.get_messages_by_thread_id(thread_id) - existing_count = len(existing_messages) + existing_ids = {msg.extra_metadata["id"] for msg in existing_messages if msg.extra_metadata and "id" in msg.extra_metadata} - # 只保存新增的消息 - new_messages = messages[existing_count :] - - for msg in new_messages: + for msg in messages: msg_dict = msg.model_dump() if hasattr(msg, "model_dump") else {} msg_type = msg_dict.get("type", "unknown") - if msg_type == "human": - # 用户消息(理论上已经保存过了,跳过) + if msg_type == "human" or msg.id in existing_ids: continue elif msg_type == "ai": @@ -242,7 +238,7 @@ async def chat_agent( logger.debug(f"Processed message type={msg_type}") - logger.info(f"Saved {len(new_messages)} new messages from LangGraph state") + logger.info(f"Saved messages from LangGraph state") except Exception as e: logger.error(f"Error saving messages from LangGraph state: {e}")