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

datasource.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. for provider in json_response.get("data", []):
  22. declaration = provider.get("declaration", {}) or {}
  23. provider_name = declaration.get("identity", {}).get("name")
  24. for datasource in declaration.get("datasources", []):
  25. datasource["identity"]["provider"] = provider_name
  26. return json_response
  27. # response = self._request_with_plugin_daemon_response(
  28. # "GET",
  29. # f"plugin/{tenant_id}/management/datasources",
  30. # list[PluginDatasourceProviderEntity],
  31. # params={"page": 1, "page_size": 256},
  32. # transformer=transformer,
  33. # )
  34. # for provider in response:
  35. # provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  36. # # override the provider name for each tool to plugin_id/provider_name
  37. # for datasource in provider.declaration.datasources:
  38. # datasource.identity.provider = provider.declaration.identity.name
  39. return [PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())]
  40. def fetch_datasource_provider(self, tenant_id: str, provider: str) -> PluginDatasourceProviderEntity:
  41. """
  42. Fetch datasource provider for the given tenant and plugin.
  43. """
  44. if provider == "langgenius/file/file":
  45. return PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())
  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/datasource",
  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: Mapping[str, Any],
  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/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: Mapping[str, Any],
  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/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/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_provider_oauth_url(self, datasource_type: str, datasource_name: str, provider: str) -> str:
  197. """
  198. get the oauth url of the provider
  199. """
  200. tool_provider_id = GenericProviderID(provider)
  201. response = self._request_with_plugin_daemon_response_stream(
  202. "GET",
  203. "plugin/datasource/oauth",
  204. PluginBasicBooleanResponse,
  205. params={"page": 1, "page_size": 256},
  206. headers={
  207. "X-Plugin-ID": tool_provider_id.plugin_id,
  208. "Content-Type": "application/json",
  209. },
  210. )
  211. for resp in response:
  212. return resp.result
  213. return False
  214. def _get_local_file_datasource_provider(self) -> dict[str, Any]:
  215. return {
  216. "id": "langgenius/file/file",
  217. "plugin_id": "langgenius/file",
  218. "provider": "langgenius",
  219. "plugin_unique_identifier": "langgenius/file:0.0.1@dify",
  220. "declaration": {
  221. "identity": {
  222. "author": "langgenius",
  223. "name": "langgenius/file/file",
  224. "label": {
  225. "zh_Hans": "File",
  226. "en_US": "File",
  227. "pt_BR": "File",
  228. "ja_JP": "File"
  229. },
  230. "icon": "https://cloud.dify.ai/console/api/workspaces/current/plugin/icon?tenant_id=945b4365-9d99-48c1-8c47-90593fe8b9c9&filename=13d9312f6b1352d3939b90a5257de58ff3cd619d5be4f5b266ff0298935ac328.svg",
  231. "description": {
  232. "zh_Hans": "File",
  233. "en_US": "File",
  234. "pt_BR": "File",
  235. "ja_JP": "File"
  236. }
  237. },
  238. "credentials_schema": [],
  239. "provider_type": "local_file",
  240. "datasources": [{
  241. "identity": {
  242. "author": "langgenius",
  243. "name": "local_file",
  244. "provider": "langgenius",
  245. "label": {
  246. "zh_Hans": "File",
  247. "en_US": "File",
  248. "pt_BR": "File",
  249. "ja_JP": "File"
  250. }
  251. },
  252. "parameters": [],
  253. "description": {
  254. "zh_Hans": "File",
  255. "en_US": "File",
  256. "pt_BR": "File",
  257. "ja_JP": "File"
  258. }
  259. }]
  260. }
  261. }