feat(distribution_manager): 在处理成功后添加短暂等待,确保清理操作完成

feat(message_manager): 简化清除未读消息逻辑,移除冗余注释
feat(action_manager): 移除自动清空未读消息的逻辑,改为手动处理
feat(plan_executor): 扩展回复类动作的分类,包含 respond 动作
This commit is contained in:
Windpicker-owo
2025-11-10 21:38:55 +08:00
parent c46df81bca
commit f3af3caf71
5 changed files with 16 additions and 86 deletions

View File

@@ -249,6 +249,10 @@ class StreamLoopManager:
self.stats["total_process_cycles"] += 1
if success:
logger.info(f"✅ [流工作器] stream={stream_id[:8]}, 任务ID={task_id}, 处理成功")
# 🔒 处理成功后,等待一小段时间确保清理操作完成
# 这样可以避免在 chatter_manager 清除未读消息之前就进入下一轮循环
await asyncio.sleep(0.1)
else:
self.stats["total_failures"] += 1
logger.warning(f"❌ [流工作器] stream={stream_id[:8]}, 任务ID={task_id}, 处理失败")
@@ -347,15 +351,9 @@ class StreamLoopManager:
# 设置处理状态为正在处理
self._set_stream_processing_status(stream_id, True)
# 子任务跟踪 - 存储到 context 中以便打断时可以访问
child_tasks = set()
context._active_process_tasks = child_tasks # 临时存储子任务集合
chatter_task = None
try:
start_time = time.time()
# 注意缓存消息刷新已移至planner开始时执行动作修改器之后此处不再刷新
# 检查未读消息,如果为空则直接返回(优化:避免无效的 chatter 调用)
unread_messages = context.get_unread_messages()
if not unread_messages:
@@ -370,9 +368,7 @@ class StreamLoopManager:
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)
energy_task.add_done_callback(lambda t: child_tasks.discard(t))
asyncio.create_task(self._refresh_focus_energy(stream_id))
# 设置 Chatter 正在处理的标志
context.is_chatter_processing = True
@@ -383,9 +379,7 @@ class StreamLoopManager:
self.chatter_manager.process_stream_context(stream_id, context),
name=f"chatter_process_{stream_id}"
)
child_tasks.add(chatter_task)
context._chatter_task = chatter_task # 保存主 chatter 任务引用
# 等待 chatter 任务完成
results = await chatter_task
success = results.get("success", False)
@@ -401,47 +395,19 @@ class StreamLoopManager:
else:
logger.warning(f"流处理失败: {stream_id} - {results.get('error_message', '未知错误')}")
return success
except asyncio.CancelledError:
logger.info(f"流处理被取消,正在清理所有子任务: {stream_id}")
# 取消所有子任务(包括 chatter 任务和其后台任务)
cancel_count = 0
for child_task in list(child_tasks): # 使用 list() 创建副本避免迭代时修改
if not child_task.done():
child_task.cancel()
cancel_count += 1
if cancel_count > 0:
logger.info(f"已取消 {cancel_count} 个子任务: {stream_id}")
# 等待所有子任务真正结束(设置短超时)
try:
await asyncio.wait_for(
asyncio.gather(*child_tasks, return_exceptions=True),
timeout=1.0
)
except asyncio.TimeoutError:
logger.warning(f"等待子任务取消超时: {stream_id}")
return success
except asyncio.CancelledError:
if chatter_task and not chatter_task.done():
chatter_task.cancel()
raise
except Exception as e:
logger.error(f"流处理异常: {stream_id} - {e}", exc_info=True)
# 异常时也要清理子任务
for child_task in child_tasks:
if not child_task.done():
child_task.cancel()
return False
finally:
# 清除 Chatter 处理标志
context.is_chatter_processing = False
logger.debug(f"清除 Chatter 处理标志: {stream_id}")
# 清理临时存储的任务引用
if hasattr(context, '_active_process_tasks'):
delattr(context, '_active_process_tasks')
if hasattr(context, '_chatter_task'):
delattr(context, '_chatter_task')
# 无论成功或失败,都要设置处理状态为未处理
self._set_stream_processing_status(stream_id, False)