diff --git a/src/app/api/provider-models/route.ts b/src/app/api/provider-models/route.ts index c230d8fcca..5fbfcd3b92 100644 --- a/src/app/api/provider-models/route.ts +++ b/src/app/api/provider-models/route.ts @@ -258,7 +258,7 @@ export async function DELETE(request) { // DELETE /api/provider-models?provider=&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 }); } diff --git a/src/lib/db/models.ts b/src/lib/db/models.ts index 671fbc1465..74234a7f12 100644 --- a/src/lib/db/models.ts +++ b/src/lib/db/models.ts @@ -383,8 +383,17 @@ export async function replaceCustomModels( source?: string; apiFormat?: string; supportedEndpoints?: string[]; - }> + }>, + { allowEmpty = false }: { allowEmpty?: boolean } = {} ) { + // 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 (models.length === 0 && !allowEmpty) { + const existing = await getCustomModels(providerId); + return Array.isArray(existing) ? existing : []; + } + const db = getDbInstance(); const existing = await getCustomModels(providerId); const existingMap = new Map();