"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleContextFaithfulness = handleContextFaithfulness;
const matchers_1 = require("../matchers");
const invariant_1 = __importDefault(require("../util/invariant"));
const logger_1 = __importDefault(require("../logger"));
async function handleContextFaithfulness({ assertion, test, output, workspaceId, }) {
    const vars = { ...test?.vars ?? {} };
    if (assertion?.value && typeof assertion?.value === 'string') {
        vars.query = assertion?.value;
    }
    if (assertion?.context && typeof assertion?.context === 'string') {
        vars.context = assertion?.context;
    }
    if (typeof output === 'object') {
        const parsedOutput = output;
        if (typeof parsedOutput.trusys === 'object') {
            if (parsedOutput.trusys.query) {
                vars.query = parsedOutput.trusys.query;
            }
            if (parsedOutput.trusys.context) {
                vars.context = parsedOutput.trusys.context;
            }
            // delete parsedOutput.trusys;
        }
    }
    logger_1.default.debug(`vars for ContextFaithfulness: ${JSON.stringify(vars)}`);
    output = JSON.stringify(output);
    (0, invariant_1.default)(vars, 'context-faithfulness assertion type must have a vars object');
    (0, invariant_1.default)(typeof vars.query === 'string', 'context-faithfulness assertion type must have a query var');
    (0, invariant_1.default)(typeof vars.context === 'string', 'context-faithfulness assertion type must have a context var');
    (0, invariant_1.default)(typeof output === 'string', 'context-faithfulness assertion type must have a string output');
    let threshold = Number(assertion?.threshold ?? 0.3);
    if (threshold == 0) {
        threshold = 0.3;
    }
    return {
        assertion,
        ...(await (0, matchers_1.matchesContextFaithfulness)(vars.query, output, vars.context, threshold, test.options, vars, workspaceId)),
    };
}
//# sourceMappingURL=contextFaithfulness.js.map