| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 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,
- 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=f"human task not found: {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=f"human task not found: {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=f"human task not found: {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=f"human task not found: {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=f"human task not found: {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=f"human task not found: {payload.human_task_id}")
- return HumanTaskResponse.from_entity(entity)
|