您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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