entities.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.tools.entities.tool_entities import ToolProviderType
  5. from core.workflow.entities.base_node_data_entities import BaseNodeData
  6. class ToolEntity(BaseModel):
  7. provider_id: str
  8. provider_type: ToolProviderType
  9. provider_name: str # redundancy
  10. tool_name: str
  11. tool_label: str # redundancy
  12. tool_configurations: dict[str, Any]
  13. @field_validator("tool_configurations", mode="before")
  14. @classmethod
  15. def validate_tool_configurations(cls, value, values: ValidationInfo):
  16. if not isinstance(value, dict):
  17. raise ValueError("tool_configurations must be a dictionary")
  18. for key in values.data.get("tool_configurations", {}):
  19. value = values.data.get("tool_configurations", {}).get(key)
  20. if not isinstance(value, str | int | float | bool):
  21. raise ValueError(f"{key} must be a string")
  22. return value
  23. class ToolNodeData(BaseNodeData, ToolEntity):
  24. class ToolInput(BaseModel):
  25. # TODO: check this type
  26. value: Union[Any, list[str]]
  27. type: Literal["mixed", "variable", "constant"]
  28. @field_validator("type", mode="before")
  29. @classmethod
  30. def check_type(cls, value, validation_info: ValidationInfo):
  31. typ = value
  32. value = validation_info.data.get("value")
  33. if typ == "mixed" and not isinstance(value, str):
  34. raise ValueError("value must be a string")
  35. elif typ == "variable":
  36. if not isinstance(value, list):
  37. raise ValueError("value must be a list")
  38. for val in value:
  39. if not isinstance(val, str):
  40. raise ValueError("value must be a list of strings")
  41. elif typ == "constant" and not isinstance(value, str | int | float | bool):
  42. raise ValueError("value must be a string, int, float, or bool")
  43. return typ
  44. """
  45. Tool Node Schema
  46. """
  47. tool_parameters: dict[str, ToolInput]