|
| 1 | +/** |
| 2 | + * Archetype (c) — Headless agent script |
| 3 | + * |
| 4 | + * Shows: no browser, no persist adapter, no preview — pure editing engine. |
| 5 | + * Agents: batch restyling, localization, A/B variants, programmatic animation. |
| 6 | + * Explicit-id ops via query API — no selection, no mouse events. |
| 7 | + * |
| 8 | + * F1 payoff: headless is possible BECAUSE ops have explicit targets. |
| 9 | + * Selection-implicit ops (old R0) would break here — no UI → no selection. |
| 10 | + */ |
| 11 | + |
| 12 | +import { openComposition } from "../src/index.js"; |
| 13 | +import type { ElementSnapshot } from "../src/index.js"; |
| 14 | + |
| 15 | +// ── Localization agent ──────────────────────────────────────────────────────── |
| 16 | +// Rewrites all text elements to a new locale. No browser, no preview. |
| 17 | + |
| 18 | +export async function localize(html: string, translations: Map<string, string>): Promise<string> { |
| 19 | + const comp = await openComposition(html); |
| 20 | + |
| 21 | + const textElements = comp.find({ tag: "div" }); |
| 22 | + |
| 23 | + comp.batch(() => { |
| 24 | + for (const id of textElements) { |
| 25 | + const el = comp.getElement(id); |
| 26 | + if (!el?.text) continue; |
| 27 | + const translated = translations.get(el.text); |
| 28 | + if (translated) comp.setText(id, translated); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + return comp.serialize(); |
| 33 | +} |
| 34 | + |
| 35 | +// ── Brand restyle agent ─────────────────────────────────────────────────────── |
| 36 | +// Apply brand colors to all elements with a matching class name. |
| 37 | + |
| 38 | +export async function applyBrandColors( |
| 39 | + html: string, |
| 40 | + brandPrimary: string, |
| 41 | + brandSecondary: string, |
| 42 | +): Promise<string> { |
| 43 | + const comp = await openComposition(html); |
| 44 | + |
| 45 | + // Query: find elements by attribute pattern |
| 46 | + const brandColorEls = comp |
| 47 | + .getElements() |
| 48 | + .filter((el) => el.attributes["data-brand-role"] === "primary"); |
| 49 | + const brandSecondaryEls = comp |
| 50 | + .getElements() |
| 51 | + .filter((el) => el.attributes["data-brand-role"] === "secondary"); |
| 52 | + |
| 53 | + comp.batch(() => { |
| 54 | + for (const el of brandColorEls) { |
| 55 | + comp.setStyle(el.id, { color: brandPrimary }); |
| 56 | + } |
| 57 | + for (const el of brandSecondaryEls) { |
| 58 | + comp.setStyle(el.id, { color: brandSecondary }); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + return comp.serialize(); |
| 63 | +} |
| 64 | + |
| 65 | +// ── A/B variant agent ───────────────────────────────────────────────────────── |
| 66 | +// Produce two HTML variants from one template. |
| 67 | + |
| 68 | +export async function createABVariants( |
| 69 | + html: string, |
| 70 | + variantB: { headlineId: string; text: string; color: string }, |
| 71 | +): Promise<{ variantA: string; variantB: string }> { |
| 72 | + const compA = await openComposition(html); |
| 73 | + const variantAHtml = compA.serialize(); |
| 74 | + compA.dispose(); |
| 75 | + |
| 76 | + const compB = await openComposition(html); |
| 77 | + compB.setText(variantB.headlineId, variantB.text); |
| 78 | + compB.setStyle(variantB.headlineId, { color: variantB.color }); |
| 79 | + const variantBHtml = compB.serialize(); |
| 80 | + compB.dispose(); |
| 81 | + |
| 82 | + return { variantA: variantAHtml, variantB: variantBHtml }; |
| 83 | +} |
| 84 | + |
| 85 | +// ── Asset swap agent ────────────────────────────────────────────────────────── |
| 86 | +// F3: setAttribute handles img src, href, alt — the full attribute space. |
| 87 | + |
| 88 | +export async function swapAssets( |
| 89 | + html: string, |
| 90 | + swaps: Array<{ id: string; src: string; alt?: string }>, |
| 91 | +): Promise<string> { |
| 92 | + const comp = await openComposition(html); |
| 93 | + |
| 94 | + comp.batch(() => { |
| 95 | + for (const swap of swaps) { |
| 96 | + comp.setAttribute(swap.id, "src", swap.src); |
| 97 | + if (swap.alt !== undefined) { |
| 98 | + comp.setAttribute(swap.id, "alt", swap.alt); |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + return comp.serialize(); |
| 104 | +} |
| 105 | + |
| 106 | +// ── Batch GSAP animation agent ──────────────────────────────────────────────── |
| 107 | +// Add staggered entrance animations to all text elements. |
| 108 | + |
| 109 | +export async function addStaggeredEntrance(html: string, staggerDelay = 0.15): Promise<string> { |
| 110 | + const comp = await openComposition(html); |
| 111 | + |
| 112 | + const textEls = comp.find({ tag: "div" }); |
| 113 | + |
| 114 | + comp.batch(() => { |
| 115 | + textEls.forEach((id, i) => { |
| 116 | + comp.addGsapTween(id, { |
| 117 | + method: "from", |
| 118 | + position: i * staggerDelay, |
| 119 | + duration: 0.5, |
| 120 | + ease: "power3.out", |
| 121 | + fromProperties: { opacity: 0, y: 30 }, |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + return comp.serialize(); |
| 127 | +} |
| 128 | + |
| 129 | +// ── Composition metadata normalization ──────────────────────────────────────── |
| 130 | + |
| 131 | +export async function normalizeToPortrait(html: string): Promise<string> { |
| 132 | + const comp = await openComposition(html); |
| 133 | + comp.dispatch({ type: "setCompositionMetadata", width: 1080, height: 1920 }); |
| 134 | + return comp.serialize(); |
| 135 | +} |
| 136 | + |
| 137 | +// ── Variable override agent ─────────────────────────────────────────────────── |
| 138 | +// Apply a brand kit as composition variable overrides. |
| 139 | + |
| 140 | +export async function applyVariableKit( |
| 141 | + html: string, |
| 142 | + kit: Record<string, string | number | boolean>, |
| 143 | +): Promise<string> { |
| 144 | + const comp = await openComposition(html); |
| 145 | + |
| 146 | + comp.batch(() => { |
| 147 | + for (const [id, value] of Object.entries(kit)) { |
| 148 | + comp.setVariableValue(id, value); |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + return comp.serialize(); |
| 153 | +} |
| 154 | + |
| 155 | +// ── Inspection utility ──────────────────────────────────────────────────────── |
| 156 | +// Agents need to discover what's in a composition before editing. |
| 157 | + |
| 158 | +export async function inspectComposition(html: string): Promise<{ |
| 159 | + elementCount: number; |
| 160 | + textElements: ElementSnapshot[]; |
| 161 | + imageElements: ElementSnapshot[]; |
| 162 | + ids: string[]; |
| 163 | +}> { |
| 164 | + const comp = await openComposition(html); |
| 165 | + |
| 166 | + const all = comp.getElements(); |
| 167 | + const textElements = all.filter((el) => ["div", "p", "h1", "h2", "h3", "span"].includes(el.tag)); |
| 168 | + const imageElements = all.filter((el) => el.tag === "img"); |
| 169 | + |
| 170 | + comp.dispose(); |
| 171 | + |
| 172 | + return { |
| 173 | + elementCount: all.length, |
| 174 | + textElements, |
| 175 | + imageElements, |
| 176 | + ids: all.map((el) => el.id), |
| 177 | + }; |
| 178 | +} |
| 179 | + |
| 180 | +// ── Timing normalization agent ──────────────────────────────────────────────── |
| 181 | + |
| 182 | +export async function normalizeTiming(html: string, totalDuration: number): Promise<string> { |
| 183 | + const comp = await openComposition(html); |
| 184 | + |
| 185 | + const timedEls = comp.getElements().filter((el) => el.start !== null && el.duration !== null); |
| 186 | + |
| 187 | + const lastEnd = timedEls.reduce( |
| 188 | + (max, el) => Math.max(max, (el.start ?? 0) + (el.duration ?? 0)), |
| 189 | + 0, |
| 190 | + ); |
| 191 | + if (lastEnd === 0) return comp.serialize(); |
| 192 | + |
| 193 | + const scale = totalDuration / lastEnd; |
| 194 | + |
| 195 | + comp.batch(() => { |
| 196 | + for (const el of timedEls) { |
| 197 | + comp.setTiming(el.id, { |
| 198 | + start: Math.round((el.start ?? 0) * scale * 100) / 100, |
| 199 | + duration: Math.round((el.duration ?? 0) * scale * 100) / 100, |
| 200 | + }); |
| 201 | + } |
| 202 | + comp.dispatch({ type: "setCompositionMetadata", duration: totalDuration }); |
| 203 | + }); |
| 204 | + |
| 205 | + return comp.serialize(); |
| 206 | +} |
0 commit comments