@ynhcj/xiaoyi-channel 0.0.214-beta → 0.0.216-beta
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/src/bot.js +35 -126
- package/dist/src/monitor.js +8 -42
- package/dist/src/reply-dispatcher.js +1 -0
- package/package.json +1 -1
package/dist/src/bot.js
CHANGED
|
@@ -7,7 +7,6 @@ import { downloadFilesFromParts } from "./file-download.js";
|
|
|
7
7
|
import { resolveXYConfig } from "./config.js";
|
|
8
8
|
import { sendStatusUpdate, sendClearContextResponse, sendTasksCancelResponse, sendA2AResponse } from "./formatter.js";
|
|
9
9
|
import { appendSelfEvolutionKeywordNudge, shouldNudgeForSelfEvolutionKeyword, } from "./self-evolution-keyword.js";
|
|
10
|
-
import { queueAgentHarnessMessage } from "openclaw/plugin-sdk/agent-harness-runtime";
|
|
11
10
|
import { runWithSessionContext } from "./tools/session-manager.js";
|
|
12
11
|
import { configManager } from "./utils/config-manager.js";
|
|
13
12
|
import { addPushId } from "./utils/pushid-manager.js";
|
|
@@ -242,18 +241,20 @@ export async function handleXYMessage(params) {
|
|
|
242
241
|
log.error(`[BOT] Failed to patch session model override:`, patchErr);
|
|
243
242
|
}
|
|
244
243
|
}
|
|
245
|
-
// 🔑
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
244
|
+
// 🔑 发送初始状态更新(仅首条消息,steer 不重复发送)
|
|
245
|
+
if (!isUpdate) {
|
|
246
|
+
log.log(`[BOT] Sending initial status update`);
|
|
247
|
+
void sendStatusUpdate({
|
|
248
|
+
config,
|
|
249
|
+
sessionId: parsed.sessionId,
|
|
250
|
+
taskId: parsed.taskId,
|
|
251
|
+
messageId: parsed.messageId,
|
|
252
|
+
text: "任务正在处理中,请稍候~",
|
|
253
|
+
state: "working",
|
|
254
|
+
}).catch((err) => {
|
|
255
|
+
log.error(`Failed to send initial status update:`, err);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
257
258
|
}
|
|
258
259
|
// Extract text and files from parts
|
|
259
260
|
const text = extractTextFromParts(parsed.parts);
|
|
@@ -278,46 +279,26 @@ export async function handleXYMessage(params) {
|
|
|
278
279
|
log.error(`[SELF_EVOLUTION] Failed to append inline keyword nudge: ${String(selfEvolutionError)}`);
|
|
279
280
|
}
|
|
280
281
|
}
|
|
281
|
-
//
|
|
282
|
-
//
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
// 会被 globalDispatchInitGate 永久阻塞。
|
|
287
|
-
params.onInitComplete?.();
|
|
288
|
-
// Steer 也支持文件 —— 提取并下载,附带到 mediaPayload
|
|
289
|
-
const steerFileParts = extractFileParts(parsed.parts);
|
|
290
|
-
const steerDownloadedFiles = await downloadFilesFromParts(steerFileParts);
|
|
291
|
-
const steerMediaPayload = buildXYMediaPayload(steerDownloadedFiles);
|
|
292
|
-
if (steerFileParts.length > 0) {
|
|
293
|
-
log.log(`[BOT] Steer message with ${steerFileParts.length} file(s), enqueuing to streaming-signal queue`);
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
log.log(`[BOT] Steer message — enqueuing to streaming-signal queue`);
|
|
297
|
-
}
|
|
298
|
-
await enqueueSteer({
|
|
299
|
-
sessionId: parsed.sessionId,
|
|
300
|
-
sessionKey: route.sessionKey,
|
|
301
|
-
steerText: textForAgent, // 原始文本,不带 /steer 前缀
|
|
302
|
-
mediaPayload: steerMediaPayload,
|
|
303
|
-
cfg,
|
|
304
|
-
runtime,
|
|
305
|
-
parsed,
|
|
306
|
-
route,
|
|
307
|
-
deviceType,
|
|
308
|
-
});
|
|
309
|
-
log.log(`[BOT] Steer queue completed`);
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
// ── First message (non-steer) path below ──────────────────────
|
|
313
|
-
// File download — only for real user messages, steer injections have no files
|
|
282
|
+
// ── Build message and dispatch via auto-reply pipeline ──────────
|
|
283
|
+
// OpenClaw's auto-reply pipeline (src/auto-reply/reply/agent-runner.ts)
|
|
284
|
+
// detects active runs and handles steer injection natively — the channel
|
|
285
|
+
// does not need its own steer detection or polling logic.
|
|
286
|
+
// File download
|
|
314
287
|
let mediaPayload = {};
|
|
315
|
-
if (!skipReg) {
|
|
288
|
+
if (!skipReg || isUpdate) {
|
|
316
289
|
const fileParts = extractFileParts(parsed.parts);
|
|
317
290
|
const downloadedFiles = await downloadFilesFromParts(fileParts);
|
|
318
291
|
log.log(`[BOT] Downloaded ${downloadedFiles.length} file(s)`);
|
|
319
292
|
mediaPayload = buildXYMediaPayload(downloadedFiles);
|
|
320
293
|
}
|
|
294
|
+
// 🔑 对于 steer 消息,将文件路径附加到消息文本中。
|
|
295
|
+
// auto-reply 管道的 steer 注入只携带 prompt 文本(followupRun.prompt),
|
|
296
|
+
// 不携带 mediaPayload,所以模型需要以文本形式看到附件路径。
|
|
297
|
+
if (isUpdate && mediaPayload.MediaPaths?.length) {
|
|
298
|
+
const fileHint = `\n【用户上传附件】:${JSON.stringify(mediaPayload.MediaPaths)}`;
|
|
299
|
+
textForAgent = `${textForAgent}${fileHint}`;
|
|
300
|
+
log.log(`[BOT] Steer: appended file paths to text`);
|
|
301
|
+
}
|
|
321
302
|
// Resolve envelope format options (following feishu pattern)
|
|
322
303
|
const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(cfg);
|
|
323
304
|
// Build message body with speaker prefix (following feishu pattern)
|
|
@@ -358,8 +339,9 @@ export async function handleXYMessage(params) {
|
|
|
358
339
|
ReplyToBody: undefined, // A2A protocol doesn't support reply/quote
|
|
359
340
|
...mediaPayload,
|
|
360
341
|
});
|
|
361
|
-
// 🔑
|
|
362
|
-
|
|
342
|
+
// 🔑 For steer messages, pre-set steered=true so the dispatcher skips final
|
|
343
|
+
// response and cleanup — the first message's dispatcher handles those.
|
|
344
|
+
const steerState = { steered: isUpdate };
|
|
363
345
|
// 🔑 创建dispatcher
|
|
364
346
|
log.log(`[BOT-DISPATCHER] Creating reply dispatcher`);
|
|
365
347
|
// Cleanup: 必须在 onIdle 内部执行(参见 reply-dispatcher.ts 中 onIdleComplete 的注释)
|
|
@@ -384,8 +366,9 @@ export async function handleXYMessage(params) {
|
|
|
384
366
|
});
|
|
385
367
|
// 🔑 注册 dispatcher 的 fallback taskId 更新函数,供 steer 路径调用
|
|
386
368
|
dispatcherUpdaters.set(parsed.sessionId, updateFallbackTaskId);
|
|
387
|
-
// Steer
|
|
388
|
-
|
|
369
|
+
// Steer messages don't need a status interval — the first message's
|
|
370
|
+
// dispatcher already has one running.
|
|
371
|
+
if (!isUpdate) {
|
|
389
372
|
startStatusInterval();
|
|
390
373
|
}
|
|
391
374
|
// Build session context for AsyncLocalStorage
|
|
@@ -492,85 +475,11 @@ function buildXYMediaPayload(mediaList) {
|
|
|
492
475
|
};
|
|
493
476
|
}
|
|
494
477
|
// ─────────────────────────────────────────────────────────────
|
|
495
|
-
//
|
|
478
|
+
// Dispatcher updaters (cross-chain taskId bridging)
|
|
496
479
|
// ─────────────────────────────────────────────────────────────
|
|
497
480
|
// Use globalThis to survive module deduplication — provider.ts may load a
|
|
498
481
|
// different copy of bot.ts, so a plain module-level Map would be two objects.
|
|
499
482
|
const _g = globalThis;
|
|
500
|
-
if (!_g.__xySteerQueues)
|
|
501
|
-
_g.__xySteerQueues = new Map();
|
|
502
483
|
if (!_g.__xyDispatcherUpdaters)
|
|
503
484
|
_g.__xyDispatcherUpdaters = new Map();
|
|
504
|
-
const steerQueues = _g.__xySteerQueues;
|
|
505
485
|
const dispatcherUpdaters = _g.__xyDispatcherUpdaters;
|
|
506
|
-
/**
|
|
507
|
-
* 将 steer 消息放入 per-session 串行队列。
|
|
508
|
-
* 通过指数退避轮询注入(queueAgentHarnessMessage),直到 Pi agent 接受消息或 run 结束。
|
|
509
|
-
* 多个 steer 按到达顺序串行处理。
|
|
510
|
-
*/
|
|
511
|
-
function enqueueSteer(params) {
|
|
512
|
-
const { sessionId } = params;
|
|
513
|
-
const log = logger.withContext(sessionId, params.parsed.taskId);
|
|
514
|
-
// 取出当前队列尾部(或 undefined),然后链上新的 Promise
|
|
515
|
-
const prev = steerQueues.get(sessionId);
|
|
516
|
-
const next = (prev ?? Promise.resolve()).then(() => dispatchSteerWhenReady(params));
|
|
517
|
-
steerQueues.set(sessionId, next);
|
|
518
|
-
// 链条结束后清理
|
|
519
|
-
next.catch((err) => {
|
|
520
|
-
log.error(`[STEER-QUEUE] Steer chain failed: ${String(err)}`);
|
|
521
|
-
}).finally(() => {
|
|
522
|
-
if (steerQueues.get(sessionId) === next) {
|
|
523
|
-
steerQueues.delete(sessionId);
|
|
524
|
-
}
|
|
525
|
-
});
|
|
526
|
-
return next;
|
|
527
|
-
}
|
|
528
|
-
/**
|
|
529
|
-
* Poll the active embedded run with exponential backoff until the steer message
|
|
530
|
-
* is accepted or the run completes.
|
|
531
|
-
*
|
|
532
|
-
* Replaces the previous signal-based approach (notifyModelStreaming) which fired
|
|
533
|
-
* from wrapStreamFn too early — before the Pi agent had set state.isStreaming=true.
|
|
534
|
-
* queueAgentHarnessMessage internally checks isStreaming(), so we retry until the
|
|
535
|
-
* Pi agent is ready to accept messages.
|
|
536
|
-
*/
|
|
537
|
-
async function dispatchSteerWhenReady(params) {
|
|
538
|
-
const { sessionId, steerText } = params;
|
|
539
|
-
const log = logger.withContext(sessionId, params.parsed.taskId);
|
|
540
|
-
const mediaPaths = params.mediaPayload?.MediaPaths;
|
|
541
|
-
const fileHint = mediaPaths && mediaPaths.length > 0
|
|
542
|
-
? `\n【用户上传附件】:${JSON.stringify(mediaPaths)}`
|
|
543
|
-
: "";
|
|
544
|
-
const steerMessage = `${steerText}${fileHint}`;
|
|
545
|
-
log.log(`[STEER-QUEUE] Injecting steer message directly into active run`);
|
|
546
|
-
// Exponential backoff polling until:
|
|
547
|
-
// 1. Injection succeeds (queueAgentHarnessMessage returns true)
|
|
548
|
-
// 2. The original run has completed (hasActiveTask returns false)
|
|
549
|
-
// 3. Maximum total wait time exceeded
|
|
550
|
-
const MAX_WAIT_MS = 30_000;
|
|
551
|
-
const MAX_BACKOFF_MS = 4_000;
|
|
552
|
-
const BASE_DELAY_MS = 500;
|
|
553
|
-
const startedAt = Date.now();
|
|
554
|
-
let attempt = 0;
|
|
555
|
-
while (Date.now() - startedAt < MAX_WAIT_MS) {
|
|
556
|
-
// Termination: first message's run has already finished
|
|
557
|
-
if (!hasActiveTask(sessionId)) {
|
|
558
|
-
log.log(`[STEER-QUEUE] First message completed, skip steer`);
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
|
-
const injected = queueAgentHarnessMessage(sessionId, steerMessage, {
|
|
562
|
-
steeringMode: "all",
|
|
563
|
-
});
|
|
564
|
-
if (injected) {
|
|
565
|
-
log.log(`[STEER-QUEUE] Steer injected successfully on attempt ${attempt}`);
|
|
566
|
-
return;
|
|
567
|
-
}
|
|
568
|
-
// Exponential backoff: min(BASE_DELAY_MS * 2^attempt, MAX_BACKOFF_MS)
|
|
569
|
-
const delay = Math.min(BASE_DELAY_MS * Math.pow(2, attempt), MAX_BACKOFF_MS);
|
|
570
|
-
log.log(`[STEER-QUEUE] Attempt ${attempt}: run not accepting, retrying in ${delay}ms`);
|
|
571
|
-
await new Promise(r => setTimeout(r, delay));
|
|
572
|
-
attempt++;
|
|
573
|
-
}
|
|
574
|
-
// Max wait exceeded — model may still be in provider retry loop or unresponsive
|
|
575
|
-
log.log(`[STEER-QUEUE] Steer injection failed after ${MAX_WAIT_MS}ms timeout`);
|
|
576
|
-
}
|
package/dist/src/monitor.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { resolveXYConfig } from "./config.js";
|
|
2
2
|
import { getXYWebSocketManager, diagnoseAllManagers, cleanupOrphanConnections, removeXYWebSocketManager } from "./client.js";
|
|
3
3
|
import { handleXYMessage } from "./bot.js";
|
|
4
|
-
import {
|
|
5
|
-
import { hasActiveTask, getAllActiveTaskBindings } from "./task-manager.js";
|
|
4
|
+
import { getAllActiveTaskBindings } from "./task-manager.js";
|
|
6
5
|
import { sendA2AResponse } from "./formatter.js";
|
|
7
6
|
import { handleTriggerEvent } from "./trigger-handler.js";
|
|
8
7
|
import { handleSelfEvolutionEvent, handleSelfEvolutionStateGetEvent } from "./self-evolution-handler.js";
|
|
@@ -119,46 +118,13 @@ export async function monitorXYProvider(opts = {}) {
|
|
|
119
118
|
activeMessages.delete(messageKey);
|
|
120
119
|
}
|
|
121
120
|
};
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
activeMessages.delete(messageKey);
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
try {
|
|
134
|
-
const parsed = parseA2AMessage(message);
|
|
135
|
-
const steerMode = cfg.messages?.queue?.mode === "steer";
|
|
136
|
-
const hasActiveRun = hasActiveTask(parsed.sessionId);
|
|
137
|
-
if (steerMode && hasActiveRun) {
|
|
138
|
-
// Steer模式且有活跃任务:不入队列,直接并发执行
|
|
139
|
-
logger.log(`[MONITOR-HANDLER] STEER MODE: Executing concurrently for messageKey=${messageKey}`);
|
|
140
|
-
void task().catch((err) => {
|
|
141
|
-
logger.error(`XY gateway: concurrent steer task failed for ${messageKey}: ${String(err)}`);
|
|
142
|
-
activeMessages.delete(messageKey);
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
// 正常模式:入队列串行执行
|
|
147
|
-
void enqueue(sessionId, task).catch((err) => {
|
|
148
|
-
logger.error(`XY gateway: queue processing failed: ${String(err)}`);
|
|
149
|
-
activeMessages.delete(messageKey);
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
catch (parseErr) {
|
|
154
|
-
// 解析失败,回退到正常队列模式
|
|
155
|
-
logger.error(`[MONITOR-HANDLER] Failed to parse message for steer detection: ${String(parseErr)}`);
|
|
156
|
-
void enqueue(sessionId, task).catch((err) => {
|
|
157
|
-
logger.error(`XY gateway: queue processing failed: ${String(err)}`);
|
|
158
|
-
activeMessages.delete(messageKey);
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
}
|
|
121
|
+
// All messages go through the per-session serial queue.
|
|
122
|
+
// Steer detection and injection is handled by OpenClaw's auto-reply pipeline
|
|
123
|
+
// (src/auto-reply/reply/agent-runner.ts), not by the channel itself.
|
|
124
|
+
void enqueue(sessionId, task).catch((err) => {
|
|
125
|
+
logger.error(`XY gateway: queue processing failed: ${String(err)}`);
|
|
126
|
+
activeMessages.delete(messageKey);
|
|
127
|
+
});
|
|
162
128
|
};
|
|
163
129
|
const connectedHandler = (serverId) => {
|
|
164
130
|
if (!loggedServers.has(serverId)) {
|
|
@@ -230,6 +230,7 @@ export function createXYReplyDispatcher(params) {
|
|
|
230
230
|
if (steerState.steered && !hasSentResponse) {
|
|
231
231
|
scopedLog().log(`[ON-IDLE] Steered dispatch, no response generated, skipping`);
|
|
232
232
|
stopStatusInterval();
|
|
233
|
+
await onIdleComplete?.();
|
|
233
234
|
return;
|
|
234
235
|
}
|
|
235
236
|
// 🔑 用 try/finally 确保 cleanup 在 onIdle 的 async 工作全部完成后才执行。
|