qwen-audio-agent 0.9.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 (122) hide show
  1. package/CONTRIBUTING.md +58 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +5 -0
  4. package/PRIVACY.md +34 -0
  5. package/README.md +282 -0
  6. package/README_EN.md +316 -0
  7. package/SECURITY.md +32 -0
  8. package/THIRD_PARTY_NOTICES.md +26 -0
  9. package/cli/bin/qwenaudio.mjs +10 -0
  10. package/cli/package.json +13 -0
  11. package/cli/src/arguments.mjs +247 -0
  12. package/cli/src/gateway-service.mjs +386 -0
  13. package/cli/src/instance-lock.mjs +65 -0
  14. package/cli/src/launcher.mjs +217 -0
  15. package/cli/src/runtime.mjs +356 -0
  16. package/cli/src/webui.mjs +61 -0
  17. package/config/acp/workspace/AGENTS.md +13 -0
  18. package/config/codebuddy/workspace/.codebuddy/models.json +19 -0
  19. package/config/codebuddy/workspace/AGENTS.md +13 -0
  20. package/config/codex/workspace/AGENTS.md +13 -0
  21. package/config/frontend-agent/PROMPT.md +127 -0
  22. package/config/hermes/workspace/AGENTS.md +13 -0
  23. package/config/openclaw/openclaw.json5 +50 -0
  24. package/config/openclaw/workspace/AGENTS.md +9 -0
  25. package/config/opencode/agents/qwen-audio-agent-backend.md +65 -0
  26. package/config/opencode/workspace/AGENTS.md +16 -0
  27. package/config/qoder/workspace/AGENTS.md +16 -0
  28. package/docs/architecture-overview-en.png +0 -0
  29. package/docs/architecture-overview.png +0 -0
  30. package/docs/architecture.md +332 -0
  31. package/docs/configuration.md +386 -0
  32. package/docs/desktop-fluid-orb-thinking.gif +0 -0
  33. package/docs/desktop-goo-orb-thinking.gif +0 -0
  34. package/docs/qwen-audio-agent-three-layer-architecture-en.png +0 -0
  35. package/docs/qwen-audio-agent-three-layer-architecture.png +0 -0
  36. package/package.json +126 -0
  37. package/scripts/audit-dependencies.mjs +109 -0
  38. package/scripts/check-desktop-release-env.mjs +41 -0
  39. package/scripts/codex-acp +58 -0
  40. package/scripts/install-global.mjs +66 -0
  41. package/scripts/openclaw +194 -0
  42. package/scripts/openclaw-gateway +8 -0
  43. package/scripts/opencode-acp +6 -0
  44. package/scripts/opencode-server +179 -0
  45. package/scripts/prepare-build.mjs +38 -0
  46. package/scripts/release-check +182 -0
  47. package/scripts/set-version.mjs +133 -0
  48. package/scripts/start.mjs +43 -0
  49. package/scripts/verify-package.mjs +109 -0
  50. package/server/package.json +18 -0
  51. package/server/src/agent/acp-backend-adapter.mjs +1138 -0
  52. package/server/src/agent/acp-backend-profile.mjs +35 -0
  53. package/server/src/agent/acp-backend-session-utils.mjs +139 -0
  54. package/server/src/agent/acp-process-client.mjs +447 -0
  55. package/server/src/agent/acp-session-registry.mjs +106 -0
  56. package/server/src/agent/acp-session-tools.mjs +257 -0
  57. package/server/src/agent/agent-client.mjs +218 -0
  58. package/server/src/agent/backend-adapter.mjs +15 -0
  59. package/server/src/agent/backend-agent-instructions.mjs +8 -0
  60. package/server/src/agent/backends/codebuddy.mjs +47 -0
  61. package/server/src/agent/backends/codex.mjs +62 -0
  62. package/server/src/agent/backends/generic-acp.mjs +46 -0
  63. package/server/src/agent/backends/hermes.mjs +29 -0
  64. package/server/src/agent/backends/openclaw.mjs +101 -0
  65. package/server/src/agent/backends/opencode.mjs +36 -0
  66. package/server/src/agent/backends/qoder.mjs +47 -0
  67. package/server/src/agent/backends/registry.mjs +32 -0
  68. package/server/src/agent/backends/shared.mjs +46 -0
  69. package/server/src/agent/coordinator.mjs +308 -0
  70. package/server/src/agent/openclaw-adapter.mjs +245 -0
  71. package/server/src/app/bootstrap.mjs +244 -0
  72. package/server/src/conversation/conversation-sync.mjs +183 -0
  73. package/server/src/conversation/frontend-agent-context.mjs +162 -0
  74. package/server/src/conversation/frontend-memory.mjs +328 -0
  75. package/server/src/conversation/profiled-memory-store.mjs +87 -0
  76. package/server/src/conversation/task-result-projector.mjs +18 -0
  77. package/server/src/conversation/user-profile.mjs +234 -0
  78. package/server/src/core/config.mjs +388 -0
  79. package/server/src/core/identity.mjs +93 -0
  80. package/server/src/core/install-paths.mjs +8 -0
  81. package/server/src/core/package-version.mjs +10 -0
  82. package/server/src/core/request-security.mjs +80 -0
  83. package/server/src/index.mjs +67 -0
  84. package/server/src/process/backend-drivers/codebuddy.mjs +15 -0
  85. package/server/src/process/backend-drivers/codex.mjs +15 -0
  86. package/server/src/process/backend-drivers/generic-acp.mjs +18 -0
  87. package/server/src/process/backend-drivers/hermes.mjs +15 -0
  88. package/server/src/process/backend-drivers/openclaw.mjs +35 -0
  89. package/server/src/process/backend-drivers/opencode.mjs +75 -0
  90. package/server/src/process/backend-drivers/qoder.mjs +15 -0
  91. package/server/src/process/backend-drivers/registry.mjs +24 -0
  92. package/server/src/process/backend-drivers/shared.mjs +46 -0
  93. package/server/src/process/managed-backend.mjs +188 -0
  94. package/server/src/task/task-manager.mjs +704 -0
  95. package/server/src/task/task-scheduler.mjs +44 -0
  96. package/server/src/task/task-store.mjs +167 -0
  97. package/server/src/voice/active-voice-clients.mjs +58 -0
  98. package/server/src/voice/announcement/announcement-manager.mjs +402 -0
  99. package/server/src/voice/announcement/announcement-window.mjs +89 -0
  100. package/server/src/voice/input-transcript.mjs +9 -0
  101. package/server/src/voice/realtime-errors.mjs +6 -0
  102. package/server/src/voice/realtime-gateway.mjs +1366 -0
  103. package/server/src/voice/realtime-provider.mjs +771 -0
  104. package/server/src/voice/reconnect-backoff.mjs +30 -0
  105. package/server/src/voice/response-context.mjs +41 -0
  106. package/server/src/voice/session-permission-policy.mjs +56 -0
  107. package/server/src/voice/tools/tool-call-handler.mjs +823 -0
  108. package/server/src/voice/tools/turn-transcripts.mjs +59 -0
  109. package/server/src/voice/turn-correlation.mjs +41 -0
  110. package/shared/backend-catalog.mjs +56 -0
  111. package/shared/gateway-client.mjs +13 -0
  112. package/shared/realtime-events.mjs +55 -0
  113. package/shared/runtime-environment.mjs +422 -0
  114. package/tui/native/macos-voice-io.swift +552 -0
  115. package/tui/native/portaudio-voice-io.py +240 -0
  116. package/tui/package.json +13 -0
  117. package/tui/src/index.mjs +1217 -0
  118. package/tui/src/macos-voice-io.mjs +186 -0
  119. package/tui/src/portaudio-voice-io.mjs +158 -0
  120. package/web/dist/assets/index-BeamDvsE.js +38 -0
  121. package/web/dist/assets/index-BuB0EFih.css +1 -0
  122. package/web/dist/index.html +14 -0
