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_shared = require("./shared-uRkDny0q.cjs");
const require_promptLength = require("./promptLength-DpSjaC0G.cjs");
const require_constants = require("./constants-DnNGMQTB.cjs");
//#region src/redteam/providers/agentic/memoryPoisoning.ts
var MemoryPoisoningProvider = class {
	constructor(config) {
		this.config = config;
	}
	id() {
		return require_constants.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;
			require_invariant.invariant(targetProvider, "Expected originalProvider to be set");
			require_invariant.invariant(context?.test, "Expected test to be set");
			require_invariant.invariant(purpose, "Expected purpose to be set");
			const scenarioRes = await require_fetch.fetchWithProxy(require_remoteGeneration.getRemoteGenerationUrl(), {
				body: JSON.stringify({
					task: "agentic:memory-poisoning-scenario",
					purpose,
					version: require_fetch.VERSION,
					email: require_accounts.getUserEmail()
				}),
				headers: require_remoteGeneration.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 = require_tokenUsageUtils.createEmptyTokenUsage();
			require_promptLength.throwIfTargetPromptExceedsMaxChars(scenario.memory);
			const memoryResponse = await targetProvider.callApi(scenario.memory, context, options);
			require_tokenUsageUtils.accumulateResponseTokenUsage(totalTokenUsage, memoryResponse);
			require_promptLength.throwIfTargetPromptExceedsMaxChars(prompt);
			const testResponse = await targetProvider.callApi(prompt, context, options);
			require_tokenUsageUtils.accumulateResponseTokenUsage(totalTokenUsage, testResponse);
			require_promptLength.throwIfTargetPromptExceedsMaxChars(scenario.followUp);
			const response = await targetProvider.callApi(scenario.followUp, context, options);
			require_tokenUsageUtils.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: require_shared.messagesToRedteamHistory(messages)
				},
				tokenUsage: totalTokenUsage
			};
		} catch (error) {
			require_logger.logger.error(`Error in MemoryPoisoningProvider: ${error}`);
			throw error;
		}
	}
};
//#endregion
exports.MemoryPoisoningProvider = MemoryPoisoningProvider;

//# sourceMappingURL=memoryPoisoning-DsdlW8SX.cjs.map