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

772 B
Raw Blame
import type { NextRequest } from "next/server";

/**
 * Derive the base URL for A2A agent card endpoints.
 * Prefers OMNIROUTE_BASE_URL env var for admin override; falls back to the
 * request's dynamic origin so the gateway works behind any hostname without
 * hardcoded localhost:20128 (S2 security fix).
 */
export function getBaseUrl(request?: NextRequest | null): string {
  if (process.env.OMNIROUTE_BASE_URL) return process.env.OMNIROUTE_BASE_URL;
  // Direct route-handler invocation (unit tests, programmatic calls) passes no
  // Request — fall back to the default local gateway origin instead of crashing.
  const defaultPort = process.env.PORT || process.env.DASHBOARD_PORT || 20128;
  return request?.nextUrl?.origin ?? `http://localhost:${defaultPort}`;
}