()
| 2024 | // ─── Init from .env ──────────────────────────────────────── |
| 2025 | |
| 2026 | export async function initAuth() { |
| 2027 | // Load persisted accounts first |
| 2028 | loadAccounts(); |
| 2029 | |
| 2030 | // Safety net: auto-reset stale inflight counters (fixes #165) |
| 2031 | startInflightCleanup(); |
| 2032 | |
| 2033 | const promises = []; |
| 2034 | |
| 2035 | // Load API keys from env (comma-separated) |
| 2036 | if (config.codeiumApiKey) { |
| 2037 | for (const key of config.codeiumApiKey.split(',').map(k => k.trim()).filter(Boolean)) { |
| 2038 | addAccountByKey(key); |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | // Load auth tokens from env (comma-separated) |
| 2043 | if (config.codeiumAuthToken) { |
| 2044 | for (const token of config.codeiumAuthToken.split(',').map(t => t.trim()).filter(Boolean)) { |
| 2045 | promises.push( |
| 2046 | addAccountByToken(token).catch(err => log.error(`Token auth failed: ${err.message}`)) |
| 2047 | ); |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | // Note: email/password login removed (Firebase API key not valid for direct login) |
| 2052 | // Use token-based auth instead |
| 2053 | |
| 2054 | if (promises.length > 0) await Promise.allSettled(promises); |
| 2055 | |
| 2056 | // Periodic re-probe so tier/capability info doesn't drift as quotas reset. |
| 2057 | const REPROBE_INTERVAL = 6 * 60 * 60 * 1000; |
| 2058 | setInterval(async () => { |
| 2059 | for (const a of accounts) { |
| 2060 | if (a.status !== 'active') continue; |
| 2061 | const admission = getLsAdmissionForAccount(a.id); |
| 2062 | if (!admission.ok || admission.reason !== 'already_running' || (admission.activeRequests || 0) > 0 || (admission.maintenanceRequests || 0) > 0 || isAccountBusyForProbe(a)) { |
| 2063 | log.info(`Scheduled probe ${a.id} skipped: ${admission.errorType || admission.reason} (wouldStart=${!!admission.wouldStart}, ls=${admission.key || '?'})`); |
| 2064 | continue; |
| 2065 | } |
| 2066 | try { await probeAccount(a.id, { allowLsStart: false }); } |
| 2067 | catch (e) { log.warn(`Scheduled probe ${a.id} failed: ${e.message}`); } |
| 2068 | } |
| 2069 | }, REPROBE_INTERVAL).unref?.(); |
| 2070 | |
| 2071 | // Periodic credit refresh (every 15 min). First run is fire-and-forget so |
| 2072 | // startup isn't blocked by cloud round-trips. |
| 2073 | const CREDIT_INTERVAL = 15 * 60 * 1000; |
| 2074 | const skipBusyMaintenance = shouldSkipBusyBackgroundMaintenance(); |
| 2075 | refreshAllCredits({ skipBusy: skipBusyMaintenance }).catch(e => log.warn(`Initial credit refresh: ${e.message}`)); |
| 2076 | setInterval(() => { |
| 2077 | refreshAllCredits({ skipBusy: skipBusyMaintenance }).catch(e => log.warn(`Scheduled credit refresh: ${e.message}`)); |
| 2078 | }, CREDIT_INTERVAL).unref?.(); |
| 2079 | |
| 2080 | // Fetch live model catalog from cloud and merge into hardcoded catalog. |
| 2081 | // Fire-and-forget — the hardcoded catalog is sufficient until this completes. |
| 2082 | fetchAndMergeModelCatalog().catch(e => log.warn(`Model catalog fetch: ${e.message}`)); |
| 2083 |
no test coverage detected