@d-id/client-sdk 1.1.11-staging.38 → 1.1.11-staging.39

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.
Files changed (51) hide show
  1. package/dist/index.js +234 -377
  2. package/dist/index.umd.cjs +1 -1
  3. package/dist/src/api/agents.d.ts +1 -0
  4. package/dist/src/api/apiClient.d.ts +4 -3
  5. package/dist/src/api/knowledge.d.ts +1 -0
  6. package/dist/src/api/streams/streamApi.d.ts +1 -0
  7. package/dist/src/auth/get-auth-header.d.ts +1 -0
  8. package/dist/src/errors/chat/chat-creation-failed.d.ts +1 -0
  9. package/dist/src/errors/chat/chat-mode-downgraded.d.ts +1 -0
  10. package/dist/src/errors/validation-error.d.ts +1 -0
  11. package/dist/src/errors/ws-error.d.ts +1 -0
  12. package/dist/src/services/agent-manager/connect-to-manager.d.ts +1 -0
  13. package/dist/src/services/agent-manager/connect-to-manager.test.d.ts +1 -0
  14. package/dist/src/services/agent-manager/index.d.ts +1 -0
  15. package/dist/src/services/agent-manager/index.test.d.ts +1 -0
  16. package/dist/src/services/chat/index.d.ts +2 -2
  17. package/dist/src/services/chat/intial-messages.d.ts +1 -0
  18. package/dist/src/services/interrupt/index.d.ts +1 -0
  19. package/dist/src/services/socket-manager/index.d.ts +1 -0
  20. package/dist/src/services/socket-manager/message-queue.d.ts +1 -0
  21. package/dist/src/services/streaming-manager/advanced.test.d.ts +5 -0
  22. package/dist/src/services/streaming-manager/business-flows.test.d.ts +5 -0
  23. package/dist/src/services/streaming-manager/core.test.d.ts +5 -0
  24. package/dist/src/services/streaming-manager/disconnect.test.d.ts +5 -0
  25. package/dist/src/services/streaming-manager/edge-cases.test.d.ts +5 -0
  26. package/dist/src/services/streaming-manager/index.d.ts +9 -1
  27. package/dist/src/services/streaming-manager/stats/poll.d.ts +1 -1
  28. package/dist/src/services/streaming-manager/stats/report.d.ts +1 -0
  29. package/dist/src/services/streaming-manager/utils.test.d.ts +5 -0
  30. package/dist/src/test-utils/factories/agent-manager.factory.d.ts +3 -0
  31. package/dist/src/test-utils/factories/agent.factory.d.ts +4 -0
  32. package/dist/src/test-utils/factories/agents-api.factory.d.ts +1 -0
  33. package/dist/src/test-utils/factories/analytics.factory.d.ts +1 -0
  34. package/dist/src/test-utils/factories/chat.factory.d.ts +1 -0
  35. package/dist/src/test-utils/factories/index.d.ts +8 -0
  36. package/dist/src/test-utils/factories/socket-manager.factory.d.ts +1 -0
  37. package/dist/src/test-utils/factories/stream-api.factory.d.ts +1 -0
  38. package/dist/src/test-utils/factories/streaming-manager.factory.d.ts +4 -0
  39. package/dist/src/types/entities/agents/agent.d.ts +1 -0
  40. package/dist/src/types/entities/agents/manager.d.ts +2 -1
  41. package/dist/src/types/entities/agents/presenter.d.ts +1 -0
  42. package/dist/src/types/entities/knowledge/document.d.ts +1 -0
  43. package/dist/src/types/entities/knowledge/record.d.ts +1 -0
  44. package/dist/src/types/stream/api/clip.d.ts +1 -0
  45. package/dist/src/types/stream/api/talk.d.ts +1 -0
  46. package/dist/src/types/stream/stream.d.ts +1 -0
  47. package/dist/src/types/stream-script.d.ts +1 -0
  48. package/dist/src/utils/agent.d.ts +2 -1
  49. package/dist/src/utils/analytics.d.ts +1 -0
  50. package/dist/src/utils/chat.d.ts +1 -0
  51. package/package.json +18 -2
package/dist/index.js CHANGED
@@ -1,16 +1,9 @@
1
1
  var ye = Object.defineProperty;
