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.2 KB
Raw Blame
/**
 * `auto/thrifty` (#15546): the pool is built in subscription-ladder rung order
 * (plan-included -> free -> metered), but the auto scorer then re-sorts every
 * target by score, which can put a metered model ahead of a plan-included one.
 * For ladder routes the rung order is the contract, so after scoring we put the
 * targets back in the order the virtual pool delivered them.
 */
interface OrderedTarget {
  modelStr: string;
  connectionId?: string | null;
}

const keyOf = (t: OrderedTarget): string => `${t.modelStr}|${t.connectionId ?? ""}`;

/** Stable re-sort of `ranked` by position in `incoming`; unknown targets keep score order, last. */
export function restoreIncomingOrder<T extends OrderedTarget>(
  ranked: readonly T[],
  incoming: readonly OrderedTarget[]
): T[] {
  const position = new Map<string, number>();
  incoming.forEach((target, index) => {
    const key = keyOf(target);
    if (!position.has(key)) position.set(key, index);
  });
  return ranked
    .map((target, scoreIndex) => ({
      target,
      rung: position.get(keyOf(target)) ?? Number.POSITIVE_INFINITY,
      scoreIndex,
    }))
    .sort((a, b) => (a.rung === b.rung ? a.scoreIndex - b.scoreIndex : a.rung - b.rung))
    .map((entry) => entry.target);
}