Skip to content
广告 · 本站推荐广告

插件清单

Plugin Manifest(插件清单)定义了 OpenClaw 插件的元数据、类型、导出约定与生命周期。每个插件都必须在 package.json 中声明 openclaw 字段以被网关正确识别。

package.json 要求

一个合规的 OpenClaw 插件 package.json 最小结构:

json
{
  "name": "@openclaw/my-plugin",
  "version": "1.0.0",
  "description": "我的 OpenClaw 插件",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "openclaw": {
    "type": "tool",
    "displayName": "我的工具插件",
    "minGatewayVersion": "0.5.0",
    "permissions": ["network", "file-read"]
  },
  "keywords": ["openclaw", "openclaw-plugin"],
  "license": "MIT"
}

清单字段详解

openclaw 节点字段

字段类型必填说明
typestring插件类型:channeltoolhook
displayNamestring显示名称
minGatewayVersionstring最低网关版本要求
permissionsstring[]所需权限列表
configSchemaobject配置项 JSON Schema
multiInstanceboolean是否支持多实例(默认 false

权限声明

声明 permissions 可以让用户在安装时了解插件的能力范围。常见权限:networkfile-readfile-writeexec

插件类型

Channel(渠道插件)

typescript
import { ChannelPlugin } from '@openclaw/sdk'

const plugin: ChannelPlugin = {
  type: 'channel',
  name: 'my-channel',

  async onMessage(message, context) {
    // 处理来自渠道的消息
  },

  async sendMessage(to, content, context) {
    // 向渠道发送消息
  }
}

export default plugin

Tool(工具插件)

typescript
import { ToolPlugin } from '@openclaw/sdk'

const plugin: ToolPlugin = {
  type: 'tool',
  tools: [
    {
      name: 'my_tool',
      description: '工具描述',
      parameters: { /* JSON Schema */ },
      handler: async (params, ctx) => {
        return { result: 'done' }
      }
    }
  ]
}

export default plugin

Hook(钩子插件)

typescript
import { HookPlugin } from '@openclaw/sdk'

const plugin: HookPlugin = {
  type: 'hook',
  hooks: {
    'message:before': async (message, ctx) => {
      // 消息处理前的拦截逻辑
      return message
    },
    'message:after': async (response, ctx) => {
      // 消息处理后的后处理逻辑
    }
  }
}

export default plugin

导出约定

导出规则

插件必须使用 默认导出export default)。网关不会识别命名导出。

typescript
// ✅ 正确
export default plugin

// ❌ 错误
export { plugin }

生命周期钩子

插件可实现以下生命周期方法:

钩子触发时机用途
onLoad插件被加载时初始化资源、建立连接
onReady网关就绪后执行启动后逻辑
onUnload插件被卸载时清理资源、关闭连接
onConfigChange配置变更时热更新配置
typescript
export default {
  type: 'tool',
  async onLoad(config, gateway) {
    console.log('插件加载完成')
  },
  async onUnload() {
    console.log('插件已卸载,资源已释放')
  }
}

本地测试

bash
openclaw plugins link ./my-plugin
bash
cd my-plugin
npm test
bash
openclaw start --debug --plugin-dev ./my-plugin

发布到 npm

bash
# 构建
npm run build

# 登录 npm
npm login

# 发布
npm publish --access public

发布检查清单

发布前确认:✅ 版本号已更新 ✅ openclaw 字段完整 ✅ 包含 README ✅ 测试通过

基于MIT协议开源 | 内容翻译自 官方文档,同步更新