routes.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from fastapi import APIRouter, Depends, HTTPException, Query
  2. from sqlalchemy import text
  3. from sqlalchemy.orm import Session
  4. from core_domain import HumanTaskStatus, ServiceHealth
  5. from app.application.services import HumanApplicationService
  6. from app.db.session import get_db
  7. from app.domain.repositories import HumanTaskRepository
  8. from app.schemas.human import (
  9. HumanTaskClaimRequest,
  10. HumanTaskCompleteRequest,
  11. HumanTaskCreateRequest,
  12. HumanTaskResponse,
  13. )
  14. router = APIRouter()
  15. def get_human_application_service(db: Session = Depends(get_db)) -> HumanApplicationService:
  16. return HumanApplicationService(human_task_repository=HumanTaskRepository(db))
  17. @router.get("/health", response_model=ServiceHealth)
  18. def health_check(db: Session = Depends(get_db)) -> ServiceHealth:
  19. db.execute(text("SELECT 1"))
  20. return ServiceHealth(service="human-service", status="ok", database="ok")
  21. @router.post("/tasks", response_model=HumanTaskResponse)
  22. def create_human_task(
  23. payload: HumanTaskCreateRequest,
  24. service: HumanApplicationService = Depends(get_human_application_service),
  25. ) -> HumanTaskResponse:
  26. return HumanTaskResponse.from_entity(service.create_task(payload))
  27. @router.get("/tasks", response_model=list[HumanTaskResponse])
  28. def list_human_tasks(
  29. tenant_id: str = Query(...),
  30. status: HumanTaskStatus | None = Query(default=None),
  31. assigned_to: str | None = Query(default=None),
  32. run_id: str | None = Query(default=None),
  33. limit: int = Query(default=100, ge=1, le=500),
  34. service: HumanApplicationService = Depends(get_human_application_service),
  35. ) -> list[HumanTaskResponse]:
  36. return [
  37. HumanTaskResponse.from_entity(item)
  38. for item in service.list_tasks(
  39. tenant_id=tenant_id,
  40. status=status,
  41. assigned_to=assigned_to,
  42. run_id=run_id,
  43. limit=limit,
  44. )
  45. ]
  46. @router.get("/tasks/{human_task_id}", response_model=HumanTaskResponse)
  47. def get_human_task(
  48. human_task_id: str,
  49. tenant_id: str = Query(...),
  50. service: HumanApplicationService = Depends(get_human_application_service),
  51. ) -> HumanTaskResponse:
  52. entity = service.get_task(tenant_id=tenant_id, human_task_id=human_task_id)
  53. if entity is None:
  54. raise HTTPException(status_code=404, detail=f"human task not found: {human_task_id}")
  55. return HumanTaskResponse.from_entity(entity)
  56. @router.post("/tasks/{human_task_id}/claim", response_model=HumanTaskResponse)
  57. def claim_human_task(
  58. human_task_id: str,
  59. payload: HumanTaskClaimRequest,
  60. service: HumanApplicationService = Depends(get_human_application_service),
  61. ) -> HumanTaskResponse:
  62. entity = service.claim_task(human_task_id=human_task_id, payload=payload)
  63. if entity is None:
  64. raise HTTPException(status_code=404, detail=f"human task not found: {human_task_id}")
  65. return HumanTaskResponse.from_entity(entity)
  66. @router.post("/tasks/{human_task_id}/complete", response_model=HumanTaskResponse)
  67. def complete_human_task(
  68. human_task_id: str,
  69. payload: HumanTaskCompleteRequest,
  70. service: HumanApplicationService = Depends(get_human_application_service),
  71. ) -> HumanTaskResponse:
  72. entity = service.complete_task(human_task_id=human_task_id, payload=payload)
  73. if entity is None:
  74. raise HTTPException(status_code=404, detail=f"human task not found: {human_task_id}")
  75. return HumanTaskResponse.from_entity(entity)