29e6797c12
- Add /admin routes for manual trigger and status inspection - Add summarizer service with batch/single summary support - Add summarize CLI command (single arxiv_id or batch pending) - Register admin router in main app - Add tests for summarizer
49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
"""管理接口 — AI 总结触发,需要 ADMIN_TOKEN 鉴权。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.config import settings
|
|
from app.database import get_db
|
|
from app.services.summarizer import summarize_batch, summarize_single
|
|
|
|
router = APIRouter(prefix="/admin", tags=["admin"])
|
|
security = HTTPBearer()
|
|
|
|
|
|
async def verify_admin(
|
|
credentials: HTTPAuthorizationCredentials = Depends(security),
|
|
) -> str:
|
|
"""验证 ADMIN_TOKEN。"""
|
|
if credentials.credentials != settings.ADMIN_TOKEN:
|
|
raise HTTPException(status_code=401, detail="Invalid admin token")
|
|
return credentials.credentials
|
|
|
|
|
|
@router.post("/summarize")
|
|
async def admin_summarize_batch(
|
|
_admin: str = Depends(verify_admin),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""批量总结所有 pending 论文。"""
|
|
result = await summarize_batch(db)
|
|
if result.get("status") == "conflict":
|
|
raise HTTPException(status_code=409, detail=result.get("error", "batch already running"))
|
|
return result
|
|
|
|
|
|
@router.post("/summarize/{arxiv_id}")
|
|
async def admin_summarize_single(
|
|
arxiv_id: str,
|
|
_admin: str = Depends(verify_admin),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""总结或重跑单篇论文。"""
|
|
result = await summarize_single(db, arxiv_id, force=True)
|
|
if result.get("status") == "not_found":
|
|
raise HTTPException(status_code=404, detail=f"Paper not found: {arxiv_id}")
|
|
return result
|