from core_domain import HumanTaskStatus, ServiceHealth from core_shared import error_detail 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, HumanTaskClaimPostRequest, HumanTaskCompleteRequest, HumanTaskCompletePostRequest, HumanTaskCreateRequest, HumanTaskDetailRequest, HumanTaskListRequest, 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.post("/tasks/list", response_model=list[HumanTaskResponse]) def list_human_tasks_post( payload: HumanTaskListRequest, service: HumanApplicationService = Depends(get_human_application_service)) -> list[HumanTaskResponse]: return [ HumanTaskResponse.from_entity(item) for item in service.list_tasks( status=payload.status, assigned_to=payload.assigned_to, run_id=payload.run_id, limit=payload.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=error_detail("error.human_task.not_found", id=human_task_id)) return HumanTaskResponse.from_entity(entity) @router.post("/tasks/detail", response_model=HumanTaskResponse) def get_human_task_post( payload: HumanTaskDetailRequest, service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse: entity = service.get_task(human_task_id=payload.human_task_id) if entity is None: raise HTTPException(status_code=404, detail=error_detail("error.human_task.not_found", id=payload.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=error_detail("error.human_task.not_found", id=human_task_id)) return HumanTaskResponse.from_entity(entity) @router.post("/tasks/claim", response_model=HumanTaskResponse) def claim_human_task_post( payload: HumanTaskClaimPostRequest, service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse: entity = service.claim_task( human_task_id=payload.human_task_id, payload=HumanTaskClaimRequest(claimed_by=payload.claimed_by)) if entity is None: raise HTTPException(status_code=404, detail=error_detail("error.human_task.not_found", id=payload.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=error_detail("error.human_task.not_found", id=human_task_id)) return HumanTaskResponse.from_entity(entity) @router.post("/tasks/complete", response_model=HumanTaskResponse) def complete_human_task_post( payload: HumanTaskCompletePostRequest, service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse: entity = service.complete_task( human_task_id=payload.human_task_id, payload=HumanTaskCompleteRequest( status=payload.status, response_payload_json=payload.response_payload_json)) if entity is None: raise HTTPException(status_code=404, detail=error_detail("error.human_task.not_found", id=payload.human_task_id)) return HumanTaskResponse.from_entity(entity)