routes.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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, build_runtime_application_service
  6. from app.bootstrap.settings import RuntimeServiceSettings
  7. from app.db.session import get_db
  8. from app.infrastructure.code_runner_client import CodeRunnerClientError
  9. from app.infrastructure.model_gateway_client import ModelGatewayClientError
  10. from app.infrastructure.tool_client import ToolServiceClientError
  11. from app.infrastructure.workflow_client import WorkflowServiceClientError
  12. from app.schemas.run import (
  13. ExecutionLogResponse,
  14. NodeArtifactResponse,
  15. NodeRunExecuteRequest,
  16. NodeRunExecuteResponse,
  17. NodeRunResponse,
  18. NodeRunStatusUpdateRequest,
  19. RunBootstrapResponse,
  20. RunCreateRequest,
  21. RunExecuteRequest,
  22. RunExecuteResponse,
  23. TraceSpanResponse,
  24. WorkerExecuteNextRequest,
  25. WorkerExecuteNextResponse,
  26. WorkflowRunResponse,
  27. WorkflowRunStatusUpdateRequest,
  28. )
  29. router = APIRouter()
  30. def get_runtime_settings() -> RuntimeServiceSettings:
  31. return RuntimeServiceSettings()
  32. def get_runtime_application_service(
  33. db: Session = Depends(get_db),
  34. settings: RuntimeServiceSettings = Depends(get_runtime_settings),
  35. ) -> RuntimeApplicationService:
  36. return build_runtime_application_service(db=db, settings=settings)
  37. @router.get("/health", response_model=ServiceHealth)
  38. def health_check(db: Session = Depends(get_db)) -> ServiceHealth:
  39. db.execute(text("SELECT 1"))
  40. return ServiceHealth(service="runtime-service", status="ok", database="ok")
  41. @router.post("/runs", response_model=RunBootstrapResponse)
  42. def create_run(
  43. payload: RunCreateRequest,
  44. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  45. ) -> RunBootstrapResponse:
  46. try:
  47. workflow_run, initial_node = service.create_run(payload)
  48. except (
  49. CodeRunnerClientError,
  50. ModelGatewayClientError,
  51. ToolServiceClientError,
  52. WorkflowServiceClientError,
  53. ) as exc:
  54. raise HTTPException(status_code=502, detail=str(exc)) from exc
  55. return RunBootstrapResponse(
  56. run=WorkflowRunResponse.from_entity(workflow_run),
  57. initial_node=NodeRunResponse.from_entity(initial_node) if initial_node else None,
  58. )
  59. @router.get("/runs", response_model=list[WorkflowRunResponse])
  60. def list_runs(
  61. tenant_id: str = Query(...),
  62. session_id: str | None = Query(default=None),
  63. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  64. ) -> list[WorkflowRunResponse]:
  65. return [
  66. WorkflowRunResponse.from_entity(item)
  67. for item in service.list_runs(tenant_id=tenant_id, session_id=session_id)
  68. ]
  69. @router.get("/node-runs", response_model=list[NodeRunResponse])
  70. def list_node_runs(
  71. tenant_id: str = Query(...),
  72. run_id: str = Query(...),
  73. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  74. ) -> list[NodeRunResponse]:
  75. return [
  76. NodeRunResponse.from_entity(item)
  77. for item in service.list_node_runs(tenant_id=tenant_id, run_id=run_id)
  78. ]
  79. @router.get("/execution-logs", response_model=list[ExecutionLogResponse])
  80. def list_execution_logs(
  81. tenant_id: str = Query(...),
  82. run_id: str | None = Query(default=None),
  83. node_run_id: str | None = Query(default=None),
  84. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  85. ) -> list[ExecutionLogResponse]:
  86. return [
  87. ExecutionLogResponse.from_entity(item)
  88. for item in service.list_execution_logs(
  89. tenant_id=tenant_id,
  90. run_id=run_id,
  91. node_run_id=node_run_id,
  92. )
  93. ]
  94. @router.get("/node-artifacts", response_model=list[NodeArtifactResponse])
  95. def list_node_artifacts(
  96. tenant_id: str = Query(...),
  97. run_id: str | None = Query(default=None),
  98. node_run_id: str | None = Query(default=None),
  99. artifact_type: str | None = Query(default=None),
  100. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  101. ) -> list[NodeArtifactResponse]:
  102. return [
  103. NodeArtifactResponse.from_entity(item)
  104. for item in service.list_node_artifacts(
  105. tenant_id=tenant_id,
  106. run_id=run_id,
  107. node_run_id=node_run_id,
  108. artifact_type=artifact_type,
  109. )
  110. ]
  111. @router.get("/trace-spans", response_model=list[TraceSpanResponse])
  112. def list_trace_spans(
  113. tenant_id: str = Query(...),
  114. run_id: str | None = Query(default=None),
  115. node_run_id: str | None = Query(default=None),
  116. span_type: str | None = Query(default=None),
  117. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  118. ) -> list[TraceSpanResponse]:
  119. return [
  120. TraceSpanResponse.from_entity(item)
  121. for item in service.list_trace_spans(
  122. tenant_id=tenant_id,
  123. run_id=run_id,
  124. node_run_id=node_run_id,
  125. span_type=span_type,
  126. )
  127. ]
  128. @router.post("/runs/{run_id}/status", response_model=WorkflowRunResponse)
  129. def update_run_status(
  130. run_id: str,
  131. payload: WorkflowRunStatusUpdateRequest,
  132. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  133. ) -> WorkflowRunResponse:
  134. entity = service.update_run_status(run_id=run_id, payload=payload)
  135. if entity is None:
  136. raise HTTPException(status_code=404, detail=f"workflow_run not found: {run_id}")
  137. return WorkflowRunResponse.from_entity(entity)
  138. @router.post("/node-runs/{node_run_id}/status", response_model=NodeRunResponse)
  139. def update_node_run_status(
  140. node_run_id: str,
  141. payload: NodeRunStatusUpdateRequest,
  142. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  143. ) -> NodeRunResponse:
  144. entity = service.update_node_run_status(node_run_id=node_run_id, payload=payload)
  145. if entity is None:
  146. raise HTTPException(status_code=404, detail=f"node_run not found: {node_run_id}")
  147. return NodeRunResponse.from_entity(entity)
  148. @router.post("/node-runs/{node_run_id}/execute", response_model=NodeRunExecuteResponse)
  149. def execute_node_run(
  150. node_run_id: str,
  151. payload: NodeRunExecuteRequest,
  152. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  153. ) -> NodeRunExecuteResponse:
  154. try:
  155. result = service.execute_node_run(node_run_id=node_run_id, payload=payload)
  156. except (
  157. CodeRunnerClientError,
  158. ModelGatewayClientError,
  159. ToolServiceClientError,
  160. WorkflowServiceClientError,
  161. ) as exc:
  162. raise HTTPException(status_code=502, detail=str(exc)) from exc
  163. if result is None:
  164. raise HTTPException(status_code=404, detail=f"node_run not found: {node_run_id}")
  165. workflow_run, node_run, executor_name = result
  166. return NodeRunExecuteResponse(
  167. run=WorkflowRunResponse.from_entity(workflow_run),
  168. node_run=NodeRunResponse.from_entity(node_run),
  169. executor_name=executor_name,
  170. )
  171. @router.post("/runs/{run_id}/execute-next", response_model=NodeRunExecuteResponse)
  172. def execute_next_node_run(
  173. run_id: str,
  174. payload: NodeRunExecuteRequest,
  175. tenant_id: str = Query(...),
  176. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  177. ) -> NodeRunExecuteResponse:
  178. try:
  179. result = service.execute_next_node_run(
  180. tenant_id=tenant_id,
  181. run_id=run_id,
  182. payload=payload,
  183. )
  184. except (
  185. CodeRunnerClientError,
  186. ModelGatewayClientError,
  187. ToolServiceClientError,
  188. WorkflowServiceClientError,
  189. ) as exc:
  190. raise HTTPException(status_code=502, detail=str(exc)) from exc
  191. if result is None:
  192. raise HTTPException(status_code=404, detail=f"queued node_run not found for run: {run_id}")
  193. workflow_run, node_run, executor_name = result
  194. return NodeRunExecuteResponse(
  195. run=WorkflowRunResponse.from_entity(workflow_run),
  196. node_run=NodeRunResponse.from_entity(node_run),
  197. executor_name=executor_name,
  198. )
  199. @router.post("/runs/{run_id}/execute", response_model=RunExecuteResponse)
  200. def execute_run(
  201. run_id: str,
  202. payload: RunExecuteRequest,
  203. tenant_id: str = Query(...),
  204. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  205. ) -> RunExecuteResponse:
  206. try:
  207. result = service.execute_run(
  208. tenant_id=tenant_id,
  209. run_id=run_id,
  210. payload=payload,
  211. )
  212. except (
  213. CodeRunnerClientError,
  214. ModelGatewayClientError,
  215. ToolServiceClientError,
  216. WorkflowServiceClientError,
  217. ) as exc:
  218. raise HTTPException(status_code=502, detail=str(exc)) from exc
  219. if result is None:
  220. raise HTTPException(status_code=404, detail=f"workflow_run not found: {run_id}")
  221. workflow_run, node_runs, executor_names = result
  222. return RunExecuteResponse(
  223. run=WorkflowRunResponse.from_entity(workflow_run),
  224. node_runs=[NodeRunResponse.from_entity(item) for item in node_runs],
  225. executor_names=executor_names,
  226. )
  227. @router.post("/workers/execute-next", response_model=WorkerExecuteNextResponse)
  228. def execute_next_worker_task(
  229. payload: WorkerExecuteNextRequest,
  230. settings: RuntimeServiceSettings = Depends(get_runtime_settings),
  231. service: RuntimeApplicationService = Depends(get_runtime_application_service),
  232. ) -> WorkerExecuteNextResponse:
  233. try:
  234. result = service.execute_next_claimed_node_run(
  235. worker_key=payload.worker_key,
  236. lease_seconds=payload.lease_seconds or settings.worker_lease_seconds,
  237. )
  238. except (
  239. CodeRunnerClientError,
  240. ModelGatewayClientError,
  241. ToolServiceClientError,
  242. WorkflowServiceClientError,
  243. ) as exc:
  244. raise HTTPException(status_code=502, detail=str(exc)) from exc
  245. if result is None:
  246. raise HTTPException(status_code=404, detail="queued worker task not found")
  247. workflow_run, node_run, executor_name, released_lease_count = result
  248. return WorkerExecuteNextResponse(
  249. run=WorkflowRunResponse.from_entity(workflow_run),
  250. node_run=NodeRunResponse.from_entity(node_run),
  251. executor_name=executor_name,
  252. released_lease_count=released_lease_count,
  253. )