@streamoji/aitwin 0.3.9 → 0.4.0

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