@streamoji/aitwin 0.2.8 → 0.3.0

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