Skip to content

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

9.8 KB
Raw Blame
import { useEffect, useMemo, useState } from "react";
import { ArrowUp, Check, Plus } from "lucide-react";
import { BotAvatar, MausAvatar } from "@/components/Avatar";
import { engineReady } from "@/components/EngineLibrary";
import { usePrefersReducedMotion } from "@/components/mascot-clock";
import { brand, productName } from "@/lib/brand";
import { cn } from "@/lib/cn";
import { ConnectAiPlaceContext, useConnectAiSteps } from "@/lib/connect-ai-events";
import { DEFAULT_BOT_NAME, myDefaultBot, useViewerEmail } from "@/lib/default-bot";
import { activeLocale, t } from "@/lib/i18n";
import { aiChoices, aiWorks, firstAis, type FirstAiKind } from "@/lib/onboarding";
import { useStore, type InstanceInfo } from "@/state/store";
import { freeFallback } from "../../../shared/ai-ranking";
import { ConnectAiDialog } from "./ConnectAi";
import { PILL, SignInPill, useSignInPill } from "./SignInPill";
import { aiRuns, useWelcomeAi } from "./welcome-ai";

const MASCOT_SIZE = 72;
const PLACEHOLDER_MS = 700;
const GRAPHEME_MS = 35;

function graphemes(text: string): string[] {
  const Segmenter = (Intl as { Segmenter?: typeof Intl.Segmenter }).Segmenter;
  return Segmenter ? Array.from(new Segmenter(activeLocale(), { granularity: "grapheme" }).segment(text), (part) => part.segment) : Array.from(text);
}

/** The demo's one sentence, typed once after the placeholder has been read; reduced motion shows it whole. */
function useDemoSentence(reduced: boolean): { text: string; typing: boolean; waiting: boolean } {
  const parts = useMemo(() => graphemes(t("onboarding.meet.demo")), []);
  const [typed, setTyped] = useState(reduced ? parts.length : -1);
  useEffect(() => {
    if (reduced || typed >= parts.length) return;
    const timer = setTimeout(() => setTyped((now) => now + 1), typed < 0 ? PLACEHOLDER_MS : GRAPHEME_MS);
    return () => clearTimeout(timer);
  }, [reduced, typed, parts.length]);
  const shown = reduced ? parts.length : typed;
  return { text: parts.slice(0, Math.max(0, shown)).join(""), typing: shown >= 0 && shown < parts.length, waiting: shown < 0 };
}

function DemoComposer({ reduced, onTyping }: { reduced: boolean; onTyping: (typing: boolean) => void }) {
  const { text, typing, waiting } = useDemoSentence(reduced);
  useEffect(() => onTyping(typing), [typing, onTyping]);
  return (
    <div aria-hidden="true" data-demo-composer=""
      className="absolute left-1/2 top-[calc(50%-14px)] flex min-h-[76px] w-[348px] max-w-[calc(100%-32px)] -translate-x-1/2 select-none flex-col gap-1 rounded-[22px] border-[0.5px] border-ink/10 bg-composer px-2 pb-1.5 pt-3 text-left [:root[data-skin=daylight]_&]:shadow-[0_2px_8px_-1px_rgb(0_0_0/0.05),0_1px_2px_rgb(0_0_0/0.03)]">
      <div className="mx-2 min-w-0 break-words text-[14px] leading-5">
        {waiting ? <span className="text-ink-tertiary">{t("onboarding.meet.placeholder", { app: productName() })}</span> : <span className="text-ink">{text}</span>}
        {!reduced && (waiting || typing) && <span className="animate-meet-caret ml-px inline-block h-[15px] w-[1.5px] translate-y-[2px] bg-ink align-[-2px]" />}
      </div>
      <div className="flex items-center justify-between">
        <span className="flex size-7 items-center justify-center rounded-full bg-[color-mix(in_srgb,var(--color-ink)_4%,var(--color-composer))] text-ink-tertiary"><Plus size={14} /></span>
        <span className="flex size-7 items-center justify-center rounded-full bg-ink text-app"><ArrowUp size={14} strokeWidth={2} /></span>
      </div>
    </div>
  );
}

/** The first run's first screen: the app's name, its mascot and a composer typing one sentence, then one pill. With
 * no AI working yet the pills are the sign-ins (Claude, ChatGPT and OpenCode), and Next comes once the AI has answered
 * its test message. */
