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

system_variable.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from collections.abc import Mapping, 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. document_id: str | None = None
  37. original_document_id: str | None = None
  38. dataset_id: str | None = None
  39. batch: str | None = None
  40. datasource_type: str | None = None
  41. datasource_info: Mapping[str, Any] | None = None
  42. invoke_from: str | None = None
  43. @model_validator(mode="before")
  44. @classmethod
  45. def validate_json_fields(cls, data):
  46. if isinstance(data, dict):
  47. # For JSON validation, only allow workflow_run_id
  48. if "workflow_execution_id" in data and "workflow_run_id" not in data:
  49. # This is likely from direct instantiation, allow it
  50. return data
  51. elif "workflow_execution_id" in data and "workflow_run_id" in data:
  52. # Both present, remove workflow_execution_id
  53. data = data.copy()
  54. data.pop("workflow_execution_id")
  55. return data
  56. return data
  57. @classmethod
  58. def empty(cls) -> "SystemVariable":
  59. return cls()
  60. def to_dict(self) -> dict[SystemVariableKey, Any]:
  61. # NOTE: This method is provided for compatibility with legacy code.
  62. # New code should use the `SystemVariable` object directly instead of converting
  63. # it to a dictionary, as this conversion results in the loss of type information
  64. # for each key, making static analysis more difficult.
  65. d: dict[SystemVariableKey, Any] = {
  66. SystemVariableKey.FILES: self.files,
  67. }
  68. if self.user_id is not None:
  69. d[SystemVariableKey.USER_ID] = self.user_id
  70. if self.app_id is not None:
  71. d[SystemVariableKey.APP_ID] = self.app_id
  72. if self.workflow_id is not None:
  73. d[SystemVariableKey.WORKFLOW_ID] = self.workflow_id
  74. if self.workflow_execution_id is not None:
  75. d[SystemVariableKey.WORKFLOW_EXECUTION_ID] = self.workflow_execution_id
  76. if self.query is not None:
  77. d[SystemVariableKey.QUERY] = self.query
  78. if self.conversation_id is not None:
  79. d[SystemVariableKey.CONVERSATION_ID] = self.conversation_id
  80. if self.dialogue_count is not None:
  81. d[SystemVariableKey.DIALOGUE_COUNT] = self.dialogue_count
  82. if self.document_id is not None:
  83. d[SystemVariableKey.DOCUMENT_ID] = self.document_id
  84. if self.original_document_id is not None:
  85. d[SystemVariableKey.ORIGINAL_DOCUMENT_ID] = self.original_document_id
  86. if self.dataset_id is not None:
  87. d[SystemVariableKey.DATASET_ID] = self.dataset_id
  88. if self.batch is not None:
  89. d[SystemVariableKey.BATCH] = self.batch
  90. if self.datasource_type is not None:
  91. d[SystemVariableKey.DATASOURCE_TYPE] = self.datasource_type
  92. if self.datasource_info is not None:
  93. d[SystemVariableKey.DATASOURCE_INFO] = self.datasource_info
  94. if self.invoke_from is not None:
  95. d[SystemVariableKey.INVOKE_FROM] = self.invoke_from
  96. return d