refactor: 恢复插件事件调用

This commit is contained in:
RockChinQ
2024-01-30 21:45:17 +08:00
parent e2de3d0102
commit 33d600fb6b
10 changed files with 298 additions and 86 deletions

View File

@@ -5,14 +5,13 @@ import typing
import pydantic
import mirai
from . import context
from ..core import entities as core_entities
from ..provider import entities as llm_entities
class BaseEventModel(pydantic.BaseModel):
query: core_entities.Query
query: core_entities.Query | None
class Config:
arbitrary_types_allowed = True
@@ -142,7 +141,7 @@ class NormalMessageResponded(BaseEventModel):
"""会话对象"""
prefix: str
"""回复消息的前缀,可修改"""
"""回复消息的前缀"""
response_text: str
"""回复消息的文本"""
@@ -157,24 +156,6 @@ class NormalMessageResponded(BaseEventModel):
"""回复消息组件列表"""
class SessionExplicitReset(BaseEventModel):
"""会话被显式重置时触发"""
session_name: str
session: core_entities.Session
class SessionExpired(BaseEventModel):
"""会话过期时触发"""
session_name: str
session: core_entities.Session
session_expire_time: int
class PromptPreProcessing(BaseEventModel):
"""会话中的Prompt预处理时触发"""
@@ -185,6 +166,3 @@ class PromptPreProcessing(BaseEventModel):
prompt: list[llm_entities.Message]
"""此对话现有消息记录,可修改"""
text_message: str
"""消息文本,可修改"""

View File

@@ -75,7 +75,7 @@ class PluginLoader(loader.PluginLoader):
for k, v in ctx.event.dict().items():
args[k] = v
await func(plugin, **args)
func(plugin, **args)
self._current_container.event_handlers[event] = handler

View File

@@ -1,6 +1,7 @@
from __future__ import annotations
import typing
import traceback
from ..core import app
from . import context, loader, events, installer, setting, models
@@ -100,6 +101,9 @@ class PluginManager:
for plugin in self.plugins:
if plugin.enabled:
if event.__class__ in plugin.event_handlers:
is_prevented_default_before_call = ctx.is_prevented_default()
try:
await plugin.event_handlers[event.__class__](
plugin.plugin_inst,
@@ -107,12 +111,19 @@ class PluginManager:
)
except Exception as e:
self.ap.logger.error(f'插件 {plugin.plugin_name} 触发事件 {event.__class__.__name__} 时发生错误: {e}')
self.ap.logger.exception(e)
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")
if not is_prevented_default_before_call and ctx.is_prevented_default():
self.ap.logger.debug(f'插件 {plugin.plugin_name} 阻止了默认行为执行')
if ctx.is_prevented_postorder():
self.ap.logger.debug(f'插件 {plugin.plugin_name} 阻止了后序插件的执行')
break
for key in ctx.__return_value__.keys():
if hasattr(ctx.event, key):
setattr(ctx.event, key, ctx.__return_value__[key][0])
self.ap.logger.debug(f'事件 {event.__class__.__name__}({ctx.eid}) 处理完成,返回值 {ctx.__return_value__}')
return ctx
return ctx

View File

@@ -3,7 +3,7 @@ from __future__ import annotations
import typing
from .context import BasePlugin as Plugin
from . import events
from .events import *
def register(
name: str,
@@ -15,7 +15,7 @@ def register(
def on(
event: typing.Type[events.BaseEventModel]
event: typing.Type[BaseEventModel]
) -> typing.Callable[[typing.Callable], typing.Callable]:
pass