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.

MOSS N7-1: the layout library every surface draws boxes with

Claude9a428759fe26
diff --git a/src/lib/catalog/types.ts b/src/lib/catalog/types.ts
index 095847ac..4d65417a 100644
--- a/src/lib/catalog/types.ts
+++ b/src/lib/catalog/types.ts
@@ -430,7 +430,9 @@ export interface FieldMap {
numFrames?: { key: string; fps: number; min?: number; max?: number; step?: number; multiple?: number; fromDuration?: boolean };
/**
* Width and height fields (Replicate's Z-Image LoRA model): the aspect ratio's shape with the area of a
- * `base`×`base` square (1024 when unset), in multiples of `multiple`, neither side above `max`.
+ * `base`×`base` square (1024 when unset), in multiples of `multiple`, neither side above `max` nor below `min`
+ * (fal's MOSS-Image takes 512–2048). A size the request asks for (params.extra[key] {width, height}: a layout's
+ * canvas) is kept on the same grid and within the same limits, as the API would.
*/
size?: {
width?: string;
@@ -439,6 +441,7 @@ export interface FieldMap {
key?: string;
multiple: number;
max?: number;
+ min?: number;
base?: number;
/** Exact sizes per resolution tier and aspect ratio (Fuobe Soul's table); the computed size where it has none. */
table?: Record<string, Record<string, [number, number]>>;
@@ -451,6 +454,11 @@ export interface FieldMap {
ranges?: Record<string, { min?: number; max?: number; integer?: boolean }>;
/** The most images this route's image list takes, when fewer than the model's options.maxImages (another route takes more). */
maxImages?: number;
+ /**
+ * The API's own prompt expansion switch (fal's enable_prompt_expansion): sent false whenever the request carries a
+ * layout (params.extra.layout), which expansion would rewrite; a plain prompt leaves the API's default.
+ */
+ promptExpansion?: string;
/** An empty prompt is left out rather than sent as "" (APIs that read a missing prompt as "no words": Bria Expand). */
omitEmptyPrompt?: boolean;
/** params.extra keys this route has no field for (a model-wide control only its other routes read): left out. */
@@ -534,8 +542,19 @@ export interface ModelOptions {
languages?: string[];
/** Numeric settings sent as-is (stability, similarity, speed, exaggeration…). */
sliders?: { param: string; label: string; min: number; max: number; step: number; default: number }[];
- /** Extra enum settings sent as-is (Cinema Studio's genre, lens, light, grade, camera move…); `labels` names values that read badly (FLUX Upscale's creativity 0 / 1). */
- controls?: { param: string; label: string; values: (string | number)[]; default?: string | number; labels?: Record<string, string> }[];
+ /**
+ * Extra enum settings sent as-is (Cinema Studio's genre, lens, light, grade, camera move…); `labels` names values that read badly
+ * (FLUX Upscale's creativity 0 / 1). `sets`: a value that stands for several settings (MOSS-Image's Quality: 50 steps, CFG 3,
+ * euler), which the plan writes into params.extra in its place; a setting the request gives itself wins.
+ */
+ controls?: {
+ param: string;
+ label: string;
+ values: (string | number)[];
+ default?: string | number;
+ labels?: Record<string, string>;
+ sets?: Record<string, Record<string, string | number>>;
+ }[];
/** Images it needs before it can run (try-on: the person and the garment). */
minImages?: number;
/** What each image is, in order ("person photo", "garment"): blockers and slot labels. */
@@ -581,8 +600,16 @@ export interface ModelOptions {
fpsTargets?: number[];
/** Slow-motion factors it takes (params.slowdown; 1 = the clip's speed). */
slowdowns?: number[];
+ /**
+ * How it reads a layout (src/lib/layout-prompt.ts): MOSS-Image's JSON boxes, FLUX.3's layout table, Ideogram 4's JSON
+ * prompt or Bria FIBO 2's VGL location words. Unset: the layout goes in as plain sentences.
+ */
+ layout?: LayoutFormat;
}
+/** The layout forms models read (ModelOptions.layout). */
+export type LayoutFormat = "moss" | "flux3" | "ideogram" | "vgl";
+
export interface Pricing {
unit: "image" | "second" | "video" | "request" | "megapixel" | "minute";
usd: number;
@@ -601,6 +628,8 @@ export interface Pricing {
factors?: { param: string; values: Record<string, number> }[];
/** The price per unit when the run carries LoRAs and goes to the route's LoRA endpoint (fal's /lora variants cost more). */
withLoras?: number;
+ /** The LoRA endpoint publishes no price yet (fal's MOSS-Image /lora): a run with LoRAs is priced as unknown, never as free. */
+ lorasUnknown?: true;
/** withLoras at each resolution, when it differs (Fuobe Cinema's LTX-2.3 bills per megapixel of video). */
withLorasByResolution?: Record<string, number>;
/**
diff --git a/src/lib/layout-prompt.ts b/src/lib/layout-prompt.ts
new file mode 100644
index 00000000..4a13d75d
--- /dev/null
+++ b/src/lib/layout-prompt.ts
@@ -0,0 +1,664 @@
+/*
+ * Layouts: boxes on a canvas, one row per box (an object, a character or exact words), drawn once and read by each
+ * model in its own form (docs/research/new-models-7.md §4.2-§4.3). Pure: no I/O and no catalog import, so the
+ * browser, the server, Assist, MCP and the CLI share it.
+ *
+ * A draft keeps its boxes as 0-1 fractions of the canvas, so it stays valid when the render size changes. A compiler
+ * turns it into the target model's prompt at the exact size the model renders:
+ * - MOSS-Image (compileMoss): its "JSON Layout" prompt, pixel boxes from the canvas centre, y before x;
+ * - FLUX.3 (compileFlux3): a caption citing <obj_N>, then the 0-1000 layout table ComfyUI's Flux3LayoutPrompt writes;
+ * - Ideogram 4.x (compileIdeogram): the JSON prompt ComfyUI's BuildJsonPromptIdeogram builds;
+ * - Bria FIBO 2 (compileVgl): VGL objects and text placed with words from a thirds grid (it reads no coordinates);
+ * - every other model (compileWords): plain sentences.
+ * parseLayout reads what people bring from ComfyUI (LinXce drafts, compiled MOSS prompts, legacy scene prompts,
+ * CreateBoundingBoxes elements) and Fuobe's own drafts; validateLayout says what stops a layout from running.
+ */
+import type { LayoutFormat } from "./catalog/types";
+
+export type { LayoutFormat };
+export type LayoutLabel = "object" | "character" | "text";
+export type LayoutCanvas = "single" | "multi_panel" | "collage";
+/** [x0, y0, x1, y1] as 0-1 fractions of the canvas, from its top-left corner. */
+export type LayoutBox = [number, number, number, number];
+
+export interface LayoutItem {
+ label: LayoutLabel;
+ /** What it looks like. */
+ caption: string;
+ /** The exact words of a text box. */
+ text?: string;
+ box: LayoutBox;
+ tags?: string[];
+ palette?: string[];
+ /** The editor's colour for the box (#rrggbb); no model sees it. */
+ color?: string;
+}
+
+export interface LayoutSummary {
+ /** The whole picture in words (the composer's prompt). */
+ caption: string;
+ meta: string[];
+ canvas: LayoutCanvas;
+ palette?: string[];
+}
+
+export interface LayoutDraft {
+ summary: LayoutSummary;
+ items: LayoutItem[];
+ /** The shape the boxes were drawn on ("3:2"), when known. */
+ aspect?: string;
+}
+
+/** Where a request carries its draft: params.extra.layout (never sent to an API as a field). */
+export const LAYOUT_PARAM = "layout";
+/** MOSS-Image's coordinate space and box order (USERGUIDE "Layout Prompts (Experimental)"). */
+export const MOSS_COORD_SPACE = "abs_centralized";
+export const MOSS_BBOX_FORMAT = "yxyx";
+/** Canvas sides MOSS-Image, fal and MOSI take: multiples of 16; past 2048 px a side or 4 MP is outside what it was made for. */
+export const LAYOUT_MULTIPLE = 16;
+export const LAYOUT_MAX_SIDE = 2048;
+export const LAYOUT_MAX_PIXELS = 4 * 1024 * 1024;
+/** Prompt lengths the hosts take, in characters (MOSI's API reference; Ideogram 4.5 on fal). */
+export const LAYOUT_PROMPT_LIMITS: Partial<Record<LayoutTarget, number>> = { mosi: 4000, ideogram: 10000 };
+/** The formats whose models read coordinates (Bria reads location words, the rest plain sentences). */
+export const BOX_READING_FORMATS: readonly LayoutFormat[] = ["moss", "flux3", "ideogram"];
+
+const LABELS: readonly LayoutLabel[] = ["object", "character", "text"];
+const CANVASES: readonly LayoutCanvas[] = ["single", "multi_panel", "collage"];
+const HEX = /^#[0-9a-f]{6}$/i;
+
+// ---------------------------------------------------------------------------
+// Small helpers
+
+const isObj = (v: unknown): v is Record<string, unknown> => !!v && typeof v === "object" && !Array.isArray(v);
+const str = (v: unknown): string => (typeof v === "string" ? v : "");
+const strings = (v: unknown): string[] => (Array.isArray(v) ? v.filter((x): x is string => typeof x === "string" && x.trim() !== "") : []);
+const num = (v: unknown): number => (typeof v === "number" ? v : typeof v === "string" && v.trim() !== "" ? Number(v) : NaN);
+
+/** Python's round(v, 9): float noise from fractions and pixel maths collapses before floor / ceil (LinXce's collapseNoise). */
+const round9 = (v: number) => Math.round(v * 1e9) / 1e9;
+
+/** Python's round(): halves go to the even neighbour (CreateBoundingBoxes' 0-1000 grid). */
+function pyRound(v: number): number {
+ const r = Math.round(v);
+ return Math.abs(v % 1) === 0.5 && r % 2 !== 0 ? r - 1 : r;
+}
+
+/** json.dumps(value, ensure_ascii=False) with Python's default separators (", " and ": "), as ComfyUI's nodes write it. */
+function pyJson(v: unknown): string {
+ if (Array.isArray(v)) return `[${v.map(pyJson).join(", ")}]`;
+ if (isObj(v)) return `{${Object.entries(v).map(([k, x]) => `${JSON.stringify(k)}: ${pyJson(x)}`).join(", ")}}`;
+ return JSON.stringify(v) ?? "null";
+}
+
+/** A box with its edges in order (x0 ≤ x1, y0 ≤ y1). */
+function ordered(b: LayoutBox): LayoutBox {
+ return [Math.min(b[0], b[2]), Math.min(b[1], b[3]), Math.max(b[0], b[2]), Math.max(b[1], b[3])];
+}
+
+function gcd(a: number, b: number): number {
+ return b ? gcd(b, a % b) : a;
+}
+
+/** "1536 × 1024" → "3:2". */
+export function aspectOf(width: number, height: number): string | undefined {
+ if (!(Number.isInteger(width) && Number.isInteger(height) && width > 0 && height > 0)) return undefined;
+ const g = gcd(width, height);
+ return `${width / g}:${height / g}`;
+}
+
+const sentence = (s: string) => {
+ const t = s.trim();
+ return !t || /[.!?。!?]$/.test(t) ? t : `${t}.`;
+};
+
+// ---------------------------------------------------------------------------
+// MOSS-Image
+
+/**
+ * A box in MOSS-Image's pixels: the edges enclosing it (top / left floored, bottom / right ceiled after Python's
+ * round(v, 9)), with the canvas centre (W // 2, H // 2) as the origin, y before x: the maths of OpenMOSS's reference
+ * compiler and LinXce's editor.
+ */
+export function mossBox(box: LayoutBox, width: number, height: number): [number, number, number, number] {
+ const [x0, y0, x1, y1] = ordered(box);
+ const hw = Math.floor(width / 2);
+ const hh = Math.floor(height / 2);
+ return [Math.floor(round9(y0 * height)) - hh, Math.floor(round9(x0 * width)) - hw, Math.ceil(round9(y1 * height)) - hh, Math.ceil(round9(x1 * width)) - hw];
+}
+
+/**
+ * The draft as MOSS-Image's JSON Layout prompt at width × height: compact JSON with every character as written (no
+ * ASCII escaping), keys in MOSS's documented order (summary: meta, canvas, caption, palette; item: label, caption, text,
+ * bbox, tags, palette), the editor colour left out. With no boxes it is the summary caption alone, trimmed.
+ */
+export function compileMoss(draft: LayoutDraft, width: number, height: number): string {
+ const s = draft.summary;
+ if (!draft.items.length) return s.caption.trim();
+ const summary: Record<string, unknown> = { meta: s.meta ?? [], canvas: s.canvas ?? "single", caption: s.caption };
+ if (s.palette?.length) summary.palette = s.palette;
+ const items = draft.items.map((it) => {
+ const out: Record<string, unknown> = { label: it.label, caption: it.caption };
+ if (it.label === "text") out.text = it.text ?? "";
+ out.bbox = mossBox(it.box, width, height);
+ if (it.tags?.length) out.tags = it.tags;
+ if (it.palette?.length) out.palette = it.palette;
+ return out;
+ });
+ return JSON.stringify({ summary, items, bbox_coord_space: MOSS_COORD_SPACE, bbox_format: MOSS_BBOX_FORMAT });
+}
+
+/** A summary read from any of the shapes below, in any key order. */
+function readSummary(v: unknown): LayoutSummary {
+ const s = isObj(v) ? v : {};
+ const canvas = CANVASES.find((c) => c === s.canvas) ?? "single";
+ const palette = strings(s.palette);
+ return { caption: str(s.caption), meta: strings(s.meta), canvas, ...(palette.length && { palette }) };
+}
+
+/** An item's label, caption, words, tags and palettes read in any key order; `box` is added by the caller. */
+function readItem(v: Record<string, unknown>): Omit<LayoutItem, "box"> {
+ const raw = str(v.label).trim().toLowerCase();
+ const label: LayoutLabel = LABELS.find((l) => l === raw) ?? (raw === "person" ? "character" : "object");
+ const tags = strings(v.tags);
+ const palette = strings(v.palette);
+ const color = str(v.color);
+ const text = str(v.text);
+ return {
+ label,
+ caption: str(v.caption),
+ ...(label === "text" || text ? { text } : {}),
+ ...(tags.length && { tags }),
+ ...(palette.length && { palette }),
+ ...(HEX.test(color) && { color }),
+ };
+}
+
+const finiteBox = (v: unknown): number[] | null => {
+ if (!Array.isArray(v) || v.length !== 4) return null;
+ const n = v.map(num);
+ return n.every(Number.isFinite) ? n : null;
+};
+
+/** Pixel boxes → fractions: MOSS's centred yxyx, or a LinXce draft's top-left xyxy. */
+function fromPixels(items: unknown[], width: number, height: number, centred: boolean): LayoutItem[] {
+ const hw = Math.floor(width / 2);
+ const hh = Math.floor(height / 2);
+ return items.flatMap((v) => {
+ if (!isObj(v)) return [];
+ const b = finiteBox(v.bbox);
+ if (!b) return [];
+ const box: LayoutBox = centred
+ ? [(b[1] + hw) / width, (b[0] + hh) / height, (b[3] + hw) / width, (b[2] + hh) / height]
+ : [b[0] / width, b[1] / height, b[2] / width, b[3] / height];
+ return [{ ...readItem(v), box: ordered(box) }];
+ });
+}
+
+/** Parse JSON, then again with Stable Diffusion's \( \) escapes made legal (LinXce's repair); undefined when it isn't JSON. */
+function parseLoose(text: string): unknown {
+ try {
+ return JSON.parse(text);
+ } catch {
+ try {
+ return JSON.parse(text.replace(/\\(u[0-9a-fA-F]{4}|["\\/bfnrt])|\\/g, (m, ok: string | undefined) => (ok ? m : "\\\\")));
+ } catch {
+ return undefined;
+ }
+ }
+}
+
+/**
+ * MOSS-Image's prompt back to a draft at the size it was rendered: blind to key order (fal's actual_prompt puts bbox
+ * before text) and to the root's bbox fields when they're missing. Plain words come back as a caption with no boxes
+ * (what compileMoss makes of a draft with none); null for JSON that isn't a layout.
+ */
+export function decodeMoss(prompt: string, width: number, height: number): LayoutDraft | null {
+ const text = prompt.trim();
+ if (!/^[[{]/.test(text)) return { summary: { caption: text, meta: [], canvas: "single" }, items: [], ...aspectField(width, height) };
+ const v = parseLoose(text);
+ if (!isObj(v) || !(Array.isArray(v.items) || isObj(v.summary))) return null;
+ const centred = v.bbox_coord_space !== "abs" && v.bbox_format !== "xyxy";
+ return { summary: readSummary(v.summary), items: fromPixels(Array.isArray(v.items) ? v.items : [], width, height, centred), ...aspectField(width, height) };
+}
+
+const aspectField = (width: number, height: number) => {
+ const aspect = aspectOf(width, height);
+ return aspect ? { aspect } : {};
+};
+
+// ---------------------------------------------------------------------------
+// Importing layouts
+
+export type LayoutSource = "fuobe" | "linxce" | "compiled" | "scene" | "elements";
+
+export interface ParsedLayout {
+ draft: LayoutDraft;
+ /** The canvas the import was drawn on: its own size when it has one, else the canvas given. */
+ width: number;
+ height: number;
+ from: LayoutSource;
+}
+
+/** Every balanced {...} (or [...]) in some prose, in order, skipping braces inside strings. */
+function* balanced(text: string, open: "{" | "["): Generator<string> {
+ const close = open === "{" ? "}" : "]";
+ for (let start = text.indexOf(open); start !== -1; start = text.indexOf(open, start + 1)) {
+ let depth = 0;
+ let inString = false;
+ let escaped = false;
+ for (let i = start; i < text.length; i++) {
+ const c = text[i];
+ if (inString) {
+ if (escaped) escaped = false;
+ else if (c === "\\") escaped = true;
+ else if (c === '"') inString = false;
+ continue;
+ }
+ if (c === '"') inString = true;
+ else if (c === open) depth++;
+ else if (c === close && --depth === 0) {
+ yield text.slice(start, i + 1);
+ break;
+ }
+ }
+ }
+}
+
+const looksLikeLayout = (v: unknown): v is Record<string, unknown> => isObj(v) && ("summary" in v || "items" in v || "elements" in v);
+
+/** ComfyUI CreateBoundingBoxes elements: {type: "obj" | "text", bbox: 0-1000 yxyx, text?, desc, color_palette?}. */
+const isElement = (v: unknown): boolean =>
+ isObj(v) && !!finiteBox(v.bbox) && ("desc" in v || v.type === "obj" || v.type === "text") && !("identity" in v || "shape" in v || "note" in v);
+const looksLikeElements = (v: unknown): v is unknown[] => Array.isArray(v) && v.length > 0 && v.every(isElement);
+
+/** The JSON in some text: the text itself (a string when it was encoded twice), else the first layout-shaped value in it. */
+function parseOnce(text: string): unknown {
+ const direct = parseLoose(text);
+ if (typeof direct === "string" || isObj(direct) || looksLikeElements(direct)) return direct;
+ for (const candidate of balanced(text, "{")) {
+ const v = parseLoose(candidate);
+ if (looksLikeLayout(v) || (isObj(v) && ("width" in v || "height" in v))) return v;
+ }
+ for (const candidate of balanced(text, "[")) {
+ const v = parseLoose(candidate);
+ if (looksLikeElements(v)) return v;
+ }
+ return null;
+}
+
+/** Scale boxes around the centre so an import that spills past the canvas fits it (LinXce's fitDraftToCanvas). */
+function fitToCanvas(items: LayoutItem[]): LayoutItem[] {
+ let hw = 0;
+ let hh = 0;
+ for (const { box } of items) {
+ hw = Math.max(hw, Math.abs(box[0] - 0.5), Math.abs(box[2] - 0.5));
+ hh = Math.max(hh, Math.abs(box[1] - 0.5), Math.abs(box[3] - 0.5));
+ }
+ if (!hw || !hh || (hw <= 0.5 + 1e-9 && hh <= 0.5 + 1e-9)) return items;
+ const k = Math.min(0.5 / hw, 0.5 / hh);
+ const at = (v: number) => (v - 0.5) * k + 0.5;
+ return items.map((it) => ({ ...it, box: [at(it.box[0]), at(it.box[1]), at(it.box[2]), at(it.box[3])] }));
+}
+
+const SCENE_LABELS: Record<string, LayoutLabel> = { panel: "object", obj: "object", object: "object", shape: "object", character: "character", person: "character", text: "text" };
+
+/**
+ * A legacy MOSS scene prompt (scene_type / gen_brief / elements / must_not): each element's identity, shape and note
+ * become its caption, a text element without words becomes an object, boxes are centred yxyx unless the prompt says "abs".
+ */
+function fromScene(v: Record<string, unknown>, width: number, height: number): LayoutDraft {
+ const centred = v.bbox_coord_space !== "abs";
+ const items: LayoutItem[] = [];
+ for (const el of Array.isArray(v.elements) ? v.elements : []) {
+ if (!isObj(el)) continue;
+ const b = finiteBox(el.bbox);
+ if (!b) continue;
+ let label = SCENE_LABELS[str(el.type).trim().toLowerCase()] ?? "object";
+ const caption = ["identity", "shape", "note"].map((k) => str(el[k]).trim()).filter(Boolean).join(" ");
+ const words = str(el.text).trim() || str(el.identity).trim();
+ if (label === "text" && !words) label = "object";
+ // LinXce adds half the canvas (not its floor) to a centred box: the same here.
+ const box: LayoutBox = centred
+ ? [(b[1] + width / 2) / width, (b[0] + height / 2) / height, (b[3] + width / 2) / width, (b[2] + height / 2) / height]
+ : [b[0] / width, b[1] / height, b[2] / width, b[3] / height];
+ items.push({ label, caption, ...(label === "text" && { text: words }), box: ordered(box) });
+ }
+ const avoid = (Array.isArray(v.must_not) ? v.must_not : [v.must_not])
+ .map((x) => str(x).trim())
+ .map((x) => (x.slice(0, 4).toLowerCase() === "not " ? x.slice(4).trim() : x))
+ .filter(Boolean)
+ .join("; ");
+ let caption = [str(v.scene_type).trim(), str(v.gen_brief).trim()].filter(Boolean).join(". ");
+ if (avoid) caption = `${caption} Avoid: ${avoid}`.trim();
+ const palette = strings(v.palette);
+ return { summary: { caption, meta: [], canvas: "single", ...(palette.length && { palette }) }, items };
+}
+
+function fromElements(elements: unknown[]): LayoutDraft {
+ const items = elements.flatMap((el): LayoutItem[] => {
+ if (!isObj(el)) return [];
+ const b = finiteBox(el.bbox)!;
+ const text = el.type === "text";
+ const palette = strings(el.color_palette);
+ return [{ label: text ? "text" : "object", caption: str(el.desc), ...(text && { text: str(el.text) }), box: ordered([b[1] / 1000, b[0] / 1000, b[3] / 1000, b[2] / 1000]), ...(palette.length && { palette }) }];
+ });
+ return { summary: { caption: "", meta: [], canvas: "single" }, items };
+}
+
+/** A Fuobe draft (fractional `box` on every item), else null. */
+export function readLayout(value: unknown): LayoutDraft | null {
+ if (!isObj(value) || !Array.isArray(value.items)) return null;
+ const items: LayoutItem[] = [];
+ for (const v of value.items) {
+ const b = isObj(v) ? finiteBox(v.box) : null;
+ if (!b || !isObj(v)) return null;
+ items.push({ ...readItem(v), box: ordered(b as LayoutBox) });
+ }
+ const aspect = str(value.aspect);
+ return { summary: readSummary(value.summary), items, ...(/^\d+(\.\d+)?:\d+(\.\d+)?$/.test(aspect) && { aspect }) };
+}
+
+const sideOf = (v: unknown, fallback: number) => {
+ const n = num(v);
+ return Number.isFinite(n) && n > 0 ? Math.round(n) : fallback;
+};
+
+function fromLayoutValue(v: Record<string, unknown> | unknown[], width: number, height: number): ParsedLayout | null {
+ if (Array.isArray(v)) return { draft: fromElements(v), width, height, from: "elements" };
+ const w = sideOf(v.width, width);
+ const h = sideOf(v.height, height);
+ if (Array.isArray(v.elements)) {
+ const cbb = looksLikeElements(v.elements) && !("scene_type" in v || "gen_brief" in v);
+ return cbb ? { draft: fromElements(v.elements), width: w, height: h, from: "elements" } : { draft: fromScene(v, w, h), width: w, height: h, from: "scene" };
+ }
+ const own = readLayout(v);
+ if (own) return { draft: own, width: w, height: h, from: "fuobe" };
+ const compiled = v.bbox_coord_space === MOSS_COORD_SPACE || v.bbox_format === MOSS_BBOX_FORMAT;
+ const items = fromPixels(Array.isArray(v.items) ? v.items : [], w, h, compiled);
+ return { draft: { summary: readSummary(v.summary), items }, width: w, height: h, from: compiled ? "compiled" : "linxce" };
+}
+
+/**
+ * A layout from what someone pastes or imports, as tolerant as LinXce's browser editor: prose around the JSON, a JSON
+ * string encoded twice, {layout | draft | source} envelopes (up to 4 levels), \( escapes; LinXce drafts (pixel boxes,
+ * "abs" / "xyxy", their own width and height), compiled MOSS prompts (decoded on their own size, else `canvas`),
+ * legacy scene prompts, CreateBoundingBoxes elements (0-1000, yxyx) and Fuobe's own drafts. Boxes that spill past
+ * the canvas are scaled to fit. Null when the text holds no layout.
+ */
+export function parseLayout(input: string | object, canvas: { width: number; height: number } = { width: 1024, height: 1024 }): ParsedLayout | null {
+ let value: unknown = typeof input === "string" ? input.trim() : input;
+ for (let depth = 0; depth < 4 && value; depth++) {
+ if (typeof value === "string") {
+ value = parseOnce(value);
+ if (typeof value === "string") {
+ value = value.trim();
+ continue;
+ }
+ }
+ if (looksLikeElements(value) || looksLikeLayout(value)) {
+ const parsed = fromLayoutValue(value as Record<string, unknown> | unknown[], canvas.width, canvas.height);
+ if (!parsed) return null;
+ const items = fitToCanvas(parsed.draft.items);
+ const aspect = parsed.draft.aspect ?? aspectOf(parsed.width, parsed.height);
+ return { ...parsed, draft: { ...parsed.draft, items, ...(aspect && { aspect }) } };
+ }
+ if (!isObj(value)) return null;
+ const v: Record<string, unknown> = value;
+ const wrapped = ["layout", "draft", "source"].map((k) => v[k]).find((x) => (typeof x === "string" && x.trim()) || isObj(x));
+ if (!wrapped) return null;
+ value = typeof wrapped === "string" ? wrapped.trim() : wrapped;
+ }
+ return null;
+}
+
+// ---------------------------------------------------------------------------
+// Checks
+
+export type LayoutTarget = LayoutFormat | "words" | "mosi";
+
+export interface LayoutIssue {
+ level: "error" | "warning";
+ message: string;
+ /** The box it is about (0-based), when it is about one. */
+ item?: number;
+}
+
+const EPS = 1e-9;
+
+/**
+ * What stops the draft from running at width × height on `target` (errors), and what may make the model drift
+ * (warnings): sides in multiples of 16, words in every text box, boxes inside the picture; past 2048 px a side or
+ * 4 MP; MOSI's 4,000 characters (and Ideogram's 10,000) for the compiled prompt.
+ */
+export function validateLayout(draft: LayoutDraft, width: number, height: number, target: LayoutTarget = "moss"): LayoutIssue[] {
+ const issues: LayoutIssue[] = [];
+ const side = (n: number) => Number.isInteger(n) && n > 0 && n % LAYOUT_MULTIPLE === 0;
+ if (!side(width) || !side(height)) issues.push({ level: "error", message: `Width and height must be multiples of ${LAYOUT_MULTIPLE} px (this canvas is ${width} × ${height})` });
+ draft.items.forEach((it, i) => {
+ const n = i + 1;
+ const [x0, y0, x1, y1] = it.box;
+ if (it.label === "text" && !it.text?.trim()) issues.push({ level: "error", message: `Type the words for box ${n}`, item: i });
+ if (!it.box.every(Number.isFinite) || x1 - x0 <= EPS || y1 - y0 <= EPS) issues.push({ level: "error", message: `Box ${n} has no size: draw it again`, item: i });
+ else if (x0 < -EPS || y0 < -EPS || x1 > 1 + EPS || y1 > 1 + EPS) issues.push({ level: "error", message: `Box ${n} goes past the edge of the picture`, item: i });
+ });
+ if (Math.max(width, height) > LAYOUT_MAX_SIDE || width * height > LAYOUT_MAX_PIXELS) {
+ issues.push({ level: "warning", message: `${width} × ${height} is past what MOSS-Image was made for (${LAYOUT_MAX_SIDE} px a side, 4 megapixels): the boxes may drift` });
+ }
+ const limit = LAYOUT_PROMPT_LIMITS[target];
+ if (limit && !issues.some((x) => x.level === "error")) {
+ const compiled = target === "ideogram" ? compileIdeogram(draft) : compileMoss(draft, width, height);
+ if (compiled.length > limit) {
+ const host = target === "mosi" ? "MOSI" : "Ideogram 4.5";
+ issues.push({ level: "error", message: `${host} takes prompts up to ${limit.toLocaleString("en-US")} characters; this layout is ${compiled.length.toLocaleString("en-US")}` });
+ }
+ }
+ return issues;
+}
+
+// ---------------------------------------------------------------------------
+// Other models
+
+/** A box on ComfyUI's 0-1000 grid, y before x, rounded as Python rounds (CreateBoundingBoxes' _norm_bbox). */
+export function gridBox(box: LayoutBox): [number, number, number, number] {
+ const g = (v: number) => Math.max(0, Math.min(1000, pyRound(v * 1000)));
+ const [x0, y0, x1, y1] = ordered(box);
+ return [g(y0), g(x0), g(y1), g(x1)];
+}
+
+/** Where a box sits on a thirds grid, in words: "top left", "center", "across the bottom", "filling the frame". */
+export function locationWords(box: LayoutBox): string {
+ const [x0, y0, x1, y1] = ordered(box);
+ const w = x1 - x0;
+ const h = y1 - y0;
+ if (w >= 0.8 && h >= 0.8) return "filling the frame";
+ const cx = (x0 + x1) / 2;
+ const cy = (y0 + y1) / 2;
+ const col = cx < 1 / 3 ? "left" : cx > 2 / 3 ? "right" : "center";
+ const row = cy < 1 / 3 ? "top" : cy > 2 / 3 ? "bottom" : "middle";
+ if (w >= 2 / 3) return `across the ${row}`;
+ if (h >= 2 / 3) return col === "center" ? "down the center" : `down the ${col} side`;
+ if (row === "middle") return col === "center" ? "center" : `middle ${col}`;
+ return `${row} ${col}`;
+}
+
+/** A caption without its closing full stop, to sit inside a sentence. */
+const bare = (s: string) => s.trim().replace(/[.。]+$/, "");
+
+/** What an item is, in a few words: the words of a text box, else its caption. */
+function itemWords(it: LayoutItem): string {
+ if (it.label === "text") return `the words "${it.text ?? ""}"${bare(it.caption) ? ` (${bare(it.caption)})` : ""}`;
+ return bare(it.caption) || (it.label === "character" ? "a person" : "an object");
+}
+
+/**
+ * FLUX.3's layout prompt (ComfyUI's Flux3LayoutPrompt): the caption, a sentence citing each box as <obj_N>, then the
+ * table [{"id": "obj_N", "bbox": [0-1000, y before x], "desc": …}] with a text box's words quoted after its desc.
+ */
+export function compileFlux3(draft: LayoutDraft): string {
+ const caption = draft.summary.caption.trim();
+ if (!draft.items.length) return caption;
+ const rows = draft.items.map((it, i) => {
+ const desc = it.label === "text" && it.text ? `${it.caption} "${it.text}"`.trim() : it.caption;
+ return { id: `obj_${i + 1}`, bbox: gridBox(it.box), desc };
+ });
+ const cites = draft.items.map((it, i) => `<obj_${i + 1}> ${itemWords(it)}`).join(", ");
+ return `${[sentence(caption), `${cites}.`].filter(Boolean).join(" ")} ${pyJson(rows)}`;
+}
+
+/** The share of the canvas a box covers that reads as its background (ComfyUI's Ideogram node takes one apart from the elements). */
+const BACKDROP = 0.9;
+
+/**
+ * Ideogram 4's JSON prompt (ComfyUI's BuildJsonPromptIdeogram): {high_level_description, style_description?,
+ * compositional_deconstruction: {background, elements}}, the elements as CreateBoundingBoxes writes them (0-1000, y
+ * before x). An object box covering the frame is the background; without one, the summary caption describes it.
+ */
+export function compileIdeogram(draft: LayoutDraft, opts: { background?: string; lighting?: string; medium?: string } = {}): string {
+ const s = draft.summary;
+ const backdrop = draft.items.findIndex((it) => it.label !== "text" && it.box[2] - it.box[0] >= BACKDROP && it.box[3] - it.box[1] >= BACKDROP);
+ const elements = draft.items
+ .filter((_, i) => i !== backdrop)
+ .map((it) => {
+ const el: Record<string, unknown> = { type: it.label === "text" ? "text" : "obj", bbox: gridBox(it.box) };
+ if (it.label === "text") el.text = it.text ?? "";
+ el.desc = it.caption;
+ if (it.palette?.length) el.color_palette = it.palette.slice(0, 5).map((c) => c.toUpperCase());
+ return el;
+ });
+ const out: Record<string, unknown> = {};
+ if (s.caption.trim()) out.high_level_description = s.caption;
+ if (s.meta.length || s.palette?.length || opts.lighting || opts.medium) {
+ out.style_description = {
+ aesthetics: s.meta.join(", "),
+ lighting: opts.lighting ?? "",
+ medium: opts.medium ?? "",
+ art_style: "",
+ ...(s.palette?.length && { color_palette: s.palette.map((c) => c.toUpperCase()) }),
+ };
+ }
+ const background = opts.background ?? (backdrop >= 0 ? draft.items[backdrop].caption : s.caption);
+ out.compositional_deconstruction = { background, elements };
+ return JSON.stringify(out);
+}
+
+/** Bria FIBO 2's structured prompt (its fal `vgl` field), the parts a layout fills. */
+export interface VglPrompt {
+ short_description: string;
+ objects: { description: string; location: string; relationship: string; relative_size: string; shape_and_color?: string }[];
+ text_render: { text: string; location: string; size: string; color?: string; appearance_details?: string }[];
+ aesthetics?: { composition: string; color_scheme?: string; mood_atmosphere?: string };
+}
+
+const sizeWords = (box: LayoutBox) => {
+ const area = (box[2] - box[0]) * (box[3] - box[1]);
+ return area < 0.05 ? "small" : area < 0.25 ? "medium" : "large";
+};
+const textSize = (box: LayoutBox) => {
+ const h = box[3] - box[1];
+ return h < 0.08 ? "small" : h < 0.2 ? "medium" : "large";
+};
+const CANVAS_WORDS: Record<LayoutCanvas, string> = { single: "a single picture", multi_panel: "a multi-panel layout", collage: "a collage" };
+
+/** How a box sits next to its nearest neighbour ("to the left of the lit candle"); Bria wants one per object. */
+function relationship(items: LayoutItem[], i: number): string {
+ const me = items[i];
+ const c = (b: LayoutBox) => [(b[0] + b[2]) / 2, (b[1] + b[3]) / 2];
+ const [mx, my] = c(me.box);
+ let best = -1;
+ let dist = Infinity;
+ items.forEach((it, j) => {
+ if (j === i) return;
+ const [x, y] = c(it.box);
+ const d = Math.hypot(x - mx, y - my);
+ if (d < dist) [best, dist] = [j, d];
+ });
+ if (best < 0) return "the only element in the picture";
+ const other = items[best];
+ const [ox, oy] = c(other.box);
+ const name = other.label === "text" ? `the words "${other.text ?? ""}"` : other.caption.trim() || (other.label === "character" ? "the person" : "the other object");
+ const dir = Math.abs(ox - mx) >= Math.abs(oy - my) ? (mx < ox ? "to the left of" : "to the right of") : my < oy ? "above" : "below";
+ return `${dir} ${name}`;
+}
+
+/**
+ * The layout as Bria FIBO 2's VGL: objects and characters with location words from a thirds grid, their size and
+ * their place next to the nearest box; text boxes as text_render with the exact words. Bria reads no coordinates.
+ */
+export function compileVgl(draft: LayoutDraft): VglPrompt {
+ const s = draft.summary;
+ const objects: VglPrompt["objects"] = [];
+ const text_render: VglPrompt["text_render"] = [];
+ draft.items.forEach((it, i) => {
+ if (it.label === "text") {
+ text_render.push({
+ text: it.text ?? "",
+ location: locationWords(it.box),
+ size: textSize(it.box),
+ ...(it.palette?.[0] && { color: it.palette[0] }),
+ ...(it.caption.trim() && { appearance_details: it.caption.trim() }),
+ });
+ return;
+ }
+ objects.push({
+ description: it.caption.trim() || (it.label === "character" ? "a person" : "an object"),
+ location: locationWords(it.box),
+ relationship: relationship(draft.items, i),
+ relative_size: sizeWords(it.box),
+ ...(it.palette?.length && { shape_and_color: `in ${it.palette.join(", ")}` }),
+ });
+ });
+ const aesthetics = {
+ composition: CANVAS_WORDS[s.canvas] ?? CANVAS_WORDS.single,
+ ...(s.palette?.length && { color_scheme: s.palette.join(", ") }),
+ ...(s.meta.length && { mood_atmosphere: s.meta.join(", ") }),
+ };
+ return { short_description: s.caption.trim(), objects, text_render, aesthetics };
+}
+
+/** "At the top left", "In the center", "Across the bottom", "Filling the frame". */
+function placePhrase(loc: string): string {
+ if (/^(across|down|filling)/.test(loc)) return loc[0].toUpperCase() + loc.slice(1);
+ return loc === "center" ? "In the center" : `At the ${loc}`;
+}
+
+/** The layout as plain sentences, for models that read neither boxes nor location fields. */
+export function compileWords(draft: LayoutDraft): string {
+ const s = draft.summary;
+ const parts = [sentence(s.caption)];
+ if (draft.items.length && s.canvas !== "single") parts.push(sentence(`Laid out as ${CANVAS_WORDS[s.canvas]}`));
+ for (const it of draft.items) parts.push(sentence(`${placePhrase(locationWords(it.box))}: ${itemWords(it)}`));
+ if (s.meta.length) parts.push(sentence(`Style: ${s.meta.join(", ")}`));
+ if (s.palette?.length) parts.push(sentence(`Color palette: ${s.palette.join(", ")}`));
+ return parts.filter(Boolean).join(" ");
+}
+
+export interface CompiledLayout {
+ format: LayoutFormat | "words";
+ /** The prompt the model gets. */
+ prompt: string;
+ /** Bria FIBO 2's structured prompt, sent beside the words. */
+ vgl?: VglPrompt;
+}
+
+/** True when the model places boxes by their coordinates (MOSS-Image, FLUX.3, Ideogram 4.x). */
+export const readsBoxes = (model: { options?: { layout?: LayoutFormat } } | undefined): boolean => !!model?.options?.layout && BOX_READING_FORMATS.includes(model.options.layout);
+
+/** The compiler a model reads layouts with (ModelOptions.layout), plain sentences for the rest. */
+export function layoutCompilerFor(model: { options?: { layout?: LayoutFormat } } | undefined): (draft: LayoutDraft, width: number, height: number) => CompiledLayout {
+ switch (model?.options?.layout) {
+ case "moss":
+ return (d, w, h) => ({ format: "moss", prompt: compileMoss(d, w, h) });
+ case "flux3":
+ return (d) => ({ format: "flux3", prompt: compileFlux3(d) });
+ case "ideogram":
+ return (d) => ({ format: "ideogram", prompt: compileIdeogram(d) });
+ case "vgl":
+ return (d) => ({ format: "vgl", prompt: compileWords(d), vgl: compileVgl(d) });
+ default:
+ return (d) => ({ format: "words", prompt: compileWords(d) });
+ }
+}
+
+/** True when a request's params.extra carries a layout draft (an API's own prompt expansion would rewrite it). */
+export const carriesLayout = (extra: Record<string, unknown> | undefined): boolean => isObj(extra?.[LAYOUT_PARAM]);
diff --git a/tests/layout-prompt.test.ts b/tests/layout-prompt.test.ts
new file mode 100644
index 00000000..011bf3aa
--- /dev/null
+++ b/tests/layout-prompt.test.ts
@@ -0,0 +1,355 @@
+import { describe, expect, it } from "vitest";
+import {
+ carriesLayout,
+ compileFlux3,
+ compileIdeogram,
+ compileMoss,
+ compileVgl,
+ compileWords,
+ decodeMoss,
+ gridBox,
+ layoutCompilerFor,
+ locationWords,
+ mossBox,
+ parseLayout,
+ readLayout,
+ readsBoxes,
+ validateLayout,
+ type LayoutBox,
+ type LayoutDraft,
+ type LayoutItem,
+} from "@/lib/layout-prompt";
+
+/** A pixel box [x1, y1, x2, y2] on a width × height canvas as the draft's fractions. */
+const px = (b: [number, number, number, number], w: number, h: number): LayoutBox => [b[0] / w, b[1] / h, b[2] / w, b[3] / h];
+const draft = (items: LayoutItem[], caption = "A poster.", extra: Partial<LayoutDraft["summary"]> = {}): LayoutDraft => ({
+ summary: { caption, meta: [], canvas: "single", ...extra },
+ items,
+});
+const item = (label: LayoutItem["label"], caption: string, box: LayoutBox, more: Partial<LayoutItem> = {}): LayoutItem => ({ label, caption, box, ...more });
+const items = (prompt: string) => (JSON.parse(prompt) as { items: { bbox: number[] }[] }).items.map((i) => i.bbox);
+
+// docs/research/new-models-7.md §4.2: re-derived by hand from LinXce's example draft, OpenMOSS's layout.md and LinXce's tests.
+describe("compileMoss: MOSS-Image's JSON layout", () => {
+ it("places the research's vectors at 1024 × 1024 and 1536 × 1024", () => {
+ const square = draft([
+ item("text", "Large title.", px([128, 96, 896, 224], 1024, 1024), { text: "EXPLORE" }),
+ item("object", "A mountain.", px([128, 288, 896, 640], 1024, 1024)),
+ item("text", "Small print.", px([288, 800, 736, 864], 1024, 1024), { text: "2026" }),
+ ]);
+ expect(items(compileMoss(square, 1024, 1024))).toEqual([
+ [-416, -384, -288, 384],
+ [-224, -384, 128, 384],
+ [288, -224, 352, 224],
+ ]);
+ const cup = draft([item("object", "A cup.", px([176, 224, 800, 864], 1536, 1024))]);
+ expect(items(compileMoss(cup, 1536, 1024))).toEqual([[-288, -592, 352, 32]]);
+ });
+
+ it("keeps LinXce's near-integer and fractional edges (tests/test_layout.py)", () => {
+ const near = parseLayout({
+ width: 1536,
+ height: 1024,
+ bbox_coord_space: "abs",
+ bbox_format: "xyxy",
+ summary: { caption: "A poster." },
+ items: [{ label: "text", caption: "Large title.", text: "EXPLORE", bbox: [927.9999999999, 271.9999999999, 1408.0000000001, 448.0000000001] }],
+ })!;
+ expect(near.from).toBe("linxce");
+ expect([near.width, near.height]).toEqual([1536, 1024]);
+ expect(items(compileMoss(near.draft, 1536, 1024))).toEqual([[-240, 160, -64, 640]]);
+ const frac = parseLayout({
+ width: 1536,
+ height: 1024,
+ bbox_coord_space: "abs",
+ bbox_format: "xyxy",
+ summary: { caption: "A poster." },
+ items: [{ label: "object", caption: "A circle.", bbox: [927.9999999, 271.9999999, 1408.0000001, 448.0000001] }],
+ })!;
+ expect(items(compileMoss(frac.draft, 1536, 1024))).toEqual([[-241, 159, -63, 641]]);
+ });
+
+ it("is the trimmed caption alone with no boxes", () => {
+ expect(compileMoss(draft([], " A quiet harbour at dawn. "), 1024, 1024)).toBe("A quiet harbour at dawn.");
+ });
+
+ it("writes keys in MOSS's documented order and leaves the editor colour out", () => {
+ const d = draft([item("text", "Neon sign.", [0.1, 0.1, 0.5, 0.3], { text: "OPEN", tags: ["neon"], palette: ["#ff0044"], color: "#00ff00" })], "A bar at night.", {
+ meta: ["photo", "night"],
+ palette: ["#101010"],
+ });
+ const s = compileMoss(d, 1024, 1024);
+ expect(s).toBe(
+ '{"summary":{"meta":["photo","night"],"canvas":"single","caption":"A bar at night.","palette":["#101010"]},' +
+ '"items":[{"label":"text","caption":"Neon sign.","text":"OPEN","bbox":[-410,-410,-204,0],"tags":["neon"],"palette":["#ff0044"]}],' +
+ '"bbox_coord_space":"abs_centralized","bbox_format":"yxyx"}',
+ );
+ expect(s).not.toContain("color");
+ expect(Object.keys(JSON.parse(s))).toEqual(["summary", "items", "bbox_coord_space", "bbox_format"]);
+ });
+
+ it("keeps Chinese text as written (no ASCII escapes)", () => {
+ const d = draft([item("text", "便利贴上的手写字。", [0.25, 0.25, 0.75, 0.5], { text: "记得买牛奶和鸡蛋!" })], "冰箱和厨房台面。");
+ const s = compileMoss(d, 1024, 1024);
+ expect(s).toContain("记得买牛奶和鸡蛋!");
+ expect(s).toContain("冰箱和厨房台面。");
+ expect(s).not.toMatch(/\\u[0-9a-f]{4}/i);
+ });
+
+ it("orders a box drawn from its far corner", () => {
+ expect(mossBox([0.75, 0.5, 0.25, 0.25], 1024, 1024)).toEqual(mossBox([0.25, 0.25, 0.75, 0.5], 1024, 1024));
+ });
+});
+
+describe("decodeMoss", () => {
+ const d = draft(
+ [item("character", "A girl in a yellow coat.", px([96, 160, 480, 992], 1024, 1024)), item("text", "Title.", px([544, 96, 960, 224], 1024, 1024), { text: "雨天" })],
+ "A rainy street.",
+ { meta: ["illustration"], canvas: "single" },
+ );
+
+ it("decodes the compiled prompt back to the same boxes", () => {
+ const back = decodeMoss(compileMoss(d, 1024, 1024), 1024, 1024)!;
+ expect(back.summary).toEqual({ caption: "A rainy street.", meta: ["illustration"], canvas: "single" });
+ expect(back.items.map((i) => i.box)).toEqual(d.items.map((i) => i.box));
+ expect(back.items[1]).toMatchObject({ label: "text", text: "雨天" });
+ expect(compileMoss(back, 1024, 1024)).toBe(compileMoss(d, 1024, 1024));
+ expect(back.aspect).toBe("1:1");
+ });
+
+ it("reads any key order (fal's actual_prompt puts bbox before text)", () => {
+ const fal = JSON.stringify({
+ bbox_format: "yxyx",
+ items: [{ bbox: [-480, -352, -352, 352], text: "SALE", caption: "Red headline.", label: "text" }],
+ summary: { caption: "A shop window.", canvas: "single", meta: [] },
+ bbox_coord_space: "abs_centralized",
+ });
+ const back = decodeMoss(fal, 1024, 1024)!;
+ expect(back.items).toEqual([{ label: "text", caption: "Red headline.", text: "SALE", box: px([160, 32, 864, 160], 1024, 1024) }]);
+ expect(compileMoss(back, 1024, 1024)).toBe(
+ '{"summary":{"meta":[],"canvas":"single","caption":"A shop window."},"items":[{"label":"text","caption":"Red headline.","text":"SALE","bbox":[-480,-352,-352,352]}],"bbox_coord_space":"abs_centralized","bbox_format":"yxyx"}',
+ );
+ });
+
+ it("takes plain words as a caption and refuses JSON that isn't a layout", () => {
+ expect(decodeMoss("A koi pond at night", 1536, 1024)).toEqual({ summary: { caption: "A koi pond at night", meta: [], canvas: "single" }, items: [], aspect: "3:2" });
+ expect(decodeMoss('{"foo": 1}', 1024, 1024)).toBeNull();
+ });
+});
+
+describe("parseLayout", () => {
+ const linxce = {
+ width: 1024,
+ height: 1024,
+ bbox_coord_space: "abs",
+ bbox_format: "xyxy",
+ summary: { caption: "A poster.", canvas: "single" },
+ items: [{ label: "object", caption: "A mountain.", bbox: [128, 288, 896, 640], color: "#3366ff" }],
+ };
+ const want = [{ label: "object", caption: "A mountain.", box: px([128, 288, 896, 640], 1024, 1024), color: "#3366ff" }];
+
+ it("reads a LinXce draft, with prose around it", () => {
+ const p = parseLayout(`Here is the layout I made in ComfyUI:\n${JSON.stringify(linxce)}\nThanks!`)!;
+ expect(p.from).toBe("linxce");
+ expect(p.draft.items).toEqual(want);
+ expect(p.draft.aspect).toBe("1:1");
+ });
+
+ it("reads a draft encoded twice, and inside {layout | draft | source} envelopes", () => {
+ expect(parseLayout(JSON.stringify(JSON.stringify(linxce)))!.draft.items).toEqual(want);
+ expect(parseLayout(JSON.stringify({ layout: JSON.stringify(linxce) }))!.draft.items).toEqual(want);
+ expect(parseLayout(JSON.stringify({ draft: linxce }))!.draft.items).toEqual(want);
+ expect(parseLayout(JSON.stringify({ source: JSON.stringify({ layout: JSON.stringify(linxce) }) }))!.draft.items).toEqual(want);
+ // Past four levels it gives up rather than guess.
+ let deep: unknown = linxce;
+ for (let i = 0; i < 5; i++) deep = { layout: JSON.stringify(deep) };
+ expect(parseLayout(JSON.stringify(deep))).toBeNull();
+ });
+
+ it("repairs Stable Diffusion's \\( escapes", () => {
+ const raw = `{"summary":{"caption":"A \\(bold\\) poster"},"items":[]}`;
+ expect(() => JSON.parse(raw)).toThrow();
+ expect(parseLayout(raw)!.draft.summary.caption).toBe("A \\(bold\\) poster");
+ });
+
+ it("decodes a compiled prompt on its own size, else the canvas given", () => {
+ const compiled = compileMoss(draft([item("object", "A cup.", px([176, 224, 800, 864], 1536, 1024))], "A table."), 1536, 1024);
+ const p = parseLayout(compiled, { width: 1536, height: 1024 })!;
+ expect(p.from).toBe("compiled");
+ expect(p.draft.items[0].box).toEqual(px([176, 224, 800, 864], 1536, 1024));
+ expect(p.draft.aspect).toBe("3:2");
+ });
+
+ it("converts a legacy scene prompt (scene_type / gen_brief / elements / must_not)", () => {
+ const scene = {
+ scene_type: "Movie poster",
+ gen_brief: "a lone astronaut",
+ must_not: ["not blurry", "watermarks"],
+ palette: ["#000000"],
+ elements: [
+ { type: "person", identity: "astronaut", shape: "standing", note: "facing left", bbox: [-256, -128, 256, 128] },
+ { type: "text", identity: "LOST", bbox: [320, -256, 448, 256] },
+ { type: "text", note: "no words given", bbox: [-480, -480, -400, -400] },
+ ],
+ };
+ const p = parseLayout(scene)!;
+ expect(p.from).toBe("scene");
+ expect(p.draft.summary).toEqual({ caption: "Movie poster. a lone astronaut Avoid: blurry; watermarks", meta: [], canvas: "single", palette: ["#000000"] });
+ expect(p.draft.items[0]).toEqual({ label: "character", caption: "astronaut standing facing left", box: [0.375, 0.25, 0.625, 0.75] });
+ expect(p.draft.items[1]).toMatchObject({ label: "text", text: "LOST", box: [0.25, 0.8125, 0.75, 0.9375] });
+ // A text element with no words becomes an object.
+ expect(p.draft.items[2].label).toBe("object");
+ });
+
+ it("scales a scene that spills past the canvas to fit it", () => {
+ const p = parseLayout({ scene_type: "Wide", elements: [{ type: "obj", identity: "bar", bbox: [-100, -1024, 100, 1024] }] })!;
+ const [x0, , x1] = p.draft.items[0].box;
+ expect(x0).toBeCloseTo(0);
+ expect(x1).toBeCloseTo(1);
+ });
+
+ it("reads ComfyUI CreateBoundingBoxes elements (0-1000, yxyx), bare or in prose", () => {
+ const elements = [
+ { type: "obj", bbox: [100, 200, 600, 800], desc: "a red car", color_palette: ["#FF0000"] },
+ { type: "text", bbox: [700, 100, 900, 900], text: "FAST", desc: "chrome letters" },
+ ];
+ const p = parseLayout(JSON.stringify(elements), { width: 1344, height: 768 })!;
+ expect(p.from).toBe("elements");
+ expect([p.width, p.height]).toEqual([1344, 768]);
+ expect(p.draft.items).toEqual([
+ { label: "object", caption: "a red car", box: [0.2, 0.1, 0.8, 0.6], palette: ["#FF0000"] },
+ { label: "text", caption: "chrome letters", text: "FAST", box: [0.1, 0.7, 0.9, 0.9] },
+ ]);
+ expect(parseLayout(`elements: ${JSON.stringify(elements)} (from the node)`)!.draft.items).toHaveLength(2);
+ });
+
+ it("reads Fuobe's own draft back (Export, then Import)", () => {
+ const d = draft([item("text", "Headline.", [0.1, 0.05, 0.9, 0.2], { text: "30% OFF" })], "A candle sale poster.", { meta: ["flat"], canvas: "collage" });
+ const p = parseLayout(JSON.stringify({ ...d, aspect: "4:5" }))!;
+ expect(p.from).toBe("fuobe");
+ expect(p.draft).toEqual({ ...d, aspect: "4:5" });
+ expect(readLayout({ ...d, aspect: "4:5" })).toEqual({ ...d, aspect: "4:5" });
+ });
+
+ it("gives null for text with no layout in it", () => {
+ expect(parseLayout("just a prompt about a koi pond")).toBeNull();
+ expect(parseLayout('{"width": 512}')).toBeNull();
+ expect(parseLayout("")).toBeNull();
+ });
+});
+
+describe("validateLayout", () => {
+ it("names what stops a run", () => {
+ const d = draft([item("object", "A cup.", [0.1, 0.1, 0.4, 0.4]), item("text", "Title.", [0.2, 0.6, 0.8, 0.8], { text: " " }), item("object", "Spill.", [0.5, 0.5, 1.2, 0.9])]);
+ expect(validateLayout(d, 1000, 1024).map((i) => i.message)).toEqual([
+ "Width and height must be multiples of 16 px (this canvas is 1000 × 1024)",
+ "Type the words for box 2",
+ "Box 3 goes past the edge of the picture",
+ ]);
+ expect(validateLayout(d, 1024, 1024).find((i) => i.item === 1)).toMatchObject({ level: "error", message: "Type the words for box 2" });
+ expect(validateLayout(draft([item("object", "Flat.", [0.2, 0.5, 0.6, 0.5])]), 1024, 1024)[0].message).toBe("Box 1 has no size: draw it again");
+ });
+
+ it("warns past 2048 px a side or 4 megapixels", () => {
+ expect(validateLayout(draft([]), 2048, 2048)).toEqual([]);
+ expect(validateLayout(draft([]), 3840, 2160)).toEqual([
+ { level: "warning", message: "3840 × 2160 is past what MOSS-Image was made for (2048 px a side, 4 megapixels): the boxes may drift" },
+ ]);
+ });
+
+ it("holds MOSI to 4,000 characters", () => {
+ const long = draft(Array.from({ length: 40 }, (_, i) => item("object", `Item number ${i} with a long description of its look and feel.`, [0.01 * i, 0.01, 0.01 * i + 0.01, 0.02])));
+ expect(validateLayout(long, 1024, 1024, "moss")).toEqual([]);
+ const issues = validateLayout(long, 1024, 1024, "mosi");
+ expect(issues).toHaveLength(1);
+ expect(issues[0].message).toMatch(/^MOSI takes prompts up to 4,000 characters; this layout is \d,\d{3}$/);
+ });
+});
+
+describe("other models' forms", () => {
+ const poster = draft(
+ [
+ item("object", "A lit candle on a wooden table.", [0.3, 0.35, 0.7, 0.95], { palette: ["#ffaa00"] }),
+ item("text", "Bold serif headline.", [0.1, 0.05, 0.9, 0.2], { text: "30% OFF" }),
+ ],
+ "A sale poster for a candle shop",
+ { meta: ["warm", "minimal"], palette: ["#2b1d0e", "#ffaa00"] },
+ );
+
+ it("gridBox and locationWords", () => {
+ expect(gridBox([0.25, 0.1, 0.75, 0.5])).toEqual([100, 250, 500, 750]);
+ // Python's round: halves go to the even neighbour.
+ expect(gridBox([0.0005, 0.0015, 0.0025, 0.0035])).toEqual([2, 0, 4, 2]);
+ expect(locationWords([0, 0, 0.3, 0.3])).toBe("top left");
+ expect(locationWords([0.4, 0.4, 0.6, 0.6])).toBe("center");
+ expect(locationWords([0.7, 0.4, 0.95, 0.6])).toBe("middle right");
+ expect(locationWords([0.4, 0.75, 0.6, 0.95])).toBe("bottom center");
+ expect(locationWords([0.1, 0.05, 0.9, 0.2])).toBe("across the top");
+ expect(locationWords([0.05, 0.1, 0.3, 0.9])).toBe("down the left side");
+ expect(locationWords([0, 0, 1, 1])).toBe("filling the frame");
+ });
+
+ it("compileFlux3: a caption citing <obj_N>, then the layout table", () => {
+ expect(compileFlux3(poster)).toBe(
+ 'A sale poster for a candle shop. <obj_1> A lit candle on a wooden table, <obj_2> the words "30% OFF" (Bold serif headline). ' +
+ '[{"id": "obj_1", "bbox": [350, 300, 950, 700], "desc": "A lit candle on a wooden table."}, {"id": "obj_2", "bbox": [50, 100, 200, 900], "desc": "Bold serif headline. \\"30% OFF\\""}]',
+ );
+ expect(compileFlux3(draft([], " Just words "))).toBe("Just words");
+ });
+
+ it("compileIdeogram: BuildJsonPromptIdeogram's shape", () => {
+ const backdrop = { ...poster, items: [item("object", "Dark wood panelling.", [0, 0, 1, 1]), ...poster.items] };
+ expect(JSON.parse(compileIdeogram(backdrop))).toEqual({
+ high_level_description: "A sale poster for a candle shop",
+ style_description: { aesthetics: "warm, minimal", lighting: "", medium: "", art_style: "", color_palette: ["#2B1D0E", "#FFAA00"] },
+ compositional_deconstruction: {
+ background: "Dark wood panelling.",
+ elements: [
+ { type: "obj", bbox: [350, 300, 950, 700], desc: "A lit candle on a wooden table.", color_palette: ["#FFAA00"] },
+ { type: "text", bbox: [50, 100, 200, 900], text: "30% OFF", desc: "Bold serif headline." },
+ ],
+ },
+ });
+ // No style words, no backdrop: the caption describes the background.
+ expect(JSON.parse(compileIdeogram(draft([item("object", "A cup.", [0.4, 0.4, 0.6, 0.6])], "A kitchen.")))).toEqual({
+ high_level_description: "A kitchen.",
+ compositional_deconstruction: { background: "A kitchen.", elements: [{ type: "obj", bbox: [400, 400, 600, 600], desc: "A cup." }] },
+ });
+ });
+
+ it("compileVgl: Bria FIBO 2's objects and text_render with location words", () => {
+ expect(compileVgl(poster)).toEqual({
+ short_description: "A sale poster for a candle shop",
+ objects: [
+ { description: "A lit candle on a wooden table.", location: "center", relationship: 'below the words "30% OFF"', relative_size: "medium", shape_and_color: "in #ffaa00" },
+ ],
+ text_render: [{ text: "30% OFF", location: "across the top", size: "medium", appearance_details: "Bold serif headline." }],
+ aesthetics: { composition: "a single picture", color_scheme: "#2b1d0e, #ffaa00", mood_atmosphere: "warm, minimal" },
+ });
+ expect(compileVgl(draft([item("character", "", [0.4, 0.4, 0.6, 0.6])])).objects[0]).toMatchObject({ description: "a person", relationship: "the only element in the picture" });
+ });
+
+ it("compileWords: plain sentences", () => {
+ expect(compileWords(poster)).toBe(
+ 'A sale poster for a candle shop. In the center: A lit candle on a wooden table. Across the top: the words "30% OFF" (Bold serif headline). Style: warm, minimal. Color palette: #2b1d0e, #ffaa00.',
+ );
+ expect(compileWords({ ...poster, summary: { ...poster.summary, canvas: "multi_panel", meta: [], palette: undefined } })).toContain("Laid out as a multi-panel layout.");
+ });
+
+ it("layoutCompilerFor picks the model's form", () => {
+ const at = (layout?: "moss" | "flux3" | "ideogram" | "vgl") => layoutCompilerFor({ options: { layout } })(poster, 1024, 1024);
+ expect(at("moss")).toEqual({ format: "moss", prompt: compileMoss(poster, 1024, 1024) });
+ expect(at("flux3")).toEqual({ format: "flux3", prompt: compileFlux3(poster) });
+ expect(at("ideogram")).toEqual({ format: "ideogram", prompt: compileIdeogram(poster) });
+ expect(at("vgl")).toEqual({ format: "vgl", prompt: compileWords(poster), vgl: compileVgl(poster) });
+ expect(at()).toEqual({ format: "words", prompt: compileWords(poster) });
+ expect(layoutCompilerFor(undefined)(poster, 1024, 1024).format).toBe("words");
+ expect([readsBoxes({ options: { layout: "moss" } }), readsBoxes({ options: { layout: "vgl" } }), readsBoxes({ options: {} })]).toEqual([true, false, false]);
+ });
+
+ it("carriesLayout reads params.extra.layout", () => {
+ expect(carriesLayout({ layout: poster })).toBe(true);
+ expect(carriesLayout({ layout: "nope" })).toBe(false);
+ expect(carriesLayout(undefined)).toBe(false);
+ });
+});