@streamoji/aitwin 0.1.9 → 0.2.1

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