useGatewayModels
neon-ui/use-gateway-modelsFreeHook
Fetches a gateway model catalog and keeps the selection valid against it: explicit pick, then preferred default, then first available.
Install it
npx shadcn@latest add https://ui.neon.com/r/use-gateway-models.jsonSource
1"use client";23import { useEffect, useMemo, useState } from "react";45/** Structurally compatible with ModelSelect's AiModel. */6export interface GatewayModel {7 /** Gateway model id in short form, e.g. "gpt-5-2". */8 id: string;9 /** Human-readable name, e.g. "GPT-5.2". */10 name: string;11 /** Provider name used for grouping, e.g. "OpenAI". */12 provider: string;13 /** Whether the model supports extended reasoning. */14 reasoning?: boolean;15}1617export type GatewayModelsStatus = "loading" | "ready" | "error";1819export interface UseGatewayModelsOptions {20 /**21 * URL of the catalog endpoint. Point it at your server-side proxy in22 * front of the gateway's `GET /v1/models` — the bearer token must not23 * ship to the browser. Accepts either the raw OpenAI-compatible shape24 * (`{ data: [...] }`, filtered to enabled models) or a pre-shaped25 * `{ models: GatewayModel[] }` payload.26 */27 endpoint?: string;28 /** Preferred model id; wins whenever the catalog includes it. */29 defaultModel?: string;30}3132/** Raw entry from the gateway's OpenAI-compatible `GET /v1/models`. */33interface RawGatewayModel {34 id: string;35 name?: string;36 owned_by?: string;37 enabled?: boolean;38}3940/** Display names for `owned_by` slugs the gateway reports. */41const PROVIDER_NAMES: Record<string, string> = {42 alibaba: "Alibaba",43 anthropic: "Anthropic",44 databricks: "Databricks",45 google: "Google",46 meta: "Meta",47 "meta-llama": "Meta",48 mistral: "Mistral",49 moonshot: "Moonshot AI",50 openai: "OpenAI",51 qwen: "Qwen",52 zhipu: "Zhipu AI",53};5455const shapeModels = (payload: unknown): GatewayModel[] => {56 if (typeof payload !== "object" || payload === null) {57 return [];58 }5960 // Pre-shaped proxy response: { models: GatewayModel[] }.61 if ("models" in payload && Array.isArray(payload.models)) {62 return payload.models as GatewayModel[];63 }6465 // OpenAI-compatible response: { data: [...] }, keep enabled models only.66 if ("data" in payload && Array.isArray(payload.data)) {67 return (payload.data as RawGatewayModel[])68 .filter((model) => model.enabled !== false)69 .map((model) => ({70 id: model.id,71 name: model.name || model.id,72 provider:73 PROVIDER_NAMES[model.owned_by ?? ""] ?? model.owned_by ?? "Other",74 }));75 }7677 return [];78};7980/**81 * The live model catalog of a Neon AI Gateway branch, with a selection82 * that is always valid against it: an explicit pick wins while listed,83// … truncated
Files it writes
More from neon-ui
All 77 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| useConsumptionHistoryuse-consumption-history | neon-ui | Hooks | Free | no deps | |
| useRegionPinguse-region-ping | neon-ui | Hooks | Free | no deps |
