Skip to content

页说明了 OpenClaw 插件在 register 回调中注入的 api.runtime 对象。它替代了直接导入 host 内部模块,涵盖 agent 身份/工作区、session 管理(getSessionEntry/listSessionEntries/patchSessionEntry)、嵌入式 agent 运行(runEmbeddedAgent)、LLM 文本补全(llm.complete)、后台 subagent 运行和等待、Task Flow 任务编排(managedFlows)、TTS 语音合成、媒体理解(含结构化提取 extractStructuredWithModel)、图像生成、网页搜索、媒体工具(QR 码生成)、配置读写(mutateConfigFile/replaceConfigFile 及其 afterWrite 策略)、系统事件、状态键值存储(openKeyedStore 带 TTL 和最大条目限制)、渠道插件媒体下载和提及策略。优先使用已传入 call path 的配置对象,仅在长时间运行 handler 中才直接调用 current()。模型覆盖(llm/subagent)需要配置 opt-in。

OpenClaw 插件运行时 api.runtime 命名空间参考

api.runtime 是注入到每个插件注册回调中的运行时辅助对象,提供 gateway 核心能力的统一调用入口。使用这些 helper 避免直接导入或复制 host 内部逻辑。

分步实操指导:渠道插件开发请参考渠道插件指南,Provider 插件开发请参考Provider 插件指南,了解如何在具体场景中使用这些 helper。

typescript
register(api) {
  const runtime = api.runtime;
}

配置加载和写入

优先使用已传入活动调用路径中的配置对象(如注册时的 api.config 或渠道/Provider 回调的 cfg 参数),这样可以保持单一进程快照,避免在热路径上重复解析配置。

只有在长时间运行 handler 无法获得传入配置,且需要当前进程快照时,再使用 api.runtime.config.current()。返回值为只读;修改前需要克隆或使用变更 helper。

Tool 工厂函数接收 ctx.runtimeConfigctx.getRuntimeConfig()。在长时间运行 tool 的 execute 回调中使用 getter 获取最新配置以防定义后配置发生变化。

使用 api.runtime.config.mutateConfigFile(...)api.runtime.config.replaceConfigFile(...) 持久化修改。每次写入必须指定 afterWrite 策略:

  • afterWrite: { mode: "auto" } 由 gateway 重载决策器决定。
  • afterWrite: { mode: "restart", reason: "..." } 写入者认为热重载不安全时强制完全重启。
  • afterWrite: { mode: "none", reason: "..." } 仅当调用者自己处理后续操作时抑制自动重载/重启。

变更 helper 返回 afterWrite 及类型化 followUp 摘要,调用者可据此记录或测试是否请求了重启。gateway 掌握实际重启的执行时机。

api.runtime.config.loadConfig()api.runtime.config.writeConfigFile(...) 是已弃用的兼容性 helper。它们会在运行时发出一次警告,在迁移窗口期内对旧的外部插件仍然可用。内部插件(bundled plugins)不能使用这些方法;如果插件代码调用它们或从插件 SDK 子路径导入这些 helper,配置边界守卫会报错。

推荐直接使用聚焦的子路径替代批量兼容 barrel:config-contracts(类型)、plugin-config-runtime(已加载配置断言和插件入口查找)、runtime-config-snapshot(当前进程快照)、config-mutation(写入)。内部插件测试应直接 mock 这些聚焦子路径,而非 mock 批量兼容 barrel。

OpenClaw 内部运行时代码遵循同样原则:在 CLI、gateway 或进程边界处加载一次配置,然后将该值传递下去。成功的配置变更写入会刷新进程运行时快照并递增内部修订号;长时间运行缓存应基于运行时的缓存键而非本地序列化配置。运行时模块对偷用 loadConfig() 的行为有零容忍扫描器;应使用传入的 cfg、请求的 context.getRuntimeConfig() 或在显式进程边界处使用 getRuntimeConfig()

Provider 和渠道执行路径必须使用活动运行时配置快照,而非用于读取/编辑返回的文件快照。文件快照保留源值(如 SecretRef 标记)供 UI 和写入使用;Provider 回调需要已解析的运行时视图。当某个 helper 可能被同时传入活动源快照或活动运行时快照时,在读取凭据前先用 selectApplicableRuntimeConfig() 路由选择。

