Skip to content

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

6.5 KB
Raw Blame
import { useCallback, useEffect, useRef, type MouseEvent } from "react";
import { isNetlifySiteUrl } from "../../shared/published-apps";
import { PAGE_ID } from "../../shared/published-page";
import type { Bot } from "@/state/store";
import { useOptionalStoreRef } from "@/state/store-ref";
import { openExternalLink } from "./app-links";
import { openAppPreview, type AppPreview } from "./app-preview";
import { browserAvailable, builtInBrowserEnabled, type FeatureFlagConfig } from "./feature-flags";
import { knownPublishedApp, showable } from "./published-apps";

export const PAGES_DOMAIN = "mausbot.page";

/** A link to a page a bot published: https://<page id>.mausbot.page/… Never an app, chat or prompt link. */
export function isPublishedPageUrl(value: string | null | undefined): value is string {
  if (typeof value !== "string") return false;
  let url: URL;
  try { url = new URL(value); } catch { return false; }
  if (url.protocol !== "https:" || url.username || url.password || url.port) return false;
  const host = url.hostname.toLowerCase(), suffix = `.${PAGES_DOMAIN}`;
  return host.endsWith(suffix) && PAGE_ID.test(host.slice(0, -suffix.length));
}

export interface PageOpenFacts {
  selectedId: string | null;
  bots: ReadonlyArray<Pick<Bot, "id" | "browser">>;
  groups: ReadonlyArray<{ id: string }>;
  config: FeatureFlagConfig | null | undefined;
  /** The desktop app's bridge, which only the desktop app's window has. */
  desktop: { remoteClient?: { active?: boolean } } | undefined;
}

/**
 * The bot whose browser, in the panel on the right, shows a published page; null opens it outside the chat instead (the
 * system browser in the desktop app, a new tab on the web). Only the desktop app's window, only a page link, and only a
 * bot chat whose browser is on and installed; never a room, and never the remote client, which has no such panel.
 */
export function pagePanelBot(url: string, facts: PageOpenFacts): string | null {
  if (!facts.desktop || facts.desktop.remoteClient?.active || !isPublishedPageUrl(url)) return null;
  if (facts.groups.some((group) => group.id === facts.selectedId)) return null;
  const bot = facts.bots.find((candidate) => candidate.id === facts.selectedId);
  const engine = facts.config?.browserEngine;
  if (!bot || bot.browser === false || !builtInBrowserEnabled(facts.config) || !browserAvailable(facts.config)) return null;
  return engine?.installing || engine?.installError ? null : bot.id;
}

export interface PanelPage { botId: string; url: string; nonce: number; at: number }
/** A request older than this is dropped, so a panel opened much later never opens a page by surprise. */
const PANEL_PAGE_FRESH_MS = 10_000;
let pending: PanelPage | null = null, nonce = 0;
const takers = new Set<() => void>();

export function requestPanelPage(botId: string, url: string, now = Date.now()): void {
  pending = { botId, url, nonce: ++nonce, at: now };
  for (const take of takers) take();
}

/** Hands `open` each page asked for this bot's panel, once: the panel shows its browser and opens the page in a new tab. */
export function usePanelPage(botId: string, open: (page: PanelPage) => void): void {
  const latest = useRef(open);
  latest.current = open;
  useEffect(() => {
    const take = () => {
      const page = pending;
      if (!page || page.botId !== botId) return;
      pending = null;
      if (Date.now() - page.at <= PANEL_PAGE_FRESH_MS) latest.current(page);
    };
    take();
    takers.add(take);
    return () => { takers.delete(take); };
  }, [botId]);
}

/** A link to something a bot put online: an app in the loaded Published apps list, a Netlify site's own address, or a
 * published page. */
export function publishedLink(href: string | null | undefined): AppPreview | null {
  if (typeof href !== "string") return null;
  const app = knownPublishedApp(href);
  if (app) return { url: href, title: app.title, ...(app.access === "netlify-team" ? { loginOnly: true } : {}) };
  return isNetlifySiteUrl(href) || isPublishedPageUrl(href) ? { url: href, title: new URL(href).host } : null;
}

const SIDE_PANELS = ["toggleComputer", "toggleSettings", "toggleInspector", "toggleActivity"] as const;

type SidePanelDispatch = (action: { type: (typeof SIDE_PANELS)[number]; open: false }) => void;

/** Closes every panel on the chat's right side, so a preview can take that side. */
export function closeSidePanels(dispatch: SidePanelDispatch): void {
  for (const type of SIDE_PANELS) dispatch({ type, open: false });
}

/** Shows a published app beside the open conversation, in place of any other panel on that side. */
export function showAppBesideChat(dispatch: SidePanelDispatch, preview: AppPreview): void {
  closeSidePanels(dispatch);
  openAppPreview(preview);
}

/** Opens a link the way a published page's Open does: a page in the bot's browser panel when the desktop app has one,
 * anything else outside the chat. It never sends a message, starts a turn or opens a chat. With `beside`, a link in a
 * message to something a bot put online opens in the preview beside the chat instead of leaving it. */
export function usePageOpener(options: { beside?: boolean } = {}): (url: string) => void {
  // Outside the app's store (a message drawn on its own) there is no panel: the page opens outside the chat.
  const store = useOptionalStoreRef();
  const latest = useRef(store);
  latest.current = store;
  const beside = options.beside === true;
  return useCallback((url: string) => {
    const app = latest.current, state = app?.state();
    const botId = app && state ? pagePanelBot(url, { selectedId: state.selectedId, bots: state.bots ?? [], groups: state.groups ?? [], config: state.config, desktop: window.ogb }) : null;
    if (app && botId) {
      requestPanelPage(botId, url);
      app.dispatch({ type: "toggleComputer", open: true });
      return;
    }
    const preview = app && beside && showable(url, window.location.origin) ? publishedLink(url) : null;
    if (app && preview) return showAppBesideChat(app.dispatch, preview);
    void openExternalLink(url).catch(() => {});
  }, [beside]);
}

/** A page link's click opens it like the card's Open; a click its hover card already took stays taken. */
export function withPageOpen<T extends { onClick?: (event: MouseEvent<HTMLElement>) => void }>(href: string | undefined, open: (url: string) => void, anchor: T): T {
  if (!publishedLink(href)) return anchor;
  const link = href as string;
  return { ...anchor, onClick: (event: MouseEvent<HTMLElement>) => {
    anchor.onClick?.(event);
    if (event.defaultPrevented) return;
    event.preventDefault();
    open(link);
  } };
}