routes.py 9.9 KB

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