可复用的运行时工具

频道回合 botLoopProtection

核心在 session 记录和分发之前应用共享的内存滑动窗口守卫,不绑定到特定频道。守卫记录 (scopeId, conversationId, participant pair) 键,对 pair 的两个方向一起计数,超出窗口预算后施加强制冷却,并机会性地清理无效条目。

渠道插件若向运维人员暴露此行为,应优先使用共享的 channels.defaults.botLoopProtection 形状设定基准预算,然后叠加频道/Provider 特定覆盖。共享配置以秒为单位(面向用户):

typescript
type ChannelBotLoopProtectionConfig = {
  enabled?: boolean;
  maxEventsPerWindow?: number;
  windowSeconds?: number;
  cooldownSeconds?: number;
};

在返回的整个 turn 中附带标准化后的 bot-pair 事实。核心会解析默认值、单位转换和 enabled 语义:

typescript
return {
  channel: "example",
  routeSessionKey,
  storePath,
  ctxPayload,
  recordInboundSession,
  runDispatch,
  botLoopProtection: {
    scopeId: "account-1",
    conversationId: "channel-1",
    senderId: "bot-a",
    receiverId: "bot-b",
    config: channelConfig.botLoopProtection,
    defaultsConfig: runtimeConfig.channels?.defaults?.botLoopProtection,
    defaultEnabled: allowBotsMode !== "off",
  },
};

仅当自定义两方事件循环不走共享频道回合内核时,才直接使用 openclaw/plugin-sdk/pair-loop-guard-runtime

Runtime 命名空间

api.runtime.agent

Agent 身份、目录和 session 管理。

typescript
// 解析 agent 工作目录
const agentDir = api.runtime.agent.resolveAgentDir(cfg);

// 解析 agent 工作区目录
const workspaceDir = api.runtime.agent.resolveAgentWorkspaceDir(cfg);

// 获取 agent 身份
const identity = api.runtime.agent.resolveAgentIdentity(cfg);

// 获取默认思考级别
const thinking = api.runtime.agent.resolveThinkingDefault({
  cfg,
  provider,
  model,
});

// 验证用户提供的思考级别与活动 Provider profile
const policy = api.runtime.agent.resolveThinkingPolicy({ provider, model });
const level = api.runtime.agent.normalizeThinkingLevel("extra high");
if (level && policy.levels.some((entry) => entry.id === level)) {
  // 传给嵌入式运行时
}

// 获取 agent 超时(毫秒)
const timeoutMs = api.runtime.agent.resolveAgentTimeoutMs(cfg);

// 确保工作区存在
await api.runtime.agent.ensureAgentWorkspace(cfg);

// 运行嵌入式 agent 回合(取代已弃用的 runEmbeddedPiAgent)
const agentDir = api.runtime.agent.resolveAgentDir(cfg);
const result = await api.runtime.agent.runEmbeddedAgent({
  sessionId: "my-plugin:task-1",
  runId: crypto.randomUUID(),
  sessionFile: path.join(agentDir, "sessions", "my-plugin-task-1.jsonl"),
  workspaceDir: api.runtime.agent.resolveAgentWorkspaceDir(cfg),
  prompt: "Summarize the latest changes",
  timeoutMs: api.runtime.agent.resolveAgentTimeoutMs(cfg),
});

runEmbeddedAgent(...) 是插件代码启动普通 OpenClaw agent 回合的通用 helper,使用与频道触发回复相同的 Provider/model 解析和 agent harness 选择。runEmbeddedPiAgent(...) 保持为兼容性别名。

resolveThinkingPolicy(...) 返回 Provider/model 支持的思考级别和可选默认值。Provider 插件通过自己的 thinking hooks 拥有模型特定 profile,因此工具插件应使用此运行时 helper 而非导入或复制 Provider 列表。

normalizeThinkingLevel(...) 将用户文本如 onx-highextra high 转换为规范存储级别后再与解析的 policy 比对。

Session store helpers 位于 api.runtime.agent.session

typescript
// 获取单个 session 条目
const entry = api.runtime.agent.session.getSessionEntry({ agentId, sessionKey });

