File size: 24,008 Bytes
46f9035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03a6a46
 
46f9035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>MagentaRT Realtime Tester (rt-mode)</title>
  <style>
    :root { font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji"; }
    body { margin: 0; padding: 24px; background: #0b0b0f; color: #e6e6ea; }
    h1 { font-size: 20px; margin: 0 0 12px; }
    .card { background: #15151c; border: 1px solid #232334; border-radius: 12px; padding: 16px; margin-bottom: 16px; }
    label { display: block; font-size: 12px; color: #b0b0bb; margin: 8px 0 6px; }
    input[type="text"], textarea, input[type="number"] { width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid #2a2a3a; background: #0f0f14; color: #e6e6ea; }
    textarea { min-height: 72px; resize: vertical; }
    .row { display: grid; grid-template-columns: repeat(12, 1fr); gap: 12px; }
    .col-6 { grid-column: span 6; }
    .col-4 { grid-column: span 4; }
    .col-3 { grid-column: span 3; }
    .col-2 { grid-column: span 2; }
    .col-12 { grid-column: span 12; }
    .btn { appearance: none; padding: 10px 14px; border-radius: 10px; border: 1px solid #2a2a3a; background: #20202a; color: #fff; cursor: pointer; }
    .btn:hover { background: #2a2a36; }
    .btn[disabled] { opacity: 0.5; cursor: not-allowed; }
    .controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
    .small { font-size: 12px; color: #a6a6b3; }
    .range-row { display: grid; grid-template-columns: 100px 1fr 60px; gap: 10px; align-items: center; }
    input[type="range"] { width: 100%; }
    .log { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; background: #0f0f14; border: 1px solid #2a2a3a; padding: 10px; border-radius: 8px; min-height: 160px; white-space: pre-wrap; overflow: auto; }
    .ok { color: #9fe870; }
    .warn { color: #ffda6b; }
    .err { color: #ff8080; }
    .badge { padding: 2px 6px; border-radius: 999px; background: #2a2a3a; font-size: 12px; }
    .sep { height: 1px; background: #212133; margin: 12px 0; }
  </style>
</head>
<body>
  <h1>MagentaRT Realtime Tester <span class="badge">rt-mode</span></h1>

  <div class="card">
    <div class="row">
      <div class="col-12"><div class="small">Model selection</div></div>
      <div class="col-6">
        <label>Checkpoint repo (HF)</label>
        <input id="selRepo" type="text" placeholder="e.g., thepatch/magenta-ft" />
      </div>
      <div class="col-3">
        <label>Checkpoint step</label>
        <input id="selStep" type="number" min="0" step="1" placeholder="e.g., 1863001" />
      </div>
      <div class="col-3">
        <label>Base size</label>
        <input id="selSize" type="text" placeholder="large" value="large" />
      </div>
      <div class="col-12 controls">
        <label class="small"><input id="chkBase" type="checkbox" /> Use base model (no checkpoint)</label>
        <label class="small"><input id="selPrewarm" type="checkbox" checked /> Prewarm before returning</label>
        <label class="small"><input id="selStopActive" type="checkbox" checked /> Stop any active jam</label>
        <button id="btnSelectModel" class="btn">Select model & warm up</button>
        <span id="selStatus" class="small"></span>
      </div>
    </div>
  </div>

  <div id="healthBanner" class="small" style="margin:8px 0 12px;"></div>

  <div class="card">
    <div class="row">
      <div class="col-12">
        <label>WebSocket URL</label>
        <input id="wsUrl" type="text" value="wss://thecollabagepatch-magenta-retry.hf.space/ws/jam" />
      </div>
      <div class="col-12 controls">
        <button id="btnStart" class="btn">Start</button>
        <button id="btnStop" class="btn" disabled>Stop</button>
        <button id="btnPing" class="btn">Ping</button>
        <label class="small"><input id="chkBinary" type="checkbox" /> Receive binary WAV frames</label>
        <label class="small"><input id="chkAutoUpdate" type="checkbox" checked /> Auto-update on slider change (150ms debounce)</label>
        <label class="small"><input id="chkLogAudio" type="checkbox" /> Log chunk sizes</label>
        <label class="small"><input id="chkRealtime" type="checkbox" checked /> Ask server to pace real-time</label>
        <!-- <label class="small"><input id="chkBootstrap" type="checkbox" checked /> Bootstrap fast (ASAP → flip to realtime)</label>
        <label class="small">Pre-roll chunks: <input id="numPreroll" type="number" min="0" max="12" step="1" value="3" style="width:60px"></label> -->
      </div>
    </div>
  </div>

  <div class="card">
    <div class="row">
      <div class="col-6">
        <div class="range-row">
          <label>Temperature</label>
          <input id="rngTemp" type="range" min="0.10" max="2.00" step="0.01" value="1.10" />
          <input id="numTemp" type="number" min="0.10" max="2.00" step="0.01" value="1.10" />
        </div>
        <div class="range-row">
          <label>Guidance</label>
          <input id="rngGuid" type="range" min="0.0" max="8.0" step="0.1" value="1.10" />
          <input id="numGuid" type="number" min="0.0" max="8.0" step="0.1" value="1.10" />
        </div>
        <div class="range-row">
          <label>TopK</label>
          <input id="rngTopk" type="range" min="1" max="256" step="1" value="40" />
          <input id="numTopk" type="number" min="1" max="256" step="1" value="40" />
        </div>
        <div class="range-row">
          <label>Volume</label>
          <input id="rngVol" type="range" min="0" max="1" step="0.01" value="1" />
          <input id="numVol" type="number" min="0" max="1" step="0.01" value="1" />
        </div>
        <div class="sep"></div>
        <button id="btnUpdate" class="btn">Send Update Now</button>
      </div>
      <div class="col-6">
        <label>Styles (comma-separated or prompt text)</label>
        <textarea id="txtStyles" placeholder="e.g., acid house, techno">warmup</textarea>
        <label>Style weights (comma-separated, optional)</label>
        <input id="txtStyleWeights" type="text" placeholder="e.g., 1.0, 0.5" />
        <label class="small"><input id="chkUseMixStyle" type="checkbox" /> Use current mix as style</label>
      </div>
    </div>
  </div>

    <div class="card" id="finetuneControls" style="display:none;">
    <div class="row">
      <div class="col-12"><div class="small">In-distribution steering</div></div>

      <div class="col-6">
        <div class="range-row">
          <label>Mean</label>
          <input id="rngMean" type="range" min="0.0" max="2.0" step="0.01" value="1.00" />
          <input id="numMean" type="number" min="0.0" max="2.0" step="0.01" value="1.00" />
        </div>

        <div class="sep"></div>

        <div class="range-row">
          <label>Centroid 1</label>
          <input id="rngC1" type="range" min="0.0" max="2.0" step="0.01" value="0.00" />
          <input id="numC1" type="number" min="0.0" max="2.0" step="0.01" value="0.00" />
        </div>
        <div class="range-row">
          <label>Centroid 2</label>
          <input id="rngC2" type="range" min="0.0" max="2.0" step="0.01" value="0.00" />
          <input id="numC2" type="number" min="0.0" max="2.0" step="0.01" value="0.00" />
        </div>
        <div class="range-row">
          <label>Centroid 3</label>
          <input id="rngC3" type="range" min="0.0" max="2.0" step="0.01" value="0.00" />
          <input id="numC3" type="number" min="0.0" max="2.0" step="0.01" value="0.00" />
        </div>
        <div class="range-row">
          <label>Centroid 4</label>
          <input id="rngC4" type="range" min="0.0" max="2.0" step="0.01" value="0.00" />
          <input id="numC4" type="number" min="0.0" max="2.0" step="0.01" value="0.00" />
        </div>
        <div class="range-row">
          <label>Centroid 5</label>
          <input id="rngC5" type="range" min="0.0" max="2.0" step="0.01" value="0.00" />
          <input id="numC5" type="number" min="0.0" max="2.0" step="0.01" value="0.00" />
        </div>
      </div>

      <div class="col-6">
        <p class="small">
          These steer the style embedding toward your finetune distribution.
          <br/>Mean defaults to 1.0 (on); centroids default to 0.0 (off).
          <br/>Adjust live while jamming.
        </p>
      </div>
    </div>
  </div>


  <div class="card">
    <div class="row">
      <div class="col-6">
        <div class="small">Audio Status</div>
        <div id="status">stopped</div>
      </div>
      <div class="col-6">
        <div class="small">Queue</div>
        <div id="queue">0 buffers, 0.00s scheduled</div>
      </div>
      <div class="col-12">
        <label>Log</label>
        <div id="log" class="log"></div>
      </div>
    </div>
  </div>

<script>
(() => {
  const $ = (id) => document.getElementById(id);
  const wsUrl = $("wsUrl");
  const btnStart = $("btnStart");
  const btnStop = $("btnStop");
  const btnPing = $("btnPing");
  const btnUpdate = $("btnUpdate");
  const chkBinary = $("chkBinary");
  const chkAutoUpdate = $("chkAutoUpdate");
  const chkLogAudio = $("chkLogAudio");
  const rngTemp = $("rngTemp"), numTemp = $("numTemp");
  const rngGuid = $("rngGuid"), numGuid = $("numGuid");
  const rngTopk = $("rngTopk"), numTopk = $("numTopk");
  const rngVol = $("rngVol"), numVol = $("numVol");
  const txtStyles = $("txtStyles");
  const txtStyleWeights = $("txtStyleWeights");
  const chkUseMixStyle = $("chkUseMixStyle");
  const statusEl = $("status");
  const queueEl = $("queue");
  const logEl = $("log");

  const rngMean = $("rngMean"), numMean = $("numMean");
  const rngC1 = $("rngC1"), numC1 = $("numC1");
  const rngC2 = $("rngC2"), numC2 = $("numC2");
  const rngC3 = $("rngC3"), numC3 = $("numC3");
  const rngC4 = $("rngC4"), numC4 = $("numC4");
  const rngC5 = $("rngC5"), numC5 = $("numC5");

  const XFADE_MS = 25; // crossfade length

  let pending = [];            // decoded AudioBuffers waiting to be scheduled
let playing = false;         // have we started playback?
const START_CUSHION = 0.12;  // already used

const fade = XFADE_MS / 1000;

function scheduleAudioBuffer(abuf) {
  // Overlap-add crossfade scheduling (same as your scheduleWavBytes but taking a decoded buffer)
  const src = ctx.createBufferSource();
  const g = ctx.createGain();
  src.buffer = abuf;
  src.connect(g); g.connect(gain);

  if (nextTime < ctx.currentTime + 0.05) nextTime = ctx.currentTime + START_CUSHION;
  const startAt = nextTime;
  const dur = abuf.duration;

  // Overlap by 'fade' so there’s no dip
  nextTime = startAt + Math.max(0, dur - fade);

  g.gain.setValueAtTime(0.0, startAt);
  g.gain.linearRampToValueAtTime(1.0, startAt + fade);
  g.gain.setValueAtTime(1.0, startAt + Math.max(0, dur - fade));
  g.gain.linearRampToValueAtTime(0.0, startAt + dur);

  src.start(startAt);
  scheduled.push({ src, when: startAt, dur });
  updateQueueUI();
  src.onended = () => { scheduled = scheduled.filter(s => s.src !== src); updateQueueUI(); };
}

function beginPlaybackFromPending() {
  if (playing) return;
  playing = true;
  nextTime = ctx.currentTime + START_CUSHION;
  while (pending.length) {
    const abuf = pending.shift();
    scheduleAudioBuffer(abuf);
  }
  // Flip server pacing to realtime after we’ve started, if bootstrapping was enabled
  if ($("chkBootstrap").checked) {
    try { ws?.send(JSON.stringify({ type: "update", pace: "realtime" })); } catch {}
  }
}

  // Audio chain
  let AudioCtx = window.AudioContext || window.webkitAudioContext;
  let ctx = null;
  let gain = null;
  let nextTime = 0;
  let scheduled = []; // [{src, when, dur}]
  let ws = null;
  let connected = false;
  let autoUpdateTimer = null;

  function log(msg, cls) {
    const line = document.createElement("div");
    line.textContent = msg;
    if (cls) line.className = cls;
    logEl.appendChild(line);
    logEl.scrollTop = logEl.scrollHeight;
  }

  function setStatus(txt) {
    statusEl.textContent = txt;
  }

  function updateQueueUI() {
    const total = scheduled.reduce((acc, s) => acc + s.dur, 0);
    queueEl.textContent = `${scheduled.length} buffers, ${total.toFixed(2)}s scheduled`;
  }

  function clearSchedule() {
    scheduled.forEach(s => { try { s.src.stop(); } catch(e){} });
    scheduled = [];
    pending = [];
    playing = false;
    nextTime = 0;
    updateQueueUI();
  }

  function base64ToArrayBuffer(b64) {
    const bin = atob(b64);
    const len = bin.length;
    const buf = new ArrayBuffer(len);
    const view = new Uint8Array(buf);
    for (let i = 0; i < len; i++) view[i] = bin.charCodeAt(i);
    return buf;
  }


async function scheduleWavBytes(arrayBuffer) {
  if (!ctx) return;
  try {
    const abuf = await ctx.decodeAudioData(arrayBuffer);
    if (!abuf) return;

    const need = parseInt($("numPreroll").value || "0", 10);
    if (!playing) {
      pending.push(abuf);
      queueEl.textContent = `${pending.length} pending, 0.00s scheduled`;
      // start once we hit the threshold
      if (pending.length >= need) beginPlaybackFromPending();
      return;
    }

    // already playing → schedule immediately
    scheduleAudioBuffer(abuf);
  } catch (e) {
    log("decode error: " + e.message, "err");
  }
}

  function currentParams() {
    return {
      temperature: parseFloat(numTemp.value),
      topk: parseInt(numTopk.value, 10),
      guidance_weight: parseFloat(numGuid.value),
      styles: txtStyles.value,
      style_weights: txtStyleWeights.value,
      use_current_mix_as_style: !!chkUseMixStyle.checked,

      // NEW:
      mean: parseFloat(numMean.value),
      centroid_weights: centroidWeightsCSV(),
    };
  }

  function sendUpdate() {
    if (!ws || ws.readyState !== 1) return;
    const msg = { type: "update", ...currentParams() };
    ws.send(JSON.stringify(msg));
    log("→ update " + JSON.stringify(msg), "small");
  }

  function debouncedUpdate() {
    if (!chkAutoUpdate.checked) return;
    if (autoUpdateTimer) clearTimeout(autoUpdateTimer);
    autoUpdateTimer = setTimeout(sendUpdate, 150);
  }

  function linkRangeNumber(range, number, cb) {
    const sync = (fromRange) => {
      if (fromRange) number.value = range.value;
      else range.value = number.value;
      cb?.();
    };
    range.addEventListener("input", () => { sync(true); debouncedUpdate(); });
    number.addEventListener("input", () => { sync(false); debouncedUpdate(); });
    sync(true);
  }

    function centroidWeightsCSV() {
    const vals = [numC1, numC2, numC3, numC4, numC5].map(n => {
      const v = parseFloat(n.value);
      return Number.isFinite(v) ? v : 0;
    });
    // Trim trailing zeros to avoid sending long tails of 0.00s
    let end = vals.length;
    while (end > 0 && Math.abs(vals[end-1]) < 1e-9) end--;
    return vals.slice(0, end).join(",");
  }

  // Wire sliders
  linkRangeNumber(rngTemp, numTemp);
  linkRangeNumber(rngGuid, numGuid);
  linkRangeNumber(rngTopk, numTopk);
  linkRangeNumber(rngVol,  numVol, () => { if (gain) gain.gain.value = parseFloat(numVol.value); });

  linkRangeNumber(rngMean, numMean);
  linkRangeNumber(rngC1, numC1);
  linkRangeNumber(rngC2, numC2);
  linkRangeNumber(rngC3, numC3);
  linkRangeNumber(rngC4, numC4);
  linkRangeNumber(rngC5, numC5);

  async function start() {
    if (connected) return;
    if (!AudioCtx) { alert("Web Audio API not supported"); return; }
    pending = [];
    playing = false;
    ctx = ctx || new AudioCtx();
    await ctx.resume();
    gain = ctx.createGain();
    gain.gain.value = parseFloat(numVol.value);
    gain.connect(ctx.destination);
    clearSchedule();

    ws = new WebSocket(wsUrl.value);
    ws.binaryType = chkBinary.checked ? "arraybuffer" : "blob"; // arraybuffer for raw, blob for JSON string frames
    setStatus("connecting...");
    log("connecting " + wsUrl.value);

    ws.onopen = () => {
      connected = true;
      btnStart.disabled = true;
      btnStop.disabled = false;
      setStatus("connected");

      const msg = {
        type: "start",
        mode: "rt",
        binary_audio: !!chkBinary.checked,
        params: {
          ...currentParams(),
          pace: $("chkBootstrap").checked
            ? "asap"       // build pre-roll as fast as possible, then we’ll flip
            : ($("chkRealtime").checked ? "realtime" : "asap")
        }
      };
      ws.send(JSON.stringify(msg));
      log("→ start " + JSON.stringify(msg), "ok");
      nextTime = ctx.currentTime + 0.12;
    };

    ws.onmessage = async (ev) => {
      try {
        if (typeof ev.data === "string") {
          // JSON (e.g., chunk with base64, status, errors)
          const msg = JSON.parse(ev.data);
          if (msg.type === "chunk" && msg.audio_base64) {
            const buf = base64ToArrayBuffer(msg.audio_base64);
            if (chkLogAudio.checked) log(`chunk (b64) ${buf.byteLength} bytes`, "small");
            scheduleWavBytes(buf);
          } else if (msg.type === "chunk_meta") {
            // meta for previous binary frame
          } else if (msg.type === "status") {
            log("status: " + JSON.stringify(msg), "small");
          } else if (msg.type === "started") {
            log("started: " + JSON.stringify(msg), "ok");
          } else if (msg.type === "error") {
            log("error: " + msg.error, "err");
          } else {
            log("msg: " + JSON.stringify(msg), "small");
          }
        } else if (ev.data instanceof Blob) {
          // Could be binary WAV frame if server sent bytes, or a JSON blob
          const ab = await ev.data.arrayBuffer();
          // Try to sniff if this is JSON (starts with '{')
          if (ab.byteLength > 0 && new Uint8Array(ab, 0, 1)[0] === 0x7b) {
            const txt = new TextDecoder().decode(ab);
            const msg = JSON.parse(txt);
            if (msg.type === "chunk_meta") {
              // ignore for now
            } else {
              log("blob-json: " + txt, "small");
            }
          } else {
            if (chkLogAudio.checked) log(`chunk (bin) ${ab.byteLength} bytes`, "small");
            scheduleWavBytes(ab);
          }
        } else if (ev.data instanceof ArrayBuffer) {
          const ab = ev.data;
          if (chkLogAudio.checked) log(`chunk (ab) ${ab.byteLength} bytes`, "small");
          scheduleWavBytes(ab);
        }
      } catch (e) {
        log("onmessage error: " + e.message, "err");
      }
    };

    ws.onclose = () => {
      connected = false;
      btnStart.disabled = false;
      btnStop.disabled = true;
      setStatus("closed");
      log("connection closed", "warn");
    };

    ws.onerror = (e) => {
      log("ws error", "err");
    };
  }

  function stop() {
    if (!connected) return;
    try {
      ws?.send(JSON.stringify({ type: "stop" }));
    } catch {}
    ws?.close();
    connected = false;
    btnStart.disabled = false;
    btnStop.disabled = true;
    setStatus("stopped");
    clearSchedule();
    log("stopped", "warn");
  }

  btnStart.addEventListener("click", start);
  btnStop.addEventListener("click", stop);
  btnPing.addEventListener("click", () => { try { ws?.send(JSON.stringify({ type: "ping"})); } catch {} });
  btnUpdate.addEventListener("click", sendUpdate);
  // --- Health check & UI gating ---
  const banner = document.getElementById("healthBanner");
  async function checkHealthAndGate() {
    try {
      const r = await fetch("/health", {cache: "no-store"});
      const data = await r.json().catch(() => ({}));
      if (!r.ok || data.ok === false) {
        const msg = data?.message || "Service unavailable";
        banner.innerHTML = `<div style="padding:10px;border-radius:8px;background:#3a1010;border:1px solid #712020">
          <strong>Not ready:</strong> ${msg}<br><small>Status: ${data?.status || r.status}</small>
        </div>`;
        // disable controls
        ["btnStart","btnSelectModel"].forEach(id => { const el = $(id); if (el) el.disabled = true; });
      } else {
        banner.innerHTML = `<div style="padding:10px;border-radius:8px;background:#0f2d17;border:1px solid #1f6a37">
          <strong>Ready</strong> — mode: ${data.mode}, warmed: ${data.warmed}
        </div>`;
        ["btnStart","btnSelectModel"].forEach(id => { const el = $(id); if (el) el.disabled = false; });
      }
    } catch (e) {
      banner.innerHTML = `<div style="padding:10px;border-radius:8px;background:#3a2a10;border:1px solid #715820">
        Health check failed.
      </div>`;
    }
  }

  // --- Model/asset helpers (with base toggle) ---
  const selRepo = $("selRepo");
  const selStep = $("selStep");
  const selSize = $("selSize");
  const selPrewarm = $("selPrewarm");
  const selStopActive = $("selStopActive");
  const btnSelectModel = $("btnSelectModel");
  const selStatus = $("selStatus");
  const chkBase = $("chkBase");

  function updateBaseToggleUI() {
    const base = !!(chkBase && chkBase.checked);
    if (selRepo) { selRepo.disabled = base; selRepo.parentElement.style.opacity = base ? 0.5 : 1; }
    if (selStep) { selStep.disabled = base; selStep.parentElement.style.opacity = base ? 0.5 : 1; }
  }
  if (chkBase) {
    chkBase.addEventListener("change", updateBaseToggleUI);
    updateBaseToggleUI();
  }

  // Default ws URL to current origin if empty/hardcoded
  try {
    const defWs = (location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/ws/jam";
    if (!wsUrl.value || /hf\.space/.test(wsUrl.value)) wsUrl.value = defWs;
  } catch {}

  async function refreshFinetuneControls() {
    try {
      const r = await fetch("/model/config", { cache: "no-store" });
      if (!r.ok) throw new Error("config status " + r.status);
      const cfg = await r.json();
      // Use in-memory flags from backend
      const show = !!(cfg.mean_loaded || cfg.centroids_loaded);
      const el = document.getElementById("finetuneControls");
      if (el) el.style.display = show ? "block" : "none";
      if (!show) log("finetune assets not detected — hiding steering controls", "small");
    } catch (e) {
      const el = document.getElementById("finetuneControls");
      if (el) el.style.display = "none";
      log("config fetch failed: " + e.message, "warn");
    }
  }

  async function selectModel() {
    selStatus.textContent = "selecting...";
    try {
      const useBase = !!(chkBase && chkBase.checked);
      const payload = {
        size: (selSize?.value || undefined),
        prewarm: !!(selPrewarm && selPrewarm.checked),
        stop_active: !!(selStopActive && selStopActive.checked)
      };
      if (useBase) {
        // Signal stock model: step="none" as per backend's selector
        payload["step"] = "none";
      } else {
        if (selRepo?.value) payload["repo_id"] = selRepo.value;
        if (selStep?.value) payload["step"] = parseInt(selStep.value, 10);
      }
      const resp = await fetch("/model/select", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(payload)
      });
      const data = await resp.json().catch(() => ({}));
      if (!resp.ok || data.ok === false) {
        const msg = data?.error || (resp.status + " " + resp.statusText);
        selStatus.innerHTML = '<span class="err">error: ' + msg + '</span>';
        log("model/select failed: " + msg, "err");
        return;
      }
      selStatus.innerHTML = '<span class="ok">selected' + (data.warmup_done ? " + warmed" : "") + "</span>";
      log("model/select ok: " + JSON.stringify(data), "ok");
      await refreshFinetuneControls();
    } catch (e) {
      selStatus.innerHTML = '<span class="err">error: ' + e.message + "</span>";
      log("model/select exception: " + e.message, "err");
    }
  }

  if (btnSelectModel) btnSelectModel.addEventListener("click", selectModel);

  // Run once on load
  checkHealthAndGate();
  refreshFinetuneControls();

})();
</script>
</body>
</html>