新增了插件相关的完整领域模型、应用服务、基础设施实现,包括: 1. 插件状态、注册模式、来源等基础枚举和数据结构 2. 插件清单解析、发现、加载工具类 3. 插件注册表领域服务和内存存储实现 4. 插件相关的命令、查询、事件定义 5. 插件REST API接口和DTO映射 6. 集成了原有通道适配器到插件系统 7. 新增内置插件注册和自动发现能力
147 lines
4.6 KiB
Python
147 lines
4.6 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from pydantic import BaseModel, Field
|
|
|
|
from yuxi.channel.container import ChannelContainer, get_channel
|
|
from yuxi.channel.domain.exception.agent_config_not_found import AgentConfigNotFoundException
|
|
from yuxi.channel.domain.exception.duplicate_binding import DuplicateBindingException
|
|
from yuxi.channel.interfaces.rest.auth.depends import channel_auth_depends, channel_auth_operator_depends
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class BindingCreateRequest(BaseModel):
|
|
channel_type: str = Field(..., min_length=1, max_length=20)
|
|
account_id: str = Field(..., min_length=1, max_length=64)
|
|
group_id: str = Field("", max_length=128)
|
|
agent_config_id: int = Field(..., gt=0)
|
|
|
|
|
|
class BindingUpdateRequest(BaseModel):
|
|
agent_config_id: int | None = None
|
|
is_enabled: bool | None = None
|
|
|
|
|
|
class BindingResponse(BaseModel):
|
|
id: int
|
|
channel_type: str
|
|
account_id: str
|
|
group_id: str
|
|
agent_config_id: int
|
|
is_enabled: bool
|
|
created_at: str | None = None
|
|
|
|
|
|
class PaginatedMeta(BaseModel):
|
|
total: int
|
|
offset: int
|
|
limit: int
|
|
|
|
|
|
class BindingListResponse(BaseModel):
|
|
items: list[BindingResponse]
|
|
meta: PaginatedMeta
|
|
|
|
|
|
@router.post("/channel/bindings", response_model=BindingResponse, status_code=201)
|
|
async def create_binding(
|
|
req: BindingCreateRequest,
|
|
channel: ChannelContainer = Depends(get_channel),
|
|
operator: str = Depends(channel_auth_operator_depends),
|
|
):
|
|
try:
|
|
created = await channel.binding_service.create(
|
|
channel_type=req.channel_type,
|
|
account_id=req.account_id,
|
|
group_id=req.group_id,
|
|
agent_config_id=req.agent_config_id,
|
|
created_by=operator,
|
|
)
|
|
except DuplicateBindingException as e:
|
|
raise HTTPException(status_code=409, detail=str(e))
|
|
except AgentConfigNotFoundException as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
return BindingResponse(
|
|
id=created.id,
|
|
channel_type=created.channel_type,
|
|
account_id=created.account_id,
|
|
group_id=created.group_id,
|
|
agent_config_id=created.agent_config_id,
|
|
is_enabled=created.is_enabled,
|
|
created_at=created.created_at.isoformat() if created.created_at else None,
|
|
)
|
|
|
|
|
|
@router.get("/channel/bindings", response_model=BindingListResponse)
|
|
async def list_bindings(
|
|
channel_type: str | None = None,
|
|
offset: int = Query(0, ge=0),
|
|
limit: int = Query(50, ge=1, le=200),
|
|
channel: ChannelContainer = Depends(get_channel),
|
|
_: bool = Depends(channel_auth_depends),
|
|
):
|
|
bindings, total = await channel.binding_service.list(
|
|
channel_type=channel_type,
|
|
offset=offset,
|
|
limit=limit,
|
|
)
|
|
return BindingListResponse(
|
|
items=[
|
|
BindingResponse(
|
|
id=b.id,
|
|
channel_type=b.channel_type,
|
|
account_id=b.account_id,
|
|
group_id=b.group_id,
|
|
agent_config_id=b.agent_config_id,
|
|
is_enabled=b.is_enabled,
|
|
created_at=b.created_at.isoformat() if b.created_at else None,
|
|
)
|
|
for b in bindings
|
|
],
|
|
meta=PaginatedMeta(total=total, offset=offset, limit=limit),
|
|
)
|
|
|
|
|
|
@router.patch("/channel/bindings/{binding_id}", response_model=BindingResponse)
|
|
async def update_binding(
|
|
binding_id: int,
|
|
req: BindingUpdateRequest,
|
|
channel: ChannelContainer = Depends(get_channel),
|
|
_: bool = Depends(channel_auth_depends),
|
|
):
|
|
if req.agent_config_id is None and req.is_enabled is None:
|
|
raise HTTPException(status_code=400, detail="no fields to update")
|
|
updated = await channel.binding_service.update(
|
|
binding_id, agent_config_id=req.agent_config_id, is_enabled=req.is_enabled
|
|
)
|
|
if not updated:
|
|
raise HTTPException(status_code=404, detail="binding not found")
|
|
return BindingResponse(
|
|
id=updated.id,
|
|
channel_type=updated.channel_type,
|
|
account_id=updated.account_id,
|
|
group_id=updated.group_id,
|
|
agent_config_id=updated.agent_config_id,
|
|
is_enabled=updated.is_enabled,
|
|
created_at=updated.created_at.isoformat() if updated.created_at else None,
|
|
)
|
|
|
|
|
|
@router.delete("/channel/bindings/{binding_id}")
|
|
async def delete_binding(
|
|
binding_id: int,
|
|
channel: ChannelContainer = Depends(get_channel),
|
|
_: bool = Depends(channel_auth_depends),
|
|
):
|
|
deleted = await channel.binding_service.delete(binding_id)
|
|
if not deleted:
|
|
raise HTTPException(status_code=404, detail="binding not found")
|
|
|
|
return {"ok": True}
|