refactor: update message gateway handling and remove adapter references

- Changed the message sending method to return DeliveryBatch instead of DeliveryReceipt in integration.py.
- Removed AdapterDeclarationPayload and related references from envelope.py, replacing them with MessageGatewayStateUpdatePayload and MessageGatewayStateUpdateResultPayload.
- Updated runner_main.py to remove adapter-related logic and methods, focusing on message gateway functionality.
- Added tests for message gateway runtime state synchronization and action bridge functionality in test files.
This commit is contained in:
DrSmoothl
2026-03-23 10:54:29 +08:00
parent d07e8f90ef
commit e26b27c287
16 changed files with 1221 additions and 760 deletions

View File

@@ -1,4 +1,4 @@
"""提供 Platform IO 的插件适配器驱动实现。"""
"""提供 Platform IO 的插件消息网关驱动实现。"""
from typing import TYPE_CHECKING, Any, Dict, Optional, Protocol
@@ -9,45 +9,49 @@ if TYPE_CHECKING:
from src.chat.message_receive.message import SessionMessage
class _AdapterSupervisorProtocol(Protocol):
"""适配器驱动依赖的 Supervisor 最小协议。"""
class _GatewaySupervisorProtocol(Protocol):
"""消息网关驱动依赖的 Supervisor 最小协议。"""
async def invoke_adapter(
async def invoke_message_gateway(
self,
plugin_id: str,
method_name: str,
component_name: str,
args: Optional[Dict[str, Any]] = None,
timeout_ms: int = 30000,
) -> Any:
"""调用适配器插件专用方法。"""
"""调用插件声明的消息网关方法。"""
class PluginPlatformDriver(PlatformIODriver):
"""面向适配器插件链路的 Platform IO 驱动。"""
"""面向插件消息网关链路的 Platform IO 驱动。"""
def __init__(
self,
driver_id: str,
platform: str,
supervisor: _AdapterSupervisorProtocol,
send_method: str = "send_to_platform",
supervisor: _GatewaySupervisorProtocol,
component_name: str,
*,
supports_send: bool,
account_id: Optional[str] = None,
scope: Optional[str] = None,
plugin_id: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None,
) -> None:
"""初始化一个插件适配器驱动。
"""初始化一个插件消息网关驱动。
Args:
driver_id: Broker 内的唯一驱动 ID。
platform: 该适配器负责的平台名称。
supervisor: 持有该适配器插件的 Supervisor。
send_method: 出站发送时要调用的插件方法名
platform: 该消息网关负责的平台名称。
supervisor: 持有该插件的 Supervisor。
component_name: 出站时要调用的网关组件名称
supports_send: 当前驱动是否具备出站能力。
account_id: 可选的账号 ID 或 self ID。
scope: 可选的额外路由作用域。
plugin_id: 拥有该适配器实现的插件 ID。
plugin_id: 拥有该实现的插件 ID。
metadata: 可选的额外驱动元数据。
"""
descriptor = DriverDescriptor(
driver_id=driver_id,
kind=DriverKind.PLUGIN,
@@ -59,7 +63,8 @@ class PluginPlatformDriver(PlatformIODriver):
)
super().__init__(descriptor)
self._supervisor = supervisor
self._send_method = send_method
self._component_name = component_name
self._supports_send = supports_send
async def send_message(
self,
@@ -67,16 +72,27 @@ class PluginPlatformDriver(PlatformIODriver):
route_key: RouteKey,
metadata: Optional[Dict[str, Any]] = None,
) -> DeliveryReceipt:
"""通过适配器插件发送消息。
"""通过插件消息网关发送消息。
Args:
message: 要投递的内部会话消息。
route_key: Broker 为本次投递选择的路由键。
metadata: 本次出站投递可选的 Broker 侧元数据。
metadata: 可选的发送元数据。
Returns:
DeliveryReceipt: 由驱动返回的规范化回执。
DeliveryReceipt: 规范化后的发送回执。
"""
if not self._supports_send:
return DeliveryReceipt(
internal_message_id=message.message_id,
route_key=route_key,
status=DeliveryStatus.FAILED,
driver_id=self.driver_id,
driver_kind=self.descriptor.kind,
error="当前消息网关仅支持接收,不支持发送",
)
from src.plugin_runtime.host.message_utils import PluginMessageUtils
plugin_id = self.descriptor.plugin_id or ""
@@ -87,14 +103,14 @@ class PluginPlatformDriver(PlatformIODriver):
status=DeliveryStatus.FAILED,
driver_id=self.driver_id,
driver_kind=self.descriptor.kind,
error="插件适配器驱动缺少 plugin_id",
error="插件消息网关驱动缺少 plugin_id",
)
try:
message_dict = PluginMessageUtils._session_message_to_dict(message)
response = await self._supervisor.invoke_adapter(
response = await self._supervisor.invoke_message_gateway(
plugin_id=plugin_id,
method_name=self._send_method,
component_name=self._component_name,
args={
"message": message_dict,
"route": {
@@ -119,7 +135,7 @@ class PluginPlatformDriver(PlatformIODriver):
return self._build_receipt(message.message_id, route_key, response)
def _build_receipt(self, internal_message_id: str, route_key: RouteKey, response: Any) -> DeliveryReceipt:
"""适配器调用响应归一化为出站回执。
"""网关调用响应归一化为出站回执。
Args:
internal_message_id: 内部消息 ID。
@@ -129,8 +145,9 @@ class PluginPlatformDriver(PlatformIODriver):
Returns:
DeliveryReceipt: 标准化后的出站回执。
"""
if getattr(response, "error", None):
error = response.error.get("message", "适配器发送失败")
error = response.error.get("message", "消息网关发送失败")
return DeliveryReceipt(
internal_message_id=internal_message_id,
route_key=route_key,
@@ -149,7 +166,7 @@ class PluginPlatformDriver(PlatformIODriver):
status=DeliveryStatus.FAILED,
driver_id=self.driver_id,
driver_kind=self.descriptor.kind,
error=str(payload.get("result", "适配器发送失败")) if isinstance(payload, dict) else "适配器发送失败",
error=str(payload.get("result", "消息网关发送失败")) if isinstance(payload, dict) else "消息网关发送失败",
)
result = payload.get("result") if isinstance(payload, dict) else None
@@ -161,7 +178,7 @@ class PluginPlatformDriver(PlatformIODriver):
status=DeliveryStatus.FAILED,
driver_id=self.driver_id,
driver_kind=self.descriptor.kind,
error=str(result.get("error", "适配器发送失败")),
error=str(result.get("error", "消息网关发送失败")),
metadata=result.get("metadata", {}) if isinstance(result.get("metadata"), dict) else {},
)
external_message_id = str(result.get("external_message_id") or result.get("message_id") or "") or None