"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Plugins = void 0;
const cache_1 = require("../../cache");
const constants_1 = require("../../constants");
const envars_1 = require("../../envars");
const accounts_1 = require("../../globalConfig/accounts");
const logger_1 = __importDefault(require("../../logger"));
const shared_1 = require("../../providers/shared");
const invariant_1 = __importDefault(require("../../util/invariant"));
const constants_2 = require("../constants");
const remoteGeneration_1 = require("../remoteGeneration");
const util_1 = require("../util");
const aegis_1 = require("./aegis");
const asciiSmuggling_1 = require("./asciiSmuggling");
const beavertails_1 = require("./beavertails");
const bfla_1 = require("./bfla");
const bola_1 = require("./bola");
const competitors_1 = require("./competitors");
const contextComplianceAttack_1 = require("./contextComplianceAttack");
const contracts_1 = require("./contracts");
const crossSessionLeak_1 = require("./crossSessionLeak");
const cyberseceval_1 = require("./cyberseceval");
const debugAccess_1 = require("./debugAccess");
const divergentRepetition_1 = require("./divergentRepetition");
const donotanswer_1 = require("./donotanswer");
const excessiveAgency_1 = require("./excessiveAgency");
const financialCalculationError_1 = require("./financial/financialCalculationError");
const financialComplianceViolation_1 = require("./financial/financialComplianceViolation");
const financialConfidentialDisclosure_1 = require("./financial/financialConfidentialDisclosure");
const financialCounterfactual_1 = require("./financial/financialCounterfactual");
const financialDataLeakage_1 = require("./financial/financialDataLeakage");
const financialDefamation_1 = require("./financial/financialDefamation");
const financialHallucination_1 = require("./financial/financialHallucination");
const financialImpartiality_1 = require("./financial/financialImpartiality");
const financialMisconduct_1 = require("./financial/financialMisconduct");
const financialSycophancy_1 = require("./financial/financialSycophancy");
const hallucination_1 = require("./hallucination");
const harmbench_1 = require("./harmbench");
const aligned_1 = require("./harmful/aligned");
const common_1 = require("./harmful/common");
const graders_1 = require("./harmful/graders");
//import { getHarmfulTests } from './harmful/unaligned';
const hijacking_1 = require("./hijacking");
const imitation_1 = require("./imitation");
const intent_1 = require("./intent");
const medicalAnchoringBias_1 = require("./medical/medicalAnchoringBias");
const medicalHallucination_1 = require("./medical/medicalHallucination");
const medicalIncorrectKnowledge_1 = require("./medical/medicalIncorrectKnowledge");
const medicalOffLabelUse_1 = require("./medical/medicalOffLabelUse");
const medicalPrioritizationError_1 = require("./medical/medicalPrioritizationError");
const medicalSycophancy_1 = require("./medical/medicalSycophancy");
const dosageCalculation_1 = require("./pharmacy/dosageCalculation");
const drugInteraction_1 = require("./pharmacy/drugInteraction");
const controlledSubstanceCompliance_1 = require("./pharmacy/controlledSubstanceCompliance");
const coverageDiscrimination_1 = require("./insurance/coverageDiscrimination");
const networkMisinformation_1 = require("./insurance/networkMisinformation");
const phiDisclosure_1 = require("./insurance/phiDisclosure");
const offTopic_1 = require("./offTopic");
const overreliance_1 = require("./overreliance");
const pii_1 = require("./pii");
const pliny_1 = require("./pliny");
const policy_1 = require("./policy");
const politics_1 = require("./politics");
const promptExtraction_1 = require("./promptExtraction");
const ragDocumentExfiltration_1 = require("./ragDocumentExfiltration");
const rbac_1 = require("./rbac");
const reasoningDos_1 = require("./reasoningDos");
const religion_1 = require("./religion");
const shellInjection_1 = require("./shellInjection");
const specialTokenInjection_1 = require("./specialTokenInjection");
const sqlInjection_1 = require("./sqlInjection");
const ssrf_1 = require("./ssrf");
const ferpa_1 = require("./compliance/ferpa");
const coppa_1 = require("./compliance/coppa");
const systemOverride_1 = require("./systemOverride");
const toolDiscovery_1 = require("./toolDiscovery");
const toxicchat_1 = require("./toxicchat");
const unsafebench_1 = require("./unsafebench");
const unverifiableClaims_1 = require("./unverifiableClaims");
const vlguard_1 = require("./vlguard");
const xstest_1 = require("./xstest");
const wordplay_1 = require("./wordplay");
const goalMisalignment_1 = require("./goalMisalignment");
const apiHealth_1 = require("../../util/apiHealth");
const ecommercePciDss_1 = require("./ecommerce/ecommercePciDss");
const ecommerceComplianceBypass_1 = require("./ecommerce/ecommerceComplianceBypass");
const ecommercePriceManipulation_1 = require("./ecommerce/ecommercePriceManipulation");
const ecommerceOrderFraud_1 = require("./ecommerce/ecommerceOrderFraud");
const plugins_1 = require("./harmful/plugins");
const plugins_2 = require("./harmful/plugins");
const plugins_3 = require("./harmful/plugins");
const bias_1 = require("./bias");
const mcp_1 = require("./mcp");
async function fetchRemoteTestCases(key, purpose, injectVar, n, config) {
    (0, invariant_1.default)(!(0, envars_1.getEnvBool)('TRUSYS_DISABLE_REDTEAM_REMOTE_GENERATION'), 'fetchRemoteTestCases should never be called when remote generation is disabled');
    // Health check remote before generating test cases
    const remoteHealth = await (0, apiHealth_1.checkRemoteHealth)((0, remoteGeneration_1.getRemoteHealthUrl)());
    if (remoteHealth.status !== 'OK') {
        logger_1.default.error(`Error generating test cases for ${key}: ${remoteHealth.message}`);
        return [];
    }
    const body = JSON.stringify({
        config,
        injectVar,
        n,
        purpose,
        task: key,
        version: constants_1.VERSION,
        email: (0, accounts_1.getUserEmail)(),
    });
    logger_1.default.debug(`Remote generation request for ${key}:\n${JSON.stringify(body)}`);
    try {
        const { data, status, statusText } = await (0, cache_1.fetchWithCache)((0, remoteGeneration_1.getRemoteGenerationUrl)(), {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body,
        }, shared_1.REQUEST_TIMEOUT_MS);
        if (status !== 200 || !data || !data.result || !Array.isArray(data.result)) {
            logger_1.default.error(`Error generating test cases for ${key}: ${statusText} ${JSON.stringify(data)}`);
            return [];
        }
        const ret = data.result;
        logger_1.default.debug(`Received remote generation for ${key}:\n${JSON.stringify(ret)}`);
        return ret;
    }
    catch (err) {
        logger_1.default.error(`Error generating test cases for ${key}: ${err}`);
        return [];
    }
}
function createPluginFactory(PluginClass, key, validate) {
    return {
        key,
        validate: validate,
        action: async ({ provider, purpose, injectVar, n, delayMs, config }) => {
            if (PluginClass.canGenerateRemote === false || !(0, remoteGeneration_1.shouldGenerateRemote)()) {
                logger_1.default.debug(`Using local redteam generation for ${key}`);
                return new PluginClass(provider, purpose, injectVar, config).generateTests(n, delayMs);
            }
            const testCases = await fetchRemoteTestCases(key, purpose, injectVar, n, config ?? {});
            return testCases.map((testCase) => ({
                ...testCase,
                metadata: {
                    ...testCase.metadata,
                    pluginId: (0, util_1.getShortPluginId)(key),
                },
            }));
        },
    };
}
const alignedHarmCategories = Object.keys(constants_2.REDTEAM_PROVIDER_HARM_PLUGINS);
/* const unalignedHarmCategories = Object.keys(UNALIGNED_PROVIDER_HARM_PLUGINS) as Array<
  keyof typeof UNALIGNED_PROVIDER_HARM_PLUGINS
>; */
const pluginFactories = [
    createPluginFactory(beavertails_1.BeavertailsPlugin, 'beavertails'),
    ...alignedHarmCategories.map((category) => createPluginFactory(class extends aligned_1.AlignedHarmfulPlugin {
        get id() {
            return category;
        }
        constructor(provider, purpose, injectVar, config) {
            super(provider, purpose, injectVar, category, config);
        }
    }, category)),
    createPluginFactory(contracts_1.ContractPlugin, 'contracts'),
    createPluginFactory(crossSessionLeak_1.CrossSessionLeakPlugin, 'cross-session-leak'),
    createPluginFactory(cyberseceval_1.CyberSecEvalPlugin, 'cyberseceval'),
    createPluginFactory(debugAccess_1.DebugAccessPlugin, 'debug-access'),
    createPluginFactory(divergentRepetition_1.DivergentRepetitionPlugin, 'divergent-repetition'),
    createPluginFactory(donotanswer_1.DoNotAnswerPlugin, 'donotanswer'),
    createPluginFactory(excessiveAgency_1.ExcessiveAgencyPlugin, 'excessive-agency'),
    createPluginFactory(xstest_1.XSTestPlugin, 'xstest'),
    createPluginFactory(toolDiscovery_1.ToolDiscoveryPlugin, 'tool-discovery'),
    createPluginFactory(harmbench_1.HarmbenchPlugin, 'harmbench'),
    createPluginFactory(toxicchat_1.ToxicChatPlugin, 'toxic-chat'),
    createPluginFactory(aegis_1.AegisPlugin, 'aegis'),
    createPluginFactory(hallucination_1.HallucinationPlugin, 'hallucination'),
    createPluginFactory(imitation_1.ImitationPlugin, 'imitation'),
    createPluginFactory(systemOverride_1.SystemPromptOverridePlugin, 'system-prompt-override'),
    createPluginFactory(intent_1.IntentPlugin, 'intent', (config) => (0, invariant_1.default)(config.intent, 'Intent plugin requires `config.intent` to be set')),
    createPluginFactory(overreliance_1.OverreliancePlugin, 'overreliance'),
    createPluginFactory(pliny_1.PlinyPlugin, 'pliny'),
    createPluginFactory(policy_1.PolicyPlugin, 'policy', (config) => 
    // Validate the policy plugin config and provide a meaningful error message to the user.
    (0, invariant_1.default)(config.policy && (typeof config.policy === 'string' || (0, policy_1.isValidPolicyObject)(config.policy)), `One of the policy plugins is invalid. The \`config\` property of a policy plugin must be \`{ "policy": { "id": "<policy_id>", "text": "<policy_text>" } }\` or \`{ "policy": "<policy_text>" }\`. Received: ${JSON.stringify(config)}`)),
    createPluginFactory(politics_1.PoliticsPlugin, 'politics'),
    createPluginFactory(promptExtraction_1.PromptExtractionPlugin, 'prompt-extraction'),
    createPluginFactory(rbac_1.RbacPlugin, 'rbac'),
    createPluginFactory(shellInjection_1.ShellInjectionPlugin, 'shell-injection'),
    createPluginFactory(sqlInjection_1.SqlInjectionPlugin, 'sql-injection'),
    createPluginFactory(unsafebench_1.UnsafeBenchPlugin, 'unsafebench'),
    createPluginFactory(unverifiableClaims_1.UnverifiableClaimsPlugin, 'unverifiable-claims'),
    createPluginFactory(vlguard_1.VLGuardPlugin, 'vlguard'),
    createPluginFactory(specialTokenInjection_1.SpecialTokenInjectionPlugin, 'special-token-injection'),
    createPluginFactory(competitors_1.CompetitorsPlugin, 'competitors'),
    createPluginFactory(financialCalculationError_1.FinancialCalculationErrorPlugin, 'financial:calculation-error'),
    createPluginFactory(financialComplianceViolation_1.FinancialComplianceViolationPlugin, 'financial:compliance-violation'),
    createPluginFactory(financialConfidentialDisclosure_1.FinancialConfidentialDisclosurePlugin, 'financial:confidential-disclosure'),
    createPluginFactory(financialCounterfactual_1.FinancialCounterfactualPlugin, 'financial:counterfactual'),
    createPluginFactory(financialDataLeakage_1.FinancialDataLeakagePlugin, 'financial:data-leakage'),
    createPluginFactory(financialDefamation_1.FinancialDefamationPlugin, 'financial:defamation'),
    createPluginFactory(financialHallucination_1.FinancialHallucinationPlugin, 'financial:hallucination'),
    createPluginFactory(financialImpartiality_1.FinancialImpartialityPlugin, 'financial:impartiality'),
    createPluginFactory(financialMisconduct_1.FinancialMisconductPlugin, 'financial:misconduct'),
    createPluginFactory(financialSycophancy_1.FinancialSycophancyPlugin, 'financial:sycophancy'),
    createPluginFactory(offTopic_1.OffTopicPlugin, 'off-topic'),
    createPluginFactory(asciiSmuggling_1.AsciiSmugglingPlugin, 'ascii-smuggling'),
    createPluginFactory(bfla_1.BflaPlugin, 'bfla'),
    createPluginFactory(bola_1.BolaPlugin, 'bola'),
    createPluginFactory(contextComplianceAttack_1.CcaPlugin, 'cca'),
    createPluginFactory(coppa_1.CoppaPlugin, 'coppa'),
    createPluginFactory(ferpa_1.FerpaPlugin, 'ferpa'),
    createPluginFactory(hijacking_1.HijackingPlugin, 'hijacking'),
    createPluginFactory(medicalAnchoringBias_1.MedicalAnchoringBiasPlugin, 'medical:anchoring-bias'),
    createPluginFactory(medicalHallucination_1.MedicalHallucinationPlugin, 'medical:hallucination'),
    createPluginFactory(medicalIncorrectKnowledge_1.MedicalIncorrectKnowledgePlugin, 'medical:incorrect-knowledge'),
    createPluginFactory(medicalOffLabelUse_1.MedicalOffLabelUsePlugin, 'medical:off-label-use'),
    createPluginFactory(medicalPrioritizationError_1.MedicalPrioritizationErrorPlugin, 'medical:prioritization-error'),
    createPluginFactory(medicalSycophancy_1.MedicalSycophancyPlugin, 'medical:sycophancy'),
    createPluginFactory(controlledSubstanceCompliance_1.PharmacyControlledSubstanceCompliancePlugin, 'pharmacy:controlled-substance-compliance'),
    createPluginFactory(drugInteraction_1.PharmacyDrugInteractionPlugin, 'pharmacy:drug-interaction'),
    createPluginFactory(dosageCalculation_1.PharmacyDosageCalculationPlugin, 'pharmacy:dosage-calculation'),
    createPluginFactory(coverageDiscrimination_1.InsuranceCoverageDiscriminationPlugin, 'insurance:coverage-discrimination'),
    createPluginFactory(networkMisinformation_1.InsuranceNetworkMisinformationPlugin, 'insurance:network-misinformation'),
    createPluginFactory(phiDisclosure_1.InsurancePhiDisclosurePlugin, 'insurance:phi-disclosure'),
    createPluginFactory(ragDocumentExfiltration_1.RagDocExfiltrationPlugin, 'rag-document-exfiltration'),
    createPluginFactory(reasoningDos_1.ReasoningDosPlugin, 'reasoning-dos'),
    createPluginFactory(ssrf_1.SsrfPlugin, 'ssrf'),
    createPluginFactory(graders_1.MisinformationDisinformationPlugin, 'harmful:misinformation-disinformation'),
    createPluginFactory(graders_1.SpecializedAdvicePlugin, 'harmful:specialized-advice'),
    createPluginFactory(religion_1.ReligionPlugin, 'religion'),
    createPluginFactory(wordplay_1.WordplayPlugin, 'wordplay'),
    createPluginFactory(goalMisalignment_1.GoalMisalignmentPlugin, 'goal-misalignment'),
    createPluginFactory(ecommercePciDss_1.EcommercePciDssPlugin, 'ecommerce:pci-dss'),
    createPluginFactory(ecommerceComplianceBypass_1.EcommerceComplianceBypassPlugin, 'ecommerce:compliance-bypass'),
    createPluginFactory(ecommerceOrderFraud_1.EcommerceOrderFraudPlugin, 'ecommerce:order-fraud'),
    createPluginFactory(ecommercePriceManipulation_1.EcommercePriceManipulationPlugin, 'ecommerce:price-manipulation'),
    createPluginFactory(plugins_1.HarmfulChildExploitationPlugin, 'harmful:child-exploitation'),
    createPluginFactory(plugins_2.HarmfulHatePlugin, 'harmful:hate'),
    createPluginFactory(plugins_3.HarmfulIndiscriminateWeaponsPlugin, 'harmful:indiscriminate-weapons'),
    createPluginFactory(plugins_3.HarmfulNonViolentCrimePlugin, 'harmful:non-violent-crime'),
    createPluginFactory(plugins_1.HarmfulSelfHarmPlugin, 'harmful:self-harm'),
    createPluginFactory(plugins_1.HarmfulSexCrimePlugin, 'harmful:sex-crime'),
    createPluginFactory(plugins_1.HarmfulSexualContentPlugin, 'harmful:sexual-content'),
    createPluginFactory(plugins_1.HarmfulViolentCrimePlugin, 'harmful:violent-crime'),
    createPluginFactory(plugins_1.HarmfulChemicalBiologicalWeaponsPlugin, 'harmful:chemical-biological-weapons'),
    createPluginFactory(plugins_1.HarmfulCopyrightViolationsPlugin, 'harmful:copyright-violations'),
    createPluginFactory(plugins_1.HarmfulCybercrimePlugin, 'harmful:cybercrime'),
    createPluginFactory(plugins_1.HarmfulGraphicContentPlugin, 'harmful:graphic-content'),
    createPluginFactory(plugins_1.HarmfulHarassmentBullyingPlugin, 'harmful:harassment-bullying'),
    createPluginFactory(plugins_1.HarmfulIllegalActivitiesPlugin, 'harmful:illegal-activities'),
    createPluginFactory(plugins_1.HarmfulIllegalDrugsPlugin, 'harmful:illegal-drugs'),
    createPluginFactory(plugins_1.HarmfulUnsafePracticesPlugin, 'harmful:unsafe-practices'),
    createPluginFactory(plugins_1.HarmfulInsultsPlugin, 'harmful:insults'),
    createPluginFactory(plugins_1.HarmfulProfanityPlugin, 'harmful:profanity'),
    createPluginFactory(plugins_1.HarmfulRadicalizationPlugin, 'harmful:radicalization'),
    createPluginFactory(plugins_1.HarmfulMethPlugin, 'harmful:illegal-drugs:meth'),
    createPluginFactory(plugins_1.HarmfulMaliciousCodePlugin, 'harmful:cybercrime:malicious-code'),
    createPluginFactory(plugins_1.HarmfulIedPlugin, 'harmful:weapons:ied'),
    createPluginFactory(bias_1.AgeBiasPlugin, 'bias:age'),
    createPluginFactory(bias_1.GenderBiasPlugin, 'bias:gender'),
    createPluginFactory(bias_1.RaceBiasPlugin, 'bias:race'),
    createPluginFactory(bias_1.DisabilityBiasPlugin, 'bias:disability'),
    createPluginFactory(mcp_1.MCPPlugin, 'mcp'),
    /* ...unalignedHarmCategories.map((category) => ({
      key: category,
      action: async (params: PluginActionParams) => {
        if (neverGenerateRemote()) {
          logger.error(`${category} plugin requires remote generation to be enabled`);
          return [];
        }
  
        const testCases = await getHarmfulTests(params, category);
        return testCases.map((testCase) => ({
          ...testCase,
          metadata: {
            ...testCase.metadata,
            pluginId: getShortPluginId(category),
          },
        }));
      },
    })), */
];
const piiPlugins = constants_2.PII_PLUGINS.map((category) => ({
    key: category,
    action: async (params) => {
        if ((0, remoteGeneration_1.shouldGenerateRemote)()) {
            const testCases = await fetchRemoteTestCases(category, params.purpose, params.injectVar, params.n, params.config ?? {});
            return testCases.map((testCase) => ({
                ...testCase,
                metadata: {
                    ...testCase.metadata,
                    pluginId: (0, util_1.getShortPluginId)(category),
                },
            }));
        }
        logger_1.default.debug(`Using local redteam generation for ${category}`);
        const testCases = await (0, pii_1.getPiiLeakTestsForCategory)(params, category);
        return testCases.map((testCase) => ({
            ...testCase,
            metadata: {
                ...testCase.metadata,
                pluginId: (0, util_1.getShortPluginId)(category),
            },
        }));
    },
}));
/* const biasPlugins: PluginFactory[] = BIAS_PLUGINS.map((category: string) => ({
  key: category,
  action: async (params: PluginActionParams) => {
    if (neverGenerateRemote()) {
      logger.error(`${category} plugin requires remote generation to be enabled`);
      return [];
    }

    const testCases = await fetchRemoteTestCases(
      category,
      params.purpose,
      params.injectVar,
      params.n,
      params.config ?? {},
    );
    return testCases.map((testCase) => ({
      ...testCase,
      metadata: {
        ...testCase.metadata,
        pluginId: getShortPluginId(category),
      },
    }));
  },
})) */ ;
function createRemotePlugin(key, validate) {
    return {
        key,
        validate: validate,
        action: async ({ purpose, injectVar, n, config }) => {
            if ((0, remoteGeneration_1.neverGenerateRemote)()) {
                logger_1.default.error(`${key} plugin requires remote generation to be enabled`);
                return [];
            }
            const testCases = await fetchRemoteTestCases(key, purpose, injectVar, n, config ?? {});
            const testsWithMetadata = testCases.map((testCase) => ({
                ...testCase,
                metadata: {
                    ...testCase.metadata,
                    pluginId: (0, util_1.getShortPluginId)(key),
                },
            }));
            if (key.startsWith('harmful:') || key.startsWith('bias:')) {
                return testsWithMetadata.map((testCase) => ({
                    ...testCase,
                    assert: (0, common_1.getHarmfulAssertions)(key),
                }));
            }
            return testsWithMetadata;
        },
    };
}
const remotePlugins = constants_2.REMOTE_ONLY_PLUGIN_IDS.map((key) => createRemotePlugin(key));
remotePlugins.push(createRemotePlugin('indirect-prompt-injection', (config) => (0, invariant_1.default)(config.indirectInjectionVar, 'Indirect prompt injection plugin requires `config.indirectInjectionVar` to be set. If using this plugin in a plugin collection, configure this plugin separately.')));
exports.Plugins = [
    ...pluginFactories,
    ...piiPlugins,
    //...biasPlugins,
    ...remotePlugins,
];
//# sourceMappingURL=index.js.map