"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleContextRecall = void 0;
const logger_1 = __importDefault(require("../logger"));
const matchers_1 = require("../matchers");
const invariant_1 = __importDefault(require("../util/invariant"));
const handleContextRecall = async ({ assertion, renderedValue, prompt, 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;
    }
    let groundTruth = renderedValue;
    if (assertion?.groundTruth && typeof assertion?.groundTruth === 'string') {
        groundTruth = assertion?.groundTruth;
    }
    if (typeof output === 'object') {
        const parsedOutput = output;
        if (typeof parsedOutput.trusys === 'object' && parsedOutput.trusys.context) {
            vars.context = parsedOutput.trusys.context;
        }
        // delete parsedOutput.trusys;
    }
    logger_1.default.debug(`vars for ContextRecall: ${JSON.stringify(vars)}`);
    (0, invariant_1.default)(typeof groundTruth === 'string', 'context-recall assertion type must have a string value');
    (0, invariant_1.default)(prompt, 'context-recall assertion type must have a prompt');
    let threshold = Number(assertion?.threshold ?? 0.7);
    if (threshold == 0) {
        threshold = 0.7;
    }
    return {
        assertion,
        ...(await (0, matchers_1.matchesContextRecall)(typeof vars?.context === 'string' ? vars.context : prompt, groundTruth, threshold, test.options, vars, workspaceId)),
    };
};
exports.handleContextRecall = handleContextRecall;
//# sourceMappingURL=contextRecall.js.map