// ---------------------------------------------------------------------------
// Per-tool ambiguity thresholds (0–100). Higher = more tolerant.
// Generative/destructive tools require lower threshold than read-only ones.
// ---------------------------------------------------------------------------
const TOOL_THRESHOLDS = {
    create_spec: 40,
    init_project: 35,
    init_constitution: 35,
    generate_tests: 45,
    generate_docs: 45,
    generate_execution_plan: 50,
    design_schema: 45,
    define_ui_contract: 45,
    estimate: 55,
    // read-only tools — very tolerant
    list_specs: 90,
    summarize_spec: 90,
    consult_docs: 85,
    detect_drift: 80,
    audit: 80,
};
const DEFAULT_GENERATIVE_THRESHOLD = 50;
const DEFAULT_READONLY_THRESHOLD = 85;
// Read-only tools that should never be blocked.
const READONLY_TOOLS = new Set([
    'list_specs',
    'summarize_spec',
    'consult_docs',
    'detect_drift',
    'audit',
    'discover_mcps',
    'suggest_mcps',
    'check_versions',
]);
// ---------------------------------------------------------------------------
// Dimension scoring helpers
// ---------------------------------------------------------------------------
/** Keywords that indicate the platform was specified. */
const PLATFORM_SIGNALS = [
    'web',
    'mobile',
    'ios',
    'android',
    'desktop',
    'api',
    'backend',
    'frontend',
    'cli',
    'server',
    'app',
    'browser',
];
/** Keywords that signal a concrete business objective. */
const OBJECTIVE_SIGNALS = [
    'to',
    'so that',
    'in order to',
    'because',
    'needs',
    'want',
    'allow',
    'enable',
    'users can',
    'user can',
    'we need',
    'must',
    'should',
];
/** Keywords that suggest stack information. */
const STACK_SIGNALS = [
    'react',
    'vue',
    'angular',
    'next',
    'node',
    'express',
    'fastapi',
    'django',
    'rails',
    'spring',
    'laravel',
    'typescript',
    'python',
    'java',
    'rust',
    'go',
    'postgres',
    'mysql',
    'mongodb',
    'sqlite',
    'redis',
    'firebase',
    'supabase',
];
/** Keywords that indicate business context. */
const CONTEXT_SIGNALS = [
    'users',
    'customers',
    'admin',
    'team',
    'company',
    'system',
    'service',
    'product',
    'feature',
    'module',
    'page',
    'screen',
    'dashboard',
    'checkout',
];
function scoreAbsence(text, signals) {
    const lower = text.toLowerCase();
    const found = signals.some((s) => lower.includes(s));
    return found ? 0 : 100;
}
function countWords(text) {
    return text.trim().split(/\s+/).filter(Boolean).length;
}
/** Compute the score for a single dimension (0 = clear, 100 = vague). */
function scoreDimension(dimension, text) {
    const wordCount = countWords(text);
    switch (dimension) {
        case 'platform': {
            // Short texts with no platform signal → highly ambiguous
            if (wordCount < 5) {
                return 80;
            }
            return scoreAbsence(text, PLATFORM_SIGNALS) * 0.7;
        }
        case 'objective': {
            // If very short or no objective signal → high ambiguity
            if (wordCount < 4) {
                return 90;
            }
            const hasObjective = scoreAbsence(text, OBJECTIVE_SIGNALS) === 0;
            return hasObjective ? 10 : 60;
        }
        case 'stack': {
            // Stack is optional for many tools — penalize less
            return scoreAbsence(text, STACK_SIGNALS) * 0.4;
        }
        case 'business-context': {
            if (wordCount < 6) {
                return 70;
            }
            return scoreAbsence(text, CONTEXT_SIGNALS) * 0.5;
        }
    }
}
// ---------------------------------------------------------------------------
// Question templates per dimension
// ---------------------------------------------------------------------------
const QUESTIONS = {
    platform: [
        'What platform is this for? (e.g., web app, mobile iOS/Android, REST API, CLI tool)',
        'Where will users access this? Web browser, mobile device, or a server-side service?',
    ],
    objective: [
        'What specific problem does this solve for users? Who are the users and what do they need to do?',
        'What is the main goal users accomplish with this feature?',
    ],
    'business-context': [
        'What type of product or system is this part of? (e.g., e-commerce, SaaS dashboard, internal tool)',
        'Who will use this — end users, admins, or an automated system?',
    ],
    stack: [
        'What tech stack will you use? (language, framework, database)',
        'What existing technologies should this integrate with?',
    ],
};
function pickQuestion(dimension) {
    const list = QUESTIONS[dimension];
    return list[0] ?? 'Could you provide more details about what you need?';
}
// ---------------------------------------------------------------------------
// Level classification
// ---------------------------------------------------------------------------
function scoreToLevel(score) {
    if (score < 25) {
        return 'none';
    }
    if (score < 50) {
        return 'low';
    }
    if (score < 70) {
        return 'medium';
    }
    return 'critical';
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Scores a request description for ambiguity.
 * Returns a full AmbiguityScore with the most blocking dimension identified.
 */
export function scoreAmbiguity(description) {
    const dimensions = {
        platform: scoreDimension('platform', description),
        objective: scoreDimension('objective', description),
        'business-context': scoreDimension('business-context', description),
        stack: scoreDimension('stack', description),
    };
    // Composite score — objective and platform matter most
    const composite = Math.round(dimensions.platform * 0.35 +
        dimensions.objective * 0.35 +
        dimensions['business-context'] * 0.2 +
        dimensions.stack * 0.1);
    // Find the most blocking dimension (highest score)
    const blockingDimension = Object.entries(dimensions).reduce((best, [dim, val]) => (val > best[1] ? [dim, val] : best), ['platform', 0])[0];
    return {
        score: composite,
        level: scoreToLevel(composite),
        blockingDimension,
        clarificationQuestion: pickQuestion(blockingDimension),
        dimensions,
    };
}
/**
 * Returns true if the request should be blocked for the given tool.
 * Read-only tools are never blocked.
 */
export function shouldBlock(toolName, score) {
    if (READONLY_TOOLS.has(toolName)) {
        return false;
    }
    const threshold = TOOL_THRESHOLDS[toolName] ?? DEFAULT_GENERATIVE_THRESHOLD;
    return score.score >= threshold;
}
/**
 * Returns the threshold for a tool name.
 * Exported for testing / configuration purposes.
 */
export function getThreshold(toolName) {
    if (READONLY_TOOLS.has(toolName)) {
        return DEFAULT_READONLY_THRESHOLD;
    }
    return TOOL_THRESHOLDS[toolName] ?? DEFAULT_GENERATIVE_THRESHOLD;
}
//# sourceMappingURL=ambiguity-detector.js.map