From 129b091081baa7e73b67ab9598adc7beb38c281e Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sun, 9 Aug 2026 17:25:48 +0800 Subject: [PATCH] fix(onebot): skip unusable reply segments Keep delivering valid message content when a reply target cannot be resolved, while retaining strict validation for all other segments. Fixes #339 --- .../src/event-converter/element-codecs.ts | 23 ++++++++++++------- packages/onebot/src/message-parser.ts | 5 ++++ packages/onebot/tests/message-parser.test.ts | 13 +++++++---- packages/onebot/tests/send-group-file.test.ts | 20 ++++++++-------- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/packages/onebot/src/event-converter/element-codecs.ts b/packages/onebot/src/event-converter/element-codecs.ts index 206a373e..97163182 100644 --- a/packages/onebot/src/event-converter/element-codecs.ts +++ b/packages/onebot/src/event-converter/element-codecs.ts @@ -54,7 +54,7 @@ type CodecOutput = T extends 'image' export interface ElementCodec { /** S 收·转:MessageElement → OneBot 段。 */ toSegment?: (element: MessageElementOf, ctx: ToSegmentContext) => Promise; - /** P 发·解:OneBot 段 data → MessageElement(null = 无法构造,parser 会 typed reject)。 */ + /** P 发·解:OneBot 段 data → MessageElement(null = 无法构造;reply 可作为可选修饰段被跳过)。 */ fromSegment?: (data: Record, options?: ParseMessageOptions) => Promise | null>; } @@ -203,8 +203,18 @@ export const ELEMENT_CODECS = { return { type: 'reply', data: { id: String(id) } }; }, async fromSegment(data, options) { - const id = intOr(data.id, 0); - if (id === 0) return null; + let id: number; + try { + id = intOr(data.id, 0); + } catch (error) { + if (!(error instanceof MessageElementValidationError)) throw error; + log.warn('could not quote the target message'); + return null; + } + if (id <= 0) { + log.warn('could not quote the target message'); + return null; + } const meta = options?.resolveReplyMeta?.(id) ?? null; if (options?.resolveReplySequence) { @@ -231,11 +241,8 @@ export const ELEMENT_CODECS = { // message and therefore has no QQ server sequence. Treating the opaque // OneBot id as a direct sequence would publish a malformed reply. if (meta?.sequenceAuthoritative === false) { - throw new MessageElementValidationError( - 'INVALID_FIELD', - `reply target ${id} has no authoritative QQ sequence`, - 'reply', - ); + log.warn('could not quote the target message'); + return null; } // Backward-compatible path: allow direct seq reply IDs. diff --git a/packages/onebot/src/message-parser.ts b/packages/onebot/src/message-parser.ts index 9fa7d8d8..989f6248 100644 --- a/packages/onebot/src/message-parser.ts +++ b/packages/onebot/src/message-parser.ts @@ -396,6 +396,11 @@ export async function segmentToElement(type: string, data: Record { [{ type: 'at', data: { qq: '123abc' } }] as any, false, )).rejects.toMatchObject({ code: 'INVALID_FIELD' }); - await expect(parseMessage( - [{ type: 'reply', data: { id: '456junk' } }] as any, - false, - )).rejects.toMatchObject({ code: 'INVALID_FIELD' }); await expect(parseMessage( [{ type: 'face', data: { id: 1.9 } }] as any, false, )).rejects.toMatchObject({ code: 'INVALID_FIELD' }); }); + it('skips an unusable reply segment while preserving sendable content', async () => { + const result = await parseMessage([ + { type: 'reply', data: { id: 'notice-event-id' } }, + { type: 'text', data: { text: 'still sent' } }, + ] as any, false); + + expect(result).toEqual([{ type: 'text', text: 'still sent' }]); + }); + it('parses image segment', async () => { const result = await parseMessage( [{ type: 'image', data: { file: 'https://example.com/img.png' } }] as any, diff --git a/packages/onebot/tests/send-group-file.test.ts b/packages/onebot/tests/send-group-file.test.ts index 9e161576..4e06d459 100644 --- a/packages/onebot/tests/send-group-file.test.ts +++ b/packages/onebot/tests/send-group-file.test.ts @@ -111,7 +111,7 @@ describe('send_group_msg with {type:"file"} segment', () => { } }); - it('rejects replying to an OIDB-only group file id as a QQ sequence (#254)', async () => { + it('skips an OIDB-only group-file reply and sends the remaining content (#254)', async () => { const messageStore = new MessageStore(':memory:'); try { const sendGroup = vi.fn(async (_groupId: number, _elements: any[]) => goodReceipt); @@ -133,21 +133,21 @@ describe('send_group_msg with {type:"file"} segment', () => { await expect(sendGroupMessage(ctx, 12345, [ { type: 'reply', data: { id: file.messageId } }, { type: 'text', data: { text: 'received' } }, - ] as any, false)).rejects.toMatchObject({ - code: 'INVALID_FIELD', - elementType: 'reply', - }); - expect(sendGroup).not.toHaveBeenCalled(); + ] as any, false)).resolves.toMatchObject({ messageId: expect.any(Number) }); + expect(sendGroup).toHaveBeenCalledWith(12345, [ + { type: 'text', text: 'received' }, + ]); } finally { messageStore.close(); } }); - it('rejects a local-only reply id even when its full event was not cached', async () => { + it('skips a local-only reply id even when its full event was not cached', async () => { const messageStore = new MessageStore(':memory:'); try { const sendGroup = vi.fn(async (_groupId: number, _elements: any[]) => goodReceipt); const bridge = fakeBridge({ + identity: { nickname: 'Bot' }, apis: { message: { sendGroup } } as any, resolveUserUid: vi.fn(), } as any); @@ -166,8 +166,10 @@ describe('send_group_msg with {type:"file"} segment', () => { await expect(sendGroupMessage(ctx, 12345, [ { type: 'reply', data: { id: 77 } }, { type: 'text', data: { text: 'received' } }, - ] as any, false)).rejects.toMatchObject({ code: 'INVALID_FIELD', elementType: 'reply' }); - expect(sendGroup).not.toHaveBeenCalled(); + ] as any, false)).resolves.toMatchObject({ messageId: expect.any(Number) }); + expect(sendGroup).toHaveBeenCalledWith(12345, [ + { type: 'text', text: 'received' }, + ]); } finally { messageStore.close(); }