qwen-audio-agent 0.10.0 → 0.12.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 (44) hide show
  1. package/README.md +42 -13
  2. package/README_EN.md +49 -15
  3. package/cli/package.json +1 -1
  4. package/cli/src/arguments.mjs +32 -32
  5. package/cli/src/launcher.mjs +39 -11
  6. package/cli/src/runtime.mjs +61 -28
  7. package/docs/architecture.md +5 -4
  8. package/docs/configuration.md +90 -65
  9. package/package.json +1 -1
  10. package/scripts/claude-code-acp +10 -0
  11. package/scripts/codex-acp +10 -1
  12. package/scripts/openclaw +80 -39
  13. package/scripts/opencode-server +23 -8
  14. package/scripts/prepare-openclaw-config.mjs +11 -0
  15. package/scripts/release-check +2 -2
  16. package/scripts/sync-openclaw-gateway-token.mjs +9 -0
  17. package/scripts/verify-package.mjs +0 -1
  18. package/server/package.json +1 -1
  19. package/server/src/agent/acp-backend-adapter.mjs +307 -29
  20. package/server/src/agent/acp-backend-session-utils.mjs +36 -23
  21. package/server/src/agent/acp-process-client.mjs +55 -6
  22. package/server/src/agent/agent-client.mjs +34 -12
  23. package/server/src/agent/backends/claude.mjs +5 -2
  24. package/server/src/agent/backends/codex.mjs +2 -1
  25. package/server/src/agent/backends/hermes.mjs +1 -1
  26. package/server/src/agent/backends/openclaw.mjs +37 -2
  27. package/server/src/agent/backends/opencode.mjs +25 -0
  28. package/server/src/agent/backends/qoder.mjs +1 -8
  29. package/server/src/agent/coordinator.mjs +6 -15
  30. package/server/src/agent/openclaw-adapter.mjs +40 -3
  31. package/server/src/app/bootstrap.mjs +4 -1
  32. package/server/src/core/config.mjs +54 -43
  33. package/server/src/index.mjs +9 -2
  34. package/server/src/process/backend-drivers/openclaw-auth.mjs +94 -0
  35. package/server/src/process/backend-drivers/registry.mjs +6 -1
  36. package/server/src/process/managed-backend.mjs +29 -18
  37. package/server/src/voice/realtime-gateway.mjs +30 -5
  38. package/server/src/voice/tools/tool-call-handler.mjs +51 -26
  39. package/shared/backend-catalog.mjs +6 -2
  40. package/shared/backend-setup.mjs +479 -0
  41. package/shared/runtime-environment.mjs +46 -20
  42. package/tui/package.json +1 -1
  43. package/web/dist/assets/{index-BeamDvsE.js → index-DfCChshH.js} +28 -28
  44. package/web/dist/index.html +1 -1
package/README.md CHANGED
@@ -23,7 +23,13 @@ Agent 也应该始终在场。
23
23
 
24
24
  “已经好了。”
25
25
 
26
- ## 核心特色
26
+ ## 对话继续,任务也在继续
27
+
28
+ 对话不会因为后台任务而停下;任务完成后,结果会自然回到当前对话:
29
+
30
+ https://github.com/user-attachments/assets/42022655-36d1-46b2-9c26-ff0765284000
31
+
32
+ ### 核心特色
27
33
 
28
34
  - 全双工实时语音交互、自然打断和持续多轮对话
29
35
  - 一键连接你喜欢的各种 Agent,使用已有的模型、工具、MCP、Skill
@@ -32,7 +38,7 @@ Agent 也应该始终在场。
32
38
  - 支持 WebUI、终端 TUI 和 macOS 桌面悬浮球
33
39
  - 支持本地用户档案与跨会话个人记忆
34
40
 
35
- ## 对话继续,任务也在继续
41
+ ## 参考架构
36
42
 
37
43
  ![qwen-audio-agent 原理图](docs/architecture-overview.png)
38
44
 
@@ -52,13 +58,13 @@ Agent 也应该始终在场。
52
58
 
53
59
  | 后台 Agent | 接入方式 | 接入准备 | 推荐指数 |
54
60
  | --- | --- | --- | --- |
