@d-id/client-sdk 2.0.2 → 2.0.3-staging.386

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.
@@ -1,484 +0,0 @@
1
- import { c as ze, C as J, S as g, a as Be, b as Je, A as l, d as Qe, l as pe, e as We, t as qe, f as A, g as j, h as He, i as Xe, j as R, n as Ze } from "./index-PB3HZRnZ.js";
2
- function Ge() {
3
- try {
4
- return Intl.DateTimeFormat().resolvedOptions().timeZone ?? "";
5
- } catch {
6
- return "";
7
- }
8
- }
9
- function Ye() {
10
- if (typeof navigator > "u")
11
- return "";
12
- const p = navigator.userAgent ?? "", C = (navigator.platform ?? "").toLowerCase();
13
- let u = "Unknown";
14
- /android/i.test(p) ? u = "Android" : /iphone|ipad|ipod/i.test(p) ? u = "iOS" : C.includes("win") ? u = "Windows" : C.includes("mac") ? u = "Mac OS X" : C.includes("linux") && (u = "Linux");
15
- const i = u === "Android" || u === "iOS" || /Mobi/i.test(p);
16
- return `${u}, ${i ? "Mobile" : "Desktop"}`;
17
- }
18
- function en() {
19
- const p = {}, C = Ge();
20
- C && (p.timezone = C);
21
- const u = Ye();
22
- return u && (p.device = u), p;
23
- }
24
- function nn(p, C, u, i) {
25
- const b = ze(p, `${C}/v2/agents/${u}`, i);
26
- return {
27
- async createStream(h) {
28
- return b.post("/sessions", h);
29
- }
30
- };
31
- }
32
- const tn = {
33
- [g.ChatAnswer]: J.Answer,
34
- [g.ChatPartial]: J.Partial
35
- }, fe = 2e4;
36
- async function on() {
37
- try {
38
- return await import("./livekit-client.esm-DypJ_fOm.js");
39
- } catch {
40
- throw new Error(
41
- "LiveKit client is required for this streaming manager. Please install it using: npm install livekit-client"
42
- );
43
- }
44
- }
45
- const rn = {
46
- excellent: j.Strong,
47
- good: j.Strong,
48
- poor: j.Weak,
49
- lost: j.Unknown,
50
- unknown: j.Unknown
51
- }, D = (p = "Stream Error") => new We(p);
52
- function he(p, C, u) {
53
- var i, b;
54
- throw C("Failed to connect to LiveKit room:", p), (i = u.onConnectionStateChange) == null || i.call(u, A.Fail, "internal:init-error"), (b = u.onError) == null || b.call(u, p, { sessionId: "" }), p;
55
- }
56
- async function cn(p, C, u) {
57
- var se;
58
- const i = Be(u.debug || !1, "LiveKitStreamingManager"), { Room: b, RoomEvent: h, ConnectionState: I, Track: G } = await on(), { callbacks: e, auth: me, baseURL: Ce, analytics: Y } = u;
59
- let a = null, E = !1;
60
- const ee = Je.Fluent;
61
- let L = null;
62
- const M = { isPublishing: !1, publication: null }, Q = { isPublishing: !1, publication: null };
63
- let m = null, S = null, O = null, V = !1;
64
- a = new b({
65
- adaptiveStream: !1,
66
- // Must be false to use mediaStreamTrack directly
67
- dynacast: !0
68
- });
69
- let P = null, T = l.Idle, $ = !0;
70
- const F = /* @__PURE__ */ new Set();
71
- let N = null, z = !1;
72
- const Se = nn(me, Ce || Qe, p, e.onError);
73
- let y, _, k, ne = !0;
74
- try {
75
- const n = await Se.createStream({
76
- transport: C.transport,
77
- chat_persist: C.chat_persist ?? !0,
78
- verbose: u.verbose ?? !1
79
- }), { id: t, session_token: o, session_url: r, interrupt_enabled: s } = n;
80
- (se = e.onStreamCreated) == null || se.call(e, { session_id: t, stream_id: t, agent_id: p }), y = t, _ = o, k = r, ne = s ?? !0, await a.prepareConnection(k, _);
81
- } catch (n) {
82
- he(n, i, e);
83
- }
84
- if (!k || !_ || !y)
85
- return Promise.reject(new Error("Failed to initialize LiveKit stream"));
86
- a.on(h.ConnectionStateChanged, ve).on(h.ConnectionQualityChanged, we).on(h.ParticipantConnected, Ae).on(h.ParticipantDisconnected, Ee).on(h.TrackSubscribed, Re).on(h.TrackUnsubscribed, Le).on(h.DataReceived, ke).on(h.MediaDevicesError, Ke).on(h.TranscriptionReceived, Te).on(h.EncryptionError, De).on(h.TrackSubscriptionFailed, Ve);
87
- function Te(n, t) {
88
- t != null && t.isLocal && (pe.update(), !z && T === l.Talking && (T = l.Idle));
89
- }
90
- async function ye() {
91
- const n = en();
92
- if (!(!a || Object.keys(n).length === 0))
93
- try {
94
- await a.localParticipant.setAttributes(n);
95
- } catch (t) {
96
- i("Failed to set user context attributes", t);
97
- }
98
- }
99
- try {
100
- await a.connect(k, _), i("LiveKit room joined successfully"), ye(), P = setTimeout(() => {
101
- var t;
102
- i(
103
- `Track subscription timeout - no track subscribed within ${fe / 1e3} seconds after connect`
104
- ), P = null;
105
- const n = D("Track subscription timeout");
106
- Y.track("connectivity-error", {
107
- error: qe(n),
108
- sessionId: y
109
- }), (t = e.onError) == null || t.call(e, n, { sessionId: y }), Z("internal:track-subscription-timeout");
110
- }, fe);
111
- } catch (n) {
112
- he(n, i, e);
113
- }
114
- Y.enrich({
115
- "stream-type": ee
116
- });
117
- function ve(n) {
118
- var t, o, r, s;
119
- switch (i("Connection state changed:", n), n) {
120
- case I.Connecting:
121
- i("CALLBACK: onConnectionStateChange(Connecting)"), (t = e.onConnectionStateChange) == null || t.call(e, A.Connecting, "livekit:connecting");
122
- break;
123
- case I.Connected:
124
- i("LiveKit room connected successfully"), E = !0;
125
- break;
126
- case I.Disconnected:
127
- i("LiveKit room disconnected"), E = !1, V = !1, M.publication = null, Q.publication = null, (o = e.onConnectionStateChange) == null || o.call(e, A.Disconnected, "livekit:disconnected");
128
- break;
129
- case I.Reconnecting:
130
- i("LiveKit room reconnecting..."), (r = e.onConnectionStateChange) == null || r.call(e, A.Connecting, "livekit:reconnecting");
131
- break;
132
- case I.SignalReconnecting:
133
- i("LiveKit room signal reconnecting..."), (s = e.onConnectionStateChange) == null || s.call(e, A.Connecting, "livekit:signal-reconnecting");
134
- break;
135
- }
136
- }
137
- function we(n, t) {
138
- var o;
139
- i("Connection quality:", n), t != null && t.isLocal && ((o = e.onConnectivityStateChange) == null || o.call(e, rn[n]));
140
- }
141
- function Ae(n) {
142
- i("Participant connected:", n.identity);
143
- }
144
- function Ee(n) {
145
- i("Participant disconnected:", n.identity), Z("livekit:participant-disconnected");
146
- }
147
- function Pe() {
148
- var n;
149
- O !== R.Start && (i("CALLBACK: onVideoStateChange(Start)"), O = R.Start, (n = e.onVideoStateChange) == null || n.call(e, R.Start));
150
- }
151
- function te(n) {
152
- var t;
153
- O !== R.Stop && (i("CALLBACK: onVideoStateChange(Stop)"), O = R.Stop, (t = e.onVideoStateChange) == null || t.call(e, R.Stop, n));
154
- }
155
- function Re(n, t, o) {
156
- var s, d, f;
157
- i(`Track subscribed: ${n.kind} from ${o.identity}`);
158
- const r = n.mediaStreamTrack;
159
- if (!r) {
160
- i(`No mediaStreamTrack available for ${n.kind}`);
161
- return;
162
- }
163
- L ? (L.addTrack(r), i(`Added ${n.kind} track to shared MediaStream`)) : (L = new MediaStream([r]), i(`Created shared MediaStream with ${n.kind} track`)), n.kind === "audio" && (S = Xe(
164
- () => n.getRTCStatsReport(),
165
- ({ sttLatency: c, serviceLatency: v }) => {
166
- var de, ue, le;
167
- const x = pe.get(!0);
168
- let U = 0;
169
- if (c) {
170
- const ge = ((ue = (de = m == null ? void 0 : m.getReport()) == null ? void 0 : de.webRTCStats) == null ? void 0 : ue.avgRtt) ?? 0;
171
- U = ge > 0 ? Math.round(ge * 1e3) : 0;
172
- }
173
- const K = x > 0 ? x + (c ?? 0) + U : void 0, w = K !== void 0 && v !== void 0 ? K - v : void 0;
174
- (le = e.onFirstAudioDetected) == null || le.call(e, { latency: K, networkLatency: w });
175
- }
176
- )), n.kind === "video" && ((s = e.onStreamReady) == null || s.call(e), i("CALLBACK: onSrcObjectReady"), (d = e.onSrcObjectReady) == null || d.call(e, L), V || (V = !0, i("CALLBACK: onConnectionStateChange(Connected)"), (f = e.onConnectionStateChange) == null || f.call(e, A.Connected, "livekit:track-subscribed")), m = He(
177
- () => n.getRTCStatsReport(),
178
- () => E,
179
- Ze,
180
- (c, v) => {
181
- i(`Video state change: ${c}`), c === R.Start ? (P && (clearTimeout(P), P = null, i("Track subscription timeout cleared")), Pe()) : c === R.Stop && te(v);
182
- }
183
- ), m.start());
184
- }
185
- function Le(n, t, o) {
186
- i(`Track unsubscribed: ${n.kind} from ${o.identity}`), n.kind === "audio" && (S == null || S.destroy(), S = null), n.kind === "video" && (te(m == null ? void 0 : m.getReport()), m == null || m.stop(), m = null);
187
- }
188
- function ie(n, t) {
189
- var r;
190
- const o = tn[n];
191
- o && ((r = e.onMessage) == null || r.call(e, o, { event: o, ...t }));
192
- }
193
- function W(n, t) {
194
- var o, r, s, d, f;
195
- if (n === g.ToolCallStarted) {
196
- const c = t;
197
- $ = c.interruptible !== !1, (o = e.onInterruptibleChange) == null || o.call(e, $), F.add(c.call_id), T = l.ToolActive, (r = e.onAgentActivityStateChange) == null || r.call(e, l.ToolActive), (s = e.onToolEvent) == null || s.call(e, g.ToolCallStarted, c);
198
- return;
199
- }
200
- if (n === g.ToolCallDone) {
201
- const c = t;
202
- oe(c.call_id), (d = e.onToolEvent) == null || d.call(e, g.ToolCallDone, c);
203
- return;
204
- }
205
- if (n === g.ToolCallError) {
206
- const c = t;
207
- oe(c.call_id), (f = e.onToolEvent) == null || f.call(e, g.ToolCallError, c);
208
- }
209
- }
210
- function oe(n) {
211
- var t;
212
- F.delete(n), !z && F.size === 0 && T === l.ToolActive && (T = l.Idle, (t = e.onAgentActivityStateChange) == null || t.call(e, l.Idle));
213
- }
214
- function Me(n, t) {
215
- var o, r, s, d, f, c;
216
- if ($ = ((o = t.metadata) == null ? void 0 : o.interruptible) !== !1, (r = e.onInterruptibleChange) == null || r.call(e, $), n === g.StreamVideoCreated) {
217
- T = l.Talking, (s = e.onAgentActivityStateChange) == null || s.call(e, l.Talking), S == null || S.arm({
218
- sttLatency: (d = t == null ? void 0 : t.stt) == null ? void 0 : d.latency,
219
- serviceLatency: t == null ? void 0 : t.serviceLatency
220
- });
221
- return;
222
- }
223
- if (F.size > 0) {
224
- T !== l.ToolActive && (T = l.ToolActive, (f = e.onAgentActivityStateChange) == null || f.call(e, l.ToolActive));
225
- return;
226
- }
227
- !z && $ && (T = l.Idle, (c = e.onAgentActivityStateChange) == null || c.call(e, l.Idle));
228
- }
229
- function B(n, t) {
230
- var d, f, c, v;
231
- const o = ((f = (d = m == null ? void 0 : m.getReport()) == null ? void 0 : d.webRTCStats) == null ? void 0 : f.avgRtt) ?? 0, r = o > 0 ? Math.round(o / 2 * 1e3) : 0, s = { ...t, downstreamNetworkLatency: r };
232
- u.debug && ((c = t == null ? void 0 : t.metadata) != null && c.sentiment) && (s.sentiment = {
233
- id: t.metadata.sentiment.id,
234
- name: t.metadata.sentiment.sentiment
235
- }), (v = e.onMessage) == null || v.call(e, n, s), Me(n, t);
236
- }
237
- function be(n, t) {
238
- var o;
239
- (o = e.onMessage) == null || o.call(e, J.Transcribe, { event: J.Transcribe, ...t }), queueMicrotask(() => {
240
- var r;
241
- (r = e.onAgentActivityStateChange) == null || r.call(e, l.Loading);
242
- });
243
- }
244
- function Ie(n, t) {
245
- var o;
246
- z = !0, N = (t == null ? void 0 : t.turn_id) ?? null, T = l.Loading, (o = e.onAgentActivityStateChange) == null || o.call(e, l.Loading);
247
- }
248
- function $e(n, t) {
249
- var r;
250
- const o = (t == null ? void 0 : t.turn_id) ?? null;
251
- N !== null && o !== null && o < N || (T = l.Idle, (r = e.onAgentActivityStateChange) == null || r.call(e, l.Idle));
252
- }
253
- const _e = {
254
- [g.ChatAnswer]: ie,
255
- [g.ChatPartial]: ie,
256
- [g.ToolCallStarted]: W,
257
- [g.ToolCallDone]: W,
258
- [g.ToolCallError]: W,
259
- [g.StreamVideoCreated]: B,
260
- [g.StreamVideoDone]: B,
261
- [g.StreamVideoError]: B,
262
- [g.StreamVideoRejected]: B,
263
- [g.ChatAudioTranscribed]: be,
264
- [g.TurnStarted]: Ie,
265
- [g.TurnEnded]: $e
266
- };
267
- function ke(n, t, o, r) {
268
- const s = new TextDecoder().decode(n);
269
- try {
270
- const d = JSON.parse(s), f = r || d.subject;
271
- if (i("Data received:", { subject: f, data: d }), !f) return;
272
- const c = _e[f];
273
- c == null || c(f, d);
274
- } catch (d) {
275
- i("Failed to parse data channel message:", d);
276
- }
277
- }
278
- function Ke(n) {
279
- var t;
280
- i("Media devices error:", n), (t = e.onError) == null || t.call(e, D(), { sessionId: y });
281
- }
282
- function De(n) {
283
- var t;
284
- i("Encryption error:", n), (t = e.onError) == null || t.call(e, D(), { sessionId: y });
285
- }
286
- function Ve(n, t, o) {
287
- i("Track subscription failed:", { trackSid: n, participant: t, reason: o });
288
- }
289
- function Fe(n, t, o) {
290
- for (const [r, s] of o)
291
- if (s.source === t && s.track) {
292
- const d = s.track.mediaStreamTrack;
293
- if (d === n || (d == null ? void 0 : d.id) === n.id)
294
- return s;
295
- }
296
- return null;
297
- }
298
- async function re(n, t, o, r, s, d) {
299
- var x, U, K;
300
- if (!E || !a)
301
- throw i(`Room is not connected, cannot publish ${r} stream`), new Error("Room is not connected");
302
- if (n.isPublishing) {
303
- i(`${r} publish already in progress, skipping`);
304
- return;
305
- }
306
- const f = o(t);
307
- if (f.length === 0)
308
- throw new Error(`No ${r} track found in the provided MediaStream`);
309
- const c = f[0], v = Fe(c, r, s());
310
- if (v) {
311
- i(`${r} track is already published, skipping`, {
312
- trackId: c.id,
313
- publishedTrackId: (U = (x = v.track) == null ? void 0 : x.mediaStreamTrack) == null ? void 0 : U.id
314
- }), n.publication = v;
315
- return;
316
- }
317
- if ((K = n.publication) != null && K.track) {
318
- const w = n.publication.track.mediaStreamTrack;
319
- w !== c && (w == null ? void 0 : w.id) !== c.id && (i(`Unpublishing existing ${r} track before publishing new one`), await d());
320
- }
321
- i(`Publishing ${r} track from provided MediaStream`, { trackId: c.id }), n.isPublishing = !0;
322
- try {
323
- n.publication = await a.localParticipant.publishTrack(c, { source: r }), i(`${r} track published successfully`, { trackSid: n.publication.trackSid });
324
- } catch (w) {
325
- throw i(`Failed to publish ${r} track:`, w), w;
326
- } finally {
327
- n.isPublishing = !1;
328
- }
329
- }
330
- async function ae(n, t) {
331
- if (!(!n.publication || !n.publication.track))
332
- try {
333
- a && (await a.localParticipant.unpublishTrack(n.publication.track, !1), i(`${t} track unpublished`));
334
- } catch (o) {
335
- i(`Error unpublishing ${t} track:`, o);
336
- } finally {
337
- n.publication = null;
338
- }
339
- }
340
- async function xe(n) {
341
- return re(
342
- M,
343
- n,
344
- (t) => t.getAudioTracks(),
345
- G.Source.Microphone,
346
- () => a.localParticipant.audioTrackPublications,
347
- q
348
- );
349
- }
350
- async function q() {
351
- return ae(M, "Microphone");
352
- }
353
- async function Ue(n) {
354
- if (!E || !a)
355
- throw i("Cannot replace microphone track: room is not connected"), new Error("Room is not connected");
356
- if (n.kind !== "audio")
357
- throw i("Cannot replace microphone track: not an audio track", { kind: n.kind }), new Error("Microphone track must be an audio track");
358
- if (M.isPublishing)
359
- throw i("Cannot replace microphone track: publish in progress"), new Error("Microphone publish in progress");
360
- const t = M.publication;
361
- if (!t || !t.track)
362
- throw i("Cannot replace microphone track: no publication to replace"), new Error("No microphone publication to replace");
363
- try {
364
- M.isPublishing = !0, await t.track.replaceTrack(n), i("Microphone track replaced", { trackId: n.id, trackSid: t.trackSid });
365
- } finally {
366
- M.isPublishing = !1;
367
- }
368
- }
369
- async function je(n) {
370
- return re(
371
- Q,
372
- n,
373
- (t) => t.getVideoTracks(),
374
- G.Source.Camera,
375
- () => a.localParticipant.videoTrackPublications,
376
- H
377
- );
378
- }
379
- async function H() {
380
- return ae(Q, "Camera");
381
- }
382
- function Oe() {
383
- L && (L.getTracks().forEach((n) => n.stop()), L = null);
384
- }
385
- async function X(n, t) {
386
- var o, r;
387
- if (!E || !a) {
388
- i("Room is not connected for sending messages"), (o = e.onError) == null || o.call(e, D(), {
389
- sessionId: y
390
- });
391
- return;
392
- }
393
- try {
394
- await a.localParticipant.sendText(n, { topic: t }), i("Message sent successfully:", n);
395
- } catch (s) {
396
- i("Failed to send message:", s), (r = e.onError) == null || r.call(e, D(), { sessionId: y });
397
- }
398
- }
399
- async function ce(n) {
400
- var t;
401
- try {
402
- const r = JSON.parse(n).topic;
403
- return X("", r);
404
- } catch (o) {
405
- i("Failed to send data channel message:", o), (t = e.onError) == null || t.call(e, D(), { sessionId: y });
406
- }
407
- }
408
- function Ne(n) {
409
- return X(
410
- n,
411
- "lk.chat"
412
- /* Chat */
413
- );
414
- }
415
- async function Z(n) {
416
- var t, o;
417
- P && (clearTimeout(P), P = null), S == null || S.destroy(), S = null, a && ((t = e.onConnectionStateChange) == null || t.call(e, A.Disconnecting, n), await Promise.all([q(), H()]), await a.disconnect()), Oe(), E = !1, V = !1, F.clear(), N = null, (o = e.onAgentActivityStateChange) == null || o.call(e, l.Idle), T = l.Idle;
418
- }
419
- return {
420
- speak(n) {
421
- const t = typeof n == "string" ? n : JSON.stringify(n);
422
- return X(
423
- t,
424
- "did.speak"
425
- /* Speak */
426
- );
427
- },
428
- disconnect: () => Z("user:disconnect"),
429
- async reconnect() {
430
- var n, t;
431
- if ((a == null ? void 0 : a.state) === I.Connected) {
432
- i("Room is already connected");
433
- return;
434
- }
435
- if (!a || !k || !_)
436
- throw i("Cannot reconnect: missing room, URL or token"), new Error("Cannot reconnect: session not available");
437
- i("Reconnecting to LiveKit room, state:", a.state), V = !1, (n = e.onConnectionStateChange) == null || n.call(e, A.Connecting, "user:reconnect");
438
- try {
439
- if (await a.connect(k, _), i("Room reconnected"), E = !0, a.remoteParticipants.size === 0) {
440
- if (i("Waiting for agent to join..."), !await new Promise((r) => {
441
- const s = setTimeout(() => {
442
- a == null || a.off(h.ParticipantConnected, d), r(!1);
443
- }, 5e3), d = () => {
444
- clearTimeout(s), a == null || a.off(h.ParticipantConnected, d), r(!0);
445
- };
446
- a == null || a.on(h.ParticipantConnected, d);
447
- }))
448
- throw i("Agent did not join within timeout"), await a.disconnect(), new Error("Agent did not rejoin the room");
449
- i("Agent joined, reconnection successful");
450
- }
451
- } catch (o) {
452
- throw i("Failed to reconnect:", o), (t = e.onConnectionStateChange) == null || t.call(e, A.Fail, "user:reconnect-failed"), o;
453
- }
454
- },
455
- sendDataChannelMessage: ce,
456
- sendTextMessage: Ne,
457
- publishMicrophoneStream: xe,
458
- unpublishMicrophoneStream: q,
459
- replaceMicrophoneTrack: Ue,
460
- publishCameraStream: je,
461
- unpublishCameraStream: H,
462
- interrupt(n) {
463
- n !== "text" && ce(JSON.stringify({
464
- topic: "did.interrupt"
465
- /* Interrupt */
466
- }));
467
- },
468
- registerRpcMethod(n, t) {
469
- a == null || a.registerRpcMethod(n, t);
470
- },
471
- unregisterRpcMethod(n) {
472
- a == null || a.unregisterRpcMethod(n);
473
- },
474
- sessionId: y,
475
- streamId: y,
476
- streamType: ee,
477
- interruptAvailable: ne,
478
- isInterruptible: $
479
- };
480
- }
481
- export {
482
- cn as createLiveKitStreamingManager,
483
- he as handleInitError
484
- };