from fastapi import APIRouter, Depends, Query from sqlalchemy import text from sqlalchemy.orm import Session from core_domain import ServiceHealth from app.application.services import RuntimeApplicationService from app.db.session import get_db from app.domain.repositories import NodeRunRepository, WorkflowRunRepository from app.schemas.run import RunBootstrapResponse, RunCreateRequest, NodeRunResponse, WorkflowRunResponse router = APIRouter() def get_runtime_application_service(db: Session = Depends(get_db)) -> RuntimeApplicationService: return RuntimeApplicationService( workflow_run_repository=WorkflowRunRepository(db), node_run_repository=NodeRunRepository(db), ) @router.get("/health", response_model=ServiceHealth) def health_check(db: Session = Depends(get_db)) -> ServiceHealth: db.execute(text("SELECT 1")) return ServiceHealth(service="runtime-service", status="ok", database="ok") @router.post("/runs", response_model=RunBootstrapResponse) def create_run( payload: RunCreateRequest, service: RuntimeApplicationService = Depends(get_runtime_application_service), ) -> RunBootstrapResponse: workflow_run, initial_node = service.create_run(payload) return RunBootstrapResponse( run=WorkflowRunResponse.from_entity(workflow_run), initial_node=NodeRunResponse.from_entity(initial_node) if initial_node else None, ) @router.get("/runs", response_model=list[WorkflowRunResponse]) def list_runs( tenant_id: str = Query(...), session_id: str | None = Query(default=None), service: RuntimeApplicationService = Depends(get_runtime_application_service), ) -> list[WorkflowRunResponse]: return [ WorkflowRunResponse.from_entity(item) for item in service.list_runs(tenant_id=tenant_id, session_id=session_id) ] @router.get("/node-runs", response_model=list[NodeRunResponse]) def list_node_runs( tenant_id: str = Query(...), run_id: str = Query(...), service: RuntimeApplicationService = Depends(get_runtime_application_service), ) -> list[NodeRunResponse]: return [ NodeRunResponse.from_entity(item) for item in service.list_node_runs(tenant_id=tenant_id, run_id=run_id) ]