qwen-audio-agent 2.0.1 → 2.2.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/README.md +21 -10
- package/README_ZH.md +19 -10
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/cli/package.json +1 -1
- package/cli/src/arguments.mjs +4 -1
- package/cli/src/config-command.mjs +36 -6
- package/cli/src/gateway-service.mjs +7 -1
- package/cli/src/launcher.mjs +13 -0
- package/config/frontend-agent/LIVE_PROMPT.md +64 -0
- package/config/frontend-agent/PROMPT.md +29 -26
- package/desktop/src/i18n.mjs +6 -1
- package/desktop/src/settings-config.mjs +32 -40
- package/desktop/src/settings-store.mjs +19 -28
- package/docs/architecture/deep-dive.md +103 -66
- package/docs/architecture/deep-dive.zh.md +73 -50
- package/docs/backends/configuration.md +4 -4
- package/docs/backends/configuration.zh.md +4 -4
- package/docs/backends/overview.md +3 -0
- package/docs/backends/overview.zh.md +3 -0
- package/docs/configuration/backend.md +15 -1
- package/docs/configuration/backend.zh.md +15 -1
- package/docs/configuration/frontend.md +78 -28
- package/docs/configuration/frontend.zh.md +76 -28
- package/docs/configuration.md +1 -1
- package/docs/configuration.zh.md +1 -1
- package/docs/contract.md +24 -2
- package/docs/contract.zh.md +20 -2
- package/docs/extensions.md +14 -0
- package/docs/extensions.zh.md +11 -0
- package/docs/gateway-protocol.md +26 -0
- package/docs/gateway-protocol.zh.md +22 -0
- package/docs/gateway-webrtc-client.md +3 -3
- package/docs/gateway-webrtc-client.zh.md +3 -3
- package/docs/getting-started/install.md +4 -1
- package/docs/getting-started/install.zh.md +4 -1
- package/docs/getting-started/mobile.md +8 -2
- package/docs/getting-started/mobile.zh.md +5 -0
- package/docs/getting-started/quickstart.md +2 -2
- package/docs/getting-started/quickstart.zh.md +2 -2
- package/docs/getting-started/webui.md +2 -0
- package/docs/getting-started/webui.zh.md +2 -0
- package/docs/guides/knowledge.md +8 -5
- package/docs/guides/knowledge.zh.md +4 -3
- package/docs/guides/tasks.md +63 -2
- package/docs/guides/tasks.zh.md +45 -1
- package/docs/guides/web-search.md +32 -1
- package/docs/guides/web-search.zh.md +28 -1
- package/docs/operations/troubleshooting.md +30 -0
- package/docs/operations/troubleshooting.zh.md +26 -0
- package/docs/reference/backend-adapter-sdk.md +33 -2
- package/docs/reference/backend-adapter-sdk.zh.md +29 -2
- package/docs/reference/cli.md +3 -3
- package/docs/reference/cli.zh.md +3 -3
- package/docs/scenarios/lightrag.md +2 -2
- package/docs/scenarios/lightrag.zh.md +2 -2
- package/docs/scenarios/x-omni.md +5 -2
- package/docs/scenarios/x-omni.zh.md +5 -2
- package/docs/voice-agent-architecture-presentation.zh.md +34 -37
- package/docs/voice-frontends/custom-provider.md +22 -4
- package/docs/voice-frontends/custom-provider.zh.md +22 -4
- package/docs/voice-frontends/google-live.md +14 -8
- package/docs/voice-frontends/google-live.zh.md +13 -8
- package/docs/voice-frontends/gpt-live-1.md +149 -0
- package/docs/voice-frontends/gpt-live-1.zh.md +114 -0
- package/docs/voice-frontends/minicpm-o.md +8 -4
- package/docs/voice-frontends/minicpm-o.zh.md +7 -4
- package/docs/voice-frontends/{gpt-live.md → openai-realtime.md} +19 -10
- package/docs/voice-frontends/{gpt-live.zh.md → openai-realtime.zh.md} +18 -10
- package/docs/voice-frontends/qwen-audio-live.md +164 -0
- package/docs/voice-frontends/qwen-audio-live.zh.md +122 -0
- package/docs/voice-frontends/qwen-audio-realtime.md +41 -15
- package/docs/voice-frontends/qwen-audio-realtime.zh.md +34 -12
- package/docs/voice-frontends/qwen-omni-realtime.md +12 -9
- package/docs/voice-frontends/qwen-omni-realtime.zh.md +12 -9
- package/docs/voice-frontends/speech-to-speech.md +8 -4
- package/docs/voice-frontends/speech-to-speech.zh.md +7 -3
- package/docs/voice-frontends/stepfun.md +7 -4
- package/docs/voice-frontends/stepfun.zh.md +7 -4
- package/examples/webrtc/start.mjs +2 -1
- package/package.json +34 -16
- package/scripts/audit-dependencies.mjs +3 -25
- package/scripts/electron-smoke.mjs +5 -5
- package/scripts/patch-build-node-gyp.mjs +63 -0
- package/scripts/prepare-build.mjs +2 -0
- package/scripts/release-check +2 -2
- package/scripts/runtime/claude-code-acp.mjs +5 -5
- package/scripts/verify-package.mjs +3 -0
- package/server/package.json +5 -5
- package/server/src/app/gateway-application.mjs +3 -0
- package/server/src/app/gateway-http-routes.mjs +3 -0
- package/server/src/app/session-conversation-history.mjs +9 -1
- package/server/src/backend/adapters/a2a/backend-adapter.mjs +32 -26
- package/server/src/backend/adapters/acp/backend-adapter.mjs +703 -251
- package/server/src/backend/adapters/acp/coordinator-contract.mjs +2 -13
- package/server/src/backend/adapters/acp/coordinator-instructions.mjs +29 -8
- package/server/src/backend/adapters/acp/coordinator-queue.mjs +58 -0
- package/server/src/backend/adapters/acp/drivers/codex.mjs +1 -1
- package/server/src/backend/adapters/acp/drivers/openclaw.mjs +1 -2
- package/server/src/backend/adapters/acp/input-broker.mjs +2 -0
- package/server/src/backend/adapters/acp/process-client.mjs +111 -16
- package/server/src/backend/adapters/acp/session-configuration.mjs +2 -19
- package/server/src/backend/adapters/acp/session-registry.mjs +9 -0
- package/server/src/backend/adapters/acp/session-tools.mjs +1 -1
- package/server/src/backend/adapters/acp/turn-updates.mjs +188 -0
- package/server/src/backend/adapters/acp/work-continuation.mjs +48 -0
- package/server/src/backend/adapters/agent-client.mjs +24 -1
- package/server/src/backend/adapters/llm/backend-adapter.mjs +393 -0
- package/server/src/backend/adapters/muse/backend-adapter.mjs +2 -0
- package/server/src/backend/availability.mjs +12 -3
- package/server/src/backend/backend-adapter-sdk.mjs +3 -0
- package/server/src/backend/backend-port.mjs +13 -0
- package/server/src/backend/backend-work-runtime.mjs +23 -0
- package/server/src/client/client-command-runtime.mjs +14 -0
- package/server/src/conversation/conversation-sync.mjs +26 -2
- package/server/src/conversation/frontend-agent-context.mjs +18 -5
- package/server/src/conversation/frontend-notes.mjs +7 -5
- package/server/src/conversation/task-result-projector.mjs +1 -1
- package/server/src/core/backend-events.mjs +4 -0
- package/server/src/core/citation.mjs +8 -2
- package/server/src/core/config.mjs +46 -54
- package/server/src/core/gateway-protocol.mjs +4 -1
- package/server/src/core/request-security.mjs +7 -1
- package/server/src/core/work-conversation-context.mjs +138 -0
- package/server/src/core/work-input-request.mjs +1 -0
- package/server/src/frontend/frontend-tools.mjs +20 -7
- package/server/src/frontend/retrieval/providers/bing.mjs +23 -12
- package/server/src/frontend/retrieval/providers/factory.mjs +18 -1
- package/server/src/frontend/retrieval/providers/mcp.mjs +30 -11
- package/server/src/frontend/retrieval/providers/so360.mjs +23 -12
- package/server/src/frontend/tools/agent-task-runtime.mjs +119 -99
- package/server/src/frontend/tools/features/agent-task-tools.mjs +23 -42
- package/server/src/frontend/tools/frontend-tool-context.mjs +2 -2
- package/server/src/frontend/tools/spawn-thinking-tool.mjs +2 -2
- package/server/src/frontend/tools/tool-call-handler.mjs +166 -86
- package/server/src/knowledge/providers/local/library.mjs +26 -6
- package/server/src/knowledge/providers/local/provider.mjs +25 -6
- package/server/src/memory/providers/markdown/context-store.mjs +24 -11
- package/server/src/orchestration/session-task-coordinator.mjs +13 -3
- package/server/src/orchestration/task-operations.mjs +54 -12
- package/server/src/orchestration/task-updates.mjs +82 -0
- package/server/src/task/reminder-scheduler.mjs +5 -1
- package/server/src/task/task-manager.mjs +72 -15
- package/server/src/task/task-recovery.mjs +12 -2
- package/server/src/task/task-scheduler.mjs +5 -5
- package/server/src/task/task-state.mjs +13 -0
- package/server/src/transport/gateway-client-transport.mjs +35 -0
- package/server/src/transport/webrtc/protocol.mjs +1 -1
- package/server/src/voice/announcement/announcement-manager.mjs +3 -0
- package/server/src/voice/audio-stream-diagnostics.mjs +139 -0
- package/server/src/voice/native-delegation-runtime.mjs +360 -0
- package/server/src/voice/providers/dashscope-live.mjs +93 -0
- package/server/src/voice/providers/dashscope.mjs +45 -33
- package/server/src/voice/providers/doubao-seeduplex-protocol.mjs +54 -0
- package/server/src/voice/providers/doubao-seeduplex.mjs +16 -11
- package/server/src/voice/providers/ga-protocol.mjs +7 -2
- package/server/src/voice/providers/google-live-protocol.mjs +124 -34
- package/server/src/voice/providers/google-live.mjs +20 -14
- package/server/src/voice/providers/gpt-live-1-protocol.mjs +597 -0
- package/server/src/voice/providers/gpt-live-1.mjs +3 -0
- package/server/src/voice/providers/gpt-live-client-context.mjs +182 -0
- package/server/src/voice/providers/minicpm-o.mjs +9 -7
- package/server/src/voice/providers/openai-live.mjs +150 -0
- package/server/src/voice/providers/{gpt-live.mjs → openai-realtime.mjs} +23 -19
- package/server/src/voice/providers/provider-registry.mjs +11 -1
- package/server/src/voice/providers/qwen-live-protocol.mjs +399 -0
- package/server/src/voice/providers/registry.mjs +47 -12
- package/server/src/voice/providers/runtime-configuration.mjs +34 -0
- package/server/src/voice/providers/s2s.mjs +9 -7
- package/server/src/voice/providers/stepfun.mjs +14 -11
- package/server/src/voice/realtime-input-runtime.mjs +58 -20
- package/server/src/voice/realtime-presentation-runtime.mjs +111 -27
- package/server/src/voice/realtime-provider-session.mjs +59 -14
- package/server/src/voice/realtime-provider.mjs +358 -45
- package/server/src/voice/realtime-session-runtime.mjs +282 -35
- package/server/src/voice/realtime-task-presentation.mjs +8 -2
- package/server/src/voice/realtime-turn-state.mjs +33 -2
- package/shared/backend/catalog.mjs +26 -2
- package/shared/backend/install.mjs +3 -1
- package/shared/backend/setup.mjs +43 -0
- package/shared/gateway/client-sdk.mjs +21 -6
- package/shared/gateway/setup.mjs +13 -1
- package/shared/gateway/websocket-auth.mjs +2 -1
- package/shared/protocol/gateway-client-protocol.mjs +14 -0
- package/shared/protocol/gateway-events.mjs +39 -1
- package/shared/realtime-legacy-environment.mjs +81 -0
- package/shared/realtime-model-catalog.mjs +68 -194
- package/shared/realtime-models/README.md +33 -0
- package/shared/realtime-models/dashscope.mjs +57 -0
- package/shared/realtime-models/google.mjs +10 -0
- package/shared/realtime-models/openai.mjs +24 -0
- package/shared/realtime-models/profile.mjs +38 -0
- package/shared/realtime-models/stepfun.mjs +10 -0
- package/shared/realtime-models/volcengine.mjs +12 -0
- package/shared/realtime-provider-catalog.mjs +23 -11
- package/shared/realtime-provider-definitions.mjs +168 -235
- package/shared/runtime-environment.mjs +23 -30
- package/shared/skill-library.mjs +49 -13
- package/shared/web-search-configuration.mjs +25 -2
- package/tui/package.json +2 -2
- package/tui/src/index.mjs +14 -9
- package/tui/src/runtime-configuration.mjs +2 -0
- package/tui/src/terminal-commands.mjs +40 -0
- package/tui/src/text-cli.mjs +10 -1
- package/web/dist/assets/index-Bc2Q04J9.css +1 -0
- package/web/dist/assets/index-bY4DFXKT.js +96 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-BRaiI-dW.js +0 -91
- package/web/dist/assets/index-D9k8R2JY.css +0 -1
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { appendPromptBlocks } from './content.mjs'
|
|
2
|
+
import { updateError } from './turn-updates.mjs'
|
|
3
|
+
|
|
4
|
+
// Adapter-private, durable routing. An empty project list means direct work;
|
|
5
|
+
// missing routing (older tokens) must never be mistaken for direct work.
|
|
6
|
+
const VERSION = 1
|
|
7
|
+
|
|
8
|
+
export function readWorkContinuation(context, protocol) {
|
|
9
|
+
if (context == null) return null
|
|
10
|
+
if (context.adapter !== protocol || context.version !== VERSION
|
|
11
|
+
|| typeof context.token !== 'string' || !context.token.trim()
|
|
12
|
+
|| !Array.isArray(context.projects)
|
|
13
|
+
|| context.projects.some(target => !target || typeof target.sessionId !== 'string'
|
|
14
|
+
|| !target.sessionId.trim() || typeof target.directory !== 'string' || !target.directory.trim())) {
|
|
15
|
+
throw updateError('context_unavailable', '原后台续接信息不完整或不兼容,不能自动重做。')
|
|
16
|
+
}
|
|
17
|
+
return context
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function projectTargets(run) {
|
|
21
|
+
const targets = new Map()
|
|
22
|
+
for (const target of [...(run.projectTargets || []), ...(run.delegations || [])]) {
|
|
23
|
+
targets.set(target.sessionId, {
|
|
24
|
+
sessionId: target.sessionId, directory: target.directory, title: target.title,
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
return [...targets.values()]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function workContinuation(protocol, sessionId, run) {
|
|
31
|
+
// Native internal delegates without session_send/turnUpdates are observable,
|
|
32
|
+
// but that alone does not make their execution contexts safely resumable.
|
|
33
|
+
if (run.delegations.some(target => !target.turnUpdates)) return null
|
|
34
|
+
return { adapter: protocol, version: VERSION, token: sessionId, projects: projectTargets(run) }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function projectUpdatePrompt(targets, prompt) {
|
|
38
|
+
return appendPromptBlocks([
|
|
39
|
+
'用户补充了以下要求。将它发送到本请求中对应的已有项目 Session,不要创建新工作:',
|
|
40
|
+
...targets.map(target => `${target.sessionId}: ${target.title || target.sessionId}`),
|
|
41
|
+
].join('\n'), typeof prompt === 'string' ? [{ type: 'text', text: prompt }] : prompt)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function assertProjectUpdateDelivered(run) {
|
|
45
|
+
if (!run.updatedTargets) {
|
|
46
|
+
throw updateError('update_not_delivered', '协调 Agent 未将补充要求送达目标工作。')
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -2,6 +2,7 @@ import { config } from '../../core/config.mjs'
|
|
|
2
2
|
import { assertBackendPort } from '../backend-port.mjs'
|
|
3
3
|
import { AgentError } from '../agent-error.mjs'
|
|
4
4
|
import { createAcpBackendAdapter } from './acp/backend-factory.mjs'
|
|
5
|
+
import { createLlmBackendAdapter } from './llm/backend-adapter.mjs'
|
|
5
6
|
import { createMuseBackendAdapter } from './muse/backend-adapter.mjs'
|
|
6
7
|
|
|
7
8
|
export { AgentError }
|
|
@@ -47,6 +48,10 @@ export class AgentClient {
|
|
|
47
48
|
return this.adapter.cancel(taskId, options)
|
|
48
49
|
}
|
|
49
50
|
|
|
51
|
+
update(taskId, input, options = {}) {
|
|
52
|
+
return this.adapter.update(taskId, input, options)
|
|
53
|
+
}
|
|
54
|
+
|
|
50
55
|
respondAuthorization(taskId, authorizationId, decision, options = {}) {
|
|
51
56
|
return this.adapter.respondAuthorization(
|
|
52
57
|
taskId,
|
|
@@ -88,6 +93,19 @@ export class AgentClient {
|
|
|
88
93
|
|
|
89
94
|
export function createAgentClient(options = {}) {
|
|
90
95
|
const protocol = options.protocol ?? config.agentProtocol
|
|
96
|
+
if (protocol === 'llm') {
|
|
97
|
+
const backend = {
|
|
98
|
+
...(config.backends?.llm || {}),
|
|
99
|
+
...(options.backends?.llm || {}),
|
|
100
|
+
}
|
|
101
|
+
return new AgentClient({
|
|
102
|
+
adapter: createLlmBackendAdapter({
|
|
103
|
+
timeoutMs: options.timeoutMs ?? config.agentTimeoutMs,
|
|
104
|
+
...backend,
|
|
105
|
+
...(options.llmFetch ? { fetchImpl: options.llmFetch } : {}),
|
|
106
|
+
}),
|
|
107
|
+
})
|
|
108
|
+
}
|
|
91
109
|
if (protocol === 'muse') {
|
|
92
110
|
const backend = {
|
|
93
111
|
...(config.backends?.muse || {}),
|
|
@@ -163,6 +181,7 @@ export const agent = {
|
|
|
163
181
|
start: (options = {}) => requireAgent().start(options),
|
|
164
182
|
submit: (work, options = {}) => requireAgent().submit(work, options),
|
|
165
183
|
cancel: (taskId, options = {}) => requireAgent().cancel(taskId, options),
|
|
184
|
+
update: (taskId, input, options = {}) => requireAgent().update(taskId, input, options),
|
|
166
185
|
respondAuthorization: (
|
|
167
186
|
taskId,
|
|
168
187
|
authorizationId,
|
|
@@ -187,5 +206,9 @@ export const agent = {
|
|
|
187
206
|
recoverDelegatedWork: (task, options = {}) =>
|
|
188
207
|
requireAgent().recoverDelegatedWork(task, options),
|
|
189
208
|
uiUrl: (options = {}) => requireAgent().uiUrl(options),
|
|
190
|
-
close: () =>
|
|
209
|
+
close: () => {
|
|
210
|
+
const current = sharedAgent
|
|
211
|
+
sharedAgent = null
|
|
212
|
+
return current ? current.close() : Promise.resolve()
|
|
213
|
+
},
|
|
191
214
|
}
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { defineBackendAdapter } from '../../backend-adapter-sdk.mjs'
|
|
2
|
+
import { backendInstructionFromWork } from '../../backend-work-input.mjs'
|
|
3
|
+
import { AgentError } from '../../agent-error.mjs'
|
|
4
|
+
import { BackendEventType, backendEvent } from '../../../core/backend-events.mjs'
|
|
5
|
+
|
|
6
|
+
const MAX_ERROR_BODY_CHARS = 4_000
|
|
7
|
+
const MAX_RESULT_CHARS = 1_000_000
|
|
8
|
+
|
|
9
|
+
function clean(value) {
|
|
10
|
+
return String(value || '').trim()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function endpointFor(baseUrl) {
|
|
14
|
+
const value = clean(baseUrl).replace(/\/+$/, '')
|
|
15
|
+
if (!value) return ''
|
|
16
|
+
let url
|
|
17
|
+
try {
|
|
18
|
+
url = new URL(value)
|
|
19
|
+
} catch {
|
|
20
|
+
throw new AgentError('LLM backend base URL must be an absolute URL', {
|
|
21
|
+
status: 400,
|
|
22
|
+
protocol: 'llm',
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
if (!['http:', 'https:'].includes(url.protocol)) {
|
|
26
|
+
throw new AgentError('LLM backend base URL must use HTTP or HTTPS', {
|
|
27
|
+
status: 400,
|
|
28
|
+
protocol: 'llm',
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
if (url.username || url.password) {
|
|
32
|
+
throw new AgentError('LLM backend base URL must not contain credentials', {
|
|
33
|
+
status: 400,
|
|
34
|
+
protocol: 'llm',
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
if (url.search || url.hash) {
|
|
38
|
+
throw new AgentError('LLM backend base URL must not contain a query or fragment', {
|
|
39
|
+
status: 400,
|
|
40
|
+
protocol: 'llm',
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
return `${value}/chat/completions`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function cancellationError(taskId, cause) {
|
|
47
|
+
const error = new AgentError(`LLM backend Task ${taskId} was cancelled`, {
|
|
48
|
+
status: 499,
|
|
49
|
+
protocol: 'llm',
|
|
50
|
+
})
|
|
51
|
+
error.code = 'WORK_CANCELLED'
|
|
52
|
+
if (cause !== undefined) error.cause = cause
|
|
53
|
+
return error
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function responseText(payload) {
|
|
57
|
+
const content = payload?.choices?.[0]?.message?.content
|
|
58
|
+
if (typeof content === 'string') return clean(content).slice(0, MAX_RESULT_CHARS)
|
|
59
|
+
if (Array.isArray(content)) {
|
|
60
|
+
return content
|
|
61
|
+
.map(part => typeof part === 'string' ? part : part?.text)
|
|
62
|
+
.map(clean)
|
|
63
|
+
.filter(Boolean)
|
|
64
|
+
.join('\n')
|
|
65
|
+
.slice(0, MAX_RESULT_CHARS)
|
|
66
|
+
}
|
|
67
|
+
return clean(payload?.output_text).slice(0, MAX_RESULT_CHARS)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function errorBody(response) {
|
|
71
|
+
try {
|
|
72
|
+
return clean(await response.text()).slice(0, MAX_ERROR_BODY_CHARS)
|
|
73
|
+
} catch {
|
|
74
|
+
return ''
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class LlmBackendAdapter {
|
|
79
|
+
constructor({
|
|
80
|
+
apiKey = '',
|
|
81
|
+
baseUrl = '',
|
|
82
|
+
model = '',
|
|
83
|
+
systemPrompt = '',
|
|
84
|
+
timeoutMs = 300_000,
|
|
85
|
+
fetchImpl = globalThis.fetch,
|
|
86
|
+
} = {}) {
|
|
87
|
+
this.protocol = 'llm'
|
|
88
|
+
this.label = 'LLM'
|
|
89
|
+
this.apiKey = clean(apiKey)
|
|
90
|
+
this.baseUrl = clean(baseUrl).replace(/\/+$/, '')
|
|
91
|
+
this.model = clean(model)
|
|
92
|
+
this.systemPrompt = clean(systemPrompt)
|
|
93
|
+
this.timeoutMs = Number.isFinite(Number(timeoutMs))
|
|
94
|
+
? Math.max(0, Number(timeoutMs))
|
|
95
|
+
: 300_000
|
|
96
|
+
this.fetchImpl = fetchImpl
|
|
97
|
+
this.ready = false
|
|
98
|
+
this.closed = false
|
|
99
|
+
this.failure = null
|
|
100
|
+
this.endpoint = ''
|
|
101
|
+
this.active = new Map()
|
|
102
|
+
this.listeners = new Set()
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
describe() {
|
|
106
|
+
return {
|
|
107
|
+
configured: Boolean(this.apiKey && this.baseUrl && this.model),
|
|
108
|
+
enabled: true,
|
|
109
|
+
protocol: this.protocol,
|
|
110
|
+
kind: this.protocol,
|
|
111
|
+
label: this.label,
|
|
112
|
+
transport: 'openai-compatible-chat-completions',
|
|
113
|
+
model: this.model || null,
|
|
114
|
+
capabilities: {
|
|
115
|
+
cancel: true,
|
|
116
|
+
authorization: false,
|
|
117
|
+
inputRequests: false,
|
|
118
|
+
taskUpdates: 'activity',
|
|
119
|
+
nativeSessionHistory: false,
|
|
120
|
+
backendUi: false,
|
|
121
|
+
delegation: false,
|
|
122
|
+
sessionMcp: false,
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
runtimeStatus() {
|
|
128
|
+
return {
|
|
129
|
+
ok: this.ready && !this.closed,
|
|
130
|
+
status: this.ready && !this.closed
|
|
131
|
+
? 'ready'
|
|
132
|
+
: this.closed ? 'stopped' : this.failure ? 'failed' : 'not_started',
|
|
133
|
+
protocol: this.protocol,
|
|
134
|
+
...(this.failure ? { error: clean(this.failure.message || this.failure) } : {}),
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async start() {
|
|
139
|
+
if (this.closed) {
|
|
140
|
+
throw new AgentError('LLM backend adapter is closed', {
|
|
141
|
+
status: 503,
|
|
142
|
+
protocol: this.protocol,
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
if (this.ready) return this.runtimeStatus()
|
|
146
|
+
try {
|
|
147
|
+
if (!this.apiKey) {
|
|
148
|
+
throw new AgentError('LLM backend API key is not configured', {
|
|
149
|
+
status: 503,
|
|
150
|
+
protocol: this.protocol,
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
if (!this.model) {
|
|
154
|
+
throw new AgentError('LLM backend model is not configured', {
|
|
155
|
+
status: 503,
|
|
156
|
+
protocol: this.protocol,
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
if (typeof this.fetchImpl !== 'function') {
|
|
160
|
+
throw new AgentError('LLM backend requires a Fetch API implementation', {
|
|
161
|
+
status: 503,
|
|
162
|
+
protocol: this.protocol,
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
this.endpoint = endpointFor(this.baseUrl)
|
|
166
|
+
if (!this.endpoint) {
|
|
167
|
+
throw new AgentError('LLM backend base URL is not configured', {
|
|
168
|
+
status: 503,
|
|
169
|
+
protocol: this.protocol,
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
this.ready = true
|
|
173
|
+
this.failure = null
|
|
174
|
+
return this.runtimeStatus()
|
|
175
|
+
} catch (error) {
|
|
176
|
+
this.ready = false
|
|
177
|
+
this.failure = error
|
|
178
|
+
throw error
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async health() {
|
|
183
|
+
if (!this.ready && !this.closed) {
|
|
184
|
+
try {
|
|
185
|
+
await this.start()
|
|
186
|
+
} catch {
|
|
187
|
+
return this.runtimeStatus()
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return this.runtimeStatus()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
publish(event, task) {
|
|
194
|
+
try {
|
|
195
|
+
task?.onEvent?.(event)
|
|
196
|
+
} catch {
|
|
197
|
+
// A per-Task observer must not interrupt model execution.
|
|
198
|
+
}
|
|
199
|
+
const published = {
|
|
200
|
+
...event,
|
|
201
|
+
taskId: task?.taskId || null,
|
|
202
|
+
ownerId: task?.ownerId || null,
|
|
203
|
+
}
|
|
204
|
+
for (const listener of this.listeners) {
|
|
205
|
+
try {
|
|
206
|
+
listener(published)
|
|
207
|
+
} catch {
|
|
208
|
+
// Subscribers are isolated from the adapter and one another.
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
subscribe(listener) {
|
|
214
|
+
if (typeof listener !== 'function') throw new TypeError('Backend listener must be a function')
|
|
215
|
+
this.listeners.add(listener)
|
|
216
|
+
return () => this.listeners.delete(listener)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async submit(work, { signal, onEvent } = {}) {
|
|
220
|
+
const taskId = clean(work?.id)
|
|
221
|
+
const ownerId = clean(work?.ownerId)
|
|
222
|
+
const instruction = backendInstructionFromWork(work)
|
|
223
|
+
if (!taskId || !ownerId || !instruction) {
|
|
224
|
+
throw new AgentError('Backend submit requires task id, owner and input', {
|
|
225
|
+
status: 400,
|
|
226
|
+
protocol: this.protocol,
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
if (this.active.has(taskId)) {
|
|
230
|
+
throw new AgentError(`Task ${taskId} is already active`, {
|
|
231
|
+
status: 409,
|
|
232
|
+
protocol: this.protocol,
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
if (signal?.aborted) throw cancellationError(taskId, signal.reason)
|
|
236
|
+
const controller = new AbortController()
|
|
237
|
+
const timeoutSignal = this.timeoutMs > 0
|
|
238
|
+
? AbortSignal.timeout(this.timeoutMs)
|
|
239
|
+
: null
|
|
240
|
+
const workSignal = AbortSignal.any(
|
|
241
|
+
[signal, controller.signal, timeoutSignal].filter(Boolean),
|
|
242
|
+
)
|
|
243
|
+
const task = {
|
|
244
|
+
taskId,
|
|
245
|
+
ownerId,
|
|
246
|
+
onEvent,
|
|
247
|
+
controller,
|
|
248
|
+
signal: workSignal,
|
|
249
|
+
activity: {
|
|
250
|
+
id: 'llm-status',
|
|
251
|
+
kind: 'status',
|
|
252
|
+
status: 'running',
|
|
253
|
+
label: 'LLM request started',
|
|
254
|
+
},
|
|
255
|
+
}
|
|
256
|
+
this.active.set(taskId, task)
|
|
257
|
+
try {
|
|
258
|
+
await this.start()
|
|
259
|
+
workSignal.throwIfAborted()
|
|
260
|
+
this.publish(backendEvent(BackendEventType.ACTIVITY, {
|
|
261
|
+
activity: task.activity,
|
|
262
|
+
}), task)
|
|
263
|
+
const response = await this.fetchImpl(this.endpoint, {
|
|
264
|
+
method: 'POST',
|
|
265
|
+
headers: {
|
|
266
|
+
authorization: `Bearer ${this.apiKey}`,
|
|
267
|
+
'content-type': 'application/json',
|
|
268
|
+
},
|
|
269
|
+
body: JSON.stringify({
|
|
270
|
+
model: this.model,
|
|
271
|
+
messages: [
|
|
272
|
+
...(this.systemPrompt ? [{ role: 'system', content: this.systemPrompt }] : []),
|
|
273
|
+
{ role: 'user', content: instruction },
|
|
274
|
+
],
|
|
275
|
+
stream: false,
|
|
276
|
+
}),
|
|
277
|
+
signal: workSignal,
|
|
278
|
+
})
|
|
279
|
+
workSignal.throwIfAborted()
|
|
280
|
+
if (!response?.ok) {
|
|
281
|
+
const body = await errorBody(response)
|
|
282
|
+
throw new AgentError(
|
|
283
|
+
`LLM backend request failed with HTTP ${response?.status || 0}`,
|
|
284
|
+
{
|
|
285
|
+
status: response?.status || 502,
|
|
286
|
+
body,
|
|
287
|
+
protocol: this.protocol,
|
|
288
|
+
},
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
let payload
|
|
292
|
+
try {
|
|
293
|
+
payload = await response.json()
|
|
294
|
+
} catch (cause) {
|
|
295
|
+
throw new AgentError('LLM backend returned invalid JSON', {
|
|
296
|
+
status: 502,
|
|
297
|
+
body: clean(cause?.message),
|
|
298
|
+
protocol: this.protocol,
|
|
299
|
+
})
|
|
300
|
+
}
|
|
301
|
+
const content = responseText(payload)
|
|
302
|
+
if (!content) {
|
|
303
|
+
throw new AgentError('LLM backend returned an empty response', {
|
|
304
|
+
status: 502,
|
|
305
|
+
protocol: this.protocol,
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
task.activity = { ...task.activity, status: 'completed', label: 'LLM request completed' }
|
|
309
|
+
this.publish(backendEvent(BackendEventType.ACTIVITY, {
|
|
310
|
+
activity: task.activity,
|
|
311
|
+
}), task)
|
|
312
|
+
return { content, artifacts: [] }
|
|
313
|
+
} catch (error) {
|
|
314
|
+
if (workSignal.aborted) {
|
|
315
|
+
if (timeoutSignal?.aborted && !controller.signal.aborted && !signal?.aborted) {
|
|
316
|
+
throw new AgentError('LLM backend request timed out', {
|
|
317
|
+
status: 504,
|
|
318
|
+
protocol: this.protocol,
|
|
319
|
+
})
|
|
320
|
+
}
|
|
321
|
+
throw cancellationError(taskId, error)
|
|
322
|
+
}
|
|
323
|
+
if (error instanceof AgentError) throw error
|
|
324
|
+
throw new AgentError(
|
|
325
|
+
`LLM backend request failed: ${clean(error?.message || error) || 'unknown network error'}`,
|
|
326
|
+
{
|
|
327
|
+
status: 502,
|
|
328
|
+
body: clean(error?.message || error).slice(0, MAX_ERROR_BODY_CHARS),
|
|
329
|
+
protocol: this.protocol,
|
|
330
|
+
},
|
|
331
|
+
)
|
|
332
|
+
} finally {
|
|
333
|
+
if (this.active.get(taskId) === task) this.active.delete(taskId)
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
status(taskId, { ownerId } = {}) {
|
|
338
|
+
const id = clean(taskId)
|
|
339
|
+
if (!id) return this.runtimeStatus()
|
|
340
|
+
const task = this.active.get(id)
|
|
341
|
+
if (!task || (ownerId && clean(ownerId) !== task.ownerId)) {
|
|
342
|
+
return { taskId: id, state: 'not_found', activity: [] }
|
|
343
|
+
}
|
|
344
|
+
return { taskId: id, state: 'working', activity: [task.activity] }
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async cancel(taskId, { ownerId } = {}) {
|
|
348
|
+
const id = clean(taskId)
|
|
349
|
+
const task = this.active.get(id)
|
|
350
|
+
if (!task) return { taskId: id, state: 'not_found' }
|
|
351
|
+
if (ownerId && clean(ownerId) !== task.ownerId) {
|
|
352
|
+
throw new AgentError('Cannot cancel LLM work owned by another user', {
|
|
353
|
+
status: 404,
|
|
354
|
+
protocol: this.protocol,
|
|
355
|
+
})
|
|
356
|
+
}
|
|
357
|
+
task.controller.abort(cancellationError(id))
|
|
358
|
+
return { taskId: id, state: 'cancelled' }
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
async respondAuthorization() {
|
|
362
|
+
throw new AgentError('LLM backend does not support authorization requests', {
|
|
363
|
+
status: 501,
|
|
364
|
+
protocol: this.protocol,
|
|
365
|
+
})
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async respondInput() {
|
|
369
|
+
throw new AgentError('LLM backend does not support input requests', {
|
|
370
|
+
status: 501,
|
|
371
|
+
protocol: this.protocol,
|
|
372
|
+
})
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
async close() {
|
|
376
|
+
if (this.closed) return
|
|
377
|
+
this.closed = true
|
|
378
|
+
for (const task of this.active.values()) {
|
|
379
|
+
task.controller.abort(cancellationError(task.taskId))
|
|
380
|
+
}
|
|
381
|
+
this.active.clear()
|
|
382
|
+
this.listeners.clear()
|
|
383
|
+
this.ready = false
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export function createLlmBackendAdapter(options) {
|
|
388
|
+
return defineBackendAdapter(new LlmBackendAdapter(options), {
|
|
389
|
+
name: 'LLM backend adapter',
|
|
390
|
+
})
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export const LLM_BACKEND_ADAPTER_VERSION = '1.0.0'
|
|
@@ -641,6 +641,8 @@ export class MuseBackendAdapter {
|
|
|
641
641
|
protocol: this.protocol,
|
|
642
642
|
})
|
|
643
643
|
}
|
|
644
|
+
// Answer submission is not a permission decision; the backend interprets
|
|
645
|
+
// the supplied text in its question context.
|
|
644
646
|
const action = ['accept', 'decline', 'cancel'].includes(response.action)
|
|
645
647
|
? response.action
|
|
646
648
|
: 'accept'
|
|
@@ -31,15 +31,24 @@ export class BackendAvailability {
|
|
|
31
31
|
snapshot() {
|
|
32
32
|
if (this.now() - this.checkedAt >= this.ttlMs) this.refresh()
|
|
33
33
|
if (!this.last) return { configured: true, ok: true, known: false }
|
|
34
|
-
|
|
34
|
+
const refreshingNegative = this.refreshing
|
|
35
|
+
&& this.last.ok === false
|
|
36
|
+
&& this.last.configured !== false
|
|
37
|
+
return { ...this.last, known: !this.transient && !refreshingNegative }
|
|
35
38
|
}
|
|
36
39
|
|
|
40
|
+
// Starting: re-probe soon. Unreachable: re-probe once per TTL, so a backend that
|
|
41
|
+
// came back is seen before the next receipt asks. Not configured: leave it.
|
|
37
42
|
scheduleRetry() {
|
|
38
|
-
if (this.closed || this.retryTimer
|
|
43
|
+
if (this.closed || this.retryTimer) return
|
|
44
|
+
const delayMs = this.transient
|
|
45
|
+
? this.retryMs
|
|
46
|
+
: this.last?.ok === false && this.last.configured !== false ? this.ttlMs : 0
|
|
47
|
+
if (!delayMs) return
|
|
39
48
|
this.retryTimer = setTimeout(() => {
|
|
40
49
|
this.retryTimer = null
|
|
41
50
|
this.refresh()
|
|
42
|
-
},
|
|
51
|
+
}, delayMs)
|
|
43
52
|
this.retryTimer.unref?.()
|
|
44
53
|
}
|
|
45
54
|
|
|
@@ -46,6 +46,9 @@ export function createBackendAgentHost(adapter, options = {}) {
|
|
|
46
46
|
start: context => backend.start(context),
|
|
47
47
|
submit: (task, context) => backend.submit(task, context),
|
|
48
48
|
cancel: (taskId, context) => backend.cancel(taskId, context),
|
|
49
|
+
...(typeof backend.update === 'function' ? {
|
|
50
|
+
update: (taskId, input, context) => backend.update(taskId, input, context),
|
|
51
|
+
} : {}),
|
|
49
52
|
respondAuthorization: (taskId, authorizationId, decision, context) => (
|
|
50
53
|
backend.respondAuthorization(taskId, authorizationId, decision, context)
|
|
51
54
|
),
|
|
@@ -12,8 +12,21 @@
|
|
|
12
12
|
* submit(task, context?) -> run one Task and return its final outcome
|
|
13
13
|
* task.continuity may be "shared" (default) or "isolated". It is a
|
|
14
14
|
* trusted Gateway hint, never a model-controlled field.
|
|
15
|
+
* An optional outcome.continuation = { adapter, token, ...adapterOwnedFields }
|
|
16
|
+
* is private to the runtime and persisted verbatim; later submit receives it
|
|
17
|
+
* as resumeContext. Extra fields are opaque to the Gateway and validated by
|
|
18
|
+
* the adapter. Explicit null revokes a prior continuation; omission retains it.
|
|
19
|
+
* Tokens identify execution context, not authentication credentials.
|
|
20
|
+
* Optional task.conversation is an immutable, bounded user/assistant text
|
|
21
|
+
* snapshot captured at admission, or a flat array of snapshots combined
|
|
22
|
+
* from queued admissions. Adapters own delivery/cursor semantics and apply
|
|
23
|
+
* one shared context budget across the array; isolated utility work never
|
|
24
|
+
* receives conversation context. update input.conversation uses the same DTO.
|
|
15
25
|
* status(taskId?, context?) -> runtime status without an ID, otherwise Task status
|
|
16
26
|
* cancel(taskId, context?) -> cancel one adapter-owned Task
|
|
27
|
+
* update(taskId, input, context?) -> admit an update to the exact active Task;
|
|
28
|
+
* optional, requires capabilities.workUpdates. Returns { completion } for
|
|
29
|
+
* delivery acknowledgement, not work completion. No implicit task creation.
|
|
17
30
|
* respondAuthorization(taskId, authorizationId, decision, context?)
|
|
18
31
|
* respondInput(taskId, inputRequestId, response, context?)
|
|
19
32
|
* subscribe(listener) -> unsubscribe function for normalized backend events
|
|
@@ -26,6 +26,9 @@ export class BackendWorkRuntime {
|
|
|
26
26
|
instruction: input?.instruction,
|
|
27
27
|
objective: input?.objective,
|
|
28
28
|
inputParts: input?.inputParts || [],
|
|
29
|
+
...(input?.resumeContext ? { resumeContext: input.resumeContext } : {}),
|
|
30
|
+
...(options.continuity !== 'isolated' && input?.conversation
|
|
31
|
+
? { conversation: input.conversation } : {}),
|
|
29
32
|
...(options.continuity === 'isolated' ? { continuity: 'isolated' } : {}),
|
|
30
33
|
}
|
|
31
34
|
work.instruction = backendInstructionFromWork(work)
|
|
@@ -48,6 +51,26 @@ export class BackendWorkRuntime {
|
|
|
48
51
|
return this.backend.cancel(taskId, options)
|
|
49
52
|
}
|
|
50
53
|
|
|
54
|
+
canUpdate() {
|
|
55
|
+
return this.backend.describe()?.capabilities?.workUpdates === true
|
|
56
|
+
&& typeof this.backend.update === 'function'
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
update(taskId, input, options = {}) {
|
|
60
|
+
if (!this.canUpdate()) {
|
|
61
|
+
throw Object.assign(new Error('当前后台不支持调整已有工作。'), { code: 'update_unsupported' })
|
|
62
|
+
}
|
|
63
|
+
const receipt = this.backend.update(taskId, input, options)
|
|
64
|
+
if (typeof receipt?.completion?.then !== 'function') {
|
|
65
|
+
// A misconfigured async adapter may already have dispatched; don't retry.
|
|
66
|
+
receipt?.catch?.(() => {})
|
|
67
|
+
throw Object.assign(new Error('后台未返回有效的调整回执,投递结果无法确认;没有自动重发。'), {
|
|
68
|
+
code: 'update_unconfirmed',
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
return receipt
|
|
72
|
+
}
|
|
73
|
+
|
|
51
74
|
status(taskId, options = {}) {
|
|
52
75
|
return this.backend.status(taskId, options)
|
|
53
76
|
}
|
|
@@ -74,6 +74,20 @@ export class GatewayClientCommandRuntime {
|
|
|
74
74
|
...common,
|
|
75
75
|
task: this.getTask(message.task_id, context),
|
|
76
76
|
}
|
|
77
|
+
case GatewayClientProtocolEvent.TASK_UPDATE: {
|
|
78
|
+
const parts = normalizeInputParts(message.message?.parts)
|
|
79
|
+
const receipt = this.taskOperations.update({
|
|
80
|
+
taskId: message.task_id,
|
|
81
|
+
instruction: objectiveFromMessage({ parts }),
|
|
82
|
+
inputParts: inputPartsFromMessage({ parts }),
|
|
83
|
+
submissionKey: clean(message.event_id),
|
|
84
|
+
}, context)
|
|
85
|
+
return {
|
|
86
|
+
type: GatewayClientProtocolEvent.TASK_UPDATE_RESULT,
|
|
87
|
+
...common, status: receipt.status,
|
|
88
|
+
task: this.getTask(receipt.taskId, context),
|
|
89
|
+
}
|
|
90
|
+
}
|
|
77
91
|
case GatewayClientProtocolEvent.TASK_LIST:
|
|
78
92
|
return {
|
|
79
93
|
type: GatewayClientProtocolEvent.TASK_LIST_RESULT,
|