"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CUSTOM_AI_CLI_ONLY_PROVIDER_ID = exports.AZURE_SESSIONS_CUSTOM_AI_PROVIDER_ID = void 0;
exports.pickCustomAIConfigForRedteamTarget = pickCustomAIConfigForRedteamTarget;
exports.buildCustomAIProviderForEval = buildCustomAIProviderForEval;
exports.normalizeRedteamCustomAITarget = normalizeRedteamCustomAITarget;
exports.AZURE_SESSIONS_CUSTOM_AI_PROVIDER_ID = 'azure-sessions-custom-ai';
exports.CUSTOM_AI_CLI_ONLY_PROVIDER_ID = 'custom-ai-cli-only';
/** Keys that belong in DB provider config only — not on redteam `targets[0]` root (strict schema). */
const CUSTOM_AI_CONFIG_KEYS = [
    'language',
    'setupMethod',
    'applicationPath',
    'transformResponse',
    'bundleBlobUrl',
    'bundleBlobPath',
    'bundleFileName',
    'secretsVaultName',
    'bundleValidatedAt',
];
const CUSTOM_AI_TOP_LEVEL_LEAK_KEYS = [
    'setupMethod',
    'bundleBlobUrl',
    'bundleBlobPath',
    'bundleFileName',
    'applicationPath',
    'secretsVaultName',
    'bundleValidatedAt',
];
function pickCustomAIConfigForRedteamTarget(providerConfig, applicationId) {
    const out = {};
    for (const key of CUSTOM_AI_CONFIG_KEYS) {
        const value = providerConfig[key];
        if (value !== undefined && value !== null && value !== '') {
            out[key] = value;
        }
    }
    if (applicationId) {
        out.applicationId = applicationId;
    }
    return out;
}
function isCustomAIApplicationRecord(application) {
    if (application?.provider_config?.config?.custom_provider_type === 'Custom AI Application') {
        return true;
    }
    const provider = application?.provider_config?.config?.provider;
    return (application?.provider_config?.provider_type === 'custom' &&
        Boolean(provider?.language) &&
        (provider?.setupMethod === 'shell' || provider?.setupMethod === 'upload'));
}
function buildCustomAIProviderForEval(application) {
    const providerConfig = application?.provider_config?.config?.provider;
    if (!isCustomAIApplicationRecord(application) || !providerConfig) {
        return null;
    }
    const setupMethod = providerConfig.setupMethod ?? 'shell';
    const applicationId = application?.id;
    const evalConfig = pickCustomAIConfigForRedteamTarget(providerConfig, applicationId);
    const cliProviderId = typeof providerConfig.id === 'string' && providerConfig.id.startsWith('file://')
        ? providerConfig.id
        : undefined;
    if (cliProviderId) {
        const nestedConfig = providerConfig.config;
        return {
            id: cliProviderId,
            label: application?.name,
            applicationId,
            enableEncryption: application?.enableEncryption,
            config: {
                ...evalConfig,
                ...(nestedConfig?.basePath ? { basePath: nestedConfig.basePath } : {}),
            },
        };
    }
    if (setupMethod === 'upload') {
        return {
            id: exports.AZURE_SESSIONS_CUSTOM_AI_PROVIDER_ID,
            label: application?.name,
            applicationId,
            enableEncryption: application?.enableEncryption,
            config: evalConfig,
        };
    }
    return {
        id: exports.CUSTOM_AI_CLI_ONLY_PROVIDER_ID,
        label: application?.name,
        applicationId,
        enableEncryption: application?.enableEncryption,
        config: evalConfig,
    };
}
/** Strip mistaken top-level Custom AI DB fields and rebuild target when `id` is missing. */
function normalizeRedteamCustomAITarget(target, applicationId) {
    if (!target || typeof target !== 'object') {
        return target ?? {};
    }
    const id = typeof target.id === 'string' ? target.id : undefined;
    const isKnownCustomAIId = id === exports.AZURE_SESSIONS_CUSTOM_AI_PROVIDER_ID || id === exports.CUSTOM_AI_CLI_ONLY_PROVIDER_ID;
    if (isKnownCustomAIId) {
        const cleaned = { ...target };
        for (const key of CUSTOM_AI_TOP_LEVEL_LEAK_KEYS) {
            delete cleaned[key];
        }
        return cleaned;
    }
    const setupMethod = (target.setupMethod ?? target.config?.setupMethod);
    const language = (target.language ?? target.config?.language);
    if (!language && !setupMethod) {
        return target;
    }
    const syntheticApplication = {
        id: target.applicationId ?? applicationId,
        name: target.label,
        enableEncryption: target.enableEncryption,
        provider_config: {
            provider_type: 'custom',
            config: {
                custom_provider_type: 'Custom AI Application',
                provider: {
                    language,
                    setupMethod: setupMethod === 'upload' ? 'upload' : 'shell',
                    applicationPath: target.applicationPath ??
                        target.config?.applicationPath,
                    bundleBlobUrl: target.bundleBlobUrl ??
                        target.config?.bundleBlobUrl,
                    transformResponse: target.transformResponse ??
                        target.config?.transformResponse,
                    bundleBlobPath: target.bundleBlobPath ??
                        target.config?.bundleBlobPath,
                    bundleFileName: target.bundleFileName ??
                        target.config?.bundleFileName,
                    secretsVaultName: target.secretsVaultName ??
                        target.config?.secretsVaultName,
                    bundleValidatedAt: target.bundleValidatedAt ??
                        target.config?.bundleValidatedAt,
                },
            },
        },
    };
    const rebuilt = buildCustomAIProviderForEval(syntheticApplication);
    if (rebuilt) {
        return rebuilt;
    }
    return target;
}
//# sourceMappingURL=buildProviderForEval.js.map