tool.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from collections.abc import Generator
  2. from typing import Any
  3. from core.plugin.entities.plugin_daemon import PluginToolProviderEntity
  4. from core.plugin.manager.base import BasePluginManager
  5. from core.tools.entities.tool_entities import ToolInvokeMessage
  6. class PluginToolManager(BasePluginManager):
  7. def fetch_tool_providers(self, tenant_id: str) -> list[PluginToolProviderEntity]:
  8. """
  9. Fetch tool providers for the given asset.
  10. """
  11. response = self._request_with_plugin_daemon_response(
  12. "GET", f"plugin/{tenant_id}/tools", list[PluginToolProviderEntity], params={"page": 1, "page_size": 256}
  13. )
  14. return response
  15. def invoke(
  16. self,
  17. tenant_id: str,
  18. user_id: str,
  19. plugin_unique_identifier: str,
  20. tool_provider: str,
  21. tool_name: str,
  22. credentials: dict[str, Any],
  23. tool_parameters: dict[str, Any],
  24. ) -> Generator[ToolInvokeMessage, None, None]:
  25. response = self._request_with_plugin_daemon_response_stream(
  26. "POST",
  27. f"plugin/{tenant_id}/tool/invoke",
  28. ToolInvokeMessage,
  29. data={
  30. "plugin_unique_identifier": plugin_unique_identifier,
  31. "user_id": user_id,
  32. "data": {
  33. "provider": tool_provider,
  34. "tool": tool_name,
  35. "credentials": credentials,
  36. "tool_parameters": tool_parameters,
  37. },
  38. },
  39. )
  40. return response
  41. def validate_provider_credentials(
  42. self, tenant_id: str, user_id: str, plugin_unique_identifier: str, provider: str, credentials: dict[str, Any]
  43. ) -> bool:
  44. """
  45. validate the credentials of the provider
  46. """
  47. response = self._request_with_plugin_daemon_response(
  48. "POST",
  49. f"plugin/{tenant_id}/tool/validate_credentials",
  50. bool,
  51. data={
  52. "plugin_unique_identifier": plugin_unique_identifier,
  53. "user_id": user_id,
  54. "data": {
  55. "provider": provider,
  56. "credentials": credentials,
  57. },
  58. },
  59. )
  60. return response