feat(onebot): unify market faces to image segments + add send support

Market faces (商城表情) now decode to an OneBot `image` segment carrying
NapCat-style markers (emoji_id/emoji_package_id/key/summary) plus a gxh
gif URL, so clients that don't special-case `mface` still render the
sticker while aware clients can reproduce it.

Adds the previously-missing send path: a dedicated `mface` segment OR an
`image` segment carrying `emoji_id` rebuilds the wire `marketFace`
(faceId=hex(emoji_id), itemType 6 / subType 3 / 300×300 /
pbReserve.field8=1) — byte-matched against NapCat and Lagrange.Core.

- proto-defs: MarketFace.key bytes→string; +MarketFacePbReserve{field8@8}
- protocol: decode faceId→emojiId(hex)/tabId/key; makeMarketFaceElem +
  buildSendElems 'mface' case; format preview keeps the sticker name
- onebot: to-segment mface→image; message-parser mface + image(emoji_id)
- tests: wire decode + full proton round-trip; parse→build; segment shape

Reply/forward round-trips preserved (element keeps type 'mface' + text).
This commit is contained in:
DrSmoothl committed 2026-06-14 20:47:50 +08:00
1 parent 58c02d415f
commit 8dc4ff48f8
10 files changed
+247 -16

No files matched your search

+34 -2
View File
@@ -2,7 +2,7 @@ import type {
MarkdownData,
MentionExtraSend,
} from '@snowluma/proto-defs/action';
import type { Elem, GroupFileExtra } from '@snowluma/proto-defs/element';
import type { Elem, GroupFileExtra, MarketFacePbReserve } from '@snowluma/proto-defs/element';
import { protobuf_encode } from '@snowluma/proton';
import { randomUUID } from 'crypto';
import { deflateSync } from 'zlib';
@@ -147,6 +147,34 @@ function makeMarkdownElem(element: MessageElement): ProtoElem {
}
/**
* Build a market-face (商城表情) wire element from the markers carried on the
* receive-side `image` segment (`emoji_id`/`emoji_package_id`/`key`/summary).
*
* The constants (`itemType=6`, `faceInfo=1`, `subType=3`, 300×300,
* `pbReserve.field8=1`) mirror NapCat's `PacketMsgMarkFaceElement.buildElement`
* (`dev/NapCatQQ/.../packet/message/element.ts:318-335`) — the QQ-NT server
* pairs them with the faceId/tabId/key to relay the sticker. `faceId` is the
* raw GUID bytes, recovered from the hex `emojiId`.
*/
function makeMarketFaceElem(element: MessageElement): ProtoElem {
const emojiId = (element.emojiId ?? '').trim();
return {
marketFace: {
faceName: element.text ?? '',
itemType: 6,
faceInfo: 1,
faceId: hexToBytes(emojiId),
tabId: element.emojiPackageId ?? 0,
subType: 3,
key: element.emojiKey ?? '',
imageWidth: 300,
imageHeight: 300,
pbReserve: protobuf_encode<MarketFacePbReserve>({ field8: 1 }),
},
};
}
function makeForwardElem(element: MessageElement): ProtoElem {
const resId = (element.resId ?? '').trim();
if (!resId) {
@@ -345,7 +373,7 @@ async function makeVideoElem(ctx: SendContext, element: MessageElement): Promise
/**
* Build proto Elem objects from an array of MessageElements.
* Supports: text, face, at, reply, json, xml, markdown, image, record, video, forward.
* Supports: text, face, mface, at, reply, json, xml, markdown, image, record, video, forward.
* Image, record and video elements trigger NTV2 highway upload via the SendContext.
*/
export async function buildSendElems(elements: MessageElement[], ctx?: SendContext): Promise<ProtoElem[]> {
@@ -361,6 +389,10 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte
if (elem.faceId !== undefined) result.push(makeFaceElem(elem.faceId));
break;
case 'mface':
if (elem.emojiId) result.push(makeMarketFaceElem(elem));
break;
case 'at':
result.push(makeMentionElem(elem, ctx));
break;
+9
View File
@@ -21,6 +21,15 @@ export interface MessageElement {
width?: number;
height?: number;
summary?: string;
// Market face (商城表情). Decoded from the wire `marketFace` element; the
// OneBot layer surfaces it as an `image` segment carrying these as markers,
// and the send path rebuilds the wire `marketFace` from them.
// emojiId = hex(MarketFace.faceId) → also builds the gxh gif URL
// emojiPackageId = MarketFace.tabId
// emojiKey = MarketFace.key
emojiId?: string;
emojiPackageId?: number;
emojiKey?: string;
flash?: boolean;
resId?: string;
fileHash?: string;
+7 -1
View File
@@ -89,7 +89,13 @@ export function formatMessageSegments(message: JsonValue): string {
function renderSegment(type: string, data: Record<string, unknown>): string {
switch (type) {
case 'text': return truncate(String(data.text ?? ''), MAX_TEXT_PREVIEW);
case 'image': return '[图片]';
// Market faces (商城表情) are surfaced as `image` segments carrying an
// `emoji_id` marker (+ `summary` = face name). Keep the sticker readable in
// previews instead of collapsing it to a generic "[图片]".
case 'image':
return data.emoji_id
? (data.summary ? `[${String(data.summary)}]` : '[表情]')
: '[图片]';
case 'face': return '[表情]';
case 'mface': return data.text ? `[${String(data.text)}]` : '[表情]';
case 'at': return data.qq === 'all' ? '@全体成员' : `@${data.qq ?? ''}`;
@@ -1,5 +1,5 @@
import { protobuf_decode } from '@snowluma/proton';
import { toHexUpper } from '@snowluma/common/hex';
import { toHex, toHexUpper } from '@snowluma/common/hex';
import type { MessageElement } from '../events';
import type {
Elem,
@@ -74,13 +74,19 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
result.push({ type: 'face', faceId: elem.face.index ?? 0 });
}
// MarketFace
// MarketFace (商城表情). Keep the wire identity (`emojiId`/`tabId`/`key`)
// on the element; the OneBot layer unifies it to an `image` segment with
// these as markers (NapCat-compatible), and the send path rebuilds the
// wire `marketFace` from them. `emojiId` is the lowercase hex of the
// `faceId` GUID bytes — it also forms the gxh gif URL on the segment side.
if (elem.marketFace) {
const mf = elem.marketFace;
result.push({
type: 'mface',
text: elem.marketFace.faceName ?? '',
faceId: elem.marketFace.tabId ?? 0,
subType: elem.marketFace.subType ?? 0,
text: mf.faceName ?? '',
emojiId: mf.faceId && mf.faceId.length > 0 ? toHex(mf.faceId) : '',
emojiPackageId: mf.tabId ?? 0,
emojiKey: mf.key ?? '',
});
}