factory.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from collections.abc import Mapping
  2. from typing import Any, Optional, cast
  3. from core.extension.extensible import ExtensionModule
  4. from extensions.ext_code_based_extension import code_based_extension
  5. class ExternalDataToolFactory:
  6. def __init__(self, name: str, tenant_id: str, app_id: str, variable: str, config: dict) -> None:
  7. extension_class = code_based_extension.extension_class(ExtensionModule.EXTERNAL_DATA_TOOL, name)
  8. self.__extension_instance = extension_class(
  9. tenant_id=tenant_id, app_id=app_id, variable=variable, config=config
  10. )
  11. @classmethod
  12. def validate_config(cls, name: str, tenant_id: str, config: dict) -> None:
  13. """
  14. Validate the incoming form config data.
  15. :param name: the name of external data tool
  16. :param tenant_id: the id of workspace
  17. :param config: the form config data
  18. :return:
  19. """
  20. extension_class = code_based_extension.extension_class(ExtensionModule.EXTERNAL_DATA_TOOL, name)
  21. # FIXME mypy issue here, figure out how to fix it
  22. extension_class.validate_config(tenant_id, config) # type: ignore
  23. def query(self, inputs: Mapping[str, Any], query: Optional[str] = None) -> str:
  24. """
  25. Query the external data tool.
  26. :param inputs: user inputs
  27. :param query: the query of chat app
  28. :return: the tool query result
  29. """
  30. return cast(str, self.__extension_instance.query(inputs, query))