From 22656c86992deccabc79ea59dbf5a2a112fd9d22 Mon Sep 17 00:00:00 2001 From: oyi77 Date: Fri, 3 Apr 2026 04:49:41 +0700 Subject: [PATCH] Add LKGP cache exports and storage functions --- src/lib/db/settings.ts | 27 +++++++++++++++++++++++++++ src/lib/localDb.ts | 2 ++ 2 files changed, 29 insertions(+) diff --git a/src/lib/db/settings.ts b/src/lib/db/settings.ts index f723748f..05e2fc0a 100644 --- a/src/lib/db/settings.ts +++ b/src/lib/db/settings.ts @@ -744,3 +744,30 @@ export async function resetCacheMetrics() { ); return getCacheMetrics(); } + +// ──────────────── Last Known Good Provider (LKGP) ──────────────── + +export async function getLKGP(comboName: string, modelId: string): Promise { + const db = getDbInstance(); + const key = `lkgp:${comboName}:${modelId}`; + const row = db + .prepare("SELECT value FROM key_value WHERE namespace = 'lkgp' AND key = ?") + .get(key); + if (!row) return null; + const record = toRecord(row); + return typeof record.value === "string" ? record.value : null; +} + +export async function setLKGP( + comboName: string, + modelId: string, + providerId: string +): Promise { + const db = getDbInstance(); + const key = `lkgp:${comboName}:${modelId}`; + db.prepare("INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES ('lkgp', ?, ?)").run( + key, + providerId + ); + backupDbFile("pre-write"); +} diff --git a/src/lib/localDb.ts b/src/lib/localDb.ts index 521416da..4a3aa106 100644 --- a/src/lib/localDb.ts +++ b/src/lib/localDb.ts @@ -158,6 +158,8 @@ export { getCachedSettings, getCachedPricing, getCachedProviderConnections, + getCachedLKGP, + setCachedLKGP, invalidateDbCache, } from "./db/readCache";