"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runPythonDocumentDatasetSynthesis = runPythonDocumentDatasetSynthesis;
const node_child_process_1 = require("node:child_process");
const decimal_js_1 = __importDefault(require("decimal.js"));
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const cleanString = (str) => {
    if (typeof str === 'string') {
        return str.replace(/\u0000/g, '').replace(/[\x00-\x08\x0B-\x0C\x0E-\x1F]/g, '');
    }
    if (Array.isArray(str)) {
        return str.map(cleanString);
    }
    if (str && typeof str === 'object') {
        const cleaned = {};
        for (const [key, value] of Object.entries(str)) {
            cleaned[key] = cleanString(value);
        }
        return cleaned;
    }
    return str;
};
const findPythonExecutable = () => {
    const isWindows = process.platform === 'win32';
    const findPythonInPath = (command) => {
        if (!isWindows) {
            return null;
        }
        const pathEnv = process.env.PATH || '';
        const paths = pathEnv.split(path.delimiter);
        for (const dir of paths) {
            const pythonPath = path.join(dir, `${command}.exe`);
            if (fs.existsSync(pythonPath)) {
                return pythonPath;
            }
        }
        return null;
    };
    const possiblePythonPaths = isWindows
        ? ['./venv/Scripts/python.exe', './venv/Scripts/python']
        : ['./venv/bin/python3', './venv/bin/python'];
    for (const pythonPath of possiblePythonPaths) {
        const fullPath = path.resolve(__dirname, '../../../../', pythonPath);
        if (fs.existsSync(fullPath)) {
            return fullPath;
        }
    }
    if (isWindows) {
        const found = findPythonInPath('python') || findPythonInPath('python3') || findPythonInPath('py');
        if (found) {
            return found;
        }
    }
    else {
        const unixPaths = [
            '/usr/local/bin/python3.10',
            '/usr/bin/python3.10',
            '/usr/local/bin/python3',
            '/usr/bin/python3',
            '/usr/bin/python',
        ];
        for (const unixPath of unixPaths) {
            if (fs.existsSync(unixPath)) {
                return unixPath;
            }
        }
    }
    return isWindows ? 'python' : 'python3.10';
};
/**
 * Runs `generate_dataset_from_docs.py` with local file paths. Does not delete input paths.
 */
function runPythonDocumentDatasetSynthesis(params) {
    const { documentPaths, includeExpectedOutput, numSamples, previousRowsJsonPath = '' } = params;
    return new Promise((resolve) => {
        const pythonScriptPath = path.join(__dirname, '../../../python/generate_dataset_from_docs.py');
        if (!fs.existsSync(pythonScriptPath)) {
            resolve({
                success: false,
                testCaseVars: [],
                totalRows: 0,
                tokenUsages: {
                    cost: new decimal_js_1.default(0),
                    tokenUsage: {
                        prompt: new decimal_js_1.default(0),
                        completion: new decimal_js_1.default(0),
                        total: new decimal_js_1.default(0),
                        cached: new decimal_js_1.default(0),
                    },
                },
                error: `Python script not found at ${pythonScriptPath}`,
            });
            return;
        }
        const documentPathsJson = JSON.stringify(documentPaths);
        const includeExpectedOutputStr = includeExpectedOutput ? 'true' : 'false';
        const maxGoldensTotal = Math.max(1, Math.floor(numSamples));
        const maxGoldensPerContext = Math.max(2, Math.ceil(maxGoldensTotal / Math.max(1, documentPaths.length)));
        const pythonExecutable = findPythonExecutable();
        const needsShell = !path.isAbsolute(pythonExecutable);
        const args = [
            pythonScriptPath,
            documentPathsJson,
            includeExpectedOutputStr,
            String(maxGoldensPerContext),
            String(maxGoldensTotal),
            previousRowsJsonPath && previousRowsJsonPath.length > 0 ? previousRowsJsonPath : '',
        ];
        let outputData = '';
        let errorData = '';
        const datasetTokenUsage = {
            cost: new decimal_js_1.default(0),
            tokenUsage: {
                prompt: new decimal_js_1.default(0),
                completion: new decimal_js_1.default(0),
                total: new decimal_js_1.default(0),
                cached: new decimal_js_1.default(0),
            },
        };
        const costRegex = /API cost:\s*(\d+\.\d+)/i;
        const pythonProcess = (0, node_child_process_1.spawn)(pythonExecutable, args, {
            env: {
                ...process.env,
                PYTHONPATH: process.env.PYTHONPATH || '',
                PATH: process.env.PATH || '',
            },
            shell: needsShell,
            windowsHide: true,
        });
        pythonProcess.on('error', (error) => {
            resolve({
                success: false,
                testCaseVars: [],
                totalRows: 0,
                tokenUsages: datasetTokenUsage,
                error: error.message,
            });
        });
        pythonProcess.stdout.on('data', (data) => {
            const strData = data.toString();
            const costMatch = strData.match(costRegex);
            if (costMatch?.[1]) {
                datasetTokenUsage.cost = new decimal_js_1.default(costMatch[1]);
            }
            outputData += strData;
        });
        pythonProcess.stderr.on('data', (data) => {
            const strData = data.toString();
            const costMatch = strData.match(costRegex);
            if (costMatch?.[1]) {
                datasetTokenUsage.cost = new decimal_js_1.default(costMatch[1]);
            }
            errorData += strData;
        });
        pythonProcess.on('close', (code) => {
            const allOutput = outputData + errorData;
            const costMatch = allOutput.match(costRegex);
            if (costMatch?.[1]) {
                datasetTokenUsage.cost = new decimal_js_1.default(costMatch[1]);
            }
            if (code !== 0) {
                resolve({
                    success: false,
                    testCaseVars: [],
                    totalRows: 0,
                    tokenUsages: datasetTokenUsage,
                    error: errorData || `Python exited with code ${code}`,
                    rawOutput: outputData,
                });
                return;
            }
            try {
                const matches = [...outputData.matchAll(/\{[\s\S]*\}$/gm)];
                const lastJson = matches.length > 0 ? matches[matches.length - 1][0] : null;
                if (!lastJson) {
                    resolve({
                        success: false,
                        testCaseVars: [],
                        totalRows: 0,
                        tokenUsages: datasetTokenUsage,
                        error: 'No valid JSON found in Python output',
                        rawOutput: outputData,
                    });
                    return;
                }
                const result = JSON.parse(lastJson);
                if (!result.success) {
                    resolve({
                        success: false,
                        testCaseVars: [],
                        totalRows: 0,
                        tokenUsages: datasetTokenUsage,
                        error: result.error || 'Dataset generation failed',
                        rawOutput: outputData,
                    });
                    return;
                }
                const data = Array.isArray(result.data) ? result.data : [];
                const testCaseVars = data.map((item, index) => cleanString({
                    prompt: item.prompt || `Generated prompt ${index + 1}`,
                    expected_output: item.expected_output || `Generated output ${index + 1}`,
                    ...item,
                }));
                resolve({
                    success: true,
                    testCaseVars,
                    totalRows: result.total_rows ?? testCaseVars.length,
                    tokenUsages: datasetTokenUsage,
                });
            }
            catch (parseError) {
                resolve({
                    success: false,
                    testCaseVars: [],
                    totalRows: 0,
                    tokenUsages: datasetTokenUsage,
                    error: parseError instanceof Error ? parseError.message : 'Parse error',
                    rawOutput: outputData,
                });
            }
        });
    });
}
//# sourceMappingURL=runPythonDocumentDatasetSynthesis.js.map