import type { EventNotificationContent } from '../../../framework/events/types.js'; import type { RuntimeEpochPluginContext } from '../../../framework/plugins/index.js'; import { compactEventForChannel, formatEventMessage } from '../../../../pipeline/event-format.js'; import { formatCanonicalEventHint, type EventHintOrigin } from '../../../../pipeline/event-hints.js'; import { buildLocalGameStartedEnrichment } from '../../../../lib/game-context.js'; import { projectClawclawCurrentState } from '../../runtime-state.js'; export type NotificationOwner = 'builtin' | 'custom' | 'system'; /** Policy belongs to the producing perception/system notification, not EventIngress or delivery. */ export function prepareNotificationContent( type: string, payload: Readonly>, state: Record | null, owner: NotificationOwner, origin: EventHintOrigin = { producer: 'local_synth' }, ): EventNotificationContent { const { hint, next_step: _next, notice: _notice, ...facts } = structuredClone(payload); const event = compactEventForChannel({ ...facts, type }, { summary: state }); if (owner === 'builtin' && type === 'game_started') Object.assign(event, buildLocalGameStartedEnrichment()); // meeting_briefing.speech_order is exclusively backend-owned. Never fill a missing order from State. const ownedHint = owner === 'custom' ? typeof hint === 'string' && hint.trim() ? hint : undefined : formatCanonicalEventHint(origin, event, { summary: state }); if (ownedHint !== undefined) event.hint = ownedHint; const content: EventNotificationContent = { event, monitorText: type.startsWith('visual.') ? `${type} | ${String(event.text ?? '')}`.trimEnd() : formatEventMessage(event, { summary: state, maxTextLength: Number.MAX_SAFE_INTEGER }), }; if (owner === 'system' && ['joined', 'room_created', 'room_joined', 'session_resumed'].includes(type)) { const omitLinks = (value: any): any => Array.isArray(value) ? value.map(omitLinks) : value && typeof value === 'object' ? Object.fromEntries(Object.entries(value) .filter(([key]) => !['url', 'spectate_url', 'spectateUrl'].includes(key)).map(([key, item]) => [key, omitLinks(item)])) : value; const desktop = omitLinks(event); if (type === 'joined') desktop.hint = event.recovered === true || event.resumed === true ? 'The game runtime reattached; do not start a duplicate runtime.' : 'The game runtime is attached; do not start a duplicate runtime.'; if (type === 'room_created') desktop.hint = 'Keep the room runtime attached.'; if (type === 'room_joined') desktop.hint = event.recovered === true || event.resumed === true ? 'The room runtime reattached; keep it attached.' : 'Keep the room runtime attached.'; content.desktopEvent = desktop; } return content; } /** The event's accepted State boundary, never the newer State at delivery time. */ export function notificationStateAt(context: RuntimeEpochPluginContext>, epochSeq: number | undefined): Record | null { if (epochSeq === undefined) return null; let beforeFrameSeq: number | undefined; while (true) { const page = context.stateHistory.queryStateFrames({ scope: context.scope, toEpochSeq: epochSeq, ...(beforeFrameSeq === undefined ? {} : { beforeFrameSeq }), order: 'desc', limit: 64 }); for (const frame of page.frames) { const state = projectClawclawCurrentState(frame.state); if (state) return state; } if (!page.page.hasMore || !page.frames.length) return null; beforeFrameSeq = page.frames.at(-1)!.frameSeq; } }