feat(proton): full longmsg + element + message migration; forward.ts on new API

Completes the proton migration started in the previous PoC commit. All
schemas needed by actions/forward.ts are now mirrored as proton interface
forms, the legacy ProtoSchema constants stay in place for back-compat,
and forward.ts has been switched over to the new compile-time codecs.

Migrated schemas:
  - element.ts (35 schemas: TextElem, FaceElem, OnlineImage, NotOnlineImage,
    TransElem, MarketFace, CustomFace, RichMsg, GroupFileElem, ExtraInfo,
    VideoFile, SrcMsg, LightAppElem, CommonElem, GeneralFlags, Elem,
    MentionExtra, QFaceExtra, QSmallFaceExtra, FileType, FileInfo,
    IndexNode, PicUrlExtInfo, PictureInfo, PicExtData, PicExtBizInfo,
    VideoExtBizInfo, PttExtBizInfo, ExtBizInfo, C2cSource, TroopSource,
    HashSum, MsgInfoBody, MsgInfo, GroupFileInfo, GroupFileExtraInner,
    GroupFileExtra, Preserve, …)
  - message.ts (PushMsg, PushMsgBody, ResponseHead, ContentHead, Ptt,
    NotOnlineFile, RichText, MessageBody, FileExtra, …)
  - longmsg.ts — the three remaining schemas (LongMsgContent / LongMsgAction
    / LongMsgResult) that depend on PushMsgBody

forward.ts now calls:
  protobuf_encode<LongMsgResult>(...)   // upload payload
  protobuf_encode<SendLongMsgReq>(...)
  protobuf_decode<SendLongMsgResp>(...)
  protobuf_encode<RecvLongMsgReq>(...)
  protobuf_decode<RecvLongMsgResp>(...)
  protobuf_decode<LongMsgResult>(...)   // download payload
  protobuf_encode<PushMsg>(...)         // re-wrap for parseMsgPush

The unused legacy schema constants are intentionally left in place — they
still back the existing forward.test.ts mock encoder and any other consumer
that hasn't been migrated yet. They will be removed in a follow-up once
every consumer is on the new API.

Verification:
  - 18/18 parity tests pass (encode + decode equivalence for every
    representative shape across element / message / longmsg)
  - 585/585 core test suite passes (network-manager.test.ts fails to load
    due to a missing native binary in this environment — pre-existing)
  - tsc --noEmit clean
This commit is contained in:
DrSmoothl committed 2026-05-19 08:19:14 +08:00
1 parent d323318a7e
commit b7d8952163
5 files changed
+790 -39

No files matched your search

+23 -23
View File
@@ -5,19 +5,19 @@
import type { Bridge } from '../bridge';
import { gunzipSync, gzipSync } from 'zlib';
import { protoDecode, protoEncode } from '../../protobuf/decode';
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
import { buildSendElems } from '../element-builder';
import { parseMsgPush } from '../msg-push';
import type { ForwardNodePayload } from '../events';
import type { PacketInfo } from '../../protocol/types';
import {
LongMsgResultSchema,
RecvLongMsgReqSchema,
RecvLongMsgRespSchema,
SendLongMsgReqSchema,
SendLongMsgRespSchema,
} from '../proto/longmsg';
import { PushMsgSchema } from '../proto/message';
import type {
LongMsgResult,
RecvLongMsgReq,
RecvLongMsgResp,
SendLongMsgReq,
SendLongMsgResp,
} from '../proto/proton/longmsg';
import type { PushMsg, PushMsgBody } from '../proto/proton/message';
import { resolveSelfUid, toInt } from './shared';
// Module-scoped cache, keyed by res_id. Survives only for the lifetime
@@ -30,7 +30,7 @@ async function buildForwardPushBody(
node: ForwardNodePayload,
groupId?: number,
userUid?: string,
): Promise<Record<string, unknown>> {
): Promise<PushMsgBody> {
const fromUin = node.userUin > 0 ? node.userUin : toInt(bridge.identity.uin);
if (fromUin <= 0) throw new Error('forward node user uin is invalid');
@@ -99,24 +99,24 @@ export async function uploadForwardNodes(
}
const msgBody = await Promise.all(nodes.map(node => buildForwardPushBody(bridge, node, groupId, userUid)));
const longMsgResult = protoEncode({
const longMsgResult = protobuf_encode<LongMsgResult>({
action: [
{
actionCommand: 'MultiMsg',
actionData: { msgBody: msgBody as any },
actionData: { msgBody },
},
],
}, LongMsgResultSchema);
});
const selfUid = await resolveSelfUid(bridge);
const info: any = {
const info: SendLongMsgReq['info'] = {
type: groupId ? 3 : 1,
uid: { uid: groupId ? String(groupId) : selfUid },
payload: gzipSync(Buffer.from(longMsgResult)),
};
if (groupId) info.groupUin = groupId;
const request = protoEncode({
const request = protobuf_encode<SendLongMsgReq>({
info,
settings: {
field1: 4,
@@ -124,14 +124,14 @@ export async function uploadForwardNodes(
field3: 7,
field4: 0,
},
}, SendLongMsgReqSchema);
});
const result = await bridge.sendRawPacket('trpc.group.long_msg_interface.MsgService.SsoSendLongMsg', request);
if (!result.success || !result.gotResponse || !result.responseData) {
throw new Error(result.errorMessage || 'upload forward message failed');
}
const resp = protoDecode(result.responseData, SendLongMsgRespSchema);
const resp = protobuf_decode<SendLongMsgResp>(result.responseData);
const resId = typeof resp?.result?.resId === 'string' ? resp.result.resId : '';
if (!resId) {
throw new Error('upload forward message response missing res_id');
@@ -169,7 +169,7 @@ export async function fetchForwardNodes(bridge: Bridge, resId: string): Promise<
}
const selfUid = await resolveSelfUid(bridge);
const request = protoEncode({
const request = protobuf_encode<RecvLongMsgReq>({
info: {
uid: { uid: selfUid },
resId,
@@ -181,27 +181,27 @@ export async function fetchForwardNodes(bridge: Bridge, resId: string): Promise<
field3: 0,
field4: 0,
},
}, RecvLongMsgReqSchema);
});
const result = await bridge.sendRawPacket('trpc.group.long_msg_interface.MsgService.SsoRecvLongMsg', request);
if (!result.success || !result.gotResponse || !result.responseData) {
throw new Error(result.errorMessage || 'download forward message failed');
}
const resp = protoDecode(result.responseData, RecvLongMsgRespSchema);
const resp = protobuf_decode<RecvLongMsgResp>(result.responseData);
const payload = resp?.result?.payload;
if (!(payload instanceof Uint8Array) || payload.length === 0) {
throw new Error('download forward message payload is empty');
}
const inflate = gunzipSync(Buffer.from(payload));
const longMsg = protoDecode(inflate, LongMsgResultSchema);
const action = longMsg?.action?.find((item: any) => item?.actionCommand === 'MultiMsg');
const longMsg = protobuf_decode<LongMsgResult>(inflate);
const action = longMsg?.action?.find((item) => item?.actionCommand === 'MultiMsg');
const msgBodyList = Array.isArray(action?.actionData?.msgBody) ? action.actionData.msgBody : [];
const nodes: ForwardNodePayload[] = [];
for (const msgBody of msgBodyList) {
const wrapped = protoEncode({ message: msgBody }, PushMsgSchema);
const wrapped = protobuf_encode<PushMsg>({ message: msgBody });
const pkt: PacketInfo = {
pid: 0,
uin: bridge.identity.uin,