Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

27.2 KB
Raw Blame
import "server-only";
import dns from "node:dns/promises";
import net from "node:net";
import { cleanCopy, type ServiceCopy } from "@/lib/service-copy";
import { cleanMarks, type ModelMarks } from "@/lib/model-marks";
import { cleanNavLink, type NavLink } from "@/lib/nav-link";
import { isCloudProvider, type CloudProvider } from "@/lib/your-keys";
import { accountEngineMode, accountKeyInfo, accountProviders, paidAllowed, usableProviders } from "./account-keys";
import { accountGpu, type Account, type AccountGpu } from "./auth";
import type { OwnerGpuView } from "@/lib/connections";
import { runnerFacts, type Principal } from "./computer/tasks";
import { offeredSummary } from "./model-access";
import { getModel, PRESETS } from "@/lib/catalog";
import { presetRoutes } from "@/lib/catalog/preset-recipe-routes";
import { operationOutput } from "@/lib/catalog/types";
import { PROVIDERS } from "./providers";
import { blockedForAccounts, fetchPublic, isLoopbackGpu, peekText } from "./remote-fetch";
import { redactKey } from "./provider-keys";
import { networkCode, recordRunEvent } from "./run-events";
import { isInstallHost, REFUSED_ADDRESS } from "./gpu/hosts";
import { balance as billingBalance, policy as billingPolicy, type Action, type Balance } from "./billing";
import { poolHasFiles, poolReady, poolTakes } from "./hosted";
import { placeName } from "./place-name";
import { helperKnown } from "./text-helper";
import { poolOwnModels, poolOwnPresets, type PresetPress } from "./pool-own-setup";
import { ranOwnFirst, runOnOf, type RunOn } from "./run-on";
import { siteFallbackFor } from "./site-fallback";
import type { SiteFallback } from "@/lib/site-run";
import { poolRulesOf, type PoolRules, type PresetModel } from "@/lib/pool-rules";
import { diagnoseAddress, diagnoseGpu, gpuHostOf, type Diagnosis, type GpuFailure, type GpuPlace, type GpuStage } from "@/lib/diagnose";

/** A diagnosis as the status strip sends it. */
const visitorTrouble = (d: Diagnosis): VisitorTrouble => ({ cause: d.cause, words: d.words, actions: d.actions, retryable: d.retryable, ...(d.details && { details: d.details }) });

/**
 * "Your GPU" (public sign-up): the ComfyUI an account connects, where its runs go. The server calls
 * it, so it must be reachable from the internet (a Cloudflare Tunnel or ngrok in front of a local
 * ComfyUI). Addresses on this machine or a private network are refused, and so is the install's own
 * ComfyUI. A ComfyUI on the visitor's own computer (localhost) is the browser's to reach, never sent here.
 */

/**
 * A check of an account's GPU that failed: its plain words (the message), and what helps (diagnose.ts). `failure`:
 * what came back, so a run can say the same in its own words (diagnoseGpu at its stage).
 */
export class GpuError extends Error {
  constructor(
    readonly trouble: Diagnosis,
    readonly failure?: GpuFailure,
  ) {
    super(trouble.words);
  }
}

/** A check's failure, read for the person. A token the answer echoed (a page that repeats the request's headers) never in it. */
const failed = (f: GpuFailure, place: GpuPlace, token?: string) => {
  const out = (t: string | undefined) => (t === undefined || !token ? t : redactKey(t, token));
  const clean = { ...f, body: out(f.body), error: out(f.error), location: out(f.location) };
  return new GpuError(diagnoseGpu(clean, place), clean);
};

const allowPrivate = () => process.env.FUOBE_ALLOW_PRIVATE_URLS === "1";

export const LOCAL_GPU_MESSAGE = diagnoseAddress("local").words;

/** An address as stored and compared: origin and path, no trailing slash. */
const normal = (u: URL) => `${u.origin}${u.pathname.replace(/\/+$/, "")}`;

/** What checkGpuAddress fixed by itself in an address as pasted: "https://" added at its start, a "?…" or "#…" part left out. */
export type AddressFix = "scheme" | "query";

