import { REMOVED_PERCEPTION_INPUT_TYPES } from './perception-module-manifest.js'; import { REMINDER_PLUGIN_API_VERSION, type ClawclawReminderManifest } from '../../sdk/perception-plugin/reminder.js'; import { REMINDER_FACT_TYPES_BY_TOPIC, REMINDER_TOPICS } from '../../sdk/perception-plugin/reminder-rules.js'; import { hasValidClawclawResourceLocalizations } from '../../sdk/resource-metadata.js'; import { normalizeBehaviorInput, normalizeBehaviorInputFields } from './behavior-input-fields.js'; /** Used by both static Perception discovery and the isolated runtime loader. */ export function normalizeReminderManifest(value: unknown, requireCurrentVersion = true, discoverRemovedInputs = false): ClawclawReminderManifest { if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('Invalid perception reminder manifest.'); const m = value as ClawclawReminderManifest; if (m.kind !== 'reminder' || !Number.isInteger(m.apiVersion) || (requireCurrentVersion && m.apiVersion !== REMINDER_PLUGIN_API_VERSION) || typeof m.id !== 'string' || !/^[a-z0-9][a-z0-9._-]*$/.test(m.id) || typeof m.description !== 'string' || !m.description.trim() || (m.name !== undefined && (typeof m.name !== 'string' || !m.name.trim())) || !hasValidClawclawResourceLocalizations(m.localizations) || !REMINDER_TOPICS.includes(m.topic) || !Array.isArray(m.consumes) || new Set(m.consumes).size !== m.consumes.length || m.consumes.some(type => !(REMINDER_FACT_TYPES_BY_TOPIC[m.topic] as readonly string[]).includes(type) && !(discoverRemovedInputs && REMOVED_PERCEPTION_INPUT_TYPES.has(type))) || (m.config !== undefined && (!m.config || typeof m.config !== 'object' || Array.isArray(m.config))) || Object.keys(m).some(key => !['apiVersion', 'kind', 'id', 'name', 'description', 'localizations', 'topic', 'consumes', 'inputs', 'config'].includes(key))) throw new Error('Invalid perception reminder manifest.'); if (!discoverRemovedInputs && m.consumes.some(type => REMOVED_PERCEPTION_INPUT_TYPES.has(type))) throw new Error('perception_input_removed: use gameState or module-local taskChanges'); const inputs = m.inputs === undefined ? undefined : normalizeBehaviorInputFields(m.inputs); const config = inputs ? normalizeBehaviorInput(m.config ?? {}, inputs) : structuredClone(m.config ?? {}); return { apiVersion: m.apiVersion, kind: 'reminder', id: m.id, description: m.description, ...(m.name === undefined ? {} : { name: m.name }), ...(m.localizations === undefined ? {} : { localizations: structuredClone(m.localizations) }), topic: m.topic, consumes: [...m.consumes], ...(inputs === undefined ? {} : { inputs }), config }; }