orb-ui 0.3.0 → 0.5.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/README.md +117 -19
- package/dist/adapters/audio-level.d.ts +23 -0
- package/dist/adapters/gemini-live/index.d.ts +70 -0
- package/dist/adapters/index.d.ts +5 -1
- package/dist/adapters/livekit/browser.d.ts +44 -0
- package/dist/adapters/livekit/index.d.ts +1 -1
- package/dist/adapters/openai-realtime/index.d.ts +37 -0
- package/dist/adapters/pipecat/index.d.ts +46 -0
- package/dist/adapters/types.d.ts +2 -2
- package/dist/adapters.cjs +1 -1
- package/dist/adapters.js +527 -200
- package/dist/components/Orb/Orb.types.d.ts +1 -12
- package/dist/components/Orb/index.d.ts +1 -1
- package/dist/components/Orb/signals.d.ts +1 -3
- package/dist/index.d.ts +1 -1
- package/dist/livekit-Dasur3lv.js +240 -0
- package/dist/livekit-Ro6A52eb.cjs +1 -0
- package/dist/livekit-adapter.cjs +1 -0
- package/dist/livekit-adapter.js +28 -0
- package/dist/orb-ui.cjs +2 -2
- package/dist/orb-ui.js +83 -110
- package/package.json +27 -4
package/dist/adapters.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { t as e } from "./livekit-Dasur3lv.js";
|
|
1
2
|
//#region src/adapters/vapi/index.ts
|
|
2
|
-
var
|
|
3
|
-
function
|
|
3
|
+
var t = .12;
|
|
4
|
+
function n(e) {
|
|
4
5
|
let t = "idle", n = null;
|
|
5
6
|
function r(r) {
|
|
6
7
|
if (n &&= (clearTimeout(n), null), t === "speaking" && r === "listening") {
|
|
@@ -19,20 +20,20 @@ function t(e) {
|
|
|
19
20
|
clearTimer: i
|
|
20
21
|
};
|
|
21
22
|
}
|
|
22
|
-
function
|
|
23
|
+
function r(e, r) {
|
|
23
24
|
let i = /* @__PURE__ */ new Set(), a = null;
|
|
24
25
|
function o() {
|
|
25
|
-
a || (a =
|
|
26
|
+
a || (a = e.start.bind(e), e.start = async (...e) => (i.forEach((e) => e()), a(...e)));
|
|
26
27
|
}
|
|
27
28
|
function s() {
|
|
28
|
-
i.size > 0 || !a || (
|
|
29
|
+
i.size > 0 || !a || (e.start = a, a = null);
|
|
29
30
|
}
|
|
30
31
|
return {
|
|
31
32
|
async start() {
|
|
32
|
-
await
|
|
33
|
+
await e.start(r?.assistantId);
|
|
33
34
|
},
|
|
34
35
|
stop() {
|
|
35
|
-
|
|
36
|
+
e.stop();
|
|
36
37
|
},
|
|
37
38
|
subscribe(r) {
|
|
38
39
|
let a = {
|
|
@@ -40,8 +41,8 @@ function n(n, r) {
|
|
|
40
41
|
volume: 0,
|
|
41
42
|
outputVolume: 0
|
|
42
43
|
}, c = 0;
|
|
43
|
-
function l(
|
|
44
|
-
let n =
|
|
44
|
+
function l(e) {
|
|
45
|
+
let n = e < t ? 0 : (e - t) / (1 - t), r = n > c ? .8 : .5;
|
|
45
46
|
return c += (n - c) * r, c;
|
|
46
47
|
}
|
|
47
48
|
function u(e) {
|
|
@@ -54,7 +55,7 @@ function n(n, r) {
|
|
|
54
55
|
state: e.state ?? a.state
|
|
55
56
|
});
|
|
56
57
|
}
|
|
57
|
-
let { emitState: f, clearTimer: p } =
|
|
58
|
+
let { emitState: f, clearTimer: p } = n((e) => d({ state: e })), m = () => f("connecting"), h = "idle", g = !1, _ = () => {
|
|
58
59
|
g = !0, h = "listening", f("listening"), d({
|
|
59
60
|
volume: 0,
|
|
60
61
|
outputVolume: 0
|
|
@@ -96,15 +97,15 @@ function n(n, r) {
|
|
|
96
97
|
error: e
|
|
97
98
|
}), c = 0;
|
|
98
99
|
};
|
|
99
|
-
return
|
|
100
|
-
p(), E(), c = 0,
|
|
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();
|
|
101
102
|
};
|
|
102
103
|
}
|
|
103
104
|
};
|
|
104
105
|
}
|
|
105
106
|
//#endregion
|
|
106
107
|
//#region src/adapters/elevenlabs/index.ts
|
|
107
|
-
function
|
|
108
|
+
function i(e, t) {
|
|
108
109
|
let n = null, r = 0, i = null, a = null, o = {
|
|
109
110
|
state: "idle",
|
|
110
111
|
volume: 0,
|
|
@@ -243,243 +244,569 @@ function r(e, t) {
|
|
|
243
244
|
};
|
|
244
245
|
}
|
|
245
246
|
//#endregion
|
|
246
|
-
//#region src/adapters/
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
247
|
+
//#region src/adapters/pipecat/index.ts
|
|
248
|
+
var a = {
|
|
249
|
+
connected: "connected",
|
|
250
|
+
disconnected: "disconnected",
|
|
251
|
+
transportStateChanged: "transportStateChanged",
|
|
252
|
+
botReady: "botReady",
|
|
253
|
+
botDisconnected: "botDisconnected",
|
|
254
|
+
error: "error",
|
|
255
|
+
messageError: "messageError",
|
|
256
|
+
deviceError: "deviceError",
|
|
257
|
+
localAudioLevel: "localAudioLevel",
|
|
258
|
+
remoteAudioLevel: "remoteAudioLevel",
|
|
259
|
+
botStartedSpeaking: "botStartedSpeaking",
|
|
260
|
+
botStoppedSpeaking: "botStoppedSpeaking",
|
|
261
|
+
userStartedSpeaking: "userStartedSpeaking",
|
|
262
|
+
userStoppedSpeaking: "userStoppedSpeaking",
|
|
263
|
+
botLlmStarted: "botLlmStarted",
|
|
264
|
+
botTtsStarted: "botTtsStarted"
|
|
265
|
+
}, o = .002, s = 4, c = .8, l = .6, u = .2;
|
|
254
266
|
function d(e) {
|
|
255
267
|
switch (e) {
|
|
256
|
-
case "
|
|
257
|
-
case "
|
|
258
|
-
case "
|
|
259
|
-
case "
|
|
268
|
+
case "initializing":
|
|
269
|
+
case "initialized":
|
|
270
|
+
case "authenticating":
|
|
271
|
+
case "authenticated":
|
|
260
272
|
case "connecting":
|
|
261
|
-
case "
|
|
262
|
-
case "
|
|
263
|
-
case "disconnected":
|
|
264
|
-
case "
|
|
265
|
-
|
|
273
|
+
case "connected": return "connecting";
|
|
274
|
+
case "ready": return "listening";
|
|
275
|
+
case "disconnected":
|
|
276
|
+
case "disconnecting": return "idle";
|
|
277
|
+
case "error": return "error";
|
|
278
|
+
default: return;
|
|
266
279
|
}
|
|
267
280
|
}
|
|
268
281
|
function f(e) {
|
|
269
|
-
|
|
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;
|
|
270
285
|
}
|
|
271
|
-
function p(e) {
|
|
272
|
-
|
|
286
|
+
function p(e, t) {
|
|
287
|
+
let n = f(e), r = n > t ? l : u;
|
|
288
|
+
return t + (n - t) * r;
|
|
273
289
|
}
|
|
274
290
|
function m(e) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
291
|
+
return typeof e == "object" && !!e && "kind" in e && typeof e.kind == "string";
|
|
292
|
+
}
|
|
293
|
+
function h(e, t = {}) {
|
|
294
|
+
let n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map(), i = {
|
|
295
|
+
state: d(e.state ?? "") ?? "idle",
|
|
296
|
+
volume: 0,
|
|
297
|
+
inputVolume: 0,
|
|
298
|
+
outputVolume: 0
|
|
299
|
+
}, o = !1, s = 0, c = 0;
|
|
300
|
+
function l(e) {
|
|
301
|
+
i = e, n.forEach((t) => t(e));
|
|
302
|
+
}
|
|
303
|
+
function u(e, t) {
|
|
304
|
+
s = 0, c = 0, l({
|
|
305
|
+
state: e,
|
|
306
|
+
volume: 0,
|
|
307
|
+
inputVolume: 0,
|
|
308
|
+
outputVolume: 0,
|
|
309
|
+
...t === void 0 ? {} : { error: t }
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
function f(e) {
|
|
313
|
+
if (i.state !== "listening") return;
|
|
314
|
+
s = p(e, s);
|
|
315
|
+
let t = s;
|
|
316
|
+
l({
|
|
317
|
+
...i,
|
|
318
|
+
volume: t,
|
|
319
|
+
inputVolume: t,
|
|
320
|
+
outputVolume: 0
|
|
321
|
+
});
|
|
322
|
+
}
|
|
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;
|
|
328
|
+
}
|
|
329
|
+
c = p(e, c);
|
|
330
|
+
let r = c;
|
|
331
|
+
l({
|
|
332
|
+
...i,
|
|
333
|
+
volume: r,
|
|
334
|
+
inputVolume: 0,
|
|
335
|
+
outputVolume: r
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
function g(e) {
|
|
339
|
+
if (!e || typeof e != "object") return !0;
|
|
340
|
+
let n = e;
|
|
341
|
+
return n.local ? !1 : t.isBotParticipant?.(n) ?? !0;
|
|
342
|
+
}
|
|
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);
|
|
347
|
+
}
|
|
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));
|
|
352
|
+
}
|
|
353
|
+
function y() {
|
|
354
|
+
r.forEach((e) => {
|
|
355
|
+
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) => {
|
|
364
|
+
if (typeof e != "string") return;
|
|
365
|
+
let t = d(e);
|
|
366
|
+
t && u(t);
|
|
367
|
+
}],
|
|
368
|
+
[a.userStartedSpeaking, () => u("listening")],
|
|
369
|
+
[a.userStoppedSpeaking, () => {
|
|
370
|
+
i.state === "listening" && u("thinking");
|
|
371
|
+
}],
|
|
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)]
|
|
383
|
+
];
|
|
384
|
+
function x() {
|
|
385
|
+
o || (o = !0, b.forEach(([t, n]) => e.on(t, n)));
|
|
386
|
+
}
|
|
387
|
+
function S() {
|
|
388
|
+
o && (o = !1, b.forEach(([t, n]) => {
|
|
389
|
+
e.off ? e.off(t, n) : e.removeListener?.(t, n);
|
|
390
|
+
}), y());
|
|
391
|
+
}
|
|
392
|
+
return {
|
|
393
|
+
subscribe(e) {
|
|
394
|
+
return n.add(e), x(), e(i), () => {
|
|
395
|
+
n.delete(e), n.size === 0 && S();
|
|
396
|
+
};
|
|
397
|
+
},
|
|
398
|
+
async start() {
|
|
399
|
+
u("connecting");
|
|
400
|
+
try {
|
|
401
|
+
await (t.connect ? t.connect() : e.connect());
|
|
402
|
+
} catch (e) {
|
|
403
|
+
throw u("error", e), e;
|
|
404
|
+
}
|
|
405
|
+
},
|
|
406
|
+
async stop() {
|
|
407
|
+
try {
|
|
408
|
+
await (t.disconnect ? t.disconnect() : e.disconnect());
|
|
409
|
+
} finally {
|
|
410
|
+
y(), u("idle");
|
|
411
|
+
}
|
|
412
|
+
}
|
|
278
413
|
};
|
|
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
414
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
415
|
+
//#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;
|
|
284
429
|
}
|
|
285
|
-
|
|
286
|
-
let t =
|
|
287
|
-
return
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|
+
//#region src/adapters/openai-realtime/index.ts
|
|
450
|
+
var x = "https://api.openai.com/v1/realtime/calls", S = .015, C = 8, w = {
|
|
451
|
+
noiseFloor: .003,
|
|
452
|
+
gain: 4,
|
|
453
|
+
exponent: .8,
|
|
454
|
+
attack: .55,
|
|
455
|
+
release: .1
|
|
456
|
+
};
|
|
457
|
+
function T() {
|
|
458
|
+
let e = window.AudioContext;
|
|
459
|
+
return e ? new e() : void 0;
|
|
291
460
|
}
|
|
292
|
-
function
|
|
293
|
-
|
|
294
|
-
return null;
|
|
461
|
+
function E(e) {
|
|
462
|
+
return Math.min(1, Math.max(0, (e * 4) ** .8));
|
|
295
463
|
}
|
|
296
|
-
function
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
r
|
|
464
|
+
function D(e, t, n) {
|
|
465
|
+
try {
|
|
466
|
+
let r = t();
|
|
467
|
+
if (!r) return;
|
|
468
|
+
let i = r.createMediaStreamSource(e), a = r.createAnalyser();
|
|
469
|
+
a.fftSize = 512, a.smoothingTimeConstant = .25, i.connect(a);
|
|
470
|
+
let o = new Float32Array(a.fftSize), s = setInterval(() => {
|
|
471
|
+
a.getFloatTimeDomainData(o);
|
|
472
|
+
let e = 0;
|
|
473
|
+
for (let t of o) e += t * t;
|
|
474
|
+
n(Math.sqrt(e / o.length));
|
|
475
|
+
}, 33);
|
|
476
|
+
return { async stop() {
|
|
477
|
+
clearInterval(s), i.disconnect(), a.disconnect(), r.state !== "closed" && await r.close();
|
|
478
|
+
} };
|
|
479
|
+
} catch {
|
|
300
480
|
return;
|
|
301
481
|
}
|
|
302
|
-
e.removeListener?.call(e, t, n);
|
|
303
482
|
}
|
|
304
|
-
function
|
|
305
|
-
e
|
|
483
|
+
function O(e) {
|
|
484
|
+
let t = typeof e == "string" ? e : e.value;
|
|
485
|
+
if (!t) throw Error("[orb-ui/openai-realtime] getClientSecret returned an empty value.");
|
|
486
|
+
return t;
|
|
306
487
|
}
|
|
307
|
-
function
|
|
308
|
-
let t =
|
|
488
|
+
function k(e) {
|
|
489
|
+
let t = /* @__PURE__ */ new Set(), n = {
|
|
309
490
|
state: "idle",
|
|
310
491
|
volume: 0,
|
|
311
492
|
inputVolume: 0,
|
|
312
493
|
outputVolume: 0
|
|
313
|
-
},
|
|
314
|
-
function
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
...e,
|
|
321
|
-
state: e.state ?? m.state
|
|
322
|
-
});
|
|
494
|
+
}, r = null, i = null, a = null, o = null, s, c, l = 0, u = 0, d = !1;
|
|
495
|
+
function f() {
|
|
496
|
+
let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
|
|
497
|
+
return {
|
|
498
|
+
...w,
|
|
499
|
+
...t
|
|
500
|
+
};
|
|
323
501
|
}
|
|
324
|
-
function
|
|
325
|
-
|
|
326
|
-
D += (t - D) * n;
|
|
327
|
-
let r = Math.min(D * s, 1);
|
|
328
|
-
return r / (r + u);
|
|
502
|
+
function p(e) {
|
|
503
|
+
n = e, t.forEach((t) => t(e));
|
|
329
504
|
}
|
|
330
|
-
function
|
|
331
|
-
|
|
505
|
+
function m(e, t) {
|
|
506
|
+
n.state === e && t === void 0 || (e !== "speaking" && (u = 0), p({
|
|
507
|
+
state: e,
|
|
332
508
|
volume: 0,
|
|
509
|
+
inputVolume: 0,
|
|
510
|
+
outputVolume: 0,
|
|
511
|
+
...t === void 0 ? {} : { error: t }
|
|
512
|
+
}));
|
|
513
|
+
}
|
|
514
|
+
function h(e) {
|
|
515
|
+
if (n.state !== "listening") return;
|
|
516
|
+
let t = E(e);
|
|
517
|
+
p({
|
|
518
|
+
...n,
|
|
519
|
+
volume: t,
|
|
520
|
+
inputVolume: t,
|
|
333
521
|
outputVolume: 0
|
|
334
522
|
});
|
|
335
523
|
}
|
|
336
|
-
function
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
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");
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
n.state === "speaking" && p({
|
|
534
|
+
...n,
|
|
535
|
+
volume: i,
|
|
536
|
+
inputVolume: 0,
|
|
537
|
+
outputVolume: i
|
|
340
538
|
});
|
|
341
539
|
}
|
|
342
|
-
function
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
540
|
+
function _(e) {
|
|
541
|
+
switch (e.type) {
|
|
542
|
+
case "session.created":
|
|
543
|
+
case "session.updated":
|
|
544
|
+
m("listening");
|
|
545
|
+
break;
|
|
546
|
+
case "input_audio_buffer.speech_started":
|
|
547
|
+
m("listening");
|
|
548
|
+
break;
|
|
549
|
+
case "input_audio_buffer.speech_stopped":
|
|
550
|
+
case "response.created":
|
|
551
|
+
m("thinking");
|
|
552
|
+
break;
|
|
553
|
+
case "response.output_audio.delta":
|
|
554
|
+
case "output_audio_buffer.started":
|
|
555
|
+
m("speaking");
|
|
556
|
+
break;
|
|
557
|
+
case "output_audio_buffer.stopped":
|
|
558
|
+
m("listening");
|
|
559
|
+
break;
|
|
560
|
+
case "response.done":
|
|
561
|
+
case "response.cancelled":
|
|
562
|
+
n.state !== "speaking" && m("listening");
|
|
563
|
+
break;
|
|
564
|
+
case "error":
|
|
565
|
+
m("error", e.error ?? e);
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
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;
|
|
571
|
+
}
|
|
572
|
+
return {
|
|
573
|
+
subscribe(e) {
|
|
574
|
+
return t.add(e), e(n), () => t.delete(e);
|
|
575
|
+
},
|
|
576
|
+
async start() {
|
|
577
|
+
if (!r) {
|
|
578
|
+
m("connecting"), d = !1;
|
|
579
|
+
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) => {
|
|
584
|
+
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"));
|
|
588
|
+
};
|
|
589
|
+
let y = v.createDataChannel("oai-events");
|
|
590
|
+
i = y, y.onopen = () => m("listening"), y.onmessage = (e) => {
|
|
591
|
+
try {
|
|
592
|
+
_(JSON.parse(String(e.data)));
|
|
593
|
+
} catch {}
|
|
594
|
+
}, y.onerror = (e) => m("error", e), y.onclose = () => {
|
|
595
|
+
d || m("idle");
|
|
596
|
+
};
|
|
597
|
+
let b = await v.createOffer();
|
|
598
|
+
await v.setLocalDescription(b);
|
|
599
|
+
let S = await l(e.callsUrl ?? x, {
|
|
600
|
+
method: "POST",
|
|
601
|
+
body: b.sdp,
|
|
602
|
+
headers: {
|
|
603
|
+
Authorization: `Bearer ${p}`,
|
|
604
|
+
"Content-Type": "application/sdp"
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
if (!S.ok) {
|
|
608
|
+
let e = await S.text();
|
|
609
|
+
throw Error(`[orb-ui/openai-realtime] Session negotiation failed (${S.status}): ${e}`);
|
|
610
|
+
}
|
|
611
|
+
await v.setRemoteDescription({
|
|
612
|
+
type: "answer",
|
|
613
|
+
sdp: await S.text()
|
|
614
|
+
});
|
|
615
|
+
} catch (e) {
|
|
616
|
+
throw await v(!1), m("error", e), e;
|
|
617
|
+
}
|
|
350
618
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
A({
|
|
355
|
-
volume: e,
|
|
356
|
-
inputVolume: e,
|
|
357
|
-
outputVolume: 0
|
|
358
|
-
});
|
|
359
|
-
}, 33);
|
|
619
|
+
},
|
|
620
|
+
async stop() {
|
|
621
|
+
await v(!0);
|
|
360
622
|
}
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
//#endregion
|
|
626
|
+
//#region src/adapters/gemini-live/index.ts
|
|
627
|
+
var A = 16e3, j = {
|
|
628
|
+
noiseFloor: .003,
|
|
629
|
+
gain: 4,
|
|
630
|
+
exponent: .8,
|
|
631
|
+
attack: .3,
|
|
632
|
+
release: .1
|
|
633
|
+
};
|
|
634
|
+
function M(e) {
|
|
635
|
+
let t = 0;
|
|
636
|
+
for (let n of e) t += n * n;
|
|
637
|
+
return Math.sqrt(t / e.length);
|
|
638
|
+
}
|
|
639
|
+
function N(e) {
|
|
640
|
+
return Math.min(1, Math.max(0, (M(e) * 4) ** .8));
|
|
641
|
+
}
|
|
642
|
+
function P(e, t, n) {
|
|
643
|
+
if (t === n) return e;
|
|
644
|
+
let r = t / n, i = Math.max(1, Math.round(e.length / r)), a = new Float32Array(i);
|
|
645
|
+
for (let t = 0; t < i; t += 1) {
|
|
646
|
+
let n = t * r, i = Math.floor(n), o = Math.min(e.length - 1, i + 1), s = n - i;
|
|
647
|
+
a[t] = e[i] * (1 - s) + e[o] * s;
|
|
648
|
+
}
|
|
649
|
+
return a;
|
|
650
|
+
}
|
|
651
|
+
function F(e) {
|
|
652
|
+
let t = new Uint8Array(e.length * 2), n = new DataView(t.buffer);
|
|
653
|
+
e.forEach((e, t) => {
|
|
654
|
+
let r = Math.max(-1, Math.min(1, e));
|
|
655
|
+
n.setInt16(t * 2, r < 0 ? r * 32768 : r * 32767, !0);
|
|
656
|
+
});
|
|
657
|
+
let r = "";
|
|
658
|
+
return t.forEach((e) => {
|
|
659
|
+
r += String.fromCharCode(e);
|
|
660
|
+
}), btoa(r);
|
|
661
|
+
}
|
|
662
|
+
function I(e) {
|
|
663
|
+
let t = atob(e), n = new Uint8Array(t.length);
|
|
664
|
+
for (let e = 0; e < t.length; e += 1) n[e] = t.charCodeAt(e);
|
|
665
|
+
let r = new DataView(n.buffer), i = new Float32Array(Math.floor(n.length / 2));
|
|
666
|
+
for (let e = 0; e < i.length; e += 1) i[e] = r.getInt16(e * 2, !0) / 32768;
|
|
667
|
+
return i;
|
|
668
|
+
}
|
|
669
|
+
function L(e) {
|
|
670
|
+
let t = e?.match(/rate=(\d+)/i);
|
|
671
|
+
return t ? Number(t[1]) : 24e3;
|
|
672
|
+
}
|
|
673
|
+
function R(e) {
|
|
674
|
+
let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), r = {
|
|
675
|
+
state: "idle",
|
|
676
|
+
volume: 0,
|
|
677
|
+
inputVolume: 0,
|
|
678
|
+
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() {
|
|
681
|
+
return e.activityDetection !== "server";
|
|
682
|
+
}
|
|
683
|
+
function y() {
|
|
684
|
+
let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
|
|
685
|
+
return {
|
|
686
|
+
...j,
|
|
687
|
+
...t
|
|
688
|
+
};
|
|
361
689
|
}
|
|
362
|
-
function
|
|
363
|
-
|
|
690
|
+
function x(e) {
|
|
691
|
+
r = e, t.forEach((t) => t(e));
|
|
692
|
+
}
|
|
693
|
+
function S(e, t) {
|
|
694
|
+
r.state === e && t === void 0 || (e !== "speaking" && (h = 0), x({
|
|
695
|
+
state: e,
|
|
364
696
|
volume: 0,
|
|
697
|
+
inputVolume: 0,
|
|
698
|
+
outputVolume: 0,
|
|
699
|
+
...t === void 0 ? {} : { error: t }
|
|
700
|
+
}));
|
|
701
|
+
}
|
|
702
|
+
function C() {
|
|
703
|
+
f &&= (clearTimeout(f), null);
|
|
704
|
+
}
|
|
705
|
+
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,
|
|
710
|
+
volume: t,
|
|
711
|
+
inputVolume: t,
|
|
365
712
|
outputVolume: 0
|
|
366
713
|
});
|
|
367
714
|
}
|
|
368
|
-
function
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
715
|
+
function T() {
|
|
716
|
+
n.forEach((e) => {
|
|
717
|
+
try {
|
|
718
|
+
e.stop();
|
|
719
|
+
} catch {}
|
|
720
|
+
e.disconnect();
|
|
721
|
+
}), n.clear(), m = o?.currentTime ?? 0;
|
|
722
|
+
}
|
|
723
|
+
function E() {
|
|
724
|
+
g && n.size === 0 && (g = !1, S("listening"));
|
|
725
|
+
}
|
|
726
|
+
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");
|
|
736
|
+
}
|
|
737
|
+
function O(e) {
|
|
738
|
+
e.setupComplete && S("listening");
|
|
739
|
+
let t = e.serverContent;
|
|
740
|
+
if (!t) return;
|
|
741
|
+
if (t.interrupted) {
|
|
742
|
+
T(), g = !1, S("listening");
|
|
374
743
|
return;
|
|
375
744
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
745
|
+
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());
|
|
747
|
+
}
|
|
748
|
+
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;
|
|
757
|
+
x({
|
|
758
|
+
...r,
|
|
759
|
+
volume: i,
|
|
381
760
|
inputVolume: 0,
|
|
382
|
-
outputVolume:
|
|
761
|
+
outputVolume: i
|
|
383
762
|
});
|
|
384
763
|
}, 33);
|
|
385
764
|
}
|
|
386
|
-
function
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
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
|
|
765
|
+
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;
|
|
768
|
+
let n = t.inputBuffer.getChannelData(0);
|
|
769
|
+
w(N(n));
|
|
770
|
+
let r = P(n, o?.sampleRate ?? A, e.inputSampleRate ?? A);
|
|
771
|
+
i.sendRealtimeInput({ audio: {
|
|
772
|
+
data: F(r),
|
|
773
|
+
mimeType: `audio/pcm;rate=${e.inputSampleRate ?? A}`
|
|
774
|
+
} });
|
|
449
775
|
});
|
|
450
776
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
return t ? {
|
|
459
|
-
subscribe: q,
|
|
777
|
+
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;
|
|
779
|
+
}
|
|
780
|
+
return {
|
|
781
|
+
subscribe(e) {
|
|
782
|
+
return t.add(e), e(r), () => t.delete(e);
|
|
783
|
+
},
|
|
460
784
|
async start() {
|
|
461
|
-
if (
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
785
|
+
if (!i) {
|
|
786
|
+
S("connecting"), _ = !1;
|
|
787
|
+
try {
|
|
788
|
+
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({
|
|
790
|
+
onopen: () => S("listening"),
|
|
791
|
+
onmessage: O,
|
|
792
|
+
onerror: (e) => S("error", e),
|
|
793
|
+
onclose: () => {
|
|
794
|
+
_ || S("idle");
|
|
795
|
+
}
|
|
796
|
+
}), R(), S("listening");
|
|
797
|
+
} catch (e) {
|
|
798
|
+
throw i?.close(), await z(!1), S("error", e), e;
|
|
469
799
|
}
|
|
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
800
|
}
|
|
473
801
|
},
|
|
474
802
|
async stop() {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
K(), e.disconnect(), r.current = null, L("idle");
|
|
803
|
+
_ = !0;
|
|
804
|
+
try {
|
|
805
|
+
p && v() && i?.sendRealtimeInput({ activityEnd: {} }), i?.sendRealtimeInput({ audioStreamEnd: !0 });
|
|
806
|
+
} catch {}
|
|
807
|
+
i?.close(), await z(!0);
|
|
481
808
|
}
|
|
482
|
-
}
|
|
809
|
+
};
|
|
483
810
|
}
|
|
484
811
|
//#endregion
|
|
485
|
-
export {
|
|
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 };
|