插件 SDK 是插件与核心之间的类型化契约。此页面是关于导入什么以及可以注册什么的参考。Documentation Index
Fetch the complete documentation index at: https://openclaw.zhcndoc.com/llms.txt
Use this file to discover all available pages before exploring further.
此页面适用于在 OpenClaw 内部使用
openclaw/plugin-sdk/* 的插件作者。对于希望通过 Gateway 运行 agent 的外部应用、脚本、仪表盘、CI 作业和 IDE 扩展,请改用 OpenClaw App SDK 和 @openclaw/sdk 包。导入约定
始终从特定子路径导入:openclaw/plugin-sdk/channel-core;将 openclaw/plugin-sdk/core 保留给
更广泛的总入口面和共享辅助函数,例如
buildChannelConfigSchema。
对于渠道配置,请通过
openclaw.plugin.json#channelConfigs 发布渠道拥有的 JSON Schema。plugin-sdk/channel-config-schema
子路径用于共享 schema 基元和通用构建器。OpenClaw 的
捆绑插件使用 plugin-sdk/bundled-channel-config-schema 来保留
捆绑渠道 schema。已弃用的兼容导出仍保留在
plugin-sdk/channel-config-schema-legacy;这两个捆绑 schema 子路径都不应
作为新插件的模式。
子路径参考
插件 SDK 以按领域分组的一组窄子路径形式暴露(插件 入口、渠道、提供方、认证、运行时、能力、内存,以及保留的 捆绑插件辅助函数)。完整目录——按组整理并带链接——请参见 插件 SDK 子路径。 生成的 200+ 个子路径列表位于scripts/lib/plugin-sdk-entrypoints.json 中。
注册 API
register(api) 回调会接收一个 OpenClawPluginApi 对象,其中包含这些
方法:
能力注册
| 方法 | 注册内容 |
|---|---|
api.registerProvider(...) | 文本推理(LLM) |
api.registerAgentHarness(...) | 实验性的底层 agent 执行器 |
api.registerCliBackend(...) | 本地 CLI 推理后端 |
api.registerChannel(...) | 消息渠道 |
api.registerSpeechProvider(...) | 文本转语音 / STT 合成 |
api.registerRealtimeTranscriptionProvider(...) | 流式实时转写 |
api.registerRealtimeVoiceProvider(...) | 双向实时语音会话 |
api.registerMediaUnderstandingProvider(...) | 图像/音频/视频分析 |
api.registerImageGenerationProvider(...) | 图像生成 |
api.registerMusicGenerationProvider(...) | 音乐生成 |
api.registerVideoGenerationProvider(...) | 视频生成 |
api.registerWebFetchProvider(...) | Web 抓取 / 抓取提供方 |
api.registerWebSearchProvider(...) | Web 搜索 |
工具和命令
| 方法 | 注册内容 |
|---|---|
api.registerTool(tool, opts?) | Agent 工具(必需或 { optional: true }) |
api.registerCommand(def) | 自定义命令(绕过 LLM) |
agentPromptGuidance。该文本应仅与命令本身相关;不要将提供方或插件特定策略加入核心提示构建器中。
基础设施
| 方法 | 注册内容 |
|---|---|
api.registerHook(events, handler, opts?) | 事件钩子 |
api.registerHttpRoute(params) | Gateway HTTP 端点 |
api.registerGatewayMethod(name, handler) | Gateway RPC 方法 |
api.registerGatewayDiscoveryService(service) | 本地 Gateway 发现广播器 |
api.registerCli(registrar, opts?) | CLI 子命令 |
api.registerService(service) | 后台服务 |
api.registerInteractiveHandler(registration) | 交互式处理器 |
api.registerAgentToolResultMiddleware(...) | 运行时工具结果中间件 |
api.registerMemoryPromptSupplement(builder) | 追加的、与内存相邻的提示词部分 |
api.registerMemoryCorpusSupplement(adapter) | 追加的内存搜索/读取语料 |
工作流插件的宿主钩子
宿主钩子是面向需要参与宿主 生命周期的插件的 SDK 接口,而不仅仅是添加提供方、渠道或工具。它们是 通用契约;Plan Mode 可以使用它们,审批工作流、 工作区策略门禁、后台监视器、安装向导以及 UI 伴随插件也都可以使用。| Method | Contract it owns |
|---|---|
api.registerSessionExtension(...) | Plugin-owned, JSON-compatible session state projected through Gateway sessions |
api.enqueueNextTurnInjection(...) | Durable exactly-once context injected into the next agent turn for one session |
api.registerTrustedToolPolicy(...) | Bundled/trusted pre-plugin tool policy that can block or rewrite tool params |
api.registerToolMetadata(...) | Tool catalog display metadata without changing the tool implementation |
api.registerCommand(...) | Scoped plugin commands; command results can set continueAgent: true; Discord native commands support descriptionLocalizations |
api.registerControlUiDescriptor(...) | Control UI contribution descriptors for session, tool, run, or settings surfaces |
api.registerRuntimeLifecycle(...) | Cleanup callbacks for plugin-owned runtime resources on reset/delete/reload paths |
api.registerAgentEventSubscription(...) | Sanitized event subscriptions for workflow state and monitors |
api.setRunContext(...) / getRunContext(...) / clearRunContext(...) | Per-run plugin scratch state cleared on terminal run lifecycle |
api.registerSessionSchedulerJob(...) | Plugin-owned session scheduler job records with deterministic cleanup |
- 外部插件可以拥有会话扩展、UI 描述符、命令、工具 元数据、下一回合注入以及普通钩子。
- 受信任的工具策略在普通
before_tool_call钩子之前运行,并且仅限捆绑插件,因为它们参与宿主安全策略。 - 保留命令所有权仅限捆绑插件。外部插件应使用它们 自己的命令名称或别名。
allowPromptInjection=false会禁用会改变提示词的钩子,包括agent_turn_prepare、before_prompt_build、heartbeat_prompt_contribution、 旧版before_agent_start中的提示词字段,以及enqueueNextTurnInjection。
| 插件类型 | 使用的钩子 |
|---|---|
| 审批工作流 | 会话扩展、命令续接、下一回合注入、UI 描述符 |
| 预算/工作区策略门禁 | 受信任的工具策略、工具元数据、会话投影 |
| 后台生命周期监视器 | 运行时生命周期清理、agent 事件订阅、会话调度器所有权/清理、heartbeat 提示词贡献、UI 描述符 |
| 安装或引导向导 | 会话扩展、作用域命令、Control UI 描述符 |
保留的核心管理命名空间(
config.*、exec.approvals.*、wizard.*、
update.*)始终保持 operator.admin,即使插件尝试分配更窄的
Gateway 方法作用域。优先为插件拥有的方法使用插件特定前缀。何时使用工具结果中间件
何时使用工具结果中间件
捆绑插件在需要在工具执行后、运行时将该结果
返回给模型之前重写工具结果时,可以使用
api.registerAgentToolResultMiddleware(...)。这是一种受信任的、与运行时无关的接口,适用于
像 tokenjuice 这样的异步输出缩减器。捆绑插件必须为每个目标运行时声明 contracts.agentToolResultMiddleware,例如 ["pi", "codex"]。外部插件
不能注册此中间件;对于不需要在模型前调整工具结果时机的工作,请保留普通的 OpenClaw 插件钩子。旧的仅 Pi 可用的嵌入式扩展工厂注册路径已被移除。Gateway 发现注册
api.registerGatewayDiscoveryService(...) 允许插件通过本地发现传输(例如 mDNS/Bonjour)公告活动中的
Gateway。OpenClaw 会在启用本地发现时于 Gateway 启动期间调用该
服务,传入当前 Gateway 端口和非机密的 TXT 提示数据,并在 Gateway 关闭期间调用返回的
stop 处理器。
CLI 注册元数据
api.registerCli(registrar, opts?) 接受两类顶层元数据:
commands:由 registrar 拥有的显式命令根descriptors:用于根 CLI 帮助、路由和惰性插件 CLI 注册的解析期命令描述符
descriptors。
commands。这种急加载兼容路径仍受支持,但它不会为解析期惰性加载安装基于描述符的占位符。
CLI 后端注册
api.registerCliBackend(...) 允许插件拥有本地
AI CLI 后端的默认配置,例如 codex-cli。
- 后端
id会成为模型引用中的提供方前缀,例如codex-cli/gpt-5。 - 后端
config使用与agents.defaults.cliBackends.<id>相同的结构。 - 用户配置仍然优先。OpenClaw 会在运行 CLI 之前,将
agents.defaults.cliBackends.<id>与插件默认配置合并。 - 当后端在合并后需要兼容性重写时,使用
normalizeConfig(例如规范化旧的参数形状)。
独占槽位
| 方法 | 注册内容 |
|---|---|
api.registerContextEngine(id, factory) | 上下文引擎(一次仅一个处于活动状态)。assemble() 回调会接收 availableTools 和 citationsMode,以便引擎可以定制提示词附加内容。 |
api.registerMemoryCapability(capability) | 统一内存能力 |
api.registerMemoryPromptSection(builder) | 内存提示词部分构建器 |
api.registerMemoryFlushPlan(resolver) | 内存刷新计划解析器 |
api.registerMemoryRuntime(runtime) | 内存运行时适配器 |
内存嵌入适配器
| 方法 | 注册内容 |
|---|---|
api.registerMemoryEmbeddingProvider(adapter) | 当前插件的内存嵌入适配器 |
registerMemoryCapability是首选的独占内存插件 API。registerMemoryCapability还可以暴露publicArtifacts.listArtifacts(...)以便伴随插件可以通过openclaw/plugin-sdk/memory-host-core消费导出的内存工件,而不是深入某个 内存插件的私有布局。registerMemoryPromptSection、registerMemoryFlushPlan和registerMemoryRuntime是兼容旧版的独占内存插件 API。MemoryFlushPlan.model可以将刷新回合固定到精确的provider/model引用,例如ollama/qwen3:8b,而不会继承当前的回退链。registerMemoryEmbeddingProvider允许活动内存插件注册一个或多个嵌入适配器 id(例如openai、gemini或自定义的 插件定义 id)。- 诸如
agents.defaults.memorySearch.provider和agents.defaults.memorySearch.fallback的用户配置,会针对这些已注册的 适配器 id 进行解析。
事件和生命周期
| 方法 | 功能 |
|---|---|
api.on(hookName, handler, opts?) | 类型化生命周期钩子 |
api.onConversationBindingResolved(handler) | 会话绑定回调 |
钩子决策语义
before_tool_call:返回{ block: true }是终止性的。一旦任何处理器设置了它,更低优先级的处理器会被跳过。before_tool_call:返回{ block: false }被视为没有决策(等同于省略block),而不是覆盖。before_install:返回{ block: true }是终止性的。一旦任何处理器设置了它,更低优先级的处理器会被跳过。before_install:返回{ block: false }被视为没有决策(等同于省略block),而不是覆盖。reply_dispatch:返回{ handled: true, ... }是终止性的。一旦任何处理器声明了派发,更低优先级的处理器和默认模型派发路径会被跳过。message_sending:返回{ cancel: true }是终止性的。一旦任何处理器设置了它,更低优先级的处理器会被跳过。message_sending:返回{ cancel: false }被视为没有决策(等同于省略cancel),而不是覆盖。message_received:当你需要传入线程/话题路由时,请使用类型化的threadId字段。将metadata保留给渠道特定的额外信息。message_sending:在回退到渠道特定的metadata之前,请使用类型化的replyToId/threadId路由字段。gateway_start:请使用ctx.config、ctx.workspaceDir和ctx.getCron?.()获取 gateway 拥有的启动状态,而不要依赖内部的gateway:startup钩子。cron_changed:观察 gateway 拥有的 cron 生命周期变化。在同步外部唤醒调度器时,请使用event.job?.state?.nextRunAtMs和ctx.getCron?.(),并让 OpenClaw 作为到期检查和执行的真实来源。
API 对象字段
| 字段 | 类型 | 说明 |
|---|---|---|
api.id | string | 插件 id |
api.name | string | 显示名称 |
api.version | string? | 插件版本(可选) |
api.description | string? | 插件描述(可选) |
api.source | string | 插件源路径 |
api.rootDir | string? | 插件根目录(可选) |
api.config | OpenClawConfig | 当前配置快照(在可用时为当前内存中的运行时 snapshot) |
api.pluginConfig | Record<string, unknown> | 来自 plugins.entries.<id>.config 的插件特定配置 |
api.runtime | PluginRuntime | 运行时辅助函数 |
api.logger | PluginLogger | 作用域日志器(debug、info、warn、error) |
api.registrationMode | PluginRegistrationMode | 当前加载模式;"setup-runtime" 是轻量级、在完整入口启动/设置之前的窗口 |
api.resolvePath(input) | (string) => string | 相对于插件根目录解析路径 |
内部模块约定
在你的插件内,使用本地 barrel 文件进行内部导入:api.ts、runtime-api.ts、
index.ts、setup-entry.ts 以及类似的公共入口文件),在 OpenClaw 已经运行时,
优先使用当前运行时配置快照。如果尚不存在运行时快照,则回退到磁盘上解析后的配置文件。
打包后的插件外观应通过 OpenClaw 的插件外观加载器加载;直接从 dist/extensions/... 导入会绕过清单和运行时 sidecar 检查,而这些检查是打包安装对插件自有代码所使用的。
提供者插件可以在某个辅助工具有意仅适用于该提供者且尚不属于通用 SDK 子路径时,
暴露一个窄范围、插件本地的 contract barrel。打包示例:
- Anthropic:面向 Claude 的
api.ts/contract-api.ts接口, 用于 beta-header 和service_tier流式辅助工具。 @openclaw/openai-provider:api.ts导出提供者构建器、 默认模型辅助工具,以及实时提供者构建器。@openclaw/openrouter-provider:api.ts导出提供者构建器 以及引导/配置辅助工具。
相关内容
入口点
definePluginEntry 和 defineChannelPluginEntry 选项。运行时辅助工具
完整的
api.runtime 命名空间参考。设置和配置
打包、清单和配置模式。
测试
测试工具和 lint 规则。
SDK 迁移
从已弃用表面迁移。
插件内部
深层架构和能力模型。