@streamoji/aitwin 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/aitwin.js CHANGED
@@ -1,42 +1,42 @@
1
- import { jsxs as kn, jsx as Rt } from "react/jsx-runtime";
2
- import { forwardRef as vn, useRef as L, useState as Pe, useCallback as oe, useEffect as be, useMemo as Dt, useImperativeHandle as bn } from "react";
1
+ import { jsxs as kn, jsx as It } from "react/jsx-runtime";
2
+ import { forwardRef as vn, useRef as F, useState as Le, useCallback as oe, useEffect as ke, useMemo as Rt, useImperativeHandle as bn } from "react";
3
3
  import { PipecatClient as Tn } from "@pipecat-ai/client-js";
4
4
  import { WebSocketTransport as An, DailyMediaManager as In } from "@pipecat-ai/websocket-transport";
5
- const Rn = "https://pub-607ad1fc22e2400eb57d17240aab857c.r2.dev", Qt = {
5
+ const Rn = "https://pub-607ad1fc22e2400eb57d17240aab857c.r2.dev", Jt = {
6
6
  publicBase: Rn
7
- }, Dn = "0.2.5", Cn = {
7
+ }, Dn = "0.2.6", Mn = {
8
8
  version: Dn
9
- }, Mn = {}, Ae = "https://ai.streamoji.com", ft = `${Qt.publicBase}/custom-faces`, xn = `${Qt.publicBase}/aitwin-workers/v${Cn.version}/visemeDiffPreview.worker.js`;
9
+ }, Cn = {}, be = "https://ai.streamoji.com", ut = `${Jt.publicBase}/custom-faces`, xn = `${Jt.publicBase}/aitwin-workers/v${Mn.version}/visemeDiffPreview.worker.js`;
10
10
  function Pn(e) {
11
11
  if (e) return e;
12
- const t = typeof import.meta < "u" ? Mn?.VITE_AITWIN_WORKER_URL : void 0;
12
+ const t = typeof import.meta < "u" ? Cn?.VITE_AITWIN_WORKER_URL : void 0;
13
13
  return t?.trim() ? t.trim() : xn;
14
14
  }
15
- const Bn = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin", Xt = "eng_a7f2b9c1", dt = "eng_d4e8f3a2", Re = "eng_c9b1e6d4", Ie = Re, Ln = /* @__PURE__ */ new Set([
16
- dt,
17
- Re
18
- ]), Zt = {
19
- [Xt]: "oculus_direct",
20
- [Re]: "oculus_direct",
21
- [dt]: "inworld_word"
22
- }, Ct = {
23
- google: Xt,
24
- inworld: dt,
25
- cartesia: Re
15
+ const Bn = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin", Qt = "eng_a7f2b9c1", lt = "eng_d4e8f3a2", Ae = "eng_c9b1e6d4", Te = Ae, Ln = /* @__PURE__ */ new Set([
16
+ lt,
17
+ Ae
18
+ ]), Xt = {
19
+ [Qt]: "oculus_direct",
20
+ [Ae]: "oculus_direct",
21
+ [lt]: "inworld_word"
22
+ }, Dt = {
23
+ google: Qt,
24
+ inworld: lt,
25
+ cartesia: Ae
26
26
  };
27
27
  function On(e) {
28
- return Zt[e];
28
+ return Xt[e];
29
29
  }
30
30
  function Fn(e) {
31
- return e === Re;
31
+ return e === Ae;
32
32
  }
33
33
  function Un(e) {
34
- if (e.ttsEngineId && e.ttsEngineId in Zt)
34
+ if (e.ttsEngineId && e.ttsEngineId in Xt)
35
35
  return e.ttsEngineId;
36
36
  const t = e.tts?.toLowerCase();
37
- return t && t in Ct ? Ct[t] : Ie;
37
+ return t && t in Dt ? Dt[t] : Te;
38
38
  }
