Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import { getUnifiedModelsResponse as fetchUnifiedModels } from "@/app/api/v1/models/catalog";
import { getProviderNodeById } from "@/lib/db/providers/nodes";
import { getServiceModels } from "@/lib/db/serviceModels";
import { isServiceBackendPluginId } from "@/lib/services/serviceBackends";
import { getRegistryEntry } from "@omniroute/open-sse/config/providerRegistry.ts";
import { getProviderById, getProviderByAlias } from "@/shared/constants/providers";
import { isCompatibleProviderConnectionId } from "@/shared/utils/compatibleProviderId";
import { stripStaleEncodingHeaders } from "@omniroute/open-sse/utils/upstreamResponseHeaders.ts";
// Object field, not a live ESM binding: tests replace `load` so a planted
// upstream response reaches GET. Production leaves it pointing at the catalog.
export const unifiedModels = { load: fetchUnifiedModels };
/**
* Handle CORS preflight
*/
export async function OPTIONS() {
return new Response(null, {
headers: {
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers": "*",
},
});
}
/**
* GET /v1/providers/{provider}/models
* Returns models for one provider with unprefixed ids.
*/
export async function GET(request: Request, { params }: { params: Promise<{ provider: string }> }) {
const { provider: rawProvider } = await params;
if (isServiceBackendPluginId(rawProvider)) {
const models = getServiceModels(rawProvider).filter((model) => model.available !== false);
return Response.json({
object: "list",
data: models.map((model) => ({
object: model.object || "model",
owned_by: rawProvider,
...model,
id: model.id,
parent: null,
})),
});
}
const providerEntry = getRegistryEntry(rawProvider);
let providerId = rawProvider;
let providerAlias = rawProvider;
let compatiblePrefix: string | null = null;
if (providerEntry) {
providerId = providerEntry.id;
providerAlias = providerEntry.alias || providerId;
} else {
// Fall back to the dashboard-facing provider catalog (covers LOCAL_PROVIDERS, SEARCH_PROVIDERS, etc.)
const catalogEntry = getProviderById(rawProvider) ?? getProviderByAlias(rawProvider);
if (catalogEntry) {
providerId = catalogEntry.id;
providerAlias = catalogEntry.alias || providerId;
} else {
// Allow fetching models by connection ID for compatible providers
const isCompatibleConnectionId = isCompatibleProviderConnectionId(rawProvider);
if (!isCompatibleConnectionId) {
return Response.json(
{
error: {
message: `Unknown provider: ${rawProvider}`,
type: "invalid_request_error",
code: "invalid_provider",
},
},
{ status: 400 }
);
}
const compatibleNode = (await getProviderNodeById(rawProvider)) as {
prefix?: unknown;
} | null;
compatiblePrefix =
typeof compatibleNode?.prefix === "string" && compatibleNode.prefix.trim().length > 0
? compatibleNode.prefix.trim()
: null;
}
}
const response = await unifiedModels.load(request);
const payload = (await response
.clone()
.json()
.catch(() => null)) as { object?: string; data?: Array<Record<string, any>> } | null;
if (!response.ok || !payload || !Array.isArray(payload.data)) {
return response;
}
const toUnprefixedModelId = (model: Record<string, any>) => {
const root = typeof model.root === "string" && model.root.trim().length > 0 ? model.root : null;
if (root) return root;
const id = typeof model.id === "string" ? model.id : "";
if (!id) return id;
if (id.startsWith(`${providerAlias}/`)) return id.slice(providerAlias.length + 1);
if (id.startsWith(`${providerId}/`)) return id.slice(providerId.length + 1);
if (compatiblePrefix && id.startsWith(`${compatiblePrefix}/`)) {
return id.slice(compatiblePrefix.length + 1);
}
return id;
};
const acceptedOwners = new Set([providerId, providerAlias]);
if (compatiblePrefix) acceptedOwners.add(compatiblePrefix);
const filtered = payload.data.filter(
(model) => typeof model?.owned_by === "string" && acceptedOwners.has(model.owned_by)
);
const deduped = new Map<string, Record<string, any>>();
for (const model of filtered) {
const unprefixedId = toUnprefixedModelId(model);
if (!unprefixedId) continue;
if (deduped.has(unprefixedId)) continue;
deduped.set(unprefixedId, {
...model,
id: unprefixedId,
parent: null,
});
}
// Re-serialization changes the byte length, so the catalog's stale
// content-length/content-encoding/transfer-encoding must not ride onto this
// smaller body — a stale length makes the response never complete for the
// client (the dashboard model picker hangs on "loading" forever, #14092).
const headers = stripStaleEncodingHeaders(response.headers);
return Response.json(
{
object: payload.object || "list",
data: [...deduped.values()],
},
{
status: response.status,
headers,
}
);
}