@hanzo/studio 0.0.0-stage → 0.1.0

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 +237 -0
  70. package/lib/wire.js +455 -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
package/lib/talk.js ADDED
@@ -0,0 +1,369 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * A CONVERSATION and its tabs: the chat that makes things on the left, every
4
+ * thing it made in a tab on the right. The same two panes as a Dev run, from
5
+ * the same parts: @hanzo/ui's `Chat` for the thread and its composer (built by
6
+ * @hanzo/build's `prompt`), and @hanzo/build's `Panel` over a `Deck` for the
7
+ * tabs, scoped to this conversation so its tabs are kept with it.
8
+ *
9
+ * A new conversation has no id in the address until its first message is sent;
10
+ * then the address names it, and the conversation in hand is the one shown.
11
+ */
12
+ import { Grip, Panel, PanelToggle, prompt, Reply, useDeck, useKey, usePanel } from '@hanzo/build';
13
+ import { SizableText, useMedia, XStack, YStack } from '@hanzo/gui';
14
+ import { Check, ChevronDown, Ellipsis, Image as ImageIcon, LoaderCircle, Palette, Sparkles, X } from '@hanzogui/lucide-icons-2';
15
+ import { DropdownMenu } from '@hanzo/ui';
16
+ import { Chat as Thread, Failure } from '@hanzo/ui/chat';
17
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
18
+ import { edit as editInPlace, turn } from './chat/turn.js';
19
+ import { keep, listed, open } from './chat/store.js';
20
+ import { Desk } from './desk.js';
21
+ import { Home } from './home.js';
22
+ import { useHost } from './host.js';
23
+ import { MODELS, useImageLane, ZEN } from './kinds/image.js';
24
+ import { ALL, kind } from './kinds/index.js';
25
+ import { lit } from './kinds/kind.js';
26
+ import { PRESETS } from './kinds/type/system.js';
27
+ import { BRAIN, KEPT, touch, useAvailable, useLibrary, useWire, WRITER } from './live.js';
28
+ import { blank, current } from './made.js';
29
+ import { take } from './places.js';
30
+ import { path } from './route.js';
31
+ import { useUrl } from './use.js';
32
+ import { put } from './wire.js';
33
+ /** The conversations open in this page, by id: an address change does not drop the one in hand. */
34
+ const held = new Map();
35
+ /** The pane edge and gutter, as @hanzo/build cuts a Dev run's two panes. */
36
+ const pane = {
37
+ bg: 'var(--pane-fill, var(--sheet-1))',
38
+ rounded: 'var(--pane-round, var(--radius-xl))',
39
+ borderWidth: 1,
40
+ borderColor: 'var(--pane-edge, var(--white-08))',
41
+ boxShadow: 'var(--shadow-sheet-2)',
42
+ };
43
+ const gap = 'var(--pane-gap, var(--space-2))';
44
+ const DESK = 560;
45
+ const DESK_FLOOR = 320;
46
+ const TALK_FLOOR = 320;
47
+ export function Talk({ id }) {
48
+ const host = useHost();
49
+ const wire = useWire(host);
50
+ const available = useAvailable(wire);
51
+ const [chat, setChat] = useState(() => (id ? (held.get(id) ?? null) : blank()));
52
+ const [failed, setFailed] = useState('');
53
+ useEffect(() => {
54
+ if (!id) {
55
+ setChat(blank());
56
+ return;
57
+ }
58
+ const mine = held.get(id);
59
+ if (mine) {
60
+ setChat(mine);
61
+ return;
62
+ }
63
+ let live = true;
64
+ setChat(null);
65
+ open(wire, id).then((c) => {
66
+ if (!live)
67
+ return;
68
+ if (!c)
69
+ return setFailed('This conversation is not here, or belongs to another organization.');
70
+ // A turn that was working when its page closed did not finish: say so.
71
+ const settled = { ...c, turns: c.turns.map((t) => (t.doing ? { ...t, doing: undefined, failed: t.failed ?? 'Stopped: the page closed before this finished.' } : t)) };
72
+ held.set(c.id, settled);
73
+ setChat(settled);
74
+ }, (e) => live && setFailed(e instanceof Error ? e.message : String(e)));
75
+ return () => {
76
+ live = false;
77
+ };
78
+ }, [id, wire]);
79
+ if (failed) {
80
+ return (_jsxs(YStack, { flex: 1, items: "center", justify: "center", gap: "$3", p: "$5", children: [_jsx(SizableText, { color: "$soft", children: failed }), _jsx(XStack, { render: "button", onPress: () => host.go(''), px: "$3", height: 34, items: "center", rounded: "$3", borderWidth: 1, borderColor: "$borderColor", cursor: "pointer", children: _jsx(SizableText, { size: "$2", children: "Start a new one" }) })] }));
81
+ }
82
+ if (!chat) {
83
+ return (_jsx(YStack, { flex: 1, items: "center", justify: "center", children: _jsx(SizableText, { color: "$soft", children: "Opening\u2026" }) }));
84
+ }
85
+ return _jsx(Conversation, { start: chat, host: host, wire: wire, available: available }, chat.id);
86
+ }
87
+ function Conversation({ start, host, wire, available }) {
88
+ const [chat, setChat] = useState(start);
89
+ const latest = useRef(chat);
90
+ const [draft, setDraft] = useState('');
91
+ const [attached, setAttached] = useState([]);
92
+ const [uploading, setUploading] = useState(0);
93
+ const [picked, setPicked] = useState(null);
94
+ const [options, setOptions] = useState({ model: 'auto', aspect: '', count: 1 });
95
+ const [working, setWorking] = useState(null);
96
+ const [editing, setEditing] = useState({});
97
+ const [editFailed, setEditFailed] = useState({});
98
+ const wide = useMedia().md;
99
+ const shelf = useLibrary(wire);
100
+ const imaging = useImageLane();
101
+ const [desk, setDesk] = usePanel('studio', true, wide);
102
+ useKey(() => setDesk(!desk));
103
+ const deck = useDeck(`studio.${chat.id}`, { tabs: chat.made.map((m) => ({ id: m.id, kind: m.id, title: m.title })), at: chat.made.at(-1)?.id ?? null });
104
+ const order = useRef([]);
105
+ order.current = [...deck.tabs.map((t) => t.kind).filter((k) => chat.made.some((m) => m.id === k)), ...chat.made.map((m) => m.id).filter((m) => !deck.tabs.some((t) => t.kind === m))];
106
+ const [span, setSpan] = useState(DESK);
107
+ const deskBox = useRef(null);
108
+ // Saved after every change, at most once a second and a half; at once when a turn ends.
109
+ const saving = useRef(null);
110
+ const persist = useCallback((c, now = false) => {
111
+ if (saving.current)
112
+ clearTimeout(saving.current);
113
+ const go = () => {
114
+ saving.current = null;
115
+ if (!c.turns.length)
116
+ return;
117
+ void keep(wire, c).catch(() => undefined);
118
+ };
119
+ if (now)
120
+ go();
121
+ else
122
+ saving.current = setTimeout(go, 1500);
123
+ }, [wire]);
124
+ const changed = useCallback((c) => {
125
+ latest.current = c;
126
+ held.set(c.id, c);
127
+ setChat(c);
128
+ if (c.turns.length)
129
+ touch(listed(c));
130
+ persist(c);
131
+ }, [persist]);
132
+ const show = useCallback((mid) => {
133
+ const m = latest.current.made.find((x) => x.id === mid);
134
+ deck.open({ id: mid, kind: mid, title: m?.title ?? 'Made' });
135
+ if (!order.current.includes(mid))
136
+ order.current = [...order.current, mid];
137
+ setDesk(true);
138
+ }, [deck, setDesk]);
139
+ const send = async (text, how = {}) => {
140
+ const words = text.trim();
141
+ if (!words && !attached.length)
142
+ return;
143
+ if (working)
144
+ return;
145
+ if (!host.person) {
146
+ host.signIn?.();
147
+ return;
148
+ }
149
+ const ctl = new AbortController();
150
+ setWorking(ctl);
151
+ const files = attached;
152
+ setDraft('');
153
+ setAttached([]);
154
+ // The address names the conversation from its first message on.
155
+ if (!latest.current.turns.length)
156
+ host.go(path({ kind: 'chat', id: latest.current.id }), { replace: true });
157
+ const asked = {
158
+ text: words || 'Here is a picture.',
159
+ attached: files,
160
+ kind: (how.kind ?? picked) || undefined,
161
+ options: { model: options.model === 'auto' ? undefined : options.model, aspect: options.aspect || undefined, count: options.count > 1 ? options.count : undefined },
162
+ };
163
+ try {
164
+ const end = await turn(latest.current, asked, {
165
+ wire,
166
+ brain: BRAIN,
167
+ writer: WRITER,
168
+ fleet: available.fleet,
169
+ models: available.models,
170
+ order: () => order.current,
171
+ show,
172
+ changed,
173
+ signal: ctl.signal,
174
+ });
175
+ persist(end, true);
176
+ }
177
+ finally {
178
+ setWorking(null);
179
+ }
180
+ };
181
+ // A starter chosen in Explore starts this conversation.
182
+ useEffect(() => {
183
+ const s = take();
184
+ if (s)
185
+ begin(s);
186
+ // eslint-disable-next-line react-hooks/exhaustive-deps
187
+ }, []);
188
+ const begin = (s) => {
189
+ setPicked(s.kind);
190
+ if (s.picture && !attached.length) {
191
+ setDraft(s.prompt);
192
+ attach();
193
+ return;
194
+ }
195
+ void send(s.prompt, { kind: s.kind });
196
+ };
197
+ const attach = () => {
198
+ const input = document.createElement('input');
199
+ input.type = 'file';
200
+ input.accept = 'image/*';
201
+ input.multiple = true;
202
+ input.onchange = () => void upload(Array.from(input.files ?? []));
203
+ input.click();
204
+ };
205
+ const upload = async (files) => {
206
+ const pics = files.filter((f) => f.type.startsWith('image/')).slice(0, 4);
207
+ if (!pics.length)
208
+ return;
209
+ setUploading((n) => n + pics.length);
210
+ for (const f of pics) {
211
+ try {
212
+ const name = f.name.replace(/[^\w.-]+/g, '-').slice(-80) || 'picture.png';
213
+ const key = await put(wire, `files/image/${latest.current.id}/attached/${Date.now().toString(36)}/${name}`, f);
214
+ const bmp = await createImageBitmap(f).catch(() => null);
215
+ setAttached((a) => [...a, { key, mime: f.type, name, width: bmp?.width, height: bmp?.height }]);
216
+ bmp?.close();
217
+ }
218
+ catch {
219
+ // The chip never appears; the person sees the picture did not attach.
220
+ }
221
+ finally {
222
+ setUploading((n) => n - 1);
223
+ }
224
+ }
225
+ };
226
+ const pick = (m, at) => changed({ ...latest.current, made: latest.current.made.map((x) => (x.id === m.id ? { ...x, at } : x)) });
227
+ const editOne = async (m, patch, note) => {
228
+ setEditing((e) => ({ ...e, [m.id]: true }));
229
+ setEditFailed((e) => ({ ...e, [m.id]: '' }));
230
+ try {
231
+ const now = latest.current.made.find((x) => x.id === m.id) ?? m;
232
+ await editInPlace(latest.current, now, patch, note, {
233
+ wire,
234
+ writer: WRITER,
235
+ fleet: available.fleet,
236
+ models: available.models,
237
+ order: () => order.current,
238
+ changed,
239
+ signal: new AbortController().signal,
240
+ });
241
+ }
242
+ catch (e) {
243
+ setEditFailed((x) => ({ ...x, [m.id]: e instanceof Error ? e.message : String(e) }));
244
+ }
245
+ finally {
246
+ setEditing((x) => ({ ...x, [m.id]: false }));
247
+ }
248
+ };
249
+ const tabs = useMemo(() => chat.made.map((m) => {
250
+ const Icon = kind(m.kind).icon;
251
+ return {
252
+ id: m.id,
253
+ label: m.title,
254
+ icon: _jsx(Icon, { size: 14 }),
255
+ bleed: true,
256
+ render: () => (_jsx(Desk, { made: chat.made.find((x) => x.id === m.id) ?? m, wire: wire, others: chat.made.filter((x) => x.id !== m.id), onPick: (at) => pick(m, at), onEdit: (patch, note) => void editOne(m, patch, note), busy: Boolean(editing[m.id]), failed: editFailed[m.id] })),
257
+ };
258
+ }),
259
+ // eslint-disable-next-line react-hooks/exhaustive-deps
260
+ [chat.made, wire, editing, editFailed]);
261
+ const messages = chat.turns.map((t) => ({ id: t.id, role: t.role, content: t.text }));
262
+ const busy = Boolean(working);
263
+ const empty = !chat.turns.length;
264
+ const body = (said) => {
265
+ const t = chat.turns.find((x) => x.id === said.id);
266
+ if (!t)
267
+ return null;
268
+ if (t.role === 'user') {
269
+ return (_jsxs(YStack, { gap: "$2", children: [t.attached?.length ? (_jsx(XStack, { gap: "$2", flexWrap: "wrap", children: t.attached.map((f) => (_jsx(Thumb, { wire: wire, file: f }, f.key))) })) : null, _jsx(SizableText, { size: "$4", whiteSpace: "pre-wrap", children: t.text })] }));
270
+ }
271
+ const made = (t.made ?? []).map((mid) => chat.made.find((m) => m.id === mid)).filter((m) => Boolean(m));
272
+ return (_jsxs(YStack, { gap: "$2", children: [t.text ? (_jsx(SizableText, { size: "$4", whiteSpace: "pre-wrap", children: t.text })) : null, made.length ? (_jsx(XStack, { gap: "$2", flexWrap: "wrap", children: made.map((m) => (_jsx(Chip, { made: m, wire: wire, n: order.current.indexOf(m.id) + 1, onPress: () => show(m.id) }, m.id))) })) : null, t.doing ? (_jsxs(XStack, { items: "center", gap: "$2", "data-slot": "doing", children: [_jsx(LoaderCircle, { size: 14 }), _jsxs(SizableText, { size: "$2", color: "$soft", children: [t.doing, "\u2026"] })] })) : null, t.failed ? _jsx(Failure, { onRetry: busy ? undefined : () => void retry(t.id), children: t.failed }) : null, t.text && !t.doing ? _jsx(Reply, { text: t.text }) : null] }));
273
+ };
274
+ const retry = (assistant) => {
275
+ const i = chat.turns.findIndex((t) => t.id === assistant);
276
+ const q = chat.turns
277
+ .slice(0, i)
278
+ .reverse()
279
+ .find((t) => t.role === 'user');
280
+ if (q)
281
+ void send(q.text);
282
+ };
283
+ const kindItems = [
284
+ { key: 'auto', label: 'Auto', description: 'Studio picks from what you ask', onSelect: () => setPicked(null) },
285
+ ...ALL.map((k) => {
286
+ const why = k.needs(available);
287
+ return { key: k.id, label: k.label, description: why ? 'Needs a GPU' : k.blurb, disabled: Boolean(why), onSelect: () => setPicked(k.id) };
288
+ }),
289
+ ];
290
+ const brandName = brandOf(chat);
291
+ const brandItems = [
292
+ { type: 'label', key: 'h', label: 'Draw in' },
293
+ { key: 'none', label: 'Hanzo (default)', onSelect: () => changed({ ...latest.current, brand: undefined }) },
294
+ ...chat.made
295
+ .filter((m) => m.kind === 'type')
296
+ .map((m) => ({ key: m.id, label: m.title, description: 'From this conversation', onSelect: () => changed({ ...latest.current, brand: m.id }) })),
297
+ { type: 'separator', key: 's' },
298
+ ...PRESETS.map((p) => ({
299
+ key: `preset-${p.name}`,
300
+ label: p.name,
301
+ description: p.blurb,
302
+ onSelect: () => {
303
+ const m = { id: `type${Date.now().toString(36)}`, kind: 'type', title: p.name, at: 0, versions: [{ id: `v${Date.now().toString(36)}`, at: Date.now(), spec: p, files: [], note: `The ${p.name} preset`, by: 'preset' }] };
304
+ changed({ ...latest.current, made: [...latest.current.made, m], brand: m.id });
305
+ show(m.id);
306
+ },
307
+ })),
308
+ { type: 'separator', key: 's2' },
309
+ { key: 'make', label: 'Make a type system…', onSelect: () => setDraft('A type system for ') },
310
+ ];
311
+ const zen = lit(available.fleet, 'image.zen');
312
+ const optionItems = [
313
+ { type: 'label', key: 'm', label: 'Image model' },
314
+ { key: 'auto', label: `${options.model === 'auto' ? '✓ ' : ''}Auto`, description: zen ? 'Zen Image on your GPU' : MODELS[0].label, onSelect: () => setOptions({ ...options, model: 'auto' }) },
315
+ ...(zen ? [{ key: ZEN, label: `${options.model === ZEN ? '✓ ' : ''}Zen Image`, description: 'On your GPU', onSelect: () => setOptions({ ...options, model: ZEN }) }] : []),
316
+ ...MODELS.map((m) => ({ key: m.id, label: `${options.model === m.id ? '✓ ' : ''}${m.label}`, description: m.id, onSelect: () => setOptions({ ...options, model: m.id }) })),
317
+ { type: 'separator', key: 's' },
318
+ { type: 'label', key: 'a', label: 'Aspect' },
319
+ ...['', '1:1', '4:5', '9:16', '16:9', '3:2'].map((a) => ({ key: `a${a}`, label: `${options.aspect === a ? '✓ ' : ''}${a || 'As asked'}`, onSelect: () => setOptions({ ...options, aspect: a }) })),
320
+ { type: 'separator', key: 's2' },
321
+ { type: 'label', key: 'c', label: 'How many' },
322
+ ...[1, 2, 4].map((n) => ({ key: `c${n}`, label: `${options.count === n ? '✓ ' : ''}${n}`, onSelect: () => setOptions({ ...options, count: n }) })),
323
+ ];
324
+ const tools = (_jsxs(XStack, { items: "center", gap: 2, children: [_jsx(DropdownMenu, { trigger: _jsx(Pillish, { label: "Kind", icon: _jsx(Sparkles, { size: 13 }), value: picked ? kind(picked).label : 'Auto' }), items: kindItems }), _jsx(DropdownMenu, { trigger: _jsx(Pillish, { label: "Brand", icon: _jsx(Palette, { size: 13 }), value: brandName }), items: brandItems }), _jsx(DropdownMenu, { trigger: _jsx(XStack, { render: "button", "aria-label": "More options: model, aspect, count", width: 32, height: 32, items: "center", justify: "center", rounded: 999, cursor: "pointer", hoverStyle: { bg: '$hover' }, children: _jsx(Ellipsis, { size: 16 }) }), items: optionItems })] }));
325
+ const head = attached.length || uploading ? (_jsxs(XStack, { gap: "$2", flexWrap: "wrap", "data-slot": "attached", children: [attached.map((f) => (_jsxs(XStack, { position: "relative", children: [_jsx(Thumb, { wire: wire, file: f }), _jsx(XStack, { render: "button", "aria-label": `Remove ${f.name}`, onPress: () => setAttached((a) => a.filter((x) => x.key !== f.key)), position: "absolute", t: -6, r: -6, width: 20, height: 20, rounded: 99, bg: "$background", borderWidth: 1, borderColor: "$borderColor", items: "center", justify: "center", cursor: "pointer", children: _jsx(X, { size: 11 }) })] }, f.key))), uploading ? (_jsx(XStack, { width: 56, height: 56, rounded: "$3", bg: "$hover", items: "center", justify: "center", children: _jsx(LoaderCircle, { size: 16 }) })) : null] })) : undefined;
326
+ const composer = prompt({
327
+ value: draft,
328
+ onChange: setDraft,
329
+ onSend: () => void send(draft),
330
+ onStop: working ? () => working.abort() : undefined,
331
+ busy,
332
+ ready: Boolean(draft.trim() || attached.length),
333
+ placeholder: empty ? 'What should we create?' : 'Ask for a change, or something new',
334
+ label: 'What should we create?',
335
+ head,
336
+ onAttach: attach,
337
+ tools,
338
+ mind: null,
339
+ narrow: !wide,
340
+ note: imaging ? `Image models are not answering (${imaging.reason}). Posters and ads are set in type until they are back.` : shelf === false ? KEPT : undefined,
341
+ });
342
+ const deskPane = (sheet) => (_jsx(Panel, { side: deck, kinds: tabs, onHide: () => setDesk(false), sheet: sheet, label: "What this conversation made" }));
343
+ return (_jsxs(XStack, { "data-slot": "studio-split", flex: 1, minH: 0, minW: 0, width: "100%", children: [_jsxs(YStack, { "data-slot": "studio-talk", ...pane, flex: 1, minH: 0, minW: 0, width: "100%", overflow: "hidden", children: [_jsxs(XStack, { px: "$4", pt: "$3", pb: "$2", gap: "$2", items: "center", minH: 44, children: [_jsx(SizableText, { render: "h1", size: "$5", color: "$ink", numberOfLines: 1, flex: 1, minW: 0, children: empty ? 'Studio' : chat.title }), chat.made.length ? _jsx(PanelToggle, { open: desk, onToggle: () => setDesk(!desk) }) : null] }), _jsx(Thread, { messages: messages, streaming: busy, send: (text) => void send(text), stop: working ? () => working.abort() : undefined, body: body, center: true, onFiles: (files) => void upload(files), empty: _jsx(Opening, {}), composer: { ...composer, foot: empty ? _jsx(Home, { available: available, onStart: begin }) : undefined } })] }), desk && chat.made.length && !wide ? deskPane(true) : null, desk && chat.made.length && wide ? (_jsxs(YStack, { "data-slot": "studio-desk", ref: (el) => {
344
+ deskBox.current = el instanceof HTMLElement ? el : null;
345
+ }, ...pane, ml: gap, position: "relative", shrink: 0, minH: 0, overflow: "hidden", style: { width: span, maxWidth: `calc(100% - ${TALK_FLOOR}px)`, minWidth: DESK_FLOOR }, children: [_jsx(Grip, { side: "right", span: span, floor: DESK_FLOOR, ceil: 1400, reset: DESK, onSpan: (n) => deskBox.current?.style.setProperty('width', `${n}px`), onKeep: setSpan, onShut: () => setDesk(false), label: "Resize the tabs", l: `calc(-1 * ${gap} - 1px)`, width: gap }), deskPane(false)] })) : null] }));
346
+ }
347
+ function brandOf(chat) {
348
+ const m = chat.brand ? chat.made.find((x) => x.id === chat.brand) : null;
349
+ return m ? m.title : 'Hanzo';
350
+ }
351
+ function Opening() {
352
+ return (_jsxs(YStack, { items: "center", width: "100%", pt: "$6", pb: "$2", gap: "$2", children: [_jsx(SizableText, { render: "h2", size: "$9", fontWeight: "600", color: "$ink", text: "center", children: "What should we create?" }), _jsx(SizableText, { size: "$3", color: "$soft", text: "center", children: "Images, posters, documents, slides, type systems, 3D, video and sound. Everything opens in a tab." })] }));
353
+ }
354
+ /** A composer chip: what it sets, and its value. */
355
+ function Pillish({ label, icon, value }) {
356
+ return (_jsxs(XStack, { render: "button", "aria-label": `${label}: ${value}`, items: "center", gap: "$1.5", height: 32, px: "$2.5", rounded: 999, borderWidth: 1, borderColor: "$borderColor", cursor: "pointer", hoverStyle: { bg: '$hover' }, children: [icon, _jsx(SizableText, { size: "$2", numberOfLines: 1, maxW: 120, children: value }), _jsx(ChevronDown, { size: 12 })] }));
357
+ }
358
+ /** What a turn made, as a chip that opens its tab. */
359
+ function Chip({ made, wire, n, onPress }) {
360
+ const k = kind(made.kind);
361
+ const Icon = k.icon;
362
+ const pic = current(made).files.find((f) => f.mime.startsWith('image/'));
363
+ const { src } = useUrl(wire, pic?.key);
364
+ return (_jsxs(XStack, { render: "button", "aria-label": `Open tab ${n}: ${made.title}`, onPress: onPress, items: "center", gap: "$2", p: "$1.5", pr: "$3", rounded: "$4", borderWidth: 1, borderColor: "$borderColor", cursor: "pointer", hoverStyle: { bg: '$hover' }, maxW: 280, children: [src ? (_jsx("img", { src: src, alt: "", width: 40, height: 40, style: { borderRadius: 8, objectFit: 'cover' } })) : (_jsx(XStack, { width: 40, height: 40, rounded: "$3", bg: "$hover", items: "center", justify: "center", children: _jsx(Icon, { size: 16 }) })), _jsxs(YStack, { minW: 0, children: [_jsx(SizableText, { size: "$2", numberOfLines: 1, children: made.title }), _jsx(SizableText, { size: "$1", color: "$soft", children: `Tab ${n} · ${k.label} · v${made.at + 1}` })] }), _jsx(Check, { size: 13 })] }));
365
+ }
366
+ function Thumb({ wire, file }) {
367
+ const { src } = useUrl(wire, file.key);
368
+ return src ? (_jsx("img", { src: src, alt: file.name, width: 56, height: 56, style: { borderRadius: 8, objectFit: 'cover' } })) : (_jsx(XStack, { width: 56, height: 56, rounded: "$3", bg: "$hover", items: "center", justify: "center", children: _jsx(ImageIcon, { size: 16 }) }));
369
+ }
package/lib/ui.d.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { ReactNode } from 'react';
2
+ export interface Option {
3
+ value: string;
4
+ label?: string;
5
+ }
6
+ export declare function Choice({ value, options, onChange, label }: {
7
+ value: string;
8
+ options: readonly Option[];
9
+ onChange: (v: string) => void;
10
+ label: string;
11
+ }): import("react").JSX.Element;
12
+ export declare function Text1({ value, onChange, label, placeholder }: {
13
+ value: string;
14
+ onChange: (v: string) => void;
15
+ label: string;
16
+ placeholder?: string;
17
+ }): import("react").JSX.Element;
18
+ export declare function TextN({ value, onChange, label, rows }: {
19
+ value: string;
20
+ onChange: (v: string) => void;
21
+ label: string;
22
+ rows?: number;
23
+ }): import("react").JSX.Element;
24
+ export declare function Color({ value, onChange, label }: {
25
+ value: string;
26
+ onChange: (v: string) => void;
27
+ label: string;
28
+ }): import("react").JSX.Element;
29
+ /** A label above a control. */
30
+ export declare function Row({ label, children }: {
31
+ label: string;
32
+ children: ReactNode;
33
+ }): import("react").JSX.Element;
34
+ /** A pill that is on or off: a category, a filter. */
35
+ export declare function Pill({ on, onPress, children, label }: {
36
+ on: boolean;
37
+ onPress: () => void;
38
+ children: ReactNode;
39
+ label?: string;
40
+ }): import("react").JSX.Element;
41
+ /** A small press with an icon and a word: the tab bar's controls. */
42
+ export declare function Act({ onPress, children, label, on, disabled }: {
43
+ onPress: () => void;
44
+ children: ReactNode;
45
+ label: string;
46
+ on?: boolean;
47
+ disabled?: boolean;
48
+ }): import("react").JSX.Element;
49
+ /** Save bytes as a file the person keeps. */
50
+ export declare function download(name: string, blob: Blob): void;
package/lib/ui.js ADDED
@@ -0,0 +1,57 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * The few controls Studio draws itself, on gui tokens: a choice from a list
4
+ * (the browser's own <select>), a colour, a line or a paragraph of text, a pill
5
+ * that is on or off, and a quiet labelled row. Everything larger is @hanzo/ui's
6
+ * or @hanzo/build's.
7
+ */
8
+ import { SizableText, styled, XStack, YStack } from '@hanzo/gui';
9
+ const field = {
10
+ size: '$2',
11
+ minW: 0,
12
+ rounded: '$3',
13
+ borderWidth: 1,
14
+ borderColor: '$borderColor',
15
+ bg: 'transparent',
16
+ color: '$ink',
17
+ focusVisibleStyle: { outlineWidth: 2, outlineStyle: 'solid', outlineColor: '$outlineColor' },
18
+ };
19
+ const Select = styled(SizableText, { name: 'StudioChoice', render: 'select', height: 32, px: '$2', cursor: 'pointer', ...field });
20
+ const Line = styled(SizableText, { name: 'StudioLine', render: 'input', height: 32, px: '$2.5', ...field });
21
+ const Lines = styled(SizableText, { name: 'StudioLines', render: 'textarea', p: '$2.5', minH: 72, ...field });
22
+ export function Choice({ value, options, onChange, label }) {
23
+ const all = value === '' || options.some((o) => o.value === value) ? options : [{ value }, ...options];
24
+ return (_jsx(Select, { ...{ value }, onChange: (e) => onChange(e.currentTarget.value), "aria-label": label, children: all.map((o) => (_jsx("option", { value: o.value, children: o.label ?? o.value }, o.value))) }));
25
+ }
26
+ export function Text1({ value, onChange, label, placeholder }) {
27
+ return (_jsx(Line, { ...{ value, placeholder }, onChange: (e) => onChange(e.currentTarget.value), "aria-label": label }));
28
+ }
29
+ export function TextN({ value, onChange, label, rows = 3 }) {
30
+ return _jsx(Lines, { ...{ value, rows }, onChange: (e) => onChange(e.currentTarget.value), "aria-label": label });
31
+ }
32
+ export function Color({ value, onChange, label }) {
33
+ return (_jsx("input", { type: "color", value: value, "aria-label": label, onChange: (e) => onChange(e.currentTarget.value), style: { width: 32, height: 32, padding: 0, border: '1px solid var(--borderColor)', borderRadius: 8, background: 'transparent', cursor: 'pointer' } }));
34
+ }
35
+ /** A label above a control. */
36
+ export function Row({ label, children }) {
37
+ return (_jsxs(YStack, { gap: "$1", minW: 0, children: [_jsx(SizableText, { size: "$1", color: "$soft", children: label }), children] }));
38
+ }
39
+ /** A pill that is on or off: a category, a filter. */
40
+ export function Pill({ on, onPress, children, label }) {
41
+ return (_jsx(XStack, { render: "button", role: "button", "aria-pressed": on, "aria-label": label, onPress: onPress, items: "center", gap: "$1.5", height: 32, px: "$3", rounded: 999, borderWidth: 1, borderColor: on ? '$ink' : '$borderColor', bg: on ? '$ink' : 'transparent', cursor: "pointer", hoverStyle: { bg: on ? '$ink' : '$hover' }, focusVisibleStyle: { outlineWidth: 2, outlineStyle: 'solid', outlineColor: '$outlineColor' }, shrink: 0, children: _jsx(SizableText, { size: "$2", color: on ? '$background' : '$ink', children: children }) }));
42
+ }
43
+ /** A small press with an icon and a word: the tab bar's controls. */
44
+ export function Act({ onPress, children, label, on = false, disabled = false }) {
45
+ return (_jsx(XStack, { render: "button", role: "button", "aria-label": label, "aria-pressed": on || undefined, disabled: disabled, onPress: disabled ? undefined : onPress, items: "center", gap: "$1.5", height: 30, px: "$2.5", rounded: "$3", bg: on ? '$hover' : 'transparent', opacity: disabled ? 0.4 : 1, cursor: disabled ? 'default' : 'pointer', hoverStyle: disabled ? undefined : { bg: '$hover' }, focusVisibleStyle: { outlineWidth: 2, outlineStyle: 'solid', outlineColor: '$outlineColor' }, shrink: 0, children: children }));
46
+ }
47
+ /** Save bytes as a file the person keeps. */
48
+ export function download(name, blob) {
49
+ const url = URL.createObjectURL(blob);
50
+ const a = document.createElement('a');
51
+ a.href = url;
52
+ a.download = name;
53
+ document.body.appendChild(a);
54
+ a.click();
55
+ a.remove();
56
+ setTimeout(() => URL.revokeObjectURL(url), 10_000);
57
+ }
package/lib/use.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { type Wire } from './wire.ts';
2
+ /** A stored file's read address, minted when drawn; '' until it is, and on failure. */
3
+ export declare function useUrl(w: Wire, key: string | undefined): {
4
+ src: string;
5
+ error: string;
6
+ };
7
+ /** A stored file's text, read when drawn. */
8
+ export declare function useText(w: Wire, key: string | undefined): {
9
+ text: string | null;
10
+ error: string;
11
+ };
package/lib/use.js ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Small hooks every tab uses: a stored file's address, and its text.
3
+ */
4
+ import { useEffect, useState } from 'react';
5
+ import { read, url } from './wire.js';
6
+ /** A stored file's read address, minted when drawn; '' until it is, and on failure. */
7
+ export function useUrl(w, key) {
8
+ const [state, setState] = useState({ src: '', error: '' });
9
+ useEffect(() => {
10
+ if (!key)
11
+ return;
12
+ let live = true;
13
+ url(w, key).then((src) => live && setState({ src, error: '' }), (e) => live && setState({ src: '', error: e instanceof Error ? e.message : String(e) }));
14
+ return () => {
15
+ live = false;
16
+ };
17
+ }, [w, key]);
18
+ return state;
19
+ }
20
+ /** A stored file's text, read when drawn. */
21
+ export function useText(w, key) {
22
+ const [state, setState] = useState({ text: null, error: '' });
23
+ useEffect(() => {
24
+ if (!key)
25
+ return;
26
+ let live = true;
27
+ read(w, key)
28
+ .then((b) => b.text())
29
+ .then((text) => live && setState({ text, error: '' }), (e) => live && setState({ text: null, error: e instanceof Error ? e.message : String(e) }));
30
+ return () => {
31
+ live = false;
32
+ };
33
+ }, [w, key]);
34
+ return state;
35
+ }