@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
@@ -0,0 +1,47 @@
1
+ import { type Stored } from '../made.ts';
2
+ import { type Fleet, type Wire } from '../wire.ts';
3
+ import { type Kind, type Work } from './kind.ts';
4
+ /** The gateway's image models, best first. */
5
+ export declare const MODELS: readonly [{
6
+ readonly id: 'google/gemini-3.1-flash-image';
7
+ readonly label: 'Nano Banana 2';
8
+ }, {
9
+ readonly id: 'google/gemini-3-pro-image';
10
+ readonly label: 'Nano Banana Pro';
11
+ }, {
12
+ readonly id: 'openai/gpt-5.4-image-2';
13
+ readonly label: 'GPT Image 2';
14
+ }, {
15
+ readonly id: 'openai/gpt-5-image-mini';
16
+ readonly label: 'GPT Image Mini';
17
+ }, {
18
+ readonly id: 'google/gemini-2.5-flash-image';
19
+ readonly label: 'Nano Banana';
20
+ }];
21
+ export declare const ZEN = "zen-image";
22
+ export declare const ASPECTS: readonly ['1:1', '4:5', '3:4', '2:3', '9:16', '16:9', '3:2', '4:3', '21:9'];
23
+ /** Which lane a request takes: the one asked for, else Zen Image on a GPU that can run it, else the gateway. */
24
+ export declare function lane(model: string, fleet: Fleet | null): string;
25
+ /** Why the gateway's image models are not answering now, or null. */
26
+ export declare function useImageLane(): {
27
+ model: string;
28
+ reason: string;
29
+ } | null;
30
+ /** Make pictures from words (and, for an edit, the pictures they start from), and keep them. */
31
+ export declare function picture(w: Work, o: {
32
+ prompt: string;
33
+ aspect: string;
34
+ model: string;
35
+ from?: Stored[];
36
+ count?: number;
37
+ }): Promise<{
38
+ files: Stored[];
39
+ by: string;
40
+ }>;
41
+ export declare const image: Kind;
42
+ export declare function Shot({ wire, file, alt, max }: {
43
+ wire: Wire;
44
+ file: Stored;
45
+ alt: string;
46
+ max: string;
47
+ }): import("react").JSX.Element;
@@ -0,0 +1,179 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * Images: made from words, or edited from a picture with words.
4
+ *
5
+ * Two lanes, chosen per request: the gateway's image models (api.hanzo.ai,
6
+ * billed to the org) and Zen Image on the org's own GPU (Z-Image-Turbo through
7
+ * the engine), which is offered only while a GPU advertises `image.zen`. Auto
8
+ * takes the GPU when one can run it, else the gateway.
9
+ */
10
+ import { SizableText, XStack, YStack } from '@hanzo/gui';
11
+ import { Image as Picture } from '@hanzogui/lucide-icons-2';
12
+ import { useState, useSyncExternalStore } from 'react';
13
+ import { render } from '../gpu.js';
14
+ import { current, filename } from '../made.js';
15
+ import { useUrl } from '../use.js';
16
+ import { draw, read, Refusal } from '../wire.js';
17
+ import { lit, num, str } from './kind.js';
18
+ /** The gateway's image models, best first. */
19
+ export const MODELS = [
20
+ { id: 'google/gemini-3.1-flash-image', label: 'Nano Banana 2' },
21
+ { id: 'google/gemini-3-pro-image', label: 'Nano Banana Pro' },
22
+ { id: 'openai/gpt-5.4-image-2', label: 'GPT Image 2' },
23
+ { id: 'openai/gpt-5-image-mini', label: 'GPT Image Mini' },
24
+ { id: 'google/gemini-2.5-flash-image', label: 'Nano Banana' },
25
+ ];
26
+ export const ZEN = 'zen-image';
27
+ export const ASPECTS = ['1:1', '4:5', '3:4', '2:3', '9:16', '16:9', '3:2', '4:3', '21:9'];
28
+ /** Pixel sizes for the GPU lane, on its 64-pixel grid. */
29
+ const PIXELS = {
30
+ '1:1': [1024, 1024],
31
+ '4:5': [896, 1120],
32
+ '3:4': [896, 1152],
33
+ '2:3': [832, 1248],
34
+ '9:16': [768, 1344],
35
+ '16:9': [1344, 768],
36
+ '3:2': [1248, 832],
37
+ '4:3': [1152, 896],
38
+ '21:9': [1536, 640],
39
+ };
40
+ const ext = (mime) => (mime === 'image/jpeg' ? 'jpg' : mime === 'image/webp' ? 'webp' : 'png');
41
+ /** Which lane a request takes: the one asked for, else Zen Image on a GPU that can run it, else the gateway. */
42
+ export function lane(model, fleet) {
43
+ if (model === ZEN || MODELS.some((m) => m.id === model))
44
+ return model;
45
+ return lit(fleet, 'image.zen') ? ZEN : MODELS[0].id;
46
+ }
47
+ async function size(blob) {
48
+ try {
49
+ const bmp = await createImageBitmap(blob);
50
+ const out = { width: bmp.width, height: bmp.height };
51
+ bmp.close();
52
+ return out;
53
+ }
54
+ catch {
55
+ return {};
56
+ }
57
+ }
58
+ /*
59
+ * Whether the gateway's image lane is answering, as the last call found it: a lane
60
+ * that refused at the source says why, in the composer, until a call succeeds.
61
+ */
62
+ let down = null;
63
+ const watchers = new Set();
64
+ const note = (next) => {
65
+ down = next;
66
+ watchers.forEach((w) => w());
67
+ };
68
+ /** Why the gateway's image models are not answering now, or null. */
69
+ export function useImageLane() {
70
+ return useSyncExternalStore((l) => {
71
+ watchers.add(l);
72
+ return () => watchers.delete(l);
73
+ }, () => down, () => down);
74
+ }
75
+ /** Make pictures from words (and, for an edit, the pictures they start from), and keep them. */
76
+ export async function picture(w, o) {
77
+ const use = lane(o.model, w.fleet);
78
+ const files = [];
79
+ if (use === ZEN) {
80
+ if (o.from?.length)
81
+ throw new Error('Zen Image draws from words only; edit with a gateway model.');
82
+ const [width, height] = PIXELS[o.aspect] ?? PIXELS['1:1'];
83
+ for (let i = 0; i < (o.count ?? 1); i++) {
84
+ const got = await render(w.wire, 'image', { prompt: o.prompt, width, height }, w.doing, w.signal);
85
+ for (const g of got)
86
+ files.push(await w.keep(`image-${files.length + 1}.png`, g.blob, await size(g.blob)));
87
+ }
88
+ return { files, by: 'Zen Image (your GPU)' };
89
+ }
90
+ const from = await Promise.all((o.from ?? []).map((f) => w.picture(f)));
91
+ const ask = from.length ? o.prompt : `${o.prompt}\n\nAspect ratio ${o.aspect}.`;
92
+ for (let i = 0; i < (o.count ?? 1); i++) {
93
+ w.doing(from.length ? 'Editing the picture' : `Drawing${(o.count ?? 1) > 1 ? ` ${i + 1} of ${o.count}` : ''}`);
94
+ let got;
95
+ try {
96
+ got = await draw(w.wire, use, ask, { from, aspect: o.aspect, signal: w.signal });
97
+ }
98
+ catch (e) {
99
+ if (e instanceof Refusal && (e.status >= 500 || e.status === 402))
100
+ note({ model: use, reason: e.message });
101
+ throw e;
102
+ }
103
+ note(null);
104
+ for (const g of got)
105
+ files.push(await w.keep(`image-${files.length + 1}.${ext(g.mime)}`, g.blob, await size(g.blob)));
106
+ }
107
+ return { files, by: MODELS.find((m) => m.id === use)?.label ?? use };
108
+ }
109
+ export const image = {
110
+ id: 'image',
111
+ label: 'Image',
112
+ blurb: 'Pictures from words, and edits of a picture in words.',
113
+ icon: Picture,
114
+ tool: {
115
+ name: 'make_image',
116
+ description: 'Make a new image from a description: a photo, an illustration, a product shot, a concept. For changing an image already in a tab, call revise instead. For an image with words laid on it (a poster, a flyer, an ad), call make_poster or make_ad.',
117
+ parameters: {
118
+ type: 'object',
119
+ properties: {
120
+ prompt: { type: 'string', description: 'A full visual description: subject, setting, light, lens, style, mood.' },
121
+ title: { type: 'string', description: 'A short name for the tab, two to four words.' },
122
+ aspect: { type: 'string', enum: ASPECTS },
123
+ count: { type: 'integer', minimum: 1, maximum: 4, description: 'How many variations, 1 unless asked.' },
124
+ from_tab: { type: 'integer', description: 'A tab whose picture this one starts from (a variation or a restyle).' },
125
+ },
126
+ required: ['prompt', 'title'],
127
+ },
128
+ },
129
+ needs: () => null,
130
+ make: async (w, args) => {
131
+ const prompt = str(args.prompt);
132
+ const aspect = ASPECTS.includes(str(args.aspect)) ? str(args.aspect) : '1:1';
133
+ const model = str(args.model);
134
+ const base = args.from_tab ? w.find(num(args.from_tab, 0)) : null;
135
+ const from = base ? current(base).files.filter((f) => f.mime.startsWith('image/')).slice(0, 1) : [];
136
+ const { files, by } = await picture(w, { prompt, aspect, model, from, count: num(args.count, 1, 1, 4) });
137
+ return { title: str(args.title, prompt.slice(0, 40)), draft: { spec: { prompt, aspect, model }, files, note: prompt, by } };
138
+ },
139
+ revise: async (w, made, change) => {
140
+ const now = current(made);
141
+ const spec = { ...now.spec, ...(change.patch ?? {}) };
142
+ const instruction = str(change.instruction) || str(spec.prompt);
143
+ const from = now.files.filter((f) => f.mime.startsWith('image/')).slice(0, 1);
144
+ // A new prompt typed in place redraws from words; an instruction edits the picture.
145
+ const words = change.patch && typeof change.patch.prompt === 'string' && !change.instruction;
146
+ const model = str(spec.model);
147
+ const { files, by } = await picture(w, {
148
+ prompt: words ? str(spec.prompt) : instruction,
149
+ aspect: str(spec.aspect, '1:1'),
150
+ model: lane(model, w.fleet) === ZEN && !words ? MODELS[0].id : model,
151
+ from: words ? [] : from,
152
+ });
153
+ return { spec: words ? spec : { ...spec, edit: instruction }, files, note: instruction, by };
154
+ },
155
+ View: Pictures,
156
+ exports: [
157
+ {
158
+ label: 'Image file',
159
+ run: async (w, m, v) => {
160
+ const f = v.files[0];
161
+ return { name: filename(m.title, f.name.split('.').pop() ?? 'png'), blob: await read(w, f.key) };
162
+ },
163
+ },
164
+ ],
165
+ };
166
+ function Pictures({ made, version, wire, edit, busy }) {
167
+ const pics = version.files.filter((f) => f.mime.startsWith('image/'));
168
+ const [words, setWords] = useState(String(version.spec.prompt ?? ''));
169
+ return (_jsxs(YStack, { flex: 1, minH: 0, children: [_jsx(YStack, { flex: 1, minH: 0, items: "center", justify: "center", p: "$3", gap: "$3", overflow: "scroll", children: pics.length > 1 ? (_jsx(XStack, { flexWrap: "wrap", gap: "$3", justify: "center", children: pics.map((f) => (_jsx(Shot, { wire: wire, file: f, alt: made.title, max: "45%" }, f.key))) })) : pics[0] ? (_jsx(Shot, { wire: wire, file: pics[0], alt: made.title, max: "100%" })) : (_jsx(SizableText, { color: "$soft", children: "No picture in this version." })) }), _jsxs(XStack, { gap: "$2", p: "$3", borderTopWidth: 1, borderColor: "$borderColor", items: "center", children: [_jsx("input", { value: words, onChange: (e) => setWords(e.currentTarget.value), onKeyDown: (e) => {
170
+ if (e.key === 'Enter' && words.trim())
171
+ edit({ prompt: words.trim() }, `Redrawn: ${words.trim()}`);
172
+ }, "aria-label": "The words this picture is drawn from", style: { flex: 1, minWidth: 0, height: 32, padding: '0 10px', borderRadius: 8, border: '1px solid var(--borderColor)', background: 'transparent', color: 'inherit', font: 'inherit' } }), _jsx(XStack, { render: "button", "aria-label": "Redraw from these words", onPress: () => words.trim() && edit({ prompt: words.trim() }, `Redrawn: ${words.trim()}`), disabled: busy, px: "$3", height: 32, items: "center", rounded: "$3", borderWidth: 1, borderColor: "$borderColor", cursor: "pointer", hoverStyle: { bg: '$hover' }, children: _jsx(SizableText, { size: "$2", children: busy ? 'Drawing…' : 'Redraw' }) })] })] }));
173
+ }
174
+ export function Shot({ wire, file, alt, max }) {
175
+ const { src, error } = useUrl(wire, file.key);
176
+ if (error)
177
+ return _jsx(SizableText, { color: "$soft", children: error });
178
+ return src ? (_jsx("img", { src: src, alt: alt, width: file.width, height: file.height, style: { maxWidth: max, maxHeight: '100%', height: 'auto', objectFit: 'contain', borderRadius: 12 } })) : (_jsx(YStack, { width: 240, height: 240, rounded: "$4", bg: "$hover" }));
179
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Every kind Studio makes, in the order the home and the pills list them.
3
+ */
4
+ import type { KindId } from '../made.ts';
5
+ import type { Kind } from './kind.ts';
6
+ export declare const ALL: readonly Kind[];
7
+ export declare const kind: (id: KindId) => Kind;
8
+ /** The kind a tool name calls. */
9
+ export declare const called: (tool: string) => Kind | null;
@@ -0,0 +1,12 @@
1
+ import { ad } from './ad.js';
2
+ import { document_, slides } from './document.js';
3
+ import { image } from './image.js';
4
+ import { mesh } from './mesh/index.js';
5
+ import { poster } from './poster/index.js';
6
+ import { music, voice } from './sound.js';
7
+ import { type } from './type/index.js';
8
+ import { video } from './video.js';
9
+ export const ALL = [image, poster, document_, slides, type, mesh, video, ad, voice, music];
10
+ export const kind = (id) => ALL.find((k) => k.id === id) ?? image;
11
+ /** The kind a tool name calls. */
12
+ export const called = (tool) => ALL.find((k) => k.tool.name === tool) ?? null;
@@ -0,0 +1,113 @@
1
+ /**
2
+ * A KIND is one thing Studio makes: an image, a poster, a type system, a deck.
3
+ * Each is the same five parts, so the chat, the tabs and the library treat
4
+ * every kind one way:
5
+ *
6
+ * tool what the chat calls to make one (a function the model may call)
7
+ * needs why it cannot run right now, or null: a GPU that is not online,
8
+ * a lane that is not served. The home shows only kinds that can run.
9
+ * make a first version from the tool's arguments
10
+ * revise a new version from the shown one and an instruction, or a patch
11
+ * to its spec (an edit in place)
12
+ * View the tab: one version drawn; `Edit` the controls an edit in place
13
+ * offers, beside it
14
+ * exports the files a person takes away
15
+ *
16
+ * A kind never touches the network itself. It is handed a `Work`, which stores
17
+ * what it makes under the version's own folder and reads what it needs, all
18
+ * through the one client module (wire.ts).
19
+ */
20
+ import type { ComponentType } from 'react';
21
+ import type { KindId, Made, Stored, Version } from '../made.ts';
22
+ import type { TypeSystem } from './type/system.ts';
23
+ import type { Fleet, Wire } from '../wire.ts';
24
+ /** A version as a kind produces it: the id and time are the conversation's to give. */
25
+ export type Draft = Omit<Version, 'id' | 'at'>;
26
+ export interface Work {
27
+ wire: Wire;
28
+ signal: AbortSignal;
29
+ /** Say what is happening now: drawn under the turn while it works. */
30
+ doing: (line: string) => void;
31
+ /** Keep bytes as one of this version's files. */
32
+ keep: (name: string, blob: Blob, extra?: {
33
+ width?: number;
34
+ height?: number;
35
+ }) => Promise<Stored>;
36
+ /** A stored picture as a data: URL, for a model to start from. */
37
+ picture: (f: Stored) => Promise<string>;
38
+ /** Another thing this conversation made, by its tab number (1-based) or id. */
39
+ find: (ref: number | string) => Made | null;
40
+ /** The type system things are drawn in, when one is chosen. */
41
+ brand: TypeSystem | null;
42
+ /** The org's GPUs and their lanes, as last read; null when unread. */
43
+ fleet: Fleet | null;
44
+ /** The gateway's models that make something other than words. */
45
+ models: readonly Served[];
46
+ /** The model the chat itself runs on, for a kind that writes words. */
47
+ writer: string;
48
+ }
49
+ /** A gateway model, as the catalog lists it: what it takes and what it makes. */
50
+ export interface Served {
51
+ id: string;
52
+ name: string;
53
+ inputs: readonly string[];
54
+ outputs: readonly string[];
55
+ }
56
+ /** What can run right now: the org's GPUs and the gateway's models. */
57
+ export interface Available {
58
+ fleet: Fleet | null;
59
+ models: readonly Served[];
60
+ }
61
+ /** The first gateway model that makes `output`. */
62
+ export declare const maker: (a: Pick<Available, 'models'>, output: string) => Served | null;
63
+ export interface Tool {
64
+ name: string;
65
+ description: string;
66
+ /** A JSON Schema for the arguments. */
67
+ parameters: object;
68
+ }
69
+ export interface ViewProps {
70
+ made: Made;
71
+ version: Version;
72
+ wire: Wire;
73
+ /** Make a new version from a patch to this one's spec: an edit in place. */
74
+ edit: (patch: Record<string, unknown>, note: string) => void;
75
+ busy: boolean;
76
+ /** Everything else this conversation made, in tab order: a poster takes its picture from one. */
77
+ others: readonly Made[];
78
+ }
79
+ export interface Export {
80
+ label: string;
81
+ /** The file's name, from the title. */
82
+ run: (wire: Wire, made: Made, version: Version) => Promise<{
83
+ name: string;
84
+ blob: Blob;
85
+ }>;
86
+ }
87
+ export interface Kind {
88
+ id: KindId;
89
+ label: string;
90
+ /** One line: what it makes. */
91
+ blurb: string;
92
+ icon: ComponentType<{
93
+ size?: number;
94
+ }>;
95
+ tool: Tool;
96
+ needs: (a: Available) => string | null;
97
+ make: (w: Work, args: Record<string, unknown>) => Promise<{
98
+ title: string;
99
+ draft: Draft;
100
+ }>;
101
+ revise: (w: Work, made: Made, change: {
102
+ instruction?: string;
103
+ patch?: Record<string, unknown>;
104
+ }) => Promise<Draft>;
105
+ View: ComponentType<ViewProps>;
106
+ exports: Export[];
107
+ }
108
+ /** Whether an online GPU of the org advertises `lane`. */
109
+ export declare function lit(fleet: Fleet | null, lane: string): boolean;
110
+ /** A string argument, trimmed, or `or`. */
111
+ export declare const str: (v: unknown, or?: string) => string;
112
+ /** A number argument within bounds, or `or`. */
113
+ export declare const num: (v: unknown, or: number, lo?: number, hi?: number) => number;
@@ -0,0 +1,13 @@
1
+ /** The first gateway model that makes `output`. */
2
+ export const maker = (a, output) => a.models.find((m) => m.outputs.includes(output)) ?? null;
3
+ /** Whether an online GPU of the org advertises `lane`. */
4
+ export function lit(fleet, lane) {
5
+ return Boolean(fleet?.nodes.some((n) => n.online !== false && n.lanes?.includes(lane)));
6
+ }
7
+ /** A string argument, trimmed, or `or`. */
8
+ export const str = (v, or = '') => (typeof v === 'string' && v.trim() ? v.trim() : or);
9
+ /** A number argument within bounds, or `or`. */
10
+ export const num = (v, or, lo = -Infinity, hi = Infinity) => {
11
+ const n = typeof v === 'number' ? v : typeof v === 'string' ? Number(v) : NaN;
12
+ return Number.isFinite(n) ? Math.min(hi, Math.max(lo, n)) : or;
13
+ };
@@ -0,0 +1,2 @@
1
+ import { type Kind } from '../kind.ts';
2
+ export declare const mesh: Kind;
@@ -0,0 +1,76 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /**
3
+ * 3D: a mesh from a picture, or from words (the image lane draws the picture
4
+ * first). It runs on the org's GPU, through the engine's 3D route, and only
5
+ * while a GPU advertises the `mesh` lane; until then the kind says it needs a
6
+ * GPU and the home does not offer it. The tab is a glTF viewer: orbit, zoom,
7
+ * and the .glb to take away.
8
+ */
9
+ import { SizableText, YStack } from '@hanzo/gui';
10
+ import { Box } from '@hanzogui/lucide-icons-2';
11
+ import { lazy, Suspense } from 'react';
12
+ import { render } from '../../gpu.js';
13
+ import { current, filename } from '../../made.js';
14
+ import { read, stage } from '../../wire.js';
15
+ import { picture } from '../image.js';
16
+ import { lit, num, str } from '../kind.js';
17
+ const Viewer = lazy(() => import('./viewer.js'));
18
+ export const mesh = {
19
+ id: 'mesh',
20
+ label: '3D',
21
+ blurb: 'A 3D model from a picture or from words, as glTF.',
22
+ icon: Box,
23
+ tool: {
24
+ name: 'make_3d',
25
+ description: 'Make a 3D model (a mesh, glTF/.glb) of one object: from a picture in a tab, or from a description (a picture of it is drawn first).',
26
+ parameters: {
27
+ type: 'object',
28
+ properties: {
29
+ title: { type: 'string', description: 'A short name for the tab.' },
30
+ prompt: { type: 'string', description: 'The object, alone on a plain background, three-quarter view.' },
31
+ image_tab: { type: 'integer', description: 'A tab whose picture to model.' },
32
+ },
33
+ required: ['title'],
34
+ },
35
+ },
36
+ needs: (a) => (lit(a.fleet, 'mesh') ? null : '3D runs on a GPU. Connect one with `hanzo link`, and it appears here when the GPU can run it.'),
37
+ make: async (w, args) => {
38
+ const why = mesh.needs(w);
39
+ if (why)
40
+ throw new Error(why);
41
+ const tab = num(args.image_tab, 0) ? w.find(num(args.image_tab, 0)) : null;
42
+ let from = tab ? current(tab).files.find((f) => f.mime.startsWith('image/')) : undefined;
43
+ const prompt = str(args.prompt);
44
+ if (!from) {
45
+ if (!prompt)
46
+ throw new Error('Say what to model, or name a tab with a picture.');
47
+ const got = await picture(w, { prompt: `${prompt}, a single object centred on a plain light background, three-quarter view, soft even light`, aspect: '1:1', model: '' });
48
+ from = got.files[0];
49
+ }
50
+ if (!from)
51
+ throw new Error('No picture to model.');
52
+ const upload = await stage(w.wire, await read(w.wire, from.key), 'reference.png');
53
+ const got = await render(w.wire, 'model3d', { upload, prompt }, w.doing, w.signal);
54
+ const glb = got.find((g) => g.path.endsWith('.glb')) ?? got[0];
55
+ const file = await w.keep('model.glb', new Blob([glb.blob], { type: 'model/gltf-binary' }));
56
+ return { title: str(args.title, prompt.slice(0, 40) || 'Model'), draft: { spec: { prompt, picture: from.key }, files: [file, from], note: prompt || 'From a picture', by: 'your GPU' } };
57
+ },
58
+ revise: async (w, made, change) => {
59
+ const prompt = str(change.instruction) || str(current(made).spec.prompt);
60
+ const { draft } = await mesh.make(w, { title: made.title, prompt });
61
+ return draft;
62
+ },
63
+ View: Model,
64
+ exports: [
65
+ {
66
+ label: 'glTF binary (.glb)',
67
+ run: async (w, m, v) => ({ name: filename(m.title, 'glb'), blob: await read(w, v.files.find((f) => f.name.endsWith('.glb')).key) }),
68
+ },
69
+ ],
70
+ };
71
+ function Model({ version, wire, made }) {
72
+ const glb = version.files.find((f) => f.name.endsWith('.glb'));
73
+ if (!glb)
74
+ return _jsx(SizableText, { color: "$soft", children: "No model in this version." });
75
+ return (_jsx(YStack, { flex: 1, minH: 0, children: _jsx(Suspense, { fallback: _jsx(SizableText, { color: "$soft", children: "Opening the viewer\u2026" }), children: _jsx(Viewer, { wire: wire, file: glb, title: made.title }) }) }));
76
+ }
@@ -0,0 +1,7 @@
1
+ import type { Stored } from '../../made.ts';
2
+ import { type Wire } from '../../wire.ts';
3
+ export default function Viewer({ wire, file, title }: {
4
+ wire: Wire;
5
+ file: Stored;
6
+ title: string;
7
+ }): import("react").JSX.Element;
@@ -0,0 +1,79 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * The glTF viewer: three.js (MIT), its GLTFLoader and OrbitControls. Drag to
4
+ * orbit, scroll or pinch to zoom, right-drag to pan; the model is framed on
5
+ * load. Loaded only when a 3D tab is opened.
6
+ */
7
+ import { SizableText, YStack } from '@hanzo/gui';
8
+ import { useEffect, useRef, useState } from 'react';
9
+ import { AmbientLight, Box3, Color, DirectionalLight, PerspectiveCamera, Scene, Vector3, WebGLRenderer } from 'three';
10
+ import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
11
+ import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
12
+ import { read } from '../../wire.js';
13
+ export default function Viewer({ wire, file, title }) {
14
+ const box = useRef(null);
15
+ const [error, setError] = useState('');
16
+ useEffect(() => {
17
+ const host = box.current;
18
+ if (!host)
19
+ return;
20
+ let live = true;
21
+ const renderer = new WebGLRenderer({ antialias: true, alpha: true });
22
+ renderer.setPixelRatio(Math.min(2, window.devicePixelRatio));
23
+ host.appendChild(renderer.domElement);
24
+ renderer.domElement.setAttribute('aria-label', `${title}, a 3D model: drag to turn it, scroll to zoom`);
25
+ renderer.domElement.setAttribute('role', 'img');
26
+ const scene = new Scene();
27
+ scene.background = new Color(0x111114);
28
+ const camera = new PerspectiveCamera(40, 1, 0.01, 1000);
29
+ scene.add(new AmbientLight(0xffffff, 1.1));
30
+ const sun = new DirectionalLight(0xffffff, 2.2);
31
+ sun.position.set(3, 5, 4);
32
+ scene.add(sun);
33
+ const controls = new OrbitControls(camera, renderer.domElement);
34
+ controls.enableDamping = true;
35
+ const size = () => {
36
+ const w = host.clientWidth || 1;
37
+ const h = host.clientHeight || 1;
38
+ renderer.setSize(w, h, false);
39
+ camera.aspect = w / h;
40
+ camera.updateProjectionMatrix();
41
+ };
42
+ const watch = new ResizeObserver(size);
43
+ watch.observe(host);
44
+ size();
45
+ let frame = 0;
46
+ const tick = () => {
47
+ controls.update();
48
+ renderer.render(scene, camera);
49
+ frame = requestAnimationFrame(tick);
50
+ };
51
+ tick();
52
+ read(wire, file.key)
53
+ .then((b) => b.arrayBuffer())
54
+ .then((buf) => new GLTFLoader().parseAsync(buf, ''))
55
+ .then((gltf) => {
56
+ if (!live)
57
+ return;
58
+ scene.add(gltf.scene);
59
+ const bounds = new Box3().setFromObject(gltf.scene);
60
+ const centre = bounds.getCenter(new Vector3());
61
+ const span = bounds.getSize(new Vector3()).length() || 1;
62
+ controls.target.copy(centre);
63
+ camera.position.copy(centre).add(new Vector3(span * 0.6, span * 0.4, span * 0.9));
64
+ camera.near = span / 100;
65
+ camera.far = span * 100;
66
+ camera.updateProjectionMatrix();
67
+ })
68
+ .catch((e) => live && setError(e instanceof Error ? e.message : String(e)));
69
+ return () => {
70
+ live = false;
71
+ cancelAnimationFrame(frame);
72
+ watch.disconnect();
73
+ controls.dispose();
74
+ renderer.dispose();
75
+ renderer.domElement.remove();
76
+ };
77
+ }, [wire, file.key, title]);
78
+ return (_jsxs(YStack, { flex: 1, minH: 240, position: "relative", children: [_jsx("div", { ref: box, style: { position: 'absolute', inset: 0 } }), error ? (_jsx(YStack, { position: "absolute", inset: 0, items: "center", justify: "center", children: _jsx(SizableText, { color: "$soft", children: error }) })) : null] }));
79
+ }
@@ -0,0 +1,40 @@
1
+ import type { TypeSystem } from '../type/system.ts';
2
+ export interface Format {
3
+ id: string;
4
+ label: string;
5
+ /** Pixels drawn. */
6
+ width: number;
7
+ height: number;
8
+ /** The page, in points, for the PDF. */
9
+ points: [number, number];
10
+ }
11
+ export declare const FORMATS: readonly Format[];
12
+ export declare const format: (id: string) => Format;
13
+ /** The aspect a picture for this format is drawn at (the image lane's names). */
14
+ export declare function aspect(f: Format, layout: Layout): string;
15
+ export declare const LAYOUTS: readonly ['hero', 'center', 'split', 'frame', 'type'];
16
+ export type Layout = (typeof LAYOUTS)[number];
17
+ export declare const LAYOUT_NAMES: Record<Layout, string>;
18
+ export interface Words {
19
+ headline: string;
20
+ subhead: string;
21
+ /** Lines: a date, a place, a price. */
22
+ details: string;
23
+ cta: string;
24
+ }
25
+ export interface Poster extends Words {
26
+ format: string;
27
+ /** Pixels, for a size no format names (an ad unit). */
28
+ size?: {
29
+ width: number;
30
+ height: number;
31
+ };
32
+ layout: Layout;
33
+ /** A data: URL of the picture, or '' for none. */
34
+ picture: string;
35
+ system: TypeSystem;
36
+ }
37
+ /** Load the faces a poster draws in, so measuring and drawing use them. */
38
+ export declare function ready(t: TypeSystem): Promise<void>;
39
+ /** The poster as SVG, faces and picture inside it. */
40
+ export declare function compose(p: Poster): Promise<string>;