新增了插件相关的完整领域模型、应用服务、基础设施实现,包括: 1. 插件状态、注册模式、来源等基础枚举和数据结构 2. 插件清单解析、发现、加载工具类 3. 插件注册表领域服务和内存存储实现 4. 插件相关的命令、查询、事件定义 5. 插件REST API接口和DTO映射 6. 集成了原有通道适配器到插件系统 7. 新增内置插件注册和自动发现能力
115 lines
3.9 KiB
Python
115 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from yuxi.channel.channels.hooks.config import HookMapping, HooksConfig
|
|
from yuxi.channel.channels.hooks.outbound import HOOKS_CAPABILITIES
|
|
from yuxi.channel.channels.hooks.translator import HooksTranslator
|
|
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.ws_connection_port import WsConnectionPort
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class _HooksRouteContributor:
|
|
@property
|
|
def router(self) -> object:
|
|
from yuxi.channel.channels.hooks.routes import router
|
|
|
|
return router
|
|
|
|
|
|
class HooksAdapter:
|
|
def __init__(self, *, mappings: list[dict] | None = None) -> None:
|
|
self._mappings: dict[tuple[str, str], HookMapping] = {}
|
|
self._opened = False
|
|
if mappings:
|
|
for m in mappings:
|
|
mapping = HookMapping(**m)
|
|
self._mappings[(mapping.match_path, mapping.match_source)] = mapping
|
|
|
|
@property
|
|
def capabilities(self) -> ChannelCapabilities:
|
|
return HOOKS_CAPABILITIES
|
|
|
|
@property
|
|
def channel_type(self) -> str:
|
|
return ChannelType.HOOKS.value
|
|
|
|
@property
|
|
def ws_connection(self) -> WsConnectionPort | None:
|
|
return None
|
|
|
|
@property
|
|
def route_contributor(self) -> ChannelRouteContributor | None:
|
|
return _HooksRouteContributor()
|
|
|
|
@classmethod
|
|
def get_default_config(cls) -> dict:
|
|
return {
|
|
"mappings": [],
|
|
}
|
|
|
|
@classmethod
|
|
def from_config(cls, config: HooksConfig) -> HooksAdapter:
|
|
mapping_dicts = [
|
|
{
|
|
"match_path": m.match_path,
|
|
"match_source": m.match_source,
|
|
"default_agent_id": m.default_agent_id,
|
|
"allowed_agent_ids": m.allowed_agent_ids,
|
|
"default_session_key": m.default_session_key,
|
|
"allow_request_session_key": m.allow_request_session_key,
|
|
"allowed_session_key_prefixes": m.allowed_session_key_prefixes,
|
|
"session_key_strategy": m.session_key_strategy,
|
|
"channel": m.channel,
|
|
"deliver": m.deliver,
|
|
"max_body_bytes": m.max_body_bytes,
|
|
"secret": m.secret,
|
|
}
|
|
for m in config.mappings
|
|
]
|
|
return cls(mappings=mapping_dicts)
|
|
|
|
def match_hook(self, path: str, source: str = "*") -> HookMapping | None:
|
|
exact = self._mappings.get((path, source))
|
|
if exact:
|
|
return exact
|
|
wildcard = self._mappings.get((path, "*"))
|
|
return wildcard
|
|
|
|
async def open(self) -> None:
|
|
self._opened = True
|
|
|
|
async def close(self) -> None:
|
|
self._opened = False
|
|
|
|
async def receive_message(self, raw: dict) -> UnifiedMessage:
|
|
path = raw.get("match_path", "")
|
|
source = raw.get("match_source", "*")
|
|
mapping = self.match_hook(path, source)
|
|
if not mapping:
|
|
raise ValueError(f"no hook mapping for path={path}, source={source}")
|
|
raw["_hook_mapping"] = mapping
|
|
return HooksTranslator.translate(raw)
|
|
|
|
async def send_message(self, session_id: str, content: str, *, channel_type: str, metadata: dict) -> SendResult:
|
|
logger.info(
|
|
"hooks send to %s (fire-and-forget, response saved to conversation)",
|
|
session_id,
|
|
)
|
|
return SendResult(success=True)
|
|
|
|
async def send_typing(self, session_id: str) -> None:
|
|
pass
|
|
|
|
async def send_media(self, session_id: str, *, url: str, media_type: str, metadata: dict) -> bool:
|
|
return True
|
|
|
|
async def is_healthy(self) -> bool:
|
|
return self._opened and bool(self._mappings)
|