feat: reply_message api

This commit is contained in:
Junyan Qin
2025-07-13 16:31:25 +08:00
parent 5922be7e15
commit 4b57771eb1
4 changed files with 32 additions and 18 deletions

View File

@@ -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 文档。')

View File

@@ -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,

View File

@@ -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

View File

@@ -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(