"""API routes for evaluation reports.""" from fastapi import APIRouter, Depends, HTTPException, Query, Response from sqlmodel import Session from agenteval.evaluation.report import ( generate_compare_report, generate_report, render_html_report, render_json_report, render_markdown_report, ) from agenteval.storage.repository import RunRepository from agenteval.web.deps import get_db router = APIRouter() @router.get("/compare") def get_compare_report( run1: str = Query(..., description="First run ID"), run2: str = Query(..., description="Second run ID"), session: Session = Depends(get_db), ) -> dict: repo = RunRepository(session) run_a = repo.get(run1) run_b = repo.get(run2) if not run_a: raise HTTPException(status_code=404, detail=f"run not found: {run1}") if not run_b: raise HTTPException(status_code=404, detail=f"run not found: {run2}") if run_a.scenario_id != run_b.scenario_id: raise HTTPException(status_code=400, detail="对比报告要求两个运行使用相同场景") return generate_compare_report(run1, run2, session) @router.get("/{run_id}") def get_report(run_id: str, session: Session = Depends(get_db)) -> dict: run = RunRepository(session).get(run_id) if not run: raise HTTPException(status_code=404, detail="run not found") return generate_report(run_id, session) @router.get("/{run_id}/html") def get_html_report(run_id: str, session: Session = Depends(get_db)) -> Response: run = RunRepository(session).get(run_id) if not run: raise HTTPException(status_code=404, detail="run not found") html = render_html_report(run_id, session) return Response(content=html, media_type="text/html") @router.get("/{run_id}/json") def get_json_report(run_id: str, session: Session = Depends(get_db)) -> Response: run = RunRepository(session).get(run_id) if not run: raise HTTPException(status_code=404, detail="run not found") json_text = render_json_report(run_id, session) return Response(content=json_text, media_type="application/json") @router.get("/{run_id}/markdown") def get_markdown_report(run_id: str, session: Session = Depends(get_db)) -> Response: run = RunRepository(session).get(run_id) if not run: raise HTTPException(status_code=404, detail="run not found") md = render_markdown_report(run_id, session) return Response( content=md, media_type="text/markdown; charset=utf-8", headers={"Content-Disposition": f'attachment; filename="report-{run_id}.md"'}, )