Skip to content

Commit

Permalink
Merge branch 'fix/chore-fix' into dev/plugin-deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Nov 22, 2024
2 parents f69d5ca + 200f9af commit dfc94cd
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 54 deletions.
5 changes: 5 additions & 0 deletions api/controllers/console/datasets/datasets_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from core.model_manager import ModelManager
from core.model_runtime.entities.model_entities import ModelType
from core.model_runtime.errors.invoke import InvokeAuthorizationError
from core.plugin.manager.exc import PluginNotFoundError
from core.rag.extractor.entity.extract_setting import ExtractSetting
from extensions.ext_database import db
from extensions.ext_redis import redis_client
Expand Down Expand Up @@ -415,6 +416,8 @@ def get(self, dataset_id, document_id):
)
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description)
except PluginNotFoundError as ex:
raise ProviderNotInitializeError(ex.description)
except Exception as e:
raise IndexingEstimateError(str(e))

Expand Down Expand Up @@ -516,6 +519,8 @@ def get(self, dataset_id, batch):
)
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description)
except PluginNotFoundError as ex:
raise ProviderNotInitializeError(ex.description)
except Exception as e:
raise IndexingEstimateError(str(e))
return response
Expand Down
147 changes: 104 additions & 43 deletions api/controllers/console/workspace/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from controllers.console.workspace import plugin_permission_required
from controllers.console.wraps import account_initialization_required, setup_required
from core.model_runtime.utils.encoders import jsonable_encoder
from core.plugin.manager.exc import PluginDaemonBadRequestError
from libs.login import login_required
from models.account import TenantPluginPermission
from services.plugin.plugin_permission_service import PluginPermissionService
Expand All @@ -24,11 +25,14 @@ class PluginDebuggingKeyApi(Resource):
def get(self):
tenant_id = current_user.current_tenant_id

return {
"key": PluginService.get_debugging_key(tenant_id),
"host": dify_config.PLUGIN_REMOTE_INSTALL_HOST,
"port": dify_config.PLUGIN_REMOTE_INSTALL_PORT,
}
try:
return {
"key": PluginService.get_debugging_key(tenant_id),
"host": dify_config.PLUGIN_REMOTE_INSTALL_HOST,
"port": dify_config.PLUGIN_REMOTE_INSTALL_PORT,
}
except PluginDaemonBadRequestError as e:
raise ValueError(e)


class PluginListApi(Resource):
Expand All @@ -37,7 +41,11 @@ class PluginListApi(Resource):
@account_initialization_required
def get(self):
tenant_id = current_user.current_tenant_id
plugins = PluginService.list(tenant_id)
try:
plugins = PluginService.list(tenant_id)
except PluginDaemonBadRequestError as e:
raise ValueError(e)

return jsonable_encoder({"plugins": plugins})


Expand All @@ -52,7 +60,11 @@ def post(self):
parser.add_argument("plugin_ids", type=list, required=True, location="json")
args = parser.parse_args()

plugins = PluginService.list_installations_from_ids(tenant_id, args["plugin_ids"])
try:
plugins = PluginService.list_installations_from_ids(tenant_id, args["plugin_ids"])
except PluginDaemonBadRequestError as e:
raise ValueError(e)

return jsonable_encoder({"plugins": plugins})


Expand All @@ -64,7 +76,10 @@ def get(self):
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"])
try:
icon_bytes, mimetype = PluginService.get_asset(args["tenant_id"], args["filename"])
except PluginDaemonBadRequestError as e:
raise ValueError(e)

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)
Expand All @@ -85,7 +100,10 @@ def post(self):
raise ValueError("File size exceeds the maximum allowed size")

content = file.read()
response = PluginService.upload_pkg(tenant_id, content)
try:
response = PluginService.upload_pkg(tenant_id, content)
except PluginDaemonBadRequestError as e:
raise ValueError(e)

return jsonable_encoder(response)

