ForcePilot/backend/package/yuxi/channel/interfaces/rest/router/bindings.py
Kris c61d5f0163 feat: 完成通道服务多轮功能迭代
本次提交完成了一系列核心功能迭代与优化:
1.  新增并完善了多个领域模型与端口定义,补充了`__all__`导出规范
2.  优化了会话、绑定、出箱等模块的数据模型,修复了时间字段类型不一致问题
3.  新增了代理ID解析、缓存发布等接口,扩展了系统能力
4.  重构了去重中间件逻辑,优化了空内容校验规则
5.  新增了认证中间件的匿名访问支持,完善了鉴权流程
6.  优化了SSE连接管理,增加了单会话连接上限限制
7.  重构了消息日志与仓储相关代码,将数据类迁移至对应模型目录
8.  新增了重复绑定校验、绑定更新接口,完善了绑定服务逻辑
9.  优化了健康检查逻辑,新增了环境变量控制启动时间线展示
10. 重构了出箱重试工作线程,使用缓存端口替代直接redis操作,新增了消息处理标记逻辑
11. 完善了飞书、Web、钩子等通道的翻译器逻辑,补充了账户ID传递
12. 新增了多种自定义异常类型,优化了异常映射与错误处理流程
13. 完善了配置热重载逻辑,同步认证凭证与校验器配置
14. 重构了Redis缓存实现,增加了异常捕获与包装
2026-05-31 21:42:03 +08:00

147 lines
4.4 KiB
Python

from __future__ import annotations
import logging
from datetime import datetime
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field
from yuxi.channel.container import ChannelContainer, get_channel
from yuxi.channel.domain.exception.duplicate_binding import DuplicateBindingException
from yuxi.channel.domain.exception.invalid_agent_config import InvalidAgentConfigException
from yuxi.channel.interfaces.rest.auth.depends import channel_auth_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: datetime
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),
_: bool = Depends(channel_auth_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,
)
except DuplicateBindingException as e:
raise HTTPException(status_code=409, detail=str(e))
except InvalidAgentConfigException 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,
)
@router.get("/channel/bindings", response_model=BindingListResponse)
async def list_bindings(
channel_type: str | None = None,
offset: int = 0,
limit: int = 50,
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,
)
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,
)
@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}