/**
 * src/models/bundled-key.ts
 *
 * Phase-1 testing-only fallback for users who don't have their own NIM key.
 *
 * ─── Important caveats ─────────────────────────────────────────────────
 * The NVIDIA NIM key below is XOR-obfuscated against a random pad. It is
 * NOT cryptographically protected — anyone with shell access to a customer
 * device can reconstruct it in two lines of JavaScript. Treat this as a
 * convenience for testing/demos, not a real secret.
 *
 * Side effects when this fallback fires:
 *   • Every customer's NIM usage hits the bundled key's quota — rate limits
 *     are global, one bad actor blows everyone's budget.
 *   • A leak (or NVIDIA's bot scanning public dist files) auto-revokes the
 *     key. Every customer device dies until you ship a new version.
 *   • NVIDIA's TOS likely forbids embedding/redistribution of the key.
 *
 * Strategic plan: replace this with the TaseesAI NIM proxy (server-side
 * key, no exposure) once the proxy's Vercel timeout is fixed. See
 * memory/al-moasis.md for the migration plan.
 *
 * To rotate the bundled key:
 *   node -e '
 *     const crypto = require("crypto");
 *     const k = "nvapi-NEW-KEY-HERE";
 *     const pad = crypto.randomBytes(k.length);
 *     const buf = Buffer.from(k);
 *     const out = Buffer.alloc(k.length);
 *     for (let i = 0; i < k.length; i++) out[i] = buf[i] ^ pad[i];
 *     console.log("PAD =", pad.toString("base64"));
 *     console.log("PAY =", out.toString("base64"));
 *   '
 * Paste PAD into BUNDLED_PAD_B64 and PAY into BUNDLED_PAYLOAD_B64 below.
 */
const BUNDLED_PAD_B64 = '';
const BUNDLED_PAYLOAD_B64 = '';
/**
 * Reconstruct the bundled NIM key. Returns null if the pad/payload pair
 * is malformed (e.g. someone half-rotated the constants and the build
 * shipped anyway).
 */
// (Audit C6) Hard-disabled by default. The XOR pad ships in the same dist/ —
// `npm pack` gives anyone the key. The proxy at taseesai-nim-proxy.vercel.app
// is the documented path. Re-enable only via env when running internal demos.
const BUNDLED_NIM_DISABLED = process.env.MUASSIS_ALLOW_BUNDLED_NIM !== 'true';
let warnedThisProcess = false;
export function getBundledNimKey() {
    if (BUNDLED_NIM_DISABLED) {
        if (!warnedThisProcess) {
            // Surface why the fallback didn't fire so debugging is straightforward.
            // eslint-disable-next-line no-console
            console.warn('[muassis] bundled NIM fallback DISABLED. Set NVIDIA_NIM_BASE_URL=https://taseesai-nim-proxy.vercel.app/v1, ' +
                'or supply NVIDIA_NIM_API_KEY explicitly. Set MUASSIS_ALLOW_BUNDLED_NIM=true to re-enable the obfuscated bundled key (NOT recommended — extractable by anyone with the dist).');
            warnedThisProcess = true;
        }
        return null;
    }
    try {
        const pad = Buffer.from(BUNDLED_PAD_B64, 'base64');
        const payload = Buffer.from(BUNDLED_PAYLOAD_B64, 'base64');
        if (pad.length === 0 || pad.length !== payload.length)
            return null;
        const out = Buffer.alloc(pad.length);
        for (let i = 0; i < pad.length; i++) {
            out[i] = pad[i] ^ payload[i];
        }
        const result = out.toString('utf-8');
        if (!/^nvapi-[A-Za-z0-9_-]{40,}$/.test(result))
            return null;
        if (!warnedThisProcess) {
            // eslint-disable-next-line no-console
            console.warn('[muassis] using BUNDLED NIM key — extractable from the dist tarball. Replace with proxy ASAP. (Set MUASSIS_ALLOW_BUNDLED_NIM=false to disable.)');
            warnedThisProcess = true;
        }
        return result;
    }
    catch {
        return null;
    }
}
// ─── Bundled Ollama Cloud key (zero-config demo default) ──────────────────
// Distinct from the audit-disabled NIM key above. The shipped DEFAULT chain
// uses model `kimi-k2.5`, which lives on Ollama Cloud (https://ollama.com/v1),
// NOT on NVIDIA's catalog. Without a working key+base-url a fresh friend
// install sends `kimi-k2.5` to the dead Vercel proxy → 404/400 on every chat.
// This bundled Ollama key + base-url makes a fresh install chat out-of-box,
// matching exactly what the owner's local .env does. ENABLED by default
// (demo-phase "zero user setup" design). Disable with MUASSIS_NO_BUNDLED_OLLAMA=1.
// Same XOR-obfuscation caveats as the NIM key: extractable from the dist,
// shared quota across all installs, rotate by regenerating PAD/PAY below.
const BUNDLED_OLLAMA_PAD_B64 = '';
const BUNDLED_OLLAMA_PAYLOAD_B64 = '';
let warnedOllamaThisProcess = false;
export function getBundledOllamaKey() {
    if (process.env.MUASSIS_NO_BUNDLED_OLLAMA === '1')
        return null;
    try {
        const pad = Buffer.from(BUNDLED_OLLAMA_PAD_B64, 'base64');
        const payload = Buffer.from(BUNDLED_OLLAMA_PAYLOAD_B64, 'base64');
        if (pad.length === 0 || pad.length !== payload.length)
            return null;
        const out = Buffer.alloc(pad.length);
        for (let i = 0; i < pad.length; i++) {
            out[i] = pad[i] ^ payload[i];
        }
        const result = out.toString('utf-8');
        // Ollama Cloud keys are long opaque tokens; sanity-check length only.
        if (result.length < 32)
            return null;
        if (!warnedOllamaThisProcess) {
            // eslint-disable-next-line no-console
            console.warn('[muassis] using BUNDLED Ollama Cloud key for zero-config chat (deepseek-v3.1 @ ollama.com/v1). ' +
                'Shared quota across installs + extractable from the dist. Add your own key in Settings to use a private provider. ' +
                '(Set MUASSIS_NO_BUNDLED_OLLAMA=1 to disable.)');
            warnedOllamaThisProcess = true;
        }
        return result;
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=bundled-key.js.map