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.

16.5 KB
Raw Blame
/**
 * Home's first screen: a grid of every kind of media Fuobe makes, each tile a real picture, clip or sound from the
 * studio it opens (src/components/home/home-bento.tsx). Every number is read from the catalog and every picture is a
 * published file with its content version, so nothing here is typed in twice (tests/home-bento.test.ts).
 */
import "server-only";
import { createHash } from "node:crypto";
import fs from "node:fs/promises";
import path from "node:path";
import { MODELS, isListed } from "@/lib/catalog";
import { getTemplate } from "@/lib/catalog/canvas";
import { runsOn } from "@/lib/catalog/engines";
import type { ModelDef } from "@/lib/catalog/types";
import { HOME_BENTO_GENJUTSU, HOME_BENTO_MEDIA, HOME_BENTO_REEL, HOME_BENTO_TINT } from "@/lib/home-bento.generated";
import { HOME_RELEASE, releaseArtSrc, releaseHref } from "@/lib/home-release";
import type { ReelClip, ReelWipe } from "@/lib/home-reel";
import type { Demo } from "@/lib/live/demo";
import { presetCover } from "@/lib/preset-covers";
import { RELEASE_TITLES } from "@/lib/release-day";
import { templateCover } from "@/lib/template-covers";

/** The preset whose clip the 3D tile plays (no other card on Home plays it: tests/home-showcase.test.ts). */
export const BENTO_CLIPS = { threeD: "app-3d-rotation" } as const;
/** Each clip's copies here (scripts/gen-home-bento.mjs): its name, and the widths it and its poster come in. */
export const BENTO_CLIP_FILES = { threeD: "rotation" } as const;
export const BENTO_CLIP_WIDTHS = [640, 1024, 1536, 2048] as const;
/**
 * The presets the video tile plays in turn, in this order: scripts/gen-home-bento.mjs (REEL) cuts each one's stretch
 * and frames it. The first one's first frame is the tile's poster.
 */
export const BENTO_REEL = HOME_BENTO_REEL.map((c) => c.id);
/** The video tile's id in the playback store: its own, as it plays several presets (one of them may be a card lower down). */
export const BENTO_REEL_ID = "home-reel";
/** The width of the JPEG copy of each clip's first frame, for a browser without AVIF. */
export const BENTO_CLIP_FALLBACK = 1024;
/**
 * The release tile: the latest open model's pictures (HOME_RELEASE.tile, from public/previews/release/), each shown for
 * `every` ms and faded into the next over `fade` ms, and the words of its badge.
 */
export const BENTO_RELEASE = { every: 3000, fade: 900, badge: "New · Day 0" } as const;
/**
 * The Genjutsu tile: a reel of swaps shown as they happen (HOME_BENTO_GENJUTSU, made by scripts/gen-home-bento.mjs):
 * Genjutsu's own Group swap example first (a couple, both people swapped in one take; the studio's cover
 * "genjutsu-group-swap"), then a character swap by the lake. Each opens on the original clip, and a divider sweeps the
 * swapped take in across it. Its id in the playback store is its own, so a card lower down can play the same cover.
 * Each clip ends faded to dark: after `hold` ms on that dark frame the next fades in over it, in `fade` ms (two faces
 * are never laid over each other).
 */
export const BENTO_GENJUTSU = { id: "home-genjutsu", cover: "genjutsu-group-swap", hold: 100, fade: 400 } as const;
/** The widths the Genjutsu clips come in: their renders are about 1024 px wide, and nothing is made up past that. */
export const BENTO_GENJUTSU_WIDTHS = [640, 768, 1024] as const;
/** The Canvas board the canvas tile draws: the same film as /canvas's "What's a canvas?". */
export const BENTO_BOARD = "effect-chain-pearl-earring";
/**
 * The Audio tile: Clean up voice's own demo, played in the tile (src/components/home/bento-voice.tsx, with the studio's
 * sound: src/components/audio-tools/use-clean-voice-demo.ts). Its photos are copies of the demo's, made by
 * scripts/gen-home-bento.mjs in these widths: `room` as it is, and `quiet`, the room softened around her, which shows
 * once the noise is off.
 */
export const BENTO_VOICE = { room: "voice-room", quiet: "voice-quiet", widths: [480, 768, 1024] } as const;
/** The Live edit steps the live tile plays: the session's first three changes (Home's Live edit section plays them all). */
export const BENTO_LIVE_STEPS = ["s0", "s1", "s2", "s3"];

