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.2 KB
Raw Blame
"use client";

import { useState } from "react";
import { useTranslations } from "next-intl";
import type { MemorySettingsExtended } from "@/shared/schemas/memory";

type Props = {
  settings: MemorySettingsExtended;
  onSave: (updates: Partial<MemorySettingsExtended>) => Promise<boolean>;
  saving?: boolean;
};

function normalizeBaseUrl(value: string): string | null {
  const trimmed = value.trim();
  if (!trimmed) return null;
  try {
    const url = new URL(trimmed);
    if (
      (url.protocol !== "http:" && url.protocol !== "https:") ||
      url.username ||
      url.password ||
      url.search ||
      url.hash
    ) {
      return null;
    }
    return trimmed.replace(/\/+$/, "");
  } catch {
    return null;
  }
}

export default function CustomEmbeddingEndpointFields({ settings, onSave, saving }: Props) {
  const t = useTranslations("memory");
  const [baseUrl, setBaseUrl] = useState(settings.customBaseUrl ?? "");
  const [modelId, setModelId] = useState(settings.customModelId ?? "");
  const [error, setError] = useState<string | null>(null);

  const save = async () => {
    const normalizedBaseUrl = normalizeBaseUrl(baseUrl);
    const normalizedModelId = modelId.trim() || null;
    if ((baseUrl.trim() || normalizedModelId) && (!normalizedBaseUrl || !normalizedModelId)) {
      setError(t("embedding.customEndpointInvalid"));
      return;
    }
    setError(null);
    const saved = await onSave({
      customBaseUrl: normalizedBaseUrl,
      customModelId: normalizedModelId,
    });
    if (!saved) {
      setError(t("embedding.customEndpointSaveFailed"));
      return;
    }
    setBaseUrl(normalizedBaseUrl ?? "");
    setModelId(normalizedModelId ?? "");
  };

  return (
    <div className="mt-4 pt-4 border-t border-border/60 space-y-3">
      <div>
        <label className="block text-sm font-medium text-text-main mb-1">
          {t("embedding.customBaseUrlLabel")}
        </label>
        <input
          value={baseUrl}
          onChange={(event) => setBaseUrl(event.target.value)}
          placeholder="http://localhost:8000/v1"
          disabled={saving}
          data-testid="embedding-custom-base-url"
          className="w-full px-3 py-2 rounded-lg bg-background border border-border text-sm"
        />
      </div>
      <div>
        <label className="block text-sm font-medium text-text-main mb-1">
          {t("embedding.customModelIdLabel")}
        </label>
        <input
          value={modelId}
          onChange={(event) => setModelId(event.target.value)}
          placeholder="my-embedding-model"
          disabled={saving}
          data-testid="embedding-custom-model-id"
          className="w-full px-3 py-2 rounded-lg bg-background border border-border text-sm"
        />
      </div>
      <p className="text-xs text-text-muted">{t("embedding.customEndpointHelp")}</p>
      {error && (
        <p role="alert" className="text-xs text-red-400">
          {error}
        </p>
      )}
      <button
        type="button"
        onClick={() => void save()}
        disabled={saving}
        data-testid="embedding-custom-save"
        className="px-3 py-2 rounded-lg bg-violet-500 text-white text-sm disabled:opacity-50"
      >
        {t("embedding.customEndpointSave")}
      </button>
    </div>
  );
}