From faae82eae1b8ed0d1322213d9116bb7398fae334 Mon Sep 17 00:00:00 2001 From: Chris Staley Date: Thu, 2 Apr 2026 10:18:26 -0600 Subject: [PATCH] feat(v1beta): use stored token limits and metadata in Gemini models endpoint --- src/app/api/v1beta/models/route.ts | 33 +++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/app/api/v1beta/models/route.ts b/src/app/api/v1beta/models/route.ts index 1838e63375..e9bbd3842f 100644 --- a/src/app/api/v1beta/models/route.ts +++ b/src/app/api/v1beta/models/route.ts @@ -1,5 +1,6 @@ import { CORS_ORIGIN } from "@/shared/utils/cors"; import { PROVIDER_MODELS } from "@/shared/constants/models"; +import { getAllCustomModels } from "@/lib/db/models"; /** * Handle CORS preflight @@ -16,13 +17,13 @@ export async function OPTIONS() { /** * GET /v1beta/models - Gemini compatible models list - * Returns models in Gemini API format + * Returns models in Gemini API format with real token limits when available. */ export async function GET() { try { - // Collect all models from all providers const models = []; + // Built-in models (hardcoded defaults) for (const [provider, providerModels] of Object.entries(PROVIDER_MODELS)) { for (const model of providerModels) { models.push({ @@ -36,8 +37,34 @@ export async function GET() { } } + // Custom models (use stored metadata from provider APIs) + try { + const customModelsMap = (await getAllCustomModels()) as Record; + for (const [providerId, rawModels] of Object.entries(customModelsMap)) { + if (!Array.isArray(rawModels)) continue; + for (const model of rawModels) { + if (!model || typeof model !== "object" || typeof (model as any).id !== "string") continue; + const m = model as Record; + if (m.isHidden === true) continue; + models.push({ + name: `models/${providerId}/${m.id}`, + displayName: m.name || m.id, + ...(typeof m.description === "string" ? { description: m.description } : {}), + supportedGenerationMethods: ["generateContent"], + inputTokenLimit: + typeof m.inputTokenLimit === "number" ? m.inputTokenLimit : 128000, + outputTokenLimit: + typeof m.outputTokenLimit === "number" ? m.outputTokenLimit : 8192, + ...(m.supportsThinking === true ? { thinking: true } : {}), + }); + } + } + } catch { + // Custom models are optional — skip on error + } + return Response.json({ models }); - } catch (error) { + } catch (error: any) { console.log("Error fetching models:", error); return Response.json({ error: { message: error.message } }, { status: 500 }); }