Skip to content

Canvix — Free & open-source graphic design tool. A community-built Canva alternative with a drag-and-drop editor, templates, shapes, text, images & one-click export. Works on desktop, tablet & mobile.

feat(v0.31): the DesignAgent generation panel — the P1 gap closed

Ashiq Hussain Mir2ae7e24482cd
diff --git a/AGENTS.md b/AGENTS.md
new file mode 100644
index 0000000..439a977
--- /dev/null
+++ b/AGENTS.md
@@ -0,0 +1,30 @@
+# AGENTS.md — canvix
+
+## Git identity (mandatory)
+
+All commits in this repository MUST be authored as:
+
+- **Name:** `Ashiq Hussain Mir`
+- **Email:** `[email protected]`
+- **GitHub account:** `mir-ashiq`
+
+If a noreply address is ever required, the only correct form is
+`[email protected]` (user-id prefix + hyphen).
+A wrong noreply guess (`mirashiq@…`) once attributed commits to an
+unrelated account and added a phantom contributor (corrected 25 Nov 2026).
+
+Verify before committing:
+
+```bash
+git config user.name # Ashiq Hussain Mir
+git config user.email # [email protected]
+```
+
+## Gates (CI order — all green before push)
+
+```bash
+npm run typecheck
+npm run lint
+npm test
+npm run build
+```
diff --git a/src/app/api/ai/capabilities/route.ts b/src/app/api/ai/capabilities/route.ts
index bfa2f62..f16107e 100644
--- a/src/app/api/ai/capabilities/route.ts
+++ b/src/app/api/ai/capabilities/route.ts
@@ -66,6 +66,10 @@ async function __canvix_get(req: NextRequest) {
themeGeneration: text && flags.themeGeneration, // palette + fonts
altText: vision && flags.altText, // image alt text
videoGeneration: video && flags.videoGeneration, // text-to-video slot
+ // v0.31 (DesignAgent panel): the autonomous pipeline — needs the
+ // text slot (planning/revision); the vision slot is OPTIONAL
+ // (visual verification discloses 'unavailable' without it)
+ designAgent: text && flags.designAgent,
upscale: true, // local Lanczos — always available
brushEraser: true, // local inpainting — always available
},
diff --git a/src/components/editor/apps/DesignAgentApp.tsx b/src/components/editor/apps/DesignAgentApp.tsx
new file mode 100644
index 0000000..7c97a8d
--- /dev/null
+++ b/src/components/editor/apps/DesignAgentApp.tsx
@@ -0,0 +1,603 @@
+'use client'
+
+// ─────────────────────────────────────────────────────────────
+// DesignAgentApp (v0.31) — the autonomous generation panel.
+//
+// The P1 gap from docs/DESIGN-AGENT-ACCEPTANCE-REPORT.md: the
+// pipeline shipped as a server contract (POST /api/ai/design-agent)
+// without a user-facing surface. This panel is that surface, and it
+// follows the house rule every generative surface obeys: NOTHING
+// lands on the canvas unreviewed.
+//
+// prompt → [autonomous run, live status + stop]
+// → REVIEW GATE: brief contract, per-page toggles,
+// conformance failures, the full run report
+// → Apply: deterministic canvas fit + appendPages
+// (one Ctrl+Z removes everything)
+// → optional post-apply critique on the REAL rendered
+// pixels (captureStage → vision slot — the browser
+// capture path, richer than the server's SVG review)
+//
+// Honesty rules mirrored from the pipeline: a failed run shows its
+// evidence (best-attempt pages + unmet requirements) instead of a
+// shrug; visual verification says 'unavailable' when no vision
+// model answers; abort keeps nothing applied.
+// ─────────────────────────────────────────────────────────────
+
+import { useEffect, useRef, useState } from 'react'
+import {
+ Workflow, Loader2, Sparkles, Check, Square, Gauge, ListChecks, Eye, EyeOff,
+ ShieldCheck, ShieldAlert, RefreshCw, X,
+} from 'lucide-react'
+import { useEditorStore } from '@/store/editor-store'
+import { AppHeader, type AppPanelProps } from './registry'
+import { cn } from '@/lib/utils'
+import { toast } from '@/hooks/use-toast'
+import { useAICapabilities } from '@/hooks/use-ai-capabilities'
+import { requestDesignAgent, critiqueAppliedCanvas, type DesignAgentRunResult } from '@/lib/design-agent/client'
+import { fitPagesToCanvas } from '@/lib/design-agent/fit-pages'
+import type { PageData, AnyElement } from '@/lib/types'
+import type { AgentOutcome } from '@/lib/design-agent/pipeline'
+
+const IDEAS = [
+ 'A launch poster for a night market — "Friday 6 PM", neon purples, 1080×1350',
+ 'An Instagram post for a bakery opening — warm cream and chocolate browns, exact text "Opening 12 March"',
+ 'A conference poster: "AI & Design 2026", 3–5 March, blue #1B4F9C accents',
+]
+
+/** the pipeline's fixed stage order — shown while it runs so the wait
+ * is legible; the authoritative record is the run report at the end */
+const EXPECTED_STAGES = [
+ 'extract brief',
+ 'plan',
+ 'compile + checks',
+ 'brief conformance',
+ 'self-correction',
+ 'render',
+ 'visual critique',
+ 'final verdict',
+]
+
+const OUTCOME_META: Record<AgentOutcome, { label: string; tone: string }> = {
+ passed: { label: 'Passed — every deterministic check is green', tone: 'border-emerald-400/30 bg-emerald-400/10 text-emerald-200' },
+ 'failed-honestly': { label: 'Stopped honestly — the agent would not fake a pass', tone: 'border-amber-400/30 bg-amber-400/10 text-amber-200' },
+ 'guard-cycled': { label: 'Stopped by the cycle guard — the plan repeated itself', tone: 'border-amber-400/30 bg-amber-400/10 text-amber-200' },
+ aborted: { label: 'Stopped — nothing was applied', tone: 'border-white/15 bg-white/5 text-white/70' },
+}
+
+const VISUAL_META: Record<string, string> = {
+ verified: 'verified',
+ improved: 'improved after critique',
+ unverified: 'ran without vision critique',
+ unavailable: 'unavailable (no vision model)',
+ aborted: 'aborted',
+}
+
+const PASS_ACTION_META: Record<string, string> = {
+ revised: 'revision accepted',
+ 'accepted-as-is': 'accepted as-is',
+ 'rejected-by-checks': 'rejected — checks outrank critique',
+ 'no-improvement': 'kept previous (no improvement)',
+ 'budget-exhausted': 'budget exhausted',
+ 'unparseable-discarded': 'discarded unparseable critique',
+ unavailable: 'unavailable',
+}
+
+export function DesignAgentApp({ onClose }: AppPanelProps) {
+ const [prompt, setPrompt] = useState('')
+ const [revisions, setRevisions] = useState(3)
+ /** null = follow the vision capability; the user can always opt out */
+ const [visualOverride, setVisualOverride] = useState<boolean | null>(null)
+ const [busy, setBusy] = useState(false)
+ const [elapsed, setElapsed] = useState(0)
+ const [result, setResult] = useState<DesignAgentRunResult | null>(null)
+ const [enabled, setEnabled] = useState<boolean[]>([])
+ const [applied, setApplied] = useState(false)
+ const [critique, setCritique] = useState<{ findings: { severity: string; issue: string; fix?: string }[]; note: string } | null>(null)
+ const [critiquing, setCritiquing] = useState(false)
+ const abortRef = useRef<AbortController | null>(null)
+
+ const width = useEditorStore((s) => s.width)
+ const height = useEditorStore((s) => s.height)
+ const { configured: aiConfigured, loaded: aiLoaded, vision, designAgent } = useAICapabilities()
+
+ const visualCheck = visualOverride ?? vision
+ const runAvailable = aiLoaded && aiConfigured && designAgent
+
+ // elapsed timer while the pipeline runs (reset at run start; the
+ // effect itself only ticks — no synchronous setState in effects)
+ useEffect(() => {
+ if (!busy) return
+ const started = Date.now()
+ const t = setInterval(() => setElapsed(Math.round((Date.now() - started) / 1000)), 1000)
+ return () => clearInterval(t)
+ }, [busy])
+
+ const generate = async () => {
+ const text = prompt.trim()
+ if (!text || busy) return
+ const controller = new AbortController()
+ abortRef.current = controller
+ setElapsed(0)
+ setBusy(true)
+ setResult(null)
+ setEnabled([])
+ setApplied(false)
+ setCritique(null)
+ try {
+ const res = await requestDesignAgent(
+ text,
+ { visual: visualCheck, maxRevisions: revisions },
+ controller.signal,
+ )
+ setResult(res)
+ setEnabled(res.pages.map(() => true))
+ if (!res.pages.length && res.error) {
+ toast({ title: 'The design agent stopped', description: res.error })
+ }
+ } catch (e) {
+ if (e instanceof Error && e.name === 'AbortError') {
+ setResult({ outcome: 'aborted', pages: [], briefConformance: [], warnings: [], report: null, format: null })
+ } else {
+ toast({ title: 'The design agent failed', description: e instanceof Error ? e.message : 'Unknown error' })
+ }
+ } finally {
+ setBusy(false)
+ abortRef.current = null
+ }
+ }
+
+ const stop = () => abortRef.current?.abort()
+
+ const apply = () => {
+ if (!result || applied) return
+ const chosen = result.pages.filter((_, i) => enabled[i])
+ if (!chosen.length) {
+ toast({ title: 'Nothing to apply', description: 'Enable at least one page first.' })
+ return
+ }
+ const from = result.format ?? { width, height }
+ const fit = fitPagesToCanvas(chosen, from, { width, height })
+ useEditorStore.getState().appendPages(fit.pages)
+ setApplied(true)
+ toast({
+ title: `${chosen.length} page${chosen.length > 1 ? 's' : ''} added`,
+ description:
+ fit.scale !== 1
+ ? `Rescaled from ${from.width}×${from.height} to fit the ${width}×${height} canvas — fully editable. Ctrl+Z removes them all.`
+ : 'Fully editable elements. Ctrl+Z removes them all.',
+ })
+ }
+
+ /** post-apply critique: capture the REAL rendered canvas (the newly
+ * appended page is current) and ask the vision slot against the brief */
+ const runCritique = async () => {
+ if (!result || critiquing) return
+ setCritiquing(true)
+ try {
+ const { captureStage } = await import('@/components/editor/canvas/canvas-bridge')
+ const image = await captureStage({ pixelScale: 0.5, mimeType: 'image/jpeg', quality: 0.7 })
+ const brief = result.report?.brief
+ ? {
+ requiredTexts: result.report.brief.requiredTexts.map((t) => ({ text: t.text })),
+ requiredColors: result.report.brief.requiredColors.map((c) => ({ hex: c.hex })),
+ }
+ : { requiredTexts: [], requiredColors: [] }
+ setCritique(await critiqueAppliedCanvas(image, brief))
+ } catch {
+ setCritique({ findings: [], note: 'Could not capture the canvas — critique skipped.' })
+ } finally {
+ setCritiquing(false)
+ }
+ }
+
+ const brief = result?.report?.brief
+ const specFormat = result?.format
+ const rescale = specFormat && (specFormat.width !== width || specFormat.height !== height)
+ const enabledCount = enabled.filter(Boolean).length
+ const meta = result ? OUTCOME_META[result.outcome] : null
+
+ return (
+ <div data-testid="design-agent-panel">
+ <AppHeader icon={Workflow} title="Design agent" onClose={onClose} />
+
+ {aiLoaded && !aiConfigured && (
+ <div data-testid="design-agent-no-provider" className="mb-3 rounded-xl border border-amber-500/30 bg-amber-500/10 px-3 py-2.5 text-[11.5px] text-amber-200">
+ No AI provider configured yet — add a key in Admin → AI &amp; models (or your profile) to use the design agent.
+ </div>
+ )}
+ {aiLoaded && aiConfigured && !designAgent && (
+ <div className="mb-3 rounded-xl border border-amber-500/30 bg-amber-500/10 px-3 py-2.5 text-[11.5px] text-amber-200">
+ The design agent is switched off in the admin console (AI &amp; models → features).
+ </div>
+ )}
+
+ <p className="text-[11.5px] text-white/50 leading-snug mb-3">
+ One prompt in — the agent plans, compiles, checks and visually reviews a real design. Nothing lands
+ until you review it; a failed run shows its evidence, never a fake pass.
+ </p>
+
+ <textarea
+ value={prompt}
+ onChange={(e) => setPrompt(e.target.value)}
+ placeholder="e.g. A 1080×1350 poster for a marathon — “Run Dal Lake 2026”, 26 Jan, navy #1B3A6B + coral accents, stats band, framed photo area"
+ className="w-full h-24 rounded-xl bg-white/[0.05] border border-white/10 text-white text-[12.5px] p-3 resize-none focus:outline-none focus:border-[#02C0CC]"
+ maxLength={2000}
+ aria-label="Design brief"
+ data-testid="design-agent-prompt"
+ />
+
+ {/* options row */}
+ <div className="flex items-center gap-2 mt-2.5 flex-wrap">
+ <button
+ onClick={() => setVisualOverride(!visualCheck)}
+ disabled={!vision}
+ title={vision ? 'Render the pages and critique the real pixels before you review' : 'No vision model configured'}
+ className={cn(
+ 'flex items-center gap-1.5 h-8 px-2.5 rounded-lg text-[11px] font-semibold transition-colors',
+ visualCheck ? 'bg-[#02C0CC]/15 text-[#02C0CC]' : 'bg-white/5 text-white/45',
+ !vision && 'opacity-50 cursor-not-allowed',
+ )}
+ aria-pressed={visualCheck}
+ >
+ {visualCheck ? <Eye size={12} /> : <EyeOff size={12} />}
+ Visual check
+ </button>
+ <div className="flex items-center gap-1" role="group" aria-label="Revision rounds">
+ {[1, 2, 3].map((n) => (
+ <button
+ key={n}
+ onClick={() => setRevisions(n)}
+ className={cn(
+ 'w-7 h-7 rounded-lg text-[11px] font-bold transition-colors',
+ revisions === n ? 'bg-[#7630D7] text-white' : 'bg-white/[0.05] text-white/55 hover:bg-white/10',
+ )}
+ aria-pressed={revisions === n}
+ title={`${n} self-correction round${n > 1 ? 's' : ''}`}
+ >
+ {n}
+ </button>
+ ))}
+ </div>
+ <button
+ className="btn-cv h-9 px-4 ml-auto text-[12px] flex items-center gap-1.5 disabled:opacity-60"
+ onClick={() => void generate()}
+ disabled={busy || !prompt.trim() || (aiLoaded && (!aiConfigured || !designAgent))}
+ data-testid="design-agent-run"
+ >
+ {busy ? <Loader2 size={13} className="animate-spin" /> : <Sparkles size={13} />}
+ {busy ? 'Running…' : 'Generate'}
+ </button>
+ </div>
+
+ {!result && !busy && (
+ <div className="mt-3 space-y-1.5">
+ {IDEAS.map((idea) => (
+ <button
+ key={idea}
+ onClick={() => setPrompt(idea)}
+ className="block w-full text-left text-[11px] text-white/45 hover:text-white/80 px-2.5 py-2 rounded-lg hover:bg-white/[0.04] transition-colors"
+ >
+ &ldquo;{idea}&rdquo;
+ </button>
+ ))}
+ </div>
+ )}
+
+ {/* the live run */}
+ {busy && (
+ <div className="mt-4 rounded-xl border border-white/10 bg-white/[0.02] p-3" data-testid="design-agent-running">
+ <div className="flex items-center gap-2 text-[11.5px] text-white/60 mb-2">
+ <Loader2 size={12} className="animate-spin text-[#02C0CC]" />
+ The autonomous pipeline is running
+ <span className="text-white/35">{elapsed}s</span>
+ <button onClick={stop} className="ml-auto flex items-center gap-1 px-2 h-6 rounded-full bg-red-500/15 text-red-300 text-[10.5px] font-semibold hover:bg-red-500/25 transition-colors">
+ <Square size={9} /> Stop
+ </button>
+ </div>
+ <ol className="space-y-0.5">
+ {EXPECTED_STAGES.map((s, i) => (
+ <li key={s} className={cn('text-[11px] flex items-center gap-1.5', i === 0 ? 'text-white/70' : 'text-white/35')}>
+ <span className={cn('w-1 h-1 rounded-full', i === 0 ? 'bg-[#02C0CC]' : 'bg-white/20')} />
+ {s}
+ </li>
+ ))}
+ </ol>
+ <p className="mt-2 text-[10px] text-white/30 leading-snug">
+ Stage order is fixed; the authoritative record arrives with the run report.
+ </p>
+ </div>
+ )}
+
+ {/* the review gate */}
+ {result && !busy && (
+ <div className="mt-4 space-y-3" data-testid="design-agent-result">
+ {meta && (
+ <div className={cn('rounded-xl border px-3 py-2 text-[12px] font-semibold flex items-center gap-1.5', meta.tone)} data-testid="design-agent-outcome">
+ {result.outcome === 'passed' ? <ShieldCheck size={13} /> : <ShieldAlert size={13} />}
+ {meta.label}
+ </div>
+ )}
+
+ {result.error && (
+ <p className="rounded-xl border border-red-400/30 bg-red-400/10 px-3 py-2 text-[11.5px] text-red-200">
+ {result.error}
+ </p>
+ )}
+
+ {/* brief contract */}
+ {brief && (brief.requiredTexts.length > 0 || brief.requiredColors.length > 0) && (
+ <div className="rounded-xl border border-white/10 bg-white/[0.03] px-3 py-2">
+ <p className="text-[10px] font-bold uppercase tracking-wide text-white/50 mb-1.5">Brief contract (checked verbatim)</p>
+ <div className="flex flex-wrap gap-1.5">
+ {brief.requiredTexts.map((t, i) => (
+ <span key={i} className="px-1.5 py-0.5 rounded bg-white/8 text-[10.5px] text-white/80 font-medium">
+ &ldquo;{t.text}&rdquo;
+ </span>
+ ))}
+ {brief.requiredColors.map((c, i) => (
+ <span key={i} className="px-1.5 py-0.5 rounded bg-white/8 text-[10.5px] text-white/80 font-mono flex items-center gap-1">
+ <span className="w-2.5 h-2.5 rounded-sm inline-block border border-white/20" style={{ background: c.hex }} />
+ {c.hex.toUpperCase()}
+ </span>
+ ))}
+ </div>
+ </div>
+ )}
+
+ {/* conformance failures — the honest-failure evidence */}
+ {result.briefConformance.length > 0 && (
+ <div className="rounded-xl border border-amber-400/25 bg-amber-400/8 px-3 py-2" data-testid="design-agent-conformance">
+ <p className="text-[10px] font-bold uppercase tracking-wide text-amber-300/80 mb-1">Unmet requirements</p>
+ <ul className="space-y-0.5">
+ {result.briefConformance.map((f, i) => (
+ <li key={i} className="text-[11px] text-amber-100/80 leading-snug">· {f}</li>
+ ))}
+ </ul>
+ </div>
+ )}
+ {result.warnings.length > 0 && (
+ <div className="rounded-xl border border-white/10 bg-white/[0.02] px-3 py-2">
+ <p className="text-[10px] font-bold uppercase tracking-wide text-white/45 mb-1">Warnings</p>
+ <ul className="space-y-0.5">
+ {result.warnings.map((w, i) => (
+ <li key={i} className="text-[11px] text-white/55 leading-snug">· {w}</li>
+ ))}
+ </ul>
+ </div>
+ )}
+
+ {/* pages review */}
+ {result.pages.length > 0 && (
+ <div className="space-y-2">
+ {result.pages.map((page, i) => (
+ <PageReviewCard
+ key={i}
+ index={i}
+ page={page}
+ specWidth={specFormat?.width ?? width}
+ specHeight={specFormat?.height ?? height}
+ enabled={enabled[i] ?? true}
+ onToggle={() => setEnabled((e) => e.map((v, j) => (j === i ? !v : v)))}
+ />
+ ))}
+ </div>
+ )}
+
+ {/* apply row */}
+ {result.pages.length > 0 && (
+ <div className="flex items-center gap-2 flex-wrap">
+ {!applied ? (
+ <>
+ <button className="btn-cv h-8 px-3.5 text-[11.5px] flex items-center gap-1.5" onClick={apply} data-testid="design-agent-apply">
+ <Check size={12} /> Apply {enabledCount > 0 ? `${enabledCount} page${enabledCount > 1 ? 's' : ''}` : ''}
+ </button>
+ {rescale && (
+ <span className="text-[10.5px] text-white/40">
+ pages were compiled {specFormat!.width}×{specFormat!.height} — they will be rescaled to fit this {width}×{height} design
+ </span>
+ )}
+ <button
+ className="flex items-center gap-1 text-[11px] text-white/50 hover:text-white px-2 h-8 rounded-lg hover:bg-white/10"
+ onClick={() => void generate()}
+ >
+ <RefreshCw size={11} /> Try again
+ </button>
+ <button
+ className="flex items-center gap-1 text-[11px] text-white/50 hover:text-white px-2 h-8 rounded-lg hover:bg-white/10"
+ onClick={() => { setResult(null); setPrompt('') }}
+ >
+ <X size={11} /> Discard
+ </button>
+ </>
+ ) : (
+ <>
+ <button
+ className="h-8 px-3.5 rounded-lg bg-white/10 text-white/85 text-[11.5px] font-semibold hover:bg-white/15 transition-colors flex items-center gap-1.5 disabled:opacity-50"
+ onClick={() => void runCritique()}
+ disabled={critiquing}
+ data-testid="design-agent-critique"
+ >
+ {critiquing ? <Loader2 size={12} className="animate-spin" /> : <Eye size={12} />}
+ {critiquing ? 'Critiquing…' : 'Critique the result'}
+ </button>
+ <button className="btn-cv h-8 px-3.5 text-[11.5px]" onClick={onClose}>
+ Done
+ </button>
+ </>
+ )}
+ </div>
+ )}
+
+ {/* post-apply critique on the real pixels */}
+ {critique && (
+ <div className="rounded-xl border border-white/10 bg-white/[0.03] px-3 py-2" data-testid="design-agent-critique-result">
+ <p className="text-[10px] font-bold uppercase tracking-wide text-white/50 mb-1.5">Visual critique (real pixels)</p>
+ {critique.findings.length === 0 ? (
+ <p className="text-[11px] text-white/55 leading-snug">{critique.note}</p>
+ ) : (
+ <>
+ <ul className="space-y-1 mb-1.5">
+ {critique.findings.slice(0, 8).map((f, i) => (
+ <li key={i} className="text-[11px] leading-snug">
+ <span
+ className={cn(
+ 'inline-block px-1.5 rounded text-[10px] font-bold mr-1.5',
+ f.severity === 'P1' && 'bg-amber-500/20 text-amber-300',
+ f.severity === 'P2' && 'bg-blue-500/20 text-blue-300',
+ f.severity === 'P3' && 'bg-white/10 text-white/50',
+ )}
+ >
+ {f.severity}
+ </span>
+ <span className="text-white/80">{f.issue}</span>
+ {f.fix && <span className="text-white/45"> → {f.fix}</span>}
+ </li>
+ ))}
+ </ul>
+ <p className="text-[10px] text-white/35">{critique.note}</p>
+ </>
+ )}
+ </div>
+ )}
+
+ {/* the run report */}
+ {result.report && (
+ <details className="rounded-xl border border-white/8 bg-white/[0.02] overflow-hidden" data-testid="design-agent-report">
+ <summary className="px-3 py-2 text-[11px] font-semibold text-white/60 cursor-pointer flex items-center gap-1.5">
+ <ListChecks size={12} /> Run report
+ {result.report.modelCalls > 0 && (
+ <span className="text-white/35">
+ · {result.report.modelCalls} model calls · {result.report.revisionRounds} revision rounds
+ </span>
+ )}
+ </summary>
+ <div className="px-3 pb-2.5 space-y-2">
+ <div className="space-y-1">
+ {result.report.stages.map((s, i) => (
+ <div key={i} className="flex items-start gap-1.5 text-[10.5px] leading-snug">
+ <span className={s.ok ? 'text-emerald-400' : 'text-red-400'}>{s.ok ? '✓' : '✗'}</span>
+ <span className="text-white/75 font-mono shrink-0">{s.name}</span>
+ {s.modelCall && <span className="text-[#02C0CC]/70 text-[9.5px] mt-px">model</span>}
+ <span className="text-white/45 min-w-0">{s.detail}</span>
+ </div>
+ ))}
+ </div>
+ {result.report.visualPasses.length > 0 && (
+ <div className="border-t border-white/8 pt-2 space-y-1.5">
+ <p className="text-[10px] font-bold uppercase tracking-wide text-white/40">
+ Visual verification — {VISUAL_META[result.report.visualVerification] ?? result.report.visualVerification}
+ </p>
+ {result.report.visualPasses.map((p, i) => (
+ <div key={i} className="rounded-lg border border-white/8 px-2.5 py-1.5">
+ <div className="flex items-center gap-1.5 text-[10.5px] text-white/60">
+ <span className="font-mono">pass {p.round}</span>
+ <span className="px-1.5 rounded bg-[#02C0CC]/10 text-[#02C0CC] text-[9.5px] font-semibold">
+ {PASS_ACTION_META[p.action] ?? p.action}
+ </span>
+ </div>
+ {p.summary && <p className="text-[10.5px] text-white/45 mt-0.5 leading-snug">{p.summary}</p>}
+ {p.issues.slice(0, 5).map((issue, j) => (
+ <p key={j} className="text-[10.5px] text-white/55 leading-snug mt-0.5">
+ <span className="font-bold mr-1">{issue.severity}</span>
+ {issue.issue}
+ </p>
+ ))}
+ </div>
+ ))}
+ </div>
+ )}
+ <div className="flex items-center gap-2 text-[10px] text-white/35 border-t border-white/8 pt-2">
+ <Gauge size={10} />
+ <span>
+ {result.report.modelCalls} model calls · {result.report.revisionRounds} revision rounds
+ {result.report.cycleGuard ? ' · cycle guard tripped' : ''}
+ {result.report.spec?.format
+ ? ` · compiled ${result.report.spec.format.width}×${result.report.spec.format.height}`
+ : ''}
+ </span>
+ </div>
+ </div>
+ </details>
+ )}
+ </div>
+ )}
+ </div>
+ )
+}
+
+/** One reviewable compiled page: honest geometry wireframe + toggle. */
+function PageReviewCard({
+ index,
+ page,
+ specWidth,
+ specHeight,
+ enabled,
+ onToggle,
+}: {
+ index: number
+ page: PageData
+ specWidth: number
+ specHeight: number
+ enabled: boolean
+ onToggle: () => void
+}) {
+ const previewW = 96
+ const previewH = Math.max(48, Math.round((previewW * specHeight) / Math.max(1, specWidth)))
+ const bg =
+ page.background?.type === 'gradient'
+ ? `linear-gradient(${page.background.angle}deg, ${page.background.from}, ${page.background.to})`
+ : page.background?.type === 'solid'
+ ? page.background.color
+ : '#FFFFFF'
+
+ return (
+ <div
+ className={cn(
+ 'rounded-xl border p-2.5 flex gap-2.5 items-center transition-colors',
+ enabled ? 'border-white/12 bg-white/[0.04]' : 'border-white/8 bg-white/[0.015] opacity-60',
+ )}
+ >
+ <button
+ onClick={onToggle}
+ className={cn(
+ 'w-5 h-5 rounded-md border flex items-center justify-center shrink-0 transition-colors',
+ enabled ? 'bg-[#02C0CC] border-[#02C0CC] text-black' : 'border-white/25 text-transparent',
+ )}
+ aria-label={enabled ? `Disable page ${index + 1}` : `Enable page ${index + 1}`}
+ aria-pressed={enabled}
+ >
+ <Check size={12} strokeWidth={3.5} />
+ </button>
+ <div
+ className="relative shrink-0 rounded-lg overflow-hidden border border-white/10"
+ style={{ width: previewW, height: previewH, background: bg }}
+ aria-hidden
+ >
+ {page.elements.map((el: AnyElement) => (
+ <div
+ key={el.id}
+ className="absolute rounded-[2px]"
+ style={{
+ left: `${(el.x / specWidth) * 100}%`,
+ top: `${(el.y / specHeight) * 100}%`,
+ width: `${(el.width / specWidth) * 100}%`,
+ height: `${(el.height / specHeight) * 100}%`,
+ background: el.type === 'text' ? 'rgba(255,255,255,0.75)' : (('fill' in el ? String((el as { fill?: unknown }).fill ?? '') : '') || '#02C0CC'),
+ opacity: el.type === 'text' ? 0.85 : 0.8,
+ minHeight: 2,
+ }}
+ />
+ ))}
+ </div>
+ <div className="flex-1 min-w-0">
+ <p className="text-[11.5px] font-semibold text-white/85">
+ Page {index + 1}
+ <span className="ml-1.5 text-[10px] text-white/35 font-normal">
+ {specWidth}×{specHeight} · {page.elements.length} element{page.elements.length === 1 ? '' : 's'}
+ </span>
+ </p>
+ </div>
+ </div>
+ )
+}
diff --git a/src/components/editor/apps/registry.tsx b/src/components/editor/apps/registry.tsx
index 7a0dc32..1dac597 100644
--- a/src/components/editor/apps/registry.tsx
+++ b/src/components/editor/apps/registry.tsx
@@ -1,6 +1,6 @@
'use client'
-import { BarChart3, QrCode, Smile, Shapes, Palette, Type, Wand2, ImageIcon, Bot, LayoutTemplate, SwatchBook, Video } from 'lucide-react'
+import { BarChart3, QrCode, Smile, Shapes, Palette, Type, Wand2, ImageIcon, Bot, LayoutTemplate, SwatchBook, Video, Workflow } from 'lucide-react'
import type { LucideIcon } from 'lucide-react'
/** ── Canvix app registry ─────────────────────────────────────
@@ -36,10 +36,14 @@ import { AIImageApp } from './AIImageApp'
import { AssistantApp } from './AssistantApp'
import { AIPlanApp } from './AIPlanApp'
import { AIThemeApp } from './AIThemeApp'
+import { DesignAgentApp } from './DesignAgentApp'
export const CANVIX_APPS: CanvixApp[] = [
// AI apps first (canva 2026: “Canva AI” is the flagship surface)
{ id: 'assistant', name: 'Canvix AI', desc: 'Chat copilot — copy, palettes, images, translate', icon: Bot, color: '#7630D7', content: AssistantApp },
+ // v0.31: the autonomous generation pipeline — one prompt →
+ // planned, compiled, checked, visually reviewed, REVIEWED BY YOU
+ { id: 'design-agent', name: 'Design agent', desc: 'One prompt in — a checked, reviewed, editable design out', icon: Workflow, color: '#02C0CC', content: DesignAgentApp },
{ id: 'magic-write', name: 'Magic Write', desc: 'AI copy — headlines, taglines & captions', icon: Wand2, color: '#9B6BFF', content: MagicWriteApp },
{ id: 'ai-image', name: 'AI image generator', desc: 'Turn a prompt into a design-ready image', icon: ImageIcon, color: '#02C0CC', content: AIImageApp },
// v0.19 (Wave 4 — AI Power-Up):
diff --git a/src/hooks/use-ai-capabilities.ts b/src/hooks/use-ai-capabilities.ts
index 865be09..2361df8 100644
--- a/src/hooks/use-ai-capabilities.ts
+++ b/src/hooks/use-ai-capabilities.ts
@@ -11,22 +11,31 @@ export function useAICapabilities() {
loaded: boolean
configured: boolean
vision: boolean
- }>({ loaded: false, configured: false, vision: false })
+ designAgent: boolean
+ }>({ loaded: false, configured: false, vision: false, designAgent: false })
useEffect(() => {
let cancelled = false
fetch('/api/ai/capabilities')
.then((r) => (r.ok ? r.json() : null))
- .then((data: { configured?: boolean; capabilities?: { vision?: boolean } } | null) => {
- if (cancelled || !data) return
- setState({
- loaded: true,
- configured: Boolean(data.configured),
- vision: Boolean(data.capabilities?.vision),
- })
- })
+ .then(
+ (
+ data: {
+ configured?: boolean
+ capabilities?: { vision?: boolean; designAgent?: boolean }
+ } | null,
+ ) => {
+ if (cancelled || !data) return
+ setState({
+ loaded: true,
+ configured: Boolean(data.configured),
+ vision: Boolean(data.capabilities?.vision),
+ designAgent: Boolean(data.capabilities?.designAgent),
+ })
+ },
+ )
.catch(() => {
- if (!cancelled) setState({ loaded: true, configured: false, vision: false })
+ if (!cancelled) setState({ loaded: true, configured: false, vision: false, designAgent: false })
})
return () => {
cancelled = true
diff --git a/src/lib/design-agent/client.test.ts b/src/lib/design-agent/client.test.ts
new file mode 100644
index 0000000..247cc3b
--- /dev/null
+++ b/src/lib/design-agent/client.test.ts
@@ -0,0 +1,137 @@
+// Unit tests for the design-agent client (v0.31) — the panel's typed
+// seam to POST /api/ai/design-agent. Verifies response parsing for
+// every status the route emits (200 / 422 / 499 / 429 / 503) plus the
+// post-apply critique helper's honest 503 handling.
+
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
+import { requestDesignAgent, critiqueAppliedCanvas } from './client'
+
+const fetchMock = vi.fn()
+beforeEach(() => {
+ vi.stubGlobal('fetch', fetchMock)
+})
+afterEach(() => {
+ vi.unstubAllGlobals()
+ vi.clearAllMocks()
+})
+
+function jsonResponse(status: number, body: unknown) {
+ return {
+ ok: status >= 200 && status < 300,
+ status,
+ json: async () => body,
+ }
+}
+
+const FULL_BODY = {
+ outcome: 'passed',
+ pages: [{ id: 'p1', background: { type: 'solid', color: '#fff' }, elements: [] }],
+ briefConformance: [],
+ warnings: ['vision slot missing'],
+ report: {
+ stages: [{ name: 'plan', ok: true, detail: 'round 1', modelCall: true }],
+ modelCalls: 5,
+ revisionRounds: 2,
+ cycleGuard: false,
+ brief: { requiredTexts: [{ text: '12km' }], requiredColors: [{ hex: '#1B4F9C' }], restrictions: [], notes: [] },
+ spec: { format: { width: 1080, height: 1350, preset: 'instagram-portrait' } },
+ limits: { maxRevisions: 3, maxVisualPasses: 1 },
+ visualPasses: [],
+ visualVerification: 'unavailable',
+ },
+}
+
+describe('requestDesignAgent', () => {
+ it('parses a passing run: pages, format from the report, brief, warnings', async () => {
+ fetchMock.mockResolvedValue(jsonResponse(200, FULL_BODY))
+ const r = await requestDesignAgent('a poster', { visual: true, maxRevisions: 3 })
+ expect(r.outcome).toBe('passed')
+ expect(r.pages).toHaveLength(1)
+ expect(r.format).toEqual({ width: 1080, height: 1350 })
+ expect(r.briefConformance).toEqual([])
+ expect(r.warnings).toEqual(['vision slot missing'])
+ expect(r.report?.modelCalls).toBe(5)
+ expect(r.error).toBeUndefined()
+ // the request body carried the panel options
+ const sent = JSON.parse(fetchMock.mock.calls[0]![1].body)
+ expect(sent).toEqual({ prompt: 'a poster', visual: true, maxRevisions: 3 })
+ })
+
+ it('a 422 honest failure is a RESULT (evidence preserved), not an error', async () => {
+ fetchMock.mockResolvedValue(jsonResponse(422, {
+ outcome: 'failed-honestly',
+ pages: FULL_BODY.pages,
+ briefConformance: ['required text "12km" not found in any text slot'],
+ warnings: [],
+ report: FULL_BODY.report,
+ }))
+ const r = await requestDesignAgent('a poster')
+ expect(r.outcome).toBe('failed-honestly')
+ expect(r.pages).toHaveLength(1) // best attempt still reviewable
+ expect(r.briefConformance[0]).toContain('12km')
+ expect(r.error).toBeUndefined()
+ })
+
+ it('a 499 abort maps to the aborted outcome', async () => {
+ fetchMock.mockResolvedValue(jsonResponse(499, { outcome: 'aborted', pages: [], briefConformance: [], warnings: [], report: null }))
+ const r = await requestDesignAgent('a poster')
+ expect(r.outcome).toBe('aborted')
+ expect(r.pages).toEqual([])
+ })
+
+ it('pre-pipeline rejections (503/429) synthesize a readable error', async () => {
+ fetchMock.mockResolvedValueOnce(jsonResponse(503, { error: 'no provider configured for the text slot' }))
+ fetchMock.mockResolvedValueOnce(jsonResponse(429, { error: 'Too many requests' }))
+ fetchMock.mockResolvedValueOnce(jsonResponse(503, {})) // no body → status hint
+
+ const a = await requestDesignAgent('x')
+ expect(a.outcome).toBe('failed-honestly')
+ expect(a.error).toBe('no provider configured for the text slot')
+
+ const b = await requestDesignAgent('x')
+ expect(b.error).toBe('Too many requests')
+
+ const c = await requestDesignAgent('x')
+ expect(c.error).toContain('unavailable')
+ })
+
+ it('a body-less 200 degrades safely (empty arrays, null report/format)', async () => {
+ fetchMock.mockResolvedValue(jsonResponse(200, {}))
+ const r = await requestDesignAgent('x')
+ expect(r.outcome).toBe('failed-honestly')
+ expect(r.pages).toEqual([])
+ expect(r.briefConformance).toEqual([])
+ expect(r.report).toBeNull()
+ expect(r.format).toBeNull()
+ })
+
+ it('propagates AbortError so the panel can show its stopped state', async () => {
+ const err = new Error('aborted')
+ err.name = 'AbortError'
+ fetchMock.mockRejectedValue(err)
+ await expect(requestDesignAgent('x')).rejects.toMatchObject({ name: 'AbortError' })
+ })
+})
+
+describe('critiqueAppliedCanvas', () => {
+ it('returns findings on success', async () => {
+ fetchMock.mockResolvedValue(jsonResponse(200, { findings: [{ severity: 'P1', issue: 'title clipped', fix: 'shrink' }] }))
+ const r = await critiqueAppliedCanvas('data:image/jpeg;base64,x', { requiredTexts: [{ text: 'A' }], requiredColors: [] })
+ expect(r.findings).toHaveLength(1)
+ expect(r.findings[0]!.issue).toBe('title clipped')
+ })
+
+ it('a 503 is honest availability, not a failure', async () => {
+ fetchMock.mockResolvedValue(jsonResponse(503, { available: false }))
+ const r = await critiqueAppliedCanvas('data:image/jpeg;base64,x', { requiredTexts: [], requiredColors: [] })
+ expect(r.findings).toEqual([])
+ expect(r.note).toContain('no vision model')
+ })
+
+ it('other failures surface the route error', async () => {
+ fetchMock.mockResolvedValue(jsonResponse(400, { error: 'image must be a data URL from the canvas capture.' }))
+ const r = await critiqueAppliedCanvas('not-a-data-url', { requiredTexts: [], requiredColors: [] })
+ expect(r.findings).toEqual([])
+ expect(r.note).toContain('data URL')
+ })
+})
diff --git a/src/lib/design-agent/client.ts b/src/lib/design-agent/client.ts
new file mode 100644
index 0000000..971a53d
--- /dev/null
+++ b/src/lib/design-agent/client.ts
@@ -0,0 +1,123 @@
+// ─────────────────────────────────────────────────────────────
+// design-agent client (v0.31) — the panel's typed seam to
+// POST /api/ai/design-agent.
+//
+// One call = one autonomous pipeline run (prompt → plan → compile +
+// checks → bounded self-correction → optional visual verification).
+// The response carries the compiled pages, the brief→spec
+// conformance failures, and the full run report. Statuses:
+// 200 passed · 422 failed-honestly (body still carries evidence)
+// 499 aborted (client cancel) · 429 rate-limited · 503 disabled
+// or no provider · 403 cross-site
+// Type-only imports from the pipeline — erased at compile time, so
+// this module stays client-safe.
+// ─────────────────────────────────────────────────────────────
+
+import type { PageData } from '@/lib/types'
+import type { AgentOutcome, AgentRunReport } from './pipeline'
+
+export interface DesignAgentRunResult {
+ outcome: AgentOutcome
+ /** the last accepted compiled pages (present even on failure — evidence) */
+ pages: PageData[]
+ /** brief→spec conformance failures, human-readable, quoted */
+ briefConformance: string[]
+ warnings: string[]
+ report: AgentRunReport | null
+ /** spec format the pages were compiled for (from the run report) */
+ format: { width: number; height: number } | null
+ /** set when the route rejected the run before the pipeline started */
+ error?: string
+}
+
+export interface DesignAgentRequestOptions {
+ /** opt out of the server-side visual-verification stage */
+ visual?: boolean
+ /** revision rounds cap (1–3; default 3) */
+ maxRevisions?: number
+}
+
+const STATUS_HINTS: Record<number, string> = {
+ 403: 'Cross-site request rejected.',
+ 429: 'Rate limited — wait a moment and try again.',
+ 503: 'The design agent is unavailable (disabled or no AI provider configured).',
+}
+
+/** Run the autonomous design pipeline. Throws on network/abort errors
+ * (AbortError propagates for cancellation); pipeline failures come
+ * back as a normal result (outcome 'failed-honestly' etc.). */
+export async function requestDesignAgent(
+ prompt: string,
+ opts: DesignAgentRequestOptions = {},
+ signal?: AbortSignal,
+): Promise<DesignAgentRunResult> {
+ const res = await fetch('/api/ai/design-agent', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ signal,
+ body: JSON.stringify({ prompt, visual: opts.visual, maxRevisions: opts.maxRevisions }),
+ })
+ const data = (await res.json().catch(() => ({}))) as {
+ outcome?: AgentOutcome
+ pages?: PageData[]
+ briefConformance?: string[]
+ warnings?: string[]
+ report?: AgentRunReport | null
+ error?: string
+ }
+
+ // pre-pipeline rejections (429/503/403…) carry only { error }
+ if (!res.ok && !data.outcome) {
+ return {
+ outcome: 'failed-honestly',
+ pages: [],
+ briefConformance: [],
+ warnings: [],
+ report: null,
+ format: null,
+ error: data.error ?? STATUS_HINTS[res.status] ?? `The design agent failed (${res.status}).`,
+ }
+ }
+
+ const format = data.report?.spec?.format
+ return {
+ outcome: data.outcome ?? 'failed-honestly',
+ pages: Array.isArray(data.pages) ? data.pages : [],
+ briefConformance: Array.isArray(data.briefConformance) ? data.briefConformance : [],
+ warnings: Array.isArray(data.warnings) ? data.warnings : [],
+ report: data.report ?? null,
+ format: format && Number.isFinite(format.width) && Number.isFinite(format.height)
+ ? { width: format.width, height: format.height }
+ : null,
+ error: data.error,
+ }
+}
+
+/** POST-apply visual critique: the browser-rendered capture (real
+ * pixels of the LIVE canvas — not the server's SVG approximation)
+ * critiqued by the vision slot against the brief. Honest: a 503
+ * (no vision model) returns findings: [] + the availability note. */
+export async function critiqueAppliedCanvas(
+ image: string,
+ brief: { requiredTexts: { text: string }[]; requiredColors: { hex: string }[] },
+): Promise<{ findings: { severity: string; issue: string; fix?: string }[]; note: string }> {
+ const res = await fetch('/api/ai/agent/vision', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ image, brief }),
+ })
+ const data = (await res.json().catch(() => ({}))) as {
+ findings?: { severity: string; issue: string; fix?: string }[]
+ error?: string
+ }
+ if (res.status === 503) {
+ return {
+ findings: [],
+ note: 'Visual critique unavailable — no vision model configured. The deterministic checks stand.',
+ }
+ }
+ if (!res.ok) {
+ return { findings: [], note: data.error ?? 'Visual critique failed.' }
+ }
+ return { findings: data.findings ?? [], note: 'Critiqued the real rendered canvas (post-apply).' }
+}
diff --git a/src/lib/design-agent/fit-pages.test.ts b/src/lib/design-agent/fit-pages.test.ts
new file mode 100644
index 0000000..fd000ec
--- /dev/null
+++ b/src/lib/design-agent/fit-pages.test.ts
@@ -0,0 +1,77 @@
+// Unit tests for fit-pages (v0.31, DesignAgent panel) — the
+// deterministic canvas fit. Invariants that matter for a CHECKED
+// design: uniform factor (wrap points preserved), centering, no
+// element ever dropped, identity when no rescale is needed.
+
+import { describe, it, expect } from 'vitest'
+import { fitPagesToCanvas } from './fit-pages'
+import type { AnyElement, PageData, TextElement } from '@/lib/types'
+
+function textEl(over: Partial<TextElement> = {}): TextElement {
+ return {
+ id: 't1', type: 'text', x: 100, y: 100, width: 200, height: 50,
+ rotation: 0, opacity: 1, locked: false, visible: true, shadow: {} as never,
+ text: 'Hello', fontSize: 40, fontFamily: 'Poppins', bold: false, italic: false,
+ underline: false, strike: false, uppercase: false, fill: '#FFFFFF',
+ align: 'left', lineHeight: 1.2, letterSpacing: 2,
+ ...over,
+ } as TextElement
+}
+
+function page(elements: AnyElement[]): PageData {
+ return { id: 'p1', background: { type: 'solid', color: '#FFFFFF' }, elements } as PageData
+}
+
+describe('fitPagesToCanvas', () => {
+ it('is identity when the formats already match', () => {
+ const p = page([textEl()])
+ const r = fitPagesToCanvas([p], { width: 1080, height: 1350 }, { width: 1080, height: 1350 })
+ expect(r.scale).toBe(1)
+ expect(r.pages[0]!.elements[0]).toBe(p.elements[0]) // untouched object
+ })
+
+ it('uniformly rescales and centers a portrait compile onto a square canvas', () => {
+ const el = textEl()
+ const r = fitPagesToCanvas([page([el])], { width: 1080, height: 1350 }, { width: 1080, height: 1080 })
+ // scale = min(1080/1080, 1080/1350) = 0.8; x offset = (1080 - 864)/2 = 108
+ expect(r.scale).toBeCloseTo(0.8)
+ expect(r.offsetX).toBeCloseTo(108)
+ expect(r.offsetY).toBeCloseTo(0)
+ const t = r.pages[0]!.elements[0] as TextElement
+ expect(t.x).toBeCloseTo(100 * 0.8 + 108) // 188
+ expect(t.y).toBeCloseTo(100 * 0.8)
+ expect(t.width).toBeCloseTo(160)
+ expect(t.fontSize).toBeCloseTo(32)
+ expect(t.letterSpacing).toBeCloseTo(1.6)
+ expect(t.text).toBe('Hello') // content never changes
+ })
+
+ it('scales group children recursively (nested boxes ride the same factor)', () => {
+ const inner = textEl({ id: 'inner' })
+ const group = {
+ id: 'g1', type: 'group', x: 50, y: 50, width: 400, height: 400,
+ rotation: 0, opacity: 1, locked: false, visible: true, shadow: {} as never,
+ children: [inner],
+ } as unknown as AnyElement
+ const r = fitPagesToCanvas([page([group])], { width: 1000, height: 1000 }, { width: 500, height: 500 })
+ expect(r.scale).toBeCloseTo(0.5)
+ const g = r.pages[0]!.elements[0] as unknown as { width: number; children: TextElement[] }
+ expect(g.width).toBeCloseTo(200)
+ expect(g.children[0]!.fontSize).toBeCloseTo(20)
+ })
+
+ it('never drops or filters elements — count is preserved exactly', () => {
+ const els = Array.from({ length: 12 }, (_, i) => textEl({ id: `t${i}`, x: i * 80 }))
+ const r = fitPagesToCanvas([page(els), page([textEl({ id: 'x' })])], { width: 1080, height: 1920 }, { width: 1080, height: 1080 })
+ expect(r.pages[0]!.elements.length).toBe(12)
+ expect(r.pages[1]!.elements.length).toBe(1)
+ })
+
+ it('is identity for degenerate inputs (empty pages or unknown format)', () => {
+ expect(fitPagesToCanvas([], { width: 1080, height: 1080 }, { width: 500, height: 500 }).pages).toEqual([])
+ const p = page([textEl()])
+ const r = fitPagesToCanvas([p], { width: 0, height: NaN }, { width: 500, height: 500 })
+ expect(r.scale).toBe(1)
+ expect(r.pages[0]).toBe(p)
+ })
+})
diff --git a/src/lib/design-agent/fit-pages.ts b/src/lib/design-agent/fit-pages.ts
new file mode 100644
index 0000000..8669b66
--- /dev/null
+++ b/src/lib/design-agent/fit-pages.ts
@@ -0,0 +1,78 @@
+// ─────────────────────────────────────────────────────────────
+// fit-pages (v0.31, DesignAgent panel) — deterministic canvas fit.
+//
+// The autonomous pipeline compiles pages for the format the brief
+// demanded (e.g. 1080×1350 Instagram portrait). The editor's design
+// has ONE canvas size, so pages compiled for a different format are
+// uniformly rescaled + centered before appendPages — a pure
+// geometric transform: every coordinate, box and font size rides the
+// SAME factor, so wrap points, layering and relative layout are
+// exactly preserved (a design that fit at 1080×1350 still fits at
+// 4:5-of-the-target). No reflow, no re-interpretation — the checked
+// design stays the checked design.
+// ─────────────────────────────────────────────────────────────
+
+import type { AnyElement, PageData, TextElement } from '@/lib/types'
+
+export interface PageFormat {
+ width: number
+ height: number
+}
+
+export interface PageFitResult {
+ pages: PageData[]
+ /** applied uniform factor (1 = identity — no rescale happened) */
+ scale: number
+ /** letterbox centering offsets applied to every element */
+ offsetX: number
+ offsetY: number
+}
+
+function isGroup(el: AnyElement): el is AnyElement & { children: AnyElement[] } {
+ return el.type === 'group' && Array.isArray((el as { children?: unknown }).children)
+}
+
+function scaleElement(el: AnyElement, scale: number, ox: number, oy: number): AnyElement {
+ const base = {
+ ...el,
+ x: (el.x ?? 0) * scale + ox,
+ y: (el.y ?? 0) * scale + oy,
+ width: (el.width ?? 0) * scale,
+ height: (el.height ?? 0) * scale,
+ }
+ if (el.type === 'text') {
+ const t = el as TextElement
+ return {
+ ...base,
+ fontSize: Math.max(4, t.fontSize * scale),
+ // letterSpacing is px; lineHeight is a fontSize multiplier and
+ // needs no change. curve is an arc angle, not a length.
+ ...(t.letterSpacing ? { letterSpacing: t.letterSpacing * scale } : {}),
+ } as AnyElement
+ }
+ if (isGroup(el)) {
+ return { ...base, children: el.children.map((c) => scaleElement(c, scale, ox, oy)) } as AnyElement
+ }
+ return base as AnyElement
+}
+
+/**
+ * Fit compiled pages onto the target canvas. Identity (same pages
+ * back, scale 1) when the formats already match or no source format
+ * is known — never throws, never drops elements.
+ */
+export function fitPagesToCanvas(pages: PageData[], from: PageFormat, to: PageFormat): PageFitResult {
+ const sane = (f: PageFormat) => Number.isFinite(f.width) && Number.isFinite(f.height) && f.width >= 1 && f.height >= 1
+ if (!pages.length || !sane(from) || !sane(to)) return { pages, scale: 1, offsetX: 0, offsetY: 0 }
+
+ const scale = Math.min(to.width / from.width, to.height / from.height)
+ if (Math.abs(scale - 1) < 0.001) return { pages, scale: 1, offsetX: 0, offsetY: 0 }
+
+ const offsetX = Math.round(((to.width - from.width * scale) / 2) * 10) / 10
+ const offsetY = Math.round(((to.height - from.height * scale) / 2) * 10) / 10
+ const fitted = pages.map((p) => ({
+ ...p,
+ elements: p.elements.map((el) => scaleElement(el, scale, offsetX, offsetY)),
+ }))
+ return { pages: fitted, scale, offsetX, offsetY }
+}