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

@@ -8,6 +8,7 @@ from src.plugin_system.base.base_action import BaseAction
from src.plugin_system.base.base_chatter import BaseChatter
from src.plugin_system.base.base_command import BaseCommand
from src.plugin_system.base.base_events_handler import BaseEventHandler
from src.plugin_system.base.base_interest_calculator import BaseInterestCalculator
from src.plugin_system.base.base_tool import BaseTool
from src.plugin_system.base.component_types import (
ActionInfo,
@@ -16,6 +17,7 @@ from src.plugin_system.base.component_types import (
ComponentInfo,
ComponentType,
EventHandlerInfo,
InterestCalculatorInfo,
PluginInfo,
PlusCommandInfo,
ToolInfo,
@@ -162,8 +164,13 @@ class ComponentRegistry:
assert isinstance(component_info, ChatterInfo)
assert issubclass(component_class, BaseChatter)
ret = self._register_chatter_component(component_info, component_class)
case ComponentType.INTEREST_CALCULATOR:
assert isinstance(component_info, InterestCalculatorInfo)
assert issubclass(component_class, BaseInterestCalculator)
ret = self._register_interest_calculator_component(component_info, component_class)
case _:
logger.warning(f"未知组件类型: {component_type}")
ret = False
if not ret:
return False
@@ -311,6 +318,38 @@ class ComponentRegistry:
logger.debug(f"已注册Chatter组件: {chatter_name}")
return True
def _register_interest_calculator_component(
self, interest_calculator_info: "InterestCalculatorInfo", interest_calculator_class: type["BaseInterestCalculator"]
) -> bool:
"""注册InterestCalculator组件到特定注册表"""
calculator_name = interest_calculator_info.name
if not calculator_name:
logger.error(f"InterestCalculator组件 {interest_calculator_class.__name__} 必须指定名称")
return False
if not isinstance(interest_calculator_info, InterestCalculatorInfo) or not issubclass(interest_calculator_class, BaseInterestCalculator):
logger.error(f"注册失败: {calculator_name} 不是有效的InterestCalculator")
return False
# 创建专门的InterestCalculator注册表如果还没有
if not hasattr(self, "_interest_calculator_registry"):
self._interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {}
if not hasattr(self, "_enabled_interest_calculator_registry"):
self._enabled_interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {}
interest_calculator_class.plugin_name = interest_calculator_info.plugin_name
# 设置插件配置
interest_calculator_class.plugin_config = self.get_plugin_config(interest_calculator_info.plugin_name) or {}
self._interest_calculator_registry[calculator_name] = interest_calculator_class
if not interest_calculator_info.enabled:
logger.warning(f"InterestCalculator组件 {calculator_name} 未启用")
return True # 未启用,但是也是注册成功
self._enabled_interest_calculator_registry[calculator_name] = interest_calculator_class
logger.debug(f"已注册InterestCalculator组件: {calculator_name}")
return True
# === 组件移除相关 ===
async def remove_component(self, component_name: str, component_type: "ComponentType", plugin_name: str) -> bool:

View File

@@ -51,16 +51,28 @@ class ToolExecutor:
chat_id: 聊天标识符,用于日志记录
"""
self.chat_id = chat_id
self.chat_stream = get_chat_manager().get_stream(self.chat_id)
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.llm_model = LLMRequest(model_set=model_config.model_task_config.tool_use, request_type="tool_executor")
# 二步工具调用状态管理
self._pending_step_two_tools: dict[str, dict[str, Any]] = {}
"""待处理的第二步工具调用,格式为 {tool_name: step_two_definition}"""
self._log_prefix_initialized = False
logger.info(f"{self.log_prefix}工具执行器初始化完成")
# logger.info(f"{self.log_prefix}工具执行器初始化完成") # 移到异步初始化中
async def _initialize_log_prefix(self):
"""异步初始化log_prefix和chat_stream"""
if not self._log_prefix_initialized:
from src.chat.message_receive.chat_stream import get_chat_manager
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
logger.info(f"{self.log_prefix}工具执行器初始化完成")
async def execute_from_chat_message(
self, target_message: str, chat_history: str, sender: str, return_details: bool = False
@@ -77,6 +89,8 @@ class ToolExecutor:
如果return_details为False: Tuple[List[Dict], List[str], str] - (工具执行结果列表, 空, 空)
如果return_details为True: Tuple[List[Dict], List[str], str] - (结果列表, 使用的工具, 提示词)
"""
# 初始化log_prefix
await self._initialize_log_prefix()
# 获取可用工具
tools = self._get_tool_definitions()