@hanzo/studio 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/LICENSE +29 -0
  2. package/NOTICE +68 -0
  3. package/lib/chat/store.d.ts +33 -0
  4. package/lib/chat/store.js +56 -0
  5. package/lib/chat/turn.d.ts +51 -0
  6. package/lib/chat/turn.js +274 -0
  7. package/lib/desk.d.ts +14 -0
  8. package/lib/desk.js +72 -0
  9. package/lib/gpu.d.ts +15 -0
  10. package/lib/gpu.js +41 -0
  11. package/lib/home.d.ts +6 -0
  12. package/lib/home.js +48 -0
  13. package/lib/host.d.ts +12 -0
  14. package/lib/host.js +18 -0
  15. package/lib/index.d.ts +12 -0
  16. package/lib/index.js +11 -0
  17. package/lib/kinds/ad.d.ts +51 -0
  18. package/lib/kinds/ad.js +124 -0
  19. package/lib/kinds/document.d.ts +6 -0
  20. package/lib/kinds/document.js +271 -0
  21. package/lib/kinds/image.d.ts +47 -0
  22. package/lib/kinds/image.js +179 -0
  23. package/lib/kinds/index.d.ts +9 -0
  24. package/lib/kinds/index.js +12 -0
  25. package/lib/kinds/kind.d.ts +113 -0
  26. package/lib/kinds/kind.js +13 -0
  27. package/lib/kinds/mesh/index.d.ts +2 -0
  28. package/lib/kinds/mesh/index.js +76 -0
  29. package/lib/kinds/mesh/viewer.d.ts +7 -0
  30. package/lib/kinds/mesh/viewer.js +79 -0
  31. package/lib/kinds/poster/compose.d.ts +40 -0
  32. package/lib/kinds/poster/compose.js +289 -0
  33. package/lib/kinds/poster/index.d.ts +21 -0
  34. package/lib/kinds/poster/index.js +210 -0
  35. package/lib/kinds/sound.d.ts +6 -0
  36. package/lib/kinds/sound.js +100 -0
  37. package/lib/kinds/type/fonts.d.ts +30 -0
  38. package/lib/kinds/type/fonts.js +111 -0
  39. package/lib/kinds/type/index.d.ts +5 -0
  40. package/lib/kinds/type/index.js +117 -0
  41. package/lib/kinds/type/system.d.ts +164 -0
  42. package/lib/kinds/type/system.js +302 -0
  43. package/lib/kinds/video.d.ts +2 -0
  44. package/lib/kinds/video.js +81 -0
  45. package/lib/live.d.ts +44 -0
  46. package/lib/live.js +128 -0
  47. package/lib/made.d.ts +75 -0
  48. package/lib/made.js +36 -0
  49. package/lib/nav.d.ts +17 -0
  50. package/lib/nav.js +29 -0
  51. package/lib/pdf.d.ts +16 -0
  52. package/lib/pdf.js +53 -0
  53. package/lib/places.d.ts +6 -0
  54. package/lib/places.js +121 -0
  55. package/lib/raster.d.ts +7 -0
  56. package/lib/raster.js +53 -0
  57. package/lib/route.d.ts +33 -0
  58. package/lib/route.js +52 -0
  59. package/lib/starters.d.ts +31 -0
  60. package/lib/starters.js +62 -0
  61. package/lib/studio.d.ts +26 -0
  62. package/lib/studio.js +58 -0
  63. package/lib/talk.d.ts +3 -0
  64. package/lib/talk.js +369 -0
  65. package/lib/ui.d.ts +50 -0
  66. package/lib/ui.js +57 -0
  67. package/lib/use.d.ts +11 -0
  68. package/lib/use.js +35 -0
  69. package/lib/wire.d.ts +239 -0
  70. package/lib/wire.js +478 -0
  71. package/lib/zip.d.ts +9 -0
  72. package/lib/zip.js +60 -0
  73. package/package.json +75 -3
  74. package/README.md +0 -3
