@hanzo/studio 0.1.0 → 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/lib/gpu.js CHANGED
@@ -22,7 +22,7 @@ export async function render(w, lane, body, doing, signal) {
22
22
  for (;;) {
23
23
  if (signal.aborted)
24
24
  throw new DOMException('aborted', 'AbortError');
25
- row = (await worklog(w, signal)).find((r) => r.prompt_id === id || r.id === id || r.pid === id);
25
+ row = (await worklog(w, signal)).find((r) => r.prompt_id === id || r.id === id);
26
26
  const status = row?.status ?? 'queued';
27
27
  if (status === 'done')
28
28
  break;
package/lib/wire.d.ts CHANGED
@@ -44,6 +44,8 @@ export interface Ask {
44
44
  type?: string;
45
45
  signal?: AbortSignal;
46
46
  head?: Record<string, string>;
47
+ /** Reads the headers of an answer that succeeded. */
48
+ heard?: (h: Headers) => void;
47
49
  }
48
50
  /** One request. JSON in, JSON out, unless the answer is not JSON. */
49
51
  export declare function ask<T>(w: Wire, base: Base, path: string, o?: Ask): Promise<T>;
package/lib/wire.js CHANGED
@@ -83,6 +83,7 @@ export async function ask(w, base, path, o = {}) {
83
83
  const [msg, code] = await reason(res);
84
84
  throw new Refusal(res.status, msg || `${path.split('?')[0]} answered ${res.status}`, code);
85
85
  }
86
+ o.heard?.(res.headers);
86
87
  if (res.status === 204)
87
88
  return undefined;
88
89
  const type = res.headers.get('content-type') ?? '';
@@ -216,6 +217,7 @@ export function bytes(url) {
216
217
  * message (`images`) or among its content parts.
217
218
  */
218
219
  export async function draw(w, model, prompt, o = {}) {
220
+ let held;
219
221
  const content = [{ type: 'text', text: prompt }, ...(o.from ?? []).map((url) => ({ type: 'image_url', image_url: { url } }))];
220
222
  const out = await ask(w, 'api', '/v1/chat/completions', {
221
223
  body: {
@@ -225,6 +227,7 @@ export async function draw(w, model, prompt, o = {}) {
225
227
  ...(o.aspect ? { image_config: { aspect_ratio: o.aspect } } : {}),
226
228
  },
227
229
  signal: o.signal,
230
+ heard: (h) => (held = h),
228
231
  });
229
232
  const m = out.choices?.[0]?.message;
230
233
  const urls = [
@@ -232,9 +235,29 @@ export async function draw(w, model, prompt, o = {}) {
232
235
  ...(Array.isArray(m?.content) ? m.content.map((p) => (p.type === 'image_url' ? (p.image_url?.url ?? '') : '')) : []),
233
236
  ].filter((u) => u.startsWith('data:image/'));
234
237
  if (!urls.length)
235
- throw new Refusal(502, `${model} answered with no picture.`);
238
+ throw withheld(model, held);
236
239
  return urls.map(bytes);
237
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
+ }
238
261
  /** Speech from words: the platform's voice. */
239
262
  export async function speak(w, input, voice, model, signal) {
240
263
  return ask(w, 'api', '/v1/audio/speech', { body: { model, input, voice, response_format: 'mp3' }, signal });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/studio",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "license": "BSD-3-Clause",
6
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.",