此页面适用于在 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 子路径。 编译器入口点清单位于scripts/lib/plugin-sdk-entrypoints.json;包导出是在
从公共子集减去 scripts/lib/plugin-sdk-private-local-only-subpaths.json 中列出的仓库本地测试/内部子路径后生成的。
运行
pnpm plugin-sdk:surface 可审计公共导出数量。已弃用且历史足够久、并且未被捆绑扩展生产代码使用的公共
子路径记录在 scripts/lib/plugin-sdk-deprecated-public-subpaths.json 中;广泛的
已弃用重导出 barrel 记录在
scripts/lib/plugin-sdk-deprecated-barrel-subpaths.json 中。
注册 API
register(api) 回调会接收一个 OpenClawPluginApi 对象,其中包含这些
方法:
能力注册
| 方法 | 注册内容 |
|---|---|
api.registerProvider(...) | 文本推理(LLM) |
api.registerAgentHarness(...) | 实验性的低级 agent 执行器 |
api.registerCliBackend(...) | 本地 CLI 推理后端 |
api.registerChannel(...) | 消息渠道 |
api.registerEmbeddingProvider(...) | 可复用的向量嵌入提供方 |
api.registerSpeechProvider(...) | 文本转语音 / STT 合成 |
api.registerRealtimeTranscriptionProvider(...) | 流式实时转录 |
api.registerRealtimeVoiceProvider(...) | 双工实时语音会话 |
api.registerMediaUnderstandingProvider(...) | 图像/音频/视频分析 |
api.registerImageGenerationProvider(...) | 图像生成 |
api.registerMusicGenerationProvider(...) | 音乐生成 |
api.registerVideoGenerationProvider(...) | 视频生成 |
api.registerWebFetchProvider(...) | Web 获取 / 抓取提供方 |
api.registerWebSearchProvider(...) | Web 搜索 |
api.registerEmbeddingProvider(...) 注册的 Embedding 提供方也必须列在插件清单的 contracts.embeddingProviders 中。这是用于可复用向量生成的通用 embedding 能力面。内存搜索可以消费这个通用提供方能力面。较旧的 api.registerMemoryEmbeddingProvider(...) 和 contracts.memoryEmbeddingProviders 接缝属于已弃用的兼容路径,供现有的内存专用提供方迁移使用。
工具和命令
对固定工具名称的简单仅工具插件,请使用defineToolPlugin。
对于混合插件或完全动态的工具注册,请直接使用 api.registerTool(...)。
| 方法 | 注册内容 |
|---|---|
api.registerTool(tool, opts?) | Agent 工具(必需或 { optional: true }) |
api.registerCommand(def) | 自定义命令(绕过 LLM) |
agentPromptGuidance。该文本应仅与命令本身相关;不要将提供方或插件特定策略加入核心提示构建器中。
Guidance entries may be legacy strings, which apply to every prompt surface, or
structured entries:
surfaces may include openclaw_main, codex_app_server,
cli_backend, acp_backend, or subagent. pi_main remains a deprecated alias
for openclaw_main. Omit surfaces for intentional all-surface guidance. Do
not pass an empty surfaces array; it is rejected so accidental scope loss does
not become global prompt text.
Native Codex app-server developer instructions are stricter than other prompt
surfaces: only guidance explicitly scoped to codex_app_server is promoted into
that higher-priority lane. Legacy string guidance and unscoped structured
guidance remain available to non-Codex prompt surfaces for compatibility.
Infrastructure
| 方法 | 注册内容 |
|---|---|
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.registerNodeCliFeature(registrar, opts?) | openclaw nodes 下的 Node 功能 |
api.registerService(service) | 后台服务 |
api.registerInteractiveHandler(registration) | 交互式处理器 |
api.registerAgentToolResultMiddleware(...) | 运行时工具结果中间件 |
api.registerMemoryPromptSupplement(builder) | 额外的、与内存相邻的提示词部分 |
api.registerMemoryCorpusSupplement(adapter) | 额外的内存搜索/读取语料 |
工作流插件的宿主钩子
宿主钩子是面向需要参与宿主 生命周期的插件的 SDK 接口,而不仅仅是添加提供方、渠道或工具。它们是 通用契约;Plan Mode 可以使用它们,审批工作流、 工作区策略门禁、后台监视器、安装向导以及 UI 伴随插件也都可以使用。| 方法 | 契约所有内容 |
|---|---|
api.session.state.registerSessionExtension(...) | 由插件拥有、与 JSON 兼容的会话状态,通过 Gateway 会话进行投影 |
api.session.workflow.enqueueNextTurnInjection(...) | 持久化的、exactly-once 的上下文,注入到一个会话的下一次 agent 回合中 |
api.registerTrustedToolPolicy(...) | 捆绑/受信任的、插件前置的工具策略,可阻止或重写工具参数 |
api.registerToolMetadata(...) | 不改变工具实现的工具目录显示元数据 |
api.registerCommand(...) | 作用域化插件命令;命令结果可以设置 continueAgent: true;Discord 原生命令支持 descriptionLocalizations |
api.session.controls.registerControlUiDescriptor(...) | 会话、工具、运行或设置界面的 Control UI 贡献描述符 |
api.lifecycle.registerRuntimeLifecycle(...) | 用于插件拥有的运行时资源在重置/删除/重载路径上的清理回调 |
api.agent.events.registerAgentEventSubscription(...) | 用于工作流状态和监视器的已净化事件订阅 |
api.runContext.setRunContext(...) / getRunContext(...) / clearRunContext(...) | 每次运行的插件临时状态,在终止性运行生命周期中清除 |
api.session.workflow.registerSessionSchedulerJob(...) | 插件拥有的调度器作业的清理元数据;不会调度工作或创建任务记录 |
api.session.workflow.sendSessionAttachment(...) | 仅捆绑插件可用的、由宿主中介的文件附件投递到活动的直接出站会话路由 |
api.session.workflow.scheduleSessionTurn(...) / unscheduleSessionTurnsByTag(...) | 仅捆绑插件可用的、由 Cron 支持的计划会话回合以及基于标签的清理 |
api.session.controls.registerSessionAction(...) | 类型化会话动作,客户端可以通过 Gateway 分发 |
api.session.state.registerSessionExtension(...)api.session.workflow.enqueueNextTurnInjection(...)api.session.workflow.registerSessionSchedulerJob(...)api.session.workflow.sendSessionAttachment(...)api.session.workflow.scheduleSessionTurn(...)api.session.workflow.unscheduleSessionTurnsByTag(...)api.session.controls.registerSessionAction(...)api.session.controls.registerControlUiDescriptor(...)api.agent.events.registerAgentEventSubscription(...)api.agent.events.emitAgentEvent(...)api.runContext.setRunContext(...)/getRunContext(...)/clearRunContext(...)api.lifecycle.registerRuntimeLifecycle(...)
api.registerSessionExtension、api.enqueueNextTurnInjection、
api.registerControlUiDescriptor、api.registerRuntimeLifecycle、
api.registerAgentEventSubscription、api.emitAgentEvent、
api.setRunContext、api.getRunContext、api.clearRunContext、
api.registerSessionSchedulerJob、api.registerSessionAction、
api.sendSessionAttachment、api.scheduleSessionTurn 或
api.unscheduleSessionTurnsByTag。
scheduleSessionTurn(...) 是在 Gateway
Cron 调度器之上的会话作用域便捷封装。Cron 负责时序,并在回合运行时创建后台任务记录;Plugin SDK 仅约束目标会话、插件拥有的
命名和清理。当工作本身需要持久化的多步骤 Task Flow 状态时,请在计划中的
回合内使用 api.runtime.tasks.managedFlows。
这些契约刻意分离了权限:
- 外部插件可以拥有会话扩展、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 这样的异步输出缩减器。Bundled plugins must declare contracts.agentToolResultMiddleware for each
targeted runtime, for example ["openclaw", "codex"]. External plugins
cannot register this middleware; keep normal OpenClaw plugin hooks for work
that does not need pre-model tool-result timing. The old embedded-runner-only
extension factory registration path has been removed.Gateway 发现注册
api.registerGatewayDiscoveryService(...) 允许插件通过本地发现传输(例如 mDNS/Bonjour)公告活动中的
Gateway。OpenClaw 会在启用本地发现时于 Gateway 启动期间调用该
服务,传入当前 Gateway 端口和非机密的 TXT 提示数据,并在 Gateway 关闭期间调用返回的
stop 处理器。
CLI 注册元数据
api.registerCli(registrar, opts?) 接受两类命令元数据:
commands:由 registrar 拥有的显式命令名称descriptors:用于 CLI 帮助、路由和惰性插件 CLI 注册的解析时命令描述符parentPath:用于嵌套命令组的可选父命令路径,例如["nodes"]
api.registerNodeCliFeature(registrar, opts?)。它是
api.registerCli(..., { parentPath: ["nodes"] }) 的一个小包装,并使诸如
openclaw nodes canvas 这样的命令成为明确的插件拥有节点功能。
如果你希望插件命令在常规根 CLI 路径中保持惰性加载,
请提供覆盖该 registrar 暴露的每个顶层命令根的 descriptors。
program 传入:
commands。
该急切兼容路径仍受支持,但它不会为解析时惰性加载安装基于
描述符的占位符。
CLI 后端注册
api.registerCliBackend(...) 允许插件拥有本地
AI CLI 后端(例如 claude-cli 或 my-cli)的默认配置。
- 后端
id会成为模型引用中的提供方前缀,例如my-cli/gpt-5。 - 后端
config的形状与agents.defaults.cliBackends.<id>相同。 - 用户配置仍然优先。OpenClaw 会在运行 CLI 前,将
agents.defaults.cliBackends.<id>叠加在插件默认值之上。 - 当后端在合并后需要兼容性重写时,请使用
normalizeConfig(例如规范化旧的标志形状)。 - 当需要属于 CLI 方言的、请求作用域的 argv 重写时,请使用
resolveExecutionArgs, 例如将 OpenClaw 的思考级别映射到原生的 effort 标志。
独占槽位
| 方法 | 注册内容 |
|---|---|
api.registerContextEngine(id, factory) | 上下文引擎(一次仅一个处于活动状态)。assemble() 回调会接收 availableTools 和 citationsMode,以便引擎可以定制提示词附加内容。 |
api.registerMemoryCapability(capability) | 统一内存能力 |
api.registerMemoryPromptSection(builder) | 内存提示词部分构建器 |
api.registerMemoryFlushPlan(resolver) | 内存刷新计划解析器 |
api.registerMemoryRuntime(runtime) | 内存运行时适配器 |
Deprecated memory embedding adapters
| 方法 | 注册内容 |
|---|---|
api.registerMemoryEmbeddingProvider(adapter) | 当前插件的内存嵌入适配器 |
registerMemoryCapabilityis the preferred exclusive memory-plugin API.registerMemoryCapabilitymay also exposepublicArtifacts.listArtifacts(...)so companion plugins can consume exported memory artifacts throughopenclaw/plugin-sdk/memory-host-coreinstead of reaching into a specific memory plugin’s private layout.registerMemoryPromptSection,registerMemoryFlushPlan, andregisterMemoryRuntimeare legacy-compatible exclusive memory-plugin APIs.MemoryFlushPlan.modelcan pin the flush turn to an exactprovider/modelreference, such asollama/qwen3:8b, without inheriting the active fallback chain.registerMemoryEmbeddingProvideris deprecated. New embedding providers should useapi.registerEmbeddingProvider(...)andcontracts.embeddingProviders.- Existing memory-specific providers continue to work during the migration window, but plugin inspection reports this as compatibility debt for non-bundled plugins.
事件和生命周期
| 方法 | 功能 |
|---|---|
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 检查,而这些检查会在插件自身代码的打包安装中使用。
提供方插件可以暴露一个较窄、仅限插件本地的契约 barrel,当某个 helper 明确只适用于该提供方,并且尚未成为通用 SDK 子路径的一部分时。
打包示例:
- Anthropic:
api.ts/contract-api.ts接口用于 Claude, 适用于 beta-header 和service_tier流式处理 helper。 @openclaw/openai-provider:api.ts导出 provider 构建器、 默认模型 helper,以及 live provider 构建器。@openclaw/openrouter-provider:api.ts导出 provider 构建器 以及 bootstrap/配置 helper。
相关内容
入口点
definePluginEntry 和 defineChannelPluginEntry 选项。运行时助手
api.runtime 命名空间完整参考。设置和配置
打包、清单和配置模式。
测试
测试工具和 lint 规则。
SDK 迁移
从已弃用的表面迁移。
插件内部
深入的架构和能力模型。