import { existsSync, readFileSync } from 'fs'; import { dirname } from 'path'; import { getWorkspaceConfigFile } from './init-command.js'; import { readGameplayConfig } from './gameplay-config.js'; import { CLAWCLAW_VISUAL_EVENT_TYPES, } from '../runtime/clawclaw-event-catalog.js'; import { DEFAULT_PERCEPTION_MODULES, listClawclawPerceptionSources, type ClawclawPerceptionConfig, } from '../runtime/clawclaw/plugins/perception/index.js'; import type { ClawclawEventDelivery } from '../runtime/clawclaw-event-catalog.js'; import type { EventDeliveryClass } from '../runtime/framework/events/index.js'; export interface WorkspacePerceptionConfig { perception: ClawclawPerceptionConfig; events: { delivery: Readonly>> }; } const DELIVERY = new Set([ 'monitor_and_events', 'internal_only', ]); function record(value: unknown): Record | null { return value && typeof value === 'object' && !Array.isArray(value) ? value as Record : null; } export function readWorkspacePerceptionConfig( configPath = getWorkspaceConfigFile(), ): WorkspacePerceptionConfig { const root = existsSync(configPath) ? record(JSON.parse(readFileSync(configPath, 'utf8'))) : {}; if (!root) throw new Error('Workspace config.json must contain a JSON object.'); const perception = record(root.perception); const configuredModules = perception?.modules; if (configuredModules !== undefined) { throw new Error( 'config.json perception.modules is no longer supported; use `ccl perception --disable/--enable`.', ); } const configuredMaxBackfillVisualFrames = perception?.maxBackfillVisualFrames; const workspaceDir = dirname(configPath); const gameplay = readGameplayConfig(workspaceDir); const disabledModules = new Set(gameplay.disabled_perception_modules); const sources = listClawclawPerceptionSources(workspaceDir); const sourceIds = new Set(sources.map((source) => source.id)); const officialIds = new Set(DEFAULT_PERCEPTION_MODULES); const modules = [ ...DEFAULT_PERCEPTION_MODULES.filter((id) => ( sourceIds.has(id) && !disabledModules.has(id) )), ...sources .filter((source) => ( !officialIds.has(source.id) && !disabledModules.has(source.id) )) .map((source) => source.id), ]; const eventTypes = new Set([ ...CLAWCLAW_VISUAL_EVENT_TYPES, ...sources.filter(source => !source.reminder).flatMap((source) => [...source.manifest.produces]), ]); let maxBackfillVisualFrames: number | undefined; if (configuredMaxBackfillVisualFrames !== undefined) { if ( !Number.isInteger(configuredMaxBackfillVisualFrames) || Number(configuredMaxBackfillVisualFrames) <= 0 || Number(configuredMaxBackfillVisualFrames) > 10 ) { throw new Error('config.json perception.maxBackfillVisualFrames must be an integer between 1 and 10.'); } maxBackfillVisualFrames = Number(configuredMaxBackfillVisualFrames); } const events = record(root.events); const configuredDelivery = events?.delivery; const delivery: Partial> = {}; if (configuredDelivery !== undefined) { const values = record(configuredDelivery); if (!values) throw new Error('config.json events.delivery must be an object.'); for (const [type, value] of Object.entries(values)) { if (!eventTypes.has(type)) throw new Error(`Unknown visual Event delivery type: ${type}`); // Normalize old configuration at the boundary; no query-only mode enters Runtime. if (value === 'events_only') { delivery[type] = { deliveryClass: 'monitor_and_events', wakeAgent: false }; } else if (typeof value === 'string' && DELIVERY.has(value as EventDeliveryClass)) { delivery[type] = value as EventDeliveryClass; } else { const policy = record(value); if (!policy || !DELIVERY.has(policy.deliveryClass as EventDeliveryClass) || Object.keys(policy).some(key => key !== 'deliveryClass' && key !== 'wakeAgent') || Object.hasOwn(policy, 'wakeAgent') && typeof policy.wakeAgent !== 'boolean' || policy.deliveryClass === 'internal_only' && policy.wakeAgent === true) { throw new Error(`Invalid delivery policy for ${type}. Use deliveryClass and optional boolean wakeAgent.`); } delivery[type] = { deliveryClass: policy.deliveryClass as EventDeliveryClass, ...(policy.wakeAgent === undefined ? {} : { wakeAgent: policy.wakeAgent as boolean }) }; } } } return { perception: { modules: [...new Set(modules)], ...(maxBackfillVisualFrames === undefined ? {} : { maxBackfillVisualFrames }), }, events: { delivery }, }; }