From 4b57771eb1f8a3f659ee5f5d595761c850f71628 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Sun, 13 Jul 2025 16:31:25 +0800 Subject: [PATCH] feat: `reply_message` api --- pkg/command/operators/update.py | 14 -------------- pkg/pipeline/pipelinemgr.py | 1 + pkg/plugin/connector.py | 6 ++---- pkg/plugin/handler.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 18 deletions(-) delete mode 100644 pkg/command/operators/update.py diff --git a/pkg/command/operators/update.py b/pkg/command/operators/update.py deleted file mode 100644 index 84e5c9cc..00000000 --- a/pkg/command/operators/update.py +++ /dev/null @@ -1,14 +0,0 @@ -from __future__ import annotations - -import typing - -from .. import operator -from langbot_plugin.api.entities.builtin.command import context as command_context - - -@operator.operator_class(name='update', help='更新程序', usage='!update', privilege=2) -class UpdateCommand(operator.CommandOperator): - async def execute( - self, context: command_context.ExecuteContext - ) -> typing.AsyncGenerator[command_context.CommandReturn, None]: - yield command_context.CommandReturn(text='不再支持通过命令更新,请查看 LangBot 文档。') diff --git a/pkg/pipeline/pipelinemgr.py b/pkg/pipeline/pipelinemgr.py index 5719b9e6..b5cf664a 100644 --- a/pkg/pipeline/pipelinemgr.py +++ b/pkg/pipeline/pipelinemgr.py @@ -184,6 +184,7 @@ class RuntimePipeline: ) event_obj = event_type( + query=query, launcher_type=query.launcher_type.value, launcher_id=query.launcher_id, sender_id=query.sender_id, diff --git a/pkg/plugin/connector.py b/pkg/plugin/connector.py index 8969d35d..b46302d5 100644 --- a/pkg/plugin/connector.py +++ b/pkg/plugin/connector.py @@ -102,13 +102,11 @@ class PluginRuntimeConnector: self, event: events.BaseEventModel, ) -> context.EventContext: - event_ctx = context.EventContext( - event=event, - ) + event_ctx = context.EventContext.from_event(event) event_ctx_result = await self.handler.emit_event(event_ctx.model_dump(serialize_as_any=True)) - event_ctx = context.EventContext.parse_from_dict(event_ctx_result['event_context']) + event_ctx = context.EventContext.model_validate(event_ctx_result['event_context']) return event_ctx diff --git a/pkg/plugin/handler.py b/pkg/plugin/handler.py index 9e9449cc..a4a77ea6 100644 --- a/pkg/plugin/handler.py +++ b/pkg/plugin/handler.py @@ -11,7 +11,10 @@ from langbot_plugin.entities.io.actions.enums import ( CommonAction, RuntimeToLangBotAction, LangBotToRuntimeAction, + PluginToRuntimeAction, ) +import langbot_plugin.api.entities.context as event_context_module +import langbot_plugin.api.entities.builtin.platform.message as platform_message from ..entity.persistence import plugin as persistence_plugin @@ -62,6 +65,32 @@ class RuntimeConnectionHandler(handler.Handler): data=data, ) + @self.action(PluginToRuntimeAction.REPLY_MESSAGE) + async def reply_message(data: dict[str, Any]) -> handler.ActionResponse: + """Reply message""" + eid = data['eid'] + message_chain = data['message_chain'] + quote_origin = data['quote_origin'] + + if eid not in event_context_module.cached_event_contexts: + return handler.ActionResponse.error( + message=f'Event context with eid {eid} not found', + ) + + event_context = event_context_module.cached_event_contexts[eid] + + message_chain_obj = platform_message.MessageChain.model_validate(message_chain) + + await event_context.event.query.adapter.reply_message( + event_context.event.query.message_event, + message_chain_obj, + quote_origin, + ) + + return handler.ActionResponse.success( + data={}, + ) + async def ping(self) -> dict[str, Any]: """Ping the runtime""" return await self.call_action(