diff --git a/src/app/api/providers/[id]/models/route.ts b/src/app/api/providers/[id]/models/route.ts index 83d7a77f31..f2d4bcb2dc 100755 --- a/src/app/api/providers/[id]/models/route.ts +++ b/src/app/api/providers/[id]/models/route.ts @@ -923,10 +923,52 @@ export async function GET( }); } - const fallback = buildLocalCatalogResponse( - "No remote models discovered — using local catalog" - ); - if (fallback) return fallback; + // Empty discovery just cleared THIS connection's synced cache (via + // persistDiscoveredModels([])). `providerSyncedModels` was read at the top + // of the handler and is now stale, so it must not leak the just-cleared + // models back into the response (#3148 made synced authoritative for the + // normal path; here we re-read the current state instead). Re-derive the + // local catalog from the provider's remaining synced models (union across + // its other connections) or the static catalog when none remain. + let freshSynced: Awaited> = []; + try { + freshSynced = await getSyncedAvailableModels(provider); + } catch { + /* DB unavailable — fall through to static catalog */ + } + const freshRegistry = freshSynced.length + ? freshSynced.map((m) => ({ + id: m.id, + name: m.name || m.id, + ...(m.apiFormat ? { apiFormat: m.apiFormat } : {}), + ...(m.supportedEndpoints ? { supportedEndpoints: m.supportedEndpoints } : {}), + })) + : getModelsByProviderId(provider) || []; + const freshSpecialty = freshSynced.length ? [] : getStaticModelsForProvider(provider) || []; + const freshLocal = mergeLocalCatalogModels(freshRegistry, freshSpecialty).map((model) => ({ + id: model.id, + name: model.name || model.id, + ...((model as Record).apiFormat + ? { apiFormat: (model as Record).apiFormat as string | undefined } + : {}), + ...((model as Record).supportedEndpoints + ? { + supportedEndpoints: (model as Record).supportedEndpoints as + | string[] + | undefined, + } + : {}), + ...(freshRegistry.length > 0 ? { owned_by: provider } : {}), + })); + if (freshLocal.length > 0) { + return buildResponse({ + provider, + connectionId, + models: freshLocal, + source: "local_catalog", + warning: "No remote models discovered — using local catalog", + }); + } return buildResponse({ provider,