refactor(chat): 异步化聊天系统并重构兴趣值计算机制
将同步调用改为异步调用以提升性能,重构兴趣值计算流程以支持更灵活的组件化架构。主要改进包括: - 异步化ChatManager相关方法,避免阻塞主线程 - 重构兴趣值计算系统,从插件内部计算改为通过兴趣管理器统一处理 - 新增should_act字段支持更细粒度的动作决策 - 优化初始化逻辑,避免构造函数中的异步操作 - 扩展插件系统支持兴趣计算器组件注册 - 更新数据库模型以支持新的兴趣值相关字段 这些改进提升了系统的响应性能和可扩展性,同时保持了API的向后兼容性。
This commit is contained in:
@@ -1,11 +1,20 @@
|
||||
from abc import abstractmethod
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.plugin_system.base.component_types import ActionInfo, CommandInfo, EventHandlerInfo, PlusCommandInfo, ToolInfo
|
||||
from src.plugin_system.base.component_types import (
|
||||
ActionInfo,
|
||||
CommandInfo,
|
||||
ComponentType,
|
||||
EventHandlerInfo,
|
||||
InterestCalculatorInfo,
|
||||
PlusCommandInfo,
|
||||
ToolInfo,
|
||||
)
|
||||
|
||||
from .base_action import BaseAction
|
||||
from .base_command import BaseCommand
|
||||
from .base_events_handler import BaseEventHandler
|
||||
from .base_interest_calculator import BaseInterestCalculator
|
||||
from .base_tool import BaseTool
|
||||
from .plugin_base import PluginBase
|
||||
from .plus_command import PlusCommand
|
||||
@@ -21,6 +30,72 @@ class BasePlugin(PluginBase):
|
||||
- Command组件:处理命令请求
|
||||
- 未来可扩展:Scheduler、Listener等
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def _get_component_info_from_class(cls, component_class: type, component_type: ComponentType):
|
||||
"""从组件类自动生成组件信息
|
||||
|
||||
Args:
|
||||
component_class: 组件类
|
||||
component_type: 组件类型
|
||||
|
||||
Returns:
|
||||
对应类型的ComponentInfo对象
|
||||
"""
|
||||
if component_type == ComponentType.COMMAND:
|
||||
if hasattr(component_class, 'get_command_info'):
|
||||
return component_class.get_command_info()
|
||||
else:
|
||||
logger.warning(f"Command类 {component_class.__name__} 缺少 get_command_info 方法")
|
||||
return None
|
||||
|
||||
elif component_type == ComponentType.ACTION:
|
||||
if hasattr(component_class, 'get_action_info'):
|
||||
return component_class.get_action_info()
|
||||
else:
|
||||
logger.warning(f"Action类 {component_class.__name__} 缺少 get_action_info 方法")
|
||||
return None
|
||||
|
||||
elif component_type == ComponentType.INTEREST_CALCULATOR:
|
||||
if hasattr(component_class, 'get_interest_calculator_info'):
|
||||
return component_class.get_interest_calculator_info()
|
||||
else:
|
||||
logger.warning(f"InterestCalculator类 {component_class.__name__} 缺少 get_interest_calculator_info 方法")
|
||||
return None
|
||||
|
||||
elif component_type == ComponentType.PLUS_COMMAND:
|
||||
# PlusCommand的get_info逻辑可以在这里实现
|
||||
logger.warning("PlusCommand的get_info逻辑尚未实现")
|
||||
return None
|
||||
|
||||
elif component_type == ComponentType.TOOL:
|
||||
# Tool的get_info逻辑可以在这里实现
|
||||
logger.warning("Tool的get_info逻辑尚未实现")
|
||||
return None
|
||||
|
||||
elif component_type == ComponentType.EVENT_HANDLER:
|
||||
# EventHandler的get_info逻辑可以在这里实现
|
||||
logger.warning("EventHandler的get_info逻辑尚未实现")
|
||||
return None
|
||||
|
||||
else:
|
||||
logger.error(f"不支持的组件类型: {component_type}")
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def get_component_info(cls, component_class: type, component_type: ComponentType):
|
||||
"""获取组件信息的通用方法
|
||||
|
||||
这是一个便捷方法,内部调用_get_component_info_from_class
|
||||
|
||||
Args:
|
||||
component_class: 组件类
|
||||
component_type: 组件类型
|
||||
|
||||
Returns:
|
||||
对应类型的ComponentInfo对象
|
||||
"""
|
||||
return cls._get_component_info_from_class(component_class, component_type)
|
||||
@abstractmethod
|
||||
def get_plugin_components(
|
||||
self,
|
||||
@@ -30,6 +105,7 @@ class BasePlugin(PluginBase):
|
||||
| tuple[PlusCommandInfo, type[PlusCommand]]
|
||||
| tuple[EventHandlerInfo, type[BaseEventHandler]]
|
||||
| tuple[ToolInfo, type[BaseTool]]
|
||||
| tuple[InterestCalculatorInfo, type[BaseInterestCalculator]]
|
||||
]:
|
||||
"""获取插件包含的组件列表
|
||||
|
||||
|
||||
Reference in New Issue
Block a user