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

197 lines
6.6 KiB
Python
Raw Normal View History

2024-10-14 17:52:29 +08:00
from collections.abc import Sequence
2024-09-20 21:35:19 +08:00
2024-10-16 13:03:50 +08:00
from pydantic import BaseModel
2024-11-12 19:50:56 +08:00
from core.plugin.entities.plugin import (
PluginDeclaration,
PluginEntity,
PluginInstallation,
PluginInstallationSource,
)
from core.plugin.entities.plugin_daemon import PluginInstallTask, PluginInstallTaskStartResponse, PluginUploadResponse
2024-09-20 21:35:19 +08:00
from core.plugin.manager.base import BasePluginManager
class PluginInstallationManager(BasePluginManager):
2024-10-14 17:52:29 +08:00
def fetch_plugin_by_identifier(
self,
tenant_id: str,
identifier: str,
) -> bool:
2024-09-20 21:35:19 +08:00
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-14 17:52:29 +08:00
def upload_pkg(
2024-10-10 16:35:36 +08:00
self,
tenant_id: str,
pkg: bytes,
verify_signature: bool = False,
) -> PluginUploadResponse:
2024-09-20 21:35:19 +08:00
"""
2024-10-14 17:52:29 +08:00
Upload a plugin package and return the plugin unique identifier.
2024-09-20 21:35:19 +08:00
"""
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-09-20 21:35:19 +08:00
2024-10-14 17:52:29 +08:00
return self._request_with_plugin_daemon_response(
2024-10-08 21:28:59 +08:00
"POST",
2024-10-14 17:52:29 +08:00
f"plugin/{tenant_id}/management/install/upload",
PluginUploadResponse,
2024-10-08 21:28:59 +08:00
files=body,
2024-10-10 16:35:36 +08:00
data=data,
2024-09-20 21:35:19 +08:00
)
2024-10-14 17:52:29 +08:00
def install_from_identifiers(
self, tenant_id: str, identifiers: Sequence[str], source: PluginInstallationSource, meta: dict
2024-10-16 13:03:50 +08:00
) -> PluginInstallTaskStartResponse:
2024-09-20 21:35:19 +08:00
"""
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-16 13:03:50 +08:00
f"plugin/{tenant_id}/management/install/identifiers",
PluginInstallTaskStartResponse,
2024-09-20 21:35:19 +08:00
data={
2024-10-14 17:52:29 +08:00
"plugin_unique_identifiers": identifiers,
"source": source,
"meta": meta,
2024-09-20 21:35:19 +08:00
},
2024-10-08 21:28:59 +08:00
headers={"Content-Type": "application/json"},
2024-09-20 21:35:19 +08:00
)
2024-10-16 14:02:05 +08:00
def fetch_plugin_installation_tasks(self, tenant_id: str, page: int, page_size: int) -> Sequence[PluginInstallTask]:
2024-10-14 17:52:29 +08:00
"""
Fetch plugin installation tasks.
"""
return self._request_with_plugin_daemon_response(
"GET",
f"plugin/{tenant_id}/management/install/tasks",
list[PluginInstallTask],
2024-10-16 14:02:05 +08:00
params={"page": page, "page_size": page_size},
2024-10-14 17:52:29 +08:00
)
def fetch_plugin_installation_task(self, tenant_id: str, task_id: str) -> PluginInstallTask:
"""
Fetch a plugin installation task.
"""
return self._request_with_plugin_daemon_response(
"GET",
f"plugin/{tenant_id}/management/install/tasks/{task_id}",
PluginInstallTask,
)
2024-10-16 14:47:48 +08:00
def delete_plugin_installation_task(self, tenant_id: str, task_id: str) -> bool:
"""
Delete a plugin installation task.
"""
return self._request_with_plugin_daemon_response(
"POST",
f"plugin/{tenant_id}/management/install/tasks/{task_id}/delete",
bool,
)
def delete_plugin_installation_task_item(self, tenant_id: str, task_id: str, identifier: str) -> bool:
"""
Delete a plugin installation task item.
"""
return self._request_with_plugin_daemon_response(
"POST",
f"plugin/{tenant_id}/management/install/tasks/{task_id}/delete/{identifier}",
bool,
)
2024-10-14 17:52:29 +08:00
def fetch_plugin_manifest(self, tenant_id: str, plugin_unique_identifier: str) -> PluginDeclaration:
"""
Fetch a plugin manifest.
"""
2024-10-16 13:03:50 +08:00
class PluginDeclarationResponse(BaseModel):
declaration: PluginDeclaration
2024-10-14 17:52:29 +08:00
return self._request_with_plugin_daemon_response(
"GET",
2024-10-16 13:03:50 +08:00
f"plugin/{tenant_id}/management/fetch/manifest",
PluginDeclarationResponse,
params={"plugin_unique_identifier": plugin_unique_identifier},
).declaration
2024-10-14 17:52:29 +08:00
2024-11-12 19:50:56 +08:00
def fetch_plugin_installation_by_ids(self, tenant_id: str, plugin_ids: Sequence[str]) -> list[PluginInstallation]:
"""
Fetch plugin installations by ids.
"""
return self._request_with_plugin_daemon_response(
"POST",
f"plugin/{tenant_id}/management/installation/fetch/batch",
list[PluginInstallation],
data={"plugin_ids": plugin_ids},
headers={"Content-Type": "application/json"},
)
def fetch_missing_dependencies(self, tenant_id: str, plugin_unique_identifiers: list[str]) -> list[str]:
"""
Fetch missing dependencies
"""
return self._request_with_plugin_daemon_response(
"POST",
f"plugin/{tenant_id}/management/installation/missing",
list[str],
data={"plugin_unique_identifiers": plugin_unique_identifiers},
headers={"Content-Type": "application/json"},
)
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
)
2024-10-25 18:56:38 +08:00
def upgrade_plugin(
self,
tenant_id: str,
original_plugin_unique_identifier: str,
new_plugin_unique_identifier: str,
source: PluginInstallationSource,
meta: dict,
) -> PluginInstallTaskStartResponse:
"""
Upgrade a plugin.
"""
return self._request_with_plugin_daemon_response(
"POST",
f"plugin/{tenant_id}/management/upgrade",
PluginInstallTaskStartResponse,
data={
"original_plugin_unique_identifier": original_plugin_unique_identifier,
"new_plugin_unique_identifier": new_plugin_unique_identifier,
"source": source,
"meta": meta,
},
headers={"Content-Type": "application/json"},
)