@fabianactor/dsh-lil-whale-group 1.8.12 → 1.8.15
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/lib/index.js +48 -22
- package/lib/text-clean.js +89 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
// 按 cwd 会叫错人。cwd 只作退路,并在 plugin.log 留一行痕迹。
|
|
10
10
|
|
|
11
11
|
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
12
|
+
import { printableText } from './text-clean.js'
|
|
12
13
|
import { join, dirname, basename } from 'node:path'
|
|
13
14
|
import { homedir } from 'node:os'
|
|
14
15
|
import { pathToFileURL, fileURLToPath } from 'node:url'
|
|
@@ -1342,6 +1343,9 @@ function wakesFileOf(member) {
|
|
|
1342
1343
|
* 巡守以为她醒着,只递了一句话,没人读。
|
|
1343
1344
|
*/
|
|
1344
1345
|
const WAKE_STALE_SEC = 90
|
|
1346
|
+
// 一次"计划被判定到点"的尝试,最多认领 30 分钟。超过它就不再是"这次叫醒"——
|
|
1347
|
+
// 2026-11-17 那条僵尸 woke 行就是因为它被无限期认领(旧 wid 反复出现、lag 越涨越大)。
|
|
1348
|
+
const WAKE_ATTEMPT_TTL_SEC = 30 * 60
|
|
1345
1349
|
|
|
1346
1350
|
function presenceAgeOf(member) {
|
|
1347
1351
|
try {
|
|
@@ -1669,7 +1673,7 @@ function sayPrivate(from, to, text, image = null, images = null) {
|
|
|
1669
1673
|
mkdirSync(DM_DIR, { recursive: true })
|
|
1670
1674
|
const list = normalizeImageList(image, images)
|
|
1671
1675
|
const record = {
|
|
1672
|
-
ts: new Date().toISOString(), from, to, text, kind: 'dm', channel: 'dm',
|
|
1676
|
+
ts: new Date().toISOString(), from, to, text: printableText(text), kind: 'dm', channel: 'dm',
|
|
1673
1677
|
...(list.length === 0 ? {} : (list.length === 1 ? { image: list[0] } : { images: list })),
|
|
1674
1678
|
}
|
|
1675
1679
|
appendFileSync(dmFileOf(from, to), JSON.stringify(record) + '\n', 'utf8')
|
|
@@ -1690,7 +1694,7 @@ function say(from, to, text, channel = 'group', s = '', toSeat = '', image = nul
|
|
|
1690
1694
|
// 只追加的文件删不掉一行,所以空正文绝不能落盘。
|
|
1691
1695
|
// 现场(2026-11-11 16:23):一条没有 text 的记录进了 messages.jsonl,
|
|
1692
1696
|
// 在 群.md 里显示成 undefined —— 一个空对象,读起来却像一句话。
|
|
1693
|
-
const body = typeof text === 'string' ? text.trim() : ''
|
|
1697
|
+
const body = typeof text === 'string' ? printableText(text.trim()) : ''
|
|
1694
1698
|
// 一条消息至少要有一件东西:一段话或一张图。两个都空,仍然是"空消息"。
|
|
1695
1699
|
if (body === '' && list.length === 0) throw new Error('群消息不能没有内容(text 和 image 都是空的,拒绝写入)')
|
|
1696
1700
|
// 2026-11-12 20:20 改:以前 `to === 'fabian'` 也算私聊,于是"在群里对 Fabian 说"这条路不存在了
|
|
@@ -2952,6 +2956,16 @@ function deliverToSelf(ctx, self, text) {
|
|
|
2952
2956
|
}
|
|
2953
2957
|
}
|
|
2954
2958
|
|
|
2959
|
+
// 计划表里的点是"今天的 HH:MM";算出来比现在晚 12 小时以上,那它其实是昨天的点。
|
|
2960
|
+
function planDueMs(at) {
|
|
2961
|
+
const m = /^(\d{1,2}):(\d{2})$/.exec(String(at ?? ''))
|
|
2962
|
+
if (!m) return null
|
|
2963
|
+
const d = new Date()
|
|
2964
|
+
d.setHours(Number(m[1]), Number(m[2]), 0, 0)
|
|
2965
|
+
if (d.getTime() > Date.now() + 12 * 3600_000) d.setDate(d.getDate() - 1)
|
|
2966
|
+
return d.getTime()
|
|
2967
|
+
}
|
|
2968
|
+
|
|
2955
2969
|
function selfPlanTick(ctx) {
|
|
2956
2970
|
if (isMainProcess()) return
|
|
2957
2971
|
const self = ownMember()
|
|
@@ -2990,7 +3004,16 @@ function selfPlanTick(ctx) {
|
|
|
2990
3004
|
say(watcherKey(), self, planDeliveryPublicText(job))
|
|
2991
3005
|
watchmanLog('deliver', `reason=self-plan-public-fallback to=${self} plan=${item.id}`)
|
|
2992
3006
|
}
|
|
2993
|
-
|
|
3007
|
+
// 与巡守触发的账同形状(wid / lag / inserted 都要有):
|
|
3008
|
+
// 2026-11-17 第一条鱼:`trigger=self` 第一次出现时缺这三个字段 = 同一个词、两种账。
|
|
3009
|
+
const selfDueAt = planDueMs(item.at)
|
|
3010
|
+
const selfWid = `wl-${String(item.at ?? '').replace(':', '')}-${item.id}`
|
|
3011
|
+
watchmanLog(
|
|
3012
|
+
'woke',
|
|
3013
|
+
selfDueAt === null
|
|
3014
|
+
? `wid=${selfWid} trigger=self lag-unknown inserted=1 to=${self} plan=${item.id} at=${item.at}`
|
|
3015
|
+
: `wid=${selfWid} trigger=self lag=${Math.round((Date.now() - selfDueAt) / 1000)}s inserted=1 to=${self} plan=${item.id} at=${item.at}`,
|
|
3016
|
+
)
|
|
2994
3017
|
markWake(self, 'plan')
|
|
2995
3018
|
} catch (error) {
|
|
2996
3019
|
watchmanLog('error', `note=self-plan-deliver-failed to=${self} detail=${String(error?.message ?? error).replace(/\s+/g, ' ').slice(0, 120)} plan=${item.id}`)
|
|
@@ -3227,7 +3250,7 @@ async function intentTick(member, st, now) {
|
|
|
3227
3250
|
// lag = 发现比发生晚了多少(事情发生的时刻 → 递出去的时刻),不是"决定→插入"。
|
|
3228
3251
|
const happenedMs = Date.parse(String(s.happenedAt ?? s.firstTrueAt ?? ''))
|
|
3229
3252
|
const lagSec = Number.isFinite(happenedMs) ? Math.max(0, Math.round((now.getTime() - happenedMs) / 1000)) : null
|
|
3230
|
-
watchmanLog('woke', `wid=${wid} trigger=intent lag=${lagSec === null ? 'lag-unknown' : `${lagSec}s`} inserted=1`)
|
|
3253
|
+
watchmanLog('woke', `wid=${wid} trigger=intent lag=${lagSec === null ? 'lag-unknown' : `${lagSec}s`} inserted=1 to=${member.key}`)
|
|
3231
3254
|
markWake(member.key, 'intent')
|
|
3232
3255
|
} catch (error) {
|
|
3233
3256
|
watchmanLog('wake-skip', `wid=${wid} deny=intent-deliver-failed detail=${String(error?.message ?? error).replace(/\s+/g, ' ').slice(0, 100)}`)
|
|
@@ -3261,18 +3284,29 @@ async function watchOne(member, st) {
|
|
|
3261
3284
|
const pending = st.pendingWake
|
|
3262
3285
|
const lastTry = st.lastTry
|
|
3263
3286
|
if (pending || lastTry) {
|
|
3264
|
-
const wid = pending?.wid ?? lastTry?.wid
|
|
3287
|
+
const wid = pending?.wid ?? lastTry?.wid
|
|
3265
3288
|
const at = pending?.at ?? lastTry?.at ?? Date.now()
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3289
|
+
const ageSec = Math.round((Date.now() - at) / 1000)
|
|
3290
|
+
// 只有"这次推门"留下的尝试才算 woke。lastTry 可能是几小时前一次没投出去的
|
|
3291
|
+
// 计划(2026-11-17 的僵尸 woke 行就是它:旧 wid 不断被认领,lag 越大、inserted 恒 0)。
|
|
3292
|
+
// 2026-11-17 第三条鱼:缺席不要给它一个合法替身——没有 wid 就写 missing=wid,
|
|
3293
|
+
// 不要写 wid=none(语法上像真 id,语义上是"没有")。
|
|
3294
|
+
const relevant = Boolean(wid) && (Boolean(pending) || (Number.isFinite(ageSec) && ageSec <= WAKE_ATTEMPT_TTL_SEC))
|
|
3295
|
+
if (relevant) {
|
|
3296
|
+
watchmanLog(
|
|
3297
|
+
'woke',
|
|
3298
|
+
`wid=${wid} trigger=${pending ? (pending.pushed ? 'push' : 'external') : 'external'} lag=${ageSec}s inserted=${pending ? 1 : 0} to=${member.key}`,
|
|
3299
|
+
)
|
|
3300
|
+
} else {
|
|
3301
|
+
watchmanLog('note', `note=session-mounted member=${member.key}${wid ? ` wid=${wid}` : ' missing=wid'} try-age=${ageSec}s`)
|
|
3302
|
+
}
|
|
3270
3303
|
} else {
|
|
3271
3304
|
// 进程刚起时心跳文件还是上一轮留下的(stale),下一轮又新鲜了 ——
|
|
3272
3305
|
// 那不是"谁叫醒了她",是"会话自己挂上了"。记 note,不冒充 woke(2026-11-13 他要求的)。
|
|
3273
3306
|
watchmanLog('note', `note=session-mounted member=${member.key} hb-age=${hbNow}s`)
|
|
3274
3307
|
}
|
|
3275
3308
|
delete st.pendingWake
|
|
3309
|
+
st.lastTry = null
|
|
3276
3310
|
saveWatchState()
|
|
3277
3311
|
}
|
|
3278
3312
|
if (st.lastFresh !== freshNow) {
|
|
@@ -3325,15 +3359,7 @@ async function watchOne(member, st) {
|
|
|
3325
3359
|
// 2026-11-14 小肥鱼:`lag=0s` 是误导——它原来量的是"决定→插入",不是"到点→投递"。
|
|
3326
3360
|
// 她提这个字段时没写清是哪一段(自己埋的歧义)。现在 `lag` 一律指"到点→投递":
|
|
3327
3361
|
// 到点时刻从 job.at(HH:MM)算回来;算不出来(坏形状)才退回"现在",并在账上标 `lag-unknown`。
|
|
3328
|
-
const dueAt = (
|
|
3329
|
-
const m = /^(\d{1,2}):(\d{2})$/.exec(String(job.at ?? ''));
|
|
3330
|
-
if (!m) return null;
|
|
3331
|
-
const d = new Date();
|
|
3332
|
-
d.setHours(Number(m[1]), Number(m[2]), 0, 0);
|
|
3333
|
-
// 计划表里的点是"今天的 HH:MM";如果算出来比现在晚 12 小时以上,那它其实是昨天的点。
|
|
3334
|
-
if (d.getTime() > Date.now() + 12 * 3600_000) d.setDate(d.getDate() - 1);
|
|
3335
|
-
return d.getTime();
|
|
3336
|
-
})();
|
|
3362
|
+
const dueAt = planDueMs(job.at)
|
|
3337
3363
|
st.lastTry = { wid, at: Date.now() }
|
|
3338
3364
|
// 同一条 plan 的窗口有两分钟(tick 60s + 窗口 1 分钟),wake-try 每个 wid 只写一次。
|
|
3339
3365
|
const firstTry = st.triedWid !== wid
|
|
@@ -3366,8 +3392,8 @@ async function watchOne(member, st) {
|
|
|
3366
3392
|
watchmanLog(
|
|
3367
3393
|
'woke',
|
|
3368
3394
|
dueAt === null
|
|
3369
|
-
? `wid=${wid} trigger=plan lag-unknown inserted=1`
|
|
3370
|
-
: `wid=${wid} trigger=plan lag=${Math.round((Date.now() - dueAt) / 1000)}s inserted=1`,
|
|
3395
|
+
? `wid=${wid} trigger=plan lag-unknown inserted=1 to=${member.key}`
|
|
3396
|
+
: `wid=${wid} trigger=plan lag=${Math.round((Date.now() - dueAt) / 1000)}s inserted=1 to=${member.key}`,
|
|
3371
3397
|
)
|
|
3372
3398
|
markWake(member.key, 'plan')
|
|
3373
3399
|
} catch (error) {
|
|
@@ -3439,7 +3465,7 @@ async function watchOne(member, st) {
|
|
|
3439
3465
|
st.wakes += 1
|
|
3440
3466
|
st.fails = 0
|
|
3441
3467
|
saveWatchState()
|
|
3442
|
-
watchmanLog('woke', `ok wakes-today=${st.wakes} reason=${res.detail} night=${night} ${job.tag}`)
|
|
3468
|
+
watchmanLog('woke', `ok wakes-today=${st.wakes} reason=${res.detail} night=${night} to=${member.key} ${job.tag}`)
|
|
3443
3469
|
// 推门也算叫醒过一次:15 分钟后回看她有没有开口(A7 判据)。
|
|
3444
3470
|
markWake(member.key, 'plan')
|
|
3445
3471
|
// 窗是被"推"开的,里面还没人接话:所以把同一句话也递进群,
|
|
@@ -3462,7 +3488,7 @@ async function watchOne(member, st) {
|
|
|
3462
3488
|
} else {
|
|
3463
3489
|
st.fails += 1
|
|
3464
3490
|
saveWatchState()
|
|
3465
|
-
watchmanLog('woke', `failed fails=${st.fails} reason=${res.detail} ${job.tag}`)
|
|
3491
|
+
watchmanLog('woke', `failed fails=${st.fails} reason=${res.detail} to=${member.key} ${job.tag}`)
|
|
3466
3492
|
}
|
|
3467
3493
|
}
|
|
3468
3494
|
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// 账本里的每个字节都要能被印出来(第四条鱼 #1014,2026-11-17)。
|
|
2
|
+
//
|
|
3
|
+
// 现场:一条群消息里 markdown 的 `b 被 PowerShell 双引号 here-string 当成了退格符(0x08),
|
|
4
|
+
// 落盘后显示成 "undles=" —— 一个"合法但少一个字符"的字段名,按字段读的脚本不会报错。
|
|
5
|
+
//
|
|
6
|
+
// 规则:C0 控制字符(除换行)与 DEL 一律换成可见占位 <0xNN>;换行保留(一条消息可以多行)。
|
|
7
|
+
//
|
|
8
|
+
// 2026-11-17 第四条鱼补两句(#1016):扫字节之前先问"它是不是本来就不是文本"——
|
|
9
|
+
// UTF-16LE 的 "no" 是 6e 00 6f 00,按 UTF-8 读就是一片 NUL,那不是坏字节,是读法不对。
|
|
10
|
+
// 所以这里既给字符级的 printableText,也给字节级的 encodingOf / scanBytes。
|
|
11
|
+
const UNPRINTABLE = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g;
|
|
12
|
+
|
|
13
|
+
export function printableText(value) {
|
|
14
|
+
if (typeof value !== 'string') return value;
|
|
15
|
+
return value.replace(
|
|
16
|
+
UNPRINTABLE,
|
|
17
|
+
(ch) => '<0x' + ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0') + '>',
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function countUnprintable(value) {
|
|
22
|
+
if (typeof value !== 'string') return 0;
|
|
23
|
+
const m = value.match(UNPRINTABLE);
|
|
24
|
+
return m ? m.length : 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 猜一个 Buffer 的编码。只为"扫之前先分清楚文本还是二进制/UTF-16"这一件事,
|
|
29
|
+
* 不是通用编码识别器:能认 BOM、能认"偶数位/奇数位大量 0x00"这两种最常见的情况。
|
|
30
|
+
*/
|
|
31
|
+
export function encodingOf(buf) {
|
|
32
|
+
if (!buf || buf.length === 0) return 'empty';
|
|
33
|
+
if (buf.length >= 3 && buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) return 'utf-8-bom';
|
|
34
|
+
if (buf.length >= 2 && buf[0] === 0xff && buf[1] === 0xfe) return 'utf-16le-bom';
|
|
35
|
+
if (buf.length >= 2 && buf[0] === 0xfe && buf[1] === 0xff) return 'utf-16be-bom';
|
|
36
|
+
const n = Math.min(buf.length, 512);
|
|
37
|
+
let pairs = 0;
|
|
38
|
+
let zerosEven = 0;
|
|
39
|
+
let zerosOdd = 0;
|
|
40
|
+
for (let i = 0; i + 1 < n; i += 2) {
|
|
41
|
+
pairs += 1;
|
|
42
|
+
if (buf[i] === 0) zerosEven += 1;
|
|
43
|
+
if (buf[i + 1] === 0) zerosOdd += 1;
|
|
44
|
+
}
|
|
45
|
+
if (pairs >= 2 && zerosOdd / pairs > 0.6) return 'utf-16le';
|
|
46
|
+
if (pairs >= 2 && zerosEven / pairs > 0.6) return 'utf-16be';
|
|
47
|
+
return 'utf-8';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 扫一段字节里有没有"不可打印字符"。
|
|
52
|
+
* 返回 { encoding, decoded, unprintable: [{ index, char, code }] };
|
|
53
|
+
* UTF-16 会先解码再扫——这样 NUL 是编码差异,不会被当成内容坏了。
|
|
54
|
+
*/
|
|
55
|
+
export function scanBytes(buf) {
|
|
56
|
+
const encoding = encodingOf(buf);
|
|
57
|
+
if (encoding === 'empty') return { encoding, decoded: '', unprintable: [] };
|
|
58
|
+
if (encoding === 'utf-16le-bom' || encoding === 'utf-16be-bom') {
|
|
59
|
+
// 用 Node 的 TextDecoder(buf 可能是 Buffer 或 Uint8Array)
|
|
60
|
+
try {
|
|
61
|
+
const view = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
62
|
+
const decoded = new TextDecoder(encoding === 'utf-16le-bom' ? 'utf-16le' : 'utf-16be').decode(view);
|
|
63
|
+
return { encoding, decoded, unprintable: occurrencesOf(decoded) };
|
|
64
|
+
} catch {
|
|
65
|
+
return { encoding, decoded: '', unprintable: [] };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (encoding === 'utf-16le' || encoding === 'utf-16be') {
|
|
69
|
+
try {
|
|
70
|
+
const view = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
71
|
+
const decoded = new TextDecoder(encoding === 'utf-16le' ? 'utf-16le' : 'utf-16be').decode(view);
|
|
72
|
+
return { encoding, decoded, unprintable: occurrencesOf(decoded) };
|
|
73
|
+
} catch {
|
|
74
|
+
return { encoding, decoded: '', unprintable: [] };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const raw = buf.toString ? buf.toString('utf8') : String(buf);
|
|
78
|
+
return { encoding, decoded: raw, unprintable: occurrencesOf(raw) };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function occurrencesOf(str) {
|
|
82
|
+
const out = [];
|
|
83
|
+
const re = new RegExp(UNPRINTABLE.source, 'g');
|
|
84
|
+
let m;
|
|
85
|
+
while ((m = re.exec(str)) !== null) {
|
|
86
|
+
out.push({ index: m.index, char: m[0], code: m[0].charCodeAt(0) });
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|