Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. from collections.abc import Mapping
  2. from typing import Any
  3. from core.plugin.entities.plugin import GenericProviderID, ToolProviderID
  4. from core.plugin.entities.plugin_daemon import (
  5. PluginBasicBooleanResponse,
  6. PluginDatasourceProviderEntity,
  7. )
  8. from core.plugin.impl.base import BasePluginClient
  9. class PluginDatasourceManager(BasePluginClient):
  10. def fetch_datasource_providers(self, tenant_id: str) -> list[PluginDatasourceProviderEntity]:
  11. """
  12. Fetch datasource providers for the given tenant.
  13. """
  14. def transformer(json_response: dict[str, Any]) -> dict:
  15. for provider in json_response.get("data", []):
  16. declaration = provider.get("declaration", {}) or {}
  17. provider_name = declaration.get("identity", {}).get("name")
  18. for tool in declaration.get("tools", []):
  19. tool["identity"]["provider"] = provider_name
  20. return json_response
  21. response = self._request_with_plugin_daemon_response(
  22. "GET",
  23. f"plugin/{tenant_id}/management/datasources",
  24. list[PluginDatasourceProviderEntity],
  25. params={"page": 1, "page_size": 256},
  26. transformer=transformer,
  27. )
  28. for provider in response:
  29. provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
  30. # override the provider name for each tool to plugin_id/provider_name
  31. for datasource in provider.declaration.datasources:
  32. datasource.identity.provider = provider.declaration.identity.name
  33. return response
  34. def fetch_datasource_provider(self, tenant_id: str, provider: str) -> PluginDatasourceProviderEntity:
  35. """
  36. Fetch datasource provider for the given tenant and plugin.
  37. """
  38. tool_provider_id = ToolProviderID(provider)
  39. def transformer(json_response: dict[str, Any]) -> dict:
  40. data = json_response.get("data")
  41. if data:
  42. for datasource in data.get("declaration", {}).get("datasources", []):
  43. datasource["identity"]["provider"] = tool_provider_id.provider_name
  44. return json_response
  45. response = self._request_with_plugin_daemon_response(
  46. "GET",
  47. f"plugin/{tenant_id}/management/datasources",
  48. PluginDatasourceProviderEntity,
  49. params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
  50. transformer=transformer,
  51. )
  52. response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
  53. # override the provider name for each tool to plugin_id/provider_name
  54. for datasource in response.declaration.datasources:
  55. datasource.identity.provider = response.declaration.identity.name
  56. return response
  57. def invoke_first_step(
  58. self,
  59. tenant_id: str,
  60. user_id: str,
  61. datasource_provider: str,
  62. datasource_name: str,
  63. credentials: dict[str, Any],
  64. datasource_parameters: dict[str, Any],
  65. ) -> Mapping[str, Any]:
  66. """
  67. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  68. """
  69. datasource_provider_id = GenericProviderID(datasource_provider)
  70. response = self._request_with_plugin_daemon_response_stream(
  71. "POST",
  72. f"plugin/{tenant_id}/dispatch/datasource/first_step",
  73. dict,
  74. data={
  75. "user_id": user_id,
  76. "data": {
  77. "provider": datasource_provider_id.provider_name,
  78. "datasource": datasource_name,
  79. "credentials": credentials,
  80. "datasource_parameters": datasource_parameters,
  81. },
  82. },
  83. headers={
  84. "X-Plugin-ID": datasource_provider_id.plugin_id,
  85. "Content-Type": "application/json",
  86. },
  87. )
  88. for resp in response:
  89. return resp
  90. raise Exception("No response from plugin daemon")
  91. def invoke_second_step(
  92. self,
  93. tenant_id: str,
  94. user_id: str,
  95. datasource_provider: str,
  96. datasource_name: str,
  97. credentials: dict[str, Any],
  98. datasource_parameters: dict[str, Any],
  99. ) -> Mapping[str, Any]:
  100. """
  101. Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
  102. """
  103. datasource_provider_id = GenericProviderID(datasource_provider)
  104. response = self._request_with_plugin_daemon_response_stream(
  105. "POST",
  106. f"plugin/{tenant_id}/dispatch/datasource/second_step",
  107. dict,
  108. data={
  109. "user_id": user_id,
  110. "data": {
  111. "provider": datasource_provider_id.provider_name,
  112. "datasource": datasource_name,
  113. "credentials": credentials,
  114. "datasource_parameters": datasource_parameters,
  115. },
  116. },
  117. headers={
  118. "X-Plugin-ID": datasource_provider_id.plugin_id,
  119. "Content-Type": "application/json",
  120. },
  121. )
  122. for resp in response:
  123. return resp
  124. raise Exception("No response from plugin daemon")
  125. def validate_provider_credentials(
  126. self, tenant_id: str, user_id: str, provider: str, credentials: dict[str, Any]
  127. ) -> bool:
  128. """
  129. validate the credentials of the provider
  130. """
  131. tool_provider_id = GenericProviderID(provider)
  132. response = self._request_with_plugin_daemon_response_stream(
  133. "POST",
  134. f"plugin/{tenant_id}/dispatch/datasource/validate_credentials",
  135. PluginBasicBooleanResponse,
  136. data={
  137. "user_id": user_id,
  138. "data": {
  139. "provider": tool_provider_id.provider_name,
  140. "credentials": credentials,
  141. },
  142. },
  143. headers={
  144. "X-Plugin-ID": tool_provider_id.plugin_id,
  145. "Content-Type": "application/json",
  146. },
  147. )
  148. for resp in response:
  149. return resp.result
  150. return False