refactor(chat): 异步化聊天系统并重构兴趣值计算机制

将同步调用改为异步调用以提升性能,重构兴趣值计算流程以支持更灵活的组件化架构。主要改进包括:

- 异步化ChatManager相关方法,避免阻塞主线程
- 重构兴趣值计算系统,从插件内部计算改为通过兴趣管理器统一处理
- 新增should_act字段支持更细粒度的动作决策
- 优化初始化逻辑,避免构造函数中的异步操作
- 扩展插件系统支持兴趣计算器组件注册
- 更新数据库模型以支持新的兴趣值相关字段

这些改进提升了系统的响应性能和可扩展性,同时保持了API的向后兼容性。
This commit is contained in:
Windpicker-owo
2025-10-05 01:25:52 +08:00
parent 49025a4973
commit 624298e1b8
38 changed files with 1493 additions and 259 deletions

View File

@@ -31,8 +31,9 @@ class ActionModifier:
def __init__(self, action_manager: ChatterActionManager, chat_id: str):
"""初始化动作处理器"""
self.chat_id = chat_id
self.chat_stream: ChatStream = get_chat_manager().get_stream(self.chat_id) # type: ignore
self.log_prefix = f"[{get_chat_manager().get_stream_name(self.chat_id) or self.chat_id}]"
# chat_stream 和 log_prefix 将在异步方法中初始化
self.chat_stream = None # type: ignore
self.log_prefix = f"[{chat_id}]"
self.action_manager = action_manager
@@ -43,6 +44,15 @@ class ActionModifier:
self._llm_judge_cache = {} # 缓存LLM判定结果
self._cache_expiry_time = 30 # 缓存过期时间(秒)
self._last_context_hash = None # 上次上下文的哈希值
self._log_prefix_initialized = False
async def _initialize_log_prefix(self):
"""异步初始化log_prefix和chat_stream"""
if not self._log_prefix_initialized:
self.chat_stream = await get_chat_manager().get_stream(self.chat_id)
stream_name = await get_chat_manager().get_stream_name(self.chat_id)
self.log_prefix = f"[{stream_name or self.chat_id}]"
self._log_prefix_initialized = True
async def modify_actions(
self,
@@ -57,6 +67,9 @@ class ActionModifier:
处理后ActionManager 将包含最终的可用动作集,供规划器直接使用
"""
# 初始化log_prefix
await self._initialize_log_prefix()
logger.debug(f"{self.log_prefix}开始完整动作修改流程")
removals_s1: list[tuple[str, str]] = []
@@ -72,7 +85,7 @@ class ActionModifier:
from src.plugin_system.core.component_registry import component_registry
# 获取聊天类型
is_group_chat, _ = get_chat_type_and_target_info(self.chat_id)
is_group_chat, _ = await get_chat_type_and_target_info(self.chat_id)
all_registered_actions = component_registry.get_components_by_type(ComponentType.ACTION)
chat_type_removals = []