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): recover MIME type for QQ c2c file attachments so audio reaches the model (#16063)

Arvin Xu4386a42b9225
diff --git a/apps/server/src/services/aiAgent/ingestAttachment.ts b/apps/server/src/services/aiAgent/ingestAttachment.ts
index 0cc7f2b5c74..b486a94f4fc 100644
--- a/apps/server/src/services/aiAgent/ingestAttachment.ts
+++ b/apps/server/src/services/aiAgent/ingestAttachment.ts
@@ -129,8 +129,14 @@ export async function ingestAttachment(
throw new Error('AttachmentSource must have either buffer or url');
}
- // 2. MIME correction from filename
- if (mimeType === 'application/octet-stream' && source.name) {
+ // 2. MIME correction from filename.
+ // Recover whenever we don't have a usable MIME type β€” both the generic
+ // `application/octet-stream` and bogus non-MIME values some platforms send
+ // (e.g. QQ labels c2c file attachments as `"file"`). Without the `includes('/')`
+ // guard, a value like `"file"` slips through and an `.m4a` never gets
+ // classified as audio, so it's parsed as a document instead of passed to
+ // audio-capable models.
+ if ((mimeType === 'application/octet-stream' || !mimeType.includes('/')) && source.name) {
const inferred = mime.getType(source.name);
if (inferred) {
log('ingestAttachment: inferred mimeType from filename: %s -> %s', source.name, inferred);
diff --git a/packages/chat-adapter-qq/package.json b/packages/chat-adapter-qq/package.json
index 79348b84a38..27cfe9d8ff8 100644
--- a/packages/chat-adapter-qq/package.json
+++ b/packages/chat-adapter-qq/package.json
@@ -16,7 +16,8 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
- "chat": "^4.23.0"
+ "chat": "^4.23.0",
+ "mime": "^4.1.0"
},
"devDependencies": {
"@types/node": "^24.13.2",
diff --git a/packages/chat-adapter-qq/src/adapter.test.ts b/packages/chat-adapter-qq/src/adapter.test.ts
index 7075417e19b..904d85966c7 100644
--- a/packages/chat-adapter-qq/src/adapter.test.ts
+++ b/packages/chat-adapter-qq/src/adapter.test.ts
@@ -295,6 +295,27 @@ describe('QQAdapter', () => {
expect(message?.attachments[0].type).toBe('file');
});
+ it('should infer mime type from filename when content_type is the bare "file" label', async () => {
+ // QQ delivers c2c file attachments with content_type === 'file' (a coarse
+ // category, not a real MIME type). It must be recovered from the filename
+ // so an .m4a is classified as audio instead of an unreadable document.
+ const attachment = makeAttachment({
+ content_type: 'file',
+ filename: 'Broadstone Amelia 5.m4a',
+ });
+ const payload = makeWebhookPayload(QQ_EVENT_TYPES.GROUP_AT_MESSAGE_CREATE, {
+ attachments: [attachment],
+ content: 'audio file',
+ });
+ await adapter.handleWebhook(makeRequest(payload));
+
+ const factory = vi.mocked(mockChat.processMessage).mock.calls[0]?.[2];
+ const message = await factory?.();
+
+ expect(message?.attachments[0].mimeType).toBe('audio/mp4');
+ expect(message?.attachments[0].type).toBe('audio');
+ });
+
it('should map multiple attachments', async () => {
const attachments = [
makeAttachment({ content_type: 'image/png', filename: 'a.png' }),
diff --git a/packages/chat-adapter-qq/src/adapter.ts b/packages/chat-adapter-qq/src/adapter.ts
index 95059f65813..214b05f9dea 100644
--- a/packages/chat-adapter-qq/src/adapter.ts
+++ b/packages/chat-adapter-qq/src/adapter.ts
@@ -14,6 +14,7 @@ import type {
WebhookOptions,
} from 'chat';
import { Message, parseMarkdown } from 'chat';
+import mime from 'mime';
import { QQApiClient } from './api';
import { signWebhookResponse } from './crypto';
@@ -395,20 +396,35 @@ export class QQAdapter implements Adapter<QQThreadId, QQRawMessage> {
if (!qqAttachments || qqAttachments.length === 0) return [];
return qqAttachments.map((a) => {
- const type = this.resolveAttachmentType(a.content_type);
+ // QQ's `content_type` is not always a real MIME type β€” for c2c file
+ // attachments it comes back as the coarse category label `"file"`. Trusting
+ // it verbatim mislabels e.g. an `.m4a` as `"file"` instead of `audio/mp4`,
+ // which then defeats the filename-based MIME recovery in ingestAttachment
+ // (that only re-infers for `application/octet-stream`). Fall back to the
+ // filename when content_type isn't a usable MIME type.
+ const mimeType = this.resolveMimeType(a.content_type, a.filename);
return {
fetchData: () => this.fetchAttachmentData(a.url),
height: a.height,
- mimeType: a.content_type,
+ mimeType,
name: a.filename,
size: a.size,
- type,
+ type: this.resolveAttachmentType(mimeType),
url: a.url,
width: a.width,
} as Attachment;
});
}
+ /**
+ * Resolve a usable MIME type from QQ's `content_type`, falling back to
+ * filename-based inference when QQ sends a non-MIME value (e.g. `"file"`).
+ */
+ private resolveMimeType(contentType: string | undefined, filename?: string): string {
+ if (contentType && contentType.includes('/')) return contentType;
+ return (filename && mime.getType(filename)) || 'application/octet-stream';
+ }
+
private resolveAttachmentType(contentType: string): 'image' | 'video' | 'audio' | 'file' {
if (contentType.startsWith('image/')) return 'image';
if (contentType.startsWith('video/')) return 'video';