#!/usr/bin/env node
import { L as state } from "./logger-4ntIRWmf.js";
//#region src/redteam/shared/promptLength.ts
const MAX_CHARS_PER_MESSAGE_MODIFIER_KEY = "maxCharsPerMessage";
function isRecord(value) {
	return typeof value === "object" && value !== null;
}
function getMaxCharsPerMessage(limit) {
	const maxCharsPerMessage = limit ?? state.config?.redteam?.maxCharsPerMessage;
	if (typeof maxCharsPerMessage !== "number" || !Number.isInteger(maxCharsPerMessage) || maxCharsPerMessage <= 0) return;
	return maxCharsPerMessage;
}
function parseChatMessages(prompt) {
	try {
		const parsed = JSON.parse(prompt);
		if (Array.isArray(parsed) && parsed.every((item) => isRecord(item) && typeof item.role === "string" && typeof item.content === "string")) return parsed.map((message, index) => ({
			content: message.content,
			path: `[${index}].content`,
			role: message.role
		}));
		if (isRecord(parsed) && parsed._promptfoo_audio_hybrid === true && (parsed.history === void 0 || Array.isArray(parsed.history)) && isRecord(parsed.currentTurn) && typeof parsed.currentTurn.role === "string" && typeof parsed.currentTurn.transcript === "string" && (parsed.history === void 0 || parsed.history.every((item) => isRecord(item) && typeof item.role === "string" && typeof item.content === "string"))) return [...(parsed.history ?? []).map((message, index) => ({
			content: message.content,
			path: `history[${index}].content`,
			role: message.role
		})), {
			content: parsed.currentTurn.transcript,
			path: "currentTurn.transcript",
			role: parsed.currentTurn.role
		}];
	} catch {}
}
function getPromptLengthViolation(prompt, limit) {
	const maxCharsPerMessage = getMaxCharsPerMessage(limit);
	if (!maxCharsPerMessage) return;
	const messages = parseChatMessages(prompt);
	if (messages) {
		const oversizedMessage = messages.find((message) => message.role === "user" && message.content.length > maxCharsPerMessage);
		return oversizedMessage ? {
			length: oversizedMessage.content.length,
			limit: maxCharsPerMessage,
			path: oversizedMessage.path
		} : void 0;
	}
	if (prompt.length <= maxCharsPerMessage) return;
	return {
		length: prompt.length,
		limit: maxCharsPerMessage,
		path: "prompt"
	};
}
function getMaxCharsPerMessageModifierValue(limit) {
	const maxCharsPerMessage = getMaxCharsPerMessage(limit);
	if (!maxCharsPerMessage) return;
	return `Each generated user message must be ${maxCharsPerMessage} characters or fewer.`;
}
function getGeneratedPromptOverLimit(prompt, limit) {
	const violation = getPromptLengthViolation(prompt, limit);
	if (!violation) return;
	return {
		length: violation.length,
		limit: violation.limit
	};
}
function throwIfTargetPromptExceedsMaxChars(prompt, limit) {
	const violation = getPromptLengthViolation(prompt, limit);
	if (!violation) return;
	throw new Error(`Target prompt message at ${violation.path} exceeds maxCharsPerMessage=${violation.limit}: ${violation.length} characters.`);
}
//#endregion
export { throwIfTargetPromptExceedsMaxChars as i, getGeneratedPromptOverLimit as n, getMaxCharsPerMessageModifierValue as r, MAX_CHARS_PER_MESSAGE_MODIFIER_KEY as t };

//# sourceMappingURL=promptLength-Xit4MiMV.js.map