// 遍历 session 行,不依赖旧的 sessions.json 结构
for (const { sessionKey, entry } of api.runtime.agent.session.listSessionEntries({ agentId })) {
  // 处理每个条目
}

// 更新 session 条目(返回新 entry)
await api.runtime.agent.session.patchSessionEntry({
  agentId,
  sessionKey,
  update: (entry) => ({ thinkingLevel: "high" }),
});

优先使用 getSessionEntry(...)listSessionEntries(...)patchSessionEntry(...)upsertSessionEntry(...)。这些 helper 通过 agent/session 身份寻址,插件无需依赖旧的 sessions.json 存储格式。使用 preserveActivity: true 进行仅 metadata 的更新(不刷新 session 活动时间)。replaceEntry: true 仅在回调返回完整条目且需删除已删除字段时使用。loadSessionStore(...) 保持为已弃用的兼容逃生舱,供确实需要可变完整 store 克隆的调用者。

api.runtime.agent.defaults

默认模型和 Provider 常量:

typescript
const model = api.runtime.agent.defaults.model;     // 如 "anthropic/claude-sonnet-4-6"
const provider = api.runtime.agent.defaults.provider; // 如 "anthropic"

api.runtime.llm

执行主机拥有的文本补全,无需导入 Provider 内部或重复 OpenClaw 模型/认证/基础 URL 准备。

typescript
const result = await api.runtime.llm.complete({
  messages: [{ role: "user", content: "Summarize this transcript." }],
  purpose: "my-plugin.summary",
  maxTokens: 512,
  temperature: 0.2,
});

该 helper 使用与 OpenClaw 内置运行时及主机拥有的运行时配置快照相同的简单补全准备路径。Context 引擎接收 session 绑定的 llm.complete 能力,因此模型调用使用当前 session 的 agent,不会静默回退到默认 agent。结果包含 Provider/model/agent 归属及标准化 token、缓存、估算成本(如有)。

警告 模型覆盖需要运维人员在配置中显式开启 plugins.entries.<id>.llm.allowModelOverride: true。使用 plugins.entries.<id>.llm.allowedModels 限制受信任插件到特定规范 provider/model 目标。跨 agent 补全需要 plugins.entries.<id>.llm.allowAgentIdOverride: true

api.runtime.subagent

启动和管理后台 subagent 运行。

typescript
// 启动 subagent 运行
const { runId } = await api.runtime.subagent.run({
  sessionKey: "agent:main:subagent:search-helper",
  message: "Expand this query into focused follow-up searches.",
  provider: "openai",   // 可选覆盖
  model: "gpt-4.1-mini", // 可选覆盖
  deliver: false,
});

// 等待完成
const result = await api.runtime.subagent.waitForRun({ runId, timeoutMs: 30000 });

// 读取 session 消息
const { messages } = await api.runtime.subagent.getSessionMessages({
  sessionKey: "agent:main:subagent:search-helper",
  limit: 10,
});

// 删除 session
await api.runtime.subagent.deleteSession({
  sessionKey: "agent:main:subagent:search-helper",
});

警告 模型覆盖(provider/model)需要运维人员在配置中开启 plugins.entries.<id>.subagent.allowModelOverride: true。不受信任的插件仍可运行 subagent,但覆盖请求会被拒绝。

deleteSession(...) 只能删除由同一插件通过 api.runtime.subagent.run(...) 创建的 session。删除任意用户或运维人员的 session 仍需要 admin 级别的 Gateway 请求。

api.runtime.tasks.managedFlows

将 Task Flow 运行时绑定到已有 OpenClaw session key 或受信任工具上下文,然后无需每次调用传入 owner 即可创建和管理 Task Flow。

Task Flow 跟踪持久的、多步骤工作流状态。它不是调度器:使用 Cron 或 api.session.workflow.scheduleSessionTurn(...) 安排未来唤醒,然后在调度回合中使用 managedFlows 处理需要流状态、子任务、等待或取消的工作。

typescript
const taskFlow = api.runtime.tasks.managedFlows.fromToolContext(ctx);

const created = taskFlow.createManaged({
  controllerId: "my-plugin/review-batch",
  goal: "Review new pull requests",
});

