diff --git a/README.md b/README.md index 7193bd4..b747f41 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,10 @@ termly config set defaultAI aider termly config get defaultAI ``` +`defaultAI` is used by `termly start` when no `--ai` flag is given. If the +configured tool is unknown or not installed, Termly warns and falls back to +auto-detection instead of failing. + **Note:** Server URL is determined by environment and cannot be changed via config. ### Cleanup diff --git a/lib/ai-tools/selector.js b/lib/ai-tools/selector.js index bebb826..b6989ed 100644 --- a/lib/ai-tools/selector.js +++ b/lib/ai-tools/selector.js @@ -2,6 +2,7 @@ const inquirer = require('inquirer'); const chalk = require('chalk'); const { detectInstalledTools, isToolInstalled } = require('./detector'); const { getToolByKey } = require('./registry'); +const { getDefaultAI } = require('../config/manager'); const logger = require('../utils/logger'); // Select AI tool based on options @@ -12,6 +13,12 @@ async function selectAITool(options) { return await selectManualTool(options.ai); } + // Configured default (termly setup / termly config set defaultAI ) + const configuredTool = await selectConfiguredDefault(); + if (configuredTool) { + return configuredTool; + } + // No auto-detect mode if (options.noAutoDetect) { console.error(chalk.red('❌ Please specify AI tool with --ai flag')); @@ -27,6 +34,40 @@ async function selectAITool(options) { return await autoSelectTool(); } +// Configured default tool selection +// +// Unlike --ai, `defaultAI` is a stored preference that may have been set long +// ago, so a stale value must never kill `termly start`: warn and let the caller +// fall back to auto-detection. +async function selectConfiguredDefault() { + const defaultAI = getDefaultAI(); + + if (!defaultAI) { + return null; + } + + logger.debug(`Configured defaultAI: ${defaultAI}`); + + const tool = getToolByKey(defaultAI); + + if (!tool) { + logger.warn(`Configured default AI "${defaultAI}" is unknown - falling back to auto-detection`); + console.log(chalk.dim(' Change it with: termly config set defaultAI ')); + return null; + } + + const result = await isToolInstalled(tool.key); + + if (!result.installed) { + logger.warn(`Configured default AI ${tool.displayName} is not installed - falling back to auto-detection`); + console.log(chalk.dim(' Change it with: termly config set defaultAI ')); + return null; + } + + console.log(chalk.green(`Using ${result.tool.displayName} v${result.tool.version} (configured default)`)); + return result.tool; +} + // Manual tool selection async function selectManualTool(toolName) { const tool = getToolByKey(toolName);