entities.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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', {}).keys():
  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. 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. """
  44. Tool Node Schema
  45. """
  46. tool_parameters: dict[str, ToolInput]