@streamoji/aitwin 0.3.7 → 0.3.8

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/dist/aitwin.js CHANGED
@@ -1,42 +1,42 @@
1
1
  import { jsxs as yr, jsx as yn } from "react/jsx-runtime";
2
- import { forwardRef as _r, useRef as O, useState as at, useEffect as Ce, useCallback as Se, useMemo as _n, useImperativeHandle as Sr } from "react";
2
+ import { forwardRef as _r, useRef as U, useState as ot, useEffect as Le, useCallback as ke, useMemo as _n, useImperativeHandle as Sr } from "react";
3
3
  import { PipecatClient as kr } from "@pipecat-ai/client-js";
4
- import { WebSocketTransport as br, DailyMediaManager as vr, ProtobufFrameSerializer as Er } from "@pipecat-ai/websocket-transport";
4
+ import { WebSocketTransport as vr, DailyMediaManager as br, ProtobufFrameSerializer as Er } from "@pipecat-ai/websocket-transport";
5
5
  const Tr = "https://aitwin.bubu.social", Wn = {
6
6
  publicBase: Tr
7
- }, Ar = "0.3.7", Ir = {
7
+ }, Ar = "0.3.8", Ir = {
8
8
  version: Ar
9
- }, Mr = {}, lt = "https://ai.aitwin.me", jt = `${Wn.publicBase}/custom-faces`, Pr = `${Wn.publicBase}/aitwin-workers/v${Ir.version}/visemeDiffPreview.worker.js`;
9
+ }, Mr = {}, ft = "https://ai.aitwin.me", zt = `${Wn.publicBase}/custom-faces`, Pr = `${Wn.publicBase}/aitwin-workers/v${Ir.version}/visemeDiffPreview.worker.js`;
10
10
  function Rr(e) {
11
11
  if (e) return e;
12
12
  const t = typeof import.meta < "u" ? Mr?.VITE_AITWIN_WORKER_URL : void 0;
13
13
  return t?.trim() ? t.trim() : Pr;
14
14
  }
15
- const Dr = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin", Ct = 3e3, ut = 16, Sn = 3e3, xr = 4e3, Br = 0.85, Cr = 8, zt = 400, Hn = "eng_a7f2b9c1", Yt = "eng_d4e8f3a2", Xe = "eng_c9b1e6d4", je = Xe, $n = /* @__PURE__ */ new Set([
16
- Yt,
17
- Xe
15
+ const Dr = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin", Ft = 3e3, lt = 16, Sn = 3e3, xr = 4e3, Cr = 0.85, Br = 8, Yt = 400, Hn = "eng_a7f2b9c1", Jt = "eng_d4e8f3a2", Qe = "eng_c9b1e6d4", ze = Qe, $n = /* @__PURE__ */ new Set([
16
+ Jt,
17
+ Qe
18
18
  ]), qn = {
19
19
  [Hn]: "oculus_direct",
20
- [Xe]: "oculus_direct",
21
- [Yt]: "inworld_word"
20
+ [Qe]: "oculus_direct",
21
+ [Jt]: "inworld_word"
22
22
  }, kn = {
23
23
  google: Hn,
24
- inworld: Yt,
25
- cartesia: Xe
24
+ inworld: Jt,
25
+ cartesia: Qe
26
26
  };
27
27
  function Fr(e) {
28
28
  return qn[e];
29
29
  }
30
- function Lr(e) {
31
- return e === Xe;
32
- }
33
30
  function Or(e) {
31
+ return e === Qe;
32
+ }
33
+ function Lr(e) {
34
34
  if (e.ttsEngineId && e.ttsEngineId in qn)
35
35
  return e.ttsEngineId;
36
36
  const t = e.tts?.toLowerCase();
37
- return t && t in kn ? kn[t] : je;
37
+ return t && t in kn ? kn[t] : ze;
38
38
  }
