@streamoji/aitwin 0.2.7 → 0.2.9

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