Free, open-source AI video generator for Mac. Run LTX-2.5 and MiniMax H3 (Hailuo) locally on Apple Silicon: text-to-video and image-to-video with sound, lip-sync, character LoRA training, songs from lyrics (YuE2) and image editing (Qwen-Image-Edit). One-click install via Pinokio. Offline: no cloud, no API key, no NVIDIA GPU.
Python ai-filmmakingai-videoai-video-generatorapple-siliconhailuoimage-to-videolip-synclocal-ailora-trainingltx-2ltx-2-5ltx-videomacosminimax-h3mlxmusic-generationoffline-aipinokiotext-to-videovideo-generation
release(v3.2.0): Ideogram Layout editor — on-canvas design tool + agent API
mrbizarro611722510b82
diff --git a/VERSION b/VERSIONindex 94ff29c..944880f 100644--- a/VERSION+++ b/VERSION@@ -1 +1 @@-3.1.1+3.2.0diff --git a/docs/AGENT_API.md b/docs/AGENT_API.mdnew file mode 100644index 0000000..f8a3d66--- /dev/null+++ b/docs/AGENT_API.md@@ -0,0 +1,170 @@+# Phosphene Agent Image API — Ideogram 4 layout++A clean JSON surface that lets an LLM agent (Claude, Codex, anything that speaks HTTP) **fully compose an Ideogram 4 image** — scene, on-image text, object regions, typographic styles, colors — without ever touching the model's finicky internal caption schema.++You describe a picture in plain terms: an overall **scene**, plus a list of **boxes** placed by fraction of the frame. Phosphene translates that into the strict Ideogram 4 caption for you, renders it, and hands back the image paths.++This complements the general [`docs/API.md`](API.md). The same panel process serves both.++## Server++- **Base URL:** `http://127.0.0.1:8199`+- **Process:** `mlx_ltx_panel.py` (repo root). Caption logic: `ideogram_caption.py`.+- **Auth:** none — loopback only.+- **Two endpoints:** `GET /image/agent/schema` (the contract) and `POST /image/agent` (validate / render).++> **Agents: read the schema first.** `GET /image/agent/schema` returns the full field-by-field spec, the caption rules, and two complete worked examples. An agent that reads only that response can compose a correct request. The summary below is for humans.++---++## The box model++Coordinates are **fractions of the frame** with a **top-left origin** — aspect-independent, so the same box lands in the same relative spot at any aspect ratio.++- `x`, `y` — the **top-left corner** of the box (0 = left / top, 1 = right / bottom).+- `w`, `h` — the box's **width / height** as a fraction of the frame.+- `(0,0)` is the top-left of the image; `(1,1)` is the bottom-right.+- Keep `x + w ≤ 1` and `y + h ≤ 1`; boxes that spill past an edge are clamped (you get a warning, not an error).++Internally each box becomes a bbox `[y_min, x_min, y_max, x_max]` of **row-first integers in 0..1000**. You never write that — the server does it.++### Request fields (`POST /image/agent`)++| Field | Type | Default | Notes |+|---|---|---|---|+| `scene` | string | — (required) | The overall background / setting / mood. Describe the *world*, not the text. |+| `boxes` | array | — (required) | List of placed elements. May be `[]` for a pure-scene render. |+| `render` | `design` \| `photo` | `design` | `design` = graphic/poster/vector look; `photo` = photographic. |+| `aspect` | `16:9` \| `1:1` \| `9:16` \| `4:3` \| `3:4` \| `21:9` | `16:9` | 16:9=1280×720, 1:1=1024×1024, 9:16=720×1280, 4:3=1024×768, 3:4=768×1024, 21:9=1280×544. |+| `quality` | `turbo` \| `default` \| `quality` | `turbo` | Sampler effort → `V4_TURBO_12` / `V4_DEFAULT_20` / `V4_QUALITY_48`. |+| `n` | int 1–4 | `1` | Candidate images to render. |+| `seed` | int | `-1` | Fixed seed for reproducibility; `-1` = random. For `n>1`: seed, seed+1, … |+| `validate_only` | bool | `false` | If true: don't render — return the built caption + warnings. |+| `wait` | bool | `true` | If true: block until the render finishes and return image paths. If false: enqueue and return immediately. |++### Box fields++| Field | Type | Applies to | Notes |+|---|---|---|---|+| `type` | `text` \| `object` | both | `text` renders literal words; `object` renders a described thing. |+| `x`, `y`, `w`, `h` | number 0–1 | both | Top-left origin, fractions of the frame (see above). |+| `text` | string | **text (required)** | The literal words to render. Empty text boxes are dropped. |+| `desc` | string | **object (required)**, text (optional) | What to render. For text boxes, overrides the auto style/align/color description. |+| `style` | `headline` \| `subhead` \| `body` \| `caps` \| `script` \| `serif` | text | Typographic feel. Default `headline`. |+| `align` | `left` \| `center` \| `right` | text | Default `center`. |+| `color` | `#RRGGBB` | text | Any case; normalized to uppercase. Bad values fall back to `#FFFFFF`. |++### Responses++- **`validate_only:true`** → `200 { "ok": true, "caption": {…}, "issues": [warnings]}`.+- **`wait:true`** (default) → `200 { "ok": true, "caption": {…}, "images": [{"path","url"}], "seconds": N, "job_id": "…"}`. Open each `url` on this same host to view the PNG.+- **`wait:false`** → `202 { "ok": true, "queued": true, "job_id": "…", "caption": {…}, "where_results_land": "…"}`.+- **Bad request** → `400 {"ok": false, "issues": […]}` (missing scene, object without `desc`, `n` out of range, unknown enum, a box that renders nothing, …).+- **GPU busy / render in flight** → `503` (transient — retry once it frees up). A `wait:true` request otherwise simply waits its turn in the queue.+- **Timeout** → `504` after 25 minutes (the job may still finish on the queue).++`issues` are human-readable. **Warnings** (off-frame clamp, heavy overlap, bad-color fallback, dropped empty text box) still render; the **fatal** subset returns `400`.++---++## Curl examples++### 1. Validate a layout without rendering (free, instant)++```bash+curl -s http://127.0.0.1:8199/image/agent \+ -H 'Content-Type: application/json' \+ -d '{+ "validate_only": true,+ "scene": "A moody vintage travel poster of a mountain lake at golden hour, warm muted palette",+ "render": "design",+ "aspect": "16:9",+ "boxes": [+ {"type":"text","x":0.08,"y":0.08,"w":0.84,"h":0.18,"text":"LAKE DISTRICT","style":"headline","align":"center","color":"#F5C518"},+ {"type":"text","x":0.30,"y":0.80,"w":0.40,"h":0.10,"text":"Est. 1951","style":"serif","align":"center","color":"#FFFFFF"},+ {"type":"object","x":0.55,"y":0.32,"w":0.35,"h":0.42,"desc":"A small wooden canoe drifting on the still lake"}+ ]+ }'+```++### 2. Render a full 16:9 poster and wait for the images++```bash+curl -s http://127.0.0.1:8199/image/agent \+ -H 'Content-Type: application/json' \+ -d '{+ "scene": "A moody vintage travel poster of a mountain lake at golden hour, warm muted palette",+ "render": "design",+ "aspect": "16:9",+ "quality": "turbo",+ "n": 1,+ "wait": true,+ "boxes": [+ {"type":"text","x":0.08,"y":0.08,"w":0.84,"h":0.18,"text":"LAKE DISTRICT","style":"headline","align":"center","color":"#F5C518"},+ {"type":"text","x":0.30,"y":0.80,"w":0.40,"h":0.10,"text":"Est. 1951","style":"serif","align":"center","color":"#FFFFFF"},+ {"type":"object","x":0.55,"y":0.32,"w":0.35,"h":0.42,"desc":"A small wooden canoe drifting on the still lake"}+ ]+ }'+# -> {"ok":true,"job_id":"...","caption":{...},"images":[{"path":".../cand_..._mflux.png","url":"/file?path=..."}],"seconds":...}+```++### 3. Submit a 9:16 label and return immediately (don't block)++```bash+curl -s http://127.0.0.1:8199/image/agent \+ -H 'Content-Type: application/json' \+ -d '{+ "scene": "A minimalist product label, soft off-white paper texture, centered composition",+ "render": "design",+ "aspect": "9:16",+ "quality": "default",+ "wait": false,+ "boxes": [+ {"type":"text","x":0.10,"y":0.42,"w":0.80,"h":0.16,"text":"COLD BREW","style":"caps","align":"center","color":"#0A0A0A"}+ ]+ }'+# -> 202 {"ok":true,"queued":true,"job_id":"...","caption":{...},"where_results_land":"..."}+```++### Read the contract++```bash+curl -s http://127.0.0.1:8199/image/agent/schema | python3 -m json.tool+```++---++## Render-time expectations++Wall time ≈ **one-time model cold-load + n × per-image sampler time**. The *first* Ideogram render after the panel starts is slower because the weights load once.++| `quality` | Sampler | Speed |+|---|---|---|+| `turbo` | `V4_TURBO_12` (~12 steps) | fastest |+| `default` | `V4_DEFAULT_20` (~20 steps) | moderate |+| `quality` | `V4_QUALITY_48` (~48 steps) | slowest, highest fidelity |++Renders are **serialized** with the panel's other GPU work (video, training, other image jobs). A `wait:true` request waits behind any in-flight job; if you'd rather not hold the connection, use `wait:false` and poll `GET /state` for your `job_id`. The `wait` timeout is 25 minutes.++> **Note (Ideogram weights gate):** Ideogram 4 downloads gated weights from Hugging Face on first use. If you get a 5xx about denied access, the panel operator needs a HF **Read** token set in Settings *and* the same account must have accepted the license at `huggingface.co/ideogram-ai/ideogram-4-fp8`. This is one-time, operator-side.++---++## System-prompt snippet for an agent author++Paste this into your agent's system prompt so it drives the endpoint correctly:++```text+You can generate composed images (posters, labels, text-on-image graphics) with Phosphene's Ideogram 4 agent API at http://127.0.0.1:8199.++ALWAYS start by calling GET /image/agent/schema once — it returns the exact field schema, the rules, and two worked examples. Compose against that.++To make an image, POST JSON to /image/agent with:+ - scene: a plain-language description of the overall background/mood (required).+ - boxes: a list of placed elements (required; [] for a pure-scene image). Each box positions itself by FRACTIONS of the frame with a TOP-LEFT origin: x,y is the top-left corner and w,h the size, each in [0,1]. (0,0)=top-left, (1,1)=bottom-right. Keep x+w<=1 and y+h<=1.+ * text box: {"type":"text","x","y","w","h","text": "<the literal words>", "style":"headline|subhead|body|caps|script|serif","align":"left|center|right","color":"#RRGGBB"}+ * object box: {"type":"object","x","y","w","h","desc":"<what to render there>"} (desc is required for objects)+ - render: "design" (graphic/poster) or "photo". aspect: "16:9"|"1:1"|"9:16"|"4:3"|"3:4"|"21:9". quality: "turbo"|"default"|"quality". n: 1-4.++Iterate cheaply with "validate_only": true — it returns the built caption plus warnings without rendering. When the layout looks right, POST again with "wait": true to render and receive image urls. Do NOT hand-write Ideogram's internal caption JSON — the server builds it from your scene+boxes. Max 6 text boxes.+```diff --git a/ideogram_caption.py b/ideogram_caption.pynew file mode 100644index 0000000..6cc2952--- /dev/null+++ b/ideogram_caption.py@@ -0,0 +1,498 @@+"""Agent-facing Ideogram 4 caption builder + validator (pure stdlib).++WHY THIS EXISTS+---------------+Ideogram 4 (via mflux) is trained on a finicky, order-sensitive structured+JSON "caption" — not a free-text prompt. The browser panel hides that schema+behind a drag-on-canvas editor whose serializer lives in JS+(`ideoBuildCaption` / `ideoSynthDesc` / `ideoRectToBbox` inside+mlx_ltx_panel.py). An LLM agent has no canvas, so this module is a FAITHFUL+Python port of that serializer: the agent describes a composition in simple,+human terms (a scene + a list of fractional boxes), and `build_caption`+emits the exact strict caption the upstream verifier+(mflux .../ideogram4_text_encoder/caption.py — `Ideogram4CaptionVerifier`)+expects.++THE AGENT-FACING SPEC+---------------------+A spec is a plain dict::++ {+ "scene": "<overall background description>", # required, str+ "boxes": [ <box>, ... ], # required list (may be empty)+ "render": "design" | "photo", # default "design"+ }++Each box is::++ {+ "type": "text" | "object", # required+ "x": 0..1, "y": 0..1, # top-left corner, FRACTIONS of the frame+ "w": 0..1, "h": 0..1, # size, fractions of the frame+ "text": "...", # REQUIRED for type=text (the literal words)+ "desc": "...", # optional refinement; REQUIRED for type=object+ "style": "headline|subhead|body|caps|script|serif", # text only, default headline+ "align": "left|center|right", # text only, default center+ "color": "#RRGGBB", # text only, default #FFFFFF+ }++Coordinates are aspect-INDEPENDENT fractions with a top-left origin, exactly+like the canvas: (x=0,y=0) is the top-left of the frame, (x=1,y=1) the+bottom-right. The caption stores each box as a bbox ``[y_min, x_min, y_max,+x_max]`` of ROW-FIRST integers in 0..1000 (the single source of bbox truth is+`rect_to_bbox`, a 1:1 port of the JS `ideoRectToBbox`).++PORT FIDELITY+-------------+`build_caption` matches the JS `ideoBuildCaption` key-for-key, with ONE+deliberate difference: it OMITS the optional `style_description` block. The+upstream verifier treats `style_description` as optional (it only validates it+``if "style_description" in caption``), and the block is brittle (its strict+key-order / photo-XOR-art_style rules are a common cause of WARN noise). The+render-mode distinction the JS encodes in `style_description` is preserved+where it actually matters for the agent: in `high_level_description`+("A graphic design ..." for design vs "A photographic image ..." for photo).+Everything else — synthesized `desc` wording, bbox math, per-element color+palette (UPPERCASE #RRGGBB, ≤5), the overall scene folded into+`compositional_deconstruction.background`, dropping empty text boxes,+insertion/key order — is a faithful port.+"""++from __future__ import annotations++import re+from typing import Any++# ---------------------------------------------------------------------------+# Constants (ported from the JS panel state)+# ---------------------------------------------------------------------------+MAX_TEXT_BOXES = 6 # IDEO_MAX_TEXT_BOXES — cap on text boxes+VALID_RENDER = ("design", "photo") # agent-facing render modes+VALID_TYPES = ("text", "object") # agent-facing box types+VALID_STYLES = ("headline", "subhead", "body", "caps", "script", "serif")+VALID_ALIGN = ("left", "center", "right")+VALID_ASPECT = ("16:9", "1:1", "9:16", "4:3", "3:4", "21:9")+VALID_QUALITY = ("turbo", "default", "quality")++# quality -> mflux Ideogram sampler preset (mirrors the panel's ideo_preset).+QUALITY_PRESET = {+ "turbo": "V4_TURBO_12",+ "default": "V4_DEFAULT_20",+ "quality": "V4_QUALITY_48",+}++_HEX_RE = re.compile(r"^#?[0-9a-fA-F]{6}$")+_STRICT_HEX_RE = re.compile(r"^#[0-9A-F]{6}$")++# ideoColorName map — exact UPPERCASE-keyed table from the JS.+_COLOR_NAMES = {+ "#FFFFFF": "white", "#0A0A0A": "near-black", "#000000": "black",+ "#F5C518": "gold", "#E63946": "red", "#2F81F7": "blue",+ "#3FB950": "green", "#8B5E3C": "brown", "#5A6B3A": "olive green",+}++# ideoSynthDesc style/align word tables — exact wording from the JS.+_STYLE_WORD = {+ "headline": "a bold headline",+ "subhead": "a medium-weight subheading",+ "body": "clean body text",+ "caps": "small-caps lettering",+ "script": "a flowing script",+ "serif": "an elegant serif",+}+_ALIGN_WORD = {"left": "left-aligned", "center": "centered", "right": "right-aligned"}+++# ---------------------------------------------------------------------------+# Low-level helpers — 1:1 ports of the JS primitives+# ---------------------------------------------------------------------------+def norm_hex(s: Any) -> str:+ """Port of `ideoNormHex`: accept '#RRGGBB' or 'RRGGBB' (any case),+ return UPPERCASE '#RRGGBB'; anything else → '#FFFFFF'."""+ s = str(s if s is not None else "").strip()+ if _HEX_RE.match(s):+ if not s.startswith("#"):+ s = "#" + s+ return s.upper()+ return "#FFFFFF"+++def _clamp01(v: float) -> float:+ return max(0.0, min(1.0, v))+++def rect_to_bbox(x: float, y: float, w: float, h: float) -> list[int]:+ """Port of `ideoRectToBbox`: {x,y,w,h} fractions → [y_min, x_min, y_max,+ x_max] ROW-FIRST integers in 0..1000, corners clamped to [0,1] first then+ min/max-ordered. This is the ONE place bbox math lives, matching the JS."""+ x0 = _clamp01(float(x)); y0 = _clamp01(float(y))+ x1 = _clamp01(float(x) + float(w)); y1 = _clamp01(float(y) + float(h))+ Y0 = round(y0 * 1000); X0 = round(x0 * 1000)+ Y1 = round(y1 * 1000); X1 = round(x1 * 1000)+ return [min(Y0, Y1), min(X0, X1), max(Y0, Y1), max(X0, X1)]+++def color_name(hex_in: Any) -> str:+ """Port of `ideoColorName`: named color for the swatch table, else+ 'the color #RRGGBB'."""+ h = norm_hex(hex_in)+ return _COLOR_NAMES.get(h, "the color " + h)+++def _synth_desc(box: dict) -> str:+ """Port of `ideoSynthDesc`. A manual `desc` (agent's box["desc"]) wins+ verbatim (trimmed). Objects with no desc fall back to the JS default.+ Text boxes synthesize "<Style>, <align>, in <color>." exactly like the JS."""+ manual = str(box.get("desc") or "").strip()+ if manual:+ return manual+ if box.get("type") == "object":+ return "An object in the scene."+ style = box.get("style") or "headline"+ align = box.get("align") or "center"+ style_word = _STYLE_WORD.get(style, "text")+ align_word = _ALIGN_WORD.get(align, "centered")+ cname = color_name(box.get("color"))+ # JS: `${styleWord[0].toUpperCase()}${styleWord.slice(1)}, ${alignWord}, in ${colorName}.`+ cap_style = style_word[0].upper() + style_word[1:]+ return f"{cap_style}, {align_word}, in {cname}."+++# ---------------------------------------------------------------------------+# build_caption — the serializer (agent spec -> strict caption dict)+# ---------------------------------------------------------------------------+def build_caption(spec: dict) -> dict:+ """Build the strict Ideogram 4 caption dict from an agent-facing spec.++ Faithful port of the JS `ideoBuildCaption`, OMITTING the optional+ `style_description` block (see module docstring). Key/insertion order is+ significant and matches the upstream verifier: root keys are+ ``high_level_description`` then ``compositional_deconstruction``; each+ element is ``type, bbox, [text], desc, [color_palette]``.++ Does NOT validate — call `validate_spec` first for human-readable issues.+ Tolerant by construction (clamps, normalizes hex, drops empty text) so it+ never raises on a well-typed dict.+ """+ if not isinstance(spec, dict):+ raise TypeError("spec must be a dict")++ render = (spec.get("render") or "design")+ render = render if render in VALID_RENDER else "design"+ background = str(spec.get("scene") or "").strip() or "A clean, simple background."+ boxes = spec.get("boxes") or []+ if not isinstance(boxes, list):+ boxes = []++ # high_level_description — fold up to 4 filled text strings, mirroring JS.+ text_boxes = [b for b in boxes if isinstance(b, dict) and b.get("type") == "text"]+ filled = [str(b.get("text") or "").strip() for b in text_boxes if str(b.get("text") or "").strip()]+ hl_bits: list[str] = []+ if filled:+ quoted = ", ".join('"' + t + '"' for t in filled[:4])+ hl_bits.append("text reading " + quoted)+ feat = (" featuring " + "; ".join(hl_bits)) if hl_bits else ""+ if render == "photo":+ high_level = f"A photographic image{feat}."+ else:+ high_level = f"A graphic design{feat}."++ cap: dict[str, Any] = {}+ cap["high_level_description"] = high_level++ # compositional_deconstruction — order: background, elements.+ elements: list[dict[str, Any]] = []+ for box in boxes:+ if not isinstance(box, dict):+ continue+ btype = box.get("type")+ if btype not in VALID_TYPES:+ continue+ if btype == "text" and not str(box.get("text") or "").strip():+ continue # drop empty text boxes (JS parity)+ el: dict[str, Any] = {}+ el["type"] = "obj" if btype == "object" else "text" # type first+ el["bbox"] = rect_to_bbox( # then bbox+ box.get("x", 0), box.get("y", 0), box.get("w", 0), box.get("h", 0)+ )+ if el["type"] == "text":+ el["text"] = str(box.get("text") or "").strip() # text (text only)+ el["desc"] = _synth_desc(box) # desc+ if btype == "text": # per-element palette ≤5, text only+ el["color_palette"] = [norm_hex(box.get("color"))][:5]+ elements.append(el)++ cap["compositional_deconstruction"] = {"background": background, "elements": elements}+ return cap+++# ---------------------------------------------------------------------------+# validate_spec — human-readable issues (warnings, not hard errors)+# ---------------------------------------------------------------------------+def _overlap_fraction(a: dict, b: dict) -> float:+ """Intersection area / smaller box area, in fractional coords. Used to+ warn (not block) when two boxes overlap heavily."""+ try:+ ax0, ay0 = float(a.get("x", 0)), float(a.get("y", 0))+ ax1, ay1 = ax0 + float(a.get("w", 0)), ay0 + float(a.get("h", 0))+ bx0, by0 = float(b.get("x", 0)), float(b.get("y", 0))+ bx1, by1 = bx0 + float(b.get("w", 0)), by0 + float(b.get("h", 0))+ except (TypeError, ValueError):+ return 0.0+ ix = max(0.0, min(ax1, bx1) - max(ax0, bx0))+ iy = max(0.0, min(ay1, by1) - max(ay0, by0))+ inter = ix * iy+ area_a = max(0.0, ax1 - ax0) * max(0.0, ay1 - ay0)+ area_b = max(0.0, bx1 - bx0) * max(0.0, by1 - by0)+ smaller = min(area_a, area_b)+ if smaller <= 0:+ return 0.0+ return inter / smaller+++def validate_spec(spec: dict) -> list[str]:+ """Return a list of human-readable issues with an agent-facing spec.++ These are WARNINGS where the model would still render (overlaps, missing+ refinement desc on objects degrade quality but don't crash) and harder+ problems where it would not (no text in a text box, bbox out of range,+ bad hex). The HTTP layer decides which subset is fatal; this function just+ reports. Empty list = clean.+ """+ issues: list[str] = []+ if not isinstance(spec, dict):+ return ["spec must be a JSON object"]++ # scene+ scene = spec.get("scene")+ if scene is None or not str(scene).strip():+ issues.append("scene: required — describe the overall background/composition")+ elif not isinstance(scene, str):+ issues.append("scene: must be a string")++ # render+ render = spec.get("render", "design")+ if render not in VALID_RENDER:+ issues.append(f"render: {render!r} unknown — use one of {list(VALID_RENDER)}")++ # boxes+ boxes = spec.get("boxes")+ if boxes is None:+ issues.append("boxes: required — pass a list (may be empty for a pure-scene render)")+ boxes = []+ elif not isinstance(boxes, list):+ issues.append("boxes: must be a list")+ boxes = []++ text_count = 0+ valid_boxes_for_overlap: list[tuple[int, dict]] = []+ for i, box in enumerate(boxes):+ p = f"boxes[{i}]"+ if not isinstance(box, dict):+ issues.append(f"{p}: must be an object")+ continue+ btype = box.get("type")+ if btype not in VALID_TYPES:+ issues.append(f"{p}.type: must be 'text' or 'object' (got {btype!r})")+ continue+ if btype == "text":+ text_count += 1+ if not str(box.get("text") or "").strip():+ issues.append(f"{p}.text: text box has no text (it will be dropped from the caption)")+ style = box.get("style", "headline")+ if style not in VALID_STYLES:+ issues.append(f"{p}.style: {style!r} unknown — use one of {list(VALID_STYLES)}")+ align = box.get("align", "center")+ if align not in VALID_ALIGN:+ issues.append(f"{p}.align: {align!r} unknown — use one of {list(VALID_ALIGN)}")+ color = box.get("color")+ # norm_hex silently falls back to white on a bad color — surface that.+ if color is not None and not _HEX_RE.match(str(color)):+ issues.append(f"{p}.color: {color!r} is not a #RRGGBB hex (will default to #FFFFFF)")+ else: # object+ if not str(box.get("desc") or "").strip():+ issues.append(f"{p}.desc: object box needs a 'desc' describing what to render there")++ # geometry: x,y,w,h present and in range+ geom_ok = True+ for k in ("x", "y", "w", "h"):+ if k not in box:+ issues.append(f"{p}.{k}: missing (fraction 0..1, top-left origin)")+ geom_ok = False+ continue+ try:+ val = float(box[k])+ except (TypeError, ValueError):+ issues.append(f"{p}.{k}: must be a number (fraction 0..1)")+ geom_ok = False+ continue+ if val < 0 or val > 1:+ issues.append(f"{p}.{k}: {val} out of range — must be a fraction in [0,1]")+ # off-frame: top-left + size must stay within the unit frame, else the+ # bbox silently clamps and the box ends up a different size than asked.+ if geom_ok:+ try:+ if float(box["x"]) + float(box["w"]) > 1.0:+ issues.append(f"{p}: x+w = {round(float(box['x']) + float(box['w']), 3)} extends past the "+ f"right edge (>1) — the bbox will be clamped to the frame")+ if float(box["y"]) + float(box["h"]) > 1.0:+ issues.append(f"{p}: y+h = {round(float(box['y']) + float(box['h']), 3)} extends past the "+ f"bottom edge (>1) — the bbox will be clamped to the frame")+ except (TypeError, ValueError, KeyError):+ pass+ # zero-area / off-frame bbox check via the real bbox math+ try:+ bb = rect_to_bbox(box.get("x", 0), box.get("y", 0), box.get("w", 0), box.get("h", 0))+ if bb[0] == bb[2] or bb[1] == bb[3]:+ issues.append(f"{p}: box has zero width or height after clamping — give it a visible w/h")+ valid_boxes_for_overlap.append((i, box))+ except (TypeError, ValueError):+ pass++ if text_count > MAX_TEXT_BOXES:+ issues.append(f"boxes: {text_count} text boxes exceeds the {MAX_TEXT_BOXES}-box cap "+ f"(extra text boxes confuse the layout)")++ # overlap warnings (>40% of the smaller box)+ for a in range(len(valid_boxes_for_overlap)):+ for b in range(a + 1, len(valid_boxes_for_overlap)):+ ia, ba = valid_boxes_for_overlap[a]+ ib, bb = valid_boxes_for_overlap[b]+ ov = _overlap_fraction(ba, bb)+ if ov > 0.40:+ issues.append(f"boxes[{ia}] and boxes[{ib}] overlap ~{round(ov * 100)}% — "+ f"text/objects may collide")++ return issues+++# Convenience: caption-level validation reusing the upstream verifier's rules+# in pure Python (mirror of the JS ideoValidateCaption), so callers can assert+# the built caption itself is schema-clean without importing mflux.+def validate_caption(cap: Any) -> list[str]:+ """Validate a built caption dict against the Ideogram 4 schema (the same+ rules the upstream `Ideogram4CaptionVerifier` enforces, minus the+ ASCII-escape check). Returns human-readable problems; empty = valid."""+ out: list[str] = []++ def is_str(v: Any) -> bool:+ return isinstance(v, str)++ if not isinstance(cap, dict):+ out.append("root must be an object")+ return out+ root_known = ("high_level_description", "style_description", "compositional_deconstruction")+ for k in cap:+ if k not in root_known:+ out.append("root: unknown key " + str(k))+ if "high_level_description" in cap and not is_str(cap["high_level_description"]):+ out.append("high_level_description must be a string")++ if "style_description" in cap:+ sd = cap["style_description"]+ if not isinstance(sd, dict):+ out.append("style_description must be an object")+ else:+ has_photo = "photo" in sd+ has_art = "art_style" in sd+ if has_photo == has_art:+ out.append("style_description: exactly one of 'photo' or 'art_style'")+ order = (["aesthetics", "lighting", "photo", "medium", "color_palette"]+ if has_photo else+ ["aesthetics", "lighting", "medium", "art_style", "color_palette"])+ present = [k for k in sd if k in order]+ want = [k for k in order if k != "color_palette" or "color_palette" in sd]+ if present != want:+ out.append("style_description: key order " + ",".join(present) + " != " + ",".join(want))+ for k in sd:+ if k not in order:+ out.append("style_description: key " + str(k) + " not allowed")+ for k in order:+ if k != "color_palette" and k not in sd:+ out.append("style_description: missing " + k)+ if "color_palette" in sd:+ cp = sd["color_palette"]+ if not isinstance(cp, list):+ out.append("style_description.color_palette must be a list")+ else:+ if len(cp) > 16:+ out.append("style_description.color_palette: >16 colors")+ for c in cp:+ if not (isinstance(c, str) and _STRICT_HEX_RE.match(c)):+ out.append("style_description.color_palette: " + str(c) + " not uppercase #RRGGBB")++ if "compositional_deconstruction" not in cap:+ out.append("root: 'compositional_deconstruction' should exist")+ return out+ cd = cap["compositional_deconstruction"]+ if not isinstance(cd, dict):+ out.append("compositional_deconstruction must be an object")+ return out+ cd_present = [k for k in cd if k in ("background", "elements")]+ cd_want = [k for k in ("background", "elements") if k in cd]+ if cd_present != cd_want:+ out.append("compositional_deconstruction: key order")+ for k in cd:+ if k not in ("background", "elements"):+ out.append("compositional_deconstruction: key " + str(k) + " not allowed")+ if "background" not in cd:+ out.append("compositional_deconstruction: 'background' should exist")+ elif not is_str(cd["background"]):+ out.append("compositional_deconstruction.background must be a string")+ if "elements" not in cd:+ out.append("compositional_deconstruction: 'elements' should exist")+ return out+ if not isinstance(cd["elements"], list):+ out.append("compositional_deconstruction.elements must be a list")+ return out+ for i, el in enumerate(cd["elements"]):+ p = "elements[" + str(i) + "]"+ if not isinstance(el, dict):+ out.append(p + " must be an object")+ continue+ el_known = ("type", "bbox", "text", "desc", "color_palette")+ for k in el:+ if k not in el_known:+ out.append(p + ": unknown key " + str(k))+ if el.get("type") not in ("obj", "text"):+ out.append(p + ": type must be 'obj' or 'text'")+ continue+ order = (["type", "bbox", "text", "desc", "color_palette"]+ if el["type"] == "text" else ["type", "bbox", "desc", "color_palette"])+ present = [k for k in el if k in order]+ want = [k for k in order if (k not in ("bbox", "color_palette")) or k in el]+ if present != want:+ out.append(p + ": key order " + ",".join(present) + " != " + ",".join(want))+ if "desc" not in el:+ out.append(p + ": 'desc' should exist")+ elif not is_str(el["desc"]):+ out.append(p + ".desc must be a string")+ if el["type"] == "text":+ if "text" not in el:+ out.append(p + ": text elements should include 'text'")+ elif not is_str(el["text"]):+ out.append(p + ".text must be a string")+ if "bbox" in el:+ bb = el["bbox"]+ if not (isinstance(bb, list) and len(bb) == 4):+ out.append(p + ".bbox: expected [y_min,x_min,y_max,x_max]")+ elif not all(type(v) is int for v in bb):+ out.append(p + ".bbox: all values must be integers")+ else:+ if not all(0 <= v <= 1000 for v in bb):+ out.append(p + ".bbox: values must be in [0,1000]")+ if bb[0] > bb[2]:+ out.append(p + ".bbox: y_min > y_max")+ if bb[1] > bb[3]:+ out.append(p + ".bbox: x_min > x_max")+ if "color_palette" in el:+ cp = el["color_palette"]+ if not isinstance(cp, list):+ out.append(p + ".color_palette must be a list")+ else:+ if len(cp) > 5:+ out.append(p + ".color_palette: >5 colors")+ for c in cp:+ if not (isinstance(c, str) and _STRICT_HEX_RE.match(c)):+ out.append(p + ".color_palette: " + str(c) + " not uppercase #RRGGBB")+ return outdiff --git a/mlx_ltx_panel.py b/mlx_ltx_panel.pyindex f3705b1..c6d8257 100644--- a/mlx_ltx_panel.py+++ b/mlx_ltx_panel.py@@ -46,6 +46,14 @@ from urllib.parse import parse_qs, quote, urlparse# Pre-removal snapshot: git tag pre-agent-removal-2026-05-15.import image_engine as agent_image_engine+# Agent-facing Ideogram 4 caption builder + validator (pure stdlib). Powers+# the GET /image/agent/schema + POST /image/agent endpoints — a clean JSON+# surface that lets an LLM agent compose an Ideogram 4 layout (scene, text+# placement, object regions, colors) WITHOUT knowing the model's finicky+# internal caption schema. Faithful Python port of the panel's JS canvas+# serializer (ideoBuildCaption / ideoSynthDesc / ideoRectToBbox).+import ideogram_caption+# --- Paths -------------------------------------------------------------------# Everything below is overridable via env vars so the panel can be cloned and# run from any directory without source edits. Defaults assume the repo layout:@@ -8579,6 +8587,227 @@ class Handler(BaseHTTPRequestHandler):limit = 40self._json({"uploads": list_uploads(limit=max(1, min(200, limit)))})return+ if parsed.path == "/image/agent/schema":+ # Self-serve contract for an LLM agent driving Ideogram 4 layout+ # via POST /image/agent. An agent that reads ONLY this response+ # should be able to compose a valid request: it documents the+ # box model (fractions, top-left origin), every field + its+ # allowed values, the caption rules we enforce, two complete+ # worked examples, and how wait/validate_only behave. No secrets.+ self._json({+ "version": "1.0",+ "description": (+ "Compose an Ideogram 4 image — scene, on-image text, object "+ "regions, styles, colors — by describing it in plain terms. "+ "POST your spec to /image/agent. The server translates it into "+ "Ideogram 4's strict internal caption for you; you never write "+ "that caption by hand. Coordinates are FRACTIONS of the frame "+ "with a top-left origin: x,y is the top-left corner of a box, "+ "w,h its size, each in [0,1]. (0,0)=top-left, (1,1)=bottom-right. "+ "Coordinates are aspect-independent — the same box lands in the "+ "same relative spot at any aspect ratio."+ ),+ "endpoint": {+ "method": "POST",+ "path": "/image/agent",+ "content_type": "application/json",+ },+ "spec_schema": {+ "scene": {+ "type": "string", "required": True,+ "doc": "Overall background / setting / mood of the whole image. "+ "This becomes the caption background — describe the world, "+ "not the text. e.g. 'A moody vintage travel poster of a "+ "mountain lake at golden hour, warm muted palette'.",+ },+ "boxes": {+ "type": "array", "required": True,+ "doc": "List of placed elements (text and/or objects). May be "+ "empty for a pure-scene render. Each item is a box object "+ "(see box_schema). Max 6 TEXT boxes; object boxes are "+ "uncapped but keep layouts legible.",+ "item": "box",+ },+ "render": {+ "type": "string", "required": False, "default": "design",+ "enum": list(ideogram_caption.VALID_RENDER),+ "doc": "'design' = graphic/poster/vector look (strong typography). "+ "'photo' = photographic look. Affects the high-level "+ "description the model receives.",+ },+ "aspect": {+ "type": "string", "required": False, "default": "16:9",+ "enum": list(ideogram_caption.VALID_ASPECT),+ "doc": "Frame aspect ratio. 16:9=1280x720, 1:1=1024x1024, "+ "9:16=720x1280, 4:3=1024x768, 3:4=768x1024, 21:9=1280x544.",+ },+ "quality": {+ "type": "string", "required": False, "default": "turbo",+ "enum": list(ideogram_caption.VALID_QUALITY),+ "doc": "Sampler effort. 'turbo' (12 steps, fastest), "+ "'default' (20 steps), 'quality' (48 steps, slowest/best).",+ },+ "n": {+ "type": "integer", "required": False, "default": 1,+ "min": 1, "max": 4,+ "doc": "How many candidate images to render (1-4).",+ },+ "seed": {+ "type": "integer", "required": False, "default": -1,+ "doc": "Fixed seed for reproducibility; -1 (or omit) = random. "+ "For n>1 the server uses seed, seed+1, ... per candidate.",+ },+ "validate_only": {+ "type": "boolean", "required": False, "default": False,+ "doc": "If true, do NOT render — return the built caption + any "+ "warnings so you can inspect/iterate cheaply.",+ },+ "wait": {+ "type": "boolean", "required": False, "default": True,+ "doc": "If true (default), block until the render finishes and "+ "return image paths/urls. If false, enqueue and return "+ "immediately with queued:true.",+ },+ "box_schema": {+ "type": {+ "type": "string", "required": True,+ "enum": list(ideogram_caption.VALID_TYPES),+ "doc": "'text' renders literal words; 'object' renders a "+ "described thing in that region.",+ },+ "x": {"type": "number", "required": True, "min": 0, "max": 1,+ "doc": "Left edge as a fraction of frame width (0=left)."},+ "y": {"type": "number", "required": True, "min": 0, "max": 1,+ "doc": "Top edge as a fraction of frame height (0=top)."},+ "w": {"type": "number", "required": True, "min": 0, "max": 1,+ "doc": "Width as a fraction of frame width. x+w must be <= 1."},+ "h": {"type": "number", "required": True, "min": 0, "max": 1,+ "doc": "Height as a fraction of frame height. y+h must be <= 1."},+ "text": {"type": "string", "required": "for type=text",+ "doc": "The literal words to render. Required for text "+ "boxes; empty text boxes are dropped."},+ "desc": {"type": "string", "required": "for type=object",+ "doc": "What to render. REQUIRED for object boxes. "+ "Optional for text boxes — when given it overrides "+ "the auto-generated style/align/color description."},+ "style": {"type": "string", "required": False, "default": "headline",+ "enum": list(ideogram_caption.VALID_STYLES),+ "doc": "Text-only. Typographic feel: headline/subhead/"+ "body/caps/script/serif."},+ "align": {"type": "string", "required": False, "default": "center",+ "enum": list(ideogram_caption.VALID_ALIGN),+ "doc": "Text-only. left/center/right."},+ "color": {"type": "string", "required": False, "default": "#FFFFFF",+ "doc": "Text-only. Hex #RRGGBB (any case; normalized to "+ "uppercase). Bad values fall back to #FFFFFF."},+ },+ },+ "caption_rules_summary": [+ "Coordinates are fractions in [0,1] with a top-left origin; "+ "internally each box becomes a bbox [y_min,x_min,y_max,x_max] of "+ "row-first integers in 0..1000 (you never write this yourself).",+ "At most 6 text boxes. Object boxes carry a required desc.",+ "Each text box gets one color (#RRGGBB, uppercased). Boxes that "+ "extend past the frame edge (x+w>1 or y+h>1) are clamped — a warning "+ "is returned.",+ "Heavily overlapping boxes (>40% of the smaller box) get a warning "+ "but still render.",+ "Empty text boxes are silently dropped from the final caption.",+ ],+ "examples": [+ {+ "name": "16:9 poster — 2 text + 1 object",+ "request": {+ "scene": "A moody vintage travel poster of a mountain lake at "+ "golden hour, warm muted palette",+ "render": "design",+ "aspect": "16:9",+ "quality": "turbo",+ "n": 1,+ "boxes": [+ {"type": "text", "x": 0.08, "y": 0.08, "w": 0.84, "h": 0.18,+ "text": "LAKE DISTRICT", "style": "headline",+ "align": "center", "color": "#F5C518"},+ {"type": "text", "x": 0.30, "y": 0.80, "w": 0.40, "h": 0.10,+ "text": "Est. 1951", "style": "serif",+ "align": "center", "color": "#FFFFFF"},+ {"type": "object", "x": 0.55, "y": 0.32, "w": 0.35, "h": 0.42,+ "desc": "A small wooden canoe drifting on the still lake"},+ ],+ },+ "built_caption": ideogram_caption.build_caption({+ "scene": "A moody vintage travel poster of a mountain lake at "+ "golden hour, warm muted palette",+ "render": "design",+ "boxes": [+ {"type": "text", "x": 0.08, "y": 0.08, "w": 0.84, "h": 0.18,+ "text": "LAKE DISTRICT", "style": "headline",+ "align": "center", "color": "#F5C518"},+ {"type": "text", "x": 0.30, "y": 0.80, "w": 0.40, "h": 0.10,+ "text": "Est. 1951", "style": "serif",+ "align": "center", "color": "#FFFFFF"},+ {"type": "object", "x": 0.55, "y": 0.32, "w": 0.35, "h": 0.42,+ "desc": "A small wooden canoe drifting on the still lake"},+ ],+ }),+ },+ {+ "name": "9:16 label — single headline on a clean background",+ "request": {+ "scene": "A minimalist product label, soft off-white paper "+ "texture, centered composition",+ "render": "design",+ "aspect": "9:16",+ "quality": "default",+ "n": 1,+ "boxes": [+ {"type": "text", "x": 0.10, "y": 0.42, "w": 0.80, "h": 0.16,+ "text": "COLD BREW", "style": "caps",+ "align": "center", "color": "#0A0A0A"},+ ],+ },+ "built_caption": ideogram_caption.build_caption({+ "scene": "A minimalist product label, soft off-white paper "+ "texture, centered composition",+ "render": "design",+ "boxes": [+ {"type": "text", "x": 0.10, "y": 0.42, "w": 0.80, "h": 0.16,+ "text": "COLD BREW", "style": "caps",+ "align": "center", "color": "#0A0A0A"},+ ],+ }),+ },+ ],+ "usage_notes": {+ "validate_only": "POST with validate_only:true to get {ok, caption, "+ "issues} back without rendering — use it to iterate on "+ "a layout for free. issues is a list of human-readable "+ "warnings; an empty list means the spec is clean.",+ "wait_true": "Default. The request blocks until the render completes "+ "(or fails/times out) and returns {ok, caption, images:"+ "[{path,url}], seconds}. Open each url on this same host to "+ "view the PNG.",+ "wait_false": "Returns 202 {ok, queued:true, caption, where_results_land} "+ "immediately; the job runs on the panel's queue and the "+ "results appear in the panel's Recent tab / library.",+ "busy": "If the GPU is busy with another render or training job, a "+ "wait:true request waits in the queue. Renders are serialized; "+ "expect to wait behind any in-flight job.",+ "render_times_per_image": {+ "turbo": "~12-step sampler; fastest. Seconds-to-low-minutes per "+ "image after a one-time model load (first call also pays "+ "the cold model load).",+ "default": "~20-step sampler; moderate.",+ "quality": "~48-step sampler; slowest, highest fidelity.",+ "note": "Wall time = one-time model cold-load + n × per-image "+ "sampler time. The first Ideogram render after panel start "+ "is slower because the weights load once.",+ },+ "timeout": "A wait:true render gives up after 25 minutes and returns an "+ "error; the job may still complete on the queue.",+ },+ })+ returnif parsed.path == "/image/engine_status":# Per-engine cache + wall-time data for the Image Studio's status# pill + Generate button label. The Studio polls this on entry +@@ -10095,6 +10324,219 @@ class Handler(BaseHTTPRequestHandler):self._json({"error": f"voice upload failed: {exc}"}, 500)return+ # ===== Agent-facing Ideogram 4 layout endpoint =====================+ # POST /image/agent — an LLM agent describes a composition in plain+ # terms (scene + fractional text/object boxes) and we translate it+ # into Ideogram 4's strict internal caption, then render it through+ # the SAME queue path the browser UI uses (engine_override=+ # ideogram4_inline, prompt = the JSON caption). GET /image/agent/schema+ # is the self-serve contract an agent reads first. Lives before the+ # urlencoded parsing because the body is JSON. NEVER echoes secrets.+ if path == "/image/agent" and ctype.startswith("application/json"):+ try:+ length = int(self.headers.get("Content-Length") or "0")+ except ValueError:+ self._json({"ok": False, "error": "invalid Content-Length"}, 400); return+ if length <= 0:+ self._json({"ok": False, "error": "Content-Length required for JSON body"}, 411); return+ MAX_AGENT_JSON = 1 * 1024 * 1024+ if length > MAX_AGENT_JSON:+ self._json({"ok": False, "error": f"body too large (max {MAX_AGENT_JSON} bytes)"}, 413); return+ try:+ spec = json.loads(self.rfile.read(length).decode() or "{}")+ except (json.JSONDecodeError, UnicodeDecodeError):+ self._json({"ok": False, "error": "invalid JSON body"}, 400); return+ if not isinstance(spec, dict):+ self._json({"ok": False, "error": "body must be a JSON object (the spec)"}, 400); return++ # ---- option fields (with HTTP-layer enum/range enforcement) ----+ render = spec.get("render", "design")+ aspect = spec.get("aspect", "16:9")+ quality = spec.get("quality", "turbo")+ wait = spec.get("wait", True)+ validate_only = bool(spec.get("validate_only", False))+ try:+ n = int(spec.get("n", 1))+ except (TypeError, ValueError):+ n = -1+ try:+ seed = int(spec.get("seed", -1))+ except (TypeError, ValueError):+ seed = -1++ # Fatal, request-shape errors (400) — these block before we even+ # build a caption. Distinct from spec WARNINGS (returned in issues).+ shape_errors: list[str] = []+ if render not in ideogram_caption.VALID_RENDER:+ shape_errors.append(f"render must be one of {list(ideogram_caption.VALID_RENDER)}")+ if aspect not in ideogram_caption.VALID_ASPECT:+ shape_errors.append(f"aspect must be one of {list(ideogram_caption.VALID_ASPECT)}")+ if quality not in ideogram_caption.VALID_QUALITY:+ shape_errors.append(f"quality must be one of {list(ideogram_caption.VALID_QUALITY)}")+ if not isinstance(n, int) or n < 1 or n > 4:+ shape_errors.append("n must be an integer in 1..4")+ if not isinstance(spec.get("boxes", []), list):+ shape_errors.append("boxes must be a list")++ # Content validation — warnings + harder problems, human-readable.+ issues = ideogram_caption.validate_spec(spec)++ # Which issues are FATAL (the model could not produce a usable+ # render)? Off-frame clamps, overlaps and bad-color fallbacks are+ # tolerable warnings; the rest (no scene, object w/o desc, bad+ # type, out-of-range/missing coords, >6 text boxes) are fatal.+ def _is_warning(msg: str) -> bool:+ return (+ "extends past the" in msg+ or "overlap" in msg+ or "default to #FFFFFF" in msg+ or "it will be dropped from the caption" in msg+ )+ fatal_issues = [m for m in issues if not _is_warning(m)]++ # Build the caption regardless (so validate_only can show it even+ # when warnings exist); guarded so a malformed box can't 500.+ try:+ caption = ideogram_caption.build_caption(spec)+ except Exception as exc: # noqa: BLE001+ self._json({"ok": False, "issues": [f"could not build caption: {exc}"] + issues}, 400)+ return++ # A render needs at least one element that survives into the+ # caption (empty boxes => pure-scene is allowed, but an all-empty-+ # text spec with no objects renders nothing meaningful).+ elements = caption.get("compositional_deconstruction", {}).get("elements", [])+ boxes_in = spec.get("boxes", []) if isinstance(spec.get("boxes", []), list) else []+ if boxes_in and not elements:+ fatal_issues.append("no renderable elements — every box was empty or invalid")++ if shape_errors or fatal_issues:+ self._json({"ok": False, "issues": shape_errors + fatal_issues}, 400)+ return++ if validate_only:+ # No render — hand back the caption + any remaining warnings.+ self._json({"ok": True, "caption": caption, "issues": issues})+ return++ # ---- submit through the SAME queue path the UI uses ----+ # Build the exact form make_job()'s mode=image branch consumes,+ # then enqueue like /queue/add. The worker serializes GPU work+ # (holds _GPU_LOCK for the whole job) and routes mode=image to+ # run_image_job_inner — identical to a browser submit, so the+ # render also shows up in the panel's Now/Queue/Recent surfaces.+ prompt_json = json.dumps(caption, ensure_ascii=False)+ form = {+ "mode": "image",+ "prompt": prompt_json,+ "engine_override": "ideogram4_inline",+ "ideo_preset": ideogram_caption.QUALITY_PRESET[quality],+ "aspect": aspect,+ "n": str(n),+ "seed": str(seed),+ "refs": "[]",+ "session_tag": "agent",+ }+ try:+ job = make_job(form)+ except Exception as exc: # noqa: BLE001+ self._json({"ok": False, "error": f"could not build job: {exc}"}, 400)+ return+ with QUEUE_COND:+ STATE["queue"].append(job)+ QUEUE_COND.notify_all()+ persist_queue()+ job_id = job["id"]++ if not wait:+ # Fire-and-forget — tell the agent where to look.+ self._json({+ "ok": True,+ "queued": True,+ "job_id": job_id,+ "caption": caption,+ "where_results_land": (+ "The render runs on the panel queue; results appear in the "+ "panel's Recent tab and under panel_uploads/library/manual/"+ "<date>/. Poll GET /state for this job_id, or re-submit with "+ "wait:true to block for the images."+ ),+ }, 202)+ return++ # ---- wait: poll the job to completion (server-side) ----------+ # A job lives in STATE['queue'] (waiting), STATE['current']+ # (running) or STATE['history'] (finished) — find it by id across+ # all three under LOCK. We poll the JOB RECORD (not a global+ # guess): success => status done + output_path/candidate_paths+ # populated; failure => status failed/cancelled + error.+ def _find_job(jid: str):+ with LOCK:+ cur = STATE.get("current")+ if cur and cur.get("id") == jid:+ return cur+ for j in STATE.get("queue", []):+ if j.get("id") == jid:+ return j+ for j in STATE.get("history", []):+ if j.get("id") == jid:+ return j+ return None++ deadline = time.time() + 25 * 60 # 25 min timeout+ poll_t0 = time.time()+ while True:+ rec = _find_job(job_id)+ if rec is not None:+ status = rec.get("status")+ if status in ("done", "failed", "cancelled"):+ break+ if time.time() > deadline:+ self._json({+ "ok": False,+ "error": "render timed out after 25 minutes (the job may still "+ "finish on the queue)",+ "job_id": job_id,+ "caption": caption,+ }, 504)+ return+ time.sleep(1.0)++ rec = _find_job(job_id) or {}+ status = rec.get("status")+ if status != "done":+ err = (rec.get("error") or "render failed").strip()+ low = err.lower()+ # GPU/lock contention or video-in-flight → 503 (transient), not a+ # generic 500: tell the agent it can retry once the GPU frees up.+ if ("in progress" in low or "gpu is busy" in low+ or "already in progress" in low or "contend" in low):+ code = 503+ elif status == "cancelled":+ code = 409+ else:+ code = 500+ self._json({"ok": False, "error": err, "job_id": job_id, "caption": caption}, code)+ return++ # Success — collect the candidate PNG paths the worker recorded.+ params = rec.get("params", {}) or {}+ paths = list(params.get("candidate_paths") or [])+ if not paths and rec.get("output_path"):+ paths = [rec["output_path"]]+ images = [{"path": p, "url": "/file?path=" + quote(p)} for p in paths]+ seconds = rec.get("elapsed_sec")+ if seconds is None:+ seconds = round(time.time() - poll_t0, 2)+ self._json({+ "ok": True,+ "job_id": job_id,+ "caption": caption,+ "images": images,+ "seconds": seconds,+ })+ return+# JSON-body endpoint for the manual Image Studio. Lives BEFORE# the urlencoded body parsing because the body is JSON, not form# data. Drives `/image/generate` — the panel-side counterpart to@@ -13459,7 +13901,7 @@ HTML = r"""<!doctype html>position: absolute; box-sizing: border-box;border: 1.5px solid var(--accent);background: rgba(47,129,247,0.08);- border-radius: 3px; cursor: move; overflow: hidden;+ border-radius: 3px; cursor: move; overflow: visible;transition: border-color var(--t-fast), background var(--t-fast);}.ideo-box.obj { border-style: dashed; border-color: var(--muted); background: rgba(182,191,209,0.06); }@@ -13491,6 +13933,17 @@ HTML = r"""<!doctype html>color: var(--muted); background: rgba(10,15,36,0.7); border-bottom-right-radius: 3px;pointer-events: none; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;}+ /* object label in describe mode: fills the box as a centered caret target */+ .ideo-box-obj-label.editing {+ inset: 0; display: flex; align-items: center; justify-content: center; text-align: center;+ font-size: 12px; padding: 6px 10px; pointer-events: auto; cursor: text;+ user-select: text; -webkit-user-select: text; outline: none;+ background: rgba(8,12,28,0.30); box-shadow: inset 0 0 0 1.5px var(--accent-bright);+ white-space: normal; max-width: none; border-bottom-right-radius: 0;+ }+ .ideo-box-obj-label[data-ph]:empty::before {+ content: attr(data-ph); opacity: .4; pointer-events: none;+ }/* resize handles — 8 around the box */.ideo-handle {position: absolute; width: 9px; height: 9px; box-sizing: border-box;@@ -13506,6 +13959,75 @@ HTML = r"""<!doctype html>.ideo-handle.sw { bottom: -5px; left: -5px; cursor: nesw-resize; }.ideo-handle.se { bottom: -5px; right: -5px; cursor: nwse-resize; }+ /* ---- inline editing + per-box controls ---- */+ /* the editable text overrides the stage's user-select:none + crosshair */+ .ideo-box-text.editing {+ pointer-events: auto; cursor: text;+ user-select: text; -webkit-user-select: text; outline: none;+ background: rgba(8,12,28,0.30);+ box-shadow: inset 0 0 0 1.5px var(--accent-bright);+ }+ .ideo-box-text[data-ph]:empty::before {+ content: attr(data-ph); opacity: .4; font-weight: 400;+ text-transform: none; letter-spacing: normal; pointer-events: none;+ }+ /* delete button — just outside the top-right corner, fades in on hover/select */+ .ideo-box-del {+ position: absolute; top: -9px; right: -9px; width: 19px; height: 19px;+ display: flex; align-items: center; justify-content: center; padding: 0;+ border: 1.5px solid var(--bg, #0a0f24); border-radius: 50%;+ background: #e5484d; color: #fff; font-size: 14px; line-height: 1;+ cursor: pointer; opacity: 0; transform: scale(.5);+ transition: opacity var(--t-fast), transform var(--t-fast); z-index: 7;+ }+ .ideo-box:hover > .ideo-box-del,+ .ideo-box.selected > .ideo-box-del { opacity: 1; transform: scale(1); }+ .ideo-box-del:hover { background: #ff5b60; }+ /* floating per-box toolbar (color / align / delete) */+ .ideo-fab {+ position: absolute; z-index: 8; display: inline-flex; align-items: center; gap: 3px;+ padding: 4px 6px; white-space: nowrap;+ background: var(--panel, #11161f); color: var(--text, #e8eef6);+ border: 1px solid var(--border-strong, #2a3344); border-radius: 10px;+ box-shadow: 0 8px 24px rgba(0,0,0,0.5);+ }+ .ideo-fab.above { transform: translate(-50%, calc(-100% - 9px)); }+ .ideo-fab.below { transform: translate(-50%, 9px); }+ .ideo-fab-sw-row { display: inline-flex; gap: 4px; }+ .ideo-fab-sw {+ width: 16px; height: 16px; padding: 0; border-radius: 50%; cursor: pointer;+ border: 1.5px solid rgba(255,255,255,0.22); box-sizing: border-box;+ transition: transform var(--t-fast);+ }+ .ideo-fab-sw:hover { transform: scale(1.14); }+ .ideo-fab-sw.on { border-color: var(--accent-bright); box-shadow: 0 0 0 2px var(--accent-dim); }+ .ideo-fab-btn {+ display: inline-flex; align-items: center; justify-content: center;+ width: 25px; height: 22px; padding: 0; border-radius: 6px; cursor: pointer;+ background: transparent; border: 1px solid transparent; color: var(--muted, #9aa6bd);+ }+ .ideo-fab-btn:hover { background: var(--bg-2, #0c1226); color: var(--text, #e8eef6); }+ .ideo-fab-btn.on { background: var(--accent-dim); color: var(--accent-bright); }+ .ideo-fab-btn.ideo-fab-del { color: #e07a80; }+ .ideo-fab-btn.ideo-fab-del:hover { background: rgba(229,72,77,0.16); color: #ff6b6f; }+ .ideo-fab-sep { width: 1px; height: 16px; background: var(--border, #2a3344); margin: 0 1px; }+ .ideo-fab-style {+ height: 22px; font-size: 11.5px; background: var(--bg-2, #0c1226); color: var(--text, #e8eef6);+ border: 1px solid var(--border, #2a3344); border-radius: 6px; padding: 0 4px; max-width: 92px;+ }+ .ideo-fab-btn:focus-visible, .ideo-fab-sw:focus-visible { outline: 2px solid var(--accent-bright); outline-offset: 1px; }+ .ideo-fab-btn:focus:not(:focus-visible), .ideo-fab-sw:focus:not(:focus-visible) { outline: none; }+ /* On-stage the canvas owns text/align/color/delete, so the left inspector+ slims to just what the canvas can't do (type, style, describe), and the+ now-orphaned "placement preview" note is dropped. */+ body[data-workflow="studio"].ideo-canvas-on #ideoStageNote { display: none; }+ /* the canvas's own stage bar now carries Insert/Snap/Undo/Examples → the+ composer toolbar is redundant while the canvas holds the right stage. */+ body[data-workflow="studio"].ideo-canvas-on .ideo-toolbar { display: none; }+ body[data-workflow="studio"].ideo-canvas-on .ideo-inspector .ideo-insp-row:has(#ideoInspSwatches) { display: none; }+ body[data-workflow="studio"].ideo-canvas-on .ideo-insp-actions .ideo-danger { display: none; }+ body[data-workflow="studio"].ideo-canvas-on .ideo-insp-grid { grid-template-columns: 1fr; }+.ideo-stage-note {font-size: 10.5px; color: var(--muted); margin: 6px 2px 0; line-height: 1.45;}@@ -15550,6 +16072,108 @@ HTML = r"""<!doctype html>vertical (so a tall clip doesn't push the carousel off-screen).object-fit:contain is the safety net — even if JS fails to setaspect, vertical clips letterbox cleanly instead of crop. */+ /* ===== Ideogram Layout: the placement canvas portals into the big right+ stage column. Toggle + host are hidden until body.ideo-canvas-on. ===== */+ /* The stage bar holds creation tools + the Edit/Result toggle, so the canvas+ is self-contained on the right; hidden with the host until canvas-on. */+ #ideoStageBar, #ideoCanvasHost { display: none; }+ body[data-workflow="studio"].ideo-canvas-on #ideoStageBar {+ display: flex; align-items: center; gap: 10px;+ margin: 14px 16px 0; flex: 0 0 auto;+ }+ /* Joined segment for the two primary creation actions. flex:0 0 auto is+ load-bearing: with overflow:hidden the group has no min-content floor,+ so on a tight bar flex-shrink would crush it before anything else. */+ .ideo-stage-bar .isb-group {+ display: inline-flex; align-items: stretch; overflow: hidden;+ flex: 0 0 auto;+ background: var(--bg-2, #0c1226);+ border: 1px solid var(--border-strong, #2a3344); border-radius: 9px;+ }+ .ideo-stage-bar .isb-group .isb-btn {+ border: 0; border-radius: 0; background: transparent;+ color: var(--text, #e8eef6); font-weight: 600;+ }+ .ideo-stage-bar .isb-group .isb-btn + .isb-btn { border-left: 1px solid var(--border, #2a3344); }+ .ideo-stage-bar .isb-group .isb-btn:hover { background: var(--accent-dim); color: var(--accent-bright); }+ .ideo-stage-bar .isb-btn {+ appearance: none; display: inline-flex; align-items: center;+ width: auto; /* the panel-wide reset makes every button width:100% */+ height: 30px; padding: 0 14px; box-sizing: border-box;+ background: transparent; border: 1px solid var(--border, #2a3344);+ color: var(--text-dim, #9aa6bd); font: inherit; font-size: 12.5px;+ border-radius: 9px; cursor: pointer;+ transition: color .12s, border-color .12s, background .12s; white-space: nowrap;+ }+ .ideo-stage-bar .isb-btn:hover { color: var(--text, #e8eef6); border-color: var(--border-strong, #2a3344); }+ .ideo-stage-bar .isb-btn:disabled { opacity: .4; cursor: default; }+ .ideo-stage-bar .isb-spacer { flex: 1; }+ .ideo-stage-bar .isb-toggle {+ display: inline-flex; align-items: center; gap: 6px; height: 30px;+ padding: 0 4px; box-sizing: border-box;+ font-size: 12px; color: var(--muted, #9aa6bd); cursor: pointer;+ }+ .ideo-stage-bar .isb-toggle input { accent-color: var(--accent, #2f81f7); cursor: pointer; }+ .ideo-stage-bar .isb-select {+ appearance: none; height: 30px; box-sizing: border-box;+ width: 118px; /* size to the closed label, not the longest option */+ background: var(--bg-2, #0c1226); border: 1px solid var(--border, #2a3344);+ color: var(--text-dim, #9aa6bd); font: inherit; font-size: 12.5px;+ padding: 0 26px 0 12px; border-radius: 9px; cursor: pointer;+ transition: color .12s, border-color .12s;+ }+ .ideo-stage-bar .isb-select:hover { color: var(--text, #e8eef6); border-color: var(--border-strong, #2a3344); }+ /* The Edit/Result toggle keeps its pill-container look but is now a flex+ child of the stage bar (the spacer pushes it to the right). */+ body[data-workflow="studio"].ideo-canvas-on #stageModeToggle {+ display: inline-flex; gap: 4px; padding: 3px;+ background: var(--bg-2, #11161f); border: 1px solid var(--border);+ border-radius: 10px; flex: 0 0 auto;+ }+ body[data-workflow="studio"].ideo-canvas-on #stageModeToggle .smt-btn {+ appearance: none; border: 0; background: transparent;+ color: var(--text-dim, #9aa6bd); font: inherit; font-size: 12.5px;+ font-weight: 600; padding: 4px 14px; height: 22px; box-sizing: border-box;+ display: inline-flex; align-items: center; border-radius: 7px;+ cursor: pointer; transition: background .12s, color .12s;+ white-space: nowrap;+ }+ body[data-workflow="studio"].ideo-canvas-on #stageModeToggle .smt-btn:hover { color: var(--text, #e8eef6); }+ body[data-workflow="studio"].ideo-canvas-on #stageModeToggle .smt-btn.active { background: var(--accent, #2f81f7); color: #fff; }+ /* Edit view: show the canvas host (flex-fills the column), hide the player hero. */+ body[data-workflow="studio"].ideo-canvas-on.stage-editing #ideoCanvasHost {+ display: flex; flex: 1 1 auto; flex-direction: column;+ align-items: center; justify-content: center;+ min-height: 0; padding: 16px; gap: 12px;+ }+ body[data-workflow="studio"].ideo-canvas-on.stage-editing .stage-pane > .player-surface { display: none; }+ /* Edit view hides the outputs gallery so the canvas owns the full column+ height; the Result toggle (or selecting an output) brings both back. */+ body[data-workflow="studio"].ideo-canvas-on.stage-editing .stage-pane > .carousel-wrap { display: none; }+ #ideoCanvasHost:has(#ideoStageWrap) .ideo-canvas-empty { display: none; }+ .ideo-canvas-empty {+ display: flex; flex-direction: column; align-items: center; gap: 10px;+ color: var(--text-dim, #9aa6bd); font-size: 14px; text-align: center;+ }+ /* The portaled canvas fills the host width, capped so it stays a comfortable+ size, and never taller than the available column height. */+ #ideoCanvasHost #ideoStageWrap {+ width: 100%; max-width: none; margin: 0;+ display: flex; align-items: center; justify-content: center;+ min-height: 0;+ }+ /* On the stage the canvas is a real artboard: ideoFitStage() sizes it in px+ to fit the host at the exact aspect (equal margins all around); the card+ gets a deeper shadow and a faint dot grid for design-tool orientation. */+ #ideoCanvasHost #ideoStage {+ width: 100%; height: auto; max-width: none; margin: 0;+ border-radius: 10px; border-color: var(--border-strong, #2a3344);+ box-shadow: 0 18px 48px rgba(0,0,0,0.5), 0 0 0 1px rgba(0,0,0,0.35) inset;+ background-image:+ radial-gradient(rgba(122,140,180,0.10) 1px, transparent 1.2px),+ linear-gradient(160deg, #0d1430, #0a0f24);+ background-size: 22px 22px, 100% 100%;+ }.player-surface {position: relative;flex: 0 0 auto;@@ -19587,16 +20211,16 @@ HTML = r"""<!doctype html>ideoApplyAspect(); boxes store x,y,w,h as 0..1 fractions so aratio change just restyles the box, never moves coordinates.role=application + keyboard handlers make it a11y-operable. -->- <div class="ideo-stage-wrap">+ <div class="ideo-stage-wrap" id="ideoStageWrap"><div class="ideo-stage" id="ideoStage" role="application"aria-label="Text placement canvas. Click Insert text or drag to add a box; arrow keys nudge the selected box, Delete removes it."tabindex="0"onpointerdown="ideoStagePointerDown(event)">- <div class="ideo-stage-hint" id="ideoStageHint">Drag here, or click <strong>Insert text</strong>, to place words on the frame.</div>+ <div class="ideo-stage-hint" id="ideoStageHint">Drag to draw a text box, or use <strong>+ Text</strong> above.</div><!-- guide lines + boxes are injected here by ideoRender() --></div></div>- <div class="ideo-stage-note">+ <div class="ideo-stage-note" id="ideoStageNote"><svg class="ph" aria-hidden="true" style="margin-right:4px;vertical-align:-2px"><use href="#ph-info"/></svg>Placement preview — boxes show where words sit; Ideogram renders the final type, weight, and texture.</div>@@ -20505,6 +21129,42 @@ HTML = r"""<!doctype html>(#filterAll/#filterHidden retired with the Visible/Hiddensegmented control — orphan textContent/setFilter calls removed.) --><section class="stage-pane">+ <!-- Ideogram Layout editor: the placement canvas (#ideoStageWrap) portals+ into #ideoCanvasHost here in the big right column, and the Edit/Result+ toggle flips between the canvas and the rendered output. Both are+ display:none until body gets .ideo-canvas-on (set by ideoSyncStage). -->+ <div class="ideo-stage-bar" id="ideoStageBar">+ <div class="isb-group" role="group" aria-label="Add to canvas">+ <button type="button" class="isb-btn" onclick="ideoInsertBox('text')" title="Add a text box (or drag on an empty area of the frame)">+ Text</button>+ <button type="button" class="isb-btn" onclick="ideoInsertBox('obj')" title="Add an object region (something the model should draw)">+ Object</button>+ </div>+ <label class="isb-toggle" title="Snap boxes to thirds / center / other boxes">+ <input type="checkbox" id="ideoSnapToggleStage" checked onchange="ideoState.snap=this.checked">+ <span>Snap</span>+ </label>+ <button type="button" class="isb-btn" id="ideoUndoBtnStage" onclick="ideoUndo()" title="Undo (Cmd/Ctrl+Z)" disabled>Undo</button>+ <select id="ideoExamplePickerStage" class="isb-select" onchange="ideoLoadExample(this.value); this.value='';" title="Load an example layout">+ <option value="">Examples…</option>+ <option value="poster">Poster — 16:9 event</option>+ <option value="logo">Logo — 1:1 wordmark</option>+ <option value="label">Product label — 9:16</option>+ <option value="meme">Meme — 1:1 top/bottom</option>+ </select>+ <span class="isb-spacer"></span>+ <div class="stage-mode-toggle" id="stageModeToggle" role="group" aria-label="Stage view">+ <button type="button" class="smt-btn active" data-stage-mode="edit" onclick="stageSetMode('edit')">Edit canvas</button>+ <button type="button" class="smt-btn" data-stage-mode="result" onclick="stageSetMode('result')">Result</button>+ </div>+ </div>+ <div class="ideo-canvas-host" id="ideoCanvasHost">+ <div class="ideo-canvas-empty" id="ideoCanvasEmpty">+ <svg width="40" height="40" viewBox="0 0 56 56" fill="none" aria-hidden="true">+ <rect x="8" y="12" width="40" height="32" rx="3" stroke="currentColor" stroke-width="1.5" stroke-opacity="0.4"/>+ <path d="M16 30 L24 22 L34 32 L40 26" stroke="currentColor" stroke-width="1.5" stroke-opacity="0.4" fill="none"/>+ </svg>+ <div>Pick <strong>Layout</strong> on the left to place words on the frame.</div>+ </div>+ </div><!-- Player surface — the hero. Holds the <video>/<img>, the filenameoverlay (top), the action overlay (top-right), and an inlinejob-progress overlay (bottom) when something is rendering. The@@ -21976,7 +22636,12 @@ function imgStudioUpdateRefWarning() {// cinematic-portrait placeholder; 2+ refs swap to a multi-subject// example that anchors each reference explicitly.if (prompt) {- if (refsCount >= 2) {+ // Ideogram owns the placeholder (ideoApplyComposerChrome words it per+ // Simple/Layout mode) and takes no reference images — this handler runs+ // after it in the engine onchange chain, so writing here would stomp it.+ if (typeof ideoIsActive === 'function' && ideoIsActive()) {+ /* leave Ideogram's placeholder alone */+ } else if (refsCount >= 2) {prompt.placeholder ='the man from reference 1 and the woman from reference 2 sitting at a candlelit dinner, warm tungsten light, soft bokeh, photorealistic';} else {@@ -22263,6 +22928,7 @@ const ideoState = {snap: true,history: [],_editing: false, // a text input is focused (don't steal keys)+ _editId: null, // box currently being edited inline on the canvas_seq: 1,};@@ -22286,6 +22952,79 @@ function ideoIsActive(){}function ideoInLayout(){ return ideoIsActive() && ideoState.mode === 'layout'; }+// --- Ideogram canvas portal -------------------------------------------------+// In Layout mode the placement canvas (#ideoStageWrap) moves into the big right+// stage (#ideoCanvasHost); otherwise it sits in the composer just before the+// note. Every box handler resolves #ideoStage by id, so relocating the node is+// safe — coordinates are fractional and survive the move untouched.+var _stageMode = 'edit';+var _ideoStageWasOn = false;+function ideoCanvasPortal(toStage){+ var wrap = document.getElementById('ideoStageWrap');+ if (!wrap) return;+ if (toStage){+ var host = document.getElementById('ideoCanvasHost');+ if (host && wrap.parentNode !== host) host.appendChild(wrap);+ } else {+ var note = document.getElementById('ideoStageNote');+ if (note && note.parentNode && wrap.parentNode !== note.parentNode){+ note.parentNode.insertBefore(wrap, note);+ }+ }+}+// Size the canvas to FIT the host box at the exact aspect ratio — width-bound+// on wide hosts, height-bound on short ones — so the artboard always sits with+// even margins instead of width-filling and overflowing the leftover height.+// Inline px width is cleared when the canvas portals home (the composer copy+// keeps its own responsive sizing).+function ideoFitStage(){+ var host = document.getElementById('ideoCanvasHost');+ var stage = document.getElementById('ideoStage');+ var wrap = document.getElementById('ideoStageWrap');+ if (!host || !stage || !wrap || wrap.parentNode !== host) return;+ if (!document.body.classList.contains('stage-editing')) return;+ var cs = getComputedStyle(host);+ var availW = host.clientWidth - parseFloat(cs.paddingLeft || 0) - parseFloat(cs.paddingRight || 0);+ var availH = host.clientHeight - parseFloat(cs.paddingTop || 0) - parseFloat(cs.paddingBottom || 0);+ if (availW <= 50 || availH <= 50) return;+ var ar = String(stage.style.aspectRatio || '16 / 9').split('/');+ var r = (parseFloat(ar[0]) || 16) / (parseFloat(ar[1]) || 9);+ var w = Math.min(availW, availH * r);+ stage.style.width = Math.max(220, Math.floor(w)) + 'px';+}+// Flip the right stage between the editor canvas (edit) and the rendered output.+function stageSetMode(which){+ _stageMode = (which === 'result') ? 'result' : 'edit';+ document.body.classList.toggle('stage-editing', _stageMode === 'edit');+ document.querySelectorAll('#stageModeToggle .smt-btn').forEach(function(b){+ var on = b.dataset.stageMode === _stageMode;+ b.classList.toggle('active', on);+ b.setAttribute('aria-pressed', on ? 'true' : 'false');+ });+ if (_stageMode === 'edit') ideoFitStage(); // host was hidden in result view+}+// Park the canvas in the right place for the current engine + mode.+// The edit canvas is a property of the Images workflow: leaving for+// Video/Audio/Train suspends it (classes off, canvas parked home, player+// restored) and coming back restores it — ideoState keeps the boxes, so+// nothing is lost across the round-trip.+function ideoSyncStage(){+ var on = ideoInLayout() && document.body.getAttribute('data-workflow') === 'studio';+ document.body.classList.toggle('ideo-canvas-on', on);+ ideoCanvasPortal(on);+ if (on){+ if (!_ideoStageWasOn) stageSetMode('edit'); // fresh entry → show the canvas+ var sn = document.getElementById('ideoSnapToggleStage'); if (sn) sn.checked = !!ideoState.snap;+ ideoApplyAspect(); ideoFitStage(); ideoRender();+ } else {+ if (typeof ideoExitEdit === 'function') ideoExitEdit(); // drop any live caret+ document.body.classList.remove('stage-editing');+ var st = document.getElementById('ideoStage');+ if (st) st.style.width = ''; // composer copy sizes itself+ }+ _ideoStageWasOn = on;+}+// Show/hide the panel + re-skin the surrounding composer based on engine.function ideoSyncVisibility(){const panel = document.getElementById('ideoPanel');@@ -22298,6 +23037,7 @@ function ideoSyncVisibility(){ideoRender();ideoRefreshRaw();}+ ideoSyncStage();}// When Ideogram is active+Layout: relabel the prompt box to "Scene &@@ -22347,6 +23087,7 @@ function ideoSetMode(mode){ideoRender();ideoRefreshRaw();}+ ideoSyncStage();}// Map the studio aspect select to a CSS aspect-ratio on the stage. Boxes are@@ -22358,6 +23099,7 @@ function ideoApplyAspect(){const a = (sel.value || '16:9').split(':');const w = parseFloat(a[0]) || 16, h = parseFloat(a[1]) || 9;stage.style.aspectRatio = `${w} / ${h}`;+ if (typeof ideoFitStage === 'function') ideoFitStage(); // refit the artboard}function ideoSetRender(r){@@ -22376,8 +23118,11 @@ function ideoSnapshot(){ideoUpdateUndoBtn();}function ideoUpdateUndoBtn(){+ const dis = ideoState.history.length === 0;const b = document.getElementById('ideoUndoBtn');- if (b) b.disabled = ideoState.history.length === 0;+ if (b) b.disabled = dis;+ const bs = document.getElementById('ideoUndoBtnStage');+ if (bs) bs.disabled = dis;}function ideoUndo(){if (!ideoState.history.length) return;@@ -22426,11 +23171,9 @@ function ideoInsertBox(type){ideoState.selId = box.id;ideoRender();ideoRefreshRaw();- // Focus the text input so the user can type immediately.- if (box.type === 'text'){- const inp = document.getElementById('ideoInspText');- if (inp) { try { inp.focus(); } catch(e){} }- }+ // Drop the caret straight into the new box so the user types/describes on the+ // canvas (text boxes get a text caret; object boxes get the describe field).+ ideoEnterEdit(box.id);}function ideoSelectedBox(){ return ideoState.boxes.find(b => b.id === ideoState.selId) || null; }@@ -22468,10 +23211,113 @@ function ideoDeleteSel(){ideoSnapshot();ideoState.boxes = ideoState.boxes.filter(b => b.id !== box.id);ideoState.selId = null;+ ideoState._editId = null;+ ideoState._editing = false;ideoRender();ideoRefreshRaw();}+// ---- inline text editing on the canvas ----+// Turn the selected text box into a live-editable element: caret in the box,+// type/paste directly on the frame. Coordinates are untouched; only box.text+// changes. _editing flips so the global key handler stops stealing keystrokes.+function ideoEnterEdit(id){+ const box = ideoState.boxes.find(b => b.id === id);+ if (!box) return;+ ideoState.selId = id;+ ideoState._editId = id;+ ideoRenderBoxes(); // re-render so this box gets contentEditable+ // Text boxes edit the .ideo-box-text; object boxes edit the .ideo-box-obj-label.+ const sel = box.type === 'text' ? '.ideo-box-text' : '.ideo-box-obj-label';+ const el = document.querySelector('.ideo-box[data-id="' + id + '"] ' + sel);+ if (el){+ el.focus();+ try {+ const range = document.createRange(); range.selectNodeContents(el); range.collapse(false);+ const sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range);+ } catch(e){}+ }+ ideoSyncInspector();+}+function ideoExitEdit(){+ if (ideoState._editId == null) return;+ ideoState._editId = null;+ ideoState._editing = false;+ ideoRenderBoxes();+ ideoSyncInspector();+ ideoRefreshRaw();+}+// Live keystroke handler: mirror the box text without a full re-render (which+// would blow away the caret), and keep the empty/warn outline + inspector field+// in sync.+function ideoBoxTextInput(ev, id){+ const box = ideoState.boxes.find(b => b.id === id);+ if (!box) return;+ box.text = ev.target.textContent || '';+ const wrap = ev.target.closest('.ideo-box');+ if (wrap) wrap.classList.toggle('warn', !box.text.trim());+ const inp = document.getElementById('ideoInspText');+ if (inp && document.activeElement !== inp) inp.value = box.text;+ ideoRefreshRaw();+}+// Object-box twin of ideoBoxTextInput: live-mirror the describe text into+// box.descManual (no full re-render, which would drop the caret) and keep the+// inspector's describe field in sync.+function ideoObjDescInput(ev, id){+ const box = ideoState.boxes.find(b => b.id === id);+ if (!box) return;+ box.descManual = ev.target.textContent || '';+ const inp = document.getElementById('ideoInspDesc');+ if (inp && document.activeElement !== inp) inp.value = box.descManual;+ ideoRefreshRaw();+}+// The floating per-box toolbar (color, alignment, delete) — appended to the+// stage and positioned above the box (or below if the box hugs the top edge),+// so common edits live right on the canvas instead of a far-off side panel.+function ideoBuildBoxToolbar(box, stage){+ const bar = document.createElement('div');+ bar.className = 'ideo-fab';+ let cx = (box.x + box.w / 2) * 100;+ cx = Math.max(15, Math.min(85, cx)); // keep it from overflowing the stage sides+ bar.style.left = cx + '%';+ if (box.y < 0.14){ bar.style.top = ((box.y + box.h) * 100) + '%'; bar.classList.add('below'); }+ else { bar.style.top = (box.y * 100) + '%'; bar.classList.add('above'); }+ // no drag, keep caret focus — but DON'T preventDefault on a <select>, or its+ // native dropdown won't open (preventDefault on pointerdown swallows it).+ bar.addEventListener('pointerdown', function(e){ e.stopPropagation(); if (!e.target.closest('select')) e.preventDefault(); });+ const A = [['left','M2 4h16M2 8h9M2 12h16'],['center','M2 4h16M5 8h10M2 12h16'],['right','M2 4h16M9 8h7M2 12h16']];+ let html = '';+ if (box.type === 'text'){+ html += '<span class="ideo-fab-sw-row">';+ IDEO_SWATCHES.forEach(hex => {+ const on = (box.color || '').toUpperCase() === hex.toUpperCase();+ html += '<button type="button" class="ideo-fab-sw' + (on ? ' on' : '') + '" data-color="' + hex + '" style="background:' + hex + '" title="' + hex + '"></button>';+ });+ html += '</span><span class="ideo-fab-sep"></span>';+ A.forEach(([al, d]) => {+ const on = box.align === al;+ html += '<button type="button" class="ideo-fab-btn' + (on ? ' on' : '') + '" data-align="' + al + '" title="Align ' + al + '"><svg viewBox="0 0 18 16" width="15" height="13"><path d="' + d + '" stroke="currentColor" stroke-width="1.5" fill="none" stroke-linecap="round"/></svg></button>';+ });+ html += '<span class="ideo-fab-sep"></span>';+ // text style picker (Headline/Subhead/Body/Caps/Script/Serif)+ const STY = [['headline','Headline'],['subhead','Subhead'],['body','Body'],['caps','Caps'],['script','Script'],['serif','Serif']];+ html += '<select class="ideo-fab-style" title="Text style">';+ STY.forEach(([v, lbl]) => {+ html += '<option value="' + v + '"' + (box.style === v ? ' selected' : '') + '>' + lbl + '</option>';+ });+ html += '</select><span class="ideo-fab-sep"></span>';+ }+ html += '<button type="button" class="ideo-fab-btn ideo-fab-del" data-del title="Delete (Backspace)"><svg viewBox="0 0 20 20" width="14" height="14"><path d="M5 6h10M8 6V4.5h4V6M7.5 6l.6 9h3.8l.6-9" stroke="currentColor" stroke-width="1.4" fill="none" stroke-linecap="round" stroke-linejoin="round"/></svg></button>';+ bar.innerHTML = html;+ bar.querySelectorAll('[data-color]').forEach(b => b.addEventListener('click', e => { e.stopPropagation(); ideoState.selId = box.id; ideoUpdateSel({ color: b.dataset.color }); }));+ bar.querySelectorAll('[data-align]').forEach(b => b.addEventListener('click', e => { e.stopPropagation(); ideoState.selId = box.id; ideoUpdateSel({ align: b.dataset.align }); }));+ var st = bar.querySelector('.ideo-fab-style');+ if (st) st.addEventListener('change', function(e){ e.stopPropagation(); ideoState.selId = box.id; ideoUpdateSel({ style: st.value }); });+ const delB = bar.querySelector('[data-del]');+ if (delB) delB.addEventListener('click', e => { e.stopPropagation(); ideoState.selId = box.id; ideoDeleteSel(); });+ stage.appendChild(bar);+}+// ---- rendering ----function ideoRender(){ideoRenderBoxes();@@ -22481,8 +23327,8 @@ function ideoRender(){function ideoRenderBoxes(){const stage = document.getElementById('ideoStage');if (!stage) return;- // Remove existing boxes + guides (keep the hint node).- stage.querySelectorAll('.ideo-box, .ideo-guide').forEach(n => n.remove());+ // Remove existing boxes + guides + floating toolbars (keep the hint node).+ stage.querySelectorAll('.ideo-box, .ideo-guide, .ideo-fab').forEach(n => n.remove());stage.classList.toggle('has-boxes', ideoState.boxes.length > 0);const overlaps = ideoComputeOverlaps();ideoState.boxes.forEach(box => {@@ -22508,13 +23354,59 @@ function ideoRenderBoxes(){// scale font roughly to box height so the preview reads as placementconst px = Math.max(8, Math.min(40, Math.round(box.h * (stage.clientHeight || 300) * 0.55)));t.style.fontSize = px + 'px';+ t.setAttribute('data-ph', 'Type text…');+ // Inline editing: the selected-for-edit box becomes a live caret target.+ // plaintext-only means a paste drops formatting → clean caption text.+ if (box.id === ideoState._editId){+ t.contentEditable = 'plaintext-only';+ t.spellcheck = false;+ t.classList.add('editing');+ t.addEventListener('input', e => ideoBoxTextInput(e, box.id));+ t.addEventListener('focus', () => { ideoState._editing = true; });+ t.addEventListener('blur', () => { ideoExitEdit(); });+ t.addEventListener('pointerdown', e => e.stopPropagation()); // caret, not drag+ t.addEventListener('dblclick', e => e.stopPropagation());+ t.addEventListener('keydown', e => {+ e.stopPropagation(); // don't nudge/delete+ if (e.key === 'Escape'){ e.preventDefault(); t.blur(); }+ });+ }el.appendChild(t);} else {const lbl = document.createElement('div');lbl.className = 'ideo-box-obj-label';- lbl.textContent = box.descManual ? box.descManual.slice(0, 40) : 'object';+ if (box.id === ideoState._editId){+ // Describe mode: the label fills the box as a live caret target. Empty+ // descManual shows the placeholder (don't fall back to 'object' here).+ lbl.contentEditable = 'plaintext-only';+ lbl.spellcheck = false;+ lbl.classList.add('editing');+ lbl.setAttribute('data-ph', 'Describe the object…');+ lbl.textContent = box.descManual || '';+ lbl.addEventListener('input', e => ideoObjDescInput(e, box.id));+ lbl.addEventListener('focus', () => { ideoState._editing = true; });+ lbl.addEventListener('blur', () => { ideoExitEdit(); });+ lbl.addEventListener('pointerdown', e => e.stopPropagation()); // caret, not drag+ lbl.addEventListener('dblclick', e => e.stopPropagation());+ lbl.addEventListener('keydown', e => {+ e.stopPropagation(); // don't nudge/delete+ if (e.key === 'Escape'){ e.preventDefault(); lbl.blur(); }+ });+ } else {+ lbl.textContent = box.descManual ? box.descManual.slice(0, 40) : 'object';+ }el.appendChild(lbl);}+ // delete affordance on every box (fades in on hover / when selected)+ const del = document.createElement('button');+ del.type = 'button';+ del.className = 'ideo-box-del';+ del.setAttribute('aria-label', 'Delete this box');+ del.title = 'Delete (Backspace)';+ del.innerHTML = '×';+ del.addEventListener('pointerdown', e => { e.stopPropagation(); e.preventDefault(); });+ del.addEventListener('click', e => { e.stopPropagation(); ideoState.selId = box.id; ideoDeleteSel(); });+ el.appendChild(del);// resize handles only on the selected boxif (box.id === ideoState.selId){['nw','n','ne','e','se','s','sw','w'].forEach(dir => {@@ -22525,6 +23417,10 @@ function ideoRenderBoxes(){});}stage.appendChild(el);+ // floating toolbar for the selected box (color / align / delete)+ if (box.id === ideoState.selId){+ ideoBuildBoxToolbar(box, stage);+ }});}@@ -22551,13 +23447,17 @@ function ideoSyncInspector(){// type pillsinsp.querySelectorAll('[data-ideo-type]').forEach(b =>b.classList.toggle('active', b.dataset.ideoType === box.type));- // text row visible only for text boxes+ // text row visible only for text boxes. When the canvas is on the right+ // stage, text/align live on the canvas (inline + floating toolbar), so the+ // inspector drops them and keeps only what the canvas can't do: type, style,+ // describe.+ const onStage = document.body.classList.contains('ideo-canvas-on');const textRow = document.getElementById('ideoInspTextRow');const styleCell = document.getElementById('ideoInspStyleCell');const alignCell = document.getElementById('ideoInspAlignCell');- if (textRow) textRow.style.display = isText ? '' : 'none';+ if (textRow) textRow.style.display = (isText && !onStage) ? '' : 'none';if (styleCell) styleCell.style.display = isText ? '' : 'none';- if (alignCell) alignCell.style.display = isText ? '' : 'none';+ if (alignCell) alignCell.style.display = (isText && !onStage) ? '' : 'none';const descLabel = document.querySelector('#ideoInspDescRow .mf-label');if (descLabel) descLabel.textContent = isText? 'Describe it (optional — refines look)'@@ -22981,8 +23881,9 @@ function ideoStagePointerDown(ev){// moveconst box = ideoState.boxes.find(b => b.id === boxEl.dataset.id);if (!box) return;+ const wasSel = (ideoState.selId === box.id);ideoSnapshot();- _ideoDrag = { mode:'move', id:box.id, startX:f.x, startY:f.y, orig:{...box}, created:false };+ _ideoDrag = { mode:'move', id:box.id, startX:f.x, startY:f.y, orig:{...box}, created:false, wasSel:wasSel, moved:false };ideoState.selId = box.id;ideoRenderBoxes(); ideoSyncInspector();} else {@@ -23004,6 +23905,7 @@ function ideoStagePointerMove(ev){const box = ideoState.boxes.find(b => b.id === _ideoDrag.id);if (!box) return;const dx = f.x - _ideoDrag.startX, dy = f.y - _ideoDrag.startY;+ if (Math.abs(dx) > 0.004 || Math.abs(dy) > 0.004) _ideoDrag.moved = true;if (_ideoDrag.mode === 'move'){let nx = _ideoDrag.orig.x + dx, ny = _ideoDrag.orig.y + dy;nx = Math.max(0, Math.min(1 - box.w, nx));@@ -23023,9 +23925,10 @@ function ideoStagePointerMove(ev){}function ideoStagePointerUp(ev){if (!_ideoDrag) return;- const box = ideoState.boxes.find(b => b.id === _ideoDrag.id);+ const drag = _ideoDrag;+ const box = ideoState.boxes.find(b => b.id === drag.id);if (box){- if (_ideoDrag.mode === 'create' && (box.w < IDEO_MIN_FRAC || box.h < IDEO_MIN_FRAC)){+ if (drag.mode === 'create' && (box.w < IDEO_MIN_FRAC || box.h < IDEO_MIN_FRAC)){// tiny drag = click → snap to a default-sized box centered on the clickconst d = ideoDefaultBox('text');box.w = d.w; box.h = d.h;@@ -23042,7 +23945,14 @@ function ideoStagePointerUp(ev){ideoClearGuides();ideoRender();ideoRefreshRaw();- if (box && box.type === 'text'){ const inp = document.getElementById('ideoInspText'); if (inp) { try { inp.focus(); } catch(e){} } }+ // Enter inline edit when a text box was just drawn (drag-create is always a+ // text box), or any already-selected box was clicked without dragging+ // (Figma-style click-to-edit — text boxes type, object boxes describe).+ const enterEdit = box && (+ (drag.mode === 'create' && box.type === 'text') ||+ (drag.mode === 'move' && !drag.moved && drag.wasSel)+ );+ if (enterEdit) ideoEnterEdit(box.id);}function ideoApplyResize(box, handle, orig, dx, dy){let x0 = orig.x, y0 = orig.y, x1 = orig.x + orig.w, y1 = orig.y + orig.h;@@ -23118,6 +24028,9 @@ function ideoOnKeyDown(ev){window.addEventListener('pointerup', ideoStagePointerUp);window.addEventListener('pointercancel', ideoStagePointerUp);window.addEventListener('keydown', ideoOnKeyDown);+ window.addEventListener('resize', function(){+ if (document.body.classList.contains('ideo-canvas-on')) ideoFitStage();+ });})();async function imgStudioGenerate() {@@ -23211,6 +24124,10 @@ async function imgStudioGenerate() {}const r = await fetch('/queue/add', { method: 'POST', body: fd });if (!r.ok) throw new Error(`HTTP ${r.status}`);+ // The job is queued — if the Ideogram canvas is holding the right stage,+ // flip it to Result so the player (with its inline job-progress overlay) is+ // visible the moment the render starts.+ if (typeof ideoInLayout === 'function' && ideoInLayout() && typeof stageSetMode === 'function') stageSetMode('result');document.getElementById('imgStudioStatus').textContent ='Submitted. Watch Now / Recent → Photos.';// Non-blocking toast — the user can stay focused on the form (e.g.@@ -27030,6 +27947,10 @@ function _imgEngineLabel(token) {function animateFromPhoto(payload) {if (!payload || !payload.path) return;+ // Leave the Studio/Ideogram workflow first — otherwise body[data-workflow]+ // stays "studio" and its CSS keeps the video form (#genForm) hidden, so+ // setMode('i2v') alone would look like nothing happened. Back to Video.+ if (typeof workflowSwitch === 'function') workflowSwitch('manual');// Switch to i2v mode (pill + form fields). setMode('i2v') hides the// Studio pane, shows the video form, and applies the i2v-specific// dropdown selection.@@ -27267,6 +28188,17 @@ function findOutputByPath(path) {}function selectOutput(path) {activePath = path;+ // If the Ideogram editor canvas is holding the stage, a USER click on an+ // output means they want to see the render — flip the stage to Result.+ // Gate on isTrusted so the boot-time auto-select of the newest output (and+ // any other programmatic selection) can't yank someone out of mid-edit;+ // the Generate flow flips explicitly in imgStudioGenerate.+ var _uev = (typeof window !== 'undefined') ? window.event : null;+ if (_uev && _uev.isTrusted &&+ typeof ideoInLayout === 'function' && ideoInLayout() &&+ typeof stageSetMode === 'function') {+ stageSetMode('result');+ }document.querySelectorAll('.car-card').forEach(el => el.classList.toggle('active', el.dataset.path === path));const wrap = document.getElementById('playerWrap');wrap.classList.remove('empty');@@ -31320,6 +32252,10 @@ function workflowSwitch(name) {// Manual — show the video form, restore the previous video mode.if (manual) manual.style.display = '';}+ // The Ideogram edit canvas belongs to the Images workflow only — re-sync+ // so leaving suspends it (player/outputs restored) and returning to+ // Images with Ideogram active brings it back with its boxes intact.+ if (typeof ideoSyncStage === 'function') ideoSyncStage();try { localStorage.setItem('phos_workflow', name); } catch(e) {}}