@leoqlin/openclaw-qqbot 1.6.7-alpha1
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/LICENSE +22 -0
- package/README.md +484 -0
- package/README.zh.md +479 -0
- package/bin/qqbot-cli.js +243 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +26 -0
- package/dist/src/admin-resolver.d.ts +33 -0
- package/dist/src/admin-resolver.js +157 -0
- package/dist/src/api.d.ts +301 -0
- package/dist/src/api.js +890 -0
- package/dist/src/channel.d.ts +29 -0
- package/dist/src/channel.js +452 -0
- package/dist/src/config.d.ts +56 -0
- package/dist/src/config.js +278 -0
- package/dist/src/credential-backup.d.ts +31 -0
- package/dist/src/credential-backup.js +66 -0
- package/dist/src/deliver-debounce.d.ts +74 -0
- package/dist/src/deliver-debounce.js +174 -0
- package/dist/src/gateway.d.ts +18 -0
- package/dist/src/gateway.js +2005 -0
- package/dist/src/group-history.d.ts +136 -0
- package/dist/src/group-history.js +226 -0
- package/dist/src/image-server.d.ts +87 -0
- package/dist/src/image-server.js +570 -0
- package/dist/src/inbound-attachments.d.ts +60 -0
- package/dist/src/inbound-attachments.js +248 -0
- package/dist/src/known-users.d.ts +100 -0
- package/dist/src/known-users.js +263 -0
- package/dist/src/message-gating.d.ts +53 -0
- package/dist/src/message-gating.js +107 -0
- package/dist/src/message-queue.d.ts +89 -0
- package/dist/src/message-queue.js +257 -0
- package/dist/src/onboarding.d.ts +10 -0
- package/dist/src/onboarding.js +203 -0
- package/dist/src/outbound-deliver.d.ts +48 -0
- package/dist/src/outbound-deliver.js +392 -0
- package/dist/src/outbound.d.ts +205 -0
- package/dist/src/outbound.js +938 -0
- package/dist/src/proactive.d.ts +170 -0
- package/dist/src/proactive.js +399 -0
- package/dist/src/ref-index-store.d.ts +101 -0
- package/dist/src/ref-index-store.js +298 -0
- package/dist/src/reply-dispatcher.d.ts +35 -0
- package/dist/src/reply-dispatcher.js +311 -0
- package/dist/src/request-context.d.ts +25 -0
- package/dist/src/request-context.js +37 -0
- package/dist/src/runtime.d.ts +3 -0
- package/dist/src/runtime.js +10 -0
- package/dist/src/session-store.d.ts +52 -0
- package/dist/src/session-store.js +254 -0
- package/dist/src/slash-commands.d.ts +77 -0
- package/dist/src/slash-commands.js +1866 -0
- package/dist/src/startup-greeting.d.ts +30 -0
- package/dist/src/startup-greeting.js +97 -0
- package/dist/src/streaming.d.ts +247 -0
- package/dist/src/streaming.js +899 -0
- package/dist/src/stt.d.ts +21 -0
- package/dist/src/stt.js +70 -0
- package/dist/src/tools/channel.d.ts +16 -0
- package/dist/src/tools/channel.js +234 -0
- package/dist/src/tools/remind.d.ts +2 -0
- package/dist/src/tools/remind.js +256 -0
- package/dist/src/types.d.ts +367 -0
- package/dist/src/types.js +17 -0
- package/dist/src/typing-keepalive.d.ts +27 -0
- package/dist/src/typing-keepalive.js +64 -0
- package/dist/src/update-checker.d.ts +36 -0
- package/dist/src/update-checker.js +171 -0
- package/dist/src/utils/audio-convert.d.ts +98 -0
- package/dist/src/utils/audio-convert.js +755 -0
- package/dist/src/utils/chunked-upload.d.ts +68 -0
- package/dist/src/utils/chunked-upload.js +341 -0
- package/dist/src/utils/file-utils.d.ts +61 -0
- package/dist/src/utils/file-utils.js +172 -0
- package/dist/src/utils/image-size.d.ts +51 -0
- package/dist/src/utils/image-size.js +234 -0
- package/dist/src/utils/media-send.d.ts +158 -0
- package/dist/src/utils/media-send.js +499 -0
- package/dist/src/utils/media-tags.d.ts +14 -0
- package/dist/src/utils/media-tags.js +165 -0
- package/dist/src/utils/payload.d.ts +112 -0
- package/dist/src/utils/payload.js +186 -0
- package/dist/src/utils/pkg-version.d.ts +5 -0
- package/dist/src/utils/pkg-version.js +61 -0
- package/dist/src/utils/platform.d.ts +137 -0
- package/dist/src/utils/platform.js +390 -0
- package/dist/src/utils/ssrf-guard.d.ts +25 -0
- package/dist/src/utils/ssrf-guard.js +91 -0
- package/dist/src/utils/text-parsing.d.ts +36 -0
- package/dist/src/utils/text-parsing.js +75 -0
- package/dist/src/utils/upload-cache.d.ts +34 -0
- package/dist/src/utils/upload-cache.js +93 -0
- package/index.ts +31 -0
- package/node_modules/@eshaz/web-worker/LICENSE +201 -0
- package/node_modules/@eshaz/web-worker/README.md +134 -0
- package/node_modules/@eshaz/web-worker/browser.js +17 -0
- package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
- package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
- package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
- package/node_modules/@eshaz/web-worker/node.js +223 -0
- package/node_modules/@eshaz/web-worker/package.json +54 -0
- package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
- package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
- package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
- package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
- package/node_modules/mpg123-decoder/README.md +265 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
- package/node_modules/mpg123-decoder/index.js +8 -0
- package/node_modules/mpg123-decoder/package.json +58 -0
- package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
- package/node_modules/mpg123-decoder/types.d.ts +30 -0
- package/node_modules/silk-wasm/LICENSE +21 -0
- package/node_modules/silk-wasm/README.md +85 -0
- package/node_modules/silk-wasm/lib/index.cjs +16 -0
- package/node_modules/silk-wasm/lib/index.d.ts +70 -0
- package/node_modules/silk-wasm/lib/index.mjs +16 -0
- package/node_modules/silk-wasm/lib/silk.wasm +0 -0
- package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
- package/node_modules/silk-wasm/package.json +39 -0
- package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
- package/node_modules/simple-yenc/.prettierignore +1 -0
- package/node_modules/simple-yenc/LICENSE +7 -0
- package/node_modules/simple-yenc/README.md +163 -0
- package/node_modules/simple-yenc/dist/esm.js +1 -0
- package/node_modules/simple-yenc/dist/index.js +1 -0
- package/node_modules/simple-yenc/package.json +50 -0
- package/node_modules/simple-yenc/rollup.config.js +27 -0
- package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +548 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +131 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/node_modules/ws/lib/receiver.js +706 -0
- package/node_modules/ws/lib/sender.js +602 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +554 -0
- package/node_modules/ws/lib/websocket.js +1393 -0
- package/node_modules/ws/package.json +70 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/openclaw.plugin.json +17 -0
- package/package.json +70 -0
- package/preload.cjs +33 -0
- package/scripts/cleanup-legacy-plugins.sh +124 -0
- package/scripts/link-sdk-core.cjs +185 -0
- package/scripts/postinstall-link-sdk.js +126 -0
- package/scripts/proactive-api-server.ts +369 -0
- package/scripts/send-proactive.ts +293 -0
- package/scripts/set-markdown.sh +156 -0
- package/scripts/test-sendmedia.ts +116 -0
- package/scripts/upgrade-via-npm.ps1 +460 -0
- package/scripts/upgrade-via-npm.sh +652 -0
- package/scripts/upgrade-via-source.sh +1026 -0
- package/skills/qqbot-channel/SKILL.md +263 -0
- package/skills/qqbot-channel/references/api_references.md +521 -0
- package/skills/qqbot-media/SKILL.md +60 -0
- package/skills/qqbot-remind/SKILL.md +159 -0
- package/src/admin-resolver.ts +181 -0
- package/src/api.ts +1284 -0
- package/src/channel.ts +477 -0
- package/src/config.ts +347 -0
- package/src/credential-backup.ts +72 -0
- package/src/deliver-debounce.ts +229 -0
- package/src/gateway.ts +2245 -0
- package/src/group-history.ts +328 -0
- package/src/image-server.ts +675 -0
- package/src/inbound-attachments.ts +321 -0
- package/src/known-users.ts +353 -0
- package/src/message-gating.ts +190 -0
- package/src/message-queue.ts +352 -0
- package/src/onboarding.ts +274 -0
- package/src/openclaw-plugin-sdk.d.ts +587 -0
- package/src/outbound-deliver.ts +473 -0
- package/src/outbound.ts +1131 -0
- package/src/proactive.ts +530 -0
- package/src/ref-index-store.ts +412 -0
- package/src/reply-dispatcher.ts +334 -0
- package/src/request-context.ts +49 -0
- package/src/runtime.ts +14 -0
- package/src/session-store.ts +303 -0
- package/src/slash-commands.ts +2030 -0
- package/src/startup-greeting.ts +120 -0
- package/src/streaming.ts +1077 -0
- package/src/stt.ts +86 -0
- package/src/tools/channel.ts +281 -0
- package/src/tools/remind.ts +308 -0
- package/src/types.ts +391 -0
- package/src/typing-keepalive.ts +59 -0
- package/src/update-checker.ts +186 -0
- package/src/utils/audio-convert.ts +859 -0
- package/src/utils/chunked-upload.ts +483 -0
- package/src/utils/file-utils.ts +193 -0
- package/src/utils/image-size.ts +266 -0
- package/src/utils/media-send.ts +631 -0
- package/src/utils/media-tags.ts +183 -0
- package/src/utils/payload.ts +265 -0
- package/src/utils/pkg-version.ts +64 -0
- package/src/utils/platform.ts +435 -0
- package/src/utils/ssrf-guard.ts +102 -0
- package/src/utils/text-parsing.ts +85 -0
- package/src/utils/upload-cache.ts +128 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 出站消息投递模块
|
|
3
|
+
*
|
|
4
|
+
* 从 gateway deliver 回调中提取的两大发送管线:
|
|
5
|
+
* 1. parseAndSendMediaTags — 解析 <qqimg/qqvoice/qqvideo/qqfile/qqmedia> 标签并按顺序发送
|
|
6
|
+
* 2. sendPlainReply — 处理不含媒体标签的普通回复(markdown 图片/纯文本+图片)
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ResolvedQQBotAccount } from "./types.js";
|
|
10
|
+
import { sendC2CMessage, sendGroupMessage, sendChannelMessage, sendC2CImageMessage, sendGroupImageMessage } from "./api.js";
|
|
11
|
+
import { sendPhoto, sendMedia as sendMediaAuto, type MediaTargetContext } from "./outbound.js";
|
|
12
|
+
import { chunkText, TEXT_CHUNK_LIMIT } from "./channel.js";
|
|
13
|
+
import { getQQBotRuntime } from "./runtime.js";
|
|
14
|
+
import { getImageSize, formatQQBotMarkdownImage, hasQQBotImageSize } from "./utils/image-size.js";
|
|
15
|
+
import { parseMediaTagsToSendQueue, executeSendQueue, type MediaSendContext } from "./utils/media-send.js";
|
|
16
|
+
import { isLocalPath as isLocalFilePath } from "./utils/platform.js";
|
|
17
|
+
import { filterInternalMarkers } from "./utils/text-parsing.js";
|
|
18
|
+
|
|
19
|
+
// ============ 类型定义 ============
|
|
20
|
+
|
|
21
|
+
export interface DeliverEventContext {
|
|
22
|
+
type: "c2c" | "guild" | "dm" | "group";
|
|
23
|
+
senderId: string;
|
|
24
|
+
messageId: string;
|
|
25
|
+
channelId?: string;
|
|
26
|
+
groupOpenid?: string;
|
|
27
|
+
msgIdx?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface DeliverAccountContext {
|
|
31
|
+
account: ResolvedQQBotAccount;
|
|
32
|
+
qualifiedTarget: string;
|
|
33
|
+
log?: {
|
|
34
|
+
info: (msg: string) => void;
|
|
35
|
+
error: (msg: string) => void;
|
|
36
|
+
debug?: (msg: string) => void;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** token 重试包装 */
|
|
41
|
+
export type SendWithRetryFn = <T>(sendFn: (token: string) => Promise<T>) => Promise<T>;
|
|
42
|
+
|
|
43
|
+
/** 一次性消费引用 ref */
|
|
44
|
+
export type ConsumeQuoteRefFn = () => string | undefined;
|
|
45
|
+
|
|
46
|
+
// ============ 1. 媒体标签解析 + 发送 ============
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 解析回复文本中的媒体标签并按顺序发送。
|
|
50
|
+
*
|
|
51
|
+
* @returns true 如果检测到媒体标签并已处理;false 表示无媒体标签,调用方继续走普通文本管线
|
|
52
|
+
*/
|
|
53
|
+
export async function parseAndSendMediaTags(
|
|
54
|
+
replyText: string,
|
|
55
|
+
event: DeliverEventContext,
|
|
56
|
+
actx: DeliverAccountContext,
|
|
57
|
+
sendWithRetry: SendWithRetryFn,
|
|
58
|
+
consumeQuoteRef: ConsumeQuoteRefFn,
|
|
59
|
+
): Promise<{ handled: boolean; normalizedText: string }> {
|
|
60
|
+
const { account, log } = actx;
|
|
61
|
+
const prefix = `[qqbot:${account.accountId}]`;
|
|
62
|
+
|
|
63
|
+
// 使用 media-send.ts 的统一解析器(内含 normalizeMediaTags + 路径编码修复)
|
|
64
|
+
const { hasMediaTags: hasMedia, sendQueue } = parseMediaTagsToSendQueue(replyText, log);
|
|
65
|
+
|
|
66
|
+
if (!hasMedia || sendQueue.length === 0) {
|
|
67
|
+
return { handled: false, normalizedText: replyText };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
log?.info(`${prefix} Send queue: ${sendQueue.map(item => item.type).join(" -> ")}`);
|
|
71
|
+
|
|
72
|
+
// 构建统一的媒体发送上下文
|
|
73
|
+
const mediaTarget: MediaTargetContext = {
|
|
74
|
+
targetType: event.type === "c2c" ? "c2c" : event.type === "group" ? "group" : "channel",
|
|
75
|
+
targetId: event.type === "c2c" ? event.senderId : event.type === "group" ? event.groupOpenid! : event.channelId!,
|
|
76
|
+
account,
|
|
77
|
+
replyToId: event.messageId,
|
|
78
|
+
logPrefix: prefix,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const mediaSendCtx: MediaSendContext = {
|
|
82
|
+
mediaTarget,
|
|
83
|
+
qualifiedTarget: actx.qualifiedTarget,
|
|
84
|
+
account,
|
|
85
|
+
replyToId: event.messageId,
|
|
86
|
+
log,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// 使用 media-send.ts 的统一执行器
|
|
90
|
+
await executeSendQueue(sendQueue, mediaSendCtx, {
|
|
91
|
+
onSendText: async (textContent) => {
|
|
92
|
+
await sendTextChunks(filterInternalMarkers(textContent), event, actx, sendWithRetry, consumeQuoteRef);
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return { handled: true, normalizedText: replyText };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ============ 2. 非结构化消息发送(普通文本 + 图片) ============
|
|
100
|
+
|
|
101
|
+
export interface PlainReplyPayload {
|
|
102
|
+
text?: string;
|
|
103
|
+
mediaUrls?: string[];
|
|
104
|
+
mediaUrl?: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 发送不含媒体标签的普通回复。
|
|
109
|
+
* 处理 markdown 图片嵌入、Base64 富媒体、纯文本分块、本地媒体自动路由。
|
|
110
|
+
*/
|
|
111
|
+
export async function sendPlainReply(
|
|
112
|
+
payload: PlainReplyPayload,
|
|
113
|
+
replyText: string,
|
|
114
|
+
event: DeliverEventContext,
|
|
115
|
+
actx: DeliverAccountContext,
|
|
116
|
+
sendWithRetry: SendWithRetryFn,
|
|
117
|
+
consumeQuoteRef: ConsumeQuoteRefFn,
|
|
118
|
+
toolMediaUrls: string[],
|
|
119
|
+
): Promise<void> {
|
|
120
|
+
const { account, qualifiedTarget, log } = actx;
|
|
121
|
+
const prefix = `[qqbot:${account.accountId}]`;
|
|
122
|
+
|
|
123
|
+
// 预去重:把 payload 自带的媒体 URL 从 toolMediaUrls 中移除,
|
|
124
|
+
// 防止同一个文件既被 payload.mediaUrl/mediaUrls 发送,又被 toolMediaUrls 重复发送
|
|
125
|
+
if (toolMediaUrls.length > 0) {
|
|
126
|
+
const payloadUrls = new Set<string>();
|
|
127
|
+
if (payload.mediaUrl) payloadUrls.add(payload.mediaUrl);
|
|
128
|
+
if (payload.mediaUrls) for (const u of payload.mediaUrls) payloadUrls.add(u);
|
|
129
|
+
if (payloadUrls.size > 0) {
|
|
130
|
+
const before = toolMediaUrls.length;
|
|
131
|
+
const filtered = toolMediaUrls.filter(url => !payloadUrls.has(url));
|
|
132
|
+
if (filtered.length < before) {
|
|
133
|
+
log?.info(`${prefix} Pre-dedup: removed ${before - filtered.length} payload media URL(s) from toolMediaUrls`);
|
|
134
|
+
toolMediaUrls.length = 0;
|
|
135
|
+
toolMediaUrls.push(...filtered);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const collectedImageUrls: string[] = [];
|
|
141
|
+
const localMediaToSend: string[] = [];
|
|
142
|
+
|
|
143
|
+
const collectImageUrl = (url: string | undefined | null): boolean => {
|
|
144
|
+
if (!url) return false;
|
|
145
|
+
const isHttpUrl = url.startsWith("http://") || url.startsWith("https://");
|
|
146
|
+
const isDataUrl = url.startsWith("data:image/");
|
|
147
|
+
if (isHttpUrl || isDataUrl) {
|
|
148
|
+
if (!collectedImageUrls.includes(url)) {
|
|
149
|
+
collectedImageUrls.push(url);
|
|
150
|
+
log?.info(`${prefix} Collected ${isDataUrl ? "Base64" : "media URL"}: ${isDataUrl ? `(length: ${url.length})` : url.slice(0, 80) + "..."}`);
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
if (isLocalFilePath(url)) {
|
|
155
|
+
if (!localMediaToSend.includes(url)) {
|
|
156
|
+
localMediaToSend.push(url);
|
|
157
|
+
log?.info(`${prefix} Collected local media for auto-routing: ${url}`);
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
return false;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
if (payload.mediaUrls?.length) {
|
|
165
|
+
for (const url of payload.mediaUrls) collectImageUrl(url);
|
|
166
|
+
}
|
|
167
|
+
if (payload.mediaUrl) collectImageUrl(payload.mediaUrl);
|
|
168
|
+
|
|
169
|
+
// 提取 markdown 图片
|
|
170
|
+
const mdImageRegex = /!\[([^\]]*)\]\(([^)]+)\)/gi;
|
|
171
|
+
const mdMatches = [...replyText.matchAll(mdImageRegex)];
|
|
172
|
+
for (const m of mdMatches) {
|
|
173
|
+
const url = m[2]?.trim();
|
|
174
|
+
if (url && !collectedImageUrls.includes(url)) {
|
|
175
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
176
|
+
collectedImageUrls.push(url);
|
|
177
|
+
log?.info(`${prefix} Extracted HTTP image from markdown: ${url.slice(0, 80)}...`);
|
|
178
|
+
} else if (isLocalFilePath(url)) {
|
|
179
|
+
if (!localMediaToSend.includes(url)) {
|
|
180
|
+
localMediaToSend.push(url);
|
|
181
|
+
log?.info(`${prefix} Collected local media from markdown for auto-routing: ${url}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// 提取裸 URL 图片
|
|
188
|
+
const bareUrlRegex = /(?<![(\["'])(https?:\/\/[^\s)"'<>]+\.(?:png|jpg|jpeg|gif|webp)(?:\?[^\s"'<>]*)?)/gi;
|
|
189
|
+
const bareUrlMatches = [...replyText.matchAll(bareUrlRegex)];
|
|
190
|
+
for (const m of bareUrlMatches) {
|
|
191
|
+
const url = m[1];
|
|
192
|
+
if (url && !collectedImageUrls.includes(url)) {
|
|
193
|
+
collectedImageUrls.push(url);
|
|
194
|
+
log?.info(`${prefix} Extracted bare image URL: ${url.slice(0, 80)}...`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const useMarkdown = account.markdownSupport === true;
|
|
199
|
+
log?.info(`${prefix} Markdown mode: ${useMarkdown}, images: ${collectedImageUrls.length}`);
|
|
200
|
+
|
|
201
|
+
let textWithoutImages = filterInternalMarkers(replyText);
|
|
202
|
+
|
|
203
|
+
if (useMarkdown) {
|
|
204
|
+
await sendMarkdownReply(textWithoutImages, collectedImageUrls, mdMatches, bareUrlMatches, event, actx, sendWithRetry, consumeQuoteRef);
|
|
205
|
+
} else {
|
|
206
|
+
await sendPlainTextReply(textWithoutImages, collectedImageUrls, mdMatches, bareUrlMatches, event, actx, sendWithRetry, consumeQuoteRef);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// 发送本地媒体(由 payload.mediaUrl 或 markdown 本地路径触发)
|
|
210
|
+
if (localMediaToSend.length > 0) {
|
|
211
|
+
log?.info(`${prefix} Sending ${localMediaToSend.length} local media via sendMedia auto-routing`);
|
|
212
|
+
for (const mediaPath of localMediaToSend) {
|
|
213
|
+
try {
|
|
214
|
+
const result = await sendMediaAuto({
|
|
215
|
+
to: qualifiedTarget, text: "", mediaUrl: mediaPath,
|
|
216
|
+
accountId: account.accountId, replyToId: event.messageId, account,
|
|
217
|
+
});
|
|
218
|
+
if (result.error) {
|
|
219
|
+
log?.error(`${prefix} sendMedia(auto) error for ${mediaPath}: ${result.error}`);
|
|
220
|
+
await sendTextChunks("发送失败,请稍后重试。", event, actx, sendWithRetry, consumeQuoteRef);
|
|
221
|
+
} else {
|
|
222
|
+
log?.info(`${prefix} Sent local media: ${mediaPath}`);
|
|
223
|
+
}
|
|
224
|
+
} catch (err) {
|
|
225
|
+
log?.error(`${prefix} sendMedia(auto) failed for ${mediaPath}: ${err}`);
|
|
226
|
+
await sendTextChunks("发送失败,请稍后重试。", event, actx, sendWithRetry, consumeQuoteRef);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 转发 tool 阶段收集的媒体(去重:跳过已在 localMediaToSend 或 collectedImageUrls 中发送过的路径)
|
|
232
|
+
if (toolMediaUrls.length > 0) {
|
|
233
|
+
const alreadySent = new Set([...localMediaToSend, ...collectedImageUrls]);
|
|
234
|
+
const dedupedToolMedia = toolMediaUrls.filter(url => !alreadySent.has(url));
|
|
235
|
+
if (dedupedToolMedia.length < toolMediaUrls.length) {
|
|
236
|
+
log?.info(`${prefix} Deduped tool media: ${toolMediaUrls.length} → ${dedupedToolMedia.length} (skipped ${toolMediaUrls.length - dedupedToolMedia.length} already sent via localMedia/collectedImages)`);
|
|
237
|
+
}
|
|
238
|
+
if (dedupedToolMedia.length > 0) {
|
|
239
|
+
log?.info(`${prefix} Forwarding ${dedupedToolMedia.length} tool-collected media URL(s) after block deliver`);
|
|
240
|
+
for (const mediaUrl of dedupedToolMedia) {
|
|
241
|
+
try {
|
|
242
|
+
const result = await sendMediaAuto({
|
|
243
|
+
to: qualifiedTarget, text: "", mediaUrl,
|
|
244
|
+
accountId: account.accountId, replyToId: event.messageId, account,
|
|
245
|
+
});
|
|
246
|
+
if (result.error) {
|
|
247
|
+
log?.error(`${prefix} Tool media forward error: ${result.error}`);
|
|
248
|
+
await sendTextChunks("发送失败,请稍后重试。", event, actx, sendWithRetry, consumeQuoteRef);
|
|
249
|
+
} else {
|
|
250
|
+
log?.info(`${prefix} Forwarded tool media: ${mediaUrl.slice(0, 80)}...`);
|
|
251
|
+
}
|
|
252
|
+
} catch (err) {
|
|
253
|
+
log?.error(`${prefix} Tool media forward failed: ${err}`);
|
|
254
|
+
await sendTextChunks("发送失败,请稍后重试。", event, actx, sendWithRetry, consumeQuoteRef);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
toolMediaUrls.length = 0;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ============ 内部辅助函数 ============
|
|
263
|
+
|
|
264
|
+
/** 发送文本分块(共用逻辑) */
|
|
265
|
+
async function sendTextChunks(
|
|
266
|
+
text: string,
|
|
267
|
+
event: DeliverEventContext,
|
|
268
|
+
actx: DeliverAccountContext,
|
|
269
|
+
sendWithRetry: SendWithRetryFn,
|
|
270
|
+
consumeQuoteRef: ConsumeQuoteRefFn,
|
|
271
|
+
): Promise<void> {
|
|
272
|
+
const { account, log } = actx;
|
|
273
|
+
const prefix = `[qqbot:${account.accountId}]`;
|
|
274
|
+
const chunks = getQQBotRuntime().channel.text.chunkMarkdownText(text, TEXT_CHUNK_LIMIT);
|
|
275
|
+
for (const chunk of chunks) {
|
|
276
|
+
try {
|
|
277
|
+
await sendWithRetry(async (token) => {
|
|
278
|
+
const ref = consumeQuoteRef();
|
|
279
|
+
if (event.type === "c2c") {
|
|
280
|
+
return await sendC2CMessage(token, event.senderId, chunk, event.messageId, ref);
|
|
281
|
+
} else if (event.type === "group" && event.groupOpenid) {
|
|
282
|
+
return await sendGroupMessage(token, event.groupOpenid, chunk, event.messageId);
|
|
283
|
+
} else if (event.channelId) {
|
|
284
|
+
return await sendChannelMessage(token, event.channelId, chunk, event.messageId);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
log?.info(`${prefix} Sent text chunk (${chunk.length}/${text.length} chars): ${chunk.slice(0, 50)}...`);
|
|
288
|
+
} catch (err) {
|
|
289
|
+
log?.error(`${prefix} Failed to send text chunk: ${err}`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Markdown 模式发送 */
|
|
295
|
+
async function sendMarkdownReply(
|
|
296
|
+
textWithoutImages: string,
|
|
297
|
+
imageUrls: string[],
|
|
298
|
+
mdMatches: RegExpMatchArray[],
|
|
299
|
+
bareUrlMatches: RegExpMatchArray[],
|
|
300
|
+
event: DeliverEventContext,
|
|
301
|
+
actx: DeliverAccountContext,
|
|
302
|
+
sendWithRetry: SendWithRetryFn,
|
|
303
|
+
consumeQuoteRef: ConsumeQuoteRefFn,
|
|
304
|
+
): Promise<void> {
|
|
305
|
+
const { account, log } = actx;
|
|
306
|
+
const prefix = `[qqbot:${account.accountId}]`;
|
|
307
|
+
|
|
308
|
+
// 分离图片:公网 URL vs Base64
|
|
309
|
+
const httpImageUrls: string[] = [];
|
|
310
|
+
const base64ImageUrls: string[] = [];
|
|
311
|
+
for (const url of imageUrls) {
|
|
312
|
+
if (url.startsWith("data:image/")) base64ImageUrls.push(url);
|
|
313
|
+
else if (url.startsWith("http://") || url.startsWith("https://")) httpImageUrls.push(url);
|
|
314
|
+
}
|
|
315
|
+
log?.info(`${prefix} Image classification: httpUrls=${httpImageUrls.length}, base64=${base64ImageUrls.length}`);
|
|
316
|
+
|
|
317
|
+
// 发送 Base64 图片
|
|
318
|
+
if (base64ImageUrls.length > 0) {
|
|
319
|
+
log?.info(`${prefix} Sending ${base64ImageUrls.length} image(s) via Rich Media API...`);
|
|
320
|
+
for (const imageUrl of base64ImageUrls) {
|
|
321
|
+
try {
|
|
322
|
+
await sendWithRetry(async (token) => {
|
|
323
|
+
if (event.type === "c2c") {
|
|
324
|
+
await sendC2CImageMessage(token, event.senderId, imageUrl, event.messageId);
|
|
325
|
+
} else if (event.type === "group" && event.groupOpenid) {
|
|
326
|
+
await sendGroupImageMessage(token, event.groupOpenid, imageUrl, event.messageId);
|
|
327
|
+
} else if (event.channelId) {
|
|
328
|
+
log?.info(`${prefix} Channel does not support rich media, skipping Base64 image`);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
log?.info(`${prefix} Sent Base64 image via Rich Media API (size: ${imageUrl.length} chars)`);
|
|
332
|
+
} catch (imgErr) {
|
|
333
|
+
log?.error(`${prefix} Failed to send Base64 image via Rich Media API: ${imgErr}`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// 处理公网 URL 图片
|
|
339
|
+
const existingMdUrls = new Set(mdMatches.map((m) => m[2]));
|
|
340
|
+
const imagesToAppend: string[] = [];
|
|
341
|
+
|
|
342
|
+
for (const url of httpImageUrls) {
|
|
343
|
+
if (!existingMdUrls.has(url)) {
|
|
344
|
+
try {
|
|
345
|
+
const size = await getImageSize(url);
|
|
346
|
+
imagesToAppend.push(formatQQBotMarkdownImage(url, size));
|
|
347
|
+
log?.info(`${prefix} Formatted HTTP image: ${size ? `${size.width}x${size.height}` : "default size"} - ${url.slice(0, 60)}...`);
|
|
348
|
+
} catch (err) {
|
|
349
|
+
log?.info(`${prefix} Failed to get image size, using default: ${err}`);
|
|
350
|
+
imagesToAppend.push(formatQQBotMarkdownImage(url, null));
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// 补充已有 markdown 图片的尺寸信息
|
|
356
|
+
let result = textWithoutImages;
|
|
357
|
+
for (const m of mdMatches) {
|
|
358
|
+
const fullMatch = m[0];
|
|
359
|
+
const imgUrl = m[2];
|
|
360
|
+
const isHttpUrl = imgUrl.startsWith("http://") || imgUrl.startsWith("https://");
|
|
361
|
+
if (isHttpUrl && !hasQQBotImageSize(fullMatch)) {
|
|
362
|
+
try {
|
|
363
|
+
const size = await getImageSize(imgUrl);
|
|
364
|
+
result = result.replace(fullMatch, formatQQBotMarkdownImage(imgUrl, size));
|
|
365
|
+
log?.info(`${prefix} Updated image with size: ${size ? `${size.width}x${size.height}` : "default"} - ${imgUrl.slice(0, 60)}...`);
|
|
366
|
+
} catch (err) {
|
|
367
|
+
log?.info(`${prefix} Failed to get image size for existing md, using default: ${err}`);
|
|
368
|
+
result = result.replace(fullMatch, formatQQBotMarkdownImage(imgUrl, null));
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// 移除裸 URL 图片
|
|
374
|
+
for (const m of bareUrlMatches) {
|
|
375
|
+
result = result.replace(m[0], "").trim();
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// 追加图片
|
|
379
|
+
if (imagesToAppend.length > 0) {
|
|
380
|
+
result = result.trim();
|
|
381
|
+
result = result ? result + "\n\n" + imagesToAppend.join("\n") : imagesToAppend.join("\n");
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// 发送 markdown 文本
|
|
385
|
+
if (result.trim()) {
|
|
386
|
+
const mdChunks = chunkText(result, TEXT_CHUNK_LIMIT);
|
|
387
|
+
for (const chunk of mdChunks) {
|
|
388
|
+
try {
|
|
389
|
+
await sendWithRetry(async (token) => {
|
|
390
|
+
const ref = consumeQuoteRef();
|
|
391
|
+
if (event.type === "c2c") {
|
|
392
|
+
return await sendC2CMessage(token, event.senderId, chunk, event.messageId, ref);
|
|
393
|
+
} else if (event.type === "group" && event.groupOpenid) {
|
|
394
|
+
return await sendGroupMessage(token, event.groupOpenid, chunk, event.messageId);
|
|
395
|
+
} else if (event.channelId) {
|
|
396
|
+
return await sendChannelMessage(token, event.channelId, chunk, event.messageId);
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
log?.info(`${prefix} Sent markdown chunk (${chunk.length}/${result.length} chars) with ${httpImageUrls.length} HTTP images (${event.type})`);
|
|
400
|
+
} catch (err) {
|
|
401
|
+
log?.error(`${prefix} Failed to send markdown message chunk: ${err}`);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** 普通文本模式发送 */
|
|
408
|
+
async function sendPlainTextReply(
|
|
409
|
+
textWithoutImages: string,
|
|
410
|
+
imageUrls: string[],
|
|
411
|
+
mdMatches: RegExpMatchArray[],
|
|
412
|
+
bareUrlMatches: RegExpMatchArray[],
|
|
413
|
+
event: DeliverEventContext,
|
|
414
|
+
actx: DeliverAccountContext,
|
|
415
|
+
sendWithRetry: SendWithRetryFn,
|
|
416
|
+
consumeQuoteRef: ConsumeQuoteRefFn,
|
|
417
|
+
): Promise<void> {
|
|
418
|
+
const { account, log } = actx;
|
|
419
|
+
const prefix = `[qqbot:${account.accountId}]`;
|
|
420
|
+
|
|
421
|
+
const imgMediaTarget: MediaTargetContext = {
|
|
422
|
+
targetType: event.type === "c2c" ? "c2c" : event.type === "group" ? "group" : "channel",
|
|
423
|
+
targetId: event.type === "c2c" ? event.senderId : event.type === "group" ? event.groupOpenid! : event.channelId!,
|
|
424
|
+
account,
|
|
425
|
+
replyToId: event.messageId,
|
|
426
|
+
logPrefix: prefix,
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
let result = textWithoutImages;
|
|
430
|
+
for (const m of mdMatches) result = result.replace(m[0], "").trim();
|
|
431
|
+
for (const m of bareUrlMatches) result = result.replace(m[0], "").trim();
|
|
432
|
+
|
|
433
|
+
// 群聊 URL 点号过滤
|
|
434
|
+
if (result && event.type !== "c2c") {
|
|
435
|
+
result = result.replace(/([a-zA-Z0-9])\.([a-zA-Z0-9])/g, "$1_$2");
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
try {
|
|
439
|
+
for (const imageUrl of imageUrls) {
|
|
440
|
+
try {
|
|
441
|
+
const imgResult = await sendPhoto(imgMediaTarget, imageUrl);
|
|
442
|
+
if (imgResult.error) {
|
|
443
|
+
log?.error(`${prefix} Failed to send image: ${imgResult.error}`);
|
|
444
|
+
await sendTextChunks(`发送图片失败:${imgResult.error}`, event, actx, sendWithRetry, consumeQuoteRef);
|
|
445
|
+
} else {
|
|
446
|
+
log?.info(`${prefix} Sent image via sendPhoto: ${imageUrl.slice(0, 80)}...`);
|
|
447
|
+
}
|
|
448
|
+
} catch (imgErr) {
|
|
449
|
+
log?.error(`${prefix} Failed to send image: ${imgErr}`);
|
|
450
|
+
await sendTextChunks(`发送图片失败:${imgErr}`, event, actx, sendWithRetry, consumeQuoteRef);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (result.trim()) {
|
|
455
|
+
const plainChunks = chunkText(result, TEXT_CHUNK_LIMIT);
|
|
456
|
+
for (const chunk of plainChunks) {
|
|
457
|
+
await sendWithRetry(async (token) => {
|
|
458
|
+
const ref = consumeQuoteRef();
|
|
459
|
+
if (event.type === "c2c") {
|
|
460
|
+
return await sendC2CMessage(token, event.senderId, chunk, event.messageId, ref);
|
|
461
|
+
} else if (event.type === "group" && event.groupOpenid) {
|
|
462
|
+
return await sendGroupMessage(token, event.groupOpenid, chunk, event.messageId);
|
|
463
|
+
} else if (event.channelId) {
|
|
464
|
+
return await sendChannelMessage(token, event.channelId, chunk, event.messageId);
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
log?.info(`${prefix} Sent text chunk (${chunk.length}/${result.length} chars) (${event.type})`);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
} catch (err) {
|
|
471
|
+
log?.error(`${prefix} Send failed: ${err}`);
|
|
472
|
+
}
|
|
473
|
+
}
|