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

3.3 KB
Raw Blame
/**
 * catalogOrder.ts — canonical provider-grouped ordering for GET /v1/models.
 *
 * The catalog is assembled by many independent push loops (auto-combos, named
 * combos, static registry, codex-native, synced, OpenRouter, specialty registries,
 * custom, alias-backed, connection-fallback). The same provider appears in several
 * loops with other providers interleaved, so its models land in multiple separated
 * blocks. This module applies ONE stable, provider-grouped sort at serialization.
 *
 * Group key = owned_by (the public owner identity), NOT the model-id prefix.
 * Built-in providers use their canonical id as owned_by; compatible nodes use the
 * configured node prefix. A single routable public prefix can differ from its owner:
 * a no-auth provider publishes `<alias>/<model>` while retaining its id as owned_by.
 * Grouping by the prefix would split one provider's models; grouping by owned_by
 * keeps them contiguous.
 *
 * Order: combo block (owned_by === "combo") pinned first, preserving #4164; then
 * providers in registry precedence (OAUTH -> NOAUTH -> APIKEY canonical keys); then
 * unknown providers by locale-independent code-unit order. Within a group the input
 * order is preserved (stable), keeping combo sort_order, connection priority, custom
 * append-order, and equal-id audio twins.
 *
 * Reorders rows only. Identity, alias mapping, Combo/bare compatibility (#6940/#8530),
 * effort-variant scoping, and Claude-mirror gating are untouched. Pure; no DB/IO.
 */

import { COMBO_GROUP, groupSortPriority } from "@/shared/constants/canonicalProviderOrder";

/**
 * Locale-independent code-unit comparator. UTF-16 code units put uppercase A-Z
 * (0x41-0x5A) before lowercase a-z (0x61-0x7A); byte-deterministic, no ICU.
 */
function codeUnitCompare(a: string, b: string): number {
  return a < b ? -1 : a > b ? 1 : 0;
}

/**
 * Grouping key for a catalog row: "combo" for combo-owned rows, else owned_by
 * (canonical identity). Rows with no usable owned_by fall back to the id-prefix;
 * that path is defensive only — every published row carries owned_by.
 */
function modelGroupKey(model: Record<string, unknown>): string {
  const ownedBy = typeof model.owned_by === "string" ? model.owned_by : "";
  if (ownedBy === "combo") return COMBO_GROUP;
  if (ownedBy) return ownedBy;

  const id = typeof model.id === "string" ? model.id : "";
  const slash = id.indexOf("/");
  return slash > 0 ? id.slice(0, slash) : id;
}

/**
 * Stable provider-grouped sort. Does not mutate the input. Deterministic for a
 * given input array.
 */
export function sortCatalogModelsProviderGrouped<T extends Record<string, unknown>>(
  models: T[]
): T[] {
  if (!Array.isArray(models) || models.length < 2) return models;

  const annotated = models.map((model, index) => {
    const groupKey = modelGroupKey(model);
    return { model, groupKey, priority: groupSortPriority(groupKey), index };
  });

  annotated.sort((a, b) => {
    if (a.priority !== b.priority) return a.priority - b.priority;
    // Unknown groups (both Infinity): code-unit order by key.
    if (a.priority === Infinity && b.priority === Infinity) {
      const cmp = codeUnitCompare(a.groupKey, b.groupKey);
      if (cmp !== 0) return cmp;
    }
    // Stable: preserve input order within a group.
    return a.index - b.index;
  });

  return annotated.map((entry) => entry.model);
}