agentic_huge_data_base / wiki
页面 Dify · 7.4 插件 Daemon 与执行环境·DeepWiki 中文全文译文

7.4 · 插件 Daemon 与执行环境(Plugin Daemon and Execution Environment)

应用编排与外部知识接入 · 本章是 Dify DeepWiki 中文译文的独立章节页,保留原始链接、源码锚点、模块标签和章节层级。

项目Dify 章节7.4 状态全文译文 模块接口与服务契约、系统架构、界面与交互、配置治理
源码线索
  • api/core/entities/knowledge_entities.py
  • api/core/entities/provider_entities.py
  • api/core/helper/marketplace.py
  • api/core/plugin/endpoint/exc.py
  • api/core/plugin/entities/endpoint.py
  • api/core/plugin/entities/marketplace.py
  • api/core/plugin/entities/plugin.py
  • api/core/plugin/entities/plugin_daemon.py
  • api/core/plugin/entities/request.py
  • api/core/plugin/impl/base.py
模块标签
  • 接口与服务契约
  • 系统架构
  • 界面与交互
  • 配置治理
  • 智能体运行时

中文译文

插件 Daemon 与执行环境(中文译文)

原始 DeepWiki 页面:https://deepwiki.com/langgenius/dify/7.4-plugin-daemon-and-execution-environment
翻译时间:2026-05-27T08:44:26.485Z
翻译模型:deepseek-chat
原文字符数:12648
项目:Dify (dify)

---

插件守护进程与执行环境

相关源文件

以下文件用于生成此 Wiki 页面:

  • api/core/entities/knowledge_entities.py
  • api/core/entities/provider_entities.py
  • api/core/helper/marketplace.py
  • api/core/plugin/endpoint/exc.py
  • api/core/plugin/entities/endpoint.py
  • api/core/plugin/entities/marketplace.py
  • api/core/plugin/entities/plugin.py
  • api/core/plugin/entities/plugin_daemon.py
  • api/core/plugin/entities/request.py
  • api/core/plugin/impl/base.py
  • api/core/plugin/impl/datasource.py
  • api/core/plugin/impl/endpoint.py
  • api/core/plugin/impl/exc.py
  • api/core/plugin/impl/model.py
  • api/core/plugin/impl/oauth.py
  • api/core/plugin/impl/plugin.py
  • api/migrations/versions/2025_07_22_0019-375fe79ead14_oauth_refresh_token.py
  • api/migrations/versions/2025_09_08_1007-c20211f18133_add_headers_to_mcp_provider.py
  • api/schedule/check_upgradable_plugin_task.py
  • api/services/plugin/oauth_service.py
  • api/tasks/process_tenant_plugin_autoupgrade_check_task.py
  • api/tests/test_containers_integration_tests/services/tools/test_mcp_tools_manage_service.py
  • api/tests/unit_tests/core/plugin/test_endpoint_client.py
  • api/tests/unit_tests/core/plugin/test_plugin_runtime.py
  • api/tests/unit_tests/tasks/test_process_tenant_plugin_autoupgrade_check_task.py
  • api/tests/unit_tests/utils/http_parser/test_oauth_convert_request_to_raw_data.py
  • web/app/components/base/tab-header/index.tsx
  • web/app/components/base/tab-slider-new/index.tsx
  • web/app/components/base/tab-slider-plain/index.tsx
  • web/app/components/base/tab-slider/index.tsx
  • web/app/components/header/header-wrapper.tsx
  • web/app/components/plugins/marketplace/hydration-server.tsx
  • web/app/components/plugins/plugin-page/plugins-panel.tsx
  • web/app/components/plugins/types.ts
  • web/app/components/tools/mcp/headers-input.tsx
  • web/context/query-client-server.ts
  • web/context/query-client.tsx
  • web/hooks/use-query-params.spec.tsx
  • web/hooks/use-query-params.ts
  • web/service/use-plugins.ts

