"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.redteamRunCommand = redteamRunCommand;
const zod_1 = require("zod");
const cliState_1 = __importDefault(require("../../cliState"));
const logger_1 = __importStar(require("../../logger"));
const telemetry_1 = __importDefault(require("../../telemetry"));
const util_1 = require("../../util");
const cloud_1 = require("../../util/cloud");
const shared_1 = require("../shared");
const sharedFrontend_1 = require("../sharedFrontend");
const poison_1 = require("./poison");
const UUID_REGEX = /^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$/;
function redteamRunCommand(program) {
    program
        .command('run')
        .description('Run red teaming process (init, generate, and evaluate)')
        .option('-c, --config [path]', 'Path to configuration file or cloud config UUID. Defaults to promptfooconfig.yaml')
        .option('-o, --output [path]', 'Path to output file for generated tests. Defaults to redteam.yaml')
        .option('--no-cache', 'Do not read or write results to disk cache', false)
        .option('--env-file, --env-path <path>', 'Path to .env file')
        .option('-j, --max-concurrency <number>', 'Maximum number of concurrent API calls', (val) => Number.parseInt(val, 10))
        .option('--delay <number>', 'Delay in milliseconds between API calls', (val) => Number.parseInt(val, 10))
        .option('--remote', 'Force remote inference wherever possible', false)
        .option('--force', 'Force generation even if no changes are detected', false)
        .option('--verbose', 'Show debug output', false)
        .option('--filter-providers, --filter-targets <providers>', 'Only run tests with these providers (regex match)')
        .action(async (opts) => {
        (0, util_1.setupEnv)(opts.envPath);
        telemetry_1.default.record('command_used', {
            name: 'redteam run',
        });
        await telemetry_1.default.send();
        if (opts.verbose) {
            (0, logger_1.setLogLevel)('debug');
        }
        if (opts.config && UUID_REGEX.test(opts.config)) {
            const configObj = await (0, cloud_1.getConfigFromCloud)(opts.config);
            opts.liveRedteamConfig = (0, sharedFrontend_1.getUnifiedConfig)(configObj.config);
            opts.config = undefined;
            opts.loadedFromCloud = true;
        }
        try {
            if (opts.remote) {
                cliState_1.default.remote = true;
            }
            await (0, shared_1.doRedteamRun)(opts);
        }
        catch (error) {
            if (error instanceof zod_1.z.ZodError) {
                logger_1.default.error('Invalid options:');
                error.errors.forEach((err) => {
                    logger_1.default.error(`  ${err.path.join('.')}: ${err.message}`);
                });
            }
            else {
                logger_1.default.error(`An unexpected error occurred: ${error}`);
            }
            process.exitCode = 1;
        }
    });
    (0, poison_1.poisonCommand)(program);
}
//# sourceMappingURL=run.js.map