| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- from fastapi import APIRouter, Depends, HTTPException, Query
- from sqlalchemy import text
- from sqlalchemy.orm import Session
- from core_domain import HumanTaskStatus, ServiceHealth
- 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(
- tenant_id: str = Query(...),
- 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(
- tenant_id=tenant_id,
- status=status,
- assigned_to=assigned_to,
- run_id=run_id,
- limit=limit,
- )
- ]
- @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)
|