import type { EventNotificationContent } from '../../../framework/events/types.js'; import type { GameSyncPerceptionTime } from '../../../game-sync-protocol.js'; import { timeRange } from '../../../../sdk/perception-plugin/math.js'; import { prepareNotificationContent } from './notification-content.js'; export const PERCEPTION_REPORT_VIEW = 'clawclaw.perception-report'; export type PerceptionReportContent = GameSyncPerceptionTime & { readonly perceiver_id: string; readonly output: string; }; const isTick = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0; /** Freeze the display projection at the producing module's accepted input boundary. */ export function preparePerceptionNotification( type: string, payload: Readonly>, state: Record | null, perceiverId: string, backendTick?: number | null, ): EventNotificationContent { const notification = prepareNotificationContent(type, payload, state, 'custom'); let output = typeof payload.text === 'string' ? payload.text.trim() : ''; if (!output) return notification; const from = payload.from_source_tick ?? payload.from_tick; const to = payload.to_source_tick ?? payload.to_tick; let time: GameSyncPerceptionTime; if (from !== undefined || to !== undefined) { if (!isTick(from) || !isTick(to) || from > to || payload.source_tick !== undefined) return notification; time = from === to ? { source_tick: to } : { from_source_tick: from, to_source_tick: to }; // Existing visual modules use this exact SDK formatter. Do not parse arbitrary prose for times. if (type.startsWith('visual.')) { const prefix = timeRange(from, to); for (const separator of [',', ', ']) { if (output.startsWith(prefix + separator)) { output = output.slice(prefix.length + separator.length).trim(); break; } } } } else { const tick = payload.source_tick ?? payload.tick ?? backendTick; if (!isTick(tick)) return notification; time = { source_tick: tick }; } if (output) notification.views = { [PERCEPTION_REPORT_VIEW]: { perceiver_id: perceiverId, output, ...time, } satisfies PerceptionReportContent }; return notification; }