本文描述了插件守护进程服务和执行环境,该环境支持在 Dify 中安全、隔离地执行第三方插件。有关特定插件类型(工具、模型提供方)的信息,请参阅工具提供方类型与架构。对于不需要插件守护进程的内置工具,请参阅内置工具与 API 工具集成

目的与范围

插件守护进程是一个独立的基于 Go 的服务,负责管理第三方插件的生命周期和执行。它提供以下功能:

  • 安全隔离:插件在主 API 服务之外的独立进程空间中执行。
  • 资源管理:对插件操作进行 CPU、内存和执行时间限制。
  • 包管理:插件包的安装、缓存和版本管理。
  • 市场集成:从 Dify 市场发现和安装插件。
  • 多语言支持:提供带有隔离环境的 Python 插件运行时。

---

系统架构

插件守护进程作为独立服务与主 Dify API 并行运行,通过 HTTP 通信并在隔离环境中管理插件执行。

高层组件架构
graph TB
    subgraph "Dify API 服务(Python)"
        PluginService["PluginService<br/>services.plugin.plugin_service"]
        PluginInstaller["PluginInstaller<br/>core.plugin.impl.plugin"]
        BasePluginClient["BasePluginClient<br/>core.plugin.impl.base"]
    end

    subgraph "插件守护进程服务(Go)<br/>端口 5002"
        DaemonAPI["HTTP API<br/>/plugin/{tenant_id}/management/*"]
        RuntimeManager["插件运行时管理器<br/>Python 环境管理"]
        PackageCache["包缓存<br/>已下载的插件"]
        ExecutionPool["执行池<br/>隔离进程"]
    end

    subgraph "存储与注册表"
        Redis["Redis<br/>任务状态与缓存"]
        Marketplace["Dify 市场<br/>MARKETPLACE_API_URL"]
    end

    PluginService --> PluginInstaller
    PluginInstaller -- "继承" --> BasePluginClient
    BasePluginClient -->|"HTTP 请求<br/>_request()"| DaemonAPI

    DaemonAPI --> RuntimeManager
    DaemonAPI --> PackageCache
    RuntimeManager --> ExecutionPool

    PluginService -->|获取清单| Marketplace
    PluginInstaller -->|上传 PKG/包| DaemonAPI

    ExecutionPool -.回调.-> PluginService

来源api/core/plugin/impl/base.py:62-71api/core/plugin/impl/plugin.py:25-30api/core/helper/marketplace.py:12-13

---

插件守护进程服务

插件守护进程作为独立容器部署,拥有自己的生命周期和配置。

服务配置

服务配置由 dify_config 对象驱动,该对象将环境变量映射到 Pydantic 设置。

配置项描述
URLPLUGIN_DAEMON_URL插件守护进程服务的基础 URL
端口5002守护进程的默认 HTTP API 端口
认证密钥PLUGIN_DAEMON_KEY用于认证对守护进程 API 请求的密钥
超时时间PLUGIN_DAEMON_TIMEOUT守护进程请求的超时时间(默认 600 秒)
最大包大小PLUGIN_MAX_PACKAGE_SIZE上传插件包的最大大小

来源api/core/plugin/impl/base.py:41-52api/core/helper/marketplace.py:21-22

---

执行环境

插件在由守护进程管理的隔离 Python 环境中执行,并受插件清单中定义的严格资源限制约束。

插件资源需求

PluginResourceRequirements 类定义了每个插件的约束条件,包括内存限制和特定的权限范围(工具、模型、节点、端点、存储)。

来源api/core/plugin/entities/plugin.py:26-58

执行流程与逻辑

PluginInstaller 负责处理与守护进程的生命周期任务通信。例如,获取插件的 README 或从标识符安装插件。

sequenceDiagram
    participant API as API 服务<br/>PluginInstaller
    participant Daemon as 插件守护进程<br/>HTTP API
    participant Runtime as 运行时管理器
    participant Process as 插件进程<br/>Python

    API->>Daemon: POST /install/identifiers<br/>{identifiers, source, metas}
    Daemon->>Runtime: 准备环境
    Runtime->>Runtime: 创建虚拟环境并安装依赖
    Runtime->>Daemon: 任务 ID
    Daemon->>API: PluginInstallTaskStartResponse

    Note over API, Process: 执行阶段
    API->>Daemon: POST /execute(方法调用)
    Daemon->>Process: 生成隔离进程
    Process->>Process: 加载插件模块
    Process->>Runtime: 返回结果
    Daemon->>API: PluginBasicBooleanResponse

