ForcePilot/backend/package/yuxi/channel/channels/web/routes.py
Kris 9e503becd3
Some checks failed
Deploy VitePress site to Pages / build (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
feat(plugin): 实现完整的插件注册管理系统
新增了插件相关的完整领域模型、应用服务、基础设施实现,包括:
1. 插件状态、注册模式、来源等基础枚举和数据结构
2. 插件清单解析、发现、加载工具类
3. 插件注册表领域服务和内存存储实现
4. 插件相关的命令、查询、事件定义
5. 插件REST API接口和DTO映射
6. 集成了原有通道适配器到插件系统
7. 新增内置插件注册和自动发现能力
2026-05-31 16:44:13 +08:00

75 lines
2.4 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.domain.exception.abort_mapping import ABORT_CODE_TO_HTTP
logger = logging.getLogger(__name__)
router = APIRouter()
@router.post("/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)
adapter = container.adapters.get("web")
if not adapter:
raise HTTPException(status_code=503, detail="web adapter not available")
verifier = container.verifiers.get("web")
verify_result = None
if verifier and verifier.enabled:
verify_result = await verifier.verify(body, dict(request.headers))
if not verify_result.passed:
raise HTTPException(status_code=401, detail=verify_result.reason)
try:
raw = json.loads(body)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="invalid JSON")
try:
message = await adapter.receive_message(raw)
except Exception:
logger.exception("web message parse failed")
raise HTTPException(status_code=400, detail="message parse failed")
trace_id = raw.get("trace_id", request.headers.get("x-trace-id", str(uuid4())))
idempotency_key = request.headers.get("idempotency-key") or trace_id
message.metadata["trace_id"] = trace_id
message.metadata["idempotency_key"] = idempotency_key
message.metadata["client_ip"] = request.client.host if request.client else "unknown"
if verify_result:
message.metadata["auth_method"] = verify_result.method
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},
)