tool.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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, ToolProviderID
  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 = ToolProviderID(provider)
  38. def transformer(json_response: dict[str, Any]) -> dict:
  39. data = json_response.get("data")
  40. if data:
  41. for tool in data.get("declaration", {}).get("tools", []):
  42. tool["identity"]["provider"] = tool_provider_id.provider_name
  43. return json_response
  44. response = self._request_with_plugin_daemon_response(
  45. "GET",
  46. f"plugin/{tenant_id}/management/tool",
  47. PluginToolProviderEntity,
  48. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  49. transformer=transformer,
  50. )
  51. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  52. # override the provider name for each tool to plugin_id/provider_name
  53. for tool in response.declaration.tools:
  54. tool.identity.provider = response.declaration.identity.name
  55. return response
  56. def invoke(
  57. self,
  58. tenant_id: str,
  59. user_id: str,
  60. tool_provider: str,
  61. tool_name: str,
  62. credentials: dict[str, Any],
  63. tool_parameters: dict[str, Any],
  64. conversation_id: Optional[str] = None,
  65. app_id: Optional[str] = None,
  66. message_id: Optional[str] = None,
  67. ) -> Generator[ToolInvokeMessage, None, None]:
  68. """
  69. Invoke the tool with the given tenant, user, plugin, provider, name, credentials and parameters.
  70. """
  71. tool_provider_id = GenericProviderID(tool_provider)
  72. response = self._request_with_plugin_daemon_response_stream(
  73. "POST",
  74. f"plugin/{tenant_id}/dispatch/tool/invoke",
  75. ToolInvokeMessage,
  76. data={
  77. "user_id": user_id,
  78. "conversation_id": conversation_id,
  79. "app_id": app_id,
  80. "message_id": message_id,
  81. "data": {
  82. "provider": tool_provider_id.provider_name,
  83. "tool": tool_name,
  84. "credentials": credentials,
  85. "tool_parameters": tool_parameters,
  86. },
  87. },
  88. headers={
  89. "X-Plugin-ID": tool_provider_id.plugin_id,
  90. "Content-Type": "application/json",
  91. },
  92. )
  93. return response
  94. def validate_provider_credentials(
  95. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  96. ) -> bool:
  97. """
  98. validate the credentials of the provider
  99. """
  100. tool_provider_id = GenericProviderID(provider)
  101. response = self._request_with_plugin_daemon_response_stream(
  102. "POST",
  103. f"plugin/{tenant_id}/dispatch/tool/validate_credentials",
  104. PluginBasicBooleanResponse,
  105. data={
  106. "user_id": user_id,
  107. "data": {
  108. "provider": tool_provider_id.provider_name,
  109. "credentials": credentials,
  110. },
  111. },
  112. headers={
  113. "X-Plugin-ID": tool_provider_id.plugin_id,
  114. "Content-Type": "application/json",
  115. },
  116. )
  117. for resp in response:
  118. return resp.result
  119. return False
  120. def get_runtime_parameters(
  121. self,
  122. tenant_id: str,
  123. user_id: str,
  124. provider: str,
  125. credentials: dict[str, Any],
  126. tool: str,
  127. conversation_id: Optional[str] = None,
  128. app_id: Optional[str] = None,
  129. message_id: Optional[str] = None,
  130. ) -> list[ToolParameter]:
  131. """
  132. get the runtime parameters of the tool
  133. """
  134. tool_provider_id = GenericProviderID(provider)
  135. class RuntimeParametersResponse(BaseModel):
  136. parameters: list[ToolParameter]
  137. response = self._request_with_plugin_daemon_response_stream(
  138. "POST",
  139. f"plugin/{tenant_id}/dispatch/tool/get_runtime_parameters",
  140. RuntimeParametersResponse,
  141. data={
  142. "user_id": user_id,
  143. "conversation_id": conversation_id,
  144. "app_id": app_id,
  145. "message_id": message_id,
  146. "data": {
  147. "provider": tool_provider_id.provider_name,
  148. "tool": tool,
  149. "credentials": credentials,
  150. },
  151. },
  152. headers={
  153. "X-Plugin-ID": tool_provider_id.plugin_id,
  154. "Content-Type": "application/json",
  155. },
  156. )
  157. for resp in response:
  158. return resp.parameters
  159. return []