diff --git a/packages/core/src/onebot/message-parser.ts b/packages/core/src/onebot/message-parser.ts index f971aa0f..159dc530 100644 --- a/packages/core/src/onebot/message-parser.ts +++ b/packages/core/src/onebot/message-parser.ts @@ -336,6 +336,31 @@ async function segmentToElement(type: string, data: Record, opt // Anonymous flag — ignored during send, the protocol handles anonymity return null; } + case 'file': { + // OneBot11 file segment referencing a previously-uploaded file + // (returned by `upload_group_file` / `upload_private_file`). + // We deliberately do NOT support `{file: '/path'}` here because + // the upload pipeline is async and lives behind the bridge — + // the element-builder runs synchronously over the parsed + // message. Callers that want upload-and-send should use the + // dedicated `upload_*` actions which now publish the file in + // chat atomically (`actions/group-file.ts`). + const fileId = String(data.file_id ?? data.fileId ?? '').trim(); + if (!fileId) { + log.warn('[MsgParser] file segment without file_id is unsupported (upload first via upload_group_file / upload_private_file)'); + return null; + } + const fileName = String(data.name ?? data.filename ?? data.fileName ?? '').trim(); + const fileSize = intOr(data.size ?? data.fileSize, 0); + const md5Hex = String(data.md5 ?? data.md5Hex ?? '').trim(); + const sha1Hex = String(data.sha1 ?? data.sha1Hex ?? '').trim(); + const elem: MessageElement = { type: 'file', fileId }; + if (fileName) elem.fileName = fileName; + if (fileSize > 0) elem.fileSize = fileSize; + if (md5Hex) elem.md5Hex = md5Hex; + if (sha1Hex) elem.sha1Hex = sha1Hex; + return elem; + } default: console.warn(`[MsgParser] unsupported segment type: ${type}`); return null; diff --git a/packages/core/tests/message-parser.test.ts b/packages/core/tests/message-parser.test.ts index a7835c3a..debc405b 100644 --- a/packages/core/tests/message-parser.test.ts +++ b/packages/core/tests/message-parser.test.ts @@ -437,4 +437,48 @@ describe('parseMessage', () => { expect(result).toEqual([]); }); }); + + describe('file segment', () => { + // Closes the parser-side of the "upload says ok but message is + // empty" bug. Before this, `{type:'file', file_id:'…'}` segments + // fell through to the default warn-and-drop branch — which made + // `send_msg([{type:'file', file_id:''}])` produce either + // a "message is empty" error or, when mixed with text, a message + // with the file segment quietly missing. + it('parses {type:"file", file_id:"x"} into a file MessageElement', async () => { + const result = await parseMessage( + [{ type: 'file', data: { file_id: 'fid-1', name: 'a.zip', size: 123 } } as any], + false, + ); + expect(result).toEqual([ + { type: 'file', fileId: 'fid-1', fileName: 'a.zip', fileSize: 123 }, + ]); + }); + + it('drops a file segment with no file_id (upload-first contract)', async () => { + // Path-only segments aren't supported here — callers must + // upload via `upload_group_file` / `upload_private_file` first + // to obtain a file_id. Dropping with a warning is preferable + // to silently kicking off an upload from inside the message + // parser, which can't fail loudly without changing call sites. + const result = await parseMessage( + [{ type: 'file', data: { file: '/local/path/a.bin' } } as any], + false, + ); + expect(result).toEqual([]); + }); + + it('accepts fileId / filename / fileName / md5 / sha1 aliases', async () => { + const result = await parseMessage( + [{ + type: 'file', + data: { fileId: 'fid-2', filename: 'b.zip', fileSize: 456, md5: 'AB', sha1: 'CD' }, + } as any], + false, + ); + expect(result).toEqual([ + { type: 'file', fileId: 'fid-2', fileName: 'b.zip', fileSize: 456, md5Hex: 'AB', sha1Hex: 'CD' }, + ]); + }); + }); });