You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from collections.abc import Sequence
  2. from typing import Annotated, TypeAlias
  3. from uuid import uuid4
  4. from pydantic import Discriminator, Field, Tag
  5. from core.helper import encrypter
  6. from .segments import (
  7. ArrayAnySegment,
  8. ArrayBooleanSegment,
  9. ArrayFileSegment,
  10. ArrayNumberSegment,
  11. ArrayObjectSegment,
  12. ArraySegment,
  13. ArrayStringSegment,
  14. BooleanSegment,
  15. FileSegment,
  16. FloatSegment,
  17. IntegerSegment,
  18. NoneSegment,
  19. ObjectSegment,
  20. Segment,
  21. StringSegment,
  22. get_segment_discriminator,
  23. )
  24. from .types import SegmentType
  25. class Variable(Segment):
  26. """
  27. A variable is a segment that has a name.
  28. It is mainly used to store segments and their selector in VariablePool.
  29. Note: this class is abstract, you should use subclasses of this class instead.
  30. """
  31. id: str = Field(
  32. default_factory=lambda: str(uuid4()),
  33. description="Unique identity for variable.",
  34. )
  35. name: str
  36. description: str = Field(default="", description="Description of the variable.")
  37. selector: Sequence[str] = Field(default_factory=list)
  38. class StringVariable(StringSegment, Variable):
  39. pass
  40. class FloatVariable(FloatSegment, Variable):
  41. pass
  42. class IntegerVariable(IntegerSegment, Variable):
  43. pass
  44. class ObjectVariable(ObjectSegment, Variable):
  45. pass
  46. class ArrayVariable(ArraySegment, Variable):
  47. pass
  48. class ArrayAnyVariable(ArrayAnySegment, ArrayVariable):
  49. pass
  50. class ArrayStringVariable(ArrayStringSegment, ArrayVariable):
  51. pass
  52. class ArrayNumberVariable(ArrayNumberSegment, ArrayVariable):
  53. pass
  54. class ArrayObjectVariable(ArrayObjectSegment, ArrayVariable):
  55. pass
  56. class SecretVariable(StringVariable):
  57. value_type: SegmentType = SegmentType.SECRET
  58. @property
  59. def log(self) -> str:
  60. return encrypter.obfuscated_token(self.value)
  61. class NoneVariable(NoneSegment, Variable):
  62. value_type: SegmentType = SegmentType.NONE
  63. value: None = None
  64. class FileVariable(FileSegment, Variable):
  65. pass
  66. class BooleanVariable(BooleanSegment, Variable):
  67. pass
  68. class ArrayFileVariable(ArrayFileSegment, ArrayVariable):
  69. pass
  70. class ArrayBooleanVariable(ArrayBooleanSegment, ArrayVariable):
  71. pass
  72. # The `VariableUnion`` type is used to enable serialization and deserialization with Pydantic.
  73. # Use `Variable` for type hinting when serialization is not required.
  74. #
  75. # Note:
  76. # - All variants in `VariableUnion` must inherit from the `Variable` class.
  77. # - The union must include all non-abstract subclasses of `Segment`, except:
  78. VariableUnion: TypeAlias = Annotated[
  79. (
  80. Annotated[NoneVariable, Tag(SegmentType.NONE)]
  81. | Annotated[StringVariable, Tag(SegmentType.STRING)]
  82. | Annotated[FloatVariable, Tag(SegmentType.FLOAT)]
  83. | Annotated[IntegerVariable, Tag(SegmentType.INTEGER)]
  84. | Annotated[ObjectVariable, Tag(SegmentType.OBJECT)]
  85. | Annotated[FileVariable, Tag(SegmentType.FILE)]
  86. | Annotated[BooleanVariable, Tag(SegmentType.BOOLEAN)]
  87. | Annotated[ArrayAnyVariable, Tag(SegmentType.ARRAY_ANY)]
  88. | Annotated[ArrayStringVariable, Tag(SegmentType.ARRAY_STRING)]
  89. | Annotated[ArrayNumberVariable, Tag(SegmentType.ARRAY_NUMBER)]
  90. | Annotated[ArrayObjectVariable, Tag(SegmentType.ARRAY_OBJECT)]
  91. | Annotated[ArrayFileVariable, Tag(SegmentType.ARRAY_FILE)]
  92. | Annotated[ArrayBooleanVariable, Tag(SegmentType.ARRAY_BOOLEAN)]
  93. | Annotated[SecretVariable, Tag(SegmentType.SECRET)]
  94. ),
  95. Discriminator(get_segment_discriminator),
  96. ]