From 460e8b9c8ff88840660add6bae86dfeac201bcbf Mon Sep 17 00:00:00 2001 From: PyuraMazo <1605025385@qq.com> Date: Sun, 22 Feb 2026 18:50:12 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=88=90=E5=92=8C?= =?UTF-8?q?=E5=8D=B8=E8=BD=BD=E5=AE=8C=E6=88=90=E7=9A=84=E9=92=A9=E5=AD=90?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/api/event/filter/__init__.py | 4 +++ astrbot/core/star/register/__init__.py | 4 +++ astrbot/core/star/register/star_handler.py | 35 ++++++++++++++++++++++ astrbot/core/star/star_handler.py | 4 +++ astrbot/core/star/star_manager.py | 28 ++++++++++++++++- 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/astrbot/api/event/filter/__init__.py b/astrbot/api/event/filter/__init__.py index 7354ec766c..f7199d8656 100644 --- a/astrbot/api/event/filter/__init__.py +++ b/astrbot/api/event/filter/__init__.py @@ -35,6 +35,8 @@ ) from astrbot.core.star.register import register_regex as regex +from astrbot.core.star.register import register_on_plugin_loaded as on_plugin_loaded +from astrbot.core.star.register import register_on_plugin_unloaded as on_plugin_unloaded __all__ = [ "CustomFilter", "EventMessageType", @@ -54,6 +56,8 @@ "on_llm_request", "on_llm_response", "on_plugin_error", + "on_plugin_loaded", + "on_plugin_unloaded", "on_platform_loaded", "on_waiting_llm_request", "permission_type", diff --git a/astrbot/core/star/register/__init__.py b/astrbot/core/star/register/__init__.py index f1daf2968b..5e99948cd2 100644 --- a/astrbot/core/star/register/__init__.py +++ b/astrbot/core/star/register/__init__.py @@ -14,6 +14,8 @@ register_on_llm_tool_respond, register_on_platform_loaded, register_on_plugin_error, + register_on_plugin_loaded, + register_on_plugin_unloaded, register_on_using_llm_tool, register_on_waiting_llm_request, register_permission_type, @@ -34,6 +36,8 @@ "register_on_llm_request", "register_on_llm_response", "register_on_plugin_error", + "register_on_plugin_loaded", + "register_on_plugin_unloaded", "register_on_platform_loaded", "register_on_waiting_llm_request", "register_permission_type", diff --git a/astrbot/core/star/register/star_handler.py b/astrbot/core/star/register/star_handler.py index c4ed0d4a7e..bf2686c261 100644 --- a/astrbot/core/star/register/star_handler.py +++ b/astrbot/core/star/register/star_handler.py @@ -357,6 +357,41 @@ def decorator(awaitable): return decorator +def register_on_plugin_loaded(**kwargs): + """当有插件加载完成时 + + Hook 参数: + metadata + + 说明: + 当有插件加载完成时,触发该事件并获取到该插件的元数据 + """ + + def decorator(awaitable): + _ = get_handler_or_create(awaitable, EventType.OnPluginLoadedEvent, **kwargs) + return awaitable + + return decorator + + +def register_on_plugin_unloaded(**kwargs): + """当有插件卸载完成时 + + Hook 参数: + metadata + + 说明: + 当有插件卸载完成时,触发该事件并获取到该插件的元数据 + """ + + def decorator(awaitable): + _ = get_handler_or_create(awaitable, EventType.OnPluginUnloadedEvent, **kwargs) + return awaitable + + return decorator + + + def register_on_waiting_llm_request(**kwargs): """当等待调用 LLM 时的通知事件(在获取锁之前) diff --git a/astrbot/core/star/star_handler.py b/astrbot/core/star/star_handler.py index 63b0c447de..3b41a6f366 100644 --- a/astrbot/core/star/star_handler.py +++ b/astrbot/core/star/star_handler.py @@ -144,6 +144,8 @@ def get_handlers_by_event_type( not in ( EventType.OnAstrBotLoadedEvent, EventType.OnPlatformLoadedEvent, + EventType.OnPluginLoadedEvent, + EventType.OnPluginUnloadedEvent, ) and not plugin.reserved ): @@ -201,6 +203,8 @@ class EventType(enum.Enum): OnLLMToolRespondEvent = enum.auto() # 调用函数工具后 OnAfterMessageSentEvent = enum.auto() # 发送消息后 OnPluginErrorEvent = enum.auto() # 插件处理消息异常时 + OnPluginLoadedEvent = enum.auto() # 插件加载完成 + OnPluginUnloadedEvent = enum.auto() # 插件卸载完成 H = TypeVar("H", bound=Callable[..., Any]) diff --git a/astrbot/core/star/star_manager.py b/astrbot/core/star/star_manager.py index 93512bde21..1c64adc706 100644 --- a/astrbot/core/star/star_manager.py +++ b/astrbot/core/star/star_manager.py @@ -33,7 +33,7 @@ from .context import Context from .filter.permission import PermissionType, PermissionTypeFilter from .star import star_map, star_registry -from .star_handler import star_handlers_registry +from .star_handler import star_handlers_registry, EventType from .updator import PluginUpdator try: @@ -772,6 +772,19 @@ async def load( if hasattr(metadata.star_cls, "initialize") and metadata.star_cls: await metadata.star_cls.initialize() + # 触发插件加载事件 + handlers = star_handlers_registry.get_handlers_by_event_type( + EventType.OnPluginLoadedEvent, + ) + for handler in handlers: + try: + logger.info( + f"hook(on_plugin_loaded) -> {star_map[handler.handler_module_path].name} - {handler.handler_name}", + ) + await handler.handler(metadata) + except Exception: + logger.error(traceback.format_exc()) + except BaseException as e: logger.error(f"----- 插件 {root_dir_name} 载入失败 -----") errors = traceback.format_exc() @@ -1159,6 +1172,19 @@ async def _terminate_plugin(star_metadata: StarMetadata) -> None: elif "terminate" in star_metadata.star_cls_type.__dict__: await star_metadata.star_cls.terminate() + # 触发插件卸载事件 + handlers = star_handlers_registry.get_handlers_by_event_type( + EventType.OnPluginUnloadedEvent, + ) + for handler in handlers: + try: + logger.info( + f"hook(on_plugin_loaded) -> {star_map[handler.handler_module_path].name} - {handler.handler_name}", + ) + await handler.handler(star_metadata) + except Exception: + logger.error(traceback.format_exc()) + async def turn_on_plugin(self, plugin_name: str) -> None: plugin = self.context.get_registered_star(plugin_name) if plugin is None: From 32f11d92b982c9b409a0fd7f86c1d67e5969a7cd Mon Sep 17 00:00:00 2001 From: PyuraMazo <1605025385@qq.com> Date: Sun, 22 Feb 2026 20:38:14 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=88=90=E5=92=8C?= =?UTF-8?q?=E5=8D=B8=E8=BD=BD=E5=AE=8C=E6=88=90=E7=9A=84=E9=92=A9=E5=AD=90?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/star/star_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astrbot/core/star/star_manager.py b/astrbot/core/star/star_manager.py index 1c64adc706..1b86cd96ed 100644 --- a/astrbot/core/star/star_manager.py +++ b/astrbot/core/star/star_manager.py @@ -1179,7 +1179,7 @@ async def _terminate_plugin(star_metadata: StarMetadata) -> None: for handler in handlers: try: logger.info( - f"hook(on_plugin_loaded) -> {star_map[handler.handler_module_path].name} - {handler.handler_name}", + f"hook(on_plugin_unloaded) -> {star_map[handler.handler_module_path].name} - {handler.handler_name}", ) await handler.handler(star_metadata) except Exception: From 983da32aae0d3f5bcc30bbf4c8225d223381e6ea Mon Sep 17 00:00:00 2001 From: PyuraMazo <1605025385@qq.com> Date: Mon, 23 Feb 2026 22:34:46 +0800 Subject: [PATCH 3/4] format code with ruff --- astrbot/api/event/filter/__init__.py | 1 + astrbot/core/star/register/star_handler.py | 1 - astrbot/core/star/star_handler.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/astrbot/api/event/filter/__init__.py b/astrbot/api/event/filter/__init__.py index f7199d8656..3bae0eb79a 100644 --- a/astrbot/api/event/filter/__init__.py +++ b/astrbot/api/event/filter/__init__.py @@ -37,6 +37,7 @@ from astrbot.core.star.register import register_on_plugin_loaded as on_plugin_loaded from astrbot.core.star.register import register_on_plugin_unloaded as on_plugin_unloaded + __all__ = [ "CustomFilter", "EventMessageType", diff --git a/astrbot/core/star/register/star_handler.py b/astrbot/core/star/register/star_handler.py index bf2686c261..87b9b9998f 100644 --- a/astrbot/core/star/register/star_handler.py +++ b/astrbot/core/star/register/star_handler.py @@ -391,7 +391,6 @@ def decorator(awaitable): return decorator - def register_on_waiting_llm_request(**kwargs): """当等待调用 LLM 时的通知事件(在获取锁之前) diff --git a/astrbot/core/star/star_handler.py b/astrbot/core/star/star_handler.py index 3b41a6f366..762db86553 100644 --- a/astrbot/core/star/star_handler.py +++ b/astrbot/core/star/star_handler.py @@ -203,7 +203,7 @@ class EventType(enum.Enum): OnLLMToolRespondEvent = enum.auto() # 调用函数工具后 OnAfterMessageSentEvent = enum.auto() # 发送消息后 OnPluginErrorEvent = enum.auto() # 插件处理消息异常时 - OnPluginLoadedEvent = enum.auto() # 插件加载完成 + OnPluginLoadedEvent = enum.auto() # 插件加载完成 OnPluginUnloadedEvent = enum.auto() # 插件卸载完成 From cd6b8f2fc08b55a35a9ea29e262623041c24f35c Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 23 Feb 2026 23:13:21 +0800 Subject: [PATCH 4/4] ruff format --- astrbot/api/event/filter/__init__.py | 5 ++--- astrbot/core/star/star_manager.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/astrbot/api/event/filter/__init__.py b/astrbot/api/event/filter/__init__.py index 3bae0eb79a..f5ab15ed09 100644 --- a/astrbot/api/event/filter/__init__.py +++ b/astrbot/api/event/filter/__init__.py @@ -25,6 +25,8 @@ ) from astrbot.core.star.register import register_on_platform_loaded as on_platform_loaded from astrbot.core.star.register import register_on_plugin_error as on_plugin_error +from astrbot.core.star.register import register_on_plugin_loaded as on_plugin_loaded +from astrbot.core.star.register import register_on_plugin_unloaded as on_plugin_unloaded from astrbot.core.star.register import register_on_using_llm_tool as on_using_llm_tool from astrbot.core.star.register import ( register_on_waiting_llm_request as on_waiting_llm_request, @@ -35,9 +37,6 @@ ) from astrbot.core.star.register import register_regex as regex -from astrbot.core.star.register import register_on_plugin_loaded as on_plugin_loaded -from astrbot.core.star.register import register_on_plugin_unloaded as on_plugin_unloaded - __all__ = [ "CustomFilter", "EventMessageType", diff --git a/astrbot/core/star/star_manager.py b/astrbot/core/star/star_manager.py index 1b86cd96ed..06e59e2fd4 100644 --- a/astrbot/core/star/star_manager.py +++ b/astrbot/core/star/star_manager.py @@ -33,7 +33,7 @@ from .context import Context from .filter.permission import PermissionType, PermissionTypeFilter from .star import star_map, star_registry -from .star_handler import star_handlers_registry, EventType +from .star_handler import EventType, star_handlers_registry from .updator import PluginUpdator try: