import { accountRequiredMessage } from '../lib/desktop-mode.js'; import { Command } from 'commander'; import { existsSync } from 'node:fs'; import { AuthStore } from '../lib/auth.js'; import { getWorkspaceDir } from '../lib/init-command.js'; import { resolveActivePersonaFile, createPersonaPreset, listPersonaPresets, findPersonaPreset, writePersonaSelection, } from '../lib/persona.js'; import { ensureAccountUserDataLayout, getUserDataAccountPersonasDir, } from '../lib/user-data.js'; import { loadPersonaSnapshot } from '../lib/load-context.js'; import { discoverLocalCapabilities } from '../lib/capability-discovery.js'; import { readCapabilityWorkspaceRegistry, writeCapabilityWorkspaceRegistry, capabilityEntryForPath, capabilitySelectionRef, capabilityWorkspacePath } from '../lib/capability-workspace.js'; import { selectCapability } from '../lib/capability-selection.js'; import { HubClient } from '../lib/hub-client.js'; import { readLocalAgentWorkspaceSnapshot } from '../lib/agent-workspace-sync.js'; import { renameCapabilityOperationWhenIdle } from '../lib/capability-operation-rename.js'; function context() { const profile = new AuthStore().getActive(); if (!profile) throw new Error(accountRequiredMessage('Not logged in.')); const workspaceDir = getWorkspaceDir(); ensureAccountUserDataLayout(workspaceDir, profile); return { profile, workspaceDir, directory: getUserDataAccountPersonasDir(workspaceDir, profile), }; } export function createPersonaCommand(): Command { const persona = new Command('persona'); persona .alias('prs') .description('Per-account roleplay persona notes'); persona .command('rename ') .description('Rename one equipped Persona operation for the active account') .action(async (currentId: string, newId: string) => { const { workspaceDir, profile } = context(); console.log(JSON.stringify(await renameCapabilityOperationWhenIdle( workspaceDir, profile, 'persona', currentId, newId, ), null, 2)); }); persona .command('list') .description('List local persona presets for the active account') .action(() => { const { workspaceDir, profile } = context(); discoverLocalCapabilities(workspaceDir, profile); resolveActivePersonaFile(); const presets = Object.values(readCapabilityWorkspaceRegistry(workspaceDir, profile).entries) .filter(entry => entry.type === 'persona' && existsSync(capabilityWorkspacePath(workspaceDir, profile, entry))) .map(entry => ({ id: entry.runtimeId, selection_ref: capabilitySelectionRef(entry), name: entry.title, declared_id: entry.declaredId ?? entry.runtimeId, source: entry.hub ? 'hub' : 'local', resource_id: entry.hub?.resourceId, owner_id: entry.hub?.ownerId, owner_name: entry.hub?.ownerName, path: capabilityWorkspacePath(workspaceDir, profile, entry), selection: entry.selection })); console.log(JSON.stringify({ presets }, null, 2)); }); persona .command('path') .description('Print the absolute persona file path for the active account') .action(() => { console.log(JSON.stringify(resolveActivePersonaFile(), null, 2)); }); persona .command('load') .description('Read the active account persona file') .action(() => { console.log(JSON.stringify(loadPersonaSnapshot(), null, 2)); }); persona .command('create ') .description('Create and select an empty account-local Persona preset') .action(async (name: string) => { const { directory, workspaceDir, profile } = context(); const created = createPersonaPreset(name, directory); discoverLocalCapabilities(workspaceDir, profile); const discovered = capabilityEntryForPath(workspaceDir, profile, 'persona', created.path)!; const entry = await selectCapability(workspaceDir, profile, 'persona', capabilitySelectionRef(discovered).slice('persona/'.length), true); writePersonaSelection(workspaceDir, profile, { source: 'local', id: entry.runtimeId }); console.log(JSON.stringify({ path: created.path, preset: { id: entry.runtimeId, name: created.name, source: 'local' }, }, null, 2)); }); persona .command('use ') .description('Select an account-local Persona preset') .action(async (presetName: string) => { const { directory, workspaceDir, profile } = context(); discoverLocalCapabilities(workspaceDir, profile); resolveActivePersonaFile(); const named = Object.values(readCapabilityWorkspaceRegistry(workspaceDir, profile).entries) .filter(entry => entry.type === 'persona' && (entry.runtimeId === presetName || capabilitySelectionRef(entry) === `persona/${presetName}`)); if (named.length > 1) throw new Error(`persona_selection_ambiguous:${presetName}:use_selection_ref`); if (named.length === 1) { const entry = await selectCapability(workspaceDir, profile, 'persona', capabilitySelectionRef(named[0]!).slice('persona/'.length), true); writePersonaSelection(workspaceDir, profile, entry.hub ? { source: 'hub' } : { source: 'local', id: entry.runtimeId }); console.log(JSON.stringify({ path: capabilityWorkspacePath(workspaceDir, profile, entry), preset: { id: entry.runtimeId, name: entry.title, source: entry.hub ? 'hub' : 'local' } }, null, 2)); return; } const found = findPersonaPreset(presetName, directory); if (!found) { const available = listPersonaPresets(directory).map(p => p.name).join(', ') || 'none'; throw new Error(`Persona preset "${presetName}" not found. Available: ${available}`); } discoverLocalCapabilities(workspaceDir, profile); const discovered = capabilityEntryForPath(workspaceDir, profile, 'persona', found.path)!; const entry = await selectCapability(workspaceDir, profile, 'persona', capabilitySelectionRef(discovered).slice('persona/'.length), true); writePersonaSelection(workspaceDir, profile, entry.hub ? { source: 'hub' } : { source: 'local', id: entry.runtimeId }); console.log(JSON.stringify({ path: found.path, preset: { id: entry.runtimeId, name: found.name, source: 'local' }, message: `Persona "${found.name}" (local) selected for the active account.`, }, null, 2)); }); persona .command('use-hub') .description('Use the Persona selected by the active account Hub loadout') .action(() => { const { workspaceDir, profile } = context(); const registry = readCapabilityWorkspaceRegistry(workspaceDir, profile); for (const entry of Object.values(registry.entries)) { if (entry.type === 'persona' && !entry.hub && entry.selection === 'equipped') entry.selection = 'unequipped'; } writeCapabilityWorkspaceRegistry(workspaceDir, profile, registry); writePersonaSelection(workspaceDir, profile, { source: 'hub' }); console.log(JSON.stringify(resolveActivePersonaFile(), null, 2)); }); persona .command('clear') .description('Use no Persona for the active account') .action(async () => { const { workspaceDir, profile } = context(); const registry = readCapabilityWorkspaceRegistry(workspaceDir, profile); if (Object.values(registry.entries).some(entry => entry.type === 'persona' && entry.hub) || readLocalAgentWorkspaceSnapshot(workspaceDir, profile)?.items.persona.length) { await HubClient.fromProfile(profile).updateWorkspaceLoadout({ personaItemIds: [] }); } for (const entry of Object.values(registry.entries)) { if (entry.type === 'persona') entry.selection = 'unequipped'; } writeCapabilityWorkspaceRegistry(workspaceDir, profile, registry); writePersonaSelection(workspaceDir, profile, { source: 'none' }); console.log(JSON.stringify({ path: null, source: 'none' }, null, 2)); }); return persona; }