合并BaseEventPlugin到BasePlugin,重写了components_registry,修正了统计输出

This commit is contained in:
UnCLAS-Prommer
2025-07-19 19:16:42 +08:00
parent 8468784e86
commit 8d20134cbb
14 changed files with 233 additions and 299 deletions

View File

@@ -7,7 +7,6 @@
from .base_plugin import BasePlugin
from .base_action import BaseAction
from .base_command import BaseCommand
from .base_event_plugin import BaseEventPlugin
from .base_events_handler import BaseEventHandler
from .component_types import (
ComponentType,
@@ -39,7 +38,6 @@ __all__ = [
"ConfigField",
"EventHandlerInfo",
"EventType",
"BaseEventPlugin",
"BaseEventHandler",
"MaiMessages",
]

View File

@@ -41,6 +41,7 @@ class BaseAction(ABC):
action_message: Optional[dict] = None,
**kwargs,
):
# sourcery skip: hoist-similar-statement-from-if, merge-else-if-into-elif, move-assign-in-block, swap-if-else-branches, swap-nested-ifs
"""初始化Action组件
Args:
@@ -355,7 +356,9 @@ class BaseAction(ABC):
# 从类属性读取名称,如果没有定义则使用类名自动生成
name = getattr(cls, "action_name", cls.__name__.lower().replace("action", ""))
if "." in name:
logger.error(f"Action名称 '{name}' 包含非法字符 '.',请使用下划线替代")
raise ValueError(f"Action名称 '{name}' 包含非法字符 '.',请使用下划线替代")
# 获取focus_activation_type和normal_activation_type
focus_activation_type = getattr(cls, "focus_activation_type", ActionActivationType.ALWAYS)
normal_activation_type = getattr(cls, "normal_activation_type", ActionActivationType.ALWAYS)

View File

@@ -219,7 +219,9 @@ class BaseCommand(ABC):
Returns:
CommandInfo: 生成的Command信息对象
"""
if "." in cls.command_name:
logger.error(f"Command名称 '{cls.command_name}' 包含非法字符 '.',请使用下划线替代")
raise ValueError(f"Command名称 '{cls.command_name}' 包含非法字符 '.',请使用下划线替代")
return CommandInfo(
name=cls.command_name,
component_type=ComponentType.COMMAND,

View File

@@ -1,57 +0,0 @@
from abc import abstractmethod
from typing import List, Tuple, Type
from src.common.logger import get_logger
from .plugin_base import PluginBase
from .component_types import EventHandlerInfo
from .base_events_handler import BaseEventHandler
logger = get_logger("base_event_plugin")
class BaseEventPlugin(PluginBase):
"""基于事件的插件基类
所有事件类型的插件都应该继承这个基类
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@abstractmethod
def get_plugin_components(self) -> List[Tuple[EventHandlerInfo, Type[BaseEventHandler]]]:
"""获取插件包含的事件组件
子类必须实现此方法,返回事件组件
Returns:
List[Tuple[ComponentInfo, Type]]: [(组件信息, 组件类), ...]
"""
raise NotImplementedError("子类必须实现 get_plugin_components 方法")
def register_plugin(self) -> bool:
"""注册事件插件"""
from src.plugin_system.core.events_manager import events_manager
components = self.get_plugin_components()
# 检查依赖
if not self._check_dependencies():
logger.error(f"{self.log_prefix} 依赖检查失败,跳过注册")
return False
registered_components = []
for handler_info, handler_class in components:
handler_info.plugin_name = self.plugin_name
if events_manager.register_event_subscriber(handler_info, handler_class):
registered_components.append(handler_info)
else:
logger.error(f"{self.log_prefix} 事件处理器 {handler_info.name} 注册失败")
self.plugin_info.components = registered_components
if events_manager.register_plugins(self.plugin_info):
logger.debug(f"{self.log_prefix} 插件注册成功,包含 {len(registered_components)} 个事件处理器")
return True
else:
logger.error(f"{self.log_prefix} 插件注册失败")
return False

View File

@@ -38,9 +38,12 @@ class BaseEventHandler(ABC):
"""获取事件处理器的信息"""
# 从类属性读取名称,如果没有定义则使用类名自动生成
name: str = getattr(cls, "handler_name", cls.__name__.lower().replace("handler", ""))
if "." in name:
logger.error(f"事件处理器名称 '{name}' 包含非法字符 '.',请使用下划线替代")
raise ValueError(f"事件处理器名称 '{name}' 包含非法字符 '.',请使用下划线替代")
return EventHandlerInfo(
name=name,
component_type=ComponentType.LISTENER,
component_type=ComponentType.EVENT_HANDLER,
description=getattr(cls, "handler_description", "events处理器"),
event_type=cls.event_type,
weight=cls.weight,

View File

@@ -1,9 +1,12 @@
from abc import abstractmethod
from typing import List, Type
from typing import List, Type, Tuple, Union
from .plugin_base import PluginBase
from src.common.logger import get_logger
from src.plugin_system.base.component_types import ComponentInfo
from src.plugin_system.base.component_types import ComponentInfo, ActionInfo, CommandInfo, EventHandlerInfo
from .base_action import BaseAction
from .base_command import BaseCommand
from .base_events_handler import BaseEventHandler
logger = get_logger("base_plugin")
@@ -21,7 +24,15 @@ class BasePlugin(PluginBase):
super().__init__(*args, **kwargs)
@abstractmethod
def get_plugin_components(self) -> List[tuple[ComponentInfo, Type]]:
def get_plugin_components(
self,
) -> List[
Union[
Tuple[ActionInfo, Type[BaseAction]],
Tuple[CommandInfo, Type[BaseCommand]],
Tuple[EventHandlerInfo, Type[BaseEventHandler]],
]
]:
"""获取插件包含的组件列表
子类必须实现此方法,返回组件信息和组件类的列表

View File

@@ -11,7 +11,7 @@ class ComponentType(Enum):
ACTION = "action" # 动作组件
COMMAND = "command" # 命令组件
SCHEDULER = "scheduler" # 定时任务组件(预留)
LISTENER = "listener" # 事件监听组件(预留)
EVENT_HANDLER = "event_handler" # 事件处理组件(预留)
def __str__(self) -> str:
return self.value
@@ -161,7 +161,7 @@ class EventHandlerInfo(ComponentInfo):
def __post_init__(self):
super().__post_init__()
self.component_type = ComponentType.LISTENER
self.component_type = ComponentType.EVENT_HANDLER
@dataclass