tool.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 parts[0], 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. return response
  39. def fetch_tool_provider(self, tenant_id: str, provider: str) -> PluginToolProviderEntity:
  40. """
  41. Fetch tool provider for the given tenant and plugin.
  42. """
  43. plugin_id, provider_name = self._split_provider(provider)
  44. response = self._request_with_plugin_daemon_response(
  45. "GET",
  46. f"plugin/{tenant_id}/management/tool",
  47. PluginToolProviderEntity,
  48. params={"provider": provider_name, "plugin_id": plugin_id},
  49. )
  50. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  51. return response
  52. def invoke(
  53. self,
  54. tenant_id: str,
  55. user_id: str,
  56. tool_provider: str,
  57. tool_name: str,
  58. credentials: dict[str, Any],
  59. tool_parameters: dict[str, Any],
  60. ) -> Generator[ToolInvokeMessage, None, None]:
  61. """
  62. Invoke the tool with the given tenant, user, plugin, provider, name, credentials and parameters.
  63. """
  64. plugin_id, provider_name = self._split_provider(tool_provider)
  65. response = self._request_with_plugin_daemon_response_stream(
  66. "POST",
  67. f"plugin/{tenant_id}/dispatch/tool/invoke",
  68. ToolInvokeMessage,
  69. data={
  70. "user_id": user_id,
  71. "data": {
  72. "provider": provider_name,
  73. "tool": tool_name,
  74. "credentials": credentials,
  75. "tool_parameters": tool_parameters,
  76. },
  77. },
  78. headers={
  79. "X-Plugin-ID": plugin_id,
  80. "Content-Type": "application/json",
  81. },
  82. )
  83. return response
  84. def validate_provider_credentials(
  85. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  86. ) -> bool:
  87. """
  88. validate the credentials of the provider
  89. """
  90. plugin_id, provider_name = self._split_provider(provider)
  91. response = self._request_with_plugin_daemon_response_stream(
  92. "POST",
  93. f"plugin/{tenant_id}/dispatch/tool/validate_credentials",
  94. PluginBasicBooleanResponse,
  95. data={
  96. "user_id": user_id,
  97. "data": {
  98. "provider": provider_name,
  99. "credentials": credentials,
  100. },
  101. },
  102. headers={
  103. "X-Plugin-ID": plugin_id,
  104. "Content-Type": "application/json",
  105. },
  106. )
  107. for resp in response:
  108. return resp.result
  109. return False