@napuketto/adapter 0.16.1 → 0.16.2

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.
@@ -1,7 +1,7 @@
1
1
  import { i as runAction, n as BaseProtocolAdapter, r as ActionRegistry } from "../core-D5c78lFV.mjs";
2
- import { i as toUnixSeconds, r as subscribeRawMessages, t as forEachRawMessage } from "../raw-message-DE3sdJg8.mjs";
3
- import { a as findRevokeElement, c as MS_TO_SEC, d as toUin, i as collectGrayTipUids, l as numericUin, n as sliceRaw, o as firstUntranslatedGrayTip, s as hasGrayTip, t as logEventFailure, u as resolveUin } from "../log-BsInJV_y.mjs";
4
- import { a as nodeAt, c as textOf, d as narrowBuddyReqs, i as narrowSysMsgBlobs, l as varintAt, n as extractEssenceSetSysMsg, o as processSysMsgBlob, r as narrowEssenceGroupArg, s as recognizeSysMsg, t as EssenceListSync, u as varintOf } from "../essence-Bxs-aKSC.mjs";
2
+ import { a as firstNonEmptyString, c as MS_TO_SEC, d as toUin, i as toUnixSeconds, l as numericUin, o as stringOrUndefined, r as subscribeRawMessages, s as startupBaselineSec, t as forEachRawMessage, u as resolveUin } from "../raw-message-Cz0UOrND.mjs";
3
+ import { a as findRevokeElement, i as collectGrayTipUids, n as sliceRaw, o as firstUntranslatedGrayTip, s as hasGrayTip, t as logEventFailure } from "../log-BznNQnpx.mjs";
4
+ import { a as nodeAt, c as textOf, d as BuddyReqDedup, f as batchUnreadNums, i as narrowSysMsgBlobs, l as varintAt, m as narrowBuddyReqs, n as extractEssenceSetSysMsg, o as processSysMsgBlob, p as filterBuddyReqs, r as narrowEssenceGroupArg, s as recognizeSysMsg, t as EssenceListSync, u as varintOf } from "../essence-Gnvq6YWT.mjs";
5
5
  import { ChatType, GrayTipSubType, GroupApi, GroupNotifyMsgStatus, GroupNotifyMsgType, NTGroupRequestOperateTypes, TicketApi, TipGroupElementType, kernelError, toCanonicalElements } from "@napuketto/kernel";
6
6
  import { z } from "zod";
7
7
  import { randomUUID } from "node:crypto";
@@ -860,9 +860,12 @@ async function findBuddyReq(api, initiatorUid) {
860
860
  return hit;
861
861
  }
862
862
  // src/milky/helper/event/request.ts
