@napuketto/adapter 0.18.16 → 0.18.19

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.
Files changed (36) hide show
  1. package/dist/gray-tip-BL1rhwZK.mjs +619 -0
  2. package/dist/gray-tip-BL1rhwZK.mjs.map +1 -0
  3. package/dist/gray-tip-CcpfI1Og.d.mts +1 -0
  4. package/dist/{index-BlRIp4xi.d.mts → index-CKWWp8-H.d.mts} +3 -1
  5. package/dist/index-CKWWp8-H.d.mts.map +1 -0
  6. package/dist/{index-xj5Z4aAN.d.mts → index-DrPCnj64.d.mts} +4 -2
  7. package/dist/index-DrPCnj64.d.mts.map +1 -0
  8. package/dist/{index-BcQRoLtj.d.mts → index-tljZ0fmt.d.mts} +6 -2
  9. package/dist/index-tljZ0fmt.d.mts.map +1 -0
  10. package/dist/index.d.mts +3 -3
  11. package/dist/index.mjs +3 -3
  12. package/dist/media-cache-C_0C9eCV.d.mts +47 -0
  13. package/dist/media-cache-C_0C9eCV.d.mts.map +1 -0
  14. package/dist/milky/index.d.mts +1 -1
  15. package/dist/milky/index.mjs +1 -1
  16. package/dist/{milky-DsktXHch.mjs → milky-Dj45WP4r.mjs} +36 -24
  17. package/dist/milky-Dj45WP4r.mjs.map +1 -0
  18. package/dist/onebot11/index.d.mts +1 -1
  19. package/dist/onebot11/index.mjs +1 -1
  20. package/dist/{onebot11-CqHLrn9g.mjs → onebot11-CUK6blMn.mjs} +37 -22
  21. package/dist/onebot11-CUK6blMn.mjs.map +1 -0
  22. package/dist/satori/index.d.mts +1 -1
  23. package/dist/satori/index.mjs +1 -1
  24. package/dist/{satori-Bx8tkR9X.mjs → satori-D-C-e1_5.mjs} +30 -22
  25. package/dist/satori-D-C-e1_5.mjs.map +1 -0
  26. package/package.json +3 -3
  27. package/dist/gray-tip-BwsIxacB.mjs +0 -224
  28. package/dist/gray-tip-BwsIxacB.mjs.map +0 -1
  29. package/dist/gray-tip-DNo_Mi4E.d.mts +0 -10
  30. package/dist/gray-tip-DNo_Mi4E.d.mts.map +0 -1
  31. package/dist/index-BcQRoLtj.d.mts.map +0 -1
  32. package/dist/index-BlRIp4xi.d.mts.map +0 -1
  33. package/dist/index-xj5Z4aAN.d.mts.map +0 -1
  34. package/dist/milky-DsktXHch.mjs.map +0 -1
  35. package/dist/onebot11-CqHLrn9g.mjs.map +0 -1
  36. package/dist/satori-Bx8tkR9X.mjs.map +0 -1
@@ -2,5 +2,5 @@
2
2
  * OneBot 11 协议适配器(当前主战场)
3
3
  * 公共面:类型 + 动作注册表 + 配置 schema + 翻译层(adapter.ts 于 打通 kernel 事件后接入)。
4
4
  */
