# File: backend/app/api/v1/endpoints/assistants.py (新建) # Description: 助手的 API 路由 from fastapi import APIRouter, HTTPException, Depends, status from typing import List from app.models.pydantic_models import AssistantRead, AssistantCreate, AssistantUpdate from app.services.assistant_service import assistant_service_instance, AssistantService router = APIRouter() # --- 依赖注入 AssistantService --- def get_assistant_service() -> AssistantService: return assistant_service_instance @router.post("/", response_model=AssistantRead, status_code=status.HTTP_201_CREATED) async def create_new_assistant( assistant_data: AssistantCreate, service: AssistantService = Depends(get_assistant_service) ): """创建新助手""" return service.create_assistant(assistant_data) @router.get("/", response_model=List[AssistantRead]) async def read_all_assistants( service: AssistantService = Depends(get_assistant_service) ): """获取所有助手列表""" return service.get_assistants() @router.get("/{assistant_id}", response_model=AssistantRead) async def read_assistant_by_id( assistant_id: str, service: AssistantService = Depends(get_assistant_service) ): """根据 ID 获取特定助手""" assistant = service.get_assistant(assistant_id) if not assistant: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="找不到指定的助手") return assistant @router.put("/{assistant_id}", response_model=AssistantRead) async def update_existing_assistant( assistant_id: str, assistant_data: AssistantUpdate, service: AssistantService = Depends(get_assistant_service) ): """更新指定 ID 的助手""" updated_assistant = service.update_assistant(assistant_id, assistant_data) if not updated_assistant: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="找不到指定的助手") return updated_assistant @router.delete("/{assistant_id}", status_code=status.HTTP_204_NO_CONTENT) async def delete_existing_assistant( assistant_id: str, service: AssistantService = Depends(get_assistant_service) ): """删除指定 ID 的助手""" deleted = service.delete_assistant(assistant_id) if not deleted: # 根据服务层逻辑判断是找不到还是不允许删除 assistant = service.get_assistant(assistant_id) if assistant and assistant_id == 'asst-default': raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="不允许删除默认助手") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="找不到指定的助手") # 成功删除,不返回内容