run.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 HumanNodeResumeRequest(BaseModel):
  58. tenant_id: str
  59. human_task_id: str
  60. worker_key: str | None = None
  61. class ExecutionLogResponse(BaseModel):
  62. id: str
  63. tenant_id: str
  64. run_id: str
  65. node_run_id: str | None = None
  66. event_type: str
  67. level: str
  68. message: str
  69. detail_json: dict[str, JSONValue] | None = None
  70. created_time: datetime
  71. @classmethod
  72. def from_entity(cls, entity: "ExecutionLog") -> "ExecutionLogResponse":
  73. return cls.model_validate(entity, from_attributes=True)
  74. class NodeArtifactResponse(BaseModel):
  75. id: str
  76. tenant_id: str
  77. run_id: str
  78. node_run_id: str
  79. node_id: str
  80. artifact_type: str
  81. name: str
  82. mime_type: str | None = None
  83. content_text: str | None = None
  84. content_json: dict[str, JSONValue] | None = None
  85. storage_uri: str | None = None
  86. size_bytes: int | None = None
  87. created_time: datetime
  88. @classmethod
  89. def from_entity(cls, entity: "NodeArtifact") -> "NodeArtifactResponse":
  90. return cls.model_validate(entity, from_attributes=True)
  91. class TraceSpanResponse(BaseModel):
  92. id: str
  93. tenant_id: str
  94. run_id: str
  95. node_run_id: str | None = None
  96. parent_span_id: str | None = None
  97. span_type: str
  98. name: str
  99. status: str
  100. started_time: datetime
  101. ended_time: datetime | None = None
  102. duration_ms: int | None = None
  103. attributes_json: dict[str, JSONValue] | None = None
  104. error_code: str | None = None
  105. error_message: str | None = None
  106. created_time: datetime
  107. @classmethod
  108. def from_entity(cls, entity: "TraceSpan") -> "TraceSpanResponse":
  109. return cls.model_validate(entity, from_attributes=True)