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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.schemas.resolver import resolve_dify_schema_refs
  11. from core.tools.entities.tool_entities import CredentialType, ToolInvokeMessage, ToolParameter
  12. class PluginToolManager(BasePluginClient):
  13. def fetch_tool_providers(self, tenant_id: str) -> list[PluginToolProviderEntity]:
  14. """
  15. Fetch tool providers for the given tenant.
  16. """
  17. def transformer(json_response: dict[str, Any]) -> dict:
  18. for provider in json_response.get("data", []):
  19. declaration = provider.get("declaration", {}) or {}
  20. provider_name = declaration.get("identity", {}).get("name")
  21. for tool in declaration.get("tools", []):
  22. tool["identity"]["provider"] = provider_name
  23. # resolve refs
  24. if tool.get("output_schema"):
  25. tool["output_schema"] = resolve_dify_schema_refs(tool["output_schema"])
  26. return json_response
  27. response = self._request_with_plugin_daemon_response(
  28. "GET",
  29. f"plugin/{tenant_id}/management/tools",
  30. list[PluginToolProviderEntity],
  31. params={"page": 1, "page_size": 256},
  32. transformer=transformer,
  33. )
  34. for provider in response:
  35. provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  36. # override the provider name for each tool to plugin_id/provider_name
  37. for tool in provider.declaration.tools:
  38. tool.identity.provider = provider.declaration.identity.name
  39. return response
  40. def fetch_tool_provider(self, tenant_id: str, provider: str) -> PluginToolProviderEntity:
  41. """
  42. Fetch tool provider for the given tenant and plugin.
  43. """
  44. tool_provider_id = ToolProviderID(provider)
  45. def transformer(json_response: dict[str, Any]) -> dict:
  46. data = json_response.get("data")
  47. if data:
  48. for tool in data.get("declaration", {}).get("tools", []):
  49. tool["identity"]["provider"] = tool_provider_id.provider_name
  50. # resolve refs
  51. if tool.get("output_schema"):
  52. tool["output_schema"] = resolve_dify_schema_refs(tool["output_schema"])
  53. return json_response
  54. response = self._request_with_plugin_daemon_response(
  55. "GET",
  56. f"plugin/{tenant_id}/management/tool",
  57. PluginToolProviderEntity,
  58. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  59. transformer=transformer,
  60. )
  61. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  62. # override the provider name for each tool to plugin_id/provider_name
  63. for tool in response.declaration.tools:
  64. tool.identity.provider = response.declaration.identity.name
  65. return response
  66. def invoke(
  67. self,
  68. tenant_id: str,
  69. user_id: str,
  70. tool_provider: str,
  71. tool_name: str,
  72. credentials: dict[str, Any],
  73. credential_type: CredentialType,
  74. tool_parameters: dict[str, Any],
  75. conversation_id: Optional[str] = None,
  76. app_id: Optional[str] = None,
  77. message_id: Optional[str] = None,
  78. ) -> Generator[ToolInvokeMessage, None, None]:
  79. """
  80. Invoke the tool with the given tenant, user, plugin, provider, name, credentials and parameters.
  81. """
  82. tool_provider_id = GenericProviderID(tool_provider)
  83. response = self._request_with_plugin_daemon_response_stream(
  84. "POST",
  85. f"plugin/{tenant_id}/dispatch/tool/invoke",
  86. ToolInvokeMessage,
  87. data={
  88. "user_id": user_id,
  89. "conversation_id": conversation_id,
  90. "app_id": app_id,
  91. "message_id": message_id,
  92. "data": {
  93. "provider": tool_provider_id.provider_name,
  94. "tool": tool_name,
  95. "credentials": credentials,
  96. "credential_type": credential_type,
  97. "tool_parameters": tool_parameters,
  98. },
  99. },
  100. headers={
  101. "X-Plugin-ID": tool_provider_id.plugin_id,
  102. "Content-Type": "application/json",
  103. },
  104. )
  105. class FileChunk:
  106. """
  107. Only used for internal processing.
  108. """
  109. bytes_written: int
  110. total_length: int
  111. data: bytearray
  112. def __init__(self, total_length: int):
  113. self.bytes_written = 0
  114. self.total_length = total_length
  115. self.data = bytearray(total_length)
  116. files: dict[str, FileChunk] = {}
  117. for resp in response:
  118. if resp.type == ToolInvokeMessage.MessageType.BLOB_CHUNK:
  119. assert isinstance(resp.message, ToolInvokeMessage.BlobChunkMessage)
  120. # Get blob chunk information
  121. chunk_id = resp.message.id
  122. total_length = resp.message.total_length
  123. blob_data = resp.message.blob
  124. is_end = resp.message.end
  125. # Initialize buffer for this file if it doesn't exist
  126. if chunk_id not in files:
  127. files[chunk_id] = FileChunk(total_length)
  128. # If this is the final chunk, yield a complete blob message
  129. if is_end:
  130. yield ToolInvokeMessage(
  131. type=ToolInvokeMessage.MessageType.BLOB,
  132. message=ToolInvokeMessage.BlobMessage(blob=files[chunk_id].data),
  133. meta=resp.meta,
  134. )
  135. else:
  136. # Check if file is too large (30MB limit)
  137. if files[chunk_id].bytes_written + len(blob_data) > 30 * 1024 * 1024:
  138. # Delete the file if it's too large
  139. del files[chunk_id]
  140. # Skip yielding this message
  141. raise ValueError("File is too large which reached the limit of 30MB")
  142. # Check if single chunk is too large (8KB limit)
  143. if len(blob_data) > 8192:
  144. # Skip yielding this message
  145. raise ValueError("File chunk is too large which reached the limit of 8KB")
  146. # Append the blob data to the buffer
  147. files[chunk_id].data[
  148. files[chunk_id].bytes_written : files[chunk_id].bytes_written + len(blob_data)
  149. ] = blob_data
  150. files[chunk_id].bytes_written += len(blob_data)
  151. else:
  152. yield resp
  153. def validate_provider_credentials(
  154. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  155. ) -> bool:
  156. """
  157. validate the credentials of the provider
  158. """
  159. tool_provider_id = GenericProviderID(provider)
  160. response = self._request_with_plugin_daemon_response_stream(
  161. "POST",
  162. f"plugin/{tenant_id}/dispatch/tool/validate_credentials",
  163. PluginBasicBooleanResponse,
  164. data={
  165. "user_id": user_id,
  166. "data": {
  167. "provider": tool_provider_id.provider_name,
  168. "credentials": credentials,
  169. },
  170. },
  171. headers={
  172. "X-Plugin-ID": tool_provider_id.plugin_id,
  173. "Content-Type": "application/json",
  174. },
  175. )
  176. for resp in response:
  177. return resp.result
  178. return False
  179. def validate_datasource_credentials(
  180. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  181. ) -> bool:
  182. """
  183. validate the credentials of the datasource
  184. """
  185. tool_provider_id = GenericProviderID(provider)
  186. response = self._request_with_plugin_daemon_response_stream(
  187. "POST",
  188. f"plugin/{tenant_id}/dispatch/datasource/validate_credentials",
  189. PluginBasicBooleanResponse,
  190. data={
  191. "user_id": user_id,
  192. "data": {
  193. "provider": tool_provider_id.provider_name,
  194. "credentials": credentials,
  195. },
  196. },
  197. headers={
  198. "X-Plugin-ID": tool_provider_id.plugin_id,
  199. "Content-Type": "application/json",
  200. },
  201. )
  202. for resp in response:
  203. return resp.result
  204. return False
  205. def get_runtime_parameters(
  206. self,
  207. tenant_id: str,
  208. user_id: str,
  209. provider: str,
  210. credentials: dict[str, Any],
  211. tool: str,
  212. conversation_id: Optional[str] = None,
  213. app_id: Optional[str] = None,
  214. message_id: Optional[str] = None,
  215. ) -> list[ToolParameter]:
  216. """
  217. get the runtime parameters of the tool
  218. """
  219. tool_provider_id = GenericProviderID(provider)
  220. class RuntimeParametersResponse(BaseModel):
  221. parameters: list[ToolParameter]
  222. response = self._request_with_plugin_daemon_response_stream(
  223. "POST",
  224. f"plugin/{tenant_id}/dispatch/tool/get_runtime_parameters",
  225. RuntimeParametersResponse,
  226. data={
  227. "user_id": user_id,
  228. "conversation_id": conversation_id,
  229. "app_id": app_id,
  230. "message_id": message_id,
  231. "data": {
  232. "provider": tool_provider_id.provider_name,
  233. "tool": tool,
  234. "credentials": credentials,
  235. },
  236. },
  237. headers={
  238. "X-Plugin-ID": tool_provider_id.plugin_id,
  239. "Content-Type": "application/json",
  240. },
  241. )
  242. for resp in response:
  243. return resp.parameters
  244. return []