@d-id/client-sdk 1.1.0-beta.2 → 1.1.0-beta.21

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/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- var me = Object.defineProperty;
2
- var ue = (e, t, a) => t in e ? me(e, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : e[t] = a;
3
- var H = (e, t, a) => (ue(e, typeof t != "symbol" ? t + "" : t, a), a);
4
- class J extends Error {
1
+ var ye = Object.defineProperty;
2
+ var ve = (e, t, a) => t in e ? ye(e, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : e[t] = a;
3
+ var X = (e, t, a) => (ve(e, typeof t != "symbol" ? t + "" : t, a), a);
4
+ class Z extends Error {
5
5
  constructor({
6
6
  kind: a,
7
7
  description: n,
@@ -11,13 +11,13 @@ class J extends Error {
11
11
  kind: a,
12
12
  description: n
13
13
  }));
14
- H(this, "kind");
15
- H(this, "description");
16
- H(this, "error");
14
+ X(this, "kind");
15
+ X(this, "description");
16
+ X(this, "error");
17
17
  this.kind = a, this.description = n, this.error = s;
18
18
  }
19
19
  }
20
- class fe extends J {
20
+ class ke extends Z {
21
21
  constructor(t, a) {
22
22
  super({
23
23
  kind: "ChatCreationFailed",
@@ -25,7 +25,7 @@ class fe extends J {
25
25
  });
26
26
  }
27
27
  }
28
- class ge extends J {
28
+ class De extends Z {
29
29
  constructor(t) {
30
30
  super({
31
31
  kind: "ChatModeDowngraded",
@@ -33,17 +33,17 @@ class ge extends J {
33
33
  });
34
34
  }
35
35
  }
36
- class F extends J {
36
+ class K extends Z {
37
37
  constructor(a, n) {
38
38
  super({
39
39
  kind: "ValidationError",
40
40
  description: a
41
41
  });
42
- H(this, "key");
42
+ X(this, "key");
43
43
  this.key = n;
44
44
  }
45
45
  }
46
- class he extends J {
46
+ class Re extends Z {
47
47
  constructor(t) {
48
48
  super({
49
49
  kind: "WSError",
@@ -51,8 +51,8 @@ class he extends J {
51
51
  });
52
52
  }
53
53
  }
54
- var we = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(we || {}), pe = /* @__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))(pe || {}), ye = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(ye || {}), ke = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(ke || {}), I = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e))(I || {}), B = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(B || {}), ve = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(ve || {}), Re = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(Re || {}), Ce = /* @__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))(Ce || {}), ne = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(ne || {});
55
- const De = (e) => {
54
+ var Ce = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(Ce || {}), _e = /* @__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))(_e || {}), Se = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(Se || {}), Ie = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(Ie || {}), A = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e))(A || {}), H = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(H || {}), Ee = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(Ee || {}), Me = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(Me || {}), 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 || {}), ce = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(ce || {});
55
+ const be = (e) => {
56
56
  switch (e) {
57
57
  case "clip":
58
58
  return "clip";
@@ -62,9 +62,12 @@ const De = (e) => {
62
62
  throw new Error(`Unknown video type: ${e}`);
63
63
  }
64
64
  };
65
- var S = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(S || {}), U = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(U || {}), D = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(D || {}), Se = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(Se || {}), _e = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(_e || {});
66
- const Me = 45 * 1e3, Ee = "X-Playground-Chat", K = "https://api.d-id.com", be = "wss://notifications.d-id.com", Ie = "79f81a83a67430be2bc0fd61042b8faa", ae = (e) => new Promise((t) => setTimeout(t, e)), q = () => Math.random().toString(16).slice(2);
67
- function Pe(e, t) {
65
+ var p = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(p || {}), Y = /* @__PURE__ */ ((e) => (e.Strong = "STRONG", e.Weak = "WEAK", e.Unknown = "UNKNOWN", e))(Y || {}), V = /* @__PURE__ */ ((e) => (e.Idle = "IDLE", e.Talking = "TALKING", e))(V || {}), F = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamInterrupt = "stream/interrupt", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(F || {}), j = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(j || {}), W = /* @__PURE__ */ ((e) => (e.Legacy = "legacy", e.Fluent = "fluent", e))(W || {}), je = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(je || {}), Pe = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(Pe || {});
66
+ const Ae = 45 * 1e3, Be = "X-Playground-Chat", Q = "https://api.d-id.com", $e = "wss://notifications.d-id.com", Le = "79f81a83a67430be2bc0fd61042b8faa", de = (e) => new Promise((t) => setTimeout(t, e)), q = (e = 16) => {
67
+ const t = new Uint8Array(e);
68
+ return window.crypto.getRandomValues(t), Array.from(t, (a) => a.toString(16).padStart(2, "0")).join("").slice(0, 13);
69
+ }, ze = (e) => [A.TextOnly, A.Playground, A.Maintenance].includes(e);
70
+ function Ne(e, t) {
68
71
  let a;
69
72
  return {
70
73
  promise: new Promise((s, i) => {
@@ -73,7 +76,7 @@ function Pe(e, t) {
73
76
  clear: () => clearTimeout(a)
74
77
  };
75
78
  }
76
- async function X(e, t) {
79
+ async function ee(e, t) {
77
80
  const a = {
78
81
  limit: (t == null ? void 0 : t.limit) ?? 3,
79
82
  delayMs: (t == null ? void 0 : t.delayMs) ?? 0,
@@ -90,59 +93,63 @@ async function X(e, t) {
90
93
  return await e();
91
94
  const {
92
95
  promise: i,
93
- clear: r
94
- } = Pe(a.timeout, a.timeoutErrorMessage), o = e().finally(r);
95
- return await Promise.race([o, i]);
96
+ clear: o
97
+ } = Ne(a.timeout, a.timeoutErrorMessage), c = e().finally(o);
98
+ return await Promise.race([c, i]);
96
99
  } catch (i) {
97
100
  if (n = i, !a.shouldRetryFn(i) || s >= a.limit)
98
101
  throw i;
99
- await ae(a.delayMs), a.onRetry(i);
102
+ await de(a.delayMs), a.onRetry(i);
100
103
  }
101
104
  throw n;
102
105
  }
103
- function ie() {
106
+ function le() {
104
107
  let e = window.localStorage.getItem("did_external_key_id");
105
- return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
108
+ if (!e) {
109
+ let t = q();
110
+ window.localStorage.setItem("did_external_key_id", t), e = t;
111
+ }
112
+ return e;
106
113
  }
107
- let Te = q();
108
- function se(e) {
114
+ let xe = q();
115
+ function ue(e) {
109
116
  if (e.type === "bearer")
110
117
  return `Bearer ${e.token}`;
111
118
  if (e.type === "basic")
112
119
  return `Basic ${btoa(`${e.username}:${e.password}`)}`;
113
120
  if (e.type === "key")
114
- return `Client-Key ${e.clientKey}.${ie()}_${Te}`;
121
+ return `Client-Key ${e.clientKey}.${le()}_${xe}`;
115
122
  throw new Error(`Unknown auth type: ${e}`);
116
123
  }
117
- const $e = (e) => X(e, {
124
+ const Fe = (e) => ee(e, {
118
125
  limit: 3,
119
126
  delayMs: 1e3,
120
127
  timeout: 0,
121
128
  shouldRetryFn: (t) => t.status === 429
122
129
  });
123
- function Q(e, t = K, a) {
130
+ function ne(e, t = Q, a) {
124
131
  const n = async (s, i) => {
125
132
  const {
126
- skipErrorHandler: r,
127
- ...o
128
- } = i || {}, c = await $e(() => fetch(t + (s != null && s.startsWith("/") ? s : `/${s}`), {
129
- ...o,
133
+ skipErrorHandler: o,
134
+ ...c
135
+ } = i || {}, r = await Fe(() => fetch(t + (s != null && s.startsWith("/") ? s : `/${s}`), {
136
+ ...c,
130
137
  headers: {
131
- ...o.headers,
132
- Authorization: se(e),
138
+ ...c.headers,
139
+ Authorization: ue(e),
133
140
  "Content-Type": "application/json"
134
141
  }
135
142
  }));
136
- if (!c.ok) {
137
- let u = await c.text().catch(() => `Failed to fetch with status ${c.status}`);
138
- const d = new Error(u);
139
- throw a && !r && a(d, {
143
+ if (!r.ok) {
144
+ let l = await r.text().catch(() => `Failed to fetch with status ${r.status}`);
145
+ const d = new Error(l);
146
+ throw a && !o && a(d, {
140
147
  url: s,
141
- options: o,
142
- headers: c.headers
148
+ options: c,
149
+ headers: r.headers
143
150
  }), d;
144
151
  }
145
- return c.json();
152
+ return r.json();
146
153
  };
147
154
  return {
148
155
  get(s, i) {
@@ -151,37 +158,37 @@ function Q(e, t = K, a) {
151
158
  method: "GET"
152
159
  });
153
160
  },
154
- post(s, i, r) {
161
+ post(s, i, o) {
155
162
  return n(s, {
156
- ...r,
163
+ ...o,
157
164
  body: JSON.stringify(i),
158
165
  method: "POST"
159
166
  });
160
167
  },
161
- delete(s, i, r) {
168
+ delete(s, i, o) {
162
169
  return n(s, {
163
- ...r,
170
+ ...o,
164
171
  body: JSON.stringify(i),
165
172
  method: "DELETE"
166
173
  });
167
174
  },
168
- patch(s, i, r) {
175
+ patch(s, i, o) {
169
176
  return n(s, {
170
- ...r,
177
+ ...o,
171
178
  body: JSON.stringify(i),
172
179
  method: "PATCH"
173
180
  });
174
181
  }
175
182
  };
176
183
  }
177
- function oe(e, t = K, a) {
178
- const n = Q(e, `${t}/agents`, a);
184
+ function fe(e, t = Q, a) {
185
+ const n = ne(e, `${t}/agents`, a);
179
186
  return {
180
187
  create(s, i) {
181
188
  return n.post("/", s, i);
182
189
  },
183
190
  getAgents(s, i) {
184
- return n.get(`/${s ? `?tag=${s}` : ""}`, i).then((r) => r ?? []);
191
+ return n.get(`/${s ? `?tag=${s}` : ""}`, i).then((o) => o ?? []);
185
192
  },
186
193
  getById(s, i) {
187
194
  return n.get(`/${s}`, i);
@@ -189,58 +196,59 @@ function oe(e, t = K, a) {
189
196
  delete(s, i) {
190
197
  return n.delete(`/${s}`, void 0, i);
191
198
  },
192
- update(s, i, r) {
193
- return n.patch(`/${s}`, i, r);
199
+ update(s, i, o) {
200
+ return n.patch(`/${s}`, i, o);
194
201
  },
195
- newChat(s, i, r) {
196
- return n.post(`/${s}/chat`, i, r);
202
+ newChat(s, i, o) {
203
+ return n.post(`/${s}/chat`, i, o);
197
204
  },
198
- chat(s, i, r, o) {
199
- return n.post(`/${s}/chat/${i}`, r, o);
205
+ chat(s, i, o, c) {
206
+ return n.post(`/${s}/chat/${i}`, o, c);
200
207
  },
201
- createRating(s, i, r, o) {
202
- return n.post(`/${s}/chat/${i}/ratings`, r, o);
208
+ createRating(s, i, o, c) {
209
+ return n.post(`/${s}/chat/${i}/ratings`, o, c);
203
210
  },
204
- updateRating(s, i, r, o, c) {
205
- return n.patch(`/${s}/chat/${i}/ratings/${r}`, o, c);
211
+ updateRating(s, i, o, c, r) {
212
+ return n.patch(`/${s}/chat/${i}/ratings/${o}`, c, r);
206
213
  },
207
- deleteRating(s, i, r, o) {
208
- return n.delete(`/${s}/chat/${i}/ratings/${r}`, o);
214
+ deleteRating(s, i, o, c) {
215
+ return n.delete(`/${s}/chat/${i}/ratings/${o}`, c);
209
216
  },
210
217
  getSTTToken(s, i) {
211
218
  return n.get(`/${s}/stt-token`, i);
212
219
  }
213
220
  };
214
221
  }
215
- const Z = (e) => e.type === "clip" && e.presenter_id.startsWith("v2_") ? "clip_v2" : e.type;
216
- function Ae(e) {
217
- var s, i, r, o;
222
+ const me = (e) => e.type === "clip" && e.presenter_id.startsWith("v2_") ? "clip_v2" : e.type;
223
+ function Je(e) {
224
+ var s, i, o, c;
218
225
  const t = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop", a = () => {
219
- const c = navigator.platform;
220
- return c.toLowerCase().includes("win") ? "Windows" : c.toLowerCase().includes("mac") ? "Mac OS X" : c.toLowerCase().includes("linux") ? "Linux" : "Unknown";
226
+ const r = navigator.platform;
227
+ return r.toLowerCase().includes("win") ? "Windows" : r.toLowerCase().includes("mac") ? "Mac OS X" : r.toLowerCase().includes("linux") ? "Linux" : "Unknown";
221
228
  }, n = e.presenter;
222
229
  return {
223
230
  $os: `${a()}`,
224
231
  isMobile: `${t() == "Mobile"}`,
225
232
  browser: navigator.userAgent,
226
233
  origin: window.location.origin,
227
- agentType: Z(n),
234
+ agentType: me(n),
228
235
  agentVoice: {
229
236
  voiceId: (i = (s = e.presenter) == null ? void 0 : s.voice) == null ? void 0 : i.voice_id,
230
- provider: (o = (r = e.presenter) == null ? void 0 : r.voice) == null ? void 0 : o.type
237
+ provider: (c = (o = e.presenter) == null ? void 0 : o.voice) == null ? void 0 : c.type
231
238
  }
232
239
  };
233
240
  }
234
- function ze(e, t, a) {
235
- var c, u, d;
241
+ const We = (e) => e.reduce((t, a) => t + a, 0), ie = (e) => We(e) / e.length;
242
+ function Ue(e, t, a) {
243
+ var r, l, d;
236
244
  const {
237
245
  event: n,
238
246
  ...s
239
247
  } = e, {
240
248
  template: i
241
249
  } = (t == null ? void 0 : t.llm) || {}, {
242
- language: r
243
- } = ((c = t == null ? void 0 : t.presenter) == null ? void 0 : c.voice) || {};
250
+ language: o
251
+ } = ((r = t == null ? void 0 : t.presenter) == null ? void 0 : r.voice) || {};
244
252
  return {
245
253
  ...s,
246
254
  llm: {
@@ -250,33 +258,33 @@ function ze(e, t, a) {
250
258
  script: {
251
259
  ...s.script,
252
260
  provider: {
253
- ...(u = s == null ? void 0 : s.script) == null ? void 0 : u.provider,
254
- language: r
261
+ ...(l = s == null ? void 0 : s.script) == null ? void 0 : l.provider,
262
+ language: o
255
263
  }
256
264
  },
257
265
  stitch: (t == null ? void 0 : t.presenter.type) === "talk" ? (d = t == null ? void 0 : t.presenter) == null ? void 0 : d.stitch : void 0,
258
266
  ...a
259
267
  };
260
268
  }
261
- let V = {};
262
- const Le = "https://api-js.mixpanel.com/track/?verbose=1&ip=1";
263
- function je(e) {
264
- var i, r, o, c, u, d;
269
+ let G = {};
270
+ const Ke = "https://api-js.mixpanel.com/track/?verbose=1&ip=1";
271
+ function He(e) {
272
+ var i, o, c, r, l, d;
265
273
  const t = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", a = e.agent.presenter, n = (i = e.agent.llm) == null ? void 0 : i.prompt_customization, s = {
266
274
  token: e.token || "testKey",
267
- distinct_id: e.distinctId || ie(),
275
+ distinct_id: e.distinctId || le(),
268
276
  agentId: e.agent.id,
269
- agentType: Z(a),
277
+ agentType: me(a),
270
278
  owner_id: e.agent.owner_id ?? "",
271
- promptVersion: (r = e.agent.llm) == null ? void 0 : r.prompt_version,
279
+ promptVersion: (o = e.agent.llm) == null ? void 0 : o.prompt_version,
272
280
  behavior: {
273
281
  role: n == null ? void 0 : n.role,
274
282
  personality: n == null ? void 0 : n.personality,
275
- instructions: (o = e.agent.llm) == null ? void 0 : o.instructions
283
+ instructions: (c = e.agent.llm) == null ? void 0 : c.instructions
276
284
  },
277
- temperature: (c = e.agent.llm) == null ? void 0 : c.temperature,
285
+ temperature: (r = e.agent.llm) == null ? void 0 : r.temperature,
278
286
  knowledgeSource: n == null ? void 0 : n.knowledge_source,
279
- starterQuestionsCount: (d = (u = e.agent.knowledge) == null ? void 0 : u.starter_message) == null ? void 0 : d.length,
287
+ starterQuestionsCount: (d = (l = e.agent.knowledge) == null ? void 0 : l.starter_message) == null ? void 0 : d.length,
280
288
  topicsToAvoid: n == null ? void 0 : n.topics_to_avoid,
281
289
  maxResponseLength: n == null ? void 0 : n.max_response_length
282
290
  };
@@ -284,35 +292,30 @@ function je(e) {
284
292
  ...s,
285
293
  additionalProperties: {},
286
294
  isEnabled: e.isEnabled ?? !0,
287
- getRandom: () => Math.random().toString(16).slice(2),
288
- enrich(g) {
289
- const R = {};
290
- if (g && typeof g != "object")
291
- throw new Error("properties must be a flat json object");
292
- for (let h in g)
293
- (typeof g[h] == "string" || typeof g[h] == "number") && (R[h] = g[h]);
295
+ getRandom: q,
296
+ enrich(f) {
294
297
  this.additionalProperties = {
295
298
  ...this.additionalProperties,
296
- ...R
299
+ ...f
297
300
  };
298
301
  },
299
- async track(g, R) {
302
+ async track(f, B) {
300
303
  if (!this.isEnabled)
301
304
  return Promise.resolve();
302
305
  const {
303
- audioPath: h,
304
- ...y
305
- } = R || {}, k = {
306
+ audioPath: M,
307
+ ..._
308
+ } = B || {}, k = {
306
309
  method: "POST",
307
310
  headers: {
308
311
  "Content-Type": "application/x-www-form-urlencoded"
309
312
  },
310
313
  body: new URLSearchParams({
311
314
  data: JSON.stringify([{
312
- event: g,
315
+ event: f,
313
316
  properties: {
314
317
  ...this.additionalProperties,
315
- ...y,
318
+ ..._,
316
319
  ...s,
317
320
  source: t,
318
321
  time: Date.now(),
@@ -326,32 +329,32 @@ function je(e) {
326
329
  })
327
330
  };
328
331
  try {
329
- return await fetch(Le, k).then((C) => C.json());
330
- } catch (C) {
331
- return console.error(C);
332
+ return await fetch(Ke, k).then((D) => D.json());
333
+ } catch (D) {
334
+ return console.error(D);
332
335
  }
333
336
  },
334
- linkTrack(g, R, h, y) {
335
- V[g] || (V[g] = {
337
+ linkTrack(f, B, M, _) {
338
+ G[f] || (G[f] = {
336
339
  events: {},
337
340
  resolvedDependencies: []
338
- }), y.includes(h) || y.push(h);
339
- const k = V[g];
340
- if (k.events[h] = {
341
- props: R
342
- }, k.resolvedDependencies.push(h), y.every((A) => k.resolvedDependencies.includes(A))) {
343
- const A = y.reduce((P, l) => k.events[l] ? {
344
- ...P,
345
- ...k.events[l].props
346
- } : P, {});
347
- this.track(g, A), k.resolvedDependencies = k.resolvedDependencies.filter((P) => !y.includes(P)), y.forEach((P) => {
348
- delete k.events[P];
341
+ }), _.includes(M) || _.push(M);
342
+ const k = G[f];
343
+ if (k.events[M] = {
344
+ props: B
345
+ }, k.resolvedDependencies.push(M), _.every((N) => k.resolvedDependencies.includes(N))) {
346
+ const N = _.reduce((L, $) => k.events[$] ? {
347
+ ...L,
348
+ ...k.events[$].props
349
+ } : L, {});
350
+ this.track(f, N), k.resolvedDependencies = k.resolvedDependencies.filter((L) => !_.includes(L)), _.forEach((L) => {
351
+ delete k.events[L];
349
352
  });
350
353
  }
351
354
  }
352
355
  };
353
356
  }
354
- function xe() {
357
+ function ge() {
355
358
  let e = 0;
356
359
  return {
357
360
  reset: () => e = 0,
@@ -359,19 +362,19 @@ function xe() {
359
362
  get: (t = !1) => t ? Date.now() - e : e
360
363
  };
361
364
  }
362
- const N = xe();
363
- function ce(e) {
364
- return e === I.Playground ? {
365
+ const x = ge(), te = ge();
366
+ function he(e) {
367
+ return e === A.Playground ? {
365
368
  headers: {
366
- [Ee]: "true"
369
+ [Be]: "true"
367
370
  }
368
371
  } : {};
369
372
  }
370
- async function de(e, t, a, n, s = !1, i) {
373
+ async function we(e, t, a, n, s = !1, i) {
371
374
  try {
372
- return !i && n !== I.DirectPlayback && (i = await t.newChat(e.id, {
375
+ return !i && n !== A.DirectPlayback && (i = await t.newChat(e.id, {
373
376
  persist: s
374
- }, ce(n)), a.track("agent-chat", {
377
+ }, he(n)), a.track("agent-chat", {
375
378
  event: "created",
376
379
  chat_id: i.id,
377
380
  agent_id: e.id,
@@ -380,76 +383,84 @@ async function de(e, t, a, n, s = !1, i) {
380
383
  chat: i,
381
384
  chatMode: (i == null ? void 0 : i.chat_mode) ?? n
382
385
  };
383
- } catch (r) {
386
+ } catch (o) {
384
387
  try {
385
- const o = JSON.parse(r.message);
386
- if ((o == null ? void 0 : o.kind) === "InsufficientCreditsError")
388
+ const c = JSON.parse(o.message);
389
+ if ((c == null ? void 0 : c.kind) === "InsufficientCreditsError")
387
390
  throw new Error("InsufficientCreditsError");
388
- } catch (o) {
389
- console.error("Error parsing the error message:", o);
391
+ } catch (c) {
392
+ console.error("Error parsing the error message:", c);
390
393
  }
391
394
  throw new Error("Cannot create new chat");
392
395
  }
393
396
  }
394
- function Ne(e) {
395
- var a;
396
- const t = ((a = e.greetings) == null ? void 0 : a.filter((n) => n.length > 0)) ?? [];
397
- return t.length > 0 ? t[Math.floor(Math.random() * t.length)] : `Hi! I'm ${e.preview_name || "My Agent"}. How can I help you?`;
397
+ function qe(e) {
398
+ return e && e.length > 0 ? e : [];
398
399
  }
399
- function ee(e, t) {
400
- return t && t.length > 0 ? t : [{
401
- content: e,
402
- id: q(),
403
- role: "assistant",
404
- created_at: (/* @__PURE__ */ new Date()).toISOString()
405
- }];
400
+ function Ve(e, t, a, n, s) {
401
+ if (!e || !t)
402
+ throw new Error("Please connect to the agent first");
403
+ if (!e.interruptAvailable)
404
+ throw new Error("Interrupt is not enabled for this stream");
405
+ if (a !== W.Fluent)
406
+ throw new Error("Interrupt only available for Fluent streams");
407
+ if (!n && !s)
408
+ throw new Error("No active video to interrupt");
409
+ }
410
+ async function O(e, t) {
411
+ const a = {
412
+ type: F.StreamInterrupt,
413
+ videoId: t,
414
+ timestamp: Date.now()
415
+ };
416
+ e.sendDataChannelMessage(JSON.stringify(a));
406
417
  }
407
- function Fe(e) {
418
+ function Xe(e) {
408
419
  return new Promise((t, a) => {
409
420
  const {
410
421
  callbacks: n,
411
422
  host: s,
412
423
  auth: i
413
424
  } = e, {
414
- onMessage: r = null,
415
- onOpen: o = null,
416
- onClose: c = null,
417
- onError: u = null
418
- } = n || {}, d = new WebSocket(`${s}?authorization=${se(i)}`);
419
- d.onmessage = r, d.onclose = c, d.onerror = (g) => {
420
- console.error(g), u == null || u("Websocket failed to connect", g), a(g);
421
- }, d.onopen = (g) => {
422
- o == null || o(g), t(d);
425
+ onMessage: o = null,
426
+ onOpen: c = null,
427
+ onClose: r = null,
428
+ onError: l = null
429
+ } = n || {}, d = new WebSocket(`${s}?authorization=${ue(i)}`);
430
+ d.onmessage = o, d.onclose = r, d.onerror = (f) => {
431
+ console.error(f), l == null || l("Websocket failed to connect", f), a(f);
432
+ }, d.onopen = (f) => {
433
+ c == null || c(f), t(d);
423
434
  };
424
435
  });
425
436
  }
426
- async function Be(e) {
437
+ async function Ye(e) {
427
438
  const {
428
439
  retries: t = 1
429
440
  } = e;
430
441
  let a = null;
431
442
  for (let n = 0; (a == null ? void 0 : a.readyState) !== WebSocket.OPEN; n++)
432
443
  try {
433
- a = await Fe(e);
444
+ a = await Xe(e);
434
445
  } catch (s) {
435
446
  if (n === t)
436
447
  throw s;
437
- await ae(n * 500);
448
+ await de(n * 500);
438
449
  }
439
450
  return a;
440
451
  }
441
- async function We(e, t, a) {
442
- const n = a != null && a.onMessage ? [a.onMessage] : [], s = await Be({
452
+ async function Qe(e, t, a) {
453
+ const n = a != null && a.onMessage ? [a.onMessage] : [], s = await Ye({
443
454
  auth: e,
444
455
  host: t,
445
456
  callbacks: {
446
457
  onError: (i) => {
447
- var r;
448
- return (r = a.onError) == null ? void 0 : r.call(a, new he(i));
458
+ var o;
459
+ return (o = a.onError) == null ? void 0 : o.call(a, new Re(i));
449
460
  },
450
461
  onMessage(i) {
451
- const r = JSON.parse(i.data);
452
- n.forEach((o) => o(r.event, r));
462
+ const o = JSON.parse(i.data);
463
+ n.forEach((c) => c(o.event, o));
453
464
  }
454
465
  }
455
466
  });
@@ -459,7 +470,7 @@ async function We(e, t, a) {
459
470
  subscribeToEvents: (i) => n.push(i)
460
471
  };
461
472
  }
462
- function He(e) {
473
+ function Ze(e) {
463
474
  if (e.answer !== void 0)
464
475
  return e.answer;
465
476
  let t = 0, a = "";
@@ -467,54 +478,54 @@ function He(e) {
467
478
  a += e[t++];
468
479
  return a;
469
480
  }
470
- function Ke(e, t, a, n, s) {
481
+ function Ge(e, t, a, n, s) {
471
482
  const i = n.messages[n.messages.length - 1];
472
- if (!(e === B.Partial || e === B.Answer) || (i == null ? void 0 : i.role) !== "assistant")
483
+ if (!(e === H.Partial || e === H.Answer) || (i == null ? void 0 : i.role) !== "assistant")
473
484
  return;
474
485
  const {
475
- content: r,
476
- sequence: o
486
+ content: o,
487
+ sequence: c
477
488
  } = t;
478
- e === B.Partial ? a[o] = r : a.answer = r;
479
- const c = He(a);
480
- (i.content !== c || e === B.Answer) && (i.content = c, s == null || s([...n.messages], e));
489
+ e === H.Partial ? a[c] = o : a.answer = o;
490
+ const r = Ze(a);
491
+ (i.content !== r || e === H.Answer) && (i.content = r, s == null || s([...n.messages], e));
481
492
  }
482
- function Ue(e, t, a, n, s) {
493
+ function Oe(e, t, a, n, s) {
483
494
  let i = {};
484
495
  return {
485
496
  clearQueue: () => i = {},
486
- onMessage: (r, o) => {
487
- var c, u;
488
- if ("content" in o)
489
- Ke(r, o, i, t, a.callbacks.onNewMessage), r === B.Answer && e.track("agent-message-received", {
497
+ onMessage: (o, c) => {
498
+ var r, l;
499
+ if ("content" in c)
500
+ Ge(o, c, i, t, a.callbacks.onNewMessage), o === H.Answer && e.track("agent-message-received", {
490
501
  messages: t.messages.length,
491
502
  mode: t.chatMode
492
503
  });
493
504
  else {
494
- const d = U, g = [d.StreamVideoDone, d.StreamVideoError, d.StreamVideoRejected], R = [d.StreamFailed, d.StreamVideoError, d.StreamVideoRejected], h = ze(o, n, {
505
+ const d = F, f = [d.StreamVideoDone, d.StreamVideoError, d.StreamVideoRejected], B = [d.StreamFailed, d.StreamVideoError, d.StreamVideoRejected], M = Ue(c, n, {
495
506
  mode: t.chatMode
496
507
  });
497
- if (r = r, r === d.StreamVideoCreated)
498
- e.linkTrack("agent-video", h, d.StreamVideoCreated, ["start"]);
499
- else if (g.includes(r)) {
500
- const y = r.split("/")[1];
501
- R.includes(r) ? e.track("agent-video", {
502
- ...h,
503
- event: y
508
+ if (o = o, o === d.StreamVideoCreated)
509
+ e.linkTrack("agent-video", M, d.StreamVideoCreated, ["start"]);
510
+ else if (f.includes(o)) {
511
+ const _ = o.split("/")[1];
512
+ B.includes(o) ? e.track("agent-video", {
513
+ ...M,
514
+ event: _
504
515
  }) : e.linkTrack("agent-video", {
505
- ...h,
506
- event: y
507
- }, r, ["done"]);
516
+ ...M,
517
+ event: _
518
+ }, o, ["done"]);
508
519
  }
509
- R.includes(r) && ((u = (c = a.callbacks).onError) == null || u.call(c, new Error(`Stream failed with event ${r}`), {
510
- data: o
511
- })), o.event === d.StreamDone && s();
520
+ B.includes(o) && ((l = (r = a.callbacks).onError) == null || l.call(r, new Error(`Stream failed with event ${o}`), {
521
+ data: c
522
+ })), c.event === d.StreamDone && s();
512
523
  }
513
524
  }
514
525
  };
515
526
  }
516
- function qe(e, t, a, n) {
517
- const s = Q(e, `${t}/agents/${a}`, n);
527
+ function et(e, t, a, n) {
528
+ const s = ne(e, `${t}/agents/${a}`, n);
518
529
  return {
519
530
  createStream(i) {
520
531
  return s.post("/streams", {
@@ -522,38 +533,38 @@ function qe(e, t, a, n) {
522
533
  compatibility_mode: i.compatibility_mode,
523
534
  stream_warmup: i.stream_warmup,
524
535
  session_timeout: i.session_timeout,
525
- stream_greeting: i.stream_greeting
536
+ fluent: i.fluent
526
537
  });
527
538
  },
528
- startConnection(i, r, o) {
539
+ startConnection(i, o, c) {
529
540
  return s.post(`/streams/${i}/sdp`, {
530
- session_id: o,
531
- answer: r
541
+ session_id: c,
542
+ answer: o
532
543
  });
533
544
  },
534
- addIceCandidate(i, r, o) {
545
+ addIceCandidate(i, o, c) {
535
546
  return s.post(`/streams/${i}/ice`, {
536
- session_id: o,
537
- ...r
547
+ session_id: c,
548
+ ...o
538
549
  });
539
550
  },
540
- sendStreamRequest(i, r, o) {
551
+ sendStreamRequest(i, o, c) {
541
552
  return s.post(`/streams/${i}`, {
542
- session_id: r,
543
- ...o
553
+ session_id: o,
554
+ ...c
544
555
  });
545
556
  },
546
- close(i, r) {
557
+ close(i, o) {
547
558
  return s.delete(`/streams/${i}`, {
548
- session_id: r
559
+ session_id: o
549
560
  });
550
561
  }
551
562
  };
552
563
  }
553
- function Je(e, t, a, n) {
554
- const s = Q(e, `${t}/agents/${a}`, n);
564
+ function tt(e, t, a, n) {
565
+ const s = ne(e, `${t}/agents/${a}`, n);
555
566
  return {
556
- createStream(i, r) {
567
+ createStream(i, o) {
557
568
  return s.post("/streams", {
558
569
  driver_url: i.driver_url,
559
570
  face: i.face,
@@ -562,35 +573,35 @@ function Je(e, t, a, n) {
562
573
  compatibility_mode: i.compatibility_mode,
563
574
  stream_warmup: i.stream_warmup,
564
575
  session_timeout: i.session_timeout,
565
- stream_greeting: i.stream_greeting
566
- }, r);
576
+ fluent: i.fluent
577
+ }, o);
567
578
  },
568
- startConnection(i, r, o, c) {
579
+ startConnection(i, o, c, r) {
569
580
  return s.post(`/streams/${i}/sdp`, {
570
- session_id: o,
571
- answer: r
572
- }, c);
581
+ session_id: c,
582
+ answer: o
583
+ }, r);
573
584
  },
574
- addIceCandidate(i, r, o, c) {
585
+ addIceCandidate(i, o, c, r) {
575
586
  return s.post(`/streams/${i}/ice`, {
576
- session_id: o,
577
- ...r
578
- }, c);
587
+ session_id: c,
588
+ ...o
589
+ }, r);
579
590
  },
580
- sendStreamRequest(i, r, o, c) {
591
+ sendStreamRequest(i, o, c, r) {
581
592
  return s.post(`/streams/${i}`, {
582
- session_id: r,
583
- ...o
584
- }, c);
593
+ session_id: o,
594
+ ...c
595
+ }, r);
585
596
  },
586
- close(i, r, o) {
597
+ close(i, o, c) {
587
598
  return s.delete(`/streams/${i}`, {
588
- session_id: r
589
- }, o);
599
+ session_id: o
600
+ }, c);
590
601
  }
591
602
  };
592
603
  }
593
- function Ve(e, t, a) {
604
+ function rt(e, t, a) {
594
605
  const n = (t.timestamp - e.timestamp) / 1e3;
595
606
  return {
596
607
  duration: n,
@@ -601,6 +612,8 @@ function Ve(e, t, a) {
601
612
  framesDropped: t.framesDropped - e.framesDropped,
602
613
  framesDecoded: t.framesDecoded - e.framesDecoded,
603
614
  jitter: t.jitter,
615
+ avgJitterDelayInInterval: (t.jitterBufferDelay - e.jitterBufferDelay) / (t.jitterBufferEmittedCount - e.jitterBufferEmittedCount),
616
+ jitterBufferEmittedCount: t.jitterBufferEmittedCount - e.jitterBufferEmittedCount,
604
617
  jitterBufferDelay: (t.jitterBufferDelay - e.jitterBufferDelay) / n,
605
618
  framesPerSecond: t.framesPerSecond,
606
619
  freezeCount: t.freezeCount - e.freezeCount,
@@ -608,7 +621,7 @@ function Ve(e, t, a) {
608
621
  lowFpsCount: a
609
622
  };
610
623
  }
611
- function Xe(e) {
624
+ function nt(e) {
612
625
  return e.filter((t) => t.freezeCount > 0 || t.framesPerSecond < 21 || t.framesDropped > 0 || t.packetsLost > 0).map((t) => {
613
626
  const {
614
627
  timestamp: a,
@@ -620,32 +633,36 @@ function Xe(e) {
620
633
  };
621
634
  });
622
635
  }
623
- function Ye(e) {
624
- let t = "";
625
- for (const a of e.values())
626
- if (a && a.type === "codec" && a.mimeType.startsWith("video") && (t = a.mimeType.split("/")[1]), a && a.type === "inbound-rtp" && a.kind === "video")
636
+ function at(e) {
637
+ let t = "", a = 0;
638
+ for (const n of e.values())
639
+ if (n && n.type === "codec" && n.mimeType.startsWith("video") && (t = n.mimeType.split("/")[1]), n && n.type === "candidate-pair" && (a = n.currentRoundTripTime), n && n.type === "inbound-rtp" && n.kind === "video")
627
640
  return {
628
641
  codec: t,
629
- timestamp: a.timestamp,
630
- bytesReceived: a.bytesReceived,
631
- packetsReceived: a.packetsReceived,
632
- packetsLost: a.packetsLost,
633
- framesDropped: a.framesDropped,
634
- framesDecoded: a.framesDecoded,
635
- jitter: a.jitter,
636
- jitterBufferDelay: a.jitterBufferDelay,
637
- frameWidth: a.frameWidth,
638
- frameHeight: a.frameHeight,
639
- framesPerSecond: a.framesPerSecond,
640
- freezeCount: a.freezeCount,
641
- freezeDuration: a.totalFreezesDuration
642
+ rtt: a,
643
+ timestamp: n.timestamp,
644
+ bytesReceived: n.bytesReceived,
645
+ packetsReceived: n.packetsReceived,
646
+ packetsLost: n.packetsLost,
647
+ framesDropped: n.framesDropped,
648
+ framesDecoded: n.framesDecoded,
649
+ jitter: n.jitter,
650
+ jitterBufferDelay: n.jitterBufferDelay,
651
+ jitterBufferEmittedCount: n.jitterBufferEmittedCount,
652
+ avgJitterDelayInInterval: n.jitterBufferDelay / n.jitterBufferEmittedCount,
653
+ frameWidth: n.frameWidth,
654
+ frameHeight: n.frameHeight,
655
+ framesPerSecond: n.framesPerSecond,
656
+ freezeCount: n.freezeCount,
657
+ freezeDuration: n.totalFreezesDuration
642
658
  };
643
659
  return {};
644
660
  }
645
- function Qe(e, t, a) {
646
- const n = e.map((r, o) => o === 0 ? a ? {
661
+ function it(e, t, a) {
662
+ const n = e.map((r, l) => l === 0 ? a ? {
647
663
  timestamp: r.timestamp,
648
664
  duration: 0,
665
+ rtt: r.rtt,
649
666
  bytesReceived: r.bytesReceived - a.bytesReceived,
650
667
  bitrate: (r.bytesReceived - a.bytesReceived) * 8 / (t / 1e3),
651
668
  packetsReceived: r.packetsReceived - a.packetsReceived,
@@ -654,11 +671,14 @@ function Qe(e, t, a) {
654
671
  framesDecoded: r.framesDecoded - a.framesDecoded,
655
672
  jitter: r.jitter,
656
673
  jitterBufferDelay: r.jitterBufferDelay - a.jitterBufferDelay,
674
+ jitterBufferEmittedCount: r.jitterBufferEmittedCount - a.jitterBufferEmittedCount,
675
+ avgJitterDelayInInterval: (r.jitterBufferDelay - a.jitterBufferDelay) / (r.jitterBufferEmittedCount - a.jitterBufferEmittedCount),
657
676
  framesPerSecond: r.framesPerSecond,
658
677
  freezeCount: r.freezeCount - a.freezeCount,
659
678
  freezeDuration: r.freezeDuration - a.freezeDuration
660
679
  } : {
661
680
  timestamp: r.timestamp,
681
+ rtt: r.rtt,
662
682
  duration: 0,
663
683
  bytesReceived: r.bytesReceived,
664
684
  bitrate: r.bytesReceived * 8 / (t / 1e3),
@@ -668,524 +688,753 @@ function Qe(e, t, a) {
668
688
  framesDecoded: r.framesDecoded,
669
689
  jitter: r.jitter,
670
690
  jitterBufferDelay: r.jitterBufferDelay,
691
+ jitterBufferEmittedCount: r.jitterBufferEmittedCount,
692
+ avgJitterDelayInInterval: r.jitterBufferDelay / r.jitterBufferEmittedCount,
671
693
  framesPerSecond: r.framesPerSecond,
672
694
  freezeCount: r.freezeCount,
673
695
  freezeDuration: r.freezeDuration
674
696
  } : {
675
697
  timestamp: r.timestamp,
676
- duration: t * o / 1e3,
677
- bytesReceived: r.bytesReceived - e[o - 1].bytesReceived,
678
- bitrate: (r.bytesReceived - e[o - 1].bytesReceived) * 8 / (t / 1e3),
679
- packetsReceived: r.packetsReceived - e[o - 1].packetsReceived,
680
- packetsLost: r.packetsLost - e[o - 1].packetsLost,
681
- framesDropped: r.framesDropped - e[o - 1].framesDropped,
682
- framesDecoded: r.framesDecoded - e[o - 1].framesDecoded,
698
+ duration: t * l / 1e3,
699
+ rtt: r.rtt,
700
+ bytesReceived: r.bytesReceived - e[l - 1].bytesReceived,
701
+ bitrate: (r.bytesReceived - e[l - 1].bytesReceived) * 8 / (t / 1e3),
702
+ packetsReceived: r.packetsReceived - e[l - 1].packetsReceived,
703
+ packetsLost: r.packetsLost - e[l - 1].packetsLost,
704
+ framesDropped: r.framesDropped - e[l - 1].framesDropped,
705
+ framesDecoded: r.framesDecoded - e[l - 1].framesDecoded,
683
706
  jitter: r.jitter,
684
- jitterBufferDelay: r.jitterBufferDelay - e[o - 1].jitterBufferDelay,
707
+ jitterBufferDelay: r.jitterBufferDelay - e[l - 1].jitterBufferDelay,
708
+ jitterBufferEmittedCount: r.jitterBufferEmittedCount - e[l - 1].jitterBufferEmittedCount,
709
+ avgJitterDelayInInterval: (r.jitterBufferDelay - e[l - 1].jitterBufferDelay) / (r.jitterBufferEmittedCount - e[l - 1].jitterBufferEmittedCount),
685
710
  framesPerSecond: r.framesPerSecond,
686
- freezeCount: r.freezeCount - e[o - 1].freezeCount,
687
- freezeDuration: r.freezeDuration - e[o - 1].freezeDuration
688
- }), s = Xe(n), i = s.reduce((r, o) => r + (o.causes.includes("low fps") ? 1 : 0), 0);
711
+ freezeCount: r.freezeCount - e[l - 1].freezeCount,
712
+ freezeDuration: r.freezeDuration - e[l - 1].freezeDuration
713
+ }), s = nt(n), i = s.reduce((r, l) => r + (l.causes.includes("low fps") ? 1 : 0), 0), o = n.filter((r) => !!r.avgJitterDelayInInterval).map((r) => r.avgJitterDelayInInterval), c = n.filter((r) => !!r.rtt).map((r) => r.rtt);
689
714
  return {
690
715
  webRTCStats: {
691
716
  anomalies: s,
692
- aggregateReport: Ve(e[0], e[e.length - 1], i)
717
+ minRtt: Math.min(...c),
718
+ avgRtt: ie(c),
719
+ maxRtt: Math.max(...c),
720
+ aggregateReport: rt(e[0], e[e.length - 1], i),
721
+ minJitterDelayInInterval: Math.min(...o),
722
+ maxJitterDelayInInterval: Math.max(...o),
723
+ avgJitterDelayInInterval: ie(o)
693
724
  },
694
725
  codec: e[0].codec,
695
726
  resolution: `${e[0].frameWidth}x${e[0].frameHeight}`
696
727
  };
697
728
  }
698
- const Y = 100, Ze = Math.max(Math.ceil(400 / Y), 1);
699
- function Ge() {
700
- let e = 0;
701
- return (t) => {
702
- for (const a of t.values())
703
- if (a && a.type === "inbound-rtp" && a.kind === "video") {
704
- const n = a.framesDecoded, s = n - e > 0;
705
- return e = n, s;
729
+ const re = 100, st = Math.max(Math.ceil(400 / re), 1), ot = 0.25, ct = 0.28;
730
+ function dt() {
731
+ let e = 0, t, a, n = 0;
732
+ return (s) => {
733
+ for (const i of s.values())
734
+ if (i && i.type === "inbound-rtp" && i.kind === "video") {
735
+ const o = i.jitterBufferDelay, c = i.jitterBufferEmittedCount;
736
+ if (a && c > a) {
737
+ const d = o - t, f = c - a;
738
+ n = d / f;
739
+ }
740
+ t = o, a = c;
741
+ const r = i.framesDecoded, l = r - e > 0;
742
+ return e = r, {
743
+ isReceiving: l,
744
+ avgJitterDelayInInterval: n,
745
+ freezeCount: i.freezeCount
746
+ };
706
747
  }
707
- return !1;
748
+ return {
749
+ isReceiving: !1,
750
+ avgJitterDelayInInterval: n
751
+ };
708
752
  };
709
753
  }
710
- function Oe(e, t, a, n, s = !1, i = !1) {
711
- const r = s ? 1 : 0;
712
- let o = [], c, u = 0, d = !1, g = 0;
713
- const R = Ge();
754
+ function lt(e, t, a, n, s, i = !1) {
755
+ let o = [], c, r = 0, l = !1, d = Y.Unknown, f = Y.Unknown, B = 0, M = 0;
756
+ const _ = dt();
714
757
  return setInterval(async () => {
715
- const h = await e.getStats(), y = R(h), k = Ye(h);
716
- if (y)
717
- u = 0, d || (n == null || n(S.Start), i && g >= r && !t() && a(), c = o[o.length - 1], o = [], g++, d = !0), o.push(k);
718
- else if (d && (u++, u >= Ze)) {
719
- const C = Qe(o, Y, c);
720
- n == null || n(S.Stop, C), !i && !t() && a(), d = !1;
758
+ const k = await e.getStats(), {
759
+ isReceiving: D,
760
+ avgJitterDelayInInterval: N,
761
+ freezeCount: L
762
+ } = _(k), $ = at(k);
763
+ if (D)
764
+ r = 0, B = L - M, f = N < ot ? Y.Strong : N > ct && B > 1 ? Y.Weak : d, f !== d && (s == null || s(f), d = f, M += B, B = 0), l || (n == null || n(p.Start), c = o[o.length - 1], o = [], l = !0), o.push($);
765
+ else if (l && (r++, r >= st)) {
766
+ const u = it(o, re, c);
767
+ n == null || n(p.Stop, u), t() || a(), M = L, l = !1;
721
768
  }
722
- }, Y);
769
+ }, re);
723
770
  }
724
- let le = !1;
725
- const L = (e, t) => le && console.log(e, t), et = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
726
- function te(e) {
771
+ let pe = !1;
772
+ const z = (e, t) => pe && console.log(e, t), ut = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
773
+ function se(e) {
727
774
  switch (e) {
728
775
  case "connected":
729
- return D.Connected;
776
+ return j.Connected;
730
777
  case "checking":
731
- return D.Connecting;
778
+ return j.Connecting;
732
779
  case "failed":
733
- return D.Fail;
780
+ return j.Fail;
734
781
  case "new":
735
- return D.New;
782
+ return j.New;
736
783
  case "closed":
737
- return D.Closed;
784
+ return j.Closed;
738
785
  case "disconnected":
739
- return D.Disconnected;
786
+ return j.Disconnected;
740
787
  case "completed":
741
- return D.Completed;
788
+ return j.Completed;
742
789
  default:
743
- return D.New;
790
+ return j.New;
744
791
  }
745
792
  }
746
- function re(e, t, a, n) {
747
- e === S.Start && t === S.Start ? a == null || a(S.Start) : e === S.Stop && t === S.Stop && (a == null || a(S.Stop, n));
793
+ function ft(e) {
794
+ const [t, a = ""] = e.split(/:(.+)/);
795
+ try {
796
+ const n = JSON.parse(a);
797
+ return z("parsed data channel message", {
798
+ subject: t,
799
+ data: n
800
+ }), {
801
+ subject: t,
802
+ data: n
803
+ };
804
+ } catch (n) {
805
+ return z("Failed to parse data channel message, returning data as string", {
806
+ subject: t,
807
+ rawData: a,
808
+ error: n
809
+ }), {
810
+ subject: t,
811
+ data: a
812
+ };
813
+ }
814
+ }
815
+ function mt({
816
+ statsSignal: e,
817
+ dataChannelSignal: t,
818
+ onVideoStateChange: a,
819
+ report: n
820
+ }) {
821
+ e === p.Start && t === p.Start ? a == null || a(p.Start) : e === p.Stop && t === p.Stop && (a == null || a(p.Stop, n));
822
+ }
823
+ function gt({
824
+ statsSignal: e,
825
+ dataChannelSignal: t,
826
+ onVideoStateChange: a,
827
+ onAgentActivityStateChange: n,
828
+ report: s
829
+ }) {
830
+ e === p.Start ? a == null || a(p.Start) : e === p.Stop && (a == null || a(p.Stop, s)), t === p.Start ? n == null || n(V.Talking) : t === p.Stop && (n == null || n(V.Idle));
748
831
  }
749
- async function tt(e, t, {
832
+ function oe({
833
+ statsSignal: e,
834
+ dataChannelSignal: t,
835
+ onVideoStateChange: a,
836
+ onAgentActivityStateChange: n,
837
+ streamType: s,
838
+ report: i
839
+ }) {
840
+ s === W.Legacy ? mt({
841
+ statsSignal: e,
842
+ dataChannelSignal: t,
843
+ onVideoStateChange: a,
844
+ report: i
845
+ }) : s === W.Fluent && gt({
846
+ statsSignal: e,
847
+ dataChannelSignal: t,
848
+ onVideoStateChange: a,
849
+ onAgentActivityStateChange: n,
850
+ report: i
851
+ });
852
+ }
853
+ async function ht(e, t, {
750
854
  debug: a = !1,
751
855
  callbacks: n,
752
856
  auth: s,
753
- baseURL: i = K,
754
- warmup: r
857
+ baseURL: i = Q,
858
+ analytics: o
755
859
  }) {
756
- le = a;
757
- let o = !1, c = !1, u = S.Stop, d = S.Stop;
860
+ pe = a;
861
+ let c = !1, r = !1, l = p.Stop, d = p.Stop;
758
862
  const {
759
- startConnection: g,
760
- sendStreamRequest: R,
761
- close: h,
762
- createStream: y,
863
+ startConnection: f,
864
+ sendStreamRequest: B,
865
+ close: M,
866
+ createStream: _,
763
867
  addIceCandidate: k
764
- } = t.videoType === ne.Clip ? qe(s, i, e, n.onError) : Je(s, i, e, n.onError), {
765
- id: C,
766
- offer: A,
767
- ice_servers: P,
768
- session_id: l
769
- } = await y(t), m = new et({
770
- iceServers: P
771
- }), v = m.createDataChannel("JanusDataChannel");
772
- if (!l)
868
+ } = t.videoType === ce.Clip ? et(s, i, e, n.onError) : tt(s, i, e, n.onError), {
869
+ id: D,
870
+ offer: N,
871
+ ice_servers: L,
872
+ session_id: $,
873
+ fluent: u,
874
+ interrupt_enabled: w
875
+ } = await _(t), g = new ut({
876
+ iceServers: L
877
+ }), S = g.createDataChannel("JanusDataChannel");
878
+ if (!$)
773
879
  throw new Error("Could not create session_id");
774
- const b = () => o, T = () => {
775
- var f;
776
- o = !0, c && ((f = n.onConnectionStateChange) == null || f.call(n, D.Connected));
777
- }, _ = Oe(m, b, T, (f, w) => re(d = f, u, n.onVideoStateChange, w), r, !!t.stream_greeting);
778
- m.onicecandidate = (f) => {
779
- var w;
780
- L("peerConnection.onicecandidate", f);
880
+ const R = u ? W.Fluent : W.Legacy;
881
+ o.enrich({
882
+ "stream-type": R
883
+ });
884
+ const v = t.stream_warmup && !u, P = () => c, T = () => {
885
+ var m;
886
+ c = !0, r && ((m = n.onConnectionStateChange) == null || m.call(n, j.Connected));
887
+ }, b = lt(g, P, T, (m, h) => oe({
888
+ statsSignal: d = m,
889
+ dataChannelSignal: R === W.Legacy ? l : void 0,
890
+ onVideoStateChange: n.onVideoStateChange,
891
+ onAgentActivityStateChange: n.onAgentActivityStateChange,
892
+ report: h,
893
+ streamType: R
894
+ }), (m) => {
895
+ var h;
896
+ return (h = n.onConnectivityStateChange) == null ? void 0 : h.call(n, m);
897
+ }, v);
898
+ g.onicecandidate = (m) => {
899
+ var h;
900
+ z("peerConnection.onicecandidate", m);
781
901
  try {
782
- f.candidate && f.candidate.sdpMid && f.candidate.sdpMLineIndex !== null ? k(C, {
783
- candidate: f.candidate.candidate,
784
- sdpMid: f.candidate.sdpMid,
785
- sdpMLineIndex: f.candidate.sdpMLineIndex
786
- }, l) : k(C, {
902
+ m.candidate && m.candidate.sdpMid && m.candidate.sdpMLineIndex !== null ? k(D, {
903
+ candidate: m.candidate.candidate,
904
+ sdpMid: m.candidate.sdpMid,
905
+ sdpMLineIndex: m.candidate.sdpMLineIndex
906
+ }, $) : k(D, {
787
907
  candidate: null
788
- }, l);
789
- } catch (p) {
790
- (w = n.onError) == null || w.call(n, p, {
791
- streamId: C
908
+ }, $);
909
+ } catch (E) {
910
+ (h = n.onError) == null || h.call(n, E, {
911
+ streamId: D
792
912
  });
793
913
  }
794
- }, v.onopen = () => {
795
- c = !0, (!t.stream_warmup && !t.stream_greeting || o) && T();
796
- }, v.onmessage = (f) => {
797
- f.data === "stream/started" ? u = S.Start : f.data === "stream/done" && (u = S.Stop), re(d, u, n.onVideoStateChange);
798
- }, m.oniceconnectionstatechange = () => {
799
- var w;
800
- L("peerConnection.oniceconnectionstatechange => " + m.iceConnectionState);
801
- const f = te(m.iceConnectionState);
802
- f !== D.Connected && ((w = n.onConnectionStateChange) == null || w.call(n, f));
803
- }, m.ontrack = (f) => {
804
- var w;
805
- L("peerConnection.ontrack", f), (w = n.onSrcObjectReady) == null || w.call(n, f.streams[0]);
806
- }, await m.setRemoteDescription(A), L("set remote description OK");
807
- const $ = await m.createAnswer();
808
- return L("create answer OK"), await m.setLocalDescription($), L("set local description OK"), await g(C, $, l), L("start connection OK"), {
914
+ }, S.onopen = () => {
915
+ r = !0, (!v || c) && T();
916
+ };
917
+ function y(m) {
918
+ l = m === F.StreamStarted ? p.Start : p.Stop, oe({
919
+ statsSignal: R === W.Legacy ? d : void 0,
920
+ dataChannelSignal: l,
921
+ onVideoStateChange: n.onVideoStateChange,
922
+ onAgentActivityStateChange: n.onAgentActivityStateChange,
923
+ streamType: R
924
+ });
925
+ }
926
+ function I(m, h) {
927
+ const E = typeof h == "string" ? h : h == null ? void 0 : h.metadata;
928
+ E && o.enrich({
929
+ streamMetadata: E
930
+ }), o.track("agent-chat", {
931
+ event: "ready"
932
+ });
933
+ }
934
+ const C = {
935
+ [F.StreamStarted]: y,
936
+ [F.StreamDone]: y,
937
+ [F.StreamReady]: I
938
+ };
939
+ S.onmessage = (m) => {
940
+ var U;
941
+ const {
942
+ subject: h,
943
+ data: E
944
+ } = ft(m.data);
945
+ (U = C[h]) == null || U.call(C, h, E);
946
+ }, g.oniceconnectionstatechange = () => {
947
+ var h;
948
+ z("peerConnection.oniceconnectionstatechange => " + g.iceConnectionState);
949
+ const m = se(g.iceConnectionState);
950
+ m !== j.Connected && ((h = n.onConnectionStateChange) == null || h.call(n, m));
951
+ }, g.ontrack = (m) => {
952
+ var h;
953
+ z("peerConnection.ontrack", m), (h = n.onSrcObjectReady) == null || h.call(n, m.streams[0]);
954
+ }, await g.setRemoteDescription(N), z("set remote description OK");
955
+ const J = await g.createAnswer();
956
+ return z("create answer OK"), await g.setLocalDescription(J), z("set local description OK"), await f(D, J, $), z("start connection OK"), {
809
957
  /**
810
958
  * Method to send request to server to get clip or talk depend on you payload
811
959
  * @param payload
812
960
  */
813
- speak(f) {
814
- return R(C, l, f);
961
+ speak(m) {
962
+ return B(D, $, m);
815
963
  },
816
964
  /**
817
965
  * Method to close RTC connection
818
966
  */
819
967
  async disconnect() {
820
- var f, w;
821
- if (C) {
822
- const p = te(m.iceConnectionState);
823
- if (m) {
824
- if (p === D.New) {
825
- (f = n.onVideoStateChange) == null || f.call(n, S.Stop), clearInterval(_);
968
+ var m;
969
+ if (D) {
970
+ const h = se(g.iceConnectionState);
971
+ if (g) {
972
+ if (h === j.New) {
973
+ clearInterval(b);
826
974
  return;
827
975
  }
828
- m.close(), m.oniceconnectionstatechange = null, m.onnegotiationneeded = null, m.onicecandidate = null, m.ontrack = null;
976
+ g.close(), g.oniceconnectionstatechange = null, g.onnegotiationneeded = null, g.onicecandidate = null, g.ontrack = null;
829
977
  }
830
978
  try {
831
- p === D.Connected && await h(C, l).catch((M) => {
979
+ h === j.Connected && await M(D, $).catch((E) => {
832
980
  });
833
- } catch (M) {
834
- L("Error on close stream connection", M);
981
+ } catch (E) {
982
+ z("Error on close stream connection", E);
835
983
  }
836
- (w = n.onVideoStateChange) == null || w.call(n, S.Stop), clearInterval(_);
984
+ (m = n.onAgentActivityStateChange) == null || m.call(n, V.Idle), clearInterval(b);
985
+ }
986
+ },
987
+ /**
988
+ * Method to send data channel messages to the server
989
+ */
990
+ sendDataChannelMessage(m) {
991
+ var h, E;
992
+ if (!c || S.readyState !== "open") {
993
+ z("Data channel is not ready for sending messages"), (h = n.onError) == null || h.call(n, new Error("Data channel is not ready for sending messages"), {
994
+ streamId: D
995
+ });
996
+ return;
997
+ }
998
+ try {
999
+ S.send(m);
1000
+ } catch (U) {
1001
+ z("Error sending data channel message", U), (E = n.onError) == null || E.call(n, U, {
1002
+ streamId: D
1003
+ });
837
1004
  }
838
1005
  },
839
1006
  /**
840
1007
  * Session identifier information, should be returned in the body of all streaming requests
841
1008
  */
842
- sessionId: l,
1009
+ sessionId: $,
843
1010
  /**
844
1011
  * Id of current RTC stream
845
1012
  */
846
- streamId: C
1013
+ streamId: D,
1014
+ streamType: R,
1015
+ interruptAvailable: w
847
1016
  };
848
1017
  }
849
- function rt(e, t, a) {
850
- var s;
1018
+ function wt(e, t) {
851
1019
  const {
852
- streamOptions: n
1020
+ streamOptions: a
853
1021
  } = t ?? {};
854
1022
  return {
855
- videoType: De(e.presenter.type),
856
- output_resolution: n == null ? void 0 : n.outputResolution,
857
- session_timeout: n == null ? void 0 : n.sessionTimeout,
858
- stream_warmup: n == null ? void 0 : n.streamWarmup,
859
- compatibility_mode: n == null ? void 0 : n.compatibilityMode,
860
- stream_greeting: Z(e.presenter) !== "clip" && ((s = t == null ? void 0 : t.streamOptions) != null && s.streamGreeting) ? a : void 0
1023
+ videoType: be(e.presenter.type),
1024
+ output_resolution: a == null ? void 0 : a.outputResolution,
1025
+ session_timeout: a == null ? void 0 : a.sessionTimeout,
1026
+ stream_warmup: a == null ? void 0 : a.streamWarmup,
1027
+ compatibility_mode: a == null ? void 0 : a.compatibilityMode,
1028
+ fluent: a == null ? void 0 : a.fluent
861
1029
  };
862
1030
  }
863
- function nt(e, t, a, n) {
864
- N.get() > 0 && (e === S.Start ? n.linkTrack("agent-video", {
1031
+ function pt(e, t, a, n, s) {
1032
+ s === W.Fluent ? yt(e, t, a, n, s) : kt(e, t, a, n, s);
1033
+ }
1034
+ function yt(e, t, a, n, s) {
1035
+ e === p.Start ? n.track("stream-session", {
865
1036
  event: "start",
866
- latency: N.get(!0)
867
- }, "start", [U.StreamVideoCreated]) : e === S.Stop && n.linkTrack("agent-video", {
1037
+ "stream-type": s
1038
+ }) : e === p.Stop && n.track("stream-session", {
868
1039
  event: "stop",
869
1040
  is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
870
1041
  background: t.presenter.type === "clip" && t.presenter.background,
1042
+ "stream-type": s,
871
1043
  ...a
872
- }, "done", [U.StreamVideoDone]));
1044
+ });
873
1045
  }
874
- function at(e, t, a, n) {
875
- return N.reset(), new Promise(async (s, i) => {
876
- var r;
1046
+ function vt(e, t, a, n) {
1047
+ x.get() <= 0 || (e === p.Start ? a.linkTrack("agent-video", {
1048
+ event: "start",
1049
+ latency: x.get(!0),
1050
+ "stream-type": n
1051
+ }, "start", [F.StreamVideoCreated]) : e === p.Stop && a.linkTrack("agent-video", {
1052
+ event: "stop",
1053
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
1054
+ background: t.presenter.type === "clip" && t.presenter.background,
1055
+ "stream-type": n
1056
+ }, "done", [F.StreamVideoDone]));
1057
+ }
1058
+ function kt(e, t, a, n, s) {
1059
+ x.get() <= 0 || (e === p.Start ? n.linkTrack("agent-video", {
1060
+ event: "start",
1061
+ latency: x.get(!0),
1062
+ "stream-type": s
1063
+ }, "start", [F.StreamVideoCreated]) : e === p.Stop && n.linkTrack("agent-video", {
1064
+ event: "stop",
1065
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
1066
+ background: t.presenter.type === "clip" && t.presenter.background,
1067
+ "stream-type": s,
1068
+ ...a
1069
+ }, "done", [F.StreamVideoDone]));
1070
+ }
1071
+ function Dt(e, t, a) {
1072
+ return x.reset(), new Promise(async (n, s) => {
877
1073
  try {
878
- const o = await tt(e.id, rt(e, t, n), {
1074
+ const i = await ht(e.id, wt(e, t), {
879
1075
  ...t,
880
1076
  analytics: a,
881
- warmup: (r = t.streamOptions) == null ? void 0 : r.streamWarmup,
882
1077
  callbacks: {
883
1078
  ...t.callbacks,
884
- onConnectionStateChange: (c) => {
885
- var u, d;
886
- (d = (u = t.callbacks).onConnectionStateChange) == null || d.call(u, c), c === D.Connected && s(o);
1079
+ onConnectionStateChange: (o) => {
1080
+ var c, r;
1081
+ (r = (c = t.callbacks).onConnectionStateChange) == null || r.call(c, o), o === j.Connected && n(i);
1082
+ },
1083
+ onVideoStateChange: (o, c) => {
1084
+ var r, l;
1085
+ (l = (r = t.callbacks).onVideoStateChange) == null || l.call(r, o), pt(o, e, c, a, i.streamType);
887
1086
  },
888
- onVideoStateChange: (c, u) => {
889
- var d, g;
890
- (g = (d = t.callbacks).onVideoStateChange) == null || g.call(d, c), nt(c, e, u, a);
1087
+ onAgentActivityStateChange: (o) => {
1088
+ var c, r;
1089
+ (r = (c = t.callbacks).onAgentActivityStateChange) == null || r.call(c, o), o === V.Talking ? te.update() : te.reset(), vt(o === V.Talking ? p.Start : p.Stop, e, a, i.streamType);
891
1090
  }
892
1091
  }
893
1092
  });
894
- } catch (o) {
895
- i(o);
1093
+ } catch (i) {
1094
+ s(i);
896
1095
  }
897
1096
  });
898
1097
  }
899
- async function it(e, t, a, n, s, i) {
900
- var u, d, g, R;
1098
+ async function Rt(e, t, a, n, s) {
1099
+ var r, l, d, f;
901
1100
  const {
902
- chat: r,
1101
+ chat: i,
903
1102
  chatMode: o
904
- } = await de(e, a, n, t.mode, t.persistentChat, s);
905
- if (o && o !== t.mode && (t.mode = o, (d = (u = t.callbacks).onModeChange) == null || d.call(u, o), o === I.TextOnly))
906
- return (R = (g = t.callbacks).onError) == null || R.call(g, new ge(o)), {
907
- chat: r
1103
+ } = await we(e, a, n, t.mode, t.persistentChat, s);
1104
+ if (o && o !== t.mode && (t.mode = o, (l = (r = t.callbacks).onModeChange) == null || l.call(r, o), o === A.TextOnly))
1105
+ return (f = (d = t.callbacks).onError) == null || f.call(d, new De(o)), {
1106
+ chat: i
908
1107
  };
909
- const c = await at(e, t, n, i);
1108
+ const c = await Dt(e, t, n);
910
1109
  return {
911
- chat: r,
1110
+ chat: i,
912
1111
  streamingManager: c
913
1112
  };
914
1113
  }
915
- async function ot(e, t) {
916
- var C, A, P;
917
- let a = !0;
918
- const n = t.mixpanelKey || Ie, s = t.wsURL || be, i = t.baseURL || K, r = {
1114
+ async function _t(e, t) {
1115
+ var N, L, $;
1116
+ let a = !0, n = !1, s = !1;
1117
+ const i = t.mixpanelKey || Le, o = t.wsURL || $e, c = t.baseURL || Q, r = {
919
1118
  messages: [],
920
- chatMode: t.mode || I.Functional
921
- }, o = oe(t.auth, i, t.callbacks.onError), c = await o.getById(e), u = Ne(c), d = je({
922
- token: n,
923
- agent: c,
1119
+ chatMode: t.mode || A.Functional
1120
+ }, l = fe(t.auth, c, t.callbacks.onError), d = await l.getById(e), f = He({
1121
+ token: i,
1122
+ agent: d,
924
1123
  isEnabled: t.enableAnalitics,
925
1124
  distinctId: t.distinctId
926
1125
  }), {
927
- onMessage: g,
928
- clearQueue: R
929
- } = Ue(d, r, t, c, () => {
930
- var l;
931
- return (l = r.socketManager) == null ? void 0 : l.disconnect();
1126
+ onMessage: B,
1127
+ clearQueue: M
1128
+ } = Oe(f, r, t, d, () => {
1129
+ var u;
1130
+ return (u = r.socketManager) == null ? void 0 : u.disconnect();
932
1131
  });
933
- r.messages = ee(u, t.initialMessages), (A = (C = t.callbacks).onNewMessage) == null || A.call(C, [...r.messages], "answer"), d.track("agent-sdk", {
1132
+ r.messages = qe(t.initialMessages), (L = (N = t.callbacks).onNewMessage) == null || L.call(N, [...r.messages], "answer"), f.track("agent-sdk", {
934
1133
  event: "loaded",
935
- ...Ae(c)
1134
+ ...Je(d)
936
1135
  });
937
- async function h(l) {
938
- var $, f, w, p, M, E, j;
939
- (f = ($ = t.callbacks).onConnectionStateChange) == null || f.call($, D.Connecting), N.reset(), l && !a && (delete r.chat, r.messages = ee(u), (p = (w = t.callbacks).onNewMessage) == null || p.call(w, [...r.messages], "answer"));
940
- const m = t.mode === I.DirectPlayback ? Promise.resolve(void 0) : We(t.auth, s, {
941
- onMessage: g,
1136
+ async function _(u) {
1137
+ var P, T, b, y, I, C, J;
1138
+ (T = (P = t.callbacks).onConnectionStateChange) == null || T.call(P, j.Connecting), x.reset(), n = !1, s = !1, u && !a && (delete r.chat, (y = (b = t.callbacks).onNewMessage) == null || y.call(b, [...r.messages], "answer"));
1139
+ const w = t.mode === A.DirectPlayback ? Promise.resolve(void 0) : Qe(t.auth, o, {
1140
+ onMessage: B,
942
1141
  onError: t.callbacks.onError
943
- }), v = X(() => it(c, t, o, d, r.chat, l ? u : void 0), {
1142
+ }), g = ee(() => Rt(d, t, l, f, r.chat), {
944
1143
  limit: 3,
945
- timeout: Me,
1144
+ timeout: Ae,
946
1145
  timeoutErrorMessage: "Timeout initializing the stream",
947
1146
  // Retry on all errors except for connection errors and rate limit errors, these are already handled in client level.
948
- shouldRetryFn: (z) => (z == null ? void 0 : z.message) !== "Could not connect" && z.status !== 429,
1147
+ shouldRetryFn: (m) => (m == null ? void 0 : m.message) !== "Could not connect" && m.status !== 429,
949
1148
  delayMs: 1e3
950
- }).catch((z) => {
951
- var x, W;
952
- throw k(I.Maintenance), (W = (x = t.callbacks).onConnectionStateChange) == null || W.call(x, D.Fail), z;
953
- }), [b, {
954
- streamingManager: T,
955
- chat: _
956
- }] = await Promise.all([m, v]);
957
- _ && _.id !== ((M = r.chat) == null ? void 0 : M.id) && ((j = (E = t.callbacks).onNewChat) == null || j.call(E, _.id)), r.streamingManager = T, r.socketManager = b, r.chat = _, a = !1, k((_ == null ? void 0 : _.chat_mode) ?? t.mode ?? I.Functional);
1149
+ }).catch((m) => {
1150
+ var h, E;
1151
+ throw D(A.Maintenance), (E = (h = t.callbacks).onConnectionStateChange) == null || E.call(h, j.Fail), m;
1152
+ }), [S, {
1153
+ streamingManager: R,
1154
+ chat: v
1155
+ }] = await Promise.all([w, g]);
1156
+ v && v.id !== ((I = r.chat) == null ? void 0 : I.id) && ((J = (C = t.callbacks).onNewChat) == null || J.call(C, v.id)), r.streamingManager = R, r.socketManager = S, r.chat = v, a = !1, D((v == null ? void 0 : v.chat_mode) ?? t.mode ?? A.Functional);
958
1157
  }
959
- async function y() {
960
- var l, m, v, b;
961
- (l = r.socketManager) == null || l.disconnect(), await ((m = r.streamingManager) == null ? void 0 : m.disconnect()), delete r.streamingManager, delete r.socketManager, (b = (v = t.callbacks).onConnectionStateChange) == null || b.call(v, D.Disconnected);
1158
+ async function k() {
1159
+ var u, w, g, S;
1160
+ (u = r.socketManager) == null || u.disconnect(), await ((w = r.streamingManager) == null ? void 0 : w.disconnect()), n = !1, s = !1, delete r.streamingManager, delete r.socketManager, (S = (g = t.callbacks).onConnectionStateChange) == null || S.call(g, j.Disconnected);
962
1161
  }
963
- async function k(l) {
964
- var m, v;
965
- l !== r.chatMode && (d.track("agent-mode-change", {
966
- mode: l
967
- }), r.chatMode = l, r.chatMode !== I.Functional && await y(), (v = (m = t.callbacks).onModeChange) == null || v.call(m, l));
1162
+ async function D(u) {
1163
+ var w, g;
1164
+ u !== r.chatMode && (f.track("agent-mode-change", {
1165
+ mode: u
1166
+ }), r.chatMode = u, r.chatMode !== A.Functional && await k(), (g = (w = t.callbacks).onModeChange) == null || g.call(w, u));
968
1167
  }
969
1168
  return {
970
- agent: c,
971
- starterMessages: ((P = c.knowledge) == null ? void 0 : P.starter_message) || [],
972
- getSTTToken: () => o.getSTTToken(c.id),
973
- changeMode: k,
974
- enrichAnalytics: d.enrich,
1169
+ agent: d,
1170
+ getStreamType: () => {
1171
+ var u;
1172
+ return (u = r.streamingManager) == null ? void 0 : u.streamType;
1173
+ },
1174
+ getIsInterruptAvailable: () => {
1175
+ var u;
1176
+ return ((u = r.streamingManager) == null ? void 0 : u.interruptAvailable) ?? !1;
1177
+ },
1178
+ starterMessages: (($ = d.knowledge) == null ? void 0 : $.starter_message) || [],
1179
+ getSTTToken: () => l.getSTTToken(d.id),
1180
+ changeMode: D,
1181
+ enrichAnalytics: f.enrich,
975
1182
  async connect() {
976
- var l;
977
- await h(!0), d.track("agent-chat", {
1183
+ var u;
1184
+ await _(!0), f.track("agent-chat", {
978
1185
  event: "connect",
979
- chatId: (l = r.chat) == null ? void 0 : l.id,
980
- agentId: c.id,
1186
+ chatId: (u = r.chat) == null ? void 0 : u.id,
1187
+ agentId: d.id,
981
1188
  mode: r.chatMode
982
1189
  });
983
1190
  },
984
1191
  async reconnect() {
985
- var l;
986
- await y(), await h(!1), d.track("agent-chat", {
1192
+ var u;
1193
+ await k(), await _(!1), f.track("agent-chat", {
987
1194
  event: "reconnect",
988
- chatId: (l = r.chat) == null ? void 0 : l.id,
989
- agentId: c.id,
1195
+ chatId: (u = r.chat) == null ? void 0 : u.id,
1196
+ agentId: d.id,
990
1197
  mode: r.chatMode
991
1198
  });
992
1199
  },
993
1200
  async disconnect() {
994
- var l;
995
- await y(), d.track("agent-chat", {
1201
+ var u;
1202
+ await k(), f.track("agent-chat", {
996
1203
  event: "disconnect",
997
- chatId: (l = r.chat) == null ? void 0 : l.id,
998
- agentId: c.id,
1204
+ chatId: (u = r.chat) == null ? void 0 : u.id,
1205
+ agentId: d.id,
999
1206
  mode: r.chatMode
1000
1207
  });
1001
1208
  },
1002
- async chat(l) {
1003
- var T, _, $, f, w;
1004
- const m = () => {
1005
- if (t.mode === I.DirectPlayback)
1006
- throw new F("Direct playback is enabled, chat is disabled");
1007
- if (l.length >= 800)
1008
- throw new F("Message cannot be more than 800 characters");
1009
- if (l.length === 0)
1010
- throw new F("Message cannot be empty");
1011
- if (r.chatMode === I.Maintenance)
1012
- throw new F("Chat is in maintenance mode");
1013
- if (![I.TextOnly, I.Playground].includes(r.chatMode)) {
1209
+ async chat(u) {
1210
+ var R, v, P, T, b;
1211
+ const w = () => {
1212
+ if (t.mode === A.DirectPlayback)
1213
+ throw new K("Direct playback is enabled, chat is disabled");
1214
+ if (u.length >= 800)
1215
+ throw new K("Message cannot be more than 800 characters");
1216
+ if (u.length === 0)
1217
+ throw new K("Message cannot be empty");
1218
+ if (r.chatMode === A.Maintenance)
1219
+ throw new K("Chat is in maintenance mode");
1220
+ if (![A.TextOnly, A.Playground].includes(r.chatMode)) {
1014
1221
  if (!r.streamingManager)
1015
- throw new F("Streaming manager is not initialized");
1222
+ throw new K("Streaming manager is not initialized");
1016
1223
  if (!r.chat)
1017
- throw new F("Chat is not initialized");
1224
+ throw new K("Chat is not initialized");
1018
1225
  }
1019
- }, v = async () => {
1020
- var p, M;
1226
+ }, g = async () => {
1227
+ var y, I;
1021
1228
  if (!r.chat) {
1022
- const E = await de(c, o, d, r.chatMode, t.persistentChat);
1023
- if (!E.chat)
1024
- throw new fe(r.chatMode, !!t.persistentChat);
1025
- r.chat = E.chat, (M = (p = t.callbacks).onNewChat) == null || M.call(p, r.chat.id);
1229
+ const C = await we(d, l, f, r.chatMode, t.persistentChat);
1230
+ if (!C.chat)
1231
+ throw new ke(r.chatMode, !!t.persistentChat);
1232
+ r.chat = C.chat, (I = (y = t.callbacks).onNewChat) == null || I.call(y, r.chat.id);
1026
1233
  }
1027
1234
  return r.chat.id;
1028
- }, b = async (p, M) => X(() => {
1029
- var E, j;
1030
- return o.chat(c.id, M, {
1235
+ }, S = async (y, I) => ee(() => {
1236
+ var C, J;
1237
+ return l.chat(d.id, I, {
1031
1238
  chatMode: r.chatMode,
1032
- streamId: (E = r.streamingManager) == null ? void 0 : E.streamId,
1033
- sessionId: (j = r.streamingManager) == null ? void 0 : j.sessionId,
1034
- messages: p.map(({
1035
- matches: z,
1036
- ...x
1037
- }) => x)
1239
+ streamId: (C = r.streamingManager) == null ? void 0 : C.streamId,
1240
+ sessionId: (J = r.streamingManager) == null ? void 0 : J.sessionId,
1241
+ messages: y.map(({
1242
+ matches: m,
1243
+ ...h
1244
+ }) => h)
1038
1245
  }, {
1039
- ...ce(r.chatMode),
1246
+ ...he(r.chatMode),
1040
1247
  skipErrorHandler: !0
1041
1248
  });
1042
1249
  }, {
1043
1250
  limit: 2,
1044
- shouldRetryFn: (E) => {
1045
- var x, W, G, O;
1046
- const j = (x = E == null ? void 0 : E.message) == null ? void 0 : x.includes("missing or invalid session_id");
1047
- return !((W = E == null ? void 0 : E.message) == null ? void 0 : W.includes("Stream Error")) && !j ? ((O = (G = t.callbacks).onError) == null || O.call(G, E), !1) : !0;
1251
+ shouldRetryFn: (C) => {
1252
+ var h, E, U, ae;
1253
+ const J = (h = C == null ? void 0 : C.message) == null ? void 0 : h.includes("missing or invalid session_id");
1254
+ return !((E = C == null ? void 0 : C.message) == null ? void 0 : E.includes("Stream Error")) && !J ? ((ae = (U = t.callbacks).onError) == null || ae.call(U, C), !1) : !0;
1048
1255
  },
1049
1256
  onRetry: async () => {
1050
- await y(), await h(!1);
1257
+ await k(), await _(!1);
1051
1258
  }
1052
1259
  });
1053
1260
  try {
1054
- R(), m(), r.messages.push({
1261
+ M(), w(), r.messages.push({
1055
1262
  id: q(),
1056
1263
  role: "user",
1057
- content: l,
1058
- created_at: new Date(N.update()).toISOString()
1059
- }), (_ = (T = t.callbacks).onNewMessage) == null || _.call(T, [...r.messages], "user");
1060
- const p = await v(), M = await b([...r.messages], p);
1264
+ content: u,
1265
+ created_at: new Date(x.update()).toISOString()
1266
+ }), (v = (R = t.callbacks).onNewMessage) == null || v.call(R, [...r.messages], "user");
1267
+ const y = await g(), I = await S([...r.messages], y);
1061
1268
  return r.messages.push({
1062
1269
  id: q(),
1063
1270
  role: "assistant",
1064
- content: M.result || "",
1271
+ content: I.result || "",
1065
1272
  created_at: (/* @__PURE__ */ new Date()).toISOString(),
1066
- context: M.context,
1067
- matches: M.matches
1068
- }), d.track("agent-message-send", {
1273
+ context: I.context,
1274
+ matches: I.matches,
1275
+ videoId: I.videoId
1276
+ }), n && I.videoId && r.streamingManager && (n = !1, r.messages[r.messages.length - 1].interrupted = !0, await O(r.streamingManager, I.videoId)), f.track("agent-message-send", {
1069
1277
  event: "success",
1070
1278
  mode: r.chatMode,
1071
1279
  messages: r.messages.length + 1
1072
- }), M.result && ((f = ($ = t.callbacks).onNewMessage) == null || f.call($, [...r.messages], "answer"), d.track("agent-message-received", {
1073
- latency: N.get(!0),
1280
+ }), I.result && ((T = (P = t.callbacks).onNewMessage) == null || T.call(P, [...r.messages], "answer"), f.track("agent-message-received", {
1281
+ latency: x.get(!0),
1074
1282
  mode: r.chatMode,
1075
1283
  messages: r.messages.length
1076
- })), M;
1077
- } catch (p) {
1078
- throw ((w = r.messages[r.messages.length - 1]) == null ? void 0 : w.role) === "assistant" && r.messages.pop(), d.track("agent-message-send", {
1284
+ })), I;
1285
+ } catch (y) {
1286
+ throw n = !1, ((b = r.messages[r.messages.length - 1]) == null ? void 0 : b.role) === "assistant" && r.messages.pop(), f.track("agent-message-send", {
1079
1287
  event: "error",
1080
1288
  mode: r.chatMode,
1081
1289
  messages: r.messages.length
1082
- }), p;
1290
+ }), y;
1083
1291
  }
1084
1292
  },
1085
- rate(l, m, v) {
1086
- var _, $, f, w;
1087
- const b = r.messages.find((p) => p.id === l);
1293
+ rate(u, w, g) {
1294
+ var v, P, T, b;
1295
+ const S = r.messages.find((y) => y.id === u);
1088
1296
  if (r.chat) {
1089
- if (!b)
1297
+ if (!S)
1090
1298
  throw new Error("Message not found");
1091
1299
  } else
1092
1300
  throw new Error("Chat is not initialized");
1093
- const T = ((_ = b.matches) == null ? void 0 : _.map((p) => [p.document_id, p.id])) ?? [];
1094
- return d.track("agent-rate", {
1095
- event: v ? "update" : "create",
1096
- thumb: m === 1 ? "up" : "down",
1097
- knowledge_id: (($ = c.knowledge) == null ? void 0 : $.id) ?? "",
1301
+ const R = ((v = S.matches) == null ? void 0 : v.map((y) => [y.document_id, y.id])) ?? [];
1302
+ return f.track("agent-rate", {
1303
+ event: g ? "update" : "create",
1304
+ thumb: w === 1 ? "up" : "down",
1305
+ knowledge_id: ((P = d.knowledge) == null ? void 0 : P.id) ?? "",
1098
1306
  mode: r.chatMode,
1099
- matches: T,
1100
- score: m
1101
- }), v ? o.updateRating(c.id, r.chat.id, v, {
1102
- knowledge_id: ((f = c.knowledge) == null ? void 0 : f.id) ?? "",
1103
- message_id: l,
1104
- matches: T,
1105
- score: m
1106
- }) : o.createRating(c.id, r.chat.id, {
1107
- knowledge_id: ((w = c.knowledge) == null ? void 0 : w.id) ?? "",
1108
- message_id: l,
1109
- matches: T,
1110
- score: m
1307
+ matches: R,
1308
+ score: w
1309
+ }), g ? l.updateRating(d.id, r.chat.id, g, {
1310
+ knowledge_id: ((T = d.knowledge) == null ? void 0 : T.id) ?? "",
1311
+ message_id: u,
1312
+ matches: R,
1313
+ score: w
1314
+ }) : l.createRating(d.id, r.chat.id, {
1315
+ knowledge_id: ((b = d.knowledge) == null ? void 0 : b.id) ?? "",
1316
+ message_id: u,
1317
+ matches: R,
1318
+ score: w
1111
1319
  });
1112
1320
  },
1113
- deleteRate(l) {
1114
- var m;
1321
+ deleteRate(u) {
1322
+ var w;
1115
1323
  if (!r.chat)
1116
1324
  throw new Error("Chat is not initialized");
1117
- return d.track("agent-rate-delete", {
1325
+ return f.track("agent-rate-delete", {
1118
1326
  type: "text",
1119
- chat_id: (m = r.chat) == null ? void 0 : m.id,
1120
- id: l,
1327
+ chat_id: (w = r.chat) == null ? void 0 : w.id,
1328
+ id: u,
1121
1329
  mode: r.chatMode
1122
- }), o.deleteRating(c.id, r.chat.id, l);
1330
+ }), l.deleteRating(d.id, r.chat.id, u);
1123
1331
  },
1124
- speak(l) {
1125
- var b;
1126
- if (!r.streamingManager)
1127
- throw new Error("Please connect to the agent first");
1128
- function m() {
1129
- if (typeof l == "string") {
1130
- if (!c.presenter.voice)
1332
+ async speak(u) {
1333
+ var R, v, P, T, b;
1334
+ function w() {
1335
+ if (typeof u == "string") {
1336
+ if (!d.presenter.voice)
1131
1337
  throw new Error("Presenter voice is not initialized");
1132
1338
  return {
1133
1339
  type: "text",
1134
- provider: c.presenter.voice,
1135
- input: l,
1340
+ provider: d.presenter.voice,
1341
+ input: u,
1136
1342
  ssml: !1
1137
1343
  };
1138
1344
  }
1139
- if (l.type === "text" && !l.provider) {
1140
- if (!c.presenter.voice)
1345
+ if (u.type === "text" && !u.provider) {
1346
+ if (!d.presenter.voice)
1141
1347
  throw new Error("Presenter voice is not initialized");
1142
1348
  return {
1143
1349
  type: "text",
1144
- provider: c.presenter.voice,
1145
- input: l.input,
1146
- ssml: l.ssml
1350
+ provider: d.presenter.voice,
1351
+ input: u.input,
1352
+ ssml: u.ssml
1147
1353
  };
1148
1354
  }
1149
- return l;
1355
+ return u;
1150
1356
  }
1151
- const v = m();
1152
- return d.track("agent-speak", v), N.update(), r.streamingManager.speak({
1153
- script: v,
1154
- metadata: {
1155
- chat_id: (b = r.chat) == null ? void 0 : b.id,
1156
- agent_id: c.id
1157
- }
1158
- });
1357
+ const g = w();
1358
+ if (f.track("agent-speak", g), x.update(), r.messages && g.type === "text" && (r.messages.push({
1359
+ id: q(),
1360
+ role: "assistant",
1361
+ content: g.input,
1362
+ created_at: new Date(x.get(!0)).toISOString()
1363
+ }), (v = (R = t.callbacks).onNewMessage) == null || v.call(R, [...r.messages], "answer")), ze(r.chatMode))
1364
+ return {
1365
+ duration: 0,
1366
+ video_id: "",
1367
+ status: "success"
1368
+ };
1369
+ if (!r.streamingManager)
1370
+ throw new Error("Please connect to the agent first");
1371
+ s = !0;
1372
+ try {
1373
+ const y = await r.streamingManager.speak({
1374
+ script: g,
1375
+ metadata: {
1376
+ chat_id: (P = r.chat) == null ? void 0 : P.id,
1377
+ agent_id: d.id
1378
+ }
1379
+ });
1380
+ return s = !1, r.messages[r.messages.length - 1].videoId = y.video_id, n && y.video_id && r.streamingManager && (n = !1, r.messages[r.messages.length - 1].interrupted = !0, await O(r.streamingManager, y.video_id)), (b = (T = t.callbacks).onNewMessage) == null || b.call(T, [...r.messages], "answer"), y;
1381
+ } finally {
1382
+ s = !1;
1383
+ }
1384
+ },
1385
+ async interrupt({
1386
+ type: u
1387
+ }) {
1388
+ var R, v, P, T, b;
1389
+ const w = r.messages[r.messages.length - 1], g = (w == null ? void 0 : w.role) === "user", S = g || s;
1390
+ if (Ve(r.streamingManager, r.chat, (R = r.streamingManager) == null ? void 0 : R.streamType, S, !!(w != null && w.videoId)), f.track("agent-video-interrupt", {
1391
+ type: u || "click",
1392
+ stream_id: (v = r.streamingManager) == null ? void 0 : v.streamId,
1393
+ agent_id: d.id,
1394
+ owner_id: d.owner_id,
1395
+ video_duration_to_interrupt: te.get(!0),
1396
+ message_duration_to_interrupt: x.get(!0),
1397
+ chat_id: (P = r.chat) == null ? void 0 : P.id,
1398
+ mode: r.chatMode,
1399
+ queued_interrupt: g
1400
+ }), S) {
1401
+ n = !0;
1402
+ return;
1403
+ }
1404
+ w.interrupted = !0, (b = (T = t.callbacks).onNewMessage) == null || b.call(T, [...r.messages], "answer"), O(r.streamingManager, w.videoId);
1159
1405
  }
1160
1406
  };
1161
1407
  }
1162
- function ct(e, t, a) {
1408
+ function St(e, t, a) {
1163
1409
  const {
1164
1410
  getById: n
1165
- } = oe(t, a || K);
1411
+ } = fe(t, a || Q);
1166
1412
  return n(e);
1167
1413
  }
1168
1414
  export {
1169
- ye as AgentStatus,
1170
- fe as ChatCreationFailed,
1171
- I as ChatMode,
1172
- ge as ChatModeDowngraded,
1173
- B as ChatProgress,
1174
- D as ConnectionState,
1175
- Ce as DocumentType,
1176
- Re as KnowledgeType,
1177
- pe as PlanGroup,
1178
- Se as Providers,
1179
- ke as RateState,
1180
- U as StreamEvents,
1181
- S as StreamingState,
1182
- ve as Subject,
1183
- we as UserPlan,
1184
- F as ValidationError,
1185
- ne as VideoType,
1186
- _e as VoiceAccess,
1187
- he as WsError,
1188
- ot as createAgentManager,
1189
- ct as getAgent,
1190
- De as mapVideoType
1415
+ V as AgentActivityState,
1416
+ Se as AgentStatus,
1417
+ ke as ChatCreationFailed,
1418
+ A as ChatMode,
1419
+ De as ChatModeDowngraded,
1420
+ H as ChatProgress,
1421
+ j as ConnectionState,
1422
+ Y as ConnectivityState,
1423
+ Te as DocumentType,
1424
+ Me as KnowledgeType,
1425
+ _e as PlanGroup,
1426
+ je as Providers,
1427
+ Ie as RateState,
1428
+ F as StreamEvents,
1429
+ W as StreamType,
1430
+ p as StreamingState,
1431
+ Ee as Subject,
1432
+ Ce as UserPlan,
1433
+ K as ValidationError,
1434
+ ce as VideoType,
1435
+ Pe as VoiceAccess,
1436
+ Re as WsError,
1437
+ _t as createAgentManager,
1438
+ St as getAgent,
1439
+ be as mapVideoType
1191
1440
  };