Skip to content

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

5.0 KB
Raw Blame
import type { IncomingMessage, ServerResponse } from "node:http";
import { join } from "node:path";
import type { PublishedPageCard } from "../../shared/published-page.ts";
import type { ChatApps } from "../chat-apps.ts";
import { createPagesInternalRoutes, createPublishedPageRoutes } from "../routes/published-pages.ts";
import type { Store } from "../store.ts";
import { applyIncludedPagesMessage, pagesAccess, pagesNotOffered } from "./access.ts";
import { createPagesClient } from "./client.ts";
import { PublishedPageFiles } from "./owner-files.ts";
import { createOwnerPageLinks } from "./owner-links.ts";
import { createPagePublisher } from "./publisher.ts";
import { PagesStore } from "./store.ts";

/** How often live published pages' folders are checked for changes. */
const LIVE_EVERY_MS = 15_000;

/** Pages bots publish to a link: the bot tools' internal routes, the card's Unpublish, the desktop's sign-in message, and
 * the chat's page cards, which show a published page and its link. */
export function createPublishedPages(options: {
  dataDir: string;
  /** This server's Cloud home configuration, or null on a desktop or any other server. */
  cloudHome: { adminOrigin: string; machineId: string; bootstrapSecret: string } | null;
  /** True for `openmausbot serve`: no desktop app hands over a MausBot sign-in and there is no Cloud home. */
  selfHosted?: boolean;
  /** The address this server answers at, which an owner-only page's link names; a path alone when omitted. */
  pageOrigin?: () => string | null;
  /** True while a paired session is still signed in: an owner-only page's open copy stops answering when it ends. */
  sessionLive?: (sessionId: string) => boolean;
  patch: (threadId: string, messageId: string, text: string, card: PublishedPageCard) => void;
  /** Live pages stop when their bot can no longer read the folder, and wait while the workspace is backed up or moved. */
  liveRoots?(botId: string, threadId: string): readonly string[] | null;
  paused?(): boolean;
  fetch?: typeof fetch;
  apps?: {
    chat: Pick<ChatApps, "publishablePage" | "showPublishedPage">;
    store: Pick<Store, "botByThread" | "groupByThread" | "messagesFor" | "patchMessage">;
  };
}) {
  const store = new PagesStore(options.dataDir);
  const files = new PublishedPageFiles(join(options.dataDir, "owner-pages"));
  const links = createOwnerPageLinks({ files, ownerPage: (pageId) => store.get(pageId)?.visibility === "owner", ...(options.pageOrigin ? { pageOrigin: options.pageOrigin } : {}) });
  const client = createPagesClient({ access: () => pagesAccess(options.cloudHome), notOffered: () => pagesNotOffered(options.cloudHome),
    selfHosted: options.selfHosted === true,
    ...(options.fetch ? { fetch: options.fetch } : {}) });
  const apps = options.apps;
  const pageRow = (threadId: string, messageId: string) => apps?.store.messagesFor(threadId).find((message) => message.id === messageId && message.kind === "app");
  // A page card keeps its own text, the page's: only the card's link changes.
  const patch = (threadId: string, messageId: string, text: string, card: PublishedPageCard) => {
    if (pageRow(threadId, messageId)) apps!.store.patchMessage(threadId, messageId, { publishedPage: card });
    else options.patch(threadId, messageId, text, card);
  };
  const publisher = createPagePublisher({ client, store, files, patch, liveRoots: options.liveRoots, paused: options.paused, pageOrigin: options.pageOrigin });
  // Live pages whose folders changed are published again; each at most once a minute (the publisher's own limit).
  const liveTimer = setInterval(() => { void publisher.refreshLive(); }, LIVE_EVERY_MS);
  liveTimer.unref?.();
  const appPages = apps ? {
    botOf: (threadId: string) => (apps.store.groupByThread(threadId) ? null : apps.store.botByThread(threadId) ?? null),
    page: (threadId: string, messageId: string, rev?: number) => apps.chat.publishablePage(threadId, messageId, rev),
    published: (threadId: string, messageId: string) => {
      const card = pageRow(threadId, messageId)?.publishedPage;
      return card?.state === "live" && card.pageId ? card.pageId : null;
    },
    show: (threadId: string, messageId: string, publishedPage: PublishedPageCard) =>
      (pageRow(threadId, messageId) && apps.store.patchMessage(threadId, messageId, { publishedPage }) ? messageId : null),
  } : undefined;
  return {
    store, files, publisher, internal: createPagesInternalRoutes(publisher),
    routes: createPublishedPageRoutes(publisher, appPages, {
      servePage: links.servePage, openUrl: links.openUrl, ownerOnly: (pageId) => store.get(pageId)?.visibility === "owner", ...(options.sessionLive ? { sessionLive: options.sessionLive } : {}) }),
    serveFile: (req: IncomingMessage, res: ServerResponse, path: string, method: string) => links.serve(req, res, path, method),
    receive: applyIncludedPagesMessage,
    /** publish_page's card as the page itself, inline; null when it can't be shown so (the caller posts the link). */
    show: (args: Parameters<ChatApps["showPublishedPage"]>[0]) => apps?.chat.showPublishedPage(args) ?? null,
  };
}