import type { BehaviorDecision, FiniteBehaviorDecision, } from '../../sdk/behavior-plugin/index.js'; import { assertRuntimeModuleOutputSize } from '../framework/modules/runtime-module-output.js'; const DECISION_KEYS = new Set(['actions', 'status', 'events', 'delegate', 'finish']); const STATUS_KEYS = new Set(['summary', 'activity', 'room', 'task', 'targetPlayer', 'data']); const EVENT_KEYS = new Set(['type', 'payload', 'occurredAt']); const DELEGATE_KEYS = new Set(['behavior', 'strategy', 'input']); const FINISH_KEYS = new Set(['outcome', 'reason', 'summary']); function objectRecord(value: unknown, code: string): Record { if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error(code); return value as Record; } function nonEmptyString(value: unknown, code: string): string { if (typeof value !== 'string' || !value.trim()) throw new Error(code); return value; } function rejectUnknownFields( value: Readonly>, allowed: ReadonlySet, code: string, ): void { for (const key of Object.keys(value)) { if (!allowed.has(key)) throw new Error(`${code}:${key}`); } } /** Validates and detaches the common semantic output before it enters the ordered effect queue. */ export function normalizeBehaviorDecision( value: unknown, _options?: Readonly<{ finite?: boolean }>, ): BehaviorDecision | FiniteBehaviorDecision { if (value === undefined) return Object.freeze({}); const source = objectRecord(value, 'behavior_decision_invalid'); for (const key of Object.keys(source)) { if (!DECISION_KEYS.has(key)) throw new Error(`behavior_decision_field_unknown:${key}`); } if (source.actions !== undefined) { if (!Array.isArray(source.actions)) throw new Error('behavior_actions_invalid'); for (const action of source.actions) objectRecord(action, 'behavior_action_invalid'); } if (source.status !== undefined) { const status = objectRecord(source.status, 'behavior_status_invalid'); rejectUnknownFields(status, STATUS_KEYS, 'behavior_status_field_unknown'); nonEmptyString(status.summary, 'behavior_status_summary_invalid'); for (const field of ['activity', 'room', 'task', 'targetPlayer'] as const) { if (status[field] !== undefined) { nonEmptyString(status[field], `behavior_status_${field}_invalid`); } } if (status.data !== undefined) { objectRecord(status.data, 'behavior_status_data_invalid'); } } if (source.events !== undefined) { if (!Array.isArray(source.events)) throw new Error('behavior_events_invalid'); for (const raw of source.events) { const event = objectRecord(raw, 'behavior_event_invalid'); rejectUnknownFields(event, EVENT_KEYS, 'behavior_event_field_unknown'); nonEmptyString(event.type, 'behavior_event_type_invalid'); objectRecord(event.payload, 'behavior_event_payload_invalid'); if (event.occurredAt !== undefined && typeof event.occurredAt !== 'string') { throw new Error('behavior_event_occurred_at_invalid'); } } } let normalizedDelegate: Record | undefined; if (source.delegate !== undefined) { const delegate = objectRecord(source.delegate, 'behavior_delegate_invalid'); rejectUnknownFields(delegate, DELEGATE_KEYS, 'behavior_delegate_field_unknown'); if (delegate.behavior !== undefined && delegate.strategy !== undefined) throw new Error('strategy_delegate_name_conflict'); const target = nonEmptyString(delegate.strategy ?? delegate.behavior, 'behavior_delegate_id_invalid'); if (delegate.input !== undefined) objectRecord(delegate.input, 'behavior_delegate_input_invalid'); normalizedDelegate = { behavior: target, ...(delegate.input === undefined ? {} : { input: delegate.input }) }; } if (source.finish !== undefined) { const finish = objectRecord(source.finish, 'behavior_finish_invalid'); rejectUnknownFields(finish, FINISH_KEYS, 'behavior_finish_field_unknown'); if (finish.outcome !== 'completed' && finish.outcome !== 'failed') { throw new Error('behavior_finish_outcome_invalid'); } nonEmptyString(finish.reason, 'behavior_finish_reason_invalid'); if (finish.summary !== undefined && typeof finish.summary !== 'string') { throw new Error('behavior_finish_summary_invalid'); } } const normalized = Object.freeze(structuredClone({ ...source, ...(normalizedDelegate ? { delegate: normalizedDelegate } : {}) })) as BehaviorDecision | FiniteBehaviorDecision; assertRuntimeModuleOutputSize(normalized, 'behavior_output'); return normalized; }