routes.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from core_domain import HumanTaskStatus, ServiceHealth
  2. from core_shared import error_detail
  3. from fastapi import APIRouter, Depends, HTTPException, Query
  4. from sqlalchemy import text
  5. from sqlalchemy.orm import Session
  6. from app.application.services import HumanApplicationService
  7. from app.db.session import get_db
  8. from app.domain.repositories import HumanTaskRepository
  9. from app.schemas.human import (
  10. HumanTaskClaimRequest,
  11. HumanTaskClaimPostRequest,
  12. HumanTaskCompleteRequest,
  13. HumanTaskCompletePostRequest,
  14. HumanTaskCreateRequest,
  15. HumanTaskDetailRequest,
  16. HumanTaskListRequest,
  17. HumanTaskResponse,
  18. )
  19. router = APIRouter()
  20. def get_human_application_service(db: Session = Depends(get_db)) -> HumanApplicationService:
  21. return HumanApplicationService(human_task_repository=HumanTaskRepository(db))
  22. @router.get("/health", response_model=ServiceHealth)
  23. def health_check(db: Session = Depends(get_db)) -> ServiceHealth:
  24. db.execute(text("SELECT 1"))
  25. return ServiceHealth(service="human-service", status="ok", database="ok")
  26. @router.post("/tasks", response_model=HumanTaskResponse)
  27. def create_human_task(
  28. payload: HumanTaskCreateRequest,
  29. service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse:
  30. return HumanTaskResponse.from_entity(service.create_task(payload))
  31. @router.get("/tasks", response_model=list[HumanTaskResponse])
  32. def list_human_tasks(
  33. status: HumanTaskStatus | None = Query(default=None),
  34. assigned_to: str | None = Query(default=None),
  35. run_id: str | None = Query(default=None),
  36. limit: int = Query(default=100, ge=1, le=500),
  37. service: HumanApplicationService = Depends(get_human_application_service)) -> list[HumanTaskResponse]:
  38. return [
  39. HumanTaskResponse.from_entity(item)
  40. for item in service.list_tasks(
  41. status=status,
  42. assigned_to=assigned_to,
  43. run_id=run_id,
  44. limit=limit)
  45. ]
  46. @router.post("/tasks/list", response_model=list[HumanTaskResponse])
  47. def list_human_tasks_post(
  48. payload: HumanTaskListRequest,
  49. service: HumanApplicationService = Depends(get_human_application_service)) -> list[HumanTaskResponse]:
  50. return [
  51. HumanTaskResponse.from_entity(item)
  52. for item in service.list_tasks(
  53. status=payload.status,
  54. assigned_to=payload.assigned_to,
  55. run_id=payload.run_id,
  56. limit=payload.limit)
  57. ]
  58. @router.get("/tasks/{human_task_id}", response_model=HumanTaskResponse)
  59. def get_human_task(
  60. human_task_id: str,
  61. service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse:
  62. entity = service.get_task(human_task_id=human_task_id)
  63. if entity is None:
  64. raise HTTPException(status_code=404, detail=error_detail("error.human_task.not_found", id=human_task_id))
  65. return HumanTaskResponse.from_entity(entity)
  66. @router.post("/tasks/detail", response_model=HumanTaskResponse)
  67. def get_human_task_post(
  68. payload: HumanTaskDetailRequest,
  69. service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse:
  70. entity = service.get_task(human_task_id=payload.human_task_id)
  71. if entity is None:
  72. raise HTTPException(status_code=404, detail=error_detail("error.human_task.not_found", id=payload.human_task_id))
  73. return HumanTaskResponse.from_entity(entity)
  74. @router.post("/tasks/{human_task_id}/claim", response_model=HumanTaskResponse)
  75. def claim_human_task(
  76. human_task_id: str,
  77. payload: HumanTaskClaimRequest,
  78. service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse:
  79. entity = service.claim_task(human_task_id=human_task_id, payload=payload)
  80. if entity is None:
  81. raise HTTPException(status_code=404, detail=error_detail("error.human_task.not_found", id=human_task_id))
  82. return HumanTaskResponse.from_entity(entity)
  83. @router.post("/tasks/claim", response_model=HumanTaskResponse)
  84. def claim_human_task_post(
  85. payload: HumanTaskClaimPostRequest,
  86. service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse:
  87. entity = service.claim_task(
  88. human_task_id=payload.human_task_id,
  89. payload=HumanTaskClaimRequest(claimed_by=payload.claimed_by))
  90. if entity is None:
  91. raise HTTPException(status_code=404, detail=error_detail("error.human_task.not_found", id=payload.human_task_id))
  92. return HumanTaskResponse.from_entity(entity)
  93. @router.post("/tasks/{human_task_id}/complete", response_model=HumanTaskResponse)
  94. def complete_human_task(
  95. human_task_id: str,
  96. payload: HumanTaskCompleteRequest,
  97. service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse:
  98. entity = service.complete_task(human_task_id=human_task_id, payload=payload)
  99. if entity is None:
  100. raise HTTPException(status_code=404, detail=error_detail("error.human_task.not_found", id=human_task_id))
  101. return HumanTaskResponse.from_entity(entity)
  102. @router.post("/tasks/complete", response_model=HumanTaskResponse)
  103. def complete_human_task_post(
  104. payload: HumanTaskCompletePostRequest,
  105. service: HumanApplicationService = Depends(get_human_application_service)) -> HumanTaskResponse:
  106. entity = service.complete_task(
  107. human_task_id=payload.human_task_id,
  108. payload=HumanTaskCompleteRequest(
  109. status=payload.status,
  110. response_payload_json=payload.response_payload_json))
  111. if entity is None:
  112. raise HTTPException(status_code=404, detail=error_detail("error.human_task.not_found", id=payload.human_task_id))
  113. return HumanTaskResponse.from_entity(entity)