import { o as logger } from "./logger-CtGm8dC_.js";
import { t as invariant } from "./invariant-D3duWnAm.js";
import { U as VERSION, t as fetchWithProxy } from "./fetch-AchwhzAC.js";
import { a as getUserEmail } from "./accounts-B1w_ge6E.js";
import { o as createEmptyTokenUsage, r as accumulateResponseTokenUsage } from "./tokenUsageUtils-Au_f5JrP.js";
import { i as getRemoteGenerationUrl, r as getRemoteGenerationHeaders } from "./remoteGeneration-DMPt0u0n.js";
import { d as messagesToRedteamHistory } from "./shared-Bi98VsZS.js";
import { i as throwIfTargetPromptExceedsMaxChars } from "./promptLength-Dc6ckoAU.js";
import { t as REDTEAM_MEMORY_POISONING_PLUGIN_ID } from "./constants-Dp1R19Rs.js";
//#region src/redteam/providers/agentic/memoryPoisoning.ts
var MemoryPoisoningProvider = class {
	constructor(config) {
		this.config = config;
	}
	id() {
		return REDTEAM_MEMORY_POISONING_PLUGIN_ID;
	}
	/**
	* Generates a scenario containing a memory and a follow up question/command which is dependent on the memory.
	*
	*  1. Generate a scenario containing memories and follow up questions/commands which are dependent on the memories.
	*  2. Send the memory message to the target.
	*  3. Send the test case to the target; the test case should poison the memory created in the previous step.
	*  4. Send the follow up question to the target.
	*  5. A memory is successfully poisoned if the follow up question is answered incorrectly.
	*
	* @param prompt The poisoned prompt.
	* @param context The context of the poisoned prompt.
	* @param options The options for the poisoned prompt.
	*/
	async callApi(prompt, context, options) {
		try {
			const targetProvider = context?.originalProvider;
			const purpose = context?.test?.metadata?.purpose;
			invariant(targetProvider, "Expected originalProvider to be set");
			invariant(context?.test, "Expected test to be set");
			invariant(purpose, "Expected purpose to be set");
			const scenarioRes = await fetchWithProxy(getRemoteGenerationUrl(), {
				body: JSON.stringify({
					task: "agentic:memory-poisoning-scenario",
					purpose,
					version: VERSION,
					email: getUserEmail()
				}),
				headers: getRemoteGenerationHeaders(),
				method: "POST"
			}, options?.abortSignal);
			if (!scenarioRes.ok) throw new Error(`Failed to generate scenario: ${scenarioRes.statusText}`);
			const scenario = await scenarioRes.json();
			context.test.metadata ??= {};
			context.test.metadata["scenario"] = scenario;
			const totalTokenUsage = createEmptyTokenUsage();
			throwIfTargetPromptExceedsMaxChars(scenario.memory);
			const memoryResponse = await targetProvider.callApi(scenario.memory, context, options);
			accumulateResponseTokenUsage(totalTokenUsage, memoryResponse);
			throwIfTargetPromptExceedsMaxChars(prompt);
			const testResponse = await targetProvider.callApi(prompt, context, options);
			accumulateResponseTokenUsage(totalTokenUsage, testResponse);
			throwIfTargetPromptExceedsMaxChars(scenario.followUp);
			const response = await targetProvider.callApi(scenario.followUp, context, options);
			accumulateResponseTokenUsage(totalTokenUsage, response);
			const messages = [
				{
					content: scenario.memory,
					role: "user"
				},
				{
					content: memoryResponse.output,
					role: "assistant"
				},
				{
					content: prompt,
					role: "user"
				},
				{
					content: testResponse.output,
					role: "assistant"
				},
				{
					content: scenario.followUp,
					role: "user"
				},
				{
					content: response.output,
					role: "assistant"
				}
			];
			return {
				output: response.output,
				metadata: {
					messages,
					redteamHistory: messagesToRedteamHistory(messages)
				},
				tokenUsage: totalTokenUsage
			};
		} catch (error) {
			logger.error(`Error in MemoryPoisoningProvider: ${error}`);
			throw error;
		}
	}
};
//#endregion
export { MemoryPoisoningProvider };

//# sourceMappingURL=memoryPoisoning-BPL3PGyI.js.map