const child = taskFlow.runTask({
  flowId: created.flowId,
  runtime: "acp",
  childSessionKey: "agent:main:subagent:reviewer",
  task: "Review PR #123",
  status: "running",
  startedAt: Date.now(),
});

const waiting = taskFlow.setWaiting({
  flowId: created.flowId,
  expectedRevision: created.revision,
  currentStep: "await-human-reply",
  waitJson: { kind: "reply", channel: "telegram" },
});

使用 bindSession({ sessionKey, requesterOrigin }) 当你已有来自自己绑定层的受信任 OpenClaw session key 时。不要直接从原始用户输入绑定。

api.runtime.tts

文字转语音合成。

typescript
// 标准 TTS
const clip = await api.runtime.tts.textToSpeech({
  text: "Hello from OpenClaw",
  cfg: api.config,
});

// 电话优化 TTS
const telephonyClip = await api.runtime.tts.textToSpeechTelephony({
  text: "Hello from OpenClaw",
  cfg: api.config,
});

// 列出可用声音
const voices = await api.runtime.tts.listVoices({
  provider: "elevenlabs",
  cfg: api.config,
});

使用核心 messages.tts 配置和 Provider 选择。返回 PCM 音频 buffer + 采样率。

api.runtime.mediaUnderstanding

图像、音频和视频分析。

typescript
// 描述图像
const image = await api.runtime.mediaUnderstanding.describeImageFile({
  filePath: "/tmp/inbound-photo.jpg",
  cfg: api.config,
  agentDir: "/tmp/agent",
});

// 转录音频
const { text } = await api.runtime.mediaUnderstanding.transcribeAudioFile({
  filePath: "/tmp/inbound-audio.ogg",
  cfg: api.config,
  mime: "audio/ogg", // 可选,当 MIME 无法推断时使用
});

// 描述视频
const video = await api.runtime.mediaUnderstanding.describeVideoFile({
  filePath: "/tmp/inbound-video.mp4",
  cfg: api.config,
});

// 通用文件分析
const result = await api.runtime.mediaUnderstanding.runFile({
  filePath: "/tmp/inbound-file.pdf",
  cfg: api.config,
});

// 结构化提取:通过指定 Provider/model 提取结构化信息
const evidence = await api.runtime.mediaUnderstanding.extractStructuredWithModel({
  provider: "codex",
  model: "gpt-5.5",
  input: [
    { type: "image", buffer: receiptImageBuffer, fileName: "receipt.png", mime: "image/png" },
    { type: "text", text: "Prefer the printed total over handwritten notes." },
  ],
  instructions: "Extract vendor, total, and searchable tags.",
  schemaName: "receipt.evidence",
  jsonSchema: {
    type: "object",
    properties: {
      vendor: { type: "string" },
      total: { type: "number" },
      tags: { type: "array", items: { type: "string" } },
    },
    required: ["vendor", "total"],
  },
  cfg: api.config,
});

无输出时返回 { text: undefined }(如跳过的输入)。

信息api.runtime.stt.transcribeAudioFile(...) 仍作为 api.runtime.mediaUnderstanding.transcribeAudioFile(...) 的兼容别名存在。

api.runtime.imageGeneration

图像生成。

typescript
const result = await api.runtime.imageGeneration.generate({
  prompt: "A robot painting a sunset",
  cfg: api.config,
});

const providers = api.runtime.imageGeneration.listProviders({ cfg: api.config });

api.runtime.webSearch

网页搜索。

typescript
const providers = api.runtime.webSearch.listProviders({ config: api.config });

const result = await api.runtime.webSearch.search({
  config: api.config,
  args: { query: "OpenClaw plugin SDK", count: 5 },
});

api.runtime.media

低级媒体工具。

