Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/app/api/providers/[id]/models/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof getSyncedAvailableModels>> = [];
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<string, unknown>).apiFormat
? { apiFormat: (model as Record<string, unknown>).apiFormat as string | undefined }
: {}),
...((model as Record<string, unknown>).supportedEndpoints
? {
supportedEndpoints: (model as Record<string, unknown>).supportedEndpoints as
| string[]
| undefined,
}
: {}),
...(freshRegistry.length > 0 ? { owned_by: provider } : {}),
}));
Comment on lines +948 to +962

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The type assertions as Record<string, unknown>, as string | undefined, and as string[] | undefined are redundant here. Since mergeLocalCatalogModels returns an array of objects extending LocalCatalogModel, the properties apiFormat and supportedEndpoints are already typed correctly and can be accessed directly. Removing these assertions simplifies the code and improves readability.

      const freshLocal = mergeLocalCatalogModels(freshRegistry, freshSpecialty).map((model) => ({
        id: model.id,
        name: model.name || model.id,
        ...(model.apiFormat ? { apiFormat: model.apiFormat } : {}),
        ...(model.supportedEndpoints ? { supportedEndpoints: model.supportedEndpoints } : {}),
        ...(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,
Expand Down