2024-09-20 13:55:09 +08:00
|
|
|
import json
|
2024-10-31 09:56:52 +08:00
|
|
|
import logging
|
2024-09-23 21:13:02 +08:00
|
|
|
from collections.abc import Callable, Generator
|
2024-09-29 13:12:22 +08:00
|
|
|
from typing import Optional, TypeVar
|
2024-09-20 13:55:09 +08:00
|
|
|
|
|
|
|
import requests
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from yarl import URL
|
|
|
|
|
|
|
|
from configs import dify_config
|
2024-09-29 13:12:22 +08:00
|
|
|
from core.model_runtime.errors.invoke import (
|
|
|
|
InvokeAuthorizationError,
|
|
|
|
InvokeBadRequestError,
|
|
|
|
InvokeConnectionError,
|
|
|
|
InvokeRateLimitError,
|
|
|
|
InvokeServerUnavailableError,
|
|
|
|
)
|
|
|
|
from core.plugin.entities.plugin_daemon import PluginDaemonBasicResponse, PluginDaemonError, PluginDaemonInnerError
|
2024-09-20 13:55:09 +08:00
|
|
|
|
|
|
|
plugin_daemon_inner_api_baseurl = dify_config.PLUGIN_API_URL
|
2024-09-20 15:08:39 +08:00
|
|
|
plugin_daemon_inner_api_key = dify_config.PLUGIN_API_KEY
|
2024-09-20 13:55:09 +08:00
|
|
|
|
2024-10-14 17:52:29 +08:00
|
|
|
T = TypeVar("T", bound=(BaseModel | dict | list | bool | str))
|
2024-09-20 13:55:09 +08:00
|
|
|
|
2024-10-31 09:56:52 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-09-20 13:55:09 +08:00
|
|
|
|
|
|
|
class BasePluginManager:
|
2024-09-20 14:43:01 +08:00
|
|
|
def _request(
|
2024-09-20 21:35:19 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
headers: dict | None = None,
|
2024-09-23 21:13:02 +08:00
|
|
|
data: bytes | dict | str | None = None,
|
2024-09-23 13:09:46 +08:00
|
|
|
params: dict | None = None,
|
2024-10-08 21:28:59 +08:00
|
|
|
files: dict | None = None,
|
2024-09-20 21:35:19 +08:00
|
|
|
stream: bool = False,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> requests.Response:
|
2024-09-20 13:55:09 +08:00
|
|
|
"""
|
|
|
|
Make a request to the plugin daemon inner API.
|
|
|
|
"""
|
|
|
|
url = URL(str(plugin_daemon_inner_api_baseurl)) / path
|
2024-09-20 14:43:01 +08:00
|
|
|
headers = headers or {}
|
2024-09-20 13:55:09 +08:00
|
|
|
headers["X-Api-Key"] = plugin_daemon_inner_api_key
|
2024-09-24 16:15:50 +08:00
|
|
|
headers["Accept-Encoding"] = "gzip, deflate, br"
|
2024-09-23 21:13:02 +08:00
|
|
|
|
|
|
|
if headers.get("Content-Type") == "application/json" and isinstance(data, dict):
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
2024-10-31 09:56:52 +08:00
|
|
|
try:
|
|
|
|
response = requests.request(
|
|
|
|
method=method, url=str(url), headers=headers, data=data, params=params, stream=stream, files=files
|
|
|
|
)
|
2024-11-21 13:53:08 +08:00
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
logger.exception("Request to Plugin Daemon Service failed")
|
2024-11-02 10:20:26 +08:00
|
|
|
raise PluginDaemonInnerError(code=-500, message="Request to Plugin Daemon Service failed")
|
2024-10-31 09:56:52 +08:00
|
|
|
|
2024-09-20 13:55:09 +08:00
|
|
|
return response
|
|
|
|
|
2024-09-20 14:43:01 +08:00
|
|
|
def _stream_request(
|
2024-09-23 13:09:46 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
params: dict | None = None,
|
|
|
|
headers: dict | None = None,
|
|
|
|
data: bytes | dict | None = None,
|
2024-10-08 21:28:59 +08:00
|
|
|
files: dict | None = None,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> Generator[bytes, None, None]:
|
2024-09-20 13:55:09 +08:00
|
|
|
"""
|
|
|
|
Make a stream request to the plugin daemon inner API
|
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
response = self._request(method, path, headers, data, params, files, stream=True)
|
2024-09-23 21:13:02 +08:00
|
|
|
for line in response.iter_lines():
|
|
|
|
line = line.decode("utf-8").strip()
|
|
|
|
if line.startswith("data:"):
|
|
|
|
line = line[5:].strip()
|
2024-09-24 16:02:01 +08:00
|
|
|
if line:
|
|
|
|
yield line
|
2024-09-20 13:55:09 +08:00
|
|
|
|
|
|
|
def _stream_request_with_model(
|
2024-09-20 14:43:01 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
type: type[T],
|
|
|
|
headers: dict | None = None,
|
2024-09-20 21:35:19 +08:00
|
|
|
data: bytes | dict | None = None,
|
2024-09-23 13:09:46 +08:00
|
|
|
params: dict | None = None,
|
2024-10-08 21:28:59 +08:00
|
|
|
files: dict | None = None,
|
2024-09-20 13:55:09 +08:00
|
|
|
) -> Generator[T, None, None]:
|
|
|
|
"""
|
|
|
|
Make a stream request to the plugin daemon inner API and yield the response as a model.
|
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
for line in self._stream_request(method, path, params, headers, data, files):
|
2024-09-20 13:55:09 +08:00
|
|
|
yield type(**json.loads(line))
|
|
|
|
|
2024-09-20 14:43:01 +08:00
|
|
|
def _request_with_model(
|
2024-09-23 13:09:46 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
type: type[T],
|
|
|
|
headers: dict | None = None,
|
|
|
|
data: bytes | None = None,
|
|
|
|
params: dict | None = None,
|
2024-10-08 21:28:59 +08:00
|
|
|
files: dict | None = None,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> T:
|
2024-09-20 13:55:09 +08:00
|
|
|
"""
|
|
|
|
Make a request to the plugin daemon inner API and return the response as a model.
|
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
response = self._request(method, path, headers, data, params, files)
|
2024-09-20 13:55:09 +08:00
|
|
|
return type(**response.json())
|
2024-09-20 14:43:01 +08:00
|
|
|
|
|
|
|
def _request_with_plugin_daemon_response(
|
2024-09-23 13:09:46 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
type: type[T],
|
|
|
|
headers: dict | None = None,
|
|
|
|
data: bytes | dict | None = None,
|
|
|
|
params: dict | None = None,
|
2024-10-08 21:28:59 +08:00
|
|
|
files: dict | None = None,
|
2024-09-23 21:13:02 +08:00
|
|
|
transformer: Callable[[dict], dict] | None = None,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> T:
|
|
|
|
"""
|
|
|
|
Make a request to the plugin daemon inner API and return the response as a model.
|
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
response = self._request(method, path, headers, data, params, files)
|
2024-09-23 18:06:16 +08:00
|
|
|
json_response = response.json()
|
2024-09-23 21:13:02 +08:00
|
|
|
if transformer:
|
|
|
|
json_response = transformer(json_response)
|
2024-09-23 18:06:16 +08:00
|
|
|
|
|
|
|
rep = PluginDaemonBasicResponse[type](**json_response)
|
2024-09-20 14:43:01 +08:00
|
|
|
if rep.code != 0:
|
2024-09-29 13:12:22 +08:00
|
|
|
if rep.code == -500:
|
|
|
|
try:
|
|
|
|
error = PluginDaemonError(**json.loads(rep.message))
|
|
|
|
except Exception as e:
|
2024-11-12 20:48:28 +08:00
|
|
|
raise ValueError(f"{rep.message}, code: {rep.code}")
|
2024-09-29 13:46:16 +08:00
|
|
|
|
|
|
|
self._handle_plugin_daemon_error(error.error_type, error.message, error.args)
|
2024-11-12 20:48:28 +08:00
|
|
|
raise ValueError(f"{rep.message}, code: {rep.code}")
|
2024-09-20 14:43:01 +08:00
|
|
|
if rep.data is None:
|
2024-09-20 15:08:39 +08:00
|
|
|
raise ValueError("got empty data from plugin daemon")
|
2024-09-20 21:35:19 +08:00
|
|
|
|
2024-09-20 14:43:01 +08:00
|
|
|
return rep.data
|
2024-09-20 21:35:19 +08:00
|
|
|
|
2024-09-20 14:43:01 +08:00
|
|
|
def _request_with_plugin_daemon_response_stream(
|
2024-09-23 13:09:46 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
type: type[T],
|
|
|
|
headers: dict | None = None,
|
|
|
|
data: bytes | dict | None = None,
|
|
|
|
params: dict | None = None,
|
2024-10-08 21:28:59 +08:00
|
|
|
files: dict | None = None,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> Generator[T, None, None]:
|
|
|
|
"""
|
|
|
|
Make a stream request to the plugin daemon inner API and yield the response as a model.
|
|
|
|
"""
|
2024-10-08 21:28:59 +08:00
|
|
|
for line in self._stream_request(method, path, params, headers, data, files):
|
2024-11-02 10:20:26 +08:00
|
|
|
line_data = None
|
|
|
|
try:
|
|
|
|
line_data = json.loads(line)
|
|
|
|
rep = PluginDaemonBasicResponse[type](**line_data)
|
|
|
|
except Exception as e:
|
|
|
|
# TODO modify this when line_data has code and message
|
|
|
|
if line_data and "error" in line_data:
|
|
|
|
raise ValueError(line_data["error"])
|
|
|
|
else:
|
|
|
|
raise ValueError(line)
|
|
|
|
|
2024-09-20 14:43:01 +08:00
|
|
|
if rep.code != 0:
|
2024-09-29 13:12:22 +08:00
|
|
|
if rep.code == -500:
|
|
|
|
try:
|
|
|
|
error = PluginDaemonError(**json.loads(rep.message))
|
|
|
|
except Exception as e:
|
|
|
|
raise PluginDaemonInnerError(code=rep.code, message=rep.message)
|
2024-09-29 13:46:16 +08:00
|
|
|
|
|
|
|
self._handle_plugin_daemon_error(error.error_type, error.message, error.args)
|
2024-11-12 20:48:28 +08:00
|
|
|
raise ValueError(f"plugin daemon: {rep.message}, code: {rep.code}")
|
2024-09-20 14:43:01 +08:00
|
|
|
if rep.data is None:
|
2024-09-20 21:35:19 +08:00
|
|
|
raise ValueError("got empty data from plugin daemon")
|
|
|
|
yield rep.data
|
2024-09-29 00:13:53 +08:00
|
|
|
|
2024-09-29 13:12:22 +08:00
|
|
|
def _handle_plugin_daemon_error(self, error_type: str, message: str, args: Optional[dict] = None):
|
|
|
|
"""
|
|
|
|
handle the error from plugin daemon
|
|
|
|
"""
|
|
|
|
args = args or {}
|
|
|
|
|
|
|
|
if error_type == PluginDaemonInnerError.__name__:
|
|
|
|
raise PluginDaemonInnerError(code=-500, message=message)
|
|
|
|
elif error_type == InvokeRateLimitError.__name__:
|
|
|
|
raise InvokeRateLimitError(description=args.get("description"))
|
|
|
|
elif error_type == InvokeAuthorizationError.__name__:
|
|
|
|
raise InvokeAuthorizationError(description=args.get("description"))
|
|
|
|
elif error_type == InvokeBadRequestError.__name__:
|
|
|
|
raise InvokeBadRequestError(description=args.get("description"))
|
|
|
|
elif error_type == InvokeConnectionError.__name__:
|
|
|
|
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}")
|