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