routes.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from fastapi import APIRouter, Depends, 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.db.session import get_db
  7. from app.domain.repositories import NodeRunRepository, WorkflowRunRepository
  8. from app.schemas.run import RunBootstrapResponse, RunCreateRequest, NodeRunResponse, WorkflowRunResponse
  9. router = APIRouter()
  10. def get_runtime_application_service(db: Session = Depends(get_db)) -> RuntimeApplicationService:
  11. return RuntimeApplicationService(
  12. workflow_run_repository=WorkflowRunRepository(db),
  13. node_run_repository=NodeRunRepository(db),
  14. )
  15. @router.get("/health", response_model=ServiceHealth)
  16. def health_check(db: Session = Depends(get_db)) -> ServiceHealth:
  17. db.execute(text("SELECT 1"))
  18. return ServiceHealth(service="runtime-service", status="ok", database="ok")
  19. @router.post("/runs", response_model=RunBootstrapResponse)
  20. def create_run(
  21. payload: RunCreateRequest,
  22. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  23. ) -> RunBootstrapResponse:
  24. workflow_run, initial_node = service.create_run(payload)
  25. return RunBootstrapResponse(
  26. run=WorkflowRunResponse.from_entity(workflow_run),
  27. initial_node=NodeRunResponse.from_entity(initial_node) if initial_node else None,
  28. )
  29. @router.get("/runs", response_model=list[WorkflowRunResponse])
  30. def list_runs(
  31. tenant_id: str = Query(...),
  32. session_id: str | None = Query(default=None),
  33. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  34. ) -> list[WorkflowRunResponse]:
  35. return [
  36. WorkflowRunResponse.from_entity(item)
  37. for item in service.list_runs(tenant_id=tenant_id, session_id=session_id)
  38. ]
  39. @router.get("/node-runs", response_model=list[NodeRunResponse])
  40. def list_node_runs(
  41. tenant_id: str = Query(...),
  42. run_id: str = Query(...),
  43. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  44. ) -> list[NodeRunResponse]:
  45. return [
  46. NodeRunResponse.from_entity(item)
  47. for item in service.list_node_runs(tenant_id=tenant_id, run_id=run_id)
  48. ]