5
- import { $ as OB11MessageInfo, A as serializeCqParts, B as canonicalToCqMessage, C as ob11ConfigSchema, D as escapeCqParam, E as encodeCqCode, F as ob11ErrorCodeMap, G as CurrentTalkative, H as cqMessageToCanonical, I as OneBotApi, J as GroupInfo, K as FriendInfo, L as OneBotApiOptions, M as Ob11ActionDeps, N as createOb11ActionRegistry, O as escapeCqText, P as AtAllRemainInfo, Q as OB11Message, R as OneBotSystemOptions, S as OB11Config, T as CqCode, U as cqMessageToSegments, V as canonicalToSegments, W as segmentsToCanonical, X as HonorMember, Y as GroupMemberInfo, Z as LoginInfo, _ as OB11GroupMessageEvent, a as assembleOb11Transports, at as VersionInfo, b as OB11BaseEvent, c as OB11Event, d as OB11RequestEvent, et as OB11MessageSegment, f as OB11NoticeEvent, g as OB11Status, h as OB11MetaEvent, i as Ob11TransportSet, it as StrangerInfo, j as unescapeCqText, k as parseCqMessage, l as OB11FriendRequestEvent, m as OB11LifecycleMetaEvent, n as Ob11OutboundTransport, nt as Sender, o as NapukettoOneBot11Adapter, p as OB11HeartbeatMetaEvent, q as GroupHonorInfo, r as Ob11OutboundWsReconnect, rt as Sex, s as OneBot11AdapterOptions, t as AssembleOb11TransportsOptions, tt as OB11Return, u as OB11GroupRequestEvent, v as OB11MessageEvent, w as segmentsToCqMessage, x as OB11PostType, y as OB11PrivateMessageEvent, z as MessageUnique } from "../index-xj5Z4aAN.mjs";
5
+ import { $ as OB11MessageInfo, A as serializeCqParts, B as canonicalToCqMessage, C as ob11ConfigSchema, D as escapeCqParam, E as encodeCqCode, F as ob11ErrorCodeMap, G as CurrentTalkative, H as cqMessageToCanonical, I as OneBotApi, J as GroupInfo, K as FriendInfo, L as OneBotApiOptions, M as Ob11ActionDeps, N as createOb11ActionRegistry, O as escapeCqText, P as AtAllRemainInfo, Q as OB11Message, R as OneBotSystemOptions, S as OB11Config, T as CqCode, U as cqMessageToSegments, V as canonicalToSegments, W as segmentsToCanonical, X as HonorMember, Y as GroupMemberInfo, Z as LoginInfo, _ as OB11GroupMessageEvent, a as assembleOb11Transports, at as VersionInfo, b as OB11BaseEvent, c as OB11Event, d as OB11RequestEvent, et as OB11MessageSegment, f as OB11NoticeEvent, g as OB11Status, h as OB11MetaEvent, i as Ob11TransportSet, it as StrangerInfo, j as unescapeCqText, k as parseCqMessage, l as OB11FriendRequestEvent, m as OB11LifecycleMetaEvent, n as Ob11OutboundTransport, nt as Sender, o as NapukettoOneBot11Adapter, p as OB11HeartbeatMetaEvent, q as GroupHonorInfo, r as Ob11OutboundWsReconnect, rt as Sex, s as OneBot11AdapterOptions, t as AssembleOb11TransportsOptions, tt as OB11Return, u as OB11GroupRequestEvent, v as OB11MessageEvent, w as segmentsToCqMessage, x as OB11PostType, y as OB11PrivateMessageEvent, z as MessageUnique } from "../index-DrPCnj64.mjs";
6
6
  export { type AssembleOb11TransportsOptions, type AtAllRemainInfo, type CqCode, type CurrentTalkative, type FriendInfo, type GroupHonorInfo, type GroupInfo, type GroupMemberInfo, type HonorMember, type LoginInfo, MessageUnique, NapukettoOneBot11Adapter, type OB11BaseEvent, type OB11Config, type OB11Event, type OB11FriendRequestEvent, type OB11GroupMessageEvent, type OB11GroupRequestEvent, type OB11HeartbeatMetaEvent, type OB11LifecycleMetaEvent, type OB11Message, type OB11MessageEvent, type OB11MessageInfo, type OB11MessageSegment, type OB11MetaEvent, type OB11NoticeEvent, type OB11PostType, type OB11PrivateMessageEvent, type OB11RequestEvent, type OB11Return, type OB11Status, type Ob11ActionDeps, type Ob11OutboundTransport, type Ob11OutboundWsReconnect, type Ob11TransportSet, type OneBot11AdapterOptions, OneBotApi, type OneBotApiOptions, type OneBotSystemOptions, type Sender, type Sex, type StrangerInfo, type VersionInfo, assembleOb11Transports, canonicalToCqMessage, canonicalToSegments, cqMessageToCanonical, cqMessageToSegments, createOb11ActionRegistry, encodeCqCode, escapeCqParam, escapeCqText, ob11ConfigSchema, ob11ErrorCodeMap, parseCqMessage, segmentsToCanonical, segmentsToCqMessage, serializeCqParts, unescapeCqText };
