api.runtime 对象的参考文档,该对象会在每个插件注册时注入。请使用这些辅助工具,而不是直接导入宿主内部实现。
Channel plugins
在 channel 插件上下文中使用这些辅助工具的分步指南。
Provider plugins
在 provider 插件上下文中使用这些辅助工具的分步指南。
配置加载与写入
优先使用已经传入当前调用路径的配置,例如注册期间的api.config,或者 channel/provider 回调中的 cfg 参数。这样可以让单次进程快照贯穿整个工作流程,而不是在热点路径上重复解析配置。
仅当长生命周期处理器需要当前进程快照且该函数未传入配置时,才使用 api.runtime.config.current()。返回值是只读的;在编辑前请先克隆,或使用修改辅助工具。
工具工厂会接收 ctx.runtimeConfig 以及 ctx.getRuntimeConfig()。当长生命周期工具的 execute 回调中,配置可能在工具定义创建后发生变化时,请在回调内使用 getter。
通过 api.runtime.config.mutateConfigFile(...) 或 api.runtime.config.replaceConfigFile(...) 持久化更改。每次写入都必须选择明确的 afterWrite 策略:
afterWrite: { mode: "auto" }允许 gateway 重新加载规划器自行决定。afterWrite: { mode: "restart", reason: "..." }当写入方知道热重载不安全时,强制进行一次干净重启。afterWrite: { mode: "none", reason: "..." }仅当调用方自己负责后续处理时,才抑制自动重载/重启。
afterWrite 以及带类型的 followUp 摘要,因此调用方可以记录或测试自己是否请求了重启。gateway 仍然负责决定重启何时真正发生。
api.runtime.config.loadConfig() 和 api.runtime.config.writeConfigFile(...) 是 runtime-config-load-write 下的弃用兼容辅助工具。它们会在运行时警告一次,并在迁移窗口内继续供旧版外部插件使用。打包后的插件不得使用它们;如果插件代码调用这些辅助工具,或从插件 SDK 子路径导入它们,配置边界保护会失败。
对于直接的 SDK 导入,请改用更聚焦的 config 子路径,而不是宽泛的
openclaw/plugin-sdk/config-runtime 兼容入口:config-contracts
用于类型,plugin-config-runtime 用于已加载配置断言和插件
入口查找,runtime-config-snapshot 用于当前进程快照,以及
config-mutation 用于写入。打包后的插件测试应直接模拟这些聚焦的
子路径,而不是模拟宽泛的兼容入口。
OpenClaw 内部运行时代码遵循相同方向:在 CLI、gateway 或进程边界只加载一次配置,然后将该值向下传递。成功的修改写入会刷新进程运行时快照并推进其内部修订版本;长生命周期缓存应使用运行时拥有的缓存键,而不是在本地序列化配置。长生命周期运行时模块对环境中的 loadConfig() 调用实行零容忍扫描;请使用传入的 cfg、请求的 context.getRuntimeConfig(),或在明确的进程边界使用 getRuntimeConfig()。
provider 和 channel 的执行路径必须使用当前运行时配置快照,而不是用于配置回读或编辑的文件快照。文件快照会保留源值,例如用于 UI 和写入的 SecretRef 标记;provider 回调需要的是解析后的运行时视图。当某个辅助工具可能接收当前源快照或当前运行时快照中的任意一种时,请在读取凭据前通过 selectApplicableRuntimeConfig() 进行路由。
可复用运行时工具
Use inboundbotLoopProtection facts for bot-authored inbound messages. Core applies the shared in-memory sliding-window guard before session record and dispatch, without tying the policy to one channel. The guard tracks (scopeId, conversationId, participant pair) keys, counts both directions of a pair together, applies a cooldown once the window budget is exceeded, and prunes inactive entries opportunistically.
向操作员暴露此行为的 channel 插件应优先使用共享的 channels.defaults.botLoopProtection 结构作为基础预算,然后再叠加 channel/provider 特定覆盖。共享配置使用秒作为单位,因为它面向用户:
enabled 语义:
openclaw/plugin-sdk/pair-loop-guard-runtime 直接用于不经由共享入站回复运行器的自定义双人事件循环。
运行时命名空间
api.runtime.agent
api.runtime.agent
Agent 身份、目录和会话管理。会话工作流请优先使用
runEmbeddedAgent(...) 是从插件代码中启动正常 OpenClaw agent 回合的中性辅助工具。它使用与 channel 触发回复相同的 provider/model 解析和 agent-harness 选择。runEmbeddedPiAgent(...) 仍然作为现有插件的已弃用兼容别名保留。新代码应使用 runEmbeddedAgent(...)。resolveThinkingPolicy(...) 返回 provider/model 支持的思考级别以及可选默认值。provider 插件通过其思考钩子拥有特定于模型的 profile,因此工具插件应调用这个运行时辅助工具,而不是导入或重复 provider 列表。normalizeThinkingLevel(...) 会将用户文本(例如 on、x-high 或 extra high)转换为规范化的存储级别,然后再将其与解析出的策略进行比较。会话存储辅助工具 位于 api.runtime.agent.session 下:getSessionEntry(...)、listSessionEntries(...)、patchSessionEntry(...) 或 upsertSessionEntry(...)。这些辅助工具通过 agent/session 身份定位会话,因此插件不会依赖旧版 sessions.json 存储结构。对于不应刷新会话活动时间的仅元数据补丁,请使用 preserveActivity: true;仅当回调返回完整条目且被删除字段必须保持删除时,才使用 replaceEntry: true。loadSessionStore(...) 仍作为已弃用的兼容逃生口,供确实需要可变全量存储克隆的调用方使用。api.runtime.agent.defaults
api.runtime.agent.defaults
默认模型和 provider 常量:
api.runtime.llm
api.runtime.llm
在不导入 provider 内部实现或重复 OpenClaw 模型/认证/基础 URL 准备逻辑的情况下,运行一次宿主拥有的文本补全。该辅助工具使用与 OpenClaw 内置运行时相同的简单补全准备流程以及宿主拥有的运行时配置快照。上下文引擎会获得与会话绑定的
llm.complete 能力,因此模型调用会使用当前会话的 agent,而不会静默回退到默认 agent。返回结果会包含 provider/model/agent 归属信息,以及在可用时的规范化 token、缓存和估算成本使用情况。api.runtime.subagent
api.runtime.subagent
启动并管理后台 subagent 运行。
deleteSession(...) 可以删除由同一插件通过 api.runtime.subagent.run(...) 创建的会话。删除任意用户或操作员会话仍需要带有 admin 作用域的 Gateway 请求。api.runtime.nodes
api.runtime.nodes
列出已连接节点,并从 Gateway 加载的插件代码或插件 CLI 命令中调用节点主机命令。当插件拥有配对设备上的本地工作时使用,例如另一台 Mac 上的浏览器或音频桥接。在 Gateway 内部,此运行时在进程内执行。在插件 CLI 命令中,它通过 RPC 调用已配置的 Gateway,因此像
openclaw googlemeet recover-tab 这样的命令可以从终端检查配对节点。节点命令仍然通过正常的 Gateway 节点配对、命令允许列表、插件节点调用策略以及节点本地命令处理。暴露危险节点主机命令的插件应使用 api.registerNodeInvokePolicy(...) 注册节点调用策略。该策略会在 Gateway 中运行,先于命令被转发到节点之前执行,且在命令允许列表检查之后,因此直接的 node.invoke 调用和更高层级的插件工具共享相同的强制执行路径。api.runtime.tasks.managedFlows
api.runtime.tasks.managedFlows
将 Task Flow 运行时绑定到现有的 OpenClaw 会话键或受信任的工具上下文,然后在不必每次调用都传入 owner 的情况下创建和管理 Task Flow。Task Flow 跟踪持久的多步骤工作流状态。它不是调度器:
对未来唤醒请使用 Cron 或 当你已经从自己的绑定层获得了受信任的 OpenClaw 会话键时,请使用
api.session.workflow.scheduleSessionTurn(...),然后在该计划回合中使用
managedFlows,当该工作需要 flow 状态、子任务、等待或取消时再使用它。bindSession({ sessionKey, requesterOrigin })。不要从原始用户输入中进行绑定。api.runtime.tts
api.runtime.tts
文本转语音合成。使用核心
messages.tts 配置和 provider 选择。返回 PCM 音频缓冲区 + 采样率。api.runtime.mediaUnderstanding
api.runtime.mediaUnderstanding
图像、音频和视频分析。当未产生任何输出时返回
{ text: undefined }(例如跳过输入)。api.runtime.stt.transcribeAudioFile(...) 仍然作为 api.runtime.mediaUnderstanding.transcribeAudioFile(...) 的兼容别名保留。api.runtime.imageGeneration
api.runtime.imageGeneration
图像生成。
api.runtime.webSearch
api.runtime.webSearch
网页搜索。
api.runtime.media
api.runtime.media
低级媒体工具。
api.runtime.config
api.runtime.config
当前运行时配置快照和事务性配置写入。优先使用已经传入当前调用路径的配置;仅当处理器需要直接获取进程快照时才使用
current()。mutateConfigFile(...) 和 replaceConfigFile(...) 会返回一个 followUp
值,例如 { mode: "restart", requiresRestart: true, reason },
它记录了写入方的意图,而不会把重启控制权从 gateway 手中夺走。api.runtime.system
api.runtime.system
系统级工具。
api.runtime.events
api.runtime.events
事件订阅。
api.runtime.logging
api.runtime.logging
日志记录。
api.runtime.modelAuth
api.runtime.modelAuth
模型和 provider 认证解析。
api.runtime.state
api.runtime.state
状态目录解析和基于 SQLite 的键值存储。Keyed stores survive restarts and are isolated by the runtime-bound plugin id. Use
registerIfAbsent(...) for atomic dedupe claims: it returns true when the key was missing or expired and registered, or false when a live value already exists without overwriting its value, creation time, or TTL. Limits: maxEntries per namespace, 6,000 live rows per plugin, JSON values under 64KB, and optional TTL expiry. When a write would exceed the plugin row cap, the runtime may evict the oldest live rows from the namespace being written; sibling namespaces are not evicted for that write, and the write still fails if the namespace cannot free enough rows.api.runtime.tools
api.runtime.tools
内存工具工厂和 CLI。
api.runtime.channel
api.runtime.channel
channel 特定的运行时辅助工具(在加载了 channel 插件时可用)。当远程 URL 应该成为 OpenClaw 媒体时使用 可用的提及辅助工具:
api.runtime.channel.media 是 channel 媒体下载和存储的首选接口:saveRemoteMedia(...)。当插件已经使用插件自有的认证、重定向或允许列表处理获取到 Response 时,使用 saveResponseMedia(...)。仅当插件需要原始字节进行检查、转换、解密或重新上传时,才使用 readRemoteMediaBuffer(...)。fetchRemoteMedia(...) 仍然作为 readRemoteMediaBuffer(...) 的已弃用兼容别名保留。api.runtime.channel.mentions 是使用运行时注入的打包 channel 插件共享的入站提及策略接口:buildMentionRegexesmatchesMentionPatternsmatchesMentionWithExplicitimplicitMentionKindWhenresolveInboundMentionDecision
api.runtime.channel.mentions 刻意不暴露旧的 resolveMentionGating* 兼容辅助工具。请优先使用规范化的 { facts, policy } 路径。存储运行时引用
使用createPluginRuntimeStore 在 register 回调之外存储运行时引用:
在 runtime-store 标识中优先使用
pluginId。较低层级的 key 形式适用于少数场景,即某个插件有意需要多个运行时槽位。其他顶层 api 字段
除了 api.runtime 之外,API 对象还提供:
插件 id。
插件显示名称。
当前配置快照(如可用,则为当前内存中的运行时快照)。
来自
plugins.entries.<id>.config 的插件专属配置。作用域日志记录器(
debug、info、warn、error)。当前加载模式;
"setup-runtime" 是轻量级的、完整入口点启动/设置前窗口。解析相对于插件根目录的路径。