Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345import { existsSync } from 'fs';import { confirm, input, password, select } from '@inquirer/prompts';import { loadConfig, envGet, envSet } from '../config.js';import { composeRestart } from '../docker.js';import { applyLlmProviderConfig, getConfiguredLlmProvider, getLlmProviderPreset, LLM_PROVIDER_PRESETS } from '../llm.js';import * as d from '../utils/display.js';
// Feature flags
interface FeatureFlag { key: string; label: string; description: string;}
const FEATURES: FeatureFlag[] = [ { key: 'SUBSTRATE_ENABLE_CONTAINERS', label: 'Containers', description: 'Docker container management' }, { key: 'SUBSTRATE_ENABLE_WORKSPACE', label: 'Workspace', description: 'Persistent workspace container' }, { key: 'SUBSTRATE_ENABLE_TERMINAL_WS', label: 'Host Terminal', description: 'Substrate host PTY WebSocket' }, { key: 'SUBSTRATE_ENABLE_EMBEDDINGS', label: 'Embeddings', description: 'EmbeddingGemma vector search' }, { key: 'SUBSTRATE_ENABLE_LLM', label: 'LLM', description: 'LLM provider gateway (chat proxy)' },];
// Editable settings
interface Setting { key: string; label: string; secret?: boolean; placeholder?: string;}
const SETTINGS: Setting[] = [ { key: 'CONTAINER_RUNTIME', label: 'Container Runtime', placeholder: 'docker | podman | nerdctl' }, { key: 'SUBSTRATE_ALLOWED_ORIGINS', label: 'CORS Origins', placeholder: 'http://localhost:4305' }, { key: 'SUBSTRATE_HTTPS_PORT', label: 'HTTPS Port', placeholder: '8443' }, { key: 'SUBSTRATE_MAX_CONTAINERS', label: 'Max Containers', placeholder: '5' }, { key: 'EMBEDDINGS_DTYPE', label: 'Embeddings Dtype', placeholder: 'q8' }, { key: 'EMBEDDINGS_DIMENSIONS', label: 'Embeddings Dimensions', placeholder: '768' }, { key: 'LLM_DEFAULT_PROVIDER', label: 'LLM Provider', placeholder: 'ollama | lmstudio | anthropic | openai' }, { key: 'LLM_DEFAULT_MODEL', label: 'LLM Model', placeholder: 'claude-sonnet-4-20250514' }, { key: 'LLM_DEFAULT_BASE_URL', label: 'LLM Base URL', placeholder: 'http://localhost:11434/v1' }, { key: 'LLM_API_KEY', label: 'LLM API Key', secret: true, placeholder: 'sk-...' },];
// Feature name lookup
const FEATURE_ALIASES: Record<string, string> = {};for (const f of FEATURES) { FEATURE_ALIASES[f.label.toLowerCase()] = f.key; // Also allow the raw env key suffix: "containers", "workspace", etc. const suffix = f.key.replace('SUBSTRATE_ENABLE_', '').toLowerCase(); FEATURE_ALIASES[suffix] = f.key;}
// Maps a user-typed feature name (e.g. "containers", "Workspace", "host terminal") to its// SUBSTRATE_ENABLE_* env var. Accepts either the display label or the env-key suffix.function resolveFeatureKey(name: string): string | null { return FEATURE_ALIASES[name.toLowerCase()] ?? null;}
// Display helpers
function renderConfig(envFile: string) { d.header('Features'); for (const f of FEATURES) { const enabled = envGet(f.key, envFile) === 'true'; const dot = d.statusDot(enabled); const state = enabled ? d.GREEN('on') : d.DIM('off'); console.log(` ${dot} ${d.BOLD(f.label.padEnd(14))} ${state} ${d.DIM(f.description)}`); }
d.header('Settings'); const apiKey = envGet('SUBSTRATE_API_KEY', envFile); const llm = getConfiguredLlmProvider(envFile); d.label('API Key', apiKey ? `set (${apiKey.length} chars)` : d.RED('not set')); d.label('Docker GID', envGet('DOCKER_GID', envFile) || d.DIM('not set')); if (llm.providerName) { d.label('LLM Active', `${llm.providerName}${llm.model ? ` (${llm.model})` : ''}`); } for (const s of SETTINGS) { const value = envGet(s.key, envFile); d.label(s.label, s.secret && value ? `set (${value.length} chars)` : (value || d.DIM('not set'))); }}
// Interactive mode
export async function configCommand(opts: { key?: string; url?: string; enable?: string; disable?: string; restart?: boolean; llmProvider?: string; llmModel?: string; llmBaseUrl?: string; llmApiKey?: string;}) { const config = loadConfig(opts);
if (!existsSync(config.envFile)) { d.warn('.env file not found — run `aether-substrate init` first'); d.line(); return; }
if (opts.llmProvider) { const preset = getLlmProviderPreset(opts.llmProvider); if (!preset) { d.error(`Unknown LLM provider: "${opts.llmProvider}". Available: ${LLM_PROVIDER_PRESETS.map((provider) => provider.id).join(', ')}`); d.line(); return; }
const configured = applyLlmProviderConfig(config.envFile, { providerId: preset.id, model: opts.llmModel, baseUrl: opts.llmBaseUrl, apiKey: opts.llmApiKey, });
d.success(`LLM provider set to ${configured.providerName}`); if (configured.baseUrl) d.label('Base URL', configured.baseUrl); if (configured.model) d.label('Model', configured.model); if (preset.apiKeyRequired && !configured.apiKeyPresent) { d.warn(`${configured.providerName} usually requires LLM_API_KEY`); }
if (opts.restart) { d.info('Restarting substrate...'); composeRestart(config); d.success('Substrate restarted with new config'); } else { d.info('Restart substrate to apply: aether-substrate config --restart'); } d.line(); return; }
// One-liner mode
if (opts.enable || opts.disable) { const name = opts.enable || opts.disable!; const featureKey = resolveFeatureKey(name); if (!featureKey) { d.error(`Unknown feature: "${name}". Available: ${FEATURES.map(f => f.label.toLowerCase()).join(', ')}`); d.line(); return; } const newValue = opts.enable ? 'true' : 'false'; envSet(featureKey, newValue, config.envFile); const label = FEATURES.find(f => f.key === featureKey)!.label; d.success(`${label} ${opts.enable ? 'enabled' : 'disabled'}`);
if (opts.restart) { d.info('Restarting substrate...'); composeRestart(config); d.success('Substrate restarted with new config'); } else { d.info('Restart substrate to apply: aether-substrate config --restart'); } d.line(); return; }
// Restart-only mode
if (opts.restart) { d.info('Restarting substrate...'); composeRestart(config); d.success('Substrate restarted'); d.line(); return; }
// Interactive menu
let pendingChanges = false; let back = false;
while (!back) { console.clear(); d.banner(); d.line(); d.header('Configuration'); d.info(d.DIM(config.envFile)); d.line();
renderConfig(config.envFile);
if (pendingChanges) { d.line(); d.warn('You have unsaved changes — restart substrate to apply'); }
d.line();
const choices = [ { name: 'Toggle a feature', value: 'toggle', description: 'Enable or disable a feature flag' }, { name: 'Edit a setting', value: 'edit', description: 'Change a configuration value' }, { name: 'Configure LLM provider', value: 'llm', description: 'Set LM Studio, Ollama, or a hosted provider preset' }, ...(pendingChanges ? [{ name: d.GREEN('▸ Apply changes (restart substrate)'), value: 'restart', description: 'Restart to apply pending changes' }] : []), { name: 'Back', value: 'back' }, ];
const action = await select({ message: 'Choose an action', choices });
switch (action) { case 'toggle': { const featureChoices = FEATURES.map((f) => { const enabled = envGet(f.key, config.envFile) === 'true'; return { name: `${d.statusDot(enabled)} ${f.label.padEnd(14)} → ${enabled ? 'disable' : 'enable'}`, value: f.key, description: f.description, }; }); featureChoices.push({ name: 'Cancel', value: 'cancel', description: '' });
const featureKey = await select({ message: 'Which feature?', choices: featureChoices }); if (featureKey !== 'cancel') { const current = envGet(featureKey, config.envFile) === 'true'; const newValue = current ? 'false' : 'true'; envSet(featureKey, newValue, config.envFile); const label = FEATURES.find(f => f.key === featureKey)!.label; d.success(`${label} ${newValue === 'true' ? 'enabled' : 'disabled'}`); pendingChanges = true; } break; }
case 'edit': { const settingChoices = SETTINGS.map((s) => { const value = envGet(s.key, config.envFile); return { name: `${s.label.padEnd(22)} ${d.DIM(value || '(not set)')}`, value: s.key, }; }); settingChoices.push({ name: 'Cancel', value: 'cancel' });
const settingKey = await select({ message: 'Which setting?', choices: settingChoices }); if (settingKey !== 'cancel') { const setting = SETTINGS.find(s => s.key === settingKey)!; const currentValue = envGet(settingKey, config.envFile); const newValue = setting.secret ? await password({ message: `${setting.label}${currentValue ? ' (leave blank to keep current)' : ''}`, mask: '*', }) : await input({ message: `${setting.label}`, default: currentValue || setting.placeholder || '', }); if (setting.secret && !newValue && currentValue) { break; } if (newValue !== currentValue) { envSet(settingKey, newValue, config.envFile); d.success(`${setting.label} updated`); pendingChanges = true; } } break; }
case 'llm': { const providerId = await select({ message: 'Choose an LLM provider preset', choices: [ ...LLM_PROVIDER_PRESETS.map((provider) => ({ name: provider.name, value: provider.id, description: `${provider.type === 'local' ? 'Local' : 'Hosted'} · ${provider.baseUrl}`, })), { name: 'Cancel', value: 'cancel', description: '' }, ], });
if (providerId !== 'cancel') { const preset = getLlmProviderPreset(providerId)!; const current = getConfiguredLlmProvider(config.envFile); const currentBaseUrl = current.providerId === providerId ? current.baseUrl : ''; const currentModel = current.providerId === providerId ? current.model : '';
const model = await input({ message: `${preset.name} default model`, default: currentModel || preset.defaultModelPlaceholder, });
const baseUrl = await input({ message: `${preset.name} base URL`, default: currentBaseUrl || preset.baseUrl, });
let apiKey: string | undefined; if (preset.apiKeyRequired) { const existingApiKey = envGet('LLM_API_KEY', config.envFile); const enteredApiKey = await password({ message: `${preset.name} API key${existingApiKey ? ' (leave blank to keep current)' : ''}`, mask: '*', }); apiKey = enteredApiKey || (existingApiKey ? undefined : ''); }
applyLlmProviderConfig(config.envFile, { providerId: preset.id, model, baseUrl, apiKey, }); d.success(`${preset.name} configured`); pendingChanges = true; } break; }
case 'restart': { const yes = await confirm({ message: 'Restart substrate now?', default: true }); if (yes) { d.info('Restarting substrate...'); composeRestart(config); d.success('Substrate restarted with new config'); pendingChanges = false; } break; }
case 'back': { if (pendingChanges) { d.warn('Changes saved to .env but require a restart to take effect'); d.command('aether-substrate config --restart'); } back = true; break; } } }
d.line();}