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

6.5 KB
Raw Blame
import { isRetiredGitHubCopilotModelId } from "@omniroute/open-sse/config/providers/registry/github/retiredModels.ts";
import type { VertexModelMetadataProvenance } from "@/lib/providerModels/vertexModelMetadata";

import { asRecord, toNonEmptyString } from "./shared";

export interface SyncedAvailableModel {
  id: string;
  name: string;
  source: "imported";
  /**
   * Set on rows derived from the static registry (unionRegistryDispatchModels)
   * rather than from provider discovery; absent on discovery-synced rows. Any
   * future persist-back of catalog rows must never launder registry rows into
   * the syncedAvailableModels snapshot.
   */
  catalogOrigin?: "registry";
  /** Upstream picker visibility; does not restrict explicit inference requests. */
  visibility?: "hide" | "list";
  supportedInApi?: boolean;
  minimalClientVersion?: string;
  apiFormat?: string;
  targetFormat?: string;
  upstreamProtocol?: string;
  supportedEndpoints?: string[];
  endpointCapabilitySource?: "declared" | "inferred" | "defaulted" | "operator";
  supportedThinkingEfforts?: string[];
  defaultThinkingEffort?: string;
  inputTokenLimit?: number;
  contextWindow?: number;
  outputTokenLimit?: number;
  metadataProvenance?: VertexModelMetadataProvenance;
  description?: string;
  supportsThinking?: boolean;
  alwaysThinking?: boolean;
  supportsTools?: boolean;
  supportsVideo?: boolean;
  /** Discovery economics verdict; false preserves explicit paid evidence. */
  isFree?: boolean;
  // #4264: image-input capability captured at sync time (e.g. OpenRouter
  // `architecture.input_modalities`/`modality`) so the catalog can surface vision.
  supportsVision?: boolean;
  dimensions?: number;
  supportedInputTypes?: string[];
  modelType?: "chat" | "embedding" | "image" | "rerank";
}

export type SyncedAvailableModelInput = Omit<SyncedAvailableModel, "source"> & {
  source?: string;
};

function normalizeSyncedAvailableModel(model: unknown): SyncedAvailableModel | null {
  const record = asRecord(model);
  const id =
    toNonEmptyString(record.id) || toNonEmptyString(record.name) || toNonEmptyString(record.model);
  if (!id) return null;

  const name =
    toNonEmptyString(record.name) ||
    toNonEmptyString(record.displayName) ||
    toNonEmptyString(record.model) ||
    id;
  const supportedEndpoints = Array.isArray(record.supportedEndpoints)
    ? Array.from(
        new Set(
          record.supportedEndpoints
            .map((endpoint) => toNonEmptyString(endpoint))
            .filter((endpoint): endpoint is string => Boolean(endpoint))
        )
      ).sort()
    : undefined;

  return {
    id,
    name,
    source: "imported",
    ...(record.visibility === "hide" || record.visibility === "list"
      ? { visibility: record.visibility }
      : {}),
    ...(typeof record.supportedInApi === "boolean"
      ? { supportedInApi: record.supportedInApi }
      : {}),
    ...(toNonEmptyString(record.minimalClientVersion)
      ? { minimalClientVersion: toNonEmptyString(record.minimalClientVersion)! }
      : {}),
    ...(toNonEmptyString(record.apiFormat)
      ? { apiFormat: toNonEmptyString(record.apiFormat)! }
      : {}),
    ...(toNonEmptyString(record.targetFormat)
      ? { targetFormat: toNonEmptyString(record.targetFormat)! }
      : {}),
    // catalogOrigin is deliberately NOT copied from input records: the only
    // source is unionRegistryDispatchRows, so the registry-vs-discovery
    // marker cannot be forged through operator-supplied metadata.
    ...(toNonEmptyString(record.upstreamProtocol)
      ? { upstreamProtocol: toNonEmptyString(record.upstreamProtocol)! }
      : {}),
    ...(supportedEndpoints && supportedEndpoints.length > 0 ? { supportedEndpoints } : {}),
    ...(["declared", "inferred", "defaulted", "operator"].includes(
      String(record.endpointCapabilitySource)
    )
      ? {
          endpointCapabilitySource:
            record.endpointCapabilitySource as SyncedAvailableModel["endpointCapabilitySource"],
        }
      : {}),
    ...(Array.isArray(record.supportedThinkingEfforts)
      ? {
          supportedThinkingEfforts: record.supportedThinkingEfforts.filter(
            (effort): effort is string => typeof effort === "string" && effort.length > 0
          ),
        }
      : {}),
    ...(toNonEmptyString(record.defaultThinkingEffort)
      ? { defaultThinkingEffort: toNonEmptyString(record.defaultThinkingEffort)! }
      : {}),
    ...(typeof record.inputTokenLimit === "number"
      ? { inputTokenLimit: record.inputTokenLimit }
      : {}),
    ...(typeof record.contextWindow === "number" ? { contextWindow: record.contextWindow } : {}),
    ...(typeof record.outputTokenLimit === "number"
      ? { outputTokenLimit: record.outputTokenLimit }
      : {}),
    ...(record.metadataProvenance && typeof record.metadataProvenance === "object"
      ? {
          metadataProvenance: record.metadataProvenance as VertexModelMetadataProvenance,
        }
      : {}),
    ...(typeof record.description === "string" ? { description: record.description } : {}),
    ...(typeof record.supportsThinking === "boolean"
      ? { supportsThinking: record.supportsThinking }
      : {}),
    ...(record.alwaysThinking === true ? { alwaysThinking: true } : {}),
    ...(typeof record.supportsTools === "boolean" ? { supportsTools: record.supportsTools } : {}),
    ...(typeof record.supportsVideo === "boolean" ? { supportsVideo: record.supportsVideo } : {}),
    ...(typeof record.isFree === "boolean" ? { isFree: record.isFree } : {}),
    ...(record.supportsVision === true ? { supportsVision: true } : {}),
    ...(typeof record.dimensions === "number" && record.dimensions > 0
      ? { dimensions: record.dimensions }
      : {}),
    ...(Array.isArray(record.supportedInputTypes)
      ? {
          supportedInputTypes: record.supportedInputTypes.filter(
            (t): t is string => typeof t === "string" && t.length > 0
          ),
        }
      : {}),
    ...(typeof record.modelType === "string"
      ? { modelType: record.modelType as "chat" | "embedding" | "image" | "rerank" }
      : {}),
  };
}

export function normalizeSyncedAvailableModels(
  models: unknown,
  providerId?: string
): SyncedAvailableModel[] {
  if (!Array.isArray(models)) return [];
  const deduped = new Map<string, SyncedAvailableModel>();
  for (const model of models) {
    const normalized = normalizeSyncedAvailableModel(model);
    if (normalized && !isRetiredGitHubCopilotModelId(providerId, normalized.id)) {
      deduped.set(normalized.id, normalized);
    }
  }
  return Array.from(deduped.values());
}