@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/wire.d.ts ADDED
@@ -0,0 +1,237 @@
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
+ export interface Wire {
22
+ /** The platform origin, no trailing slash. */
23
+ api: string;
24
+ /** The engine origin, no trailing slash: '' for this origin. */
25
+ engine: string;
26
+ /** The signed-in person's bearer, read at call time. */
27
+ token: () => string | null;
28
+ /** The org every call is scoped to (`X-Org-Id`). */
29
+ org: string | null;
30
+ }
31
+ /** Where the engine is from a page that is not studio.hanzo.ai. */
32
+ export declare const ENGINE = "https://studio.hanzo.ai";
33
+ export declare class Refusal extends Error {
34
+ readonly status: number;
35
+ readonly code: string;
36
+ constructor(status: number, message: string, code?: string);
37
+ }
38
+ type Base = 'api' | 'engine';
39
+ export interface Ask {
40
+ method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
41
+ body?: unknown;
42
+ /** A body sent as is (a form, a blob). */
43
+ raw?: BodyInit;
44
+ type?: string;
45
+ signal?: AbortSignal;
46
+ head?: Record<string, string>;
47
+ }
48
+ /** One request. JSON in, JSON out, unless the answer is not JSON. */
49
+ export declare function ask<T>(w: Wire, base: Base, path: string, o?: Ask): Promise<T>;
50
+ /** `?a=1&b=2` from the defined, non-empty values. */
51
+ export declare function query(q: Record<string, string | number | boolean | null | undefined>): string;
52
+ export interface Call {
53
+ id: string;
54
+ name: string;
55
+ /** The arguments as the model wrote them: JSON text. */
56
+ args: string;
57
+ }
58
+ export interface Said {
59
+ text: string;
60
+ calls: Call[];
61
+ /** The model that answered, as the gateway names it. */
62
+ model: string;
63
+ }
64
+ export type Message = {
65
+ role: 'system' | 'user';
66
+ content: string | Part[];
67
+ } | {
68
+ role: 'assistant';
69
+ content: string;
70
+ tool_calls?: {
71
+ id: string;
72
+ type: 'function';
73
+ function: {
74
+ name: string;
75
+ arguments: string;
76
+ };
77
+ }[];
78
+ } | {
79
+ role: 'tool';
80
+ tool_call_id: string;
81
+ content: string;
82
+ };
83
+ export type Part = {
84
+ type: 'text';
85
+ text: string;
86
+ } | {
87
+ type: 'image_url';
88
+ image_url: {
89
+ url: string;
90
+ };
91
+ };
92
+ export interface Tool {
93
+ type: 'function';
94
+ function: {
95
+ name: string;
96
+ description: string;
97
+ parameters: object;
98
+ };
99
+ }
100
+ /**
101
+ * One turn of the chat that decides what to make, streamed: `heard` gets the
102
+ * words so far as they arrive. Answers the words and the tool calls in full.
103
+ */
104
+ export declare function complete(w: Wire, body: {
105
+ model: string;
106
+ messages: Message[];
107
+ tools?: Tool[];
108
+ temperature?: number;
109
+ }, heard: (text: string) => void, signal?: AbortSignal): Promise<Said>;
110
+ /** A JSON answer from a model, asked for by schema: what a generator (a type system, a deck's words) is made of. */
111
+ export declare function structured<T>(w: Wire, model: string, system: string, user: string, schema: object, signal?: AbortSignal): Promise<T>;
112
+ export interface Picture {
113
+ blob: Blob;
114
+ mime: string;
115
+ }
116
+ /** A data: URL's bytes. */
117
+ export declare function bytes(url: string): Picture;
118
+ /**
119
+ * An image from a gateway image model: words, and the pictures it starts from
120
+ * (an edit). The image models are chat models with an image output, so this is
121
+ * a chat completion asking for both modalities; the pictures come back on the
122
+ * message (`images`) or among its content parts.
123
+ */
124
+ export declare function draw(w: Wire, model: string, prompt: string, o?: {
125
+ from?: string[];
126
+ aspect?: string;
127
+ signal?: AbortSignal;
128
+ }): Promise<Picture[]>;
129
+ /** Speech from words: the platform's voice. */
130
+ export declare function speak(w: Wire, input: string, voice: string, model: string, signal?: AbortSignal): Promise<Blob>;
131
+ /** Whether api.hanzo.ai/v1/library answers for this person. */
132
+ export declare function libraryLive(w: Wire): Promise<boolean>;
133
+ /** Keep bytes at a key. Answers the key. */
134
+ export declare function put(w: Wire, key: string, blob: Blob): Promise<string>;
135
+ /** Carry up to the Library whatever is kept only here. Answers how many went. */
136
+ export declare function sync(w: Wire): Promise<number>;
137
+ /** An address a page can draw a kept file at. */
138
+ export declare function url(w: Wire, key: string): Promise<string>;
139
+ /** A kept file's bytes. */
140
+ export declare function read(w: Wire, key: string): Promise<Blob>;
141
+ export interface Entry {
142
+ /** Relative to the prefix listed. */
143
+ key: string;
144
+ size: number;
145
+ /** Unix milliseconds. */
146
+ modified: number;
147
+ /** Whether it is saved in the Library too. */
148
+ shelved: boolean;
149
+ }
150
+ /** What is kept under a prefix (one that ends in '/'), keys relative to it. */
151
+ export declare function list(w: Wire, prefix: string): Promise<Entry[]>;
152
+ export declare function remove(w: Wire, key: string): Promise<void>;
153
+ /** A kept JSON document, or null when there is none. */
154
+ export declare function load<T>(w: Wire, key: string): Promise<T | null>;
155
+ export declare const save: (w: Wire, key: string, value: unknown) => Promise<string>;
156
+ export interface Node {
157
+ identity: string;
158
+ name?: string;
159
+ online?: boolean;
160
+ /** The lanes this GPU can run now: every file its graph loads is present. */
161
+ lanes?: string[];
162
+ }
163
+ export interface Fleet {
164
+ state: 'busy' | 'ready' | 'offline';
165
+ online: number;
166
+ total: number;
167
+ nodes: Node[];
168
+ }
169
+ /** The org's GPUs and what each can run. A fleet the engine cannot read is an error, never an empty fleet. */
170
+ export declare const gpus: (w: Wire, signal?: AbortSignal) => Promise<Fleet>;
171
+ export interface Submitted {
172
+ prompt_id?: string;
173
+ id?: string;
174
+ node?: string | null;
175
+ output?: string;
176
+ outputs?: string[];
177
+ }
178
+ /** Dispatch a lane to the org's GPUs (the engine's one funnel). */
179
+ export declare const dispatch: (w: Wire, lane: 'image' | 'video' | 'fix' | 'model3d' | 'music', body: object, signal?: AbortSignal) => Promise<Submitted>;
180
+ export interface Row {
181
+ id: string;
182
+ status: string;
183
+ kind?: string;
184
+ output?: string;
185
+ outputs?: string[];
186
+ error?: string;
187
+ prompt_id?: string;
188
+ }
189
+ /** The engine's record of what was dispatched and what landed. */
190
+ export declare const worklog: (w: Wire, signal?: AbortSignal) => Promise<Row[]>;
191
+ /** A landed file of the engine library, as bytes. */
192
+ export declare const landed: (w: Wire, path: string) => Promise<Blob>;
193
+ /** Documents and decks: the engine's renderers. */
194
+ export declare const documents: {
195
+ schema: (w: Wire) => Promise<object>;
196
+ create: (w: Wire, body: {
197
+ kind: string;
198
+ ir: object;
199
+ title?: string;
200
+ }) => Promise<{
201
+ id: string;
202
+ }>;
203
+ generate: (w: Wire, body: {
204
+ templateId: string;
205
+ description?: string;
206
+ fields?: object;
207
+ }) => Promise<{
208
+ id: string;
209
+ }>;
210
+ patch: (w: Wire, id: string, body: object) => Promise<{
211
+ id: string;
212
+ }>;
213
+ get: (w: Wire, id: string) => Promise<{
214
+ id: string;
215
+ kind: string;
216
+ ir: object;
217
+ title: string;
218
+ }>;
219
+ file: (w: Wire, id: string, format: string) => Promise<Blob>;
220
+ };
221
+ /** Stage a picture in the engine's input folder for a lane that starts from one. Answers the name the lane takes. */
222
+ export declare function stage(w: Wire, blob: Blob, name: string): Promise<string>;
223
+ /** A clip from a gateway video model: started, followed, read back (the async /v1/videos API). */
224
+ export declare function film(w: Wire, model: string, prompt: string, o: {
225
+ size?: string;
226
+ seconds?: number;
227
+ doing: (l: string) => void;
228
+ signal: AbortSignal;
229
+ }): Promise<Blob>;
230
+ /** The gateway's catalog of models that make something other than words. */
231
+ export declare function makers(w: Wire, signal?: AbortSignal): Promise<{
232
+ id: string;
233
+ name: string;
234
+ inputs: string[];
235
+ outputs: string[];
236
+ }[]>;
237
+ export {};
package/lib/wire.js ADDED
@@ -0,0 +1,455 @@
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
+ if (res.status === 204)
87
+ return undefined;
88
+ const type = res.headers.get('content-type') ?? '';
89
+ if (type.includes('json'))
90
+ return envelope(await res.json());
91
+ if (type.startsWith('text/'))
92
+ return (await res.text());
93
+ return (await res.blob());
94
+ }
95
+ /** `?a=1&b=2` from the defined, non-empty values. */
96
+ export function query(q) {
97
+ const s = new URLSearchParams();
98
+ for (const [k, v] of Object.entries(q))
99
+ if (v !== undefined && v !== null && v !== '')
100
+ s.set(k, String(v));
101
+ const out = s.toString();
102
+ return out ? `?${out}` : '';
103
+ }
104
+ const enc = encodeURIComponent;
105
+ /**
106
+ * One turn of the chat that decides what to make, streamed: `heard` gets the
107
+ * words so far as they arrive. Answers the words and the tool calls in full.
108
+ */
109
+ export async function complete(w, body, heard, signal) {
110
+ const res = await fetch(`${w.api}/v1/chat/completions`, {
111
+ method: 'POST',
112
+ headers: headers(w, { 'Content-Type': 'application/json', Accept: 'text/event-stream' }),
113
+ body: JSON.stringify({ ...body, stream: true }),
114
+ cache: 'no-store',
115
+ credentials: 'omit',
116
+ signal,
117
+ });
118
+ if (!res.ok || !res.body) {
119
+ const [msg, code] = await reason(res);
120
+ throw new Refusal(res.status, msg || `The model answered ${res.status}`, code);
121
+ }
122
+ const type = res.headers.get('content-type') ?? '';
123
+ // A gateway that answers whole (an error envelope, or no stream) is read whole.
124
+ if (!type.includes('event-stream')) {
125
+ const whole = envelope(await res.json());
126
+ const m = whole.choices?.[0]?.message;
127
+ const text = m?.content ?? '';
128
+ heard(text);
129
+ return { text, model: whole.model ?? body.model, calls: (m?.tool_calls ?? []).map((c) => ({ id: c.id, name: c.function.name, args: c.function.arguments })) };
130
+ }
131
+ const reader = res.body.getReader();
132
+ const decoder = new TextDecoder();
133
+ let buffer = '';
134
+ let text = '';
135
+ let model = body.model;
136
+ const calls = new Map();
137
+ for (;;) {
138
+ const { value, done } = await reader.read();
139
+ if (done)
140
+ break;
141
+ buffer += decoder.decode(value, { stream: true });
142
+ let cut;
143
+ while ((cut = buffer.indexOf('\n')) >= 0) {
144
+ const line = buffer.slice(0, cut).trim();
145
+ buffer = buffer.slice(cut + 1);
146
+ if (!line.startsWith('data:'))
147
+ continue;
148
+ const data = line.slice(5).trim();
149
+ if (data === '[DONE]')
150
+ continue;
151
+ let chunk;
152
+ try {
153
+ chunk = JSON.parse(data);
154
+ }
155
+ catch {
156
+ continue;
157
+ }
158
+ if (chunk.error)
159
+ throw new Refusal(502, chunk.error.message ?? 'The model stopped with an error.', chunk.error.code ?? '');
160
+ if (chunk.model)
161
+ model = chunk.model;
162
+ const delta = chunk.choices?.[0]?.delta;
163
+ if (delta?.content) {
164
+ text += delta.content;
165
+ heard(text);
166
+ }
167
+ for (const t of delta?.tool_calls ?? []) {
168
+ const had = calls.get(t.index) ?? { id: '', name: '', args: '' };
169
+ calls.set(t.index, {
170
+ id: t.id || had.id,
171
+ name: had.name + (t.function?.name ?? ''),
172
+ args: had.args + (t.function?.arguments ?? ''),
173
+ });
174
+ }
175
+ }
176
+ }
177
+ return { text, model, calls: [...calls.values()].filter((c) => c.name).map((c, i) => ({ ...c, id: c.id || `call_${i}` })) };
178
+ }
179
+ /** A JSON answer from a model, asked for by schema: what a generator (a type system, a deck's words) is made of. */
180
+ export async function structured(w, model, system, user, schema, signal) {
181
+ const out = await ask(w, 'api', '/v1/chat/completions', {
182
+ body: {
183
+ model,
184
+ messages: [
185
+ { role: 'system', content: system },
186
+ { role: 'user', content: user },
187
+ ],
188
+ response_format: { type: 'json_schema', json_schema: { name: 'answer', strict: true, schema } },
189
+ },
190
+ signal,
191
+ });
192
+ const text = out.choices?.[0]?.message?.content ?? '';
193
+ const json = text.slice(text.indexOf('{'), text.lastIndexOf('}') + 1);
194
+ try {
195
+ return JSON.parse(json);
196
+ }
197
+ catch {
198
+ throw new Refusal(502, 'The model answered with no JSON.');
199
+ }
200
+ }
201
+ /** A data: URL's bytes. */
202
+ export function bytes(url) {
203
+ const comma = url.indexOf(',');
204
+ const head = url.slice(5, comma);
205
+ const mime = head.split(';')[0] || 'image/png';
206
+ const bin = atob(url.slice(comma + 1));
207
+ const out = new Uint8Array(bin.length);
208
+ for (let i = 0; i < bin.length; i++)
209
+ out[i] = bin.charCodeAt(i);
210
+ return { blob: new Blob([out], { type: mime }), mime };
211
+ }
212
+ /**
213
+ * An image from a gateway image model: words, and the pictures it starts from
214
+ * (an edit). The image models are chat models with an image output, so this is
215
+ * a chat completion asking for both modalities; the pictures come back on the
216
+ * message (`images`) or among its content parts.
217
+ */
218
+ export async function draw(w, model, prompt, o = {}) {
219
+ const content = [{ type: 'text', text: prompt }, ...(o.from ?? []).map((url) => ({ type: 'image_url', image_url: { url } }))];
220
+ const out = await ask(w, 'api', '/v1/chat/completions', {
221
+ body: {
222
+ model,
223
+ modalities: ['image', 'text'],
224
+ messages: [{ role: 'user', content }],
225
+ ...(o.aspect ? { image_config: { aspect_ratio: o.aspect } } : {}),
226
+ },
227
+ signal: o.signal,
228
+ });
229
+ const m = out.choices?.[0]?.message;
230
+ const urls = [
231
+ ...(m?.images ?? []).map((i) => i.image_url?.url ?? ''),
232
+ ...(Array.isArray(m?.content) ? m.content.map((p) => (p.type === 'image_url' ? (p.image_url?.url ?? '') : '')) : []),
233
+ ].filter((u) => u.startsWith('data:image/'));
234
+ if (!urls.length)
235
+ throw new Refusal(502, `${model} answered with no picture.`);
236
+ return urls.map(bytes);
237
+ }
238
+ /** Speech from words: the platform's voice. */
239
+ export async function speak(w, input, voice, model, signal) {
240
+ return ask(w, 'api', '/v1/audio/speech', { body: { model, input, voice, response_format: 'mp3' }, signal });
241
+ }
242
+ /** The person the bearer names: whose database this is. */
243
+ function subject(w) {
244
+ const t = w.token();
245
+ if (!t)
246
+ return '';
247
+ try {
248
+ const part = t.split('.')[1].replace(/-/g, '+').replace(/_/g, '/');
249
+ const c = JSON.parse(atob(part + '='.repeat((4 - (part.length % 4)) % 4)));
250
+ return typeof c.sub === 'string' ? c.sub : '';
251
+ }
252
+ catch {
253
+ return '';
254
+ }
255
+ }
256
+ const dbs = new Map();
257
+ /** This person's database in this org, opened once. */
258
+ function db(w) {
259
+ const who = subject(w);
260
+ if (!who || !w.org)
261
+ return Promise.reject(new Refusal(401, 'Sign in to keep what you make.'));
262
+ const name = `hanzo-studio:${w.org}:${who}`;
263
+ let open = dbs.get(name);
264
+ if (!open) {
265
+ open = new Promise((resolve, reject) => {
266
+ const req = indexedDB.open(name, 1);
267
+ req.onupgradeneeded = () => req.result.createObjectStore('files', { keyPath: 'key' });
268
+ req.onsuccess = () => resolve(req.result);
269
+ req.onerror = () => reject(new Refusal(507, 'This browser would not keep the file.'));
270
+ });
271
+ dbs.set(name, open);
272
+ open.catch(() => dbs.delete(name));
273
+ }
274
+ return open;
275
+ }
276
+ function tx(d, mode, run) {
277
+ return new Promise((resolve, reject) => {
278
+ const req = run(d.transaction('files', mode).objectStore('files'));
279
+ req.onsuccess = () => resolve(req.result);
280
+ req.onerror = () => reject(new Refusal(507, 'This browser would not keep the file.'));
281
+ });
282
+ }
283
+ const get = async (w, key) => (await tx(await db(w), 'readonly', (s) => s.get(key))) ?? null;
284
+ /* The Library: answered or not, asked once per page. */
285
+ let library = null;
286
+ /** Whether api.hanzo.ai/v1/library answers for this person. */
287
+ export function libraryLive(w) {
288
+ library ??= ask(w, 'api', '/v1/library?limit=1&source=studio').then(() => true, (e) => {
289
+ library = null;
290
+ return e instanceof Refusal && e.status !== 404 && e.status !== 405 ? Promise.reject(e) : false;
291
+ });
292
+ return library.catch(() => false);
293
+ }
294
+ const KINDS_LIB = { image: 'image', poster: 'image', ad: 'image', video: 'video', voice: 'audio', music: 'audio', document: 'document', slides: 'slides', type: 'type', mesh: 'model' };
295
+ const base64 = async (b) => {
296
+ const buf = new Uint8Array(await b.arrayBuffer());
297
+ let bin = '';
298
+ for (let i = 0; i < buf.length; i += 0x8000)
299
+ bin += String.fromCharCode(...buf.subarray(i, i + 0x8000));
300
+ return btoa(bin);
301
+ };
302
+ /** Save one kept file to the Library, idempotently (its key is the idempotency key). */
303
+ async function shelve(w, r) {
304
+ const [, kind, chat] = r.key.split('/');
305
+ if (!r.key.startsWith('files/') || !kind || !chat || r.blob.size > 10 * 1024 * 1024)
306
+ return undefined;
307
+ const item = await ask(w, 'api', '/v1/library', {
308
+ body: {
309
+ name: r.key.split('/').pop(),
310
+ mime: r.mime,
311
+ data: await base64(r.blob),
312
+ source: 'studio',
313
+ kind: KINDS_LIB[kind] ?? 'file',
314
+ ref: chat,
315
+ origin: `/studio/c/${chat}`,
316
+ key: r.key,
317
+ },
318
+ });
319
+ return item.id;
320
+ }
321
+ /** Keep bytes at a key. Answers the key. */
322
+ export async function put(w, key, blob) {
323
+ const r = { key, blob, mime: blob.type || 'application/octet-stream', modified: Date.now() };
324
+ if (key.startsWith('files/') && (await libraryLive(w)))
325
+ r.lib = await shelve(w, r).catch(() => undefined);
326
+ await tx(await db(w), 'readwrite', (s) => s.put(r));
327
+ shown.delete(key);
328
+ return key;
329
+ }
330
+ /** Carry up to the Library whatever is kept only here. Answers how many went. */
331
+ export async function sync(w) {
332
+ if (!(await libraryLive(w)))
333
+ return 0;
334
+ const all = await tx(await db(w), 'readonly', (s) => s.getAll());
335
+ let n = 0;
336
+ for (const r of all.filter((x) => x.key.startsWith('files/') && !x.lib)) {
337
+ const lib = await shelve(w, r).catch(() => undefined);
338
+ if (!lib)
339
+ continue;
340
+ await tx(await db(w), 'readwrite', (s) => s.put({ ...r, lib }));
341
+ n++;
342
+ }
343
+ return n;
344
+ }
345
+ /** Addresses handed out, kept while they work: an object URL for the page's life, a Library URL until it expires. */
346
+ const shown = new Map();
347
+ /** An address a page can draw a kept file at. */
348
+ export async function url(w, key) {
349
+ const hit = shown.get(key);
350
+ if (hit && hit.until > Date.now())
351
+ return hit.url;
352
+ const r = await get(w, key);
353
+ if (!r)
354
+ throw new Refusal(404, 'This file is not kept in this browser.');
355
+ if (r.lib) {
356
+ const item = await ask(w, 'api', `/v1/library/${enc(r.lib)}`).catch(() => null);
357
+ if (item?.url) {
358
+ shown.set(key, { url: item.url, until: item.expires ? Date.parse(item.expires) - 60_000 : Date.now() + 240_000 });
359
+ return item.url;
360
+ }
361
+ }
362
+ const made = URL.createObjectURL(r.blob);
363
+ shown.set(key, { url: made, until: Number.POSITIVE_INFINITY });
364
+ return made;
365
+ }
366
+ /** A kept file's bytes. */
367
+ export async function read(w, key) {
368
+ const r = await get(w, key);
369
+ if (!r)
370
+ throw new Refusal(404, 'This file is not kept in this browser.');
371
+ return r.blob;
372
+ }
373
+ /** What is kept under a prefix (one that ends in '/'), keys relative to it. */
374
+ export async function list(w, prefix) {
375
+ const range = IDBKeyRange.bound(prefix, `${prefix}￿`);
376
+ const all = await tx(await db(w), 'readonly', (s) => s.getAll(range));
377
+ return all.map((r) => ({ key: r.key.slice(prefix.length), size: r.blob.size, modified: r.modified, shelved: Boolean(r.lib) }));
378
+ }
379
+ export async function remove(w, key) {
380
+ const r = await get(w, key);
381
+ if (r?.lib)
382
+ await ask(w, 'api', `/v1/library/${enc(r.lib)}`, { method: 'DELETE' }).catch(() => undefined);
383
+ await tx(await db(w), 'readwrite', (s) => s.delete(key));
384
+ const hit = shown.get(key);
385
+ if (hit?.url.startsWith('blob:'))
386
+ URL.revokeObjectURL(hit.url);
387
+ shown.delete(key);
388
+ }
389
+ /** A kept JSON document, or null when there is none. */
390
+ export async function load(w, key) {
391
+ const r = await get(w, key);
392
+ if (!r)
393
+ return null;
394
+ try {
395
+ return JSON.parse(await r.blob.text());
396
+ }
397
+ catch {
398
+ return null;
399
+ }
400
+ }
401
+ export const save = (w, key, value) => put(w, key, new Blob([JSON.stringify(value)], { type: 'application/json' }));
402
+ /** The org's GPUs and what each can run. A fleet the engine cannot read is an error, never an empty fleet. */
403
+ export const gpus = (w, signal) => ask(w, 'engine', '/v1/gpu-status', { signal });
404
+ /** Dispatch a lane to the org's GPUs (the engine's one funnel). */
405
+ export const dispatch = (w, lane, body, signal) => ask(w, 'engine', `/v1/library/${lane}`, { body, signal });
406
+ /** The engine's record of what was dispatched and what landed. */
407
+ export const worklog = (w, signal) => ask(w, 'engine', '/v1/worklog', { signal }).then((r) => r.items ?? []);
408
+ /** A landed file of the engine library, as bytes. */
409
+ export const landed = (w, path) => ask(w, 'engine', `/v1/library/file${query({ path })}`);
410
+ /** Documents and decks: the engine's renderers. */
411
+ export const documents = {
412
+ schema: (w) => ask(w, 'engine', '/v1/documents/schema'),
413
+ create: (w, body) => ask(w, 'engine', '/v1/documents', { body }),
414
+ generate: (w, body) => ask(w, 'engine', '/v1/documents/generate', { body }),
415
+ patch: (w, id, body) => ask(w, 'engine', `/v1/documents/${enc(id)}`, { method: 'PATCH', body }),
416
+ get: (w, id) => ask(w, 'engine', `/v1/documents/${enc(id)}`),
417
+ file: (w, id, format) => ask(w, 'engine', `/v1/documents/${enc(id)}/download${query({ format })}`),
418
+ };
419
+ /** Stage a picture in the engine's input folder for a lane that starts from one. Answers the name the lane takes. */
420
+ export async function stage(w, blob, name) {
421
+ const form = new FormData();
422
+ form.append('image', blob, name);
423
+ form.append('type', 'input');
424
+ form.append('overwrite', 'false');
425
+ const r = await ask(w, 'engine', '/v1/upload/image', { raw: form });
426
+ return r.subfolder ? `${r.subfolder}/${r.name}` : r.name;
427
+ }
428
+ /** A clip from a gateway video model: started, followed, read back (the async /v1/videos API). */
429
+ export async function film(w, model, prompt, o) {
430
+ const job = await ask(w, 'api', '/v1/videos/generations', { body: { model, prompt, size: o.size, seconds: o.seconds }, signal: o.signal });
431
+ const id = job.id ?? job.data?.[0]?.id;
432
+ if (!id)
433
+ throw new Refusal(502, `${model} took the clip but named no job.`);
434
+ const until = Date.now() + 20 * 60_000;
435
+ for (;;) {
436
+ const s = await ask(w, 'api', `/v1/videos/${enc(id)}`, { signal: o.signal });
437
+ const status = (s.status ?? '').toLowerCase();
438
+ if (['completed', 'succeeded', 'done'].includes(status))
439
+ break;
440
+ if (['failed', 'error', 'cancelled'].includes(status))
441
+ throw new Refusal(502, typeof s.error === 'string' ? s.error : (s.error?.message ?? `The clip ${status}.`));
442
+ if (Date.now() > until)
443
+ throw new Refusal(504, 'The clip is still rendering.');
444
+ o.doing(status === 'queued' ? 'Waiting for the video model' : 'Rendering the clip');
445
+ await new Promise((r) => setTimeout(r, 4000));
446
+ }
447
+ return ask(w, 'api', `/v1/videos/${enc(id)}/content`, { signal: o.signal });
448
+ }
449
+ /** The gateway's catalog of models that make something other than words. */
450
+ export async function makers(w, signal) {
451
+ const out = await ask(w, 'api', '/v1/models', { signal });
452
+ return (out.data ?? [])
453
+ .filter((m) => (m.outputs ?? []).some((o) => ['image', 'audio', 'video', '3d', 'mesh'].includes(o)))
454
+ .map((m) => ({ id: m.id, name: m.name ?? m.id, inputs: m.inputs ?? [], outputs: m.outputs ?? [] }));
455
+ }