routes.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from fastapi import APIRouter, Depends, HTTPException, Query
  2. from sqlalchemy import text
  3. from sqlalchemy.orm import Session
  4. from core_domain import ServiceHealth
  5. from app.application.services import RuntimeApplicationService
  6. from app.bootstrap.settings import RuntimeServiceSettings
  7. from app.db.session import get_db
  8. from app.domain.repositories import NodeRunRepository, WorkflowRunRepository
  9. from app.infrastructure.workflow_client import WorkflowServiceClient, WorkflowServiceClientError
  10. from app.schemas.run import (
  11. NodeRunResponse,
  12. NodeRunStatusUpdateRequest,
  13. RunBootstrapResponse,
  14. RunCreateRequest,
  15. WorkflowRunResponse,
  16. WorkflowRunStatusUpdateRequest,
  17. )
  18. router = APIRouter()
  19. def get_runtime_settings() -> RuntimeServiceSettings:
  20. return RuntimeServiceSettings()
  21. def get_runtime_application_service(
  22. db: Session = Depends(get_db),
  23. settings: RuntimeServiceSettings = Depends(get_runtime_settings),
  24. ) -> RuntimeApplicationService:
  25. return RuntimeApplicationService(
  26. workflow_run_repository=WorkflowRunRepository(db),
  27. node_run_repository=NodeRunRepository(db),
  28. workflow_client=WorkflowServiceClient(base_url=settings.workflow_service_url),
  29. )
  30. @router.get("/health", response_model=ServiceHealth)
  31. def health_check(db: Session = Depends(get_db)) -> ServiceHealth:
  32. db.execute(text("SELECT 1"))
  33. return ServiceHealth(service="runtime-service", status="ok", database="ok")
  34. @router.post("/runs", response_model=RunBootstrapResponse)
  35. def create_run(
  36. payload: RunCreateRequest,
  37. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  38. ) -> RunBootstrapResponse:
  39. try:
  40. workflow_run, initial_node = service.create_run(payload)
  41. except WorkflowServiceClientError as exc:
  42. raise HTTPException(status_code=502, detail=str(exc)) from exc
  43. return RunBootstrapResponse(
  44. run=WorkflowRunResponse.from_entity(workflow_run),
  45. initial_node=NodeRunResponse.from_entity(initial_node) if initial_node else None,
  46. )
  47. @router.get("/runs", response_model=list[WorkflowRunResponse])
  48. def list_runs(
  49. tenant_id: str = Query(...),
  50. session_id: str | None = Query(default=None),
  51. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  52. ) -> list[WorkflowRunResponse]:
  53. return [
  54. WorkflowRunResponse.from_entity(item)
  55. for item in service.list_runs(tenant_id=tenant_id, session_id=session_id)
  56. ]
  57. @router.get("/node-runs", response_model=list[NodeRunResponse])
  58. def list_node_runs(
  59. tenant_id: str = Query(...),
  60. run_id: str = Query(...),
  61. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  62. ) -> list[NodeRunResponse]:
  63. return [
  64. NodeRunResponse.from_entity(item)
  65. for item in service.list_node_runs(tenant_id=tenant_id, run_id=run_id)
  66. ]
  67. @router.post("/runs/{run_id}/status", response_model=WorkflowRunResponse)
  68. def update_run_status(
  69. run_id: str,
  70. payload: WorkflowRunStatusUpdateRequest,
  71. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  72. ) -> WorkflowRunResponse:
  73. entity = service.update_run_status(run_id=run_id, payload=payload)
  74. if entity is None:
  75. raise HTTPException(status_code=404, detail=f"workflow_run not found: {run_id}")
  76. return WorkflowRunResponse.from_entity(entity)
  77. @router.post("/node-runs/{node_run_id}/status", response_model=NodeRunResponse)
  78. def update_node_run_status(
  79. node_run_id: str,
  80. payload: NodeRunStatusUpdateRequest,
  81. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  82. ) -> NodeRunResponse:
  83. entity = service.update_node_run_status(node_run_id=node_run_id, payload=payload)
  84. if entity is None:
  85. raise HTTPException(status_code=404, detail=f"node_run not found: {node_run_id}")
  86. return NodeRunResponse.from_entity(entity)