您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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