variable_factory.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from collections.abc import Mapping, Sequence
  2. from typing import Any
  3. from uuid import uuid4
  4. from configs import dify_config
  5. from core.file import File
  6. from core.variables.exc import VariableError
  7. from core.variables.segments import (
  8. ArrayAnySegment,
  9. ArrayFileSegment,
  10. ArrayNumberSegment,
  11. ArrayObjectSegment,
  12. ArraySegment,
  13. ArrayStringSegment,
  14. FileSegment,
  15. FloatSegment,
  16. IntegerSegment,
  17. NoneSegment,
  18. ObjectSegment,
  19. Segment,
  20. StringSegment,
  21. )
  22. from core.variables.types import SegmentType
  23. from core.variables.variables import (
  24. ArrayAnyVariable,
  25. ArrayFileVariable,
  26. ArrayNumberVariable,
  27. ArrayObjectVariable,
  28. ArrayStringVariable,
  29. FileVariable,
  30. FloatVariable,
  31. IntegerVariable,
  32. NoneVariable,
  33. ObjectVariable,
  34. SecretVariable,
  35. StringVariable,
  36. Variable,
  37. )
  38. from core.workflow.constants import CONVERSATION_VARIABLE_NODE_ID, ENVIRONMENT_VARIABLE_NODE_ID
  39. class InvalidSelectorError(ValueError):
  40. pass
  41. class UnsupportedSegmentTypeError(Exception):
  42. pass
  43. # Define the constant
  44. SEGMENT_TO_VARIABLE_MAP = {
  45. StringSegment: StringVariable,
  46. IntegerSegment: IntegerVariable,
  47. FloatSegment: FloatVariable,
  48. ObjectSegment: ObjectVariable,
  49. FileSegment: FileVariable,
  50. ArrayStringSegment: ArrayStringVariable,
  51. ArrayNumberSegment: ArrayNumberVariable,
  52. ArrayObjectSegment: ArrayObjectVariable,
  53. ArrayFileSegment: ArrayFileVariable,
  54. ArrayAnySegment: ArrayAnyVariable,
  55. NoneSegment: NoneVariable,
  56. }
  57. def build_conversation_variable_from_mapping(mapping: Mapping[str, Any], /) -> Variable:
  58. if not mapping.get("name"):
  59. raise VariableError("missing name")
  60. return _build_variable_from_mapping(mapping=mapping, selector=[CONVERSATION_VARIABLE_NODE_ID, mapping["name"]])
  61. def build_environment_variable_from_mapping(mapping: Mapping[str, Any], /) -> Variable:
  62. if not mapping.get("name"):
  63. raise VariableError("missing name")
  64. return _build_variable_from_mapping(mapping=mapping, selector=[ENVIRONMENT_VARIABLE_NODE_ID, mapping["name"]])
  65. def _build_variable_from_mapping(*, mapping: Mapping[str, Any], selector: Sequence[str]) -> Variable:
  66. """
  67. This factory function is used to create the environment variable or the conversation variable,
  68. not support the File type.
  69. """
  70. if (value_type := mapping.get("value_type")) is None:
  71. raise VariableError("missing value type")
  72. if (value := mapping.get("value")) is None:
  73. raise VariableError("missing value")
  74. match value_type:
  75. case SegmentType.STRING:
  76. result = StringVariable.model_validate(mapping)
  77. case SegmentType.SECRET:
  78. result = SecretVariable.model_validate(mapping)
  79. case SegmentType.NUMBER if isinstance(value, int):
  80. result = IntegerVariable.model_validate(mapping)
  81. case SegmentType.NUMBER if isinstance(value, float):
  82. result = FloatVariable.model_validate(mapping)
  83. case SegmentType.NUMBER if not isinstance(value, float | int):
  84. raise VariableError(f"invalid number value {value}")
  85. case SegmentType.OBJECT if isinstance(value, dict):
  86. result = ObjectVariable.model_validate(mapping)
  87. case SegmentType.ARRAY_STRING if isinstance(value, list):
  88. result = ArrayStringVariable.model_validate(mapping)
  89. case SegmentType.ARRAY_NUMBER if isinstance(value, list):
  90. result = ArrayNumberVariable.model_validate(mapping)
  91. case SegmentType.ARRAY_OBJECT if isinstance(value, list):
  92. result = ArrayObjectVariable.model_validate(mapping)
  93. case _:
  94. raise VariableError(f"not supported value type {value_type}")
  95. if result.size > dify_config.MAX_VARIABLE_SIZE:
  96. raise VariableError(f"variable size {result.size} exceeds limit {dify_config.MAX_VARIABLE_SIZE}")
  97. if not result.selector:
  98. result = result.model_copy(update={"selector": selector})
  99. return result
  100. def build_segment(value: Any, /) -> Segment:
  101. if value is None:
  102. return NoneSegment()
  103. if isinstance(value, str):
  104. return StringSegment(value=value)
  105. if isinstance(value, int):
  106. return IntegerSegment(value=value)
  107. if isinstance(value, float):
  108. return FloatSegment(value=value)
  109. if isinstance(value, dict):
  110. return ObjectSegment(value=value)
  111. if isinstance(value, File):
  112. return FileSegment(value=value)
  113. if isinstance(value, list):
  114. items = [build_segment(item) for item in value]
  115. types = {item.value_type for item in items}
  116. if len(types) != 1 or all(isinstance(item, ArraySegment) for item in items):
  117. return ArrayAnySegment(value=value)
  118. match types.pop():
  119. case SegmentType.STRING:
  120. return ArrayStringSegment(value=value)
  121. case SegmentType.NUMBER:
  122. return ArrayNumberSegment(value=value)
  123. case SegmentType.OBJECT:
  124. return ArrayObjectSegment(value=value)
  125. case SegmentType.FILE:
  126. return ArrayFileSegment(value=value)
  127. case SegmentType.NONE:
  128. return ArrayAnySegment(value=value)
  129. case _:
  130. raise ValueError(f"not supported value {value}")
  131. raise ValueError(f"not supported value {value}")
  132. def segment_to_variable(
  133. *,
  134. segment: Segment,
  135. selector: Sequence[str],
  136. id: str | None = None,
  137. name: str | None = None,
  138. description: str = "",
  139. ) -> Variable:
  140. if isinstance(segment, Variable):
  141. return segment
  142. name = name or selector[-1]
  143. id = id or str(uuid4())
  144. segment_type = type(segment)
  145. if segment_type not in SEGMENT_TO_VARIABLE_MAP:
  146. raise UnsupportedSegmentTypeError(f"not supported segment type {segment_type}")
  147. variable_class = SEGMENT_TO_VARIABLE_MAP[segment_type]
  148. return variable_class(
  149. id=id,
  150. name=name,
  151. description=description,
  152. value=segment.value,
  153. selector=selector,
  154. )