/**
 * An address as pasted, made whole: spaces trimmed, https:// added when it has no scheme (abc-8188.proxy.runpod.net),
 * and a "?…" or "#…" copied from the browser's bar left out. A password inside it is refused: it goes in the token box.
 */
export function cleanGpuAddress(raw: string): { url: URL; fixed: AddressFix[] } {
  const fixed: AddressFix[] = [];
  let text = raw.trim();
  // "abc-8188.proxy.runpod.net" or "localhost:8188": no scheme (a host:port reads as one to URL), so https:// goes first.
  if (text && !/^[a-z][a-z0-9+.-]*:\/\//i.test(text)) {
    text = `https://${text.replace(/^\/+/, "")}`;
    fixed.push("scheme");
  }
  let u: URL;
  try {
    u = new URL(text);
  } catch {
    throw new GpuError(diagnoseAddress("format"));
  }
  if (u.protocol !== "http:" && u.protocol !== "https:") throw new GpuError(diagnoseAddress("scheme"));
  if (u.username || u.password) throw new GpuError(diagnoseAddress("credentials"));
  if (u.search || u.hash) {
    u.search = "";
    u.hash = "";
    fixed.push("query");
  }
  return { url: u, fixed };
}

/**
 * Check an account's ComfyUI address: http(s), a public host, not the install's own ComfyUI. Returns it normalised (made
 * whole first by cleanGpuAddress; `fixed` gets what that changed).
 */
export async function checkGpuAddress(raw: string, o: { resolve?: (host: string) => Promise<string[]>; fixed?: AddressFix[] } = {}): Promise<string> {
  const { url: u, fixed } = cleanGpuAddress(raw);
  o.fixed?.push(...fixed);
  const host = u.hostname.replace(/^\[|\]$/g, "").toLowerCase();
  const local = host === "localhost" || host.endsWith(".localhost") || host.endsWith(".local") || host.endsWith(".lan") || host.endsWith(".internal") || !host.includes(".");
  // A stand-in ComfyUI on this machine is allowed in tests and `next dev` only (remote-fetch.ts loopbackGpuPort).
  if (!isLoopbackGpu(u)) {
    if (local && !net.isIP(host) && !allowPrivate()) throw new GpuError(diagnoseAddress("local"));
    const resolve = o.resolve ?? (async (h: string) => (await dns.lookup(h, { all: true, verbatim: true })).map((a) => a.address));
    let addrs: string[];
    try {
      addrs = net.isIP(host) ? [host] : await resolve(host);
    } catch {
      throw new GpuError(diagnoseAddress("not-found", u.href));
    }
    if (!addrs.length) throw new GpuError(diagnoseAddress("not-found", u.href));
    // Private networks, this machine's own addresses (loopback too, even with FUOBE_ALLOW_PRIVATE_URLS), wrapped IPv4.
    if (addrs.some(blockedForAccounts)) throw new GpuError(diagnoseAddress("local"));
  }
  const url = normal(u);
  // The install's own ComfyUI on any scheme, port or path (ComfyUI answers every route under /api too), compared by
  // host. One message for every such refusal, so it never confirms a guessed address.
  // (The tests' and `next dev`'s stand-in on this machine is on a port of its own: loopbackGpuPort.)
  if (!isLoopbackGpu(u) && isInstallHost(url)) throw new GpuError({ ...diagnoseAddress("studio"), words: REFUSED_ADDRESS });
  return url;
}

export interface GpuInfo {
  device?: string;
  vramBytes?: number;
  version?: string;
}

/** An answer's text, up to `max` (ComfyUI's /system_stats is a few KB; an error page's start, for the details line). */
async function headText(res: Response, max = 4096): Promise<string> {
  try {
    return (await res.text()).slice(0, max);
  } catch {
    return "";
  }
}

const safeUrl = (u: string, base?: string): URL | null => {
  try {
    return new URL(u, base);
  } catch {
    return null;
  }
};

/**
 * Ask the account's ComfyUI for /system_stats, the check the studio makes of its own. Anything but ComfyUI's answer
 * throws a GpuError that says what it is (diagnose.ts: a stopped RunPod pod, a GPU starting up, a password…). An
 * http:// address that sends to the same host's https:// is followed once (`url` then names the https address).
 */
