routes.py 5.4 KB

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