/** Where each tile goes. Each label is the navigation's name for the place (or the app's own). */
export const BENTO_HREFS = {
  video: "/cinema",
  release: releaseHref("image"),
  audio: "/audio/clean",
  threeD: "/image-to-3d",
  genjutsu: "/genjutsu",
  numbers: "#home-studios",
  live: "/live-edit?from=home",
  canvas: "/canvas",
} as const;

export type BentoKey = keyof typeof BENTO_HREFS;

/**
 * A picture in two or more widths: AVIF for the browsers that take it, the published JPEG for the rest. `tint`: its
 * mean colour, shown until it is in (a clip's poster).
 */
export interface BentoPicture {
  avif: { src: string; w: number }[];
  fallback: string;
  tint?: string;
}

/** A tile's clip: its id in the playback store, its poster, and the clip in each width (the tile picks one). */
export interface BentoMovingTile {
  id: string;
  poster: BentoPicture;
  clips: { src: string; w: number }[];
}

/** The video tile: its id in the playback store, its poster (the first clip's first frame) and its clips in order. */
export interface BentoReelTile {
  id: string;
  poster: BentoPicture;
  reel: ReelClip[];
}

/**
 * The Genjutsu tile: a reel whose clips each show a swap under a divider (`wipes`, one per clip), and `still`, the first
 * clip with its divider at rest half way across, which the tile shows instead of the poster under reduced motion.
 */
export interface BentoWipeTile extends BentoReelTile {
  still: BentoPicture;
  wipes: ReelWipe[];
}

export interface BentoTile {
  key: BentoKey;
  /** The small line over the title: what kind of media, and how much of it. */
  meta: string;
  title: string;
  /** The link's own words (the place it opens). */
  cta: string;
  href: string;
  /** What a screen reader reads for the whole tile. */
  label: string;
}

/** The numbers Home's first screen quotes, each from the catalog. */
export interface BentoFacts {
  /** Studios, counted as the navigation counts them (its Studios badge and the launcher say the same number). */
  studios: number;
  apps: number;
  effectsAndMotions: number;
  motions: number;
  openModels: number;
  imageModels: number;
  videoModels: number;
  models3d: number;
  soundTools: number;
}

/**
 * Open models a visitor can pick, of one kind (Home is the same page for everyone, and everyone runs open models). The
 * same test as modelCounts().open (src/lib/catalog/counts.ts), so the kinds add up to its total.
 */
export const openModelsOf = (kind: ModelDef["kind"], models: readonly ModelDef[] = MODELS) => models.filter((m) => isListed(m) && runsOn(m, "open") && m.kind === kind).length;

/** The widths a file of the copies named `name` comes in (`<name>-<width>.<ext>`), as scripts/gen-home-bento.mjs made them. */
const widthsOf = (name: string, ext: string) => BENTO_CLIP_WIDTHS.filter((w) => HOME_BENTO_MEDIA[`/previews/home/${name}-${w}.${ext}`]);

/** A file made by scripts/gen-home-bento.mjs, with its version. */
function made(file: string): string {
  const v = HOME_BENTO_MEDIA[file];
  if (!v) throw new Error(`Home's first screen names ${file}, which scripts/gen-home-bento.mjs didn't make`);
  return `${file}?v=${v}`;
}

/** A published file under public/ with its content version (the first 8 hex of its SHA-256, as the covers' URLs). */
async function versioned(file: string): Promise<string> {
  const bytes = await fs.readFile(path.join(process.cwd(), "public", file)).catch(() => null);
  if (!bytes?.length) throw new Error(`Home's first screen names ${file}, which isn't a published file`);
  return `${file}?v=${createHash("sha256").update(bytes).digest("hex").slice(0, 8)}`;
}

/** A picture on the release tile: its copies, its original size and its framing in the tile. */
export interface BentoSlide extends BentoPicture {
  width: number;
  height: number;
  position: string;
}

/** The release tile's pictures, each in its 480 and 960 px AVIF and its 480 px WebP, at their content versions. */
async function releaseSlides(release = HOME_RELEASE): Promise<BentoSlide[]> {
  return Promise.all(
    release.tile.map(async ({ slug, position }) => {
      const art = release.art.find((a) => a.slug === slug);
      if (!art) throw new Error(`Home's release tile names ${slug}, which isn't one of the release's pictures`);
      const [a480, a960, fallback] = await Promise.all([versioned(releaseArtSrc(slug, 480, "avif")), versioned(releaseArtSrc(slug, 960, "avif")), versioned(releaseArtSrc(slug, 480, "webp"))]);
      return { avif: [{ src: a480, w: 480 }, { src: a960, w: 960 }], fallback, width: art.width, height: art.height, position };
    }),
  );
}

