Files
gsfdc/demo-dify/dify/api/services/plugin/dispatch_identity.py
T
wuzilongandCursor 6519682885 chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 13:59:25 +08:00

48 lines
1.6 KiB
Python

"""Bind daemon dispatches to the plugin declaration used by the caller."""
from collections.abc import Mapping
from dataclasses import dataclass, field
from typing import Any, NotRequired
from typing_extensions import TypedDict
PLUGIN_IDENTIFIER_HEADER = "X-Plugin-Unique-Identifier"
@dataclass(frozen=True, slots=True)
class PluginOAuthClientConfig:
"""OAuth credentials paired with the declaration used to resolve them."""
plugin_unique_identifier: str
credentials: Mapping[str, Any] = field(repr=False)
class PluginDispatchIdentity(TypedDict, closed=True):
plugin_unique_identifier: NotRequired[str]
def plugin_dispatch_headers(plugin_id: str, identifier: str | None) -> dict[str, str]:
headers = {"X-Plugin-ID": plugin_id, "Content-Type": "application/json"}
if identifier is not None:
headers[PLUGIN_IDENTIFIER_HEADER] = identifier
return headers
def prepare_dispatch_identity(path: str, headers: dict[str, str], identifier: str | None) -> None:
parts = path.split("/")
if len(parts) < 4 or parts[0] != "plugin" or parts[2] != "dispatch":
return
identifier = headers.get(PLUGIN_IDENTIFIER_HEADER) or identifier
if identifier is None:
# Legacy calls leave installation resolution to the daemon.
return
plugin_id = headers.get("X-Plugin-ID")
if not plugin_id:
raise ValueError("X-Plugin-ID is required for plugin dispatch")
if identifier.partition(":")[0] != plugin_id:
raise ValueError("Plugin unique identifier does not match X-Plugin-ID")
headers[PLUGIN_IDENTIFIER_HEADER] = identifier