863
- /** BuddyReq → friend_request 事件(comment 取 words 字段,形状待校准,缺省空串)。 */
863
+ /**
864
+ * BuddyReq → friend_request 事件。
865
+ * comment 取验证留言:QQNT 实际字段是 `extWords`,`words` 为旧口径兜底(都缺省空串)。
866
+ */
864
867
  function toFriendRequestEvent(req, ctx) {
865
- const words = req["words"];
868
+ const comment = firstNonEmptyString(stringOrUndefined(req["extWords"]), stringOrUndefined(req["words"])) ?? "";
866
869
  return {
867
870
  time: Math.floor(Date.now() / MS_TO_SEC),
868
871
  self_id: Number(ctx.selfUin),
@@ -870,7 +873,7 @@ function toFriendRequestEvent(req, ctx) {
870
873
  data: {
871
874
  initiator_id: toUin(req.friendUid, ctx),
872
875
  initiator_uid: req.friendUid,
873
- comment: typeof words === "string" ? words : "",
876
+ comment,
874
877
  via: req.sourceName ?? ""
875
878
  }
876
879
  };
@@ -2185,22 +2188,51 @@ function createMilkyActionRegistry(opts) {
2185
2188
  return registry;
2186
2189
  }
2187
2190
  // src/milky/enrichment.ts
2191
+ /** 资料详情兜底的单批上限(好友申请批通常只有个位数,超限多为历史积压)。 */
2192
+ const MAX_PROFILE_LOOKUPS = 10;
2188
2193
  /** Milky 收链路异步回填器。 */
2189
2194
  var MilkyEnrichment = class {
2190
2195
  api;
2191
2196
  constructor(deps) {
2192
2197
  this.api = deps.api;
2193
2198
  }
2194
- /** uid 批量 → uin(空集/失败返回 undefined 语义:调用方用空 Map)。 */
2195
- resolveUidToUin(uids) {
2199
+ /**
2200
+ * uid 批量 → uin(空集/失败返回 undefined 语义:调用方用空 Map)。
2201
+ *
2202
+ * `profileFallback` 时才做资料详情兜底(issue #122):好友申请的申请人通常不在
2203
+ * 任何群里,群口径必然命中不了;而 grayTip 一类高频路径不做兜底——群口径整体失败
2204
+ * 时逐 uid 打 IPC 会把收链路拖垮。
2205
+ */
2206
+ async resolveUidToUin(uids, options = {}) {
2196
2207
  const unique = [...new Set(uids.filter((uid) => uid !== ""))];
2197
- if (unique.length === 0) return Promise.resolve(void 0);
2198
- return this.api.uidToUin(unique).catch((err) => {
2208
+ if (unique.length === 0) return;
2209
+ const map = await this.api.uidToUin(unique).catch((err) => {
2199
2210
  this.api.logger?.warn({
2200
2211
  count: unique.length,
2201
2212
  error: err instanceof Error ? err.message : String(err)
2202
2213
  }, "milky: uid→uin 批量转换失败,事件退用 uid");
2214
+ return /* @__PURE__ */ new Map();
2203
2215
  });
2216
+ if (options.profileFallback === true) await this.fillMissingUins(unique, map);
2217
+ return map;
2218
+ }
2219
+ /**
2220
+ * 群口径未命中的 uid 逐个走 `profileApi.getUinByUid` 兜底(原地补 map)。
2221
+ * fail-soft:单条失败 / 查不到保持未命中(翻译层退 0);条数设上限,防历史积压
2222
+ * 批次把逐条 IPC 打满。
2223
+ */
2224
+ async fillMissingUins(uids, map) {
2225
+ const missing = uids.filter((uid) => !map.has(uid));
2226
+ for (const uid of missing.slice(0, MAX_PROFILE_LOOKUPS)) try {
2227
+ const uin = await this.api.profileApi.getUinByUid(uid);
2228
+ if (uin !== null) map.set(uid, uin);
2229
+ } catch (err) {
2230
+ this.api.logger?.debug({ error: err instanceof Error ? err.message : String(err) }, "milky: uid→uin 资料兜底失败(该 uid 退 0)");
2231
+ }
2232
+ if (missing.length > MAX_PROFILE_LOOKUPS) this.api.logger?.debug({
2233
+ missing: missing.length,
2234
+ limit: MAX_PROFILE_LOOKUPS
2235
+ }, "milky: 待兜底 uid 超上限,其余保持未命中");
2204
2236
  }
2205
2237
  /** at uid → uin 批量转换(mention.user_id 用;失败 mention 段跳过)。 */
2206
2238
  async fillMentionUids(elements, ctx) {
@@ -2248,10 +2280,11 @@ var MilkyEventPipeline = class {
2248
2280
  /** 订阅 kernel 事件(消息 + 群 + 好友 + ,幂等;路由到 inbound/)。 */
2249
2281
  subscribe() {
2250
2282
  if (this.unsubscribes.length > 0) return;
2283
+ this.deps.request.markStartup();
2251
2284
  const { msgChannel, groupChannel, buddyChannel, recentContactChannel, loginChannel } = this.deps;
2252
2285
  this.unsubscribes.push(subscribeRawMessages(msgChannel, (msg) => this.deps.message.handle(msg).catch((err) => {
2253
2286
  this.logFailure("message_receive", err);
2254
- })), msgChannel.on("Msg/onRecvSysMsg", (a) => this.deps.sysMsg.handleRawArg(a)), msgChannel.on("Msg/onRecvOfflineFileMsg", (m) => this.deps.fileNotice.fileMessages(m)), msgChannel.on("Msg/onFileMsgCome", (m) => this.deps.fileNotice.fileMessages(m)));
2287
+ }), { logger: this.deps.logger }), msgChannel.on("Msg/onRecvSysMsg", (a) => this.deps.sysMsg.handleRawArg(a)), msgChannel.on("Msg/onRecvOfflineFileMsg", (m) => this.deps.fileNotice.fileMessages(m)), msgChannel.on("Msg/onFileMsgCome", (m) => this.deps.fileNotice.fileMessages(m)));
2255
2288
  if (groupChannel !== void 0) this.unsubscribes.push(groupChannel.on("Group/onGroupNotifiesUpdated", (doubt, notifies) => {
2256
2289
  this.deps.request.groupNotifies(doubt, notifies).catch((err) => {
2257
2290
  this.logFailure("group request", err);
@@ -2844,9 +2877,17 @@ function secondsOf(raw) {
2844
2877
  /** Milky 请求域处理器(群通知 / 好友申请的 uid 回填与广播)。 */
2845
2878
  var MilkyInboundRequest = class {
2846
2879
  deps;
2880
+ /** 已广播申请的 `${friendUid}:${reqTime}`(实例级,跨 reloadTransports 保留)。 */
2881
+ dedup = new BuddyReqDedup();
2882
+ /** 历史积压基准(订阅时刷新:重连后以此刻为界)。 */
2883
+ baselineSec = startupBaselineSec();
2847
2884
  constructor(deps) {
2848
2885
  this.deps = deps;
2849
2886
  }
2887
+ /** 刷新启动基准(pipeline subscribe 时调用)。 */
2888
+ markStartup() {
2889
+ this.baselineSec = startupBaselineSec();
2890
+ }
2850
2891
  /** 群通知通道:入群请求 / 成员邀请 / 邀请自身(uid→uin 批量后翻译)。 */
2851
2892
  async groupNotifies(doubt, notifies) {
2852
2893
  const ctx = await this.requestCtx(collectNotifyUids(notifies));
@@ -2860,17 +2901,27 @@ var MilkyInboundRequest = class {
2860
2901
  this.broadcastBuddyReqs(arg).catch((err) => logEventFailure(this.deps.logger, "milky: friend request 翻译失败(整批丢弃)", err));
2861
2902
  }
2862
2903
  async broadcastBuddyReqs(arg) {
2904
+ if (batchUnreadNums(arg) === 0) {
2905
+ this.deps.logger?.debug({}, "milky: onBuddyReqChange unreadNums=0(整批跳过)");
2906
+ return;
2907
+ }
2863
2908
  const reqs = narrowBuddyReqs(arg);
2864
2909
  if (reqs === null) {
2865
2910
  this.deps.logger?.warn({ raw: sliceRaw(JSON.stringify(arg)) }, "milky: onBuddyReqChange 未知参数形状");
2866
2911
  return;
2867
2912
  }
2868
- const ctx = await this.requestCtx(reqs.map((req) => req.friendUid));
2869
- for (const req of reqs) this.deps.broadcast(toFriendRequestEvent(req, ctx));
2870
- }
2871
- /** request 翻译上下文(uid→uin 批量转换;失败退空 Map)。 */
2913
+ const fresh = filterBuddyReqs(reqs, this.baselineSec).filter((req) => this.dedup.claim(req));
2914
+ if (fresh.length < reqs.length) this.deps.logger?.debug({
2915
+ total: reqs.length,
2916
+ kept: fresh.length
2917
+ }, "milky: 好友申请过滤(历史积压 / 已读 / 已处理 / 重复)");
2918
+ if (fresh.length === 0) return;
2919
+ const ctx = await this.requestCtx(fresh.map((req) => req.friendUid));
2920
+ for (const req of fresh) this.deps.broadcast(toFriendRequestEvent(req, ctx));
2921
+ }
2922
+ /** request 翻译上下文(uid→uin 批量转换 + 资料兜底;失败退空 Map)。 */
2872
2923
  async requestCtx(uids) {
2873
- const uidToUin = await this.deps.enrich.resolveUidToUin(uids);
2924
+ const uidToUin = await this.deps.enrich.resolveUidToUin(uids, { profileFallback: true });
2874
2925
  return {
2875
2926
  selfUin: this.deps.api.self.uin,
2876
2927
  ...uidToUin !== void 0 ? { uidToUin } : { uidToUin: /* @__PURE__ */ new Map() }