Expand All @@ -104,7 +122,10 @@ def post(self):
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"])
try:
response = PluginService.upload_pkg_from_github(tenant_id, args["repo"], args["version"], args["package"])
except PluginDaemonBadRequestError as e:
raise ValueError(e)

return jsonable_encoder(response)

Expand All @@ -124,7 +145,10 @@ def post(self):
raise ValueError("File size exceeds the maximum allowed size")

content = file.read()
response = PluginService.upload_bundle(tenant_id, content)
try:
response = PluginService.upload_bundle(tenant_id, content)
except PluginDaemonBadRequestError as e:
raise ValueError(e)

return jsonable_encoder(response)

Expand All @@ -146,7 +170,10 @@ def post(self):
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"])
try:
response = PluginService.install_from_local_pkg(tenant_id, args["plugin_unique_identifiers"])
except PluginDaemonBadRequestError as e:
raise ValueError(e)

return jsonable_encoder(response)

Expand All @@ -166,13 +193,16 @@ def post(self):
parser.add_argument("plugin_unique_identifier", type=str, required=True, location="json")
args = parser.parse_args()

response = PluginService.install_from_github(
tenant_id,
args["plugin_unique_identifier"],
args["repo"],
args["version"],
args["package"],
)
try:
response = PluginService.install_from_github(
tenant_id,
args["plugin_unique_identifier"],
args["repo"],
args["version"],
args["package"],
)
except PluginDaemonBadRequestError as e:
raise ValueError(e)

return jsonable_encoder(response)

Expand All @@ -194,7 +224,10 @@ def post(self):
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"])
try:
response = PluginService.install_from_marketplace_pkg(tenant_id, args["plugin_unique_identifiers"])
except PluginDaemonBadRequestError as e:
raise ValueError(e)

return jsonable_encoder(response)

Expand All @@ -211,9 +244,16 @@ def get(self):
parser.add_argument("plugin_unique_identifier", type=str, required=True, location="args")
args = parser.parse_args()

return jsonable_encoder(
{"manifest": PluginService.fetch_plugin_manifest(tenant_id, args["plugin_unique_identifier"]).model_dump()}
)
try:
return jsonable_encoder(
{
"manifest": PluginService.fetch_plugin_manifest(
tenant_id, args["plugin_unique_identifier"]
).model_dump()
}
)
except PluginDaemonBadRequestError as e:
raise ValueError(e)


class PluginFetchInstallTasksApi(Resource):
Expand All @@ -229,9 +269,12 @@ def get(self):
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"])}
)
try:
return jsonable_encoder(
{"tasks": PluginService.fetch_install_tasks(tenant_id, args["page"], args["page_size"])}
)
except PluginDaemonBadRequestError as e:
raise ValueError(e)


class PluginFetchInstallTaskApi(Resource):
Expand All @@ -242,7 +285,10 @@ class PluginFetchInstallTaskApi(Resource):
def get(self, task_id: str):
tenant_id = current_user.current_tenant_id

return jsonable_encoder({"task": PluginService.fetch_install_task(tenant_id, task_id)})
try:
return jsonable_encoder({"task": PluginService.fetch_install_task(tenant_id, task_id)})
except PluginDaemonBadRequestError as e:
raise ValueError(e)


class PluginDeleteInstallTaskApi(Resource):
Expand All @@ -253,7 +299,10 @@ class PluginDeleteInstallTaskApi(Resource):
def post(self, task_id: str):
tenant_id = current_user.current_tenant_id

return {"success": PluginService.delete_install_task(tenant_id, task_id)}
try:
return {"success": PluginService.delete_install_task(tenant_id, task_id)}
except PluginDaemonBadRequestError as e:
raise ValueError(e)


class PluginDeleteInstallTaskItemApi(Resource):
Expand All @@ -264,7 +313,10 @@ class PluginDeleteInstallTaskItemApi(Resource):
def post(self, task_id: str, identifier: str):
tenant_id = current_user.current_tenant_id

