refactor(message_manager): 重构消息分发机制为流循环模式

重构原有的动态消息分发管理器为流循环管理器,每个聊天流拥有独立的无限循环任务主动轮询处理消息。

主要变更:
- 移除 DistributionManager 及相关类(DistributionPriority、DistributionTask、StreamDistributionState、DistributionExecutor)
- 新增 StreamLoopManager 实现基于流的循环处理机制
- 修改 context_manager 和 message_manager 以适配新的流循环模式
- 优化 plan_filter.py 中的消息处理逻辑以适应新的数据格式

BREAKING CHANGE: 原有的分发管理器 API 已被移除,需要更新所有依赖分发功能的代码
This commit is contained in:
Windpicker-owo
2025-09-29 00:42:54 +08:00
parent 8b034e21c6
commit 903ab855bf
5 changed files with 284 additions and 1278 deletions

View File

@@ -13,7 +13,7 @@ from src.common.logger import get_logger
from src.config.config import global_config
from src.common.data_models.database_data_model import DatabaseMessages
from src.chat.energy_system import energy_manager
from .distribution_manager import distribution_manager
from .distribution_manager import stream_loop_manager
logger = get_logger("context_manager")
@@ -60,8 +60,9 @@ class SingleStreamContextManager:
self.last_access_time = time.time()
if not skip_energy_update:
await self._update_stream_energy()
distribution_manager.add_stream_message(self.stream_id, 1)
logger.debug(f"添加消息到单流上下文: {self.stream_id} (兴趣度: {interest_value:.3f})")
# 启动流的循环任务(如果还未启动)
await stream_loop_manager.start_stream_loop(self.stream_id)
logger.info(f"添加消息到单流上下文: {self.stream_id} (兴趣度: {interest_value:.3f})")
return True
except Exception as e:
logger.error(f"添加消息到单流上下文失败 {self.stream_id}: {e}", exc_info=True)
@@ -293,7 +294,8 @@ class SingleStreamContextManager:
if not skip_energy_update:
await self._update_stream_energy()
distribution_manager.add_stream_message(self.stream_id, 1)
# 启动流的循环任务(如果还未启动)
await stream_loop_manager.start_stream_loop(self.stream_id)
logger.debug(f"添加消息到单流上下文(异步): {self.stream_id} (兴趣度: {interest_value:.3f})")
return True
@@ -356,8 +358,8 @@ class SingleStreamContextManager:
stream_id=self.stream_id, messages=combined_messages, user_id=user_id
)
# 更新分发管理器
distribution_manager.update_stream_energy(self.stream_id, energy)
# 更新流循环管理器
# 注意能量更新会通过energy_manager自动同步到流循环管理器
except Exception as e:
logger.error(f"更新单流能量失败 {self.stream_id}: {e}")