import { presentCanonicalEvent, preparedMonitorText } from '../../../../pipeline/canonical-event-presentation.js'; import { fitShortMonitorPayload, } from '../../../canonical-monitor.js'; import type { CanonicalEvent } from '../../../framework/events/index.js'; import type { RuntimeReadyView } from '../../../framework/runtime.js'; import { clawclawMonitorPriority, clawclawMonitorEventPriority, } from '../../agent-event-policy.js'; import { isRoomMode } from '../../../../lib/room.js'; export const CLAWCLAW_MONITOR_INLINE_MESSAGE_MAX_CHARS = 260; /** Events whose decision-grade payload cannot fit in the short Monitor surface. */ export const CLAWCLAW_DETAIL_EVENT_TYPES = new Set([ 'game_started', 'role_assigned', 'players_changed', 'meeting_briefing', 'exile', 'no_exile', 'joined', 'room_created', 'room_joined', ]); export interface ClawclawMonitorItemPayload extends Record { schema: 'clawclaw.monitor-item.v1'; event_id: string; monitor_priority?: number; ready: { epoch_seq: number; frame_seq: number; event_commit_seq: number; }; state: Record | null; state_status: Record; event: Record; message: string; detail_required: boolean; backlog?: { remaining_events: number; through_event_commit_seq: number; command: 'ccl events'; }; } function cleanObject>(value: T): T { for (const key of Object.keys(value)) { if (value[key] === undefined) delete value[key]; } return value; } function truncate(value: string, maxChars: number): string { if (value.length <= maxChars) return value; return `${value.slice(0, Math.max(0, maxChars - 1))}…`; } export function clawclawEventRequiresDetails( event: CanonicalEvent, state?: Record | null, ): boolean { if (CLAWCLAW_DETAIL_EVENT_TYPES.has(event.type)) return true; return preparedMonitorText(event).length > CLAWCLAW_MONITOR_INLINE_MESSAGE_MAX_CHARS; } export function monitorItemPayload( event: CanonicalEvent, ready: RuntimeReadyView, ): ClawclawMonitorItemPayload { const presented = presentCanonicalEvent(event, ready.state.state); const fullMessage = preparedMonitorText(event); return { schema: 'clawclaw.monitor-item.v1', monitor_priority: clawclawMonitorEventPriority(event), event_id: event.eventId, ready: { epoch_seq: ready.epochSeq, frame_seq: ready.frameSeq, event_commit_seq: ready.eventWatermark, }, state: ready.state.state, state_status: ready.state.stateStatus, event: presented, message: truncate(fullMessage, CLAWCLAW_MONITOR_INLINE_MESSAGE_MAX_CHARS), detail_required: CLAWCLAW_DETAIL_EVENT_TYPES.has(event.type) || fullMessage.length > CLAWCLAW_MONITOR_INLINE_MESSAGE_MAX_CHARS, }; } function orderedItems(items: readonly ClawclawMonitorItemPayload[]): ClawclawMonitorItemPayload[] { return items .map((item, index) => ({ item, index })) .sort((left, right) => ( (right.item.monitor_priority ?? clawclawMonitorPriority(String(right.item.event.type ?? ''))) - (left.item.monitor_priority ?? clawclawMonitorPriority(String(left.item.event.type ?? ''))) || left.index - right.index )) .map(({ item }) => item); } /** Compose one Host wake-up from Mailbox items sharing one immutable Ready View. */ export function composeClawclawMonitorNotification( values: readonly Record[], ): Record { const items = orderedItems(values.filter((value): value is ClawclawMonitorItemPayload => ( value?.schema === 'clawclaw.monitor-item.v1' && !!value.event && typeof value.event === 'object' ))); if (items.length === 0) throw new Error('mailbox_monitor_batch_empty'); const events = items.map((item) => item.event); const types = events.map((event) => String(event.type ?? 'event')); const detailsRequired = [...new Set(items .filter((item) => item.detail_required) .map((item) => String(item.event.type ?? 'event')))]; const backlog = items.find((item) => item.backlog)?.backlog; const gameMode = events.find((event) => ( event.game_mode === 'match' || event.game_mode === 'competition' || isRoomMode(event.game_mode) ))?.game_mode; return fitShortMonitorPayload(cleanObject({ events: types, game_mode: gameMode, messages: items.map((item) => item.message), details_required: detailsRequired.length > 0 ? detailsRequired : undefined, backlog, guidance: backlog ? `Monitor delegated ${backlog.remaining_events} additional Events to the durable backlog. Read them with ccl events when appropriate.` : undefined, truncated: backlog ? true : undefined, })); } /** Pack complete short facts into lossless <=500-character Monitor notifications. */ export function partitionClawclawMonitorItems( values: readonly ClawclawMonitorItemPayload[], ): ClawclawMonitorItemPayload[][] { const chunks: ClawclawMonitorItemPayload[][] = []; let current: ClawclawMonitorItemPayload[] = []; for (const value of values) { const candidate = [...current, value]; const composed = composeClawclawMonitorNotification(candidate); if (current.length > 0 && composed.truncated === true) { chunks.push(current); current = [value]; } else { current = candidate; } } if (current.length > 0) chunks.push(current); return chunks; }