import { capabilityCost, DEFAULT_CAPABILITY_CARDS } from './capability-card.js';
import { classifyCodexTask, scoreCapabilityCard } from './task-classifier.js';
import { codexRouteCacheKey, readRouteCache, writeRouteCache } from './route-cache.js';
export function routeCodexTask(input, cards = DEFAULT_CAPABILITY_CARDS) {
    const key = codexRouteCacheKey(input);
    const cached = readRouteCache(key);
    if (cached)
        return { ...cached, cache_hit: true };
    const classification = classifyCodexTask(input);
    const hardFilters = [];
    const availableCards = input.allowLocalLlm === true ? cards : cards.filter((card) => card.id !== 'local-llm-worker');
    const scored = availableCards.map((card) => {
        const score = scoreCapabilityCard(card, classification);
        if (score === 0)
            hardFilters.push(card.id);
        return { card, score, cost: capabilityCost(card) };
    });
    const viable = scored
        .filter((row) => row.score >= classification.threshold)
        .sort((a, b) => a.cost - b.cost || b.score - a.score);
    const fallback = scored
        .filter((row) => row.card.tier === classification.tier && row.score > 0)
        .sort((a, b) => b.score - a.score || a.cost - b.cost)[0];
    const selected = viable[0] || fallback || scored.find((row) => row.card.id === 'fast-worker') || scored[0];
    if (!selected)
        throw new Error('UltraRouter requires at least one capability card');
    const decision = {
        schema: 'sks.ultra-router-decision.v1',
        selected_profile: selected.card.id,
        reason: viable[0] ? 'score>=threshold cheapest-good-enough' : 'deterministic-default-after-classifier-shortfall',
        scores: Object.fromEntries(scored.map((row) => [row.card.id, row.score])),
        costs: Object.fromEntries(scored.map((row) => [row.card.id, row.cost])),
        cache_hit: false,
        tier: classification.tier,
        threshold: classification.threshold,
        hard_filters: [...new Set(hardFilters)],
        classification
    };
    return writeRouteCache(key, decision);
}
//# sourceMappingURL=ultra-router.js.map