tool.py 6.7 KB

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