55
- | OpenCode | 原生 ACP | 可自动安装 | ★★★★★ |
56
- | OpenClaw | 内置 ACP 桥接 | 可自动安装 | ★★★★★ |
57
- | Qoder | 原生 ACP | 需要用户安装和认证 | ★★★★★ |
58
- | Hermes | 原生 ACP | 需要用户安装和配置模型 | ★★★★☆ |
59
- | CodeBuddy | 原生 ACP | 需要用户安装和认证 | ★★★★☆ |
60
- | Codex | 外部 ACP 适配 | 可自动安装 | ★★★★☆ |
61
- | Claude Code | 外部 ACP 适配 | 可自动安装,需要用户认证 | ★★★★☆ |
61
+ | OpenCode | 原生 ACP | 支持自动安装和百炼配置 | ★★★★★ |
62
+ | OpenClaw | 内置 ACP 桥接 | 支持自动安装和百炼配置 | ★★★★★ |
63
+ | Qoder | 原生 ACP | 用户自行安装和配置 | ★★★★★ |
64
+ | Hermes | 原生 ACP | 用户自行安装和配置 | ★★★★☆ |
65
+ | CodeBuddy | 原生 ACP | 用户自行安装和配置 | ★★★★☆ |
66
+ | Codex | 外部 ACP 适配 | 用户自行安装和配置 | ★★★★☆ |
67
+ | Claude Code | 外部 ACP 适配 | 用户自行安装和配置 | ★★★★☆ |
62
68
 
63
69
  推荐指数综合反映当前集成完整度、兼容性和实际验证程度:五星表示已经过充分测试的
64
70
  推荐集成,四星表示正在开发或尚未完成同等范围验证。
@@ -121,11 +127,17 @@ npm install -g git+https://github.com/QwenAudio/qwen-audio-agent.git
121
127
  qwenaudio config
122
128
  ```
123
129
 
124
- 2. 打开命令显示的 `config.env`,填写 DashScope API Key,并选择 OpenClaw:
130
+ 2. 打开命令显示的 `config.env`,填写 DashScope API Key。需要执行后台任务时,
131
+ 再选择 OpenClaw 或其他后台 Agent:
125
132
 
126
133
  ```dotenv
127
134
  DASHSCOPE_API_KEY=your-key
135
+ # 语音前台模型:qwen-audio-3.0-realtime-flash 或 qwen-audio-3.0-realtime-plus(默认)
136
+ QWEN_AUDIO_REALTIME_MODEL=qwen-audio-3.0-realtime-plus
137
+ # 后台Agent:可选,不设置或设置为 none 时,启动仅前台模式
128
138
  AGENT_PROTOCOL=openclaw
139
+ # 后台模型:可为空,留空则沿用 Agent 自身的用户配置
140
+ QWEN_AUDIO_AGENT_BACKEND_MODEL=qwen3.7-max
129
141
  ```
130
142
 
131
143
  3. 在一个终端中启动 Gateway:
@@ -201,7 +213,22 @@ qwenaudio gateway uninstall
201
213
 
202
214
  ## 选择后台 Agent
203
215
 
204
- 通过 `AGENT_PROTOCOL` 选择后台 Agent(必填,没有默认值)。使用 OpenClaw:
216
+ `AGENT_PROTOCOL` 是可选配置。留空时,Gateway 以仅前台模式运行,实时语音聊天
217
+ 保持可用;如果请求需要后台执行,前台会明确说明当前没有可用的后台 Agent。
218
+ 也可以在命令行中使用 `qwenaudio --backend none`,明确要求仅启动前台模式。
219
+
220
+ 通过 `AGENT_PROTOCOL` 环境变量或 `--backend` 参数选择后台 Agent。选择后,
221
+ OpenCode 和 OpenClaw 支持自动下载安装;配置
222
+ `DASHSCOPE_API_KEY` 和 `QWEN_AUDIO_AGENT_BACKEND_MODEL` 后即可自动接入百炼
223
+ 模型。未指定后台模型且用户已经安装并配置对应 Agent 时,则完整复用用户环境。
224
+
225
+ 查看当前可用的后台 Agent:
226
+
227
+ ```bash
228
+ qwenaudio setup
229
+ ```
230
+
231
+ 使用 OpenClaw:
205
232
 
206
233
  ```dotenv
207
234
  AGENT_PROTOCOL=openclaw
@@ -217,7 +244,6 @@ AGENT_PROTOCOL=opencode
217
244
 
218
245
  ```dotenv
219
246
  AGENT_PROTOCOL=qoder
220
- QODER_MODEL=auto
221
247
  ```
222
248
 
223
249
  Hermes、CodeBuddy、Codex 和 Claude Code 也可直接选择:
@@ -227,6 +253,9 @@ AGENT_PROTOCOL=hermes
227
253
  # 或 codebuddy、codex、claude
228
254
  ```
229
255
 
256
+ 以上其他后台暂时需要用户自行安装并完成原生配置;qwen-audio-agent 会复用其
257
+ 用户级模型、工具、MCP、Skill 和认证。
258
+
230
259
  使用其他支持 ACP stdio 的 Agent:
231
260
 
232
261
  ```dotenv
@@ -235,7 +264,7 @@ ACP_COMMAND=your-agent
235
264
  ACP_ARGS=["--acp"]
