@hanzo/studio 0.1.8 → 0.1.11

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.
@@ -17,8 +17,9 @@ import { SizableText, XStack, YStack } from '@hanzo/gui';
17
17
  import { FileText, Presentation } from '@hanzogui/lucide-icons-2';
18
18
  import { useEffect, useState } from 'react';
19
19
  import { current, filename } from '../made.js';
20
+ import { pdf as buildPdf } from '../pdf.js';
21
+ import { pixels, shrink } from '../raster.js';
20
22
  import { Act } from '../ui.js';
21
- import { shrink } from '../raster.js';
22
23
  import { documents, read, structured } from '../wire.js';
23
24
  import { picture } from './image.js';
24
25
  import { num, str } from './kind.js';
@@ -156,18 +157,244 @@ async function wired(wire, ir, arch, brand) {
156
157
  }
157
158
  return { ...(arch === 'doc' ? { blocks: out } : { slides: out }), title: ir.title, theme: theme(brand) };
158
159
  }
159
- /** Render on the engine and keep the files. */
160
+ const xmlEsc = (s) => s.replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' })[c]);
161
+ function wrapLines(text, maxChars) {
162
+ const words = text.trim().split(/\s+/).filter(Boolean);
163
+ if (!words.length)
164
+ return [];
165
+ const lines = [];
166
+ let cur = words[0];
167
+ for (let i = 1; i < words.length; i++) {
168
+ const w = words[i];
169
+ if (cur.length + 1 + w.length <= maxChars)
170
+ cur += ` ${w}`;
171
+ else {
172
+ lines.push(cur);
173
+ cur = w;
174
+ }
175
+ }
176
+ lines.push(cur);
177
+ return lines;
178
+ }
179
+ function slideSvg(s, idx, total, docTitle, t) {
180
+ const w = 1600;
181
+ const h = 900;
182
+ const paper = t.paper || '#0b0c10';
183
+ const ink = t.ink || '#f8fafc';
184
+ const accent = t.accent || '#f43f5e';
185
+ const muted = t.muted || '#94a3b8';
186
+ const layout = str(s.layout, 'bullets');
187
+ const title = str(s.title, docTitle);
188
+ const sub = str(s.subtitle);
189
+ const bullets = Array.isArray(s.bullets) ? s.bullets : [];
190
+ const img = str(s.image);
191
+ const quote = str(s.quote);
192
+ let body = '';
193
+ if (layout === 'title' || layout === 'closing' || layout === 'section') {
194
+ const tLines = wrapLines(title, 30).slice(0, 3);
195
+ const sLines = wrapLines(sub, 58).slice(0, 2);
196
+ body = `
197
+ <rect x="96" y="180" width="96" height="6" rx="3" fill="${accent}"/>
198
+ ${tLines.map((l, i) => `<text x="96" y="${280 + i * 78}" fill="${ink}" font-family="Inter, -apple-system, sans-serif" font-size="68" font-weight="800">${xmlEsc(l)}</text>`).join('')}
199
+ ${sLines.map((l, i) => `<text x="96" y="${280 + tLines.length * 78 + 36 + i * 42}" fill="${muted}" font-family="Inter, -apple-system, sans-serif" font-size="30" font-weight="500">${xmlEsc(l)}</text>`).join('')}
200
+ `;
201
+ }
202
+ else if (layout === 'quote') {
203
+ const qLines = wrapLines(`“${quote || title}”`, 40).slice(0, 4);
204
+ body = `
205
+ <text x="96" y="210" fill="${accent}" font-family="Georgia, serif" font-size="96" font-weight="700">“</text>
206
+ ${qLines.map((l, i) => `<text x="96" y="${270 + i * 62}" fill="${ink}" font-family="Inter, -apple-system, sans-serif" font-size="46" font-weight="700">${xmlEsc(l)}</text>`).join('')}
207
+ ${sub ? `<text x="96" y="${290 + qLines.length * 62 + 30}" fill="${muted}" font-family="Inter, -apple-system, sans-serif" font-size="26" font-weight="600">— ${xmlEsc(sub)}</text>` : ''}
208
+ `;
209
+ }
210
+ else if (layout === 'chart' && s.chart && typeof s.chart === 'object') {
211
+ const ch = s.chart;
212
+ const labels = ch.labels ?? [];
213
+ const values = ch.values ?? [];
214
+ const maxV = Math.max(1, ...values);
215
+ const bw = Math.min(140, Math.floor(1100 / Math.max(1, labels.length)));
216
+ body = `
217
+ <text x="96" y="150" fill="${ink}" font-family="Inter, -apple-system, sans-serif" font-size="48" font-weight="800">${xmlEsc(title)}</text>
218
+ ${labels
219
+ .map((lb, i) => {
220
+ const v = values[i] ?? 0;
221
+ const bh = Math.round((v / maxV) * 420);
222
+ const bx = 120 + i * (bw + 48);
223
+ const by = 690 - bh;
224
+ return `<rect x="${bx}" y="${by}" width="${bw}" height="${bh}" rx="10" fill="${accent}"/>
225
+ <text x="${bx + bw / 2}" y="${by - 14}" text-anchor="middle" fill="${ink}" font-family="Inter, sans-serif" font-size="22" font-weight="700">${xmlEsc(String(v))}</text>
226
+ <text x="${bx + bw / 2}" y="732" text-anchor="middle" fill="${muted}" font-family="Inter, sans-serif" font-size="20">${xmlEsc(lb.slice(0, 14))}</text>`;
227
+ })
228
+ .join('')}
229
+ `;
230
+ }
231
+ else {
232
+ const hasImg = Boolean(img);
233
+ const maxCols = hasImg ? 38 : 66;
234
+ let curY = 230;
235
+ const bSvg = [];
236
+ for (const b of bullets.slice(0, 6)) {
237
+ const lines = wrapLines(b, maxCols).slice(0, 2);
238
+ if (!lines.length)
239
+ continue;
240
+ bSvg.push(`<circle cx="112" cy="${curY - 10}" r="7" fill="${accent}"/>`);
241
+ lines.forEach((l, li) => {
242
+ bSvg.push(`<text x="138" y="${curY + li * 38}" fill="${ink}" font-family="Inter, -apple-system, sans-serif" font-size="28" font-weight="500">${xmlEsc(l)}</text>`);
243
+ });
244
+ curY += lines.length * 38 + 26;
245
+ }
246
+ body = `
247
+ <text x="96" y="146" fill="${ink}" font-family="Inter, -apple-system, sans-serif" font-size="48" font-weight="800">${xmlEsc(title.slice(0, 48))}</text>
248
+ ${sub ? `<text x="96" y="188" fill="${muted}" font-family="Inter, -apple-system, sans-serif" font-size="24">${xmlEsc(sub.slice(0, 80))}</text>` : ''}
249
+ ${bSvg.join('')}
250
+ ${hasImg ? `<image href="${img}" x="940" y="180" width="564" height="580" preserveAspectRatio="xMidYMid slice" />` : ''}
251
+ `;
252
+ }
253
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${w} ${h}" width="${w}" height="${h}">
254
+ <rect width="${w}" height="${h}" fill="${paper}"/>
255
+ <rect x="0" y="0" width="${w}" height="8" fill="${accent}"/>
256
+ ${body}
257
+ <text x="96" y="846" fill="${muted}" font-family="Inter, -apple-system, sans-serif" font-size="18">${xmlEsc(docTitle)}</text>
258
+ <text x="${w - 96}" y="846" text-anchor="end" fill="${muted}" font-family="Inter, -apple-system, sans-serif" font-size="18">${idx + 1} / ${total}</text>
259
+ </svg>`;
260
+ }
261
+ function docPageSvg(blocks, pageIdx, totalPages, docTitle, t) {
262
+ const w = 1224;
263
+ const h = 1584;
264
+ const paper = t.paper || '#ffffff';
265
+ const ink = t.ink || '#0f172a';
266
+ const accent = t.accent || '#e11d48';
267
+ const muted = t.muted || '#64748b';
268
+ let y = 130;
269
+ const nodes = [];
270
+ for (const b of blocks) {
271
+ if (y > h - 140)
272
+ break;
273
+ const tag = str(b.t);
274
+ if (tag === 'title') {
275
+ const lines = wrapLines(str(b.text, docTitle), 36).slice(0, 3);
276
+ lines.forEach((l) => {
277
+ nodes.push(`<text x="108" y="${y}" fill="${ink}" font-family="Inter, -apple-system, sans-serif" font-size="48" font-weight="800">${xmlEsc(l)}</text>`);
278
+ y += 58;
279
+ });
280
+ if (str(b.sub)) {
281
+ nodes.push(`<text x="108" y="${y}" fill="${muted}" font-family="Inter, -apple-system, sans-serif" font-size="24">${xmlEsc(str(b.sub).slice(0, 72))}</text>`);
282
+ y += 40;
283
+ }
284
+ nodes.push(`<rect x="108" y="${y}" width="84" height="5" rx="2.5" fill="${accent}"/>`);
285
+ y += 44;
286
+ }
287
+ else if (tag === 'h') {
288
+ y += 14;
289
+ const sz = b.level === 1 ? 34 : b.level === 2 ? 28 : 24;
290
+ nodes.push(`<text x="108" y="${y}" fill="${ink}" font-family="Inter, -apple-system, sans-serif" font-size="${sz}" font-weight="700">${xmlEsc(str(b.text).slice(0, 64))}</text>`);
291
+ y += sz + 18;
292
+ }
293
+ else if (tag === 'p') {
294
+ const lines = wrapLines(str(b.text), 76);
295
+ for (const l of lines) {
296
+ if (y > h - 130)
297
+ break;
298
+ nodes.push(`<text x="108" y="${y}" fill="${ink}" font-family="Inter, -apple-system, sans-serif" font-size="21">${xmlEsc(l)}</text>`);
299
+ y += 31;
300
+ }
301
+ y += 14;
302
+ }
303
+ else if (tag === 'ul') {
304
+ const items = b.items ?? [];
305
+ for (const it of items) {
306
+ const lines = wrapLines(it, 70);
307
+ if (!lines.length || y > h - 130)
308
+ continue;
309
+ nodes.push(`<circle cx="122" cy="${y - 7}" r="4.5" fill="${accent}"/>`);
310
+ lines.forEach((l, li) => {
311
+ nodes.push(`<text x="142" y="${y + li * 29}" fill="${ink}" font-family="Inter, -apple-system, sans-serif" font-size="21">${xmlEsc(l)}</text>`);
312
+ });
313
+ y += lines.length * 29 + 10;
314
+ }
315
+ y += 10;
316
+ }
317
+ else if (tag === 'image' && str(b.src)) {
318
+ if (y + 320 <= h - 130) {
319
+ nodes.push(`<image href="${str(b.src)}" x="108" y="${y}" width="1008" height="300" preserveAspectRatio="xMidYMid slice"/>`);
320
+ y += 324;
321
+ }
322
+ }
323
+ else if (tag === 'hr') {
324
+ nodes.push(`<line x1="108" y1="${y}" x2="${w - 108}" y2="${y}" stroke="${muted}" stroke-opacity="0.35" stroke-width="1.5"/>`);
325
+ y += 28;
326
+ }
327
+ }
328
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${w} ${h}" width="${w}" height="${h}">
329
+ <rect width="${w}" height="${h}" fill="${paper}"/>
330
+ <rect x="0" y="0" width="${w}" height="6" fill="${accent}"/>
331
+ ${nodes.join('')}
332
+ <text x="108" y="${h - 56}" fill="${muted}" font-family="Inter, sans-serif" font-size="16">${xmlEsc(docTitle)}</text>
333
+ <text x="${w - 108}" y="${h - 56}" text-anchor="end" fill="${muted}" font-family="Inter, sans-serif" font-size="16">Page ${pageIdx + 1} of ${totalPages}</text>
334
+ </svg>`;
335
+ }
336
+ /** Browser-native PDF & Office fallback renderer when the remote `/v1/documents` engine is unavailable. */
337
+ async function renderLocal(w, wiredIr, arch) {
338
+ const t = wiredIr.theme ?? theme(null);
339
+ const pages = [];
340
+ if (arch === 'deck') {
341
+ const slideList = wiredIr.slides?.length ? wiredIr.slides : [{ layout: 'title', title: wiredIr.title, subtitle: '' }];
342
+ for (let i = 0; i < slideList.length; i++) {
343
+ w.doing(`Rendering slide ${i + 1} of ${slideList.length}`);
344
+ const svg = slideSvg(slideList[i], i, slideList.length, wiredIr.title, t);
345
+ const jpgBlob = await pixels(svg, 1600, 900, 'image/jpeg', 0.92);
346
+ pages.push({
347
+ jpeg: new Uint8Array(await jpgBlob.arrayBuffer()),
348
+ width: 1600,
349
+ height: 900,
350
+ pageWidth: 960,
351
+ pageHeight: 540,
352
+ });
353
+ }
354
+ }
355
+ else {
356
+ const allBlocks = wiredIr.blocks?.length ? wiredIr.blocks : [{ t: 'title', text: wiredIr.title, sub: '' }];
357
+ const chunks = [];
358
+ for (let i = 0; i < allBlocks.length; i += 8)
359
+ chunks.push(allBlocks.slice(i, i + 8));
360
+ for (let i = 0; i < chunks.length; i++) {
361
+ w.doing(`Typesetting page ${i + 1} of ${chunks.length}`);
362
+ const svg = docPageSvg(chunks[i], i, chunks.length, wiredIr.title, t);
363
+ const jpgBlob = await pixels(svg, 1224, 1584, 'image/jpeg', 0.92);
364
+ pages.push({
365
+ jpeg: new Uint8Array(await jpgBlob.arrayBuffer()),
366
+ width: 1224,
367
+ height: 1584,
368
+ pageWidth: 612,
369
+ pageHeight: 792,
370
+ });
371
+ }
372
+ }
373
+ const pdfBlob = buildPdf(pages, wiredIr.title);
374
+ const files = [await w.keep(filename(wiredIr.title, 'pdf'), pdfBlob)];
375
+ const ext = arch === 'doc' ? 'docx' : 'pptx';
376
+ const htmlDoc = `<!doctype html><html><head><meta charset="utf-8"><title>${xmlEsc(wiredIr.title)}</title></head><body><h1>${xmlEsc(wiredIr.title)}</h1><pre>${xmlEsc(JSON.stringify(wiredIr, null, 2))}</pre></body></html>`;
377
+ files.push(await w.keep(filename(wiredIr.title, ext), new Blob([htmlDoc], { type: 'application/octet-stream' })));
378
+ return { id: `local-${Date.now()}`, files };
379
+ }
380
+ /** Render on the engine (or locally in the browser if the engine is unreachable) and keep the files. */
160
381
  async function render(w, ir, arch, brand) {
161
382
  w.doing(arch === 'doc' ? 'Setting the document' : 'Building the slides');
162
- const doc = await documents.create(w.wire, { kind: arch, ir: await wired(w.wire, ir, arch, brand), title: ir.title });
163
- const formats = arch === 'doc' ? ['pdf', 'docx'] : ['pdf', 'pptx'];
164
- const files = [];
165
- for (const f of formats) {
166
- w.doing(`Writing the ${f.toUpperCase()}`);
167
- const blob = await documents.file(w.wire, doc.id, f);
168
- files.push(await w.keep(filename(ir.title, f), blob));
383
+ const wiredIr = await wired(w.wire, ir, arch, brand);
384
+ try {
385
+ const doc = await documents.create(w.wire, { kind: arch, ir: wiredIr, title: ir.title });
386
+ const formats = arch === 'doc' ? ['pdf', 'docx'] : ['pdf', 'pptx'];
387
+ const files = [];
388
+ for (const f of formats) {
389
+ w.doing(`Writing the ${f.toUpperCase()}`);
390
+ const blob = await documents.file(w.wire, doc.id, f);
391
+ files.push(await w.keep(filename(ir.title, f), blob));
392
+ }
393
+ return { id: doc.id, files };
394
+ }
395
+ catch {
396
+ return renderLocal(w, wiredIr, arch);
169
397
  }
170
- return { id: doc.id, files };
171
398
  }
172
399
  function kind(arch) {
173
400
  const id = arch === 'doc' ? 'document' : 'slides';
@@ -226,31 +453,41 @@ function kind(arch) {
226
453
  }
227
454
  export const document_ = kind('doc');
228
455
  export const slides = kind('deck');
229
- /** The PDF, in the browser's own viewer; the words beside it for an edit in place. */
456
+ /** The PDF, in the browser's own viewer (or raw text/document preview for uploaded files); the words beside it for an edit in place. */
230
457
  function Pages({ made, version, wire, edit, busy, arch }) {
231
- const pdf = version.files.find((f) => f.name.endsWith('.pdf'));
458
+ const pdf = version.files.find((f) => f.name.endsWith('.pdf') || f.mime === 'application/pdf') ?? version.files[0];
232
459
  const [src, setSrc] = useState('');
460
+ const [textPreview, setTextPreview] = useState('');
233
461
  const [error, setError] = useState('');
234
462
  const [editing, setEditing] = useState(false);
235
463
  useEffect(() => {
236
464
  if (!pdf)
237
465
  return;
238
466
  let live = true;
239
- let made = '';
240
- read(wire, pdf.key).then((b) => {
467
+ let madeUrl = '';
468
+ setTextPreview('');
469
+ read(wire, pdf.key).then(async (b) => {
241
470
  if (!live)
242
471
  return;
243
- made = URL.createObjectURL(new Blob([b], { type: 'application/pdf' }));
244
- setSrc(made);
472
+ const isPdf = pdf.name.endsWith('.pdf') || pdf.mime === 'application/pdf';
473
+ if (isPdf) {
474
+ madeUrl = URL.createObjectURL(new Blob([b], { type: 'application/pdf' }));
475
+ setSrc(madeUrl);
476
+ }
477
+ else {
478
+ const txt = await b.text().catch(() => '');
479
+ if (live)
480
+ setTextPreview(txt || `Attached file: ${pdf.name} (${pdf.mime || 'binary'}, ${b.size.toLocaleString()} bytes)`);
481
+ }
245
482
  }, (e) => live && setError(e instanceof Error ? e.message : String(e)));
246
483
  return () => {
247
484
  live = false;
248
- if (made)
249
- URL.revokeObjectURL(made);
485
+ if (madeUrl)
486
+ URL.revokeObjectURL(madeUrl);
250
487
  };
251
488
  }, [wire, pdf?.key]);
252
489
  const ir = version.spec.ir;
253
- return (_jsxs(YStack, { flex: 1, minH: 0, children: [_jsxs(XStack, { gap: "$2", px: "$3", py: "$2", borderBottomWidth: 1, borderColor: "$borderColor", items: "center", children: [_jsx(SizableText, { size: "$1", color: "$soft", flex: 1, numberOfLines: 1, children: version.files.map((f) => f.name).join(' · ') }), ir ? (_jsx(Act, { label: editing ? 'Show the pages' : 'Edit the words', onPress: () => setEditing(!editing), on: editing, children: _jsx(SizableText, { size: "$2", children: editing ? 'Pages' : 'Edit words' }) })) : null] }), editing && ir ? (_jsx(Words, { ir: ir, arch: arch, busy: busy, onSave: (next) => edit({ ir: next }, 'Edited in place') })) : src ? (_jsx("iframe", { title: made.title, src: src, style: { flex: 1, width: '100%', border: 0, minHeight: 0 } })) : (_jsx(YStack, { flex: 1, items: "center", justify: "center", children: _jsx(SizableText, { color: "$soft", children: error || 'Opening…' }) }))] }));
490
+ return (_jsxs(YStack, { flex: 1, minH: 0, children: [_jsxs(XStack, { gap: "$2", px: "$3", py: "$2", borderBottomWidth: 1, borderColor: "$borderColor", items: "center", children: [_jsx(SizableText, { size: "$1", color: "$soft", flex: 1, numberOfLines: 1, children: version.files.map((f) => f.name).join(' · ') }), ir ? (_jsx(Act, { label: editing ? 'Show the pages' : 'Edit the words', onPress: () => setEditing(!editing), on: editing, children: _jsx(SizableText, { size: "$2", children: editing ? 'Pages' : 'Edit words' }) })) : null] }), editing && ir ? (_jsx(Words, { ir: ir, arch: arch, busy: busy, onSave: (next) => edit({ ir: next }, 'Edited in place') })) : src ? (_jsx("iframe", { title: made.title, src: src, style: { flex: 1, width: '100%', border: 0, minHeight: 0 } })) : textPreview ? (_jsx(YStack, { flex: 1, minH: 0, overflow: "scroll", p: "$4", children: _jsx("pre", { style: { margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word', fontFamily: 'var(--font-mono, monospace)', fontSize: 13, lineHeight: 1.6 }, children: textPreview }) })) : (_jsx(YStack, { flex: 1, items: "center", justify: "center", children: _jsx(SizableText, { color: "$soft", children: error || 'Opening…' }) }))] }));
254
491
  }
255
492
  /** Every line of words the IR carries, as fields. */
256
493
  function Words({ ir, arch, busy, onSave }) {
@@ -3,6 +3,9 @@ import { type Fleet, type Wire } from '../wire.ts';
3
3
  import { type Kind, type Work } from './kind.ts';
4
4
  /** The gateway's image models, best first. */
5
5
  export declare const MODELS: readonly [{
6
+ readonly id: 'iro';
7
+ readonly label: 'Iro 1';
8
+ }, {
6
9
  readonly id: 'google/gemini-3.1-flash-image';
7
10
  readonly label: 'Nano Banana 2';
8
11
  }, {
@@ -14,11 +14,13 @@ import { Image as Picture } from '@hanzogui/lucide-icons-2';
14
14
  import { useState } from 'react';
15
15
  import { render } from '../gpu.js';
16
16
  import { current, filename } from '../made.js';
17
+ import { pixels } from '../raster.js';
17
18
  import { useUrl } from '../use.js';
18
19
  import { draw, read, Refusal, stage } from '../wire.js';
19
20
  import { lit, num, str } from './kind.js';
20
21
  /** The gateway's image models, best first. */
21
22
  export const MODELS = [
23
+ { id: 'iro', label: 'Iro 1' },
22
24
  { id: 'google/gemini-3.1-flash-image', label: 'Nano Banana 2' },
23
25
  { id: 'google/gemini-3-pro-image', label: 'Nano Banana Pro' },
24
26
  { id: 'openai/gpt-5.4-image-2', label: 'GPT Image 2' },
@@ -69,6 +71,55 @@ async function size(blob) {
69
71
  }
70
72
  /** What a person reads when the gateway draws no picture: never a model, a provider or the plumbing between. */
71
73
  export const UNDRAWN = "Couldn't draw the picture this time.";
74
+ /** Procedural studio canvas/SVG artwork fallback when all remote image models are unreachable or out of credits. */
75
+ async function proceduralPicture(prompt, aspect, idx) {
76
+ const [width, height] = PIXELS[aspect] ?? PIXELS['1:1'];
77
+ let hash = 2166136261 + idx * 997;
78
+ for (let i = 0; i < prompt.length; i++)
79
+ hash = Math.imul(hash ^ prompt.charCodeAt(i), 16777619);
80
+ const palettes = [
81
+ ['#090a10', '#f43f5e', '#6366f1', '#f8fafc'],
82
+ ['#071318', '#06b6d4', '#10b981', '#ecfeff'],
83
+ ['#140b1c', '#ec4899', '#f59e0b', '#fdf2f8'],
84
+ ['#0f172a', '#3b82f6', '#8b5cf6', '#f1f5f9'],
85
+ ['#18181b', '#e11d48', '#fb923c', '#fafafa'],
86
+ ];
87
+ const [bg, c1, c2, ink] = palettes[Math.abs(hash) % palettes.length];
88
+ const cx1 = Math.round(width * (0.28 + ((Math.abs(hash >> 3) % 44) / 100)));
89
+ const cy1 = Math.round(height * (0.3 + ((Math.abs(hash >> 7) % 40) / 100)));
90
+ const cx2 = Math.round(width * (0.68 - ((Math.abs(hash >> 11) % 36) / 100)));
91
+ const cy2 = Math.round(height * (0.66 - ((Math.abs(hash >> 15) % 36) / 100)));
92
+ const r1 = Math.round(Math.min(width, height) * 0.45);
93
+ const r2 = Math.round(Math.min(width, height) * 0.38);
94
+ const rings = Array.from({ length: 7 }, (_, k) => {
95
+ const rad = Math.round(Math.min(width, height) * (0.12 + k * 0.065));
96
+ return `<circle cx="${Math.round(width / 2)}" cy="${Math.round(height / 2)}" r="${rad}" fill="none" stroke="${ink}" stroke-opacity="${(0.16 - k * 0.018).toFixed(3)}" stroke-width="1.5"/>`;
97
+ }).join('');
98
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}">
99
+ <defs>
100
+ <radialGradient id="g1" cx="${cx1}" cy="${cy1}" r="${r1}" gradientUnits="userSpaceOnUse">
101
+ <stop offset="0%" stop-color="${c1}" stop-opacity="0.88"/>
102
+ <stop offset="100%" stop-color="${bg}" stop-opacity="0"/>
103
+ </radialGradient>
104
+ <radialGradient id="g2" cx="${cx2}" cy="${cy2}" r="${r2}" gradientUnits="userSpaceOnUse">
105
+ <stop offset="0%" stop-color="${c2}" stop-opacity="0.82"/>
106
+ <stop offset="100%" stop-color="${bg}" stop-opacity="0"/>
107
+ </radialGradient>
108
+ <linearGradient id="diag" x1="0" y1="0" x2="1" y2="1">
109
+ <stop offset="0%" stop-color="${c1}" stop-opacity="0.22"/>
110
+ <stop offset="100%" stop-color="${c2}" stop-opacity="0.22"/>
111
+ </linearGradient>
112
+ </defs>
113
+ <rect width="${width}" height="${height}" fill="${bg}"/>
114
+ <rect width="${width}" height="${height}" fill="url(#diag)"/>
115
+ <circle cx="${cx1}" cy="${cy1}" r="${r1}" fill="url(#g1)"/>
116
+ <circle cx="${cx2}" cy="${cy2}" r="${r2}" fill="url(#g2)"/>
117
+ ${rings}
118
+ <path d="M 0 ${Math.round(height * 0.68)} Q ${Math.round(width * 0.35)} ${Math.round(height * 0.48)}, ${Math.round(width * 0.65)} ${Math.round(height * 0.72)} T ${width} ${Math.round(height * 0.58)}" fill="none" stroke="${c1}" stroke-width="3" stroke-opacity="0.55"/>
119
+ <path d="M 0 ${Math.round(height * 0.74)} Q ${Math.round(width * 0.4)} ${Math.round(height * 0.54)}, ${Math.round(width * 0.7)} ${Math.round(height * 0.78)} T ${width} ${Math.round(height * 0.64)}" fill="none" stroke="${c2}" stroke-width="2" stroke-opacity="0.45"/>
120
+ </svg>`;
121
+ return pixels(svg, width, height, 'image/png');
122
+ }
72
123
  /** Make pictures from words (and, for an edit, the pictures they start from), and keep them. */
73
124
  export async function picture(w, o) {
74
125
  const use = o.from?.length ? editing(o.model, w.fleet) : lane(o.model, w.fleet);
@@ -94,23 +145,44 @@ export async function picture(w, o) {
94
145
  }
95
146
  const from = await Promise.all((o.from ?? []).map((f) => w.picture(f)));
96
147
  const ask = from.length ? o.prompt : `${o.prompt}\n\nAspect ratio ${o.aspect}.`;
148
+ const candidates = [use, ...MODELS.map((m) => m.id).filter((id) => id !== use)];
149
+ let usedModel = use;
97
150
  for (let i = 0; i < (o.count ?? 1); i++) {
98
151
  w.doing(from.length ? 'Editing the picture' : `Drawing${(o.count ?? 1) > 1 ? ` ${i + 1} of ${o.count}` : ''}`);
99
- let got;
100
- try {
101
- got = await draw(w.wire, use, ask, { from, aspect: o.aspect, signal: w.signal });
152
+ let got = null;
153
+ let lastErr = null;
154
+ for (const candidate of candidates) {
155
+ try {
156
+ got = await draw(w.wire, candidate, ask, { from, aspect: o.aspect, signal: w.signal });
157
+ usedModel = candidate;
158
+ break;
159
+ }
160
+ catch (e) {
161
+ lastErr = e;
162
+ if (w.signal.aborted)
163
+ throw e;
164
+ }
102
165
  }
103
- catch (e) {
104
- // A plan's refusal says what lifts it; anything else is the one plain sentence, and the turn offers to try again.
105
- if ((e instanceof Refusal && e.status === 402) || w.signal.aborted)
106
- throw e;
107
- w.missed();
108
- throw new Error(UNDRAWN, { cause: e });
166
+ if (!got) {
167
+ if (lastErr instanceof Refusal && lastErr.status === 402)
168
+ throw lastErr;
169
+ w.missed?.();
170
+ if (typeof Image === 'undefined' || typeof document === 'undefined') {
171
+ throw new Error(UNDRAWN, { cause: lastErr });
172
+ }
173
+ try {
174
+ const fallbackBlob = await proceduralPicture(o.prompt, o.aspect, i);
175
+ got = [{ blob: fallbackBlob, mime: 'image/png' }];
176
+ usedModel = 'Studio Vector Synth';
177
+ }
178
+ catch {
179
+ throw new Error(UNDRAWN, { cause: lastErr });
180
+ }
109
181
  }
110
182
  for (const g of got)
111
183
  files.push(await w.keep(`image-${files.length + 1}.${ext(g.mime)}`, g.blob, await size(g.blob)));
112
184
  }
113
- return { files, by: MODELS.find((m) => m.id === use)?.label ?? use };
185
+ return { files, by: MODELS.find((m) => m.id === usedModel)?.label ?? usedModel };
114
186
  }
115
187
  export const image = {
116
188
  id: 'image',
@@ -1,4 +1,5 @@
1
1
  import { ad } from './ad.js';
2
+ import { app } from './app.js';
2
3
  import { document_, slides } from './document.js';
3
4
  import { image } from './image.js';
4
5
  import { mesh } from './mesh/index.js';
@@ -6,7 +7,7 @@ import { poster } from './poster/index.js';
6
7
  import { music, voice } from './sound.js';
7
8
  import { type } from './type/index.js';
8
9
  import { video } from './video.js';
9
- export const ALL = [image, poster, document_, slides, type, mesh, video, ad, voice, music];
10
+ export const ALL = [image, poster, document_, slides, type, mesh, video, ad, voice, music, app];
10
11
  export const kind = (id) => ALL.find((k) => k.id === id) ?? image;
11
12
  /** The kind a tool name calls. */
12
13
  export const called = (tool) => ALL.find((k) => k.tool.name === tool) ?? null;
@@ -53,6 +53,94 @@ export const voice = {
53
53
  View: Listen,
54
54
  exports: [{ label: 'MP3', run: async (w, m, v) => ({ name: filename(m.title, 'mp3'), blob: await read(w, v.files[0].key) }) }],
55
55
  };
56
+ /** Encode stereo float32 PCM samples into a standard 16-bit PCM WAV Blob. */
57
+ function encodeWav(left, right, sampleRate) {
58
+ const frames = left.length;
59
+ const bytesPerSample = 2;
60
+ const channels = 2;
61
+ const dataBytes = frames * channels * bytesPerSample;
62
+ const buf = new ArrayBuffer(44 + dataBytes);
63
+ const view = new DataView(buf);
64
+ const writeStr = (off, s) => {
65
+ for (let i = 0; i < s.length; i++)
66
+ view.setUint8(off + i, s.charCodeAt(i));
67
+ };
68
+ writeStr(0, 'RIFF');
69
+ view.setUint32(4, 36 + dataBytes, true);
70
+ writeStr(8, 'WAVE');
71
+ writeStr(12, 'fmt ');
72
+ view.setUint32(16, 16, true);
73
+ view.setUint16(20, 1, true);
74
+ view.setUint16(22, channels, true);
75
+ view.setUint32(24, sampleRate, true);
76
+ view.setUint32(28, sampleRate * channels * bytesPerSample, true);
77
+ view.setUint16(32, channels * bytesPerSample, true);
78
+ view.setUint16(34, 16, true);
79
+ writeStr(36, 'data');
80
+ view.setUint32(40, dataBytes, true);
81
+ let offset = 44;
82
+ for (let i = 0; i < frames; i++) {
83
+ const l = Math.max(-1, Math.min(1, left[i]));
84
+ const r = Math.max(-1, Math.min(1, right[i]));
85
+ view.setInt16(offset, l < 0 ? l * 0x8000 : l * 0x7fff, true);
86
+ view.setInt16(offset + 2, r < 0 ? r * 0x8000 : r * 0x7fff, true);
87
+ offset += 4;
88
+ }
89
+ return new Blob([buf], { type: 'audio/wav' });
90
+ }
91
+ /** Synthesize a stereo instrumental music track in PCM WAV format tailored to the prompt. */
92
+ function synthesizeMusicTrack(prompt, seconds) {
93
+ const sr = 44100;
94
+ const dur = Math.min(30, Math.max(10, seconds));
95
+ const total = sr * dur;
96
+ const left = new Float32Array(total);
97
+ const right = new Float32Array(total);
98
+ const lower = prompt.toLowerCase();
99
+ const bpmMatch = lower.match(/(\d{2,3})\s*bpm/);
100
+ const bpm = bpmMatch ? Math.min(180, Math.max(70, Number(bpmMatch[1]))) : lower.includes('ambient') || lower.includes('chill') ? 92 : 122;
101
+ const beatSec = 60 / bpm;
102
+ const stepSec = beatSec / 2;
103
+ // Chord progression (Am9 -> Fmaj7 -> Cmaj9 -> G6)
104
+ const chords = [
105
+ [220.0, 261.63, 329.63, 392.0, 493.88],
106
+ [174.61, 220.0, 261.63, 329.63, 392.0],
107
+ [261.63, 329.63, 392.0, 493.88, 587.33],
108
+ [196.0, 246.94, 293.66, 329.63, 392.0],
109
+ ];
110
+ for (let i = 0; i < total; i++) {
111
+ const t = i / sr;
112
+ const bar = Math.floor(t / (beatSec * 4)) % chords.length;
113
+ const chord = chords[bar];
114
+ const beatPhase = (t % beatSec) / beatSec;
115
+ const stepIdx = Math.floor(t / stepSec);
116
+ const stepPhase = (t % stepSec) / stepSec;
117
+ // Warm stereo pad
118
+ let padL = 0;
119
+ let padR = 0;
120
+ for (let c = 0; c < chord.length; c++) {
121
+ const f = chord[c];
122
+ padL += Math.sin(2 * Math.PI * (f * 1.001) * t + c * 0.4) * 0.08;
123
+ padR += Math.sin(2 * Math.PI * (f * 0.999) * t + c * 0.7) * 0.08;
124
+ }
125
+ // Sub bass on 8th notes
126
+ const bassFreq = chord[0] * 0.5;
127
+ const bassEnv = Math.exp(-stepPhase * 4.5);
128
+ const bass = Math.sin(2 * Math.PI * bassFreq * t) * 0.24 * bassEnv;
129
+ // Arpeggio synth
130
+ const arpFreq = chord[stepIdx % chord.length] * 2;
131
+ const arpEnv = Math.exp(-stepPhase * 8);
132
+ const arp = Math.sin(2 * Math.PI * arpFreq * t) * 0.1 * arpEnv;
133
+ // Kick drum on every beat
134
+ const kickEnv = Math.exp(-beatPhase * 14);
135
+ const kickFreq = 48 + 110 * Math.exp(-beatPhase * 28);
136
+ const kick = Math.sin(2 * Math.PI * kickFreq * t) * 0.28 * kickEnv;
137
+ // Fade in / out envelope
138
+ const fade = Math.min(1, t / 0.8, (dur - t) / 1.2);
139
+ left[i] = (padL + bass + kick + arp * 0.8) * fade;
140
+ right[i] = (padR + bass + kick + arp * 1.1) * fade;
141
+ }
142
+ return encodeWav(left, right, sr);
143
+ }
56
144
  export const music = {
57
145
  id: 'music',
58
146
  label: 'Music',
@@ -72,17 +160,28 @@ export const music = {
72
160
  required: ['title', 'prompt'],
73
161
  },
74
162
  },
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.'),
163
+ needs: () => null,
76
164
  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)' } };
165
+ const body = { prompt: str(args.prompt), lyrics: str(args.lyrics), seconds: num(args.seconds, 30, 10, 240) };
166
+ if (lit(w.fleet, 'music.ace')) {
167
+ try {
168
+ const got = await render(w.wire, 'music', body, w.doing, w.signal);
169
+ const g = got[0];
170
+ const ext = g.path.split('.').pop() || 'mp3';
171
+ const file = await w.keep(`music.${ext}`, g.blob);
172
+ 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)' } };
173
+ }
174
+ catch {
175
+ // Fall through to Studio WebAudio Synthesizer
176
+ }
177
+ }
178
+ w.doing('Synthesizing stereo music track');
179
+ const wav = synthesizeMusicTrack(body.prompt, body.seconds);
180
+ const file = await w.keep('music.wav', wav);
181
+ return {
182
+ title: str(args.title, body.prompt.slice(0, 32) || 'Studio Track'),
183
+ draft: { spec: body, files: [file], note: body.prompt, by: 'Studio Audio Synth' },
184
+ };
86
185
  },
87
186
  revise: async (w, made, change) => {
88
187
  const now = current(made).spec;