Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

3.8 KB
Raw Blame
import { useEffect, useRef, useState } from "react";

import { api, useStore, type ConfigStatus } from "@/state/store";
import { liveDisclosure, liveKeyCopy } from "@/lib/call-mode";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";

/**
 * The server binds a key to the provider set when the key arrives, so the provider select is held while a save is
 * pending (`onSaving`), and no key is sent while a provider change is (`locked`).
 */
export function LiveKeySetup({ onSaved, onSaving, locked = false, compact = false }: {
  onSaved: () => void;
  onSaving?: (saving: boolean) => void;
  locked?: boolean;
  compact?: boolean;
}) {
  const { state, dispatch } = useStore();
  const provider = state.config?.live?.provider;
  const azure = provider === "azure";
  const custom = provider === "custom";
  const fish = provider === "fish";
  const gemini = provider === "gemini";
  const elevenlabs = provider === "elevenlabs";
  const copy = liveKeyCopy(provider);
  const [key, setKey] = useState("");
  const [saving, setSaving] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const mounted = useRef(true);
  useEffect(() => {
    mounted.current = true;
    return () => { mounted.current = false; };
  }, []);
  const save = async () => {
    const value = key.trim();
    if (!value || saving || locked) return;
    setSaving(true);
    onSaving?.(true);
    setError(null);
    try {
      const status: ConfigStatus = window.ogb?.setCredential
        ? await window.ogb.setCredential(fish ? "fishAudioKey" : gemini ? "geminiTtsKey" : elevenlabs ? "ttsKey" : "openaiLiveKey", value)
        : await api("/api/config", { method: "PUT", body: JSON.stringify(fish ? { tts: { fishKey: value } } : gemini ? { tts: { geminiKey: value } } : elevenlabs ? { tts: { key: value } } : { live: { key: value } }) });
      dispatch({ type: "configStatus", config: status });
      // The save may finish after the prompt was dismissed; never turn it into a call the user already cancelled.
      if (!mounted.current) return;
      setKey("");
      onSaved();
    } catch (saveError) {
      if (mounted.current) setError(saveError instanceof Error ? saveError.message : String(saveError));
    } finally {
      if (mounted.current) setSaving(false);
      onSaving?.(false);
    }
  };
  return (
    <form
      className={cn(
        "flex flex-col gap-2.5 rounded-xl border border-hairline bg-panel p-4 text-left",
        compact ? "w-full" : "w-[420px] max-w-[90vw]",
      )}
      onSubmit={(event) => {
        event.preventDefault();
        void save();
      }}
    >
      <div className="text-[14px] font-medium text-ink">{t(copy.title)}</div>
      <div className="text-[13px] leading-[1.45] text-ink-secondary">{t(copy.explain)}</div>
      <div className="text-[13px] leading-[1.45] text-ink-secondary">{liveDisclosure({ cloudHome: state.config?.cloudHome === true, azure, custom, fish, gemini, elevenlabs })}</div>
      <input
        type="password"
        autoComplete="off"
        spellCheck={false}
        value={key}
        onChange={(event) => setKey(event.target.value)}
        placeholder={azure || custom || fish || gemini ? "…" : elevenlabs ? "sk_…" : "sk-…"}
        aria-label={t(copy.field)}
        className="rounded-lg border border-hairline bg-inset px-3 py-2 text-[13px] text-ink outline-none focus:border-accent/60"
      />
      {error && <div className="text-[12px] text-danger">{error}</div>}
      <button
        type="submit"
        disabled={!key.trim() || saving || locked}
        className="self-start rounded-lg bg-accent px-3 py-1.5 text-[13px] font-medium text-white hover:brightness-110 disabled:opacity-50"
      >
        {saving ? t("call.live.keySaving") : compact ? t("common.save") : t("call.live.keySaveAndCall")}
      </button>
    </form>
  );
}