ForcePilot/backend/package/yuxi/channel/channels/hooks/adapter.py
Kris 9a8a27bf36 feat(channel): 新增渠道网关模块完整实现
本次提交新增了完整的多渠道消息网关系统,包括:
1. 支持飞书、钉钉、Web、Hook 四种渠道的适配器与配置
2. 领域模型层:消息、会话、绑定、出箱等核心实体
3. 应用服务层:管道、中间件、DTO 与业务逻辑
4. 基础设施层:持久化、过滤器、队列等端口实现
5. 接口层:REST API、SSE、WebSocket 通信端点
6. 前端页面与路由配置,添加渠道管理菜单
7. 新增相关依赖包与 docker-compose 部署配置
2026-05-30 21:53:09 +08:00

103 lines
3.2 KiB
Python

from __future__ import annotations
import logging
from yuxi.channel.channels.hooks.config import HookMapping
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.domain.port.channel_route_contributor import ChannelRouteContributor
from yuxi.channel.domain.port.ws_connection_port import WsConnectionPort
logger = logging.getLogger(__name__)
HOOKS_CAPABILITIES = ChannelCapabilities(
media=False,
group=False,
dm=True,
streaming=False,
typing=False,
reaction=False,
thread=False,
max_text_length=32768,
)
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": [],
}
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}")
return HooksTranslator.translate(raw, mapping)
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)