clawconnect-agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/.env.example +41 -0
  2. package/README.md +259 -0
  3. package/README.zh-CN.md +329 -0
  4. package/dist/commands/backup-manager.d.ts +21 -0
  5. package/dist/commands/backup-manager.js +346 -0
  6. package/dist/commands/backup-manager.js.map +1 -0
  7. package/dist/commands/install.d.ts +6 -0
  8. package/dist/commands/install.js +107 -0
  9. package/dist/commands/install.js.map +1 -0
  10. package/dist/commands/local-handlers.d.ts +2 -0
  11. package/dist/commands/local-handlers.js +375 -0
  12. package/dist/commands/local-handlers.js.map +1 -0
  13. package/dist/commands/local-runtime.d.ts +36 -0
  14. package/dist/commands/local-runtime.js +445 -0
  15. package/dist/commands/local-runtime.js.map +1 -0
  16. package/dist/commands/pair.d.ts +7 -0
  17. package/dist/commands/pair.js +100 -0
  18. package/dist/commands/pair.js.map +1 -0
  19. package/dist/commands/provider-config.d.ts +35 -0
  20. package/dist/commands/provider-config.js +291 -0
  21. package/dist/commands/provider-config.js.map +1 -0
  22. package/dist/commands/provider-handlers.d.ts +2 -0
  23. package/dist/commands/provider-handlers.js +216 -0
  24. package/dist/commands/provider-handlers.js.map +1 -0
  25. package/dist/commands/provider-registry.d.ts +19 -0
  26. package/dist/commands/provider-registry.js +120 -0
  27. package/dist/commands/provider-registry.js.map +1 -0
  28. package/dist/commands/run.d.ts +1 -0
  29. package/dist/commands/run.js +35 -0
  30. package/dist/commands/run.js.map +1 -0
  31. package/dist/commands/send-file-utils.d.ts +6 -0
  32. package/dist/commands/send-file-utils.js +94 -0
  33. package/dist/commands/send-file-utils.js.map +1 -0
  34. package/dist/commands/send-file.d.ts +41 -0
  35. package/dist/commands/send-file.js +492 -0
  36. package/dist/commands/send-file.js.map +1 -0
  37. package/dist/commands/set-token.d.ts +1 -0
  38. package/dist/commands/set-token.js +43 -0
  39. package/dist/commands/set-token.js.map +1 -0
  40. package/dist/commands/status.d.ts +1 -0
  41. package/dist/commands/status.js +135 -0
  42. package/dist/commands/status.js.map +1 -0
  43. package/dist/commands/voice-reply.d.ts +39 -0
  44. package/dist/commands/voice-reply.js +329 -0
  45. package/dist/commands/voice-reply.js.map +1 -0
  46. package/dist/config/config.d.ts +32 -0
  47. package/dist/config/config.js +103 -0
  48. package/dist/config/config.js.map +1 -0
  49. package/dist/config/env.d.ts +16 -0
  50. package/dist/config/env.js +138 -0
  51. package/dist/config/env.js.map +1 -0
  52. package/dist/i18n/index.d.ts +1 -0
  53. package/dist/i18n/index.js +147 -0
  54. package/dist/i18n/index.js.map +1 -0
  55. package/dist/index.d.ts +2 -0
  56. package/dist/index.js +117 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/platform/service-manager-common.d.ts +26 -0
  59. package/dist/platform/service-manager-common.js +51 -0
  60. package/dist/platform/service-manager-common.js.map +1 -0
  61. package/dist/platform/service-manager-linux.d.ts +6 -0
  62. package/dist/platform/service-manager-linux.js +229 -0
  63. package/dist/platform/service-manager-linux.js.map +1 -0
  64. package/dist/platform/service-manager.d.ts +16 -0
  65. package/dist/platform/service-manager.js +165 -0
  66. package/dist/platform/service-manager.js.map +1 -0
  67. package/dist/relay/attachment-staging.d.ts +12 -0
  68. package/dist/relay/attachment-staging.js +116 -0
  69. package/dist/relay/attachment-staging.js.map +1 -0
  70. package/dist/relay/chat-history.d.ts +29 -0
  71. package/dist/relay/chat-history.js +69 -0
  72. package/dist/relay/chat-history.js.map +1 -0
  73. package/dist/relay/chat-payload.d.ts +6 -0
  74. package/dist/relay/chat-payload.js +170 -0
  75. package/dist/relay/chat-payload.js.map +1 -0
  76. package/dist/relay/chat-send-attachments.d.ts +4 -0
  77. package/dist/relay/chat-send-attachments.js +47 -0
  78. package/dist/relay/chat-send-attachments.js.map +1 -0
  79. package/dist/relay/gateway-client.d.ts +41 -0
  80. package/dist/relay/gateway-client.js +288 -0
  81. package/dist/relay/gateway-client.js.map +1 -0
  82. package/dist/relay/office-payload.d.ts +19 -0
  83. package/dist/relay/office-payload.js +205 -0
  84. package/dist/relay/office-payload.js.map +1 -0
  85. package/dist/relay/reconnect.d.ts +14 -0
  86. package/dist/relay/reconnect.js +27 -0
  87. package/dist/relay/reconnect.js.map +1 -0
  88. package/dist/relay/relay-manager.d.ts +36 -0
  89. package/dist/relay/relay-manager.js +619 -0
  90. package/dist/relay/relay-manager.js.map +1 -0
  91. package/dist/relay/session-context.d.ts +19 -0
  92. package/dist/relay/session-context.js +323 -0
  93. package/dist/relay/session-context.js.map +1 -0
  94. package/dist/relay/slash-command-catalog.d.ts +8 -0
  95. package/dist/relay/slash-command-catalog.generated.d.ts +226 -0
  96. package/dist/relay/slash-command-catalog.generated.js +275 -0
  97. package/dist/relay/slash-command-catalog.generated.js.map +1 -0
  98. package/dist/relay/slash-command-catalog.js +3 -0
  99. package/dist/relay/slash-command-catalog.js.map +1 -0
  100. package/dist/relay/voice-reply-preference.d.ts +25 -0
  101. package/dist/relay/voice-reply-preference.js +103 -0
  102. package/dist/relay/voice-reply-preference.js.map +1 -0
  103. package/package.json +50 -0
