前言

一个优秀的框架不仅要有强大的核心功能,更要有开放的扩展能力。OpenClaw 的 Plugins 系统正是为了打造开放生态而设计的。本篇文章将深入剖析这个插件系统的架构与实现。


1. 插件系统概述

1.1 为什么需要插件系统?

┌─────────────────────────────────────────────────────────────────┐
│                   插件系统的必要性                                │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│   没有插件系统:                                                    │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                          │   │
│   │   OpenClaw ──────────────────────────────────► 功能固定  │   │
│   │                                                          │   │
│   │   • 无法扩展新模型                                        │   │
│   │   • 无法支持新平台                                        │   │
│   │   • 无法自定义工具                                        │   │
│   │   • 依赖官方更新                                         │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                  │
│   有插件系统:                                                      │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                          │   │
│   │        OpenClaw Core                                     │   │
│   │            │                                              │   │
│   │   ┌────────┼────────┐                                    │   │
│   │   │        │        │                                    │   │
│   │   ▼        ▼        ▼                                    │   │
│   │  ┌───┐  ┌───┐  ┌───┐  ┌───┐                           │   │
│   │  │插件│  │插件│  │插件│  │插件│  ──► 无限扩展           │   │
│   │  │ 1 │  │ 2 │  │ 3 │  │ 4 │                           │   │
│   │  └───┘  └───┘  └───┘  └───┘                           │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

1.2 插件类型

类型 说明 示例
Provider AI 模型提供商 extensions/anthropic/
Channel 消息渠道集成 extensions/telegram/
Memory 记忆存储引擎 extensions/memory-lancedb/
Capability 扩展能力 extensions/browser/
Tool 工具扩展 自定义工具

2. 目录结构

src/plugins/
├── manifest.ts          # 插件清单解析
├── loader.ts            # 插件加载器
├── registry.ts          # 插件注册表
├── hooks.ts             # 钩子管理
├── install.ts           # 安装逻辑
├── update.ts            # 更新管理
└── sandbox.ts           # 沙箱隔离

packages/
├── plugin-sdk/         # 插件 SDK (150+ 导出)
│   ├── core/           # 核心类型和接口
│   ├── runtime/        # 运行时支持
│   ├── channel-runtime/# 渠道运行时
│   ├── agent-runtime/  # Agent 运行时
│   ├── memory-core/    # 记忆核心
│   ├── security-runtime/# 安全运行时
│   └── media-runtime/  # 媒体运行时

extensions/              # 插件目录 (100+ 插件)
├── anthropic/          # Claude 提供商
├── openai/             # OpenAI 提供商
├── telegram/          # Telegram 渠道
├── discord/            # Discord 渠道
├── memory-core/        # 核心记忆
├── memory-lancedb/     # LanceDB 存储
├── browser/           # 浏览器控制
├── speech-core/       # 语音核心
└── ...

3. Plugin Manifest

3.1 清单结构

每个插件必须包含 manifest.json5 文件:

// extensions/telegram/manifest.json5
{
  // 基础信息
  id: "telegram",
  name: "Telegram",
  description: "Connect your Telegram bots to OpenClaw",
  version: "1.0.0",
  author: "OpenClaw Team",
  homepage: "https://openclaw.ai/plugins/telegram",

  // 插件类型
  type: "channel",

  // 声明的能力
  capabilities: [
    "channel:telegram",
    "media:image",
    "media:video",
    "media:document",
    "media:audio",
    "streaming",
    "typing-indicator",
  ],

  // 声明的技能
  skills: [
    "messaging",
    "group-management",
    "bot-commands",
  ],

  // 配置模式
  configSchema: {
    botToken: { type: "string", required: true },
    allowedChats: { type: "array", required: false },
  },

  // 钩子配置
  hooks: {
    "before-message-receive": "./hooks/before-receive.ts",
    "after-message-send": "./hooks/after-send.ts",
  },

  // 依赖
  dependencies: {
    "@openclaw/plugin-sdk": "^2026.1.0",
  },

  // 权限要求
  permissions: [
    "network:external",
    "storage:config",
  ],
}

