@streamoji/aitwin 0.2.6 → 0.2.8

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