@@ -0,0 +1,289 @@
1
+ /**
2
+ * A poster, laid out: a size, a layout, the words and a picture, drawn in a
3
+ * type system, as one SVG that carries its faces and its picture inside it.
4
+ * The same SVG is the preview, the PNG and the PDF, so what is shown is what
5
+ * is shipped.
6
+ *
7
+ * Words are set, not dropped in: the headline takes the largest size at which
8
+ * it fits its box in at most its lines, measured in the face it is drawn in;
9
+ * every other block wraps at its measure and stops at its line limit.
10
+ */
11
+ import { face, stack, use } from '../type/fonts.js';
12
+ const mm = (v) => (v * 72) / 25.4;
13
+ export const FORMATS = [
14
+ { id: 'a4', label: 'A4', width: 1240, height: 1754, points: [mm(210), mm(297)] },
15
+ { id: 'a3', label: 'A3', width: 1754, height: 2480, points: [mm(297), mm(420)] },
16
+ { id: 'a2', label: 'A2', width: 2480, height: 3508, points: [mm(420), mm(594)] },
17
+ { id: 'letter', label: 'US Letter', width: 1275, height: 1650, points: [612, 792] },
18
+ { id: '18x24', label: '18 × 24 in', width: 1800, height: 2400, points: [1296, 1728] },
19
+ { id: '24x36', label: '24 × 36 in', width: 1800, height: 2700, points: [1728, 2592] },
20
+ { id: 'square', label: 'Social 1:1', width: 1080, height: 1080, points: [810, 810] },
21
+ { id: 'portrait', label: 'Social 4:5', width: 1080, height: 1350, points: [810, 1012.5] },
22
+ { id: 'story', label: 'Story 9:16', width: 1080, height: 1920, points: [810, 1440] },
23
+ { id: 'landscape', label: 'Landscape 16:9', width: 1920, height: 1080, points: [1440, 810] },
24
+ ];
25
+ export const format = (id) => FORMATS.find((f) => f.id === id) ?? FORMATS[0];
26
+ /** The aspect a picture for this format is drawn at (the image lane's names). */
27
+ export function aspect(f, layout) {
28
+ const r = layout === 'split' ? f.width / (f.height * 0.58) : f.width / f.height;
29
+ const named = [
30
+ ['1:1', 1],
31
+ ['4:5', 0.8],
32
+ ['3:4', 0.75],
33
+ ['2:3', 0.667],
34
+ ['9:16', 0.5625],
35
+ ['16:9', 1.778],
36
+ ['3:2', 1.5],
37
+ ['4:3', 1.333],
38
+ ['21:9', 2.333],
39
+ ];
40
+ return named.reduce((best, n) => (Math.abs(n[1] - r) < Math.abs(best[1] - r) ? n : best))[0];
41
+ }
42
+ export const LAYOUTS = ['hero', 'center', 'split', 'frame', 'type'];
43
+ export const LAYOUT_NAMES = {
44
+ hero: 'Full bleed, words low',
45
+ center: 'Full bleed, words centred',
46
+ split: 'Picture over a colour block',
47
+ frame: 'Framed picture on paper',
48
+ type: 'Type only',
49
+ };
50
+ const xml = (s) => s.replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&apos;' })[c]);
51
+ let measurer = null;
52
+ function width(text, family, weight, px, tracking) {
53
+ measurer ??= document.createElement('canvas').getContext('2d');
54
+ if (!measurer)
55
+ return text.length * px * 0.55;
56
+ measurer.font = `${weight} ${px}px ${stack(family)}`;
57
+ return measurer.measureText(text).width + Math.max(0, text.length - 1) * tracking * px;
58
+ }
59
+ /** Words into lines no wider than `max`. A word wider than the line stands alone on one. */
60
+ function wrap(text, max, family, weight, px, tracking) {
61
+ const out = [];
62
+ for (const para of text.split('\n')) {
63
+ let line = '';
64
+ for (const word of para.split(/\s+/).filter(Boolean)) {
65
+ const next = line ? `${line} ${word}` : word;
66
+ if (line && width(next, family, weight, px, tracking) > max) {
67
+ out.push(line);
68
+ line = word;
69
+ }
70
+ else
71
+ line = next;
72
+ }
73
+ if (line)
74
+ out.push(line);
75
+ }
76
+ return out;
77
+ }
78
+ /**
79
+ * The largest size, between `lo` and `hi`, at which `text` sets in at most
80
+ * `lines` lines of `max`, standing no taller than `tall` at `leading`.
81
+ */
82
+ function fit(text, max, lines, family, weight, tracking, lo, hi, tall = Infinity, leading = 1) {
83
+ let a = lo;
84
+ let b = hi;
85
+ for (let i = 0; i < 14; i++) {
86
+ const m = (a + b) / 2;
87
+ const set = wrap(text, max, family, weight, m, tracking);
88
+ const ok = set.length <= lines && set.every((l) => width(l, family, weight, m, tracking) <= max) && m * (1 + (set.length - 1) * leading) <= tall;
89
+ if (ok)
90
+ a = m;
91
+ else
92
+ b = m;
93
+ }
94
+ return Math.floor(a);
95
+ }
96
+ function block(b, x, y, anchor = 'start') {
97
+ if (!b.lines.length)
98
+ return '';
99
+ const spans = b.lines.map((l, i) => `<tspan x="${x}" dy="${i === 0 ? 0 : b.px * b.leading}">${xml(b.upper ? l.toUpperCase() : l)}</tspan>`).join('');
100
+ return `<text x="${x}" y="${y + b.px * 0.8}" text-anchor="${anchor}" font-family="${xml(stack(b.family))}" font-weight="${b.weight}" font-size="${b.px}" letter-spacing="${(b.tracking * b.px).toFixed(2)}" fill="${b.fill}">${spans}</text>`;
101
+ }
102
+ const height = (b) => (b.lines.length ? b.px * (1 + (b.lines.length - 1) * b.leading) : 0);
103
+ /** Load the faces a poster draws in, so measuring and drawing use them. */
104
+ export async function ready(t) {
105
+ use(t.display);
106
+ use(t.text);
107
+ await Promise.all([
108
+ document.fonts.load(`${t.weights.display} 64px ${stack(t.display)}`),
109
+ document.fonts.load(`${t.weights.body} 32px ${stack(t.text)}`),
110
+ document.fonts.load(`${t.weights.strong} 32px ${stack(t.text)}`),
111
+ ]).catch(() => undefined);
112
+ }
113
+ /** The poster as SVG, faces and picture inside it. */
114
+ export async function compose(p) {
115
+ const f = p.size ?? format(p.format);
116
+ const t = p.system;
117
+ const c = t.colors;
118
+ await ready(t);
119
+ const W = f.width;
120
+ const H = f.height;
121
+ if (W / H > 3 || W / H < 0.4)
122
+ return banner(p, W, H);
123
+ const u = Math.min(W, H) / 100;
124
+ const margin = Math.round(u * 6.5);
125
+ const measure = W - margin * 2;
126
+ const layout = p.picture ? p.layout : 'type';
127
+ const dark = layout === 'hero' || layout === 'center';
128
+ const ink = dark ? '#ffffff' : c.ink;
129
+ const soft = dark ? 'rgba(255,255,255,0.82)' : c.muted;
130
+ const headWidth = layout === 'center' || layout === 'type' ? measure : measure * (W > H ? 0.6 : 0.92);
131
+ const headLines = layout === 'type' ? 5 : 3;
132
+ const headMax = layout === 'type' ? u * 30 : u * 17;
133
+ // The headline's room: what the layout leaves once the margins, the details and the call to action are placed.
134
+ const headTall = { hero: H * 0.42, center: H * 0.5, split: H * 0.42 - margin * 2 - u * 8, frame: H * 0.3, type: H - margin * 2 - u * 6 - u * 12 }[layout];
135
+ const headPx = fit(p.headline || ' ', headWidth, headLines, t.display, t.weights.display, t.tracking.display, u * 4, headMax, headTall, t.leading.display);
136
+ const head = {
137
+ lines: wrap(p.headline, headWidth, t.display, t.weights.display, headPx, t.tracking.display),
138
+ px: headPx,
139
+ family: t.display,
140
+ weight: t.weights.display,
141
+ leading: t.leading.display,
142
+ tracking: t.tracking.display,
143
+ fill: ink,
144
+ };
145
+ const subPx = Math.round(Math.max(u * 2.6, Math.min(u * 4.4, headPx * 0.34)));
146
+ const sub = {
147
+ lines: wrap(p.subhead, headWidth, t.text, t.weights.body, subPx, 0).slice(0, 3),
148
+ px: subPx,
149
+ family: t.text,
150
+ weight: t.weights.body,
151
+ leading: t.leading.body,
152
+ tracking: 0,
153
+ fill: soft,
154
+ };
155
+ const detPx = Math.round(u * 2.4);
156
+ const det = {
157
+ lines: wrap(p.details, measure * 0.7, t.text, t.weights.strong, detPx, t.tracking.caps).slice(0, 4),
158
+ px: detPx,
159
+ family: t.text,
160
+ weight: t.weights.strong,
161
+ leading: 1.35,
162
+ tracking: t.tracking.caps,
163
+ fill: ink,
164
+ upper: true,
165
+ };
166
+ const ctaPx = Math.round(u * 2.6);
167
+ const cta = p.cta.trim();
168
+ const ctaW = cta ? width(cta.toUpperCase(), t.text, t.weights.strong, ctaPx, t.tracking.caps) + ctaPx * 2.4 : 0;
169
+ const ctaH = ctaPx * 2.4;
170
+ const ctaFill = layout === 'type' || layout === 'frame' || layout === 'split' ? c.accent : '#ffffff';
171
+ const ctaInk = readable(ctaFill);
172
+ const pill = (x, y, anchor = 'start') => {
173
+ if (!cta)
174
+ return '';
175
+ const left = anchor === 'middle' ? x - ctaW / 2 : x;
176
+ return `<rect x="${left}" y="${y}" width="${ctaW}" height="${ctaH}" rx="${ctaH / 2}" fill="${ctaFill}"/>${block({ lines: [cta], px: ctaPx, family: t.text, weight: t.weights.strong, leading: 1, tracking: t.tracking.caps, fill: ctaInk, upper: true }, left + ctaW / 2, y + ctaH / 2 - ctaPx * 0.62, 'middle')}`;
177
+ };
178
+ const gap = u * 2.2;
179
+ const words = (x, y, anchor = 'start') => {
180
+ let at = y;
181
+ let out = block(head, x, at, anchor);
182
+ at += height(head) + (sub.lines.length ? gap : 0);
183
+ out += block(sub, x, at, anchor);
184
+ at += height(sub);
185
+ return { out, at };
186
+ };
187
+ const wordsHeight = height(head) + (sub.lines.length ? gap + height(sub) : 0);
188
+ const footHeight = Math.max(height(det), cta ? ctaH : 0);
189
+ let body = '';
190
+ const pic = (x, y, w, h, round = 0) => `<clipPath id="pic"><rect x="${x}" y="${y}" width="${w}" height="${h}" rx="${round}"/></clipPath><image href="${p.picture}" x="${x}" y="${y}" width="${w}" height="${h}" preserveAspectRatio="xMidYMid slice" clip-path="url(#pic)"/>`;
191
+ if (layout === 'hero') {
192
+ body += pic(0, 0, W, H);
193
+ body += `<linearGradient id="scrim" x1="0" y1="0" x2="0" y2="1"><stop offset="0.35" stop-color="#000" stop-opacity="0"/><stop offset="1" stop-color="#000" stop-opacity="0.78"/></linearGradient><rect width="${W}" height="${H}" fill="url(#scrim)"/>`;
194
+ const top = H - margin - footHeight - gap * 2 - wordsHeight;
195
+ body += words(margin, top).out;
196
+ body += block(det, margin, H - margin - height(det));
197
+ body += pill(W - margin - ctaW, H - margin - ctaH);
198
+ }
199
+ else if (layout === 'center') {
200
+ body += pic(0, 0, W, H);
201
+ body += `<rect width="${W}" height="${H}" fill="#000" fill-opacity="0.42"/>`;
202
+ const top = (H - wordsHeight) / 2;
203
+ body += words(W / 2, top, 'middle').out;
204
+ body += block({ ...det, lines: det.lines }, W / 2, H - margin - height(det) - (cta ? ctaH + gap : 0), 'middle');
205
+ body += pill(W / 2, H - margin - ctaH, 'middle');
206
+ }
207
+ else if (layout === 'split') {
208
+ const ph = Math.round(H * 0.58);
209
+ body += `<rect width="${W}" height="${H}" fill="${c.paper}"/>`;
210
+ body += pic(0, 0, W, ph);
211
+ body += `<rect x="0" y="${ph}" width="${W}" height="${u * 0.8}" fill="${c.accent}"/>`;
212
+ body += words(margin, ph + margin).out;
213
+ body += block(det, margin, H - margin - height(det));
214
+ body += pill(W - margin - ctaW, H - margin - ctaH);
215
+ }
216
+ else if (layout === 'frame') {
217
+ body += `<rect width="${W}" height="${H}" fill="${c.paper}"/>`;
218
+ const w = words(margin, margin);
219
+ body += w.out;
220
+ const top = w.at + gap * 1.5;
221
+ const bottom = H - margin - footHeight - gap * 1.5;
222
+ body += pic(margin, top, measure, Math.max(u * 10, bottom - top), u * 1.6);
223
+ body += block(det, margin, H - margin - height(det));
224
+ body += pill(W - margin - ctaW, H - margin - ctaH);
225
+ }
226
+ else {
227
+ body += `<rect width="${W}" height="${H}" fill="${c.paper}"/>`;
228
+ body += `<rect x="${margin}" y="${margin}" width="${u * 14}" height="${u * 1.4}" fill="${c.accent}"/>`;
229
+ body += words(margin, margin + u * 6).out;
230
+ body += block(det, margin, H - margin - height(det));
231
+ body += pill(W - margin - ctaW, H - margin - ctaH);
232
+ }
233
+ const faces = (await Promise.all([...new Set([t.display, t.text])].map((fam) => face(fam).catch(() => '')))).join('\n');
234
+ return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}"><defs><style><![CDATA[${faces}]]></style></defs>${body}</svg>`;
235
+ }
236
+ /**
237
+ * An ad unit too long or too tall for a poster's layouts (728x90, 320x50,
238
+ * 160x600): the picture at one end, the headline in the middle, the call to
239
+ * action at the other.
240
+ */
241
+ async function banner(p, W, H) {
242
+ const t = p.system;
243
+ const c = t.colors;
244
+ const wide = W > H;
245
+ const pad = Math.round(Math.min(W, H) * (wide ? 0.14 : 0.08));
246
+ const cta = p.cta.trim();
247
+ const ctaPx = Math.round(wide ? H * 0.2 : W * 0.085);
248
+ const ctaW = cta ? width(cta.toUpperCase(), t.text, t.weights.strong, ctaPx, t.tracking.caps) + ctaPx * 2 : 0;
249
+ const ctaH = ctaPx * 2.2;
250
+ const ink = readable(c.paper) === '#ffffff' ? '#ffffff' : c.ink;
251
+ let body = `<rect width="${W}" height="${H}" fill="${c.paper}"/>`;
252
+ let x = pad;
253
+ let y = pad;
254
+ let room = wide ? W - pad * 2 : H - pad * 2;
255
+ if (p.picture) {
256
+ const pw = wide ? Math.round(H * 1.25) : W;
257
+ const ph = wide ? H : Math.round(W * 0.9);
258
+ body += `<clipPath id="pic"><rect width="${pw}" height="${ph}"/></clipPath><image href="${p.picture}" width="${pw}" height="${ph}" preserveAspectRatio="xMidYMid slice" clip-path="url(#pic)"/>`;
259
+ if (wide) {
260
+ x = pw + pad;
261
+ room -= pw;
262
+ }
263
+ else {
264
+ y = ph + pad;
265
+ room -= ph;
266
+ }
267
+ }
268
+ const across = wide ? room - (cta ? ctaW + pad : 0) : W - pad * 2;
269
+ const lines = wide ? (H < 70 ? 1 : 2) : 4;
270
+ const px = fit(p.headline || ' ', across, lines, t.display, t.weights.display, t.tracking.display, 8, wide ? H * 0.42 : W * 0.16);
271
+ const set = wrap(p.headline, across, t.display, t.weights.display, px, t.tracking.display);
272
+ const tall = px * (1 + (set.length - 1) * t.leading.display);
273
+ const top = wide ? (H - tall) / 2 : y;
274
+ body += block({ lines: set, px, family: t.display, weight: t.weights.display, leading: t.leading.display, tracking: t.tracking.display, fill: ink }, x, top);
275
+ if (cta) {
276
+ const cx = wide ? W - pad - ctaW : pad;
277
+ const cy = wide ? (H - ctaH) / 2 : H - pad - ctaH;
278
+ body += `<rect x="${cx}" y="${cy}" width="${ctaW}" height="${ctaH}" rx="${ctaH / 2}" fill="${c.accent}"/>`;
279
+ body += block({ lines: [cta], px: ctaPx, family: t.text, weight: t.weights.strong, leading: 1, tracking: t.tracking.caps, fill: readable(c.accent), upper: true }, cx + ctaW / 2, cy + ctaH / 2 - ctaPx * 0.62, 'middle');
280
+ }
281
+ const faces = (await Promise.all([...new Set([t.display, t.text])].map((fam) => face(fam).catch(() => '')))).join('\n');
282
+ return `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}"><defs><style><![CDATA[${faces}]]></style></defs>${body}</svg>`;
283
+ }
284
+ /** Black or white, whichever reads on `bg`. */
285
+ function readable(bg) {
286
+ const v = parseInt(bg.slice(1), 16);
287
+ const l = 0.299 * ((v >> 16) & 255) + 0.587 * ((v >> 8) & 255) + 0.114 * (v & 255);
288
+ return l > 150 ? '#0a0a0a' : '#ffffff';
289
+ }
@@ -0,0 +1,21 @@
1
+ import { type Wire } from '../../wire.ts';
2
+ import { type Kind } from '../kind.ts';
3
+ import { type TypeSystem } from '../type/system.ts';
4
+ import { type Layout } from './compose.ts';
5
+ export interface Spec {
6
+ format: string;
7
+ layout: Layout;
8
+ headline: string;
9
+ subhead: string;
10
+ details: string;
11
+ cta: string;
12
+ /** The picture: a stored file's key, or '' for none. */
13
+ picture: string;
14
+ /** What the picture was drawn from, to draw it again. */
15
+ prompt: string;
16
+ system: TypeSystem;
17
+ [k: string]: unknown;
18
+ }
19
+ /** The SVG for a spec: its picture read back from the store. */
20
+ export declare function svg(wire: Wire, s: Spec): Promise<string>;
21
+ export declare const poster: Kind;
@@ -0,0 +1,210 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * Posters: a fixed size (A-series, Letter, 18x24, 24x36, social and story),
4
+ * a picture from the image lane, words from the chat, laid out in the chosen
5
+ * type system and drawn in the browser. A version keeps its words, layout and
6
+ * picture as its spec and the PNG as its file; the PDF is drawn from the same
7
+ * SVG on export, at the page's physical size.
8
+ *
9
+ * Without a picture (none asked for, or the image lane could not draw one) a
10
+ * poster is set in type alone and the turn says why.
11
+ */
12
+ import { SizableText, XStack, YStack } from '@hanzo/gui';
13
+ import { Frame } from '@hanzogui/lucide-icons-2';
14
+ import { useEffect, useState } from 'react';
15
+ import { current, filename } from '../../made.js';
16
+ import { data, page, pixels } from '../../raster.js';
17
+ import { Choice, Row, Text1, TextN } from '../../ui.js';
18
+ import { read, structured, url } from '../../wire.js';
19
+ import { picture } from '../image.js';
20
+ import { num, str } from '../kind.js';
21
+ import { PRESETS, settle } from '../type/system.js';
22
+ import { aspect, compose, format, FORMATS, LAYOUT_NAMES, LAYOUTS } from './compose.js';
23
+ const spec = (v) => {
24
+ const s = v;
25
+ return {
26
+ format: FORMATS.some((f) => f.id === s.format) ? s.format : 'a3',
27
+ layout: LAYOUTS.includes(s.layout ?? '') ? s.layout : 'hero',
28
+ headline: str(s.headline, 'Untitled'),
29
+ subhead: str(s.subhead),
30
+ details: typeof s.details === 'string' ? s.details : '',
31
+ cta: str(s.cta),
32
+ picture: str(s.picture),
33
+ prompt: str(s.prompt),
34
+ system: settle((s.system ?? {}), PRESETS[0]),
35
+ };
36
+ };
37
+ /** The SVG for a spec: its picture read back from the store. */
38
+ export async function svg(wire, s) {
39
+ const pic = s.picture ? await data(await read(wire, s.picture)) : '';
40
+ const p = { ...s, picture: pic };
41
+ return compose(p);
42
+ }
43
+ /** Draw a spec and keep its PNG. */
44
+ async function draw(w, s) {
45
+ w.doing('Laying out the poster');
46
+ const f = format(s.format);
47
+ const png = await pixels(await svg(w.wire, s), f.width, f.height);
48
+ return [await w.keep('poster.png', png, { width: f.width, height: f.height })];
49
+ }
50
+ /** A picture for a poster: from a tab, else drawn from words; '' and the reason when none can be had. */
51
+ async function art(w, s, from) {
52
+ const tab = from ? w.find(from) : null;
53
+ if (tab) {
54
+ const f = current(tab).files.find((x) => x.mime.startsWith('image/'));
55
+ if (f)
56
+ return { key: f.key, why: '' };
57
+ }
58
+ if (!s.prompt)
59
+ return { key: '', why: '' };
60
+ try {
61
+ const got = await picture(w, { prompt: s.prompt, aspect: aspect(format(s.format), s.layout), model: '' });
62
+ return { key: got.files[0]?.key ?? '', why: '' };
63
+ }
64
+ catch (e) {
65
+ return { key: '', why: e instanceof Error ? e.message : String(e) };
66
+ }
67
+ }
68
+ const CHANGE = {
69
+ type: 'object',
70
+ additionalProperties: false,
71
+ required: ['headline', 'subhead', 'details', 'cta', 'layout', 'format', 'picture'],
72
+ properties: {
73
+ headline: { type: 'string' },
74
+ subhead: { type: 'string' },
75
+ details: { type: 'string' },
76
+ cta: { type: 'string' },
77
+ layout: { type: 'string', enum: LAYOUTS },
78
+ format: { type: 'string', enum: FORMATS.map((f) => f.id) },
79
+ picture: { type: 'string', description: 'An instruction to edit the picture, a new description to draw it again prefixed with "new:", or "" to keep it.' },
80
+ },
81
+ };
82
+ export const poster = {
83
+ id: 'poster',
84
+ label: 'Poster',
85
+ blurb: 'Print and social posters: a picture, a headline, the details, laid out in your type.',
86
+ icon: Frame,
87
+ tool: {
88
+ name: 'make_poster',
89
+ description: 'Make a poster or flyer: a picture with a headline, a subhead, details (date, place, price) and a call to action, laid out at a print or social size in the chosen type system.',
90
+ parameters: {
91
+ type: 'object',
92
+ properties: {
93
+ title: { type: 'string', description: 'A short name for the tab.' },
94
+ headline: { type: 'string', description: 'The headline, short and strong: two to seven words.' },
95
+ subhead: { type: 'string', description: 'One supporting line.' },
96
+ details: { type: 'string', description: 'Lines separated by newlines: date and time, place, price.' },
97
+ cta: { type: 'string', description: 'A call to action of one to three words, or empty.' },
98
+ format: { type: 'string', enum: FORMATS.map((f) => f.id), description: 'a3 for print unless asked; story for stories; square or portrait for social feeds.' },
99
+ layout: { type: 'string', enum: LAYOUTS, description: Object.entries(LAYOUT_NAMES).map(([k, v]) => `${k}: ${v}`).join('; ') },
100
+ image_prompt: { type: 'string', description: 'A full visual description of the picture to draw, with no words in it. Empty for a type-only poster.' },
101
+ image_tab: { type: 'integer', description: 'A tab whose picture to use instead of drawing one.' },
102
+ },
103
+ required: ['title', 'headline'],
104
+ },
105
+ },
106
+ needs: () => null,
107
+ make: async (w, args) => {
108
+ const s = spec({
109
+ format: str(args.format, 'a3'),
110
+ layout: str(args.layout, 'hero'),
111
+ headline: str(args.headline),
112
+ subhead: str(args.subhead),
113
+ details: str(args.details),
114
+ cta: str(args.cta),
115
+ prompt: str(args.image_prompt),
116
+ system: w.brand ?? PRESETS[0],
117
+ });
118
+ const got = await art(w, s, num(args.image_tab, 0));
119
+ s.picture = got.key;
120
+ const files = await draw(w, s);
121
+ const note = got.why ? `Set in type alone: the picture could not be drawn (${got.why})` : s.headline;
122
+ return { title: str(args.title, s.headline), draft: { spec: s, files, note, by: 'Studio layout' } };
123
+ },
124
+ revise: async (w, made, change) => {
125
+ const now = spec(current(made).spec);
126
+ let next = { ...now, ...(change.patch ?? {}) };
127
+ let why = '';
128
+ if (change.instruction) {
129
+ w.doing('Rewriting the poster');
130
+ const ask = await structured(w.wire, w.writer, 'You revise posters. Given the poster as JSON and an instruction, answer the whole poster with the instruction applied. Keep everything the instruction does not ask to change exactly as it is. picture: "" keeps the picture; a plain instruction ("warmer", "remove the people") edits it; "new: <description>" draws a new one.', `Poster: ${JSON.stringify({ headline: now.headline, subhead: now.subhead, details: now.details, cta: now.cta, layout: now.layout, format: now.format, picture: '' })}\nInstruction: ${change.instruction}`, CHANGE, w.signal);
131
+ next = spec({ ...now, ...ask, picture: now.picture, prompt: now.prompt, system: w.brand ?? now.system });
132
+ const p = str(ask.picture);
133
+ if (p.startsWith('new:')) {
134
+ next.prompt = p.slice(4).trim();
135
+ const got = await art(w, { ...next, picture: '' }, 0);
136
+ next.picture = got.key || now.picture;
137
+ why = got.why;
138
+ }
139
+ else if (p && now.picture) {
140
+ try {
141
+ const f = current(made).files[0];
142
+ const src = { key: now.picture, mime: f?.mime ?? 'image/png', name: 'picture' };
143
+ const got = await picture(w, { prompt: p, aspect: aspect(format(next.format), next.layout), model: '', from: [src] });
144
+ next.picture = got.files[0]?.key ?? now.picture;
145
+ }
146
+ catch (e) {
147
+ why = e instanceof Error ? e.message : String(e);
148
+ }
149
+ }
150
+ }
151
+ const files = await draw(w, next);
152
+ return { spec: next, files, note: why ? `${change.instruction} (the picture is unchanged: ${why})` : (change.instruction ?? 'Edited'), by: 'Studio layout' };
153
+ },
154
+ View: Sheet,
155
+ exports: [
156
+ {
157
+ label: 'PNG',
158
+ run: async (w, m, v) => ({ name: filename(m.title, 'png'), blob: await read(w, v.files[0].key) }),
159
+ },
160
+ {
161
+ label: 'PDF (print size)',
162
+ run: async (w, m, v) => {
163
+ const s = spec(v.spec);
164
+ const f = format(s.format);
165
+ return { name: filename(m.title, 'pdf'), blob: await page(await svg(w, s), f.width, f.height, f.points, m.title) };
166
+ },
167
+ },
168
+ {
169
+ label: 'SVG',
170
+ run: async (w, m, v) => ({ name: filename(m.title, 'svg'), blob: new Blob([await svg(w, spec(v.spec))], { type: 'image/svg+xml' }) }),
171
+ },
172
+ ],
173
+ };
174
+ function Sheet({ made, version, wire, edit, busy, others }) {
175
+ const s = spec(version.spec);
176
+ const [draft, setDraft] = useState(s);
177
+ useEffect(() => setDraft(spec(version.spec)), [version.id]);
178
+ const [preview, setPreview] = useState('');
179
+ const changed = JSON.stringify({ ...draft, system: null }) !== JSON.stringify({ ...s, system: null });
180
+ // The shown version's PNG, or a live draw of the words being edited.
181
+ useEffect(() => {
182
+ let live = true;
183
+ let made = '';
184
+ const run = async () => {
185
+ const text = changed ? await svg(wire, draft) : '';
186
+ if (!live)
187
+ return;
188
+ if (text) {
189
+ made = URL.createObjectURL(new Blob([text], { type: 'image/svg+xml' }));
190
+ setPreview(made);
191
+ }
192
+ else {
193
+ const f = version.files[0];
194
+ if (f)
195
+ setPreview(await url(wire, f.key));
196
+ }
197
+ };
198
+ const t = setTimeout(() => void run().catch(() => undefined), changed ? 250 : 0);
199
+ return () => {
200
+ live = false;
201
+ clearTimeout(t);
202
+ if (made)
203
+ URL.revokeObjectURL(made);
204
+ };
205
+ }, [wire, version.id, changed, JSON.stringify(draft)]);
206
+ const pictures = others.flatMap((m) => m.kind === 'image' ? current(m).files.filter((f) => f.mime.startsWith('image/')).map((f, i) => ({ value: f.key, label: `${m.title}${i ? ` ${i + 1}` : ''}` })) : []);
207
+ const set = (patch) => setDraft({ ...draft, ...patch });
208
+ const f = format(draft.format);
209
+ return (_jsxs(YStack, { flex: 1, minH: 0, $md: { flexDirection: 'row' }, children: [_jsx(YStack, { flex: 1, minH: 240, items: "center", justify: "center", p: "$3", bg: "$hover", children: preview ? (_jsx("img", { src: preview, alt: made.title, style: { maxWidth: '100%', maxHeight: '100%', aspectRatio: `${f.width} / ${f.height}`, objectFit: 'contain', boxShadow: '0 8px 30px rgba(0,0,0,.35)' } })) : (_jsx(SizableText, { color: "$soft", children: "Drawing\u2026" })) }), _jsxs(YStack, { width: "100%", $md: { width: 300 }, gap: "$3", p: "$3", overflow: "scroll", borderLeftWidth: 1, borderColor: "$borderColor", children: [_jsx(Row, { label: "Headline", children: _jsx(TextN, { label: "Headline", rows: 2, value: draft.headline, onChange: (headline) => set({ headline }) }) }), _jsx(Row, { label: "Subhead", children: _jsx(Text1, { label: "Subhead", value: draft.subhead, onChange: (subhead) => set({ subhead }) }) }), _jsx(Row, { label: "Details", children: _jsx(TextN, { label: "Details", value: draft.details, onChange: (details) => set({ details }) }) }), _jsx(Row, { label: "Call to action", children: _jsx(Text1, { label: "Call to action", value: draft.cta, onChange: (cta) => set({ cta }) }) }), _jsx(Row, { label: "Layout", children: _jsx(Choice, { label: "Layout", value: draft.layout, options: LAYOUTS.map((l) => ({ value: l, label: LAYOUT_NAMES[l] })), onChange: (layout) => set({ layout: layout }) }) }), _jsx(Row, { label: "Size", children: _jsx(Choice, { label: "Size", value: draft.format, options: FORMATS.map((x) => ({ value: x.id, label: x.label })), onChange: (format) => set({ format }) }) }), _jsx(Row, { label: "Picture", children: _jsx(Choice, { label: "Picture", value: draft.picture, options: [{ value: '', label: 'None (type only)' }, ...pictures, ...(draft.picture && !pictures.some((p) => p.value === draft.picture) ? [{ value: draft.picture, label: 'This poster’s picture' }] : [])], onChange: (picture) => set({ picture }) }) }), _jsxs(XStack, { gap: "$2", children: [_jsx(XStack, { render: "button", "aria-label": "Save these changes as a new version", disabled: !changed || busy, onPress: () => changed && !busy && edit(draft, 'Edited in place'), flex: 1, height: 36, items: "center", justify: "center", rounded: "$3", bg: changed ? '$ink' : '$hover', cursor: changed ? 'pointer' : 'default', children: _jsx(SizableText, { size: "$2", color: changed ? '$background' : '$soft', children: busy ? 'Saving…' : 'Save version' }) }), changed ? (_jsx(XStack, { render: "button", "aria-label": "Discard changes", onPress: () => setDraft(s), height: 36, px: "$3", items: "center", rounded: "$3", borderWidth: 1, borderColor: "$borderColor", cursor: "pointer", children: _jsx(SizableText, { size: "$2", children: "Discard" }) })) : null] })] })] }));
210
+ }
@@ -0,0 +1,6 @@
1
+ import { type Kind } from './kind.ts';
2
+ /** The platform's voice and the voices it reads in. */
3
+ export declare const VOICE = "zen-voice-mini";
4
+ export declare const VOICES: readonly ['alloy', 'ash', 'coral', 'echo', 'fable', 'nova', 'onyx', 'sage', 'shimmer'];
5
+ export declare const voice: Kind;
6
+ export declare const music: Kind;
@@ -0,0 +1,100 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * Sound: a voice reading words (the platform's speech, api.hanzo.ai
4
+ * /v1/audio/speech, billed to the org), and music (ACE-Step 1.5 on the org's
5
+ * GPU, offered while a GPU advertises `music.ace`).
6
+ */
7
+ import { SizableText, YStack } from '@hanzo/gui';
8
+ import { AudioLines, Music } from '@hanzogui/lucide-icons-2';
9
+ import { render } from '../gpu.js';
10
+ import { current, filename } from '../made.js';
11
+ import { useUrl } from '../use.js';
12
+ import { read, speak } from '../wire.js';
13
+ import { lit, num, str } from './kind.js';
14
+ /** The platform's voice and the voices it reads in. */
15
+ export const VOICE = 'zen-voice-mini';
16
+ export const VOICES = ['alloy', 'ash', 'coral', 'echo', 'fable', 'nova', 'onyx', 'sage', 'shimmer'];
17
+ export const voice = {
18
+ id: 'voice',
19
+ label: 'Voice',
20
+ blurb: 'Words read aloud: voiceovers, narration, announcements.',
21
+ icon: AudioLines,
22
+ tool: {
23
+ name: 'make_voice',
24
+ description: 'Read words aloud as speech: a voiceover, narration, an announcement, a podcast intro. Write the exact words to be spoken.',
25
+ parameters: {
26
+ type: 'object',
27
+ properties: {
28
+ title: { type: 'string', description: 'A short name for the tab.' },
29
+ script: { type: 'string', description: 'The exact words to speak, as they should be read.' },
30
+ voice: { type: 'string', enum: VOICES },
31
+ },
32
+ required: ['title', 'script'],
33
+ },
34
+ },
35
+ needs: () => null,
36
+ make: async (w, args) => {
37
+ const script = str(args.script);
38
+ if (!script)
39
+ throw new Error('Nothing to read.');
40
+ const v = VOICES.includes(str(args.voice)) ? str(args.voice) : 'nova';
41
+ w.doing('Reading it aloud');
42
+ const file = await w.keep('voice.mp3', await speak(w.wire, script.slice(0, 4000), v, VOICE, w.signal));
43
+ return { title: str(args.title, script.slice(0, 32)), draft: { spec: { script, voice: v }, files: [file], note: script.slice(0, 160), by: VOICE } };
44
+ },
45
+ revise: async (w, made, change) => {
46
+ const now = current(made).spec;
47
+ const next = { ...now, ...(change.patch ?? {}) };
48
+ if (change.instruction && !change.patch)
49
+ next.script = change.instruction;
50
+ const file = await w.keep('voice.mp3', await speak(w.wire, next.script.slice(0, 4000), next.voice, VOICE, w.signal));
51
+ return { spec: next, files: [file], note: change.instruction ?? 'Read again', by: VOICE };
52
+ },
53
+ View: Listen,
54
+ exports: [{ label: 'MP3', run: async (w, m, v) => ({ name: filename(m.title, 'mp3'), blob: await read(w, v.files[0].key) }) }],
55
+ };
56
+ export const music = {
57
+ id: 'music',
58
+ label: 'Music',
59
+ blurb: 'Songs and beds from a description, with lyrics or without.',
60
+ icon: Music,
61
+ tool: {
62
+ name: 'make_music',
63
+ description: 'Make music: a song, a jingle, a background bed. Describe the genre, mood, tempo and instruments; lyrics are optional.',
64
+ parameters: {
65
+ type: 'object',
66
+ properties: {
67
+ title: { type: 'string', description: 'A short name for the tab.' },
68
+ prompt: { type: 'string', description: 'Genre, mood, tempo, instruments.' },
69
+ lyrics: { type: 'string', description: 'Lyrics with [verse] and [chorus] marks, or empty for instrumental.' },
70
+ seconds: { type: 'integer', minimum: 10, maximum: 240 },
71
+ },
72
+ required: ['title', 'prompt'],
73
+ },
74
+ },
75
+ needs: (a) => (lit(a.fleet, 'music.ace') ? null : 'Music runs on a GPU. Connect one with `hanzo link`, and it appears here when the GPU can run it.'),
76
+ make: async (w, args) => {
77
+ const why = music.needs(w);
78
+ if (why)
79
+ throw new Error(why);
80
+ const body = { prompt: str(args.prompt), lyrics: str(args.lyrics), seconds: num(args.seconds, 60, 10, 240) };
81
+ const got = await render(w.wire, 'music', body, w.doing, w.signal);
82
+ const g = got[0];
83
+ const ext = g.path.split('.').pop() || 'mp3';
84
+ const file = await w.keep(`music.${ext}`, g.blob);
85
+ return { title: str(args.title, body.prompt.slice(0, 32)), draft: { spec: body, files: [file], note: body.prompt, by: 'ACE-Step 1.5 (your GPU)' } };
86
+ },
87
+ revise: async (w, made, change) => {
88
+ const now = current(made).spec;
89
+ const { draft } = await music.make(w, { title: made.title, ...now, ...(change.patch ?? {}), prompt: change.instruction ? `${now.prompt}. ${change.instruction}` : now.prompt });
90
+ return draft;
91
+ },
92
+ View: Listen,
93
+ exports: [{ label: 'Audio file', run: async (w, m, v) => ({ name: filename(m.title, v.files[0].name.split('.').pop() ?? 'mp3'), blob: await read(w, v.files[0].key) }) }],
94
+ };
95
+ function Listen({ version, wire, made }) {
96
+ const f = version.files[0];
97
+ const { src, error } = useUrl(wire, f?.key);
98
+ const words = String(version.spec.script ?? version.spec.lyrics ?? '');
99
+ return (_jsxs(YStack, { flex: 1, minH: 0, p: "$4", gap: "$4", items: "center", justify: "center", children: [error ? _jsx(SizableText, { color: "$soft", children: error }) : src ? _jsx("audio", { controls: true, src: src, "aria-label": made.title, style: { width: '100%', maxWidth: 520 } }) : null, words ? (_jsx(SizableText, { size: "$3", color: "$soft", maxW: 520, whiteSpace: "pre-wrap", children: words })) : null] }));
100
+ }