export async function testGpu(
  gpu: { url: string; token?: string },
  o: { resolve?: (host: string) => Promise<string[]>; timeoutMs?: number; maxBytes?: number; stage?: GpuStage } = {},
): Promise<GpuInfo & { url?: string }> {
  const ask = (url: string) =>
    fetchPublic(
      `${url}/system_stats`,
      // ngrok's free addresses show their warning page instead unless this is sent.
      { headers: { "ngrok-skip-browser-warning": "1", ...(gpu.token ? { Authorization: `Bearer ${gpu.token}` } : {}) } },
      { timeoutMs: o.timeoutMs ?? 10_000, deadlineMs: (o.timeoutMs ?? 10_000) + 5_000, maxBytes: o.maxBytes ?? 1 * 2 ** 20, resolve: o.resolve },
    );
  const place = { kind: "link" as const, url: gpu.url, hasToken: !!gpu.token };
  const stage = o.stage ?? "test";
  let url = gpu.url;
  let res: Response;
  try {
    res = await ask(url);
    // http:// sent to the same host's https://: followed once.
    const to = res.status >= 300 && res.status < 400 ? res.headers.get("location") : null;
    const next = to ? safeUrl(to, url) : null;
    const from = safeUrl(url);
    if (next && from && from.protocol === "http:" && next.protocol === "https:" && next.hostname === from.hostname) {
      res.body?.cancel().catch(() => {});
      // To the port the redirect named (none: 443), never one it didn't.
      url = normal(new URL(`https://${next.host}${from.pathname}`));
      res = await ask(url);
    }
  } catch (e) {
    // The kind of address and what went wrong, never the address (nor the IP and port a socket error names).
    // (Not for the status strip's check, every 30 s while a page is open: a run that can't reach it logs its own line.)
    if (stage !== "status") recordRunEvent("your-gpu-check", { stage, hostKind: gpuHostOf(gpu.url).kind, code: networkCode((e as Error).message) }, "warn");
    throw failed({ stage, error: (e as Error).message }, place, gpu.token);
  }
  const contentType = res.headers.get("content-type") ?? undefined;
  if (!res.ok) {
    const redirect = res.status >= 300 && res.status < 400;
    // An error page's start only, briefly: one that never ends can't hold the check.
    const body = redirect ? "" : await peekText(res);
    res.body?.cancel().catch(() => {});
    const retryAfterS = Number(res.headers.get("retry-after")) || undefined;
    throw failed({ stage, status: res.status, body, contentType, location: res.headers.get("location") ?? undefined, retryAfterS }, place, gpu.token);
  }
  const text = await headText(res, o.maxBytes ?? 1 * 2 ** 20);
  let s: { system?: { comfyui_version?: string }; devices?: { name?: string; vram_total?: number }[] } | null = null;
  try {
    s = JSON.parse(text);
  } catch {}
  if (!s?.system) throw failed({ stage, status: res.status, body: text.slice(0, 4096), contentType }, place, gpu.token);
  const dev = s.devices?.[0];
  return { device: dev?.name, vramBytes: typeof dev?.vram_total === "number" ? dev.vram_total : undefined, version: s.system.comfyui_version, ...(url !== gpu.url && { url }) };
}

declare global {
  /** Each account's GPU as last asked (Your GPU: online or not, what it is, and why not), for the status strip. */
  var __fuobeAccountGpus: Map<string, { url: string; at: number; online: boolean; info: GpuInfo; trouble?: Diagnosis }> | undefined;
  /** The checks on their way, one per account: status requests that arrive meanwhile wait for it. */
  var __fuobeAccountGpuChecks: Map<string, Promise<GpuStateAnswer>> | undefined;
}

/** What a status check reads of /system_stats at most: a ComfyUI's answer is a few KB. */
const STATUS_MAX_BYTES = 64 * 1024;

/** What the status check says of an account's GPU: online or not, what it is, and (offline) why, with what helps. */
export type GpuStateAnswer = { online: boolean; comfy?: "down" | "no-setting"; trouble?: Diagnosis } & GpuInfo;

/** How long a status check's answer is kept: the status strip asks an account's GPU at most this often. */
export const GPU_STATE_MS = 30_000;

