@d-id/client-sdk 2.0.5-staging.393 → 2.0.5-staging.395

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