// tools/suggest-tooling/llm-security-catalog.ts — SPEC-045
// Builds LLM guardrails and MLOps tooling recommendation lines for suggest_tooling.
import { readFile } from 'node:fs/promises';
import { generateMlopsToolingLines } from '../../engine/llm-security/index.js';
let configCache = null;
/** Reset in-memory cache (for testing). */
export function resetLlmSecurityCatalogCache() {
    configCache = null;
}
async function loadConfig() {
    if (configCache) {
        return configCache;
    }
    try {
        const configPath = new URL('../../config/llm-guardrails-tooling.json', import.meta.url);
        const content = await readFile(configPath, 'utf-8');
        const raw = JSON.parse(content);
        delete raw._description;
        delete raw._regulation_date;
        configCache = raw;
        return configCache;
    }
    catch {
        return {
            highRiskDomains: {},
            unacceptableRiskKeywords: [],
            guardrailTools: { python: [], node: [], enterprise: [] },
            mlopsTools: { start: [], scale: [], enterprise: [], llmops: [] },
        };
    }
}
function formatToolEntry(tool, required) {
    const requiredTag = required ? ' **(required)**' : '';
    return [`**${tool.name}**${requiredTag} — ${tool.description}`, `- Docs: ${tool.docsUrl}`, ''];
}
/**
 * Build LLM guardrails and MLOps tooling recommendation lines.
 * Only emits when llmDetection is present and isLlmProject is true.
 */
export async function buildLlmSecurityToolingLines(knowledge) {
    const detection = knowledge.llmDetection;
    if (!detection?.isLlmProject) {
        return [];
    }
    const config = await loadConfig();
    const lang = knowledge.language.toLowerCase();
    const isNodeProject = lang === 'typescript' || lang === 'javascript';
    const isPythonProject = lang === 'python';
    const lines = ['', '## LLM Security & Guardrails', ''];
    lines.push(`**Provider detectado:** ${detection.provider ?? 'unknown'}`);
    lines.push(`**Framework:** ${detection.framework}`);
    if (detection.hasRAG) {
        lines.push(`**RAG:** detectado${detection.vectorStore ? ` (${detection.vectorStore})` : ''}`);
    }
    if (detection.hasToolUse) {
        lines.push('**Tool Use:** detectado — guardrails **requeridos** para proyectos con tool use');
    }
    lines.push('');
    // Guardrails section
    lines.push('### Guardrails Recomendados', '');
    const toolRequired = detection.hasToolUse;
    if (isPythonProject) {
        for (const tool of config.guardrailTools.python) {
            lines.push(...formatToolEntry(tool, toolRequired && tool.name === 'LLM Guard'));
        }
    }
    if (isNodeProject) {
        for (const tool of config.guardrailTools.node) {
            lines.push(...formatToolEntry(tool, toolRequired));
        }
    }
    // Enterprise guardrails always shown
    for (const tool of config.guardrailTools.enterprise) {
        lines.push(...formatToolEntry(tool, false));
    }
    // MLOps section
    const mlopsLines = generateMlopsToolingLines(detection);
    if (mlopsLines.length > 0) {
        lines.push(...mlopsLines);
    }
    return lines;
}
//# sourceMappingURL=llm-security-catalog.js.map