feat(ai): Integrate OpenAI provider and enhance model config

- Add OpenAI provider implementation using @ai-sdk/openai.\n- Update `models` command/tool to display API key status for configured providers.\n- Implement model-specific `maxTokens` override logic in `config-manager.js` using `supported-models.json`.\n- Improve AI error message parsing in `ai-services-unified.js` for better clarity.
This commit is contained in:
Eyal Toledano
2025-04-27 03:56:23 -04:00
parent 842eaf7224
commit 2517bc112c
21 changed files with 1350 additions and 662 deletions

View File

@@ -1814,6 +1814,210 @@ async function confirmTaskOverwrite(tasksPath) {
return answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes';
}
/**
* Displays the API key status for different providers.
* @param {Array<{provider: string, cli: boolean, mcp: boolean}>} statusReport - The report generated by getApiKeyStatusReport.
*/
function displayApiKeyStatus(statusReport) {
if (!statusReport || statusReport.length === 0) {
console.log(chalk.yellow('No API key status information available.'));
return;
}
const table = new Table({
head: [
chalk.cyan('Provider'),
chalk.cyan('CLI Key (.env)'),
chalk.cyan('MCP Key (mcp.json)')
],
colWidths: [15, 20, 25],
chars: { mid: '', 'left-mid': '', 'mid-mid': '', 'right-mid': '' }
});
statusReport.forEach(({ provider, cli, mcp }) => {
const cliStatus = cli ? chalk.green('✅ Found') : chalk.red('❌ Missing');
const mcpStatus = mcp ? chalk.green('✅ Found') : chalk.red('❌ Missing');
// Capitalize provider name for display
const providerName = provider.charAt(0).toUpperCase() + provider.slice(1);
table.push([providerName, cliStatus, mcpStatus]);
});
console.log(chalk.bold('\n🔑 API Key Status:'));
console.log(table.toString());
console.log(
chalk.gray(
' Note: Some providers (e.g., Azure, Ollama) may require additional endpoint configuration in .taskmasterconfig.'
)
);
}
// --- Formatting Helpers (Potentially move some to utils.js if reusable) ---
const formatSweScoreWithTertileStars = (score, allModels) => {
// ... (Implementation from previous version or refine) ...
if (score === null || score === undefined || score <= 0) return 'N/A';
const formattedPercentage = `${(score * 100).toFixed(1)}%`;
const validScores = allModels
.map((m) => m.sweScore)
.filter((s) => s !== null && s !== undefined && s > 0);
const sortedScores = [...validScores].sort((a, b) => b - a);
const n = sortedScores.length;
let stars = chalk.gray('☆☆☆');
if (n > 0) {
const topThirdIndex = Math.max(0, Math.floor(n / 3) - 1);
const midThirdIndex = Math.max(0, Math.floor((2 * n) / 3) - 1);
if (score >= sortedScores[topThirdIndex]) stars = chalk.yellow('★★★');
else if (score >= sortedScores[midThirdIndex])
stars = chalk.yellow('★★') + chalk.gray('☆');
else stars = chalk.yellow('★') + chalk.gray('☆☆');
}
return `${formattedPercentage} ${stars}`;
};
const formatCost = (costObj) => {
// ... (Implementation from previous version or refine) ...
if (!costObj) return 'N/A';
if (costObj.input === 0 && costObj.output === 0) {
return chalk.green('Free');
}
const formatSingleCost = (costValue) => {
if (costValue === null || costValue === undefined) return 'N/A';
const isInteger = Number.isInteger(costValue);
return `$${costValue.toFixed(isInteger ? 0 : 2)}`;
};
return `${formatSingleCost(costObj.input)} in, ${formatSingleCost(costObj.output)} out`;
};
// --- Display Functions ---
/**
* Displays the currently configured active models.
* @param {ConfigData} configData - The active configuration data.
* @param {AvailableModel[]} allAvailableModels - Needed for SWE score tertiles.
*/
function displayModelConfiguration(configData, allAvailableModels = []) {
console.log(chalk.cyan.bold('\nActive Model Configuration:'));
const active = configData.activeModels;
const activeTable = new Table({
head: [
'Role',
'Provider',
'Model ID',
'SWE Score',
'Cost ($/1M tkns)'
// 'API Key Status' // Removed, handled by separate displayApiKeyStatus
].map((h) => chalk.cyan.bold(h)),
colWidths: [10, 14, 30, 18, 20 /*, 28 */], // Adjusted widths
style: { head: ['cyan', 'bold'] }
});
activeTable.push([
chalk.white('Main'),
active.main.provider,
active.main.modelId,
formatSweScoreWithTertileStars(active.main.sweScore, allAvailableModels),
formatCost(active.main.cost)
// getCombinedStatus(active.main.keyStatus) // Removed
]);
activeTable.push([
chalk.white('Research'),
active.research.provider,
active.research.modelId,
formatSweScoreWithTertileStars(
active.research.sweScore,
allAvailableModels
),
formatCost(active.research.cost)
// getCombinedStatus(active.research.keyStatus) // Removed
]);
if (active.fallback && active.fallback.provider && active.fallback.modelId) {
activeTable.push([
chalk.white('Fallback'),
active.fallback.provider,
active.fallback.modelId,
formatSweScoreWithTertileStars(
active.fallback.sweScore,
allAvailableModels
),
formatCost(active.fallback.cost)
// getCombinedStatus(active.fallback.keyStatus) // Removed
]);
} else {
activeTable.push([
chalk.white('Fallback'),
chalk.gray('-'),
chalk.gray('(Not Set)'),
chalk.gray('-'),
chalk.gray('-')
// chalk.gray('-') // Removed
]);
}
console.log(activeTable.toString());
}
/**
* Displays the list of available models not currently configured.
* @param {AvailableModel[]} availableModels - List of available models.
*/
function displayAvailableModels(availableModels) {
if (!availableModels || availableModels.length === 0) {
console.log(
chalk.gray('\n(No other models available or all are configured)')
);
return;
}
console.log(chalk.cyan.bold('\nOther Available Models:'));
const availableTable = new Table({
head: ['Provider', 'Model ID', 'SWE Score', 'Cost ($/1M tkns)'].map((h) =>
chalk.cyan.bold(h)
),
colWidths: [15, 40, 18, 25],
style: { head: ['cyan', 'bold'] }
});
availableModels.forEach((model) => {
availableTable.push([
model.provider,
model.modelId,
formatSweScoreWithTertileStars(model.sweScore, availableModels), // Pass itself for comparison
formatCost(model.cost)
]);
});
console.log(availableTable.toString());
// --- Suggested Actions Section (moved here from models command) ---
console.log(
boxen(
chalk.white.bold('Next Steps:') +
'\n' +
chalk.cyan(
`1. Set main model: ${chalk.yellow('task-master models --set-main <model_id>')}`
) +
'\n' +
chalk.cyan(
`2. Set research model: ${chalk.yellow('task-master models --set-research <model_id>')}`
) +
'\n' +
chalk.cyan(
`3. Set fallback model: ${chalk.yellow('task-master models --set-fallback <model_id>')}`
) +
'\n' +
chalk.cyan(
`4. Run interactive setup: ${chalk.yellow('task-master models --setup')}`
),
{
padding: 1,
borderColor: 'yellow',
borderStyle: 'round',
margin: { top: 1 }
}
)
);
}
// Export UI functions
export {
displayBanner,
@@ -1828,5 +2032,8 @@ export {
displayTaskById,
displayComplexityReport,
generateComplexityAnalysisPrompt,
confirmTaskOverwrite
confirmTaskOverwrite,
displayApiKeyStatus,
displayModelConfiguration,
displayAvailableModels
};