@@ -1 +1 @@
1
- export { y as MessageUnique, t as NapukettoOneBot11Adapter, r as OneBotApi, n as assembleOb11Transports, a as canonicalToCqMessage, o as canonicalToSegments, s as cqMessageToCanonical, c as cqMessageToSegments, i as createOb11ActionRegistry, f as encodeCqCode, p as escapeCqParam, m as escapeCqText, u as ob11ConfigSchema, v as ob11ErrorCodeMap, h as parseCqMessage, l as segmentsToCanonical, d as segmentsToCqMessage, g as serializeCqParts, _ as unescapeCqText } from "../onebot11-CqHLrn9g.mjs";
1
+ export { y as MessageUnique, t as NapukettoOneBot11Adapter, r as OneBotApi, n as assembleOb11Transports, a as canonicalToCqMessage, o as canonicalToSegments, s as cqMessageToCanonical, c as cqMessageToSegments, i as createOb11ActionRegistry, f as encodeCqCode, p as escapeCqParam, m as escapeCqText, u as ob11ConfigSchema, v as ob11ErrorCodeMap, h as parseCqMessage, l as segmentsToCanonical, d as segmentsToCqMessage, g as serializeCqParts, _ as unescapeCqText } from "../onebot11-CUK6blMn.mjs";
@@ -1,14 +1,12 @@
1
1
  import { a as resolveProtocolConfig, d as runAction, i as BaseProtocolAdapter, o as BEARER_PREFIX, r as createServerSchema, s as createTokenAuthorize, u as ActionRegistry } from "./framework-BbtmlUXJ.mjs";
2
+ import { a as logEventFailure, c as downloadBufferToFile, d as prepareVideoElements, f as MediaCache, h as withMediaScope, i as hasGrayTip, l as ensureSilk, m as protectMediaPath, o as sliceRaw, p as cacheMediaFile, r as firstUntranslatedGrayTip, s as truncRaw, t as collectGrayTipUids, u as fileUriToPath } from "./gray-tip-BL1rhwZK.mjs";
2
3
  import { a as firstNonEmptyString, c as MS_TO_SEC, d as numericUin, f as resolveUidToUinWithFallback, i as toUnixSeconds, l as NUMERIC_RE, m as toUin, n as senderNickname, o as stringOrUndefined, r as subscribeRawMessages, s as startupBaselineSec, u as collectGroupNotifyUids } from "./raw-message-DU8YOhLV.mjs";
3
- import { a as logEventFailure, c as downloadBufferToFile, d as prepareVideoElements, i as hasGrayTip, l as ensureSilk, o as sliceRaw, r as firstUntranslatedGrayTip, s as truncRaw, t as collectGrayTipUids, u as fileUriToPath } from "./gray-tip-BwsIxacB.mjs";
4
4
  import { a as processSysMsgBlob, d as processInboundBuddyReqs, n as OBSERVED_UNNAMED_SYSMSG_KEYS, o as recognizeSysMsg, r as narrowSysMsgBlobs, t as extractEssenceSetSysMsg, u as BuddyReqDedup } from "./essence-Cn1oRaLw.mjs";
5
5
  import { ChatType, EssenceListSync, GrayTipSubType, GroupApi, GroupNotifyMsgStatus, GroupNotifyMsgType, NTGroupMemberRole, NTGroupRequestOperateTypes, TicketApi, TipGroupElementType, WebHonorType, errorMessage, hasStringProp, isRecord, kernelError, narrowEssenceGroupArg, toCanonicalElements } from "@napuketto/kernel";
