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

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