PMFlow/backend/app/api/ai_steward_api/routes_mounted_ai.py

128 lines
4.9 KiB
Python

from __future__ import annotations
from fastapi import Depends, HTTPException, Query
from sqlalchemy.ext.asyncio import AsyncSession
from ...core.response import ApiResponse, success_response
from ...dependencies.permissions import require_manager_user
from ...dependencies.tenant import get_tenant_id
from ...models import User
from ...schemas.mounted_ai_runtime import (
MountedAICutoverStatusPayload,
MountedAIEvalSuiteDetailPayload,
MountedAIEvalSuiteListPayload,
MountedAIRunDetailPayload,
MountedAIRunListPayload,
MountedAISourceListPayload,
MountedAITopicListPayload,
)
from ...services.mounted_ai_runtime_service import MountedAIRuntimeService
from .shared import *
@router.get("/mounted-ai/sources", response_model=ApiResponse[MountedAISourceListPayload])
async def list_mounted_ai_sources(
current_user: User = Depends(require_manager_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
effective_tenant_id = int(getattr(current_user, "tenant_id", tenant_id) or tenant_id)
data = await MountedAIRuntimeService(db).list_sources(tenant_id=effective_tenant_id)
return success_response(data)
@router.get("/mounted-ai/topics", response_model=ApiResponse[MountedAITopicListPayload])
async def list_mounted_ai_topics(
mode: str | None = Query(default=None),
limit: int = Query(default=20, ge=1, le=100),
offset: int = Query(default=0, ge=0),
current_user: User = Depends(require_manager_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
effective_tenant_id = int(getattr(current_user, "tenant_id", tenant_id) or tenant_id)
data = await MountedAIRuntimeService(db).list_topics(
tenant_id=effective_tenant_id,
limit=limit,
offset=offset,
mode=mode,
)
return success_response(data)
@router.get("/mounted-ai/runs", response_model=ApiResponse[MountedAIRunListPayload])
async def list_mounted_ai_runs(
status: str | None = Query(default=None),
limit: int = Query(default=20, ge=1, le=100),
offset: int = Query(default=0, ge=0),
current_user: User = Depends(require_manager_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
effective_tenant_id = int(getattr(current_user, "tenant_id", tenant_id) or tenant_id)
data = await MountedAIRuntimeService(db).list_runs(
tenant_id=effective_tenant_id,
limit=limit,
offset=offset,
status=status,
)
return success_response(data)
@router.get("/mounted-ai/runs/{run_id}", response_model=ApiResponse[MountedAIRunDetailPayload])
async def get_mounted_ai_run_detail(
run_id: int,
current_user: User = Depends(require_manager_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
effective_tenant_id = int(getattr(current_user, "tenant_id", tenant_id) or tenant_id)
try:
data = await MountedAIRuntimeService(db).get_run_detail(
tenant_id=effective_tenant_id,
run_id=run_id,
)
except ValueError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
return success_response(data)
@router.get("/mounted-ai/cutover-status", response_model=ApiResponse[MountedAICutoverStatusPayload])
async def get_mounted_ai_cutover_status(
current_user: User = Depends(require_manager_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
effective_tenant_id = int(getattr(current_user, "tenant_id", tenant_id) or tenant_id)
data = await MountedAIRuntimeService(db).get_cutover_status(tenant_id=effective_tenant_id)
return success_response(data)
@router.get("/mounted-ai/eval-suites", response_model=ApiResponse[MountedAIEvalSuiteListPayload])
async def list_mounted_ai_eval_suites(
current_user: User = Depends(require_manager_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
effective_tenant_id = int(getattr(current_user, "tenant_id", tenant_id) or tenant_id)
data = await MountedAIRuntimeService(db).list_eval_suites(tenant_id=effective_tenant_id)
return success_response(data)
@router.get("/mounted-ai/eval-suites/{dataset_id}", response_model=ApiResponse[MountedAIEvalSuiteDetailPayload])
async def get_mounted_ai_eval_suite_detail(
dataset_id: int,
current_user: User = Depends(require_manager_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
effective_tenant_id = int(getattr(current_user, "tenant_id", tenant_id) or tenant_id)
try:
data = await MountedAIRuntimeService(db).get_eval_suite_detail(
tenant_id=effective_tenant_id,
dataset_id=dataset_id,
)
except ValueError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
return success_response(data)