entities.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from typing import Any, Literal, Union
  2. from pydantic import BaseModel, ValidationInfo, field_validator
  3. from core.workflow.nodes.base.entities import BaseNodeData
  4. class AgentEntity(BaseModel):
  5. agent_strategy_provider_name: str # redundancy
  6. agent_strategy_name: str
  7. agent_strategy_label: str # redundancy
  8. agent_configurations: dict[str, Any]
  9. plugin_unique_identifier: str
  10. @field_validator("agent_configurations", mode="before")
  11. @classmethod
  12. def validate_agent_configurations(cls, value, values: ValidationInfo):
  13. if not isinstance(value, dict):
  14. raise ValueError("agent_configurations must be a dictionary")
  15. for key in values.data.get("agent_configurations", {}):
  16. value = values.data.get("agent_configurations", {}).get(key)
  17. if not isinstance(value, str | int | float | bool):
  18. raise ValueError(f"{key} must be a string")
  19. return value
  20. class AgentNodeData(BaseNodeData, AgentEntity):
  21. class AgentInput(BaseModel):
  22. # TODO: check this type
  23. value: Union[Any, list[str]]
  24. type: Literal["mixed", "variable", "constant"]
  25. @field_validator("type", mode="before")
  26. @classmethod
  27. def check_type(cls, value, validation_info: ValidationInfo):
  28. typ = value
  29. value = validation_info.data.get("value")
  30. if typ == "mixed" and not isinstance(value, str):
  31. raise ValueError("value must be a string")
  32. elif typ == "variable":
  33. if not isinstance(value, list):
  34. raise ValueError("value must be a list")
  35. for val in value:
  36. if not isinstance(val, str):
  37. raise ValueError("value must be a list of strings")
  38. elif typ == "constant" and not isinstance(value, str | int | float | bool):
  39. raise ValueError("value must be a string, int, float, or bool")
  40. return typ
  41. agent_parameters: dict[str, AgentInput]