run.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from datetime import datetime
  2. from typing import TYPE_CHECKING
  3. from pydantic import BaseModel
  4. from core_domain import (
  5. InitialNodeContract,
  6. NodeExecutionRequestContract,
  7. NodeRunContract,
  8. NodeRunStatusUpdateContract,
  9. RunBootstrapContract,
  10. RunCreateContract,
  11. RunExecutionRequestContract,
  12. WorkflowRunStatusUpdateContract,
  13. WorkflowRunContract,
  14. )
  15. from core_shared import JSONValue
  16. if TYPE_CHECKING:
  17. from app.db.models import ExecutionLog, NodeArtifact, NodeRun, TraceSpan, WorkflowRun
  18. class InitialNodeCreateRequest(InitialNodeContract):
  19. pass
  20. class RunCreateRequest(RunCreateContract):
  21. initial_node: InitialNodeCreateRequest | None = None
  22. class WorkflowRunResponse(WorkflowRunContract):
  23. @classmethod
  24. def from_entity(cls, entity: "WorkflowRun") -> "WorkflowRunResponse":
  25. return cls.model_validate(entity, from_attributes=True)
  26. class NodeRunResponse(NodeRunContract):
  27. @classmethod
  28. def from_entity(cls, entity: "NodeRun") -> "NodeRunResponse":
  29. return cls.model_validate(entity, from_attributes=True)
  30. class RunBootstrapResponse(RunBootstrapContract):
  31. run: WorkflowRunResponse
  32. initial_node: NodeRunResponse | None = None
  33. class WorkflowRunStatusUpdateRequest(WorkflowRunStatusUpdateContract):
  34. pass
  35. class NodeRunStatusUpdateRequest(NodeRunStatusUpdateContract):
  36. pass
  37. class NodeRunExecuteRequest(NodeExecutionRequestContract):
  38. pass
  39. class NodeRunExecuteResponse(BaseModel):
  40. run: WorkflowRunResponse
  41. node_run: NodeRunResponse
  42. executor_name: str
  43. class RunExecuteRequest(RunExecutionRequestContract):
  44. pass
  45. class RunExecuteResponse(BaseModel):
  46. run: WorkflowRunResponse
  47. node_runs: list[NodeRunResponse]
  48. executor_names: list[str]
  49. class ExecutionLogResponse(BaseModel):
  50. id: str
  51. tenant_id: str
  52. run_id: str
  53. node_run_id: str | None = None
  54. event_type: str
  55. level: str
  56. message: str
  57. detail_json: dict[str, JSONValue] | None = None
  58. created_time: datetime
  59. @classmethod
  60. def from_entity(cls, entity: "ExecutionLog") -> "ExecutionLogResponse":
  61. return cls.model_validate(entity, from_attributes=True)
  62. class NodeArtifactResponse(BaseModel):
  63. id: str
  64. tenant_id: str
  65. run_id: str
  66. node_run_id: str
  67. node_id: str
  68. artifact_type: str
  69. name: str
  70. mime_type: str | None = None
  71. content_text: str | None = None
  72. content_json: dict[str, JSONValue] | None = None
  73. storage_uri: str | None = None
  74. size_bytes: int | None = None
  75. created_time: datetime
  76. @classmethod
  77. def from_entity(cls, entity: "NodeArtifact") -> "NodeArtifactResponse":
  78. return cls.model_validate(entity, from_attributes=True)
  79. class TraceSpanResponse(BaseModel):
  80. id: str
  81. tenant_id: str
  82. run_id: str
  83. node_run_id: str | None = None
  84. parent_span_id: str | None = None
  85. span_type: str
  86. name: str
  87. status: str
  88. started_time: datetime
  89. ended_time: datetime | None = None
  90. duration_ms: int | None = None
  91. attributes_json: dict[str, JSONValue] | None = None
  92. error_code: str | None = None
  93. error_message: str | None = None
  94. created_time: datetime
  95. @classmethod
  96. def from_entity(cls, entity: "TraceSpan") -> "TraceSpanResponse":
  97. return cls.model_validate(entity, from_attributes=True)