return {"success": PluginService.delete_install_task_item(tenant_id, task_id, identifier)}
try:
return {"success": PluginService.delete_install_task_item(tenant_id, task_id, identifier)}
except PluginDaemonBadRequestError as e:
raise ValueError(e)


class PluginUpgradeFromMarketplaceApi(Resource):
Expand All @@ -280,11 +332,14 @@ def post(self):
parser.add_argument("new_plugin_unique_identifier", type=str, required=True, location="json")
args = parser.parse_args()

return jsonable_encoder(
PluginService.upgrade_plugin_with_marketplace(
tenant_id, args["original_plugin_unique_identifier"], args["new_plugin_unique_identifier"]
try:
return jsonable_encoder(
PluginService.upgrade_plugin_with_marketplace(
tenant_id, args["original_plugin_unique_identifier"], args["new_plugin_unique_identifier"]
)
)
)
except PluginDaemonBadRequestError as e:
raise ValueError(e)


class PluginUpgradeFromGithubApi(Resource):
Expand All @@ -303,16 +358,19 @@ def post(self):
parser.add_argument("package", type=str, required=True, location="json")
args = parser.parse_args()

return jsonable_encoder(
PluginService.upgrade_plugin_with_github(
tenant_id,
args["original_plugin_unique_identifier"],
args["new_plugin_unique_identifier"],
args["repo"],
args["version"],
args["package"],
try:
return jsonable_encoder(
PluginService.upgrade_plugin_with_github(
tenant_id,
args["original_plugin_unique_identifier"],
args["new_plugin_unique_identifier"],
args["repo"],
args["version"],
args["package"],
)
)
)
except PluginDaemonBadRequestError as e:
raise ValueError(e)


class PluginUninstallApi(Resource):
Expand All @@ -327,7 +385,10 @@ def post(self):

tenant_id = current_user.current_tenant_id

return {"success": PluginService.uninstall(tenant_id, args["plugin_installation_id"])}
try:
return {"success": PluginService.uninstall(tenant_id, args["plugin_installation_id"])}
except PluginDaemonBadRequestError as e:
raise ValueError(e)


class PluginChangePermissionApi(Resource):
Expand Down
18 changes: 7 additions & 11 deletions api/core/plugin/manager/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,12 @@ def _request_with_plugin_daemon_response(

rep = PluginDaemonBasicResponse[type](**json_response)
if rep.code != 0:
if rep.code == -500:
try:
error = PluginDaemonError(**json.loads(rep.message))
except Exception as e:
raise ValueError(f"{rep.message}, code: {rep.code}")

self._handle_plugin_daemon_error(error.error_type, error.message, error.args)
raise ValueError(f"{rep.message}, code: {rep.code}")
try:
error = PluginDaemonError(**json.loads(rep.message))
except Exception as e:
raise ValueError(f"{rep.message}, code: {rep.code}")

self._handle_plugin_daemon_error(error.error_type, error.message, error.args)
if rep.data is None:
frame = inspect.currentframe()
raise ValueError(f"got empty data from plugin daemon: {frame.f_lineno if frame else 'unknown'}")
Expand Down Expand Up @@ -224,6 +222,4 @@ def _handle_plugin_daemon_error(self, error_type: str, message: str, args: Optio
case PluginPermissionDeniedError.__name__:
raise PluginPermissionDeniedError(description=message)
case _:
raise ValueError(
f"got unknown error from plugin daemon: {error_type}, message: {message}, args: {args}"
)
raise Exception(f"got unknown error from plugin daemon: {error_type}, message: {message}, args: {args}")
4 changes: 4 additions & 0 deletions api/core/plugin/manager/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class PluginDaemonError(Exception):
def __init__(self, description: str) -> None:
self.description = description

def __str__(self) -> str:
# returns the class name and description
return f"{self.__class__.__name__}: {self.description}"


class PluginDaemonInternalServerError(PluginDaemonError):
description: str = "Internal Server Error"
Expand Down

0 comments on commit dfc94cd

Please sign in to comment.