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,823 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CANCEL_AGENT_TASK_TOOL_NAME,
|
|
3
|
+
DELEGATE_TOOL_NAME,
|
|
4
|
+
GET_AGENT_TASK_STATUS_TOOL_NAME,
|
|
5
|
+
GET_CURRENT_TIME_TOOL_NAME,
|
|
6
|
+
USER_MEMORY_TOOL_NAME,
|
|
7
|
+
RESPOND_AGENT_PERMISSION_TOOL_NAME,
|
|
8
|
+
} from '../realtime-provider.mjs'
|
|
9
|
+
import { currentTimeSnapshot } from '../../conversation/frontend-agent-context.mjs'
|
|
10
|
+
|
|
11
|
+
const SENSITIVE_MEMORY = /(?:pass(?:word)?|secret|api[_ -]?key|access[_ -]?token|credential|验证码|密码|密钥|令牌|\bsk-[a-z0-9_-]+)/i
|
|
12
|
+
|
|
13
|
+
function normalizedEvidence(value) {
|
|
14
|
+
return String(value || '').replace(/\s+/g, ' ').trim().toLocaleLowerCase()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function failure(errorCode, userMessage, {
|
|
18
|
+
retryable = false,
|
|
19
|
+
status = 'failed',
|
|
20
|
+
} = {}) {
|
|
21
|
+
return {
|
|
22
|
+
status,
|
|
23
|
+
error: true,
|
|
24
|
+
error_code: errorCode,
|
|
25
|
+
user_message: userMessage,
|
|
26
|
+
retryable,
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class ToolCallHandler {
|
|
31
|
+
constructor({
|
|
32
|
+
taskManager,
|
|
33
|
+
ownerId,
|
|
34
|
+
sessionId,
|
|
35
|
+
transcripts,
|
|
36
|
+
getFrontend,
|
|
37
|
+
getTurnId,
|
|
38
|
+
getTurnGeneration,
|
|
39
|
+
coordinator,
|
|
40
|
+
coordinatorAvailable = async () => true,
|
|
41
|
+
memoryStore,
|
|
42
|
+
getClientContext = () => ({}),
|
|
43
|
+
getConversationContext = () => [],
|
|
44
|
+
onMemoryChanged = () => {},
|
|
45
|
+
respondPermission,
|
|
46
|
+
permissionPolicy,
|
|
47
|
+
}) {
|
|
48
|
+
this.taskManager = taskManager
|
|
49
|
+
this.ownerId = ownerId
|
|
50
|
+
this.sessionId = sessionId
|
|
51
|
+
this.transcripts = transcripts
|
|
52
|
+
this.getFrontend = getFrontend
|
|
53
|
+
this.getTurnId = getTurnId
|
|
54
|
+
this.getTurnGeneration = getTurnGeneration
|
|
55
|
+
this.coordinator = coordinator
|
|
56
|
+
this.coordinatorAvailable = coordinatorAvailable
|
|
57
|
+
this.memoryStore = memoryStore
|
|
58
|
+
this.getClientContext = getClientContext
|
|
59
|
+
this.getConversationContext = getConversationContext
|
|
60
|
+
this.onMemoryChanged = onMemoryChanged
|
|
61
|
+
this.respondPermission = respondPermission
|
|
62
|
+
this.permissionPolicy = permissionPolicy
|
|
63
|
+
this.gatewayApprovedPermissions = new Set()
|
|
64
|
+
this.processedCalls = new Set()
|
|
65
|
+
this.turnTasks = new Map()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
isStale(turnId, generation) {
|
|
69
|
+
return (
|
|
70
|
+
generation !== this.getTurnGeneration()
|
|
71
|
+
|| Boolean(turnId && this.getTurnId() && turnId !== this.getTurnId())
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async sendOutput(callId, output, turnId, taskId, options) {
|
|
76
|
+
const {
|
|
77
|
+
responseContext,
|
|
78
|
+
...frontendOptions
|
|
79
|
+
} = options || {}
|
|
80
|
+
await this.getFrontend()?.sendFunctionOutput(
|
|
81
|
+
callId,
|
|
82
|
+
output,
|
|
83
|
+
{ turnId, taskId, ...(responseContext || {}) },
|
|
84
|
+
frontendOptions,
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async closeStaleCall(callId, turnId) {
|
|
89
|
+
await this.sendOutput(
|
|
90
|
+
callId,
|
|
91
|
+
{
|
|
92
|
+
status: 'superseded',
|
|
93
|
+
message: '用户已经开始了新一轮,这次尚未提交。',
|
|
94
|
+
},
|
|
95
|
+
turnId,
|
|
96
|
+
null,
|
|
97
|
+
{ createResponse: false },
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
forwardCoordinatorEvent(event, onEvent) {
|
|
102
|
+
const permission = event?.permission
|
|
103
|
+
if (
|
|
104
|
+
event?.type === 'backend.permission.resolved'
|
|
105
|
+
&& permission?.id
|
|
106
|
+
&& this.gatewayApprovedPermissions.delete(permission.id)
|
|
107
|
+
) return
|
|
108
|
+
if (
|
|
109
|
+
event?.type !== 'backend.permission.requested'
|
|
110
|
+
|| !permission?.id
|
|
111
|
+
|| !this.respondPermission
|
|
112
|
+
|| !this.permissionPolicy?.shouldAutoAllow(
|
|
113
|
+
this.ownerId,
|
|
114
|
+
this.sessionId,
|
|
115
|
+
)
|
|
116
|
+
) {
|
|
117
|
+
onEvent(event)
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
this.gatewayApprovedPermissions.add(permission.id)
|
|
121
|
+
let approval
|
|
122
|
+
try {
|
|
123
|
+
approval = this.respondPermission(
|
|
124
|
+
permission.id,
|
|
125
|
+
'always',
|
|
126
|
+
{ ownerId: this.ownerId },
|
|
127
|
+
)
|
|
128
|
+
} catch {
|
|
129
|
+
this.gatewayApprovedPermissions.delete(permission.id)
|
|
130
|
+
onEvent(event)
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
Promise.resolve(approval)
|
|
134
|
+
.then(() => this.gatewayApprovedPermissions.delete(permission.id))
|
|
135
|
+
.catch(() => {
|
|
136
|
+
if (this.gatewayApprovedPermissions.delete(permission.id)) {
|
|
137
|
+
onEvent(event)
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
createWork({ turnId, originalRequest, objective, submissionKey }) {
|
|
143
|
+
let workId = ''
|
|
144
|
+
const task = this.taskManager.create({
|
|
145
|
+
objective: originalRequest,
|
|
146
|
+
ownerId: this.ownerId,
|
|
147
|
+
sessionId: this.sessionId,
|
|
148
|
+
turnId,
|
|
149
|
+
submissionKey,
|
|
150
|
+
laneKey: `coordinator:${this.ownerId}`,
|
|
151
|
+
laneLimit: 1,
|
|
152
|
+
runner: async (_ignored, { onEvent, signal }) => this.coordinator.run({
|
|
153
|
+
originalRequest,
|
|
154
|
+
objective,
|
|
155
|
+
conversationContext: this.getConversationContext(),
|
|
156
|
+
userMemories: this.memoryStore?.list(this.ownerId) || [],
|
|
157
|
+
timeZone: this.getClientContext()?.timeZone,
|
|
158
|
+
workingDirectory: this.getClientContext()?.workingDirectory,
|
|
159
|
+
}, {
|
|
160
|
+
ownerId: this.ownerId,
|
|
161
|
+
sessionId: this.sessionId,
|
|
162
|
+
turnId,
|
|
163
|
+
coordinationRunId: workId,
|
|
164
|
+
signal,
|
|
165
|
+
onEvent: event => this.forwardCoordinatorEvent(event, onEvent),
|
|
166
|
+
}),
|
|
167
|
+
canceler: async ({ previousStatus, abort }) => {
|
|
168
|
+
if (previousStatus === 'delegated') {
|
|
169
|
+
const result = await this.coordinator.cancelDelegatedWork(
|
|
170
|
+
workId,
|
|
171
|
+
{ ownerId: this.ownerId },
|
|
172
|
+
)
|
|
173
|
+
abort()
|
|
174
|
+
return result
|
|
175
|
+
}
|
|
176
|
+
abort()
|
|
177
|
+
return {
|
|
178
|
+
route: 'adapter',
|
|
179
|
+
layer: previousStatus === 'finalizing' ? 'finalizing' : 'coordinator',
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
})
|
|
183
|
+
workId = task.id
|
|
184
|
+
this.turnTasks.set(turnId, task.id)
|
|
185
|
+
if (this.turnTasks.size > 100) {
|
|
186
|
+
this.turnTasks.delete(this.turnTasks.keys().next().value)
|
|
187
|
+
}
|
|
188
|
+
return task
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async handle(event, callContext = {}) {
|
|
192
|
+
const callId = event.call_id || event.item?.call_id || ''
|
|
193
|
+
const toolName = event.name || event.item?.name || ''
|
|
194
|
+
if (!callId) throw new Error('Realtime 工具调用缺少 call_id')
|
|
195
|
+
if (this.processedCalls.has(callId)) return
|
|
196
|
+
this.processedCalls.add(callId)
|
|
197
|
+
if (this.processedCalls.size > 500) {
|
|
198
|
+
this.processedCalls.delete(this.processedCalls.values().next().value)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const turnId = callContext.turnId
|
|
202
|
+
|| event.__voiceContext?.turnId
|
|
203
|
+
|| this.getTurnId()
|
|
204
|
+
const generation = Number.isInteger(callContext.turnGeneration)
|
|
205
|
+
? callContext.turnGeneration
|
|
206
|
+
: Number.isInteger(event.__voiceContext?.turnGeneration)
|
|
207
|
+
? event.__voiceContext.turnGeneration
|
|
208
|
+
: this.getTurnGeneration()
|
|
209
|
+
let args = {}
|
|
210
|
+
try {
|
|
211
|
+
args = JSON.parse(event.arguments || '{}')
|
|
212
|
+
} catch {
|
|
213
|
+
// Invalid arguments are handled as missing fields below.
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (this.isStale(turnId, generation)) {
|
|
217
|
+
await this.closeStaleCall(callId, turnId)
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (toolName === GET_CURRENT_TIME_TOOL_NAME) {
|
|
222
|
+
await this.getCurrentTime(callId, turnId)
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
if (toolName === USER_MEMORY_TOOL_NAME) {
|
|
226
|
+
await this.userMemory(callId, turnId, generation, args)
|
|
227
|
+
return
|
|
228
|
+
}
|
|
229
|
+
if (toolName === CANCEL_AGENT_TASK_TOOL_NAME) {
|
|
230
|
+
await this.cancelAgentTask(callId, turnId, args)
|
|
231
|
+
return
|
|
232
|
+
}
|
|
233
|
+
if (toolName === GET_AGENT_TASK_STATUS_TOOL_NAME) {
|
|
234
|
+
await this.getAgentTaskStatus(callId, turnId, args)
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
if (toolName === RESPOND_AGENT_PERMISSION_TOOL_NAME) {
|
|
238
|
+
await this.respondAgentPermission(callId, turnId, args)
|
|
239
|
+
return
|
|
240
|
+
}
|
|
241
|
+
if (toolName !== DELEGATE_TOOL_NAME) {
|
|
242
|
+
await this.sendOutput(
|
|
243
|
+
callId,
|
|
244
|
+
failure('unsupported_tool', '当前无法执行这个操作。'),
|
|
245
|
+
turnId,
|
|
246
|
+
)
|
|
247
|
+
return
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const pendingPermissionTask = this.taskManager.list({
|
|
251
|
+
ownerId: this.ownerId,
|
|
252
|
+
sessionId: this.sessionId,
|
|
253
|
+
active: true,
|
|
254
|
+
}).find(task => task.authorization?.status === 'pending')
|
|
255
|
+
if (pendingPermissionTask) {
|
|
256
|
+
await this.sendOutput(
|
|
257
|
+
callId,
|
|
258
|
+
{
|
|
259
|
+
status: 'authorization_pending',
|
|
260
|
+
error: true,
|
|
261
|
+
error_code: 'permission_decision_required',
|
|
262
|
+
authorization_id: pendingPermissionTask.authorization.id,
|
|
263
|
+
operation: pendingPermissionTask.authorization.summary,
|
|
264
|
+
user_message: '当前有一项权限请求正在等待用户决定,不能把本轮回答提交成新工作。',
|
|
265
|
+
retryable: true,
|
|
266
|
+
},
|
|
267
|
+
turnId,
|
|
268
|
+
pendingPermissionTask.id,
|
|
269
|
+
{
|
|
270
|
+
response: {
|
|
271
|
+
instructions: [
|
|
272
|
+
'当前有一项权限请求正在等待决定,本轮不能调用 spawn_thinking。',
|
|
273
|
+
'重新结合刚才提出的具体权限问题和本轮用户原话判断。',
|
|
274
|
+
'若用户已自然表达同意或拒绝,立即调用 respond_agent_permission,并逐字引用本轮原话作为 evidence;不要要求固定口令。',
|
|
275
|
+
'若用户没有作出决定,只用一句自然的话继续确认。',
|
|
276
|
+
'绝对不要代替用户同意,也不要声称权限已经生效。',
|
|
277
|
+
].join(' '),
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
)
|
|
281
|
+
return
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const resolved = await this.transcripts.resolveDelegation(
|
|
285
|
+
turnId,
|
|
286
|
+
args.objective,
|
|
287
|
+
)
|
|
288
|
+
if (this.isStale(turnId, generation)) {
|
|
289
|
+
await this.closeStaleCall(callId, turnId)
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
const originalRequest = String(
|
|
293
|
+
resolved.originalRequest || resolved.objective || '',
|
|
294
|
+
).trim()
|
|
295
|
+
const objective = String(
|
|
296
|
+
resolved.objective || resolved.originalRequest || '',
|
|
297
|
+
).trim()
|
|
298
|
+
if (!objective) {
|
|
299
|
+
await this.sendOutput(
|
|
300
|
+
callId,
|
|
301
|
+
failure(
|
|
302
|
+
'missing_objective',
|
|
303
|
+
'没有获得完整、可执行的目标,需要用户补充必要信息。',
|
|
304
|
+
{ retryable: true },
|
|
305
|
+
),
|
|
306
|
+
turnId,
|
|
307
|
+
)
|
|
308
|
+
return
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
let coordinatorReady = false
|
|
312
|
+
try {
|
|
313
|
+
coordinatorReady = await this.coordinatorAvailable()
|
|
314
|
+
} catch {
|
|
315
|
+
coordinatorReady = false
|
|
316
|
+
}
|
|
317
|
+
if (!coordinatorReady) {
|
|
318
|
+
await this.sendOutput(
|
|
319
|
+
callId,
|
|
320
|
+
failure(
|
|
321
|
+
'backend_unavailable',
|
|
322
|
+
'后台 Agent 当前未连接。可以继续语音交流,连接 OpenCode 或 OpenClaw 后再执行这项工作。',
|
|
323
|
+
{ retryable: true },
|
|
324
|
+
),
|
|
325
|
+
turnId,
|
|
326
|
+
)
|
|
327
|
+
return
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const existingId = this.turnTasks.get(turnId)
|
|
331
|
+
if (existingId) {
|
|
332
|
+
await this.sendOutput(
|
|
333
|
+
callId,
|
|
334
|
+
{
|
|
335
|
+
status: 'duplicate',
|
|
336
|
+
work_id: existingId,
|
|
337
|
+
message: '这一轮已经提交,不要重复执行。',
|
|
338
|
+
},
|
|
339
|
+
turnId,
|
|
340
|
+
existingId,
|
|
341
|
+
{ createResponse: false },
|
|
342
|
+
)
|
|
343
|
+
return
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
let task
|
|
347
|
+
try {
|
|
348
|
+
const submissionKey = [
|
|
349
|
+
'delegation',
|
|
350
|
+
this.sessionId,
|
|
351
|
+
turnId || callId,
|
|
352
|
+
].join(':')
|
|
353
|
+
task = this.createWork({
|
|
354
|
+
turnId,
|
|
355
|
+
originalRequest,
|
|
356
|
+
objective,
|
|
357
|
+
submissionKey,
|
|
358
|
+
})
|
|
359
|
+
} catch {
|
|
360
|
+
await this.sendOutput(
|
|
361
|
+
callId,
|
|
362
|
+
failure(
|
|
363
|
+
'work_submission_failed',
|
|
364
|
+
'暂时没有成功提交这次请求,请稍后重试。',
|
|
365
|
+
{ retryable: true },
|
|
366
|
+
),
|
|
367
|
+
turnId,
|
|
368
|
+
)
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
await this.sendOutput(
|
|
372
|
+
callId,
|
|
373
|
+
task.reused
|
|
374
|
+
? {
|
|
375
|
+
status: 'duplicate',
|
|
376
|
+
work_id: task.id,
|
|
377
|
+
message: '这一轮已经提交,不要重复执行。',
|
|
378
|
+
}
|
|
379
|
+
: {
|
|
380
|
+
status: 'accepted',
|
|
381
|
+
marker: '[thinking]',
|
|
382
|
+
work_id: task.id,
|
|
383
|
+
},
|
|
384
|
+
turnId,
|
|
385
|
+
task.id,
|
|
386
|
+
task.reused
|
|
387
|
+
? { createResponse: false }
|
|
388
|
+
: (
|
|
389
|
+
callContext.hasAudio
|
|
390
|
+
|| callContext.transcriptDone
|
|
391
|
+
|| callContext.pendingTranscripts?.some(item => (
|
|
392
|
+
String(item?.content || '').trim()
|
|
393
|
+
))
|
|
394
|
+
)
|
|
395
|
+
? { createResponse: false }
|
|
396
|
+
: {
|
|
397
|
+
// Let Realtime decide whether the user still needs an acknowledgement,
|
|
398
|
+
// based on what it already said before invoking the tool.
|
|
399
|
+
response: {
|
|
400
|
+
instructions: [
|
|
401
|
+
'结合本轮调用工具前你已经对用户说过的内容,自主判断是否还需要回应。',
|
|
402
|
+
'如果已经说明正在处理,不要重复或换一种说法再次确认。',
|
|
403
|
+
'如果此前没有确认,可以用包含具体任务对象的短句确认;避免“好的、收到”等通用承接语。',
|
|
404
|
+
'accepted 不代表已经完成。',
|
|
405
|
+
].join(' '),
|
|
406
|
+
},
|
|
407
|
+
},
|
|
408
|
+
)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
notifyMemoryChanged() {
|
|
412
|
+
try {
|
|
413
|
+
this.onMemoryChanged()
|
|
414
|
+
} catch {
|
|
415
|
+
// Persistence succeeded even if a live prompt refresh did not.
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
async respondAgentPermission(callId, turnId, args) {
|
|
420
|
+
const authorizationId = String(args.authorization_id || '').trim()
|
|
421
|
+
const decision = String(args.decision || '').trim()
|
|
422
|
+
const evidence = normalizedEvidence(args.evidence)
|
|
423
|
+
const transcript = String(await this.transcripts.transcript(turnId)).trim()
|
|
424
|
+
if (
|
|
425
|
+
!authorizationId
|
|
426
|
+
|| !['always', 'reject'].includes(decision)
|
|
427
|
+
|| !evidence
|
|
428
|
+
) {
|
|
429
|
+
await this.sendOutput(
|
|
430
|
+
callId,
|
|
431
|
+
failure('invalid_permission_response', '没有找到有效的权限请求或决定。'),
|
|
432
|
+
turnId,
|
|
433
|
+
)
|
|
434
|
+
return
|
|
435
|
+
}
|
|
436
|
+
if (!normalizedEvidence(transcript).includes(evidence)) {
|
|
437
|
+
await this.sendOutput(
|
|
438
|
+
callId,
|
|
439
|
+
failure(
|
|
440
|
+
'permission_evidence_mismatch',
|
|
441
|
+
'权限决定没有对应到本轮用户原话,不能提交。',
|
|
442
|
+
{ retryable: false },
|
|
443
|
+
),
|
|
444
|
+
turnId,
|
|
445
|
+
)
|
|
446
|
+
return
|
|
447
|
+
}
|
|
448
|
+
const pendingTask = this.taskManager.list({
|
|
449
|
+
ownerId: this.ownerId,
|
|
450
|
+
sessionId: this.sessionId,
|
|
451
|
+
active: true,
|
|
452
|
+
}).find(task => task.authorization?.id === authorizationId)
|
|
453
|
+
if (!pendingTask) {
|
|
454
|
+
await this.sendOutput(
|
|
455
|
+
callId,
|
|
456
|
+
failure(
|
|
457
|
+
'permission_not_pending',
|
|
458
|
+
'这项权限请求已经失效或不属于当前任务。',
|
|
459
|
+
{ retryable: false },
|
|
460
|
+
),
|
|
461
|
+
turnId,
|
|
462
|
+
)
|
|
463
|
+
return
|
|
464
|
+
}
|
|
465
|
+
if (!this.respondPermission) {
|
|
466
|
+
await this.sendOutput(
|
|
467
|
+
callId,
|
|
468
|
+
failure('permission_unavailable', '当前后台无法接收权限决定。'),
|
|
469
|
+
turnId,
|
|
470
|
+
)
|
|
471
|
+
return
|
|
472
|
+
}
|
|
473
|
+
const previousPermissionMode = this.permissionPolicy?.mode(
|
|
474
|
+
this.ownerId,
|
|
475
|
+
this.sessionId,
|
|
476
|
+
)
|
|
477
|
+
this.permissionPolicy?.applyDecision(
|
|
478
|
+
this.ownerId,
|
|
479
|
+
this.sessionId,
|
|
480
|
+
decision,
|
|
481
|
+
)
|
|
482
|
+
try {
|
|
483
|
+
const permission = await this.respondPermission(
|
|
484
|
+
authorizationId,
|
|
485
|
+
decision,
|
|
486
|
+
{ ownerId: this.ownerId },
|
|
487
|
+
)
|
|
488
|
+
await this.sendOutput(callId, {
|
|
489
|
+
status: permission.status,
|
|
490
|
+
authorization_id: permission.id,
|
|
491
|
+
}, turnId, permission.workId, {
|
|
492
|
+
response: {
|
|
493
|
+
instructions: decision === 'always'
|
|
494
|
+
? [
|
|
495
|
+
'权限已经成功生效。',
|
|
496
|
+
'只用一句简短自然口语确认“已允许,后台继续执行”。',
|
|
497
|
+
'不要重述操作,不要再次询问,不要调用工具。',
|
|
498
|
+
].join(' ')
|
|
499
|
+
: [
|
|
500
|
+
'权限已经成功拒绝。',
|
|
501
|
+
'只用一句简短自然口语确认“已拒绝,后台不会执行这项操作”。',
|
|
502
|
+
'不要重述操作,不要再次询问,不要调用工具。',
|
|
503
|
+
].join(' '),
|
|
504
|
+
},
|
|
505
|
+
})
|
|
506
|
+
} catch (error) {
|
|
507
|
+
if (previousPermissionMode) {
|
|
508
|
+
this.permissionPolicy?.setMode(
|
|
509
|
+
this.ownerId,
|
|
510
|
+
this.sessionId,
|
|
511
|
+
previousPermissionMode,
|
|
512
|
+
)
|
|
513
|
+
}
|
|
514
|
+
await this.sendOutput(
|
|
515
|
+
callId,
|
|
516
|
+
failure(
|
|
517
|
+
'permission_response_failed',
|
|
518
|
+
error?.message || '没有成功提交权限决定。',
|
|
519
|
+
{ retryable: false },
|
|
520
|
+
),
|
|
521
|
+
turnId,
|
|
522
|
+
)
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
async cancelAgentTask(callId, turnId, args) {
|
|
527
|
+
const requestedId = String(args.work_id || '').trim()
|
|
528
|
+
const targetId = requestedId || this.taskManager.list({
|
|
529
|
+
ownerId: this.ownerId,
|
|
530
|
+
sessionId: this.sessionId,
|
|
531
|
+
active: true,
|
|
532
|
+
})[0]?.id
|
|
533
|
+
if (!targetId) {
|
|
534
|
+
await this.sendOutput(callId, {
|
|
535
|
+
status: 'not_found',
|
|
536
|
+
message: '当前没有仍在排队或执行的工作。',
|
|
537
|
+
}, turnId)
|
|
538
|
+
return
|
|
539
|
+
}
|
|
540
|
+
const relatedQueries = this.taskManager.list({
|
|
541
|
+
ownerId: this.ownerId,
|
|
542
|
+
active: true,
|
|
543
|
+
includeControl: true,
|
|
544
|
+
}).filter(task => (
|
|
545
|
+
task.kind === 'control'
|
|
546
|
+
&& task.parentWorkId === targetId
|
|
547
|
+
))
|
|
548
|
+
await Promise.all(relatedQueries.map(task => (
|
|
549
|
+
this.taskManager.cancel(task.id, { ownerId: this.ownerId })
|
|
550
|
+
)))
|
|
551
|
+
const task = await this.taskManager.cancel(targetId, {
|
|
552
|
+
ownerId: this.ownerId,
|
|
553
|
+
})
|
|
554
|
+
if (!task) {
|
|
555
|
+
await this.sendOutput(callId, {
|
|
556
|
+
status: 'not_active',
|
|
557
|
+
work_id: targetId,
|
|
558
|
+
message: '这项工作已经结束,当前无法取消。',
|
|
559
|
+
}, turnId)
|
|
560
|
+
return
|
|
561
|
+
}
|
|
562
|
+
await this.sendOutput(callId, task.status === 'cancelled' ? {
|
|
563
|
+
status: task.status,
|
|
564
|
+
work_id: task.id,
|
|
565
|
+
message: '已取消这项工作。',
|
|
566
|
+
} : failure(
|
|
567
|
+
'work_cancellation_failed',
|
|
568
|
+
task.error || '没有成功取消这项工作。',
|
|
569
|
+
), turnId, task.id)
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
async getAgentTaskStatus(callId, turnId, args) {
|
|
573
|
+
const requestedId = String(args.work_id || '').trim()
|
|
574
|
+
const task = requestedId
|
|
575
|
+
? this.taskManager.get(requestedId, { ownerId: this.ownerId })
|
|
576
|
+
: this.taskManager.list({
|
|
577
|
+
ownerId: this.ownerId,
|
|
578
|
+
sessionId: this.sessionId,
|
|
579
|
+
})[0]
|
|
580
|
+
if (!task) {
|
|
581
|
+
await this.sendOutput(callId, {
|
|
582
|
+
status: 'not_found',
|
|
583
|
+
message: '当前语音会话中还没有可查询的后台工作。',
|
|
584
|
+
}, turnId)
|
|
585
|
+
return
|
|
586
|
+
}
|
|
587
|
+
if (task.status === 'delegated') {
|
|
588
|
+
const existing = this.taskManager.list({
|
|
589
|
+
ownerId: this.ownerId,
|
|
590
|
+
sessionId: this.sessionId,
|
|
591
|
+
active: true,
|
|
592
|
+
includeControl: true,
|
|
593
|
+
}).find(item => (
|
|
594
|
+
item.kind === 'control'
|
|
595
|
+
&& item.parentWorkId === task.id
|
|
596
|
+
))
|
|
597
|
+
if (existing) {
|
|
598
|
+
await this.sendOutput(callId, {
|
|
599
|
+
status: 'querying',
|
|
600
|
+
work_id: task.id,
|
|
601
|
+
query_work_id: existing.id,
|
|
602
|
+
message: '这个项目的状态和进度已经在查询中。',
|
|
603
|
+
}, turnId, task.id)
|
|
604
|
+
return
|
|
605
|
+
}
|
|
606
|
+
const transcript = String(
|
|
607
|
+
args.question || await this.transcripts.transcript(turnId) || '',
|
|
608
|
+
).trim()
|
|
609
|
+
const query = this.taskManager.create({
|
|
610
|
+
kind: 'control',
|
|
611
|
+
parentWorkId: task.id,
|
|
612
|
+
priority: 100,
|
|
613
|
+
objective: `查询“${task.objective.slice(0, 200)}”的状态:${
|
|
614
|
+
transcript || '查询当前状态和进度'
|
|
615
|
+
}`,
|
|
616
|
+
ownerId: this.ownerId,
|
|
617
|
+
sessionId: this.sessionId,
|
|
618
|
+
turnId,
|
|
619
|
+
laneKey: `coordinator:${this.ownerId}`,
|
|
620
|
+
laneLimit: 1,
|
|
621
|
+
runner: async (_ignored, { signal }) => {
|
|
622
|
+
try {
|
|
623
|
+
return await this.coordinator.queryDelegatedWork(
|
|
624
|
+
task.id,
|
|
625
|
+
transcript || '用户想了解这个第三层任务当前的状态和进度。',
|
|
626
|
+
{
|
|
627
|
+
ownerId: this.ownerId,
|
|
628
|
+
signal,
|
|
629
|
+
},
|
|
630
|
+
)
|
|
631
|
+
} catch (error) {
|
|
632
|
+
const latest = this.taskManager.get(task.id, {
|
|
633
|
+
ownerId: this.ownerId,
|
|
634
|
+
})
|
|
635
|
+
if (latest && latest.status !== 'delegated') {
|
|
636
|
+
const messages = {
|
|
637
|
+
completed: '这项工作已经完成,最终结果正在或已经交付。',
|
|
638
|
+
finalizing: '项目执行已经完成,系统正在整理最终结果。',
|
|
639
|
+
cancelled: '这项工作已经取消。',
|
|
640
|
+
failed: `这项工作已经失败:${latest.error || '没有更多错误信息。'}`,
|
|
641
|
+
}
|
|
642
|
+
return {
|
|
643
|
+
content: messages[latest.status]
|
|
644
|
+
|| `这项工作当前状态是 ${latest.status}。`,
|
|
645
|
+
metadata: {
|
|
646
|
+
parentWorkId: task.id,
|
|
647
|
+
resolvedFromLedger: true,
|
|
648
|
+
},
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
throw error
|
|
652
|
+
}
|
|
653
|
+
},
|
|
654
|
+
canceler: async ({ abort }) => {
|
|
655
|
+
abort()
|
|
656
|
+
return {
|
|
657
|
+
route: 'gateway',
|
|
658
|
+
layer: 'delegated_status_query',
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
})
|
|
662
|
+
await this.sendOutput(callId, {
|
|
663
|
+
status: 'querying',
|
|
664
|
+
work_id: task.id,
|
|
665
|
+
query_work_id: query.id,
|
|
666
|
+
message: '正在查询这个项目的状态和进度,结果出来后会自动告诉你。',
|
|
667
|
+
}, turnId, task.id)
|
|
668
|
+
return
|
|
669
|
+
}
|
|
670
|
+
const lastActivity = task.activity.at(-1)
|
|
671
|
+
const consumesTaskNotification = (
|
|
672
|
+
['completed', 'failed'].includes(task.status)
|
|
673
|
+
&& ['pending', 'delivering'].includes(task.notificationStatus)
|
|
674
|
+
)
|
|
675
|
+
await this.sendOutput(callId, {
|
|
676
|
+
status: 'ok',
|
|
677
|
+
work_id: task.id,
|
|
678
|
+
work_status: task.status,
|
|
679
|
+
objective: task.objective.slice(0, 300),
|
|
680
|
+
elapsed_ms: task.elapsedMs,
|
|
681
|
+
delegation: task.delegation
|
|
682
|
+
? {
|
|
683
|
+
status: task.delegation.status,
|
|
684
|
+
title: task.delegation.title,
|
|
685
|
+
}
|
|
686
|
+
: null,
|
|
687
|
+
authorization_pending: task.authorization?.status === 'pending',
|
|
688
|
+
last_activity: lastActivity
|
|
689
|
+
? {
|
|
690
|
+
category: lastActivity.category || lastActivity.kind,
|
|
691
|
+
status: lastActivity.status,
|
|
692
|
+
detail: String(lastActivity.detail || '').slice(0, 160),
|
|
693
|
+
}
|
|
694
|
+
: null,
|
|
695
|
+
result: task.status === 'completed'
|
|
696
|
+
? String(task.result || '').slice(0, 500)
|
|
697
|
+
: null,
|
|
698
|
+
error: ['failed', 'cancelled'].includes(task.status)
|
|
699
|
+
? task.error
|
|
700
|
+
: null,
|
|
701
|
+
}, turnId, task.id, consumesTaskNotification
|
|
702
|
+
? { responseContext: { consumesTaskNotification: true } }
|
|
703
|
+
: undefined)
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
async getCurrentTime(callId, turnId) {
|
|
707
|
+
await this.sendOutput(callId, {
|
|
708
|
+
status: 'ok',
|
|
709
|
+
...currentTimeSnapshot(this.getClientContext()),
|
|
710
|
+
}, turnId)
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
async userMemory(callId, turnId, generation, args) {
|
|
714
|
+
const action = String(args.action || '').trim().toLowerCase()
|
|
715
|
+
const scope = String(args.scope || '').trim().toLowerCase()
|
|
716
|
+
const content = String(args.content || '').trim()
|
|
717
|
+
const query = String(args.query || '').trim()
|
|
718
|
+
const memoryIds = Array.isArray(args.memory_ids)
|
|
719
|
+
? args.memory_ids.map(id => String(id || '').trim()).filter(Boolean).slice(0, 20)
|
|
720
|
+
: []
|
|
721
|
+
const all = args.all === true
|
|
722
|
+
let output
|
|
723
|
+
if (!this.memoryStore) {
|
|
724
|
+
output = failure('memory_unavailable', '前台记忆功能当前不可用。')
|
|
725
|
+
} else if (!['recall', 'remember', 'replace', 'forget'].includes(action)) {
|
|
726
|
+
output = failure('invalid_memory_action', '没有识别出要执行的记忆操作。')
|
|
727
|
+
} else if (!['profile', 'long_term', 'all'].includes(scope)) {
|
|
728
|
+
output = failure('invalid_memory_scope', '没有识别出记忆范围。')
|
|
729
|
+
} else if (action === 'recall') {
|
|
730
|
+
const memories = this.memoryStore.list(this.ownerId, { scope, query })
|
|
731
|
+
output = {
|
|
732
|
+
status: memories.length ? 'ok' : 'not_found',
|
|
733
|
+
count: memories.length,
|
|
734
|
+
memories,
|
|
735
|
+
}
|
|
736
|
+
} else if (action === 'remember' || action === 'replace') {
|
|
737
|
+
if (scope === 'all' || !content) {
|
|
738
|
+
output = failure('missing_memory', '需要明确记忆类型和要记住的内容。')
|
|
739
|
+
} else if (action === 'replace' && !memoryIds.length) {
|
|
740
|
+
output = failure(
|
|
741
|
+
'missing_memory_ids',
|
|
742
|
+
'需要先找到要被新事实取代的旧记忆。',
|
|
743
|
+
{ retryable: true },
|
|
744
|
+
)
|
|
745
|
+
} else if (SENSITIVE_MEMORY.test(content)) {
|
|
746
|
+
output = failure(
|
|
747
|
+
'sensitive_memory',
|
|
748
|
+
'为了安全,不会保存密码、密钥、验证码或令牌。',
|
|
749
|
+
{ status: 'rejected' },
|
|
750
|
+
)
|
|
751
|
+
} else {
|
|
752
|
+
try {
|
|
753
|
+
if (action === 'replace') {
|
|
754
|
+
const result = this.memoryStore.replace(this.ownerId, {
|
|
755
|
+
scope,
|
|
756
|
+
ids: memoryIds,
|
|
757
|
+
content,
|
|
758
|
+
})
|
|
759
|
+
output = {
|
|
760
|
+
status: result.replaced ? 'replaced' : 'not_found',
|
|
761
|
+
...result,
|
|
762
|
+
}
|
|
763
|
+
if (result.replaced) this.notifyMemoryChanged()
|
|
764
|
+
} else {
|
|
765
|
+
output = {
|
|
766
|
+
status: 'remembered',
|
|
767
|
+
memory: this.memoryStore.remember(this.ownerId, { scope, content }),
|
|
768
|
+
}
|
|
769
|
+
this.notifyMemoryChanged()
|
|
770
|
+
}
|
|
771
|
+
} catch {
|
|
772
|
+
output = failure(
|
|
773
|
+
'memory_write_failed',
|
|
774
|
+
'暂时无法保存这项记忆,请稍后再试。',
|
|
775
|
+
{ retryable: true },
|
|
776
|
+
)
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
} else {
|
|
780
|
+
const transcript = await this.transcripts.transcript(turnId)
|
|
781
|
+
if (this.isStale(turnId, generation)) {
|
|
782
|
+
await this.closeStaleCall(callId, turnId)
|
|
783
|
+
return
|
|
784
|
+
}
|
|
785
|
+
const explicit = /(?:忘|删除|清空|别再记|不要记|\bforget\b|\bdelete\b|\bclear\b)/i
|
|
786
|
+
.test(transcript)
|
|
787
|
+
const explicitAll = /(?:清空|全部.*(?:忘|删除)|(?:忘|删除).*全部|所有.*记忆|\bforget\s+all\b|\bclear\s+all\b)/i
|
|
788
|
+
.test(transcript)
|
|
789
|
+
if (!explicit) {
|
|
790
|
+
output = failure(
|
|
791
|
+
'explicit_consent_required',
|
|
792
|
+
'没有听到明确的遗忘请求,因此没有删除记忆。',
|
|
793
|
+
{ status: 'rejected' },
|
|
794
|
+
)
|
|
795
|
+
} else if (all && !explicitAll) {
|
|
796
|
+
output = failure(
|
|
797
|
+
'clear_all_consent_required',
|
|
798
|
+
'用户没有明确要求清空全部记忆,因此没有删除。',
|
|
799
|
+
{ status: 'rejected' },
|
|
800
|
+
)
|
|
801
|
+
} else if (!all && !query) {
|
|
802
|
+
output = failure('missing_memory_query', '需要明确要遗忘的内容。')
|
|
803
|
+
} else {
|
|
804
|
+
try {
|
|
805
|
+
const removed = this.memoryStore.forget(this.ownerId, {
|
|
806
|
+
scope,
|
|
807
|
+
query,
|
|
808
|
+
all,
|
|
809
|
+
})
|
|
810
|
+
if (removed) this.notifyMemoryChanged()
|
|
811
|
+
output = { status: removed ? 'forgotten' : 'not_found', removed }
|
|
812
|
+
} catch {
|
|
813
|
+
output = failure(
|
|
814
|
+
'memory_write_failed',
|
|
815
|
+
'暂时无法删除这项记忆,请稍后再试。',
|
|
816
|
+
{ retryable: true },
|
|
817
|
+
)
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
await this.sendOutput(callId, output, turnId)
|
|
822
|
+
}
|
|
823
|
+
}
|