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

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