fix(onebot): parse {type:'file', file_id:'…'} segments instead of dropping them

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:'<previously uploaded>'}])

— 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.
This commit is contained in:
DrSmoothl committed 2026-05-19 12:42:47 +08:00
1 parent e637647877
commit 10154cfbff
2 files changed
+69

No files matched your search

@@ -336,6 +336,31 @@ async function segmentToElement(type: string, data: Record<string, unknown>, 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;