orb-ui 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/adapters.js CHANGED
@@ -1,7 +1,7 @@
1
- import { t as e } from "./livekit-Dasur3lv.js";
1
+ import { i as e, n as t, r as n, t as r } from "./livekit-CukEeYFK.js";
2
2
  //#region src/adapters/vapi/index.ts
3
- var t = .12;
4
- function n(e) {
3
+ var i = .12;
4
+ function a(e) {
5
5
  let t = "idle", n = null;
6
6
  function r(r) {
7
7
  if (n &&= (clearTimeout(n), null), t === "speaking" && r === "listening") {
@@ -20,42 +20,42 @@ function n(e) {
20
20
  clearTimer: i
21
21
  };
22
22
  }
23
- function r(e, r) {
24
- let i = /* @__PURE__ */ new Set(), a = null;
23
+ function o(e, t) {
24
+ let n = /* @__PURE__ */ new Set(), r = null;
25
25
  function o() {
26
- a || (a = e.start.bind(e), e.start = async (...e) => (i.forEach((e) => e()), a(...e)));
26
+ r || (r = e.start.bind(e), e.start = async (...e) => (n.forEach((e) => e()), r(...e)));
27
27
  }
28
28
  function s() {
29
- i.size > 0 || !a || (e.start = a, a = null);
29
+ n.size > 0 || !r || (e.start = r, r = null);
30
30
  }
31
31
  return {
32
32
  async start() {
33
- await e.start(r?.assistantId);
33
+ await e.start(t?.assistantId);
34
34
  },
35
35
  stop() {
36
36
  e.stop();
37
37
  },
38
- subscribe(r) {
39
- let a = {
38
+ subscribe(t) {
39
+ let r = {
40
40
  state: "idle",
41
41
  volume: 0,
42
42
  outputVolume: 0
43
43
  }, c = 0;
44
44
  function l(e) {
45
- let n = e < t ? 0 : (e - t) / (1 - t), r = n > c ? .8 : .5;
46
- return c += (n - c) * r, c;
45
+ let t = e < i ? 0 : (e - i) / (1 - i), n = t > c ? .8 : .5;
46
+ return c += (t - c) * n, c;
47
47
  }
48
48
  function u(e) {
49
- a = e, r(e);
49
+ r = e, t(e);
50
50
  }
51
51
  function d(e) {
52
52
  u({
53
- ...a,
53
+ ...r,
54
54
  ...e,
55
- state: e.state ?? a.state
55
+ state: e.state ?? r.state
56
56
  });
57
57
  }
58
- let { emitState: f, clearTimer: p } = n((e) => d({ state: e })), m = () => f("connecting"), h = "idle", g = !1, _ = () => {
58
+ let { emitState: f, clearTimer: p } = a((e) => d({ state: e })), m = () => f("connecting"), h = "idle", g = !1, _ = () => {
59
59
  g = !0, h = "listening", f("listening"), d({
60
60
  volume: 0,
61
61
  outputVolume: 0
@@ -97,15 +97,15 @@ function r(e, r) {
97
97
  error: e
98
98
  }), c = 0;
99
99
  };
100
- return e.on("call-start", _), e.on("call-end", v), e.on("speech-start", y), e.on("speech-end", b), e.on("volume-level", D), e.on("message", O), e.on("error", k), i.add(m), o(), () => {
101
- p(), E(), c = 0, e.removeListener("call-start", _), e.removeListener("call-end", v), e.removeListener("speech-start", y), e.removeListener("speech-end", b), e.removeListener("volume-level", D), e.removeListener("message", O), e.removeListener("error", k), i.delete(m), s();
100
+ return e.on("call-start", _), e.on("call-end", v), e.on("speech-start", y), e.on("speech-end", b), e.on("volume-level", D), e.on("message", O), e.on("error", k), n.add(m), o(), () => {
101
+ p(), E(), c = 0, e.removeListener("call-start", _), e.removeListener("call-end", v), e.removeListener("speech-start", y), e.removeListener("speech-end", b), e.removeListener("volume-level", D), e.removeListener("message", O), e.removeListener("error", k), n.delete(m), s();
102
102
  };
103
103
  }
104
104
  };
105
105
  }
106
106
  //#endregion
107
107
  //#region src/adapters/elevenlabs/index.ts
108
- function i(e, t) {
108
+ function s(e, t) {
109
109
  let n = null, r = 0, i = null, a = null, o = {
110
110
  state: "idle",
111
111
  volume: 0,
@@ -245,7 +245,7 @@ function i(e, t) {
245
245
  }
246
246
  //#endregion
247
247
  //#region src/adapters/pipecat/index.ts
248
- var a = {
248
+ var c = {
249
249
  connected: "connected",
250
250
  disconnected: "disconnected",
251
251
  transportStateChanged: "transportStateChanged",
@@ -262,8 +262,19 @@ var a = {
262
262
  userStoppedSpeaking: "userStoppedSpeaking",
263
263
  botLlmStarted: "botLlmStarted",
264
264
  botTtsStarted: "botTtsStarted"
265
- }, o = .002, s = 4, c = .8, l = .6, u = .2;
266
- function d(e) {
265
+ }, l = .002, u = 4, d = .8, f = .6, p = .2, m = {
266
+ noiseFloor: .002,
267
+ gain: 5.1,
268
+ exponent: .8,
269
+ attack: .5,
270
+ release: .22
271
+ };
272
+ function h() {
273
+ if (typeof window > "u") return;
274
+ let e = window.AudioContext;
275
+ return e ? new e() : void 0;
276
+ }
277
+ function g(e) {
267
278
  switch (e) {
268
279
  case "initializing":
269
280
  case "initialized":
@@ -278,30 +289,37 @@ function d(e) {
278
289
  default: return;
279
290
  }
280
291
  }
281
- function f(e) {
282
- if (typeof e != "number" || !Number.isFinite(e) || e <= o) return 0;
283
- let t = Math.min(1, Math.max(0, e - o));
284
- return Math.min(1, t * s) ** +c;
292
+ function _(e) {
293
+ if (typeof e != "number" || !Number.isFinite(e) || e <= l) return 0;
294
+ let t = Math.min(1, Math.max(0, e - l));
295
+ return Math.min(1, t * u) ** +d;
285
296
  }
286
- function p(e, t) {
287
- let n = f(e), r = n > t ? l : u;
297
+ function v(e, t) {
298
+ let n = _(e), r = n > t ? f : p;
288
299
  return t + (n - t) * r;
289
300
  }
290
- function m(e) {
301
+ function y(e) {
291
302
  return typeof e == "object" && !!e && "kind" in e && typeof e.kind == "string";
292
303
  }
293
- function h(e, t = {}) {
294
- let n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map(), i = {
295
- state: d(e.state ?? "") ?? "idle",
304
+ function b(t, r = {}) {
305
+ let i = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Map(), o = {
306
+ state: g(t.state ?? "") ?? "idle",
296
307
  volume: 0,
297
308
  inputVolume: 0,
298
309
  outputVolume: 0
299
- }, o = !1, s = 0, c = 0;
300
- function l(e) {
301
- i = e, n.forEach((t) => t(e));
310
+ }, s = !1, l = 0, u = 0, d = null, f = null, p, _;
311
+ function b() {
312
+ let e = typeof r.outputVolumeCalibration == "function" ? r.outputVolumeCalibration() : r.outputVolumeCalibration;
313
+ return {
314
+ ...m,
315
+ ...e
316
+ };
317
+ }
318
+ function x(e) {
319
+ o = e, i.forEach((t) => t(e));
302
320
  }
303
- function u(e, t) {
304
- s = 0, c = 0, l({
321
+ function S(e, t) {
322
+ l = 0, u = 0, x({
305
323
  state: e,
306
324
  volume: 0,
307
325
  inputVolume: 0,
@@ -309,143 +327,144 @@ function h(e, t = {}) {
309
327
  ...t === void 0 ? {} : { error: t }
310
328
  });
311
329
  }
312
- function f(e) {
313
- if (i.state !== "listening") return;
314
- s = p(e, s);
315
- let t = s;
316
- l({
317
- ...i,
330
+ function C(e) {
331
+ if (o.state !== "listening") return;
332
+ l = v(e, l);
333
+ let t = l;
334
+ x({
335
+ ...o,
318
336
  volume: t,
319
337
  inputVolume: t,
320
338
  outputVolume: 0
321
339
  });
322
340
  }
323
- function h(e, n) {
324
- if (i.state !== "speaking") return;
325
- if (n && typeof n == "object") {
326
- let e = n;
327
- if (e.local || t.isBotParticipant && !t.isBotParticipant(e)) return;
341
+ function w(e, t) {
342
+ if (o.state !== "speaking") return;
343
+ if (t && typeof t == "object") {
344
+ let e = t;
345
+ if (e.local || r.isBotParticipant && !r.isBotParticipant(e)) return;
328
346
  }
329
- c = p(e, c);
330
- let r = c;
331
- l({
332
- ...i,
333
- volume: r,
347
+ let i = n(typeof e == "number" ? e : 0, u, b);
348
+ u = i.normalized, r.onOutputVolumeSample?.(i);
349
+ let a = i.normalized;
350
+ x({
351
+ ...o,
352
+ volume: a,
334
353
  inputVolume: 0,
335
- outputVolume: r
354
+ outputVolume: a
336
355
  });
337
356
  }
338
- function g(e) {
357
+ function T() {
358
+ d = null, p?.stop(), p = void 0;
359
+ }
360
+ function E() {
361
+ f = null, _?.stop(), _ = void 0;
362
+ }
363
+ function D() {
364
+ T(), E();
365
+ }
366
+ function O(t) {
367
+ d === t && p || (T(), d = t, p = e(t, r.createAudioContext ?? h, C), p || (d = null));
368
+ }
369
+ function k(t) {
370
+ f === t && _ || (E(), f = t, _ = e(t, r.createAudioContext ?? h, w), _ || (f = null));
371
+ }
372
+ function A() {
373
+ let e = t.tracks?.();
374
+ e?.local?.audio && O(e.local.audio), e?.bot?.audio && k(e.bot.audio);
375
+ }
376
+ function j(e) {
339
377
  if (!e || typeof e != "object") return !0;
340
- let n = e;
341
- return n.local ? !1 : t.isBotParticipant?.(n) ?? !0;
378
+ let t = e;
379
+ return t.local ? !1 : r.isBotParticipant?.(t) ?? !0;
342
380
  }
343
- function _(e, n) {
344
- if (t.playRemoteAudio === !1 || !m(e) || e.kind !== "audio" || !g(n)) return;
345
- let i = t.createAudioElement?.() ?? new Audio();
346
- i.autoplay = !0, i.srcObject = new MediaStream([e]), r.set(e, i), i.play().catch(() => void 0);
381
+ function M(e, t) {
382
+ if (r.playRemoteAudio === !1 || !y(e) || e.kind !== "audio" || !j(t)) return;
383
+ let n = r.createAudioElement?.() ?? new Audio();
384
+ n.autoplay = !0, n.srcObject = new MediaStream([e]), a.set(e, n), n.play().catch(() => void 0);
347
385
  }
348
- function v(e) {
349
- if (!m(e)) return;
350
- let t = r.get(e);
351
- t && (t.pause(), t.srcObject = null, t.remove(), r.delete(e));
386
+ function N(e) {
387
+ if (!y(e)) return;
388
+ let t = a.get(e);
389
+ t && (t.pause(), t.srcObject = null, t.remove(), a.delete(e));
352
390
  }
353
- function y() {
354
- r.forEach((e) => {
391
+ function P(e, n) {
392
+ if (!y(e) || e.kind !== "audio") return;
393
+ let r = t.tracks?.().local?.audio;
394
+ n?.local === !0 || e === r ? O(e) : j(n) && k(e), M(e, n);
395
+ }
396
+ function F(e) {
397
+ e === d && T(), e === f && E(), N(e);
398
+ }
399
+ function I() {
400
+ a.forEach((e) => {
355
401
  e.pause(), e.srcObject = null, e.remove();
356
- }), r.clear();
357
- }
358
- let b = [
359
- [a.connected, () => u("connecting")],
360
- [a.botReady, () => u("listening")],
361
- [a.disconnected, () => u("idle")],
362
- [a.botDisconnected, () => u("idle")],
363
- [a.transportStateChanged, (e) => {
402
+ }), a.clear();
403
+ }
404
+ function L() {
405
+ D(), S("idle");
406
+ }
407
+ function R() {
408
+ S("listening"), A();
409
+ }
410
+ let z = [
411
+ [c.connected, () => S("connecting")],
412
+ [c.botReady, R],
413
+ [c.disconnected, L],
414
+ [c.botDisconnected, L],
415
+ [c.transportStateChanged, (e) => {
364
416
  if (typeof e != "string") return;
365
- let t = d(e);
366
- t && u(t);
417
+ let t = g(e);
418
+ t && S(t);
367
419
  }],
368
- [a.userStartedSpeaking, () => u("listening")],
369
- [a.userStoppedSpeaking, () => {
370
- i.state === "listening" && u("thinking");
420
+ [c.userStartedSpeaking, () => S("listening")],
421
+ [c.userStoppedSpeaking, () => {
422
+ o.state === "listening" && S("thinking");
371
423
  }],
372
- [a.botLlmStarted, () => u("thinking")],
373
- [a.botTtsStarted, () => u("thinking")],
374
- [a.botStartedSpeaking, () => u("speaking")],
375
- [a.botStoppedSpeaking, () => u("listening")],
376
- [a.localAudioLevel, f],
377
- [a.remoteAudioLevel, h],
378
- ["trackStarted", _],
379
- ["trackStopped", v],
380
- [a.error, (e) => u("error", e)],
381
- [a.messageError, (e) => u("error", e)],
382
- [a.deviceError, (e) => u("error", e)]
424
+ [c.botLlmStarted, () => S("thinking")],
425
+ [c.botTtsStarted, () => S("thinking")],
426
+ [c.botStartedSpeaking, () => S("speaking")],
427
+ [c.botStoppedSpeaking, () => S("listening")],
428
+ [c.localAudioLevel, (e) => !p && C(e)],
429
+ [c.remoteAudioLevel, (e, t) => !_ && w(e, t)],
430
+ ["trackStarted", P],
431
+ ["trackStopped", F],
432
+ [c.error, (e) => S("error", e)],
433
+ [c.messageError, (e) => S("error", e)],
434
+ [c.deviceError, (e) => S("error", e)]
383
435
  ];
384
- function x() {
385
- o || (o = !0, b.forEach(([t, n]) => e.on(t, n)));
436
+ function B() {
437
+ s || (s = !0, z.forEach(([e, n]) => t.on(e, n)));
386
438
  }
387
- function S() {
388
- o && (o = !1, b.forEach(([t, n]) => {
389
- e.off ? e.off(t, n) : e.removeListener?.(t, n);
390
- }), y());
439
+ function V() {
440
+ s && (s = !1, z.forEach(([e, n]) => {
441
+ t.off ? t.off(e, n) : t.removeListener?.(e, n);
442
+ }), D(), I());
391
443
  }
392
444
  return {
393
445
  subscribe(e) {
394
- return n.add(e), x(), e(i), () => {
395
- n.delete(e), n.size === 0 && S();
446
+ return i.add(e), B(), e(o), () => {
447
+ i.delete(e), i.size === 0 && V();
396
448
  };
397
449
  },
398
450
  async start() {
399
- u("connecting");
451
+ S("connecting");
400
452
  try {
401
- await (t.connect ? t.connect() : e.connect());
453
+ await (r.connect ? r.connect() : t.connect());
402
454
  } catch (e) {
403
- throw u("error", e), e;
455
+ throw S("error", e), e;
404
456
  }
405
457
  },
406
458
  async stop() {
407
459
  try {
408
- await (t.disconnect ? t.disconnect() : e.disconnect());
460
+ await (r.disconnect ? r.disconnect() : t.disconnect());
409
461
  } finally {
410
- y(), u("idle");
462
+ D(), I(), S("idle");
411
463
  }
412
464
  }
413
465
  };
414
466
  }
415
467
  //#endregion
416
- //#region src/adapters/audio-level.ts
417
- var g = {
418
- noiseFloor: 0,
419
- gain: 4,
420
- exponent: .8,
421
- attack: 1,
422
- release: 1
423
- };
424
- function _(e, t, n) {
425
- return Math.min(n, Math.max(t, e));
426
- }
427
- function v(e, t) {
428
- return typeof e == "number" && Number.isFinite(e) ? e : t;
429
- }
430
- function y(e) {
431
- let t = typeof e == "function" ? e() : e;
432
- return {
433
- noiseFloor: _(v(t?.noiseFloor, g.noiseFloor), 0, 1),
434
- gain: Math.max(0, v(t?.gain, g.gain)),
435
- exponent: Math.max(.01, v(t?.exponent, g.exponent)),
436
- attack: _(v(t?.attack, g.attack), 0, 1),
437
- release: _(v(t?.release, g.release), 0, 1)
438
- };
439
- }
440
- function b(e, t, n) {
441
- let r = Number.isFinite(e) ? _(e, 0, 1) : 0, i = y(n), a = _((r <= i.noiseFloor ? 0 : r - i.noiseFloor) * i.gain, 0, 1) ** +i.exponent, o = a > t ? i.attack : i.release;
442
- return {
443
- raw: r,
444
- shaped: a,
445
- normalized: _(t + (a - t) * o, 0, 1)
446
- };
447
- }
448
- //#endregion
449
468
  //#region src/adapters/openai-realtime/index.ts
450
469
  var x = "https://api.openai.com/v1/realtime/calls", S = .015, C = 8, w = {
451
470
  noiseFloor: .003,
@@ -486,24 +505,24 @@ function O(e) {
486
505
  return t;
487
506
  }
488
507
  function k(e) {
489
- let t = /* @__PURE__ */ new Set(), n = {
508
+ let t = /* @__PURE__ */ new Set(), r = {
490
509
  state: "idle",
491
510
  volume: 0,
492
511
  inputVolume: 0,
493
512
  outputVolume: 0
494
- }, r = null, i = null, a = null, o = null, s, c, l = 0, u = 0, d = !1;
495
- function f() {
513
+ }, i = null, a = null, o = null, s = null, c, l, u = 0, d = 0, f = !1;
514
+ function p() {
496
515
  let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
497
516
  return {
498
517
  ...w,
499
518
  ...t
500
519
  };
501
520
  }
502
- function p(e) {
503
- n = e, t.forEach((t) => t(e));
521
+ function m(e) {
522
+ r = e, t.forEach((t) => t(e));
504
523
  }
505
- function m(e, t) {
506
- n.state === e && t === void 0 || (e !== "speaking" && (u = 0), p({
524
+ function h(e, t) {
525
+ r.state === e && t === void 0 || (e !== "speaking" && (d = 0), m({
507
526
  state: e,
508
527
  volume: 0,
509
528
  inputVolume: 0,
@@ -511,92 +530,92 @@ function k(e) {
511
530
  ...t === void 0 ? {} : { error: t }
512
531
  }));
513
532
  }
514
- function h(e) {
515
- if (n.state !== "listening") return;
533
+ function g(e) {
534
+ if (r.state !== "listening") return;
516
535
  let t = E(e);
517
- p({
518
- ...n,
536
+ m({
537
+ ...r,
519
538
  volume: t,
520
539
  inputVolume: t,
521
540
  outputVolume: 0
522
541
  });
523
542
  }
524
- function g(t) {
525
- let r = b(t, u, f);
526
- u = r.normalized, e.onOutputVolumeSample?.(r);
527
- let i = r.normalized;
528
- if (i > S) l = 0, n.state !== "speaking" && m("speaking");
529
- else if (n.state === "speaking" && (l += 1, l >= C)) {
530
- l = 0, m("listening");
543
+ function _(t) {
544
+ let i = n(t, d, p);
545
+ d = i.normalized, e.onOutputVolumeSample?.(i);
546
+ let a = i.normalized;
547
+ if (a > S) u = 0, r.state !== "speaking" && h("speaking");
548
+ else if (r.state === "speaking" && (u += 1, u >= C)) {
549
+ u = 0, h("listening");
531
550
  return;
532
551
  }
533
- n.state === "speaking" && p({
534
- ...n,
535
- volume: i,
552
+ r.state === "speaking" && m({
553
+ ...r,
554
+ volume: a,
536
555
  inputVolume: 0,
537
- outputVolume: i
556
+ outputVolume: a
538
557
  });
539
558
  }
540
- function _(e) {
559
+ function v(e) {
541
560
  switch (e.type) {
542
561
  case "session.created":
543
562
  case "session.updated":
544
- m("listening");
563
+ h("listening");
545
564
  break;
546
565
  case "input_audio_buffer.speech_started":
547
- m("listening");
566
+ h("listening");
548
567
  break;
549
568
  case "input_audio_buffer.speech_stopped":
550
569
  case "response.created":
551
- m("thinking");
570
+ h("thinking");
552
571
  break;
553
572
  case "response.output_audio.delta":
554
573
  case "output_audio_buffer.started":
555
- m("speaking");
574
+ h("speaking");
556
575
  break;
557
576
  case "output_audio_buffer.stopped":
558
- m("listening");
577
+ h("listening");
559
578
  break;
560
579
  case "response.done":
561
580
  case "response.cancelled":
562
- n.state !== "speaking" && m("listening");
581
+ r.state !== "speaking" && h("listening");
563
582
  break;
564
583
  case "error":
565
- m("error", e.error ?? e);
584
+ h("error", e.error ?? e);
566
585
  break;
567
586
  }
568
587
  }
569
- async function v(e) {
570
- d = !0, i?.close(), i = null, r?.close(), r = null, a?.getTracks().forEach((e) => e.stop()), a = null, o?.pause(), o && (o.srcObject = null), o?.remove(), o = null, await Promise.all([s?.stop(), c?.stop()]), s = void 0, c = void 0, l = 0, u = 0, e && m("idle"), d = !1;
588
+ async function y(e) {
589
+ f = !0, a?.close(), a = null, i?.close(), i = null, o?.getTracks().forEach((e) => e.stop()), o = null, s?.pause(), s && (s.srcObject = null), s?.remove(), s = null, await Promise.all([c?.stop(), l?.stop()]), c = void 0, l = void 0, u = 0, d = 0, e && h("idle"), f = !1;
571
590
  }
572
591
  return {
573
592
  subscribe(e) {
574
- return t.add(e), e(n), () => t.delete(e);
593
+ return t.add(e), e(r), () => t.delete(e);
575
594
  },
576
595
  async start() {
577
- if (!r) {
578
- m("connecting"), d = !1;
596
+ if (!i) {
597
+ h("connecting"), f = !1;
579
598
  try {
580
- let t = e.getUserMedia ?? ((e) => navigator.mediaDevices.getUserMedia(e)), n = e.createPeerConnection ?? (() => new RTCPeerConnection()), l = e.fetch ?? fetch, u = e.createAudioElement ?? (() => new Audio()), f = e.createAudioContext ?? T, p = O(await e.getClientSecret());
581
- a = await t(e.mediaStreamConstraints ?? { audio: !0 }), s = D(a, f, h);
582
- let v = n();
583
- r = v, a.getAudioTracks().forEach((e) => v.addTrack(e, a)), o = u(), o.autoplay = !0, v.ontrack = (e) => {
599
+ let t = e.getUserMedia ?? ((e) => navigator.mediaDevices.getUserMedia(e)), n = e.createPeerConnection ?? (() => new RTCPeerConnection()), r = e.fetch ?? fetch, u = e.createAudioElement ?? (() => new Audio()), d = e.createAudioContext ?? T, p = O(await e.getClientSecret());
600
+ o = await t(e.mediaStreamConstraints ?? { audio: !0 }), c = D(o, d, g);
601
+ let m = n();
602
+ i = m, o.getAudioTracks().forEach((e) => m.addTrack(e, o)), s = u(), s.autoplay = !0, m.ontrack = (e) => {
584
603
  let t = e.streams[0] ?? new MediaStream([e.track]);
585
- o && (o.srcObject = t), o?.play().catch(() => void 0), c?.stop(), c = D(t, f, g);
586
- }, v.onconnectionstatechange = () => {
587
- d || (v.connectionState === "failed" ? m("error", /* @__PURE__ */ Error("[orb-ui/openai-realtime] WebRTC connection failed.")) : (v.connectionState === "disconnected" || v.connectionState === "closed") && m("idle"));
604
+ s && (s.srcObject = t), s?.play().catch(() => void 0), l?.stop(), l = D(t, d, _);
605
+ }, m.onconnectionstatechange = () => {
606
+ f || (m.connectionState === "failed" ? h("error", /* @__PURE__ */ Error("[orb-ui/openai-realtime] WebRTC connection failed.")) : (m.connectionState === "disconnected" || m.connectionState === "closed") && h("idle"));
588
607
  };
589
- let y = v.createDataChannel("oai-events");
590
- i = y, y.onopen = () => m("listening"), y.onmessage = (e) => {
608
+ let y = m.createDataChannel("oai-events");
609
+ a = y, y.onopen = () => h("listening"), y.onmessage = (e) => {
591
610
  try {
592
- _(JSON.parse(String(e.data)));
611
+ v(JSON.parse(String(e.data)));
593
612
  } catch {}
594
- }, y.onerror = (e) => m("error", e), y.onclose = () => {
595
- d || m("idle");
613
+ }, y.onerror = (e) => h("error", e), y.onclose = () => {
614
+ f || h("idle");
596
615
  };
597
- let b = await v.createOffer();
598
- await v.setLocalDescription(b);
599
- let S = await l(e.callsUrl ?? x, {
616
+ let b = await m.createOffer();
617
+ await m.setLocalDescription(b);
618
+ let S = await r(e.callsUrl ?? x, {
600
619
  method: "POST",
601
620
  body: b.sdp,
602
621
  headers: {
@@ -608,17 +627,17 @@ function k(e) {
608
627
  let e = await S.text();
609
628
  throw Error(`[orb-ui/openai-realtime] Session negotiation failed (${S.status}): ${e}`);
610
629
  }
611
- await v.setRemoteDescription({
630
+ await m.setRemoteDescription({
612
631
  type: "answer",
613
632
  sdp: await S.text()
614
633
  });
615
634
  } catch (e) {
616
- throw await v(!1), m("error", e), e;
635
+ throw await y(!1), h("error", e), e;
617
636
  }
618
637
  }
619
638
  },
620
639
  async stop() {
621
- await v(!0);
640
+ await y(!0);
622
641
  }
623
642
  };
624
643
  }
@@ -671,16 +690,16 @@ function L(e) {
671
690
  return t ? Number(t[1]) : 24e3;
672
691
  }
673
692
  function R(e) {
674
- let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), r = {
693
+ let t = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Set(), i = {
675
694
  state: "idle",
676
695
  volume: 0,
677
696
  inputVolume: 0,
678
697
  outputVolume: 0
679
- }, i = null, a = null, o = null, s = null, c = null, l = null, u = null, d = null, f = null, p = !1, m = 0, h = 0, g = !1, _ = !1;
680
- function v() {
698
+ }, a = null, o = null, s = null, c = null, l = null, u = null, d = null, f = null, p = null, m = !1, h = 0, g = 0, _ = !1, v = !1;
699
+ function y() {
681
700
  return e.activityDetection !== "server";
682
701
  }
683
- function y() {
702
+ function b() {
684
703
  let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
685
704
  return {
686
705
  ...j,
@@ -688,10 +707,10 @@ function R(e) {
688
707
  };
689
708
  }
690
709
  function x(e) {
691
- r = e, t.forEach((t) => t(e));
710
+ i = e, t.forEach((t) => t(e));
692
711
  }
693
712
  function S(e, t) {
694
- r.state === e && t === void 0 || (e !== "speaking" && (h = 0), x({
713
+ i.state === e && t === void 0 || (e !== "speaking" && (g = 0), x({
695
714
  state: e,
696
715
  volume: 0,
697
716
  inputVolume: 0,
@@ -700,113 +719,113 @@ function R(e) {
700
719
  }));
701
720
  }
702
721
  function C() {
703
- f &&= (clearTimeout(f), null);
722
+ p &&= (clearTimeout(p), null);
704
723
  }
705
724
  function w(t) {
706
- t > (e.speechThreshold ?? .04) ? (!p && v() && i?.sendRealtimeInput({ activityStart: {} }), p = !0, C(), r.state !== "listening" && S("listening")) : p && !f && (f = setTimeout(() => {
707
- f = null, p = !1, v() && i?.sendRealtimeInput({ activityEnd: {} }), r.state === "listening" && S("thinking");
708
- }, e.speechEndDelayMs ?? 500)), r.state === "listening" && x({
709
- ...r,
725
+ t > (e.speechThreshold ?? .04) ? (!m && y() && a?.sendRealtimeInput({ activityStart: {} }), m = !0, C(), i.state !== "listening" && S("listening")) : m && !p && (p = setTimeout(() => {
726
+ p = null, m = !1, y() && a?.sendRealtimeInput({ activityEnd: {} }), i.state === "listening" && S("thinking");
727
+ }, e.speechEndDelayMs ?? 500)), i.state === "listening" && x({
728
+ ...i,
710
729
  volume: t,
711
730
  inputVolume: t,
712
731
  outputVolume: 0
713
732
  });
714
733
  }
715
734
  function T() {
716
- n.forEach((e) => {
735
+ r.forEach((e) => {
717
736
  try {
718
737
  e.stop();
719
738
  } catch {}
720
739
  e.disconnect();
721
- }), n.clear(), m = o?.currentTime ?? 0;
740
+ }), r.clear(), h = s?.currentTime ?? 0;
722
741
  }
723
742
  function E() {
724
- g && n.size === 0 && (g = !1, S("listening"));
743
+ _ && r.size === 0 && (_ = !1, S("listening"));
725
744
  }
726
745
  function D(e) {
727
- if (!o || !u || !e.data) return;
728
- let t = I(e.data), i = o.createBuffer(1, t.length, L(e.mimeType));
729
- i.getChannelData(0).set(t);
730
- let a = o.createBufferSource();
731
- a.buffer = i, a.connect(u);
732
- let s = Math.max(o.currentTime, m);
733
- m = s + i.duration, n.add(a), a.onended = () => {
734
- n.delete(a), a.disconnect(), E();
735
- }, a.start(s), r.state !== "speaking" && S("speaking");
746
+ if (!s || !d || !e.data) return;
747
+ let t = I(e.data), n = s.createBuffer(1, t.length, L(e.mimeType));
748
+ n.getChannelData(0).set(t);
749
+ let a = s.createBufferSource();
750
+ a.buffer = n, a.connect(d);
751
+ let o = Math.max(s.currentTime, h);
752
+ h = o + n.duration, r.add(a), a.onended = () => {
753
+ r.delete(a), a.disconnect(), E();
754
+ }, a.start(o), i.state !== "speaking" && S("speaking");
736
755
  }
737
756
  function O(e) {
738
757
  e.setupComplete && S("listening");
739
758
  let t = e.serverContent;
740
759
  if (!t) return;
741
760
  if (t.interrupted) {
742
- T(), g = !1, S("listening");
761
+ T(), _ = !1, S("listening");
743
762
  return;
744
763
  }
745
764
  let n = t.modelTurn?.parts?.filter((e) => e.inlineData?.data) ?? [];
746
- t.modelTurn && n.length === 0 && r.state !== "speaking" && S("thinking"), n.forEach((e) => D(e.inlineData)), t.waitingForInput && (T(), S("listening")), (t.turnComplete || t.generationComplete) && (g = !0, E());
765
+ t.modelTurn && n.length === 0 && i.state !== "speaking" && S("thinking"), n.forEach((e) => D(e.inlineData)), t.waitingForInput && (T(), S("listening")), (t.turnComplete || t.generationComplete) && (_ = !0, E());
747
766
  }
748
767
  function k() {
749
- if (!u) return;
750
- let t = new Float32Array(u.fftSize);
751
- d = setInterval(() => {
752
- if (!u || r.state !== "speaking") return;
753
- u.getFloatTimeDomainData(t);
754
- let n = b(M(t), h, y);
755
- h = n.normalized, e.onOutputVolumeSample?.(n);
756
- let i = n.normalized;
768
+ if (!d) return;
769
+ let t = new Float32Array(d.fftSize);
770
+ f = setInterval(() => {
771
+ if (!d || i.state !== "speaking") return;
772
+ d.getFloatTimeDomainData(t);
773
+ let r = n(M(t), g, b);
774
+ g = r.normalized, e.onOutputVolumeSample?.(r);
775
+ let a = r.normalized;
757
776
  x({
758
- ...r,
759
- volume: i,
777
+ ...i,
778
+ volume: a,
760
779
  inputVolume: 0,
761
- outputVolume: i
780
+ outputVolume: a
762
781
  });
763
782
  }, 33);
764
783
  }
765
784
  function R() {
766
- !o || !a || !i || (s = o.createMediaStreamSource(a), c = o.createScriptProcessor(4096, 1, 1), l = o.createGain(), l.gain.value = 0, s.connect(c), c.connect(l), l.connect(o.destination), c.onaudioprocess = (t) => {
767
- if (!i || _) return;
785
+ !s || !o || !a || (c = s.createMediaStreamSource(o), l = s.createScriptProcessor(4096, 1, 1), u = s.createGain(), u.gain.value = 0, c.connect(l), l.connect(u), u.connect(s.destination), l.onaudioprocess = (t) => {
786
+ if (!a || v) return;
768
787
  let n = t.inputBuffer.getChannelData(0);
769
788
  w(N(n));
770
- let r = P(n, o?.sampleRate ?? A, e.inputSampleRate ?? A);
771
- i.sendRealtimeInput({ audio: {
789
+ let r = P(n, s?.sampleRate ?? A, e.inputSampleRate ?? A);
790
+ a.sendRealtimeInput({ audio: {
772
791
  data: F(r),
773
792
  mimeType: `audio/pcm;rate=${e.inputSampleRate ?? A}`
774
793
  } });
775
794
  });
776
795
  }
777
796
  async function z(e) {
778
- _ = !0, C(), d && clearInterval(d), d = null, T(), c?.disconnect(), s?.disconnect(), l?.disconnect(), u?.disconnect(), c = null, s = null, l = null, u = null, a?.getTracks().forEach((e) => e.stop()), a = null, o && o.state !== "closed" && await o.close(), o = null, i = null, p = !1, g = !1, h = 0, e && S("idle"), _ = !1;
797
+ v = !0, C(), f && clearInterval(f), f = null, T(), l?.disconnect(), c?.disconnect(), u?.disconnect(), d?.disconnect(), l = null, c = null, u = null, d = null, o?.getTracks().forEach((e) => e.stop()), o = null, s && s.state !== "closed" && await s.close(), s = null, a = null, m = !1, _ = !1, g = 0, e && S("idle"), v = !1;
779
798
  }
780
799
  return {
781
800
  subscribe(e) {
782
- return t.add(e), e(r), () => t.delete(e);
801
+ return t.add(e), e(i), () => t.delete(e);
783
802
  },
784
803
  async start() {
785
- if (!i) {
786
- S("connecting"), _ = !1;
804
+ if (!a) {
805
+ S("connecting"), v = !1;
787
806
  try {
788
807
  let t = e.getUserMedia ?? ((e) => navigator.mediaDevices.getUserMedia(e)), n = e.createAudioContext ?? (() => new window.AudioContext());
789
- a = await t(e.mediaStreamConstraints ?? { audio: !0 }), o = n(), o.state === "suspended" && await o.resume(), u = o.createAnalyser(), u.fftSize = 512, u.smoothingTimeConstant = .25, u.connect(o.destination), k(), i = await e.connect({
808
+ o = await t(e.mediaStreamConstraints ?? { audio: !0 }), s = n(), s.state === "suspended" && await s.resume(), d = s.createAnalyser(), d.fftSize = 512, d.smoothingTimeConstant = .25, d.connect(s.destination), k(), a = await e.connect({
790
809
  onopen: () => S("listening"),
791
810
  onmessage: O,
792
811
  onerror: (e) => S("error", e),
793
812
  onclose: () => {
794
- _ || S("idle");
813
+ v || S("idle");
795
814
  }
796
815
  }), R(), S("listening");
797
816
  } catch (e) {
798
- throw i?.close(), await z(!1), S("error", e), e;
817
+ throw a?.close(), await z(!1), S("error", e), e;
799
818
  }
800
819
  }
801
820
  },
802
821
  async stop() {
803
- _ = !0;
822
+ v = !0;
804
823
  try {
805
- p && v() && i?.sendRealtimeInput({ activityEnd: {} }), i?.sendRealtimeInput({ audioStreamEnd: !0 });
824
+ m && y() && a?.sendRealtimeInput({ activityEnd: {} }), a?.sendRealtimeInput({ audioStreamEnd: !0 });
806
825
  } catch {}
807
- i?.close(), await z(!0);
826
+ a?.close(), await z(!0);
808
827
  }
809
828
  };
810
829
  }
811
830
  //#endregion
812
- export { g as DEFAULT_OUTPUT_VOLUME_CALIBRATION, i as createElevenLabsAdapter, R as createGeminiLiveAdapter, e as createLiveKitAdapter, k as createOpenAIRealtimeAdapter, h as createPipecatAdapter, r as createVapiAdapter };
831
+ export { t as DEFAULT_OUTPUT_VOLUME_CALIBRATION, s as createElevenLabsAdapter, R as createGeminiLiveAdapter, r as createLiveKitAdapter, k as createOpenAIRealtimeAdapter, b as createPipecatAdapter, o as createVapiAdapter };