@streamoji/aitwin 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/aitwin.js CHANGED
@@ -1,73 +1,73 @@
1
- import { jsxs as It, jsx as Qe } from "react/jsx-runtime";
2
- import { forwardRef as Tt, useRef as B, useState as me, useCallback as ne, useEffect as oe, useMemo as At, useImperativeHandle as bt } from "react";
3
- const Rt = "https://pub-607ad1fc22e2400eb57d17240aab857c.r2.dev", dt = {
4
- publicBase: Rt
5
- }, Dt = "0.1.7", xt = {
6
- version: Dt
7
- }, Ct = {}, Re = "https://ai.streamoji.com", Mt = `${dt.publicBase}/custom-faces`, Ft = `${dt.publicBase}/aitwin-workers/v${xt.version}/visemeDiffPreview.worker.js`;
8
- function Ot(e) {
1
+ import { jsxs as Bt, jsx as it } from "react/jsx-runtime";
2
+ import { forwardRef as Nt, useRef as R, useState as ke, useCallback as ie, useEffect as Te, useMemo as Vt, useImperativeHandle as $t } from "react";
3
+ const Ht = "https://pub-607ad1fc22e2400eb57d17240aab857c.r2.dev", St = {
4
+ publicBase: Ht
5
+ }, Wt = "0.1.8", qt = {
6
+ version: Wt
7
+ }, Kt = {}, Pe = "https://ai.streamoji.com", jt = `${St.publicBase}/custom-faces`, Gt = `${St.publicBase}/aitwin-workers/v${qt.version}/visemeDiffPreview.worker.js`;
8
+ function zt(e) {
9
9
  if (e) return e;
10
- const t = typeof import.meta < "u" ? Ct?.VITE_AITWIN_WORKER_URL : void 0;
11
- return t?.trim() ? t.trim() : Ft;
12
- }
13
- const Pt = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin", ht = "eng_a7f2b9c1", Ke = "eng_d4e8f3a2", pe = "eng_c9b1e6d4", ce = pe, Lt = /* @__PURE__ */ new Set([
14
- Ke,
15
- pe
16
- ]), mt = {
17
- [ht]: "oculus_direct",
18
- [pe]: "oculus_direct",
19
- [Ke]: "inworld_word"
20
- }, Je = {
21
- google: ht,
22
- inworld: Ke,
23
- cartesia: pe
10
+ const t = typeof import.meta < "u" ? Kt?.VITE_AITWIN_WORKER_URL : void 0;
11
+ return t?.trim() ? t.trim() : Gt;
12
+ }
13
+ const Qt = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin", kt = "eng_a7f2b9c1", Ze = "eng_d4e8f3a2", be = "eng_c9b1e6d4", pe = be, Jt = /* @__PURE__ */ new Set([
14
+ Ze,
15
+ be
16
+ ]), Tt = {
17
+ [kt]: "oculus_direct",
18
+ [be]: "oculus_direct",
19
+ [Ze]: "inworld_word"
20
+ }, st = {
21
+ google: kt,
22
+ inworld: Ze,
23
+ cartesia: be
24
24
  };
