from core_domain import HumanTaskStatus, ServiceHealth from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import text from sqlalchemy.orm import Session from app.application.services import HumanApplicationService from app.db.session import get_db from app.domain.repositories import HumanTaskRepository from app.schemas.human import ( HumanTaskClaimRequest, HumanTaskCompleteRequest, HumanTaskCreateRequest, HumanTaskResponse, ) router = APIRouter() def get_human_application_service(db: Session = Depends(get_db)) -> HumanApplicationService: return HumanApplicationService(human_task_repository=HumanTaskRepository(db)) @router.get("/health", response_model=ServiceHealth) def health_check(db: Session = Depends(get_db)) -> ServiceHealth: db.execute(text("SELECT 1")) return ServiceHealth(service="human-service", status="ok", database="ok") @router.post("/tasks", response_model=HumanTaskResponse) def create_human_task( payload: HumanTaskCreateRequest, service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse: return HumanTaskResponse.from_entity(service.create_task(payload)) @router.get("/tasks", response_model=list[HumanTaskResponse]) def list_human_tasks( status: HumanTaskStatus | None = Query(default=None), assigned_to: str | None = Query(default=None), run_id: str | None = Query(default=None), limit: int = Query(default=100, ge=1, le=500), service: HumanApplicationService = Depends(get_human_application_service)) -> list[HumanTaskResponse]: return [ HumanTaskResponse.from_entity(item) for item in service.list_tasks( status=status, assigned_to=assigned_to, run_id=run_id, limit=limit) ] @router.get("/tasks/{human_task_id}", response_model=HumanTaskResponse) def get_human_task( human_task_id: str, service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse: entity = service.get_task(human_task_id=human_task_id) if entity is None: raise HTTPException(status_code=404, detail=f"human task not found: {human_task_id}") return HumanTaskResponse.from_entity(entity) @router.post("/tasks/{human_task_id}/claim", response_model=HumanTaskResponse) def claim_human_task( human_task_id: str, payload: HumanTaskClaimRequest, service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse: entity = service.claim_task(human_task_id=human_task_id, payload=payload) if entity is None: raise HTTPException(status_code=404, detail=f"human task not found: {human_task_id}") return HumanTaskResponse.from_entity(entity) @router.post("/tasks/{human_task_id}/complete", response_model=HumanTaskResponse) def complete_human_task( human_task_id: str, payload: HumanTaskCompleteRequest, service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse: entity = service.complete_task(human_task_id=human_task_id, payload=payload) if entity is None: raise HTTPException(status_code=404, detail=f"human task not found: {human_task_id}") return HumanTaskResponse.from_entity(entity)