@napuketto/adapter 0.18.14 → 0.18.18

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 (44) hide show
  1. package/dist/core/index.mjs +1 -1
  2. package/dist/{framework-Br7Tfx20.mjs → framework-BbtmlUXJ.mjs} +28 -5
  3. package/dist/framework-BbtmlUXJ.mjs.map +1 -0
  4. package/dist/gray-tip-BL1rhwZK.mjs +619 -0
  5. package/dist/gray-tip-BL1rhwZK.mjs.map +1 -0
  6. package/dist/gray-tip-CcpfI1Og.d.mts +1 -0
  7. package/dist/index-BZfGz6FA.d.mts.map +1 -1
  8. package/dist/{index-BlRIp4xi.d.mts → index-CKWWp8-H.d.mts} +3 -1
  9. package/dist/index-CKWWp8-H.d.mts.map +1 -0
  10. package/dist/{index-xj5Z4aAN.d.mts → index-DrPCnj64.d.mts} +4 -2
  11. package/dist/index-DrPCnj64.d.mts.map +1 -0
  12. package/dist/{index-f9U_MMJB.d.mts → index-tljZ0fmt.d.mts} +8 -2
  13. package/dist/index-tljZ0fmt.d.mts.map +1 -0
  14. package/dist/index.d.mts +3 -3
  15. package/dist/index.mjs +5 -5
  16. package/dist/kurobot/index.mjs +1 -1
  17. package/dist/{kurobot-Gb0VmKZf.mjs → kurobot-BUJkleqO.mjs} +3 -3
  18. package/dist/{kurobot-Gb0VmKZf.mjs.map → kurobot-BUJkleqO.mjs.map} +1 -1
  19. package/dist/media-cache-C_0C9eCV.d.mts +47 -0
  20. package/dist/media-cache-C_0C9eCV.d.mts.map +1 -0
  21. package/dist/milky/index.d.mts +1 -1
  22. package/dist/milky/index.mjs +1 -1
  23. package/dist/{milky-B9YPk8QN.mjs → milky-KOD9KyeP.mjs} +35 -23
  24. package/dist/milky-KOD9KyeP.mjs.map +1 -0
  25. package/dist/onebot11/index.d.mts +1 -1
  26. package/dist/onebot11/index.mjs +1 -1
  27. package/dist/{onebot11-ZYyghjp_.mjs → onebot11-CUK6blMn.mjs} +38 -23
  28. package/dist/onebot11-CUK6blMn.mjs.map +1 -0
  29. package/dist/satori/index.d.mts +1 -1
  30. package/dist/satori/index.mjs +1 -1
  31. package/dist/{satori-C1RaFrGq.mjs → satori-Dl9iaDNY.mjs} +53 -27
  32. package/dist/satori-Dl9iaDNY.mjs.map +1 -0
  33. package/package.json +3 -3
  34. package/dist/framework-Br7Tfx20.mjs.map +0 -1
  35. package/dist/gray-tip-BwsIxacB.mjs +0 -224
  36. package/dist/gray-tip-BwsIxacB.mjs.map +0 -1
  37. package/dist/gray-tip-DNo_Mi4E.d.mts +0 -10
  38. package/dist/gray-tip-DNo_Mi4E.d.mts.map +0 -1
  39. package/dist/index-BlRIp4xi.d.mts.map +0 -1
  40. package/dist/index-f9U_MMJB.d.mts.map +0 -1
  41. package/dist/index-xj5Z4aAN.d.mts.map +0 -1
  42. package/dist/milky-B9YPk8QN.mjs.map +0 -1
  43. package/dist/onebot11-ZYyghjp_.mjs.map +0 -1
  44. package/dist/satori-C1RaFrGq.mjs.map +0 -1
@@ -1,13 +1,11 @@
1
- import { a as resolveProtocolConfig, c as matchTokenCredential, i as BaseProtocolAdapter, l as ActionRegistry, o as BEARER_PREFIX, s as createTokenAuthorize, u as runAction } from "./framework-Br7Tfx20.mjs";
2
- import { a as logEventFailure, i as hasGrayTip, n as findRevokeElement, o as sliceRaw, r as firstUntranslatedGrayTip, t as collectGrayTipUids, u as fileUriToPath } from "./gray-tip-BwsIxacB.mjs";
1
+ import { a as resolveProtocolConfig, c as matchTokenCredential, d as runAction, i as BaseProtocolAdapter, o as BEARER_PREFIX, s as createTokenAuthorize, u as ActionRegistry } from "./framework-BbtmlUXJ.mjs";
2
+ import { a as logEventFailure, f as MediaCache, h as withMediaScope, i as hasGrayTip, m as protectMediaPath, n as findRevokeElement, o as sliceRaw, p as cacheMediaFile, r as firstUntranslatedGrayTip, t as collectGrayTipUids, u as fileUriToPath } from "./gray-tip-BL1rhwZK.mjs";
3
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, o as stringOrUndefined, p as resolveUin, r as subscribeRawMessages, s as startupBaselineSec, t as forEachRawMessage } from "./raw-message-DU8YOhLV.mjs";
4
4
  import { a as processSysMsgBlob, c as varintAt, d as processInboundBuddyReqs, i as nodeAt, l as varintOf, n as OBSERVED_UNNAMED_SYSMSG_KEYS, o as recognizeSysMsg, r as narrowSysMsgBlobs, s as textOf, t as extractEssenceSetSysMsg, u as BuddyReqDedup } from "./essence-Cn1oRaLw.mjs";
