entities.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from typing import Any, Literal, Union
  2. from pydantic import BaseModel, field_validator
  3. from pydantic_core.core_schema import ValidationInfo
  4. from core.workflow.nodes.base import BaseNodeData
  5. class ToolEntity(BaseModel):
  6. provider_id: str
  7. provider_type: Literal["builtin", "api", "workflow"]
  8. provider_name: str # redundancy
  9. tool_name: str
  10. tool_label: str # redundancy
  11. tool_configurations: dict[str, Any]
  12. @field_validator("tool_configurations", mode="before")
  13. @classmethod
  14. def validate_tool_configurations(cls, value, values: ValidationInfo):
  15. if not isinstance(value, dict):
  16. raise ValueError("tool_configurations must be a dictionary")
  17. for key in values.data.get("tool_configurations", {}):
  18. value = values.data.get("tool_configurations", {}).get(key)
  19. if not isinstance(value, str | int | float | bool):
  20. raise ValueError(f"{key} must be a string")
  21. return value
  22. class ToolNodeData(BaseNodeData, ToolEntity):
  23. class ToolInput(BaseModel):
  24. # TODO: check this type
  25. value: Union[Any, list[str]]
  26. type: Literal["mixed", "variable", "constant"]
  27. @field_validator("type", mode="before")
  28. @classmethod
  29. def check_type(cls, value, validation_info: ValidationInfo):
  30. typ = value
  31. value = validation_info.data.get("value")
  32. if typ == "mixed" and not isinstance(value, str):
  33. raise ValueError("value must be a string")
  34. elif typ == "variable":
  35. if not isinstance(value, list):
  36. raise ValueError("value must be a list")
  37. for val in value:
  38. if not isinstance(val, str):
  39. raise ValueError("value must be a list of strings")
  40. elif typ == "constant" and not isinstance(value, str | int | float | bool):
  41. raise ValueError("value must be a string, int, float, or bool")
  42. return typ
  43. tool_parameters: dict[str, ToolInput]