import { Command } from 'commander'; import { ApiError, formatApiError } from '../lib/http-transport.js'; import { hubErrorFields } from '../lib/hub-errors.js'; import { HubClient, parseResourceRef } from '../lib/hub-client.js'; import type { ListOptions, ResourceType } from '../lib/hub-client.js'; export interface HubCommandDeps { clientFactory?: () => HubClient; } function getClient(deps: HubCommandDeps): HubClient { return (deps.clientFactory ?? (() => HubClient.fromAuth()))(); } function fail(payload: Record): never { console.error(JSON.stringify(payload, null, 2)); process.exit(1); } function handleError(err: unknown): never { if (err instanceof ApiError) { fail({ error: 'api_error', ...(hubErrorFields(err) ?? { status: err.status, message: formatApiError(err) }) }); } fail({ error: 'hub_error', message: (err as any)?.message ?? String(err) }); } function typeOption(value: string): ResourceType { if (!['skill', 'strategy', 'persona', 'perception'].includes(value)) { throw new Error('type must be skill, strategy, persona, or perception'); } return value as ResourceType; } export function createHubCommand(deps: HubCommandDeps = {}): Command { const hub = new Command('hub') .description('Browse Hub resources and manage the active account workspace'); hub.command('search [query]') .description('Search or browse public Hub resources') .option('--type ', 'skill | strategy | persona | perception', typeOption) .option('--sort ', 'latest | likes | comments', 'latest') .option('--limit ', 'max results', '20') .option('--cursor ', 'cursor from the previous response') .option('--json', 'machine-readable output') .action(async (query: string | undefined, opts: any) => { try { const search: ListOptions = { q: query, type: opts.type, sort: opts.sort, limit: Number(opts.limit), cursor: opts.cursor, }; const result = await getClient(deps).list(search); if (opts.json) { console.log(JSON.stringify(result, null, 2)); return; } if (result.items.length === 0) { console.log('No Hub resources found.'); return; } for (const item of result.items) { const installed = item.isInstalled ? ' installed' : ''; console.log(`${item.type}/${item.id} [${item.type}${installed}] installs:${item.stats.downloadCount} likes:${item.stats.starCount}`); console.log(` ${item.skillName ?? item.title} — ${item.description}`); if (item.creatorDisplayName) console.log(` by ${item.creatorDisplayName}`); } if (result.pagination.hasMore && result.pagination.nextCursor) { console.log(`\nMore: --cursor ${result.pagination.nextCursor}`); } console.log('\nInstall to workspace: ccl hub install /'); } catch (err) { handleError(err); } }); hub.command('info ') .description('Show one Hub resource') .option('--json', 'machine-readable output') .action(async (ref: string, opts: any) => { try { const { type, id } = parseResourceRef(ref); const detail = await getClient(deps).getResource(type, id); if (opts.json) { console.log(JSON.stringify(detail, null, 2)); return; } console.log(`${detail.type}/${detail.id} [${detail.type}] installs:${detail.stats.downloadCount} likes:${detail.stats.starCount}`); console.log(detail.skillName ?? detail.title); console.log(detail.description); if (detail.creatorDisplayName) console.log(`by ${detail.creatorDisplayName}`); if (detail.fileName) console.log(`file: ${detail.fileName}`); console.log(detail.isInstalled ? '\nAlready installed in this account workspace.' : `\nInstall: ccl hub install ${detail.type}/${detail.id}`); } catch (err) { handleError(err); } }); hub.command('install ') .description('Install a published Hub resource into the active account workspace') .option('--json', 'machine-readable output') .action(async (raw: string, opts: any) => { try { const ref = parseResourceRef(raw); const result = await getClient(deps).installResource(ref.id); const output = { ref: raw, ...result, message: 'Installed in workspace. Select it in the Hub loadout before ccl prepare applies it.' }; if (opts.json) { console.log(JSON.stringify(output, null, 2)); return; } console.log(`${result.created ? 'Installed' : 'Already installed'} ${raw} in the active account workspace.`); console.log('Select it in the Hub loadout; ccl prepare applies the selected revision before play.'); } catch (err) { handleError(err); } }); hub.command('installed') .description('List resources in the active account workspace') .option('--type ', 'skill | strategy | persona | perception', typeOption) .option('--cursor ', 'cursor from the previous response') .option('--json', 'machine-readable output') .action(async (opts: any) => { try { const result = await getClient(deps).listWorkspace({ type: opts.type, cursor: opts.cursor, limit: 100, }); if (opts.json) { console.log(JSON.stringify(result, null, 2)); return; } if (result.items.length === 0) { console.log('No Hub resources installed in the active account workspace.'); return; } for (const item of result.items) { console.log(`${item.resourceType}/${item.resourceId} "${item.skillName ?? item.title}"${item.equipped ? ' [equipped]' : ''}${item.hasUpdate ? ' [update available]' : ''}`); } } catch (err) { handleError(err); } }); hub.command('sync ') .description('Update one installed workspace item to the current published version') .option('--json', 'machine-readable output') .action(async (raw: string, opts: any) => { try { const { type, id } = parseResourceRef(raw); const client = getClient(deps); const item = await client.findWorkspaceItem(type, id); if (!item) throw new Error(`${raw} is not installed in the active account workspace`); const result = await client.syncWorkspaceItem(item.workspaceItemId); const output = { ref: raw, workspaceItemId: item.workspaceItemId, ...result }; if (opts.json) console.log(JSON.stringify(output, null, 2)); else console.log(result.updated ? `Updated ${raw}.` : `${raw} is already current.`); } catch (err) { handleError(err); } }); hub.command('uninstall ', { hidden: true }) .description('Remove an unequipped resource from the active account workspace') .option('--json', 'machine-readable output') .action(async (raw: string, opts: any) => { try { const { type, id } = parseResourceRef(raw); const client = getClient(deps); const item = await client.findWorkspaceItem(type, id); if (!item) { const output = { ref: raw, removed: false }; if (opts.json) console.log(JSON.stringify(output, null, 2)); else console.log(`${raw} is not installed.`); return; } await client.deleteWorkspaceItem(item.workspaceItemId); const output = { ref: raw, workspaceItemId: item.workspaceItemId, removed: true }; if (opts.json) console.log(JSON.stringify(output, null, 2)); else console.log(`Removed ${raw} from the active account workspace.`); } catch (err) { handleError(err); } }); return hub; }