refactor(chat): 异步化聊天系统并重构兴趣值计算机制
将同步调用改为异步调用以提升性能,重构兴趣值计算流程以支持更灵活的组件化架构。主要改进包括: - 异步化ChatManager相关方法,避免阻塞主线程 - 重构兴趣值计算系统,从插件内部计算改为通过兴趣管理器统一处理 - 新增should_act字段支持更细粒度的动作决策 - 优化初始化逻辑,避免构造函数中的异步操作 - 扩展插件系统支持兴趣计算器组件注册 - 更新数据库模型以支持新的兴趣值相关字段 这些改进提升了系统的响应性能和可扩展性,同时保持了API的向后兼容性。
This commit is contained in:
@@ -58,16 +58,24 @@ class RelationshipBuilder:
|
||||
# 最后清理时间,用于定期清理老消息段
|
||||
self.last_cleanup_time = 0.0
|
||||
|
||||
# 获取聊天名称用于日志
|
||||
try:
|
||||
chat_name = get_chat_manager().get_stream_name(self.chat_id)
|
||||
self.log_prefix = f"[{chat_name}]"
|
||||
except Exception:
|
||||
self.log_prefix = f"[{self.chat_id}]"
|
||||
# log_prefix 将在异步方法中初始化
|
||||
self.log_prefix = f"[{self.chat_id}]"
|
||||
self._log_prefix_initialized = False
|
||||
|
||||
# 加载持久化的缓存
|
||||
self._load_cache()
|
||||
|
||||
async def _initialize_log_prefix(self):
|
||||
"""异步初始化log_prefix"""
|
||||
if not self._log_prefix_initialized:
|
||||
try:
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
chat_name = await get_chat_manager().get_stream_name(self.chat_id)
|
||||
self.log_prefix = f"[{chat_name}]"
|
||||
except Exception:
|
||||
self.log_prefix = f"[{self.chat_id}]"
|
||||
self._log_prefix_initialized = True
|
||||
|
||||
# ================================
|
||||
# 缓存管理模块
|
||||
# 负责持久化存储、状态管理、缓存读写
|
||||
@@ -339,6 +347,9 @@ class RelationshipBuilder:
|
||||
"""构建关系
|
||||
immediate_build: 立即构建关系,可选值为"all"或person_id
|
||||
"""
|
||||
# 初始化log_prefix
|
||||
await self._initialize_log_prefix()
|
||||
|
||||
self._cleanup_old_segments()
|
||||
current_time = time.time()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user