fix(chat): 在聊天流处理后清除未读消息
在之前的逻辑中,当 ChatterManager 处理完一个聊天流后,该流上下文中的 unread_messages 列表并未被清空。这可能导致在后续的处理周期中,相同的消息被重复获取和处理,引发非预期的行为并浪费计算资源。 此更改通过在 MessageManager 中新增 `clear_stream_unread_messages` 方法,并在每次成功处理流之后立即调用它,确保消息只被处理一次,解决了潜在的重复处理问题。
This commit is contained in:
@@ -122,6 +122,13 @@ class ChatterManager:
|
||||
actions_count = result.get("actions_count", 0)
|
||||
logger.debug(f"流 {stream_id} 处理完成: 成功={success}, 动作数={actions_count}")
|
||||
|
||||
# 在处理完成后,清除该流的未读消息
|
||||
try:
|
||||
from src.chat.message_manager.message_manager import message_manager
|
||||
await message_manager.clear_stream_unread_messages(stream_id)
|
||||
except Exception as clear_e:
|
||||
logger.error(f"清除流 {stream_id} 未读消息时发生错误: {clear_e}")
|
||||
|
||||
return result
|
||||
except Exception as e:
|
||||
self.stats["failed_executions"] += 1
|
||||
|
||||
@@ -354,6 +354,25 @@ class MessageManager:
|
||||
except Exception as e:
|
||||
logger.error(f"清除未读消息时发生错误: {e}")
|
||||
|
||||
async def clear_stream_unread_messages(self, stream_id: str):
|
||||
"""清除指定聊天流的所有未读消息"""
|
||||
try:
|
||||
chat_manager = get_chat_manager()
|
||||
chat_stream = chat_manager.get_stream(stream_id)
|
||||
if not chat_stream:
|
||||
logger.warning(f"clear_stream_unread_messages: 聊天流 {stream_id} 不存在")
|
||||
return
|
||||
|
||||
context = chat_stream.context_manager.context
|
||||
if hasattr(context, 'unread_messages') and context.unread_messages:
|
||||
logger.debug(f"正在为流 {stream_id} 清除 {len(context.unread_messages)} 条未读消息")
|
||||
context.unread_messages.clear()
|
||||
else:
|
||||
logger.debug(f"流 {stream_id} 没有需要清除的未读消息")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"清除流 {stream_id} 的未读消息时发生错误: {e}")
|
||||
|
||||
|
||||
# 创建全局消息管理器实例
|
||||
message_manager = MessageManager()
|
||||
|
||||
Reference in New Issue
Block a user