6
- import { randomUUID } from "node:crypto";
7
6
  import { HttpClient, HttpServer, WsClient, WsServer } from "@napuketto/network";
8
7
  import { z } from "zod";
9
8
  import { existsSync } from "node:fs";
10
- import { mkdir } from "node:fs/promises";
11
- import { basename, join } from "node:path";
9
+ import { join } from "node:path";
12
10
  import { downloadUrl, inferExtension } from "@napuketto/media";
13
11
  // src/onebot11/helper/message-unique.ts
14
12
  /** OB11 message_id 上限(int32 max = 0x7FFF_FFFF)。 */
@@ -126,7 +124,7 @@ function defineOb11Action(spec) {
126
124
  const run = async (payload, invalidRetcode, echo) => {
127
125
  const outcome = await runAction({
128
126
  schema: spec.schema,
129
- handle: spec.handle,
127
+ handle: (input) => withMediaScope(() => spec.handle(input)),
130
128
  errorCodeMap: ob11ErrorCodeMap,
131
129
  invalidRetcode
132
130
  }, payload);
@@ -2156,12 +2154,10 @@ function resolveLocalMediaFile(relPath, deps) {
2156
2154
  async function downloadImageToCache(url, deps) {
2157
2155
  const cacheDir = deps.cacheDir;
2158
2156
  if (cacheDir === void 0 || cacheDir === "") return null;
2159
- const mediaDir = join(cacheDir, "media");
2160
- await mkdir(mediaDir, { recursive: true });
2161
- const dest = join(mediaDir, `${randomUUID()}${inferExtension(null, url)}`);
2162
2157
  try {
2163
- await downloadUrl(url, dest);
2164
- return dest;
2158
+ return await cacheMediaFile(cacheDir, inferExtension(null, url), async (dest) => {
2159
+ await downloadUrl(url, dest);
2160
+ }, "reference");
2165
2161
  } catch (err) {
2166
2162
  deps.logger?.warn({
2167
2163
  url,
@@ -2425,6 +2421,7 @@ async function sendOb11Message(payload, deps) {
2425
2421
  let canonical;
2426
2422
  if (Array.isArray(payload.message)) canonical = segmentsToCanonical(parseOB11MessageSegments(payload.message));
2427
2423
  else canonical = cqMessageToCanonical(payload.message);
2424
+ await protectElements(canonical);
2428
2425
  const atUins = [];
2429
2426
  for (const el of canonical) if (el.type === "at" && el.target !== "all") atUins.push(el.target);
2430
2427
  let uinToUid;
@@ -2456,6 +2453,12 @@ async function sendOb11Message(payload, deps) {
2456
2453
  await prepared.cleanup();
2457
2454
  }
2458
2455
  }
2456
+ async function protectElements(elements) {
2457
+ for (const el of elements) {
2458
+ if ("path" in el) await protectMediaPath(el.path);
2459
+ if (el.type === "video" && el.thumbPath) await protectMediaPath(el.thumbPath);
2460
+ }
2461
+ }
2459
2462
  /** CQ 特殊字符转义(auto_escape,&、[、]、, 前置 &)。 */
2460
2463
  function escapeText(text) {
2461
2464
  return text.replaceAll("&", "&").replaceAll("[", "[").replaceAll("]", "]").replaceAll(",", ",");
@@ -2528,9 +2531,9 @@ file: z.string() });
2528
2531
  /** URL 头像下载到缓存目录(cacheDir 缺省抛错)。 */
2529
2532
  async function downloadAvatarToCache(url, cacheDir) {
2530
2533
  if (cacheDir === void 0 || cacheDir === "") throw kernelError("set_qq_avatar 处理 URL 需要缓存目录(装配方未注入)", "INVALID_STATE");
2531
- const safeName = basename(new URL(url).pathname) || "avatar.bin";
2532
- const filePath = join(cacheDir, `avatar-${Date.now()}-${safeName}`);
2533
- return await downloadBufferToFile(url, filePath, void 0, "头像下载失败");
2534
+ return await cacheMediaFile(cacheDir, inferExtension(null, url), async (path) => {
2535
+ await downloadBufferToFile(url, path, void 0, "头像下载失败");
2536
+ });
2534
2537
  }
2535
2538
  /**
2536
2539
  * 账号票据与资料域动作工厂(cookies / clientkey / csrf_token / credentials /
@@ -2605,7 +2608,7 @@ function createSystemAccountActions(api) {
2605
2608
  let filePath = payload.file;
2606
2609
  if (URL_PREFIX.test(filePath)) filePath = await downloadAvatarToCache(filePath, api.cacheDir);
2607
2610
  if (!existsSync(filePath)) throw kernelError(`头像文件不存在: ${filePath}`, "NOT_FOUND");
2608
- await api.profileApi.setHeader(filePath);
2611
+ await api.profileApi.setHeader(await protectMediaPath(filePath));
2609
2612
  return null;
2610
2613
  }
2611
2614
  })
@@ -2672,19 +2675,18 @@ function createSystemFileActions(api) {
2672
2675
  handle: async (payload) => {
2673
2676
  const { cacheDir } = api;
2674
2677
  if (cacheDir === void 0 || cacheDir === "") throw kernelError("download_file 未配置缓存目录(装配方未注入)", "INVALID_STATE");
2675
- const safeName = basename(new URL(payload.url).pathname) || "download.bin";
2676
- const filePath = join(cacheDir, `download-${Date.now()}-${safeName}`);
2677
2678
  const init = {};
2678
2679
  if (payload.headers !== void 0) init.headers = payload.headers;
2679
- await downloadBufferToFile(payload.url, filePath, init);
2680
- return { file: filePath };
2680
+ return { file: await cacheMediaFile(cacheDir, inferExtension(null, payload.url), async (path) => {
2681
+ await downloadBufferToFile(payload.url, path, init);
2682
+ }, "reference") };
2681
2683
  }
2682
2684
  }),
2683
2685
  defineOb11Action({
2684
2686
  name: "upload_group_file",
2685
2687
  schema: uploadGroupFileSchema,
2686
2688
  handle: async (payload) => {
2687
- const path = resolveLocalFile(payload.file);
2689
+ const path = await protectMediaPath(resolveLocalFile(payload.file));
2688
2690
  const peer = {
2689
2691
  chatType: ChatType.GROUP,
2690
2692
  peerUid: String(payload.group_id)
@@ -2696,7 +2698,7 @@ function createSystemFileActions(api) {
2696
2698
  name: "upload_private_file",
2697
2699
  schema: uploadPrivateFileSchema,
2698
2700
  handle: async (payload) => {
2699
- const path = resolveLocalFile(payload.file);
2701
+ const path = await protectMediaPath(resolveLocalFile(payload.file));
2700
2702
  const peer = await resolveTargetPeer({ user_id: payload.user_id }, api, "upload_private_file 需要 user_id");
2701
2703
  return { file_id: (await api.flashTransferApi.sendFile(peer, [path], { name: payload.name ?? basename$1(path) })).fileSetId };
2702
2704
  }
@@ -2916,7 +2918,11 @@ const ACTION_FACTORIES = [
2916
2918
  /** 构建 OB11 动作注册表(81 个动作,领域工厂遍历注册)。 */
2917
2919
  function createOb11ActionRegistry(deps) {
2918
2920
  const registry = new ActionRegistry();
2919
- for (const factory of ACTION_FACTORIES) for (const action of factory(deps.api)) registry.register(action);
2921
+ for (const factory of ACTION_FACTORIES) for (const action of factory(deps.api)) registry.register({
2922
+ ...action,
2923
+ handle: (payload) => deps.api.mediaCache.run(() => action.handle(payload)),
2924
+ websocketHandle: (payload, echo) => deps.api.mediaCache.run(() => action.websocketHandle(payload, echo))
2925
+ });
2920
2926
  return registry;
2921
2927
  }
2922
2928
  // src/internal/infra/outbound-open.ts
@@ -3003,6 +3009,7 @@ function openOutboundWithRetry(transport, opts) {
3003
3009
  * 各动作经 `Pick<OneBotApi, ...>` 取所需成员;注册处只构造一次本对象。
3004
3010
  */
3005
3011
  var OneBotApi = class {
3012
+ mediaCache;
3006
3013
  /** kernel 消息 API(send_msg 等消息类动作用)。 */
3007
3014
  msgApi;
3008
3015
  /** kernel 群 API。 */
@@ -3046,6 +3053,10 @@ var OneBotApi = class {
3046
3053
  /** 降级分支可观测出口(缺省静默)。 */
3047
3054
  logger;
3048
3055
  constructor(opts) {
3056
+ this.mediaCache = new MediaCache({
3057
+ root: opts.system?.cacheDir,
3058
+ logger: opts.logger
3059
+ });
3049
3060
  this.msgApi = opts.msgApi;
3050
3061
  this.groupApi = opts.groupApi;
3051
3062
  this.groupNotifyApi = opts.groupNotifyApi;
@@ -3941,6 +3952,7 @@ var NapukettoOneBot11Adapter = class extends BaseProtocolAdapter {
3941
3952
  await Promise.all(this.transports.servers.map((s) => s.open()));
3942
3953
  await this.openOutboundTransports();
3943
3954
  }
3955
+ await this.oneBotApi.mediaCache.start();
3944
3956
  this.pipeline.subscribe();
3945
3957
  this.broadcastEvent({
3946
3958
  time: Math.floor(Date.now() / MS_TO_SEC),
@@ -3999,6 +4011,7 @@ var NapukettoOneBot11Adapter = class extends BaseProtocolAdapter {
3999
4011
  }
4000
4012
  /** 停止:心跳 + 传输 + 退订(含取消出向传输的后台重连)。 */
4001
4013
  async stopAll() {
4014
+ this.oneBotApi.mediaCache.stop();
4002
4015
  if (this.heartbeatTimer !== null) {
4003
4016
  clearInterval(this.heartbeatTimer);
4004
4017
  this.heartbeatTimer = null;
@@ -4017,12 +4030,14 @@ var NapukettoOneBot11Adapter = class extends BaseProtocolAdapter {
4017
4030
  */
4018
4031
  async subscribeOnly() {
4019
4032
  const config = await this.config.load();
4033
+ await this.oneBotApi.mediaCache.start();
4020
4034
  this.pipeline.applyConfig(config);
4021
4035
  this.subscribedOnly = true;
4022
4036
  this.pipeline.subscribe();
4023
4037
  }
4024
4038
  /** 仅退订(与 subscribeOnly 配对的进程级清理;传输关闭仍走 stop())。 */
4025
4039
  unsubscribeOnly() {
4040
+ this.oneBotApi.mediaCache.stop();
4026
4041
  this.subscribedOnly = false;
4027
4042
  this.pipeline.unsubscribeAll();
4028
4043
  }
@@ -4070,4 +4085,4 @@ function echoFieldOf(req) {
4070
4085
  }
4071
4086
  export { unescapeCqText as _, canonicalToCqMessage as a, cqMessageToSegments as c, segmentsToCqMessage as d, encodeCqCode as f, serializeCqParts as g, parseCqMessage as h, createOb11ActionRegistry as i, segmentsToCanonical as l, escapeCqText as m, assembleOb11Transports as n, canonicalToSegments as o, escapeCqParam as p, OneBotApi as r, cqMessageToCanonical as s, NapukettoOneBot11Adapter as t, ob11ConfigSchema as u, ob11ErrorCodeMap as v, MessageUnique as y };
4072
4087
 
4073
- //# sourceMappingURL=onebot11-CqHLrn9g.mjs.map
4088
+ //# sourceMappingURL=onebot11-CUK6blMn.mjs.map