// engine/facilitate/router.ts — SPEC-264 Routing logic for facilitate orchestrator
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function findRelatedActiveSpec(context, keywords) {
    const lowerKeywords = keywords.map((k) => k.toLowerCase());
    for (const spec of context.activeSpecs) {
        const titleLower = spec.title.toLowerCase();
        if (lowerKeywords.some((kw) => titleLower.includes(kw))) {
            return spec.id;
        }
    }
    // No keyword match — return first active spec if only one
    if (context.activeSpecs.length === 1 && context.activeSpecs[0]) {
        return context.activeSpecs[0].id;
    }
    return null;
}
function buildSpecDescription(request, intent) {
    const typePrefix = intent.type === 'bugfix' ? 'Fix: ' : '';
    return `${typePrefix}${request}`;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Decide the best next action given classified intent and loaded context.
 */
export function route(intent, context, request) {
    // 1. Resume active work when there's a related spec in-progress
    if (context.hasActiveWork) {
        const relatedId = findRelatedActiveSpec(context, intent.keywords);
        if (relatedId) {
            const spec = context.activeSpecs.find((s) => s.id === relatedId);
            return {
                action: 'resume',
                specId: relatedId,
                reason: `Spec ${relatedId} "${spec?.title ?? ''}" is in progress and matches your request`,
            };
        }
    }
    // 2. Trivial requests → answer directly without creating a spec
    if (intent.complexity === 'trivial') {
        return {
            action: 'direct',
            reason: 'Request is trivial — no spec needed, handle inline',
        };
    }
    // 3. Exploration and questions → direct answer
    if (intent.type === 'exploration' || intent.type === 'question') {
        return {
            action: 'direct',
            reason: 'Informational request — answer directly',
        };
    }
    // 4. Ambiguous architectural requests → ask for clarification
    if (intent.complexity === 'architectural' && intent.confidence < 0.7) {
        return {
            action: 'clarify',
            question: 'This looks like a significant architectural change. ' +
                'Could you describe the current pain point and the desired end state?',
        };
    }
    // 5. Default → create a spec
    return {
        action: 'create-spec',
        description: buildSpecDescription(request, intent),
        type: intent.type,
    };
}
//# sourceMappingURL=router.js.map