"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleRoleAdherence = 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 executeAndGradeRoleAdherence(llmOutput, modelProvider, providerType, context) {
    const pythonScriptPath = path_1.default.join(__dirname, 'python', 'role_adherence_eval.py');
    const contextJson = JSON.stringify(context);
    const jsonPromise = new Promise((resolve, reject) => {
        const py = (0, child_process_1.spawn)('python3', [pythonScriptPath, llmOutput, JSON.stringify(modelProvider), providerType], {
            env: process.env,
            stdio: ['pipe', 'pipe', 'pipe'],
        });
        let stdout = '', stderr = '';
        py.stdin.write(contextJson);
        py.stdin.end();
        py.stdout.on('data', (chunk) => {
            stdout += chunk.toString();
        });
        py.stderr.on('data', (chunk) => {
            stderr += chunk.toString();
        });
        py.on('close', (code) => {
            if (code === 0) {
                resolve(stdout.trim());
            }
            else {
                reject(new Error(`Python script failed with exit code ${code}\nStderr: ${stderr}`));
            }
        });
        py.on('error', (err) => {
            reject(new Error(`Failed to spawn Python process: ${err.message}`));
        });
    });
    try {
        return JSON.parse(await jsonPromise);
    }
    catch (e) {
        return {
            pass: false,
            score: 0,
            reason: `evaluation failed: ${e instanceof Error ? e.message : 'unknown error'}`,
        };
    }
}
const handleRoleAdherence = async ({ assertion, outputString, test, context, workspaceId, }) => {
    (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, 'role-adherence');
    const providerType = textProvider.constructor.name;
    const modelProvider = textProvider;
    const contextForPython = context;
    const grading = await executeAndGradeRoleAdherence(outputString, modelProvider, providerType, contextForPython);
    return {
        assertion,
        pass: grading.pass,
        score: grading.score,
        reason: grading.reason,
    };
};
exports.handleRoleAdherence = handleRoleAdherence;
//# sourceMappingURL=handleRoleAdherence.js.map