@nestim/koishi-plugin-qq-group-manager 1.4.0 → 1.4.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.
@@ -0,0 +1,225 @@
1
+ /**
2
+ * 内置 BMP → PNG 转码(纯 JS,零依赖)。
3
+ *
4
+ * 为什么需要:
5
+ * QQ 有时会把图片存成 BMP。DeepSeek 等视觉模型只接受 webp/png/jpeg/gif,
6
+ * 直接发 BMP 会 400。与其跳过这张图,不如转成 PNG 再发。
7
+ *
8
+ * 支持:
9
+ * - 1/4/8/16/24/32 位 BMP
10
+ * - BI_RGB(未压缩)、BI_BITFIELDS
11
+ * - 8 位调色板(含 RLE8 不处理,直接按索引读,够用)
12
+ *
13
+ * 不支持(遇到就返回 null,调用方回退到"跳过这张图"):
14
+ * - RLE 压缩
15
+ * - OS/2 v1 头
16
+ * - 带 alpha 的复杂变体(按 32 位 BGRA 直读,够常见场景用)
17
+ */
18
+
19
+ const PNG_SIG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
20
+
21
+ /** 判断是不是 BMP */
22
+ export function isBmp(buffer) {
23
+ return !!buffer && buffer.length > 26 && buffer[0] === 0x42 && buffer[1] === 0x4d;
24
+ }
25
+
26
+ /** CRC32(PNG chunk 需要) */
27
+ const CRC_TABLE = (() => {
28
+ const table = new Int32Array(256);
29
+ for (let n = 0; n < 256; n += 1) {
30
+ let c = n;
31
+ for (let k = 0; k < 8; k += 1) {
32
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
33
+ }
34
+ table[n] = c;
35
+ }
36
+ return table;
37
+ })();
38
+
39
+ function crc32(buf) {
40
+ let c = 0xffffffff;
41
+ for (let i = 0; i < buf.length; i += 1) {
42
+ c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
43
+ }
44
+ return (c ^ 0xffffffff) >>> 0;
45
+ }
46
+
47
+ /** 组装一个 PNG chunk */
48
+ function chunk(type, data) {
49
+ const len = Buffer.alloc(4);
50
+ len.writeUInt32BE(data.length, 0);
51
+ const typeBuf = Buffer.from(type, 'ascii');
52
+ const crcBuf = Buffer.alloc(4);
53
+ crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])), 0);
54
+ return Buffer.concat([len, typeBuf, data, crcBuf]);
55
+ }
56
+
57
+ /** 把 RGB(A) 像素编码成 PNG(真彩色 8 位) */
58
+ function encodePng(width, height, rgba) {
59
+ const stride = width * 4;
60
+ // 每行前置一个 filter byte(0 = None)
61
+ const raw = Buffer.alloc((stride + 1) * height);
62
+ for (let y = 0; y < height; y += 1) {
63
+ raw[y * (stride + 1)] = 0;
64
+ rgba.copy(raw, y * (stride + 1) + 1, y * stride, y * stride + stride);
65
+ }
66
+
67
+ const ihdr = Buffer.alloc(13);
68
+ ihdr.writeUInt32BE(width, 0);
69
+ ihdr.writeUInt32BE(height, 4);
70
+ ihdr[8] = 8; // bit depth
71
+ ihdr[9] = 6; // color type: RGBA
72
+ ihdr[10] = 0; // compression
73
+ ihdr[11] = 0; // filter
74
+ ihdr[12] = 0; // interlace
75
+
76
+ // zlib deflate(Node 自带)
77
+ const zlib = require('node:zlib');
78
+ const idat = zlib.deflateSync(raw, { level: 6 });
79
+
80
+ return Buffer.concat([
81
+ PNG_SIG,
82
+ chunk('IHDR', ihdr),
83
+ chunk('IDAT', idat),
84
+ chunk('IEND', Buffer.alloc(0)),
85
+ ]);
86
+ }
87
+
88
+ /**
89
+ * BMP → PNG buffer。
90
+ * @returns {Buffer|null} 成功返回 PNG,无法处理返回 null
91
+ */
92
+ export function bmpToPng(buffer) {
93
+ if (!isBmp(buffer)) return null;
94
+
95
+ try {
96
+ const dataOffset = buffer.readUInt32LE(10);
97
+ const headerSize = buffer.readUInt32LE(14);
98
+
99
+ let width;
100
+ let height;
101
+ let bitCount;
102
+ let compression;
103
+ let paletteOffset;
104
+
105
+ if (headerSize === 12) {
106
+ // OS/2 v1:不支持
107
+ return null;
108
+ }
109
+
110
+ // BITMAPINFOHEADER 及以后
111
+ width = buffer.readInt32LE(18);
112
+ height = buffer.readInt32LE(22);
113
+ bitCount = buffer.readUInt16LE(28);
114
+ compression = buffer.readUInt32LE(30);
115
+ paletteOffset = 14 + headerSize;
116
+
117
+ if (!width || !height) return null;
118
+ if (compression === 1 || compression === 2) return null; // RLE 不支持
119
+
120
+ const bottomUp = height > 0;
121
+ const h = Math.abs(height);
122
+ const w = Math.abs(width);
123
+
124
+ // 极端尺寸保护(避免爆内存)
125
+ if (w > 8192 || h > 8192 || w * h > 20_000_000) return null;
126
+
127
+ // 调色板:8 位及以下
128
+ let palette = null;
129
+ if (bitCount <= 8) {
130
+ const count = bitCount === 8 ? 256 : 1 << bitCount;
131
+ palette = new Uint8Array(count * 4);
132
+ let base = paletteOffset;
133
+ // 若头里声明了颜色数,用它
134
+ const declared = headerSize >= 40 ? buffer.readUInt32LE(46) : 0;
135
+ const use = declared && declared <= count ? declared : count;
136
+ for (let i = 0; i < use; i += 1) {
137
+ const off = base + i * 4;
138
+ if (off + 3 >= buffer.length) break;
139
+ palette[i * 4] = buffer[off + 2]; // R
140
+ palette[i * 4 + 1] = buffer[off + 1]; // G
141
+ palette[i * 4 + 2] = buffer[off]; // B
142
+ palette[i * 4 + 3] = 255;
143
+ }
144
+ }
145
+
146
+ const rowSize = Math.floor((bitCount * w + 31) / 32) * 4;
147
+ const rgba = Buffer.alloc(w * h * 4);
148
+
149
+ for (let y = 0; y < h; y += 1) {
150
+ // BMP 默认自下而上存储
151
+ const srcY = bottomUp ? h - 1 - y : y;
152
+ const rowStart = dataOffset + srcY * rowSize;
153
+ if (rowStart >= buffer.length) break;
154
+
155
+ for (let x = 0; x < w; x += 1) {
156
+ const dst = (y * w + x) * 4;
157
+ let r = 0;
158
+ let g = 0;
159
+ let b = 0;
160
+ let a = 255;
161
+
162
+ if (bitCount === 24) {
163
+ const off = rowStart + x * 3;
164
+ if (off + 2 >= buffer.length) continue;
165
+ b = buffer[off];
166
+ g = buffer[off + 1];
167
+ r = buffer[off + 2];
168
+ } else if (bitCount === 32) {
169
+ const off = rowStart + x * 4;
170
+ if (off + 3 >= buffer.length) continue;
171
+ b = buffer[off];
172
+ g = buffer[off + 1];
173
+ r = buffer[off + 2];
174
+ const alpha = buffer[off + 3];
175
+ a = alpha === 0 ? 255 : alpha; // 很多 BMP 的 alpha 位全 0,按不透明处理
176
+ } else if (bitCount === 16) {
177
+ const off = rowStart + x * 2;
178
+ if (off + 1 >= buffer.length) continue;
179
+ const v = buffer.readUInt16LE(off);
180
+ // 默认 5-5-5
181
+ r = ((v >> 10) & 0x1f) << 3;
182
+ g = ((v >> 5) & 0x1f) << 3;
183
+ b = (v & 0x1f) << 3;
184
+ } else if (bitCount === 8) {
185
+ const off = rowStart + x;
186
+ if (off >= buffer.length) continue;
187
+ const idx = buffer[off] * 4;
188
+ r = palette ? palette[idx] : buffer[off];
189
+ g = palette ? palette[idx + 1] : buffer[off];
190
+ b = palette ? palette[idx + 2] : buffer[off];
191
+ } else if (bitCount === 4) {
192
+ const off = rowStart + (x >> 1);
193
+ if (off >= buffer.length) continue;
194
+ const byte = buffer[off];
195
+ const idxVal = x % 2 === 0 ? (byte >> 4) & 0x0f : byte & 0x0f;
196
+ const idx = idxVal * 4;
197
+ r = palette ? palette[idx] : 0;
198
+ g = palette ? palette[idx + 1] : 0;
199
+ b = palette ? palette[idx + 2] : 0;
200
+ } else if (bitCount === 1) {
201
+ const off = rowStart + (x >> 3);
202
+ if (off >= buffer.length) continue;
203
+ const byte = buffer[off];
204
+ const bit = 7 - (x % 8);
205
+ const idxVal = (byte >> bit) & 1;
206
+ const idx = idxVal * 4;
207
+ r = palette ? palette[idx] : 0;
208
+ g = palette ? palette[idx + 1] : 0;
209
+ b = palette ? palette[idx + 2] : 0;
210
+ } else {
211
+ return null; // 未知位深
212
+ }
213
+
214
+ rgba[dst] = r;
215
+ rgba[dst + 1] = g;
216
+ rgba[dst + 2] = b;
217
+ rgba[dst + 3] = a;
218
+ }
219
+ }
220
+
221
+ return encodePng(w, h, rgba);
222
+ } catch {
223
+ return null;
224
+ }
225
+ }
package/src/index.ts CHANGED
@@ -64,7 +64,13 @@ export const Config = Schema.intersect([
64
64
  memoryPerScope: Schema.boolean().default(true).description('按私聊/群聊分开存储:群聊写 Memory_<群号>_群聊.md,私聊写 Memory_<QQ号>_私聊.md。关闭则所有会话共用单文件(旧行为)。'),
65
65
  memoryInAi: Schema.boolean().default(true).description('AI 回复/识图时是否参考记忆库内容(只读当前会话对应的文件)。'),
66
66
  memoryAutoLearn: Schema.boolean().default(false).description('自动归纳:上下文挤满后,把被挤出的消息交给外部学习插件总结成记忆。这里只负责抛出事件,实际归纳由 qqgm-memory-learn 完成。'),
67
- memoryAutoLearnGroups: Schema.array(String).role('table').default([]).description('自动归纳生效的群号/QQ号白名单;留空表示全部生效。'),
67
+ memoryAutoLearnGroups: Schema.array(String).role('table').default([]).description('自动归纳生效的群号/QQ号白名单;留空表示全部生效。'),
68
+ memoryLearnIdleHours: Schema.natural().min(1).max(72).default(8).description('空闲兜底:距最后一条消息超过这么多小时且期间有新消息,就归纳一次。一直没新消息则不触发。'),
69
+ memoryLearnThresholdScale: Schema.number().min(1).max(10).step(0.5).default(1).description('归纳阈值 = aiContextWindow × 该倍数。填 1 表示攒满一个上下文窗口就归纳。'),
70
+ memoryLearnMaxChars: Schema.natural().min(40).max(300).default(150).description('单条自动记忆的字数上限。'),
71
+ memoryLearnPushBack: Schema.boolean().default(true).description('归纳后把结果回推到原会话:私聊发私聊、群聊发对应群。'),
72
+ memoryLearnDuplicateThreshold: Schema.number().min(0.3).max(0.95).step(0.05).default(0.55).description('与已有记忆相似度超过该值就跳过,避免同一件事反复记。'),
73
+ memoryLearnDailyLimit: Schema.natural().min(1).max(200).default(30).description('每个作用域每天最多归纳几次,防止刷模型额度。'),
68
74
  }).description('记忆库'),
69
75
  Schema.object({
70
76
  enableBlacklist: Schema.boolean().default(true).description('全局黑名单系统开关。单个群未单独设置时跟随此值;单个群设置后以该群为准(全局关着也能为某群单独开启)。'),