Skip to content

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

3.5 KB
Raw Blame
import { useEffect, useId, useState } from "react";
import { KeyRound } from "lucide-react";

import { liveDisclosure, liveKeyCopy, liveKeyFormKey } from "@/lib/call-mode";
import { dismissLiveNotice } from "@/lib/live-call-media";
import { t } from "@/lib/i18n";
import { useStore } from "@/state/store";
import { openKeySettings } from "./keys/key-focus";
import { LiveKeySetup } from "./LiveKeySetup";
import { LiveProviderSettings } from "./LiveProviderSettings";

export function LiveKeyNeeded({ override }: { override?: "openai" | "fish" } = {}) {
  const { state, dispatch } = useStore();
  const titleId = useId();
  if (override) {
    const copy = liveKeyCopy(override);
    return <section aria-labelledby={titleId} className="flex flex-col gap-2.5 rounded-xl border border-hairline bg-panel p-4 text-left text-[13px]">
      <h2 id={titleId} className="font-medium text-ink">{t(copy.needs)}</h2>
      <button type="button" className="ui-button ui-button-primary self-start" onClick={() => { dismissLiveNotice(); dispatch(openKeySettings(override === "fish" ? "fishAudio" : "openai")); }}>{t("call.live.openApiKeys")}</button>
    </section>;
  }
  const live = state.config?.live;
  const codex = live?.provider === "codex";
  const azure = live?.provider === "azure";
  const custom = live?.provider === "custom";
  const fish = live?.provider === "fish";
  const gemini = live?.provider === "gemini";
  const elevenlabs = live?.provider === "elevenlabs";
  const copy = liveKeyCopy(live?.provider);
  // A key is tied to the endpoint it is saved for, so the endpoint comes first.
  const keyForm = (Boolean((azure && live?.azureEndpoint) || (custom && live?.customEndpoint)) && live?.keySource !== "live") || (fish && live?.keySource !== "fishAudio") || (gemini && live?.keySource !== "gemini")
    || (elevenlabs && live?.keySource !== "elevenlabs");
  return <section aria-labelledby={titleId} className="flex flex-col gap-2.5 rounded-xl border border-hairline bg-panel p-4 text-left text-[13px]">
    <div className="flex items-start gap-2.5">
      <KeyRound size={16} className="mt-0.5 shrink-0 text-ink-secondary" aria-hidden="true" />
      <h2 id={titleId} className="text-[14px] font-medium leading-[1.4] text-ink">{t(copy.needs)}</h2>
    </div>
    <ProviderAndKey formKey={liveKeyFormKey(live)} keyForm={keyForm} />
    {(codex || azure || custom || fish || gemini || elevenlabs) && <DismissWhenReady ready={live?.configured === true} />}
    {!codex && !azure && !custom && !fish && !gemini && !elevenlabs && <button type="button" className="ui-button ui-button-primary self-start" onClick={() => { dismissLiveNotice(); dispatch(openKeySettings("openai", "liveCalls")); }}>{t("call.live.openApiKeys")}</button>}
    {!codex && <p className="text-[12px] leading-[1.45] text-ink-tertiary">{!keyForm && `${liveDisclosure({ cloudHome: state.config?.cloudHome === true, azure, custom, fish, gemini, elevenlabs })} `}{t(copy.cost)}</p>}
  </section>;
}

function ProviderAndKey({ formKey, keyForm }: { formKey: string; keyForm: boolean }) {
  const [keySaving, setKeySaving] = useState(false);
  const [providerSaving, setProviderSaving] = useState(false);
  return <>
    <LiveProviderSettings locked={keySaving} onSaving={setProviderSaving} />
    {keyForm && <LiveKeySetup key={formKey} compact locked={providerSaving} onSaving={setKeySaving} onSaved={() => {}} />}
  </>;
}

function DismissWhenReady({ ready }: { ready: boolean }) {
  useEffect(() => {
    if (ready) dismissLiveNotice();
  }, [ready]);
  return null;
}