来源api/core/plugin/impl/plugin.py:119-140api/core/plugin/entities/plugin_daemon.py:70-77

---

插件安装与生命周期

插件从 PluginInstallationSource 定义的四个主要来源安装:GithubMarketplacePackage(本地上传)和 Remote(调试)。

来源api/core/plugin/entities/plugin.py:19-23

安装过程

PluginInstaller 类提供了与守护进程交互进行包管理的方法。

  • upload_pkg:将 .dify 包文件上传到守护进程 api/core/plugin/impl/plugin.py:77-100
  • install_from_identifiers:通过唯一标识符触发多个插件的安装 api/core/plugin/impl/plugin.py:119-140
  • fetch_plugin_installation_task:轮询正在进行的安装任务的状态 api/core/plugin/impl/plugin.py:153-161
市场集成

Dify 与市场集成以实现插件发现。marketplace.py 辅助模块负责处理与市场 API 的直接通信。

# api/core/helper/marketplace.py
def download_plugin_pkg(plugin_unique_identifier: str):
    return download_with_size_limit(get_plugin_pkg_url(plugin_unique_identifier), dify_config.PLUGIN_MAX_PACKAGE_SIZE)

来源api/core/helper/marketplace.py:16-21web/service/use-plugins.ts:201-204

---

自动升级机制

Dify 包含一个后台任务系统,用于根据租户特定策略检查和应用插件更新。

自动升级策略

租户可以通过 TenantPluginAutoUpgradeStrategy 配置策略:

  • LATEST:始终升级到最新版本。
  • FIX_ONLY:仅在补丁版本(X.Y.Z -> X.Y.Z+1)发生变化时升级。
  • DISABLED:不进行自动更新。

来源api/tasks/process_tenant_plugin_autoupgrade_check_task.py:160-163

升级任务流程
graph TD
    Cron["Celery Beat<br/>check_upgradable_plugin_task"] --> FetchGlobal["fetch_global_plugin_manifest<br/>(市场 API)"]
    FetchGlobal --> CacheRedis["缓存到 Redis<br/>(1 小时 TTL)"]
    CacheRedis --> Dispatch["分发每个租户的任务"]
    Dispatch --> TenantTask["process_tenant_plugin_autoupgrade_check_task"]
    TenantTask --> Compare["比较版本与策略"]
    Compare -->|需要升级| Upgrade["PluginService.upgrade_plugin_with_marketplace"]

来源api/schedule/check_upgradable_plugin_task.py:25-64api/tasks/process_tenant_plugin_autoupgrade_check_task.py:149-178

---

数据模型与实体

插件清单(manifest.json

PluginDeclaration 类表示插件的元数据,包括其类别、作者和所需资源。

字段类型描述
versionstr插件版本字符串
categoryPluginCategory工具、模型、扩展、代理策略等
resourcePluginResourceRequirements内存和权限限制
pluginsPlugins提供的组件列表(工具、模型、端点)

来源api/core/plugin/entities/plugin.py:70-113

自然语言到代码实体的映射
系统概念代码实体文件
安装任务PluginInstallTaskapi/core/plugin/entities/plugin_daemon.py:161-166
插件实体PluginEntityapi/core/plugin/entities/plugin.py:157-168
守护进程客户端BasePluginClientapi/core/plugin/impl/base.py:62-71
市场辅助模块marketplace.pyapi/core/helper/marketplace.py:1-13
插件安装器PluginInstallerapi/core/plugin/impl/plugin.py:25-210

来源api/core/plugin/entities/plugin_daemon.py:161-166api/core/plugin/impl/base.py:62-71api/core/plugin/impl/plugin.py:25-30