entities.py 2.1 KB

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