236
265
  ```
237
266
 
238
- 通用 ACP 入口不需要修改 Gateway 代码。命令、参数、显示名称、模型和工作目录可分别通过 `ACP_COMMAND`、`ACP_ARGS`、`ACP_LABEL`、`ACP_MODEL` 和 `ACP_WORKSPACE` 配置。
267
+ 通用 ACP 入口不需要修改 Gateway 代码。命令、参数、显示名称和工作目录可分别通过 `ACP_COMMAND`、`ACP_ARGS`、`ACP_LABEL` 和 `ACP_WORKSPACE` 配置。
239
268
 
240
269
  后台权限默认使用 `native`,由后台 Agent 在需要时询问。只有在可信项目中,并且
241
270
  明确接受自动执行命令和修改文件时,才应启用:
package/README_EN.md CHANGED
@@ -27,7 +27,14 @@ task is complete, naturally tells you:
27
27
 
28
28
  “It’s ready.”
29
29
 
30
- ## Core Features
30
+ ## Keep Talking While Tasks Keep Running
31
+
32
+ Conversation continues while the Agent works in the background. When the task
33
+ is done, the result returns naturally to the conversation:
34
+
35
+ https://github.com/user-attachments/assets/42022655-36d1-46b2-9c26-ff0765284000
36
+
37
+ ### Core Features
31
38
 
32
39
  - Full-duplex realtime voice interaction, natural interruption, and continuous
33
40
  multi-turn conversation
@@ -40,7 +47,7 @@ task is complete, naturally tells you:
40
47
  - WebUI, terminal TUI, and a macOS desktop orb
41
48
  - Local user profile and personal memory across sessions
42
49
 
43
- ## Keep Talking While Tasks Keep Running
50
+ ## Reference Architecture
44
51
 
45
52
  ![qwen-audio-agent architecture](docs/architecture-overview-en.png)
46
53
 
@@ -63,13 +70,13 @@ module walkthrough.
63
70
 
64
71
  | Backend Agent | Integration | Setup | Rating |
65
72
  | --- | --- | --- | --- |
66
- | OpenCode | Native ACP | Automatic installation supported | ★★★★★ |
67
- | OpenClaw | Built-in ACP Bridge | Automatic installation supported | ★★★★★ |
68
- | Qoder | Native ACP | Requires user installation and authentication | ★★★★★ |
69
- | Hermes | Native ACP | Requires user installation and model configuration | ★★★★☆ |
70
- | CodeBuddy | Native ACP | Requires user installation and authentication | ★★★★☆ |
71
- | Codex | External ACP Adapter | Automatic installation supported | ★★★★☆ |
72
- | Claude Code | External ACP Adapter | Automatic installation supported; user authentication required | ★★★★☆ |
73
+ | OpenCode | Native ACP | Automatic installation and Bailian setup supported | ★★★★★ |
74
+ | OpenClaw | Built-in ACP Bridge | Automatic installation and Bailian setup supported | ★★★★★ |
75
+ | Qoder | Native ACP | User installation and configuration required | ★★★★★ |
76
+ | Hermes | Native ACP | User installation and configuration required | ★★★★☆ |
77
+ | CodeBuddy | Native ACP | User installation and configuration required | ★★★★☆ |
78
+ | Codex | External ACP Adapter | User installation and configuration required | ★★★★☆ |
79
+ | Claude Code | External ACP Adapter | User installation and configuration required | ★★★★☆ |
73
80
 
74
81
  Ratings reflect the current integration completeness, compatibility, and
75
82
  extent of real-world validation. Five stars identify recommended integrations
@@ -141,12 +148,17 @@ for details.
141
148
  qwenaudio config
142
149
  ```
143
150
 
144
- 2. Open the displayed `config.env` file, add your DashScope API Key, and select
145
- OpenClaw:
151
+ 2. Open the displayed `config.env` file and add your DashScope API Key. Select
152
+ OpenClaw or another backend Agent only when you need background tasks:
146
153
 
147
154
  ```dotenv
148
155
  DASHSCOPE_API_KEY=your-key
149
- AGENT_PROTOCOL=openclaw
156
+ # Voice model: qwen-audio-3.0-realtime-flash or qwen-audio-3.0-realtime-plus (default)
157
+ QWEN_AUDIO_REALTIME_MODEL=qwen-audio-3.0-realtime-plus
158
+ # Optional; uncomment when background tasks are needed
159
+ # AGENT_PROTOCOL=openclaw
160
+ # Backend model: can be left empty; the Agent uses its own user configuration
161
+ # QWEN_AUDIO_AGENT_BACKEND_MODEL=qwen3.7-max
150
162
  ```
151
163
 
152
164
  3. Start the Gateway in one terminal:
@@ -227,7 +239,26 @@ qwenaudio gateway uninstall
227
239
 
228
240
  ## Choose a Backend Agent
229
241
 
