// Process-wide catalog of QQ system faces. QQ publishes the mapping through // 0x9154_1; this store makes that catalog durable, queryable, and safe to use // from the asynchronous send path. import { createLogger } from '@snowluma/common/logger'; import { FetchSysFaces, isSuperFaceEntry, type SysFaceEntry, type SysFacePackEntry, } from './oidb-services/sys-faces/fetch-sys-faces'; import type { OidbSender } from './oidb-service'; export type { SysFaceEntry, SysFacePackEntry } from './oidb-services/sys-faces/fetch-sys-faces'; const log = createLogger('SysFace'); /** Wire encoding a face id maps to. `classic` -> legacy FaceElem; `small` -> * CommonElem serviceType 33; `super` -> CommonElem serviceType 37. */ export type FaceWire = | { kind: 'classic' } | { kind: 'small' } | { kind: 'super'; packId: string; stickerId: string; stickerType: number }; export interface SysFaceCatalogSnapshot { schemaVersion: 1; fetchedAt: number; packs: SysFacePackEntry[]; } export interface SysFaceCatalogStorage { load(): Promise; save(snapshot: SysFaceCatalogSnapshot): Promise; } export interface SysFaceStoreOptions { storage?: SysFaceCatalogStorage; fetchCatalog?: (sender: OidbSender) => Promise; now?: () => number; } interface IndexedCatalog { packs: SysFacePackEntry[]; byId: Map; entryCount: number; overlapCount: number; } /** Pure classification once an authoritative entry is known. */ export function faceWireFor(entry: SysFaceEntry | null | undefined, faceId: number): FaceWire { if (!entry) { throw new Error(`QQ system face id ${faceId} is absent from the current catalog`); } if (isSuperFaceEntry(entry)) { if (entry.aniStickerPackId == null || entry.aniStickerId == null || entry.aniStickerType == null) { throw new Error(`QQ system face id ${faceId} has incomplete super-face metadata`); } return { kind: 'super', packId: String(entry.aniStickerPackId), stickerId: String(entry.aniStickerId), stickerType: entry.aniStickerType, }; } return faceId < 260 ? { kind: 'classic' } : { kind: 'small' }; } export class SysFaceStore { private catalog: IndexedCatalog | null = null; private storage: SysFaceCatalogStorage | null; private readonly fetchCatalog: (sender: OidbSender) => Promise; private readonly now: () => number; private cacheLoadCompleted = false; private cacheLoadInflight: Promise | null = null; private refreshInflight: Promise | null = null; private refreshedInProcess = false; private prewarmInflight: Promise | null = null; private prewarmed = false; constructor(options: SysFaceStoreOptions = {}) { this.storage = options.storage ?? null; this.fetchCatalog = options.fetchCatalog ?? ((sender) => FetchSysFaces.invoke(sender)); this.now = options.now ?? Date.now; } /** Configure persistence before the shared store has been used. */ configureStorage(storage: SysFaceCatalogStorage): void { if (this.cacheLoadCompleted || this.cacheLoadInflight || this.catalog || this.refreshInflight) { throw new Error('system face catalog storage must be configured before first use'); } this.storage = storage; } /** Index a trusted catalog. Kept public for protocol consumers that already * obtained a full catalog and want to reuse the same query/send semantics. */ load(packs: SysFacePackEntry[]): void { this.catalog = indexCatalog(validateCatalog(packs)); } lookup(faceId: number): SysFaceEntry | null { return this.catalog?.byId.get(String(faceId)) ?? null; } /** Case-insensitive query over id, description, aliases, and pack name. */ search(query: string): SysFaceEntry[] { const needle = query.trim().toLocaleLowerCase(); if (!needle || !this.catalog) return []; const matches: SysFaceEntry[] = []; const matchedIds = new Set(); for (const pack of this.catalog.packs) { for (const face of pack.emojis) { if (matchedIds.has(face.qSid)) continue; const haystacks = [ face.qSid, face.qDes, face.emCode, pack.packName, ...face.emojiNameAlias, ]; if (haystacks.some((value) => value.toLocaleLowerCase().includes(needle))) { const canonical = this.catalog.byId.get(face.qSid); if (!canonical) { throw new Error(`system face index is missing catalog id ${face.qSid}`); } matches.push(canonical); matchedIds.add(face.qSid); } } } return matches; } /** Synchronous classification for callers that already established catalog * readiness. Unknown ids fail instead of guessing a QQ wire shape. */ classify(faceId: number): FaceWire { return faceWireFor(this.lookup(faceId), faceId); } /** Load durable state, fetching only when no usable catalog exists. */ async ensureReady(sender: OidbSender): Promise { if (this.refreshInflight) { await this.refreshInflight; return; } if (this.catalog) return; const loaded = await this.loadCache(); if (!loaded) await this.refresh(sender); } /** Login hook: restore the durable snapshot for immediate reads, then refresh * from QQ exactly once per process. Multiple accounts share the same job. */ prewarm(sender: OidbSender): Promise { if (this.prewarmed) return Promise.resolve(); if (this.prewarmInflight) return this.prewarmInflight; this.prewarmInflight = (async () => { try { await this.loadCache(); } catch { // loadCache already emitted the path and failure. A fresh server fetch // is the authoritative repair for a corrupt rebuildable snapshot. } await this.refresh(sender); this.prewarmed = true; })().finally(() => { this.prewarmInflight = null; }); return this.prewarmInflight; } /** Fetch the current authoritative catalog. Concurrent callers share one * request and one atomic persistence operation. */ refresh(sender: OidbSender): Promise { if (this.refreshInflight) return this.refreshInflight; const startedAt = this.now(); this.refreshInflight = this.fetchCatalog(sender) .then(async (packs) => { const validated = validateCatalog(packs); const snapshot: SysFaceCatalogSnapshot = { schemaVersion: 1, fetchedAt: this.now(), packs: validated, }; this.catalog = indexCatalog(validated); this.refreshedInProcess = true; if (this.storage) await this.storage.save(snapshot); log.info( 'system face catalog refreshed: entries=%d uniqueFaces=%d overlaps=%d packs=%d elapsedMs=%d', this.catalog.entryCount, this.catalog.byId.size, this.catalog.overlapCount, validated.length, this.now() - startedAt, ); return validated; }) .catch((error) => { log.error( 'system face catalog refresh failed: elapsedMs=%d error=%s', this.now() - startedAt, error instanceof Error ? error.message : String(error), ); throw error; }) .finally(() => { this.refreshInflight = null; }); return this.refreshInflight; } /** Resolve one id. A miss from a persisted snapshot causes exactly one * authoritative refresh; a miss from that fresh catalog remains null. */ async resolve(sender: OidbSender, faceId: number): Promise { await this.ensureReady(sender); const cached = this.lookup(faceId); if (cached || this.refreshedInProcess) return cached; await this.refresh(sender); return this.lookup(faceId); } async resolveWire(sender: OidbSender, faceId: number): Promise { const entry = await this.resolve(sender, faceId); if (!entry) { throw new Error(`QQ system face id ${faceId} is absent from the current catalog`); } return faceWireFor(entry, faceId); } async getCatalog(sender: OidbSender, refresh = false): Promise { if (refresh) await this.refresh(sender); else await this.ensureReady(sender); if (!this.catalog) { throw new Error('system face catalog is unavailable after readiness completed'); } return this.catalog.packs; } private loadCache(): Promise { if (this.catalog) return Promise.resolve(true); if (this.cacheLoadCompleted) return Promise.resolve(false); if (this.cacheLoadInflight) return this.cacheLoadInflight; if (!this.storage) { this.cacheLoadCompleted = true; return Promise.resolve(false); } const startedAt = this.now(); this.cacheLoadInflight = this.storage.load() .then((snapshot) => { this.cacheLoadCompleted = true; if (!snapshot) return false; validateSnapshot(snapshot); this.catalog = indexCatalog(snapshot.packs); log.info( 'system face catalog restored: entries=%d uniqueFaces=%d overlaps=%d packs=%d ' + 'fetchedAt=%d elapsedMs=%d', this.catalog.entryCount, this.catalog.byId.size, this.catalog.overlapCount, snapshot.packs.length, snapshot.fetchedAt, this.now() - startedAt, ); return true; }) .catch((error) => { this.cacheLoadCompleted = true; log.error( 'system face catalog restore failed: elapsedMs=%d error=%s', this.now() - startedAt, error instanceof Error ? error.message : String(error), ); throw error; }) .finally(() => { this.cacheLoadInflight = null; }); return this.cacheLoadInflight; } } function validateSnapshot(snapshot: SysFaceCatalogSnapshot): void { if (typeof snapshot !== 'object' || snapshot === null) { throw new Error('invalid system face catalog snapshot root'); } if (snapshot.schemaVersion !== 1) { throw new Error(`unsupported system face catalog schema version: ${String(snapshot.schemaVersion)}`); } if (!Number.isSafeInteger(snapshot.fetchedAt) || snapshot.fetchedAt < 0) { throw new Error(`invalid system face catalog fetchedAt: ${String(snapshot.fetchedAt)}`); } validateCatalog(snapshot.packs); } function validateCatalog(packs: SysFacePackEntry[]): SysFacePackEntry[] { if (!Array.isArray(packs) || packs.length === 0) { throw new Error('system face catalog is empty'); } const seen = new Map; }>(); let faceCount = 0; for (const [packIndex, pack] of packs.entries()) { if (!pack || typeof pack.packName !== 'string' || !Array.isArray(pack.emojis)) { throw new Error(`invalid system face pack at index ${packIndex}`); } for (const [faceIndex, face] of pack.emojis.entries()) { 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)}`, ); } if (!Array.isArray(face.emojiNameAlias) || face.emojiNameAlias.some((alias) => typeof alias !== 'string')) { throw new Error(`invalid aliases for system face id ${face.qSid}`); } if (typeof face.qDes !== 'string' || typeof face.emCode !== 'string') { throw new Error(`invalid text metadata for system face id ${face.qSid}`); } for (const [name, value] of [ ['qCid', face.qCid], ['aniStickerType', face.aniStickerType], ['aniStickerPackId', face.aniStickerPackId], ['aniStickerId', face.aniStickerId], ['aniStickerWidth', face.aniStickerWidth], ['aniStickerHeight', face.aniStickerHeight], ] as const) { if (value !== null && !Number.isSafeInteger(value)) { throw new Error(`invalid ${name} for system face id ${face.qSid}`); } } if (face.url !== null && typeof face.url !== 'string') { throw new Error(`invalid url for system face id ${face.qSid}`); } const previous = seen.get(face.qSid); if (previous) { const previousFaceIndex = previous.faceIndexByPack.get(packIndex); if (previousFaceIndex !== undefined) { throw new Error( `duplicate system face id ${face.qSid} within pack ${packIndex}: ` + `faces ${previousFaceIndex} and ${faceIndex}`, ); } if (!hasCompatibleSendMetadata(previous.face, face)) { throw new Error( `conflicting system face send metadata for id ${face.qSid}: ` + `pack ${previous.packIndex}, face ${previous.faceIndex} vs ` + `pack ${packIndex}, face ${faceIndex}`, ); } previous.faceIndexByPack.set(packIndex, faceIndex); } else { seen.set(face.qSid, { face, packIndex, faceIndex, faceIndexByPack: new Map([[packIndex, faceIndex]]), }); } faceCount += 1; } } if (faceCount === 0) throw new Error('system face catalog contains no faces'); return packs; } function hasCompatibleSendMetadata(left: SysFaceEntry, right: SysFaceEntry): boolean { if (!isAddressableFaceId(left.qSid)) return true; const leftIsSuper = isSuperFaceEntry(left); const rightIsSuper = isSuperFaceEntry(right); if (leftIsSuper !== rightIsSuper) return false; if (!leftIsSuper) return true; return left.aniStickerType === right.aniStickerType && left.aniStickerPackId === right.aniStickerPackId && left.aniStickerId === right.aniStickerId; } function isAddressableFaceId(id: string): boolean { if (!/^(?:0|[1-9]\d*)$/.test(id)) return false; const numeric = Number(id); return Number.isSafeInteger(numeric) && numeric >= 0; } function indexCatalog(packs: SysFacePackEntry[]): IndexedCatalog { const byId = new Map(); let entryCount = 0; for (const pack of packs) { for (const emoji of pack.emojis) { entryCount += 1; if (!byId.has(emoji.qSid)) byId.set(emoji.qSid, emoji); } } return { packs, byId, entryCount, overlapCount: entryCount - byId.size, }; } /** Shared, process-wide catalog (the face set is account-independent). */ export const sysFaceStore = new SysFaceStore();