work-rhythm 0.1.8 → 0.2.3
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/.agents/notes/AGENTS.md +7 -1
- package/.agents/notes/decisions/2026-12-01-interjections-batch-into-one-answer-and-interrupt-reruns.md +33 -0
- package/.agents/notes/decisions/2026-12-01-long-runs-are-bounded-by-silence-not-wall-clock.md +35 -0
- package/.agents/notes/decisions/2026-12-01-router-and-worker-pass-content-in-handoff-docs.md +41 -0
- package/.agents/notes/pitfalls/2026-12-01-replace-string-dollar-quote-expands.md +16 -0
- package/.agents/skills/work-rhythm/SKILL.md +14 -12
- package/README.md +7 -4
- package/collectors/local.env.example +10 -1
- package/docs/agents.md +28 -11
- package/docs/channels.md +9 -6
- package/docs/router-worker.md +241 -0
- package/docs/scheduled-tasks.md +4 -4
- package/package.json +1 -1
- package/scripts/cli.js +4 -3
- package/scripts/cmd/agent.js +78 -40
- package/scripts/cmd/check.js +4 -0
- package/scripts/cmd/listen.js +18 -4
- package/scripts/cmd/rhythm.js +6 -2
- package/scripts/cmd/trigger.js +143 -0
- package/scripts/lib/agent.js +377 -30
- package/scripts/lib/bot.js +140 -35
- package/scripts/lib/engine.js +523 -0
- package/scripts/lib/handoff.js +165 -0
- package/scripts/lib/markdown.js +328 -38
- package/scripts/lib/rhythm.js +369 -31
- package/scripts/lib/router.js +161 -0
- package/scripts/lib/run.js +43 -0
- package/scripts/lib/view.js +583 -0
- package/scripts/lib/worker.js +97 -0
- package/scripts/test/agent.test.js +17 -9
- package/scripts/test/check.test.js +7 -2
- package/scripts/test/harness.js +13 -1
- package/scripts/test/listen.test.js +2 -2
- package/scripts/test/rhythm.test.js +28 -6
- package/scripts/test/router-helper.js +80 -0
- package/scripts/test/router.test.js +509 -0
- package/scripts/test/stubs.js +82 -12
- package/scripts/test/timeout.test.js +99 -0
- package/templates/00-config/profile.example.yaml +7 -0
- package/templates/AGENTS.md +3 -1
- package/templates/README.md +4 -1
package/scripts/lib/bot.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
'use strict';
|
|
9
9
|
const fs = require('node:fs');
|
|
10
10
|
const path = require('node:path');
|
|
11
|
-
const { EXIT, Fail, fmtStamp } = require('./run');
|
|
11
|
+
const { EXIT, Fail, fmtStamp, seconds, humanDuration, progressPings } = require('./run');
|
|
12
12
|
const { logger } = require('./log');
|
|
13
13
|
const { notifyChannel, pushText } = require('./notify');
|
|
14
14
|
const { findWebhook, mask } = require('./robot');
|
|
@@ -19,6 +19,8 @@ const HEARTBEAT_MS = 15000;
|
|
|
19
19
|
const STALE_MS = 45000;
|
|
20
20
|
const RECONNECT_DELAYS_MS = [1000, 2000, 5000, 10000, 30000, 60000];
|
|
21
21
|
const ACK_TEXT = '收到,正在处理,稍等。';
|
|
22
|
+
// 上一句还没答完又来一句:先回这句,告诉他会和上一句一起考虑
|
|
23
|
+
const ACK_MORE_TEXT = '收到,上一句还在答,这句会和它一起考虑。';
|
|
22
24
|
const DEFAULT_PREAMBLE = '(这句来自云之家:你的回复会原样以纯文本私聊发给我。直接回答,不用表格,尽量简短;一件事要查很久就先说大概要多久。)';
|
|
23
25
|
|
|
24
26
|
const positiveInt = (value, fallback) => {
|
|
@@ -147,9 +149,12 @@ const reconnectDelay = (attempts) => RECONNECT_DELAYS_MS[Math.min(Math.max(attem
|
|
|
147
149
|
|
|
148
150
|
// 建一个值守对象。env 是设置(环境变量,上面盖一层 local.env),配置都在里面:
|
|
149
151
|
// YZJ_BOT_AGENT 让哪个 agent 回复(zcode、dsh,或一条自己的命令)、YZJ_BOT_AGENT_MODE(plan 只读 / edit 可写,默认 plan)、
|
|
150
|
-
// YZJ_MY_OPEN_ID 只认我本人、YZJ_BOT_TIMEOUT
|
|
152
|
+
// YZJ_MY_OPEN_ID 只认我本人、YZJ_BOT_TIMEOUT 一次最多等几秒(硬上限,默认 900)、YZJ_BOT_IDLE 多久没输出算卡住(默认 180,0 不查)、
|
|
153
|
+
// YZJ_BOT_PROGRESS 跑多久先说一声还在做(默认 90 秒,0 不说)、YZJ_BOT_REPLY_MAX 回复最多几个字、
|
|
151
154
|
// YZJ_BOT_ACK=0 关掉「收到,正在处理」那句、YZJ_BOT_PREAMBLE 换掉或清空随每句话带给 agent 的提示
|
|
152
|
-
|
|
155
|
+
// 路由开着时(profile 的 rhythm.router,本地服务传进来):值守只管收——本人校验、去重、/new——收到的话变成触发交给
|
|
156
|
+
// onMessage(engine.js),由路由判断再派给执行者,回复也由引擎发。这时配置查的是路由的 agent(doctorAgent),/new 调 resetSession
|
|
157
|
+
function createBot({ root, env, onMessage = null, resetSession = null, doctorAgent = null }) {
|
|
153
158
|
const STATE_DIR = path.join(root, '01-inbox', '_bot');
|
|
154
159
|
const LOG_FILE = path.join(root, '01-inbox', '_bot.log');
|
|
155
160
|
const SESSION_FILE = path.join(STATE_DIR, 'session-id');
|
|
@@ -158,7 +163,12 @@ function createBot({ root, env }) {
|
|
|
158
163
|
const OWNER = (env.YZJ_MY_OPEN_ID || '').trim();
|
|
159
164
|
const AGENT = (env.YZJ_BOT_AGENT || '').trim();
|
|
160
165
|
const AGENT_MODE = (env.YZJ_BOT_AGENT_MODE || 'plan').trim();
|
|
161
|
-
const TIMEOUT_S = positiveInt(env.YZJ_BOT_TIMEOUT,
|
|
166
|
+
const TIMEOUT_S = positiveInt(env.YZJ_BOT_TIMEOUT, 900);
|
|
167
|
+
// 静默闸:一直在吐输出的 agent(dsh、kimi)多久没动静才算卡住;0 关掉,只看硬上限
|
|
168
|
+
const IDLE_S = seconds(env.YZJ_BOT_IDLE, 180);
|
|
169
|
+
// 跑得久先说一声,省得我对着屏幕猜它死没死。第一条 PROGRESS_S 秒后,之后每条隔 2 倍
|
|
170
|
+
const PROGRESS_S = seconds(env.YZJ_BOT_PROGRESS, 90);
|
|
171
|
+
const PROGRESS_MAX = positiveInt(env.YZJ_BOT_PROGRESS_MAX, 3);
|
|
162
172
|
const REPLY_MAX = positiveInt(env.YZJ_BOT_REPLY_MAX, 2000);
|
|
163
173
|
// 收到就先回一句,agent 慢的时候我不用干等
|
|
164
174
|
const ACK = env.YZJ_BOT_ACK !== '0';
|
|
@@ -167,7 +177,7 @@ function createBot({ root, env }) {
|
|
|
167
177
|
const BOUND_SESSION = (env.YZJ_BOT_SESSION || '').trim();
|
|
168
178
|
const PREAMBLE = env.YZJ_BOT_PREAMBLE !== undefined ? env.YZJ_BOT_PREAMBLE : DEFAULT_PREAMBLE;
|
|
169
179
|
// 模型和思考强度(collectors/local.env 的 YZJ_BOT_MODEL / YZJ_BOT_EFFORT),只有支持的预置才认;不写就用 agent 自己的默认
|
|
170
|
-
const agent = createAgent({ root, agent: AGENT, mode: AGENT_MODE, timeoutS: TIMEOUT_S, preamble: PREAMBLE, log, env, model: (env.YZJ_BOT_MODEL || '').trim(), effort: (env.YZJ_BOT_EFFORT || '').trim() });
|
|
180
|
+
const agent = createAgent({ root, agent: AGENT, mode: AGENT_MODE, timeoutS: TIMEOUT_S, idleS: IDLE_S, preamble: PREAMBLE, log, env, model: (env.YZJ_BOT_MODEL || '').trim(), effort: (env.YZJ_BOT_EFFORT || '').trim() });
|
|
171
181
|
|
|
172
182
|
// ---------------------------------------------------------------- 配置
|
|
173
183
|
// 检查值守的配置和依赖:有问题一条一个字符串;没问题就是空数组。check 配了值守时也调它
|
|
@@ -176,7 +186,7 @@ function createBot({ root, env }) {
|
|
|
176
186
|
if (notifyChannel(root) !== 'yzj-robot') problems.push('值守属于 yzj-robot 渠道:preferences.notify 要设成 yzj-robot');
|
|
177
187
|
if (!findWebhook(env).webhook) problems.push(`钥匙串里没有 ${env.YZJ_ROBOT_KEYCHAIN_SERVICE || 'work-rhythm-yzj-robot-webhook'}(开通步骤见 docs/channels.md「云之家」)`);
|
|
178
188
|
if (!OWNER) problems.push('值守只处理我本人的消息:collectors/local.env 里要写 YZJ_MY_OPEN_ID');
|
|
179
|
-
problems.push(...agent.doctor());
|
|
189
|
+
problems.push(...(doctorAgent ? doctorAgent() : agent.doctor()));
|
|
180
190
|
return problems;
|
|
181
191
|
}
|
|
182
192
|
|
|
@@ -189,7 +199,10 @@ function createBot({ root, env }) {
|
|
|
189
199
|
if (found.problem) throw new Fail(found.code, found.problem);
|
|
190
200
|
if (needAgent) {
|
|
191
201
|
if (!OWNER) throw new Fail(EXIT.NOT_CONFIGURED, '取不到我的 openId:在 collectors/local.env 里写 YZJ_MY_OPEN_ID,值守只处理我本人的消息');
|
|
192
|
-
if (
|
|
202
|
+
if (doctorAgent) {
|
|
203
|
+
const problems = doctorAgent();
|
|
204
|
+
if (problems.length) throw new Fail(EXIT.NOT_CONFIGURED, `路由用的 agent 配得不对:${problems.join(';')}`);
|
|
205
|
+
} else if (!AGENT) throw new Fail(EXIT.NOT_CONFIGURED, '没绑 agent,值守开不了:先 work-rhythm agent detect 看本机有哪些,再 work-rhythm agent bind <名字>(写进 collectors/local.env 的 YZJ_BOT_AGENT);也可以手写它,值是预置的名字或一条把回复打到标准输出的命令');
|
|
193
206
|
}
|
|
194
207
|
return deriveWsUrl(found.webhook);
|
|
195
208
|
}
|
|
@@ -213,7 +226,12 @@ function createBot({ root, env }) {
|
|
|
213
226
|
fs.mkdirSync(STATE_DIR, { recursive: true });
|
|
214
227
|
fs.writeFileSync(SESSION_FILE, `${id}\n`);
|
|
215
228
|
};
|
|
229
|
+
// 丢掉专属会话。路由开着时专属会话归路由管(01-inbox/_bot/session-id),交还给它清
|
|
216
230
|
const clearSession = () => {
|
|
231
|
+
if (resetSession) {
|
|
232
|
+
resetSession();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
217
235
|
try {
|
|
218
236
|
fs.unlinkSync(SESSION_FILE);
|
|
219
237
|
} catch {
|
|
@@ -266,22 +284,6 @@ function createBot({ root, env }) {
|
|
|
266
284
|
clearSession();
|
|
267
285
|
};
|
|
268
286
|
|
|
269
|
-
// 一条消息 → 要回复的文字。agent 失败也回一句,不让我干等
|
|
270
|
-
function answer(message) {
|
|
271
|
-
const text = String(message.content || '').trim();
|
|
272
|
-
if (!text) return '收到一条空消息。';
|
|
273
|
-
if (/^\/new$/i.test(text)) {
|
|
274
|
-
clearSession();
|
|
275
|
-
if (!BOUND_SESSION) return '已开新会话。';
|
|
276
|
-
forgetBoundLost();
|
|
277
|
-
return '已丢掉值守自己开的会话,下一句从绑定的那个会话接着走。';
|
|
278
|
-
}
|
|
279
|
-
const used = startSession();
|
|
280
|
-
const r = agent.run(text, { session: used, onSessionLost: () => sessionLost(used), stateDir: STATE_DIR });
|
|
281
|
-
if (r.sessionId) writeSession(r.sessionId);
|
|
282
|
-
return truncate(r.failed ? `没处理成功:${r.failed}` : r.reply);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
287
|
// 回复经推送发出去。发不出去记一笔日志,不影响值守
|
|
286
288
|
async function sendReply(reply) {
|
|
287
289
|
const r = await pushText(root, reply, { env });
|
|
@@ -290,6 +292,80 @@ function createBot({ root, env }) {
|
|
|
290
292
|
return false;
|
|
291
293
|
}
|
|
292
294
|
|
|
295
|
+
// ---------------------------------------------------------------- 排队答(值守用)
|
|
296
|
+
// 一条一条答,但不卡住事件循环:agent 用异步的 start(),等的期间长连接、网页、定时看表都照常。
|
|
297
|
+
// 上一句还在答的时候又来了几句:先各回一句「收到,会和上一句一起考虑」,等上一句答完,把排着的几句合成一次运行——
|
|
298
|
+
// 它们都是在我回答之前插进来的,一起看(promptOf)。每句的 Promise 在它那一批答完时给出 { text, speak }
|
|
299
|
+
const promptOf = (items) => {
|
|
300
|
+
const [first, ...rest] = items;
|
|
301
|
+
if (!rest.length) return first.text;
|
|
302
|
+
return `${first.text}\n\n(你回答之前,用户又插了下面这些话,一起看完再答:\n${rest.map((x) => `- ${x.text}`).join('\n')}\n)`;
|
|
303
|
+
};
|
|
304
|
+
const waiting = []; // 排着没答的:{ msgId, text, resolve }
|
|
305
|
+
let running = null; // 正在跑的那次:{ handle }
|
|
306
|
+
let halted = false; // 服务在停:答了一半的不再发出去,排着的也不再答
|
|
307
|
+
const idleWaiters = [];
|
|
308
|
+
// 答一批。返回 { text, speak }:speak 为 false 时这句话不发(服务在停)
|
|
309
|
+
async function runBatch(items) {
|
|
310
|
+
const used = startSession();
|
|
311
|
+
const started = Date.now();
|
|
312
|
+
const h = agent.start(promptOf(items), { session: used, onSessionLost: () => sessionLost(used), stateDir: STATE_DIR });
|
|
313
|
+
running = { handle: h };
|
|
314
|
+
// 跑得久先说一声:第一条在 PROGRESS_S 秒后,之后每条隔 2 倍,最多 PROGRESS_MAX 条
|
|
315
|
+
const pings = progressPings({ firstS: PROGRESS_S, everyS: PROGRESS_S * 2, max: PROGRESS_MAX, say: () => sendReply(`还在做(已经 ${humanDuration((Date.now() - started) / 1000)}),做完就发给你。`) });
|
|
316
|
+
let r;
|
|
317
|
+
try {
|
|
318
|
+
r = await h.done;
|
|
319
|
+
} finally {
|
|
320
|
+
pings.stop();
|
|
321
|
+
running = null;
|
|
322
|
+
}
|
|
323
|
+
if (halted) return { text: '', speak: false };
|
|
324
|
+
if (r.sessionId) writeSession(r.sessionId);
|
|
325
|
+
// 被时限停掉的:已经说出口的话先发给我,别只剩一句「没处理成功」
|
|
326
|
+
if (r.failed && r.partial) return { text: `时间到了,先给你说到这:\n\n${r.partial}`, speak: true };
|
|
327
|
+
return { text: r.failed ? `没处理成功:${r.failed}` : truncate(r.reply), speak: true };
|
|
328
|
+
}
|
|
329
|
+
async function drain() {
|
|
330
|
+
if (running) return;
|
|
331
|
+
while (waiting.length) {
|
|
332
|
+
const batch = waiting.splice(0);
|
|
333
|
+
let out;
|
|
334
|
+
try {
|
|
335
|
+
out = await runBatch(batch);
|
|
336
|
+
} catch (error) {
|
|
337
|
+
out = { text: `没处理成功:${error && error.message ? error.message : String(error)}`, speak: !halted };
|
|
338
|
+
}
|
|
339
|
+
// 一批只答一次:合成的那句由这一批的第一条发出去,后面的不再各发一遍
|
|
340
|
+
batch.forEach((item, i) => item.resolve({ ...out, speak: out.speak && i === 0 }));
|
|
341
|
+
}
|
|
342
|
+
for (const w of idleWaiters.splice(0)) w();
|
|
343
|
+
}
|
|
344
|
+
// 排进队列。返回的 Promise 在它那一批答完时给出 { text, speak }
|
|
345
|
+
function enqueue(msgId, text) {
|
|
346
|
+
return new Promise((resolve) => {
|
|
347
|
+
waiting.push({ msgId, text, resolve });
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
// 等到排着的都答完。timeoutMs 为 0 时一直等
|
|
351
|
+
function idle(timeoutMs = 0) {
|
|
352
|
+
if (!running && !waiting.length) return Promise.resolve(true);
|
|
353
|
+
return new Promise((resolve) => {
|
|
354
|
+
let timer = null;
|
|
355
|
+
idleWaiters.push(() => {
|
|
356
|
+
if (timer) clearTimeout(timer);
|
|
357
|
+
resolve(true);
|
|
358
|
+
});
|
|
359
|
+
if (timeoutMs) timer = setTimeout(() => resolve(false), timeoutMs);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
// 停掉正在答的那次(服务要停时):答了一半的不发,排着的也不再答
|
|
363
|
+
function abort(reason = '服务停止') {
|
|
364
|
+
halted = true;
|
|
365
|
+
if (running) running.handle.kill(reason);
|
|
366
|
+
for (const item of waiting.splice(0)) item.resolve({ text: '', speak: false });
|
|
367
|
+
}
|
|
368
|
+
|
|
293
369
|
// 收到一帧之后做的事。send 为 false 时只算出回复、不发
|
|
294
370
|
async function processFrame(raw, send = true) {
|
|
295
371
|
const c = classify(raw);
|
|
@@ -302,14 +378,45 @@ function createBot({ root, env }) {
|
|
|
302
378
|
return { result: `忽略 msgId=${m.msgId}(不是本人)`, code: 0 };
|
|
303
379
|
}
|
|
304
380
|
const started = Date.now();
|
|
305
|
-
const
|
|
381
|
+
const text = String(m.content || '').trim();
|
|
382
|
+
const immediate = /^\/new$/i.test(text);
|
|
306
383
|
log('msg 接受', `msgId=${m.msgId} groupType=${m.groupType}`);
|
|
384
|
+
// 路由开着:/new 丢掉路由的专属会话;别的话变成一条触发交给引擎,回复由它发(「收到」也在那边)
|
|
385
|
+
if (onMessage) {
|
|
386
|
+
if (immediate) {
|
|
387
|
+
if (resetSession) resetSession();
|
|
388
|
+
log('msg 完成', `msgId=${m.msgId} /new`);
|
|
389
|
+
return { result: '已丢掉路由自己的专属会话,下一句从绑定的那个会话接着走。', code: 0 };
|
|
390
|
+
}
|
|
391
|
+
const t = onMessage({ msgId: m.msgId, content: text, time: m.time, groupType: m.groupType });
|
|
392
|
+
log('msg 交给路由', `msgId=${m.msgId} ${t ? t.id : '重复的'}`);
|
|
393
|
+
return { result: `msgId=${m.msgId} 已交给路由(${t ? t.id : '重复的'})`, code: 0 };
|
|
394
|
+
}
|
|
395
|
+
let reply = '';
|
|
396
|
+
let speak = true;
|
|
307
397
|
let sent = null;
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
398
|
+
// /new 丢掉专属会话:马上回一句,不排队
|
|
399
|
+
if (immediate) {
|
|
400
|
+
clearSession();
|
|
401
|
+
if (BOUND_SESSION) forgetBoundLost();
|
|
402
|
+
reply = BOUND_SESSION ? '已丢掉值守自己开的会话,下一句从绑定的那个会话接着走。' : '已开新会话。';
|
|
403
|
+
} else if (!text) {
|
|
404
|
+
reply = '收到一条空消息。';
|
|
405
|
+
} else {
|
|
406
|
+
// 先排进去:同一刻到来的后一句看得到前面还排着
|
|
407
|
+
const answered = enqueue(m.msgId, text);
|
|
408
|
+
if (send) {
|
|
409
|
+
// 收到就回一句「收到」,agent 慢的话我不用干等;上一句还在答、或者前面还排着,就说明会和它一起考虑。
|
|
410
|
+
// 回完再开跑,顺序才是「收到」在前、回复在后
|
|
411
|
+
const first = waiting.length === 1 && !running;
|
|
412
|
+
if (ACK) await sendReply(first ? ACK_TEXT : ACK_MORE_TEXT);
|
|
413
|
+
}
|
|
414
|
+
drain();
|
|
415
|
+
const a = await answered;
|
|
416
|
+
reply = a.text;
|
|
417
|
+
speak = a.speak;
|
|
312
418
|
}
|
|
419
|
+
if (send && speak) sent = await sendReply(reply);
|
|
313
420
|
log('msg 完成', `msgId=${m.msgId} ${Math.round((Date.now() - started) / 1000)}s${sent === null ? '' : ` sent=${sent}`}`);
|
|
314
421
|
return { result: `处理 msgId=${m.msgId}${sent === false ? ',但回复没发出去' : ''}`, code: 0, sent };
|
|
315
422
|
}
|
|
@@ -337,7 +444,7 @@ function createBot({ root, env }) {
|
|
|
337
444
|
async function run({ signals = true } = {}) {
|
|
338
445
|
const url = requireConfig(true);
|
|
339
446
|
needWebSocket();
|
|
340
|
-
log('值守开始', `pid=${process.pid} agent=${agent.kind !== 'custom' ? `${AGENT} mode=${AGENT_MODE}` : '自定义命令'} 超时=${TIMEOUT_S}s 会话=${BOUND_SESSION ? `绑定 ${BOUND_SESSION}` : '值守自己的专属会话'}`);
|
|
447
|
+
log('值守开始', `pid=${process.pid} ${onMessage ? '回复交给路由(profile 的 rhythm.router)' : `agent=${agent.kind !== 'custom' ? `${AGENT} mode=${AGENT_MODE}` : '自定义命令'} 超时=${TIMEOUT_S}s`} 会话=${BOUND_SESSION ? `绑定 ${BOUND_SESSION}` : '值守自己的专属会话'}`);
|
|
341
448
|
let stopping = false;
|
|
342
449
|
let wake = null;
|
|
343
450
|
const stopped = new Promise((resolve) => {
|
|
@@ -346,12 +453,12 @@ function createBot({ root, env }) {
|
|
|
346
453
|
// 等待的时候也能被停下的信号叫醒
|
|
347
454
|
const pause = (ms) => Promise.race([sleep(ms), stopped]);
|
|
348
455
|
let current = null;
|
|
349
|
-
let chain = Promise.resolve();
|
|
350
456
|
const stop = (signal) => {
|
|
351
457
|
if (stopping) return;
|
|
352
458
|
stopping = true;
|
|
353
459
|
log(`收到 ${signal},准备退出`);
|
|
354
460
|
if (current && current.ws.readyState === WebSocket.OPEN) current.ws.close(1000, 'bye');
|
|
461
|
+
abort(`收到 ${signal}`);
|
|
355
462
|
wake();
|
|
356
463
|
};
|
|
357
464
|
stopFn = stop;
|
|
@@ -364,10 +471,8 @@ function createBot({ root, env }) {
|
|
|
364
471
|
const conn = await openSocket(url, (raw, me) => {
|
|
365
472
|
const c = classify(raw);
|
|
366
473
|
if (c.kind === 'control' && c.ack && me.ws.readyState === WebSocket.OPEN) me.ws.send(c.ack);
|
|
367
|
-
//
|
|
368
|
-
if (c.kind === 'dispatch')
|
|
369
|
-
chain = chain.then(() => (stopping ? undefined : processFrame(raw, true)));
|
|
370
|
-
}
|
|
474
|
+
// 一到就处理:去重、回「收到」在这里,排队交给 enqueue / drain;值守在停了就不再接
|
|
475
|
+
if (c.kind === 'dispatch' && !stopping) processFrame(raw, true).catch((error) => log('msg 出错', error && error.message ? error.message : String(error)));
|
|
371
476
|
}, log);
|
|
372
477
|
if (!conn) {
|
|
373
478
|
attempts += 1;
|
|
@@ -380,7 +485,7 @@ function createBot({ root, env }) {
|
|
|
380
485
|
current = null;
|
|
381
486
|
if (!stopping) await pause(1000);
|
|
382
487
|
}
|
|
383
|
-
await
|
|
488
|
+
await idle(2000);
|
|
384
489
|
log('值守结束');
|
|
385
490
|
return EXIT.OK;
|
|
386
491
|
}
|