/**
 * Note what a check or a run just learnt of an account's link (Settings' Check my GPU, a run that couldn't reach it, or
 * one that did): the status strip, the composer's check before Run and the next run read it without asking again.
 */
export function noteGpuState(id: string, url: string, online: boolean, o: { info?: GpuInfo; trouble?: Diagnosis } = {}): void {
  const cache = (globalThis.__fuobeAccountGpus ??= new Map());
  const hit = cache.get(id);
  const info = o.info ?? (hit?.url === url ? hit.info : {});
  cache.delete(id);
  cache.set(id, { url, at: Date.now(), online, info, ...(!online && o.trouble && { trouble: o.trouble }) });
  if (cache.size > 5000) cache.delete(cache.keys().next().value!);
}

/** The last status check's answer for an account's saved link, while it is fresh: never asks the GPU. */
export function cachedGpuState(id: string, url: string): GpuStateAnswer | undefined {
  const hit = globalThis.__fuobeAccountGpus?.get(id);
  if (!hit || hit.url !== url || Date.now() - hit.at >= GPU_STATE_MS) return undefined;
  return { online: hit.online, ...hit.info, ...(hit.trouble && { trouble: hit.trouble }) };
}

/**
 * Is the account's ComfyUI answering, and what is it? Asked at most every 30 s per account, and one check at a time:
 * status requests that arrive while one is on its way share its answer.
 */
export async function gpuState(id: string, gpu: AccountGpu): Promise<GpuStateAnswer> {
  // On the person's own computer: what its Fuobe tab last said (online while it beats and its ComfyUI answers; `comfy`:
  // the tab is open, but ComfyUI is closed or runs without Fuobe's setting).
  if (gpu.mode === "computer") {
    const f = runnerFacts(`acct:${id}`);
    return { online: !!f?.online, ...(f?.comfy && { comfy: f.comfy }), ...(f?.device && { device: f.device }), ...(f?.vramBytes && { vramBytes: f.vramBytes }), ...(f?.version && { version: f.version }) };
  }
  const cache = (globalThis.__fuobeAccountGpus ??= new Map());
  const hit = cache.get(id);
  if (hit && hit.url === gpu.url && Date.now() - hit.at < GPU_STATE_MS) return { online: hit.online, ...hit.info, ...(hit.trouble && { trouble: hit.trouble }) };
  const checks = (globalThis.__fuobeAccountGpuChecks ??= new Map());
  const key = `${id} ${gpu.url}`;
  const going = checks.get(key);
  if (going) return going;
  const check = (async (): Promise<GpuStateAnswer> => {
    let online = false;
    let info: GpuInfo = hit?.url === gpu.url ? hit.info : {};
    let trouble: Diagnosis | undefined;
    try {
      const { url: _moved, ...read } = await testGpu(gpu, { timeoutMs: 5000, maxBytes: STATUS_MAX_BYTES, stage: "status" });
      void _moved;
      info = read;
      online = true;
    } catch (e) {
      if (e instanceof GpuError) trouble = e.trouble;
    }
    noteGpuState(id, gpu.url, online, { info, trouble });
    return { online, ...info, ...(trouble && { trouble }) };
  })().finally(() => checks.delete(key));
  checks.set(key, check);
  return check;
}

/**
 * The device type ("cuda", "cpu", "mps"…) of an account's ComfyUI as its last status check read it (the start of
 * /system_stats devices[0].name: "cuda:0 NVIDIA …"), without asking it again; undefined when no check of its saved
 * address has read one. A price check reads this and never calls the GPU (Split soundtrack's pick and time).
 */
export function lastGpuDevice(id: string): string | undefined {
  const gpu = accountGpu(id);
  // On the person's own computer: what its Fuobe tab last said.
  if (gpu?.mode === "computer") return /^[a-z]+/i.exec(runnerFacts(`acct:${id}`)?.device ?? "")?.[0].toLowerCase();
  const hit = globalThis.__fuobeAccountGpus?.get(id);
  if (!gpu || hit?.url !== gpu.url) return undefined;
  return /^[a-z]+/i.exec(hit.info.device ?? "")?.[0].toLowerCase();
}

