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

datasource.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. from typing import Any
  2. from core.datasource.entities.api_entities import DatasourceProviderApiEntity
  3. from core.datasource.entities.datasource_entities import (
  4. GetOnlineDocumentPageContentRequest,
  5. GetOnlineDocumentPageContentResponse,
  6. GetOnlineDocumentPagesRequest,
  7. GetOnlineDocumentPagesResponse,
  8. GetWebsiteCrawlRequest,
  9. GetWebsiteCrawlResponse,
  10. )
  11. from core.plugin.entities.plugin import GenericProviderID, ToolProviderID
  12. from core.plugin.entities.plugin_daemon import (
  13. PluginBasicBooleanResponse,
  14. PluginDatasourceProviderEntity,
  15. )
  16. from core.plugin.impl.base import BasePluginClient
  17. class PluginDatasourceManager(BasePluginClient):
  18. def fetch_datasource_providers(self, tenant_id: str) -> list[DatasourceProviderApiEntity]:
  19. """
  20. Fetch datasource providers for the given tenant.
  21. """
  22. def transformer(json_response: dict[str, Any]) -> dict:
  23. for provider in json_response.get("data", []):
  24. declaration = provider.get("declaration", {}) or {}
  25. provider_name = declaration.get("identity", {}).get("name")
  26. for datasource in declaration.get("datasources", []):
  27. datasource["identity"]["provider"] = provider_name
  28. return json_response
  29. # response = self._request_with_plugin_daemon_response(
  30. # "GET",
  31. # f"plugin/{tenant_id}/management/datasources",
  32. # list[PluginDatasourceProviderEntity],
  33. # params={"page": 1, "page_size": 256},
  34. # transformer=transformer,
  35. # )
  36. # for provider in response:
  37. # provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  38. # # override the provider name for each tool to plugin_id/provider_name
  39. # for datasource in provider.declaration.datasources:
  40. # datasource.identity.provider = provider.declaration.identity.name
  41. return [DatasourceProviderApiEntity(**self._get_local_file_datasource_provider())]
  42. def fetch_datasource_provider(self, tenant_id: str, provider: str) -> PluginDatasourceProviderEntity:
  43. """
  44. Fetch datasource provider for the given tenant and plugin.
  45. """
  46. tool_provider_id = ToolProviderID(provider)
  47. def transformer(json_response: dict[str, Any]) -> dict:
  48. data = json_response.get("data")
  49. if data:
  50. for datasource in data.get("declaration", {}).get("datasources", []):
  51. datasource["identity"]["provider"] = tool_provider_id.provider_name
  52. return json_response
  53. response = self._request_with_plugin_daemon_response(
  54. "GET",
  55. f"plugin/{tenant_id}/management/datasources",
  56. PluginDatasourceProviderEntity,
  57. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  58. transformer=transformer,
  59. )
  60. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  61. # override the provider name for each tool to plugin_id/provider_name
  62. for datasource in response.declaration.datasources:
  63. datasource.identity.provider = response.declaration.identity.name
  64. return response
  65. def get_website_crawl(
  66. self,
  67. tenant_id: str,
  68. user_id: str,
  69. datasource_provider: str,
  70. datasource_name: str,
  71. credentials: dict[str, Any],
  72. datasource_parameters: GetWebsiteCrawlRequest,
  73. provider_type: str,
  74. ) -> GetWebsiteCrawlResponse:
  75. """
  76. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  77. """
  78. datasource_provider_id = GenericProviderID(datasource_provider)
  79. response = self._request_with_plugin_daemon_response_stream(
  80. "POST",
  81. f"plugin/{tenant_id}/dispatch/datasource/{provider_type}/get_website_crawl",
  82. GetWebsiteCrawlResponse,
  83. data={
  84. "user_id": user_id,
  85. "data": {
  86. "provider": datasource_provider_id.provider_name,
  87. "datasource": datasource_name,
  88. "credentials": credentials,
  89. "datasource_parameters": datasource_parameters,
  90. },
  91. },
  92. headers={
  93. "X-Plugin-ID": datasource_provider_id.plugin_id,
  94. "Content-Type": "application/json",
  95. },
  96. )
  97. for resp in response:
  98. return resp
  99. raise Exception("No response from plugin daemon")
  100. def get_online_document_pages(
  101. self,
  102. tenant_id: str,
  103. user_id: str,
  104. datasource_provider: str,
  105. datasource_name: str,
  106. credentials: dict[str, Any],
  107. datasource_parameters: GetOnlineDocumentPagesRequest,
  108. provider_type: str,
  109. ) -> GetOnlineDocumentPagesResponse:
  110. """
  111. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  112. """
  113. datasource_provider_id = GenericProviderID(datasource_provider)
  114. response = self._request_with_plugin_daemon_response_stream(
  115. "POST",
  116. f"plugin/{tenant_id}/dispatch/datasource/{provider_type}/get_online_document_pages",
  117. GetOnlineDocumentPagesResponse,
  118. data={
  119. "user_id": user_id,
  120. "data": {
  121. "provider": datasource_provider_id.provider_name,
  122. "datasource": datasource_name,
  123. "credentials": credentials,
  124. "datasource_parameters": datasource_parameters,
  125. },
  126. },
  127. headers={
  128. "X-Plugin-ID": datasource_provider_id.plugin_id,
  129. "Content-Type": "application/json",
  130. },
  131. )
  132. for resp in response:
  133. return resp
  134. raise Exception("No response from plugin daemon")
  135. def get_online_document_page_content(
  136. self,
  137. tenant_id: str,
  138. user_id: str,
  139. datasource_provider: str,
  140. datasource_name: str,
  141. credentials: dict[str, Any],
  142. datasource_parameters: GetOnlineDocumentPageContentRequest,
  143. provider_type: str,
  144. ) -> GetOnlineDocumentPageContentResponse:
  145. """
  146. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  147. """
  148. datasource_provider_id = GenericProviderID(datasource_provider)
  149. response = self._request_with_plugin_daemon_response_stream(
  150. "POST",
  151. f"plugin/{tenant_id}/dispatch/datasource/{provider_type}/get_online_document_page_content",
  152. GetOnlineDocumentPageContentResponse,
  153. data={
  154. "user_id": user_id,
  155. "data": {
  156. "provider": datasource_provider_id.provider_name,
  157. "datasource": datasource_name,
  158. "credentials": credentials,
  159. "datasource_parameters": datasource_parameters,
  160. },
  161. },
  162. headers={
  163. "X-Plugin-ID": datasource_provider_id.plugin_id,
  164. "Content-Type": "application/json",
  165. },
  166. )
  167. for resp in response:
  168. return resp
  169. raise Exception("No response from plugin daemon")
  170. def validate_provider_credentials(
  171. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  172. ) -> bool:
  173. """
  174. validate the credentials of the provider
  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_local_file_datasource_provider(self) -> dict[str, Any]:
  197. return {
  198. "id": "langgenius/file/file",
  199. "author": "langgenius",
  200. "name": "langgenius/file/file",
  201. "plugin_id": "langgenius/file",
  202. "plugin_unique_identifier": "langgenius/file:0.0.1@dify",
  203. "description": {
  204. "zh_Hans": "File",
  205. "en_US": "File",
  206. "pt_BR": "File",
  207. "ja_JP": "File"
  208. },
  209. "icon": "https://cloud.dify.ai/console/api/workspaces/current/plugin/icon?tenant_id=945b4365-9d99-48c1-8c47-90593fe8b9c9&filename=13d9312f6b1352d3939b90a5257de58ff3cd619d5be4f5b266ff0298935ac328.svg",
  210. "label": {
  211. "zh_Hans": "File",
  212. "en_US": "File",
  213. "pt_BR": "File",
  214. "ja_JP": "File"
  215. },
  216. "type": "datasource",
  217. "team_credentials": {},
  218. "is_team_authorization": False,
  219. "allow_delete": True,
  220. "datasources": [{
  221. "author": "langgenius",
  222. "name": "upload_file",
  223. "label": {
  224. "en_US": "File",
  225. "zh_Hans": "File",
  226. "pt_BR": "File",
  227. "ja_JP": "File"
  228. },
  229. "description": {
  230. "en_US": "File",
  231. "zh_Hans": "File",
  232. "pt_BR": "File",
  233. "ja_JP": "File."
  234. },
  235. "parameters": [],
  236. "labels": [
  237. "search"
  238. ],
  239. "output_schema": None
  240. }],
  241. "labels": [
  242. "search"
  243. ]
  244. }