diff --git a/packages/core/tests/msg-push-handler.test.ts b/packages/core/tests/msg-push-handler.test.ts index e706d9c6..4aeac485 100644 --- a/packages/core/tests/msg-push-handler.test.ts +++ b/packages/core/tests/msg-push-handler.test.ts @@ -14,6 +14,7 @@ import type { GroupMemberInfo, QQGroupInfo } from '@snowluma/protocol/qq-info'; // inline-extract the kind. import type { GroupChange, NewFriend, FriendRecall, OperatorInfo, SelfJoinInGroup, GroupAdmin, + InputStatusNotify, } from '@snowluma/proto-defs/notify'; import type { PushMsg } from '@snowluma/proto-defs/message'; import type { @@ -254,6 +255,53 @@ describe('parseMsgPush Event0x210 subType=179/226 (NewFriend → friend_add)', ( }); }); +describe('parseMsgPush Event0x210 subType=277 (input status → 对方正在输入)', () => { + // RE'd from wrapper.linux.node aio_input_state_worker.cc: 0x210/0x115 sys_msg, + // body = InputStatusNotify {fromUid, toUid, notifyItem{eventType@f4}}. + type FriendInputStatusEvent = Extract; + + function makeInputStatusPacket(fromUid: string, eventType: number): PacketInfo { + const content = protobuf_encode({ + fromUid, + toUid: 'u_self', + notifyItem: { eventType }, + }); + return makeEvent0x210PacketAny(277, content); + } + + it('emits friend_input_status (typing) for eventType 1', () => { + const member = makeGroupMember(77777, 'u_typer'); + const [event] = parseMsgPush( + makeInputStatusPacket(member.uid, 1), + makeIdentity([member]), + ) as FriendInputStatusEvent[]; + + expect(event.kind).toBe('friend_input_status'); + expect(event.userUin).toBe(77777); + expect(event.eventType).toBe(1); + expect(event.statusText).toBe('对方正在输入...'); + }); + + it('surfaces eventType 3 as the voice-recording status text', () => { + const member = makeGroupMember(77777, 'u_typer'); + const [event] = parseMsgPush( + makeInputStatusPacket(member.uid, 3), + makeIdentity([member]), + ) as FriendInputStatusEvent[]; + + expect(event.eventType).toBe(3); + expect(event.statusText).toBe('对方正在讲话...'); + }); + + it('drops a body with no fromUid (nothing to key user_id on)', () => { + const events = parseMsgPush( + makeEvent0x210PacketAny(277, protobuf_encode({ notifyItem: { eventType: 1 } })), + makeIdentity(), + ); + expect(events).toEqual([]); + }); +}); + describe('parseMsgPush Event0x210 subType=139 (self-recall direction)', () => { // 138 = friend recalled their own message sent to bot // (peer in friend_recall event = fromUid) diff --git a/packages/onebot/src/event-converter/index.ts b/packages/onebot/src/event-converter/index.ts index 5a0b6cf7..1e15ac83 100644 --- a/packages/onebot/src/event-converter/index.ts +++ b/packages/onebot/src/event-converter/index.ts @@ -7,6 +7,7 @@ import { } from './to-message'; import { convertFriendAdd, + convertFriendInputStatus, convertFriendPoke, convertFriendRecall, convertGroupAdmin, @@ -78,6 +79,7 @@ const CONVERTERS: ConverterRegistry = { group_essence: convertGroupEssence, group_file_upload: convertGroupFileUpload, friend_add: convertFriendAdd, + friend_input_status: convertFriendInputStatus, group_msg_emoji_like: convertGroupMsgEmojiLike, // Requests. friend_request: convertFriendRequest, diff --git a/packages/onebot/src/event-converter/to-notice.ts b/packages/onebot/src/event-converter/to-notice.ts index 05b8b762..cec7659f 100644 --- a/packages/onebot/src/event-converter/to-notice.ts +++ b/packages/onebot/src/event-converter/to-notice.ts @@ -17,6 +17,7 @@ type GroupEssence = Extract; type GroupFileUpload = Extract; type FriendAdd = Extract; type GroupMsgEmojiLike = Extract; +type FriendInputStatus = Extract; export function convertGroupMemberJoin(ctx: ConverterContext, event: GroupMemberJoin): JsonObject { return notice(ctx, event, { @@ -156,6 +157,18 @@ export function convertFriendAdd(ctx: ConverterContext, event: FriendAdd): JsonO }); } +export function convertFriendInputStatus(ctx: ConverterContext, event: FriendInputStatus): JsonObject { + // Mirrors NapCat's OB11InputStatusEvent: notify/input_status with the peer's + // uin, the raw event_type (1 = typing, 3 = recording voice) and status_text. + return notice(ctx, event, { + notice_type: 'notify', + sub_type: 'input_status', + user_id: event.userUin, + event_type: event.eventType, + status_text: event.statusText, + }); +} + export function convertGroupMsgEmojiLike(ctx: ConverterContext, event: GroupMsgEmojiLike): JsonObject { const messageId = applyMessageIdResolver( ctx.messageIdResolver, true, event.groupId, event.msgSeq, GROUP_MESSAGE_EVENT, diff --git a/packages/onebot/src/event-pipeline.ts b/packages/onebot/src/event-pipeline.ts index c339c50a..a96a8eb7 100644 --- a/packages/onebot/src/event-pipeline.ts +++ b/packages/onebot/src/event-pipeline.ts @@ -70,6 +70,7 @@ const NOTICE_KINDS = [ 'group_essence', 'group_file_upload', 'friend_add', + 'friend_input_status', 'group_msg_emoji_like', ] as const satisfies readonly QQEventVariant['kind'][]; diff --git a/packages/proto-defs/src/notify.ts b/packages/proto-defs/src/notify.ts index 68903cb2..b32292e6 100644 --- a/packages/proto-defs/src/notify.ts +++ b/packages/proto-defs/src/notify.ts @@ -262,3 +262,22 @@ export interface GroupReactNotify { field13?: pb<13, uint_32>; groupReactionData?: pb<44, GroupReactionData>; } + +// C2C input-status notify — the "对方正在输入…" push. Delivered as a system +// message (msgType 0x210 / subMsgType 0x115) whose `MsgBody.msgContent` carries +// this body. Field layout RE'd from `wrapper.linux.node` +// `aio_input_state_worker.cc::ProcessInputStateNotifySysMsg` (reads f1=fromUid, +// f2=toUid, f3=notifyItem; the item's f4 is the event type). +export interface InputStatusNotifyItem { + field2?: pb<2, uint_32>; + field3?: pb<3, uint_64>; + eventType?: pb<4, uint_32>; // 1 = 正在输入 (typing), 3 = 正在讲话 (recording voice) + field5?: pb<5, uint_32>; + statusText?: pb<6, string>; +} + +export interface InputStatusNotify { + fromUid?: pb<1, string>; + toUid?: pb<2, string>; + notifyItem?: pb<3, InputStatusNotifyItem>; +} diff --git a/packages/protocol/src/events.ts b/packages/protocol/src/events.ts index 5b0af057..a06cb16f 100644 --- a/packages/protocol/src/events.ts +++ b/packages/protocol/src/events.ts @@ -294,6 +294,19 @@ export interface PttTransResultEvent extends QQEvent { text: string; } +/** + * C2C "对方正在输入…" input-status push (Event 0x210 subType 0x115 / 277). + * `eventType` 1 = typing, 3 = recording a voice message. Mirrors NapCat's + * `onInputStatusPush` → OB11 `notice/notify` `sub_type:'input_status'`. + */ +export interface FriendInputStatusEvent extends QQEvent { + kind: 'friend_input_status'; + userUin: number; // the peer whose input status changed (the typer) + userUid: string; + eventType: number; // 1 = 正在输入, 3 = 正在讲话(录音) + statusText: string; +} + export type QQEventVariant = | FriendMessage | GroupMessage @@ -312,4 +325,5 @@ export type QQEventVariant = | GroupFileUploadEvent | FriendAddEvent | GroupMsgEmojiLikeEvent - | PttTransResultEvent; + | PttTransResultEvent + | FriendInputStatusEvent; diff --git a/packages/protocol/src/msg-push/decoders/event-0x210.ts b/packages/protocol/src/msg-push/decoders/event-0x210.ts index 65a922c0..2bb43324 100644 --- a/packages/protocol/src/msg-push/decoders/event-0x210.ts +++ b/packages/protocol/src/msg-push/decoders/event-0x210.ts @@ -10,6 +10,7 @@ import type { FriendRecall, FriendRequest, GeneralGrayTipInfo, + InputStatusNotify, NewFriend, } from '@snowluma/proto-defs/notify'; import type { PttTransPush } from '@snowluma/proto-defs/ptt-trans'; @@ -23,6 +24,7 @@ import { import type { MsgPushDecoder } from '../registry'; type FriendRecallEvent = Extract; +type FriendInputStatusEvent = Extract; const unknownLog = createLogger('MsgPush.Unknown'); @@ -37,6 +39,7 @@ export const decodeEvent0x210: MsgPushDecoder = (ctx) => { case Event0x210SubType.FriendRecallSelfNotice: return decodeFriendRecall(ctx); case Event0x210SubType.FriendPokeNotice: return decodeFriendPoke(ctx); + case Event0x210SubType.InputStatusNotice: return decodeInputStatus(ctx); // 179 + 226 both carry the NewFriend payload — see enum comment. case Event0x210SubType.NewFriendNotice: case Event0x210SubType.NewFriendNoticeAlt: @@ -128,6 +131,34 @@ function decodeNewFriend(ctx: MsgPushContext): QQEventVariant[] { return [ev]; } +// C2C "对方正在输入…" input-status push. Body (`InputStatusNotify`) rides in +// `body.msgContent` (= ctx.content). Layout + event-type semantics RE'd from +// `aio_input_state_worker.cc::ProcessInputStateNotifySysMsg`: the notify item's +// field 4 is the event type (1 = typing, 3 = recording voice). The client +// synthesises the status text from that type, so we do the same for OneBot +// parity (NapCat's `onInputStatusPush` → `notice/notify` `input_status`). +function decodeInputStatus(ctx: MsgPushContext): QQEventVariant[] { + const notify = protobuf_decode(ctx.content); + const fromUid = notify?.fromUid ?? ''; + if (!fromUid) return []; + const eventType = notify.notifyItem?.eventType ?? 1; + const ev: FriendInputStatusEvent = { + kind: 'friend_input_status', + time: ctx.head.timestamp, + selfUin: ctx.selfUin, + userUin: resolveUidToUin(ctx.identity, 0, fromUid, ctx.fromUin), + userUid: fromUid, + eventType, + statusText: inputStatusText(eventType), + }; + return [ev]; +} + +// The client's own strings (wrapper.linux.node @0xB8FFE0 / @0xB90000). +function inputStatusText(eventType: number): string { + return eventType === 3 ? '对方正在讲话...' : '对方正在输入...'; +} + function decodeFriendPoke(ctx: MsgPushContext): QQEventVariant[] { const grayTip = protobuf_decode(ctx.content); if (!grayTip || (grayTip.busiType ?? 0n) !== 12n) return []; diff --git a/packages/protocol/src/msg-push/enums.ts b/packages/protocol/src/msg-push/enums.ts index e94c157c..d972def1 100644 --- a/packages/protocol/src/msg-push/enums.ts +++ b/packages/protocol/src/msg-push/enums.ts @@ -67,6 +67,15 @@ export enum Event0x210SubType { */ NewFriendNotice = 179, NewFriendNoticeAlt = 226, + /** + * C2C input-status notify — the "对方正在输入…" push. subMsgType 0x115 (277). + * RE-confirmed against `wrapper.linux.node` + * `aio_input_state_worker.cc::IsInputStateNotifySysMsg`, which matches exactly + * `(msg_type ^ 0x210) | (sub_msg_type ^ 0x115) == 0`. The payload + * (`InputStatusNotify`) rides in `body.msgContent`. Mirrors NapCat's + * `onInputStatusPush`. + */ + InputStatusNotice = 277, /** * Group-app state push (troop shortcut bar / discussion app). *