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

1.6 KB
Raw Blame
/**
 * functionalGatewayPredicate.ts — catalog-side predicate for the functional
 * gateway mirror gate.
 *
 * Mirrors the 3-level gate of ccDiscoveryAliases (global > provider > model) but
 * for functional gateway mirrors. Storage reuses the same `key_value` namespace
 * conventions but under a dedicated feature flag (see db wiring in Task 4).
 */

export type FunctionalGatewaySetting = "on" | "off" | null;

export interface FunctionalGatewayGateSnapshot {
  global: boolean;
  providers: Map<string, FunctionalGatewaySetting>;
  models: Map<string, FunctionalGatewaySetting>;
}

function resolveSetting(
  model: FunctionalGatewaySetting,
  provider: FunctionalGatewaySetting,
  global: boolean
): boolean {
  if (model === "off") return false;
  if (model === "on") return true;
  if (provider === "off") return false;
  if (provider === "on") return true;
  return global;
}

/**
 * Builds a predicate suitable for filtering `appendFunctionalGatewayMirrors`
 * output from a single settings snapshot.
 */
export function buildFunctionalGatewayPredicate(
  snapshot: FunctionalGatewayGateSnapshot
): (entry: { id?: unknown; owned_by?: unknown }) => boolean {
  return (entry) => {
    const id = entry.id;
    if (typeof id !== "string" || id.length === 0) return false;

    const slashIndex = id.indexOf("/");
    if (slashIndex === -1) return snapshot.global;

    const providerId = id.slice(0, slashIndex);
    const modelId = id.slice(slashIndex + 1);
    const provider = snapshot.providers.get(providerId) ?? null;
    const model = snapshot.models.get(`${providerId}/${modelId}`) ?? null;
    return resolveSetting(model, provider, snapshot.global);
  };
}