@ryantest/openclaw-qqbot 0.0.1

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 (197) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +483 -0
  3. package/README.zh.md +478 -0
  4. package/bin/qqbot-cli.js +243 -0
  5. package/clawdbot.plugin.json +16 -0
  6. package/dist/index.d.ts +17 -0
  7. package/dist/index.js +26 -0
  8. package/dist/src/admin-resolver.d.ts +27 -0
  9. package/dist/src/admin-resolver.js +122 -0
  10. package/dist/src/api.d.ts +156 -0
  11. package/dist/src/api.js +599 -0
  12. package/dist/src/channel.d.ts +11 -0
  13. package/dist/src/channel.js +354 -0
  14. package/dist/src/config.d.ts +25 -0
  15. package/dist/src/config.js +161 -0
  16. package/dist/src/credential-backup.d.ts +31 -0
  17. package/dist/src/credential-backup.js +66 -0
  18. package/dist/src/gateway.d.ts +18 -0
  19. package/dist/src/gateway.js +1265 -0
  20. package/dist/src/image-server.d.ts +68 -0
  21. package/dist/src/image-server.js +462 -0
  22. package/dist/src/inbound-attachments.d.ts +58 -0
  23. package/dist/src/inbound-attachments.js +234 -0
  24. package/dist/src/known-users.d.ts +100 -0
  25. package/dist/src/known-users.js +263 -0
  26. package/dist/src/message-queue.d.ts +50 -0
  27. package/dist/src/message-queue.js +115 -0
  28. package/dist/src/onboarding.d.ts +10 -0
  29. package/dist/src/onboarding.js +203 -0
  30. package/dist/src/outbound-deliver.d.ts +48 -0
  31. package/dist/src/outbound-deliver.js +462 -0
  32. package/dist/src/outbound.d.ts +203 -0
  33. package/dist/src/outbound.js +1102 -0
  34. package/dist/src/proactive.d.ts +170 -0
  35. package/dist/src/proactive.js +399 -0
  36. package/dist/src/ref-index-store.d.ts +70 -0
  37. package/dist/src/ref-index-store.js +273 -0
  38. package/dist/src/reply-dispatcher.d.ts +35 -0
  39. package/dist/src/reply-dispatcher.js +311 -0
  40. package/dist/src/runtime.d.ts +3 -0
  41. package/dist/src/runtime.js +10 -0
  42. package/dist/src/session-store.d.ts +52 -0
  43. package/dist/src/session-store.js +254 -0
  44. package/dist/src/slash-commands.d.ts +71 -0
  45. package/dist/src/slash-commands.js +1179 -0
  46. package/dist/src/startup-greeting.d.ts +30 -0
  47. package/dist/src/startup-greeting.js +78 -0
  48. package/dist/src/stt.d.ts +21 -0
  49. package/dist/src/stt.js +70 -0
  50. package/dist/src/tools/channel.d.ts +16 -0
  51. package/dist/src/tools/channel.js +234 -0
  52. package/dist/src/tools/remind.d.ts +2 -0
  53. package/dist/src/tools/remind.js +247 -0
  54. package/dist/src/types.d.ts +175 -0
  55. package/dist/src/types.js +1 -0
  56. package/dist/src/typing-keepalive.d.ts +27 -0
  57. package/dist/src/typing-keepalive.js +64 -0
  58. package/dist/src/update-checker.d.ts +34 -0
  59. package/dist/src/update-checker.js +166 -0
  60. package/dist/src/user-messages.d.ts +8 -0
  61. package/dist/src/user-messages.js +8 -0
  62. package/dist/src/utils/audio-convert.d.ts +89 -0
  63. package/dist/src/utils/audio-convert.js +704 -0
  64. package/dist/src/utils/file-utils.d.ts +55 -0
  65. package/dist/src/utils/file-utils.js +150 -0
  66. package/dist/src/utils/image-size.d.ts +51 -0
  67. package/dist/src/utils/image-size.js +234 -0
  68. package/dist/src/utils/media-tags.d.ts +14 -0
  69. package/dist/src/utils/media-tags.js +164 -0
  70. package/dist/src/utils/payload.d.ts +112 -0
  71. package/dist/src/utils/payload.js +186 -0
  72. package/dist/src/utils/platform.d.ts +137 -0
  73. package/dist/src/utils/platform.js +390 -0
  74. package/dist/src/utils/text-parsing.d.ts +32 -0
  75. package/dist/src/utils/text-parsing.js +80 -0
  76. package/dist/src/utils/upload-cache.d.ts +34 -0
  77. package/dist/src/utils/upload-cache.js +93 -0
  78. package/index.ts +31 -0
  79. package/moltbot.plugin.json +16 -0
  80. package/node_modules/@eshaz/web-worker/LICENSE +201 -0
  81. package/node_modules/@eshaz/web-worker/README.md +134 -0
  82. package/node_modules/@eshaz/web-worker/browser.js +17 -0
  83. package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
  84. package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
  85. package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
  86. package/node_modules/@eshaz/web-worker/node.js +223 -0
  87. package/node_modules/@eshaz/web-worker/package.json +54 -0
  88. package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
  89. package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
  90. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
  91. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
  92. package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
  93. package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
  94. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
  95. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
  96. package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
  97. package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
  98. package/node_modules/mpg123-decoder/README.md +265 -0
  99. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
  100. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
  101. package/node_modules/mpg123-decoder/index.js +8 -0
  102. package/node_modules/mpg123-decoder/package.json +58 -0
  103. package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
  104. package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
  105. package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
  106. package/node_modules/mpg123-decoder/types.d.ts +30 -0
  107. package/node_modules/silk-wasm/LICENSE +21 -0
  108. package/node_modules/silk-wasm/README.md +85 -0
  109. package/node_modules/silk-wasm/lib/index.cjs +16 -0
  110. package/node_modules/silk-wasm/lib/index.d.ts +70 -0
  111. package/node_modules/silk-wasm/lib/index.mjs +16 -0
  112. package/node_modules/silk-wasm/lib/silk.wasm +0 -0
  113. package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
  114. package/node_modules/silk-wasm/package.json +39 -0
  115. package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
  116. package/node_modules/simple-yenc/.prettierignore +1 -0
  117. package/node_modules/simple-yenc/LICENSE +7 -0
  118. package/node_modules/simple-yenc/README.md +163 -0
  119. package/node_modules/simple-yenc/dist/esm.js +1 -0
  120. package/node_modules/simple-yenc/dist/index.js +1 -0
  121. package/node_modules/simple-yenc/package.json +50 -0
  122. package/node_modules/simple-yenc/rollup.config.js +27 -0
  123. package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
  124. package/node_modules/ws/LICENSE +20 -0
  125. package/node_modules/ws/README.md +548 -0
  126. package/node_modules/ws/browser.js +8 -0
  127. package/node_modules/ws/index.js +13 -0
  128. package/node_modules/ws/lib/buffer-util.js +131 -0
  129. package/node_modules/ws/lib/constants.js +19 -0
  130. package/node_modules/ws/lib/event-target.js +292 -0
  131. package/node_modules/ws/lib/extension.js +203 -0
  132. package/node_modules/ws/lib/limiter.js +55 -0
  133. package/node_modules/ws/lib/permessage-deflate.js +528 -0
  134. package/node_modules/ws/lib/receiver.js +706 -0
  135. package/node_modules/ws/lib/sender.js +602 -0
  136. package/node_modules/ws/lib/stream.js +161 -0
  137. package/node_modules/ws/lib/subprotocol.js +62 -0
  138. package/node_modules/ws/lib/validation.js +152 -0
  139. package/node_modules/ws/lib/websocket-server.js +554 -0
  140. package/node_modules/ws/lib/websocket.js +1393 -0
  141. package/node_modules/ws/package.json +69 -0
  142. package/node_modules/ws/wrapper.mjs +8 -0
  143. package/openclaw.plugin.json +16 -0
  144. package/package.json +76 -0
  145. package/scripts/cleanup-legacy-plugins.sh +124 -0
  146. package/scripts/proactive-api-server.ts +369 -0
  147. package/scripts/send-proactive.ts +293 -0
  148. package/scripts/set-markdown.sh +156 -0
  149. package/scripts/test-sendmedia.ts +116 -0
  150. package/scripts/upgrade-via-alt-pkg.sh +307 -0
  151. package/scripts/upgrade-via-npm.ps1 +296 -0
  152. package/scripts/upgrade-via-npm.sh +301 -0
  153. package/scripts/upgrade-via-source.sh +774 -0
  154. package/skills/qqbot-channel/SKILL.md +263 -0
  155. package/skills/qqbot-channel/references/api_references.md +521 -0
  156. package/skills/qqbot-media/SKILL.md +56 -0
  157. package/skills/qqbot-remind/SKILL.md +149 -0
  158. package/src/admin-resolver.ts +140 -0
  159. package/src/api.ts +819 -0
  160. package/src/bot-logs-2026-03-21T11-21-47(2).txt +46 -0
  161. package/src/channel.ts +381 -0
  162. package/src/config.ts +187 -0
  163. package/src/credential-backup.ts +72 -0
  164. package/src/gateway.log +43 -0
  165. package/src/gateway.ts +1404 -0
  166. package/src/image-server.ts +539 -0
  167. package/src/inbound-attachments.ts +304 -0
  168. package/src/known-users.ts +353 -0
  169. package/src/message-queue.ts +169 -0
  170. package/src/onboarding.ts +274 -0
  171. package/src/openclaw-2026-03-21.log +3729 -0
  172. package/src/openclaw-plugin-sdk.d.ts +522 -0
  173. package/src/outbound-deliver.ts +552 -0
  174. package/src/outbound.ts +1266 -0
  175. package/src/proactive.ts +530 -0
  176. package/src/ref-index-store.ts +357 -0
  177. package/src/reply-dispatcher.ts +334 -0
  178. package/src/runtime.ts +14 -0
  179. package/src/session-store.ts +303 -0
  180. package/src/slash-commands.ts +1305 -0
  181. package/src/startup-greeting.ts +98 -0
  182. package/src/stt.ts +86 -0
  183. package/src/tools/channel.ts +281 -0
  184. package/src/tools/remind.ts +296 -0
  185. package/src/types.ts +183 -0
  186. package/src/typing-keepalive.ts +59 -0
  187. package/src/update-checker.ts +179 -0
  188. package/src/user-messages.ts +7 -0
  189. package/src/utils/audio-convert.ts +803 -0
  190. package/src/utils/file-utils.ts +167 -0
  191. package/src/utils/image-size.ts +266 -0
  192. package/src/utils/media-tags.ts +182 -0
  193. package/src/utils/payload.ts +265 -0
  194. package/src/utils/platform.ts +435 -0
  195. package/src/utils/text-parsing.ts +82 -0
  196. package/src/utils/upload-cache.ts +128 -0
  197. package/tsconfig.json +16 -0