/**
 * What someone's computer's Fuobe tab last said (an account's "this computer", or the owner's when ComfyUI runs on the
 * owner's computer): its GPU, memory, ComfyUI version and port, the guard and the cross-site setting, and whether it
 * is open now. Never an address, a token or anything of the server's.
 */
export function computerView(principal: Principal): OwnerGpuView {
  const f = runnerFacts(principal);
  return {
    mode: "computer" as const,
    online: !!f?.online,
    ...(f?.comfy && { comfy: f.comfy }),
    ...(f?.guard !== undefined && { guard: f.guard }),
    ...(f?.device && { device: f.device }),
    ...(f?.vramBytes && { vramBytes: f.vramBytes }),
    ...(f?.version && { version: f.version }),
    ...(f?.port && { port: f.port }),
    ...(f?.cors && { cors: f.cors }),
  };
}

/** What a visitor's status strip shows (GET /api/status): its own GPU, never the install's providers, servers or Demo mode. */
export interface VisitorView {
  kind: "guest" | "account";
  /** `mode` "computer": the ComfyUI on the person's own computer, which their Fuobe tab runs. */
  gpu: {
    connected: boolean;
    mode?: "link" | "computer";
    online?: boolean;
    comfy?: "down" | "no-setting";
    device?: string;
    vramBytes?: number;
    /** A link that didn't answer its last status check: why, in plain words, and what helps (diagnose.ts). */
    trouble?: VisitorTrouble;
    /** A link's kind: a RunPod pod's port gets "Start your pod". */
    host?: "runpod" | "tunnel" | "web";
  };
  /** The providers it has its own key for that this server can open ("Your keys"): never a key. */
  keys: CloudProvider[];
  /** Of those, the ones a run would be taken on now (not refused by the provider, balance not known to be empty). */
  usableKeys?: CloudProvider[];
  /** Keys whose provider account's balance ran out on a run (Add credit before the next one). */
  emptyKeys?: CloudProvider[];
  /** The studio lets accounts run paid models on their own keys (it isn't open-source only). */
  paidAllowed: boolean;
  /**
   * Hosted runs (FUOBE_HOSTED_RUNS=1): this account (or a guest, once signed up) may run on this install's GPUs, so
   * connecting a GPU of its own is optional.
   */
  hosted?: boolean;
  /**
   * "pool": runs go to this install's GPUs first, even when the account's own GPU is online (the billing service's
   * policy says so). Absent: its own GPU first, as before. Either can always be picked.
   */
  prefer?: "pool";
  /** The name visitors read for this install's GPUs ("Runs on Our GPUs"; src/server/place-name.ts). Only with `hosted`. */
  placeName?: string;
  /** The free text helper writes for this visitor ("Write the lyrics", "Rewrite my prompt now"). Only with `hosted`. */
  helper?: true;
  /**
   * Where the account's runs go, as it picked (run-on.ts; only with `hosted`, only to that account). Absent: the
   * install's default. `runKey`: with "key", the provider whose key runs them.
   */
  runOn?: RunOn;
  runKey?: CloudProvider;
  /**
   * The account ran on its own GPU or key before it first ran on this install's GPUs, and has picked no place yet:
   * the one-time note says runs go to this install's GPUs now (only with `hosted`, only to that account).
   */
  ranOwn?: true;
  /**
   * Its answer to "Run on {Place} automatically when your GPU can't" (site-fallback.ts; only with `hosted`, only to that
   * account): "on" or "off". Absent: never asked (the question comes once, after its first "Run on {Place}").
   */
  siteFallback?: SiteFallback;
  /**
   * The words a preset card shows when its run would go to this install's GPUs (the billing service's policy, such as
   * "Try free"; with no service, "Try free": those runs are free). Absent: the cards show none.
   */
  cardText?: string;
  /**
   * The billing service's words for open models in the model menu, by id, in place of "Free" (src/lib/model-marks.ts).
   * Only while the viewer's runs go to this install's GPUs.
   */
  marks?: ModelMarks;
  /** The billing service's link for the top bar (src/lib/nav-link.ts): its words and a same-site address. */
  navLink?: NavLink;
  /**
   * What this install's GPUs keep off (the policy's model and app lists, and its kinds), for a visitor with no GPU of its
   * own: its pickers leave those out instead of failing at Run. Absent: nothing is kept off, or it has a GPU of its own.
   */
  pool?: PoolRules;
  /** The billing service's words for the account's balance (billing.ts): the top bar's chip and the account menu. */
  balance?: Balance;
  /** Extra links the billing service sends for the menu. */
  links?: Action[];
  /** A notice to show once per id (the billing service's policy). */
  notice?: { id: string; text: string; url?: string };
  /** The billing service's display copy (src/lib/service-copy.ts), cleaned. Only with `hosted`. */
  copy?: ServiceCopy;
}