/** The phrase typed before each step of the Live edit session, in its own words: every `type` since the last step. */
export function livePhrases(demo: Pick<Demo, "script">, steps = BENTO_LIVE_STEPS): string[] {
  const out: string[] = [];
  let typed = "";
  for (const s of demo.script) {
    if (s.type === "type") typed += s.text;
    else if (s.type === "frame" && !s.live && steps.includes(s.frame) && s.frame !== steps[0]) {
      out.push(typed.replace(/^[,\s]+/, ""));
      typed = "";
    }
  }
  return out;
}

async function readDemo(): Promise<Demo> {
  return JSON.parse(await fs.readFile(path.join(process.cwd(), "public", "previews", "live-edit", "demo.json"), "utf8")) as Demo;
}

/**
 * What the canvas tile draws: the board's nodes in order (its photo, its steps, its film), each with its label; the
 * photo and the film show the board's own pictures (scripts/gen-home-bento.mjs makes their small copies).
 */
function board() {
  const t = getTemplate(BENTO_BOARD);
  if (!t || !templateCover("canvas", BENTO_BOARD)) throw new Error(`Home's canvas tile names ${BENTO_BOARD}, which has no board or cover`);
  const nodes = t.build("open").nodes.map((n) => ({
    id: n.id,
    label: n.data.label ?? n.id,
    kind: n.type === "image-input" ? ("photo" as const) : n.type === "output" ? ("film" as const) : ("step" as const),
  }));
  const photo = nodes.find((n) => n.kind === "photo");
  const film = nodes.find((n) => n.kind === "film");
  const steps = nodes.filter((n) => n.kind === "step");
  if (!photo || !film || steps.length !== 2) throw new Error(`Home's canvas tile draws a photo, two steps and a film; ${BENTO_BOARD} has another shape`);
  return {
    photo: { ...photo, thumb: made("/previews/home/board-photo.webp") },
    steps,
    film: { ...film, thumb: made("/previews/home/board-film.webp") },
  };
}

