From b884a1c4790759ab2a57ffd4b1773757f69a84d7 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 23 Jul 2026 12:18:02 +0800 Subject: [PATCH] fix(onebot): unify message segment type normalization Keep empty-placeholder detection aligned with strict segment parsing. --- packages/onebot/src/message-parser.ts | 14 +++++++++----- packages/onebot/tests/message-parser.test.ts | 13 +++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/onebot/src/message-parser.ts b/packages/onebot/src/message-parser.ts index b4968b7c..c1cb8074 100644 --- a/packages/onebot/src/message-parser.ts +++ b/packages/onebot/src/message-parser.ts @@ -64,6 +64,10 @@ function isSegmentArray(val: unknown): val is MessageSegment[] { ); } +function normalizeSegmentType(type: string): string { + return type.toLowerCase(); +} + function outboundInputSegmentTypes(message: JsonValue, autoEscape: boolean): string[] { if (typeof message === 'string') { if (autoEscape) return ['text']; @@ -71,7 +75,7 @@ function outboundInputSegmentTypes(message: JsonValue, autoEscape: boolean): str let lastIndex = 0; for (const match of message.matchAll(CQ_REGEX)) { if (match.index! > lastIndex) types.push('text'); - types.push((match[1] ?? '').toLowerCase()); + types.push(normalizeSegmentType(match[1] ?? '')); lastIndex = match.index! + match[0].length; } if (lastIndex < message.length) types.push('text'); @@ -81,12 +85,12 @@ function outboundInputSegmentTypes(message: JsonValue, autoEscape: boolean): str return message.flatMap((item) => { if (typeof item !== 'object' || item === null || Array.isArray(item)) return []; const type = (item as { type?: unknown }).type; - return typeof type === 'string' ? [type.toLowerCase()] : []; + return typeof type === 'string' ? [normalizeSegmentType(type)] : []; }); } if (typeof message === 'object' && message !== null) { const type = (message as { type?: unknown }).type; - return typeof type === 'string' ? [type.toLowerCase()] : []; + return typeof type === 'string' ? [normalizeSegmentType(type)] : []; } return []; } @@ -173,7 +177,7 @@ function requireCoordinate( } export async function segmentToElement(type: string, data: Record, options?: ParseMessageOptions): Promise { - const normalizedType = type.toLowerCase(); + const normalizedType = normalizeSegmentType(type); // Ordinary OneBot segment fields are scalar. Reject objects/arrays before // codecs can stringify them into "[object Object]" and alter caller intent. @@ -411,7 +415,7 @@ export async function parseMessage(message: JsonValue, autoEscape: boolean, opti // Some OneBot clients insert empty text between media as a separator. // Only the exact empty string is a no-op; malformed and whitespace text // must still pass through strict element validation. - if (seg.type.trim().toLowerCase() === 'text' && data.text === '') { + if (normalizeSegmentType(seg.type) === 'text' && data.text === '') { log.debug('ignored explicit empty text placeholder in message segment array'); continue; } diff --git a/packages/onebot/tests/message-parser.test.ts b/packages/onebot/tests/message-parser.test.ts index 6fd101b8..10514574 100644 --- a/packages/onebot/tests/message-parser.test.ts +++ b/packages/onebot/tests/message-parser.test.ts @@ -148,6 +148,19 @@ describe('parseMessage', () => { }); }); + it('does not treat whitespace-padded segment types as empty text placeholders', async () => { + await expect(parseMessage( + [ + { type: 'image', data: { file: 'https://example.com/image.png' } }, + { type: ' text ', data: { text: '' } }, + ] as any, + false, + )).rejects.toMatchObject({ + code: 'UNKNOWN_TYPE', + elementType: ' text ', + }); + }); + it('preserves whitespace-only text segments', async () => { await expect(parseMessage( [{ type: 'text', data: { text: ' ' } }] as any,