Skip to content

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

6.6 KB
Raw Blame
// Another bot's long request reads as one line in the chat. It opens in place, and its back-and-forth with this bot opens beside the chat.
import { createContext, use, useMemo, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { ChevronRight, MessagesSquare, X } from "lucide-react";
import { BotAvatar } from "./Avatar";
import { useModalDialog } from "@/hooks/use-modal-dialog";
import { formatTime } from "@/lib/clock-time";
import { normalizeState } from "@/lib/mascot";
import { peerExchange, type ExchangeLine, type PeerLine } from "@/lib/peer-message";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import type { Bot, Message } from "@/state/store";

/** The thread's messages, so an opened request can show its whole exchange. */
export const PeerMessagesContext = createContext<readonly Message[]>([]);

const authorOf = (bots: readonly Bot[], peer: Pick<PeerLine, "botId" | "name">) =>
  bots.find((b) => b.id === peer.botId) ?? bots.find((b) => b.name === peer.name);

function PeerAvatar({ bot, name }: { bot?: Bot; name: string }) {
  return (
    <BotAvatar
      bot={bot ?? { name, color: "blue" }}
      state={normalizeState(bot?.mascotExpression) ?? "happy"}
      size={16}
      motion="none"
      motionKey={0}
      animated={false}
    />
  );
}

/** How the other bot reached this one, for its label. */
export const PEER_HOW = {
  delegate_bot: "chat.peer.handedTask",
  start_thread: "chat.peer.openedThread",
  coordinate_bots: "chat.peer.requested",
  ask_bot: "chat.peer.asked",
} as const satisfies Record<PeerLine["delivery"], string>;

/** A folded message: one line, opened by a click, with `label` before the preview. */
export function PeerCompactRow({ preview, onOpen, label, testId = "peer-compact" }: { preview: string; onOpen: () => void; label?: string; testId?: string }) {
  return (
    <button
      type="button"
      onClick={onOpen}
      title={t("chat.showFull")}
      data-testid={testId}
      className="flex w-fit max-w-[min(var(--chat-bubble-max),var(--chat-bubble-pct))] items-center gap-1.5 rounded-xl bg-card px-3 py-1.5 text-left text-[14px] text-ink-secondary hover:text-ink"
    >
      <ChevronRight size={14} className="shrink-0" />
      {label && <span className="shrink-0 font-medium">{label}</span>}
      <span className={label ? "min-w-0 truncate" : "truncate"}>{preview}</span>
    </button>
  );
}

interface PeerContext {
  peer: PeerLine;
  bots: readonly Bot[];
  botId: string;
  botName: string;
}

/** Under an opened request: fold it again, or see the whole exchange beside the chat. */
export function PeerOpenedActions({ onFold, ...context }: PeerContext & { onFold: () => void }) {
  const [viewing, setViewing] = useState(false);
  return (
    <div className="mt-1 flex items-center gap-3 pl-0.5 text-[13px] text-ink-secondary">
      <button type="button" onClick={onFold} className="hover:text-ink">{t("chat.showLess")}</button>
      <button type="button" onClick={() => setViewing(true)} className="flex items-center gap-1 hover:text-ink" data-testid="peer-view-exchange">
        <MessagesSquare size={13} />
        {t("chat.peer.viewExchange")}
      </button>
      {viewing && <PeerExchangePanel {...context} onClose={() => setViewing(false)} />}
    </div>
  );
}

function PeerExchangePanel({ peer, bots, botId, botName, onClose }: PeerContext & { onClose: () => void }) {
  const messages = use(PeerMessagesContext);
  const lines = useMemo(() => peerExchange(messages, botId, peer), [messages, botId, peer.botId, peer.name]);
  return createPortal(
    <PeerExchangeView lines={lines} peerName={peer.name} peerBot={authorOf(bots, peer)} botName={botName} self={bots.find((b) => b.id === botId)} onClose={onClose} />,
    document.body,
  );
}

export function PeerExchangeView({ lines, peerName, peerBot, botName, self, onClose }: {
  lines: readonly ExchangeLine[];
  peerName: string;
  peerBot?: Bot;
  botName: string;
  self?: Bot;
  onClose: () => void;
}) {
  const title = t("chat.peer.exchangeTitle", { peer: peerName, bot: botName });
  // Keeps Tab inside the panel, closes on Escape and gives focus back to "View exchange".
  const dialogRef = useRef<HTMLElement>(null);
  useModalDialog(dialogRef, onClose);
  return (
    <div className="fixed inset-0 z-50 flex justify-end bg-black/40" onMouseDown={(event) => event.target === event.currentTarget && onClose()}>
      <aside ref={dialogRef} tabIndex={-1} role="dialog" aria-modal="true" aria-label={title} data-testid="peer-exchange" className="flex h-full w-[min(440px,100vw)] flex-col border-l border-hairline/50 bg-panel shadow-2xl shadow-black/50">
        <header className="flex items-center justify-between gap-3 border-b border-hairline/40 px-4 py-3">
          <h2 className="truncate text-[14px] font-medium text-ink">{title}</h2>
          <button type="button" onClick={onClose} title={t("common.close")} aria-label={t("common.close")} className="rounded p-1 text-ink-secondary hover:bg-raised hover:text-ink">
            <X size={16} />
          </button>
        </header>
        <ol className="flex-1 space-y-3 overflow-y-auto px-4 py-3">
          {lines.map((line) => {
            const name = line.from === "peer" ? peerName : botName;
            return (
              <li key={line.id} className="flex flex-col gap-1" data-from={line.from}>
                <div className="flex items-center gap-1.5 text-[11px] text-ink-tertiary">
                  <PeerAvatar bot={line.from === "peer" ? peerBot : self} name={name} />
                  <span className="font-medium text-ink-secondary">{name}</span>
                  <span>· {formatTime(line.at)}</span>
                </div>
                <div className={cn("whitespace-pre-wrap [overflow-wrap:anywhere] rounded-xl px-3 py-2 text-[14px] leading-relaxed text-ink", line.from === "peer" ? "bg-card" : "bg-inset/40")}>
                  {line.text}
                </div>
              </li>
            );
          })}
        </ol>
      </aside>
    </div>
  );
}

export function PeerLabel({ peer, bots }: { peer: PeerLine; bots: readonly Bot[] }) {
  const author = authorOf(bots, peer);
  const how = t(PEER_HOW[peer.delivery]);
  return (
    <div className="mb-1 flex items-center gap-1.5 pl-0.5" data-testid="peer-label">
      <BotAvatar
        bot={author ?? { name: peer.name, color: "blue" }}
        state={normalizeState(author?.mascotExpression) ?? "happy"}
        size={20}
        motion="none"
        motionKey={0}
        animated={false}
      />
      <span className={cn("text-[13px] font-semibold", author ? "bot-identity" : "text-ink-secondary")} data-bot-color={author?.color}>{peer.name}</span>
      <span className="text-[12px] text-ink-tertiary">· {how}</span>
    </div>
  );
}