From 805665a6d6b8e7a54acf100755faae8287a4f372 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 26 Sep 2026 16:06:52 +0800 Subject: [PATCH] feat(onebot): support choosing animated system faces Send available face animations by default and accept large=false for the small representation. Preserve the sticker business type when encoding animated faces. Fixes #483 --- .../src/event-converter/element-codecs.ts | 12 ++++++- packages/onebot/tests/message-parser.test.ts | 12 +++++++ packages/protocol/src/element-builder.ts | 13 +++---- packages/protocol/src/element-manifest.ts | 4 +-- packages/protocol/src/events.ts | 2 ++ .../sys-faces/fetch-sys-faces.ts | 5 ++- .../protocol/tests/element-manifest.test.ts | 2 +- packages/protocol/tests/sys-faces.test.ts | 36 ++++++++++++++----- 8 files changed, 64 insertions(+), 22 deletions(-) diff --git a/packages/onebot/src/event-converter/element-codecs.ts b/packages/onebot/src/event-converter/element-codecs.ts index 7d8a954f..1814f16d 100644 --- a/packages/onebot/src/event-converter/element-codecs.ts +++ b/packages/onebot/src/event-converter/element-codecs.ts @@ -155,7 +155,17 @@ export const ELEMENT_CODECS = { async fromSegment(data) { const id = intOr(data.id, -1); if (id < 0) return null; - return { type: 'face', faceId: id }; + const element: MessageElementOf<'face'> = { type: 'face', faceId: id }; + if (data.large !== undefined) { + if (data.large === true || data.large === 'true' || data.large === 1 || data.large === '1') { + element.large = true; + } else if (data.large === false || data.large === 'false' || data.large === 0 || data.large === '0') { + element.large = false; + } else { + throw new MessageElementValidationError('INVALID_FIELD', 'face.large must be a boolean'); + } + } + return element; }, }, diff --git a/packages/onebot/tests/message-parser.test.ts b/packages/onebot/tests/message-parser.test.ts index 04086b88..75ec8dca 100644 --- a/packages/onebot/tests/message-parser.test.ts +++ b/packages/onebot/tests/message-parser.test.ts @@ -43,6 +43,18 @@ describe('parseMessage', () => { expect(result[0].faceId).toBe(123); }); + it.each([true, false, 'true', 'false', 1, 0, '1', '0'])('preserves face animation choice %s', async (large) => { + const result = await parseMessage([{ type: 'face', data: { id: '424', large } }], false); + expect(result).toEqual([{ type: 'face', faceId: 424, large: [true, 'true', 1, '1'].includes(large) }]); + }); + + it('accepts the CQ animation switch and rejects invalid booleans', async () => { + expect(await parseMessage('[CQ:face,id=424,large=false]', false)) + .toEqual([{ type: 'face', faceId: 424, large: false }]); + await expect(parseMessage('[CQ:face,id=424,large=maybe]', false)) + .rejects.toMatchObject({ code: 'INVALID_FIELD' }); + }); + it('parses at CQ code', async () => { const result = await parseMessage('[CQ:at,qq=12345]', false); expect(result).toHaveLength(1); diff --git a/packages/protocol/src/element-builder.ts b/packages/protocol/src/element-builder.ts index 690c1a16..11c4e88b 100644 --- a/packages/protocol/src/element-builder.ts +++ b/packages/protocol/src/element-builder.ts @@ -7,7 +7,7 @@ import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import { randomUUID } from 'crypto'; import { deflateSync } from 'zlib'; import type { BridgeContext } from './bridge-context'; -import { sysFaceStore } from './sys-face-store'; +import { sysFaceStore, type FaceWire } from './sys-face-store'; import type { MessageElement } from './events'; import { assertVideoSendPolicy, @@ -53,15 +53,16 @@ function makeTextElem(text: string): ProtoElem { // renders classic small faces; newer "super" / animated faces sent that way are // silently remapped by the server (e.g. 424→168, issue #168). The split is // data-driven off the system-face catalog (0x9154_1, see sys-face-store): -// super (animated, aniSticker not pack (1,1)) → CommonElem 37 + QFaceExtra +// animated (unless large=false) → CommonElem 37 + QFaceExtra // other id ≥ 260 → CommonElem 33 + QSmallFaceExtra // classic id < 260 → legacy FaceElem -async function makeFaceElem(faceId: number, ctx?: SendContext): Promise { +async function makeFaceElem(faceId: number, large: boolean, ctx?: SendContext): Promise { // With a live bridge, wait for the authoritative catalog. Login normally // preloads it, while this await closes the reconnect / first-send race. - const wire = ctx + const resolved = ctx ? await sysFaceStore.resolveWire(ctx.bridge, faceId) : sysFaceStore.classify(faceId); + const wire: FaceWire = large ? resolved : { kind: faceId < 260 ? 'classic' : 'small' }; if (wire.kind === 'super') { return { commonElem: { @@ -74,7 +75,7 @@ async function makeFaceElem(faceId: number, ctx?: SendContext): Promise()(['faceId']), + fields: fieldsFor<'face'>()(['faceId', 'large']), requiredFields: ['faceId'], }, reply: { @@ -258,7 +258,7 @@ const NUMBER_FIELDS: ReadonlySet = new Set([ 'width', 'height', 'emojiPackageId', 'sceneType', 'forwardTSum', 'picFormat', 'videoFormat', 'voiceFormat', ]); -const BOOLEAN_FIELDS: ReadonlySet = new Set(['flash', 'noByteFallback']); +const BOOLEAN_FIELDS: ReadonlySet = new Set(['flash', 'noByteFallback', 'large']); function throwValidation( code: MessageElementValidationCode, diff --git a/packages/protocol/src/events.ts b/packages/protocol/src/events.ts index 50bec6bc..57f1343d 100644 --- a/packages/protocol/src/events.ts +++ b/packages/protocol/src/events.ts @@ -39,6 +39,8 @@ export interface AtElement { export interface FaceElement { type: 'face'; faceId: number; + /** Send the catalog animation when available (default true). */ + large?: boolean; } export interface ReplyElement { diff --git a/packages/protocol/src/oidb-services/sys-faces/fetch-sys-faces.ts b/packages/protocol/src/oidb-services/sys-faces/fetch-sys-faces.ts index f15b56e2..5bd2b4ff 100644 --- a/packages/protocol/src/oidb-services/sys-faces/fetch-sys-faces.ts +++ b/packages/protocol/src/oidb-services/sys-faces/fetch-sys-faces.ts @@ -172,10 +172,9 @@ export function findFaceEntity(packs: SysFacePackEntry[], faceId: number): SysFa return null; } -/** True when the catalog metadata selects the existing super-face send path. */ +/** True when the catalog declares an animated sticker representation. */ export function isSuperFaceEntry(emoji: SysFaceEntry): boolean { - if (emoji.aniStickerType == null || emoji.aniStickerPackId == null) return false; - return !(emoji.aniStickerType === 1 && emoji.aniStickerPackId === 1); + return emoji.aniStickerType != null && emoji.aniStickerType > 0; } export function isSuperFaceId(packs: SysFacePackEntry[], faceId: number): boolean { diff --git a/packages/protocol/tests/element-manifest.test.ts b/packages/protocol/tests/element-manifest.test.ts index c46d5cd1..878030c5 100644 --- a/packages/protocol/tests/element-manifest.test.ts +++ b/packages/protocol/tests/element-manifest.test.ts @@ -59,7 +59,7 @@ describe('element-manifest 对账(protocol 侧:D 收·解 / W 发·打包) if (field === 'mediaNode') return {}; if (field === 'emojiId' || field === 'md5Hex') return 'ab'.repeat(16); if (field === 'sha1Hex') return 'cd'.repeat(20); - if (field === 'flash' || field === 'noByteFallback') return true; + if (field === 'flash' || field === 'noByteFallback' || field === 'large') return true; if (field === 'botAppid') return '1'; if (field === 'targetUin' || field === 'faceId' || field === 'fileSize' || field.startsWith('reply') || field === 'subType' || field === 'duration' diff --git a/packages/protocol/tests/sys-faces.test.ts b/packages/protocol/tests/sys-faces.test.ts index d948373d..3159a420 100644 --- a/packages/protocol/tests/sys-faces.test.ts +++ b/packages/protocol/tests/sys-faces.test.ts @@ -38,12 +38,12 @@ function entry( }; } -// 392 is a real non-(1,1) super face; 424 is a real (1,1) "new small" face. +// Animated faces include ordinary type-1 stickers as well as special types. const CATALOG: SysFacePackEntry[] = [ { packName: '经典', emojis: [entry('14', null, null, null, '/微笑', ['smile'])] }, { packName: '超级表情', emojis: [ entry('392', 3, 2, 38, '/龙年快乐'), // super → CommonElem 37 - entry('424', 1, 1, 52), // (1,1) → small → CommonElem 33 + entry('424', 1, 1, 52), entry('358', 2, 1, 33), // super (packId 1 but type 2) → CommonElem 37 ] }, ]; @@ -62,18 +62,19 @@ class MemoryCatalogStorage implements SysFaceCatalogStorage { } } -describe('isSuperFaceEntry / isSuperFaceId — the (1,1) rule', () => { - it('treats only non-(1,1) aniSticker faces as super', () => { +describe('isSuperFaceEntry / isSuperFaceId', () => { + it('recognizes every animated sticker type', () => { expect(isSuperFaceEntry(entry('x', 3, 2, 1))).toBe(true); expect(isSuperFaceEntry(entry('x', 2, 1, 1))).toBe(true); // type≠1 expect(isSuperFaceEntry(entry('x', 1, 2, 1))).toBe(true); // pack≠1 - expect(isSuperFaceEntry(entry('x', 1, 1, 52))).toBe(false); // the (1,1) pack + expect(isSuperFaceEntry(entry('x', 1, 1, 52))).toBe(true); + expect(isSuperFaceEntry(entry('x', 0, 0, 0))).toBe(false); expect(isSuperFaceEntry(entry('x', null, null, null))).toBe(false); // not aniSticker }); it('isSuperFaceId walks the packs by id', () => { expect(isSuperFaceId(CATALOG, 392)).toBe(true); - expect(isSuperFaceId(CATALOG, 424)).toBe(false); + expect(isSuperFaceId(CATALOG, 424)).toBe(true); expect(isSuperFaceId(CATALOG, 14)).toBe(false); expect(isSuperFaceId(CATALOG, 99999)).toBe(false); // unknown }); @@ -94,8 +95,10 @@ describe('faceWireFor — classification', () => { expect(() => faceWireFor(entry('392', 3, 2, null), 392)) .toThrow(/incomplete super-face metadata/); }); - it('(1,1) face → small/classic by id range, not super', () => { - expect(faceWireFor(entry('424', 1, 1, 52), 424)).toEqual({ kind: 'small' }); + it('uses the animation metadata for type-1 stickers too', () => { + expect(faceWireFor(entry('424', 1, 1, 52), 424)).toEqual({ + kind: 'super', packId: '1', stickerId: '52', stickerType: 1, + }); }); it('rejects an unknown id instead of guessing a wire shape', () => { expect(() => faceWireFor(null, 14)).toThrow(/absent from the current catalog/); @@ -499,8 +502,13 @@ describe('makeFaceElem (via buildSendElems) — three-way wire encoding', () => expect(big.qsid).toBe(392); expect(big.packId).toBe('2'); expect(big.stickerId).toBe('38'); + expect(superElem.commonElem?.businessType).toBe(3); - const [smallElem] = await buildSendElems([{ type: 'face', faceId: 424 }], ctx); + const [animated] = await buildSendElems([{ type: 'face', faceId: 424 }], ctx); + expect(animated.commonElem?.serviceType).toBe(37); + expect(protobuf_decode(animated.commonElem!.pbElem!).stickerId).toBe('52'); + + const [smallElem] = await buildSendElems([{ type: 'face', faceId: 424, large: false }], ctx); expect(smallElem.commonElem?.serviceType).toBe(33); const small = protobuf_decode(smallElem.commonElem!.pbElem!); expect(small.faceId).toBe(424); @@ -509,6 +517,16 @@ describe('makeFaceElem (via buildSendElems) — three-way wire encoding', () => expect(classicElem.face?.index).toBe(14); }); + it('allows special animated faces to be sent small and normalizes newer business types', async () => { + sysFaceStore.load([{ packName: 'special', emojis: [entry('504', 4, 1, 90)] }]); + const [large] = await buildSendElems([{ type: 'face', faceId: 504, large: true }]); + expect(large.commonElem?.businessType).toBe(1); + expect(protobuf_decode(large.commonElem!.pbElem!).stickerType).toBe(4); + const [small] = await buildSendElems([{ type: 'face', faceId: 504, large: false }]); + expect(small.commonElem?.serviceType).toBe(33); + expect(protobuf_decode(small.commonElem!.pbElem!).faceId).toBe(504); + }); + it('rejects an unknown id even when building without a live send context', async () => { sysFaceStore.load(CATALOG);