@streamoji/aitwin 0.3.6 → 0.3.7

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,47 +1,47 @@
1
- import { jsxs as dr, jsx as wn } from "react/jsx-runtime";
2
- import { forwardRef as mr, useRef as V, useState as ct, useEffect as Fe, useCallback as ge, useMemo as gn, useImperativeHandle as hr } from "react";
3
- import { PipecatClient as pr } from "@pipecat-ai/client-js";
4
- import { WebSocketTransport as wr, DailyMediaManager as gr, ProtobufFrameSerializer as yr } from "@pipecat-ai/websocket-transport";
5
- const _r = "https://aitwin.bubu.social", Nn = {
6
- publicBase: _r
7
- }, Sr = "0.3.6", kr = {
8
- version: Sr
9
- }, br = {}, dt = "https://ai.aitwin.me", jt = `${Nn.publicBase}/custom-faces`, vr = `${Nn.publicBase}/aitwin-workers/v${kr.version}/visemeDiffPreview.worker.js`;
10
- function Er(e) {
1
+ import { jsxs as yr, jsx as yn } from "react/jsx-runtime";
2
+ import { forwardRef as _r, useRef as O, useState as at, useEffect as Ce, useCallback as Se, useMemo as _n, useImperativeHandle as Sr } from "react";
3
+ import { PipecatClient as kr } from "@pipecat-ai/client-js";
4
+ import { WebSocketTransport as br, DailyMediaManager as vr, ProtobufFrameSerializer as Er } from "@pipecat-ai/websocket-transport";
5
+ const Tr = "https://aitwin.bubu.social", Wn = {
6
+ publicBase: Tr
7
+ }, Ar = "0.3.7", Ir = {
8
+ version: Ar
9
+ }, Mr = {}, lt = "https://ai.aitwin.me", jt = `${Wn.publicBase}/custom-faces`, Pr = `${Wn.publicBase}/aitwin-workers/v${Ir.version}/visemeDiffPreview.worker.js`;
10
+ function Rr(e) {
11
11
  if (e) return e;
12
- const t = typeof import.meta < "u" ? br?.VITE_AITWIN_WORKER_URL : void 0;
13
- return t?.trim() ? t.trim() : vr;
14
- }
15
- const Tr = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin", Bt = 3e3, ft = 16, yn = 3e3, Ar = 4e3, Ir = 0.85, Mr = 8, Un = "eng_a7f2b9c1", zt = "eng_d4e8f3a2", Je = "eng_c9b1e6d4", Ge = Je, Vn = /* @__PURE__ */ new Set([
16
- zt,
17
- Je
18
- ]), Wn = {
19
- [Un]: "oculus_direct",
20
- [Je]: "oculus_direct",
21
- [zt]: "inworld_word"
22
- }, _n = {
23
- google: Un,
24
- inworld: zt,
25
- cartesia: Je
12
+ const t = typeof import.meta < "u" ? Mr?.VITE_AITWIN_WORKER_URL : void 0;
13
+ return t?.trim() ? t.trim() : Pr;
14
+ }
15
+ const Dr = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin", Ct = 3e3, ut = 16, Sn = 3e3, xr = 4e3, Br = 0.85, Cr = 8, zt = 400, Hn = "eng_a7f2b9c1", Yt = "eng_d4e8f3a2", Xe = "eng_c9b1e6d4", je = Xe, $n = /* @__PURE__ */ new Set([
16
+ Yt,
17
+ Xe
18
+ ]), qn = {
19
+ [Hn]: "oculus_direct",
20
+ [Xe]: "oculus_direct",
21
+ [Yt]: "inworld_word"
22
+ }, kn = {
23
+ google: Hn,
24
+ inworld: Yt,
25
+ cartesia: Xe
26
26
  };
