"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePulseCsvFromResultList = exports.generateCsvFromTable = exports.generatePulseFunctionalEvalCsvRow = exports.generatePulseRedteamCsvRow = exports.generateFunctionalEvalCsvRow = exports.generateRedteamCsvRow = exports.getPulseFunctionalEvalCsvHeaders = exports.getPulseRedteamCsvHeaders = exports.getFunctionalEvalCsvHeaders = exports.getRedteamCsvHeaders = exports.getCategoryNameForPlugin = exports.formatPromptForCsv = void 0;
const lodash_1 = __importDefault(require("lodash"));
const constants_1 = require("../redteam/constants");
/**
 * Format prompt text for CSV export
 * Handles JSON parsing, audio type, conversation arrays
 */
const formatPromptForCsv = (textValue) => {
    if (typeof textValue !== 'string') {
        return textValue;
    }
    try {
        const parsed = JSON.parse(textValue);
        if (parsed && parsed.type === 'audio') {
            return 'type=audio';
        }
        return textValue;
    }
    catch {
        return textValue;
    }
};
exports.formatPromptForCsv = formatPromptForCsv;
/**
 * Get category name for security/redteam plugins
 */
const getCategoryNameForPlugin = (pluginId) => {
    return (constants_1.displayNameOverrides[pluginId] ??
        (pluginId?.includes('trusys:redteam:')
            ? constants_1.displayNameOverrides[pluginId.replace('trusys:redteam:', '')]
            : undefined));
};
exports.getCategoryNameForPlugin = getCategoryNameForPlugin;
/**
 * Generate CSV headers for redteam results
 */
const getRedteamCsvHeaders = () => {
    return ['Vulnerable Category', 'Strategy', 'Prompt', 'Output', 'Score', 'Pass/Fail', 'Response'];
};
exports.getRedteamCsvHeaders = getRedteamCsvHeaders;
/**
 * Generate CSV headers for functional eval results
 */
const getFunctionalEvalCsvHeaders = (providerName) => {
    return [
        'Test Case ID',
        'Prompt',
        providerName ? `Output (${providerName})` : 'Output',
        'Prompt Score',
        'Prompt Pass/Fail',
        'Metric Name',
        'Metric Pass/Fail',
        'Metric Score',
        'Metric Response Value',
    ];
};
exports.getFunctionalEvalCsvHeaders = getFunctionalEvalCsvHeaders;
/**
 * Generate CSV headers for Pulse redteam results
 */
const getPulseRedteamCsvHeaders = () => {
    return [
        'Evaluation Type',
        'Trace ID',
        'Session ID',
        'Vulnerable Category',
        'Strategy',
        'Input',
        'Response',
        'Score',
        'Pass/Fail',
        'Output',
    ];
};
exports.getPulseRedteamCsvHeaders = getPulseRedteamCsvHeaders;
/**
 * Generate CSV headers for Pulse functional eval results
 */
const getPulseFunctionalEvalCsvHeaders = () => {
    return [
        'Evaluation Type',
        'Trace ID',
        'Session ID',
        'Metric Name',
        'Metric Pass/Fail',
        'Metric Score',
        'Input',
        'Response',
        'Score',
        'Pass/Fail',
        'Metric Response Value',
    ];
};
exports.getPulseFunctionalEvalCsvHeaders = getPulseFunctionalEvalCsvHeaders;
/**
 * Generate CSV row for redteam result
 */
const generateRedteamCsvRow = (output, metricResult) => {
    const pluginId = output?.metadata?.pluginId;
    const categoryName = (0, exports.getCategoryNameForPlugin)(pluginId);
    const strategyName = constants_1.displayNameOverrides[output?.metadata?.strategyId] ||
        'Baseline Testing';
    return [
        categoryName,
        strategyName,
        (0, exports.formatPromptForCsv)(output?.prompt),
        output?.text,
        output?.score !== undefined && output?.score !== null ? Number(output?.score).toFixed(2) : '',
        output?.pass ? 'Pass' : 'Fail',
        metricResult?.reason,
    ];
};
exports.generateRedteamCsvRow = generateRedteamCsvRow;
/**
 * Generate CSV row for functional eval result
 */
const generateFunctionalEvalCsvRow = (output, metricResult) => {
    return [
        output?.id,
        output?.providerId === 'phone'
            ? output?.test?.vars?.prompt?.map((step) => step.value).join('\n') ?? 'Phone Call'
            : (0, exports.formatPromptForCsv)(output?.prompt),
        output?.providerId === 'phone' ? output?.response?.audio?.data : output?.text,
        output?.score !== undefined && output?.score !== null ? Number(output?.score).toFixed(2) : '',
        output?.pass ? 'Pass' : 'Fail',
        lodash_1.default.isEmpty(metricResult?.assertion?.displayLabel)
            ? metricResult?.assertion?.type
            : metricResult?.assertion?.displayLabel,
        metricResult?.pass ? 'Pass' : 'Fail',
        metricResult?.score !== undefined && metricResult?.score !== null
            ? Number(metricResult?.score).toFixed(2)
            : '',
        metricResult?.assertion?.value,
    ];
};
exports.generateFunctionalEvalCsvRow = generateFunctionalEvalCsvRow;
/**
 * Generate CSV row for Pulse redteam result
 */
