Repository navigation
fix(provider-models): delete an individual synced model, persistently (#3199) #3211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -714,7 +714,11 @@ export async function replaceSyncedAvailableModelsForConnection( | |
| ): Promise<SyncedAvailableModel[]> { | ||
| const db = getDbInstance(); | ||
| const key = `${providerId}:${connectionId}`; | ||
| const normalizedModels = normalizeSyncedAvailableModels(models); | ||
| // #3199 — a model the operator explicitly deleted is marked hidden; skip those | ||
| // here so an auto-fetch re-import cannot re-add a deleted/synced model. | ||
| const normalizedModels = normalizeSyncedAvailableModels(models).filter( | ||
| (m) => !getModelIsHidden(providerId, m.id) | ||
| ); | ||
| if (normalizedModels.length === 0) { | ||
| db.prepare("DELETE FROM key_value WHERE namespace = 'syncedAvailableModels' AND key = ?").run( | ||
| key | ||
|
|
@@ -762,6 +766,71 @@ export async function deleteSyncedAvailableModelsForProvider(providerId: string) | |
| return Number(result.changes || 0); | ||
| } | ||
|
|
||
| /** | ||
| * Remove a single model from the synced available-model set for a provider (#3199). | ||
| * | ||
| * Without a `connectionId` the model is removed from every connection list that | ||
| * advertises it (the lists are unioned on read). With a `connectionId` only that | ||
| * connection's list is touched. Returns `true` if at least one list changed. | ||
| * | ||
| * Note: this only clears the stored copy. To make the deletion survive a later | ||
| * auto-fetch the caller must also mark the model hidden via | ||
| * `mergeModelCompatOverride(providerId, modelId, { isHidden: true })` — the sync | ||
| * write path (`replaceSyncedAvailableModelsForConnection`) skips hidden ids. | ||
| */ | ||
| export async function removeSyncedAvailableModel( | ||
| providerId: string, | ||
| modelId: string, | ||
| connectionId?: string | ||
| ): Promise<boolean> { | ||
| const db = getDbInstance(); | ||
| const targetId = toNonEmptyString(modelId); | ||
| if (!targetId) return false; | ||
|
|
||
| const rows: Array<{ key: string; value: string }> = []; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: Type annotation mismatch — |
||
| if (connectionId) { | ||
| const key = `${providerId}:${connectionId}`; | ||
| const row = db | ||
| .prepare("SELECT key, value FROM key_value WHERE namespace = 'syncedAvailableModels' AND key = ?") | ||
| .get(key); | ||
| const { key: k, value } = getKeyValue(row); | ||
| if (k && value !== null) rows.push({ key: k, value }); | ||
| } else { | ||
| const found = db | ||
| .prepare("SELECT key, value FROM key_value WHERE namespace = 'syncedAvailableModels' AND key LIKE ?") | ||
| .all(`${providerId}:%`); | ||
| for (const r of found) { | ||
| const { key, value } = getKeyValue(r); | ||
| if (key && value !== null) rows.push({ key, value }); | ||
| } | ||
| } | ||
|
|
||
| let changed = false; | ||
| for (const { key, value } of rows) { | ||
| let models: SyncedAvailableModel[]; | ||
| try { | ||
| models = normalizeSyncedAvailableModels(JSON.parse(value)); | ||
| } catch { | ||
| continue; | ||
| } | ||
| const filtered = models.filter((m) => m.id !== targetId); | ||
| if (filtered.length === models.length) continue; | ||
| changed = true; | ||
| if (filtered.length === 0) { | ||
| db.prepare("DELETE FROM key_value WHERE namespace = 'syncedAvailableModels' AND key = ?").run( | ||
| key | ||
| ); | ||
| } else { | ||
| db.prepare( | ||
| "UPDATE key_value SET value = ? WHERE namespace = 'syncedAvailableModels' AND key = ?" | ||
| ).run(JSON.stringify(filtered), key); | ||
| } | ||
| } | ||
|
|
||
| if (changed) backupDbFile("pre-write"); | ||
| return changed; | ||
| } | ||
|
|
||
| /** | ||
| * Prune stale synced available models for a provider, keeping only the specified allowed connection IDs. | ||
| * Returns the number of keys deleted. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Regression guard for #3199 — a user could not delete an individual model from a | ||
| // llama-cpp (local/catalog) provider. Fetched models are stored as synced | ||
| // (authoritative, #3148) and there was no per-model synced delete, so deleting a | ||
| // model either failed or re-appeared on the next auto-fetch. | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
|
|
||
| const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-synced-delete-")); | ||
| process.env.DATA_DIR = TEST_DATA_DIR; | ||
|
|
||
| const core = await import("../../src/lib/db/core.ts"); | ||
| const modelsDb = await import("../../src/lib/db/models.ts"); | ||
| const discovery = await import("../../src/lib/providerModels/modelDiscovery.ts"); | ||
|
|
||
| async function resetStorage() { | ||
| core.resetDbInstance(); | ||
| for (let attempt = 0; attempt < 10; attempt++) { | ||
| try { | ||
| if (fs.existsSync(TEST_DATA_DIR)) { | ||
| fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); | ||
| } | ||
| break; | ||
| } catch (error: any) { | ||
| if ((error?.code === "EBUSY" || error?.code === "EPERM") && attempt < 9) { | ||
| await new Promise((resolve) => setTimeout(resolve, 50 * (attempt + 1))); | ||
| } else { | ||
| throw error; | ||
| } | ||
| } | ||
| } | ||
| fs.mkdirSync(TEST_DATA_DIR, { recursive: true }); | ||
| } | ||
|
|
||
| test.beforeEach(async () => { | ||
| await resetStorage(); | ||
| }); | ||
|
|
||
| test.after(async () => { | ||
| core.resetDbInstance(); | ||
| fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| const PROVIDER = "llamacpp"; | ||
| const CONN = "conn-1"; | ||
|
|
||
| test("removeSyncedAvailableModel removes a single model from the synced list", async () => { | ||
| await modelsDb.replaceSyncedAvailableModelsForConnection(PROVIDER, CONN, [ | ||
| { id: "model-a", name: "Model A" }, | ||
| { id: "model-b", name: "Model B" }, | ||
| { id: "model-c", name: "Model C" }, | ||
| ]); | ||
|
|
||
| const removed = await modelsDb.removeSyncedAvailableModel(PROVIDER, "model-b"); | ||
| assert.equal(removed, true); | ||
|
|
||
| const ids = (await modelsDb.getSyncedAvailableModels(PROVIDER)).map((m) => m.id).sort(); | ||
| assert.deepEqual(ids, ["model-a", "model-c"]); | ||
| }); | ||
|
|
||
| test("removeSyncedAvailableModel returns false for a missing model id", async () => { | ||
| await modelsDb.replaceSyncedAvailableModelsForConnection(PROVIDER, CONN, [ | ||
| { id: "model-a", name: "Model A" }, | ||
| ]); | ||
| const removed = await modelsDb.removeSyncedAvailableModel(PROVIDER, "does-not-exist"); | ||
| assert.equal(removed, false); | ||
| }); | ||
|
|
||
| test("deleted synced model stays deleted across a re-fetch (hidden marker)", async () => { | ||
| // Seed via the same discovery path the provider /models route uses. | ||
| await discovery.persistDiscoveredModels(PROVIDER, CONN, [ | ||
| { id: "model-a", name: "Model A" }, | ||
| { id: "model-b", name: "Model B" }, | ||
| ]); | ||
|
|
||
| // Delete model-b and mark it hidden (the persistent "deleted" marker). | ||
| await modelsDb.removeSyncedAvailableModel(PROVIDER, "model-b"); | ||
| modelsDb.mergeModelCompatOverride(PROVIDER, "model-b", { isHidden: true }); | ||
|
|
||
| assert.equal(modelsDb.getModelIsHidden(PROVIDER, "model-b"), true); | ||
|
|
||
| // Auto-fetch fires again and the upstream still advertises model-b. | ||
| await discovery.persistDiscoveredModels(PROVIDER, CONN, [ | ||
| { id: "model-a", name: "Model A" }, | ||
| { id: "model-b", name: "Model B" }, | ||
| ]); | ||
|
|
||
| // model-b must NOT come back: the merge/sync path skips hidden ids. | ||
| const ids = (await modelsDb.getSyncedAvailableModels(PROVIDER)).map((m) => m.id).sort(); | ||
| assert.deepEqual(ids, ["model-a"]); | ||
| }); | ||
|
|
||
| test("removeSyncedAvailableModel scoped to a single connection only", async () => { | ||
| await modelsDb.replaceSyncedAvailableModelsForConnection(PROVIDER, "conn-1", [ | ||
| { id: "model-a", name: "Model A" }, | ||
| { id: "shared", name: "Shared" }, | ||
| ]); | ||
| await modelsDb.replaceSyncedAvailableModelsForConnection(PROVIDER, "conn-2", [ | ||
| { id: "shared", name: "Shared" }, | ||
| ]); | ||
|
|
||
| const removed = await modelsDb.removeSyncedAvailableModel(PROVIDER, "shared", "conn-1"); | ||
| assert.equal(removed, true); | ||
|
|
||
| // conn-2 still has it, so the union still contains "shared". | ||
| const ids = (await modelsDb.getSyncedAvailableModels(PROVIDER)).map((m) => m.id).sort(); | ||
| assert.deepEqual(ids, ["model-a", "shared"]); | ||
|
|
||
| // conn-1 no longer has it. | ||
| const conn1Ids = (await modelsDb.getSyncedAvailableModelsForConnection(PROVIDER, "conn-1")) | ||
| .map((m) => m.id) | ||
| .sort(); | ||
| assert.deepEqual(conn1Ids, ["model-a"]); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a refresh/auto-fetch runs after a synced model was deleted, this filter prevents the hidden ID from being written back to SQLite, but
persistDiscoveredModels()still returns its pre-filtered normalized input andsrc/app/api/providers/[id]/models/route.tstreats any non-emptydiscoveredModelsas a successful API result and returns the raw upstreammodels. In that scenario the just-deleted model still appears in the/api/providers/[id]/models?refresh=trueresponse whenever the upstream advertises it, and if all discovered IDs are hidden the route also skips the intended local fallback; return/use the filtered list fromreplaceSyncedAvailableModelsForConnection()or filter the API response before deciding it is non-empty.Useful? React with 👍 / 👎.