diff --git a/packages/onebot/src/actions/system-face.ts b/packages/onebot/src/actions/system-face.ts index 67e7d900..4444e5cf 100644 --- a/packages/onebot/src/actions/system-face.ts +++ b/packages/onebot/src/actions/system-face.ts @@ -27,7 +27,7 @@ const nullableString = { type: ['string', 'null'] }; const faceSchema = { type: 'object', properties: { - q_sid: { type: 'string', description: '表情编号' }, + q_sid: { type: 'string', description: 'QQ 表情目录标识(部分 emoji 为 Unicode 表情字符串)' }, q_des: { type: 'string', description: '表情描述' }, em_code: { type: 'string' }, q_cid: nullableInteger, diff --git a/packages/onebot/tests/system-face-actions.test.ts b/packages/onebot/tests/system-face-actions.test.ts index 69191540..b63cc30f 100644 --- a/packages/onebot/tests/system-face-actions.test.ts +++ b/packages/onebot/tests/system-face-actions.test.ts @@ -31,6 +31,32 @@ describe('system face actions', () => { }); }); + it('preserves Unicode catalog identifiers in the public catalog response', async () => { + const unicodeFace = { + ...FACE, + qSid: '😊', + qDes: '/嘿嘿', + qCid: 128522, + emCode: '400832', + aniStickerType: null, + aniStickerPackId: null, + aniStickerId: null, + }; + const fetchCatalog = vi.fn(async () => [{ packName: 'emoji', emojis: [unicodeFace] }]); + + const response = await handler({ fetchCatalog }).handle('fetch_sys_faces', {}); + + expect(response).toMatchObject({ + status: 'ok', + data: { + packs: [{ + pack_name: 'emoji', + emojis: [{ q_sid: '😊', q_des: '/嘿嘿', q_cid: 128522, em_code: '400832' }], + }], + }, + }); + }); + it('looks up one id without redownloading a fresh catalog', async () => { const fetchFace = vi.fn(async () => FACE); const response = await handler({ fetchFace }).handle('fetch_face_entity', { face_id: '392' }); diff --git a/packages/proto-defs/src/oidb-actions/sys-faces.ts b/packages/proto-defs/src/oidb-actions/sys-faces.ts index b291c5f3..96a94aea 100644 --- a/packages/proto-defs/src/oidb-actions/sys-faces.ts +++ b/packages/proto-defs/src/oidb-actions/sys-faces.ts @@ -1,10 +1,4 @@ -// 0x9154_1 — system face / emoji catalog (sysface) protobuf shapes. -// -// Mirrors Lagrange.Core's FetchFullSysFacesService response: one round-trip -// returns every pack — common faces, "big"/super animated faces, and the -// magic-face pack. Each emoji carries its `aniSticker*` metadata, which the -// send path uses to pick the right wire encoding for a face id (classic -// FaceElem vs CommonElem 37/33). See protocol/oidb-services/sys-faces. +// 0x9154_1 — QQ system face / emoji catalog protobuf shapes. import type { pb, pb_repeated, int_32 } from '@snowluma/proton'; @@ -47,10 +41,15 @@ export interface OidbFaceMagicContent { resourceUrl?: pb<2, OidbFaceResourceUrl>; } +export interface OidbFetchSysFacesExpInfo { + field1?: pb<1, string>; +} + export interface OidbFetchSysFacesReq { field1?: pb<1, int_32>; field2?: pb<2, int_32>; - field3?: pb<3, string>; + field3?: pb<3, int_32>; + field4?: pb<4, OidbFetchSysFacesExpInfo>; } export interface OidbFetchSysFacesResp { 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 bedc2e32..f15b56e2 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 @@ -1,10 +1,8 @@ // 0x9154_1 — fetch the bot's system face / emoji catalog. // -// Mirrors Lagrange.Core's `FetchFullSysFacesService`. One server round-trip -// returns the entire pack list — common faces, "big"/super faces (the giant -// animated emoji), and the magic-face pack. The send path uses each emoji's -// `aniSticker*` metadata to pick the right wire encoding for a face id (see -// element-builder's makeFaceElem + sys-face-store). +// QQ returns the common, special-big, and magic-face panels in one response. +// The send path uses the catalog metadata to select the corresponding face +// element encoding (see element-builder and sys-face-store). import { createLogger } from '@snowluma/common/logger'; import { @@ -22,8 +20,9 @@ import { invokeOidb, type OidbSender } from '../../oidb-service'; const log = createLogger('SysFace'); -/** A single system face / emoji. Field names match Lagrange's `SysFaceEntry`; - * `qSid` is the numeric face ID as a string. */ +/** A single system face / emoji. `qSid` is QQ's opaque catalog identifier: + * numeric system faces use decimal strings, while emoji groups may use a + * Unicode emoji string. */ export interface SysFaceEntry { qSid: string; qDes: string; @@ -118,7 +117,7 @@ export namespace FetchSysFaces { export const serialize = (_ctx: Deps, _p: Params): OidbFetchSysFacesReq => ({ field1: 0, field2: 7, - field3: '0', + field3: 0, }); export const deserialize = (_ctx: Deps, body: OidbFetchSysFacesResp): SysFacePackEntry[] => { @@ -138,8 +137,8 @@ export namespace FetchSysFaces { } } - // Magic faces arrive as a single un-named bundle — Lagrange tags it - // "MagicFace" client-side, so we match for parity. + // QQ returns magic faces as a single unnamed bundle. Keep a stable local + // name so callers can identify the group across refreshes. const magicEmojis = body.specialMagicFace?.field1?.emojiList ?? []; if (magicEmojis.length > 0) { const packIndex = packs.length; @@ -173,10 +172,7 @@ export function findFaceEntity(packs: SysFacePackEntry[], faceId: number): SysFa return null; } -/** True when `faceId` is an animated "super face" — has both aniStickerType - * and aniStickerPackId set, and is NOT the (1,1) pack (the regular static - * emojis). Matches Lagrange's `GetUniqueSuperQSids` filter. Super faces ride - * CommonElem serviceType 37 (QFaceExtra); the (1,1) ones ride serviceType 33. */ +/** True when the catalog metadata selects the existing super-face send path. */ export function isSuperFaceEntry(emoji: SysFaceEntry): boolean { if (emoji.aniStickerType == null || emoji.aniStickerPackId == null) return false; return !(emoji.aniStickerType === 1 && emoji.aniStickerPackId === 1); diff --git a/packages/protocol/src/sys-face-store.ts b/packages/protocol/src/sys-face-store.ts index 1d49faf3..a0418fed 100644 --- a/packages/protocol/src/sys-face-store.ts +++ b/packages/protocol/src/sys-face-store.ts @@ -295,8 +295,7 @@ function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] { throw new Error(`invalid system face pack at index ${packIndex}`); } for (const [faceIndex, face] of pack.emojis.entries()) { - if (!face || typeof face.qSid !== 'string' || !/^(?:0|[1-9]\d*)$/.test(face.qSid) - || !Number.isSafeInteger(Number(face.qSid)) || Number(face.qSid) < 0) { + if (!face || typeof face.qSid !== 'string' || face.qSid.length === 0) { throw new Error( `invalid system face id at pack ${packIndex}, face ${faceIndex}: ` + `value=${JSON.stringify(face?.qSid)}`, diff --git a/packages/protocol/tests/sys-faces.test.ts b/packages/protocol/tests/sys-faces.test.ts index a44946e6..7d3da09a 100644 --- a/packages/protocol/tests/sys-faces.test.ts +++ b/packages/protocol/tests/sys-faces.test.ts @@ -2,7 +2,10 @@ import { describe, it, expect } from 'vitest'; import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import type { QFaceExtra, QSmallFaceExtra } from '@snowluma/proto-defs/element'; import type { OidbBase } from '@snowluma/proto-defs/oidb'; -import type { OidbFetchSysFacesResp } from '@snowluma/proto-defs/oidb-actions/sys-faces'; +import type { + OidbFetchSysFacesReq, + OidbFetchSysFacesResp, +} from '@snowluma/proto-defs/oidb-actions/sys-faces'; import { FetchSysFaces, findFaceEntity, @@ -100,6 +103,27 @@ describe('faceWireFor — classification', () => { }); }); +describe('FetchSysFaces protocol contract', () => { + it('encodes the native 0x9154_1 request layout', () => { + const body = FetchSysFaces.serialize({} as never, {}); + expect(body).toEqual({ + field1: 0, + field2: 7, + field3: 0, + }); + + const decoded = protobuf_decode>( + FetchSysFaces.encode({ body }), + ); + expect(decoded.body).toEqual({ + field1: null, + field2: 7, + field3: null, + field4: null, + }); + }); +}); + describe('FetchSysFaces.deserialize', () => { it('flattens common + big + magic packs', () => { const packs = FetchSysFaces.deserialize({} as never, { @@ -111,6 +135,27 @@ describe('FetchSysFaces.deserialize', () => { expect(packs[1].emojis[0].aniStickerType).toBe(3); }); + it('preserves Unicode catalog identifiers returned by QQ', () => { + const packs = FetchSysFaces.deserialize({} as never, { + commonFace: { + emojiList: [{ + emojiPackName: 'emoji', + emojiDetail: [{ qSid: '😊', qDes: '/嘿嘿', qCid: 128522, emCode: '400832' }], + }], + }, + } as never); + + expect(packs).toEqual([{ + packName: 'emoji', + emojis: [expect.objectContaining({ + qSid: '😊', + qDes: '/嘿嘿', + qCid: 128522, + emCode: '400832', + })], + }]); + }); + it('fails closed when an id field uses an unsupported wire encoding', () => { const driftedFace = { qDes: '/未知表情' }; Object.defineProperty(driftedFace, Symbol.for('snowluma.proton.unknownFields'), { @@ -167,6 +212,37 @@ describe('SysFaceStore — persistent catalog and query seam', () => { expect(store.search('经典').map((face) => face.qSid)).toEqual(['14']); }); + it('persists and searches Unicode catalog ids without exposing them as numeric face ids', async () => { + const unicodeFace = entry('😊', null, null, null, '/嘿嘿'); + unicodeFace.qCid = 128522; + unicodeFace.emCode = '400832'; + const catalog: SysFacePackEntry[] = [{ packName: 'emoji', emojis: [unicodeFace] }]; + const storage = new MemoryCatalogStorage(); + const store = new SysFaceStore({ + storage, + now: () => 456, + fetchCatalog: async () => catalog, + }); + + await expect(store.refresh(sender)).resolves.toEqual(catalog); + + expect(storage.saved[0]?.packs).toEqual(catalog); + expect(store.search('😊').map((face) => face.qSid)).toEqual(['😊']); + expect(store.search('嘿嘿').map((face) => face.qSid)).toEqual(['😊']); + expect(store.lookup(128522)).toBeNull(); + }); + + it('still rejects empty and duplicate catalog identifiers', () => { + const store = new SysFaceStore(); + + expect(() => store.load([{ packName: 'emoji', emojis: [entry('', null, null, null)] }])) + .toThrow(/invalid system face id/); + expect(() => store.load([{ + packName: 'emoji', + emojis: [entry('😊', null, null, null), entry('😊', null, null, null)], + }])).toThrow(/duplicate system face id/); + }); + it('coalesces concurrent refreshes and persists the authoritative result once', async () => { const storage = new MemoryCatalogStorage(); let fetches = 0;