dify/api/core/plugin/impl/datasource.py

330 lines
12 KiB
Python
Raw Normal View History

2025-06-13 17:36:24 +08:00
from collections.abc import Generator, Mapping
from typing import Any
2025-04-22 16:08:58 +08:00
2025-05-23 00:05:57 +08:00
from core.datasource.entities.datasource_entities import (
DatasourceMessage,
2025-05-23 00:05:57 +08:00
GetOnlineDocumentPageContentRequest,
2025-06-13 18:22:15 +08:00
OnlineDocumentPagesMessage,
OnlineDriveBrowseFilesRequest,
OnlineDriveBrowseFilesResponse,
OnlineDriveDownloadFileRequest,
2025-06-13 17:36:24 +08:00
WebsiteCrawlMessage,
2025-05-23 00:05:57 +08:00
)
from core.plugin.entities.plugin import DatasourceProviderID, GenericProviderID
from core.plugin.entities.plugin_daemon import (
PluginBasicBooleanResponse,
PluginDatasourceProviderEntity,
)
2025-04-27 14:31:19 +08:00
from core.plugin.impl.base import BasePluginClient
2025-07-01 14:04:09 +08:00
from services.tools.tools_transform_service import ToolTransformService
2025-04-22 16:08:58 +08:00
2025-04-27 14:31:19 +08:00
class PluginDatasourceManager(BasePluginClient):
2025-05-25 23:09:01 +08:00
def fetch_datasource_providers(self, tenant_id: str) -> list[PluginDatasourceProviderEntity]:
2025-04-22 16:08:58 +08:00
"""
Fetch datasource providers for the given tenant.
"""
def transformer(json_response: dict[str, Any]) -> dict:
2025-05-29 09:53:42 +08:00
if json_response.get("data"):
for provider in json_response.get("data", []):
declaration = provider.get("declaration", {}) or {}
provider_name = declaration.get("identity", {}).get("name")
for datasource in declaration.get("datasources", []):
datasource["identity"]["provider"] = provider_name
2025-04-22 16:08:58 +08:00
return json_response
2025-05-28 17:56:04 +08:00
response = self._request_with_plugin_daemon_response(
"GET",
f"plugin/{tenant_id}/management/datasources",
list[PluginDatasourceProviderEntity],
params={"page": 1, "page_size": 256},
transformer=transformer,
)
local_file_datasource_provider = PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())
2025-07-01 14:04:09 +08:00
for provider in response:
ToolTransformService.repack_provider(tenant_id=tenant_id, provider=provider)
2025-06-04 15:12:05 +08:00
all_response = [local_file_datasource_provider] + response
2025-04-22 16:08:58 +08:00
2025-06-04 15:12:05 +08:00
for provider in all_response:
provider.declaration.identity.name = f"{provider.plugin_id}/{provider.declaration.identity.name}"
# override the provider name for each tool to plugin_id/provider_name
for tool in provider.declaration.datasources:
tool.identity.provider = provider.declaration.identity.name
return all_response
def fetch_datasource_provider(self, tenant_id: str, provider_id: str) -> PluginDatasourceProviderEntity:
2025-04-22 16:08:58 +08:00
"""
Fetch datasource provider for the given tenant and plugin.
"""
2025-06-04 15:12:05 +08:00
if provider_id == "langgenius/file/file":
2025-05-25 23:09:01 +08:00
return PluginDatasourceProviderEntity(**self._get_local_file_datasource_provider())
tool_provider_id = DatasourceProviderID(provider_id)
2025-04-22 16:08:58 +08:00
def transformer(json_response: dict[str, Any]) -> dict:
data = json_response.get("data")
if data:
for datasource in data.get("declaration", {}).get("datasources", []):
datasource["identity"]["provider"] = tool_provider_id.provider_name
return json_response
response = self._request_with_plugin_daemon_response(
"GET",
f"plugin/{tenant_id}/management/datasource",
PluginDatasourceProviderEntity,
2025-04-22 16:08:58 +08:00
params={"provider": tool_provider_id.provider_name, "plugin_id": tool_provider_id.plugin_id},
transformer=transformer,
)
response.declaration.identity.name = f"{response.plugin_id}/{response.declaration.identity.name}"
# override the provider name for each tool to plugin_id/provider_name
for datasource in response.declaration.datasources:
datasource.identity.provider = response.declaration.identity.name
2025-04-22 16:08:58 +08:00
return response
2025-05-23 00:05:57 +08:00
def get_website_crawl(
2025-04-22 16:08:58 +08:00
self,
tenant_id: str,
user_id: str,
datasource_provider: str,
datasource_name: str,
credentials: dict[str, Any],
2025-05-23 17:11:56 +08:00
datasource_parameters: Mapping[str, Any],
2025-05-23 00:05:57 +08:00
provider_type: str,
2025-06-13 17:36:24 +08:00
) -> Generator[WebsiteCrawlMessage, None, None]:
2025-04-22 16:08:58 +08:00
"""
Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
"""
datasource_provider_id = GenericProviderID(datasource_provider)
2025-06-13 17:36:24 +08:00
return self._request_with_plugin_daemon_response_stream(
2025-04-22 16:08:58 +08:00
"POST",
2025-05-23 15:10:16 +08:00
f"plugin/{tenant_id}/dispatch/datasource/get_website_crawl",
2025-06-13 17:36:24 +08:00
WebsiteCrawlMessage,
2025-04-22 16:08:58 +08:00
data={
"user_id": user_id,
"data": {
"provider": datasource_provider_id.provider_name,
"datasource": datasource_name,
"credentials": credentials,
"datasource_parameters": datasource_parameters,
},
},
headers={
"X-Plugin-ID": datasource_provider_id.plugin_id,
"Content-Type": "application/json",
},
)
2025-04-27 14:31:19 +08:00
2025-05-23 00:05:57 +08:00
def get_online_document_pages(
2025-04-22 16:08:58 +08:00
self,
tenant_id: str,
user_id: str,
datasource_provider: str,
datasource_name: str,
credentials: dict[str, Any],
2025-05-23 17:11:56 +08:00
datasource_parameters: Mapping[str, Any],
2025-05-23 00:05:57 +08:00
provider_type: str,
2025-06-13 18:22:15 +08:00
) -> Generator[OnlineDocumentPagesMessage, None, None]:
2025-04-22 16:08:58 +08:00
"""
Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
"""
datasource_provider_id = GenericProviderID(datasource_provider)
return self._request_with_plugin_daemon_response_stream(
2025-04-22 16:08:58 +08:00
"POST",
2025-05-23 15:10:16 +08:00
f"plugin/{tenant_id}/dispatch/datasource/get_online_document_pages",
2025-06-13 18:22:15 +08:00
OnlineDocumentPagesMessage,
2025-05-23 00:05:57 +08:00
data={
"user_id": user_id,
"data": {
"provider": datasource_provider_id.provider_name,
"datasource": datasource_name,
"credentials": credentials,
"datasource_parameters": datasource_parameters,
},
},
headers={
"X-Plugin-ID": datasource_provider_id.plugin_id,
"Content-Type": "application/json",
},
)
def get_online_document_page_content(
self,
tenant_id: str,
user_id: str,
datasource_provider: str,
datasource_name: str,
credentials: dict[str, Any],
datasource_parameters: GetOnlineDocumentPageContentRequest,
provider_type: str,
) -> Generator[DatasourceMessage, None, None]:
2025-05-23 00:05:57 +08:00
"""
Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
"""
datasource_provider_id = GenericProviderID(datasource_provider)
return self._request_with_plugin_daemon_response_stream(
2025-05-23 00:05:57 +08:00
"POST",
2025-05-23 15:10:16 +08:00
f"plugin/{tenant_id}/dispatch/datasource/get_online_document_page_content",
DatasourceMessage,
2025-04-22 16:08:58 +08:00
data={
"user_id": user_id,
"data": {
"provider": datasource_provider_id.provider_name,
"datasource": datasource_name,
"credentials": credentials,
2025-06-19 15:30:46 +08:00
"page": datasource_parameters.model_dump(),
2025-04-22 16:08:58 +08:00
},
},
headers={
"X-Plugin-ID": datasource_provider_id.plugin_id,
"Content-Type": "application/json",
},
)
def online_drive_browse_files(
self,
tenant_id: str,
user_id: str,
datasource_provider: str,
datasource_name: str,
credentials: dict[str, Any],
request: OnlineDriveBrowseFilesRequest,
provider_type: str,
) -> Generator[OnlineDriveBrowseFilesResponse, None, None]:
"""
Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
"""
datasource_provider_id = GenericProviderID(datasource_provider)
response = self._request_with_plugin_daemon_response_stream(
"POST",
f"plugin/{tenant_id}/dispatch/datasource/online_drive_browse_files",
OnlineDriveBrowseFilesResponse,
data={
"user_id": user_id,
"data": {
"provider": datasource_provider_id.provider_name,
"datasource": datasource_name,
"credentials": credentials,
"request": request.model_dump(),
},
},
headers={
"X-Plugin-ID": datasource_provider_id.plugin_id,
"Content-Type": "application/json",
},
)
yield from response
def online_drive_download_file(
self,
tenant_id: str,
user_id: str,
datasource_provider: str,
datasource_name: str,
credentials: dict[str, Any],
request: OnlineDriveDownloadFileRequest,
provider_type: str,
) -> Generator[DatasourceMessage, None, None]:
"""
Invoke the datasource with the given tenant, user, plugin, provider, name, credentials and parameters.
"""
datasource_provider_id = GenericProviderID(datasource_provider)
response = self._request_with_plugin_daemon_response_stream(
"POST",
f"plugin/{tenant_id}/dispatch/datasource/online_drive_download_file",
DatasourceMessage,
data={
"user_id": user_id,
"data": {
"provider": datasource_provider_id.provider_name,
"datasource": datasource_name,
"credentials": credentials,
"request": request.model_dump(),
},
},
headers={
"X-Plugin-ID": datasource_provider_id.plugin_id,
"Content-Type": "application/json",
},
)
yield from response
2025-04-22 16:08:58 +08:00
def validate_provider_credentials(
2025-06-05 11:12:06 +08:00
self, tenant_id: str, user_id: str, provider: str, plugin_id: str, credentials: dict[str, Any]
2025-04-22 16:08:58 +08:00
) -> bool:
"""
validate the credentials of the provider
"""
2025-06-05 11:12:06 +08:00
# datasource_provider_id = GenericProviderID(provider_id)
2025-04-22 16:08:58 +08:00
response = self._request_with_plugin_daemon_response_stream(
"POST",
f"plugin/{tenant_id}/dispatch/datasource/validate_credentials",
2025-04-22 16:08:58 +08:00
PluginBasicBooleanResponse,
data={
"user_id": user_id,
"data": {
2025-06-05 11:12:06 +08:00
"provider": provider,
2025-04-22 16:08:58 +08:00
"credentials": credentials,
},
},
headers={
2025-06-05 11:12:06 +08:00
"X-Plugin-ID": plugin_id,
2025-04-22 16:08:58 +08:00
"Content-Type": "application/json",
},
)
for resp in response:
return resp.result
return False
2025-05-23 00:05:57 +08:00
def _get_local_file_datasource_provider(self) -> dict[str, Any]:
return {
"id": "langgenius/file/file",
2025-06-04 15:12:05 +08:00
"plugin_id": "langgenius/file",
"provider": "file",
2025-05-23 00:05:57 +08:00
"plugin_unique_identifier": "langgenius/file:0.0.1@dify",
2025-05-25 23:09:01 +08:00
"declaration": {
"identity": {
2025-05-23 15:10:16 +08:00
"author": "langgenius",
2025-06-04 15:12:05 +08:00
"name": "file",
2025-06-03 19:02:57 +08:00
"label": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
2025-06-11 17:10:20 +08:00
"icon": "https://assets.dify.ai/images/File%20Upload.svg",
2025-06-03 19:02:57 +08:00
"description": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
2025-05-25 23:09:01 +08:00
},
"credentials_schema": [],
"provider_type": "local_file",
2025-06-03 19:02:57 +08:00
"datasources": [
{
"identity": {
"author": "langgenius",
"name": "upload-file",
2025-06-04 15:12:05 +08:00
"provider": "file",
2025-06-03 19:02:57 +08:00
"label": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
},
"parameters": [],
"description": {"zh_Hans": "File", "en_US": "File", "pt_BR": "File", "ja_JP": "File"},
2025-05-25 23:09:01 +08:00
}
2025-06-03 19:02:57 +08:00
],
},
2025-05-23 00:05:57 +08:00
}