feat(message-manager): 改进流生命周期管理和消息对象处理

-通过适当的任务取消为流循环添加强制重启功能
-通过更安全的删除和适当的任务终止来增强流清理
-改进亲和流聊天插件中的消息对象转换
-用DatabaseMessages对象替换基于字典的消息处理
-为任务取消添加超时处理,以防止死锁
-简化计划执行中的用户ID提取和消息ID处理
This commit is contained in:
Windpicker-owo
2025-10-31 21:27:11 +08:00
parent 655b4f20c6
commit ce03ced355
5 changed files with 73 additions and 53 deletions

View File

@@ -111,10 +111,23 @@ class StreamLoopManager:
logger.warning(f"无法获取流上下文: {stream_id}")
return False
# 快速路径:如果流已存在,无需处理
if context.stream_loop_task and not context.stream_loop_task.done():
# 快速路径:如果流已存在且不是强制启动,无需处理
if not force and context.stream_loop_task and not context.stream_loop_task.done():
logger.debug(f"{stream_id} 循环已在运行")
return True
# 如果是强制启动且任务仍在运行,先取消旧任务
if force and context.stream_loop_task and not context.stream_loop_task.done():
logger.info(f"强制启动模式:先取消现有流循环任务: {stream_id}")
old_task = context.stream_loop_task
old_task.cancel()
try:
await asyncio.wait_for(old_task, timeout=2.0)
logger.info(f"旧流循环任务已结束: {stream_id}")
except (asyncio.TimeoutError, asyncio.CancelledError):
logger.debug(f"旧流循环任务已取消或超时: {stream_id}")
except Exception as e:
logger.warning(f"等待旧任务结束时出错: {e}")
# 创建流循环任务
try: