feat(onebot): support private window shake messages

Encode poke message segments as private window shake messages while keeping the dedicated send_poke action unchanged.

Restrict window shakes to a single ordinary private-chat segment. Reject group, temporary-session, mixed-message, and forwarded usage before identity lookup, media upload, or packet transmission.

Add full outbound request coverage, OneBot action regressions, and live private-chat verification for issue #259.

Closes #259
This commit is contained in:
DrSmoothl committed 2026-07-21 19:28:17 +08:00
1 parent d8d556eb70
commit ff60221330
15 files changed
+529 -44

No files matched your search

+26 -2
View File
@@ -2,7 +2,7 @@ import type {
MarkdownData,
MentionExtraSend,
} from '@snowluma/proto-defs/action';
import type { Elem, GroupFileExtra, MarketFacePbReserve, QFaceExtra, QSmallFaceExtra } from '@snowluma/proto-defs/element';
import type { Elem, GroupFileExtra, MarketFacePbReserve, PokeExtra, QFaceExtra, QSmallFaceExtra } from '@snowluma/proto-defs/element';
import { protobuf_encode } from '@snowluma/proton';
import { randomUUID } from 'crypto';
import { deflateSync } from 'zlib';
@@ -10,8 +10,10 @@ import type { BridgeContext } from './bridge-context';
import { sysFaceStore } from './sys-face-store';
import type { MessageElement } from './events';
import {
assertWindowShakeSendPolicy,
assertValidMessageElements,
MessageElementValidationError,
type OutboundMessageScene,
} from './element-manifest';
import { uploadImageMsgInfo } from './highway/image-upload';
import { hexToBytes } from './highway/pipeline';
@@ -24,6 +26,8 @@ export interface SendContext {
bridge: BridgeContext;
groupId?: number;
userUid?: string;
/** Explicit transport scene used by scene-limited message elements. */
scene?: OutboundMessageScene;
/**
* Set by the forward-message upload path. When `true`, file segments
* are encoded as receive-side wire shapes (group → `transElem(24)`,
@@ -204,6 +208,17 @@ function makeMarkdownElem(element: MessageElement): ProtoElem {
};
}
function makePokeElem(element: MessageElement): ProtoElem {
const pokeType = element.subType;
return {
commonElem: {
serviceType: 2,
pbElem: protobuf_encode<PokeExtra>({ type: pokeType }),
businessType: pokeType,
},
};
}
/**
* Build a market-face (商城表情) wire element from the markers carried on the
@@ -435,7 +450,7 @@ async function makeVideoElem(ctx: SendContext, element: MessageElement): Promise
/**
* Build proto Elem objects from an array of MessageElements.
* Supports: text, face, mface, at, reply, json, xml, markdown, image, record, video, forward.
* Supports: text, face, mface, at, reply, json, xml, markdown, image, record, video, forward, poke.
* Image, record and video elements trigger NTV2 highway upload via the SendContext.
*/
export async function buildSendElems(elements: MessageElement[], ctx?: SendContext): Promise<ProtoElem[]> {
@@ -443,6 +458,11 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte
// preserves all-or-nothing semantics: a malformed later segment cannot make
// an earlier media segment perform side effects before the send is rejected.
assertValidMessageElements(elements, 'W');
assertWindowShakeSendPolicy(
elements.filter((element) => element.type === 'poke').length,
elements.length,
ctx?.scene,
);
for (const element of elements) {
if ((element.type === 'image' || element.type === 'record' || element.type === 'video') && !ctx) {
throw new MessageElementValidationError(
@@ -478,6 +498,10 @@ export async function buildSendElems(elements: MessageElement[], ctx?: SendConte
result.push(makeFaceElem(elem.faceId, ctx));
break;
case 'poke':
result.push(makePokeElem(elem));
break;
case 'mface':
result.push(makeMarketFaceElem(elem));
break;
+39 -11
View File
@@ -1,7 +1,7 @@
/**
* 消息元素「四向」对账清单 —— 单一权威。
*
* 一种消息元素(文字/图片/戳一戳/商城表情…)要在两个流向、共 4 处处理,散在
* 一种消息元素(文字/图片/窗口抖动/商城表情…)要在两个流向、共 4 处处理,散在
* 不同文件、不同包。历史上这 4 处各写各的、没人对账,导致字段名对不上、漏写
* 方向无人知(都走运行时兜底 `default`,悄悄失败)。
*
@@ -37,6 +37,36 @@ export type ElementDirection = (typeof ELEMENT_DIRECTIONS)[number];
export type DirectionSupport = 'yes' | 'no' | 'by-design-no';
/** The outbound transport selected before any resolver, upload, or packet send. */
export type OutboundMessageScene = 'direct-private' | 'group' | 'group-temp' | 'forward';
/**
* Window shake is an action-like CommonElem, not ordinary message content.
* Keep its narrow send contract in the protocol package so every caller
* rejects unsupported destinations and mixed payloads before side effects.
*/
export function assertWindowShakeSendPolicy(
windowShakeCount: number,
segmentCount: number,
scene?: OutboundMessageScene,
): void {
if (windowShakeCount === 0) return;
if (scene !== 'direct-private') {
throw new MessageElementValidationError(
'UNSENDABLE_TYPE',
'message element "poke" can only be sent in a direct private chat',
'poke',
);
}
if (windowShakeCount !== 1 || segmentCount !== 1) {
throw new MessageElementValidationError(
'UNSENDABLE_TYPE',
'message element "poke" must be the only segment in a private window-shake request',
'poke',
);
}
}
type ElementField<T extends MessageElementType> = Exclude<keyof MessageElementOf<T>, 'type'> & string;
export interface ElementSpec<T extends MessageElementType> {
@@ -138,10 +168,10 @@ export const ELEMENT_MANIFEST = {
note: 'W 特殊:live-send 在 OneBot 层被拆走走独立上传管线,仅 forwardFake 时落 transElem(24)',
},
poke: {
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'by-design-no' },
directions: { D: 'yes', S: 'yes', P: 'yes', W: 'yes' },
fields: fieldsFor<'poke'>()(['subType']),
requiredFields: ['subType'],
note: 'P 仅负责把 OneBot 段规范化为 subType;W 会明确拒绝,因为 QQ 不允许戳一戳作为消息元素发送。请使用独立 poke Action',
note: 'W 仅支持普通好友私聊窗口抖动;send_poke Action 仍表示拍一拍',
},
markdown: {
directions: { D: 'no', S: 'no', P: 'yes', W: 'yes' },
@@ -429,11 +459,9 @@ export function assertValidMessageElement(
const spec = ELEMENT_MANIFEST[type];
if (direction && spec.directions[direction] !== 'yes') {
const hint = type === 'poke'
? 'use the dedicated poke Action instead'
: type === 'flash_file'
? 'use the send_flash_msg Action instead'
: `direction ${direction} is not supported`;
const hint = type === 'flash_file'
? 'use the send_flash_msg Action instead'
: `direction ${direction} is not supported`;
throwValidation('UNSENDABLE_TYPE', `message element "${type}" cannot be sent; ${hint}`, type);
}
@@ -485,9 +513,9 @@ export function typesForDirection(dir: ElementDirection): Set<string> {
/**
* 发·解(P)侧还识别一批「纯 OneBot 输入词」:可执行的会塌缩成 json / face,
* node 仅允许在 forward node list 的专用解析器中使用,shake / anonymous 因没有
* 合法消息元素语义而返回 typed validation error。它们没有收侧对应,也没有专属
* wire 形态,故不进本清单;对账 P 时须先从代码里剔除这些类型。
* node 仅允许在 forward node list 的专用解析器中使用,shake 会规范化为 poke,
* anonymous 因没有合法消息元素语义而返回 typed validation error。它们没有收侧
* 对应,也没有专属 wire 形态,故不进本清单;对账 P 时须先从代码里剔除这些类型。
*/
export const INPUT_SUGAR_SEGMENTS: ReadonlySet<string> = new Set([
'node', 'share', 'music', 'location', 'contact', 'rps', 'dice', 'shake', 'anonymous',
+1 -1
View File
@@ -149,7 +149,7 @@ export interface FileElement {
sha1Hex?: string;
}
/** Receive-side representation only; sending uses the dedicated poke Action. */
/** Window-shake element; outbound use is limited to ordinary friend C2C. */
export interface PokeElement {
type: 'poke';
subType: number;
+1 -1
View File
@@ -124,7 +124,7 @@ function renderSegment(type: string, data: Record<string, unknown>): string {
case 'xml': return '[XML]';
case 'markdown': return '[Markdown]';
case 'forward': return '[聊天记录]';
case 'poke': return '[戳一戳]';
case 'poke': return '[窗口抖动]';
case 'flash_file': {
const title = data.title ? String(data.title) : '';
return title ? `[闪传文件:${truncate(title, 20)}]` : '[闪传文件]';