fix(onebot): unify message segment type normalization

Keep empty-placeholder detection aligned with strict segment parsing.
This commit is contained in:
DrSmoothl committed 2026-07-23 12:18:02 +08:00
1 parent edd2ac278a
commit b884a1c479
2 files changed
+22 -5

No files matched your search

+9 -5
View File
@@ -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<string, unknown>, options?: ParseMessageOptions): Promise<MessageElement | null> {
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;
}
@@ -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,