export function MeetScreen({ hosted, onNext }: { hosted: boolean; onNext: () => void }) {
  const { state } = useStore();
  const reduced = usePrefersReducedMotion();
  const maus = myDefaultBot(state.bots, useViewerEmail());
  // An AI counts once it answered a test message (or cannot be sent one); OpenCode's free model only once picked here.
  const [chosen, setChosen] = useState<string | null>(null);
  const tested = useWelcomeAi(state.instances, !hosted, chosen, "meet");
  const ready = aiWorks(tested);
  const known = state.instances.length > 0;
  const [asking, setAsking] = useState(false);
  const step = useConnectAiSteps("meet");
  useEffect(() => {
    if (hosted || !known || tested?.state === "testing") return;
    step("opened", { aiReady: ready, ...(tested ? { driverKind: tested.instance.driverKind } : {}), ...(tested?.state === "failed" ? { reason: tested.reason ?? "other" } : {}) });
    if (!ready) setAsking(true);
  }, [hosted, known, ready, tested, step]);
  const works = (instance: InstanceInfo) => aiRuns(instance) && !freeFallback(instance);
  // Picked and installed, still listing its models: its pill waits until its test message.
  const settling = (instance: InstanceInfo | undefined) => Boolean(instance && instance.instanceId === chosen && engineReady(instance) && !aiRuns(instance));
  const first = firstAis(state.instances, works).filter(({ instance }) => !works(instance) && (instance.install || engineReady(instance)));
  const { recommended } = aiChoices(state.instances.filter((instance) => instance.install && !engineReady(instance)));
  const choice = (kind: FirstAiKind) => first.find((entry) => entry.kind === kind)?.instance;
  const lead = first.length ? undefined : recommended;
  const pills = {
    claude: useSignInPill(choice("claude") ?? lead, "meet"),
    chatgpt: useSignInPill(choice("chatgpt"), "meet"),
    opencode: useSignInPill(choice("opencode"), "meet"),
  };
  const shown = first.length ? first : [{ kind: "claude" as const, instance: lead }];
  const [dialog, setDialog] = useState(false);
  const [typing, setTyping] = useState(false);

  const logo = brand().logo;
  const face = typing ? "listening" : "idle";
  const testing = tested?.state === "testing" ? tested.instance.instanceId : null;
  const waitsFor = shown.some(({ instance }) => instance?.instanceId === testing) ? testing : shown[0]?.instance?.instanceId;
  const failure = shown.map(({ kind }) => pills[kind].error).find(Boolean)
    ?? (tested?.state === "failed" ? tested.error ?? t("onboarding.engines.noAnswer", { name: tested.instance.displayName }) : null);
  const signingIn = asking && !ready;
  return (
    <ConnectAiPlaceContext.Provider value="meet">
    <div className="relative h-full min-h-[600px] w-full text-center">
      <h1 id="welcome-title" className="absolute left-1/2 top-[calc(50%-264px)] w-max max-w-[min(560px,calc(100%-32px))] -translate-x-1/2 text-[28px] font-normal leading-[34px] tracking-[-0.02em] text-ink">
        {t("onboarding.meet.title", { app: productName() })}
      </h1>
      <span className="absolute left-1/2 top-[calc(50%-75px)] flex -translate-x-1/2 -translate-y-1/2" style={{ width: MASCOT_SIZE, height: MASCOT_SIZE }}>
        {logo ? (
          <img src={logo} alt="" width={MASCOT_SIZE} height={MASCOT_SIZE} className="size-full object-contain" />
        ) : maus ? (
          <BotAvatar bot={maus} size={MASCOT_SIZE} state={face} animated={!reduced} />
        ) : (
          <MausAvatar color="chrome" size={MASCOT_SIZE} state={face} animated={!reduced} label={DEFAULT_BOT_NAME} />
        )}
      </span>
      <DemoComposer reduced={reduced} onTyping={setTyping} />
      {hosted && (
        <p className="absolute left-1/2 top-[calc(50%+86px)] w-[420px] max-w-[calc(100%-32px)] -translate-x-1/2 text-[13px] leading-[18px] text-ink-secondary">
          {t("onboarding.hosted.intro")}
        </p>
      )}
      <div className={cn("absolute left-1/2 flex w-[320px] max-w-[calc(100%-32px)] -translate-x-1/2 flex-col items-center",
        signingIn ? "top-[min(calc(50%+172px),calc(100%-180px))]" : "top-[calc(50%+172px)]")}>
        {asking && ready && tested && (
          <p role="status" data-ai-connected="" className="absolute -top-8 flex items-center gap-1.5 whitespace-nowrap text-[13px] leading-[18px] text-success">
            <Check size={14} strokeWidth={2.5} aria-hidden="true" />{t("onboarding.meet.connected", { name: tested.instance.displayName })}
          </p>
        )}
        {signingIn ? (
          <div data-first-ais="" className="flex flex-col items-center gap-2">
            {shown.map(({ kind, instance }, index) => (
              <SignInPill key={kind} pill={pills[kind]} instance={instance} quiet={index > 0}
                name={kind === "chatgpt" ? t("cloudSignIn.codex") : undefined}
                setUp={kind === "chatgpt" && instance?.snapshot.state !== "available"}
                waiting={(testing !== null && instance?.instanceId === waitsFor) || settling(instance)}
                onDialog={() => setDialog(true)} onPick={instance && (() => setChosen(instance.instanceId))} />
            ))}
          </div>
        ) : (
          <button type="button" data-primary="" onClick={onNext} className={cn(PILL, asking && "animate-fade-in [animation-duration:150ms]")}>
            {t("onboarding.meet.next")}
          </button>
        )}
        {signingIn && (
          <p className="mt-3 text-[13px] leading-[18px] text-ink-secondary">
            <button type="button" onClick={() => setDialog(true)} className="rounded transition-colors hover:text-ink">{t("onboarding.meet.otherWays")}</button>
            <span aria-hidden="true"> · </span>
            <button type="button" data-welcome-later="" onClick={() => { step("later"); onNext(); }} className="rounded transition-colors hover:text-ink">{t("onboarding.meet.setupLater")}</button>
          </p>
        )}
        {signingIn && failure && <p role="alert" className="mt-2 text-[13px] leading-[18px] text-danger">{failure}</p>}
      </div>
      {dialog && <ConnectAiDialog hosted={hosted} tested={tested} onChoose={setChosen} onClose={() => setDialog(false)} />}
    </div>
    </ConnectAiPlaceContext.Provider>
  );
}