const require_logger = require('./logger-DXu5t4bD.cjs');
const require_types = require('./types-ykSCHP0x.cjs');
const require_time = require('./time-CbtsO5_a.cjs');
const require_utils = require('./utils-CjsFjkWW.cjs');
const require_extractor = require('./extractor-D-3T1AaH.cjs');
const require_tables = require('./tables-HmCWjdx2.cjs');
let drizzle_orm = require("drizzle-orm");

//#region src/models/evalResult.ts
function sanitizeProvider(provider) {
	try {
		if (require_types.isApiProvider(provider)) return {
			id: provider.id(),
			label: provider.label,
			...provider.config && { config: JSON.parse(require_logger.safeJsonStringify(provider.config)) }
		};
		if (require_types.isProviderOptions(provider)) return {
			id: provider.id,
			label: provider.label,
			...provider.config && { config: JSON.parse(require_logger.safeJsonStringify(provider.config)) }
		};
		if (typeof provider === "object" && provider) {
			const providerObj = provider;
			return {
				id: typeof providerObj.id === "function" ? providerObj.id() : providerObj.id,
				label: providerObj.label,
				...providerObj.config && { config: JSON.parse(require_logger.safeJsonStringify(providerObj.config)) }
			};
		}
	} catch {}
	return JSON.parse(require_logger.safeJsonStringify(provider));
}
var EvalResult = class EvalResult {
	static async createFromEvaluateResult(evalId, result, opts) {
		const persist = opts?.persist == null ? true : opts.persist;
		const { prompt, error, score, latencyMs, success, provider, gradingResult, namedScores, cost, metadata, failureReason, testCase } = result;
		const preSanitizeTestCase = {
			...testCase,
			...testCase.provider && { provider: sanitizeProvider(testCase.provider) }
		};
		const processedResponse = await require_extractor.extractAndStoreBinaryData(result.response, {
			evalId,
			testIdx: result.testIdx,
			promptIdx: result.promptIdx
		});
		const args = {
			id: crypto.randomUUID(),
			evalId,
			testCase: preSanitizeTestCase,
			promptIdx: result.promptIdx,
			testIdx: result.testIdx,
			prompt,
			promptId: require_utils.hashPrompt(prompt),
			error: error?.toString(),
			success,
			score: score == null ? 0 : score,
			response: processedResponse || null,
			gradingResult: gradingResult || null,
			namedScores,
			provider: sanitizeProvider(provider),
			latencyMs,
			cost,
			metadata,
			failureReason
		};
		if (persist) return new EvalResult({
			...(await require_tables.getDb().insert(require_tables.evalResultsTable).values(args).returning())[0],
			persisted: true
		});
		return new EvalResult(args);
	}
	static async createManyFromEvaluateResult(results, evalId) {
		const db = require_tables.getDb();
		const returnResults = [];
		const processedResults = [];
		for (const result of results) {
			const processedResponse = require_extractor.isBlobStorageEnabled() ? await require_extractor.extractAndStoreBinaryData(result.response, {
				evalId,
				testIdx: result.testIdx,
				promptIdx: result.promptIdx
			}) : result.response;
			processedResults.push({
				...result,
				response: processedResponse ?? void 0
			});
		}
		db.transaction(() => {
			for (const result of processedResults) {
				const dbResult = db.insert(require_tables.evalResultsTable).values({
					...result,
					evalId,
					id: crypto.randomUUID()
				}).returning().get();
				returnResults.push(new EvalResult({
					...dbResult,
					persisted: true
				}));
			}
		});
		return returnResults;
	}
	static async findById(id) {
		const result = await require_tables.getDb().select().from(require_tables.evalResultsTable).where((0, drizzle_orm.eq)(require_tables.evalResultsTable.id, id));
		return result.length > 0 ? new EvalResult({
			...result[0],
			persisted: true
		}) : null;
	}
	static async findManyByEvalId(evalId, opts) {
		return (await require_tables.getDb().select().from(require_tables.evalResultsTable).where((0, drizzle_orm.and)((0, drizzle_orm.eq)(require_tables.evalResultsTable.evalId, evalId), opts?.testIdx == null ? void 0 : (0, drizzle_orm.eq)(require_tables.evalResultsTable.testIdx, opts.testIdx)))).map((result) => new EvalResult({
			...result,
			persisted: true
		}));
	}
	static async findManyByEvalIdAndTestIndices(evalId, testIndices) {
		if (!testIndices.length) return [];
		return (await require_tables.getDb().select().from(require_tables.evalResultsTable).where((0, drizzle_orm.and)((0, drizzle_orm.eq)(require_tables.evalResultsTable.evalId, evalId), testIndices.length === 1 ? (0, drizzle_orm.eq)(require_tables.evalResultsTable.testIdx, testIndices[0]) : (0, drizzle_orm.inArray)(require_tables.evalResultsTable.testIdx, testIndices)))).map((result) => new EvalResult({
			...result,
			persisted: true
		}));
	}
	/**
	* Returns a set of completed (testIdx,promptIdx) pairs for a given eval.
	* Key format: `${testIdx}:${promptIdx}`
	*/
	static async getCompletedIndexPairs(evalId) {
		const rows = await require_tables.getDb().select({
			testIdx: require_tables.evalResultsTable.testIdx,
			promptIdx: require_tables.evalResultsTable.promptIdx
		}).from(require_tables.evalResultsTable).where((0, drizzle_orm.eq)(require_tables.evalResultsTable.evalId, evalId));
		const ret = /* @__PURE__ */ new Set();
		for (const r of rows) ret.add(`${r.testIdx}:${r.promptIdx}`);
		return ret;
	}
	static async *findManyByEvalIdBatched(evalId, opts) {
		const db = require_tables.getDb();
		const batchSize = opts?.batchSize || 100;
		let offset = 0;
		while (true) {
			const results = await db.select().from(require_tables.evalResultsTable).where((0, drizzle_orm.and)((0, drizzle_orm.eq)(require_tables.evalResultsTable.evalId, evalId), (0, drizzle_orm.gte)(require_tables.evalResultsTable.testIdx, offset), (0, drizzle_orm.lt)(require_tables.evalResultsTable.testIdx, offset + batchSize))).all();
			if (results.length === 0) break;
			yield results.map((result) => new EvalResult({
				...result,
				persisted: true
			}));
			offset += batchSize;
		}
	}
	id;
	evalId;
	description;
	promptIdx;
	testIdx;
	testCase;
	prompt;
	promptId;
	error;
	success;
	score;
	response;
	gradingResult;
	namedScores;
	provider;
	latencyMs;
	cost;
	metadata;
	failureReason;
	persisted;
	pluginId;
	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 || require_utils.hashPrompt(opts.prompt);
		this.error = opts.error;
		this.score = opts.score;
		this.success = opts.success;
		this.response = opts.response || void 0;
		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 = require_types.isResultFailureReason(opts.failureReason) ? opts.failureReason : require_types.ResultFailureReason.NONE;
		this.persisted = opts.persisted || false;
		this.pluginId = opts.testCase.metadata?.pluginId;
	}
	async save() {
		const db = require_tables.getDb();
		if (this.persisted) await db.update(require_tables.evalResultsTable).set({
			...this,
			updatedAt: require_time.getCurrentTimestamp()
		}).where((0, drizzle_orm.eq)(require_tables.evalResultsTable.id, this.id));
		else {
			this.id = (await db.insert(require_tables.evalResultsTable).values(this).returning())[0].id;
			this.persisted = true;
		}
	}
	toEvaluateResult() {
		const shouldStripPromptText = require_logger.getEnvBool("PROMPTFOO_STRIP_PROMPT_TEXT", false);
		const shouldStripResponseOutput = require_logger.getEnvBool("PROMPTFOO_STRIP_RESPONSE_OUTPUT", false);
		const shouldStripTestVars = require_logger.getEnvBool("PROMPTFOO_STRIP_TEST_VARS", false);
		const shouldStripGradingResult = require_logger.getEnvBool("PROMPTFOO_STRIP_GRADING_RESULT", false);
		const shouldStripMetadata = require_logger.getEnvBool("PROMPTFOO_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: void 0
		} : this.testCase;
		return {
			cost: this.cost,
			description: this.description || void 0,
			error: this.error || void 0,
			gradingResult: shouldStripGradingResult ? null : this.gradingResult,
			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,
			testCase,
			testIdx: this.testIdx,
			vars: shouldStripTestVars ? {} : this.testCase.vars || {},
			metadata: shouldStripMetadata ? {} : this.metadata,
			failureReason: this.failureReason
		};
	}
};

//#endregion
Object.defineProperty(exports, 'EvalResult', {
  enumerable: true,
  get: function () {
    return EvalResult;
  }
});
Object.defineProperty(exports, 'sanitizeProvider', {
  enumerable: true,
  get: function () {
    return sanitizeProvider;
  }
});
//# sourceMappingURL=evalResult-B_W-eS37.cjs.map