Skip to content

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

5.8 KB
Raw Blame
/**
 * Parses the Google Generative Language `v1beta/models` listing into discovery models.
 *
 * Each model's `supportedGenerationMethods` is mapped to OmniRoute endpoints:
 *   - generateContent / generateAnswer → "chat"
 *   - predict                          → "images"  (Imagen image generation)
 *   - predictLongRunning               → "videos"  (Veo video generation)
 *   - embedContent                     → "embeddings"
 *   - bidiGenerateContent              → ignored   (Gemini Live is not proxied)
 *
 * Model-id heuristics refine the long-running bucket because Google exposes both
 * Imagen and Veo via long-running methods on the same endpoint:
 *   - id contains "veo"    → ensure "videos"
 *   - id contains "imagen" → force "images" (never "videos")
 *   - Gemini *-tts[-preview] via generateContent → "audio-speech"
 *   - Gemini *-transcribe via generateContent  → "audio-transcriptions"
 *
 * Note: `gemini-*-image` models (e.g. gemini-3-pro-image) generate images via the
 * regular `generateContent` path, so they stay "chat" (image output is a chat
 * modality) and are intentionally NOT reclassified as "images".
 *
 * This is shared by the `gemini` discovery config and the `vertex` /
 * `vertex-partner` (incl. Vertex AI Express key) discovery branches, so every
 * supported model the account can access — chat, image, video and embeddings —
 * surfaces dynamically instead of being limited to the small static registry.
 */
const METHOD_TO_ENDPOINT: Record<string, string> = {
  generateContent: "chat",
  embedContent: "embeddings",
  predict: "images",
  predictLongRunning: "videos",
  bidiGenerateContent: "audio",
  generateAnswer: "chat",
};

const IGNORED_METHODS = new Set([
  "countTokens",
  "countTextTokens",
  "createCachedContent",
  "batchGenerateContent",
  "asyncBatchEmbedContent",
  "bidiGenerateContent",
]);

const RETIRED_GEMINI_MODEL_IDS = new Set(["gemini-3.5-flash"]);

export interface GeminiDiscoveryModel {
  id: string;
  name: string;
  supportedEndpoints: string[];
  endpointCapabilitySource: "declared" | "inferred" | "defaulted";
  inputTokenLimit?: number;
  outputTokenLimit?: number;
  description?: string;
  supportsThinking?: boolean;
  [key: string]: unknown;
}

/** Keep specialty inference outside the catalog parsing hot path. */
function inferGeminiAudioEndpoint(
  lowerId: string,
  methods: readonly string[]
): "audio-transcriptions" | "audio-speech" | null {
  if (!methods.includes("generateContent")) return null;
  if (/-transcribe(?:-|$)/.test(lowerId)) return "audio-transcriptions";
  if (/-tts(?:-|$)/.test(lowerId)) return "audio-speech";
  return null;
}

export function parseGeminiModelsList(data: any): GeminiDiscoveryModel[] {
  return (data?.models || [])
    .map((m: Record<string, unknown>) => {
      const methods: string[] = Array.isArray(m.supportedGenerationMethods)
        ? (m.supportedGenerationMethods as string[])
        : [];

      const endpoints = new Set<string>(
        methods
          .filter((method) => !IGNORED_METHODS.has(method))
          .map((method) => METHOD_TO_ENDPOINT[method] || "chat")
      );

      const id = ((m.name as string) || (m.id as string) || "").replace(/^models\//, "");
      const lowerId = id.toLowerCase();
      let inferredById = false;

      // Google exposes Imagen (image) and Veo (video) via long-running methods; the
      // method alone can't always distinguish them, so refine by model id.
      if (lowerId.includes("veo")) {
        inferredById = true;
        endpoints.add("videos");
      }
      if (lowerId.includes("imagen")) {
        inferredById = true;
        endpoints.delete("videos");
        endpoints.add("images");
      }

      // Lyria generates music, not chat responses, even when advertised via
      // generateContent. A matching Vertex music adapter exists, but AI Studio
      // must remain visibly unsupported until it has its own adapter.
      if (lowerId.startsWith("lyria-") && methods.includes("generateContent")) {
        inferredById = true;
        endpoints.delete("chat");
        endpoints.add("music");
      }

      // Google serves specialized speech/transcription models through
      // generateContent too. They do NOT support Chat Completions: importing
      // them as "chat" makes the router try an incompatible endpoint (#16929).
      // Only override the method-derived chat tag, preserving any other
      // explicitly advertised endpoint. Image-capable Gemini chat models
      // intentionally remain chat-selectable.
      const audioEndpoint = inferGeminiAudioEndpoint(lowerId, methods);
      if (audioEndpoint) {
        inferredById = true;
        endpoints.delete("chat");
        endpoints.add(audioEndpoint);
      }

      if (
        endpoints.size === 0 &&
        methods.length > 0 &&
        methods.every((method) => IGNORED_METHODS.has(method))
      ) {
        return null;
      }
      if (endpoints.size === 0) endpoints.add("chat");

      return {
        ...m,
        id,
        name: (m.displayName as string) || id,
        supportedEndpoints: [...endpoints],
        endpointCapabilitySource: inferredById
          ? "inferred"
          : methods.some((method) =>
                ["embedContent", "predict", "predictLongRunning"].includes(method)
              )
            ? "declared"
            : "defaulted",
        ...(typeof m.inputTokenLimit === "number" ? { inputTokenLimit: m.inputTokenLimit } : {}),
        ...(typeof m.outputTokenLimit === "number" ? { outputTokenLimit: m.outputTokenLimit } : {}),
        ...(typeof m.description === "string" ? { description: m.description } : {}),
        ...(m.thinking === true ? { supportsThinking: true } : {}),
      } as GeminiDiscoveryModel;
    })
    .filter(
      (model: GeminiDiscoveryModel | null): model is GeminiDiscoveryModel =>
        Boolean(model) && !RETIRED_GEMINI_MODEL_IDS.has(model.id)
    );
}