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.
- package/CONTRIBUTING.md +58 -0
- package/LICENSE +201 -0
- package/NOTICE +5 -0
- package/PRIVACY.md +34 -0
- package/README.md +282 -0
- package/README_EN.md +316 -0
- package/SECURITY.md +32 -0
- package/THIRD_PARTY_NOTICES.md +26 -0
- package/cli/bin/qwenaudio.mjs +10 -0
- package/cli/package.json +13 -0
- package/cli/src/arguments.mjs +247 -0
- package/cli/src/gateway-service.mjs +386 -0
- package/cli/src/instance-lock.mjs +65 -0
- package/cli/src/launcher.mjs +217 -0
- package/cli/src/runtime.mjs +356 -0
- package/cli/src/webui.mjs +61 -0
- package/config/acp/workspace/AGENTS.md +13 -0
- package/config/codebuddy/workspace/.codebuddy/models.json +19 -0
- package/config/codebuddy/workspace/AGENTS.md +13 -0
- package/config/codex/workspace/AGENTS.md +13 -0
- package/config/frontend-agent/PROMPT.md +127 -0
- package/config/hermes/workspace/AGENTS.md +13 -0
- package/config/openclaw/openclaw.json5 +50 -0
- package/config/openclaw/workspace/AGENTS.md +9 -0
- package/config/opencode/agents/qwen-audio-agent-backend.md +65 -0
- package/config/opencode/workspace/AGENTS.md +16 -0
- package/config/qoder/workspace/AGENTS.md +16 -0
- package/docs/architecture-overview-en.png +0 -0
- package/docs/architecture-overview.png +0 -0
- package/docs/architecture.md +332 -0
- package/docs/configuration.md +386 -0
- package/docs/desktop-fluid-orb-thinking.gif +0 -0
- package/docs/desktop-goo-orb-thinking.gif +0 -0
- package/docs/qwen-audio-agent-three-layer-architecture-en.png +0 -0
- package/docs/qwen-audio-agent-three-layer-architecture.png +0 -0
- package/package.json +126 -0
- package/scripts/audit-dependencies.mjs +109 -0
- package/scripts/check-desktop-release-env.mjs +41 -0
- package/scripts/codex-acp +58 -0
- package/scripts/install-global.mjs +66 -0
- package/scripts/openclaw +194 -0
- package/scripts/openclaw-gateway +8 -0
- package/scripts/opencode-acp +6 -0
- package/scripts/opencode-server +179 -0
- package/scripts/prepare-build.mjs +38 -0
- package/scripts/release-check +182 -0
- package/scripts/set-version.mjs +133 -0
- package/scripts/start.mjs +43 -0
- package/scripts/verify-package.mjs +109 -0
- package/server/package.json +18 -0
- package/server/src/agent/acp-backend-adapter.mjs +1138 -0
- package/server/src/agent/acp-backend-profile.mjs +35 -0
- package/server/src/agent/acp-backend-session-utils.mjs +139 -0
- package/server/src/agent/acp-process-client.mjs +447 -0
- package/server/src/agent/acp-session-registry.mjs +106 -0
- package/server/src/agent/acp-session-tools.mjs +257 -0
- package/server/src/agent/agent-client.mjs +218 -0
- package/server/src/agent/backend-adapter.mjs +15 -0
- package/server/src/agent/backend-agent-instructions.mjs +8 -0
- package/server/src/agent/backends/codebuddy.mjs +47 -0
- package/server/src/agent/backends/codex.mjs +62 -0
- package/server/src/agent/backends/generic-acp.mjs +46 -0
- package/server/src/agent/backends/hermes.mjs +29 -0
- package/server/src/agent/backends/openclaw.mjs +101 -0
- package/server/src/agent/backends/opencode.mjs +36 -0
- package/server/src/agent/backends/qoder.mjs +47 -0
- package/server/src/agent/backends/registry.mjs +32 -0
- package/server/src/agent/backends/shared.mjs +46 -0
- package/server/src/agent/coordinator.mjs +308 -0
- package/server/src/agent/openclaw-adapter.mjs +245 -0
- package/server/src/app/bootstrap.mjs +244 -0
- package/server/src/conversation/conversation-sync.mjs +183 -0
- package/server/src/conversation/frontend-agent-context.mjs +162 -0
- package/server/src/conversation/frontend-memory.mjs +328 -0
- package/server/src/conversation/profiled-memory-store.mjs +87 -0
- package/server/src/conversation/task-result-projector.mjs +18 -0
- package/server/src/conversation/user-profile.mjs +234 -0
- package/server/src/core/config.mjs +388 -0
- package/server/src/core/identity.mjs +93 -0
- package/server/src/core/install-paths.mjs +8 -0
- package/server/src/core/package-version.mjs +10 -0
- package/server/src/core/request-security.mjs +80 -0
- package/server/src/index.mjs +67 -0
- package/server/src/process/backend-drivers/codebuddy.mjs +15 -0
- package/server/src/process/backend-drivers/codex.mjs +15 -0
- package/server/src/process/backend-drivers/generic-acp.mjs +18 -0
- package/server/src/process/backend-drivers/hermes.mjs +15 -0
- package/server/src/process/backend-drivers/openclaw.mjs +35 -0
- package/server/src/process/backend-drivers/opencode.mjs +75 -0
- package/server/src/process/backend-drivers/qoder.mjs +15 -0
- package/server/src/process/backend-drivers/registry.mjs +24 -0
- package/server/src/process/backend-drivers/shared.mjs +46 -0
- package/server/src/process/managed-backend.mjs +188 -0
- package/server/src/task/task-manager.mjs +704 -0
- package/server/src/task/task-scheduler.mjs +44 -0
- package/server/src/task/task-store.mjs +167 -0
- package/server/src/voice/active-voice-clients.mjs +58 -0
- package/server/src/voice/announcement/announcement-manager.mjs +402 -0
- package/server/src/voice/announcement/announcement-window.mjs +89 -0
- package/server/src/voice/input-transcript.mjs +9 -0
- package/server/src/voice/realtime-errors.mjs +6 -0
- package/server/src/voice/realtime-gateway.mjs +1366 -0
- package/server/src/voice/realtime-provider.mjs +771 -0
- package/server/src/voice/reconnect-backoff.mjs +30 -0
- package/server/src/voice/response-context.mjs +41 -0
- package/server/src/voice/session-permission-policy.mjs +56 -0
- package/server/src/voice/tools/tool-call-handler.mjs +823 -0
- package/server/src/voice/tools/turn-transcripts.mjs +59 -0
- package/server/src/voice/turn-correlation.mjs +41 -0
- package/shared/backend-catalog.mjs +56 -0
- package/shared/gateway-client.mjs +13 -0
- package/shared/realtime-events.mjs +55 -0
- package/shared/runtime-environment.mjs +422 -0
- package/tui/native/macos-voice-io.swift +552 -0
- package/tui/native/portaudio-voice-io.py +240 -0
- package/tui/package.json +13 -0
- package/tui/src/index.mjs +1217 -0
- package/tui/src/macos-voice-io.mjs +186 -0
- package/tui/src/portaudio-voice-io.mjs +158 -0
- package/web/dist/assets/index-BeamDvsE.js +38 -0
- package/web/dist/assets/index-BuB0EFih.css +1 -0
- package/web/dist/index.html +14 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export class TurnTranscripts {
|
|
2
|
+
constructor({ waitMs = 800, maxTurns = 20 } = {}) {
|
|
3
|
+
this.waitMs = waitMs
|
|
4
|
+
this.maxTurns = maxTurns
|
|
5
|
+
this.values = new Map()
|
|
6
|
+
this.waiters = new Map()
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
record(turnId, transcript) {
|
|
10
|
+
if (!turnId) return
|
|
11
|
+
const content = String(transcript || '').trim()
|
|
12
|
+
this.values.set(turnId, content)
|
|
13
|
+
while (this.values.size > this.maxTurns) {
|
|
14
|
+
this.values.delete(this.values.keys().next().value)
|
|
15
|
+
}
|
|
16
|
+
const waiters = this.waiters.get(turnId) || []
|
|
17
|
+
this.waiters.delete(turnId)
|
|
18
|
+
waiters.forEach(resolve => resolve(content))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async transcript(turnId) {
|
|
22
|
+
if (this.values.has(turnId)) return this.values.get(turnId)
|
|
23
|
+
return new Promise(resolve => {
|
|
24
|
+
const waiters = this.waiters.get(turnId) || []
|
|
25
|
+
let settled = false
|
|
26
|
+
const finish = value => {
|
|
27
|
+
if (settled) return
|
|
28
|
+
settled = true
|
|
29
|
+
clearTimeout(timer)
|
|
30
|
+
resolve(value)
|
|
31
|
+
}
|
|
32
|
+
waiters.push(finish)
|
|
33
|
+
this.waiters.set(turnId, waiters)
|
|
34
|
+
const timer = setTimeout(() => {
|
|
35
|
+
const pending = this.waiters.get(turnId) || []
|
|
36
|
+
const index = pending.indexOf(finish)
|
|
37
|
+
if (index >= 0) pending.splice(index, 1)
|
|
38
|
+
if (pending.length) this.waiters.set(turnId, pending)
|
|
39
|
+
else this.waiters.delete(turnId)
|
|
40
|
+
finish('')
|
|
41
|
+
}, this.waitMs)
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async resolveDelegation(turnId, suppliedObjective) {
|
|
46
|
+
const objective = String(suppliedObjective || '').replace(/\s+/g, ' ').trim()
|
|
47
|
+
const transcript = await this.transcript(turnId)
|
|
48
|
+
const originalRequest = String(transcript || objective).replace(/\s+/g, ' ').trim()
|
|
49
|
+
return {
|
|
50
|
+
originalRequest,
|
|
51
|
+
objective: objective || originalRequest,
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
close() {
|
|
56
|
+
this.waiters.forEach(waiters => waiters.forEach(resolve => resolve('')))
|
|
57
|
+
this.waiters.clear()
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export class TurnCorrelation {
|
|
2
|
+
constructor({ maxItems = 100 } = {}) {
|
|
3
|
+
this.maxItems = maxItems
|
|
4
|
+
this.turns = new Map()
|
|
5
|
+
this.invalidItems = new Set()
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
remember(itemId, context) {
|
|
9
|
+
if (!itemId) return
|
|
10
|
+
this.turns.set(itemId, context)
|
|
11
|
+
while (this.turns.size > this.maxItems) {
|
|
12
|
+
const oldest = this.turns.keys().next().value
|
|
13
|
+
this.turns.delete(oldest)
|
|
14
|
+
this.invalidItems.delete(oldest)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
resolve(itemId, fallback) {
|
|
19
|
+
return this.turns.get(itemId) || fallback
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
invalidate(itemId) {
|
|
23
|
+
if (itemId) this.invalidItems.add(itemId)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
isInvalid(itemId) {
|
|
27
|
+
return this.invalidItems.has(itemId)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
complete(itemId, fallback) {
|
|
31
|
+
const context = this.resolve(itemId, fallback)
|
|
32
|
+
const invalid = this.invalidItems.delete(itemId)
|
|
33
|
+
this.turns.delete(itemId)
|
|
34
|
+
return { context, invalid }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
clear() {
|
|
38
|
+
this.turns.clear()
|
|
39
|
+
this.invalidItems.clear()
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const definitions = new Map([
|
|
2
|
+
['opencode', {
|
|
3
|
+
id: 'opencode',
|
|
4
|
+
label: 'OpenCode',
|
|
5
|
+
baseUrlEnvironment: 'OPENCODE_BASE_URL',
|
|
6
|
+
defaultBaseUrl: 'http://127.0.0.1:4096',
|
|
7
|
+
supportsCompatible: true,
|
|
8
|
+
supportsFullPermission: true,
|
|
9
|
+
}],
|
|
10
|
+
['openclaw', {
|
|
11
|
+
id: 'openclaw',
|
|
12
|
+
label: 'OpenClaw',
|
|
13
|
+
baseUrlEnvironment: 'OPENCLAW_BASE_URL',
|
|
14
|
+
defaultBaseUrl: 'http://127.0.0.1:18789',
|
|
15
|
+
supportsCompatible: true,
|
|
16
|
+
supportsFullPermission: false,
|
|
17
|
+
}],
|
|
18
|
+
['qoder', {
|
|
19
|
+
id: 'qoder',
|
|
20
|
+
label: 'Qoder',
|
|
21
|
+
supportsCompatible: false,
|
|
22
|
+
supportsFullPermission: true,
|
|
23
|
+
}],
|
|
24
|
+
['hermes', {
|
|
25
|
+
id: 'hermes',
|
|
26
|
+
label: 'Hermes',
|
|
27
|
+
supportsCompatible: false,
|
|
28
|
+
supportsFullPermission: true,
|
|
29
|
+
}],
|
|
30
|
+
['codebuddy', {
|
|
31
|
+
id: 'codebuddy',
|
|
32
|
+
label: 'CodeBuddy',
|
|
33
|
+
supportsCompatible: false,
|
|
34
|
+
supportsFullPermission: true,
|
|
35
|
+
}],
|
|
36
|
+
['codex', {
|
|
37
|
+
id: 'codex',
|
|
38
|
+
label: 'Codex',
|
|
39
|
+
supportsCompatible: false,
|
|
40
|
+
supportsFullPermission: true,
|
|
41
|
+
}],
|
|
42
|
+
['acp', {
|
|
43
|
+
id: 'acp',
|
|
44
|
+
label: 'ACP Agent',
|
|
45
|
+
supportsCompatible: false,
|
|
46
|
+
supportsFullPermission: false,
|
|
47
|
+
}],
|
|
48
|
+
])
|
|
49
|
+
|
|
50
|
+
export function backendDefinition(protocol) {
|
|
51
|
+
return definitions.get(String(protocol || '').trim().toLowerCase()) || null
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function backendNames() {
|
|
55
|
+
return [...definitions.keys()]
|
|
56
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export async function readGatewayHealth(baseUrl, fetchImpl = fetch) {
|
|
2
|
+
try {
|
|
3
|
+
const response = await fetchImpl(`${baseUrl}/api/health`, {
|
|
4
|
+
signal: AbortSignal.timeout(1500),
|
|
5
|
+
})
|
|
6
|
+
const payload = await response.json()
|
|
7
|
+
return payload && typeof payload === 'object' && payload.backend
|
|
8
|
+
? payload
|
|
9
|
+
: null
|
|
10
|
+
} catch {
|
|
11
|
+
return null
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export const GatewayClientEvent = Object.freeze({
|
|
2
|
+
CONNECT: 'connect',
|
|
3
|
+
UNMUTE: 'unmute',
|
|
4
|
+
MUTE: 'mute',
|
|
5
|
+
INPUT_UNMUTE: 'input.unmute',
|
|
6
|
+
INPUT_MUTE: 'input.mute',
|
|
7
|
+
AUDIO_APPEND: 'audio.append',
|
|
8
|
+
TEXT_MESSAGE: 'text.message',
|
|
9
|
+
INTERRUPT: 'interrupt',
|
|
10
|
+
PLAYBACK_STARTED: 'playback.started',
|
|
11
|
+
PLAYBACK_ENDED: 'playback.ended',
|
|
12
|
+
PLAYBACK_CANCELLED: 'playback.cancelled',
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
export const GatewayServerEvent = Object.freeze({
|
|
16
|
+
GATEWAY_CONNECTED: 'gateway.connected',
|
|
17
|
+
GATEWAY_DISCONNECTED: 'gateway.disconnected',
|
|
18
|
+
VOICE_READY: 'voice.ready',
|
|
19
|
+
VOICE_STATE: 'voice.state',
|
|
20
|
+
VOICE_OWNERSHIP: 'voice.ownership',
|
|
21
|
+
VOICE_DEACTIVATED: 'voice.deactivated',
|
|
22
|
+
TURN_STARTED: 'turn.started',
|
|
23
|
+
PLAYBACK_CLEAR: 'playback.clear',
|
|
24
|
+
AUDIO_DELTA: 'audio.delta',
|
|
25
|
+
AUDIO_DONE: 'audio.done',
|
|
26
|
+
RESPONSE_STARTED: 'response.started',
|
|
27
|
+
RESPONSE_INTERRUPTED: 'response.interrupted',
|
|
28
|
+
TRANSCRIPT_DELTA: 'transcript.delta',
|
|
29
|
+
TRANSCRIPT_FINAL: 'transcript.final',
|
|
30
|
+
TRANSCRIPT_DISCARD: 'transcript.discard',
|
|
31
|
+
TIMELINE_INLINE: 'timeline.inline',
|
|
32
|
+
ERROR: 'error',
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
export const GatewayTaskEvent = Object.freeze({
|
|
36
|
+
RUNNING: 'task.running',
|
|
37
|
+
DELEGATED: 'task.delegated',
|
|
38
|
+
FINALIZING: 'task.finalizing',
|
|
39
|
+
CANCELLING: 'task.cancelling',
|
|
40
|
+
PROGRESS: 'task.progress',
|
|
41
|
+
COMPLETED: 'task.completed',
|
|
42
|
+
FAILED: 'task.failed',
|
|
43
|
+
CANCELLED: 'task.cancelled',
|
|
44
|
+
PERMISSION_REQUESTED: 'task.permission.requested',
|
|
45
|
+
PERMISSION_RESOLVED: 'task.permission.resolved',
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
export const GATEWAY_CLIENT_EVENT_TYPES = new Set(
|
|
49
|
+
Object.values(GatewayClientEvent),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
export const GATEWAY_SERVER_EVENT_TYPES = new Set([
|
|
53
|
+
...Object.values(GatewayServerEvent),
|
|
54
|
+
...Object.values(GatewayTaskEvent),
|
|
55
|
+
])
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto'
|
|
2
|
+
import {
|
|
3
|
+
constants,
|
|
4
|
+
copyFileSync,
|
|
5
|
+
chmodSync,
|
|
6
|
+
linkSync,
|
|
7
|
+
lstatSync,
|
|
8
|
+
mkdirSync,
|
|
9
|
+
readFileSync,
|
|
10
|
+
unlinkSync,
|
|
11
|
+
writeFileSync,
|
|
12
|
+
} from 'node:fs'
|
|
13
|
+
import { homedir } from 'node:os'
|
|
14
|
+
import { dirname, resolve } from 'node:path'
|
|
15
|
+
import { parseEnv } from 'node:util'
|
|
16
|
+
|
|
17
|
+
const SECRET_KEY = 'QWEN_AUDIO_AGENT_AUTH_SECRET'
|
|
18
|
+
const USER_CONFIG_TEMPLATE = [
|
|
19
|
+
'# qwen-audio-agent 用户配置',
|
|
20
|
+
'DASHSCOPE_API_KEY=',
|
|
21
|
+
'QWEN_AUDIO_REALTIME_PROVIDER=dashscope',
|
|
22
|
+
'',
|
|
23
|
+
'# 必填:选择后台 Agent(openclaw、opencode、qoder、hermes、codebuddy、codex 或 acp)',
|
|
24
|
+
'AGENT_PROTOCOL=',
|
|
25
|
+
'# QWEN_AUDIO_AGENT_BACKEND_MODE=managed',
|
|
26
|
+
'# 权限模式:native(后台自行询问)或 full(最高权限;仅支持安全映射的 managed 后端)',
|
|
27
|
+
'# QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE=native',
|
|
28
|
+
'# QWEN_AUDIO_AGENT_BACKEND_MODEL=qwen3.7-max',
|
|
29
|
+
'# 兼容模式可选:QWEN_AUDIO_AGENT_BACKEND_AGENT=已有 Agent ID',
|
|
30
|
+
'# 通用 ACP:ACP_COMMAND=your-agent,ACP_ARGS=["--acp"]',
|
|
31
|
+
'',
|
|
32
|
+
].join('\n')
|
|
33
|
+
const USER_PROFILE_TEMPLATE = [
|
|
34
|
+
'# USER',
|
|
35
|
+
'',
|
|
36
|
+
'<!--',
|
|
37
|
+
'这是你的本地长期档案。只填写希望语音助手长期了解的稳定信息。',
|
|
38
|
+
'不要在这里保存密码、API Key、验证码或令牌。',
|
|
39
|
+
'-->',
|
|
40
|
+
'',
|
|
41
|
+
'## 基本信息',
|
|
42
|
+
'',
|
|
43
|
+
'- 称呼:',
|
|
44
|
+
'- 所在地:',
|
|
45
|
+
'',
|
|
46
|
+
'## 长期偏好',
|
|
47
|
+
'',
|
|
48
|
+
'- ',
|
|
49
|
+
'',
|
|
50
|
+
'## 常用项目',
|
|
51
|
+
'',
|
|
52
|
+
'- ',
|
|
53
|
+
'',
|
|
54
|
+
].join('\n')
|
|
55
|
+
|
|
56
|
+
function loadFile(path, env) {
|
|
57
|
+
let values
|
|
58
|
+
try {
|
|
59
|
+
values = parseEnv(readFileSync(path, 'utf8'))
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (error.code === 'ENOENT') return false
|
|
62
|
+
throw error
|
|
63
|
+
}
|
|
64
|
+
for (const [key, value] of Object.entries(values)) {
|
|
65
|
+
if (env[key] === undefined) env[key] = value
|
|
66
|
+
}
|
|
67
|
+
return true
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function userConfigDirectory(
|
|
71
|
+
env = process.env,
|
|
72
|
+
homeDirectory = homedir(),
|
|
73
|
+
) {
|
|
74
|
+
if (env.QWAUDIO_CONFIG_DIR) return resolve(env.QWAUDIO_CONFIG_DIR)
|
|
75
|
+
const base = env.XDG_CONFIG_HOME
|
|
76
|
+
? resolve(env.XDG_CONFIG_HOME)
|
|
77
|
+
: resolve(homeDirectory, '.config')
|
|
78
|
+
return resolve(base, 'qwaudio')
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function ensureGeneratedSecret(env, configDirectory) {
|
|
82
|
+
if (env[SECRET_KEY]) return { generated: false, statePath: null }
|
|
83
|
+
const statePath = resolve(configDirectory, 'state.env')
|
|
84
|
+
loadFile(statePath, env)
|
|
85
|
+
if (env[SECRET_KEY]) {
|
|
86
|
+
try {
|
|
87
|
+
chmodSync(statePath, 0o600)
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (error.code !== 'ENOENT') throw error
|
|
90
|
+
}
|
|
91
|
+
return { generated: false, statePath }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
mkdirSync(configDirectory, { recursive: true, mode: 0o700 })
|
|
95
|
+
const secret = randomBytes(32).toString('hex')
|
|
96
|
+
try {
|
|
97
|
+
writeFileSync(statePath, `${SECRET_KEY}=${secret}\n`, {
|
|
98
|
+
encoding: 'utf8',
|
|
99
|
+
flag: 'wx',
|
|
100
|
+
mode: 0o600,
|
|
101
|
+
})
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error.code !== 'EEXIST') throw error
|
|
104
|
+
loadFile(statePath, env)
|
|
105
|
+
if (!env[SECRET_KEY]) {
|
|
106
|
+
throw new Error(`自动生成的本地认证配置无效:${statePath}`)
|
|
107
|
+
}
|
|
108
|
+
return { generated: false, statePath }
|
|
109
|
+
}
|
|
110
|
+
env[SECRET_KEY] = secret
|
|
111
|
+
return { generated: true, statePath }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function ensureUserConfig(configDirectory) {
|
|
115
|
+
const configPath = resolve(configDirectory, 'config.env')
|
|
116
|
+
try {
|
|
117
|
+
writeFileSync(configPath, USER_CONFIG_TEMPLATE, {
|
|
118
|
+
encoding: 'utf8',
|
|
119
|
+
flag: 'wx',
|
|
120
|
+
mode: 0o600,
|
|
121
|
+
})
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (error.code !== 'EEXIST') throw error
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
chmodSync(configPath, 0o600)
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (error.code !== 'ENOENT') throw error
|
|
129
|
+
}
|
|
130
|
+
return configPath
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function ensureUserProfile(configDirectory) {
|
|
134
|
+
const profilePath = resolve(configDirectory, 'USER.md')
|
|
135
|
+
try {
|
|
136
|
+
writeFileSync(profilePath, USER_PROFILE_TEMPLATE, {
|
|
137
|
+
encoding: 'utf8',
|
|
138
|
+
flag: 'wx',
|
|
139
|
+
mode: 0o600,
|
|
140
|
+
})
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (error.code !== 'EEXIST') throw error
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
chmodSync(profilePath, 0o600)
|
|
146
|
+
} catch (error) {
|
|
147
|
+
if (error.code !== 'ENOENT') throw error
|
|
148
|
+
}
|
|
149
|
+
return profilePath
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function ensureManagedWorkspace(directory, templatePath) {
|
|
153
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 })
|
|
154
|
+
const instructionsPath = resolve(directory, 'AGENTS.md')
|
|
155
|
+
try {
|
|
156
|
+
copyFileSync(templatePath, instructionsPath, constants.COPYFILE_EXCL)
|
|
157
|
+
chmodSync(instructionsPath, 0o600)
|
|
158
|
+
} catch (error) {
|
|
159
|
+
if (error.code !== 'EEXIST') throw error
|
|
160
|
+
}
|
|
161
|
+
return directory
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function codeBuddyModelName(env, fallback) {
|
|
165
|
+
const configured = String(
|
|
166
|
+
env.CODEBUDDY_MODEL
|
|
167
|
+
|| env.QWEN_AUDIO_AGENT_BACKEND_MODEL
|
|
168
|
+
|| fallback,
|
|
169
|
+
).trim() || fallback
|
|
170
|
+
const separator = configured.indexOf('/')
|
|
171
|
+
return separator >= 0 ? configured.slice(separator + 1) : configured
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function codeBuddyTemplateContent(templatePath, model) {
|
|
175
|
+
const template = JSON.parse(readFileSync(templatePath, 'utf8'))
|
|
176
|
+
const defaultModel = String(template.models?.[0]?.id || '').trim()
|
|
177
|
+
if (!defaultModel) {
|
|
178
|
+
throw new Error(`CodeBuddy 模型模板缺少默认模型:${templatePath}`)
|
|
179
|
+
}
|
|
180
|
+
template.models = template.models.map(entry => (
|
|
181
|
+
entry.id === defaultModel
|
|
182
|
+
? {
|
|
183
|
+
...entry,
|
|
184
|
+
id: model,
|
|
185
|
+
...(entry.name ? {
|
|
186
|
+
name: model === defaultModel ? entry.name : model,
|
|
187
|
+
} : {}),
|
|
188
|
+
}
|
|
189
|
+
: entry
|
|
190
|
+
))
|
|
191
|
+
if (Array.isArray(template.availableModels)) {
|
|
192
|
+
template.availableModels = template.availableModels.map(id => (
|
|
193
|
+
id === defaultModel ? model : id
|
|
194
|
+
))
|
|
195
|
+
}
|
|
196
|
+
return `${JSON.stringify(template, null, 2)}\n`
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function ensureCodeBuddyTemplate(templatePath, targetPath, env) {
|
|
200
|
+
mkdirSync(dirname(targetPath), { recursive: true, mode: 0o700 })
|
|
201
|
+
const template = JSON.parse(readFileSync(templatePath, 'utf8'))
|
|
202
|
+
const fallback = String(template.models?.[0]?.id || '').trim()
|
|
203
|
+
const model = codeBuddyModelName(env, fallback)
|
|
204
|
+
const desired = codeBuddyTemplateContent(templatePath, model)
|
|
205
|
+
try {
|
|
206
|
+
writeFileSync(targetPath, desired, {
|
|
207
|
+
encoding: 'utf8',
|
|
208
|
+
flag: 'wx',
|
|
209
|
+
mode: 0o600,
|
|
210
|
+
})
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (error.code !== 'EEXIST') throw error
|
|
213
|
+
const current = readFileSync(targetPath, 'utf8')
|
|
214
|
+
let generated = false
|
|
215
|
+
try {
|
|
216
|
+
const currentModel = String(
|
|
217
|
+
JSON.parse(current).models?.[0]?.id || '',
|
|
218
|
+
).trim()
|
|
219
|
+
generated = Boolean(currentModel) && current === codeBuddyTemplateContent(
|
|
220
|
+
templatePath,
|
|
221
|
+
currentModel,
|
|
222
|
+
)
|
|
223
|
+
} catch {
|
|
224
|
+
// Preserve malformed or manually edited user configuration.
|
|
225
|
+
}
|
|
226
|
+
if (generated && current !== desired) {
|
|
227
|
+
writeFileSync(targetPath, desired, {
|
|
228
|
+
encoding: 'utf8',
|
|
229
|
+
mode: 0o600,
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
chmodSync(targetPath, 0o600)
|
|
234
|
+
return targetPath
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function migratePrivateFile(legacyPath, targetPath) {
|
|
238
|
+
try {
|
|
239
|
+
lstatSync(targetPath)
|
|
240
|
+
return false
|
|
241
|
+
} catch (error) {
|
|
242
|
+
if (error.code !== 'ENOENT') throw error
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
if (!lstatSync(legacyPath).isFile()) return false
|
|
247
|
+
} catch (error) {
|
|
248
|
+
if (error.code === 'ENOENT') return false
|
|
249
|
+
throw error
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
try {
|
|
253
|
+
linkSync(legacyPath, targetPath)
|
|
254
|
+
unlinkSync(legacyPath)
|
|
255
|
+
} catch (error) {
|
|
256
|
+
if (['EEXIST', 'ENOENT'].includes(error.code)) return false
|
|
257
|
+
if (error.code !== 'EXDEV') throw error
|
|
258
|
+
try {
|
|
259
|
+
copyFileSync(legacyPath, targetPath, constants.COPYFILE_EXCL)
|
|
260
|
+
unlinkSync(legacyPath)
|
|
261
|
+
} catch (copyError) {
|
|
262
|
+
if (copyError.code === 'EEXIST') return false
|
|
263
|
+
throw copyError
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
chmodSync(targetPath, 0o600)
|
|
267
|
+
return true
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function loadRuntimeEnvironment({
|
|
271
|
+
root,
|
|
272
|
+
env = process.env,
|
|
273
|
+
homeDirectory = homedir(),
|
|
274
|
+
generateSecret = true,
|
|
275
|
+
prepareBackendRuntime = true,
|
|
276
|
+
} = {}) {
|
|
277
|
+
if (!root) throw new Error('loadRuntimeEnvironment requires root')
|
|
278
|
+
const configDirectory = userConfigDirectory(env, homeDirectory)
|
|
279
|
+
const candidates = [
|
|
280
|
+
resolve(root, '.env.local'),
|
|
281
|
+
resolve(root, '.env'),
|
|
282
|
+
resolve(configDirectory, 'config.env'),
|
|
283
|
+
]
|
|
284
|
+
const loadedFiles = candidates.filter(path => loadFile(path, env))
|
|
285
|
+
mkdirSync(configDirectory, { recursive: true, mode: 0o700 })
|
|
286
|
+
const configPath = ensureUserConfig(configDirectory)
|
|
287
|
+
const userProfilePath = ensureUserProfile(configDirectory)
|
|
288
|
+
const frontendMemoryPath = resolve(configDirectory, 'frontend-memory.json')
|
|
289
|
+
const taskStatePath = resolve(configDirectory, 'tasks.json')
|
|
290
|
+
const defaultOpenCodeWorkspace = !env.OPENCODE_WORKSPACE
|
|
291
|
+
const openCodeWorkspace = env.OPENCODE_WORKSPACE
|
|
292
|
+
? resolve(root, env.OPENCODE_WORKSPACE)
|
|
293
|
+
: resolve(configDirectory, 'workspaces/opencode')
|
|
294
|
+
const defaultOpenClawWorkspace = !env.QWEN_AUDIO_AGENT_OPENCLAW_WORKSPACE
|
|
295
|
+
const openClawWorkspace = env.QWEN_AUDIO_AGENT_OPENCLAW_WORKSPACE
|
|
296
|
+
? resolve(root, env.QWEN_AUDIO_AGENT_OPENCLAW_WORKSPACE)
|
|
297
|
+
: resolve(configDirectory, 'workspaces/openclaw')
|
|
298
|
+
const openClawStateDirectory = env.OPENCLAW_STATE_DIR
|
|
299
|
+
? resolve(root, env.OPENCLAW_STATE_DIR)
|
|
300
|
+
: resolve(configDirectory, 'backends/openclaw/state')
|
|
301
|
+
const defaultQoderWorkspace = !env.QODER_WORKSPACE
|
|
302
|
+
const qoderWorkspace = env.QODER_WORKSPACE
|
|
303
|
+
? resolve(root, env.QODER_WORKSPACE)
|
|
304
|
+
: resolve(configDirectory, 'workspaces/qoder')
|
|
305
|
+
const defaultHermesWorkspace = !env.HERMES_WORKSPACE
|
|
306
|
+
const hermesWorkspace = env.HERMES_WORKSPACE
|
|
307
|
+
? resolve(root, env.HERMES_WORKSPACE)
|
|
308
|
+
: resolve(configDirectory, 'workspaces/hermes')
|
|
309
|
+
const defaultCodeBuddyWorkspace = !env.CODEBUDDY_WORKSPACE
|
|
310
|
+
const codeBuddyWorkspace = env.CODEBUDDY_WORKSPACE
|
|
311
|
+
? resolve(root, env.CODEBUDDY_WORKSPACE)
|
|
312
|
+
: resolve(configDirectory, 'workspaces/codebuddy')
|
|
313
|
+
const defaultCodexWorkspace = !env.CODEX_WORKSPACE
|
|
314
|
+
const codexWorkspace = env.CODEX_WORKSPACE
|
|
315
|
+
? resolve(root, env.CODEX_WORKSPACE)
|
|
316
|
+
: resolve(configDirectory, 'workspaces/codex')
|
|
317
|
+
const defaultAcpWorkspace = !env.ACP_WORKSPACE
|
|
318
|
+
const acpWorkspace = env.ACP_WORKSPACE
|
|
319
|
+
? resolve(root, env.ACP_WORKSPACE)
|
|
320
|
+
: resolve(configDirectory, 'workspaces/acp')
|
|
321
|
+
let migratedFiles = []
|
|
322
|
+
if (prepareBackendRuntime) {
|
|
323
|
+
if (defaultOpenCodeWorkspace) {
|
|
324
|
+
ensureManagedWorkspace(
|
|
325
|
+
openCodeWorkspace,
|
|
326
|
+
resolve(root, 'config/opencode/workspace/AGENTS.md'),
|
|
327
|
+
)
|
|
328
|
+
}
|
|
329
|
+
if (defaultOpenClawWorkspace) {
|
|
330
|
+
ensureManagedWorkspace(
|
|
331
|
+
openClawWorkspace,
|
|
332
|
+
resolve(root, 'config/openclaw/workspace/AGENTS.md'),
|
|
333
|
+
)
|
|
334
|
+
}
|
|
335
|
+
if (defaultQoderWorkspace) {
|
|
336
|
+
ensureManagedWorkspace(
|
|
337
|
+
qoderWorkspace,
|
|
338
|
+
resolve(root, 'config/qoder/workspace/AGENTS.md'),
|
|
339
|
+
)
|
|
340
|
+
}
|
|
341
|
+
if (defaultHermesWorkspace) {
|
|
342
|
+
ensureManagedWorkspace(
|
|
343
|
+
hermesWorkspace,
|
|
344
|
+
resolve(root, 'config/hermes/workspace/AGENTS.md'),
|
|
345
|
+
)
|
|
346
|
+
}
|
|
347
|
+
if (defaultCodeBuddyWorkspace) {
|
|
348
|
+
ensureManagedWorkspace(
|
|
349
|
+
codeBuddyWorkspace,
|
|
350
|
+
resolve(root, 'config/codebuddy/workspace/AGENTS.md'),
|
|
351
|
+
)
|
|
352
|
+
ensureCodeBuddyTemplate(
|
|
353
|
+
resolve(root, 'config/codebuddy/workspace/.codebuddy/models.json'),
|
|
354
|
+
resolve(codeBuddyWorkspace, '.codebuddy/models.json'),
|
|
355
|
+
env,
|
|
356
|
+
)
|
|
357
|
+
}
|
|
358
|
+
if (defaultCodexWorkspace) {
|
|
359
|
+
ensureManagedWorkspace(
|
|
360
|
+
codexWorkspace,
|
|
361
|
+
resolve(root, 'config/codex/workspace/AGENTS.md'),
|
|
362
|
+
)
|
|
363
|
+
}
|
|
364
|
+
if (defaultAcpWorkspace) {
|
|
365
|
+
ensureManagedWorkspace(
|
|
366
|
+
acpWorkspace,
|
|
367
|
+
resolve(root, 'config/acp/workspace/AGENTS.md'),
|
|
368
|
+
)
|
|
369
|
+
}
|
|
370
|
+
mkdirSync(openClawStateDirectory, { recursive: true, mode: 0o700 })
|
|
371
|
+
env.OPENCODE_WORKSPACE = openCodeWorkspace
|
|
372
|
+
env.QWEN_AUDIO_AGENT_OPENCLAW_WORKSPACE = openClawWorkspace
|
|
373
|
+
env.OPENCLAW_STATE_DIR = openClawStateDirectory
|
|
374
|
+
env.QODER_WORKSPACE = qoderWorkspace
|
|
375
|
+
env.HERMES_WORKSPACE = hermesWorkspace
|
|
376
|
+
env.CODEBUDDY_WORKSPACE = codeBuddyWorkspace
|
|
377
|
+
env.CODEX_WORKSPACE = codexWorkspace
|
|
378
|
+
env.ACP_WORKSPACE = acpWorkspace
|
|
379
|
+
migratedFiles = [
|
|
380
|
+
[resolve(root, 'runtime/frontend-memory.json'), frontendMemoryPath],
|
|
381
|
+
[resolve(root, 'runtime/tasks.json'), taskStatePath],
|
|
382
|
+
].filter(([legacyPath, targetPath]) => (
|
|
383
|
+
migratePrivateFile(legacyPath, targetPath)
|
|
384
|
+
)).map(([, targetPath]) => targetPath)
|
|
385
|
+
}
|
|
386
|
+
const secret = generateSecret
|
|
387
|
+
? ensureGeneratedSecret(env, configDirectory)
|
|
388
|
+
: { generated: false, statePath: null }
|
|
389
|
+
return {
|
|
390
|
+
configDirectory,
|
|
391
|
+
configPath,
|
|
392
|
+
userProfilePath,
|
|
393
|
+
frontendMemoryPath,
|
|
394
|
+
taskStatePath,
|
|
395
|
+
openCodeWorkspace,
|
|
396
|
+
openClawWorkspace,
|
|
397
|
+
qoderWorkspace,
|
|
398
|
+
hermesWorkspace,
|
|
399
|
+
codeBuddyWorkspace,
|
|
400
|
+
codexWorkspace,
|
|
401
|
+
acpWorkspace,
|
|
402
|
+
openClawStateDirectory,
|
|
403
|
+
migratedFiles,
|
|
404
|
+
loadedFiles,
|
|
405
|
+
generatedSecret: secret.generated,
|
|
406
|
+
statePath: secret.statePath,
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export function hasDashScopeCredential(env = process.env) {
|
|
411
|
+
return Boolean(
|
|
412
|
+
env.QWEN_AUDIO_REALTIME_API_KEY
|
|
413
|
+
|| env.DASHSCOPE_API_KEY,
|
|
414
|
+
)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function requireDashScopeCredential(env = process.env) {
|
|
418
|
+
if (hasDashScopeCredential(env)) return
|
|
419
|
+
throw new Error(
|
|
420
|
+
'缺少 DASHSCOPE_API_KEY。请运行 qwenaudio config 查看配置文件位置。',
|
|
421
|
+
)
|
|
422
|
+
}
|