39
- class st extends Error {
39
+ class nt extends Error {
40
40
  constructor(t = "AI twin not found") {
41
41
  super(t), this.name = "AiTwinNotFoundError";
42
42
  }
@@ -46,12 +46,12 @@ async function Nn(e, t = Bn) {
46
46
  n.searchParams.set("id", e);
47
47
  const r = await fetch(n.toString());
48
48
  if (r.status === 404)
49
- throw new st();
49
+ throw new nt();
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 st(s.error ?? "AI twin not found");
54
+ throw new nt(s.error ?? "AI twin not found");
55
55
  const { tts: i, ...o } = s.data;
56
56
  return {
57
57
  ...o,
@@ -59,7 +59,7 @@ async function Nn(e, t = Bn) {
59
59
  };
60
60
  }
61
61
  const Vn = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken", $n = "client_6TNvp3SCs4Og0a1ijm9TommXLql1", Wn = "kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1", Hn = "6TNvp3SCs4Og0a1ijm9TommXLql1", qn = "Swaraj Mali";
62
- async function nt() {
62
+ async function et() {
63
63
  const e = await fetch(Vn, {
64
64
  method: "POST",
65
65
  headers: {
@@ -79,7 +79,7 @@ async function nt() {
79
79
  throw new Error("getAuthToken returned no token");
80
80
  return n;
81
81
  }
82
- async function Kn(e, t = Ae) {
82
+ async function Kn(e, t = be) {
83
83
  const n = await fetch(`${t.replace(/\/$/, "")}/api/session-value`, {
84
84
  headers: { Authorization: `Bearer ${e}` }
85
85
  });
@@ -97,7 +97,7 @@ function jn(e) {
97
97
  n[r / 2] = Number.parseInt(t.slice(r, r + 2), 16);
98
98
  return n;
99
99
  }
100
- function Ne(e) {
100
+ function Ve(e) {
101
101
  return Uint8Array.from(e);
102
102
  }
103
103
  function Gn(e, t) {
@@ -108,7 +108,7 @@ async function zn(e, t) {
108
108
  if (!t) throw new Error("Asset key is required for encrypted assets");
109
109
  if (e.byteLength < 28)
110
110
  throw new Error("Encrypted payload too small");
111
- const n = new Uint8Array(e), r = Ne(n.subarray(0, 12)), s = Ne(n.subarray(12, 28)), i = Ne(n.subarray(28)), o = Ne(Gn(i, s)), a = await crypto.subtle.importKey(
111
+ const n = new Uint8Array(e), r = Ve(n.subarray(0, 12)), s = Ve(n.subarray(12, 28)), i = Ve(n.subarray(28)), o = Ve(Gn(i, s)), a = await crypto.subtle.importKey(
112
112
  "raw",
113
113
  jn(t),
114
114
  { name: "AES-GCM" },
@@ -121,22 +121,22 @@ async function zn(e, t) {
121
121
  o
122
122
  );
123
123
  }
124
- let it = null;
125
- const ot = /* @__PURE__ */ new Set();
126
- function Ve(e) {
124
+ let rt = null;
125
+ const st = /* @__PURE__ */ new Set();
126
+ function $e(e) {
127
127
  return e.includes("/custom-faces/");
128
128
  }
129
- function He(e) {
129
+ function qe(e) {
130
130
  const t = e.binBase.trim().replace(/\/$/, ""), n = e.twinBase.trim().replace(/\/$/, "");
131
- if (!(e.encrypted || Ve(t) || Ve(n)))
131
+ if (!(e.encrypted || $e(t) || $e(n)))
132
132
  return { twinBase: n, binBase: t, encrypted: !1 };
133
- const s = Ve(t) ? t : Ve(n) ? n : t;
133
+ const s = $e(t) ? t : $e(n) ? n : t;
134
134
  return { twinBase: s, binBase: s, encrypted: !0 };
135
135
  }
136
- function De(e) {
137
- return He(e).encrypted;
136
+ function Ie(e) {
137
+ return qe(e).encrypted;
138
138
  }
139
- function Mt(e, t = ft) {
139
+ function Mt(e, t = ut) {
140
140
  const n = `${t.replace(/\/$/, "")}/${e}`;
141
141
  return { twinBase: n, binBase: n, encrypted: !0 };
142
142
  }
@@ -144,12 +144,12 @@ function Yn(e, t) {
144
144
  return (e ?? t)?.trim() || void 0;
145
145
  }
146
146
  function Jn(e) {
147
- it = He(e);
148
- for (const t of ot) t();
147
+ rt = qe(e);
148
+ for (const t of st) t();
149
149
  }
150
150
  function Qn(e, t) {
151
151
  const n = typeof globalThis.location < "u" ? globalThis.location.href : void 0;
152
- if (!n) return He(e);
152
+ if (!n) return qe(e);
153
153
  const r = (s) => {
154
154
  try {
155
155
  return new URL(s, n).href.replace(/\/$/, "");
@@ -157,42 +157,42 @@ function Qn(e, t) {
157
157
  return s;
158
158
  }
159
159
  };
160
- return He({
160
+ return qe({
161
161
  twinBase: r(e.twinBase),
162
162
  binBase: r(e.binBase),
163
163
  encrypted: e.encrypted
164
164
  });
165
165
  }
166
- function Se() {
167
- if (!it)
166
+ function Ee() {
167
+ if (!rt)
168
168
  throw new Error("Twin assets not configured; call setActiveTwinAssets first");
169
- return it;
169
+ return rt;
170
170
  }
171
171
  function Xn(e) {
172
- return ot.add(e), () => ot.delete(e);
172
+ return st.add(e), () => st.delete(e);
173
173
  }
174
- function xt() {
175
- const e = Se();
176
- return De(e) ? `${e.binBase}/sil.bin` : `${e.twinBase}/sil.png`;
174
+ function Ct() {
175
+ const e = Ee();
176
+ return Ie(e) ? `${e.binBase}/sil.bin` : `${e.twinBase}/sil.png`;
177
177
  }
178
178
  function Zn() {
179
- const e = Se();
180
- return De(e) ? `${e.binBase}/idle.bin` : `${e.twinBase}/idle.mp4`;
179
+ const e = Ee();
180
+ return Ie(e) ? `${e.binBase}/idle.bin` : `${e.twinBase}/idle.mp4`;
181
181
  }
182
182
  function er() {
183
- const e = Se();
184
- return De(e) ? e.binBase : e.twinBase;
183
+ const e = Ee();
184
+ return Ie(e) ? e.binBase : e.twinBase;
185
185
  }
186
- function je() {
187
- return De(Se());
186
+ function Ge() {
187
+ return Ie(Ee());
188
188
  }
189
189
  function tr() {
190
- const e = Se();
191
- return De(e) ? `${e.binBase}/atlas.bin` : `${e.twinBase}/atlas.json`;
190
+ const e = Ee();
191
+ return Ie(e) ? `${e.binBase}/atlas.bin` : `${e.twinBase}/atlas.json`;
192
192
  }
193
193
  function nr() {
194
- const e = Se();
195
- return De(e) ? `${e.binBase}/expression_atlas.bin` : `${e.twinBase}/expression_atlas.json`;
194
+ const e = Ee();
195
+ return Ie(e) ? `${e.binBase}/expression_atlas.bin` : `${e.twinBase}/expression_atlas.json`;
196
196
  }
197
197
  function rr(e) {
198
198
  const t = e.split(/\r?\n/);
@@ -257,12 +257,12 @@ function ur(e, t) {
257
257
  const s = Math.max(0, Math.min(1, (t - e.wtime) / r));
258
258
  return Math.min(n - 1, Math.floor(s * n));
259
259
  }
260
- function Ge(e, t) {
260
+ function ze(e, t) {
261
261
  const n = Math.max(0, Math.min(e.length - 1, t));
262
262
  return e[n] ?? "";
263
263
  }
264
264
  function lr(e, t) {
265
- const n = e.toLowerCase(), r = Ge(n, t), s = n.slice(Math.max(0, t - 1), t + 2);
265
+ const n = e.toLowerCase(), r = ze(n, t), s = n.slice(Math.max(0, t - 1), t + 2);
266
266
  return r === "i" || r === "y" ? "I" : s.includes("ee") || r === "e" || n.includes("ea") ? "E" : "aa";
267
267
  }
268
268
  function fr(e, t) {
@@ -271,7 +271,7 @@ function fr(e, t) {
271
271
  Math.min(n.length, t + 3)
272
272
  );
273
273
  if (/oo|ou|uw/.test(r)) return "U";
274
- const s = Ge(n, t);
274
+ const s = ze(n, t);
275
275
  return s === "u" || s === "w" ? "U" : "O";
276
276
  }
277
277
  function dr(e, t) {
@@ -281,9 +281,9 @@ function dr(e, t) {
281
281
  Math.max(0, t - 1),
282
282
  Math.min(n.length, t + 3)
283
283
  );
284
- return /wo|wh/.test(s) || Ge(n, t) === "o" ? "O" : "U";
284
+ return /wo|wh/.test(s) || ze(n, t) === "o" ? "O" : "U";
285
285
  }
286
- const $e = {
286
+ const We = {
287
287
  d: "DD",
288
288
  t: "DD",
289
289
  n: "DD",
@@ -296,11 +296,11 @@ const $e = {
296
296
  j: "CH"
297
297
  };
298
298
  function hr(e, t) {
299
- const n = e.toLowerCase(), r = Ge(n, t);
300
- if ($e[r]) return $e[r];
299
+ const n = e.toLowerCase(), r = ze(n, t);
300
+ if (We[r]) return We[r];
301
301
  for (let s = 0; s < n.length; s++) {
302
302
  const i = n[s];
303
- if ($e[i]) return $e[i];
303
+ if (We[i]) return We[i];
304
304
  }
305
305
  return "DD";
306
306
  }
@@ -334,7 +334,7 @@ function gr(e) {
334
334
  function _r(e) {
335
335
  return e.length >= 4 && e[0] === 82 && e[1] === 73 && e[2] === 70 && e[3] === 70;
336
336
  }
337
- function Pt(e, t, n = wr) {
337
+ function xt(e, t, n = wr) {
338
338
  const r = t.byteOffset % 2 === 0 ? t : t.slice(), s = new Int16Array(
339
339
  r.buffer,
340
340
  r.byteOffset,
@@ -361,7 +361,7 @@ async function yr(e, t) {
361
361
  return e.decodeAudioData(r);
362
362
  }
363
363
  if (n.length >= 2 && n.length % 2 === 0)
364
- return Pt(e, n);
364
+ return xt(e, n);
365
365
  try {
366
366
  const r = n.buffer.slice(
367
367
  n.byteOffset,
@@ -369,10 +369,10 @@ async function yr(e, t) {
369
369
  );
370
370
  return await e.decodeAudioData(r);
371
371
  } catch {
372
- return Pt(e, n);
372
+ return xt(e, n);
373
373
  }
374
374
  }
375
- function Bt(e) {
375
+ function Pt(e) {
376
376
  let t = 0;
377
377
  for (const n of e)
378
378
  t = Math.max(t, n.vtime + n.vduration);
@@ -384,11 +384,11 @@ function Er(e, t) {
384
384
  return n;
385
385
  return null;
386
386
  }
387
- function en(e, t) {
387
+ function Zt(e, t) {
388
388
  const n = [], r = [];
389
- let s = null, i = !1, o = !1, a = !1, f = !1, l = 0, w = 0, _ = 0, m = 0, v = null, k = 1, u = t, p = !1;
389
+ let s = null, i = !1, o = !1, a = !1, f = !1, l = 0, w = 0, _ = 0, m = 0, k = null, S = 1, u = t, p = !1;
390
390
  const d = () => {
391
- v != null && (clearTimeout(v), v = null);
391
+ k != null && (clearTimeout(k), k = null);
392
392
  }, I = () => {
393
393
  for (const g of r)
394
394
  try {
@@ -396,73 +396,73 @@ function en(e, t) {
396
396
  } catch {
397
397
  }
398
398
  r.length = 0;
399
- }, S = () => {
399
+ }, E = () => {
400
400
  for (const g of r)
401
- g.playbackRate.value = k;
401
+ g.playbackRate.value = S;
402
402
  }, R = () => {
403
403
  !f || i || r.length > 0 || n.length > 0 || (o = !1, d(), e.onSpeakingDone(), e.onNotify());
404
- }, q = (g) => {
405
- const M = Bt(g);
406
- if (M <= 0) {
404
+ }, H = (g) => {
405
+ const C = Pt(g);
406
+ if (C <= 0) {
407
407
  e.onSpeakingDone(), e.onNotify();
408
408
  return;
409
409
  }
410
410
  o = !0, l = performance.now(), e.onSpeakingStart(), e.onNotify(), d();
411
- const b = k > 0 ? M / k : M;
412
- v = setTimeout(() => {
413
- p || (o = !1, v = null, e.onSpeakingDone(), e.onNotify());
411
+ const b = S > 0 ? C / S : C;
412
+ k = setTimeout(() => {
413
+ p || (o = !1, k = null, e.onSpeakingDone(), e.onNotify());
414
414
  }, b);
415
- }, N = (g, M, b, D) => {
416
- M.length !== 0 && (D && (_ = b), M.forEach((A) => {
417
- const O = String(A.word ?? "").trim();
418
- if (!O) return;
419
- const W = Math.round((A.start ?? 0) * 1e3), G = A.duration != null ? Math.round(A.duration * 1e3) : A.end != null ? Math.max(1, Math.round((A.end - (A.start ?? 0)) * 1e3)) : 0;
415
+ }, V = (g, C, b, D) => {
416
+ C.length !== 0 && (D && (_ = b), C.forEach((A) => {
417
+ const B = String(A.word ?? "").trim();
418
+ if (!B) return;
419
+ const W = Math.round((A.start ?? 0) * 1e3), U = A.duration != null ? Math.round(A.duration * 1e3) : A.end != null ? Math.max(1, Math.round((A.end - (A.start ?? 0)) * 1e3)) : 0;
420
420
  g.push({
421
- word: O,
421
+ word: B,
422
422
  wtime: _ + W,
423
- wduration: G,
423
+ wduration: U,
424
424
  queueIndex: g.length,
425
425
  gesture: A.gesture ?? null,
426
426
  emphasis: A.emphasis === !0
427
427
  });
428
428
  }));
429
- }, $ = (g, M, b, D, A, P) => {
430
- b.length !== 0 && (A && (_ = D), b.forEach((O) => {
431
- const W = String(O.symbol ?? O.viseme ?? "").trim();
429
+ }, $ = (g, C, b, D, A, P) => {
430
+ b.length !== 0 && (A && (_ = D), b.forEach((B) => {
431
+ const W = String(B.symbol ?? B.viseme ?? "").trim();
432
432
  if (!W) return;
433
- const G = Math.round((O.start ?? 0) * 1e3), H = Math.max(1, Math.round((O.duration ?? 0) * 1e3)), Y = _ + G;
434
- let J;
433
+ const U = Math.round((B.start ?? 0) * 1e3), X = Math.max(1, Math.round((B.duration ?? 0) * 1e3)), se = _ + U;
434
+ let Z;
435
435
  if (On(P) === "oculus_direct")
436
- J = or(W);
436
+ Z = or(W);
437
437
  else {
438
- const fe = Er(M, Y), ee = fe ? ur(fe, Y) : 0;
439
- J = mr(
438
+ const ce = Er(C, se), J = ce ? ur(ce, se) : 0;
439
+ Z = mr(
440
440
  W,
441
- fe?.word ?? "",
442
- ee
441
+ ce?.word ?? "",
442
+ J
443
443
  );
444
444
  }
445
445
  g.push({
446
- viseme: J,
446
+ viseme: Z,
447
447
  weight: 1,
448
- vtime: Y,
449
- vduration: H
448
+ vtime: se,
449
+ vduration: X
450
450
  });
451
451
  }), e.onNotify());
452
- }, z = (g, M, b, D, A, P, O) => {
453
- N(M, D, A, P), $(
452
+ }, j = (g, C, b, D, A, P, B) => {
453
+ V(C, D, A, P), $(
454
454
  g,
455
- M,
455
+ C,
456
456
  b,
457
457
  A,
458
458
  P,
459
- O
459
+ B
460
460
  );
461
- }, y = async (g, M, b, D, A, P, O) => {
461
+ }, y = async (g, C, b, D, A, P, B) => {
462
462
  if (p || !b?.trim()) {
463
- (D.length > 0 || A.length > 0) && !a && z(
463
+ (D.length > 0 || A.length > 0) && !a && j(
464
464
  g,
465
- M,
465
+ C,
466
466
  D,
467
467
  A,
468
468
  _,
@@ -484,32 +484,32 @@ function en(e, t) {
484
484
  try {
485
485
  const W = window.AudioContext || window.webkitAudioContext;
486
486
  s || (s = new W());
487
- const G = s;
488
- G.state === "suspended" && await G.resume();
489
- const H = await yr(G, b);
487
+ const U = s;
488
+ U.state === "suspended" && await U.resume();
489
+ const X = await yr(U, b);
490
490
  a = !0;
491
- const Y = G.currentTime;
492
- let J = m;
493
- const fe = !o;
494
- J < Y && (J = Y + 0.1), m = J + H.duration;
495
- const ee = G.createBufferSource();
496
- if (ee.buffer = H, ee.playbackRate.value = k, ee.connect(G.destination), r.push(ee), p) {
491
+ const se = U.currentTime;
492
+ let Z = m;
493
+ const ce = !o;
494
+ Z < se && (Z = se + 0.1), m = Z + X.duration;
495
+ const J = U.createBufferSource();
496
+ if (J.buffer = X, J.playbackRate.value = S, J.connect(U.destination), r.push(J), p) {
497
497
  r.pop();
498
498
  return;
499
499
  }
500
- if (fe) {
501
- o = !0, e.onSpeakingStart(), w = J, _ = 0, g.length = 0, M.length = 0, O?.();
502
- const E = (J - Y) * 1e3;
503
- l = performance.now() + E;
500
+ if (ce) {
501
+ o = !0, e.onSpeakingStart(), w = Z, _ = 0, g.length = 0, C.length = 0, B?.();
502
+ const v = (Z - se) * 1e3;
503
+ l = performance.now() + v;
504
504
  }
505
- ee.onended = () => {
506
- const E = r.indexOf(ee);
507
- E >= 0 && r.splice(E, 1), R(), e.onNotify();
508
- }, ee.start(J);
509
- const c = (J - w) * 1e3;
510
- z(
505
+ J.onended = () => {
506
+ const v = r.indexOf(J);
507
+ v >= 0 && r.splice(v, 1), R(), e.onNotify();
508
+ }, J.start(Z);
509
+ const c = (Z - w) * 1e3;
510
+ j(
511
511
  g,
512
- M,
512
+ C,
513
513
  D,
514
514
  A,
515
515
  c,
@@ -523,12 +523,12 @@ function en(e, t) {
523
523
  const W = n.shift();
524
524
  W ? await y(
525
525
  g,
526
- M,
526
+ C,
527
527
  W.audio,
528
528
  W.visemes,
529
529
  W.words,
530
530
  W.isNewSegment,
531
- O
531
+ B
532
532
  ) : R();
533
533
  }
534
534
  };
@@ -537,9 +537,9 @@ function en(e, t) {
537
537
  u = g;
538
538
  },
539
539
  setPlaybackSpeed: (g) => {
540
- k = Math.max(0.1, Math.min(1, g)), S();
540
+ S = Math.max(0.1, Math.min(1, g)), E();
541
541
  },
542
- getPlaybackSpeed: () => k,
542
+ getPlaybackSpeed: () => S,
543
543
  resetForNewStream: (g) => {
544
544
  p = !1, f = !1, u = g;
545
545
  },
@@ -553,17 +553,17 @@ function en(e, t) {
553
553
  },
554
554
  markStreamComplete: (g) => {
555
555
  if (f = !0, !a && g.length > 0) {
556
- q(g);
556
+ H(g);
557
557
  return;
558
558
  }
559
559
  R();
560
560
  },
561
561
  playAudioChunk: y,
562
- queueTimingsOnly: (g, M, b, D, A) => {
562
+ queueTimingsOnly: (g, C, b, D, A) => {
563
563
  let P = _;
564
- A && g.length > 0 && (P = Bt(g)), z(
564
+ A && g.length > 0 && (P = Pt(g)), j(
565
565
  g,
566
- M,
566
+ C,
567
567
  b,
568
568
  D,
569
569
  P,
@@ -574,7 +574,7 @@ function en(e, t) {
574
574
  getPlaybackElapsedMs: () => o ? a && s && w > 0 ? Math.max(
575
575
  0,
576
576
  (s.currentTime - w) * 1e3
577
- ) : Math.max(0, (performance.now() - l) * k) : 0,
577
+ ) : Math.max(0, (performance.now() - l) * S) : 0,
578
578
  isSpeaking: () => o,
579
579
  getPlaybackStartPerf: () => l,
580
580
  /** User spoke over the bot — drop in-flight chunks until the next utterance. */
@@ -586,101 +586,90 @@ function en(e, t) {
586
586
  }
587
587
  };
588
588
  }
589
- function ze(e, t) {
589
+ function ft(e, t) {
590
590
  let n = null;
591
591
  for (const r of e)
592
592
  t < r.vtime || t >= r.vtime + r.vduration || (!n || r.vtime >= n.vtime) && (n = r);
593
593
  return n ? n.viseme : "sil";
594
594
  }
595
- function Sr(e, t, n = 120) {
596
- const r = ze(e, t);
597
- if (r !== "sil") return r;
598
- let s = null;
599
- for (const i of e) {
600
- if (i.vtime > t) break;
601
- const o = i.vtime + i.vduration;
602
- o <= t && t - o <= n && (s = i);
603
- }
604
- return s?.viseme ?? "sil";
605
- }
606
- function zs(e, t) {
595
+ function js(e, t) {
607
596
  for (const n of e)
608
597
  if (t >= n.wtime && t < n.wtime + n.wduration)
609
598
  return n;
610
599
  return null;
611
600
  }
612
- const tn = 0.5, nn = 1.5, rn = "Olivia", kr = "f786b574-daa5-4673-aa0c-cbe3e8534c02", ht = 0.85;
613
- function Lt(e, t = Ae) {
601
+ const en = 0.5, tn = 1.5, nn = "Olivia", Sr = "f786b574-daa5-4673-aa0c-cbe3e8534c02", dt = 0.85;
602
+ function Bt(e, t = be) {
614
603
  const n = `${t.replace(/\/$/, "")}/avatar_ttsWithPoses`, r = [], s = [], i = [];
615
- let o = "neutral", a = "", f = Ie;
616
- const l = /* @__PURE__ */ new Set(), w = () => l.forEach((k) => k()), _ = en(
604
+ let o = "neutral", a = "", f = Te;
605
+ const l = /* @__PURE__ */ new Set(), w = () => l.forEach((S) => S()), _ = Zt(
617
606
  {
618
607
  onSpeakingStart: () => e("speaking"),
619
608
  onSpeakingDone: () => e("done"),
620
609
  onNotify: w
621
610
  },
622
- Ie
611
+ Te
623
612
  ), m = () => {
624
613
  _.stop(), r.length = 0, s.length = 0, i.length = 0, o = "neutral", e("idle"), w();
625
614
  };
626
615
  return {
627
- setDeveloperToken: (k) => {
628
- a = k;
616
+ setDeveloperToken: (S) => {
617
+ a = S;
629
618
  },
630
- setTtsEngineId: (k) => {
631
- f = k;
619
+ setTtsEngineId: (S) => {
620
+ f = S;
632
621
  },
633
622
  getTtsEngineId: () => f,
634
- speak: async (k, u) => {
623
+ speak: async (S, u) => {
635
624
  m(), e("loading");
636
625
  const p = a.trim();
637
626
  if (!p)
638
627
  throw new Error("Developer token required for avatar_ttsWithPoses");
639
- const d = u?.ttsEngineId ?? f ?? Ie;
628
+ const d = u?.ttsEngineId ?? f ?? Te;
640
629
  _.resetForNewStream(d), _.setStreamTtsEngineId(d);
641
- const I = u?.voiceId ?? rn, S = u?.speakingRate ?? ht, R = Math.max(
642
- tn,
643
- Math.min(nn, S)
644
- ), q = {
645
- user_query: k,
630
+ const I = u?.voiceId ?? nn, E = u?.speakingRate ?? dt, R = Math.max(
631
+ en,
632
+ Math.min(tn, E)
633
+ ), H = {
634
+ user_query: S,
646
635
  ttsEngineId: d,
647
636
  speakingRate: R,
648
637
  tenant: "aiTwin"
649
638
  };
650
- Ln.has(d) && (q.voice_id = I);
639
+ Ln.has(d) && (H.voice_id = I);
651
640
  try {
652
- const N = await fetch(n, {
641
+ const V = await fetch(n, {
653
642
  method: "POST",
654
643
  headers: {
655
644
  "Content-Type": "application/json",
656
645
  Authorization: `Bearer ${p}`
657
646
  },
658
- body: JSON.stringify(q)
647
+ body: JSON.stringify(H)
659
648
  });
660
- if (!N.ok)
661
- throw new Error(`avatar_ttsWithPoses failed (${N.status})`);
662
- const $ = N.body;
649
+ if (!V.ok)
650
+ throw new Error(`avatar_ttsWithPoses failed (${V.status})`);
651
+ const $ = V.body;
663
652
  if (!$) throw new Error("No response body");
664
- const z = $.getReader(), y = new TextDecoder();
653
+ const j = $.getReader(), y = new TextDecoder();
665
654
  let T = "";
666
- const g = async (M) => {
667
- const b = rr(M);
655
+ const g = async (C) => {
656
+ const b = rr(C);
668
657
  if (b) {
669
658
  if (b.event === "audio") {
670
- const D = b.data.chunk, A = b.data.visemes ?? [], P = b.data.words ?? [], O = b.data.is_new_segment ?? !1;
659
+ const D = b.data.chunk, A = b.data.visemes ?? [], P = b.data.words ?? [], B = b.data.is_new_segment ?? !1;
671
660
  D ? await _.playAudioChunk(
672
661
  r,
673
662
  s,
674
663
  D,
675
664
  A,
676
665
  P,
677
- O
666
+ B
678
667
  ) : (A.length > 0 || P.length > 0) && _.queueTimingsOnly(
679
668
  r,
680
669
  s,
681
670
  A,
682
671
  P,
683
- O
672
+ B
684
673
  );
685
674
  } else if (b.event === "metadata") {
686
675
  const D = b.data.mood;
@@ -690,14 +679,14 @@ function Lt(e, t = Ae) {
690
679
  i.length = 0;
691
680
  for (const P of A) {
692
681
  if (!P || typeof P != "object") continue;
693
- const O = P;
682
+ const B = P;
694
683
  i.push({
695
- sentence_index: Number(O.sentence_index ?? 0),
696
- text: String(O.text ?? ""),
697
- sentiment: typeof O.sentiment == "string" ? O.sentiment : void 0,
698
- emotion: String(O.emotion ?? o),
699
- start_word: Number(O.start_word ?? 0),
700
- end_word: Number(O.end_word ?? 0)
684
+ sentence_index: Number(B.sentence_index ?? 0),
685
+ text: String(B.text ?? ""),
686
+ sentiment: typeof B.sentiment == "string" ? B.sentiment : void 0,
687
+ emotion: String(B.emotion ?? o),
688
+ start_word: Number(B.start_word ?? 0),
689
+ end_word: Number(B.end_word ?? 0)
701
690
  });
702
691
  }
703
692
  }
@@ -709,20 +698,20 @@ function Lt(e, t = Ae) {
709
698
  }
710
699
  };
711
700
  for (; ; ) {
712
- const { done: M, value: b } = await z.read();
701
+ const { done: C, value: b } = await j.read();
713
702
  b && (T += y.decode(b, { stream: !0 }));
714
703
  const D = T.split(`
715
704
 
716
705
  `);
717
706
  T = D.pop() ?? "";
718
707
  for (const A of D) await g(A);
719
- if (M) {
708
+ if (C) {
720
709
  T.trim() && await g(T.trim()), _.markStreamComplete(r);
721
710
  break;
722
711
  }
723
712
  }
724
- } catch (N) {
725
- throw console.error("[avatarTtsLipsync]", N), m(), e("error"), N;
713
+ } catch (V) {
714
+ throw console.error("[avatarTtsLipsync]", V), m(), e("error"), V;
726
715
  }
727
716
  },
728
717
  stop: m,
@@ -732,15 +721,15 @@ function Lt(e, t = Ae) {
732
721
  getStreamMood: () => o,
733
722
  getPlaybackElapsedMs: () => _.getPlaybackElapsedMs(),
734
723
  isSpeaking: () => _.isSpeaking(),
735
- setPlaybackSpeed: (k) => {
736
- _.setPlaybackSpeed(k);
724
+ setPlaybackSpeed: (S) => {
725
+ _.setPlaybackSpeed(S);
737
726
  },
738
727
  getPlaybackSpeed: () => _.getPlaybackSpeed(),
739
- subscribe: (k) => (l.add(k), () => l.delete(k))
728
+ subscribe: (S) => (l.add(S), () => l.delete(S))
740
729
  };
741
730
  }
742
- const sn = 16e3, on = 24e3;
743
- class vr extends In {
731
+ const rn = 16e3, sn = 24e3;
732
+ class kr extends In {
744
733
  constructor() {
745
734
  super(
746
735
  !0,
@@ -748,40 +737,40 @@ class vr extends In {
748
737
  void 0,
749
738
  void 0,
750
739
  512,
751
- sn,
752
- on
740
+ rn,
741
+ sn
753
742
  );
754
743
  }
755
744
  /** Pipecat protobuf bot audio is unused — playback comes from avatar_audio_chunk. */
756
745
  bufferBotAudio(t, n) {
757
746
  }
758
747
  }
759
- const br = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
760
- function Tr(e) {
761
- return br.test(e.trim());
748
+ const vr = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
749
+ function br(e) {
750
+ return vr.test(e.trim());
762
751
  }
763
- function Ar(e) {
752
+ function Tr(e) {
764
753
  const t = e.replace(/\/$/, "");
765
754
  return t.startsWith("https://") ? `wss://${t.slice(8)}` : t.startsWith("http://") ? `ws://${t.slice(7)}` : t;
766
755
  }
767
- function Ir(e) {
756
+ function Ar(e) {
768
757
  if (e.wsUrl?.trim()) return e.wsUrl.trim();
769
758
  const t = e.authToken?.trim();
770
759
  if (!t)
771
760
  throw new Error(
772
761
  "Voice connect requires authToken (same JWT as SSE Authorization Bearer)."
773
762
  );
774
- const n = e.apiBase ?? Ae, r = new URL(`${Ar(n)}/ws/voice`);
763
+ const n = e.apiBase ?? be, r = new URL(`${Tr(n)}/ws/voice`);
775
764
  r.searchParams.set("authToken", t);
776
765
  const s = e.voiceId?.trim();
777
- s && Tr(s) && r.searchParams.set("voiceId", s);
778
- const i = e.speakingRate ?? ht;
766
+ s && br(s) && r.searchParams.set("voiceId", s);
767
+ const i = e.speakingRate ?? dt;
779
768
  return r.searchParams.set(
780
769
  "speaking_rate",
781
- String(Math.max(tn, Math.min(nn, i)))
770
+ String(Math.max(en, Math.min(tn, i)))
782
771
  ), e.ttsStream === !1 ? r.searchParams.set("tts_stream", "false") : r.searchParams.set("tts_stream", "true"), r.toString();
783
772
  }
784
- function Ot(e, t) {
773
+ function Lt(e, t) {
785
774
  if (!Array.isArray(e)) return [];
786
775
  const n = [];
787
776
  for (const r of e)
@@ -795,116 +784,116 @@ function Ot(e, t) {
795
784
  });
796
785
  return n;
797
786
  }
798
- function Rr(e) {
787
+ function Ir(e) {
799
788
  if (!e || typeof e != "object") return null;
800
789
  const t = e, n = t.data;
801
790
  return n && typeof n == "object" ? n : t;
802
791
  }
803
- function Dr(e, t = {}) {
792
+ function Rr(e, t = {}) {
804
793
  const n = [], r = [], s = [], i = [];
805
- let o = "", a = "neutral", f = Re, l = !1, w = null, _ = null, m = { ...t }, v = 0, k = null;
794
+ let o = "", a = "neutral", f = Ae, l = !1, w = null, _ = null, m = { ...t }, k = 0, S = null;
806
795
  const u = /* @__PURE__ */ new Set(), p = [];
807
796
  let d = !1, I = null;
808
- const S = () => u.forEach((c) => c()), R = en(
797
+ const E = () => u.forEach((c) => c()), R = Zt(
809
798
  {
810
799
  onSpeakingStart: () => e("speaking"),
811
800
  onSpeakingDone: () => e("done"),
812
- onNotify: S
801
+ onNotify: E
813
802
  },
814
803
  f
815
- ), q = () => {
804
+ ), H = () => {
816
805
  if (I) return I;
817
806
  const c = new MessageChannel();
818
807
  return c.port1.onmessage = () => {
819
- const E = p.shift();
820
- E !== void 0 && W(E), p.length > 0 ? c.port2.postMessage(null) : d = !1;
808
+ const v = p.shift();
809
+ v !== void 0 && W(v), p.length > 0 ? c.port2.postMessage(null) : d = !1;
821
810
  }, I = c, c;
822
- }, N = (c) => {
823
- p.push(c), !d && (d = !0, q().port2.postMessage(null));
824
- }, $ = () => R.getPlaybackElapsedMs(), z = (c, E = o) => {
811
+ }, V = (c) => {
812
+ p.push(c), !d && (d = !0, H().port2.postMessage(null));
813
+ }, $ = () => R.getPlaybackElapsedMs(), j = (c, v = o) => {
825
814
  if (!m.onLipsyncDebug) return;
826
- const B = $(), Q = n;
815
+ const L = $(), z = n;
827
816
  m.onLipsyncDebug({
828
817
  event: c,
829
- spokenText: E,
818
+ spokenText: v,
830
819
  mood: a,
831
820
  playbackAnchorPerf: R.getPlaybackStartPerf(),
832
- playbackElapsedMs: B,
833
- queueLength: Q.length,
834
- activeViseme: ze(Q, B),
835
- segments: i.map((te) => ({
836
- ...te,
837
- visemes: [...te.visemes]
821
+ playbackElapsedMs: L,
822
+ queueLength: z.length,
823
+ activeViseme: ft(z, L),
824
+ segments: i.map((ne) => ({
825
+ ...ne,
826
+ visemes: [...ne.visemes]
838
827
  }))
839
828
  });
840
- }, y = (c, E) => {
841
- m.onLipsyncDebug && (k = { event: c, spokenText: E }, v === 0 && (v = requestAnimationFrame(() => {
842
- v = 0;
843
- const B = k;
844
- k = null, B && z(B.event, B.spokenText);
829
+ }, y = (c, v) => {
830
+ m.onLipsyncDebug && (S = { event: c, spokenText: v }, k === 0 && (k = requestAnimationFrame(() => {
831
+ k = 0;
832
+ const L = S;
833
+ S = null, L && j(L.event, L.spokenText);
845
834
  })));
846
835
  }, T = () => {
847
836
  n.length = 0, r.length = 0, i.length = 0, o = "", a = "neutral", s.length = 0;
848
837
  }, g = () => {
849
- R.stop(), T(), y("reset"), S();
850
- }, M = () => {
851
- g(), e("idle"), S();
852
- }, b = (c, E) => {
853
- typeof c.mood == "string" && c.mood.trim() && (a = c.mood.trim()), Array.isArray(c.sentence_emotions) && c.sentence_emotions.length > 0 && (E && (s.length = 0), s.push(
854
- ...Ot(c.sentence_emotions, a)
838
+ R.stop(), T(), y("reset"), E();
839
+ }, C = () => {
840
+ g(), e("idle"), E();
841
+ }, b = (c, v) => {
842
+ typeof c.mood == "string" && c.mood.trim() && (a = c.mood.trim()), Array.isArray(c.sentence_emotions) && c.sentence_emotions.length > 0 && (v && (s.length = 0), s.push(
843
+ ...Lt(c.sentence_emotions, a)
855
844
  ));
856
845
  }, D = (c) => {
857
- const E = String(c.spoken_text ?? c.text ?? "").trim();
858
- E && (o = E), R.resetForNewStream(f), typeof c.mood == "string" && c.mood.trim() && (a = c.mood.trim()), s.length = 0, s.push(
859
- ...Ot(c.sentence_emotions, a)
860
- ), Array.isArray(c.gestures) && c.gestures, y("metadata", E), S();
861
- }, A = (c, E, B, Q) => {
846
+ const v = String(c.spoken_text ?? c.text ?? "").trim();
847
+ v && (o = v), R.resetForNewStream(f), typeof c.mood == "string" && c.mood.trim() && (a = c.mood.trim()), s.length = 0, s.push(
848
+ ...Lt(c.sentence_emotions, a)
849
+ ), Array.isArray(c.gestures) && c.gestures, y("metadata", v), E();
850
+ }, A = (c, v, L, z) => {
862
851
  if (!m.onLipsyncDebug) return;
863
- const te = n.slice(B), ke = Q.filter(
864
- (re) => String(re.symbol ?? re.viseme ?? "").trim()
852
+ const ne = n.slice(L), Re = z.filter(
853
+ (G) => String(G.symbol ?? G.viseme ?? "").trim()
865
854
  );
866
855
  i.push({
867
- spokenText: E,
868
- audioOffsetMs: te[0]?.vtime ?? 0,
856
+ spokenText: v,
857
+ audioOffsetMs: ne[0]?.vtime ?? 0,
869
858
  isNewSegment: !!c.is_new_segment,
870
859
  chunkDurationMs: c.chunk_duration_ms != null ? Number(c.chunk_duration_ms) : null,
871
860
  receivedAtMs: performance.now(),
872
- visemes: te.map((re, ce) => {
873
- const ue = ke[ce];
861
+ visemes: ne.map((G, Se) => {
862
+ const Y = Re[Se];
874
863
  return {
875
- rawSymbol: String(ue?.symbol ?? ue?.viseme ?? re.viseme).trim(),
876
- viseme: re.viseme,
877
- startSec: Number(ue?.start ?? 0),
878
- durationSec: Number(ue?.duration ?? 0),
879
- vtimeMs: re.vtime,
880
- vdurationMs: re.vduration
864
+ rawSymbol: String(Y?.symbol ?? Y?.viseme ?? G.viseme).trim(),
865
+ viseme: G.viseme,
866
+ startSec: Number(Y?.start ?? 0),
867
+ durationSec: Number(Y?.duration ?? 0),
868
+ vtimeMs: G.vtime,
869
+ vdurationMs: G.vduration
881
870
  };
882
871
  })
883
872
  });
884
873
  }, P = (c) => {
885
- const E = String(c.spoken_text ?? c.text ?? "").trim(), B = !!c.is_new_segment || Number(c.chunk_index ?? -1) === 0, Q = c.visemes ?? [], te = c.words ?? [], ke = String(c.chunk ?? "").trim();
886
- E && (o = E), b(c, B), B && R.resetForNewStream(f);
887
- const re = n.length;
874
+ const v = String(c.spoken_text ?? c.text ?? "").trim(), L = !!c.is_new_segment || Number(c.chunk_index ?? -1) === 0, z = c.visemes ?? [], ne = c.words ?? [], Re = String(c.chunk ?? "").trim();
875
+ v && (o = v), b(c, L), L && R.resetForNewStream(f);
876
+ const G = n.length;
888
877
  R.playAudioChunk(
889
878
  n,
890
879
  r,
891
- ke,
892
- Q,
893
- te,
894
- B,
880
+ Re,
881
+ z,
882
+ ne,
883
+ L,
895
884
  () => {
896
885
  i.length = 0;
897
886
  }
898
887
  ).then(() => {
899
- A(c, E, re, Q), y("audio_chunk", E);
888
+ A(c, v, G, z), y("audio_chunk", v);
900
889
  });
901
- }, O = (c) => {
890
+ }, B = (c) => {
902
891
  console.warn(
903
892
  "[realtimeVoice] avatar_lipsync is deprecated — expect avatar_audio_chunk"
904
893
  );
905
- const E = String(c.spoken_text ?? c.text ?? "").trim();
906
- E && (o = E), b(c, !!c.is_new_segment);
907
- const B = n.length;
894
+ const v = String(c.spoken_text ?? c.text ?? "").trim();
895
+ v && (o = v), b(c, !!c.is_new_segment);
896
+ const L = n.length;
908
897
  R.queueTimingsOnly(
909
898
  n,
910
899
  r,
@@ -913,66 +902,66 @@ function Dr(e, t = {}) {
913
902
  !!c.is_new_segment
914
903
  ), A(
915
904
  c,
916
- E,
917
- B,
905
+ v,
906
+ L,
918
907
  c.visemes ?? []
919
- ), y("lipsync", E), S();
908
+ ), y("lipsync", v), E();
920
909
  }, W = (c) => {
921
- const E = Rr(c);
922
- if (!E) {
910
+ const v = Ir(c);
911
+ if (!v) {
923
912
  console.warn("[realtimeVoice] server message with no payload", c);
924
913
  return;
925
914
  }
926
- const B = String(E.type ?? "unknown");
927
- switch (B !== "avatar_metadata" && B !== "avatar_audio_chunk" && B !== "avatar_lipsync" && B !== "error" && console.debug("[realtimeVoice] server message", B, E), B) {
915
+ const L = String(v.type ?? "unknown");
916
+ switch (L !== "avatar_metadata" && L !== "avatar_audio_chunk" && L !== "avatar_lipsync" && L !== "error" && console.debug("[realtimeVoice] server message", L, v), L) {
928
917
  case "avatar_metadata":
929
- D(E);
918
+ D(v);
930
919
  break;
931
920
  case "avatar_audio_chunk":
932
- P(E);
921
+ P(v);
933
922
  break;
934
923
  case "avatar_lipsync":
935
- O(E);
924
+ B(v);
936
925
  break;
937
926
  case "error": {
938
- const Q = String(E.message ?? "Voice session error");
939
- console.error("[realtimeVoice] server error:", Q), e("error"), S();
927
+ const z = String(v.message ?? "Voice session error");
928
+ console.error("[realtimeVoice] server error:", z), e("error"), E();
940
929
  break;
941
930
  }
942
931
  case "avatar_speak":
943
932
  console.warn(
944
933
  "[realtimeVoice] avatar_speak received; tts_stream=false Mode B is not wired yet:",
945
- E.text
934
+ v.text
946
935
  );
947
936
  break;
948
937
  }
949
- }, G = () => w || (_ = new vr(), w = new Tn({
938
+ }, U = () => w || (_ = new kr(), w = new Tn({
950
939
  transport: new An({
951
940
  mediaManager: _,
952
- recorderSampleRate: sn,
953
- playerSampleRate: on
941
+ recorderSampleRate: rn,
942
+ playerSampleRate: sn
954
943
  }),
955
944
  enableCam: !1,
956
945
  enableMic: !0,
957
946
  callbacks: {
958
947
  onBotReady: () => {
959
- l = !0, e("idle"), S();
948
+ l = !0, e("idle"), E();
960
949
  },
961
950
  onDisconnected: () => {
962
- l = !1, M();
951
+ l = !1, C();
963
952
  },
964
953
  onError: (c) => {
965
- const E = typeof c == "object" && c !== null && "data" in c ? String(c.data ?? c) : String(c);
966
- console.error("[realtimeVoice]", E, c), e("error"), S();
954
+ const v = typeof c == "object" && c !== null && "data" in c ? String(c.data ?? c) : String(c);
955
+ console.error("[realtimeVoice]", v, c), e("error"), E();
967
956
  },
968
957
  onTransportStateChanged: (c) => {
969
- c === "error" && (e("error"), S());
958
+ c === "error" && (e("error"), E());
970
959
  },
971
960
  onServerMessage: (c) => {
972
- N(c);
961
+ V(c);
973
962
  },
974
963
  onUserStartedSpeaking: () => {
975
- R.bargeIn(), T(), e("idle"), y("user_started"), S();
964
+ R.bargeIn(), T(), e("idle"), y("user_started"), E();
976
965
  },
977
966
  onBotStartedSpeaking: () => {
978
967
  y("bot_started");
@@ -995,18 +984,18 @@ function Dr(e, t = {}) {
995
984
  f = c, R.setStreamTtsEngineId(c);
996
985
  },
997
986
  getTtsEngineId: () => f,
998
- speak: async (c, E) => {
999
- const B = c.trim();
1000
- if (B) {
987
+ speak: async (c, v) => {
988
+ const L = c.trim();
989
+ if (L) {
1001
990
  if (!w || !l)
1002
991
  throw new Error(
1003
992
  "Realtime voice is not connected. Call connect() first."
1004
993
  );
1005
- g(), await w.sendText(B);
994
+ g(), await w.sendText(L);
1006
995
  }
1007
996
  },
1008
997
  stop: () => {
1009
- g(), e("idle"), S();
998
+ g(), e("idle"), E();
1010
999
  },
1011
1000
  connect: async (c) => {
1012
1001
  if (c && (m = { ...m, ...c }), !m.authToken?.trim())
@@ -1014,18 +1003,18 @@ function Dr(e, t = {}) {
1014
1003
  "Voice connect requires authToken (same JWT as SSE Authorization Bearer)."
1015
1004
  );
1016
1005
  e("loading");
1017
- const E = G();
1018
- await E.initDevices();
1019
- const B = Ir(m);
1020
- console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):", B);
1006
+ const v = U();
1007
+ await v.initDevices();
1008
+ const L = Ar(m);
1009
+ console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):", L);
1021
1010
  try {
1022
- await E.connect({ wsUrl: B });
1023
- } catch (Q) {
1024
- l = !1, e("error"), S();
1025
- const te = Q instanceof Error ? Q.message : "Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";
1026
- throw new Error(`${te} — ${B}`);
1011
+ await v.connect({ wsUrl: L });
1012
+ } catch (z) {
1013
+ l = !1, e("error"), E();
1014
+ const ne = z instanceof Error ? z.message : "Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";
1015
+ throw new Error(`${ne} — ${L}`);
1027
1016
  }
1028
- l = !0, e("idle"), S();
1017
+ l = !0, e("idle"), E();
1029
1018
  },
1030
1019
  disconnect: async () => {
1031
1020
  if (p.length = 0, d = !1, w)
@@ -1034,7 +1023,7 @@ function Dr(e, t = {}) {
1034
1023
  } catch (c) {
1035
1024
  console.error("[realtimeVoice] disconnect failed:", c);
1036
1025
  }
1037
- l = !1, M();
1026
+ l = !1, C();
1038
1027
  },
1039
1028
  isConnected: () => l,
1040
1029
  unlockAudio: async () => {
@@ -1053,7 +1042,7 @@ function Dr(e, t = {}) {
1053
1042
  subscribe: (c) => (u.add(c), () => u.delete(c))
1054
1043
  };
1055
1044
  }
1056
- function Cr(e, t, n) {
1045
+ function Dr(e, t, n) {
1057
1046
  return {
1058
1047
  width: e.crop?.source_width ?? t,
1059
1048
  height: e.crop?.source_height ?? n
@@ -1068,7 +1057,7 @@ function Mr(e, t) {
1068
1057
  height: t?.source_h ?? n?.height ?? e.frame_height ?? 0
1069
1058
  };
1070
1059
  }
1071
- function xr(e) {
1060
+ function Cr(e) {
1072
1061
  return e ? (e.w ?? 0) > 0 && (e.h ?? 0) > 0 : !1;
1073
1062
  }
1074
1063
  async function Ye(e, t) {
@@ -1077,12 +1066,12 @@ async function Ye(e, t) {
1077
1066
  const r = await n.arrayBuffer();
1078
1067
  return zn(r, t);
1079
1068
  }
1080
- async function Pr(e, t) {
1069
+ async function xr(e, t) {
1081
1070
  if (!t) throw new Error("Missing asset key for encrypted JSON");
1082
1071
  const n = await Ye(e, t), r = new TextDecoder().decode(n);
1083
1072
  return JSON.parse(r);
1084
1073
  }
1085
- async function Br(e) {
1074
+ async function Pr(e) {
1086
1075
  const t = new Blob([e], { type: "image/webp" }), n = URL.createObjectURL(t);
1087
1076
  return new Promise((r, s) => {
1088
1077
  const i = new Image();
@@ -1093,7 +1082,7 @@ async function Br(e) {
1093
1082
  }, i.src = n;
1094
1083
  });
1095
1084
  }
1096
- async function Lr(e) {
1085
+ async function Br(e) {
1097
1086
  const t = new Blob([e], { type: "image/png" }), n = URL.createObjectURL(t);
1098
1087
  return new Promise((r, s) => {
1099
1088
  const i = new Image();
@@ -1104,28 +1093,28 @@ async function Lr(e) {
1104
1093
  }, i.src = n;
1105
1094
  });
1106
1095
  }
1107
- function Or(e) {
1096
+ function Lr(e) {
1108
1097
  const t = new Blob([e], { type: "video/mp4" });
1109
1098
  return URL.createObjectURL(t);
1110
1099
  }
1111
- async function Fr(e) {
1100
+ async function Or(e) {
1112
1101
  const t = Zn();
1113
- if (!je()) return t;
1102
+ if (!Ge()) return t;
1114
1103
  if (!e) throw new Error("Missing asset key for encrypted idle video");
1115
1104
  const n = await Ye(t, e);
1116
- return Or(n);
1105
+ return Lr(n);
1117
1106
  }
1118
- function mt(e, t) {
1107
+ function ht(e, t) {
1119
1108
  return `${e}/${t}_minus_sil.png`;
1120
1109
  }
1121
- const an = 5;
1122
- function cn(e) {
1110
+ const on = 5;
1111
+ function an(e) {
1123
1112
  const t = e?.composite_threshold;
1124
1113
  if (typeof t == "number" && t > 0) return t;
1125
1114
  const n = e?.crop?.content_threshold;
1126
- return typeof n == "number" && n > 0 ? n : an;
1115
+ return typeof n == "number" && n > 0 ? n : on;
1127
1116
  }
1128
- const Ys = [
1117
+ const Gs = [
1129
1118
  "aa",
1130
1119
  "E",
1131
1120
  "I",
@@ -1140,11 +1129,11 @@ const Ys = [
1140
1129
  "RR",
1141
1130
  "kk",
1142
1131
  "nn"
1143
- ], at = [
1132
+ ], it = [
1144
1133
  "t01_0.25",
1145
1134
  "t02_0.50",
1146
1135
  "t03_0.75"
1147
- ], Ur = at.length, Ft = /* @__PURE__ */ new Set([
1136
+ ], Fr = it.length, Ot = /* @__PURE__ */ new Set([
1148
1137
  "aa__kk",
1149
1138
  "aa__nn",
1150
1139
  "aa__sil",
@@ -1251,75 +1240,75 @@ const Ys = [
1251
1240
  "U__nn",
1252
1241
  "U__sil"
1253
1242
  ]);
1254
- function Ut(e, t) {
1243
+ function Ft(e, t) {
1255
1244
  return `${e}__${t}`;
1256
1245
  }
1257
- function Nt(e, t) {
1246
+ function Ut(e, t) {
1258
1247
  return `pairs/${e}/${t}_minus_sil.png`;
1259
1248
  }
1260
- function qe(e) {
1249
+ function Ke(e) {
1261
1250
  return `${e}_minus_sil.png`;
1262
1251
  }
1263
- function Nr(e, t) {
1252
+ function Ur(e, t) {
1264
1253
  if (t.has(e)) return e;
1265
1254
  const n = /^pairs\/([^/]+)\//.exec(e);
1266
1255
  if (!n) return null;
1267
1256
  const r = n[1].split("__")[1];
1268
1257
  if (!r || r === "sil") return null;
1269
- const s = qe(r);
1258
+ const s = Ke(r);
1270
1259
  return t.has(s) ? s : null;
1271
1260
  }
1272
- function un(e, t) {
1261
+ function cn(e, t) {
1273
1262
  return e.filter((n) => t.has(n));
1274
1263
  }
1275
- function Vr(e, t, n) {
1276
- const r = wt(e, t);
1277
- return un(r, n).length > 0;
1264
+ function Nr(e, t, n) {
1265
+ const r = mt(e, t);
1266
+ return cn(r, n).length > 0;
1278
1267
  }
1279
- function wt(e, t) {
1268
+ function mt(e, t) {
1280
1269
  if (e === t) return [];
1281
- const n = [], r = Ut(e, t);
1282
- if (Ft.has(r)) {
1283
- for (const i of at)
1284
- n.push(Nt(r, i));
1270
+ const n = [], r = Ft(e, t);
1271
+ if (Ot.has(r)) {
1272
+ for (const i of it)
1273
+ n.push(Ut(r, i));
1285
1274
  return n;
1286
1275
  }
1287
- const s = Ut(t, e);
1288
- if (Ft.has(s)) {
1289
- for (let i = Ur - 1; i >= 0; i--)
1290
- n.push(Nt(s, at[i]));
1276
+ const s = Ft(t, e);
1277
+ if (Ot.has(s)) {
1278
+ for (let i = Fr - 1; i >= 0; i--)
1279
+ n.push(Ut(s, it[i]));
1291
1280
  return n;
1292
1281
  }
1293
1282
  return [];
1294
1283
  }
1295
- function $r(e) {
1284
+ function Vr(e) {
1296
1285
  return e < 16 ? [] : e < 35 ? [1] : e < 51 ? [0, 2] : [0, 1, 2];
1297
1286
  }
1298
- function Wr(e, t) {
1299
- return $r(t).map((r) => e[r]).filter((r) => r != null);
1287
+ function $r(e, t) {
1288
+ return Vr(t).map((r) => e[r]).filter((r) => r != null);
1300
1289
  }
1301
- function Hr(e, t, n, r) {
1290
+ function Wr(e, t, n, r) {
1302
1291
  if (r <= 0) return 0;
1303
1292
  const i = Math.max(1, n - t) / r, o = e - t, a = Math.floor(o / i);
1304
1293
  return Math.min(r - 1, Math.max(0, a));
1305
1294
  }
1306
- function qr(e, t, n) {
1295
+ function Hr(e, t, n) {
1307
1296
  if (!n) return `${e}/${t}`;
1308
1297
  const r = t.split("/").pop() ?? t, s = r.includes(".") ? r.slice(0, r.lastIndexOf(".")) : r;
1309
1298
  return `${e}/${s}.bin`;
1310
1299
  }
1311
- const Kr = [0.32, 0.24, 0.16, 0.08, 0], jr = Kr.map(
1300
+ const qr = [0.32, 0.24, 0.16, 0.08, 0], Kr = qr.map(
1312
1301
  (e) => `eyes_${e.toFixed(2)}`
1313
- ), Gr = [
1302
+ ), jr = [
1314
1303
  "eyes_0.32",
1315
1304
  "eyes_0.16",
1316
1305
  "eyes_0.00"
1317
- ], zr = [
1306
+ ], Gr = [
1318
1307
  "eyes_0.32",
1319
1308
  "eyes_0.16",
1320
1309
  "eyes_0.08",
1321
1310
  "eyes_0.00"
1322
- ], Vt = 2, Yr = -30, ct = 20, Jr = {
1311
+ ], Nt = 2, zr = -30, ot = 20, Yr = {
1323
1312
  fear: -30,
1324
1313
  anger: -20,
1325
1314
  sad: -10,
@@ -1328,24 +1317,24 @@ const Kr = [0.32, 0.24, 0.16, 0.08, 0], jr = Kr.map(
1328
1317
  happy: 0,
1329
1318
  disgust: 20
1330
1319
  };
1331
- function gt(e) {
1320
+ function wt(e) {
1332
1321
  return `brow_${e}`;
1333
1322
  }
1334
- function _t(e) {
1335
- return Jr[e];
1323
+ function gt(e) {
1324
+ return Yr[e];
1336
1325
  }
1337
- function Ee(e) {
1338
- return Math.max(Yr, Math.min(ct, e));
1326
+ function ye(e) {
1327
+ return Math.max(zr, Math.min(ot, e));
1339
1328
  }
1340
- function Qr(e, t) {
1329
+ function Jr(e, t) {
1341
1330
  if (e === t) return [];
1342
- const n = e < t ? Vt : -Vt, r = [];
1331
+ const n = e < t ? Nt : -Nt, r = [];
1343
1332
  let s = e;
1344
1333
  for (; s !== t; )
1345
1334
  s += n, r.push(s);
1346
1335
  return r;
1347
1336
  }
1348
- const Xr = {
1337
+ const Qr = {
1349
1338
  fear: "fear",
1350
1339
  afraid: "fear",
1351
1340
  anxiety: "fear",
@@ -1368,63 +1357,63 @@ const Xr = {
1368
1357
  disgust: "disgust",
1369
1358
  disgusted: "disgust"
1370
1359
  };
1371
- function Te(e, t = "neutral") {
1360
+ function ve(e, t = "neutral") {
1372
1361
  const n = String(e ?? "").trim().toLowerCase();
1373
- return n ? Xr[n] ?? t : t;
1362
+ return n ? Qr[n] ?? t : t;
1374
1363
  }
1375
- function ln(e) {
1376
- return e === 0 ? null : mt(gt(e), "eyes_standard");
1364
+ function un(e) {
1365
+ return e === 0 ? null : ht(wt(e), "eyes_standard");
1377
1366
  }
1378
- function fn(e) {
1379
- return ln(_t(e));
1367
+ function ln(e) {
1368
+ return un(gt(e));
1380
1369
  }
1381
- function $t(e, t) {
1382
- const n = gt(_t(e));
1383
- return !t || t === "eyes_standard" ? fn(e) : e === "neutral" && t === "eyes_standard" ? null : mt(n, t);
1370
+ function Vt(e, t) {
1371
+ const n = wt(gt(e));
1372
+ return !t || t === "eyes_standard" ? ln(e) : e === "neutral" && t === "eyes_standard" ? null : ht(n, t);
1384
1373
  }
1385
- function Zr(e, t) {
1374
+ function Xr(e, t) {
1386
1375
  const n = [];
1387
1376
  for (const r of t)
1388
- n.push($t(e, r));
1377
+ n.push(Vt(e, r));
1389
1378
  for (let r = t.length - 2; r >= 0; r--)
1390
- n.push($t(e, t[r]));
1391
- return n.push(fn(e)), n;
1379
+ n.push(Vt(e, t[r]));
1380
+ return n.push(ln(e)), n;
1392
1381
  }
1393
- function es(e) {
1394
- return e === 0 ? null : mt(gt(e), "eyes_standard");
1382
+ function Zr(e) {
1383
+ return e === 0 ? null : ht(wt(e), "eyes_standard");
1395
1384
  }
1396
- function ts(e, t) {
1397
- return Qr(e, t).map(es).filter((n) => n != null);
1385
+ function es(e, t) {
1386
+ return Jr(e, t).map(Zr).filter((n) => n != null);
1398
1387
  }
1399
- const ns = 6;
1400
- function rs(e, t) {
1388
+ const ts = 6;
1389
+ function ns(e, t) {
1401
1390
  for (const n of e)
1402
1391
  if (t >= n.wtime && t < n.wtime + n.wduration)
1403
1392
  return n;
1404
1393
  return null;
1405
1394
  }
1406
- function ss(e) {
1407
- const t = Ee(e);
1408
- return t >= ct ? ct : Ee(t + ns);
1395
+ function rs(e) {
1396
+ const t = ye(e);
1397
+ return t >= ot ? ot : ye(t + ts);
1409
1398
  }
1410
- function is(e, t, n) {
1411
- const r = Ee(n);
1412
- return rs(t, e)?.emphasis ? ss(r) : r;
1399
+ function ss(e, t, n) {
1400
+ const r = ye(n);
1401
+ return ns(t, e)?.emphasis ? rs(r) : r;
1413
1402
  }
1414
- function Wt(e) {
1403
+ function $t(e) {
1415
1404
  if (!e) return 0;
1416
1405
  const t = e.match(/^brow_(-?\d+)\//);
1417
1406
  if (!t?.[1]) return null;
1418
1407
  const n = Number.parseInt(t[1], 10);
1419
- return Number.isFinite(n) ? Ee(n) : null;
1408
+ return Number.isFinite(n) ? ye(n) : null;
1420
1409
  }
1421
- function Le(e, t) {
1410
+ function Fe(e, t) {
1422
1411
  return e + Math.random() * (t - e);
1423
1412
  }
1424
- function We() {
1425
- return Le(14, 32);
1413
+ function He() {
1414
+ return Fe(14, 32);
1426
1415
  }
1427
- function os(e = {}) {
1416
+ function is(e = {}) {
1428
1417
  const {
1429
1418
  initialDelayMs: t = 800 + Math.random() * 1200,
1430
1419
  intervalMinMs: n = 2200,
@@ -1436,130 +1425,130 @@ function os(e = {}) {
1436
1425
  postEmotionChangeDelayMs: f = 380,
1437
1426
  minDwellMs: l = 550
1438
1427
  } = e;
1439
- let w = "neutral", _ = "neutral", m = 0, v = 0, k = 0, u = {
1428
+ let w = "neutral", _ = "neutral", m = 0, k = 0, S = 0, u = {
1440
1429
  kind: "idle",
1441
1430
  nextBlinkAt: performance.now() + t
1442
1431
  }, p = null, d = "__eyes_open__|neutral|brow:0";
1443
1432
  function I(y = w) {
1444
- d = `${p ?? "__eyes_open__"}|${y}|brow:${v}`;
1433
+ d = `${p ?? "__eyes_open__"}|${y}|brow:${k}`;
1445
1434
  }
1446
- function S(y, T = w) {
1435
+ function E(y, T = w) {
1447
1436
  p = y;
1448
- const g = Wt(y);
1449
- g != null ? v = g : y == null && (v = 0), I(T);
1437
+ const g = $t(y);
1438
+ g != null ? k = g : y == null && (k = 0), I(T);
1450
1439
  }
1451
1440
  function R(y) {
1452
- return ln(y);
1441
+ return un(y);
1453
1442
  }
1454
- function q(y, T, g) {
1455
- const M = Ee(T), b = Ee(g);
1456
- if (m = b, M === b) {
1457
- S(R(b));
1443
+ function H(y, T, g) {
1444
+ const C = ye(T), b = ye(g);
1445
+ if (m = b, C === b) {
1446
+ E(R(b));
1458
1447
  return;
1459
1448
  }
1460
1449
  const D = [
1461
- ...ts(M, b),
1450
+ ...es(C, b),
1462
1451
  R(b)
1463
1452
  ];
1464
1453
  u = {
1465
1454
  kind: "browBlend",
1466
1455
  paths: D,
1467
1456
  index: 0,
1468
- holdUntil: y + We(),
1457
+ holdUntil: y + He(),
1469
1458
  targetBrow: b
1470
- }, S(D[0] ?? null);
1459
+ }, E(D[0] ?? null);
1471
1460
  }
1472
- function N(y, T) {
1461
+ function V(y, T) {
1473
1462
  u = {
1474
1463
  kind: "idle",
1475
- nextBlinkAt: y + Le(n, r)
1476
- }, _ = T, S(R(m), T);
1464
+ nextBlinkAt: y + Fe(n, r)
1465
+ }, _ = T, E(R(m), T);
1477
1466
  }
1478
1467
  function $(y, T, g) {
1479
- const M = g ? Gr : Math.random() < a ? jr : zr;
1468
+ const C = g ? jr : Math.random() < a ? Kr : Gr;
1480
1469
  u = {
1481
1470
  kind: "playing",
1482
- paths: Zr(T, M),
1471
+ paths: Xr(T, C),
1483
1472
  index: 0,
1484
- holdUntil: y + We(),
1473
+ holdUntil: y + He(),
1485
1474
  after: "idle",
1486
1475
  blinkEmotion: T
1487
- }, _ = T, S(u.paths[0] ?? null, T);
1476
+ }, _ = T, E(u.paths[0] ?? null, T);
1488
1477
  }
1489
- function z(y) {
1478
+ function j(y) {
1490
1479
  if (u.kind === "browBlend") {
1491
1480
  for (; y >= u.holdUntil && u.index < u.paths.length - 1; )
1492
- u.index += 1, u.holdUntil = y + We();
1481
+ u.index += 1, u.holdUntil = y + He();
1493
1482
  const T = u.paths[u.index] ?? null;
1494
- S(T), y >= u.holdUntil && u.index >= u.paths.length - 1 && (v = u.targetBrow, m = u.targetBrow, S(R(u.targetBrow)), u = {
1483
+ E(T), y >= u.holdUntil && u.index >= u.paths.length - 1 && (k = u.targetBrow, m = u.targetBrow, E(R(u.targetBrow)), u = {
1495
1484
  kind: "idle",
1496
1485
  nextBlinkAt: y + f
1497
1486
  });
1498
1487
  return;
1499
1488
  }
1500
1489
  if (u.kind === "idle") {
1501
- S(R(m), _), y >= u.nextBlinkAt && $(y, _, !1);
1490
+ E(R(m), _), y >= u.nextBlinkAt && $(y, _, !1);
1502
1491
  return;
1503
1492
  }
1504
1493
  if (u.kind === "doublePause") {
1505
- S(R(m), u.blinkEmotion), y >= u.resumeAt && $(y, u.blinkEmotion, !0);
1494
+ E(R(m), u.blinkEmotion), y >= u.resumeAt && $(y, u.blinkEmotion, !0);
1506
1495
  return;
1507
1496
  }
1508
1497
  if (u.kind === "playing") {
1509
1498
  for (; y >= u.holdUntil && u.index < u.paths.length - 1; )
1510
- u.index += 1, u.holdUntil = y + We();
1511
- if (S(u.paths[u.index] ?? null, u.blinkEmotion), y >= u.holdUntil && u.index >= u.paths.length - 1) {
1499
+ u.index += 1, u.holdUntil = y + He();
1500
+ if (E(u.paths[u.index] ?? null, u.blinkEmotion), y >= u.holdUntil && u.index >= u.paths.length - 1) {
1512
1501
  if (u.after === "resumeIdle") {
1513
- N(y + f, w);
1502
+ V(y + f, w);
1514
1503
  return;
1515
1504
  }
1516
1505
  if (u.after === "doublePause") {
1517
1506
  u = {
1518
1507
  kind: "doublePause",
1519
- resumeAt: y + Le(i, o),
1508
+ resumeAt: y + Fe(i, o),
1520
1509
  blinkEmotion: u.blinkEmotion
1521
- }, S(R(m), u.blinkEmotion);
1510
+ }, E(R(m), u.blinkEmotion);
1522
1511
  return;
1523
1512
  }
1524
1513
  if (u.after === "idle" && Math.random() < s) {
1525
1514
  u = {
1526
1515
  kind: "doublePause",
1527
- resumeAt: y + Le(i, o),
1516
+ resumeAt: y + Fe(i, o),
1528
1517
  blinkEmotion: u.blinkEmotion
1529
- }, S(R(m), u.blinkEmotion);
1518
+ }, E(R(m), u.blinkEmotion);
1530
1519
  return;
1531
1520
  }
1532
- N(y, u.blinkEmotion);
1521
+ V(y, u.blinkEmotion);
1533
1522
  }
1534
1523
  }
1535
1524
  }
1536
1525
  return {
1537
- advance: z,
1526
+ advance: j,
1538
1527
  setTargetEmotion(y) {
1539
- const T = Te(y, w);
1528
+ const T = ve(y, w);
1540
1529
  if (T === w) return;
1541
1530
  const g = performance.now();
1542
- l > 0 && T !== w && !(w === "neutral" && T !== "neutral") && g - k < l || (w = T, _ = T, k = g);
1531
+ l > 0 && T !== w && !(w === "neutral" && T !== "neutral") && g - S < l || (w = T, _ = T, S = g);
1543
1532
  },
1544
1533
  setTargetEyebrow(y) {
1545
- const T = Ee(y);
1534
+ const T = ye(y);
1546
1535
  if (T === m && u.kind !== "browBlend") return;
1547
- const g = performance.now(), M = u.kind === "browBlend" ? Wt(p) ?? v : v;
1548
- m = T, !(T === M && u.kind !== "browBlend") && (u.kind === "playing" || u.kind === "doublePause" || q(g, M, T));
1536
+ const g = performance.now(), C = u.kind === "browBlend" ? $t(p) ?? k : k;
1537
+ m = T, !(T === C && u.kind !== "browBlend") && (u.kind === "playing" || u.kind === "doublePause" || H(g, C, T));
1549
1538
  },
1550
1539
  getTargetEmotion: () => w,
1551
1540
  getTargetEyebrow: () => m,
1552
1541
  getExpressionPath: () => p,
1553
1542
  getDrawKey: () => d,
1554
1543
  reset() {
1555
- w = "neutral", _ = "neutral", m = 0, v = 0, k = 0, N(
1556
- performance.now() + Le(n, r),
1544
+ w = "neutral", _ = "neutral", m = 0, k = 0, S = 0, V(
1545
+ performance.now() + Fe(n, r),
1557
1546
  "neutral"
1558
1547
  );
1559
1548
  }
1560
1549
  };
1561
1550
  }
1562
- function as() {
1551
+ function os() {
1563
1552
  const e = document.createElement("video");
1564
1553
  e.muted = !0, e.playsInline = !0, e.preload = "auto", e.setAttribute("playsinline", ""), e.loop = !0;
1565
1554
  let t = !1, n = null, r = "";
@@ -1602,17 +1591,17 @@ function as() {
1602
1591
  }
1603
1592
  };
1604
1593
  }
1605
- const cs = 50, dn = 400;
1606
- function us(e, t, n) {
1594
+ const as = 50, fn = 400;
1595
+ function cs(e, t, n) {
1607
1596
  return e.filter((r) => {
1608
1597
  const s = r.queueIndex ?? -1;
1609
1598
  return s >= t && s <= n;
1610
1599
  });
1611
1600
  }
1612
- function ls(e, t) {
1601
+ function us(e, t) {
1613
1602
  const n = [];
1614
1603
  for (const r of e) {
1615
- const s = us(
1604
+ const s = cs(
1616
1605
  t,
1617
1606
  r.start_word,
1618
1607
  r.end_word
@@ -1621,30 +1610,30 @@ function ls(e, t) {
1621
1610
  const i = s[0], o = s[s.length - 1];
1622
1611
  n.push({
1623
1612
  sentence: r,
1624
- startMs: Math.max(0, i.wtime - cs),
1625
- endMs: o.wtime + o.wduration + dn
1613
+ startMs: Math.max(0, i.wtime - as),
1614
+ endMs: o.wtime + o.wduration + fn
1626
1615
  });
1627
1616
  }
1628
1617
  return n.sort((r, s) => r.startMs - s.startMs);
1629
1618
  }
1630
- function fs(e, t, n, r = "neutral") {
1631
- const s = Te(r);
1619
+ function ls(e, t, n, r = "neutral") {
1620
+ const s = ve(r);
1632
1621
  if (t.length === 0) return s;
1633
1622
  if (n.length === 0 || e < n[0].wtime)
1634
- return Te(t[0]?.emotion, s);
1635
- const i = ls(t, n);
1623
+ return ve(t[0]?.emotion, s);
1624
+ const i = us(t, n);
1636
1625
  let o = null;
1637
1626
  for (const l of i)
1638
1627
  e >= l.startMs && e <= l.endMs && (o = l);
1639
1628
  if (o)
1640
- return Te(o.sentence.emotion, s);
1629
+ return ve(o.sentence.emotion, s);
1641
1630
  const a = n[n.length - 1], f = a.wtime + a.wduration;
1642
- return e > f + dn ? Te(
1631
+ return e > f + fn ? ve(
1643
1632
  t[t.length - 1]?.emotion,
1644
1633
  s
1645
1634
  ) : s;
1646
1635
  }
1647
- function hn(e, t, n, r, s = an) {
1636
+ function dn(e, t, n, r, s = on) {
1648
1637
  const i = new OffscreenCanvas(n, r), o = i.getContext("2d", { willReadFrequently: !0 });
1649
1638
  o.drawImage(e, 0, 0, n, r);
1650
1639
  const a = o.getImageData(0, 0, n, r), f = o.createImageData(n, r);
@@ -1653,45 +1642,45 @@ function hn(e, t, n, r, s = an) {
1653
1642
  for (const _ of t) {
1654
1643
  w.clearRect(0, 0, n, r), w.drawImage(_, 0, 0, n, r);
1655
1644
  const m = w.getImageData(0, 0, n, r);
1656
- for (let v = 0; v < f.data.length; v += 4) {
1657
- const k = m.data[v], u = m.data[v + 1], p = m.data[v + 2];
1658
- Math.max(k, u, p) > s && (f.data[v] = k, f.data[v + 1] = u, f.data[v + 2] = p, f.data[v + 3] = 255);
1645
+ for (let k = 0; k < f.data.length; k += 4) {
1646
+ const S = m.data[k], u = m.data[k + 1], p = m.data[k + 2];
1647
+ Math.max(S, u, p) > s && (f.data[k] = S, f.data[k + 1] = u, f.data[k + 2] = p, f.data[k + 3] = 255);
1659
1648
  }
1660
1649
  }
1661
1650
  return o.putImageData(f, 0, 0), i;
1662
1651
  }
1663
- let ye = null, Oe = null;
1664
- const Ke = /* @__PURE__ */ new Map();
1665
- function ds() {
1666
- ye = null, Oe = null, Ke.clear();
1652
+ let pe = null, Ue = null;
1653
+ const je = /* @__PURE__ */ new Map();
1654
+ function fs() {
1655
+ pe = null, Ue = null, je.clear();
1667
1656
  }
1668
- Xn(ds);
1669
- function hs(e) {
1670
- return e ?? Oe ?? void 0;
1657
+ Xn(fs);
1658
+ function ds(e) {
1659
+ return e ?? Ue ?? void 0;
1671
1660
  }
1672
- function pt(e) {
1661
+ function _t(e) {
1673
1662
  return new Promise((t, n) => {
1674
1663
  const r = new Image();
1675
1664
  r.decoding = "async", r.onload = () => t(r), r.onerror = () => n(new Error(`Failed to load image: ${e}`)), r.src = e;
1676
1665
  });
1677
1666
  }
1678
- async function ms(e) {
1679
- return je() ? Lr(
1680
- await Ye(xt(), e ?? "")
1681
- ) : pt(xt());
1667
+ async function hs(e) {
1668
+ return Ge() ? Br(
1669
+ await Ye(Ct(), e ?? "")
1670
+ ) : _t(Ct());
1682
1671
  }
1683
- async function Ht(e, t, n, r) {
1684
- const s = n ? await Pr(e, r) : await fetch(e).then((l) => {
1672
+ async function Wt(e, t, n, r) {
1673
+ const s = n ? await xr(e, r) : await fetch(e).then((l) => {
1685
1674
  if (!l.ok) throw new Error(`Failed to load ${e}`);
1686
1675
  return l.json();
1687
1676
  }), i = s.sheets?.[0];
1688
1677
  if (!i?.path) throw new Error(`${e} has no sheets[0].path`);
1689
- const o = n ? await Br(
1678
+ const o = n ? await Pr(
1690
1679
  await Ye(
1691
- qr(t, i.path, n),
1680
+ Hr(t, i.path, n),
1692
1681
  r ?? ""
1693
1682
  )
1694
- ) : await pt(`${t}/${i.path}`), a = /* @__PURE__ */ new Map();
1683
+ ) : await _t(`${t}/${i.path}`), a = /* @__PURE__ */ new Map();
1695
1684
  for (const l of s.cells ?? [])
1696
1685
  l.path && a.set(l.path, l);
1697
1686
  const f = /* @__PURE__ */ new Map();
@@ -1699,21 +1688,21 @@ async function Ht(e, t, n, r) {
1699
1688
  f.set(l.index, l);
1700
1689
  return { atlas: o, atlasMeta: s, cellByPath: a, sheetByIndex: f, diffBase: t };
1701
1690
  }
1702
- async function ut(e) {
1703
- const t = hs(e), n = je();
1691
+ async function at(e) {
1692
+ const t = ds(e), n = Ge();
1704
1693
  if (n && !t)
1705
1694
  throw new Error("Encrypted assets enabled but no key provided");
1706
- if (ye && (!n || Oe === (t ?? null))) {
1707
- const i = await ye;
1695
+ if (pe && (!n || Ue === (t ?? null))) {
1696
+ const i = await pe;
1708
1697
  return {
1709
- ...Fe(i),
1698
+ ...Ne(i),
1710
1699
  visemePaths: [...i.viseme.cellByPath.keys()]
1711
1700
  };
1712
1701
  }
1713
- Oe = t ?? null, Ke.clear();
1702
+ Ue = t ?? null, je.clear();
1714
1703
  const r = er();
1715
- ye = (async () => {
1716
- const i = await ms(t), o = await Ht(
1704
+ pe = (async () => {
1705
+ const i = await hs(t), o = await Wt(
1717
1706
  tr(),
1718
1707
  r,
1719
1708
  n,
@@ -1721,7 +1710,7 @@ async function ut(e) {
1721
1710
  );
1722
1711
  let a = null;
1723
1712
  try {
1724
- a = await Ht(
1713
+ a = await Wt(
1725
1714
  nr(),
1726
1715
  r,
1727
1716
  n,
@@ -1732,90 +1721,90 @@ async function ut(e) {
1732
1721
  }
1733
1722
  return { sil: i, viseme: o, expression: a };
1734
1723
  })();
1735
- const s = await ye;
1736
- return { ...Fe(s), visemePaths: [...s.viseme.cellByPath.keys()] };
1724
+ const s = await pe;
1725
+ return { ...Ne(s), visemePaths: [...s.viseme.cellByPath.keys()] };
1737
1726
  }
1738
- async function mn(e) {
1739
- if (await ut(e), !ye) throw new Error("Assets not loaded");
1740
- return ye;
1727
+ async function hn(e) {
1728
+ if (await at(e), !pe) throw new Error("Assets not loaded");
1729
+ return pe;
1741
1730
  }
1742
- function Fe(e) {
1743
- return Cr(
1731
+ function Ne(e) {
1732
+ return Dr(
1744
1733
  e.viseme.atlasMeta,
1745
1734
  e.sil.naturalWidth,
1746
1735
  e.sil.naturalHeight
1747
1736
  );
1748
1737
  }
1749
- function ws(e, t) {
1738
+ function ms(e, t) {
1750
1739
  const n = document.createElement("canvas");
1751
1740
  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;
1752
1741
  }
1753
- async function qt(e, t, n) {
1754
- const r = `${t.diffBase}::${n}`, s = Ke.get(r);
1742
+ async function Ht(e, t, n) {
1743
+ const r = `${t.diffBase}::${n}`, s = je.get(r);
1755
1744
  if (s) return s;
1756
1745
  const i = (async () => {
1757
1746
  const o = t.cellByPath.get(n);
1758
1747
  if (!o) throw new Error(`No atlas cell for path: ${n}`);
1759
- const a = Mr(t.atlasMeta, o), f = Fe(e);
1760
- if (xr(o)) {
1761
- const _ = ws(t, o), m = document.createElement("canvas");
1748
+ const a = Mr(t.atlasMeta, o), f = Ne(e);
1749
+ if (Cr(o)) {
1750
+ const _ = ms(t, o), m = document.createElement("canvas");
1762
1751
  m.width = f.width, m.height = f.height;
1763
- const v = m.getContext("2d");
1764
- return v.fillStyle = "#000", v.fillRect(0, 0, f.width, f.height), v.drawImage(_, a.x, a.y), m;
1752
+ const k = m.getContext("2d");
1753
+ return k.fillStyle = "#000", k.fillRect(0, 0, f.width, f.height), k.drawImage(_, a.x, a.y), m;
1765
1754
  }
1766
- const l = `${t.diffBase}/${n}`, w = await pt(l);
1755
+ const l = `${t.diffBase}/${n}`, w = await _t(l);
1767
1756
  if (w.naturalWidth === f.width && w.naturalHeight === f.height)
1768
1757
  return w;
1769
1758
  throw new Error(`Diff PNG wrong size for ${n}`);
1770
1759
  })();
1771
- return Ke.set(r, i), i;
1760
+ return je.set(r, i), i;
1772
1761
  }
1773
- async function Kt(e, t, n, r, s, i) {
1762
+ async function qt(e, t, n, r, s, i) {
1774
1763
  if (s) {
1775
- const { width: v, height: k } = s;
1764
+ const { width: k, height: S } = s;
1776
1765
  if (i && !i() || t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA)
1777
1766
  return;
1778
1767
  const u = document.createElement("canvas");
1779
- u.width = v, u.height = k;
1768
+ u.width = k, u.height = S;
1780
1769
  const p = u.getContext("2d");
1781
- if (p.clearRect(0, 0, v, k), p.drawImage(t, 0, 0, v, k), i && !i()) return;
1782
- (e.width !== v || e.height !== k) && (e.width = v, e.height = k);
1770
+ if (p.clearRect(0, 0, k, S), p.drawImage(t, 0, 0, k, S), i && !i()) return;
1771
+ (e.width !== k || e.height !== S) && (e.width = k, e.height = S);
1783
1772
  const d = e.getContext("2d");
1784
- d.clearRect(0, 0, v, k), d.drawImage(u, 0, 0);
1773
+ d.clearRect(0, 0, k, S), d.drawImage(u, 0, 0);
1785
1774
  return;
1786
1775
  }
1787
- const o = await mn(), a = cn(o.viseme.atlasMeta), { width: f, height: l } = Fe(o);
1776
+ const o = await hn(), a = an(o.viseme.atlasMeta), { width: f, height: l } = Ne(o);
1788
1777
  (e.width !== f || e.height !== l) && (e.width = f, e.height = l);
1789
1778
  const w = [], _ = e.getContext("2d");
1790
1779
  if (_.clearRect(0, 0, f, l), t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
1791
- const v = o.sil;
1792
- _.drawImage(v, 0, 0, f, l);
1780
+ const k = o.sil;
1781
+ _.drawImage(k, 0, 0, f, l);
1793
1782
  return;
1794
1783
  }
1795
1784
  if (w.length === 0) {
1796
1785
  _.drawImage(t, 0, 0, f, l);
1797
1786
  return;
1798
1787
  }
1799
- const m = hn(t, w, f, l, a);
1788
+ const m = dn(t, w, f, l, a);
1800
1789
  _.drawImage(m, 0, 0);
1801
1790
  }
1802
- async function lt(e, t, n) {
1803
- const r = await mn(Oe ?? void 0), s = cn(r.viseme.atlasMeta), { width: i, height: o } = Fe(r);
1791
+ async function ct(e, t, n) {
1792
+ const r = await hn(Ue ?? void 0), s = an(r.viseme.atlasMeta), { width: i, height: o } = Ne(r);
1804
1793
  (e.width !== i || e.height !== o) && (e.width = i, e.height = o);
1805
1794
  const a = [];
1806
1795
  if (t.mouthPath) {
1807
- const w = new Set(r.viseme.cellByPath.keys()), _ = Nr(t.mouthPath, w) ?? (w.has(t.mouthPath) ? t.mouthPath : null);
1808
- _ && a.push(await qt(r, r.viseme, _));
1796
+ const w = new Set(r.viseme.cellByPath.keys()), _ = Ur(t.mouthPath, w) ?? (w.has(t.mouthPath) ? t.mouthPath : null);
1797
+ _ && a.push(await Ht(r, r.viseme, _));
1809
1798
  }
1810
1799
  t.expressionPath && r.expression && a.push(
1811
- await qt(r, r.expression, t.expressionPath)
1800
+ await Ht(r, r.expression, t.expressionPath)
1812
1801
  );
1813
1802
  const f = e.getContext("2d");
1814
1803
  if (f.clearRect(0, 0, i, o), a.length === 0) {
1815
1804
  f.drawImage(r.sil, 0, 0, i, o);
1816
1805
  return;
1817
1806
  }
1818
- const l = hn(
1807
+ const l = dn(
1819
1808
  r.sil,
1820
1809
  a,
1821
1810
  i,
@@ -1824,78 +1813,75 @@ async function lt(e, t, n) {
1824
1813
  );
1825
1814
  f.drawImage(l, 0, 0);
1826
1815
  }
1827
- async function gs(e) {
1828
- await lt(e, { mouthPath: null, expressionPath: null });
1816
+ async function ws(e) {
1817
+ await ct(e, { mouthPath: null, expressionPath: null });
1829
1818
  }
1830
- function _s(e) {
1819
+ function gs(e) {
1831
1820
  const t = /* @__PURE__ */ new Map();
1832
1821
  for (const n of e)
1833
1822
  t.has(n.vtime) || t.set(n.vtime, n);
1834
1823
  return [...t.values()].sort((n, r) => n.vtime - r.vtime);
1835
1824
  }
1836
- function ps(e) {
1837
- const t = _s(e), n = [];
1825
+ function _s(e) {
1826
+ const t = gs(e), n = [];
1838
1827
  for (let r = 0; r < t.length - 1; r++) {
1839
1828
  const s = t[r].viseme, i = t[r + 1].viseme, o = t[r].vtime, a = t[r + 1].vtime;
1840
1829
  s === i || a <= o || n.push({ from: s, to: i, fromVtime: o, toVtime: a });
1841
1830
  }
1842
1831
  return n;
1843
1832
  }
1844
- function ys(e, t) {
1833
+ function ps(e, t) {
1845
1834
  let n = null;
1846
1835
  for (const r of e)
1847
1836
  t >= r.fromVtime && t < r.toVtime && (n = r);
1848
1837
  return n;
1849
1838
  }
1850
- function Es(e, t) {
1839
+ function ys(e, t) {
1851
1840
  return { gapMs: Math.max(0, t - e), holdEnd: e, transStart: e };
1852
1841
  }
1853
- const wn = 400, Ss = 200, ks = 600, vs = "__sil__";
1854
- function bs() {
1842
+ const mn = 400, Es = 200, Ss = 600, wn = "__sil__";
1843
+ function ks(e) {
1844
+ return e.lastDrawnKey ? e.lastDrawnKey.split("|")[0] === wn : !1;
1845
+ }
1846
+ function vs() {
1855
1847
  return { lastDrawnKey: "" };
1856
1848
  }
1857
- function we(e) {
1849
+ function fe(e) {
1858
1850
  e.lastDrawnKey = "", e.transitionCache = void 0;
1859
1851
  }
1860
- function Ts(e, t) {
1852
+ function bs(e, t) {
1861
1853
  if (t?.transitionCache && t.transitionCache.queueLength === e.length)
1862
1854
  return t.transitionCache.transitions;
1863
- const n = ps(e);
1855
+ const n = _s(e);
1864
1856
  return t && (t.transitionCache = { queueLength: e.length, transitions: n }), n;
1865
1857
  }
1866
- function As(e, t) {
1867
- let n = "sil";
1868
- for (const r of e)
1869
- r.viseme !== "sil" && (t != null && r.vtime > t || (n = r.viseme));
1870
- return n;
1871
- }
1872
- function Is(e, t, n, r = 120) {
1873
- if (n !== "sil")
1874
- return n;
1875
- const s = Sr(e, t, r);
1876
- return s !== "sil" ? s : As(e, t);
1858
+ function Ts(e) {
1859
+ let t = "sil";
1860
+ for (const n of e)
1861
+ n.viseme !== "sil" && (t = n.viseme);
1862
+ return t;
1877
1863
  }
1878
- function Rs(e, t) {
1879
- return e === "sil" ? !1 : t ? Vr(e, "sil", t) : wt(e, "sil").length > 0;
1864
+ function As(e, t) {
1865
+ return e === "sil" ? !1 : t ? Nr(e, "sil", t) : mt(e, "sil").length > 0;
1880
1866
  }
1881
- function Ds() {
1882
- return wn + ks;
1867
+ function Is() {
1868
+ return mn + Ss;
1883
1869
  }
1884
- function jt(e) {
1885
- return e ?? vs;
1870
+ function Kt(e) {
1871
+ return e ?? wn;
1886
1872
  }
1887
- function Cs(e, t, n, r) {
1873
+ function Rs(e, t, n, r) {
1888
1874
  if (e.length === 0)
1889
1875
  return { diffPath: null, label: "sil" };
1890
- const s = Ts(e, r), i = ys(s, t);
1876
+ const s = bs(e, r), i = ps(s, t);
1891
1877
  if (i) {
1892
- const a = i.toVtime - i.fromVtime, f = Es(i.fromVtime, i.toVtime);
1878
+ const a = i.toVtime - i.fromVtime, f = ys(i.fromVtime, i.toVtime);
1893
1879
  if (t >= f.transStart && t < i.toVtime) {
1894
- let l = wt(i.from, i.to);
1895
- n && (l = un(l, n));
1896
- const w = Wr(l, a);
1880
+ let l = mt(i.from, i.to);
1881
+ n && (l = cn(l, n));
1882
+ const w = $r(l, a);
1897
1883
  if (w.length > 0) {
1898
- const _ = Hr(
1884
+ const _ = Wr(
1899
1885
  t,
1900
1886
  f.transStart,
1901
1887
  i.toVtime,
@@ -1903,26 +1889,26 @@ function Cs(e, t, n, r) {
1903
1889
  );
1904
1890
  return { diffPath: w[_] ?? w[w.length - 1], label: `${i.from}→${i.to}` };
1905
1891
  }
1906
- return { diffPath: qe(i.to), label: i.to };
1892
+ return { diffPath: Ke(i.to), label: i.to };
1907
1893
  }
1908
1894
  }
1909
- const o = ze(e, t);
1910
- return o === "sil" ? { diffPath: null, label: "sil" } : { diffPath: qe(o), label: o };
1895
+ const o = ft(e, t);
1896
+ return o === "sil" ? { diffPath: null, label: "sil" } : { diffPath: Ke(o), label: o };
1911
1897
  }
1912
1898
  function gn(e, t) {
1913
- const n = t ?? jt(e.expressionPath);
1914
- return `${jt(e.mouthPath)}|${n}`;
1899
+ const n = t ?? Kt(e.expressionPath);
1900
+ return `${Kt(e.mouthPath)}|${n}`;
1915
1901
  }
1916
- function Ms(e, t, n, r, s, i, o, a, f, l) {
1917
- const w = Cs(t, n, f, i), _ = {
1902
+ function Ds(e, t, n, r, s, i, o, a, f, l) {
1903
+ const w = Rs(t, n, f, i), _ = {
1918
1904
  mouthPath: w.diffPath,
1919
1905
  expressionPath: r
1920
1906
  }, m = gn(_, s);
1921
- m !== i.lastDrawnKey && (a(w.label), o(e, _, m, (v) => {
1922
- v === m && (i.lastDrawnKey = m);
1907
+ m !== i.lastDrawnKey && (a(w.label), o(e, _, m, (k) => {
1908
+ k === m && (i.lastDrawnKey = m);
1923
1909
  }));
1924
1910
  }
1925
- function Gt(e, t, n, r, s, i, o = null) {
1911
+ function jt(e, t, n, r, s, i, o = null) {
1926
1912
  const a = { mouthPath: null, expressionPath: t }, f = o != null ? `|idle@${Math.round(o * 1e3)}` : "", l = gn(a, n) + f;
1927
1913
  !(o != null) && l === r.lastDrawnKey || (i(
1928
1914
  o != null ? "idle" : "sil"
@@ -1966,9 +1952,9 @@ function _n(e) {
1966
1952
  }
1967
1953
  };
1968
1954
  }
1969
- const zt = -1, Yt = /* @__PURE__ */ new Map();
1970
- async function xs(e) {
1971
- let t = Yt.get(e);
1955
+ const Gt = -1, zt = /* @__PURE__ */ new Map();
1956
+ async function Ms(e) {
1957
+ let t = zt.get(e);
1972
1958
  if (!t) {
1973
1959
  const n = await fetch(e);
1974
1960
  if (!n.ok)
@@ -1976,32 +1962,32 @@ async function xs(e) {
1976
1962
  `Failed to fetch worker script (${n.status}): ${e}`
1977
1963
  );
1978
1964
  const r = await n.blob();
1979
- t = URL.createObjectURL(r), Yt.set(e, t);
1965
+ t = URL.createObjectURL(r), zt.set(e, t);
1980
1966
  }
1981
1967
  return new Worker(t, { name: "visemeDiffPreview" });
1982
1968
  }
1983
- function Ps() {
1969
+ function Cs() {
1984
1970
  return typeof Worker < "u" && typeof OffscreenCanvas < "u";
1985
1971
  }
1986
- function Bs(e, t) {
1972
+ function xs(e, t) {
1987
1973
  (e.width !== t.width || e.height !== t.height) && (e.width = t.width, e.height = t.height);
1988
1974
  const n = e.getContext("2d");
1989
1975
  n && n.drawImage(t, 0, 0);
1990
1976
  }
1991
- function Jt(e, t) {
1977
+ function Yt(e, t) {
1992
1978
  let n = 0, r = null;
1993
- const s = _n(async (i, o) => (await ut(), await lt(e, i), s.isLatestSeq(o)));
1979
+ const s = _n(async (i, o) => (await at(), await ct(e, i), s.isLatestSeq(o)));
1994
1980
  return {
1995
1981
  usesWorker: !1,
1996
1982
  async loadAssets(i) {
1997
- const o = await ut(i);
1983
+ const o = await at(i);
1998
1984
  return r = o.visemePaths ? new Set(o.visemePaths) : null, o;
1999
1985
  },
2000
1986
  getVisemeAtlasPaths() {
2001
1987
  return r;
2002
1988
  },
2003
1989
  async renderSilOnly() {
2004
- n += 1, s.reset(), await gs(e);
1990
+ n += 1, s.reset(), await ws(e);
2005
1991
  },
2006
1992
  drawLiveFrame(i, o, a, f) {
2007
1993
  s.enqueue(o, a, f);
@@ -2009,8 +1995,8 @@ function Jt(e, t) {
2009
1995
  async renderViseme(i, o) {
2010
1996
  n += 1;
2011
1997
  const a = n;
2012
- t.onStatus?.(o), await lt(i, {
2013
- mouthPath: qe(o),
1998
+ t.onStatus?.(o), await ct(i, {
1999
+ mouthPath: Ke(o),
2014
2000
  expressionPath: null
2015
2001
  });
2016
2002
  },
@@ -2019,12 +2005,12 @@ function Jt(e, t) {
2019
2005
  }
2020
2006
  };
2021
2007
  }
2022
- function Ls(e, t, n) {
2008
+ function Ps(e, t, n) {
2023
2009
  let r = null, s = 1, i = 0, o = !1, a = !1, f = null;
2024
2010
  const l = /* @__PURE__ */ new Map(), w = _n(async (p, d, I) => (l.set(d, {
2025
2011
  drawKey: I.drawKey,
2026
2012
  onDisplayed: I.onDisplayed
2027
- }), await k(
2013
+ }), await S(
2028
2014
  {
2029
2015
  type: "drawFrame",
2030
2016
  diffPath: p.mouthPath,
@@ -2034,30 +2020,30 @@ function Ls(e, t, n) {
2034
2020
  void 0,
2035
2021
  !0
2036
2022
  ), !1)), _ = (p) => {
2037
- if (p === zt) {
2038
- let S = -1, R;
2039
- for (const [q, N] of l)
2040
- q >= S && (S = q, R = N);
2041
- S >= 0 && l.delete(S), R?.onDisplayed?.(R.drawKey);
2023
+ if (p === Gt) {
2024
+ let E = -1, R;
2025
+ for (const [H, V] of l)
2026
+ H >= E && (E = H, R = V);
2027
+ E >= 0 && l.delete(E), R?.onDisplayed?.(R.drawKey);
2042
2028
  return;
2043
2029
  }
2044
2030
  const d = -p, I = l.get(d);
2045
2031
  l.delete(d), I?.onDisplayed?.(I.drawKey);
2046
- }, m = /* @__PURE__ */ new Map(), v = (p, d) => {
2032
+ }, m = /* @__PURE__ */ new Map(), k = (p, d) => {
2047
2033
  a || !r || r.postMessage(p, []);
2048
- }, k = (p, d, I = !1) => {
2034
+ }, S = (p, d, I = !1) => {
2049
2035
  if (a) return Promise.resolve();
2050
- const S = s++;
2051
- return new Promise((R, q) => {
2052
- m.set(S, {
2053
- resolve: (N) => R(N),
2054
- reject: q,
2036
+ const E = s++;
2037
+ return new Promise((R, H) => {
2038
+ m.set(E, {
2039
+ resolve: (V) => R(V),
2040
+ reject: H,
2055
2041
  generation: typeof p.generation == "number" ? p.generation : void 0,
2056
2042
  expectRenderDone: I
2057
- }), v({ ...p, requestId: S });
2043
+ }), k({ ...p, requestId: E });
2058
2044
  });
2059
2045
  }, u = (async () => {
2060
- r = await xs(n), r.onmessage = (p) => {
2046
+ r = await Ms(n), r.onmessage = (p) => {
2061
2047
  if (a) return;
2062
2048
  const d = p.data;
2063
2049
  if (d.type === "status") {
@@ -2065,23 +2051,23 @@ function Ls(e, t, n) {
2065
2051
  return;
2066
2052
  }
2067
2053
  if (d.type === "frame") {
2068
- const S = d.generation < 0;
2069
- if (!S && d.generation !== i) {
2054
+ const E = d.generation < 0;
2055
+ if (!E && d.generation !== i) {
2070
2056
  d.bitmap.close();
2071
2057
  return;
2072
2058
  }
2073
- if (S && !(d.generation === zt)) {
2074
- const q = -d.generation;
2075
- if (!w.isLatestSeq(q)) {
2059
+ if (E && !(d.generation === Gt)) {
2060
+ const H = -d.generation;
2061
+ if (!w.isLatestSeq(H)) {
2076
2062
  d.bitmap.close();
2077
2063
  return;
2078
2064
  }
2079
2065
  }
2080
- if (S && t.shouldAcceptLiveFrame && !t.shouldAcceptLiveFrame()) {
2066
+ if (E && t.shouldAcceptLiveFrame && !t.shouldAcceptLiveFrame()) {
2081
2067
  d.bitmap.close();
2082
2068
  return;
2083
2069
  }
2084
- Bs(e, d.bitmap), d.bitmap.close(), S && _(d.generation);
2070
+ xs(e, d.bitmap), d.bitmap.close(), E && _(d.generation);
2085
2071
  return;
2086
2072
  }
2087
2073
  const I = m.get(d.requestId);
@@ -2112,7 +2098,7 @@ function Ls(e, t, n) {
2112
2098
  for (const [, I] of m)
2113
2099
  I.reject(new Error(d));
2114
2100
  m.clear();
2115
- }, await k({ type: "init" }), o = !0;
2101
+ }, await S({ type: "init" }), o = !0;
2116
2102
  })();
2117
2103
  return {
2118
2104
  usesWorker: !0,
@@ -2120,10 +2106,10 @@ function Ls(e, t, n) {
2120
2106
  if (a) return { width: 842, height: 1264 };
2121
2107
  if (await u, a) return { width: 842, height: 1264 };
2122
2108
  if (!o) throw new Error("Worker init failed");
2123
- return await k({
2109
+ return await S({
2124
2110
  type: "loadAssets",
2125
2111
  keyHex: p,
2126
- urls: Qn(Se())
2112
+ urls: Qn(Ee())
2127
2113
  }) ?? { width: 842, height: 1264 };
2128
2114
  },
2129
2115
  getVisemeAtlasPaths() {
@@ -2133,26 +2119,26 @@ function Ls(e, t, n) {
2133
2119
  if (a || (await u, a)) return;
2134
2120
  i += 1, w.reset(), l.clear();
2135
2121
  const p = i;
2136
- await k({ type: "cancelLiveDraws" }), await k(
2122
+ await S({ type: "cancelLiveDraws" }), await S(
2137
2123
  { type: "renderSil", generation: p },
2138
2124
  void 0,
2139
2125
  !0
2140
2126
  );
2141
2127
  },
2142
- drawLiveFrame(p, d, I, S) {
2128
+ drawLiveFrame(p, d, I, E) {
2143
2129
  a || u.then(() => {
2144
- a || w.enqueue(d, I, S);
2130
+ a || w.enqueue(d, I, E);
2145
2131
  });
2146
2132
  },
2147
2133
  async renderViseme(p, d, I = {}) {
2148
2134
  if (a || (await u, a)) return;
2149
2135
  i += 1, w.reset(), l.clear();
2150
- const S = i;
2136
+ const E = i;
2151
2137
  try {
2152
- await k({ type: "cancelLiveDraws" }), await k(
2138
+ await S({ type: "cancelLiveDraws" }), await S(
2153
2139
  {
2154
2140
  type: "renderViseme",
2155
- generation: S,
2141
+ generation: E,
2156
2142
  to: d,
2157
2143
  from: I.from,
2158
2144
  transitionDurationMs: I.transitionDurationMs,
@@ -2177,25 +2163,25 @@ function Ls(e, t, n) {
2177
2163
  }
2178
2164
  };
2179
2165
  }
2180
- function Os(e, t = {}) {
2181
- if (!Ps())
2182
- return Jt(e, t);
2166
+ function Bs(e, t = {}) {
2167
+ if (!Cs())
2168
+ return Yt(e, t);
2183
2169
  const n = Pn(t.workerScriptUrl);
2184
2170
  try {
2185
- return Ls(e, t, n);
2171
+ return Ps(e, t, n);
2186
2172
  } catch {
2187
- return Jt(e, t);
2173
+ return Yt(e, t);
2188
2174
  }
2189
2175
  }
2190
- class Fs extends Error {
2176
+ class Ls extends Error {
2191
2177
  constructor(t, n) {
2192
2178
  super(`${t} timed out after ${Math.round(n / 1e3)}s`), this.name = "LoadTimeoutError";
2193
2179
  }
2194
2180
  }
2195
- function rt(e, t, n) {
2181
+ function tt(e, t, n) {
2196
2182
  return new Promise((r, s) => {
2197
2183
  const i = globalThis.setTimeout(() => {
2198
- s(new Fs(n, t));
2184
+ s(new Ls(n, t));
2199
2185
  }, t);
2200
2186
  e.then(
2201
2187
  (o) => {
@@ -2207,15 +2193,15 @@ function rt(e, t, n) {
2207
2193
  );
2208
2194
  });
2209
2195
  }
2210
- const Us = {
2196
+ const Os = {
2211
2197
  position: "relative",
2212
2198
  display: "inline-block",
2213
2199
  lineHeight: 0
2214
- }, Ns = {
2200
+ }, Fs = {
2215
2201
  display: "block",
2216
2202
  maxWidth: "100%",
2217
2203
  height: "auto"
2218
- }, Vs = {
2204
+ }, Us = {
2219
2205
  position: "absolute",
2220
2206
  inset: 0,
2221
2207
  display: "flex",
@@ -2227,78 +2213,78 @@ const Us = {
2227
2213
  fontSize: "0.875rem",
2228
2214
  textAlign: "center"
2229
2215
  };
2230
- function Be(e) {
2231
- return Fn(e) ? kr : rn;
2216
+ function Oe(e) {
2217
+ return Fn(e) ? Sr : nn;
2232
2218
  }
2233
- function ge(e, t) {
2219
+ function we(e, t) {
2234
2220
  return t?.isSpeaking() ? t : e.isSpeaking() ? e : t?.isConnected() ? t : e;
2235
2221
  }
2236
- const Js = vn(function({
2222
+ const zs = vn(function({
2237
2223
  id: t,
2238
2224
  avatarId: n,
2239
2225
  faceId: r,
2240
2226
  assets: s,
2241
2227
  authToken: i,
2242
- ttsEngineId: o = Ie,
2228
+ ttsEngineId: o = Te,
2243
2229
  voiceId: a,
2244
- speakingRate: f = ht,
2230
+ speakingRate: f = dt,
2245
2231
  className: l,
2246
2232
  style: w,
2247
2233
  canvasStyle: _,
2248
2234
  onStatusChange: m,
2249
- onDisplayStatus: v,
2250
- onError: k,
2235
+ onDisplayStatus: k,
2236
+ onError: S,
2251
2237
  onReady: u,
2252
2238
  showErrorOverlay: p = !0,
2253
2239
  onUserTranscript: d,
2254
2240
  onBotOutput: I,
2255
- onRealtimeLipsyncDebug: S
2241
+ onRealtimeLipsyncDebug: E
2256
2242
  }, R) {
2257
- const q = L(null), N = L(null), $ = L(bs()), z = L(os()), y = L(0), T = L(0), g = L(0), M = L(null), b = L(null), D = L(!1), A = L(!1), P = L(!1), O = L("sil"), W = L(0), G = L(!1), H = L(
2243
+ const H = F(null), V = F(null), $ = F(vs()), j = F(is()), y = F(0), T = F(0), g = F(0), C = F(null), b = F(null), D = F(!1), A = F(!1), P = F(!1), B = F("sil"), W = F(!1), U = F(
2258
2244
  null
2259
- ), Y = L(!1), [J, fe] = Pe("idle"), [ee, c] = Pe(null), [E, B] = Pe(!1), [Q, te] = Pe(!1), [ke, re] = Pe({ width: 842, height: 1264 }), ce = L(i ?? null), ue = L(null), se = L(null), Ce = L(!1), Je = L({ width: 842, height: 1264 }), Qe = L(null), yt = L(u), X = L(v), Et = L(k), St = L(d), kt = L(I), vt = L(S);
2260
- yt.current = u, X.current = v, Et.current = k, St.current = d, kt.current = I, vt.current = S;
2261
- const bt = oe(
2245
+ ), X = F(!1), [se, Z] = Le("idle"), [ce, J] = Le(null), [c, v] = Le(!1), [L, z] = Le(!1), [ne, Re] = Le({ width: 842, height: 1264 }), G = F(i ?? null), Se = F(null), Y = F(null), De = F(!1), Je = F({ width: 842, height: 1264 }), Qe = F(null), pt = F(u), ee = F(k), yt = F(S), Et = F(d), St = F(I), kt = F(E);
2246
+ pt.current = u, ee.current = k, yt.current = S, Et.current = d, St.current = I, kt.current = E;
2247
+ const vt = oe(
2262
2248
  (h) => {
2263
- const C = h?.ttsEngineId ?? o ?? se.current?.ttsEngineId ?? Ie, x = h?.voiceId ?? a ?? se.current?.voiceId ?? Be(C);
2264
- return { ttsEngineId: C, voiceId: x };
2249
+ const M = h?.ttsEngineId ?? o ?? Y.current?.ttsEngineId ?? Te, x = h?.voiceId ?? a ?? Y.current?.voiceId ?? Oe(M);
2250
+ return { ttsEngineId: M, voiceId: x };
2265
2251
  },
2266
2252
  [o, a]
2267
2253
  ), de = oe((h) => {
2268
- c(h), Et.current?.(h);
2254
+ J(h), yt.current?.(h);
2269
2255
  }, []), Me = oe(
2270
2256
  (h) => {
2271
- fe(h), m?.(h);
2257
+ Z(h), m?.(h);
2272
2258
  },
2273
2259
  [m]
2274
- ), K = L(
2275
- Lt(Me, Ae)
2276
- ), j = L(null);
2277
- be(() => (K.current = Lt(
2260
+ ), q = F(
2261
+ Bt(Me, be)
2262
+ ), K = F(null);
2263
+ ke(() => (q.current = Bt(
2278
2264
  Me,
2279
- Ae
2280
- ), i && K.current.setDeveloperToken(i), K.current.setTtsEngineId(o), () => {
2281
- K.current.stop();
2282
- const h = j.current;
2283
- h && (h.disconnect(), j.current = null);
2265
+ be
2266
+ ), i && q.current.setDeveloperToken(i), q.current.setTtsEngineId(o), () => {
2267
+ q.current.stop();
2268
+ const h = K.current;
2269
+ h && (h.disconnect(), K.current = null);
2284
2270
  }), [Me, i, o]);
2285
- const Tt = oe(() => (j.current || (j.current = Dr(
2271
+ const bt = oe(() => (K.current || (K.current = Rr(
2286
2272
  Me,
2287
2273
  {
2288
2274
  speakingRate: f,
2289
- onUserTranscript: (h, C) => St.current?.(h, C),
2290
- onBotOutput: (h) => kt.current?.(h),
2291
- onLipsyncDebug: (h) => vt.current?.(h)
2275
+ onUserTranscript: (h, M) => Et.current?.(h, M),
2276
+ onBotOutput: (h) => St.current?.(h),
2277
+ onLipsyncDebug: (h) => kt.current?.(h)
2292
2278
  }
2293
- ), j.current.setTtsEngineId(o)), j.current), [Me, f, o]), ie = oe(() => {
2294
- M.current != null && (clearTimeout(M.current), M.current = null);
2295
- }, []), le = oe(() => {
2279
+ ), K.current.setTtsEngineId(o)), K.current), [Me, f, o]), ie = oe(() => {
2280
+ C.current != null && (clearTimeout(C.current), C.current = null);
2281
+ }, []), ue = oe(() => {
2296
2282
  b.current != null && (clearTimeout(b.current), b.current = null), P.current = !1;
2297
- }, []), At = oe(() => {
2298
- !Y.current || !H.current || (le(), P.current = !0, b.current = setTimeout(() => {
2299
- if (b.current = null, ge(
2300
- K.current,
2301
- j.current
2283
+ }, []), Tt = oe(() => {
2284
+ !X.current || !U.current || (ue(), P.current = !0, b.current = setTimeout(() => {
2285
+ if (b.current = null, we(
2286
+ q.current,
2287
+ K.current
2302
2288
  ).isSpeaking()) {
2303
2289
  P.current = !1;
2304
2290
  return;
@@ -2307,435 +2293,435 @@ const Js = vn(function({
2307
2293
  P.current = !1;
2308
2294
  return;
2309
2295
  }
2310
- const h = H.current;
2296
+ const h = U.current;
2311
2297
  if (!h) {
2312
2298
  P.current = !1;
2313
2299
  return;
2314
2300
  }
2315
2301
  h.restart(), h.setActive(!0);
2316
- const C = h.getVideo();
2302
+ const M = h.getVideo();
2317
2303
  let x = !1;
2318
- const F = () => {
2319
- x || (x = !0, C.removeEventListener("playing", U), P.current = !1);
2320
- }, U = () => {
2321
- F();
2304
+ const O = () => {
2305
+ x || (x = !0, M.removeEventListener("playing", N), P.current = !1);
2306
+ }, N = () => {
2307
+ O();
2322
2308
  };
2323
- if (C.addEventListener("playing", U, { once: !0 }), C.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && !C.paused) {
2324
- F();
2309
+ if (M.addEventListener("playing", N, { once: !0 }), M.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && !M.paused) {
2310
+ O();
2325
2311
  return;
2326
2312
  }
2327
- setTimeout(F, 300);
2328
- }, Ss));
2329
- }, [le]), Ue = oe(() => {
2330
- if (ie(), X.current?.("sil"), D.current = !1, !Y.current || !H.current) {
2313
+ setTimeout(O, 300);
2314
+ }, Es));
2315
+ }, [ue]), Ce = oe(() => {
2316
+ if (ie(), ee.current?.("sil"), D.current = !1, !X.current || !U.current) {
2331
2317
  P.current = !1;
2332
2318
  return;
2333
2319
  }
2334
- At();
2335
- }, [ie, At]), Xe = oe(
2320
+ Tt();
2321
+ }, [ie, Tt]), Xe = oe(
2336
2322
  async (h) => {
2337
- const C = T.current, x = N.current, F = q.current, U = () => {
2323
+ const M = T.current, x = V.current, O = H.current, N = () => {
2338
2324
  h?.fallbackOnly || (D.current = !1);
2339
2325
  };
2340
- if (!x || !F || !E) {
2341
- U();
2326
+ if (!x || !O || !c) {
2327
+ N();
2342
2328
  return;
2343
2329
  }
2344
- if (ge(
2345
- K.current,
2346
- j.current
2330
+ if (we(
2331
+ q.current,
2332
+ K.current
2347
2333
  ).isSpeaking()) {
2348
- U();
2334
+ N();
2349
2335
  return;
2350
2336
  }
2351
2337
  if (h?.fallbackOnly) {
2352
- if (!D.current || (we($.current), await x.renderSilOnly(), C !== T.current) || ge(
2353
- K.current,
2354
- j.current
2338
+ if (!D.current || (fe($.current), await x.renderSilOnly(), M !== T.current) || we(
2339
+ q.current,
2340
+ K.current
2355
2341
  ).isSpeaking())
2356
2342
  return;
2357
- Ue();
2343
+ Ce();
2358
2344
  return;
2359
2345
  }
2360
- const Z = Is(
2361
- ge(
2362
- K.current,
2363
- j.current
2364
- ).getVisemeQueue(),
2365
- W.current,
2366
- O.current
2367
- ), ae = Rs(
2368
- Z,
2346
+ if (ks($.current)) {
2347
+ fe($.current), Ce();
2348
+ return;
2349
+ }
2350
+ const te = B.current !== "sil" ? B.current : Ts(
2351
+ we(
2352
+ q.current,
2353
+ K.current
2354
+ ).getVisemeQueue()
2355
+ ), ae = As(
2356
+ te,
2369
2357
  Qe.current
2370
2358
  );
2371
- if (we($.current), !ae) {
2372
- if (await x.renderSilOnly(), C !== T.current || ge(
2373
- K.current,
2374
- j.current
2359
+ if (fe($.current), !ae) {
2360
+ if (await x.renderSilOnly(), M !== T.current || we(
2361
+ q.current,
2362
+ K.current
2375
2363
  ).isSpeaking())
2376
2364
  return;
2377
- Ue();
2365
+ Ce();
2378
2366
  return;
2379
2367
  }
2380
2368
  try {
2381
- await x.renderViseme(F, "sil", {
2382
- from: Z,
2383
- transitionDurationMs: wn
2369
+ await x.renderViseme(O, "sil", {
2370
+ from: te,
2371
+ transitionDurationMs: mn
2384
2372
  });
2385
2373
  } catch (he) {
2386
2374
  if (he?.name !== "AbortError")
2387
2375
  throw he;
2388
2376
  }
2389
- if (C !== T.current) {
2390
- U();
2377
+ if (M !== T.current) {
2378
+ N();
2391
2379
  return;
2392
2380
  }
2393
- if (ge(
2394
- K.current,
2395
- j.current
2381
+ if (we(
2382
+ q.current,
2383
+ K.current
2396
2384
  ).isSpeaking()) {
2397
- U();
2385
+ N();
2398
2386
  return;
2399
2387
  }
2400
- Ue();
2388
+ Ce();
2401
2389
  },
2402
- [E, Ue]
2390
+ [c, Ce]
2403
2391
  ), xe = oe(() => {
2404
2392
  if (A.current || D.current) return;
2405
2393
  A.current = !0, D.current = !0, ie();
2406
2394
  const h = T.current;
2407
2395
  Xe().finally(() => {
2408
2396
  A.current = !1;
2409
- }), M.current = setTimeout(() => {
2410
- M.current = null, h === T.current && Xe({ fallbackOnly: !0 });
2411
- }, Ds());
2412
- }, [ie, Xe]), It = oe(async () => {
2397
+ }), C.current = setTimeout(() => {
2398
+ C.current = null, h === T.current && Xe({ fallbackOnly: !0 });
2399
+ }, Is());
2400
+ }, [ie, Xe]), At = oe(async () => {
2413
2401
  const h = Math.floor(Date.now() / 1e3);
2414
- if (ue.current && h < ue.current.expiresAt - 60)
2415
- return ue.current.key;
2416
- const C = ce.current ?? await nt();
2417
- ce.current = C, K.current.setDeveloperToken(C);
2418
- const x = await Kn(C, Ae);
2419
- return ue.current = {
2402
+ if (Se.current && h < Se.current.expiresAt - 60)
2403
+ return Se.current.key;
2404
+ const M = G.current ?? await et();
2405
+ G.current = M, q.current.setDeveloperToken(M);
2406
+ const x = await Kn(M, be);
2407
+ return Se.current = {
2420
2408
  key: x.value,
2421
2409
  expiresAt: x.expiresAt
2422
2410
  }, x.value;
2423
2411
  }, []);
2424
- be(() => {
2425
- i && (ce.current = i, K.current.setDeveloperToken(i));
2426
- }, [i]), be(() => {
2427
- K.current.setTtsEngineId(o), j.current?.setTtsEngineId(o), se.current && (se.current.ttsEngineId = o);
2428
- }, [o]), be(() => {
2429
- const h = a ?? Be(o);
2430
- se.current && (se.current.voiceId = h);
2412
+ ke(() => {
2413
+ i && (G.current = i, q.current.setDeveloperToken(i));
2414
+ }, [i]), ke(() => {
2415
+ q.current.setTtsEngineId(o), K.current?.setTtsEngineId(o), Y.current && (Y.current.ttsEngineId = o);
2416
+ }, [o]), ke(() => {
2417
+ const h = a ?? Oe(o);
2418
+ Y.current && (Y.current.voiceId = h);
2431
2419
  }, [a, o]);
2432
- const _e = Dt(
2420
+ const ge = Rt(
2433
2421
  () => Yn(n, r),
2434
2422
  [n, r]
2435
- ), pn = Dt(() => s ? JSON.stringify(s) : _e ? `face:${_e}` : `id:${t ?? ""}`, [s, _e, t]);
2436
- return be(() => {
2437
- if (!t && !s && !_e) {
2423
+ ), pn = Rt(() => s ? JSON.stringify(s) : ge ? `face:${ge}` : `id:${t ?? ""}`, [s, ge, t]);
2424
+ return ke(() => {
2425
+ if (!t && !s && !ge) {
2438
2426
  de("AiTwin requires `assets`, `avatarId`/`faceId`, or `id`");
2439
2427
  return;
2440
2428
  }
2441
2429
  let h = !1;
2442
- Ce.current = !1, B(!1), te(!1), c(null), se.current = null, H.current = null, Y.current = !1;
2443
- const C = q.current;
2444
- if (!C) return;
2430
+ De.current = !1, v(!1), z(!1), J(null), Y.current = null, U.current = null, X.current = !1;
2431
+ const M = H.current;
2432
+ if (!M) return;
2445
2433
  let x = null;
2446
2434
  return (async () => {
2447
2435
  try {
2448
- let F, U, Z;
2436
+ let O, N, te;
2449
2437
  if (s)
2450
- F = s, U = o, Z = a ?? Be(U);
2451
- else if (_e)
2452
- X.current?.("loadFace"), F = Mt(_e), U = o, Z = a ?? Be(U);
2438
+ O = s, N = o, te = a ?? Oe(N);
2439
+ else if (ge)
2440
+ ee.current?.("loadFace"), O = Mt(ge), N = o, te = a ?? Oe(N);
2453
2441
  else {
2454
- X.current?.("getAiTwin");
2455
- const V = await rt(
2442
+ ee.current?.("getAiTwin");
2443
+ const Q = await tt(
2456
2444
  Nn(t),
2457
2445
  3e4,
2458
2446
  "getAiTwin"
2459
2447
  );
2460
2448
  if (h) return;
2461
- if (!V.faceId?.trim())
2449
+ if (!Q.faceId?.trim())
2462
2450
  throw new Error("getAiTwin returned no faceId");
2463
- U = V.ttsEngineId, Z = a ?? V.voiceId ?? Be(U), F = Mt(V.faceId);
2451
+ N = Q.ttsEngineId, te = a ?? Q.voiceId ?? Oe(N), O = Mt(Q.faceId);
2464
2452
  }
2465
- se.current = { ttsEngineId: U, voiceId: Z }, Jn(F), H.current = as(), x = Os(C, {
2466
- onStatus: (V) => {
2467
- h || X.current?.(V);
2453
+ Y.current = { ttsEngineId: N, voiceId: te }, Jn(O), U.current = os(), x = Bs(M, {
2454
+ onStatus: (Q) => {
2455
+ h || ee.current?.(Q);
2468
2456
  },
2469
- onError: (V) => {
2470
- h || de(V);
2457
+ onError: (Q) => {
2458
+ h || de(Q);
2471
2459
  },
2472
- shouldAcceptLiveFrame: () => ge(
2473
- K.current,
2474
- j.current
2460
+ shouldAcceptLiveFrame: () => we(
2461
+ q.current,
2462
+ K.current
2475
2463
  ).isSpeaking() && !D.current
2476
- }), N.current = x, X.current?.("auth");
2477
- const ae = ce.current ?? i ?? await rt(nt(), 3e4, "getAuthToken");
2464
+ }), V.current = x, ee.current?.("auth");
2465
+ const ae = G.current ?? i ?? await tt(et(), 3e4, "getAuthToken");
2478
2466
  if (h) return;
2479
- ce.current = ae, K.current.setDeveloperToken(ae), K.current.setTtsEngineId(U);
2480
- const he = je() ? await It() : void 0;
2467
+ G.current = ae, q.current.setDeveloperToken(ae), q.current.setTtsEngineId(N);
2468
+ const he = Ge() ? await At() : void 0;
2481
2469
  if (h) return;
2482
- X.current?.("loadAssets");
2483
- const pe = await rt(
2470
+ ee.current?.("loadAssets");
2471
+ const le = await tt(
2484
2472
  x.loadAssets(he),
2485
2473
  18e4,
2486
2474
  "load face assets"
2487
2475
  );
2488
- if (!h && pe.width > 0 && pe.height > 0 && (Je.current = pe, re(pe)), h) return;
2489
- Qe.current = x.getVisemeAtlasPaths(), Ce.current = !0, B(!0), yt.current?.(), x.renderSilOnly().then(() => {
2490
- h || X.current?.("sil");
2476
+ if (!h && le.width > 0 && le.height > 0 && (Je.current = le, Re(le)), h) return;
2477
+ Qe.current = x.getVisemeAtlasPaths(), De.current = !0, v(!0), pt.current?.(), x.renderSilOnly().then(() => {
2478
+ h || ee.current?.("sil");
2491
2479
  }), (async () => {
2492
2480
  try {
2493
- const V = await Fr(he);
2494
- if (await H.current.load(V), h) return;
2495
- Y.current = !0, te(!0), we($.current), H.current.setActive(!0);
2496
- const ne = H.current.getVideo();
2497
- await Kt(
2498
- C,
2499
- ne,
2481
+ const Q = await Or(he);
2482
+ if (await U.current.load(Q), h) return;
2483
+ X.current = !0, z(!0), fe($.current), U.current.setActive(!0);
2484
+ const re = U.current.getVideo();
2485
+ await qt(
2486
+ M,
2487
+ re,
2500
2488
  null,
2501
2489
  void 0,
2502
2490
  Je.current
2503
- ), h || X.current?.("idle");
2504
- } catch (V) {
2491
+ ), h || ee.current?.("idle");
2492
+ } catch (Q) {
2505
2493
  if (h) return;
2506
- Y.current = !1, te(!1), console.warn("[AiTwin] Idle video unavailable:", V);
2494
+ X.current = !1, z(!1), console.warn("[AiTwin] Idle video unavailable:", Q);
2507
2495
  }
2508
2496
  })();
2509
- } catch (F) {
2497
+ } catch (O) {
2510
2498
  if (h) return;
2511
- if (F instanceof st) {
2512
- de(F.message);
2499
+ if (O instanceof nt) {
2500
+ de(O.message);
2513
2501
  return;
2514
2502
  }
2515
- de(F instanceof Error ? F.message : "Failed to load AI twin");
2503
+ de(O instanceof Error ? O.message : "Failed to load AI twin");
2516
2504
  }
2517
2505
  })(), () => {
2518
- h = !0, H.current?.dispose(), x?.dispose(), N.current = null;
2506
+ h = !0, U.current?.dispose(), x?.dispose(), V.current = null;
2519
2507
  };
2520
- }, [pn, s, _e, t, o, a, i, It, de]), be(() => {
2521
- const h = { current: !1 }, C = () => {
2522
- const x = q.current, F = N.current, U = ge(
2523
- K.current,
2524
- j.current
2508
+ }, [pn, s, ge, t, o, a, i, At, de]), ke(() => {
2509
+ const h = { current: !1 }, M = () => {
2510
+ const x = H.current, O = V.current, N = we(
2511
+ q.current,
2512
+ K.current
2525
2513
  );
2526
- if (!x || !F || !E) {
2527
- y.current = requestAnimationFrame(C);
2514
+ if (!x || !O || !c) {
2515
+ y.current = requestAnimationFrame(M);
2528
2516
  return;
2529
2517
  }
2530
- const Z = U.isSpeaking(), ae = Z && !G.current, he = G.current && !Z;
2531
- ae && (ie(), le(), D.current = !1, A.current = !1, we($.current), g.current = 0, O.current = "sil", W.current = 0, H.current?.setActive(!1)), he && (z.current.reset(), H.current?.setActive(!1), xe());
2532
- const pe = he || D.current || P.current;
2533
- if (Z) {
2534
- Y.current && H.current?.setActive(!1);
2535
- const V = U.getVisemeQueue(), ne = U.getPlaybackElapsedMs();
2536
- W.current = ne;
2537
- const me = V.length > 0 ? ze(V, ne) : "sil";
2538
- me !== "sil" && (O.current = me);
2539
- const ve = fs(
2540
- ne,
2541
- U.getSentenceEmotions(),
2542
- U.getWordQueue(),
2543
- U.getStreamMood()
2518
+ const te = N.isSpeaking(), ae = te && !W.current, he = W.current && !te;
2519
+ ae && (ie(), ue(), D.current = !1, A.current = !1, fe($.current), g.current = 0, B.current = "sil", U.current?.setActive(!1)), he && (j.current.reset(), U.current?.setActive(!1), xe());
2520
+ const le = N.getPlaybackElapsedMs(), Q = he || D.current || P.current;
2521
+ if (te) {
2522
+ X.current && U.current?.setActive(!1);
2523
+ const re = N.getVisemeQueue(), _e = re.length > 0 ? ft(re, le) : "sil";
2524
+ _e !== "sil" && (B.current = _e);
2525
+ const me = ls(
2526
+ le,
2527
+ N.getSentenceEmotions(),
2528
+ N.getWordQueue(),
2529
+ N.getStreamMood()
2544
2530
  );
2545
- z.current.setTargetEmotion(ve), z.current.setTargetEyebrow(
2546
- is(
2547
- ne,
2548
- U.getWordQueue(),
2549
- _t(ve)
2531
+ j.current.setTargetEmotion(me), j.current.setTargetEyebrow(
2532
+ ss(
2533
+ le,
2534
+ N.getWordQueue(),
2535
+ gt(me)
2550
2536
  )
2551
- ), z.current.advance(performance.now());
2552
- const Ze = z.current.getExpressionPath(), et = z.current.getDrawKey();
2553
- Ms(
2537
+ ), j.current.advance(performance.now());
2538
+ const Pe = j.current.getExpressionPath(), Ze = j.current.getDrawKey();
2539
+ Ds(
2554
2540
  x,
2555
- V,
2556
- ne,
2541
+ re,
2542
+ le,
2543
+ Pe,
2557
2544
  Ze,
2558
- et,
2559
2545
  $.current,
2560
- (tt, yn, En, Sn) => F.drawLiveFrame(tt, yn, En, Sn),
2561
- (tt) => X.current?.(tt),
2546
+ (Be, yn, En, Sn) => O.drawLiveFrame(Be, yn, En, Sn),
2547
+ (Be) => ee.current?.(Be),
2562
2548
  Qe.current
2563
2549
  );
2564
- } else if (!pe && Y.current && H.current) {
2565
- const V = H.current, ne = V.getVideo();
2566
- ne.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA ? (V.setActive(!0), h.current || (h.current = !0, Kt(
2550
+ } else if (!Q && X.current && U.current) {
2551
+ const re = U.current, _e = re.getVideo();
2552
+ _e.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA ? (re.setActive(!0), h.current || (h.current = !0, qt(
2567
2553
  x,
2568
- ne,
2554
+ _e,
2569
2555
  null,
2570
2556
  void 0,
2571
2557
  Je.current,
2572
- () => !U.isSpeaking() && !D.current && !P.current
2573
- ).then(() => X.current?.("idle")).finally(() => {
2558
+ () => !N.isSpeaking() && !D.current && !P.current
2559
+ ).then(() => ee.current?.("idle")).finally(() => {
2574
2560
  h.current = !1;
2575
- }))) : Gt(
2561
+ }))) : jt(
2576
2562
  x,
2577
2563
  null,
2578
2564
  "__sil__",
2579
2565
  $.current,
2580
- (me, ve, Ze, et) => F.drawLiveFrame(me, ve, Ze, et),
2581
- (me) => X.current?.(me)
2566
+ (me, Pe, Ze, Be) => O.drawLiveFrame(me, Pe, Ze, Be),
2567
+ (me) => ee.current?.(me)
2582
2568
  );
2583
- } else pe || Gt(
2569
+ } else Q || jt(
2584
2570
  x,
2585
2571
  null,
2586
2572
  "__sil__",
2587
2573
  $.current,
2588
- (V, ne, me, ve) => F.drawLiveFrame(V, ne, me, ve),
2589
- (V) => X.current?.(V)
2574
+ (re, _e, me, Pe) => O.drawLiveFrame(re, _e, me, Pe),
2575
+ (re) => ee.current?.(re)
2590
2576
  );
2591
- G.current = Z, y.current = requestAnimationFrame(C);
2577
+ W.current = te, y.current = requestAnimationFrame(M);
2592
2578
  };
2593
- return y.current = requestAnimationFrame(C), () => {
2594
- cancelAnimationFrame(y.current), ie(), le(), K.current.stop(), j.current?.stop();
2579
+ return y.current = requestAnimationFrame(M), () => {
2580
+ cancelAnimationFrame(y.current), ie(), ue(), q.current.stop(), K.current?.stop();
2595
2581
  };
2596
2582
  }, [
2597
- E,
2598
- Q,
2583
+ c,
2584
+ L,
2599
2585
  xe,
2600
2586
  ie,
2601
- le
2587
+ ue
2602
2588
  ]), bn(
2603
2589
  R,
2604
2590
  () => ({
2605
- speakText: async (h, C) => {
2591
+ speakText: async (h, M) => {
2606
2592
  const x = h.trim();
2607
2593
  if (!x) return;
2608
- const F = N.current;
2609
- if (!F || !Ce.current && !E)
2594
+ const O = V.current;
2595
+ if (!O || !De.current && !c)
2610
2596
  throw new Error("AI twin is not ready");
2611
- const { ttsEngineId: U, voiceId: Z } = bt(C);
2612
- c(null), T.current += 1, ie(), le(), D.current = !1, A.current = !1, O.current = "sil", W.current = 0, we($.current), z.current.reset(), G.current = !1;
2597
+ const { ttsEngineId: N, voiceId: te } = vt(M);
2598
+ J(null), T.current += 1, ie(), ue(), D.current = !1, A.current = !1, B.current = "sil", fe($.current), j.current.reset(), W.current = !1;
2613
2599
  try {
2614
- Y.current ? (H.current?.setActive(!1), H.current?.restart(), we($.current)) : await F.renderSilOnly(), await K.current.speak(x, {
2615
- voiceId: Z,
2616
- speakingRate: C?.speakingRate ?? f,
2617
- ttsEngineId: U
2600
+ X.current ? (U.current?.setActive(!1), U.current?.restart(), fe($.current)) : await O.renderSilOnly(), await q.current.speak(x, {
2601
+ voiceId: te,
2602
+ speakingRate: M?.speakingRate ?? f,
2603
+ ttsEngineId: N
2618
2604
  });
2619
2605
  } catch (ae) {
2620
2606
  throw de(ae instanceof Error ? ae.message : "TTS failed"), ae;
2621
2607
  }
2622
2608
  },
2623
2609
  stop: () => {
2624
- T.current += 1, ie(), le(), D.current = !1, A.current = !1, K.current.stop(), j.current?.stop(), we($.current), G.current = !1, xe();
2610
+ T.current += 1, ie(), ue(), D.current = !1, A.current = !1, q.current.stop(), K.current?.stop(), fe($.current), W.current = !1, xe();
2625
2611
  },
2626
2612
  setTtsEngineId: (h) => {
2627
- K.current.setTtsEngineId(h), j.current?.setTtsEngineId(h), se.current && (se.current.ttsEngineId = h);
2613
+ q.current.setTtsEngineId(h), K.current?.setTtsEngineId(h), Y.current && (Y.current.ttsEngineId = h);
2628
2614
  },
2629
- renderViseme: async (h, C) => {
2630
- const x = N.current, F = q.current;
2631
- if (!x || !F || !Ce.current)
2615
+ renderViseme: async (h, M) => {
2616
+ const x = V.current, O = H.current;
2617
+ if (!x || !O || !De.current)
2632
2618
  throw new Error("AI twin is not ready");
2633
- await x.renderViseme(F, h, {
2634
- from: C?.from,
2635
- transitionDurationMs: C?.transitionDurationMs,
2636
- gapMs: C?.gapMs,
2637
- onStatus: (U) => X.current?.(U)
2619
+ await x.renderViseme(O, h, {
2620
+ from: M?.from,
2621
+ transitionDurationMs: M?.transitionDurationMs,
2622
+ gapMs: M?.gapMs,
2623
+ onStatus: (N) => ee.current?.(N)
2638
2624
  });
2639
2625
  },
2640
- isReady: () => Ce.current,
2641
- getStatus: () => J,
2626
+ isReady: () => De.current,
2627
+ getStatus: () => se,
2642
2628
  connect: async (h = {}) => {
2643
- c(null);
2629
+ J(null);
2644
2630
  try {
2645
- let C = h.authToken?.trim();
2646
- C || (C = ce.current ?? i ?? await nt(), ce.current = C);
2647
- const x = h.voiceId?.trim() ?? a ?? se.current?.voiceId, F = Tt();
2648
- await F.unlockAudio(), await F.connect({
2631
+ let M = h.authToken?.trim();
2632
+ M || (M = G.current ?? i ?? await et(), G.current = M);
2633
+ const x = h.voiceId?.trim() ?? a ?? Y.current?.voiceId, O = bt();
2634
+ await O.unlockAudio(), await O.connect({
2649
2635
  ...h,
2650
- authToken: C,
2636
+ authToken: M,
2651
2637
  voiceId: x,
2652
2638
  speakingRate: h.speakingRate ?? f
2653
2639
  });
2654
- } catch (C) {
2640
+ } catch (M) {
2655
2641
  throw de(
2656
- C instanceof Error ? C.message : "Voice connect failed"
2657
- ), C;
2642
+ M instanceof Error ? M.message : "Voice connect failed"
2643
+ ), M;
2658
2644
  }
2659
2645
  },
2660
2646
  disconnect: async () => {
2661
- const h = j.current;
2662
- h && (T.current += 1, ie(), le(), D.current = !1, A.current = !1, we($.current), G.current = !1, await h.disconnect(), xe());
2647
+ const h = K.current;
2648
+ h && (T.current += 1, ie(), ue(), D.current = !1, A.current = !1, fe($.current), W.current = !1, await h.disconnect(), xe());
2663
2649
  },
2664
- isConnected: () => j.current?.isConnected() ?? !1
2650
+ isConnected: () => K.current?.isConnected() ?? !1
2665
2651
  }),
2666
2652
  [
2667
- E,
2653
+ c,
2668
2654
  f,
2669
2655
  i,
2670
2656
  a,
2671
2657
  ie,
2672
- le,
2658
+ ue,
2673
2659
  xe,
2674
2660
  de,
2661
+ vt,
2675
2662
  bt,
2676
- Tt,
2677
- J
2663
+ se
2678
2664
  ]
2679
- ), /* @__PURE__ */ kn("div", { className: l, style: { ...Us, ...w }, children: [
2680
- /* @__PURE__ */ Rt(
2665
+ ), /* @__PURE__ */ kn("div", { className: l, style: { ...Os, ...w }, children: [
2666
+ /* @__PURE__ */ It(
2681
2667
  "canvas",
2682
2668
  {
2683
- ref: q,
2684
- width: ke.width,
2685
- height: ke.height,
2686
- style: { ...Ns, ..._ },
2669
+ ref: H,
2670
+ width: ne.width,
2671
+ height: ne.height,
2672
+ style: { ...Fs, ..._ },
2687
2673
  "aria-label": t ? `AI twin ${t}` : "AI twin face"
2688
2674
  }
2689
2675
  ),
2690
- p && ee ? /* @__PURE__ */ Rt("div", { style: Vs, children: ee }) : null
2676
+ p && ce ? /* @__PURE__ */ It("div", { style: Us, children: ce }) : null
2691
2677
  ] });
2692
- }), $s = "thumbnail.webp";
2693
- function Qs(e, t = ft) {
2678
+ }), Ns = "thumbnail.webp";
2679
+ function Ys(e, t = ut) {
2694
2680
  const n = e.trim();
2695
2681
  if (!n)
2696
2682
  throw new Error("faceId is required");
2697
- return `${t.replace(/\/$/, "")}/${n}/${$s}`;
2683
+ return `${t.replace(/\/$/, "")}/${n}/${Ns}`;
2698
2684
  }
2699
- const Ws = /* @__PURE__ */ new Set([".png", ".jpg", ".jpeg", ".webp"]);
2700
- function Hs(e = ".png") {
2685
+ const Vs = /* @__PURE__ */ new Set([".png", ".jpg", ".jpeg", ".webp"]);
2686
+ function $s(e = ".png") {
2701
2687
  const t = e.toLowerCase();
2702
- return Ws.has(t) ? `source${t}` : "source.png";
2688
+ return Vs.has(t) ? `source${t}` : "source.png";
2703
2689
  }
2704
- function Xs(e, t = ft, n = ".png") {
2690
+ function Js(e, t = ut, n = ".png") {
2705
2691
  const r = e.trim();
2706
2692
  if (!r)
2707
2693
  throw new Error("faceId is required");
2708
- return `${t.replace(/\/$/, "")}/${r}/${Hs(n)}`;
2694
+ return `${t.replace(/\/$/, "")}/${r}/${$s(n)}`;
2709
2695
  }
2710
2696
  export {
2711
- $s as AI_TWIN_THUMBNAIL_FILENAME,
2712
- Js as AiTwin,
2713
- st as AiTwinNotFoundError,
2714
- Ie as DEFAULT_TTS_ENGINE_ID,
2715
- sn as MIC_SAMPLE_RATE,
2697
+ Ns as AI_TWIN_THUMBNAIL_FILENAME,
2698
+ zs as AiTwin,
2699
+ nt as AiTwinNotFoundError,
2700
+ Te as DEFAULT_TTS_ENGINE_ID,
2701
+ rn as MIC_SAMPLE_RATE,
2716
2702
  sr as OCULUS_VISEME_IDS,
2717
- on as PLAYBACK_SAMPLE_RATE,
2718
- nn as SPEAKING_RATE_MAX,
2719
- tn as SPEAKING_RATE_MIN,
2720
- Re as TTS_ENGINE_CARTESIA,
2721
- Xt as TTS_ENGINE_GOOGLE,
2722
- dt as TTS_ENGINE_INWORLD,
2723
- Ys as VISEME_IDS,
2724
- kr as VISEME_TEST_CARTESIA_VOICE_ID,
2725
- ht as VISEME_TEST_SPEAKING_RATE,
2726
- rn as VISEME_TEST_VOICE_ID,
2727
- Hs as aiTwinSourceFilename,
2728
- Ir as buildVoiceWebSocketUrl,
2729
- Lt as createAvatarTtsLipsyncController,
2730
- Dr as createRealtimeVoiceLipsyncController,
2703
+ sn as PLAYBACK_SAMPLE_RATE,
2704
+ tn as SPEAKING_RATE_MAX,
2705
+ en as SPEAKING_RATE_MIN,
2706
+ Ae as TTS_ENGINE_CARTESIA,
2707
+ Qt as TTS_ENGINE_GOOGLE,
2708
+ lt as TTS_ENGINE_INWORLD,
2709
+ Gs as VISEME_IDS,
2710
+ Sr as VISEME_TEST_CARTESIA_VOICE_ID,
2711
+ dt as VISEME_TEST_SPEAKING_RATE,
2712
+ nn as VISEME_TEST_VOICE_ID,
2713
+ $s as aiTwinSourceFilename,
2714
+ Ar as buildVoiceWebSocketUrl,
2715
+ Bt as createAvatarTtsLipsyncController,
2716
+ Rr as createRealtimeVoiceLipsyncController,
2731
2717
  Mt as faceAssetUrls,
2732
2718
  Nn as fetchAiTwin,
2733
- nt as fetchDevAuthToken,
2734
- Xs as getAiTwinSourceUrl,
2735
- Qs as getAiTwinThumbnailUrl,
2719
+ et as fetchDevAuthToken,
2720
+ Js as getAiTwinSourceUrl,
2721
+ Ys as getAiTwinThumbnailUrl,
2736
2722
  or as normalizeOculusViseme,
2737
2723
  Yn as resolveDirectFaceId,
2738
- ze as resolveVisemeAtTime,
2739
- zs as resolveWordAtTime,
2724
+ ft as resolveVisemeAtTime,
2725
+ js as resolveWordAtTime,
2740
2726
  Jn as setActiveTwinAssets
2741
2727
  };