Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

datasource.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. from collections.abc import Generator, Mapping
  2. from typing import Any
  3. from core.datasource.entities.datasource_entities import (
  4. DatasourceMessage,
  5. GetOnlineDocumentPageContentRequest,
  6. OnlineDocumentPagesMessage,
  7. OnlineDriveBrowseFilesRequest,
  8. OnlineDriveBrowseFilesResponse,
  9. OnlineDriveDownloadFileRequest,
  10. WebsiteCrawlMessage,
  11. )
  12. from core.plugin.entities.plugin import DatasourceProviderID, GenericProviderID
  13. from core.plugin.entities.plugin_daemon import (
  14. PluginBasicBooleanResponse,
  15. PluginDatasourceProviderEntity,
  16. )
  17. from core.plugin.impl.base import BasePluginClient
  18. from services.tools.tools_transform_service import ToolTransformService
  19. class PluginDatasourceManager(BasePluginClient):
  20. def fetch_datasource_providers(self, tenant_id: str) -> list[PluginDatasourceProviderEntity]:
  21. """
  22. Fetch datasource providers for the given tenant.
  23. """
  24. def transformer(json_response: dict[str, Any]) -> dict:
  25. if json_response.get("data"):
  26. for provider in json_response.get("data", []):
  27. declaration = provider.get("declaration", {}) or {}
  28. provider_name = declaration.get("identity", {}).get("name")
  29. for datasource in declaration.get("datasources", []):
  30. datasource["identity"]["provider"] = provider_name
  31. return json_response
  32. response = self._request_with_plugin_daemon_response(
  33. "GET",
  34. f"plugin/{tenant_id}/management/datasources",
  35. list[PluginDatasourceProviderEntity],
  36. params={"page": 1, "page_size": 256},
  37. transformer=transformer,
  38. )
  39. local_file_datasource_provider = PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())
  40. for provider in response:
  41. ToolTransformService.repack_provider(tenant_id=tenant_id, provider=provider)
  42. all_response = [local_file_datasource_provider] + response
  43. for provider in all_response:
  44. provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  45. # override the provider name for each tool to plugin_id/provider_name
  46. for tool in provider.declaration.datasources:
  47. tool.identity.provider = provider.declaration.identity.name
  48. return all_response
  49. def fetch_installed_datasource_providers(self, tenant_id: str) -> list[PluginDatasourceProviderEntity]:
  50. """
  51. Fetch datasource providers for the given tenant.
  52. """
  53. def transformer(json_response: dict[str, Any]) -> dict:
  54. if json_response.get("data"):
  55. for provider in json_response.get("data", []):
  56. declaration = provider.get("declaration", {}) or {}
  57. provider_name = declaration.get("identity", {}).get("name")
  58. for datasource in declaration.get("datasources", []):
  59. datasource["identity"]["provider"] = provider_name
  60. return json_response
  61. response = self._request_with_plugin_daemon_response(
  62. "GET",
  63. f"plugin/{tenant_id}/management/datasources",
  64. list[PluginDatasourceProviderEntity],
  65. params={"page": 1, "page_size": 256},
  66. transformer=transformer,
  67. )
  68. for provider in response:
  69. ToolTransformService.repack_provider(tenant_id=tenant_id, provider=provider)
  70. for provider in response:
  71. provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  72. # override the provider name for each tool to plugin_id/provider_name
  73. for tool in provider.declaration.datasources:
  74. tool.identity.provider = provider.declaration.identity.name
  75. return response
  76. def fetch_datasource_provider(self, tenant_id: str, provider_id: str) -> PluginDatasourceProviderEntity:
  77. """
  78. Fetch datasource provider for the given tenant and plugin.
  79. """
  80. if provider_id == "langgenius/file/file":
  81. return PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())
  82. tool_provider_id = DatasourceProviderID(provider_id)
  83. def transformer(json_response: dict[str, Any]) -> dict:
  84. data = json_response.get("data")
  85. if data:
  86. for datasource in data.get("declaration", {}).get("datasources", []):
  87. datasource["identity"]["provider"] = tool_provider_id.provider_name
  88. return json_response
  89. response = self._request_with_plugin_daemon_response(
  90. "GET",
  91. f"plugin/{tenant_id}/management/datasource",
  92. PluginDatasourceProviderEntity,
  93. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  94. transformer=transformer,
  95. )
  96. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  97. # override the provider name for each tool to plugin_id/provider_name
  98. for datasource in response.declaration.datasources:
  99. datasource.identity.provider = response.declaration.identity.name
  100. return response
  101. def get_website_crawl(
  102. self,
  103. tenant_id: str,
  104. user_id: str,
  105. datasource_provider: str,
  106. datasource_name: str,
  107. credentials: dict[str, Any],
  108. datasource_parameters: Mapping[str, Any],
  109. provider_type: str,
  110. ) -> Generator[WebsiteCrawlMessage, None, None]:
  111. """
  112. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  113. """
  114. datasource_provider_id = GenericProviderID(datasource_provider)
  115. return self._request_with_plugin_daemon_response_stream(
  116. "POST",
  117. f"plugin/{tenant_id}/dispatch/datasource/get_website_crawl",
  118. WebsiteCrawlMessage,
  119. data={
  120. "user_id": user_id,
  121. "data": {
  122. "provider": datasource_provider_id.provider_name,
  123. "datasource": datasource_name,
  124. "credentials": credentials,
  125. "datasource_parameters": datasource_parameters,
  126. },
  127. },
  128. headers={
  129. "X-Plugin-ID": datasource_provider_id.plugin_id,
  130. "Content-Type": "application/json",
  131. },
  132. )
  133. def get_online_document_pages(
  134. self,
  135. tenant_id: str,
  136. user_id: str,
  137. datasource_provider: str,
  138. datasource_name: str,
  139. credentials: dict[str, Any],
  140. datasource_parameters: Mapping[str, Any],
  141. provider_type: str,
  142. ) -> Generator[OnlineDocumentPagesMessage, None, None]:
  143. """
  144. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  145. """
  146. datasource_provider_id = GenericProviderID(datasource_provider)
  147. return self._request_with_plugin_daemon_response_stream(
  148. "POST",
  149. f"plugin/{tenant_id}/dispatch/datasource/get_online_document_pages",
  150. OnlineDocumentPagesMessage,
  151. data={
  152. "user_id": user_id,
  153. "data": {
  154. "provider": datasource_provider_id.provider_name,
  155. "datasource": datasource_name,
  156. "credentials": credentials,
  157. "datasource_parameters": datasource_parameters,
  158. },
  159. },
  160. headers={
  161. "X-Plugin-ID": datasource_provider_id.plugin_id,
  162. "Content-Type": "application/json",
  163. },
  164. )
  165. def get_online_document_page_content(
  166. self,
  167. tenant_id: str,
  168. user_id: str,
  169. datasource_provider: str,
  170. datasource_name: str,
  171. credentials: dict[str, Any],
  172. datasource_parameters: GetOnlineDocumentPageContentRequest,
  173. provider_type: str,
  174. ) -> Generator[DatasourceMessage, None, None]:
  175. """
  176. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  177. """
  178. datasource_provider_id = GenericProviderID(datasource_provider)
  179. return self._request_with_plugin_daemon_response_stream(
  180. "POST",
  181. f"plugin/{tenant_id}/dispatch/datasource/get_online_document_page_content",
  182. DatasourceMessage,
  183. data={
  184. "user_id": user_id,
  185. "data": {
  186. "provider": datasource_provider_id.provider_name,
  187. "datasource": datasource_name,
  188. "credentials": credentials,
  189. "page": datasource_parameters.model_dump(),
  190. },
  191. },
  192. headers={
  193. "X-Plugin-ID": datasource_provider_id.plugin_id,
  194. "Content-Type": "application/json",
  195. },
  196. )
  197. def online_drive_browse_files(
  198. self,
  199. tenant_id: str,
  200. user_id: str,
  201. datasource_provider: str,
  202. datasource_name: str,
  203. credentials: dict[str, Any],
  204. request: OnlineDriveBrowseFilesRequest,
  205. provider_type: str,
  206. ) -> Generator[OnlineDriveBrowseFilesResponse, None, None]:
  207. """
  208. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  209. """
  210. datasource_provider_id = GenericProviderID(datasource_provider)
  211. response = self._request_with_plugin_daemon_response_stream(
  212. "POST",
  213. f"plugin/{tenant_id}/dispatch/datasource/online_drive_browse_files",
  214. OnlineDriveBrowseFilesResponse,
  215. data={
  216. "user_id": user_id,
  217. "data": {
  218. "provider": datasource_provider_id.provider_name,
  219. "datasource": datasource_name,
  220. "credentials": credentials,
  221. "request": request.model_dump(),
  222. },
  223. },
  224. headers={
  225. "X-Plugin-ID": datasource_provider_id.plugin_id,
  226. "Content-Type": "application/json",
  227. },
  228. )
  229. yield from response
  230. def online_drive_download_file(
  231. self,
  232. tenant_id: str,
  233. user_id: str,
  234. datasource_provider: str,
  235. datasource_name: str,
  236. credentials: dict[str, Any],
  237. request: OnlineDriveDownloadFileRequest,
  238. provider_type: str,
  239. ) -> Generator[DatasourceMessage, None, None]:
  240. """
  241. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  242. """
  243. datasource_provider_id = GenericProviderID(datasource_provider)
  244. response = self._request_with_plugin_daemon_response_stream(
  245. "POST",
  246. f"plugin/{tenant_id}/dispatch/datasource/online_drive_download_file",
  247. DatasourceMessage,
  248. data={
  249. "user_id": user_id,
  250. "data": {
  251. "provider": datasource_provider_id.provider_name,
  252. "datasource": datasource_name,
  253. "credentials": credentials,
  254. "request": request.model_dump(),
  255. },
  256. },
  257. headers={
  258. "X-Plugin-ID": datasource_provider_id.plugin_id,
  259. "Content-Type": "application/json",
  260. },
  261. )
  262. yield from response
  263. def validate_provider_credentials(
  264. self, tenant_id: str, user_id: str, provider: str, plugin_id: str, credentials: dict[str, Any]
  265. ) -> bool:
  266. """
  267. validate the credentials of the provider
  268. """
  269. # datasource_provider_id = GenericProviderID(provider_id)
  270. response = self._request_with_plugin_daemon_response_stream(
  271. "POST",
  272. f"plugin/{tenant_id}/dispatch/datasource/validate_credentials",
  273. PluginBasicBooleanResponse,
  274. data={
  275. "user_id": user_id,
  276. "data": {
  277. "provider": provider,
  278. "credentials": credentials,
  279. },
  280. },
  281. headers={
  282. "X-Plugin-ID": plugin_id,
  283. "Content-Type": "application/json",
  284. },
  285. )
  286. for resp in response:
  287. return resp.result
  288. return False
  289. def _get_local_file_datasource_provider(self) -> dict[str, Any]:
  290. return {
  291. "id": "langgenius/file/file",
  292. "plugin_id": "langgenius/file",
  293. "provider": "file",
  294. "plugin_unique_identifier": "langgenius/file:0.0.1@dify",
  295. "declaration": {
  296. "identity": {
  297. "author": "langgenius",
  298. "name": "file",
  299. "label": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  300. "icon": "https://assets.dify.ai/images/File%20Upload.svg",
  301. "description": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  302. },
  303. "credentials_schema": [],
  304. "provider_type": "local_file",
  305. "datasources": [
  306. {
  307. "identity": {
  308. "author": "langgenius",
  309. "name": "upload-file",
  310. "provider": "file",
  311. "label": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  312. },
  313. "parameters": [],
  314. "description": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  315. }
  316. ],
  317. },
  318. }