230
- Select the backend Agent with `AGENT_PROTOCOL` (required, no default value).
242
+ `AGENT_PROTOCOL` is optional. When it is empty, the Gateway runs in
243
+ frontend-only mode and realtime voice chat remains available. If a request
244
+ requires background execution, the frontend clearly explains that no backend
245
+ Agent is available. You can also run `qwenaudio --backend none` to explicitly
246
+ start in frontend-only mode.
247
+
248
+ Select the backend Agent with `AGENT_PROTOCOL` or `--backend`. After selecting
249
+ one, OpenCode and OpenClaw can be downloaded automatically. Configure
250
+ `DASHSCOPE_API_KEY` and
251
+ `QWEN_AUDIO_AGENT_BACKEND_MODEL` to connect them to a Bailian model
252
+ automatically. When no backend model is specified and the Agent is already
253
+ installed and configured, qwen-audio-agent fully reuses the user's existing
254
+ environment.
255
+
256
+ Check which backends are available:
257
+
258
+ ```bash
259
+ qwenaudio setup
260
+ ```
261
+
231
262
  Use OpenClaw:
232
263
 
233
264
  ```dotenv
@@ -244,7 +275,6 @@ Use Qoder:
244
275
 
245
276
  ```dotenv
246
277
  AGENT_PROTOCOL=qoder
247
- QODER_MODEL=auto
248
278
  ```
249
279
 
250
280
  Hermes, CodeBuddy, Codex, and Claude Code can also be selected directly:
@@ -254,6 +284,10 @@ AGENT_PROTOCOL=hermes
254
284
  # Or codebuddy, codex, or claude
255
285
  ```
256
286
 
287
+ These other backends currently require the user to install and configure the
288
+ native Agent first. qwen-audio-agent reuses its user-level model, tools, MCP
289
+ servers, Skills, and authentication.
290
+
257
291
  Use another Agent that supports ACP over stdio:
258
292
 
259
293
  ```dotenv
@@ -262,7 +296,7 @@ ACP_COMMAND=your-agent
262
296
  ACP_ARGS=["--acp"]
263
297
  ```
264
298
 
265
- The generic ACP entry point requires no Gateway code changes. Configure its command, arguments, display label, model, and workspace with `ACP_COMMAND`, `ACP_ARGS`, `ACP_LABEL`, `ACP_MODEL`, and `ACP_WORKSPACE`.
299
+ The generic ACP entry point requires no Gateway code changes. Configure its command, arguments, display label, and workspace with `ACP_COMMAND`, `ACP_ARGS`, `ACP_LABEL`, and `ACP_WORKSPACE`.
266
300
 
267
301
  Backend permissions default to `native`, so the backend Agent asks when
268
302
  permission is required. Enable the following option only in trusted projects
package/cli/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qwen-audio-agent/cli",
3
3
  "private": true,
4
- "version": "0.10.0",
4
+ "version": "0.12.0",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "qwenaudio": "./bin/qwenaudio.mjs"
@@ -2,9 +2,17 @@ import { randomUUID } from 'node:crypto'
2
2
  import {
3
3
  backendDefinition,
4
4
  backendNames,
5
+ normalizeBackendProtocol,
5
6
  } from '../../shared/backend-catalog.mjs'
6
7
 
