f6e5e90736
- Redesign index.html from system-monitor to user-centric workspace (welcome banner, summary cards, 今日待办, AI 智能建议, collapsible admin section) - Add fetchTodos/fetchSuggestions/getPriorityClass/isOverdue methods - Update uni-nav current-page from 'dashboard' to 'workspace' - Add require_role to 10 admin router imports (RBAC foundation) - Update ai-assistant.js: add factory/insights pages, org_id in chat requests
93 lines
2.9 KiB
Python
93 lines
2.9 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Request
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from ..database import get_db
|
|
from ..models import LLMConfig
|
|
from ..schemas import LLMConfigBase, LLMConfigResponse
|
|
from .auth import get_current_admin, require_role
|
|
|
|
router = APIRouter(prefix="/api/admin/llmconfigs", tags=["admin"])
|
|
|
|
def _apply_default_exclusive(config: LLMConfig, db: Session):
|
|
"""当 config.is_default=True 时,将其他所有配置的 is_default 置为 False"""
|
|
if config.is_default:
|
|
db.query(LLMConfig).filter(LLMConfig.id != config.id).update(
|
|
{"is_default": False}, synchronize_session=False
|
|
)
|
|
db.flush()
|
|
|
|
@router.get("", response_model=List[LLMConfigResponse])
|
|
def list_llm_configs(
|
|
request: Request,
|
|
db: Session = Depends(get_db),
|
|
admin_user = Depends(get_current_admin)
|
|
):
|
|
"""获取 LLM 配置列表"""
|
|
configs = db.query(LLMConfig).all()
|
|
return [LLMConfigResponse.model_validate(c) for c in configs]
|
|
|
|
@router.get("/{config_id}", response_model=LLMConfigResponse)
|
|
def get_llm_config(
|
|
config_id: int,
|
|
request: Request,
|
|
db: Session = Depends(get_db),
|
|
admin_user = Depends(get_current_admin)
|
|
):
|
|
"""获取单个 LLM 配置详情"""
|
|
config = db.query(LLMConfig).filter(LLMConfig.id == config_id).first()
|
|
if not config:
|
|
raise HTTPException(status_code=404, detail="配置不存在")
|
|
return config
|
|
|
|
@router.post("", response_model=LLMConfigResponse)
|
|
def create_llm_config(
|
|
config_data: LLMConfigBase,
|
|
request: Request,
|
|
db: Session = Depends(get_db),
|
|
admin_user = Depends(get_current_admin)
|
|
):
|
|
"""创建 LLM 配置"""
|
|
config = LLMConfig(**config_data.model_dump())
|
|
db.add(config)
|
|
db.flush()
|
|
_apply_default_exclusive(config, db)
|
|
db.commit()
|
|
db.refresh(config)
|
|
return config
|
|
|
|
@router.put("/{config_id}", response_model=LLMConfigResponse)
|
|
def update_llm_config(
|
|
config_id: int,
|
|
config_update: LLMConfigBase,
|
|
request: Request,
|
|
db: Session = Depends(get_db),
|
|
admin_user = Depends(get_current_admin)
|
|
):
|
|
"""更新 LLM 配置"""
|
|
config = db.query(LLMConfig).filter(LLMConfig.id == config_id).first()
|
|
if not config:
|
|
raise HTTPException(status_code=404, detail="配置不存在")
|
|
update_data = config_update.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(config, field, value)
|
|
_apply_default_exclusive(config, db)
|
|
db.commit()
|
|
db.refresh(config)
|
|
return config
|
|
|
|
@router.delete("/{config_id}")
|
|
def delete_llm_config(
|
|
config_id: int,
|
|
request: Request,
|
|
db: Session = Depends(get_db),
|
|
admin_user = Depends(get_current_admin)
|
|
):
|
|
"""删除 LLM 配置"""
|
|
config = db.query(LLMConfig).filter(LLMConfig.id == config_id).first()
|
|
if not config:
|
|
raise HTTPException(status_code=404, detail="配置不存在")
|
|
db.delete(config)
|
|
db.commit()
|
|
return {"message": "删除成功"}
|