import { C as safeJsonStringify, o as logger } from "./logger-CfDSdx1q.js";
import { W as VERSION, t as fetchWithProxy } from "./fetch-CHx36OdV.js";
import { t as invariant } from "./invariant-vgHWClmd.js";
import { s as getUserEmail } from "./accounts-Dz-Gsg_B.js";
import { o as createEmptyTokenUsage, r as accumulateResponseTokenUsage } from "./tokenUsageUtils-BcRzoYR7.js";
import { c as neverGenerateRemote, i as getRemoteGenerationUrl, r as getRemoteGenerationHeaders } from "./remoteGeneration-BNr30mu3.js";
import { i as throwIfTargetPromptExceedsMaxChars } from "./promptLength-VChEPvhr.js";
import { n as renderPrompt } from "./server/index.js";
//#region src/redteam/providers/authoritativeMarkupInjection.ts
var AuthoritativeMarkupInjectionProvider = class {
	config;
	id() {
		return "promptfoo:redteam:authoritative-markup-injection";
	}
	constructor(options = {}) {
		if (neverGenerateRemote()) throw new Error(`Authoritative Markup Injection strategy requires remote grading to be enabled`);
		invariant(typeof options.injectVar === "string", "Expected injectVar to be set");
		this.config = { injectVar: options.injectVar };
		logger.debug("[AuthoritativeMarkupInjection] Constructor options", { injectVar: options.injectVar });
	}
	async callApi(_prompt, context, options) {
		logger.debug("[AuthoritativeMarkupInjection] callApi context", { context });
		invariant(context?.originalProvider, "Expected originalProvider to be set");
		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: VERSION,
			email: getUserEmail(),
			purpose: context?.test?.metadata?.purpose
		});
		logger.debug(`[AuthoritativeMarkupInjection] Sending request to ${getRemoteGenerationUrl()}: ${body}`);
		const data = await (await fetchWithProxy(getRemoteGenerationUrl(), {
			body,
			headers: 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: ${safeJsonStringify(data)}`);
		const attackerMessage = data.message;
		const targetVars = {
			...context.vars,
			[this.config.injectVar]: attackerMessage.content
		};
		const renderedAttackerPrompt = await renderPrompt(context.prompt, targetVars, context.filters, targetProvider, [this.config.injectVar]);
		logger.debug(`[AuthoritativeMarkupInjection] Rendered attack prompt`, { prompt: renderedAttackerPrompt });
		const totalTokenUsage = createEmptyTokenUsage();
		throwIfTargetPromptExceedsMaxChars(renderedAttackerPrompt);
		const targetResponse = await targetProvider.callApi(renderedAttackerPrompt, context, options);
		accumulateResponseTokenUsage(totalTokenUsage, targetResponse);
		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
export { AuthoritativeMarkupInjectionProvider as default };

//# sourceMappingURL=authoritativeMarkupInjection-0MNIipzG.js.map