25
- function Ut(e) {
26
- return mt[e];
25
+ function Yt(e) {
26
+ return Tt[e];
27
27
  }
28
- function Bt(e) {
29
- return e === pe;
28
+ function Xt(e) {
29
+ return e === be;
30
30
  }
31
- function Nt(e) {
32
- if (e.ttsEngineId && e.ttsEngineId in mt)
31
+ function Zt(e) {
32
+ if (e.ttsEngineId && e.ttsEngineId in Tt)
33
33
  return e.ttsEngineId;
34
34
  const t = e.tts?.toLowerCase();
35
- return t && t in Je ? Je[t] : ce;
35
+ return t && t in st ? st[t] : pe;
36
36
  }
37
- class Ue extends Error {
37
+ class Ke extends Error {
38
38
  constructor(t = "AI twin not found") {
39
39
  super(t), this.name = "AiTwinNotFoundError";
40
40
  }
41
41
  }
42
- async function $t(e, t = Pt) {
42
+ async function en(e, t = Qt) {
43
43
  const n = new URL(t);
44
44
  n.searchParams.set("id", e);
45
45
  const r = await fetch(n.toString());
46
46
  if (r.status === 404)
47
- throw new Ue();
47
+ throw new Ke();
48
48
  if (!r.ok)
49
49
  throw new Error(`getAiTwin failed (${r.status})`);
50
50
  const i = await r.json();
51
51
  if (!i.success)
52
- throw new Ue(i.error ?? "AI twin not found");
52
+ throw new Ke(i.error ?? "AI twin not found");
53
53
  const { tts: s, ...c } = i.data;
54
54
  return {
55
55
  ...c,
56
- ttsEngineId: Nt(i.data)
56
+ ttsEngineId: Zt(i.data)
57
57
  };
58
58
  }
59
- const Vt = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken", Ht = "client_6TNvp3SCs4Og0a1ijm9TommXLql1", Wt = "kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1", qt = "6TNvp3SCs4Og0a1ijm9TommXLql1", Kt = "Swaraj Mali";
60
- async function Ye() {
61
- const e = await fetch(Vt, {
59
+ const tn = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken", nn = "client_6TNvp3SCs4Og0a1ijm9TommXLql1", rn = "kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1", sn = "6TNvp3SCs4Og0a1ijm9TommXLql1", on = "Swaraj Mali";
60
+ async function ot() {
61
+ const e = await fetch(tn, {
62
62
  method: "POST",
63
63
  headers: {
64
64
  "Content-Type": "application/json",
65
- "Client-Id": Ht,
66
- "Client-Secret": Wt
65
+ "Client-Id": nn,
66
+ "Client-Secret": rn
67
67
  },
68
68
  body: JSON.stringify({
69
- userId: qt,
70
- userName: Kt
69
+ userId: sn,
70
+ userName: on
71
71
  })
72
72
  });
73
73
  if (!e.ok)
@@ -77,7 +77,7 @@ async function Ye() {
77
77
  throw new Error("getAuthToken returned no token");
78
78
  return n;
79
79
  }
80
- async function jt(e, t = Re) {
80
+ async function an(e, t = Pe) {
81
81
  const n = await fetch(`${t.replace(/\/$/, "")}/api/session-value`, {
82
82
  headers: { Authorization: `Bearer ${e}` }
83
83
  });
@@ -87,7 +87,7 @@ async function jt(e, t = Re) {
87
87
  if (!r.value) throw new Error("Session value response missing value");
88
88
  return r;
89
89
  }
90
- function Gt(e) {
90
+ function cn(e) {
91
91
  const t = e.trim();
92
92
  if (t.length % 2 !== 0) throw new Error("Invalid hex key");
93
93
  const n = new Uint8Array(t.length / 2);
@@ -95,20 +95,20 @@ function Gt(e) {
95
95
  n[r / 2] = Number.parseInt(t.slice(r, r + 2), 16);
96
96
  return n;
97
97
  }
98
- function Ie(e) {
98
+ function Ce(e) {
99
99
  return Uint8Array.from(e);
100
100
  }
101
- function zt(e, t) {
101
+ function un(e, t) {
102
102
  const n = new Uint8Array(e.length + t.length);
103
103
  return n.set(e, 0), n.set(t, e.length), n;
104
104
  }
105
- async function Qt(e, t) {
105
+ async function ln(e, t) {
106
106
  if (!t) throw new Error("Asset key is required for encrypted assets");
107
107
  if (e.byteLength < 28)
108
108
  throw new Error("Encrypted payload too small");
109
- const n = new Uint8Array(e), r = Ie(n.subarray(0, 12)), i = Ie(n.subarray(12, 28)), s = Ie(n.subarray(28)), c = Ie(zt(s, i)), a = await crypto.subtle.importKey(
109
+ const n = new Uint8Array(e), r = Ce(n.subarray(0, 12)), i = Ce(n.subarray(12, 28)), s = Ce(n.subarray(28)), c = Ce(un(s, i)), a = await crypto.subtle.importKey(
110
110
  "raw",
111
- Gt(t),
111
+ cn(t),
112
112
  { name: "AES-GCM" },
113
113
  !1,
114
114
  ["decrypt"]
@@ -119,17 +119,17 @@ async function Qt(e, t) {
119
119
  c
120
120
  );
121
121
  }
122
- let Be = null;
123
- const Ne = /* @__PURE__ */ new Set();
124
- function Jt(e, t = Mt) {
122
+ let je = null;
123
+ const Ge = /* @__PURE__ */ new Set();
124
+ function fn(e, t = jt) {
125
125
  const n = `${t.replace(/\/$/, "")}/${e}`;
126
126
  return { twinBase: n, binBase: n, encrypted: !0 };
127
127
  }
128
- function Yt(e) {
129
- Be = e;
130
- for (const t of Ne) t();
128
+ function dn(e) {
129
+ je = e;
130
+ for (const t of Ge) t();
131
131
  }
132
- function Xt(e, t) {
132
+ function hn(e, t) {
133
133
  const n = typeof globalThis.location < "u" ? globalThis.location.href : void 0;
134
134
  if (!n) return e;
135
135
  const r = (i) => {
@@ -145,38 +145,38 @@ function Xt(e, t) {
145
145
  encrypted: e.encrypted
146
146
  };
147
147
  }
148
- function ie() {
149
- if (!Be)
148
+ function he() {
149
+ if (!je)
150
150
  throw new Error("Twin assets not configured; call setActiveTwinAssets first");
151
- return Be;
151
+ return je;
152
152
  }
153
- function Zt(e) {
154
- return Ne.add(e), () => Ne.delete(e);
153
+ function mn(e) {
154
+ return Ge.add(e), () => Ge.delete(e);
155
155
  }
156
- function Xe() {
157
- const { binBase: e, twinBase: t, encrypted: n } = ie();
158
- return n ? `${e}/sil.bin` : `${t}/sil.png`;
156
+ function at() {
157
+ const { encrypted: e, binBase: t, twinBase: n } = he();
158
+ return e ? `${t}/sil.bin` : `${n}/sil.png`;
159
159
  }
160
- function en() {
161
- const { binBase: e, twinBase: t, encrypted: n } = ie();
162
- return n ? `${e}/idle.bin` : `${t}/idle.mp4`;
160
+ function _n() {
161
+ const { encrypted: e, binBase: t, twinBase: n } = he();
162
+ return e ? `${t}/idle.bin` : `${n}/idle.mp4`;
163
163
  }
164
- function tn() {
165
- const { encrypted: e, binBase: t, twinBase: n } = ie();
164
+ function wn() {
165
+ const { encrypted: e, binBase: t, twinBase: n } = he();
166
166
  return e ? t : n;
167
167
  }
168
- function je() {
169
- return ie().encrypted;
168
+ function Be() {
169
+ return he().encrypted;
170
170
  }
171
- function nn() {
172
- const { binBase: e, twinBase: t, encrypted: n } = ie();
171
+ function gn() {
172
+ const { binBase: e, twinBase: t, encrypted: n } = he();
173
173
  return n ? `${e}/atlas.bin` : `${t}/atlas.json`;
174
174
  }
175
- function rn() {
176
- const { binBase: e, twinBase: t, encrypted: n } = ie();
175
+ function yn() {
176
+ const { binBase: e, twinBase: t, encrypted: n } = he();
177
177
  return n ? `${e}/expression_atlas.bin` : `${t}/expression_atlas.json`;
178
178
  }
179
- function sn(e) {
179
+ function pn(e) {
180
180
  const t = e.split(/\r?\n/);
181
181
  let n = "", r = "";
182
182
  for (const s of t)
@@ -191,7 +191,7 @@ function sn(e) {
191
191
  }
192
192
  return { event: n, data: i };
193
193
  }
194
- const on = [
194
+ const En = [
195
195
  "aa",
196
196
  "CH",
197
197
  "DD",
@@ -207,14 +207,14 @@ const on = [
207
207
  "kk",
208
208
  "nn",
209
209
  "sil"
210
- ], an = new Map(
211
- on.map((e) => [e.toLowerCase(), e])
210
+ ], Sn = new Map(
211
+ En.map((e) => [e.toLowerCase(), e])
212
212
  );
213
- function cn(e) {
213
+ function kn(e) {
214
214
  const t = (e ?? "").trim();
215
- return t ? an.get(t.toLowerCase()) ?? "sil" : "sil";
215
+ return t ? Sn.get(t.toLowerCase()) ?? "sil" : "sil";
216
216
  }
217
- const un = {
217
+ const Tn = {
218
218
  aei: ["aa", "E", "I"],
219
219
  o: ["O", "U"],
220
220
  ee: ["I"],
@@ -226,12 +226,12 @@ const un = {
226
226
  qw: ["U", "O"],
227
227
  cdgknstxyz: ["DD", "SS", "kk", "CH"]
228
228
  };
229
- function ln(e) {
229
+ function vn(e) {
230
230
  if (!e) return ["sil"];
231
231
  const t = e.toLowerCase();
232
- return un[t] ?? ["sil"];
232
+ return Tn[t] ?? ["sil"];
233
233
  }
234
- function fn(e, t) {
234
+ function In(e, t) {
235
235
  const n = e.word.length;
236
236
  if (n <= 0) return 0;
237
237
  const r = e.wduration;
@@ -239,33 +239,33 @@ function fn(e, t) {
239
239
  const i = Math.max(0, Math.min(1, (t - e.wtime) / r));
240
240
  return Math.min(n - 1, Math.floor(i * n));
241
241
  }
242
- function xe(e, t) {
242
+ function Ne(e, t) {
243
243
  const n = Math.max(0, Math.min(e.length - 1, t));
244
244
  return e[n] ?? "";
245
245
  }
246
- function dn(e, t) {
247
- const n = e.toLowerCase(), r = xe(n, t), i = n.slice(Math.max(0, t - 1), t + 2);
246
+ function An(e, t) {
247
+ const n = e.toLowerCase(), r = Ne(n, t), i = n.slice(Math.max(0, t - 1), t + 2);
248
248
  return r === "i" || r === "y" ? "I" : i.includes("ee") || r === "e" || n.includes("ea") ? "E" : "aa";
249
249
  }
250
- function hn(e, t) {
250
+ function bn(e, t) {
251
251
  const n = e.toLowerCase(), r = n.slice(
252
252
  Math.max(0, t - 1),
253
253
  Math.min(n.length, t + 3)
254
254
  );
255
255
  if (/oo|ou|uw/.test(r)) return "U";
256
- const i = xe(n, t);
256
+ const i = Ne(n, t);
257
257
  return i === "u" || i === "w" ? "U" : "O";
258
258
  }
259
- function mn(e, t) {
259
+ function Rn(e, t) {
260
260
  const n = e.toLowerCase(), r = n.slice(Math.max(0, t - 2), t + 1);
261
261
  if (/qu/.test(r)) return "U";
262
262
  const i = n.slice(
263
263
  Math.max(0, t - 1),
264
264
  Math.min(n.length, t + 3)
265
265
  );
266
- return /wo|wh/.test(i) || xe(n, t) === "o" ? "O" : "U";
266
+ return /wo|wh/.test(i) || Ne(n, t) === "o" ? "O" : "U";
267
267
  }
268
- const Te = {
268
+ const Fe = {
269
269
  d: "DD",
270
270
  t: "DD",
271
271
  n: "DD",
@@ -277,46 +277,46 @@ const Te = {
277
277
  x: "CH",
278
278
  j: "CH"
279
279
  };
280
- function _n(e, t) {
281
- const n = e.toLowerCase(), r = xe(n, t);
282
- if (Te[r]) return Te[r];
280
+ function Dn(e, t) {
281
+ const n = e.toLowerCase(), r = Ne(n, t);
282
+ if (Fe[r]) return Fe[r];
283
283
  for (let i = 0; i < n.length; i++) {
284
284
  const s = n[i];
285
- if (Te[s]) return Te[s];
285
+ if (Fe[s]) return Fe[s];
286
286
  }
287
287
  return "DD";
288
288
  }
289
- function wn(e, t, n) {
290
- const r = ln(e);
289
+ function xn(e, t, n) {
290
+ const r = vn(e);
291
291
  if (r.length === 1) return r[0];
292
292
  const i = t.trim();
293
293
  if (!i) return r[0];
294
294
  const s = (e ?? "").toLowerCase(), c = Math.max(0, n);
295
295
  switch (s) {
296
296
  case "aei":
297
- return dn(i, c);
297
+ return An(i, c);
298
298
  case "o":
299
- return hn(i, c);
299
+ return bn(i, c);
300
300
  case "qw":
301
- return mn(i, c);
301
+ return Rn(i, c);
302
302
  case "cdgknstxyz":
303
- return _n(i, c);
303
+ return Dn(i, c);
304
304
  default:
305
305
  return r[0];
306
306
  }
307
307
  }
308
- function gn(e) {
308
+ function Mn(e) {
309
309
  let t = e.trim();
310
310
  const n = t.indexOf(",");
311
311
  t.startsWith("data:") && n >= 0 && (t = t.slice(n + 1)), t = t.replace(/\s+/g, "").replace(/-/g, "+").replace(/_/g, "/");
312
312
  const r = t.length % 4;
313
313
  return r !== 0 && (t += "=".repeat(4 - r)), t;
314
314
  }
315
- const yn = 24e3;
316
- function pn(e) {
315
+ const Cn = 24e3;
316
+ function Fn(e) {
317
317
  return e.length >= 4 && e[0] === 82 && e[1] === 73 && e[2] === 70 && e[3] === 70;
318
318
  }
319
- function Ze(e, t, n = yn) {
319
+ function ct(e, t, n = Cn) {
320
320
  const r = t.byteOffset % 2 === 0 ? t : t.slice(), i = new Int16Array(
321
321
  r.buffer,
322
322
  r.byteOffset,
@@ -327,15 +327,15 @@ function Ze(e, t, n = yn) {
327
327
  const c = e.createBuffer(1, s.length, n);
328
328
  return c.copyToChannel(s, 0), c;
329
329
  }
330
- function En(e) {
331
- const t = gn(e), n = window.atob(t), r = new Uint8Array(n.length);
330
+ function Ln(e) {
331
+ const t = Mn(e), n = window.atob(t), r = new Uint8Array(n.length);
332
332
  for (let i = 0; i < n.length; i++)
333
333
  r[i] = n.charCodeAt(i);
334
334
  return r;
335
335
  }
336
- async function kn(e, t) {
337
- const n = En(t);
338
- if (pn(n)) {
336
+ async function On(e, t) {
337
+ const n = Ln(t);
338
+ if (Fn(n)) {
339
339
  const r = n.buffer.slice(
340
340
  n.byteOffset,
341
341
  n.byteOffset + n.byteLength
@@ -343,7 +343,7 @@ async function kn(e, t) {
343
343
  return e.decodeAudioData(r);
344
344
  }
345
345
  if (n.length >= 2 && n.length % 2 === 0)
346
- return Ze(e, n);
346
+ return ct(e, n);
347
347
  try {
348
348
  const r = n.buffer.slice(
349
349
  n.byteOffset,
@@ -351,265 +351,265 @@ async function kn(e, t) {
351
351
  );
352
352
  return await e.decodeAudioData(r);
353
353
  } catch {
354
- return Ze(e, n);
354
+ return ct(e, n);
355
355
  }
356
356
  }
357
- function Sn(e, t) {
357
+ function vt(e, t) {
358
358
  let n = null;
359
359
  for (const r of e)
360
360
  t < r.vtime || t >= r.vtime + r.vduration || (!n || r.vtime >= n.vtime) && (n = r);
361
361
  return n ? n.viseme : "sil";
362
362
  }
363
- function vn(e, t) {
363
+ function Pn(e, t) {
364
364
  for (const n of e)
365
365
  if (t >= n.wtime && t < n.wtime + n.wduration)
366
366
  return n;
367
367
  return null;
368
368
  }
369
- function et(e) {
369
+ function ut(e) {
370
370
  let t = 0;
371
371
  for (const n of e)
372
372
  t = Math.max(t, n.vtime + n.vduration);
373
373
  return t;
374
374
  }
375
- const In = 0.5, Tn = 1.5, _t = "Olivia", An = "f786b574-daa5-4673-aa0c-cbe3e8534c02", wt = 0.85;
376
- function bn(e, t = Re) {
375
+ const Un = 0.5, Bn = 1.5, It = "Olivia", Nn = "f786b574-daa5-4673-aa0c-cbe3e8534c02", At = 0.85;
376
+ function Vn(e, t = Pe) {
377
377
  const n = `${t.replace(/\/$/, "")}/avatar_ttsWithPoses`, r = [], i = [], s = [];
378
378
  let c = "neutral";
379
379
  const a = [], l = [];
380
- let d = null, _ = !1, m = !1, S = !1, o = !1, D = 0, w = 0, u = 0, g = 0, p = null, A = 1, V = "", y = ce, E = ce, x = !1;
381
- const R = /* @__PURE__ */ new Set(), W = () => {
382
- for (const h of l)
383
- h.playbackRate.value = A;
384
- }, q = () => R.forEach((h) => h()), ue = () => {
385
- p != null && (clearTimeout(p), p = null);
386
- }, Ee = () => {
387
- for (const h of l)
380
+ let d = null, _ = !1, m = !1, S = !1, o = !1, F = 0, w = 0, u = 0, g = 0, y = null, v = 1, H = "", h = pe, E = pe, D = !1;
381
+ const L = /* @__PURE__ */ new Set(), z = () => {
382
+ for (const p of l)
383
+ p.playbackRate.value = v;
384
+ }, U = () => L.forEach((p) => p()), ae = () => {
385
+ y != null && (clearTimeout(y), y = null);
386
+ }, me = () => {
387
+ for (const p of l)
388
388
  try {
389
- h.stop();
389
+ p.stop();
390
390
  } catch {
391
391
  }
392
392
  l.length = 0;
393
- }, Z = () => {
394
- x = !0, ue(), Ee(), a.length = 0, _ = !1, m = !1, S = !1, o = !1, r.length = 0, i.length = 0, s.length = 0, c = "neutral", u = 0, g = 0, w = 0, e("idle"), q();
395
- }, K = () => {
396
- !o || _ || l.length > 0 || a.length > 0 || (m = !1, ue(), e("done"), q());
397
- }, ke = () => {
398
- const h = et(r);
399
- if (h <= 0) {
400
- e("done"), q();
393
+ }, N = () => {
394
+ D = !0, ae(), me(), a.length = 0, _ = !1, m = !1, S = !1, o = !1, r.length = 0, i.length = 0, s.length = 0, c = "neutral", u = 0, g = 0, w = 0, e("idle"), U();
395
+ }, X = () => {
396
+ !o || _ || l.length > 0 || a.length > 0 || (m = !1, ae(), e("done"), U());
397
+ }, Re = () => {
398
+ const p = ut(r);
399
+ if (p <= 0) {
400
+ e("done"), U();
401
401
  return;
402
402
  }
403
- m = !0, D = performance.now(), e("speaking"), q(), ue();
404
- const C = A > 0 ? h / A : h;
405
- p = setTimeout(() => {
406
- x || (m = !1, p = null, e("done"), q());
407
- }, C);
408
- }, Fe = (h, C, N) => {
409
- h.length !== 0 && (N && (u = C), h.forEach((O) => {
410
- const L = String(O.word ?? "").trim();
411
- if (!L) return;
412
- const Q = Math.round((O.start ?? 0) * 1e3), $ = Math.round((O.duration ?? 0) * 1e3);
403
+ m = !0, F = performance.now(), e("speaking"), U(), ae();
404
+ const V = v > 0 ? p / v : p;
405
+ y = setTimeout(() => {
406
+ D || (m = !1, y = null, e("done"), U());
407
+ }, V);
408
+ }, He = (p, V, W) => {
409
+ p.length !== 0 && (W && (u = V), p.forEach((x) => {
410
+ const I = String(x.word ?? "").trim();
411
+ if (!I) return;
412
+ const Q = Math.round((x.start ?? 0) * 1e3), J = Math.round((x.duration ?? 0) * 1e3);
413
413
  i.push({
414
- word: L,
414
+ word: I,
415
415
  wtime: u + Q,
416
- wduration: $,
416
+ wduration: J,
417
417
  queueIndex: i.length
418
418
  });
419
419
  }));
420
- }, le = (h, C, N, O) => {
421
- h.length !== 0 && (N && (u = C), h.forEach((v) => {
422
- const L = String(v.symbol ?? "").trim();
423
- if (!L) return;
424
- const Q = Math.round((v.start ?? 0) * 1e3), $ = Math.max(
420
+ }, De = (p, V, W, x) => {
421
+ p.length !== 0 && (W && (u = V), p.forEach((M) => {
422
+ const I = String(M.symbol ?? "").trim();
423
+ if (!I) return;
424
+ const Q = Math.round((M.start ?? 0) * 1e3), J = Math.max(
425
425
  1,
426
- Math.round((v.duration ?? 0) * 1e3)
426
+ Math.round((M.duration ?? 0) * 1e3)
427
427
  ), P = u + Q;
428
- let M;
429
- if (Ut(O) === "oculus_direct")
430
- M = cn(L);
428
+ let O;
429
+ if (Yt(x) === "oculus_direct")
430
+ O = kn(I);
431
431
  else {
432
- const F = vn(i, P), J = F ? fn(F, P) : 0;
433
- M = wn(
434
- L,
435
- F?.word ?? "",
436
- J
432
+ const q = Pn(i, P), ce = q ? In(q, P) : 0;
433
+ O = xn(
434
+ I,
435
+ q?.word ?? "",
436
+ ce
437
437
  );
438
438
  }
439
439
  r.push({
440
- viseme: M,
440
+ viseme: O,
441
441
  weight: 1,
442
442
  vtime: P,
443
- vduration: $
443
+ vduration: J
444
444
  });
445
- }), q());
446
- }, se = (h, C, N, O, v) => {
447
- Fe(C, N, O), le(h, N, O, v);
448
- }, Se = async (h, C, N, O) => {
449
- if (x || !h?.trim()) {
450
- (C.length > 0 || N.length > 0) && !S && se(
451
- C,
452
- N,
445
+ }), U());
446
+ }, ue = (p, V, W, x, M) => {
447
+ He(V, W, x), De(p, W, x, M);
448
+ }, se = async (p, V, W, x) => {
449
+ if (D || !p?.trim()) {
450
+ (V.length > 0 || W.length > 0) && !S && ue(
451
+ V,
452
+ W,
453
453
  u,
454
- O,
454
+ x,
455
455
  E
456
456
  );
457
457
  return;
458
458
  }
459
459
  if (_) {
460
460
  a.push({
461
- audio: h,
462
- visemes: C,
463
- words: N,
464
- isNewSegment: O
461
+ audio: p,
462
+ visemes: V,
463
+ words: W,
464
+ isNewSegment: x
465
465
  });
466
466
  return;
467
467
  }
468
468
  _ = !0;
469
469
  try {
470
- const v = window.AudioContext || window.webkitAudioContext, L = d ?? new v();
471
- L.state === "suspended" && await L.resume(), d = L;
472
- const Q = await kn(L, h);
470
+ const M = window.AudioContext || window.webkitAudioContext, I = d ?? new M();
471
+ I.state === "suspended" && await I.resume(), d = I;
472
+ const Q = await On(I, p);
473
473
  S = !0;
474
- const $ = L.currentTime;
474
+ const J = I.currentTime;
475
475
  let P = g;
476
- const M = !m;
477
- P < $ && (P = $ + 0.1), g = P + Q.duration;
478
- const F = L.createBufferSource();
479
- if (F.buffer = Q, F.playbackRate.value = A, F.connect(L.destination), l.push(F), x) {
476
+ const O = !m;
477
+ P < J && (P = J + 0.1), g = P + Q.duration;
478
+ const q = I.createBufferSource();
479
+ if (q.buffer = Q, q.playbackRate.value = v, q.connect(I.destination), l.push(q), D) {
480
480
  l.pop();
481
481
  return;
482
482
  }
483
- if (M) {
483
+ if (O) {
484
484
  m = !0, e("speaking"), w = P, u = 0, r.length = 0, i.length = 0;
485
- const H = (P - $) * 1e3;
486
- D = performance.now() + H;
485
+ const $ = (P - J) * 1e3;
486
+ F = performance.now() + $;
487
487
  }
488
- F.onended = () => {
489
- const H = l.indexOf(F);
490
- H >= 0 && l.splice(H, 1), K(), q();
491
- }, F.start(P);
492
- const J = (P - w) * 1e3;
493
- se(
494
- C,
495
- N,
496
- J,
497
- O,
488
+ q.onended = () => {
489
+ const $ = l.indexOf(q);
490
+ $ >= 0 && l.splice($, 1), X(), U();
491
+ }, q.start(P);
492
+ const ce = (P - w) * 1e3;
493
+ ue(
494
+ V,
495
+ W,
496
+ ce,
497
+ x,
498
498
  E
499
499
  );
500
- } catch (v) {
501
- console.error("[avatarTtsLipsync] audio chunk failed:", v);
500
+ } catch (M) {
501
+ console.error("[avatarTtsLipsync] audio chunk failed:", M);
502
502
  } finally {
503
503
  _ = !1;
504
- const v = a.shift();
505
- v ? await Se(
506
- v.audio,
507
- v.visemes,
508
- v.words,
509
- v.isNewSegment
510
- ) : K();
504
+ const M = a.shift();
505
+ M ? await se(
506
+ M.audio,
507
+ M.visemes,
508
+ M.words,
509
+ M.isNewSegment
510
+ ) : X();
511
511
  }
512
- }, ee = () => {
512
+ }, xe = () => {
513
513
  if (o = !0, !S && r.length > 0) {
514
- ke();
514
+ Re();
515
515
  return;
516
516
  }
517
- K();
517
+ X();
518
518
  };
519
519
  return {
520
- setDeveloperToken: (h) => {
521
- V = h;
520
+ setDeveloperToken: (p) => {
521
+ H = p;
522
522
  },
523
- setTtsEngineId: (h) => {
524
- y = h;
523
+ setTtsEngineId: (p) => {
524
+ h = p;
525
525
  },
526
- getTtsEngineId: () => y,
527
- speak: async (h, C) => {
528
- Z(), x = !1, e("loading");
529
- const N = V.trim();
530
- if (!N)
526
+ getTtsEngineId: () => h,
527
+ speak: async (p, V) => {
528
+ N(), D = !1, e("loading");
529
+ const W = H.trim();
530
+ if (!W)
531
531
  throw new Error("Developer token required for avatar_ttsWithPoses");
532
- const O = C?.ttsEngineId ?? y ?? ce;
533
- E = O;
534
- const v = C?.voiceId ?? _t, L = C?.speakingRate ?? wt, Q = Math.max(
535
- In,
536
- Math.min(Tn, L)
537
- ), $ = {
538
- user_query: h,
539
- ttsEngineId: O,
532
+ const x = V?.ttsEngineId ?? h ?? pe;
533
+ E = x;
534
+ const M = V?.voiceId ?? It, I = V?.speakingRate ?? At, Q = Math.max(
535
+ Un,
536
+ Math.min(Bn, I)
537
+ ), J = {
538
+ user_query: p,
539
+ ttsEngineId: x,
540
540
  speakingRate: Q
541
541
  };
542
- Lt.has(O) && ($.voice_id = v);
542
+ Jt.has(x) && (J.voice_id = M);
543
543
  try {
544
544
  const P = await fetch(n, {
545
545
  method: "POST",
546
546
  headers: {
547
547
  "Content-Type": "application/json",
548
- Authorization: `Bearer ${N}`
548
+ Authorization: `Bearer ${W}`
549
549
  },
550
- body: JSON.stringify($)
550
+ body: JSON.stringify(J)
551
551
  });
552
552
  if (!P.ok)
553
553
  throw new Error(`avatar_ttsWithPoses failed (${P.status})`);
554
- const M = P.body;
555
- if (!M) throw new Error("No response body");
556
- const F = M.getReader(), J = new TextDecoder();
557
- let H = "";
558
- const de = async (he) => {
559
- const f = sn(he);
560
- if (f) {
561
- if (f.event === "audio") {
562
- const k = f.data.chunk, I = f.data.visemes ?? [], T = f.data.words ?? [], b = f.data.is_new_segment ?? !1;
563
- if (k)
564
- await Se(k, I, T, b);
565
- else if (I.length > 0 || T.length > 0) {
566
- const j = b && r.length > 0 ? et(r) : u;
567
- se(I, T, j, b, O);
554
+ const O = P.body;
555
+ if (!O) throw new Error("No response body");
556
+ const q = O.getReader(), ce = new TextDecoder();
557
+ let $ = "";
558
+ const Me = async (B) => {
559
+ const b = pn(B);
560
+ if (b) {
561
+ if (b.event === "audio") {
562
+ const K = b.data.chunk, te = b.data.visemes ?? [], Z = b.data.words ?? [], j = b.data.is_new_segment ?? !1;
563
+ if (K)
564
+ await se(K, te, Z, j);
565
+ else if (te.length > 0 || Z.length > 0) {
566
+ const _e = j && r.length > 0 ? ut(r) : u;
567
+ ue(te, Z, _e, j, x);
568
568
  }
569
- } else if (f.event === "metadata") {
570
- const k = f.data.mood;
571
- typeof k == "string" && k.trim() && (c = k.trim());
572
- const I = f.data.sentence_emotions;
573
- if (Array.isArray(I)) {
569
+ } else if (b.event === "metadata") {
570
+ const K = b.data.mood;
571
+ typeof K == "string" && K.trim() && (c = K.trim());
572
+ const te = b.data.sentence_emotions;
573
+ if (Array.isArray(te)) {
574
574
  s.length = 0;
575
- for (const T of I) {
576
- if (!T || typeof T != "object") continue;
577
- const b = T;
575
+ for (const Z of te) {
576
+ if (!Z || typeof Z != "object") continue;
577
+ const j = Z;
578
578
  s.push({
579
- sentence_index: Number(b.sentence_index ?? 0),
580
- text: String(b.text ?? ""),
581
- sentiment: typeof b.sentiment == "string" ? b.sentiment : void 0,
582
- emotion: String(b.emotion ?? c),
583
- start_word: Number(b.start_word ?? 0),
584
- end_word: Number(b.end_word ?? 0)
579
+ sentence_index: Number(j.sentence_index ?? 0),
580
+ text: String(j.text ?? ""),
581
+ sentiment: typeof j.sentiment == "string" ? j.sentiment : void 0,
582
+ emotion: String(j.emotion ?? c),
583
+ start_word: Number(j.start_word ?? 0),
584
+ end_word: Number(j.end_word ?? 0)
585
585
  });
586
586
  }
587
587
  }
588
- q();
589
- } else if (f.event === "error")
588
+ U();
589
+ } else if (b.event === "error")
590
590
  throw new Error(
591
- String(f.data.message ?? "avatar_ttsWithPoses stream error")
591
+ String(b.data.message ?? "avatar_ttsWithPoses stream error")
592
592
  );
593
593
  }
594
594
  };
595
595
  for (; ; ) {
596
- const { done: he, value: f } = await F.read();
597
- f && (H += J.decode(f, { stream: !0 }));
598
- const k = H.split(`
596
+ const { done: B, value: b } = await q.read();
597
+ b && ($ += ce.decode(b, { stream: !0 }));
598
+ const K = $.split(`
599
599
 
600
600
  `);
601
- H = k.pop() ?? "";
602
- for (const I of k) await de(I);
603
- if (he) {
604
- H.trim() && await de(H.trim()), x || ee();
601
+ $ = K.pop() ?? "";
602
+ for (const te of K) await Me(te);
603
+ if (B) {
604
+ $.trim() && await Me($.trim()), D || xe();
605
605
  break;
606
606
  }
607
607
  }
608
608
  } catch (P) {
609
- throw console.error("[avatarTtsLipsync]", P), Z(), e("error"), P;
609
+ throw console.error("[avatarTtsLipsync]", P), N(), e("error"), P;
610
610
  }
611
611
  },
612
- stop: Z,
612
+ stop: N,
613
613
  getVisemeQueue: () => r,
614
614
  getWordQueue: () => i,
615
615
  getSentenceEmotions: () => s,
@@ -617,22 +617,22 @@ function bn(e, t = Re) {
617
617
  getPlaybackElapsedMs: () => m ? S && d && w > 0 ? Math.max(
618
618
  0,
619
619
  (d.currentTime - w) * 1e3
620
- ) : Math.max(0, (performance.now() - D) * A) : 0,
620
+ ) : Math.max(0, (performance.now() - F) * v) : 0,
621
621
  isSpeaking: () => m,
622
- setPlaybackSpeed: (h) => {
623
- A = Math.max(0.1, Math.min(1, h)), W();
622
+ setPlaybackSpeed: (p) => {
623
+ v = Math.max(0.1, Math.min(1, p)), z();
624
624
  },
625
- getPlaybackSpeed: () => A,
626
- subscribe: (h) => (R.add(h), () => R.delete(h))
625
+ getPlaybackSpeed: () => v,
626
+ subscribe: (p) => (L.add(p), () => L.delete(p))
627
627
  };
628
628
  }
629
- function Rn(e, t, n) {
629
+ function $n(e, t, n) {
630
630
  return {
631
631
  width: e.crop?.source_width ?? t,
632
632
  height: e.crop?.source_height ?? n
633
633
  };
634
634
  }
635
- function Dn(e, t) {
635
+ function Hn(e, t) {
636
636
  const n = e.crop;
637
637
  return {
638
638
  x: t?.source_x ?? n?.x ?? 0,
@@ -641,21 +641,21 @@ function Dn(e, t) {
641
641
  height: t?.source_h ?? n?.height ?? e.frame_height ?? 0
642
642
  };
643
643
  }
644
- function xn(e) {
644
+ function Wn(e) {
645
645
  return e ? (e.w ?? 0) > 0 && (e.h ?? 0) > 0 : !1;
646
646
  }
647
- async function Ce(e, t) {
647
+ async function Ve(e, t) {
648
648
  const n = await fetch(e);
649
649
  if (!n.ok) throw new Error(`Failed to load encrypted asset: ${e}`);
650
650
  const r = await n.arrayBuffer();
651
- return Qt(r, t);
651
+ return ln(r, t);
652
652
  }
653
- async function Cn(e, t) {
653
+ async function qn(e, t) {
654
654
  if (!t) throw new Error("Missing asset key for encrypted JSON");
655
- const n = await Ce(e, t), r = new TextDecoder().decode(n);
655
+ const n = await Ve(e, t), r = new TextDecoder().decode(n);
656
656
  return JSON.parse(r);
657
657
  }
658
- async function Mn(e) {
658
+ async function Kn(e) {
659
659
  const t = new Blob([e], { type: "image/webp" }), n = URL.createObjectURL(t);
660
660
  return new Promise((r, i) => {
661
661
  const s = new Image();
@@ -666,7 +666,7 @@ async function Mn(e) {
666
666
  }, s.src = n;
667
667
  });
668
668
  }
669
- async function Fn(e) {
669
+ async function jn(e) {
670
670
  const t = new Blob([e], { type: "image/png" }), n = URL.createObjectURL(t);
671
671
  return new Promise((r, i) => {
672
672
  const s = new Image();
@@ -677,17 +677,21 @@ async function Fn(e) {
677
677
  }, s.src = n;
678
678
  });
679
679
  }
680
- async function On(e) {
681
- const t = en();
682
- if (!je()) return t;
680
+ function Gn(e) {
681
+ const t = new Blob([e], { type: "video/mp4" });
682
+ return URL.createObjectURL(t);
683
+ }
684
+ async function zn(e) {
685
+ const t = _n();
686
+ if (!Be()) return t;
683
687
  if (!e) throw new Error("Missing asset key for encrypted idle video");
684
- const n = await Ce(t, e);
685
- return URL.createObjectURL(new Blob([n], { type: "video/mp4" }));
688
+ const n = await Ve(t, e);
689
+ return Gn(n);
686
690
  }
687
- function gt(e, t) {
691
+ function bt(e, t) {
688
692
  return `${e}/${t}_minus_sil.png`;
689
693
  }
690
- const Ge = 15, Dr = [
694
+ const et = 15, Gr = [
691
695
  "aa",
692
696
  "E",
693
697
  "I",
@@ -702,13 +706,13 @@ const Ge = 15, Dr = [
702
706
  "RR",
703
707
  "kk",
704
708
  "nn"
705
- ], $e = [
709
+ ], ze = [
706
710
  "t01_0.17",
707
711
  "t02_0.33",
708
712
  "t03_0.50",
709
713
  "t04_0.67",
710
714
  "t05_0.83"
711
- ], Pn = $e.length, tt = /* @__PURE__ */ new Set([
715
+ ], Qn = ze.length, lt = /* @__PURE__ */ new Set([
712
716
  "aa__kk",
713
717
  "aa__nn",
714
718
  "aa__sil",
@@ -815,59 +819,59 @@ const Ge = 15, Dr = [
815
819
  "U__nn",
816
820
  "U__sil"
817
821
  ]);
818
- function nt(e, t) {
822
+ function ft(e, t) {
819
823
  return `${e}__${t}`;
820
824
  }
821
- function rt(e, t) {
825
+ function dt(e, t) {
822
826
  return `pairs/${e}/${t}_minus_sil.png`;
823
827
  }
824
- function Ve(e) {
828
+ function Qe(e) {
825
829
  return `${e}_minus_sil.png`;
826
830
  }
827
- function Ln(e, t) {
831
+ function Rt(e, t) {
828
832
  if (e === t) return [];
829
- const n = [], r = nt(e, t);
830
- if (tt.has(r)) {
831
- for (const s of $e)
832
- n.push(rt(r, s));
833
+ const n = [], r = ft(e, t);
834
+ if (lt.has(r)) {
835
+ for (const s of ze)
836
+ n.push(dt(r, s));
833
837
  return n;
834
838
  }
835
- const i = nt(t, e);
836
- if (tt.has(i)) {
837
- for (let s = Pn - 1; s >= 0; s--)
838
- n.push(rt(i, $e[s]));
839
+ const i = ft(t, e);
840
+ if (lt.has(i)) {
841
+ for (let s = Qn - 1; s >= 0; s--)
842
+ n.push(dt(i, ze[s]));
839
843
  return n;
840
844
  }
841
845
  return [];
842
846
  }
843
- function Un(e) {
847
+ function Jn(e) {
844
848
  return e < 16 ? [] : e < 35 ? [2] : e < 51 ? [0, 4] : e < 67 ? [0, 2, 4] : e < 83 ? [0, 1, 2, 3] : [0, 1, 2, 3, 4];
845
849
  }
846
- function Bn(e, t) {
847
- return Un(t).map((r) => e[r]).filter((r) => r != null);
850
+ function Yn(e, t) {
851
+ return Jn(t).map((r) => e[r]).filter((r) => r != null);
848
852
  }
849
- function Nn(e, t, n, r) {
853
+ function Xn(e, t, n, r) {
850
854
  if (r <= 0) return 0;
851
855
  const s = Math.max(1, n - t) / r, c = e - t, a = Math.floor(c / s);
852
856
  return Math.min(r - 1, Math.max(0, a));
853
857
  }
854
- function $n(e, t, n) {
858
+ function Zn(e, t, n) {
855
859
  if (!n) return `${e}/${t}`;
856
860
  const r = t.split("/").pop() ?? t, i = r.includes(".") ? r.slice(0, r.lastIndexOf(".")) : r;
857
861
  return `${e}/${i}.bin`;
858
862
  }
859
- const Vn = [0.32, 0.24, 0.16, 0.08, 0], Me = Vn.map(
863
+ const er = [0.32, 0.24, 0.16, 0.08, 0], $e = er.map(
860
864
  (e) => `eyes_${e.toFixed(2)}`
861
- ), Hn = [
865
+ ), tr = [
862
866
  "eyes_0.32",
863
867
  "eyes_0.16",
864
868
  "eyes_0.00"
865
- ], Wn = [
869
+ ], nr = [
866
870
  "eyes_0.32",
867
871
  "eyes_0.16",
868
872
  "eyes_0.08",
869
873
  "eyes_0.00"
870
- ], qn = {
874
+ ], rr = {
871
875
  fear: "fear",
872
876
  afraid: "fear",
873
877
  anxiety: "fear",
@@ -890,67 +894,67 @@ const Vn = [0.32, 0.24, 0.16, 0.08, 0], Me = Vn.map(
890
894
  disgust: "disgust",
891
895
  disgusted: "disgust"
892
896
  };
893
- function ae(e, t = "neutral") {
897
+ function ye(e, t = "neutral") {
894
898
  const n = String(e ?? "").trim().toLowerCase();
895
- return n ? qn[n] ?? t : t;
899
+ return n ? rr[n] ?? t : t;
896
900
  }
897
- function z(e) {
898
- return e === "neutral" ? null : gt(e, "eyes_standard");
901
+ function ee(e) {
902
+ return e === "neutral" ? null : bt(e, "eyes_standard");
899
903
  }
900
- function He(e, t) {
901
- return !t || t === "eyes_standard" ? z(e) : e === "neutral" && t === "eyes_standard" ? null : gt(e, t);
904
+ function Je(e, t) {
905
+ return !t || t === "eyes_standard" ? ee(e) : e === "neutral" && t === "eyes_standard" ? null : bt(e, t);
902
906
  }
903
- function Kn(e) {
907
+ function ir(e) {
904
908
  if (!e) return "open";
905
909
  const t = e.match(/\/(eyes_[^/]+)_minus_sil\.png$/);
906
910
  if (!t?.[1]) return "open";
907
911
  const n = t[1];
908
- return Me.includes(n) ? n : "open";
912
+ return $e.includes(n) ? n : "open";
909
913
  }
910
- function jn(e) {
911
- return e === "open" ? -1 : Me.indexOf(e);
914
+ function sr(e) {
915
+ return e === "open" ? -1 : $e.indexOf(e);
912
916
  }
913
- function Gn(e, t) {
917
+ function or(e, t) {
914
918
  const n = [];
915
919
  for (const r of t)
916
- n.push(He(e, r));
920
+ n.push(Je(e, r));
917
921
  for (let r = t.length - 2; r >= 0; r--)
918
- n.push(He(e, t[r]));
919
- return n.push(z(e)), n;
922
+ n.push(Je(e, t[r]));
923
+ return n.push(ee(e)), n;
920
924
  }
921
- function zn(e, t) {
922
- if (t === "open") return [z(e)];
923
- const n = jn(t);
924
- if (n < 0) return [z(e)];
925
+ function ar(e, t) {
926
+ if (t === "open") return [ee(e)];
927
+ const n = sr(t);
928
+ if (n < 0) return [ee(e)];
925
929
  const r = [];
926
930
  for (let i = n; i >= 0; i--)
927
- r.push(He(e, Me[i]));
928
- return r.push(z(e)), r;
931
+ r.push(Je(e, $e[i]));
932
+ return r.push(ee(e)), r;
929
933
  }
930
- const Qn = [
934
+ const cr = [
931
935
  "t01_0.20",
932
936
  "t02_0.40",
933
937
  "t03_0.60",
934
938
  "t04_0.80"
935
939
  ];
936
- function Jn(e, t) {
940
+ function ur(e, t) {
937
941
  return `${e}__${t}`;
938
942
  }
939
- function Yn(e, t, n) {
940
- return `pairs/${Jn(e, t)}/${n}_minus_sil.png`;
943
+ function lr(e, t, n) {
944
+ return `pairs/${ur(e, t)}/${n}_minus_sil.png`;
941
945
  }
942
- function Xn(e, t) {
943
- return Qn.map(
944
- (n) => Yn(e, t, n)
946
+ function fr(e, t) {
947
+ return cr.map(
948
+ (n) => lr(e, t, n)
945
949
  );
946
950
  }
947
- function we(e, t) {
951
+ function ve(e, t) {
948
952
  return e + Math.random() * (t - e);
949
953
  }
950
- function Ae() {
951
- return we(14, 32);
954
+ function Le() {
955
+ return ve(14, 32);
952
956
  }
953
- function Zn(e = {}) {
957
+ function dr(e = {}) {
954
958
  const {
955
959
  initialDelayMs: t = 800 + Math.random() * 1200,
956
960
  intervalMinMs: n = 2200,
@@ -965,99 +969,99 @@ function Zn(e = {}) {
965
969
  let _ = "neutral", m = "neutral", S = 0, o = {
966
970
  kind: "idle",
967
971
  nextBlinkAt: performance.now() + t
968
- }, D = null, w = "__eyes_open__|neutral";
969
- function u(y, E) {
970
- D = y, w = `${y ?? "__eyes_open__"}|${E}`;
972
+ }, F = null, w = "__eyes_open__|neutral";
973
+ function u(h, E) {
974
+ F = h, w = `${h ?? "__eyes_open__"}|${E}`;
971
975
  }
972
- function g(y, E) {
976
+ function g(h, E) {
973
977
  o = {
974
978
  kind: "idle",
975
- nextBlinkAt: y + we(n, r)
976
- }, m = E, u(z(E), E);
979
+ nextBlinkAt: h + ve(n, r)
980
+ }, m = E, u(ee(E), E);
977
981
  }
978
- function p(y, E, x) {
979
- const R = x ? Hn : Math.random() < a ? Me : Wn;
982
+ function y(h, E, D) {
983
+ const L = D ? tr : Math.random() < a ? $e : nr;
980
984
  o = {
981
985
  kind: "playing",
982
- paths: Gn(E, R),
986
+ paths: or(E, L),
983
987
  index: 0,
984
- holdUntil: y + Ae(),
988
+ holdUntil: h + Le(),
985
989
  after: "idle",
986
990
  blinkEmotion: E
987
991
  }, m = E, u(o.paths[0] ?? null, E);
988
992
  }
989
- function A(y, E, x) {
990
- const R = Kn(D), W = [];
991
- if (R !== "open") {
992
- const q = zn(E, R);
993
- W.push(...q.slice(0, -1));
993
+ function v(h, E, D) {
994
+ const L = ir(F), z = [];
995
+ if (L !== "open") {
996
+ const U = ar(E, L);
997
+ z.push(...U.slice(0, -1));
994
998
  }
995
- W.push(...Xn(E, x)), W.push(z(x)), o = {
999
+ z.push(...fr(E, D)), z.push(ee(D)), o = {
996
1000
  kind: "emotionBlend",
997
- paths: W,
1001
+ paths: z,
998
1002
  index: 0,
999
- holdUntil: y + Ae(),
1000
- thenEmotion: x
1001
- }, u(W[0] ?? null, E);
1003
+ holdUntil: h + Le(),
1004
+ thenEmotion: D
1005
+ }, u(z[0] ?? null, E);
1002
1006
  }
1003
- function V(y) {
1007
+ function H(h) {
1004
1008
  if (o.kind === "emotionBlend") {
1005
- for (; y >= o.holdUntil && o.index < o.paths.length - 1; )
1006
- o.index += 1, o.holdUntil = y + Ae();
1009
+ for (; h >= o.holdUntil && o.index < o.paths.length - 1; )
1010
+ o.index += 1, o.holdUntil = h + Le();
1007
1011
  const E = o.paths[o.index] ?? null;
1008
- u(E, o.thenEmotion), y >= o.holdUntil && o.index >= o.paths.length - 1 && (_ = o.thenEmotion, m = o.thenEmotion, u(z(o.thenEmotion), o.thenEmotion), o = {
1012
+ u(E, o.thenEmotion), h >= o.holdUntil && o.index >= o.paths.length - 1 && (_ = o.thenEmotion, m = o.thenEmotion, u(ee(o.thenEmotion), o.thenEmotion), o = {
1009
1013
  kind: "idle",
1010
- nextBlinkAt: y + l
1014
+ nextBlinkAt: h + l
1011
1015
  });
1012
1016
  return;
1013
1017
  }
1014
1018
  if (o.kind === "idle") {
1015
- u(z(m), m), y >= o.nextBlinkAt && p(y, m, !1);
1019
+ u(ee(m), m), h >= o.nextBlinkAt && y(h, m, !1);
1016
1020
  return;
1017
1021
  }
1018
1022
  if (o.kind === "doublePause") {
1019
- u(z(o.blinkEmotion), o.blinkEmotion), y >= o.resumeAt && p(y, o.blinkEmotion, !0);
1023
+ u(ee(o.blinkEmotion), o.blinkEmotion), h >= o.resumeAt && y(h, o.blinkEmotion, !0);
1020
1024
  return;
1021
1025
  }
1022
1026
  if (o.kind === "playing") {
1023
- for (; y >= o.holdUntil && o.index < o.paths.length - 1; )
1024
- o.index += 1, o.holdUntil = y + Ae();
1025
- if (u(o.paths[o.index] ?? null, o.blinkEmotion), y >= o.holdUntil && o.index >= o.paths.length - 1) {
1027
+ for (; h >= o.holdUntil && o.index < o.paths.length - 1; )
1028
+ o.index += 1, o.holdUntil = h + Le();
1029
+ if (u(o.paths[o.index] ?? null, o.blinkEmotion), h >= o.holdUntil && o.index >= o.paths.length - 1) {
1026
1030
  if (o.after === "resumeIdle") {
1027
- g(y + l, _);
1031
+ g(h + l, _);
1028
1032
  return;
1029
1033
  }
1030
1034
  if (o.after === "doublePause") {
1031
1035
  o = {
1032
1036
  kind: "doublePause",
1033
- resumeAt: y + we(s, c),
1037
+ resumeAt: h + ve(s, c),
1034
1038
  blinkEmotion: o.blinkEmotion
1035
- }, u(z(o.blinkEmotion), o.blinkEmotion);
1039
+ }, u(ee(o.blinkEmotion), o.blinkEmotion);
1036
1040
  return;
1037
1041
  }
1038
1042
  if (o.after === "idle" && Math.random() < i) {
1039
1043
  o = {
1040
1044
  kind: "doublePause",
1041
- resumeAt: y + we(s, c),
1045
+ resumeAt: h + ve(s, c),
1042
1046
  blinkEmotion: o.blinkEmotion
1043
- }, u(z(o.blinkEmotion), o.blinkEmotion);
1047
+ }, u(ee(o.blinkEmotion), o.blinkEmotion);
1044
1048
  return;
1045
1049
  }
1046
- g(y, o.blinkEmotion);
1050
+ g(h, o.blinkEmotion);
1047
1051
  }
1048
1052
  }
1049
1053
  }
1050
1054
  return {
1051
- advance: V,
1052
- setTargetEmotion(y) {
1053
- const E = ae(y, _);
1055
+ advance: H,
1056
+ setTargetEmotion(h) {
1057
+ const E = ye(h, _);
1054
1058
  if (E === _ && o.kind !== "emotionBlend") return;
1055
- const x = performance.now();
1056
- if (d > 0 && E !== _ && o.kind !== "emotionBlend" && !(_ === "neutral" && E !== "neutral") && x - S < d)
1059
+ const D = performance.now();
1060
+ if (d > 0 && E !== _ && o.kind !== "emotionBlend" && !(_ === "neutral" && E !== "neutral") && D - S < d)
1057
1061
  return;
1058
- const R = _;
1059
- if (_ = E, S = x, o.kind === "playing") {
1060
- A(x, m, E);
1062
+ const L = _;
1063
+ if (_ = E, S = D, o.kind === "playing") {
1064
+ v(D, m, E);
1061
1065
  return;
1062
1066
  }
1063
1067
  if (o.kind === "emotionBlend") {
@@ -1067,20 +1071,20 @@ function Zn(e = {}) {
1067
1071
  };
1068
1072
  return;
1069
1073
  }
1070
- A(x, R, E);
1074
+ v(D, L, E);
1071
1075
  },
1072
1076
  getTargetEmotion: () => _,
1073
- getExpressionPath: () => D,
1077
+ getExpressionPath: () => F,
1074
1078
  getDrawKey: () => w,
1075
1079
  reset() {
1076
1080
  _ = "neutral", m = "neutral", S = 0, g(
1077
- performance.now() + we(n, r),
1081
+ performance.now() + ve(n, r),
1078
1082
  "neutral"
1079
1083
  );
1080
1084
  }
1081
1085
  };
1082
1086
  }
1083
- function er() {
1087
+ function hr() {
1084
1088
  const e = document.createElement("video");
1085
1089
  e.muted = !0, e.playsInline = !0, e.preload = "auto", e.setAttribute("playsinline", ""), e.loop = !0;
1086
1090
  let t = !1, n = null, r = "";
@@ -1123,17 +1127,17 @@ function er() {
1123
1127
  }
1124
1128
  };
1125
1129
  }
1126
- const tr = 50, yt = 400;
1127
- function nr(e, t, n) {
1130
+ const mr = 50, Dt = 400;
1131
+ function _r(e, t, n) {
1128
1132
  return e.filter((r) => {
1129
1133
  const i = r.queueIndex ?? -1;
1130
1134
  return i >= t && i <= n;
1131
1135
  });
1132
1136
  }
1133
- function rr(e, t) {
1137
+ function wr(e, t) {
1134
1138
  const n = [];
1135
1139
  for (const r of e) {
1136
- const i = nr(
1140
+ const i = _r(
1137
1141
  t,
1138
1142
  r.start_word,
1139
1143
  r.end_word
@@ -1142,30 +1146,30 @@ function rr(e, t) {
1142
1146
  const s = i[0], c = i[i.length - 1];
1143
1147
  n.push({
1144
1148
  sentence: r,
1145
- startMs: Math.max(0, s.wtime - tr),
1146
- endMs: c.wtime + c.wduration + yt
1149
+ startMs: Math.max(0, s.wtime - mr),
1150
+ endMs: c.wtime + c.wduration + Dt
1147
1151
  });
1148
1152
  }
1149
1153
  return n.sort((r, i) => r.startMs - i.startMs);
1150
1154
  }
1151
- function ir(e, t, n, r = "neutral") {
1152
- const i = ae(r);
1155
+ function gr(e, t, n, r = "neutral") {
1156
+ const i = ye(r);
1153
1157
  if (t.length === 0) return i;
1154
1158
  if (n.length === 0 || e < n[0].wtime)
1155
- return ae(t[0]?.emotion, i);
1156
- const s = rr(t, n);
1159
+ return ye(t[0]?.emotion, i);
1160
+ const s = wr(t, n);
1157
1161
  let c = null;
1158
1162
  for (const d of s)
1159
1163
  e >= d.startMs && e <= d.endMs && (c = d);
1160
1164
  if (c)
1161
- return ae(c.sentence.emotion, i);
1165
+ return ye(c.sentence.emotion, i);
1162
1166
  const a = n[n.length - 1], l = a.wtime + a.wduration;
1163
- return e > l + yt ? ae(
1167
+ return e > l + Dt ? ye(
1164
1168
  t[t.length - 1]?.emotion,
1165
1169
  i
1166
1170
  ) : i;
1167
1171
  }
1168
- function pt(e, t, n, r, i = Ge) {
1172
+ function xt(e, t, n, r, i = et) {
1169
1173
  const s = new OffscreenCanvas(n, r), c = s.getContext("2d", { willReadFrequently: !0 });
1170
1174
  c.drawImage(e, 0, 0, n, r);
1171
1175
  const a = c.getImageData(0, 0, n, r), l = c.createImageData(n, r);
@@ -1175,39 +1179,44 @@ function pt(e, t, n, r, i = Ge) {
1175
1179
  _.clearRect(0, 0, n, r), _.drawImage(m, 0, 0, n, r);
1176
1180
  const S = _.getImageData(0, 0, n, r);
1177
1181
  for (let o = 0; o < l.data.length; o += 4) {
1178
- const D = S.data[o], w = S.data[o + 1], u = S.data[o + 2];
1179
- Math.max(D, w, u) > i && (l.data[o] = D, l.data[o + 1] = w, l.data[o + 2] = u, l.data[o + 3] = 255);
1182
+ const F = S.data[o], w = S.data[o + 1], u = S.data[o + 2];
1183
+ Math.max(F, w, u) > i && (l.data[o] = F, l.data[o + 1] = w, l.data[o + 2] = u, l.data[o + 3] = 255);
1180
1184
  }
1181
1185
  }
1182
1186
  return c.putImageData(l, 0, 0), s;
1183
1187
  }
1184
- let re = null, ge = null;
1185
- const De = /* @__PURE__ */ new Map();
1186
- function sr() {
1187
- re = null, ge = null, De.clear();
1188
+ let de = null, Ie = null;
1189
+ const Ue = /* @__PURE__ */ new Map();
1190
+ function yr() {
1191
+ de = null, Ie = null, Ue.clear();
1188
1192
  }
1189
- Zt(sr);
1190
- function or(e) {
1191
- return e ?? ge ?? void 0;
1193
+ mn(yr);
1194
+ function pr(e) {
1195
+ return e ?? Ie ?? void 0;
1192
1196
  }
1193
- function ze(e) {
1197
+ function tt(e) {
1194
1198
  return new Promise((t, n) => {
1195
1199
  const r = new Image();
1196
1200
  r.decoding = "async", r.onload = () => t(r), r.onerror = () => n(new Error(`Failed to load image: ${e}`)), r.src = e;
1197
1201
  });
1198
1202
  }
1199
- async function it(e, t, n, r) {
1200
- const i = n ? await Cn(e, r) : await fetch(e).then((d) => {
1203
+ async function Er(e) {
1204
+ return Be() ? jn(
1205
+ await Ve(at(), e ?? "")
1206
+ ) : tt(at());
1207
+ }
1208
+ async function ht(e, t, n, r) {
1209
+ const i = n ? await qn(e, r) : await fetch(e).then((d) => {
1201
1210
  if (!d.ok) throw new Error(`Failed to load ${e}`);
1202
1211
  return d.json();
1203
1212
  }), s = i.sheets?.[0];
1204
1213
  if (!s?.path) throw new Error(`${e} has no sheets[0].path`);
1205
- const c = n ? await Mn(
1206
- await Ce(
1207
- $n(t, s.path, n),
1214
+ const c = n ? await Kn(
1215
+ await Ve(
1216
+ Zn(t, s.path, n),
1208
1217
  r ?? ""
1209
1218
  )
1210
- ) : await ze(`${t}/${s.path}`), a = /* @__PURE__ */ new Map();
1219
+ ) : await tt(`${t}/${s.path}`), a = /* @__PURE__ */ new Map();
1211
1220
  for (const d of i.cells ?? [])
1212
1221
  d.path && a.set(d.path, d);
1213
1222
  const l = /* @__PURE__ */ new Map();
@@ -1215,29 +1224,27 @@ async function it(e, t, n, r) {
1215
1224
  l.set(d.index, d);
1216
1225
  return { atlas: c, atlasMeta: i, cellByPath: a, sheetByIndex: l, diffBase: t };
1217
1226
  }
1218
- async function We(e) {
1219
- const t = or(e), n = je();
1227
+ async function Ye(e) {
1228
+ const t = pr(e), n = Be();
1220
1229
  if (n && !t)
1221
1230
  throw new Error("Encrypted assets enabled but no key provided");
1222
- if (re && (!n || ge === (t ?? null))) {
1223
- const s = await re;
1224
- return ye(s);
1231
+ if (de && (!n || Ie === (t ?? null))) {
1232
+ const s = await de;
1233
+ return Ae(s);
1225
1234
  }
1226
- ge = t ?? null, De.clear();
1227
- const r = tn();
1228
- re = (async () => {
1229
- const s = n ? await Fn(
1230
- await Ce(Xe(), t ?? "")
1231
- ) : await ze(Xe()), c = await it(
1232
- nn(),
1235
+ Ie = t ?? null, Ue.clear();
1236
+ const r = wn();
1237
+ de = (async () => {
1238
+ const s = await Er(t), c = await ht(
1239
+ gn(),
1233
1240
  r,
1234
1241
  n,
1235
1242
  t
1236
1243
  );
1237
1244
  let a = null;
1238
1245
  try {
1239
- a = await it(
1240
- rn(),
1246
+ a = await ht(
1247
+ yn(),
1241
1248
  r,
1242
1249
  n,
1243
1250
  t
@@ -1247,59 +1254,59 @@ async function We(e) {
1247
1254
  }
1248
1255
  return { sil: s, viseme: c, expression: a };
1249
1256
  })();
1250
- const i = await re;
1251
- return ye(i);
1257
+ const i = await de;
1258
+ return Ae(i);
1252
1259
  }
1253
- async function Et(e) {
1254
- if (await We(e), !re) throw new Error("Assets not loaded");
1255
- return re;
1260
+ async function Mt(e) {
1261
+ if (await Ye(e), !de) throw new Error("Assets not loaded");
1262
+ return de;
1256
1263
  }
1257
- function ye(e) {
1258
- return Rn(
1264
+ function Ae(e) {
1265
+ return $n(
1259
1266
  e.viseme.atlasMeta,
1260
1267
  e.sil.naturalWidth,
1261
1268
  e.sil.naturalHeight
1262
1269
  );
1263
1270
  }
1264
- function ar(e, t) {
1271
+ function Sr(e, t) {
1265
1272
  const n = document.createElement("canvas");
1266
1273
  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;
1267
1274
  }
1268
- async function st(e, t, n) {
1269
- const r = `${t.diffBase}::${n}`, i = De.get(r);
1275
+ async function mt(e, t, n) {
1276
+ const r = `${t.diffBase}::${n}`, i = Ue.get(r);
1270
1277
  if (i) return i;
1271
1278
  const s = (async () => {
1272
1279
  const c = t.cellByPath.get(n);
1273
1280
  if (!c) throw new Error(`No atlas cell for path: ${n}`);
1274
- const a = Dn(t.atlasMeta, c), l = ye(e);
1275
- if (xn(c)) {
1276
- const m = ar(t, c), S = document.createElement("canvas");
1281
+ const a = Hn(t.atlasMeta, c), l = Ae(e);
1282
+ if (Wn(c)) {
1283
+ const m = Sr(t, c), S = document.createElement("canvas");
1277
1284
  S.width = l.width, S.height = l.height;
1278
1285
  const o = S.getContext("2d");
1279
1286
  return o.fillStyle = "#000", o.fillRect(0, 0, l.width, l.height), o.drawImage(m, a.x, a.y), S;
1280
1287
  }
1281
- const d = `${t.diffBase}/${n}`, _ = await ze(d);
1288
+ const d = `${t.diffBase}/${n}`, _ = await tt(d);
1282
1289
  if (_.naturalWidth === l.width && _.naturalHeight === l.height)
1283
1290
  return _;
1284
1291
  throw new Error(`Diff PNG wrong size for ${n}`);
1285
1292
  })();
1286
- return De.set(r, s), s;
1293
+ return Ue.set(r, s), s;
1287
1294
  }
1288
- async function ot(e, t, n, r = Ge, i, s) {
1295
+ async function _t(e, t, n, r = et, i, s) {
1289
1296
  if (i) {
1290
1297
  const { width: S, height: o } = i;
1291
1298
  if (s && !s() || t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA)
1292
1299
  return;
1293
- const D = document.createElement("canvas");
1294
- D.width = S, D.height = o;
1295
- const w = D.getContext("2d");
1300
+ const F = document.createElement("canvas");
1301
+ F.width = S, F.height = o;
1302
+ const w = F.getContext("2d");
1296
1303
  if (w.clearRect(0, 0, S, o), w.drawImage(t, 0, 0, S, o), s && !s()) return;
1297
1304
  (e.width !== S || e.height !== o) && (e.width = S, e.height = o);
1298
1305
  const u = e.getContext("2d");
1299
- u.clearRect(0, 0, S, o), u.drawImage(D, 0, 0);
1306
+ u.clearRect(0, 0, S, o), u.drawImage(F, 0, 0);
1300
1307
  return;
1301
1308
  }
1302
- const c = await Et(), { width: a, height: l } = ye(c);
1309
+ const c = await Mt(), { width: a, height: l } = Ae(c);
1303
1310
  (e.width !== a || e.height !== l) && (e.width = a, e.height = l);
1304
1311
  const d = [], _ = e.getContext("2d");
1305
1312
  if (_.clearRect(0, 0, a, l), t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
@@ -1311,22 +1318,22 @@ async function ot(e, t, n, r = Ge, i, s) {
1311
1318
  _.drawImage(t, 0, 0, a, l);
1312
1319
  return;
1313
1320
  }
1314
- const m = pt(t, d, a, l, r);
1321
+ const m = xt(t, d, a, l, r);
1315
1322
  _.drawImage(m, 0, 0);
1316
1323
  }
1317
- async function qe(e, t, n = Ge) {
1318
- const r = await Et(ge ?? void 0), { width: i, height: s } = ye(r);
1324
+ async function Xe(e, t, n = et) {
1325
+ const r = await Mt(Ie ?? void 0), { width: i, height: s } = Ae(r);
1319
1326
  (e.width !== i || e.height !== s) && (e.width = i, e.height = s);
1320
1327
  const c = [];
1321
- t.mouthPath && c.push(await st(r, r.viseme, t.mouthPath)), t.expressionPath && r.expression && c.push(
1322
- await st(r, r.expression, t.expressionPath)
1328
+ t.mouthPath && c.push(await mt(r, r.viseme, t.mouthPath)), t.expressionPath && r.expression && c.push(
1329
+ await mt(r, r.expression, t.expressionPath)
1323
1330
  );
1324
1331
  const a = e.getContext("2d");
1325
1332
  if (a.clearRect(0, 0, i, s), c.length === 0) {
1326
1333
  a.drawImage(r.sil, 0, 0, i, s);
1327
1334
  return;
1328
1335
  }
1329
- const l = pt(
1336
+ const l = xt(
1330
1337
  r.sil,
1331
1338
  c,
1332
1339
  i,
@@ -1335,52 +1342,64 @@ async function qe(e, t, n = Ge) {
1335
1342
  );
1336
1343
  a.drawImage(l, 0, 0);
1337
1344
  }
1338
- async function cr(e) {
1339
- await qe(e, { mouthPath: null, expressionPath: null });
1345
+ async function kr(e) {
1346
+ await Xe(e, { mouthPath: null, expressionPath: null });
1340
1347
  }
1341
- function ur(e) {
1348
+ function Tr(e) {
1342
1349
  const t = /* @__PURE__ */ new Map();
1343
1350
  for (const n of e)
1344
1351
  t.has(n.vtime) || t.set(n.vtime, n);
1345
1352
  return [...t.values()].sort((n, r) => n.vtime - r.vtime);
1346
1353
  }
1347
- function lr(e) {
1348
- const t = ur(e), n = [];
1354
+ function vr(e) {
1355
+ const t = Tr(e), n = [];
1349
1356
  for (let r = 0; r < t.length - 1; r++) {
1350
1357
  const i = t[r].viseme, s = t[r + 1].viseme, c = t[r].vtime, a = t[r + 1].vtime;
1351
1358
  i === s || a <= c || n.push({ from: i, to: s, fromVtime: c, toVtime: a });
1352
1359
  }
1353
1360
  return n;
1354
1361
  }
1355
- function fr(e, t) {
1362
+ function Ir(e, t) {
1356
1363
  let n = null;
1357
1364
  for (const r of e)
1358
1365
  t >= r.fromVtime && t < r.toVtime && (n = r);
1359
1366
  return n;
1360
1367
  }
1361
- function dr(e, t) {
1368
+ function Ar(e, t) {
1362
1369
  return { gapMs: Math.max(0, t - e), holdEnd: e, transStart: e };
1363
1370
  }
1364
- const hr = 500, mr = "__sil__";
1365
- function _r() {
1371
+ const Ct = 400, br = 200, Rr = 600, Dr = "__sil__";
1372
+ function xr() {
1366
1373
  return { lastDrawnKey: "" };
1367
1374
  }
1368
- function _e(e) {
1375
+ function ge(e) {
1369
1376
  e.lastDrawnKey = "";
1370
1377
  }
1371
- function at(e) {
1372
- return e ?? mr;
1378
+ function Mr(e) {
1379
+ let t = "sil";
1380
+ for (const n of e)
1381
+ n.viseme !== "sil" && (t = n.viseme);
1382
+ return t;
1383
+ }
1384
+ function Cr(e) {
1385
+ return e === "sil" ? !1 : Rt(e, "sil").length > 0;
1373
1386
  }
1374
- function wr(e, t) {
1387
+ function Fr() {
1388
+ return Ct + Rr;
1389
+ }
1390
+ function wt(e) {
1391
+ return e ?? Dr;
1392
+ }
1393
+ function Lr(e, t) {
1375
1394
  if (e.length === 0)
1376
1395
  return { diffPath: null, label: "sil" };
1377
- const n = lr(e), r = fr(n, t);
1396
+ const n = vr(e), r = Ir(n, t);
1378
1397
  if (r) {
1379
- const s = r.toVtime - r.fromVtime, c = dr(r.fromVtime, r.toVtime);
1398
+ const s = r.toVtime - r.fromVtime, c = Ar(r.fromVtime, r.toVtime);
1380
1399
  if (t >= c.transStart && t < r.toVtime) {
1381
- const a = Ln(r.from, r.to), l = Bn(a, s);
1400
+ const a = Rt(r.from, r.to), l = Yn(a, s);
1382
1401
  if (l.length > 0) {
1383
- const d = Nn(
1402
+ const d = Xn(
1384
1403
  t,
1385
1404
  c.transStart,
1386
1405
  r.toVtime,
@@ -1388,34 +1407,34 @@ function wr(e, t) {
1388
1407
  );
1389
1408
  return { diffPath: l[d] ?? l[l.length - 1], label: `${r.from}→${r.to}` };
1390
1409
  }
1391
- return { diffPath: Ve(r.to), label: r.to };
1410
+ return { diffPath: Qe(r.to), label: r.to };
1392
1411
  }
1393
1412
  }
1394
- const i = Sn(e, t);
1395
- return i === "sil" ? { diffPath: null, label: "sil" } : { diffPath: Ve(i), label: i };
1413
+ const i = vt(e, t);
1414
+ return i === "sil" ? { diffPath: null, label: "sil" } : { diffPath: Qe(i), label: i };
1396
1415
  }
1397
- function kt(e, t) {
1398
- const n = t ?? at(e.expressionPath);
1399
- return `${at(e.mouthPath)}|${n}`;
1416
+ function Ft(e, t) {
1417
+ const n = t ?? wt(e.expressionPath);
1418
+ return `${wt(e.mouthPath)}|${n}`;
1400
1419
  }
1401
- function gr(e, t, n, r, i, s, c, a) {
1402
- const l = wr(t, n), d = {
1420
+ function Or(e, t, n, r, i, s, c, a) {
1421
+ const l = Lr(t, n), d = {
1403
1422
  mouthPath: l.diffPath,
1404
1423
  expressionPath: r
1405
- }, _ = kt(d, i);
1424
+ }, _ = Ft(d, i);
1406
1425
  _ !== s.lastDrawnKey && (a(l.label), c(e, d, _, (m) => {
1407
1426
  m === _ && (s.lastDrawnKey = _);
1408
1427
  }));
1409
1428
  }
1410
- function ct(e, t, n, r, i, s, c = null) {
1411
- const a = { mouthPath: null, expressionPath: t }, l = c != null ? `|idle@${Math.round(c * 1e3)}` : "", d = kt(a, n) + l;
1429
+ function gt(e, t, n, r, i, s, c = null) {
1430
+ const a = { mouthPath: null, expressionPath: t }, l = c != null ? `|idle@${Math.round(c * 1e3)}` : "", d = Ft(a, n) + l;
1412
1431
  !(c != null) && d === r.lastDrawnKey || (s(
1413
1432
  c != null ? "idle" : "sil"
1414
1433
  ), i(e, a, d, (m) => {
1415
1434
  m === d && (r.lastDrawnKey = d);
1416
1435
  }));
1417
1436
  }
1418
- function St(e) {
1437
+ function Lt(e) {
1419
1438
  let t = 0, n = null, r = !1, i = !1;
1420
1439
  const s = async () => {
1421
1440
  if (!r) {
@@ -1451,9 +1470,9 @@ function St(e) {
1451
1470
  }
1452
1471
  };
1453
1472
  }
1454
- const ut = -1, lt = /* @__PURE__ */ new Map();
1455
- async function yr(e) {
1456
- let t = lt.get(e);
1473
+ const yt = -1, pt = /* @__PURE__ */ new Map();
1474
+ async function Pr(e) {
1475
+ let t = pt.get(e);
1457
1476
  if (!t) {
1458
1477
  const n = await fetch(e);
1459
1478
  if (!n.ok)
@@ -1461,28 +1480,28 @@ async function yr(e) {
1461
1480
  `Failed to fetch worker script (${n.status}): ${e}`
1462
1481
  );
1463
1482
  const r = await n.blob();
1464
- t = URL.createObjectURL(r), lt.set(e, t);
1483
+ t = URL.createObjectURL(r), pt.set(e, t);
1465
1484
  }
1466
1485
  return new Worker(t, { name: "visemeDiffPreview" });
1467
1486
  }
1468
- function pr() {
1487
+ function Ur() {
1469
1488
  return typeof Worker < "u" && typeof OffscreenCanvas < "u";
1470
1489
  }
1471
- function Er(e, t) {
1490
+ function Br(e, t) {
1472
1491
  (e.width !== t.width || e.height !== t.height) && (e.width = t.width, e.height = t.height);
1473
1492
  const n = e.getContext("2d");
1474
1493
  n && n.drawImage(t, 0, 0);
1475
1494
  }
1476
- function ft(e, t) {
1495
+ function Et(e, t) {
1477
1496
  let n = 0;
1478
- const r = St(async (i, s) => (await We(), await qe(e, i), r.isLatestSeq(s)));
1497
+ const r = Lt(async (i, s) => (await Ye(), await Xe(e, i), r.isLatestSeq(s)));
1479
1498
  return {
1480
1499
  usesWorker: !1,
1481
1500
  async loadAssets(i) {
1482
- return await We(i);
1501
+ return await Ye(i);
1483
1502
  },
1484
1503
  async renderSilOnly() {
1485
- n += 1, r.reset(), await cr(e);
1504
+ n += 1, r.reset(), await kr(e);
1486
1505
  },
1487
1506
  drawLiveFrame(i, s, c, a) {
1488
1507
  r.enqueue(s, c, a);
@@ -1490,8 +1509,8 @@ function ft(e, t) {
1490
1509
  async renderViseme(i, s) {
1491
1510
  n += 1;
1492
1511
  const c = n;
1493
- t.onStatus?.(s), await qe(i, {
1494
- mouthPath: Ve(s),
1512
+ t.onStatus?.(s), await Xe(i, {
1513
+ mouthPath: Qe(s),
1495
1514
  expressionPath: null
1496
1515
  });
1497
1516
  },
@@ -1500,9 +1519,9 @@ function ft(e, t) {
1500
1519
  }
1501
1520
  };
1502
1521
  }
1503
- function kr(e, t, n) {
1522
+ function Nr(e, t, n) {
1504
1523
  let r = null, i = 1, s = 0, c = !1, a = !1;
1505
- const l = /* @__PURE__ */ new Map(), d = St(async (w, u, g) => (l.set(u, {
1524
+ const l = /* @__PURE__ */ new Map(), d = Lt(async (w, u, g) => (l.set(u, {
1506
1525
  drawKey: g.drawKey,
1507
1526
  onDisplayed: g.onDisplayed
1508
1527
  }), await o(
@@ -1515,11 +1534,11 @@ function kr(e, t, n) {
1515
1534
  void 0,
1516
1535
  !0
1517
1536
  ), !1)), _ = (w) => {
1518
- if (w === ut) {
1519
- let p = -1, A;
1520
- for (const [V, y] of l)
1521
- V >= p && (p = V, A = y);
1522
- p >= 0 && l.delete(p), A?.onDisplayed?.(A.drawKey);
1537
+ if (w === yt) {
1538
+ let y = -1, v;
1539
+ for (const [H, h] of l)
1540
+ H >= y && (y = H, v = h);
1541
+ y >= 0 && l.delete(y), v?.onDisplayed?.(v.drawKey);
1523
1542
  return;
1524
1543
  }
1525
1544
  const u = -w, g = l.get(u);
@@ -1528,17 +1547,17 @@ function kr(e, t, n) {
1528
1547
  a || !r || r.postMessage(w, []);
1529
1548
  }, o = (w, u, g = !1) => {
1530
1549
  if (a) return Promise.resolve();
1531
- const p = i++;
1532
- return new Promise((A, V) => {
1533
- m.set(p, {
1534
- resolve: (y) => A(y),
1535
- reject: V,
1550
+ const y = i++;
1551
+ return new Promise((v, H) => {
1552
+ m.set(y, {
1553
+ resolve: (h) => v(h),
1554
+ reject: H,
1536
1555
  generation: typeof w.generation == "number" ? w.generation : void 0,
1537
1556
  expectRenderDone: g
1538
- }), S({ ...w, requestId: p });
1557
+ }), S({ ...w, requestId: y });
1539
1558
  });
1540
- }, D = (async () => {
1541
- r = await yr(n), r.onmessage = (w) => {
1559
+ }, F = (async () => {
1560
+ r = await Pr(n), r.onmessage = (w) => {
1542
1561
  if (a) return;
1543
1562
  const u = w.data;
1544
1563
  if (u.type === "status") {
@@ -1546,23 +1565,23 @@ function kr(e, t, n) {
1546
1565
  return;
1547
1566
  }
1548
1567
  if (u.type === "frame") {
1549
- const p = u.generation < 0;
1550
- if (!p && u.generation !== s) {
1568
+ const y = u.generation < 0;
1569
+ if (!y && u.generation !== s) {
1551
1570
  u.bitmap.close();
1552
1571
  return;
1553
1572
  }
1554
- if (p && !(u.generation === ut)) {
1555
- const V = -u.generation;
1556
- if (!d.isLatestSeq(V)) {
1573
+ if (y && !(u.generation === yt)) {
1574
+ const H = -u.generation;
1575
+ if (!d.isLatestSeq(H)) {
1557
1576
  u.bitmap.close();
1558
1577
  return;
1559
1578
  }
1560
1579
  }
1561
- if (p && t.shouldAcceptLiveFrame && !t.shouldAcceptLiveFrame()) {
1580
+ if (y && t.shouldAcceptLiveFrame && !t.shouldAcceptLiveFrame()) {
1562
1581
  u.bitmap.close();
1563
1582
  return;
1564
1583
  }
1565
- Er(e, u.bitmap), u.bitmap.close(), p && _(u.generation);
1584
+ Br(e, u.bitmap), u.bitmap.close(), y && _(u.generation);
1566
1585
  return;
1567
1586
  }
1568
1587
  const g = m.get(u.requestId);
@@ -1599,38 +1618,38 @@ function kr(e, t, n) {
1599
1618
  usesWorker: !0,
1600
1619
  async loadAssets(w) {
1601
1620
  if (a) return { width: 842, height: 1264 };
1602
- if (await D, a) return { width: 842, height: 1264 };
1621
+ if (await F, a) return { width: 842, height: 1264 };
1603
1622
  if (!c) throw new Error("Worker init failed");
1604
1623
  return await o({
1605
1624
  type: "loadAssets",
1606
1625
  keyHex: w,
1607
- urls: Xt(ie())
1626
+ urls: hn(he())
1608
1627
  }) ?? { width: 842, height: 1264 };
1609
1628
  },
1610
1629
  async renderSilOnly() {
1611
- if (a || (await D, a)) return;
1630
+ if (a || (await F, a)) return;
1612
1631
  s += 1, d.reset(), l.clear();
1613
1632
  const w = s;
1614
- await o(
1633
+ await o({ type: "cancelLiveDraws" }), await o(
1615
1634
  { type: "renderSil", generation: w },
1616
1635
  void 0,
1617
1636
  !0
1618
1637
  );
1619
1638
  },
1620
- drawLiveFrame(w, u, g, p) {
1621
- a || D.then(() => {
1622
- a || d.enqueue(u, g, p);
1639
+ drawLiveFrame(w, u, g, y) {
1640
+ a || F.then(() => {
1641
+ a || d.enqueue(u, g, y);
1623
1642
  });
1624
1643
  },
1625
1644
  async renderViseme(w, u, g = {}) {
1626
- if (a || (await D, a)) return;
1627
- s += 1;
1628
- const p = s;
1645
+ if (a || (await F, a)) return;
1646
+ s += 1, d.reset(), l.clear();
1647
+ const y = s;
1629
1648
  try {
1630
- await o(
1649
+ await o({ type: "cancelLiveDraws" }), await o(
1631
1650
  {
1632
1651
  type: "renderViseme",
1633
- generation: p,
1652
+ generation: y,
1634
1653
  to: u,
1635
1654
  from: g.from,
1636
1655
  transitionDurationMs: g.transitionDurationMs,
@@ -1640,9 +1659,9 @@ function kr(e, t, n) {
1640
1659
  void 0,
1641
1660
  !0
1642
1661
  );
1643
- } catch (A) {
1644
- if (A?.name === "AbortError") return;
1645
- throw A;
1662
+ } catch (v) {
1663
+ if (v?.name === "AbortError") return;
1664
+ throw v;
1646
1665
  }
1647
1666
  },
1648
1667
  dispose() {
@@ -1655,25 +1674,25 @@ function kr(e, t, n) {
1655
1674
  }
1656
1675
  };
1657
1676
  }
1658
- function Sr(e, t = {}) {
1659
- if (!pr())
1660
- return ft(e, t);
1661
- const n = Ot(t.workerScriptUrl);
1677
+ function Vr(e, t = {}) {
1678
+ if (!Ur())
1679
+ return Et(e, t);
1680
+ const n = zt(t.workerScriptUrl);
1662
1681
  try {
1663
- return kr(e, t, n);
1682
+ return Nr(e, t, n);
1664
1683
  } catch {
1665
- return ft(e, t);
1684
+ return Et(e, t);
1666
1685
  }
1667
1686
  }
1668
- class vr extends Error {
1687
+ class $r extends Error {
1669
1688
  constructor(t, n) {
1670
1689
  super(`${t} timed out after ${Math.round(n / 1e3)}s`), this.name = "LoadTimeoutError";
1671
1690
  }
1672
1691
  }
1673
- function Le(e, t, n) {
1692
+ function qe(e, t, n) {
1674
1693
  return new Promise((r, i) => {
1675
1694
  const s = globalThis.setTimeout(() => {
1676
- i(new vr(n, t));
1695
+ i(new $r(n, t));
1677
1696
  }, t);
1678
1697
  e.then(
1679
1698
  (c) => {
@@ -1685,15 +1704,15 @@ function Le(e, t, n) {
1685
1704
  );
1686
1705
  });
1687
1706
  }
1688
- const Ir = {
1707
+ const Hr = {
1689
1708
  position: "relative",
1690
1709
  display: "inline-block",
1691
1710
  lineHeight: 0
1692
- }, Tr = {
1711
+ }, Wr = {
1693
1712
  display: "block",
1694
1713
  maxWidth: "100%",
1695
1714
  height: "auto"
1696
- }, Ar = {
1715
+ }, qr = {
1697
1716
  position: "absolute",
1698
1717
  inset: 0,
1699
1718
  display: "flex",
@@ -1705,16 +1724,16 @@ const Ir = {
1705
1724
  fontSize: "0.875rem",
1706
1725
  textAlign: "center"
1707
1726
  };
1708
- function be(e) {
1709
- return Bt(e) ? An : _t;
1727
+ function Oe(e) {
1728
+ return Xt(e) ? Nn : It;
1710
1729
  }
1711
- const xr = Tt(function({
1730
+ const zr = Nt(function({
1712
1731
  id: t,
1713
1732
  assets: n,
1714
1733
  authToken: r,
1715
- ttsEngineId: i = ce,
1734
+ ttsEngineId: i = pe,
1716
1735
  voiceId: s,
1717
- speakingRate: c = wt,
1736
+ speakingRate: c = At,
1718
1737
  className: a,
1719
1738
  style: l,
1720
1739
  canvasStyle: d,
@@ -1722,288 +1741,377 @@ const xr = Tt(function({
1722
1741
  onDisplayStatus: m,
1723
1742
  onError: S,
1724
1743
  onReady: o,
1725
- showErrorOverlay: D = !0
1744
+ showErrorOverlay: F = !0
1726
1745
  }, w) {
1727
- const u = B(null), g = B(null), p = B(_r()), A = B(Zn()), V = B(0), y = B(0), E = B(null), x = B(!1), R = B(
1746
+ const u = R(null), g = R(null), y = R(xr()), v = R(dr()), H = R(0), h = R(0), E = R(null), D = R(null), L = R(!1), z = R(!1), U = R(!1), ae = R("sil"), me = R(!1), N = R(
1728
1747
  null
1729
- ), W = B(!1), [q, ue] = me("idle"), [Ee, Z] = me(null), [K, ke] = me(!1), [Fe, le] = me(!1), [se, Se] = me({ width: 842, height: 1264 }), ee = B(r ?? null), fe = B(null), h = B(null), C = B(!1), N = B({ width: 842, height: 1264 }), O = B(o), v = B(m), L = B(S);
1730
- O.current = o, v.current = m, L.current = S;
1731
- const Q = ne(
1748
+ ), X = R(!1), [Re, He] = ke("idle"), [De, ue] = ke(null), [se, xe] = ke(!1), [nt, p] = ke(!1), [V, W] = ke({ width: 842, height: 1264 }), x = R(r ?? null), M = R(null), I = R(null), Q = R(!1), J = R({ width: 842, height: 1264 }), P = R(o), O = R(m), q = R(S);
1749
+ P.current = o, O.current = m, q.current = S;
1750
+ const ce = ie(
1732
1751
  (f) => {
1733
- const k = f?.ttsEngineId ?? i ?? h.current?.ttsEngineId ?? ce, I = f?.voiceId ?? s ?? h.current?.voiceId ?? be(k);
1734
- return { ttsEngineId: k, voiceId: I };
1752
+ const k = f?.ttsEngineId ?? i ?? I.current?.ttsEngineId ?? pe, T = f?.voiceId ?? s ?? I.current?.voiceId ?? Oe(k);
1753
+ return { ttsEngineId: k, voiceId: T };
1735
1754
  },
1736
1755
  [i, s]
1737
- ), $ = ne((f) => {
1738
- Z(f), L.current?.(f);
1739
- }, []), P = ne(
1756
+ ), $ = ie((f) => {
1757
+ ue(f), q.current?.(f);
1758
+ }, []), Me = ie(
1740
1759
  (f) => {
1741
- ue(f), _?.(f);
1760
+ He(f), _?.(f);
1742
1761
  },
1743
1762
  [_]
1744
- ), M = B(
1745
- bn(P, Re)
1746
- ), F = ne(() => {
1763
+ ), B = R(
1764
+ Vn(Me, Pe)
1765
+ ), b = ie(() => {
1747
1766
  E.current != null && (clearTimeout(E.current), E.current = null);
1748
- }, []), J = ne(async () => {
1749
- const f = y.current, k = g.current;
1750
- !k || !K || M.current.isSpeaking() || (_e(p.current), await k.renderSilOnly(), f === y.current && (M.current.isSpeaking() || (v.current?.("sil"), W.current && R.current && (R.current.restart(), R.current.setActive(!0)))));
1751
- }, [K]), H = ne(() => {
1752
- F();
1753
- const f = y.current;
1754
- J(), E.current = setTimeout(() => {
1755
- E.current = null, f === y.current && J();
1756
- }, hr);
1757
- }, [F, J]), de = ne(async () => {
1767
+ }, []), K = ie(() => {
1768
+ D.current != null && (clearTimeout(D.current), D.current = null), U.current = !1;
1769
+ }, []), te = ie(() => {
1770
+ !X.current || !N.current || (K(), U.current = !0, D.current = setTimeout(() => {
1771
+ if (D.current = null, B.current.isSpeaking()) {
1772
+ U.current = !1;
1773
+ return;
1774
+ }
1775
+ if (L.current) {
1776
+ U.current = !1;
1777
+ return;
1778
+ }
1779
+ const f = N.current;
1780
+ if (!f) {
1781
+ U.current = !1;
1782
+ return;
1783
+ }
1784
+ f.restart(), f.setActive(!0);
1785
+ const k = f.getVideo();
1786
+ let T = !1;
1787
+ const A = () => {
1788
+ T || (T = !0, k.removeEventListener("playing", C), U.current = !1);
1789
+ }, C = () => {
1790
+ A();
1791
+ };
1792
+ if (k.addEventListener("playing", C, { once: !0 }), k.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && !k.paused) {
1793
+ A();
1794
+ return;
1795
+ }
1796
+ setTimeout(A, 300);
1797
+ }, br));
1798
+ }, [K]), Z = ie(() => {
1799
+ if (b(), O.current?.("sil"), L.current = !1, !X.current || !N.current) {
1800
+ U.current = !1;
1801
+ return;
1802
+ }
1803
+ te();
1804
+ }, [b, te]), j = ie(
1805
+ async (f) => {
1806
+ const k = h.current, T = g.current, A = u.current, C = () => {
1807
+ f?.fallbackOnly || (L.current = !1);
1808
+ };
1809
+ if (!T || !A || !se) {
1810
+ C();
1811
+ return;
1812
+ }
1813
+ if (B.current.isSpeaking()) {
1814
+ C();
1815
+ return;
1816
+ }
1817
+ if (f?.fallbackOnly) {
1818
+ if (!L.current || (ge(y.current), await T.renderSilOnly(), k !== h.current) || B.current.isSpeaking()) return;
1819
+ Z();
1820
+ return;
1821
+ }
1822
+ const Y = ae.current !== "sil" ? ae.current : Mr(B.current.getVisemeQueue()), ne = Cr(Y);
1823
+ if (ge(y.current), !ne) {
1824
+ if (await T.renderSilOnly(), k !== h.current || B.current.isSpeaking()) return;
1825
+ Z();
1826
+ return;
1827
+ }
1828
+ try {
1829
+ await T.renderViseme(A, "sil", {
1830
+ from: Y,
1831
+ transitionDurationMs: Ct
1832
+ });
1833
+ } catch (oe) {
1834
+ if (oe?.name !== "AbortError")
1835
+ throw oe;
1836
+ }
1837
+ if (k !== h.current) {
1838
+ C();
1839
+ return;
1840
+ }
1841
+ if (B.current.isSpeaking()) {
1842
+ C();
1843
+ return;
1844
+ }
1845
+ Z();
1846
+ },
1847
+ [se, Z]
1848
+ ), _e = ie(() => {
1849
+ if (z.current || L.current) return;
1850
+ z.current = !0, L.current = !0, b();
1851
+ const f = h.current;
1852
+ j().finally(() => {
1853
+ z.current = !1;
1854
+ }), E.current = setTimeout(() => {
1855
+ E.current = null, f === h.current && j({ fallbackOnly: !0 });
1856
+ }, Fr());
1857
+ }, [b, j]), rt = ie(async () => {
1758
1858
  const f = Math.floor(Date.now() / 1e3);
1759
- if (fe.current && f < fe.current.expiresAt - 60)
1760
- return fe.current.key;
1761
- const k = ee.current ?? await Ye();
1762
- ee.current = k, M.current.setDeveloperToken(k);
1763
- const I = await jt(k, Re);
1764
- return fe.current = {
1765
- key: I.value,
1766
- expiresAt: I.expiresAt
1767
- }, I.value;
1859
+ if (M.current && f < M.current.expiresAt - 60)
1860
+ return M.current.key;
1861
+ const k = x.current ?? await ot();
1862
+ x.current = k, B.current.setDeveloperToken(k);
1863
+ const T = await an(k, Pe);
1864
+ return M.current = {
1865
+ key: T.value,
1866
+ expiresAt: T.expiresAt
1867
+ }, T.value;
1768
1868
  }, []);
1769
- oe(() => {
1770
- r && (ee.current = r, M.current.setDeveloperToken(r));
1771
- }, [r]), oe(() => {
1772
- M.current.setTtsEngineId(i), h.current && (h.current.ttsEngineId = i);
1773
- }, [i]), oe(() => {
1774
- const f = s ?? be(i);
1775
- h.current && (h.current.voiceId = f);
1869
+ Te(() => {
1870
+ r && (x.current = r, B.current.setDeveloperToken(r));
1871
+ }, [r]), Te(() => {
1872
+ B.current.setTtsEngineId(i), I.current && (I.current.ttsEngineId = i);
1873
+ }, [i]), Te(() => {
1874
+ const f = s ?? Oe(i);
1875
+ I.current && (I.current.voiceId = f);
1776
1876
  }, [s, i]);
1777
- const he = At(
1877
+ const Ot = Vt(
1778
1878
  () => n ? JSON.stringify(n) : `id:${t ?? ""}`,
1779
1879
  [n, t]
1780
1880
  );
1781
- return oe(() => {
1881
+ return Te(() => {
1782
1882
  if (!t && !n) {
1783
1883
  $("AiTwin requires either `id` or `assets`");
1784
1884
  return;
1785
1885
  }
1786
1886
  let f = !1;
1787
- C.current = !1, ke(!1), le(!1), Z(null), h.current = null, R.current = null, W.current = !1;
1887
+ Q.current = !1, xe(!1), p(!1), ue(null), I.current = null, N.current = null, X.current = !1;
1788
1888
  const k = u.current;
1789
1889
  if (!k) return;
1790
- let I = null;
1890
+ let T = null;
1791
1891
  return (async () => {
1792
1892
  try {
1793
- let T, b, j;
1893
+ let A, C, Y;
1794
1894
  if (n)
1795
- T = n, b = i, j = s ?? be(b);
1895
+ A = n, C = i, Y = s ?? Oe(C);
1796
1896
  else {
1797
- v.current?.("getAiTwin");
1798
- const U = await Le(
1799
- $t(t),
1897
+ O.current?.("getAiTwin");
1898
+ const G = await qe(
1899
+ en(t),
1800
1900
  3e4,
1801
1901
  "getAiTwin"
1802
1902
  );
1803
1903
  if (f) return;
1804
- if (!U.faceId?.trim())
1904
+ if (!G.faceId?.trim())
1805
1905
  throw new Error("getAiTwin returned no faceId");
1806
- b = U.ttsEngineId, j = s ?? U.voiceId ?? be(b), T = Jt(U.faceId);
1906
+ C = G.ttsEngineId, Y = s ?? G.voiceId ?? Oe(C), A = fn(G.faceId);
1807
1907
  }
1808
- h.current = { ttsEngineId: b, voiceId: j }, Yt(T), R.current = er(), I = Sr(k, {
1809
- onStatus: (U) => {
1810
- f || v.current?.(U);
1908
+ I.current = { ttsEngineId: C, voiceId: Y }, dn(A), N.current = hr(), T = Vr(k, {
1909
+ onStatus: (G) => {
1910
+ f || O.current?.(G);
1811
1911
  },
1812
- onError: (U) => {
1813
- f || $(U);
1912
+ onError: (G) => {
1913
+ f || $(G);
1814
1914
  },
1815
- shouldAcceptLiveFrame: () => M.current.isSpeaking()
1816
- }), g.current = I, v.current?.("auth");
1817
- const Y = ee.current ?? r ?? await Le(Ye(), 3e4, "getAuthToken");
1915
+ shouldAcceptLiveFrame: () => B.current.isSpeaking() && !L.current
1916
+ }), g.current = T, O.current?.("auth");
1917
+ const ne = x.current ?? r ?? await qe(ot(), 3e4, "getAuthToken");
1818
1918
  if (f) return;
1819
- ee.current = Y, M.current.setDeveloperToken(Y), M.current.setTtsEngineId(b);
1820
- const ve = je() ? await de() : void 0;
1919
+ x.current = ne, B.current.setDeveloperToken(ne), B.current.setTtsEngineId(C);
1920
+ const oe = Be() ? await rt() : void 0;
1821
1921
  if (f) return;
1822
- v.current?.("loadAssets");
1823
- const G = await Le(
1824
- I.loadAssets(ve),
1922
+ O.current?.("loadAssets");
1923
+ const we = await qe(
1924
+ T.loadAssets(oe),
1825
1925
  18e4,
1826
1926
  "load face assets"
1827
1927
  );
1828
- if (!f && G.width > 0 && G.height > 0 && (N.current = G, Se(G)), f) return;
1829
- C.current = !0, ke(!0), O.current?.(), I.renderSilOnly().then(() => {
1830
- f || v.current?.("sil");
1928
+ if (!f && we.width > 0 && we.height > 0 && (J.current = we, W(we)), f) return;
1929
+ Q.current = !0, xe(!0), P.current?.(), T.renderSilOnly().then(() => {
1930
+ f || O.current?.("sil");
1831
1931
  }), (async () => {
1832
1932
  try {
1833
- const U = await On(ve);
1834
- if (await R.current.load(U), f) return;
1835
- W.current = !0, le(!0), _e(p.current), R.current.setActive(!0);
1836
- const X = R.current.getVideo();
1837
- await ot(
1933
+ const G = await zn(oe);
1934
+ if (await N.current.load(G), f) return;
1935
+ X.current = !0, p(!0), ge(y.current), N.current.setActive(!0);
1936
+ const re = N.current.getVideo();
1937
+ await _t(
1838
1938
  k,
1839
- X,
1939
+ re,
1840
1940
  null,
1841
1941
  void 0,
1842
- N.current
1843
- ), f || v.current?.("idle");
1844
- } catch (U) {
1942
+ J.current
1943
+ ), f || O.current?.("idle");
1944
+ } catch (G) {
1845
1945
  if (f) return;
1846
- W.current = !1, le(!1), console.warn("[AiTwin] Idle video unavailable:", U);
1946
+ X.current = !1, p(!1), console.warn("[AiTwin] Idle video unavailable:", G);
1847
1947
  }
1848
1948
  })();
1849
- } catch (T) {
1949
+ } catch (A) {
1850
1950
  if (f) return;
1851
- if (T instanceof Ue) {
1852
- $(T.message);
1951
+ if (A instanceof Ke) {
1952
+ $(A.message);
1853
1953
  return;
1854
1954
  }
1855
- $(T instanceof Error ? T.message : "Failed to load AI twin");
1955
+ $(A instanceof Error ? A.message : "Failed to load AI twin");
1856
1956
  }
1857
1957
  })(), () => {
1858
- f = !0, R.current?.dispose(), I?.dispose(), g.current = null;
1958
+ f = !0, N.current?.dispose(), T?.dispose(), g.current = null;
1859
1959
  };
1860
- }, [he, n, i, s, r, de, $]), oe(() => {
1861
- q === "done" && K && !M.current.isSpeaking() && H();
1862
- }, [q, K, H]), oe(() => {
1863
- const f = M.current, k = { current: !1 }, I = () => {
1864
- const T = u.current, b = g.current;
1865
- if (!T || !b || !K) {
1866
- V.current = requestAnimationFrame(I);
1960
+ }, [Ot, n, i, s, r, rt, $]), Te(() => {
1961
+ const f = B.current, k = { current: !1 }, T = () => {
1962
+ const A = u.current, C = g.current;
1963
+ if (!A || !C || !se) {
1964
+ H.current = requestAnimationFrame(T);
1867
1965
  return;
1868
1966
  }
1869
- const j = f.isSpeaking(), Y = f.getPlaybackElapsedMs(), ve = j ? ir(
1870
- Y,
1967
+ const Y = f.isSpeaking(), ne = me.current && !Y;
1968
+ ne && (v.current.reset(), N.current?.setActive(!1), _e());
1969
+ const oe = f.getPlaybackElapsedMs(), we = Y ? gr(
1970
+ oe,
1871
1971
  f.getSentenceEmotions(),
1872
1972
  f.getWordQueue(),
1873
1973
  f.getStreamMood()
1874
- ) : f.getStreamMood();
1875
- if (j) {
1876
- if (W.current && R.current?.setActive(!1), f.getVisemeQueue().length === 0) {
1877
- V.current = requestAnimationFrame(I);
1974
+ ) : f.getStreamMood(), G = ne || L.current || U.current;
1975
+ if (Y) {
1976
+ X.current && N.current?.setActive(!1);
1977
+ const re = f.getVisemeQueue();
1978
+ if (re.length === 0) {
1979
+ H.current = requestAnimationFrame(T);
1878
1980
  return;
1879
1981
  }
1880
- A.current.setTargetEmotion(ve), A.current.advance(performance.now());
1881
- const U = A.current.getExpressionPath(), X = A.current.getDrawKey();
1882
- gr(
1883
- T,
1982
+ const le = vt(re, oe);
1983
+ le !== "sil" && (ae.current = le), v.current.setTargetEmotion(we), v.current.advance(performance.now());
1984
+ const fe = v.current.getExpressionPath(), Ee = v.current.getDrawKey();
1985
+ Or(
1986
+ A,
1884
1987
  f.getVisemeQueue(),
1885
- Y,
1886
- U,
1887
- X,
1888
- p.current,
1889
- (te, Oe, Pe, vt) => b.drawLiveFrame(te, Oe, Pe, vt),
1890
- (te) => v.current?.(te)
1988
+ oe,
1989
+ fe,
1990
+ Ee,
1991
+ y.current,
1992
+ (Se, We, Pt, Ut) => C.drawLiveFrame(Se, We, Pt, Ut),
1993
+ (Se) => O.current?.(Se)
1891
1994
  );
1892
- } else if (W.current && R.current) {
1893
- const G = R.current, U = G.getVideo();
1894
- U.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA ? (G.setActive(!0), k.current || (k.current = !0, ot(
1895
- T,
1896
- U,
1995
+ } else if (!G && X.current && N.current) {
1996
+ const re = N.current, le = re.getVideo();
1997
+ le.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA ? (re.setActive(!0), k.current || (k.current = !0, _t(
1998
+ A,
1999
+ le,
1897
2000
  null,
1898
2001
  void 0,
1899
- N.current,
1900
- () => !f.isSpeaking()
1901
- ).then(() => v.current?.("idle")).finally(() => {
2002
+ J.current,
2003
+ () => !f.isSpeaking() && !L.current && !U.current
2004
+ ).then(() => O.current?.("idle")).finally(() => {
1902
2005
  k.current = !1;
1903
- }))) : ct(
1904
- T,
2006
+ }))) : gt(
2007
+ A,
1905
2008
  null,
1906
2009
  "__sil__",
1907
- p.current,
1908
- (X, te, Oe, Pe) => b.drawLiveFrame(X, te, Oe, Pe),
1909
- (X) => v.current?.(X)
2010
+ y.current,
2011
+ (fe, Ee, Se, We) => C.drawLiveFrame(fe, Ee, Se, We),
2012
+ (fe) => O.current?.(fe)
1910
2013
  );
1911
- } else
1912
- ct(
1913
- T,
1914
- null,
1915
- "__sil__",
1916
- p.current,
1917
- (G, U, X, te) => b.drawLiveFrame(G, U, X, te),
1918
- (G) => v.current?.(G)
1919
- );
1920
- x.current && !j && (A.current.reset(), R.current?.setActive(!1), H()), x.current = j, V.current = requestAnimationFrame(I);
2014
+ } else G || gt(
2015
+ A,
2016
+ null,
2017
+ "__sil__",
2018
+ y.current,
2019
+ (re, le, fe, Ee) => C.drawLiveFrame(re, le, fe, Ee),
2020
+ (re) => O.current?.(re)
2021
+ );
2022
+ me.current = Y, H.current = requestAnimationFrame(T);
1921
2023
  };
1922
- return V.current = requestAnimationFrame(I), () => {
1923
- cancelAnimationFrame(V.current), F(), f.stop();
2024
+ return H.current = requestAnimationFrame(T), () => {
2025
+ cancelAnimationFrame(H.current), b(), K(), f.stop();
1924
2026
  };
1925
- }, [K, Fe, H, F]), bt(
2027
+ }, [
2028
+ se,
2029
+ nt,
2030
+ _e,
2031
+ b,
2032
+ K
2033
+ ]), $t(
1926
2034
  w,
1927
2035
  () => ({
1928
2036
  speakText: async (f, k) => {
1929
- const I = f.trim();
1930
- if (!I) return;
1931
- const T = g.current;
1932
- if (!T || !C.current && !K)
2037
+ const T = f.trim();
2038
+ if (!T) return;
2039
+ const A = g.current;
2040
+ if (!A || !Q.current && !se)
1933
2041
  throw new Error("AI twin is not ready");
1934
- const { ttsEngineId: b, voiceId: j } = Q(k);
1935
- Z(null), y.current += 1, F(), _e(p.current), A.current.reset(), x.current = !1;
2042
+ const { ttsEngineId: C, voiceId: Y } = ce(k);
2043
+ ue(null), h.current += 1, b(), K(), L.current = !1, z.current = !1, ae.current = "sil", ge(y.current), v.current.reset(), me.current = !1;
1936
2044
  try {
1937
- W.current ? (R.current?.setActive(!1), R.current?.restart(), _e(p.current)) : await T.renderSilOnly(), await M.current.speak(I, {
1938
- voiceId: j,
2045
+ X.current ? (N.current?.setActive(!1), N.current?.restart(), ge(y.current)) : await A.renderSilOnly(), await B.current.speak(T, {
2046
+ voiceId: Y,
1939
2047
  speakingRate: k?.speakingRate ?? c,
1940
- ttsEngineId: b
2048
+ ttsEngineId: C
1941
2049
  });
1942
- } catch (Y) {
1943
- throw $(Y instanceof Error ? Y.message : "TTS failed"), Y;
2050
+ } catch (ne) {
2051
+ throw $(ne instanceof Error ? ne.message : "TTS failed"), ne;
1944
2052
  }
1945
2053
  },
1946
2054
  stop: () => {
1947
- y.current += 1, F(), M.current.stop(), _e(p.current), x.current = !1, H();
2055
+ h.current += 1, b(), K(), L.current = !1, z.current = !1, B.current.stop(), ge(y.current), me.current = !1, _e();
1948
2056
  },
1949
2057
  setTtsEngineId: (f) => {
1950
- M.current.setTtsEngineId(f), h.current && (h.current.ttsEngineId = f);
2058
+ B.current.setTtsEngineId(f), I.current && (I.current.ttsEngineId = f);
1951
2059
  },
1952
2060
  renderViseme: async (f, k) => {
1953
- const I = g.current, T = u.current;
1954
- if (!I || !T || !C.current)
2061
+ const T = g.current, A = u.current;
2062
+ if (!T || !A || !Q.current)
1955
2063
  throw new Error("AI twin is not ready");
1956
- await I.renderViseme(T, f, {
2064
+ await T.renderViseme(A, f, {
1957
2065
  from: k?.from,
1958
2066
  transitionDurationMs: k?.transitionDurationMs,
1959
2067
  gapMs: k?.gapMs,
1960
- onStatus: (b) => v.current?.(b)
2068
+ onStatus: (C) => O.current?.(C)
1961
2069
  });
1962
2070
  },
1963
- isReady: () => C.current,
1964
- getStatus: () => q
2071
+ isReady: () => Q.current,
2072
+ getStatus: () => Re
1965
2073
  }),
1966
2074
  [
1967
- K,
2075
+ se,
1968
2076
  c,
1969
- F,
1970
- H,
2077
+ b,
2078
+ _e,
1971
2079
  $,
1972
- Q,
1973
- q
2080
+ ce,
2081
+ Re
1974
2082
  ]
1975
- ), /* @__PURE__ */ It("div", { className: a, style: { ...Ir, ...l }, children: [
1976
- /* @__PURE__ */ Qe(
2083
+ ), /* @__PURE__ */ Bt("div", { className: a, style: { ...Hr, ...l }, children: [
2084
+ /* @__PURE__ */ it(
1977
2085
  "canvas",
1978
2086
  {
1979
2087
  ref: u,
1980
- width: se.width,
1981
- height: se.height,
1982
- style: { ...Tr, ...d },
2088
+ width: V.width,
2089
+ height: V.height,
2090
+ style: { ...Wr, ...d },
1983
2091
  "aria-label": t ? `AI twin ${t}` : "AI twin face"
1984
2092
  }
1985
2093
  ),
1986
- D && Ee ? /* @__PURE__ */ Qe("div", { style: Ar, children: Ee }) : null
2094
+ F && De ? /* @__PURE__ */ it("div", { style: qr, children: De }) : null
1987
2095
  ] });
1988
2096
  });
1989
2097
  export {
1990
- xr as AiTwin,
1991
- Ue as AiTwinNotFoundError,
1992
- ce as DEFAULT_TTS_ENGINE_ID,
1993
- on as OCULUS_VISEME_IDS,
1994
- Tn as SPEAKING_RATE_MAX,
1995
- In as SPEAKING_RATE_MIN,
1996
- pe as TTS_ENGINE_CARTESIA,
1997
- ht as TTS_ENGINE_GOOGLE,
1998
- Ke as TTS_ENGINE_INWORLD,
1999
- Dr as VISEME_IDS,
2000
- An as VISEME_TEST_CARTESIA_VOICE_ID,
2001
- wt as VISEME_TEST_SPEAKING_RATE,
2002
- _t as VISEME_TEST_VOICE_ID,
2003
- bn as createAvatarTtsLipsyncController,
2004
- $t as fetchAiTwin,
2005
- Ye as fetchDevAuthToken,
2006
- cn as normalizeOculusViseme,
2007
- Sn as resolveVisemeAtTime,
2008
- vn as resolveWordAtTime
2098
+ zr as AiTwin,
2099
+ Ke as AiTwinNotFoundError,
2100
+ pe as DEFAULT_TTS_ENGINE_ID,
2101
+ En as OCULUS_VISEME_IDS,
2102
+ Bn as SPEAKING_RATE_MAX,
2103
+ Un as SPEAKING_RATE_MIN,
2104
+ be as TTS_ENGINE_CARTESIA,
2105
+ kt as TTS_ENGINE_GOOGLE,
2106
+ Ze as TTS_ENGINE_INWORLD,
2107
+ Gr as VISEME_IDS,
2108
+ Nn as VISEME_TEST_CARTESIA_VOICE_ID,
2109
+ At as VISEME_TEST_SPEAKING_RATE,
2110
+ It as VISEME_TEST_VOICE_ID,
2111
+ Vn as createAvatarTtsLipsyncController,
2112
+ en as fetchAiTwin,
2113
+ ot as fetchDevAuthToken,
2114
+ kn as normalizeOculusViseme,
2115
+ vt as resolveVisemeAtTime,
2116
+ Pn as resolveWordAtTime
2009
2117
  };