@hanzo/studio 0.1.13 → 0.1.15

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/chat/turn.js CHANGED
@@ -172,8 +172,11 @@ export async function turn(start, asked, ctx) {
172
172
  catch (e) {
173
173
  if (e?.name === 'AbortError')
174
174
  set({ doing: undefined, failed: 'Stopped.' });
175
- else
176
- set({ doing: undefined, failed: words(e) });
175
+ else {
176
+ // A round that fails after a tool ran (the closing line, say) leaves what it made standing.
177
+ const n = mine().made?.length ?? 0;
178
+ set({ doing: undefined, failed: n ? `${words(e)} What this turn made is in its tab${n > 1 ? 's' : ''}.` : words(e) });
179
+ }
177
180
  }
178
181
  return chat;
179
182
  async function call(name, args) {
@@ -53,94 +53,6 @@ export const voice = {
53
53
  View: Listen,
54
54
  exports: [{ label: 'MP3', run: async (w, m, v) => ({ name: filename(m.title, 'mp3'), blob: await read(w, v.files[0].key) }) }],
55
55
  };
56
- /** Encode stereo float32 PCM samples into a standard 16-bit PCM WAV Blob. */
57
- function encodeWav(left, right, sampleRate) {
58
- const frames = left.length;
59
- const bytesPerSample = 2;
60
- const channels = 2;
61
- const dataBytes = frames * channels * bytesPerSample;
62
- const buf = new ArrayBuffer(44 + dataBytes);
63
- const view = new DataView(buf);
64
- const writeStr = (off, s) => {
65
- for (let i = 0; i < s.length; i++)
66
- view.setUint8(off + i, s.charCodeAt(i));
67
- };
68
- writeStr(0, 'RIFF');
69
- view.setUint32(4, 36 + dataBytes, true);
70
- writeStr(8, 'WAVE');
71
- writeStr(12, 'fmt ');
72
- view.setUint32(16, 16, true);
73
- view.setUint16(20, 1, true);
74
- view.setUint16(22, channels, true);
75
- view.setUint32(24, sampleRate, true);
76
- view.setUint32(28, sampleRate * channels * bytesPerSample, true);
77
- view.setUint16(32, channels * bytesPerSample, true);
78
- view.setUint16(34, 16, true);
79
- writeStr(36, 'data');
80
- view.setUint32(40, dataBytes, true);
81
- let offset = 44;
82
- for (let i = 0; i < frames; i++) {
83
- const l = Math.max(-1, Math.min(1, left[i]));
84
- const r = Math.max(-1, Math.min(1, right[i]));
85
- view.setInt16(offset, l < 0 ? l * 0x8000 : l * 0x7fff, true);
86
- view.setInt16(offset + 2, r < 0 ? r * 0x8000 : r * 0x7fff, true);
87
- offset += 4;
88
- }
89
- return new Blob([buf], { type: 'audio/wav' });
90
- }
91
- /** Synthesize a stereo instrumental music track in PCM WAV format tailored to the prompt. */
92
- function synthesizeMusicTrack(prompt, seconds) {
93
- const sr = 44100;
94
- const dur = Math.min(30, Math.max(10, seconds));
95
- const total = sr * dur;
96
- const left = new Float32Array(total);
97
- const right = new Float32Array(total);
98
- const lower = prompt.toLowerCase();
99
- const bpmMatch = lower.match(/(\d{2,3})\s*bpm/);
100
- const bpm = bpmMatch ? Math.min(180, Math.max(70, Number(bpmMatch[1]))) : lower.includes('ambient') || lower.includes('chill') ? 92 : 122;
101
- const beatSec = 60 / bpm;
102
- const stepSec = beatSec / 2;
103
- // Chord progression (Am9 -> Fmaj7 -> Cmaj9 -> G6)
104
- const chords = [
105
- [220.0, 261.63, 329.63, 392.0, 493.88],
106
- [174.61, 220.0, 261.63, 329.63, 392.0],
107
- [261.63, 329.63, 392.0, 493.88, 587.33],
108
- [196.0, 246.94, 293.66, 329.63, 392.0],
109
- ];
110
- for (let i = 0; i < total; i++) {
111
- const t = i / sr;
112
- const bar = Math.floor(t / (beatSec * 4)) % chords.length;
113
- const chord = chords[bar];
114
- const beatPhase = (t % beatSec) / beatSec;
115
- const stepIdx = Math.floor(t / stepSec);
116
- const stepPhase = (t % stepSec) / stepSec;
117
- // Warm stereo pad
118
- let padL = 0;
119
- let padR = 0;
120
- for (let c = 0; c < chord.length; c++) {
121
- const f = chord[c];
122
- padL += Math.sin(2 * Math.PI * (f * 1.001) * t + c * 0.4) * 0.08;
123
- padR += Math.sin(2 * Math.PI * (f * 0.999) * t + c * 0.7) * 0.08;
124
- }
125
- // Sub bass on 8th notes
126
- const bassFreq = chord[0] * 0.5;
127
- const bassEnv = Math.exp(-stepPhase * 4.5);
128
- const bass = Math.sin(2 * Math.PI * bassFreq * t) * 0.24 * bassEnv;
129
- // Arpeggio synth
130
- const arpFreq = chord[stepIdx % chord.length] * 2;
131
- const arpEnv = Math.exp(-stepPhase * 8);
132
- const arp = Math.sin(2 * Math.PI * arpFreq * t) * 0.1 * arpEnv;
133
- // Kick drum on every beat
134
- const kickEnv = Math.exp(-beatPhase * 14);
135
- const kickFreq = 48 + 110 * Math.exp(-beatPhase * 28);
136
- const kick = Math.sin(2 * Math.PI * kickFreq * t) * 0.28 * kickEnv;
137
- // Fade in / out envelope
138
- const fade = Math.min(1, t / 0.8, (dur - t) / 1.2);
139
- left[i] = (padL + bass + kick + arp * 0.8) * fade;
140
- right[i] = (padR + bass + kick + arp * 1.1) * fade;
141
- }
142
- return encodeWav(left, right, sr);
143
- }
144
56
  export const music = {
145
57
  id: 'music',
146
58
  label: 'Music',
@@ -160,28 +72,19 @@ export const music = {
160
72
  required: ['title', 'prompt'],
161
73
  },
162
74
  },
163
- needs: () => null,
75
+ // A song is generated on a GPU or not at all (hanzo-inc/cloud#754): a synth loop
76
+ // returned in its place played the same chords for every prompt.
77
+ needs: (a) => (lit(a.fleet, 'music.ace') ? null : 'Music runs on a GPU. Connect one with `hanzo link`, and it appears here when the GPU can run it.'),
164
78
  make: async (w, args) => {
79
+ const why = music.needs(w);
80
+ if (why)
81
+ throw new Error(why);
165
82
  const body = { prompt: str(args.prompt), lyrics: str(args.lyrics), seconds: num(args.seconds, 30, 10, 240) };
166
- if (lit(w.fleet, 'music.ace')) {
167
- try {
168
- const got = await render(w.wire, 'music', body, w.doing, w.signal);
169
- const g = got[0];
170
- const ext = g.path.split('.').pop() || 'mp3';
171
- const file = await w.keep(`music.${ext}`, g.blob);
172
- return { title: str(args.title, body.prompt.slice(0, 32)), draft: { spec: body, files: [file], note: body.prompt, by: 'ACE-Step 1.5 (your GPU)' } };
173
- }
174
- catch {
175
- // Fall through to Studio WebAudio Synthesizer
176
- }
177
- }
178
- w.doing('Synthesizing stereo music track');
179
- const wav = synthesizeMusicTrack(body.prompt, body.seconds);
180
- const file = await w.keep('music.wav', wav);
181
- return {
182
- title: str(args.title, body.prompt.slice(0, 32) || 'Studio Track'),
183
- draft: { spec: body, files: [file], note: body.prompt, by: 'Studio Audio Synth' },
184
- };
83
+ const got = await render(w.wire, 'music', body, w.doing, w.signal);
84
+ const g = got[0];
85
+ const ext = g.path.split('.').pop() || 'mp3';
86
+ const file = await w.keep(`music.${ext}`, g.blob);
87
+ return { title: str(args.title, body.prompt.slice(0, 32)), draft: { spec: body, files: [file], note: body.prompt, by: 'ACE-Step 1.5 (your GPU)' } };
185
88
  },
186
89
  revise: async (w, made, change) => {
187
90
  const now = current(made).spec;
package/lib/wire.d.ts CHANGED
@@ -35,6 +35,8 @@ export declare class Refusal extends Error {
35
35
  readonly code: string;
36
36
  constructor(status: number, message: string, code?: string);
37
37
  }
38
+ export declare const UNREACHED = "unreached";
39
+ export declare function unreached(since: number, partway?: boolean): Refusal;
38
40
  type Base = 'api' | 'engine';
39
41
  export interface Ask {
40
42
  method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
package/lib/wire.js CHANGED
@@ -30,6 +30,34 @@ export class Refusal extends Error {
30
30
  this.name = 'Refusal';
31
31
  }
32
32
  }
33
+ /*
34
+ * A request the connection lost. The browser says only "Failed to fetch" (or
35
+ * "network error" partway through a body), which tells a person nothing: the
36
+ * flyer that failed with it (hanzo-inc/app#302) was a chat turn the edge dropped while the
37
+ * model was still thinking, and the same turn went through when sent again.
38
+ * It is said as a Refusal with status 0 and the code `unreached`: how long it
39
+ * had waited, and what to do.
40
+ */
41
+ export const UNREACHED = 'unreached';
42
+ export function unreached(since, partway = false) {
43
+ const s = Math.max(1, Math.round((Date.now() - since) / 1000));
44
+ return new Refusal(0, partway
45
+ ? `The connection to Hanzo dropped ${s} s into the answer. Try again.`
46
+ : `Couldn't get an answer from Hanzo: the connection dropped after ${s} s. Check your connection and try again.`, UNREACHED);
47
+ }
48
+ const stopped = (e, signal) => Boolean(signal?.aborted) || e?.name === 'AbortError';
49
+ /** fetch, with a lost connection said as a Refusal. A stop is still the AbortError it was. */
50
+ async function reach(input, init = {}) {
51
+ const since = Date.now();
52
+ try {
53
+ return await fetch(input, init);
54
+ }
55
+ catch (e) {
56
+ if (stopped(e, init.signal))
57
+ throw e;
58
+ throw unreached(since);
59
+ }
60
+ }
33
61
  const origin = (w, base) => (base === 'api' ? w.api : w.engine);
34
62
  function headers(w, extra) {
35
63
  const h = new Headers(extra);
@@ -71,7 +99,7 @@ export async function ask(w, base, path, o = {}) {
71
99
  }
72
100
  else if (o.type)
73
101
  h.set('Content-Type', o.type);
74
- const res = await fetch(`${origin(w, base)}${path}`, {
102
+ const res = await reach(`${origin(w, base)}${path}`, {
75
103
  method: o.method ?? (body === undefined ? 'GET' : 'POST'),
76
104
  headers: h,
77
105
  body,
@@ -108,14 +136,24 @@ const enc = encodeURIComponent;
108
136
  * words so far as they arrive. Answers the words and the tool calls in full.
109
137
  */
110
138
  export async function complete(w, body, heard, signal) {
111
- const res = await fetch(`${w.api}/v1/chat/completions`, {
139
+ const init = {
112
140
  method: 'POST',
113
141
  headers: headers(w, { 'Content-Type': 'application/json', Accept: 'text/event-stream' }),
114
142
  body: JSON.stringify({ ...body, stream: true }),
115
143
  cache: 'no-store',
116
144
  credentials: 'omit',
117
145
  signal,
118
- });
146
+ };
147
+ let res;
148
+ try {
149
+ res = await reach(`${w.api}/v1/chat/completions`, init);
150
+ }
151
+ catch (e) {
152
+ if (!(e instanceof Refusal) || e.code !== UNREACHED)
153
+ throw e;
154
+ // Lost before any answer began, so nothing was said: asked once more.
155
+ res = await reach(`${w.api}/v1/chat/completions`, init);
156
+ }
119
157
  if (!res.ok || !res.body) {
120
158
  const [msg, code] = await reason(res);
121
159
  throw new Refusal(res.status, msg || `The model answered ${res.status}`, code);
@@ -131,12 +169,22 @@ export async function complete(w, body, heard, signal) {
131
169
  }
132
170
  const reader = res.body.getReader();
133
171
  const decoder = new TextDecoder();
172
+ const began = Date.now();
134
173
  let buffer = '';
135
174
  let text = '';
136
175
  let model = body.model;
137
176
  const calls = new Map();
138
177
  for (;;) {
139
- const { value, done } = await reader.read();
178
+ let read;
179
+ try {
180
+ read = await reader.read();
181
+ }
182
+ catch (e) {
183
+ if (stopped(e, signal))
184
+ throw e;
185
+ throw unreached(began, true);
186
+ }
187
+ const { value, done } = read;
140
188
  if (done)
141
189
  break;
142
190
  buffer += decoder.decode(value, { stream: true });
@@ -371,7 +419,7 @@ export async function saveToLibrary(w, key, blob) {
371
419
  }
372
420
  /** The bytes at an address a Library item gave, or the reason they did not come. */
373
421
  export async function fileAt(href) {
374
- const r = await fetch(href, { credentials: 'omit' });
422
+ const r = await reach(href, { credentials: 'omit' });
375
423
  if (!r.ok)
376
424
  throw new Error(`Download failed (HTTP ${r.status})`);
377
425
  return r.blob();
@@ -445,7 +493,7 @@ export async function read(w, key) {
445
493
  throw new Refusal(404, 'This file is not in your Library.');
446
494
  if (r.blob)
447
495
  return r.blob;
448
- const res = await fetch(await url(w, key), { cache: 'no-store', credentials: 'omit' });
496
+ const res = await reach(await url(w, key), { cache: 'no-store', credentials: 'omit' });
449
497
  if (!res.ok)
450
498
  throw new Refusal(res.status, 'The Library would not hand the file back.');
451
499
  const b = await res.blob();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/studio",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
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.",