Files

466 lines
20 KiB
Python
Raw Permalink Normal View History

+109
2025-02-17 17:05:13 +08:00
import inspect
import json
import logging
from collections.abc import Callable, Generator, Mapping
from contextlib import contextmanager
from contextvars import ContextVar
from typing import Any, cast
from urllib.parse import unquote
+109
2025-02-17 17:05:13 +08:00
2025-10-11 00:41:16 +09:00
import httpx
+109
2025-02-17 17:05:13 +08:00
from pydantic import BaseModel
from yarl import URL
from configs import dify_config
from core.helper.http_client_pooling import get_pooled_http_client
from core.plugin.endpoint.exc import EndpointSetupFailedError
from core.plugin.entities.plugin_daemon import (
PluginDaemonBasicResponse,
PluginDaemonError,
PluginDaemonInnerError,
PluginListResponse,
)
from core.plugin.impl.exc import (
+109
2025-02-17 17:05:13 +08:00
PluginDaemonBadRequestError,
PluginDaemonClientSideError,
+109
2025-02-17 17:05:13 +08:00
PluginDaemonInternalServerError,
PluginDaemonNotFoundError,
PluginDaemonUnauthorizedError,
PluginInvokeError,
2026-06-18 07:34:33 +08:00
PluginLLMPollingUnsupportedError,
+109
2025-02-17 17:05:13 +08:00
PluginNotFoundError,
PluginPermissionDeniedError,
PluginRuntimeError,
+109
2025-02-17 17:05:13 +08:00
PluginUniqueIdentifierError,
)
+3
2025-11-12 17:59:37 +08:00
from core.trigger.errors import (
EventIgnoreError,
TriggerInvokeError,
TriggerPluginInvokeError,
TriggerProviderCredentialValidationError,
)
2026-04-16 16:50:02 +08:00
from graphon.model_runtime.errors.invoke import (
InvokeAuthorizationError,
InvokeBadRequestError,
InvokeConnectionError,
InvokeRateLimitError,
InvokeServerUnavailableError,
)
from graphon.model_runtime.errors.validate import CredentialsValidateFailedError
+109
2025-02-17 17:05:13 +08:00
plugin_daemon_inner_api_baseurl = URL(str(dify_config.PLUGIN_DAEMON_URL))
_plugin_daemon_timeout_config = cast(
float | httpx.Timeout | None,
getattr(dify_config, "PLUGIN_DAEMON_TIMEOUT", 600.0),
)
plugin_daemon_request_timeout: httpx.Timeout | None
match _plugin_daemon_timeout_config:
case None:
plugin_daemon_request_timeout = None
case httpx.Timeout():
plugin_daemon_request_timeout = _plugin_daemon_timeout_config
case _:
plugin_daemon_request_timeout = httpx.Timeout(_plugin_daemon_timeout_config)
+109
2025-02-17 17:05:13 +08:00
_plugin_daemon_request_timeout_override: ContextVar[httpx.Timeout | None] = ContextVar(
"plugin_daemon_request_timeout_override",
default=None,
)
+109
2025-02-17 17:05:13 +08:00
logger = logging.getLogger(__name__)
PLUGIN_DAEMON_MAX_PATH_LENGTH = 4096
PLUGIN_DAEMON_MAX_PATH_DECODE_DEPTH = 8
_httpx_client: httpx.Client = get_pooled_http_client(
"plugin_daemon",
lambda: httpx.Client(limits=httpx.Limits(max_keepalive_connections=50, max_connections=100), trust_env=False),
)
+109
2025-02-17 17:05:13 +08:00
@contextmanager
def use_plugin_daemon_request_timeout(timeout_seconds: float) -> Generator[None]:
"""Temporarily shorten plugin-daemon requests made in the current context."""
if timeout_seconds <= 0:
raise ValueError("timeout_seconds must be greater than zero")
token = _plugin_daemon_request_timeout_override.set(httpx.Timeout(timeout_seconds))
try:
yield
finally:
_plugin_daemon_request_timeout_override.reset(token)
def _get_plugin_daemon_request_timeout() -> httpx.Timeout | None:
return _plugin_daemon_request_timeout_override.get() or plugin_daemon_request_timeout
def _normalize_plugin_daemon_response_for_type(json_response: Any, type_: type[object]) -> Any:
if type_ is not PluginListResponse:
return json_response
if isinstance(json_response, list):
return {
"code": 0,
"message": "",
"data": {"list": json_response, "total": len(json_response)},
}
if isinstance(json_response, dict):
data = json_response.get("data")
if isinstance(data, list):
return {
**json_response,
"data": {"list": data, "total": len(data)},
}
return json_response
class BasePluginClient:
def __init__(self, *, plugin_unique_identifier: str | None = None) -> None:
if plugin_unique_identifier == "":
raise ValueError("Plugin unique identifier must not be empty")
self._plugin_unique_identifier = plugin_unique_identifier
+109
2025-02-17 17:05:13 +08:00
def _request(
self,
method: str,
path: str,
+3
2025-11-12 17:59:37 +08:00
headers: dict[str, str] | None = None,
data: bytes | dict[str, Any] | str | None = None,
params: dict[str, Any] | None = None,
files: dict[str, Any] | None = None,
2025-10-11 00:41:16 +09:00
) -> httpx.Response:
+109
2025-02-17 17:05:13 +08:00
"""
Make a request to the plugin daemon inner API.
"""
2025-10-11 00:41:16 +09:00
url, headers, prepared_data, params, files = self._prepare_request(path, headers, data, params, files)
+109
2025-02-17 17:05:13 +08:00
2025-10-11 00:41:16 +09:00
request_kwargs: dict[str, Any] = {
"method": method,
"url": url,
"headers": headers,
"params": params,
"files": files,
"timeout": _get_plugin_daemon_request_timeout(),
2025-10-11 00:41:16 +09:00
}
if isinstance(prepared_data, dict):
request_kwargs["data"] = prepared_data
elif prepared_data is not None:
request_kwargs["content"] = prepared_data
+109
2025-02-17 17:05:13 +08:00
try:
response = _httpx_client.request(**request_kwargs)
2026-10-19 12:47:53 +00:00
except httpx.RequestError as caught_error:
+109
2025-02-17 17:05:13 +08:00
logger.exception("Request to Plugin Daemon Service failed")
2026-10-19 12:47:53 +00:00
raise PluginDaemonInnerError(code=-500, message="Request to Plugin Daemon Service failed") from caught_error
+109
2025-02-17 17:05:13 +08:00
return response
2025-10-11 00:41:16 +09:00
def _prepare_request(
self,
path: str,
+3
2025-11-12 17:59:37 +08:00
headers: dict[str, str] | None,
data: bytes | dict[str, Any] | str | None,
params: dict[str, Any] | None,
files: dict[str, Any] | None,
) -> tuple[str, dict[str, str], bytes | dict[str, Any] | str | None, dict[str, Any] | None, dict[str, Any] | None]:
if len(path) > PLUGIN_DAEMON_MAX_PATH_LENGTH:
raise ValueError(f"Invalid plugin daemon path: path length exceeds {PLUGIN_DAEMON_MAX_PATH_LENGTH}")
decoded_path = path
for _ in range(PLUGIN_DAEMON_MAX_PATH_DECODE_DEPTH):
next_decoded_path = unquote(decoded_path)
if next_decoded_path == decoded_path:
break
decoded_path = next_decoded_path
else:
raise ValueError("Invalid plugin daemon path: path is too deeply encoded")
if any(seg == ".." for seg in decoded_path.split("/")):
raise ValueError(f"Invalid plugin daemon path: traversal sequence detected in {path!r}")
2025-10-11 00:41:16 +09:00
url = plugin_daemon_inner_api_baseurl / path
prepared_headers = dict(headers or {})
prepared_headers["X-Api-Key"] = dify_config.PLUGIN_DAEMON_KEY
prepared_headers.setdefault("Accept-Encoding", "gzip, deflate, br")
from services.plugin.dispatch_identity import prepare_dispatch_identity
prepare_dispatch_identity(path, prepared_headers, self._plugin_unique_identifier)
# Inject traceparent header for distributed tracing
self._inject_trace_headers(prepared_headers)
+3
2025-11-12 17:59:37 +08:00
prepared_data: bytes | dict[str, Any] | str | None = (
2025-10-11 00:41:16 +09:00
data if isinstance(data, (bytes, str, dict)) or data is None else None
)
if isinstance(data, dict):
2026-10-15 09:45:52 +00:00
prepared_data = json.dumps(data) if prepared_headers.get("Content-Type") == "application/json" else data
2025-10-11 00:41:16 +09:00
return str(url), prepared_headers, prepared_data, params, files
def _inject_trace_headers(self, headers: dict[str, str]) -> None:
"""
Inject W3C traceparent header for distributed tracing.
This ensures trace context is propagated to plugin daemon even if
HTTPXClientInstrumentor doesn't cover module-level httpx functions.
"""
if not dify_config.ENABLE_OTEL:
return
import contextlib
# Skip if already present (case-insensitive check)
for key in headers:
if key.lower() == "traceparent":
return
# Inject traceparent - works as fallback when OTEL instrumentation doesn't cover this call
with contextlib.suppress(Exception):
from core.helper.trace_id_helper import generate_traceparent_header
traceparent = generate_traceparent_header()
if traceparent:
headers["traceparent"] = traceparent
+109
2025-02-17 17:05:13 +08:00
def _stream_request(
self,
method: str,
path: str,
+3
2025-11-12 17:59:37 +08:00
params: dict[str, Any] | None = None,
headers: dict[str, str] | None = None,
data: bytes | dict[str, Any] | None = None,
files: dict[str, Any] | None = None,
) -> Generator[str]:
+109
2025-02-17 17:05:13 +08:00
"""
Make a stream request to the plugin daemon inner API
"""
2025-10-11 00:41:16 +09:00
url, headers, prepared_data, params, files = self._prepare_request(path, headers, data, params, files)
stream_kwargs: dict[str, Any] = {
"method": method,
"url": url,
"headers": headers,
"params": params,
"files": files,
"timeout": _get_plugin_daemon_request_timeout(),
2025-10-11 00:41:16 +09:00
}
if isinstance(prepared_data, dict):
stream_kwargs["data"] = prepared_data
elif prepared_data is not None:
stream_kwargs["content"] = prepared_data
try:
with _httpx_client.stream(**stream_kwargs) as response:
2025-10-11 00:41:16 +09:00
for raw_line in response.iter_lines():
+3
2025-11-12 17:59:37 +08:00
if not raw_line:
2025-10-11 00:41:16 +09:00
continue
line = raw_line.decode("utf-8") if isinstance(raw_line, bytes) else raw_line
line = line.strip()
if line.startswith("data:"):
line = line[5:].strip()
if line:
yield line
2026-10-19 12:47:53 +00:00
except httpx.RequestError as caught_error:
2025-10-11 00:41:16 +09:00
logger.exception("Stream request to Plugin Daemon Service failed")
2026-10-19 12:47:53 +00:00
raise PluginDaemonInnerError(code=-500, message="Request to Plugin Daemon Service failed") from caught_error
+109
2025-02-17 17:05:13 +08:00
def _stream_request_with_model[T: BaseModel | dict[str, Any] | list[Any] | bool | str](
+109
2025-02-17 17:05:13 +08:00
self,
method: str,
path: str,
2025-10-10 17:30:13 +09:00
type_: type[T],
+3
2025-11-12 17:59:37 +08:00
headers: dict[str, str] | None = None,
data: bytes | dict[str, Any] | None = None,
params: dict[str, Any] | None = None,
files: dict[str, Any] | None = None,
) -> Generator[T]:
+109
2025-02-17 17:05:13 +08:00
"""
Make a stream request to the plugin daemon inner API and yield the response as a model.
"""
for line in self._stream_request(method, path, params, headers, data, files):
2025-10-10 17:30:13 +09:00
yield type_(**json.loads(line)) # type: ignore
+109
2025-02-17 17:05:13 +08:00
def _request_with_model[T: BaseModel | dict[str, Any] | list[Any] | bool | str](
+109
2025-02-17 17:05:13 +08:00
self,
method: str,
path: str,
2025-10-10 17:30:13 +09:00
type_: type[T],
+3
2025-11-12 17:59:37 +08:00
headers: dict[str, str] | None = None,
+109
2025-02-17 17:05:13 +08:00
data: bytes | None = None,
+3
2025-11-12 17:59:37 +08:00
params: dict[str, Any] | None = None,
files: dict[str, Any] | None = None,
+109
2025-02-17 17:05:13 +08:00
) -> T:
"""
Make a request to the plugin daemon inner API and return the response as a model.
"""
response = self._request(method, path, headers, data, params, files)
2025-10-21 12:26:58 +09:00
return type_(**response.json()) # type: ignore[return-value]
+109
2025-02-17 17:05:13 +08:00
def _request_with_plugin_daemon_response[T: BaseModel | dict[str, Any] | list[Any] | bool | str](
+109
2025-02-17 17:05:13 +08:00
self,
method: str,
path: str,
2025-10-10 17:30:13 +09:00
type_: type[T],
+3
2025-11-12 17:59:37 +08:00
headers: dict[str, str] | None = None,
data: bytes | dict[str, Any] | None = None,
params: dict[str, Any] | None = None,
files: dict[str, Any] | None = None,
transformer: Callable[[dict[str, Any]], dict[str, Any]] | None = None,
+109
2025-02-17 17:05:13 +08:00
) -> T:
"""
Make a request to the plugin daemon inner API and return the response as a model.
"""
try:
response = self._request(method, path, headers, data, params, files)
response.raise_for_status()
2025-10-11 00:41:16 +09:00
except httpx.HTTPStatusError as e:
2025-10-10 17:30:13 +09:00
logger.exception("Failed to request plugin daemon, status: %s, url: %s", e.response.status_code, path)
if e.response.status_code < 500:
2026-10-19 12:47:53 +00:00
raise PluginDaemonClientSideError(description=str(e)) from e
else:
2026-10-19 12:47:53 +00:00
raise PluginDaemonInternalServerError(description=str(e)) from e
except Exception as e:
msg = f"Failed to request plugin daemon, url: {path}"
2025-10-10 17:30:13 +09:00
logger.exception("Failed to request plugin daemon, url: %s", path)
raise ValueError(msg) from e
try:
json_response = response.json()
if transformer:
json_response = transformer(json_response)
json_response = _normalize_plugin_daemon_response_for_type(json_response, type_)
2025-10-10 17:30:13 +09:00
# https://stackoverflow.com/questions/59634937/variable-foo-class-is-not-valid-as-type-but-why
rep = PluginDaemonBasicResponse[type_].model_validate(json_response) # type: ignore
except Exception as e:
msg = (
2025-10-10 17:30:13 +09:00
f"Failed to parse response from plugin daemon to PluginDaemonBasicResponse [{str(type_.__name__)}],"
f" url: {path}"
)
logger.exception(msg)
raise ValueError(msg) from e
+109
2025-02-17 17:05:13 +08:00
if rep.code != 0:
try:
2025-10-10 17:30:13 +09:00
error = PluginDaemonError.model_validate(json.loads(rep.message))
except Exception as e:
raise ValueError(f"{rep.message}, code: {rep.code}") from e
+109
2025-02-17 17:05:13 +08:00
self._handle_plugin_daemon_error(error.error_type, error.message)
if rep.data is None:
frame = inspect.currentframe()
raise ValueError(f"got empty data from plugin daemon: {frame.f_lineno if frame else 'unknown'}")
return rep.data
def _request_with_plugin_daemon_response_stream[T: BaseModel | dict[str, Any] | list[Any] | bool | str](
+109
2025-02-17 17:05:13 +08:00
self,
method: str,
path: str,
2025-10-10 17:30:13 +09:00
type_: type[T],
+3
2025-11-12 17:59:37 +08:00
headers: dict[str, str] | None = None,
data: bytes | dict[str, Any] | None = None,
params: dict[str, Any] | None = None,
files: dict[str, Any] | None = None,
) -> Generator[T]:
+109
2025-02-17 17:05:13 +08:00
"""
Make a stream request to the plugin daemon inner API and yield the response as a model.
"""
for line in self._stream_request(method, path, params, headers, data, files):
try:
2025-10-10 17:30:13 +09:00
rep = PluginDaemonBasicResponse[type_].model_validate_json(line) # type: ignore
2026-10-19 12:47:53 +00:00
except (ValueError, TypeError) as caught_error:
+109
2025-02-17 17:05:13 +08:00
# TODO modify this when line_data has code and message
try:
line_data = json.loads(line)
2026-10-19 12:47:53 +00:00
except (ValueError, TypeError) as caught_error_inner:
raise ValueError(line) from caught_error_inner
# If the dictionary contains the `error` key, use its value as the argument
# for `ValueError`.
# Otherwise, use the `line` to provide better contextual information about the error.
2026-10-19 12:47:53 +00:00
raise ValueError(line_data.get("error", line)) from caught_error
+109
2025-02-17 17:05:13 +08:00
if rep.code != 0:
if rep.code == -500:
try:
2025-10-10 17:30:13 +09:00
error = PluginDaemonError.model_validate(json.loads(rep.message))
except Exception as e:
raise PluginDaemonInnerError(code=rep.code, message=rep.message) from e
+109
2025-02-17 17:05:13 +08:00
2025-10-13 11:04:19 +09:00
logger.error("Error in stream response for plugin %s", rep.__dict__)
+109
2025-02-17 17:05:13 +08:00
self._handle_plugin_daemon_error(error.error_type, error.message)
raise ValueError(f"plugin daemon: {rep.message}, code: {rep.code}")
if rep.data is None:
frame = inspect.currentframe()
raise ValueError(f"got empty data from plugin daemon: {frame.f_lineno if frame else 'unknown'}")
yield rep.data
def _handle_plugin_daemon_error(self, error_type: str, message: str):
"""
handle the error from plugin daemon
"""
match error_type:
case PluginDaemonInnerError.__name__:
raise PluginDaemonInnerError(code=-500, message=message)
case PluginInvokeError.__name__:
error_object = json.loads(message)
invoke_error_type = error_object.get("error_type")
match invoke_error_type:
case InvokeRateLimitError.__name__:
raise InvokeRateLimitError(description=error_object.get("message"))
+109
2025-02-17 17:05:13 +08:00
case InvokeAuthorizationError.__name__:
raise InvokeAuthorizationError(description=error_object.get("message"))
+109
2025-02-17 17:05:13 +08:00
case InvokeBadRequestError.__name__:
raise InvokeBadRequestError(description=error_object.get("message"))
+109
2025-02-17 17:05:13 +08:00
case InvokeConnectionError.__name__:
raise InvokeConnectionError(description=error_object.get("message"))
+109
2025-02-17 17:05:13 +08:00
case InvokeServerUnavailableError.__name__:
raise InvokeServerUnavailableError(description=error_object.get("message"))
+109
2025-02-17 17:05:13 +08:00
case CredentialsValidateFailedError.__name__:
raise CredentialsValidateFailedError(error_object.get("message"))
case EndpointSetupFailedError.__name__:
raise EndpointSetupFailedError(error_object.get("message"))
+3
2025-11-12 17:59:37 +08:00
case TriggerProviderCredentialValidationError.__name__:
raise TriggerProviderCredentialValidationError(error_object.get("message"))
case TriggerPluginInvokeError.__name__:
raise TriggerPluginInvokeError(description=error_object.get("message"))
+3
2025-11-12 17:59:37 +08:00
case TriggerInvokeError.__name__:
raise TriggerInvokeError(error_object.get("message"))
case EventIgnoreError.__name__:
raise EventIgnoreError(description=error_object.get("message"))
2026-06-18 07:34:33 +08:00
# NOTE: current plugin sdk / plugin daemon does not raise exception with
# type `PluginLLMPollingUnsupportedError`.
case PluginLLMPollingUnsupportedError.__name__:
raise PluginLLMPollingUnsupportedError(description=error_object.get("message"))
case PluginRuntimeError.__name__:
args = error_object.get("args")
lambda_request_id = args.get("request_id") if isinstance(args, Mapping) else None
if not isinstance(lambda_request_id, str):
lambda_request_id = None
runtime_message = error_object.get("message")
if not isinstance(runtime_message, str):
runtime_message = "Plugin runtime request failed"
raise PluginRuntimeError(
description=runtime_message,
lambda_request_id=lambda_request_id,
)
+109
2025-02-17 17:05:13 +08:00
case _:
raise PluginInvokeError(description=message)
case PluginDaemonInternalServerError.__name__:
raise PluginDaemonInternalServerError(description=message)
case PluginDaemonBadRequestError.__name__:
raise PluginDaemonBadRequestError(description=message)
case PluginDaemonNotFoundError.__name__:
raise PluginDaemonNotFoundError(description=message)
case PluginUniqueIdentifierError.__name__:
raise PluginUniqueIdentifierError(description=message)
case PluginNotFoundError.__name__:
raise PluginNotFoundError(description=message)
case PluginDaemonUnauthorizedError.__name__:
raise PluginDaemonUnauthorizedError(description=message)
case PluginPermissionDeniedError.__name__:
raise PluginPermissionDeniedError(description=message)
case _:
raise Exception(f"got unknown error from plugin daemon: {error_type}, message: {message}")