Merge fix/chore-fix into fix/chore-fix

This commit is contained in:
Yeuoly
2024-09-29 13:12:22 +08:00
parent 47c8824be6
commit 7b76b1ff82
3 changed files with 60 additions and 16 deletions

View File

@ -87,3 +87,22 @@ class PluginVoiceEntity(BaseModel):
class PluginVoicesResponse(BaseModel): class PluginVoicesResponse(BaseModel):
voices: list[PluginVoiceEntity] = Field(description="The result of the voices.") voices: list[PluginVoiceEntity] = Field(description="The result of the voices.")
class PluginDaemonError(BaseModel):
"""
Error from plugin daemon.
"""
error_type: str
message: str
args: Optional[dict] = None
class PluginDaemonInnerError(Exception):
code: int
message: str
def __init__(self, code: int, message: str):
self.code = code
self.message = message

View File

@ -1,13 +1,20 @@
import json import json
from collections.abc import Callable, Generator from collections.abc import Callable, Generator
from typing import TypeVar from typing import Optional, TypeVar
import requests import requests
from pydantic import BaseModel from pydantic import BaseModel
from yarl import URL from yarl import URL
from configs import dify_config from configs import dify_config
from core.plugin.entities.plugin_daemon import PluginDaemonBasicResponse from core.model_runtime.errors.invoke import (
InvokeAuthorizationError,
InvokeBadRequestError,
InvokeConnectionError,
InvokeRateLimitError,
InvokeServerUnavailableError,
)
from core.plugin.entities.plugin_daemon import PluginDaemonBasicResponse, PluginDaemonError, PluginDaemonInnerError
plugin_daemon_inner_api_baseurl = dify_config.PLUGIN_API_URL plugin_daemon_inner_api_baseurl = dify_config.PLUGIN_API_URL
plugin_daemon_inner_api_key = dify_config.PLUGIN_API_KEY plugin_daemon_inner_api_key = dify_config.PLUGIN_API_KEY
@ -110,6 +117,12 @@ class BasePluginManager:
rep = PluginDaemonBasicResponse[type](**json_response) rep = PluginDaemonBasicResponse[type](**json_response)
if rep.code != 0: if rep.code != 0:
if rep.code == -500:
try:
error = PluginDaemonError(**json.loads(rep.message))
self._handle_plugin_daemon_error(error.error_type, error.message, error.args)
except Exception as e:
raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}")
raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}") raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}")
if rep.data is None: if rep.data is None:
raise ValueError("got empty data from plugin daemon") raise ValueError("got empty data from plugin daemon")
@ -132,19 +145,34 @@ class BasePluginManager:
line_data = json.loads(line) line_data = json.loads(line)
rep = PluginDaemonBasicResponse[type](**line_data) rep = PluginDaemonBasicResponse[type](**line_data)
if rep.code != 0: if rep.code != 0:
raise PluginDaemonRespError(rep.message, rep.code) if rep.code == -500:
try:
error = PluginDaemonError(**json.loads(rep.message))
self._handle_plugin_daemon_error(error.error_type, error.message, error.args)
except Exception as e:
raise PluginDaemonInnerError(code=rep.code, message=rep.message)
raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}")
if rep.data is None: if rep.data is None:
raise ValueError("got empty data from plugin daemon") raise ValueError("got empty data from plugin daemon")
yield rep.data yield rep.data
def _handle_plugin_daemon_error(self, error_type: str, message: str, args: Optional[dict] = None):
"""
handle the error from plugin daemon
"""
args = args or {}
class PluginDaemonRespError(Exception): if error_type == PluginDaemonInnerError.__name__:
""" raise PluginDaemonInnerError(code=-500, message=message)
Plugin daemon response error. elif error_type == InvokeRateLimitError.__name__:
""" raise InvokeRateLimitError(description=args.get("description"))
elif error_type == InvokeAuthorizationError.__name__:
def __init__(self, resp_message: str, code: int): raise InvokeAuthorizationError(description=args.get("description"))
super().__init__() elif error_type == InvokeBadRequestError.__name__:
self.message = f"got error from plugin daemon: {resp_message}, code: {code}" raise InvokeBadRequestError(description=args.get("description"))
self.resp_message = resp_message elif error_type == InvokeConnectionError.__name__:
self.code = code raise InvokeConnectionError(description=args.get("description"))
elif error_type == InvokeServerUnavailableError.__name__:
raise InvokeServerUnavailableError(description=args.get("description"))
else:
raise ValueError(f"got unknown error from plugin daemon: {error_type}, message: {message}, args: {args}")

View File

@ -618,9 +618,6 @@ class ToolManager:
""" """
get api provider get api provider
""" """
"""
get tool provider
"""
provider_obj: ApiToolProvider | None = ( provider_obj: ApiToolProvider | None = (
db.session.query(ApiToolProvider) db.session.query(ApiToolProvider)
.filter( .filter(