feat(proton): migrate action.ts schemas + bridge.ts + element-builder.ts

Mirrors all 11 schemas in bridge/proto/action.ts as proton interface
forms and switches the two consumers (element-builder.ts and the
send-message paths in bridge.ts) over to the new compile-time API.

Migrated schemas (proton/action.ts):
  RoutingC2C, RoutingGroup, RoutingHead, SendContentHead,
  MessageControl, SendRichText, SendMessageBody, SendMessageRequest,
  SendMessageResponse, MentionExtraSend, MarkdownData

Consumer switches:
  - element-builder.ts: protobuf_encode<MentionExtraSend> for @ mention
    pbReserve payload; protobuf_encode<MarkdownData> for markdown elem
  - bridge.ts: protobuf_encode<SendMessageRequest> +
    protobuf_decode<SendMessageResponse> for both sendGroupMessage and
    sendPrivateMessage

The `ProtoElem` type alias in element-builder.ts (Partial<ProtoDecoded<typeof
ElemSchema>>) stays as-is — it's a pure TS type used internally for
building elem objects and doesn't affect runtime behaviour. Replacing
it with the proton Elem type is a larger refactor that can come later
when there's a reason to.

Verification:
  - 6 new parity tests for action schemas (RoutingHead c2c+grp,
    SendMessageRequest full shape, SendMessageResponse, MentionExtraSend,
    MarkdownData)
  - 591/591 core test suite passes — the existing send-message and
    element-builder tests implicitly validated wire format equivalence
  - tsc --noEmit clean
This commit is contained in:
DrSmoothl committed 2026-05-19 08:19:14 +08:00
1 parent 9f32dbd60c
commit ccc31a760c
4 files changed
+195 -24

No files matched your search

