import { Command } from 'commander'; import * as fs from 'fs'; import ora from 'ora'; import { IndemnClient } from '../sdk/client.js'; import { ConfigSDK } from '../sdk/config.js'; import { printJson, printError, printSuccess } from './utils/output.js'; export function registerConfigCommands(program: Command): void { const config = program.command('config').description('Manage agent configuration'); config .command('get') .argument('', 'Agent ID') .description('Get agent configuration') .option('--field ', 'Get specific field (prompt, greeting, model)') .option('--json', 'Output as JSON') .action(async (agentId: string, options) => { try { const client = new IndemnClient(); const sdk = new ConfigSDK(client); const spinner = ora('Fetching configuration...').start(); const cfg = await sdk.getConfig(agentId); spinner.stop(); if (options.field) { const field = options.field as string; let value: unknown; switch (field) { case 'prompt': value = cfg.ai_config?.system_prompt; break; case 'greeting': value = cfg.first_message; break; case 'model': { const llm = cfg.llm_config as Record | undefined; value = llm ? `${llm.provider}/${llm.model}` : undefined; break; } default: value = (cfg as Record)[field]; } if (value === undefined) { console.log(`Field "${field}" is not set.`); return; } if (options.json || typeof value === 'object') { printJson(value); } else { console.log(String(value)); } return; } if (options.json) { printJson(cfg); return; } // Pretty print the configuration console.log('Agent Configuration:'); if (cfg.ai_config?.system_prompt) { console.log('\nSystem Prompt:'); console.log(' ' + cfg.ai_config.system_prompt.replace(/\n/g, '\n ')); } if (cfg.first_message) { console.log('\nGreeting:'); if (cfg.first_message.text) { console.log(` Text: ${cfg.first_message.text}`); } if (cfg.first_message.quick_replies?.length) { console.log(` Quick Replies: ${cfg.first_message.quick_replies.map((q) => q.title).join(', ')}`); } } if (cfg.ai_config?.kb_configuration) { console.log('\nKB Configuration:'); printJson(cfg.ai_config.kb_configuration); } } catch (err) { printError(err); process.exit(1); } }); config .command('set') .argument('', 'Agent ID') .description('Update agent configuration') .option('--prompt ', 'Set system prompt text') .option('--prompt-file ', 'Set system prompt from file') .option('--model ', 'Set model name') .action(async (agentId: string, options) => { try { const input: Record = {}; if (options.promptFile) { const filePath = options.promptFile as string; if (!fs.existsSync(filePath)) { printError(`File not found: ${filePath}`); process.exit(1); } input.system_prompt = fs.readFileSync(filePath, 'utf-8'); } else if (options.prompt) { input.system_prompt = options.prompt; } if (options.model) { const model = options.model as string; const provider = model.startsWith('claude') ? 'anthropic' : model.startsWith('gemini') ? 'google' : 'openai'; input.llm_config = { model, provider }; } if (Object.keys(input).length === 0) { printError('No configuration options provided. Use --prompt, --prompt-file, or --model.'); process.exit(1); } const client = new IndemnClient(); const sdk = new ConfigSDK(client); const spinner = ora('Updating configuration...').start(); await sdk.setConfig(agentId, input); spinner.stop(); printSuccess(`Configuration updated for agent ${agentId}.`); } catch (err) { printError(err); process.exit(1); } }); }