@lofcz/orb-ui 0.7.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/LICENSE +21 -0
- package/README.md +330 -0
- package/dist/adapters/audio-level.d.ts +28 -0
- package/dist/adapters/elevenlabs/index.d.ts +85 -0
- package/dist/adapters/gemini-live/index.d.ts +70 -0
- package/dist/adapters/index.d.ts +8 -0
- package/dist/adapters/livekit/browser.d.ts +49 -0
- package/dist/adapters/livekit/index.d.ts +136 -0
- package/dist/adapters/openai-realtime/index.d.ts +37 -0
- package/dist/adapters/pipecat/index.d.ts +62 -0
- package/dist/adapters/types.d.ts +7 -0
- package/dist/adapters/vapi/index.d.ts +42 -0
- package/dist/adapters.cjs +1 -0
- package/dist/adapters.js +829 -0
- package/dist/components/Orb/Orb.d.ts +2 -0
- package/dist/components/Orb/Orb.types.d.ts +87 -0
- package/dist/components/Orb/index.d.ts +2 -0
- package/dist/components/Orb/signals.d.ts +3 -0
- package/dist/index.d.ts +4 -0
- package/dist/livekit-D-s4sYba.cjs +1 -0
- package/dist/livekit-Z3IMmWaG.js +316 -0
- package/dist/livekit-adapter.cjs +1 -0
- package/dist/livekit-adapter.js +30 -0
- package/dist/orb-ui.cjs +203 -0
- package/dist/orb-ui.js +984 -0
- package/dist/themes/bars/BarsTheme.d.ts +14 -0
- package/dist/themes/bars/index.d.ts +1 -0
- package/dist/themes/circle/CircleTheme.d.ts +14 -0
- package/dist/themes/circle/index.d.ts +1 -0
- package/dist/themes/cloud/CloudTheme.d.ts +14 -0
- package/dist/themes/cloud/index.d.ts +1 -0
- package/dist/themes/debug/DebugTheme.d.ts +15 -0
- package/dist/themes/debug/index.d.ts +1 -0
- package/dist/themes/index.d.ts +5 -0
- package/dist/themes/radial/RadialTheme.d.ts +13 -0
- package/dist/themes/radial/index.d.ts +1 -0
- package/package.json +122 -0
package/dist/adapters.js
ADDED
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
import { i as e, n as t, r as n, t as r } from "./livekit-Z3IMmWaG.js";
|
|
2
|
+
//#region src/adapters/vapi/index.ts
|
|
3
|
+
var i = .12;
|
|
4
|
+
function a(e) {
|
|
5
|
+
let t = "idle", n = null;
|
|
6
|
+
function r(r) {
|
|
7
|
+
if (n &&= (clearTimeout(n), null), t === "speaking" && r === "listening") {
|
|
8
|
+
n = setTimeout(() => {
|
|
9
|
+
t = r, e(r), n = null;
|
|
10
|
+
}, 350);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
t = r, e(r);
|
|
14
|
+
}
|
|
15
|
+
function i() {
|
|
16
|
+
n &&= (clearTimeout(n), null);
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
emitState: r,
|
|
20
|
+
clearTimer: i
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function o(e, t) {
|
|
24
|
+
let n = /* @__PURE__ */ new Set(), r = null;
|
|
25
|
+
function o() {
|
|
26
|
+
r || (r = e.start.bind(e), e.start = async (...e) => (n.forEach((e) => e()), r(...e)));
|
|
27
|
+
}
|
|
28
|
+
function s() {
|
|
29
|
+
n.size > 0 || !r || (e.start = r, r = null);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
async start() {
|
|
33
|
+
await e.start(t?.assistantId);
|
|
34
|
+
},
|
|
35
|
+
stop() {
|
|
36
|
+
e.stop();
|
|
37
|
+
},
|
|
38
|
+
subscribe(t) {
|
|
39
|
+
let r = {
|
|
40
|
+
state: "idle",
|
|
41
|
+
volume: 0,
|
|
42
|
+
outputVolume: 0
|
|
43
|
+
}, c = 0;
|
|
44
|
+
function l(e) {
|
|
45
|
+
let t = e < i ? 0 : (e - i) / .88, n = t > c ? .8 : .5;
|
|
46
|
+
return c += (t - c) * n, c;
|
|
47
|
+
}
|
|
48
|
+
function u(e) {
|
|
49
|
+
r = e, t(e);
|
|
50
|
+
}
|
|
51
|
+
function d(e) {
|
|
52
|
+
u({
|
|
53
|
+
...r,
|
|
54
|
+
...e,
|
|
55
|
+
state: e.state ?? r.state
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
let { emitState: f, clearTimer: p } = a((e) => d({ state: e })), m = () => f("connecting"), h = "idle", g = !1, _ = () => {
|
|
59
|
+
g = !0, h = "listening", f("listening"), d({
|
|
60
|
+
volume: 0,
|
|
61
|
+
outputVolume: 0
|
|
62
|
+
});
|
|
63
|
+
}, v = () => {
|
|
64
|
+
g = !1, h = "idle", E(), f("idle"), d({
|
|
65
|
+
volume: 0,
|
|
66
|
+
outputVolume: 0
|
|
67
|
+
}), c = 0;
|
|
68
|
+
}, y = () => {
|
|
69
|
+
g && (h = "speaking", f("speaking"), d({
|
|
70
|
+
volume: 0,
|
|
71
|
+
outputVolume: 0
|
|
72
|
+
}), T());
|
|
73
|
+
}, b = () => {
|
|
74
|
+
g && (E(), h = "listening", d({
|
|
75
|
+
volume: 0,
|
|
76
|
+
outputVolume: 0
|
|
77
|
+
}), c = 0, f("listening"));
|
|
78
|
+
}, x = 0, S = 0, C = 0, w = () => {
|
|
79
|
+
h === "speaking" && (S += (x - S) * .1, d({
|
|
80
|
+
volume: S,
|
|
81
|
+
outputVolume: S
|
|
82
|
+
})), C = requestAnimationFrame(w);
|
|
83
|
+
}, T = () => {
|
|
84
|
+
C ||= requestAnimationFrame(w);
|
|
85
|
+
}, E = () => {
|
|
86
|
+
C &&= (cancelAnimationFrame(C), 0), S = 0, x = 0;
|
|
87
|
+
}, D = (e) => {
|
|
88
|
+
if (h === "speaking") {
|
|
89
|
+
let t = l(e);
|
|
90
|
+
x = t / (t + .3);
|
|
91
|
+
}
|
|
92
|
+
}, O = () => {}, k = (e) => {
|
|
93
|
+
console.error("[orb-ui/vapi] Error:", e), g = !1, h = "error", p(), E(), d({
|
|
94
|
+
state: "error",
|
|
95
|
+
volume: 0,
|
|
96
|
+
outputVolume: 0,
|
|
97
|
+
error: e
|
|
98
|
+
}), c = 0;
|
|
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), 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
|
+
};
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/adapters/elevenlabs/index.ts
|
|
108
|
+
function s(e, t) {
|
|
109
|
+
let n = null, r = 0, i = null, a = null, o = {
|
|
110
|
+
state: "idle",
|
|
111
|
+
volume: 0,
|
|
112
|
+
inputVolume: 0,
|
|
113
|
+
outputVolume: 0
|
|
114
|
+
}, s = "idle", c = /* @__PURE__ */ new Set();
|
|
115
|
+
function l(e) {
|
|
116
|
+
o = e, c.forEach((t) => t(e));
|
|
117
|
+
}
|
|
118
|
+
function u(e) {
|
|
119
|
+
o = {
|
|
120
|
+
...o,
|
|
121
|
+
...e,
|
|
122
|
+
state: e.state ?? o.state
|
|
123
|
+
}, l(o);
|
|
124
|
+
}
|
|
125
|
+
function d(e) {
|
|
126
|
+
s = e, u({ state: e });
|
|
127
|
+
}
|
|
128
|
+
function f() {
|
|
129
|
+
a ||= setInterval(() => {
|
|
130
|
+
if (n) {
|
|
131
|
+
if (s === "listening") {
|
|
132
|
+
let e = Math.min(n.getInputVolume() * 2, 1);
|
|
133
|
+
u({
|
|
134
|
+
volume: e,
|
|
135
|
+
inputVolume: e
|
|
136
|
+
});
|
|
137
|
+
} else if (s === "speaking") {
|
|
138
|
+
let e = Math.min(n.getOutputVolume() * 2, 1);
|
|
139
|
+
u({
|
|
140
|
+
volume: e,
|
|
141
|
+
outputVolume: e
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}, 33);
|
|
146
|
+
}
|
|
147
|
+
function p() {
|
|
148
|
+
a &&= (clearInterval(a), null);
|
|
149
|
+
}
|
|
150
|
+
function m() {
|
|
151
|
+
p(), u({
|
|
152
|
+
volume: 0,
|
|
153
|
+
inputVolume: 0,
|
|
154
|
+
outputVolume: 0
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function h() {
|
|
158
|
+
s = "idle", u({
|
|
159
|
+
state: "idle",
|
|
160
|
+
volume: 0,
|
|
161
|
+
inputVolume: 0,
|
|
162
|
+
outputVolume: 0
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
function g(e) {
|
|
166
|
+
return e === r;
|
|
167
|
+
}
|
|
168
|
+
function _(e) {
|
|
169
|
+
return {
|
|
170
|
+
onStatusChange: ({ status: t }) => {
|
|
171
|
+
g(e) && t === "connecting" && d("connecting");
|
|
172
|
+
},
|
|
173
|
+
onConnect: () => {
|
|
174
|
+
g(e) && (d("listening"), f());
|
|
175
|
+
},
|
|
176
|
+
onModeChange: ({ mode: t }) => {
|
|
177
|
+
g(e) && (t === "speaking" ? (d("speaking"), f()) : (d("listening"), f()));
|
|
178
|
+
},
|
|
179
|
+
onDisconnect: () => {
|
|
180
|
+
g(e) && (p(), n = null, h());
|
|
181
|
+
},
|
|
182
|
+
onError: (t) => {
|
|
183
|
+
g(e) && (console.error("[orb-ui/elevenlabs] Error:", t), p(), s = "error", u({
|
|
184
|
+
state: "error",
|
|
185
|
+
volume: 0,
|
|
186
|
+
inputVolume: 0,
|
|
187
|
+
outputVolume: 0,
|
|
188
|
+
error: t
|
|
189
|
+
}), n = null);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
subscribe(e) {
|
|
195
|
+
return c.add(e), e(o), n && (s === "listening" || s === "speaking") && f(), () => {
|
|
196
|
+
c.delete(e), c.size === 0 && m();
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
async start() {
|
|
200
|
+
if (n || i) return i ?? void 0;
|
|
201
|
+
r += 1;
|
|
202
|
+
let a = r;
|
|
203
|
+
return i = (async () => {
|
|
204
|
+
try {
|
|
205
|
+
let r = await e.startSession({
|
|
206
|
+
...t,
|
|
207
|
+
..._(a)
|
|
208
|
+
});
|
|
209
|
+
if (!g(a)) {
|
|
210
|
+
await r.endSession().catch((e) => {
|
|
211
|
+
console.error("[orb-ui/elevenlabs] stale session cleanup failed:", e);
|
|
212
|
+
});
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
n = r;
|
|
216
|
+
} catch (e) {
|
|
217
|
+
if (!g(a)) return;
|
|
218
|
+
console.error("[orb-ui/elevenlabs] startSession failed:", e), d("error"), u({
|
|
219
|
+
volume: 0,
|
|
220
|
+
inputVolume: 0,
|
|
221
|
+
outputVolume: 0,
|
|
222
|
+
error: e
|
|
223
|
+
});
|
|
224
|
+
} finally {
|
|
225
|
+
i = null;
|
|
226
|
+
}
|
|
227
|
+
})(), i;
|
|
228
|
+
},
|
|
229
|
+
async stop() {
|
|
230
|
+
let e = n;
|
|
231
|
+
if (!e) {
|
|
232
|
+
i && (r += 1, p(), h(), await i);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
let t = r;
|
|
236
|
+
p();
|
|
237
|
+
try {
|
|
238
|
+
await e.endSession();
|
|
239
|
+
} catch (e) {
|
|
240
|
+
console.error("[orb-ui/elevenlabs] endSession failed:", e);
|
|
241
|
+
}
|
|
242
|
+
n === e && g(t) && (n = null, h());
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/adapters/pipecat/index.ts
|
|
248
|
+
var c = {
|
|
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
|
+
}, 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) {
|
|
278
|
+
switch (e) {
|
|
279
|
+
case "initializing":
|
|
280
|
+
case "initialized":
|
|
281
|
+
case "authenticating":
|
|
282
|
+
case "authenticated":
|
|
283
|
+
case "connecting":
|
|
284
|
+
case "connected": return "connecting";
|
|
285
|
+
case "ready": return "listening";
|
|
286
|
+
case "disconnected":
|
|
287
|
+
case "disconnecting": return "idle";
|
|
288
|
+
case "error": return "error";
|
|
289
|
+
default: return;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
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;
|
|
296
|
+
}
|
|
297
|
+
function v(e, t) {
|
|
298
|
+
let n = _(e), r = n > t ? f : p;
|
|
299
|
+
return t + (n - t) * r;
|
|
300
|
+
}
|
|
301
|
+
function y(e) {
|
|
302
|
+
return typeof e == "object" && !!e && "kind" in e && typeof e.kind == "string";
|
|
303
|
+
}
|
|
304
|
+
function b(t, r = {}) {
|
|
305
|
+
let i = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Map(), o = {
|
|
306
|
+
state: g(t.state ?? "") ?? "idle",
|
|
307
|
+
volume: 0,
|
|
308
|
+
inputVolume: 0,
|
|
309
|
+
outputVolume: 0
|
|
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));
|
|
320
|
+
}
|
|
321
|
+
function S(e, t) {
|
|
322
|
+
l = 0, u = 0, x({
|
|
323
|
+
state: e,
|
|
324
|
+
volume: 0,
|
|
325
|
+
inputVolume: 0,
|
|
326
|
+
outputVolume: 0,
|
|
327
|
+
...t === void 0 ? {} : { error: t }
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
function C(e) {
|
|
331
|
+
if (o.state !== "listening") return;
|
|
332
|
+
l = v(e, l);
|
|
333
|
+
let t = l;
|
|
334
|
+
x({
|
|
335
|
+
...o,
|
|
336
|
+
volume: t,
|
|
337
|
+
inputVolume: t,
|
|
338
|
+
outputVolume: 0
|
|
339
|
+
});
|
|
340
|
+
}
|
|
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;
|
|
346
|
+
}
|
|
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,
|
|
353
|
+
inputVolume: 0,
|
|
354
|
+
outputVolume: a
|
|
355
|
+
});
|
|
356
|
+
}
|
|
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) {
|
|
377
|
+
if (!e || typeof e != "object") return !0;
|
|
378
|
+
let t = e;
|
|
379
|
+
return t.local ? !1 : r.isBotParticipant?.(t) ?? !0;
|
|
380
|
+
}
|
|
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);
|
|
385
|
+
}
|
|
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));
|
|
390
|
+
}
|
|
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) => {
|
|
401
|
+
e.pause(), e.srcObject = null, e.remove();
|
|
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) => {
|
|
416
|
+
if (typeof e != "string") return;
|
|
417
|
+
let t = g(e);
|
|
418
|
+
t && S(t);
|
|
419
|
+
}],
|
|
420
|
+
[c.userStartedSpeaking, () => S("listening")],
|
|
421
|
+
[c.userStoppedSpeaking, () => {
|
|
422
|
+
o.state === "listening" && S("thinking");
|
|
423
|
+
}],
|
|
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)]
|
|
435
|
+
];
|
|
436
|
+
function B() {
|
|
437
|
+
s || (s = !0, z.forEach(([e, n]) => t.on(e, n)));
|
|
438
|
+
}
|
|
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());
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
subscribe(e) {
|
|
446
|
+
return i.add(e), B(), e(o), () => {
|
|
447
|
+
i.delete(e), i.size === 0 && V();
|
|
448
|
+
};
|
|
449
|
+
},
|
|
450
|
+
async start() {
|
|
451
|
+
S("connecting");
|
|
452
|
+
try {
|
|
453
|
+
await (r.connect ? r.connect() : t.connect());
|
|
454
|
+
} catch (e) {
|
|
455
|
+
throw S("error", e), e;
|
|
456
|
+
}
|
|
457
|
+
},
|
|
458
|
+
async stop() {
|
|
459
|
+
try {
|
|
460
|
+
await (r.disconnect ? r.disconnect() : t.disconnect());
|
|
461
|
+
} finally {
|
|
462
|
+
D(), I(), S("idle");
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
//#endregion
|
|
468
|
+
//#region src/adapters/openai-realtime/index.ts
|
|
469
|
+
var x = "https://api.openai.com/v1/realtime/calls", S = .015, C = 8, w = {
|
|
470
|
+
noiseFloor: .003,
|
|
471
|
+
gain: 4,
|
|
472
|
+
exponent: .8,
|
|
473
|
+
attack: .55,
|
|
474
|
+
release: .1
|
|
475
|
+
};
|
|
476
|
+
function T() {
|
|
477
|
+
let e = window.AudioContext;
|
|
478
|
+
return e ? new e() : void 0;
|
|
479
|
+
}
|
|
480
|
+
function E(e) {
|
|
481
|
+
return Math.min(1, Math.max(0, (e * 4) ** .8));
|
|
482
|
+
}
|
|
483
|
+
function D(e, t, n) {
|
|
484
|
+
try {
|
|
485
|
+
let r = t();
|
|
486
|
+
if (!r) return;
|
|
487
|
+
let i = r.createMediaStreamSource(e), a = r.createAnalyser();
|
|
488
|
+
a.fftSize = 512, a.smoothingTimeConstant = .25, i.connect(a);
|
|
489
|
+
let o = new Float32Array(a.fftSize), s = setInterval(() => {
|
|
490
|
+
a.getFloatTimeDomainData(o);
|
|
491
|
+
let e = 0;
|
|
492
|
+
for (let t of o) e += t * t;
|
|
493
|
+
n(Math.sqrt(e / o.length));
|
|
494
|
+
}, 33);
|
|
495
|
+
return { async stop() {
|
|
496
|
+
clearInterval(s), i.disconnect(), a.disconnect(), r.state !== "closed" && await r.close();
|
|
497
|
+
} };
|
|
498
|
+
} catch {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
function O(e) {
|
|
503
|
+
let t = typeof e == "string" ? e : e.value;
|
|
504
|
+
if (!t) throw Error("[orb-ui/openai-realtime] getClientSecret returned an empty value.");
|
|
505
|
+
return t;
|
|
506
|
+
}
|
|
507
|
+
function k(e) {
|
|
508
|
+
let t = /* @__PURE__ */ new Set(), r = {
|
|
509
|
+
state: "idle",
|
|
510
|
+
volume: 0,
|
|
511
|
+
inputVolume: 0,
|
|
512
|
+
outputVolume: 0
|
|
513
|
+
}, i = null, a = null, o = null, s = null, c, l, u = 0, d = 0, f = !1;
|
|
514
|
+
function p() {
|
|
515
|
+
let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
|
|
516
|
+
return {
|
|
517
|
+
...w,
|
|
518
|
+
...t
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
function m(e) {
|
|
522
|
+
r = e, t.forEach((t) => t(e));
|
|
523
|
+
}
|
|
524
|
+
function h(e, t) {
|
|
525
|
+
(r.state !== e || t !== void 0) && (e !== "speaking" && (d = 0), m({
|
|
526
|
+
state: e,
|
|
527
|
+
volume: 0,
|
|
528
|
+
inputVolume: 0,
|
|
529
|
+
outputVolume: 0,
|
|
530
|
+
...t === void 0 ? {} : { error: t }
|
|
531
|
+
}));
|
|
532
|
+
}
|
|
533
|
+
function g(e) {
|
|
534
|
+
if (r.state !== "listening") return;
|
|
535
|
+
let t = E(e);
|
|
536
|
+
m({
|
|
537
|
+
...r,
|
|
538
|
+
volume: t,
|
|
539
|
+
inputVolume: t,
|
|
540
|
+
outputVolume: 0
|
|
541
|
+
});
|
|
542
|
+
}
|
|
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");
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
r.state === "speaking" && m({
|
|
553
|
+
...r,
|
|
554
|
+
volume: a,
|
|
555
|
+
inputVolume: 0,
|
|
556
|
+
outputVolume: a
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
function v(e) {
|
|
560
|
+
switch (e.type) {
|
|
561
|
+
case "session.created":
|
|
562
|
+
case "session.updated":
|
|
563
|
+
h("listening");
|
|
564
|
+
break;
|
|
565
|
+
case "input_audio_buffer.speech_started":
|
|
566
|
+
h("listening");
|
|
567
|
+
break;
|
|
568
|
+
case "input_audio_buffer.speech_stopped":
|
|
569
|
+
case "response.created":
|
|
570
|
+
h("thinking");
|
|
571
|
+
break;
|
|
572
|
+
case "response.output_audio.delta":
|
|
573
|
+
case "output_audio_buffer.started":
|
|
574
|
+
h("speaking");
|
|
575
|
+
break;
|
|
576
|
+
case "output_audio_buffer.stopped":
|
|
577
|
+
h("listening");
|
|
578
|
+
break;
|
|
579
|
+
case "response.done":
|
|
580
|
+
case "response.cancelled":
|
|
581
|
+
r.state !== "speaking" && h("listening");
|
|
582
|
+
break;
|
|
583
|
+
case "error": h("error", e.error ?? e);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
async function y(e) {
|
|
587
|
+
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;
|
|
588
|
+
}
|
|
589
|
+
return {
|
|
590
|
+
subscribe(e) {
|
|
591
|
+
return t.add(e), e(r), () => t.delete(e);
|
|
592
|
+
},
|
|
593
|
+
async start() {
|
|
594
|
+
if (!i) {
|
|
595
|
+
h("connecting"), f = !1;
|
|
596
|
+
try {
|
|
597
|
+
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());
|
|
598
|
+
o = await t(e.mediaStreamConstraints ?? { audio: !0 }), c = D(o, d, g);
|
|
599
|
+
let m = n();
|
|
600
|
+
i = m, o.getAudioTracks().forEach((e) => m.addTrack(e, o)), s = u(), s.autoplay = !0, m.ontrack = (e) => {
|
|
601
|
+
let t = e.streams[0] ?? new MediaStream([e.track]);
|
|
602
|
+
s && (s.srcObject = t), s?.play().catch(() => void 0), l?.stop(), l = D(t, d, _);
|
|
603
|
+
}, m.onconnectionstatechange = () => {
|
|
604
|
+
f || (m.connectionState === "failed" ? h("error", /* @__PURE__ */ Error("[orb-ui/openai-realtime] WebRTC connection failed.")) : (m.connectionState === "disconnected" || m.connectionState === "closed") && h("idle"));
|
|
605
|
+
};
|
|
606
|
+
let y = m.createDataChannel("oai-events");
|
|
607
|
+
a = y, y.onopen = () => h("listening"), y.onmessage = (e) => {
|
|
608
|
+
try {
|
|
609
|
+
v(JSON.parse(String(e.data)));
|
|
610
|
+
} catch {}
|
|
611
|
+
}, y.onerror = (e) => h("error", e), y.onclose = () => {
|
|
612
|
+
f || h("idle");
|
|
613
|
+
};
|
|
614
|
+
let b = await m.createOffer();
|
|
615
|
+
await m.setLocalDescription(b);
|
|
616
|
+
let S = await r(e.callsUrl ?? x, {
|
|
617
|
+
method: "POST",
|
|
618
|
+
body: b.sdp,
|
|
619
|
+
headers: {
|
|
620
|
+
Authorization: `Bearer ${p}`,
|
|
621
|
+
"Content-Type": "application/sdp"
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
if (!S.ok) {
|
|
625
|
+
let e = await S.text();
|
|
626
|
+
throw Error(`[orb-ui/openai-realtime] Session negotiation failed (${S.status}): ${e}`);
|
|
627
|
+
}
|
|
628
|
+
await m.setRemoteDescription({
|
|
629
|
+
type: "answer",
|
|
630
|
+
sdp: await S.text()
|
|
631
|
+
});
|
|
632
|
+
} catch (e) {
|
|
633
|
+
throw await y(!1), h("error", e), e;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
},
|
|
637
|
+
async stop() {
|
|
638
|
+
await y(!0);
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
//#endregion
|
|
643
|
+
//#region src/adapters/gemini-live/index.ts
|
|
644
|
+
var A = 16e3, j = {
|
|
645
|
+
noiseFloor: .003,
|
|
646
|
+
gain: 4,
|
|
647
|
+
exponent: .8,
|
|
648
|
+
attack: .3,
|
|
649
|
+
release: .1
|
|
650
|
+
};
|
|
651
|
+
function M(e) {
|
|
652
|
+
let t = 0;
|
|
653
|
+
for (let n of e) t += n * n;
|
|
654
|
+
return Math.sqrt(t / e.length);
|
|
655
|
+
}
|
|
656
|
+
function N(e) {
|
|
657
|
+
return Math.min(1, Math.max(0, (M(e) * 4) ** .8));
|
|
658
|
+
}
|
|
659
|
+
function P(e, t, n) {
|
|
660
|
+
if (t === n) return e;
|
|
661
|
+
let r = t / n, i = Math.max(1, Math.round(e.length / r)), a = new Float32Array(i);
|
|
662
|
+
for (let t = 0; t < i; t += 1) {
|
|
663
|
+
let n = t * r, i = Math.floor(n), o = Math.min(e.length - 1, i + 1), s = n - i;
|
|
664
|
+
a[t] = e[i] * (1 - s) + e[o] * s;
|
|
665
|
+
}
|
|
666
|
+
return a;
|
|
667
|
+
}
|
|
668
|
+
function F(e) {
|
|
669
|
+
let t = new Uint8Array(e.length * 2), n = new DataView(t.buffer);
|
|
670
|
+
e.forEach((e, t) => {
|
|
671
|
+
let r = Math.max(-1, Math.min(1, e));
|
|
672
|
+
n.setInt16(t * 2, r < 0 ? r * 32768 : r * 32767, !0);
|
|
673
|
+
});
|
|
674
|
+
let r = "";
|
|
675
|
+
return t.forEach((e) => {
|
|
676
|
+
r += String.fromCharCode(e);
|
|
677
|
+
}), btoa(r);
|
|
678
|
+
}
|
|
679
|
+
function I(e) {
|
|
680
|
+
let t = atob(e), n = new Uint8Array(t.length);
|
|
681
|
+
for (let e = 0; e < t.length; e += 1) n[e] = t.charCodeAt(e);
|
|
682
|
+
let r = new DataView(n.buffer), i = new Float32Array(Math.floor(n.length / 2));
|
|
683
|
+
for (let e = 0; e < i.length; e += 1) i[e] = r.getInt16(e * 2, !0) / 32768;
|
|
684
|
+
return i;
|
|
685
|
+
}
|
|
686
|
+
function L(e) {
|
|
687
|
+
let t = e?.match(/rate=(\d+)/i);
|
|
688
|
+
return t ? Number(t[1]) : 24e3;
|
|
689
|
+
}
|
|
690
|
+
function R(e) {
|
|
691
|
+
let t = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Set(), i = {
|
|
692
|
+
state: "idle",
|
|
693
|
+
volume: 0,
|
|
694
|
+
inputVolume: 0,
|
|
695
|
+
outputVolume: 0
|
|
696
|
+
}, 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;
|
|
697
|
+
function y() {
|
|
698
|
+
return e.activityDetection !== "server";
|
|
699
|
+
}
|
|
700
|
+
function b() {
|
|
701
|
+
let t = typeof e.outputVolumeCalibration == "function" ? e.outputVolumeCalibration() : e.outputVolumeCalibration;
|
|
702
|
+
return {
|
|
703
|
+
...j,
|
|
704
|
+
...t
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
function x(e) {
|
|
708
|
+
i = e, t.forEach((t) => t(e));
|
|
709
|
+
}
|
|
710
|
+
function S(e, t) {
|
|
711
|
+
(i.state !== e || t !== void 0) && (e !== "speaking" && (g = 0), x({
|
|
712
|
+
state: e,
|
|
713
|
+
volume: 0,
|
|
714
|
+
inputVolume: 0,
|
|
715
|
+
outputVolume: 0,
|
|
716
|
+
...t === void 0 ? {} : { error: t }
|
|
717
|
+
}));
|
|
718
|
+
}
|
|
719
|
+
function C() {
|
|
720
|
+
p &&= (clearTimeout(p), null);
|
|
721
|
+
}
|
|
722
|
+
function w(t) {
|
|
723
|
+
t > (e.speechThreshold ?? .04) ? (!m && y() && a?.sendRealtimeInput({ activityStart: {} }), m = !0, C(), i.state !== "listening" && S("listening")) : m && !p && (p = setTimeout(() => {
|
|
724
|
+
p = null, m = !1, y() && a?.sendRealtimeInput({ activityEnd: {} }), i.state === "listening" && S("thinking");
|
|
725
|
+
}, e.speechEndDelayMs ?? 500)), i.state === "listening" && x({
|
|
726
|
+
...i,
|
|
727
|
+
volume: t,
|
|
728
|
+
inputVolume: t,
|
|
729
|
+
outputVolume: 0
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
function T() {
|
|
733
|
+
r.forEach((e) => {
|
|
734
|
+
try {
|
|
735
|
+
e.stop();
|
|
736
|
+
} catch {}
|
|
737
|
+
e.disconnect();
|
|
738
|
+
}), r.clear(), h = s?.currentTime ?? 0;
|
|
739
|
+
}
|
|
740
|
+
function E() {
|
|
741
|
+
_ && r.size === 0 && (_ = !1, S("listening"));
|
|
742
|
+
}
|
|
743
|
+
function D(e) {
|
|
744
|
+
if (!s || !d || !e.data) return;
|
|
745
|
+
let t = I(e.data), n = s.createBuffer(1, t.length, L(e.mimeType));
|
|
746
|
+
n.getChannelData(0).set(t);
|
|
747
|
+
let a = s.createBufferSource();
|
|
748
|
+
a.buffer = n, a.connect(d);
|
|
749
|
+
let o = Math.max(s.currentTime, h);
|
|
750
|
+
h = o + n.duration, r.add(a), a.onended = () => {
|
|
751
|
+
r.delete(a), a.disconnect(), E();
|
|
752
|
+
}, a.start(o), i.state !== "speaking" && S("speaking");
|
|
753
|
+
}
|
|
754
|
+
function O(e) {
|
|
755
|
+
e.setupComplete && S("listening");
|
|
756
|
+
let t = e.serverContent;
|
|
757
|
+
if (!t) return;
|
|
758
|
+
if (t.interrupted) {
|
|
759
|
+
T(), _ = !1, S("listening");
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
let n = t.modelTurn?.parts?.filter((e) => e.inlineData?.data) ?? [];
|
|
763
|
+
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());
|
|
764
|
+
}
|
|
765
|
+
function k() {
|
|
766
|
+
if (!d) return;
|
|
767
|
+
let t = new Float32Array(d.fftSize);
|
|
768
|
+
f = setInterval(() => {
|
|
769
|
+
if (!d || i.state !== "speaking") return;
|
|
770
|
+
d.getFloatTimeDomainData(t);
|
|
771
|
+
let r = n(M(t), g, b);
|
|
772
|
+
g = r.normalized, e.onOutputVolumeSample?.(r);
|
|
773
|
+
let a = r.normalized;
|
|
774
|
+
x({
|
|
775
|
+
...i,
|
|
776
|
+
volume: a,
|
|
777
|
+
inputVolume: 0,
|
|
778
|
+
outputVolume: a
|
|
779
|
+
});
|
|
780
|
+
}, 33);
|
|
781
|
+
}
|
|
782
|
+
function R() {
|
|
783
|
+
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) => {
|
|
784
|
+
if (!a || v) return;
|
|
785
|
+
let n = t.inputBuffer.getChannelData(0);
|
|
786
|
+
w(N(n));
|
|
787
|
+
let r = P(n, s?.sampleRate ?? A, e.inputSampleRate ?? A);
|
|
788
|
+
a.sendRealtimeInput({ audio: {
|
|
789
|
+
data: F(r),
|
|
790
|
+
mimeType: `audio/pcm;rate=${e.inputSampleRate ?? A}`
|
|
791
|
+
} });
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
async function z(e) {
|
|
795
|
+
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;
|
|
796
|
+
}
|
|
797
|
+
return {
|
|
798
|
+
subscribe(e) {
|
|
799
|
+
return t.add(e), e(i), () => t.delete(e);
|
|
800
|
+
},
|
|
801
|
+
async start() {
|
|
802
|
+
if (!a) {
|
|
803
|
+
S("connecting"), v = !1;
|
|
804
|
+
try {
|
|
805
|
+
let t = e.getUserMedia ?? ((e) => navigator.mediaDevices.getUserMedia(e)), n = e.createAudioContext ?? (() => new window.AudioContext());
|
|
806
|
+
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({
|
|
807
|
+
onopen: () => S("listening"),
|
|
808
|
+
onmessage: O,
|
|
809
|
+
onerror: (e) => S("error", e),
|
|
810
|
+
onclose: () => {
|
|
811
|
+
v || S("idle");
|
|
812
|
+
}
|
|
813
|
+
}), R(), S("listening");
|
|
814
|
+
} catch (e) {
|
|
815
|
+
throw a?.close(), await z(!1), S("error", e), e;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
},
|
|
819
|
+
async stop() {
|
|
820
|
+
v = !0;
|
|
821
|
+
try {
|
|
822
|
+
m && y() && a?.sendRealtimeInput({ activityEnd: {} }), a?.sendRealtimeInput({ audioStreamEnd: !0 });
|
|
823
|
+
} catch {}
|
|
824
|
+
a?.close(), await z(!0);
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
//#endregion
|
|
829
|
+
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 };
|