@zeroweight/react 0.2.28 → 0.2.30

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.
@@ -1,189 +1,194 @@
1
- import { useMemo as ve, useRef as A, useState as v, useCallback as w, useEffect as y } from "react";
2
- import { ZeroWeightRenderer as ye, ActionQueue as be, VoiceActivityDetector as Se } from "@zeroweight/renderer";
3
- import { jsxs as b, jsx as r, Fragment as P } from "react/jsx-runtime";
4
- import { useVoiceAssistant as xe, useLocalParticipant as we, useIsSpeaking as Ae, useDataChannel as ke, RoomAudioRenderer as Te, LiveKitRoom as Ie } from "@livekit/components-react";
1
+ import { useMemo as be, useRef as m, useState as x, useCallback as S, useEffect as h } from "react";
2
+ import { ZeroWeightRenderer as Se, ActionQueue as xe, VoiceActivityDetector as ke } from "@zeroweight/renderer";
3
+ import { jsxs as k, jsx as r, Fragment as j } from "react/jsx-runtime";
4
+ import { useVoiceAssistant as we, useLocalParticipant as Ae, useIsSpeaking as Te, useDataChannel as Ie, RoomAudioRenderer as Re, LiveKitRoom as Me } from "@livekit/components-react";
5
5
  import "@livekit/components-styles";