declare global {
  var __fuobePresetModels: PresetModel[] | undefined;
  var __fuobePresetPresses: PresetPress[] | undefined;
}

/** A preset's open route as its run plans it: its recipe's (with the end frame it edits first), else its own. */
const openRoute = (p: (typeof PRESETS)[number]) => presetRoutes(p, "open", (id) => !!getModel(id))[0] ?? p.open ?? undefined;

/**
 * Every preset with the open model it runs on and the kind it makes, and the open models of its run's other steps (the
 * edit that makes its end frame first), for the pickers' rules: made once.
 */
const presetModels = (): PresetModel[] =>
  (globalThis.__fuobePresetModels ??= PRESETS.map((p) => {
    const m = p.open ? getModel(p.open.modelId) : undefined;
    if (!m || !p.open) return { id: p.id };
    const step = openRoute(p)?.end;
    const end = step && getModel(step.modelId);
    return { id: p.id, model: { id: m.id, kind: operationOutput(p.open.op, m.kind) }, ...(step && end && { steps: [{ id: end.id, kind: operationOutput(step.op, end.kind) }] }) };
  }));

/**
 * Every preset's press on its open route, step by step (its end frame's edit first, then its clip to that frame, with the
 * LoRA files it adds): made once.
 */
const presetPresses = (): PresetPress[] =>
  (globalThis.__fuobePresetPresses ??= PRESETS.flatMap((p) => {
    const route = openRoute(p);
    if (!route) return [];
    const end = route.end;
    const loras = (route.loras ?? []).map((l) => l.file).filter(Boolean);
    return [{ id: p.id, steps: [...(end ? [{ modelId: end.modelId, op: end.op }] : []), { modelId: route.modelId, op: end ? "flf2v" : route.op, ...(loras.length && { loras }) }] }];
  }));

/** Keys whose balance ran out on a run, as the status says them. */
function emptyKeysOf(id: string): { emptyKeys: CloudProvider[] } | Record<string, never> {
  const info = accountKeyInfo(id);
  const empty = (Object.keys(info) as CloudProvider[]).filter((p) => info[p]?.balanceEmptyAt);
  return empty.length ? { emptyKeys: empty } : {};
}

/** What the status strip and the composer show of a GPU that didn't answer: never more than the person's own GPU said. */
export type VisitorTrouble = Pick<Diagnosis, "cause" | "words" | "actions" | "retryable" | "details">;

/**
 * GET /api/status for a guest or an account: ComfyUI is its own GPU (taken as there until it has one,
 * so the studios offer Generate and the run asks it to connect one), fal.ai and Replicate are its own
 * keys, the engine mode they allow (account-keys.ts), no Demo mode, and nothing of the install's own
 * servers, keys or addresses. `zone`: the browser's own time zone (the fuobe_tz cookie, src/lib/nav-link.ts), passed
 * on to the billing service with the balance.
 */
