ForcePilot/backend/package/yuxi/channel/channels/web/routes.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

66 lines
2.1 KiB
Python

from __future__ import annotations
import json
import logging
from uuid import uuid4
from fastapi import APIRouter, Body, HTTPException, Request
from fastapi.responses import JSONResponse
from yuxi.channel.channels.web.translator import WebTranslator
from yuxi.channel.domain.exception.abort_mapping import ABORT_CODE_TO_HTTP
logger = logging.getLogger(__name__)
router = APIRouter()
@router.post("/channel/web/message")
async def web_message(
request: Request,
body: bytes = Body(..., max_length=256 * 1024),
):
from yuxi.channel.container import get_channel
container = get_channel(request)
if not container:
raise HTTPException(status_code=503, detail="channel not initialized")
adapter = container.adapters.get("web")
if not adapter:
raise HTTPException(status_code=503, detail="web adapter not available")
try:
raw = json.loads(body)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="invalid JSON")
message = WebTranslator.translate_message(raw)
trace_id = raw.get("trace_id", request.headers.get("x-trace-id", str(uuid4())))
idempotency_key = request.headers.get("idempotency-key")
message.metadata["trace_id"] = trace_id
if idempotency_key:
message.metadata["idempotency_key"] = idempotency_key
message.metadata["client_ip"] = request.client.host if request.client else "unknown"
result = await container.inbound_service.submit(message, channel_type="web", trace_id=trace_id)
if result.is_aborted:
abort_code = result.abort_code or "PIPELINE_ABORTED"
http_status = ABORT_CODE_TO_HTTP.get(abort_code, 400)
raise HTTPException(
status_code=http_status,
detail={
"error": result.abort_reason,
"code": abort_code,
"trace_id": trace_id,
},
)
status = "skipped" if result.is_skipped else "accepted"
return JSONResponse(
status_code=202,
content={"trace_id": trace_id, "status": status, "message_id": message.message_id},
)