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