package/.env.example ADDED
@@ -0,0 +1,41 @@
1
+ # ClawConnect Agent environment configuration.
2
+ # Copy this file to ~/.clawconnect/.env for an installed agent.
3
+ # For local development from this package directory, copy it to .env.local.
4
+ #
5
+ # Precedence:
6
+ # 1. Shell environment variables
7
+ # 2. CLAWCONNECT_ENV_FILE, if set
8
+ # 3. ~/.clawconnect/.env
9
+ # 4. .env.local
10
+ # 5. .env
11
+ # 6. Built-in defaults
12
+ #
13
+ # Existing pairing credentials in ~/.clawconnect/config.json still control
14
+ # `clawconnect run`, `status`, and `send-file`. Use `clawconnect pair --server`
15
+ # or `clawconnect reset` when switching relay servers.
16
+
17
+ # Default relay used by `clawconnect pair` when --server is omitted.
18
+ CLAWCONNECT_RELAY_SERVER_URL=https://clawlinks.cn
19
+
20
+ # Optional: explicitly select another env file.
21
+ # Usually set this in your shell, not inside ~/.clawconnect/.env.
22
+ # CLAWCONNECT_ENV_FILE=/Users/you/.clawconnect/.env
23
+
24
+ # Optional: force the local OpenClaw Gateway websocket URL instead of reading
25
+ # ~/.openclaw/openclaw.json and falling back to ws://localhost:18789.
26
+ # CLAWCONNECT_GATEWAY_URL=ws://localhost:18789
27
+
28
+ # Optional: Gateway auth fallback.
29
+ # Prefer `clawconnect set-token` or ~/.openclaw/openclaw.json when possible.
30
+ # OPENCLAW_GATEWAY_TOKEN=
31
+ # OPENCLAW_GATEWAY_PASSWORD=
32
+
33
+ # Optional: assistant voice replies.
34
+ # OPENCLAW_TTS_ENABLED=0
35
+ # OPENCLAW_TTS_VOICE=Ting-Ting
36
+ # OPENCLAW_TTS_RATE=
37
+ # OPENCLAW_TTS_ENGINE=
38
+
39
+ # Optional: override how local maintenance commands find the OpenClaw CLI.
40
+ # OPENCLAW_BIN=/usr/local/bin/openclaw
41
+ # OPENCLAW_PACKAGE_BIN=/path/to/openclaw/package/dist/index.js
package/README.md ADDED
@@ -0,0 +1,259 @@
1
+ # ClawConnect Agent
2
+
3
+ > OpenClaw host agent for macOS and Linux hosts — connects your gateway machine to your ClawConnect relay server.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -g clawconnect-agent
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Pair
14
+
15
+ Generate a QR code for mobile pairing:
16
+
17
+ ```bash
18
+ clawconnect pair
19
+ ```
20
+
21
+ Print only the pairing code without rendering a QR code:
22
+
23
+ ```bash
24
+ clawconnect pair --code-only
25
+ ```
26
+
27
+ Options:
28
+ - `-n, --name <name>` — Display name for this host
29
+ - `-s, --server <url>` — Relay server URL
30
+ - `--code-only` — Print only the access code and skip QR code output
31
+
32
+ ### Environment Configuration
33
+
34
+ For an installed agent, the CLI creates `~/.clawconnect/.env` with all configurable values documented as comments the first time `clawconnect` runs. You can edit that file to keep defaults in one place:
35
+
36
+ ```bash
37
+ $EDITOR ~/.clawconnect/.env
38
+ ```
39
+
40
+ When developing from this package directory, you can copy `.env.example` to `.env.local`. Existing `~/.clawconnect/.env` files are never overwritten automatically.
41
+
42
+ Supported values:
43
+
44
+ - `CLAWCONNECT_RELAY_SERVER_URL` — default relay URL used by `clawconnect pair` when `--server` is omitted
45
+ - `CLAWCONNECT_GATEWAY_URL` — optional local Gateway websocket URL override
46
+ - `CLAWCONNECT_ENV_FILE` — optional explicit env file path. When unset, the agent reads `~/.clawconnect/.env`, then `.env.local`, then `.env`
47
+ - `OPENCLAW_GATEWAY_TOKEN` / `OPENCLAW_GATEWAY_PASSWORD` — Gateway auth fallback
48
+ - `OPENCLAW_TTS_ENABLED`, `OPENCLAW_TTS_VOICE`, `OPENCLAW_TTS_RATE`, `OPENCLAW_TTS_ENGINE` — voice reply defaults
49
+
50
+ Shell environment variables take priority over env files. Existing pairing credentials in `~/.clawconnect/config.json` still take priority for `clawconnect run`, `status`, and `send-file`; run `clawconnect pair --server <url>` or `clawconnect reset` when you intentionally switch relay servers.
51
+
52
+ ### Run
53
+
54
+ Start the host agent:
55
+
56
+ ```bash
57
+ clawconnect run
58
+ ```
59
+
60
+ Optional: enable assistant voice replies only when you want audio output by setting:
61
+
62
+ ```bash
63
+ OPENCLAW_TTS_ENABLED=1 clawconnect run
64
+ ```
65
+
66
+ When this flag is not set, assistant replies stay text-only.
67
+
68
+ ### Check Status
69
+
70
+ Show pairing config and background service status:
71
+
72
+ ```bash
73
+ clawconnect status
74
+ ```
75
+
76
+ ### Install Background Service
77
+
78
+ Install as a background service:
79
+
80
+ ```bash
81
+ clawconnect install
82
+ ```
83
+
84
+ - macOS: installs a `launchd` user agent
85
+ - Linux: prefers `systemd --user`, and falls back to `nohup` when `systemctl --user` is unavailable
86
+
87
+ On Linux hosts without `systemd --user`, `clawconnect install` will generate a fallback launcher at:
88
+
89
+ ```bash
90
+ ~/.clawconnect/clawconnect-start.sh
91
+ ```
92
+
93
+ You can start it manually with:
94
+
95
+ ```bash
96
+ bash ~/.clawconnect/clawconnect-start.sh
97
+ ```
98
+
99
+ ### Restart Service
100
+
101
+ Restart the background service:
102
+
103
+ ```bash
104
+ clawconnect restart
105
+ ```
106
+
107
+ ### Stop Service
108
+
109
+ Stop the host agent background service:
110
+
111
+ ```bash
112
+ clawconnect stop
113
+ ```
114
+
115
+ ### Set Gateway Token
116
+
117
+ Store the OpenClaw Gateway token locally when token auth is enabled:
118
+
119
+ ```bash
120
+ clawconnect set-token
121
+ ```
122
+
123
+ ### Remove Service
124
+
125
+ Remove the background service (keeps config):
126
+
127
+ ```bash
128
+ clawconnect uninstall
129
+ ```
130
+
131
+ ### Reset Pairing
132
+
133
+ Stop the service and clear local pairing config:
134
+
135
+ ```bash
136
+ clawconnect reset
137
+ ```
138
+
139
+ ### Send Files / Images
140
+
141
+ Send a local file into the paired chat session:
142
+
143
+ ```bash
144
+ clawconnect send-file ~/Pictures/demo.jpg
145
+ ```
146
+
147
+ Options:
148
+ - `-g, --gateway <id>` — Override the gateway ID from local config
149
+ - `-s, --session <key>` — Target chat session key. If omitted, the latest active session is used
150
+ - `--json` — Print the upload result as JSON
151
+
152
+ Notes:
153
+ - `send-file` uploads through the relay and posts a file message to mobile.
154
+ - Image MIME types render as preview cards in the iPhone chat UI.
155
+ - `chat.send` attachments are local staging references, not the cross-device file transfer path.
156
+ - Voice replies are disabled by default; set `OPENCLAW_TTS_ENABLED=1` to have the agent synthesize assistant replies as audio files.
157
+ - The agent now prefers `edge-tts-universal` for synthesis and falls back to the computer's built-in TTS on failure.
158
+
159
+ ## Project Structure
160
+
161
+ ```text
162
+ clawconnect-agent/
163
+ README.md
164
+ README.zh-CN.md
165
+ package.json
166
+ tsconfig.json
167
+ tsconfig.build.json
168
+ src/
169
+ index.ts
170
+ commands/
171
+ pair.ts
172
+ run.ts
173
+ send-file.ts
174
+ status.ts
175
+ install.ts
176
+ set-token.ts
177
+ local-handlers.ts
178
+ local-runtime.ts
179
+ provider-handlers.ts
180
+ provider-config.ts
181
+ provider-registry.ts
182
+ backup-manager.ts
183
+ send-file-utils.ts
184
+ *.test.ts
185
+ config/
186
+ config.ts
187
+ env.ts
188
+ i18n/
189
+ index.ts
190
+ platform/
191
+ service-manager.ts
192
+ service-manager-common.ts
193
+ service-manager-linux.ts
194
+ *.test.ts
195
+ relay/
196
+ gateway-client.ts
197
+ relay-manager.ts
198
+ session-context.ts
199
+ chat-payload.ts
200
+ chat-history.ts
201
+ attachment-staging.ts
202
+ chat-send-attachments.ts
203
+ reconnect.ts
204
+ *.test.ts
205
+ ```
206
+
207
+ Tests are colocated with the source as `*.test.ts`. They run with `npm test`, and the build/publish flow excludes them from the package tarball.
208
+
209
+ ## Key Modules and Responsibilities
210
+
211
+ - `src/index.ts` is the CLI entrypoint. It wires every command together and handles top-level errors.
212
+ - `src/commands/pair.ts` registers a host, saves relay config, and installs the background service after pairing.
213
+ - `src/commands/run.ts` starts the relay loop in the foreground for service-managed or debug runs.
214
+ - `src/commands/send-file.ts` uploads local files or images into the paired chat session.
215
+ - `src/commands/install.ts` manages service install, restart, stop, uninstall, and reset flows.
216
+ - `src/commands/set-token.ts` stores a local Gateway token when token auth is required.
217
+ - `src/commands/local-handlers.ts` handles legacy command prefixes and local maintenance actions such as backup, restore, logs, doctor, and gateway restart.
218
+ - `src/commands/local-runtime.ts` resolves the `openclaw` binary and triggers gateway lifecycle actions.
219
+ - `src/commands/provider-handlers.ts` routes provider-specific commands and reuses the same gateway restart path.
220
+ - `src/config/env.ts` loads `.env` files and exposes agent defaults such as relay and Gateway URLs.
221
+ - `src/platform/service-manager.ts` exposes a cross-platform service facade for macOS and Linux.
222
+ - `src/relay/relay-manager.ts` bridges the relay server and the local Gateway, dispatches commands, and handles chat/history fallbacks.
223
+ - `src/relay/gateway-client.ts` manages the websocket connection to OpenClaw Gateway and device authentication.
224
+ - `src/relay/session-context.ts` reads session defaults and context usage snapshots.
225
+ - `src/relay/chat-payload.ts`, `src/relay/chat-history.ts`, `src/relay/attachment-staging.ts`, and `src/relay/chat-send-attachments.ts` handle chat normalization, fallback, and attachment staging.
226
+ - `src/config/config.ts` reads and writes local pairing config under `~/.clawconnect`.
227
+ - `src/i18n/index.ts` contains the localized CLI strings.
228
+
229
+ ## Key Functions
230
+
231
+ - `pairCommand()` registers a host, generates the pairing payload, and persists relay config.
232
+ - `runCommand()` starts the foreground relay client loop used by the background service.
233
+ - `sendFileCommand()` stages a local file and uploads it into the paired chat session.
234
+ - `installCommand()`, `restartCommand()`, `stopCommand()`, `uninstallCommand()`, and `resetCommand()` manage the service lifecycle.
235
+ - `statusCommand()` prints pairing config, gateway state, and service health.
236
+ - `setTokenCommand()` stores the local OpenClaw Gateway token.
237
+ - `handleLocalCommand()` and `handleProviderCommand()` process local control-plane commands before forwarding to the gateway.
238
+ - `OpenClawGatewayClient` manages the gateway websocket, reconnects, and request/response frames.
239
+ - `canonicalizeRelayParams()`, `extractGatewaySessionDefaults()`, and `buildContextUsageFingerprint()` normalize relay protocol payloads.
240
+ - `normalizeChatEventPayload()`, `extractChatText()`, `extractHistoryOutcome()`, and `withMessageText()` keep chat payloads stable across relay and gateway responses.
241
+ - `buildAttachmentStagingPath()` and `resolveAttachmentFileName()` manage file staging for attachments.
242
+ - `getServiceStatus()`, `installService()`, `restartService()`, `stopService()`, and `uninstallService()` implement platform-specific service control.
243
+
244
+ ## How It Works
245
+
246
+ 1. **Pair** — Generate a QR code
247
+ 2. **Scan QR with mobile app** — iOS or Android app pairs with your host
248
+ 3. **Run** — Host agent stays connected to your relay server
249
+ 4. **Communicate** — Mobile app sends commands through the relay to OpenClaw
250
+ 5. **Send files** — Use `clawconnect send-file <path>` to deliver a local file or image into the chat session
251
+
252
+ ## Requirements
253
+
254
+ - macOS or Linux
255
+ - Node.js 18+
256
+
257
+ ## License
258
+
259
+ MIT
@@ -0,0 +1,329 @@
1
+ # ClawConnect Agent 使用说明
2
+
3
+ `ClawConnect Agent` 是运行在 macOS 或 Linux 主机上的 OpenClaw 远程连接代理,用于把本机 OpenClaw Gateway 接入你的中继站,供移动端远程访问。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install -g clawconnect-agent
9
+ ```
10
+
11
+ 安装完成后可使用命令:
12
+
13
+ ```bash
14
+ clawconnect --help
15
+ ```
16
+
17
+ ## 使用方式
18
+
19
+ ### 1. 配对
20
+
21
+ 生成移动端扫码配对用的二维码:
22
+
23
+ ```bash
24
+ clawconnect pair
25
+ ```
26
+
27
+ 如果只想输出配对码,不打印二维码:
28
+
29
+ ```bash
30
+ clawconnect pair --code-only
31
+ ```
32
+
33
+ 可选参数:
34
+
35
+ - `-n, --name <name>`:指定这台主机在移动端显示的名称
36
+ - `-s, --server <url>`:指定中继服务器地址
37
+ - `--code-only`:只输出访问码,不打印二维码
38
+
39
+ ### 环境配置
40
+
41
+ 本地安装后的 agent 会在第一次运行 `clawconnect` 时自动创建 `~/.clawconnect/.env`,里面会用注释列出所有可配置项。你可以直接编辑这个文件集中维护默认配置:
42
+
43
+ ```bash
44
+ $EDITOR ~/.clawconnect/.env
45
+ ```
46
+
47
+ 如果是在这个源码目录里开发,可以把 `.env.example` 复制为 `.env.local`。已有的 `~/.clawconnect/.env` 不会被自动覆盖。
48
+
49
+ 支持的配置项:
50
+
51
+ - `CLAWCONNECT_RELAY_SERVER_URL`:未传 `--server` 时,`clawconnect pair` 使用的默认中继地址
52
+ - `CLAWCONNECT_GATEWAY_URL`:可选,本机 OpenClaw Gateway WebSocket 地址覆盖值
53
+ - `CLAWCONNECT_ENV_FILE`:可选,显式指定 env 文件路径;未设置时会依次读取 `~/.clawconnect/.env`、`.env.local`、`.env`
54
+ - `OPENCLAW_GATEWAY_TOKEN` / `OPENCLAW_GATEWAY_PASSWORD`:Gateway 鉴权兜底值
55
+ - `OPENCLAW_TTS_ENABLED`、`OPENCLAW_TTS_VOICE`、`OPENCLAW_TTS_RATE`、`OPENCLAW_TTS_ENGINE`:语音回复默认值
56
+
57
+ Shell 里已经设置的环境变量优先级高于 env 文件。`clawconnect run`、`status`、`send-file` 会继续使用 `~/.clawconnect/config.json` 里已配对保存的 relay;如果要切换中继服务器,请执行 `clawconnect pair --server <url>` 或 `clawconnect reset`。
58
+
59
+ ### 2. 前台运行
60
+
61
+ 以当前终端前台方式启动代理:
62
+
63
+ ```bash
64
+ clawconnect run
65
+ ```
66
+
67
+ 适合调试中继连接、本地 Gateway 鉴权和日志输出。
68
+
69
+ 如果需要语音播报助手回复,可以显式开启:
70
+
71
+ ```bash
72
+ OPENCLAW_TTS_ENABLED=1 clawconnect run
73
+ ```
74
+
75
+ 未设置这个开关时,助手回复保持纯文字。
76
+
77
+ ### 3. 查看状态
78
+
79
+ 查看当前配对信息、网关地址和后台服务状态:
80
+
81
+ ```bash
82
+ clawconnect status
83
+ ```
84
+
85
+ ### 4. 安装后台服务
86
+
87
+ 将代理安装成后台常驻服务:
88
+
89
+ ```bash
90
+ clawconnect install
91
+ ```
92
+
93
+ 行为说明:
94
+
95
+ - macOS:安装为 `launchd` 用户服务
96
+ - Linux:优先使用 `systemd --user`
97
+ - 如果 Linux 当前环境不支持 `systemd --user`,会自动回退到 `nohup`
98
+
99
+ 在不支持 `systemd --user` 的 Linux 环境下,会生成一个备用启动脚本:
100
+
101
+ ```bash
102
+ ~/.clawconnect/clawconnect-start.sh
103
+ ```
104
+
105
+ 你也可以手动执行:
106
+
107
+ ```bash
108
+ bash ~/.clawconnect/clawconnect-start.sh
109
+ ```
110
+
111
+ ### 5. 停止服务
112
+
113
+ 停止后台代理服务:
114
+
115
+ ```bash
116
+ clawconnect stop
117
+ ```
118
+
119
+ ### 6. 重启服务
120
+
121
+ 重启后台代理服务:
122
+
123
+ ```bash
124
+ clawconnect restart
125
+ ```
126
+
127
+ ### 7. 设置 Gateway Token
128
+
129
+ 当本机 Gateway 使用 token 鉴权时,可以手动保存 token:
130
+
131
+ ```bash
132
+ clawconnect set-token
133
+ ```
134
+
135
+ ### 8. 卸载服务
136
+
137
+ 移除后台服务定义,但保留本地配置:
138
+
139
+ ```bash
140
+ clawconnect uninstall
141
+ ```
142
+
143
+ ### 9. 重置配对
144
+
145
+ 停止服务并清除本地配对配置:
146
+
147
+ ```bash
148
+ clawconnect reset
149
+ ```
150
+
151
+ ### 10. 发送文件/图片
152
+
153
+ 把本地图片或其他文件发到已配对的聊天会话:
154
+
155
+ ```bash
156
+ clawconnect send-file ~/Pictures/demo.jpg
157
+ ```
158
+
159
+ 可选参数:
160
+
161
+ - `-g, --gateway <id>`:覆盖本地配置里的网关 ID
162
+ - `-s, --session <key>`:指定聊天会话,不传时默认使用最近活跃会话
163
+ - `--json`:以 JSON 输出上传结果
164
+
165
+ 说明:
166
+
167
+ - `send-file` 会先把文件上传到 relay,再把文件消息发到手机端。
168
+ - 图片类型会在 iPhone 聊天里显示预览图,其他类型则显示文件卡片。
169
+ - `chat.send` 的 `attachments` 目前只是本地落盘引用,不是跨设备文件传输入口。
170
+ - 默认不启用语音回复;只有设置 `OPENCLAW_TTS_ENABLED=1` 时,agent 才会把助手回复合成为音频文件。
171
+ - 语音合成优先使用 `edge-tts-universal`,如果生成失败则回退到电脑自带的 TTS。
172
+
173
+ ## 代码结构
174
+
175
+ ```text
176
+ clawconnect-agent/
177
+ README.md
178
+ README.zh-CN.md
179
+ package.json
180
+ tsconfig.json
181
+ tsconfig.build.json
182
+ src/
183
+ index.ts CLI 入口和命令注册
184
+ commands/
185
+ pair.ts 配对、写入配置、安装后台服务
186
+ run.ts 前台运行 relay 主循环
187
+ send-file.ts 将本地文件/图片发送到已配对会话
188
+ status.ts 查看配对与服务状态
189
+ install.ts install / restart / stop / uninstall / reset
190
+ set-token.ts 手动保存 Gateway Token
191
+ local-handlers.ts 兼容旧命令前缀和本地维护命令
192
+ local-runtime.ts 解析 OpenClaw 可执行文件并触发网关生命周期
193
+ provider-handlers.ts provider 命令分发和网关重启
194
+ provider-config.ts provider 配置读写
195
+ provider-registry.ts provider 注册信息
196
+ backup-manager.ts 本地配置备份管理
197
+ send-file-utils.ts relay 地址和文件传输辅助
198
+ *.test.ts 紧邻源码的单元测试
199
+ config/
200
+ config.ts 配对配置读写
201
+ env.ts env 文件加载和 agent 默认配置
202
+ i18n/
203
+ index.ts CLI 文案
204
+ platform/
205
+ service-manager.ts 跨平台服务入口
206
+ service-manager-common.ts 通用平台辅助常量
207
+ service-manager-linux.ts Linux systemd/nohup 逻辑
208
+ *.test.ts 平台层单元测试
209
+ relay/
210
+ gateway-client.ts 连接并认证本地 OpenClaw Gateway
211
+ relay-manager.ts 中继服务器桥接与命令分发
212
+ session-context.ts 会话默认值和上下文使用量快照
213
+ chat-payload.ts 聊天 payload 归一化
214
+ chat-history.ts 历史消息回退
215
+ attachment-staging.ts 附件本地落盘
216
+ chat-send-attachments.ts 发送带附件消息的参数准备
217
+ reconnect.ts 重连策略
218
+ *.test.ts relay 层单元测试
219
+ ```
220
+
221
+ 测试文件和源码采用邻近放置的方式,统一以 `*.test.ts` 命名。它们会被 `npm test` 执行,但不会进入发布包。
222
+
223
+ ## 关键模块与功能
224
+
225
+ - `src/index.ts` 是 CLI 入口,负责命令注册和顶层错误处理。
226
+ - `src/commands/pair.ts` 负责主机配对、保存 relay 配置,并在配对后安装后台服务。
227
+ - `src/commands/run.ts` 负责前台启动 relay 循环,供服务管理或调试使用。
228
+ - `src/commands/send-file.ts` 负责把本地文件或图片上传到已配对的聊天会话。
229
+ - `src/commands/install.ts` 负责后台服务的安装、重启、停止、卸载和重置。
230
+ - `src/commands/set-token.ts` 负责在需要 token 鉴权时保存本地 Gateway Token。
231
+ - `src/commands/local-handlers.ts` 负责兼容旧命令前缀,以及备份、恢复、日志、doctor、gateway 重启等本地维护命令。
232
+ - `src/commands/local-runtime.ts` 负责解析 `openclaw` 可执行文件并触发网关生命周期动作。
233
+ - `src/commands/provider-handlers.ts` 负责 provider 专属命令分发,并复用同一套 gateway 重启逻辑。
234
+ - `src/config/env.ts` 负责加载 `.env` 文件,并集中维护 relay、Gateway 等 agent 默认配置。
235
+ - `src/platform/service-manager.ts` 负责 macOS 和 Linux 的跨平台服务管理入口。
236
+ - `src/relay/relay-manager.ts` 负责中继服务器与本地 Gateway 的桥接、命令分发,以及 chat/history 回退处理。
237
+ - `src/relay/gateway-client.ts` 负责连接 OpenClaw Gateway,并处理设备身份认证。
238
+ - `src/relay/session-context.ts` 负责读取会话默认值和上下文使用量快照。
239
+ - `src/relay/chat-payload.ts`、`src/relay/chat-history.ts`、`src/relay/attachment-staging.ts`、`src/relay/chat-send-attachments.ts` 负责聊天数据归一化、历史回退和附件暂存。
240
+ - `src/config/config.ts` 负责读写 `~/.clawconnect` 下的本地配对配置。
241
+ - `src/i18n/index.ts` 负责 CLI 文案。
242
+
243
+ ## 重要函数
244
+
245
+ - `pairCommand()` 负责主机配对、生成配对 payload,并持久化 relay 配置。
246
+ - `runCommand()` 负责启动前台 relay 客户端循环,供后台服务使用。
247
+ - `sendFileCommand()` 负责暂存本地文件并上传到已配对的聊天会话。
248
+ - `installCommand()`、`restartCommand()`、`stopCommand()`、`uninstallCommand()`、`resetCommand()` 负责服务生命周期管理。
249
+ - `statusCommand()` 负责打印配对配置、Gateway 状态和服务健康信息。
250
+ - `setTokenCommand()` 负责保存本地 OpenClaw Gateway Token。
251
+ - `handleLocalCommand()` 和 `handleProviderCommand()` 负责在转发前处理本地控制平面命令。
252
+ - `OpenClawGatewayClient` 负责管理 Gateway WebSocket、重连和请求/响应帧。
253
+ - `canonicalizeRelayParams()`、`extractGatewaySessionDefaults()`、`buildContextUsageFingerprint()` 负责统一 relay 协议 payload。
254
+ - `normalizeChatEventPayload()`、`extractChatText()`、`extractHistoryOutcome()`、`withMessageText()` 负责稳定聊天事件与历史回退结果。
255
+ - `buildAttachmentStagingPath()` 和 `resolveAttachmentFileName()` 负责附件暂存路径和文件名处理。
256
+ - `getServiceStatus()`、`installService()`、`restartService()`、`stopService()`、`uninstallService()` 负责跨平台服务控制。
257
+
258
+ ## 工作原理
259
+
260
+ 整个链路如下:
261
+
262
+ 1. 在主机上运行 `clawconnect pair`,向中继站注册当前设备
263
+ 2. 使用移动端扫描二维码或输入访问码完成绑定
264
+ 3. `ClawConnect Agent` 与中继站保持长连接
265
+ 4. `ClawConnect Agent` 再连接本机 OpenClaw Gateway
266
+ 5. 移动端的聊天、模型切换、技能操作等请求,通过中继转发到本机 OpenClaw
267
+ 6. 本机文件可通过 `clawconnect send-file <path>` 发送到聊天会话,并在手机端作为文件消息展示
268
+
269
+ ## 本地目录
270
+
271
+ 代理默认使用以下目录:
272
+
273
+ ```bash
274
+ ~/.clawconnect/
275
+ ```
276
+
277
+ 常见文件包括:
278
+
279
+ - `config.json`:配对配置
280
+ - `.env`:agent 默认环境配置
281
+ - `device-identity.json`:本机身份信息
282
+ - `clawconnect.log`:运行日志
283
+ - `clawconnect-error.log`:错误日志
284
+ - `clawconnect-start.sh`:Linux `nohup` 启动脚本
285
+
286
+ ## 前置要求
287
+
288
+ - macOS 或 Linux
289
+ - Node.js `18+`
290
+ - 本机已安装并可运行 `openclaw`
291
+ - 本机 OpenClaw Gateway 能正常启动
292
+
293
+ ## 常见问题
294
+
295
+ ### 配对成功但手机显示离线
296
+
297
+ 优先检查:
298
+
299
+ - `clawconnect status`
300
+ - 中继服务器地址是否正确
301
+ - 本机网络是否可访问中继站
302
+ - 后台服务是否已启动
303
+
304
+ ### 中继在线但 OpenClaw 连不上
305
+
306
+ 检查:
307
+
308
+ - 本地 OpenClaw 是否正在运行
309
+ - Gateway 端口是否正确
310
+ - Gateway Token / Password 是否匹配
311
+
312
+ ### 需要手动设置 Gateway Token
313
+
314
+ 如果自动读取本地 OpenClaw 配置失败,可手动设置:
315
+
316
+ ```bash
317
+ clawconnect set-token
318
+ ```
319
+
320
+ ## 建议
321
+
322
+ - 不要把此代理默认装在高敏感生产机器上
323
+ - 不使用远控时可执行 `clawconnect stop`
324
+ - 定期重置配对关系
325
+ - 不要泄露 `~/.clawconnect/config.json`
326
+
327
+ ## 许可证
328
+
329
+ MIT
@@ -0,0 +1,21 @@
1
+ export interface BackupRecord {
2
+ id: string;
3
+ title: string;
4
+ detail: string;
5
+ filename: string;
6
+ sizeBytes: number;
7
+ createdAt: string;
8
+ updatedAt: string;
9
+ }
10
+ export interface BackupListResult {
11
+ backups: BackupRecord[];
12
+ maxBackups: number;
13
+ }
14
+ export interface BackupMutationResult extends BackupListResult {
15
+ backup: BackupRecord;
16
+ }
17
+ export declare function listBackups(): BackupListResult;
18
+ export declare function createBackup(params: unknown): BackupMutationResult;
19
+ export declare function updateBackup(params: unknown): BackupMutationResult;
20
+ export declare function deleteBackup(params: unknown): BackupMutationResult;
21
+ export declare function restoreBackup(params: unknown): BackupMutationResult;