const generatePulseRedteamCsvRow = (output, metricResult, getCategoryNameFn) => {
    const pluginId = output?.metadata?.pluginId;
    const categoryName = getCategoryNameFn
        ? getCategoryNameFn(pluginId)
        : (0, exports.getCategoryNameForPlugin)(pluginId);
    const strategyName = constants_1.displayNameOverrides[output?.metadata?.strategyId] ||
        'Baseline Testing';
    return [
        output?.sessionId ? 'Session' : 'Trace',
        output?.sessionId ? output?.traceIds?.join(',') : output?.traceId,
        output?.sessionId ?? '-',
        categoryName,
        strategyName,
        (0, exports.formatPromptForCsv)(output?.prompt),
        output?.response?.output,
        output?.score !== undefined && output?.score !== null ? Number(output?.score).toFixed(2) : '',
        output?.pass ? 'Pass' : 'Fail',
        metricResult?.reason,
    ];
};
exports.generatePulseRedteamCsvRow = generatePulseRedteamCsvRow;
/**
 * Generate CSV row for Pulse functional eval result
 */
const generatePulseFunctionalEvalCsvRow = (output, metricResult) => {
    return [
        output?.sessionId ? 'Session' : 'Trace',
        output?.sessionId ? output?.traceIds?.join(',') : output?.traceId,
        output?.sessionId ?? '-',
        lodash_1.default.isEmpty(metricResult?.assertion?.displayLabel)
            ? metricResult?.assertion?.type
            : metricResult?.assertion?.displayLabel,
        metricResult?.pass ? 'Pass' : 'Fail',
        metricResult?.score !== undefined && metricResult?.score !== null
            ? Number(metricResult?.score).toFixed(2)
            : '',
        (0, exports.formatPromptForCsv)(output?.prompt),
        output?.response?.output,
        output?.score !== undefined && output?.score !== null ? Number(output?.score).toFixed(2) : '',
        output?.pass ? 'Pass' : 'Fail',
        metricResult?.assertion?.value,
    ];
};
exports.generatePulseFunctionalEvalCsvRow = generatePulseFunctionalEvalCsvRow;
/**
 * Generate complete CSV data from table
 */
const generateCsvFromTable = (table, isRedteam = false) => {
    const csvRows = [];
    if (isRedteam) {
        table?.head?.columns?.forEach((column, index) => {
            // Add headers
            csvRows.push((0, exports.getRedteamCsvHeaders)());
            // Get outputs for this column
            const providerOutputs = table?.body
                ?.map((item) => item?.outputs?.filter((output, outputIdx) => index === outputIdx))
                .flat();
            // Add rows
            providerOutputs?.forEach((output) => {
                output?.gradingResult?.componentResults?.forEach((metricResult) => {
                    if (metricResult) {
                        csvRows.push((0, exports.generateRedteamCsvRow)(output, metricResult));
                    }
                });
            });
        });
    }
    else {
        table?.head?.columns?.forEach((column, index) => {
            // Add headers
            csvRows.push((0, exports.getFunctionalEvalCsvHeaders)(column?.provider));
            // Get outputs for this column
            const providerOutputs = table?.body
                ?.map((item) => item?.outputs?.filter((output, outputIdx) => index === outputIdx))
                .flat();
            // Add rows
            providerOutputs?.forEach((output) => {
                output?.gradingResult?.componentResults?.forEach((metricResult) => {
                    if (metricResult) {
                        csvRows.push((0, exports.generateFunctionalEvalCsvRow)(output, metricResult));
                    }
                });
            });
            // Add separators between columns (except last)
            if (index !== table.head.columns.length - 1) {
                csvRows.push(['', '', '', '', '']);
                csvRows.push(['', '', '', '', '']);
                csvRows.push(['--------------------------------', '', '', '', '']);
                csvRows.push(['', '', '', '', '']);
                csvRows.push(['', '', '', '', '']);
            }
        });
    }
    return csvRows;
};
exports.generateCsvFromTable = generateCsvFromTable;
/**
 * Generate CSV data from Pulse result list
 */
const generatePulseCsvFromResultList = (resultList, isRedteam = false, getCategoryNameFn) => {
    const csvRows = [];
    if (isRedteam) {
        // Add headers
        csvRows.push((0, exports.getPulseRedteamCsvHeaders)());
        // Add rows
        resultList?.forEach((output) => {
            output?.grading_result?.componentResults?.forEach((metricResult) => {
                if (metricResult) {
                    csvRows.push((0, exports.generatePulseRedteamCsvRow)(output, metricResult, getCategoryNameFn));
                }
            });
        });
    }
    else {
        // Add headers
        csvRows.push((0, exports.getPulseFunctionalEvalCsvHeaders)());
        // Add rows
        resultList?.forEach((output) => {
            output?.grading_result?.componentResults?.forEach((metricResult) => {
                if (metricResult) {
                    csvRows.push((0, exports.generatePulseFunctionalEvalCsvRow)(output, metricResult));
                }
            });
        });
    }
    return csvRows;
};
exports.generatePulseCsvFromResultList = generatePulseCsvFromResultList;
//# sourceMappingURL=csvGenerator.js.map