+16 -16
View File
@@ -7,16 +7,16 @@ import type { ForwardNodePayload, QQEventVariant, MessageElement } from './event
import type { FriendInfo, QQGroupInfo, GroupMemberInfo, UserProfileInfo, GroupRequestInfo } from './qq-info';
import { MSG_PUSH_CMD, parseMsgPush } from './msg-push';
import type { PacketSender, SendPacketResult } from '../protocol/packet-sender';
import { protoEncode, protoDecode } from '../protobuf/decode';
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
import { buildSendElems } from './element-builder';
import { IdentityService } from './identity-service';
import type { BridgeInterface } from './bridge-interface';
import { IncomingPacketPipeline, type CmdParser } from './packet-pipeline';
import { createLogger } from '../utils/logger';
import {
SendMessageRequestSchema,
SendMessageResponseSchema,
} from './proto/action';
import type {
SendMessageRequest,
SendMessageResponse,
} from './proto/proton/action';
// Delegated modules
import {
@@ -282,13 +282,13 @@ export class Bridge implements BridgeInterface {
const protoElems = await buildSendElems(elements, { bridge: this, groupId });
const random = this.nextMessageRandom();
const request = protoEncode({
const request = protobuf_encode<SendMessageRequest>({
routingHead: {
grp: { groupCode: BigInt(groupId) },
} as any,
},
contentHead: {
type: 1,
} as any,
},
messageBody: {
richText: {
elems: protoElems,
@@ -300,7 +300,7 @@ export class Bridge implements BridgeInterface {
via: 0,
dataStatist: 0,
multiSendSeq: 0,
}, SendMessageRequestSchema);
});
const result = await this.sendRawPacket(Bridge.SEND_MSG_CMD, request);
@@ -308,7 +308,7 @@ export class Bridge implements BridgeInterface {
throw new Error(`send group message failed: ${result.errorMessage || 'no response'}`);
}
const response = protoDecode(result.responseData, SendMessageResponseSchema);
const response = protobuf_decode<SendMessageResponse>(result.responseData);
if (!response) {
throw new Error('failed to decode SendMessageResponse');
}
@@ -343,18 +343,18 @@ export class Bridge implements BridgeInterface {
const random = this.nextMessageRandom();
const clientSeq = this.nextClientSequence();
const request = protoEncode({
const request = protobuf_encode<SendMessageRequest>({
routingHead: {
c2c: {
uin: userUin,
...(userUid ? { uid: userUid } : {}),
},
} as any,
},
contentHead: {
type: 1,
subType: 0,
c2cCmd: 11,
} as any,
},
messageBody: {
richText: {
elems: protoElems,
@@ -367,9 +367,9 @@ export class Bridge implements BridgeInterface {
dataStatist: 0,
ctrl: {
msgFlag: Math.floor(Date.now() / 1000),
} as any,
},
multiSendSeq: 0,
}, SendMessageRequestSchema);
});
const result = await this.sendRawPacket(Bridge.SEND_MSG_CMD, request);
@@ -377,7 +377,7 @@ export class Bridge implements BridgeInterface {
throw new Error(`send private message failed: ${result.errorMessage || 'no response'}`);
}
const response = protoDecode(result.responseData, SendMessageResponseSchema);
const response = protobuf_decode<SendMessageResponse>(result.responseData);
if (!response) {
throw new Error('failed to decode SendMessageResponse');
}
+8 -8
View File
@@ -5,14 +5,14 @@
import type { Bridge } from './bridge';
import type { MessageElement } from './events';
import type { ProtoDecoded } from '../protobuf/decode';
import { protoEncode } from '../protobuf/decode';
import { protobuf_encode } from '@snowluma/proton';
import {
ElemSchema,
} from './proto/element';
import {
MentionExtraSendSchema,
MarkdownDataSchema,
} from './proto/action';
import type {
MentionExtraSend,
MarkdownData,
} from './proto/proton/action';
import { uploadImageMsgInfo } from './highway/image-upload';
import { uploadPttMsgInfo } from './highway/ptt-upload';
import { uploadVideoMsgInfo } from './highway/video-upload';
@@ -51,12 +51,12 @@ function makeMentionElem(element: MessageElement, ctx?: SendContext): ProtoElem
const mentionAll = element.uid === 'all' || element.targetUin === 0;
const targetUin = element.targetUin ?? 0;
const extra = protoEncode({
const extra = protobuf_encode<MentionExtraSend>({
type: mentionAll ? 1 : 2,
uin: mentionAll ? 0 : targetUin,
field5: 0,
uid: mentionAll ? 'all' : (element.uid ?? ''),
}, MentionExtraSendSchema);
});
// Prefer an explicit display string from the caller; otherwise look the
// target up in the roster so QQ renders `@昵称` instead of `@QQ号`.
@@ -128,7 +128,7 @@ function makeXmlElem(element: MessageElement): ProtoElem {
}
function makeMarkdownElem(element: MessageElement): ProtoElem {
const data = protoEncode({ content: element.text ?? '' }, MarkdownDataSchema);
const data = protobuf_encode<MarkdownData>({ content: element.text ?? '' });
return {
commonElem: {
@@ -0,0 +1,93 @@
// Proton (compile-time) form of bridge/proto/action.ts.
// One-to-one mirror; legacy `*Schema` constants stay alongside for back-compat.
import type { pb, pb_repeated, int_32, uint_32, uint_64, bytes } from '@snowluma/proton';
import type { Elem } from './element';
// ── Routing ─────────────────────────────────────────────────────────
export interface RoutingC2C {
uin?: pb<1, uint_32>;
uid?: pb<2, string>;
}
export interface RoutingGroup {
groupCode?: pb<1, uint_64>;
}
export interface RoutingHead {
c2c?: pb<1, RoutingC2C>;
grp?: pb<2, RoutingGroup>;
}
// ── Content Head (for send) ─────────────────────────────────────────
export interface SendContentHead {
type?: pb<1, uint_32>;
subType?: pb<2, uint_32>;
c2cCmd?: pb<3, uint_32>;
}
// ── Message Control ─────────────────────────────────────────────────
export interface MessageControl {
msgFlag?: pb<1, int_32>;
}
// ── RichText (for send — only elems) ────────────────────────────────
export interface SendRichText {
elems?: pb_repeated<2, Elem>;
}
// ── MessageBody (for send) ──────────────────────────────────────────
export interface SendMessageBody {
richText?: pb<1, SendRichText>;
}
// ── SendMessageRequest ──────────────────────────────────────────────
export interface SendMessageRequest {
routingHead?: pb<1, RoutingHead>;
contentHead?: pb<2, SendContentHead>;
messageBody?: pb<3, SendMessageBody>;
clientSequence?: pb<4, uint_32>;
random?: pb<5, uint_32>;
syncCookie?: pb<6, bytes>;
via?: pb<8, uint_32>;
dataStatist?: pb<9, uint_32>;
ctrl?: pb<12, MessageControl>;
multiSendSeq?: pb<14, uint_32>;
}
// ── SendMessageResponse ─────────────────────────────────────────────
export interface SendMessageResponse {
result?: pb<1, int_32>;
errMsg?: pb<2, string>;
timestamp1?: pb<3, uint_32>;
field10?: pb<10, uint_32>;
groupSequence?: pb<11, uint_32>;
timestamp2?: pb<12, uint_32>;
privateSequence?: pb<14, uint_32>;
}
// ── MentionExtra (for building @mention text element) ───────────────
//
// Same wire shape as element.ts's MentionExtra (which is used for decoding
// inbound mentions) — kept under a distinct name to mark the send-side
// usage, matching the legacy convention.
export interface MentionExtraSend {
type?: pb<3, int_32>;
uin?: pb<4, uint_32>;
field5?: pb<5, int_32>;
uid?: pb<9, string>;
}
// ── MarkdownData ────────────────────────────────────────────────────
export interface MarkdownData {
content?: pb<1, string>;
}
+78
View File
@@ -36,6 +36,13 @@ import {
FriendRequestSchema,
GroupJoinSchema,
} from '../src/bridge/proto/notify';
import {
RoutingHeadSchema,
SendMessageRequestSchema,
SendMessageResponseSchema,
MentionExtraSendSchema,
MarkdownDataSchema,
} from '../src/bridge/proto/action';
import type {
SendLongMsgResp,
@@ -66,6 +73,13 @@ import type {
FriendRequest as FriendRequestNotify,
GroupJoin,
} from '../src/bridge/proto/proton/notify';
import type {
RoutingHead,
SendMessageRequest,
SendMessageResponse,
MentionExtraSend,
MarkdownData,
} from '../src/bridge/proto/proton/action';
function hex(buf: Uint8Array): string {
return Buffer.from(buf).toString('hex');
@@ -385,6 +399,70 @@ describe('proton ↔ legacy parity (longmsg subset)', () => {
expect(hex(proton)).toBe(hex(legacy));
});
// ── action.ts coverage ─────────────────────────────────────────────
it('encodes RoutingHead (c2c variant) identically', () => {
const data: RoutingHead = { c2c: { uin: 10001, uid: 'u_a' } };
const legacy = protoEncode(data as any, RoutingHeadSchema);
const proton = protobuf_encode<RoutingHead>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes RoutingHead (group variant) identically', () => {
const data: RoutingHead = { grp: { groupCode: 555n } };
const legacy = protoEncode(data as any, RoutingHeadSchema);
const proton = protobuf_encode<RoutingHead>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes SendMessageRequest (group with two text elements) identically', () => {
const data: SendMessageRequest = {
routingHead: { grp: { groupCode: 555n } },
contentHead: { type: 1, subType: 1, c2cCmd: 11 },
messageBody: {
richText: {
elems: [
{ text: { str: 'hi ' } },
{ text: { str: 'world', link: 'lnk' } },
],
},
},
clientSequence: 1,
random: 12345,
syncCookie: new Uint8Array([0xaa, 0xbb]),
via: 1,
dataStatist: 1,
multiSendSeq: 1,
};
const legacy = protoEncode(data as any, SendMessageRequestSchema);
const proton = protobuf_encode<SendMessageRequest>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes SendMessageResponse identically', () => {
const data: SendMessageResponse = {
result: 1, errMsg: 'ok', timestamp1: 1234567890,
field10: 1, groupSequence: 100, timestamp2: 1234567891, privateSequence: 200,
};
const legacy = protoEncode(data as any, SendMessageResponseSchema);
const proton = protobuf_encode<SendMessageResponse>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes MentionExtraSend identically', () => {
const data: MentionExtraSend = { type: 2, uin: 10001, field5: 1, uid: 'u_a' };
const legacy = protoEncode(data as any, MentionExtraSendSchema);
const proton = protobuf_encode<MentionExtraSend>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes MarkdownData identically', () => {
const data: MarkdownData = { content: '**bold** _italic_' };
const legacy = protoEncode(data as any, MarkdownDataSchema);
const proton = protobuf_encode<MarkdownData>(data);
expect(hex(proton)).toBe(hex(legacy));
});
// ── Documented divergence: proto3 default-value omission ────────────
it('proton omits default-valued numeric fields (legacy emits them)', () => {