typescript
const webMedia = await api.runtime.media.loadWebMedia(url);
const mime = await api.runtime.media.detectMime(buffer);
const kind = api.runtime.media.mediaKindFromMime("image/jpeg"); // "image"
const isVoice = api.runtime.media.isVoiceCompatibleAudio(filePath);
const metadata = await api.runtime.media.getImageMetadata(filePath);
const resized = await api.runtime.media.resizeToJpeg(buffer, { maxWidth: 800 });
const terminalQr = await api.runtime.media.renderQrTerminal("https://openclaw.ai");
const pngQr = await api.runtime.media.renderQrPngBase64("https://openclaw.ai", {
  scale: 6, // 1-12
  marginModules: 4, // 0-16
});
const pngQrDataUrl = await api.runtime.media.renderQrPngDataUrl("https://openclaw.ai");
const tmpRoot = resolvePreferredOpenClawTmpDir();
const pngQrFile = await api.runtime.media.writeQrPngTempFile("https://openclaw.ai", {
  tmpRoot,
  dirPrefix: "my-plugin-qr-",
  fileName: "qr.png",
});

api.runtime.config

当前运行时配置快照和事务性配置写入。优先使用已传入活动调用路径的配置;仅在 handler 需要直接进程快照时使用 current()

typescript
const cfg = api.runtime.config.current();
await api.runtime.config.mutateConfigFile({
  afterWrite: { mode: "auto" },
  mutate(draft) {
    draft.plugins ??= {};
  },
});

mutateConfigFile(...)replaceConfigFile(...) 返回 followUp 值(例如 { mode: "restart", requiresRestart: true, reason }),记录写入者意图而不剥夺 gateway 的重启控制权。

api.runtime.system

系统级工具。

typescript
await api.runtime.system.enqueueSystemEvent(event);
api.runtime.system.requestHeartbeat({
  source: "other",
  intent: "event",
  reason: "plugin-event",
});
api.runtime.system.requestHeartbeatNow({ reason: "plugin-event" }); // 已弃用的兼容性别名
const output = await api.runtime.system.runCommandWithTimeout(cmd, args, opts);
const hint = api.runtime.system.formatNativeDependencyHint(pkg);

api.runtime.events

事件订阅。

typescript
api.runtime.events.onAgentEvent((event) => {
  /* ... */
});
api.runtime.events.onSessionTranscriptUpdate((update) => {
  /* ... */
});

api.runtime.logging

日志记录。

typescript
const verbose = api.runtime.logging.shouldLogVerbose();
const childLogger = api.runtime.logging.getChildLogger({ plugin: "my-plugin" }, { level: "debug" });

api.runtime.modelAuth

模型和 Provider 认证解析。

typescript
const auth = await api.runtime.modelAuth.getApiKeyForModel({ model, cfg });
const providerAuth = await api.runtime.modelAuth.resolveApiKeyForProvider({
  provider: "openai",
  cfg,
});

api.runtime.state

状态目录解析和 SQLite 支持的键值存储。

typescript
const stateDir = api.runtime.state.resolveStateDir(process.env);
const store = api.runtime.state.openKeyedStore<MyRecord>({
  namespace: "my-feature",
  maxEntries: 200,
  defaultTtlMs: 15 * 60_000,
});

await store.register("key-1", { value: "hello" });
const claimed = await store.registerIfAbsent("dedupe-key", { value: "first" });
const value = await store.lookup("key-1");
await store.consume("key-1");
await store.clear();

键值存储在重启后保持,且按运行时绑定的插件 ID 隔离。使用 registerIfAbsent(...) 实现原子去重声明:当 key 缺失或过期且成功注册时返回 true,当活跃值已存在时返回 false 而不覆盖其值、创建时间或 TTL。限制:每个命名空间 maxEntries,每个插件最多 1000 行活跃记录,JSON 值 < 64KB,可选 TTL 过期。

警告 当前版本仅支持内部插件(bundled plugins)。

api.runtime.tools

内存工具工厂和 CLI。

typescript
const getTool = api.runtime.tools.createMemoryGetTool(/* ... */);
const searchTool = api.runtime.tools.createMemorySearchTool(/* ... */);
api.runtime.tools.registerMemoryCli(/* ... */);

api.runtime.channel

渠道特定的运行时 helper(渠道插件加载时可用)。

api.runtime.channel.media 是渠道媒体下载和存储的首选接口:

typescript
const saved = await api.runtime.channel.media.saveRemoteMedia({
  url,
  subdir: "inbound",
  maxBytes,
  filePathHint: fileName,
});

