2024-10-14 17:52:29 +08:00
|
|
|
from collections.abc import Sequence
|
2024-10-08 21:28:59 +08:00
|
|
|
from mimetypes import guess_type
|
|
|
|
|
2024-10-17 18:44:16 +08:00
|
|
|
from configs import dify_config
|
2024-10-10 16:35:36 +08:00
|
|
|
from core.helper.download import download_with_size_limit
|
2024-10-11 02:00:02 +08:00
|
|
|
from core.helper.marketplace import download_plugin_pkg
|
2024-10-14 17:52:29 +08:00
|
|
|
from core.plugin.entities.plugin import PluginDeclaration, PluginEntity, PluginInstallationSource
|
2024-10-21 18:48:03 +08:00
|
|
|
from core.plugin.entities.plugin_daemon import PluginInstallTask, PluginUploadResponse
|
2024-10-08 21:28:59 +08:00
|
|
|
from core.plugin.manager.asset import PluginAssetManager
|
|
|
|
from core.plugin.manager.debugging import PluginDebuggingManager
|
|
|
|
from core.plugin.manager.plugin import PluginInstallationManager
|
|
|
|
|
|
|
|
|
|
|
|
class PluginService:
|
|
|
|
@staticmethod
|
2024-10-10 16:35:36 +08:00
|
|
|
def get_debugging_key(tenant_id: str) -> str:
|
2024-10-14 17:52:29 +08:00
|
|
|
"""
|
|
|
|
get the debugging key of the tenant
|
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
manager = PluginDebuggingManager()
|
|
|
|
return manager.get_debugging_key(tenant_id)
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-10 16:35:36 +08:00
|
|
|
def list(tenant_id: str) -> list[PluginEntity]:
|
2024-10-14 17:52:29 +08:00
|
|
|
"""
|
|
|
|
list all plugins of the tenant
|
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
manager = PluginInstallationManager()
|
|
|
|
return manager.list_plugins(tenant_id)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_asset(tenant_id: str, asset_file: str) -> tuple[bytes, str]:
|
2024-10-14 17:52:29 +08:00
|
|
|
"""
|
|
|
|
get the asset file of the plugin
|
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
manager = PluginAssetManager()
|
|
|
|
# guess mime type
|
|
|
|
mime_type, _ = guess_type(asset_file)
|
|
|
|
return manager.fetch_asset(tenant_id, asset_file), mime_type or "application/octet-stream"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def check_plugin_unique_identifier(tenant_id: str, plugin_unique_identifier: str) -> bool:
|
2024-10-14 17:52:29 +08:00
|
|
|
"""
|
|
|
|
check if the plugin unique identifier is already installed by other tenant
|
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
manager = PluginInstallationManager()
|
|
|
|
return manager.fetch_plugin_by_identifier(tenant_id, plugin_unique_identifier)
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-14 17:52:29 +08:00
|
|
|
def fetch_plugin_manifest(tenant_id: str, plugin_unique_identifier: str) -> PluginDeclaration:
|
|
|
|
manager = PluginInstallationManager()
|
|
|
|
return manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-16 14:02:05 +08:00
|
|
|
def fetch_install_tasks(tenant_id: str, page: int, page_size: int) -> Sequence[PluginInstallTask]:
|
2024-10-14 17:52:29 +08:00
|
|
|
manager = PluginInstallationManager()
|
2024-10-16 14:02:05 +08:00
|
|
|
return manager.fetch_plugin_installation_tasks(tenant_id, page, page_size)
|
2024-10-14 17:52:29 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def fetch_install_task(tenant_id: str, task_id: str) -> PluginInstallTask:
|
2024-10-08 21:28:59 +08:00
|
|
|
manager = PluginInstallationManager()
|
2024-10-14 17:52:29 +08:00
|
|
|
return manager.fetch_plugin_installation_task(tenant_id, task_id)
|
2024-10-08 21:28:59 +08:00
|
|
|
|
2024-10-16 14:47:48 +08:00
|
|
|
@staticmethod
|
|
|
|
def delete_install_task(tenant_id: str, task_id: str) -> bool:
|
|
|
|
manager = PluginInstallationManager()
|
|
|
|
return manager.delete_plugin_installation_task(tenant_id, task_id)
|
|
|
|
|
2024-10-25 13:22:37 +08:00
|
|
|
@staticmethod
|
|
|
|
def delete_install_task_item(tenant_id: str, task_id: str, identifier: str) -> bool:
|
|
|
|
manager = PluginInstallationManager()
|
|
|
|
return manager.delete_plugin_installation_task_item(tenant_id, task_id, identifier)
|
|
|
|
|
2024-10-08 21:28:59 +08:00
|
|
|
@staticmethod
|
2024-10-21 18:48:03 +08:00
|
|
|
def upload_pkg(tenant_id: str, pkg: bytes, verify_signature: bool = False) -> PluginUploadResponse:
|
2024-10-10 16:35:36 +08:00
|
|
|
"""
|
2024-10-14 17:52:29 +08:00
|
|
|
Upload plugin package files
|
|
|
|
|
|
|
|
returns: plugin_unique_identifier
|
2024-10-10 16:35:36 +08:00
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
manager = PluginInstallationManager()
|
2024-10-16 14:02:05 +08:00
|
|
|
return manager.upload_pkg(tenant_id, pkg, verify_signature)
|
2024-10-08 22:38:33 +08:00
|
|
|
|
|
|
|
@staticmethod
|
2024-10-16 14:02:05 +08:00
|
|
|
def upload_pkg_from_github(
|
|
|
|
tenant_id: str, repo: str, version: str, package: str, verify_signature: bool = False
|
2024-10-21 18:48:03 +08:00
|
|
|
) -> PluginUploadResponse:
|
2024-10-10 16:35:36 +08:00
|
|
|
"""
|
2024-10-14 17:52:29 +08:00
|
|
|
Install plugin from github release package files,
|
|
|
|
returns plugin_unique_identifier
|
2024-10-10 16:35:36 +08:00
|
|
|
"""
|
|
|
|
pkg = download_with_size_limit(
|
2024-10-17 18:44:16 +08:00
|
|
|
f"https://github.com/{repo}/releases/download/{version}/{package}", dify_config.PLUGIN_MAX_PACKAGE_SIZE
|
2024-10-10 16:35:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
manager = PluginInstallationManager()
|
2024-10-14 17:52:29 +08:00
|
|
|
return manager.upload_pkg(
|
|
|
|
tenant_id,
|
|
|
|
pkg,
|
2024-10-16 14:02:05 +08:00
|
|
|
verify_signature,
|
2024-10-14 17:52:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-16 14:02:05 +08:00
|
|
|
def install_from_local_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
|
2024-10-14 17:52:29 +08:00
|
|
|
manager = PluginInstallationManager()
|
|
|
|
return manager.install_from_identifiers(
|
|
|
|
tenant_id,
|
2024-10-16 14:02:05 +08:00
|
|
|
plugin_unique_identifiers,
|
2024-10-14 17:52:29 +08:00
|
|
|
PluginInstallationSource.Package,
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-16 13:03:50 +08:00
|
|
|
def install_from_github(tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str):
|
2024-10-14 17:52:29 +08:00
|
|
|
"""
|
|
|
|
Install plugin from github release package files,
|
|
|
|
returns plugin_unique_identifier
|
|
|
|
"""
|
|
|
|
manager = PluginInstallationManager()
|
|
|
|
return manager.install_from_identifiers(
|
|
|
|
tenant_id,
|
|
|
|
[plugin_unique_identifier],
|
|
|
|
PluginInstallationSource.Github,
|
|
|
|
{
|
|
|
|
"repo": repo,
|
|
|
|
"version": version,
|
|
|
|
"package": package,
|
|
|
|
},
|
|
|
|
)
|
2024-10-10 16:35:36 +08:00
|
|
|
|
|
|
|
@staticmethod
|
2024-10-16 14:02:05 +08:00
|
|
|
def install_from_marketplace_pkg(
|
|
|
|
tenant_id: str, plugin_unique_identifiers: Sequence[str], verify_signature: bool = False
|
|
|
|
):
|
2024-10-10 16:35:36 +08:00
|
|
|
"""
|
2024-10-14 17:52:29 +08:00
|
|
|
Install plugin from marketplace package files,
|
|
|
|
returns installation task id
|
2024-10-10 16:35:36 +08:00
|
|
|
"""
|
|
|
|
manager = PluginInstallationManager()
|
|
|
|
|
2024-10-16 14:02:05 +08:00
|
|
|
# check if already downloaded
|
|
|
|
for plugin_unique_identifier in plugin_unique_identifiers:
|
|
|
|
try:
|
|
|
|
manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
|
|
|
|
# already downloaded, skip
|
|
|
|
except Exception:
|
|
|
|
# plugin not installed, download and upload pkg
|
|
|
|
pkg = download_plugin_pkg(plugin_unique_identifier)
|
|
|
|
manager.upload_pkg(tenant_id, pkg, verify_signature)
|
2024-10-14 17:52:29 +08:00
|
|
|
|
|
|
|
return manager.install_from_identifiers(
|
|
|
|
tenant_id,
|
2024-10-16 14:02:05 +08:00
|
|
|
plugin_unique_identifiers,
|
2024-10-14 17:52:29 +08:00
|
|
|
PluginInstallationSource.Marketplace,
|
|
|
|
{
|
|
|
|
"plugin_unique_identifier": plugin_unique_identifier,
|
|
|
|
},
|
|
|
|
)
|
2024-10-10 16:35:36 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def uninstall(tenant_id: str, plugin_installation_id: str) -> bool:
|
2024-10-08 22:38:33 +08:00
|
|
|
manager = PluginInstallationManager()
|
|
|
|
return manager.uninstall(tenant_id, plugin_installation_id)
|