"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleMultimodalContextualRecall = void 0;
const child_process_1 = require("child_process");
const path_1 = __importDefault(require("path"));
const matchers_1 = require("../matchers");
const defaults_1 = require("../providers/defaults");
const invariant_1 = __importDefault(require("../util/invariant"));
async function executeAndGradeMultimodalRecallEval(llmOutput, context, modelProvider, providerType, renderedValue) {
    const pythonScriptPath = path_1.default.join(__dirname, 'python', 'multimodal_contextual_recall_eval.py');
    const contextJson = JSON.stringify({
        ...context,
        options: {
            provider: modelProvider,
        },
    });
    const jsonPromise = new Promise((resolve, reject) => {
        const py = (0, child_process_1.spawn)('python', [
            pythonScriptPath,
            llmOutput,
            JSON.stringify(modelProvider),
            providerType,
            JSON.stringify(renderedValue),
        ], {
            env: process.env,
            stdio: ['pipe', 'pipe', 'pipe'],
        });
        let stdoutData = '';
        let stderrData = '';
        py.stdin.write(contextJson);
        py.stdin.end();
        py.stdout.on('data', (chunk) => (stdoutData += chunk.toString()));
        py.stderr.on('data', (chunk) => {
            stderrData += chunk.toString();
            console.error('[multimodal_contextual_recall_eval.py stderr]:', chunk.toString());
        });
        py.on('close', (code) => {
            if (code === 0 && stdoutData.trim()) {
                resolve(stdoutData.trim());
            }
            else {
                reject(new Error(`Python exited with code ${code}. Stdout: ${stdoutData.trim()} Stderr: ${stderrData.trim()}`));
            }
        });
        py.on('error', (err) => {
            reject(new Error(`Failed to start Python: ${err.message}`));
        });
    });
    try {
        return JSON.parse(await jsonPromise);
    }
    catch (e) {
        console.error('Failed to parse Python output:', e.message);
        return {
            pass: false,
            score: 0,
            reason: 'evaluation failed: invalid JSON from Python',
        };
    }
}
const handleMultimodalContextualRecall = async ({ assertion, outputString, test, context, renderedValue, workspaceId, }) => {
    (0, invariant_1.default)(typeof renderedValue === 'string' || Array.isArray(renderedValue), 'G-Eval assertion type must have a string or array of strings value');
    (0, invariant_1.default)(typeof outputString === 'string', 'output must be string');
    const textProvider = await (0, matchers_1.getAndCheckProvider)('text', test.options?.provider, (await (0, defaults_1.getDefaultProviders)(undefined, workspaceId)).gradingProvider, 'multimodal-contextual-recall');
    const providerType = textProvider.constructor.name;
    console.log('MultimodalContextualPrecision provider ID:', textProvider);
    console.log('MultimodalContextualPrecision Construction name:', providerType);
    console.log('Rendered Value:', renderedValue);
    const contextForPython = {
        context,
        metricConfig: {
            name: assertion.name || 'Multimodal Contextual Recall',
            value: assertion.value, // expected_output is taken from assertion.value
            threshold: typeof assertion.threshold === 'number' ? assertion.threshold : 0.7,
        },
    };
    console.log('Context for Python: ', contextForPython);
    const result = await executeAndGradeMultimodalRecallEval(outputString, contextForPython, textProvider, providerType, renderedValue);
    return {
        assertion,
        pass: result.pass,
        score: result.score,
        reason: result.reason,
    };
};
exports.handleMultimodalContextualRecall = handleMultimodalContextualRecall;
//# sourceMappingURL=handleMultimodalContextualRecall.js.map