export async function visitorStatus(account: Account | null, zone?: string | null) {
  const gpu = account ? accountGpu(account.id) : null;
  const live = account && gpu ? await gpuState(account.id, gpu) : null;
  const keys = account ? accountProviders(account.id) : [];
  const viewer: VisitorView = {
    kind: account ? "account" : "guest",
    gpu: gpu
      ? {
          connected: true,
          mode: gpu.mode === "computer" ? "computer" : "link",
          online: !!live?.online,
          ...(live?.comfy && { comfy: live.comfy }),
          ...(live?.device && { device: live.device }),
          ...(live?.vramBytes && { vramBytes: live.vramBytes }),
          ...(gpu.mode !== "computer" && { host: gpuHostOf(gpu.url).kind }),
          ...(!live?.online && live?.trouble && { trouble: visitorTrouble(live.trouble) }),
        }
      : { connected: false },
    keys,
    ...(account && keys.length && { usableKeys: usableProviders(account.id) }),
    ...(account && keys.length && emptyKeysOf(account.id)),
    paidAllowed: paidAllowed(),
  };
  // Hosted runs and the billing hook: nothing of either with them off.
  if (poolReady()) {
    // A guest is told what the billing service's policy for guests says (no service: what any account gets).
    const p = await billingPolicy(account?.id ?? null);
    if (p?.hosted) viewer.hosted = true;
    if (p?.hosted && p.prefer === "pool") viewer.prefer = "pool";
    // What the visitor reads for this install's GPUs ("Runs on {name}"): the policy's name, else the install's.
    if (p?.hosted) viewer.placeName = placeName(p);
    // The free text helper (lyrics, a prompt rewrite) on this install's workers.
    if (p?.hosted && helperKnown()) viewer.helper = true;
    const card = p?.hosted && typeof p.cardText === "string" ? p.cardText.trim().slice(0, 40) : "";
    if (card) viewer.cardText = card;
    // The account's own pick of where its runs go, and whether the one-time note is for it (it had runs on its own GPU
    // or key before, and has picked nothing yet).
    if (account && p?.hosted) {
      const pick = runOnOf(account.id, keys);
      if (pick) {
        viewer.runOn = pick.runOn;
        if (pick.runKey) viewer.runKey = pick.runKey;
      } else if ((gpu || keys.length) && ranOwnFirst(account.id)) viewer.ranOwn = true;
      const fallback = siteFallbackFor(account.id);
      if (fallback) viewer.siteFallback = fallback;
    }
    // What this studio's GPUs keep off, the open models they can't run right now among it: shown in the pickers with
    // what the visitor needs to run them on its own setup. For every viewer, a GPU of its own or not: its runs go
    // there first too, and the label says what runs on its own GPU instead.
    const rules = p?.hosted ? poolRulesOf(p, presetModels(), poolOwnModels(), poolOwnPresets(presetPresses(), poolTakes, poolHasFiles)) : undefined;
    if (rules) viewer.pool = rules;
    if (account && p?.links?.length) viewer.links = p.links;
    if (account && p?.notice) viewer.notice = p.notice;
    // The service's words for open models in the menus, while this viewer's runs go to this install's GPUs (not a GPU
    // of its own it runs on first).
    const toSite = !gpu || viewer.runOn === "site" || (!viewer.runOn && p?.prefer === "pool");
    const marks = p?.hosted && toSite ? cleanMarks(p.marks) : null;
    if (marks) viewer.marks = marks;
    // The service's link for the top bar, for guests and accounts alike.
    const navLink = p?.hosted ? cleanNavLink(p.navLink) : null;
    if (navLink) viewer.navLink = navLink;
    // The service's display copy (wait and limit lines), for guests and accounts alike.
    const copy = p?.hosted ? cleanCopy(p.copy) : null;
    if (copy) viewer.copy = copy;
    const b = account ? await billingBalance(account.id, zone) : null;
    if (b) viewer.balance = b;
  }
  const configured = (id: string) => (id === "comfyui" ? (gpu ? !!live?.online : true) : isCloudProvider(id) && keys.includes(id));
  // With keys: the paid models the owner switched off (a cloud route no live call has passed yet runs on the visitor's
  // own key, labelled so: src/lib/own-setup.ts).
  const access = keys.length ? offeredSummary() : undefined;
  return {
    providers: Object.fromEntries(Object.values(PROVIDERS).map((p) => [p.id, { label: p.id === "comfyui" ? "Your GPU" : p.label, configured: configured(p.id) }])),
    demoMode: "off",
    engineMode: account ? accountEngineMode(account.id) : "open",
    cloudProvider: "auto",
    onboarded: true,
    // Settings → Models & licences: the paid models the owner switched off stay out of its pickers too.
    ...(access && { access }),
    viewer,
  };
}