7
- const COMMANDS = new Set(['gateway', 'tui', 'webui', 'status', 'config'])
8
+ const COMMANDS = new Set([
9
+ 'gateway',
10
+ 'tui',
11
+ 'webui',
12
+ 'status',
13
+ 'config',
14
+ 'setup',
15
+ ])
8
16
  const GATEWAY_ACTIONS = new Set([
9
17
  'run',
10
18
  'install',
@@ -62,10 +70,7 @@ export function parseArguments(argv, env = process.env) {
62
70
  audioMode: String(
63
71
  env.QWEN_AUDIO_AGENT_TUI_AUDIO_MODE || 'half',
64
72
  ).toLowerCase(),
65
- backend: String(env.AGENT_PROTOCOL || '').toLowerCase(),
66
- attachOpenClaw: String(
67
- env.OPENCLAW_ATTACH_EXISTING || '',
68
- ).toLowerCase() === 'true',
73
+ backend: normalizeBackendProtocol(env.AGENT_PROTOCOL),
69
74
  backendPermissionMode: String(
70
75
  env.QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE || 'native',
71
76
  ).toLowerCase(),
@@ -75,6 +80,8 @@ export function parseArguments(argv, env = process.env) {
75
80
  backendUrl: '',
76
81
  openBrowser: true,
77
82
  takeover: false,
83
+ json: false,
84
+ backendSpecified: false,
78
85
  gatewayConfigurationSpecified: false,
79
86
  }
80
87
  let audioModeSpecified = false
@@ -85,10 +92,10 @@ export function parseArguments(argv, env = process.env) {
85
92
  options.url = nextValue(args, index++, '--url')
86
93
  options.gatewayConfigurationSpecified = true
87
94
  } else if (argument === '--backend') {
88
- options.backend = nextValue(args, index++, '--backend').toLowerCase()
89
- options.gatewayConfigurationSpecified = true
90
- } else if (argument === '--attach-openclaw') {
91
- options.attachOpenClaw = true
95
+ options.backend = normalizeBackendProtocol(
96
+ nextValue(args, index++, '--backend'),
97
+ )
98
+ options.backendSpecified = true
92
99
  options.gatewayConfigurationSpecified = true
93
100
  } else if (argument === '--backend-agent') {
94
101
  options.backendAgent = nextValue(args, index++, '--backend-agent').trim()
@@ -110,6 +117,7 @@ export function parseArguments(argv, env = process.env) {
110
117
  audioModeSpecified = true
111
118
  } else if (argument === '--no-open') options.openBrowser = false
112
119
  else if (argument === '--takeover') options.takeover = true
120
+ else if (argument === '--json') options.json = true
113
121
  else if (argument === '--help' || argument === '-h') options.help = true
114
122
  else throw new Error(`未知参数:${argument}`)
115
123
  }
@@ -122,7 +130,10 @@ export function parseArguments(argv, env = process.env) {
122
130
  `不支持的后台:${options.backend}(可选 ${backendNames().join('、')})`,
123
131
  )
124
132
  }
125
- if (!BACKEND_PERMISSION_MODES.has(options.backendPermissionMode)) {
133
+ if (
134
+ options.backend
135
+ && !BACKEND_PERMISSION_MODES.has(options.backendPermissionMode)
136
+ ) {
126
137
  throw new Error(
127
138
  `不支持的后台权限模式:${options.backendPermissionMode}`
128
139
  + '(可选 native、full)',
@@ -134,6 +145,9 @@ export function parseArguments(argv, env = process.env) {
134
145
  if (!['tui', 'webui'].includes(command) && options.takeover) {
135
146
  throw new Error('--takeover 只适用于 tui 或 webui')
136
147
  }
148
+ if (command !== 'setup' && options.json) {
149
+ throw new Error('--json 只适用于 setup')
150
+ }
137
151
  if (command !== 'tui' && audioModeSpecified) {
138
152
  throw new Error('--audio-mode 只适用于 tui')
139
153
  }
@@ -159,17 +173,9 @@ export function parseArguments(argv, env = process.env) {
159
173
  options.backendUrl = definition?.baseUrlEnvironment
160
174
  ? cleanOrigin(options.backendUrl || configuredBackendUrl, '后台地址')
161
175
  : ''
162
- if (options.attachOpenClaw && options.backend !== 'openclaw') {
163
- throw new Error('--attach-openclaw 只适用于 OpenClaw')
164
- }
165
176
  if (
166
- options.backendPermissionMode === 'full'
167
- && options.attachOpenClaw
168
- ) {
169
- throw new Error('最高权限模式只支持由 Gateway 启动的后台 Agent')
170
- }
171
- if (
172
- definition
177
+ command !== 'setup'
178
+ && definition
173
179
  && options.backendPermissionMode === 'full'
174
180
  && !definition.supportsFullPermission
175
181
  ) {
@@ -177,16 +183,6 @@ export function parseArguments(argv, env = process.env) {
177
183
  `${definition.label} 不支持 Gateway 统一最高权限模式`,
178
184
  )
179
185
  }
180
- if (
181
- command === 'gateway'
182
- && options.gatewayAction === 'run'
183
- && !options.backend
184
- ) {
185
- throw new Error(
186
- '必须指定后台 Agent:在 config.env 设置 AGENT_PROTOCOL,'
187
- + `或使用 --backend(可选 ${backendNames().join('、')})`,
188
- )
189
- }
190
186
  options.sessionId = String(options.sessionId || '').trim()
191
187
  if (!options.sessionId) throw new Error('--session 不能为空')
192
188
  return options
@@ -208,15 +204,19 @@ export function helpText() {
208
204
  ' qwenaudio webui [选项] 打开现有 Gateway 的 WebUI',
209
205
  ' qwenaudio status [选项] gateway status 的兼容别名',
210
206
  ' qwenaudio config 显示用户配置文件位置',
207
+ ' qwenaudio setup [选项] 只读检查后台 Agent 接入准备情况',
211
208
  '',
212
209
  'Gateway 选项:',
213
210
  ' --url URL Gateway 地址(默认 http://127.0.0.1:3101)',
214
- ' --backend NAME 必填:openclaw、opencode、qoder、hermes、codebuddy、codex、claude 或 acp(也可在 config.env 设置 AGENT_PROTOCOL)',
215
- ' --attach-openclaw 连接用户已启动的 OpenClaw Gateway',
211
+ ' --backend NAME 可选:openclaw、opencode、qoder、hermes、codebuddy、codex、claude、acp 或 none;不设置或使用 none 时仅前台聊天',
216
212
  ' --backend-permission-mode MODE native(默认)或 full(最高权限)',
217
213
  ' --backend-url URL 后台 Server 地址',
218
214
  ' --backend-agent ID 指定协调 Agent',
219
215
  '',
216
+ 'Setup 选项:',
217
+ ' --backend NAME 只检查指定后台;默认检查全部后台',
218
+ ' --json 输出供桌面版或脚本使用的 JSON',
219
+ '',
220
220
  '界面选项:',
221
221
  ' --session ID 复用指定语音会话',
222
222
  ' --audio-mode MODE Linux / Windows 使用 half(默认)或 full',
@@ -2,6 +2,10 @@ import { dirname, resolve } from 'node:path'
2
2
  import { fileURLToPath, pathToFileURL } from 'node:url'
3
3
  import { loadRuntimeEnvironment } from '../../shared/runtime-environment.mjs'
4
4
  import { backendDefinition } from '../../shared/backend-catalog.mjs'
5
+ import {
6
+ formatBackendSetup,
7
+ inspectBackendSetups,
8
+ } from '../../shared/backend-setup.mjs'
5
9
  import { helpText, parseArguments } from './arguments.mjs'
6
10
  import {
7
11
  ensureRuntime,
@@ -28,25 +32,28 @@ async function runMinimal(options) {
28
32
  }
29
33
 
30
34
  function applyGatewayOptions(env, options) {
31
- env.AGENT_PROTOCOL = options.backend
32
- if (options.attachOpenClaw) {
33
- env.OPENCLAW_ATTACH_EXISTING = 'true'
35
+ // Keep an explicit empty value so the environment loader cannot restore a
36
+ // backend from config.env after --backend none selected frontend-only mode.
37
+ env.AGENT_PROTOCOL = options.backend || ''
38
+ const definition = backendDefinition(options.backend)
39
+ if (options.backend) {
40
+ env.QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE =
41
+ options.backendPermissionMode
34
42
  } else {
35
- delete env.OPENCLAW_ATTACH_EXISTING
43
+ delete env.QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE
36
44
  }
37
- env.QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE = options.backendPermissionMode
38
- if (options.backendAgent) {
45
+ if (options.backend && options.backendAgent) {
39
46
  env.QWEN_AUDIO_AGENT_BACKEND_AGENT = options.backendAgent
40
47
  } else {
41
48
  delete env.QWEN_AUDIO_AGENT_BACKEND_AGENT
42
49
  }
43
- const definition = backendDefinition(options.backend)
44
50
  if (definition?.baseUrlEnvironment) {
45
51
  env[definition.baseUrlEnvironment] = options.backendUrl
46
52
  }
47
53
  }
48
54
 
49
55
  function gatewaySummary(health) {
56
+ if (health?.backend?.enabled === false) return '仅前台聊天模式'
50
57
  const label = health?.backend?.label
51
58
  || health?.backend?.kind
52
59
  || health?.backend?.protocol
@@ -72,17 +79,27 @@ export async function main(argv, {
72
79
  env = process.env,
73
80
  stdout = process.stdout,
74
81
  signalSource = process,
75
- prepareEnvironment = () => loadRuntimeEnvironment({ root, env }),
82
+ prepareEnvironment = ({ readOnly = false } = {}) => loadRuntimeEnvironment({
83
+ root,
84
+ env,
85
+ readOnly,
86
+ }),
87
+ inspectSetups = options => inspectBackendSetups({
88
+ env,
89
+ backend: options.backendSpecified ? options.backend : '',
90
+ }),
76
91
  runMinimalTui = runMinimal,
77
92
  prepareRuntime = options => ensureRuntime(options, { root, env }),
78
93
  inspectGateway = url => readGatewayHealth(url),
79
94
  manageService = (action, options) => manageGatewayService(action, options),
80
- waitForService = url => waitForGateway(url, { requireBackend: true }),
95
+ waitForService = (url, { requireBackend = false } = {}) =>
96
+ waitForGateway(url, { requireBackend }),
81
97
  waitForServiceStop = url => waitForGatewayStop(url, { inspectGateway }),
82
98
  runWebUi = options => launchWebUi(options),
83
99
  acquireInstance = directory => acquireCliInstance(directory),
84
100
  } = {}) {
85
- const environment = prepareEnvironment()
101
+ const setupCommand = argv[0] === 'setup'
102
+ const environment = prepareEnvironment({ readOnly: setupCommand })
86
103
  const options = parseArguments(argv, env)
87
104
  if (options.help) {
88
105
  stdout.write(`${helpText()}\n`)
@@ -93,6 +110,15 @@ export async function main(argv, {
93
110
  || resolve(environment.configDirectory, 'config.env')}\n`)
94
111
  return 0
95
112
  }
113
+ if (options.command === 'setup') {
114
+ const report = inspectSetups(options)
115
+ stdout.write(options.json
116
+ ? `${JSON.stringify(report, null, 2)}\n`
117
+ : `${formatBackendSetup(report)}\n`)
118
+ const selected = report.backends.find(item => item.selected)
119
+ || (options.backendSpecified ? report.backends[0] : null)
120
+ return selected && !selected.ready ? 1 : 0
121
+ }
96
122
 
97
123
  if (
98
124
  (options.command === 'gateway' && options.gatewayAction !== 'run')
@@ -139,7 +165,9 @@ export async function main(argv, {
139
165
  )
140
166
  const serviceUrl = service.installedMetadata?.url || currentUrl
141
167
  if (['install', 'start', 'restart'].includes(options.gatewayAction)) {
142
- const ready = await waitForService(serviceUrl)
168
+ const ready = await waitForService(serviceUrl, {
169
+ requireBackend: Boolean(options.backend),
170
+ })
143
171
  stdout.write(
144
172
  `Gateway 后台服务已${
145
173
  options.gatewayAction === 'restart' ? '重启' : '启动'
@@ -3,6 +3,7 @@ import { resolve } from 'node:path'
3
3
  import {
4
4
  backendDefinition,
5
5
  backendNames,
6
+ normalizeBackendProtocol,
6
7
  } from '../../shared/backend-catalog.mjs'
7
8
  import {
8
9
  loadRuntimeEnvironment,
@@ -27,25 +28,24 @@ function normalizedOrigin(value) {
27
28
  }
28
29
 
29
30
  export function resolveBackend(options = {}, env = process.env) {
30
- const protocol = String(
31
- options.backend || env.AGENT_PROTOCOL || '',
32
- ).toLowerCase()
33
- if (!protocol) throw new Error(
34
- '必须指定后台 Agent:在 config.env 设置 AGENT_PROTOCOL,'
35
- + `或使用 --backend(可选 ${backendNames().join('、')})`,
31
+ const protocol = normalizeBackendProtocol(
32
+ options.backend || env.AGENT_PROTOCOL,
36
33
  )
34
+ if (!protocol) {
35
+ return {
36
+ enabled: false,
37
+ protocol: null,
38
+ ownership: null,
39
+ permissionMode: null,
40
+ agentId: '',
41
+ baseUrl: null,
42
+ }
43
+ }
37
44
  const definition = backendDefinition(protocol)
38
45
  if (!definition) throw new Error(
39
46
  `不支持的后台 Agent:${protocol}(可选 ${backendNames().join('、')})`,
40
47
  )
41
- const attachOpenClaw = (
42
- options.attachOpenClaw === true
43
- || String(env.OPENCLAW_ATTACH_EXISTING || '').toLowerCase() === 'true'
44
- )
45
- if (attachOpenClaw && protocol !== 'openclaw') {
46
- throw new Error('连接已有 OpenClaw Gateway 只适用于 OpenClaw')
47
- }
48
- const ownership = attachOpenClaw ? 'external' : 'owned'
48
+ const ownership = 'owned'
49
49
  const permissionMode = String(
50
50
  options.backendPermissionMode
51
51
  || env.QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE
@@ -66,7 +66,6 @@ export function resolveBackend(options = {}, env = process.env) {
66
66
  return {
67
67
  protocol,
68
68
  ownership,
69
- attachOpenClaw,
70
69
  permissionMode,
71
70
  agentId: String(
72
71
  options.backendAgent || env.QWEN_AUDIO_AGENT_BACKEND_AGENT || '',
@@ -78,6 +77,15 @@ export function resolveBackend(options = {}, env = process.env) {
78
77
  }
79
78
 
80
79
  export function assertGatewayCompatibility(health, backend) {
80
+ const actualEnabled = health?.backend?.enabled !== false
81
+ if (!backend.protocol) {
82
+ if (!actualEnabled) return
83
+ const actualProtocol = health?.backend?.kind || health?.backend?.protocol
84
+ throw new Error(
85
+ `现有 Gateway 使用 ${actualProtocol || '后台 Agent'},`
86
+ + '与当前仅前台聊天配置不一致',
87
+ )
88
+ }
81
89
  const actualProtocol = health?.backend?.kind || health?.backend?.protocol
82
90
  const actualBaseUrl = health?.backend?.baseUrl
83
91
  const actualOwnership = health?.backend?.ownership
@@ -177,18 +185,26 @@ function waitForReadiness(child, readiness, label) {
177
185
  }
178
186
 
179
187
  function backendEnvironment(env, backend) {
188
+ if (!backend.protocol) {
189
+ const next = {
190
+ ...env,
191
+ // An empty value is an intentional override. Deleting it would let the
192
+ // child Gateway reload AGENT_PROTOCOL from config.env.
193
+ AGENT_PROTOCOL: '',
194
+ }
195
+ delete next.QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE
196
+ delete next.QWEN_AUDIO_AGENT_BACKEND_AGENT
197
+ return next
198
+ }
199
+ const definition = backendDefinition(backend.protocol)
180
200
  const next = {
181
201
  ...env,
182
202
  AGENT_PROTOCOL: backend.protocol,
183
- ...(backend.attachOpenClaw
184
- ? { OPENCLAW_ATTACH_EXISTING: 'true' }
185
- : {}),
186
203
  QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE: backend.permissionMode,
187
204
  ...(backend.agentId
188
205
  ? { QWEN_AUDIO_AGENT_BACKEND_AGENT: backend.agentId }
189
206
  : {}),
190
207
  }
191
- const definition = backendDefinition(backend.protocol)
192
208
  if (!definition?.baseUrlEnvironment) return next
193
209
  const target = new URL(backend.baseUrl)
194
210
  next[definition.baseUrlEnvironment] = backend.baseUrl
@@ -218,7 +234,9 @@ function gatewaySpawn(root, platform, env, target, options) {
218
234
  PORT: String(options.listenPort || target.port || '80'),
219
235
  },
220
236
  detached: platform !== 'win32',
221
- stdio: 'inherit',
237
+ // Keep a private IPC channel so the Gateway can detect an npm/CLI
238
+ // launcher that disappears before its JavaScript signal handlers run.
239
+ stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
222
240
  },
223
241
  }
224
242
  }
@@ -243,7 +261,7 @@ export class ManagedRuntime {
243
261
  if (this.platform !== 'win32' && Number.isInteger(child.pid)) {
244
262
  try {
245
263
  // Managed services are spawned as process-group leaders. Signalling
246
- // the group also stops npm's Node/OpenCode descendants.
264
+ // the group also stops package-manager and backend descendants.
247
265
  this.killImpl(-child.pid, signal)
248
266
  continue
249
267
  } catch {
@@ -294,6 +312,9 @@ export async function ensureRuntime(options, {
294
312
  const runtime = new ManagedRuntime([], { platform })
295
313
  const local = isLocalGateway(options.url)
296
314
  const backend = resolveBackend(options, env)
315
+ const backendLabel = backend.protocol
316
+ ? backendDefinition(backend.protocol)?.label || backend.protocol
317
+ : '后台 Agent'
297
318
  let health = await readGatewayHealth(options.url, fetchImpl)
298
319
  const existingGateway = Boolean(health)
299
320
 
@@ -319,7 +340,9 @@ export async function ensureRuntime(options, {
319
340
  gateway,
320
341
  waitForGateway(options.url, {
321
342
  fetchImpl,
322
- requireBackend: options.waitForBackend !== false,
343
+ requireBackend: Boolean(
344
+ backend.protocol && options.waitForBackend !== false
345
+ ),
323
346
  }),
324
347
  'Gateway',
325
348
  )
@@ -330,11 +353,17 @@ export async function ensureRuntime(options, {
330
353
  if (existingGateway || backend.ownership === 'external') {
331
354
  assertGatewayCompatibility(health, backend)
332
355
  } else {
333
- const actualProtocol = health?.backend?.kind || health?.backend?.protocol
334
- if (actualProtocol !== backend.protocol) {
356
+ const actualProtocol = (
357
+ health?.backend?.kind || health?.backend?.protocol || null
358
+ )
359
+ const actualEnabled = health?.backend?.enabled !== false
360
+ if (
361
+ actualProtocol !== backend.protocol
362
+ || actualEnabled !== Boolean(backend.protocol)
363
+ ) {
335
364
  throw new Error(
336
- `Gateway 启动了 ${actualProtocol || '未知'} 后台,`
337
- + `与当前配置 ${backend.protocol} 不一致`,
365
+ `Gateway 启动了 ${actualProtocol || '仅前台聊天模式'},`
366
+ + `与当前配置 ${backend.protocol || '仅前台聊天模式'} 不一致`,
338
367
  )
339
368
  }
340
369
  }
@@ -347,10 +376,14 @@ export async function ensureRuntime(options, {
347
376
  )
348
377
  }
349
378
 
350
- if (health.backend?.ok !== true && options.waitForBackend !== false) {
379
+ if (
380
+ backend.protocol
381
+ && health.backend?.ok !== true
382
+ && options.waitForBackend !== false
383
+ ) {
351
384
  throw new Error(
352
385
  backend.ownership === 'external'
353
- ? `未连接到已有 OpenClaw Gateway,请先启动并检查 ${backend.baseUrl}`
386
+ ? `未连接到已有 ${backendLabel} 后台服务,请先启动并检查 ${backend.baseUrl}`
354
387
  : `Gateway 启动的后台 Agent 未就绪:${health.backend?.baseUrl || backend.baseUrl}`,
355
388
  )
356
389
  }