@napuketto/kernel 0.16.3 → 0.17.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/index.d.mts CHANGED
@@ -2089,6 +2089,12 @@ export declare class MsgApi {
2089
2089
  emojiType: string;
2090
2090
  like: boolean;
2091
2091
  }): Promise<void>;
2092
+ /** 按原生消息 ID 添加或移除 QQ 系统表情表态。 */
2093
+ setMsgEmojiLikeById(target: Peer, { msgId, emojiId, like }: {
2094
+ msgId: string;
2095
+ emojiId: string;
2096
+ like: boolean;
2097
+ }): Promise<void>;
2092
2098
  /**
2093
2099
  * 语音转文字(fetch_ptt_text)。
2094
2100
  * 流程:按 msgId 拉消息 → 找 PTT 元素 → translatePtt2Text(异步转写)
@@ -3245,11 +3251,14 @@ interface CoreLoginOptions {
3245
3251
  * (qrFallback 可接)。底层 promise 无法取消:超时后悬挂丢弃。
3246
3252
  */
3247
3253
  quickLoginTimeoutMs?: number | undefined;
3248
- /** 快速登录失败时回退 QR 登录(二维码写缓存目录),默认 false。 */
3254
+ /** 快速登录失败时回退 QR 登录(二维码写账号 cache/qr 临时目录),默认 false。 */
3249
3255
  qrFallback?: boolean | undefined;
3250
3256
  /** 强制扫码:跳过快速登录,直接走 QR 登录(koishi 强制扫码 / 切账号)。默认 false。 */
3251
3257
  qrOnly?: boolean | undefined;
3252
- /** QR 二维码图片保存路径(默认缓存目录 qrcode.png)。 */
3258
+ /**
3259
+ * QR 二维码图片保存路径(显式指定时由调用方接管生命周期;默认
3260
+ * `<账号目录>/cache/qr/qrcode.png`,登录结束删除)。
3261
+ */
3253
3262
  qrCodePath?: string | undefined;
3254
3263
  /** 登录进度回调(QR 阶段:二维码数据 + 状态变化)。子进程 IPC 模式转发用。 */
3255
3264
  onLoginProgress?: ((progress: LoginProgress) => void) | undefined;