27
- function Pr(e) {
28
- return Wn[e];
27
+ function Fr(e) {
28
+ return qn[e];
29
29
  }
30
- function Dr(e) {
31
- return e === Je;
30
+ function Lr(e) {
31
+ return e === Xe;
32
32
  }
33
- function Rr(e) {
34
- if (e.ttsEngineId && e.ttsEngineId in Wn)
33
+ function Or(e) {
34
+ if (e.ttsEngineId && e.ttsEngineId in qn)
35
35
  return e.ttsEngineId;
36
36
  const t = e.tts?.toLowerCase();
37
- return t && t in _n ? _n[t] : Ge;
37
+ return t && t in kn ? kn[t] : je;
38
38
  }
39
39
  class Ut extends Error {
40
40
  constructor(t = "AI twin not found") {
41
41
  super(t), this.name = "AiTwinNotFoundError";
42
42
  }
43
43
  }
44
- async function xr(e, t = Tr) {
44
+ async function Nr(e, t = Dr) {
45
45
  const n = new URL(t);
46
46
  n.searchParams.set("id", e);
47
47
  const r = await fetch(n.toString());
@@ -55,22 +55,22 @@ async function xr(e, t = Tr) {
55
55
  const { tts: i, token: a, ...o } = s.data, f = String(o.authToken ?? a ?? "").trim() || void 0;
56
56
  return {
57
57
  ...o,
58
- ttsEngineId: Rr(s.data),
58
+ ttsEngineId: Or(s.data),
59
59
  authToken: f
60
60
  };
61
61
  }
62
- const Cr = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken", Br = "client_6TNvp3SCs4Og0a1ijm9TommXLql1", Fr = "kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1", Lr = "6TNvp3SCs4Og0a1ijm9TommXLql1", Or = "Swaraj Mali";
62
+ const Ur = "https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken", Vr = "client_6TNvp3SCs4Og0a1ijm9TommXLql1", Wr = "kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1", Hr = "6TNvp3SCs4Og0a1ijm9TommXLql1", $r = "Swaraj Mali";
63
63
  async function Ft() {
64
- const e = await fetch(Cr, {
64
+ const e = await fetch(Ur, {
65
65
  method: "POST",
66
66
  headers: {
67
67
  "Content-Type": "application/json",
68
- "Client-Id": Br,
69
- "Client-Secret": Fr
68
+ "Client-Id": Vr,
69
+ "Client-Secret": Wr
70
70
  },
71
71
  body: JSON.stringify({
72
- userId: Lr,
73
- userName: Or
72
+ userId: Hr,
73
+ userName: $r
74
74
  })
75
75
  });
76
76
  if (!e.ok)
@@ -80,7 +80,7 @@ async function Ft() {
80
80
  throw new Error("getAuthToken returned no token");
81
81
  return n;
82
82
  }
83
- async function Nr(e, t = dt) {
83
+ async function qr(e, t = lt) {
84
84
  const n = await fetch(`${t.replace(/\/$/, "")}/api/session-value`, {
85
85
  headers: { Authorization: `Bearer ${e}` }
86
86
  });
@@ -90,7 +90,7 @@ async function Nr(e, t = dt) {
90
90
  if (!r.value) throw new Error("Session value response missing value");
91
91
  return r;
92
92
  }
93
- function Ur(e) {
93
+ function Kr(e) {
94
94
  const t = e.trim();
95
95
  if (t.length % 2 !== 0) throw new Error("Invalid hex key");
96
96
  const n = new Uint8Array(t.length / 2);
@@ -98,20 +98,20 @@ function Ur(e) {
98
98
  n[r / 2] = Number.parseInt(t.slice(r, r + 2), 16);
99
99
  return n;
100
100
  }
101
- function wt(e) {
101
+ function gt(e) {
102
102
  return Uint8Array.from(e);
103
103
  }
104
- function Vr(e, t) {
104
+ function Gr(e, t) {
105
105
  const n = new Uint8Array(e.length + t.length);
106
106
  return n.set(e, 0), n.set(t, e.length), n;
107
107
  }
108
- async function Wr(e, t) {
108
+ async function jr(e, t) {
109
109
  if (!t) throw new Error("Asset key is required for encrypted assets");
110
110
  if (e.byteLength < 28)
111
111
  throw new Error("Encrypted payload too small");
112
- const n = new Uint8Array(e), r = wt(n.subarray(0, 12)), s = wt(n.subarray(12, 28)), i = wt(n.subarray(28)), a = wt(Vr(i, s)), o = await crypto.subtle.importKey(
112
+ const n = new Uint8Array(e), r = gt(n.subarray(0, 12)), s = gt(n.subarray(12, 28)), i = gt(n.subarray(28)), a = gt(Gr(i, s)), o = await crypto.subtle.importKey(
113
113
  "raw",
114
- Ur(t),
114
+ Kr(t),
115
115
  { name: "AES-GCM" },
116
116
  !1,
117
117
  ["decrypt"]
@@ -124,33 +124,33 @@ async function Wr(e, t) {
124
124
  }
125
125
  let Vt = null;
126
126
  const Wt = /* @__PURE__ */ new Set();
127
- function gt(e) {
127
+ function yt(e) {
128
128
  return e.includes("/custom-faces/");
129
129
  }
130
- function kt(e) {
130
+ function bt(e) {
131
131
  const t = e.binBase.trim().replace(/\/$/, ""), n = e.twinBase.trim().replace(/\/$/, "");
132
- if (!(e.encrypted || gt(t) || gt(n)))
132
+ if (!(e.encrypted || yt(t) || yt(n)))
133
133
  return { twinBase: n, binBase: t, encrypted: !1 };
134
- const s = gt(t) ? t : gt(n) ? n : t;
134
+ const s = yt(t) ? t : yt(n) ? n : t;
135
135
  return { twinBase: s, binBase: s, encrypted: !0 };
136
136
  }
137
- function Xe(e) {
138
- return kt(e).encrypted;
137
+ function Qe(e) {
138
+ return bt(e).encrypted;
139
139
  }
140
- function Sn(e, t = jt) {
140
+ function bn(e, t = jt) {
141
141
  const n = `${t.replace(/\/$/, "")}/${e}`;
142
142
  return { twinBase: n, binBase: n, encrypted: !0 };
143
143
  }
144
- function Hr(e, t) {
144
+ function zr(e, t) {
145
145
  return (e ?? t)?.trim() || void 0;
146
146
  }
147
- function $r(e) {
148
- Vt = kt(e);
147
+ function Yr(e) {
148
+ Vt = bt(e);
149
149
  for (const t of Wt) t();
150
150
  }
151
- function qr(e, t) {
151
+ function Jr(e, t) {
152
152
  const n = typeof globalThis.location < "u" ? globalThis.location.href : void 0;
153
- if (!n) return kt(e);
153
+ if (!n) return bt(e);
154
154
  const r = (s) => {
155
155
  try {
156
156
  return new URL(s, n).href.replace(/\/$/, "");
@@ -158,7 +158,7 @@ function qr(e, t) {
158
158
  return s;
159
159
  }
160
160
  };
161
- return kt({
161
+ return bt({
162
162
  twinBase: r(e.twinBase),
163
163
  binBase: r(e.binBase),
164
164
  encrypted: e.encrypted
@@ -169,33 +169,33 @@ function Ue() {
169
169
  throw new Error("Twin assets not configured; call setActiveTwinAssets first");
170
170
  return Vt;
171
171
  }
172
- function Kr(e) {
172
+ function Xr(e) {
173
173
  return Wt.add(e), () => Wt.delete(e);
174
174
  }
175
- function kn() {
175
+ function vn() {
176
176
  const e = Ue();
177
- return Xe(e) ? `${e.binBase}/sil.bin` : `${e.twinBase}/sil.png`;
177
+ return Qe(e) ? `${e.binBase}/sil.bin` : `${e.twinBase}/sil.png`;
178
178
  }
179
- function Gr() {
179
+ function Qr() {
180
180
  const e = Ue();
181
- return Xe(e) ? `${e.binBase}/idle.bin` : `${e.twinBase}/idle.mp4`;
181
+ return Qe(e) ? `${e.binBase}/idle.bin` : `${e.twinBase}/idle.mp4`;
182
182
  }
183
- function jr() {
183
+ function Zr() {
184
184
  const e = Ue();
185
- return Xe(e) ? e.binBase : e.twinBase;
185
+ return Qe(e) ? e.binBase : e.twinBase;
186
186
  }
187
- function At() {
188
- return Xe(Ue());
187
+ function It() {
188
+ return Qe(Ue());
189
189
  }
190
- function zr() {
190
+ function es() {
191
191
  const e = Ue();
192
- return Xe(e) ? `${e.binBase}/atlas.bin` : `${e.twinBase}/atlas.json`;
192
+ return Qe(e) ? `${e.binBase}/atlas.bin` : `${e.twinBase}/atlas.json`;
193
193
  }
194
- function Yr() {
194
+ function ts() {
195
195
  const e = Ue();
196
- return Xe(e) ? `${e.binBase}/expression_atlas.bin` : `${e.twinBase}/expression_atlas.json`;
196
+ return Qe(e) ? `${e.binBase}/expression_atlas.bin` : `${e.twinBase}/expression_atlas.json`;
197
197
  }
198
- function Hn(e) {
198
+ function Kn(e) {
199
199
  const t = e.split(/\r?\n/);
200
200
  let n = "", r = "";
201
201
  for (const i of t)
@@ -210,7 +210,7 @@ function Hn(e) {
210
210
  }
211
211
  return { event: n, data: s };
212
212
  }
213
- const Jr = [
213
+ const ns = [
214
214
  "aa",
215
215
  "CH",
216
216
  "DD",
@@ -226,14 +226,14 @@ const Jr = [
226
226
  "kk",
227
227
  "nn",
228
228
  "sil"
229
- ], Xr = new Map(
230
- Jr.map((e) => [e.toLowerCase(), e])
229
+ ], rs = new Map(
230
+ ns.map((e) => [e.toLowerCase(), e])
231
231
  );
232
- function Qr(e) {
232
+ function ss(e) {
233
233
  const t = (e ?? "").trim();
234
- return t ? Xr.get(t.toLowerCase()) ?? "sil" : "sil";
234
+ return t ? rs.get(t.toLowerCase()) ?? "sil" : "sil";
235
235
  }
236
- const Zr = {
236
+ const is = {
237
237
  aei: ["aa", "E", "I"],
238
238
  o: ["O", "U"],
239
239
  ee: ["I"],
@@ -245,12 +245,12 @@ const Zr = {
245
245
  qw: ["U", "O"],
246
246
  cdgknstxyz: ["DD", "SS", "kk", "CH"]
247
247
  };
248
- function es(e) {
248
+ function as(e) {
249
249
  if (!e) return ["sil"];
250
250
  const t = e.toLowerCase();
251
- return Zr[t] ?? ["sil"];
251
+ return is[t] ?? ["sil"];
252
252
  }
253
- function ts(e, t) {
253
+ function os(e, t) {
254
254
  const n = e.word.length;
255
255
  if (n <= 0) return 0;
256
256
  const r = e.wduration;
@@ -258,40 +258,40 @@ function ts(e, t) {
258
258
  const s = Math.max(0, Math.min(1, (t - e.wtime) / r));
259
259
  return Math.min(n - 1, Math.floor(s * n));
260
260
  }
261
- function je(e, t) {
261
+ function ze(e, t) {
262
262
  const n = Math.max(0, Math.min(e.length - 1, t));
263
263
  return e[n] ?? "";
264
264
  }
265
- function Yt(e, t) {
265
+ function Jt(e, t) {
266
266
  const n = e.toLowerCase().replace(/[^a-z']/g, "");
267
- return !n || je(n, t) !== "w" || t > 0 && je(n, t - 1) === "q" ? !1 : t === 0 && /^w[aeiouy]/.test(n);
267
+ return !n || ze(n, t) !== "w" || t > 0 && ze(n, t - 1) === "q" ? !1 : t === 0 && /^w[aeiouy]/.test(n);
268
268
  }
269
- function ns(e, t) {
270
- const n = e.toLowerCase(), r = je(n, t), s = n.slice(Math.max(0, t - 1), t + 2);
269
+ function cs(e, t) {
270
+ const n = e.toLowerCase(), r = ze(n, t), s = n.slice(Math.max(0, t - 1), t + 2);
271
271
  return r === "i" || r === "y" ? "I" : s.includes("ee") || r === "e" || n.includes("ea") ? "E" : "aa";
272
272
  }
273
- function rs(e, t) {
273
+ function us(e, t) {
274
274
  const n = e.toLowerCase(), r = n.slice(
275
275
  Math.max(0, t - 1),
276
276
  Math.min(n.length, t + 3)
277
277
  );
278
278
  if (/oo|ou|uw/.test(r)) return "U";
279
- if (Yt(n, t)) return "FF";
280
- const s = je(n, t);
279
+ if (Jt(n, t)) return "FF";
280
+ const s = ze(n, t);
281
281
  return s === "u" || s === "w" ? "U" : "O";
282
282
  }
283
- function ss(e, t) {
283
+ function ls(e, t) {
284
284
  const n = e.toLowerCase();
285
- if (Yt(n, t)) return "FF";
285
+ if (Jt(n, t)) return "FF";
286
286
  const r = n.slice(Math.max(0, t - 2), t + 1);
287
287
  if (/qu/.test(r)) return "U";
288
288
  const s = n.slice(
289
289
  Math.max(0, t - 1),
290
290
  Math.min(n.length, t + 3)
291
291
  );
292
- return /wo|wh/.test(s) || je(n, t) === "o" ? "O" : "U";
292
+ return /wo|wh/.test(s) || ze(n, t) === "o" ? "O" : "U";
293
293
  }
294
- const yt = {
294
+ const _t = {
295
295
  d: "DD",
296
296
  t: "DD",
297
297
  n: "DD",
@@ -303,50 +303,121 @@ const yt = {
303
303
  x: "CH",
304
304
  j: "CH"
305
305
  };
306
- function is(e, t) {
307
- const n = e.toLowerCase(), r = je(n, t);
308
- if (yt[r]) return yt[r];
306
+ function fs(e, t) {
307
+ const n = e.toLowerCase(), r = ze(n, t);
308
+ if (_t[r]) return _t[r];
309
309
  for (let s = 0; s < n.length; s++) {
310
310
  const i = n[s];
311
- if (yt[i]) return yt[i];
311
+ if (_t[i]) return _t[i];
312
312
  }
313
313
  return "DD";
314
314
  }
315
- function as(e, t, n) {
316
- const r = es(e);
315
+ function ds(e, t, n) {
316
+ const r = as(e);
317
317
  if (r.length === 1) return r[0];
318
318
  const s = t.trim();
319
319
  if (!s) return r[0];
320
320
  const i = (e ?? "").toLowerCase(), a = Math.max(0, n);
321
321
  switch (i) {
322
322
  case "aei":
323
- return ns(s, a);
323
+ return cs(s, a);
324
324
  case "o":
325
- return rs(s, a);
325
+ return us(s, a);
326
326
  case "qw":
327
- return ss(s, a);
327
+ return ls(s, a);
328
328
  case "cdgknstxyz":
329
- return is(s, a);
329
+ return fs(s, a);
330
330
  default:
331
331
  return r[0];
332
332
  }
333
333
  }
334
- const Jt = 16e3;
335
- function $n(e) {
334
+ const ms = 32;
335
+ function Gn(e) {
336
+ let t = 0;
337
+ for (const n of e)
338
+ t = Math.max(t, n.vtime + n.vduration);
339
+ return t;
340
+ }
341
+ function jn(e) {
342
+ let t = null, n = -1;
343
+ for (const r of e) {
344
+ if (r.viseme === "sil") continue;
345
+ const s = r.vtime + r.vduration;
346
+ s > n && (t = r, n = s);
347
+ }
348
+ return t;
349
+ }
350
+ function hs(e) {
351
+ return zn(e).find((n) => n.viseme !== "sil") ?? null;
352
+ }
353
+ function Fe(e) {
354
+ const t = Gn(e);
355
+ return jn(e) ? t + zt : t;
356
+ }
357
+ function ps(e) {
358
+ const t = [], n = hs(e);
359
+ n && n.vtime > 0 && t.push({
360
+ from: "sil",
361
+ to: n.viseme,
362
+ fromVtime: 0,
363
+ toVtime: n.vtime
364
+ });
365
+ const r = jn(e), s = Gn(e);
366
+ return r && t.push({
367
+ from: r.viseme,
368
+ to: "sil",
369
+ fromVtime: s,
370
+ toVtime: s + zt
371
+ }), t;
372
+ }
373
+ function zn(e) {
374
+ const t = /* @__PURE__ */ new Map();
375
+ for (const n of e)
376
+ t.has(n.vtime) || t.set(n.vtime, n);
377
+ return [...t.values()].sort((n, r) => n.vtime - r.vtime);
378
+ }
379
+ function ws(e) {
380
+ const t = zn(e), n = [];
381
+ for (let r = 0; r < t.length - 1; r++) {
382
+ const s = t[r].viseme, i = t[r + 1].viseme, a = t[r].vtime, o = t[r + 1].vtime;
383
+ s === i || o <= a || n.push({ from: s, to: i, fromVtime: a, toVtime: o });
384
+ }
385
+ return n;
386
+ }
387
+ function gs(e) {
388
+ return e.length === 0 ? [] : [
389
+ ...ps(e),
390
+ ...ws(e)
391
+ ];
392
+ }
393
+ function ys(e, t) {
394
+ let n = null;
395
+ for (const r of e) {
396
+ const { transEnd: s } = Yn(r.fromVtime, r.toVtime);
397
+ t >= r.fromVtime && t < s && (n = r);
398
+ }
399
+ return n;
400
+ }
401
+ function Yn(e, t) {
402
+ const n = Math.max(0, t - e), r = n > 0 ? Math.max(n, ms) : 0, s = e, i = e + r;
403
+ return { gapMs: n, effectiveGapMs: r, holdEnd: e, transStart: s, transEnd: i };
404
+ }
405
+ const Xt = 16e3;
406
+ function Jn(e) {
336
407
  const t = Number(e);
337
- return Number.isFinite(t) && t > 0 ? t : Jt;
408
+ return Number.isFinite(t) && t > 0 ? t : Xt;
338
409
  }
339
- function os(e) {
410
+ function _s(e) {
340
411
  let t = e.trim();
341
412
  const n = t.indexOf(",");
342
413
  t.startsWith("data:") && n >= 0 && (t = t.slice(n + 1)), t = t.replace(/\s+/g, "").replace(/-/g, "+").replace(/_/g, "/");
343
414
  const r = t.length % 4;
344
415
  return r !== 0 && (t += "=".repeat(4 - r)), t;
345
416
  }
346
- function cs(e) {
417
+ function Ss(e) {
347
418
  return e.length >= 4 && e[0] === 82 && e[1] === 73 && e[2] === 70 && e[3] === 70;
348
419
  }
349
- function bn(e, t, n = Jt) {
420
+ function En(e, t, n = Xt) {
350
421
  const r = t.byteOffset % 2 === 0 ? t : t.slice(), s = new Int16Array(
351
422
  r.buffer,
352
423
  r.byteOffset,
@@ -357,15 +428,15 @@ function bn(e, t, n = Jt) {
357
428
  const a = e.createBuffer(1, i.length, n);
358
429
  return a.copyToChannel(i, 0), a;
359
430
  }
360
- function us(e) {
361
- const t = os(e), n = window.atob(t), r = new Uint8Array(n.length);
431
+ function ks(e) {
432
+ const t = _s(e), n = window.atob(t), r = new Uint8Array(n.length);
362
433
  for (let s = 0; s < n.length; s++)
363
434
  r[s] = n.charCodeAt(s);
364
435
  return r;
365
436
  }
366
- async function ls(e, t, n = Jt) {
367
- const r = us(t), s = $n(n);
368
- if (cs(r)) {
437
+ async function bs(e, t, n = Xt) {
438
+ const r = ks(t), s = Jn(n);
439
+ if (Ss(r)) {
369
440
  const i = r.buffer.slice(
370
441
  r.byteOffset,
371
442
  r.byteOffset + r.byteLength
@@ -373,7 +444,7 @@ async function ls(e, t, n = Jt) {
373
444
  return e.decodeAudioData(i);
374
445
  }
375
446
  if (r.length >= 2 && r.length % 2 === 0)
376
- return bn(e, r, s);
447
+ return En(e, r, s);
377
448
  try {
378
449
  const i = r.buffer.slice(
379
450
  r.byteOffset,
@@ -381,166 +452,166 @@ async function ls(e, t, n = Jt) {
381
452
  );
382
453
  return await e.decodeAudioData(i);
383
454
  } catch {
384
- return bn(e, r, s);
455
+ return En(e, r, s);
385
456
  }
386
457
  }
387
- function xe(e) {
458
+ function Tn(e) {
388
459
  let t = 0;
389
460
  for (const n of e)
390
461
  t = Math.max(t, n.vtime + n.vduration);
391
462
  return t;
392
463
  }
393
- function fs(e, t) {
464
+ function vs(e, t) {
394
465
  for (const n of e)
395
466
  if (t >= n.wtime && t < n.wtime + n.wduration)
396
467
  return n;
397
468
  return null;
398
469
  }
399
- function qn(e, t) {
470
+ function Xn(e, t) {
400
471
  const n = [], r = [];
401
- let s = null, i = !1, a = !1, o = !1, f = !1, d = 0, u = 0, c = 0, m = 0, g = null, b = 1, h = t, M = !1, E = null, _ = 0, p = !1, v = !1, O = 0;
402
- const F = [], z = () => {
403
- v = !1, O = 0, F.length = 0;
404
- }, x = () => {
405
- g != null && (clearTimeout(g), g = null);
472
+ let s = null, i = !1, a = !1, o = !1, f = !1, d = 0, u = 0, c = 0, m = 0, w = null, b = 1, h = t, I = !1, E = null, _ = 0, p = !1, v = !1, N = 0;
473
+ const F = [], j = () => {
474
+ v = !1, N = 0, F.length = 0;
475
+ }, R = () => {
476
+ w != null && (clearTimeout(w), w = null);
406
477
  }, S = () => {
407
- for (const w of r) {
478
+ for (const y of r) {
408
479
  try {
409
- w.onended = null, w.stop(0);
480
+ y.onended = null, y.stop(0);
410
481
  } catch {
411
482
  }
412
483
  try {
413
- w.disconnect();
484
+ y.disconnect();
414
485
  } catch {
415
486
  }
416
487
  }
417
488
  r.length = 0;
418
- }, P = () => {
419
- for (const w of r)
420
- w.playbackRate.value = b;
489
+ }, M = () => {
490
+ for (const y of r)
491
+ y.playbackRate.value = b;
421
492
  }, T = () => {
422
- if (!(M || !f || i || r.length > 0 || n.length > 0)) {
493
+ if (!(I || !f || i || r.length > 0 || n.length > 0)) {
423
494
  if (o && s && u > 0) {
424
- const w = (s.currentTime - u) * 1e3;
425
- if (w < _) {
426
- x();
427
- const R = b > 0 ? (_ - w) / b : _ - w;
428
- g = setTimeout(() => {
429
- M || (a = !1, g = null, e.onSpeakingDone(), e.onNotify());
430
- }, Math.max(0, R));
495
+ const y = (s.currentTime - u) * 1e3;
496
+ if (y < _) {
497
+ R();
498
+ const A = b > 0 ? (_ - y) / b : _ - y;
499
+ w = setTimeout(() => {
500
+ I || (a = !1, w = null, e.onSpeakingDone(), e.onNotify());
501
+ }, Math.max(0, A));
431
502
  return;
432
503
  }
433
504
  }
434
- a = !1, x(), e.onSpeakingDone(), e.onNotify();
505
+ a = !1, R(), e.onSpeakingDone(), e.onNotify();
435
506
  }
436
- }, j = (w) => {
437
- const R = xe(w);
438
- if (R <= 0) {
507
+ }, Z = (y) => {
508
+ const A = Fe(y);
509
+ if (A <= 0) {
439
510
  e.onSpeakingDone(), e.onNotify();
440
511
  return;
441
512
  }
442
- a = !0, d = performance.now(), e.onSpeakingStart(), e.onNotify(), x();
443
- const q = b > 0 ? R / b : R;
444
- g = setTimeout(() => {
445
- M || (a = !1, g = null, e.onSpeakingDone(), e.onNotify());
513
+ a = !0, d = performance.now(), e.onSpeakingStart(), e.onNotify(), R();
514
+ const q = b > 0 ? A / b : A;
515
+ w = setTimeout(() => {
516
+ I || (a = !1, w = null, e.onSpeakingDone(), e.onNotify());
446
517
  }, q);
447
- }, I = (w, R, q, L) => {
448
- R.length !== 0 && (L && (c = q), R.forEach((D) => {
449
- const A = String(D.word ?? "").trim();
450
- if (!A) return;
451
- const U = Math.round((D.start ?? 0) * 1e3), re = D.duration != null ? Math.round(D.duration * 1e3) : D.end != null ? Math.max(1, Math.round((D.end - (D.start ?? 0)) * 1e3)) : 0;
452
- w.push({
453
- word: A,
454
- wtime: c + U,
455
- wduration: re,
456
- queueIndex: w.length,
457
- gesture: D.gesture ?? null,
458
- emphasis: D.emphasis === !0
518
+ }, D = (y, A, q, x) => {
519
+ A.length !== 0 && (x && (c = q), A.forEach((P) => {
520
+ const C = String(P.word ?? "").trim();
521
+ if (!C) return;
522
+ const B = Math.round((P.start ?? 0) * 1e3), Y = P.duration != null ? Math.round(P.duration * 1e3) : P.end != null ? Math.max(1, Math.round((P.end - (P.start ?? 0)) * 1e3)) : 0;
523
+ y.push({
524
+ word: C,
525
+ wtime: c + B,
526
+ wduration: Y,
527
+ queueIndex: y.length,
528
+ gesture: P.gesture ?? null,
529
+ emphasis: P.emphasis === !0
459
530
  });
460
531
  }));
461
- }, ne = (w, R, q, L, D, N) => {
462
- q.length !== 0 && (D && (c = L), q.forEach((A) => {
463
- const U = String(A.symbol ?? A.viseme ?? "").trim();
464
- if (!U) return;
465
- const re = Math.round((A.start ?? 0) * 1e3), ee = Math.max(1, Math.round((A.duration ?? 0) * 1e3)), de = c + re, ue = fs(R, de), ae = ue ? ts(ue, de) : 0;
466
- let we;
467
- Pr(N) === "oculus_direct" ? (we = Qr(U), we === "U" && ue && Yt(ue.word, ae) && (we = "FF")) : we = as(
468
- U,
469
- ue?.word ?? "",
470
- ae
471
- ), w.push({
472
- viseme: we,
532
+ }, X = (y, A, q, x, P, $) => {
533
+ q.length !== 0 && (P && (c = x), q.forEach((C) => {
534
+ const B = String(C.symbol ?? C.viseme ?? "").trim();
535
+ if (!B) return;
536
+ const Y = Math.round((C.start ?? 0) * 1e3), ee = Math.max(1, Math.round((C.duration ?? 0) * 1e3)), pe = c + Y, me = vs(A, pe), le = me ? os(me, pe) : 0;
537
+ let fe;
538
+ Fr($) === "oculus_direct" ? (fe = ss(B), fe === "U" && me && Jt(me.word, le) && (fe = "FF")) : fe = ds(
539
+ B,
540
+ me?.word ?? "",
541
+ le
542
+ ), y.push({
543
+ viseme: fe,
473
544
  weight: 1,
474
- vtime: de,
545
+ vtime: pe,
475
546
  vduration: ee
476
547
  });
477
548
  }), e.onNotify());
478
- }, Z = (w, R, q, L, D, N, A) => {
479
- I(R, L, D, N), ne(
480
- w,
481
- R,
549
+ }, z = (y, A, q, x, P, $, C) => {
550
+ D(A, x, P, $), X(
551
+ y,
552
+ A,
482
553
  q,
483
- D,
484
- N,
485
- A
554
+ P,
555
+ $,
556
+ C
486
557
  );
487
- }, X = (w, R) => {
558
+ }, se = (y, A) => {
488
559
  if (!s) return;
489
- const q = s, L = q.currentTime;
490
- let D = R?.startTime ?? m;
491
- const N = R?.isFirstAudio ?? !a;
492
- D < L && (D = L + 0.1), m = D + w.duration;
493
- const A = q.createBufferSource();
494
- if (A.buffer = w, A.playbackRate.value = b, A.connect(q.destination), E && A.connect(E), r.push(A), M) {
560
+ const q = s, x = q.currentTime;
561
+ let P = A?.startTime ?? m;
562
+ const $ = A?.isFirstAudio ?? !a;
563
+ P < x && (P = x + 0.1), m = P + y.duration;
564
+ const C = q.createBufferSource();
565
+ if (C.buffer = y, C.playbackRate.value = b, C.connect(q.destination), E && C.connect(E), r.push(C), I) {
495
566
  r.pop();
496
567
  return;
497
568
  }
498
- if (N) {
499
- a = !0, e.onSpeakingStart(), R?.isFirstOfStream ? (u = D, c = 0, R.onNewStream?.()) : u <= 0 && (u = D);
500
- const U = (D - L) * 1e3;
501
- d = performance.now() + U;
569
+ if ($) {
570
+ a = !0, e.onSpeakingStart(), A?.isFirstOfStream ? (u = P, c = 0, A.onNewStream?.()) : u <= 0 && (u = P);
571
+ const B = (P - x) * 1e3;
572
+ d = performance.now() + B;
502
573
  }
503
- A.onended = () => {
504
- const U = r.indexOf(A);
505
- U >= 0 && r.splice(U, 1), T(), e.onNotify();
506
- }, A.start(D);
507
- }, he = () => {
574
+ C.onended = () => {
575
+ const B = r.indexOf(C);
576
+ B >= 0 && r.splice(B, 1), T(), e.onNotify();
577
+ }, C.start(P);
578
+ }, ne = () => {
508
579
  if (!v || F.length === 0) return;
509
580
  v = !1;
510
- const w = F.splice(0);
511
- O = 0;
512
- for (let R = 0; R < w.length; R++)
513
- X(w[R].audioBuffer, {
514
- isFirstAudio: R === 0 && !a,
515
- isFirstOfStream: R === 0
581
+ const y = F.splice(0);
582
+ N = 0;
583
+ for (let A = 0; A < y.length; A++)
584
+ se(y[A].audioBuffer, {
585
+ isFirstAudio: A === 0 && !a,
586
+ isFirstOfStream: A === 0
516
587
  });
517
588
  e.onNotify();
518
- }, se = async (w, R, q, L, D, N, A) => {
519
- if (M) return;
520
- const U = $n(A?.sampleRate), re = A?.newStream === !0;
589
+ }, de = async (y, A, q, x, P, $, C) => {
590
+ if (I) return;
591
+ const B = Jn(C?.sampleRate), Y = C?.newStream === !0;
521
592
  if (!q?.trim()) {
522
- if (L.length === 0 && D.length === 0) return;
523
- re && (c = 0, _ = 0, w.length = 0, R.length = 0, A?.onNewStream?.());
593
+ if (x.length === 0 && P.length === 0) return;
594
+ Y && (c = 0, _ = 0, y.length = 0, A.length = 0, C?.onNewStream?.());
524
595
  let ee = c;
525
- N && w.length > 0 && !re && (ee = xe(w)), Z(
526
- w,
527
- R,
528
- L,
529
- D,
596
+ $ && y.length > 0 && !Y && (ee = Tn(y)), z(
597
+ y,
598
+ A,
599
+ x,
600
+ P,
530
601
  ee,
531
- N,
602
+ $,
532
603
  h
533
- ), _ = Math.max(_, xe(w)), !a && _ > 0 && j(w), e.onNotify();
604
+ ), _ = Math.max(_, Fe(y)), !a && _ > 0 && Z(y), e.onNotify();
534
605
  return;
535
606
  }
536
607
  if (i) {
537
608
  n.push({
538
609
  audio: q,
539
- visemes: L,
540
- words: D,
541
- isNewSegment: N,
542
- sampleRate: U,
543
- newStream: re
610
+ visemes: x,
611
+ words: P,
612
+ isNewSegment: $,
613
+ sampleRate: B,
614
+ newStream: Y
544
615
  });
545
616
  return;
546
617
  }
@@ -548,53 +619,53 @@ function qn(e, t) {
548
619
  try {
549
620
  const ee = window.AudioContext || window.webkitAudioContext;
550
621
  s || (s = new ee());
551
- const de = s;
552
- de.state === "suspended" && await de.resume(), E || (E = de.createMediaStreamDestination());
553
- const ue = await ls(
554
- de,
622
+ const pe = s;
623
+ pe.state === "suspended" && await pe.resume(), E || (E = pe.createMediaStreamDestination());
624
+ const me = await bs(
625
+ pe,
555
626
  q,
556
- U
627
+ B
557
628
  );
558
629
  o = !0;
559
- const ae = !a;
560
- if (p && (e.shouldHoldForWarmup?.() ?? !1) && (v || ae && re)) {
561
- v || (v = !0, O = 0, F.length = 0, re && (c = 0, _ = 0, w.length = 0, R.length = 0, A?.onNewStream?.())), Z(
562
- w,
563
- R,
564
- L,
565
- D,
566
- O,
630
+ const le = !a;
631
+ if (p && (e.shouldHoldForWarmup?.() ?? !1) && (v || le && Y)) {
632
+ v || (v = !0, N = 0, F.length = 0, Y && (c = 0, _ = 0, y.length = 0, A.length = 0, C?.onNewStream?.())), z(
633
+ y,
634
+ A,
635
+ x,
636
+ P,
567
637
  N,
638
+ $,
568
639
  h
569
- ), O += Math.round(ue.duration * 1e3), F.push({ audioBuffer: ue }), _ = Math.max(_, xe(w)), e.onNotify();
640
+ ), N += Math.round(me.duration * 1e3), F.push({ audioBuffer: me }), _ = Math.max(_, Fe(y)), e.onNotify();
570
641
  return;
571
642
  }
572
- ae && re && (_ = 0, w.length = 0, R.length = 0, c = 0, A?.onNewStream?.());
573
- const Qe = de.currentTime;
574
- let Ee = m;
575
- Ee < Qe && (Ee = Qe + 0.1);
576
- const Ze = ae && re ? 0 : u > 0 ? Math.max(0, (Ee - u) * 1e3) : 0;
577
- X(ue, {
578
- startTime: Ee,
579
- isFirstAudio: ae,
580
- isFirstOfStream: ae && re
581
- }), Z(
582
- w,
583
- R,
584
- L,
585
- D,
586
- Ze,
587
- N,
643
+ le && Y && (_ = 0, y.length = 0, A.length = 0, c = 0, C?.onNewStream?.());
644
+ const Ve = pe.currentTime;
645
+ let xe = m;
646
+ xe < Ve && (xe = Ve + 0.1);
647
+ const We = le && Y ? 0 : u > 0 ? Math.max(0, (xe - u) * 1e3) : 0;
648
+ se(me, {
649
+ startTime: xe,
650
+ isFirstAudio: le,
651
+ isFirstOfStream: le && Y
652
+ }), z(
653
+ y,
654
+ A,
655
+ x,
656
+ P,
657
+ We,
658
+ $,
588
659
  h
589
- ), _ = Math.max(_, xe(w));
660
+ ), _ = Math.max(_, Fe(y));
590
661
  } catch (ee) {
591
662
  console.error("[avatarChunkPlayback] audio chunk failed:", ee);
592
663
  } finally {
593
- if (i = !1, M) return;
664
+ if (i = !1, I) return;
594
665
  const ee = n.shift();
595
- ee ? await se(
596
- w,
597
- R,
666
+ ee ? await de(
667
+ y,
668
+ A,
598
669
  ee.audio,
599
670
  ee.visemes,
600
671
  ee.words,
@@ -602,63 +673,63 @@ function qn(e, t) {
602
673
  {
603
674
  sampleRate: ee.sampleRate,
604
675
  newStream: ee.newStream,
605
- onNewStream: A?.onNewStream
676
+ onNewStream: C?.onNewStream
606
677
  }
607
678
  ) : T();
608
679
  }
609
680
  };
610
681
  return {
611
- setStreamTtsEngineId: (w) => {
612
- h = w;
682
+ setStreamTtsEngineId: (y) => {
683
+ h = y;
613
684
  },
614
- setPlaybackSpeed: (w) => {
615
- b = Math.max(0.1, Math.min(1, w)), P();
685
+ setPlaybackSpeed: (y) => {
686
+ b = Math.max(0.1, Math.min(1, y)), M();
616
687
  },
617
688
  getPlaybackSpeed: () => b,
618
- setLipsyncWarmupHoldEnabled: (w) => {
619
- p = w, w || z();
689
+ setLipsyncWarmupHoldEnabled: (y) => {
690
+ p = y, y || j();
620
691
  },
621
692
  isWarmingUp: () => v,
622
- releaseWarmupHold: he,
623
- resetForNewStream: (w) => {
624
- M = !1, f = !1, h = w, _ = 0;
693
+ releaseWarmupHold: ne,
694
+ resetForNewStream: (y) => {
695
+ I = !1, f = !1, h = y, _ = 0;
625
696
  },
626
697
  /** Next sentence in the same bot turn — keep viseme tail, allow more chunks. */
627
698
  resumeBotStream: () => {
628
- M = !1, f = !1;
699
+ I = !1, f = !1;
629
700
  },
630
701
  /** After interrupt — allow the next utterance to queue/play again. */
631
702
  clearAborted: () => {
632
- M = !1;
703
+ I = !1;
633
704
  },
634
705
  /** Call from a user gesture before bot audio (connect / unlock). */
635
706
  primeAudioContext: async () => {
636
- const w = window.AudioContext || window.webkitAudioContext;
637
- s || (s = new w()), s.state === "suspended" && await s.resume(), s && !E && (E = s.createMediaStreamDestination());
707
+ const y = window.AudioContext || window.webkitAudioContext;
708
+ s || (s = new y()), s.state === "suspended" && await s.resume(), s && !E && (E = s.createMediaStreamDestination());
638
709
  },
639
710
  stop: () => {
640
- M = !0, x(), S(), n.length = 0, i = !1, a = !1, o = !1, f = !1, c = 0, m = 0, u = 0, d = 0, _ = 0, z();
711
+ I = !0, R(), S(), n.length = 0, i = !1, a = !1, o = !1, f = !1, c = 0, m = 0, u = 0, d = 0, _ = 0, j();
641
712
  },
642
- markStreamComplete: (w) => {
643
- if (_ = Math.max(_, xe(w)), f = !0, !o && w.length > 0) {
644
- j(w);
713
+ markStreamComplete: (y) => {
714
+ if (_ = Math.max(_, Fe(y)), f = !0, !o && y.length > 0) {
715
+ Z(y);
645
716
  return;
646
717
  }
647
718
  T();
648
719
  },
649
- playAudioChunk: se,
650
- queueTimingsOnly: (w, R, q, L, D) => {
651
- if (M) return;
652
- let N = c;
653
- D && w.length > 0 && (N = xe(w)), Z(
654
- w,
655
- R,
720
+ playAudioChunk: de,
721
+ queueTimingsOnly: (y, A, q, x, P) => {
722
+ if (I) return;
723
+ let $ = c;
724
+ P && y.length > 0 && ($ = Tn(y)), z(
725
+ y,
726
+ A,
656
727
  q,
657
- L,
658
- N,
659
- D,
728
+ x,
729
+ $,
730
+ P,
660
731
  h
661
- ), _ = Math.max(_, xe(w));
732
+ ), _ = Math.max(_, Fe(y));
662
733
  },
663
734
  getPlaybackElapsedMs: () => o && s && u > 0 ? Math.max(
664
735
  0,
@@ -669,21 +740,21 @@ function qn(e, t) {
669
740
  getPlaybackStartPerf: () => d,
670
741
  /** User spoke over the bot — drop in-flight chunks until the next utterance. */
671
742
  bargeIn: () => {
672
- M = !0, f = !1, x(), S(), n.length = 0, i = !1, a = !1, o = !1, c = 0, m = 0, u = 0, d = 0, _ = 0, z();
743
+ I = !0, f = !1, R(), S(), n.length = 0, i = !1, a = !1, o = !1, c = 0, m = 0, u = 0, d = 0, _ = 0, j();
673
744
  },
674
745
  interrupt: () => {
675
- M = !0, f = !1, x(), S(), n.length = 0, i = !1, a = !1, o = !1, c = 0, m = 0, u = 0, d = 0, _ = 0, z();
746
+ I = !0, f = !1, R(), S(), n.length = 0, i = !1, a = !1, o = !1, c = 0, m = 0, u = 0, d = 0, _ = 0, j();
676
747
  }
677
748
  };
678
749
  }
679
- function It(e, t) {
750
+ function Mt(e, t) {
680
751
  let n = null;
681
752
  for (const r of e)
682
753
  t < r.vtime || t >= r.vtime + r.vduration || (!n || r.vtime >= n.vtime) && (n = r);
683
754
  return n ? n.viseme : "sil";
684
755
  }
685
- function Kn(e, t, n = 120) {
686
- const r = It(e, t);
756
+ function Qn(e, t, n = 120) {
757
+ const r = Mt(e, t);
687
758
  if (r !== "sil") return r;
688
759
  let s = null;
689
760
  for (const i of e) {
@@ -693,7 +764,7 @@ function Kn(e, t, n = 120) {
693
764
  }
694
765
  return s?.viseme ?? "sil";
695
766
  }
696
- function ds(e, t, n = 400) {
767
+ function Es(e, t, n = 400) {
697
768
  const r = t - n;
698
769
  let s = 0;
699
770
  for (const i of e)
@@ -701,13 +772,13 @@ function ds(e, t, n = 400) {
701
772
  else break;
702
773
  s > 0 && e.splice(0, s);
703
774
  }
704
- function Gi(e, t) {
775
+ function ea(e, t) {
705
776
  for (const n of e)
706
777
  if (t >= n.wtime && t < n.wtime + n.wduration)
707
778
  return n;
708
779
  return null;
709
780
  }
710
- function ms(e, t) {
781
+ function Ts(e, t) {
711
782
  const n = e.trim();
712
783
  if (!n) return null;
713
784
  try {
@@ -717,64 +788,64 @@ function ms(e, t) {
717
788
  return null;
718
789
  }
719
790
  }
720
- const Xt = 0.5, Qt = 1.5, Gn = "Olivia", jn = "f786b574-daa5-4673-aa0c-cbe3e8534c02", Mt = 0.85;
721
- function vn(e, t = dt, n = {}) {
791
+ const Qt = 0.5, Zt = 1.5, Zn = "Olivia", er = "f786b574-daa5-4673-aa0c-cbe3e8534c02", Pt = 0.85;
792
+ function An(e, t = lt, n = {}) {
722
793
  const r = `${t.replace(/\/$/, "")}/avatar_ttsWithPoses`, s = [], i = [], a = [], o = [];
723
- let f = "", d = "neutral", u = "", c = Ge;
794
+ let f = "", d = "neutral", u = "", c = je;
724
795
  const m = /* @__PURE__ */ new Set();
725
- let g = 0, b = null;
726
- const h = () => m.forEach((x) => x());
727
- let M = () => !1;
728
- const E = qn(
796
+ let w = 0, b = null;
797
+ const h = () => m.forEach((R) => R());
798
+ let I = () => !1;
799
+ const E = Xn(
729
800
  {
730
801
  onSpeakingStart: () => e("speaking"),
731
802
  onSpeakingDone: () => e("done"),
732
803
  onNotify: h,
733
- shouldHoldForWarmup: () => M()
804
+ shouldHoldForWarmup: () => I()
734
805
  },
735
- Ge
736
- ), _ = () => E.getPlaybackElapsedMs(), p = (x, S = f) => {
806
+ je
807
+ ), _ = () => E.getPlaybackElapsedMs(), p = (R, S = f) => {
737
808
  if (!n.onLipsyncDebug) return;
738
- const P = _(), T = s;
809
+ const M = _(), T = s;
739
810
  n.onLipsyncDebug({
740
- event: x,
811
+ event: R,
741
812
  spokenText: S,
742
813
  mood: d,
743
814
  playbackAnchorPerf: E.getPlaybackStartPerf(),
744
- playbackElapsedMs: P,
815
+ playbackElapsedMs: M,
745
816
  queueLength: T.length,
746
- activeViseme: It(T, P),
747
- segments: o.map((j) => ({
748
- ...j,
749
- visemes: [...j.visemes]
817
+ activeViseme: Mt(T, M),
818
+ segments: o.map((Z) => ({
819
+ ...Z,
820
+ visemes: [...Z.visemes]
750
821
  }))
751
822
  });
752
- }, v = (x, S) => {
753
- n.onLipsyncDebug && (b = { event: x, spokenText: S }, g === 0 && (g = requestAnimationFrame(() => {
754
- g = 0;
755
- const P = b;
756
- b = null, P && p(P.event, P.spokenText);
823
+ }, v = (R, S) => {
824
+ n.onLipsyncDebug && (b = { event: R, spokenText: S }, w === 0 && (w = requestAnimationFrame(() => {
825
+ w = 0;
826
+ const M = b;
827
+ b = null, M && p(M.event, M.spokenText);
757
828
  })));
758
- }, O = (x, S, P, T, j) => {
829
+ }, N = (R, S, M, T, Z) => {
759
830
  if (!n.onLipsyncDebug) return;
760
- const I = s.slice(S), ne = P.filter(
761
- (Z) => String(Z.symbol ?? Z.viseme ?? "").trim()
831
+ const D = s.slice(S), X = M.filter(
832
+ (z) => String(z.symbol ?? z.viseme ?? "").trim()
762
833
  );
763
834
  o.push({
764
- spokenText: x,
765
- audioOffsetMs: I[0]?.vtime ?? 0,
835
+ spokenText: R,
836
+ audioOffsetMs: D[0]?.vtime ?? 0,
766
837
  isNewSegment: T,
767
- chunkDurationMs: j,
838
+ chunkDurationMs: Z,
768
839
  receivedAtMs: performance.now(),
769
- visemes: I.map((Z, X) => {
770
- const he = ne[X];
840
+ visemes: D.map((z, se) => {
841
+ const ne = X[se];
771
842
  return {
772
- rawSymbol: String(he?.symbol ?? he?.viseme ?? Z.viseme).trim(),
773
- viseme: Z.viseme,
774
- startSec: Number(he?.start ?? 0),
775
- durationSec: Number(he?.duration ?? 0),
776
- vtimeMs: Z.vtime,
777
- vdurationMs: Z.vduration
843
+ rawSymbol: String(ne?.symbol ?? ne?.viseme ?? z.viseme).trim(),
844
+ viseme: z.viseme,
845
+ startSec: Number(ne?.start ?? 0),
846
+ durationSec: Number(ne?.duration ?? 0),
847
+ vtimeMs: z.vtime,
848
+ vdurationMs: z.vduration
778
849
  };
779
850
  })
780
851
  });
@@ -782,118 +853,118 @@ function vn(e, t = dt, n = {}) {
782
853
  E.stop(), s.length = 0, i.length = 0, a.length = 0, d = "neutral", o.length = 0, f = "", e("idle"), v("reset"), h();
783
854
  };
784
855
  return {
785
- setDeveloperToken: (x) => {
786
- u = x;
856
+ setDeveloperToken: (R) => {
857
+ u = R;
787
858
  },
788
- setTtsEngineId: (x) => {
789
- c = x;
859
+ setTtsEngineId: (R) => {
860
+ c = R;
790
861
  },
791
862
  getTtsEngineId: () => c,
792
- speak: async (x, S) => {
793
- F(), e("loading"), f = x, o.length = 0;
794
- const P = u.trim();
795
- if (!P)
863
+ speak: async (R, S) => {
864
+ F(), e("loading"), f = R, o.length = 0;
865
+ const M = u.trim();
866
+ if (!M)
796
867
  throw new Error("Developer token required for avatar_ttsWithPoses");
797
- const T = S?.ttsEngineId ?? c ?? Ge;
868
+ const T = S?.ttsEngineId ?? c ?? je;
798
869
  E.resetForNewStream(T), E.setStreamTtsEngineId(T);
799
- const j = S?.voiceId ?? Gn, I = S?.speakingRate ?? Mt, ne = Math.max(
800
- Xt,
801
- Math.min(Qt, I)
802
- ), Z = {
803
- user_query: x,
870
+ const Z = S?.voiceId ?? Zn, D = S?.speakingRate ?? Pt, X = Math.max(
871
+ Qt,
872
+ Math.min(Zt, D)
873
+ ), z = {
874
+ user_query: R,
804
875
  ttsEngineId: T,
805
- speakingRate: ne,
876
+ speakingRate: X,
806
877
  tenant: "aiTwin"
807
878
  };
808
- Vn.has(T) && (Z.voice_id = j);
879
+ $n.has(T) && (z.voice_id = Z);
809
880
  try {
810
- const X = await fetch(r, {
881
+ const se = await fetch(r, {
811
882
  method: "POST",
812
883
  headers: {
813
884
  "Content-Type": "application/json",
814
- Authorization: `Bearer ${P}`
885
+ Authorization: `Bearer ${M}`
815
886
  },
816
- body: JSON.stringify(Z)
887
+ body: JSON.stringify(z)
817
888
  });
818
- if (!X.ok)
819
- throw new Error(`avatar_ttsWithPoses failed (${X.status})`);
820
- const he = X.body;
821
- if (!he) throw new Error("No response body");
822
- const se = he.getReader(), ce = new TextDecoder();
823
- let w = "";
824
- const R = async (q) => {
825
- const L = Hn(q);
826
- if (L) {
827
- if (L.event === "audio") {
828
- const D = L.data.chunk, N = L.data.visemes ?? [], A = L.data.words ?? [], U = L.data.is_new_segment ?? !1, re = L.data.sample_rate, ee = s.length;
829
- D ? (await E.playAudioChunk(
889
+ if (!se.ok)
890
+ throw new Error(`avatar_ttsWithPoses failed (${se.status})`);
891
+ const ne = se.body;
892
+ if (!ne) throw new Error("No response body");
893
+ const de = ne.getReader(), ue = new TextDecoder();
894
+ let y = "";
895
+ const A = async (q) => {
896
+ const x = Kn(q);
897
+ if (x) {
898
+ if (x.event === "audio") {
899
+ const P = x.data.chunk, $ = x.data.visemes ?? [], C = x.data.words ?? [], B = x.data.is_new_segment ?? !1, Y = x.data.sample_rate, ee = s.length;
900
+ P ? (await E.playAudioChunk(
830
901
  s,
831
902
  i,
832
- D,
833
- N,
834
- A,
835
- U,
836
- { sampleRate: re }
837
- ), O(
838
- x,
903
+ P,
904
+ $,
905
+ C,
906
+ B,
907
+ { sampleRate: Y }
908
+ ), N(
909
+ R,
839
910
  ee,
840
- N,
841
- U,
842
- ms(D, re)
843
- ), v("audio_chunk", x)) : (N.length > 0 || A.length > 0) && (E.queueTimingsOnly(
911
+ $,
912
+ B,
913
+ Ts(P, Y)
914
+ ), v("audio_chunk", R)) : ($.length > 0 || C.length > 0) && (E.queueTimingsOnly(
844
915
  s,
845
916
  i,
846
- N,
847
- A,
848
- U
849
- ), O(
850
- x,
917
+ $,
918
+ C,
919
+ B
920
+ ), N(
921
+ R,
851
922
  ee,
852
- N,
853
- U,
923
+ $,
924
+ B,
854
925
  null
855
- ), v("audio_chunk", x));
856
- } else if (L.event === "metadata") {
857
- const D = L.data.mood;
858
- typeof D == "string" && D.trim() && (d = D.trim());
859
- const N = L.data.sentence_emotions;
860
- if (Array.isArray(N)) {
926
+ ), v("audio_chunk", R));
927
+ } else if (x.event === "metadata") {
928
+ const P = x.data.mood;
929
+ typeof P == "string" && P.trim() && (d = P.trim());
930
+ const $ = x.data.sentence_emotions;
931
+ if (Array.isArray($)) {
861
932
  a.length = 0;
862
- for (const A of N) {
863
- if (!A || typeof A != "object") continue;
864
- const U = A;
933
+ for (const C of $) {
934
+ if (!C || typeof C != "object") continue;
935
+ const B = C;
865
936
  a.push({
866
- sentence_index: Number(U.sentence_index ?? 0),
867
- text: String(U.text ?? ""),
868
- sentiment: typeof U.sentiment == "string" ? U.sentiment : void 0,
869
- emotion: String(U.emotion ?? d),
870
- start_word: Number(U.start_word ?? 0),
871
- end_word: Number(U.end_word ?? 0)
937
+ sentence_index: Number(B.sentence_index ?? 0),
938
+ text: String(B.text ?? ""),
939
+ sentiment: typeof B.sentiment == "string" ? B.sentiment : void 0,
940
+ emotion: String(B.emotion ?? d),
941
+ start_word: Number(B.start_word ?? 0),
942
+ end_word: Number(B.end_word ?? 0)
872
943
  });
873
944
  }
874
945
  }
875
- h(), v("metadata", x);
876
- } else if (L.event === "error")
946
+ h(), v("metadata", R);
947
+ } else if (x.event === "error")
877
948
  throw new Error(
878
- String(L.data.message ?? "avatar_ttsWithPoses stream error")
949
+ String(x.data.message ?? "avatar_ttsWithPoses stream error")
879
950
  );
880
951
  }
881
952
  };
882
953
  for (; ; ) {
883
- const { done: q, value: L } = await se.read();
884
- L && (w += ce.decode(L, { stream: !0 }));
885
- const D = w.split(`
954
+ const { done: q, value: x } = await de.read();
955
+ x && (y += ue.decode(x, { stream: !0 }));
956
+ const P = y.split(`
886
957
 
887
958
  `);
888
- w = D.pop() ?? "";
889
- for (const N of D) await R(N);
959
+ y = P.pop() ?? "";
960
+ for (const $ of P) await A($);
890
961
  if (q) {
891
- w.trim() && await R(w.trim()), E.markStreamComplete(s), v("bot_stopped", x);
962
+ y.trim() && await A(y.trim()), E.markStreamComplete(s), v("bot_stopped", R);
892
963
  break;
893
964
  }
894
965
  }
895
- } catch (X) {
896
- throw console.error("[avatarTtsLipsync]", X), F(), e("error"), X;
966
+ } catch (se) {
967
+ throw console.error("[avatarTtsLipsync]", se), F(), e("error"), se;
897
968
  }
898
969
  },
899
970
  stop: F,
@@ -905,19 +976,19 @@ function vn(e, t = dt, n = {}) {
905
976
  isSpeaking: () => E.isSpeaking(),
906
977
  isWarmingUp: () => E.isWarmingUp(),
907
978
  releaseWarmupHold: () => E.releaseWarmupHold(),
908
- setLipsyncWarmupHold: (x, S) => {
909
- S && (M = S), E.setLipsyncWarmupHoldEnabled(x);
979
+ setLipsyncWarmupHold: (R, S) => {
980
+ S && (I = S), E.setLipsyncWarmupHoldEnabled(R);
910
981
  },
911
982
  getVoiceStream: () => E.getVoiceStream(),
912
- setPlaybackSpeed: (x) => {
913
- E.setPlaybackSpeed(x);
983
+ setPlaybackSpeed: (R) => {
984
+ E.setPlaybackSpeed(R);
914
985
  },
915
986
  getPlaybackSpeed: () => E.getPlaybackSpeed(),
916
- subscribe: (x) => (m.add(x), () => m.delete(x))
987
+ subscribe: (R) => (m.add(R), () => m.delete(R))
917
988
  };
918
989
  }
919
- const zn = 16e3, Yn = 16e3;
920
- class hs extends yr {
990
+ const tr = 16e3, nr = 16e3;
991
+ class As extends Er {
921
992
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
922
993
  async deserialize(t) {
923
994
  try {
@@ -929,7 +1000,7 @@ class hs extends yr {
929
1000
  }
930
1001
  }
931
1002
  }
932
- class ps extends gr {
1003
+ class Is extends vr {
933
1004
  constructor() {
934
1005
  super(
935
1006
  !0,
@@ -937,39 +1008,39 @@ class ps extends gr {
937
1008
  void 0,
938
1009
  void 0,
939
1010
  512,
940
- zn,
941
- Yn
1011
+ tr,
1012
+ nr
942
1013
  );
943
1014
  }
944
1015
  /** Pipecat protobuf bot audio is unused — playback comes from avatar_audio_chunk. */
945
1016
  bufferBotAudio(t, n) {
946
1017
  }
947
1018
  }
948
- const ws = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
949
- function gs(e) {
950
- return ws.test(e.trim());
1019
+ const Ms = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
1020
+ function Ps(e) {
1021
+ return Ms.test(e.trim());
951
1022
  }
952
- function ys(e) {
1023
+ function Rs(e) {
953
1024
  const t = e.replace(/\/$/, "");
954
1025
  return t.startsWith("https://") ? `wss://${t.slice(8)}` : t.startsWith("http://") ? `ws://${t.slice(7)}` : t;
955
1026
  }
956
- function _s(e) {
1027
+ function Ds(e) {
957
1028
  if (e.wsUrl?.trim()) return e.wsUrl.trim();
958
1029
  const t = e.authToken?.trim();
959
1030
  if (!t)
960
1031
  throw new Error(
961
1032
  "Voice connect requires authToken (same JWT as SSE Authorization Bearer)."
962
1033
  );
963
- const n = e.apiBase ?? dt, r = new URL(`${ys(n)}/ws/voice`);
1034
+ const n = e.apiBase ?? lt, r = new URL(`${Rs(n)}/ws/voice`);
964
1035
  r.searchParams.set("authToken", t), r.searchParams.set("tenant", "aiTwin");
965
1036
  const s = e.voiceId?.trim();
966
- s && gs(s) && r.searchParams.set("voiceId", s);
1037
+ s && Ps(s) && r.searchParams.set("voiceId", s);
967
1038
  const i = e.personaId?.trim();
968
1039
  i && r.searchParams.set("personaId", i);
969
- const a = e.speakingRate ?? Mt;
1040
+ const a = e.speakingRate ?? Pt;
970
1041
  return r.searchParams.set(
971
1042
  "speaking_rate",
972
- String(Math.max(Xt, Math.min(Qt, a)))
1043
+ String(Math.max(Qt, Math.min(Zt, a)))
973
1044
  ), e.ttsStream === !1 ? r.searchParams.set("tts_stream", "false") : r.searchParams.set("tts_stream", "true"), r.toString();
974
1045
  }
975
1046
  function Lt(e, t) {
@@ -986,7 +1057,7 @@ function Lt(e, t) {
986
1057
  });
987
1058
  return n;
988
1059
  }
989
- function Ss(e) {
1060
+ function xs(e) {
990
1061
  if (!e || typeof e != "object") return null;
991
1062
  const t = e, r = (t.data && typeof t.data == "object" && !Array.isArray(t.data) ? t.data : null) || t;
992
1063
  return {
@@ -997,63 +1068,63 @@ function Ss(e) {
997
1068
  function Ot(e) {
998
1069
  return e.is_new_segment === !0 || Number(e.audio_offset_ms ?? -1) === 0 ? !0 : Number(e.chunk_index ?? -1) === 0;
999
1070
  }
1000
- function ks(e) {
1071
+ function Bs(e) {
1001
1072
  return e?.transport?._mediaManager?._wavStreamPlayer ?? null;
1002
1073
  }
1003
- function bs(e, t = {}) {
1074
+ function Cs(e, t = {}) {
1004
1075
  const n = [], r = [], s = [], i = [];
1005
- let a = "", o = "neutral", f = Je, d = !1, u = null, c = null, m = { ...t }, g = 0, b = null;
1006
- const h = /* @__PURE__ */ new Set(), M = [];
1076
+ let a = "", o = "neutral", f = Xe, d = !1, u = null, c = null, m = { ...t }, w = 0, b = null;
1077
+ const h = /* @__PURE__ */ new Set(), I = [];
1007
1078
  let E = !1, _ = null, p = !0, v = "default";
1008
- const O = /* @__PURE__ */ new Set();
1009
- let F = null, z = 0, x = !1, S = null;
1010
- const P = () => {
1079
+ const N = /* @__PURE__ */ new Set();
1080
+ let F = null, j = 0, R = !1, S = null;
1081
+ const M = () => {
1011
1082
  S != null && (clearTimeout(S), S = null);
1012
1083
  }, T = () => h.forEach((l) => l());
1013
- let j = () => !1;
1014
- const I = qn(
1084
+ let Z = () => !1;
1085
+ const D = Xn(
1015
1086
  {
1016
1087
  onSpeakingStart: () => e("speaking"),
1017
1088
  onSpeakingDone: () => e("done"),
1018
1089
  onNotify: T,
1019
- shouldHoldForWarmup: () => j()
1090
+ shouldHoldForWarmup: () => Z()
1020
1091
  },
1021
1092
  f
1022
- ), ne = () => {
1093
+ ), X = () => {
1023
1094
  if (_) return _;
1024
1095
  const l = new MessageChannel();
1025
1096
  return l.port1.onmessage = () => {
1026
- const k = M.shift();
1027
- k !== void 0 && ae(k), M.length > 0 ? l.port2.postMessage(null) : E = !1;
1097
+ const k = I.shift();
1098
+ k !== void 0 && le(k), I.length > 0 ? l.port2.postMessage(null) : E = !1;
1028
1099
  }, _ = l, l;
1029
- }, Z = (l) => {
1030
- M.push(l), !E && (E = !0, ne().port2.postMessage(null));
1031
- }, X = () => I.getPlaybackElapsedMs(), he = (l, k = a) => {
1100
+ }, z = (l) => {
1101
+ I.push(l), !E && (E = !0, X().port2.postMessage(null));
1102
+ }, se = () => D.getPlaybackElapsedMs(), ne = (l, k = a) => {
1032
1103
  if (!m.onLipsyncDebug) return;
1033
- const C = X(), Y = n;
1104
+ const V = se(), K = n;
1034
1105
  m.onLipsyncDebug({
1035
1106
  event: l,
1036
1107
  spokenText: k,
1037
1108
  mood: o,
1038
- playbackAnchorPerf: I.getPlaybackStartPerf(),
1039
- playbackElapsedMs: C,
1040
- queueLength: Y.length,
1041
- activeViseme: It(Y, C),
1042
- segments: i.map((oe) => ({
1043
- ...oe,
1044
- visemes: [...oe.visemes]
1109
+ playbackAnchorPerf: D.getPlaybackStartPerf(),
1110
+ playbackElapsedMs: V,
1111
+ queueLength: K.length,
1112
+ activeViseme: Mt(K, V),
1113
+ segments: i.map((ie) => ({
1114
+ ...ie,
1115
+ visemes: [...ie.visemes]
1045
1116
  }))
1046
1117
  });
1047
- }, se = (l, k) => {
1048
- m.onLipsyncDebug && (b = { event: l, spokenText: k }, g === 0 && (g = requestAnimationFrame(() => {
1049
- g = 0;
1050
- const C = b;
1051
- b = null, C && he(C.event, C.spokenText);
1118
+ }, de = (l, k) => {
1119
+ m.onLipsyncDebug && (b = { event: l, spokenText: k }, w === 0 && (w = requestAnimationFrame(() => {
1120
+ w = 0;
1121
+ const V = b;
1122
+ b = null, V && ne(V.event, V.spokenText);
1052
1123
  })));
1053
- }, ce = () => {
1054
- p = !0, O.clear(), I.clearAborted();
1055
- }, w = () => {
1056
- const l = ks(u);
1124
+ }, ue = () => {
1125
+ p = !0, N.clear(), D.clearAborted();
1126
+ }, y = () => {
1127
+ const l = Bs(u);
1057
1128
  if (l) {
1058
1129
  v = `bot-${Date.now()}`, l.interruptedTrackIds = { [v]: !0 };
1059
1130
  try {
@@ -1066,225 +1137,225 @@ function bs(e, t = {}) {
1066
1137
  k?.interrupt?.();
1067
1138
  } catch {
1068
1139
  }
1069
- }, R = (l) => {
1140
+ }, A = (l) => {
1070
1141
  const k = l.context_id?.trim();
1071
- return k && O.has(k) ? !0 : p ? !1 : !(l.type === "avatar_metadata" || Ot(l));
1142
+ return k && N.has(k) ? !0 : p ? !1 : !(l.type === "avatar_metadata" || Ot(l));
1072
1143
  }, q = (l) => {
1073
1144
  p = !1;
1074
1145
  const k = l.context_id?.trim();
1075
- k && O.add(k), P(), x = !1, M.length = 0, E = !1, F?.abort(), F = null, z += 1, w(), I.bargeIn(), L(), e("idle"), se("interrupt"), T();
1076
- }, L = () => {
1146
+ k && N.add(k), M(), R = !1, I.length = 0, E = !1, F?.abort(), F = null, j += 1, y(), D.bargeIn(), x(), e("idle"), de("interrupt"), T();
1147
+ }, x = () => {
1077
1148
  n.length = 0, r.length = 0, i.length = 0, a = "", o = "neutral", s.length = 0;
1078
- }, D = () => {
1079
- I.stop(), L(), se("reset"), T();
1080
- }, N = () => {
1081
- D(), e("idle"), T();
1082
- }, A = (l, k) => {
1149
+ }, P = () => {
1150
+ D.stop(), x(), de("reset"), T();
1151
+ }, $ = () => {
1152
+ P(), e("idle"), T();
1153
+ }, C = (l, k) => {
1083
1154
  typeof l.mood == "string" && l.mood.trim() && (o = l.mood.trim()), Array.isArray(l.sentence_emotions) && l.sentence_emotions.length > 0 && (k && (s.length = 0), s.push(
1084
1155
  ...Lt(l.sentence_emotions, o)
1085
1156
  ));
1086
- }, U = (l) => {
1087
- if (R({ ...l, type: "avatar_metadata" }))
1157
+ }, B = (l) => {
1158
+ if (A({ ...l, type: "avatar_metadata" }))
1088
1159
  return;
1089
- ce(), x = !0;
1160
+ ue(), R = !0;
1090
1161
  const k = String(l.spoken_text ?? l.text ?? "").trim();
1091
- k && (a = k), I.resetForNewStream(f), typeof l.mood == "string" && l.mood.trim() && (o = l.mood.trim()), s.length = 0, s.push(
1162
+ k && (a = k), D.resetForNewStream(f), typeof l.mood == "string" && l.mood.trim() && (o = l.mood.trim()), s.length = 0, s.push(
1092
1163
  ...Lt(l.sentence_emotions, o)
1093
- ), Array.isArray(l.gestures) && l.gestures, se("metadata", k), T();
1094
- }, re = (l, k, C, Y) => {
1164
+ ), Array.isArray(l.gestures) && l.gestures, de("metadata", k), T();
1165
+ }, Y = (l, k, V, K) => {
1095
1166
  if (!m.onLipsyncDebug) return;
1096
- const oe = n.slice(C), ye = Y.filter(
1097
- (le) => String(le.symbol ?? le.viseme ?? "").trim()
1167
+ const ie = n.slice(V), Pe = K.filter(
1168
+ (ae) => String(ae.symbol ?? ae.viseme ?? "").trim()
1098
1169
  );
1099
1170
  i.push({
1100
1171
  spokenText: k,
1101
- audioOffsetMs: oe[0]?.vtime ?? 0,
1172
+ audioOffsetMs: ie[0]?.vtime ?? 0,
1102
1173
  isNewSegment: !!l.is_new_segment,
1103
1174
  chunkDurationMs: l.chunk_duration_ms != null ? Number(l.chunk_duration_ms) : null,
1104
1175
  receivedAtMs: performance.now(),
1105
- visemes: oe.map((le, _e) => {
1106
- const Se = ye[_e];
1176
+ visemes: ie.map((ae, Re) => {
1177
+ const ge = Pe[Re];
1107
1178
  return {
1108
- rawSymbol: String(Se?.symbol ?? Se?.viseme ?? le.viseme).trim(),
1109
- viseme: le.viseme,
1110
- startSec: Number(Se?.start ?? 0),
1111
- durationSec: Number(Se?.duration ?? 0),
1112
- vtimeMs: le.vtime,
1113
- vdurationMs: le.vduration
1179
+ rawSymbol: String(ge?.symbol ?? ge?.viseme ?? ae.viseme).trim(),
1180
+ viseme: ae.viseme,
1181
+ startSec: Number(ge?.start ?? 0),
1182
+ durationSec: Number(ge?.duration ?? 0),
1183
+ vtimeMs: ae.vtime,
1184
+ vdurationMs: ae.vduration
1114
1185
  };
1115
1186
  })
1116
1187
  });
1117
1188
  }, ee = (l) => {
1118
- if (R(l)) return;
1119
- P(), Ot(l) && ce();
1120
- const k = String(l.spoken_text ?? l.text ?? "").trim(), C = Ot(l), Y = l.visemes ?? [], oe = l.words ?? [], ye = String(l.chunk ?? "").trim(), le = x;
1121
- le && (x = !1), k && (a = k), A(l, C), le ? I.resetForNewStream(f) : C && I.resumeBotStream();
1122
- const _e = n.length;
1123
- I.playAudioChunk(
1189
+ if (A(l)) return;
1190
+ M(), Ot(l) && ue();
1191
+ const k = String(l.spoken_text ?? l.text ?? "").trim(), V = Ot(l), K = l.visemes ?? [], ie = l.words ?? [], Pe = String(l.chunk ?? "").trim(), ae = R;
1192
+ ae && (R = !1), k && (a = k), C(l, V), ae ? D.resetForNewStream(f) : V && D.resumeBotStream();
1193
+ const Re = n.length;
1194
+ D.playAudioChunk(
1124
1195
  n,
1125
1196
  r,
1126
- ye,
1127
- Y,
1128
- oe,
1129
- C,
1197
+ Pe,
1198
+ K,
1199
+ ie,
1200
+ V,
1130
1201
  {
1131
1202
  sampleRate: l.sample_rate,
1132
- newStream: le,
1203
+ newStream: ae,
1133
1204
  onNewStream: () => {
1134
1205
  i.length = 0;
1135
1206
  }
1136
1207
  }
1137
1208
  ).then(() => {
1138
- re(l, k, _e, Y), se("audio_chunk", k);
1209
+ Y(l, k, Re, K), de("audio_chunk", k);
1139
1210
  });
1140
- }, de = (l) => {
1141
- if (R(l)) return;
1142
- (l.is_new_segment || Number(l.audio_offset_ms ?? -1) === 0) && ce(), console.warn(
1211
+ }, pe = (l) => {
1212
+ if (A(l)) return;
1213
+ (l.is_new_segment || Number(l.audio_offset_ms ?? -1) === 0) && ue(), console.warn(
1143
1214
  "[realtimeVoice] avatar_lipsync is deprecated — expect avatar_audio_chunk"
1144
1215
  );
1145
1216
  const k = String(l.spoken_text ?? l.text ?? "").trim();
1146
- k && (a = k), A(l, !!l.is_new_segment);
1147
- const C = n.length;
1148
- I.queueTimingsOnly(
1217
+ k && (a = k), C(l, !!l.is_new_segment);
1218
+ const V = n.length;
1219
+ D.queueTimingsOnly(
1149
1220
  n,
1150
1221
  r,
1151
1222
  l.visemes ?? [],
1152
1223
  l.words ?? [],
1153
1224
  !!l.is_new_segment
1154
- ), re(
1225
+ ), Y(
1155
1226
  l,
1156
1227
  k,
1157
- C,
1228
+ V,
1158
1229
  l.visemes ?? []
1159
- ), se("lipsync", k), T();
1160
- }, ue = async (l) => {
1230
+ ), de("lipsync", k), T();
1231
+ }, me = async (l) => {
1161
1232
  const k = String(l.text ?? "").trim();
1162
1233
  if (!k) return;
1163
- const C = m.authToken?.trim();
1164
- if (!C) {
1234
+ const V = m.authToken?.trim();
1235
+ if (!V) {
1165
1236
  console.error("[realtimeVoice] avatar_speak requires authToken");
1166
1237
  return;
1167
1238
  }
1168
1239
  F?.abort();
1169
- const Y = new AbortController();
1170
- F = Y;
1171
- const oe = ++z;
1172
- ce(), I.resetForNewStream(f), a = k;
1173
- const le = `${(m.apiBase ?? dt).replace(/\/$/, "")}/avatar_ttsWithPoses`, _e = m.voiceId?.trim() || jn, Se = m.speakingRate ?? Mt, mt = Math.max(
1174
- Xt,
1175
- Math.min(Qt, Se)
1176
- ), ie = {
1240
+ const K = new AbortController();
1241
+ F = K;
1242
+ const ie = ++j;
1243
+ ue(), D.resetForNewStream(f), a = k;
1244
+ const ae = `${(m.apiBase ?? lt).replace(/\/$/, "")}/avatar_ttsWithPoses`, Re = m.voiceId?.trim() || er, ge = m.speakingRate ?? Pt, Ze = Math.max(
1245
+ Qt,
1246
+ Math.min(Zt, ge)
1247
+ ), et = {
1177
1248
  user_query: k,
1178
1249
  ttsEngineId: f,
1179
- speakingRate: mt,
1250
+ speakingRate: Ze,
1180
1251
  tenant: "aiTwin"
1181
1252
  };
1182
- Vn.has(f) && (ie.voice_id = _e);
1253
+ $n.has(f) && (et.voice_id = Re);
1183
1254
  try {
1184
- const De = await fetch(le, {
1255
+ const re = await fetch(ae, {
1185
1256
  method: "POST",
1186
1257
  headers: {
1187
1258
  "Content-Type": "application/json",
1188
- Authorization: `Bearer ${C}`
1259
+ Authorization: `Bearer ${V}`
1189
1260
  },
1190
- body: JSON.stringify(ie),
1191
- signal: Y.signal
1261
+ body: JSON.stringify(et),
1262
+ signal: K.signal
1192
1263
  });
1193
- if (!De.ok)
1194
- throw new Error(`avatar_ttsWithPoses failed (${De.status})`);
1195
- const et = De.body;
1196
- if (!et) throw new Error("No response body");
1197
- const ht = et.getReader(), Ve = new TextDecoder();
1198
- let Re = "";
1199
- const Te = async (We) => {
1200
- if (oe !== z || Y.signal.aborted || !p)
1264
+ if (!re.ok)
1265
+ throw new Error(`avatar_ttsWithPoses failed (${re.status})`);
1266
+ const tt = re.body;
1267
+ if (!tt) throw new Error("No response body");
1268
+ const dt = tt.getReader(), mt = new TextDecoder();
1269
+ let be = "";
1270
+ const nt = async (ve) => {
1271
+ if (ie !== j || K.signal.aborted || !p)
1201
1272
  return;
1202
- const Q = Hn(We);
1203
- if (Q) {
1204
- if (Q.event === "audio") {
1205
- const fe = Q.data.chunk, K = Q.data.visemes ?? [], J = Q.data.words ?? [], tt = Q.data.is_new_segment ?? !1, pe = Q.data.sample_rate;
1206
- fe ? await I.playAudioChunk(
1273
+ const oe = Kn(ve);
1274
+ if (oe) {
1275
+ if (oe.event === "audio") {
1276
+ const ce = oe.data.chunk, he = oe.data.visemes ?? [], J = oe.data.words ?? [], Q = oe.data.is_new_segment ?? !1, ht = oe.data.sample_rate;
1277
+ ce ? await D.playAudioChunk(
1207
1278
  n,
1208
1279
  r,
1209
- fe,
1210
- K,
1280
+ ce,
1281
+ he,
1211
1282
  J,
1212
- tt,
1213
- { sampleRate: pe }
1214
- ) : (K.length > 0 || J.length > 0) && I.queueTimingsOnly(
1283
+ Q,
1284
+ { sampleRate: ht }
1285
+ ) : (he.length > 0 || J.length > 0) && D.queueTimingsOnly(
1215
1286
  n,
1216
1287
  r,
1217
- K,
1288
+ he,
1218
1289
  J,
1219
- tt
1290
+ Q
1220
1291
  );
1221
- } else if (Q.event === "metadata") {
1222
- const fe = Q.data.mood;
1223
- typeof fe == "string" && fe.trim() && (o = fe.trim());
1224
- const K = Q.data.sentence_emotions;
1225
- Array.isArray(K) && (s.length = 0, s.push(
1226
- ...Lt(K, o)
1292
+ } else if (oe.event === "metadata") {
1293
+ const ce = oe.data.mood;
1294
+ typeof ce == "string" && ce.trim() && (o = ce.trim());
1295
+ const he = oe.data.sentence_emotions;
1296
+ Array.isArray(he) && (s.length = 0, s.push(
1297
+ ...Lt(he, o)
1227
1298
  )), T();
1228
- } else if (Q.event === "error")
1299
+ } else if (oe.event === "error")
1229
1300
  throw new Error(
1230
- String(Q.data.message ?? "avatar_ttsWithPoses stream error")
1301
+ String(oe.data.message ?? "avatar_ttsWithPoses stream error")
1231
1302
  );
1232
1303
  }
1233
1304
  };
1234
- for (; !(oe !== z || Y.signal.aborted || !p); ) {
1235
- const { done: We, value: Q } = await ht.read();
1236
- Q && (Re += Ve.decode(Q, { stream: !0 }));
1237
- const fe = Re.split(`
1305
+ for (; !(ie !== j || K.signal.aborted || !p); ) {
1306
+ const { done: ve, value: oe } = await dt.read();
1307
+ oe && (be += mt.decode(oe, { stream: !0 }));
1308
+ const ce = be.split(`
1238
1309
 
1239
1310
  `);
1240
- Re = fe.pop() ?? "";
1241
- for (const K of fe) await Te(K);
1242
- if (We) {
1243
- Re.trim() && await Te(Re.trim()), oe === z && !Y.signal.aborted && p && I.markStreamComplete(n);
1311
+ be = ce.pop() ?? "";
1312
+ for (const he of ce) await nt(he);
1313
+ if (ve) {
1314
+ be.trim() && await nt(be.trim()), ie === j && !K.signal.aborted && p && D.markStreamComplete(n);
1244
1315
  break;
1245
1316
  }
1246
1317
  }
1247
- } catch (De) {
1248
- if (Y.signal.aborted || !p) return;
1249
- console.error("[realtimeVoice] avatar_speak failed:", De), e("error"), T();
1318
+ } catch (re) {
1319
+ if (K.signal.aborted || !p) return;
1320
+ console.error("[realtimeVoice] avatar_speak failed:", re), e("error"), T();
1250
1321
  } finally {
1251
- F === Y && (F = null);
1322
+ F === K && (F = null);
1252
1323
  }
1253
- }, ae = (l) => {
1254
- const k = Ss(l);
1324
+ }, le = (l) => {
1325
+ const k = xs(l);
1255
1326
  if (!k) {
1256
1327
  console.warn("[realtimeVoice] server message with no payload", l);
1257
1328
  return;
1258
1329
  }
1259
- const C = String(k.type ?? "unknown");
1260
- switch (C !== "avatar_metadata" && C !== "avatar_audio_chunk" && C !== "avatar_lipsync" && C !== "avatar_interrupt" && C !== "error" && console.debug("[realtimeVoice] server message", C, k), C) {
1330
+ const V = String(k.type ?? "unknown");
1331
+ switch (V !== "avatar_metadata" && V !== "avatar_audio_chunk" && V !== "avatar_lipsync" && V !== "avatar_interrupt" && V !== "error" && console.debug("[realtimeVoice] server message", V, k), V) {
1261
1332
  case "avatar_metadata":
1262
- U(k);
1333
+ B(k);
1263
1334
  break;
1264
1335
  case "avatar_audio_chunk":
1265
1336
  ee(k);
1266
1337
  break;
1267
1338
  case "avatar_lipsync":
1268
- de(k);
1339
+ pe(k);
1269
1340
  break;
1270
1341
  case "avatar_interrupt":
1271
1342
  q(k);
1272
1343
  break;
1273
1344
  case "error": {
1274
- const Y = String(k.message ?? "Voice session error");
1275
- console.error("[realtimeVoice] server error:", Y), e("error"), T();
1345
+ const K = String(k.message ?? "Voice session error");
1346
+ console.error("[realtimeVoice] server error:", K), e("error"), T();
1276
1347
  break;
1277
1348
  }
1278
1349
  case "avatar_speak":
1279
- ue(k);
1350
+ me(k);
1280
1351
  break;
1281
1352
  }
1282
- }, we = () => u || (c = new ps(), u = new pr({
1283
- transport: new wr({
1353
+ }, fe = () => u || (c = new Is(), u = new kr({
1354
+ transport: new br({
1284
1355
  mediaManager: c,
1285
- serializer: new hs(),
1286
- recorderSampleRate: zn,
1287
- playerSampleRate: Yn
1356
+ serializer: new As(),
1357
+ recorderSampleRate: tr,
1358
+ playerSampleRate: nr
1288
1359
  }),
1289
1360
  enableCam: !1,
1290
1361
  enableMic: !0,
@@ -1293,7 +1364,7 @@ function bs(e, t = {}) {
1293
1364
  d = !0, e("idle"), T();
1294
1365
  },
1295
1366
  onDisconnected: () => {
1296
- d = !1, N();
1367
+ d = !1, $();
1297
1368
  },
1298
1369
  onError: (l) => {
1299
1370
  const k = typeof l == "object" && l !== null && "data" in l ? String(l.data ?? l) : String(l);
@@ -1303,17 +1374,17 @@ function bs(e, t = {}) {
1303
1374
  l === "error" && (e("error"), T());
1304
1375
  },
1305
1376
  onServerMessage: (l) => {
1306
- Z(l);
1377
+ z(l);
1307
1378
  },
1308
1379
  onUserStartedSpeaking: () => {
1309
1380
  q({});
1310
1381
  },
1311
1382
  onBotStartedSpeaking: () => {
1312
- ce(), se("bot_started");
1383
+ ue(), de("bot_started");
1313
1384
  },
1314
1385
  onBotStoppedSpeaking: () => {
1315
- p && (P(), S = setTimeout(() => {
1316
- S = null, p && (I.markStreamComplete(n), se("bot_stopped"));
1386
+ p && (M(), S = setTimeout(() => {
1387
+ S = null, p && (D.markStreamComplete(n), de("bot_stopped"));
1317
1388
  }, 450));
1318
1389
  },
1319
1390
  onUserTranscript: (l) => {
@@ -1328,21 +1399,21 @@ function bs(e, t = {}) {
1328
1399
  setDeveloperToken: (l) => {
1329
1400
  },
1330
1401
  setTtsEngineId: (l) => {
1331
- f = l, I.setStreamTtsEngineId(l);
1402
+ f = l, D.setStreamTtsEngineId(l);
1332
1403
  },
1333
1404
  getTtsEngineId: () => f,
1334
1405
  speak: async (l, k) => {
1335
- const C = l.trim();
1336
- if (C) {
1406
+ const V = l.trim();
1407
+ if (V) {
1337
1408
  if (!u || !d)
1338
1409
  throw new Error(
1339
1410
  "Realtime voice is not connected. Call connect() first."
1340
1411
  );
1341
- D(), await u.sendText(C);
1412
+ P(), await u.sendText(V);
1342
1413
  }
1343
1414
  },
1344
1415
  stop: () => {
1345
- F?.abort(), F = null, z += 1, ce(), D(), e("idle"), T();
1416
+ F?.abort(), F = null, j += 1, ue(), P(), e("idle"), T();
1346
1417
  },
1347
1418
  connect: async (l) => {
1348
1419
  if (l && (m = { ...m, ...l }), !m.authToken?.trim())
@@ -1350,58 +1421,58 @@ function bs(e, t = {}) {
1350
1421
  "Voice connect requires authToken (same JWT as SSE Authorization Bearer)."
1351
1422
  );
1352
1423
  e("loading");
1353
- const k = we();
1424
+ const k = fe();
1354
1425
  await k.initDevices();
1355
- const C = _s(m);
1356
- console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):", C);
1426
+ const V = Ds(m);
1427
+ console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):", V);
1357
1428
  try {
1358
- await k.connect({ wsUrl: C });
1359
- } catch (Y) {
1429
+ await k.connect({ wsUrl: V });
1430
+ } catch (K) {
1360
1431
  d = !1, e("error"), T();
1361
- const oe = Y instanceof Error ? Y.message : "Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";
1362
- throw new Error(`${oe} — ${C}`);
1432
+ const ie = K instanceof Error ? K.message : "Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";
1433
+ throw new Error(`${ie} — ${V}`);
1363
1434
  }
1364
1435
  d = !0, e("idle"), T();
1365
1436
  },
1366
1437
  disconnect: async () => {
1367
- if (P(), x = !1, M.length = 0, E = !1, F?.abort(), F = null, z += 1, ce(), u)
1438
+ if (M(), R = !1, I.length = 0, E = !1, F?.abort(), F = null, j += 1, ue(), u)
1368
1439
  try {
1369
1440
  await u.disconnect();
1370
1441
  } catch (l) {
1371
1442
  console.error("[realtimeVoice] disconnect failed:", l);
1372
1443
  }
1373
- d = !1, N();
1444
+ d = !1, $();
1374
1445
  },
1375
1446
  isConnected: () => d,
1376
1447
  unlockAudio: async () => {
1377
- await I.primeAudioContext();
1448
+ await D.primeAudioContext();
1378
1449
  },
1379
1450
  getVisemeQueue: () => n,
1380
1451
  getWordQueue: () => r,
1381
1452
  getSentenceEmotions: () => s,
1382
1453
  getStreamMood: () => o,
1383
- getPlaybackElapsedMs: X,
1384
- isSpeaking: () => I.isSpeaking(),
1385
- isWarmingUp: () => I.isWarmingUp(),
1386
- releaseWarmupHold: () => I.releaseWarmupHold(),
1454
+ getPlaybackElapsedMs: se,
1455
+ isSpeaking: () => D.isSpeaking(),
1456
+ isWarmingUp: () => D.isWarmingUp(),
1457
+ releaseWarmupHold: () => D.releaseWarmupHold(),
1387
1458
  setLipsyncWarmupHold: (l, k) => {
1388
- k && (j = k), I.setLipsyncWarmupHoldEnabled(l);
1459
+ k && (Z = k), D.setLipsyncWarmupHoldEnabled(l);
1389
1460
  },
1390
- getVoiceStream: () => I.getVoiceStream(),
1461
+ getVoiceStream: () => D.getVoiceStream(),
1391
1462
  setPlaybackSpeed: (l) => {
1392
- I.setPlaybackSpeed(l);
1463
+ D.setPlaybackSpeed(l);
1393
1464
  },
1394
- getPlaybackSpeed: () => I.getPlaybackSpeed(),
1465
+ getPlaybackSpeed: () => D.getPlaybackSpeed(),
1395
1466
  subscribe: (l) => (h.add(l), () => h.delete(l))
1396
1467
  };
1397
1468
  }
1398
- function vs(e, t, n) {
1469
+ function Fs(e, t, n) {
1399
1470
  return {
1400
1471
  width: e.crop?.source_width ?? t,
1401
1472
  height: e.crop?.source_height ?? n
1402
1473
  };
1403
1474
  }
1404
- function Es(e, t) {
1475
+ function Ls(e, t) {
1405
1476
  const n = e.crop;
1406
1477
  return {
1407
1478
  x: t?.source_x ?? n?.x ?? 0,
@@ -1410,21 +1481,21 @@ function Es(e, t) {
1410
1481
  height: t?.source_h ?? n?.height ?? e.frame_height ?? 0
1411
1482
  };
1412
1483
  }
1413
- function Ts(e) {
1484
+ function Os(e) {
1414
1485
  return e ? (e.w ?? 0) > 0 && (e.h ?? 0) > 0 : !1;
1415
1486
  }
1416
- async function Pt(e, t) {
1487
+ async function Rt(e, t) {
1417
1488
  const n = await fetch(e);
1418
1489
  if (!n.ok) throw new Error(`Failed to load encrypted asset: ${e}`);
1419
1490
  const r = await n.arrayBuffer();
1420
- return Wr(r, t);
1491
+ return jr(r, t);
1421
1492
  }
1422
- async function As(e, t) {
1493
+ async function Ns(e, t) {
1423
1494
  if (!t) throw new Error("Missing asset key for encrypted JSON");
1424
- const n = await Pt(e, t), r = new TextDecoder().decode(n);
1495
+ const n = await Rt(e, t), r = new TextDecoder().decode(n);
1425
1496
  return JSON.parse(r);
1426
1497
  }
1427
- async function Is(e) {
1498
+ async function Us(e) {
1428
1499
  const t = new Blob([e], { type: "image/webp" }), n = URL.createObjectURL(t);
1429
1500
  return new Promise((r, s) => {
1430
1501
  const i = new Image();
@@ -1435,7 +1506,7 @@ async function Is(e) {
1435
1506
  }, i.src = n;
1436
1507
  });
1437
1508
  }
1438
- async function Ms(e) {
1509
+ async function Vs(e) {
1439
1510
  const t = new Blob([e], { type: "image/png" }), n = URL.createObjectURL(t);
1440
1511
  return new Promise((r, s) => {
1441
1512
  const i = new Image();
@@ -1446,28 +1517,28 @@ async function Ms(e) {
1446
1517
  }, i.src = n;
1447
1518
  });
1448
1519
  }
1449
- function Ps(e) {
1520
+ function Ws(e) {
1450
1521
  const t = new Blob([e], { type: "video/mp4" });
1451
1522
  return URL.createObjectURL(t);
1452
1523
  }
1453
- async function Ds(e) {
1454
- const t = Gr();
1455
- if (!At()) return t;
1524
+ async function Hs(e) {
1525
+ const t = Qr();
1526
+ if (!It()) return t;
1456
1527
  if (!e) throw new Error("Missing asset key for encrypted idle video");
1457
- const n = await Pt(t, e);
1458
- return Ps(n);
1528
+ const n = await Rt(t, e);
1529
+ return Ws(n);
1459
1530
  }
1460
- function Zt(e, t) {
1531
+ function en(e, t) {
1461
1532
  return `${e}/${t}_minus_sil.png`;
1462
1533
  }
1463
- const Jn = 5;
1464
- function en(e) {
1534
+ const rr = 5;
1535
+ function tn(e) {
1465
1536
  const t = e?.composite_threshold;
1466
1537
  if (typeof t == "number" && t > 0) return t;
1467
1538
  const n = e?.crop?.content_threshold;
1468
- return typeof n == "number" && n > 0 ? n : Jn;
1539
+ return typeof n == "number" && n > 0 ? n : rr;
1469
1540
  }
1470
- const ji = [
1541
+ const ta = [
1471
1542
  "aa",
1472
1543
  "E",
1473
1544
  "I",
@@ -1486,7 +1557,7 @@ const ji = [
1486
1557
  "t01_0.25",
1487
1558
  "t02_0.50",
1488
1559
  "t03_0.75"
1489
- ], Rs = Ht.length, En = /* @__PURE__ */ new Set([
1560
+ ], $s = Ht.length, In = /* @__PURE__ */ new Set([
1490
1561
  "aa__kk",
1491
1562
  "aa__nn",
1492
1563
  "aa__sil",
@@ -1593,75 +1664,75 @@ const ji = [
1593
1664
  "U__nn",
1594
1665
  "U__sil"
1595
1666
  ]);
1596
- function Tn(e, t) {
1667
+ function Mn(e, t) {
1597
1668
  return `${e}__${t}`;
1598
1669
  }
1599
- function An(e, t) {
1670
+ function Pn(e, t) {
1600
1671
  return `pairs/${e}/${t}_minus_sil.png`;
1601
1672
  }
1602
- function bt(e) {
1673
+ function vt(e) {
1603
1674
  return `${e}_minus_sil.png`;
1604
1675
  }
1605
- function Xn(e, t) {
1676
+ function sr(e, t) {
1606
1677
  if (t.has(e)) return e;
1607
1678
  const n = /^pairs\/([^/]+)\//.exec(e);
1608
1679
  if (!n) return null;
1609
1680
  const r = n[1].split("__")[1];
1610
1681
  if (!r || r === "sil") return null;
1611
- const s = bt(r);
1682
+ const s = vt(r);
1612
1683
  return t.has(s) ? s : null;
1613
1684
  }
1614
- function Qn(e, t) {
1685
+ function ir(e, t) {
1615
1686
  return e.filter((n) => t.has(n));
1616
1687
  }
1617
- function xs(e, t, n) {
1618
- const r = tn(e, t);
1619
- return Qn(r, n).length > 0;
1688
+ function qs(e, t, n) {
1689
+ const r = nn(e, t);
1690
+ return ir(r, n).length > 0;
1620
1691
  }
1621
- function tn(e, t) {
1692
+ function nn(e, t) {
1622
1693
  if (e === t) return [];
1623
- const n = [], r = Tn(e, t);
1624
- if (En.has(r)) {
1694
+ const n = [], r = Mn(e, t);
1695
+ if (In.has(r)) {
1625
1696
  for (const i of Ht)
1626
- n.push(An(r, i));
1697
+ n.push(Pn(r, i));
1627
1698
  return n;
1628
1699
  }
1629
- const s = Tn(t, e);
1630
- if (En.has(s)) {
1631
- for (let i = Rs - 1; i >= 0; i--)
1632
- n.push(An(s, Ht[i]));
1700
+ const s = Mn(t, e);
1701
+ if (In.has(s)) {
1702
+ for (let i = $s - 1; i >= 0; i--)
1703
+ n.push(Pn(s, Ht[i]));
1633
1704
  return n;
1634
1705
  }
1635
1706
  return [];
1636
1707
  }
1637
- function Cs(e) {
1708
+ function Ks(e) {
1638
1709
  return e < 16 ? [1] : e < 24 ? [1] : e < 35 ? [0, 1, 2] : [0, 1, 2];
1639
1710
  }
1640
- function Bs(e, t) {
1641
- return Cs(t).map((r) => e[r]).filter((r) => r != null);
1711
+ function Gs(e, t) {
1712
+ return Ks(t).map((r) => e[r]).filter((r) => r != null);
1642
1713
  }
1643
- function Fs(e, t, n, r) {
1714
+ function js(e, t, n, r) {
1644
1715
  if (r <= 0) return 0;
1645
1716
  const i = Math.max(1, n - t) / r, a = e - t, o = Math.floor(a / i);
1646
1717
  return Math.min(r - 1, Math.max(0, o));
1647
1718
  }
1648
- function Ls(e, t, n) {
1719
+ function zs(e, t, n) {
1649
1720
  if (!n) return `${e}/${t}`;
1650
1721
  const r = t.split("/").pop() ?? t, s = r.includes(".") ? r.slice(0, r.lastIndexOf(".")) : r;
1651
1722
  return `${e}/${s}.bin`;
1652
1723
  }
1653
- const Os = [0.32, 0.24, 0.16, 0.08, 0], Ns = Os.map(
1724
+ const Ys = [0.32, 0.24, 0.16, 0.08, 0], Js = Ys.map(
1654
1725
  (e) => `eyes_${e.toFixed(2)}`
1655
- ), Us = [
1726
+ ), Xs = [
1656
1727
  "eyes_0.32",
1657
1728
  "eyes_0.16",
1658
1729
  "eyes_0.00"
1659
- ], Vs = [
1730
+ ], Qs = [
1660
1731
  "eyes_0.32",
1661
1732
  "eyes_0.16",
1662
1733
  "eyes_0.08",
1663
1734
  "eyes_0.00"
1664
- ], In = 2, Ws = -30, $t = 20, Hs = {
1735
+ ], Rn = 2, Zs = -30, $t = 20, ei = {
1665
1736
  fear: -30,
1666
1737
  anger: -20,
1667
1738
  sad: -10,
@@ -1670,24 +1741,24 @@ const Os = [0.32, 0.24, 0.16, 0.08, 0], Ns = Os.map(
1670
1741
  happy: 0,
1671
1742
  disgust: 20
1672
1743
  };
1673
- function nn(e) {
1744
+ function rn(e) {
1674
1745
  return `brow_${e}`;
1675
1746
  }
1676
- function rn(e) {
1677
- return Hs[e];
1747
+ function sn(e) {
1748
+ return ei[e];
1678
1749
  }
1679
1750
  function Oe(e) {
1680
- return Math.max(Ws, Math.min($t, e));
1751
+ return Math.max(Zs, Math.min($t, e));
1681
1752
  }
1682
- function $s(e, t) {
1753
+ function ti(e, t) {
1683
1754
  if (e === t) return [];
1684
- const n = e < t ? In : -In, r = [];
1755
+ const n = e < t ? Rn : -Rn, r = [];
1685
1756
  let s = e;
1686
1757
  for (; s !== t; )
1687
1758
  s += n, r.push(s);
1688
1759
  return r;
1689
1760
  }
1690
- const qs = {
1761
+ const ni = {
1691
1762
  fear: "fear",
1692
1763
  afraid: "fear",
1693
1764
  anxiety: "fear",
@@ -1710,63 +1781,63 @@ const qs = {
1710
1781
  disgust: "disgust",
1711
1782
  disgusted: "disgust"
1712
1783
  };
1713
- function Ke(e, t = "neutral") {
1784
+ function Ge(e, t = "neutral") {
1714
1785
  const n = String(e ?? "").trim().toLowerCase();
1715
- return n ? qs[n] ?? t : t;
1786
+ return n ? ni[n] ?? t : t;
1716
1787
  }
1717
- function Zn(e) {
1718
- return e === 0 ? null : Zt(nn(e), "eyes_standard");
1788
+ function ar(e) {
1789
+ return e === 0 ? null : en(rn(e), "eyes_standard");
1719
1790
  }
1720
- function er(e) {
1721
- return Zn(rn(e));
1791
+ function or(e) {
1792
+ return ar(sn(e));
1722
1793
  }
1723
- function Mn(e, t) {
1724
- const n = nn(rn(e));
1725
- return !t || t === "eyes_standard" ? er(e) : e === "neutral" && t === "eyes_standard" ? null : Zt(n, t);
1794
+ function Dn(e, t) {
1795
+ const n = rn(sn(e));
1796
+ return !t || t === "eyes_standard" ? or(e) : e === "neutral" && t === "eyes_standard" ? null : en(n, t);
1726
1797
  }
1727
- function Ks(e, t) {
1798
+ function ri(e, t) {
1728
1799
  const n = [];
1729
1800
  for (const r of t)
1730
- n.push(Mn(e, r));
1801
+ n.push(Dn(e, r));
1731
1802
  for (let r = t.length - 2; r >= 0; r--)
1732
- n.push(Mn(e, t[r]));
1733
- return n.push(er(e)), n;
1803
+ n.push(Dn(e, t[r]));
1804
+ return n.push(or(e)), n;
1734
1805
  }
1735
- function Gs(e) {
1736
- return e === 0 ? null : Zt(nn(e), "eyes_standard");
1806
+ function si(e) {
1807
+ return e === 0 ? null : en(rn(e), "eyes_standard");
1737
1808
  }
1738
- function js(e, t) {
1739
- return $s(e, t).map(Gs).filter((n) => n != null);
1809
+ function ii(e, t) {
1810
+ return ti(e, t).map(si).filter((n) => n != null);
1740
1811
  }
1741
- const zs = 6;
1742
- function Ys(e, t) {
1812
+ const ai = 6;
1813
+ function oi(e, t) {
1743
1814
  for (const n of e)
1744
1815
  if (t >= n.wtime && t < n.wtime + n.wduration)
1745
1816
  return n;
1746
1817
  return null;
1747
1818
  }
1748
- function Js(e) {
1819
+ function ci(e) {
1749
1820
  const t = Oe(e);
1750
- return t >= $t ? $t : Oe(t + zs);
1821
+ return t >= $t ? $t : Oe(t + ai);
1751
1822
  }
1752
- function Xs(e, t, n) {
1823
+ function ui(e, t, n) {
1753
1824
  const r = Oe(n);
1754
- return Ys(t, e)?.emphasis ? Js(r) : r;
1825
+ return oi(t, e)?.emphasis ? ci(r) : r;
1755
1826
  }
1756
- function Pn(e) {
1827
+ function xn(e) {
1757
1828
  if (!e) return 0;
1758
1829
  const t = e.match(/^brow_(-?\d+)\//);
1759
1830
  if (!t?.[1]) return null;
1760
1831
  const n = Number.parseInt(t[1], 10);
1761
1832
  return Number.isFinite(n) ? Oe(n) : null;
1762
1833
  }
1763
- function lt(e, t) {
1834
+ function ct(e, t) {
1764
1835
  return e + Math.random() * (t - e);
1765
1836
  }
1766
- function _t() {
1767
- return lt(14, 32);
1837
+ function St() {
1838
+ return ct(14, 32);
1768
1839
  }
1769
- function Qs(e = {}) {
1840
+ function li(e = {}) {
1770
1841
  const {
1771
1842
  initialDelayMs: t = 800 + Math.random() * 1200,
1772
1843
  intervalMinMs: n = 2200,
@@ -1778,78 +1849,78 @@ function Qs(e = {}) {
1778
1849
  postEmotionChangeDelayMs: f = 380,
1779
1850
  minDwellMs: d = 550
1780
1851
  } = e;
1781
- let u = "neutral", c = "neutral", m = 0, g = 0, b = 0, h = {
1852
+ let u = "neutral", c = "neutral", m = 0, w = 0, b = 0, h = {
1782
1853
  kind: "idle",
1783
1854
  nextBlinkAt: performance.now() + t
1784
- }, M = null, E = "__eyes_open__|neutral|brow:0";
1855
+ }, I = null, E = "__eyes_open__|neutral|brow:0";
1785
1856
  function _(S = u) {
1786
- E = `${M ?? "__eyes_open__"}|${S}|brow:${g}`;
1857
+ E = `${I ?? "__eyes_open__"}|${S}|brow:${w}`;
1787
1858
  }
1788
- function p(S, P = u) {
1789
- M = S;
1790
- const T = Pn(S);
1791
- T != null ? g = T : S == null && (g = 0), _(P);
1859
+ function p(S, M = u) {
1860
+ I = S;
1861
+ const T = xn(S);
1862
+ T != null ? w = T : S == null && (w = 0), _(M);
1792
1863
  }
1793
1864
  function v(S) {
1794
- return Zn(S);
1865
+ return ar(S);
1795
1866
  }
1796
- function O(S, P, T) {
1797
- const j = Oe(P), I = Oe(T);
1798
- if (m = I, j === I) {
1799
- p(v(I));
1867
+ function N(S, M, T) {
1868
+ const Z = Oe(M), D = Oe(T);
1869
+ if (m = D, Z === D) {
1870
+ p(v(D));
1800
1871
  return;
1801
1872
  }
1802
- const ne = [
1803
- ...js(j, I),
1804
- v(I)
1873
+ const X = [
1874
+ ...ii(Z, D),
1875
+ v(D)
1805
1876
  ];
1806
1877
  h = {
1807
1878
  kind: "browBlend",
1808
- paths: ne,
1879
+ paths: X,
1809
1880
  index: 0,
1810
- holdUntil: S + _t(),
1811
- targetBrow: I
1812
- }, p(ne[0] ?? null);
1881
+ holdUntil: S + St(),
1882
+ targetBrow: D
1883
+ }, p(X[0] ?? null);
1813
1884
  }
1814
- function F(S, P) {
1885
+ function F(S, M) {
1815
1886
  h = {
1816
1887
  kind: "idle",
1817
- nextBlinkAt: S + lt(n, r)
1818
- }, c = P, p(v(m), P);
1888
+ nextBlinkAt: S + ct(n, r)
1889
+ }, c = M, p(v(m), M);
1819
1890
  }
1820
- function z(S, P, T) {
1821
- const j = T ? Us : Math.random() < o ? Ns : Vs;
1891
+ function j(S, M, T) {
1892
+ const Z = T ? Xs : Math.random() < o ? Js : Qs;
1822
1893
  h = {
1823
1894
  kind: "playing",
1824
- paths: Ks(P, j),
1895
+ paths: ri(M, Z),
1825
1896
  index: 0,
1826
- holdUntil: S + _t(),
1897
+ holdUntil: S + St(),
1827
1898
  after: "idle",
1828
- blinkEmotion: P
1829
- }, c = P, p(h.paths[0] ?? null, P);
1899
+ blinkEmotion: M
1900
+ }, c = M, p(h.paths[0] ?? null, M);
1830
1901
  }
1831
- function x(S) {
1902
+ function R(S) {
1832
1903
  if (h.kind === "browBlend") {
1833
1904
  for (; S >= h.holdUntil && h.index < h.paths.length - 1; )
1834
- h.index += 1, h.holdUntil = S + _t();
1835
- const P = h.paths[h.index] ?? null;
1836
- p(P), S >= h.holdUntil && h.index >= h.paths.length - 1 && (g = h.targetBrow, m = h.targetBrow, p(v(h.targetBrow)), h = {
1905
+ h.index += 1, h.holdUntil = S + St();
1906
+ const M = h.paths[h.index] ?? null;
1907
+ p(M), S >= h.holdUntil && h.index >= h.paths.length - 1 && (w = h.targetBrow, m = h.targetBrow, p(v(h.targetBrow)), h = {
1837
1908
  kind: "idle",
1838
1909
  nextBlinkAt: S + f
1839
1910
  });
1840
1911
  return;
1841
1912
  }
1842
1913
  if (h.kind === "idle") {
1843
- p(v(m), c), S >= h.nextBlinkAt && z(S, c, !1);
1914
+ p(v(m), c), S >= h.nextBlinkAt && j(S, c, !1);
1844
1915
  return;
1845
1916
  }
1846
1917
  if (h.kind === "doublePause") {
1847
- p(v(m), h.blinkEmotion), S >= h.resumeAt && z(S, h.blinkEmotion, !0);
1918
+ p(v(m), h.blinkEmotion), S >= h.resumeAt && j(S, h.blinkEmotion, !0);
1848
1919
  return;
1849
1920
  }
1850
1921
  if (h.kind === "playing") {
1851
1922
  for (; S >= h.holdUntil && h.index < h.paths.length - 1; )
1852
- h.index += 1, h.holdUntil = S + _t();
1923
+ h.index += 1, h.holdUntil = S + St();
1853
1924
  if (p(h.paths[h.index] ?? null, h.blinkEmotion), S >= h.holdUntil && h.index >= h.paths.length - 1) {
1854
1925
  if (h.after === "resumeIdle") {
1855
1926
  F(S + f, u);
@@ -1858,7 +1929,7 @@ function Qs(e = {}) {
1858
1929
  if (h.after === "doublePause") {
1859
1930
  h = {
1860
1931
  kind: "doublePause",
1861
- resumeAt: S + lt(i, a),
1932
+ resumeAt: S + ct(i, a),
1862
1933
  blinkEmotion: h.blinkEmotion
1863
1934
  }, p(v(m), h.blinkEmotion);
1864
1935
  return;
@@ -1866,7 +1937,7 @@ function Qs(e = {}) {
1866
1937
  if (h.after === "idle" && Math.random() < s) {
1867
1938
  h = {
1868
1939
  kind: "doublePause",
1869
- resumeAt: S + lt(i, a),
1940
+ resumeAt: S + ct(i, a),
1870
1941
  blinkEmotion: h.blinkEmotion
1871
1942
  }, p(v(m), h.blinkEmotion);
1872
1943
  return;
@@ -1876,32 +1947,32 @@ function Qs(e = {}) {
1876
1947
  }
1877
1948
  }
1878
1949
  return {
1879
- advance: x,
1950
+ advance: R,
1880
1951
  setTargetEmotion(S) {
1881
- const P = Ke(S, u);
1882
- if (P === u) return;
1952
+ const M = Ge(S, u);
1953
+ if (M === u) return;
1883
1954
  const T = performance.now();
1884
- d > 0 && P !== u && !(u === "neutral" && P !== "neutral") && T - b < d || (u = P, c = P, b = T);
1955
+ d > 0 && M !== u && !(u === "neutral" && M !== "neutral") && T - b < d || (u = M, c = M, b = T);
1885
1956
  },
1886
1957
  setTargetEyebrow(S) {
1887
- const P = Oe(S);
1888
- if (P === m) return;
1889
- const T = performance.now(), j = h.kind === "browBlend" ? Pn(M) ?? g : g;
1890
- m = P, !(P === j && h.kind !== "browBlend") && (h.kind === "playing" || h.kind === "doublePause" || O(T, j, P));
1958
+ const M = Oe(S);
1959
+ if (M === m) return;
1960
+ const T = performance.now(), Z = h.kind === "browBlend" ? xn(I) ?? w : w;
1961
+ m = M, !(M === Z && h.kind !== "browBlend") && (h.kind === "playing" || h.kind === "doublePause" || N(T, Z, M));
1891
1962
  },
1892
1963
  getTargetEmotion: () => u,
1893
1964
  getTargetEyebrow: () => m,
1894
- getExpressionPath: () => M,
1965
+ getExpressionPath: () => I,
1895
1966
  getDrawKey: () => E,
1896
1967
  reset() {
1897
- u = "neutral", c = "neutral", m = 0, g = 0, b = 0, F(
1898
- performance.now() + lt(n, r),
1968
+ u = "neutral", c = "neutral", m = 0, w = 0, b = 0, F(
1969
+ performance.now() + ct(n, r),
1899
1970
  "neutral"
1900
1971
  );
1901
1972
  }
1902
1973
  };
1903
1974
  }
1904
- function Zs() {
1975
+ function fi() {
1905
1976
  const e = document.createElement("video");
1906
1977
  e.muted = !0, e.playsInline = !0, e.preload = "auto", e.setAttribute("playsinline", ""), e.loop = !0;
1907
1978
  let t = !1, n = null, r = "";
@@ -1944,17 +2015,17 @@ function Zs() {
1944
2015
  }
1945
2016
  };
1946
2017
  }
1947
- const ei = 50, tr = 400;
1948
- function ti(e, t, n) {
2018
+ const di = 50, cr = 400;
2019
+ function mi(e, t, n) {
1949
2020
  return e.filter((r) => {
1950
2021
  const s = r.queueIndex ?? -1;
1951
2022
  return s >= t && s <= n;
1952
2023
  });
1953
2024
  }
1954
- function ni(e, t) {
2025
+ function hi(e, t) {
1955
2026
  const n = [];
1956
2027
  for (const r of e) {
1957
- const s = ti(
2028
+ const s = mi(
1958
2029
  t,
1959
2030
  r.start_word,
1960
2031
  r.end_word
@@ -1963,30 +2034,30 @@ function ni(e, t) {
1963
2034
  const i = s[0], a = s[s.length - 1];
1964
2035
  n.push({
1965
2036
  sentence: r,
1966
- startMs: Math.max(0, i.wtime - ei),
1967
- endMs: a.wtime + a.wduration + tr
2037
+ startMs: Math.max(0, i.wtime - di),
2038
+ endMs: a.wtime + a.wduration + cr
1968
2039
  });
1969
2040
  }
1970
2041
  return n.sort((r, s) => r.startMs - s.startMs);
1971
2042
  }
1972
- function ri(e, t, n, r = "neutral") {
1973
- const s = Ke(r);
2043
+ function pi(e, t, n, r = "neutral") {
2044
+ const s = Ge(r);
1974
2045
  if (t.length === 0) return s;
1975
2046
  if (n.length === 0 || e < n[0].wtime)
1976
- return Ke(t[0]?.emotion, s);
1977
- const i = ni(t, n);
2047
+ return Ge(t[0]?.emotion, s);
2048
+ const i = hi(t, n);
1978
2049
  let a = null;
1979
2050
  for (const d of i)
1980
2051
  e >= d.startMs && e <= d.endMs && (a = d);
1981
2052
  if (a)
1982
- return Ke(a.sentence.emotion, s);
2053
+ return Ge(a.sentence.emotion, s);
1983
2054
  const o = n[n.length - 1], f = o.wtime + o.wduration;
1984
- return e > f + tr ? Ke(
2055
+ return e > f + cr ? Ge(
1985
2056
  t[t.length - 1]?.emotion,
1986
2057
  s
1987
2058
  ) : s;
1988
2059
  }
1989
- function sn(e, t, n, r, s = Jn) {
2060
+ function an(e, t, n, r, s = rr) {
1990
2061
  const i = new OffscreenCanvas(n, r), a = i.getContext("2d", { willReadFrequently: !0 });
1991
2062
  a.drawImage(e, 0, 0, n, r);
1992
2063
  const o = a.getImageData(0, 0, n, r), f = a.createImageData(n, r);
@@ -1995,45 +2066,45 @@ function sn(e, t, n, r, s = Jn) {
1995
2066
  for (const c of t) {
1996
2067
  u.clearRect(0, 0, n, r), u.drawImage(c, 0, 0, n, r);
1997
2068
  const m = u.getImageData(0, 0, n, r);
1998
- for (let g = 0; g < f.data.length; g += 4) {
1999
- const b = m.data[g], h = m.data[g + 1], M = m.data[g + 2];
2000
- Math.max(b, h, M) > s && (f.data[g] = b, f.data[g + 1] = h, f.data[g + 2] = M, f.data[g + 3] = 255);
2069
+ for (let w = 0; w < f.data.length; w += 4) {
2070
+ const b = m.data[w], h = m.data[w + 1], I = m.data[w + 2];
2071
+ Math.max(b, h, I) > s && (f.data[w] = b, f.data[w + 1] = h, f.data[w + 2] = I, f.data[w + 3] = 255);
2001
2072
  }
2002
2073
  }
2003
2074
  return a.putImageData(f, 0, 0), i;
2004
2075
  }
2005
- let Le = null, ze = null;
2006
- const vt = /* @__PURE__ */ new Map();
2007
- function si() {
2008
- Le = null, ze = null, vt.clear();
2076
+ let Le = null, Ye = null;
2077
+ const Et = /* @__PURE__ */ new Map();
2078
+ function wi() {
2079
+ Le = null, Ye = null, Et.clear();
2009
2080
  }
2010
- Kr(si);
2011
- function ii(e) {
2012
- return e ?? ze ?? void 0;
2081
+ Xr(wi);
2082
+ function gi(e) {
2083
+ return e ?? Ye ?? void 0;
2013
2084
  }
2014
- function an(e) {
2085
+ function on(e) {
2015
2086
  return new Promise((t, n) => {
2016
2087
  const r = new Image();
2017
2088
  r.decoding = "async", r.onload = () => t(r), r.onerror = () => n(new Error(`Failed to load image: ${e}`)), r.src = e;
2018
2089
  });
2019
2090
  }
2020
- async function ai(e) {
2021
- return At() ? Ms(
2022
- await Pt(kn(), e ?? "")
2023
- ) : an(kn());
2091
+ async function yi(e) {
2092
+ return It() ? Vs(
2093
+ await Rt(vn(), e ?? "")
2094
+ ) : on(vn());
2024
2095
  }
2025
- async function Dn(e, t, n, r) {
2026
- const s = n ? await As(e, r) : await fetch(e).then((d) => {
2096
+ async function Bn(e, t, n, r) {
2097
+ const s = n ? await Ns(e, r) : await fetch(e).then((d) => {
2027
2098
  if (!d.ok) throw new Error(`Failed to load ${e}`);
2028
2099
  return d.json();
2029
2100
  }), i = s.sheets?.[0];
2030
2101
  if (!i?.path) throw new Error(`${e} has no sheets[0].path`);
2031
- const a = n ? await Is(
2032
- await Pt(
2033
- Ls(t, i.path, n),
2102
+ const a = n ? await Us(
2103
+ await Rt(
2104
+ zs(t, i.path, n),
2034
2105
  r ?? ""
2035
2106
  )
2036
- ) : await an(`${t}/${i.path}`), o = /* @__PURE__ */ new Map();
2107
+ ) : await on(`${t}/${i.path}`), o = /* @__PURE__ */ new Map();
2037
2108
  for (const d of s.cells ?? [])
2038
2109
  d.path && o.set(d.path, d);
2039
2110
  const f = /* @__PURE__ */ new Map();
@@ -2042,29 +2113,29 @@ async function Dn(e, t, n, r) {
2042
2113
  return { atlas: a, atlasMeta: s, cellByPath: o, sheetByIndex: f, diffBase: t };
2043
2114
  }
2044
2115
  async function qt(e) {
2045
- const t = ii(e), n = At();
2116
+ const t = gi(e), n = It();
2046
2117
  if (n && !t)
2047
2118
  throw new Error("Encrypted assets enabled but no key provided");
2048
- if (Le && (!n || ze === (t ?? null))) {
2119
+ if (Le && (!n || Ye === (t ?? null))) {
2049
2120
  const i = await Le;
2050
2121
  return {
2051
- ...Ye(i),
2122
+ ...Je(i),
2052
2123
  visemePaths: [...i.viseme.cellByPath.keys()]
2053
2124
  };
2054
2125
  }
2055
- ze = t ?? null, vt.clear();
2056
- const r = jr();
2126
+ Ye = t ?? null, Et.clear();
2127
+ const r = Zr();
2057
2128
  Le = (async () => {
2058
- const i = await ai(t), a = await Dn(
2059
- zr(),
2129
+ const i = await yi(t), a = await Bn(
2130
+ es(),
2060
2131
  r,
2061
2132
  n,
2062
2133
  t
2063
2134
  );
2064
2135
  let o = null;
2065
2136
  try {
2066
- o = await Dn(
2067
- Yr(),
2137
+ o = await Bn(
2138
+ ts(),
2068
2139
  r,
2069
2140
  n,
2070
2141
  t
@@ -2075,89 +2146,89 @@ async function qt(e) {
2075
2146
  return { sil: i, viseme: a, expression: o };
2076
2147
  })();
2077
2148
  const s = await Le;
2078
- return { ...Ye(s), visemePaths: [...s.viseme.cellByPath.keys()] };
2149
+ return { ...Je(s), visemePaths: [...s.viseme.cellByPath.keys()] };
2079
2150
  }
2080
- async function on(e) {
2151
+ async function cn(e) {
2081
2152
  if (await qt(e), !Le) throw new Error("Assets not loaded");
2082
2153
  return Le;
2083
2154
  }
2084
- function Ye(e) {
2085
- return vs(
2155
+ function Je(e) {
2156
+ return Fs(
2086
2157
  e.viseme.atlasMeta,
2087
2158
  e.sil.naturalWidth,
2088
2159
  e.sil.naturalHeight
2089
2160
  );
2090
2161
  }
2091
- function oi(e, t) {
2162
+ function _i(e, t) {
2092
2163
  const n = document.createElement("canvas");
2093
2164
  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;
2094
2165
  }
2095
- async function Et(e, t, n) {
2096
- const r = `${t.diffBase}::${n}`, s = vt.get(r);
2166
+ async function Tt(e, t, n) {
2167
+ const r = `${t.diffBase}::${n}`, s = Et.get(r);
2097
2168
  if (s) return s;
2098
2169
  const i = (async () => {
2099
2170
  const a = t.cellByPath.get(n);
2100
2171
  if (!a) throw new Error(`No atlas cell for path: ${n}`);
2101
- const o = Es(t.atlasMeta, a), f = Ye(e);
2102
- if (Ts(a)) {
2103
- const c = oi(t, a), m = document.createElement("canvas");
2172
+ const o = Ls(t.atlasMeta, a), f = Je(e);
2173
+ if (Os(a)) {
2174
+ const c = _i(t, a), m = document.createElement("canvas");
2104
2175
  m.width = f.width, m.height = f.height;
2105
- const g = m.getContext("2d");
2106
- return g.fillStyle = "#000", g.fillRect(0, 0, f.width, f.height), g.drawImage(c, o.x, o.y), m;
2176
+ const w = m.getContext("2d");
2177
+ return w.fillStyle = "#000", w.fillRect(0, 0, f.width, f.height), w.drawImage(c, o.x, o.y), m;
2107
2178
  }
2108
- const d = `${t.diffBase}/${n}`, u = await an(d);
2179
+ const d = `${t.diffBase}/${n}`, u = await on(d);
2109
2180
  if (u.naturalWidth === f.width && u.naturalHeight === f.height)
2110
2181
  return u;
2111
2182
  throw new Error(`Diff PNG wrong size for ${n}`);
2112
2183
  })();
2113
- return vt.set(r, i), i;
2184
+ return Et.set(r, i), i;
2114
2185
  }
2115
- async function Rn(e, t, n, r, s, i) {
2186
+ async function Cn(e, t, n, r, s, i) {
2116
2187
  if (s) {
2117
- const { width: g, height: b } = s;
2188
+ const { width: w, height: b } = s;
2118
2189
  if (i && !i() || t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA)
2119
2190
  return;
2120
2191
  const h = document.createElement("canvas");
2121
- h.width = g, h.height = b;
2122
- const M = h.getContext("2d");
2123
- if (M.clearRect(0, 0, g, b), M.drawImage(t, 0, 0, g, b), i && !i()) return;
2124
- (e.width !== g || e.height !== b) && (e.width = g, e.height = b);
2192
+ h.width = w, h.height = b;
2193
+ const I = h.getContext("2d");
2194
+ if (I.clearRect(0, 0, w, b), I.drawImage(t, 0, 0, w, b), i && !i()) return;
2195
+ (e.width !== w || e.height !== b) && (e.width = w, e.height = b);
2125
2196
  const E = e.getContext("2d");
2126
- E.clearRect(0, 0, g, b), E.drawImage(h, 0, 0);
2197
+ E.clearRect(0, 0, w, b), E.drawImage(h, 0, 0);
2127
2198
  return;
2128
2199
  }
2129
- const a = await on(), o = en(a.viseme.atlasMeta), { width: f, height: d } = Ye(a);
2200
+ const a = await cn(), o = tn(a.viseme.atlasMeta), { width: f, height: d } = Je(a);
2130
2201
  (e.width !== f || e.height !== d) && (e.width = f, e.height = d);
2131
2202
  const u = [], c = e.getContext("2d");
2132
2203
  if (c.clearRect(0, 0, f, d), t.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
2133
- const g = a.sil;
2134
- c.drawImage(g, 0, 0, f, d);
2204
+ const w = a.sil;
2205
+ c.drawImage(w, 0, 0, f, d);
2135
2206
  return;
2136
2207
  }
2137
2208
  if (u.length === 0) {
2138
2209
  c.drawImage(t, 0, 0, f, d);
2139
2210
  return;
2140
2211
  }
2141
- const m = sn(t, u, f, d, o);
2212
+ const m = an(t, u, f, d, o);
2142
2213
  c.drawImage(m, 0, 0);
2143
2214
  }
2144
2215
  async function Kt(e, t, n) {
2145
- const r = await on(ze ?? void 0), s = en(r.viseme.atlasMeta), { width: i, height: a } = Ye(r);
2216
+ const r = await cn(Ye ?? void 0), s = tn(r.viseme.atlasMeta), { width: i, height: a } = Je(r);
2146
2217
  (e.width !== i || e.height !== a) && (e.width = i, e.height = a);
2147
2218
  const o = [];
2148
2219
  if (t.mouthPath) {
2149
- const u = new Set(r.viseme.cellByPath.keys()), c = Xn(t.mouthPath, u) ?? (u.has(t.mouthPath) ? t.mouthPath : null);
2150
- c && o.push(await Et(r, r.viseme, c));
2220
+ const u = new Set(r.viseme.cellByPath.keys()), c = sr(t.mouthPath, u) ?? (u.has(t.mouthPath) ? t.mouthPath : null);
2221
+ c && o.push(await Tt(r, r.viseme, c));
2151
2222
  }
2152
2223
  t.expressionPath && r.expression && o.push(
2153
- await Et(r, r.expression, t.expressionPath)
2224
+ await Tt(r, r.expression, t.expressionPath)
2154
2225
  );
2155
2226
  const f = e.getContext("2d");
2156
2227
  if (f.clearRect(0, 0, i, a), o.length === 0) {
2157
2228
  f.drawImage(r.sil, 0, 0, i, a);
2158
2229
  return;
2159
2230
  }
2160
- const d = sn(
2231
+ const d = an(
2161
2232
  r.sil,
2162
2233
  o,
2163
2234
  i,
@@ -2166,19 +2237,19 @@ async function Kt(e, t, n) {
2166
2237
  );
2167
2238
  f.drawImage(d, 0, 0);
2168
2239
  }
2169
- async function ci(e, t) {
2170
- const n = await on(ze ?? void 0), r = en(n.viseme.atlasMeta), { width: s, height: i } = Ye(n), a = [];
2240
+ async function Si(e, t) {
2241
+ const n = await cn(Ye ?? void 0), r = tn(n.viseme.atlasMeta), { width: s, height: i } = Je(n), a = [];
2171
2242
  if (e.mouthPath) {
2172
- const u = new Set(n.viseme.cellByPath.keys()), c = Xn(e.mouthPath, u) ?? (u.has(e.mouthPath) ? e.mouthPath : null);
2173
- c && a.push(await Et(n, n.viseme, c));
2243
+ const u = new Set(n.viseme.cellByPath.keys()), c = sr(e.mouthPath, u) ?? (u.has(e.mouthPath) ? e.mouthPath : null);
2244
+ c && a.push(await Tt(n, n.viseme, c));
2174
2245
  }
2175
2246
  e.expressionPath && n.expression && a.push(
2176
- await Et(n, n.expression, e.expressionPath)
2247
+ await Tt(n, n.expression, e.expressionPath)
2177
2248
  );
2178
2249
  const o = new OffscreenCanvas(s, i), f = o.getContext("2d");
2179
2250
  if (a.length === 0)
2180
2251
  return f.drawImage(n.sil, 0, 0, s, i), o.transferToImageBitmap();
2181
- const d = sn(
2252
+ const d = an(
2182
2253
  n.sil,
2183
2254
  a,
2184
2255
  s,
@@ -2187,47 +2258,20 @@ async function ci(e, t) {
2187
2258
  );
2188
2259
  return f.drawImage(d, 0, 0), o.transferToImageBitmap();
2189
2260
  }
2190
- async function ui(e) {
2261
+ async function ki(e) {
2191
2262
  await Kt(e, { mouthPath: null, expressionPath: null });
2192
2263
  }
2193
- const li = 32;
2194
- function fi(e) {
2195
- const t = /* @__PURE__ */ new Map();
2196
- for (const n of e)
2197
- t.has(n.vtime) || t.set(n.vtime, n);
2198
- return [...t.values()].sort((n, r) => n.vtime - r.vtime);
2199
- }
2200
- function di(e) {
2201
- const t = fi(e), n = [];
2202
- for (let r = 0; r < t.length - 1; r++) {
2203
- const s = t[r].viseme, i = t[r + 1].viseme, a = t[r].vtime, o = t[r + 1].vtime;
2204
- s === i || o <= a || n.push({ from: s, to: i, fromVtime: a, toVtime: o });
2205
- }
2206
- return n;
2207
- }
2208
- function mi(e, t) {
2209
- let n = null;
2210
- for (const r of e) {
2211
- const { transEnd: s } = nr(r.fromVtime, r.toVtime);
2212
- t >= r.fromVtime && t < s && (n = r);
2213
- }
2214
- return n;
2215
- }
2216
- function nr(e, t) {
2217
- const n = Math.max(0, t - e), r = n > 0 ? Math.max(n, li) : 0, s = e, i = e + r;
2218
- return { gapMs: n, effectiveGapMs: r, holdEnd: e, transStart: s, transEnd: i };
2219
- }
2220
- let rr = null;
2221
- function xn(e) {
2222
- rr = e;
2264
+ let ur = null;
2265
+ function Fn(e) {
2266
+ ur = e;
2223
2267
  }
2224
2268
  function Ne() {
2225
- return rr;
2269
+ return ur;
2226
2270
  }
2227
- function hi(e) {
2271
+ function bi(e) {
2228
2272
  return `${e.from}→${e.to}@${e.fromVtime}`;
2229
2273
  }
2230
- function Cn() {
2274
+ function Ln() {
2231
2275
  return {
2232
2276
  rafTicksSpeaking: 0,
2233
2277
  rafTicksDeduped: 0,
@@ -2245,12 +2289,12 @@ function Cn() {
2245
2289
  bufferPrefetched: 0
2246
2290
  };
2247
2291
  }
2248
- function pi() {
2249
- let e = Cn();
2292
+ function vi() {
2293
+ let e = Ln();
2250
2294
  const t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Set();
2251
2295
  let s = "", i = -1, a = "";
2252
2296
  const o = () => {
2253
- e = Cn(), t.clear(), n.clear(), r.clear(), s = "", i = -1, a = "";
2297
+ e = Ln(), t.clear(), n.clear(), r.clear(), s = "", i = -1, a = "";
2254
2298
  }, f = (u) => {
2255
2299
  t.add(u);
2256
2300
  }, d = (u) => {
@@ -2274,7 +2318,7 @@ function pi() {
2274
2318
  e.workerStale += 1;
2275
2319
  },
2276
2320
  onTransitionSample(u, c) {
2277
- const m = hi(u);
2321
+ const m = bi(u);
2278
2322
  e.transitionFramesSampled += 1, m !== s && (s = m, i = -1, a !== m && (a = m, e.rifeFramesOmittedByGap += u.omittedByGap)), c && n.add(c), i >= 0 && u.frameIdx > i + 1 && (e.transitionFramesMissed += u.frameIdx - i - 1), i = u.frameIdx;
2279
2323
  },
2280
2324
  onBufferTick() {
@@ -2294,8 +2338,8 @@ function pi() {
2294
2338
  onDrawDisplayed(u, c) {
2295
2339
  if (e.drawsDisplayed += 1, u.transitionMeta) {
2296
2340
  e.transitionFramesDisplayed += 1;
2297
- const g = u.transitionMeta;
2298
- f(g.fromVtime), f(g.toVtime), d(u.mouthPath);
2341
+ const w = u.transitionMeta;
2342
+ f(w.fromVtime), f(w.toVtime), d(u.mouthPath);
2299
2343
  return;
2300
2344
  }
2301
2345
  if (u.label.includes("→")) {
@@ -2304,88 +2348,90 @@ function pi() {
2304
2348
  }
2305
2349
  const m = u.label.trim();
2306
2350
  if (!(!m || m === "sil")) {
2307
- for (const g of c)
2308
- if (g.viseme === m && u.elapsedMs >= g.vtime && u.elapsedMs < g.vtime + g.vduration) {
2309
- f(g.vtime);
2351
+ for (const w of c)
2352
+ if (w.viseme === m && u.elapsedMs >= w.vtime && u.elapsedMs < w.vtime + w.vduration) {
2353
+ f(w.vtime);
2310
2354
  break;
2311
2355
  }
2312
2356
  }
2313
2357
  },
2314
2358
  snapshot(u, c) {
2315
- let m = 0, g = 0;
2359
+ let m = 0, w = 0;
2316
2360
  for (const _ of u)
2317
2361
  if (_.viseme !== "sil") {
2318
2362
  if (_.vtime >= c) break;
2319
- m += 1, t.has(_.vtime) && (g += 1);
2363
+ m += 1, t.has(_.vtime) && (w += 1);
2320
2364
  }
2321
- const b = m > 0 ? Math.round(g / m * 100) : 100, h = e.queueSuperseded + e.workerStale + Math.max(0, e.drawsRequested - e.drawsDisplayed), M = m - g, E = e.bufferTicks > 0 ? Math.round(e.bufferHits / e.bufferTicks * 100) : 100;
2365
+ const b = m > 0 ? Math.round(w / m * 100) : 100, h = e.queueSuperseded + e.workerStale + Math.max(0, e.drawsRequested - e.drawsDisplayed), I = m - w, E = e.bufferTicks > 0 ? Math.round(e.bufferHits / e.bufferTicks * 100) : 100;
2322
2366
  return {
2323
2367
  ...e,
2324
2368
  bufferHitRate: E,
2325
2369
  renderLoss: h,
2326
2370
  visemeSlotsTotal: m,
2327
- visemeSlotsCovered: g,
2371
+ visemeSlotsCovered: w,
2328
2372
  visemeCoveragePct: b,
2329
2373
  transitionFramesUniqueSampled: n.size,
2330
2374
  transitionFramesUniquePainted: r.size,
2331
2375
  drawsDeduped: e.rafTicksDeduped,
2332
2376
  renderSkips: e.rafTicksDeduped + h,
2333
- visemesShown: g,
2334
- visemesSkipped: M,
2377
+ visemesShown: w,
2378
+ visemesSkipped: I,
2335
2379
  rifeTransitionFramesSkipped: e.transitionFramesMissed
2336
2380
  };
2337
2381
  }
2338
2382
  };
2339
2383
  }
2340
- const sr = 400, wi = 200, gi = 100, yi = 600, ir = "__sil__";
2341
- function _i(e) {
2342
- return e.lastDrawnKey ? e.lastDrawnKey.split("|")[0] === ir : !1;
2384
+ const lr = zt, Ei = 200, Ti = 100, Ai = 600, fr = "__sil__";
2385
+ function Ii(e) {
2386
+ return e.lastDrawnKey ? e.lastDrawnKey.split("|")[0] === fr : !1;
2343
2387
  }
2344
- function cn() {
2388
+ function un() {
2345
2389
  return { lastDrawnKey: "" };
2346
2390
  }
2347
- function ve(e) {
2391
+ function ke(e) {
2348
2392
  e.lastDrawnKey = "", e.transitionCache = void 0;
2349
2393
  }
2350
- function Si(e, t) {
2394
+ function Mi(e, t) {
2351
2395
  if (t?.transitionCache && t.transitionCache.queueLength === e.length)
2352
2396
  return t.transitionCache.transitions;
2353
- const n = di(e);
2397
+ const n = gs(e);
2354
2398
  return t && (t.transitionCache = { queueLength: e.length, transitions: n }), n;
2355
2399
  }
2356
- function ki(e, t) {
2400
+ function Pi(e, t) {
2357
2401
  let n = "sil";
2358
2402
  for (const r of e)
2359
2403
  r.viseme !== "sil" && (t != null && r.vtime > t || (n = r.viseme));
2360
2404
  return n;
2361
2405
  }
2362
- function bi(e, t, n, r = 120) {
2406
+ function Ri(e, t, n, r = 120) {
2363
2407
  if (n !== "sil")
2364
2408
  return n;
2365
- const s = Kn(e, t, r);
2366
- return s !== "sil" ? s : ki(e, t);
2409
+ const s = Qn(e, t, r);
2410
+ return s !== "sil" ? s : Pi(e, t);
2367
2411
  }
2368
- function vi(e, t) {
2369
- return e === "sil" ? !1 : t ? xs(e, "sil", t) : tn(e, "sil").length > 0;
2412
+ function Di(e, t) {
2413
+ return e === "sil" ? !1 : t ? qs(e, "sil", t) : nn(e, "sil").length > 0;
2370
2414
  }
2371
- function Ei() {
2372
- return sr + yi;
2415
+ function xi() {
2416
+ return lr + Ai;
2373
2417
  }
2374
- function Tt(e) {
2375
- return e ?? ir;
2418
+ function At(e) {
2419
+ return e ?? fr;
2376
2420
  }
2377
- function un(e, t, n, r, s = gi) {
2421
+ function ln(e, t, n, r, s = Ti) {
2378
2422
  if (e.length === 0)
2379
2423
  return { diffPath: null, label: "sil" };
2380
- const i = Si(e, r), a = mi(i, t);
2424
+ if (t >= Fe(e))
2425
+ return { diffPath: null, label: "sil" };
2426
+ const i = Mi(e, r), a = ys(i, t);
2381
2427
  if (a) {
2382
- const f = nr(a.fromVtime, a.toVtime);
2428
+ const f = Yn(a.fromVtime, a.toVtime);
2383
2429
  if (t >= f.transStart && t < f.transEnd) {
2384
- let d = tn(a.from, a.to);
2385
- n && (d = Qn(d, n));
2386
- const u = Bs(d, f.effectiveGapMs);
2430
+ let d = nn(a.from, a.to);
2431
+ n && (d = ir(d, n));
2432
+ const u = Gs(d, f.effectiveGapMs);
2387
2433
  if (u.length > 0) {
2388
- const c = Fs(
2434
+ const c = js(
2389
2435
  t,
2390
2436
  f.transStart,
2391
2437
  f.transEnd,
@@ -2406,48 +2452,48 @@ function un(e, t, n, r, s = gi) {
2406
2452
  }
2407
2453
  };
2408
2454
  }
2409
- return { diffPath: bt(a.to), label: a.to };
2455
+ return { diffPath: vt(a.to), label: a.to };
2410
2456
  }
2411
2457
  }
2412
- const o = Kn(e, t, s);
2413
- return o === "sil" ? { diffPath: null, label: "sil" } : { diffPath: bt(o), label: o };
2458
+ const o = Qn(e, t, s);
2459
+ return o === "sil" ? { diffPath: null, label: "sil" } : { diffPath: vt(o), label: o };
2414
2460
  }
2415
- function ln(e, t) {
2416
- const n = t ?? Tt(e.expressionPath);
2417
- return `${Tt(e.mouthPath)}|${n}`;
2461
+ function fn(e, t) {
2462
+ const n = t ?? At(e.expressionPath);
2463
+ return `${At(e.mouthPath)}|${n}`;
2418
2464
  }
2419
- function Ti(e, t, n, r, s, i, a, o, f, d) {
2420
- const u = un(t, n, f, i), c = {
2465
+ function Bi(e, t, n, r, s, i, a, o, f, d) {
2466
+ const u = ln(t, n, f, i), c = {
2421
2467
  mouthPath: u.diffPath,
2422
2468
  expressionPath: r
2423
- }, m = ln(c, s), g = Ne();
2424
- if (u.transitionMeta && g?.onTransitionSample(u.transitionMeta, u.diffPath), m === i.lastDrawnKey) {
2425
- g?.onDrawDeduped();
2469
+ }, m = fn(c, s), w = Ne();
2470
+ if (u.transitionMeta && w?.onTransitionSample(u.transitionMeta, u.diffPath), m === i.lastDrawnKey) {
2471
+ w?.onDrawDeduped();
2426
2472
  return;
2427
2473
  }
2428
- g?.onDrawRequested(), o(u), i.lastDrawnKey = m, a(e, c, m, (b) => {
2474
+ w?.onDrawRequested(), o(u), i.lastDrawnKey = m, a(e, c, m, (b) => {
2429
2475
  b === m && (i.lastDrawnKey = m);
2430
2476
  });
2431
2477
  }
2432
- function Bn(e, t, n, r, s, i, a = null) {
2433
- const o = { mouthPath: null, expressionPath: t }, f = a != null ? `|idle@${Math.round(a * 1e3)}` : "", d = ln(o, n) + f;
2478
+ function On(e, t, n, r, s, i, a = null) {
2479
+ const o = { mouthPath: null, expressionPath: t }, f = a != null ? `|idle@${Math.round(a * 1e3)}` : "", d = fn(o, n) + f;
2434
2480
  !(a != null) && d === r.lastDrawnKey || (i(
2435
2481
  a != null ? "idle" : "sil"
2436
2482
  ), s(e, o, d, (c) => {
2437
2483
  c === d && (r.lastDrawnKey = d);
2438
2484
  }));
2439
2485
  }
2440
- function ar(e, t, n, r, s = ft, i) {
2486
+ function dr(e, t, n, r, s = ut, i) {
2441
2487
  if (n <= t || e.length === 0) return [];
2442
- const a = i ?? cn(), o = [];
2488
+ const a = i ?? un(), o = [];
2443
2489
  let f = "";
2444
2490
  for (let d = t; d < n; d += s) {
2445
- const u = un(
2491
+ const u = ln(
2446
2492
  e,
2447
2493
  d,
2448
2494
  r,
2449
2495
  a
2450
- ), c = Tt(u.diffPath);
2496
+ ), c = At(u.diffPath);
2451
2497
  c !== f && (f = c, o.push({
2452
2498
  displayAtMs: d,
2453
2499
  mouthPath: u.diffPath,
@@ -2457,8 +2503,8 @@ function ar(e, t, n, r, s = ft, i) {
2457
2503
  }
2458
2504
  return o;
2459
2505
  }
2460
- const Ai = 3;
2461
- function or(e) {
2506
+ const Ci = 3;
2507
+ function mr(e) {
2462
2508
  let t = 0;
2463
2509
  const n = [];
2464
2510
  let r = !1, s = !1;
@@ -2482,7 +2528,7 @@ function or(e) {
2482
2528
  return {
2483
2529
  enqueue(o, f, d) {
2484
2530
  const u = n.findIndex((c) => c.drawKey === f);
2485
- u >= 0 ? n[u] = { diffs: o, drawKey: f, onDisplayed: d } : (n.length >= Ai && (n.shift(), Ne()?.onQueueSuperseded()), n.push({ diffs: o, drawKey: f, onDisplayed: d })), a();
2531
+ u >= 0 ? n[u] = { diffs: o, drawKey: f, onDisplayed: d } : (n.length >= Ci && (n.shift(), Ne()?.onQueueSuperseded()), n.push({ diffs: o, drawKey: f, onDisplayed: d })), a();
2486
2532
  },
2487
2533
  reset() {
2488
2534
  t += 1, n.length = 0;
@@ -2495,7 +2541,7 @@ function or(e) {
2495
2541
  }
2496
2542
  };
2497
2543
  }
2498
- function Ii(e, t = 2) {
2544
+ function Fi(e, t = 2) {
2499
2545
  const n = [], r = /* @__PURE__ */ new Set();
2500
2546
  let s = 0, i = !1, a = 0;
2501
2547
  const o = (d, u) => `${d}|${u}`, f = async () => {
@@ -2537,7 +2583,7 @@ function Ii(e, t = 2) {
2537
2583
  }
2538
2584
  };
2539
2585
  }
2540
- function Mi() {
2586
+ function Li() {
2541
2587
  const e = /* @__PURE__ */ new Map(), t = (n) => {
2542
2588
  n.bitmap.close();
2543
2589
  };
@@ -2572,13 +2618,13 @@ function Mi() {
2572
2618
  }
2573
2619
  };
2574
2620
  }
2575
- function Pi(e, t, n, r, s, i) {
2576
- const a = ar(
2621
+ function Oi(e, t, n, r, s, i) {
2622
+ const a = dr(
2577
2623
  t,
2578
2624
  n,
2579
2625
  r,
2580
2626
  s,
2581
- ft,
2627
+ ut,
2582
2628
  i
2583
2629
  );
2584
2630
  if (a.length === 0) return { ready: 0, total: 0 };
@@ -2587,10 +2633,10 @@ function Pi(e, t, n, r, s, i) {
2587
2633
  e.has(f.displayAtMs, f.mouthKey) && (o += 1);
2588
2634
  return { ready: o, total: a.length };
2589
2635
  }
2590
- function Di(e) {
2591
- const t = Mi(), n = cn(), r = /* @__PURE__ */ new Set();
2636
+ function Ni(e) {
2637
+ const t = Li(), n = un(), r = /* @__PURE__ */ new Set();
2592
2638
  let s = "", i = null;
2593
- const a = Ii(
2639
+ const a = Fi(
2594
2640
  async (c) => {
2595
2641
  try {
2596
2642
  if (!e.supportsBufferedPlayback) return;
@@ -2610,7 +2656,7 @@ function Di(e) {
2610
2656
  r.delete(c.mouthKey);
2611
2657
  }
2612
2658
  },
2613
- Mr
2659
+ Cr
2614
2660
  ), o = (c) => {
2615
2661
  for (const m of c)
2616
2662
  t.has(m.displayAtMs, m.mouthKey) || r.has(m.mouthKey) || a.isQueuedOrInflight(0, m.mouthKey) || (r.add(m.mouthKey), a.enqueue({
@@ -2622,23 +2668,23 @@ function Di(e) {
2622
2668
  expressionPath: null
2623
2669
  }
2624
2670
  }));
2625
- }, f = (c, m, g, b = Bt) => {
2671
+ }, f = (c, m, w, b = Ct) => {
2626
2672
  if (!e.supportsBufferedPlayback) return;
2627
- const h = Math.max(0, m - ft * 4), M = ar(
2673
+ const h = Math.max(0, m - ut * 4), I = dr(
2628
2674
  c,
2629
2675
  h,
2630
2676
  m + b,
2631
- g,
2632
- ft,
2677
+ w,
2678
+ ut,
2633
2679
  n
2634
2680
  );
2635
- o(M);
2681
+ o(I);
2636
2682
  }, d = (c, m) => {
2637
2683
  s = c, i = m;
2638
2684
  }, u = (c, m) => !(s !== c || i !== m);
2639
2685
  return {
2640
2686
  reset() {
2641
- t.reset(), a.reset(), r.clear(), ve(n), s = "", i = null, Ne()?.onBufferReset();
2687
+ t.reset(), a.reset(), r.clear(), ke(n), s = "", i = null, Ne()?.onBufferReset();
2642
2688
  },
2643
2689
  markExternallyDisplayed(c, m) {
2644
2690
  d(c.split("|")[0] ?? c, m);
@@ -2648,12 +2694,12 @@ function Di(e) {
2648
2694
  c.queue,
2649
2695
  0,
2650
2696
  c.availablePaths,
2651
- Math.max(yn, Bt)
2697
+ Math.max(Sn, Ct)
2652
2698
  );
2653
2699
  },
2654
- isWarmupReady(c, m = yn) {
2700
+ isWarmupReady(c, m = Sn) {
2655
2701
  if (!e.supportsBufferedPlayback) return !0;
2656
- const { ready: g, total: b } = Pi(
2702
+ const { ready: w, total: b } = Oi(
2657
2703
  t,
2658
2704
  c.queue,
2659
2705
  0,
@@ -2661,7 +2707,7 @@ function Di(e) {
2661
2707
  c.availablePaths,
2662
2708
  n
2663
2709
  );
2664
- return b === 0 ? !1 : g / b >= Ir;
2710
+ return b === 0 ? !1 : w / b >= Br;
2665
2711
  },
2666
2712
  getBufferedFrameCount: () => t.size(),
2667
2713
  tick(c) {
@@ -2670,22 +2716,22 @@ function Di(e) {
2670
2716
  c.queue,
2671
2717
  c.elapsedMs,
2672
2718
  c.availablePaths
2673
- ), t.pruneBefore(c.elapsedMs - Bt);
2674
- const g = un(
2719
+ ), t.pruneBefore(c.elapsedMs - Ct);
2720
+ const w = ln(
2675
2721
  c.queue,
2676
2722
  c.elapsedMs,
2677
2723
  c.availablePaths,
2678
2724
  n
2679
- ), b = Tt(g.diffPath), h = ln(
2680
- { mouthPath: g.diffPath, expressionPath: c.expressionPath },
2725
+ ), b = At(w.diffPath), h = fn(
2726
+ { mouthPath: w.diffPath, expressionPath: c.expressionPath },
2681
2727
  c.expressionDrawKey
2682
- ), M = () => (d(b, c.expressionPath), m?.onBufferHit(), { usedBuffer: !0, underrun: !1, held: !0 });
2728
+ ), I = () => (d(b, c.expressionPath), m?.onBufferHit(), { usedBuffer: !0, underrun: !1, held: !0 });
2683
2729
  if (u(b, c.expressionPath) || c.playbackState.lastDrawnKey === h)
2684
- return M();
2730
+ return I();
2685
2731
  const E = t.findForMouthKey(
2686
2732
  b,
2687
2733
  c.elapsedMs,
2688
- ft
2734
+ ut
2689
2735
  );
2690
2736
  return E && c.expressionPath != null ? (m?.onBufferHit(), { usedBuffer: !1, underrun: !1, held: !1 }) : E ? (e.blitBitmap(c.canvas, E.bitmap), d(E.mouthKey, c.expressionPath), c.playbackState.lastDrawnKey = h, c.onStatus({
2691
2737
  diffPath: E.mouthPath,
@@ -2694,9 +2740,9 @@ function Di(e) {
2694
2740
  }
2695
2741
  };
2696
2742
  }
2697
- const Fn = -1, St = 1e5, Ln = /* @__PURE__ */ new Map();
2698
- async function Ri(e) {
2699
- let t = Ln.get(e);
2743
+ const Nn = -1, kt = 1e5, Un = /* @__PURE__ */ new Map();
2744
+ async function Ui(e) {
2745
+ let t = Un.get(e);
2700
2746
  if (!t) {
2701
2747
  const n = await fetch(e);
2702
2748
  if (!n.ok)
@@ -2704,11 +2750,11 @@ async function Ri(e) {
2704
2750
  `Failed to fetch worker script (${n.status}): ${e}`
2705
2751
  );
2706
2752
  const r = await n.blob();
2707
- t = URL.createObjectURL(r), Ln.set(e, t);
2753
+ t = URL.createObjectURL(r), Un.set(e, t);
2708
2754
  }
2709
2755
  return new Worker(t, { name: "visemeDiffPreview" });
2710
2756
  }
2711
- function xi() {
2757
+ function Vi() {
2712
2758
  return typeof Worker < "u" && typeof OffscreenCanvas < "u";
2713
2759
  }
2714
2760
  function Gt(e, t) {
@@ -2716,9 +2762,9 @@ function Gt(e, t) {
2716
2762
  const n = e.getContext("2d");
2717
2763
  n && n.drawImage(t, 0, 0);
2718
2764
  }
2719
- function On(e, t) {
2765
+ function Vn(e, t) {
2720
2766
  let n = 0, r = null;
2721
- const s = or(async (i, a) => (await qt(), await Kt(e, i), s.isLatestSeq(a)));
2767
+ const s = mr(async (i, a) => (await qt(), await Kt(e, i), s.isLatestSeq(a)));
2722
2768
  return {
2723
2769
  usesWorker: !1,
2724
2770
  async loadAssets(i) {
@@ -2729,14 +2775,14 @@ function On(e, t) {
2729
2775
  return r;
2730
2776
  },
2731
2777
  async renderSilOnly() {
2732
- n += 1, s.reset(), await ui(e);
2778
+ n += 1, s.reset(), await ki(e);
2733
2779
  },
2734
2780
  drawLiveFrame(i, a, o, f) {
2735
2781
  s.enqueue(a, o, f);
2736
2782
  },
2737
2783
  async compositePrefetchFrame(i, a) {
2738
2784
  try {
2739
- return await ci(i);
2785
+ return await Si(i);
2740
2786
  } catch {
2741
2787
  return null;
2742
2788
  }
@@ -2749,7 +2795,7 @@ function On(e, t) {
2749
2795
  n += 1;
2750
2796
  const o = n;
2751
2797
  t.onStatus?.(a), await Kt(i, {
2752
- mouthPath: bt(a),
2798
+ mouthPath: vt(a),
2753
2799
  expressionPath: null
2754
2800
  });
2755
2801
  },
@@ -2758,14 +2804,14 @@ function On(e, t) {
2758
2804
  }
2759
2805
  };
2760
2806
  }
2761
- function Ci(e, t, n) {
2807
+ function Wi(e, t, n) {
2762
2808
  let r = null, s = 1, i = 0, a = !1, o = !1, f = null;
2763
2809
  const d = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map();
2764
- let c = St;
2765
- const m = or(async (_, p, v) => (d.set(p, {
2810
+ let c = kt;
2811
+ const m = mr(async (_, p, v) => (d.set(p, {
2766
2812
  drawKey: v.drawKey,
2767
2813
  onDisplayed: v.onDisplayed
2768
- }), await M(
2814
+ }), await I(
2769
2815
  {
2770
2816
  type: "drawFrame",
2771
2817
  diffPath: _.mouthPath,
@@ -2774,31 +2820,31 @@ function Ci(e, t, n) {
2774
2820
  },
2775
2821
  void 0,
2776
2822
  !0
2777
- ), !1)), g = (_) => {
2778
- if (_ === Fn) {
2779
- let O = -1, F;
2780
- for (const [z, x] of d)
2781
- z >= O && (O = z, F = x);
2782
- O >= 0 && d.delete(O), F?.onDisplayed?.(F.drawKey);
2823
+ ), !1)), w = (_) => {
2824
+ if (_ === Nn) {
2825
+ let N = -1, F;
2826
+ for (const [j, R] of d)
2827
+ j >= N && (N = j, F = R);
2828
+ N >= 0 && d.delete(N), F?.onDisplayed?.(F.drawKey);
2783
2829
  return;
2784
2830
  }
2785
2831
  const p = -_, v = d.get(p);
2786
2832
  d.delete(p), v?.onDisplayed?.(v.drawKey);
2787
2833
  }, b = /* @__PURE__ */ new Map(), h = (_, p) => {
2788
2834
  o || !r || r.postMessage(_, []);
2789
- }, M = (_, p, v = !1) => {
2835
+ }, I = (_, p, v = !1) => {
2790
2836
  if (o) return Promise.resolve();
2791
- const O = s++;
2792
- return new Promise((F, z) => {
2793
- b.set(O, {
2794
- resolve: (x) => F(x),
2795
- reject: z,
2837
+ const N = s++;
2838
+ return new Promise((F, j) => {
2839
+ b.set(N, {
2840
+ resolve: (R) => F(R),
2841
+ reject: j,
2796
2842
  generation: typeof _.generation == "number" ? _.generation : void 0,
2797
2843
  expectRenderDone: v
2798
- }), h({ ..._, requestId: O });
2844
+ }), h({ ..._, requestId: N });
2799
2845
  });
2800
2846
  }, E = (async () => {
2801
- r = await Ri(n), r.onmessage = (_) => {
2847
+ r = await Ui(n), r.onmessage = (_) => {
2802
2848
  if (o) return;
2803
2849
  const p = _.data;
2804
2850
  if (p.type === "status") {
@@ -2806,28 +2852,28 @@ function Ci(e, t, n) {
2806
2852
  return;
2807
2853
  }
2808
2854
  if (p.type === "frame") {
2809
- if (p.generation <= -St) {
2810
- const F = -p.generation, z = u.get(F);
2811
- u.delete(F), z?.resolve(p.bitmap);
2855
+ if (p.generation <= -kt) {
2856
+ const F = -p.generation, j = u.get(F);
2857
+ u.delete(F), j?.resolve(p.bitmap);
2812
2858
  return;
2813
2859
  }
2814
- const O = p.generation < 0;
2815
- if (!O && p.generation !== i) {
2860
+ const N = p.generation < 0;
2861
+ if (!N && p.generation !== i) {
2816
2862
  p.bitmap.close();
2817
2863
  return;
2818
2864
  }
2819
- if (O && !(p.generation === Fn)) {
2820
- const z = -p.generation;
2821
- if (!m.isLatestSeq(z)) {
2865
+ if (N && !(p.generation === Nn)) {
2866
+ const j = -p.generation;
2867
+ if (!m.isLatestSeq(j)) {
2822
2868
  Ne()?.onWorkerStale(), p.bitmap.close();
2823
2869
  return;
2824
2870
  }
2825
2871
  }
2826
- if (O && t.shouldAcceptLiveFrame && !t.shouldAcceptLiveFrame()) {
2872
+ if (N && t.shouldAcceptLiveFrame && !t.shouldAcceptLiveFrame()) {
2827
2873
  Ne()?.onWorkerStale(), p.bitmap.close();
2828
2874
  return;
2829
2875
  }
2830
- Gt(e, p.bitmap), p.bitmap.close(), O && g(p.generation);
2876
+ Gt(e, p.bitmap), p.bitmap.close(), N && w(p.generation);
2831
2877
  return;
2832
2878
  }
2833
2879
  const v = b.get(p.requestId);
@@ -2858,7 +2904,7 @@ function Ci(e, t, n) {
2858
2904
  for (const [, v] of b)
2859
2905
  v.reject(new Error(p));
2860
2906
  b.clear();
2861
- }, await M({ type: "init" }), a = !0;
2907
+ }, await I({ type: "init" }), a = !0;
2862
2908
  })();
2863
2909
  return {
2864
2910
  usesWorker: !0,
@@ -2866,10 +2912,10 @@ function Ci(e, t, n) {
2866
2912
  if (o) return { width: 842, height: 1264 };
2867
2913
  if (await E, o) return { width: 842, height: 1264 };
2868
2914
  if (!a) throw new Error("Worker init failed");
2869
- return await M({
2915
+ return await I({
2870
2916
  type: "loadAssets",
2871
2917
  keyHex: _,
2872
- urls: qr(Ue())
2918
+ urls: Jr(Ue())
2873
2919
  }) ?? { width: 842, height: 1264 };
2874
2920
  },
2875
2921
  getVisemeAtlasPaths() {
@@ -2877,25 +2923,25 @@ function Ci(e, t, n) {
2877
2923
  },
2878
2924
  async renderSilOnly() {
2879
2925
  if (o || (await E, o)) return;
2880
- i += 1, m.reset(), d.clear(), u.forEach(({ resolve: p }) => p(null)), u.clear(), c = St;
2926
+ i += 1, m.reset(), d.clear(), u.forEach(({ resolve: p }) => p(null)), u.clear(), c = kt;
2881
2927
  const _ = i;
2882
- await M({ type: "cancelLiveDraws" }), await M(
2928
+ await I({ type: "cancelLiveDraws" }), await I(
2883
2929
  { type: "renderSil", generation: _ },
2884
2930
  void 0,
2885
2931
  !0
2886
2932
  );
2887
2933
  },
2888
- drawLiveFrame(_, p, v, O) {
2934
+ drawLiveFrame(_, p, v, N) {
2889
2935
  o || E.then(() => {
2890
- o || m.enqueue(p, v, O);
2936
+ o || m.enqueue(p, v, N);
2891
2937
  });
2892
2938
  },
2893
2939
  compositePrefetchFrame(_, p) {
2894
2940
  return o ? Promise.resolve(null) : E.then(() => {
2895
2941
  if (o) return null;
2896
2942
  const v = ++c;
2897
- return new Promise((O) => {
2898
- u.set(v, { resolve: O }), M(
2943
+ return new Promise((N) => {
2944
+ u.set(v, { resolve: N }), I(
2899
2945
  {
2900
2946
  type: "drawFrame",
2901
2947
  diffPath: _.mouthPath,
@@ -2905,7 +2951,7 @@ function Ci(e, t, n) {
2905
2951
  void 0,
2906
2952
  !0
2907
2953
  ).catch(() => {
2908
- u.delete(v), O(null);
2954
+ u.delete(v), N(null);
2909
2955
  });
2910
2956
  });
2911
2957
  });
@@ -2917,12 +2963,12 @@ function Ci(e, t, n) {
2917
2963
  async renderViseme(_, p, v = {}) {
2918
2964
  if (o || (await E, o)) return;
2919
2965
  i += 1, m.reset(), d.clear();
2920
- const O = i;
2966
+ const N = i;
2921
2967
  try {
2922
- await M({ type: "cancelLiveDraws" }), await M(
2968
+ await I({ type: "cancelLiveDraws" }), await I(
2923
2969
  {
2924
2970
  type: "renderViseme",
2925
- generation: O,
2971
+ generation: N,
2926
2972
  to: p,
2927
2973
  from: v.from,
2928
2974
  transitionDurationMs: v.transitionDurationMs,
@@ -2939,7 +2985,7 @@ function Ci(e, t, n) {
2939
2985
  },
2940
2986
  dispose() {
2941
2987
  if (!o) {
2942
- o = !0, i += 1, m.reset(), d.clear(), u.forEach(({ resolve: _ }) => _(null)), u.clear(), c = St;
2988
+ o = !0, i += 1, m.reset(), d.clear(), u.forEach(({ resolve: _ }) => _(null)), u.clear(), c = kt;
2943
2989
  for (const [, _] of b)
2944
2990
  _.resolve();
2945
2991
  b.clear(), r?.terminate(), r = null;
@@ -2947,17 +2993,17 @@ function Ci(e, t, n) {
2947
2993
  }
2948
2994
  };
2949
2995
  }
2950
- function Bi(e, t = {}) {
2951
- if (!xi())
2952
- return On(e, t);
2953
- const n = Er(t.workerScriptUrl);
2996
+ function Hi(e, t = {}) {
2997
+ if (!Vi())
2998
+ return Vn(e, t);
2999
+ const n = Rr(t.workerScriptUrl);
2954
3000
  try {
2955
- return Ci(e, t, n);
3001
+ return Wi(e, t, n);
2956
3002
  } catch {
2957
- return On(e, t);
3003
+ return Vn(e, t);
2958
3004
  }
2959
3005
  }
2960
- class Fi extends Error {
3006
+ class $i extends Error {
2961
3007
  constructor(t, n) {
2962
3008
  super(`${t} timed out after ${Math.round(n / 1e3)}s`), this.name = "LoadTimeoutError";
2963
3009
  }
@@ -2965,7 +3011,7 @@ class Fi extends Error {
2965
3011
  function Nt(e, t, n) {
2966
3012
  return new Promise((r, s) => {
2967
3013
  const i = globalThis.setTimeout(() => {
2968
- s(new Fi(n, t));
3014
+ s(new $i(n, t));
2969
3015
  }, t);
2970
3016
  e.then(
2971
3017
  (a) => {
@@ -2977,15 +3023,15 @@ function Nt(e, t, n) {
2977
3023
  );
2978
3024
  });
2979
3025
  }
2980
- const Li = {
3026
+ const qi = {
2981
3027
  position: "relative",
2982
3028
  display: "inline-block",
2983
3029
  lineHeight: 0
2984
- }, Oi = {
3030
+ }, Ki = {
2985
3031
  display: "block",
2986
3032
  maxWidth: "100%",
2987
3033
  height: "auto"
2988
- }, Ni = {
3034
+ }, Gi = {
2989
3035
  position: "absolute",
2990
3036
  inset: 0,
2991
3037
  display: "flex",
@@ -2997,603 +3043,614 @@ const Li = {
2997
3043
  fontSize: "0.875rem",
2998
3044
  textAlign: "center"
2999
3045
  };
3000
- function ut(e) {
3001
- return Dr(e) ? jn : Gn;
3046
+ function ot(e) {
3047
+ return Lr(e) ? er : Zn;
3002
3048
  }
3003
- function Ce(e, t) {
3049
+ function Me(e, t) {
3004
3050
  return t?.isSpeaking() ? t : e.isSpeaking() ? e : t?.isConnected() ? t : e;
3005
3051
  }
3006
- const zi = mr(function({
3052
+ const na = _r(function({
3007
3053
  id: t,
3008
3054
  avatarId: n,
3009
3055
  faceId: r,
3010
3056
  assets: s,
3011
3057
  authToken: i,
3012
3058
  apiBase: a,
3013
- ttsEngineId: o = Ge,
3059
+ ttsEngineId: o = je,
3014
3060
  voiceId: f,
3015
- speakingRate: d = Mt,
3061
+ speakingRate: d = Pt,
3016
3062
  className: u,
3017
3063
  style: c,
3018
3064
  canvasStyle: m,
3019
- onStatusChange: g,
3065
+ onStatusChange: w,
3020
3066
  onDisplayStatus: b,
3021
3067
  onError: h,
3022
- onReady: M,
3068
+ onReady: I,
3023
3069
  showErrorOverlay: E = !0,
3024
3070
  onUserTranscript: _,
3025
3071
  onBotOutput: p,
3026
3072
  onRealtimeLipsyncDebug: v,
3027
- onFrameRenderStats: O,
3073
+ onFrameRenderStats: N,
3028
3074
  lipsyncBufferedPlayback: F = !0,
3029
- lipsyncPlaybackWarmupMs: z = 1500
3030
- }, x) {
3031
- const S = V(null), P = V(null), T = V(null), j = V(0), I = V(cn()), ne = V(Qs()), Z = V(0), X = V(0), he = V(0), se = V(null), ce = V(null), w = V(!1), R = V(!1), q = V(!1), L = V("sil"), D = V(0), N = V(!1), A = V(
3075
+ lipsyncPlaybackWarmupMs: j = 1500
3076
+ }, R) {
3077
+ const S = O(null), M = O(null), T = O(null), Z = O(F);
3078
+ Z.current = F;
3079
+ const D = O(0), X = O(un()), z = O(li()), se = O(0), ne = O(0), de = O(0), ue = O(null), y = O(null), A = O(!1), q = O(!1), x = O(!1), P = O("sil"), $ = O(0), C = O(!1), B = O(
3032
3080
  null
3033
- ), U = V(!1), [re, ee] = ct("idle"), [de, ue] = ct(null), [ae, we] = ct(!1), [Qe, Ee] = ct(!1), [Ze, Dt] = ct({ width: 842, height: 1264 }), Pe = V(i ?? null), l = a?.trim() || dt, k = V(null), C = V(null), Y = V(!1), oe = V({ width: 842, height: 1264 }), ye = V(null), le = V("sil"), _e = V(void 0), Se = V(/* @__PURE__ */ new Map()), mt = V(M), ie = V(b), De = V(h), et = V(_), ht = V(p), Ve = V(v), Re = V(O), Te = V(pi());
3034
- mt.current = M, ie.current = b, De.current = h, et.current = _, ht.current = p, Ve.current = v, Re.current = O, Fe(() => {
3035
- const y = Te.current;
3036
- return xn(y), () => xn(null);
3081
+ ), Y = O(!1), [ee, pe] = at("idle"), [me, le] = at(null), [fe, Ve] = at(!1), [xe, We] = at(!1), [ft, dn] = at({ width: 842, height: 1264 }), l = O(i ?? null), k = a?.trim() || lt, V = O(null), K = O(null), ie = O(!1), Pe = O({ width: 842, height: 1264 }), ae = O(null), Re = O("sil"), ge = O(void 0), Ze = O(/* @__PURE__ */ new Map()), et = O(I), re = O(b), tt = O(h), dt = O(_), mt = O(p), be = O(v), nt = O(N), ve = O(vi());
3082
+ et.current = I, re.current = b, tt.current = h, dt.current = _, mt.current = p, be.current = v, nt.current = N, Ce(() => {
3083
+ const g = ve.current;
3084
+ return Fn(g), () => Fn(null);
3037
3085
  }, []);
3038
- const We = ge(
3039
- (y) => {
3040
- const B = y?.ttsEngineId ?? o ?? C.current?.ttsEngineId ?? Ge, W = y?.voiceId ?? f ?? C.current?.voiceId ?? ut(B);
3041
- return { ttsEngineId: B, voiceId: W };
3086
+ const oe = Se(
3087
+ (g) => {
3088
+ const L = g?.ttsEngineId ?? o ?? K.current?.ttsEngineId ?? je, U = g?.voiceId ?? f ?? K.current?.voiceId ?? ot(L);
3089
+ return { ttsEngineId: L, voiceId: U };
3042
3090
  },
3043
3091
  [o, f]
3044
- ), Q = ge((y) => {
3045
- ue(y), De.current?.(y);
3046
- }, []), fe = ge(
3047
- (y) => {
3048
- ee(y), g?.(y);
3049
- },
3050
- [g]
3051
- ), K = V(
3052
- vn(fe, l, {
3053
- onLipsyncDebug: (y) => Ve.current?.(y)
3092
+ ), ce = Se((g) => {
3093
+ le(g), tt.current?.(g);
3094
+ }, []), he = Se(
3095
+ (g) => {
3096
+ pe(g), w?.(g);
3097
+ },
3098
+ [w]
3099
+ ), J = O(
3100
+ An(he, k, {
3101
+ onLipsyncDebug: (g) => be.current?.(g)
3054
3102
  })
3055
- ), J = V(null);
3056
- Fe(() => (K.current = vn(
3057
- fe,
3058
- l,
3103
+ ), Q = O(null);
3104
+ Ce(() => (J.current = An(
3105
+ he,
3106
+ k,
3059
3107
  {
3060
- onLipsyncDebug: (y) => Ve.current?.(y)
3108
+ onLipsyncDebug: (g) => be.current?.(g)
3061
3109
  }
3062
- ), i && K.current.setDeveloperToken(i), K.current.setTtsEngineId(o), () => {
3063
- K.current.stop();
3064
- const y = J.current;
3065
- y && (y.disconnect(), J.current = null);
3066
- }), [fe, i, o, l]);
3067
- const tt = ge(() => (J.current || (J.current = bs(
3068
- fe,
3110
+ ), i && J.current.setDeveloperToken(i), J.current.setTtsEngineId(o), () => {
3111
+ J.current.stop();
3112
+ const g = Q.current;
3113
+ g && (g.disconnect(), Q.current = null);
3114
+ }), [he, i, o, k]);
3115
+ const ht = Se(() => (Q.current || (Q.current = Cs(
3116
+ he,
3069
3117
  {
3070
3118
  speakingRate: d,
3071
- onUserTranscript: (y, B) => et.current?.(y, B),
3072
- onBotOutput: (y) => ht.current?.(y),
3073
- onLipsyncDebug: (y) => Ve.current?.(y)
3119
+ onUserTranscript: (g, L) => dt.current?.(g, L),
3120
+ onBotOutput: (g) => mt.current?.(g),
3121
+ onLipsyncDebug: (g) => be.current?.(g)
3074
3122
  }
3075
- ), J.current.setTtsEngineId(o)), J.current), [fe, d, o]), pe = ge(() => {
3076
- se.current != null && (clearTimeout(se.current), se.current = null);
3077
- }, []), Ae = ge(() => {
3078
- ce.current != null && (clearTimeout(ce.current), ce.current = null), q.current = !1;
3079
- }, []), fn = ge(() => {
3080
- !U.current || !A.current || (Ae(), q.current = !0, ce.current = setTimeout(() => {
3081
- if (ce.current = null, Ce(
3082
- K.current,
3083
- J.current
3123
+ ), Q.current.setTtsEngineId(o)), Q.current), [he, d, o]), ye = Se(() => {
3124
+ ue.current != null && (clearTimeout(ue.current), ue.current = null);
3125
+ }, []), Te = Se(() => {
3126
+ y.current != null && (clearTimeout(y.current), y.current = null), x.current = !1;
3127
+ }, []), mn = Se(() => {
3128
+ !Y.current || !B.current || (Te(), x.current = !0, y.current = setTimeout(() => {
3129
+ if (y.current = null, Me(
3130
+ J.current,
3131
+ Q.current
3084
3132
  ).isSpeaking()) {
3085
- q.current = !1;
3133
+ x.current = !1;
3086
3134
  return;
3087
3135
  }
3088
- if (w.current) {
3089
- q.current = !1;
3136
+ if (A.current) {
3137
+ x.current = !1;
3090
3138
  return;
3091
3139
  }
3092
- const y = A.current;
3093
- if (!y) {
3094
- q.current = !1;
3140
+ const g = B.current;
3141
+ if (!g) {
3142
+ x.current = !1;
3095
3143
  return;
3096
3144
  }
3097
- y.restart(), y.setActive(!0);
3098
- const B = y.getVideo();
3099
- let W = !1;
3145
+ g.restart(), g.setActive(!0);
3146
+ const L = g.getVideo();
3147
+ let U = !1;
3100
3148
  const G = () => {
3101
- W || (W = !0, B.removeEventListener("playing", H), q.current = !1);
3102
- }, H = () => {
3149
+ U || (U = !0, L.removeEventListener("playing", W), x.current = !1);
3150
+ }, W = () => {
3103
3151
  G();
3104
3152
  };
3105
- if (B.addEventListener("playing", H, { once: !0 }), B.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && !B.paused) {
3153
+ if (L.addEventListener("playing", W, { once: !0 }), L.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && !L.paused) {
3106
3154
  G();
3107
3155
  return;
3108
3156
  }
3109
3157
  setTimeout(G, 300);
3110
- }, wi));
3111
- }, [Ae]), nt = ge(() => {
3112
- if (pe(), ie.current?.("sil"), w.current = !1, !U.current || !A.current) {
3113
- q.current = !1;
3158
+ }, Ei));
3159
+ }, [Te]), He = Se(() => {
3160
+ if (ye(), re.current?.("sil"), A.current = !1, !Y.current || !B.current) {
3161
+ x.current = !1;
3114
3162
  return;
3115
3163
  }
3116
- fn();
3117
- }, [pe, fn]), Rt = ge(
3118
- async (y) => {
3119
- const B = X.current, W = P.current, G = S.current, H = () => {
3120
- y?.fallbackOnly || (w.current = !1);
3164
+ mn();
3165
+ }, [ye, mn]), Dt = Se(
3166
+ async (g) => {
3167
+ const L = ne.current, U = M.current, G = S.current, W = () => {
3168
+ g?.fallbackOnly || (A.current = !1);
3121
3169
  };
3122
- if (!W || !G || !ae) {
3123
- H();
3170
+ if (!U || !G || !fe) {
3171
+ W();
3124
3172
  return;
3125
3173
  }
3126
- if (Ce(
3127
- K.current,
3128
- J.current
3174
+ if (Me(
3175
+ J.current,
3176
+ Q.current
3129
3177
  ).isSpeaking()) {
3130
- H();
3178
+ W();
3131
3179
  return;
3132
3180
  }
3133
- if (y?.fallbackOnly) {
3134
- if (!w.current || (ve(I.current), await W.renderSilOnly(), B !== X.current) || Ce(
3135
- K.current,
3136
- J.current
3181
+ if (g?.fallbackOnly) {
3182
+ if (!A.current || (ke(X.current), await U.renderSilOnly(), L !== ne.current) || Me(
3183
+ J.current,
3184
+ Q.current
3137
3185
  ).isSpeaking())
3138
3186
  return;
3139
- nt();
3187
+ He();
3140
3188
  return;
3141
3189
  }
3142
- if (_i(I.current)) {
3143
- ve(I.current), nt();
3190
+ if (Ii(X.current)) {
3191
+ ke(X.current), He();
3144
3192
  return;
3145
3193
  }
3146
- const me = bi(
3147
- Ce(
3148
- K.current,
3149
- J.current
3194
+ if (Z.current && U.supportsBufferedPlayback && !g?.fallbackOnly) {
3195
+ if (ke(X.current), T.current?.reset(), await U.renderSilOnly(), L !== ne.current || Me(
3196
+ J.current,
3197
+ Q.current
3198
+ ).isSpeaking())
3199
+ return;
3200
+ He();
3201
+ return;
3202
+ }
3203
+ const _e = Ri(
3204
+ Me(
3205
+ J.current,
3206
+ Q.current
3150
3207
  ).getVisemeQueue(),
3151
- D.current,
3152
- L.current
3153
- ), ke = vi(
3154
- me,
3155
- ye.current
3208
+ $.current,
3209
+ P.current
3210
+ ), pt = Di(
3211
+ _e,
3212
+ ae.current
3156
3213
  );
3157
- if (ve(I.current), !ke) {
3158
- if (await W.renderSilOnly(), B !== X.current || Ce(
3159
- K.current,
3160
- J.current
3214
+ if (ke(X.current), !pt) {
3215
+ if (await U.renderSilOnly(), L !== ne.current || Me(
3216
+ J.current,
3217
+ Q.current
3161
3218
  ).isSpeaking())
3162
3219
  return;
3163
- nt();
3220
+ He();
3164
3221
  return;
3165
3222
  }
3166
3223
  try {
3167
- await W.renderViseme(G, "sil", {
3168
- from: me,
3169
- transitionDurationMs: sr
3224
+ await U.renderViseme(G, "sil", {
3225
+ from: _e,
3226
+ transitionDurationMs: lr
3170
3227
  });
3171
- } catch (st) {
3172
- if (st?.name !== "AbortError")
3173
- throw st;
3228
+ } catch (De) {
3229
+ if (De?.name !== "AbortError")
3230
+ throw De;
3174
3231
  }
3175
- if (B !== X.current) {
3176
- H();
3232
+ if (L !== ne.current) {
3233
+ W();
3177
3234
  return;
3178
3235
  }
3179
- if (Ce(
3180
- K.current,
3181
- J.current
3236
+ if (Me(
3237
+ J.current,
3238
+ Q.current
3182
3239
  ).isSpeaking()) {
3183
- H();
3240
+ W();
3184
3241
  return;
3185
3242
  }
3186
- nt();
3187
- },
3188
- [ae, nt]
3189
- ), rt = ge(() => {
3190
- if (R.current || w.current) return;
3191
- R.current = !0, w.current = !0, pe();
3192
- const y = X.current;
3193
- Rt().finally(() => {
3194
- R.current = !1;
3195
- }), se.current = setTimeout(() => {
3196
- se.current = null, y === X.current && Rt({ fallbackOnly: !0 });
3197
- }, Ei());
3198
- }, [pe, Rt]), dn = ge(async () => {
3199
- const y = Math.floor(Date.now() / 1e3);
3200
- if (k.current && y < k.current.expiresAt - 60)
3201
- return k.current.key;
3202
- const B = Pe.current ?? await Ft();
3203
- Pe.current = B, K.current.setDeveloperToken(B);
3204
- const W = await Nr(B, l);
3205
- return k.current = {
3206
- key: W.value,
3207
- expiresAt: W.expiresAt
3208
- }, W.value;
3209
- }, [l]);
3210
- Fe(() => {
3211
- i && (Pe.current = i, K.current.setDeveloperToken(i));
3212
- }, [i]), Fe(() => {
3213
- K.current.setTtsEngineId(o), J.current?.setTtsEngineId(o), C.current && (C.current.ttsEngineId = o);
3214
- }, [o]), Fe(() => {
3215
- const y = f ?? ut(o);
3216
- C.current && (C.current.voiceId = y);
3243
+ He();
3244
+ },
3245
+ [fe, He]
3246
+ ), rt = Se(() => {
3247
+ if (q.current || A.current) return;
3248
+ q.current = !0, A.current = !0, ye();
3249
+ const g = ne.current;
3250
+ Dt().finally(() => {
3251
+ q.current = !1;
3252
+ }), ue.current = setTimeout(() => {
3253
+ ue.current = null, g === ne.current && Dt({ fallbackOnly: !0 });
3254
+ }, xi());
3255
+ }, [ye, Dt]), hn = Se(async () => {
3256
+ const g = Math.floor(Date.now() / 1e3);
3257
+ if (V.current && g < V.current.expiresAt - 60)
3258
+ return V.current.key;
3259
+ const L = l.current ?? await Ft();
3260
+ l.current = L, J.current.setDeveloperToken(L);
3261
+ const U = await qr(L, k);
3262
+ return V.current = {
3263
+ key: U.value,
3264
+ expiresAt: U.expiresAt
3265
+ }, U.value;
3266
+ }, [k]);
3267
+ Ce(() => {
3268
+ i && (l.current = i, J.current.setDeveloperToken(i));
3269
+ }, [i]), Ce(() => {
3270
+ J.current.setTtsEngineId(o), Q.current?.setTtsEngineId(o), K.current && (K.current.ttsEngineId = o);
3271
+ }, [o]), Ce(() => {
3272
+ const g = f ?? ot(o);
3273
+ K.current && (K.current.voiceId = g);
3217
3274
  }, [f, o]);
3218
- const Be = gn(
3219
- () => Hr(n, r),
3275
+ const Be = _n(
3276
+ () => zr(n, r),
3220
3277
  [n, r]
3221
- ), cr = gn(() => s ? JSON.stringify(s) : Be ? `face:${Be}` : `id:${t ?? ""}`, [s, Be, t]);
3222
- return Fe(() => {
3278
+ ), hr = _n(() => s ? JSON.stringify(s) : Be ? `face:${Be}` : `id:${t ?? ""}`, [s, Be, t]);
3279
+ return Ce(() => {
3223
3280
  if (!t && !s && !Be) {
3224
- Q("AiTwin requires `assets`, `avatarId`/`faceId`, or `id`");
3281
+ ce("AiTwin requires `assets`, `avatarId`/`faceId`, or `id`");
3225
3282
  return;
3226
3283
  }
3227
- let y = !1;
3228
- Y.current = !1, we(!1), Ee(!1), ue(null), C.current = null, A.current = null, U.current = !1;
3229
- const B = S.current;
3230
- if (!B) return;
3231
- let W = null;
3284
+ let g = !1;
3285
+ ie.current = !1, Ve(!1), We(!1), le(null), K.current = null, B.current = null, Y.current = !1;
3286
+ const L = S.current;
3287
+ if (!L) return;
3288
+ let U = null;
3232
3289
  return (async () => {
3233
3290
  try {
3234
- let G, H, me, ke;
3291
+ let G, W, we, _e;
3235
3292
  if (s)
3236
- G = s, H = o, me = f ?? ut(H);
3293
+ G = s, W = o, we = f ?? ot(W);
3237
3294
  else if (Be)
3238
- ie.current?.("loadFace"), G = Sn(Be), H = o, me = f ?? ut(H);
3295
+ re.current?.("loadFace"), G = bn(Be), W = o, we = f ?? ot(W);
3239
3296
  else {
3240
- ie.current?.("getAiTwin");
3241
- const $ = await Nt(
3242
- xr(t),
3297
+ re.current?.("getAiTwin");
3298
+ const H = await Nt(
3299
+ Nr(t),
3243
3300
  3e4,
3244
3301
  "getAiTwin"
3245
3302
  );
3246
- if (y) return;
3247
- if (!$.faceId?.trim())
3303
+ if (g) return;
3304
+ if (!H.faceId?.trim())
3248
3305
  throw new Error("getAiTwin returned no faceId");
3249
- H = $.ttsEngineId, me = f ?? $.voiceId ?? ut(H), ke = $.knowledgeContextId?.trim() || void 0, G = Sn($.faceId);
3306
+ W = H.ttsEngineId, we = f ?? H.voiceId ?? ot(W), _e = H.knowledgeContextId?.trim() || void 0, G = bn(H.faceId);
3250
3307
  }
3251
- C.current = { ttsEngineId: H, voiceId: me, personaId: ke }, $r(G), A.current = Zs(), W = Bi(B, {
3252
- onStatus: ($) => {
3253
- y || ie.current?.($);
3308
+ K.current = { ttsEngineId: W, voiceId: we, personaId: _e }, Yr(G), B.current = fi(), U = Hi(L, {
3309
+ onStatus: (H) => {
3310
+ g || re.current?.(H);
3254
3311
  },
3255
- onError: ($) => {
3256
- y || Q($);
3312
+ onError: (H) => {
3313
+ g || ce(H);
3257
3314
  },
3258
- shouldAcceptLiveFrame: () => Ce(
3259
- K.current,
3260
- J.current
3261
- ).isSpeaking() && !w.current
3262
- }), P.current = W, T.current = Di(W), (($) => {
3263
- const te = () => $ && F && (P.current?.supportsBufferedPlayback ?? !1) && z > 0;
3264
- K.current.setLipsyncWarmupHold($, te), J.current?.setLipsyncWarmupHold($, te);
3265
- })(!0), ie.current?.("auth");
3266
- const it = Pe.current ?? i ?? await Nt(Ft(), 3e4, "getAuthToken");
3267
- if (y) return;
3268
- Pe.current = it, K.current.setDeveloperToken(it), K.current.setTtsEngineId(H);
3269
- const at = At() ? await dn() : void 0;
3270
- if (y) return;
3271
- ie.current?.("loadAssets");
3272
- const He = await Nt(
3273
- W.loadAssets(at),
3315
+ shouldAcceptLiveFrame: () => Me(
3316
+ J.current,
3317
+ Q.current
3318
+ ).isSpeaking() && !A.current
3319
+ }), M.current = U, T.current = Ni(U), ((H) => {
3320
+ const te = () => H && F && (M.current?.supportsBufferedPlayback ?? !1) && j > 0;
3321
+ J.current.setLipsyncWarmupHold(H, te), Q.current?.setLipsyncWarmupHold(H, te);
3322
+ })(!0), re.current?.("auth");
3323
+ const De = l.current ?? i ?? await Nt(Ft(), 3e4, "getAuthToken");
3324
+ if (g) return;
3325
+ l.current = De, J.current.setDeveloperToken(De), J.current.setTtsEngineId(W);
3326
+ const st = It() ? await hn() : void 0;
3327
+ if (g) return;
3328
+ re.current?.("loadAssets");
3329
+ const $e = await Nt(
3330
+ U.loadAssets(st),
3274
3331
  18e4,
3275
3332
  "load face assets"
3276
3333
  );
3277
- if (!y && He.width > 0 && He.height > 0 && (oe.current = He, Dt(He)), y) return;
3278
- ye.current = W.getVisemeAtlasPaths(), Y.current = !0, we(!0), mt.current?.(), W.renderSilOnly().then(() => {
3279
- y || ie.current?.("sil");
3334
+ if (!g && $e.width > 0 && $e.height > 0 && (Pe.current = $e, dn($e)), g) return;
3335
+ ae.current = U.getVisemeAtlasPaths(), ie.current = !0, Ve(!0), et.current?.(), U.renderSilOnly().then(() => {
3336
+ g || re.current?.("sil");
3280
3337
  }), (async () => {
3281
3338
  try {
3282
- const $ = await Ds(at);
3283
- if (await A.current.load($), y) return;
3284
- U.current = !0, Ee(!0), ve(I.current), A.current.setActive(!0);
3285
- const te = A.current.getVideo();
3286
- await Rn(
3287
- B,
3339
+ const H = await Hs(st);
3340
+ if (await B.current.load(H), g) return;
3341
+ Y.current = !0, We(!0), ke(X.current), B.current.setActive(!0);
3342
+ const te = B.current.getVideo();
3343
+ await Cn(
3344
+ L,
3288
3345
  te,
3289
3346
  null,
3290
3347
  void 0,
3291
- oe.current
3292
- ), y || ie.current?.("idle");
3293
- } catch ($) {
3294
- if (y) return;
3295
- U.current = !1, Ee(!1), console.warn("[AiTwin] Idle video unavailable:", $);
3348
+ Pe.current
3349
+ ), g || re.current?.("idle");
3350
+ } catch (H) {
3351
+ if (g) return;
3352
+ Y.current = !1, We(!1), console.warn("[AiTwin] Idle video unavailable:", H);
3296
3353
  }
3297
3354
  })();
3298
3355
  } catch (G) {
3299
- if (y) return;
3356
+ if (g) return;
3300
3357
  if (G instanceof Ut) {
3301
- Q(G.message);
3358
+ ce(G.message);
3302
3359
  return;
3303
3360
  }
3304
- Q(G instanceof Error ? G.message : "Failed to load AI twin");
3361
+ ce(G instanceof Error ? G.message : "Failed to load AI twin");
3305
3362
  }
3306
3363
  })(), () => {
3307
- y = !0, A.current?.dispose(), W?.dispose(), P.current = null;
3364
+ g = !0, B.current?.dispose(), U?.dispose(), M.current = null;
3308
3365
  };
3309
- }, [cr, s, Be, t, o, f, i, dn, Q]), Fe(() => {
3310
- const y = { current: !1 }, B = () => {
3311
- const W = S.current, G = P.current, H = Ce(
3312
- K.current,
3313
- J.current
3366
+ }, [hr, s, Be, t, o, f, i, hn, ce]), Ce(() => {
3367
+ const g = { current: !1 }, L = () => {
3368
+ const U = S.current, G = M.current, W = Me(
3369
+ J.current,
3370
+ Q.current
3314
3371
  );
3315
- if (!W || !G || !ae) {
3316
- Z.current = requestAnimationFrame(B);
3372
+ if (!U || !G || !fe) {
3373
+ se.current = requestAnimationFrame(L);
3317
3374
  return;
3318
3375
  }
3319
- const me = H.isSpeaking(), ke = H.isWarmingUp(), st = me && !N.current, it = N.current && !me;
3320
- st && (pe(), Ae(), w.current = !1, R.current = !1, ve(I.current), (T.current?.getBufferedFrameCount() ?? 0) === 0 && T.current?.reset(), Te.current.reset(), _e.current = void 0, he.current = 0, L.current = "sil", D.current = 0, A.current?.setActive(!1)), it && (ne.current.reset(), A.current?.setActive(!1), rt());
3321
- const at = it || w.current || q.current, He = F && G.supportsBufferedPlayback;
3322
- if (ke && He && T.current) {
3323
- U.current && A.current?.setActive(!1);
3324
- const $ = H.getVisemeQueue();
3325
- $.length > 0 && j.current === 0 && (j.current = performance.now()), ne.current.advance(performance.now());
3326
- const te = ne.current.getExpressionPath(), be = ne.current.getDrawKey(), Ie = {
3327
- queue: $,
3376
+ const we = W.isSpeaking(), _e = W.isWarmingUp(), pt = we && !C.current, De = C.current && !we;
3377
+ pt && (ye(), Te(), A.current = !1, q.current = !1, ke(X.current), (T.current?.getBufferedFrameCount() ?? 0) === 0 && T.current?.reset(), ve.current.reset(), ge.current = void 0, de.current = 0, P.current = "sil", $.current = 0, B.current?.setActive(!1)), De && (z.current.reset(), B.current?.setActive(!1), rt());
3378
+ const st = De || A.current || x.current, $e = F && G.supportsBufferedPlayback;
3379
+ if (_e && $e && T.current) {
3380
+ Y.current && B.current?.setActive(!1);
3381
+ const H = W.getVisemeQueue();
3382
+ H.length > 0 && D.current === 0 && (D.current = performance.now()), z.current.advance(performance.now());
3383
+ const te = z.current.getExpressionPath(), Ee = z.current.getDrawKey(), Ae = {
3384
+ queue: H,
3328
3385
  elapsedMs: 0,
3329
3386
  expressionPath: te,
3330
- expressionDrawKey: be,
3331
- availablePaths: ye.current
3387
+ expressionDrawKey: Ee,
3388
+ availablePaths: ae.current
3332
3389
  };
3333
- T.current.warmupTick(Ie);
3334
- const Me = T.current.isWarmupReady(
3335
- Ie,
3336
- z
3337
- ), $e = j.current > 0 && performance.now() - j.current > Ar;
3338
- ie.current?.(
3339
- Me ? "buffer ready" : "buffering lipsync…"
3340
- ), (Me || $e) && (j.current = 0, H.releaseWarmupHold()), N.current = H.isSpeaking(), Z.current = requestAnimationFrame(B);
3390
+ T.current.warmupTick(Ae);
3391
+ const Ie = T.current.isWarmupReady(
3392
+ Ae,
3393
+ j
3394
+ ), qe = D.current > 0 && performance.now() - D.current > xr;
3395
+ re.current?.(
3396
+ Ie ? "buffer ready" : "buffering lipsync…"
3397
+ ), (Ie || qe) && (D.current = 0, W.releaseWarmupHold()), C.current = W.isSpeaking(), se.current = requestAnimationFrame(L);
3341
3398
  return;
3342
3399
  }
3343
- if (me) {
3344
- U.current && A.current?.setActive(!1);
3345
- const $ = H.getVisemeQueue(), te = H.getPlaybackElapsedMs();
3346
- ds($, te), D.current = te;
3347
- const be = $.length > 0 ? It($, te) : "sil";
3348
- be !== "sil" && (L.current = be);
3349
- const Ie = ri(
3400
+ if (we) {
3401
+ Y.current && B.current?.setActive(!1);
3402
+ const H = W.getVisemeQueue(), te = W.getPlaybackElapsedMs();
3403
+ Es(H, te), $.current = te;
3404
+ const Ee = H.length > 0 ? Mt(H, te) : "sil";
3405
+ Ee !== "sil" && (P.current = Ee);
3406
+ const Ae = pi(
3350
3407
  te,
3351
- H.getSentenceEmotions(),
3352
- H.getWordQueue(),
3353
- H.getStreamMood()
3408
+ W.getSentenceEmotions(),
3409
+ W.getWordQueue(),
3410
+ W.getStreamMood()
3354
3411
  );
3355
- ne.current.setTargetEmotion(Ie), ne.current.setTargetEyebrow(
3356
- Xs(
3412
+ z.current.setTargetEmotion(Ae), z.current.setTargetEyebrow(
3413
+ ui(
3357
3414
  te,
3358
- H.getWordQueue(),
3359
- rn(Ie)
3415
+ W.getWordQueue(),
3416
+ sn(Ae)
3360
3417
  )
3361
- ), ne.current.advance(performance.now());
3362
- const Me = ne.current.getExpressionPath(), $e = ne.current.getDrawKey(), ur = (qe, xt, ot, Ct) => {
3363
- const pn = {
3364
- label: le.current,
3418
+ ), z.current.advance(performance.now());
3419
+ const Ie = z.current.getExpressionPath(), qe = z.current.getDrawKey(), pr = (Ke, xt, it, Bt) => {
3420
+ const gn = {
3421
+ label: Re.current,
3365
3422
  elapsedMs: te,
3366
3423
  mouthPath: xt.mouthPath,
3367
- transitionMeta: _e.current
3424
+ transitionMeta: ge.current
3368
3425
  };
3369
- Se.current.set(ot, pn), T.current?.markExternallyDisplayed(
3370
- ot,
3371
- Me
3372
- ), G.drawLiveFrame(qe, xt, ot, (pt) => {
3373
- const fr = Se.current.get(pt) ?? pn;
3374
- Se.current.delete(pt), Te.current.onDrawDisplayed(
3375
- fr,
3376
- $
3426
+ Ze.current.set(it, gn), T.current?.markExternallyDisplayed(
3427
+ it,
3428
+ Ie
3429
+ ), G.drawLiveFrame(Ke, xt, it, (wt) => {
3430
+ const gr = Ze.current.get(wt) ?? gn;
3431
+ Ze.current.delete(wt), ve.current.onDrawDisplayed(
3432
+ gr,
3433
+ H
3377
3434
  ), T.current?.markExternallyDisplayed(
3378
- pt,
3379
- Me
3380
- ), Ct?.(pt);
3435
+ wt,
3436
+ Ie
3437
+ ), Bt?.(wt);
3381
3438
  });
3382
- }, mn = (qe) => {
3383
- le.current = qe.label, _e.current = qe.transitionMeta, ie.current?.(qe.label);
3384
- }, lr = F && G.supportsBufferedPlayback;
3385
- let hn = !1;
3386
- lr && T.current && (hn = T.current.tick({
3387
- canvas: W,
3388
- queue: $,
3439
+ }, pn = (Ke) => {
3440
+ Re.current = Ke.label, ge.current = Ke.transitionMeta, re.current?.(Ke.label);
3441
+ }, wr = F && G.supportsBufferedPlayback;
3442
+ let wn = !1;
3443
+ wr && T.current && (wn = T.current.tick({
3444
+ canvas: U,
3445
+ queue: H,
3389
3446
  elapsedMs: te,
3390
- expressionPath: Me,
3391
- expressionDrawKey: $e,
3392
- playbackState: I.current,
3393
- availablePaths: ye.current,
3394
- onStatus: mn,
3395
- onDisplayed: (xt, ot, Ct) => {
3396
- Te.current.onDrawDisplayed(
3447
+ expressionPath: Ie,
3448
+ expressionDrawKey: qe,
3449
+ playbackState: X.current,
3450
+ availablePaths: ae.current,
3451
+ onStatus: pn,
3452
+ onDisplayed: (xt, it, Bt) => {
3453
+ ve.current.onDrawDisplayed(
3397
3454
  {
3398
- label: Ct,
3455
+ label: Bt,
3399
3456
  elapsedMs: te,
3400
- mouthPath: ot,
3401
- transitionMeta: _e.current
3457
+ mouthPath: it,
3458
+ transitionMeta: ge.current
3402
3459
  },
3403
- $
3460
+ H
3404
3461
  );
3405
3462
  }
3406
- }).usedBuffer), hn || Ti(
3407
- W,
3408
- $,
3463
+ }).usedBuffer), wn || Bi(
3464
+ U,
3465
+ H,
3409
3466
  te,
3410
- Me,
3411
- $e,
3412
- I.current,
3413
- ur,
3414
- mn,
3415
- ye.current
3416
- ), Te.current.onRafTickSpeaking(), Re.current?.(
3417
- Te.current.snapshot($, te)
3467
+ Ie,
3468
+ qe,
3469
+ X.current,
3470
+ pr,
3471
+ pn,
3472
+ ae.current
3473
+ ), ve.current.onRafTickSpeaking(), nt.current?.(
3474
+ ve.current.snapshot(H, te)
3418
3475
  );
3419
- } else if (!at && U.current && A.current) {
3420
- const $ = A.current, te = $.getVideo();
3421
- te.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA ? ($.setActive(!0), y.current || (y.current = !0, Rn(
3422
- W,
3476
+ } else if (!st && Y.current && B.current) {
3477
+ const H = B.current, te = H.getVideo();
3478
+ te.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA ? (H.setActive(!0), g.current || (g.current = !0, Cn(
3479
+ U,
3423
3480
  te,
3424
3481
  null,
3425
3482
  void 0,
3426
- oe.current,
3427
- () => !H.isSpeaking() && !w.current && !q.current
3428
- ).then(() => ie.current?.("idle")).finally(() => {
3429
- y.current = !1;
3430
- }))) : Bn(
3431
- W,
3483
+ Pe.current,
3484
+ () => !W.isSpeaking() && !A.current && !x.current
3485
+ ).then(() => re.current?.("idle")).finally(() => {
3486
+ g.current = !1;
3487
+ }))) : On(
3488
+ U,
3432
3489
  null,
3433
3490
  "__sil__",
3434
- I.current,
3435
- (be, Ie, Me, $e) => G.drawLiveFrame(be, Ie, Me, $e),
3436
- (be) => ie.current?.(be)
3491
+ X.current,
3492
+ (Ee, Ae, Ie, qe) => G.drawLiveFrame(Ee, Ae, Ie, qe),
3493
+ (Ee) => re.current?.(Ee)
3437
3494
  );
3438
- } else at || Bn(
3439
- W,
3495
+ } else st || On(
3496
+ U,
3440
3497
  null,
3441
3498
  "__sil__",
3442
- I.current,
3443
- ($, te, be, Ie) => G.drawLiveFrame($, te, be, Ie),
3444
- ($) => ie.current?.($)
3499
+ X.current,
3500
+ (H, te, Ee, Ae) => G.drawLiveFrame(H, te, Ee, Ae),
3501
+ (H) => re.current?.(H)
3445
3502
  );
3446
- N.current = me, Z.current = requestAnimationFrame(B);
3503
+ C.current = we, se.current = requestAnimationFrame(L);
3447
3504
  };
3448
- return Z.current = requestAnimationFrame(B), () => {
3449
- cancelAnimationFrame(Z.current), pe(), Ae(), K.current.stop(), J.current?.stop();
3505
+ return se.current = requestAnimationFrame(L), () => {
3506
+ cancelAnimationFrame(se.current), ye(), Te(), J.current.stop(), Q.current?.stop();
3450
3507
  };
3451
3508
  }, [
3452
- ae,
3453
- Qe,
3509
+ fe,
3510
+ xe,
3454
3511
  rt,
3455
- pe,
3456
- Ae
3457
- ]), hr(
3458
- x,
3512
+ ye,
3513
+ Te
3514
+ ]), Sr(
3515
+ R,
3459
3516
  () => ({
3460
- speakText: async (y, B) => {
3461
- const W = y.trim();
3462
- if (!W) return;
3463
- const G = P.current;
3464
- if (!G || !Y.current && !ae)
3517
+ speakText: async (g, L) => {
3518
+ const U = g.trim();
3519
+ if (!U) return;
3520
+ const G = M.current;
3521
+ if (!G || !ie.current && !fe)
3465
3522
  throw new Error("AI twin is not ready");
3466
- const { ttsEngineId: H, voiceId: me } = We(B);
3467
- ue(null), X.current += 1, pe(), Ae(), w.current = !1, R.current = !1, L.current = "sil", D.current = 0, ve(I.current), ne.current.reset(), N.current = !1;
3523
+ const { ttsEngineId: W, voiceId: we } = oe(L);
3524
+ le(null), ne.current += 1, ye(), Te(), A.current = !1, q.current = !1, P.current = "sil", $.current = 0, ke(X.current), z.current.reset(), C.current = !1;
3468
3525
  try {
3469
- U.current ? (A.current?.setActive(!1), A.current?.restart(), ve(I.current)) : await G.renderSilOnly(), await K.current.speak(W, {
3470
- voiceId: me,
3471
- speakingRate: B?.speakingRate ?? d,
3472
- ttsEngineId: H
3526
+ Y.current ? (B.current?.setActive(!1), B.current?.restart(), ke(X.current)) : await G.renderSilOnly(), await J.current.speak(U, {
3527
+ voiceId: we,
3528
+ speakingRate: L?.speakingRate ?? d,
3529
+ ttsEngineId: W
3473
3530
  });
3474
- } catch (ke) {
3475
- throw Q(ke instanceof Error ? ke.message : "TTS failed"), ke;
3531
+ } catch (_e) {
3532
+ throw ce(_e instanceof Error ? _e.message : "TTS failed"), _e;
3476
3533
  }
3477
3534
  },
3478
3535
  stop: () => {
3479
- X.current += 1, pe(), Ae(), w.current = !1, R.current = !1, K.current.stop(), J.current?.stop(), ve(I.current), T.current?.reset(), j.current = 0, N.current = !1, rt();
3536
+ ne.current += 1, ye(), Te(), A.current = !1, q.current = !1, J.current.stop(), Q.current?.stop(), ke(X.current), T.current?.reset(), D.current = 0, C.current = !1, rt();
3480
3537
  },
3481
- setTtsEngineId: (y) => {
3482
- K.current.setTtsEngineId(y), J.current?.setTtsEngineId(y), C.current && (C.current.ttsEngineId = y);
3538
+ setTtsEngineId: (g) => {
3539
+ J.current.setTtsEngineId(g), Q.current?.setTtsEngineId(g), K.current && (K.current.ttsEngineId = g);
3483
3540
  },
3484
- renderViseme: async (y, B) => {
3485
- const W = P.current, G = S.current;
3486
- if (!W || !G || !Y.current)
3541
+ renderViseme: async (g, L) => {
3542
+ const U = M.current, G = S.current;
3543
+ if (!U || !G || !ie.current)
3487
3544
  throw new Error("AI twin is not ready");
3488
- await W.renderViseme(G, y, {
3489
- from: B?.from,
3490
- transitionDurationMs: B?.transitionDurationMs,
3491
- gapMs: B?.gapMs,
3492
- onStatus: (H) => ie.current?.(H)
3545
+ await U.renderViseme(G, g, {
3546
+ from: L?.from,
3547
+ transitionDurationMs: L?.transitionDurationMs,
3548
+ gapMs: L?.gapMs,
3549
+ onStatus: (W) => re.current?.(W)
3493
3550
  });
3494
3551
  },
3495
- isReady: () => Y.current,
3496
- getStatus: () => re,
3497
- connect: async (y = {}) => {
3498
- ue(null);
3552
+ isReady: () => ie.current,
3553
+ getStatus: () => ee,
3554
+ connect: async (g = {}) => {
3555
+ le(null);
3499
3556
  try {
3500
- let B = y.authToken?.trim();
3501
- B || (B = Pe.current ?? i ?? await Ft(), Pe.current = B);
3502
- const W = y.voiceId?.trim() ?? f ?? C.current?.voiceId, G = y.personaId?.trim() ?? C.current?.personaId, H = tt();
3503
- await H.unlockAudio(), await H.connect({
3504
- ...y,
3505
- apiBase: y.apiBase?.trim() || l,
3506
- authToken: B,
3507
- voiceId: W,
3557
+ let L = g.authToken?.trim();
3558
+ L || (L = l.current ?? i ?? await Ft(), l.current = L);
3559
+ const U = g.voiceId?.trim() ?? f ?? K.current?.voiceId, G = g.personaId?.trim() ?? K.current?.personaId, W = ht();
3560
+ await W.unlockAudio(), await W.connect({
3561
+ ...g,
3562
+ apiBase: g.apiBase?.trim() || k,
3563
+ authToken: L,
3564
+ voiceId: U,
3508
3565
  personaId: G,
3509
- speakingRate: y.speakingRate ?? d
3566
+ speakingRate: g.speakingRate ?? d
3510
3567
  });
3511
- } catch (B) {
3512
- throw Q(
3513
- B instanceof Error ? B.message : "Voice connect failed"
3514
- ), B;
3568
+ } catch (L) {
3569
+ throw ce(
3570
+ L instanceof Error ? L.message : "Voice connect failed"
3571
+ ), L;
3515
3572
  }
3516
3573
  },
3517
3574
  disconnect: async () => {
3518
- const y = J.current;
3519
- y && (X.current += 1, pe(), Ae(), w.current = !1, R.current = !1, ve(I.current), T.current?.reset(), j.current = 0, N.current = !1, await y.disconnect(), rt());
3575
+ const g = Q.current;
3576
+ g && (ne.current += 1, ye(), Te(), A.current = !1, q.current = !1, ke(X.current), T.current?.reset(), D.current = 0, C.current = !1, await g.disconnect(), rt());
3520
3577
  },
3521
- isConnected: () => J.current?.isConnected() ?? !1,
3522
- getVoiceStream: () => J.current?.getVoiceStream() ?? K.current?.getVoiceStream() ?? null
3578
+ isConnected: () => Q.current?.isConnected() ?? !1,
3579
+ getVoiceStream: () => Q.current?.getVoiceStream() ?? J.current?.getVoiceStream() ?? null
3523
3580
  }),
3524
3581
  [
3525
- ae,
3582
+ fe,
3526
3583
  d,
3527
3584
  i,
3528
3585
  f,
3529
- pe,
3530
- Ae,
3586
+ ye,
3587
+ Te,
3531
3588
  rt,
3532
- Q,
3533
- We,
3534
- tt,
3535
- re
3589
+ ce,
3590
+ oe,
3591
+ ht,
3592
+ ee
3536
3593
  ]
3537
- ), /* @__PURE__ */ dr("div", { className: u, style: { ...Li, ...c }, children: [
3538
- /* @__PURE__ */ wn(
3594
+ ), /* @__PURE__ */ yr("div", { className: u, style: { ...qi, ...c }, children: [
3595
+ /* @__PURE__ */ yn(
3539
3596
  "canvas",
3540
3597
  {
3541
3598
  ref: S,
3542
- width: Ze.width,
3543
- height: Ze.height,
3544
- style: { ...Oi, ...m },
3599
+ width: ft.width,
3600
+ height: ft.height,
3601
+ style: { ...Ki, ...m },
3545
3602
  "aria-label": t ? `AI twin ${t}` : "AI twin face"
3546
3603
  }
3547
3604
  ),
3548
- E && de ? /* @__PURE__ */ wn("div", { style: Ni, children: de }) : null
3605
+ E && me ? /* @__PURE__ */ yn("div", { style: Gi, children: me }) : null
3549
3606
  ] });
3550
- }), Ui = "thumbnail.webp";
3551
- function Yi(e, t = jt) {
3607
+ }), ji = "thumbnail.webp";
3608
+ function ra(e, t = jt) {
3552
3609
  const n = e.trim();
3553
3610
  if (!n)
3554
3611
  throw new Error("faceId is required");
3555
- return `${t.replace(/\/$/, "")}/${n}/${Ui}`;
3612
+ return `${t.replace(/\/$/, "")}/${n}/${ji}`;
3556
3613
  }
3557
- const Vi = /* @__PURE__ */ new Set([".png", ".jpg", ".jpeg", ".webp"]);
3558
- function Wi(e = ".png") {
3614
+ const zi = /* @__PURE__ */ new Set([".png", ".jpg", ".jpeg", ".webp"]);
3615
+ function Yi(e = ".png") {
3559
3616
  const t = e.toLowerCase();
3560
- return Vi.has(t) ? `source${t}` : "source.png";
3617
+ return zi.has(t) ? `source${t}` : "source.png";
3561
3618
  }
3562
- function Ji(e, t = jt, n = ".png") {
3619
+ function sa(e, t = jt, n = ".png") {
3563
3620
  const r = e.trim();
3564
3621
  if (!r)
3565
3622
  throw new Error("faceId is required");
3566
- return `${t.replace(/\/$/, "")}/${r}/${Wi(n)}`;
3623
+ return `${t.replace(/\/$/, "")}/${r}/${Yi(n)}`;
3567
3624
  }
3568
3625
  export {
3569
- Ui as AI_TWIN_THUMBNAIL_FILENAME,
3570
- zi as AiTwin,
3626
+ ji as AI_TWIN_THUMBNAIL_FILENAME,
3627
+ na as AiTwin,
3571
3628
  Ut as AiTwinNotFoundError,
3572
- Ge as DEFAULT_TTS_ENGINE_ID,
3573
- zn as MIC_SAMPLE_RATE,
3574
- Jr as OCULUS_VISEME_IDS,
3575
- Yn as PLAYBACK_SAMPLE_RATE,
3576
- Qt as SPEAKING_RATE_MAX,
3577
- Xt as SPEAKING_RATE_MIN,
3578
- Je as TTS_ENGINE_CARTESIA,
3579
- Un as TTS_ENGINE_GOOGLE,
3580
- zt as TTS_ENGINE_INWORLD,
3581
- ji as VISEME_IDS,
3582
- jn as VISEME_TEST_CARTESIA_VOICE_ID,
3583
- Mt as VISEME_TEST_SPEAKING_RATE,
3584
- Gn as VISEME_TEST_VOICE_ID,
3585
- Wi as aiTwinSourceFilename,
3586
- _s as buildVoiceWebSocketUrl,
3587
- vn as createAvatarTtsLipsyncController,
3588
- bs as createRealtimeVoiceLipsyncController,
3589
- Sn as faceAssetUrls,
3590
- xr as fetchAiTwin,
3629
+ je as DEFAULT_TTS_ENGINE_ID,
3630
+ tr as MIC_SAMPLE_RATE,
3631
+ ns as OCULUS_VISEME_IDS,
3632
+ nr as PLAYBACK_SAMPLE_RATE,
3633
+ Zt as SPEAKING_RATE_MAX,
3634
+ Qt as SPEAKING_RATE_MIN,
3635
+ Xe as TTS_ENGINE_CARTESIA,
3636
+ Hn as TTS_ENGINE_GOOGLE,
3637
+ Yt as TTS_ENGINE_INWORLD,
3638
+ ta as VISEME_IDS,
3639
+ er as VISEME_TEST_CARTESIA_VOICE_ID,
3640
+ Pt as VISEME_TEST_SPEAKING_RATE,
3641
+ Zn as VISEME_TEST_VOICE_ID,
3642
+ Yi as aiTwinSourceFilename,
3643
+ Ds as buildVoiceWebSocketUrl,
3644
+ An as createAvatarTtsLipsyncController,
3645
+ Cs as createRealtimeVoiceLipsyncController,
3646
+ bn as faceAssetUrls,
3647
+ Nr as fetchAiTwin,
3591
3648
  Ft as fetchDevAuthToken,
3592
- Ji as getAiTwinSourceUrl,
3593
- Yi as getAiTwinThumbnailUrl,
3594
- Qr as normalizeOculusViseme,
3595
- Hr as resolveDirectFaceId,
3596
- It as resolveVisemeAtTime,
3597
- Gi as resolveWordAtTime,
3598
- $r as setActiveTwinAssets
3649
+ sa as getAiTwinSourceUrl,
3650
+ ra as getAiTwinThumbnailUrl,
3651
+ ss as normalizeOculusViseme,
3652
+ zr as resolveDirectFaceId,
3653
+ Mt as resolveVisemeAtTime,
3654
+ ea as resolveWordAtTime,
3655
+ Yr as setActiveTwinAssets
3599
3656
  };