6
- import { Loader2 as V, MicOff as Re, Mic as Ce, Power as Me, Activity as Ee } from "lucide-react";
7
- const Le = 3e4, De = 120, ze = "wss://prod-project-pazuyq69.livekit.cloud", Y = "https://api.zeroweight.ai";
8
- function Pe(S) {
6
+ import { Loader2 as q, MicOff as Ce, Mic as Ee, Power as Le, Activity as De } from "lucide-react";
7
+ const ze = 3e4, Pe = 120, Fe = "wss://prod-project-pazuyq69.livekit.cloud", ne = "https://api.zeroweight.ai";
8
+ function Ue(v) {
9
9
  const {
10
10
  avatarId: a,
11
- apiKey: t = null,
12
- api: f,
13
- livekitUrl: g = ze,
14
- sessionDuration: d = De,
15
- inactivityTimeout: x = Le
16
- } = S, M = g, u = ve(() => {
17
- if (f) return f;
18
- const e = t ? { "X-ZW-Api-Key": t } : void 0;
11
+ apiKey: n = null,
12
+ api: u,
13
+ turnOffMicWhenAISpeaking: d = !0,
14
+ livekitUrl: w = Fe,
15
+ sessionDuration: p = Pe,
16
+ inactivityTimeout: R = ze
17
+ } = v, L = w, M = be(() => {
18
+ if (u) return u;
19
+ const e = n ? { "X-ZW-Api-Key": n } : void 0;
19
20
  return {
20
- getBundle: async (p) => {
21
- const k = await fetch(
22
- `${Y}/avatars/bundle/${encodeURIComponent(
23
- p
21
+ getBundle: async (f) => {
22
+ const T = await fetch(
23
+ `${ne}/avatars/bundle/${encodeURIComponent(
24
+ f
24
25
  )}`,
25
26
  { headers: e }
26
27
  );
27
- if (!k.ok)
28
+ if (!T.ok)
28
29
  throw new Error(
29
- `Failed to fetch avatar bundle (${k.status} ${k.statusText})`
30
+ `Failed to fetch avatar bundle (${T.status} ${T.statusText})`
30
31
  );
31
- return k.json();
32
+ return T.json();
32
33
  },
33
- getLiveKitToken: async (p) => {
34
- const k = new URLSearchParams({
35
- avatar_id: p,
34
+ getLiveKitToken: async (f) => {
35
+ const T = new URLSearchParams({
36
+ avatar_id: f,
36
37
  name: "anonymuous"
37
- }), l = await fetch(
38
- `${Y}/livekit/token?${k.toString()}`,
38
+ }), c = await fetch(
39
+ `${ne}/livekit/token?${T.toString()}`,
39
40
  { headers: e }
40
41
  );
41
- if (!l.ok)
42
+ if (!c.ok)
42
43
  throw new Error(
43
- `Failed to fetch LiveKit token (${l.status} ${l.statusText})`
44
+ `Failed to fetch LiveKit token (${c.status} ${c.statusText})`
44
45
  );
45
- return l.json();
46
+ return c.json();
46
47
  }
47
48
  };
48
- }, [f, t]), h = A(null), m = A(null), i = A(null), c = A(null), [R, o] = v("idle"), [n, s] = v(null), [E, L] = v(
49
+ }, [u, n]), s = m(null), y = m(null), i = m(null), b = m(null), [g, C] = x("idle"), [o, z] = x(null), [A, t] = x(
49
50
  /* @__PURE__ */ new Set(["listening"])
50
- ), [D, F] = v({
51
+ ), [l, U] = x({
51
52
  listening: { kind: "looped" },
52
53
  speaking: { kind: "looped" }
53
- }), [te, B] = v(!1), [ne, H] = v(null), [_, U] = v(!1), [j, Q] = v(!1), [re, $] = v(d), I = A(null), K = A(() => {
54
- }), [ie, oe] = v(!1), [ae, q] = v(0.8), C = A(null), O = R === "ready", J = w(() => {
55
- const e = h.current;
54
+ }), [F, P] = x(!1), [$, J] = x(null), [K, O] = x(!1), [W, Z] = x(!1), [ae, N] = x(p), E = m(null), V = m(() => {
55
+ }), [oe, X] = x(!1), [se, Y] = x(0.8), D = m(null), B = g === "ready", G = S(() => {
56
+ const e = s.current;
56
57
  if (!e) return null;
57
- let p = e.querySelector("canvas");
58
- return p || (p = document.createElement("canvas"), p.style.width = "100%", p.style.height = "100%", p.style.display = "block", e.appendChild(p)), m.current = p, p;
58
+ let f = e.querySelector("canvas");
59
+ return f || (f = document.createElement("canvas"), f.style.width = "100%", f.style.height = "100%", f.style.display = "block", e.appendChild(f)), y.current = f, f;
59
60
  }, []);
60
- y(() => {
61
+ h(() => {
61
62
  let e = !1;
62
63
  return (async () => {
63
- const k = J();
64
- if (!k) return;
65
- const l = new ye();
66
- i.current = l;
67
- const W = new be((T, z) => {
68
- l.play(T, z);
64
+ const T = G();
65
+ if (!T) return;
66
+ const c = new Se();
67
+ i.current = c;
68
+ const Q = new xe((I, _) => {
69
+ c.play(I, _);
69
70
  });
70
- c.current = W, l.on("stateChanged", (T) => {
71
- e || o(T);
72
- }), l.on("dimensions", (T, z) => {
73
- e || s({ width: T, height: z });
74
- }), l.on("actionLoaded", (T) => {
75
- e || L((z) => {
76
- const X = new Set(z);
77
- return X.add(T), X;
71
+ b.current = Q, c.on("stateChanged", (I) => {
72
+ e || C(I);
73
+ }), c.on("dimensions", (I, _) => {
74
+ e || z({ width: I, height: _ });
75
+ }), c.on("actionLoaded", (I) => {
76
+ e || t((_) => {
77
+ const te = new Set(_);
78
+ return te.add(I), te;
78
79
  });
79
- }), l.on("allActionsLoaded", () => {
80
- e || B(!1);
81
- }), l.on("ready", () => {
82
- e || (F(l.getActionMetadata()), W.setActionMetadata(l.getActionMetadata()));
80
+ }), c.on("allActionsLoaded", () => {
81
+ e || P(!1);
82
+ }), c.on("ready", () => {
83
+ e || (U(c.getActionMetadata()), Q.setActionMetadata(c.getActionMetadata()));
83
84
  });
84
85
  try {
85
- B(!0);
86
- const T = await u.getBundle(a);
87
- if (e || (await l.init(k, { payload: T.payload }), e)) return;
88
- F(l.getActionMetadata()), W.setActionMetadata(l.getActionMetadata());
89
- } catch (T) {
90
- console.error("[useAvatarSession] Init failed:", T);
86
+ P(!0);
87
+ const I = await M.getBundle(a);
88
+ if (e || (await c.init(T, { payload: I.payload }), e)) return;
89
+ U(c.getActionMetadata()), Q.setActionMetadata(c.getActionMetadata());
90
+ } catch (I) {
91
+ console.error("[useAvatarSession] Init failed:", I);
91
92
  }
92
93
  })(), () => {
93
- e = !0, i.current?.destroy(), i.current = null, c.current = null;
94
+ e = !0, i.current?.destroy(), i.current = null, b.current = null;
94
95
  };
95
- }, [a, J, u]);
96
- const Z = A(!1);
97
- y(() => {
98
- O && i.current && E.has("wave_hand") && !Z.current && (i.current.play("wave_hand", "listening"), Z.current = !0);
99
- }, [O, E]);
100
- const se = w(async () => {
101
- if (!(_ || j)) {
102
- U(!0);
96
+ }, [a, G, M]);
97
+ const ee = m(!1);
98
+ h(() => {
99
+ B && i.current && A.has("wave_hand") && !ee.current && (i.current.play("wave_hand", "listening"), ee.current = !0);
100
+ }, [B, A]);
101
+ const ce = S(async () => {
102
+ if (!(K || W)) {
103
+ O(!0);
103
104
  try {
104
105
  await navigator.mediaDevices.getUserMedia({ audio: !0 });
105
- const e = await u.getLiveKitToken(a);
106
- H(e.token);
106
+ const e = await M.getLiveKitToken(a);
107
+ J(e.token);
107
108
  } catch (e) {
108
- console.error("[useAvatarSession] Failed to connect:", e), U(!1);
109
+ console.error("[useAvatarSession] Failed to connect:", e), O(!1);
109
110
  }
110
111
  }
111
- }, [_, j, a, u]), N = w(() => {
112
- I.current && (clearInterval(I.current), I.current = null), $(d), C.current && (clearTimeout(C.current), C.current = null), H(null), Q(!1), U(!1), c.current?.forceListening();
113
- }, [d]);
114
- y(() => {
115
- K.current = N;
116
- }, [N]);
117
- const ce = w(() => {
118
- Q(!0), U(!1);
119
- }, []), le = w(() => {
120
- I.current && clearInterval(I.current), $(d), I.current = setInterval(() => {
121
- $((e) => e <= 1 ? (clearInterval(I.current), I.current = null, setTimeout(() => K.current(), 0), 0) : e - 1);
112
+ }, [K, W, a, M]), H = S(() => {
113
+ E.current && (clearInterval(E.current), E.current = null), N(p), D.current && (clearTimeout(D.current), D.current = null), J(null), Z(!1), O(!1), b.current?.forceListening();
114
+ }, [p]);
115
+ h(() => {
116
+ V.current = H;
117
+ }, [H]);
118
+ const le = S(() => {
119
+ Z(!0), O(!1);
120
+ }, []), ue = S(() => {
121
+ E.current && clearInterval(E.current), N(p), E.current = setInterval(() => {
122
+ N((e) => e <= 1 ? (clearInterval(E.current), E.current = null, setTimeout(() => V.current(), 0), 0) : e - 1);
122
123
  }, 1e3);
123
- }, [d]), de = w((e) => {
124
- const p = Math.floor(e / 60).toString().padStart(2, "0"), k = (e % 60).toString().padStart(2, "0");
125
- return `${p}:${k}`;
124
+ }, [p]), de = S((e) => {
125
+ const f = Math.floor(e / 60).toString().padStart(2, "0"), T = (e % 60).toString().padStart(2, "0");
126
+ return `${f}:${T}`;
126
127
  }, []);
127
- y(() => () => {
128
- I.current && clearInterval(I.current);
128
+ h(() => () => {
129
+ E.current && clearInterval(E.current);
129
130
  }, []);
130
- const ue = w(
131
+ const pe = S(
131
132
  (e) => {
132
133
  if (!e) {
133
- C.current && (clearTimeout(C.current), C.current = null);
134
+ D.current && (clearTimeout(D.current), D.current = null);
134
135
  return;
135
136
  }
136
- C.current || (C.current = setTimeout(() => {
137
- C.current = null, K.current();
138
- }, x));
137
+ D.current || (D.current = setTimeout(() => {
138
+ D.current = null, V.current();
139
+ }, R));
139
140
  },
140
- [x]
141
- ), pe = w(() => {
142
- oe((e) => !e);
143
- }, []), fe = w((e) => {
144
- q(e);
145
- }, []), ge = w(() => {
146
- q((e) => e > 0 ? 0 : 1);
147
- }, []), he = w(() => {
141
+ [R]
142
+ ), fe = S(() => {
143
+ X((e) => !e);
144
+ }, []), ge = S((e) => {
145
+ X(e);
146
+ }, []), he = S((e) => {
147
+ Y(e);
148
+ }, []), me = S(() => {
149
+ Y((e) => e > 0 ? 0 : 1);
150
+ }, []), ve = S(() => {
148
151
  i.current?.interrupt();
149
- }, []), me = w((e) => {
152
+ }, []), ye = S((e) => {
150
153
  i.current?.play(e, "listening");
151
154
  }, []);
152
155
  return {
153
- containerRef: h,
156
+ containerRef: s,
154
157
  renderer: i.current,
155
- actionQueue: c.current,
156
- rendererState: R,
157
- avatarDimensions: n,
158
- loadedActions: E,
159
- actionMetadata: D,
160
- isLoadingActions: te,
161
- isEngineReady: O,
162
- token: ne,
163
- isConnecting: _,
164
- isConnected: j,
165
- livekitUrl: M,
166
- timeRemaining: re,
158
+ actionQueue: b.current,
159
+ rendererState: g,
160
+ avatarDimensions: o,
161
+ loadedActions: A,
162
+ actionMetadata: l,
163
+ isLoadingActions: F,
164
+ isEngineReady: B,
165
+ token: $,
166
+ isConnecting: K,
167
+ isConnected: W,
168
+ livekitUrl: L,
169
+ timeRemaining: ae,
167
170
  formatTime: de,
168
- micMuted: ie,
169
- volume: ae,
170
- connect: se,
171
- disconnect: N,
172
- toggleMic: pe,
173
- setVolume: fe,
174
- toggleVolume: ge,
175
- interrupt: he,
176
- runAction: me,
177
- startSessionTimer: le,
178
- markConnected: ce,
179
- setInactivityActive: ue
171
+ micMuted: oe,
172
+ volume: se,
173
+ turnOffMicWhenAISpeaking: d,
174
+ connect: ce,
175
+ disconnect: H,
176
+ toggleMic: fe,
177
+ setMicMuted: ge,
178
+ setVolume: he,
179
+ toggleVolume: me,
180
+ interrupt: ve,
181
+ runAction: ye,
182
+ startSessionTimer: ue,
183
+ markConnected: le,
184
+ setInactivityActive: pe
180
185
  };
181
186
  }
182
- const Fe = ({
183
- session: S,
187
+ const _e = ({
188
+ session: v,
184
189
  style: a,
185
- loadingContent: t
186
- }) => /* @__PURE__ */ b(
190
+ loadingContent: n
191
+ }) => /* @__PURE__ */ k(
187
192
  "div",
188
193
  {
189
194
  style: {
@@ -213,14 +218,14 @@ const Fe = ({
213
218
  /* @__PURE__ */ r(
214
219
  "div",
215
220
  {
216
- ref: S.containerRef,
221
+ ref: v.containerRef,
217
222
  style: {
218
223
  position: "relative",
219
224
  width: "100%",
220
225
  height: "100%",
221
226
  transition: "all 0.5s ease-in-out"
222
227
  },
223
- children: !S.isEngineReady && /* @__PURE__ */ r(
228
+ children: !v.isEngineReady && /* @__PURE__ */ r(
224
229
  "div",
225
230
  {
226
231
  style: {
@@ -233,7 +238,7 @@ const Fe = ({
233
238
  backdropFilter: "blur(4px)",
234
239
  zIndex: 10
235
240
  },
236
- children: t || /* @__PURE__ */ b(
241
+ children: n || /* @__PURE__ */ k(
237
242
  "div",
238
243
  {
239
244
  style: {
@@ -244,7 +249,7 @@ const Fe = ({
244
249
  },
245
250
  children: [
246
251
  /* @__PURE__ */ r(
247
- V,
252
+ q,
248
253
  {
249
254
  style: {
250
255
  width: 40,
@@ -277,7 +282,7 @@ const Fe = ({
277
282
  )
278
283
  ]
279
284
  }
280
- ), G = {
285
+ ), re = {
281
286
  flexShrink: 0,
282
287
  borderRadius: 9999,
283
288
  padding: 16,
@@ -287,7 +292,7 @@ const Fe = ({
287
292
  display: "inline-flex",
288
293
  alignItems: "center",
289
294
  justifyContent: "center"
290
- }, ee = {
295
+ }, ie = {
291
296
  position: "relative",
292
297
  display: "inline-flex",
293
298
  alignItems: "center",
@@ -302,41 +307,41 @@ const Fe = ({
302
307
  transition: "all 0.3s",
303
308
  border: "none",
304
309
  cursor: "pointer"
305
- }, Ue = ({
306
- session: S,
310
+ }, je = ({
311
+ session: v,
307
312
  style: a
308
313
  }) => {
309
314
  const {
310
- micMuted: t,
311
- toggleMic: f,
312
- isConnected: g,
313
- isConnecting: d,
314
- isEngineReady: x,
315
- loadedActions: M,
316
- connect: u,
317
- disconnect: h
318
- } = S, [m, i] = v(!1), [c, R] = v(!1), o = x && M.has("listening") && M.has("speaking"), n = t ? {
319
- ...G,
315
+ micMuted: n,
316
+ toggleMic: u,
317
+ isConnected: d,
318
+ isConnecting: w,
319
+ isEngineReady: p,
320
+ loadedActions: R,
321
+ connect: L,
322
+ disconnect: M
323
+ } = v, [s, y] = x(!1), [i, b] = x(!1), g = p && R.has("listening") && R.has("speaking"), C = n ? {
324
+ ...re,
320
325
  background: "rgba(239,68,68,0.1)",
321
326
  color: "#f87171",
322
327
  boxShadow: "inset 0 0 0 1px rgba(239,68,68,0.3)",
323
- ...m ? { background: "rgba(239,68,68,0.2)" } : {}
328
+ ...s ? { background: "rgba(239,68,68,0.2)" } : {}
324
329
  } : {
325
- ...G,
326
- background: m ? "rgba(255,255,255,0.1)" : "rgba(0,0,0,0.4)",
330
+ ...re,
331
+ background: s ? "rgba(255,255,255,0.1)" : "rgba(0,0,0,0.4)",
327
332
  color: "#fff",
328
333
  boxShadow: "inset 0 0 0 1px rgba(255,255,255,0.1)"
329
- }, s = g ? {
330
- ...ee,
331
- background: c ? "rgba(239,68,68,0.22)" : "rgba(239,68,68,0.14)",
334
+ }, o = d ? {
335
+ ...ie,
336
+ background: i ? "rgba(239,68,68,0.22)" : "rgba(239,68,68,0.14)",
332
337
  boxShadow: "inset 0 0 0 1px rgba(239,68,68,0.5), 0 0 20px rgba(239,68,68,0.2)"
333
338
  } : {
334
- ...ee,
339
+ ...ie,
335
340
  background: "linear-gradient(to right, #7c3aed, #db2777, #f97316)",
336
341
  boxShadow: "0 0 30px rgba(236,72,153,0.3)",
337
- opacity: c ? 0.9 : 1
342
+ opacity: i ? 0.9 : 1
338
343
  };
339
- return (d || !o) && (s.opacity = 0.5, s.cursor = "not-allowed"), /* @__PURE__ */ b(
344
+ return (w || !g) && (o.opacity = 0.5, o.cursor = "not-allowed"), /* @__PURE__ */ k(
340
345
  "div",
341
346
  {
342
347
  style: {
@@ -356,7 +361,7 @@ const Fe = ({
356
361
  to { transform: rotate(360deg); }
357
362
  }
358
363
  ` }),
359
- /* @__PURE__ */ b(
364
+ /* @__PURE__ */ k(
360
365
  "div",
361
366
  {
362
367
  style: {
@@ -373,34 +378,34 @@ const Fe = ({
373
378
  "button",
374
379
  {
375
380
  type: "button",
376
- onClick: f,
377
- onMouseEnter: () => i(!0),
378
- onMouseLeave: () => i(!1),
379
- style: n,
380
- title: t ? "Unmute mic" : "Mute mic",
381
- children: t ? /* @__PURE__ */ r(Re, { size: 24 }) : /* @__PURE__ */ r(Ce, { size: 24 })
381
+ onClick: u,
382
+ onMouseEnter: () => y(!0),
383
+ onMouseLeave: () => y(!1),
384
+ style: C,
385
+ title: n ? "Unmute mic" : "Mute mic",
386
+ children: n ? /* @__PURE__ */ r(Ce, { size: 24 }) : /* @__PURE__ */ r(Ee, { size: 24 })
382
387
  }
383
388
  ),
384
389
  /* @__PURE__ */ r(
385
390
  "button",
386
391
  {
387
392
  type: "button",
388
- onClick: g ? h : u,
389
- onMouseEnter: () => R(!0),
390
- onMouseLeave: () => R(!1),
391
- disabled: !g && (d || !o),
392
- style: s,
393
- children: d ? /* @__PURE__ */ b(P, { children: [
394
- /* @__PURE__ */ r(V, { size: 20, style: { animation: "zwr-spin 1s linear infinite" } }),
393
+ onClick: d ? M : L,
394
+ onMouseEnter: () => b(!0),
395
+ onMouseLeave: () => b(!1),
396
+ disabled: !d && (w || !g),
397
+ style: o,
398
+ children: w ? /* @__PURE__ */ k(j, { children: [
399
+ /* @__PURE__ */ r(q, { size: 20, style: { animation: "zwr-spin 1s linear infinite" } }),
395
400
  /* @__PURE__ */ r("span", { children: "Connecting..." })
396
- ] }) : !g && !o ? /* @__PURE__ */ b(P, { children: [
397
- /* @__PURE__ */ r(V, { size: 20, style: { animation: "zwr-spin 1s linear infinite" } }),
401
+ ] }) : !d && !g ? /* @__PURE__ */ k(j, { children: [
402
+ /* @__PURE__ */ r(q, { size: 20, style: { animation: "zwr-spin 1s linear infinite" } }),
398
403
  /* @__PURE__ */ r("span", { children: "Loading Avatar..." })
399
- ] }) : g ? /* @__PURE__ */ b(P, { children: [
400
- /* @__PURE__ */ r(Me, { size: 20 }),
404
+ ] }) : d ? /* @__PURE__ */ k(j, { children: [
405
+ /* @__PURE__ */ r(Le, { size: 20 }),
401
406
  /* @__PURE__ */ r("span", { children: "End Session" })
402
- ] }) : /* @__PURE__ */ b(P, { children: [
403
- /* @__PURE__ */ r(Ee, { size: 20 }),
407
+ ] }) : /* @__PURE__ */ k(j, { children: [
408
+ /* @__PURE__ */ r(De, { size: 20 }),
404
409
  /* @__PURE__ */ r("span", { children: "Start Session" })
405
410
  ] })
406
411
  }
@@ -411,10 +416,10 @@ const Fe = ({
411
416
  ]
412
417
  }
413
418
  );
414
- }, _e = ({
415
- session: S
419
+ }, Oe = ({
420
+ session: v
416
421
  }) => {
417
- const { isConnected: a, timeRemaining: t, formatTime: f } = S, g = a && t <= 30, d = a && t > 30 && t <= 60, x = {
422
+ const { isConnected: a, timeRemaining: n, formatTime: u } = v, d = a && n <= 30, w = a && n > 30 && n <= 60, p = {
418
423
  display: "flex",
419
424
  alignItems: "center",
420
425
  gap: 8,
@@ -424,12 +429,12 @@ const Fe = ({
424
429
  border: "1px solid",
425
430
  pointerEvents: "auto",
426
431
  transition: "all 0.3s",
427
- ...g ? {
432
+ ...d ? {
428
433
  background: "rgba(239,68,68,0.3)",
429
434
  borderColor: "rgba(239,68,68,0.4)",
430
435
  boxShadow: "0 0 15px rgba(239,68,68,0.3)",
431
436
  animation: "zwr-pulse 2s ease-in-out infinite"
432
- } : d ? {
437
+ } : w ? {
433
438
  background: "rgba(249,115,22,0.2)",
434
439
  borderColor: "rgba(249,115,22,0.3)",
435
440
  boxShadow: "0 4px 12px rgba(0,0,0,0.3)"
@@ -439,14 +444,14 @@ const Fe = ({
439
444
  boxShadow: "0 4px 12px rgba(0,0,0,0.3)"
440
445
  }
441
446
  };
442
- return /* @__PURE__ */ b(P, { children: [
447
+ return /* @__PURE__ */ k(j, { children: [
443
448
  /* @__PURE__ */ r("style", { children: `
444
449
  @keyframes zwr-pulse {
445
450
  0%, 100% { opacity: 1; }
446
451
  50% { opacity: 0.5; }
447
452
  }
448
453
  ` }),
449
- /* @__PURE__ */ b("div", { style: x, children: [
454
+ /* @__PURE__ */ k("div", { style: p, children: [
450
455
  /* @__PURE__ */ r(
451
456
  "div",
452
457
  {
@@ -473,123 +478,133 @@ const Fe = ({
473
478
  color: "rgba(255,255,255,0.7)",
474
479
  textTransform: "uppercase"
475
480
  },
476
- children: a ? `Online ${f(t)}` : "Offline"
481
+ children: a ? `Online ${u(n)}` : "Offline"
477
482
  }
478
483
  )
479
484
  ] })
480
485
  ] });
481
- }, je = ({
482
- session: S
486
+ }, $e = ({
487
+ session: v
483
488
  }) => {
484
- const { renderer: a, actionQueue: t, micMuted: f, volume: g, setInactivityActive: d, loadedActions: x } = S, M = A(!1), { state: u, audioTrack: h } = xe(), m = we(), i = Ae(m.localParticipant), c = A(null), R = A(x);
485
- y(() => {
486
- R.current = x;
487
- }, [x]), y(() => {
488
- const n = m.localParticipant;
489
- if (!a || !n) return;
490
- const s = new TextEncoder();
491
- return a.onOneshotComplete((L) => {
492
- const D = s.encode(
489
+ const { renderer: a, actionQueue: n, micMuted: u, volume: d, setInactivityActive: w, loadedActions: p } = v, { turnOffMicWhenAISpeaking: R, setMicMuted: L } = v, M = m(!1), { state: s, audioTrack: y } = we(), i = Ae(), b = Te(i.localParticipant), g = m(null), C = m(p);
490
+ h(() => {
491
+ C.current = p;
492
+ }, [p]), h(() => {
493
+ const t = i.localParticipant;
494
+ if (!a || !t) return;
495
+ const l = new TextEncoder();
496
+ return a.onOneshotComplete((F) => {
497
+ const P = l.encode(
493
498
  JSON.stringify({
494
499
  type: "ACTION_FINISHED",
495
- action: L
500
+ action: F
496
501
  })
497
502
  );
498
- n.publishData(D, {
503
+ t.publishData(P, {
499
504
  reliable: !0
500
- }).catch((F) => {
501
- console.error("[LiveKitAvatarProvider] Failed to publish action completion:", F);
505
+ }).catch(($) => {
506
+ console.error("[LiveKitAvatarProvider] Failed to publish action completion:", $);
502
507
  });
503
508
  });
504
- }, [a, m.localParticipant]), y(() => {
505
- if (!t) return;
506
- const n = new Se({
509
+ }, [a, i.localParticipant]), h(() => {
510
+ if (!n) return;
511
+ const t = new ke({
507
512
  threshold: 8e-3,
508
513
  analyseIntervalMs: 30,
509
514
  speechStartFrames: 1,
510
515
  speechPauseFrames: 30,
511
516
  turnEndFrames: 50
512
517
  });
513
- return c.current = n, n.on("speechStart", () => {
514
- t.setTurnActive(!0), t.setSpeechState("speaking");
515
- }), n.on("turnEnd", () => {
516
- t.setTurnActive(!1);
518
+ return g.current = t, t.on("speechStart", () => {
519
+ n.setTurnActive(!0), n.setSpeechState("speaking");
520
+ }), t.on("turnEnd", () => {
521
+ n.setTurnActive(!1);
517
522
  }), () => {
518
- n.stop(), c.current = null;
523
+ t.stop(), g.current = null;
519
524
  };
520
- }, [t]), y(() => {
521
- const n = c.current;
522
- if (n)
523
- if (h?.publication?.track) {
524
- const s = h.publication.track.mediaStreamTrack;
525
- s && n.start(s);
525
+ }, [n]), h(() => {
526
+ const t = g.current;
527
+ if (t)
528
+ if (y?.publication?.track) {
529
+ const l = y.publication.track.mediaStreamTrack;
530
+ l && t.start(l);
526
531
  } else
527
- n.stop();
528
- }, [h?.publication?.track]);
529
- const o = A(null);
530
- return y(() => {
531
- if (!t) return;
532
- const n = u;
533
- n === "speaking" ? (o.current && (clearTimeout(o.current), o.current = null), t.setTurnActive(!0), t.setSpeechState("speaking")) : n === "listening" || n === "idle" ? (o.current && (clearTimeout(o.current), o.current = null), c.current?.endTurn(), t.setTurnActive(!1)) : n === "thinking" && (o.current || (o.current = setTimeout(() => {
534
- o.current = null, c.current?.endTurn(), t.setTurnActive(!1);
532
+ t.stop();
533
+ }, [y?.publication?.track]);
534
+ const o = m(null), z = m(!1), A = m(!1);
535
+ return h(() => {
536
+ if (!n) return;
537
+ const t = s;
538
+ t === "speaking" ? (o.current && (clearTimeout(o.current), o.current = null), n.setTurnActive(!0), n.setSpeechState("speaking")) : t === "listening" || t === "idle" ? (o.current && (clearTimeout(o.current), o.current = null), g.current?.endTurn(), n.setTurnActive(!1)) : t === "thinking" && (o.current || (o.current = setTimeout(() => {
539
+ o.current = null, g.current?.endTurn(), n.setTurnActive(!1);
535
540
  }, 500)));
536
- }, [u, t]), y(() => () => {
541
+ }, [s, n]), h(() => {
542
+ const t = s === "speaking";
543
+ if (!R) {
544
+ z.current = t, A.current = !1;
545
+ return;
546
+ }
547
+ const l = z.current;
548
+ !l && t ? u ? A.current = !1 : (L(!0), A.current = !0) : l && t ? A.current && !u && (A.current = !1) : l && !t && (A.current && L(!1), A.current = !1), z.current = t;
549
+ }, [u, L, s, R]), h(() => () => {
537
550
  o.current && clearTimeout(o.current);
538
- }, []), ke((n) => {
551
+ }, []), Ie((t) => {
539
552
  try {
540
- const E = new TextDecoder().decode(n.payload), L = JSON.parse(E);
541
- if (L.type === "AVATAR_UPDATE") {
542
- const D = L.action;
543
- if (!R.current.has(D))
553
+ const U = new TextDecoder().decode(t.payload), F = JSON.parse(U);
554
+ if (F.type === "AVATAR_UPDATE") {
555
+ const P = F.action;
556
+ if (!C.current.has(P))
544
557
  return;
545
- t?.dispatch(D);
558
+ n?.dispatch(P);
546
559
  }
547
- } catch (s) {
548
- console.error("[LiveKitAvatarProvider] Failed to parse data message:", s);
560
+ } catch (l) {
561
+ console.error("[LiveKitAvatarProvider] Failed to parse data message:", l);
549
562
  }
550
- }), y(() => {
551
- d(!(u === "speaking") && !i);
552
- }, [i, d, u]), y(() => {
553
- if (!t) return;
554
- const n = !!h;
555
- (!n || u === "disconnected") && (c.current?.endTurn(), t.forceListening()), M.current = n;
556
- }, [h, u, t]), y(() => {
557
- const n = m.localParticipant;
558
- n && n.setMicrophoneEnabled(!f).catch((s) => {
559
- console.error("[LiveKitAvatarProvider] Failed to set mic state:", s);
563
+ }), h(() => {
564
+ w(!(s === "speaking") && !b);
565
+ }, [b, w, s]), h(() => {
566
+ if (!n) return;
567
+ const t = !!y;
568
+ (!t || s === "disconnected") && (g.current?.endTurn(), n.forceListening()), M.current = t;
569
+ }, [y, s, n]), h(() => {
570
+ const t = i.localParticipant;
571
+ t && t.setMicrophoneEnabled(!u).catch((l) => {
572
+ console.error("[LiveKitAvatarProvider] Failed to set mic state:", l);
560
573
  });
561
- }, [f, m.localParticipant]), /* @__PURE__ */ r("div", { style: { position: "absolute", bottom: 80, left: 8, right: 8, display: "flex", flexDirection: "column", gap: 8 }, children: /* @__PURE__ */ r(Te, { volume: g }) });
562
- }, Be = ({
563
- avatarId: S,
574
+ }, [u, i.localParticipant]), /* @__PURE__ */ r("div", { style: { position: "absolute", bottom: 80, left: 8, right: 8, display: "flex", flexDirection: "column", gap: 8 }, children: /* @__PURE__ */ r(Re, { volume: d }) });
575
+ }, Qe = ({
576
+ avatarId: v,
564
577
  apiKey: a,
565
- api: t,
566
- livekitUrl: f,
567
- sessionDuration: g,
568
- inactivityTimeout: d,
569
- style: x,
570
- className: M,
571
- loadingContent: u,
572
- customControls: h,
573
- customStatusBadge: m
578
+ api: n,
579
+ turnOffMicWhenAISpeaking: u,
580
+ livekitUrl: d,
581
+ sessionDuration: w,
582
+ inactivityTimeout: p,
583
+ style: R,
584
+ className: L,
585
+ loadingContent: M,
586
+ customControls: s,
587
+ customStatusBadge: y
574
588
  }) => {
575
- const i = Pe({
576
- avatarId: S,
589
+ const i = Ue({
590
+ avatarId: v,
577
591
  apiKey: a,
578
- api: t,
579
- livekitUrl: f,
580
- sessionDuration: g,
581
- inactivityTimeout: d
592
+ api: n,
593
+ turnOffMicWhenAISpeaking: u,
594
+ livekitUrl: d,
595
+ sessionDuration: w,
596
+ inactivityTimeout: p
582
597
  }), {
583
- token: c,
584
- isConnected: R,
585
- avatarDimensions: o,
586
- disconnect: n,
587
- startSessionTimer: s
598
+ token: b,
599
+ isConnected: g,
600
+ avatarDimensions: C,
601
+ disconnect: o,
602
+ startSessionTimer: z
588
603
  } = i;
589
- return /* @__PURE__ */ b(
604
+ return /* @__PURE__ */ k(
590
605
  "section",
591
606
  {
592
- className: M,
607
+ className: L,
593
608
  style: {
594
609
  position: "relative",
595
610
  display: "flex",
@@ -603,12 +618,12 @@ const Fe = ({
603
618
  height: "80vh",
604
619
  width: "auto",
605
620
  maxWidth: "100%",
606
- aspectRatio: o ? `${o.width} / ${o.height}` : "3 / 4",
607
- ...x
621
+ aspectRatio: C ? `${C.width} / ${C.height}` : "3 / 4",
622
+ ...R
608
623
  },
609
624
  children: [
610
- /* @__PURE__ */ r(Fe, { session: i, loadingContent: u }),
611
- /* @__PURE__ */ b(
625
+ /* @__PURE__ */ r(_e, { session: i, loadingContent: M }),
626
+ /* @__PURE__ */ k(
612
627
  "div",
613
628
  {
614
629
  style: {
@@ -622,7 +637,7 @@ const Fe = ({
622
637
  padding: 16
623
638
  },
624
639
  children: [
625
- /* @__PURE__ */ b(
640
+ /* @__PURE__ */ k(
626
641
  "div",
627
642
  {
628
643
  style: {
@@ -632,28 +647,28 @@ const Fe = ({
632
647
  justifyContent: "space-between"
633
648
  },
634
649
  children: [
635
- m ? m(i) : /* @__PURE__ */ r(_e, { session: i }),
650
+ y ? y(i) : /* @__PURE__ */ r(Oe, { session: i }),
636
651
  /* @__PURE__ */ r("div", {})
637
652
  ]
638
653
  }
639
654
  ),
640
- h ? h(i) : /* @__PURE__ */ r(Ue, { session: i })
655
+ s ? s(i) : /* @__PURE__ */ r(je, { session: i })
641
656
  ]
642
657
  }
643
658
  ),
644
- /* @__PURE__ */ r("div", { style: { display: "none" }, children: c && f && /* @__PURE__ */ r(
645
- Ie,
659
+ /* @__PURE__ */ r("div", { style: { display: "none" }, children: b && d && /* @__PURE__ */ r(
660
+ Me,
646
661
  {
647
- serverUrl: f,
648
- token: c,
662
+ serverUrl: d,
663
+ token: b,
649
664
  connect: !0,
650
665
  video: !1,
651
666
  audio: !0,
652
667
  onConnected: () => {
653
- i.markConnected(), s();
668
+ i.markConnected(), z();
654
669
  },
655
- onDisconnected: n,
656
- children: /* @__PURE__ */ r(je, { session: i })
670
+ onDisconnected: o,
671
+ children: /* @__PURE__ */ r($e, { session: i })
657
672
  }
658
673
  ) })
659
674
  ]
@@ -661,11 +676,11 @@ const Fe = ({
661
676
  );
662
677
  };
663
678
  export {
664
- Fe as AvatarCanvas,
665
- Ue as AvatarControls,
666
- _e as AvatarStatusBadge,
667
- je as LiveKitAvatarProvider,
668
- Be as LiveKitAvatarSession,
669
- Pe as useAvatarSession
679
+ _e as AvatarCanvas,
680
+ je as AvatarControls,
681
+ Oe as AvatarStatusBadge,
682
+ $e as LiveKitAvatarProvider,
683
+ Qe as LiveKitAvatarSession,
684
+ Ue as useAvatarSession
670
685
  };
671
686
  //# sourceMappingURL=zeroweight-renderer-react.es.js.map