package/README_EN.md ADDED
@@ -0,0 +1,316 @@
1
+ # Qwen Audio Agent
2
+
3
+ [中文](README.md) | [English](README_EN.md)
4
+
5
+ [![CI](https://github.com/QwenAudio/qwen-audio-agent/actions/workflows/ci.yml/badge.svg)](https://github.com/QwenAudio/qwen-audio-agent/actions/workflows/ci.yml)
6
+ [![npm](https://img.shields.io/badge/npm-v0.9.0-orange)](https://www.npmjs.com/package/qwen-audio-agent)
7
+ [![node](https://img.shields.io/badge/node-%E2%89%A522.22.2-brightgreen)](https://nodejs.org/)
8
+ [![license](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE)
9
+
10
+ ## Say it. Don't prompt it.
11
+
12
+ No one says one thing, then stands there waiting for a minute.
13
+
14
+ No one stops an entire conversation because someone needs to look something
15
+ up.
16
+
17
+ Real conversation is continuous.
18
+
19
+ Agents should be too.
20
+
21
+ That is why we built **qwen-audio-agent**.
22
+
23
+ A realtime voice runtime that keeps Agents talking, working, and present.
24
+
25
+ *Real-time Voice Runtime for AI Agents*
26
+
27
+ ## Core Features
28
+
29
+ - Full-duplex realtime voice interaction, natural interruption, and continuous
30
+ multi-turn conversation
31
+ - One-click connection to the Agents you prefer, reusing their existing
32
+ models, tools, MCP servers, and Skills
33
+ - Voice conversation and backend tasks run in parallel, with progress queries
34
+ and cancellation available at any time
35
+ - Task results return automatically to the current context for follow-up and
36
+ revision
37
+ - WebUI, terminal TUI, and a macOS desktop orb
38
+ - Local user profile and personal memory across sessions
39
+
40
+ ## Keep Talking While Tasks Keep Running
41
+
42
+ ![qwen-audio-agent architecture](docs/architecture-overview-en.png)
43
+
44
+ Questions that can be answered directly receive an immediate response. Work
45
+ that needs tools or sustained processing is delegated to a backend Agent.
46
+ Throughout the entire interaction, you are always talking to the same
47
+ assistant.
48
+
49
+ <details>
50
+ <summary>View the detailed architecture</summary>
51
+
52
+ ![qwen-audio-agent integration reference architecture](docs/qwen-audio-agent-three-layer-architecture-en.png)
53
+
54
+ See the [architecture document](docs/architecture.md) for the full design and
55
+ module walkthrough.
56
+
57
+ </details>
58
+
59
+ ## Agent Support
60
+
61
+ | Backend Agent | Status | Main capabilities | Rating |
62
+ | --- | --- | --- | --- |
63
+ | OpenCode | Supported | Tools and coding tasks, project Sessions, progress queries, and cancellation | ★★★★★ |
64
+ | OpenClaw | Supported | Agents, tools, task execution, and permission control | ★★★★★ |
65
+ | Qoder | Supported | Native CLI Sessions, starting or continuing projects, task delegation, and cancellation | ★★★★★ |
66
+ | Hermes | Supported | Native ACP, tool calls, project Sessions, and task execution | ★★★★☆ |
67
+ | CodeBuddy | Supported | Native ACP, custom models, project Sessions, and permission control | ★★★★☆ |
68
+ | Codex | Supported | Codex ACP, coding tasks, project Sessions, and permission control | ★★★★☆ |
69
+
70
+ Ratings reflect the current integration completeness, compatibility, and
71
+ extent of real-world validation. Five stars identify recommended integrations
72
+ that have been thoroughly tested; four stars identify integrations still in
73
+ development or not yet validated to the same extent. Refer to the status column
74
+ for current availability. See the [configuration guide](docs/configuration.md)
75
+ for detailed setup and capability boundaries.
76
+
77
+ ## Installation
78
+
79
+ You need Node.js 22.22.2+ or 24.15.0+, npm 10+, and a
80
+ DashScope API Key. The repository includes `.nvmrc` and `.node-version`; if you
81
+ use nvm, run `nvm use`.
82
+
83
+ One-line install (recommended, from npm):
84
+
85
+ ```bash
86
+ npm install -g qwen-audio-agent
87
+ ```
88
+
89
+ You can also install the latest code directly from GitHub:
90
+
91
+ ```bash
92
+ npm install -g git+https://github.com/QwenAudio/qwen-audio-agent.git
93
+ ```
94
+
95
+ Install from source:
96
+
97
+ ```bash
98
+ git clone https://github.com/QwenAudio/qwen-audio-agent.git
99
+ cd qwen-audio-agent
100
+ npm install
101
+ npm run install:global
102
+ ```
103
+
104
+ Upgrade to the latest npm release:
105
+
106
+ ```bash
107
+ npm install -g qwen-audio-agent@latest
108
+ ```
109
+
110
+ Upgrade to the latest code from GitHub:
111
+
112
+ ```bash
113
+ npm install -g git+https://github.com/QwenAudio/qwen-audio-agent.git
114
+ ```
115
+
116
+ ## Quick Start
117
+
118
+ 1. Create your configuration:
119
+
120
+ ```bash
121
+ qwenaudio config
122
+ ```
123
+
124
+ 2. Open the displayed `config.env` file, add your DashScope API Key, and select
125
+ OpenClaw:
126
+
127
+ ```dotenv
128
+ DASHSCOPE_API_KEY=your-key
129
+ AGENT_PROTOCOL=openclaw
130
+ ```
131
+
132
+ 3. Start the Gateway in one terminal:
133
+
134
+ ```bash
135
+ qwenaudio
136
+ ```
137
+
138
+ 4. Open another terminal and start the TUI:
139
+
140
+ ```bash
141
+ qwenaudio tui
142
+ ```
143
+
144
+ You can use the browser interface instead:
145
+
146
+ ```bash
147
+ qwenaudio webui
148
+ ```
149
+
150
+ ### TUI Notes
151
+
152
+ | Platform | Default mode | How to interrupt |
153
+ | --- | --- | --- |
154
+ | macOS | Full duplex with echo cancellation | Start speaking |
155
+ | Linux / Windows | Half duplex | Press `x` during playback |
156
+
157
+ Before first use on Linux or Windows, install `sounddevice` and ensure system
158
+ PortAudio is available. You can also enable full-duplex mode without echo
159
+ cancellation. Wear headphones in this mode to avoid speaker output causing
160
+ false transcription:
161
+
162
+ ```bash
163
+ qwenaudio tui --audio-mode full
164
+ ```
165
+
166
+ ## macOS Desktop App
167
+
168
+ The desktop app provides a persistent voice orb. Start the Gateway before using
169
+ it and grant microphone permission on first launch. The settings page lets you
170
+ switch the Gateway address and orb appearance, and shows the active model and
171
+ backend Agent.
172
+
173
+ The desktop app includes a streaming wave orb and a liquid gradient orb. Their
174
+ original animated thinking / breathing states are shown below:
175
+
176
+ | Streaming Wave Orb | Liquid Gradient Orb |
177
+ | --- | --- |
178
+ | ![Streaming wave orb thinking animation](docs/desktop-fluid-orb-thinking.gif) | ![Liquid gradient orb thinking animation](docs/desktop-goo-orb-thinking.gif) |
179
+
180
+ Download the `.dmg` from the releases page, open it, and drag
181
+ **Qwen Audio Agent** into Applications.
182
+
183
+ Build a local test package from source:
184
+
185
+ ```bash
186
+ npm run desktop:build:local
187
+ ```
188
+
189
+ ## Run the Gateway in the Background
190
+
191
+ To keep your personal assistant available, install the Gateway as a user
192
+ service:
193
+
194
+ ```bash
195
+ qwenaudio gateway install
196
+ ```
197
+
198
+ Common management commands:
199
+
200
+ ```bash
201
+ qwenaudio gateway status
202
+ qwenaudio gateway restart
203
+ qwenaudio gateway stop
204
+ qwenaudio gateway start
205
+ qwenaudio gateway uninstall
206
+ ```
207
+
208
+ ## Choose a Backend Agent
209
+
210
+ Select the backend Agent with `AGENT_PROTOCOL` (required, no default value).
211
+ Use OpenClaw:
212
+
213
+ ```dotenv
214
+ AGENT_PROTOCOL=openclaw
215
+ ```
216
+
217
+ Use OpenCode:
218
+
219
+ ```dotenv
220
+ AGENT_PROTOCOL=opencode
221
+ ```
222
+
223
+ Use Qoder:
224
+
225
+ ```dotenv
226
+ AGENT_PROTOCOL=qoder
227
+ QODER_MODEL=auto
228
+ ```
229
+
230
+ Hermes, CodeBuddy, and Codex can also be selected directly:
231
+
232
+ ```dotenv
233
+ AGENT_PROTOCOL=hermes
234
+ # Or codebuddy or codex
235
+ ```
236
+
237
+ Use another Agent that supports ACP over stdio:
238
+
239
+ ```dotenv
240
+ AGENT_PROTOCOL=acp
241
+ ACP_COMMAND=your-agent
242
+ ACP_ARGS=["--acp"]
243
+ ```
244
+
245
+ 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`.
246
+
247
+ Backend permissions default to `native`, so the backend Agent asks when
248
+ permission is required. Enable the following option only in trusted projects
249
+ and only if you explicitly accept automatic command execution and file
250
+ changes:
251
+
252
+ ```dotenv
253
+ QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE=full
254
+ ```
255
+
256
+ See the [configuration guide](docs/configuration.md) for all options.
257
+
258
+ ## User Profile and Memory
259
+
260
+ User data is stored in `~/.config/qwaudio/`:
261
+
262
+ - `USER.md`: your preferred name, location, preferences, and frequently used
263
+ projects
264
+ - `frontend-memory.json`: information you explicitly ask the assistant to
265
+ remember long term
266
+ - `tasks.json`: task results and pending notification state
267
+
268
+ These files remain on your computer and are never written to the source
269
+ repository. You can edit `USER.md` directly or ask the assistant to remember or
270
+ forget information during a conversation.
271
+
272
+ ## Usage Notes
273
+
274
+ - Do not store passwords, API Keys, verification codes, or access tokens in
275
+ your user profile or conversations.
276
+ - Microphone audio and realtime conversations are sent to the configured Qwen
277
+ Audio Realtime service.
278
+ - Backend tasks may call models, tools, MCP servers, and external services
279
+ configured for the selected Agent.
280
+ - `full` permission allows command execution and file changes. Use it only in
281
+ trusted projects.
282
+ - The Gateway is local-only by default. Do not expose it directly to a LAN or
283
+ the public internet.
284
+ - Wear headphones when using full duplex without echo cancellation on Linux or
285
+ Windows.
286
+
287
+ See the [privacy notice](PRIVACY.md) for data boundaries and the
288
+ [configuration guide](docs/configuration.md) for network and permission
289
+ settings.
290
+
291
+ ## Development
292
+
293
+ ```bash
294
+ npm install
295
+ npm run build
296
+ npm test
297
+ ```
298
+
299
+ ```bash
300
+ npm run dev # Gateway and WebUI with hot reload
301
+ npm run desktop # macOS desktop orb
302
+ ```
303
+
304
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for more about building, testing, and
305
+ releasing.
306
+
307
+ ## Contributing and Security
308
+
309
+ - Development and contribution guide: [CONTRIBUTING.md](CONTRIBUTING.md)
310
+ - Security reports: [SECURITY.md](SECURITY.md)
311
+ - Data flow and privacy: [PRIVACY.md](PRIVACY.md)
312
+ - Third-party notices: [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)
313
+
314
+ ## License
315
+
316
+ [Apache License 2.0](LICENSE)
package/SECURITY.md ADDED
@@ -0,0 +1,32 @@
1
+ # Security Policy
2
+
3
+ ## 支持范围
4
+
5
+ 安全修复优先提供给最新发布版本。尚未发布的 main 分支问题会在下一个版本中修复。
6
+
7
+ ## 报告安全问题
8
+
9
+ 请不要公开提交包含利用细节、用户数据或密钥的 Issue。优先使用 GitHub 仓库的
10
+ **Security → Report a vulnerability** 私密报告功能:
11
+
12
+ https://github.com/QwenAudio/qwen-audio-agent/security/advisories/new
13
+
14
+ 报告请包含受影响版本、复现条件、潜在影响和可行的缓解方式。维护者确认并准备好
15
+ 修复前,请避免公开漏洞细节。
16
+
17
+ ## 安全边界
18
+
19
+ - Gateway 默认仅供本机使用。
20
+ - 远程访问必须通过带认证的 HTTPS 反向代理,并显式配置可信 Origin。
21
+ - `QWEN_AUDIO_AGENT_AUTH_SECRET` 是身份签名密钥,不是远程访问密码。
22
+ - API Key、用户档案、记忆和任务状态必须留在用户配置目录,不得提交到仓库。
23
+ - 从不受信任来源获得的 Agent 输出、Markdown、URL 和媒体都应视为不可信数据。
24
+
25
+ ## 临时构建链例外
26
+
27
+ 正式发布检查会同时审计生产依赖和开发/构建依赖。目前
28
+ `electron-builder 26.15.7` 的间接依赖仍受
29
+ `GHSA-mh99-v99m-4gvg` 影响。该依赖只在维护者使用仓库内受控路径构建桌面安装包时
30
+ 运行,不会进入 Gateway、TUI 或 WebUI 的用户运行时。项目对这一单一 advisory
31
+ 设置了截至 2026-08-15 的临时例外;出现兼容的稳定版修复后必须升级并删除例外,
32
+ 到期后发布检查会自动失败。任何其他 high 或 critical 问题都会立即阻止发布。
@@ -0,0 +1,26 @@
1
+ # Third-Party Notices
2
+
3
+ qwen-audio-agent 使用并分发若干开源组件。项目自身使用 Apache License 2.0;
4
+ 第三方组件仍分别受其原许可证约束。
5
+
6
+ 主要组件包括:
7
+
8
+ | Component | License | Project |
9
+ | --- | --- | --- |
10
+ | Electron | MIT | https://github.com/electron/electron |
11
+ | React / React DOM | MIT | https://github.com/facebook/react |
12
+ | Express | MIT | https://github.com/expressjs/express |
13
+ | ws | MIT | https://github.com/websockets/ws |
14
+ | Vite | MIT | https://github.com/vitejs/vite |
15
+ | react-markdown | MIT | https://github.com/remarkjs/react-markdown |
16
+ | remark-gfm | MIT | https://github.com/remarkjs/remark-gfm |
17
+ | electron-builder | MIT | https://github.com/electron-userland/electron-builder |
18
+ | Agent Client Protocol TypeScript SDK | Apache-2.0 | https://github.com/agentclientprotocol/typescript-sdk |
19
+ | Codex ACP adapter | Apache-2.0 | https://github.com/agentclientprotocol/codex-acp |
20
+ | Model Context Protocol TypeScript SDK | MIT | https://github.com/modelcontextprotocol/typescript-sdk |
21
+ | Zod | MIT | https://github.com/colinhacks/zod |
22
+ | concurrently | MIT | https://github.com/open-cli-tools/concurrently |
23
+
24
+ 完整、可复现的依赖版本记录在 `package-lock.json`。npm 安装保留每个依赖包自带的
25
+ 许可证文件;Electron 安装包同时保留 Electron/Chromium 自带的许可证与 notices。
26
+ 发布者在升级或新增依赖时必须检查其许可证兼容性,并同步更新本文件。
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { main } from '../src/launcher.mjs'
4
+
5
+ main(process.argv.slice(2)).then(code => {
6
+ if (Number.isInteger(code)) process.exitCode = code
7
+ }).catch(error => {
8
+ process.stderr.write(`qwenaudio: ${error.message}\n`)
9
+ process.exitCode = 1
10
+ })
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@qwen-audio-agent/cli",
3
+ "private": true,
4
+ "version": "0.9.0",
5
+ "type": "module",
6
+ "bin": {
7
+ "qwenaudio": "./bin/qwenaudio.mjs"
8
+ },
9
+ "scripts": {
10
+ "start": "node bin/qwenaudio.mjs",
11
+ "test": "node --test"
12
+ }
13
+ }
@@ -0,0 +1,247 @@
1
+ import { randomUUID } from 'node:crypto'
2
+ import {
3
+ backendDefinition,
4
+ backendNames,
5
+ } from '../../shared/backend-catalog.mjs'
6
+
7
+ const COMMANDS = new Set(['gateway', 'tui', 'webui', 'status', 'config'])
8
+ const GATEWAY_ACTIONS = new Set([
9
+ 'run',
10
+ 'install',
11
+ 'start',
12
+ 'stop',
13
+ 'restart',
14
+ 'status',
15
+ 'uninstall',
16
+ ])
17
+ const BACKEND_MODES = new Set(['managed', 'compatible'])
18
+ const BACKEND_PERMISSION_MODES = new Set(['native', 'full'])
19
+ const TUI_AUDIO_MODES = new Set(['half', 'full'])
20
+
21
+ export function createVoiceSessionId() {
22
+ return `voice-${randomUUID().replaceAll('-', '')}`
23
+ }
24
+
25
+ function nextValue(argv, index, option) {
26
+ const value = argv[index + 1]
27
+ if (!value || value.startsWith('-')) throw new Error(`${option} 缺少参数`)
28
+ return value
29
+ }
30
+
31
+ function cleanOrigin(value, label) {
32
+ let url
33
+ try {
34
+ url = new URL(value)
35
+ } catch {
36
+ throw new Error(`无效的${label}:${value}`)
37
+ }
38
+ if (!['http:', 'https:'].includes(url.protocol)) {
39
+ throw new Error(`${label}只支持 http 或 https`)
40
+ }
41
+ return url.origin
42
+ }
43
+
44
+ export function parseArguments(argv, env = process.env) {
45
+ const args = [...argv]
46
+ const first = args[0]
47
+ const command = first && !first.startsWith('-') ? args.shift() : 'gateway'
48
+ if (!COMMANDS.has(command)) throw new Error(`未知命令:${command}`)
49
+ const gatewayAction = command === 'gateway'
50
+ && args[0]
51
+ && !args[0].startsWith('-')
52
+ ? args.shift()
53
+ : command === 'status' ? 'status' : 'run'
54
+ if (command === 'gateway' && !GATEWAY_ACTIONS.has(gatewayAction)) {
55
+ throw new Error(`未知 Gateway 命令:${gatewayAction}`)
56
+ }
57
+
58
+ const options = {
59
+ command,
60
+ gatewayAction,
61
+ url: env.QWEN_AUDIO_AGENT_URL || 'http://127.0.0.1:3101',
62
+ sessionId: env.QWEN_AUDIO_AGENT_SESSION_ID || createVoiceSessionId(),
63
+ audioMode: String(
64
+ env.QWEN_AUDIO_AGENT_TUI_AUDIO_MODE || 'half',
65
+ ).toLowerCase(),
66
+ backend: String(env.AGENT_PROTOCOL || '').toLowerCase(),
67
+ backendMode: String(
68
+ env.QWEN_AUDIO_AGENT_BACKEND_MODE || 'managed',
69
+ ).toLowerCase(),
70
+ backendPermissionMode: String(
71
+ env.QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE || 'native',
72
+ ).toLowerCase(),
73
+ backendAgent: String(
74
+ env.QWEN_AUDIO_AGENT_BACKEND_AGENT || '',
75
+ ).trim(),
76
+ backendUrl: '',
77
+ openBrowser: true,
78
+ takeover: false,
79
+ gatewayConfigurationSpecified: false,
80
+ }
81
+ let audioModeSpecified = false
82
+
83
+ for (let index = 0; index < args.length; index += 1) {
84
+ const argument = args[index]
85
+ if (argument === '--url') {
86
+ options.url = nextValue(args, index++, '--url')
87
+ options.gatewayConfigurationSpecified = true
88
+ } else if (argument === '--backend') {
89
+ options.backend = nextValue(args, index++, '--backend').toLowerCase()
90
+ options.gatewayConfigurationSpecified = true
91
+ } else if (argument === '--backend-mode') {
92
+ options.backendMode = nextValue(
93
+ args,
94
+ index++,
95
+ '--backend-mode',
96
+ ).toLowerCase()
97
+ options.gatewayConfigurationSpecified = true
98
+ } else if (argument === '--backend-agent') {
99
+ options.backendAgent = nextValue(args, index++, '--backend-agent').trim()
100
+ options.gatewayConfigurationSpecified = true
101
+ } else if (argument === '--backend-permission-mode') {
102
+ options.backendPermissionMode = nextValue(
103
+ args,
104
+ index++,
105
+ '--backend-permission-mode',
106
+ ).toLowerCase()
107
+ options.gatewayConfigurationSpecified = true
108
+ } else if (argument === '--backend-url') {
109
+ options.backendUrl = nextValue(args, index++, '--backend-url')
110
+ options.gatewayConfigurationSpecified = true
111
+ } else if (argument === '--session') {
112
+ options.sessionId = nextValue(args, index++, '--session')
113
+ } else if (argument === '--audio-mode') {
114
+ options.audioMode = nextValue(args, index++, '--audio-mode').toLowerCase()
115
+ audioModeSpecified = true
116
+ } else if (argument === '--no-open') options.openBrowser = false
117
+ else if (argument === '--takeover') options.takeover = true
118
+ else if (argument === '--help' || argument === '-h') options.help = true
119
+ else throw new Error(`未知参数:${argument}`)
120
+ }
121
+
122
+ const definition = options.backend
123
+ ? backendDefinition(options.backend)
124
+ : null
125
+ if (options.backend && !definition) {
126
+ throw new Error(
127
+ `不支持的后台:${options.backend}(可选 ${backendNames().join('、')})`,
128
+ )
129
+ }
130
+ if (!BACKEND_MODES.has(options.backendMode)) {
131
+ throw new Error(
132
+ `不支持的后台模式:${options.backendMode}(可选 managed、compatible)`,
133
+ )
134
+ }
135
+ if (!BACKEND_PERMISSION_MODES.has(options.backendPermissionMode)) {
136
+ throw new Error(
137
+ `不支持的后台权限模式:${options.backendPermissionMode}`
138
+ + '(可选 native、full)',
139
+ )
140
+ }
141
+ if (command !== 'webui' && !options.openBrowser) {
142
+ throw new Error('--no-open 只适用于 webui')
143
+ }
144
+ if (!['tui', 'webui'].includes(command) && options.takeover) {
145
+ throw new Error('--takeover 只适用于 tui 或 webui')
146
+ }
147
+ if (command !== 'tui' && audioModeSpecified) {
148
+ throw new Error('--audio-mode 只适用于 tui')
149
+ }
150
+ if (command === 'tui' && !TUI_AUDIO_MODES.has(options.audioMode)) {
151
+ throw new Error(
152
+ `不支持的音频模式:${options.audioMode}(可选 half、full)`,
153
+ )
154
+ }
155
+ if (
156
+ command === 'gateway'
157
+ && !['run', 'status'].includes(options.gatewayAction)
158
+ && options.gatewayConfigurationSpecified
159
+ ) {
160
+ throw new Error(
161
+ 'Gateway 后台服务从 config.env 读取配置;请先修改配置,再执行服务命令',
162
+ )
163
+ }
164
+
165
+ options.url = cleanOrigin(options.url, ' Gateway URL')
166
+ const configuredBackendUrl = definition?.baseUrlEnvironment
167
+ ? env[definition.baseUrlEnvironment] || definition.defaultBaseUrl
168
+ : ''
169
+ options.backendUrl = definition?.baseUrlEnvironment
170
+ ? cleanOrigin(options.backendUrl || configuredBackendUrl, '后台地址')
171
+ : ''
172
+ if (
173
+ definition
174
+ && !definition.supportsCompatible
175
+ && options.backendMode !== 'managed'
176
+ ) {
177
+ throw new Error(`${definition.label} 后台当前只支持 managed 模式`)
178
+ }
179
+ if (
180
+ options.backendPermissionMode === 'full'
181
+ && options.backendMode !== 'managed'
182
+ ) {
183
+ throw new Error('最高权限模式只支持由 Gateway 管理的后台 Agent')
184
+ }
185
+ if (
186
+ definition
187
+ && options.backendPermissionMode === 'full'
188
+ && !definition.supportsFullPermission
189
+ ) {
190
+ throw new Error(
191
+ `${definition.label} 不支持 Gateway 统一最高权限模式`,
192
+ )
193
+ }
194
+ if (
195
+ command === 'gateway'
196
+ && options.gatewayAction === 'run'
197
+ && !options.backend
198
+ ) {
199
+ throw new Error(
200
+ '必须指定后台 Agent:在 config.env 设置 AGENT_PROTOCOL,'
201
+ + `或使用 --backend(可选 ${backendNames().join('、')})`,
202
+ )
203
+ }
204
+ options.sessionId = String(options.sessionId || '').trim()
205
+ if (!options.sessionId) throw new Error('--session 不能为空')
206
+ return options
207
+ }
208
+
209
+ export function helpText() {
210
+ return [
211
+ 'qwenaudio',
212
+ '',
213
+ '用法:',
214
+ ' qwenaudio [gateway] [run] [选项] 前台运行 Gateway(默认)',
215
+ ' qwenaudio gateway install 安装并启动后台常驻服务',
216
+ ' qwenaudio gateway start 启动后台服务',
217
+ ' qwenaudio gateway status 查看 Gateway 状态',
218
+ ' qwenaudio gateway stop 停止后台服务',
219
+ ' qwenaudio gateway restart 重启后台服务',
220
+ ' qwenaudio gateway uninstall 移除后台常驻服务',
221
+ ' qwenaudio tui [选项] 连接现有 Gateway 的终端界面',
222
+ ' qwenaudio webui [选项] 打开现有 Gateway 的 WebUI',
223
+ ' qwenaudio status [选项] gateway status 的兼容别名',
224
+ ' qwenaudio config 显示用户配置文件位置',
225
+ '',
226
+ 'Gateway 选项:',
227
+ ' --url URL Gateway 地址(默认 http://127.0.0.1:3101)',
228
+ ' --backend NAME 必填:openclaw、opencode、qoder、hermes、codebuddy、codex 或 acp(也可在 config.env 设置 AGENT_PROTOCOL)',
229
+ ' --backend-mode MODE managed(默认)或 compatible',
230
+ ' --backend-permission-mode MODE native(默认)或 full(最高权限)',
231
+ ' --backend-url URL 后台 Server 地址',
232
+ ' --backend-agent ID compatible 模式使用的 Agent',
233
+ '',
234
+ '界面选项:',
235
+ ' --session ID 复用指定语音会话',
236
+ ' --audio-mode MODE Linux / Windows 使用 half(默认)或 full',
237
+ ' --takeover 接管当前语音控制权',
238
+ ' --no-open WebUI 只打印地址,不打开浏览器',
239
+ ' -h, --help 显示帮助',
240
+ '',
241
+ 'TUI 按键:',
242
+ ' x 半双工模式下手动打断当前回复',
243
+ ' m 静音 / 恢复麦克风',
244
+ ' h 在 TUI 内显示帮助',
245
+ ' q 退出 TUI',
246
+ ].join('\n')
247
+ }