新增了插件相关的完整领域模型、应用服务、基础设施实现,包括: 1. 插件状态、注册模式、来源等基础枚举和数据结构 2. 插件清单解析、发现、加载工具类 3. 插件注册表领域服务和内存存储实现 4. 插件相关的命令、查询、事件定义 5. 插件REST API接口和DTO映射 6. 集成了原有通道适配器到插件系统 7. 新增内置插件注册和自动发现能力
83 lines
2.8 KiB
Python
83 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from yuxi.channel.channels.web.config import WebConfig
|
|
from yuxi.channel.channels.web.outbound import WEB_CAPABILITIES, WebOutbound
|
|
from yuxi.channel.channels.web.translator import WebTranslator
|
|
from yuxi.channel.domain.model.message.dispatch_result import SendResult
|
|
from yuxi.channel.domain.model.message.unified_message import UnifiedMessage
|
|
from yuxi.channel.domain.model.shared.channel_capabilities import ChannelCapabilities
|
|
from yuxi.channel.domain.model.shared.channel_type import ChannelType
|
|
from yuxi.channel.interfaces.rest.router.contributor import ChannelRouteContributor
|
|
from yuxi.channel.domain.port.sse_push_port import SsePushPort
|
|
from yuxi.channel.domain.port.ws_connection_port import WsConnectionPort
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class _WebRouteContributor:
|
|
@property
|
|
def router(self) -> object:
|
|
from yuxi.channel.channels.web.routes import router
|
|
|
|
return router
|
|
|
|
|
|
class WebAdapter:
|
|
def __init__(self, *, sse_push: SsePushPort | None = None) -> None:
|
|
self._outbound = WebOutbound(sse_push=sse_push)
|
|
self._opened = False
|
|
|
|
@property
|
|
def capabilities(self) -> ChannelCapabilities:
|
|
return WEB_CAPABILITIES
|
|
|
|
@property
|
|
def channel_type(self) -> str:
|
|
return ChannelType.WEB.value
|
|
|
|
@property
|
|
def ws_connection(self) -> WsConnectionPort | None:
|
|
return None
|
|
|
|
@property
|
|
def route_contributor(self) -> ChannelRouteContributor | None:
|
|
return _WebRouteContributor()
|
|
|
|
@classmethod
|
|
def get_default_config(cls) -> dict:
|
|
return {
|
|
"max_connections": 1000,
|
|
}
|
|
|
|
@classmethod
|
|
def from_config(cls, config: WebConfig) -> WebAdapter:
|
|
return cls(sse_push=None)
|
|
|
|
async def open(self) -> None:
|
|
self._opened = True
|
|
|
|
async def close(self) -> None:
|
|
self._opened = False
|
|
|
|
async def receive_message(self, raw: dict) -> UnifiedMessage:
|
|
return WebTranslator.translate(raw)
|
|
|
|
async def send_message(self, session_id: str, content: str, *, channel_type: str, metadata: dict) -> SendResult:
|
|
try:
|
|
ok = await self._outbound.send_text(session_id, content, metadata=metadata)
|
|
return SendResult(success=ok, error="" if ok else "sse_push_failed")
|
|
except Exception as exc:
|
|
logger.error("SSE push failed for session %s: %s", session_id, exc)
|
|
return SendResult(success=False, error=str(exc))
|
|
|
|
async def send_typing(self, session_id: str) -> None:
|
|
await self._outbound.send_typing(session_id)
|
|
|
|
async def send_media(self, session_id: str, *, url: str, media_type: str, metadata: dict) -> bool:
|
|
return await self._outbound.send_media(session_id, url=url, media_type=media_type, metadata=metadata)
|
|
|
|
async def is_healthy(self) -> bool:
|
|
return self._opened
|