from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import text from sqlalchemy.orm import Session from core_domain import ServiceHealth from app.application.services import AgentApplicationService from app.db.session import get_db from app.domain.repositories import ( AgentDefinitionRepository, AgentRunRepository, AgentVersionRepository, ) from app.schemas.agent import ( AgentCreateRequest, AgentResponse, AgentRunCreateRequest, AgentRunResponse, AgentRunStatusUpdateRequest, AgentStatusUpdateRequest, AgentVersionCreateRequest, AgentVersionResponse, ) router = APIRouter() def get_agent_application_service(db: Session = Depends(get_db)) -> AgentApplicationService: return AgentApplicationService( agent_repository=AgentDefinitionRepository(db), agent_version_repository=AgentVersionRepository(db), agent_run_repository=AgentRunRepository(db), ) @router.get("/health", response_model=ServiceHealth) def health_check(db: Session = Depends(get_db)) -> ServiceHealth: db.execute(text("SELECT 1")) return ServiceHealth(service="agent-service", status="ok", database="ok") @router.post("", response_model=AgentResponse) def create_agent( payload: AgentCreateRequest, service: AgentApplicationService = Depends(get_agent_application_service), ) -> AgentResponse: entity = service.create_agent(payload) return AgentResponse.from_entity(entity) @router.get("", response_model=list[AgentResponse]) def list_agents( tenant_id: str = Query(...), service: AgentApplicationService = Depends(get_agent_application_service), ) -> list[AgentResponse]: return [AgentResponse.from_entity(item) for item in service.list_agents(tenant_id=tenant_id)] @router.patch("/{agent_id}/status", response_model=AgentResponse) def update_agent_status( agent_id: str, payload: AgentStatusUpdateRequest, service: AgentApplicationService = Depends(get_agent_application_service), ) -> AgentResponse: entity = service.update_agent_status(agent_id=agent_id, payload=payload) if entity is None: raise HTTPException(status_code=404, detail=f"agent not found: {agent_id}") return AgentResponse.from_entity(entity) @router.post("/versions", response_model=AgentVersionResponse) def create_agent_version( payload: AgentVersionCreateRequest, service: AgentApplicationService = Depends(get_agent_application_service), ) -> AgentVersionResponse: try: entity = service.create_agent_version(payload) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc return AgentVersionResponse.from_entity(entity) @router.get("/versions", response_model=list[AgentVersionResponse]) def list_agent_versions( tenant_id: str = Query(...), agent_id: str = Query(...), service: AgentApplicationService = Depends(get_agent_application_service), ) -> list[AgentVersionResponse]: return [ AgentVersionResponse.from_entity(item) for item in service.list_agent_versions(tenant_id=tenant_id, agent_id=agent_id) ] @router.post("/runs", response_model=AgentRunResponse) def create_agent_run( payload: AgentRunCreateRequest, service: AgentApplicationService = Depends(get_agent_application_service), ) -> AgentRunResponse: try: entity = service.create_agent_run(payload) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc return AgentRunResponse.from_entity(entity) @router.get("/runs", response_model=list[AgentRunResponse]) def list_agent_runs( tenant_id: str = Query(...), agent_id: str | None = Query(default=None), session_id: str | None = Query(default=None), service: AgentApplicationService = Depends(get_agent_application_service), ) -> list[AgentRunResponse]: return [ AgentRunResponse.from_entity(item) for item in service.list_agent_runs( tenant_id=tenant_id, agent_id=agent_id, session_id=session_id, ) ] @router.post("/runs/{agent_run_id}/status", response_model=AgentRunResponse) def update_agent_run_status( agent_run_id: str, payload: AgentRunStatusUpdateRequest, service: AgentApplicationService = Depends(get_agent_application_service), ) -> AgentRunResponse: entity = service.update_agent_run_status(agent_run_id=agent_run_id, payload=payload) if entity is None: raise HTTPException(status_code=404, detail=f"agent_run not found: {agent_run_id}") return AgentRunResponse.from_entity(entity)