import { normalizePerceptionModuleManifest } from './perception-module-manifest.js'; import type { ResolvedClawclawBehaviorSource } from './plugins/behavior/behavior-source.js'; import type { ResolvedClawclawPerceptionSource } from './plugins/perception/perception-source.js'; type TriggerDeclaration = Pick; export function strategyActivationMode(source: TriggerDeclaration) { return source.trigger || source.triggerError ? 'automatic_only' : 'standard'; } export function strategyActivationError(source: ResolvedClawclawBehaviorSource, request?: Readonly<{ source: string; triggerId: string }>): string | undefined { if (source.triggerError) return source.triggerError; if (!source.trigger) return undefined; if (request?.source === 'perception' && Object.keys(source.trigger.manifest.behaviors ?? {}).some( key => request.triggerId === `perception:${strategyPerceptionId(source.id)}:${key}`, )) return undefined; return 'strategy_automatic_only'; } function strategyPerceptionId(strategyId: string): string { return 'strategy:' + strategyId; } export function normalizeStrategyPerception(value: unknown, strategyId: string) { const manifest = normalizePerceptionModuleManifest(value); const bindings = Object.values(manifest.behaviors ?? {}); if (!bindings.length || bindings.some(binding => binding.name !== strategyId || binding.resourceId !== undefined)) { throw Error('strategy_perception_self_binding_required'); } return manifest; } /** Route a named perception through the same host as an equipped Hub perception. */ export function strategyTriggerSource(source: ResolvedClawclawBehaviorSource): ResolvedClawclawPerceptionSource | undefined { if (!source.trigger) return undefined; const { manifest, sha256 } = source.trigger; const id = strategyPerceptionId(source.id); const name = manifest.name ?? manifest.id; const behaviors = Object.fromEntries(Object.entries(manifest.behaviors!).map(([key, binding]) => [key, { ...binding, name: source.id }])); return { id, name, description: manifest.description, apiVersion: 3, sourcePath: source.sourcePath, sourceRoot: source.sourceRoot, embedded: { strategyId: source.id, sha256, declaredManifest: manifest }, module: { ...manifest, behaviors }, manifest: { apiVersion: 2, id, name, description: manifest.description, subjectScope: 'self_and_others', produces: manifest.produces ?? [] } }; }