@ynhcj/xiaoyi-channel 0.0.231-next → 0.0.232-next
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/dist/index.js +171 -6
- package/dist/src/channel.js +5 -2
- package/dist/src/conversation/conversation-manager.d.ts +12 -3
- package/dist/src/conversation/conversation-manager.js +89 -8
- package/dist/src/conversation/conversation-session.d.ts +16 -0
- package/dist/src/conversation/conversation-session.js +5 -0
- package/dist/src/conversation/cron-buffer.d.ts +36 -11
- package/dist/src/conversation/cron-buffer.js +157 -56
- package/dist/src/conversation/outbound-gateway.d.ts +4 -0
- package/dist/src/conversation/outbound-gateway.js +2 -2
- package/dist/src/conversation/outbound-queue.d.ts +35 -0
- package/dist/src/conversation/outbound-queue.js +96 -0
- package/dist/src/conversation/stream-assembler.d.ts +52 -0
- package/dist/src/conversation/stream-assembler.js +122 -0
- package/dist/src/cron-command.js +2 -24
- package/dist/src/cspl/call_api.js +8 -2
- package/dist/src/cspl/constants.d.ts +1 -0
- package/dist/src/cspl/constants.js +2 -0
- package/dist/src/cspl/sentinel_hook.js +6 -9
- package/dist/src/cspl/utils.d.ts +2 -2
- package/dist/src/cspl/utils.js +8 -8
- package/dist/src/dispatch/bot.js +43 -3
- package/dist/src/dispatch/outbound.js +14 -5
- package/dist/src/dispatch/reply-dispatcher.js +283 -155
- package/dist/src/dynamic-skill-trigger.d.ts +8 -0
- package/dist/src/dynamic-skill-trigger.js +62 -0
- package/dist/src/formatter.d.ts +7 -0
- package/dist/src/formatter.js +71 -94
- package/dist/src/provider.js +87 -4
- package/dist/src/reply-dispatcher.js +16 -12
- package/dist/src/self-evolution-handler.d.ts +1 -1
- package/dist/src/skill-retriever/config.js +2 -2
- package/dist/src/skill-retriever/hooks.js +34 -13
- package/dist/src/skill-retriever/skill-status.d.ts +22 -0
- package/dist/src/skill-retriever/skill-status.js +125 -0
- package/dist/src/skill-retriever/tool-search.d.ts +4 -2
- package/dist/src/skill-retriever/tool-search.js +75 -15
- package/dist/src/skill-retriever/types.d.ts +1 -0
- package/dist/src/tools/agent-as-skill-tool.js +1 -1
- package/dist/src/tools/calendar-tool.js +1 -1
- package/dist/src/tools/call-device-tool.js +24 -13
- package/dist/src/tools/call-phone-tool.js +4 -2
- package/dist/src/tools/check-plugin-privilege-tool.js +1 -1
- package/dist/src/tools/create-alarm-tool.js +1 -1
- package/dist/src/tools/delete-alarm-tool.js +1 -1
- package/dist/src/tools/discover-cross-devices-tool.js +1 -1
- package/dist/src/tools/display-a2ui-card-bypath-tool.js +43 -10
- package/dist/src/tools/hmos-cli.js +2 -2
- package/dist/src/tools/invoke.js +4 -4
- package/dist/src/tools/location-tool.js +1 -1
- package/dist/src/tools/login-token-tool.js +1 -1
- package/dist/src/tools/modify-alarm-tool.js +1 -1
- package/dist/src/tools/modify-note-tool.js +1 -1
- package/dist/src/tools/note-tool.js +1 -1
- package/dist/src/tools/query-app-message-tool.js +1 -1
- package/dist/src/tools/query-memory-data-tool.js +1 -1
- package/dist/src/tools/query-todo-task-tool.js +1 -1
- package/dist/src/tools/save-file-to-phone-tool.js +1 -1
- package/dist/src/tools/save-media-to-gallery-tool.js +1 -1
- package/dist/src/tools/search-alarm-tool.js +1 -1
- package/dist/src/tools/search-calendar-tool.js +1 -1
- package/dist/src/tools/search-contact-tool.js +1 -1
- package/dist/src/tools/search-email-tool.js +1 -1
- package/dist/src/tools/search-file-tool.js +1 -1
- package/dist/src/tools/search-message-tool.js +1 -1
- package/dist/src/tools/search-note-tool.js +1 -1
- package/dist/src/tools/search-photo-gallery-tool.js +1 -1
- package/dist/src/tools/send-cross-device-task-tool.js +1 -1
- package/dist/src/tools/send-email-tool.js +1 -2
- package/dist/src/tools/send-file-to-user-tool.js +1 -1
- package/dist/src/tools/send-message-tool.js +4 -2
- package/dist/src/tools/session-manager.d.ts +4 -4
- package/dist/src/tools/session-manager.js +23 -12
- package/dist/src/tools/upload-file-tool.js +1 -1
- package/dist/src/tools/upload-photo-tool.js +1 -1
- package/dist/src/tools/xiaoyi-add-collection-tool.js +1 -1
- package/dist/src/tools/xiaoyi-collection-tool.js +1 -1
- package/dist/src/tools/xiaoyi-delete-collection-tool.js +1 -1
- package/dist/src/tools/xiaoyi-gui-tool.js +3 -3
- package/dist/src/transport/push.d.ts +3 -1
- package/dist/src/transport/push.js +9 -1
- package/dist/src/trigger-handler.js +1 -1
- package/dist/src/utils/async-mutex.d.ts +5 -0
- package/dist/src/utils/async-mutex.js +14 -0
- package/dist/src/utils/cron-push-map.js +29 -21
- package/dist/src/utils/pushdata-manager.d.ts +9 -1
- package/dist/src/utils/pushdata-manager.js +28 -19
- package/package.json +1 -1
|
@@ -5,7 +5,16 @@ import { clearRunCrossTaskSentFiles, getCurrentSessionContext } from "../tools/s
|
|
|
5
5
|
import fs from "fs/promises";
|
|
6
6
|
import path from "path";
|
|
7
7
|
import { logger } from "../utils/logger.js";
|
|
8
|
-
import { getWaitState, clearWaitState, getCurrentTaskId, getCurrentMessageId, startStatusHeartbeat, stopStatusHeartbeat, setSessionState, } from "../conversation/conversation-manager.js";
|
|
8
|
+
import { getWaitState, clearWaitState, getCurrentTaskId, getCurrentMessageId, getOrCreateSession, startStatusHeartbeat, stopStatusHeartbeat, setSessionState, clearTurnInflight, deliverSubagentFinalResult, } from "../conversation/conversation-manager.js";
|
|
9
|
+
import { StreamAssembler } from "../conversation/stream-assembler.js";
|
|
10
|
+
// ⚙️ 前缀是 openclaw 系统消息稳定标记(infra/system-message.ts SYSTEM_MARK)。
|
|
11
|
+
// ACP 绑定会话 turn 结束会以 kind=final 尾随投递系统诊断通知(如
|
|
12
|
+
// "Session ids resolved"),deliver 捕获权威文本时必须排除,否则最终帧
|
|
13
|
+
// 会把答案替换成通知。
|
|
14
|
+
const SYSTEM_NOTICE_MARK = "⚙️";
|
|
15
|
+
function isSystemNoticeText(text) {
|
|
16
|
+
return text.trimStart().startsWith(SYSTEM_NOTICE_MARK);
|
|
17
|
+
}
|
|
9
18
|
const TEMP_FILE_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
10
19
|
const RUN_CROSS_TASK_LOG_TAG = "[RunCrossTask]";
|
|
11
20
|
function buildDistributionStatusCommand(context) {
|
|
@@ -121,18 +130,22 @@ export function createXYReplyDispatcher(params) {
|
|
|
121
130
|
};
|
|
122
131
|
let hasSentResponse = false;
|
|
123
132
|
let finalSent = false;
|
|
124
|
-
|
|
125
|
-
//
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
//
|
|
134
|
-
//
|
|
135
|
-
|
|
133
|
+
// ── 文本装配与出站时序由对话管理层拥有 ─────────────────────
|
|
134
|
+
// assembler:model/injected 段拼接 + deliver 权威文本修正,生命周期随本
|
|
135
|
+
// dispatcher(同时挂载到 session 上,供 display-a2ui-card 等工具注入文本段)。
|
|
136
|
+
// outboundQueue:会话级出站 FIFO,所有出站帧的唯一时序收口(partial 帧
|
|
137
|
+
// coalescing、终态帧 delayMs)。dispatcher 只做事件翻译和业务决策。
|
|
138
|
+
const session = getOrCreateSession(sessionId);
|
|
139
|
+
const assembler = new StreamAssembler();
|
|
140
|
+
session.assembler = assembler;
|
|
141
|
+
const outboundQueue = session.outboundQueue;
|
|
142
|
+
// 终态帧延迟:正文 artifact 与终态帧几乎同一毫秒发出,下游服务端对长正文
|
|
143
|
+
// 走慢速管道会把正文延迟到最后投递,导致微服务先收到终态帧(0 关闭)。
|
|
144
|
+
const terminalFrameDelayMs = config.terminalFrameDelayMs ?? 0;
|
|
145
|
+
// 串行化 onPartialReply 回调体:SDK fire-and-forget 模式下多个回调可能
|
|
146
|
+
// 并发进入,streamChain 保证 assembler 读写互斥;onIdle 在 finalize 前
|
|
147
|
+
// await 它,确保在途回调全部落账(否则读取的装配状态缺最后几个流式块)。
|
|
148
|
+
let streamChain = Promise.resolve();
|
|
136
149
|
const initialRunCrossTaskContext = getCurrentSessionContext()?.runCrossTaskContext;
|
|
137
150
|
const getRunCrossTaskContext = () => {
|
|
138
151
|
return getCurrentSessionContext()?.runCrossTaskContext ?? initialRunCrossTaskContext;
|
|
@@ -173,9 +186,12 @@ export function createXYReplyDispatcher(params) {
|
|
|
173
186
|
scopedLog().log(`[DELIVER SKIP] Empty text, skipping`);
|
|
174
187
|
return;
|
|
175
188
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
189
|
+
// 捕获权威最终文本(canonical,来自最后一条 assistant 消息,
|
|
190
|
+
// 非流式累计),供 onIdle 拼接最终帧。compaction 通知和 ⚙️ 系统
|
|
191
|
+
// 通知(如 ACP "Session ids resolved")不是答案文本,不能污染。
|
|
192
|
+
if (info?.kind === "final" && !payload.isCompactionNotice && !isSystemNoticeText(text)) {
|
|
193
|
+
assembler.onFinalText(text);
|
|
194
|
+
scopedLog().log(`[DELIVER] Captured final reply text, length=${text.length}`);
|
|
179
195
|
}
|
|
180
196
|
// onPartialReply 已经通过 append:false 全量发送了所有文本,deliver 不再重复发送
|
|
181
197
|
if (hasSentResponse) {
|
|
@@ -183,15 +199,23 @@ export function createXYReplyDispatcher(params) {
|
|
|
183
199
|
return;
|
|
184
200
|
}
|
|
185
201
|
hasSentResponse = true;
|
|
186
|
-
// 非流式回退路径(onPartialReply
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
202
|
+
// 非流式回退路径(onPartialReply 未触发时,如 ACP 绑定会话),
|
|
203
|
+
// 同样经出站队列保序;与流式帧同 coalesceKey,可被后续全量帧合并。
|
|
204
|
+
const deliverTaskId = getActiveTaskId();
|
|
205
|
+
const deliverMessageId = getActiveMessageId();
|
|
206
|
+
outboundQueue.enqueue({
|
|
207
|
+
taskId: deliverTaskId,
|
|
208
|
+
label: "deliver-fallback",
|
|
209
|
+
coalesceKey: `partial:${deliverTaskId}`,
|
|
210
|
+
send: () => sendA2AResponse({
|
|
211
|
+
config,
|
|
212
|
+
sessionId,
|
|
213
|
+
taskId: deliverTaskId,
|
|
214
|
+
messageId: deliverMessageId,
|
|
215
|
+
text,
|
|
216
|
+
append: false,
|
|
217
|
+
final: false,
|
|
218
|
+
}),
|
|
195
219
|
});
|
|
196
220
|
}
|
|
197
221
|
catch (deliverError) {
|
|
@@ -202,47 +226,54 @@ export function createXYReplyDispatcher(params) {
|
|
|
202
226
|
runtime.error?.(`xy: ${info.kind} reply failed: ${String(err)}`);
|
|
203
227
|
stopStatusInterval();
|
|
204
228
|
if (!hasSentResponse) {
|
|
205
|
-
|
|
206
|
-
|
|
229
|
+
const errorTaskId = getActiveTaskId();
|
|
230
|
+
outboundQueue.enqueue({
|
|
231
|
+
taskId: errorTaskId,
|
|
232
|
+
label: "error-status",
|
|
233
|
+
send: () => sendStatusUpdate({
|
|
207
234
|
config,
|
|
208
235
|
sessionId,
|
|
209
|
-
taskId:
|
|
236
|
+
taskId: errorTaskId,
|
|
210
237
|
messageId: getActiveMessageId(),
|
|
211
238
|
text: "处理失败,请稍后重试",
|
|
212
239
|
state: "failed",
|
|
213
|
-
})
|
|
214
|
-
}
|
|
215
|
-
catch (statusError) {
|
|
216
|
-
scopedLog().error(`Failed to send error status:`, statusError);
|
|
217
|
-
}
|
|
240
|
+
}),
|
|
241
|
+
});
|
|
218
242
|
}
|
|
219
243
|
},
|
|
220
244
|
onIdle: async () => {
|
|
221
245
|
scopedLog().log(`[ON-IDLE] Reply idle, hasSentResponse=${hasSentResponse}, finalSent=${finalSent}`);
|
|
246
|
+
// 🔑 本 turn 不再产生流式帧:清除在途标记,放行 subagent 终态帧的
|
|
247
|
+
// 时机门控(见 conversation-manager.deliverSubagentFinalResult)。
|
|
248
|
+
clearTurnInflight(sessionId, taskId);
|
|
222
249
|
// ── Subagent wait state check ─────────────────────────────
|
|
223
250
|
// If this session has pending subagent completions, suppress final:true
|
|
224
251
|
// and keep the A2A session alive. The final response will be sent when
|
|
225
252
|
// all completions arrive via xyOutbound.sendText interception.
|
|
226
|
-
const waitState = getWaitState(sessionId, taskId);
|
|
253
|
+
const waitState = getWaitState(sessionId, taskId) ?? getWaitState(sessionId);
|
|
227
254
|
if (waitState &&
|
|
228
255
|
waitState.deliveredCompletions < waitState.expectedCompletions &&
|
|
229
256
|
!finalSent) {
|
|
230
257
|
scopedLog().log(`[ON-IDLE] Waiting for subagent completions ${waitState.deliveredCompletions}/${waitState.expectedCompletions}, suppressing final:true`);
|
|
231
258
|
// 会话不结束:转入 waiting-subagent,心跳由对话管理层继续维持
|
|
232
259
|
setSessionState(sessionId, "waiting-subagent");
|
|
233
|
-
|
|
234
|
-
|
|
260
|
+
// 🔑 保活帧必须使用最新 A2A 身份:steer 场景下旧 taskId 已被服务端
|
|
261
|
+
// 作废,用旧身份发帧无任何回应。读任务链末尾(新 dispatcher 场景下
|
|
262
|
+
// 与本 dispatcher 闭包 taskId 一致;旧 dispatcher 晚到触发时除外)。
|
|
263
|
+
const aliveTaskId = getActiveTaskId();
|
|
264
|
+
const aliveMessageId = getActiveMessageId();
|
|
265
|
+
outboundQueue.enqueue({
|
|
266
|
+
taskId: aliveTaskId,
|
|
267
|
+
label: "subagent-waiting-status",
|
|
268
|
+
send: () => sendStatusUpdate({
|
|
235
269
|
config,
|
|
236
270
|
sessionId,
|
|
237
|
-
taskId,
|
|
238
|
-
messageId,
|
|
271
|
+
taskId: aliveTaskId,
|
|
272
|
+
messageId: aliveMessageId,
|
|
239
273
|
text: "子任务正在处理中,请稍候~",
|
|
240
274
|
state: "working",
|
|
241
|
-
})
|
|
242
|
-
}
|
|
243
|
-
catch (err) {
|
|
244
|
-
scopedLog().error(`[ON-IDLE] Failed to send subagent waiting status:`, err);
|
|
245
|
-
}
|
|
275
|
+
}),
|
|
276
|
+
});
|
|
246
277
|
// 心跳由对话管理层拥有,subagent 等待期间自动存活,无需附加到等待态
|
|
247
278
|
return;
|
|
248
279
|
}
|
|
@@ -252,6 +283,17 @@ export function createXYReplyDispatcher(params) {
|
|
|
252
283
|
// otherwise the mock server/client sees two final frames.
|
|
253
284
|
if (waitState && waitState.deliveredCompletions >= waitState.expectedCompletions) {
|
|
254
285
|
scopedLog().log(`[ON-IDLE] Subagent completions all arrived (${waitState.deliveredCompletions}/${waitState.expectedCompletions}), deferring final to parent-settled path`);
|
|
286
|
+
// 🔑 父 turn 已 settle 时就地交付:finalize 可能因本 turn 当时在途
|
|
287
|
+
// 被时机门控推迟,而 parentSettled 早已被(旧 dispatcher 的)
|
|
288
|
+
// onSettled 标记 —— 那条路径不会再触发交付,必须由本分支收口。
|
|
289
|
+
// 本 turn 已 idle(在途标记已在入口清除),交付不会被门控拦截;
|
|
290
|
+
// deliverSubagentFinalResult 幂等,重复触发为 no-op。
|
|
291
|
+
if (waitState.parentSettled) {
|
|
292
|
+
await deliverSubagentFinalResult({
|
|
293
|
+
state: waitState,
|
|
294
|
+
reason: "parent-settled-completions-ready-at-idle",
|
|
295
|
+
});
|
|
296
|
+
}
|
|
255
297
|
return;
|
|
256
298
|
}
|
|
257
299
|
// ── End subagent wait state check ─────────────────────────
|
|
@@ -262,56 +304,107 @@ export function createXYReplyDispatcher(params) {
|
|
|
262
304
|
try {
|
|
263
305
|
// 正常模式
|
|
264
306
|
if (hasSentResponse && !finalSent) {
|
|
265
|
-
//
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
//
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
307
|
+
// 🔑 先等在途 onPartialReply 全部落账,再读装配状态。openclaw 以
|
|
308
|
+
// void fire-and-forget 调用 onPartialReply,onIdle 触发时最后一个
|
|
309
|
+
// 回调可能仍在 streamChain 上排队;提前读取会缺最后几个流式块。
|
|
310
|
+
await streamChain;
|
|
311
|
+
// 🔑 终态帧延迟会让出宏任务,期间新用户消息可能把 session 绑定
|
|
312
|
+
// 更新到新 task,必须提前捕获 taskId/messageId,避免终态帧误挂到
|
|
313
|
+
// 新 task 上、提前终止新任务的流。
|
|
314
|
+
const terminalTaskId = getActiveTaskId();
|
|
315
|
+
const terminalMessageId = getActiveMessageId();
|
|
316
|
+
// 🔑 权威文本合并:deliver(kind=final) 捕获的 canonical 文本
|
|
317
|
+
// (openclaw 从最后一条 assistant 消息直接提取)非空时替换流式
|
|
318
|
+
// 累计的末轮 —— message_end 的最终清洗文本不走 onPartialReply,
|
|
319
|
+
// 流式累计天然可能缺尾,以 canonical 为准。
|
|
320
|
+
const { fullText: fullFinalText, resolvedLastText, diagnostic } = assembler.finalize();
|
|
321
|
+
if (diagnostic === "patched-tail") {
|
|
322
|
+
scopedLog().log(`[ON-IDLE] Streamed text missing tail, patched from final payload, canonical.length=${resolvedLastText.length}`);
|
|
323
|
+
}
|
|
324
|
+
else if (diagnostic === "canonical-shorter") {
|
|
325
|
+
scopedLog().log(`[ON-IDLE] Final text shorter than streamed, using canonical final, canonical.length=${resolvedLastText.length}`);
|
|
326
|
+
}
|
|
327
|
+
else if (diagnostic === "diverged") {
|
|
328
|
+
scopedLog().warn(`[ON-IDLE] Final text diverged from streamed text, using canonical final, canonical.length=${resolvedLastText.length}`);
|
|
329
|
+
}
|
|
330
|
+
const crossTaskResultMessage = resolvedLastText.trim();
|
|
331
|
+
scopedLog().log(`[ON-IDLE] [SendCrossResult]Sending cross-task result, resultMessage.length=${crossTaskResultMessage.length}`);
|
|
274
332
|
try {
|
|
275
333
|
const runCrossTaskContext = getRunCrossTaskContext();
|
|
276
334
|
if (runCrossTaskContext) {
|
|
277
|
-
|
|
335
|
+
const ctx = runCrossTaskContext;
|
|
336
|
+
outboundQueue.enqueue({
|
|
337
|
+
taskId: terminalTaskId,
|
|
338
|
+
label: "cross-task-result",
|
|
339
|
+
send: () => sendRunCrossTaskResult({
|
|
340
|
+
config,
|
|
341
|
+
sessionId,
|
|
342
|
+
taskId: terminalTaskId,
|
|
343
|
+
messageId: terminalMessageId,
|
|
344
|
+
context: ctx,
|
|
345
|
+
resultCode: "0",
|
|
346
|
+
resultMessage: crossTaskResultMessage,
|
|
347
|
+
}),
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
// 🔑 终态帧延迟:让已发出的长正文先穿过下游服务端慢速管道,
|
|
351
|
+
// 保证终态帧最后到达(openclaw6.6 3e7b1aa/cdc4cdc 同款治理,
|
|
352
|
+
// delayMs 在队列 drain 内 sleep,阻塞后续帧)。
|
|
353
|
+
outboundQueue.enqueue({
|
|
354
|
+
taskId: terminalTaskId,
|
|
355
|
+
label: "terminal-status",
|
|
356
|
+
delayMs: terminalFrameDelayMs,
|
|
357
|
+
send: () => sendStatusUpdate({
|
|
278
358
|
config,
|
|
279
359
|
sessionId,
|
|
280
|
-
taskId:
|
|
281
|
-
messageId:
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
360
|
+
taskId: terminalTaskId,
|
|
361
|
+
messageId: terminalMessageId,
|
|
362
|
+
text: "任务处理已完成~",
|
|
363
|
+
state: "completed",
|
|
364
|
+
}),
|
|
365
|
+
});
|
|
366
|
+
// 🔑 最终帧携带权威全文本(append:false 整体替换)—— 流式期间
|
|
367
|
+
// 缺失的尾部在此补齐。空文本属异常路径,回退旧的空帧语义
|
|
368
|
+
// (append:true 仅标记流结束),避免把客户端已展示内容刷空。
|
|
369
|
+
if (fullFinalText) {
|
|
370
|
+
outboundQueue.enqueue({
|
|
371
|
+
taskId: terminalTaskId,
|
|
372
|
+
label: "terminal-final",
|
|
373
|
+
delayMs: terminalFrameDelayMs,
|
|
374
|
+
send: () => sendA2AResponse({
|
|
375
|
+
config,
|
|
376
|
+
sessionId,
|
|
377
|
+
taskId: terminalTaskId,
|
|
378
|
+
messageId: terminalMessageId,
|
|
379
|
+
text: fullFinalText,
|
|
380
|
+
append: false,
|
|
381
|
+
final: true,
|
|
382
|
+
}),
|
|
285
383
|
});
|
|
286
384
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
messageId: getActiveMessageId(),
|
|
305
|
-
text: "",
|
|
306
|
-
append: true,
|
|
307
|
-
final: true,
|
|
308
|
-
});
|
|
385
|
+
else {
|
|
386
|
+
outboundQueue.enqueue({
|
|
387
|
+
taskId: terminalTaskId,
|
|
388
|
+
label: "terminal-final-empty",
|
|
389
|
+
delayMs: terminalFrameDelayMs,
|
|
390
|
+
send: () => sendA2AResponse({
|
|
391
|
+
config,
|
|
392
|
+
sessionId,
|
|
393
|
+
taskId: terminalTaskId,
|
|
394
|
+
messageId: terminalMessageId,
|
|
395
|
+
text: "",
|
|
396
|
+
append: true,
|
|
397
|
+
final: true,
|
|
398
|
+
}),
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
await outboundQueue.whenIdle();
|
|
309
402
|
finalSent = true;
|
|
310
403
|
if (waitState) {
|
|
311
404
|
clearWaitState(sessionId, "main-final-delivered", taskId);
|
|
312
405
|
}
|
|
313
406
|
setSessionState(sessionId, "completed");
|
|
314
|
-
scopedLog().log(`[ON-IDLE] Sent final response (
|
|
407
|
+
scopedLog().log(`[ON-IDLE] Sent final response (${fullFinalText ? `append=false, full text length=${fullFinalText.length}` : "append=true, empty stream-end"})`);
|
|
315
408
|
}
|
|
316
409
|
catch (err) {
|
|
317
410
|
scopedLog().error(`[ON-IDLE] Failed to send final response:`, err);
|
|
@@ -320,39 +413,54 @@ export function createXYReplyDispatcher(params) {
|
|
|
320
413
|
else {
|
|
321
414
|
// 正常失败场景
|
|
322
415
|
scopedLog().log(`[ON-IDLE] Skipping final message: hasSentResponse=${hasSentResponse}, finalSent=${finalSent}`);
|
|
416
|
+
const failureTaskId = getActiveTaskId();
|
|
417
|
+
const failureMessageId = getActiveMessageId();
|
|
323
418
|
try {
|
|
324
419
|
const runCrossTaskContext = getRunCrossTaskContext();
|
|
325
420
|
if (runCrossTaskContext) {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
421
|
+
const ctx = runCrossTaskContext;
|
|
422
|
+
outboundQueue.enqueue({
|
|
423
|
+
taskId: failureTaskId,
|
|
424
|
+
label: "cross-task-result-failure",
|
|
425
|
+
send: () => sendRunCrossTaskResult({
|
|
426
|
+
config,
|
|
427
|
+
sessionId,
|
|
428
|
+
taskId: failureTaskId,
|
|
429
|
+
messageId: failureMessageId,
|
|
430
|
+
context: ctx,
|
|
431
|
+
resultCode: "1",
|
|
432
|
+
resultMessage: "任务执行异常,请重试",
|
|
433
|
+
}),
|
|
334
434
|
});
|
|
335
435
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
436
|
+
outboundQueue.enqueue({
|
|
437
|
+
taskId: failureTaskId,
|
|
438
|
+
label: "failure-status",
|
|
439
|
+
send: () => sendStatusUpdate({
|
|
440
|
+
config,
|
|
441
|
+
sessionId,
|
|
442
|
+
taskId: failureTaskId,
|
|
443
|
+
messageId: failureMessageId,
|
|
444
|
+
text: "任务处理中断了~",
|
|
445
|
+
state: "failed",
|
|
446
|
+
}),
|
|
343
447
|
});
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
448
|
+
outboundQueue.enqueue({
|
|
449
|
+
taskId: failureTaskId,
|
|
450
|
+
label: "failure-final",
|
|
451
|
+
send: () => sendA2AResponse({
|
|
452
|
+
config,
|
|
453
|
+
sessionId,
|
|
454
|
+
taskId: failureTaskId,
|
|
455
|
+
messageId: failureMessageId,
|
|
456
|
+
text: "任务执行异常,请重试~",
|
|
457
|
+
append: false,
|
|
458
|
+
final: true,
|
|
459
|
+
errorCode: 99921111,
|
|
460
|
+
errorMessage: "任务执行异常,请重试",
|
|
461
|
+
}),
|
|
355
462
|
});
|
|
463
|
+
await outboundQueue.whenIdle();
|
|
356
464
|
finalSent = true;
|
|
357
465
|
setSessionState(sessionId, "failed");
|
|
358
466
|
scopedLog().log(`[ON-IDLE] Sent error response, code=99921111`);
|
|
@@ -364,6 +472,11 @@ export function createXYReplyDispatcher(params) {
|
|
|
364
472
|
}
|
|
365
473
|
finally {
|
|
366
474
|
stopStatusInterval();
|
|
475
|
+
// 卸载 assembler(终态后工具不再有注入窗口);仅在仍属于本
|
|
476
|
+
// dispatcher 时卸载,避免误删 steer 新 dispatcher 的实例。
|
|
477
|
+
if (session.assembler === assembler) {
|
|
478
|
+
session.assembler = undefined;
|
|
479
|
+
}
|
|
367
480
|
// 🔑 清理必须在 onIdle 内部完成,因为 openclaw 的 waitForIdle() 不会
|
|
368
481
|
// await onIdle 返回的 Promise(源码中为 void options.onIdle?.()),
|
|
369
482
|
// 导致 onSettled 在 onIdle 的 async 工作完成之前就执行。
|
|
@@ -395,13 +508,18 @@ export function createXYReplyDispatcher(params) {
|
|
|
395
508
|
return;
|
|
396
509
|
}
|
|
397
510
|
try {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
511
|
+
const toolTaskId = getActiveTaskId();
|
|
512
|
+
outboundQueue.enqueue({
|
|
513
|
+
taskId: toolTaskId,
|
|
514
|
+
label: "tool-start-status",
|
|
515
|
+
send: () => sendStatusUpdate({
|
|
516
|
+
config,
|
|
517
|
+
sessionId,
|
|
518
|
+
taskId: toolTaskId,
|
|
519
|
+
messageId: getActiveMessageId(),
|
|
520
|
+
text: `正在使用工具: ${toolName}...`,
|
|
521
|
+
state: "working",
|
|
522
|
+
}),
|
|
405
523
|
});
|
|
406
524
|
scopedLog().log(`[TOOL-START] Sent status update for tool start: ${toolName}`);
|
|
407
525
|
}
|
|
@@ -417,13 +535,18 @@ export function createXYReplyDispatcher(params) {
|
|
|
417
535
|
try {
|
|
418
536
|
if (text.length > 0 || hasMedia) {
|
|
419
537
|
const resultText = text.length > 0 ? text : "工具执行完成";
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
538
|
+
const toolTaskId = getActiveTaskId();
|
|
539
|
+
outboundQueue.enqueue({
|
|
540
|
+
taskId: toolTaskId,
|
|
541
|
+
label: "tool-result-status",
|
|
542
|
+
send: () => sendStatusUpdate({
|
|
543
|
+
config,
|
|
544
|
+
sessionId,
|
|
545
|
+
taskId: toolTaskId,
|
|
546
|
+
messageId: getActiveMessageId(),
|
|
547
|
+
text: resultText,
|
|
548
|
+
state: "working",
|
|
549
|
+
}),
|
|
427
550
|
});
|
|
428
551
|
scopedLog().log(`[TOOL-RESULT] Sent tool result as status update`);
|
|
429
552
|
}
|
|
@@ -441,13 +564,18 @@ export function createXYReplyDispatcher(params) {
|
|
|
441
564
|
}
|
|
442
565
|
try {
|
|
443
566
|
if (text.length > 0) {
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
567
|
+
const reasoningTaskId = getActiveTaskId();
|
|
568
|
+
outboundQueue.enqueue({
|
|
569
|
+
taskId: reasoningTaskId,
|
|
570
|
+
label: "reasoning-stream",
|
|
571
|
+
send: () => sendReasoningTextUpdate({
|
|
572
|
+
config,
|
|
573
|
+
sessionId,
|
|
574
|
+
taskId: reasoningTaskId,
|
|
575
|
+
messageId: getActiveMessageId(),
|
|
576
|
+
text,
|
|
577
|
+
append: false,
|
|
578
|
+
}),
|
|
451
579
|
});
|
|
452
580
|
}
|
|
453
581
|
}
|
|
@@ -460,41 +588,41 @@ export function createXYReplyDispatcher(params) {
|
|
|
460
588
|
if (text.length === 0)
|
|
461
589
|
return;
|
|
462
590
|
hasSentResponse = true;
|
|
463
|
-
//
|
|
464
|
-
//
|
|
465
|
-
//
|
|
466
|
-
const
|
|
467
|
-
let
|
|
468
|
-
|
|
469
|
-
|
|
591
|
+
// 串行化回调体:SDK fire-and-forget 模式下多个 onPartialReply 可能
|
|
592
|
+
// 并发进入,streamChain 保证 assembler 读写互斥;发送本身由会话
|
|
593
|
+
// 出站队列保序,append:false 全量帧携带 coalesceKey 可被合并。
|
|
594
|
+
const prevChain = streamChain;
|
|
595
|
+
let releaseChain;
|
|
596
|
+
streamChain = new Promise((resolve) => {
|
|
597
|
+
releaseChain = resolve;
|
|
470
598
|
});
|
|
471
599
|
try {
|
|
472
|
-
await
|
|
473
|
-
//
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
600
|
+
await prevChain;
|
|
601
|
+
// assembler 内部完成 startsWith 边界检测与跨调用锁存(含轮间换行)
|
|
602
|
+
const { fullText } = assembler.onStreamText(text);
|
|
603
|
+
const partialTaskId = getActiveTaskId();
|
|
604
|
+
const partialMessageId = getActiveMessageId();
|
|
605
|
+
outboundQueue.enqueue({
|
|
606
|
+
taskId: partialTaskId,
|
|
607
|
+
label: "partial",
|
|
608
|
+
coalesceKey: `partial:${partialTaskId}`,
|
|
609
|
+
send: () => sendA2AResponse({
|
|
610
|
+
config,
|
|
611
|
+
sessionId,
|
|
612
|
+
taskId: partialTaskId,
|
|
613
|
+
messageId: partialMessageId,
|
|
614
|
+
text: fullText,
|
|
615
|
+
append: false,
|
|
616
|
+
final: false,
|
|
617
|
+
log: false,
|
|
618
|
+
}),
|
|
491
619
|
});
|
|
492
620
|
}
|
|
493
621
|
catch (err) {
|
|
494
622
|
scopedLog().error(`[PARTIAL-REPLY] Failed to send:`, err);
|
|
495
623
|
}
|
|
496
624
|
finally {
|
|
497
|
-
|
|
625
|
+
releaseChain();
|
|
498
626
|
}
|
|
499
627
|
},
|
|
500
628
|
},
|