refactor(plugin): 引入旧版Command兼容层并重构基类

为了平滑过渡到新的`PlusCommand`插件架构,本次重构引入了一个兼容层。

`BaseCommand`现在继承自`PlusCommand`,并剥离了大部分重复的功能实现(如消息发送、配置获取等),转而依赖`PlusCommand`的基类实现。这大大简化了`BaseCommand`,使其专注于作为旧版插件的兼容适配器。

在组件注册流程中,增加了对旧版`BaseCommand`的识别。当检测到旧版命令时,会自动使用`create_legacy_command_adapter`工厂函数将其包装成一个标准的`PlusCommand`实例。这使得旧插件无需修改代码即可在新架构下运行,同时会在启动时打印警告,鼓励开发者迁移。
This commit is contained in:
minecraft1024a
2025-11-01 16:12:02 +08:00
committed by Windpicker-owo
parent fc3aea9fd5
commit d7f8a8de26
3 changed files with 91 additions and 243 deletions

View File

@@ -26,7 +26,7 @@ from src.plugin_system.base.component_types import (
PromptInfo,
ToolInfo,
)
from src.plugin_system.base.plus_command import PlusCommand
from src.plugin_system.base.plus_command import PlusCommand, create_legacy_command_adapter
logger = get_logger("component_registry")
@@ -221,25 +221,18 @@ class ComponentRegistry:
def _register_command_component(self, command_info: CommandInfo, command_class: type[BaseCommand]) -> bool:
"""注册Command组件到Command特定注册表"""
if not (command_name := command_info.name):
logger.error(f"Command组件 {command_class.__name__} 必须指定名称")
return False
if not isinstance(command_info, CommandInfo) or not issubclass(command_class, BaseCommand):
logger.error(f"注册失败: {command_name} 不是有效的Command")
return False
_assign_plugin_attrs(
command_class, command_info.plugin_name, self.get_plugin_config(command_info.plugin_name) or {}
)
self._command_registry[command_name] = command_class
if command_info.enabled and command_info.command_pattern:
pattern = re.compile(command_info.command_pattern, re.IGNORECASE | re.DOTALL)
if pattern not in self._command_patterns:
self._command_patterns[pattern] = command_name
else:
logger.warning(
f"'{command_name}' 对应的命令模式与 '{self._command_patterns[pattern]}' 重复,忽略此命令"
)
return True
# 检查是否为旧版Command
if getattr(command_class, "_is_legacy", False):
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 # 继承插件名
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]