import type { ClawclawReactionManifest, ReactionBehaviorBinding } from '../../sdk/perception-plugin/reaction.js'; import { hasValidClawclawResourceLocalizations } from '../../sdk/resource-metadata.js'; import { normalizeBehaviorInput, normalizeBehaviorInputFields } from './behavior-input-fields.js'; import { isClawclawFixedEventType } from '../clawclaw-event-catalog.js'; const ID = /^[a-z0-9][a-z0-9._-]*$/; const OPERATION = /^[A-Za-z0-9][A-Za-z0-9._-]{0,255}$/; const record = (value: unknown): value is Record => !!value && typeof value === 'object' && !Array.isArray(value); const integer = (value: unknown, minimum: number, maximum: number) => Number.isSafeInteger(value) && Number(value) >= minimum && Number(value) <= maximum; export function normalizeReactionManifest(value: unknown, requireCurrentVersion = true): ClawclawReactionManifest { const m = value as ClawclawReactionManifest; if (!record(value) || m.kind !== 'reaction' || !Number.isInteger(m.apiVersion) || requireCurrentVersion && m.apiVersion !== 2 || typeof m.id !== 'string' || !ID.test(m.id) || typeof m.description !== 'string' || !m.description.trim() || m.name !== undefined && (typeof m.name !== 'string' || !m.name.trim()) || !hasValidClawclawResourceLocalizations(m.localizations) || !Array.isArray(m.consumes) || m.consumes.some(type => typeof type !== 'string' || !ID.test(type) || /^(reaction|perception|behavior|decision)\./.test(type)) || !Array.isArray(m.produces) || m.produces.some(type => typeof type !== 'string' || !/^reaction\.[a-z0-9][a-z0-9._-]*$/.test(type) || isClawclawFixedEventType(type)) || m.behaviors !== undefined && !record(m.behaviors) || m.config !== undefined && !record(m.config) || Object.keys(m).some(key => !['apiVersion', 'kind', 'id', 'name', 'description', 'localizations', 'consumes', 'produces', 'behaviors', 'inputs', 'config'].includes(key))) throw new Error('reaction_manifest_invalid'); const behaviors = normalizePerceptionBehaviorBindings(m.behaviors); const inputs = m.inputs ? normalizeBehaviorInputFields(m.inputs) : undefined; const config = inputs ? normalizeBehaviorInput(m.config ?? {}, inputs) : structuredClone(m.config ?? {}); return { apiVersion: m.apiVersion, kind: 'reaction', id: m.id, description: m.description, ...(m.name === undefined ? {} : { name: m.name }), ...(m.localizations === undefined ? {} : { localizations: structuredClone(m.localizations) }), consumes: [...new Set(m.consumes)], produces: [...new Set(m.produces)], behaviors, ...(inputs ? { inputs } : {}), config }; } export function normalizePerceptionBehaviorBindings(value: unknown): Record { if (value !== undefined && !record(value)) throw Error("perception_behaviors_invalid"); const behaviors: Record = {}; if (Object.keys(value ?? {}).length > 16) throw new Error('reaction_behavior_limit'); for (const [key, binding] of Object.entries(value ?? {})) { if (!ID.test(key) || ['__proto__', 'constructor', 'prototype'].includes(key) || !record(binding) || typeof binding.name !== 'string' || !OPERATION.test(binding.name) || binding.resourceId !== undefined && (typeof binding.resourceId !== 'string' || !binding.resourceId.trim() || binding.resourceId.length > 255) || binding.layer !== undefined && binding.layer !== 'reflex' || binding.input !== undefined && !record(binding.input) || binding.reflexPriority !== undefined && !integer(binding.reflexPriority, -1_000_000, 1_000_000) || binding.cooldownMs !== undefined && !integer(binding.cooldownMs, 0, 86_400_000) || binding.maxRuntimeMs !== undefined && !integer(binding.maxRuntimeMs, 1, 120_000) || binding.rearm !== undefined && !['after_clear', 'after_cooldown'].includes(String(binding.rearm)) || Object.keys(binding).some(field => !['name', 'resourceId', 'layer', 'input', 'reflexPriority', 'cooldownMs', 'maxRuntimeMs', 'rearm'].includes(field))) throw new Error(`reaction_behavior_invalid:${key}`); behaviors[key] = structuredClone(binding) as unknown as ReactionBehaviorBinding; } return behaviors; }