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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. from collections.abc import Mapping
  2. from typing import Any
  3. from core.datasource.entities.api_entities import DatasourceProviderApiEntity
  4. from core.datasource.entities.datasource_entities import (
  5. GetOnlineDocumentPageContentRequest,
  6. GetOnlineDocumentPageContentResponse,
  7. GetOnlineDocumentPagesResponse,
  8. GetWebsiteCrawlResponse, DatasourceProviderEntity,
  9. )
  10. from core.plugin.entities.plugin import GenericProviderID, ToolProviderID
  11. from core.plugin.entities.plugin_daemon import (
  12. PluginBasicBooleanResponse,
  13. PluginDatasourceProviderEntity,
  14. )
  15. from core.plugin.impl.base import BasePluginClient
  16. class PluginDatasourceManager(BasePluginClient):
  17. def fetch_datasource_providers(self, tenant_id: str) -> list[PluginDatasourceProviderEntity]:
  18. """
  19. Fetch datasource providers for the given tenant.
  20. """
  21. def transformer(json_response: dict[str, Any]) -> dict:
  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. # for provider in response:
  36. # provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  37. # # override the provider name for each tool to plugin_id/provider_name
  38. # for datasource in provider.declaration.datasources:
  39. # datasource.identity.provider = provider.declaration.identity.name
  40. return [PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())]
  41. def fetch_datasource_provider(self, tenant_id: str, provider: str) -> PluginDatasourceProviderEntity:
  42. """
  43. Fetch datasource provider for the given tenant and plugin.
  44. """
  45. if provider == "langgenius/file/file":
  46. return PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())
  47. tool_provider_id = ToolProviderID(provider)
  48. def transformer(json_response: dict[str, Any]) -> dict:
  49. data = json_response.get("data")
  50. if data:
  51. for datasource in data.get("declaration", {}).get("datasources", []):
  52. datasource["identity"]["provider"] = tool_provider_id.provider_name
  53. return json_response
  54. response = self._request_with_plugin_daemon_response(
  55. "GET",
  56. f"plugin/{tenant_id}/management/datasource",
  57. PluginDatasourceProviderEntity,
  58. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  59. transformer=transformer,
  60. )
  61. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  62. # override the provider name for each tool to plugin_id/provider_name
  63. for datasource in response.declaration.datasources:
  64. datasource.identity.provider = response.declaration.identity.name
  65. return response
  66. def get_website_crawl(
  67. self,
  68. tenant_id: str,
  69. user_id: str,
  70. datasource_provider: str,
  71. datasource_name: str,
  72. credentials: dict[str, Any],
  73. datasource_parameters: Mapping[str, Any],
  74. provider_type: str,
  75. ) -> GetWebsiteCrawlResponse:
  76. """
  77. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  78. """
  79. datasource_provider_id = GenericProviderID(datasource_provider)
  80. response = self._request_with_plugin_daemon_response_stream(
  81. "POST",
  82. f"plugin/{tenant_id}/dispatch/datasource/get_website_crawl",
  83. GetWebsiteCrawlResponse,
  84. data={
  85. "user_id": user_id,
  86. "data": {
  87. "provider": datasource_provider_id.provider_name,
  88. "datasource": datasource_name,
  89. "credentials": credentials,
  90. "datasource_parameters": datasource_parameters,
  91. },
  92. },
  93. headers={
  94. "X-Plugin-ID": datasource_provider_id.plugin_id,
  95. "Content-Type": "application/json",
  96. },
  97. )
  98. for resp in response:
  99. return resp
  100. raise Exception("No response from plugin daemon")
  101. def get_online_document_pages(
  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. ) -> GetOnlineDocumentPagesResponse:
  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. response = self._request_with_plugin_daemon_response_stream(
  116. "POST",
  117. f"plugin/{tenant_id}/dispatch/datasource/get_online_document_pages",
  118. GetOnlineDocumentPagesResponse,
  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. for resp in response:
  134. return resp
  135. raise Exception("No response from plugin daemon")
  136. def get_online_document_page_content(
  137. self,
  138. tenant_id: str,
  139. user_id: str,
  140. datasource_provider: str,
  141. datasource_name: str,
  142. credentials: dict[str, Any],
  143. datasource_parameters: GetOnlineDocumentPageContentRequest,
  144. provider_type: str,
  145. ) -> GetOnlineDocumentPageContentResponse:
  146. """
  147. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  148. """
  149. datasource_provider_id = GenericProviderID(datasource_provider)
  150. response = self._request_with_plugin_daemon_response_stream(
  151. "POST",
  152. f"plugin/{tenant_id}/dispatch/datasource/get_online_document_page_content",
  153. GetOnlineDocumentPageContentResponse,
  154. data={
  155. "user_id": user_id,
  156. "data": {
  157. "provider": datasource_provider_id.provider_name,
  158. "datasource": datasource_name,
  159. "credentials": credentials,
  160. "datasource_parameters": datasource_parameters,
  161. },
  162. },
  163. headers={
  164. "X-Plugin-ID": datasource_provider_id.plugin_id,
  165. "Content-Type": "application/json",
  166. },
  167. )
  168. for resp in response:
  169. return resp
  170. raise Exception("No response from plugin daemon")
  171. def validate_provider_credentials(
  172. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  173. ) -> bool:
  174. """
  175. validate the credentials of the provider
  176. """
  177. tool_provider_id = GenericProviderID(provider)
  178. response = self._request_with_plugin_daemon_response_stream(
  179. "POST",
  180. f"plugin/{tenant_id}/dispatch/datasource/validate_credentials",
  181. PluginBasicBooleanResponse,
  182. data={
  183. "user_id": user_id,
  184. "data": {
  185. "provider": tool_provider_id.provider_name,
  186. "credentials": credentials,
  187. },
  188. },
  189. headers={
  190. "X-Plugin-ID": tool_provider_id.plugin_id,
  191. "Content-Type": "application/json",
  192. },
  193. )
  194. for resp in response:
  195. return resp.result
  196. return False
  197. def _get_local_file_datasource_provider(self) -> dict[str, Any]:
  198. return {
  199. "id": "langgenius/file/file",
  200. "plugin_id": "langgenius/file",
  201. "provider": "langgenius",
  202. "plugin_unique_identifier": "langgenius/file:0.0.1@dify",
  203. "declaration": {
  204. "identity": {
  205. "author": "langgenius",
  206. "name": "langgenius/file/file",
  207. "label": {
  208. "zh_Hans": "File",
  209. "en_US": "File",
  210. "pt_BR": "File",
  211. "ja_JP": "File"
  212. },
  213. "icon": "https://cloud.dify.ai/console/api/workspaces/current/plugin/icon?tenant_id=945b4365-9d99-48c1-8c47-90593fe8b9c9&filename=13d9312f6b1352d3939b90a5257de58ff3cd619d5be4f5b266ff0298935ac328.svg",
  214. "description": {
  215. "zh_Hans": "File",
  216. "en_US": "File",
  217. "pt_BR": "File",
  218. "ja_JP": "File"
  219. }
  220. },
  221. "credentials_schema": [],
  222. "provider_type": "local_file",
  223. "datasources": [{
  224. "identity": {
  225. "author": "langgenius",
  226. "name": "local_file",
  227. "provider": "langgenius",
  228. "label": {
  229. "zh_Hans": "File",
  230. "en_US": "File",
  231. "pt_BR": "File",
  232. "ja_JP": "File"
  233. }
  234. },
  235. "parameters": [],
  236. "description": {
  237. "zh_Hans": "File",
  238. "en_US": "File",
  239. "pt_BR": "File",
  240. "ja_JP": "File"
  241. }
  242. }]
  243. }
  244. }