/** * Next.js instrumentation hook — runs once per server process. * Schedules the model registry refresh (startup + interval) using the owner keys from the * environment. Users additionally trigger syncs with their own keys when they add a provider. */ export async function register() { if (process.env.NEXT_RUNTIME !== "nodejs") return; if (process.env.POLYLLM_DISABLE_SCHEDULER === "1") return; const { syncAllWithEnvKeys } = await import("@/lib/ai/registry"); const { log } = await import("@/lib/log"); const minutes = Number(process.env.MODEL_SYNC_INTERVAL_MINUTES ?? 360); const run = async (why: string) => { try { const results = await syncAllWithEnvKeys(why); log.info("scheduled model sync", { why, results: results.map((r) => ({ provider: r.provider, ok: r.ok, found: r.found, added: r.added, removed: r.removed })) }); } catch (e) { log.warn("scheduled model sync failed", { error: (e as Error).message }); } }; // Startup sync (delayed so the DB pool and migrations are ready). setTimeout(() => void run("startup"), 5_000).unref?.(); if (minutes > 0) setInterval(() => void run("schedule"), minutes * 60_000).unref?.(); }