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.1 KB
Raw Blame
/**
 * quota-scraping credential fields rendered by QuotaScrapingFields.tsx.
 *
 * Kept in a UI-free module on purpose: importing the .tsx pulls in
 * `@/shared/components`, whose barrel reaches untranspiled ESM deps
 * (@lobehub/icons) that the node:test runner cannot parse. Unit tests import
 * this file instead; the component re-exports it for existing callers.
 */

import { getProviderConnectionFamilyIds } from "@/shared/constants/providers";

/** Providers whose quota lives behind the Qwen/Model Studio console gateway (#9603). */
export const QWEN_TOKEN_PLAN_PROVIDERS = new Set(["qwen-cloud-token-plan", "bailian-coding-plan"]);

/** Providers whose quota lives behind the Volcano Engine console gateway. */
export const VOLCENGINE_PLAN_PROVIDERS = new Set([
  "volcengine-coding-plan",
  "volcengine-agent-plan",
]);

/** Providers whose Token Plan quota lives behind the Xiaomi MiMo console (#15753). */
export const XIAOMI_MIMO_PROVIDERS = new Set(["xiaomi-mimo", "xiaomi-mimo-token-plan"]);

export type QuotaScrapingFieldValues = {
  ollamaCloudUsageCookie: string;
  alibabaConsoleCookie: string;
  alibabaConsoleSecToken: string;
  qwenCloudCookie: string;
  qwenCloudSecToken: string;
  volcConsoleCookie: string;
  xiaomiMimoConsoleCookie: string;
};

export const EMPTY_QUOTA_SCRAPING_FIELDS: QuotaScrapingFieldValues = {
  ollamaCloudUsageCookie: "",
  alibabaConsoleCookie: "",
  alibabaConsoleSecToken: "",
  qwenCloudCookie: "",
  qwenCloudSecToken: "",
  volcConsoleCookie: "",
  xiaomiMimoConsoleCookie: "",
};

export function assignQuotaScrapingProviderData(
  provider: string | undefined,
  values: QuotaScrapingFieldValues,
  target: Record<string, unknown>
) {
  if (provider === "ollama-cloud" && values.ollamaCloudUsageCookie.trim()) {
    target.ollamaCloudUsageCookie = values.ollamaCloudUsageCookie.trim();
  } else if (
    getProviderConnectionFamilyIds("alibaba").includes(provider) &&
    values.alibabaConsoleCookie.trim()
  ) {
    target.alibabaConsoleCookie = values.alibabaConsoleCookie.trim();
    if (values.alibabaConsoleSecToken.trim()) {
      target.alibabaConsoleSecToken = values.alibabaConsoleSecToken.trim();
    }
  } else if (QWEN_TOKEN_PLAN_PROVIDERS.has(provider ?? "") && values.qwenCloudCookie?.trim()) {
    // Optional access: callers (AddApiKeyModal/EditConnectionModal form state, and
    // existing tests) may pass a partial form object without the newer fields —
    // bailian-coding-plan previously matched no branch here at all.
    target.qwenCloudCookie = values.qwenCloudCookie.trim();
    if (values.qwenCloudSecToken?.trim()) {
      target.qwenCloudSecToken = values.qwenCloudSecToken.trim();
    }
  } else if (VOLCENGINE_PLAN_PROVIDERS.has(provider ?? "") && values.volcConsoleCookie?.trim()) {
    target.volcConsoleCookie = values.volcConsoleCookie.trim();
  } else if (XIAOMI_MIMO_PROVIDERS.has(provider ?? "") && values.xiaomiMimoConsoleCookie?.trim()) {
    // Optional access: callers may pass a partial form object without the newer
    // fields (same contract as the qwen branch above).
    target.xiaomiMimoConsoleCookie = values.xiaomiMimoConsoleCookie.trim();
  }
}