/** Everything the first screen shows, computed once per build with the rest of Home (homeShowcase). */
export async function homeBento(f: BentoFacts) {
  const threeD = presetCover(BENTO_CLIPS.threeD);
  if (!threeD?.video || BENTO_REEL.some((id) => !presetCover(id)?.video)) throw new Error("Home's first screen names a clip without its published media");
  const demo = await readDemo();
  const frame = (id: string) => {
    const fr = demo.frames[id];
    if (!fr?.small || !fr.mid || !fr.src || !fr.webp) throw new Error(`Home's live tile names frame ${id}, which the Live edit session doesn't have`);
    return { avif: [{ src: fr.small, w: 512 }, { src: fr.mid, w: 768 }, { src: fr.src, w: 1280 }], fallback: fr.webp } satisfies BentoPicture;
  };
  // The video tile's reel: each clip in the widths it comes in, and the first one's first frame as the poster, in its
  // widths (and a JPEG at 1024 px for a browser without AVIF).
  const reel = (): BentoReelTile => {
    const first = HOME_BENTO_REEL[0];
    return {
      id: BENTO_REEL_ID,
      poster: {
        avif: widthsOf(first.name, "avif").map((w) => ({ src: made(`/previews/home/${first.name}-${w}.avif`), w })),
        fallback: made(`/previews/home/${first.name}-${BENTO_CLIP_FALLBACK}.jpg`),
        tint: HOME_BENTO_TINT[first.name],
      },
      reel: HOME_BENTO_REEL.map((c) => {
        const widths = widthsOf(c.name, "mp4");
        if (!widths.length) throw new Error(`Home's video tile names ${c.name}, which scripts/gen-home-bento.mjs didn't make`);
        return { id: c.id, position: c.position, hold: c.hold, fade: c.fade, clips: widths.map((w) => ({ src: made(`/previews/home/${c.name}-${w}.mp4`), w })) };
      }),
    };
  };
  // The Genjutsu tile's reel: each clip in its three widths; the first one's first frame as the poster, and the same
  // clip at rest half way across as the reduced-motion still (a JPEG of the poster for a browser without AVIF).
  const genjutsu = (): BentoWipeTile => {
    const first = HOME_BENTO_GENJUTSU[0].name;
    const fallback = made(`/previews/home/${first}-${BENTO_CLIP_FALLBACK}.jpg`);
    return {
      id: BENTO_GENJUTSU.id,
      poster: { avif: BENTO_GENJUTSU_WIDTHS.map((w) => ({ src: made(`/previews/home/${first}-${w}.avif`), w })), fallback, tint: HOME_BENTO_TINT[first] },
      still: { avif: BENTO_GENJUTSU_WIDTHS.map((w) => ({ src: made(`/previews/home/${first}-split-${w}.avif`), w })), fallback, tint: HOME_BENTO_TINT[first] },
      reel: HOME_BENTO_GENJUTSU.map((c) => ({
        id: c.name,
        hold: BENTO_GENJUTSU.hold,
        fade: BENTO_GENJUTSU.fade,
        clips: BENTO_GENJUTSU_WIDTHS.map((w) => ({ src: made(`/previews/home/${c.name}-${w}.mp4`), w })),
      })),
      wipes: HOME_BENTO_GENJUTSU.map(({ labels, fps, fadeFrom, line }) => ({ labels, fps, fadeFrom, line })),
    };
  };
  // A moving tile: its poster (the clip's first frame) and its clip, each in every width it comes in; for a browser
  // without AVIF, the 1024 px clip's first frame as a JPEG.
  const moving = (id: string, name: string, widths: readonly number[] = BENTO_CLIP_WIDTHS): BentoMovingTile => ({
    id,
    poster: {
      avif: widths.map((w) => ({ src: made(`/previews/home/${name}-${w}.avif`), w })),
      fallback: made(`/previews/home/${name}-${BENTO_CLIP_FALLBACK}.jpg`),
      tint: HOME_BENTO_TINT[name],
    },
    clips: widths.map((w) => ({ src: made(`/previews/home/${name}-${w}.mp4`), w })),
  });
  // The Audio tile's photos, each in its widths, and a JPEG at 1024 px for a browser without AVIF.
  const voicePic = (name: string): BentoPicture => ({
    avif: BENTO_VOICE.widths.map((w) => ({ src: made(`/previews/home/${name}-${w}.avif`), w })),
    fallback: made(`/previews/home/${name}-${BENTO_CLIP_FALLBACK}.jpg`),
  });
  const tile = (key: BentoKey, kind: string, meta: string, title: string, cta: string): BentoTile => ({
    key,
    meta,
    title,
    cta,
    href: BENTO_HREFS[key],
    label: `${kind}: ${title}. ${cta}`,
  });
  // The latest release, by its own name: the model over the title, and the studio it opens on that model. The title
  // here is the one for after its first day (the page is built once; ReleaseTitle says "today" on the day itself).
  const release: BentoTile = {
    key: "release",
    meta: HOME_RELEASE.name,
    title: RELEASE_TITLES.later,
    cta: "Try it now",
    href: BENTO_HREFS.release,
    label: `${HOME_RELEASE.name}, new: try it now`,
  };
  return {
    facts: f,
    tiles: {
      video: tile("video", "Video", `${f.videoModels} video models · ${f.motions} camera moves`, "Films, ads and shorts", "Open Cinema Studio"),
      release,
      audio: tile("audio", "Audio", `Audio · ${f.soundTools} sound tools`, "Studio-clean voice", "Open Clean up voice"),
      threeD: tile("threeD", "3D", `3D · ${f.models3d} models`, "Spin it in 3D", "Open Image to 3D"),
      genjutsu: tile("genjutsu", "Genjutsu", "Genjutsu", "Swap anyone in a clip", "Open Genjutsu"),
      numbers: {
        ...tile("numbers", "Studios", "", "Every studio, app and effect", "All studios"),
        // Read as the numbers it shows, and say that it opens the list of studios in place.
        label: `${f.studios} studios, ${f.apps} apps, ${f.effectsAndMotions} effects and camera moves. Opens all studios`,
      },
      live: tile("live", "Live edit", "Live edit", "Type it, watch it change", "Open Live edit"),
      canvas: tile("canvas", "Canvas", "Canvas", "Chain any step, end to end", "Open Canvas"),
    },
    video: reel(),
    release: { ...BENTO_RELEASE, released: HOME_RELEASE.released, slides: await releaseSlides() },
    threeD: moving(BENTO_CLIPS.threeD, BENTO_CLIP_FILES.threeD),
    genjutsu: genjutsu(),
    voice: { room: voicePic(BENTO_VOICE.room), quiet: voicePic(BENTO_VOICE.quiet) },
    live: { frames: BENTO_LIVE_STEPS.map(frame), phrases: livePhrases(demo) },
    canvas: board(),
    numbers: [
      { value: f.studios, label: "studios" },
      { value: f.apps, label: "apps" },
      { value: f.effectsAndMotions, label: "effects & moves" },
    ],
    numbersLine: `Plus ${f.openModels} open models for pictures, video, sound and 3D.`,
  };
}

export type HomeBento = Awaited<ReturnType<typeof homeBento>>;