@streamoji/aitwin 0.1.2 → 0.1.4

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