// tools/recommend-model-handler.ts — Handler for the recommend_model MCP tool.
// SPEC-076 AC-05
import { extractSignals, buildSignalsFromPartial, } from '../engine/model-router/complexity-analyzer.js';
import { evaluateRules } from '../engine/model-router/rules-engine.js';
import { adjustFromHistory, computeTaskSignature, } from '../engine/model-router/historical-learner.js';
import { calculateCost } from '../engine/model-router/cost-estimator.js';
/**
 * Handle the recommend_model tool call.
 * Accepts a specId, taskDescription, or explicit signals and returns
 * a model recommendation with reasoning and cost estimate.
 */
export function handleRecommendModel(args) {
    // 1. Build signals
    const text = args.taskDescription ?? args.specId ?? '';
    const signals = args.signals
        ? buildSignalsFromPartial(args.signals)
        : extractSignals(text, undefined);
    // 2. Evaluate rules
    const rulesResult = evaluateRules(signals);
    // 3. Apply historical adjustment
    const taskSig = computeTaskSignature(text || 'unnamed-task');
    const adjustment = adjustFromHistory(rulesResult.model, taskSig);
    const finalModel = adjustment.adjusted ? adjustment.adjustedModel : rulesResult.model;
    const reasoning = [...rulesResult.reasoning];
    if (adjustment.adjusted) {
        reasoning.push(adjustment.reason);
    }
    // 4. Calculate costs
    const costEstimate = calculateCost(finalModel, signals);
    const alternatives = rulesResult.alternatives.map((alt) => ({
        model: alt.model,
        confidence: alt.confidence,
        costEstimate: calculateCost(alt.model, signals),
    }));
    const recommendation = {
        model: finalModel,
        confidence: adjustment.adjusted
            ? Math.max(rulesResult.confidence * 0.9, 0.5)
            : rulesResult.confidence,
        reasoning,
        costEstimate,
        alternatives,
        ultrathinkSuggested: rulesResult.ultrathinkSuggested,
        ultrathinkReason: rulesResult.ultrathinkReason,
        effortLevelCommand: rulesResult.effortLevelCommand,
        thinkingToggle: rulesResult.thinkingToggle,
    };
    // 5. Format output
    const lines = [
        `## Model Recommendation: **${recommendation.model}**`,
        '',
        `**Confidence:** ${(recommendation.confidence * 100).toFixed(0)}%`,
        `**Estimated cost:** $${recommendation.costEstimate.estimatedUSD.toFixed(4)} (${String(recommendation.costEstimate.estimatedTokens)} tokens)`,
        '',
        '### Reasoning',
        ...recommendation.reasoning.map((r) => `- ${r}`),
    ];
    if (recommendation.alternatives.length > 0) {
        lines.push('', '### Alternatives');
        for (const alt of recommendation.alternatives) {
            lines.push(`- **${alt.model}** (${(alt.confidence * 100).toFixed(0)}% confidence) — $${alt.costEstimate.estimatedUSD.toFixed(4)}`);
        }
    }
    if (recommendation.ultrathinkSuggested) {
        lines.push('', '### Extended Thinking Recommended', `> ${recommendation.ultrathinkReason ?? ''}`, '', `**Effort level:** ${recommendation.effortLevelCommand ?? ''}`, `**Toggle thinking:** ${recommendation.thinkingToggle ?? ''}`);
    }
    lines.push('', '### Complexity Signals', `- Criteria: ${String(signals.criteriaCount)}`, `- Files: ${String(signals.fileCount)}`, `- Cross-layer: ${String(signals.crossLayerCount)}`, `- Task type: ${signals.taskType}`, `- Overall score: ${signals.overallScore.toFixed(2)}`);
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
//# sourceMappingURL=recommend-model-handler.js.map