import type { AuthProfile } from './auth.js'; import { HubClient } from './hub-client.js'; import { reservedSkillName } from './package-skills.js'; import { CommandOutputError } from './command-error.js'; import { assignCapabilityOperationName, capabilitySelectionRef, readCapabilityWorkspaceRegistry, writeCapabilityWorkspaceRegistry, type CapabilityWorkspaceType } from './capability-workspace.js'; import { updateGameplayConfig } from './gameplay-config.js'; import { skillNameCandidate, skillNameConflicts, throwSkillNameConflict } from './skill-name-conflicts.js'; export type EquipmentClient = Pick; /** One selection path for all currently exposed Agent commands. Cloud failures * leave local selection untouched; a confirmed cloud choice is not rolled back * using a possibly stale local snapshot. */ export async function selectCapability( workspaceDir: string, profile: Pick, type: CapabilityWorkspaceType, id: string, equipped: boolean, providedClient?: EquipmentClient, ) { const registry = readCapabilityWorkspaceRegistry(workspaceDir, profile); const matches = Object.values(registry.entries).filter(entry => entry.type === type && ( id.startsWith('@') ? capabilitySelectionRef(entry) === `${type}/${id}` : entry.runtimeId === id )); if (matches.length !== 1) throw new Error(`capability_selection_${matches.length ? 'ambiguous' : 'not_found'}:${type}:${id}`); const entry = matches[0]!; if (type === 'skill' && equipped) { const skill = skillNameCandidate(workspaceDir, profile, entry); const reserved = reservedSkillName(entry.runtimeId) ?? reservedSkillName(skill.name, skill.working_copy_path); if (reserved) throw new CommandOutputError(`reserved_skill_name:${reserved}`, { error: 'reserved_skill_name', name: reserved, path: skill.working_copy_path, retryable: false, }); const candidates = Object.values(registry.entries).filter(other => other.type === 'skill' && (other.key === entry.key || (other.selection === 'equipped' && !other.hub?.cloudStatus))); const conflict = skillNameConflicts(candidates.map(other => skillNameCandidate(workspaceDir, profile, other))) .find(item => item.candidates.some(candidate => candidate.selection_ref === capabilitySelectionRef(entry))); if (conflict) throwSkillNameConflict(conflict); } if (entry.hub) { const client = providedClient ?? HubClient.fromProfile(profile); if (entry.hub.cloudStatus) throw new Error(`capability_workspace_removed:${type}/${id}`); let itemId = entry.hub.workspaceItemId; if (!itemId) { const item = await client.findWorkspaceItem(type === 'behavior' ? 'strategy' : type, entry.hub.resourceId); if (!item) throw new Error(`capability_cloud_workspace_item_missing:${type}/${id}`); itemId = item.workspaceItemId; } if (type === 'persona' && equipped) { await client.updateWorkspaceLoadout({ personaItemIds: [itemId] }); } else { await client.updateWorkspaceEquipment(itemId, equipped); } entry.hub.workspaceItemId = itemId; delete entry.hub.pendingEquipment; } if (type === 'persona' && equipped) { for (const other of Object.values(registry.entries)) { if (other.type === 'persona' && other.key !== entry.key && other.selection === 'equipped') { other.selection = 'unequipped'; } } } if (equipped) assignCapabilityOperationName(registry, entry); entry.selection = equipped ? 'equipped' : 'unequipped'; entry.updatedAt = new Date().toISOString(); writeCapabilityWorkspaceRegistry(workspaceDir, profile, registry); if (type === 'behavior' || type === 'perception') { updateGameplayConfig(config => { const field = type === 'behavior' ? 'disabled_behaviors' : 'disabled_perception_modules'; config[field] = config[field].filter(value => value !== entry.runtimeId && value !== id); if (!equipped && !entry.hub) config[field].push(entry.runtimeId); }, workspaceDir, profile); } return entry; }