Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(bot): warn when a WeChat file attachment can't be retrieved instead of silently dropping it (#16087)

Arvin Xubd034c3aef3e
diff --git a/apps/server/src/services/bot/platforms/wechat/client.test.ts b/apps/server/src/services/bot/platforms/wechat/client.test.ts
index d6061c1c85d..639fdcb0769 100644
--- a/apps/server/src/services/bot/platforms/wechat/client.test.ts
+++ b/apps/server/src/services/bot/platforms/wechat/client.test.ts
@@ -263,9 +263,10 @@ describe('WechatGatewayClient', () => {
expect.anything(), // WechatApiClient instance
raw,
);
- expect(result).toEqual([
- { buffer, mimeType: 'image/jpeg', name: 'image.jpg', size: undefined },
- ]);
+ expect(result).toEqual({
+ files: [{ buffer, mimeType: 'image/jpeg', name: 'image.jpg', size: undefined }],
+ warnings: undefined,
+ });
});
it('returns undefined when downloadMediaFromRawMessage resolves to an empty array', async () => {
@@ -276,6 +277,34 @@ describe('WechatGatewayClient', () => {
expect(result).toBeUndefined();
});
+ it('warns when a FILE item has no downloadable media (e.g. oversized) and was dropped', async () => {
+ // WeChat relays oversized files as metadata only β€” no CDN media handle β€”
+ // so downloadMediaFromRawMessage returns nothing for them. We must surface
+ // a warning instead of silently passing only the `[file: name]` text.
+ mockDownloadMediaFromRawMessage.mockResolvedValue([]);
+ const client = createClient();
+ const result = (await client.extractFiles!(
+ makeMessage({
+ item_list: [
+ {
+ file_item: {
+ file_name: 'October 11, 2023 Alta Town Council Meeting Audio.mp3',
+ len: '132800970',
+ },
+ type: 4, // MessageItemType.FILE
+ },
+ ],
+ }),
+ )) as { files?: unknown[]; warnings?: string[] } | undefined;
+ expect(result?.files).toBeUndefined();
+ expect(result?.warnings).toHaveLength(1);
+ expect(result?.warnings?.[0]).toContain(
+ 'October 11, 2023 Alta Town Council Meeting Audio.mp3',
+ );
+ expect(result?.warnings?.[0]).toContain('126.6 MB');
+ expect(result?.warnings?.[0]).toContain('could not be retrieved');
+ });
+
it('maps file attachments preserving name + size', async () => {
const buffer = Buffer.from('pdf-bytes');
mockDownloadMediaFromRawMessage.mockResolvedValue([
@@ -303,9 +332,10 @@ describe('WechatGatewayClient', () => {
],
}),
);
- expect(result).toEqual([
- { buffer, mimeType: 'application/pdf', name: 'report.pdf', size: 4096 },
- ]);
+ expect(result).toEqual({
+ files: [{ buffer, mimeType: 'application/pdf', name: 'report.pdf', size: 4096 }],
+ warnings: undefined,
+ });
});
it('maps multiple attachments in a single message', async () => {
@@ -324,10 +354,13 @@ describe('WechatGatewayClient', () => {
],
}),
);
- expect(result).toEqual([
- { buffer: imageBuf, mimeType: 'image/jpeg', name: 'image.jpg', size: undefined },
- { buffer: voiceBuf, mimeType: 'audio/silk', name: undefined, size: undefined },
- ]);
+ expect(result).toEqual({
+ files: [
+ { buffer: imageBuf, mimeType: 'image/jpeg', name: 'image.jpg', size: undefined },
+ { buffer: voiceBuf, mimeType: 'audio/silk', name: undefined, size: undefined },
+ ],
+ warnings: undefined,
+ });
});
it('propagates errors from downloadMediaFromRawMessage as undefined gracefully', async () => {
diff --git a/apps/server/src/services/bot/platforms/wechat/client.ts b/apps/server/src/services/bot/platforms/wechat/client.ts
index 07cc1310b29..ec41b45aac2 100644
--- a/apps/server/src/services/bot/platforms/wechat/client.ts
+++ b/apps/server/src/services/bot/platforms/wechat/client.ts
@@ -2,6 +2,7 @@ import type { WechatRawMessage } from '@lobechat/chat-adapter-wechat';
import {
createWechatAdapter,
downloadMediaFromRawMessage,
+ MessageItemType,
MessageState,
MessageType,
WechatApiClient,
@@ -20,6 +21,7 @@ import {
type BotPlatformRuntimeContext,
type BotProviderConfig,
ClientFactory,
+ type ExtractFilesResult,
type MessengerContent,
messengerContentText,
type PlatformClient,
@@ -347,30 +349,63 @@ class WechatGatewayClient implements PlatformClient {
* `parseRawEvent` runs at adapter parse time β€” including the cascading
* image fallback (CDN main β†’ thumb β†’ direct URL).
*/
- async extractFiles(message: Message): Promise<AttachmentSource[] | undefined> {
+ async extractFiles(message: Message): Promise<ExtractFilesResult | undefined> {
const raw = (message as any).raw as WechatRawMessage | undefined;
if (!raw?.item_list?.length) return undefined;
log('extractFiles: msgId=%s, items=%d', (message as any).id, raw.item_list.length);
const attachments = await downloadMediaFromRawMessage(this.api, raw);
- if (attachments.length === 0) {
+
+ // Detect FILE items that arrived as metadata only. WeChat does not relay a
+ // downloadable CDN media descriptor for oversized files, so
+ // downloadMediaFromRawMessage silently drops them. Without a warning the
+ // agent only sees the bare `[file: name]` text placeholder (from the
+ // adapter's extractText) and hallucinates that it received the file β€” e.g.
+ // claiming it can't "hear" an audio it never actually got. Surface a
+ // warning so the model can tell the user the file couldn't be retrieved.
+ const downloadedNames = new Set(
+ attachments.map((att: any) => att.name).filter(Boolean) as string[],
+ );
+ const warnings: string[] = [];
+ for (const item of raw.item_list) {
+ if (item.type !== MessageItemType.FILE || !item.file_item) continue;
+ const fileName = item.file_item.file_name;
+ if (fileName && downloadedNames.has(fileName)) continue;
+ const sizeBytes = Number(item.file_item.len);
+ const sizeHint =
+ Number.isFinite(sizeBytes) && sizeBytes > 0
+ ? ` (${(sizeBytes / (1024 * 1024)).toFixed(1)} MB)`
+ : '';
+ warnings.push(
+ `File "${fileName || 'unknown'}"${sizeHint} could not be retrieved from WeChat ` +
+ `(it may be too large) and was not processed.`,
+ );
+ }
+
+ if (attachments.length === 0 && warnings.length === 0) {
log('extractFiles: no media items resolved for msgId=%s', (message as any).id);
return undefined;
}
log(
- 'extractFiles: resolved %d media item(s) for msgId=%s',
+ 'extractFiles: resolved %d media item(s), %d warning(s) for msgId=%s',
attachments.length,
+ warnings.length,
(message as any).id,
);
- return attachments.map((att: any) => ({
+ const files: AttachmentSource[] = attachments.map((att: any) => ({
buffer: att.buffer,
mimeType: att.mimeType,
name: att.name,
size: att.size,
}));
+
+ return {
+ files: files.length > 0 ? files : undefined,
+ warnings: warnings.length > 0 ? warnings : undefined,
+ };
}
getMessenger(platformThreadId: string): PlatformMessenger {