@hanzo/studio 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +29 -0
- package/NOTICE +68 -0
- package/lib/chat/store.d.ts +33 -0
- package/lib/chat/store.js +56 -0
- package/lib/chat/turn.d.ts +51 -0
- package/lib/chat/turn.js +274 -0
- package/lib/desk.d.ts +14 -0
- package/lib/desk.js +72 -0
- package/lib/gpu.d.ts +15 -0
- package/lib/gpu.js +41 -0
- package/lib/home.d.ts +6 -0
- package/lib/home.js +48 -0
- package/lib/host.d.ts +12 -0
- package/lib/host.js +18 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +11 -0
- package/lib/kinds/ad.d.ts +51 -0
- package/lib/kinds/ad.js +124 -0
- package/lib/kinds/document.d.ts +6 -0
- package/lib/kinds/document.js +271 -0
- package/lib/kinds/image.d.ts +47 -0
- package/lib/kinds/image.js +179 -0
- package/lib/kinds/index.d.ts +9 -0
- package/lib/kinds/index.js +12 -0
- package/lib/kinds/kind.d.ts +113 -0
- package/lib/kinds/kind.js +13 -0
- package/lib/kinds/mesh/index.d.ts +2 -0
- package/lib/kinds/mesh/index.js +76 -0
- package/lib/kinds/mesh/viewer.d.ts +7 -0
- package/lib/kinds/mesh/viewer.js +79 -0
- package/lib/kinds/poster/compose.d.ts +40 -0
- package/lib/kinds/poster/compose.js +289 -0
- package/lib/kinds/poster/index.d.ts +21 -0
- package/lib/kinds/poster/index.js +210 -0
- package/lib/kinds/sound.d.ts +6 -0
- package/lib/kinds/sound.js +100 -0
- package/lib/kinds/type/fonts.d.ts +30 -0
- package/lib/kinds/type/fonts.js +111 -0
- package/lib/kinds/type/index.d.ts +5 -0
- package/lib/kinds/type/index.js +117 -0
- package/lib/kinds/type/system.d.ts +164 -0
- package/lib/kinds/type/system.js +302 -0
- package/lib/kinds/video.d.ts +2 -0
- package/lib/kinds/video.js +81 -0
- package/lib/live.d.ts +44 -0
- package/lib/live.js +128 -0
- package/lib/made.d.ts +75 -0
- package/lib/made.js +36 -0
- package/lib/nav.d.ts +17 -0
- package/lib/nav.js +29 -0
- package/lib/pdf.d.ts +16 -0
- package/lib/pdf.js +53 -0
- package/lib/places.d.ts +6 -0
- package/lib/places.js +121 -0
- package/lib/raster.d.ts +7 -0
- package/lib/raster.js +53 -0
- package/lib/route.d.ts +33 -0
- package/lib/route.js +52 -0
- package/lib/starters.d.ts +31 -0
- package/lib/starters.js +62 -0
- package/lib/studio.d.ts +26 -0
- package/lib/studio.js +58 -0
- package/lib/talk.d.ts +3 -0
- package/lib/talk.js +369 -0
- package/lib/ui.d.ts +50 -0
- package/lib/ui.js +57 -0
- package/lib/use.d.ts +11 -0
- package/lib/use.js +35 -0
- package/lib/wire.d.ts +239 -0
- package/lib/wire.js +478 -0
- package/lib/zip.d.ts +9 -0
- package/lib/zip.js +60 -0
- package/package.json +75 -3
- package/README.md +0 -3
package/lib/wire.js
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE ONE CLIENT MODULE. Everything Studio asks of the network goes through
|
|
3
|
+
* here, so the transport is one change when the native workflow plane
|
|
4
|
+
* (`api.hanzo.ai/v1/studio/*`) answers.
|
|
5
|
+
*
|
|
6
|
+
* Three places are reached today:
|
|
7
|
+
*
|
|
8
|
+
* gateway api.hanzo.ai/v1: the models — the chat that decides what to make,
|
|
9
|
+
* image generation and edit, speech, music. Billed to `org`.
|
|
10
|
+
* store this browser's IndexedDB, per person and org, and the person's
|
|
11
|
+
* private Library (api.hanzo.ai/v1/library) once it answers.
|
|
12
|
+
* engine the studio engine: documents and decks, and the GPU lanes
|
|
13
|
+
* (Zen Image, Wan video, ACE music, mesh) dispatched to the org's
|
|
14
|
+
* GPUs. Same origin on studio.hanzo.ai; https://studio.hanzo.ai
|
|
15
|
+
* from anywhere else, with the bearer, never a cookie.
|
|
16
|
+
*
|
|
17
|
+
* A refusal is a `Refusal` carrying the status and the server's own sentence,
|
|
18
|
+
* whichever envelope it came in (problem details, `{error}`, `{status:'error',
|
|
19
|
+
* msg}`), and a 200 that says `status: "error"` is a refusal too.
|
|
20
|
+
*/
|
|
21
|
+
/** Where the engine is from a page that is not studio.hanzo.ai. */
|
|
22
|
+
export const ENGINE = 'https://studio.hanzo.ai';
|
|
23
|
+
export class Refusal extends Error {
|
|
24
|
+
status;
|
|
25
|
+
code;
|
|
26
|
+
constructor(status, message, code = '') {
|
|
27
|
+
super(message);
|
|
28
|
+
this.status = status;
|
|
29
|
+
this.code = code;
|
|
30
|
+
this.name = 'Refusal';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const origin = (w, base) => (base === 'api' ? w.api : w.engine);
|
|
34
|
+
function headers(w, extra) {
|
|
35
|
+
const h = new Headers(extra);
|
|
36
|
+
const t = w.token();
|
|
37
|
+
if (t)
|
|
38
|
+
h.set('Authorization', `Bearer ${t}`);
|
|
39
|
+
if (w.org)
|
|
40
|
+
h.set('X-Org-Id', w.org);
|
|
41
|
+
return h;
|
|
42
|
+
}
|
|
43
|
+
/** The sentence a refusal carries, and its code. */
|
|
44
|
+
async function reason(res) {
|
|
45
|
+
try {
|
|
46
|
+
const b = (await res.json());
|
|
47
|
+
const err = b.error;
|
|
48
|
+
const said = typeof err === 'string' ? err : err?.message;
|
|
49
|
+
const msg = [b.detail, said, b.msg, b.message, b.title].find((v) => typeof v === 'string' && v);
|
|
50
|
+
return [msg ?? '', String(b.code ?? (typeof err === 'object' ? err?.code : '') ?? '')];
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return ['', ''];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** A 200 in the `{status:'error', msg}` envelope is a refusal. */
|
|
57
|
+
function envelope(v) {
|
|
58
|
+
const r = v;
|
|
59
|
+
if (r && typeof r === 'object' && r.status === 'error') {
|
|
60
|
+
throw new Refusal(400, typeof r.msg === 'string' && r.msg ? r.msg : 'The platform refused that.', String(r.code ?? ''));
|
|
61
|
+
}
|
|
62
|
+
return v;
|
|
63
|
+
}
|
|
64
|
+
/** One request. JSON in, JSON out, unless the answer is not JSON. */
|
|
65
|
+
export async function ask(w, base, path, o = {}) {
|
|
66
|
+
const h = headers(w, o.head);
|
|
67
|
+
let body = o.raw;
|
|
68
|
+
if (o.body !== undefined) {
|
|
69
|
+
h.set('Content-Type', 'application/json');
|
|
70
|
+
body = JSON.stringify(o.body);
|
|
71
|
+
}
|
|
72
|
+
else if (o.type)
|
|
73
|
+
h.set('Content-Type', o.type);
|
|
74
|
+
const res = await fetch(`${origin(w, base)}${path}`, {
|
|
75
|
+
method: o.method ?? (body === undefined ? 'GET' : 'POST'),
|
|
76
|
+
headers: h,
|
|
77
|
+
body,
|
|
78
|
+
cache: 'no-store',
|
|
79
|
+
credentials: base === 'engine' && !w.engine ? 'same-origin' : 'omit',
|
|
80
|
+
signal: o.signal,
|
|
81
|
+
});
|
|
82
|
+
if (!res.ok) {
|
|
83
|
+
const [msg, code] = await reason(res);
|
|
84
|
+
throw new Refusal(res.status, msg || `${path.split('?')[0]} answered ${res.status}`, code);
|
|
85
|
+
}
|
|
86
|
+
o.heard?.(res.headers);
|
|
87
|
+
if (res.status === 204)
|
|
88
|
+
return undefined;
|
|
89
|
+
const type = res.headers.get('content-type') ?? '';
|
|
90
|
+
if (type.includes('json'))
|
|
91
|
+
return envelope(await res.json());
|
|
92
|
+
if (type.startsWith('text/'))
|
|
93
|
+
return (await res.text());
|
|
94
|
+
return (await res.blob());
|
|
95
|
+
}
|
|
96
|
+
/** `?a=1&b=2` from the defined, non-empty values. */
|
|
97
|
+
export function query(q) {
|
|
98
|
+
const s = new URLSearchParams();
|
|
99
|
+
for (const [k, v] of Object.entries(q))
|
|
100
|
+
if (v !== undefined && v !== null && v !== '')
|
|
101
|
+
s.set(k, String(v));
|
|
102
|
+
const out = s.toString();
|
|
103
|
+
return out ? `?${out}` : '';
|
|
104
|
+
}
|
|
105
|
+
const enc = encodeURIComponent;
|
|
106
|
+
/**
|
|
107
|
+
* One turn of the chat that decides what to make, streamed: `heard` gets the
|
|
108
|
+
* words so far as they arrive. Answers the words and the tool calls in full.
|
|
109
|
+
*/
|
|
110
|
+
export async function complete(w, body, heard, signal) {
|
|
111
|
+
const res = await fetch(`${w.api}/v1/chat/completions`, {
|
|
112
|
+
method: 'POST',
|
|
113
|
+
headers: headers(w, { 'Content-Type': 'application/json', Accept: 'text/event-stream' }),
|
|
114
|
+
body: JSON.stringify({ ...body, stream: true }),
|
|
115
|
+
cache: 'no-store',
|
|
116
|
+
credentials: 'omit',
|
|
117
|
+
signal,
|
|
118
|
+
});
|
|
119
|
+
if (!res.ok || !res.body) {
|
|
120
|
+
const [msg, code] = await reason(res);
|
|
121
|
+
throw new Refusal(res.status, msg || `The model answered ${res.status}`, code);
|
|
122
|
+
}
|
|
123
|
+
const type = res.headers.get('content-type') ?? '';
|
|
124
|
+
// A gateway that answers whole (an error envelope, or no stream) is read whole.
|
|
125
|
+
if (!type.includes('event-stream')) {
|
|
126
|
+
const whole = envelope(await res.json());
|
|
127
|
+
const m = whole.choices?.[0]?.message;
|
|
128
|
+
const text = m?.content ?? '';
|
|
129
|
+
heard(text);
|
|
130
|
+
return { text, model: whole.model ?? body.model, calls: (m?.tool_calls ?? []).map((c) => ({ id: c.id, name: c.function.name, args: c.function.arguments })) };
|
|
131
|
+
}
|
|
132
|
+
const reader = res.body.getReader();
|
|
133
|
+
const decoder = new TextDecoder();
|
|
134
|
+
let buffer = '';
|
|
135
|
+
let text = '';
|
|
136
|
+
let model = body.model;
|
|
137
|
+
const calls = new Map();
|
|
138
|
+
for (;;) {
|
|
139
|
+
const { value, done } = await reader.read();
|
|
140
|
+
if (done)
|
|
141
|
+
break;
|
|
142
|
+
buffer += decoder.decode(value, { stream: true });
|
|
143
|
+
let cut;
|
|
144
|
+
while ((cut = buffer.indexOf('\n')) >= 0) {
|
|
145
|
+
const line = buffer.slice(0, cut).trim();
|
|
146
|
+
buffer = buffer.slice(cut + 1);
|
|
147
|
+
if (!line.startsWith('data:'))
|
|
148
|
+
continue;
|
|
149
|
+
const data = line.slice(5).trim();
|
|
150
|
+
if (data === '[DONE]')
|
|
151
|
+
continue;
|
|
152
|
+
let chunk;
|
|
153
|
+
try {
|
|
154
|
+
chunk = JSON.parse(data);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (chunk.error)
|
|
160
|
+
throw new Refusal(502, chunk.error.message ?? 'The model stopped with an error.', chunk.error.code ?? '');
|
|
161
|
+
if (chunk.model)
|
|
162
|
+
model = chunk.model;
|
|
163
|
+
const delta = chunk.choices?.[0]?.delta;
|
|
164
|
+
if (delta?.content) {
|
|
165
|
+
text += delta.content;
|
|
166
|
+
heard(text);
|
|
167
|
+
}
|
|
168
|
+
for (const t of delta?.tool_calls ?? []) {
|
|
169
|
+
const had = calls.get(t.index) ?? { id: '', name: '', args: '' };
|
|
170
|
+
calls.set(t.index, {
|
|
171
|
+
id: t.id || had.id,
|
|
172
|
+
name: had.name + (t.function?.name ?? ''),
|
|
173
|
+
args: had.args + (t.function?.arguments ?? ''),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return { text, model, calls: [...calls.values()].filter((c) => c.name).map((c, i) => ({ ...c, id: c.id || `call_${i}` })) };
|
|
179
|
+
}
|
|
180
|
+
/** A JSON answer from a model, asked for by schema: what a generator (a type system, a deck's words) is made of. */
|
|
181
|
+
export async function structured(w, model, system, user, schema, signal) {
|
|
182
|
+
const out = await ask(w, 'api', '/v1/chat/completions', {
|
|
183
|
+
body: {
|
|
184
|
+
model,
|
|
185
|
+
messages: [
|
|
186
|
+
{ role: 'system', content: system },
|
|
187
|
+
{ role: 'user', content: user },
|
|
188
|
+
],
|
|
189
|
+
response_format: { type: 'json_schema', json_schema: { name: 'answer', strict: true, schema } },
|
|
190
|
+
},
|
|
191
|
+
signal,
|
|
192
|
+
});
|
|
193
|
+
const text = out.choices?.[0]?.message?.content ?? '';
|
|
194
|
+
const json = text.slice(text.indexOf('{'), text.lastIndexOf('}') + 1);
|
|
195
|
+
try {
|
|
196
|
+
return JSON.parse(json);
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
throw new Refusal(502, 'The model answered with no JSON.');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/** A data: URL's bytes. */
|
|
203
|
+
export function bytes(url) {
|
|
204
|
+
const comma = url.indexOf(',');
|
|
205
|
+
const head = url.slice(5, comma);
|
|
206
|
+
const mime = head.split(';')[0] || 'image/png';
|
|
207
|
+
const bin = atob(url.slice(comma + 1));
|
|
208
|
+
const out = new Uint8Array(bin.length);
|
|
209
|
+
for (let i = 0; i < bin.length; i++)
|
|
210
|
+
out[i] = bin.charCodeAt(i);
|
|
211
|
+
return { blob: new Blob([out], { type: mime }), mime };
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* An image from a gateway image model: words, and the pictures it starts from
|
|
215
|
+
* (an edit). The image models are chat models with an image output, so this is
|
|
216
|
+
* a chat completion asking for both modalities; the pictures come back on the
|
|
217
|
+
* message (`images`) or among its content parts.
|
|
218
|
+
*/
|
|
219
|
+
export async function draw(w, model, prompt, o = {}) {
|
|
220
|
+
let held;
|
|
221
|
+
const content = [{ type: 'text', text: prompt }, ...(o.from ?? []).map((url) => ({ type: 'image_url', image_url: { url } }))];
|
|
222
|
+
const out = await ask(w, 'api', '/v1/chat/completions', {
|
|
223
|
+
body: {
|
|
224
|
+
model,
|
|
225
|
+
modalities: ['image', 'text'],
|
|
226
|
+
messages: [{ role: 'user', content }],
|
|
227
|
+
...(o.aspect ? { image_config: { aspect_ratio: o.aspect } } : {}),
|
|
228
|
+
},
|
|
229
|
+
signal: o.signal,
|
|
230
|
+
heard: (h) => (held = h),
|
|
231
|
+
});
|
|
232
|
+
const m = out.choices?.[0]?.message;
|
|
233
|
+
const urls = [
|
|
234
|
+
...(m?.images ?? []).map((i) => i.image_url?.url ?? ''),
|
|
235
|
+
...(Array.isArray(m?.content) ? m.content.map((p) => (p.type === 'image_url' ? (p.image_url?.url ?? '') : '')) : []),
|
|
236
|
+
].filter((u) => u.startsWith('data:image/'));
|
|
237
|
+
if (!urls.length)
|
|
238
|
+
throw withheld(model, held);
|
|
239
|
+
return urls.map(bytes);
|
|
240
|
+
}
|
|
241
|
+
/*
|
|
242
|
+
* The gateway answers a request it will not serve with a model it will, in
|
|
243
|
+
* words: X-Hanzo-Usage-Reason says why and X-Hanzo-Upgrade-Url or
|
|
244
|
+
* X-Hanzo-Topup-Url what lifts it. A picture request answered that way is a
|
|
245
|
+
* refusal of the lane, said in the gateway's own reason.
|
|
246
|
+
*/
|
|
247
|
+
const WITHHELD = {
|
|
248
|
+
paid_plan_required: 'Pictures from this model need a paid plan',
|
|
249
|
+
plan_allowance_used: "This plan's pictures are used for now",
|
|
250
|
+
usage_cap_exceeded: "This org's usage cap is reached",
|
|
251
|
+
free_plan_cap: 'The free plan has no more pictures today',
|
|
252
|
+
model_cap: "This model's share is used for now",
|
|
253
|
+
};
|
|
254
|
+
function withheld(model, h) {
|
|
255
|
+
const why = h?.get('X-Hanzo-Usage-Reason') ?? '';
|
|
256
|
+
if (!why)
|
|
257
|
+
return new Refusal(502, `${model} answered with no picture.`);
|
|
258
|
+
const fix = h?.get('X-Hanzo-Upgrade-Url') || h?.get('X-Hanzo-Topup-Url') || '';
|
|
259
|
+
return new Refusal(402, `${WITHHELD[why] ?? `Pictures are withheld (${why})`}${fix ? `: ${fix}` : '.'}`, why);
|
|
260
|
+
}
|
|
261
|
+
/** Speech from words: the platform's voice. */
|
|
262
|
+
export async function speak(w, input, voice, model, signal) {
|
|
263
|
+
return ask(w, 'api', '/v1/audio/speech', { body: { model, input, voice, response_format: 'mp3' }, signal });
|
|
264
|
+
}
|
|
265
|
+
/** The person the bearer names: whose database this is. */
|
|
266
|
+
function subject(w) {
|
|
267
|
+
const t = w.token();
|
|
268
|
+
if (!t)
|
|
269
|
+
return '';
|
|
270
|
+
try {
|
|
271
|
+
const part = t.split('.')[1].replace(/-/g, '+').replace(/_/g, '/');
|
|
272
|
+
const c = JSON.parse(atob(part + '='.repeat((4 - (part.length % 4)) % 4)));
|
|
273
|
+
return typeof c.sub === 'string' ? c.sub : '';
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return '';
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const dbs = new Map();
|
|
280
|
+
/** This person's database in this org, opened once. */
|
|
281
|
+
function db(w) {
|
|
282
|
+
const who = subject(w);
|
|
283
|
+
if (!who || !w.org)
|
|
284
|
+
return Promise.reject(new Refusal(401, 'Sign in to keep what you make.'));
|
|
285
|
+
const name = `hanzo-studio:${w.org}:${who}`;
|
|
286
|
+
let open = dbs.get(name);
|
|
287
|
+
if (!open) {
|
|
288
|
+
open = new Promise((resolve, reject) => {
|
|
289
|
+
const req = indexedDB.open(name, 1);
|
|
290
|
+
req.onupgradeneeded = () => req.result.createObjectStore('files', { keyPath: 'key' });
|
|
291
|
+
req.onsuccess = () => resolve(req.result);
|
|
292
|
+
req.onerror = () => reject(new Refusal(507, 'This browser would not keep the file.'));
|
|
293
|
+
});
|
|
294
|
+
dbs.set(name, open);
|
|
295
|
+
open.catch(() => dbs.delete(name));
|
|
296
|
+
}
|
|
297
|
+
return open;
|
|
298
|
+
}
|
|
299
|
+
function tx(d, mode, run) {
|
|
300
|
+
return new Promise((resolve, reject) => {
|
|
301
|
+
const req = run(d.transaction('files', mode).objectStore('files'));
|
|
302
|
+
req.onsuccess = () => resolve(req.result);
|
|
303
|
+
req.onerror = () => reject(new Refusal(507, 'This browser would not keep the file.'));
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
const get = async (w, key) => (await tx(await db(w), 'readonly', (s) => s.get(key))) ?? null;
|
|
307
|
+
/* The Library: answered or not, asked once per page. */
|
|
308
|
+
let library = null;
|
|
309
|
+
/** Whether api.hanzo.ai/v1/library answers for this person. */
|
|
310
|
+
export function libraryLive(w) {
|
|
311
|
+
library ??= ask(w, 'api', '/v1/library?limit=1&source=studio').then(() => true, (e) => {
|
|
312
|
+
library = null;
|
|
313
|
+
return e instanceof Refusal && e.status !== 404 && e.status !== 405 ? Promise.reject(e) : false;
|
|
314
|
+
});
|
|
315
|
+
return library.catch(() => false);
|
|
316
|
+
}
|
|
317
|
+
const KINDS_LIB = { image: 'image', poster: 'image', ad: 'image', video: 'video', voice: 'audio', music: 'audio', document: 'document', slides: 'slides', type: 'type', mesh: 'model' };
|
|
318
|
+
const base64 = async (b) => {
|
|
319
|
+
const buf = new Uint8Array(await b.arrayBuffer());
|
|
320
|
+
let bin = '';
|
|
321
|
+
for (let i = 0; i < buf.length; i += 0x8000)
|
|
322
|
+
bin += String.fromCharCode(...buf.subarray(i, i + 0x8000));
|
|
323
|
+
return btoa(bin);
|
|
324
|
+
};
|
|
325
|
+
/** Save one kept file to the Library, idempotently (its key is the idempotency key). */
|
|
326
|
+
async function shelve(w, r) {
|
|
327
|
+
const [, kind, chat] = r.key.split('/');
|
|
328
|
+
if (!r.key.startsWith('files/') || !kind || !chat || r.blob.size > 10 * 1024 * 1024)
|
|
329
|
+
return undefined;
|
|
330
|
+
const item = await ask(w, 'api', '/v1/library', {
|
|
331
|
+
body: {
|
|
332
|
+
name: r.key.split('/').pop(),
|
|
333
|
+
mime: r.mime,
|
|
334
|
+
data: await base64(r.blob),
|
|
335
|
+
source: 'studio',
|
|
336
|
+
kind: KINDS_LIB[kind] ?? 'file',
|
|
337
|
+
ref: chat,
|
|
338
|
+
origin: `/studio/c/${chat}`,
|
|
339
|
+
key: r.key,
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
return item.id;
|
|
343
|
+
}
|
|
344
|
+
/** Keep bytes at a key. Answers the key. */
|
|
345
|
+
export async function put(w, key, blob) {
|
|
346
|
+
const r = { key, blob, mime: blob.type || 'application/octet-stream', modified: Date.now() };
|
|
347
|
+
if (key.startsWith('files/') && (await libraryLive(w)))
|
|
348
|
+
r.lib = await shelve(w, r).catch(() => undefined);
|
|
349
|
+
await tx(await db(w), 'readwrite', (s) => s.put(r));
|
|
350
|
+
shown.delete(key);
|
|
351
|
+
return key;
|
|
352
|
+
}
|
|
353
|
+
/** Carry up to the Library whatever is kept only here. Answers how many went. */
|
|
354
|
+
export async function sync(w) {
|
|
355
|
+
if (!(await libraryLive(w)))
|
|
356
|
+
return 0;
|
|
357
|
+
const all = await tx(await db(w), 'readonly', (s) => s.getAll());
|
|
358
|
+
let n = 0;
|
|
359
|
+
for (const r of all.filter((x) => x.key.startsWith('files/') && !x.lib)) {
|
|
360
|
+
const lib = await shelve(w, r).catch(() => undefined);
|
|
361
|
+
if (!lib)
|
|
362
|
+
continue;
|
|
363
|
+
await tx(await db(w), 'readwrite', (s) => s.put({ ...r, lib }));
|
|
364
|
+
n++;
|
|
365
|
+
}
|
|
366
|
+
return n;
|
|
367
|
+
}
|
|
368
|
+
/** Addresses handed out, kept while they work: an object URL for the page's life, a Library URL until it expires. */
|
|
369
|
+
const shown = new Map();
|
|
370
|
+
/** An address a page can draw a kept file at. */
|
|
371
|
+
export async function url(w, key) {
|
|
372
|
+
const hit = shown.get(key);
|
|
373
|
+
if (hit && hit.until > Date.now())
|
|
374
|
+
return hit.url;
|
|
375
|
+
const r = await get(w, key);
|
|
376
|
+
if (!r)
|
|
377
|
+
throw new Refusal(404, 'This file is not kept in this browser.');
|
|
378
|
+
if (r.lib) {
|
|
379
|
+
const item = await ask(w, 'api', `/v1/library/${enc(r.lib)}`).catch(() => null);
|
|
380
|
+
if (item?.url) {
|
|
381
|
+
shown.set(key, { url: item.url, until: item.expires ? Date.parse(item.expires) - 60_000 : Date.now() + 240_000 });
|
|
382
|
+
return item.url;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const made = URL.createObjectURL(r.blob);
|
|
386
|
+
shown.set(key, { url: made, until: Number.POSITIVE_INFINITY });
|
|
387
|
+
return made;
|
|
388
|
+
}
|
|
389
|
+
/** A kept file's bytes. */
|
|
390
|
+
export async function read(w, key) {
|
|
391
|
+
const r = await get(w, key);
|
|
392
|
+
if (!r)
|
|
393
|
+
throw new Refusal(404, 'This file is not kept in this browser.');
|
|
394
|
+
return r.blob;
|
|
395
|
+
}
|
|
396
|
+
/** What is kept under a prefix (one that ends in '/'), keys relative to it. */
|
|
397
|
+
export async function list(w, prefix) {
|
|
398
|
+
const range = IDBKeyRange.bound(prefix, `${prefix}`);
|
|
399
|
+
const all = await tx(await db(w), 'readonly', (s) => s.getAll(range));
|
|
400
|
+
return all.map((r) => ({ key: r.key.slice(prefix.length), size: r.blob.size, modified: r.modified, shelved: Boolean(r.lib) }));
|
|
401
|
+
}
|
|
402
|
+
export async function remove(w, key) {
|
|
403
|
+
const r = await get(w, key);
|
|
404
|
+
if (r?.lib)
|
|
405
|
+
await ask(w, 'api', `/v1/library/${enc(r.lib)}`, { method: 'DELETE' }).catch(() => undefined);
|
|
406
|
+
await tx(await db(w), 'readwrite', (s) => s.delete(key));
|
|
407
|
+
const hit = shown.get(key);
|
|
408
|
+
if (hit?.url.startsWith('blob:'))
|
|
409
|
+
URL.revokeObjectURL(hit.url);
|
|
410
|
+
shown.delete(key);
|
|
411
|
+
}
|
|
412
|
+
/** A kept JSON document, or null when there is none. */
|
|
413
|
+
export async function load(w, key) {
|
|
414
|
+
const r = await get(w, key);
|
|
415
|
+
if (!r)
|
|
416
|
+
return null;
|
|
417
|
+
try {
|
|
418
|
+
return JSON.parse(await r.blob.text());
|
|
419
|
+
}
|
|
420
|
+
catch {
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
export const save = (w, key, value) => put(w, key, new Blob([JSON.stringify(value)], { type: 'application/json' }));
|
|
425
|
+
/** The org's GPUs and what each can run. A fleet the engine cannot read is an error, never an empty fleet. */
|
|
426
|
+
export const gpus = (w, signal) => ask(w, 'engine', '/v1/gpu-status', { signal });
|
|
427
|
+
/** Dispatch a lane to the org's GPUs (the engine's one funnel). */
|
|
428
|
+
export const dispatch = (w, lane, body, signal) => ask(w, 'engine', `/v1/library/${lane}`, { body, signal });
|
|
429
|
+
/** The engine's record of what was dispatched and what landed. */
|
|
430
|
+
export const worklog = (w, signal) => ask(w, 'engine', '/v1/worklog', { signal }).then((r) => r.items ?? []);
|
|
431
|
+
/** A landed file of the engine library, as bytes. */
|
|
432
|
+
export const landed = (w, path) => ask(w, 'engine', `/v1/library/file${query({ path })}`);
|
|
433
|
+
/** Documents and decks: the engine's renderers. */
|
|
434
|
+
export const documents = {
|
|
435
|
+
schema: (w) => ask(w, 'engine', '/v1/documents/schema'),
|
|
436
|
+
create: (w, body) => ask(w, 'engine', '/v1/documents', { body }),
|
|
437
|
+
generate: (w, body) => ask(w, 'engine', '/v1/documents/generate', { body }),
|
|
438
|
+
patch: (w, id, body) => ask(w, 'engine', `/v1/documents/${enc(id)}`, { method: 'PATCH', body }),
|
|
439
|
+
get: (w, id) => ask(w, 'engine', `/v1/documents/${enc(id)}`),
|
|
440
|
+
file: (w, id, format) => ask(w, 'engine', `/v1/documents/${enc(id)}/download${query({ format })}`),
|
|
441
|
+
};
|
|
442
|
+
/** Stage a picture in the engine's input folder for a lane that starts from one. Answers the name the lane takes. */
|
|
443
|
+
export async function stage(w, blob, name) {
|
|
444
|
+
const form = new FormData();
|
|
445
|
+
form.append('image', blob, name);
|
|
446
|
+
form.append('type', 'input');
|
|
447
|
+
form.append('overwrite', 'false');
|
|
448
|
+
const r = await ask(w, 'engine', '/v1/upload/image', { raw: form });
|
|
449
|
+
return r.subfolder ? `${r.subfolder}/${r.name}` : r.name;
|
|
450
|
+
}
|
|
451
|
+
/** A clip from a gateway video model: started, followed, read back (the async /v1/videos API). */
|
|
452
|
+
export async function film(w, model, prompt, o) {
|
|
453
|
+
const job = await ask(w, 'api', '/v1/videos/generations', { body: { model, prompt, size: o.size, seconds: o.seconds }, signal: o.signal });
|
|
454
|
+
const id = job.id ?? job.data?.[0]?.id;
|
|
455
|
+
if (!id)
|
|
456
|
+
throw new Refusal(502, `${model} took the clip but named no job.`);
|
|
457
|
+
const until = Date.now() + 20 * 60_000;
|
|
458
|
+
for (;;) {
|
|
459
|
+
const s = await ask(w, 'api', `/v1/videos/${enc(id)}`, { signal: o.signal });
|
|
460
|
+
const status = (s.status ?? '').toLowerCase();
|
|
461
|
+
if (['completed', 'succeeded', 'done'].includes(status))
|
|
462
|
+
break;
|
|
463
|
+
if (['failed', 'error', 'cancelled'].includes(status))
|
|
464
|
+
throw new Refusal(502, typeof s.error === 'string' ? s.error : (s.error?.message ?? `The clip ${status}.`));
|
|
465
|
+
if (Date.now() > until)
|
|
466
|
+
throw new Refusal(504, 'The clip is still rendering.');
|
|
467
|
+
o.doing(status === 'queued' ? 'Waiting for the video model' : 'Rendering the clip');
|
|
468
|
+
await new Promise((r) => setTimeout(r, 4000));
|
|
469
|
+
}
|
|
470
|
+
return ask(w, 'api', `/v1/videos/${enc(id)}/content`, { signal: o.signal });
|
|
471
|
+
}
|
|
472
|
+
/** The gateway's catalog of models that make something other than words. */
|
|
473
|
+
export async function makers(w, signal) {
|
|
474
|
+
const out = await ask(w, 'api', '/v1/models', { signal });
|
|
475
|
+
return (out.data ?? [])
|
|
476
|
+
.filter((m) => (m.outputs ?? []).some((o) => ['image', 'audio', 'video', '3d', 'mesh'].includes(o)))
|
|
477
|
+
.map((m) => ({ id: m.id, name: m.name ?? m.id, inputs: m.inputs ?? [], outputs: m.outputs ?? [] }));
|
|
478
|
+
}
|
package/lib/zip.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A ZIP of files, stored (no compression): what an ad set downloads as. PNGs
|
|
3
|
+
* are compressed already, so storing them costs nothing and keeps the writer
|
|
4
|
+
* to the format's two records and a CRC.
|
|
5
|
+
*/
|
|
6
|
+
export declare function zip(files: readonly {
|
|
7
|
+
name: string;
|
|
8
|
+
blob: Blob;
|
|
9
|
+
}[]): Promise<Blob>;
|
package/lib/zip.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A ZIP of files, stored (no compression): what an ad set downloads as. PNGs
|
|
3
|
+
* are compressed already, so storing them costs nothing and keeps the writer
|
|
4
|
+
* to the format's two records and a CRC.
|
|
5
|
+
*/
|
|
6
|
+
const TABLE = (() => {
|
|
7
|
+
const t = new Uint32Array(256);
|
|
8
|
+
for (let n = 0; n < 256; n++) {
|
|
9
|
+
let c = n;
|
|
10
|
+
for (let k = 0; k < 8; k++)
|
|
11
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
12
|
+
t[n] = c >>> 0;
|
|
13
|
+
}
|
|
14
|
+
return t;
|
|
15
|
+
})();
|
|
16
|
+
function crc32(b) {
|
|
17
|
+
let c = 0xffffffff;
|
|
18
|
+
for (let i = 0; i < b.length; i++)
|
|
19
|
+
c = TABLE[(c ^ b[i]) & 0xff] ^ (c >>> 8);
|
|
20
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
21
|
+
}
|
|
22
|
+
export async function zip(files) {
|
|
23
|
+
const enc = new TextEncoder();
|
|
24
|
+
const out = [];
|
|
25
|
+
const central = [];
|
|
26
|
+
let offset = 0;
|
|
27
|
+
for (const f of files) {
|
|
28
|
+
const name = enc.encode(f.name);
|
|
29
|
+
const data = new Uint8Array(await f.blob.arrayBuffer());
|
|
30
|
+
const crc = crc32(data);
|
|
31
|
+
const local = new DataView(new ArrayBuffer(30));
|
|
32
|
+
local.setUint32(0, 0x04034b50, true);
|
|
33
|
+
local.setUint16(4, 20, true);
|
|
34
|
+
local.setUint16(8, 0, true);
|
|
35
|
+
local.setUint32(14, crc, true);
|
|
36
|
+
local.setUint32(18, data.length, true);
|
|
37
|
+
local.setUint32(22, data.length, true);
|
|
38
|
+
local.setUint16(26, name.length, true);
|
|
39
|
+
out.push(new Uint8Array(local.buffer), name, data);
|
|
40
|
+
const dir = new DataView(new ArrayBuffer(46));
|
|
41
|
+
dir.setUint32(0, 0x02014b50, true);
|
|
42
|
+
dir.setUint16(4, 20, true);
|
|
43
|
+
dir.setUint16(6, 20, true);
|
|
44
|
+
dir.setUint32(16, crc, true);
|
|
45
|
+
dir.setUint32(20, data.length, true);
|
|
46
|
+
dir.setUint32(24, data.length, true);
|
|
47
|
+
dir.setUint16(28, name.length, true);
|
|
48
|
+
dir.setUint32(42, offset, true);
|
|
49
|
+
central.push(new Uint8Array(dir.buffer), name);
|
|
50
|
+
offset += 30 + name.length + data.length;
|
|
51
|
+
}
|
|
52
|
+
const size = central.reduce((n, b) => n + b.length, 0);
|
|
53
|
+
const end = new DataView(new ArrayBuffer(22));
|
|
54
|
+
end.setUint32(0, 0x06054b50, true);
|
|
55
|
+
end.setUint16(8, files.length, true);
|
|
56
|
+
end.setUint16(10, files.length, true);
|
|
57
|
+
end.setUint32(12, size, true);
|
|
58
|
+
end.setUint32(16, offset, true);
|
|
59
|
+
return new Blob([...out, ...central, new Uint8Array(end.buffer)], { type: 'application/zip' });
|
|
60
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,78 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hanzo/studio",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "BSD-3-Clause",
|
|
6
|
+
"description": "Hanzo Studio: a chat that makes images, posters, documents, slides, type systems, 3D, video and sound, with every result open in a tab beside it. Mounted by the Hanzo app at hanzo.ai/studio and served whole at studio.hanzo.ai.",
|
|
7
|
+
"homepage": "https://studio.hanzo.ai",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=22.18"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"three": "0.186.1"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@axe-core/playwright": "4.14.0",
|
|
16
|
+
"@hanzo/build": "0.2.56",
|
|
17
|
+
"@hanzo/composer": "0.2.0",
|
|
18
|
+
"@hanzo/font": "1.9.3",
|
|
19
|
+
"@hanzo/gui": "8.3.5",
|
|
20
|
+
"@hanzo/ui": "8.27.91",
|
|
21
|
+
"@hanzo/vite": "0.1.4",
|
|
22
|
+
"@hanzo/voice": "0.1.18",
|
|
23
|
+
"@hanzogui/lucide-icons-2": "8.3.5",
|
|
24
|
+
"@hanzogui/next-theme": "8.3.5",
|
|
25
|
+
"@playwright/test": "1.63.0",
|
|
26
|
+
"@types/node": "26.5.0",
|
|
27
|
+
"@types/react": "19.2.18",
|
|
28
|
+
"@types/react-dom": "19.2.7",
|
|
29
|
+
"@types/three": "0.186.0",
|
|
30
|
+
"@vitejs/plugin-react": "^6.1.1",
|
|
31
|
+
"next-themes": "^0.4.6",
|
|
32
|
+
"react": "19.2.8",
|
|
33
|
+
"react-dom": "19.2.8",
|
|
34
|
+
"react-native-web": "^0.21.2",
|
|
35
|
+
"typescript": "7.0.2",
|
|
36
|
+
"vite": "^8.2.2",
|
|
37
|
+
"vitest": "^4.1.11"
|
|
38
|
+
},
|
|
39
|
+
"exports": {
|
|
40
|
+
".": {
|
|
41
|
+
"types": "./lib/index.d.ts",
|
|
42
|
+
"import": "./lib/index.js",
|
|
43
|
+
"default": "./lib/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./package.json": "./package.json"
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"lib",
|
|
49
|
+
"LICENSE",
|
|
50
|
+
"NOTICE"
|
|
51
|
+
],
|
|
52
|
+
"sideEffects": false,
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "git+https://github.com/hanzoai/studio.git"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"@hanzo/build": ">=0.2.56",
|
|
62
|
+
"@hanzo/composer": ">=0.2.0",
|
|
63
|
+
"@hanzo/gui": ">=8.3.5",
|
|
64
|
+
"@hanzo/ui": ">=8.27.77",
|
|
65
|
+
"@hanzo/voice": ">=0.1.16",
|
|
66
|
+
"@hanzogui/lucide-icons-2": ">=8.3.5",
|
|
67
|
+
"react": ">=19.2.8"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"dev": "vite",
|
|
71
|
+
"build": "tsc --noEmit && vite build",
|
|
72
|
+
"build:lib": "rm -rf lib && tsc -p tsconfig.lib.json",
|
|
73
|
+
"typecheck": "tsc --noEmit",
|
|
74
|
+
"preview": "vite preview",
|
|
75
|
+
"test": "vitest run",
|
|
76
|
+
"e2e": "playwright test"
|
|
77
|
+
}
|
|
6
78
|
}
|
package/README.md
DELETED