"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleAgentCustomEval = handleAgentCustomEval;
const matchers_1 = require("../../matchers");
const invariant_1 = __importDefault(require("../../util/invariant"));
const logger_1 = __importDefault(require("../../logger"));
const util_1 = require("./util");
async function handleAgentCustomEval({ assertion, test, output, prompt, renderedValue, }) {
    logger_1.default.info(`agent-custom-eval, renderedValue: ${JSON.stringify(renderedValue)}`);
    (0, invariant_1.default)(Array.isArray(renderedValue) || typeof renderedValue === 'string', 'agent-custom-eval assertion type must have a string or array of strings value (criteria)');
    const criteria = Array.isArray(renderedValue)
        ? renderedValue.map((c) => {
            if (typeof c === 'string') {
                return c;
            }
            if (c && typeof c === 'object' && 'value' in c && typeof c.value === 'string') {
                return c.value;
            }
            return String(c);
        })
        : [renderedValue];
    logger_1.default.info(`criteria: ${JSON.stringify(criteria)}`);
    (0, invariant_1.default)(criteria.length > 0, 'agent-custom-eval requires at least one criterion in value');
    const vars = { ...(test?.vars ?? {}) };
    const { task, plan, trace, agentOutput, definitions } = (0, util_1.getResolvedTrace)(output, vars, prompt);
    const threshold = typeof assertion.threshold === 'number' ? assertion.threshold : 0.7;
    logger_1.default.debug(`agent-custom-eval: task length=${task.length}, criteria count=${criteria.length}, trace count=${Array.isArray(trace) ? trace.length : 'n/a'}`);
    (0, invariant_1.default)(typeof task === 'string', 'agent-custom-eval requires a task (prompt) in test.vars or output.trusys');
    return {
        assertion,
        ...(await (0, matchers_1.matchesAgentCustomEval)(task, plan, definitions, trace, agentOutput, criteria, test.options, threshold)),
    };
}
//# sourceMappingURL=agentCustomEval.js.map