tool_provider.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from abc import ABC, abstractmethod
  2. from copy import deepcopy
  3. from typing import Any
  4. from core.entities.provider_entities import ProviderConfig
  5. from core.tools.__base.tool import Tool
  6. from core.tools.entities.tool_entities import (
  7. ToolProviderEntity,
  8. ToolProviderType,
  9. )
  10. from core.tools.errors import ToolProviderCredentialValidationError
  11. class ToolProviderController(ABC):
  12. def __init__(self, entity: ToolProviderEntity) -> None:
  13. self.entity = entity
  14. def get_credentials_schema(self) -> list[ProviderConfig]:
  15. """
  16. returns the credentials schema of the provider
  17. :return: the credentials schema
  18. """
  19. return deepcopy(self.entity.credentials_schema)
  20. @abstractmethod
  21. def get_tool(self, tool_name: str) -> Tool:
  22. """
  23. returns a tool that the provider can provide
  24. :return: tool
  25. """
  26. pass
  27. @property
  28. def provider_type(self) -> ToolProviderType:
  29. """
  30. returns the type of the provider
  31. :return: type of the provider
  32. """
  33. return ToolProviderType.BUILT_IN
  34. def validate_credentials_format(self, credentials: dict[str, Any]) -> None:
  35. """
  36. validate the format of the credentials of the provider and set the default value if needed
  37. :param credentials: the credentials of the tool
  38. """
  39. credentials_schema = dict[str, ProviderConfig]()
  40. if credentials_schema is None:
  41. return
  42. for credential in self.entity.credentials_schema:
  43. credentials_schema[credential.name] = credential
  44. credentials_need_to_validate: dict[str, ProviderConfig] = {}
  45. for credential_name in credentials_schema:
  46. credentials_need_to_validate[credential_name] = credentials_schema[credential_name]
  47. for credential_name in credentials:
  48. if credential_name not in credentials_need_to_validate:
  49. raise ToolProviderCredentialValidationError(
  50. f"credential {credential_name} not found in provider {self.entity.identity.name}"
  51. )
  52. # check type
  53. credential_schema = credentials_need_to_validate[credential_name]
  54. if not credential_schema.required and credentials[credential_name] is None:
  55. continue
  56. if credential_schema.type in {ProviderConfig.Type.SECRET_INPUT, ProviderConfig.Type.TEXT_INPUT}:
  57. if not isinstance(credentials[credential_name], str):
  58. raise ToolProviderCredentialValidationError(f"credential {credential_name} should be string")
  59. elif credential_schema.type == ProviderConfig.Type.SELECT:
  60. if not isinstance(credentials[credential_name], str):
  61. raise ToolProviderCredentialValidationError(f"credential {credential_name} should be string")
  62. options = credential_schema.options
  63. if not isinstance(options, list):
  64. raise ToolProviderCredentialValidationError(f"credential {credential_name} options should be list")
  65. if credentials[credential_name] not in [x.value for x in options]:
  66. raise ToolProviderCredentialValidationError(
  67. f"credential {credential_name} should be one of {options}"
  68. )
  69. credentials_need_to_validate.pop(credential_name)
  70. for credential_name in credentials_need_to_validate:
  71. credential_schema = credentials_need_to_validate[credential_name]
  72. if credential_schema.required:
  73. raise ToolProviderCredentialValidationError(f"credential {credential_name} is required")
  74. # the credential is not set currently, set the default value if needed
  75. if credential_schema.default is not None:
  76. default_value = credential_schema.default
  77. # parse default value into the correct type
  78. if credential_schema.type in {
  79. ProviderConfig.Type.SECRET_INPUT,
  80. ProviderConfig.Type.TEXT_INPUT,
  81. ProviderConfig.Type.SELECT,
  82. }:
  83. default_value = str(default_value)
  84. credentials[credential_name] = default_value