import { accountRequiredMessage } from '../lib/desktop-mode.js'; import { BUILTIN_PERCEPTION_NAMES } from '../runtime/clawclaw/plugins/system-perception/system-perceptions.js'; import { BUILTIN_PERCEPTION_PREFIX } from '../runtime/clawclaw/notification-policy.js'; import { runOwnerCommand, parseModuleInput, type ModuleRuntimeCommandDependencies } from './module-runtime-control.js'; import { Command } from 'commander'; import { AuthStore, type AuthProfile } from '../lib/auth.js'; import { getWorkspaceDir } from '../lib/init-command.js'; import { readGameplayConfig, updateGameplayConfig, } from '../lib/gameplay-config.js'; import { ensureAccountUserDataLayout, getUserDataAccountGameplayConfigFile, getUserDataAccountPerceptionsDir, } from '../lib/user-data.js'; import { readLocalAgentWorkspaceSnapshot } from '../lib/agent-workspace-sync.js'; import { filterInactiveAgentWorkspaceSnapshot } from '../lib/capability-cloud-presence.js'; import { listAgentWorkspacePerceptionSources, mergeAgentWorkspacePerceptionSources, } from '../lib/agent-workspace-runtime.js'; import { expectedPerceptionApiVersion } from '../runtime/clawclaw/plugins/perception/perception-source.js'; import { pluginApiVersionStatus } from '../runtime/clawclaw/plugins/plugin-api-version.js'; import { discoverLocalCapabilities } from '../lib/capability-discovery.js'; import { capabilityEntryForPath, readCapabilityWorkspaceRegistry } from '../lib/capability-workspace.js'; import { selectCapability } from '../lib/capability-selection.js'; import { renameCapabilityOperationWhenIdle } from '../lib/capability-operation-rename.js'; export interface PerceptionCommandDependencies extends ModuleRuntimeCommandDependencies {} function activeProfile(dependencies: PerceptionCommandDependencies): AuthProfile { const profile = (dependencies.activeProfile ?? (() => new AuthStore().getActive()))(); if (!profile) throw new Error(accountRequiredMessage('No active account. Run `ccl account register` or switch accounts first.')); return profile; } export function createPerceptionCommand(dependencies: PerceptionCommandDependencies = {}): Command { const cmd = new Command('perception') .alias('pcp') .description('Inspect, enable, or disable perception modules') .option('--list', 'List installed perception modules') .option('--disable ', 'Disable a local perception module on the next runtime') .option('--enable ', 'Enable a local perception module on the next runtime') .addHelpText('after', () => { const profile = (dependencies.activeProfile ?? (() => new AuthStore().getActive()))(); const directory = profile ? getUserDataAccountPerceptionsDir(getWorkspaceDir(), profile) : '/capabilities/perceptions'; return ` Perception modules: Place custom .ts or .js modules in ${directory} Local modules require one persisted ccl capability equip/unequip choice. Hub module enablement is controlled by the Hub loadout. `; }) .action(async (opts: { list?: boolean; disable?: string; enable?: string }) => { if (opts.disable && opts.enable) { throw new Error('Use only one of --disable or --enable at a time.'); } const workspaceDir = getWorkspaceDir(); const profile = activeProfile(dependencies); ensureAccountUserDataLayout(workspaceDir, profile); const localCapabilities = discoverLocalCapabilities(workspaceDir, profile).perceptions; const snapshot = filterInactiveAgentWorkspaceSnapshot( workspaceDir, profile, readLocalAgentWorkspaceSnapshot(workspaceDir, profile), ); const hub = snapshot ? listAgentWorkspacePerceptionSources(workspaceDir, profile, snapshot) : []; const hubIds = new Set(hub.map(({ id }) => id)); const local = localCapabilities .filter(({ entry }) => !snapshot?.items.perception.some(item => item.resourceId === entry.hub?.resourceId)) .map(({ source }) => source); const registered = (id: string) => Object.values(readCapabilityWorkspaceRegistry(workspaceDir, profile).entries) .some(entry => entry.type === 'perception' && entry.runtimeId === id); if (opts.disable) { const source = local.find(({ id }) => id === opts.disable); if (!source && !hubIds.has(opts.disable) && !registered(opts.disable)) throw new Error(`Unknown local Perception '${opts.disable}'.`); await selectCapability(workspaceDir, profile, 'perception', opts.disable, false); const updated = readGameplayConfig(workspaceDir, profile); console.log(JSON.stringify({ perception_module: opts.disable, disabled: true, effective: 'next_runtime', config_file: getUserDataAccountGameplayConfigFile(workspaceDir, profile), disabled_perception_modules: updated.disabled_perception_modules, }, null, 2)); return; } if (opts.enable) { const source = local.find(({ id }) => id === opts.enable); if (!source && !hubIds.has(opts.enable) && !registered(opts.enable)) throw new Error(`Unknown local Perception '${opts.enable}'.`); await selectCapability(workspaceDir, profile, 'perception', opts.enable, true); const updated = readGameplayConfig(workspaceDir, profile); console.log(JSON.stringify({ perception_module: opts.enable, disabled: false, effective: 'next_runtime', config_file: getUserDataAccountGameplayConfigFile(workspaceDir, profile), disabled_perception_modules: updated.disabled_perception_modules, }, null, 2)); return; } if (opts.list) { const config = readGameplayConfig(workspaceDir, profile); const disabled = new Set(config.disabled_perception_modules); const installed = mergeAgentWorkspacePerceptionSources(local, hub).map((source) => ({ id: source.id, declared_id: source.declaredId ?? source.id, working_copy_path: source.workingCopyPath ?? source.sourcePath, runtime_source_path: source.sourcePath, name: source.name, description: source.description, ...(source.module?.behaviors ? { behaviors: source.module.behaviors } : {}), source: hubIds.has(source.id) ? 'hub' : 'local', ...pluginApiVersionStatus( 'perception', expectedPerceptionApiVersion(source), source.apiVersion, ), ...(source.compatibilityError ? { loadable: false, compatibility_error: source.compatibilityError } : {}), disabled: hubIds.has(source.id) ? false : disabled.has(source.id) || capabilityEntryForPath( workspaceDir, profile, 'perception', source.sourcePath, )?.selection !== 'equipped', })); console.log(JSON.stringify({ perception_modules: installed, builtin_perceptions: Object.entries(BUILTIN_PERCEPTION_NAMES).map(([id, name]) => ({ id: `${BUILTIN_PERCEPTION_PREFIX}${id}`, name, mandatory: true, configurable: false })), ignored_disabled_ids: config.disabled_perception_modules .filter((id) => !local.some((module) => module.id === id)), }, null, 2)); return; } cmd.help(); }); cmd.addCommand(new Command('update') .description('Patch an equipped perception configuration in the current game; observing and notifying continue') .argument('') .requiredOption('--input ', 'Configuration patch as a JSON object', parseModuleInput) .allowExcessArguments(false) .action(async (id: string, opts: { input: Record }) => { await runOwnerCommand('ccl perception update','perception_update',{ perception: id, input: opts.input },true,dependencies); })); cmd.addCommand(new Command('status') .description('Inspect current perception configuration, observation health and strategy bindings') .argument('[perception-id]') .allowExcessArguments(false) .action(async (id?: string) => { await runOwnerCommand('ccl perception status','perception_status',id ? { perception: id } : {},false,dependencies); })); cmd.addCommand(new Command('rename') .description('Rename one equipped Perception operation for the active account') .argument('', 'Current CCL operation name') .argument('', 'New account-local operation name') .allowExcessArguments(false) .action(async (currentId: string, newId: string) => { console.log(JSON.stringify(await renameCapabilityOperationWhenIdle( getWorkspaceDir(), activeProfile(dependencies), 'perception', currentId, newId, ), null, 2)); })); return cmd; }