fix(chat): 修复消息打断后的重复处理并优化回复逻辑
本次提交包含两项关键更改: 1. 将 `triggering_user_id` 的设置逻辑从 `ChatterManager` 迁移至 `StreamLoopManager` 的处理循环初期。这确保了触发用户ID能够更早、更可靠地被设置,为回复保护机制提供了正确的上下文,避免了潜在的逻辑错误。 2. 在消息成功打断正在执行的任务后,立即调用 `clear_all_unread_messages`。此举修复了当新消息打断旧任务时,旧消息队列未被清空,导致机器人可能在之后重新处理这些过时消息的缺陷,从而防止了重复响应。
This commit is contained in:
@@ -110,11 +110,6 @@ class ChatterManager:
|
||||
|
||||
self.stats["streams_processed"] += 1
|
||||
try:
|
||||
# 设置触发用户ID
|
||||
last_message = context.get_last_message()
|
||||
if last_message:
|
||||
context.triggering_user_id = last_message.user_info.user_id
|
||||
|
||||
result = await self.instances[stream_id].execute(context)
|
||||
|
||||
# 检查执行结果是否真正成功
|
||||
|
||||
@@ -378,6 +378,11 @@ class StreamLoopManager:
|
||||
if cached_messages:
|
||||
logger.info(f"处理开始前刷新缓存消息: stream={stream_id}, 数量={len(cached_messages)}")
|
||||
|
||||
# 设置触发用户ID,以实现回复保护
|
||||
last_message = context.get_last_message()
|
||||
if last_message:
|
||||
context.triggering_user_id = last_message.user_info.user_id
|
||||
|
||||
# 创建子任务用于刷新能量(不阻塞主流程)
|
||||
energy_task = asyncio.create_task(self._refresh_focus_energy(stream_id))
|
||||
child_tasks.add(energy_task)
|
||||
|
||||
@@ -405,6 +405,9 @@ class MessageManager:
|
||||
|
||||
if cancelled_count > 0:
|
||||
logger.info(f"消息打断成功取消 {cancelled_count} 个任务: {chat_stream.stream_id}")
|
||||
|
||||
# 修复:打断后,将被打断的消息标记为已读,防止重复处理
|
||||
await self.clear_all_unread_messages(chat_stream.stream_id)
|
||||
else:
|
||||
logger.warning(f"消息打断未能取消任何任务: {chat_stream.stream_id}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user