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,10 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
|
|
4
|
+
const manifestPath = fileURLToPath(
|
|
5
|
+
new URL('../../../package.json', import.meta.url),
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
export const PACKAGE_VERSION = JSON.parse(
|
|
9
|
+
readFileSync(manifestPath, 'utf8'),
|
|
10
|
+
).version
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { config } from './config.mjs'
|
|
2
|
+
|
|
3
|
+
const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '[::1]', '::1'])
|
|
4
|
+
|
|
5
|
+
function normalizedOrigin(value) {
|
|
6
|
+
try {
|
|
7
|
+
return new URL(value).origin
|
|
8
|
+
} catch {
|
|
9
|
+
return ''
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function parsedHost(value) {
|
|
14
|
+
try {
|
|
15
|
+
const url = new URL(`http://${value}`)
|
|
16
|
+
return {
|
|
17
|
+
host: url.host,
|
|
18
|
+
hostname: url.hostname,
|
|
19
|
+
}
|
|
20
|
+
} catch {
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function trustedOrigins(allowedOrigins) {
|
|
26
|
+
return allowedOrigins
|
|
27
|
+
.map(normalizedOrigin)
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
.filter(value => {
|
|
30
|
+
const url = new URL(value)
|
|
31
|
+
return (
|
|
32
|
+
url.protocol === 'https:'
|
|
33
|
+
|| (
|
|
34
|
+
url.protocol === 'http:'
|
|
35
|
+
&& LOOPBACK_HOSTS.has(url.hostname)
|
|
36
|
+
)
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function isAllowedOrigin(
|
|
42
|
+
req,
|
|
43
|
+
{ allowedOrigins = config.allowedOrigins } = {},
|
|
44
|
+
) {
|
|
45
|
+
const requestHost = parsedHost(req.headers.host)
|
|
46
|
+
if (!requestHost) return false
|
|
47
|
+
const origin = normalizedOrigin(req.headers.origin)
|
|
48
|
+
const configured = trustedOrigins(allowedOrigins)
|
|
49
|
+
const trustedHost = configured.some(value => (
|
|
50
|
+
new URL(value).host === requestHost.host
|
|
51
|
+
))
|
|
52
|
+
|
|
53
|
+
// CLI and other non-browser clients do not send Origin. They are accepted
|
|
54
|
+
// only through a loopback address or an explicitly trusted reverse proxy.
|
|
55
|
+
if (!origin) {
|
|
56
|
+
return LOOPBACK_HOSTS.has(requestHost.hostname) || trustedHost
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const originUrl = new URL(origin)
|
|
60
|
+
if (configured.includes(origin)) {
|
|
61
|
+
return originUrl.host === requestHost.host
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Comparing arbitrary Origin and Host values is vulnerable to DNS rebinding.
|
|
65
|
+
// The implicit same-origin path is therefore limited to literal loopback
|
|
66
|
+
// hosts. Public hostnames must be explicitly allowlisted.
|
|
67
|
+
return (
|
|
68
|
+
LOOPBACK_HOSTS.has(requestHost.hostname)
|
|
69
|
+
&& LOOPBACK_HOSTS.has(originUrl.hostname)
|
|
70
|
+
&& originUrl.host === requestHost.host
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function enforceSameOrigin(req, res, next) {
|
|
75
|
+
if (!isAllowedOrigin(req)) {
|
|
76
|
+
res.status(403).json({ error: 'origin not allowed' })
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
next()
|
|
80
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { dirname, resolve } from 'node:path'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
import { loadRuntimeEnvironment } from '../../shared/runtime-environment.mjs'
|
|
4
|
+
import { startManagedBackend } from './process/managed-backend.mjs'
|
|
5
|
+
|
|
6
|
+
const root = resolve(dirname(fileURLToPath(import.meta.url)), '../..')
|
|
7
|
+
loadRuntimeEnvironment({ root })
|
|
8
|
+
|
|
9
|
+
let backendRuntime
|
|
10
|
+
let agentClient
|
|
11
|
+
let stopPromise
|
|
12
|
+
let exitTimer
|
|
13
|
+
|
|
14
|
+
function stop(signal = 'SIGTERM') {
|
|
15
|
+
if (stopPromise) return stopPromise
|
|
16
|
+
backendRuntime?.close(signal)
|
|
17
|
+
stopPromise = Promise.resolve(agentClient?.close()).catch(error => {
|
|
18
|
+
process.stderr.write(`后台 Agent 停止失败:${error.message}\n`)
|
|
19
|
+
})
|
|
20
|
+
return stopPromise
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function stopAndExit(signal) {
|
|
24
|
+
if (!exitTimer) {
|
|
25
|
+
exitTimer = setTimeout(() => process.exit(0), 2000)
|
|
26
|
+
}
|
|
27
|
+
stop(signal).finally(() => process.exit(0))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
backendRuntime = await startManagedBackend({ root })
|
|
32
|
+
const managedBackend = backendRuntime.child
|
|
33
|
+
const onManagedBackendExit = (code, signal) => {
|
|
34
|
+
if (stopPromise) return
|
|
35
|
+
const reason = signal || code || 'unknown'
|
|
36
|
+
process.stderr.write(`后台 Agent 意外退出:${reason}\n`)
|
|
37
|
+
stopPromise = Promise.resolve(agentClient?.close()).catch(error => {
|
|
38
|
+
process.stderr.write(`后台 Agent 停止失败:${error.message}\n`)
|
|
39
|
+
})
|
|
40
|
+
stopPromise.finally(() => process.exit(1))
|
|
41
|
+
}
|
|
42
|
+
if (managedBackend?.exitCode != null || managedBackend?.signalCode != null) {
|
|
43
|
+
onManagedBackendExit(
|
|
44
|
+
managedBackend.exitCode,
|
|
45
|
+
managedBackend.signalCode,
|
|
46
|
+
)
|
|
47
|
+
} else {
|
|
48
|
+
managedBackend?.once('exit', onManagedBackendExit)
|
|
49
|
+
}
|
|
50
|
+
const agentModule = await import('./agent/agent-client.mjs')
|
|
51
|
+
agentClient = agentModule.agent
|
|
52
|
+
process.once('SIGINT', () => {
|
|
53
|
+
stopAndExit('SIGINT')
|
|
54
|
+
})
|
|
55
|
+
process.once('SIGTERM', () => {
|
|
56
|
+
stopAndExit('SIGTERM')
|
|
57
|
+
})
|
|
58
|
+
process.once('exit', () => {
|
|
59
|
+
backendRuntime?.close()
|
|
60
|
+
agentClient?.close()
|
|
61
|
+
})
|
|
62
|
+
await import('./app/bootstrap.mjs')
|
|
63
|
+
} catch (error) {
|
|
64
|
+
stop()
|
|
65
|
+
process.stderr.write(`Gateway 启动失败:${error.message}\n`)
|
|
66
|
+
process.exitCode = 1
|
|
67
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { managedOnlyBackend } from './shared.mjs'
|
|
2
|
+
|
|
3
|
+
export const codeBuddyRuntimeDriver = {
|
|
4
|
+
id: 'codebuddy',
|
|
5
|
+
separateManagedProcess: false,
|
|
6
|
+
|
|
7
|
+
resolve({ mode, permissionMode }) {
|
|
8
|
+
return managedOnlyBackend({
|
|
9
|
+
id: this.id,
|
|
10
|
+
mode,
|
|
11
|
+
permissionMode,
|
|
12
|
+
label: 'CodeBuddy',
|
|
13
|
+
})
|
|
14
|
+
},
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { managedOnlyBackend } from './shared.mjs'
|
|
2
|
+
|
|
3
|
+
export const codexRuntimeDriver = {
|
|
4
|
+
id: 'codex',
|
|
5
|
+
separateManagedProcess: false,
|
|
6
|
+
|
|
7
|
+
resolve({ mode, permissionMode }) {
|
|
8
|
+
return managedOnlyBackend({
|
|
9
|
+
id: this.id,
|
|
10
|
+
mode,
|
|
11
|
+
permissionMode,
|
|
12
|
+
label: 'Codex',
|
|
13
|
+
})
|
|
14
|
+
},
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { managedOnlyBackend } from './shared.mjs'
|
|
2
|
+
|
|
3
|
+
export const genericAcpRuntimeDriver = {
|
|
4
|
+
id: 'acp',
|
|
5
|
+
separateManagedProcess: false,
|
|
6
|
+
|
|
7
|
+
resolve({ mode, permissionMode }) {
|
|
8
|
+
if (permissionMode === 'full') {
|
|
9
|
+
throw new Error('通用 ACP 后端无法安全地统一开启最高权限模式')
|
|
10
|
+
}
|
|
11
|
+
return managedOnlyBackend({
|
|
12
|
+
id: this.id,
|
|
13
|
+
mode,
|
|
14
|
+
permissionMode,
|
|
15
|
+
label: 'ACP Agent',
|
|
16
|
+
})
|
|
17
|
+
},
|
|
18
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { managedOnlyBackend } from './shared.mjs'
|
|
2
|
+
|
|
3
|
+
export const hermesRuntimeDriver = {
|
|
4
|
+
id: 'hermes',
|
|
5
|
+
separateManagedProcess: false,
|
|
6
|
+
|
|
7
|
+
resolve({ mode, permissionMode }) {
|
|
8
|
+
return managedOnlyBackend({
|
|
9
|
+
id: this.id,
|
|
10
|
+
mode,
|
|
11
|
+
permissionMode,
|
|
12
|
+
label: 'Hermes',
|
|
13
|
+
})
|
|
14
|
+
},
|
|
15
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyLocalAddress,
|
|
3
|
+
localManagedBackend,
|
|
4
|
+
} from './shared.mjs'
|
|
5
|
+
|
|
6
|
+
export const openClawRuntimeDriver = {
|
|
7
|
+
id: 'openclaw',
|
|
8
|
+
separateManagedProcess: true,
|
|
9
|
+
managedScript: 'openclaw-gateway',
|
|
10
|
+
managedNpmScript: 'openclaw',
|
|
11
|
+
|
|
12
|
+
resolve({ env, mode, permissionMode }) {
|
|
13
|
+
if (permissionMode === 'full') {
|
|
14
|
+
throw new Error(
|
|
15
|
+
'OpenClaw 的最高权限需要单独配置 exec approvals、elevated 和 host,'
|
|
16
|
+
+ '不能由 Gateway 的统一权限开关安全启用',
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
return localManagedBackend({
|
|
20
|
+
id: this.id,
|
|
21
|
+
env,
|
|
22
|
+
mode,
|
|
23
|
+
permissionMode,
|
|
24
|
+
baseUrlEnvironment: 'OPENCLAW_BASE_URL',
|
|
25
|
+
defaultBaseUrl: 'http://127.0.0.1:18789',
|
|
26
|
+
})
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
applyAddress(env, backend) {
|
|
30
|
+
applyLocalAddress(env, backend, {
|
|
31
|
+
baseUrlEnvironment: 'OPENCLAW_BASE_URL',
|
|
32
|
+
portEnvironment: 'OPENCLAW_PORT',
|
|
33
|
+
})
|
|
34
|
+
},
|
|
35
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyLocalAddress,
|
|
3
|
+
localManagedBackend,
|
|
4
|
+
} from './shared.mjs'
|
|
5
|
+
|
|
6
|
+
function inlineConfig(value) {
|
|
7
|
+
if (!String(value || '').trim()) return {}
|
|
8
|
+
let parsed
|
|
9
|
+
try {
|
|
10
|
+
parsed = JSON.parse(value)
|
|
11
|
+
} catch {
|
|
12
|
+
throw new Error('OPENCODE_CONFIG_CONTENT 不是有效的 JSON')
|
|
13
|
+
}
|
|
14
|
+
if (!parsed || Array.isArray(parsed) || typeof parsed !== 'object') {
|
|
15
|
+
throw new Error('OPENCODE_CONFIG_CONTENT 必须是 JSON 对象')
|
|
16
|
+
}
|
|
17
|
+
return parsed
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const openCodeRuntimeDriver = {
|
|
21
|
+
id: 'opencode',
|
|
22
|
+
separateManagedProcess: true,
|
|
23
|
+
managedScript: 'opencode-server',
|
|
24
|
+
managedNpmScript: 'opencode',
|
|
25
|
+
|
|
26
|
+
resolve({ env, mode, permissionMode }) {
|
|
27
|
+
return localManagedBackend({
|
|
28
|
+
id: this.id,
|
|
29
|
+
env,
|
|
30
|
+
mode,
|
|
31
|
+
permissionMode,
|
|
32
|
+
baseUrlEnvironment: 'OPENCODE_BASE_URL',
|
|
33
|
+
defaultBaseUrl: 'http://127.0.0.1:4096',
|
|
34
|
+
})
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
applyPermissionMode(env, backend) {
|
|
38
|
+
if (backend.permissionMode !== 'full') return env
|
|
39
|
+
const config = inlineConfig(env.OPENCODE_CONFIG_CONTENT)
|
|
40
|
+
const agents = {
|
|
41
|
+
...(config.agent && typeof config.agent === 'object'
|
|
42
|
+
? config.agent
|
|
43
|
+
: {}),
|
|
44
|
+
}
|
|
45
|
+
const names = new Set([
|
|
46
|
+
String(
|
|
47
|
+
env.QWEN_AUDIO_AGENT_BACKEND_AGENT
|
|
48
|
+
|| env.OPENCODE_COORDINATOR_AGENT
|
|
49
|
+
|| 'qwen-audio-agent-backend',
|
|
50
|
+
).trim(),
|
|
51
|
+
String(env.OPENCODE_TASK_AGENT || 'build').trim(),
|
|
52
|
+
])
|
|
53
|
+
for (const name of names) {
|
|
54
|
+
if (!name) continue
|
|
55
|
+
const existing = agents[name]
|
|
56
|
+
agents[name] = {
|
|
57
|
+
...(existing && typeof existing === 'object' ? existing : {}),
|
|
58
|
+
permission: 'allow',
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
env.OPENCODE_CONFIG_CONTENT = JSON.stringify({
|
|
62
|
+
...config,
|
|
63
|
+
permission: 'allow',
|
|
64
|
+
agent: agents,
|
|
65
|
+
})
|
|
66
|
+
return env
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
applyAddress(env, backend) {
|
|
70
|
+
applyLocalAddress(env, backend, {
|
|
71
|
+
baseUrlEnvironment: 'OPENCODE_BASE_URL',
|
|
72
|
+
portEnvironment: 'OPENCODE_PORT',
|
|
73
|
+
})
|
|
74
|
+
},
|
|
75
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { managedOnlyBackend } from './shared.mjs'
|
|
2
|
+
|
|
3
|
+
export const qoderRuntimeDriver = {
|
|
4
|
+
id: 'qoder',
|
|
5
|
+
separateManagedProcess: false,
|
|
6
|
+
|
|
7
|
+
resolve({ mode, permissionMode }) {
|
|
8
|
+
return managedOnlyBackend({
|
|
9
|
+
id: this.id,
|
|
10
|
+
mode,
|
|
11
|
+
permissionMode,
|
|
12
|
+
label: 'Qoder',
|
|
13
|
+
})
|
|
14
|
+
},
|
|
15
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { codeBuddyRuntimeDriver } from './codebuddy.mjs'
|
|
2
|
+
import { codexRuntimeDriver } from './codex.mjs'
|
|
3
|
+
import { genericAcpRuntimeDriver } from './generic-acp.mjs'
|
|
4
|
+
import { hermesRuntimeDriver } from './hermes.mjs'
|
|
5
|
+
import { openClawRuntimeDriver } from './openclaw.mjs'
|
|
6
|
+
import { openCodeRuntimeDriver } from './opencode.mjs'
|
|
7
|
+
import { qoderRuntimeDriver } from './qoder.mjs'
|
|
8
|
+
|
|
9
|
+
const drivers = new Map([
|
|
10
|
+
openCodeRuntimeDriver,
|
|
11
|
+
openClawRuntimeDriver,
|
|
12
|
+
qoderRuntimeDriver,
|
|
13
|
+
hermesRuntimeDriver,
|
|
14
|
+
codeBuddyRuntimeDriver,
|
|
15
|
+
codexRuntimeDriver,
|
|
16
|
+
genericAcpRuntimeDriver,
|
|
17
|
+
].map(driver => [driver.id, driver]))
|
|
18
|
+
|
|
19
|
+
export function backendRuntimeDriver(protocol) {
|
|
20
|
+
const id = String(protocol || '').trim().toLowerCase()
|
|
21
|
+
const driver = drivers.get(id)
|
|
22
|
+
if (!driver) throw new Error(`不支持的后台 Agent:${id}`)
|
|
23
|
+
return driver
|
|
24
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export function origin(value) {
|
|
2
|
+
return new URL(value).origin
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function localManagedBackend({
|
|
6
|
+
id,
|
|
7
|
+
env,
|
|
8
|
+
mode,
|
|
9
|
+
permissionMode,
|
|
10
|
+
baseUrlEnvironment,
|
|
11
|
+
defaultBaseUrl,
|
|
12
|
+
}) {
|
|
13
|
+
return {
|
|
14
|
+
protocol: id,
|
|
15
|
+
mode,
|
|
16
|
+
permissionMode,
|
|
17
|
+
baseUrl: origin(env[baseUrlEnvironment] || defaultBaseUrl),
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function managedOnlyBackend({
|
|
22
|
+
id,
|
|
23
|
+
mode,
|
|
24
|
+
permissionMode,
|
|
25
|
+
label,
|
|
26
|
+
}) {
|
|
27
|
+
if (mode !== 'managed') {
|
|
28
|
+
throw new Error(`${label} 后台当前只支持 managed 模式`)
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
protocol: id,
|
|
32
|
+
mode,
|
|
33
|
+
permissionMode,
|
|
34
|
+
baseUrl: null,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function applyLocalAddress(env, backend, {
|
|
39
|
+
baseUrlEnvironment,
|
|
40
|
+
portEnvironment,
|
|
41
|
+
}) {
|
|
42
|
+
const target = new URL(backend.baseUrl)
|
|
43
|
+
env[baseUrlEnvironment] = backend.baseUrl
|
|
44
|
+
env[portEnvironment] = target.port
|
|
45
|
+
|| (target.protocol === 'https:' ? '443' : '80')
|
|
46
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { createConnection, createServer } from 'node:net'
|
|
3
|
+
import { resolve } from 'node:path'
|
|
4
|
+
import { backendRuntimeDriver } from './backend-drivers/registry.mjs'
|
|
5
|
+
|
|
6
|
+
const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '::1', '[::1]'])
|
|
7
|
+
|
|
8
|
+
function permissionMode(env) {
|
|
9
|
+
const mode = String(
|
|
10
|
+
env.QWEN_AUDIO_AGENT_BACKEND_PERMISSION_MODE || 'native',
|
|
11
|
+
).toLowerCase()
|
|
12
|
+
if (!['native', 'full'].includes(mode)) {
|
|
13
|
+
throw new Error(`不支持的后台权限模式:${mode}(可选 native、full)`)
|
|
14
|
+
}
|
|
15
|
+
return mode
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function resolveManagedBackend(env = process.env) {
|
|
19
|
+
const protocol = String(env.AGENT_PROTOCOL || '').toLowerCase()
|
|
20
|
+
if (!protocol) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
'必须指定后台 Agent:请在 config.env 中设置 AGENT_PROTOCOL',
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
const driver = backendRuntimeDriver(protocol)
|
|
26
|
+
const mode = String(
|
|
27
|
+
env.QWEN_AUDIO_AGENT_BACKEND_MODE || 'managed',
|
|
28
|
+
).toLowerCase()
|
|
29
|
+
const resolvedPermissionMode = permissionMode(env)
|
|
30
|
+
if (!['managed', 'compatible'].includes(mode)) {
|
|
31
|
+
throw new Error(`不支持的后台模式:${mode}`)
|
|
32
|
+
}
|
|
33
|
+
if (resolvedPermissionMode === 'full' && mode !== 'managed') {
|
|
34
|
+
throw new Error('最高权限模式只支持由 Gateway 管理的后台 Agent')
|
|
35
|
+
}
|
|
36
|
+
return driver.resolve({
|
|
37
|
+
env,
|
|
38
|
+
mode,
|
|
39
|
+
permissionMode: resolvedPermissionMode,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function applyBackendPermissionMode(env, backend) {
|
|
44
|
+
if (backend.permissionMode !== 'full') return env
|
|
45
|
+
if (backend.mode !== 'managed') {
|
|
46
|
+
throw new Error('最高权限模式只支持由 Gateway 管理的后台 Agent')
|
|
47
|
+
}
|
|
48
|
+
return backendRuntimeDriver(backend.protocol)
|
|
49
|
+
.applyPermissionMode?.(env, backend)
|
|
50
|
+
|| env
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function isLocalBackend(baseUrl) {
|
|
54
|
+
return LOOPBACK_HOSTS.has(new URL(baseUrl).hostname)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function backendAddressInUse(baseUrl, timeoutMs = 300) {
|
|
58
|
+
const target = new URL(baseUrl)
|
|
59
|
+
const port = Number(target.port || (target.protocol === 'https:' ? 443 : 80))
|
|
60
|
+
return new Promise(resolvePromise => {
|
|
61
|
+
const socket = createConnection({ host: target.hostname, port })
|
|
62
|
+
const finish = value => {
|
|
63
|
+
socket.destroy()
|
|
64
|
+
resolvePromise(value)
|
|
65
|
+
}
|
|
66
|
+
socket.setTimeout(timeoutMs)
|
|
67
|
+
socket.once('connect', () => finish(true))
|
|
68
|
+
socket.once('timeout', () => finish(false))
|
|
69
|
+
socket.once('error', () => finish(false))
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function allocateBackendAddress(baseUrl) {
|
|
74
|
+
const target = new URL(baseUrl)
|
|
75
|
+
const host = target.hostname === 'localhost' ? '127.0.0.1' : target.hostname
|
|
76
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
77
|
+
const server = createServer()
|
|
78
|
+
server.unref()
|
|
79
|
+
server.once('error', rejectPromise)
|
|
80
|
+
server.listen(0, host, () => {
|
|
81
|
+
const address = server.address()
|
|
82
|
+
server.close(error => {
|
|
83
|
+
if (error) return rejectPromise(error)
|
|
84
|
+
target.hostname = host
|
|
85
|
+
target.port = String(address.port)
|
|
86
|
+
resolvePromise(target.origin)
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function applyBackendAddress(env, backend) {
|
|
93
|
+
backendRuntimeDriver(backend.protocol).applyAddress?.(env, backend)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function spawnSpec(root, platform, env, driver) {
|
|
97
|
+
const childEnvironment = {
|
|
98
|
+
...env,
|
|
99
|
+
QWEN_AUDIO_AGENT_ENV_LOADED: '1',
|
|
100
|
+
QWEN_AUDIO_AGENT_NODE: process.execPath,
|
|
101
|
+
}
|
|
102
|
+
if (platform === 'win32') {
|
|
103
|
+
return {
|
|
104
|
+
command: 'npm.cmd',
|
|
105
|
+
args: ['run', driver.managedNpmScript],
|
|
106
|
+
options: {
|
|
107
|
+
cwd: root,
|
|
108
|
+
env: childEnvironment,
|
|
109
|
+
detached: false,
|
|
110
|
+
stdio: 'inherit',
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
command: resolve(root, `scripts/${driver.managedScript}`),
|
|
116
|
+
args: [],
|
|
117
|
+
options: {
|
|
118
|
+
cwd: root,
|
|
119
|
+
env: childEnvironment,
|
|
120
|
+
detached: true,
|
|
121
|
+
stdio: 'inherit',
|
|
122
|
+
},
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export class ManagedBackendRuntime {
|
|
127
|
+
constructor(child = null, {
|
|
128
|
+
platform = process.platform,
|
|
129
|
+
killImpl = process.kill,
|
|
130
|
+
} = {}) {
|
|
131
|
+
this.child = child
|
|
132
|
+
this.platform = platform
|
|
133
|
+
this.killImpl = killImpl
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
get ownsProcess() {
|
|
137
|
+
return Boolean(this.child)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
close(signal = 'SIGTERM') {
|
|
141
|
+
const child = this.child
|
|
142
|
+
if (!child || child.exitCode != null || child.signalCode != null) return
|
|
143
|
+
if (this.platform !== 'win32' && Number.isInteger(child.pid)) {
|
|
144
|
+
try {
|
|
145
|
+
this.killImpl(-child.pid, signal)
|
|
146
|
+
return
|
|
147
|
+
} catch {
|
|
148
|
+
// The group may have exited between the status check and signal.
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
child.kill(signal)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export async function startManagedBackend({
|
|
156
|
+
root,
|
|
157
|
+
env = process.env,
|
|
158
|
+
platform = process.platform,
|
|
159
|
+
spawnImpl = spawn,
|
|
160
|
+
isAddressInUse = backendAddressInUse,
|
|
161
|
+
findFreeAddress = allocateBackendAddress,
|
|
162
|
+
} = {}) {
|
|
163
|
+
const backend = resolveManagedBackend(env)
|
|
164
|
+
const driver = backendRuntimeDriver(backend.protocol)
|
|
165
|
+
applyBackendPermissionMode(env, backend)
|
|
166
|
+
if (!driver.separateManagedProcess) {
|
|
167
|
+
return new ManagedBackendRuntime(null, { platform })
|
|
168
|
+
}
|
|
169
|
+
if (backend.mode === 'compatible') {
|
|
170
|
+
return new ManagedBackendRuntime(null, { platform })
|
|
171
|
+
}
|
|
172
|
+
if (backend.mode !== 'managed') {
|
|
173
|
+
throw new Error(`不支持的后台模式:${backend.mode}`)
|
|
174
|
+
}
|
|
175
|
+
if (!isLocalBackend(backend.baseUrl)) {
|
|
176
|
+
throw new Error(`增强模式只能管理本机后台 Agent:${backend.baseUrl}`)
|
|
177
|
+
}
|
|
178
|
+
if (await isAddressInUse(backend.baseUrl)) {
|
|
179
|
+
backend.baseUrl = await findFreeAddress(backend.baseUrl)
|
|
180
|
+
}
|
|
181
|
+
applyBackendAddress(env, backend)
|
|
182
|
+
const spec = spawnSpec(root, platform, env, driver)
|
|
183
|
+
const child = spawnImpl(spec.command, spec.args, spec.options)
|
|
184
|
+
child.once?.('error', error => {
|
|
185
|
+
process.stderr.write(`后台 Agent 进程启动失败:${error.message}\n`)
|
|
186
|
+
})
|
|
187
|
+
return new ManagedBackendRuntime(child, { platform })
|
|
188
|
+
}
|