5
5
  import { ChatType, EssenceListSync, GrayTipSubType, GroupApi, GroupNotifyMsgStatus, GroupNotifyMsgType, NTGroupMemberRole, NTGroupRequestOperateTypes, TicketApi, TipGroupElementType, errorMessage, hasNumberProp, hasStringProp, isRecord, kernelError, narrowEssenceGroupArg, toCanonicalElements } from "@napuketto/kernel";
6
6
  import { HttpClient, HttpServer, createSseResponse, isWsOpen } from "@napuketto/network";
7
7
  import { z } from "zod";
8
- import { randomUUID } from "node:crypto";
9
- import { mkdir, rename, writeFile } from "node:fs/promises";
10
- import { join } from "node:path";
8
+ import { rename, writeFile } from "node:fs/promises";
11
9
  import { detectFileType, downloadUrl, inferExtension } from "@napuketto/media";
12
10
  // src/milky/action/base-action.ts
13
11
  /** KernelErrorCode → Milky retcode 映射表(-400/-403 规范语义,其余取常见 HTTP 语义近似负数)。 */
@@ -35,7 +33,7 @@ function defineMilkyAction(spec) {
35
33
  handle: async (payload) => {
36
34
  const outcome = await runAction({
37
35
  schema: spec.schema,
38
- handle: spec.handle,
36
+ handle: (input) => withMediaScope(() => spec.handle(input)),
39
37
  errorCodeMap: milkyErrorCodeMap,
40
38
  invalidRetcode: -400,
41
39
  invalidMessage: (error) => `参数解析失败: ${zodSummary(error)}`
@@ -328,13 +326,14 @@ function essenceToEntity(item, groupId) {
328
326
  * - http(s):// → @napuketto/media downloadUrl 落盘 cacheDir/milky/
329
327
  * 未配置 mediaDir 时远程 URI 报错(不发静默空消息)。
330
328
  */
329
+ const DETECTED_EXTENSION_RE = /^[a-zA-Z0-9]+$/;
331
330
  /** 解析远程媒体 URI 为本地绝对路径(base64:// / http(s)://;file:// 走同步面)。 */
332
331
  async function resolveMediaUri(uri, mediaDir) {
333
332
  if (mediaDir === void 0 || mediaDir === "") throw kernelError("远程媒体发送需要配置 [accounts.milky] cacheDir(下载缓存目录)", "INVALID_PARAM");
334
333
  if (uri.startsWith("base64://")) return await writeBase64(uri.slice(9), mediaDir);
335
- const dest = join(mediaDir, `${randomUUID()}${inferExtension(null, uri)}`);
336
- await downloadUrl(uri, dest);
337
- return dest;
334
+ return await cacheMediaFile(mediaDir, inferExtension(null, uri), async (dest) => {
335
+ await downloadUrl(uri, dest);
336
+ });
338
337
  }
339
338
  /**
340
339
  * 动作参数 URI(image_uri / file_uri)→ 本地路径:file:// 同步展开(fileURLToPath
@@ -342,9 +341,9 @@ async function resolveMediaUri(uri, mediaDir) {
342
341
  * resolveMediaUri 落盘;裸路径(go-cqhttp 惯例)原样。
343
342
  */
344
343
  async function resolveUriToPath(uri, mediaDir) {
345
- if (uri.startsWith("file://")) return fileUriToPath(uri);
344
+ if (uri.startsWith("file://")) return await protectMediaPath(fileUriToPath(uri));
346
345
  if (uri.startsWith("http://") || uri.startsWith("https://") || uri.startsWith("base64://")) return await resolveMediaUri(uri, mediaDir);
347
- return uri;
346
+ return await protectMediaPath(uri);
348
347
  }
349
348
  /** base64 载荷 → 落盘(先写 .bin,探测到扩展名再改名)。 */
350
349
  async function writeBase64(payload, mediaDir) {
@@ -355,17 +354,19 @@ async function writeBase64(payload, mediaDir) {
355
354
  throw kernelError("base64:// 载荷解码失败", "INVALID_PARAM");
356
355
  }
357
356
  if (bytes.length === 0) throw kernelError("base64:// 载荷为空", "INVALID_PARAM");
358
- await mkdir(mediaDir, { recursive: true });
359
- const raw = join(mediaDir, `${randomUUID()}.bin`);
360
- await writeFile(raw, bytes);
361
- try {
362
- const { ext } = await detectFileType(raw);
363
- const final = join(mediaDir, `${randomUUID()}${ext}`);
364
- await rename(raw, final);
357
+ return await cacheMediaFile(mediaDir, ".bin", async (raw) => {
358
+ await writeFile(raw, bytes, { flag: "wx" });
359
+ let ext;
360
+ try {
361
+ ({ext} = await detectFileType(raw));
362
+ } catch {
363
+ return raw;
364
+ }
365
+ if (!DETECTED_EXTENSION_RE.test(ext)) return raw;
366
+ const final = `${raw.slice(0, -4)}.${ext}`;
367
+ if (final !== raw) await rename(raw, final);
365
368
  return final;
366
- } catch {
367
- return raw;
368
- }
369
+ });
369
370
  }
370
371
  // src/milky/action/group-file.ts
371
372
  const fileIdField = { file_id: z.string().min(1) };
@@ -536,6 +537,7 @@ function folderToEntity(info, groupId) {
536
537
  }
537
538
  // src/milky/api.ts
538
539
  var MilkyApi = class {
540
+ mediaCache;
539
541
  msgApi;
540
542
  groupApi;
541
543
  friendApi;
@@ -558,6 +560,10 @@ var MilkyApi = class {
558
560
  /** message_seq ↔ msgId 双向映射(接收链路 record,动作侧反查)。 */
559
561
  seqIndex;
560
562
  constructor(opts, seqIndex) {
563
+ this.mediaCache = new MediaCache({
564
+ root: opts.mediaDir,
565
+ logger: opts.logger
566
+ });
561
567
  this.msgApi = opts.msgApi;
562
568
  this.groupApi = opts.groupApi;
563
569
  this.friendApi = opts.friendApi;
@@ -1913,6 +1919,7 @@ async function resolveElement(el, uinToUid, api) {
1913
1919
  ...el,
1914
1920
  path: await resolveMediaUri(el.path, api.mediaDir)
1915
1921
  };
1922
+ if ("path" in el) await protectMediaPath(el.path);
1916
1923
  return el;
1917
1924
  }
1918
1925
  /** 远程媒体元素(image/voice/video 且 path 为需落盘的 URI)。 */
@@ -2228,7 +2235,10 @@ const ACTION_FACTORIES = [
2228
2235
  /** 构建 Milky 动作注册表(64 个动作,领域工厂遍历注册)。 */
2229
2236
  function createMilkyActionRegistry(opts) {
2230
2237
  const registry = new ActionRegistry();
2231
- for (const factory of ACTION_FACTORIES) for (const action of factory(opts.api)) registry.register(action);
2238
+ for (const factory of ACTION_FACTORIES) for (const action of factory(opts.api)) registry.register({
2239
+ ...action,
2240
+ handle: (payload) => opts.api.mediaCache.run(() => action.handle(payload))
2241
+ });
2232
2242
  return registry;
2233
2243
  }
2234
2244
  // src/milky/config.ts
@@ -3377,6 +3387,7 @@ var NapukettoMilkyAdapter = class extends BaseProtocolAdapter {
3377
3387
  this.stopTransports = set.close;
3378
3388
  await Promise.all(set.adapters.map((a) => a.open()));
3379
3389
  }
3390
+ await this.api.mediaCache.start();
3380
3391
  this.pipeline.subscribe();
3381
3392
  this.calibLogger?.info({
3382
3393
  host: config.host,
@@ -3391,6 +3402,7 @@ var NapukettoMilkyAdapter = class extends BaseProtocolAdapter {
3391
3402
  }
3392
3403
  /** 停止:退订 + 关传输。 */
3393
3404
  async stopAll() {
3405
+ this.api.mediaCache.stop();
3394
3406
  this.pipeline.unsubscribeAll();
3395
3407
  if (this.stopTransports !== null) {
3396
3408
  await this.stopTransports();
@@ -3400,4 +3412,4 @@ var NapukettoMilkyAdapter = class extends BaseProtocolAdapter {
3400
3412
  };
3401
3413
  export { MilkyApi as a, createMilkyActionRegistry as i, assembleMilkyTransports as n, milkyErrorCodeMap as o, milkyConfigSchema as r, NapukettoMilkyAdapter as t };
3402
3414
 
3403
- //# sourceMappingURL=milky-B9YPk8QN.mjs.map
3415
+ //# sourceMappingURL=milky-KOD9KyeP.mjs.map