@@ -3269,6 +3278,8 @@ export declare class NapukettoCore {
3269
3278
  readonly ctx: CoreContext;
3270
3279
  /** QR 登录会话句柄(登录期间非空;登录终态后清空)。refreshQr() 据此可达。 */
3271
3280
  private qrSession;
3281
+ /** 本次登录默认落盘的临时二维码路径(显式 qrCodePath 不纳入自动清理)。 */
3282
+ private ephemeralQrCodePath;
3272
3283
  /** 由 createRuntime() 装配的运行时服务集合(stop/dispose 时统一清理)。 */
3273
3284
  private runtime;
3274
3285
  /** stop 幂等标记。 */
@@ -3339,7 +3350,7 @@ export declare class NapukettoCore {
3339
3350
  * koishi 前端「刷新二维码」按钮经 loader IPC `login.refreshQr` 动作直达。
3340
3351
  */
3341
3352
  refreshQr(): boolean;
3342
- /** QR 登录:QrLoginSession 状态机 + 二维码写缓存目录(无 UI,cli 可打印路径)。 */
3353
+ /** QR 登录:QrLoginSession 状态机 + 二维码写独占缓存目录(无 UI,cli 可打印路径)。 */
3343
3354
  private loginByQr;
3344
3355
  /** 订阅 QR 进度:二维码写盘 + 状态机经 onLoginProgress 转发。 */
3345
3356
  private subscribeQrProgress;
@@ -3350,6 +3361,8 @@ export declare class NapukettoCore {
3350
3361
  * 重复调用直接返回(宿主关停钩子可能同时触发多次)。
3351
3362
  */
3352
3363
  stop(): void;
3364
+ /** 删除本次登录默认落盘的临时二维码(显式 qrCodePath 不删)。幂等。 */
3365
+ private cleanupEphemeralQrCode;
3353
3366
  /** 显式释放(`using` / 手动调用的统一入口;等价 stop)。 */
3354
3367
  dispose(): void;
3355
3368
  /** `using core = ...` 自动释放。 */
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createHash, randomUUID } from "node:crypto";
2
- import { appendFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
2
+ import { appendFileSync, chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
3
3
  import { open, readFile, rename, rm, stat } from "node:fs/promises";
4
4
  import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
5
5
  import process from "node:process";
@@ -750,7 +750,10 @@ const SENSITIVE_FIELDS = /* @__PURE__ */ new Set([
750
750
  "skey",
751
751
  "p_skey",
752
752
  "csrf_token",
753
- "bkn"
753
+ "bkn",
754
+ "qrcodeurl",
755
+ "qr_code_url",
756
+ "qrcode_url"
754
757
  ]);
755
758
  /** 只遍历刚解析的 JSON 树;不会触碰业务对象、getter、toJSON 或循环引用。 */
756
759
  function redactFields(record) {
@@ -2345,6 +2348,8 @@ const SEND_STATUS = {
2345
2348
  };
2346
2349
  /** 发送确认超时(毫秒)。 */
2347
2350
  const SEND_CONFIRM_TIMEOUT_MS = 15e3;
2351
+ const QQ_EMOJI_ID_PATTERN = /^\d+$/;
2352
+ const MESSAGE_SEQ_PATTERN = /^[1-9]\d*$/;
2348
2353
  /**
2349
2354
  * PIC 元素发送契约值(NapCat 同款发送契约值,未逐字段校准)。
2350
2355
  * 下列固定值均为实测调通的 wrapper 发送契约(picType / picSubType /
@@ -2770,6 +2775,21 @@ var MsgApi = class {
2770
2775
  async setMsgEmojiLike(target, opts) {
2771
2776
  unwrapResult("setMsgEmojiLikes", await this.service.setMsgEmojiLikes(target, opts.msgSeq, opts.emojiId, opts.emojiType, opts.like));
2772
2777
  }
2778
+ /** 按原生消息 ID 添加或移除 QQ 系统表情表态。 */
2779
+ async setMsgEmojiLikeById(target, { msgId, emojiId, like }) {
2780
+ if (typeof msgId !== "string" || msgId.trim() === "") throw kernelError("缺少原生消息 ID", "INVALID_PARAM");
2781
+ if (typeof emojiId !== "string" || !QQ_EMOJI_ID_PATTERN.test(emojiId)) throw kernelError("表情 ID 必须是 QQ 系统表情索引的十进制字符串", "INVALID_PARAM");
2782
+ const message = (await this.fetchMsgsByMsgId(target, [msgId])).find((msg) => msg.msgId === msgId && msg.chatType === target.chatType && msg.peerUid === target.peerUid);
2783
+ if (message === void 0) throw kernelError("会话中找不到指定消息", "NOT_FOUND");
2784
+ const { msgSeq } = message;
2785
+ if (typeof msgSeq !== "string" || !MESSAGE_SEQ_PATTERN.test(msgSeq)) throw kernelError("消息缺少可用的序号", "INVALID_STATE");
2786
+ await this.setMsgEmojiLike(target, {
2787
+ msgSeq,
2788
+ emojiId,
2789
+ emojiType: "1",
2790
+ like
2791
+ });
2792
+ }
2773
2793
  /**
2774
2794
  * 语音转文字(fetch_ptt_text)。
2775
2795
  * 流程:按 msgId 拉消息 → 找 PTT 元素 → translatePtt2Text(异步转写)
@@ -5832,6 +5852,8 @@ var NapukettoCore = class NapukettoCore {
5832
5852
  ctx;
5833
5853
  /** QR 登录会话句柄(登录期间非空;登录终态后清空)。refreshQr() 据此可达。 */
5834
5854
  qrSession = null;
5855
+ /** 本次登录默认落盘的临时二维码路径(显式 qrCodePath 不纳入自动清理)。 */
5856
+ ephemeralQrCodePath = null;
5835
5857
  /** 由 createRuntime() 装配的运行时服务集合(stop/dispose 时统一清理)。 */
5836
5858
  runtime = null;
5837
5859
  /** stop 幂等标记。 */
@@ -6002,13 +6024,15 @@ var NapukettoCore = class NapukettoCore {
6002
6024
  this.qrSession.refresh();
6003
6025
  return true;
6004
6026
  }
6005
- /** QR 登录:QrLoginSession 状态机 + 二维码写缓存目录(无 UI,cli 可打印路径)。 */
6027
+ /** QR 登录:QrLoginSession 状态机 + 二维码写独占缓存目录(无 UI,cli 可打印路径)。 */
6006
6028
  async loginByQr(opts) {
6007
6029
  const { wrapper } = this.ctx;
6008
6030
  if (wrapper === null || wrapper.loginService === null) throw kernelError("wrapper/loginService 不可用,无法 QR 登录", "INVALID_STATE");
6009
6031
  const session = new QrLoginSession(wrapper.loginService);
6010
6032
  this.qrSession = session;
6011
- const qrPath = opts.qrCodePath ?? this.ctx.paths.file("cache", "qrcode.png");
6033
+ const explicitPath = opts.qrCodePath;
6034
+ const qrPath = explicitPath ?? this.ctx.paths.file("cache", QR_CODE_DIR, QR_CODE_FILENAME);
6035
+ this.ephemeralQrCodePath = explicitPath === void 0 ? qrPath : null;
6012
6036
  this.subscribeQrProgress(session, opts, qrPath);
6013
6037
  try {
6014
6038
  session.start();
@@ -6022,12 +6046,13 @@ var NapukettoCore = class NapukettoCore {
6022
6046
  } finally {
6023
6047
  session.stop();
6024
6048
  this.qrSession = null;
6049
+ this.cleanupEphemeralQrCode();
6025
6050
  }
6026
6051
  }
6027
6052
  /** 订阅 QR 进度:二维码写盘 + 状态机经 onLoginProgress 转发。 */
6028
6053
  subscribeQrProgress(session, opts, qrPath) {
6029
6054
  session.onQrCode((qr) => {
6030
- this.ctx.logger.warn(`请扫描二维码登录(保存: ${qrPath} | URL: ${qr.qrcodeUrl})`);
6055
+ this.ctx.logger.warn(`请扫描二维码登录(图片: ${qrPath})`);
6031
6056
  if (qr.pngBase64 !== "") writeQrCodePng(qrPath, qr.pngBase64);
6032
6057
  opts.onLoginProgress?.({
6033
6058
  state: "waiting_scan",
@@ -6053,10 +6078,17 @@ var NapukettoCore = class NapukettoCore {
6053
6078
  this.stopped = true;
6054
6079
  this.qrSession?.stop();
6055
6080
  this.qrSession = null;
6081
+ this.cleanupEphemeralQrCode();
6056
6082
  this.runtime?.dispose();
6057
6083
  this.runtime = null;
6058
6084
  this.ctx.logger.info("NapukettoCore 停止");
6059
6085
  }
6086
+ /** 删除本次登录默认落盘的临时二维码(显式 qrCodePath 不删)。幂等。 */
6087
+ cleanupEphemeralQrCode() {
6088
+ const path = this.ephemeralQrCodePath;
6089
+ this.ephemeralQrCodePath = null;
6090
+ if (path !== null) removeQrCodePng(path);
6091
+ }
6060
6092
  /** 显式释放(`using` / 手动调用的统一入口;等价 stop)。 */
6061
6093
  dispose() {
6062
6094
  this.stop();
@@ -6104,11 +6136,33 @@ function resolveSessionAccountPath(ctx, wrapper) {
6104
6136
  function hasInitConfig(val) {
6105
6137
  return typeof val["initConfig"] === "function";
6106
6138
  }
6107
- /** 写二维码 png 到磁盘(base64 解码,供 cli/用户扫码)。 */
6139
+ /** 二维码 png 默认落盘目录(账号 cache 下的独占子目录)。 */
6140
+ const QR_CODE_DIR = "qr";
6141
+ /** 二维码 png 默认文件名。 */
6142
+ const QR_CODE_FILENAME = "qrcode.png";
6143
+ /**
6144
+ * 写二维码 png 到磁盘(base64 解码,供 cli/用户扫码)。
6145
+ *
6146
+ * 安全边界:目录按 0o700 创建、文件按 0o600 写入;写入前拒绝符号链接,避免被
6147
+ * 预置链接引到其他文件。失败不阻塞登录(cli 仍可打印 URL / 终端渲染)。
6148
+ */
6108
6149
  function writeQrCodePng(filePath, pngBase64) {
6109
6150
  try {
6110
- mkdirSync(dirname(filePath), { recursive: true });
6111
- writeFileSync(filePath, Buffer.from(pngBase64, "base64"));
6151
+ mkdirSync(dirname(filePath), {
6152
+ recursive: true,
6153
+ mode: 448
6154
+ });
6155
+ try {
6156
+ if (lstatSync(filePath).isSymbolicLink()) return;
6157
+ } catch {}
6158
+ writeFileSync(filePath, Buffer.from(pngBase64, "base64"), { mode: 384 });
6159
+ chmodSync(filePath, 384);
6160
+ } catch {}
6161
+ }
6162
+ /** 删除默认落盘的二维码文件(不存在 / 权限失败静默)。 */
6163
+ function removeQrCodePng(filePath) {
6164
+ try {
6165
+ unlinkSync(filePath);
6112
6166
  } catch {}
6113
6167
  }
6114
6168
  /** CoreLoginOptions → quickLogin 参数(仅取快速登录相关两键,QR 键不透传)。 */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@napuketto/kernel",
4
- "version": "0.16.3",
4
+ "version": "0.17.0",
5
5
  "type": "module",
6
6
  "description": "Core runtime kernel of NapukettoQQ",
7
7
  "keywords": [