import type { CanonicalEvent } from '../runtime/framework/events/index.js'; /** Read producer-prepared content. A historical record without content is never enriched at delivery. */ export function canonicalEventForAgent(event: CanonicalEvent): Record { return structuredClone(event.notificationContent?.event ?? { ...event.payload, type: event.type }); } export function presentCanonicalEvent(event: CanonicalEvent, _state?: unknown): Record { return canonicalEventForAgent(event); } export function presentCanonicalEventForChannel(event: CanonicalEvent, _state?: unknown): Record | string { const value = canonicalEventForAgent(event); return event.type.startsWith('visual.') ? `${event.type} | ${String(value.text ?? '')}`.trimEnd() : value; } export const presentCanonicalEventForCclEvents = presentCanonicalEventForChannel; /** Old ledgers predate producer-prepared Desktop variants. Preserve the transport's link privacy boundary. */ function legacyDesktopPrivacy(value: any): any { if (Array.isArray(value)) return value.map(legacyDesktopPrivacy); if (!value || typeof value !== 'object') return value; return Object.fromEntries(Object.entries(value).filter(([key]) => !['url', 'spectate_url', 'spectateUrl'].includes(key)) .map(([key, item]) => [key, legacyDesktopPrivacy(item)])); } export function presentCanonicalEventForDesktop(event: CanonicalEvent, _state?: unknown): Record | string { if (event.notificationContent?.desktopEvent) return structuredClone(event.notificationContent.desktopEvent); const presented = presentCanonicalEventForChannel(event); return !event.notificationContent && ['joined', 'room_created', 'room_joined', 'session_resumed'].includes(event.type) ? legacyDesktopPrivacy(presented) : presented; } /** Transport can shorten or split this text, but cannot infer new game facts from current State. */ export function preparedMonitorText(event: CanonicalEvent): string { return event.notificationContent?.monitorText ?? JSON.stringify(canonicalEventForAgent(event)); }