2
2
  var ve = (e, t, n) => t in e ? ye(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var Y = (e, t, n) => (ve(e, typeof t != "symbol" ? t + "" : t, n), n);
3
+ var Y = (e, t, n) => ve(e, typeof t != "symbol" ? t + "" : t, n);
4
4
  class G extends Error {
5
- constructor({
6
- kind: n,
7
- description: r,
8
- error: s
9
- }) {
10
- super(JSON.stringify({
11
- kind: n,
12
- description: r
13
- }));
5
+ constructor({ kind: n, description: r, error: s }) {
6
+ super(JSON.stringify({ kind: n, description: r }));
14
7
  Y(this, "kind");
15
8
  Y(this, "description");
16
9
  Y(this, "error");
@@ -27,28 +20,19 @@ class ke extends G {
27
20
  }
28
21
  class De extends G {
29
22
  constructor(t) {
30
- super({
31
- kind: "ChatModeDowngraded",
32
- description: `Chat mode downgraded to ${t}`
33
- });
23
+ super({ kind: "ChatModeDowngraded", description: `Chat mode downgraded to ${t}` });
34
24
  }
35
25
  }
36
26
  class H extends G {
37
27
  constructor(n, r) {
38
- super({
39
- kind: "ValidationError",
40
- description: n
41
- });
28
+ super({ kind: "ValidationError", description: n });
42
29
  Y(this, "key");
43
30
  this.key = r;
44
31
  }
45
32
  }
46
33
  class Ce extends G {
47
34
  constructor(t) {
48
- super({
49
- kind: "WSError",
50
- description: t
51
- });
35
+ super({ kind: "WSError", description: t });
52
36
  }
53
37
  }
54
38
  var Re = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(Re || {}), Se = /* @__PURE__ */ ((e) => (e.TRIAL = "deid-trial", e.PRO = "deid-pro", e.ENTERPRISE = "deid-enterprise", e.LITE = "deid-lite", e.ADVANCED = "deid-advanced", e.BUILD = "deid-api-build", e.LAUNCH = "deid-api-launch", e.SCALE = "deid-api-scale", e))(Se || {}), Ie = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(Ie || {}), Ee = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(Ee || {}), M = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e.Off = "Off", e))(M || {}), q = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(q || {}), Me = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(Me || {}), _e = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(_e || {}), Te = /* @__PURE__ */ ((e) => (e.Pdf = "pdf", e.Text = "text", e.Html = "html", e.Word = "word", e.Json = "json", e.Markdown = "markdown", e.Csv = "csv", e.Excel = "excel", e.Powerpoint = "powerpoint", e.Archive = "archive", e.Image = "image", e.Audio = "audio", e.Video = "video", e))(Te || {}), je = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(je || {});
@@ -91,10 +75,7 @@ async function te(e, t) {
91
75
  try {
92
76
  if (!n.timeout)
93
77
  return await e();
94
- const {
95
- promise: o,
96
- clear: c
97
- } = Fe(n.timeout, n.timeoutErrorMessage), a = e().finally(c);
78
+ const { promise: o, clear: c } = Fe(n.timeout, n.timeoutErrorMessage), a = e().finally(c);
98
79
  return await Promise.race([a, o]);
99
80
  } catch (o) {
100
81
  if (r = o, !n.shouldRetryFn(o) || s >= n.limit)
@@ -129,55 +110,35 @@ const xe = (e) => te(e, {
129
110
  });
130
111
  function ue(e, t = Z, n) {
131
112
  const r = async (s, o) => {
132
- const {
133
- skipErrorHandler: c,
134
- ...a
135
- } = o || {}, i = await xe(() => fetch(t + (s != null && s.startsWith("/") ? s : `/${s}`), {
136
- ...a,
137
- headers: {
138
- ...a.headers,
139
- Authorization: le(e),
140
- "Content-Type": "application/json"
141
- }
142
- }));
113
+ const { skipErrorHandler: c, ...a } = o || {}, i = await xe(
114
+ () => fetch(t + (s != null && s.startsWith("/") ? s : `/${s}`), {
115
+ ...a,
116
+ headers: {
117
+ ...a.headers,
118
+ Authorization: le(e),
119
+ "Content-Type": "application/json"
120
+ }
121
+ })
122
+ );
143
123
  if (!i.ok) {
144
124
  let d = await i.text().catch(() => `Failed to fetch with status ${i.status}`);
145
125
  const l = new Error(d);
146
- throw n && !c && n(l, {
147
- url: s,
148
- options: a,
149
- headers: i.headers
150
- }), l;
126
+ throw n && !c && n(l, { url: s, options: a, headers: i.headers }), l;
151
127
  }
152
128
  return i.json();
153
129
  };
154
130
  return {
155
131
  get(s, o) {
156
- return r(s, {
157
- ...o,
158
- method: "GET"
159
- });
132
+ return r(s, { ...o, method: "GET" });
160
133
  },
161
134
  post(s, o, c) {
162
- return r(s, {
163
- ...c,
164
- body: JSON.stringify(o),
165
- method: "POST"
166
- });
135
+ return r(s, { ...c, body: JSON.stringify(o), method: "POST" });
167
136
  },
168
137
  delete(s, o, c) {
169
- return r(s, {
170
- ...c,
171
- body: JSON.stringify(o),
172
- method: "DELETE"
173
- });
138
+ return r(s, { ...c, body: JSON.stringify(o), method: "DELETE" });
174
139
  },
175
140
  patch(s, o, c) {
176
- return r(s, {
177
- ...c,
178
- body: JSON.stringify(o),
179
- method: "PATCH"
180
- });
141
+ return r(s, { ...c, body: JSON.stringify(o), method: "PATCH" });
181
142
  }
182
143
  };
183
144
  }
@@ -258,35 +219,17 @@ function We(e) {
258
219
  agentId: e.id,
259
220
  access: e.access,
260
221
  name: e.preview_name,
261
- ...e.access === "public" ? {
262
- from: "agent-template"
263
- } : {}
222
+ ...e.access === "public" ? { from: "agent-template" } : {}
264
223
  };
265
224
  }
266
225
  const Ue = (e) => e.reduce((t, n) => t + n, 0), ae = (e) => Ue(e) / e.length;
267
226
  function Ke(e, t, n) {
268
227
  var i, d, l;
269
- const {
270
- event: r,
271
- ...s
272
- } = e, {
273
- template: o
274
- } = (t == null ? void 0 : t.llm) || {}, {
275
- language: c
276
- } = ((i = t == null ? void 0 : t.presenter) == null ? void 0 : i.voice) || {};
228
+ const { event: r, ...s } = e, { template: o } = (t == null ? void 0 : t.llm) || {}, { language: c } = ((i = t == null ? void 0 : t.presenter) == null ? void 0 : i.voice) || {};
277
229
  return {
278
230
  ...s,
279
- llm: {
280
- ...s.llm,
281
- template: o
282
- },
283
- script: {
284
- ...s.script,
285
- provider: {
286
- ...(d = s == null ? void 0 : s.script) == null ? void 0 : d.provider,
287
- language: c
288
- }
289
- },
231
+ llm: { ...s.llm, template: o },
232
+ script: { ...s.script, provider: { ...(d = s == null ? void 0 : s.script) == null ? void 0 : d.provider, language: c } },
290
233
  stitch: (t == null ? void 0 : t.presenter.type) === "talk" ? (l = t == null ? void 0 : t.presenter) == null ? void 0 : l.stitch : void 0,
291
234
  ...n
292
235
  };
@@ -303,39 +246,35 @@ function qe(e) {
303
246
  isEnabled: e.isEnabled ?? !0,
304
247
  getRandom: V,
305
248
  enrich(n) {
306
- this.additionalProperties = {
307
- ...this.additionalProperties,
308
- ...n
309
- };
249
+ this.additionalProperties = { ...this.additionalProperties, ...n };
310
250
  },
311
251
  async track(n, r) {
312
252
  if (!this.isEnabled)
313
253
  return Promise.resolve();
314
- const {
315
- audioPath: s,
316
- ...o
317
- } = r || {}, c = {
254
+ const { audioPath: s, ...o } = r || {}, c = {
318
255
  method: "POST",
319
256
  headers: {
320
257
  "Content-Type": "application/x-www-form-urlencoded"
321
258
  },
322
259
  body: new URLSearchParams({
323
- data: JSON.stringify([{
324
- event: n,
325
- properties: {
326
- ...this.additionalProperties,
327
- ...o,
328
- agentId: this.agentId,
329
- source: t,
330
- token: this.token,
331
- time: Date.now(),
332
- $insert_id: this.getRandom(),
333
- origin: window.location.href,
334
- "Screen Height": window.screen.height || window.innerWidth,
335
- "Screen Width": window.screen.width || window.innerHeight,
336
- "User Agent": navigator.userAgent
260
+ data: JSON.stringify([
261
+ {
262
+ event: n,
263
+ properties: {
264
+ ...this.additionalProperties,
265
+ ...o,
266
+ agentId: this.agentId,
267
+ source: t,
268
+ token: this.token,
269
+ time: Date.now(),
270
+ $insert_id: this.getRandom(),
271
+ origin: window.location.href,
272
+ "Screen Height": window.screen.height || window.innerWidth,
273
+ "Screen Width": window.screen.width || window.innerHeight,
274
+ "User Agent": navigator.userAgent
275
+ }
337
276
  }
338
- }])
277
+ ])
339
278
  })
340
279
  };
341
280
  try {
@@ -345,19 +284,15 @@ function qe(e) {
345
284
  }
346
285
  },
347
286
  linkTrack(n, r, s, o) {
348
- ee[n] || (ee[n] = {
349
- events: {},
350
- resolvedDependencies: []
351
- }), o.includes(s) || o.push(s);
287
+ ee[n] || (ee[n] = { events: {}, resolvedDependencies: [] }), o.includes(s) || o.push(s);
352
288
  const c = ee[n];
353
- if (c.events[s] = {
354
- props: r
355
- }, c.resolvedDependencies.push(s), o.every((i) => c.resolvedDependencies.includes(i))) {
356
- const i = o.reduce((d, l) => c.events[l] ? {
357
- ...d,
358
- ...c.events[l].props
359
- } : d, {});
360
- this.track(n, i), c.resolvedDependencies = c.resolvedDependencies.filter((d) => !o.includes(d)), o.forEach((d) => {
289
+ if (c.events[s] = { props: r }, c.resolvedDependencies.push(s), o.every(
290
+ (i) => c.resolvedDependencies.includes(i)
291
+ )) {
292
+ const i = o.reduce((d, l) => c.events[l] ? { ...d, ...c.events[l].props } : d, {});
293
+ this.track(n, i), c.resolvedDependencies = c.resolvedDependencies.filter(
294
+ (d) => !o.includes(d)
295
+ ), o.forEach((d) => {
361
296
  delete c.events[d];
362
297
  });
363
298
  }
@@ -374,24 +309,15 @@ function ge() {
374
309
  }
375
310
  const N = ge(), re = ge();
376
311
  function he(e) {
377
- return e === M.Playground ? {
378
- headers: {
379
- [Be]: "true"
380
- }
381
- } : {};
312
+ return e === M.Playground ? { headers: { [Be]: "true" } } : {};
382
313
  }
383
314
  async function we(e, t, n, r, s = !1, o) {
384
315
  try {
385
- return !o && !ce(r) && (o = await t.newChat(e.id, {
386
- persist: s
387
- }, he(r)), n.track("agent-chat", {
316
+ return !o && !ce(r) && (o = await t.newChat(e.id, { persist: s }, he(r)), n.track("agent-chat", {
388
317
  event: "created",
389
318
  chatId: o.id,
390
319
  mode: r
391
- })), {
392
- chat: o,
393
- chatMode: (o == null ? void 0 : o.chat_mode) ?? r
394
- };
320
+ })), { chat: o, chatMode: (o == null ? void 0 : o.chat_mode) ?? r };
395
321
  } catch (c) {
396
322
  try {
397
323
  const a = JSON.parse(c.message);
@@ -426,16 +352,7 @@ async function Xe(e, t) {
426
352
  }
427
353
  function Ye(e) {
428
354
  return new Promise((t, n) => {
429
- const {
430
- callbacks: r,
431
- host: s,
432
- auth: o
433
- } = e, {
434
- onMessage: c = null,
435
- onOpen: a = null,
436
- onClose: i = null,
437
- onError: d = null
438
- } = r || {}, l = new WebSocket(`${s}?authorization=${le(o)}`);
355
+ const { callbacks: r, host: s, auth: o } = e, { onMessage: c = null, onOpen: a = null, onClose: i = null, onError: d = null } = r || {}, l = new WebSocket(`${s}?authorization=${le(o)}`);
439
356
  l.onmessage = c, l.onclose = i, l.onerror = (y) => {
440
357
  console.error(y), d == null || d("Websocket failed to connect", y), n(y);
441
358
  }, l.onopen = (y) => {
@@ -444,9 +361,7 @@ function Ye(e) {
444
361
  });
445
362
  }
446
363
  async function Qe(e) {
447
- const {
448
- retries: t = 1
449
- } = e;
364
+ const { retries: t = 1 } = e;
450
365
  let n = null;
451
366
  for (let r = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; r++)
452
367
  try {
@@ -491,10 +406,7 @@ function et(e, t, n, r, s) {
491
406
  const o = r.messages[r.messages.length - 1];
492
407
  if (!(e === q.Partial || e === q.Answer) || (o == null ? void 0 : o.role) !== "assistant")
493
408
  return;
494
- const {
495
- content: c,
496
- sequence: a
497
- } = t;
409
+ const { content: c, sequence: a } = t;
498
410
  e === q.Partial ? n[a] = c : n.answer = c;
499
411
  const i = Ge(n);
500
412
  (o.content !== i || e === q.Answer) && (o.content = i, s == null || s([...r.messages], e));
@@ -511,24 +423,14 @@ function tt(e, t, n, r, s) {
511
423
  mode: t.chatMode
512
424
  });
513
425
  else {
514
- const l = A, y = [l.StreamVideoDone, l.StreamVideoError, l.StreamVideoRejected], _ = [l.StreamFailed, l.StreamVideoError, l.StreamVideoRejected], T = Ke(a, r, {
515
- mode: t.chatMode
516
- });
426
+ const l = A, y = [l.StreamVideoDone, l.StreamVideoError, l.StreamVideoRejected], _ = [l.StreamFailed, l.StreamVideoError, l.StreamVideoRejected], T = Ke(a, r, { mode: t.chatMode });
517
427
  if (c = c, c === l.StreamVideoCreated)
518
428
  e.linkTrack("agent-video", T, l.StreamVideoCreated, ["start"]);
519
429
  else if (y.includes(c)) {
520
430
  const j = c.split("/")[1];
521
- _.includes(c) ? e.track("agent-video", {
522
- ...T,
523
- event: j
524
- }) : e.linkTrack("agent-video", {
525
- ...T,
526
- event: j
527
- }, c, ["done"]);
431
+ _.includes(c) ? e.track("agent-video", { ...T, event: j }) : e.linkTrack("agent-video", { ...T, event: j }, c, ["done"]);
528
432
  }
529
- _.includes(c) && ((d = (i = n.callbacks).onError) == null || d.call(i, new Error(`Stream failed with event ${c}`), {
530
- data: a
531
- })), a.event === l.StreamDone && s();
433
+ _.includes(c) && ((d = (i = n.callbacks).onError) == null || d.call(i, new Error(`Stream failed with event ${c}`), { data: a })), a.event === l.StreamDone && s();
532
434
  }
533
435
  }
534
436
  };
@@ -558,9 +460,7 @@ function rt(e, t, n, r) {
558
460
  });
559
461
  },
560
462
  close(o, c) {
561
- return s.delete(`/streams/${o}`, {
562
- session_id: c
563
- });
463
+ return s.delete(`/streams/${o}`, { session_id: c });
564
464
  }
565
465
  };
566
466
  }
@@ -585,11 +485,10 @@ function nt(e, t, n) {
585
485
  };
586
486
  }
587
487
  function at(e) {
588
- return e.filter((t) => t.freezeCount > 0 || t.framesPerSecond < 21 || t.framesDropped > 0 || t.packetsLost > 0).map((t) => {
589
- const {
590
- timestamp: n,
591
- ...r
592
- } = t, s = [];
488
+ return e.filter(
489
+ (t) => t.freezeCount > 0 || t.framesPerSecond < 21 || t.framesDropped > 0 || t.packetsLost > 0
490
+ ).map((t) => {
491
+ const { timestamp: n, ...r } = t, s = [];
593
492
  return t.freezeCount > 0 && s.push("freeze"), t.framesPerSecond < 21 && s.push("low fps"), t.framesDropped > 0 && s.push("frames dropped"), t.packetsLost > 0 && s.push("packet loss"), {
594
493
  ...r,
595
494
  causes: s
@@ -702,27 +601,16 @@ function lt() {
702
601
  }
703
602
  t = c, n = a;
704
603
  const i = o.framesDecoded, d = i - e > 0;
705
- return e = i, {
706
- isReceiving: d,
707
- avgJitterDelayInInterval: r,
708
- freezeCount: o.freezeCount
709
- };
604
+ return e = i, { isReceiving: d, avgJitterDelayInInterval: r, freezeCount: o.freezeCount };
710
605
  }
711
- return {
712
- isReceiving: !1,
713
- avgJitterDelayInInterval: r
714
- };
606
+ return { isReceiving: !1, avgJitterDelayInInterval: r };
715
607
  };
716
608
  }
717
609
  function ut(e, t, n, r, s, o = !1) {
718
610
  let c = [], a, i = 0, d = !1, l = Q.Unknown, y = Q.Unknown, _ = 0, T = 0;
719
611
  const j = lt();
720
612
  return setInterval(async () => {
721
- const x = await e.getStats(), {
722
- isReceiving: R,
723
- avgJitterDelayInInterval: U,
724
- freezeCount: K
725
- } = j(x), P = it(x);
613
+ const x = await e.getStats(), { isReceiving: R, avgJitterDelayInInterval: U, freezeCount: K } = j(x), P = it(x);
726
614
  if (R)
727
615
  i = 0, _ = K - T, y = U < ct ? Q.Strong : U > dt && _ > 1 ? Q.Weak : l, y !== l && (s == null || s(y), l = y, T += _, _ = 0), d || (r == null || r(h.Start), a = c[c.length - 1], c = [], d = !0), c.push(P);
728
616
  else if (d && (i++, i >= ot)) {
@@ -757,22 +645,9 @@ function mt(e) {
757
645
  const [t, n = ""] = e.split(/:(.+)/);
758
646
  try {
759
647
  const r = JSON.parse(n);
760
- return B("parsed data channel message", {
761
- subject: t,
762
- data: r
763
- }), {
764
- subject: t,
765
- data: r
766
- };
648
+ return B("parsed data channel message", { subject: t, data: r }), { subject: t, data: r };
767
649
  } catch (r) {
768
- return B("Failed to parse data channel message, returning data as string", {
769
- subject: t,
770
- rawData: n,
771
- error: r
772
- }), {
773
- subject: t,
774
- data: n
775
- };
650
+ return B("Failed to parse data channel message, returning data as string", { subject: t, rawData: n, error: r }), { subject: t, data: n };
776
651
  }
777
652
  }
778
653
  function gt({
@@ -800,12 +675,7 @@ function se({
800
675
  streamType: s,
801
676
  report: o
802
677
  }) {
803
- s === W.Legacy ? gt({
804
- statsSignal: e,
805
- dataChannelSignal: t,
806
- onVideoStateChange: n,
807
- report: o
808
- }) : s === W.Fluent && ht({
678
+ s === W.Legacy ? gt({ statsSignal: e, dataChannelSignal: t, onVideoStateChange: n, report: o }) : s === W.Fluent && ht({
809
679
  statsSignal: e,
810
680
  dataChannelSignal: t,
811
681
  onVideoStateChange: n,
@@ -813,23 +683,16 @@ function se({
813
683
  report: o
814
684
  });
815
685
  }
816
- async function wt(e, t, {
817
- debug: n = !1,
818
- callbacks: r,
819
- auth: s,
820
- baseURL: o = Z,
821
- analytics: c
822
- }) {
686
+ async function wt(e, t, { debug: n = !1, callbacks: r, auth: s, baseURL: o = Z, analytics: c }) {
823
687
  var J;
824
688
  pe = n;
825
689
  let a = !1, i = !1, d = h.Stop, l = h.Stop;
826
- const {
827
- startConnection: y,
828
- sendStreamRequest: _,
829
- close: T,
830
- createStream: j,
831
- addIceCandidate: x
832
- } = rt(s, o, e, r.onError), {
690
+ const { startConnection: y, sendStreamRequest: _, close: T, createStream: j, addIceCandidate: x } = rt(
691
+ s,
692
+ o,
693
+ e,
694
+ r.onError
695
+ ), {
833
696
  id: R,
834
697
  offer: U,
835
698
  ice_servers: K,
@@ -837,49 +700,48 @@ async function wt(e, t, {
837
700
  fluent: u,
838
701
  interrupt_enabled: v
839
702
  } = await j(t);
840
- (J = r.onStreamCreated) == null || J.call(r, {
841
- stream_id: R,
842
- session_id: P,
843
- agent_id: e
844
- });
845
- const m = new ft({
846
- iceServers: K
847
- }), D = m.createDataChannel("JanusDataChannel");
703
+ (J = r.onStreamCreated) == null || J.call(r, { stream_id: R, session_id: P, agent_id: e });
704
+ const m = new ft({ iceServers: K }), D = m.createDataChannel("JanusDataChannel");
848
705
  if (!P)
849
706
  throw new Error("Could not create session_id");
850
707
  const w = u ? W.Fluent : W.Legacy;
851
- c.enrich({
852
- "stream-type": w
853
- });
708
+ c.enrich({ "stream-type": w });
854
709
  const p = t.stream_warmup && !u, b = () => a, L = () => {
855
710
  var f;
856
711
  a = !0, i && ((f = r.onConnectionStateChange) == null || f.call(r, I.Connected));
857
- }, $ = ut(m, b, L, (f, g) => se({
858
- statsSignal: l = f,
859
- dataChannelSignal: w === W.Legacy ? d : void 0,
860
- onVideoStateChange: r.onVideoStateChange,
861
- onAgentActivityStateChange: r.onAgentActivityStateChange,
862
- report: g,
863
- streamType: w
864
- }), (f) => {
865
- var g;
866
- return (g = r.onConnectivityStateChange) == null ? void 0 : g.call(r, f);
867
- }, p);
712
+ }, $ = ut(
713
+ m,
714
+ b,
715
+ L,
716
+ (f, g) => se({
717
+ statsSignal: l = f,
718
+ dataChannelSignal: w === W.Legacy ? d : void 0,
719
+ onVideoStateChange: r.onVideoStateChange,
720
+ onAgentActivityStateChange: r.onAgentActivityStateChange,
721
+ report: g,
722
+ streamType: w
723
+ }),
724
+ (f) => {
725
+ var g;
726
+ return (g = r.onConnectivityStateChange) == null ? void 0 : g.call(r, f);
727
+ },
728
+ p
729
+ );
868
730
  m.onicecandidate = (f) => {
869
731
  var g;
870
732
  B("peerConnection.onicecandidate", f);
871
733
  try {
872
- f.candidate && f.candidate.sdpMid && f.candidate.sdpMLineIndex !== null ? x(R, {
873
- candidate: f.candidate.candidate,
874
- sdpMid: f.candidate.sdpMid,
875
- sdpMLineIndex: f.candidate.sdpMLineIndex
876
- }, P) : x(R, {
877
- candidate: null
878
- }, P);
734
+ f.candidate && f.candidate.sdpMid && f.candidate.sdpMLineIndex !== null ? x(
735
+ R,
736
+ {
737
+ candidate: f.candidate.candidate,
738
+ sdpMid: f.candidate.sdpMid,
739
+ sdpMLineIndex: f.candidate.sdpMLineIndex
740
+ },
741
+ P
742
+ ) : x(R, { candidate: null }, P);
879
743
  } catch (S) {
880
- (g = r.onError) == null || g.call(r, S, {
881
- streamId: R
882
- });
744
+ (g = r.onError) == null || g.call(r, S, { streamId: R });
883
745
  }
884
746
  }, D.onopen = () => {
885
747
  i = !0, (!p || a) && L();
@@ -903,11 +765,7 @@ async function wt(e, t, {
903
765
  }
904
766
  function C(f, g) {
905
767
  const S = typeof g == "string" ? g : g == null ? void 0 : g.metadata;
906
- S && c.enrich({
907
- streamMetadata: S
908
- }), c.track("agent-chat", {
909
- event: "ready"
910
- });
768
+ S && c.enrich({ streamMetadata: S }), c.track("agent-chat", { event: "ready" });
911
769
  }
912
770
  const z = {
913
771
  [A.StreamStarted]: E,
@@ -916,10 +774,7 @@ async function wt(e, t, {
916
774
  };
917
775
  D.onmessage = (f) => {
918
776
  var X;
919
- const {
920
- subject: g,
921
- data: S
922
- } = mt(f.data);
777
+ const { subject: g, data: S } = mt(f.data);
923
778
  (X = z[g]) == null || X.call(z, g, S);
924
779
  }, m.oniceconnectionstatechange = () => {
925
780
  var g;
@@ -976,9 +831,7 @@ async function wt(e, t, {
976
831
  try {
977
832
  D.send(f);
978
833
  } catch (X) {
979
- B("Error sending data channel message", X), (S = r.onError) == null || S.call(r, X, {
980
- streamId: R
981
- });
834
+ B("Error sending data channel message", X), (S = r.onError) == null || S.call(r, X, { streamId: R });
982
835
  }
983
836
  },
984
837
  /**
@@ -994,9 +847,7 @@ async function wt(e, t, {
994
847
  };
995
848
  }
996
849
  function pt(e) {
997
- const {
998
- streamOptions: t
999
- } = e ?? {};
850
+ const { streamOptions: t } = e ?? {};
1000
851
  return {
1001
852
  output_resolution: t == null ? void 0 : t.outputResolution,
1002
853
  session_timeout: t == null ? void 0 : t.sessionTimeout,
@@ -1009,10 +860,7 @@ function yt(e, t, n, r, s) {
1009
860
  s === W.Fluent ? vt(e, t, n, r, s) : Dt(e, t, n, r, s);
1010
861
  }
1011
862
  function vt(e, t, n, r, s) {
1012
- e === h.Start ? r.track("stream-session", {
1013
- event: "start",
1014
- "stream-type": s
1015
- }) : e === h.Stop && r.track("stream-session", {
863
+ e === h.Start ? r.track("stream-session", { event: "start", "stream-type": s }) : e === h.Stop && r.track("stream-session", {
1016
864
  event: "stop",
1017
865
  is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
1018
866
  background: t.presenter.type === "clip" && t.presenter.background,
@@ -1021,29 +869,41 @@ function vt(e, t, n, r, s) {
1021
869
  });
1022
870
  }
1023
871
  function kt(e, t, n, r) {
1024
- N.get() <= 0 || (e === h.Start ? n.linkTrack("agent-video", {
1025
- event: "start",
1026
- latency: N.get(!0),
1027
- "stream-type": r
1028
- }, "start", [A.StreamVideoCreated]) : e === h.Stop && n.linkTrack("agent-video", {
1029
- event: "stop",
1030
- is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
1031
- background: t.presenter.type === "clip" && t.presenter.background,
1032
- "stream-type": r
1033
- }, "done", [A.StreamVideoDone]));
872
+ N.get() <= 0 || (e === h.Start ? n.linkTrack(
873
+ "agent-video",
874
+ { event: "start", latency: N.get(!0), "stream-type": r },
875
+ "start",
876
+ [A.StreamVideoCreated]
877
+ ) : e === h.Stop && n.linkTrack(
878
+ "agent-video",
879
+ {
880
+ event: "stop",
881
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
882
+ background: t.presenter.type === "clip" && t.presenter.background,
883
+ "stream-type": r
884
+ },
885
+ "done",
886
+ [A.StreamVideoDone]
887
+ ));
1034
888
  }
1035
889
  function Dt(e, t, n, r, s) {
1036
- N.get() <= 0 || (e === h.Start ? r.linkTrack("agent-video", {
1037
- event: "start",
1038
- latency: N.get(!0),
1039
- "stream-type": s
1040
- }, "start", [A.StreamVideoCreated]) : e === h.Stop && r.linkTrack("agent-video", {
1041
- event: "stop",
1042
- is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
1043
- background: t.presenter.type === "clip" && t.presenter.background,
1044
- "stream-type": s,
1045
- ...n
1046
- }, "done", [A.StreamVideoDone]));
890
+ N.get() <= 0 || (e === h.Start ? r.linkTrack(
891
+ "agent-video",
892
+ { event: "start", latency: N.get(!0), "stream-type": s },
893
+ "start",
894
+ [A.StreamVideoCreated]
895
+ ) : e === h.Stop && r.linkTrack(
896
+ "agent-video",
897
+ {
898
+ event: "stop",
899
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
900
+ background: t.presenter.type === "clip" && t.presenter.background,
901
+ "stream-type": s,
902
+ ...n
903
+ },
904
+ "done",
905
+ [A.StreamVideoDone]
906
+ ));
1047
907
  }
1048
908
  function Ct(e, t, n) {
1049
909
  return N.reset(), new Promise(async (r, s) => {
@@ -1059,11 +919,22 @@ function Ct(e, t, n) {
1059
919
  },
1060
920
  onVideoStateChange: (c, a) => {
1061
921
  var i, d;
1062
- (d = (i = t.callbacks).onVideoStateChange) == null || d.call(i, c), yt(c, e, a, n, o.streamType);
922
+ (d = (i = t.callbacks).onVideoStateChange) == null || d.call(i, c), yt(
923
+ c,
924
+ e,
925
+ a,
926
+ n,
927
+ o.streamType
928
+ );
1063
929
  },
1064
930
  onAgentActivityStateChange: (c) => {
1065
931
  var a, i;
1066
- (i = (a = t.callbacks).onAgentActivityStateChange) == null || i.call(a, c), c === O.Talking ? re.update() : re.reset(), kt(c === O.Talking ? h.Start : h.Stop, e, n, o.streamType);
932
+ (i = (a = t.callbacks).onAgentActivityStateChange) == null || i.call(a, c), c === O.Talking ? re.update() : re.reset(), kt(
933
+ c === O.Talking ? h.Start : h.Stop,
934
+ e,
935
+ n,
936
+ o.streamType
937
+ );
1067
938
  }
1068
939
  }
1069
940
  });
@@ -1074,16 +945,8 @@ function Ct(e, t, n) {
1074
945
  }
1075
946
  async function Rt(e, t, n, r, s) {
1076
947
  var y, _, T, j;
1077
- const o = we(e, n, r, t.mode, t.persistentChat, s), c = Ct(e, t, r), [a, i] = await Promise.all([o, c]), {
1078
- chat: d,
1079
- chatMode: l
1080
- } = a;
1081
- return l && l !== t.mode && (t.mode = l, (_ = (y = t.callbacks).onModeChange) == null || _.call(y, l), l !== M.Functional) ? ((j = (T = t.callbacks).onError) == null || j.call(T, new De(l)), i == null || i.disconnect(), {
1082
- chat: d
1083
- }) : {
1084
- chat: d,
1085
- streamingManager: i
1086
- };
948
+ const o = we(e, n, r, t.mode, t.persistentChat, s), c = Ct(e, t, r), [a, i] = await Promise.all([o, c]), { chat: d, chatMode: l } = a;
949
+ return l && l !== t.mode && (t.mode = l, (_ = (y = t.callbacks).onModeChange) == null || _.call(y, l), l !== M.Functional) ? ((j = (T = t.callbacks).onError) == null || j.call(T, new De(l)), i == null || i.disconnect(), { chat: d }) : { chat: d, streamingManager: i };
1087
950
  }
1088
951
  async function Et(e, t) {
1089
952
  var U, K, P;
@@ -1097,52 +960,47 @@ async function Et(e, t) {
1097
960
  isEnabled: t.enableAnalitics,
1098
961
  distinctId: t.distinctId
1099
962
  });
1100
- i.track("agent-sdk", {
1101
- event: "init"
1102
- });
963
+ i.track("agent-sdk", { event: "init" });
1103
964
  const d = fe(t.auth, c, t.callbacks.onError), l = await d.getById(e);
1104
965
  i.enrich(We(l));
1105
- const {
1106
- onMessage: y,
1107
- clearQueue: _
1108
- } = tt(i, a, t, l, () => {
1109
- var u;
1110
- return (u = a.socketManager) == null ? void 0 : u.disconnect();
1111
- });
966
+ const { onMessage: y, clearQueue: _ } = tt(
967
+ i,
968
+ a,
969
+ t,
970
+ l,
971
+ () => {
972
+ var u;
973
+ return (u = a.socketManager) == null ? void 0 : u.disconnect();
974
+ }
975
+ );
1112
976
  a.messages = Ve(t.initialMessages), (K = (U = t.callbacks).onNewMessage) == null || K.call(U, [...a.messages], "answer");
1113
977
  const T = (u) => {
1114
978
  r = u;
1115
979
  };
1116
- i.track("agent-sdk", {
1117
- event: "loaded",
1118
- ...Je(l)
1119
- });
980
+ i.track("agent-sdk", { event: "loaded", ...Je(l) });
1120
981
  async function j(u) {
1121
982
  var b, L, $, k, E, C, z;
1122
983
  (L = (b = t.callbacks).onConnectionStateChange) == null || L.call(b, I.Connecting), N.reset(), u && !n && (delete a.chat, (k = ($ = t.callbacks).onNewMessage) == null || k.call($, [...a.messages], "answer"));
1123
- const v = t.mode === M.DirectPlayback ? Promise.resolve(void 0) : Ze(t.auth, o, {
1124
- onMessage: y,
1125
- onError: t.callbacks.onError
1126
- }), m = te(() => Rt(l, {
1127
- ...t,
1128
- callbacks: {
1129
- ...t.callbacks,
1130
- onVideoIdChange: T
984
+ const v = t.mode === M.DirectPlayback ? Promise.resolve(void 0) : Ze(t.auth, o, { onMessage: y, onError: t.callbacks.onError }), m = te(
985
+ () => Rt(
986
+ l,
987
+ { ...t, callbacks: { ...t.callbacks, onVideoIdChange: T } },
988
+ d,
989
+ i,
990
+ a.chat
991
+ ),
992
+ {
993
+ limit: 3,
994
+ timeout: Ae,
995
+ timeoutErrorMessage: "Timeout initializing the stream",
996
+ // Retry on all errors except for connection errors and rate limit errors, these are already handled in client level.
997
+ shouldRetryFn: (F) => (F == null ? void 0 : F.message) !== "Could not connect" && F.status !== 429,
998
+ delayMs: 1e3
1131
999
  }
1132
- }, d, i, a.chat), {
1133
- limit: 3,
1134
- timeout: Ae,
1135
- timeoutErrorMessage: "Timeout initializing the stream",
1136
- // Retry on all errors except for connection errors and rate limit errors, these are already handled in client level.
1137
- shouldRetryFn: (F) => (F == null ? void 0 : F.message) !== "Could not connect" && F.status !== 429,
1138
- delayMs: 1e3
1139
- }).catch((F) => {
1000
+ ).catch((F) => {
1140
1001
  var J, f;
1141
1002
  throw R(M.Maintenance), (f = (J = t.callbacks).onConnectionStateChange) == null || f.call(J, I.Fail), F;
1142
- }), [D, {
1143
- streamingManager: w,
1144
- chat: p
1145
- }] = await Promise.all([v, m]);
1003
+ }), [D, { streamingManager: w, chat: p }] = await Promise.all([v, m]);
1146
1004
  p && p.id !== ((E = a.chat) == null ? void 0 : E.id) && ((z = (C = t.callbacks).onNewChat) == null || z.call(C, p.id)), a.streamingManager = w, a.socketManager = D, a.chat = p, n = !1, i.enrich({
1147
1005
  chatId: p == null ? void 0 : p.id,
1148
1006
  streamId: w == null ? void 0 : w.streamId,
@@ -1155,9 +1013,7 @@ async function Et(e, t) {
1155
1013
  }
1156
1014
  async function R(u) {
1157
1015
  var v, m;
1158
- u !== a.chatMode && (i.track("agent-mode-change", {
1159
- mode: u
1160
- }), a.chatMode = u, a.chatMode !== M.Functional && await x(), (m = (v = t.callbacks).onModeChange) == null || m.call(v, u));
1016
+ u !== a.chatMode && (i.track("agent-mode-change", { mode: u }), a.chatMode = u, a.chatMode !== M.Functional && await x(), (m = (v = t.callbacks).onModeChange) == null || m.call(v, u));
1161
1017
  }
1162
1018
  return {
1163
1019
  agent: l,
@@ -1211,37 +1067,48 @@ async function Et(e, t) {
1211
1067
  }, m = async () => {
1212
1068
  var k, E;
1213
1069
  if (!a.chat) {
1214
- const C = await we(l, d, i, a.chatMode, t.persistentChat);
1070
+ const C = await we(
1071
+ l,
1072
+ d,
1073
+ i,
1074
+ a.chatMode,
1075
+ t.persistentChat
1076
+ );
1215
1077
  if (!C.chat)
1216
1078
  throw new ke(a.chatMode, !!t.persistentChat);
1217
1079
  a.chat = C.chat, (E = (k = t.callbacks).onNewChat) == null || E.call(k, a.chat.id);
1218
1080
  }
1219
1081
  return a.chat.id;
1220
- }, D = async (k, E) => te(() => {
1221
- var C, z;
1222
- return d.chat(l.id, E, {
1223
- chatMode: a.chatMode,
1224
- streamId: (C = a.streamingManager) == null ? void 0 : C.streamId,
1225
- sessionId: (z = a.streamingManager) == null ? void 0 : z.sessionId,
1226
- messages: k.map(({
1227
- matches: F,
1228
- ...J
1229
- }) => J)
1230
- }, {
1231
- ...he(a.chatMode),
1232
- skipErrorHandler: !0
1233
- });
1234
- }, {
1235
- limit: 2,
1236
- shouldRetryFn: (C) => {
1237
- var J, f, g, S;
1238
- const z = (J = C == null ? void 0 : C.message) == null ? void 0 : J.includes("missing or invalid session_id");
1239
- return !((f = C == null ? void 0 : C.message) == null ? void 0 : f.includes("Stream Error")) && !z ? ((S = (g = t.callbacks).onError) == null || S.call(g, C), !1) : !0;
1082
+ }, D = async (k, E) => te(
1083
+ () => {
1084
+ var C, z;
1085
+ return d.chat(
1086
+ l.id,
1087
+ E,
1088
+ {
1089
+ chatMode: a.chatMode,
1090
+ streamId: (C = a.streamingManager) == null ? void 0 : C.streamId,
1091
+ sessionId: (z = a.streamingManager) == null ? void 0 : z.sessionId,
1092
+ messages: k.map(({ matches: F, ...J }) => J)
1093
+ },
1094
+ {
1095
+ ...he(a.chatMode),
1096
+ skipErrorHandler: !0
1097
+ }
1098
+ );
1240
1099
  },
1241
- onRetry: async () => {
1242
- await x(), await j(!1);
1100
+ {
1101
+ limit: 2,
1102
+ shouldRetryFn: (C) => {
1103
+ var J, f, g, S;
1104
+ const z = (J = C == null ? void 0 : C.message) == null ? void 0 : J.includes("missing or invalid session_id");
1105
+ return !((f = C == null ? void 0 : C.message) == null ? void 0 : f.includes("Stream Error")) && !z ? ((S = (g = t.callbacks).onError) == null || S.call(g, C), !1) : !0;
1106
+ },
1107
+ onRetry: async () => {
1108
+ await x(), await j(!1);
1109
+ }
1243
1110
  }
1244
- });
1111
+ );
1245
1112
  try {
1246
1113
  _(), v(), a.messages.push({
1247
1114
  id: V(),
@@ -1277,8 +1144,7 @@ async function Et(e, t) {
1277
1144
  if (a.chat) {
1278
1145
  if (!D)
1279
1146
  throw new Error("Message not found");
1280
- } else
1281
- throw new Error("Chat is not initialized");
1147
+ } else throw new Error("Chat is not initialized");
1282
1148
  const w = ((p = D.matches) == null ? void 0 : p.map((k) => [k.document_id, k.id])) ?? [];
1283
1149
  return i.track("agent-rate", {
1284
1150
  event: m ? "update" : "create",
@@ -1301,9 +1167,7 @@ async function Et(e, t) {
1301
1167
  deleteRate(u) {
1302
1168
  if (!a.chat)
1303
1169
  throw new Error("Chat is not initialized");
1304
- return i.track("agent-rate-delete", {
1305
- type: "text"
1306
- }), d.deleteRating(l.id, a.chat.id, u);
1170
+ return i.track("agent-rate-delete", { type: "text" }), d.deleteRating(l.id, a.chat.id, u);
1307
1171
  },
1308
1172
  async speak(u) {
1309
1173
  var w, p, b;
@@ -1346,15 +1210,10 @@ async function Et(e, t) {
1346
1210
  throw new Error("Please connect to the agent first");
1347
1211
  return a.streamingManager.speak({
1348
1212
  script: m,
1349
- metadata: {
1350
- chat_id: (b = a.chat) == null ? void 0 : b.id,
1351
- agent_id: l.id
1352
- }
1213
+ metadata: { chat_id: (b = a.chat) == null ? void 0 : b.id, agent_id: l.id }
1353
1214
  });
1354
1215
  },
1355
- async interrupt({
1356
- type: u
1357
- }) {
1216
+ async interrupt({ type: u }) {
1358
1217
  var m, D, w;
1359
1218
  Oe(a.streamingManager, (m = a.streamingManager) == null ? void 0 : m.streamType, r);
1360
1219
  const v = a.messages[a.messages.length - 1];
@@ -1367,9 +1226,7 @@ async function Et(e, t) {
1367
1226
  };
1368
1227
  }
1369
1228
  function Mt(e, t, n) {
1370
- const {
1371
- getById: r
1372
- } = fe(t, n || Z);
1229
+ const { getById: r } = fe(t, n || Z);
1373
1230
  return r(e);
1374
1231
  }
1375
1232
  export {