import { t as invariant } from "./invariant-D3duWnAm.js";
import * as path$1 from "path";
import path from "path";
import yaml from "js-yaml";
import { AsyncLocalStorage } from "node:async_hooks";
import dotenv from "dotenv";
import fs from "fs";
import chalk from "chalk";
import winston from "winston";
import * as os$1 from "os";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import safeStringify from "fast-safe-stringify";
//#region src/envOverrides.ts
/**
* Module-level singleton; last-writer-wins. Scoped to the current process —
* not propagated to `worker_threads` or child processes, which must register
* their own provider (typically by importing `./cliState`).
*
* Kept dependency-free on purpose: this module is reachable from the bottom
* of the import graph (via `envars`), so adding imports here risks circular
* cycles with logger / cliState.
*/
let envOverridesProvider;
function setEnvOverridesProvider(provider) {
	envOverridesProvider = provider;
}
/**
* Returns the current env overrides snapshot, or `undefined` if no provider is
* registered. Swallows provider exceptions to preserve the invariant that
* `getEnvString` (and its delegates `getEnvBool` / `getEnvInt` / etc.) never
* throw on environment access — relied on by ~148 call sites.
*/
function getEnvOverrides() {
	if (!envOverridesProvider) return;
	try {
		return envOverridesProvider();
	} catch {
		return;
	}
}
//#endregion
//#region src/cliState.ts
const maxConcurrencyContext = new AsyncLocalStorage();
let globalMaxConcurrency;
const state = {
	get maxConcurrency() {
		const store = maxConcurrencyContext.getStore();
		if (store) return store.maxConcurrency;
		return globalMaxConcurrency;
	},
	set maxConcurrency(value) {
		const store = maxConcurrencyContext.getStore();
		if (store) {
			store.maxConcurrency = value;
			return;
		}
		globalMaxConcurrency = value;
	},
	withMaxConcurrency(maxConcurrency, fn) {
		return maxConcurrencyContext.run({ maxConcurrency }, fn);
	}
};
setEnvOverridesProvider(() => state.config?.env);
//#endregion
//#region src/envars.ts
dotenv.config({ quiet: true });
function getEnvString(key, defaultValue) {
	const envOverrides = getEnvOverrides();
	if (envOverrides) {
		const envValue = envOverrides[key];
		if (envValue !== void 0) return String(envValue);
	}
	const value = process.env[key];
	if (value === void 0) return defaultValue;
	return value;
}
/**
* Get a boolean environment variable.
* @param key The name of the environment variable.
* @param defaultValue Optional default value if the environment variable is not set.
* @returns The boolean value of the environment variable, or the default value if provided.
*/
function getEnvBool(key, defaultValue) {
	const value = getEnvString(key) || defaultValue;
	if (typeof value === "boolean") return value;
	if (typeof value === "string") return [
		"1",
		"true",
		"yes",
		"yup",
		"yeppers"
	].includes(value.toLowerCase());
	return Boolean(defaultValue);
}
function getEnvInt(key, defaultValue) {
	const str = getEnvString(key);
	if (str === void 0 || str === "") return defaultValue;
	const parsedValue = Number.parseInt(str, 10);
	if (!Number.isNaN(parsedValue)) return parsedValue;
	return defaultValue;
}
function getEnvFloat(key, defaultValue) {
	const str = getEnvString(key);
	if (str === void 0 || str === "") return defaultValue;
	const parsedValue = Number.parseFloat(str);
	if (!Number.isNaN(parsedValue)) return parsedValue;
	return defaultValue;
}
/**
* Get the timeout in milliseconds for each individual test case/provider API call.
* When this timeout is reached, that specific test is marked as an error.
* @param defaultValue Optional default value if the environment variable is not set. Defaults to 0 (no timeout).
* @returns The timeout value in milliseconds, or the default value if not set.
*/
function getEvalTimeoutMs(defaultValue = 0) {
	return getEnvInt("PROMPTFOO_EVAL_TIMEOUT_MS", defaultValue);
}
/**
* Get the maximum total runtime in milliseconds for the entire evaluation process.
* When this timeout is reached, all remaining tests are marked as errors and the evaluation ends.
* @param defaultValue Optional default value if the environment variable is not set. Defaults to 0 (no limit).
* @returns The max duration in milliseconds, or the default value if not set.
*/
function getMaxEvalTimeMs(defaultValue = 0) {
	return getEnvInt("PROMPTFOO_MAX_EVAL_TIME_MS", defaultValue);
}
/**
* Check if the application is running in a CI environment.
* @returns True if running in a CI environment, false otherwise.
*/
function isCI() {
	return getEnvBool("CI") || getEnvBool("GITHUB_ACTIONS") || getEnvBool("TRAVIS") || getEnvBool("CIRCLECI") || getEnvBool("JENKINS") || getEnvBool("GITLAB_CI") || getEnvBool("APPVEYOR") || getEnvBool("CODEBUILD_BUILD_ID") || getEnvBool("TF_BUILD") || getEnvBool("BITBUCKET_COMMIT") || getEnvBool("BUDDY") || getEnvBool("BUILDKITE") || getEnvBool("TEAMCITY_VERSION");
}
//#endregion
//#region src/util/config/manage.ts
let configDirectoryPath = getEnvString("PROMPTFOO_CONFIG_DIR");
const isNodeEnvironment = typeof process !== "undefined" && process.versions && process.versions.node;
function getConfigDirectoryPath(createIfNotExists = false) {
	const p = configDirectoryPath || path$1.join(os$1.homedir(), ".promptfoo");
	if (createIfNotExists && isNodeEnvironment) try {
		fs.mkdirSync(p, { recursive: true });
	} catch {}
	return p;
}
//#endregion
//#region src/util/json.ts
let ajvInstance = null;
function getAjv() {
	if (!ajvInstance) {
		ajvInstance = new Ajv({ strictSchema: !getEnvBool("PROMPTFOO_DISABLE_AJV_STRICT_MODE") });
		addFormats(ajvInstance);
	}
	return ajvInstance;
}
function isValidJson(str) {
	try {
		JSON.parse(str);
		return true;
	} catch {
		return false;
	}
}
/**
* Creates a truncated version of an object for safe JSON stringification.
* Prevents memory issues by limiting string, array, and object sizes.
*
* @param value - The value to truncate and stringify
* @param prettyPrint - Whether to format the JSON with indentation
* @returns A JSON string representation of the truncated value
*/
function safeJsonStringifyTruncated(value, prettyPrint = false) {
	const cache = /* @__PURE__ */ new Set();
	const space = prettyPrint ? 2 : void 0;
	const truncateValue = (val) => {
		if (typeof val === "string") return val.length > 1e3 ? val.substring(0, 1e3) + "...[truncated]" : val;
		if (Array.isArray(val)) {
			const truncated = val.slice(0, 10).map(truncateValue);
			if (val.length > 10) truncated.push(`...[${val.length - 10} more items]`);
			return truncated;
		}
		if (typeof val === "object" && val !== null) {
			if (cache.has(val)) return "[Circular Reference]";
			cache.add(val);
			const truncated = {};
			let count = 0;
			for (const [k, v] of Object.entries(val)) {
				if (count >= 20) {
					truncated["...[truncated]"] = `${Object.keys(val).length - count} more keys`;
					break;
				}
				truncated[k] = truncateValue(v);
				count++;
			}
			cache.delete(val);
			return truncated;
		}
		return val;
	};
	try {
		return JSON.stringify(truncateValue(value), null, space) || "{}";
	} catch {
		return `{"error": "Failed to stringify even truncated data", "type": "${typeof value}", "constructor": "${value?.constructor?.name || "unknown"}"}`;
	}
}
/**
* Safely stringify a value to JSON, handling circular references and large objects.
*
* @param value - The value to stringify
* @param prettyPrint - Whether to format the JSON with indentation
* @returns JSON string representation, or undefined if serialization fails
*/
function safeJsonStringify(value, prettyPrint = false) {
	const ancestors = [];
	const space = prettyPrint ? 2 : void 0;
	try {
		return JSON.stringify(value, function(_key, val) {
			if (typeof val === "object" && val !== null) {
				while (ancestors.length > 0 && ancestors[ancestors.length - 1] !== this) ancestors.pop();
				if (ancestors.includes(val)) return;
				ancestors.push(val);
			}
			return val;
		}, space) || void 0;
	} catch (error) {
		if (error instanceof RangeError && error.message.includes("Invalid string length")) return safeJsonStringifyTruncated(value, prettyPrint);
		return;
	}
}
function convertSlashCommentsToHash(str) {
	return str.split("\n").map((line) => {
		let state = "normal";
		let result = "";
		let i = 0;
		while (i < line.length) {
			const char = line[i];
			const nextChar = line[i + 1];
			const prevChar = i > 0 ? line[i - 1] : "";
			switch (state) {
				case "normal":
					if (char === "'" && !/[a-zA-Z]/.test(prevChar)) {
						state = "singleQuote";
						result += char;
					} else if (char === "\"") {
						state = "doubleQuote";
						result += char;
					} else if (char === "/" && nextChar === "/") {
						let tokenStart = 0;
						for (let j = i - 1; j >= 0; j--) if (/\s/.test(line[j])) {
							tokenStart = j + 1;
							break;
						}
						if (line.slice(tokenStart, i + 2).includes("://")) {
							result += char;
							break;
						}
						let slashCount = 2;
						while (i + slashCount < line.length && line[i + slashCount] === "/") slashCount++;
						const hashes = "#".repeat(Math.floor(slashCount / 2));
						return result + hashes + line.slice(i + slashCount);
					} else result += char;
					break;
				case "singleQuote":
					result += char;
					if (char === "'" && prevChar !== "\\" && !/[a-zA-Z]/.test(nextChar)) state = "normal";
					break;
				case "doubleQuote":
					result += char;
					if (char === "\"" && prevChar !== "\\") state = "normal";
					break;
			}
			i++;
		}
		return result;
	}).join("\n");
}
function extractJsonObjects(str) {
	const jsonObjects = [];
	const maxJsonLength = 1e5;
	for (let i = 0; i < str.length; i++) if (str[i] === "{") {
		let openBraces = 1;
		let closeBraces = 0;
		let j = i + 1;
		while (j < Math.min(i + maxJsonLength, str.length) && openBraces > closeBraces) {
			if (str[j] === "{") openBraces++;
			if (str[j] === "}") closeBraces++;
			j++;
			if (openBraces === closeBraces || j === str.length || j === i + maxJsonLength) try {
				let potentialJson = str.slice(i, j);
				if (openBraces > closeBraces) potentialJson += "}".repeat(openBraces - closeBraces);
				const processedJson = convertSlashCommentsToHash(potentialJson);
				const parsedObj = yaml.load(processedJson, { json: true });
				if (typeof parsedObj === "object" && parsedObj !== null) {
					jsonObjects.push(parsedObj);
					i = j - 1;
					break;
				}
			} catch {
				if (openBraces === closeBraces) break;
			}
		}
	}
	return jsonObjects;
}
function extractFirstJsonObject(str) {
	const jsonObjects = extractJsonObjects(str);
	invariant(jsonObjects.length >= 1, `Expected a JSON object, but got ${JSON.stringify(str)}`);
	return jsonObjects[0];
}
/**
* Reorders the keys of an object based on a specified order, preserving any unspecified keys.
* Symbol keys are preserved and added at the end.
*
* @param obj - The object whose keys need to be reordered.
* @param order - An array specifying the desired order of keys.
* @returns A new object with keys reordered according to the specified order.
*
* @example
* const obj = { c: 3, a: 1, b: 2 };
* const orderedObj = orderKeys(obj, ['a', 'b']);
* // Result: { a: 1, b: 2, c: 3 }
*/
function orderKeys(obj, order) {
	const result = {};
	for (const key of order) if (key in obj && obj[key] !== void 0) result[key] = obj[key];
	for (const key in obj) if (!(key in result) && obj[key] !== void 0) result[key] = obj[key];
	const symbolKeys = Object.getOwnPropertySymbols(obj);
	for (const sym of symbolKeys) if (obj[sym] !== void 0) result[sym] = obj[sym];
	return result;
}
/**
* Creates a summary of an EvaluateResult for logging purposes, avoiding RangeError
* when stringifying large evaluation results.
*
* Extracts key information while truncating potentially large fields like response
* outputs and metadata values.
*
* @param result - The evaluation result to summarize
* @param maxOutputLength - Maximum length for response output before truncation. Default: 500
* @param includeMetadataKeys - Whether to include metadata keys in the summary. Default: true
* @returns A summarized version safe for JSON stringification
* @throws {TypeError} If result is null or undefined
*/
function summarizeEvaluateResultForLogging(result, maxOutputLength = 500, includeMetadataKeys = true) {
	if (!result) throw new TypeError("EvaluateResult cannot be null or undefined");
	const summary = {
		id: result.id,
		testIdx: result.testIdx,
		promptIdx: result.promptIdx,
		success: result.success,
		score: result.score,
		error: result.error,
		failureReason: result.failureReason
	};
	if (result.provider) summary.provider = {
		id: result.provider.id || "",
		label: result.provider.label
	};
	if (result.response) {
		summary.response = {
			error: result.response.error,
			cached: result.response.cached,
			cost: result.response.cost,
			tokenUsage: result.response.tokenUsage
		};
		if (result.response.output != null) {
			const output = String(result.response.output);
			summary.response.output = output.length > maxOutputLength ? output.substring(0, maxOutputLength) + "...[truncated]" : output;
		}
		if (result.response.metadata && includeMetadataKeys) summary.response.metadata = {
			keys: Object.keys(result.response.metadata),
			keyCount: Object.keys(result.response.metadata).length
		};
	}
	if (result.testCase) summary.testCase = {
		description: result.testCase.description,
		vars: result.testCase.vars ? Object.keys(result.testCase.vars) : void 0
	};
	return summary;
}
//#endregion
//#region src/util/sanitizer.ts
/**
* Generic utility functions for sanitizing objects to prevent logging of secrets and credentials
* Uses a custom recursive approach for reliable deep object sanitization.
*/
const MAX_DEPTH = 4;
const DUMMY_BASE = "http://placeholder";
const REDACTED = "[REDACTED]";
/**
* Set of field names that should be redacted (case-insensitive, with hyphens/underscores normalized)
* Note: Keys are stored in their normalized form (lowercase, no hyphens/underscores)
*/
const SECRET_FIELD_NAMES = new Set([
	"password",
	"passwd",
	"pwd",
	"secret",
	"secrets",
	"secretkey",
	"credentials",
	"apikey",
	"apisecret",
	"token",
	"accesstoken",
	"refreshtoken",
	"idtoken",
	"bearertoken",
	"authtoken",
	"clientsecret",
	"webhooksecret",
	"anthropicapikey",
	"awsbearertokenbedrock",
	"authorization",
	"auth",
	"bearer",
	"apikeyenvar",
	"xapikey",
	"xauthtoken",
	"xaccesstoken",
	"xauth",
	"xsecret",
	"xcsrftoken",
	"xsessiondata",
	"csrftoken",
	"sessionid",
	"session",
	"cookie",
	"setcookie",
	"certificatepassword",
	"keystorepassword",
	"pfxpassword",
	"privatekey",
	"certkey",
	"encryptionkey",
	"signingkey",
	"signature",
	"sig",
	"passphrase",
	"certificatecontent",
	"keystorecontent",
	"pfx",
	"pfxcontent",
	"keycontent",
	"certcontent"
]);
/**
* Normalize field names for comparison (lowercase, no hyphens/underscores)
*/
function normalizeFieldName(fieldName) {
	return fieldName.toLowerCase().replace(/[-_]/g, "");
}
/**
* Check if a field name should be redacted
*/
function isSecretField(fieldName) {
	return SECRET_FIELD_NAMES.has(normalizeFieldName(fieldName));
}
/**
* Sanitizes runtime options to ensure only JSON-serializable data is persisted or exported.
* Removes non-serializable fields like AbortSignal, functions, and symbols.
*/
function sanitizeRuntimeOptions(options) {
	if (!options) return;
	const sanitized = { ...options };
	delete sanitized.abortSignal;
	const sanitizedEntries = sanitized;
	for (const [key, value] of Object.entries(sanitizedEntries)) if (typeof value === "function" || typeof value === "symbol") delete sanitizedEntries[key];
	return sanitized;
}
/**
* Check if a value looks like a secret based on common patterns.
* Detects API keys, tokens, and other credential patterns.
*/
function looksLikeSecret(value) {
	if (typeof value !== "string") return false;
	if (/^sk-[a-zA-Z0-9-_]{20,}/.test(value)) return true;
	if (/^sk-proj-[a-zA-Z0-9-_]{20,}/.test(value)) return true;
	if (/^sk-ant-[a-zA-Z0-9-_]{20,}/.test(value)) return true;
	if (/^key-[a-zA-Z0-9]{20,}/.test(value)) return true;
	if (/^Bearer\s+.{20,}/i.test(value)) return true;
	if (/^Basic\s+.{20,}/i.test(value)) return true;
	if (/^[a-zA-Z0-9+/=_-]{64,}$/.test(value)) return true;
	if (/^AKIA[A-Z0-9]{16}/.test(value)) return true;
	if (/^AIza[a-zA-Z0-9_-]{35}/.test(value)) return true;
	return false;
}
/**
* Detect class instances (objects with custom prototypes and methods)
*/
function isClassInstance(obj) {
	const proto = Object.getPrototypeOf(obj);
	if (!proto || proto === Object.prototype) return false;
	return Object.getOwnPropertyNames(proto).some((prop) => prop !== "constructor" && typeof proto[prop] === "function");
}
function redactAzureBlobSasToken(value) {
	if (!/^az:\/\//i.test(value)) return value;
	const queryStart = value.indexOf("?");
	if (queryStart === -1) return value;
	const fragmentStart = value.indexOf("#", queryStart);
	const queryEnd = fragmentStart === -1 ? value.length : fragmentStart;
	let redacted = false;
	const sanitizedQuery = value.slice(queryStart + 1, queryEnd).split("&").map((parameter) => {
		const equalsIndex = parameter.indexOf("=");
		const encodedName = equalsIndex === -1 ? parameter : parameter.slice(0, equalsIndex);
		let name;
		try {
			name = decodeURIComponent(encodedName.replace(/\+/g, " "));
		} catch {
			return parameter;
		}
		if (name.toLowerCase() !== "sig") return parameter;
		redacted = true;
		return `${encodedName}=${encodeURIComponent(REDACTED)}`;
	}).join("&");
	return redacted ? `${value.slice(0, queryStart + 1)}${sanitizedQuery}${value.slice(queryEnd)}` : value;
}
/**
* Redact SAS credentials embedded in Azure Blob config references without
* applying broader output sanitization to configuration consumed by clients.
*/
function redactAzureBlobSasTokens(value) {
	if (typeof value === "string") return redactAzureBlobSasToken(value);
	if (Array.isArray(value)) return value.map((item) => redactAzureBlobSasTokens(item));
	if (!value || typeof value !== "object" || isClassInstance(value)) return value;
	const redactedEntries = Object.entries(value).map(([key, item]) => [key, redactAzureBlobSasTokens(item)]);
	return Object.fromEntries(redactedEntries);
}
/**
* Parse and sanitize JSON strings, also check if the string looks like a secret
*/
function sanitizeJsonString(str, depth, maxDepth) {
	const redactedAzureBlobUri = redactAzureBlobSasToken(str);
	if (redactedAzureBlobUri !== str) return redactedAzureBlobUri;
	try {
		const parsed = JSON.parse(str);
		if (parsed && typeof parsed === "object") {
			const sanitized = recursiveSanitize(parsed, depth, maxDepth);
			return JSON.stringify(sanitized);
		}
	} catch {
		if (looksLikeSecret(str)) return REDACTED;
	}
	return str;
}
/**
* Sanitize plain object fields
*/
function sanitizePlainObject(obj, depth, maxDepth) {
	const sanitized = {};
	for (const [key, value] of Object.entries(obj)) if (key === "url" && typeof value === "string") sanitized[key] = sanitizeUrl(value);
	else if (isSecretField(key)) sanitized[key] = REDACTED;
	else if (typeof value === "string" && looksLikeSecret(value)) sanitized[key] = REDACTED;
	else sanitized[key] = recursiveSanitize(value, depth + 1, maxDepth);
	return sanitized;
}
/**
* Recursively sanitize an object, redacting secret fields at any depth
*/
function recursiveSanitize(obj, depth = 0, maxDepth = MAX_DEPTH) {
	if (typeof obj === "function") return `[Function] ${obj.name}`;
	if (typeof obj === "string") return sanitizeJsonString(obj, depth, maxDepth);
	if (obj === null || obj === void 0 || typeof obj !== "object") return obj;
	if (depth > maxDepth) return "[...]";
	if (Array.isArray(obj)) return obj.map((item) => recursiveSanitize(item, depth + 1, maxDepth));
	if (isClassInstance(obj)) return `[${obj.constructor?.name || "Object"} Instance]`;
	return sanitizePlainObject(obj, depth, maxDepth);
}
/**
* Generic function to sanitize any object by removing or redacting sensitive information
* @param obj - The object to sanitize
* @param options - Optional configuration
* @returns A sanitized copy of the object with secrets redacted
*/
function sanitizeObject(obj, options = {}) {
	const { context = "object", throwOnError = false, maxDepth = MAX_DEPTH } = options;
	try {
		if (obj === null || obj === void 0) return obj;
		if (typeof obj === "string") return sanitizeJsonString(obj, 0, maxDepth);
		if (typeof obj !== "object") return obj;
		return recursiveSanitize(JSON.parse(safeStringify(obj, (_key, val) => {
			if (val instanceof Error) return {
				name: val.name,
				message: val.message
			};
			return val;
		}, void 0, {
			depthLimit: Number.MAX_SAFE_INTEGER,
			edgesLimit: Number.MAX_SAFE_INTEGER
		})), 0, maxDepth);
	} catch (error) {
		if (throwOnError) throw error;
		console.error(`Error sanitizing ${context}:`, error);
		return obj;
	}
}
function sanitizeUrl(url) {
	try {
		if (typeof url !== "string" || !url.trim()) return url;
		if (url.includes("{{") && url.includes("}}")) return url;
		const isPathOnly = url.startsWith("/") && !url.startsWith("//");
		const parsedUrl = isPathOnly ? new URL(url, DUMMY_BASE) : new URL(url);
		const sanitizedUrl = new URL(parsedUrl.href);
		if (sanitizedUrl.username || sanitizedUrl.password) {
			sanitizedUrl.username = "***";
			sanitizedUrl.password = "***";
		}
		const sensitiveParams = /(api[_-]?key|token|password|secret|signature|sig|access[_-]?token|refresh[_-]?token|id[_-]?token|client[_-]?secret|authorization)/i;
		try {
			for (const key of Array.from(sanitizedUrl.searchParams.keys())) if (sensitiveParams.test(key)) sanitizedUrl.searchParams.set(key, "[REDACTED]");
		} catch (paramError) {
			console.warn(`Failed to sanitize URL parameters ${url}: ${paramError}`);
		}
		if (isPathOnly) return sanitizedUrl.pathname + sanitizedUrl.search + sanitizedUrl.hash;
		return sanitizedUrl.toString();
	} catch (error) {
		console.warn(`Failed to sanitize URL ${url}: ${error}`);
		return url;
	}
}
//#endregion
//#region src/logger.ts
let globalLogCallback = null;
function setLogCallback(callback) {
	globalLogCallback = callback;
}
function clearLogCallbackIfOwned(callback) {
	if (callback && globalLogCallback === callback) globalLogCallback = null;
}
const LOG_LEVELS = {
	error: 0,
	warn: 1,
	info: 2,
	debug: 3
};
let sourceMapSupportInitialized = false;
let sourceMapSupportInitializationPromise = null;
async function initializeSourceMapSupport() {
	if (sourceMapSupportInitialized) return;
	sourceMapSupportInitializationPromise ??= (async () => {
		try {
			(await import("source-map-support")).install();
			sourceMapSupportInitialized = true;
		} catch {} finally {
			sourceMapSupportInitializationPromise = null;
		}
	})();
	await sourceMapSupportInitializationPromise;
}
/**
* Gets the caller location (filename and line number)
* @returns String with file location information
*/
function getCallerLocation() {
	try {
		const callerLine = ((/* @__PURE__ */ new Error("stack trace capture")).stack?.split("\n") || [])[3];
		if (callerLine) {
			const matchParens = callerLine.match(/at (?:.*) \((.+):(\d+):(\d+)\)/);
			const matchNormal = callerLine.match(/at (.+):(\d+):(\d+)/);
			const match = matchParens || matchNormal;
			if (match) {
				const filePath = match[1];
				const line = match[2];
				return `[${path.basename(filePath)}:${line}]`;
			}
		}
	} catch {}
	return "";
}
/**
* Extracts the actual message string from potentially nested info objects
*/
function extractMessage(info) {
	if (typeof info.message === "object" && info.message !== null && "message" in info.message) return typeof info.message.message === "string" ? info.message.message : String(info.message.message);
	return typeof info.message === "string" ? info.message : JSON.stringify(info.message);
}
const consoleFormatter = winston.format.printf((info) => {
	const message = extractMessage(info);
	if (globalLogCallback) globalLogCallback(message);
	const location = info.location ? `${info.location} ` : "";
	if (info.level === "error") return chalk.red(`${location}${message}`);
	else if (info.level === "warn") return chalk.yellow(`${location}${message}`);
	else if (info.level === "info") return `${location}${message}`;
	else if (info.level === "debug") return `${chalk.cyan(location)}${message}`;
	throw new Error(`Invalid log level: ${info.level}`);
});
winston.format.printf((info) => {
	const timestamp = (/* @__PURE__ */ new Date()).toISOString();
	const location = info.location ? ` ${info.location}` : "";
	const message = extractMessage(info);
	return `${timestamp} [${info.level.toUpperCase()}]${location}: ${message}`;
});
const winstonLogger = winston.createLogger({
	levels: LOG_LEVELS,
	transports: [new winston.transports.Console({
		level: getEnvString("LOG_LEVEL", "info"),
		...getEnvString("PROMPTFOO_LOG_TO_STDERR") === "true" ? { stderrLevels: Object.keys(LOG_LEVELS) } : {},
		format: winston.format.combine(winston.format.simple(), consoleFormatter)
	})]
});
function getLogLevel() {
	return winstonLogger.transports[0].level;
}
function setLogLevel(level) {
	if (level in LOG_LEVELS) {
		winstonLogger.transports[0].level = level;
		if (level === "debug") initializeSourceMapSupport();
	} else throw new Error(`Invalid log level: ${level}`);
}
function isDebugEnabled() {
	return getLogLevel() === "debug";
}
/**
* Creates a logger method for the specified log level.
* Accepts either a string message or a structured object with a message field.
*/
function createLogMethod(level) {
	return (input) => {
		const location = level === "debug" ? getCallerLocation() : isDebugEnabled() ? getCallerLocation() : "";
		if (level === "debug") initializeSourceMapSupport();
		const message = typeof input === "string" ? input : input.message;
		return winstonLogger[level]({
			message,
			location
		});
	};
}
let internalLogger = Object.assign({}, winstonLogger, {
	error: createLogMethod("error"),
	warn: createLogMethod("warn"),
	info: createLogMethod("info"),
	debug: createLogMethod("debug"),
	add: winstonLogger.add.bind(winstonLogger),
	remove: winstonLogger.remove.bind(winstonLogger),
	transports: winstonLogger.transports
});
/**
* Sanitizes context object for logging using generic sanitization
*/
function sanitizeContext(context) {
	const contextWithSanitizedUrls = {};
	for (const [key, value] of Object.entries(context)) if (key === "url" && typeof value === "string") contextWithSanitizedUrls[key] = sanitizeUrl(value);
	else contextWithSanitizedUrls[key] = value;
	return sanitizeObject(contextWithSanitizedUrls, { context: "log context" });
}
/**
* Creates a log method that accepts an optional context parameter.
* If context is provided, it will be sanitized and formatted.
*
* When structured logging is enabled (via setStructuredLogging(true)):
* - Passes { message, ...context } object to the logger
* - Ideal for cloud logging integrations that expect structured data
*
* When structured logging is disabled (default):
* - Formats context as JSON string appended to message
* - Suitable for CLI/console output
*/
function createLogMethodWithContext(level) {
	return (message, context) => {
		if (!context) {
			internalLogger[level](message);
			return;
		}
		const sanitized = sanitizeContext(context);
		{
			const contextStr = safeJsonStringify(sanitized, true);
			internalLogger[level](`${message}\n${contextStr}`);
		}
	};
}
const logger = {
	error: createLogMethodWithContext("error"),
	warn: createLogMethodWithContext("warn"),
	info: createLogMethodWithContext("info"),
	debug: createLogMethodWithContext("debug"),
	add: (transport) => internalLogger.add ? internalLogger.add(transport) : void 0,
	remove: (transport) => internalLogger.remove ? internalLogger.remove(transport) : void 0,
	get transports() {
		return internalLogger.transports || [];
	},
	get level() {
		return internalLogger.transports?.[0]?.level || "info";
	},
	set level(newLevel) {
		if (internalLogger.transports?.[0]) internalLogger.transports[0].level = newLevel;
	}
};
/**
* Logs request/response details in a formatted way
* @param url - Request URL
* @param requestBody - Request body object
* @param response - Response object (optional)
* @param error - Whether to log as error (true) or debug (false)
*/
async function logRequestResponse(options) {
	const { url, requestBody, requestMethod, response, error } = options;
	const logMethod = error ? logger.error : logger.debug;
	let responseText = "";
	if (response) try {
		responseText = await response.clone().text();
	} catch {
		responseText = "Unable to read response";
	}
	logMethod("Api Request", {
		message: "API request",
		url: sanitizeUrl(url),
		method: requestMethod,
		requestBody: sanitizeObject(requestBody, { context: "request body" }),
		...response && {
			status: response.status,
			statusText: response.statusText
		},
		...responseText && { response: responseText }
	});
}
if (getEnvString("LOG_LEVEL", "info") === "debug") initializeSourceMapSupport();
//#endregion
export { getMaxEvalTimeMs as A, summarizeEvaluateResultForLogging as C, getEnvInt as D, getEnvFloat as E, state as M, getEnvString as O, safeJsonStringify as S, getEnvBool as T, extractFirstJsonObject as _, logRequestResponse as a, isValidJson as b, setLogLevel as c, looksLikeSecret as d, normalizeFieldName as f, sanitizeUrl as g, sanitizeRuntimeOptions as h, isDebugEnabled as i, isCI as j, getEvalTimeoutMs as k, REDACTED as l, sanitizeObject as m, getLogLevel as n, logger as o, redactAzureBlobSasTokens as p, globalLogCallback as r, setLogCallback as s, clearLogCallbackIfOwned as t, isSecretField as u, extractJsonObjects as v, getConfigDirectoryPath as w, orderKeys as x, getAjv as y };

//# sourceMappingURL=logger-CtGm8dC_.js.map