// Env + feature-gate resolution for ai-asset (Meshy-only via LiteLLM gateway). // // Real Meshy calls are OFF by default. They turn on ONLY when both: // 1. AIASSET_ENABLE_REAL_PROVIDERS === "1", and // 2. a 3D gateway key is present in Studio global .env (FORGEAX_3D_GATEWAY_KEY, // ANTHROPIC_API_KEY, or LITELLM_PROXY_KEY — see pickLitellmFromEnv). // Otherwise generation falls back to the deterministic no-quota mock. Secrets // are read from process.env (server loads $FORGEAX_PROJECT_ROOT/.env) or, for // standalone smokes, from the plugin-local .env. Nothing here is logged. import { existsSync, readFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; let loaded = false; // Minimal .env parser for the plugin-local file. Does not override values // already present in process.env (the server's .env wins). function loadPluginEnvOnce(): void { if (loaded) return; loaded = true; // Stable across runtimes: import.meta.dir is bun-specific, but // import.meta.url + fileURLToPath works in bun, node, and tsc typecheck. const envPath = resolve(dirname(fileURLToPath(import.meta.url)), '..', '.env'); if (!existsSync(envPath)) return; for (const rawLine of readFileSync(envPath, 'utf8').split('\n')) { const line = rawLine.trim(); if (!line || line.startsWith('#')) continue; const eq = line.indexOf('='); if (eq === -1) continue; const key = line.slice(0, eq).trim(); if (key in process.env) continue; let value = line.slice(eq + 1).trim(); if ( (value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'")) ) { value = value.slice(1, -1); } process.env[key] = value; } } function read(name: string): string | undefined { loadPluginEnvOnce(); const value = process.env[name]; return value && value.trim() ? value.trim() : undefined; } export function realProvidersEnabled(): boolean { return read('AIASSET_ENABLE_REAL_PROVIDERS') === '1'; } function toInt(value: string | undefined, fallback: number): number { const n = value ? Number.parseInt(value, 10) : NaN; return Number.isFinite(n) && n > 0 ? n : fallback; } export interface LitellmEnv { apiKey: string; baseUrl: string; } const DEFAULT_LITELLM_BASE = 'https://llm-proxy.forgeax.com'; function normalizeProxyBase(raw: string): string { return raw.replace(/\/+$/, '').replace(/\/v1$/, ''); } // 3D gateway base URL is resolved SEPARATELY from the chat/image // LITELLM_PROXY_BASE_URL: that variable points at the chat proxy (e.g. // Moonshot) which does NOT host /v1/3d/generations. Priority: // 1. FORGEAX_3D_GATEWAY_BASE_URL — 3D-only override (shared by gen3d / // ai-asset; ops pins a 3D-specific host) // 2. ANTHROPIC_BASE_URL — the forgeax proxy (same host that serves 3D) // 3. DEFAULT_LITELLM_BASE — https://llm-proxy.forgeax.com // LITELLM_PROXY_BASE_URL is intentionally NOT consulted for 3D. function resolveGatewayBaseUrl(env: Record): string { const dedicated = env.FORGEAX_3D_GATEWAY_BASE_URL?.trim(); if (dedicated) return normalizeProxyBase(dedicated); const anthropic = env.ANTHROPIC_BASE_URL?.trim(); if (anthropic) return normalizeProxyBase(anthropic); return DEFAULT_LITELLM_BASE; } /** * Resolve LiteLLM gateway credentials for 3D generation. The 3D gateway is * decoupled from the chat/image LITELLM_PROXY_* vars, which may point at a * different proxy (e.g. Moonshot) that does not host /v1/3d/generations. * * Key priority: * 1. FORGEAX_3D_GATEWAY_KEY — 3D-only override (shared by all 3D plugins) * 2. ANTHROPIC_API_KEY — the forgeax proxy key (default 3D gateway auth) * 3. LITELLM_PROXY_KEY — legacy fallback (kept for pre-decouple configs) * Base URL priority: FORGEAX_3D_GATEWAY_BASE_URL > ANTHROPIC_BASE_URL > default. * Plugin-local .env must NOT hold a separate gateway key — configure in * Studio Settings → API Keys. */ export function pickLitellmFromEnv(env: Record): LitellmEnv | null { const dedicatedKey = env.FORGEAX_3D_GATEWAY_KEY?.trim(); if (dedicatedKey) { return { apiKey: dedicatedKey, baseUrl: resolveGatewayBaseUrl(env) }; } const anthropicKey = env.ANTHROPIC_API_KEY?.trim(); if (anthropicKey) { return { apiKey: anthropicKey, baseUrl: resolveGatewayBaseUrl(env) }; } const proxyKey = env.LITELLM_PROXY_KEY?.trim(); if (proxyKey) { return { apiKey: proxyKey, baseUrl: resolveGatewayBaseUrl(env) }; } return null; } // Returns the LiteLLM gateway credentials, or null if not configured. // All 3D providers now route through this single gateway key. export function getLitellmEnv(): LitellmEnv | null { loadPluginEnvOnce(); return pickLitellmFromEnv(process.env); } export interface MeshyEnv { apiKey: string; baseUrl: string; // Low-poly target the plugin aims for by default (small props, not characters). defaultPolycount: number; pollIntervalMs: number; pollTimeoutMs: number; rateLimitPerMin: number; } // Returns null when the real Meshy path is not fully configured. Now reads the // LiteLLM gateway key instead of the direct MESHY_API_KEY. Callers MUST fall // back to mock when this is null — never turn on a quotaed path by accident. export function getMeshyEnv(): MeshyEnv | null { if (!realProvidersEnabled()) return null; const litellm = getLitellmEnv(); if (!litellm) return null; return { apiKey: litellm.apiKey, baseUrl: litellm.baseUrl, defaultPolycount: toInt(read('MESHY_DEFAULT_POLYCOUNT'), 6000), pollIntervalMs: toInt(read('MESHY_POLL_INTERVAL_MS'), 5000), pollTimeoutMs: toInt(read('MESHY_POLL_TIMEOUT_MS'), 600000), rateLimitPerMin: toInt(read('MESHY_RATE_LIMIT_PER_MIN'), 3), }; } export interface CosEnv { secretId: string; secretKey: string; bucket: string; region: string; // Presigned URL lifetime in seconds (input images are transfer artifacts). signExpiresSec: number; } // Returns null when COS upload is not fully configured. Unlike the provider env, // COS upload is a transport convenience and is NOT gated by // AIASSET_ENABLE_REAL_PROVIDERS: a user can upload a local image to host it even // in mock mode. Callers fall back to "URL only" when this is null. export function getCosEnv(): CosEnv | null { const secretId = read('COS_SECRET_ID'); const secretKey = read('COS_SECRET_KEY'); const bucket = read('COS_BUCKET'); const region = read('COS_REGION'); if (!secretId || !secretKey || !bucket || !region) return null; return { secretId, secretKey, bucket, region, signExpiresSec: toInt(read('COS_SIGN_EXPIRES_SEC'), 24 * 3600), }; }