tool.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from collections.abc import Generator
  2. from typing import Any
  3. from core.plugin.entities.plugin_daemon import PluginBasicBooleanResponse, PluginToolProviderEntity
  4. from core.plugin.manager.base import BasePluginManager
  5. from core.tools.entities.tool_entities import ToolInvokeMessage
  6. class PluginToolManager(BasePluginManager):
  7. def _split_provider(self, provider: str) -> tuple[str, str]:
  8. """
  9. split the provider to plugin_id and provider_name
  10. provider follows format: plugin_id/provider_name
  11. """
  12. if "/" in provider:
  13. parts = provider.split("/", -1)
  14. if len(parts) >= 2:
  15. return "/".join(parts[:-1]), parts[-1]
  16. raise ValueError(f"invalid provider format: {provider}")
  17. raise ValueError(f"invalid provider format: {provider}")
  18. def fetch_tool_providers(self, tenant_id: str) -> list[PluginToolProviderEntity]:
  19. """
  20. Fetch tool providers for the given tenant.
  21. """
  22. def transformer(json_response: dict[str, Any]) -> dict:
  23. for provider in json_response.get("data", []):
  24. declaration = provider.get("declaration", {}) or {}
  25. provider_name = declaration.get("identity", {}).get("name")
  26. for tool in declaration.get("tools", []):
  27. tool["identity"]["provider"] = provider_name
  28. return json_response
  29. response = self._request_with_plugin_daemon_response(
  30. "GET",
  31. f"plugin/{tenant_id}/management/tools",
  32. list[PluginToolProviderEntity],
  33. params={"page": 1, "page_size": 256},
  34. transformer=transformer,
  35. )
  36. for provider in response:
  37. provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  38. # override the provider name for each tool to plugin_id/provider_name
  39. for tool in provider.declaration.tools:
  40. tool.identity.provider = provider.declaration.identity.name
  41. return response
  42. def fetch_tool_provider(self, tenant_id: str, provider: str) -> PluginToolProviderEntity:
  43. """
  44. Fetch tool provider for the given tenant and plugin.
  45. """
  46. plugin_id, provider_name = self._split_provider(provider)
  47. def transformer(json_response: dict[str, Any]) -> dict:
  48. for tool in json_response.get("data", {}).get("declaration", {}).get("tools", []):
  49. tool["identity"]["provider"] = provider_name
  50. return json_response
  51. response = self._request_with_plugin_daemon_response(
  52. "GET",
  53. f"plugin/{tenant_id}/management/tool",
  54. PluginToolProviderEntity,
  55. params={"provider": provider_name, "plugin_id": plugin_id},
  56. transformer=transformer,
  57. )
  58. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  59. # override the provider name for each tool to plugin_id/provider_name
  60. for tool in response.declaration.tools:
  61. tool.identity.provider = response.declaration.identity.name
  62. return response
  63. def invoke(
  64. self,
  65. tenant_id: str,
  66. user_id: str,
  67. tool_provider: str,
  68. tool_name: str,
  69. credentials: dict[str, Any],
  70. tool_parameters: dict[str, Any],
  71. ) -> Generator[ToolInvokeMessage, None, None]:
  72. """
  73. Invoke the tool with the given tenant, user, plugin, provider, name, credentials and parameters.
  74. """
  75. plugin_id, provider_name = self._split_provider(tool_provider)
  76. response = self._request_with_plugin_daemon_response_stream(
  77. "POST",
  78. f"plugin/{tenant_id}/dispatch/tool/invoke",
  79. ToolInvokeMessage,
  80. data={
  81. "user_id": user_id,
  82. "data": {
  83. "provider": provider_name,
  84. "tool": tool_name,
  85. "credentials": credentials,
  86. "tool_parameters": tool_parameters,
  87. },
  88. },
  89. headers={
  90. "X-Plugin-ID": plugin_id,
  91. "Content-Type": "application/json",
  92. },
  93. )
  94. return response
  95. def validate_provider_credentials(
  96. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  97. ) -> bool:
  98. """
  99. validate the credentials of the provider
  100. """
  101. plugin_id, provider_name = self._split_provider(provider)
  102. response = self._request_with_plugin_daemon_response_stream(
  103. "POST",
  104. f"plugin/{tenant_id}/dispatch/tool/validate_credentials",
  105. PluginBasicBooleanResponse,
  106. data={
  107. "user_id": user_id,
  108. "data": {
  109. "provider": provider_name,
  110. "credentials": credentials,
  111. },
  112. },
  113. headers={
  114. "X-Plugin-ID": plugin_id,
  115. "Content-Type": "application/json",
  116. },
  117. )
  118. for resp in response:
  119. return resp.result
  120. return False