From fc80a598c0ce90daf3cae98fbc106a2a8ffc5c15 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 20 Jul 2026 14:03:13 +0800 Subject: [PATCH] fix(onebot): return AI voice message IDs Build send_group_ai_record results from the canonical group-message sequence returned by the Core receipt correlator, using the same message ID mapping as normal inbound events. Stop reporting the historical placeholder value of zero and surface receipt failures through the standard Action error path. Preserve the existing get_ai_record URL response and update the SDK result documentation without changing its public shape. Add Action-level coverage for canonical ID generation and missing-receipt failures. Refs #250 --- packages/onebot/src/actions/extended.ts | 16 ++++++---- .../onebot/tests/extended-actions.test.ts | 30 +++++++++++++++---- packages/sdk/src/types/results.ts | 2 +- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/packages/onebot/src/actions/extended.ts b/packages/onebot/src/actions/extended.ts index 070b6a46..1379340e 100644 --- a/packages/onebot/src/actions/extended.ts +++ b/packages/onebot/src/actions/extended.ts @@ -4,6 +4,7 @@ import { asNumber, asString } from '../api-handler'; import type { ForwardPreviewMeta } from '../modules/message-actions'; import { JsonObject, RETCODE, failedResponse, okResponse } from '../types'; import { defineAction, groupAction, groupUserAction, f } from '../action-kit'; +import { GROUP_MESSAGE_EVENT, hashMessageIdInt32 } from '../message-id'; const DOWNLOAD_FILE_MAX_BYTES = 1024 * 1024 * 1024; // 1 GiB const DOWNLOAD_FILE_TIMEOUT_MS = 60_000; @@ -1846,20 +1847,25 @@ export const actions = [ }, }), - // NapCat 的 send_group_ai_record 是仅产生副作用的调用。 - // 调用 fetchAiVoice 会把语音发布到群里。 - // 返回的 message_id 始终为 0,因为 oidb 调用不会回显消息 ID。 groupAction({ name: 'send_group_ai_record', summary: '发送 AI 语音到群', + returns: '{ message_id }:已发送 AI 语音的 OneBot 消息 ID。', params: { character: f.string({ allowEmpty: false }), text: f.string({ allowEmpty: false }), chat_type: f.int({ min: 0 }).default(1), }, run: async (p, ctx) => { - await ctx.bridge.apis.extras.fetchAiVoice(p.group_id, p.character, p.text, p.chat_type); - return okResponse({ message_id: 0 }); + const receipt = await ctx.bridge.apis.extras.sendAiVoice( + p.group_id, + p.character, + p.text, + p.chat_type, + ); + return okResponse({ + message_id: hashMessageIdInt32(receipt.sequence, p.group_id, GROUP_MESSAGE_EVENT), + }); }, }), diff --git a/packages/onebot/tests/extended-actions.test.ts b/packages/onebot/tests/extended-actions.test.ts index 733ac0ea..021c6287 100644 --- a/packages/onebot/tests/extended-actions.test.ts +++ b/packages/onebot/tests/extended-actions.test.ts @@ -9,6 +9,7 @@ import { ApiHandler, type ApiActionContext } from '../src/api-handler'; import type { BridgeInterface } from '../../src/bridge/bridge-interface'; import type { GroupRequestInfo } from '@snowluma/protocol/qq-info'; import type { MessageMeta } from '../src/types'; +import { GROUP_MESSAGE_EVENT, hashMessageIdInt32 } from '../src/message-id'; function fakeMeta(overrides: Partial = {}): MessageMeta { return { @@ -70,6 +71,7 @@ const APIS_ROUTING: Record = { getStrangerStatus: ['extras', 'getStrangerStatus'], fetchAiVoiceList: ['extras', 'fetchAiVoiceList'], fetchAiVoice: ['extras', 'fetchAiVoice'], + sendAiVoice: ['extras', 'sendAiVoice'], }; function fakeBridge(overrides: Record = {}): BridgeInterface { @@ -757,19 +759,37 @@ describe('extended-actions / get_ai_record', () => { }); describe('extended-actions / send_group_ai_record', () => { - it('side-effects fetchAiVoice (no URL fetch) and returns message_id=0', async () => { - const fetchAiVoice = vi.fn(async () => ({ fileUuid: 'uuid' })); + it('returns the canonical group message id from the matched self-message receipt', async () => { + const sendAiVoice = vi.fn(async () => ({ + sequence: 319, + })); const fetchGroupPttUrlByNode = vi.fn(); const bridge = fakeBridge({ - fetchAiVoice: fetchAiVoice as any, + sendAiVoice: sendAiVoice as any, fetchGroupPttUrlByNode: fetchGroupPttUrlByNode as any, }); const res = await makeHandler(fakeCtx(bridge)).handle('send_group_ai_record', { group_id: 100, character: 'v', text: 'hi', }); - expect(fetchAiVoice).toHaveBeenCalledOnce(); + expect(sendAiVoice).toHaveBeenCalledWith(100, 'v', 'hi', 1); expect(fetchGroupPttUrlByNode).not.toHaveBeenCalled(); - expect(res).toMatchObject({ status: 'ok', data: { message_id: 0 } }); + expect(res).toMatchObject({ + status: 'ok', + data: { message_id: hashMessageIdInt32(319, 100, GROUP_MESSAGE_EVENT) }, + }); + }); + + it('surfaces a missing canonical receipt instead of returning a fake id', async () => { + const bridge = fakeBridge({ + sendAiVoice: (async () => { + throw new Error('AI voice was published but no matching group-message receipt arrived'); + }) as any, + }); + const res = await makeHandler(fakeCtx(bridge)).handle('send_group_ai_record', { + group_id: 100, character: 'v', text: 'hi', + }); + expect(res).toMatchObject({ status: 'failed', retcode: 100 }); + expect(res.wording).toMatch(/group-message receipt/i); }); }); diff --git a/packages/sdk/src/types/results.ts b/packages/sdk/src/types/results.ts index 4fe5d7c8..5fdfa31c 100644 --- a/packages/sdk/src/types/results.ts +++ b/packages/sdk/src/types/results.ts @@ -205,7 +205,7 @@ export interface UserOnlineStatus { ext_status: number; } -// — send_group_ai_record 返回(合成是异步副作用,message_id 总为 0) — +// — send_group_ai_record 返回 — export interface SendGroupAiRecordResult { message_id: number; }