"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleContextRelevance = void 0;
const logger_1 = __importDefault(require("../logger"));
const matchers_1 = require("../matchers");
const invariant_1 = __importDefault(require("../util/invariant"));
const handleContextRelevance = async ({ 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 ContextRelevance: ${JSON.stringify(vars)}`);
    (0, invariant_1.default)(vars, 'context-relevance assertion type must have a vars object');
    (0, invariant_1.default)(typeof vars.query === 'string', 'context-relevance assertion type must have a query var');
    (0, invariant_1.default)(typeof vars.context === 'string', 'context-relevance assertion type must have a context var');
    let threshold = Number(assertion?.threshold ?? 0.3);
    if (threshold == 0) {
        threshold = 0.3;
    }
    return {
        assertion,
        ...(await (0, matchers_1.matchesContextRelevance)(vars.query, vars.context, threshold, test.options, workspaceId)),
    };
};
exports.handleContextRelevance = handleContextRelevance;
//# sourceMappingURL=contextRelevance.js.map