orb-ui 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/adapters.js CHANGED
@@ -1,137 +1,485 @@
1
- function F(t) {
2
- let l = "idle", e = null;
3
- function i(n) {
4
- if (e && (clearTimeout(e), e = null), l === "speaking" && n === "listening") {
5
- e = setTimeout(() => {
6
- l = n, t(n), e = null;
7
- }, 350);
8
- return;
9
- }
10
- l = n, t(n);
11
- }
12
- function s() {
13
- e && (clearTimeout(e), e = null);
14
- }
15
- return { emitState: i, clearTimer: s };
1
+ //#region src/adapters/vapi/index.ts
2
+ var e = .12;
3
+ function t(e) {
4
+ let t = "idle", n = null;
5
+ function r(r) {
6
+ if (n &&= (clearTimeout(n), null), t === "speaking" && r === "listening") {
7
+ n = setTimeout(() => {
8
+ t = r, e(r), n = null;
9
+ }, 350);
10
+ return;
11
+ }
12
+ t = r, e(r);
13
+ }
14
+ function i() {
15
+ n &&= (clearTimeout(n), null);
16
+ }
17
+ return {
18
+ emitState: r,
19
+ clearTimer: i
20
+ };
16
21
  }
17
- function y(t, l) {
18
- let e = 0;
19
- function i(s) {
20
- const n = s < 0.12 ? 0 : (s - 0.12) / 0.88, o = n > e ? 0.8 : 0.5;
21
- return e = e + (n - e) * o, e;
22
- }
23
- return {
24
- async start() {
25
- await t.start(l == null ? void 0 : l.assistantId);
26
- },
27
- stop() {
28
- t.stop();
29
- },
30
- subscribe({ onStateChange: s, onVolumeChange: n }) {
31
- const { emitState: o, clearTimer: d } = F(s);
32
- let a = "idle", u = !1;
33
- const r = () => {
34
- u = !0, a = "listening", o("listening");
35
- }, c = () => {
36
- u = !1, a = "idle", p(), o("idle"), n(0), e = 0;
37
- }, S = () => {
38
- u && (a = "speaking", o("speaking"), I());
39
- }, L = () => {
40
- u && (p(), a = "listening", o("listening"));
41
- };
42
- let v = 0, g = 0, m = 0;
43
- const b = () => {
44
- a === "speaking" && (g += (v - g) * 0.1, n(g)), m = requestAnimationFrame(b);
45
- }, I = () => {
46
- m || (m = requestAnimationFrame(b));
47
- }, p = () => {
48
- m && (cancelAnimationFrame(m), m = 0), g = 0, v = 0;
49
- }, E = (f) => {
50
- if (a === "speaking") {
51
- const k = i(f);
52
- v = k / (k + 0.3);
53
- }
54
- }, O = (f) => {
55
- }, h = (f) => {
56
- console.error("[orb-ui/vapi] Error:", f), p(), o("error"), n(0), e = 0;
57
- };
58
- t.on("call-start", r), t.on("call-end", c), t.on("speech-start", S), t.on("speech-end", L), t.on("volume-level", E), t.on("message", O), t.on("error", h);
59
- const V = t.start.bind(t);
60
- return t.start = async (...f) => (o("connecting"), V(...f)), () => {
61
- d(), t.removeListener("call-start", r), t.removeListener("call-end", c), t.removeListener("speech-start", S), t.removeListener("speech-end", L), t.removeListener("volume-level", E), t.removeListener("message", O), t.removeListener("error", h), t.start = V;
62
- };
63
- }
64
- };
22
+ function n(n, r) {
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);
29
+ }
30
+ return {
31
+ async start() {
32
+ await n.start(r?.assistantId);
33
+ },
34
+ stop() {
35
+ n.stop();
36
+ },
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
+ });
62
+ }, v = () => {
63
+ g = !1, h = "idle", E(), f("idle"), d({
64
+ volume: 0,
65
+ outputVolume: 0
66
+ }), c = 0;
67
+ }, y = () => {
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);
90
+ }
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;
98
+ };
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();
101
+ };
102
+ }
103
+ };
65
104
  }
66
- function A(t, l) {
67
- let e = null, i = null;
68
- const s = /* @__PURE__ */ new Set();
69
- function n(r) {
70
- s.forEach((c) => c.onStateChange(r));
71
- }
72
- function o(r) {
73
- s.forEach((c) => c.onVolumeChange(r));
74
- }
75
- function d() {
76
- i || (i = setInterval(() => {
77
- if (!e) return;
78
- const r = e.getOutputVolume();
79
- o(Math.min(r * 2, 1));
80
- }, 33));
81
- }
82
- function a() {
83
- i && (clearInterval(i), i = null), o(0);
84
- }
85
- const u = {
86
- onStatusChange: ({ status: r }) => {
87
- r === "connecting" && n("connecting");
88
- },
89
- onConnect: () => {
90
- n("listening");
91
- },
92
- onModeChange: ({ mode: r }) => {
93
- r === "speaking" ? (n("speaking"), d()) : (n("listening"), a());
94
- },
95
- onDisconnect: () => {
96
- a(), n("idle"), o(0), e = null;
97
- },
98
- onError: (r) => {
99
- console.error("[orb-ui/elevenlabs] Error:", r), a(), n("error"), o(0), e = null;
100
- }
101
- };
102
- return {
103
- // ── OrbAdapter.subscribe ────────────────────────────────────────────────
104
- subscribe(r) {
105
- return s.add(r), () => {
106
- s.delete(r), s.size === 0 && a();
107
- };
108
- },
109
- // ── Lifecycle ───────────────────────────────────────────────────────────
110
- async start() {
111
- if (!e)
112
- try {
113
- e = await t.startSession({
114
- ...l,
115
- ...u
116
- });
117
- } catch (r) {
118
- console.error("[orb-ui/elevenlabs] startSession failed:", r), n("error"), o(0);
119
- }
120
- },
121
- async stop() {
122
- if (e) {
123
- a();
124
- try {
125
- await e.endSession();
126
- } catch (r) {
127
- console.error("[orb-ui/elevenlabs] endSession failed:", r);
128
- }
129
- e = null;
130
- }
131
- }
132
- };
105
+ //#endregion
106
+ //#region src/adapters/elevenlabs/index.ts
107
+ function r(e, t) {
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));
116
+ }
117
+ function u(e) {
118
+ o = {
119
+ ...o,
120
+ ...e,
121
+ state: e.state ?? o.state
122
+ }, l(o);
123
+ }
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
+ }
144
+ }, 33);
145
+ }
146
+ function p() {
147
+ a &&= (clearInterval(a), null);
148
+ }
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
+ };
197
+ },
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;
227
+ },
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());
242
+ }
243
+ };
133
244
  }
134
- export {
135
- A as createElevenLabsAdapter,
136
- y as createVapiAdapter
137
- };
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
+ export { r as createElevenLabsAdapter, b as createLiveKitAdapter, n as createVapiAdapter };
@@ -0,0 +1,2 @@
1
+ import { OrbProps } from './Orb.types.js';
2
+ export declare function Orb({ signal: signalProp, state: stateProp, volume: volumeProp, adapter, theme, size, className, style, disabled, onStart, onStop, ...htmlProps }: OrbProps): import("react/jsx-runtime").JSX.Element;