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.1 KB
Raw Blame
/**
 * Pure, dependency-free resolution of which scope(s) a subscription's synced
 * proxy pool should be bound to.
 *
 * Extracted from `subscriptionService.resolveTargetScopes` so the routing
 * rule is unit-testable without the full DB / Next.js stack.
 */

export type TargetScope = { scope: "global" | "provider"; scopeId: string | null };

export interface ScopeInput {
  mode: "global" | "rule";
  ruleProviders?: string[] | null;
}

/**
 * Resolve the target scopes for a subscription.
 *
 *  - `global` mode (or `rule` mode with no providers selected) binds the
 *    global scope, so every provider's traffic is proxied.
 *  - `rule` mode with providers binds one provider scope per selected
 *    provider, so only those providers' traffic is proxied and the rest go
 *    direct.
 */
export function resolveTargetScopes(sub: ScopeInput): TargetScope[] {
  if (sub.mode === "rule" && sub.ruleProviders && sub.ruleProviders.length > 0) {
    return sub.ruleProviders.map((p) => ({ scope: "provider" as const, scopeId: p }));
  }
  // global mode, or rule mode with no providers selected → bind global.
  return [{ scope: "global" as const, scopeId: null }];
}