fix(model-sync): skip replace when auto-sync returns empty model list

Prevent auto-sync from wiping manually-imported models when the upstream
/models endpoint fails, times out, or returns an empty list. Added
`allowEmpty` option (default false) to replaceCustomModels — callers that
intentionally clear all models (DELETE ?all=true) pass `allowEmpty: true`.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
tombii
2026-04-01 23:01:30 +02:00
parent 49c9eaa6e1
commit 557509ef84
2 changed files with 9 additions and 2 deletions

View File

@@ -258,7 +258,7 @@ export async function DELETE(request) {
// DELETE /api/provider-models?provider=<id>&all=true — clear all models
const all = searchParams.get("all");
if (all === "true") {
await replaceCustomModels(provider, []);
await replaceCustomModels(provider, [], { allowEmpty: true });
return Response.json({ cleared: true });
}

View File

@@ -383,7 +383,8 @@ export async function replaceCustomModels(
source?: string;
apiFormat?: string;
supportedEndpoints?: string[];
}>
}>,
{ allowEmpty = false }: { allowEmpty?: boolean } = {}
) {
const db = getDbInstance();
const existing = await getCustomModels(providerId);
@@ -420,6 +421,12 @@ export async function replaceCustomModels(
});
if (merged.length === 0) {
// Guard: skip destructive clear when the caller hasn't explicitly opted in.
// This prevents auto-sync from wiping manually-imported models when the
// upstream /models endpoint fails, times out, or returns an empty list.
if (!allowEmpty) {
return Array.isArray(existing) ? existing : [];
}
db.prepare("DELETE FROM key_value WHERE namespace = 'customModels' AND key = ?").run(
providerId
);