@streamoji/aitwin 0.1.2 → 0.1.5

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