"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestRunResult = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const database_1 = require("../database");
const tables_1 = require("../database/tables");
const envars_1 = require("../envars");
const types_1 = require("../types");
class TestRunResult {
    static async findManyByTestRunId(testRunId, filter) {
        const db = (0, database_1.getDb)();
        // First get all eval IDs associated with this test run
        const testRunEvals = await db
            .select()
            .from(tables_1.testRunsToEvalsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.testRunsToEvalsTable.testRunId, testRunId));
        const evalIds = testRunEvals.map((rel) => rel.evalId);
        if (evalIds.length === 0) {
            return [];
        }
        // Build where conditions
        const conditions = [(0, drizzle_orm_1.inArray)(tables_1.evalResultsTable.evalId, evalIds)];
        // Apply filter if provided and not 'all'
        if (filter && filter !== 'all') {
            if (filter === 'errors') {
                // Filter for errors: success = 0 (false) and failureReason = 2
                conditions.push((0, drizzle_orm_1.eq)(tables_1.evalResultsTable.success, 0));
                conditions.push((0, drizzle_orm_1.eq)(tables_1.evalResultsTable.failureReason, types_1.ResultFailureReason.ERROR));
            }
            else if (filter === 'failures') {
                // Filter for failures: success = 0 (false) and failureReason != 2
                conditions.push((0, drizzle_orm_1.eq)(tables_1.evalResultsTable.success, 0));
                conditions.push((0, drizzle_orm_1.ne)(tables_1.evalResultsTable.failureReason, types_1.ResultFailureReason.ERROR));
            }
        }
        // Then get all results for these evals with filter applied
        const results = await db
            .select()
            .from(tables_1.evalResultsTable)
            .where((0, drizzle_orm_1.and)(...conditions));
        let filteredResults = results ?? [];
        if (filter === 'reviewed') {
            filteredResults = filteredResults.filter((result) => Boolean(result?.gradingResult?.reviewedBy));
        }
        else if (filter === 'unreviewed') {
            filteredResults = filteredResults.filter((result) => Boolean(result?.gradingResult?.reviewedBy) === false);
        }
        return filteredResults.map((result) => new TestRunResult({ ...result, persisted: true }));
    }
    constructor(opts) {
        this.id = opts.id;
        this.evalId = opts.evalId;
        this.promptIdx = opts.promptIdx;
        this.testIdx = opts.testIdx;
        this.testCase = opts.testCase;
        this.prompt = opts.prompt;
        this.promptId = opts.promptId;
        this.error = opts.error;
        this.vars = opts.vars || {};
        this.success = opts.success;
        this.score = opts.score;
        this.response = opts.response;
        this.gradingResult = opts.gradingResult;
        this.namedScores = opts.namedScores || {};
        this.provider = opts.provider;
        this.latencyMs = opts.latencyMs || 0;
        this.cost = opts.cost || 0;
        this.metadata = opts.metadata || {};
        this.failureReason = opts.failureReason;
        this.persisted = opts.persisted || false;
    }
    async save() {
        const db = (0, database_1.getDb)();
        const resultData = {
            ...this,
            updatedAt: (0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP`,
        };
        if (this.persisted) {
            await db.update(tables_1.evalResultsTable).set(resultData).where((0, drizzle_orm_1.eq)(tables_1.evalResultsTable.id, this.id));
        }
        else {
            const result = await db.insert(tables_1.evalResultsTable).values(resultData).returning();
            this.id = result[0].id;
            this.persisted = true;
        }
    }
    toEvaluateResult() {
        const shouldStripPromptText = (0, envars_1.getEnvBool)('TRUSYS_STRIP_PROMPT_TEXT', false);
        const shouldStripResponseOutput = (0, envars_1.getEnvBool)('TRUSYS_STRIP_RESPONSE_OUTPUT', false);
        const shouldStripTestVars = (0, envars_1.getEnvBool)('TRUSYS_STRIP_TEST_VARS', false);
        const shouldStripGradingResult = (0, envars_1.getEnvBool)('TRUSYS_STRIP_GRADING_RESULT', false);
        const shouldStripMetadata = (0, envars_1.getEnvBool)('TRUSYS_STRIP_METADATA', false);
        const response = shouldStripResponseOutput && this.response
            ? {
                ...this.response,
                output: '[output stripped]',
            }
            : this.response;
        const prompt = shouldStripPromptText
            ? {
                ...this.prompt,
                raw: '[prompt stripped]',
            }
            : this.prompt;
        const testCase = shouldStripTestVars
            ? {
                ...this.testCase,
                vars: undefined,
            }
            : this.testCase;
        return {
            cost: this.cost,
            error: this.error || undefined,
            gradingResult: shouldStripGradingResult ? null : this.gradingResult,
            evalId: this.evalId,
            id: this.id,
            latencyMs: this.latencyMs,
            namedScores: this.namedScores,
            prompt,
            promptId: this.promptId,
            promptIdx: this.promptIdx,
            provider: { id: this.provider.id, label: this.provider.label },
            response,
            score: this.score,
            success: this.success === 1,
            testCase,
            testIdx: this.testIdx,
            vars: shouldStripTestVars ? {} : this.testCase.vars || {},
            metadata: shouldStripMetadata ? {} : this.metadata,
            failureReason: this.failureReason,
        };
    }
}
exports.TestRunResult = TestRunResult;
//# sourceMappingURL=testRunResult.js.map