@napuketto/adapter 0.8.4 → 0.10.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/dist/core/index.d.mts +2 -2
- package/dist/core/index.mjs +2 -2
- package/dist/core-DW5i42XH.mjs +481 -0
- package/dist/core-DW5i42XH.mjs.map +1 -0
- package/dist/essence-DSSyWl36.mjs +138 -0
- package/dist/essence-DSSyWl36.mjs.map +1 -0
- package/dist/index-h-tsUu0j.d.mts +240 -0
- package/dist/index-h-tsUu0j.d.mts.map +1 -0
- package/dist/kurobot/index.d.mts +1 -1
- package/dist/kurobot/index.mjs +1 -1
- package/dist/milky/index.d.mts +57 -3
- package/dist/milky/index.d.mts.map +1 -1
- package/dist/milky/index.mjs +502 -3
- package/dist/milky/index.mjs.map +1 -1
- package/dist/onebot11/index.d.mts +38 -4
- package/dist/onebot11/index.d.mts.map +1 -1
- package/dist/onebot11/index.mjs +204 -249
- package/dist/onebot11/index.mjs.map +1 -1
- package/dist/satori/index.d.mts +1 -1
- package/dist/satori/index.mjs +1 -1
- package/package.json +2 -2
- package/dist/buddy-req-Q7Q4bVuz.mjs +0 -43
- package/dist/buddy-req-Q7Q4bVuz.mjs.map +0 -1
- package/dist/core-DyXyDUcU.mjs +0 -183
- package/dist/core-DyXyDUcU.mjs.map +0 -1
- package/dist/index-CDKj-jus.d.mts +0 -117
- package/dist/index-CDKj-jus.d.mts.map +0 -1
package/dist/milky/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { _ as BaseProtocolAdapter, b as ActionRegistry, c as nodeAt, d as toHex, f as varintAt, i as extractSysMsgEnvelope, l as recognizeSysMsg, n as decodeProtoTree, p as varintOf, s as narrowSysMsgBlobs, u as textOf } from "../core-DW5i42XH.mjs";
|
|
2
2
|
import { n as firstUntranslatedGrayTip, t as collectGrayTipUids } from "../gray-tip-LwBee5mo.mjs";
|
|
3
|
-
import {
|
|
4
|
-
import { n as subscribeRawMessages } from "../raw-message-D_ecJy4C.mjs";
|
|
3
|
+
import { a as MS_TO_SEC$2, i as narrowBuddyReqs, n as extractEssenceSetSysMsg, o as resolveUin, r as narrowEssenceGroupArg, t as diffEssenceList } from "../essence-DSSyWl36.mjs";
|
|
4
|
+
import { n as subscribeRawMessages, t as forEachRawMessage } from "../raw-message-D_ecJy4C.mjs";
|
|
5
5
|
import { ChatType, GrayTipSubType, GroupApi, GroupNotifyMsgStatus, GroupNotifyMsgType, NTGroupRequestOperateTypes, TicketApi, TipGroupElementType, isKernelError, kernelError, toCanonicalElements } from "@napuketto/kernel";
|
|
6
6
|
import { mkdir, rename, writeFile } from "node:fs/promises";
|
|
7
7
|
import { detectFileType, downloadUrl, inferExtension } from "@napuketto/media";
|
|
@@ -2250,6 +2250,35 @@ function createMilkyActionRegistry(opts) {
|
|
|
2250
2250
|
return registry;
|
|
2251
2251
|
}
|
|
2252
2252
|
//#endregion
|
|
2253
|
+
//#region src/milky/helper/file-upload.ts
|
|
2254
|
+
/**
|
|
2255
|
+
* friend_file_upload 翻译(Milky 批次三,2026-11-01 录制定型)
|
|
2256
|
+
*
|
|
2257
|
+
* 源:Msg/onRecvOfflineFileMsg 与 Msg/onFileMsgCome(同载荷 RawMessage 数组,
|
|
2258
|
+
* 双回调按 msgId 在适配器去重);fileElement{fileName, fileSize 字符串,
|
|
2259
|
+
* file10MMd5, fileUuid, ...},sendType 0=收到(sender=对端)/ 2=自己发出。
|
|
2260
|
+
*
|
|
2261
|
+
* 已知差异(milky.md §0.8.6):官方 file_hash 语义为 TriSHA1,接收侧
|
|
2262
|
+
* fileSha/fileSha3 实测为空——取 file10MMd5(md5)充值,上传侧校准后回填。
|
|
2263
|
+
*/
|
|
2264
|
+
/** 单条 C2C 文件消息 → 事件载荷(非 C2C / 无 fileElement / 无 fileUuid → null)。 */
|
|
2265
|
+
function toFriendFileUploadData(msg, selfUin, uidToUin) {
|
|
2266
|
+
if (msg.chatType !== ChatType.C2C) return null;
|
|
2267
|
+
const file = msg.elements.find((el) => el.fileElement !== void 0)?.fileElement;
|
|
2268
|
+
if (file === void 0 || typeof file.fileUuid !== "string" || file.fileUuid === "") return null;
|
|
2269
|
+
const isSelf = msg.sendType === 2 || String(msg.senderUin) === selfUin;
|
|
2270
|
+
const userUin = msg.senderUin !== "" ? Number(msg.senderUin) : resolveUin(msg.senderUid, uidToUin);
|
|
2271
|
+
const size = Number(file.fileSize ?? 0);
|
|
2272
|
+
return {
|
|
2273
|
+
user_id: Number.isFinite(userUin) ? userUin : 0,
|
|
2274
|
+
file_id: file.fileUuid,
|
|
2275
|
+
file_name: file.fileName ?? "",
|
|
2276
|
+
file_size: Number.isFinite(size) ? size : 0,
|
|
2277
|
+
file_hash: file.file10MMd5 ?? file.fileSha ?? "",
|
|
2278
|
+
is_self: isSelf
|
|
2279
|
+
};
|
|
2280
|
+
}
|
|
2281
|
+
//#endregion
|
|
2253
2282
|
//#region src/milky/helper/notice.ts
|
|
2254
2283
|
/** 毫秒 → 秒(Unix 时间戳)。 */
|
|
2255
2284
|
const MS_TO_SEC = 1e3;
|
|
@@ -2396,6 +2425,94 @@ function toMilkyNoticeEvent(msg, ctx) {
|
|
|
2396
2425
|
return null;
|
|
2397
2426
|
}
|
|
2398
2427
|
//#endregion
|
|
2428
|
+
//#region src/milky/helper/peer-pin.ts
|
|
2429
|
+
/**
|
|
2430
|
+
* peer_pin_change 翻译(Milky 批次三,RecentContact/onRecentContactListChangedVer2)
|
|
2431
|
+
*
|
|
2432
|
+
* 回调随消息活动高频快照(含 topFlag 的全量条目 + chatType=7 影子条目),
|
|
2433
|
+
* 不能见条目就广播——按「同会话 topFlag 前后值翻转」检测(PinWatch)。
|
|
2434
|
+
* 启动期以 getPinnedContacts 播种基线(重启后首个取消置顶也能观测到)。
|
|
2435
|
+
*
|
|
2436
|
+
* 证据:2026-11-01 set_peer_pin 自触发翻转实验——changedList 条目
|
|
2437
|
+
* {chatType, peerUin, topFlag(1/0), topFlagTime(秒级 unix 字符串)},与操作
|
|
2438
|
+
* 一一对应(milky.md §0.8.6)。
|
|
2439
|
+
*/
|
|
2440
|
+
/** uin 形判别(peer_id 必须是数字串)。 */
|
|
2441
|
+
const NUMERIC_RE$2 = /^\d+$/;
|
|
2442
|
+
/** 置顶翻转检测(每适配器实例一份,键 = chatType:peerUin——群号与 uin 数值空间可重叠)。 */
|
|
2443
|
+
var PinWatch = class {
|
|
2444
|
+
states = /* @__PURE__ */ new Map();
|
|
2445
|
+
/** 启动基线:置顶会话播种(只记状态,不产生事件)。 */
|
|
2446
|
+
seed(pins) {
|
|
2447
|
+
for (const pin of pins) {
|
|
2448
|
+
if (pin.peerUin === void 0 || pin.peerUin === "") continue;
|
|
2449
|
+
const chatType = pin.scene === "group" ? ChatType.GROUP : ChatType.C2C;
|
|
2450
|
+
this.states.set(`${chatType}:${pin.peerUin}`, 1);
|
|
2451
|
+
}
|
|
2452
|
+
}
|
|
2453
|
+
/**
|
|
2454
|
+
* 观测一个变更条目:topFlag 相对上次观测翻转 → true(应广播);
|
|
2455
|
+
* 首见(基线未建立)或未变 → false。
|
|
2456
|
+
*/
|
|
2457
|
+
observe(chatType, peerUin, topFlag) {
|
|
2458
|
+
const key = `${chatType}:${peerUin}`;
|
|
2459
|
+
const prev = this.states.get(key);
|
|
2460
|
+
this.states.set(key, topFlag);
|
|
2461
|
+
return prev !== void 0 && prev !== topFlag;
|
|
2462
|
+
}
|
|
2463
|
+
};
|
|
2464
|
+
/** 场景映射(changedList 条目 chatType → Milky message_scene;影子/其他类型丢弃)。 */
|
|
2465
|
+
function pinScene(chatType) {
|
|
2466
|
+
if (chatType === ChatType.GROUP) return "group";
|
|
2467
|
+
if (chatType === ChatType.C2C) return "friend";
|
|
2468
|
+
return null;
|
|
2469
|
+
}
|
|
2470
|
+
/** 变更条目 → 事件载荷(null = 条目不含置顶字段/场景不支持,跳过不广播)。 */
|
|
2471
|
+
function toPeerPinChangeData(entry) {
|
|
2472
|
+
const scene = pinScene(entry.chatType);
|
|
2473
|
+
if (scene === null || entry.topFlag === void 0) return null;
|
|
2474
|
+
const peerUin = typeof entry.peerUin === "string" && entry.peerUin !== "" ? entry.peerUin : scene === "group" ? entry.peerUid : "";
|
|
2475
|
+
if (peerUin === "" || !NUMERIC_RE$2.test(peerUin)) return null;
|
|
2476
|
+
return {
|
|
2477
|
+
data: {
|
|
2478
|
+
message_scene: scene,
|
|
2479
|
+
peer_id: Number(peerUin),
|
|
2480
|
+
is_pinned: entry.topFlag === 1
|
|
2481
|
+
},
|
|
2482
|
+
topFlagTime: typeof entry.topFlagTime === "string" ? entry.topFlagTime : void 0,
|
|
2483
|
+
peerUin
|
|
2484
|
+
};
|
|
2485
|
+
}
|
|
2486
|
+
//#endregion
|
|
2487
|
+
//#region src/milky/helper/recent-keys.ts
|
|
2488
|
+
/**
|
|
2489
|
+
* 带过期去重键缓存(Milky 批次三)
|
|
2490
|
+
*
|
|
2491
|
+
* 两个场景共用:friend_file_upload 双回调(onRecvOfflineFileMsg / onFileMsgCome
|
|
2492
|
+
* 同载荷,按 msgId 去重)与 group_essence_message_change 双路径(732:1 快路径
|
|
2493
|
+
* vs 列表 diff,按 群:seq:方向 去重)。claim 语义 = 未登记过(或已过期)则登记
|
|
2494
|
+
* 并返回 true——调用方据此决定是否广播。
|
|
2495
|
+
*/
|
|
2496
|
+
var RecentKeyCache = class {
|
|
2497
|
+
entries = /* @__PURE__ */ new Map();
|
|
2498
|
+
ttlMs;
|
|
2499
|
+
constructor(ttlMs) {
|
|
2500
|
+
this.ttlMs = ttlMs;
|
|
2501
|
+
}
|
|
2502
|
+
/** 未命中则登记并返回 true(应广播);命中(窗口内重复)返回 false。 */
|
|
2503
|
+
claim(key, now = Date.now()) {
|
|
2504
|
+
this.prune(now);
|
|
2505
|
+
const expiry = this.entries.get(key);
|
|
2506
|
+
if (expiry !== void 0 && expiry > now) return false;
|
|
2507
|
+
this.entries.set(key, now + this.ttlMs);
|
|
2508
|
+
return true;
|
|
2509
|
+
}
|
|
2510
|
+
/** 惰性清理过期键(每次 claim 顺带执行,量级小无需定时器)。 */
|
|
2511
|
+
prune(now) {
|
|
2512
|
+
for (const [key, expiry] of this.entries) if (expiry <= now) this.entries.delete(key);
|
|
2513
|
+
}
|
|
2514
|
+
};
|
|
2515
|
+
//#endregion
|
|
2399
2516
|
//#region src/milky/helper/seq-index.ts
|
|
2400
2517
|
/** 单方向最大条目数(测试用例引用容量语义)。 */
|
|
2401
2518
|
const MAX_ENTRIES = 4096;
|
|
@@ -2474,6 +2591,97 @@ var MessageSeqIndex = class {
|
|
|
2474
2591
|
}
|
|
2475
2592
|
};
|
|
2476
2593
|
//#endregion
|
|
2594
|
+
//#region src/milky/helper/sysmsg.ts
|
|
2595
|
+
/**
|
|
2596
|
+
* Milky sysmsg 识别表(批次三,2026-11-01;识别机制共享自 core/sysmsg.ts)
|
|
2597
|
+
*
|
|
2598
|
+
* 键 = 原生分发键 "msgType:subType",规则产出事件 data 体(信封 time/self_id/
|
|
2599
|
+
* event_type 由适配器统一包装)。路径依据 = 三轮录制 wire bytes 全量解码
|
|
2600
|
+
* (milky.md §0.8.6 识别表;临时解码脚本 .napuketto/decode-sysmsg*.mjs 不入库)。
|
|
2601
|
+
*
|
|
2602
|
+
* - 732:16 群消息表情回应:f1.f1=群号;f3.f2.f44.f1.f1={f1 变动值, f2.f1=msgSeq,
|
|
2603
|
+
* f3={f1 faceId 字符串, f5: 1=添加/2=撤销, f3:1 仅添加时存在}}。
|
|
2604
|
+
* operator 不在 push(f4=bot 自身 uid 占位)——user_id 省略。
|
|
2605
|
+
* - 732:1 群精华设置:f1.f1=群号, f1.f8.f8.f1=msgSeq(路径提取共享自
|
|
2606
|
+
* core/essence.ts extractEssenceSetSysMsg);取消精华无独立 sysmsg
|
|
2607
|
+
* (remove 走 onGroupEssenceListChange 列表 diff,core/essence.ts)。
|
|
2608
|
+
* - 528:349/381/382 多端设备同步族:不广播。
|
|
2609
|
+
*/
|
|
2610
|
+
/** 数字串判别(faceId 实证为数字字符串)。 */
|
|
2611
|
+
const NUMERIC_RE$1 = /^\d+$/;
|
|
2612
|
+
/** 732:16 → group_message_reaction。 */
|
|
2613
|
+
const REACTION_RULE = {
|
|
2614
|
+
kind: "group_message_reaction",
|
|
2615
|
+
extract: (tree) => reactionData(tree)
|
|
2616
|
+
};
|
|
2617
|
+
/** 路径提取:f1.f1=群号 + f3.f2.f44.f1.f1=回应明细(任一缺失 → null)。 */
|
|
2618
|
+
function reactionData(tree) {
|
|
2619
|
+
const groupId = varintAt(tree, [1, 1]);
|
|
2620
|
+
const detail = nodeAt(tree, [
|
|
2621
|
+
3,
|
|
2622
|
+
2,
|
|
2623
|
+
44,
|
|
2624
|
+
1,
|
|
2625
|
+
1
|
|
2626
|
+
]);
|
|
2627
|
+
if (groupId === null || detail === null) return null;
|
|
2628
|
+
return reactionDetail(groupId, detail);
|
|
2629
|
+
}
|
|
2630
|
+
/** 明细提取:f2.f1=msgSeq + f3=face 块(任一缺失 → null)。 */
|
|
2631
|
+
function reactionDetail(groupId, detail) {
|
|
2632
|
+
const msgSeq = varintAt(detail, [2, 1]);
|
|
2633
|
+
const face = nodeAt(detail, [3]);
|
|
2634
|
+
if (msgSeq === null || face === null) return null;
|
|
2635
|
+
return reactionFace(groupId, msgSeq, face);
|
|
2636
|
+
}
|
|
2637
|
+
/** face 块提取:faceId 实证为数字字符串("76"/"124"…);f5 双判据(f3 仅添加时存在)。 */
|
|
2638
|
+
function reactionFace(groupId, msgSeq, face) {
|
|
2639
|
+
const isAdd = reactionIsAdd(face);
|
|
2640
|
+
const faceId = reactionFaceId(face);
|
|
2641
|
+
if (isAdd === null || faceId === null) return null;
|
|
2642
|
+
return {
|
|
2643
|
+
group_id: groupId,
|
|
2644
|
+
message_seq: msgSeq,
|
|
2645
|
+
face_id: faceId,
|
|
2646
|
+
reaction_type: "face",
|
|
2647
|
+
is_add: isAdd
|
|
2648
|
+
};
|
|
2649
|
+
}
|
|
2650
|
+
/** faceId 字段(数字字符串 → number;缺失/非数字 → null)。 */
|
|
2651
|
+
function reactionFaceId(face) {
|
|
2652
|
+
const faceIdText = textOf(face, 1);
|
|
2653
|
+
return faceIdText !== null && NUMERIC_RE$1.test(faceIdText) ? Number(faceIdText) : null;
|
|
2654
|
+
}
|
|
2655
|
+
/** is_add 判定:f5=1 添加 / 2 撤销(f3=1 仅添加时存在,双判据);非法 → null。 */
|
|
2656
|
+
function reactionIsAdd(face) {
|
|
2657
|
+
const op = varintOf(face, 5);
|
|
2658
|
+
if (op === 1) return true;
|
|
2659
|
+
return op === 2 ? false : null;
|
|
2660
|
+
}
|
|
2661
|
+
/** (msgType, subType) → 规则表(键 = 原生分发键)。 */
|
|
2662
|
+
const KIND_TABLE = /* @__PURE__ */ new Map([["732:16", REACTION_RULE], ["732:1", {
|
|
2663
|
+
kind: "group_essence_message_change",
|
|
2664
|
+
extract: (tree) => {
|
|
2665
|
+
const hit = extractEssenceSetSysMsg(tree);
|
|
2666
|
+
if (hit === null) return null;
|
|
2667
|
+
return {
|
|
2668
|
+
group_id: hit.groupCode,
|
|
2669
|
+
message_seq: hit.messageSeq,
|
|
2670
|
+
is_set: true
|
|
2671
|
+
};
|
|
2672
|
+
}
|
|
2673
|
+
}]]);
|
|
2674
|
+
/** 已观测但未命名的键(多端设备同步族等,不广播只走校准日志)。 */
|
|
2675
|
+
const OBSERVED_UNNAMED = /* @__PURE__ */ new Set([
|
|
2676
|
+
"528:382",
|
|
2677
|
+
"528:349",
|
|
2678
|
+
"528:381"
|
|
2679
|
+
]);
|
|
2680
|
+
/** (msgType, subType) → 识别结果(绑定 Milky 表;table 参数供单测注入)。 */
|
|
2681
|
+
function recognizeMilkySysMsg(msgType, subType, table = KIND_TABLE) {
|
|
2682
|
+
return recognizeSysMsg(msgType, subType, table, OBSERVED_UNNAMED);
|
|
2683
|
+
}
|
|
2684
|
+
//#endregion
|
|
2477
2685
|
//#region src/milky/transport.ts
|
|
2478
2686
|
/** /event 路径(规范固定)。 */
|
|
2479
2687
|
const EVENT_PATH = "/event";
|
|
@@ -2648,6 +2856,18 @@ var NapukettoMilkyAdapter = class extends BaseProtocolAdapter {
|
|
|
2648
2856
|
msgChannel;
|
|
2649
2857
|
groupChannel;
|
|
2650
2858
|
buddyChannel;
|
|
2859
|
+
recentContactChannel;
|
|
2860
|
+
loginChannel;
|
|
2861
|
+
/** peer_pin 翻转检测(批次三;启动期 getPinnedContacts 播种)。 */
|
|
2862
|
+
pinWatch = new PinWatch();
|
|
2863
|
+
/** friend_file_upload 双回调去重(同载荷,按 msgId)。 */
|
|
2864
|
+
fileKeys = new RecentKeyCache(6e5);
|
|
2865
|
+
/** essence 双路径去重(732:1 快路径 vs 列表 diff,键 群:seq:方向)。 */
|
|
2866
|
+
essenceKeys = new RecentKeyCache(3e5);
|
|
2867
|
+
/** 精华列表缓存(群号 → seq → 条目{操作者/发送者})。 */
|
|
2868
|
+
essenceCaches = /* @__PURE__ */ new Map();
|
|
2869
|
+
/** 精华列表拉取串行链(并发回写乱序会把新列表覆盖成旧列表)。 */
|
|
2870
|
+
essenceChain = Promise.resolve();
|
|
2651
2871
|
calibLogger;
|
|
2652
2872
|
api;
|
|
2653
2873
|
seqIndex = new MessageSeqIndex();
|
|
@@ -2668,6 +2888,8 @@ var NapukettoMilkyAdapter = class extends BaseProtocolAdapter {
|
|
|
2668
2888
|
this.msgChannel = opts.msgChannel;
|
|
2669
2889
|
this.groupChannel = opts.groupChannel;
|
|
2670
2890
|
this.buddyChannel = opts.buddyChannel;
|
|
2891
|
+
this.recentContactChannel = opts.recentContactChannel;
|
|
2892
|
+
this.loginChannel = opts.loginChannel;
|
|
2671
2893
|
this.calibLogger = opts.logger;
|
|
2672
2894
|
this.api = new MilkyApi(opts, this.seqIndex);
|
|
2673
2895
|
this.registry = createMilkyActionRegistry({ api: this.api });
|
|
@@ -2715,6 +2937,19 @@ var NapukettoMilkyAdapter = class extends BaseProtocolAdapter {
|
|
|
2715
2937
|
this.unsubscribes.push(subscribeRawMessages(this.msgChannel, (msg) => {
|
|
2716
2938
|
this.handleIncoming(msg);
|
|
2717
2939
|
}));
|
|
2940
|
+
this.unsubscribes.push(this.msgChannel.on("Msg/onRecvSysMsg", (arg) => {
|
|
2941
|
+
this.handleSysMsg(arg);
|
|
2942
|
+
}));
|
|
2943
|
+
this.unsubscribes.push(this.msgChannel.on("Msg/onRecvOfflineFileMsg", (msgs) => {
|
|
2944
|
+
this.handleFileMessages(msgs);
|
|
2945
|
+
}), this.msgChannel.on("Msg/onFileMsgCome", (msgs) => {
|
|
2946
|
+
this.handleFileMessages(msgs);
|
|
2947
|
+
}));
|
|
2948
|
+
this.subscribeGroupAndBuddy();
|
|
2949
|
+
this.subscribeBatch3();
|
|
2950
|
+
}
|
|
2951
|
+
/** 群通知 + 好友请求通道(P1 事件源;通道缺省不订阅)。 */
|
|
2952
|
+
subscribeGroupAndBuddy() {
|
|
2718
2953
|
if (this.groupChannel !== void 0) {
|
|
2719
2954
|
const groupChannel = this.groupChannel;
|
|
2720
2955
|
this.unsubscribes.push(groupChannel.on("Group/onGroupNotifiesUpdated", (doubt, notifies) => {
|
|
@@ -2728,6 +2963,30 @@ var NapukettoMilkyAdapter = class extends BaseProtocolAdapter {
|
|
|
2728
2963
|
}));
|
|
2729
2964
|
}
|
|
2730
2965
|
}
|
|
2966
|
+
/** 批次三通道:精华列表变更 / 置顶翻转(含播种基线)/ bot_offline。 */
|
|
2967
|
+
subscribeBatch3() {
|
|
2968
|
+
if (this.groupChannel !== void 0) {
|
|
2969
|
+
const groupChannel = this.groupChannel;
|
|
2970
|
+
this.unsubscribes.push(groupChannel.on("Group/onGroupEssenceListChange", (arg) => {
|
|
2971
|
+
this.handleEssenceChange(arg);
|
|
2972
|
+
}));
|
|
2973
|
+
}
|
|
2974
|
+
if (this.recentContactChannel !== void 0) {
|
|
2975
|
+
const recentContactChannel = this.recentContactChannel;
|
|
2976
|
+
this.unsubscribes.push(recentContactChannel.on("RecentContact/onRecentContactListChangedVer2", (lists) => {
|
|
2977
|
+
this.handlePinChanges(lists);
|
|
2978
|
+
}));
|
|
2979
|
+
this.seedPinWatch();
|
|
2980
|
+
}
|
|
2981
|
+
if (this.loginChannel !== void 0) {
|
|
2982
|
+
const loginChannel = this.loginChannel;
|
|
2983
|
+
this.unsubscribes.push(loginChannel.on("Login/onLoginDisConnected", (...args) => {
|
|
2984
|
+
this.broadcastBotOffline("disconnected", args);
|
|
2985
|
+
}), loginChannel.on("Login/onLogoutSucceed", (...args) => {
|
|
2986
|
+
this.broadcastBotOffline("logout", args);
|
|
2987
|
+
}));
|
|
2988
|
+
}
|
|
2989
|
+
}
|
|
2731
2990
|
/** 退订(幂等)。 */
|
|
2732
2991
|
unsubscribeAll() {
|
|
2733
2992
|
for (const off of this.unsubscribes) off();
|
|
@@ -2835,6 +3094,200 @@ var NapukettoMilkyAdapter = class extends BaseProtocolAdapter {
|
|
|
2835
3094
|
msgId: msg.msgId
|
|
2836
3095
|
});
|
|
2837
3096
|
}
|
|
3097
|
+
/**
|
|
3098
|
+
* sysmsg 总闸:解码 → 识别 → 表命中才广播(732:16 表情回应 / 732:1 精华
|
|
3099
|
+
* 设置);未识别 debug 校准日志。规则提取失败不广播半成品事件。
|
|
3100
|
+
*/
|
|
3101
|
+
handleSysMsg(arg) {
|
|
3102
|
+
const blobs = narrowSysMsgBlobs(arg);
|
|
3103
|
+
if (blobs === null) {
|
|
3104
|
+
this.calibLogger?.warn({ raw: truncate(JSON.stringify(arg)) }, "milky: onRecvSysMsg 未知参数形状");
|
|
3105
|
+
return;
|
|
3106
|
+
}
|
|
3107
|
+
for (const blob of blobs) this.handleSysMsgBlob(blob);
|
|
3108
|
+
}
|
|
3109
|
+
/** 单条字节块:解码 → 识别 → 提取;任一步失败走校准日志返回 null。 */
|
|
3110
|
+
handleSysMsgBlob(blob) {
|
|
3111
|
+
const hit = this.sysMsgHit(blob);
|
|
3112
|
+
if (hit === null) return;
|
|
3113
|
+
if (!this.claimEssenceFastPath(hit)) return;
|
|
3114
|
+
this.broadcastEvent({
|
|
3115
|
+
time: hit.env.time ?? Math.floor(Date.now() / 1e3),
|
|
3116
|
+
self_id: Number(this.api.self.uin),
|
|
3117
|
+
event_type: hit.rule.kind,
|
|
3118
|
+
data: hit.data
|
|
3119
|
+
});
|
|
3120
|
+
}
|
|
3121
|
+
/** 解码 + 识别 + 提取(每步失败各自打 debug 校准日志)。 */
|
|
3122
|
+
sysMsgHit(blob) {
|
|
3123
|
+
const tree = decodeProtoTree(blob);
|
|
3124
|
+
if (tree === null) {
|
|
3125
|
+
this.calibLogger?.debug({ raw: toHex(blob).slice(0, 1024) }, "milky: onRecvSysMsg protobuf 解码失败");
|
|
3126
|
+
return null;
|
|
3127
|
+
}
|
|
3128
|
+
const env = extractSysMsgEnvelope(tree);
|
|
3129
|
+
const rule = this.sysMsgRule(env, tree, blob);
|
|
3130
|
+
if (rule === null) return null;
|
|
3131
|
+
return {
|
|
3132
|
+
env,
|
|
3133
|
+
rule: rule.rule,
|
|
3134
|
+
data: rule.data
|
|
3135
|
+
};
|
|
3136
|
+
}
|
|
3137
|
+
/** 识别 + 规则提取(未识别/提取失败 → 校准日志 + null)。 */
|
|
3138
|
+
sysMsgRule(env, tree, blob) {
|
|
3139
|
+
const rec = recognizeMilkySysMsg(env.msgType, env.subType);
|
|
3140
|
+
if (rec.kind !== "notice") {
|
|
3141
|
+
this.calibLogger?.debug({
|
|
3142
|
+
verdict: rec.kind,
|
|
3143
|
+
msgType: env.msgType,
|
|
3144
|
+
subType: env.subType,
|
|
3145
|
+
strings: env.strings,
|
|
3146
|
+
raw: toHex(blob).slice(0, 1024)
|
|
3147
|
+
}, "milky: onRecvSysMsg 校准数据(未识别,不广播)");
|
|
3148
|
+
return null;
|
|
3149
|
+
}
|
|
3150
|
+
const ctx = {
|
|
3151
|
+
selfUin: Number(this.api.self.uin),
|
|
3152
|
+
time: env.time ?? Math.floor(Date.now() / 1e3)
|
|
3153
|
+
};
|
|
3154
|
+
const data = rec.rule.extract(tree, env, () => 0, ctx);
|
|
3155
|
+
if (data === null) {
|
|
3156
|
+
this.calibLogger?.debug({
|
|
3157
|
+
kind: rec.rule.kind,
|
|
3158
|
+
msgType: env.msgType,
|
|
3159
|
+
subType: env.subType
|
|
3160
|
+
}, "milky: sysmsg 规则提取失败(不广播)");
|
|
3161
|
+
return null;
|
|
3162
|
+
}
|
|
3163
|
+
return {
|
|
3164
|
+
rule: rec.rule,
|
|
3165
|
+
data
|
|
3166
|
+
};
|
|
3167
|
+
}
|
|
3168
|
+
/** 精华快路径去重守卫:非精华事件恒 true;精华事件 claim 失败(已由列表 diff 抢发)→ false。 */
|
|
3169
|
+
claimEssenceFastPath(hit) {
|
|
3170
|
+
if (hit.rule.kind !== "group_essence_message_change") return true;
|
|
3171
|
+
const essence = hit.data;
|
|
3172
|
+
return this.essenceKeys.claim(`${essence.group_id}:${essence.message_seq}:1`);
|
|
3173
|
+
}
|
|
3174
|
+
/** 私聊文件消息 → friend_file_upload(双回调同载荷,按 msgId 去重)。 */
|
|
3175
|
+
handleFileMessages(msgs) {
|
|
3176
|
+
forEachRawMessage(msgs, (msg) => {
|
|
3177
|
+
const data = toFriendFileUploadData(msg, this.api.self.uin, /* @__PURE__ */ new Map());
|
|
3178
|
+
if (data === null) return;
|
|
3179
|
+
if (data.user_id === 0) this.calibLogger?.debug({
|
|
3180
|
+
msgId: msg.msgId,
|
|
3181
|
+
senderUid: msg.senderUid
|
|
3182
|
+
}, "milky: friend_file_upload senderUin 缺失(user_id=0)");
|
|
3183
|
+
if (!this.fileKeys.claim(`file:${msg.msgId}`)) return;
|
|
3184
|
+
this.broadcastEvent({
|
|
3185
|
+
time: secondsOf(msg.msgTime),
|
|
3186
|
+
self_id: Number(this.api.self.uin),
|
|
3187
|
+
event_type: "friend_file_upload",
|
|
3188
|
+
data
|
|
3189
|
+
});
|
|
3190
|
+
});
|
|
3191
|
+
}
|
|
3192
|
+
/** 精华列表变更:拉全量列表 diff(remove 路径 + operator 回填;串行防乱序)。 */
|
|
3193
|
+
handleEssenceChange(arg) {
|
|
3194
|
+
const groupCode = narrowEssenceGroupArg(arg);
|
|
3195
|
+
if (groupCode === null) {
|
|
3196
|
+
this.calibLogger?.warn({ raw: truncate(JSON.stringify(arg)) }, "milky: onGroupEssenceListChange 未知参数形状");
|
|
3197
|
+
return;
|
|
3198
|
+
}
|
|
3199
|
+
this.essenceChain = this.essenceChain.then(() => this.syncEssenceList(groupCode), () => void 0);
|
|
3200
|
+
}
|
|
3201
|
+
/** 单群精华列表同步:拉取 → diff → 广播 → 更新缓存(基线未建立只建基线)。 */
|
|
3202
|
+
async syncEssenceList(groupCode) {
|
|
3203
|
+
let items;
|
|
3204
|
+
try {
|
|
3205
|
+
items = await this.api.webApi.getEssenceMsgList(groupCode);
|
|
3206
|
+
} catch (e) {
|
|
3207
|
+
this.calibLogger?.warn({
|
|
3208
|
+
group: groupCode,
|
|
3209
|
+
err: String(e)
|
|
3210
|
+
}, "milky: 精华列表拉取失败(diff 跳过)");
|
|
3211
|
+
return;
|
|
3212
|
+
}
|
|
3213
|
+
const prev = this.essenceCaches.get(groupCode);
|
|
3214
|
+
const { cache, added, removed } = diffEssenceList(prev, items);
|
|
3215
|
+
this.essenceCaches.set(groupCode, cache);
|
|
3216
|
+
if (prev === void 0) return;
|
|
3217
|
+
const selfId = Number(this.api.self.uin);
|
|
3218
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
3219
|
+
this.broadcastEssenceAdded(groupCode, added, now, selfId);
|
|
3220
|
+
this.broadcastEssenceRemoved(groupCode, removed, now, selfId);
|
|
3221
|
+
}
|
|
3222
|
+
/** diff 新增项广播(is_set=true;operator = add_digest_uin;快路径已发则跳过)。 */
|
|
3223
|
+
broadcastEssenceAdded(groupCode, added, now, selfId) {
|
|
3224
|
+
for (const item of added) {
|
|
3225
|
+
if (!this.essenceKeys.claim(`${groupCode}:${item.messageSeq}:1`)) continue;
|
|
3226
|
+
const data = essenceAddedData(groupCode, item);
|
|
3227
|
+
this.broadcastEvent({
|
|
3228
|
+
time: now,
|
|
3229
|
+
self_id: selfId,
|
|
3230
|
+
event_type: "group_essence_message_change",
|
|
3231
|
+
data
|
|
3232
|
+
});
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3235
|
+
/** diff 移除项广播(is_set=false;取消无 sysmsg,operator 不可得省略)。 */
|
|
3236
|
+
broadcastEssenceRemoved(groupCode, removed, now, selfId) {
|
|
3237
|
+
for (const seq of removed) {
|
|
3238
|
+
if (!this.essenceKeys.claim(`${groupCode}:${seq}:0`)) continue;
|
|
3239
|
+
this.broadcastEvent({
|
|
3240
|
+
time: now,
|
|
3241
|
+
self_id: selfId,
|
|
3242
|
+
event_type: "group_essence_message_change",
|
|
3243
|
+
data: {
|
|
3244
|
+
group_id: Number(groupCode),
|
|
3245
|
+
message_seq: seq,
|
|
3246
|
+
is_set: false
|
|
3247
|
+
}
|
|
3248
|
+
});
|
|
3249
|
+
}
|
|
3250
|
+
}
|
|
3251
|
+
/** 会话列表变更 → peer_pin_change(topFlag 翻转检测,条目自带 chatType 判场景)。 */
|
|
3252
|
+
handlePinChanges(lists) {
|
|
3253
|
+
const arr = Array.isArray(lists) ? lists : [lists];
|
|
3254
|
+
for (const list of arr) this.handlePinChangeList(list);
|
|
3255
|
+
}
|
|
3256
|
+
/** 单个变更列表:changedList 逐条目翻转检测 + 广播。 */
|
|
3257
|
+
handlePinChangeList(list) {
|
|
3258
|
+
for (const entry of changedListOf(list)) this.handlePinEntry(entry);
|
|
3259
|
+
}
|
|
3260
|
+
/** 单个变更条目:翻译 + 翻转检测 + 广播(首见/未变/不支持场景静默跳过)。 */
|
|
3261
|
+
handlePinEntry(entry) {
|
|
3262
|
+
const payload = pinPayloadOf(entry);
|
|
3263
|
+
if (payload === null) return;
|
|
3264
|
+
if (!this.pinWatch.observe(payload.chatType, payload.peerUin, payload.topFlag)) return;
|
|
3265
|
+
this.broadcastEvent({
|
|
3266
|
+
time: secondsOf(payload.topFlagTime),
|
|
3267
|
+
self_id: Number(this.api.self.uin),
|
|
3268
|
+
event_type: "peer_pin_change",
|
|
3269
|
+
data: payload.data
|
|
3270
|
+
});
|
|
3271
|
+
}
|
|
3272
|
+
/** 启动基线:置顶会话播种(重启后首个取消置顶也能观测到翻转;失败静默)。 */
|
|
3273
|
+
seedPinWatch() {
|
|
3274
|
+
this.api.recentContactApi.getPinnedContacts().then((pins) => {
|
|
3275
|
+
this.pinWatch.seed(pins);
|
|
3276
|
+
}).catch(() => void 0);
|
|
3277
|
+
}
|
|
3278
|
+
/** bot_offline:Login 桥回调(载荷零样本,reason 固定值 + raw 校准日志待野捕)。 */
|
|
3279
|
+
broadcastBotOffline(reason, args) {
|
|
3280
|
+
this.calibLogger?.debug({
|
|
3281
|
+
reason,
|
|
3282
|
+
raw: truncate(JSON.stringify(args))
|
|
3283
|
+
}, "milky: bot_offline(reason 为固定值,回调载荷待野捕校准)");
|
|
3284
|
+
this.broadcastEvent({
|
|
3285
|
+
time: Math.floor(Date.now() / 1e3),
|
|
3286
|
+
self_id: Number(this.api.self.uin),
|
|
3287
|
+
event_type: "bot_offline",
|
|
3288
|
+
data: { reason }
|
|
3289
|
+
});
|
|
3290
|
+
}
|
|
2838
3291
|
/** uid 批量 → uin(空集/失败返回 undefined 语义:调用方用空 Map)。 */
|
|
2839
3292
|
resolveUidToUin(uids) {
|
|
2840
3293
|
const unique = [...new Set(uids.filter((uid) => uid !== ""))];
|
|
@@ -2854,6 +3307,52 @@ function findRevokeElement(msg) {
|
|
|
2854
3307
|
function truncate(raw) {
|
|
2855
3308
|
return raw === void 0 ? "" : raw.slice(0, 2e3);
|
|
2856
3309
|
}
|
|
3310
|
+
/** 数字串判别(uin / 秒级时间戳)。 */
|
|
3311
|
+
const NUMERIC_RE = /^\d+$/;
|
|
3312
|
+
/** diff 新增项 → 事件载荷(operator = add_digest_uin,非数字串省略)。 */
|
|
3313
|
+
function essenceAddedData(groupCode, item) {
|
|
3314
|
+
const base = {
|
|
3315
|
+
group_id: Number(groupCode),
|
|
3316
|
+
message_seq: item.messageSeq
|
|
3317
|
+
};
|
|
3318
|
+
if (item.operatorUin !== null && NUMERIC_RE.test(item.operatorUin)) return {
|
|
3319
|
+
...base,
|
|
3320
|
+
operator_id: Number(item.operatorUin),
|
|
3321
|
+
is_set: true
|
|
3322
|
+
};
|
|
3323
|
+
return {
|
|
3324
|
+
...base,
|
|
3325
|
+
is_set: true
|
|
3326
|
+
};
|
|
3327
|
+
}
|
|
3328
|
+
/** 变更列表 → changedList 条目数组(形状不符 → 空)。 */
|
|
3329
|
+
function changedListOf(list) {
|
|
3330
|
+
if (list === null || typeof list !== "object") return [];
|
|
3331
|
+
const changed = list.changedList;
|
|
3332
|
+
return Array.isArray(changed) ? changed : [];
|
|
3333
|
+
}
|
|
3334
|
+
/** 变更条目 → 翻转检测输入(null = 条目不支持/缺置顶字段)。 */
|
|
3335
|
+
function pinPayloadOf(entry) {
|
|
3336
|
+
if (entry === null || typeof entry !== "object") return null;
|
|
3337
|
+
return pinPayloadFrom(entry);
|
|
3338
|
+
}
|
|
3339
|
+
function pinPayloadFrom(contact) {
|
|
3340
|
+
const topFlag = contact.topFlag;
|
|
3341
|
+
const payload = toPeerPinChangeData(contact);
|
|
3342
|
+
if (topFlag === void 0 || payload === null) return null;
|
|
3343
|
+
return {
|
|
3344
|
+
chatType: contact.chatType,
|
|
3345
|
+
peerUin: payload.peerUin,
|
|
3346
|
+
topFlag,
|
|
3347
|
+
topFlagTime: payload.topFlagTime,
|
|
3348
|
+
data: payload.data
|
|
3349
|
+
};
|
|
3350
|
+
}
|
|
3351
|
+
/** 秒级时间戳归一(空/非法 → 当前时间;topFlagTime/msgTime 均为秒级字符串)。 */
|
|
3352
|
+
function secondsOf(raw) {
|
|
3353
|
+
if (raw !== void 0 && NUMERIC_RE.test(raw)) return Number(raw);
|
|
3354
|
+
return Math.floor(Date.now() / 1e3);
|
|
3355
|
+
}
|
|
2857
3356
|
//#endregion
|
|
2858
3357
|
//#region src/milky/config.ts
|
|
2859
3358
|
/**
|