tool.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. from collections.abc import Generator
  2. from typing import Any, Optional
  3. from pydantic import BaseModel
  4. from core.plugin.entities.plugin import GenericProviderID
  5. from core.plugin.entities.plugin_daemon import PluginBasicBooleanResponse, PluginToolProviderEntity
  6. from core.plugin.manager.base import BasePluginManager
  7. from core.tools.entities.tool_entities import ToolInvokeMessage, ToolParameter
  8. class PluginToolManager(BasePluginManager):
  9. def fetch_tool_providers(self, tenant_id: str) -> list[PluginToolProviderEntity]:
  10. """
  11. Fetch tool providers for the given tenant.
  12. """
  13. def transformer(json_response: dict[str, Any]) -> dict:
  14. for provider in json_response.get("data", []):
  15. declaration = provider.get("declaration", {}) or {}
  16. provider_name = declaration.get("identity", {}).get("name")
  17. for tool in declaration.get("tools", []):
  18. tool["identity"]["provider"] = provider_name
  19. return json_response
  20. response = self._request_with_plugin_daemon_response(
  21. "GET",
  22. f"plugin/{tenant_id}/management/tools",
  23. list[PluginToolProviderEntity],
  24. params={"page": 1, "page_size": 256},
  25. transformer=transformer,
  26. )
  27. for provider in response:
  28. provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  29. # override the provider name for each tool to plugin_id/provider_name
  30. for tool in provider.declaration.tools:
  31. tool.identity.provider = provider.declaration.identity.name
  32. return response
  33. def fetch_tool_provider(self, tenant_id: str, provider: str) -> PluginToolProviderEntity:
  34. """
  35. Fetch tool provider for the given tenant and plugin.
  36. """
  37. tool_provider_id = GenericProviderID(provider)
  38. def transformer(json_response: dict[str, Any]) -> dict:
  39. for tool in json_response.get("data", {}).get("declaration", {}).get("tools", []):
  40. tool["identity"]["provider"] = tool_provider_id.provider_name
  41. return json_response
  42. response = self._request_with_plugin_daemon_response(
  43. "GET",
  44. f"plugin/{tenant_id}/management/tool",
  45. PluginToolProviderEntity,
  46. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  47. transformer=transformer,
  48. )
  49. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  50. # override the provider name for each tool to plugin_id/provider_name
  51. for tool in response.declaration.tools:
  52. tool.identity.provider = response.declaration.identity.name
  53. return response
  54. def invoke(
  55. self,
  56. tenant_id: str,
  57. user_id: str,
  58. tool_provider: str,
  59. tool_name: str,
  60. credentials: dict[str, Any],
  61. tool_parameters: dict[str, Any],
  62. conversation_id: Optional[str] = None,
  63. app_id: Optional[str] = None,
  64. message_id: Optional[str] = None,
  65. ) -> Generator[ToolInvokeMessage, None, None]:
  66. """
  67. Invoke the tool with the given tenant, user, plugin, provider, name, credentials and parameters.
  68. """
  69. tool_provider_id = GenericProviderID(tool_provider)
  70. response = self._request_with_plugin_daemon_response_stream(
  71. "POST",
  72. f"plugin/{tenant_id}/dispatch/tool/invoke",
  73. ToolInvokeMessage,
  74. data={
  75. "user_id": user_id,
  76. "conversation_id": conversation_id,
  77. "app_id": app_id,
  78. "message_id": message_id,
  79. "data": {
  80. "provider": tool_provider_id.provider_name,
  81. "tool": tool_name,
  82. "credentials": credentials,
  83. "tool_parameters": tool_parameters,
  84. },
  85. },
  86. headers={
  87. "X-Plugin-ID": tool_provider_id.plugin_id,
  88. "Content-Type": "application/json",
  89. },
  90. )
  91. return response
  92. def validate_provider_credentials(
  93. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  94. ) -> bool:
  95. """
  96. validate the credentials of the provider
  97. """
  98. tool_provider_id = GenericProviderID(provider)
  99. response = self._request_with_plugin_daemon_response_stream(
  100. "POST",
  101. f"plugin/{tenant_id}/dispatch/tool/validate_credentials",
  102. PluginBasicBooleanResponse,
  103. data={
  104. "user_id": user_id,
  105. "data": {
  106. "provider": tool_provider_id.provider_name,
  107. "credentials": credentials,
  108. },
  109. },
  110. headers={
  111. "X-Plugin-ID": tool_provider_id.plugin_id,
  112. "Content-Type": "application/json",
  113. },
  114. )
  115. for resp in response:
  116. return resp.result
  117. return False
  118. def get_runtime_parameters(
  119. self,
  120. tenant_id: str,
  121. user_id: str,
  122. provider: str,
  123. credentials: dict[str, Any],
  124. tool: str,
  125. conversation_id: Optional[str] = None,
  126. app_id: Optional[str] = None,
  127. message_id: Optional[str] = None,
  128. ) -> list[ToolParameter]:
  129. """
  130. get the runtime parameters of the tool
  131. """
  132. tool_provider_id = GenericProviderID(provider)
  133. class RuntimeParametersResponse(BaseModel):
  134. parameters: list[ToolParameter]
  135. response = self._request_with_plugin_daemon_response_stream(
  136. "POST",
  137. f"plugin/{tenant_id}/dispatch/tool/get_runtime_parameters",
  138. RuntimeParametersResponse,
  139. data={
  140. "user_id": user_id,
  141. "conversation_id": conversation_id,
  142. "app_id": app_id,
  143. "message_id": message_id,
  144. "data": {
  145. "provider": tool_provider_id.provider_name,
  146. "tool": tool,
  147. "credentials": credentials,
  148. },
  149. },
  150. headers={
  151. "X-Plugin-ID": tool_provider_id.plugin_id,
  152. "Content-Type": "application/json",
  153. },
  154. )
  155. for resp in response:
  156. return resp.parameters
  157. return []