dify/api/controllers/console/workspace/plugin.py

281 lines
9.2 KiB
Python
Raw Normal View History

2024-10-08 21:28:59 +08:00
import io
2024-10-14 17:52:29 +08:00
from flask import request, send_file
2024-09-20 15:08:39 +08:00
from flask_login import current_user
2024-10-08 21:28:59 +08:00
from flask_restful import Resource, reqparse
2024-09-20 15:08:39 +08:00
from werkzeug.exceptions import Forbidden
2024-10-08 21:28:59 +08:00
from configs import dify_config
2024-09-20 15:08:39 +08:00
from controllers.console import api
from controllers.console.setup import setup_required
from controllers.console.wraps import account_initialization_required
2024-10-08 21:28:59 +08:00
from core.model_runtime.utils.encoders import jsonable_encoder
2024-09-20 15:08:39 +08:00
from libs.login import login_required
2024-10-08 21:28:59 +08:00
from services.plugin.plugin_service import PluginService
2024-09-20 15:08:39 +08:00
class PluginDebuggingKeyApi(Resource):
@setup_required
@login_required
@account_initialization_required
def get(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
2024-09-20 15:08:39 +08:00
tenant_id = user.current_tenant_id
2024-10-08 21:28:59 +08:00
return {
2024-10-10 16:35:36 +08:00
"key": PluginService.get_debugging_key(tenant_id),
2024-10-08 21:28:59 +08:00
"host": dify_config.PLUGIN_REMOTE_INSTALL_HOST,
"port": dify_config.PLUGIN_REMOTE_INSTALL_PORT,
}
class PluginListApi(Resource):
@setup_required
@login_required
@account_initialization_required
def get(self):
user = current_user
tenant_id = user.current_tenant_id
2024-10-10 16:35:36 +08:00
plugins = PluginService.list(tenant_id)
2024-10-08 21:28:59 +08:00
return jsonable_encoder({"plugins": plugins})
class PluginIconApi(Resource):
@setup_required
def get(self):
req = reqparse.RequestParser()
req.add_argument("tenant_id", type=str, required=True, location="args")
req.add_argument("filename", type=str, required=True, location="args")
args = req.parse_args()
icon_bytes, mimetype = PluginService.get_asset(args["tenant_id"], args["filename"])
icon_cache_max_age = dify_config.TOOL_ICON_CACHE_MAX_AGE
return send_file(io.BytesIO(icon_bytes), mimetype=mimetype, max_age=icon_cache_max_age)
2024-10-14 17:52:29 +08:00
class PluginUploadPkgApi(Resource):
2024-10-08 21:28:59 +08:00
@setup_required
@login_required
@account_initialization_required
2024-10-14 17:52:29 +08:00
def post(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
file = request.files["pkg"]
content = file.read()
return {"plugin_unique_identifier": PluginService.upload_pkg(tenant_id, content)}
2024-10-08 21:28:59 +08:00
2024-10-14 17:52:29 +08:00
class PluginUploadFromPkgApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self):
2024-10-08 21:28:59 +08:00
user = current_user
2024-10-14 17:52:29 +08:00
if not user.is_admin_or_owner:
raise Forbidden()
2024-10-08 21:28:59 +08:00
tenant_id = user.current_tenant_id
2024-10-14 17:52:29 +08:00
file = request.files["pkg"]
content = file.read()
response = PluginService.upload_pkg(tenant_id, content)
return {
"plugin_unique_identifier": response,
}
2024-10-08 21:28:59 +08:00
2024-10-14 17:52:29 +08:00
class PluginUploadFromGithubApi(Resource):
2024-10-08 21:28:59 +08:00
@setup_required
@login_required
@account_initialization_required
def post(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
2024-10-14 17:52:29 +08:00
parser = reqparse.RequestParser()
parser.add_argument("repo", type=str, required=True, location="json")
parser.add_argument("version", type=str, required=True, location="json")
parser.add_argument("package", type=str, required=True, location="json")
args = parser.parse_args()
response = PluginService.upload_pkg_from_github(tenant_id, args["repo"], args["version"], args["package"])
return {
"plugin_unique_identifier": response,
}
2024-10-08 21:28:59 +08:00
class PluginInstallFromPkgApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
2024-10-14 17:52:29 +08:00
parser = reqparse.RequestParser()
2024-10-16 14:02:05 +08:00
parser.add_argument("plugin_unique_identifiers", type=list, required=True, location="json")
2024-10-14 17:52:29 +08:00
args = parser.parse_args()
2024-10-08 21:28:59 +08:00
2024-10-16 14:02:05 +08:00
# check if all plugin_unique_identifiers are valid string
for plugin_unique_identifier in args["plugin_unique_identifiers"]:
if not isinstance(plugin_unique_identifier, str):
raise ValueError("Invalid plugin unique identifier")
response = PluginService.install_from_local_pkg(tenant_id, args["plugin_unique_identifiers"])
2024-10-10 16:35:36 +08:00
2024-10-16 13:03:50 +08:00
return response.model_dump()
2024-10-10 16:35:36 +08:00
class PluginInstallFromGithubApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
parser = reqparse.RequestParser()
parser.add_argument("repo", type=str, required=True, location="json")
parser.add_argument("version", type=str, required=True, location="json")
parser.add_argument("package", type=str, required=True, location="json")
2024-10-14 17:52:29 +08:00
parser.add_argument("plugin_unique_identifier", type=str, required=True, location="json")
2024-10-10 16:35:36 +08:00
args = parser.parse_args()
2024-10-14 17:52:29 +08:00
response = PluginService.install_from_github(
2024-10-16 14:02:05 +08:00
tenant_id,
args["plugin_unique_identifier"],
args["repo"],
args["version"],
args["package"],
2024-10-14 17:52:29 +08:00
)
2024-10-10 16:35:36 +08:00
2024-10-16 13:03:50 +08:00
return response.model_dump()
2024-10-10 16:35:36 +08:00
class PluginInstallFromMarketplaceApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
parser = reqparse.RequestParser()
2024-10-16 14:02:05 +08:00
parser.add_argument("plugin_unique_identifiers", type=list, required=True, location="json")
2024-10-10 16:35:36 +08:00
args = parser.parse_args()
2024-10-16 14:02:05 +08:00
# check if all plugin_unique_identifiers are valid string
for plugin_unique_identifier in args["plugin_unique_identifiers"]:
if not isinstance(plugin_unique_identifier, str):
raise ValueError("Invalid plugin unique identifier")
response = PluginService.install_from_marketplace_pkg(tenant_id, args["plugin_unique_identifiers"])
2024-10-14 17:52:29 +08:00
2024-10-16 13:03:50 +08:00
return response.model_dump()
2024-10-14 17:52:29 +08:00
class PluginFetchManifestApi(Resource):
@setup_required
@login_required
@account_initialization_required
def get(self):
user = current_user
parser = reqparse.RequestParser()
parser.add_argument("plugin_unique_identifier", type=str, required=True, location="args")
args = parser.parse_args()
tenant_id = user.current_tenant_id
2024-10-16 13:03:50 +08:00
return jsonable_encoder(
{"manifest": PluginService.fetch_plugin_manifest(tenant_id, args["plugin_unique_identifier"]).model_dump()}
)
2024-10-14 17:52:29 +08:00
class PluginFetchInstallTasksApi(Resource):
@setup_required
@login_required
@account_initialization_required
def get(self):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
2024-10-16 14:02:05 +08:00
parser = reqparse.RequestParser()
parser.add_argument("page", type=int, required=True, location="args")
parser.add_argument("page_size", type=int, required=True, location="args")
args = parser.parse_args()
return jsonable_encoder(
{"tasks": PluginService.fetch_install_tasks(tenant_id, args["page"], args["page_size"])}
)
2024-10-14 17:52:29 +08:00
class PluginFetchInstallTaskApi(Resource):
@setup_required
@login_required
@account_initialization_required
def get(self, task_id: str):
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
2024-10-08 21:28:59 +08:00
2024-10-16 14:02:05 +08:00
return jsonable_encoder({"task": PluginService.fetch_install_task(tenant_id, task_id)})
2024-09-20 15:08:39 +08:00
2024-10-08 22:38:33 +08:00
class PluginUninstallApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self):
req = reqparse.RequestParser()
req.add_argument("plugin_installation_id", type=str, required=True, location="json")
args = req.parse_args()
user = current_user
if not user.is_admin_or_owner:
raise Forbidden()
tenant_id = user.current_tenant_id
2024-10-10 16:35:36 +08:00
return {"success": PluginService.uninstall(tenant_id, args["plugin_installation_id"])}
2024-10-08 22:38:33 +08:00
api.add_resource(PluginDebuggingKeyApi, "/workspaces/current/plugin/debugging-key")
2024-10-08 21:28:59 +08:00
api.add_resource(PluginListApi, "/workspaces/current/plugin/list")
api.add_resource(PluginIconApi, "/workspaces/current/plugin/icon")
2024-10-14 17:52:29 +08:00
api.add_resource(PluginUploadFromPkgApi, "/workspaces/current/plugin/upload/pkg")
api.add_resource(PluginUploadFromGithubApi, "/workspaces/current/plugin/upload/github")
api.add_resource(PluginInstallFromPkgApi, "/workspaces/current/plugin/install/pkg")
api.add_resource(PluginInstallFromGithubApi, "/workspaces/current/plugin/install/github")
api.add_resource(PluginInstallFromMarketplaceApi, "/workspaces/current/plugin/install/marketplace")
api.add_resource(PluginFetchManifestApi, "/workspaces/current/plugin/fetch-manifest")
api.add_resource(PluginFetchInstallTasksApi, "/workspaces/current/plugin/tasks")
api.add_resource(PluginFetchInstallTaskApi, "/workspaces/current/plugin/tasks/<task_id>")
2024-10-08 22:38:33 +08:00
api.add_resource(PluginUninstallApi, "/workspaces/current/plugin/uninstall")