From 10154cfbff9e25742dd7ce7fc3896cc2cc1a2725 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Tue, 19 May 2026 12:42:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(onebot):=20parse=20`{type:'file',=20file=5F?= =?UTF-8?q?id:'=E2=80=A6'}`=20segments=20instead=20of=20dropping=20them?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The companion to `e637647`. That commit fixed `upload_group_file` / `upload_private_file` to publish the file as a chat message atomically (matching napcat). This one fixes the OTHER half of the same bug: when a bot framework does send_msg([{type:'file', file_id:''}]) — attaching an already-uploaded file as a message segment — the parser used to fall through to its default warn-and-drop branch (`message-parser.ts:340`). The dropped segment then either: - became "message is empty" if it was the only segment, or - silently disappeared if it shared the payload with text, producing a chat bubble missing the file. Adds a `case 'file':` that maps the OneBot segment shape to the existing `MessageElement{type:'file', fileId, fileName, fileSize, md5Hex, sha1Hex}` that `element-builder` already knows how to emit as `TransElem(24)`. Accepts the common alias spellings (`file_id`/`fileId`, `name`/`filename`/`fileName`, `size`/`fileSize`, `md5`/`md5Hex`, `sha1`/`sha1Hex`). Deliberately rejects `{file: '/path'}` form — kicking off an upload from inside the synchronous parser would either swallow errors or restructure the whole parseMessage signature, neither worth it. Callers wanting upload-and-send should keep using `upload_group_file` / `upload_private_file`, which now does both atomically. core: 621/621 tests pass (+3), typecheck clean. --- packages/core/src/onebot/message-parser.ts | 25 ++++++++++++ packages/core/tests/message-parser.test.ts | 44 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) 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' }, + ]); + }); + }); });