Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from collections.abc import Sequence
  2. from typing import Any
  3. from pydantic import AliasChoices, BaseModel, ConfigDict, Field, model_validator
  4. from core.file.models import File
  5. from core.workflow.enums import SystemVariableKey
  6. class SystemVariable(BaseModel):
  7. """A model for managing system variables.
  8. Fields with a value of `None` are treated as absent and will not be included
  9. in the variable pool.
  10. """
  11. model_config = ConfigDict(
  12. extra="forbid",
  13. serialize_by_alias=True,
  14. validate_by_alias=True,
  15. )
  16. user_id: str | None = None
  17. # Ideally, `app_id` and `workflow_id` should be required and not `None`.
  18. # However, there are scenarios in the codebase where these fields are not set.
  19. # To maintain compatibility, they are marked as optional here.
  20. app_id: str | None = None
  21. workflow_id: str | None = None
  22. files: Sequence[File] = Field(default_factory=list)
  23. # NOTE: The `workflow_execution_id` field was previously named `workflow_run_id`.
  24. # To maintain compatibility with existing workflows, it must be serialized
  25. # as `workflow_run_id` in dictionaries or JSON objects, and also referenced
  26. # as `workflow_run_id` in the variable pool.
  27. workflow_execution_id: str | None = Field(
  28. validation_alias=AliasChoices("workflow_execution_id", "workflow_run_id"),
  29. serialization_alias="workflow_run_id",
  30. default=None,
  31. )
  32. # Chatflow related fields.
  33. query: str | None = None
  34. conversation_id: str | None = None
  35. dialogue_count: int | None = None
  36. @model_validator(mode="before")
  37. @classmethod
  38. def validate_json_fields(cls, data):
  39. if isinstance(data, dict):
  40. # For JSON validation, only allow workflow_run_id
  41. if "workflow_execution_id" in data and "workflow_run_id" not in data:
  42. # This is likely from direct instantiation, allow it
  43. return data
  44. elif "workflow_execution_id" in data and "workflow_run_id" in data:
  45. # Both present, remove workflow_execution_id
  46. data = data.copy()
  47. data.pop("workflow_execution_id")
  48. return data
  49. return data
  50. @classmethod
  51. def empty(cls) -> "SystemVariable":
  52. return cls()
  53. def to_dict(self) -> dict[SystemVariableKey, Any]:
  54. # NOTE: This method is provided for compatibility with legacy code.
  55. # New code should use the `SystemVariable` object directly instead of converting
  56. # it to a dictionary, as this conversion results in the loss of type information
  57. # for each key, making static analysis more difficult.
  58. d: dict[SystemVariableKey, Any] = {
  59. SystemVariableKey.FILES: self.files,
  60. }
  61. if self.user_id is not None:
  62. d[SystemVariableKey.USER_ID] = self.user_id
  63. if self.app_id is not None:
  64. d[SystemVariableKey.APP_ID] = self.app_id
  65. if self.workflow_id is not None:
  66. d[SystemVariableKey.WORKFLOW_ID] = self.workflow_id
  67. if self.workflow_execution_id is not None:
  68. d[SystemVariableKey.WORKFLOW_EXECUTION_ID] = self.workflow_execution_id
  69. if self.query is not None:
  70. d[SystemVariableKey.QUERY] = self.query
  71. if self.conversation_id is not None:
  72. d[SystemVariableKey.CONVERSATION_ID] = self.conversation_id
  73. if self.dialogue_count is not None:
  74. d[SystemVariableKey.DIALOGUE_COUNT] = self.dialogue_count
  75. return d