3.2 类型定义

// manifest 类型定义
interface PluginManifest {
  // 必需字段
  id: string;
  name: string;
  version: string;
  type: PluginType;

  // 可选字段
  description?: string;
  author?: string;
  homepage?: string;

  // 能力声明
  capabilities?: string[];
  skills?: string[];

  // 生命周期钩子
  hooks?: Record<HookType, string>;

  // 依赖
  dependencies?: Record<string, string>;

  // 配置
  configSchema?: ConfigSchema;

  // 权限
  permissions?: Permission[];
}

type PluginType = 
  | "provider"      // AI 模型提供商
  | "channel"       // 消息渠道
  | "memory"        // 记忆存储
  | "capability"    // 扩展能力
  | "tool";         // 工具扩展

4. 插件生命周期

4.1 完整生命周期

┌─────────────────────────────────────────────────────────────────┐
│                      插件生命周期                                 │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│   1. Discovery (发现)                                            │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                          │   │
│   │   扫描目录:                                              │   │
│   │   • extensions/                                          │   │
│   │   • ~/.openclaw/plugins/                                 │   │
│   │   • npm 包 (@openclaw/*)                                │   │
│   │                                                          │   │
│   │   验证清单: manifest.json5                                │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                        │                                         │
│                        ▼                                         │
│   2. Validation (验证)                                          │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                          │   │
│   │   • 版本兼容性检查                                       │   │
│   │   • 必需依赖检查                                         │   │
│   │   • 权限声明验证                                         │   │
│   │   • 能力冲突检测                                         │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                        │                                         │
│                        ▼                                         │
│   3. Installation (安装)                                        │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                          │   │
│   │   • 依赖解析                                             │   │
│   │   • npm 包安装 (如需要)                                  │   │
│   │   • 资源复制                                             │   │
│   │   • 配置生成                                             │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                        │                                         │
│                        ▼                                         │
│   4. Registration (注册)                                        │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                          │   │
│   │   • 能力注册到 Registry                                  │   │
│   │   • 钩子函数注册                                         │   │
│   │   • 路由规则注册                                         │   │
│   │   • 服务暴露注册                                         │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                        │                                         │
│                        ▼                                         │
│   5. Activation (激活)                                           │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                          │   │
│   │   • 初始化插件实例                                       │   │
│   │   • 建立连接 (如需要)                                    │   │
│   │   • 注册事件监听                                         │   │
│   │   • 健康检查                                             │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                        │                                         │
│                        ▼                                         │
│   6. Runtime (运行)                                             │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                          │   │
│   │   处理请求 ←→ 响应事件 ←→ 定时任务                        │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                        │                                         │
│                        ▼                                         │
│   7. Deactivation (停用)                                        │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                          │   │
│   │   • 关闭连接                                             │   │
│   │   • 清理监听                                             │   │
│   │   • 保存状态                                             │   │
│   │   • 释放资源                                             │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

4.2 加载器实现

// src/plugins/loader.ts (简化版)
export class PluginLoader {
  async load(pluginPath: string): Promise<PluginInstance> {
    // 1. 读取清单
    const manifest = await this.loadManifest(pluginPath);

    // 2. 验证清单
    this.validateManifest(manifest);

    // 3. 加载代码
    const module = await import(pluginPath);

    // 4. 创建实例
    const instance = this.createInstance(manifest, module);

    // 5. 注册钩子
    if (manifest.hooks) {
      await this.registerHooks(instance, manifest.hooks);
    }

    // 6. 注册能力
    if (manifest.capabilities) {
      await this.registerCapabilities(instance, manifest.capabilities);
    }

    return instance;
  }

  async activate(plugin: PluginInstance): Promise<void> {
    if (plugin.activate) {
      await plugin.activate();
    }
    plugin.status = "active";
  }

  async deactivate(plugin: PluginInstance): Promise<void> {
    if (plugin.deactivate) {
      await plugin.deactivate();
    }
    plugin.status = "inactive";
  }
}

5. Plugin SDK

5.1 SDK 导出概览

Plugin SDK 提供 150+ 导出模块:

// 核心模块
export * from "./plugin-sdk/core";           // 核心类型和接口
export * from "./plugin-sdk/runtime";        // 运行时支持

// 渠道模块
export * from "./plugin-sdk/channel-runtime";       // 渠道运行时
export * from "./plugin-sdk/channel-config-primitives"; // 配置原语

// Agent 模块
export * from "./plugin-sdk/agent-runtime";    // Agent 运行时
export * from "./plugin-sdk/models-provider-runtime"; // 模型提供商

// 记忆模块
export * from "./plugin-sdk/memory-core";     // 记忆核心
export * from "./plugin-sdk/memory-host-core"; // 记忆主机

// 安全模块
export * from "./plugin-sdk/security-runtime";    // 安全运行时
export * from "./plugin-sdk/approval-runtime";     // 审批运行时

// 媒体模块
export * from "./plugin-sdk/media-runtime";       // 媒体运行时
export * from "./plugin-sdk/image-generation-core"; // 图像生成
export * from "./plugin-sdk/speech-core";          // 语音核心

5.2 核心接口

// 插件主接口
export interface OpenClawPlugin {
  // 必需属性
  readonly manifest: PluginManifest;
  readonly status: PluginStatus;

  // 生命周期
  activate?(): Promise<void>;
  deactivate?(): Promise<void>;

  // 能力实现
  getCapabilities?(): Capability[];
  getServices?(): Service[];

  // 钩子实现
  beforeAgentStart?(ctx: HookContext): Promise<void>;
  afterAgentReply?(ctx: HookContext): Promise<void>;
}

// 能力定义
export interface Capability {
  id: string;
  type: CapabilityType;
  description: string;
  execute(ctx: CapabilityContext): Promise<CapabilityResult>;
}

// 服务定义
export interface Service {
  name: string;
  api: Record<string, ServiceMethod>;
}

6. 钩子系统

6.1 可用钩子类型

// 钩子类型定义
type HookType =
  // 消息钩子
  | "on-message-received"       // 消息接收时
  | "before-message-send"      // 消息发送前
  | "after-message-send"        // 消息发送后

  // Agent 钩子
  | "before-agent-start"       // Agent 启动前
  | "after-agent-reply"        // Agent 回复后

  // 工具钩子
  | "before-tool-call"         // 工具调用前
  | "after-tool-call"          // 工具调用后

  // 会话钩子
  | "on-session-create"        // 会话创建时
  | "on-session-close"        // 会话关闭时

  // 错误钩子
  | "on-error";               // 错误发生时

// 钩子接口
interface Hook {
  type: HookType;
  handler: (context: HookContext) => Promise<void>;
  priority: number;           // 执行优先级
  async: boolean;             // 是否异步执行
}

6.2 钩子执行流程

┌─────────────────────────────────────────────────────────────────┐
│                      钩子执行流程                                 │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│   [事件触发]                                                      │
│        │                                                          │
│        ▼                                                          │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  收集钩子                                                 │   │
│   │                                                          │   │
│   │   1. 系统内置钩子                                         │   │
│   │   2. 插件声明钩子                                         │   │
│   │   3. 用户自定义钩子                                       │   │
│   │                                                          │   │
│   │   按 priority 排序                                        │   │
│   └─────────────────────────────────────────────────────────┘   │
│        │                                                          │
│        ▼                                                          │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  执行钩子链                                               │   │
│   │                                                          │   │
│   │   for (hook of sortedHooks) {                           │   │
│   │     if (hook.async) {                                   │   │
│   │       await hook.handler(context);                      │   │
│   │     } else {                                            │   │
│   │       hook.handler(context);                            │   │
│   │     }                                                    │   │
│   │   }                                                      │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│        │                                                          │
│        ▼                                                          │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  结果处理                                                 │   │
│   │                                                          │   │
│   │   • 修改 context (如需要)                                │   │
│   │   • 短路处理 (如返回 false)                               │   │
│   │   • 错误处理                                             │   │
│   │                                                          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

7. 插件市场

7.1 插件发现

┌─────────────────────────────────────────────────────────────────┐
│                      插件发现机制                                 │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                     官方市场                             │   │
│   │                     openclaw.ai/plugins                   │   │
│   └─────────────────────────────────────────────────────────┘   │
│                              │                                   │
│         ┌────────────────────┼────────────────────┐             │
│         │                    │                    │             │
│         ▼                    ▼                    ▼             │
│   ┌──────────┐        ┌──────────┐        ┌──────────┐        │
│   │  官方插件 │        │  社区插件 │        │  私有插件 │        │
│   │          │        │          │        │          │        │
│   │ • Claude │        │ • Custom │        │ • 企业   │        │
│   │ • OpenAI │        │   Tools  │        │   内部   │        │
│   │ • Tele. │        │ • 实验性  │        │ • 定制   │        │
│   │ • Discord│        │   功能   │        │   功能   │        │
│   └──────────┘        └──────────┘        └──────────┘        │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

7.2 安装命令

# 从市场安装
openclaw plugin install telegram
openclaw plugin install anthropic
openclaw plugin install memory-lancedb

# 从本地安装
openclaw plugin install ./my-plugin

# 从 URL 安装
openclaw plugin install https://example.com/plugin.tgz

# 列出已安装
openclaw plugin list

# 更新插件
openclaw plugin update telegram

# 卸载插件
openclaw plugin uninstall telegram

8. 安全隔离

8.1 沙箱机制

// 沙箱配置
const sandboxConfig = {
  // 资源限制
  timeout: 30000,              // 30 秒超时
  memoryLimit: "256MB",        // 内存限制
  cpuLimit: 0.5,              // CPU 限制

  // 网络限制
  allowedNetworks: ["api.openai.com", "api.anthropic.com"],
  blockedHosts: ["internal.corp.local"],

  // 文件系统限制
  allowedPaths: ["/data/plugins"],
  readOnlyPaths: ["/system"],

  // 权限控制
  capabilities: ["net:http", "fs:read"],
};

9. 本章小结

┌─────────────────────────────────────────────────────────────────┐
│                        本章要点                                   │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│  🔌 插件系统价值                                                │
│  ├── 扩展核心功能,不影响稳定性                                 │
│  ├── 社区共建,丰富生态                                        │
│  └── 灵活定制,满足个性化需求                                   │
│                                                                  │
│  📦 插件结构                                                    │
│  ├── manifest.json5: 元数据声明                                 │
│  ├── 能力注册: capabilities                                    │
│  ├── 钩子配置: hooks                                           │
│  └── 依赖声明: dependencies                                     │
│                                                                  │
│  🔄 生命周期                                                    │
│  └── 发现 → 验证 → 安装 → 注册 → 激活 → 运行 → 停用             │
│                                                                  │
│  🪝 钩子系统                                                    │
│  └── 消息钩子 / Agent 钩子 / 工具钩子 / 会话钩子               │
│                                                                  │
│  🛡️ 安全隔离                                                    │
│  └── 沙箱机制 / 资源限制 / 权限控制                            │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

系列导航

章节 标题 状态
01 OpenClaw 是什么? ✅ 已发布
02 系统架构全景图 ✅ 已发布
03 Gateway 网关层 ✅ 已发布
04 Agents 模块 ✅ 已发布
05 Channels 消息渠道 ✅ 已发布
06 Plugins 插件系统(本文) ✅ 已发布
07 数据流设计 🔜 下一章

如有问题欢迎在评论区留言!

Logo

小龙虾开发者社区是 CSDN 旗下专注 OpenClaw 生态的官方阵地,聚焦技能开发、插件实践与部署教程,为开发者提供可直接落地的方案、工具与交流平台,助力高效构建与落地 AI 应用

更多推荐