diff --git a/changes.md b/changes.md index 4986e4d60..1f53d7e50 100644 --- a/changes.md +++ b/changes.md @@ -20,4 +20,7 @@ - `chat_api.py`中获取流的参数中可以使用一个特殊的枚举类型来获得所有平台的 ChatStream 了。 - `config_api.py`中的`get_global_config`和`get_plugin_config`方法现在支持嵌套访问的配置键名。 - `database_api.py`中的`db_query`方法调整了参数顺序以增强参数限制的同时,保证了typing正确;`db_get`方法增加了`single_result`参数,与`db_query`保持一致。 -4. 现在增加了参数类型检查,完善了对应注释 \ No newline at end of file +4. 现在增加了参数类型检查,完善了对应注释 +5. 现在插件抽象出了总基类 `PluginBase` + - 基于`Action`和`Command`的插件基类现在为`BasePlugin`,它继承自`PluginBase`,由`register_plugin`装饰器注册。 + - 基于`Event`的插件基类现在为`BaseEventPlugin`,它也继承自`PluginBase`,由`register_event_plugin`装饰器注册。 \ No newline at end of file diff --git a/src/plugin_system/apis/plugin_register_api.py b/src/plugin_system/apis/plugin_register_api.py index b3cc58450..7970f3421 100644 --- a/src/plugin_system/apis/plugin_register_api.py +++ b/src/plugin_system/apis/plugin_register_api.py @@ -34,7 +34,4 @@ def register_event_plugin(cls, *args, **kwargs): 用法: @register_event_plugin - class MyEventPlugin: - event_type = EventType.MESSAGE_RECEIVED - ... """ \ No newline at end of file diff --git a/src/plugin_system/base/base_event_plugin.py b/src/plugin_system/base/base_event_plugin.py index 2261fee26..859d43f06 100644 --- a/src/plugin_system/base/base_event_plugin.py +++ b/src/plugin_system/base/base_event_plugin.py @@ -1,18 +1,14 @@ -from abc import ABC, abstractmethod +from abc import abstractmethod -class BaseEventsPlugin(ABC): +from .plugin_base import PluginBase +from src.common.logger import get_logger + + +class BaseEventPlugin(PluginBase): + """基于事件的插件基类 + + 所有事件类型的插件都应该继承这个基类 """ - 事件触发型插件基类 - - 所有事件触发型插件都应该继承这个基类而不是 BasePlugin - """ - - @property - @abstractmethod - def plugin_name(self) -> str: - return "" # 插件内部标识符(如 "hello_world_plugin") - - @property - @abstractmethod - def enable_plugin(self) -> bool: - return False \ No newline at end of file + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) diff --git a/src/plugin_system/base/base_plugin.py b/src/plugin_system/base/base_plugin.py index fe79d8e9a..a93de5fab 100644 --- a/src/plugin_system/base/base_plugin.py +++ b/src/plugin_system/base/base_plugin.py @@ -7,10 +7,19 @@ from src.plugin_system.base.component_types import ComponentInfo logger = get_logger("base_plugin") + class BasePlugin(PluginBase): + """基于Action和Command的插件基类 + + 所有上述类型的插件都应该继承这个基类,一个插件可以包含多种组件: + - Action组件:处理聊天中的动作 + - Command组件:处理命令请求 + - 未来可扩展:Scheduler、Listener等 + """ + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - + @abstractmethod def get_plugin_components(self) -> List[tuple[ComponentInfo, Type]]: """获取插件包含的组件列表 @@ -21,7 +30,7 @@ class BasePlugin(PluginBase): List[tuple[ComponentInfo, Type]]: [(组件信息, 组件类), ...] """ raise NotImplementedError("Subclasses must implement this method") - + def register_plugin(self) -> bool: """注册插件及其所有组件""" from src.plugin_system.core.component_registry import component_registry diff --git a/src/plugin_system/base/plugin_base.py b/src/plugin_system/base/plugin_base.py index ceb8dcb61..0b7f15d17 100644 --- a/src/plugin_system/base/plugin_base.py +++ b/src/plugin_system/base/plugin_base.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import Dict, List, Type, Any, Union +from typing import Dict, List, Any, Union import os import inspect import toml @@ -10,7 +10,6 @@ import datetime from src.common.logger import get_logger from src.plugin_system.base.component_types import ( PluginInfo, - ComponentInfo, PythonDependency, ) from src.plugin_system.base.config_types import ConfigField @@ -20,12 +19,9 @@ logger = get_logger("plugin_base") class PluginBase(ABC): - """插件基类 + """插件总基类 - 所有插件都应该继承这个基类,一个插件可以包含多种组件: - - Action组件:处理聊天中的动作 - - Command组件:处理命令请求 - - 未来可扩展:Scheduler、Listener等 + 所有衍生插件基类都应该继承自此类,这个类定义了插件的基本结构和行为。 """ # 插件基本信息(子类必须定义)