Skip to content

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

24.1 KB
Raw Blame
// Main's side of the one-time move to My Cloud (one-home.mjs) and of the send it runs (cloud-send-main.mjs): its
// journal, this computer's server's freeze, My Cloud's owner sessions, the person's typing, and coming home when My
// Cloud ends (one-home-again.mjs). The Admin's word decides: a move starts only for an account it lets move, to a Cloud
// that is ready, and a Cloud it holds after it ended brings everything home, as does its word that the Cloud this computer
// moved to is gone while My Cloud is another (`gone`). The person's own
// choice to keep this computer's bots here (Settings → MausBot Cloud, Advanced) is kept beside the journal.
import { createHash } from "node:crypto";
import { createReadStream, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { Readable } from "node:stream";

import { installCloudSend, openOwnerSession } from "./cloud-send-main.mjs";
import { choosable, createBotMove } from "./bot-move-main.mjs";
import { createMoveLease } from "./move-lease.mjs";
import { moveLending as lendingFacts, shownFolder } from "./move-lending.mjs";
import { createMoveSession } from "./move-session.mjs";
import { createMoveStatusReport, moveStatus } from "./move-status.mjs";
import { createHomeAgain } from "./one-home-again.mjs";
import { awayIn, createOneHome, parseJournal, underWay } from "./one-home.mjs";
import { myCloudOf } from "./cloud-list.mjs";
import { teamPlan } from "./cloud-team-plan.mjs";
import { installTeamMove } from "./team-move-main.mjs";
import nativeText from "./native-text.cjs";

const MOVE_LABEL = "Move to My Cloud";
const HOME_LABEL = "Bring My Cloud home";
const KEEP_LABEL = "Bring this computer's bots home";

/** This computer as the Admin knows it, the same across sign-outs and sign-ins: a hash of its own server's identity, so
 * the identity itself never leaves it. */
export function installationOf(environmentId) {
  return createHash("sha256").update(`openmausbot-installation:${environmentId}`).digest("base64url");
}

/** One home's view of the person's Cloud, from the Admin's last verified word; null while nobody is signed in. An ended
 * Cloud's `homecoming` is the computer its things come home to; `signIn`, this app's sign-in. */
/** This computer's server's answer to the move's freeze: frozen, or what still holds the move, by name ("" when it has
 * none) and what it is (server/routes/move-freeze.ts). */
export function freezeAnswer(got) {
  if (got.status === 409 && typeof got.body?.busy === "string") return { busy: got.body.busy, ...(typeof got.body.what === "string" ? { what: got.body.what } : {}) };
  if (!got.ok || got.body?.frozen !== true) throw Object.assign(new Error("OpenMausBot on this computer could not pause."), { status: got.status });
  return { frozen: true };
}

export function oneHomeCloud(state) {
  const account = state?.account?.id;
  if (typeof account !== "string" || !account) return null;
  const connected = state.status === "connected", machine = connected ? state.machine ?? null : null;
  const trialOn = ["active", "ending"].includes(state.trial?.state ?? "");
  const homecoming = machine?.hold?.homecoming ?? null, mine = myCloudOf(state);
  return {
    origin: machine?.origin ?? null,
    ...(mine?.id ? { id: mine.id } : {}),
    ...(mine?.formerOrigins.length ? { formerOrigins: mine.formerOrigins } : {}),
    account,
    machine: machine?.origin ?? null,
    ready: connected && machine?.status === "ready" && Boolean(machine.origin),
    moves: connected && machine?.moves === true,
    ended: connected && Boolean(machine?.hold),
    paid: connected && state.entitlement?.status === "active" && !trialOn && !teamPlan(state),
    ...(homecoming ? { homecoming: { installationId: homecoming.installationId, name: homecoming.name } } : {}),
    ...(connected && typeof state.deviceId === "string" && state.deviceId ? { signIn: state.deviceId } : {}),
    verified: connected,
  };
}

/** How long an answer that leaves this computer where it is ("live", "unknown", none) stands before the Admin is asked
 * again about the same Cloud. */
export const GONE_ASK_MS = 10 * 60_000;

/** One home's `gone(origin)`: `ask(origin)` is the Admin's word on this account's Cloud at `origin` (cloud-account.mjs
 * `movedTo`), kept per account (`account()`): a Cloud found gone stays gone; any other answer, or a failed ask, is asked
 * again only after GONE_ASK_MS. */
export function createGoneCheck({ ask, account, now = Date.now }) {
  const asked = new Map();
  return async (origin) => {
    const who = account();
    if (!who) return null;
    const key = `${who} ${origin}`, last = asked.get(key);
    if (last?.gone) return last.gone;
    if (last && now() - last.at < GONE_ASK_MS) return null;
    asked.set(key, { at: now(), gone: null });
    const said = await ask(origin).catch(() => null);
    const gone = said === "deleted" || said === "ended" ? said : null;
    asked.set(key, { at: now(), gone });
    return gone;
  };
}

function jsonFile(file) {
  return {
    read: () => { try { return JSON.parse(readFileSync(file(), "utf8")); } catch { return null; } },
    write: (value) => {
      const target = file();
      mkdirSync(dirname(target), { recursive: true });
      writeFileSync(`${target}.tmp`, JSON.stringify(value ?? null), { mode: 0o600 });
      renameSync(`${target}.tmp`, target);
    },
  };
}

async function answer(response) {
  const text = await response.text();
  try { return { ok: response.ok, status: response.status, body: text ? JSON.parse(text) : null }; }
  catch { return { ok: response.ok, status: response.status, body: null }; }
}

/**
 * @param {Omit<Parameters<typeof installCloudSend>[0], "local" | "move"> & {
 *   localRequest(route: string, init?: RequestInit): Promise<Response>,
 *   account(): { state(): any, pairHome(): Promise<{ origin: string, code: string }>, exportHome(): Promise<any> } | null,
 *   journalFile(): string,
 *   tempRoot(): string,
 *   lend?(projects: string[]): Promise<unknown>,
 *   phone?: { paired(): boolean, start(): Promise<void> },
 *   openHome?(): void,
 *   openLocal?(): void,
 *   restart?(): void,
 *   version?: string,
 *   log?(line: string): void,
 *   journal?: { read(): unknown, write(value: unknown): void },
 *   openTeam?(team: { id: string, name: string }): void,
 *   teamJournal?: { read(): unknown, write(value: unknown): void },
 *   keep?: { read(): unknown, write(value: unknown): void },
 *   reportTimers?: { set(work: () => void, ms: number): unknown, clear(timer: unknown): void },
 *   other?(): null | { view(): unknown, seen(): unknown, retry(): Promise<unknown>, active(): boolean },
 *   accountsHere?(): number,
 *   clean?: import("./clean-workspace.mjs").CleanWorkspace | null,
 * }} deps `accountsHere`: how many accounts are signed in on this device: with more than one, which Cloud this computer's bots
 * belong in is the person's to say, so no first move starts by itself. `localRequest`: this computer's server, as its owner (main adds the owner's header), and only its own copy while `clean`
 * (the clean workspace) runs. `account`: the MausBot
 * Cloud sign-in (cloud-account.mjs). `journal`: where the move's journal is kept, journalFile by default; `keep`: the
 * person's choice to keep this computer's bots here, one-home-keep.json beside it by default. `openTeam`:
 * shows a team's page, where a bot moved into it from My Cloud now lives (team-move-main.mjs, whose journal is
 * `teamJournal`, team-moves.json beside this one by default). `reportTimers`: the waits of the move's report to the Admin
 * (move-status.mjs), setTimeout's by default. `other`: the move to a server of the person's own (own-home-main.mjs):
 * while it is chosen no move to My Cloud starts, and its line is the one this computer's page shows. The rest are
 * installCloudSend's.
 */
export function installOneHome({ localRequest: anyServer, clean = null, account, journalFile, tempRoot, lend, phone, openHome, openLocal, restart, version, log = () => {}, journal: kept, keep: keptChoice, openTeam, teamJournal,
  reportTimers, other = () => null, accountsHere = () => 1, chooses = false, t = nativeText.englishText.t, moveLending = () => null, allowCommands = async () => false, ...sendDeps }) {
  const fetchImpl = sendDeps.fetch ?? globalThis.fetch;
  const localRequest = clean ? clean.realCopy(anyServer) : anyServer;
  const onOwnCopy = (work) => (clean ? clean.holding(work) : work());
  let home = null;
  let lastInput = 0;
  const watched = new WeakSet();
  let teamMove = null;
  let botMove = null;
  // One owner session for the whole move, every pass and check and the switch: each new one is one of the Admin's codes.
  const moveSession = createMoveSession({ open: (target) => openOwnerSession(target, fetchImpl, { label: MOVE_LABEL, background: false }) });
  const send = installCloudSend({ ...sendDeps, t, local: (route, init) => localRequest(route, { method: init?.method ?? "GET", signal: AbortSignal.timeout(init?.timeoutMs ?? 30_000) }),
    move: () => (home?.view() === null && other()?.view() ? other() : home), teamMove: () => teamMove, botMove: () => botMove, moveSession });
  teamMove = installTeamMove({ t, ipcMain: sendDeps.ipcMain, cloudPage: sendDeps.cloudPage, pageOrigin: sendDeps.pageOrigin, confirmMove: sendDeps.confirmMove, home: sendDeps.home, account: () => account(), fetch: fetchImpl, openTeam,
    file: () => join(dirname(journalFile()), "team-move-send.json"), journalFile: () => join(dirname(journalFile()), "team-moves.json"), ...(teamJournal ? { journal: teamJournal } : {}),
    onChange: () => send.publish(), log });
  const journal = kept ?? jsonFile(journalFile);
  const keep = keptChoice ?? jsonFile(() => join(dirname(journalFile()), "one-home-keep.json"));
  const status = () => moveStatus({ journal: parseJournal(journal.read()), state: account()?.state(), kept: home.keeps(), facts: home.facts(), now: Date.now(), version });
  const report = createMoveStatusReport({
    status,
    // A sign-in the Admin has ended (or that expired) only gets 401s until the person signs in again.
    signIn: () => { const state = account()?.state(); return state?.status === "reauth-required" ? null : state?.deviceId ?? null; },
    send: async (body) => (await account()?.reportMoveStatus?.(body)) === true,
    log,
    ...(reportTimers ? { timers: reportTimers } : {}),
  });
  /** When the person last typed or clicked in the window. */
  const typed = () => {
    const win = sendDeps.window?.();
    if (win && !win.isDestroyed?.() && !watched.has(win.webContents)) {
      watched.add(win.webContents);
      win.webContents.on?.("before-input-event", () => { lastInput = Date.now(); });
      // A watch set up just now has seen nothing yet: it counts as a key now, never as "typed long ago".
      lastInput = Math.max(lastInput, Date.now());
    }
    return lastInput;
  };

  const local = {
    call: async (method, route, body) => answer(await localRequest(route, { method, signal: AbortSignal.timeout(120_000),
      ...(body === undefined ? {} : { headers: { "content-type": "application/json" }, body: JSON.stringify(body) }) })),
    async json(route) {
      const got = await local.call("GET", route);
      if (!got.ok) throw new Error("OpenMausBot on this computer did not answer. Try again in a moment.");
      return got.body;
    },
  };
  const cloudOrigin = () => journal.read()?.cloud?.origin ?? null;
  let known = null;
  /** This computer's installation (`installationOf`), once its server has said who it is; null until then. */
  const installation = async () => {
    if (known) return known;
    const id = (await local.json("/.well-known/openmausbot/environment").catch(() => null))?.environmentId;
    if (typeof id === "string" && id) known = installationOf(id);
    return known;
  };
  const target = () => {
    const client = account(), origin = cloudOrigin() ?? oneHomeCloud(client?.state())?.origin;
    if (!client || !origin) throw new Error("This computer has no My Cloud yet.");
    return { origin, grant: () => client.pairHome() };
  };

  const backupLocal = {
    json: local.call,
    upload: async (file, bytes) => answer(await localRequest("/api/workspace-backup/upload", { method: "POST", duplex: "half", signal: AbortSignal.timeout(60 * 60_000),
      headers: { "content-type": "application/octet-stream", "content-length": String(bytes) }, body: Readable.toWeb(createReadStream(file)) })),
  };
  const again = createHomeAgain({
    async exportGrant() {
      const asked = await account()?.exportHome({ installation: (await installation()) ?? undefined });
      if (!asked) throw new Error("Sign in to MausBot Cloud first.");
      // The code goes only to the Cloud this computer moved to.
      if ("grant" in asked && asked.grant.origin !== cloudOrigin()) throw new Error("This is not the Cloud this computer moved to.");
      return asked;
    },
    open: (grant) => openOwnerSession({ origin: grant.origin, grant: async () => grant }, fetchImpl, { label: HOME_LABEL, background: false }),
    local: backupLocal,
    tempRoot: tempRoot(),
  });
  const keepHome = createHomeAgain({
    async exportGrant() {
      // My Cloud goes on: its owner's own code, for the Cloud this computer moved to only.
      const grant = await target().grant();
      if (grant?.origin !== cloudOrigin()) throw new Error("This is not the Cloud this computer moved to.");
      return { grant };
    },
    open: (grant) => openOwnerSession({ origin: grant.origin, grant: async () => grant }, fetchImpl, { label: KEEP_LABEL, background: false }),
    local: backupLocal,
    tempRoot: tempRoot(),
  });

  // Two computers moving into one Cloud take turns at the heavy part (server/cloud-send-lease.ts).
  const lease = createMoveLease({ session: () => moveSession.open(target()), holder: installation, log,
    label: async () => String((await local.json("/api/cloud-connections").catch(() => null))?.computer?.label ?? "") });
  // The person says which bots go, once, when there is a choice to make.
  const choice = jsonFile(() => join(dirname(journalFile()), "one-home-choice.json"));
  const botsHere = async () => choosable((await local.json("/api/bots?messages=0"))?.bots);
  const cloudTakesBots = () => { const cloud = oneHomeCloud(account()?.state()); return Boolean(cloud?.origin && cloud.moves && !cloud.ended); };
  botMove = createBotMove({ journal: jsonFile(() => join(dirname(journalFile()), "bot-moves.json")), local, send, session: () => moveSession.open(target()), ready: cloudTakesBots, lend, log, onChange: () => send.publish(),
    lease: createMoveLease({ session: () => moveSession.open(target()), holder: installation, log, label: async () => String((await local.json("/api/cloud-connections").catch(() => null))?.computer?.label ?? "") }) });
  home = createOneHome({
    journal,
    t,
    lease,
    ...(chooses ? { choosing: async () => choice.read()?.answered !== true && (await botsHere()).length > 1 } : {}),
    // Every run starts here: the key watch is set up before any wait asks, so a key pressed since then counts.
    cloud: () => {
      typed();
      const cloud = oneHomeCloud(account()?.state()), moved = parseJournal(journal.read());
      return cloud && (other()?.active() || (accountsHere() > 1 && (!moved || moved.phase === "back"))) ? { ...cloud, moves: false } : cloud;
    },
    send,
    local: {
      ...local,
      freeze: async (origin) => freezeAnswer(await local.call("POST", "/api/move/freeze", { cloud: origin })),
      async unfreeze(options) {
        const got = await onOwnCopy(() => local.call("DELETE", "/api/move/freeze", options?.automations === "off" ? { automations: "off" } : undefined));
        if (!got.ok) throw Object.assign(new Error("OpenMausBot on this computer could not go on again."), { status: got.status });
      },
      frozen: async () => (await local.json("/api/move/freeze"))?.frozen === true,
      marker: () => local.json("/api/move/freeze"),
      async forget() {
        const got = await onOwnCopy(() => local.call("POST", "/api/move/forget"));
        if (!got.ok) throw Object.assign(new Error("This computer's copy could not be deleted."), { status: got.status });
      },
    },
    gone: createGoneCheck({ ask: async (origin) => (await account()?.movedTo?.(origin)) ?? null, account: () => account()?.state()?.account?.id ?? null }),
    session: () => moveSession.open(target()),
    release: () => moveSession.end(),
    lend, phone, openHome, openLocal, restart, version, log,
    again,
    keep,
    keepHome,
    installation,
    async reportMove() {
      const client = account(), mine = await installation();
      return Boolean(client?.reportMove && mine && await client.reportMove({ installation: mine }));
    },
    async reportKept() {
      const client = account(), mine = await installation();
      return Boolean(client?.reportKept && mine && await client.reportKept({ installation: mine }));
    },
    lastInput: typed,
    ownCopy: () => clean?.ownCopy() ?? true,
    onChange: () => { send.publish(); report.changed(); },
  });

  /** The switch "Keep this computer's bots on this computer": `shown` only to someone with a Cloud of their own the move
   * applies to; `moved`, turning it on brings this computer's bots home; `coming`, they are on their way past turning back. */
  const keepState = () => {
    const cloud = oneHomeCloud(account()?.state()), moving = parseJournal(journal.read());
    return { shown: Boolean(cloud?.origin) && (!moving || moving.cloud.account === cloud.account) && (cloud.moves || Boolean(moving) || home.keeps()), on: home.keeps(), moved: awayIn(moving) && moving.phase !== "keeping",
      coming: underWay(moving) };
  };
  const onPage = (channel, handler) => (event, ...args) => {
    if (!sendDeps.cloudPage(event)) throw new Error(`${channel} is only available in this app's window`);
    return handler(...args);
  };
  sendDeps.ipcMain.handle("one-home:keep", onPage("one-home:keep", keepState));
  sendDeps.ipcMain.handle("one-home:keep-set", onPage("one-home:keep-set", (on) => {
    if (on === true && !keepState().shown) throw new Error(t("native.cloud.keepNoCloud"));
    if (on !== true) void botMove.drop().catch((error) => log(`bot move: ${error?.message ?? error}`));
    void home.keep(on === true).catch((error) => log(`one home: ${error?.message ?? error}`));
    return keepState();
  }));

  const localPage = (channel, handler) => (event, ...args) => {
    if (sendDeps.cloudPage(event) !== "local") throw new Error(`${channel} is only available on this computer's page`);
    return handler(...args);
  };
  const botMoveState = () => {
    const moving = parseJournal(journal.read());
    return { available: cloudTakesBots() && (!moving || moving.phase === "back"), moving: botMove.moving() };
  };
  sendDeps.ipcMain.handle("cloud-send:bot-move-state", localPage("cloud-send:bot-move-state", botMoveState));
  // Asked on whichever page is open: a person just back from buying My Cloud is looking at My Cloud. The bots come from this
  // computer's own server, never from the page; the page only names which of them.
  sendDeps.ipcMain.handle("cloud-send:bot-choices", onPage("cloud-send:bot-choices", async () => ({ bots: (await botsHere()).map((bot) => ({ id: bot.id, name: bot.name })) })));
  // What moving these bots changes for them, before the person presses Move: their setup from this computer's server,
  // and what this computer lends My Cloud now.
  sendDeps.ipcMain.handle("cloud-send:move-changes", localPage("cloud-send:move-changes", async (ids) => {
    const asked = (Array.isArray(ids) ? ids.map(String) : []).filter((id) => /^[\w-]{1,128}$/u.test(id)).slice(0, 500);
    if (!asked.length) return null;
    const got = await local.call("GET", `/api/cloud-send/changes?bots=${asked.map(encodeURIComponent).join(",")}`);
    if (!got.ok || !got.body?.facts) return null;
    const lent = await Promise.resolve().then(moveLending).catch(() => null);
    const { paths, ...facts } = got.body.facts;
    return { facts: { ...facts, folder: shownFolder(facts.folder ?? null, homedir()) },
      lending: lendingFacts({ state: lent?.state ?? null, paused: lent?.paused === true, folders: Array.isArray(paths) ? paths : [], home: homedir() }) };
  }));
  // `options`, from the move step: `folder` false keeps the bots' working folders from being lent (they were before the
  // step showed it, and are whenever it says nothing); `commands` asks main's own dialog, which defaults to Cancel, to let
  // the person's Clouds run commands here, unless they may already. A grant that fails is said on the move's own line.
  sendDeps.ipcMain.handle("cloud-send:move-bots", onPage("cloud-send:move-bots", async (ids, options) => {
    if (!botMoveState().available) throw new Error(t("native.cloud.keepNoCloud"));
    const asked = new Set(Array.isArray(ids) ? ids.map(String) : []);
    const bots = (await botsHere()).filter((bot) => asked.has(bot.id)).map((bot) => ({ id: bot.id, name: bot.name }));
    if (!bots.length) throw new Error(t("teamMove.refused.notABot"));
    let commandsError = null;
    if (options?.commands === true) {
      const lent = await Promise.resolve().then(moveLending).catch(() => null);
      if (lent?.state?.terminal !== true) {
        await Promise.resolve().then(allowCommands).catch((error) => {
          commandsError = String(error?.message ?? error).slice(0, 300) || "?";
          log(`bot move: commands not lent (${commandsError})`);
        });
      }
    }
    choice.write({ v: 1, answered: true });
    await home.keep(true);
    botMove.start(bots, oneHomeCloud(account()?.state())?.account ?? null, { keepFolders: options?.folder !== false, ...(commandsError ? { commandsError } : {}) });
    return botMoveState();
  }));
  sendDeps.ipcMain.handle("cloud-send:move-everything", onPage("cloud-send:move-everything", () => {
    choice.write({ v: 1, answered: true });
    void botMove.drop().catch((error) => log(`bot move: ${error?.message ?? error}`));
    void home.keep(false).catch((error) => log(`one home: ${error?.message ?? error}`));
    return botMoveState();
  }));
  sendDeps.ipcMain.handle("cloud-send:bot-move-retry", localPage("cloud-send:bot-move-retry", () => { void botMove.retry(); return botMove.view(); }));
  sendDeps.ipcMain.handle("cloud-send:open-my-cloud", localPage("cloud-send:open-my-cloud", () => { openHome?.(); }));

  return {
    ...send,
    /** The switch "Keep this computer's bots on this computer", as Settings shows it, and the person turning it. */
    keep: { state: keepState, set: (on) => home.keep(on === true) },
    /** Why the account signed in can't be left now: its move, or a bot of its own moving into a team, is under way. */
    holding: () => { const bot = teamMove.view()?.phase; return home.moving() || (bot && !["done", "failed"].includes(bot)) || botMove.moving().length ? "moving" : null; },
    /** A move to My Cloud, its way home, or a bot's move is under way (any journal phase but done and back, unless paused): the
     * session is read every minute for it, as when the window is in front (cloud-account.mjs `attended`). */
    underWay: () => { const moving = parseJournal(journal.read()); const bot = teamMove.view()?.phase;
      return Boolean(moving && !moving.paused && !["done", "back"].includes(moving.phase)) || Boolean(bot && !["done", "failed"].includes(bot)) || botMove.moving().length > 0; },
    /** Goes on with the move, and with any bot moving into a team: at launch, and whenever the MausBot Cloud sign-in changes. */
    kick: () => {
      void teamMove.kick();
      void botMove.kick();
      const cloud = oneHomeCloud(account()?.state());
      if (cloud?.ended) void botMove.back(cloud.account);
      return home.kick();
    },
    /** Home is My Cloud: this computer's own server is frozen for good. */
    away: () => home.away(),
    view: () => home.view(),
    close: () => { teamMove.close(); botMove.close(); home.close(); report.close(); void moveSession.end(); },
    /** A bot moving from My Cloud into a team (team-move-main.mjs). */
    teamMove,
    oneHome: home,
    /** The journal as it is on disk. */
    journal: () => journal.read(),
    /** Where the move stands as the Admin is told it (move-status.mjs), and that report sent now if it is due. */
    moveStatus: { read: status, flush: () => report.flush() },
    /** This computer's server's freeze marker: `{ frozen, cloud? }`. */
    marker: () => local.json("/api/move/freeze"),
    lastInput: typed,
  };
}