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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.impl.base import BasePluginClient
  7. from core.tools.entities.tool_entities import CredentialType, ToolInvokeMessage, ToolParameter
  8. class PluginToolManager(BasePluginClient):
  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. credential_type: CredentialType,
  64. tool_parameters: dict[str, Any],
  65. conversation_id: Optional[str] = None,
  66. app_id: Optional[str] = None,
  67. message_id: Optional[str] = None,
  68. ) -> Generator[ToolInvokeMessage, None, None]:
  69. """
  70. Invoke the tool with the given tenant, user, plugin, provider, name, credentials and parameters.
  71. """
  72. tool_provider_id = GenericProviderID(tool_provider)
  73. response = self._request_with_plugin_daemon_response_stream(
  74. "POST",
  75. f"plugin/{tenant_id}/dispatch/tool/invoke",
  76. ToolInvokeMessage,
  77. data={
  78. "user_id": user_id,
  79. "conversation_id": conversation_id,
  80. "app_id": app_id,
  81. "message_id": message_id,
  82. "data": {
  83. "provider": tool_provider_id.provider_name,
  84. "tool": tool_name,
  85. "credentials": credentials,
  86. "credential_type": credential_type,
  87. "tool_parameters": tool_parameters,
  88. },
  89. },
  90. headers={
  91. "X-Plugin-ID": tool_provider_id.plugin_id,
  92. "Content-Type": "application/json",
  93. },
  94. )
  95. class FileChunk:
  96. """
  97. Only used for internal processing.
  98. """
  99. bytes_written: int
  100. total_length: int
  101. data: bytearray
  102. def __init__(self, total_length: int):
  103. self.bytes_written = 0
  104. self.total_length = total_length
  105. self.data = bytearray(total_length)
  106. files: dict[str, FileChunk] = {}
  107. for resp in response:
  108. if resp.type == ToolInvokeMessage.MessageType.BLOB_CHUNK:
  109. assert isinstance(resp.message, ToolInvokeMessage.BlobChunkMessage)
  110. # Get blob chunk information
  111. chunk_id = resp.message.id
  112. total_length = resp.message.total_length
  113. blob_data = resp.message.blob
  114. is_end = resp.message.end
  115. # Initialize buffer for this file if it doesn't exist
  116. if chunk_id not in files:
  117. files[chunk_id] = FileChunk(total_length)
  118. # If this is the final chunk, yield a complete blob message
  119. if is_end:
  120. yield ToolInvokeMessage(
  121. type=ToolInvokeMessage.MessageType.BLOB,
  122. message=ToolInvokeMessage.BlobMessage(blob=files[chunk_id].data),
  123. meta=resp.meta,
  124. )
  125. else:
  126. # Check if file is too large (30MB limit)
  127. if files[chunk_id].bytes_written + len(blob_data) > 30 * 1024 * 1024:
  128. # Delete the file if it's too large
  129. del files[chunk_id]
  130. # Skip yielding this message
  131. raise ValueError("File is too large which reached the limit of 30MB")
  132. # Check if single chunk is too large (8KB limit)
  133. if len(blob_data) > 8192:
  134. # Skip yielding this message
  135. raise ValueError("File chunk is too large which reached the limit of 8KB")
  136. # Append the blob data to the buffer
  137. files[chunk_id].data[
  138. files[chunk_id].bytes_written : files[chunk_id].bytes_written + len(blob_data)
  139. ] = blob_data
  140. files[chunk_id].bytes_written += len(blob_data)
  141. else:
  142. yield resp
  143. def validate_provider_credentials(
  144. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  145. ) -> bool:
  146. """
  147. validate the credentials of the provider
  148. """
  149. tool_provider_id = GenericProviderID(provider)
  150. response = self._request_with_plugin_daemon_response_stream(
  151. "POST",
  152. f"plugin/{tenant_id}/dispatch/tool/validate_credentials",
  153. PluginBasicBooleanResponse,
  154. data={
  155. "user_id": user_id,
  156. "data": {
  157. "provider": tool_provider_id.provider_name,
  158. "credentials": credentials,
  159. },
  160. },
  161. headers={
  162. "X-Plugin-ID": tool_provider_id.plugin_id,
  163. "Content-Type": "application/json",
  164. },
  165. )
  166. for resp in response:
  167. return resp.result
  168. return False
  169. def get_runtime_parameters(
  170. self,
  171. tenant_id: str,
  172. user_id: str,
  173. provider: str,
  174. credentials: dict[str, Any],
  175. tool: str,
  176. conversation_id: Optional[str] = None,
  177. app_id: Optional[str] = None,
  178. message_id: Optional[str] = None,
  179. ) -> list[ToolParameter]:
  180. """
  181. get the runtime parameters of the tool
  182. """
  183. tool_provider_id = GenericProviderID(provider)
  184. class RuntimeParametersResponse(BaseModel):
  185. parameters: list[ToolParameter]
  186. response = self._request_with_plugin_daemon_response_stream(
  187. "POST",
  188. f"plugin/{tenant_id}/dispatch/tool/get_runtime_parameters",
  189. RuntimeParametersResponse,
  190. data={
  191. "user_id": user_id,
  192. "conversation_id": conversation_id,
  193. "app_id": app_id,
  194. "message_id": message_id,
  195. "data": {
  196. "provider": tool_provider_id.provider_name,
  197. "tool": tool,
  198. "credentials": credentials,
  199. },
  200. },
  201. headers={
  202. "X-Plugin-ID": tool_provider_id.plugin_id,
  203. "Content-Type": "application/json",
  204. },
  205. )
  206. for resp in response:
  207. return resp.parameters
  208. return []