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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. return [local_file_datasource_provider] + response
  37. def fetch_datasource_provider(self, tenant_id: str, provider: str) -> PluginDatasourceProviderEntity:
  38. """
  39. Fetch datasource provider for the given tenant and plugin.
  40. """
  41. if provider == "langgenius/file/file":
  42. return PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())
  43. tool_provider_id = ToolProviderID(provider)
  44. def transformer(json_response: dict[str, Any]) -> dict:
  45. data = json_response.get("data")
  46. if data:
  47. for datasource in data.get("declaration", {}).get("datasources", []):
  48. datasource["identity"]["provider"] = tool_provider_id.provider_name
  49. return json_response
  50. response = self._request_with_plugin_daemon_response(
  51. "GET",
  52. f"plugin/{tenant_id}/management/datasource",
  53. PluginDatasourceProviderEntity,
  54. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  55. transformer=transformer,
  56. )
  57. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  58. # override the provider name for each tool to plugin_id/provider_name
  59. for datasource in response.declaration.datasources:
  60. datasource.identity.provider = response.declaration.identity.name
  61. return response
  62. def get_website_crawl(
  63. self,
  64. tenant_id: str,
  65. user_id: str,
  66. datasource_provider: str,
  67. datasource_name: str,
  68. credentials: dict[str, Any],
  69. datasource_parameters: Mapping[str, Any],
  70. provider_type: str,
  71. ) -> GetWebsiteCrawlResponse:
  72. """
  73. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  74. """
  75. datasource_provider_id = GenericProviderID(datasource_provider)
  76. response = self._request_with_plugin_daemon_response_stream(
  77. "POST",
  78. f"plugin/{tenant_id}/dispatch/datasource/get_website_crawl",
  79. GetWebsiteCrawlResponse,
  80. data={
  81. "user_id": user_id,
  82. "data": {
  83. "provider": datasource_provider_id.provider_name,
  84. "datasource": datasource_name,
  85. "credentials": credentials,
  86. "datasource_parameters": datasource_parameters,
  87. },
  88. },
  89. headers={
  90. "X-Plugin-ID": datasource_provider_id.plugin_id,
  91. "Content-Type": "application/json",
  92. },
  93. )
  94. for resp in response:
  95. return resp
  96. raise Exception("No response from plugin daemon")
  97. def get_online_document_pages(
  98. self,
  99. tenant_id: str,
  100. user_id: str,
  101. datasource_provider: str,
  102. datasource_name: str,
  103. credentials: dict[str, Any],
  104. datasource_parameters: Mapping[str, Any],
  105. provider_type: str,
  106. ) -> GetOnlineDocumentPagesResponse:
  107. """
  108. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  109. """
  110. datasource_provider_id = GenericProviderID(datasource_provider)
  111. response = self._request_with_plugin_daemon_response_stream(
  112. "POST",
  113. f"plugin/{tenant_id}/dispatch/datasource/get_online_document_pages",
  114. GetOnlineDocumentPagesResponse,
  115. data={
  116. "user_id": user_id,
  117. "data": {
  118. "provider": datasource_provider_id.provider_name,
  119. "datasource": datasource_name,
  120. "credentials": credentials,
  121. "datasource_parameters": datasource_parameters,
  122. },
  123. },
  124. headers={
  125. "X-Plugin-ID": datasource_provider_id.plugin_id,
  126. "Content-Type": "application/json",
  127. },
  128. )
  129. for resp in response:
  130. return resp
  131. raise Exception("No response from plugin daemon")
  132. def get_online_document_page_content(
  133. self,
  134. tenant_id: str,
  135. user_id: str,
  136. datasource_provider: str,
  137. datasource_name: str,
  138. credentials: dict[str, Any],
  139. datasource_parameters: GetOnlineDocumentPageContentRequest,
  140. provider_type: str,
  141. ) -> GetOnlineDocumentPageContentResponse:
  142. """
  143. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  144. """
  145. datasource_provider_id = GenericProviderID(datasource_provider)
  146. response = self._request_with_plugin_daemon_response_stream(
  147. "POST",
  148. f"plugin/{tenant_id}/dispatch/datasource/get_online_document_page_content",
  149. GetOnlineDocumentPageContentResponse,
  150. data={
  151. "user_id": user_id,
  152. "data": {
  153. "provider": datasource_provider_id.provider_name,
  154. "datasource": datasource_name,
  155. "credentials": credentials,
  156. "datasource_parameters": datasource_parameters,
  157. },
  158. },
  159. headers={
  160. "X-Plugin-ID": datasource_provider_id.plugin_id,
  161. "Content-Type": "application/json",
  162. },
  163. )
  164. for resp in response:
  165. return resp
  166. raise Exception("No response from plugin daemon")
  167. def validate_provider_credentials(
  168. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  169. ) -> bool:
  170. """
  171. validate the credentials of the provider
  172. """
  173. datasource_provider_id = GenericProviderID(provider)
  174. response = self._request_with_plugin_daemon_response_stream(
  175. "POST",
  176. f"plugin/{tenant_id}/dispatch/datasource/validate_credentials",
  177. PluginBasicBooleanResponse,
  178. data={
  179. "user_id": user_id,
  180. "data": {
  181. "provider": datasource_provider_id.provider_name,
  182. "credentials": credentials,
  183. },
  184. },
  185. headers={
  186. "X-Plugin-ID": datasource_provider_id.plugin_id,
  187. "Content-Type": "application/json",
  188. },
  189. )
  190. for resp in response:
  191. return resp.result
  192. return False
  193. def _get_local_file_datasource_provider(self) -> dict[str, Any]:
  194. return {
  195. "id": "langgenius/file/file",
  196. "plugin_id": "langgenius/file/file",
  197. "provider": "langgenius",
  198. "plugin_unique_identifier": "langgenius/file:0.0.1@dify",
  199. "declaration": {
  200. "identity": {
  201. "author": "langgenius",
  202. "name": "langgenius/file/file",
  203. "label": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  204. "icon": "https://cloud.dify.ai/console/api/workspaces/current/plugin/icon?tenant_id=945b4365-9d99-48c1-8c47-90593fe8b9c9&filename=13d9312f6b1352d3939b90a5257de58ff3cd619d5be4f5b266ff0298935ac328.svg",
  205. "description": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  206. },
  207. "credentials_schema": [],
  208. "provider_type": "local_file",
  209. "datasources": [
  210. {
  211. "identity": {
  212. "author": "langgenius",
  213. "name": "upload-file",
  214. "provider": "langgenius",
  215. "label": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  216. },
  217. "parameters": [],
  218. "description": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
  219. }
  220. ],
  221. },
  222. }