diff --git a/packages/onebot/src/network/adapter.ts b/packages/onebot/src/network/adapter.ts index 2c432538..e1bda450 100644 --- a/packages/onebot/src/network/adapter.ts +++ b/packages/onebot/src/network/adapter.ts @@ -1,6 +1,6 @@ import { createLogger, type Logger } from '@snowluma/common/logger'; import type { ApiHandler } from '../api-handler'; -import type { DispatchPayload } from '../event-filter'; +import { shapeEventForAdapter, type DispatchPayload, type EventReportOptions } from '../event-filter'; import type { JsonObject, NetworkBase } from '../types'; export interface NetworkAdapterContext { @@ -58,6 +58,39 @@ export abstract class IOneBotNetworkAdapter { /** Report live connection health for the WebUI dashboard. */ abstract describeStatus(): AdapterStatus; + // ── meta-event framing: shared by the connection-oriented adapters ───── + // + // Lifecycle (connect/enable/disable) and heartbeat meta events don't flow + // through `OneBotNetworkManager.emitEvent` (no pre-serialized DispatchPayload), + // so each connection-oriented adapter shapes them per its report options and + // serializes on the spot. The shape+serialize policy lives here once; the + // adapters only own the transport (which socket(s) to `safeSend` to). + + /** Shape one meta event for `options` and serialize it to a wire frame, or + * `null` when the shaper drops it (e.g. a self-message under + * `reportSelfMessage:false`). */ + protected metaFrame(event: JsonObject, options: EventReportOptions): string | null { + const shaped = shapeEventForAdapter(event, options); + return shaped === null ? null : JSON.stringify(shaped); + } + + /** The bootstrap meta frames an event client receives on (re)connect: + * `connect`, `enable`, then a `heartbeat`, shaped for `options`. Frames the + * shaper drops are omitted. */ + protected bootstrapMetaFrames(options: EventReportOptions): string[] { + const events = [ + this.ctx.buildLifecycleEvent('connect'), + this.ctx.buildLifecycleEvent('enable'), + this.ctx.buildHeartbeatEvent(), + ]; + const frames: string[] = []; + for (const event of events) { + const frame = this.metaFrame(event, options); + if (frame !== null) frames.push(frame); + } + return frames; + } + // ── reload: shared hot-reload state machine (template method) ────────── // // All four adapters reconcile a live config swap identically: decide diff --git a/packages/onebot/src/network/ws-client-adapter.ts b/packages/onebot/src/network/ws-client-adapter.ts index 891958b8..c68f0049 100644 --- a/packages/onebot/src/network/ws-client-adapter.ts +++ b/packages/onebot/src/network/ws-client-adapter.ts @@ -3,7 +3,6 @@ import { createLogger } from '@snowluma/common/logger'; import { pickDispatchJson, resolveReportOptions, - shapeEventForAdapter, type DispatchPayload, type EventReportOptions, } from '../event-filter'; @@ -153,16 +152,7 @@ export class WsClientAdapter extends IOneBotNetworkAdapter { private sendBootstrapMetaEvents(socket: WebSocket): void { if (this.role !== 'Event' && this.role !== 'Universal') return; - const events = [ - this.ctx.buildLifecycleEvent('connect'), - this.ctx.buildLifecycleEvent('enable'), - this.ctx.buildHeartbeatEvent(), - ]; - for (const event of events) { - const shaped = shapeEventForAdapter(event, this.options); - if (!shaped) continue; - safeSend(socket, JSON.stringify(shaped)); - } + for (const frame of this.bootstrapMetaFrames(this.options)) safeSend(socket, frame); } } diff --git a/packages/onebot/src/network/ws-server-adapter.ts b/packages/onebot/src/network/ws-server-adapter.ts index 55c0a285..1904b32f 100644 --- a/packages/onebot/src/network/ws-server-adapter.ts +++ b/packages/onebot/src/network/ws-server-adapter.ts @@ -4,7 +4,6 @@ import { createLogger } from '@snowluma/common/logger'; import { pickDispatchJson, resolveReportOptions, - shapeEventForAdapter, type DispatchPayload, type EventReportOptions, } from '../event-filter'; @@ -45,9 +44,8 @@ export class WsServerAdapter extends IOneBotNetworkAdapter { const lifecycle = this.ctx.buildLifecycleEvent('disable'); for (const conn of this.connections.values()) { if (conn.role === 'Api') continue; - const shaped = shapeEventForAdapter(lifecycle, conn.options); - if (!shaped) continue; - safeSend(conn.socket, JSON.stringify(shaped)); + const frame = this.metaFrame(lifecycle, conn.options); + if (frame) safeSend(conn.socket, frame); } this.isEnabled = false; @@ -142,16 +140,7 @@ export class WsServerAdapter extends IOneBotNetworkAdapter { } private sendBootstrapMetaEvents(socket: WebSocket): void { - const events = [ - this.ctx.buildLifecycleEvent('connect'), - this.ctx.buildLifecycleEvent('enable'), - this.ctx.buildHeartbeatEvent(), - ]; - for (const event of events) { - const shaped = shapeEventForAdapter(event, this.options); - if (!shaped) continue; - safeSend(socket, JSON.stringify(shaped)); - } + for (const frame of this.bootstrapMetaFrames(this.options)) safeSend(socket, frame); } }