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.9 KB
Raw Blame
import type { EmbeddingProvider } from "@omniroute/open-sse/config/embeddingRegistry.ts";
import {
  parseAndValidateNonMetadataUrl,
  parseAndValidatePublicUrl,
} from "@/shared/network/outboundUrlGuard";
import { getProviderValidationGuard } from "@/shared/network/outboundUrlGuardPolicy";

type CustomEmbeddingSettings = {
  customBaseUrl?: string | null;
  customModelId?: string | null;
};

export type ResolvedMemoryCustomEmbeddingProvider = {
  provider: EmbeddingProvider;
  model: string;
  identity: string;
};

export class MemoryCustomEmbeddingConfigError extends Error {
  constructor() {
    super("Custom embedding endpoint is invalid or blocked");
    this.name = "MemoryCustomEmbeddingConfigError";
  }
}

function validateEndpoint(rawBaseUrl: string): URL {
  const guard = getProviderValidationGuard();
  if (guard === "public-only") return parseAndValidatePublicUrl(rawBaseUrl);
  return parseAndValidateNonMetadataUrl(rawBaseUrl);
}

function toEmbeddingsUrl(url: URL): string {
  if (url.search || url.hash) throw new MemoryCustomEmbeddingConfigError();
  const normalized = url.toString().replace(/\/+$/, "");
  return normalized.endsWith("/embeddings") ? normalized : `${normalized}/embeddings`;
}

export function resolveMemoryCustomEmbeddingProvider(
  settings: CustomEmbeddingSettings
): ResolvedMemoryCustomEmbeddingProvider | null {
  const rawBaseUrl = settings.customBaseUrl?.trim() ?? "";
  const model = settings.customModelId?.trim() ?? "";
  if (!rawBaseUrl && !model) return null;
  if (!rawBaseUrl || !model) throw new MemoryCustomEmbeddingConfigError();

  try {
    const baseUrl = toEmbeddingsUrl(validateEndpoint(rawBaseUrl));
    return {
      provider: {
        id: "memory-custom",
        baseUrl,
        authType: "none",
        authHeader: "none",
        models: [],
      },
      model,
      identity: `${baseUrl}|${model}`,
    };
  } catch {
    throw new MemoryCustomEmbeddingConfigError();
  }
}