fix: 优化消息保存逻辑,避免重复保存用户消息,简化日志记录
This commit is contained in:
parent
d46f2a035d
commit
4bc9a7010f
@ -163,17 +163,13 @@ async def chat_agent(
|
|||||||
|
|
||||||
# 获取已保存的消息数量,避免重复保存
|
# 获取已保存的消息数量,避免重复保存
|
||||||
existing_messages = conv_mgr.get_messages_by_thread_id(thread_id)
|
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}
|
||||||
|
|
||||||
# 只保存新增的消息
|
for msg in messages:
|
||||||
new_messages = messages[existing_count :]
|
|
||||||
|
|
||||||
for msg in new_messages:
|
|
||||||
msg_dict = msg.model_dump() if hasattr(msg, "model_dump") else {}
|
msg_dict = msg.model_dump() if hasattr(msg, "model_dump") else {}
|
||||||
msg_type = msg_dict.get("type", "unknown")
|
msg_type = msg_dict.get("type", "unknown")
|
||||||
|
|
||||||
if msg_type == "human":
|
if msg_type == "human" or msg.id in existing_ids:
|
||||||
# 用户消息(理论上已经保存过了,跳过)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
elif msg_type == "ai":
|
elif msg_type == "ai":
|
||||||
@ -242,7 +238,7 @@ async def chat_agent(
|
|||||||
|
|
||||||
logger.debug(f"Processed message type={msg_type}")
|
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:
|
except Exception as e:
|
||||||
logger.error(f"Error saving messages from LangGraph state: {e}")
|
logger.error(f"Error saving messages from LangGraph state: {e}")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user