@streamoji/aitwin 0.3.1 → 0.3.4

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