Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor: decompose Universe Builder controller and tab components (#2374)

Adam Eivy1db9fdf21a55
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index c3b02f3f44..fb9fe9da6f 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -100,3 +100,4 @@
- **[issue-2375] Split the ~1,380-line `server/routes/apps.js` into domain-focused sub-routers.** The single apps router (24+ endpoints spanning CRUD/status enrichment, PM2 lifecycle, config repair, Xcode provisioning, icon serving, task-type overrides, document git-commits, editor/folder launch, and agent history) is now assembled from nine mountable sub-routers under `server/routes/apps/` (`crud`, `lifecycle`, `viteTls`, `xcode`, `icons`, `taskTypes`, `launch`, `documents`, `agents`), mirroring the pipeline sub-router pattern. Shared plumbing (`loadApp` middleware, `pathExists`, and the port/overall-status derivation helpers previously duplicated between the list and detail endpoints) lives in one `apps/shared.js`, and the ~130-line ecosystem-config port-rewrite orchestration moved out of the PUT handler into a `server/services/appPortConfig.js` service. The `/api/apps` route contract is byte-for-byte identical (all 35 path+method pairs verified) and the oversized route test file is split along the same domain boundaries. No behavior change.
- **[issue-2389] The Sharing page's per-bucket detail tab (Inbox / Activity / Settings) is now URL-derived and history-safe.** The sub-tab was held in component-local `useState('inbox')`, so it wasn't deep-linkable and browser back/forward could disagree with the visible tab. It now derives from a validated `?tab=` search param via `useDrawerTab('tab', 'inbox', BUCKET_TAB_IDS)` — a direct link opens the named tab, a stale/hand-edited value degrades to Inbox, and switching buckets (a fresh route without the query) resets to Inbox. New render tests cover the direct-link, invalid-fallback, and back/forward cases.
+- **[issue-2374] Universe Builder page decomposed — controller hook + shared/count libs + tab components extracted.** `client/src/pages/UniverseBuilder.jsx` was ~3,700 lines with render-job orchestration, shared constants, prompt-count math, and several full tab/editor components all inline. This slice pulls out the independently-testable pieces without changing behavior or the URL-source-of-truth navigation: a `useRenderJobQueue` controller hook (the per-entry `pendingByEntryId` queue, head-map, enqueue/clear reducer), two pure libs — `universeBuilderShared` (trunk/tab/composite constants + category helpers) and `universeBuilderCounts` (server-mirrored `renderPromptCount`/`scopedPromptCount`/canon-content helpers) — and three tab/editor components moved to `components/universeBuilder/` (`CompositeSheetsEditor`, `BucketChipStrip`, `RenderTab`). Each extraction ships with unit tests; the existing `CategoryEditor`/`TrunkView`/`OtherTab`/`UniverseSelector` test seam is preserved. The page drops ~810 lines. Remaining tab extractions (CategoryEditor, VariationCard, BibleTab, TrunkView, OtherTab) and a fuller page-orchestration hook are deferred.
diff --git a/client/src/components/universeBuilder/BucketChipStrip.jsx b/client/src/components/universeBuilder/BucketChipStrip.jsx
new file mode 100644
index 0000000000..2b15073fb1
--- /dev/null
+++ b/client/src/components/universeBuilder/BucketChipStrip.jsx
@@ -0,0 +1,57 @@
+import { humanizeCategory } from '../../lib/universeBuilderShared';
+
+// Pill-strip bucket selector for the Universe Builder trunk/other tabs. Renders
+// an optional "All" pill, caller-supplied extra chips (e.g. the Canon
+// pseudo-bucket), then one pill per real bucket. Extracted from
+// UniverseBuilder.jsx (#2374). `setBucket('')` clears the selection.
+export default function BucketChipStrip({ buckets, activeBucket, setBucket, showAll = true, extraChips = [] }) {
+ if (buckets.length === 0 && extraChips.length === 0) return null;
+ return (
+ <div className="flex flex-wrap items-center gap-1.5">
+ {showAll && (
+ <button
+ type="button"
+ onClick={() => setBucket('')}
+ className={`px-2.5 py-1.5 rounded-full text-xs min-h-[32px] transition-colors ${
+ !activeBucket
+ ? 'bg-port-accent/25 text-port-accent border border-port-accent/40'
+ : 'bg-port-bg text-gray-300 border border-port-border hover:border-gray-500'
+ }`}
+ >
+ All
+ </button>
+ )}
+ {extraChips.map((chip) => (
+ <button
+ key={chip.key}
+ type="button"
+ onClick={() => setBucket(chip.key)}
+ className={`px-2.5 py-1.5 rounded-full text-xs min-h-[32px] transition-colors ${
+ activeBucket === chip.key
+ ? 'bg-port-accent/25 text-port-accent border border-port-accent/40'
+ : 'bg-port-bg text-gray-300 border border-port-border hover:border-gray-500'
+ }`}
+ >
+ {chip.label}
+ </button>
+ ))}
+ {buckets.map((bucket) => {
+ const active = activeBucket === bucket;
+ return (
+ <button
+ key={bucket}
+ type="button"
+ onClick={() => setBucket(active ? '' : bucket)}
+ className={`px-2.5 py-1.5 rounded-full text-xs min-h-[32px] transition-colors ${
+ active
+ ? 'bg-port-accent/25 text-port-accent border border-port-accent/40'
+ : 'bg-port-bg text-gray-300 border border-port-border hover:border-gray-500'
+ }`}
+ >
+ {humanizeCategory(bucket)}
+ </button>
+ );
+ })}
+ </div>
+ );
+}
diff --git a/client/src/components/universeBuilder/BucketChipStrip.test.jsx b/client/src/components/universeBuilder/BucketChipStrip.test.jsx
new file mode 100644
index 0000000000..e946bc56f7
--- /dev/null
+++ b/client/src/components/universeBuilder/BucketChipStrip.test.jsx
@@ -0,0 +1,47 @@
+import { describe, it, expect, vi } from 'vitest';
+import { render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import BucketChipStrip from './BucketChipStrip.jsx';
+
+describe('BucketChipStrip', () => {
+ it('renders nothing when there are no buckets or extra chips', () => {
+ const { container } = render(
+ <BucketChipStrip buckets={[]} activeBucket="" setBucket={() => {}} />,
+ );
+ expect(container.firstChild).toBeNull();
+ });
+
+ it('renders All + humanized bucket pills and toggles selection', async () => {
+ const setBucket = vi.fn();
+ render(
+ <BucketChipStrip buckets={['deep_space']} activeBucket="" setBucket={setBucket} />,
+ );
+ expect(screen.getByRole('button', { name: 'All' })).toBeInTheDocument();
+ const pill = screen.getByRole('button', { name: 'Deep Space' });
+ await userEvent.click(pill);
+ expect(setBucket).toHaveBeenCalledWith('deep_space');
+ });
+
+ it('clicking the active bucket clears the selection', async () => {
+ const setBucket = vi.fn();
+ render(
+ <BucketChipStrip buckets={['heroes']} activeBucket="heroes" setBucket={setBucket} />,
+ );
+ await userEvent.click(screen.getByRole('button', { name: 'Heroes' }));
+ expect(setBucket).toHaveBeenCalledWith('');
+ });
+
+ it('renders caller-supplied extra chips (e.g. the Canon pseudo-bucket)', async () => {
+ const setBucket = vi.fn();
+ render(
+ <BucketChipStrip
+ buckets={[]}
+ activeBucket=""
+ setBucket={setBucket}
+ extraChips={[{ key: 'canon', label: 'Canon (3)' }]}
+ />,
+ );
+ await userEvent.click(screen.getByRole('button', { name: 'Canon (3)' }));
+ expect(setBucket).toHaveBeenCalledWith('canon');
+ });
+});
diff --git a/client/src/components/universeBuilder/CompositeSheetsEditor.jsx b/client/src/components/universeBuilder/CompositeSheetsEditor.jsx
new file mode 100644
index 0000000000..daacc2cfd6
--- /dev/null
+++ b/client/src/components/universeBuilder/CompositeSheetsEditor.jsx
@@ -0,0 +1,244 @@
+import { useState } from 'react';
+import { Plus, Play, Lock, Unlock, Edit3, X } from 'lucide-react';
+import { COMPOSITE_PROMPT_MAX } from '../../services/api';
+import { COMPOSITE_BOARD_KINDS, compositeKindLabel } from '../../lib/universeBuilderShared';
+
+// Composite-boards editor for the Universe Builder Composites tab. Add / edit /
+// remove reference sheets + world-pitch posters, with per-board + bulk lock
+// toggles (locked boards survive AI Expand). Extracted from UniverseBuilder.jsx
+// (#2374). Pure presentational — `onChange(nextSheets)` owns persistence.
+export default function CompositeSheetsEditor({ sheets, onChange, canRender = false, onRender = null }) {
+ const [adding, setAdding] = useState(false);
+ const [newKind, setNewKind] = useState('reference_sheet');
+ const [newLabel, setNewLabel] = useState('');
+ const [newPrompt, setNewPrompt] = useState('');
+ const [editIdx, setEditIdx] = useState(null);
+ const [editKind, setEditKind] = useState('reference_sheet');
+ const [editLabel, setEditLabel] = useState('');
+ const [editPrompt, setEditPrompt] = useState('');
+
+ const addSheet = () => {
+ const label = newLabel.trim();
+ const prompt = newPrompt.trim();
+ if (!label || !prompt) return;
+ // Stamp `locked: true` up front so the in-draft row matches the
+ // lock-by-default contract before the next save round-trips through
+ // sanitizeCompositeSheet — otherwise the bulk-toggle's locked-count
+ // gate would treat freshly-added boards as unlocked.
+ onChange([...sheets, { kind: newKind, label: label.slice(0, 120), prompt: prompt.slice(0, COMPOSITE_PROMPT_MAX), locked: true }]);
+ setNewKind('reference_sheet');
+ setNewLabel('');
+ setNewPrompt('');
+ setAdding(false);
+ };
+
+ const removeAt = (idx) => onChange(sheets.filter((_, i) => i !== idx));
+
+ // Sheets default to locked at the sanitizer (locked-by-default contract);
+ // persist an unlock as explicit `false` so it survives the next read.
+ const toggleLockAt = (idx) => onChange(sheets.map((s, i) => {
+ if (i !== idx) return s;
+ return { ...s, locked: !s.locked };
+ }));
+
+ const startEdit = (idx, sheet) => {
+ setEditIdx(idx);
+ setEditKind(sheet.kind || 'reference_sheet');
+ setEditLabel(sheet.label);
+ setEditPrompt(sheet.prompt);
+ };
+
+ const saveEdit = () => {
+ const label = editLabel.trim();
+ const prompt = editPrompt.trim();
+ if (!label || !prompt) return;
+ const next = [...sheets];
+ // Preserve `id`, `locked`, `imageRefs` — see VariationCard.saveEdit for
+ // the rationale. The editor only owns kind/label/prompt.
+ next[editIdx] = {
+ ...next[editIdx],
+ kind: editKind,
+ label: label.slice(0, 120),
+ prompt: prompt.slice(0, COMPOSITE_PROMPT_MAX),
+ };
+ onChange(next);
+ setEditIdx(null);
+ };
+
+ const setAllSheetsLocked = (nextLocked) =>
+ onChange(sheets.map((s) => (s?.locked === nextLocked ? s : { ...s, locked: nextLocked })));
+ const sheetsLockedCount = sheets.filter((s) => s?.locked === true).length;
+ const allSheetsLocked = sheets.length > 0 && sheetsLockedCount === sheets.length;
+
+ return (
+ <section className="bg-port-card border border-port-border rounded p-4 flex flex-col gap-3">
+ <div className="flex items-center justify-between gap-2">
+ <h2 className="text-sm font-semibold text-white">
+ Composite boards
+ <span className="ml-2 text-xs text-gray-500">{sheets.length}</span>
+ </h2>
+ <div className="flex items-center gap-1">
+ {sheets.length > 0 && (
+ <button
+ onClick={() => setAllSheetsLocked(!allSheetsLocked)}
+ title={allSheetsLocked
+ ? 'Unlock all composite boards — Expand may overwrite them'
+ : 'Lock all composite boards — Expand will preserve them'}
+ aria-label={allSheetsLocked ? 'Unlock all composite boards' : 'Lock all composite boards'}
+ aria-pressed={allSheetsLocked}
+ className={`p-1 rounded ${
+ allSheetsLocked
+ ? 'text-port-accent hover:bg-port-accent/20'
+ : 'text-gray-500 hover:text-gray-300'
+ }`}
+ >
+ {allSheetsLocked ? <Lock size={14} /> : <Unlock size={14} />}
+ </button>
+ )}
+ <button
+ onClick={() => setAdding((v) => !v)}
+ className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 text-port-accent rounded flex items-center gap-1 min-h-[40px] sm:min-h-0"
+ >
+ <Plus size={12} /> Add
+ </button>
+ </div>
+ </div>
+ {adding && (
+ <div className="bg-port-bg border border-port-border rounded p-2 flex flex-col gap-2">
+ <select
+ value={newKind}
+ onChange={(e) => setNewKind(e.target.value)}
+ className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm min-h-[40px]"
+ >
+ {COMPOSITE_BOARD_KINDS.map((kind) => (
+ <option key={kind.value} value={kind.value}>{kind.label}</option>
+ ))}
+ </select>
+ <input
+ value={newLabel}
+ onChange={(e) => setNewLabel(e.target.value)}
+ placeholder={newKind === 'world_pitch_poster' ? 'World summary concept pitch poster' : 'Gas-Giant Drifters costume sheet'}
+ className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm"
+ maxLength={120}
+ />
+ <textarea
+ value={newPrompt}
+ onChange={(e) => setNewPrompt(e.target.value)}
+ placeholder={newKind === 'world_pitch_poster'
+ ? 'Create a cinematic world summary concept pitch poster with a hero panorama, inset environments, cultures, creatures, visual language, palette, materials, and theme icons...'
+ : 'Create a clean illustrated costume reference sheet...'}
+ className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm"
+ rows={6}
+ maxLength={COMPOSITE_PROMPT_MAX}
+ />
+ <div className="flex items-center gap-2">
+ <button
+ onClick={addSheet}
+ disabled={!newLabel.trim() || !newPrompt.trim()}
+ className="text-xs px-2 py-1 bg-port-accent hover:bg-port-accent/90 disabled:opacity-50 text-white rounded min-h-[40px] sm:min-h-0"
+ >
+ Save
+ </button>
+ <button
+ onClick={() => {
+ setAdding(false);
+ setNewKind('reference_sheet');
+ setNewLabel('');
+ setNewPrompt('');
+ }}
+ className="text-xs px-2 py-1 bg-port-bg hover:bg-port-border text-gray-300 rounded min-h-[40px] sm:min-h-0"
+ >
+ Cancel
+ </button>
+ </div>
+ </div>
+ )}
+ {sheets.length === 0 ? (
+ <p className="text-xs text-gray-500">No composite boards yet.</p>
+ ) : (
+ <ul className="flex flex-col gap-1.5 max-h-96 overflow-y-auto">
+ {sheets.map((sheet, idx) => (
+ <li key={`${sheet.label}-${idx}`} className={`bg-port-bg border rounded p-2 text-sm ${sheet.locked ? 'border-port-accent/50' : 'border-port-border'}`}>
+ {editIdx === idx ? (
+ <div className="flex flex-col gap-1">
+ <select
+ value={editKind}
+ onChange={(e) => setEditKind(e.target.value)}
+ className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm min-h-[40px]"
+ >
+ {COMPOSITE_BOARD_KINDS.map((kind) => (
+ <option key={kind.value} value={kind.value}>{kind.label}</option>
+ ))}
+ </select>
+ <input
+ value={editLabel}
+ onChange={(e) => setEditLabel(e.target.value)}
+ className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm"
+ maxLength={120}
+ />
+ <textarea
+ value={editPrompt}
+ onChange={(e) => setEditPrompt(e.target.value)}
+ rows={8}
+ className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm"
+ maxLength={COMPOSITE_PROMPT_MAX}
+ />
+ <div className="flex gap-2">
+ <button onClick={saveEdit} className="text-xs px-2 py-1 bg-port-accent text-white rounded min-h-[40px] sm:min-h-0">Save</button>
+ <button onClick={() => setEditIdx(null)} className="text-xs px-2 py-1 bg-port-bg text-gray-300 rounded min-h-[40px] sm:min-h-0">Cancel</button>
+ </div>
+ </div>
+ ) : (
+ <div className="flex items-start justify-between gap-2">
+ <div className="flex-1 min-w-0">
+ <div className="flex items-center gap-2">
+ <div className="text-white font-medium truncate">{sheet.label}</div>
+ <span className="shrink-0 text-[10px] uppercase tracking-wide px-1.5 py-0.5 rounded bg-port-accent/10 text-port-accent border border-port-accent/20">
+ {compositeKindLabel(sheet.kind)}
+ </span>
+ </div>
+ <div className="text-xs text-gray-400 line-clamp-3">{sheet.prompt}</div>
+ </div>
+ <div className="flex items-center gap-1 shrink-0">
+ {onRender && (
+ <button
+ onClick={() => onRender(sheet)}
+ disabled={!canRender}
+ className="p-1 text-gray-400 hover:text-port-accent disabled:opacity-30 disabled:cursor-not-allowed rounded"
+ title={canRender ? 'Render this board' : 'Save the world and configure a render backend to enable'}
+ >
+ <Play size={14} />
+ </button>
+ )}
+ <button
+ onClick={() => toggleLockAt(idx)}
+ className={`p-1 rounded ${sheet.locked ? 'text-port-accent hover:bg-port-accent/20' : 'text-gray-500 hover:text-gray-300'}`}
+ title={sheet.locked ? 'Locked — AI expand will preserve this board' : 'Lock this board against AI expand'}
+ aria-pressed={!!sheet.locked}
+ >
+ {sheet.locked ? <Lock size={14} /> : <Unlock size={14} />}
+ </button>
+ <button
+ onClick={() => startEdit(idx, sheet)}
+ className="p-1 text-gray-400 hover:text-port-accent rounded"
+ title="Edit"
+ >
+ <Edit3 size={14} />
+ </button>
+ <button
+ onClick={() => removeAt(idx)}
+ className="p-1 text-gray-400 hover:text-port-error rounded"
+ title="Remove"
+ >
+ <X size={14} />
+ </button>
+ </div>
+ </div>
+ )}
+ </li>
+ ))}
+ </ul>
+ )}
+ </section>
+ );
+}
diff --git a/client/src/components/universeBuilder/CompositeSheetsEditor.test.jsx b/client/src/components/universeBuilder/CompositeSheetsEditor.test.jsx
new file mode 100644
index 0000000000..2486fbeafe
--- /dev/null
+++ b/client/src/components/universeBuilder/CompositeSheetsEditor.test.jsx
@@ -0,0 +1,41 @@
+import { describe, it, expect, vi } from 'vitest';
+import { render, screen } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import CompositeSheetsEditor from './CompositeSheetsEditor.jsx';
+
+const sheet = (over = {}) => ({ kind: 'reference_sheet', label: 'Costume sheet', prompt: 'A clean sheet.', locked: true, ...over });
+
+describe('CompositeSheetsEditor', () => {
+ it('shows the empty state and count', () => {
+ render(<CompositeSheetsEditor sheets={[]} onChange={() => {}} />);
+ expect(screen.getByText('No composite boards yet.')).toBeInTheDocument();
+ });
+
+ it('renders a board with its kind label', () => {
+ render(<CompositeSheetsEditor sheets={[sheet({ kind: 'world_pitch_poster', label: 'Pitch' })]} onChange={() => {}} />);
+ expect(screen.getByText('Pitch')).toBeInTheDocument();
+ expect(screen.getByText('World pitch poster')).toBeInTheDocument();
+ });
+
+ it('removing a board calls onChange without it', async () => {
+ const onChange = vi.fn();
+ render(<CompositeSheetsEditor sheets={[sheet({ label: 'A' }), sheet({ label: 'B' })]} onChange={onChange} />);
+ await userEvent.click(screen.getAllByTitle('Remove')[0]);
+ expect(onChange).toHaveBeenCalledWith([expect.objectContaining({ label: 'B' })]);
+ });
+
+ it('toggling a board lock flips its locked flag', async () => {
+ const onChange = vi.fn();
+ render(<CompositeSheetsEditor sheets={[sheet({ locked: true })]} onChange={onChange} />);
+ await userEvent.click(screen.getByTitle('Locked — AI expand will preserve this board'));
+ expect(onChange).toHaveBeenCalledWith([expect.objectContaining({ locked: false })]);
+ });
+
+ it('render button only appears when onRender is provided and gates on canRender', async () => {
+ const onRender = vi.fn();
+ const s = sheet();
+ render(<CompositeSheetsEditor sheets={[s]} onChange={() => {}} onRender={onRender} canRender />);
+ await userEvent.click(screen.getByTitle('Render this board'));
+ expect(onRender).toHaveBeenCalledWith(s);
+ });
+});
diff --git a/client/src/components/universeBuilder/RenderTab.jsx b/client/src/components/universeBuilder/RenderTab.jsx
new file mode 100644
index 0000000000..ec30c43442
--- /dev/null
+++ b/client/src/components/universeBuilder/RenderTab.jsx
@@ -0,0 +1,292 @@
+import { useMemo } from 'react';
+import { Link } from 'react-router-dom';
+import { FolderOpen, Loader2, Sparkles, FolderTree, Layers } from 'lucide-react';
+import { formatDateTime } from '../../utils/formatters';
+import { RUNNER_FAMILIES, loraCompatKey } from '../../lib/runnerFamilies';
+import { IMAGE_GEN_MODE } from '../../lib/imageGenBackends';
+import ImageGenSettingsForm from '../imageGen/ImageGenSettingsForm';
+import { TRUNK_TABS, humanizeCategory } from '../../lib/universeBuilderShared';
+import { totalVariationCount, countCanonWithContent } from '../../lib/universeBuilderCounts';
+
+// Batch-render tab for the Universe Builder. Renders the shared image-gen
+// settings form plus per-trunk / canon / composite bulk-render targets and a
+// recent-runs list. Extracted from UniverseBuilder.jsx (#2374). handleRender
+// (owned by the page) queues the actual render jobs.
+export default function RenderTab({
+ draft, selectedId, bucketsByKind, renderOpts, setRenderOpts,
+ availableBackends, defaultMode, imageModels, availableLoras = [],
+ handleRender, rendering, runs,
+}) {
+ const currentModel = useMemo(
+ () => imageModels.find((m) => m.id === renderOpts.modelId),
+ [imageModels, renderOpts.modelId],
+ );
+ const currentRunnerFamily = currentModel?.runner || RUNNER_FAMILIES.MFLUX;
+ const currentCompatKey = loraCompatKey(currentModel);
+ // Memoize the counts that drive button labels + disable states. Drafts can
+ // be large (full canon + variations + sheets) and ImageGenSettingsForm
+ // re-renders RenderTab on every keystroke into the per-batch fields.
+ const counts = useMemo(() => {
+ // Mirror server-side compile skip rules — `renderPromptCount` already
+ // filters canon via `canonEntryHasContent`, so each trunk's counts and
+ // the "Render everything" total agree with what the server enqueues.
+ const totalSheets = draft.compositeSheets?.length || 0;
+ const totalVariations = totalVariationCount(draft);
+ const totalCanon = countCanonWithContent(draft, 'characters')
+ + countCanonWithContent(draft, 'places')
+ + countCanonWithContent(draft, 'objects');
+ const otherBuckets = bucketsByKind?.other || [];
+ const totalOtherVariations = otherBuckets.reduce(
+ (n, k) => n + (draft.categories?.[k]?.variations?.length || 0),
+ 0,
+ );
+ return {
+ totalSheets,
+ totalVariations,
+ totalCanon,
+ otherBuckets,
+ totalOtherVariations,
+ totalEverything: totalSheets + totalVariations + totalCanon,
+ };
+ }, [draft, bucketsByKind]);
+ const { totalSheets, totalCanon, otherBuckets, totalOtherVariations, totalEverything } = counts;
+ const perPrompt = renderOpts.batchPerVariation || 1;
+
+ return (
+ <>
+ <section className="bg-port-card border border-port-border rounded p-4 flex flex-col gap-3">
+ <h2 className="text-sm font-semibold text-white flex items-center gap-2">
+ <FolderOpen size={16} className="text-port-accent" /> Batch render
+ </h2>
+ {availableBackends.length === 0 && (
+ <p className="text-xs text-port-warning">
+ Configure a local mflux Python path or enable Codex Imagegen in Settings → Image Gen
+ to enable batch render.
+ </p>
+ )}
+ <ImageGenSettingsForm
+ value={{ ...renderOpts, mode: renderOpts.mode || defaultMode || IMAGE_GEN_MODE.LOCAL }}
+ onChange={(next) => setRenderOpts(next)}
+ models={imageModels}
+ availableBackends={availableBackends}
+ showLoras
+ availableLoras={availableLoras}
+ currentRunnerFamily={currentRunnerFamily}
+ currentCompatKey={currentCompatKey}
+ showStylePreset
+ />
+ <div className="grid grid-cols-2 sm:grid-cols-3 gap-3">
+ <div>
+ <label htmlFor="world-render-batch" className="block text-xs font-medium text-gray-400 mb-1">Renders per prompt</label>
+ <input
+ id="world-render-batch"
+ type="number" min={1} max={20}
+ value={renderOpts.batchPerVariation ?? 1}
+ onChange={(e) => {
+ const n = Number(e.target.value);
+ setRenderOpts((r) => ({ ...r, batchPerVariation: Number.isFinite(n) && n > 0 ? n : 1 }));
+ }}
+ className="w-full bg-port-bg border border-port-border rounded-lg px-2 py-2 text-sm text-white focus:outline-none focus:border-port-accent"
+ />
+ </div>
+ </div>
+ <div className="flex items-center gap-2 flex-wrap">
+ <button
+ onClick={() => handleRender({
+ promptMode: 'all',
+ // Both `scopedPromptCount` (client) and `compilePrompts` (server)
+ // skip canon entirely when `canonSelection` is omitted. The button
+ // label promises "everything", so we must explicitly select every
+ // canon trunk — derived from TRUNK_TABS so the set stays in sync
+ // when a new trunk is added.
+ canonSelection: Object.fromEntries(TRUNK_TABS.map((t) => [t.kind, 'all'])),
+ })}
+ disabled={rendering || !selectedId || totalEverything === 0 || availableBackends.length === 0}
+ className="px-4 py-2 bg-port-accent hover:bg-port-accent/90 disabled:opacity-50 text-white rounded flex items-center gap-2 min-h-[40px]"
+ title="Render every canon entry + every variation + every composite board with these knobs"
+ >
+ {rendering ? <Loader2 size={16} className="animate-spin" /> : <Sparkles size={16} />}
+ Render everything ({totalEverything * perPrompt} image{totalEverything * perPrompt === 1 ? '' : 's'})
+ </button>
+ <span className="text-[11px] text-gray-500">…or pick a narrower scope below.</span>
+ </div>
+ {!selectedId && <p className="text-xs text-gray-500">Save the world first to enable rendering.</p>}
+ </section>
+
+ <section className="bg-port-card border border-port-border rounded p-4 flex flex-col gap-3">
+ <h3 className="text-sm font-semibold text-white">Render targets</h3>
+ <p className="text-[11px] text-gray-500 -mt-1">
+ Click a target to queue that scope immediately with the knobs above.
+ </p>
+ <div className="flex flex-col gap-2">
+ {TRUNK_TABS.map((trunk) => {
+ const buckets = bucketsByKind[trunk.kind] || [];
+ // Use the synthesizable count for both the display label and the
+ // "Bulk-render all" total so the button advertises the number that
+ // will actually land on the server.
+ const canonCount = countCanonWithContent(draft, trunk.kind);
+ const variationCount = buckets.reduce((n, k) => n + (draft.categories?.[k]?.variations?.length || 0), 0);
+ const total = canonCount + variationCount;
+ if (total === 0) return null;
+ const Icon = trunk.icon;
+ return (
+ <div key={trunk.id} className="border border-port-border rounded p-2 bg-port-bg flex flex-col gap-1.5">
+ <div className="flex items-center justify-between gap-2 flex-wrap">
+ <div className="flex items-center gap-2 text-sm text-gray-200">
+ <Icon size={14} className="text-port-accent" />
+ <span className="font-medium">{trunk.label}</span>
+ <span className="text-[11px] text-gray-500">
+ {canonCount} canon · {variationCount} variation{variationCount === 1 ? '' : 's'}
+ </span>
+ </div>
+ <button
+ type="button"
+ onClick={() => handleRender({
+ promptMode: 'all',
+ selection: Object.fromEntries(buckets.map((b) => [b, 'all'])),
+ canonSelection: canonCount > 0 ? { [trunk.kind]: 'all' } : undefined,
+ // Trunk scope excludes composite sheets — see comment on
+ // TrunkView's onBulkRenderTrunk for why this opt-out matters.
+ sheetSelection: [],
+ })}
+ disabled={!selectedId || availableBackends.length === 0 || rendering}
+ className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 disabled:opacity-30 disabled:cursor-not-allowed text-port-accent rounded min-h-[32px]"
+ title={`Render every ${trunk.label.toLowerCase()} canon entry AND every variation under this trunk`}
+ >
+ Bulk-render all ({total})
+ </button>
+ </div>
+ {buckets.length > 0 && (
+ <div className="flex flex-wrap items-center gap-1 pl-5">
+ {buckets.map((bucket) => {
+ const count = draft.categories?.[bucket]?.variations?.length || 0;
+ return (
+ <button
+ key={bucket}
+ type="button"
+ onClick={() => handleRender({ promptMode: 'variations', selection: { [bucket]: 'all' } })}
+ disabled={count === 0 || !selectedId || availableBackends.length === 0 || rendering}
+ className="text-[11px] px-1.5 py-0.5 bg-port-card border border-port-border hover:border-port-accent disabled:opacity-30 disabled:cursor-not-allowed text-gray-300 rounded"
+ title={count === 0 ? 'No variations yet' : `Bulk-render ${humanizeCategory(bucket)} (${count})`}
+ >
+ {humanizeCategory(bucket)} ({count})
+ </button>
+ );
+ })}
+ </div>
+ )}
+ </div>
+ );
+ })}
+ {otherBuckets.length > 0 && totalOtherVariations > 0 ? (
+ <div className="border border-port-border rounded p-2 bg-port-bg flex flex-col gap-1.5">
+ <div className="flex items-center justify-between gap-2 flex-wrap">
+ <div className="flex items-center gap-2 text-sm text-gray-200">
+ <FolderTree size={14} className="text-port-accent" />
+ <span className="font-medium">Other</span>
+ <span className="text-[11px] text-gray-500">
+ {otherBuckets.length} bucket{otherBuckets.length === 1 ? '' : 's'}
+ {' · '}{totalOtherVariations} variation{totalOtherVariations === 1 ? '' : 's'}
+ </span>
+ </div>
+ <button
+ type="button"
+ onClick={() => handleRender({
+ promptMode: 'variations',
+ selection: Object.fromEntries(otherBuckets.map((b) => [b, 'all'])),
+ })}
+ disabled={!selectedId || availableBackends.length === 0 || rendering}
+ className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 disabled:opacity-30 disabled:cursor-not-allowed text-port-accent rounded min-h-[32px]"
+ title="Render every variation in every Other bucket"
+ >
+ Bulk-render all ({totalOtherVariations})
+ </button>
+ </div>
+ <div className="flex flex-wrap items-center gap-1 pl-5">
+ {otherBuckets.map((bucket) => {
+ const count = draft.categories?.[bucket]?.variations?.length || 0;
+ return (
+ <button
+ key={bucket}
+ type="button"
+ onClick={() => handleRender({ promptMode: 'variations', selection: { [bucket]: 'all' } })}
+ disabled={count === 0 || !selectedId || availableBackends.length === 0 || rendering}
+ className="text-[11px] px-1.5 py-0.5 bg-port-card border border-port-border hover:border-port-accent disabled:opacity-30 disabled:cursor-not-allowed text-gray-300 rounded"
+ title={count === 0 ? 'No variations yet' : `Bulk-render ${humanizeCategory(bucket)} (${count})`}
+ >
+ {humanizeCategory(bucket)} ({count})
+ </button>
+ );
+ })}
+ </div>
+ </div>
+ ) : null}
+ {totalCanon > 0 ? (
+ <div className="border border-port-border rounded p-2 bg-port-bg flex items-center justify-between gap-2">
+ <span className="text-sm text-gray-200 flex items-center gap-2">
+ <Sparkles size={14} className="text-port-accent" />
+ All canon
+ <span className="text-[11px] text-gray-500">
+ {totalCanon} entr{totalCanon === 1 ? 'y' : 'ies'}
+ </span>
+ </span>
+ <button
+ type="button"
+ onClick={() => handleRender({
+ promptMode: 'canon',
+ canonSelection: Object.fromEntries(TRUNK_TABS.map((t) => [t.kind, 'all'])),
+ })}
+ disabled={!selectedId || availableBackends.length === 0 || rendering}
+ className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 disabled:opacity-30 disabled:cursor-not-allowed text-port-accent rounded min-h-[32px]"
+ >
+ Bulk-render all canon
+ </button>
+ </div>
+ ) : null}
+ {totalSheets > 0 && (
+ <div className="border border-port-border rounded p-2 bg-port-bg flex items-center justify-between gap-2">
+ <span className="text-sm text-gray-200 flex items-center gap-2">
+ <Layers size={14} className="text-port-accent" />
+ Composite boards
+ <span className="text-[11px] text-gray-500">{totalSheets} board{totalSheets === 1 ? '' : 's'}</span>
+ </span>
+ <button
+ type="button"
+ onClick={() => handleRender({ promptMode: 'sheets', sheetSelection: 'all' })}
+ disabled={!selectedId || availableBackends.length === 0 || rendering}
+ className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 disabled:opacity-30 disabled:cursor-not-allowed text-port-accent rounded min-h-[32px]"
+ >
+ Bulk-render composites
+ </button>
+ </div>
+ )}
+ </div>
+ </section>
+
+ {selectedId && runs.length > 0 && (
+ <section className="bg-port-card border border-port-border rounded p-4">
+ <h2 className="text-sm font-semibold text-white mb-2">Recent runs</h2>
+ <ul className="flex flex-col gap-1">
+ {runs.map((r) => (
+ <li key={r.id} className="flex items-center justify-between text-sm text-gray-300 border-b border-port-border/40 py-1.5">
+ <span className="truncate">
+ <span className="text-gray-500">{formatDateTime(r.createdAt)} —</span>{' '}
+ {r.promptCount} prompt{r.promptCount === 1 ? '' : 's'}
+ </span>
+ {r.collectionId && (
+ <Link
+ to={`/media/collections/${r.collectionId}`}
+ className="text-xs text-port-accent hover:underline whitespace-nowrap"
+ >
+ Open collection →
+ </Link>
+ )}
+ </li>
+ ))}
+ </ul>
+ </section>
+ )}
+
+ </>
+ );
+}
diff --git a/client/src/hooks/README.md b/client/src/hooks/README.md
index 2f393342f4..387a004b00 100644
--- a/client/src/hooks/README.md
+++ b/client/src/hooks/README.md
@@ -146,6 +146,7 @@ grep -i "what you want to do" client/src/hooks/README.md
| `useSongTraining` | Drives a song training run over one scope (whole song or a sliced section) on top of `useColorMatch`: counts in, walks + grades the scope's score, then folds the finished take into the rolling progress history (`songProgress.recordAttempt`) and surfaces the next aggregate via `onProgress`. Supports looping (auto-restart after a run) and keeps a `lastSummary`. Exposes `{ running, countingIn, noteColors, activeIndex, lastSummary, start, stop }`; cancels pending loop relaunches on stop/unmount. | The Song editor's Training panel. |
| `useSyncIntegrity` | Fetches per-kind integrity diff from every eligible online peer; reduces to worst-case `statusById` + `byPeer` breakdown maps. | Federated media sync integrity UI — badges, drawers, peer breakdown. |
| `useTwinEvaluationSuite` | Shared lifecycle for the three Digital Twin evaluation-suite panels (Values-Alignment, Adversarial-Boundary, Multi-Turn): loads suite + recent history (silenced), runs every selected provider/model in parallel, prepends fresh runs, and handles the stale-persona / partial-failure toast fan-out. `countField` names the suite's pass-count key so history stays generic. Returns `{ items, history, loading, running, results, expanded, setExpanded, run, reload }`. | The Digital Twin `TwinEvaluationSuitePanel` presentation shell — don't re-roll the load/run/history dance per suite. |
+| `useRenderJobQueue` | Per-entry render-job queue for the Universe Builder — `pendingByEntryId` (entryId → jobId[]), `pendingHeadByEntryId` (first jobId per entry for single-subscription thumbs), `enqueueEntryJobs` (append variation/canon jobs), `clearPendingForEntry` (shift a jobId out, or drop the entry). | Universe Builder render orchestration. Don't re-roll the per-entry pending-jobs reducer. |
| `useUniverse` | Loads the universe record for a `universeId` with mount/cancel guards; returns `[universe, setUniverse, loading, error]` (setter exposed for optimistic post-mutation updates). | Any surface that loads a linked universe (pipeline stages, etc.) — use instead of re-rolling the getUniverse-on-mount effect. |
| `useUniverseAction` | LLM-driven universe mutation scaffolding. | Universe Builder action UIs. |
| `useUniverseNav` | `goToWorld(id)` → navigate to `/universes/:id`, preserve `location.search`. | Any Universe Builder caller that needs to switch worlds via URL. |
diff --git a/client/src/hooks/index.js b/client/src/hooks/index.js
index 05a847612b..1bfc3bcd1d 100644
--- a/client/src/hooks/index.js
+++ b/client/src/hooks/index.js
@@ -127,6 +127,7 @@ export * from './useDeathClock.js';
export * from './useGoalDetail.js';
export * from './usePostSession.js';
export * from './useRecordMerge.js';
+export * from './useRenderJobQueue.js';
export * from './useSyncIntegrity.js';
export * from './useTwinEvaluationSuite.js';
export * from './useUniverseNav.js';
diff --git a/client/src/hooks/useRenderJobQueue.js b/client/src/hooks/useRenderJobQueue.js
new file mode 100644
index 0000000000..9963a50c7f
--- /dev/null
+++ b/client/src/hooks/useRenderJobQueue.js
@@ -0,0 +1,79 @@
+import { useCallback, useMemo, useState } from 'react';
+
+// Per-entry render-job queue controller for the Universe Builder page.
+//
+// A single render batch can queue several jobs against the SAME entry id
+// (`batchPerVariation > 1`, or back-to-back renders on one row), so each
+// entry maps to an ARRAY of in-flight jobIds — a scalar would overwrite
+// siblings and the row spinner would clear as soon as the first job settled
+// while others are still running. Canon entries share this queue too: the
+// batch `/render` route compiles canon prompts when the user picks
+// "all" / "canon" mode, and those jobs don't route through
+// `UniverseCanonSection.renderingJobs`.
+//
+// Extracted from UniverseBuilder.jsx (#2374) so the queue reducer logic is
+// unit-tested in isolation from the page's render orchestration.
+export function useRenderJobQueue() {
+ // entryId → jobId[] queue for any row with one or more in-flight renders.
+ const [pendingByEntryId, setPendingByEntryId] = useState({});
+
+ // First-jobId-per-entry view for components that only need a single
+ // subscription per row (MediaJobThumb takes one jobId). When the head
+ // finishes we shift it off and the next jobId in the queue takes over.
+ const pendingHeadByEntryId = useMemo(() => {
+ const out = {};
+ for (const [entryId, jobs] of Object.entries(pendingByEntryId)) {
+ if (Array.isArray(jobs) && jobs.length > 0) out[entryId] = jobs[0];
+ }
+ return out;
+ }, [pendingByEntryId]);
+
+ // Remove a specific completed jobId from the entry's queue (preferred path —
+ // handlers know which jobId finished). When called without a jobId (failure
+ // paths that want to bail entirely on the entry), drops every pending job
+ // for that entry.
+ const clearPendingForEntry = useCallback((entryId, jobId = null) => {
+ if (!entryId) return;
+ setPendingByEntryId((prev) => {
+ const jobs = prev[entryId];
+ if (!Array.isArray(jobs) || jobs.length === 0) return prev;
+ const next = { ...prev };
+ if (jobId == null) {
+ delete next[entryId];
+ return next;
+ }
+ const remaining = jobs.filter((j) => j !== jobId);
+ if (remaining.length === 0) delete next[entryId];
+ else next[entryId] = remaining;
+ return next;
+ });
+ }, []);
+
+ // Append freshly-queued jobs to their per-entry queues. Each `entryJobs`
+ // item is `{ jobId, entryRef: { id, kind } }`; only `variation` / `canon`
+ // kinds have per-row pending UI, so sheet (and other) kinds are skipped —
+ // otherwise their jobIds would accumulate forever with no consumer to clear
+ // them.
+ const enqueueEntryJobs = useCallback((entryJobs) => {
+ if (!Array.isArray(entryJobs) || entryJobs.length === 0) return;
+ setPendingByEntryId((prev) => {
+ const next = { ...prev };
+ for (const { jobId, entryRef } of entryJobs) {
+ if (!jobId || !entryRef?.id) continue;
+ if (entryRef.kind !== 'variation' && entryRef.kind !== 'canon') continue;
+ const existing = Array.isArray(next[entryRef.id]) ? next[entryRef.id] : [];
+ next[entryRef.id] = [...existing, jobId];
+ }
+ return next;
+ });
+ }, []);
+
+ return {
+ pendingByEntryId,
+ pendingHeadByEntryId,
+ clearPendingForEntry,
+ enqueueEntryJobs,
+ };
+}
+
+export default useRenderJobQueue;
diff --git a/client/src/hooks/useRenderJobQueue.test.jsx b/client/src/hooks/useRenderJobQueue.test.jsx
new file mode 100644
index 0000000000..4dbdf40bff
--- /dev/null
+++ b/client/src/hooks/useRenderJobQueue.test.jsx
@@ -0,0 +1,89 @@
+import { describe, it, expect } from 'vitest';
+import { renderHook, act } from '@testing-library/react';
+import { useRenderJobQueue } from './useRenderJobQueue.js';
+
+describe('useRenderJobQueue', () => {
+ it('starts empty', () => {
+ const { result } = renderHook(() => useRenderJobQueue());
+ expect(result.current.pendingByEntryId).toEqual({});
+ expect(result.current.pendingHeadByEntryId).toEqual({});
+ });
+
+ it('enqueues variation and canon jobs, skipping other kinds', () => {
+ const { result } = renderHook(() => useRenderJobQueue());
+ act(() => {
+ result.current.enqueueEntryJobs([
+ { jobId: 'j1', entryRef: { id: 'a', kind: 'variation' } },
+ { jobId: 'j2', entryRef: { id: 'b', kind: 'canon' } },
+ { jobId: 'j3', entryRef: { id: 'c', kind: 'sheet' } },
+ { jobId: 'j4', entryRef: { id: 'a', kind: 'variation' } },
+ ]);
+ });
+ expect(result.current.pendingByEntryId).toEqual({ a: ['j1', 'j4'], b: ['j2'] });
+ // Head map exposes only the first jobId per entry.
+ expect(result.current.pendingHeadByEntryId).toEqual({ a: 'j1', b: 'j2' });
+ });
+
+ it('ignores malformed entries and no-ops on empty input', () => {
+ const { result } = renderHook(() => useRenderJobQueue());
+ act(() => {
+ result.current.enqueueEntryJobs([]);
+ result.current.enqueueEntryJobs([
+ { jobId: '', entryRef: { id: 'a', kind: 'variation' } },
+ { jobId: 'j1', entryRef: null },
+ { jobId: 'j2', entryRef: { kind: 'canon' } },
+ ]);
+ });
+ expect(result.current.pendingByEntryId).toEqual({});
+ });
+
+ it('clears a specific jobId and shifts the head forward', () => {
+ const { result } = renderHook(() => useRenderJobQueue());
+ act(() => {
+ result.current.enqueueEntryJobs([
+ { jobId: 'j1', entryRef: { id: 'a', kind: 'variation' } },
+ { jobId: 'j2', entryRef: { id: 'a', kind: 'variation' } },
+ ]);
+ });
+ act(() => result.current.clearPendingForEntry('a', 'j1'));
+ expect(result.current.pendingByEntryId).toEqual({ a: ['j2'] });
+ expect(result.current.pendingHeadByEntryId).toEqual({ a: 'j2' });
+ });
+
+ it('deletes the entry when the last jobId clears', () => {
+ const { result } = renderHook(() => useRenderJobQueue());
+ act(() => {
+ result.current.enqueueEntryJobs([
+ { jobId: 'j1', entryRef: { id: 'a', kind: 'canon' } },
+ ]);
+ });
+ act(() => result.current.clearPendingForEntry('a', 'j1'));
+ expect(result.current.pendingByEntryId).toEqual({});
+ });
+
+ it('drops every pending job for an entry when no jobId is given', () => {
+ const { result } = renderHook(() => useRenderJobQueue());
+ act(() => {
+ result.current.enqueueEntryJobs([
+ { jobId: 'j1', entryRef: { id: 'a', kind: 'variation' } },
+ { jobId: 'j2', entryRef: { id: 'a', kind: 'variation' } },
+ ]);
+ });
+ act(() => result.current.clearPendingForEntry('a'));
+ expect(result.current.pendingByEntryId).toEqual({});
+ });
+
+ it('is a no-op when clearing an unknown entry or with no entryId', () => {
+ const { result } = renderHook(() => useRenderJobQueue());
+ act(() => {
+ result.current.enqueueEntryJobs([
+ { jobId: 'j1', entryRef: { id: 'a', kind: 'variation' } },
+ ]);
+ });
+ act(() => {
+ result.current.clearPendingForEntry('missing', 'jX');
+ result.current.clearPendingForEntry(null);
+ });
+ expect(result.current.pendingByEntryId).toEqual({ a: ['j1'] });
+ });
+});
diff --git a/client/src/lib/README.md b/client/src/lib/README.md
index 7c269917d8..dd4bf3e60b 100644
--- a/client/src/lib/README.md
+++ b/client/src/lib/README.md
@@ -125,5 +125,7 @@ grep -i "what you want to do" client/src/lib/README.md
| `streakGlyph.js` | `streakGlyph(streak)` — the single POST streak emoji (✨ / ⚡ / 🔥 by day count) shared by the POST launcher, Morse trainer, daily widget, and dashboard streak widget. |
| `syncCounts.js` | Directional pending-sync counts for the Instances federation cards. `directionalCounts({ localMax, peerMax, ourCursor, peerCursorForUs })` → `{ toPull, toPush }` (each a non-negative Number, or `null` when unknown) from monotonic change-log seqs; `diffSeq(ahead, behind)` is the BigInt-safe clamp-at-zero subtraction (handles numeric strings past `MAX_SAFE_INTEGER`); `describeDirectional({ toPull, toPush })` renders the plain-language `{ state, text }` ("in sync" / "N to pull · M to push" / "checking…"). No React — `SyncStatusBadge` drives it. |
| `terminalTheme.js` | Pure xterm.js palette builder for the Shell terminal. `buildTerminalTheme({ bg, fg, accent, card, error, success, warning }, mode)` assembles the xterm `theme` object — base ANSI colors from the active theme's CSS vars, the rest from mode-tuned literals (`ANSI_NIGHT` bright/pastel for dark backgrounds, `ANSI_DAY` darkened/saturated so colored CLI output stays legible on daytime themes). `parseCssColorToHex(raw, fallback)` normalizes both the `15 15 15` triple form and the `rgb(7 7 7 / 0.86)` function form (used by `--port-terminal-*` tokens) to `#rrggbb`, dropping alpha. No DOM reads — `Shell.jsx` resolves CSS vars and re-applies on theme switch. |
+| `universeBuilderShared.js` | Shared constants + pure category/trunk/composite helpers for the Universe Builder page and its extracted tab/editor components (#2374): `TRUNK_TABS`/`TRUNK_BY_ID`/`TRUNK_BY_KIND`, `TAB_*` + `BUCKET_CANON`, `CATEGORY_LABELS`, `DEFAULT_RENDER_OPTS`, `COMPOSITE_BOARD_KINDS`; and helpers `groupBucketsByKind`, `normalizeCategoryKey`, `humanizeCategory`, `ensureDraftCategories`, `getCategoryKeys`, `compositeKindLabel`. No React state — safe to import from the page or any tab component without a cycle. |
+| `universeBuilderCounts.js` | Pure prompt-count helpers for the Universe Builder render buttons (#2374), mirroring the server's compile/skip rules so inline counts match what `/render` will enqueue: `totalVariationCount`, `canonEntryHasContent`, `countCanonWithContent`, `renderPromptCount`, `scopedPromptCount`. The server `compilePrompts`/`synthesizeCanonPrompt` copy is authoritative — keep in sync. |
| `wrSceneCursor.js` | Resolve which script scene the editor caret sits in (`sceneAtCursor`, `sceneAnchorIndex`) — inverse of WorkEditor's jump-to-scene text search; drives the live render preview's "scene at cursor" target. |
| `writingGuide.js` | Canonical Writers Room reference data + craft principles rendered by the Guide page (`/writers-room/guide`): `WRITING_LENGTH_TARGETS` (microfiction→novel word/char bands), `BOOK_LENGTH_ESTIMATES` (page-based), `WRITING_PRINCIPLES`, `PLANNED_ANALYSES` (e.g. the emotional-roadmap evaluator), and `classifyByWordCount(n)` for labelling a draft's length. Future word-count gauges / length checks read from here so targets don't drift from the docs. |
diff --git a/client/src/lib/index.js b/client/src/lib/index.js
index 1d55ebdc76..4609395e35 100644
--- a/client/src/lib/index.js
+++ b/client/src/lib/index.js
@@ -12,6 +12,8 @@ export * from './seasonStructure.js';
export * from './sheetPointers.js';
export * from './shotContinuity.js';
export * from './shotGrammar.js';
+export * from './universeBuilderCounts.js';
+export * from './universeBuilderShared.js';
export * from './universeRunTag.js';
export * from './universeStylePreset.js';
diff --git a/client/src/lib/universeBuilderCounts.js b/client/src/lib/universeBuilderCounts.js
new file mode 100644
index 0000000000..8eca30ecd9
--- /dev/null
+++ b/client/src/lib/universeBuilderCounts.js
@@ -0,0 +1,115 @@
+import { hasCanonDescriptorContent } from './canonPrompt.js';
+import { getCategoryKeys } from './universeBuilderShared.js';
+
+// Pure prompt-count helpers for the Universe Builder (#2374). They mirror the
+// server's compile/skip rules so inline "Render N images" buttons can advertise
+// the exact number the server will enqueue, without a round trip. The server
+// copy in server/services/universeBuilder.js (compilePrompts /
+// synthesizeCanonPrompt) is authoritative — keep these in sync.
+
+export function totalVariationCount(world) {
+ return getCategoryKeys(world?.categories).reduce(
+ (n, c) => n + (world?.categories?.[c]?.variations?.length || 0),
+ 0,
+ );
+}
+
+const hasNonBlankString = (v) => typeof v === 'string' && v.trim().length > 0;
+
+// Mirror the server's synthesizeCanonPrompt skip rule: entries with no
+// identity-anchor (name / slugline / prompt) AND no descriptive content for the
+// kind compile to an empty seed and get skipped at render time.
+export const canonEntryHasContent = (e, kind) => {
+ if (!e) return false;
+ if (hasNonBlankString(e.prompt)) return true;
+ // Identifier anchors per kind — places allow slugline-only entries (bible
+ // sanitizer); characters/objects ignore stray slugline. Mirrors server
+ // synthesizeCanonPrompt's identifier-seed rule.
+ if (hasNonBlankString(e.name)) return true;
+ if (kind === 'places' && hasNonBlankString(e.slugline)) return true;
+ return hasCanonDescriptorContent(kind, e);
+};
+
+export const countCanonWithContent = (world, kind) =>
+ (Array.isArray(world?.[kind]) ? world[kind] : []).filter((e) => canonEntryHasContent(e, kind)).length;
+
+export function renderPromptCount(world, promptMode = 'variations') {
+ if (promptMode === 'variations') return totalVariationCount(world);
+ const sheets = world?.compositeSheets?.length || 0;
+ if (promptMode === 'sheets') return sheets;
+ const canon = countCanonWithContent(world, 'characters')
+ + countCanonWithContent(world, 'places')
+ + countCanonWithContent(world, 'objects');
+ if (promptMode === 'canon') return canon;
+ return totalVariationCount(world) + sheets + canon;
+}
+
+// Mirrors the server's compilePrompts for selection/sheetSelection/canonSelection
+// so an inline "Render" button can disable itself + show an accurate count
+// without a round trip.
+//
+// Defaulting rules — mirror server/services/universeBuilder.js compilePrompts:
+// - sheets/all + no sheetSelection → render every sheet (server defaults to 'all')
+// - variations/all + no selection → render every category (server falls back
+// to a full category map via getWorldCategoryKeys)
+// - canon/all + no canonSelection → render NOTHING (server gates on a non-null
+// canonSelection; missing key skips the trunk entirely)
+// Canon entries are filtered through `canonEntryHasContent(kind)` since the
+// server skips entries whose synthesized seed is empty.
+export function scopedPromptCount(world, scope) {
+ if (!scope) return 0;
+ const mode = scope.promptMode || 'variations';
+ let n = 0;
+ if (mode === 'sheets' || mode === 'all') {
+ const sheets = world?.compositeSheets || [];
+ if (scope.sheetSelection === 'all' || scope.sheetSelection === undefined || scope.sheetSelection === null) {
+ // Both `sheets` and `all` default to every sheet when sheetSelection is
+ // omitted — server: `options.sheetSelection || 'all'`.
+ n += sheets.length;
+ } else if (Array.isArray(scope.sheetSelection)) {
+ const set = new Set(scope.sheetSelection.map((s) => s.toLowerCase()));
+ n += sheets.filter((s) => set.has((s.label || '').toLowerCase())).length;
+ }
+ }
+ if (mode === 'variations' || mode === 'all') {
+ if (scope.selection) {
+ for (const [cat, pick] of Object.entries(scope.selection)) {
+ const vars = world?.categories?.[cat]?.variations || [];
+ if (pick === 'all') n += vars.length;
+ else if (Array.isArray(pick)) {
+ const labels = new Set(pick.map((p) => p.toLowerCase()));
+ n += vars.filter((v) => labels.has((v.label || '').toLowerCase())).length;
+ }
+ }
+ } else {
+ // No selection ⇒ server treats this as "every category, all variations".
+ n += totalVariationCount(world);
+ }
+ }
+ if (mode === 'canon' || mode === 'all') {
+ if (scope.canonSelection) {
+ for (const trunk of ['characters', 'places', 'objects']) {
+ const pick = scope.canonSelection[trunk];
+ if (!pick) continue;
+ const entries = Array.isArray(world?.[trunk]) ? world[trunk] : [];
+ const withContent = entries.filter((e) => canonEntryHasContent(e, trunk));
+ if (pick === 'all') n += withContent.length;
+ else if (Array.isArray(pick)) {
+ const needles = new Set(pick.map((p) => p.toLowerCase()));
+ // Mirror server: slugline matching is places-only — name is the
+ // shared anchor for characters/objects.
+ n += withContent.filter((e) => {
+ const name = (e.name || '').toLowerCase();
+ if (needles.has(name)) return true;
+ if (trunk === 'places') {
+ const slug = (e.slugline || '').toLowerCase();
+ if (needles.has(slug)) return true;
+ }
+ return false;
+ }).length;
+ }
+ }
+ }
+ }
+ return n;
+}
diff --git a/client/src/lib/universeBuilderCounts.test.js b/client/src/lib/universeBuilderCounts.test.js
new file mode 100644
index 0000000000..3d5d4f7161
--- /dev/null
+++ b/client/src/lib/universeBuilderCounts.test.js
@@ -0,0 +1,94 @@
+import { describe, it, expect } from 'vitest';
+import {
+ totalVariationCount, canonEntryHasContent, countCanonWithContent,
+ renderPromptCount, scopedPromptCount,
+} from './universeBuilderCounts.js';
+
+const world = () => ({
+ categories: {
+ heroes: { variations: [{ label: 'a' }, { label: 'b' }] },
+ cities: { variations: [{ label: 'c' }] },
+ },
+ compositeSheets: [{ label: 'Poster' }, { label: 'Sheet' }],
+ characters: [
+ { name: 'Nova', prompt: 'A drifter.' },
+ { name: '' }, // no anchor, no content → skipped
+ ],
+ places: [
+ { slugline: 'THE-RIFT' }, // places allow slugline-only
+ ],
+ objects: [
+ { prompt: 'A brass sextant.' },
+ ],
+});
+
+describe('universeBuilderCounts — totalVariationCount', () => {
+ it('sums variations across buckets', () => {
+ expect(totalVariationCount(world())).toBe(3);
+ });
+ it('tolerates missing categories', () => {
+ expect(totalVariationCount({})).toBe(0);
+ expect(totalVariationCount(undefined)).toBe(0);
+ });
+});
+
+describe('universeBuilderCounts — canon content', () => {
+ it('counts prompt / name anchors and places slugline', () => {
+ const w = world();
+ expect(canonEntryHasContent(w.characters[0], 'characters')).toBe(true);
+ expect(canonEntryHasContent(w.characters[1], 'characters')).toBe(false);
+ expect(canonEntryHasContent(w.places[0], 'places')).toBe(true);
+ // slugline is a places-only anchor — characters ignore it.
+ expect(canonEntryHasContent({ slugline: 'X' }, 'characters')).toBe(false);
+ expect(canonEntryHasContent(null, 'objects')).toBe(false);
+ });
+ it('countCanonWithContent filters empty-seed entries', () => {
+ const w = world();
+ expect(countCanonWithContent(w, 'characters')).toBe(1);
+ expect(countCanonWithContent(w, 'places')).toBe(1);
+ expect(countCanonWithContent(w, 'objects')).toBe(1);
+ });
+});
+
+describe('universeBuilderCounts — renderPromptCount', () => {
+ it('modes cover variations / sheets / canon / all', () => {
+ const w = world();
+ expect(renderPromptCount(w, 'variations')).toBe(3);
+ expect(renderPromptCount(w, 'sheets')).toBe(2);
+ expect(renderPromptCount(w, 'canon')).toBe(3);
+ expect(renderPromptCount(w, 'all')).toBe(3 + 2 + 3);
+ });
+});
+
+describe('universeBuilderCounts — scopedPromptCount', () => {
+ it('returns 0 for a null scope', () => {
+ expect(scopedPromptCount(world(), null)).toBe(0);
+ });
+ it('counts a variation selection', () => {
+ expect(scopedPromptCount(world(), { promptMode: 'variations', selection: { heroes: 'all' } })).toBe(2);
+ });
+ it('variations with no selection means all categories', () => {
+ expect(scopedPromptCount(world(), { promptMode: 'variations' })).toBe(3);
+ });
+ it('sheets default to every sheet when sheetSelection omitted', () => {
+ expect(scopedPromptCount(world(), { promptMode: 'sheets' })).toBe(2);
+ });
+ it('sheets honor a label array selection', () => {
+ expect(scopedPromptCount(world(), { promptMode: 'sheets', sheetSelection: ['poster'] })).toBe(1);
+ });
+ it('canon selection counts only content-bearing entries', () => {
+ expect(scopedPromptCount(world(), { promptMode: 'canon', canonSelection: { characters: 'all' } })).toBe(1);
+ });
+ it('canon with no selection renders nothing', () => {
+ expect(scopedPromptCount(world(), { promptMode: 'canon' })).toBe(0);
+ });
+ it('all-mode sums sheets + variations + canon selections', () => {
+ const scope = {
+ promptMode: 'all',
+ selection: { heroes: 'all' },
+ canonSelection: { objects: 'all' },
+ };
+ // 2 sheets (default all) + 2 hero variations + 1 object canon = 5
+ expect(scopedPromptCount(world(), scope)).toBe(5);
+ });
+});
diff --git a/client/src/lib/universeBuilderShared.js b/client/src/lib/universeBuilderShared.js
new file mode 100644
index 0000000000..d01b11e4b4
--- /dev/null
+++ b/client/src/lib/universeBuilderShared.js
@@ -0,0 +1,113 @@
+import { Users, MapPin, Package } from 'lucide-react';
+import { WORLD_CATEGORIES, WORLD_CATEGORY_KEY_MAX } from '../services/api';
+
+// Shared constants + pure category/trunk/composite helpers for the Universe
+// Builder page and its extracted tab/editor components (#2374). Kept free of
+// React state so the helpers are unit-testable and importable from either the
+// page or the component modules without a cycle.
+
+export const CATEGORY_LABELS = {
+ landscapes: 'Landscapes',
+ environments: 'Environments',
+ structures: 'Structures',
+ vehicles: 'Vehicles',
+};
+
+// Tab order in the Universe Builder. Bible / Composites / Render are always
+// visible; the three canon trunks (Cast / Places / Objects) render even when
+// empty so the user has a discoverable target for canon+variation work; Other
+// only renders when at least one un-kinded bucket exists.
+export const TAB_BIBLE = 'bible';
+export const TAB_CAST = 'cast';
+export const TAB_PLACES = 'places';
+export const TAB_OBJECTS = 'objects';
+export const TAB_OTHER = 'other';
+export const TAB_COMPOSITES = 'composites';
+export const TAB_RENDER = 'render';
+
+// Pseudo-bucket key for the canon-only view inside a trunk. Overloads
+// `?bucket=` (alongside real bucket keys) AND a `promptMode` value on the
+// render route; same string in both contexts to keep the contract consistent.
+export const BUCKET_CANON = 'canon';
+
+// `kind` doubles as the canon-array key on the universe (`draft[kind]`) and
+// the canon-trunk identifier the server's `canonSelection` schema accepts.
+export const TRUNK_TABS = [
+ { id: TAB_CAST, kind: 'characters', label: 'Cast', icon: Users },
+ { id: TAB_PLACES, kind: 'places', label: 'Places', icon: MapPin },
+ { id: TAB_OBJECTS, kind: 'objects', label: 'Objects', icon: Package },
+];
+export const TRUNK_BY_ID = Object.fromEntries(TRUNK_TABS.map((t) => [t.id, t]));
+export const TRUNK_BY_KIND = Object.fromEntries(TRUNK_TABS.map((t) => [t.kind, t]));
+
+// Group category buckets by their `kind` tag. Buckets with an unknown / missing
+// kind fall into the `other` bin — that bin drives whether the Other tab shows.
+export const groupBucketsByKind = (categories = {}) => {
+ const out = { characters: [], places: [], objects: [], other: [] };
+ for (const [key, bucket] of Object.entries(categories || {})) {
+ const kind = bucket?.kind || 'other';
+ if (out[kind]) out[kind].push(key);
+ else out.other.push(key);
+ }
+ return out;
+};
+
+export const normalizeCategoryKey = (raw) => (raw || '')
+ .trim()
+ .toLowerCase()
+ .replace(/&/g, ' and ')
+ .replace(/[^a-z0-9]+/g, '_')
+ .replace(/^_+|_+$/g, '')
+ .replace(/_{2,}/g, '_')
+ .slice(0, WORLD_CATEGORY_KEY_MAX);
+
+export const humanizeCategory = (key) => CATEGORY_LABELS[key]
+ || (key || '').replace(/_/g, ' ').replace(/\b\w/g, (ch) => ch.toUpperCase());
+
+export const ensureDraftCategories = (categories = {}) => ({
+ ...Object.fromEntries(WORLD_CATEGORIES.map((c) => [c, { variations: [] }])),
+ ...(categories || {}),
+});
+
+export const getCategoryKeys = (categories = {}) => {
+ const seen = new Set();
+ const keys = [];
+ for (const key of [...WORLD_CATEGORIES, ...Object.keys(categories || {})]) {
+ const normalized = normalizeCategoryKey(key);
+ if (!normalized || seen.has(normalized)) continue;
+ seen.add(normalized);
+ keys.push(normalized);
+ }
+ return keys;
+};
+
+// Default per-render knobs. Mirrors the Image Gen page's default chip.
+export const DEFAULT_RENDER_OPTS = {
+ width: 1024,
+ height: 1024,
+ steps: 30,
+ guidance: '',
+ cfgScale: 7,
+ quantize: '8',
+ modelId: '',
+ mode: '',
+ promptMode: 'variations',
+ batchPerVariation: 1,
+ // Extended Image Gen surface used by the batch render form. Server accepts
+ // these as optional patches on top of the universe's stored influences.
+ seed: '',
+ negativePrompt: '',
+ extraStyle: '',
+ stylePreset: null,
+ loras: [],
+};
+
+// Mirror of COMPOSITE_SHEET_KINDS in server/services/universeBuilder.js — keep
+// in sync when adding kinds.
+export const COMPOSITE_BOARD_KINDS = [
+ { value: 'reference_sheet', label: 'Reference sheet' },
+ { value: 'world_pitch_poster', label: 'World pitch poster' },
+];
+
+export const compositeKindLabel = (kind) =>
+ COMPOSITE_BOARD_KINDS.find((k) => k.value === kind)?.label || 'Reference sheet';
diff --git a/client/src/lib/universeBuilderShared.test.js b/client/src/lib/universeBuilderShared.test.js
new file mode 100644
index 0000000000..e52145325e
--- /dev/null
+++ b/client/src/lib/universeBuilderShared.test.js
@@ -0,0 +1,84 @@
+import { describe, it, expect } from 'vitest';
+import {
+ TRUNK_TABS, TRUNK_BY_ID, TRUNK_BY_KIND, BUCKET_CANON,
+ groupBucketsByKind, normalizeCategoryKey, humanizeCategory,
+ ensureDraftCategories, getCategoryKeys, compositeKindLabel, COMPOSITE_BOARD_KINDS,
+} from './universeBuilderShared.js';
+
+describe('universeBuilderShared — trunk maps', () => {
+ it('indexes trunks by id and kind', () => {
+ expect(TRUNK_TABS).toHaveLength(3);
+ expect(TRUNK_BY_ID.cast.kind).toBe('characters');
+ expect(TRUNK_BY_KIND.places.id).toBe('places');
+ expect(BUCKET_CANON).toBe('canon');
+ });
+});
+
+describe('universeBuilderShared — groupBucketsByKind', () => {
+ it('bins buckets by kind and folds unknown/missing into other', () => {
+ const grouped = groupBucketsByKind({
+ heroes: { kind: 'characters' },
+ cities: { kind: 'places' },
+ loot: { kind: 'objects' },
+ misc: { kind: 'weird' },
+ untagged: {},
+ });
+ expect(grouped.characters).toEqual(['heroes']);
+ expect(grouped.places).toEqual(['cities']);
+ expect(grouped.objects).toEqual(['loot']);
+ expect(grouped.other.sort()).toEqual(['misc', 'untagged']);
+ });
+
+ it('tolerates empty / missing input', () => {
+ expect(groupBucketsByKind()).toEqual({ characters: [], places: [], objects: [], other: [] });
+ });
+});
+
+describe('universeBuilderShared — normalizeCategoryKey', () => {
+ it('slugifies to lowercase underscore keys', () => {
+ expect(normalizeCategoryKey(' Heroes & Villains ')).toBe('heroes_and_villains');
+ expect(normalizeCategoryKey('Deep Space!!')).toBe('deep_space');
+ expect(normalizeCategoryKey('__weird__')).toBe('weird');
+ });
+
+ it('returns empty string for blank / nullish input', () => {
+ expect(normalizeCategoryKey('')).toBe('');
+ expect(normalizeCategoryKey(null)).toBe('');
+ expect(normalizeCategoryKey('!!!')).toBe('');
+ });
+});
+
+describe('universeBuilderShared — humanizeCategory', () => {
+ it('uses the label table when present', () => {
+ expect(humanizeCategory('landscapes')).toBe('Landscapes');
+ });
+
+ it('title-cases unknown keys', () => {
+ expect(humanizeCategory('deep_space')).toBe('Deep Space');
+ expect(humanizeCategory('')).toBe('');
+ });
+});
+
+describe('universeBuilderShared — draft categories', () => {
+ it('ensureDraftCategories seeds the world defaults and preserves overrides', () => {
+ const out = ensureDraftCategories({ heroes: { variations: [{ label: 'x' }] } });
+ expect(out.heroes.variations).toHaveLength(1);
+ // Seeded default keys exist too.
+ expect(Object.keys(out).length).toBeGreaterThan(1);
+ });
+
+ it('getCategoryKeys dedupes normalized keys', () => {
+ const keys = getCategoryKeys({ Heroes: {}, heroes: {}, 'Deep Space': {} });
+ expect(keys).toContain('heroes');
+ expect(keys).toContain('deep_space');
+ expect(keys.filter((k) => k === 'heroes')).toHaveLength(1);
+ });
+});
+
+describe('universeBuilderShared — compositeKindLabel', () => {
+ it('maps a known kind and falls back to reference sheet', () => {
+ expect(compositeKindLabel('world_pitch_poster')).toBe('World pitch poster');
+ expect(compositeKindLabel('nonsense')).toBe('Reference sheet');
+ expect(COMPOSITE_BOARD_KINDS.length).toBeGreaterThan(0);
+ });
+});
diff --git a/client/src/pages/UniverseBuilder.jsx b/client/src/pages/UniverseBuilder.jsx
index 9ad47bc23e..5b41f13a3b 100644
--- a/client/src/pages/UniverseBuilder.jsx
+++ b/client/src/pages/UniverseBuilder.jsx
@@ -10,19 +10,18 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Link, useLocation, useParams, useSearchParams } from 'react-router-dom';
import {
- Plus, Trash2, Sparkles, Wand2, Loader2, Save, FolderOpen,
+ Plus, Trash2, Sparkles, Wand2, Loader2, Save,
Edit3, X, MessageSquarePlus, Play, Lock, Unlock,
ArrowUpCircle, ArrowLeft,
BookOpen, Users, MapPin, Package, Layers, ImagePlus, FolderTree, Images,
} from 'lucide-react';
import toast from '../components/ui/Toast';
import InlineConfirmRow from '../components/ui/InlineConfirmRow';
-import { formatDateTime } from '../utils/formatters';
import {
listUniverses, getUniverse, createUniverse, updateUniverse, deleteUniverse, expandUniverse,
generateCategoryVariations, promoteVariationToCanon, autoSortBuckets,
renderWorld, listWorldRuns, getProviders, refineWorldPrompts, WORLD_CATEGORIES,
- WORLD_CATEGORY_KEY_MAX, COMPOSITE_PROMPT_MAX, WORLD_LOGLINE_MAX,
+ WORLD_CATEGORY_KEY_MAX, WORLD_LOGLINE_MAX,
WORLD_PREMISE_MAX, WORLD_STYLE_NOTES_MAX, WORLD_LOCKABLE_FIELDS,
ensureInfluences, isInfluenceLockField,
listImageModels, listLorasFull, getSettings,
@@ -32,12 +31,14 @@ import EntityCombobox from '../components/EntityCombobox';
import { useLocalStoragePersisted } from '../hooks/useLocalStorageBool';
import useUniverseAction from '../hooks/useUniverseAction';
import { useUniverseNav } from '../hooks/useUniverseNav';
+import { useRenderJobQueue } from '../hooks/useRenderJobQueue';
import InfluenceChipsInput from '../components/universeBuilder/InfluenceChipsInput';
+import CompositeSheetsEditor from '../components/universeBuilder/CompositeSheetsEditor';
+import BucketChipStrip from '../components/universeBuilder/BucketChipStrip';
+import RenderTab from '../components/universeBuilder/RenderTab';
import ProviderModelSelector from '../components/ProviderModelSelector';
import VisionProviderPicker from '../components/universe/VisionProviderPicker';
import GalleryImagePicker from '../components/imageGen/GalleryImagePicker';
-import ImageGenSettingsForm from '../components/imageGen/ImageGenSettingsForm';
-import { RUNNER_FAMILIES, loraCompatKey } from '../lib/runnerFamilies';
import ShareToButton from '../components/sharing/ShareToButton';
import SyncToPeerButton from '../components/sharing/SyncToPeerButton';
import OriginBadge from '../components/sharing/OriginBadge';
@@ -55,7 +56,7 @@ import { listImageGallery } from '../services/apiImageVideo';
import TabPills from '../components/ui/TabPills';
import { deriveAvailableBackends, IMAGE_GEN_MODE } from '../lib/imageGenBackends';
import { PIPELINE_IMAGE_DEFAULTS, readPipelineImageSettings } from '../lib/pipelineImageDefaults';
-import { hasCanonDescriptorContent, descriptorForCanonEntry } from '../lib/canonPrompt';
+import { descriptorForCanonEntry } from '../lib/canonPrompt';
import { listSheetPointers } from '../lib/sheetPointers';
import { upsertByIdPrepend } from '../lib/upsertByIdPrepend';
import { sameJsonShape } from '../lib/sameJsonShape';
@@ -63,62 +64,16 @@ import { capImageRefs } from '../lib/bibleLimits';
import {
mergeVariations, mergeCanonByName, mergeExpandIntoDraft, extractPreservedFromDraft,
} from '../lib/universeBuilderExpand';
-
-const CATEGORY_LABELS = {
- landscapes: 'Landscapes',
- environments: 'Environments',
- structures: 'Structures',
- vehicles: 'Vehicles',
-};
-
-// Tab order in the Universe Builder. Bible / Composites / Render are always
-// visible; the three canon trunks (Cast / Places / Objects) render even when
-// empty so the user has a discoverable target for canon+variation work; Other
-// only renders when at least one un-kinded bucket exists.
-const TAB_BIBLE = 'bible';
-const TAB_CAST = 'cast';
-const TAB_PLACES = 'places';
-const TAB_OBJECTS = 'objects';
-const TAB_OTHER = 'other';
-const TAB_COMPOSITES = 'composites';
-const TAB_RENDER = 'render';
-// Pseudo-bucket key for the canon-only view inside a trunk. Overloads
-// `?bucket=` (alongside real bucket keys) AND a `promptMode` value on the
-// render route; same string in both contexts to keep the contract consistent.
-const BUCKET_CANON = 'canon';
-// `kind` doubles as the canon-array key on the universe (`draft[kind]`) and
-// the canon-trunk identifier the server's `canonSelection` schema accepts.
-const TRUNK_TABS = [
- { id: TAB_CAST, kind: 'characters', label: 'Cast', icon: Users },
- { id: TAB_PLACES, kind: 'places', label: 'Places', icon: MapPin },
- { id: TAB_OBJECTS, kind: 'objects', label: 'Objects', icon: Package },
-];
-const TRUNK_BY_ID = Object.fromEntries(TRUNK_TABS.map((t) => [t.id, t]));
-const TRUNK_BY_KIND = Object.fromEntries(TRUNK_TABS.map((t) => [t.kind, t]));
-
-// Group category buckets by their `kind` tag. Buckets with an unknown / missing
-// kind fall into the `other` bin — that bin drives whether the Other tab shows.
-const groupBucketsByKind = (categories = {}) => {
- const out = { characters: [], places: [], objects: [], other: [] };
- for (const [key, bucket] of Object.entries(categories || {})) {
- const kind = bucket?.kind || 'other';
- if (out[kind]) out[kind].push(key);
- else out.other.push(key);
- }
- return out;
-};
-
-const normalizeCategoryKey = (raw) => (raw || '')
- .trim()
- .toLowerCase()
- .replace(/&/g, ' and ')
- .replace(/[^a-z0-9]+/g, '_')
- .replace(/^_+|_+$/g, '')
- .replace(/_{2,}/g, '_')
- .slice(0, WORLD_CATEGORY_KEY_MAX);
-
-const humanizeCategory = (key) => CATEGORY_LABELS[key]
- || key.replace(/_/g, ' ').replace(/\b\w/g, (ch) => ch.toUpperCase());
+import {
+ TAB_BIBLE, TAB_CAST, TAB_PLACES, TAB_OBJECTS, TAB_OTHER, TAB_COMPOSITES, TAB_RENDER,
+ BUCKET_CANON, TRUNK_TABS, TRUNK_BY_ID, TRUNK_BY_KIND,
+ groupBucketsByKind, normalizeCategoryKey, humanizeCategory,
+ ensureDraftCategories, getCategoryKeys, DEFAULT_RENDER_OPTS,
+} from '../lib/universeBuilderShared';
+import {
+ totalVariationCount, canonEntryHasContent,
+ renderPromptCount, scopedPromptCount,
+} from '../lib/universeBuilderCounts';
// Toast summary for handleExpand — same body, different tail depending on
// whether the auto-save succeeded.
@@ -133,53 +88,6 @@ const expandToast = ({ variationCount, sheetCount, addedCanonCount, saved }) =>
return `${base} — ${saved ? 'saved' : 'review then Save'}`;
};
-const ensureDraftCategories = (categories = {}) => ({
- ...Object.fromEntries(WORLD_CATEGORIES.map((c) => [c, { variations: [] }])),
- ...(categories || {}),
-});
-
-const getCategoryKeys = (categories = {}) => {
- const seen = new Set();
- const keys = [];
- for (const key of [...WORLD_CATEGORIES, ...Object.keys(categories || {})]) {
- const normalized = normalizeCategoryKey(key);
- if (!normalized || seen.has(normalized)) continue;
- seen.add(normalized);
- keys.push(normalized);
- }
- return keys;
-};
-
-// Default per-render knobs. Mirrors the Image Gen page's default chip.
-const DEFAULT_RENDER_OPTS = {
- width: 1024,
- height: 1024,
- steps: 30,
- guidance: '',
- cfgScale: 7,
- quantize: '8',
- modelId: '',
- mode: '',
- promptMode: 'variations',
- batchPerVariation: 1,
- // Extended Image Gen surface used by the batch render form. Server accepts
- // these as optional patches on top of the universe's stored influences.
- seed: '',
- negativePrompt: '',
- extraStyle: '',
- stylePreset: null,
- loras: [],
-};
-
-// Mirror of COMPOSITE_SHEET_KINDS in server/services/universeBuilder.js — keep
-// in sync when adding kinds.
-const COMPOSITE_BOARD_KINDS = [
- { value: 'reference_sheet', label: 'Reference sheet' },
- { value: 'world_pitch_poster', label: 'World pitch poster' },
-];
-
-const compositeKindLabel = (kind) => COMPOSITE_BOARD_KINDS.find((k) => k.value === kind)?.label || 'Reference sheet';
-
const emptyTemplate = () => ({
name: '',
starterPrompt: '',
@@ -346,45 +254,12 @@ export default function UniverseBuilder() {
const [saving, setSaving] = useState(false);
const [expanding, setExpanding] = useState(false);
const [rendering, setRendering] = useState(false);
- // entryId → jobId[] queue for any row with one or more in-flight renders.
- // Stores an ARRAY because `batchPerVariation > 1` (or any path that queues
- // multiple jobs for the same entry) lands several jobIds against the same
- // entryId — a scalar would overwrite siblings and the spinner would clear
- // as soon as the first finished, even though others are still running.
- // Canon entries are included here too (the batch `/render` route compiles
- // canon prompts when the user picks "all" / "canon" mode, and those jobs
- // don't go through `UniverseCanonSection.renderingJobs`).
- const [pendingByEntryId, setPendingByEntryId] = useState({});
- // First-jobId-per-entry view for components that just need a single
- // subscription per row (MediaJobThumb takes one jobId). When the head
- // finishes we shift it off and the next jobId in the queue takes over.
- const pendingHeadByEntryId = useMemo(() => {
- const out = {};
- for (const [entryId, jobs] of Object.entries(pendingByEntryId)) {
- if (Array.isArray(jobs) && jobs.length > 0) out[entryId] = jobs[0];
- }
- return out;
- }, [pendingByEntryId]);
- // Remove a specific completed jobId from the entry's queue (preferred path
- // — handlers know which jobId finished). When called without a jobId
- // (failure paths that want to bail entirely on the entry), drops every
- // pending job for that entry.
- const clearPendingForEntry = useCallback((entryId, jobId = null) => {
- if (!entryId) return;
- setPendingByEntryId((prev) => {
- const jobs = prev[entryId];
- if (!Array.isArray(jobs) || jobs.length === 0) return prev;
- const next = { ...prev };
- if (jobId == null) {
- delete next[entryId];
- return next;
- }
- const remaining = jobs.filter((j) => j !== jobId);
- if (remaining.length === 0) delete next[entryId];
- else next[entryId] = remaining;
- return next;
- });
- }, []);
+ // Per-entry render-job queue (variations + canon). Extracted to
+ // useRenderJobQueue (#2374) — see the hook for the array-per-entry / head-map
+ // rationale and the variation/canon-only enqueue filter.
+ const {
+ pendingHeadByEntryId, clearPendingForEntry, enqueueEntryJobs,
+ } = useRenderJobQueue();
const [providers, setProviders] = useState([]);
const [activeProviderId, setActiveProviderId] = useState(null);
// Image-gen plumbing for the batch-render form (reused from Image Gen).
@@ -1261,18 +1136,7 @@ export default function UniverseBuilder() {
// renders on the same row should all stay tracked. Each completion
// shifts its jobId out (see `clearPendingForEntry`); the head jobId
// is the one MediaJobThumb subscribes to at any moment.
- if (Array.isArray(result.entryJobs) && result.entryJobs.length > 0) {
- setPendingByEntryId((prev) => {
- const next = { ...prev };
- for (const { jobId, entryRef } of result.entryJobs) {
- if (!jobId || !entryRef?.id) continue;
- if (entryRef.kind !== 'variation' && entryRef.kind !== 'canon') continue;
- const existing = Array.isArray(next[entryRef.id]) ? next[entryRef.id] : [];
- next[entryRef.id] = [...existing, jobId];
- }
- return next;
- });
- }
+ enqueueEntryJobs(result.entryJobs);
toast.success(`Queued ${result.promptCount} renders → "${result.collectionName}"`);
const updated = await listWorldRuns(selectedId).catch(() => runs);
setRuns(updated);
@@ -1951,347 +1815,6 @@ export default function UniverseBuilder() {
);
}
-function totalVariationCount(world) {
- return getCategoryKeys(world.categories).reduce((n, c) => n + (world.categories?.[c]?.variations?.length || 0), 0);
-}
-
-// Mirror the server's synthesizeCanonPrompt skip rule: entries with no
-// identity-anchor (name / slugline / prompt) AND no descriptive content for
-// the kind compile to an empty seed and get skipped at render time. The
-// "Render N images" button count must not overshoot what the server will
-// actually enqueue.
-const hasNonBlankString = (v) => typeof v === 'string' && v.trim().length > 0;
-
-const canonEntryHasContent = (e, kind) => {
- if (!e) return false;
- if (hasNonBlankString(e.prompt)) return true;
- // Identifier anchors per kind — places allow slugline-only entries (bible
- // sanitizer); characters/objects ignore stray slugline. Mirrors server
- // synthesizeCanonPrompt's identifier-seed rule.
- if (hasNonBlankString(e.name)) return true;
- if (kind === 'places' && hasNonBlankString(e.slugline)) return true;
- return hasCanonDescriptorContent(kind, e);
-};
-
-const countCanonWithContent = (world, kind) =>
- (Array.isArray(world?.[kind]) ? world[kind] : []).filter((e) => canonEntryHasContent(e, kind)).length;
-
-function renderPromptCount(world, promptMode = 'variations') {
- if (promptMode === 'variations') return totalVariationCount(world);
- const sheets = world.compositeSheets?.length || 0;
- if (promptMode === 'sheets') return sheets;
- const canon = countCanonWithContent(world, 'characters')
- + countCanonWithContent(world, 'places')
- + countCanonWithContent(world, 'objects');
- if (promptMode === 'canon') return canon;
- return totalVariationCount(world) + sheets + canon;
-}
-
-// Mirrors the server's compilePrompts for selection/sheetSelection/canonSelection
-// so an inline "Render" button can disable itself + show an accurate count
-// without a round trip.
-//
-// Defaulting rules — mirror server/services/universeBuilder.js compilePrompts:
-// - sheets/all + no sheetSelection → render every sheet (server defaults to 'all')
-// - variations/all + no selection → render every category (server falls back
-// to a full category map via getWorldCategoryKeys)
-// - canon/all + no canonSelection → render NOTHING (server gates on a non-null
-// canonSelection; missing key skips the trunk entirely)
-// Canon entries are filtered through `canonEntryHasContent(kind)` since the
-// server skips entries whose synthesized seed is empty.
-function scopedPromptCount(world, scope) {
- if (!scope) return 0;
- const mode = scope.promptMode || 'variations';
- let n = 0;
- if (mode === 'sheets' || mode === 'all') {
- const sheets = world.compositeSheets || [];
- if (scope.sheetSelection === 'all' || scope.sheetSelection === undefined || scope.sheetSelection === null) {
- // Both `sheets` and `all` default to every sheet when sheetSelection is
- // omitted — server: `options.sheetSelection || 'all'`.
- n += sheets.length;
- } else if (Array.isArray(scope.sheetSelection)) {
- const set = new Set(scope.sheetSelection.map((s) => s.toLowerCase()));
- n += sheets.filter((s) => set.has((s.label || '').toLowerCase())).length;
- }
- }
- if (mode === 'variations' || mode === 'all') {
- if (scope.selection) {
- for (const [cat, pick] of Object.entries(scope.selection)) {
- const vars = world.categories?.[cat]?.variations || [];
- if (pick === 'all') n += vars.length;
- else if (Array.isArray(pick)) {
- const labels = new Set(pick.map((p) => p.toLowerCase()));
- n += vars.filter((v) => labels.has((v.label || '').toLowerCase())).length;
- }
- }
- } else {
- // No selection ⇒ server treats this as "every category, all variations".
- n += totalVariationCount(world);
- }
- }
- if (mode === 'canon' || mode === 'all') {
- if (scope.canonSelection) {
- for (const trunk of ['characters', 'places', 'objects']) {
- const pick = scope.canonSelection[trunk];
- if (!pick) continue;
- const entries = Array.isArray(world[trunk]) ? world[trunk] : [];
- const withContent = entries.filter((e) => canonEntryHasContent(e, trunk));
- if (pick === 'all') n += withContent.length;
- else if (Array.isArray(pick)) {
- const needles = new Set(pick.map((p) => p.toLowerCase()));
- // Mirror server: slugline matching is places-only — name is the
- // shared anchor for characters/objects.
- n += withContent.filter((e) => {
- const name = (e.name || '').toLowerCase();
- if (needles.has(name)) return true;
- if (trunk === 'places') {
- const slug = (e.slugline || '').toLowerCase();
- if (needles.has(slug)) return true;
- }
- return false;
- }).length;
- }
- }
- }
- }
- return n;
-}
-
-function CompositeSheetsEditor({ sheets, onChange, canRender = false, onRender = null }) {
- const [adding, setAdding] = useState(false);
- const [newKind, setNewKind] = useState('reference_sheet');
- const [newLabel, setNewLabel] = useState('');
- const [newPrompt, setNewPrompt] = useState('');
- const [editIdx, setEditIdx] = useState(null);
- const [editKind, setEditKind] = useState('reference_sheet');
- const [editLabel, setEditLabel] = useState('');
- const [editPrompt, setEditPrompt] = useState('');
-
- const addSheet = () => {
- const label = newLabel.trim();
- const prompt = newPrompt.trim();
- if (!label || !prompt) return;
- // Stamp `locked: true` up front so the in-draft row matches the
- // lock-by-default contract before the next save round-trips through
- // sanitizeCompositeSheet — otherwise the bulk-toggle's locked-count
- // gate would treat freshly-added boards as unlocked.
- onChange([...sheets, { kind: newKind, label: label.slice(0, 120), prompt: prompt.slice(0, COMPOSITE_PROMPT_MAX), locked: true }]);
- setNewKind('reference_sheet');
- setNewLabel('');
- setNewPrompt('');
- setAdding(false);
- };
-
- const removeAt = (idx) => onChange(sheets.filter((_, i) => i !== idx));
-
- // Sheets default to locked at the sanitizer (locked-by-default contract);
- // persist an unlock as explicit `false` so it survives the next read.
- const toggleLockAt = (idx) => onChange(sheets.map((s, i) => {
- if (i !== idx) return s;
- return { ...s, locked: !s.locked };
- }));
-
- const startEdit = (idx, sheet) => {
- setEditIdx(idx);
- setEditKind(sheet.kind || 'reference_sheet');
- setEditLabel(sheet.label);
- setEditPrompt(sheet.prompt);
- };
-
- const saveEdit = () => {
- const label = editLabel.trim();
- const prompt = editPrompt.trim();
- if (!label || !prompt) return;
- const next = [...sheets];
- // Preserve `id`, `locked`, `imageRefs` — see VariationCard.saveEdit for
- // the rationale. The editor only owns kind/label/prompt.
- next[editIdx] = {
- ...next[editIdx],
- kind: editKind,
- label: label.slice(0, 120),
- prompt: prompt.slice(0, COMPOSITE_PROMPT_MAX),
- };
- onChange(next);
- setEditIdx(null);
- };
-
- const setAllSheetsLocked = (nextLocked) =>
- onChange(sheets.map((s) => (s?.locked === nextLocked ? s : { ...s, locked: nextLocked })));
- const sheetsLockedCount = sheets.filter((s) => s?.locked === true).length;
- const allSheetsLocked = sheets.length > 0 && sheetsLockedCount === sheets.length;
-
- return (
- <section className="bg-port-card border border-port-border rounded p-4 flex flex-col gap-3">
- <div className="flex items-center justify-between gap-2">
- <h2 className="text-sm font-semibold text-white">
- Composite boards
- <span className="ml-2 text-xs text-gray-500">{sheets.length}</span>
- </h2>
- <div className="flex items-center gap-1">
- {sheets.length > 0 && (
- <button
- onClick={() => setAllSheetsLocked(!allSheetsLocked)}
- title={allSheetsLocked
- ? 'Unlock all composite boards — Expand may overwrite them'
- : 'Lock all composite boards — Expand will preserve them'}
- aria-label={allSheetsLocked ? 'Unlock all composite boards' : 'Lock all composite boards'}
- aria-pressed={allSheetsLocked}
- className={`p-1 rounded ${
- allSheetsLocked
- ? 'text-port-accent hover:bg-port-accent/20'
- : 'text-gray-500 hover:text-gray-300'
- }`}
- >
- {allSheetsLocked ? <Lock size={14} /> : <Unlock size={14} />}
- </button>
- )}
- <button
- onClick={() => setAdding((v) => !v)}
- className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 text-port-accent rounded flex items-center gap-1 min-h-[40px] sm:min-h-0"
- >
- <Plus size={12} /> Add
- </button>
- </div>
- </div>
- {adding && (
- <div className="bg-port-bg border border-port-border rounded p-2 flex flex-col gap-2">
- <select
- value={newKind}
- onChange={(e) => setNewKind(e.target.value)}
- className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm min-h-[40px]"
- >
- {COMPOSITE_BOARD_KINDS.map((kind) => (
- <option key={kind.value} value={kind.value}>{kind.label}</option>
- ))}
- </select>
- <input
- value={newLabel}
- onChange={(e) => setNewLabel(e.target.value)}
- placeholder={newKind === 'world_pitch_poster' ? 'World summary concept pitch poster' : 'Gas-Giant Drifters costume sheet'}
- className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm"
- maxLength={120}
- />
- <textarea
- value={newPrompt}
- onChange={(e) => setNewPrompt(e.target.value)}
- placeholder={newKind === 'world_pitch_poster'
- ? 'Create a cinematic world summary concept pitch poster with a hero panorama, inset environments, cultures, creatures, visual language, palette, materials, and theme icons...'
- : 'Create a clean illustrated costume reference sheet...'}
- className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm"
- rows={6}
- maxLength={COMPOSITE_PROMPT_MAX}
- />
- <div className="flex items-center gap-2">
- <button
- onClick={addSheet}
- disabled={!newLabel.trim() || !newPrompt.trim()}
- className="text-xs px-2 py-1 bg-port-accent hover:bg-port-accent/90 disabled:opacity-50 text-white rounded min-h-[40px] sm:min-h-0"
- >
- Save
- </button>
- <button
- onClick={() => {
- setAdding(false);
- setNewKind('reference_sheet');
- setNewLabel('');
- setNewPrompt('');
- }}
- className="text-xs px-2 py-1 bg-port-bg hover:bg-port-border text-gray-300 rounded min-h-[40px] sm:min-h-0"
- >
- Cancel
- </button>
- </div>
- </div>
- )}
- {sheets.length === 0 ? (
- <p className="text-xs text-gray-500">No composite boards yet.</p>
- ) : (
- <ul className="flex flex-col gap-1.5 max-h-96 overflow-y-auto">
- {sheets.map((sheet, idx) => (
- <li key={`${sheet.label}-${idx}`} className={`bg-port-bg border rounded p-2 text-sm ${sheet.locked ? 'border-port-accent/50' : 'border-port-border'}`}>
- {editIdx === idx ? (
- <div className="flex flex-col gap-1">
- <select
- value={editKind}
- onChange={(e) => setEditKind(e.target.value)}
- className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm min-h-[40px]"
- >
- {COMPOSITE_BOARD_KINDS.map((kind) => (
- <option key={kind.value} value={kind.value}>{kind.label}</option>
- ))}
- </select>
- <input
- value={editLabel}
- onChange={(e) => setEditLabel(e.target.value)}
- className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm"
- maxLength={120}
- />
- <textarea
- value={editPrompt}
- onChange={(e) => setEditPrompt(e.target.value)}
- rows={8}
- className="bg-port-card border border-port-border rounded px-2 py-1 text-white text-sm"
- maxLength={COMPOSITE_PROMPT_MAX}
- />
- <div className="flex gap-2">
- <button onClick={saveEdit} className="text-xs px-2 py-1 bg-port-accent text-white rounded min-h-[40px] sm:min-h-0">Save</button>
- <button onClick={() => setEditIdx(null)} className="text-xs px-2 py-1 bg-port-bg text-gray-300 rounded min-h-[40px] sm:min-h-0">Cancel</button>
- </div>
- </div>
- ) : (
- <div className="flex items-start justify-between gap-2">
- <div className="flex-1 min-w-0">
- <div className="flex items-center gap-2">
- <div className="text-white font-medium truncate">{sheet.label}</div>
- <span className="shrink-0 text-[10px] uppercase tracking-wide px-1.5 py-0.5 rounded bg-port-accent/10 text-port-accent border border-port-accent/20">
- {compositeKindLabel(sheet.kind)}
- </span>
- </div>
- <div className="text-xs text-gray-400 line-clamp-3">{sheet.prompt}</div>
- </div>
- <div className="flex items-center gap-1 shrink-0">
- {onRender && (
- <button
- onClick={() => onRender(sheet)}
- disabled={!canRender}
- className="p-1 text-gray-400 hover:text-port-accent disabled:opacity-30 disabled:cursor-not-allowed rounded"
- title={canRender ? 'Render this board' : 'Save the world and configure a render backend to enable'}
- >
- <Play size={14} />
- </button>
- )}
- <button
- onClick={() => toggleLockAt(idx)}
- className={`p-1 rounded ${sheet.locked ? 'text-port-accent hover:bg-port-accent/20' : 'text-gray-500 hover:text-gray-300'}`}
- title={sheet.locked ? 'Locked — AI expand will preserve this board' : 'Lock this board against AI expand'}
- aria-pressed={!!sheet.locked}
- >
- {sheet.locked ? <Lock size={14} /> : <Unlock size={14} />}
- </button>
- <button
- onClick={() => startEdit(idx, sheet)}
- className="p-1 text-gray-400 hover:text-port-accent rounded"
- title="Edit"
- >
- <Edit3 size={14} />
- </button>
- <button
- onClick={() => removeAt(idx)}
- className="p-1 text-gray-400 hover:text-port-error rounded"
- title="Remove"
- >
- <X size={14} />
- </button>
- </div>
- </div>
- )}
- </li>
- ))}
- </ul>
- )}
- </section>
- );
-}
const GENERATE_PRESETS = [3, 5, 10];
const GENERATE_CUSTOM_MIN = 1;
@@ -3121,58 +2644,6 @@ function BibleTab({
// pseudo-bucket that clears `?bucket=`. Each real bucket key gets its own
// chip; clicking toggles it on/off (toggle-off returns to All). Designed so
// it works one-handed on mobile (38px tap targets, wraps to multiple lines).
-function BucketChipStrip({ buckets, activeBucket, setBucket, showAll = true, extraChips = [] }) {
- if (buckets.length === 0 && extraChips.length === 0) return null;
- return (
- <div className="flex flex-wrap items-center gap-1.5">
- {showAll && (
- <button
- type="button"
- onClick={() => setBucket('')}
- className={`px-2.5 py-1.5 rounded-full text-xs min-h-[32px] transition-colors ${
- !activeBucket
- ? 'bg-port-accent/25 text-port-accent border border-port-accent/40'
- : 'bg-port-bg text-gray-300 border border-port-border hover:border-gray-500'
- }`}
- >
- All
- </button>
- )}
- {extraChips.map((chip) => (
- <button
- key={chip.key}
- type="button"
- onClick={() => setBucket(chip.key)}
- className={`px-2.5 py-1.5 rounded-full text-xs min-h-[32px] transition-colors ${
- activeBucket === chip.key
- ? 'bg-port-accent/25 text-port-accent border border-port-accent/40'
- : 'bg-port-bg text-gray-300 border border-port-border hover:border-gray-500'
- }`}
- >
- {chip.label}
- </button>
- ))}
- {buckets.map((bucket) => {
- const active = activeBucket === bucket;
- return (
- <button
- key={bucket}
- type="button"
- onClick={() => setBucket(active ? '' : bucket)}
- className={`px-2.5 py-1.5 rounded-full text-xs min-h-[32px] transition-colors ${
- active
- ? 'bg-port-accent/25 text-port-accent border border-port-accent/40'
- : 'bg-port-bg text-gray-300 border border-port-border hover:border-gray-500'
- }`}
- >
- {humanizeCategory(bucket)}
- </button>
- );
- })}
- </div>
- );
-}
-
// Per-trunk view. Three modes driven by `?bucket=`:
// - blank (default "All"): renders canon + every variation under this trunk
// - BUCKET_CANON: renders only canon entries (via the existing UniverseCanonSection)
@@ -3421,282 +2892,3 @@ export function OtherTab({
</>
);
}
-
-function RenderTab({
- draft, selectedId, bucketsByKind, renderOpts, setRenderOpts,
- availableBackends, defaultMode, imageModels, availableLoras = [],
- handleRender, rendering, runs,
-}) {
- const currentModel = useMemo(
- () => imageModels.find((m) => m.id === renderOpts.modelId),
- [imageModels, renderOpts.modelId],
- );
- const currentRunnerFamily = currentModel?.runner || RUNNER_FAMILIES.MFLUX;
- const currentCompatKey = loraCompatKey(currentModel);
- // Memoize the counts that drive button labels + disable states. Drafts can
- // be large (full canon + variations + sheets) and ImageGenSettingsForm
- // re-renders RenderTab on every keystroke into the per-batch fields.
- const counts = useMemo(() => {
- // Mirror server-side compile skip rules — `renderPromptCount` already
- // filters canon via `canonEntryHasContent`, so each trunk's counts and
- // the "Render everything" total agree with what the server enqueues.
- const totalSheets = draft.compositeSheets?.length || 0;
- const totalVariations = totalVariationCount(draft);
- const totalCanon = countCanonWithContent(draft, 'characters')
- + countCanonWithContent(draft, 'places')
- + countCanonWithContent(draft, 'objects');
- const otherBuckets = bucketsByKind?.other || [];
- const totalOtherVariations = otherBuckets.reduce(
- (n, k) => n + (draft.categories?.[k]?.variations?.length || 0),
- 0,
- );
- return {
- totalSheets,
- totalVariations,
- totalCanon,
- otherBuckets,
- totalOtherVariations,
- totalEverything: totalSheets + totalVariations + totalCanon,
- };
- }, [draft, bucketsByKind]);
- const { totalSheets, totalCanon, otherBuckets, totalOtherVariations, totalEverything } = counts;
- const perPrompt = renderOpts.batchPerVariation || 1;
-
- return (
- <>
- <section className="bg-port-card border border-port-border rounded p-4 flex flex-col gap-3">
- <h2 className="text-sm font-semibold text-white flex items-center gap-2">
- <FolderOpen size={16} className="text-port-accent" /> Batch render
- </h2>
- {availableBackends.length === 0 && (
- <p className="text-xs text-port-warning">
- Configure a local mflux Python path or enable Codex Imagegen in Settings → Image Gen
- to enable batch render.
- </p>
- )}
- <ImageGenSettingsForm
- value={{ ...renderOpts, mode: renderOpts.mode || defaultMode || IMAGE_GEN_MODE.LOCAL }}
- onChange={(next) => setRenderOpts(next)}
- models={imageModels}
- availableBackends={availableBackends}
- showLoras
- availableLoras={availableLoras}
- currentRunnerFamily={currentRunnerFamily}
- currentCompatKey={currentCompatKey}
- showStylePreset
- />
- <div className="grid grid-cols-2 sm:grid-cols-3 gap-3">
- <div>
- <label htmlFor="world-render-batch" className="block text-xs font-medium text-gray-400 mb-1">Renders per prompt</label>
- <input
- id="world-render-batch"
- type="number" min={1} max={20}
- value={renderOpts.batchPerVariation ?? 1}
- onChange={(e) => {
- const n = Number(e.target.value);
- setRenderOpts((r) => ({ ...r, batchPerVariation: Number.isFinite(n) && n > 0 ? n : 1 }));
- }}
- className="w-full bg-port-bg border border-port-border rounded-lg px-2 py-2 text-sm text-white focus:outline-none focus:border-port-accent"
- />
- </div>
- </div>
- <div className="flex items-center gap-2 flex-wrap">
- <button
- onClick={() => handleRender({
- promptMode: 'all',
- // Both `scopedPromptCount` (client) and `compilePrompts` (server)
- // skip canon entirely when `canonSelection` is omitted. The button
- // label promises "everything", so we must explicitly select every
- // canon trunk — derived from TRUNK_TABS so the set stays in sync
- // when a new trunk is added.
- canonSelection: Object.fromEntries(TRUNK_TABS.map((t) => [t.kind, 'all'])),
- })}
- disabled={rendering || !selectedId || totalEverything === 0 || availableBackends.length === 0}
- className="px-4 py-2 bg-port-accent hover:bg-port-accent/90 disabled:opacity-50 text-white rounded flex items-center gap-2 min-h-[40px]"
- title="Render every canon entry + every variation + every composite board with these knobs"
- >
- {rendering ? <Loader2 size={16} className="animate-spin" /> : <Sparkles size={16} />}
- Render everything ({totalEverything * perPrompt} image{totalEverything * perPrompt === 1 ? '' : 's'})
- </button>
- <span className="text-[11px] text-gray-500">…or pick a narrower scope below.</span>
- </div>
- {!selectedId && <p className="text-xs text-gray-500">Save the world first to enable rendering.</p>}
- </section>
-
- <section className="bg-port-card border border-port-border rounded p-4 flex flex-col gap-3">
- <h3 className="text-sm font-semibold text-white">Render targets</h3>
- <p className="text-[11px] text-gray-500 -mt-1">
- Click a target to queue that scope immediately with the knobs above.
- </p>
- <div className="flex flex-col gap-2">
- {TRUNK_TABS.map((trunk) => {
- const buckets = bucketsByKind[trunk.kind] || [];
- // Use the synthesizable count for both the display label and the
- // "Bulk-render all" total so the button advertises the number that
- // will actually land on the server.
- const canonCount = countCanonWithContent(draft, trunk.kind);
- const variationCount = buckets.reduce((n, k) => n + (draft.categories?.[k]?.variations?.length || 0), 0);
- const total = canonCount + variationCount;
- if (total === 0) return null;
- const Icon = trunk.icon;
- return (
- <div key={trunk.id} className="border border-port-border rounded p-2 bg-port-bg flex flex-col gap-1.5">
- <div className="flex items-center justify-between gap-2 flex-wrap">
- <div className="flex items-center gap-2 text-sm text-gray-200">
- <Icon size={14} className="text-port-accent" />
- <span className="font-medium">{trunk.label}</span>
- <span className="text-[11px] text-gray-500">
- {canonCount} canon · {variationCount} variation{variationCount === 1 ? '' : 's'}
- </span>
- </div>
- <button
- type="button"
- onClick={() => handleRender({
- promptMode: 'all',
- selection: Object.fromEntries(buckets.map((b) => [b, 'all'])),
- canonSelection: canonCount > 0 ? { [trunk.kind]: 'all' } : undefined,
- // Trunk scope excludes composite sheets — see comment on
- // TrunkView's onBulkRenderTrunk for why this opt-out matters.
- sheetSelection: [],
- })}
- disabled={!selectedId || availableBackends.length === 0 || rendering}
- className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 disabled:opacity-30 disabled:cursor-not-allowed text-port-accent rounded min-h-[32px]"
- title={`Render every ${trunk.label.toLowerCase()} canon entry AND every variation under this trunk`}
- >
- Bulk-render all ({total})
- </button>
- </div>
- {buckets.length > 0 && (
- <div className="flex flex-wrap items-center gap-1 pl-5">
- {buckets.map((bucket) => {
- const count = draft.categories?.[bucket]?.variations?.length || 0;
- return (
- <button
- key={bucket}
- type="button"
- onClick={() => handleRender({ promptMode: 'variations', selection: { [bucket]: 'all' } })}
- disabled={count === 0 || !selectedId || availableBackends.length === 0 || rendering}
- className="text-[11px] px-1.5 py-0.5 bg-port-card border border-port-border hover:border-port-accent disabled:opacity-30 disabled:cursor-not-allowed text-gray-300 rounded"
- title={count === 0 ? 'No variations yet' : `Bulk-render ${humanizeCategory(bucket)} (${count})`}
- >
- {humanizeCategory(bucket)} ({count})
- </button>
- );
- })}
- </div>
- )}
- </div>
- );
- })}
- {otherBuckets.length > 0 && totalOtherVariations > 0 ? (
- <div className="border border-port-border rounded p-2 bg-port-bg flex flex-col gap-1.5">
- <div className="flex items-center justify-between gap-2 flex-wrap">
- <div className="flex items-center gap-2 text-sm text-gray-200">
- <FolderTree size={14} className="text-port-accent" />
- <span className="font-medium">Other</span>
- <span className="text-[11px] text-gray-500">
- {otherBuckets.length} bucket{otherBuckets.length === 1 ? '' : 's'}
- {' · '}{totalOtherVariations} variation{totalOtherVariations === 1 ? '' : 's'}
- </span>
- </div>
- <button
- type="button"
- onClick={() => handleRender({
- promptMode: 'variations',
- selection: Object.fromEntries(otherBuckets.map((b) => [b, 'all'])),
- })}
- disabled={!selectedId || availableBackends.length === 0 || rendering}
- className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 disabled:opacity-30 disabled:cursor-not-allowed text-port-accent rounded min-h-[32px]"
- title="Render every variation in every Other bucket"
- >
- Bulk-render all ({totalOtherVariations})
- </button>
- </div>
- <div className="flex flex-wrap items-center gap-1 pl-5">
- {otherBuckets.map((bucket) => {
- const count = draft.categories?.[bucket]?.variations?.length || 0;
- return (
- <button
- key={bucket}
- type="button"
- onClick={() => handleRender({ promptMode: 'variations', selection: { [bucket]: 'all' } })}
- disabled={count === 0 || !selectedId || availableBackends.length === 0 || rendering}
- className="text-[11px] px-1.5 py-0.5 bg-port-card border border-port-border hover:border-port-accent disabled:opacity-30 disabled:cursor-not-allowed text-gray-300 rounded"
- title={count === 0 ? 'No variations yet' : `Bulk-render ${humanizeCategory(bucket)} (${count})`}
- >
- {humanizeCategory(bucket)} ({count})
- </button>
- );
- })}
- </div>
- </div>
- ) : null}
- {totalCanon > 0 ? (
- <div className="border border-port-border rounded p-2 bg-port-bg flex items-center justify-between gap-2">
- <span className="text-sm text-gray-200 flex items-center gap-2">
- <Sparkles size={14} className="text-port-accent" />
- All canon
- <span className="text-[11px] text-gray-500">
- {totalCanon} entr{totalCanon === 1 ? 'y' : 'ies'}
- </span>
- </span>
- <button
- type="button"
- onClick={() => handleRender({
- promptMode: 'canon',
- canonSelection: Object.fromEntries(TRUNK_TABS.map((t) => [t.kind, 'all'])),
- })}
- disabled={!selectedId || availableBackends.length === 0 || rendering}
- className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 disabled:opacity-30 disabled:cursor-not-allowed text-port-accent rounded min-h-[32px]"
- >
- Bulk-render all canon
- </button>
- </div>
- ) : null}
- {totalSheets > 0 && (
- <div className="border border-port-border rounded p-2 bg-port-bg flex items-center justify-between gap-2">
- <span className="text-sm text-gray-200 flex items-center gap-2">
- <Layers size={14} className="text-port-accent" />
- Composite boards
- <span className="text-[11px] text-gray-500">{totalSheets} board{totalSheets === 1 ? '' : 's'}</span>
- </span>
- <button
- type="button"
- onClick={() => handleRender({ promptMode: 'sheets', sheetSelection: 'all' })}
- disabled={!selectedId || availableBackends.length === 0 || rendering}
- className="text-xs px-2 py-1 bg-port-accent/15 hover:bg-port-accent/25 disabled:opacity-30 disabled:cursor-not-allowed text-port-accent rounded min-h-[32px]"
- >
- Bulk-render composites
- </button>
- </div>
- )}
- </div>
- </section>
-
- {selectedId && runs.length > 0 && (
- <section className="bg-port-card border border-port-border rounded p-4">
- <h2 className="text-sm font-semibold text-white mb-2">Recent runs</h2>
- <ul className="flex flex-col gap-1">
- {runs.map((r) => (
- <li key={r.id} className="flex items-center justify-between text-sm text-gray-300 border-b border-port-border/40 py-1.5">
- <span className="truncate">
- <span className="text-gray-500">{formatDateTime(r.createdAt)} —</span>{' '}
- {r.promptCount} prompt{r.promptCount === 1 ? '' : 's'}
- </span>
- {r.collectionId && (
- <Link
- to={`/media/collections/${r.collectionId}`}
- className="text-xs text-port-accent hover:underline whitespace-nowrap"
- >
- Open collection →
- </Link>
- )}
- </li>
- ))}
- </ul>
- </section>
- )}
-
- </>
- );
-}