You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tool.py 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 (
  6. PluginBasicBooleanResponse,
  7. PluginToolProviderEntity,
  8. )
  9. from core.plugin.impl.base import BasePluginClient
  10. from core.plugin.utils.chunk_merger import merge_blob_chunks
  11. from core.schemas.resolver import resolve_dify_schema_refs
  12. from core.tools.entities.tool_entities import CredentialType, ToolInvokeMessage, ToolParameter
  13. class PluginToolManager(BasePluginClient):
  14. def fetch_tool_providers(self, tenant_id: str) -> list[PluginToolProviderEntity]:
  15. """
  16. Fetch tool providers for the given tenant.
  17. """
  18. def transformer(json_response: dict[str, Any]) -> dict:
  19. for provider in json_response.get("data", []):
  20. declaration = provider.get("declaration", {}) or {}
  21. provider_name = declaration.get("identity", {}).get("name")
  22. for tool in declaration.get("tools", []):
  23. tool["identity"]["provider"] = provider_name
  24. # resolve refs
  25. if tool.get("output_schema"):
  26. tool["output_schema"] = resolve_dify_schema_refs(tool["output_schema"])
  27. return json_response
  28. response = self._request_with_plugin_daemon_response(
  29. "GET",
  30. f"plugin/{tenant_id}/management/tools",
  31. list[PluginToolProviderEntity],
  32. params={"page": 1, "page_size": 256},
  33. transformer=transformer,
  34. )
  35. for provider in response:
  36. provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  37. # override the provider name for each tool to plugin_id/provider_name
  38. for tool in provider.declaration.tools:
  39. tool.identity.provider = provider.declaration.identity.name
  40. return response
  41. def fetch_tool_provider(self, tenant_id: str, provider: str) -> PluginToolProviderEntity:
  42. """
  43. Fetch tool provider for the given tenant and plugin.
  44. """
  45. tool_provider_id = ToolProviderID(provider)
  46. def transformer(json_response: dict[str, Any]) -> dict:
  47. data = json_response.get("data")
  48. if data:
  49. for tool in data.get("declaration", {}).get("tools", []):
  50. tool["identity"]["provider"] = tool_provider_id.provider_name
  51. # resolve refs
  52. if tool.get("output_schema"):
  53. tool["output_schema"] = resolve_dify_schema_refs(tool["output_schema"])
  54. return json_response
  55. response = self._request_with_plugin_daemon_response(
  56. "GET",
  57. f"plugin/{tenant_id}/management/tool",
  58. PluginToolProviderEntity,
  59. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  60. transformer=transformer,
  61. )
  62. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  63. # override the provider name for each tool to plugin_id/provider_name
  64. for tool in response.declaration.tools:
  65. tool.identity.provider = response.declaration.identity.name
  66. return response
  67. def invoke(
  68. self,
  69. tenant_id: str,
  70. user_id: str,
  71. tool_provider: str,
  72. tool_name: str,
  73. credentials: dict[str, Any],
  74. credential_type: CredentialType,
  75. tool_parameters: dict[str, Any],
  76. conversation_id: Optional[str] = None,
  77. app_id: Optional[str] = None,
  78. message_id: Optional[str] = None,
  79. ) -> Generator[ToolInvokeMessage, None, None]:
  80. """
  81. Invoke the tool with the given tenant, user, plugin, provider, name, credentials and parameters.
  82. """
  83. tool_provider_id = GenericProviderID(tool_provider)
  84. response = self._request_with_plugin_daemon_response_stream(
  85. "POST",
  86. f"plugin/{tenant_id}/dispatch/tool/invoke",
  87. ToolInvokeMessage,
  88. data={
  89. "user_id": user_id,
  90. "conversation_id": conversation_id,
  91. "app_id": app_id,
  92. "message_id": message_id,
  93. "data": {
  94. "provider": tool_provider_id.provider_name,
  95. "tool": tool_name,
  96. "credentials": credentials,
  97. "credential_type": credential_type,
  98. "tool_parameters": tool_parameters,
  99. },
  100. },
  101. headers={
  102. "X-Plugin-ID": tool_provider_id.plugin_id,
  103. "Content-Type": "application/json",
  104. },
  105. )
  106. return merge_blob_chunks(response)
  107. def validate_provider_credentials(
  108. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  109. ) -> bool:
  110. """
  111. validate the credentials of the provider
  112. """
  113. tool_provider_id = GenericProviderID(provider)
  114. response = self._request_with_plugin_daemon_response_stream(
  115. "POST",
  116. f"plugin/{tenant_id}/dispatch/tool/validate_credentials",
  117. PluginBasicBooleanResponse,
  118. data={
  119. "user_id": user_id,
  120. "data": {
  121. "provider": tool_provider_id.provider_name,
  122. "credentials": credentials,
  123. },
  124. },
  125. headers={
  126. "X-Plugin-ID": tool_provider_id.plugin_id,
  127. "Content-Type": "application/json",
  128. },
  129. )
  130. for resp in response:
  131. return resp.result
  132. return False
  133. def validate_datasource_credentials(
  134. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  135. ) -> bool:
  136. """
  137. validate the credentials of the datasource
  138. """
  139. tool_provider_id = GenericProviderID(provider)
  140. response = self._request_with_plugin_daemon_response_stream(
  141. "POST",
  142. f"plugin/{tenant_id}/dispatch/datasource/validate_credentials",
  143. PluginBasicBooleanResponse,
  144. data={
  145. "user_id": user_id,
  146. "data": {
  147. "provider": tool_provider_id.provider_name,
  148. "credentials": credentials,
  149. },
  150. },
  151. headers={
  152. "X-Plugin-ID": tool_provider_id.plugin_id,
  153. "Content-Type": "application/json",
  154. },
  155. )
  156. for resp in response:
  157. return resp.result
  158. return False
  159. def get_runtime_parameters(
  160. self,
  161. tenant_id: str,
  162. user_id: str,
  163. provider: str,
  164. credentials: dict[str, Any],
  165. tool: str,
  166. conversation_id: Optional[str] = None,
  167. app_id: Optional[str] = None,
  168. message_id: Optional[str] = None,
  169. ) -> list[ToolParameter]:
  170. """
  171. get the runtime parameters of the tool
  172. """
  173. tool_provider_id = GenericProviderID(provider)
  174. class RuntimeParametersResponse(BaseModel):
  175. parameters: list[ToolParameter]
  176. response = self._request_with_plugin_daemon_response_stream(
  177. "POST",
  178. f"plugin/{tenant_id}/dispatch/tool/get_runtime_parameters",
  179. RuntimeParametersResponse,
  180. data={
  181. "user_id": user_id,
  182. "conversation_id": conversation_id,
  183. "app_id": app_id,
  184. "message_id": message_id,
  185. "data": {
  186. "provider": tool_provider_id.provider_name,
  187. "tool": tool,
  188. "credentials": credentials,
  189. },
  190. },
  191. headers={
  192. "X-Plugin-ID": tool_provider_id.plugin_id,
  193. "Content-Type": "application/json",
  194. },
  195. )
  196. for resp in response:
  197. return resp.parameters
  198. return []