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.0 KB
Raw Blame
const DEFAULT_TRAE_API_HOST = "https://api-us-east.trae.ai";

/**
 * Origin of a Trae API host, or null when the value is not an https origin under trae.ai.
 * The token refresh posts credentials to this host, so nothing else is accepted.
 */
export function parseTraeApiHost(value: unknown): string | null {
  if (typeof value !== "string" || !value.trim()) return null;
  try {
    const url = new URL(value.trim());
    const hostname = url.hostname.toLowerCase();
    const isTraeHost = hostname === "trae.ai" || hostname.endsWith(".trae.ai");
    if (url.protocol === "https:" && isTraeHost && !url.username && !url.password && !url.port) {
      return url.origin;
    }
  } catch {
    // Not a URL.
  }
  return null;
}

/**
 * Base URL for Trae's API, taken from a stored `host` value. Anything that is not a
 * trae.ai https origin falls back to the default region so the token refresh can never be
 * pointed at an arbitrary server.
 */
export function resolveTraeApiHost(value: unknown): string {
  return parseTraeApiHost(value) ?? DEFAULT_TRAE_API_HOST;
}