refactor(plugin_system): 移除静态PlusCommandAdapter并优化命令注册逻辑
移除了不再需要的静态`PlusCommandAdapter`类,该类最初用于将`PlusCommand`适配到旧的`BaseCommand`系统。 随着插件系统重构,所有命令(包括旧版命令)现在都统一通过动态创建的适配器在`ComponentRegistry`中转换为`PlusCommand`进行注册和处理。此更改简化了`plus_command.py`模块,消除了冗余代码,并使命令注册流程更加清晰和一致。 主要变更: - 从`plus_command.py`中删除了`PlusCommandAdapter`类。 - 调整了`ComponentRegistry`中的日志记录逻辑,现在所有旧版命令都会被明确标记并适配,无需之前的条件检查。 - 相应地更新了相关模块的导入语句。
This commit is contained in:
committed by
Windpicker-owo
parent
141ef2d6ab
commit
2f22246829
@@ -43,7 +43,6 @@ from .base import (
|
||||
PluginInfo,
|
||||
# 新增的增强命令系统
|
||||
PlusCommand,
|
||||
PlusCommandAdapter,
|
||||
PythonDependency,
|
||||
ToolInfo,
|
||||
ToolParamType,
|
||||
|
||||
@@ -29,7 +29,7 @@ from .component_types import (
|
||||
ToolParamType,
|
||||
)
|
||||
from .config_types import ConfigField
|
||||
from .plus_command import PlusCommand, PlusCommandAdapter, create_plus_command_adapter
|
||||
from .plus_command import PlusCommand, create_plus_command_adapter
|
||||
|
||||
__all__ = [
|
||||
"ActionActivationType",
|
||||
|
||||
@@ -11,7 +11,6 @@ from src.common.data_models.database_data_model import DatabaseMessages
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
from src.plugin_system.apis import send_api
|
||||
from src.plugin_system.base.base_command import BaseCommand
|
||||
from src.plugin_system.base.command_args import CommandArgs
|
||||
from src.plugin_system.base.component_types import ChatType, ComponentType, PlusCommandInfo
|
||||
|
||||
@@ -337,54 +336,6 @@ class PlusCommand(ABC):
|
||||
return pattern
|
||||
|
||||
|
||||
class PlusCommandAdapter(BaseCommand):
|
||||
"""PlusCommand适配器
|
||||
|
||||
将PlusCommand适配到现有的插件系统,继承BaseCommand
|
||||
"""
|
||||
|
||||
def __init__(self, plus_command_class, message: DatabaseMessages, plugin_config: dict | None = None):
|
||||
"""初始化适配器
|
||||
|
||||
Args:
|
||||
plus_command_class: PlusCommand子类
|
||||
message: 消息对象(DatabaseMessages)
|
||||
plugin_config: 插件配置
|
||||
"""
|
||||
# 先设置必要的类属性
|
||||
self.command_name = plus_command_class.command_name
|
||||
self.command_description = plus_command_class.command_description
|
||||
self.command_pattern = plus_command_class._generate_command_pattern()
|
||||
self.chat_type_allow = getattr(plus_command_class, "chat_type_allow", ChatType.ALL)
|
||||
self.priority = getattr(plus_command_class, "priority", 0)
|
||||
self.intercept_message = getattr(plus_command_class, "intercept_message", False)
|
||||
|
||||
# 调用父类初始化
|
||||
super().__init__(message, plugin_config)
|
||||
|
||||
# 创建PlusCommand实例
|
||||
self.plus_command = plus_command_class(message, plugin_config)
|
||||
|
||||
async def execute(self) -> tuple[bool, str | None, bool]:
|
||||
"""执行命令
|
||||
|
||||
Returns:
|
||||
Tuple[bool, Optional[str], bool]: 执行结果
|
||||
"""
|
||||
# 检查命令是否匹配
|
||||
if not self.plus_command.is_command_match():
|
||||
return False, "命令不匹配", False
|
||||
|
||||
# 检查聊天类型权限
|
||||
if not self.plus_command.is_chat_type_allowed():
|
||||
return False, "不支持当前聊天类型", self.intercept_message
|
||||
|
||||
# 执行命令
|
||||
try:
|
||||
return await self.plus_command.execute(self.plus_command.args)
|
||||
except Exception as e:
|
||||
logger.error(f"执行命令时出错: {e}", exc_info=True)
|
||||
return False, f"命令执行出错: {e!s}", self.intercept_message
|
||||
|
||||
|
||||
def create_plus_command_adapter(plus_command_class):
|
||||
@@ -396,7 +347,7 @@ def create_plus_command_adapter(plus_command_class):
|
||||
Returns:
|
||||
适配器类
|
||||
"""
|
||||
|
||||
from src.plugin_system.base.base_command import BaseCommand
|
||||
class AdapterClass(BaseCommand):
|
||||
command_name = plus_command_class.command_name
|
||||
command_description = plus_command_class.command_description
|
||||
|
||||
@@ -221,18 +221,16 @@ class ComponentRegistry:
|
||||
|
||||
def _register_command_component(self, command_info: CommandInfo, command_class: type[BaseCommand]) -> bool:
|
||||
"""注册Command组件到Command特定注册表"""
|
||||
# 检查是否为旧版Command
|
||||
if getattr(command_class, "_is_legacy", False):
|
||||
logger.warning(
|
||||
logger.warning(
|
||||
f"检测到旧版Command组件 '{command_class.command_name}' (来自插件: {command_info.plugin_name})。"
|
||||
"它将通过兼容层运行,但建议尽快迁移到PlusCommand以获得更好的性能和功能。"
|
||||
)
|
||||
# 使用适配器将其转换为PlusCommand
|
||||
adapted_class = create_legacy_command_adapter(command_class)
|
||||
plus_command_info = adapted_class.get_plus_command_info()
|
||||
plus_command_info.plugin_name = command_info.plugin_name # 继承插件名
|
||||
# 使用适配器将其转换为PlusCommand
|
||||
adapted_class = create_legacy_command_adapter(command_class)
|
||||
plus_command_info = adapted_class.get_plus_command_info()
|
||||
plus_command_info.plugin_name = command_info.plugin_name # 继承插件名
|
||||
|
||||
return self._register_plus_command_component(plus_command_info, adapted_class)
|
||||
return self._register_plus_command_component(plus_command_info, adapted_class)
|
||||
|
||||
def _register_plus_command_component(
|
||||
self, plus_command_info: PlusCommandInfo, plus_command_class: type[PlusCommand]
|
||||
|
||||
Reference in New Issue
Block a user