最后更新:2025-12-09
gateway 命令。CLI 入口点:openclaw gateway。openclaw gateway --port 18789
# 在 stdio 中获取完整的调试/追踪日志:
openclaw gateway --port 18789 --verbose
# 如果端口被占用,终止监听器然后启动:
openclaw gateway --force
# 开发循环(TS 更改时自动重载):
pnpm gateway:watch
~/.openclaw/openclaw.json(或 OPENCLAW_CONFIG_PATH)。gateway.reload.mode="hybrid"(热应用安全更改,关键更改时重启)。gateway.reload.mode="off" 禁用。127.0.0.1:<port>(默认 18789)。/v1/chat/completions。/v1/responses。/tools/invoke。canvasHost.port(默认 18793)上启动 Canvas 文件服务器,从 ~/.openclaw/workspace/canvas 提供 http://<gateway-host>:18793/__openclaw__/canvas/。使用 canvasHost.enabled=false 或 OPENCLAW_SKIP_CANVAS_HOST=1 禁用。--verbose 以将调试日志(握手、请求/响应、事件)从日志文件镜像到 stdio。--force 使用 lsof 查找所选端口上的监听器,发送 SIGTERM,记录它终止了什么,然后启动 Gateway 网关(如果缺少 lsof 则快速失败)。pnpm ELIFECYCLE 退出码 143(SIGTERM),这是正常关闭,不是崩溃。commands.restart 以进行手动重启)。gateway.auth.token(或 OPENCLAW_GATEWAY_TOKEN)或 gateway.auth.password。客户端必须发送 connect.params.auth.token/password,除非使用 Tailscale Serve 身份。--port > OPENCLAW_GATEWAY_PORT > gateway.port > 默认 18789。ssh -N -L 18789:127.0.0.1:18789 user@host
ws://127.0.0.1:18789。connect.params.auth.token 中包含它。通常不需要:一个 Gateway 网关可以服务多个消息渠道和智能体。仅在需要冗余或严格隔离(例如:救援机器人)时使用多个 Gateway 网关。
如果你隔离状态 + 配置并使用唯一端口,则支持。完整指南:多个 Gateway 网关。
服务名称是配置文件感知的:
bot.molt.<profile>(旧版 com.openclaw.* 可能仍然存在)openclaw-gateway-<profile>.serviceOpenClaw Gateway (<profile>)安装元数据嵌入在服务配置中:
OPENCLAW_SERVICE_MARKER=openclawOPENCLAW_SERVICE_KIND=gatewayOPENCLAW_SERVICE_VERSION=<version>救援机器人模式:保持第二个 Gateway 网关隔离,使用自己的配置文件、状态目录、工作区和基础端口间隔。完整指南:救援机器人指南。
--dev)快速路径:运行完全隔离的 dev 实例(配置/状态/工作区)而不触及你的主设置。
openclaw --dev setup
openclaw --dev gateway --allow-unconfigured
# 然后定位到 dev 实例:
openclaw --dev status
openclaw --dev health
默认值(可通过 env/flags/config 覆盖):
OPENCLAW_STATE_DIR=~/.openclaw-devOPENCLAW_CONFIG_PATH=~/.openclaw-dev/openclaw.jsonOPENCLAW_GATEWAY_PORT=19001(Gateway 网关 WS + HTTP)19003(派生:gateway.port+2,仅 loopback)canvasHost.port=19005(派生:gateway.port+4)--dev 下运行 setup/onboard 时,agents.defaults.workspace 默认变为 ~/.openclaw/workspace-dev。派生端口(经验法则):
gateway.port(或 OPENCLAW_GATEWAY_PORT / --port)canvasHost.port = 基础 + 4(或 OPENCLAW_CANVAS_HOST_PORT / 配置覆盖)browser.controlPort + 9 .. + 108 自动分配(按配置文件持久化)。每个实例的检查清单:
gateway.portOPENCLAW_CONFIG_PATHOPENCLAW_STATE_DIRagents.defaults.workspace按配置文件安装服务:
openclaw --profile main gateway install
openclaw --profile rescue gateway install
示例:
OPENCLAW_CONFIG_PATH=~/.openclaw/a.json OPENCLAW_STATE_DIR=~/.openclaw-a openclaw gateway --port 19001
OPENCLAW_CONFIG_PATH=~/.openclaw/b.json OPENCLAW_STATE_DIR=~/.openclaw-b openclaw gateway --port 19002
req {type:"req", id, method:"connect", params:{minProtocol,maxProtocol,client:{id,displayName?,version,platform,deviceFamily?,modelIdentifier?,mode,instanceId?}, caps, auth?, locale?, userAgent? } }。res {type:"res", id, ok:true, payload:hello-ok }(或 ok:false 带错误,然后关闭)。{type:"req", id, method, params} → {type:"res", id, ok, payload|error}{type:"event", event, payload, seq?, stateVersion?}{host, ip, version, platform?, deviceFamily?, modelIdentifier?, mode, lastInputSeconds?, ts, reason?, tags?[], instanceId? }(对于 WS 客户端,instanceId 来自 connect.client.instanceId)。agent 响应是两阶段的:首先 res 确认 {runId,status:"accepted"},然后在运行完成后发送最终 res {runId,status:"ok"|"error",summary};流式输出作为 event:"agent" 到达。health — 完整健康快照(与 openclaw health --json 形状相同)。status — 简短摘要。system-presence — 当前 presence 列表。system-event — 发布 presence/系统注释(结构化)。send — 通过活跃渠道发送消息。agent — 运行智能体轮次(在同一连接上流回事件)。node.list — 列出已配对 + 当前连接的节点(包括 caps、deviceFamily、modelIdentifier、paired、connected 和广播的 commands)。node.describe — 描述节点(能力 + 支持的 node.invoke 命令;适用于已配对节点和当前连接的未配对节点)。node.invoke — 在节点上调用命令(例如 canvas.*、camera.*)。node.pair.* — 配对生命周期(request、list、approve、reject、verify)。另见:Presence 了解 presence 如何产生/去重以及为什么稳定的 client.instanceId 很重要。
agent — 来自智能体运行的流式工具/输出事件(带 seq 标记)。presence — presence 更新(带 stateVersion 的增量)推送到所有连接的客户端。tick — 定期保活/无操作以确认活跃。shutdown — Gateway 网关正在退出;payload 包括 reason 和可选的 restartExpectedMs。客户端应重新连接。connect 期间包含它。presence 事件以更新 UI。pnpm protocol:genpnpm protocol:gen:swifthello-ok 包含带有 presence、health、stateVersion 和 uptimeMs 的 snapshot,以及 policy {maxPayload,maxBufferedBytes,tickIntervalMs},这样客户端无需额外请求即可立即渲染。health/system-presence 仍可用于手动刷新,但在连接时不是必需的。{ code, message, details?, retryable?, retryAfterMs? }。NOT_LINKED — WhatsApp 未认证。AGENT_TIMEOUT — 智能体未在配置的截止时间内响应。INVALID_REQUEST — schema/参数验证失败。UNAVAILABLE — Gateway 网关正在关闭或依赖项不可用。tick 事件(或 WS ping/pong)定期发出,以便客户端知道即使没有流量时 Gateway 网关也是活跃的。health + system-presence)。WebChat 和 macOS 客户端现在会在间隙时自动刷新。openclaw 的路径gatewaysyslogopenclaw gateway install 写入 ~/Library/LaunchAgents/bot.molt.gateway.plist
(或 bot.molt.<profile>.plist;旧版 com.openclaw.* 会被清理)。openclaw doctor 审计 LaunchAgent 配置,可以将其更新为当前默认值。使用 Gateway 网关 CLI 进行 install/start/stop/restart/status:
openclaw gateway status
openclaw gateway install
openclaw gateway stop
openclaw gateway restart
openclaw logs --follow
注意事项:
gateway status 默认使用服务解析的端口/配置探测 Gateway 网关 RPC(使用 --url 覆盖)。gateway status --deep 添加系统级扫描(LaunchDaemons/系统单元)。gateway status --no-probe 跳过 RPC 探测(在网络故障时有用)。gateway status --json 对脚本是稳定的。gateway status 将 supervisor 运行时(launchd/systemd 运行中)与 RPC 可达性(WS 连接 + status RPC)分开报告。gateway status 打印配置路径 + 探测目标以避免"localhost vs LAN 绑定"混淆和配置文件不匹配。gateway status 在服务看起来正在运行但端口已关闭时包含最后一行 Gateway 网关错误。logs 通过 RPC 尾随 Gateway 网关文件日志(无需手动 tail/grep)。openclaw gateway uninstall(当前服务)和 openclaw doctor(旧版迁移)。gateway install 在已安装时是无操作的;使用 openclaw gateway install --force 重新安装(配置文件/env/路径更改)。捆绑的 mac 应用:
bot.molt.gateway(或 bot.molt.<profile>;旧版 com.openclaw.* 标签仍能干净卸载)的按用户 LaunchAgent。openclaw gateway stop(或 launchctl bootout gui/$UID/bot.molt.gateway)。openclaw gateway restart(或 launchctl kickstart -k gui/$UID/bot.molt.gateway)。launchctl 仅在 LaunchAgent 已安装时有效;否则先使用 openclaw gateway install。bot.molt.<profile>。OpenClaw 在 Linux/WSL2 上默认安装 systemd 用户服务。我们 建议单用户机器使用用户服务(更简单的 env,按用户配置)。 对于多用户或常驻服务器使用系统服务(无需 lingering, 共享监管)。
openclaw gateway install 写入用户单元。openclaw doctor 审计
单元并可以将其更新以匹配当前推荐的默认值。
创建 ~/.config/systemd/user/openclaw-gateway[-<profile>].service:
[Unit]
Description=OpenClaw Gateway (profile: <profile>, v<version>)
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/openclaw gateway --port 18789
Restart=always
RestartSec=5
Environment=OPENCLAW_GATEWAY_TOKEN=
WorkingDirectory=/home/youruser
[Install]
WantedBy=default.target
启用 lingering(必需,以便用户服务在登出/空闲后继续存活):
sudo loginctl enable-linger youruser
新手引导在 Linux/WSL2 上运行此命令(可能提示输入 sudo;写入 /var/lib/systemd/linger)。
然后启用服务:
systemctl --user enable --now openclaw-gateway[-<profile>].service
替代方案(系统服务) - 对于常驻或多用户服务器,你可以
安装 systemd 系统单元而不是用户单元(无需 lingering)。
创建 /etc/systemd/system/openclaw-gateway[-<profile>].service(复制上面的单元,
切换 WantedBy=multi-user.target,设置 User= + WorkingDirectory=),然后:
sudo systemctl daemon-reload
sudo systemctl enable --now openclaw-gateway[-<profile>].service
Windows 安装应使用 WSL2 并遵循上面的 Linux systemd 部分。
req:connect → 期望收到带有 payload.type="hello-ok"(带快照)的 res。health → 期望 ok: true 并在 linkChannel 中有已关联的渠道(适用时)。tick 和 presence 事件;确保 status 显示已关联/认证时间;presence 条目显示 Gateway 网关主机和已连接的客户端。shutdown 事件;客户端必须处理关闭 + 重新连接。openclaw gateway health|status — 通过 Gateway 网关 WS 请求 health/status。openclaw message send --target <num> --message "hi" [--media ...] — 通过 Gateway 网关发送(对 WhatsApp 是幂等的)。openclaw agent --message "hi" --to <num> — 运行智能体轮次(默认等待最终结果)。openclaw gateway call <method> --params '{"k":"v"}' — 用于调试的原始方法调用器。openclaw gateway stop|restart — 停止/重启受监管的 Gateway 网关服务(launchd/systemd)。--url 上有运行中的 Gateway 网关;它们不再自动生成一个。openclaw gateway 和旧版 TCP 控制端口的使用。