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
This commit is contained in:
DrSmoothl committed 2026-07-20 14:03:13 +08:00
1 parent 297f0428cc
commit fc80a598c0
3 files changed
+37 -11

No files matched your search

+11 -5
View File
@@ -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),
});
},
}),
+25 -5
View File
@@ -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> = {}): MessageMeta {
return {
@@ -70,6 +71,7 @@ const APIS_ROUTING: Record<string, [string, string]> = {
getStrangerStatus: ['extras', 'getStrangerStatus'],
fetchAiVoiceList: ['extras', 'fetchAiVoiceList'],
fetchAiVoice: ['extras', 'fetchAiVoice'],
sendAiVoice: ['extras', 'sendAiVoice'],
};
function fakeBridge(overrides: Record<string, any> = {}): 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);
});
});
+1 -1
View File
@@ -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;
}