import { BEHAVIOR_PLUGIN_API_VERSION } from '../../../../sdk/behavior-plugin/index.js'; import { pluginApiVersionDiagnostic } from '../plugin-api-version.js'; import { loadClawclawBehaviorModule, type LoadedClawclawBehaviorModule, } from './behavior-module-loader.js'; import { type PreparedClawclawBehavior, } from './behavior-normalization.js'; import { behaviorSourceCompatibilityError, type ResolvedClawclawBehaviorSource, } from './behavior-source.js'; import { prepareBehaviorInWorker } from './thread-behavior-executor.js'; import { deepFreezeBehaviorValue } from './behavior-manifest.js'; import { strategyActivationError, strategyActivationMode } from '../../behavior-trigger.js'; import { normalizeBehaviorInput } from '../../behavior-input-fields.js'; export interface ClawclawBehaviorCatalogEntry { readonly apiVersion: number | null; readonly expectedApiVersion: typeof BEHAVIOR_PLUGIN_API_VERSION; readonly id: string; readonly declaredId?: string; readonly resourceId?: string; readonly name: string; readonly description: string; readonly localizations?: ResolvedClawclawBehaviorSource['localizations']; readonly inputs: ResolvedClawclawBehaviorSource['inputs']; readonly execution: ResolvedClawclawBehaviorSource['execution']; readonly stateMode?: ResolvedClawclawBehaviorSource['stateMode']; readonly everyTicks: number; readonly produces?: readonly string[]; readonly runtime?: ResolvedClawclawBehaviorSource['runtime']; readonly provenance?: ResolvedClawclawBehaviorSource['provenance']; readonly activationMode?: ReturnType; readonly loadable: boolean; readonly compatibilityError?: string; } function catalogEntry(source: ResolvedClawclawBehaviorSource): ClawclawBehaviorCatalogEntry { const apiError = source.apiVersion === BEHAVIOR_PLUGIN_API_VERSION ? undefined : pluginApiVersionDiagnostic( 'behavior_api_version_unsupported', BEHAVIOR_PLUGIN_API_VERSION, source.apiVersion, ); const compatibilityError = apiError ?? behaviorSourceCompatibilityError(source); return structuredClone({ apiVersion: source.apiVersion ?? null, expectedApiVersion: BEHAVIOR_PLUGIN_API_VERSION, id: source.id, ...(source.declaredId === undefined ? {} : { declaredId: source.declaredId }), ...(source.resourceId === undefined ? {} : { resourceId: source.resourceId }), name: source.name, description: source.description, ...(source.localizations === undefined ? {} : { localizations: source.localizations }), inputs: source.inputs, execution: source.execution, ...(source.stateMode === undefined ? {} : { stateMode: source.stateMode }), everyTicks: source.everyTicks, ...(source.produces === undefined ? {} : { produces: source.produces }), ...(source.runtime === undefined ? {} : { runtime: source.runtime }), ...(source.provenance === undefined ? {} : { provenance: source.provenance }), activationMode: strategyActivationMode(source), loadable: compatibilityError === undefined, ...(compatibilityError === undefined ? {} : { compatibilityError }), }); } /** A static catalog: discovered modules are imported only by load()/prepare(). */ export class ClawclawBehaviorCatalog { private readonly sources = new Map(); private readonly loaded = new Map>(); constructor(sources: readonly ResolvedClawclawBehaviorSource[]) { for (const source of sources) { if (this.sources.has(source.id)) { throw new Error(`Duplicate behavior module id '${source.id}'.`); } this.sources.set(source.id, source); } } list(): readonly ClawclawBehaviorCatalogEntry[] { return [...this.sources.values()].map(catalogEntry); } has(id: string): boolean { return this.sources.has(id); } activationError(id: string, request?: Readonly<{ source: string; triggerId: string }>): string | undefined { const source = this.sources.get(id); return source && strategyActivationError(source, request); } load(id: string): Promise { const source = this.sources.get(id); if (!source) return Promise.reject(new Error(`Unknown behavior module '${id}'.`)); let loading = this.loaded.get(id); if (!loading) { loading = loadClawclawBehaviorModule(source); this.loaded.set(id, loading); } return loading; } prepareForActivation(id: string, input: unknown, diagnosticsEnabled = false): Promise { return this.prepare(id, input, true, diagnosticsEnabled); } async prepare(id: string, input: unknown, retainWorker = false, diagnosticsEnabled = false): Promise { const source = this.sources.get(id); if (!source) throw new Error(`Unknown behavior module '${id}'.`); const rawInput = deepFreezeBehaviorValue(normalizeBehaviorInput(input, source.inputs, { allowedKeys: ['timeout_ms', 'duration_ms'] })); const prepared = await prepareBehaviorInWorker(source, input as Readonly>, { retainWorker, diagnosticsEnabled, }); return Object.freeze({ manifest: deepFreezeBehaviorValue(structuredClone(prepared.manifest)), input: deepFreezeBehaviorValue(structuredClone(prepared.input)), rawInput, ...(prepared.worker ? { worker: prepared.worker } : {}), source: prepared.source, display: source.display, ...(prepared.timing === undefined ? {} : { timing: prepared.timing }), }); } }