39
- class Ut extends Error {
39
+ class Vt extends Error {
40
40
  constructor(t = "AI twin not found") {
41
41
  super(t), this.name = "AiTwinNotFoundError";
42
42
  }
@@ -46,21 +46,21 @@ async function Nr(e, t = Dr) {
46
46
  n.searchParams.set("id", e);
47
47
  const r = await fetch(n.toString());
48
48
  if (r.status === 404)
49
- throw new Ut();
49
+ throw new Vt();
50
50
  if (!r.ok)
51
51
  throw new Error(`getAiTwin failed (${r.status})`);
52
52
  const s = await r.json();
53
53
  if (!s.success)
54
- throw new Ut(s.error ?? "AI twin not found");
55
- const { tts: i, token: a, ...o } = s.data, f = String(o.authToken ?? a ?? "").trim() || void 0;
54
+ throw new Vt(s.error ?? "AI twin not found");
55
+ const { tts: i, token: a, ...o } = s.data, d = String(o.authToken ?? a ?? "").trim() || void 0;
56
56
  return {
57
57
  ...o,
58
- ttsEngineId: Or(s.data),
59
- authToken: f
58
+ ttsEngineId: Lr(s.data),
59
+ authToken: d
60
60
  };
61
61
  }
62
62
  const Ur = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken", Vr = "client_6TNvp3SCs4Og0a1ijm9TommXLql1", Wr = "kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1", Hr = "6TNvp3SCs4Og0a1ijm9TommXLql1", $r = "Swaraj Mali";
63
- async function Ft() {
63
+ async function Ot() {
64
64
  const e = await fetch(Ur, {
65
65
  method: "POST",
66
66
  headers: {
@@ -80,7 +80,7 @@ async function Ft() {
80
80
  throw new Error("getAuthToken returned no token");
81
81
  return n;
82
82
  }
83
- async function qr(e, t = lt) {
83
+ async function qr(e, t = ft) {
84
84
  const n = await fetch(`${t.replace(/\/$/, "")}/api/session-value`, {
85
85
  headers: { Authorization: `Bearer ${e}` }
86
86
  });
@@ -98,7 +98,7 @@ function Kr(e) {
98
98
  n[r / 2] = Number.parseInt(t.slice(r, r + 2), 16);
99
99
  return n;
100
100
  }
101
- function gt(e) {
101
+ function yt(e) {
102
102
  return Uint8Array.from(e);
103
103
  }
104
104
  function Gr(e, t) {
@@ -109,7 +109,7 @@ async function jr(e, t) {
109
109
  if (!t) throw new Error("Asset key is required for encrypted assets");
110
110
  if (e.byteLength < 28)
111
111
  throw new Error("Encrypted payload too small");
112
- const n = new Uint8Array(e), r = gt(n.subarray(0, 12)), s = gt(n.subarray(12, 28)), i = gt(n.subarray(28)), a = gt(Gr(i, s)), o = await crypto.subtle.importKey(
112
+ const n = new Uint8Array(e), r = yt(n.subarray(0, 12)), s = yt(n.subarray(12, 28)), i = yt(n.subarray(28)), a = yt(Gr(i, s)), o = await crypto.subtle.importKey(
113
113
  "raw",
114
114
  Kr(t),
115
115
  { name: "AES-GCM" },
@@ -122,22 +122,22 @@ async function jr(e, t) {
122
122
  a
123
123
  );
124
124
  }
125
- let Vt = null;
126
- const Wt = /* @__PURE__ */ new Set();
127
- function yt(e) {
125
+ let Wt = null;
126
+ const Ht = /* @__PURE__ */ new Set();
127
+ function _t(e) {
128
128
  return e.includes("/custom-faces/");
129
129
  }
130
130
  function bt(e) {
131
131
  const t = e.binBase.trim().replace(/\/$/, ""), n = e.twinBase.trim().replace(/\/$/, "");
132
- if (!(e.encrypted || yt(t) || yt(n)))
132
+ if (!(e.encrypted || _t(t) || _t(n)))
133
133
  return { twinBase: n, binBase: t, encrypted: !1 };
134
- const s = yt(t) ? t : yt(n) ? n : t;
134
+ const s = _t(t) ? t : _t(n) ? n : t;
135
135
  return { twinBase: s, binBase: s, encrypted: !0 };
136
136
  }
137
- function Qe(e) {
137
+ function Ze(e) {
138
138
  return bt(e).encrypted;
139
139
  }
140
- function bn(e, t = jt) {
140
+ function vn(e, t = zt) {
141
141
  const n = `${t.replace(/\/$/, "")}/${e}`;
142
142
  return { twinBase: n, binBase: n, encrypted: !0 };
143
143
  }
@@ -145,8 +145,8 @@ function zr(e, t) {
145
145
  return (e ?? t)?.trim() || void 0;
146
146
  }
147
147
  function Yr(e) {
148
- Vt = bt(e);
149
- for (const t of Wt) t();
148
+ Wt = bt(e);
149
+ for (const t of Ht) t();
150
150
  }
151
151
  function Jr(e, t) {
152
152
  const n = typeof globalThis.location < "u" ? globalThis.location.href : void 0;
@@ -164,36 +164,36 @@ function Jr(e, t) {
164
164
  encrypted: e.encrypted
165
165
  });
166
166
  }
167
- function Ue() {
168
- if (!Vt)
167
+ function He() {
168
+ if (!Wt)
169
169
  throw new Error("Twin assets not configured; call setActiveTwinAssets first");
170
- return Vt;
170
+ return Wt;
171
171
  }
172
172
  function Xr(e) {
173
- return Wt.add(e), () => Wt.delete(e);
173
+ return Ht.add(e), () => Ht.delete(e);
174
174
  }
175
- function vn() {
176
- const e = Ue();
177
- return Qe(e) ? `${e.binBase}/sil.bin` : `${e.twinBase}/sil.png`;
175
+ function bn() {
176
+ const e = He();
177
+ return Ze(e) ? `${e.binBase}/sil.bin` : `${e.twinBase}/sil.png`;
178
178
  }
179
179
  function Qr() {
180
- const e = Ue();
181
- return Qe(e) ? `${e.binBase}/idle.bin` : `${e.twinBase}/idle.mp4`;
180
+ const e = He();
181
+ return Ze(e) ? `${e.binBase}/idle.bin` : `${e.twinBase}/idle.mp4`;
182
182
  }
183
183
  function Zr() {
184
- const e = Ue();
185
- return Qe(e) ? e.binBase : e.twinBase;
184
+ const e = He();
185
+ return Ze(e) ? e.binBase : e.twinBase;
186
186
  }
187
- function It() {
188
- return Qe(Ue());
187
+ function Mt() {
188
+ return Ze(He());
189
189
  }
190
190
  function es() {
191
- const e = Ue();
192
- return Qe(e) ? `${e.binBase}/atlas.bin` : `${e.twinBase}/atlas.json`;
191
+ const e = He();
192
+ return Ze(e) ? `${e.binBase}/atlas.bin` : `${e.twinBase}/atlas.json`;
193
193
  }
194
194
  function ts() {
195
- const e = Ue();
196
- return Qe(e) ? `${e.binBase}/expression_atlas.bin` : `${e.twinBase}/expression_atlas.json`;
195
+ const e = He();
196
+ return Ze(e) ? `${e.binBase}/expression_atlas.bin` : `${e.twinBase}/expression_atlas.json`;
197
197
  }
198
198
  function Kn(e) {
199
199
  const t = e.split(/\r?\n/);
@@ -258,16 +258,16 @@ function os(e, t) {
258
258
  const s = Math.max(0, Math.min(1, (t - e.wtime) / r));
259
259
  return Math.min(n - 1, Math.floor(s * n));
260
260
  }
261
- function ze(e, t) {
261
+ function Ye(e, t) {
262
262
  const n = Math.max(0, Math.min(e.length - 1, t));
263
263
  return e[n] ?? "";
264
264
  }
265
- function Jt(e, t) {
265
+ function Xt(e, t) {
266
266
  const n = e.toLowerCase().replace(/[^a-z']/g, "");
267
- return !n || ze(n, t) !== "w" || t > 0 && ze(n, t - 1) === "q" ? !1 : t === 0 && /^w[aeiouy]/.test(n);
267
+ return !n || Ye(n, t) !== "w" || t > 0 && Ye(n, t - 1) === "q" ? !1 : t === 0 && /^w[aeiouy]/.test(n);
268
268
  }
269
269
  function cs(e, t) {
270
- const n = e.toLowerCase(), r = ze(n, t), s = n.slice(Math.max(0, t - 1), t + 2);
270
+ const n = e.toLowerCase(), r = Ye(n, t), s = n.slice(Math.max(0, t - 1), t + 2);
271
271
  return r === "i" || r === "y" ? "I" : s.includes("ee") || r === "e" || n.includes("ea") ? "E" : "aa";
272
272
  }
273
273
  function us(e, t) {
@@ -276,22 +276,22 @@ function us(e, t) {
276
276
  Math.min(n.length, t + 3)
277
277
  );
278
278
  if (/oo|ou|uw/.test(r)) return "U";
279
- if (Jt(n, t)) return "FF";
280
- const s = ze(n, t);
279
+ if (Xt(n, t)) return "FF";
280
+ const s = Ye(n, t);
281
281
  return s === "u" || s === "w" ? "U" : "O";
282
282
  }
283
283
  function ls(e, t) {
284
284
  const n = e.toLowerCase();
285
- if (Jt(n, t)) return "FF";
285
+ if (Xt(n, t)) return "FF";
286
286
  const r = n.slice(Math.max(0, t - 2), t + 1);
287
287
  if (/qu/.test(r)) return "U";
288
288
  const s = n.slice(
289
289
  Math.max(0, t - 1),
290
290
  Math.min(n.length, t + 3)
291
291
  );
292
- return /wo|wh/.test(s) || ze(n, t) === "o" ? "O" : "U";
292
+ return /wo|wh/.test(s) || Ye(n, t) === "o" ? "O" : "U";
293
293
  }
294
- const _t = {
294
+ const St = {
295
295
  d: "DD",
296
296
  t: "DD",
297
297
  n: "DD",
@@ -304,11 +304,11 @@ const _t = {
304
304
  j: "CH"
305
305
  };
306
306
  function fs(e, t) {
307
- const n = e.toLowerCase(), r = ze(n, t);
308
- if (_t[r]) return _t[r];
307
+ const n = e.toLowerCase(), r = Ye(n, t);
308
+ if (St[r]) return St[r];
309
309
  for (let s = 0; s < n.length; s++) {
310
310
  const i = n[s];
311
- if (_t[i]) return _t[i];
311
+ if (St[i]) return St[i];
312
312
  }
313
313
  return "DD";
314
314
  }
@@ -350,9 +350,9 @@ function jn(e) {
350
350
  function hs(e) {
351
351
  return zn(e).find((n) => n.viseme !== "sil") ?? null;
352
352
  }
353
- function Fe(e) {
353
+ function Ne(e) {
354
354
  const t = Gn(e);
355
- return jn(e) ? t + zt : t;
355
+ return jn(e) ? t + Yt : t;
356
356
  }
357
357
  function ps(e) {
358
358
  const t = [], n = hs(e);
@@ -367,7 +367,7 @@ function ps(e) {
367
367
  from: r.viseme,
368
368
  to: "sil",
369
369
  fromVtime: s,
370
- toVtime: s + zt
370
+ toVtime: s + Yt
371
371
  }), t;
372
372
  }
373
373
  function zn(e) {
@@ -402,10 +402,10 @@ function Yn(e, t) {
402
402
  const n = Math.max(0, t - e), r = n > 0 ? Math.max(n, ms) : 0, s = e, i = e + r;
403
403
  return { gapMs: n, effectiveGapMs: r, holdEnd: e, transStart: s, transEnd: i };
404
404
  }
405
- const Xt = 16e3;
405
+ const Qt = 16e3;
406
406
  function Jn(e) {
407
407
  const t = Number(e);
408
- return Number.isFinite(t) && t > 0 ? t : Xt;
408
+ return Number.isFinite(t) && t > 0 ? t : Qt;
409
409
  }
410
410
  function _s(e) {
411
411
  let t = e.trim();
@@ -417,7 +417,7 @@ function _s(e) {
417
417
  function Ss(e) {
418
418
  return e.length >= 4 && e[0] === 82 && e[1] === 73 && e[2] === 70 && e[3] === 70;
419
419
  }
420
- function En(e, t, n = Xt) {
420
+ function En(e, t, n = Qt) {
421
421
  const r = t.byteOffset % 2 === 0 ? t : t.slice(), s = new Int16Array(
422
422
  r.buffer,
423
423
  r.byteOffset,
@@ -434,7 +434,7 @@ function ks(e) {
434
434
  r[s] = n.charCodeAt(s);
435
435
  return r;
436
436
  }
437
- async function bs(e, t, n = Xt) {
437
+ async function vs(e, t, n = Qt) {
438
438
  const r = ks(t), s = Jn(n);
439
439
  if (Ss(r)) {
440
440
  const i = r.buffer.slice(
@@ -461,7 +461,7 @@ function Tn(e) {
461
461
  t = Math.max(t, n.vtime + n.vduration);
462
462
  return t;
463
463
  }
464
- function vs(e, t) {
464
+ function bs(e, t) {
465
465
  for (const n of e)
466
466
  if (t >= n.wtime && t < n.wtime + n.wduration)
467
467
  return n;
@@ -469,292 +469,312 @@ function vs(e, t) {
469
469
  }
470
470
  function Xn(e, t) {
471
471
  const n = [], r = [];
472
- let s = null, i = !1, a = !1, o = !1, f = !1, d = 0, u = 0, c = 0, m = 0, w = null, b = 1, h = t, I = !1, E = null, _ = 0, p = !1, v = !1, N = 0;
473
- const F = [], j = () => {
474
- v = !1, N = 0, F.length = 0;
475
- }, R = () => {
472
+ let s = null, i = !1, a = !1, o = !1, d = !1, m = 0, c = 0, u = 0, f = 0, w = null, b = 1, p = t, P = !1, E = null, _ = 0, h = 0, A = !1, O = !1, B = 0;
473
+ const G = [], C = () => {
474
+ O = !1, B = 0, G.length = 0;
475
+ }, k = () => {
476
476
  w != null && (clearTimeout(w), w = null);
477
- }, S = () => {
478
- for (const y of r) {
477
+ }, I = () => {
478
+ for (const g of r) {
479
479
  try {
480
- y.onended = null, y.stop(0);
480
+ g.onended = null, g.stop(0);
481
481
  } catch {
482
482
  }
483
483
  try {
484
- y.disconnect();
484
+ g.disconnect();
485
485
  } catch {
486
486
  }
487
487
  }
488
488
  r.length = 0;
489
489
  }, M = () => {
490
- for (const y of r)
491
- y.playbackRate.value = b;
492
- }, T = () => {
493
- if (!(I || !f || i || r.length > 0 || n.length > 0)) {
494
- if (o && s && u > 0) {
495
- const y = (s.currentTime - u) * 1e3;
496
- if (y < _) {
497
- R();
498
- const A = b > 0 ? (_ - y) / b : _ - y;
490
+ for (const g of r)
491
+ g.playbackRate.value = b;
492
+ }, Q = () => Math.max(_, h), x = () => {
493
+ if (!(P || !d || i || r.length > 0 || n.length > 0)) {
494
+ if (o && s && c > 0) {
495
+ const g = (s.currentTime - c) * 1e3, S = Q();
496
+ if (g < S) {
497
+ k();
498
+ const L = b > 0 ? (S - g) / b : S - g;
499
499
  w = setTimeout(() => {
500
- I || (a = !1, w = null, e.onSpeakingDone(), e.onNotify());
501
- }, Math.max(0, A));
500
+ P || (a = !1, w = null, e.onSpeakingDone(), e.onNotify());
501
+ }, Math.max(0, L));
502
502
  return;
503
503
  }
504
504
  }
505
- a = !1, R(), e.onSpeakingDone(), e.onNotify();
505
+ a = !1, k(), e.onSpeakingDone(), e.onNotify();
506
506
  }
507
- }, Z = (y) => {
508
- const A = Fe(y);
509
- if (A <= 0) {
507
+ }, z = (g) => {
508
+ const S = Ne(g);
509
+ if (S <= 0) {
510
510
  e.onSpeakingDone(), e.onNotify();
511
511
  return;
512
512
  }
513
- a = !0, d = performance.now(), e.onSpeakingStart(), e.onNotify(), R();
514
- const q = b > 0 ? A / b : A;
513
+ a = !0, m = performance.now(), e.onSpeakingStart(), e.onNotify(), k();
514
+ const L = b > 0 ? S / b : S;
515
515
  w = setTimeout(() => {
516
- I || (a = !1, w = null, e.onSpeakingDone(), e.onNotify());
517
- }, q);
518
- }, D = (y, A, q, x) => {
519
- A.length !== 0 && (x && (c = q), A.forEach((P) => {
520
- const C = String(P.word ?? "").trim();
521
- if (!C) return;
522
- const B = Math.round((P.start ?? 0) * 1e3), Y = P.duration != null ? Math.round(P.duration * 1e3) : P.end != null ? Math.max(1, Math.round((P.end - (P.start ?? 0)) * 1e3)) : 0;
523
- y.push({
524
- word: C,
525
- wtime: c + B,
526
- wduration: Y,
527
- queueIndex: y.length,
528
- gesture: P.gesture ?? null,
529
- emphasis: P.emphasis === !0
516
+ P || (a = !1, w = null, e.onSpeakingDone(), e.onNotify());
517
+ }, L);
518
+ }, J = (g, S, L, W) => {
519
+ S.length !== 0 && (W && (u = L), S.forEach((D) => {
520
+ const T = String(D.word ?? "").trim();
521
+ if (!T) return;
522
+ const Z = Math.round((D.start ?? 0) * 1e3), ae = D.duration != null ? Math.round(D.duration * 1e3) : D.end != null ? Math.max(1, Math.round((D.end - (D.start ?? 0)) * 1e3)) : 0;
523
+ g.push({
524
+ word: T,
525
+ wtime: u + Z,
526
+ wduration: ae,
527
+ queueIndex: g.length,
528
+ gesture: D.gesture ?? null,
529
+ emphasis: D.emphasis === !0
530
530
  });
531
531
  }));
532
- }, X = (y, A, q, x, P, $) => {
533
- q.length !== 0 && (P && (c = x), q.forEach((C) => {
534
- const B = String(C.symbol ?? C.viseme ?? "").trim();
535
- if (!B) return;
536
- const Y = Math.round((C.start ?? 0) * 1e3), ee = Math.max(1, Math.round((C.duration ?? 0) * 1e3)), pe = c + Y, me = vs(A, pe), le = me ? os(me, pe) : 0;
537
- let fe;
538
- Fr($) === "oculus_direct" ? (fe = ss(B), fe === "U" && me && Jt(me.word, le) && (fe = "FF")) : fe = ds(
539
- B,
532
+ }, ie = (g, S, L, W, D, R) => {
533
+ L.length !== 0 && (D && (u = W), L.forEach((T) => {
534
+ const Z = String(T.symbol ?? T.viseme ?? "").trim();
535
+ if (!Z) return;
536
+ const ae = Math.round((T.start ?? 0) * 1e3), te = Math.max(1, Math.round((T.duration ?? 0) * 1e3)), ue = u + ae, me = bs(S, ue), ge = me ? os(me, ue) : 0;
537
+ let Ae;
538
+ Fr(R) === "oculus_direct" ? (Ae = ss(Z), Ae === "U" && me && Xt(me.word, ge) && (Ae = "FF")) : Ae = ds(
539
+ Z,
540
540
  me?.word ?? "",
541
- le
542
- ), y.push({
543
- viseme: fe,
541
+ ge
542
+ ), g.push({
543
+ viseme: Ae,
544
544
  weight: 1,
545
- vtime: pe,
546
- vduration: ee
545
+ vtime: ue,
546
+ vduration: te
547
547
  });
548
548
  }), e.onNotify());
549
- }, z = (y, A, q, x, P, $, C) => {
550
- D(A, x, P, $), X(
551
- y,
552
- A,
553
- q,
554
- P,
555
- $,
556
- C
549
+ }, X = (g, S, L, W, D, R, T) => {
550
+ J(S, W, D, R), ie(
551
+ g,
552
+ S,
553
+ L,
554
+ D,
555
+ R,
556
+ T
557
557
  );
558
- }, se = (y, A) => {
558
+ }, he = (g, S) => {
559
559
  if (!s) return;
560
- const q = s, x = q.currentTime;
561
- let P = A?.startTime ?? m;
562
- const $ = A?.isFirstAudio ?? !a;
563
- P < x && (P = x + 0.1), m = P + y.duration;
564
- const C = q.createBufferSource();
565
- if (C.buffer = y, C.playbackRate.value = b, C.connect(q.destination), E && C.connect(E), r.push(C), I) {
560
+ const L = s, W = L.currentTime;
561
+ let D = S?.startTime ?? f;
562
+ const R = S?.isFirstAudio ?? !a;
563
+ D < W && (D = W + 0.1), f = D + g.duration;
564
+ const T = L.createBufferSource();
565
+ if (T.buffer = g, T.playbackRate.value = b, T.connect(L.destination), E && T.connect(E), r.push(T), P) {
566
566
  r.pop();
567
567
  return;
568
568
  }
569
- if ($) {
570
- a = !0, e.onSpeakingStart(), A?.isFirstOfStream ? (u = P, c = 0, A.onNewStream?.()) : u <= 0 && (u = P);
571
- const B = (P - x) * 1e3;
572
- d = performance.now() + B;
569
+ if (R) {
570
+ a = !0, e.onSpeakingStart(), S?.isFirstOfStream ? (c = D, u = 0, S.onNewStream?.()) : c <= 0 && (c = D);
571
+ const Z = (D - W) * 1e3;
572
+ m = performance.now() + Z;
573
573
  }
574
- C.onended = () => {
575
- const B = r.indexOf(C);
576
- B >= 0 && r.splice(B, 1), T(), e.onNotify();
577
- }, C.start(P);
578
- }, ne = () => {
579
- if (!v || F.length === 0) return;
580
- v = !1;
581
- const y = F.splice(0);
582
- N = 0;
583
- for (let A = 0; A < y.length; A++)
584
- se(y[A].audioBuffer, {
585
- isFirstAudio: A === 0 && !a,
586
- isFirstOfStream: A === 0
574
+ T.onended = () => {
575
+ const Z = r.indexOf(T);
576
+ Z >= 0 && r.splice(Z, 1), x(), e.onNotify();
577
+ }, T.start(D), c > 0 && (h = Math.max(
578
+ h,
579
+ (f - c) * 1e3
580
+ ));
581
+ }, fe = () => {
582
+ if (!O || G.length === 0) return;
583
+ O = !1;
584
+ const g = G.splice(0);
585
+ B = 0;
586
+ for (let S = 0; S < g.length; S++)
587
+ he(g[S].audioBuffer, {
588
+ isFirstAudio: S === 0 && !a,
589
+ isFirstOfStream: S === 0
587
590
  });
588
591
  e.onNotify();
589
- }, de = async (y, A, q, x, P, $, C) => {
590
- if (I) return;
591
- const B = Jn(C?.sampleRate), Y = C?.newStream === !0;
592
- if (!q?.trim()) {
593
- if (x.length === 0 && P.length === 0) return;
594
- Y && (c = 0, _ = 0, y.length = 0, A.length = 0, C?.onNewStream?.());
595
- let ee = c;
596
- $ && y.length > 0 && !Y && (ee = Tn(y)), z(
597
- y,
598
- A,
599
- x,
600
- P,
601
- ee,
602
- $,
603
- h
604
- ), _ = Math.max(_, Fe(y)), !a && _ > 0 && Z(y), e.onNotify();
592
+ }, de = async (g, S, L, W, D, R, T) => {
593
+ if (P) return;
594
+ const Z = Jn(T?.sampleRate), ae = T?.newStream === !0;
595
+ if (!L?.trim()) {
596
+ if (W.length === 0 && D.length === 0) return;
597
+ ae && (u = 0, _ = 0, h = 0, g.length = 0, S.length = 0, T?.onNewStream?.());
598
+ let te = T?.audioOffsetMs != null ? T.audioOffsetMs : u;
599
+ T?.audioOffsetMs == null && R && g.length > 0 && !ae && (te = Tn(g));
600
+ const ue = T?.audioOffsetMs != null ? !0 : R;
601
+ X(
602
+ g,
603
+ S,
604
+ W,
605
+ D,
606
+ te,
607
+ ue,
608
+ p
609
+ ), _ = Math.max(_, Ne(g)), !a && _ > 0 && z(g), e.onNotify();
605
610
  return;
606
611
  }
607
612
  if (i) {
608
613
  n.push({
609
- audio: q,
610
- visemes: x,
611
- words: P,
612
- isNewSegment: $,
613
- sampleRate: B,
614
- newStream: Y
614
+ audio: L,
615
+ visemes: W,
616
+ words: D,
617
+ isNewSegment: R,
618
+ sampleRate: Z,
619
+ newStream: ae,
620
+ audioOffsetMs: T?.audioOffsetMs
615
621
  });
616
622
  return;
617
623
  }
618
624
  i = !0;
619
625
  try {
620
- const ee = window.AudioContext || window.webkitAudioContext;
621
- s || (s = new ee());
622
- const pe = s;
623
- pe.state === "suspended" && await pe.resume(), E || (E = pe.createMediaStreamDestination());
624
- const me = await bs(
625
- pe,
626
- q,
627
- B
626
+ const te = window.AudioContext || window.webkitAudioContext;
627
+ s || (s = new te());
628
+ const ue = s;
629
+ ue.state === "suspended" && await ue.resume(), E || (E = ue.createMediaStreamDestination());
630
+ const me = await vs(
631
+ ue,
632
+ L,
633
+ Z
628
634
  );
629
635
  o = !0;
630
- const le = !a;
631
- if (p && (e.shouldHoldForWarmup?.() ?? !1) && (v || le && Y)) {
632
- v || (v = !0, N = 0, F.length = 0, Y && (c = 0, _ = 0, y.length = 0, A.length = 0, C?.onNewStream?.())), z(
633
- y,
634
- A,
635
- x,
636
- P,
636
+ const ge = !a;
637
+ if (A && (e.shouldHoldForWarmup?.() ?? !1) && (O || ge && ae)) {
638
+ O || (O = !0, B = 0, G.length = 0, ae && (u = 0, _ = 0, h = 0, g.length = 0, S.length = 0, T?.onNewStream?.()));
639
+ const v = T?.audioOffsetMs != null ? T.audioOffsetMs : B, N = T?.audioOffsetMs != null ? !0 : R;
640
+ X(
641
+ g,
642
+ S,
643
+ W,
644
+ D,
645
+ v,
637
646
  N,
638
- $,
639
- h
640
- ), N += Math.round(me.duration * 1e3), F.push({ audioBuffer: me }), _ = Math.max(_, Fe(y)), e.onNotify();
647
+ p
648
+ ), T?.audioOffsetMs == null && (B += Math.round(me.duration * 1e3)), G.push({ audioBuffer: me }), _ = Math.max(_, Ne(g)), e.onNotify();
641
649
  return;
642
650
  }
643
- le && Y && (_ = 0, y.length = 0, A.length = 0, c = 0, C?.onNewStream?.());
644
- const Ve = pe.currentTime;
645
- let xe = m;
646
- xe < Ve && (xe = Ve + 0.1);
647
- const We = le && Y ? 0 : u > 0 ? Math.max(0, (xe - u) * 1e3) : 0;
648
- se(me, {
649
- startTime: xe,
650
- isFirstAudio: le,
651
- isFirstOfStream: le && Y
652
- }), z(
653
- y,
654
- A,
655
- x,
656
- P,
657
- We,
658
- $,
659
- h
660
- ), _ = Math.max(_, Fe(y));
661
- } catch (ee) {
662
- console.error("[avatarChunkPlayback] audio chunk failed:", ee);
651
+ ge && ae && (_ = 0, h = 0, g.length = 0, S.length = 0, u = 0, T?.onNewStream?.());
652
+ const Fe = ue.currentTime;
653
+ let De = f;
654
+ De < Fe && (De = Fe + 0.1);
655
+ const dt = T?.audioOffsetMs ?? (ge && ae ? 0 : c > 0 ? Math.max(0, (De - c) * 1e3) : 0), l = T?.audioOffsetMs != null ? !0 : R;
656
+ he(me, {
657
+ startTime: De,
658
+ isFirstAudio: ge,
659
+ isFirstOfStream: ge && ae
660
+ }), X(
661
+ g,
662
+ S,
663
+ W,
664
+ D,
665
+ dt,
666
+ l,
667
+ p
668
+ ), _ = Math.max(_, Ne(g));
669
+ } catch (te) {
670
+ console.error("[avatarChunkPlayback] audio chunk failed:", te);
663
671
  } finally {
664
- if (i = !1, I) return;
665
- const ee = n.shift();
666
- ee ? await de(
667
- y,
668
- A,
669
- ee.audio,
670
- ee.visemes,
671
- ee.words,
672
- ee.isNewSegment,
672
+ if (i = !1, P) return;
673
+ const te = n.shift();
674
+ te ? await de(
675
+ g,
676
+ S,
677
+ te.audio,
678
+ te.visemes,
679
+ te.words,
680
+ te.isNewSegment,
673
681
  {
674
- sampleRate: ee.sampleRate,
675
- newStream: ee.newStream,
676
- onNewStream: C?.onNewStream
682
+ sampleRate: te.sampleRate,
683
+ newStream: te.newStream,
684
+ audioOffsetMs: te.audioOffsetMs,
685
+ onNewStream: T?.onNewStream
677
686
  }
678
- ) : T();
687
+ ) : x();
679
688
  }
680
689
  };
681
690
  return {
682
- setStreamTtsEngineId: (y) => {
683
- h = y;
691
+ setStreamTtsEngineId: (g) => {
692
+ p = g;
684
693
  },
685
- setPlaybackSpeed: (y) => {
686
- b = Math.max(0.1, Math.min(1, y)), M();
694
+ setPlaybackSpeed: (g) => {
695
+ b = Math.max(0.1, Math.min(1, g)), M();
687
696
  },
688
697
  getPlaybackSpeed: () => b,
689
- setLipsyncWarmupHoldEnabled: (y) => {
690
- p = y, y || j();
698
+ setLipsyncWarmupHoldEnabled: (g) => {
699
+ A = g, g || C();
691
700
  },
692
- isWarmingUp: () => v,
693
- releaseWarmupHold: ne,
694
- resetForNewStream: (y) => {
695
- I = !1, f = !1, h = y, _ = 0;
701
+ isWarmingUp: () => O,
702
+ releaseWarmupHold: fe,
703
+ resetForNewStream: (g) => {
704
+ P = !1, d = !1, p = g, _ = 0, h = 0;
696
705
  },
697
706
  /** Next sentence in the same bot turn — keep viseme tail, allow more chunks. */
698
707
  resumeBotStream: () => {
699
- I = !1, f = !1;
708
+ P = !1, d = !1;
700
709
  },
701
710
  /** After interrupt — allow the next utterance to queue/play again. */
702
711
  clearAborted: () => {
703
- I = !1;
712
+ P = !1;
704
713
  },
705
714
  /** Call from a user gesture before bot audio (connect / unlock). */
706
715
  primeAudioContext: async () => {
707
- const y = window.AudioContext || window.webkitAudioContext;
708
- s || (s = new y()), s.state === "suspended" && await s.resume(), s && !E && (E = s.createMediaStreamDestination());
716
+ const g = window.AudioContext || window.webkitAudioContext;
717
+ s || (s = new g()), s.state === "suspended" && await s.resume(), s && !E && (E = s.createMediaStreamDestination());
709
718
  },
710
719
  stop: () => {
711
- I = !0, R(), S(), n.length = 0, i = !1, a = !1, o = !1, f = !1, c = 0, m = 0, u = 0, d = 0, _ = 0, j();
720
+ P = !0, k(), I(), n.length = 0, i = !1, a = !1, o = !1, d = !1, u = 0, f = 0, c = 0, m = 0, _ = 0, h = 0, C();
712
721
  },
713
- markStreamComplete: (y) => {
714
- if (_ = Math.max(_, Fe(y)), f = !0, !o && y.length > 0) {
715
- Z(y);
722
+ markStreamComplete: (g) => {
723
+ if (_ = Math.max(_, Ne(g)), d = !0, !o && g.length > 0) {
724
+ z(g);
716
725
  return;
717
726
  }
718
- T();
727
+ x();
719
728
  },
720
729
  playAudioChunk: de,
721
- queueTimingsOnly: (y, A, q, x, P) => {
722
- if (I) return;
723
- let $ = c;
724
- P && y.length > 0 && ($ = Tn(y)), z(
725
- y,
726
- A,
727
- q,
728
- x,
729
- $,
730
- P,
731
- h
732
- ), _ = Math.max(_, Fe(y));
733
- },
734
- getPlaybackElapsedMs: () => o && s && u > 0 ? Math.max(
730
+ queueTimingsOnly: (g, S, L, W, D, R) => {
731
+ if (P) return;
732
+ let T = R ?? u;
733
+ R == null && D && g.length > 0 && (T = Tn(g));
734
+ const Z = R != null ? !0 : D;
735
+ X(
736
+ g,
737
+ S,
738
+ L,
739
+ W,
740
+ T,
741
+ Z,
742
+ p
743
+ ), _ = Math.max(_, Ne(g));
744
+ },
745
+ getPlaybackElapsedMs: () => o && s && c > 0 ? Math.max(
735
746
  0,
736
- (s.currentTime - u) * 1e3
737
- ) : a ? Math.max(0, (performance.now() - d) * b) : 0,
738
- isSpeaking: () => a ? !0 : !o || !s || u <= 0 ? !1 : (s.currentTime - u) * 1e3 < _,
747
+ (s.currentTime - c) * 1e3
748
+ ) : a ? Math.max(0, (performance.now() - m) * b) : 0,
749
+ isSpeaking: () => a ? !0 : !o || !s || c <= 0 ? !1 : (s.currentTime - c) * 1e3 < Q(),
739
750
  getVoiceStream: () => E ? E.stream : null,
740
- getPlaybackStartPerf: () => d,
751
+ getPlaybackStartPerf: () => m,
741
752
  /** User spoke over the bot — drop in-flight chunks until the next utterance. */
742
753
  bargeIn: () => {
743
- I = !0, f = !1, R(), S(), n.length = 0, i = !1, a = !1, o = !1, c = 0, m = 0, u = 0, d = 0, _ = 0, j();
754
+ P = !0, d = !1, k(), I(), n.length = 0, i = !1, a = !1, o = !1, u = 0, f = 0, c = 0, m = 0, _ = 0, h = 0, C();
744
755
  },
745
756
  interrupt: () => {
746
- I = !0, f = !1, R(), S(), n.length = 0, i = !1, a = !1, o = !1, c = 0, m = 0, u = 0, d = 0, _ = 0, j();
757
+ P = !0, d = !1, k(), I(), n.length = 0, i = !1, a = !1, o = !1, u = 0, f = 0, c = 0, m = 0, _ = 0, h = 0, C();
747
758
  }
748
759
  };
749
760
  }
750
- function Mt(e, t) {
761
+ function Pt(e, t) {
751
762
  let n = null;
752
763
  for (const r of e)
753
764
  t < r.vtime || t >= r.vtime + r.vduration || (!n || r.vtime >= n.vtime) && (n = r);
754
- return n ? n.viseme : "sil";
765
+ if (!n) {
766
+ let r = null, s = -1;
767
+ for (const i of e) {
768
+ if (i.viseme === "sil") continue;
769
+ const a = i.vtime + i.vduration;
770
+ a <= t && a >= s && (r = i, s = a);
771
+ }
772
+ return r?.viseme ?? "sil";
773
+ }
774
+ return n.viseme;
755
775
  }
756
776
  function Qn(e, t, n = 120) {
757
- const r = Mt(e, t);
777
+ const r = Pt(e, t);
758
778
  if (r !== "sil") return r;
759
779
  let s = null;
760
780
  for (const i of e) {
@@ -788,203 +808,204 @@ function Ts(e, t) {
788
808
  return null;
789
809
  }
790
810
  }
791
- const Qt = 0.5, Zt = 1.5, Zn = "Olivia", er = "f786b574-daa5-4673-aa0c-cbe3e8534c02", Pt = 0.85;
792
- function An(e, t = lt, n = {}) {
811
+ const Zt = 0.5, en = 1.5, Zn = "Olivia", er = "f786b574-daa5-4673-aa0c-cbe3e8534c02", Rt = 0.85;
812
+ function An(e, t = ft, n = {}) {
793
813
  const r = `${t.replace(/\/$/, "")}/avatar_ttsWithPoses`, s = [], i = [], a = [], o = [];
794
- let f = "", d = "neutral", u = "", c = je;
795
- const m = /* @__PURE__ */ new Set();
814
+ let d = "", m = "neutral", c = "", u = ze;
815
+ const f = /* @__PURE__ */ new Set();
796
816
  let w = 0, b = null;
797
- const h = () => m.forEach((R) => R());
798
- let I = () => !1;
817
+ const p = () => f.forEach((C) => C());
818
+ let P = () => !1;
799
819
  const E = Xn(
800
820
  {
801
821
  onSpeakingStart: () => e("speaking"),
802
822
  onSpeakingDone: () => e("done"),
803
- onNotify: h,
804
- shouldHoldForWarmup: () => I()
823
+ onNotify: p,
824
+ shouldHoldForWarmup: () => P()
805
825
  },
806
- je
807
- ), _ = () => E.getPlaybackElapsedMs(), p = (R, S = f) => {
826
+ ze
827
+ ), _ = () => E.getPlaybackElapsedMs(), h = (C, k = d) => {
808
828
  if (!n.onLipsyncDebug) return;
809
- const M = _(), T = s;
829
+ const I = _(), M = s;
810
830
  n.onLipsyncDebug({
811
- event: R,
812
- spokenText: S,
813
- mood: d,
831
+ event: C,
832
+ spokenText: k,
833
+ mood: m,
814
834
  playbackAnchorPerf: E.getPlaybackStartPerf(),
815
- playbackElapsedMs: M,
816
- queueLength: T.length,
817
- activeViseme: Mt(T, M),
818
- segments: o.map((Z) => ({
819
- ...Z,
820
- visemes: [...Z.visemes]
835
+ playbackElapsedMs: I,
836
+ queueLength: M.length,
837
+ activeViseme: Pt(M, I),
838
+ segments: o.map((Q) => ({
839
+ ...Q,
840
+ visemes: [...Q.visemes]
821
841
  }))
822
842
  });
823
- }, v = (R, S) => {
824
- n.onLipsyncDebug && (b = { event: R, spokenText: S }, w === 0 && (w = requestAnimationFrame(() => {
843
+ }, A = (C, k) => {
844
+ n.onLipsyncDebug && (b = { event: C, spokenText: k }, w === 0 && (w = requestAnimationFrame(() => {
825
845
  w = 0;
826
- const M = b;
827
- b = null, M && p(M.event, M.spokenText);
846
+ const I = b;
847
+ b = null, I && h(I.event, I.spokenText);
828
848
  })));
829
- }, N = (R, S, M, T, Z) => {
849
+ }, O = (C, k, I, M, Q) => {
830
850
  if (!n.onLipsyncDebug) return;
831
- const D = s.slice(S), X = M.filter(
832
- (z) => String(z.symbol ?? z.viseme ?? "").trim()
851
+ const x = s.slice(k), z = I.filter(
852
+ (J) => String(J.symbol ?? J.viseme ?? "").trim()
833
853
  );
834
854
  o.push({
835
- spokenText: R,
836
- audioOffsetMs: D[0]?.vtime ?? 0,
837
- isNewSegment: T,
838
- chunkDurationMs: Z,
855
+ spokenText: C,
856
+ audioOffsetMs: x[0]?.vtime ?? 0,
857
+ isNewSegment: M,
858
+ chunkDurationMs: Q,
839
859
  receivedAtMs: performance.now(),
840
- visemes: D.map((z, se) => {
841
- const ne = X[se];
860
+ visemes: x.map((J, ie) => {
861
+ const X = z[ie];
842
862
  return {
843
- rawSymbol: String(ne?.symbol ?? ne?.viseme ?? z.viseme).trim(),
844
- viseme: z.viseme,
845
- startSec: Number(ne?.start ?? 0),
846
- durationSec: Number(ne?.duration ?? 0),
847
- vtimeMs: z.vtime,
848
- vdurationMs: z.vduration
863
+ rawSymbol: String(X?.symbol ?? X?.viseme ?? J.viseme).trim(),
864
+ viseme: J.viseme,
865
+ startSec: Number(X?.start ?? 0),
866
+ durationSec: Number(X?.duration ?? 0),
867
+ vtimeMs: J.vtime,
868
+ vdurationMs: J.vduration
849
869
  };
850
870
  })
851
871
  });
852
- }, F = () => {
853
- E.stop(), s.length = 0, i.length = 0, a.length = 0, d = "neutral", o.length = 0, f = "", e("idle"), v("reset"), h();
872
+ }, B = () => {
873
+ E.stop(), s.length = 0, i.length = 0, a.length = 0, m = "neutral", o.length = 0, d = "", e("idle"), A("reset"), p();
854
874
  };
855
875
  return {
856
- setDeveloperToken: (R) => {
857
- u = R;
876
+ setDeveloperToken: (C) => {
877
+ c = C;
858
878
  },
859
- setTtsEngineId: (R) => {
860
- c = R;
879
+ setTtsEngineId: (C) => {
880
+ u = C;
861
881
  },
862
- getTtsEngineId: () => c,
863
- speak: async (R, S) => {
864
- F(), e("loading"), f = R, o.length = 0;
865
- const M = u.trim();
866
- if (!M)
882
+ getTtsEngineId: () => u,
883
+ speak: async (C, k) => {
884
+ B(), e("loading"), d = C, o.length = 0;
885
+ const I = c.trim();
886
+ if (!I)
867
887
  throw new Error("Developer token required for avatar_ttsWithPoses");
868
- const T = S?.ttsEngineId ?? c ?? je;
869
- E.resetForNewStream(T), E.setStreamTtsEngineId(T);
870
- const Z = S?.voiceId ?? Zn, D = S?.speakingRate ?? Pt, X = Math.max(
871
- Qt,
872
- Math.min(Zt, D)
873
- ), z = {
874
- user_query: R,
875
- ttsEngineId: T,
876
- speakingRate: X,
888
+ const M = k?.ttsEngineId ?? u ?? ze;
889
+ E.resetForNewStream(M), E.setStreamTtsEngineId(M), await E.primeAudioContext();
890
+ const Q = k?.voiceId ?? Zn, x = k?.speakingRate ?? Rt, z = Math.max(
891
+ Zt,
892
+ Math.min(en, x)
893
+ ), J = {
894
+ user_query: C,
895
+ ttsEngineId: M,
896
+ speakingRate: z,
877
897
  tenant: "aiTwin"
878
898
  };
879
- $n.has(T) && (z.voice_id = Z);
899
+ $n.has(M) && (J.voice_id = Q);
880
900
  try {
881
- const se = await fetch(r, {
901
+ const ie = await fetch(r, {
882
902
  method: "POST",
883
903
  headers: {
884
904
  "Content-Type": "application/json",
885
- Authorization: `Bearer ${M}`
905
+ Authorization: `Bearer ${I}`
886
906
  },
887
- body: JSON.stringify(z)
907
+ body: JSON.stringify(J)
888
908
  });
889
- if (!se.ok)
890
- throw new Error(`avatar_ttsWithPoses failed (${se.status})`);
891
- const ne = se.body;
892
- if (!ne) throw new Error("No response body");
893
- const de = ne.getReader(), ue = new TextDecoder();
894
- let y = "";
895
- const A = async (q) => {
896
- const x = Kn(q);
897
- if (x) {
898
- if (x.event === "audio") {
899
- const P = x.data.chunk, $ = x.data.visemes ?? [], C = x.data.words ?? [], B = x.data.is_new_segment ?? !1, Y = x.data.sample_rate, ee = s.length;
900
- P ? (await E.playAudioChunk(
909
+ if (!ie.ok)
910
+ throw new Error(`avatar_ttsWithPoses failed (${ie.status})`);
911
+ const X = ie.body;
912
+ if (!X) throw new Error("No response body");
913
+ const he = X.getReader(), fe = new TextDecoder();
914
+ let de = "";
915
+ const ne = async (g) => {
916
+ const S = Kn(g);
917
+ if (S) {
918
+ if (S.event === "audio") {
919
+ const L = S.data.chunk, W = S.data.visemes ?? [], D = S.data.words ?? [], R = S.data.is_new_segment ?? !1, T = S.data.sample_rate, Z = Number(S.data.audio_offset_ms ?? 0), ae = s.length;
920
+ L ? (await E.playAudioChunk(
901
921
  s,
902
922
  i,
903
- P,
904
- $,
923
+ L,
924
+ W,
925
+ D,
926
+ R,
927
+ { sampleRate: T, audioOffsetMs: Z }
928
+ ), O(
905
929
  C,
906
- B,
907
- { sampleRate: Y }
908
- ), N(
930
+ ae,
931
+ W,
909
932
  R,
910
- ee,
911
- $,
912
- B,
913
- Ts(P, Y)
914
- ), v("audio_chunk", R)) : ($.length > 0 || C.length > 0) && (E.queueTimingsOnly(
933
+ Ts(L, T)
934
+ ), A("audio_chunk", C)) : (W.length > 0 || D.length > 0) && (E.queueTimingsOnly(
915
935
  s,
916
936
  i,
917
- $,
937
+ W,
938
+ D,
939
+ R,
940
+ Z
941
+ ), O(
918
942
  C,
919
- B
920
- ), N(
943
+ ae,
944
+ W,
921
945
  R,
922
- ee,
923
- $,
924
- B,
925
946
  null
926
- ), v("audio_chunk", R));
927
- } else if (x.event === "metadata") {
928
- const P = x.data.mood;
929
- typeof P == "string" && P.trim() && (d = P.trim());
930
- const $ = x.data.sentence_emotions;
931
- if (Array.isArray($)) {
947
+ ), A("audio_chunk", C));
948
+ } else if (S.event === "metadata") {
949
+ const L = S.data.mood;
950
+ typeof L == "string" && L.trim() && (m = L.trim());
951
+ const W = S.data.sentence_emotions;
952
+ if (Array.isArray(W)) {
932
953
  a.length = 0;
933
- for (const C of $) {
934
- if (!C || typeof C != "object") continue;
935
- const B = C;
954
+ for (const D of W) {
955
+ if (!D || typeof D != "object") continue;
956
+ const R = D;
936
957
  a.push({
937
- sentence_index: Number(B.sentence_index ?? 0),
938
- text: String(B.text ?? ""),
939
- sentiment: typeof B.sentiment == "string" ? B.sentiment : void 0,
940
- emotion: String(B.emotion ?? d),
941
- start_word: Number(B.start_word ?? 0),
942
- end_word: Number(B.end_word ?? 0)
958
+ sentence_index: Number(R.sentence_index ?? 0),
959
+ text: String(R.text ?? ""),
960
+ sentiment: typeof R.sentiment == "string" ? R.sentiment : void 0,
961
+ emotion: String(R.emotion ?? m),
962
+ start_word: Number(R.start_word ?? 0),
963
+ end_word: Number(R.end_word ?? 0)
943
964
  });
944
965
  }
945
966
  }
946
- h(), v("metadata", R);
947
- } else if (x.event === "error")
967
+ p(), A("metadata", C);
968
+ } else if (S.event === "error")
948
969
  throw new Error(
949
- String(x.data.message ?? "avatar_ttsWithPoses stream error")
970
+ String(S.data.message ?? "avatar_ttsWithPoses stream error")
950
971
  );
951
972
  }
952
973
  };
953
974
  for (; ; ) {
954
- const { done: q, value: x } = await de.read();
955
- x && (y += ue.decode(x, { stream: !0 }));
956
- const P = y.split(`
975
+ const { done: g, value: S } = await he.read();
976
+ S && (de += fe.decode(S, { stream: !0 }));
977
+ const L = de.split(`
957
978
 
958
979
  `);
959
- y = P.pop() ?? "";
960
- for (const $ of P) await A($);
961
- if (q) {
962
- y.trim() && await A(y.trim()), E.markStreamComplete(s), v("bot_stopped", R);
980
+ de = L.pop() ?? "";
981
+ for (const W of L) await ne(W);
982
+ if (g) {
983
+ de.trim() && await ne(de.trim()), E.markStreamComplete(s), A("bot_stopped", C);
963
984
  break;
964
985
  }
965
986
  }
966
- } catch (se) {
967
- throw console.error("[avatarTtsLipsync]", se), F(), e("error"), se;
987
+ } catch (ie) {
988
+ throw console.error("[avatarTtsLipsync]", ie), B(), e("error"), ie;
968
989
  }
969
990
  },
970
- stop: F,
991
+ stop: B,
971
992
  getVisemeQueue: () => s,
972
993
  getWordQueue: () => i,
973
994
  getSentenceEmotions: () => a,
974
- getStreamMood: () => d,
995
+ getStreamMood: () => m,
975
996
  getPlaybackElapsedMs: () => E.getPlaybackElapsedMs(),
976
997
  isSpeaking: () => E.isSpeaking(),
977
998
  isWarmingUp: () => E.isWarmingUp(),
978
999
  releaseWarmupHold: () => E.releaseWarmupHold(),
979
- setLipsyncWarmupHold: (R, S) => {
980
- S && (I = S), E.setLipsyncWarmupHoldEnabled(R);
1000
+ setLipsyncWarmupHold: (C, k) => {
1001
+ k && (P = k), E.setLipsyncWarmupHoldEnabled(C);
981
1002
  },
982
1003
  getVoiceStream: () => E.getVoiceStream(),
983
- setPlaybackSpeed: (R) => {
984
- E.setPlaybackSpeed(R);
1004
+ setPlaybackSpeed: (C) => {
1005
+ E.setPlaybackSpeed(C);
985
1006
  },
986
1007
  getPlaybackSpeed: () => E.getPlaybackSpeed(),
987
- subscribe: (R) => (m.add(R), () => m.delete(R))
1008
+ subscribe: (C) => (f.add(C), () => f.delete(C))
988
1009
  };
989
1010
  }
990
1011
  const tr = 16e3, nr = 16e3;
@@ -1000,7 +1021,7 @@ class As extends Er {
1000
1021
  }
1001
1022
  }
1002
1023
  }
1003
- class Is extends vr {
1024
+ class Is extends br {
1004
1025
  constructor() {
1005
1026
  super(
1006
1027
  !0,
@@ -1031,16 +1052,16 @@ function Ds(e) {
1031
1052
  throw new Error(
1032
1053
  "Voice connect requires authToken (same JWT as SSE Authorization Bearer)."
1033
1054
  );
1034
- const n = e.apiBase ?? lt, r = new URL(`${Rs(n)}/ws/voice`);
1055
+ const n = e.apiBase ?? ft, r = new URL(`${Rs(n)}/ws/voice`);
1035
1056
  r.searchParams.set("authToken", t), r.searchParams.set("tenant", "aiTwin");
1036
1057
  const s = e.voiceId?.trim();
1037
1058
  s && Ps(s) && r.searchParams.set("voiceId", s);
1038
1059
  const i = e.personaId?.trim();
1039
1060
  i && r.searchParams.set("personaId", i);
1040
- const a = e.speakingRate ?? Pt;
1061
+ const a = e.speakingRate ?? Rt;
1041
1062
  return r.searchParams.set(
1042
1063
  "speaking_rate",
1043
- String(Math.max(Qt, Math.min(Zt, a)))
1064
+ String(Math.max(Zt, Math.min(en, a)))
1044
1065
  ), e.ttsStream === !1 ? r.searchParams.set("tts_stream", "false") : r.searchParams.set("tts_stream", "true"), r.toString();
1045
1066
  }
1046
1067
  function Lt(e, t) {
@@ -1065,294 +1086,297 @@ function xs(e) {
1065
1086
  type: String(r.type ?? t.type ?? "server-message")
1066
1087
  };
1067
1088
  }
1068
- function Ot(e) {
1089
+ function Nt(e) {
1069
1090
  return e.is_new_segment === !0 || Number(e.audio_offset_ms ?? -1) === 0 ? !0 : Number(e.chunk_index ?? -1) === 0;
1070
1091
  }
1071
- function Bs(e) {
1092
+ function Cs(e) {
1072
1093
  return e?.transport?._mediaManager?._wavStreamPlayer ?? null;
1073
1094
  }
1074
- function Cs(e, t = {}) {
1095
+ function Bs(e, t = {}) {
1075
1096
  const n = [], r = [], s = [], i = [];
1076
- let a = "", o = "neutral", f = Xe, d = !1, u = null, c = null, m = { ...t }, w = 0, b = null;
1077
- const h = /* @__PURE__ */ new Set(), I = [];
1078
- let E = !1, _ = null, p = !0, v = "default";
1079
- const N = /* @__PURE__ */ new Set();
1080
- let F = null, j = 0, R = !1, S = null;
1081
- const M = () => {
1082
- S != null && (clearTimeout(S), S = null);
1083
- }, T = () => h.forEach((l) => l());
1084
- let Z = () => !1;
1085
- const D = Xn(
1097
+ let a = "", o = "neutral", d = Qe, m = !1, c = null, u = null, f = { ...t }, w = 0, b = null;
1098
+ const p = /* @__PURE__ */ new Set(), P = [];
1099
+ let E = !1, _ = null, h = !0, A = "default";
1100
+ const O = /* @__PURE__ */ new Set();
1101
+ let B = null, G = 0, C = !1, k = null;
1102
+ const I = () => {
1103
+ k != null && (clearTimeout(k), k = null);
1104
+ }, M = () => p.forEach((l) => l());
1105
+ let Q = () => !1;
1106
+ const x = Xn(
1086
1107
  {
1087
1108
  onSpeakingStart: () => e("speaking"),
1088
1109
  onSpeakingDone: () => e("done"),
1089
- onNotify: T,
1090
- shouldHoldForWarmup: () => Z()
1110
+ onNotify: M,
1111
+ shouldHoldForWarmup: () => Q()
1091
1112
  },
1092
- f
1093
- ), X = () => {
1113
+ d
1114
+ ), z = () => {
1094
1115
  if (_) return _;
1095
1116
  const l = new MessageChannel();
1096
1117
  return l.port1.onmessage = () => {
1097
- const k = I.shift();
1098
- k !== void 0 && le(k), I.length > 0 ? l.port2.postMessage(null) : E = !1;
1118
+ const v = P.shift();
1119
+ v !== void 0 && ue(v), P.length > 0 ? l.port2.postMessage(null) : E = !1;
1099
1120
  }, _ = l, l;
1100
- }, z = (l) => {
1101
- I.push(l), !E && (E = !0, X().port2.postMessage(null));
1102
- }, se = () => D.getPlaybackElapsedMs(), ne = (l, k = a) => {
1103
- if (!m.onLipsyncDebug) return;
1104
- const V = se(), K = n;
1105
- m.onLipsyncDebug({
1121
+ }, J = (l) => {
1122
+ P.push(l), !E && (E = !0, z().port2.postMessage(null));
1123
+ }, ie = () => x.getPlaybackElapsedMs(), X = (l, v = a) => {
1124
+ if (!f.onLipsyncDebug) return;
1125
+ const N = ie(), q = n;
1126
+ f.onLipsyncDebug({
1106
1127
  event: l,
1107
- spokenText: k,
1128
+ spokenText: v,
1108
1129
  mood: o,
1109
- playbackAnchorPerf: D.getPlaybackStartPerf(),
1110
- playbackElapsedMs: V,
1111
- queueLength: K.length,
1112
- activeViseme: Mt(K, V),
1113
- segments: i.map((ie) => ({
1114
- ...ie,
1115
- visemes: [...ie.visemes]
1130
+ playbackAnchorPerf: x.getPlaybackStartPerf(),
1131
+ playbackElapsedMs: N,
1132
+ queueLength: q.length,
1133
+ activeViseme: Pt(q, N),
1134
+ segments: i.map((re) => ({
1135
+ ...re,
1136
+ visemes: [...re.visemes]
1116
1137
  }))
1117
1138
  });
1118
- }, de = (l, k) => {
1119
- m.onLipsyncDebug && (b = { event: l, spokenText: k }, w === 0 && (w = requestAnimationFrame(() => {
1139
+ }, he = (l, v) => {
1140
+ f.onLipsyncDebug && (b = { event: l, spokenText: v }, w === 0 && (w = requestAnimationFrame(() => {
1120
1141
  w = 0;
1121
- const V = b;
1122
- b = null, V && ne(V.event, V.spokenText);
1142
+ const N = b;
1143
+ b = null, N && X(N.event, N.spokenText);
1123
1144
  })));
1124
- }, ue = () => {
1125
- p = !0, N.clear(), D.clearAborted();
1126
- }, y = () => {
1127
- const l = Bs(u);
1145
+ }, fe = () => {
1146
+ h = !0, O.clear(), x.clearAborted();
1147
+ }, de = () => {
1148
+ const l = Cs(c);
1128
1149
  if (l) {
1129
- v = `bot-${Date.now()}`, l.interruptedTrackIds = { [v]: !0 };
1150
+ A = `bot-${Date.now()}`, l.interruptedTrackIds = { [A]: !0 };
1130
1151
  try {
1131
1152
  l.interrupt?.();
1132
1153
  } catch {
1133
1154
  }
1134
1155
  }
1135
- const k = c;
1156
+ const v = u;
1136
1157
  try {
1137
- k?.interrupt?.();
1158
+ v?.interrupt?.();
1138
1159
  } catch {
1139
1160
  }
1140
- }, A = (l) => {
1141
- const k = l.context_id?.trim();
1142
- return k && N.has(k) ? !0 : p ? !1 : !(l.type === "avatar_metadata" || Ot(l));
1143
- }, q = (l) => {
1144
- p = !1;
1145
- const k = l.context_id?.trim();
1146
- k && N.add(k), M(), R = !1, I.length = 0, E = !1, F?.abort(), F = null, j += 1, y(), D.bargeIn(), x(), e("idle"), de("interrupt"), T();
1147
- }, x = () => {
1161
+ }, ne = (l) => {
1162
+ const v = l.context_id?.trim();
1163
+ return v && O.has(v) ? !0 : h ? !1 : !(l.type === "avatar_metadata" || Nt(l));
1164
+ }, g = (l) => {
1165
+ h = !1;
1166
+ const v = l.context_id?.trim();
1167
+ v && O.add(v), I(), C = !1, P.length = 0, E = !1, B?.abort(), B = null, G += 1, de(), x.bargeIn(), S(), e("idle"), he("interrupt"), M();
1168
+ }, S = () => {
1148
1169
  n.length = 0, r.length = 0, i.length = 0, a = "", o = "neutral", s.length = 0;
1149
- }, P = () => {
1150
- D.stop(), x(), de("reset"), T();
1151
- }, $ = () => {
1152
- P(), e("idle"), T();
1153
- }, C = (l, k) => {
1154
- typeof l.mood == "string" && l.mood.trim() && (o = l.mood.trim()), Array.isArray(l.sentence_emotions) && l.sentence_emotions.length > 0 && (k && (s.length = 0), s.push(
1170
+ }, L = () => {
1171
+ x.stop(), S(), he("reset"), M();
1172
+ }, W = () => {
1173
+ L(), e("idle"), M();
1174
+ }, D = (l, v) => {
1175
+ typeof l.mood == "string" && l.mood.trim() && (o = l.mood.trim()), Array.isArray(l.sentence_emotions) && l.sentence_emotions.length > 0 && (v && (s.length = 0), s.push(
1155
1176
  ...Lt(l.sentence_emotions, o)
1156
1177
  ));
1157
- }, B = (l) => {
1158
- if (A({ ...l, type: "avatar_metadata" }))
1178
+ }, R = (l) => {
1179
+ if (ne({ ...l, type: "avatar_metadata" }))
1159
1180
  return;
1160
- ue(), R = !0;
1161
- const k = String(l.spoken_text ?? l.text ?? "").trim();
1162
- k && (a = k), D.resetForNewStream(f), typeof l.mood == "string" && l.mood.trim() && (o = l.mood.trim()), s.length = 0, s.push(
1181
+ fe(), C = !0;
1182
+ const v = String(l.spoken_text ?? l.text ?? "").trim();
1183
+ v && (a = v), x.resetForNewStream(d), typeof l.mood == "string" && l.mood.trim() && (o = l.mood.trim()), s.length = 0, s.push(
1163
1184
  ...Lt(l.sentence_emotions, o)
1164
- ), Array.isArray(l.gestures) && l.gestures, de("metadata", k), T();
1165
- }, Y = (l, k, V, K) => {
1166
- if (!m.onLipsyncDebug) return;
1167
- const ie = n.slice(V), Pe = K.filter(
1168
- (ae) => String(ae.symbol ?? ae.viseme ?? "").trim()
1185
+ ), Array.isArray(l.gestures) && l.gestures, he("metadata", v), M();
1186
+ }, T = (l, v, N, q) => {
1187
+ if (!f.onLipsyncDebug) return;
1188
+ const re = n.slice(N), xe = q.filter(
1189
+ (oe) => String(oe.symbol ?? oe.viseme ?? "").trim()
1169
1190
  );
1170
1191
  i.push({
1171
- spokenText: k,
1172
- audioOffsetMs: ie[0]?.vtime ?? 0,
1192
+ spokenText: v,
1193
+ audioOffsetMs: re[0]?.vtime ?? 0,
1173
1194
  isNewSegment: !!l.is_new_segment,
1174
1195
  chunkDurationMs: l.chunk_duration_ms != null ? Number(l.chunk_duration_ms) : null,
1175
1196
  receivedAtMs: performance.now(),
1176
- visemes: ie.map((ae, Re) => {
1177
- const ge = Pe[Re];
1197
+ visemes: re.map((oe, Ce) => {
1198
+ const _e = xe[Ce];
1178
1199
  return {
1179
- rawSymbol: String(ge?.symbol ?? ge?.viseme ?? ae.viseme).trim(),
1180
- viseme: ae.viseme,
1181
- startSec: Number(ge?.start ?? 0),
1182
- durationSec: Number(ge?.duration ?? 0),
1183
- vtimeMs: ae.vtime,
1184
- vdurationMs: ae.vduration
1200
+ rawSymbol: String(_e?.symbol ?? _e?.viseme ?? oe.viseme).trim(),
1201
+ viseme: oe.viseme,
1202
+ startSec: Number(_e?.start ?? 0),
1203
+ durationSec: Number(_e?.duration ?? 0),
1204
+ vtimeMs: oe.vtime,
1205
+ vdurationMs: oe.vduration
1185
1206
  };
1186
1207
  })
1187
1208
  });
1188
- }, ee = (l) => {
1189
- if (A(l)) return;
1190
- M(), Ot(l) && ue();
1191
- const k = String(l.spoken_text ?? l.text ?? "").trim(), V = Ot(l), K = l.visemes ?? [], ie = l.words ?? [], Pe = String(l.chunk ?? "").trim(), ae = R;
1192
- ae && (R = !1), k && (a = k), C(l, V), ae ? D.resetForNewStream(f) : V && D.resumeBotStream();
1193
- const Re = n.length;
1194
- D.playAudioChunk(
1209
+ }, Z = (l) => {
1210
+ if (ne(l)) return;
1211
+ I(), Nt(l) && fe();
1212
+ const v = String(l.spoken_text ?? l.text ?? "").trim(), N = Nt(l), q = l.visemes ?? [], re = l.words ?? [], xe = String(l.chunk ?? "").trim(), oe = C;
1213
+ oe && (C = !1), v && (a = v), D(l, N), oe ? x.resetForNewStream(d) : N && x.resumeBotStream();
1214
+ const Ce = n.length;
1215
+ x.playAudioChunk(
1195
1216
  n,
1196
1217
  r,
1197
- Pe,
1198
- K,
1199
- ie,
1200
- V,
1218
+ xe,
1219
+ q,
1220
+ re,
1221
+ N,
1201
1222
  {
1202
1223
  sampleRate: l.sample_rate,
1203
- newStream: ae,
1224
+ newStream: oe,
1225
+ audioOffsetMs: l.audio_offset_ms != null ? Number(l.audio_offset_ms) : void 0,
1204
1226
  onNewStream: () => {
1205
1227
  i.length = 0;
1206
1228
  }
1207
1229
  }
1208
1230
  ).then(() => {
1209
- Y(l, k, Re, K), de("audio_chunk", k);
1231
+ T(l, v, Ce, q), he("audio_chunk", v);
1210
1232
  });
1211
- }, pe = (l) => {
1212
- if (A(l)) return;
1213
- (l.is_new_segment || Number(l.audio_offset_ms ?? -1) === 0) && ue(), console.warn(
1233
+ }, ae = (l) => {
1234
+ if (ne(l)) return;
1235
+ (l.is_new_segment || Number(l.audio_offset_ms ?? -1) === 0) && fe(), console.warn(
1214
1236
  "[realtimeVoice] avatar_lipsync is deprecated — expect avatar_audio_chunk"
1215
1237
  );
1216
- const k = String(l.spoken_text ?? l.text ?? "").trim();
1217
- k && (a = k), C(l, !!l.is_new_segment);
1218
- const V = n.length;
1219
- D.queueTimingsOnly(
1238
+ const v = String(l.spoken_text ?? l.text ?? "").trim();
1239
+ v && (a = v), D(l, !!l.is_new_segment);
1240
+ const N = n.length;
1241
+ x.queueTimingsOnly(
1220
1242
  n,
1221
1243
  r,
1222
1244
  l.visemes ?? [],
1223
1245
  l.words ?? [],
1224
- !!l.is_new_segment
1225
- ), Y(
1246
+ !!l.is_new_segment,
1247
+ l.audio_offset_ms != null ? Number(l.audio_offset_ms) : void 0
1248
+ ), T(
1226
1249
  l,
1227
- k,
1228
- V,
1250
+ v,
1251
+ N,
1229
1252
  l.visemes ?? []
1230
- ), de("lipsync", k), T();
1231
- }, me = async (l) => {
1232
- const k = String(l.text ?? "").trim();
1233
- if (!k) return;
1234
- const V = m.authToken?.trim();
1235
- if (!V) {
1253
+ ), he("lipsync", v), M();
1254
+ }, te = async (l) => {
1255
+ const v = String(l.text ?? "").trim();
1256
+ if (!v) return;
1257
+ const N = f.authToken?.trim();
1258
+ if (!N) {
1236
1259
  console.error("[realtimeVoice] avatar_speak requires authToken");
1237
1260
  return;
1238
1261
  }
1239
- F?.abort();
1240
- const K = new AbortController();
1241
- F = K;
1242
- const ie = ++j;
1243
- ue(), D.resetForNewStream(f), a = k;
1244
- const ae = `${(m.apiBase ?? lt).replace(/\/$/, "")}/avatar_ttsWithPoses`, Re = m.voiceId?.trim() || er, ge = m.speakingRate ?? Pt, Ze = Math.max(
1245
- Qt,
1246
- Math.min(Zt, ge)
1247
- ), et = {
1248
- user_query: k,
1249
- ttsEngineId: f,
1250
- speakingRate: Ze,
1262
+ B?.abort();
1263
+ const q = new AbortController();
1264
+ B = q;
1265
+ const re = ++G;
1266
+ fe(), x.resetForNewStream(d), await x.primeAudioContext(), a = v;
1267
+ const oe = `${(f.apiBase ?? ft).replace(/\/$/, "")}/avatar_ttsWithPoses`, Ce = f.voiceId?.trim() || er, _e = f.speakingRate ?? Rt, et = Math.max(
1268
+ Zt,
1269
+ Math.min(en, _e)
1270
+ ), tt = {
1271
+ user_query: v,
1272
+ ttsEngineId: d,
1273
+ speakingRate: et,
1251
1274
  tenant: "aiTwin"
1252
1275
  };
1253
- $n.has(f) && (et.voice_id = Re);
1276
+ $n.has(d) && (tt.voice_id = Ce);
1254
1277
  try {
1255
- const re = await fetch(ae, {
1278
+ const se = await fetch(oe, {
1256
1279
  method: "POST",
1257
1280
  headers: {
1258
1281
  "Content-Type": "application/json",
1259
- Authorization: `Bearer ${V}`
1282
+ Authorization: `Bearer ${N}`
1260
1283
  },
1261
- body: JSON.stringify(et),
1262
- signal: K.signal
1284
+ body: JSON.stringify(tt),
1285
+ signal: q.signal
1263
1286
  });
1264
- if (!re.ok)
1265
- throw new Error(`avatar_ttsWithPoses failed (${re.status})`);
1266
- const tt = re.body;
1267
- if (!tt) throw new Error("No response body");
1268
- const dt = tt.getReader(), mt = new TextDecoder();
1287
+ if (!se.ok)
1288
+ throw new Error(`avatar_ttsWithPoses failed (${se.status})`);
1289
+ const nt = se.body;
1290
+ if (!nt) throw new Error("No response body");
1291
+ const mt = nt.getReader(), ht = new TextDecoder();
1269
1292
  let be = "";
1270
- const nt = async (ve) => {
1271
- if (ie !== j || K.signal.aborted || !p)
1293
+ const rt = async (Ee) => {
1294
+ if (re !== G || q.signal.aborted || !h)
1272
1295
  return;
1273
- const oe = Kn(ve);
1274
- if (oe) {
1275
- if (oe.event === "audio") {
1276
- const ce = oe.data.chunk, he = oe.data.visemes ?? [], J = oe.data.words ?? [], Q = oe.data.is_new_segment ?? !1, ht = oe.data.sample_rate;
1277
- ce ? await D.playAudioChunk(
1296
+ const ce = Kn(Ee);
1297
+ if (ce) {
1298
+ if (ce.event === "audio") {
1299
+ const le = ce.data.chunk, pe = ce.data.visemes ?? [], j = ce.data.words ?? [], Y = ce.data.is_new_segment ?? !1, pt = ce.data.sample_rate, we = Number(ce.data.audio_offset_ms ?? 0);
1300
+ le ? await x.playAudioChunk(
1278
1301
  n,
1279
1302
  r,
1280
- ce,
1281
- he,
1282
- J,
1283
- Q,
1284
- { sampleRate: ht }
1285
- ) : (he.length > 0 || J.length > 0) && D.queueTimingsOnly(
1303
+ le,
1304
+ pe,
1305
+ j,
1306
+ Y,
1307
+ { sampleRate: pt, audioOffsetMs: we }
1308
+ ) : (pe.length > 0 || j.length > 0) && x.queueTimingsOnly(
1286
1309
  n,
1287
1310
  r,
1288
- he,
1289
- J,
1290
- Q
1311
+ pe,
1312
+ j,
1313
+ Y,
1314
+ we
1291
1315
  );
1292
- } else if (oe.event === "metadata") {
1293
- const ce = oe.data.mood;
1294
- typeof ce == "string" && ce.trim() && (o = ce.trim());
1295
- const he = oe.data.sentence_emotions;
1296
- Array.isArray(he) && (s.length = 0, s.push(
1297
- ...Lt(he, o)
1298
- )), T();
1299
- } else if (oe.event === "error")
1316
+ } else if (ce.event === "metadata") {
1317
+ const le = ce.data.mood;
1318
+ typeof le == "string" && le.trim() && (o = le.trim());
1319
+ const pe = ce.data.sentence_emotions;
1320
+ Array.isArray(pe) && (s.length = 0, s.push(
1321
+ ...Lt(pe, o)
1322
+ )), M();
1323
+ } else if (ce.event === "error")
1300
1324
  throw new Error(
1301
- String(oe.data.message ?? "avatar_ttsWithPoses stream error")
1325
+ String(ce.data.message ?? "avatar_ttsWithPoses stream error")
1302
1326
  );
1303
1327
  }
1304
1328
  };
1305
- for (; !(ie !== j || K.signal.aborted || !p); ) {
1306
- const { done: ve, value: oe } = await dt.read();
1307
- oe && (be += mt.decode(oe, { stream: !0 }));
1308
- const ce = be.split(`
1329
+ for (; !(re !== G || q.signal.aborted || !h); ) {
1330
+ const { done: Ee, value: ce } = await mt.read();
1331
+ ce && (be += ht.decode(ce, { stream: !0 }));
1332
+ const le = be.split(`
1309
1333
 
1310
1334
  `);
1311
- be = ce.pop() ?? "";
1312
- for (const he of ce) await nt(he);
1313
- if (ve) {
1314
- be.trim() && await nt(be.trim()), ie === j && !K.signal.aborted && p && D.markStreamComplete(n);
1335
+ be = le.pop() ?? "";
1336
+ for (const pe of le) await rt(pe);
1337
+ if (Ee) {
1338
+ be.trim() && await rt(be.trim()), re === G && !q.signal.aborted && h && x.markStreamComplete(n);
1315
1339
  break;
1316
1340
  }
1317
1341
  }
1318
- } catch (re) {
1319
- if (K.signal.aborted || !p) return;
1320
- console.error("[realtimeVoice] avatar_speak failed:", re), e("error"), T();
1342
+ } catch (se) {
1343
+ if (q.signal.aborted || !h) return;
1344
+ console.error("[realtimeVoice] avatar_speak failed:", se), e("error"), M();
1321
1345
  } finally {
1322
- F === K && (F = null);
1346
+ B === q && (B = null);
1323
1347
  }
1324
- }, le = (l) => {
1325
- const k = xs(l);
1326
- if (!k) {
1348
+ }, ue = (l) => {
1349
+ const v = xs(l);
1350
+ if (!v) {
1327
1351
  console.warn("[realtimeVoice] server message with no payload", l);
1328
1352
  return;
1329
1353
  }
1330
- const V = String(k.type ?? "unknown");
1331
- switch (V !== "avatar_metadata" && V !== "avatar_audio_chunk" && V !== "avatar_lipsync" && V !== "avatar_interrupt" && V !== "error" && console.debug("[realtimeVoice] server message", V, k), V) {
1354
+ const N = String(v.type ?? "unknown");
1355
+ switch (N !== "avatar_metadata" && N !== "avatar_audio_chunk" && N !== "avatar_lipsync" && N !== "avatar_interrupt" && N !== "error" && console.debug("[realtimeVoice] server message", N, v), N) {
1332
1356
  case "avatar_metadata":
1333
- B(k);
1357
+ R(v);
1334
1358
  break;
1335
1359
  case "avatar_audio_chunk":
1336
- ee(k);
1360
+ Z(v);
1337
1361
  break;
1338
1362
  case "avatar_lipsync":
1339
- pe(k);
1363
+ ae(v);
1340
1364
  break;
1341
1365
  case "avatar_interrupt":
1342
- q(k);
1366
+ g(v);
1343
1367
  break;
1344
1368
  case "error": {
1345
- const K = String(k.message ?? "Voice session error");
1346
- console.error("[realtimeVoice] server error:", K), e("error"), T();
1369
+ const q = String(v.message ?? "Voice session error");
1370
+ console.error("[realtimeVoice] server error:", q), e("error"), M();
1347
1371
  break;
1348
1372
  }
1349
1373
  case "avatar_speak":
1350
- me(k);
1374
+ te(v);
1351
1375
  break;
1352
1376
  }
1353
- }, fe = () => u || (c = new Is(), u = new kr({
1354
- transport: new br({
1355
- mediaManager: c,
1377
+ }, me = () => c || (u = new Is(), c = new kr({
1378
+ transport: new vr({
1379
+ mediaManager: u,
1356
1380
  serializer: new As(),
1357
1381
  recorderSampleRate: tr,
1358
1382
  playerSampleRate: nr
@@ -1361,109 +1385,109 @@ function Cs(e, t = {}) {
1361
1385
  enableMic: !0,
1362
1386
  callbacks: {
1363
1387
  onBotReady: () => {
1364
- d = !0, e("idle"), T();
1388
+ m = !0, e("idle"), M();
1365
1389
  },
1366
1390
  onDisconnected: () => {
1367
- d = !1, $();
1391
+ m = !1, W();
1368
1392
  },
1369
1393
  onError: (l) => {
1370
- const k = typeof l == "object" && l !== null && "data" in l ? String(l.data ?? l) : String(l);
1371
- console.error("[realtimeVoice]", k, l), e("error"), T();
1394
+ const v = typeof l == "object" && l !== null && "data" in l ? String(l.data ?? l) : String(l);
1395
+ console.error("[realtimeVoice]", v, l), e("error"), M();
1372
1396
  },
1373
1397
  onTransportStateChanged: (l) => {
1374
- l === "error" && (e("error"), T());
1398
+ l === "error" && (e("error"), M());
1375
1399
  },
1376
1400
  onServerMessage: (l) => {
1377
- z(l);
1401
+ J(l);
1378
1402
  },
1379
1403
  onUserStartedSpeaking: () => {
1380
- q({});
1404
+ g({});
1381
1405
  },
1382
1406
  onBotStartedSpeaking: () => {
1383
- ue(), de("bot_started");
1407
+ fe(), he("bot_started");
1384
1408
  },
1385
1409
  onBotStoppedSpeaking: () => {
1386
- p && (M(), S = setTimeout(() => {
1387
- S = null, p && (D.markStreamComplete(n), de("bot_stopped"));
1410
+ h && (I(), k = setTimeout(() => {
1411
+ k = null, h && (x.markStreamComplete(n), he("bot_stopped"));
1388
1412
  }, 450));
1389
1413
  },
1390
1414
  onUserTranscript: (l) => {
1391
- m.onUserTranscript?.(l.text, !!l.final);
1415
+ f.onUserTranscript?.(l.text, !!l.final);
1392
1416
  },
1393
1417
  onBotOutput: (l) => {
1394
- m.onBotOutput?.(l.text);
1418
+ f.onBotOutput?.(l.text);
1395
1419
  }
1396
1420
  }
1397
- }), u);
1421
+ }), c);
1398
1422
  return {
1399
1423
  setDeveloperToken: (l) => {
1400
1424
  },
1401
1425
  setTtsEngineId: (l) => {
1402
- f = l, D.setStreamTtsEngineId(l);
1426
+ d = l, x.setStreamTtsEngineId(l);
1403
1427
  },
1404
- getTtsEngineId: () => f,
1405
- speak: async (l, k) => {
1406
- const V = l.trim();
1407
- if (V) {
1408
- if (!u || !d)
1428
+ getTtsEngineId: () => d,
1429
+ speak: async (l, v) => {
1430
+ const N = l.trim();
1431
+ if (N) {
1432
+ if (!c || !m)
1409
1433
  throw new Error(
1410
1434
  "Realtime voice is not connected. Call connect() first."
1411
1435
  );
1412
- P(), await u.sendText(V);
1436
+ L(), await c.sendText(N);
1413
1437
  }
1414
1438
  },
1415
1439
  stop: () => {
1416
- F?.abort(), F = null, j += 1, ue(), P(), e("idle"), T();
1440
+ B?.abort(), B = null, G += 1, fe(), L(), e("idle"), M();
1417
1441
  },
1418
1442
  connect: async (l) => {
1419
- if (l && (m = { ...m, ...l }), !m.authToken?.trim())
1443
+ if (l && (f = { ...f, ...l }), !f.authToken?.trim())
1420
1444
  throw new Error(
1421
1445
  "Voice connect requires authToken (same JWT as SSE Authorization Bearer)."
1422
1446
  );
1423
1447
  e("loading");
1424
- const k = fe();
1425
- await k.initDevices();
1426
- const V = Ds(m);
1427
- console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):", V);
1448
+ const v = me();
1449
+ await v.initDevices();
1450
+ const N = Ds(f);
1451
+ console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):", N);
1428
1452
  try {
1429
- await k.connect({ wsUrl: V });
1430
- } catch (K) {
1431
- d = !1, e("error"), T();
1432
- const ie = K instanceof Error ? K.message : "Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";
1433
- throw new Error(`${ie} — ${V}`);
1453
+ await v.connect({ wsUrl: N });
1454
+ } catch (q) {
1455
+ m = !1, e("error"), M();
1456
+ const re = q instanceof Error ? q.message : "Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";
1457
+ throw new Error(`${re} — ${N}`);
1434
1458
  }
1435
- d = !0, e("idle"), T();
1459
+ m = !0, e("idle"), M();
1436
1460
  },
1437
1461
  disconnect: async () => {
1438
- if (M(), R = !1, I.length = 0, E = !1, F?.abort(), F = null, j += 1, ue(), u)
1462
+ if (I(), C = !1, P.length = 0, E = !1, B?.abort(), B = null, G += 1, fe(), c)
1439
1463
  try {
1440
- await u.disconnect();
1464
+ await c.disconnect();
1441
1465
  } catch (l) {
1442
1466
  console.error("[realtimeVoice] disconnect failed:", l);
1443
1467
  }
1444
- d = !1, $();
1468
+ m = !1, W();
1445
1469
  },
1446
- isConnected: () => d,
1470
+ isConnected: () => m,
1447
1471
  unlockAudio: async () => {
1448
- await D.primeAudioContext();
1472
+ await x.primeAudioContext();
1449
1473
  },
1450
1474
  getVisemeQueue: () => n,
1451
1475
  getWordQueue: () => r,
1452
1476
  getSentenceEmotions: () => s,
1453
1477
  getStreamMood: () => o,
1454
- getPlaybackElapsedMs: se,
1455
- isSpeaking: () => D.isSpeaking(),
1456
- isWarmingUp: () => D.isWarmingUp(),
1457
- releaseWarmupHold: () => D.releaseWarmupHold(),
1458
- setLipsyncWarmupHold: (l, k) => {
1459
- k && (Z = k), D.setLipsyncWarmupHoldEnabled(l);
1460
- },
1461
- getVoiceStream: () => D.getVoiceStream(),
1478
+ getPlaybackElapsedMs: ie,
1479
+ isSpeaking: () => x.isSpeaking(),
1480
+ isWarmingUp: () => x.isWarmingUp(),
1481
+ releaseWarmupHold: () => x.releaseWarmupHold(),
1482
+ setLipsyncWarmupHold: (l, v) => {
1483
+ v && (Q = v), x.setLipsyncWarmupHoldEnabled(l);
1484
+ },
1485
+ getVoiceStream: () => x.getVoiceStream(),
1462
1486
  setPlaybackSpeed: (l) => {
1463
- D.setPlaybackSpeed(l);
1487
+ x.setPlaybackSpeed(l);
1464
1488
  },
1465
- getPlaybackSpeed: () => D.getPlaybackSpeed(),
1466
- subscribe: (l) => (h.add(l), () => h.delete(l))
1489
+ getPlaybackSpeed: () => x.getPlaybackSpeed(),
1490
+ subscribe: (l) => (p.add(l), () => p.delete(l))
1467
1491
  };
1468
1492
  }
1469
1493
  function Fs(e, t, n) {
@@ -1472,7 +1496,7 @@ function Fs(e, t, n) {
1472
1496
  height: e.crop?.source_height ?? n
1473
1497
  };
1474
1498
  }
1475
- function Ls(e, t) {
1499
+ function Os(e, t) {
1476
1500
  const n = e.crop;
1477
1501
  return {
1478
1502
  x: t?.source_x ?? n?.x ?? 0,
@@ -1481,10 +1505,10 @@ function Ls(e, t) {
1481
1505
  height: t?.source_h ?? n?.height ?? e.frame_height ?? 0
1482
1506
  };
1483
1507
  }
1484
- function Os(e) {
1508
+ function Ls(e) {
1485
1509
  return e ? (e.w ?? 0) > 0 && (e.h ?? 0) > 0 : !1;
1486
1510
  }
1487
- async function Rt(e, t) {
1511
+ async function Dt(e, t) {
1488
1512
  const n = await fetch(e);
1489
1513
  if (!n.ok) throw new Error(`Failed to load encrypted asset: ${e}`);
1490
1514
  const r = await n.arrayBuffer();
@@ -1492,7 +1516,7 @@ async function Rt(e, t) {
1492
1516
  }
1493
1517
  async function Ns(e, t) {
1494
1518
  if (!t) throw new Error("Missing asset key for encrypted JSON");
1495
- const n = await Rt(e, t), r = new TextDecoder().decode(n);
1519
+ const n = await Dt(e, t), r = new TextDecoder().decode(n);
1496
1520
  return JSON.parse(r);
1497
1521
  }
1498
1522
  async function Us(e) {
@@ -1523,16 +1547,16 @@ function Ws(e) {
1523
1547
  }
1524
1548
  async function Hs(e) {
1525
1549
  const t = Qr();
1526
- if (!It()) return t;
1550
+ if (!Mt()) return t;
1527
1551
  if (!e) throw new Error("Missing asset key for encrypted idle video");
1528
- const n = await Rt(t, e);
1552
+ const n = await Dt(t, e);
1529
1553
  return Ws(n);
1530
1554
  }
1531
- function en(e, t) {
1555
+ function tn(e, t) {
1532
1556
  return `${e}/${t}_minus_sil.png`;
1533
1557
  }
1534
1558
  const rr = 5;
1535
- function tn(e) {
1559
+ function nn(e) {
1536
1560
  const t = e?.composite_threshold;
1537
1561
  if (typeof t == "number" && t > 0) return t;
1538
1562
  const n = e?.crop?.content_threshold;
@@ -1553,11 +1577,11 @@ const ta = [
1553
1577
  "RR",
1554
1578
  "kk",
1555
1579
  "nn"
1556
- ], Ht = [
1580
+ ], $t = [
1557
1581
  "t01_0.25",
1558
1582
  "t02_0.50",
1559
1583
  "t03_0.75"
1560
- ], $s = Ht.length, In = /* @__PURE__ */ new Set([
1584
+ ], $s = $t.length, In = /* @__PURE__ */ new Set([
1561
1585
  "aa__kk",
1562
1586
  "aa__nn",
1563
1587
  "aa__sil",
@@ -1670,7 +1694,7 @@ function Mn(e, t) {
1670
1694
  function Pn(e, t) {
1671
1695
  return `pairs/${e}/${t}_minus_sil.png`;
1672
1696
  }
1673
- function vt(e) {
1697
+ function Et(e) {
1674
1698
  return `${e}_minus_sil.png`;
1675
1699
  }
1676
1700
  function sr(e, t) {
@@ -1679,28 +1703,28 @@ function sr(e, t) {
1679
1703
  if (!n) return null;
1680
1704
  const r = n[1].split("__")[1];
1681
1705
  if (!r || r === "sil") return null;
1682
- const s = vt(r);
1706
+ const s = Et(r);
1683
1707
  return t.has(s) ? s : null;
1684
1708
  }
1685
1709
  function ir(e, t) {
1686
1710
  return e.filter((n) => t.has(n));
1687
1711
  }
1688
1712
  function qs(e, t, n) {
1689
- const r = nn(e, t);
1713
+ const r = rn(e, t);
1690
1714
  return ir(r, n).length > 0;
1691
1715
  }
1692
- function nn(e, t) {
1716
+ function rn(e, t) {
1693
1717
  if (e === t) return [];
1694
1718
  const n = [], r = Mn(e, t);
1695
1719
  if (In.has(r)) {
1696
- for (const i of Ht)
1720
+ for (const i of $t)
1697
1721
  n.push(Pn(r, i));
1698
1722
  return n;
1699
1723
  }
1700
1724
  const s = Mn(t, e);
1701
1725
  if (In.has(s)) {
1702
1726
  for (let i = $s - 1; i >= 0; i--)
1703
- n.push(Pn(s, Ht[i]));
1727
+ n.push(Pn(s, $t[i]));
1704
1728
  return n;
1705
1729
  }
1706
1730
  return [];
@@ -1732,7 +1756,7 @@ const Ys = [0.32, 0.24, 0.16, 0.08, 0], Js = Ys.map(
1732
1756
  "eyes_0.16",
1733
1757
  "eyes_0.08",
1734
1758
  "eyes_0.00"
1735
- ], Rn = 2, Zs = -30, $t = 20, ei = {
1759
+ ], Rn = 2, Zs = -30, qt = 20, ei = {
1736
1760
  fear: -30,
1737
1761
  anger: -20,
1738
1762
  sad: -10,
@@ -1741,14 +1765,14 @@ const Ys = [0.32, 0.24, 0.16, 0.08, 0], Js = Ys.map(
1741
1765
  happy: 0,
1742
1766
  disgust: 20
1743
1767
  };
1744
- function rn(e) {
1768
+ function sn(e) {
1745
1769
  return `brow_${e}`;
1746
1770
  }
1747
- function sn(e) {
1771
+ function an(e) {
1748
1772
  return ei[e];
1749
1773
  }
1750
- function Oe(e) {
1751
- return Math.max(Zs, Math.min($t, e));
1774
+ function Ve(e) {
1775
+ return Math.max(Zs, Math.min(qt, e));
1752
1776
  }
1753
1777
  function ti(e, t) {
1754
1778
  if (e === t) return [];
@@ -1781,19 +1805,19 @@ const ni = {
1781
1805
  disgust: "disgust",
1782
1806
  disgusted: "disgust"
1783
1807
  };
1784
- function Ge(e, t = "neutral") {
1808
+ function je(e, t = "neutral") {
1785
1809
  const n = String(e ?? "").trim().toLowerCase();
1786
1810
  return n ? ni[n] ?? t : t;
1787
1811
  }
1788
1812
  function ar(e) {
1789
- return e === 0 ? null : en(rn(e), "eyes_standard");
1813
+ return e === 0 ? null : tn(sn(e), "eyes_standard");
1790
1814
  }
1791
1815
  function or(e) {
1792
- return ar(sn(e));
1816
+ return ar(an(e));
1793
1817
  }
1794
1818
  function Dn(e, t) {
1795
- const n = rn(sn(e));
1796
- return !t || t === "eyes_standard" ? or(e) : e === "neutral" && t === "eyes_standard" ? null : en(n, t);
1819
+ const n = sn(an(e));
1820
+ return !t || t === "eyes_standard" ? or(e) : e === "neutral" && t === "eyes_standard" ? null : tn(n, t);
1797
1821
  }
1798
1822
  function ri(e, t) {
1799
1823
  const n = [];
@@ -1804,7 +1828,7 @@ function ri(e, t) {
1804
1828
  return n.push(or(e)), n;
1805
1829
  }
1806
1830
  function si(e) {
1807
- return e === 0 ? null : en(rn(e), "eyes_standard");
1831
+ return e === 0 ? null : tn(sn(e), "eyes_standard");
1808
1832
  }
1809
1833
  function ii(e, t) {
1810
1834
  return ti(e, t).map(si).filter((n) => n != null);
@@ -1817,11 +1841,11 @@ function oi(e, t) {
1817
1841
  return null;
1818
1842
  }
1819
1843
  function ci(e) {
1820
- const t = Oe(e);
1821
- return t >= $t ? $t : Oe(t + ai);
1844
+ const t = Ve(e);
1845
+ return t >= qt ? qt : Ve(t + ai);
1822
1846
  }
1823
1847
  function ui(e, t, n) {
1824
- const r = Oe(n);
1848
+ const r = Ve(n);
1825
1849
  return oi(t, e)?.emphasis ? ci(r) : r;
1826
1850
  }
1827
1851
  function xn(e) {
@@ -1829,13 +1853,13 @@ function xn(e) {
1829
1853
  const t = e.match(/^brow_(-?\d+)\//);
1830
1854
  if (!t?.[1]) return null;
1831
1855
  const n = Number.parseInt(t[1], 10);
1832
- return Number.isFinite(n) ? Oe(n) : null;
1856
+ return Number.isFinite(n) ? Ve(n) : null;
1833
1857
  }
1834
- function ct(e, t) {
1858
+ function ut(e, t) {
1835
1859
  return e + Math.random() * (t - e);
1836
1860
  }
1837
- function St() {
1838
- return ct(14, 32);
1861
+ function kt() {
1862
+ return ut(14, 32);
1839
1863
  }
1840
1864
  function li(e = {}) {
1841
1865
  const {
@@ -1846,127 +1870,127 @@ function li(e = {}) {
1846
1870
  doubleBlinkGapMinMs: i = 120,
1847
1871
  doubleBlinkGapMaxMs: a = 220,
1848
1872
  fullBlinkChance: o = 0.75,
1849
- postEmotionChangeDelayMs: f = 380,
1850
- minDwellMs: d = 550
1873
+ postEmotionChangeDelayMs: d = 380,
1874
+ minDwellMs: m = 550
1851
1875
  } = e;
1852
- let u = "neutral", c = "neutral", m = 0, w = 0, b = 0, h = {
1876
+ let c = "neutral", u = "neutral", f = 0, w = 0, b = 0, p = {
1853
1877
  kind: "idle",
1854
1878
  nextBlinkAt: performance.now() + t
1855
- }, I = null, E = "__eyes_open__|neutral|brow:0";
1856
- function _(S = u) {
1857
- E = `${I ?? "__eyes_open__"}|${S}|brow:${w}`;
1879
+ }, P = null, E = "__eyes_open__|neutral|brow:0";
1880
+ function _(k = c) {
1881
+ E = `${P ?? "__eyes_open__"}|${k}|brow:${w}`;
1858
1882
  }
1859
- function p(S, M = u) {
1860
- I = S;
1861
- const T = xn(S);
1862
- T != null ? w = T : S == null && (w = 0), _(M);
1883
+ function h(k, I = c) {
1884
+ P = k;
1885
+ const M = xn(k);
1886
+ M != null ? w = M : k == null && (w = 0), _(I);
1863
1887
  }
1864
- function v(S) {
1865
- return ar(S);
1888
+ function A(k) {
1889
+ return ar(k);
1866
1890
  }
1867
- function N(S, M, T) {
1868
- const Z = Oe(M), D = Oe(T);
1869
- if (m = D, Z === D) {
1870
- p(v(D));
1891
+ function O(k, I, M) {
1892
+ const Q = Ve(I), x = Ve(M);
1893
+ if (f = x, Q === x) {
1894
+ h(A(x));
1871
1895
  return;
1872
1896
  }
1873
- const X = [
1874
- ...ii(Z, D),
1875
- v(D)
1897
+ const z = [
1898
+ ...ii(Q, x),
1899
+ A(x)
1876
1900
  ];
1877
- h = {
1901
+ p = {
1878
1902
  kind: "browBlend",
1879
- paths: X,
1903
+ paths: z,
1880
1904
  index: 0,
1881
- holdUntil: S + St(),
1882
- targetBrow: D
1883
- }, p(X[0] ?? null);
1905
+ holdUntil: k + kt(),
1906
+ targetBrow: x
1907
+ }, h(z[0] ?? null);
1884
1908
  }
1885
- function F(S, M) {
1886
- h = {
1909
+ function B(k, I) {
1910
+ p = {
1887
1911
  kind: "idle",
1888
- nextBlinkAt: S + ct(n, r)
1889
- }, c = M, p(v(m), M);
1912
+ nextBlinkAt: k + ut(n, r)
1913
+ }, u = I, h(A(f), I);
1890
1914
  }
1891
- function j(S, M, T) {
1892
- const Z = T ? Xs : Math.random() < o ? Js : Qs;
1893
- h = {
1915
+ function G(k, I, M) {
1916
+ const Q = M ? Xs : Math.random() < o ? Js : Qs;
1917
+ p = {
1894
1918
  kind: "playing",
1895
- paths: ri(M, Z),
1919
+ paths: ri(I, Q),
1896
1920
  index: 0,
1897
- holdUntil: S + St(),
1921
+ holdUntil: k + kt(),
1898
1922
  after: "idle",
1899
- blinkEmotion: M
1900
- }, c = M, p(h.paths[0] ?? null, M);
1923
+ blinkEmotion: I
1924
+ }, u = I, h(p.paths[0] ?? null, I);
1901
1925
  }
1902
- function R(S) {
1903
- if (h.kind === "browBlend") {
1904
- for (; S >= h.holdUntil && h.index < h.paths.length - 1; )
1905
- h.index += 1, h.holdUntil = S + St();
1906
- const M = h.paths[h.index] ?? null;
1907
- p(M), S >= h.holdUntil && h.index >= h.paths.length - 1 && (w = h.targetBrow, m = h.targetBrow, p(v(h.targetBrow)), h = {
1926
+ function C(k) {
1927
+ if (p.kind === "browBlend") {
1928
+ for (; k >= p.holdUntil && p.index < p.paths.length - 1; )
1929
+ p.index += 1, p.holdUntil = k + kt();
1930
+ const I = p.paths[p.index] ?? null;
1931
+ h(I), k >= p.holdUntil && p.index >= p.paths.length - 1 && (w = p.targetBrow, f = p.targetBrow, h(A(p.targetBrow)), p = {
1908
1932
  kind: "idle",
1909
- nextBlinkAt: S + f
1933
+ nextBlinkAt: k + d
1910
1934
  });
1911
1935
  return;
1912
1936
  }
1913
- if (h.kind === "idle") {
1914
- p(v(m), c), S >= h.nextBlinkAt && j(S, c, !1);
1937
+ if (p.kind === "idle") {
1938
+ h(A(f), u), k >= p.nextBlinkAt && G(k, u, !1);
1915
1939
  return;
1916
1940
  }
1917
- if (h.kind === "doublePause") {
1918
- p(v(m), h.blinkEmotion), S >= h.resumeAt && j(S, h.blinkEmotion, !0);
1941
+ if (p.kind === "doublePause") {
1942
+ h(A(f), p.blinkEmotion), k >= p.resumeAt && G(k, p.blinkEmotion, !0);
1919
1943
  return;
1920
1944
  }
1921
- if (h.kind === "playing") {
1922
- for (; S >= h.holdUntil && h.index < h.paths.length - 1; )
1923
- h.index += 1, h.holdUntil = S + St();
1924
- if (p(h.paths[h.index] ?? null, h.blinkEmotion), S >= h.holdUntil && h.index >= h.paths.length - 1) {
1925
- if (h.after === "resumeIdle") {
1926
- F(S + f, u);
1945
+ if (p.kind === "playing") {
1946
+ for (; k >= p.holdUntil && p.index < p.paths.length - 1; )
1947
+ p.index += 1, p.holdUntil = k + kt();
1948
+ if (h(p.paths[p.index] ?? null, p.blinkEmotion), k >= p.holdUntil && p.index >= p.paths.length - 1) {
1949
+ if (p.after === "resumeIdle") {
1950
+ B(k + d, c);
1927
1951
  return;
1928
1952
  }
1929
- if (h.after === "doublePause") {
1930
- h = {
1953
+ if (p.after === "doublePause") {
1954
+ p = {
1931
1955
  kind: "doublePause",
1932
- resumeAt: S + ct(i, a),
1933
- blinkEmotion: h.blinkEmotion
1934
- }, p(v(m), h.blinkEmotion);
1956
+ resumeAt: k + ut(i, a),
1957
+ blinkEmotion: p.blinkEmotion
1958
+ }, h(A(f), p.blinkEmotion);
1935
1959
  return;
1936
1960
  }
1937
- if (h.after === "idle" && Math.random() < s) {
1938
- h = {
1961
+ if (p.after === "idle" && Math.random() < s) {
1962
+ p = {
1939
1963
  kind: "doublePause",
1940
- resumeAt: S + ct(i, a),
1941
- blinkEmotion: h.blinkEmotion
1942
- }, p(v(m), h.blinkEmotion);
1964
+ resumeAt: k + ut(i, a),
1965
+ blinkEmotion: p.blinkEmotion
1966
+ }, h(A(f), p.blinkEmotion);
1943
1967
  return;
1944
1968
  }
1945
- F(S, h.blinkEmotion);
1969
+ B(k, p.blinkEmotion);
1946
1970
  }
1947
1971
  }
1948
1972
  }
1949
1973
  return {
1950
- advance: R,
1951
- setTargetEmotion(S) {
1952
- const M = Ge(S, u);
1953
- if (M === u) return;
1954
- const T = performance.now();
1955
- d > 0 && M !== u && !(u === "neutral" && M !== "neutral") && T - b < d || (u = M, c = M, b = T);
1956
- },
1957
- setTargetEyebrow(S) {
1958
- const M = Oe(S);
1959
- if (M === m) return;
1960
- const T = performance.now(), Z = h.kind === "browBlend" ? xn(I) ?? w : w;
1961
- m = M, !(M === Z && h.kind !== "browBlend") && (h.kind === "playing" || h.kind === "doublePause" || N(T, Z, M));
1962
- },
1963
- getTargetEmotion: () => u,
1964
- getTargetEyebrow: () => m,
1965
- getExpressionPath: () => I,
1974
+ advance: C,
1975
+ setTargetEmotion(k) {
1976
+ const I = je(k, c);
1977
+ if (I === c) return;
1978
+ const M = performance.now();
1979
+ m > 0 && I !== c && !(c === "neutral" && I !== "neutral") && M - b < m || (c = I, u = I, b = M);
1980
+ },
1981
+ setTargetEyebrow(k) {
1982
+ const I = Ve(k);
1983
+ if (I === f) return;
1984
+ const M = performance.now(), Q = p.kind === "browBlend" ? xn(P) ?? w : w;
1985
+ f = I, !(I === Q && p.kind !== "browBlend") && (p.kind === "playing" || p.kind === "doublePause" || O(M, Q, I));
1986
+ },
1987
+ getTargetEmotion: () => c,
1988
+ getTargetEyebrow: () => f,
1989
+ getExpressionPath: () => P,
1966
1990
  getDrawKey: () => E,
1967
1991
  reset() {
1968
- u = "neutral", c = "neutral", m = 0, w = 0, b = 0, F(
1969
- performance.now() + ct(n, r),
1992
+ c = "neutral", u = "neutral", f = 0, w = 0, b = 0, B(
1993
+ performance.now() + ut(n, r),
1970
1994
  "neutral"
1971
1995
  );
1972
1996
  }
@@ -1982,15 +2006,15 @@ function fi() {
1982
2006
  return {
1983
2007
  async load(i) {
1984
2008
  t && r === i || (s(), t = !1, r = i, i.startsWith("blob:") && (n = i), await new Promise((a, o) => {
1985
- const f = () => {
1986
- const d = e.duration;
1987
- if (!Number.isFinite(d) || d <= 0) {
2009
+ const d = () => {
2010
+ const m = e.duration;
2011
+ if (!Number.isFinite(m) || m <= 0) {
1988
2012
  o(new Error(`Idle video has invalid duration: ${i}`));
1989
2013
  return;
1990
2014
  }
1991
2015
  t = !0, e.currentTime = 0, a();
1992
2016
  };
1993
- e.addEventListener("loadeddata", f, { once: !0 }), e.addEventListener(
2017
+ e.addEventListener("loadeddata", d, { once: !0 }), e.addEventListener(
1994
2018
  "error",
1995
2019
  () => o(new Error(`Failed to load idle video: ${i}`)),
1996
2020
  { once: !0 }
@@ -2041,92 +2065,92 @@ function hi(e, t) {
2041
2065
  return n.sort((r, s) => r.startMs - s.startMs);
2042
2066
  }
2043
2067
  function pi(e, t, n, r = "neutral") {
2044
- const s = Ge(r);
2068
+ const s = je(r);
2045
2069
  if (t.length === 0) return s;
2046
2070
  if (n.length === 0 || e < n[0].wtime)
2047
- return Ge(t[0]?.emotion, s);
2071
+ return je(t[0]?.emotion, s);
2048
2072
  const i = hi(t, n);
2049
2073
  let a = null;
2050
- for (const d of i)
2051
- e >= d.startMs && e <= d.endMs && (a = d);
2074
+ for (const m of i)
2075
+ e >= m.startMs && e <= m.endMs && (a = m);
2052
2076
  if (a)
2053
- return Ge(a.sentence.emotion, s);
2054
- const o = n[n.length - 1], f = o.wtime + o.wduration;
2055
- return e > f + cr ? Ge(
2077
+ return je(a.sentence.emotion, s);
2078
+ const o = n[n.length - 1], d = o.wtime + o.wduration;
2079
+ return e > d + cr ? je(
2056
2080
  t[t.length - 1]?.emotion,
2057
2081
  s
2058
2082
  ) : s;
2059
2083
  }
2060
- function an(e, t, n, r, s = rr) {
2084
+ function on(e, t, n, r, s = rr) {
2061
2085
  const i = new OffscreenCanvas(n, r), a = i.getContext("2d", { willReadFrequently: !0 });
2062
2086
  a.drawImage(e, 0, 0, n, r);
2063
- const o = a.getImageData(0, 0, n, r), f = a.createImageData(n, r);
2064
- f.data.set(o.data);
2065
- const u = new OffscreenCanvas(n, r).getContext("2d", { willReadFrequently: !0 });
2066
- for (const c of t) {
2067
- u.clearRect(0, 0, n, r), u.drawImage(c, 0, 0, n, r);
2068
- const m = u.getImageData(0, 0, n, r);
2069
- for (let w = 0; w < f.data.length; w += 4) {
2070
- const b = m.data[w], h = m.data[w + 1], I = m.data[w + 2];
2071
- Math.max(b, h, I) > s && (f.data[w] = b, f.data[w + 1] = h, f.data[w + 2] = I, f.data[w + 3] = 255);
2087
+ const o = a.getImageData(0, 0, n, r), d = a.createImageData(n, r);
2088
+ d.data.set(o.data);
2089
+ const c = new OffscreenCanvas(n, r).getContext("2d", { willReadFrequently: !0 });
2090
+ for (const u of t) {
2091
+ c.clearRect(0, 0, n, r), c.drawImage(u, 0, 0, n, r);
2092
+ const f = c.getImageData(0, 0, n, r);
2093
+ for (let w = 0; w < d.data.length; w += 4) {
2094
+ const b = f.data[w], p = f.data[w + 1], P = f.data[w + 2];
2095
+ Math.max(b, p, P) > s && (d.data[w] = b, d.data[w + 1] = p, d.data[w + 2] = P, d.data[w + 3] = 255);
2072
2096
  }
2073
2097
  }
2074
- return a.putImageData(f, 0, 0), i;
2098
+ return a.putImageData(d, 0, 0), i;
2075
2099
  }
2076
- let Le = null, Ye = null;
2077
- const Et = /* @__PURE__ */ new Map();
2100
+ let Ue = null, Je = null;
2101
+ const Tt = /* @__PURE__ */ new Map();
2078
2102
  function wi() {
2079
- Le = null, Ye = null, Et.clear();
2103
+ Ue = null, Je = null, Tt.clear();
2080
2104
  }
2081
2105
  Xr(wi);
2082
2106
  function gi(e) {
2083
- return e ?? Ye ?? void 0;
2107
+ return e ?? Je ?? void 0;
2084
2108
  }
2085
- function on(e) {
2109
+ function cn(e) {
2086
2110
  return new Promise((t, n) => {
2087
2111
  const r = new Image();
2088
2112
  r.decoding = "async", r.onload = () => t(r), r.onerror = () => n(new Error(`Failed to load image: ${e}`)), r.src = e;
2089
2113
  });
2090
2114
  }
2091
2115
  async function yi(e) {
2092
- return It() ? Vs(
2093
- await Rt(vn(), e ?? "")
2094
- ) : on(vn());
2095
- }
2096
- async function Bn(e, t, n, r) {
2097
- const s = n ? await Ns(e, r) : await fetch(e).then((d) => {
2098
- if (!d.ok) throw new Error(`Failed to load ${e}`);
2099
- return d.json();
2116
+ return Mt() ? Vs(
2117
+ await Dt(bn(), e ?? "")
2118
+ ) : cn(bn());
2119
+ }
2120
+ async function Cn(e, t, n, r) {
2121
+ const s = n ? await Ns(e, r) : await fetch(e).then((m) => {
2122
+ if (!m.ok) throw new Error(`Failed to load ${e}`);
2123
+ return m.json();
2100
2124
  }), i = s.sheets?.[0];
2101
2125
  if (!i?.path) throw new Error(`${e} has no sheets[0].path`);
2102
2126
  const a = n ? await Us(
2103
- await Rt(
2127
+ await Dt(
2104
2128
  zs(t, i.path, n),
2105
2129
  r ?? ""
2106
2130
  )
2107
- ) : await on(`${t}/${i.path}`), o = /* @__PURE__ */ new Map();
2108
- for (const d of s.cells ?? [])
2109
- d.path && o.set(d.path, d);
2110
- const f = /* @__PURE__ */ new Map();
2111
- for (const d of s.sheets ?? [])
2112
- f.set(d.index, d);
2113
- return { atlas: a, atlasMeta: s, cellByPath: o, sheetByIndex: f, diffBase: t };
2114
- }
2115
- async function qt(e) {
2116
- const t = gi(e), n = It();
2131
+ ) : await cn(`${t}/${i.path}`), o = /* @__PURE__ */ new Map();
2132
+ for (const m of s.cells ?? [])
2133
+ m.path && o.set(m.path, m);
2134
+ const d = /* @__PURE__ */ new Map();
2135
+ for (const m of s.sheets ?? [])
2136
+ d.set(m.index, m);
2137
+ return { atlas: a, atlasMeta: s, cellByPath: o, sheetByIndex: d, diffBase: t };
2138
+ }
2139
+ async function Kt(e) {
2140
+ const t = gi(e), n = Mt();
2117
2141
  if (n && !t)
2118
2142
  throw new Error("Encrypted assets enabled but no key provided");
2119
- if (Le && (!n || Ye === (t ?? null))) {
2120
- const i = await Le;
2143
+ if (Ue && (!n || Je === (t ?? null))) {
2144
+ const i = await Ue;
2121
2145
  return {
2122
- ...Je(i),
2146
+ ...Xe(i),
2123
2147
  visemePaths: [...i.viseme.cellByPath.keys()]
2124
2148
  };
2125
2149
  }
2126
- Ye = t ?? null, Et.clear();
2150
+ Je = t ?? null, Tt.clear();
2127
2151
  const r = Zr();
2128
- Le = (async () => {
2129
- const i = await yi(t), a = await Bn(
2152
+ Ue = (async () => {
2153
+ const i = await yi(t), a = await Cn(
2130
2154
  es(),
2131
2155
  r,
2132
2156
  n,
@@ -2134,7 +2158,7 @@ async function qt(e) {
2134
2158
  );
2135
2159
  let o = null;
2136
2160
  try {
2137
- o = await Bn(
2161
+ o = await Cn(
2138
2162
  ts(),
2139
2163
  r,
2140
2164
  n,
@@ -2145,14 +2169,14 @@ async function qt(e) {
2145
2169
  }
2146
2170
  return { sil: i, viseme: a, expression: o };
2147
2171
  })();
2148
- const s = await Le;
2149
- return { ...Je(s), visemePaths: [...s.viseme.cellByPath.keys()] };
2172
+ const s = await Ue;
2173
+ return { ...Xe(s), visemePaths: [...s.viseme.cellByPath.keys()] };
2150
2174
  }
2151
- async function cn(e) {
2152
- if (await qt(e), !Le) throw new Error("Assets not loaded");
2153
- return Le;
2175
+ async function un(e) {
2176
+ if (await Kt(e), !Ue) throw new Error("Assets not loaded");
2177
+ return Ue;
2154
2178
  }
2155
- function Je(e) {
2179
+ function Xe(e) {
2156
2180
  return Fs(
2157
2181
  e.viseme.atlasMeta,
2158
2182
  e.sil.naturalWidth,
@@ -2163,115 +2187,115 @@ function _i(e, t) {
2163
2187
  const n = document.createElement("canvas");
2164
2188
  return n.width = t.w, n.height = t.h, n.getContext("2d", { willReadFrequently: !0 }).drawImage(e.atlas, t.x, t.y, t.w, t.h, 0, 0, t.w, t.h), n;
2165
2189
  }
2166
- async function Tt(e, t, n) {
2167
- const r = `${t.diffBase}::${n}`, s = Et.get(r);
2190
+ async function At(e, t, n) {
2191
+ const r = `${t.diffBase}::${n}`, s = Tt.get(r);
2168
2192
  if (s) return s;
2169
2193
  const i = (async () => {
2170
2194
  const a = t.cellByPath.get(n);
2171
2195
  if (!a) throw new Error(`No atlas cell for path: ${n}`);
2172
- const o = Ls(t.atlasMeta, a), f = Je(e);
2173
- if (Os(a)) {
2174
- const c = _i(t, a), m = document.createElement("canvas");
2175
- m.width = f.width, m.height = f.height;
2176
- const w = m.getContext("2d");
2177
- return w.fillStyle = "#000", w.fillRect(0, 0, f.width, f.height), w.drawImage(c, o.x, o.y), m;
2196
+ const o = Os(t.atlasMeta, a), d = Xe(e);
2197
+ if (Ls(a)) {
2198
+ const u = _i(t, a), f = document.createElement("canvas");
2199
+ f.width = d.width, f.height = d.height;
2200
+ const w = f.getContext("2d");
2201
+ return w.fillStyle = "#000", w.fillRect(0, 0, d.width, d.height), w.drawImage(u, o.x, o.y), f;
2178
2202
  }
2179
- const d = `${t.diffBase}/${n}`, u = await on(d);
2180
- if (u.naturalWidth === f.width && u.naturalHeight === f.height)
2181
- return u;
2203
+ const m = `${t.diffBase}/${n}`, c = await cn(m);
2204
+ if (c.naturalWidth === d.width && c.naturalHeight === d.height)
2205
+ return c;
2182
2206
  throw new Error(`Diff PNG wrong size for ${n}`);
2183
2207
  })();
2184
- return Et.set(r, i), i;
2208
+ return Tt.set(r, i), i;
2185
2209
  }
2186
- async function Cn(e, t, n, r, s, i) {
2210
+ async function Bn(e, t, n, r, s, i) {
2187
2211
  if (s) {
2188
2212
  const { width: w, height: b } = s;
2189
2213
  if (i && !i() || t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA)
2190
2214
  return;
2191
- const h = document.createElement("canvas");
2192
- h.width = w, h.height = b;
2193
- const I = h.getContext("2d");
2194
- if (I.clearRect(0, 0, w, b), I.drawImage(t, 0, 0, w, b), i && !i()) return;
2215
+ const p = document.createElement("canvas");
2216
+ p.width = w, p.height = b;
2217
+ const P = p.getContext("2d");
2218
+ if (P.clearRect(0, 0, w, b), P.drawImage(t, 0, 0, w, b), i && !i()) return;
2195
2219
  (e.width !== w || e.height !== b) && (e.width = w, e.height = b);
2196
2220
  const E = e.getContext("2d");
2197
- E.clearRect(0, 0, w, b), E.drawImage(h, 0, 0);
2221
+ E.clearRect(0, 0, w, b), E.drawImage(p, 0, 0);
2198
2222
  return;
2199
2223
  }
2200
- const a = await cn(), o = tn(a.viseme.atlasMeta), { width: f, height: d } = Je(a);
2201
- (e.width !== f || e.height !== d) && (e.width = f, e.height = d);
2202
- const u = [], c = e.getContext("2d");
2203
- if (c.clearRect(0, 0, f, d), t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
2224
+ const a = await un(), o = nn(a.viseme.atlasMeta), { width: d, height: m } = Xe(a);
2225
+ (e.width !== d || e.height !== m) && (e.width = d, e.height = m);
2226
+ const c = [], u = e.getContext("2d");
2227
+ if (u.clearRect(0, 0, d, m), t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
2204
2228
  const w = a.sil;
2205
- c.drawImage(w, 0, 0, f, d);
2229
+ u.drawImage(w, 0, 0, d, m);
2206
2230
  return;
2207
2231
  }
2208
- if (u.length === 0) {
2209
- c.drawImage(t, 0, 0, f, d);
2232
+ if (c.length === 0) {
2233
+ u.drawImage(t, 0, 0, d, m);
2210
2234
  return;
2211
2235
  }
2212
- const m = an(t, u, f, d, o);
2213
- c.drawImage(m, 0, 0);
2236
+ const f = on(t, c, d, m, o);
2237
+ u.drawImage(f, 0, 0);
2214
2238
  }
2215
- async function Kt(e, t, n) {
2216
- const r = await cn(Ye ?? void 0), s = tn(r.viseme.atlasMeta), { width: i, height: a } = Je(r);
2239
+ async function Gt(e, t, n) {
2240
+ const r = await un(Je ?? void 0), s = nn(r.viseme.atlasMeta), { width: i, height: a } = Xe(r);
2217
2241
  (e.width !== i || e.height !== a) && (e.width = i, e.height = a);
2218
2242
  const o = [];
2219
2243
  if (t.mouthPath) {
2220
- const u = new Set(r.viseme.cellByPath.keys()), c = sr(t.mouthPath, u) ?? (u.has(t.mouthPath) ? t.mouthPath : null);
2221
- c && o.push(await Tt(r, r.viseme, c));
2244
+ const c = new Set(r.viseme.cellByPath.keys()), u = sr(t.mouthPath, c) ?? (c.has(t.mouthPath) ? t.mouthPath : null);
2245
+ u && o.push(await At(r, r.viseme, u));
2222
2246
  }
2223
2247
  t.expressionPath && r.expression && o.push(
2224
- await Tt(r, r.expression, t.expressionPath)
2248
+ await At(r, r.expression, t.expressionPath)
2225
2249
  );
2226
- const f = e.getContext("2d");
2227
- if (f.clearRect(0, 0, i, a), o.length === 0) {
2228
- f.drawImage(r.sil, 0, 0, i, a);
2250
+ const d = e.getContext("2d");
2251
+ if (d.clearRect(0, 0, i, a), o.length === 0) {
2252
+ d.drawImage(r.sil, 0, 0, i, a);
2229
2253
  return;
2230
2254
  }
2231
- const d = an(
2255
+ const m = on(
2232
2256
  r.sil,
2233
2257
  o,
2234
2258
  i,
2235
2259
  a,
2236
2260
  s
2237
2261
  );
2238
- f.drawImage(d, 0, 0);
2262
+ d.drawImage(m, 0, 0);
2239
2263
  }
2240
2264
  async function Si(e, t) {
2241
- const n = await cn(Ye ?? void 0), r = tn(n.viseme.atlasMeta), { width: s, height: i } = Je(n), a = [];
2265
+ const n = await un(Je ?? void 0), r = nn(n.viseme.atlasMeta), { width: s, height: i } = Xe(n), a = [];
2242
2266
  if (e.mouthPath) {
2243
- const u = new Set(n.viseme.cellByPath.keys()), c = sr(e.mouthPath, u) ?? (u.has(e.mouthPath) ? e.mouthPath : null);
2244
- c && a.push(await Tt(n, n.viseme, c));
2267
+ const c = new Set(n.viseme.cellByPath.keys()), u = sr(e.mouthPath, c) ?? (c.has(e.mouthPath) ? e.mouthPath : null);
2268
+ u && a.push(await At(n, n.viseme, u));
2245
2269
  }
2246
2270
  e.expressionPath && n.expression && a.push(
2247
- await Tt(n, n.expression, e.expressionPath)
2271
+ await At(n, n.expression, e.expressionPath)
2248
2272
  );
2249
- const o = new OffscreenCanvas(s, i), f = o.getContext("2d");
2273
+ const o = new OffscreenCanvas(s, i), d = o.getContext("2d");
2250
2274
  if (a.length === 0)
2251
- return f.drawImage(n.sil, 0, 0, s, i), o.transferToImageBitmap();
2252
- const d = an(
2275
+ return d.drawImage(n.sil, 0, 0, s, i), o.transferToImageBitmap();
2276
+ const m = on(
2253
2277
  n.sil,
2254
2278
  a,
2255
2279
  s,
2256
2280
  i,
2257
2281
  r
2258
2282
  );
2259
- return f.drawImage(d, 0, 0), o.transferToImageBitmap();
2283
+ return d.drawImage(m, 0, 0), o.transferToImageBitmap();
2260
2284
  }
2261
2285
  async function ki(e) {
2262
- await Kt(e, { mouthPath: null, expressionPath: null });
2286
+ await Gt(e, { mouthPath: null, expressionPath: null });
2263
2287
  }
2264
2288
  let ur = null;
2265
2289
  function Fn(e) {
2266
2290
  ur = e;
2267
2291
  }
2268
- function Ne() {
2292
+ function We() {
2269
2293
  return ur;
2270
2294
  }
2271
- function bi(e) {
2295
+ function vi(e) {
2272
2296
  return `${e.from}→${e.to}@${e.fromVtime}`;
2273
2297
  }
2274
- function Ln() {
2298
+ function On() {
2275
2299
  return {
2276
2300
  rafTicksSpeaking: 0,
2277
2301
  rafTicksDeduped: 0,
@@ -2289,16 +2313,16 @@ function Ln() {
2289
2313
  bufferPrefetched: 0
2290
2314
  };
2291
2315
  }
2292
- function vi() {
2293
- let e = Ln();
2316
+ function bi() {
2317
+ let e = On();
2294
2318
  const t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Set();
2295
2319
  let s = "", i = -1, a = "";
2296
2320
  const o = () => {
2297
- e = Ln(), t.clear(), n.clear(), r.clear(), s = "", i = -1, a = "";
2298
- }, f = (u) => {
2299
- t.add(u);
2300
- }, d = (u) => {
2301
- u && r.add(u);
2321
+ e = On(), t.clear(), n.clear(), r.clear(), s = "", i = -1, a = "";
2322
+ }, d = (c) => {
2323
+ t.add(c);
2324
+ }, m = (c) => {
2325
+ c && r.add(c);
2302
2326
  };
2303
2327
  return {
2304
2328
  reset: o,
@@ -2317,9 +2341,9 @@ function vi() {
2317
2341
  onWorkerStale() {
2318
2342
  e.workerStale += 1;
2319
2343
  },
2320
- onTransitionSample(u, c) {
2321
- const m = bi(u);
2322
- e.transitionFramesSampled += 1, m !== s && (s = m, i = -1, a !== m && (a = m, e.rifeFramesOmittedByGap += u.omittedByGap)), c && n.add(c), i >= 0 && u.frameIdx > i + 1 && (e.transitionFramesMissed += u.frameIdx - i - 1), i = u.frameIdx;
2344
+ onTransitionSample(c, u) {
2345
+ const f = vi(c);
2346
+ e.transitionFramesSampled += 1, f !== s && (s = f, i = -1, a !== f && (a = f, e.rifeFramesOmittedByGap += c.omittedByGap)), u && n.add(u), i >= 0 && c.frameIdx > i + 1 && (e.transitionFramesMissed += c.frameIdx - i - 1), i = c.frameIdx;
2323
2347
  },
2324
2348
  onBufferTick() {
2325
2349
  e.bufferTicks += 1;
@@ -2335,60 +2359,60 @@ function vi() {
2335
2359
  },
2336
2360
  onBufferReset() {
2337
2361
  },
2338
- onDrawDisplayed(u, c) {
2339
- if (e.drawsDisplayed += 1, u.transitionMeta) {
2362
+ onDrawDisplayed(c, u) {
2363
+ if (e.drawsDisplayed += 1, c.transitionMeta) {
2340
2364
  e.transitionFramesDisplayed += 1;
2341
- const w = u.transitionMeta;
2342
- f(w.fromVtime), f(w.toVtime), d(u.mouthPath);
2365
+ const w = c.transitionMeta;
2366
+ d(w.fromVtime), d(w.toVtime), m(c.mouthPath);
2343
2367
  return;
2344
2368
  }
2345
- if (u.label.includes("→")) {
2369
+ if (c.label.includes("→")) {
2346
2370
  e.transitionFramesDisplayed += 1;
2347
2371
  return;
2348
2372
  }
2349
- const m = u.label.trim();
2350
- if (!(!m || m === "sil")) {
2351
- for (const w of c)
2352
- if (w.viseme === m && u.elapsedMs >= w.vtime && u.elapsedMs < w.vtime + w.vduration) {
2353
- f(w.vtime);
2373
+ const f = c.label.trim();
2374
+ if (!(!f || f === "sil")) {
2375
+ for (const w of u)
2376
+ if (w.viseme === f && c.elapsedMs >= w.vtime && c.elapsedMs < w.vtime + w.vduration) {
2377
+ d(w.vtime);
2354
2378
  break;
2355
2379
  }
2356
2380
  }
2357
2381
  },
2358
- snapshot(u, c) {
2359
- let m = 0, w = 0;
2360
- for (const _ of u)
2382
+ snapshot(c, u) {
2383
+ let f = 0, w = 0;
2384
+ for (const _ of c)
2361
2385
  if (_.viseme !== "sil") {
2362
- if (_.vtime >= c) break;
2363
- m += 1, t.has(_.vtime) && (w += 1);
2386
+ if (_.vtime >= u) break;
2387
+ f += 1, t.has(_.vtime) && (w += 1);
2364
2388
  }
2365
- const b = m > 0 ? Math.round(w / m * 100) : 100, h = e.queueSuperseded + e.workerStale + Math.max(0, e.drawsRequested - e.drawsDisplayed), I = m - w, E = e.bufferTicks > 0 ? Math.round(e.bufferHits / e.bufferTicks * 100) : 100;
2389
+ const b = f > 0 ? Math.round(w / f * 100) : 100, p = e.queueSuperseded + e.workerStale + Math.max(0, e.drawsRequested - e.drawsDisplayed), P = f - w, E = e.bufferTicks > 0 ? Math.round(e.bufferHits / e.bufferTicks * 100) : 100;
2366
2390
  return {
2367
2391
  ...e,
2368
2392
  bufferHitRate: E,
2369
- renderLoss: h,
2370
- visemeSlotsTotal: m,
2393
+ renderLoss: p,
2394
+ visemeSlotsTotal: f,
2371
2395
  visemeSlotsCovered: w,
2372
2396
  visemeCoveragePct: b,
2373
2397
  transitionFramesUniqueSampled: n.size,
2374
2398
  transitionFramesUniquePainted: r.size,
2375
2399
  drawsDeduped: e.rafTicksDeduped,
2376
- renderSkips: e.rafTicksDeduped + h,
2400
+ renderSkips: e.rafTicksDeduped + p,
2377
2401
  visemesShown: w,
2378
- visemesSkipped: I,
2402
+ visemesSkipped: P,
2379
2403
  rifeTransitionFramesSkipped: e.transitionFramesMissed
2380
2404
  };
2381
2405
  }
2382
2406
  };
2383
2407
  }
2384
- const lr = zt, Ei = 200, Ti = 100, Ai = 600, fr = "__sil__";
2408
+ const lr = Yt, Ei = 200, Ti = 100, Ai = 600, fr = "__sil__";
2385
2409
  function Ii(e) {
2386
2410
  return e.lastDrawnKey ? e.lastDrawnKey.split("|")[0] === fr : !1;
2387
2411
  }
2388
- function un() {
2412
+ function ln() {
2389
2413
  return { lastDrawnKey: "" };
2390
2414
  }
2391
- function ke(e) {
2415
+ function ve(e) {
2392
2416
  e.lastDrawnKey = "", e.transitionCache = void 0;
2393
2417
  }
2394
2418
  function Mi(e, t) {
@@ -2410,100 +2434,100 @@ function Ri(e, t, n, r = 120) {
2410
2434
  return s !== "sil" ? s : Pi(e, t);
2411
2435
  }
2412
2436
  function Di(e, t) {
2413
- return e === "sil" ? !1 : t ? qs(e, "sil", t) : nn(e, "sil").length > 0;
2437
+ return e === "sil" ? !1 : t ? qs(e, "sil", t) : rn(e, "sil").length > 0;
2414
2438
  }
2415
2439
  function xi() {
2416
2440
  return lr + Ai;
2417
2441
  }
2418
- function At(e) {
2442
+ function It(e) {
2419
2443
  return e ?? fr;
2420
2444
  }
2421
- function ln(e, t, n, r, s = Ti) {
2445
+ function fn(e, t, n, r, s = Ti) {
2422
2446
  if (e.length === 0)
2423
2447
  return { diffPath: null, label: "sil" };
2424
- if (t >= Fe(e))
2448
+ if (t >= Ne(e))
2425
2449
  return { diffPath: null, label: "sil" };
2426
2450
  const i = Mi(e, r), a = ys(i, t);
2427
2451
  if (a) {
2428
- const f = Yn(a.fromVtime, a.toVtime);
2429
- if (t >= f.transStart && t < f.transEnd) {
2430
- let d = nn(a.from, a.to);
2431
- n && (d = ir(d, n));
2432
- const u = Gs(d, f.effectiveGapMs);
2433
- if (u.length > 0) {
2434
- const c = js(
2452
+ const d = Yn(a.fromVtime, a.toVtime);
2453
+ if (t >= d.transStart && t < d.transEnd) {
2454
+ let m = rn(a.from, a.to);
2455
+ n && (m = ir(m, n));
2456
+ const c = Gs(m, d.effectiveGapMs);
2457
+ if (c.length > 0) {
2458
+ const u = js(
2435
2459
  t,
2436
- f.transStart,
2437
- f.transEnd,
2438
- u.length
2460
+ d.transStart,
2461
+ d.transEnd,
2462
+ c.length
2439
2463
  );
2440
2464
  return {
2441
- diffPath: u[c] ?? u[u.length - 1],
2465
+ diffPath: c[u] ?? c[c.length - 1],
2442
2466
  label: `${a.from}→${a.to}`,
2443
2467
  transitionMeta: {
2444
2468
  from: a.from,
2445
2469
  to: a.to,
2446
2470
  fromVtime: a.fromVtime,
2447
2471
  toVtime: a.toVtime,
2448
- frameIdx: c,
2449
- frameCount: u.length,
2450
- fullFrameCount: d.length,
2451
- omittedByGap: Math.max(0, d.length - u.length)
2472
+ frameIdx: u,
2473
+ frameCount: c.length,
2474
+ fullFrameCount: m.length,
2475
+ omittedByGap: Math.max(0, m.length - c.length)
2452
2476
  }
2453
2477
  };
2454
2478
  }
2455
- return { diffPath: vt(a.to), label: a.to };
2479
+ return { diffPath: Et(a.to), label: a.to };
2456
2480
  }
2457
2481
  }
2458
2482
  const o = Qn(e, t, s);
2459
- return o === "sil" ? { diffPath: null, label: "sil" } : { diffPath: vt(o), label: o };
2483
+ return o === "sil" ? { diffPath: null, label: "sil" } : { diffPath: Et(o), label: o };
2460
2484
  }
2461
- function fn(e, t) {
2462
- const n = t ?? At(e.expressionPath);
2463
- return `${At(e.mouthPath)}|${n}`;
2485
+ function dn(e, t) {
2486
+ const n = t ?? It(e.expressionPath);
2487
+ return `${It(e.mouthPath)}|${n}`;
2464
2488
  }
2465
- function Bi(e, t, n, r, s, i, a, o, f, d) {
2466
- const u = ln(t, n, f, i), c = {
2467
- mouthPath: u.diffPath,
2489
+ function Ci(e, t, n, r, s, i, a, o, d, m) {
2490
+ const c = fn(t, n, d, i), u = {
2491
+ mouthPath: c.diffPath,
2468
2492
  expressionPath: r
2469
- }, m = fn(c, s), w = Ne();
2470
- if (u.transitionMeta && w?.onTransitionSample(u.transitionMeta, u.diffPath), m === i.lastDrawnKey) {
2493
+ }, f = dn(u, s), w = We();
2494
+ if (c.transitionMeta && w?.onTransitionSample(c.transitionMeta, c.diffPath), f === i.lastDrawnKey) {
2471
2495
  w?.onDrawDeduped();
2472
2496
  return;
2473
2497
  }
2474
- w?.onDrawRequested(), o(u), i.lastDrawnKey = m, a(e, c, m, (b) => {
2475
- b === m && (i.lastDrawnKey = m);
2498
+ w?.onDrawRequested(), o(c), i.lastDrawnKey = f, a(e, u, f, (b) => {
2499
+ b === f && (i.lastDrawnKey = f);
2476
2500
  });
2477
2501
  }
2478
- function On(e, t, n, r, s, i, a = null) {
2479
- const o = { mouthPath: null, expressionPath: t }, f = a != null ? `|idle@${Math.round(a * 1e3)}` : "", d = fn(o, n) + f;
2480
- !(a != null) && d === r.lastDrawnKey || (i(
2502
+ function Ln(e, t, n, r, s, i, a = null) {
2503
+ const o = { mouthPath: null, expressionPath: t }, d = a != null ? `|idle@${Math.round(a * 1e3)}` : "", m = dn(o, n) + d;
2504
+ !(a != null) && m === r.lastDrawnKey || (i(
2481
2505
  a != null ? "idle" : "sil"
2482
- ), s(e, o, d, (c) => {
2483
- c === d && (r.lastDrawnKey = d);
2506
+ ), s(e, o, m, (u) => {
2507
+ u === m && (r.lastDrawnKey = m);
2484
2508
  }));
2485
2509
  }
2486
- function dr(e, t, n, r, s = ut, i) {
2510
+ function dr(e, t, n, r, s = lt, i) {
2487
2511
  if (n <= t || e.length === 0) return [];
2488
- const a = i ?? un(), o = [];
2489
- let f = "";
2490
- for (let d = t; d < n; d += s) {
2491
- const u = ln(
2512
+ const a = i ?? ln(), o = [];
2513
+ let d = "";
2514
+ for (let m = t; m < n; m += s) {
2515
+ const c = fn(
2492
2516
  e,
2493
- d,
2517
+ m,
2494
2518
  r,
2495
2519
  a
2496
- ), c = At(u.diffPath);
2497
- c !== f && (f = c, o.push({
2498
- displayAtMs: d,
2499
- mouthPath: u.diffPath,
2500
- mouthKey: c,
2501
- label: u.label
2520
+ ), u = It(c.diffPath);
2521
+ u !== d && (d = u, o.push({
2522
+ displayAtMs: m,
2523
+ mouthPath: c.diffPath,
2524
+ mouthKey: u,
2525
+ label: c.label
2502
2526
  }));
2503
2527
  }
2504
2528
  return o;
2505
2529
  }
2506
- const Ci = 3;
2530
+ const Bi = 3;
2507
2531
  function mr(e) {
2508
2532
  let t = 0;
2509
2533
  const n = [];
@@ -2513,8 +2537,8 @@ function mr(e) {
2513
2537
  r = !0;
2514
2538
  try {
2515
2539
  for (; n.length > 0; ) {
2516
- const o = n.shift(), f = ++t;
2517
- await e(o.diffs, f, o) && f === t && o.onDisplayed?.(o.drawKey);
2540
+ const o = n.shift(), d = ++t;
2541
+ await e(o.diffs, d, o) && d === t && o.onDisplayed?.(o.drawKey);
2518
2542
  }
2519
2543
  } finally {
2520
2544
  r = !1, n.length > 0 && i();
@@ -2526,9 +2550,9 @@ function mr(e) {
2526
2550
  }));
2527
2551
  };
2528
2552
  return {
2529
- enqueue(o, f, d) {
2530
- const u = n.findIndex((c) => c.drawKey === f);
2531
- u >= 0 ? n[u] = { diffs: o, drawKey: f, onDisplayed: d } : (n.length >= Ci && (n.shift(), Ne()?.onQueueSuperseded()), n.push({ diffs: o, drawKey: f, onDisplayed: d })), a();
2553
+ enqueue(o, d, m) {
2554
+ const c = n.findIndex((u) => u.drawKey === d);
2555
+ c >= 0 ? n[c] = { diffs: o, drawKey: d, onDisplayed: m } : (n.length >= Bi && (n.shift(), We()?.onQueueSuperseded()), n.push({ diffs: o, drawKey: d, onDisplayed: m })), a();
2532
2556
  },
2533
2557
  reset() {
2534
2558
  t += 1, n.length = 0;
@@ -2544,35 +2568,35 @@ function mr(e) {
2544
2568
  function Fi(e, t = 2) {
2545
2569
  const n = [], r = /* @__PURE__ */ new Set();
2546
2570
  let s = 0, i = !1, a = 0;
2547
- const o = (d, u) => `${d}|${u}`, f = async () => {
2571
+ const o = (m, c) => `${m}|${c}`, d = async () => {
2548
2572
  if (!i) {
2549
2573
  i = !0;
2550
2574
  try {
2551
2575
  for (; s < t && n.length > 0; ) {
2552
- const d = n.shift(), u = o(d.displayAtMs, d.drawKey);
2553
- if (r.has(u)) continue;
2554
- r.add(u), s += 1;
2555
- const c = a;
2576
+ const m = n.shift(), c = o(m.displayAtMs, m.drawKey);
2577
+ if (r.has(c)) continue;
2578
+ r.add(c), s += 1;
2579
+ const u = a;
2556
2580
  try {
2557
- await e(d);
2581
+ await e(m);
2558
2582
  } finally {
2559
- if (r.delete(u), s -= 1, c !== a) return;
2583
+ if (r.delete(c), s -= 1, u !== a) return;
2560
2584
  }
2561
2585
  }
2562
2586
  } finally {
2563
- i = !1, n.length > 0 && s < t && f();
2587
+ i = !1, n.length > 0 && s < t && d();
2564
2588
  }
2565
2589
  }
2566
2590
  };
2567
2591
  return {
2568
- enqueue(d) {
2569
- const u = o(d.displayAtMs, d.drawKey);
2570
- r.has(u) || n.some((c) => o(c.displayAtMs, c.drawKey) === u) || (n.push(d), f());
2571
- },
2572
- isQueuedOrInflight(d, u) {
2573
- const c = o(d, u);
2574
- return r.has(c) ? !0 : n.some(
2575
- (m) => o(m.displayAtMs, m.drawKey) === c
2592
+ enqueue(m) {
2593
+ const c = o(m.displayAtMs, m.drawKey);
2594
+ r.has(c) || n.some((u) => o(u.displayAtMs, u.drawKey) === c) || (n.push(m), d());
2595
+ },
2596
+ isQueuedOrInflight(m, c) {
2597
+ const u = o(m, c);
2598
+ return r.has(u) ? !0 : n.some(
2599
+ (f) => o(f.displayAtMs, f.drawKey) === u
2576
2600
  );
2577
2601
  },
2578
2602
  reset() {
@@ -2583,7 +2607,7 @@ function Fi(e, t = 2) {
2583
2607
  }
2584
2608
  };
2585
2609
  }
2586
- function Li() {
2610
+ function Oi() {
2587
2611
  const e = /* @__PURE__ */ new Map(), t = (n) => {
2588
2612
  n.bitmap.close();
2589
2613
  };
@@ -2618,129 +2642,129 @@ function Li() {
2618
2642
  }
2619
2643
  };
2620
2644
  }
2621
- function Oi(e, t, n, r, s, i) {
2645
+ function Li(e, t, n, r, s, i) {
2622
2646
  const a = dr(
2623
2647
  t,
2624
2648
  n,
2625
2649
  r,
2626
2650
  s,
2627
- ut,
2651
+ lt,
2628
2652
  i
2629
2653
  );
2630
2654
  if (a.length === 0) return { ready: 0, total: 0 };
2631
2655
  let o = 0;
2632
- for (const f of a)
2633
- e.has(f.displayAtMs, f.mouthKey) && (o += 1);
2656
+ for (const d of a)
2657
+ e.has(d.displayAtMs, d.mouthKey) && (o += 1);
2634
2658
  return { ready: o, total: a.length };
2635
2659
  }
2636
2660
  function Ni(e) {
2637
- const t = Li(), n = un(), r = /* @__PURE__ */ new Set();
2661
+ const t = Oi(), n = ln(), r = /* @__PURE__ */ new Set();
2638
2662
  let s = "", i = null;
2639
2663
  const a = Fi(
2640
- async (c) => {
2664
+ async (u) => {
2641
2665
  try {
2642
2666
  if (!e.supportsBufferedPlayback) return;
2643
- const m = await e.compositePrefetchFrame(
2644
- c.diffs,
2645
- c.mouthKey
2667
+ const f = await e.compositePrefetchFrame(
2668
+ u.diffs,
2669
+ u.mouthKey
2646
2670
  );
2647
- if (!m) return;
2671
+ if (!f) return;
2648
2672
  t.markRendered(
2649
- c.displayAtMs,
2650
- c.mouthKey,
2651
- c.label,
2652
- c.mouthPath,
2653
- m
2654
- ), Ne()?.onBufferPrefetched();
2673
+ u.displayAtMs,
2674
+ u.mouthKey,
2675
+ u.label,
2676
+ u.mouthPath,
2677
+ f
2678
+ ), We()?.onBufferPrefetched();
2655
2679
  } finally {
2656
- r.delete(c.mouthKey);
2680
+ r.delete(u.mouthKey);
2657
2681
  }
2658
2682
  },
2659
- Cr
2660
- ), o = (c) => {
2661
- for (const m of c)
2662
- t.has(m.displayAtMs, m.mouthKey) || r.has(m.mouthKey) || a.isQueuedOrInflight(0, m.mouthKey) || (r.add(m.mouthKey), a.enqueue({
2663
- ...m,
2683
+ Br
2684
+ ), o = (u) => {
2685
+ for (const f of u)
2686
+ t.has(f.displayAtMs, f.mouthKey) || r.has(f.mouthKey) || a.isQueuedOrInflight(0, f.mouthKey) || (r.add(f.mouthKey), a.enqueue({
2687
+ ...f,
2664
2688
  displayAtMs: 0,
2665
- drawKey: m.mouthKey,
2689
+ drawKey: f.mouthKey,
2666
2690
  diffs: {
2667
- mouthPath: m.mouthPath,
2691
+ mouthPath: f.mouthPath,
2668
2692
  expressionPath: null
2669
2693
  }
2670
2694
  }));
2671
- }, f = (c, m, w, b = Ct) => {
2695
+ }, d = (u, f, w, b = Ft) => {
2672
2696
  if (!e.supportsBufferedPlayback) return;
2673
- const h = Math.max(0, m - ut * 4), I = dr(
2674
- c,
2675
- h,
2676
- m + b,
2697
+ const p = Math.max(0, f - lt * 4), P = dr(
2698
+ u,
2699
+ p,
2700
+ f + b,
2677
2701
  w,
2678
- ut,
2702
+ lt,
2679
2703
  n
2680
2704
  );
2681
- o(I);
2682
- }, d = (c, m) => {
2683
- s = c, i = m;
2684
- }, u = (c, m) => !(s !== c || i !== m);
2705
+ o(P);
2706
+ }, m = (u, f) => {
2707
+ s = u, i = f;
2708
+ }, c = (u, f) => !(s !== u || i !== f);
2685
2709
  return {
2686
2710
  reset() {
2687
- t.reset(), a.reset(), r.clear(), ke(n), s = "", i = null, Ne()?.onBufferReset();
2711
+ t.reset(), a.reset(), r.clear(), ve(n), s = "", i = null, We()?.onBufferReset();
2688
2712
  },
2689
- markExternallyDisplayed(c, m) {
2690
- d(c.split("|")[0] ?? c, m);
2713
+ markExternallyDisplayed(u, f) {
2714
+ m(u.split("|")[0] ?? u, f);
2691
2715
  },
2692
- warmupTick(c) {
2693
- e.supportsBufferedPlayback && f(
2694
- c.queue,
2716
+ warmupTick(u) {
2717
+ e.supportsBufferedPlayback && d(
2718
+ u.queue,
2695
2719
  0,
2696
- c.availablePaths,
2697
- Math.max(Sn, Ct)
2720
+ u.availablePaths,
2721
+ Math.max(Sn, Ft)
2698
2722
  );
2699
2723
  },
2700
- isWarmupReady(c, m = Sn) {
2724
+ isWarmupReady(u, f = Sn) {
2701
2725
  if (!e.supportsBufferedPlayback) return !0;
2702
- const { ready: w, total: b } = Oi(
2726
+ const { ready: w, total: b } = Li(
2703
2727
  t,
2704
- c.queue,
2728
+ u.queue,
2705
2729
  0,
2706
- m,
2707
- c.availablePaths,
2730
+ f,
2731
+ u.availablePaths,
2708
2732
  n
2709
2733
  );
2710
- return b === 0 ? !1 : w / b >= Br;
2734
+ return b === 0 ? !1 : w / b >= Cr;
2711
2735
  },
2712
2736
  getBufferedFrameCount: () => t.size(),
2713
- tick(c) {
2714
- const m = Ne();
2715
- m?.onBufferTick(), f(
2716
- c.queue,
2717
- c.elapsedMs,
2718
- c.availablePaths
2719
- ), t.pruneBefore(c.elapsedMs - Ct);
2720
- const w = ln(
2721
- c.queue,
2722
- c.elapsedMs,
2723
- c.availablePaths,
2737
+ tick(u) {
2738
+ const f = We();
2739
+ f?.onBufferTick(), d(
2740
+ u.queue,
2741
+ u.elapsedMs,
2742
+ u.availablePaths
2743
+ ), t.pruneBefore(u.elapsedMs - Ft);
2744
+ const w = fn(
2745
+ u.queue,
2746
+ u.elapsedMs,
2747
+ u.availablePaths,
2724
2748
  n
2725
- ), b = At(w.diffPath), h = fn(
2726
- { mouthPath: w.diffPath, expressionPath: c.expressionPath },
2727
- c.expressionDrawKey
2728
- ), I = () => (d(b, c.expressionPath), m?.onBufferHit(), { usedBuffer: !0, underrun: !1, held: !0 });
2729
- if (u(b, c.expressionPath) || c.playbackState.lastDrawnKey === h)
2730
- return I();
2749
+ ), b = It(w.diffPath), p = dn(
2750
+ { mouthPath: w.diffPath, expressionPath: u.expressionPath },
2751
+ u.expressionDrawKey
2752
+ ), P = () => (m(b, u.expressionPath), f?.onBufferHit(), { usedBuffer: !0, underrun: !1, held: !0 });
2753
+ if (c(b, u.expressionPath) || u.playbackState.lastDrawnKey === p)
2754
+ return P();
2731
2755
  const E = t.findForMouthKey(
2732
2756
  b,
2733
- c.elapsedMs,
2734
- ut
2757
+ u.elapsedMs,
2758
+ lt
2735
2759
  );
2736
- return E && c.expressionPath != null ? (m?.onBufferHit(), { usedBuffer: !1, underrun: !1, held: !1 }) : E ? (e.blitBitmap(c.canvas, E.bitmap), d(E.mouthKey, c.expressionPath), c.playbackState.lastDrawnKey = h, c.onStatus({
2760
+ return E && u.expressionPath != null ? (f?.onBufferHit(), { usedBuffer: !1, underrun: !1, held: !1 }) : E ? (e.blitBitmap(u.canvas, E.bitmap), m(E.mouthKey, u.expressionPath), u.playbackState.lastDrawnKey = p, u.onStatus({
2737
2761
  diffPath: E.mouthPath,
2738
2762
  label: E.label
2739
- }), c.onDisplayed(h, E.mouthPath, E.label), m?.onBufferHit(), { usedBuffer: !0, underrun: !1, held: !1 }) : (m?.onBufferUnderrun(), { usedBuffer: !1, underrun: !0, held: !1 });
2763
+ }), u.onDisplayed(p, E.mouthPath, E.label), f?.onBufferHit(), { usedBuffer: !0, underrun: !1, held: !1 }) : (f?.onBufferUnderrun(), { usedBuffer: !1, underrun: !0, held: !1 });
2740
2764
  }
2741
2765
  };
2742
2766
  }
2743
- const Nn = -1, kt = 1e5, Un = /* @__PURE__ */ new Map();
2767
+ const Nn = -1, vt = 1e5, Un = /* @__PURE__ */ new Map();
2744
2768
  async function Ui(e) {
2745
2769
  let t = Un.get(e);
2746
2770
  if (!t) {
@@ -2757,18 +2781,18 @@ async function Ui(e) {
2757
2781
  function Vi() {
2758
2782
  return typeof Worker < "u" && typeof OffscreenCanvas < "u";
2759
2783
  }
2760
- function Gt(e, t) {
2784
+ function jt(e, t) {
2761
2785
  (e.width !== t.width || e.height !== t.height) && (e.width = t.width, e.height = t.height);
2762
2786
  const n = e.getContext("2d");
2763
2787
  n && n.drawImage(t, 0, 0);
2764
2788
  }
2765
2789
  function Vn(e, t) {
2766
2790
  let n = 0, r = null;
2767
- const s = mr(async (i, a) => (await qt(), await Kt(e, i), s.isLatestSeq(a)));
2791
+ const s = mr(async (i, a) => (await Kt(), await Gt(e, i), s.isLatestSeq(a)));
2768
2792
  return {
2769
2793
  usesWorker: !1,
2770
2794
  async loadAssets(i) {
2771
- const a = await qt(i);
2795
+ const a = await Kt(i);
2772
2796
  return r = a.visemePaths ? new Set(a.visemePaths) : null, a;
2773
2797
  },
2774
2798
  getVisemeAtlasPaths() {
@@ -2777,8 +2801,8 @@ function Vn(e, t) {
2777
2801
  async renderSilOnly() {
2778
2802
  n += 1, s.reset(), await ki(e);
2779
2803
  },
2780
- drawLiveFrame(i, a, o, f) {
2781
- s.enqueue(a, o, f);
2804
+ drawLiveFrame(i, a, o, d) {
2805
+ s.enqueue(a, o, d);
2782
2806
  },
2783
2807
  async compositePrefetchFrame(i, a) {
2784
2808
  try {
@@ -2788,14 +2812,14 @@ function Vn(e, t) {
2788
2812
  }
2789
2813
  },
2790
2814
  blitBitmap(i, a) {
2791
- Gt(i, a);
2815
+ jt(i, a);
2792
2816
  },
2793
2817
  supportsBufferedPlayback: !0,
2794
2818
  async renderViseme(i, a) {
2795
2819
  n += 1;
2796
2820
  const o = n;
2797
- t.onStatus?.(a), await Kt(i, {
2798
- mouthPath: vt(a),
2821
+ t.onStatus?.(a), await Gt(i, {
2822
+ mouthPath: Et(a),
2799
2823
  expressionPath: null
2800
2824
  });
2801
2825
  },
@@ -2805,106 +2829,106 @@ function Vn(e, t) {
2805
2829
  };
2806
2830
  }
2807
2831
  function Wi(e, t, n) {
2808
- let r = null, s = 1, i = 0, a = !1, o = !1, f = null;
2809
- const d = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map();
2810
- let c = kt;
2811
- const m = mr(async (_, p, v) => (d.set(p, {
2812
- drawKey: v.drawKey,
2813
- onDisplayed: v.onDisplayed
2814
- }), await I(
2832
+ let r = null, s = 1, i = 0, a = !1, o = !1, d = null;
2833
+ const m = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map();
2834
+ let u = vt;
2835
+ const f = mr(async (_, h, A) => (m.set(h, {
2836
+ drawKey: A.drawKey,
2837
+ onDisplayed: A.onDisplayed
2838
+ }), await P(
2815
2839
  {
2816
2840
  type: "drawFrame",
2817
2841
  diffPath: _.mouthPath,
2818
2842
  expressionDiffPath: _.expressionPath,
2819
- liveSeq: p
2843
+ liveSeq: h
2820
2844
  },
2821
2845
  void 0,
2822
2846
  !0
2823
2847
  ), !1)), w = (_) => {
2824
2848
  if (_ === Nn) {
2825
- let N = -1, F;
2826
- for (const [j, R] of d)
2827
- j >= N && (N = j, F = R);
2828
- N >= 0 && d.delete(N), F?.onDisplayed?.(F.drawKey);
2849
+ let O = -1, B;
2850
+ for (const [G, C] of m)
2851
+ G >= O && (O = G, B = C);
2852
+ O >= 0 && m.delete(O), B?.onDisplayed?.(B.drawKey);
2829
2853
  return;
2830
2854
  }
2831
- const p = -_, v = d.get(p);
2832
- d.delete(p), v?.onDisplayed?.(v.drawKey);
2833
- }, b = /* @__PURE__ */ new Map(), h = (_, p) => {
2855
+ const h = -_, A = m.get(h);
2856
+ m.delete(h), A?.onDisplayed?.(A.drawKey);
2857
+ }, b = /* @__PURE__ */ new Map(), p = (_, h) => {
2834
2858
  o || !r || r.postMessage(_, []);
2835
- }, I = (_, p, v = !1) => {
2859
+ }, P = (_, h, A = !1) => {
2836
2860
  if (o) return Promise.resolve();
2837
- const N = s++;
2838
- return new Promise((F, j) => {
2839
- b.set(N, {
2840
- resolve: (R) => F(R),
2841
- reject: j,
2861
+ const O = s++;
2862
+ return new Promise((B, G) => {
2863
+ b.set(O, {
2864
+ resolve: (C) => B(C),
2865
+ reject: G,
2842
2866
  generation: typeof _.generation == "number" ? _.generation : void 0,
2843
- expectRenderDone: v
2844
- }), h({ ..._, requestId: N });
2867
+ expectRenderDone: A
2868
+ }), p({ ..._, requestId: O });
2845
2869
  });
2846
2870
  }, E = (async () => {
2847
2871
  r = await Ui(n), r.onmessage = (_) => {
2848
2872
  if (o) return;
2849
- const p = _.data;
2850
- if (p.type === "status") {
2851
- t.onStatus?.(p.label);
2873
+ const h = _.data;
2874
+ if (h.type === "status") {
2875
+ t.onStatus?.(h.label);
2852
2876
  return;
2853
2877
  }
2854
- if (p.type === "frame") {
2855
- if (p.generation <= -kt) {
2856
- const F = -p.generation, j = u.get(F);
2857
- u.delete(F), j?.resolve(p.bitmap);
2878
+ if (h.type === "frame") {
2879
+ if (h.generation <= -vt) {
2880
+ const B = -h.generation, G = c.get(B);
2881
+ c.delete(B), G?.resolve(h.bitmap);
2858
2882
  return;
2859
2883
  }
2860
- const N = p.generation < 0;
2861
- if (!N && p.generation !== i) {
2862
- p.bitmap.close();
2884
+ const O = h.generation < 0;
2885
+ if (!O && h.generation !== i) {
2886
+ h.bitmap.close();
2863
2887
  return;
2864
2888
  }
2865
- if (N && !(p.generation === Nn)) {
2866
- const j = -p.generation;
2867
- if (!m.isLatestSeq(j)) {
2868
- Ne()?.onWorkerStale(), p.bitmap.close();
2889
+ if (O && !(h.generation === Nn)) {
2890
+ const G = -h.generation;
2891
+ if (!f.isLatestSeq(G)) {
2892
+ We()?.onWorkerStale(), h.bitmap.close();
2869
2893
  return;
2870
2894
  }
2871
2895
  }
2872
- if (N && t.shouldAcceptLiveFrame && !t.shouldAcceptLiveFrame()) {
2873
- Ne()?.onWorkerStale(), p.bitmap.close();
2896
+ if (O && t.shouldAcceptLiveFrame && !t.shouldAcceptLiveFrame()) {
2897
+ We()?.onWorkerStale(), h.bitmap.close();
2874
2898
  return;
2875
2899
  }
2876
- Gt(e, p.bitmap), p.bitmap.close(), N && w(p.generation);
2900
+ jt(e, h.bitmap), h.bitmap.close(), O && w(h.generation);
2877
2901
  return;
2878
2902
  }
2879
- const v = b.get(p.requestId);
2880
- if (v)
2881
- switch (p.type) {
2903
+ const A = b.get(h.requestId);
2904
+ if (A)
2905
+ switch (h.type) {
2882
2906
  case "ready":
2883
- b.delete(p.requestId), v.resolve();
2907
+ b.delete(h.requestId), A.resolve();
2884
2908
  break;
2885
2909
  case "loadAssetsDone":
2886
- b.delete(p.requestId), f = p.visemePaths?.length ? new Set(p.visemePaths) : null, v.resolve({ width: p.width, height: p.height, visemePaths: p.visemePaths });
2910
+ b.delete(h.requestId), d = h.visemePaths?.length ? new Set(h.visemePaths) : null, A.resolve({ width: h.width, height: h.height, visemePaths: h.visemePaths });
2887
2911
  break;
2888
2912
  case "renderDone":
2889
- if (v.expectRenderDone && v.generation != null && p.generation !== v.generation)
2913
+ if (A.expectRenderDone && A.generation != null && h.generation !== A.generation)
2890
2914
  return;
2891
- b.delete(p.requestId), v.resolve();
2915
+ b.delete(h.requestId), A.resolve();
2892
2916
  break;
2893
2917
  case "renderAborted":
2894
- b.delete(p.requestId), v.resolve();
2918
+ b.delete(h.requestId), A.resolve();
2895
2919
  break;
2896
2920
  case "error":
2897
- b.delete(p.requestId), t.onError?.(p.message), v.reject(new Error(p.message));
2921
+ b.delete(h.requestId), t.onError?.(h.message), A.reject(new Error(h.message));
2898
2922
  break;
2899
2923
  }
2900
2924
  }, r.onerror = (_) => {
2901
2925
  if (o) return;
2902
- const p = _.message || "Worker error";
2903
- t.onError?.(p);
2904
- for (const [, v] of b)
2905
- v.reject(new Error(p));
2926
+ const h = _.message || "Worker error";
2927
+ t.onError?.(h);
2928
+ for (const [, A] of b)
2929
+ A.reject(new Error(h));
2906
2930
  b.clear();
2907
- }, await I({ type: "init" }), a = !0;
2931
+ }, await P({ type: "init" }), a = !0;
2908
2932
  })();
2909
2933
  return {
2910
2934
  usesWorker: !0,
@@ -2912,80 +2936,80 @@ function Wi(e, t, n) {
2912
2936
  if (o) return { width: 842, height: 1264 };
2913
2937
  if (await E, o) return { width: 842, height: 1264 };
2914
2938
  if (!a) throw new Error("Worker init failed");
2915
- return await I({
2939
+ return await P({
2916
2940
  type: "loadAssets",
2917
2941
  keyHex: _,
2918
- urls: Jr(Ue())
2942
+ urls: Jr(He())
2919
2943
  }) ?? { width: 842, height: 1264 };
2920
2944
  },
2921
2945
  getVisemeAtlasPaths() {
2922
- return f;
2946
+ return d;
2923
2947
  },
2924
2948
  async renderSilOnly() {
2925
2949
  if (o || (await E, o)) return;
2926
- i += 1, m.reset(), d.clear(), u.forEach(({ resolve: p }) => p(null)), u.clear(), c = kt;
2950
+ i += 1, f.reset(), m.clear(), c.forEach(({ resolve: h }) => h(null)), c.clear(), u = vt;
2927
2951
  const _ = i;
2928
- await I({ type: "cancelLiveDraws" }), await I(
2952
+ await P({ type: "cancelLiveDraws" }), await P(
2929
2953
  { type: "renderSil", generation: _ },
2930
2954
  void 0,
2931
2955
  !0
2932
2956
  );
2933
2957
  },
2934
- drawLiveFrame(_, p, v, N) {
2958
+ drawLiveFrame(_, h, A, O) {
2935
2959
  o || E.then(() => {
2936
- o || m.enqueue(p, v, N);
2960
+ o || f.enqueue(h, A, O);
2937
2961
  });
2938
2962
  },
2939
- compositePrefetchFrame(_, p) {
2963
+ compositePrefetchFrame(_, h) {
2940
2964
  return o ? Promise.resolve(null) : E.then(() => {
2941
2965
  if (o) return null;
2942
- const v = ++c;
2943
- return new Promise((N) => {
2944
- u.set(v, { resolve: N }), I(
2966
+ const A = ++u;
2967
+ return new Promise((O) => {
2968
+ c.set(A, { resolve: O }), P(
2945
2969
  {
2946
2970
  type: "drawFrame",
2947
2971
  diffPath: _.mouthPath,
2948
2972
  expressionDiffPath: _.expressionPath,
2949
- liveSeq: v
2973
+ liveSeq: A
2950
2974
  },
2951
2975
  void 0,
2952
2976
  !0
2953
2977
  ).catch(() => {
2954
- u.delete(v), N(null);
2978
+ c.delete(A), O(null);
2955
2979
  });
2956
2980
  });
2957
2981
  });
2958
2982
  },
2959
- blitBitmap(_, p) {
2960
- Gt(_, p);
2983
+ blitBitmap(_, h) {
2984
+ jt(_, h);
2961
2985
  },
2962
2986
  supportsBufferedPlayback: !0,
2963
- async renderViseme(_, p, v = {}) {
2987
+ async renderViseme(_, h, A = {}) {
2964
2988
  if (o || (await E, o)) return;
2965
- i += 1, m.reset(), d.clear();
2966
- const N = i;
2989
+ i += 1, f.reset(), m.clear();
2990
+ const O = i;
2967
2991
  try {
2968
- await I({ type: "cancelLiveDraws" }), await I(
2992
+ await P({ type: "cancelLiveDraws" }), await P(
2969
2993
  {
2970
2994
  type: "renderViseme",
2971
- generation: N,
2972
- to: p,
2973
- from: v.from,
2974
- transitionDurationMs: v.transitionDurationMs,
2975
- gapMs: v.gapMs,
2976
- threshold: v.threshold
2995
+ generation: O,
2996
+ to: h,
2997
+ from: A.from,
2998
+ transitionDurationMs: A.transitionDurationMs,
2999
+ gapMs: A.gapMs,
3000
+ threshold: A.threshold
2977
3001
  },
2978
3002
  void 0,
2979
3003
  !0
2980
3004
  );
2981
- } catch (F) {
2982
- if (F?.name === "AbortError") return;
2983
- throw F;
3005
+ } catch (B) {
3006
+ if (B?.name === "AbortError") return;
3007
+ throw B;
2984
3008
  }
2985
3009
  },
2986
3010
  dispose() {
2987
3011
  if (!o) {
2988
- o = !0, i += 1, m.reset(), d.clear(), u.forEach(({ resolve: _ }) => _(null)), u.clear(), c = kt;
3012
+ o = !0, i += 1, f.reset(), m.clear(), c.forEach(({ resolve: _ }) => _(null)), c.clear(), u = vt;
2989
3013
  for (const [, _] of b)
2990
3014
  _.resolve();
2991
3015
  b.clear(), r?.terminate(), r = null;
@@ -3008,7 +3032,7 @@ class $i extends Error {
3008
3032
  super(`${t} timed out after ${Math.round(n / 1e3)}s`), this.name = "LoadTimeoutError";
3009
3033
  }
3010
3034
  }
3011
- function Nt(e, t, n) {
3035
+ function Ut(e, t, n) {
3012
3036
  return new Promise((r, s) => {
3013
3037
  const i = globalThis.setTimeout(() => {
3014
3038
  s(new $i(n, t));
@@ -3043,10 +3067,10 @@ const qi = {
3043
3067
  fontSize: "0.875rem",
3044
3068
  textAlign: "center"
3045
3069
  };
3046
- function ot(e) {
3047
- return Lr(e) ? er : Zn;
3070
+ function ct(e) {
3071
+ return Or(e) ? er : Zn;
3048
3072
  }
3049
- function Me(e, t) {
3073
+ function Re(e, t) {
3050
3074
  return t?.isSpeaking() ? t : e.isSpeaking() ? e : t?.isConnected() ? t : e;
3051
3075
  }
3052
3076
  const na = _r(function({
@@ -3056,556 +3080,560 @@ const na = _r(function({
3056
3080
  assets: s,
3057
3081
  authToken: i,
3058
3082
  apiBase: a,
3059
- ttsEngineId: o = je,
3060
- voiceId: f,
3061
- speakingRate: d = Pt,
3062
- className: u,
3063
- style: c,
3064
- canvasStyle: m,
3083
+ ttsEngineId: o = ze,
3084
+ voiceId: d,
3085
+ speakingRate: m = Rt,
3086
+ className: c,
3087
+ style: u,
3088
+ canvasStyle: f,
3065
3089
  onStatusChange: w,
3066
3090
  onDisplayStatus: b,
3067
- onError: h,
3068
- onReady: I,
3091
+ onError: p,
3092
+ onReady: P,
3069
3093
  showErrorOverlay: E = !0,
3070
3094
  onUserTranscript: _,
3071
- onBotOutput: p,
3072
- onRealtimeLipsyncDebug: v,
3073
- onFrameRenderStats: N,
3074
- lipsyncBufferedPlayback: F = !0,
3075
- lipsyncPlaybackWarmupMs: j = 1500
3076
- }, R) {
3077
- const S = O(null), M = O(null), T = O(null), Z = O(F);
3078
- Z.current = F;
3079
- const D = O(0), X = O(un()), z = O(li()), se = O(0), ne = O(0), de = O(0), ue = O(null), y = O(null), A = O(!1), q = O(!1), x = O(!1), P = O("sil"), $ = O(0), C = O(!1), B = O(
3095
+ onBotOutput: h,
3096
+ onRealtimeLipsyncDebug: A,
3097
+ onFrameRenderStats: O,
3098
+ lipsyncBufferedPlayback: B = !0,
3099
+ lipsyncPlaybackWarmupMs: G = 1500
3100
+ }, C) {
3101
+ const k = U(null), I = U(null), M = U(null), Q = U(B);
3102
+ Q.current = B;
3103
+ const x = U(0), z = U(ln()), J = U(li()), ie = U(0), X = U(0), he = U(0), fe = U(null), de = U(null), ne = U(!1), g = U(!1), S = U(!1), L = U("sil"), W = U(0), D = U(!1), R = U(
3080
3104
  null
3081
- ), Y = O(!1), [ee, pe] = at("idle"), [me, le] = at(null), [fe, Ve] = at(!1), [xe, We] = at(!1), [ft, dn] = at({ width: 842, height: 1264 }), l = O(i ?? null), k = a?.trim() || lt, V = O(null), K = O(null), ie = O(!1), Pe = O({ width: 842, height: 1264 }), ae = O(null), Re = O("sil"), ge = O(void 0), Ze = O(/* @__PURE__ */ new Map()), et = O(I), re = O(b), tt = O(h), dt = O(_), mt = O(p), be = O(v), nt = O(N), ve = O(vi());
3082
- et.current = I, re.current = b, tt.current = h, dt.current = _, mt.current = p, be.current = v, nt.current = N, Ce(() => {
3083
- const g = ve.current;
3084
- return Fn(g), () => Fn(null);
3105
+ ), T = U(!1), [Z, ae] = ot("idle"), [te, ue] = ot(null), [me, ge] = ot(!1), [Ae, Fe] = ot(!1), [De, dt] = ot({ width: 842, height: 1264 }), l = U(i ?? null), v = a?.trim() || ft, N = U(null), q = U(null), re = U(!1), xe = U({ width: 842, height: 1264 }), oe = U(null), Ce = U("sil"), _e = U(void 0), et = U(/* @__PURE__ */ new Map()), tt = U(P), se = U(b), nt = U(p), mt = U(_), ht = U(h), be = U(A), rt = U(O), Ee = U(bi());
3106
+ tt.current = P, se.current = b, nt.current = p, mt.current = _, ht.current = h, be.current = A, rt.current = O, Le(() => {
3107
+ const y = Ee.current;
3108
+ return Fn(y), () => Fn(null);
3085
3109
  }, []);
3086
- const oe = Se(
3087
- (g) => {
3088
- const L = g?.ttsEngineId ?? o ?? K.current?.ttsEngineId ?? je, U = g?.voiceId ?? f ?? K.current?.voiceId ?? ot(L);
3089
- return { ttsEngineId: L, voiceId: U };
3090
- },
3091
- [o, f]
3092
- ), ce = Se((g) => {
3093
- le(g), tt.current?.(g);
3094
- }, []), he = Se(
3095
- (g) => {
3096
- pe(g), w?.(g);
3110
+ const ce = ke(
3111
+ (y) => {
3112
+ const F = y?.ttsEngineId ?? o ?? q.current?.ttsEngineId ?? ze, V = y?.voiceId ?? d ?? q.current?.voiceId ?? ct(F);
3113
+ return { ttsEngineId: F, voiceId: V };
3114
+ },
3115
+ [o, d]
3116
+ ), le = ke((y) => {
3117
+ ue(y), nt.current?.(y);
3118
+ }, []), pe = ke(
3119
+ (y) => {
3120
+ ae(y), w?.(y);
3097
3121
  },
3098
3122
  [w]
3099
- ), J = O(
3100
- An(he, k, {
3101
- onLipsyncDebug: (g) => be.current?.(g)
3123
+ ), j = U(
3124
+ An(pe, v, {
3125
+ onLipsyncDebug: (y) => be.current?.(y)
3102
3126
  })
3103
- ), Q = O(null);
3104
- Ce(() => (J.current = An(
3105
- he,
3106
- k,
3127
+ ), Y = U(null);
3128
+ Le(() => (j.current = An(
3129
+ pe,
3130
+ v,
3107
3131
  {
3108
- onLipsyncDebug: (g) => be.current?.(g)
3132
+ onLipsyncDebug: (y) => be.current?.(y)
3109
3133
  }
3110
- ), i && J.current.setDeveloperToken(i), J.current.setTtsEngineId(o), () => {
3111
- J.current.stop();
3112
- const g = Q.current;
3113
- g && (g.disconnect(), Q.current = null);
3114
- }), [he, i, o, k]);
3115
- const ht = Se(() => (Q.current || (Q.current = Cs(
3116
- he,
3134
+ ), i && j.current.setDeveloperToken(i), j.current.setTtsEngineId(o), () => {
3135
+ j.current.stop();
3136
+ const y = Y.current;
3137
+ y && (y.disconnect(), Y.current = null);
3138
+ }), [pe, i, o, v]);
3139
+ const pt = ke(() => (Y.current || (Y.current = Bs(
3140
+ pe,
3117
3141
  {
3118
- speakingRate: d,
3119
- onUserTranscript: (g, L) => dt.current?.(g, L),
3120
- onBotOutput: (g) => mt.current?.(g),
3121
- onLipsyncDebug: (g) => be.current?.(g)
3142
+ speakingRate: m,
3143
+ onUserTranscript: (y, F) => mt.current?.(y, F),
3144
+ onBotOutput: (y) => ht.current?.(y),
3145
+ onLipsyncDebug: (y) => be.current?.(y)
3122
3146
  }
3123
- ), Q.current.setTtsEngineId(o)), Q.current), [he, d, o]), ye = Se(() => {
3124
- ue.current != null && (clearTimeout(ue.current), ue.current = null);
3125
- }, []), Te = Se(() => {
3126
- y.current != null && (clearTimeout(y.current), y.current = null), x.current = !1;
3127
- }, []), mn = Se(() => {
3128
- !Y.current || !B.current || (Te(), x.current = !0, y.current = setTimeout(() => {
3129
- if (y.current = null, Me(
3130
- J.current,
3131
- Q.current
3147
+ ), Y.current.setTtsEngineId(o)), Y.current), [pe, m, o]), we = ke(() => {
3148
+ fe.current != null && (clearTimeout(fe.current), fe.current = null);
3149
+ }, []), Ie = ke(() => {
3150
+ de.current != null && (clearTimeout(de.current), de.current = null), S.current = !1;
3151
+ }, []), mn = ke(() => {
3152
+ !T.current || !R.current || (Ie(), S.current = !0, de.current = setTimeout(() => {
3153
+ if (de.current = null, Re(
3154
+ j.current,
3155
+ Y.current
3132
3156
  ).isSpeaking()) {
3133
- x.current = !1;
3157
+ S.current = !1;
3134
3158
  return;
3135
3159
  }
3136
- if (A.current) {
3137
- x.current = !1;
3160
+ if (ne.current) {
3161
+ S.current = !1;
3138
3162
  return;
3139
3163
  }
3140
- const g = B.current;
3141
- if (!g) {
3142
- x.current = !1;
3164
+ const y = R.current;
3165
+ if (!y) {
3166
+ S.current = !1;
3143
3167
  return;
3144
3168
  }
3145
- g.restart(), g.setActive(!0);
3146
- const L = g.getVideo();
3147
- let U = !1;
3148
- const G = () => {
3149
- U || (U = !0, L.removeEventListener("playing", W), x.current = !1);
3150
- }, W = () => {
3151
- G();
3169
+ y.restart(), y.setActive(!0);
3170
+ const F = y.getVideo();
3171
+ let V = !1;
3172
+ const K = () => {
3173
+ V || (V = !0, F.removeEventListener("playing", H), S.current = !1);
3174
+ }, H = () => {
3175
+ K();
3152
3176
  };
3153
- if (L.addEventListener("playing", W, { once: !0 }), L.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && !L.paused) {
3154
- G();
3177
+ if (F.addEventListener("playing", H, { once: !0 }), F.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && !F.paused) {
3178
+ K();
3155
3179
  return;
3156
3180
  }
3157
- setTimeout(G, 300);
3181
+ setTimeout(K, 300);
3158
3182
  }, Ei));
3159
- }, [Te]), He = Se(() => {
3160
- if (ye(), re.current?.("sil"), A.current = !1, !Y.current || !B.current) {
3161
- x.current = !1;
3183
+ }, [Ie]), $e = ke(() => {
3184
+ if (we(), se.current?.("sil"), ne.current = !1, !T.current || !R.current) {
3185
+ S.current = !1;
3162
3186
  return;
3163
3187
  }
3164
3188
  mn();
3165
- }, [ye, mn]), Dt = Se(
3166
- async (g) => {
3167
- const L = ne.current, U = M.current, G = S.current, W = () => {
3168
- g?.fallbackOnly || (A.current = !1);
3189
+ }, [we, mn]), xt = ke(
3190
+ async (y) => {
3191
+ const F = X.current, V = I.current, K = k.current, H = () => {
3192
+ y?.fallbackOnly || (ne.current = !1);
3169
3193
  };
3170
- if (!U || !G || !fe) {
3171
- W();
3194
+ if (!V || !K || !re.current) {
3195
+ H();
3172
3196
  return;
3173
3197
  }
3174
- if (Me(
3175
- J.current,
3176
- Q.current
3198
+ if (Re(
3199
+ j.current,
3200
+ Y.current
3177
3201
  ).isSpeaking()) {
3178
- W();
3202
+ H();
3179
3203
  return;
3180
3204
  }
3181
- if (g?.fallbackOnly) {
3182
- if (!A.current || (ke(X.current), await U.renderSilOnly(), L !== ne.current) || Me(
3183
- J.current,
3184
- Q.current
3205
+ if (y?.fallbackOnly) {
3206
+ if (!ne.current || (ve(z.current), await V.renderSilOnly(), F !== X.current) || Re(
3207
+ j.current,
3208
+ Y.current
3185
3209
  ).isSpeaking())
3186
3210
  return;
3187
- He();
3211
+ $e();
3188
3212
  return;
3189
3213
  }
3190
- if (Ii(X.current)) {
3191
- ke(X.current), He();
3214
+ if (Ii(z.current)) {
3215
+ ve(z.current), $e();
3192
3216
  return;
3193
3217
  }
3194
- if (Z.current && U.supportsBufferedPlayback && !g?.fallbackOnly) {
3195
- if (ke(X.current), T.current?.reset(), await U.renderSilOnly(), L !== ne.current || Me(
3196
- J.current,
3197
- Q.current
3218
+ if (Q.current && V.supportsBufferedPlayback && !y?.fallbackOnly) {
3219
+ if (ve(z.current), M.current?.reset(), await V.renderSilOnly(), F !== X.current || Re(
3220
+ j.current,
3221
+ Y.current
3198
3222
  ).isSpeaking())
3199
3223
  return;
3200
- He();
3224
+ $e();
3201
3225
  return;
3202
3226
  }
3203
- const _e = Ri(
3204
- Me(
3205
- J.current,
3206
- Q.current
3227
+ const Se = Ri(
3228
+ Re(
3229
+ j.current,
3230
+ Y.current
3207
3231
  ).getVisemeQueue(),
3208
- $.current,
3209
- P.current
3210
- ), pt = Di(
3211
- _e,
3212
- ae.current
3232
+ W.current,
3233
+ L.current
3234
+ ), wt = Di(
3235
+ Se,
3236
+ oe.current
3213
3237
  );
3214
- if (ke(X.current), !pt) {
3215
- if (await U.renderSilOnly(), L !== ne.current || Me(
3216
- J.current,
3217
- Q.current
3238
+ if (ve(z.current), !wt) {
3239
+ if (await V.renderSilOnly(), F !== X.current || Re(
3240
+ j.current,
3241
+ Y.current
3218
3242
  ).isSpeaking())
3219
3243
  return;
3220
- He();
3244
+ $e();
3221
3245
  return;
3222
3246
  }
3223
3247
  try {
3224
- await U.renderViseme(G, "sil", {
3225
- from: _e,
3248
+ await V.renderViseme(K, "sil", {
3249
+ from: Se,
3226
3250
  transitionDurationMs: lr
3227
3251
  });
3228
- } catch (De) {
3229
- if (De?.name !== "AbortError")
3230
- throw De;
3252
+ } catch (Be) {
3253
+ if (Be?.name !== "AbortError")
3254
+ throw Be;
3231
3255
  }
3232
- if (L !== ne.current) {
3233
- W();
3256
+ if (F !== X.current) {
3257
+ H();
3234
3258
  return;
3235
3259
  }
3236
- if (Me(
3237
- J.current,
3238
- Q.current
3260
+ if (Re(
3261
+ j.current,
3262
+ Y.current
3239
3263
  ).isSpeaking()) {
3240
- W();
3264
+ H();
3241
3265
  return;
3242
3266
  }
3243
- He();
3244
- },
3245
- [fe, He]
3246
- ), rt = Se(() => {
3247
- if (q.current || A.current) return;
3248
- q.current = !0, A.current = !0, ye();
3249
- const g = ne.current;
3250
- Dt().finally(() => {
3251
- q.current = !1;
3252
- }), ue.current = setTimeout(() => {
3253
- ue.current = null, g === ne.current && Dt({ fallbackOnly: !0 });
3267
+ $e();
3268
+ },
3269
+ [me, $e]
3270
+ ), st = ke(() => {
3271
+ if (g.current || ne.current) return;
3272
+ g.current = !0, ne.current = !0, we();
3273
+ const y = X.current;
3274
+ xt().finally(() => {
3275
+ g.current = !1;
3276
+ }), fe.current = setTimeout(() => {
3277
+ fe.current = null, y === X.current && xt({ fallbackOnly: !0 });
3254
3278
  }, xi());
3255
- }, [ye, Dt]), hn = Se(async () => {
3256
- const g = Math.floor(Date.now() / 1e3);
3257
- if (V.current && g < V.current.expiresAt - 60)
3258
- return V.current.key;
3259
- const L = l.current ?? await Ft();
3260
- l.current = L, J.current.setDeveloperToken(L);
3261
- const U = await qr(L, k);
3262
- return V.current = {
3263
- key: U.value,
3264
- expiresAt: U.expiresAt
3265
- }, U.value;
3266
- }, [k]);
3267
- Ce(() => {
3268
- i && (l.current = i, J.current.setDeveloperToken(i));
3269
- }, [i]), Ce(() => {
3270
- J.current.setTtsEngineId(o), Q.current?.setTtsEngineId(o), K.current && (K.current.ttsEngineId = o);
3271
- }, [o]), Ce(() => {
3272
- const g = f ?? ot(o);
3273
- K.current && (K.current.voiceId = g);
3274
- }, [f, o]);
3275
- const Be = _n(
3279
+ }, [we, xt]), hn = ke(async () => {
3280
+ const y = Math.floor(Date.now() / 1e3);
3281
+ if (N.current && y < N.current.expiresAt - 60)
3282
+ return N.current.key;
3283
+ const F = l.current ?? await Ot();
3284
+ l.current = F, j.current.setDeveloperToken(F);
3285
+ const V = await qr(F, v);
3286
+ return N.current = {
3287
+ key: V.value,
3288
+ expiresAt: V.expiresAt
3289
+ }, V.value;
3290
+ }, [v]);
3291
+ Le(() => {
3292
+ i && (l.current = i, j.current.setDeveloperToken(i));
3293
+ }, [i]), Le(() => {
3294
+ j.current.setTtsEngineId(o), Y.current?.setTtsEngineId(o), q.current && (q.current.ttsEngineId = o);
3295
+ }, [o]), Le(() => {
3296
+ const y = d ?? ct(o);
3297
+ q.current && (q.current.voiceId = y);
3298
+ }, [d, o]);
3299
+ const Oe = _n(
3276
3300
  () => zr(n, r),
3277
3301
  [n, r]
3278
- ), hr = _n(() => s ? JSON.stringify(s) : Be ? `face:${Be}` : `id:${t ?? ""}`, [s, Be, t]);
3279
- return Ce(() => {
3280
- if (!t && !s && !Be) {
3281
- ce("AiTwin requires `assets`, `avatarId`/`faceId`, or `id`");
3302
+ ), hr = _n(() => s ? JSON.stringify(s) : Oe ? `face:${Oe}` : `id:${t ?? ""}`, [s, Oe, t]);
3303
+ return Le(() => {
3304
+ if (!t && !s && !Oe) {
3305
+ le("AiTwin requires `assets`, `avatarId`/`faceId`, or `id`");
3282
3306
  return;
3283
3307
  }
3284
- let g = !1;
3285
- ie.current = !1, Ve(!1), We(!1), le(null), K.current = null, B.current = null, Y.current = !1;
3286
- const L = S.current;
3287
- if (!L) return;
3288
- let U = null;
3308
+ let y = !1;
3309
+ re.current = !1, ge(!1), Fe(!1), ue(null), q.current = null, R.current = null, T.current = !1;
3310
+ const F = k.current;
3311
+ if (!F) return;
3312
+ let V = null;
3289
3313
  return (async () => {
3290
3314
  try {
3291
- let G, W, we, _e;
3315
+ let K, H, ye, Se;
3292
3316
  if (s)
3293
- G = s, W = o, we = f ?? ot(W);
3294
- else if (Be)
3295
- re.current?.("loadFace"), G = bn(Be), W = o, we = f ?? ot(W);
3317
+ K = s, H = o, ye = d ?? ct(H);
3318
+ else if (Oe)
3319
+ se.current?.("loadFace"), K = vn(Oe), H = o, ye = d ?? ct(H);
3296
3320
  else {
3297
- re.current?.("getAiTwin");
3298
- const H = await Nt(
3321
+ se.current?.("getAiTwin");
3322
+ const $ = await Ut(
3299
3323
  Nr(t),
3300
3324
  3e4,
3301
3325
  "getAiTwin"
3302
3326
  );
3303
- if (g) return;
3304
- if (!H.faceId?.trim())
3327
+ if (y) return;
3328
+ if (!$.faceId?.trim())
3305
3329
  throw new Error("getAiTwin returned no faceId");
3306
- W = H.ttsEngineId, we = f ?? H.voiceId ?? ot(W), _e = H.knowledgeContextId?.trim() || void 0, G = bn(H.faceId);
3330
+ H = $.ttsEngineId, ye = d ?? $.voiceId ?? ct(H), Se = $.knowledgeContextId?.trim() || void 0, K = vn($.faceId);
3307
3331
  }
3308
- K.current = { ttsEngineId: W, voiceId: we, personaId: _e }, Yr(G), B.current = fi(), U = Hi(L, {
3309
- onStatus: (H) => {
3310
- g || re.current?.(H);
3332
+ q.current = { ttsEngineId: H, voiceId: ye, personaId: Se }, Yr(K), R.current = fi(), V = Hi(F, {
3333
+ onStatus: ($) => {
3334
+ y || se.current?.($);
3311
3335
  },
3312
- onError: (H) => {
3313
- g || ce(H);
3336
+ onError: ($) => {
3337
+ y || le($);
3314
3338
  },
3315
- shouldAcceptLiveFrame: () => Me(
3316
- J.current,
3317
- Q.current
3318
- ).isSpeaking() && !A.current
3319
- }), M.current = U, T.current = Ni(U), ((H) => {
3320
- const te = () => H && F && (M.current?.supportsBufferedPlayback ?? !1) && j > 0;
3321
- J.current.setLipsyncWarmupHold(H, te), Q.current?.setLipsyncWarmupHold(H, te);
3322
- })(!0), re.current?.("auth");
3323
- const De = l.current ?? i ?? await Nt(Ft(), 3e4, "getAuthToken");
3324
- if (g) return;
3325
- l.current = De, J.current.setDeveloperToken(De), J.current.setTtsEngineId(W);
3326
- const st = It() ? await hn() : void 0;
3327
- if (g) return;
3328
- re.current?.("loadAssets");
3329
- const $e = await Nt(
3330
- U.loadAssets(st),
3339
+ shouldAcceptLiveFrame: () => Re(
3340
+ j.current,
3341
+ Y.current
3342
+ ).isSpeaking() && !ne.current
3343
+ }), I.current = V, M.current = Ni(V), (($) => {
3344
+ const ee = () => $ && B && (I.current?.supportsBufferedPlayback ?? !1) && G > 0;
3345
+ j.current.setLipsyncWarmupHold($, ee), Y.current?.setLipsyncWarmupHold($, ee);
3346
+ })(!0), se.current?.("auth");
3347
+ const Be = l.current ?? i ?? await Ut(Ot(), 3e4, "getAuthToken");
3348
+ if (y) return;
3349
+ l.current = Be, j.current.setDeveloperToken(Be), j.current.setTtsEngineId(H);
3350
+ const it = Mt() ? await hn() : void 0;
3351
+ if (y) return;
3352
+ se.current?.("loadAssets");
3353
+ const qe = await Ut(
3354
+ V.loadAssets(it),
3331
3355
  18e4,
3332
3356
  "load face assets"
3333
3357
  );
3334
- if (!g && $e.width > 0 && $e.height > 0 && (Pe.current = $e, dn($e)), g) return;
3335
- ae.current = U.getVisemeAtlasPaths(), ie.current = !0, Ve(!0), et.current?.(), U.renderSilOnly().then(() => {
3336
- g || re.current?.("sil");
3358
+ if (!y && qe.width > 0 && qe.height > 0 && (xe.current = qe, dt(qe)), y) return;
3359
+ oe.current = V.getVisemeAtlasPaths(), re.current = !0, ge(!0), requestAnimationFrame(() => {
3360
+ requestAnimationFrame(() => {
3361
+ tt.current?.();
3362
+ });
3363
+ }), V.renderSilOnly().then(() => {
3364
+ y || se.current?.("sil");
3337
3365
  }), (async () => {
3338
3366
  try {
3339
- const H = await Hs(st);
3340
- if (await B.current.load(H), g) return;
3341
- Y.current = !0, We(!0), ke(X.current), B.current.setActive(!0);
3342
- const te = B.current.getVideo();
3343
- await Cn(
3344
- L,
3345
- te,
3367
+ const $ = await Hs(it);
3368
+ if (await R.current.load($), y) return;
3369
+ T.current = !0, Fe(!0), ve(z.current), R.current.setActive(!0);
3370
+ const ee = R.current.getVideo();
3371
+ await Bn(
3372
+ F,
3373
+ ee,
3346
3374
  null,
3347
3375
  void 0,
3348
- Pe.current
3349
- ), g || re.current?.("idle");
3350
- } catch (H) {
3351
- if (g) return;
3352
- Y.current = !1, We(!1), console.warn("[AiTwin] Idle video unavailable:", H);
3376
+ xe.current
3377
+ ), y || se.current?.("idle");
3378
+ } catch ($) {
3379
+ if (y) return;
3380
+ T.current = !1, Fe(!1), console.warn("[AiTwin] Idle video unavailable:", $);
3353
3381
  }
3354
3382
  })();
3355
- } catch (G) {
3356
- if (g) return;
3357
- if (G instanceof Ut) {
3358
- ce(G.message);
3383
+ } catch (K) {
3384
+ if (y) return;
3385
+ if (K instanceof Vt) {
3386
+ le(K.message);
3359
3387
  return;
3360
3388
  }
3361
- ce(G instanceof Error ? G.message : "Failed to load AI twin");
3389
+ le(K instanceof Error ? K.message : "Failed to load AI twin");
3362
3390
  }
3363
3391
  })(), () => {
3364
- g = !0, B.current?.dispose(), U?.dispose(), M.current = null;
3392
+ y = !0, R.current?.dispose(), V?.dispose(), I.current = null;
3365
3393
  };
3366
- }, [hr, s, Be, t, o, f, i, hn, ce]), Ce(() => {
3367
- const g = { current: !1 }, L = () => {
3368
- const U = S.current, G = M.current, W = Me(
3369
- J.current,
3370
- Q.current
3394
+ }, [hr, s, Oe, t, o, d, i, hn, le]), Le(() => {
3395
+ const y = { current: !1 }, F = () => {
3396
+ const V = k.current, K = I.current, H = Re(
3397
+ j.current,
3398
+ Y.current
3371
3399
  );
3372
- if (!U || !G || !fe) {
3373
- se.current = requestAnimationFrame(L);
3400
+ if (!V || !K || !re.current) {
3401
+ ie.current = requestAnimationFrame(F);
3374
3402
  return;
3375
3403
  }
3376
- const we = W.isSpeaking(), _e = W.isWarmingUp(), pt = we && !C.current, De = C.current && !we;
3377
- pt && (ye(), Te(), A.current = !1, q.current = !1, ke(X.current), (T.current?.getBufferedFrameCount() ?? 0) === 0 && T.current?.reset(), ve.current.reset(), ge.current = void 0, de.current = 0, P.current = "sil", $.current = 0, B.current?.setActive(!1)), De && (z.current.reset(), B.current?.setActive(!1), rt());
3378
- const st = De || A.current || x.current, $e = F && G.supportsBufferedPlayback;
3379
- if (_e && $e && T.current) {
3380
- Y.current && B.current?.setActive(!1);
3381
- const H = W.getVisemeQueue();
3382
- H.length > 0 && D.current === 0 && (D.current = performance.now()), z.current.advance(performance.now());
3383
- const te = z.current.getExpressionPath(), Ee = z.current.getDrawKey(), Ae = {
3384
- queue: H,
3404
+ const ye = H.isSpeaking(), Se = H.isWarmingUp(), wt = ye && !D.current, Be = D.current && !ye;
3405
+ wt && (we(), Ie(), ne.current = !1, g.current = !1, ve(z.current), (M.current?.getBufferedFrameCount() ?? 0) === 0 && M.current?.reset(), Ee.current.reset(), _e.current = void 0, he.current = 0, L.current = "sil", W.current = 0, R.current?.setActive(!1)), Be && (J.current.reset(), R.current?.setActive(!1), st());
3406
+ const it = Be || ne.current || S.current, qe = B && K.supportsBufferedPlayback;
3407
+ if (Se && qe && M.current) {
3408
+ T.current && R.current?.setActive(!1);
3409
+ const $ = H.getVisemeQueue();
3410
+ $.length > 0 && x.current === 0 && (x.current = performance.now()), J.current.advance(performance.now());
3411
+ const ee = J.current.getExpressionPath(), Te = J.current.getDrawKey(), Me = {
3412
+ queue: $,
3385
3413
  elapsedMs: 0,
3386
- expressionPath: te,
3387
- expressionDrawKey: Ee,
3388
- availablePaths: ae.current
3414
+ expressionPath: ee,
3415
+ expressionDrawKey: Te,
3416
+ availablePaths: oe.current
3389
3417
  };
3390
- T.current.warmupTick(Ae);
3391
- const Ie = T.current.isWarmupReady(
3392
- Ae,
3393
- j
3394
- ), qe = D.current > 0 && performance.now() - D.current > xr;
3395
- re.current?.(
3396
- Ie ? "buffer ready" : "buffering lipsync…"
3397
- ), (Ie || qe) && (D.current = 0, W.releaseWarmupHold()), C.current = W.isSpeaking(), se.current = requestAnimationFrame(L);
3418
+ M.current.warmupTick(Me);
3419
+ const Pe = M.current.isWarmupReady(
3420
+ Me,
3421
+ G
3422
+ ), Ke = x.current > 0 && performance.now() - x.current > xr;
3423
+ se.current?.(
3424
+ Pe ? "buffer ready" : "buffering lipsync…"
3425
+ ), (Pe || Ke) && (x.current = 0, H.releaseWarmupHold()), D.current = H.isSpeaking(), ie.current = requestAnimationFrame(F);
3398
3426
  return;
3399
3427
  }
3400
- if (we) {
3401
- Y.current && B.current?.setActive(!1);
3402
- const H = W.getVisemeQueue(), te = W.getPlaybackElapsedMs();
3403
- Es(H, te), $.current = te;
3404
- const Ee = H.length > 0 ? Mt(H, te) : "sil";
3405
- Ee !== "sil" && (P.current = Ee);
3406
- const Ae = pi(
3407
- te,
3408
- W.getSentenceEmotions(),
3409
- W.getWordQueue(),
3410
- W.getStreamMood()
3428
+ if (ye) {
3429
+ T.current && R.current?.setActive(!1);
3430
+ const $ = H.getVisemeQueue(), ee = H.getPlaybackElapsedMs();
3431
+ Es($, ee), W.current = ee;
3432
+ const Te = $.length > 0 ? Pt($, ee) : "sil";
3433
+ Te !== "sil" && (L.current = Te);
3434
+ const Me = pi(
3435
+ ee,
3436
+ H.getSentenceEmotions(),
3437
+ H.getWordQueue(),
3438
+ H.getStreamMood()
3411
3439
  );
3412
- z.current.setTargetEmotion(Ae), z.current.setTargetEyebrow(
3440
+ J.current.setTargetEmotion(Me), J.current.setTargetEyebrow(
3413
3441
  ui(
3414
- te,
3415
- W.getWordQueue(),
3416
- sn(Ae)
3442
+ ee,
3443
+ H.getWordQueue(),
3444
+ an(Me)
3417
3445
  )
3418
- ), z.current.advance(performance.now());
3419
- const Ie = z.current.getExpressionPath(), qe = z.current.getDrawKey(), pr = (Ke, xt, it, Bt) => {
3446
+ ), J.current.advance(performance.now());
3447
+ const Pe = J.current.getExpressionPath(), Ke = J.current.getDrawKey(), pr = (Ge, Ct, at, Bt) => {
3420
3448
  const gn = {
3421
- label: Re.current,
3422
- elapsedMs: te,
3423
- mouthPath: xt.mouthPath,
3424
- transitionMeta: ge.current
3449
+ label: Ce.current,
3450
+ elapsedMs: ee,
3451
+ mouthPath: Ct.mouthPath,
3452
+ transitionMeta: _e.current
3425
3453
  };
3426
- Ze.current.set(it, gn), T.current?.markExternallyDisplayed(
3427
- it,
3428
- Ie
3429
- ), G.drawLiveFrame(Ke, xt, it, (wt) => {
3430
- const gr = Ze.current.get(wt) ?? gn;
3431
- Ze.current.delete(wt), ve.current.onDrawDisplayed(
3454
+ et.current.set(at, gn), M.current?.markExternallyDisplayed(
3455
+ at,
3456
+ Pe
3457
+ ), K.drawLiveFrame(Ge, Ct, at, (gt) => {
3458
+ const gr = et.current.get(gt) ?? gn;
3459
+ et.current.delete(gt), Ee.current.onDrawDisplayed(
3432
3460
  gr,
3433
- H
3434
- ), T.current?.markExternallyDisplayed(
3435
- wt,
3436
- Ie
3437
- ), Bt?.(wt);
3461
+ $
3462
+ ), M.current?.markExternallyDisplayed(
3463
+ gt,
3464
+ Pe
3465
+ ), Bt?.(gt);
3438
3466
  });
3439
- }, pn = (Ke) => {
3440
- Re.current = Ke.label, ge.current = Ke.transitionMeta, re.current?.(Ke.label);
3441
- }, wr = F && G.supportsBufferedPlayback;
3467
+ }, pn = (Ge) => {
3468
+ Ce.current = Ge.label, _e.current = Ge.transitionMeta, se.current?.(Ge.label);
3469
+ }, wr = B && K.supportsBufferedPlayback;
3442
3470
  let wn = !1;
3443
- wr && T.current && (wn = T.current.tick({
3444
- canvas: U,
3445
- queue: H,
3446
- elapsedMs: te,
3447
- expressionPath: Ie,
3448
- expressionDrawKey: qe,
3449
- playbackState: X.current,
3450
- availablePaths: ae.current,
3471
+ wr && M.current && (wn = M.current.tick({
3472
+ canvas: V,
3473
+ queue: $,
3474
+ elapsedMs: ee,
3475
+ expressionPath: Pe,
3476
+ expressionDrawKey: Ke,
3477
+ playbackState: z.current,
3478
+ availablePaths: oe.current,
3451
3479
  onStatus: pn,
3452
- onDisplayed: (xt, it, Bt) => {
3453
- ve.current.onDrawDisplayed(
3480
+ onDisplayed: (Ct, at, Bt) => {
3481
+ Ee.current.onDrawDisplayed(
3454
3482
  {
3455
3483
  label: Bt,
3456
- elapsedMs: te,
3457
- mouthPath: it,
3458
- transitionMeta: ge.current
3484
+ elapsedMs: ee,
3485
+ mouthPath: at,
3486
+ transitionMeta: _e.current
3459
3487
  },
3460
- H
3488
+ $
3461
3489
  );
3462
3490
  }
3463
- }).usedBuffer), wn || Bi(
3464
- U,
3465
- H,
3466
- te,
3467
- Ie,
3468
- qe,
3469
- X.current,
3491
+ }).usedBuffer), wn || Ci(
3492
+ V,
3493
+ $,
3494
+ ee,
3495
+ Pe,
3496
+ Ke,
3497
+ z.current,
3470
3498
  pr,
3471
3499
  pn,
3472
- ae.current
3473
- ), ve.current.onRafTickSpeaking(), nt.current?.(
3474
- ve.current.snapshot(H, te)
3500
+ oe.current
3501
+ ), Ee.current.onRafTickSpeaking(), rt.current?.(
3502
+ Ee.current.snapshot($, ee)
3475
3503
  );
3476
- } else if (!st && Y.current && B.current) {
3477
- const H = B.current, te = H.getVideo();
3478
- te.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA ? (H.setActive(!0), g.current || (g.current = !0, Cn(
3479
- U,
3480
- te,
3504
+ } else if (!it && T.current && R.current) {
3505
+ const $ = R.current, ee = $.getVideo();
3506
+ ee.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA ? ($.setActive(!0), y.current || (y.current = !0, Bn(
3507
+ V,
3508
+ ee,
3481
3509
  null,
3482
3510
  void 0,
3483
- Pe.current,
3484
- () => !W.isSpeaking() && !A.current && !x.current
3485
- ).then(() => re.current?.("idle")).finally(() => {
3486
- g.current = !1;
3487
- }))) : On(
3488
- U,
3511
+ xe.current,
3512
+ () => !H.isSpeaking() && !ne.current && !S.current
3513
+ ).then(() => se.current?.("idle")).finally(() => {
3514
+ y.current = !1;
3515
+ }))) : Ln(
3516
+ V,
3489
3517
  null,
3490
3518
  "__sil__",
3491
- X.current,
3492
- (Ee, Ae, Ie, qe) => G.drawLiveFrame(Ee, Ae, Ie, qe),
3493
- (Ee) => re.current?.(Ee)
3519
+ z.current,
3520
+ (Te, Me, Pe, Ke) => K.drawLiveFrame(Te, Me, Pe, Ke),
3521
+ (Te) => se.current?.(Te)
3494
3522
  );
3495
- } else st || On(
3496
- U,
3523
+ } else it || Ln(
3524
+ V,
3497
3525
  null,
3498
3526
  "__sil__",
3499
- X.current,
3500
- (H, te, Ee, Ae) => G.drawLiveFrame(H, te, Ee, Ae),
3501
- (H) => re.current?.(H)
3527
+ z.current,
3528
+ ($, ee, Te, Me) => K.drawLiveFrame($, ee, Te, Me),
3529
+ ($) => se.current?.($)
3502
3530
  );
3503
- C.current = we, se.current = requestAnimationFrame(L);
3531
+ D.current = ye, ie.current = requestAnimationFrame(F);
3504
3532
  };
3505
- return se.current = requestAnimationFrame(L), () => {
3506
- cancelAnimationFrame(se.current), ye(), Te(), J.current.stop(), Q.current?.stop();
3533
+ return ie.current = requestAnimationFrame(F), () => {
3534
+ cancelAnimationFrame(ie.current), we(), Ie(), j.current.stop(), Y.current?.stop();
3507
3535
  };
3508
3536
  }, [
3509
- fe,
3510
- xe,
3511
- rt,
3512
- ye,
3513
- Te
3537
+ me,
3538
+ Ae,
3539
+ st,
3540
+ we,
3541
+ Ie
3514
3542
  ]), Sr(
3515
- R,
3543
+ C,
3516
3544
  () => ({
3517
- speakText: async (g, L) => {
3518
- const U = g.trim();
3519
- if (!U) return;
3520
- const G = M.current;
3521
- if (!G || !ie.current && !fe)
3545
+ speakText: async (y, F) => {
3546
+ const V = y.trim();
3547
+ if (!V) return;
3548
+ const K = I.current;
3549
+ if (!K || !re.current && !me)
3522
3550
  throw new Error("AI twin is not ready");
3523
- const { ttsEngineId: W, voiceId: we } = oe(L);
3524
- le(null), ne.current += 1, ye(), Te(), A.current = !1, q.current = !1, P.current = "sil", $.current = 0, ke(X.current), z.current.reset(), C.current = !1;
3551
+ const { ttsEngineId: H, voiceId: ye } = ce(F);
3552
+ ue(null), X.current += 1, we(), Ie(), ne.current = !1, g.current = !1, L.current = "sil", W.current = 0, ve(z.current), J.current.reset(), D.current = !1;
3525
3553
  try {
3526
- Y.current ? (B.current?.setActive(!1), B.current?.restart(), ke(X.current)) : await G.renderSilOnly(), await J.current.speak(U, {
3527
- voiceId: we,
3528
- speakingRate: L?.speakingRate ?? d,
3529
- ttsEngineId: W
3554
+ T.current ? (R.current?.setActive(!1), R.current?.restart(), ve(z.current)) : await K.renderSilOnly(), await j.current.speak(V, {
3555
+ voiceId: ye,
3556
+ speakingRate: F?.speakingRate ?? m,
3557
+ ttsEngineId: H
3530
3558
  });
3531
- } catch (_e) {
3532
- throw ce(_e instanceof Error ? _e.message : "TTS failed"), _e;
3559
+ } catch (Se) {
3560
+ throw le(Se instanceof Error ? Se.message : "TTS failed"), Se;
3533
3561
  }
3534
3562
  },
3535
3563
  stop: () => {
3536
- ne.current += 1, ye(), Te(), A.current = !1, q.current = !1, J.current.stop(), Q.current?.stop(), ke(X.current), T.current?.reset(), D.current = 0, C.current = !1, rt();
3564
+ X.current += 1, we(), Ie(), ne.current = !1, g.current = !1, j.current.stop(), Y.current?.stop(), ve(z.current), M.current?.reset(), x.current = 0, D.current = !1, st();
3537
3565
  },
3538
- setTtsEngineId: (g) => {
3539
- J.current.setTtsEngineId(g), Q.current?.setTtsEngineId(g), K.current && (K.current.ttsEngineId = g);
3566
+ setTtsEngineId: (y) => {
3567
+ j.current.setTtsEngineId(y), Y.current?.setTtsEngineId(y), q.current && (q.current.ttsEngineId = y);
3540
3568
  },
3541
- renderViseme: async (g, L) => {
3542
- const U = M.current, G = S.current;
3543
- if (!U || !G || !ie.current)
3569
+ renderViseme: async (y, F) => {
3570
+ const V = I.current, K = k.current;
3571
+ if (!V || !K || !re.current)
3544
3572
  throw new Error("AI twin is not ready");
3545
- await U.renderViseme(G, g, {
3546
- from: L?.from,
3547
- transitionDurationMs: L?.transitionDurationMs,
3548
- gapMs: L?.gapMs,
3549
- onStatus: (W) => re.current?.(W)
3573
+ await V.renderViseme(K, y, {
3574
+ from: F?.from,
3575
+ transitionDurationMs: F?.transitionDurationMs,
3576
+ gapMs: F?.gapMs,
3577
+ onStatus: (H) => se.current?.(H)
3550
3578
  });
3551
3579
  },
3552
- isReady: () => ie.current,
3553
- getStatus: () => ee,
3554
- connect: async (g = {}) => {
3555
- le(null);
3580
+ isReady: () => re.current,
3581
+ getStatus: () => Z,
3582
+ connect: async (y = {}) => {
3583
+ ue(null);
3556
3584
  try {
3557
- let L = g.authToken?.trim();
3558
- L || (L = l.current ?? i ?? await Ft(), l.current = L);
3559
- const U = g.voiceId?.trim() ?? f ?? K.current?.voiceId, G = g.personaId?.trim() ?? K.current?.personaId, W = ht();
3560
- await W.unlockAudio(), await W.connect({
3561
- ...g,
3562
- apiBase: g.apiBase?.trim() || k,
3563
- authToken: L,
3564
- voiceId: U,
3565
- personaId: G,
3566
- speakingRate: g.speakingRate ?? d
3585
+ let F = y.authToken?.trim();
3586
+ F || (F = l.current ?? i ?? await Ot(), l.current = F);
3587
+ const V = y.voiceId?.trim() ?? d ?? q.current?.voiceId, K = y.personaId?.trim() ?? q.current?.personaId, H = pt();
3588
+ await H.unlockAudio(), await H.connect({
3589
+ ...y,
3590
+ apiBase: y.apiBase?.trim() || v,
3591
+ authToken: F,
3592
+ voiceId: V,
3593
+ personaId: K,
3594
+ speakingRate: y.speakingRate ?? m
3567
3595
  });
3568
- } catch (L) {
3569
- throw ce(
3570
- L instanceof Error ? L.message : "Voice connect failed"
3571
- ), L;
3596
+ } catch (F) {
3597
+ throw le(
3598
+ F instanceof Error ? F.message : "Voice connect failed"
3599
+ ), F;
3572
3600
  }
3573
3601
  },
3574
3602
  disconnect: async () => {
3575
- const g = Q.current;
3576
- g && (ne.current += 1, ye(), Te(), A.current = !1, q.current = !1, ke(X.current), T.current?.reset(), D.current = 0, C.current = !1, await g.disconnect(), rt());
3603
+ const y = Y.current;
3604
+ y && (X.current += 1, we(), Ie(), ne.current = !1, g.current = !1, ve(z.current), M.current?.reset(), x.current = 0, D.current = !1, await y.disconnect(), st());
3577
3605
  },
3578
- isConnected: () => Q.current?.isConnected() ?? !1,
3579
- getVoiceStream: () => Q.current?.getVoiceStream() ?? J.current?.getVoiceStream() ?? null
3606
+ isConnected: () => Y.current?.isConnected() ?? !1,
3607
+ getVoiceStream: () => Y.current?.getVoiceStream() ?? j.current?.getVoiceStream() ?? null
3580
3608
  }),
3581
3609
  [
3582
- fe,
3583
- d,
3610
+ me,
3611
+ m,
3584
3612
  i,
3585
- f,
3586
- ye,
3587
- Te,
3588
- rt,
3613
+ d,
3614
+ we,
3615
+ Ie,
3616
+ st,
3617
+ le,
3589
3618
  ce,
3590
- oe,
3591
- ht,
3592
- ee
3619
+ pt,
3620
+ Z
3593
3621
  ]
3594
- ), /* @__PURE__ */ yr("div", { className: u, style: { ...qi, ...c }, children: [
3622
+ ), /* @__PURE__ */ yr("div", { className: c, style: { ...qi, ...u }, children: [
3595
3623
  /* @__PURE__ */ yn(
3596
3624
  "canvas",
3597
3625
  {
3598
- ref: S,
3599
- width: ft.width,
3600
- height: ft.height,
3601
- style: { ...Ki, ...m },
3626
+ ref: k,
3627
+ width: De.width,
3628
+ height: De.height,
3629
+ style: { ...Ki, ...f },
3602
3630
  "aria-label": t ? `AI twin ${t}` : "AI twin face"
3603
3631
  }
3604
3632
  ),
3605
- E && me ? /* @__PURE__ */ yn("div", { style: Gi, children: me }) : null
3633
+ E && te ? /* @__PURE__ */ yn("div", { style: Gi, children: te }) : null
3606
3634
  ] });
3607
3635
  }), ji = "thumbnail.webp";
3608
- function ra(e, t = jt) {
3636
+ function ra(e, t = zt) {
3609
3637
  const n = e.trim();
3610
3638
  if (!n)
3611
3639
  throw new Error("faceId is required");
@@ -3616,7 +3644,7 @@ function Yi(e = ".png") {
3616
3644
  const t = e.toLowerCase();
3617
3645
  return zi.has(t) ? `source${t}` : "source.png";
3618
3646
  }
3619
- function sa(e, t = jt, n = ".png") {
3647
+ function sa(e, t = zt, n = ".png") {
3620
3648
  const r = e.trim();
3621
3649
  if (!r)
3622
3650
  throw new Error("faceId is required");
@@ -3625,32 +3653,32 @@ function sa(e, t = jt, n = ".png") {
3625
3653
  export {
3626
3654
  ji as AI_TWIN_THUMBNAIL_FILENAME,
3627
3655
  na as AiTwin,
3628
- Ut as AiTwinNotFoundError,
3629
- je as DEFAULT_TTS_ENGINE_ID,
3656
+ Vt as AiTwinNotFoundError,
3657
+ ze as DEFAULT_TTS_ENGINE_ID,
3630
3658
  tr as MIC_SAMPLE_RATE,
3631
3659
  ns as OCULUS_VISEME_IDS,
3632
3660
  nr as PLAYBACK_SAMPLE_RATE,
3633
- Zt as SPEAKING_RATE_MAX,
3634
- Qt as SPEAKING_RATE_MIN,
3635
- Xe as TTS_ENGINE_CARTESIA,
3661
+ en as SPEAKING_RATE_MAX,
3662
+ Zt as SPEAKING_RATE_MIN,
3663
+ Qe as TTS_ENGINE_CARTESIA,
3636
3664
  Hn as TTS_ENGINE_GOOGLE,
3637
- Yt as TTS_ENGINE_INWORLD,
3665
+ Jt as TTS_ENGINE_INWORLD,
3638
3666
  ta as VISEME_IDS,
3639
3667
  er as VISEME_TEST_CARTESIA_VOICE_ID,
3640
- Pt as VISEME_TEST_SPEAKING_RATE,
3668
+ Rt as VISEME_TEST_SPEAKING_RATE,
3641
3669
  Zn as VISEME_TEST_VOICE_ID,
3642
3670
  Yi as aiTwinSourceFilename,
3643
3671
  Ds as buildVoiceWebSocketUrl,
3644
3672
  An as createAvatarTtsLipsyncController,
3645
- Cs as createRealtimeVoiceLipsyncController,
3646
- bn as faceAssetUrls,
3673
+ Bs as createRealtimeVoiceLipsyncController,
3674
+ vn as faceAssetUrls,
3647
3675
  Nr as fetchAiTwin,
3648
- Ft as fetchDevAuthToken,
3676
+ Ot as fetchDevAuthToken,
3649
3677
  sa as getAiTwinSourceUrl,
3650
3678
  ra as getAiTwinThumbnailUrl,
3651
3679
  ss as normalizeOculusViseme,
3652
3680
  zr as resolveDirectFaceId,
3653
- Mt as resolveVisemeAtTime,
3681
+ Pt as resolveVisemeAtTime,
3654
3682
  ea as resolveWordAtTime,
3655
3683
  Yr as setActiveTwinAssets
3656
3684
  };