ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

oh-my-openagent 中的 openclaw-core:OpenClaw 双向网关与回复监听守护进程深度解析

oh-my-openagent 中的 openclaw-core:OpenClaw 双向网关与回复监听守护进程深度解析 人工智能AI Agent代码智能体多智能体MCP ClientsAgent 编排【免费下载链接】oh-my-openagentOmO: Just type mass ulw keyword with your prompt. Now you are the master of graph engineering.项目地址https://gitcode.com/gh_mirrors/oh/oh-my-openagent点击查看免费下载oh-my-opencode/openclaw-core是 oh-my-openagent 项目中负责 OpenClaw 双向集成的核心包出站方向把会话事件以 HTTP Webhook 或 Shell 命令的形式派发给外部 OpenClaw 网关入站方向由独立的回复监听守护进程轮询 Discord/Telegram 消息并把回复注入回跟踪的 tmux 窗格。本文以 packages/openclaw-core/AGENTS.md 为骨架结合源码与测试完整讲解该包的配置模型、出站派发链路、入站守护进程生命周期、会话注册表、注入安全机制并给出可直接套用的配置示例。包定位Harness-Neutral 的双向集成核心openclaw-core的核心设计原则是harness-neutral与宿主无关它不依赖任何特定的 Agent 前端只定义通用的 OpenClaw 集成原语由宿主适配器按需消费。在 oh-my-openagent 中OpenCode 适配器位于 packages/omo-opencode/src/openclaw/它通过 re-export shim 引用本包能力// packages/omo-opencode/src/openclaw/config.ts export * from oh-my-opencode/openclaw-core/config从包声明看packages/openclaw-core/package.json 的描述为 Harness-neutral OpenClaw gateway, reply-listener daemon, session registry, and tmux injection primitives仅依赖oh-my-opencode/tmux-core与oh-my-opencode/utils两个 workspace 包。整体能力可概括为两条链路出站Outbound会话事件触发wakeOpenClaw()按hooks[event]解析出网关与指令模板最终以 HTTP POST 或 Shell 命令方式派发入站InboundstartReplyListener()拉起一个分离的 Bun 守护进程轮询 Discord/Telegram查会话注册表定位目标 tmux 窗格经过限流与净化后把回复send-keys注入窗格。核心类型与配置模型配置模型定义在 packages/openclaw-core/src/types.ts顶层结构为export type OpenClawConfig { readonly enabled: boolean readonly gateways: Recordstring, OpenClawGateway readonly hooks: Recordstring, OpenClawHook readonly replyListener?: OpenClawReplyListenerConfig }网关Gatewayexport type OpenClawGateway { readonly type?: http | command readonly url?: string readonly method?: string readonly headers?: Recordstring, string readonly command?: string readonly timeout?: number }type缺省按http处理HTTP 网关走urlPOST JSON命令网关走commandsh -c执行。Zod schemapackages/omo-opencode/src/config/schema/openclaw.ts给出默认method: POST。钩子Hookexport type OpenClawHook { readonly enabled?: boolean readonly gateway: string readonly instruction: string }hooks以会话事件名为键instruction是携带{{变量}}占位符的指令模板gateway引用gateways中的某个网关名。事件名包括session-start、session-end、stop等详见下文事件映射。回复监听配置Reply Listenerexport type OpenClawReplyListenerConfig { readonly discordBotToken?: string readonly discordChannelId?: string readonly discordMention?: string readonly authorizedDiscordUserIds?: readonly string[] readonly telegramBotToken?: string readonly telegramChatId?: string readonly pollIntervalMs?: number readonly rateLimitPerMinute?: number readonly maxMessageLength?: number readonly includePrefix?: boolean }事件上下文与负载OpenClawContext是白名单化的事件上下文包含sessionId、projectPath、prompt、contextSummary、reasoning、question、tmuxTail、replyChannel、replyTarget、replyThread等可选字段。OpenClawPayload是 HTTP 网关收到的 JSON 负载除上述字段外还有event、instruction、text、timestamp并把回复相关字段映射为channel、to、threadId见 packages/openclaw-core/src/index.ts。出站链路从会话事件到网关派发事件映射与派发编排packages/openclaw-core/src/runtime-dispatch.ts 的dispatchOpenClawEvent()是出站入口先做事件别名映射const aliases: Recordstring, string { session.created: session-start, session.deleted: session-end, session.idle: stop, }每个原始事件会被映射为去重后的 12 个事件名依次尝试派发任一事件派发成功即停止。派发成功后若结果携带messageId与platform且上下文包含sessionId、projectPath、tmuxPaneId则调用registerMessage()把消息 ID ↔ 会话 ID ↔ tmux 窗格写入会话注册表——这是入站回复能定位目标窗格的关键而session.deleted事件则会触发removeSession()清理注册表。网关解析packages/openclaw-core/src/config.ts 的resolveGateway()遵循严格的三段解析config.enabled为真 →hooks[event]存在且enabled→gateways[mapping.gateway]存在且具备有效urlhttp或commandcommand任一环节缺失即返回null派发静默跳过。变量插值packages/openclaw-core/src/dispatcher.ts 的interpolateInstruction()用正则/\{\{(\w)\}\}/g替换模板中的{{变量}}。wakeOpenClaw()在 packages/openclaw-core/src/index.ts 中构建的完整变量表包括变量来源sessionId/projectPath/projectName上下文projectName由basename(projectPath)推导tmuxSession上下文缺省时从TMUX环境变量推断prompt/contextSummary/reasoning/question上下文tmuxTail上下文事件为stop或session-end时自动捕获当前窗格最近 15 行event/timestamp派发事件与 ISO 时间戳replyChannel/replyTarget/replyThread上下文缺省时回退到环境变量OPENCLAW_REPLY_CHANNEL/OPENCLAW_REPLY_TARGET/OPENCLAW_REPLY_THREAD回复定位信息频道、目标、线程因此既可通过上下文注入也可通过环境变量全局配置为两种接入方式提供了统一入口。HTTP 网关HTTPS 校验、超时与响应元数据wakeGateway()packages/openclaw-core/src/dispatcher.ts先执行 URL 校验不通过直接返回success: false, error: Invalid URL (HTTPS required)。校验逻辑见 packages/openclaw-core/src/gateway-url-validation.ts仅允许https:协议例外是localhost、127.0.0.1、::1的http:其余一律拒绝。默认超时 10 秒DEFAULT_HTTP_TIMEOUT_MS 10_000通过AbortController实现。派发成功后会解析响应文本中的回复元数据parseWakeMetadata()先尝试 JSON.parse并在一组嵌套候选data/result/message等中按messageId、platform、channelId、threadId的加权打分选出最佳匹配JSON 解析失败时退化为正则提取message id: ...与sent via ...。这些元数据最终成为WakeResult的一部分供runtime-dispatch.ts判断是否需要登记回复关联。命令网关Shell 执行与安全转义wakeCommandGateway()packages/openclaw-core/src/dispatcher.ts把命令模板插值后交给sh -c执行。变量值经shellEscapeArg()转义单引号包裹、内部单引号以\转义避免注入命令超时由resolveCommandTimeoutMs()解析优先级为网关timeout字段 → 环境变量OMO_OPENCLAW_COMMAND_TIMEOUT_MS→ 默认 5000ms并钳制在 100ms300000ms 之间。超时后terminateCommandProcess()在非 Windows 平台先尝试按进程组kill(-pid, SIGKILL)失败再回退直接 kill确保整棵命令进程树被回收。入站链路Reply Listener 守护进程生命周期与启动流程startReplyListener()packages/openclaw-core/src/reply-listener-start.ts的启动流程严格有序归一化配置normalizeReplyListenerConfig若既无 Discord token 也无 Telegram token直接返回失败若守护进程已在运行比较持久化的configSignature与当前配置签名相同则复用不同则先停止再重启检查tmux可用性——回复注入依赖 tmux不可用则启动失败持久化 daemon 配置与待就绪状态生成一次性startupToken以分离进程方式 spawn daemon.tsunref()脱离父进程写入 PID 文件在启动超时窗口内轮询 daemon 状态直至就绪超时则终止进程并清理。守护进程的持久化状态由 packages/openclaw-core/src/reply-listener-state.ts 管理PID、配置签名、最近轮询时间、Discord/Telegram 游标、错误计数等这是守护进程可跨主机生命周期恢复、可安全重启的关键。轮询循环pollLoop() 是守护进程主体每轮先写轮询状态再依次调用pollDiscordReplies()与pollTelegramReplies()默认间隔 3000mspollIntervalMs可配置范围 500ms60000ms发生异常时错误计数 1 并写入状态退避为两倍间隔后重试每隔 1 小时PRUNE_INTERVAL_MS 60 * 60 * 1000执行一次pruneStale()清理过期注册项注册SIGTERM/SIGINT处理置状态为停止、删除 PID、优雅退出。Discord 轮询与限流退避pollDiscordReplies()packages/openclaw-core/src/reply-listener-discord.ts请求https://discord.com/api/v10/channels/{channelId}/messages使用游标afterdiscordLastMessageId增量拉取并通过x-ratelimit-remaining头探测 Discord 限流水位——剩余配额低于 2 时按x-ratelimit-reset时间戳全局退避。同时要求authorizedDiscordUserIds非空未配置授权用户白名单时直接跳过 Discord 轮询。会话注册表文件锁保护的 JSONLpackages/openclaw-core/src/session-registry.ts 实现的注册表用于消息 ID ↔ 会话 ID ↔ tmux 窗格关联registerMessage()以追加模式O_APPEND | O_CREAT写入 JSONL文件权限使用安全模式写入全程持有文件锁lookupByMessageId(platform, messageId)入站回复按平台与消息 ID 反查目标窗格removeSession()/removeMessagesByPane()会话结束或窗格置信度不足时按维度清理pruneStale()按MAX_AGE_MS剔除超龄条目。文件锁与存储实现分别在 session-registry-lock.ts 与 session-registry-storage.ts写入类型SessionMapping定义见 session-registry-types.ts含platform、messageId、channelId、threadId、createdAt等。回复注入置信度检查、净化、限流与 send-keys原文档给出的入站链路 FLOW 如下这是理解注入流程的主干Discord/Telegram API → reply-listener-poll-loop.ts → session-registry.ts: lookup tmux pane by message ID → reply-listener-injection.ts: rate limit check → sanitize → send-keys into paneinjectReplyIntoPane() 依次执行四步置信度检查捕获窗格最近 15 行内容analyzePaneContent()计算置信度低于 0.3 判定窗格未运行 OpenCode CLI跳过注入并移除该窗格的过期映射前缀includePrefix ! false时拼接[reply:{platform}]前缀净化sanitizeReplyInput()剔除终端控制字符与 Unicode 双向控制符、换行转空格并转义\、反引号、$(、${防止注入内容触发 shell/终端副作用限流与截断ReplyListenerRateLimiter以 60 秒滑动窗口计数默认每窗格每分钟最多 10 次注入rateLimitPerMinute文本按maxMessageLength默认 500上限 4000截断。最终由 packages/openclaw-core/src/tmux.ts 的sendToPane()执行先send-keys -l字面量发送文本不做键盘映射解释再发送Enter确认。窗格捕获对应capture-pane -p -t {paneId} -S -{lines}会话名通过display-message -p #S获取所有 tmux 命令经oh-my-opencode/tmux-core的runTmuxCommand执行TMUX环境变量缺失时推断当前会话返回null。配置归一化与默认值速查normalizeReplyListenerConfig()packages/openclaw-core/src/config.ts对数值参数做钳制归一化非数值回退默认值配置项默认值取值范围说明pollIntervalMs300050060000轮询间隔毫秒rateLimitPerMinute10≥1每窗格每分钟最大注入次数maxMessageLength50014000注入文本最大长度includePrefixtrue布尔是否加[reply:platform]前缀false才关闭authorizedDiscordUserIds[]字符串数组过滤空串为空则禁用 Discord 轮询配置归一化在启动守护进程前执行见startReplyListener第一步保证写入 daemon 的配置与运行期行为一致。Zod schema 侧的默认值enabled: false、method: POST等见 packages/omo-opencode/src/config/schema/openclaw.ts与核心包归一化逻辑相互印证。完整配置示例综合 packages/omo-opencode/src/config/schema/openclaw.ts 与核心包类型一份同时启用出站 HTTP 网关与 Telegram 回复监听的配置如下{ openclaw: { enabled: true, gateways: { bot-gateway: { type: http, url: https://your-openclaw-host.example.com/hook, method: POST, headers: { Authorization: Bearer secret }, timeout: 15000 }, notify-command: { type: command, command: notify-send {{event}} {{projectName}}, timeout: 5000 } }, hooks: { session-start: { enabled: true, gateway: bot-gateway, instruction: 会话 {{sessionId}} 在项目 {{projectName}} 启动提示词{{prompt}} }, session-end: { enabled: true, gateway: notify-command, instruction: 会话结束 } }, replyListener: { telegramBotToken: bot-token, telegramChatId: chat-id, pollIntervalMs: 3000, rateLimitPerMinute: 10, maxMessageLength: 500, includePrefix: true } } }要点HTTP 网关 URL 必须是 HTTPS 或 localhostsession-end事件会自动附加{{tmuxTail}}供指令模板使用若以 Discord 作为回复通道务必配置authorizedDiscordUserIds否则轮询不会生效。安全设计汇总URL 白名单协议非 localhost 强制 HTTPSgateway-url-validation.ts命令转义命令网关变量值经shellEscapeArg单引号转义超时强制 SIGKILL 进程组dispatcher.ts授权用户白名单authorizedDiscordUserIds为空即不轮询 Discord注入净化剥离控制字符/方向控制符并转义 shell 元字符reply-listener-injection.ts注入限流每窗格每分钟默认 10 次防止消息风暴打崩终端上下文白名单HTTP 负载中的context仅包含白名单字段packages/openclaw-core/src/index.ts其余上下文字段不会外泄。测试覆盖与验证路径包内测试与源码同目录存放覆盖各关键环节可作为行为契约参考config.test.ts配置归一化与网关解析dispatcher.test.tsHTTP/命令网关、插值与转义gateway-url-validation.test.tsHTTPS/locahost 校验规则runtime-dispatch.test.ts事件映射与回复关联登记session-registry.test.tsJSONL 注册表增删查与清理reply-listener-injection.test.ts净化、限流与注入reply-listener-discord.test.tsDiscord 轮询与游标推进tmux.test.ts窗格捕获与 send-keys守护进程生命周期相关reply-listener-startup.test.ts、reply-listener-process.test.ts、reply-listener-state-stop.test.ts、reply-listener-restart-runtime-signature.test.ts。运行测试bun test src/*.test.ts src/**/*.test.ts见 package.json 的 scripts。关键文件索引文件职责src/index.tsBarrelwakeOpenClaw()、initializeOpenClaw()、startReplyListener、stopReplyListenersrc/types.tsOpenClawConfig、OpenClawPayload、WakeResult等类型src/config.ts网关解析 回复监听配置归一化src/dispatcher.tsHTTP POST 与 Shell 命令执行、变量插值、超时控制src/runtime-dispatch.ts事件映射与派发编排、回复关联登记src/session-registry.ts文件锁 JSONL 注册表消息 ID ↔ 会话 ↔ 窗格src/reply-listener.ts守护进程生命周期 barrelsrc/reply-listener-start.ts以分离 Bun 进程启动守护进程src/reply-listener-poll-loop.ts每 3 秒轮询 Discord/Telegram每小时清理过期条目src/reply-listener-injection.ts限流、净化与 tmux 注入src/tmux.tscaptureTmuxPane()、sendToPane()、analyzePaneContent()src/daemon.ts分离守护进程入口整体来看openclaw-core 通过出站派发 入站监听 注册表关联 tmux 注入四个模块把外部 IM 平台与终端内的 Agent 会话闭环打通会话事件出站通知外部网关外部用户的回复再经守护进程精准注入回对应窗格而 HTTPS 强制校验、授权白名单、输入净化、限流与置信度检查共同构成了这条双向通道的安全边界。赞分享人工智能AI Agent代码智能体多智能体MCP ClientsAgent 编排【免费下载链接】oh-my-openagentOmO: Just type mass ulw keyword with your prompt. Now you are the master of graph engineering.项目地址https://gitcode.com/gh_mirrors/oh/oh-my-openagent点击查看免费下载相关推荐oh-my-openagent 的 openclaw 双向外部集成事件通知网关与回复注入守护进程全解析oh my openagent 的 openclaw 双向外部集成事件通知网关与回复注入守护进程全解析 导读 packages/omo opencode/sr人工智能AI Agent代码智能体多智能体MCP ClientsAgent 编排oh-my-posh 的 zsh 集成深度解析zle widget、coproc 守护进程与 GLOB_SUBST 陷阱oh my posh 的 zsh 集成深度解析zle widget、coproc 守护进程与 GLOB_SUBST 陷阱 Oh My Posh 在 zsh 上CLI开发工具oh-my-openagent omo-config-core 深度解析Harness 无关的 omo.json 配置核心oh my openagent omo config core 深度解析Harness 无关的 omo.json 配置核心 本文围绕 oh my openag人工智能AI Agent代码智能体多智能体MCP ClientsAgent 编排创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表