Skip to main content

Plugin SDK Overview

Plugin SDK 是外掛與核心之間的類型合約。本頁是以下內容的參考指南:應該 import 什麼 以及 可以註冊什麼
在找實作指南?

Import 慣例

一律從具體 subpath import:
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
import { defineChannelPluginEntry } from "openclaw/plugin-sdk/core";
每個 subpath 都是小型、自包含的模組。這樣可以保持啟動速度快,並防止循環依賴問題。

Subpath 參考

最常見的 subpath,按用途分組。完整的 100+ subpath 清單在 scripts/lib/plugin-sdk-entrypoints.json

Plugin entry

Subpath主要 Export
plugin-sdk/plugin-entrydefinePluginEntry
plugin-sdk/coredefineChannelPluginEntry, createChatChannelPlugin, createChannelPluginBase, defineSetupPluginEntry, buildChannelConfigSchema
Subpath主要 Export
plugin-sdk/channel-setupcreateOptionalChannelSetupSurface
plugin-sdk/channel-pairingcreateChannelPairingController
plugin-sdk/channel-reply-pipelinecreateChannelReplyPipeline
plugin-sdk/channel-config-helperscreateHybridChannelConfigAdapter
plugin-sdk/channel-config-schemaChannel config schema 類型
plugin-sdk/channel-policyresolveChannelGroupRequireMention
plugin-sdk/channel-lifecyclecreateAccountStatusSink
plugin-sdk/channel-inboundDebounce、mention matching、envelope 輔助函式
plugin-sdk/channel-send-resultReply result 類型
plugin-sdk/channel-actionscreateMessageToolButtonsSchema, createMessageToolCardSchema
plugin-sdk/channel-targetsTarget 解析/匹配輔助函式
plugin-sdk/channel-contractChannel contract 類型
plugin-sdk/channel-feedbackFeedback/reaction 配線
Subpath主要 Export
plugin-sdk/provider-authcreateProviderApiKeyAuthMethod, ensureApiKeyFromOptionEnvOrPrompt, upsertAuthProfile
plugin-sdk/provider-modelsnormalizeModelCompat
plugin-sdk/provider-catalogCatalog type 重新 export
plugin-sdk/provider-usagefetchClaudeUsage 及類似函式
plugin-sdk/provider-streamStream wrapper 類型
plugin-sdk/provider-onboardOnboarding config patch 輔助函式
Subpath主要 Export
plugin-sdk/command-authresolveControlCommandGate
plugin-sdk/allow-fromformatAllowFromLowercase
plugin-sdk/secret-inputSecret input 解析輔助函式
plugin-sdk/webhook-ingressWebhook request/target 輔助函式
Subpath主要 Export
plugin-sdk/runtime-storecreatePluginRuntimeStore
plugin-sdk/config-runtimeConfig 載入/寫入輔助函式
plugin-sdk/infra-runtimeSystem event/heartbeat 輔助函式
plugin-sdk/agent-runtimeAgent 目錄/身分/工作區輔助函式
plugin-sdk/directory-runtimeConfig 支持的 directory 查詢/去重
plugin-sdk/keyed-async-queueKeyedAsyncQueue
Subpath主要 Export
plugin-sdk/image-generationImage generation provider 類型
plugin-sdk/media-understandingMedia understanding provider 類型
plugin-sdk/speechSpeech provider 類型
plugin-sdk/testinginstallCommonResolveTargetErrorCases, shouldAckReaction

註冊 API

register(api) 回調函式收到 OpenClawPluginApi 物件,提供這些方法:

能力註冊

方法註冊內容
api.registerProvider(...)文字推理(LLM)
api.registerChannel(...)傳訊頻道
api.registerSpeechProvider(...)文字轉語音/語音合成
api.registerMediaUnderstandingProvider(...)圖片/音訊/影片分析
api.registerImageGenerationProvider(...)圖像生成
api.registerWebSearchProvider(...)網路搜尋

工具與命令

方法註冊內容
api.registerTool(tool, opts?)Agent 工具(必須或 { optional: true }
api.registerCommand(def)自訂命令(繞過 LLM)

基礎設施

方法註冊內容
api.registerHook(events, handler, opts?)Event hook
api.registerHttpRoute(params)Gateway HTTP 端點
api.registerGatewayMethod(name, handler)Gateway RPC 方法
api.registerCli(registrar, opts?)CLI 子命令
api.registerService(service)背景服務
api.registerInteractiveHandler(registration)Interactive handler

獨佔插槽

方法註冊內容
api.registerContextEngine(id, factory)Context engine(一次只能有一個有效)
api.registerMemoryPromptSection(builder)Memory prompt section builder

事件與生命週期

方法功能
api.on(hookName, handler, opts?)類型化生命週期 hook
api.onConversationBindingResolved(handler)Conversation binding 回調

API 物件欄位

欄位型別說明
api.idstring外掛 id
api.namestring顯示名稱
api.versionstring?外掛版本(可選)
api.descriptionstring?外掛說明(可選)
api.sourcestring外掛來源路徑
api.rootDirstring?外掛根目錄(可選)
api.configOpenClawConfig目前 config 快照
api.pluginConfigRecord<string, unknown>外掛特定 config,來自 plugins.entries.<id>.config
api.runtimePluginRuntimeRuntime 輔助函式
api.loggerPluginLogger範疇化 logger(debug, info, warn, error
api.registrationModePluginRegistrationMode"full", "setup-only", 或 "setup-runtime"
api.resolvePath(input)(string) => string解析相對於外掛根的路徑

內部模組慣例

在你的外掛內,使用本地 barrel 檔案供內部 import:
my-plugin/
  api.ts            # 為外部消費者匯出的公開 export
  runtime-api.ts    # 僅限內部的 runtime export
  index.ts          # 外掛進入點
  setup-entry.ts    # 輕量級僅 setup 進入點(可選)
絕對不要從生產代碼中透過 openclaw/plugin-sdk/<your-plugin> import 你自己的外掛。改為透過 ./api.ts./runtime-api.ts 路由內部 import。SDK 路徑僅作為外部合約。

相關