import logging from fastapi import APIRouter from pydantic import BaseModel from typing import Any, Dict, Optional from app.graphs.self_check_analysis_graph import get_graph from app.graphs.self_check_trend_graph import get_trend_graph logger = logging.getLogger(__name__) router = APIRouter(prefix="/api/v1", tags=["self-check"]) class SelfCheckAnalysisRequest(BaseModel): scores: Dict[str, Any] question_ids: list user_id: int recent_history: Optional[list] = None class SelfCheckTrendRequest(BaseModel): history: list user_id: int @router.post("/self-check/analysis") async def self_check_analysis(req: SelfCheckAnalysisRequest): graph = get_graph() state = { "scores": req.scores, "question_ids": req.question_ids, "user_id": req.user_id, "recent_history": req.recent_history or [], "advice": None, "error": None, } result = await graph.ainvoke(state) advice = result.get("advice") or {} return { "advice_json": advice.get("advice_json"), "fallback_used": advice.get("fallback_used", True), "error": result.get("error"), } @router.post("/self-check/trend") async def self_check_trend(req: SelfCheckTrendRequest): graph = get_trend_graph() state = {"history": req.history, "user_id": req.user_id, "insight": None, "error": None} result = await graph.ainvoke(state) insight = result.get("insight") or {} return { "aiInsight": insight.get("aiInsight", ""), "trendSummary": insight.get("trendSummary", ""), "error": result.get("error"), }