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.

datasource.py 9.8KB

6 months ago
5 months ago
5 months ago
5 months ago
6 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
5 months ago
5 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
5 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
5 months ago
6 months ago
5 months ago
5 months ago
6 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. from collections.abc import Mapping
  2. from typing import Any
  3. from core.datasource.entities.datasource_entities import (
  4. GetOnlineDocumentPageContentRequest,
  5. GetOnlineDocumentPageContentResponse,
  6. GetOnlineDocumentPagesResponse,
  7. GetWebsiteCrawlResponse,
  8. )
  9. from core.plugin.entities.plugin import GenericProviderID, ToolProviderID
  10. from core.plugin.entities.plugin_daemon import (
  11. PluginBasicBooleanResponse,
  12. PluginDatasourceProviderEntity,
  13. )
  14. from core.plugin.impl.base import BasePluginClient
  15. class PluginDatasourceManager(BasePluginClient):
  16. def fetch_datasource_providers(self, tenant_id: str) -> list[PluginDatasourceProviderEntity]:
  17. """
  18. Fetch datasource providers for the given tenant.
  19. """
  20. def transformer(json_response: dict[str, Any]) -> dict:
  21. if json_response.get("data"):
  22. for provider in json_response.get("data", []):
  23. declaration = provider.get("declaration", {}) or {}
  24. provider_name = declaration.get("identity", {}).get("name")
  25. for datasource in declaration.get("datasources", []):
  26. datasource["identity"]["provider"] = provider_name
  27. return json_response
  28. response = self._request_with_plugin_daemon_response(
  29. "GET",
  30. f"plugin/{tenant_id}/management/datasources",
  31. list[PluginDatasourceProviderEntity],
  32. params={"page": 1, "page_size": 256},
  33. transformer=transformer,
  34. )
  35. local_file_datasource_provider = PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())
  36. all_response = [local_file_datasource_provider] + response
  37. for provider in all_response:
  38. provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  39. # override the provider name for each tool to plugin_id/provider_name
  40. for tool in provider.declaration.datasources:
  41. tool.identity.provider = provider.declaration.identity.name
  42. return all_response
  43. def fetch_datasource_provider(self, tenant_id: str, provider_id: str) -> PluginDatasourceProviderEntity:
  44. """
  45. Fetch datasource provider for the given tenant and plugin.
  46. """
  47. if provider_id == "langgenius/file/file":
  48. return PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())
  49. tool_provider_id = ToolProviderID(provider_id)
  50. def transformer(json_response: dict[str, Any]) -> dict:
  51. data = json_response.get("data")
  52. if data:
  53. for datasource in data.get("declaration", {}).get("datasources", []):
  54. datasource["identity"]["provider"] = tool_provider_id.provider_name
  55. return json_response
  56. response = self._request_with_plugin_daemon_response(
  57. "GET",
  58. f"plugin/{tenant_id}/management/datasource",
  59. PluginDatasourceProviderEntity,
  60. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  61. transformer=transformer,
  62. )
  63. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  64. # override the provider name for each tool to plugin_id/provider_name
  65. for datasource in response.declaration.datasources:
  66. datasource.identity.provider = response.declaration.identity.name
  67. return response
  68. def get_website_crawl(
  69. self,
  70. tenant_id: str,
  71. user_id: str,
  72. datasource_provider: str,
  73. datasource_name: str,
  74. credentials: dict[str, Any],
  75. datasource_parameters: Mapping[str, Any],
  76. provider_type: str,
  77. ) -> GetWebsiteCrawlResponse:
  78. """
  79. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  80. """
  81. datasource_provider_id = GenericProviderID(datasource_provider)
  82. response = self._request_with_plugin_daemon_response_stream(
  83. "POST",
  84. f"plugin/{tenant_id}/dispatch/datasource/get_website_crawl",
  85. GetWebsiteCrawlResponse,
  86. data={
  87. "user_id": user_id,
  88. "data": {
  89. "provider": datasource_provider_id.provider_name,
  90. "datasource": datasource_name,
  91. "credentials": credentials,
  92. "datasource_parameters": datasource_parameters,
  93. },
  94. },
  95. headers={
  96. "X-Plugin-ID": datasource_provider_id.plugin_id,
  97. "Content-Type": "application/json",
  98. },
  99. )
  100. for resp in response:
  101. return resp
  102. raise Exception("No response from plugin daemon")
  103. def get_online_document_pages(
  104. self,
  105. tenant_id: str,
  106. user_id: str,
  107. datasource_provider: str,
  108. datasource_name: str,
  109. credentials: dict[str, Any],
  110. datasource_parameters: Mapping[str, Any],
  111. provider_type: str,
  112. ) -> GetOnlineDocumentPagesResponse:
  113. """
  114. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  115. """
  116. datasource_provider_id = GenericProviderID(datasource_provider)
  117. response = self._request_with_plugin_daemon_response_stream(
  118. "POST",
  119. f"plugin/{tenant_id}/dispatch/datasource/get_online_document_pages",
  120. GetOnlineDocumentPagesResponse,
  121. data={
  122. "user_id": user_id,
  123. "data": {
  124. "provider": datasource_provider_id.provider_name,
  125. "datasource": datasource_name,
  126. "credentials": credentials,
  127. "datasource_parameters": datasource_parameters,
  128. },
  129. },
  130. headers={
  131. "X-Plugin-ID": datasource_provider_id.plugin_id,
  132. "Content-Type": "application/json",
  133. },
  134. )
  135. for resp in response:
  136. return resp
  137. raise Exception("No response from plugin daemon")
  138. def get_online_document_page_content(
  139. self,
  140. tenant_id: str,
  141. user_id: str,
  142. datasource_provider: str,
  143. datasource_name: str,
  144. credentials: dict[str, Any],
  145. datasource_parameters: GetOnlineDocumentPageContentRequest,
  146. provider_type: str,
  147. ) -> GetOnlineDocumentPageContentResponse:
  148. """
  149. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  150. """
  151. datasource_provider_id = GenericProviderID(datasource_provider)
  152. response = self._request_with_plugin_daemon_response_stream(
  153. "POST",
  154. f"plugin/{tenant_id}/dispatch/datasource/get_online_document_page_content",
  155. GetOnlineDocumentPageContentResponse,
  156. data={
  157. "user_id": user_id,
  158. "data": {
  159. "provider": datasource_provider_id.provider_name,
  160. "datasource": datasource_name,
  161. "credentials": credentials,
  162. "datasource_parameters": datasource_parameters,
  163. },
  164. },
  165. headers={
  166. "X-Plugin-ID": datasource_provider_id.plugin_id,
  167. "Content-Type": "application/json",
  168. },
  169. )
  170. for resp in response:
  171. return resp
  172. raise Exception("No response from plugin daemon")
  173. def validate_provider_credentials(
  174. self, tenant_id: str, user_id: str, provider: str, plugin_id: str, credentials: dict[str, Any]
  175. ) -> bool:
  176. """
  177. validate the credentials of the provider
  178. """
  179. # datasource_provider_id = GenericProviderID(provider_id)
  180. response = self._request_with_plugin_daemon_response_stream(
  181. "POST",
  182. f"plugin/{tenant_id}/dispatch/datasource/validate_credentials",
  183. PluginBasicBooleanResponse,
  184. data={
  185. "user_id": user_id,
  186. "data": {
  187. "provider": provider,
  188. "credentials": credentials,
  189. },
  190. },
  191. headers={
  192. "X-Plugin-ID": plugin_id,
  193. "Content-Type": "application/json",
  194. },
  195. )
  196. for resp in response:
  197. return resp.result
  198. return False
  199. def _get_local_file_datasource_provider(self) -> dict[str, Any]:
  200. return {
  201. "id": "langgenius/file/file",
  202. "plugin_id": "langgenius/file",
  203. "provider": "file",
  204. "plugin_unique_identifier": "langgenius/file:0.0.1@dify",
  205. "declaration": {
  206. "identity": {
  207. "author": "langgenius",
  208. "name": "file",
  209. "label": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  210. "icon": "https://assets.dify.ai/images/File%20Upload.svg",
  211. "description": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  212. },
  213. "credentials_schema": [],
  214. "provider_type": "local_file",
  215. "datasources": [
  216. {
  217. "identity": {
  218. "author": "langgenius",
  219. "name": "upload-file",
  220. "provider": "file",
  221. "label": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  222. },
  223. "parameters": [],
  224. "description": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  225. }
  226. ],
  227. },
  228. }