From 5553a86ac8a2f396069e63debc52842d83dd232b Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Tue, 17 Jun 2025 15:00:49 +0800 Subject: [PATCH] feat: preliminary migration of events entities --- pkg/pipeline/pipelinemgr.py | 30 +++++++--- pkg/plugin/context.py | 105 +-------------------------------- pkg/plugin/loaders/classic.py | 3 +- pkg/plugin/loaders/manifest.py | 3 +- pkg/plugin/models.py | 3 +- 5 files changed, 28 insertions(+), 116 deletions(-) diff --git a/pkg/pipeline/pipelinemgr.py b/pkg/pipeline/pipelinemgr.py index 6dbfd1fa..9407daf8 100644 --- a/pkg/pipeline/pipelinemgr.py +++ b/pkg/pipeline/pipelinemgr.py @@ -11,11 +11,12 @@ from ..entity.persistence import pipeline as persistence_pipeline from . import stage import langbot_plugin.api.entities.builtin.platform.message as platform_message import langbot_plugin.api.entities.builtin.platform.events as platform_events -from ..plugin import events +import langbot_plugin.api.entities.events as events from ..utils import importutil import langbot_plugin.api.entities.builtin.provider.session as provider_session import langbot_plugin.api.entities.builtin.pipeline.query as pipeline_query +import langbot_plugin.api.entities.context as event_context from . import ( resprule, @@ -183,16 +184,27 @@ class RuntimePipeline: else events.GroupMessageReceived ) - event_ctx = await self.ap.plugin_mgr.emit_event( - event=event_type( - launcher_type=query.launcher_type.value, - launcher_id=query.launcher_id, - sender_id=query.sender_id, - message_chain=query.message_chain, - query=query, - ) + print(query) + print(query.model_dump(exclude_none=True)) + + event_obj = event_type( + launcher_type=query.launcher_type.value, + launcher_id=query.launcher_id, + sender_id=query.sender_id, + message_chain=query.message_chain, + query=query, ) + event_ctx = event_context.EventContext( + event=event_obj, + ) + + event_ctx_result = await self.ap.plugin_connector.handler.emit_event( + event_ctx.model_dump(exclude_none=True) + ) + + event_ctx.update(**event_ctx_result) + if event_ctx.is_prevented_default(): return diff --git a/pkg/plugin/context.py b/pkg/plugin/context.py index 2a081127..a95660c1 100644 --- a/pkg/plugin/context.py +++ b/pkg/plugin/context.py @@ -3,10 +3,10 @@ from __future__ import annotations import typing import abc -from . import events from ..core import app import langbot_plugin.api.entities.builtin.platform.message as platform_message import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter +import langbot_plugin.api.entities.events as events def register( @@ -279,106 +279,3 @@ class EventContext: self.__prevent_postorder__ = False self.__return_value__ = {} EventContext.eid += 1 - - -# class RuntimeContainerStatus(enum.Enum): -# """插件容器状态""" - -# MOUNTED = 'mounted' -# """已加载进内存,所有位于运行时记录中的 RuntimeContainer 至少是这个状态""" - -# INITIALIZED = 'initialized' -# """已初始化""" - - -# class RuntimeContainer(pydantic.BaseModel): -# """运行时的插件容器 - -# 运行期间存储单个插件的信息 -# """ - -# plugin_name: str -# """插件名称""" - -# plugin_label: discover_engine.I18nString -# """插件标签""" - -# plugin_description: discover_engine.I18nString -# """插件描述""" - -# plugin_version: str -# """插件版本""" - -# plugin_author: str -# """插件作者""" - -# plugin_repository: str -# """插件源码地址""" - -# main_file: str -# """插件主文件路径""" - -# pkg_path: str -# """插件包路径""" - -# plugin_class: typing.Type[BasePlugin] = None -# """插件类""" - -# enabled: typing.Optional[bool] = True -# """是否启用""" - -# priority: typing.Optional[int] = 0 -# """优先级""" - -# config_schema: typing.Optional[list[dict]] = [] -# """插件配置模板""" - -# plugin_config: typing.Optional[dict] = {} -# """插件配置""" - -# plugin_inst: typing.Optional[BasePlugin] = None -# """插件实例""" - -# event_handlers: dict[ -# typing.Type[events.BaseEventModel], -# typing.Callable[[BasePlugin, EventContext], typing.Awaitable[None]], -# ] = {} -# """事件处理器""" - -# tools: list[tools_entities.LLMFunction] = [] -# """内容函数""" - -# status: RuntimeContainerStatus = RuntimeContainerStatus.MOUNTED -# """插件状态""" - -# class Config: -# arbitrary_types_allowed = True - -# def model_dump(self, *args, **kwargs): -# return { -# 'name': self.plugin_name, -# 'label': self.plugin_label.to_dict(), -# 'description': self.plugin_description.to_dict(), -# 'version': self.plugin_version, -# 'author': self.plugin_author, -# 'repository': self.plugin_repository, -# 'main_file': self.main_file, -# 'pkg_path': self.pkg_path, -# 'enabled': self.enabled, -# 'priority': self.priority, -# 'config_schema': self.config_schema, -# 'event_handlers': { -# event_name.__name__: handler.__name__ for event_name, handler in self.event_handlers.items() -# }, -# 'tools': [ -# { -# 'name': function.name, -# 'human_desc': function.human_desc, -# 'description': function.description, -# 'parameters': function.parameters, -# 'func': function.func.__name__, -# } -# for function in self.tools -# ], -# 'status': self.status.value, -# } diff --git a/pkg/plugin/loaders/classic.py b/pkg/plugin/loaders/classic.py index 6613bb63..98a625d4 100644 --- a/pkg/plugin/loaders/classic.py +++ b/pkg/plugin/loaders/classic.py @@ -5,11 +5,12 @@ import pkgutil import importlib import traceback -from .. import loader, events, context, models +from .. import loader, context, models from langbot_plugin.api.entities.builtin.resource import tool as resource_tool from ...utils import funcschema from ...discover import engine as discover_engine import langbot_plugin.api.entities.builtin.pipeline.query as pipeline_query +import langbot_plugin.api.entities.events as events class PluginLoader(loader.PluginLoader): diff --git a/pkg/plugin/loaders/manifest.py b/pkg/plugin/loaders/manifest.py index c5a78078..91b15f02 100644 --- a/pkg/plugin/loaders/manifest.py +++ b/pkg/plugin/loaders/manifest.py @@ -5,10 +5,11 @@ import os import traceback from ...core import app -from .. import context, events +from .. import context from .. import loader from ...utils import funcschema import langbot_plugin.api.entities.builtin.resource.tool as resource_tool +import langbot_plugin.api.entities.events as events class PluginManifestLoader(loader.PluginLoader): diff --git a/pkg/plugin/models.py b/pkg/plugin/models.py index dbde89a9..1b3a9b3f 100644 --- a/pkg/plugin/models.py +++ b/pkg/plugin/models.py @@ -8,6 +8,7 @@ import typing from .context import BasePlugin as Plugin from .events import * +import langbot_plugin.api.entities.events as events def register( @@ -17,7 +18,7 @@ def register( def on( - event: typing.Type[BaseEventModel], + event: typing.Type[events.BaseEventModel], ) -> typing.Callable[[typing.Callable], typing.Callable]: pass