orb-ui 0.2.4 → 0.4.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/adapters.js CHANGED
@@ -20,10 +20,12 @@ function t(e) {
20
20
  };
21
21
  }
22
22
  function n(n, r) {
23
- let i = 0;
24
- function a(t) {
25
- let n = t < e ? 0 : (t - e) / (1 - e), r = n > i ? .8 : .5;
26
- return i += (n - i) * r, i;
23
+ let i = /* @__PURE__ */ new Set(), a = null;
24
+ function o() {
25
+ a || (a = n.start.bind(n), n.start = async (...e) => (i.forEach((e) => e()), a(...e)));
26
+ }
27
+ function s() {
28
+ i.size > 0 || !a || (n.start = a, a = null);
27
29
  }
28
30
  return {
29
31
  async start() {
@@ -32,33 +34,70 @@ function n(n, r) {
32
34
  stop() {
33
35
  n.stop();
34
36
  },
35
- subscribe({ onStateChange: e, onVolumeChange: r }) {
36
- let { emitState: o, clearTimer: s } = t(e), c = "idle", l = !1, u = () => {
37
- l = !0, c = "listening", o("listening");
38
- }, d = () => {
39
- l = !1, c = "idle", y(), o("idle"), r(0), i = 0;
40
- }, f = () => {
41
- l && (c = "speaking", o("speaking"), v());
42
- }, p = () => {
43
- l && (y(), c = "listening", o("listening"));
44
- }, m = 0, h = 0, g = 0, _ = () => {
45
- c === "speaking" && (h += (m - h) * .1, r(h)), g = requestAnimationFrame(_);
37
+ subscribe(r) {
38
+ let a = {
39
+ state: "idle",
40
+ volume: 0,
41
+ outputVolume: 0
42
+ }, c = 0;
43
+ function l(t) {
44
+ let n = t < e ? 0 : (t - e) / (1 - e), r = n > c ? .8 : .5;
45
+ return c += (n - c) * r, c;
46
+ }
47
+ function u(e) {
48
+ a = e, r(e);
49
+ }
50
+ function d(e) {
51
+ u({
52
+ ...a,
53
+ ...e,
54
+ state: e.state ?? a.state
55
+ });
56
+ }
57
+ let { emitState: f, clearTimer: p } = t((e) => d({ state: e })), m = () => f("connecting"), h = "idle", g = !1, _ = () => {
58
+ g = !0, h = "listening", f("listening"), d({
59
+ volume: 0,
60
+ outputVolume: 0
61
+ });
46
62
  }, v = () => {
47
- g ||= requestAnimationFrame(_);
63
+ g = !1, h = "idle", E(), f("idle"), d({
64
+ volume: 0,
65
+ outputVolume: 0
66
+ }), c = 0;
48
67
  }, y = () => {
49
- g &&= (cancelAnimationFrame(g), 0), h = 0, m = 0;
50
- }, b = (e) => {
51
- if (c === "speaking") {
52
- let t = a(e);
53
- m = t / (t + .3);
68
+ g && (h = "speaking", f("speaking"), d({
69
+ volume: 0,
70
+ outputVolume: 0
71
+ }), T());
72
+ }, b = () => {
73
+ g && (E(), h = "listening", d({
74
+ volume: 0,
75
+ outputVolume: 0
76
+ }), c = 0, f("listening"));
77
+ }, x = 0, S = 0, C = 0, w = () => {
78
+ h === "speaking" && (S += (x - S) * .1, d({
79
+ volume: S,
80
+ outputVolume: S
81
+ })), C = requestAnimationFrame(w);
82
+ }, T = () => {
83
+ C ||= requestAnimationFrame(w);
84
+ }, E = () => {
85
+ C &&= (cancelAnimationFrame(C), 0), S = 0, x = 0;
86
+ }, D = (e) => {
87
+ if (h === "speaking") {
88
+ let t = l(e);
89
+ x = t / (t + .3);
54
90
  }
55
- }, x = () => {}, S = (e) => {
56
- console.error("[orb-ui/vapi] Error:", e), y(), o("error"), r(0), i = 0;
91
+ }, O = () => {}, k = (e) => {
92
+ console.error("[orb-ui/vapi] Error:", e), g = !1, h = "error", p(), E(), d({
93
+ state: "error",
94
+ volume: 0,
95
+ outputVolume: 0,
96
+ error: e
97
+ }), c = 0;
57
98
  };
58
- n.on("call-start", u), n.on("call-end", d), n.on("speech-start", f), n.on("speech-end", p), n.on("volume-level", b), n.on("message", x), n.on("error", S);
59
- let C = n.start.bind(n);
60
- return n.start = async (...e) => (o("connecting"), C(...e)), () => {
61
- s(), n.removeListener("call-start", u), n.removeListener("call-end", d), n.removeListener("speech-start", f), n.removeListener("speech-end", p), n.removeListener("volume-level", b), n.removeListener("message", x), n.removeListener("error", S), n.start = C;
99
+ return n.on("call-start", _), n.on("call-end", v), n.on("speech-start", y), n.on("speech-end", b), n.on("volume-level", D), n.on("message", O), n.on("error", k), i.add(m), o(), () => {
100
+ p(), E(), c = 0, n.removeListener("call-start", _), n.removeListener("call-end", v), n.removeListener("speech-start", y), n.removeListener("speech-end", b), n.removeListener("volume-level", D), n.removeListener("message", O), n.removeListener("error", k), i.delete(m), s();
62
101
  };
63
102
  }
64
103
  };
@@ -66,68 +105,946 @@ function n(n, r) {
66
105
  //#endregion
67
106
  //#region src/adapters/elevenlabs/index.ts
68
107
  function r(e, t) {
69
- let n = null, r = null, i = /* @__PURE__ */ new Set();
70
- function a(e) {
71
- i.forEach((t) => t.onStateChange(e));
108
+ let n = null, r = 0, i = null, a = null, o = {
109
+ state: "idle",
110
+ volume: 0,
111
+ inputVolume: 0,
112
+ outputVolume: 0
113
+ }, s = "idle", c = /* @__PURE__ */ new Set();
114
+ function l(e) {
115
+ o = e, c.forEach((t) => t(e));
72
116
  }
73
- function o(e) {
74
- i.forEach((t) => t.onVolumeChange(e));
117
+ function u(e) {
118
+ o = {
119
+ ...o,
120
+ ...e,
121
+ state: e.state ?? o.state
122
+ }, l(o);
75
123
  }
76
- function s() {
77
- r ||= setInterval(() => {
78
- if (!n) return;
79
- let e = n.getOutputVolume();
80
- o(Math.min(e * 2, 1));
124
+ function d(e) {
125
+ s = e, u({ state: e });
126
+ }
127
+ function f() {
128
+ a ||= setInterval(() => {
129
+ if (n) {
130
+ if (s === "listening") {
131
+ let e = Math.min(n.getInputVolume() * 2, 1);
132
+ u({
133
+ volume: e,
134
+ inputVolume: e
135
+ });
136
+ } else if (s === "speaking") {
137
+ let e = Math.min(n.getOutputVolume() * 2, 1);
138
+ u({
139
+ volume: e,
140
+ outputVolume: e
141
+ });
142
+ }
143
+ }
81
144
  }, 33);
82
145
  }
83
- function c() {
84
- r &&= (clearInterval(r), null), o(0);
146
+ function p() {
147
+ a &&= (clearInterval(a), null);
85
148
  }
86
- let l = {
87
- onStatusChange: ({ status: e }) => {
88
- e === "connecting" && a("connecting");
89
- },
90
- onConnect: () => {
91
- a("listening");
92
- },
93
- onModeChange: ({ mode: e }) => {
94
- e === "speaking" ? (a("speaking"), s()) : (a("listening"), c());
149
+ function m() {
150
+ p(), u({
151
+ volume: 0,
152
+ inputVolume: 0,
153
+ outputVolume: 0
154
+ });
155
+ }
156
+ function h() {
157
+ s = "idle", u({
158
+ state: "idle",
159
+ volume: 0,
160
+ inputVolume: 0,
161
+ outputVolume: 0
162
+ });
163
+ }
164
+ function g(e) {
165
+ return e === r;
166
+ }
167
+ function _(e) {
168
+ return {
169
+ onStatusChange: ({ status: t }) => {
170
+ g(e) && t === "connecting" && d("connecting");
171
+ },
172
+ onConnect: () => {
173
+ g(e) && (d("listening"), f());
174
+ },
175
+ onModeChange: ({ mode: t }) => {
176
+ g(e) && (t === "speaking" ? (d("speaking"), f()) : (d("listening"), f()));
177
+ },
178
+ onDisconnect: () => {
179
+ g(e) && (p(), n = null, h());
180
+ },
181
+ onError: (t) => {
182
+ g(e) && (console.error("[orb-ui/elevenlabs] Error:", t), p(), s = "error", u({
183
+ state: "error",
184
+ volume: 0,
185
+ inputVolume: 0,
186
+ outputVolume: 0,
187
+ error: t
188
+ }), n = null);
189
+ }
190
+ };
191
+ }
192
+ return {
193
+ subscribe(e) {
194
+ return c.add(e), e(o), n && (s === "listening" || s === "speaking") && f(), () => {
195
+ c.delete(e), c.size === 0 && m();
196
+ };
95
197
  },
96
- onDisconnect: () => {
97
- c(), a("idle"), o(0), n = null;
198
+ async start() {
199
+ if (n || i) return i ?? void 0;
200
+ r += 1;
201
+ let a = r;
202
+ return i = (async () => {
203
+ try {
204
+ let r = await e.startSession({
205
+ ...t,
206
+ ..._(a)
207
+ });
208
+ if (!g(a)) {
209
+ await r.endSession().catch((e) => {
210
+ console.error("[orb-ui/elevenlabs] stale session cleanup failed:", e);
211
+ });
212
+ return;
213
+ }
214
+ n = r;
215
+ } catch (e) {
216
+ if (!g(a)) return;
217
+ console.error("[orb-ui/elevenlabs] startSession failed:", e), d("error"), u({
218
+ volume: 0,
219
+ inputVolume: 0,
220
+ outputVolume: 0,
221
+ error: e
222
+ });
223
+ } finally {
224
+ i = null;
225
+ }
226
+ })(), i;
98
227
  },
99
- onError: (e) => {
100
- console.error("[orb-ui/elevenlabs] Error:", e), c(), a("error"), o(0), n = null;
228
+ async stop() {
229
+ let e = n;
230
+ if (!e) {
231
+ i && (r += 1, p(), h(), await i);
232
+ return;
233
+ }
234
+ let t = r;
235
+ p();
236
+ try {
237
+ await e.endSession();
238
+ } catch (e) {
239
+ console.error("[orb-ui/elevenlabs] endSession failed:", e);
240
+ }
241
+ n === e && g(t) && (n = null, h());
101
242
  }
102
243
  };
244
+ }
245
+ //#endregion
246
+ //#region src/adapters/types.ts
247
+ function i(e) {
248
+ let t = Math.min(e / .5, 1);
249
+ return t **= 1.3, t;
250
+ }
251
+ //#endregion
252
+ //#region src/adapters/livekit/index.ts
253
+ var a = 4, o = .03, s = 1.5, c = .6, l = .2, u = .35;
254
+ function d(e) {
255
+ switch (e) {
256
+ case "speaking": return "speaking";
257
+ case "thinking": return "thinking";
258
+ case "listening":
259
+ case "idle": return "listening";
260
+ case "connecting":
261
+ case "pre-connect-buffering":
262
+ case "initializing": return "connecting";
263
+ case "disconnected": return "idle";
264
+ case "failed": return "error";
265
+ default: return "idle";
266
+ }
267
+ }
268
+ function f(e) {
269
+ return "RoomClass" in e;
270
+ }
271
+ function p(e) {
272
+ return typeof e == "function" ? e() : e;
273
+ }
274
+ function m(e) {
275
+ if (!e) return {};
276
+ let t = {}, n = (e, n) => {
277
+ n !== void 0 && (t[e] = n);
278
+ };
279
+ return n("roomName", p(e.roomName)), n("participantName", p(e.participantName)), n("participantIdentity", p(e.participantIdentity)), n("participantMetadata", p(e.participantMetadata)), n("participantAttributes", typeof e.participantAttributes == "function" ? e.participantAttributes() : e.participantAttributes), n("agentName", p(e.agentName)), n("agentMetadata", p(e.agentMetadata)), n("deployment", p(e.deployment)), t;
280
+ }
281
+ function h(e) {
282
+ if (!e.serverUrl || !e.participantToken) throw Error("[orb-ui/livekit] connection details must include serverUrl and participantToken.");
283
+ return e;
284
+ }
285
+ async function g(e) {
286
+ let t = m(e.tokenOptions);
287
+ return typeof e.getConnectionDetails == "function" ? h(await e.getConnectionDetails(t)) : e.tokenSource ? h(await e.tokenSource.fetch(t)) : h({
288
+ serverUrl: e.serverUrl,
289
+ participantToken: e.participantToken
290
+ });
291
+ }
292
+ function _(e) {
293
+ for (let [, t] of e.remoteParticipants) if (!t.isLocal && (t.kind === a || t.attributes?.["lk.agent.state"] !== void 0)) return t;
294
+ return null;
295
+ }
296
+ function v(e, t, n) {
297
+ let r = e.off;
298
+ if (r) {
299
+ r.call(e, t, n);
300
+ return;
301
+ }
302
+ e.removeListener?.call(e, t, n);
303
+ }
304
+ function y(e, t, n) {
305
+ e.on.call(e, t, n);
306
+ }
307
+ function b(e) {
308
+ let t = f(e), n = e.createAudioAnalyser, r = { current: t ? null : e.room }, p = /* @__PURE__ */ new Set(), m = {
309
+ state: "idle",
310
+ volume: 0,
311
+ inputVolume: 0,
312
+ outputVolume: 0
313
+ }, h = "idle", b = null, x = null, S = null, C = null, w = null, T = null, E = null, D = 0, O = null;
314
+ function k(e) {
315
+ m = e, p.forEach((t) => t(e));
316
+ }
317
+ function A(e) {
318
+ k({
319
+ ...m,
320
+ ...e,
321
+ state: e.state ?? m.state
322
+ });
323
+ }
324
+ function j(e) {
325
+ let t = e < o ? 0 : e, n = t > D ? c : l;
326
+ D += (t - D) * n;
327
+ let r = Math.min(D * s, 1);
328
+ return r / (r + u);
329
+ }
330
+ function M() {
331
+ D = 0, A({
332
+ volume: 0,
333
+ outputVolume: 0
334
+ });
335
+ }
336
+ function N({ emitZero: e = !0 } = {}) {
337
+ w &&= (clearInterval(w), null), C &&= (C.cleanup().catch(() => {}), null), e && A({
338
+ volume: 0,
339
+ inputVolume: 0
340
+ });
341
+ }
342
+ function P(e) {
343
+ if (!(b === e && C)) {
344
+ N({ emitZero: !1 }), b = e;
345
+ try {
346
+ C = n(e);
347
+ } catch (e) {
348
+ console.warn("[orb-ui/livekit] local createAudioAnalyser failed:", e);
349
+ return;
350
+ }
351
+ w = setInterval(() => {
352
+ if (!C || h !== "listening") return;
353
+ let e = i(C.calculateVolume());
354
+ A({
355
+ volume: e,
356
+ inputVolume: e,
357
+ outputVolume: 0
358
+ });
359
+ }, 33);
360
+ }
361
+ }
362
+ function F({ emitZero: e = !0 } = {}) {
363
+ E &&= (clearInterval(E), null), T &&= (T.cleanup().catch(() => {}), null), D = 0, e && A({
364
+ volume: 0,
365
+ outputVolume: 0
366
+ });
367
+ }
368
+ function I(e) {
369
+ F();
370
+ try {
371
+ T = n(e);
372
+ } catch (e) {
373
+ console.warn("[orb-ui/livekit] remote createAudioAnalyser failed:", e);
374
+ return;
375
+ }
376
+ E = setInterval(() => {
377
+ if (!T || h !== "speaking") return;
378
+ let e = j(T.calculateVolume());
379
+ A({
380
+ volume: e,
381
+ inputVolume: 0,
382
+ outputVolume: e
383
+ });
384
+ }, 33);
385
+ }
386
+ function L(e) {
387
+ h = e, A({
388
+ state: e,
389
+ volume: 0,
390
+ inputVolume: 0,
391
+ outputVolume: 0
392
+ }), e === "speaking" && x ? I(x) : e !== "speaking" && F({ emitZero: !1 });
393
+ }
394
+ function R(e) {
395
+ S || typeof document > "u" || (S = e.attach(), S.id = "orb-ui-livekit-agent-audio", document.body.appendChild(S));
396
+ }
397
+ function z() {
398
+ S &&= (S.remove(), null);
399
+ }
400
+ function B(e) {
401
+ x = e, R(e), h === "speaking" && I(e);
402
+ }
403
+ function V(e) {
404
+ P(e);
405
+ }
406
+ function H(e) {
407
+ let t = e.getTrackPublication?.("microphone");
408
+ t?.track && V(t.track);
409
+ }
410
+ function U(e) {
411
+ let t = d(e);
412
+ t !== h && L(t);
413
+ }
414
+ function W(e) {
415
+ let t = e.attributes?.["lk.agent.state"];
416
+ t && U(t);
417
+ let n = e.getTrackPublication("microphone");
418
+ n?.track && B(n.track);
419
+ }
420
+ function G(e) {
421
+ if (O) return;
422
+ let t = (e, t) => {
423
+ if (t.isLocal) return;
424
+ let n = e["lk.agent.state"];
425
+ n && U(n);
426
+ }, n = (e, t, n) => {
427
+ n.isLocal || !(n.kind === a || n.attributes?.["lk.agent.state"] !== void 0) || e.mediaStreamTrack.kind !== "audio" || B(e);
428
+ }, r = (e) => {
429
+ x === e && (x = null, z(), F());
430
+ }, i = (e) => {
431
+ e.source !== "microphone" || !e.track || V(e.track);
432
+ }, o = (e) => {
433
+ e.source === "microphone" && (b = null, N());
434
+ }, s = () => {
435
+ b = null, x = null, z(), N({ emitZero: !1 }), F({ emitZero: !1 }), L("idle");
436
+ };
437
+ if (y(e, "participantAttributesChanged", t), y(e, "trackSubscribed", n), y(e, "trackUnsubscribed", r), y(e, "localTrackPublished", i), y(e, "localTrackUnpublished", o), y(e, "disconnected", s), O = () => {
438
+ v(e, "participantAttributesChanged", t), v(e, "trackSubscribed", n), v(e, "trackUnsubscribed", r), v(e, "localTrackPublished", i), v(e, "localTrackUnpublished", o), v(e, "disconnected", s);
439
+ }, H(e.localParticipant), e.state === "connected") {
440
+ let t = _(e);
441
+ t ? W(t) : L("connecting");
442
+ } else L("idle");
443
+ }
444
+ function K() {
445
+ O?.(), O = null, b = null, x = null, z(), N({ emitZero: !1 }), F({ emitZero: !1 }), A({
446
+ volume: 0,
447
+ inputVolume: 0,
448
+ outputVolume: 0
449
+ });
450
+ }
451
+ let q = (e) => {
452
+ p.add(e), e(m);
453
+ let t = r.current;
454
+ return t && G(t), () => {
455
+ p.delete(e), p.size === 0 && K();
456
+ };
457
+ };
458
+ return t ? {
459
+ subscribe: q,
460
+ async start() {
461
+ if (r.current) return;
462
+ let t = e, n = new t.RoomClass();
463
+ L("connecting");
464
+ try {
465
+ let e = await g(t);
466
+ if (await n.connect(e.serverUrl, e.participantToken, t.connectOptions), r.current = n, G(n), t.enableMicrophone !== !1) {
467
+ let e = (await n.localParticipant.setMicrophoneEnabled(!0))?.track ?? n.localParticipant.getTrackPublication?.("microphone")?.track;
468
+ e && V(e);
469
+ }
470
+ } catch (e) {
471
+ throw console.error("[orb-ui/livekit] connect failed:", e), r.current === n && K(), n.disconnect(), r.current = null, L("error"), M(), A({ error: e }), e;
472
+ }
473
+ },
474
+ async stop() {
475
+ let e = r.current;
476
+ if (!e) {
477
+ L("idle");
478
+ return;
479
+ }
480
+ K(), e.disconnect(), r.current = null, L("idle");
481
+ }
482
+ } : { subscribe: q };
483
+ }
484
+ //#endregion
485
+ //#region src/adapters/pipecat/index.ts
486
+ var x = {
487
+ connected: "connected",
488
+ disconnected: "disconnected",
489
+ transportStateChanged: "transportStateChanged",
490
+ botReady: "botReady",
491
+ botDisconnected: "botDisconnected",
492
+ error: "error",
493
+ messageError: "messageError",
494
+ deviceError: "deviceError",
495
+ localAudioLevel: "localAudioLevel",
496
+ remoteAudioLevel: "remoteAudioLevel",
497
+ botStartedSpeaking: "botStartedSpeaking",
498
+ botStoppedSpeaking: "botStoppedSpeaking",
499
+ userStartedSpeaking: "userStartedSpeaking",
500
+ userStoppedSpeaking: "userStoppedSpeaking",
501
+ botLlmStarted: "botLlmStarted",
502
+ botTtsStarted: "botTtsStarted"
503
+ }, S = .002, C = 4, w = .8, T = .6, E = .2;
504
+ function D(e) {
505
+ switch (e) {
506
+ case "initializing":
507
+ case "initialized":
508
+ case "authenticating":
509
+ case "authenticated":
510
+ case "connecting":
511
+ case "connected": return "connecting";
512
+ case "ready": return "listening";
513
+ case "disconnected":
514
+ case "disconnecting": return "idle";
515
+ case "error": return "error";
516
+ default: return;
517
+ }
518
+ }
519
+ function O(e) {
520
+ if (typeof e != "number" || !Number.isFinite(e) || e <= S) return 0;
521
+ let t = Math.min(1, Math.max(0, e - S));
522
+ return Math.min(1, t * C) ** +w;
523
+ }
524
+ function k(e, t) {
525
+ let n = O(e), r = n > t ? T : E;
526
+ return t + (n - t) * r;
527
+ }
528
+ function A(e) {
529
+ return typeof e == "object" && !!e && "kind" in e && typeof e.kind == "string";
530
+ }
531
+ function j(e, t = {}) {
532
+ let n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map(), i = {
533
+ state: D(e.state ?? "") ?? "idle",
534
+ volume: 0,
535
+ inputVolume: 0,
536
+ outputVolume: 0
537
+ }, a = !1, o = 0, s = 0;
538
+ function c(e) {
539
+ i = e, n.forEach((t) => t(e));
540
+ }
541
+ function l(e, t) {
542
+ o = 0, s = 0, c({
543
+ state: e,
544
+ volume: 0,
545
+ inputVolume: 0,
546
+ outputVolume: 0,
547
+ ...t === void 0 ? {} : { error: t }
548
+ });
549
+ }
550
+ function u(e) {
551
+ if (i.state !== "listening") return;
552
+ o = k(e, o);
553
+ let t = o;
554
+ c({
555
+ ...i,
556
+ volume: t,
557
+ inputVolume: t,
558
+ outputVolume: 0
559
+ });
560
+ }
561
+ function d(e, n) {
562
+ if (i.state !== "speaking") return;
563
+ if (n && typeof n == "object") {
564
+ let e = n;
565
+ if (e.local || t.isBotParticipant && !t.isBotParticipant(e)) return;
566
+ }
567
+ s = k(e, s);
568
+ let r = s;
569
+ c({
570
+ ...i,
571
+ volume: r,
572
+ inputVolume: 0,
573
+ outputVolume: r
574
+ });
575
+ }
576
+ function f(e) {
577
+ if (!e || typeof e != "object") return !0;
578
+ let n = e;
579
+ return n.local ? !1 : t.isBotParticipant?.(n) ?? !0;
580
+ }
581
+ function p(e, n) {
582
+ if (t.playRemoteAudio === !1 || !A(e) || e.kind !== "audio" || !f(n)) return;
583
+ let i = t.createAudioElement?.() ?? new Audio();
584
+ i.autoplay = !0, i.srcObject = new MediaStream([e]), r.set(e, i), i.play().catch(() => void 0);
585
+ }
586
+ function m(e) {
587
+ if (!A(e)) return;
588
+ let t = r.get(e);
589
+ t && (t.pause(), t.srcObject = null, t.remove(), r.delete(e));
590
+ }
591
+ function h() {
592
+ r.forEach((e) => {
593
+ e.pause(), e.srcObject = null, e.remove();
594
+ }), r.clear();
595
+ }
596
+ let g = [
597
+ [x.connected, () => l("connecting")],
598
+ [x.botReady, () => l("listening")],
599
+ [x.disconnected, () => l("idle")],
600
+ [x.botDisconnected, () => l("idle")],
601
+ [x.transportStateChanged, (e) => {
602
+ if (typeof e != "string") return;
603
+ let t = D(e);
604
+ t && l(t);
605
+ }],
606
+ [x.userStartedSpeaking, () => l("listening")],
607
+ [x.userStoppedSpeaking, () => {
608
+ i.state === "listening" && l("thinking");
609
+ }],
610
+ [x.botLlmStarted, () => l("thinking")],
611
+ [x.botTtsStarted, () => l("thinking")],
612
+ [x.botStartedSpeaking, () => l("speaking")],
613
+ [x.botStoppedSpeaking, () => l("listening")],
614
+ [x.localAudioLevel, u],
615
+ [x.remoteAudioLevel, d],
616
+ ["trackStarted", p],
617
+ ["trackStopped", m],
618
+ [x.error, (e) => l("error", e)],
619
+ [x.messageError, (e) => l("error", e)],
620
+ [x.deviceError, (e) => l("error", e)]
621
+ ];
622
+ function _() {
623
+ a || (a = !0, g.forEach(([t, n]) => e.on(t, n)));
624
+ }
625
+ function v() {
626
+ a && (a = !1, g.forEach(([t, n]) => {
627
+ e.off ? e.off(t, n) : e.removeListener?.(t, n);
628
+ }), h());
629
+ }
103
630
  return {
104
631
  subscribe(e) {
105
- return i.add(e), () => {
106
- i.delete(e), i.size === 0 && c();
632
+ return n.add(e), _(), e(i), () => {
633
+ n.delete(e), n.size === 0 && v();
107
634
  };
108
635
  },
109
636
  async start() {
110
- if (!n) try {
111
- n = await e.startSession({
112
- ...t,
113
- ...l
114
- });
637
+ l("connecting");
638
+ try {
639
+ await (t.connect ? t.connect() : e.connect());
115
640
  } catch (e) {
116
- console.error("[orb-ui/elevenlabs] startSession failed:", e), a("error"), o(0);
641
+ throw l("error", e), e;
117
642
  }
118
643
  },
119
644
  async stop() {
120
- if (n) {
121
- c();
645
+ try {
646
+ await (t.disconnect ? t.disconnect() : e.disconnect());
647
+ } finally {
648
+ h(), l("idle");
649
+ }
650
+ }
651
+ };
652
+ }
653
+ //#endregion
654
+ //#region src/adapters/audio-level.ts
655
+ var M = {
656
+ noiseFloor: 0,
657
+ gain: 4,
658
+ exponent: .8,
659
+ attack: 1,
660
+ release: 1
661
+ };
662
+ function N(e, t, n) {
663
+ return Math.min(n, Math.max(t, e));
664
+ }
665
+ function P(e, t) {
666
+ return typeof e == "number" && Number.isFinite(e) ? e : t;
667
+ }
668
+ function F(e) {
669
+ let t = typeof e == "function" ? e() : e;
670
+ return {
671
+ noiseFloor: N(P(t?.noiseFloor, M.noiseFloor), 0, 1),
672
+ gain: Math.max(0, P(t?.gain, M.gain)),
673
+ exponent: Math.max(.01, P(t?.exponent, M.exponent)),
674
+ attack: N(P(t?.attack, M.attack), 0, 1),
675
+ release: N(P(t?.release, M.release), 0, 1)
676
+ };
677
+ }
678
+ function I(e, t, n) {
679
+ let r = Number.isFinite(e) ? N(e, 0, 1) : 0, i = F(n), a = N((r <= i.noiseFloor ? 0 : r - i.noiseFloor) * i.gain, 0, 1) ** +i.exponent, o = a > t ? i.attack : i.release;
680
+ return {
681
+ raw: r,
682
+ shaped: a,
683
+ normalized: N(t + (a - t) * o, 0, 1)
684
+ };
685
+ }
686
+ //#endregion
687
+ //#region src/adapters/openai-realtime/index.ts
688
+ var L = "https://api.openai.com/v1/realtime/calls", R = .015, z = 8, B = {
689
+ noiseFloor: .003,
690
+ gain: 4,
691
+ exponent: .8,
692
+ attack: .55,
693
+ release: .1
694
+ };
695
+ function V() {
696
+ let e = window.AudioContext;
697
+ return e ? new e() : void 0;
698
+ }
699
+ function H(e) {
700
+ return Math.min(1, Math.max(0, (e * 4) ** .8));
701
+ }
702
+ function U(e, t, n) {
703
+ try {
704
+ let r = t();
705
+ if (!r) return;
706
+ let i = r.createMediaStreamSource(e), a = r.createAnalyser();
707
+ a.fftSize = 512, a.smoothingTimeConstant = .25, i.connect(a);
708
+ let o = new Float32Array(a.fftSize), s = setInterval(() => {
709
+ a.getFloatTimeDomainData(o);
710
+ let e = 0;
711
+ for (let t of o) e += t * t;
712
+ n(Math.sqrt(e / o.length));
713
+ }, 33);
714
+ return { async stop() {
715
+ clearInterval(s), i.disconnect(), a.disconnect(), r.state !== "closed" && await r.close();
716
+ } };
717
+ } catch {
718
+ return;
719
+ }
720
+ }
721
+ function W(e) {
722
+ let t = typeof e == "string" ? e : e.value;
723
+ if (!t) throw Error("[orb-ui/openai-realtime] getClientSecret returned an empty value.");
724
+ return t;
725
+ }
726
+ function G(e) {
727
+ let t = /* @__PURE__ */ new Set(), n = {
728
+ state: "idle",
729
+ volume: 0,
730
+ inputVolume: 0,
731
+ outputVolume: 0
732
+ }, r = null, i = null, a = null, o = null, s, c, l = 0, u = 0, d = !1;
733
+ function f() {
734
+ let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
735
+ return {
736
+ ...B,
737
+ ...t
738
+ };
739
+ }
740
+ function p(e) {
741
+ n = e, t.forEach((t) => t(e));
742
+ }
743
+ function m(e, t) {
744
+ n.state === e && t === void 0 || (e !== "speaking" && (u = 0), p({
745
+ state: e,
746
+ volume: 0,
747
+ inputVolume: 0,
748
+ outputVolume: 0,
749
+ ...t === void 0 ? {} : { error: t }
750
+ }));
751
+ }
752
+ function h(e) {
753
+ if (n.state !== "listening") return;
754
+ let t = H(e);
755
+ p({
756
+ ...n,
757
+ volume: t,
758
+ inputVolume: t,
759
+ outputVolume: 0
760
+ });
761
+ }
762
+ function g(t) {
763
+ let r = I(t, u, f);
764
+ u = r.normalized, e.onOutputVolumeSample?.(r);
765
+ let i = r.normalized;
766
+ if (i > R) l = 0, n.state !== "speaking" && m("speaking");
767
+ else if (n.state === "speaking" && (l += 1, l >= z)) {
768
+ l = 0, m("listening");
769
+ return;
770
+ }
771
+ n.state === "speaking" && p({
772
+ ...n,
773
+ volume: i,
774
+ inputVolume: 0,
775
+ outputVolume: i
776
+ });
777
+ }
778
+ function _(e) {
779
+ switch (e.type) {
780
+ case "session.created":
781
+ case "session.updated":
782
+ m("listening");
783
+ break;
784
+ case "input_audio_buffer.speech_started":
785
+ m("listening");
786
+ break;
787
+ case "input_audio_buffer.speech_stopped":
788
+ case "response.created":
789
+ m("thinking");
790
+ break;
791
+ case "response.output_audio.delta":
792
+ case "output_audio_buffer.started":
793
+ m("speaking");
794
+ break;
795
+ case "output_audio_buffer.stopped":
796
+ m("listening");
797
+ break;
798
+ case "response.done":
799
+ case "response.cancelled":
800
+ n.state !== "speaking" && m("listening");
801
+ break;
802
+ case "error":
803
+ m("error", e.error ?? e);
804
+ break;
805
+ }
806
+ }
807
+ async function v(e) {
808
+ 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;
809
+ }
810
+ return {
811
+ subscribe(e) {
812
+ return t.add(e), e(n), () => t.delete(e);
813
+ },
814
+ async start() {
815
+ if (!r) {
816
+ m("connecting"), d = !1;
122
817
  try {
123
- await n.endSession();
818
+ 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 ?? V, p = W(await e.getClientSecret());
819
+ a = await t(e.mediaStreamConstraints ?? { audio: !0 }), s = U(a, f, h);
820
+ let v = n();
821
+ r = v, a.getAudioTracks().forEach((e) => v.addTrack(e, a)), o = u(), o.autoplay = !0, v.ontrack = (e) => {
822
+ let t = e.streams[0] ?? new MediaStream([e.track]);
823
+ o && (o.srcObject = t), o?.play().catch(() => void 0), c?.stop(), c = U(t, f, g);
824
+ }, v.onconnectionstatechange = () => {
825
+ d || (v.connectionState === "failed" ? m("error", /* @__PURE__ */ Error("[orb-ui/openai-realtime] WebRTC connection failed.")) : (v.connectionState === "disconnected" || v.connectionState === "closed") && m("idle"));
826
+ };
827
+ let y = v.createDataChannel("oai-events");
828
+ i = y, y.onopen = () => m("listening"), y.onmessage = (e) => {
829
+ try {
830
+ _(JSON.parse(String(e.data)));
831
+ } catch {}
832
+ }, y.onerror = (e) => m("error", e), y.onclose = () => {
833
+ d || m("idle");
834
+ };
835
+ let b = await v.createOffer();
836
+ await v.setLocalDescription(b);
837
+ let x = await l(e.callsUrl ?? L, {
838
+ method: "POST",
839
+ body: b.sdp,
840
+ headers: {
841
+ Authorization: `Bearer ${p}`,
842
+ "Content-Type": "application/sdp"
843
+ }
844
+ });
845
+ if (!x.ok) {
846
+ let e = await x.text();
847
+ throw Error(`[orb-ui/openai-realtime] Session negotiation failed (${x.status}): ${e}`);
848
+ }
849
+ await v.setRemoteDescription({
850
+ type: "answer",
851
+ sdp: await x.text()
852
+ });
124
853
  } catch (e) {
125
- console.error("[orb-ui/elevenlabs] endSession failed:", e);
854
+ throw await v(!1), m("error", e), e;
126
855
  }
127
- n = null;
128
856
  }
857
+ },
858
+ async stop() {
859
+ await v(!0);
860
+ }
861
+ };
862
+ }
863
+ //#endregion
864
+ //#region src/adapters/gemini-live/index.ts
865
+ var K = 16e3, q = {
866
+ noiseFloor: .003,
867
+ gain: 4,
868
+ exponent: .8,
869
+ attack: .3,
870
+ release: .1
871
+ };
872
+ function J(e) {
873
+ let t = 0;
874
+ for (let n of e) t += n * n;
875
+ return Math.sqrt(t / e.length);
876
+ }
877
+ function Y(e) {
878
+ return Math.min(1, Math.max(0, (J(e) * 4) ** .8));
879
+ }
880
+ function X(e, t, n) {
881
+ if (t === n) return e;
882
+ let r = t / n, i = Math.max(1, Math.round(e.length / r)), a = new Float32Array(i);
883
+ for (let t = 0; t < i; t += 1) {
884
+ let n = t * r, i = Math.floor(n), o = Math.min(e.length - 1, i + 1), s = n - i;
885
+ a[t] = e[i] * (1 - s) + e[o] * s;
886
+ }
887
+ return a;
888
+ }
889
+ function Z(e) {
890
+ let t = new Uint8Array(e.length * 2), n = new DataView(t.buffer);
891
+ e.forEach((e, t) => {
892
+ let r = Math.max(-1, Math.min(1, e));
893
+ n.setInt16(t * 2, r < 0 ? r * 32768 : r * 32767, !0);
894
+ });
895
+ let r = "";
896
+ return t.forEach((e) => {
897
+ r += String.fromCharCode(e);
898
+ }), btoa(r);
899
+ }
900
+ function Q(e) {
901
+ let t = atob(e), n = new Uint8Array(t.length);
902
+ for (let e = 0; e < t.length; e += 1) n[e] = t.charCodeAt(e);
903
+ let r = new DataView(n.buffer), i = new Float32Array(Math.floor(n.length / 2));
904
+ for (let e = 0; e < i.length; e += 1) i[e] = r.getInt16(e * 2, !0) / 32768;
905
+ return i;
906
+ }
907
+ function $(e) {
908
+ let t = e?.match(/rate=(\d+)/i);
909
+ return t ? Number(t[1]) : 24e3;
910
+ }
911
+ function ee(e) {
912
+ let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), r = {
913
+ state: "idle",
914
+ volume: 0,
915
+ inputVolume: 0,
916
+ outputVolume: 0
917
+ }, 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;
918
+ function v() {
919
+ return e.activityDetection !== "server";
920
+ }
921
+ function y() {
922
+ let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
923
+ return {
924
+ ...q,
925
+ ...t
926
+ };
927
+ }
928
+ function b(e) {
929
+ r = e, t.forEach((t) => t(e));
930
+ }
931
+ function x(e, t) {
932
+ r.state === e && t === void 0 || (e !== "speaking" && (h = 0), b({
933
+ state: e,
934
+ volume: 0,
935
+ inputVolume: 0,
936
+ outputVolume: 0,
937
+ ...t === void 0 ? {} : { error: t }
938
+ }));
939
+ }
940
+ function S() {
941
+ f &&= (clearTimeout(f), null);
942
+ }
943
+ function C(t) {
944
+ t > (e.speechThreshold ?? .04) ? (!p && v() && i?.sendRealtimeInput({ activityStart: {} }), p = !0, S(), r.state !== "listening" && x("listening")) : p && !f && (f = setTimeout(() => {
945
+ f = null, p = !1, v() && i?.sendRealtimeInput({ activityEnd: {} }), r.state === "listening" && x("thinking");
946
+ }, e.speechEndDelayMs ?? 500)), r.state === "listening" && b({
947
+ ...r,
948
+ volume: t,
949
+ inputVolume: t,
950
+ outputVolume: 0
951
+ });
952
+ }
953
+ function w() {
954
+ n.forEach((e) => {
955
+ try {
956
+ e.stop();
957
+ } catch {}
958
+ e.disconnect();
959
+ }), n.clear(), m = o?.currentTime ?? 0;
960
+ }
961
+ function T() {
962
+ g && n.size === 0 && (g = !1, x("listening"));
963
+ }
964
+ function E(e) {
965
+ if (!o || !u || !e.data) return;
966
+ let t = Q(e.data), i = o.createBuffer(1, t.length, $(e.mimeType));
967
+ i.getChannelData(0).set(t);
968
+ let a = o.createBufferSource();
969
+ a.buffer = i, a.connect(u);
970
+ let s = Math.max(o.currentTime, m);
971
+ m = s + i.duration, n.add(a), a.onended = () => {
972
+ n.delete(a), a.disconnect(), T();
973
+ }, a.start(s), r.state !== "speaking" && x("speaking");
974
+ }
975
+ function D(e) {
976
+ e.setupComplete && x("listening");
977
+ let t = e.serverContent;
978
+ if (!t) return;
979
+ if (t.interrupted) {
980
+ w(), g = !1, x("listening");
981
+ return;
982
+ }
983
+ let n = t.modelTurn?.parts?.filter((e) => e.inlineData?.data) ?? [];
984
+ t.modelTurn && n.length === 0 && r.state !== "speaking" && x("thinking"), n.forEach((e) => E(e.inlineData)), t.waitingForInput && (w(), x("listening")), (t.turnComplete || t.generationComplete) && (g = !0, T());
985
+ }
986
+ function O() {
987
+ if (!u) return;
988
+ let t = new Float32Array(u.fftSize);
989
+ d = setInterval(() => {
990
+ if (!u || r.state !== "speaking") return;
991
+ u.getFloatTimeDomainData(t);
992
+ let n = I(J(t), h, y);
993
+ h = n.normalized, e.onOutputVolumeSample?.(n);
994
+ let i = n.normalized;
995
+ b({
996
+ ...r,
997
+ volume: i,
998
+ inputVolume: 0,
999
+ outputVolume: i
1000
+ });
1001
+ }, 33);
1002
+ }
1003
+ function k() {
1004
+ !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) => {
1005
+ if (!i || _) return;
1006
+ let n = t.inputBuffer.getChannelData(0);
1007
+ C(Y(n));
1008
+ let r = X(n, o?.sampleRate ?? K, e.inputSampleRate ?? K);
1009
+ i.sendRealtimeInput({ audio: {
1010
+ data: Z(r),
1011
+ mimeType: `audio/pcm;rate=${e.inputSampleRate ?? K}`
1012
+ } });
1013
+ });
1014
+ }
1015
+ async function A(e) {
1016
+ _ = !0, S(), d && clearInterval(d), d = null, w(), 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 && x("idle"), _ = !1;
1017
+ }
1018
+ return {
1019
+ subscribe(e) {
1020
+ return t.add(e), e(r), () => t.delete(e);
1021
+ },
1022
+ async start() {
1023
+ if (!i) {
1024
+ x("connecting"), _ = !1;
1025
+ try {
1026
+ let t = e.getUserMedia ?? ((e) => navigator.mediaDevices.getUserMedia(e)), n = e.createAudioContext ?? (() => new window.AudioContext());
1027
+ 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), O(), i = await e.connect({
1028
+ onopen: () => x("listening"),
1029
+ onmessage: D,
1030
+ onerror: (e) => x("error", e),
1031
+ onclose: () => {
1032
+ _ || x("idle");
1033
+ }
1034
+ }), k(), x("listening");
1035
+ } catch (e) {
1036
+ throw i?.close(), await A(!1), x("error", e), e;
1037
+ }
1038
+ }
1039
+ },
1040
+ async stop() {
1041
+ _ = !0;
1042
+ try {
1043
+ p && v() && i?.sendRealtimeInput({ activityEnd: {} }), i?.sendRealtimeInput({ audioStreamEnd: !0 });
1044
+ } catch {}
1045
+ i?.close(), await A(!0);
129
1046
  }
130
1047
  };
131
1048
  }
132
1049
  //#endregion
133
- export { r as createElevenLabsAdapter, n as createVapiAdapter };
1050
+ export { M as DEFAULT_OUTPUT_VOLUME_CALIBRATION, r as createElevenLabsAdapter, ee as createGeminiLiveAdapter, b as createLiveKitAdapter, G as createOpenAIRealtimeAdapter, j as createPipecatAdapter, n as createVapiAdapter };