const require_logger = require("./logger-DuG3amVa.cjs");
const require_invariant = require("./invariant-KxYLhw_b.cjs");
const require_fetch = require("./fetch-Hdu2Q-w3.cjs");
const require_accounts = require("./accounts-CibSkI-0.cjs");
const require_tokenUsageUtils = require("./tokenUsageUtils-DQwV4JGQ.cjs");
const require_remoteGeneration = require("./remoteGeneration-BeDYd404.cjs");
const require_promptLength = require("./promptLength-DpSjaC0G.cjs");
const require_evaluatorHelpers = require("./evaluatorHelpers-D2-tB9VN.cjs");
//#region src/redteam/providers/authoritativeMarkupInjection.ts
var AuthoritativeMarkupInjectionProvider = class {
	config;
	id() {
		return "promptfoo:redteam:authoritative-markup-injection";
	}
	constructor(options = {}) {
		if (require_remoteGeneration.neverGenerateRemote()) throw new Error(`Authoritative Markup Injection strategy requires remote grading to be enabled`);
		require_invariant.invariant(typeof options.injectVar === "string", "Expected injectVar to be set");
		this.config = { injectVar: options.injectVar };
		require_logger.logger.debug("[AuthoritativeMarkupInjection] Constructor options", { injectVar: options.injectVar });
	}
	async callApi(_prompt, context, options) {
		require_logger.logger.debug("[AuthoritativeMarkupInjection] callApi context", { context });
		require_invariant.invariant(context?.originalProvider, "Expected originalProvider to be set");
		require_invariant.invariant(context?.vars, "Expected vars to be set");
		const targetProvider = context.originalProvider;
		const originalText = context?.test?.metadata?.originalText || context?.vars[this.config.injectVar];
		const body = JSON.stringify({
			originalText,
			i: 0,
			prompt: context?.prompt?.raw,
			task: "authoritative-markup-injection",
			version: require_fetch.VERSION,
			email: require_accounts.getUserEmail(),
			purpose: context?.test?.metadata?.purpose
		});
		require_logger.logger.debug(`[AuthoritativeMarkupInjection] Sending request to ${require_remoteGeneration.getRemoteGenerationUrl()}: ${body}`);
		const data = await (await require_fetch.fetchWithProxy(require_remoteGeneration.getRemoteGenerationUrl(), {
			body,
			headers: require_remoteGeneration.getRemoteGenerationHeaders(),
			method: "POST"
		}, options?.abortSignal)).json();
		if (typeof data?.message !== "object" || !data.message?.content || !data.message?.role) throw new Error(`[AuthoritativeMarkupInjection] Invalid response from server: ${require_logger.safeJsonStringify(data)}`);
		const attackerMessage = data.message;
		const targetVars = {
			...context.vars,
			[this.config.injectVar]: attackerMessage.content
		};
		const renderedAttackerPrompt = await require_evaluatorHelpers.renderPrompt(context.prompt, targetVars, context.filters, targetProvider, [this.config.injectVar]);
		require_logger.logger.debug(`[AuthoritativeMarkupInjection] Rendered attack prompt`, { prompt: renderedAttackerPrompt });
		const totalTokenUsage = require_tokenUsageUtils.createEmptyTokenUsage();
		require_promptLength.throwIfTargetPromptExceedsMaxChars(renderedAttackerPrompt);
		const targetResponse = await targetProvider.callApi(renderedAttackerPrompt, context, options);
		require_tokenUsageUtils.accumulateResponseTokenUsage(totalTokenUsage, targetResponse);
		require_logger.logger.debug("[AuthoritativeMarkupInjection] Target response", { response: targetResponse });
		if (targetResponse.error) return {
			...targetResponse,
			tokenUsage: totalTokenUsage
		};
		return {
			...targetResponse,
			prompt: renderedAttackerPrompt,
			metadata: {
				...targetResponse.metadata,
				redteamFinalPrompt: renderedAttackerPrompt
			},
			tokenUsage: totalTokenUsage
		};
	}
};
//#endregion
exports.default = AuthoritativeMarkupInjectionProvider;

//# sourceMappingURL=authoritativeMarkupInjection-C76UKO5W.cjs.map