run.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from datetime import datetime
  2. from typing import TYPE_CHECKING
  3. from pydantic import BaseModel, Field
  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 WorkerExecuteNextRequest(BaseModel):
  50. worker_key: str
  51. lease_seconds: int | None = Field(default=None, gt=0)
  52. class WorkerExecuteNextResponse(BaseModel):
  53. run: WorkflowRunResponse
  54. node_run: NodeRunResponse
  55. executor_name: str
  56. released_lease_count: int = 0
  57. class ExecutionLogResponse(BaseModel):
  58. id: str
  59. tenant_id: str
  60. run_id: str
  61. node_run_id: str | None = None
  62. event_type: str
  63. level: str
  64. message: str
  65. detail_json: dict[str, JSONValue] | None = None
  66. created_time: datetime
  67. @classmethod
  68. def from_entity(cls, entity: "ExecutionLog") -> "ExecutionLogResponse":
  69. return cls.model_validate(entity, from_attributes=True)
  70. class NodeArtifactResponse(BaseModel):
  71. id: str
  72. tenant_id: str
  73. run_id: str
  74. node_run_id: str
  75. node_id: str
  76. artifact_type: str
  77. name: str
  78. mime_type: str | None = None
  79. content_text: str | None = None
  80. content_json: dict[str, JSONValue] | None = None
  81. storage_uri: str | None = None
  82. size_bytes: int | None = None
  83. created_time: datetime
  84. @classmethod
  85. def from_entity(cls, entity: "NodeArtifact") -> "NodeArtifactResponse":
  86. return cls.model_validate(entity, from_attributes=True)
  87. class TraceSpanResponse(BaseModel):
  88. id: str
  89. tenant_id: str
  90. run_id: str
  91. node_run_id: str | None = None
  92. parent_span_id: str | None = None
  93. span_type: str
  94. name: str
  95. status: str
  96. started_time: datetime
  97. ended_time: datetime | None = None
  98. duration_ms: int | None = None
  99. attributes_json: dict[str, JSONValue] | None = None
  100. error_code: str | None = None
  101. error_message: str | None = None
  102. created_time: datetime
  103. @classmethod
  104. def from_entity(cls, entity: "TraceSpan") -> "TraceSpanResponse":
  105. return cls.model_validate(entity, from_attributes=True)