dify/api/core/plugin/manager/plugin.py

87 lines
2.8 KiB
Python
Raw Normal View History

2024-10-10 16:35:36 +08:00
import json
from collections.abc import Generator, Mapping
from typing import Any
2024-09-20 21:35:19 +08:00
2024-10-10 16:35:36 +08:00
from core.plugin.entities.plugin import PluginEntity, PluginInstallationSource
2024-09-20 21:35:19 +08:00
from core.plugin.entities.plugin_daemon import InstallPluginMessage
from core.plugin.manager.base import BasePluginManager
class PluginInstallationManager(BasePluginManager):
def fetch_plugin_by_identifier(self, tenant_id: str, identifier: str) -> bool:
# urlencode the identifier
return self._request_with_plugin_daemon_response(
2024-10-08 21:28:59 +08:00
"GET",
f"plugin/{tenant_id}/management/fetch/identifier",
bool,
params={"plugin_unique_identifier": identifier},
2024-09-20 21:35:19 +08:00
)
2024-09-26 12:49:00 +08:00
def list_plugins(self, tenant_id: str) -> list[PluginEntity]:
return self._request_with_plugin_daemon_response(
"GET",
f"plugin/{tenant_id}/management/list",
list[PluginEntity],
params={"page": 1, "page_size": 256},
)
2024-10-09 23:13:01 +08:00
def install_from_pkg(
2024-10-10 16:35:36 +08:00
self,
tenant_id: str,
pkg: bytes,
source: PluginInstallationSource,
meta: Mapping[str, Any],
verify_signature: bool = False,
2024-10-09 23:13:01 +08:00
) -> Generator[InstallPluginMessage, None, None]:
2024-09-20 21:35:19 +08:00
"""
Install a plugin from a package.
"""
# using multipart/form-data to encode body
2024-10-09 23:13:01 +08:00
body = {
"dify_pkg": ("dify_pkg", pkg, "application/octet-stream"),
2024-10-10 16:35:36 +08:00
}
data = {
2024-10-09 23:13:01 +08:00
"verify_signature": "true" if verify_signature else "false",
2024-10-10 16:35:36 +08:00
"source": source.value,
"meta": json.dumps(meta),
2024-10-09 23:13:01 +08:00
}
2024-09-20 21:35:19 +08:00
return self._request_with_plugin_daemon_response_stream(
2024-10-08 21:28:59 +08:00
"POST",
f"plugin/{tenant_id}/management/install/pkg",
InstallPluginMessage,
files=body,
2024-10-10 16:35:36 +08:00
data=data,
2024-09-20 21:35:19 +08:00
)
def install_from_identifier(self, tenant_id: str, identifier: str) -> bool:
"""
Install a plugin from an identifier.
"""
# exception will be raised if the request failed
2024-09-20 21:50:44 +08:00
return self._request_with_plugin_daemon_response(
2024-09-20 21:35:19 +08:00
"POST",
2024-10-08 21:28:59 +08:00
f"plugin/{tenant_id}/management/install/identifier",
2024-09-20 21:50:44 +08:00
bool,
2024-09-20 21:35:19 +08:00
data={
"plugin_unique_identifier": identifier,
},
2024-10-08 21:28:59 +08:00
headers={"Content-Type": "application/json"},
2024-09-20 21:35:19 +08:00
)
2024-10-08 22:38:33 +08:00
def uninstall(self, tenant_id: str, plugin_installation_id: str) -> bool:
2024-09-20 21:50:44 +08:00
"""
Uninstall a plugin.
"""
return self._request_with_plugin_daemon_response(
2024-10-08 22:38:33 +08:00
"POST",
2024-10-08 21:28:59 +08:00
f"plugin/{tenant_id}/management/uninstall",
bool,
data={
2024-10-08 22:38:33 +08:00
"plugin_installation_id": plugin_installation_id,
2024-10-08 21:28:59 +08:00
},
headers={"Content-Type": "application/json"},
2024-09-20 21:50:44 +08:00
)