使用 saveRemoteMedia(...) 当远程 URL 应转化为 OpenClaw 媒体。使用 saveResponseMedia(...) 当插件已使用插件拥有的认证、重定向或白名单处理获取了 Response。仅当插件需要原始字节进行检测、转换、解密或重新上传时,才使用 readRemoteMediaBuffer(...)fetchRemoteMedia(...) 保持为 readRemoteMediaBuffer(...) 的已弃用兼容性别名。

api.runtime.channel.mentions 是供使用运行时注入的内部渠道插件的共享入站提及策略接口:

typescript
const mentionMatch = api.runtime.channel.mentions.matchesMentionWithExplicit(text, {
  mentionRegexes,
  mentionPatterns,
});

const decision = api.runtime.channel.mentions.resolveInboundMentionDecision({
  facts: {
    canDetectMention: true,
    wasMentioned: mentionMatch.matched,
    implicitMentionKinds: api.runtime.channel.mentions.implicitMentionKindWhen(
      "reply_to_bot",
      isReplyToBot,
    ),
  },
  policy: {
    isGroup,
    requireMention,
    allowTextCommands,
    hasControlCommand,
    commandAuthorized,
  },
});

可用的提及 helper:

  • buildMentionRegexes
  • matchesMentionPatterns
  • matchesMentionWithExplicit
  • implicitMentionKindWhen
  • resolveInboundMentionDecision

api.runtime.channel.mentions 有意不暴露旧的 resolveMentionGating* 兼容性 helper。优先使用标准化的 { facts, policy } 路径。

存储 Runtime 引用

使用 createPluginRuntimeStore 存储运行时引用,以便在 register 回调之外的模块中使用。

  1. 创建 store
typescript
import { createPluginRuntimeStore } from "openclaw/plugin-sdk/runtime-store";
import type { PluginRuntime } from "openclaw/plugin-sdk/runtime-store";

const store = createPluginRuntimeStore<PluginRuntime>({
  pluginId: "my-plugin",
  errorMessage: "my-plugin runtime not initialized",
});
  1. 接入入口文件
typescript
export default defineChannelPluginEntry({
  id: "my-plugin",
  name: "My Plugin",
  description: "Example",
  plugin: myPlugin,
  setRuntime: store.setRuntime,
});
  1. 从其他文件访问
typescript
export function getRuntime() {
  return store.getRuntime(); // 未初始化时抛出异常
}

export function tryGetRuntime() {
  return store.tryGetRuntime(); // 未初始化时返回 null
}

注意 推荐使用 pluginId 作为运行时 store 标识。低层级 key 形式用于一个插件需要多个运行时槽位的罕见情况。

其他顶级 api 字段

api.runtime 外,API 对象还提供:

字段类型说明
api.idstring插件 id
api.namestring插件显示名称
api.configOpenClawConfig当前配置快照(运行时可用时为内存中的活跃快照)
api.pluginConfigRecord<string, unknown>来自 plugins.entries.&lt;id&gt;.config 的插件专属配置
api.loggerPluginLogger作用域日志器(debug/info/warn/error
api.registrationModePluginRegistrationMode当前加载模式;"setup-runtime" 是轻量预完整入口启动/setup 窗口
api.resolvePath(input)(string) => string解析相对于插件根的路径

相关文档

常见问题

如何在 register 回调之外的模块中访问 runtime?

使用 createPluginRuntimeStore 创建 store,在入口文件中通过 setRuntime 钩子注入,然后在其他模块中调用 store.getRuntime() 获取。tryGetRuntime() 在未初始化时返回 null 而不是抛出异常。

api.runtime.subagent.run 的模型覆盖为什么被拒绝?

模型覆盖需要运维人员配置 plugins.entries.&lt;id&gt;.subagent.allowModelOverride: true。这是安全机制,防止不受信任的插件绕过模型选择策略。如果插件是可信任的,在配置中显式开启此开关即可。

api.runtime.llm.complete 与直接调用 Provider API 有什么区别?

llm.complete 使用 OpenClaw 内置的简单补全准备逻辑:自动解析 Provider、模型、认证、基础 URL,并返回标准化的 token 用量、缓存状态和估算成本。它还继承当前 session 的 agent 配置,不会静默回退到默认 agent。模型覆盖需要运维人员开启 allowModelOverride,跨 agent 调用需要开启 allowAgentIdOverride