import { normalizePerceptionBehaviorBindings } from './reaction-manifest.js'; import type { PerceptionModuleManifest } from '../../sdk/perception-plugin/module.js'; import { hasValidClawclawResourceLocalizations } from '../../sdk/resource-metadata.js'; import { normalizeBehaviorInputFields, normalizeBehaviorInput } from './behavior-input-fields.js'; import { isClawclawFixedEventType, CLAWCLAW_GAME_STREAM_BACKEND_EVENT_TYPES, CLAWCLAW_BACKEND_AGENT_EVENT_TYPES, CLAWCLAW_LOCAL_AGENT_EVENT_TYPES, CLAWCLAW_VISUAL_EVENT_TYPES } from '../clawclaw-event-catalog.js'; export const REMOVED_PERCEPTION_INPUT_TYPES = new Set(['players_changed', 'tasks_changed', 'meeting_state', 'ability.ready', 'ability.remaining_uses_changed', 'ability.exhausted', 'emergency_countdown_warning', 'room_ready_to_start']); const INPUT_TYPES = new Set([...CLAWCLAW_GAME_STREAM_BACKEND_EVENT_TYPES, ...CLAWCLAW_BACKEND_AGENT_EVENT_TYPES, ...CLAWCLAW_LOCAL_AGENT_EVENT_TYPES.filter(type => !/^(behavior|reminder|perception)\./.test(type)), 'tasks_changed', 'meeting_state']); const PERCEPTION_OWNED_TYPES = new Set([...CLAWCLAW_VISUAL_EVENT_TYPES, 'reminder.task', 'reminder.speech']); const record = (value: unknown): value is Record => !!value && typeof value === 'object' && !Array.isArray(value); export function normalizePerceptionModuleManifest(value: unknown, current = true, discoverRemovedInputs = false): PerceptionModuleManifest { if (!record(value) || !Number.isInteger(value.apiVersion) || current && value.apiVersion !== 3 || Object.keys(value).some(key => !['apiVersion', 'id', 'name', 'description', 'localizations', 'consumes', 'produces', 'inputs', 'config', 'behaviors', 'strategies'].includes(key)) || typeof value.id !== 'string' || !/^[a-z0-9][a-z0-9._-]*$/.test(value.id) || typeof value.description !== 'string' || !value.description.trim() || value.name !== undefined && (typeof value.name !== 'string' || !value.name.trim()) || !hasValidClawclawResourceLocalizations(value.localizations) || value.config !== undefined && !record(value.config)) throw new Error('perception_module_manifest_invalid'); const consumes = value.consumes ?? [], produces = value.produces ?? []; if (value.strategies !== undefined && value.behaviors !== undefined) throw new Error('perception_strategy_bindings_conflict'); const bindings = value.strategies ?? value.behaviors; if (!Array.isArray(produces) || produces.some(type => typeof type !== 'string' || !/^[a-z0-9][a-z0-9._-]*$/.test(type) || isClawclawFixedEventType(type) && !PERCEPTION_OWNED_TYPES.has(type)) || !Array.isArray(consumes) || consumes.some(type => typeof type !== 'string' || !INPUT_TYPES.has(type) || /^(visual|reminder|reaction|perception|behavior|decision)\./.test(type))) throw new Error('perception_module_event_contract_invalid'); if (!discoverRemovedInputs && consumes.some(type => REMOVED_PERCEPTION_INPUT_TYPES.has(type))) throw new Error('perception_input_removed: use gameState, frame or module-local taskChanges; perceptions cannot subscribe to other perceptions'); const inputs = value.inputs ? normalizeBehaviorInputFields(value.inputs) : undefined; const config = inputs ? normalizeBehaviorInput(value.config ?? {}, inputs) : structuredClone(value.config ?? {}); return { apiVersion: value.apiVersion, id: value.id, description: value.description, ...(value.name === undefined ? {} : { name: value.name }), ...(value.localizations === undefined ? {} : { localizations: structuredClone(value.localizations) }), ...(bindings === undefined ? {} : { behaviors: normalizePerceptionBehaviorBindings(bindings) }), consumes: [...new Set(consumes)], produces: [...new Set(produces)], ...(inputs ? { inputs } : {}), config }; }