@@ -0,0 +1,357 @@
1
+ /**
2
+ * QQ Bot 引用索引持久化存储
3
+ *
4
+ * QQ Bot 使用 REFIDX_xxx 索引体系做引用消息,
5
+ * 入站事件只有索引值,无 API 可回查内容。
6
+ * 采用 内存缓存 + JSONL 追加写持久化 方案,确保重启后历史引用仍可命中。
7
+ *
8
+ * 存储位置:~/.openclaw/qqbot/data/ref-index.jsonl
9
+ *
10
+ * 每行格式:{"k":"REFIDX_xxx","v":{...},"t":1709000000}
11
+ * - k = refIdx 键
12
+ * - v = 消息数据
13
+ * - t = 写入时间(用于 TTL 淘汰和 compact)
14
+ */
15
+
16
+ import fs from "node:fs";
17
+ import path from "node:path";
18
+ import { getQQBotDataDir } from "./utils/platform.js";
19
+
20
+ // ============ 存储的消息摘要 ============
21
+
22
+ export interface RefIndexEntry {
23
+ /** 消息文本内容(完整保存) */
24
+ content: string;
25
+ /** 发送者 ID */
26
+ senderId: string;
27
+ /** 发送者名称 */
28
+ senderName?: string;
29
+ /** 消息时间戳 (ms) */
30
+ timestamp: number;
31
+ /** 是否是 bot 发出的消息 */
32
+ isBot?: boolean;
33
+ /** 附件摘要(图片/语音/视频/文件等) */
34
+ attachments?: RefAttachmentSummary[];
35
+ }
36
+
37
+ /** 附件摘要:存本地路径、在线 URL 和类型描述 */
38
+ export interface RefAttachmentSummary {
39
+ /** 附件类型 */
40
+ type: "image" | "voice" | "video" | "file" | "unknown";
41
+ /** 文件名(如有) */
42
+ filename?: string;
43
+ /** MIME 类型 */
44
+ contentType?: string;
45
+ /** 语音转录文本(入站:STT/ASR识别结果;出站:TTS原文本) */
46
+ transcript?: string;
47
+ /** 语音转录来源:stt=本地STT、asr=平台ASR、tts=TTS原文本、fallback=兜底文案 */
48
+ transcriptSource?: "stt" | "asr" | "tts" | "fallback";
49
+ /** 已下载到本地的文件路径(持久化后可供引用时访问) */
50
+ localPath?: string;
51
+ /** 在线来源 URL(公网图片/文件等) */
52
+ url?: string;
53
+ }
54
+
55
+ // ============ 配置 ============
56
+
57
+ const STORAGE_DIR = getQQBotDataDir("data");
58
+ const REF_INDEX_FILE = path.join(STORAGE_DIR, "ref-index.jsonl");
59
+ const MAX_ENTRIES = 50000; // 内存中最大缓存条目数
60
+ const TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 天
61
+ const COMPACT_THRESHOLD_RATIO = 2; // 文件行数超过有效条目 N 倍时 compact
62
+
63
+ // ============ JSONL 行格式 ============
64
+
65
+ interface RefIndexLine {
66
+ /** refIdx 键 */
67
+ k: string;
68
+ /** 消息数据 */
69
+ v: RefIndexEntry;
70
+ /** 写入时间 (ms) */
71
+ t: number;
72
+ }
73
+
74
+ // ============ 内存缓存 ============
75
+
76
+ let cache: Map<string, RefIndexEntry & { _createdAt: number }> | null = null;
77
+ let totalLinesOnDisk = 0; // 磁盘文件总行数(含过期 / 被覆盖的)
78
+
79
+ /**
80
+ * 从 JSONL 文件加载到内存(懒加载,首次访问时触发)
81
+ */
82
+ function loadFromFile(): Map<string, RefIndexEntry & { _createdAt: number }> {
83
+ if (cache !== null) return cache;
84
+
85
+ cache = new Map();
86
+ totalLinesOnDisk = 0;
87
+
88
+ try {
89
+ if (!fs.existsSync(REF_INDEX_FILE)) {
90
+ return cache;
91
+ }
92
+
93
+ const raw = fs.readFileSync(REF_INDEX_FILE, "utf-8");
94
+ const lines = raw.split("\n");
95
+ const now = Date.now();
96
+ let expired = 0;
97
+
98
+ for (const line of lines) {
99
+ const trimmed = line.trim();
100
+ if (!trimmed) continue;
101
+ totalLinesOnDisk++;
102
+
103
+ try {
104
+ const entry = JSON.parse(trimmed) as RefIndexLine;
105
+ if (!entry.k || !entry.v || !entry.t) continue;
106
+
107
+ // 跳过过期条目
108
+ if (now - entry.t > TTL_MS) {
109
+ expired++;
110
+ continue;
111
+ }
112
+
113
+ cache.set(entry.k, {
114
+ ...entry.v,
115
+ _createdAt: entry.t,
116
+ });
117
+ } catch {
118
+ // 跳过损坏的行
119
+ }
120
+ }
121
+
122
+ console.log(
123
+ `[ref-index-store] Loaded ${cache.size} entries from ${totalLinesOnDisk} lines (${expired} expired)`,
124
+ );
125
+
126
+ // 启动时检查是否需要 compact
127
+ if (shouldCompact()) {
128
+ compactFile();
129
+ }
130
+ } catch (err) {
131
+ console.error(`[ref-index-store] Failed to load: ${err}`);
132
+ cache = new Map();
133
+ }
134
+
135
+ return cache;
136
+ }
137
+
138
+ // ============ JSONL 追加写入 ============
139
+
140
+ /**
141
+ * 追加一行到 JSONL 文件
142
+ */
143
+ function appendLine(line: RefIndexLine): void {
144
+ try {
145
+ ensureDir();
146
+ fs.appendFileSync(REF_INDEX_FILE, JSON.stringify(line) + "\n", "utf-8");
147
+ totalLinesOnDisk++;
148
+ } catch (err) {
149
+ console.error(`[ref-index-store] Failed to append: ${err}`);
150
+ }
151
+ }
152
+
153
+ function ensureDir(): void {
154
+ if (!fs.existsSync(STORAGE_DIR)) {
155
+ fs.mkdirSync(STORAGE_DIR, { recursive: true });
156
+ }
157
+ }
158
+
159
+ // ============ Compact:重写文件,去除过期和被覆盖的条目 ============
160
+
161
+ function shouldCompact(): boolean {
162
+ if (!cache) return false;
163
+ // 文件行数远超有效条目数时 compact
164
+ return totalLinesOnDisk > cache.size * COMPACT_THRESHOLD_RATIO && totalLinesOnDisk > 1000;
165
+ }
166
+
167
+ function compactFile(): void {
168
+ if (!cache) return;
169
+
170
+ const before = totalLinesOnDisk;
171
+ try {
172
+ ensureDir();
173
+ const tmpPath = REF_INDEX_FILE + ".tmp";
174
+ const lines: string[] = [];
175
+
176
+ for (const [key, entry] of cache) {
177
+ const line: RefIndexLine = {
178
+ k: key,
179
+ v: {
180
+ content: entry.content,
181
+ senderId: entry.senderId,
182
+ senderName: entry.senderName,
183
+ timestamp: entry.timestamp,
184
+ isBot: entry.isBot,
185
+ attachments: entry.attachments,
186
+ },
187
+ t: entry._createdAt,
188
+ };
189
+ lines.push(JSON.stringify(line));
190
+ }
191
+
192
+ fs.writeFileSync(tmpPath, lines.join("\n") + "\n", "utf-8");
193
+ fs.renameSync(tmpPath, REF_INDEX_FILE);
194
+ totalLinesOnDisk = cache.size;
195
+ console.log(`[ref-index-store] Compacted: ${before} lines → ${totalLinesOnDisk} lines`);
196
+ } catch (err) {
197
+ console.error(`[ref-index-store] Compact failed: ${err}`);
198
+ }
199
+ }
200
+
201
+ // ============ 溢出淘汰 ============
202
+
203
+ function evictIfNeeded(): void {
204
+ if (!cache || cache.size < MAX_ENTRIES) return;
205
+
206
+ const now = Date.now();
207
+ // 第一轮:清理过期
208
+ for (const [key, entry] of cache) {
209
+ if (now - entry._createdAt > TTL_MS) {
210
+ cache.delete(key);
211
+ }
212
+ }
213
+
214
+ // 第二轮:仍超限,按时间删最旧
215
+ if (cache.size >= MAX_ENTRIES) {
216
+ const sorted = [...cache.entries()].sort((a, b) => a[1]._createdAt - b[1]._createdAt);
217
+ const toRemove = sorted.slice(0, cache.size - MAX_ENTRIES + 1000);
218
+ for (const [key] of toRemove) {
219
+ cache.delete(key);
220
+ }
221
+ console.log(`[ref-index-store] Evicted ${toRemove.length} oldest entries`);
222
+ }
223
+ }
224
+
225
+ // ============ 公共 API ============
226
+
227
+ /**
228
+ * 存储一条消息的 refIdx 映射
229
+ */
230
+ export function setRefIndex(refIdx: string, entry: RefIndexEntry): void {
231
+ const store = loadFromFile();
232
+ evictIfNeeded();
233
+
234
+ const now = Date.now();
235
+ store.set(refIdx, {
236
+ content: entry.content,
237
+ senderId: entry.senderId,
238
+ senderName: entry.senderName,
239
+ timestamp: entry.timestamp,
240
+ isBot: entry.isBot,
241
+ attachments: entry.attachments,
242
+ _createdAt: now,
243
+ });
244
+
245
+ // 追加写入 JSONL
246
+ appendLine({
247
+ k: refIdx,
248
+ v: {
249
+ content: entry.content,
250
+ senderId: entry.senderId,
251
+ senderName: entry.senderName,
252
+ timestamp: entry.timestamp,
253
+ isBot: entry.isBot,
254
+ attachments: entry.attachments,
255
+ },
256
+ t: now,
257
+ });
258
+
259
+ // 检查是否需要 compact
260
+ if (shouldCompact()) {
261
+ compactFile();
262
+ }
263
+ }
264
+
265
+ /**
266
+ * 查找被引用消息
267
+ */
268
+ export function getRefIndex(refIdx: string): RefIndexEntry | null {
269
+ const store = loadFromFile();
270
+ const entry = store.get(refIdx);
271
+ if (!entry) return null;
272
+
273
+ // 检查过期
274
+ if (Date.now() - entry._createdAt > TTL_MS) {
275
+ store.delete(refIdx);
276
+ return null;
277
+ }
278
+
279
+ return {
280
+ content: entry.content,
281
+ senderId: entry.senderId,
282
+ senderName: entry.senderName,
283
+ timestamp: entry.timestamp,
284
+ isBot: entry.isBot,
285
+ attachments: entry.attachments,
286
+ };
287
+ }
288
+
289
+ /**
290
+ * 将引用消息内容格式化为人类可读的描述(供 AI 上下文注入)
291
+ */
292
+ export function formatRefEntryForAgent(entry: RefIndexEntry): string {
293
+ const parts: string[] = [];
294
+
295
+ // 文本内容
296
+ if (entry.content.trim()) {
297
+ parts.push(entry.content);
298
+ }
299
+
300
+ // 附件描述
301
+ if (entry.attachments?.length) {
302
+ for (const att of entry.attachments) {
303
+ const sourceHint = att.localPath ? ` (${att.localPath})` : att.url ? ` (${att.url})` : "";
304
+ switch (att.type) {
305
+ case "image":
306
+ parts.push(`[图片${att.filename ? `: ${att.filename}` : ""}${sourceHint}]`);
307
+ break;
308
+ case "voice":
309
+ if (att.transcript) {
310
+ const sourceMap = { stt: "本地识别", asr: "官方识别", tts: "TTS原文", fallback: "兜底文案" };
311
+ const sourceTag = att.transcriptSource ? ` - ${sourceMap[att.transcriptSource] || att.transcriptSource}` : "";
312
+ parts.push(`[语音消息(内容: "${att.transcript}"${sourceTag})${sourceHint}]`);
313
+ } else {
314
+ parts.push(`[语音消息${sourceHint}]`);
315
+ }
316
+ break;
317
+ case "video":
318
+ parts.push(`[视频${att.filename ? `: ${att.filename}` : ""}${sourceHint}]`);
319
+ break;
320
+ case "file":
321
+ parts.push(`[文件${att.filename ? `: ${att.filename}` : ""}${sourceHint}]`);
322
+ break;
323
+ default:
324
+ parts.push(`[附件${att.filename ? `: ${att.filename}` : ""}${sourceHint}]`);
325
+ }
326
+ }
327
+ }
328
+
329
+ return parts.join(" ") || "[空消息]";
330
+ }
331
+
332
+ /**
333
+ * 进程退出前强制 compact(确保数据一致性)
334
+ */
335
+ export function flushRefIndex(): void {
336
+ if (cache && shouldCompact()) {
337
+ compactFile();
338
+ }
339
+ }
340
+
341
+ /**
342
+ * 缓存统计(调试用)
343
+ */
344
+ export function getRefIndexStats(): {
345
+ size: number;
346
+ maxEntries: number;
347
+ totalLinesOnDisk: number;
348
+ filePath: string;
349
+ } {
350
+ const store = loadFromFile();
351
+ return {
352
+ size: store.size,
353
+ maxEntries: MAX_ENTRIES,
354
+ totalLinesOnDisk,
355
+ filePath: REF_INDEX_FILE,
356
+ };
357
+ }
@@ -0,0 +1,334 @@
1
+ import path from "node:path";
2
+ import type { ResolvedQQBotAccount } from "./types.js";
3
+ import { getAccessToken, sendC2CMessage, sendChannelMessage, sendGroupMessage, clearTokenCache, sendC2CImageMessage, sendGroupImageMessage, sendC2CVoiceMessage, sendGroupVoiceMessage, sendC2CVideoMessage, sendGroupVideoMessage, sendC2CFileMessage, sendGroupFileMessage } from "./api.js";
4
+ import { parseQQBotPayload, encodePayloadForCron, isCronReminderPayload, isMediaPayload, type MediaPayload } from "./utils/payload.js";
5
+ import { resolveTTSConfig, textToSilk, formatDuration } from "./utils/audio-convert.js";
6
+ import { checkFileSize, readFileAsync, fileExistsAsync, formatFileSize } from "./utils/file-utils.js";
7
+ import { getQQBotDataDir, normalizePath, sanitizeFileName } from "./utils/platform.js";
8
+
9
+ export interface MessageTarget {
10
+ type: "c2c" | "guild" | "dm" | "group";
11
+ senderId: string;
12
+ messageId: string;
13
+ channelId?: string;
14
+ groupOpenid?: string;
15
+ }
16
+
17
+ export interface ReplyContext {
18
+ target: MessageTarget;
19
+ account: ResolvedQQBotAccount;
20
+ cfg: unknown;
21
+ log?: {
22
+ info: (msg: string) => void;
23
+ error: (msg: string) => void;
24
+ debug?: (msg: string) => void;
25
+ };
26
+ }
27
+
28
+ /**
29
+ * 带 token 过期重试的消息发送
30
+ */
31
+ export async function sendWithTokenRetry<T>(
32
+ appId: string,
33
+ clientSecret: string,
34
+ sendFn: (token: string) => Promise<T>,
35
+ log?: ReplyContext["log"],
36
+ accountId?: string,
37
+ ): Promise<T> {
38
+ try {
39
+ const token = await getAccessToken(appId, clientSecret);
40
+ return await sendFn(token);
41
+ } catch (err) {
42
+ const errMsg = String(err);
43
+ if (errMsg.includes("401") || errMsg.includes("token") || errMsg.includes("access_token")) {
44
+ log?.info(`[qqbot:${accountId}] Token may be expired, refreshing...`);
45
+ clearTokenCache(appId);
46
+ const newToken = await getAccessToken(appId, clientSecret);
47
+ return await sendFn(newToken);
48
+ } else {
49
+ throw err;
50
+ }
51
+ }
52
+ }
53
+
54
+ /**
55
+ * 根据消息类型路由发送文本
56
+ */
57
+ export async function sendTextToTarget(
58
+ ctx: ReplyContext,
59
+ text: string,
60
+ refIdx?: string,
61
+ ): Promise<void> {
62
+ const { target, account } = ctx;
63
+ await sendWithTokenRetry(account.appId, account.clientSecret, async (token) => {
64
+ if (target.type === "c2c") {
65
+ await sendC2CMessage(token, target.senderId, text, target.messageId, refIdx);
66
+ } else if (target.type === "group" && target.groupOpenid) {
67
+ await sendGroupMessage(token, target.groupOpenid, text, target.messageId);
68
+ } else if (target.channelId) {
69
+ await sendChannelMessage(token, target.channelId, text, target.messageId);
70
+ } else if (target.type === "dm") {
71
+ await sendC2CMessage(token, target.senderId, text, target.messageId, refIdx);
72
+ }
73
+ }, ctx.log, account.accountId);
74
+ }
75
+
76
+ /**
77
+ * 发送错误提示给用户
78
+ */
79
+ export async function sendErrorToTarget(ctx: ReplyContext, errorText: string): Promise<void> {
80
+ try {
81
+ await sendTextToTarget(ctx, errorText);
82
+ } catch (sendErr) {
83
+ ctx.log?.error(`[qqbot:${ctx.account.accountId}] Failed to send error message: ${sendErr}`);
84
+ }
85
+ }
86
+
87
+ /**
88
+ * 处理结构化载荷(QQBOT_PAYLOAD: 前缀的 JSON)
89
+ * 返回 true 表示已处理,false 表示不是结构化载荷
90
+ */
91
+ export async function handleStructuredPayload(
92
+ ctx: ReplyContext,
93
+ replyText: string,
94
+ recordActivity: () => void,
95
+ ): Promise<boolean> {
96
+ const { target, account, cfg, log } = ctx;
97
+ const payloadResult = parseQQBotPayload(replyText);
98
+
99
+ if (!payloadResult.isPayload) return false;
100
+
101
+ if (payloadResult.error) {
102
+ log?.error(`[qqbot:${account.accountId}] Payload parse error: ${payloadResult.error}`);
103
+ return true;
104
+ }
105
+
106
+ if (!payloadResult.payload) return true;
107
+
108
+ const parsedPayload = payloadResult.payload;
109
+ log?.info(`[qqbot:${account.accountId}] Detected structured payload, type: ${parsedPayload.type}`);
110
+
111
+ if (isCronReminderPayload(parsedPayload)) {
112
+ log?.info(`[qqbot:${account.accountId}] Processing cron_reminder payload`);
113
+ const cronMessage = encodePayloadForCron(parsedPayload);
114
+ const confirmText = `⏰ 提醒已设置,将在指定时间发送: "${parsedPayload.content}"`;
115
+ try {
116
+ await sendTextToTarget(ctx, confirmText);
117
+ log?.info(`[qqbot:${account.accountId}] Cron reminder confirmation sent, cronMessage: ${cronMessage}`);
118
+ } catch (err) {
119
+ log?.error(`[qqbot:${account.accountId}] Failed to send cron confirmation: ${err}`);
120
+ }
121
+ recordActivity();
122
+ return true;
123
+ }
124
+
125
+ if (isMediaPayload(parsedPayload)) {
126
+ log?.info(`[qqbot:${account.accountId}] Processing media payload, mediaType: ${parsedPayload.mediaType}`);
127
+
128
+ if (parsedPayload.mediaType === "image") {
129
+ await handleImagePayload(ctx, parsedPayload);
130
+ } else if (parsedPayload.mediaType === "audio") {
131
+ await handleAudioPayload(ctx, parsedPayload);
132
+ } else if (parsedPayload.mediaType === "video") {
133
+ await handleVideoPayload(ctx, parsedPayload);
134
+ } else if (parsedPayload.mediaType === "file") {
135
+ await handleFilePayload(ctx, parsedPayload);
136
+ } else {
137
+ log?.error(`[qqbot:${account.accountId}] Unknown media type: ${(parsedPayload as MediaPayload).mediaType}`);
138
+ }
139
+ recordActivity();
140
+ return true;
141
+ }
142
+
143
+ log?.error(`[qqbot:${account.accountId}] Unknown payload type: ${(parsedPayload as any).type}`);
144
+ return true;
145
+ }
146
+
147
+ // ============ 媒体载荷处理 ============
148
+
149
+ async function handleImagePayload(ctx: ReplyContext, payload: MediaPayload): Promise<void> {
150
+ const { target, account, log } = ctx;
151
+ let imageUrl = normalizePath(payload.path);
152
+ const originalImagePath = payload.source === "file" ? imageUrl : undefined;
153
+
154
+ if (payload.source === "file") {
155
+ try {
156
+ if (!(await fileExistsAsync(imageUrl))) {
157
+ log?.error(`[qqbot:${account.accountId}] Image not found: ${imageUrl}`);
158
+ return;
159
+ }
160
+ const imgSzCheck = checkFileSize(imageUrl);
161
+ if (!imgSzCheck.ok) {
162
+ log?.error(`[qqbot:${account.accountId}] Image size check failed: ${imgSzCheck.error}`);
163
+ return;
164
+ }
165
+ const fileBuffer = await readFileAsync(imageUrl);
166
+ const base64Data = fileBuffer.toString("base64");
167
+ const ext = path.extname(imageUrl).toLowerCase();
168
+ const mimeTypes: Record<string, string> = {
169
+ ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png",
170
+ ".gif": "image/gif", ".webp": "image/webp", ".bmp": "image/bmp",
171
+ };
172
+ const mimeType = mimeTypes[ext];
173
+ if (!mimeType) {
174
+ log?.error(`[qqbot:${account.accountId}] Unsupported image format: ${ext}`);
175
+ return;
176
+ }
177
+ imageUrl = `data:${mimeType};base64,${base64Data}`;
178
+ log?.info(`[qqbot:${account.accountId}] Converted local image to Base64 (size: ${formatFileSize(fileBuffer.length)})`);
179
+ } catch (readErr) {
180
+ log?.error(`[qqbot:${account.accountId}] Failed to read local image: ${readErr}`);
181
+ return;
182
+ }
183
+ }
184
+
185
+ try {
186
+ await sendWithTokenRetry(account.appId, account.clientSecret, async (token) => {
187
+ if (target.type === "c2c") {
188
+ await sendC2CImageMessage(token, target.senderId, imageUrl, target.messageId, undefined, originalImagePath);
189
+ } else if (target.type === "group" && target.groupOpenid) {
190
+ await sendGroupImageMessage(token, target.groupOpenid, imageUrl, target.messageId);
191
+ } else if (target.channelId) {
192
+ await sendChannelMessage(token, target.channelId, `![](${payload.path})`, target.messageId);
193
+ }
194
+ }, log, account.accountId);
195
+ log?.info(`[qqbot:${account.accountId}] Sent image via media payload`);
196
+
197
+ if (payload.caption) {
198
+ await sendTextToTarget(ctx, payload.caption);
199
+ }
200
+ } catch (err) {
201
+ log?.error(`[qqbot:${account.accountId}] Failed to send image: ${err}`);
202
+ }
203
+ }
204
+
205
+ async function handleAudioPayload(ctx: ReplyContext, payload: MediaPayload): Promise<void> {
206
+ const { target, account, cfg, log } = ctx;
207
+ try {
208
+ const ttsText = payload.caption || payload.path;
209
+ if (!ttsText?.trim()) {
210
+ log?.error(`[qqbot:${account.accountId}] Voice missing text`);
211
+ } else {
212
+ const ttsCfg = resolveTTSConfig(cfg as Record<string, unknown>);
213
+ if (!ttsCfg) {
214
+ log?.error(`[qqbot:${account.accountId}] TTS not configured (channels.qqbot.tts in openclaw.json)`);
215
+ } else {
216
+ log?.info(`[qqbot:${account.accountId}] TTS: "${ttsText.slice(0, 50)}..." via ${ttsCfg.model}`);
217
+ const ttsDir = getQQBotDataDir("tts");
218
+ const { silkPath, silkBase64, duration } = await textToSilk(ttsText, ttsCfg, ttsDir);
219
+ log?.info(`[qqbot:${account.accountId}] TTS done: ${formatDuration(duration)}, file saved: ${silkPath}`);
220
+
221
+ await sendWithTokenRetry(account.appId, account.clientSecret, async (token) => {
222
+ if (target.type === "c2c") {
223
+ await sendC2CVoiceMessage(token, target.senderId, silkBase64, target.messageId, ttsText, silkPath);
224
+ } else if (target.type === "group" && target.groupOpenid) {
225
+ await sendGroupVoiceMessage(token, target.groupOpenid, silkBase64, target.messageId);
226
+ } else if (target.channelId) {
227
+ log?.error(`[qqbot:${account.accountId}] Voice not supported in channel, sending text fallback`);
228
+ await sendChannelMessage(token, target.channelId, ttsText, target.messageId);
229
+ }
230
+ }, log, account.accountId);
231
+ log?.info(`[qqbot:${account.accountId}] Voice message sent`);
232
+ }
233
+ }
234
+ } catch (err) {
235
+ log?.error(`[qqbot:${account.accountId}] TTS/voice send failed: ${err}`);
236
+ }
237
+ }
238
+
239
+ async function handleVideoPayload(ctx: ReplyContext, payload: MediaPayload): Promise<void> {
240
+ const { target, account, log } = ctx;
241
+ try {
242
+ const videoPath = normalizePath(payload.path ?? "");
243
+ if (!videoPath?.trim()) {
244
+ log?.error(`[qqbot:${account.accountId}] Video missing path`);
245
+ } else {
246
+ const isHttpUrl = videoPath.startsWith("http://") || videoPath.startsWith("https://");
247
+ log?.info(`[qqbot:${account.accountId}] Video send: "${videoPath.slice(0, 60)}..."`);
248
+
249
+ await sendWithTokenRetry(account.appId, account.clientSecret, async (token) => {
250
+ if (isHttpUrl) {
251
+ if (target.type === "c2c") {
252
+ await sendC2CVideoMessage(token, target.senderId, videoPath, undefined, target.messageId);
253
+ } else if (target.type === "group" && target.groupOpenid) {
254
+ await sendGroupVideoMessage(token, target.groupOpenid, videoPath, undefined, target.messageId);
255
+ } else if (target.channelId) {
256
+ log?.error(`[qqbot:${account.accountId}] Video not supported in channel`);
257
+ }
258
+ } else {
259
+ if (!(await fileExistsAsync(videoPath))) {
260
+ throw new Error(`视频文件不存在: ${videoPath}`);
261
+ }
262
+ const vPaySzCheck = checkFileSize(videoPath);
263
+ if (!vPaySzCheck.ok) {
264
+ throw new Error(vPaySzCheck.error!);
265
+ }
266
+ const fileBuffer = await readFileAsync(videoPath);
267
+ const videoBase64 = fileBuffer.toString("base64");
268
+ log?.info(`[qqbot:${account.accountId}] Read local video (${formatFileSize(fileBuffer.length)}): ${videoPath}`);
269
+
270
+ if (target.type === "c2c") {
271
+ await sendC2CVideoMessage(token, target.senderId, undefined, videoBase64, target.messageId, undefined, videoPath);
272
+ } else if (target.type === "group" && target.groupOpenid) {
273
+ await sendGroupVideoMessage(token, target.groupOpenid, undefined, videoBase64, target.messageId);
274
+ } else if (target.channelId) {
275
+ log?.error(`[qqbot:${account.accountId}] Video not supported in channel`);
276
+ }
277
+ }
278
+ }, log, account.accountId);
279
+ log?.info(`[qqbot:${account.accountId}] Video message sent`);
280
+
281
+ if (payload.caption) {
282
+ await sendTextToTarget(ctx, payload.caption);
283
+ }
284
+ }
285
+ } catch (err) {
286
+ log?.error(`[qqbot:${account.accountId}] Video send failed: ${err}`);
287
+ }
288
+ }
289
+
290
+ async function handleFilePayload(ctx: ReplyContext, payload: MediaPayload): Promise<void> {
291
+ const { target, account, log } = ctx;
292
+ try {
293
+ const filePath = normalizePath(payload.path ?? "");
294
+ if (!filePath?.trim()) {
295
+ log?.error(`[qqbot:${account.accountId}] File missing path`);
296
+ } else {
297
+ const isHttpUrl = filePath.startsWith("http://") || filePath.startsWith("https://");
298
+ const fileName = sanitizeFileName(path.basename(filePath));
299
+ log?.info(`[qqbot:${account.accountId}] File send: "${filePath.slice(0, 60)}..." (${isHttpUrl ? "URL" : "local"})`);
300
+
301
+ await sendWithTokenRetry(account.appId, account.clientSecret, async (token) => {
302
+ if (isHttpUrl) {
303
+ if (target.type === "c2c") {
304
+ await sendC2CFileMessage(token, target.senderId, undefined, filePath, target.messageId, fileName);
305
+ } else if (target.type === "group" && target.groupOpenid) {
306
+ await sendGroupFileMessage(token, target.groupOpenid, undefined, filePath, target.messageId, fileName);
307
+ } else if (target.channelId) {
308
+ log?.error(`[qqbot:${account.accountId}] File not supported in channel`);
309
+ }
310
+ } else {
311
+ if (!(await fileExistsAsync(filePath))) {
312
+ throw new Error(`文件不存在: ${filePath}`);
313
+ }
314
+ const fPaySzCheck = checkFileSize(filePath);
315
+ if (!fPaySzCheck.ok) {
316
+ throw new Error(fPaySzCheck.error!);
317
+ }
318
+ const fileBuffer = await readFileAsync(filePath);
319
+ const fileBase64 = fileBuffer.toString("base64");
320
+ if (target.type === "c2c") {
321
+ await sendC2CFileMessage(token, target.senderId, fileBase64, undefined, target.messageId, fileName, filePath);
322
+ } else if (target.type === "group" && target.groupOpenid) {
323
+ await sendGroupFileMessage(token, target.groupOpenid, fileBase64, undefined, target.messageId, fileName);
324
+ } else if (target.channelId) {
325
+ log?.error(`[qqbot:${account.accountId}] File not supported in channel`);
326
+ }
327
+ }
328
+ }, log, account.accountId);
329
+ log?.info(`[qqbot:${account.accountId}] File message sent`);
330
+ }
331
+ } catch (err) {
332
+ log?.error(`[qqbot:${account.accountId}] File send failed: ${err}`);
333
+ }
334
+ }
package/src/runtime.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { PluginRuntime } from "openclaw/plugin-sdk";
2
+
3
+ let runtime: PluginRuntime | null = null;
4
+
5
+ export function setQQBotRuntime(next: PluginRuntime) {
6
+ runtime = next;
7
+ }
8
+
9
+ export function getQQBotRuntime(): PluginRuntime {
10
+ if (!runtime) {
11
+ throw new Error("QQBot runtime not initialized");
12
+ }
13
+ return runtime;
14
+ }