refactor(chat): 重构消息管理器以使用集中式聊天流API

移除对context.chat_stream的直接依赖,改为通过get_chat_manager().get_stream()统一获取聊天流实例。这提高了模块独立性,符合"高内聚、低耦合"原则。

- 在MessageManager中统一使用chat_api获取stream实例
- 移除mood_manager中直接更新chat_stream的逻辑
- 在affinity_flow_chatter中统一处理兴趣度更新
- 消除直接属性访问带来的强耦合依赖
This commit is contained in:
Windpicker-owo
2025-09-26 11:45:20 +08:00
parent a2baec088e
commit ad36a6c48f
3 changed files with 29 additions and 24 deletions

View File

@@ -314,8 +314,10 @@ class MessageManager:
stream_count = 0
for context in active_streams:
if hasattr(context, 'chat_stream') and context.chat_stream:
focus_energy = context.chat_stream.focus_energy
from src.plugin_system.apis.chat_api import get_chat_manager
chat_stream = get_chat_manager().get_stream(context.stream_id)
if chat_stream:
focus_energy = chat_stream.focus_energy
total_focus_energy += focus_energy
max_focus_energy = max(max_focus_energy, focus_energy)
stream_count += 1
@@ -364,16 +366,18 @@ class MessageManager:
"""计算单个聊天流的分发周期 - 基于阈值感知的focus_energy"""
if not global_config.chat.dynamic_distribution_enabled:
return self.check_interval # 使用固定间隔
from src.plugin_system.apis.chat_api import get_chat_manager
chat_stream = get_chat_manager().get_stream(context.stream_id)
# 获取该流的focus_energy新的阈值感知版本
focus_energy = 0.5 # 默认值
avg_message_interest = 0.5 # 默认平均兴趣度
if hasattr(context, 'chat_stream') and context.chat_stream:
focus_energy = context.chat_stream.focus_energy
if chat_stream:
focus_energy = chat_stream.focus_energy
# 获取平均消息兴趣度用于更精确的计算
if context.chat_stream.message_count > 0:
avg_message_interest = context.chat_stream.message_interest_total / context.chat_stream.message_count
if chat_stream.message_count > 0:
avg_message_interest = chat_stream.message_interest_total / chat_stream.message_count
# 获取AFC阈值用于参考添加None值检查
reply_threshold = getattr(global_config.affinity_flow, 'reply_action_interest_threshold', 0.4)
@@ -492,7 +496,9 @@ class MessageManager:
# 如果没有处理任务,创建一个
if not context.processing_task or context.processing_task.done():
focus_energy = context.chat_stream.focus_energy if hasattr(context, 'chat_stream') and context.chat_stream else 0.5
from src.plugin_system.apis.chat_api import get_chat_manager
chat_stream = get_chat_manager().get_stream(context.stream_id)
focus_energy = chat_stream.focus_energy if chat_stream else 0.5
# 根据优先级记录日志
if focus_energy >= 0.7:
@@ -533,9 +539,11 @@ class MessageManager:
continue
# 获取focus_energy如果不存在则使用默认值
from src.plugin_system.apis.chat_api import get_chat_manager
chat_stream = get_chat_manager().get_stream(context.stream_id)
focus_energy = 0.5
if hasattr(context, 'chat_stream') and context.chat_stream:
focus_energy = context.chat_stream.focus_energy
if chat_stream:
focus_energy = chat_stream.focus_energy
# 计算流优先级分数
priority_score = self._calculate_stream_priority(context, focus_energy)
@@ -574,6 +582,8 @@ class MessageManager:
def _calculate_stream_priority(self, context: StreamContext, focus_energy: float) -> float:
"""计算聊天流的优先级分数"""
from src.plugin_system.apis.chat_api import get_chat_manager
chat_stream = get_chat_manager().get_stream(context.stream_id)
# 基础优先级focus_energy
base_priority = focus_energy
@@ -587,8 +597,8 @@ class MessageManager:
time_penalty = max(0, 1.0 - time_since_active / 3600.0) # 1小时内无惩罚
# 连续无回复惩罚
if hasattr(context, 'chat_stream') and context.chat_stream:
consecutive_no_reply = context.chat_stream.consecutive_no_reply
if chat_stream:
consecutive_no_reply = chat_stream.consecutive_no_reply
no_reply_penalty = max(0, 1.0 - consecutive_no_reply * 0.05) # 每次无回复降低5%
else:
no_reply_penalty = 1.0