entities.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from typing import Annotated, Literal, Optional
  2. from pydantic import AfterValidator, BaseModel
  3. from core.helper.code_executor.code_executor import CodeLanguage
  4. from core.variables.types import SegmentType
  5. from core.workflow.entities.variable_entities import VariableSelector
  6. from core.workflow.nodes.base import BaseNodeData
  7. _ALLOWED_OUTPUT_FROM_CODE = frozenset(
  8. [
  9. SegmentType.STRING,
  10. SegmentType.NUMBER,
  11. SegmentType.OBJECT,
  12. SegmentType.BOOLEAN,
  13. SegmentType.ARRAY_STRING,
  14. SegmentType.ARRAY_NUMBER,
  15. SegmentType.ARRAY_OBJECT,
  16. SegmentType.ARRAY_BOOLEAN,
  17. ]
  18. )
  19. def _validate_type(segment_type: SegmentType) -> SegmentType:
  20. if segment_type not in _ALLOWED_OUTPUT_FROM_CODE:
  21. raise ValueError(f"invalid type for code output, expected {_ALLOWED_OUTPUT_FROM_CODE}, actual {segment_type}")
  22. return segment_type
  23. class CodeNodeData(BaseNodeData):
  24. """
  25. Code Node Data.
  26. """
  27. class Output(BaseModel):
  28. type: Annotated[SegmentType, AfterValidator(_validate_type)]
  29. children: Optional[dict[str, "CodeNodeData.Output"]] = None
  30. class Dependency(BaseModel):
  31. name: str
  32. version: str
  33. variables: list[VariableSelector]
  34. code_language: Literal[CodeLanguage.PYTHON3, CodeLanguage.JAVASCRIPT]
  35. code: str
  36. outputs: dict[str, Output]
  37. dependencies: Optional[list[Dependency]] = None