@d-id/client-sdk 1.1.33 → 1.1.34-staging.156

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,16 +1,16 @@
1
- var Qe = Object.defineProperty;
2
- var Xe = (e, t, i) => t in e ? Qe(e, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : e[t] = i;
3
- var fe = (e, t, i) => Xe(e, typeof t != "symbol" ? t + "" : t, i);
4
- class we extends Error {
1
+ var Ze = Object.defineProperty;
2
+ var et = (e, t, i) => t in e ? Ze(e, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : e[t] = i;
3
+ var me = (e, t, i) => et(e, typeof t != "symbol" ? t + "" : t, i);
4
+ class Ce extends Error {
5
5
  constructor({ kind: i, description: n, error: s }) {
6
6
  super(JSON.stringify({ kind: i, description: n }));
7
- fe(this, "kind");
8
- fe(this, "description");
9
- fe(this, "error");
7
+ me(this, "kind");
8
+ me(this, "description");
9
+ me(this, "error");
10
10
  this.kind = i, this.description = n, this.error = s;
11
11
  }
12
12
  }
13
- class Ye extends we {
13
+ class tt extends Ce {
14
14
  constructor(t, i) {
15
15
  super({
16
16
  kind: "ChatCreationFailed",
@@ -18,25 +18,25 @@ class Ye extends we {
18
18
  });
19
19
  }
20
20
  }
21
- class Ge extends we {
21
+ class nt extends Ce {
22
22
  constructor(t) {
23
23
  super({ kind: "ChatModeDowngraded", description: `Chat mode downgraded to ${t}` });
24
24
  }
25
25
  }
26
- class ue extends we {
26
+ class ue extends Ce {
27
27
  constructor(i, n) {
28
28
  super({ kind: "ValidationError", description: i });
29
- fe(this, "key");
29
+ me(this, "key");
30
30
  this.key = n;
31
31
  }
32
32
  }
33
- class Ze extends we {
33
+ class rt extends Ce {
34
34
  constructor(t) {
35
35
  super({ kind: "WSError", description: t });
36
36
  }
37
37
  }
38
- var et = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(et || {}), tt = /* @__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))(tt || {}), nt = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(nt || {}), rt = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(rt || {}), F = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e.Off = "Off", e))(F || {}), X = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Transcribe = "transcribe", e.Complete = "done", e))(X || {}), it = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(it || {}), at = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(at || {}), ot = /* @__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))(ot || {}), Ke = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e.Expressive = "expressive", e))(Ke || {});
39
- const mn = (e) => {
38
+ var it = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(it || {}), at = /* @__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))(at || {}), ot = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(ot || {}), st = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(st || {}), B = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e.Off = "Off", e))(B || {}), Z = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Transcribe = "transcribe", e.Complete = "done", e))(Z || {}), ct = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(ct || {}), dt = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(dt || {}), ut = /* @__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))(ut || {}), Ve = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e.Expressive = "expressive", e))(Ve || {});
39
+ const pn = (e) => {
40
40
  switch (e) {
41
41
  case "clip":
42
42
  return "clip";
@@ -48,12 +48,13 @@ const mn = (e) => {
48
48
  throw new Error(`Unknown video type: ${e}`);
49
49
  }
50
50
  };
51
- var S = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(S || {}), ie = /* @__PURE__ */ ((e) => (e.Strong = "STRONG", e.Weak = "WEAK", e.Unknown = "UNKNOWN", e))(ie || {}), W = /* @__PURE__ */ ((e) => (e.Idle = "IDLE", e.Loading = "LOADING", e.Talking = "TALKING", e))(W || {}), j = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.ChatAudioTranscribed = "chat/audio-transcribed", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamInterrupt = "stream/interrupt", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(j || {}), R = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(R || {}), ae = /* @__PURE__ */ ((e) => (e.Legacy = "legacy", e.Fluent = "fluent", e))(ae || {}), ye = /* @__PURE__ */ ((e) => (e.Livekit = "livekit", e))(ye || {}), st = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.AzureOpenAi = "azure-openai", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(st || {}), ct = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(ct || {});
52
- const dt = 45 * 1e3, ut = "X-Playground-Chat", ge = "https://api.d-id.com", lt = "wss://notifications.d-id.com", ft = "79f81a83a67430be2bc0fd61042b8faa", Je = (e) => new Promise((t) => setTimeout(t, e)), le = (e = 16) => {
51
+ var D = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(D || {}), re = /* @__PURE__ */ ((e) => (e.Strong = "STRONG", e.Weak = "WEAK", e.Unknown = "UNKNOWN", e))(re || {}), H = /* @__PURE__ */ ((e) => (e.Idle = "IDLE", e.Loading = "LOADING", e.Talking = "TALKING", e))(H || {}), b = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.ChatAudioTranscribed = "chat/audio-transcribed", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamInterrupt = "stream/interrupt", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(b || {}), 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 || {}), ie = /* @__PURE__ */ ((e) => (e.Legacy = "legacy", e.Fluent = "fluent", e))(ie || {}), Se = /* @__PURE__ */ ((e) => (e.Livekit = "livekit", e))(Se || {}), lt = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.AzureOpenAi = "azure-openai", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(lt || {}), ft = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(ft || {});
52
+ const mt = 45 * 1e3, gt = "X-Playground-Chat", he = "https://api.d-id.com", ht = "wss://notifications.d-id.com", pt = "79f81a83a67430be2bc0fd61042b8faa", wt = (...e) => {
53
+ }, We = (e) => new Promise((t) => setTimeout(t, e)), le = (e = 16) => {
53
54
  const t = new Uint8Array(e);
54
55
  return window.crypto.getRandomValues(t), Array.from(t, (i) => i.toString(16).padStart(2, "0")).join("").slice(0, 13);
55
- }, Ue = (e) => e.type === "clip" && e.presenter_id.startsWith("v2_") ? "clip_v2" : e.type, Ie = (e) => e === Ke.Expressive, mt = (e) => [F.TextOnly, F.Playground, F.Maintenance].includes(e), Ve = (e) => e && [F.DirectPlayback, F.Off].includes(e);
56
- function gt(e, t) {
56
+ }, Oe = (e) => e.type === "clip" && e.presenter_id.startsWith("v2_") ? "clip_v2" : e.type, Pe = (e) => e === Ve.Expressive, yt = (e) => [B.TextOnly, B.Playground, B.Maintenance].includes(e), He = (e) => e && [B.DirectPlayback, B.Off].includes(e);
57
+ function vt(e, t) {
57
58
  let i;
58
59
  return {
59
60
  promise: new Promise((s, o) => {
@@ -62,7 +63,7 @@ function gt(e, t) {
62
63
  clear: () => clearTimeout(i)
63
64
  };
64
65
  }
65
- async function Se(e, t) {
66
+ async function Me(e, t) {
66
67
  const i = {
67
68
  limit: (t == null ? void 0 : t.limit) ?? 3,
68
69
  delayMs: (t == null ? void 0 : t.delayMs) ?? 0,
@@ -77,16 +78,16 @@ async function Se(e, t) {
77
78
  try {
78
79
  if (!i.timeout)
79
80
  return await e();
80
- const { promise: o, clear: c } = gt(i.timeout, i.timeoutErrorMessage), a = e().finally(c);
81
+ const { promise: o, clear: c } = vt(i.timeout, i.timeoutErrorMessage), a = e().finally(c);
81
82
  return await Promise.race([a, o]);
82
83
  } catch (o) {
83
84
  if (n = o, !i.shouldRetryFn(o) || s >= i.limit)
84
85
  throw o;
85
- await Je(i.delayMs), i.onRetry(o);
86
+ await We(i.delayMs), i.onRetry(o);
86
87
  }
87
88
  throw n;
88
89
  }
89
- function ke(e) {
90
+ function Ie(e) {
90
91
  if (e !== void 0)
91
92
  return window.localStorage.setItem("did_external_key_id", e), e;
92
93
  let t = window.localStorage.getItem("did_external_key_id");
@@ -96,38 +97,38 @@ function ke(e) {
96
97
  }
97
98
  return t;
98
99
  }
99
- let ht = le();
100
- function We(e, t) {
100
+ let Ct = le();
101
+ function qe(e, t) {
101
102
  if (e.type === "bearer")
102
103
  return `Bearer ${e.token}`;
103
104
  if (e.type === "basic")
104
105
  return `Basic ${btoa(`${e.username}:${e.password}`)}`;
105
106
  if (e.type === "key")
106
- return `Client-Key ${e.clientKey}.${ke(t)}_${ht}`;
107
+ return `Client-Key ${e.clientKey}.${Ie(t)}_${Ct}`;
107
108
  throw new Error(`Unknown auth type: ${e}`);
108
109
  }
109
- const pt = (e) => Se(e, {
110
+ const St = (e) => Me(e, {
110
111
  limit: 3,
111
112
  delayMs: 1e3,
112
113
  timeout: 0,
113
114
  shouldRetryFn: (t) => t.status === 429
114
115
  });
115
- function Te(e, t = ge, i, n) {
116
+ function Le(e, t = he, i, n) {
116
117
  const s = async (o, c) => {
117
- const { skipErrorHandler: a, ...r } = c || {}, d = await pt(
118
+ const { skipErrorHandler: a, ...r } = c || {}, d = await St(
118
119
  () => fetch(t + (o != null && o.startsWith("/") ? o : `/${o}`), {
119
120
  ...r,
120
121
  headers: {
121
122
  ...r.headers,
122
- Authorization: We(e, n),
123
+ Authorization: qe(e, n),
123
124
  "Content-Type": "application/json"
124
125
  }
125
126
  })
126
127
  );
127
128
  if (!d.ok) {
128
- let w = await d.text().catch(() => `Failed to fetch with status ${d.status}`);
129
- const p = new Error(w);
130
- throw i && !a && i(p, { url: o, options: r, headers: d.headers }), p;
129
+ let h = await d.text().catch(() => `Failed to fetch with status ${d.status}`);
130
+ const w = new Error(h);
131
+ throw i && !a && i(w, { url: o, options: r, headers: d.headers }), w;
131
132
  }
132
133
  return d.json();
133
134
  };
@@ -146,8 +147,8 @@ function Te(e, t = ge, i, n) {
146
147
  }
147
148
  };
148
149
  }
149
- function wt(e, t = ge, i, n) {
150
- const s = Te(e, `${t}/agents`, i, n);
150
+ function kt(e, t = he, i, n) {
151
+ const s = Le(e, `${t}/agents`, i, n);
151
152
  return {
152
153
  create(o, c) {
153
154
  return s.post("/", o, c);
@@ -184,7 +185,7 @@ function wt(e, t = ge, i, n) {
184
185
  }
185
186
  };
186
187
  }
187
- function yt(e) {
188
+ function Rt(e) {
188
189
  var s, o, c, a;
189
190
  const t = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop", i = () => {
190
191
  const r = navigator.platform;
@@ -195,18 +196,18 @@ function yt(e) {
195
196
  isMobile: `${t() == "Mobile"}`,
196
197
  browser: navigator.userAgent,
197
198
  origin: window.location.origin,
198
- agentType: Ue(n),
199
+ agentType: Oe(n),
199
200
  agentVoice: {
200
201
  voiceId: (o = (s = e.presenter) == null ? void 0 : s.voice) == null ? void 0 : o.voice_id,
201
202
  provider: (a = (c = e.presenter) == null ? void 0 : c.voice) == null ? void 0 : a.type
202
203
  }
203
204
  };
204
205
  }
205
- function vt(e) {
206
+ function Dt(e) {
206
207
  var i, n, s, o, c, a;
207
208
  const t = (i = e.llm) == null ? void 0 : i.prompt_customization;
208
209
  return {
209
- agentType: Ue(e.presenter),
210
+ agentType: Oe(e.presenter),
210
211
  owner_id: e.owner_id ?? "",
211
212
  promptVersion: (n = e.llm) == null ? void 0 : n.prompt_version,
212
213
  behavior: {
@@ -225,31 +226,31 @@ function vt(e) {
225
226
  ...e.access === "public" ? { from: "agent-template" } : {}
226
227
  };
227
228
  }
228
- const Ct = (e) => e.reduce((t, i) => t + i, 0), $e = (e) => Ct(e) / e.length;
229
- function St(e, t, i) {
230
- var r, d, w;
229
+ const Et = (e) => e.reduce((t, i) => t + i, 0), xe = (e) => Et(e) / e.length;
230
+ function Mt(e, t, i) {
231
+ var r, d, h;
231
232
  const { event: n, ...s } = e, { template: o } = (t == null ? void 0 : t.llm) || {}, { language: c } = ((r = t == null ? void 0 : t.presenter) == null ? void 0 : r.voice) || {};
232
233
  return {
233
234
  ...s,
234
235
  llm: { ...s.llm, template: o },
235
236
  script: { ...s.script, provider: { ...(d = s == null ? void 0 : s.script) == null ? void 0 : d.provider, language: c } },
236
- stitch: (t == null ? void 0 : t.presenter.type) === "talk" ? (w = t == null ? void 0 : t.presenter) == null ? void 0 : w.stitch : void 0,
237
+ stitch: (t == null ? void 0 : t.presenter.type) === "talk" ? (h = t == null ? void 0 : t.presenter) == null ? void 0 : h.stitch : void 0,
237
238
  ...i
238
239
  };
239
240
  }
240
- function Be(e) {
241
+ function Fe(e) {
241
242
  "requestIdleCallback" in window ? requestIdleCallback(e, { timeout: 2e3 }) : setTimeout(e, 0);
242
243
  }
243
- let ve = {};
244
- const kt = "https://api-js.mixpanel.com/track/?verbose=1&ip=1";
245
- function Rt(e) {
244
+ let De = {};
245
+ const It = "https://api-js.mixpanel.com/track/?verbose=1&ip=1";
246
+ function Tt(e) {
246
247
  const t = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk";
247
248
  return {
248
249
  token: e.token || "testKey",
249
- distinct_id: ke(e.externalId),
250
+ distinct_id: Ie(e.externalId),
250
251
  agentId: e.agentId,
251
252
  additionalProperties: {
252
- id: ke(e.externalId),
253
+ id: Ie(e.externalId),
253
254
  ...e.mixpanelAdditionalProperties || {}
254
255
  },
255
256
  isEnabled: e.isEnabled ?? !0,
@@ -286,18 +287,18 @@ function Rt(e) {
286
287
  ])
287
288
  })
288
289
  };
289
- return fetch(kt, {
290
+ return fetch(It, {
290
291
  ...r,
291
292
  keepalive: !0
292
293
  }).catch((d) => console.error("Analytics tracking error:", d)), Promise.resolve();
293
294
  },
294
295
  linkTrack(i, n, s, o) {
295
- ve[i] || (ve[i] = { events: {}, resolvedDependencies: [] }), o.includes(s) || o.push(s);
296
- const c = ve[i];
296
+ De[i] || (De[i] = { events: {}, resolvedDependencies: [] }), o.includes(s) || o.push(s);
297
+ const c = De[i];
297
298
  if (c.events[s] = { props: n }, c.resolvedDependencies.push(s), o.every(
298
299
  (r) => c.resolvedDependencies.includes(r)
299
300
  )) {
300
- const r = o.reduce((d, w) => c.events[w] ? { ...d, ...c.events[w].props } : d, {});
301
+ const r = o.reduce((d, h) => c.events[h] ? { ...d, ...c.events[h].props } : d, {});
301
302
  this.track(i, r), c.resolvedDependencies = c.resolvedDependencies.filter(
302
303
  (d) => !o.includes(d)
303
304
  ), o.forEach((d) => {
@@ -307,7 +308,7 @@ function Rt(e) {
307
308
  }
308
309
  };
309
310
  }
310
- function _e() {
311
+ function be() {
311
312
  let e = 0;
312
313
  return {
313
314
  reset: () => e = 0,
@@ -315,22 +316,22 @@ function _e() {
315
316
  get: (t = !1) => t ? Date.now() - e : e
316
317
  };
317
318
  }
318
- const ee = _e(), Re = _e(), xe = _e();
319
- function Oe(e) {
320
- return e === F.Playground ? { headers: { [ut]: "true" } } : {};
319
+ const ne = be(), Te = be(), ze = be();
320
+ function Qe(e) {
321
+ return e === B.Playground ? { headers: { [gt]: "true" } } : {};
321
322
  }
322
- async function He(e, t, i, n, s = !1, o) {
323
+ async function Xe(e, t, i, n, s = !1, o) {
323
324
  try {
324
- return !o && !Ve(n) && (o = await t.newChat(e.id, { persist: s }, Oe(n)), i.track("agent-chat", {
325
+ return !o && !He(n) && (o = await t.newChat(e.id, { persist: s }, Qe(n)), i.track("agent-chat", {
325
326
  event: "created",
326
327
  chatId: o.id,
327
328
  mode: n
328
329
  })), { chat: o, chatMode: (o == null ? void 0 : o.chat_mode) ?? n };
329
330
  } catch (c) {
330
- throw Dt(c) === "InsufficientCreditsError" ? new Error("InsufficientCreditsError") : new Error("Cannot create new chat");
331
+ throw _t(c) === "InsufficientCreditsError" ? new Error("InsufficientCreditsError") : new Error("Cannot create new chat");
331
332
  }
332
333
  }
333
- const Dt = (e) => {
334
+ const _t = (e) => {
334
335
  try {
335
336
  const t = JSON.parse(e.message);
336
337
  return t == null ? void 0 : t.kind;
@@ -338,19 +339,191 @@ const Dt = (e) => {
338
339
  return "UnknownError";
339
340
  }
340
341
  };
341
- function Mt(e) {
342
+ function At(e) {
342
343
  return e && e.length > 0 ? e : [];
343
344
  }
344
- function Et(e, t, i, n) {
345
- const s = Te(e, `${t}/v2/agents/${i}`, n);
345
+ function Pt(e, t, i, n) {
346
+ const s = Le(e, `${t}/v2/agents/${i}`, n);
346
347
  return {
347
348
  async createStream(o) {
348
349
  return s.post("/sessions", o);
349
350
  }
350
351
  };
351
352
  }
352
- const qe = (e, t) => (i, n) => e && console.log(`[${t}] ${i}`, n ?? "");
353
- async function Ce() {
353
+ const Ye = (e, t) => (i, n) => e && console.log(`[${t}] ${i}`, n ?? "");
354
+ function Lt(e, t, i) {
355
+ const n = (t.timestamp - e.timestamp) / 1e3;
356
+ return {
357
+ duration: n,
358
+ bytesReceived: t.bytesReceived - e.bytesReceived,
359
+ bitrate: Math.round((t.bytesReceived - e.bytesReceived) * 8 / n),
360
+ packetsReceived: t.packetsReceived - e.packetsReceived,
361
+ packetsLost: t.packetsLost - e.packetsLost,
362
+ framesDropped: t.framesDropped - e.framesDropped,
363
+ framesDecoded: t.framesDecoded - e.framesDecoded,
364
+ jitter: t.jitter,
365
+ avgJitterDelayInInterval: (t.jitterBufferDelay - e.jitterBufferDelay) / (t.jitterBufferEmittedCount - e.jitterBufferEmittedCount),
366
+ jitterBufferEmittedCount: t.jitterBufferEmittedCount - e.jitterBufferEmittedCount,
367
+ jitterBufferDelay: (t.jitterBufferDelay - e.jitterBufferDelay) / n,
368
+ framesPerSecond: t.framesPerSecond,
369
+ freezeCount: t.freezeCount - e.freezeCount,
370
+ freezeDuration: t.freezeDuration - e.freezeDuration,
371
+ lowFpsCount: i
372
+ };
373
+ }
374
+ function bt(e) {
375
+ return e.filter(
376
+ (t) => t.freezeCount > 0 || t.framesPerSecond < 21 || t.framesDropped > 0 || t.packetsLost > 0
377
+ ).map((t) => {
378
+ const { timestamp: i, ...n } = t, s = [];
379
+ 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"), {
380
+ ...n,
381
+ causes: s
382
+ };
383
+ });
384
+ }
385
+ function jt(e) {
386
+ let t = "", i = 0;
387
+ for (const n of e.values()) {
388
+ if (n && n.type === "codec" && n.mimeType.startsWith("video") && (t = n.mimeType.split("/")[1]), n && n.type === "candidate-pair") {
389
+ const s = n.currentRoundTripTime, c = n.nominated === !0;
390
+ s > 0 && (c || i === 0) && (i = s);
391
+ }
392
+ if (n && n.type === "inbound-rtp" && n.kind === "video")
393
+ return {
394
+ codec: t,
395
+ rtt: i,
396
+ timestamp: n.timestamp,
397
+ bytesReceived: n.bytesReceived,
398
+ packetsReceived: n.packetsReceived,
399
+ packetsLost: n.packetsLost,
400
+ framesDropped: n.framesDropped,
401
+ framesDecoded: n.framesDecoded,
402
+ jitter: n.jitter,
403
+ jitterBufferDelay: n.jitterBufferDelay,
404
+ jitterBufferEmittedCount: n.jitterBufferEmittedCount,
405
+ avgJitterDelayInInterval: n.jitterBufferDelay / n.jitterBufferEmittedCount,
406
+ frameWidth: n.frameWidth,
407
+ frameHeight: n.frameHeight,
408
+ framesPerSecond: n.framesPerSecond,
409
+ freezeCount: n.freezeCount,
410
+ freezeDuration: n.totalFreezesDuration
411
+ };
412
+ }
413
+ return {};
414
+ }
415
+ function Ke(e, t, i) {
416
+ const n = e.map((r, d) => d === 0 ? i ? {
417
+ timestamp: r.timestamp,
418
+ duration: 0,
419
+ rtt: r.rtt,
420
+ bytesReceived: r.bytesReceived - i.bytesReceived,
421
+ bitrate: (r.bytesReceived - i.bytesReceived) * 8 / (t / 1e3),
422
+ packetsReceived: r.packetsReceived - i.packetsReceived,
423
+ packetsLost: r.packetsLost - i.packetsLost,
424
+ framesDropped: r.framesDropped - i.framesDropped,
425
+ framesDecoded: r.framesDecoded - i.framesDecoded,
426
+ jitter: r.jitter,
427
+ jitterBufferDelay: r.jitterBufferDelay - i.jitterBufferDelay,
428
+ jitterBufferEmittedCount: r.jitterBufferEmittedCount - i.jitterBufferEmittedCount,
429
+ avgJitterDelayInInterval: (r.jitterBufferDelay - i.jitterBufferDelay) / (r.jitterBufferEmittedCount - i.jitterBufferEmittedCount),
430
+ framesPerSecond: r.framesPerSecond,
431
+ freezeCount: r.freezeCount - i.freezeCount,
432
+ freezeDuration: r.freezeDuration - i.freezeDuration
433
+ } : {
434
+ timestamp: r.timestamp,
435
+ rtt: r.rtt,
436
+ duration: 0,
437
+ bytesReceived: r.bytesReceived,
438
+ bitrate: r.bytesReceived * 8 / (t / 1e3),
439
+ packetsReceived: r.packetsReceived,
440
+ packetsLost: r.packetsLost,
441
+ framesDropped: r.framesDropped,
442
+ framesDecoded: r.framesDecoded,
443
+ jitter: r.jitter,
444
+ jitterBufferDelay: r.jitterBufferDelay,
445
+ jitterBufferEmittedCount: r.jitterBufferEmittedCount,
446
+ avgJitterDelayInInterval: r.jitterBufferDelay / r.jitterBufferEmittedCount,
447
+ framesPerSecond: r.framesPerSecond,
448
+ freezeCount: r.freezeCount,
449
+ freezeDuration: r.freezeDuration
450
+ } : {
451
+ timestamp: r.timestamp,
452
+ duration: t * d / 1e3,
453
+ rtt: r.rtt,
454
+ bytesReceived: r.bytesReceived - e[d - 1].bytesReceived,
455
+ bitrate: (r.bytesReceived - e[d - 1].bytesReceived) * 8 / (t / 1e3),
456
+ packetsReceived: r.packetsReceived - e[d - 1].packetsReceived,
457
+ packetsLost: r.packetsLost - e[d - 1].packetsLost,
458
+ framesDropped: r.framesDropped - e[d - 1].framesDropped,
459
+ framesDecoded: r.framesDecoded - e[d - 1].framesDecoded,
460
+ jitter: r.jitter,
461
+ jitterBufferDelay: r.jitterBufferDelay - e[d - 1].jitterBufferDelay,
462
+ jitterBufferEmittedCount: r.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount,
463
+ avgJitterDelayInInterval: (r.jitterBufferDelay - e[d - 1].jitterBufferDelay) / (r.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount),
464
+ framesPerSecond: r.framesPerSecond,
465
+ freezeCount: r.freezeCount - e[d - 1].freezeCount,
466
+ freezeDuration: r.freezeDuration - e[d - 1].freezeDuration
467
+ }), s = bt(n), o = s.reduce((r, d) => r + (d.causes.includes("low fps") ? 1 : 0), 0), c = n.filter((r) => !!r.avgJitterDelayInInterval).map((r) => r.avgJitterDelayInInterval), a = n.filter((r) => !!r.rtt).map((r) => r.rtt);
468
+ return {
469
+ webRTCStats: {
470
+ anomalies: s,
471
+ minRtt: Math.min(...a),
472
+ avgRtt: xe(a),
473
+ maxRtt: Math.max(...a),
474
+ aggregateReport: Lt(e[0], e[e.length - 1], o),
475
+ minJitterDelayInInterval: Math.min(...c),
476
+ maxJitterDelayInInterval: Math.max(...c),
477
+ avgJitterDelayInInterval: xe(c)
478
+ },
479
+ codec: e[0].codec,
480
+ resolution: `${e[0].frameWidth}x${e[0].frameHeight}`
481
+ };
482
+ }
483
+ const ve = 100, $t = Math.max(Math.ceil(400 / ve), 1), Bt = 0.25, xt = 0.28;
484
+ function Ft() {
485
+ let e = 0, t, i, n = 0;
486
+ return (s) => {
487
+ for (const o of s.values())
488
+ if (o && o.type === "inbound-rtp" && o.kind === "video") {
489
+ const c = o.jitterBufferDelay, a = o.jitterBufferEmittedCount;
490
+ if (i && a > i) {
491
+ const h = c - t, w = a - i;
492
+ n = h / w;
493
+ }
494
+ t = c, i = a;
495
+ const r = o.framesDecoded, d = r - e > 0;
496
+ return e = r, { isReceiving: d, avgJitterDelayInInterval: n, freezeCount: o.freezeCount };
497
+ }
498
+ return { isReceiving: !1, avgJitterDelayInInterval: n };
499
+ };
500
+ }
501
+ function Ge(e, t, i, n, s) {
502
+ let o = null, c = [], a, r = 0, d = !1, h = re.Unknown, w = re.Unknown, u = 0, C = 0;
503
+ const x = Ft();
504
+ async function j() {
505
+ const S = await e();
506
+ if (!S)
507
+ return;
508
+ const { isReceiving: R, avgJitterDelayInInterval: F, freezeCount: k } = x(S), q = jt(S);
509
+ if (R)
510
+ r = 0, u = k - C, w = F < Bt ? re.Strong : F > xt && u > 1 ? re.Weak : h, w !== h && (s == null || s(w), h = w, C += u, u = 0), d || (n == null || n(D.Start), a = c[c.length - 1], c = [], d = !0), c.push(q);
511
+ else if (d && (r++, r >= $t)) {
512
+ const N = Ke(c, ve, a);
513
+ n == null || n(D.Stop, N), t() || i(), C = k, d = !1;
514
+ }
515
+ }
516
+ return {
517
+ start: () => {
518
+ o || (o = setInterval(j, ve));
519
+ },
520
+ stop: () => {
521
+ o && (clearInterval(o), o = null);
522
+ },
523
+ getReport: () => Ke(c, ve, a)
524
+ };
525
+ }
526
+ async function Ee() {
354
527
  try {
355
528
  return await import("./livekit-client.esm-C1GRQdZY.js");
356
529
  } catch {
@@ -359,364 +532,375 @@ async function Ce() {
359
532
  );
360
533
  }
361
534
  }
362
- const It = {
363
- excellent: ie.Strong,
364
- good: ie.Strong,
365
- poor: ie.Weak,
366
- lost: ie.Unknown,
367
- unknown: ie.Unknown
368
- }, me = JSON.stringify({
535
+ const zt = {
536
+ excellent: re.Strong,
537
+ good: re.Strong,
538
+ poor: re.Weak,
539
+ lost: re.Unknown,
540
+ unknown: re.Unknown
541
+ }, ge = JSON.stringify({
369
542
  kind: "InternalServerError",
370
543
  description: "Stream Error"
371
544
  });
372
- var Ae = /* @__PURE__ */ ((e) => (e.Chat = "lk.chat", e.Speak = "did.speak", e.Interrupt = "did.interrupt", e))(Ae || {});
373
- function De(e, t, i) {
545
+ var je = /* @__PURE__ */ ((e) => (e.Chat = "lk.chat", e.Speak = "did.speak", e.Interrupt = "did.interrupt", e))(je || {});
546
+ function _e(e, t, i) {
374
547
  var n, s;
375
- throw t("Failed to connect to LiveKit room:", e), (n = i.onConnectionStateChange) == null || n.call(i, R.Fail), (s = i.onError) == null || s.call(i, e, { sessionId: "" }), e;
548
+ throw t("Failed to connect to LiveKit room:", e), (n = i.onConnectionStateChange) == null || n.call(i, M.Fail), (s = i.onError) == null || s.call(i, e, { sessionId: "" }), e;
376
549
  }
377
- async function Tt(e, t, i) {
378
- var Pe;
379
- const n = qe(i.debug || !1, "LiveKitStreamingManager"), { Room: s, RoomEvent: o, ConnectionState: c } = await Ce(), { callbacks: a, auth: r, baseURL: d, analytics: w, microphoneStream: p } = i;
380
- let u = null, _ = !1;
381
- const $ = ae.Fluent;
382
- let A = null, C = null, B = !1;
550
+ async function Kt(e, t, i) {
551
+ var $e;
552
+ const n = Ye(i.debug || !1, "LiveKitStreamingManager"), { Room: s, RoomEvent: o, ConnectionState: c } = await Ee(), { callbacks: a, auth: r, baseURL: d, analytics: h, microphoneStream: w } = i;
553
+ let u = null, C = !1;
554
+ const x = ie.Fluent;
555
+ let j = null, S = null, R = null, F = !1;
383
556
  u = new s({
384
557
  adaptiveStream: !1,
385
558
  // Must be false to use mediaStreamTrack directly
386
559
  dynacast: !0
387
560
  });
388
- let z = null;
389
- const I = 2e4;
390
- let U = W.Idle;
391
- const te = Et(r, d || ge, e, a.onError);
392
- let M, Y, Z;
561
+ let k = null;
562
+ const q = 2e4;
563
+ let N = H.Idle;
564
+ const Q = Pt(r, d || he, e, a.onError);
565
+ let z, te, f;
393
566
  try {
394
- const l = await te.createStream({
395
- transport_provider: ye.Livekit,
567
+ const l = await Q.createStream({
568
+ transport_provider: Se.Livekit,
396
569
  chat_persist: t.chat_persist ?? !0
397
- }), { id: g, session_token: v, session_url: D } = l;
398
- (Pe = a.onStreamCreated) == null || Pe.call(a, { session_id: g, stream_id: g, agent_id: e }), M = g, Y = v, Z = D, await u.prepareConnection(Z, Y);
570
+ }), { id: g, session_token: v, session_url: I } = l;
571
+ ($e = a.onStreamCreated) == null || $e.call(a, { session_id: g, stream_id: g, agent_id: e }), z = g, te = v, f = I, await u.prepareConnection(f, te);
399
572
  } catch (l) {
400
- De(l, n, a);
573
+ _e(l, n, a);
401
574
  }
402
- if (!Z || !Y || !M)
575
+ if (!f || !te || !z)
403
576
  return Promise.reject(new Error("Failed to initialize LiveKit stream"));
404
- u.on(o.ConnectionStateChanged, y).on(o.ConnectionQualityChanged, k).on(o.ActiveSpeakersChanged, T).on(o.ParticipantConnected, P).on(o.ParticipantDisconnected, E).on(o.TrackSubscribed, J).on(o.TrackUnsubscribed, O).on(o.DataReceived, H).on(o.MediaDevicesError, L).on(o.TranscriptionReceived, f).on(o.EncryptionError, N).on(o.TrackSubscriptionFailed, q);
405
- function f(l, g) {
577
+ u.on(o.ConnectionStateChanged, E).on(o.ConnectionQualityChanged, A).on(o.ActiveSpeakersChanged, P).on(o.ParticipantConnected, T).on(o.ParticipantDisconnected, J).on(o.TrackSubscribed, O).on(o.TrackUnsubscribed, X).on(o.DataReceived, L).on(o.MediaDevicesError, K).on(o.TranscriptionReceived, y).on(o.EncryptionError, Y).on(o.TrackSubscriptionFailed, oe);
578
+ function y(l, g) {
406
579
  var v;
407
- g != null && g.isLocal && (ee.update(), U === W.Talking && ((v = a.onInterruptDetected) == null || v.call(a, { type: "audio" }), U = W.Idle));
580
+ g != null && g.isLocal && (ne.update(), N === H.Talking && ((v = a.onInterruptDetected) == null || v.call(a, { type: "audio" }), N = H.Idle));
408
581
  }
409
582
  try {
410
- await u.connect(Z, Y), n("LiveKit room joined successfully"), z = setTimeout(() => {
583
+ await u.connect(f, te), n("LiveKit room joined successfully"), k = setTimeout(() => {
411
584
  var l;
412
- n("Track subscription timeout - no track subscribed within 30 seconds after connect"), z = null, w.track("connectivity-error", {
585
+ n("Track subscription timeout - no track subscribed within 30 seconds after connect"), k = null, h.track("connectivity-error", {
413
586
  error: "Track subscription timeout",
414
- sessionId: M
415
- }), (l = a.onError) == null || l.call(a, new Error("Track subscription timeout"), { sessionId: M }), ce();
416
- }, I);
587
+ sessionId: z
588
+ }), (l = a.onError) == null || l.call(a, new Error("Track subscription timeout"), { sessionId: z }), ke();
589
+ }, q);
417
590
  } catch (l) {
418
- De(l, n, a);
591
+ _e(l, n, a);
419
592
  }
420
- w.enrich({
421
- "stream-type": $
593
+ h.enrich({
594
+ "stream-type": x
422
595
  });
423
- function y(l) {
424
- var g, v, D, b;
596
+ function E(l) {
597
+ var g, v, I, _;
425
598
  switch (n("Connection state changed:", l), l) {
426
599
  case c.Connecting:
427
- n("CALLBACK: onConnectionStateChange(Connecting)"), (g = a.onConnectionStateChange) == null || g.call(a, R.Connecting);
600
+ n("CALLBACK: onConnectionStateChange(Connecting)"), (g = a.onConnectionStateChange) == null || g.call(a, M.Connecting);
428
601
  break;
429
602
  case c.Connected:
430
- n("LiveKit room connected successfully"), _ = !0;
603
+ n("LiveKit room connected successfully"), C = !0;
431
604
  break;
432
605
  case c.Disconnected:
433
- n("LiveKit room disconnected"), _ = !1, B = !1, (v = a.onConnectionStateChange) == null || v.call(a, R.Disconnected);
606
+ n("LiveKit room disconnected"), C = !1, F = !1, (v = a.onConnectionStateChange) == null || v.call(a, M.Disconnected);
434
607
  break;
435
608
  case c.Reconnecting:
436
- n("LiveKit room reconnecting..."), (D = a.onConnectionStateChange) == null || D.call(a, R.Connecting);
609
+ n("LiveKit room reconnecting..."), (I = a.onConnectionStateChange) == null || I.call(a, M.Connecting);
437
610
  break;
438
611
  case c.SignalReconnecting:
439
- n("LiveKit room signal reconnecting..."), (b = a.onConnectionStateChange) == null || b.call(a, R.Connecting);
612
+ n("LiveKit room signal reconnecting..."), (_ = a.onConnectionStateChange) == null || _.call(a, M.Connecting);
440
613
  break;
441
614
  }
442
615
  }
443
- function k(l, g) {
616
+ function A(l, g) {
444
617
  var v;
445
- n("Connection quality:", l), g != null && g.isLocal && ((v = a.onConnectivityStateChange) == null || v.call(a, It[l]));
618
+ n("Connection quality:", l), g != null && g.isLocal && ((v = a.onConnectivityStateChange) == null || v.call(a, zt[l]));
446
619
  }
447
- function T(l) {
620
+ function P(l) {
448
621
  n("Active speakers changed:", l);
449
622
  }
450
- function P(l) {
623
+ function T(l) {
451
624
  n("Participant connected:", l.identity);
452
625
  }
453
- function E(l) {
454
- n("Participant disconnected:", l.identity), ce();
626
+ function J(l) {
627
+ n("Participant disconnected:", l.identity), ke();
455
628
  }
456
- function J(l, g, v) {
457
- var b, Q, G, se;
458
- n(`Track subscribed: ${l.kind} from ${v.identity}`), z && (clearTimeout(z), z = null, n("Track subscription timeout cleared"));
459
- const D = l.mediaStreamTrack;
460
- if (!D) {
629
+ function O(l, g, v) {
630
+ var _, G, ee, se;
631
+ n(`Track subscribed: ${l.kind} from ${v.identity}`), k && (clearTimeout(k), k = null, n("Track subscription timeout cleared"));
632
+ const I = l.mediaStreamTrack;
633
+ if (!I) {
461
634
  n(`No mediaStreamTrack available for ${l.kind}`);
462
635
  return;
463
636
  }
464
- A ? (A.addTrack(D), n(`Added ${l.kind} track to shared MediaStream`)) : (A = new MediaStream([D]), n(`Created shared MediaStream with ${l.kind} track`)), l.kind === "video" && ((b = a.onStreamReady) == null || b.call(a), n("CALLBACK: onSrcObjectReady"), (Q = a.onSrcObjectReady) == null || Q.call(a, A), B || (B = !0, n("CALLBACK: onConnectionStateChange(Connected)"), (G = a.onConnectionStateChange) == null || G.call(a, R.Connected)), n("CALLBACK: onVideoStateChange(Start)"), (se = a.onVideoStateChange) == null || se.call(a, S.Start));
637
+ j ? (j.addTrack(I), n(`Added ${l.kind} track to shared MediaStream`)) : (j = new MediaStream([I]), n(`Created shared MediaStream with ${l.kind} track`)), l.kind === "video" && ((_ = a.onStreamReady) == null || _.call(a), n("CALLBACK: onSrcObjectReady"), (G = a.onSrcObjectReady) == null || G.call(a, j), F || (F = !0, n("CALLBACK: onConnectionStateChange(Connected)"), (ee = a.onConnectionStateChange) == null || ee.call(a, M.Connected)), n("CALLBACK: onVideoStateChange(Start)"), (se = a.onVideoStateChange) == null || se.call(a, D.Start), R = Ge(
638
+ () => l.getRTCStatsReport(),
639
+ () => C,
640
+ wt,
641
+ (ye, Re) => {
642
+ n(`Video state change: ${ye}`);
643
+ }
644
+ ), R.start());
465
645
  }
466
- function O(l, g, v) {
467
- var D;
468
- n(`Track unsubscribed: ${l.kind} from ${v.identity}`), l.kind === "video" && ((D = a.onVideoStateChange) == null || D.call(a, S.Stop));
646
+ function X(l, g, v) {
647
+ var I;
648
+ if (n(`Track unsubscribed: ${l.kind} from ${v.identity}`), l.kind === "video") {
649
+ const _ = R == null ? void 0 : R.getReport();
650
+ R == null || R.stop(), R = null, (I = a.onVideoStateChange) == null || I.call(a, D.Stop, _);
651
+ }
469
652
  }
470
- function H(l, g, v, D) {
471
- var Q, G, se, Le, be;
472
- const b = new TextDecoder().decode(l);
473
- n("Data received:", b);
653
+ function L(l, g, v, I) {
654
+ var G, ee, se, ye, Re, Be;
655
+ const _ = new TextDecoder().decode(l);
656
+ n("Data received:", _);
474
657
  try {
475
- const ne = JSON.parse(b), de = D || ne.subject;
476
- if (de === j.ChatAnswer) {
477
- const re = X.Answer;
478
- (Q = a.onMessage) == null || Q.call(a, re, {
479
- event: re,
480
- ...ne
481
- });
482
- } else if (de === j.ChatPartial) {
483
- const re = X.Partial;
484
- (G = a.onMessage) == null || G.call(a, re, {
485
- event: re,
486
- ...ne
658
+ const W = JSON.parse(_), de = I || W.subject;
659
+ if (de === b.ChatAnswer) {
660
+ const ae = Z.Answer;
661
+ (G = a.onMessage) == null || G.call(a, ae, {
662
+ event: ae,
663
+ ...W
487
664
  });
488
- } else if ([j.StreamVideoCreated, j.StreamVideoDone].includes(de)) {
489
- U = de === j.StreamVideoCreated ? W.Talking : W.Idle, (se = a.onAgentActivityStateChange) == null || se.call(a, U);
490
- const re = (ne == null ? void 0 : ne.role) || (g == null ? void 0 : g.identity) || "datachannel";
491
- (Le = a.onMessage) == null || Le.call(a, de, {
492
- [re]: ne
665
+ } else if (de === b.ChatPartial) {
666
+ const ae = Z.Partial;
667
+ (ee = a.onMessage) == null || ee.call(a, ae, {
668
+ event: ae,
669
+ ...W
493
670
  });
494
- } else if (de === j.ChatAudioTranscribed) {
495
- const re = X.Transcribe;
496
- (be = a.onMessage) == null || be.call(a, re, {
497
- event: re,
498
- ...ne
671
+ } else if ([b.StreamVideoCreated, b.StreamVideoDone].includes(de)) {
672
+ N = de === b.StreamVideoCreated ? H.Talking : H.Idle, (se = a.onAgentActivityStateChange) == null || se.call(a, N);
673
+ const fe = { [(W == null ? void 0 : W.role) || (g == null ? void 0 : g.identity) || "datachannel"]: W };
674
+ i.debug && ((ye = W == null ? void 0 : W.metadata) != null && ye.sentiment) && (fe.sentiment = {
675
+ id: W.metadata.sentiment.id,
676
+ name: W.metadata.sentiment.sentiment
677
+ }), (Re = a.onMessage) == null || Re.call(a, de, fe);
678
+ } else if (de === b.ChatAudioTranscribed) {
679
+ const ae = Z.Transcribe;
680
+ (Be = a.onMessage) == null || Be.call(a, ae, {
681
+ event: ae,
682
+ ...W
499
683
  }), queueMicrotask(() => {
500
- var je;
501
- (je = a.onAgentActivityStateChange) == null || je.call(a, W.Loading);
684
+ var fe;
685
+ (fe = a.onAgentActivityStateChange) == null || fe.call(a, H.Loading);
502
686
  });
503
687
  }
504
- } catch (ne) {
505
- n("Failed to parse data channel message:", ne);
688
+ } catch (W) {
689
+ n("Failed to parse data channel message:", W);
506
690
  }
507
691
  }
508
- function L(l) {
692
+ function K(l) {
509
693
  var g;
510
- n("Media devices error:", l), (g = a.onError) == null || g.call(a, new Error(me), { sessionId: M });
694
+ n("Media devices error:", l), (g = a.onError) == null || g.call(a, new Error(ge), { sessionId: z });
511
695
  }
512
- function N(l) {
696
+ function Y(l) {
513
697
  var g;
514
- n("Encryption error:", l), (g = a.onError) == null || g.call(a, new Error(me), { sessionId: M });
698
+ n("Encryption error:", l), (g = a.onError) == null || g.call(a, new Error(ge), { sessionId: z });
515
699
  }
516
- function q(l, g, v) {
700
+ function oe(l, g, v) {
517
701
  n("Track subscription failed:", { trackSid: l, participant: g, reason: v });
518
702
  }
519
- async function oe(l) {
703
+ async function U(l) {
520
704
  if (!u) return null;
521
- const { Track: g } = await Ce(), v = u.localParticipant.audioTrackPublications;
705
+ const { Track: g } = await Ee(), v = u.localParticipant.audioTrackPublications;
522
706
  if (v) {
523
- for (const [D, b] of v)
524
- if (b.source === g.Source.Microphone && b.track) {
525
- const G = b.track.mediaStreamTrack;
526
- if (G === l || G && G.id === l.id)
527
- return b;
707
+ for (const [I, _] of v)
708
+ if (_.source === g.Source.Microphone && _.track) {
709
+ const ee = _.track.mediaStreamTrack;
710
+ if (ee === l || ee && ee.id === l.id)
711
+ return _;
528
712
  }
529
713
  }
530
714
  return null;
531
715
  }
532
- function V(l) {
533
- if (!C || !C.track)
716
+ function m(l) {
717
+ if (!S || !S.track)
534
718
  return !1;
535
- const g = C.track.mediaStreamTrack;
719
+ const g = S.track.mediaStreamTrack;
536
720
  return g !== l && (g == null ? void 0 : g.id) !== l.id;
537
721
  }
538
- async function m(l) {
539
- var Q, G;
540
- if (!_ || !u)
722
+ async function p(l) {
723
+ var G, ee;
724
+ if (!C || !u)
541
725
  throw n("Room is not connected, cannot publish microphone stream"), new Error("Room is not connected");
542
726
  const g = l.getAudioTracks();
543
727
  if (g.length === 0) {
544
728
  n("No audio track found in the provided MediaStream");
545
729
  return;
546
730
  }
547
- const v = g[0], { Track: D } = await Ce(), b = await oe(v);
548
- if (b) {
731
+ const v = g[0], { Track: I } = await Ee(), _ = await U(v);
732
+ if (_) {
549
733
  n("Microphone track is already published, skipping", {
550
734
  trackId: v.id,
551
- publishedTrackId: (G = (Q = b.track) == null ? void 0 : Q.mediaStreamTrack) == null ? void 0 : G.id
552
- }), C = b;
735
+ publishedTrackId: (ee = (G = _.track) == null ? void 0 : G.mediaStreamTrack) == null ? void 0 : ee.id
736
+ }), S = _;
553
737
  return;
554
738
  }
555
- V(v) && (n("Unpublishing existing microphone track before publishing new one"), await h()), n("Publishing microphone track from provided MediaStream", { trackId: v.id });
739
+ m(v) && (n("Unpublishing existing microphone track before publishing new one"), await $()), n("Publishing microphone track from provided MediaStream", { trackId: v.id });
556
740
  try {
557
- C = await u.localParticipant.publishTrack(v, {
558
- source: D.Source.Microphone
559
- }), n("Microphone track published successfully", { trackSid: C.trackSid });
741
+ S = await u.localParticipant.publishTrack(v, {
742
+ source: I.Source.Microphone
743
+ }), n("Microphone track published successfully", { trackSid: S.trackSid });
560
744
  } catch (se) {
561
745
  throw n("Failed to publish microphone track:", se), se;
562
746
  }
563
747
  }
564
- async function h() {
565
- if (!(!C || !C.track))
748
+ async function $() {
749
+ if (!(!S || !S.track))
566
750
  try {
567
- u && (await u.localParticipant.unpublishTrack(C.track), n("Microphone track unpublished"));
751
+ u && (await u.localParticipant.unpublishTrack(S.track), n("Microphone track unpublished"));
568
752
  } catch (l) {
569
753
  n("Error unpublishing microphone track:", l);
570
754
  } finally {
571
- C = null;
755
+ S = null;
572
756
  }
573
757
  }
574
- function x() {
575
- A && (A.getTracks().forEach((l) => l.stop()), A = null);
758
+ function V() {
759
+ j && (j.getTracks().forEach((l) => l.stop()), j = null);
576
760
  }
577
- async function K(l, g) {
578
- var v, D;
579
- if (!_ || !u) {
580
- n("Room is not connected for sending messages"), (v = a.onError) == null || v.call(a, new Error(me), {
581
- sessionId: M
761
+ async function ce(l, g) {
762
+ var v, I;
763
+ if (!C || !u) {
764
+ n("Room is not connected for sending messages"), (v = a.onError) == null || v.call(a, new Error(ge), {
765
+ sessionId: z
582
766
  });
583
767
  return;
584
768
  }
585
769
  try {
586
770
  await u.localParticipant.sendText(l, { topic: g }), n("Message sent successfully:", l);
587
- } catch (b) {
588
- n("Failed to send message:", b), (D = a.onError) == null || D.call(a, new Error(me), { sessionId: M });
771
+ } catch (_) {
772
+ n("Failed to send message:", _), (I = a.onError) == null || I.call(a, new Error(ge), { sessionId: z });
589
773
  }
590
774
  }
591
- async function he(l) {
775
+ async function pe(l) {
592
776
  var g;
593
777
  try {
594
- const D = JSON.parse(l).topic;
595
- return K("", D);
778
+ const I = JSON.parse(l).topic;
779
+ return ce("", I);
596
780
  } catch (v) {
597
- n("Failed to send data channel message:", v), (g = a.onError) == null || g.call(a, new Error(me), { sessionId: M });
781
+ n("Failed to send data channel message:", v), (g = a.onError) == null || g.call(a, new Error(ge), { sessionId: z });
598
782
  }
599
783
  }
600
- function pe(l) {
601
- return K(
784
+ function we(l) {
785
+ return ce(
602
786
  l,
603
787
  "lk.chat"
604
788
  /* Chat */
605
789
  );
606
790
  }
607
- async function ce() {
791
+ async function ke() {
608
792
  var l, g;
609
- z && (clearTimeout(z), z = null), u && (await h(), await u.disconnect()), x(), _ = !1, B = !1, (l = a.onConnectionStateChange) == null || l.call(a, R.Disconnected), (g = a.onAgentActivityStateChange) == null || g.call(a, W.Idle), U = W.Idle;
793
+ k && (clearTimeout(k), k = null), u && (await $(), await u.disconnect()), V(), C = !1, F = !1, (l = a.onConnectionStateChange) == null || l.call(a, M.Disconnected), (g = a.onAgentActivityStateChange) == null || g.call(a, H.Idle), N = H.Idle;
610
794
  }
611
795
  return {
612
796
  speak(l) {
613
797
  const g = typeof l == "string" ? l : JSON.stringify(l);
614
- return K(
798
+ return ce(
615
799
  g,
616
800
  "did.speak"
617
801
  /* Speak */
618
802
  );
619
803
  },
620
- disconnect: ce,
804
+ disconnect: ke,
621
805
  async reconnect() {
622
806
  var l, g;
623
807
  if ((u == null ? void 0 : u.state) === c.Connected) {
624
808
  n("Room is already connected");
625
809
  return;
626
810
  }
627
- if (!u || !Z || !Y)
811
+ if (!u || !f || !te)
628
812
  throw n("Cannot reconnect: missing room, URL or token"), new Error("Cannot reconnect: session not available");
629
- n("Reconnecting to LiveKit room, state:", u.state), B = !1, (l = a.onConnectionStateChange) == null || l.call(a, R.Connecting);
813
+ n("Reconnecting to LiveKit room, state:", u.state), F = !1, (l = a.onConnectionStateChange) == null || l.call(a, M.Connecting);
630
814
  try {
631
- if (await u.connect(Z, Y), n("Room reconnected"), _ = !0, u.remoteParticipants.size === 0) {
632
- if (n("Waiting for agent to join..."), !await new Promise((D) => {
633
- const b = setTimeout(() => {
634
- u == null || u.off(o.ParticipantConnected, Q), D(!1);
635
- }, 5e3), Q = () => {
636
- clearTimeout(b), u == null || u.off(o.ParticipantConnected, Q), D(!0);
815
+ if (await u.connect(f, te), n("Room reconnected"), C = !0, u.remoteParticipants.size === 0) {
816
+ if (n("Waiting for agent to join..."), !await new Promise((I) => {
817
+ const _ = setTimeout(() => {
818
+ u == null || u.off(o.ParticipantConnected, G), I(!1);
819
+ }, 5e3), G = () => {
820
+ clearTimeout(_), u == null || u.off(o.ParticipantConnected, G), I(!0);
637
821
  };
638
- u == null || u.on(o.ParticipantConnected, Q);
822
+ u == null || u.on(o.ParticipantConnected, G);
639
823
  }))
640
824
  throw n("Agent did not join within timeout"), await u.disconnect(), new Error("Agent did not rejoin the room");
641
825
  n("Agent joined");
642
826
  }
643
827
  } catch (v) {
644
- throw n("Failed to reconnect:", v), (g = a.onConnectionStateChange) == null || g.call(a, R.Fail), v;
828
+ throw n("Failed to reconnect:", v), (g = a.onConnectionStateChange) == null || g.call(a, M.Fail), v;
645
829
  }
646
830
  },
647
- sendDataChannelMessage: he,
648
- sendTextMessage: pe,
649
- publishMicrophoneStream: m,
650
- unpublishMicrophoneStream: h,
651
- sessionId: M,
652
- streamId: M,
653
- streamType: $,
831
+ sendDataChannelMessage: pe,
832
+ sendTextMessage: we,
833
+ publishMicrophoneStream: p,
834
+ unpublishMicrophoneStream: $,
835
+ sessionId: z,
836
+ streamId: z,
837
+ streamType: x,
654
838
  interruptAvailable: !0,
655
839
  triggersAvailable: !1
656
840
  };
657
841
  }
658
- const _t = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
842
+ const Nt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
659
843
  __proto__: null,
660
- DataChannelTopic: Ae,
661
- createLiveKitStreamingManager: Tt,
662
- handleInitError: De
844
+ DataChannelTopic: je,
845
+ createLiveKitStreamingManager: Kt,
846
+ handleInitError: _e
663
847
  }, Symbol.toStringTag, { value: "Module" }));
664
- function At(e, t, i) {
848
+ function Jt(e, t, i) {
665
849
  if (!e)
666
850
  throw new Error("Please connect to the agent first");
667
851
  if (!e.interruptAvailable)
668
852
  throw new Error("Interrupt is not enabled for this stream");
669
- if (t !== ae.Fluent)
853
+ if (t !== ie.Fluent)
670
854
  throw new Error("Interrupt only available for Fluent streams");
671
855
  if (!i)
672
856
  throw new Error("No active video to interrupt");
673
857
  }
674
- async function Pt(e, t) {
858
+ async function Ut(e, t) {
675
859
  const i = {
676
- type: j.StreamInterrupt,
860
+ type: b.StreamInterrupt,
677
861
  videoId: t,
678
862
  timestamp: Date.now()
679
863
  };
680
864
  e.sendDataChannelMessage(JSON.stringify(i));
681
865
  }
682
- async function Lt(e) {
866
+ async function Vt(e) {
683
867
  const t = {
684
- topic: Ae.Interrupt
868
+ topic: je.Interrupt
685
869
  };
686
870
  e.sendDataChannelMessage(JSON.stringify(t));
687
871
  }
688
- function bt(e) {
872
+ function Wt(e) {
689
873
  return new Promise((t, i) => {
690
- const { callbacks: n, host: s, auth: o, externalId: c } = e, { onMessage: a = null, onOpen: r = null, onClose: d = null, onError: w = null } = n || {}, p = new WebSocket(`${s}?authorization=${encodeURIComponent(We(o, c))}`);
691
- p.onmessage = a, p.onclose = d, p.onerror = (u) => {
692
- console.error(u), w == null || w("Websocket failed to connect", u), i(u);
693
- }, p.onopen = (u) => {
694
- r == null || r(u), t(p);
874
+ const { callbacks: n, host: s, auth: o, externalId: c } = e, { onMessage: a = null, onOpen: r = null, onClose: d = null, onError: h = null } = n || {}, w = new WebSocket(`${s}?authorization=${encodeURIComponent(qe(o, c))}`);
875
+ w.onmessage = a, w.onclose = d, w.onerror = (u) => {
876
+ console.error(u), h == null || h("Websocket failed to connect", u), i(u);
877
+ }, w.onopen = (u) => {
878
+ r == null || r(u), t(w);
695
879
  };
696
880
  });
697
881
  }
698
- async function jt(e) {
882
+ async function Ot(e) {
699
883
  const { retries: t = 1 } = e;
700
884
  let i = null;
701
885
  for (let n = 0; (i == null ? void 0 : i.readyState) !== WebSocket.OPEN; n++)
702
886
  try {
703
- i = await bt(e);
887
+ i = await Wt(e);
704
888
  } catch (s) {
705
889
  if (n === t)
706
890
  throw s;
707
- await Je(n * 500);
891
+ await We(n * 500);
708
892
  }
709
893
  return i;
710
894
  }
711
- async function $t(e, t, i, n) {
712
- const s = i != null && i.onMessage ? [i.onMessage] : [], o = await jt({
895
+ async function Ht(e, t, i, n) {
896
+ const s = i != null && i.onMessage ? [i.onMessage] : [], o = await Ot({
713
897
  auth: e,
714
898
  host: t,
715
899
  externalId: n,
716
900
  callbacks: {
717
901
  onError: (c) => {
718
902
  var a;
719
- return (a = i.onError) == null ? void 0 : a.call(i, new Ze(c));
903
+ return (a = i.onError) == null ? void 0 : a.call(i, new rt(c));
720
904
  },
721
905
  onMessage(c) {
722
906
  const a = JSON.parse(c.data);
@@ -730,7 +914,7 @@ async function $t(e, t, i, n) {
730
914
  subscribeToEvents: (c) => s.push(c)
731
915
  };
732
916
  }
733
- function Bt(e) {
917
+ function qt(e) {
734
918
  if (e.answer !== void 0)
735
919
  return e.answer;
736
920
  let t = 0, i = "";
@@ -738,7 +922,7 @@ function Bt(e) {
738
922
  i += e[t++];
739
923
  return i;
740
924
  }
741
- function xt(e, t, i) {
925
+ function Qt(e, t, i) {
742
926
  if (!e.content)
743
927
  return;
744
928
  const n = t.messages[t.messages.length - 1];
@@ -752,17 +936,17 @@ function xt(e, t, i) {
752
936
  };
753
937
  t.messages.push(s), i == null || i([...t.messages], "user");
754
938
  }
755
- function Ft(e, t, i, n, s) {
756
- if (e === X.Transcribe && t.content) {
757
- xt(t, n, s);
939
+ function Xt(e, t, i, n, s) {
940
+ if (e === Z.Transcribe && t.content) {
941
+ Qt(t, n, s);
758
942
  return;
759
943
  }
760
- if (!(e === X.Partial || e === X.Answer))
944
+ if (!(e === Z.Partial || e === Z.Answer))
761
945
  return;
762
946
  const o = n.messages[n.messages.length - 1];
763
947
  let c;
764
948
  if (o != null && o.transcribed && o.role === "user")
765
- e === X.Answer && t.content, c = {
949
+ e === Z.Answer && t.content, c = {
766
950
  id: t.id || `assistant-${Date.now()}`,
767
951
  role: t.role || "assistant",
768
952
  content: t.content || "",
@@ -773,41 +957,48 @@ function Ft(e, t, i, n, s) {
773
957
  else
774
958
  return;
775
959
  const { content: a, sequence: r } = t;
776
- e === X.Partial ? i[r] = a : i.answer = a;
777
- const d = Bt(i);
778
- (c.content !== d || e === X.Answer) && (c.content = d, s == null || s([...n.messages], e));
960
+ e === Z.Partial ? i[r] = a : i.answer = a;
961
+ const d = qt(i);
962
+ (c.content !== d || e === Z.Answer) && (c.content = d, s == null || s([...n.messages], e));
779
963
  }
780
- function zt(e, t, i, n, s) {
964
+ function Yt(e, t, i, n, s) {
781
965
  let o = {};
782
- const c = () => o = {}, a = (r, d) => {
783
- var w, p;
784
- d === "user" && c(), (p = (w = i.callbacks).onNewMessage) == null || p.call(w, r, d);
966
+ const c = () => o = {};
967
+ let a = "answer";
968
+ const r = (d, h) => {
969
+ var w, u;
970
+ h === "user" && c(), a = h, (u = (w = i.callbacks).onNewMessage) == null || u.call(w, d, h);
785
971
  };
786
972
  return {
787
973
  clearQueue: c,
788
- onMessage: (r, d) => {
789
- var w, p;
790
- if ("content" in d) {
791
- const u = r === j.ChatAnswer ? X.Answer : r === j.ChatAudioTranscribed ? X.Transcribe : r;
792
- Ft(u, d, o, t, a), u === X.Answer && e.track("agent-message-received", {
974
+ onMessage: (d, h) => {
975
+ var w, u;
976
+ if ("content" in h) {
977
+ const C = d === b.ChatAnswer ? Z.Answer : d === b.ChatAudioTranscribed ? Z.Transcribe : d;
978
+ Xt(C, h, o, t, r), C === Z.Answer && e.track("agent-message-received", {
793
979
  messages: t.messages.length,
794
980
  mode: t.chatMode
795
981
  });
796
982
  } else {
797
- const u = j, _ = [u.StreamVideoDone, u.StreamVideoError, u.StreamVideoRejected], $ = [u.StreamFailed, u.StreamVideoError, u.StreamVideoRejected], A = St(d, n, { mode: t.chatMode });
798
- if (r = r, r === u.StreamVideoCreated)
799
- e.linkTrack("agent-video", A, u.StreamVideoCreated, ["start"]);
800
- else if (_.includes(r)) {
801
- const C = r.split("/")[1];
802
- $.includes(r) ? e.track("agent-video", { ...A, event: C }) : e.linkTrack("agent-video", { ...A, event: C }, r, ["done"]);
983
+ const C = b, x = [C.StreamVideoDone, C.StreamVideoError, C.StreamVideoRejected], j = [C.StreamFailed, C.StreamVideoError, C.StreamVideoRejected], S = Mt(h, n, { mode: t.chatMode });
984
+ if (d = d, d === C.StreamVideoCreated && (e.linkTrack("agent-video", S, C.StreamVideoCreated, ["start"]), h.sentiment)) {
985
+ const R = t.messages[t.messages.length - 1];
986
+ if ((R == null ? void 0 : R.role) === "assistant") {
987
+ const F = { ...R, sentiment: h.sentiment };
988
+ t.messages[t.messages.length - 1] = F, r == null || r([...t.messages], a);
989
+ }
990
+ }
991
+ if (x.includes(d)) {
992
+ const R = d.split("/")[1];
993
+ j.includes(d) ? e.track("agent-video", { ...S, event: R }) : e.linkTrack("agent-video", { ...S, event: R }, d, ["done"]);
803
994
  }
804
- $.includes(r) && ((p = (w = i.callbacks).onError) == null || p.call(w, new Error(`Stream failed with event ${r}`), { data: d })), d.event === u.StreamDone && s();
995
+ j.includes(d) && ((u = (w = i.callbacks).onError) == null || u.call(w, new Error(`Stream failed with event ${d}`), { data: h })), h.event === C.StreamDone && s();
805
996
  }
806
997
  }
807
998
  };
808
999
  }
809
- function Nt(e, t, i, n) {
810
- const s = Te(e, `${t}/agents/${i}`, n);
1000
+ function Gt(e, t, i, n) {
1001
+ const s = Le(e, `${t}/agents/${i}`, n);
811
1002
  return {
812
1003
  createStream(o, c) {
813
1004
  return s.post("/streams", o, { signal: c });
@@ -843,188 +1034,28 @@ function Nt(e, t, i, n) {
843
1034
  }
844
1035
  };
845
1036
  }
846
- function Kt(e, t, i) {
847
- const n = (t.timestamp - e.timestamp) / 1e3;
848
- return {
849
- duration: n,
850
- bytesReceived: t.bytesReceived - e.bytesReceived,
851
- bitrate: Math.round((t.bytesReceived - e.bytesReceived) * 8 / n),
852
- packetsReceived: t.packetsReceived - e.packetsReceived,
853
- packetsLost: t.packetsLost - e.packetsLost,
854
- framesDropped: t.framesDropped - e.framesDropped,
855
- framesDecoded: t.framesDecoded - e.framesDecoded,
856
- jitter: t.jitter,
857
- avgJitterDelayInInterval: (t.jitterBufferDelay - e.jitterBufferDelay) / (t.jitterBufferEmittedCount - e.jitterBufferEmittedCount),
858
- jitterBufferEmittedCount: t.jitterBufferEmittedCount - e.jitterBufferEmittedCount,
859
- jitterBufferDelay: (t.jitterBufferDelay - e.jitterBufferDelay) / n,
860
- framesPerSecond: t.framesPerSecond,
861
- freezeCount: t.freezeCount - e.freezeCount,
862
- freezeDuration: t.freezeDuration - e.freezeDuration,
863
- lowFpsCount: i
864
- };
865
- }
866
- function Jt(e) {
867
- return e.filter(
868
- (t) => t.freezeCount > 0 || t.framesPerSecond < 21 || t.framesDropped > 0 || t.packetsLost > 0
869
- ).map((t) => {
870
- const { timestamp: i, ...n } = t, s = [];
871
- 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"), {
872
- ...n,
873
- causes: s
874
- };
875
- });
876
- }
877
- function Ut(e) {
878
- let t = "", i = 0;
879
- for (const n of e.values()) {
880
- if (n && n.type === "codec" && n.mimeType.startsWith("video") && (t = n.mimeType.split("/")[1]), n && n.type === "candidate-pair") {
881
- const s = n.currentRoundTripTime, c = n.nominated === !0;
882
- s > 0 && (c || i === 0) && (i = s);
883
- }
884
- if (n && n.type === "inbound-rtp" && n.kind === "video")
885
- return {
886
- codec: t,
887
- rtt: i,
888
- timestamp: n.timestamp,
889
- bytesReceived: n.bytesReceived,
890
- packetsReceived: n.packetsReceived,
891
- packetsLost: n.packetsLost,
892
- framesDropped: n.framesDropped,
893
- framesDecoded: n.framesDecoded,
894
- jitter: n.jitter,
895
- jitterBufferDelay: n.jitterBufferDelay,
896
- jitterBufferEmittedCount: n.jitterBufferEmittedCount,
897
- avgJitterDelayInInterval: n.jitterBufferDelay / n.jitterBufferEmittedCount,
898
- frameWidth: n.frameWidth,
899
- frameHeight: n.frameHeight,
900
- framesPerSecond: n.framesPerSecond,
901
- freezeCount: n.freezeCount,
902
- freezeDuration: n.totalFreezesDuration
903
- };
904
- }
905
- return {};
906
- }
907
- function Vt(e, t, i) {
908
- const n = e.map((r, d) => d === 0 ? i ? {
909
- timestamp: r.timestamp,
910
- duration: 0,
911
- rtt: r.rtt,
912
- bytesReceived: r.bytesReceived - i.bytesReceived,
913
- bitrate: (r.bytesReceived - i.bytesReceived) * 8 / (t / 1e3),
914
- packetsReceived: r.packetsReceived - i.packetsReceived,
915
- packetsLost: r.packetsLost - i.packetsLost,
916
- framesDropped: r.framesDropped - i.framesDropped,
917
- framesDecoded: r.framesDecoded - i.framesDecoded,
918
- jitter: r.jitter,
919
- jitterBufferDelay: r.jitterBufferDelay - i.jitterBufferDelay,
920
- jitterBufferEmittedCount: r.jitterBufferEmittedCount - i.jitterBufferEmittedCount,
921
- avgJitterDelayInInterval: (r.jitterBufferDelay - i.jitterBufferDelay) / (r.jitterBufferEmittedCount - i.jitterBufferEmittedCount),
922
- framesPerSecond: r.framesPerSecond,
923
- freezeCount: r.freezeCount - i.freezeCount,
924
- freezeDuration: r.freezeDuration - i.freezeDuration
925
- } : {
926
- timestamp: r.timestamp,
927
- rtt: r.rtt,
928
- duration: 0,
929
- bytesReceived: r.bytesReceived,
930
- bitrate: r.bytesReceived * 8 / (t / 1e3),
931
- packetsReceived: r.packetsReceived,
932
- packetsLost: r.packetsLost,
933
- framesDropped: r.framesDropped,
934
- framesDecoded: r.framesDecoded,
935
- jitter: r.jitter,
936
- jitterBufferDelay: r.jitterBufferDelay,
937
- jitterBufferEmittedCount: r.jitterBufferEmittedCount,
938
- avgJitterDelayInInterval: r.jitterBufferDelay / r.jitterBufferEmittedCount,
939
- framesPerSecond: r.framesPerSecond,
940
- freezeCount: r.freezeCount,
941
- freezeDuration: r.freezeDuration
942
- } : {
943
- timestamp: r.timestamp,
944
- duration: t * d / 1e3,
945
- rtt: r.rtt,
946
- bytesReceived: r.bytesReceived - e[d - 1].bytesReceived,
947
- bitrate: (r.bytesReceived - e[d - 1].bytesReceived) * 8 / (t / 1e3),
948
- packetsReceived: r.packetsReceived - e[d - 1].packetsReceived,
949
- packetsLost: r.packetsLost - e[d - 1].packetsLost,
950
- framesDropped: r.framesDropped - e[d - 1].framesDropped,
951
- framesDecoded: r.framesDecoded - e[d - 1].framesDecoded,
952
- jitter: r.jitter,
953
- jitterBufferDelay: r.jitterBufferDelay - e[d - 1].jitterBufferDelay,
954
- jitterBufferEmittedCount: r.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount,
955
- avgJitterDelayInInterval: (r.jitterBufferDelay - e[d - 1].jitterBufferDelay) / (r.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount),
956
- framesPerSecond: r.framesPerSecond,
957
- freezeCount: r.freezeCount - e[d - 1].freezeCount,
958
- freezeDuration: r.freezeDuration - e[d - 1].freezeDuration
959
- }), s = Jt(n), o = s.reduce((r, d) => r + (d.causes.includes("low fps") ? 1 : 0), 0), c = n.filter((r) => !!r.avgJitterDelayInInterval).map((r) => r.avgJitterDelayInInterval), a = n.filter((r) => !!r.rtt).map((r) => r.rtt);
960
- return {
961
- webRTCStats: {
962
- anomalies: s,
963
- minRtt: Math.min(...a),
964
- avgRtt: $e(a),
965
- maxRtt: Math.max(...a),
966
- aggregateReport: Kt(e[0], e[e.length - 1], o),
967
- minJitterDelayInInterval: Math.min(...c),
968
- maxJitterDelayInInterval: Math.max(...c),
969
- avgJitterDelayInInterval: $e(c)
970
- },
971
- codec: e[0].codec,
972
- resolution: `${e[0].frameWidth}x${e[0].frameHeight}`
973
- };
974
- }
975
- const Me = 100, Wt = Math.max(Math.ceil(400 / Me), 1), Ot = 0.25, Ht = 0.28;
976
- function qt() {
977
- let e = 0, t, i, n = 0;
978
- return (s) => {
979
- for (const o of s.values())
980
- if (o && o.type === "inbound-rtp" && o.kind === "video") {
981
- const c = o.jitterBufferDelay, a = o.jitterBufferEmittedCount;
982
- if (i && a > i) {
983
- const w = c - t, p = a - i;
984
- n = w / p;
985
- }
986
- t = c, i = a;
987
- const r = o.framesDecoded, d = r - e > 0;
988
- return e = r, { isReceiving: d, avgJitterDelayInInterval: n, freezeCount: o.freezeCount };
989
- }
990
- return { isReceiving: !1, avgJitterDelayInInterval: n };
991
- };
992
- }
993
- function Qt(e, t, i, n, s) {
994
- let o = [], c, a = 0, r = !1, d = ie.Unknown, w = ie.Unknown, p = 0, u = 0;
995
- const _ = qt();
996
- return setInterval(async () => {
997
- const $ = await e.getStats(), { isReceiving: A, avgJitterDelayInInterval: C, freezeCount: B } = _($), z = Ut($);
998
- if (A)
999
- a = 0, p = B - u, w = C < Ot ? ie.Strong : C > Ht && p > 1 ? ie.Weak : d, w !== d && (s == null || s(w), d = w, u += p, p = 0), r || (n == null || n(S.Start), c = o[o.length - 1], o = [], r = !0), o.push(z);
1000
- else if (r && (a++, a >= Wt)) {
1001
- const I = Vt(o, Me, c);
1002
- n == null || n(S.Stop, I), t() || i(), u = B, r = !1;
1003
- }
1004
- }, Me);
1005
- }
1006
- const Xt = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
1007
- function Fe(e) {
1037
+ const Zt = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
1038
+ function Ne(e) {
1008
1039
  switch (e) {
1009
1040
  case "connected":
1010
- return R.Connected;
1041
+ return M.Connected;
1011
1042
  case "checking":
1012
- return R.Connecting;
1043
+ return M.Connecting;
1013
1044
  case "failed":
1014
- return R.Fail;
1045
+ return M.Fail;
1015
1046
  case "new":
1016
- return R.New;
1047
+ return M.New;
1017
1048
  case "closed":
1018
- return R.Closed;
1049
+ return M.Closed;
1019
1050
  case "disconnected":
1020
- return R.Disconnected;
1051
+ return M.Disconnected;
1021
1052
  case "completed":
1022
- return R.Completed;
1053
+ return M.Completed;
1023
1054
  default:
1024
- return R.New;
1055
+ return M.New;
1025
1056
  }
1026
1057
  }
1027
- const Yt = (e) => (t) => {
1058
+ const en = (e) => (t) => {
1028
1059
  const [i, n = ""] = t.split(/:(.+)/);
1029
1060
  try {
1030
1061
  const s = JSON.parse(n);
@@ -1033,16 +1064,16 @@ const Yt = (e) => (t) => {
1033
1064
  return e("Failed to parse data channel message, returning data as string", { subject: i, rawData: n, error: s }), { subject: i, data: n };
1034
1065
  }
1035
1066
  };
1036
- function Gt({
1067
+ function tn({
1037
1068
  statsSignal: e,
1038
1069
  dataChannelSignal: t,
1039
1070
  onVideoStateChange: i,
1040
1071
  report: n,
1041
1072
  log: s
1042
1073
  }) {
1043
- e === S.Start && t === S.Start ? (s("CALLBACK: onVideoStateChange(Start)"), i == null || i(S.Start)) : e === S.Stop && t === S.Stop && (s("CALLBACK: onVideoStateChange(Stop)"), i == null || i(S.Stop, n));
1074
+ e === D.Start && t === D.Start ? (s("CALLBACK: onVideoStateChange(Start)"), i == null || i(D.Start)) : e === D.Stop && t === D.Stop && (s("CALLBACK: onVideoStateChange(Stop)"), i == null || i(D.Stop, n));
1044
1075
  }
1045
- function Zt({
1076
+ function nn({
1046
1077
  statsSignal: e,
1047
1078
  dataChannelSignal: t,
1048
1079
  onVideoStateChange: i,
@@ -1050,9 +1081,9 @@ function Zt({
1050
1081
  report: s,
1051
1082
  log: o
1052
1083
  }) {
1053
- e === S.Start ? (o("CALLBACK: onVideoStateChange(Start)"), i == null || i(S.Start)) : e === S.Stop && (o("CALLBACK: onVideoStateChange(Stop)"), i == null || i(S.Stop, s)), t === S.Start ? n == null || n(W.Talking) : t === S.Stop && (n == null || n(W.Idle));
1084
+ e === D.Start ? (o("CALLBACK: onVideoStateChange(Start)"), i == null || i(D.Start)) : e === D.Stop && (o("CALLBACK: onVideoStateChange(Stop)"), i == null || i(D.Stop, s)), t === D.Start ? n == null || n(H.Talking) : t === D.Stop && (n == null || n(H.Idle));
1054
1085
  }
1055
- function ze({
1086
+ function Je({
1056
1087
  statsSignal: e,
1057
1088
  dataChannelSignal: t,
1058
1089
  onVideoStateChange: i,
@@ -1061,7 +1092,7 @@ function ze({
1061
1092
  report: o,
1062
1093
  log: c
1063
1094
  }) {
1064
- s === ae.Legacy ? Gt({ statsSignal: e, dataChannelSignal: t, onVideoStateChange: i, report: o, log: c }) : s === ae.Fluent && Zt({
1095
+ s === ie.Legacy ? tn({ statsSignal: e, dataChannelSignal: t, onVideoStateChange: i, report: o, log: c }) : s === ie.Fluent && nn({
1065
1096
  statsSignal: e,
1066
1097
  dataChannelSignal: t,
1067
1098
  onVideoStateChange: i,
@@ -1070,189 +1101,189 @@ function ze({
1070
1101
  log: c
1071
1102
  });
1072
1103
  }
1073
- async function en(e, t, { debug: i = !1, callbacks: n, auth: s, baseURL: o = ge, analytics: c }, a) {
1074
- var V;
1075
- const r = qe(i, "WebRTCStreamingManager"), d = Yt(r);
1076
- let w = !1, p = !1, u = S.Stop, _ = S.Stop;
1077
- const { startConnection: $, sendStreamRequest: A, close: C, createStream: B, addIceCandidate: z } = Nt(
1104
+ async function rn(e, t, { debug: i = !1, callbacks: n, auth: s, baseURL: o = he, analytics: c }, a) {
1105
+ var U;
1106
+ const r = Ye(i, "WebRTCStreamingManager"), d = en(r);
1107
+ let h = !1, w = !1, u = D.Stop, C = D.Stop;
1108
+ const { startConnection: x, sendStreamRequest: j, close: S, createStream: R, addIceCandidate: F } = Gt(
1078
1109
  s,
1079
1110
  o,
1080
1111
  e,
1081
1112
  n.onError
1082
1113
  ), {
1083
- id: I,
1084
- offer: U,
1085
- ice_servers: te,
1086
- session_id: M,
1087
- fluent: Y,
1088
- interrupt_enabled: Z,
1114
+ id: k,
1115
+ offer: q,
1116
+ ice_servers: N,
1117
+ session_id: Q,
1118
+ fluent: z,
1119
+ interrupt_enabled: te,
1089
1120
  triggers_enabled: f
1090
- } = await B(t, a);
1091
- (V = n.onStreamCreated) == null || V.call(n, { stream_id: I, session_id: M, agent_id: e });
1092
- const y = new Xt({ iceServers: te }), k = y.createDataChannel("JanusDataChannel");
1093
- if (!M)
1121
+ } = await R(t, a);
1122
+ (U = n.onStreamCreated) == null || U.call(n, { stream_id: k, session_id: Q, agent_id: e });
1123
+ const y = new Zt({ iceServers: N }), E = y.createDataChannel("JanusDataChannel");
1124
+ if (!Q)
1094
1125
  throw new Error("Could not create session_id");
1095
- const T = Y ? ae.Fluent : ae.Legacy;
1126
+ const A = z ? ie.Fluent : ie.Legacy;
1096
1127
  c.enrich({
1097
- "stream-type": T
1128
+ "stream-type": A
1098
1129
  });
1099
- const P = t.stream_warmup && !Y, E = () => w, J = () => {
1130
+ const P = t.stream_warmup && !z, T = () => h, J = () => {
1100
1131
  var m;
1101
- w = !0, p && (r("CALLBACK: onConnectionStateChange(Connected)"), (m = n.onConnectionStateChange) == null || m.call(n, R.Connected));
1102
- }, O = Qt(
1103
- y,
1104
- E,
1132
+ h = !0, w && (r("CALLBACK: onConnectionStateChange(Connected)"), (m = n.onConnectionStateChange) == null || m.call(n, M.Connected));
1133
+ }, O = Ge(
1134
+ () => y.getStats(),
1135
+ T,
1105
1136
  J,
1106
- (m, h) => ze({
1107
- statsSignal: _ = m,
1108
- dataChannelSignal: T === ae.Legacy ? u : void 0,
1137
+ (m, p) => Je({
1138
+ statsSignal: C = m,
1139
+ dataChannelSignal: A === ie.Legacy ? u : void 0,
1109
1140
  onVideoStateChange: n.onVideoStateChange,
1110
1141
  onAgentActivityStateChange: n.onAgentActivityStateChange,
1111
- report: h,
1112
- streamType: T,
1142
+ report: p,
1143
+ streamType: A,
1113
1144
  log: r
1114
1145
  }),
1115
1146
  (m) => {
1116
- var h;
1117
- return (h = n.onConnectivityStateChange) == null ? void 0 : h.call(n, m);
1147
+ var p;
1148
+ return (p = n.onConnectivityStateChange) == null ? void 0 : p.call(n, m);
1118
1149
  }
1119
1150
  );
1120
- y.onicecandidate = (m) => {
1121
- var h;
1151
+ O.start(), y.onicecandidate = (m) => {
1152
+ var p;
1122
1153
  r("peerConnection.onicecandidate", m);
1123
1154
  try {
1124
- m.candidate && m.candidate.sdpMid && m.candidate.sdpMLineIndex !== null ? z(
1125
- I,
1155
+ m.candidate && m.candidate.sdpMid && m.candidate.sdpMLineIndex !== null ? F(
1156
+ k,
1126
1157
  {
1127
1158
  candidate: m.candidate.candidate,
1128
1159
  sdpMid: m.candidate.sdpMid,
1129
1160
  sdpMLineIndex: m.candidate.sdpMLineIndex
1130
1161
  },
1131
- M,
1162
+ Q,
1132
1163
  a
1133
- ) : z(I, { candidate: null }, M, a);
1134
- } catch (x) {
1135
- (h = n.onError) == null || h.call(n, x, { streamId: I });
1164
+ ) : F(k, { candidate: null }, Q, a);
1165
+ } catch ($) {
1166
+ (p = n.onError) == null || p.call(n, $, { streamId: k });
1136
1167
  }
1137
- }, k.onopen = () => {
1138
- p = !0, (!P || w) && J();
1168
+ }, E.onopen = () => {
1169
+ w = !0, (!P || h) && J();
1139
1170
  };
1140
- const H = (m) => {
1141
- var h;
1142
- (h = n.onVideoIdChange) == null || h.call(n, m);
1171
+ const X = (m) => {
1172
+ var p;
1173
+ (p = n.onVideoIdChange) == null || p.call(n, m);
1143
1174
  };
1144
- function L(m, h) {
1145
- if (m === j.StreamStarted && typeof h == "object" && "metadata" in h) {
1146
- const x = h.metadata;
1147
- H(x.videoId);
1175
+ function L(m, p) {
1176
+ if (m === b.StreamStarted && typeof p == "object" && "metadata" in p) {
1177
+ const $ = p.metadata;
1178
+ X($.videoId);
1148
1179
  }
1149
- m === j.StreamDone && H(null), u = m === j.StreamStarted ? S.Start : S.Stop, ze({
1150
- statsSignal: T === ae.Legacy ? _ : void 0,
1180
+ m === b.StreamDone && X(null), u = m === b.StreamStarted ? D.Start : D.Stop, Je({
1181
+ statsSignal: A === ie.Legacy ? C : void 0,
1151
1182
  dataChannelSignal: u,
1152
1183
  onVideoStateChange: n.onVideoStateChange,
1153
1184
  onAgentActivityStateChange: n.onAgentActivityStateChange,
1154
- streamType: T,
1185
+ streamType: A,
1155
1186
  log: r
1156
1187
  });
1157
1188
  }
1158
- function N(m, h) {
1159
- var K;
1160
- const x = typeof h == "string" ? h : h == null ? void 0 : h.metadata;
1161
- x && c.enrich({ streamMetadata: x }), (K = n.onStreamReady) == null || K.call(n);
1189
+ function K(m, p) {
1190
+ var V;
1191
+ const $ = typeof p == "string" ? p : p == null ? void 0 : p.metadata;
1192
+ $ && c.enrich({ streamMetadata: $ }), (V = n.onStreamReady) == null || V.call(n);
1162
1193
  }
1163
- const q = {
1164
- [j.StreamStarted]: L,
1165
- [j.StreamDone]: L,
1166
- [j.StreamReady]: N
1194
+ const Y = {
1195
+ [b.StreamStarted]: L,
1196
+ [b.StreamDone]: L,
1197
+ [b.StreamReady]: K
1167
1198
  };
1168
- k.onmessage = (m) => {
1169
- var K;
1170
- const { subject: h, data: x } = d(m.data);
1171
- (K = q[h]) == null || K.call(q, h, x);
1199
+ E.onmessage = (m) => {
1200
+ var V;
1201
+ const { subject: p, data: $ } = d(m.data);
1202
+ (V = Y[p]) == null || V.call(Y, p, $);
1172
1203
  }, y.oniceconnectionstatechange = () => {
1173
- var h;
1204
+ var p;
1174
1205
  r("peerConnection.oniceconnectionstatechange => " + y.iceConnectionState);
1175
- const m = Fe(y.iceConnectionState);
1176
- m !== R.Connected && ((h = n.onConnectionStateChange) == null || h.call(n, m));
1206
+ const m = Ne(y.iceConnectionState);
1207
+ m !== M.Connected && ((p = n.onConnectionStateChange) == null || p.call(n, m));
1177
1208
  }, y.ontrack = (m) => {
1178
- var h;
1179
- r("peerConnection.ontrack", m), r("CALLBACK: onSrcObjectReady"), (h = n.onSrcObjectReady) == null || h.call(n, m.streams[0]);
1180
- }, await y.setRemoteDescription(U), r("set remote description OK");
1209
+ var p;
1210
+ r("peerConnection.ontrack", m), r("CALLBACK: onSrcObjectReady"), (p = n.onSrcObjectReady) == null || p.call(n, m.streams[0]);
1211
+ }, await y.setRemoteDescription(q), r("set remote description OK");
1181
1212
  const oe = await y.createAnswer();
1182
- return r("create answer OK"), await y.setLocalDescription(oe), r("set local description OK"), await $(I, oe, M, a), r("start connection OK"), {
1213
+ return r("create answer OK"), await y.setLocalDescription(oe), r("set local description OK"), await x(k, oe, Q, a), r("start connection OK"), {
1183
1214
  /**
1184
1215
  * Method to send request to server to get clip or talk depend on you payload
1185
1216
  * @param payload
1186
1217
  */
1187
1218
  speak(m) {
1188
- return A(I, M, m);
1219
+ return j(k, Q, m);
1189
1220
  },
1190
1221
  /**
1191
1222
  * Method to close RTC connection
1192
1223
  */
1193
1224
  async disconnect() {
1194
1225
  var m;
1195
- if (I) {
1196
- const h = Fe(y.iceConnectionState);
1226
+ if (k) {
1227
+ const p = Ne(y.iceConnectionState);
1197
1228
  if (y) {
1198
- if (h === R.New) {
1199
- clearInterval(O);
1229
+ if (p === M.New) {
1230
+ O.stop();
1200
1231
  return;
1201
1232
  }
1202
1233
  y.close(), y.oniceconnectionstatechange = null, y.onnegotiationneeded = null, y.onicecandidate = null, y.ontrack = null;
1203
1234
  }
1204
1235
  try {
1205
- h === R.Connected && await C(I, M).catch((x) => {
1236
+ p === M.Connected && await S(k, Q).catch(($) => {
1206
1237
  });
1207
- } catch (x) {
1208
- r("Error on close stream connection", x);
1238
+ } catch ($) {
1239
+ r("Error on close stream connection", $);
1209
1240
  }
1210
- (m = n.onAgentActivityStateChange) == null || m.call(n, W.Idle), clearInterval(O);
1241
+ (m = n.onAgentActivityStateChange) == null || m.call(n, H.Idle), O.stop();
1211
1242
  }
1212
1243
  },
1213
1244
  /**
1214
1245
  * Method to send data channel messages to the server
1215
1246
  */
1216
1247
  sendDataChannelMessage(m) {
1217
- var h, x;
1218
- if (!w || k.readyState !== "open") {
1219
- r("Data channel is not ready for sending messages"), (h = n.onError) == null || h.call(n, new Error("Data channel is not ready for sending messages"), {
1220
- streamId: I
1248
+ var p, $;
1249
+ if (!h || E.readyState !== "open") {
1250
+ r("Data channel is not ready for sending messages"), (p = n.onError) == null || p.call(n, new Error("Data channel is not ready for sending messages"), {
1251
+ streamId: k
1221
1252
  });
1222
1253
  return;
1223
1254
  }
1224
1255
  try {
1225
- k.send(m);
1226
- } catch (K) {
1227
- r("Error sending data channel message", K), (x = n.onError) == null || x.call(n, K, { streamId: I });
1256
+ E.send(m);
1257
+ } catch (V) {
1258
+ r("Error sending data channel message", V), ($ = n.onError) == null || $.call(n, V, { streamId: k });
1228
1259
  }
1229
1260
  },
1230
1261
  /**
1231
1262
  * Session identifier information, should be returned in the body of all streaming requests
1232
1263
  */
1233
- sessionId: M,
1264
+ sessionId: Q,
1234
1265
  /**
1235
1266
  * Id of current RTC stream
1236
1267
  */
1237
- streamId: I,
1238
- streamType: T,
1239
- interruptAvailable: Z ?? !1,
1268
+ streamId: k,
1269
+ streamType: A,
1270
+ interruptAvailable: te ?? !1,
1240
1271
  triggersAvailable: f ?? !1
1241
1272
  };
1242
1273
  }
1243
- var Ee = /* @__PURE__ */ ((e) => (e.V1 = "v1", e.V2 = "v2", e))(Ee || {});
1244
- async function tn(e, t, i, n) {
1274
+ var Ae = /* @__PURE__ */ ((e) => (e.V1 = "v1", e.V2 = "v2", e))(Ae || {});
1275
+ async function an(e, t, i, n) {
1245
1276
  const s = e.id;
1246
1277
  switch (t.version) {
1247
1278
  case "v1": {
1248
1279
  const { version: o, ...c } = t;
1249
- return en(s, c, i, n);
1280
+ return rn(s, c, i, n);
1250
1281
  }
1251
1282
  case "v2": {
1252
1283
  const { version: o, ...c } = t;
1253
1284
  switch (c.transport_provider) {
1254
- case ye.Livekit:
1255
- const { createLiveKitStreamingManager: a } = await Promise.resolve().then(() => _t);
1285
+ case Se.Livekit:
1286
+ const { createLiveKitStreamingManager: a } = await Promise.resolve().then(() => Nt);
1256
1287
  return a(s, c, i);
1257
1288
  default:
1258
1289
  throw new Error(`Unsupported transport provider: ${c.transport_provider}`);
@@ -1262,13 +1293,13 @@ async function tn(e, t, i, n) {
1262
1293
  throw new Error(`Invalid stream version: ${t.version}`);
1263
1294
  }
1264
1295
  }
1265
- const nn = "cht";
1266
- function rn() {
1296
+ const on = "cht";
1297
+ function sn() {
1267
1298
  return {
1268
- transport_provider: ye.Livekit
1299
+ transport_provider: Se.Livekit
1269
1300
  };
1270
1301
  }
1271
- function an(e) {
1302
+ function cn(e) {
1272
1303
  var s, o;
1273
1304
  const { streamOptions: t } = e ?? {}, i = ((s = e == null ? void 0 : e.mixpanelAdditionalProperties) == null ? void 0 : s.plan) !== void 0 ? {
1274
1305
  plan: (o = e.mixpanelAdditionalProperties) == null ? void 0 : o.plan
@@ -1281,14 +1312,14 @@ function an(e) {
1281
1312
  fluent: t == null ? void 0 : t.fluent
1282
1313
  }, ...i && { end_user_data: i } };
1283
1314
  }
1284
- function on(e, t) {
1285
- return Ie(e.presenter.type) ? { version: Ee.V2, ...rn() } : { version: Ee.V1, ...an(t) };
1315
+ function dn(e, t) {
1316
+ return Pe(e.presenter.type) ? { version: Ae.V2, ...sn() } : { version: Ae.V1, ...cn(t) };
1286
1317
  }
1287
- function sn(e, t, i, n, s) {
1288
- s === ae.Fluent ? cn(e, t, i, n, s) : un(e, t, i, n, s);
1318
+ function un(e, t, i, n, s) {
1319
+ s === ie.Fluent ? ln(e, t, i, n, s) : mn(e, t, i, n, s);
1289
1320
  }
1290
- function cn(e, t, i, n, s) {
1291
- e === S.Start ? n.track("stream-session", { event: "start", "stream-type": s }) : e === S.Stop && n.track("stream-session", {
1321
+ function ln(e, t, i, n, s) {
1322
+ e === D.Start ? n.track("stream-session", { event: "start", "stream-type": s }) : e === D.Stop && n.track("stream-session", {
1292
1323
  event: "stop",
1293
1324
  is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
1294
1325
  background: t.presenter.type === "clip" && t.presenter.background,
@@ -1296,13 +1327,13 @@ function cn(e, t, i, n, s) {
1296
1327
  ...i
1297
1328
  });
1298
1329
  }
1299
- function dn(e, t, i, n) {
1300
- ee.get() <= 0 || (e === S.Start ? i.linkTrack(
1330
+ function fn(e, t, i, n) {
1331
+ ne.get() <= 0 || (e === D.Start ? i.linkTrack(
1301
1332
  "agent-video",
1302
- { event: "start", latency: ee.get(!0), "stream-type": n },
1333
+ { event: "start", latency: ne.get(!0), "stream-type": n },
1303
1334
  "start",
1304
- [j.StreamVideoCreated]
1305
- ) : e === S.Stop && i.linkTrack(
1335
+ [b.StreamVideoCreated]
1336
+ ) : e === D.Stop && i.linkTrack(
1306
1337
  "agent-video",
1307
1338
  {
1308
1339
  event: "stop",
@@ -1311,16 +1342,16 @@ function dn(e, t, i, n) {
1311
1342
  "stream-type": n
1312
1343
  },
1313
1344
  "done",
1314
- [j.StreamVideoDone]
1345
+ [b.StreamVideoDone]
1315
1346
  ));
1316
1347
  }
1317
- function un(e, t, i, n, s) {
1318
- ee.get() <= 0 || (e === S.Start ? n.linkTrack(
1348
+ function mn(e, t, i, n, s) {
1349
+ ne.get() <= 0 || (e === D.Start ? n.linkTrack(
1319
1350
  "agent-video",
1320
- { event: "start", latency: ee.get(!0), "stream-type": s },
1351
+ { event: "start", latency: ne.get(!0), "stream-type": s },
1321
1352
  "start",
1322
- [j.StreamVideoCreated]
1323
- ) : e === S.Stop && n.linkTrack(
1353
+ [b.StreamVideoCreated]
1354
+ ) : e === D.Stop && n.linkTrack(
1324
1355
  "agent-video",
1325
1356
  {
1326
1357
  event: "stop",
@@ -1330,17 +1361,17 @@ function un(e, t, i, n, s) {
1330
1361
  ...i
1331
1362
  },
1332
1363
  "done",
1333
- [j.StreamVideoDone]
1364
+ [b.StreamVideoDone]
1334
1365
  ));
1335
1366
  }
1336
- function Ne(e, t, i, n) {
1337
- return ee.reset(), xe.update(), new Promise(async (s, o) => {
1367
+ function Ue(e, t, i, n) {
1368
+ return ne.reset(), ze.update(), new Promise(async (s, o) => {
1338
1369
  try {
1339
1370
  let c, a = !1;
1340
- const r = on(e, t);
1371
+ const r = dn(e, t);
1341
1372
  i.enrich({
1342
1373
  "stream-version": r.version.toString()
1343
- }), c = await tn(
1374
+ }), c = await an(
1344
1375
  e,
1345
1376
  r,
1346
1377
  {
@@ -1349,30 +1380,30 @@ function Ne(e, t, i, n) {
1349
1380
  callbacks: {
1350
1381
  ...t.callbacks,
1351
1382
  onConnectionStateChange: (d) => {
1352
- var w, p;
1353
- (p = (w = t.callbacks).onConnectionStateChange) == null || p.call(w, d), d === R.Connected && (c ? s(c) : a = !0);
1383
+ var h, w;
1384
+ (w = (h = t.callbacks).onConnectionStateChange) == null || w.call(h, d), d === M.Connected && (c ? s(c) : a = !0);
1354
1385
  },
1355
- onVideoStateChange: (d, w) => {
1356
- var p, u;
1357
- (u = (p = t.callbacks).onVideoStateChange) == null || u.call(p, d), sn(
1386
+ onVideoStateChange: (d, h) => {
1387
+ var w, u;
1388
+ (u = (w = t.callbacks).onVideoStateChange) == null || u.call(w, d), un(
1358
1389
  d,
1359
1390
  e,
1360
- w,
1391
+ h,
1361
1392
  i,
1362
1393
  c.streamType
1363
1394
  );
1364
1395
  },
1365
1396
  onAgentActivityStateChange: (d) => {
1366
- var w, p;
1367
- (p = (w = t.callbacks).onAgentActivityStateChange) == null || p.call(w, d), d === W.Talking ? Re.update() : Re.reset(), dn(
1368
- d === W.Talking ? S.Start : S.Stop,
1397
+ var h, w;
1398
+ (w = (h = t.callbacks).onAgentActivityStateChange) == null || w.call(h, d), d === H.Talking ? Te.update() : Te.reset(), fn(
1399
+ d === H.Talking ? D.Start : D.Stop,
1369
1400
  e,
1370
1401
  i,
1371
1402
  c.streamType
1372
1403
  );
1373
1404
  },
1374
1405
  onStreamReady: () => {
1375
- const d = xe.get(!0);
1406
+ const d = ze.get(!0);
1376
1407
  i.track("agent-chat", { event: "ready", latency: d });
1377
1408
  }
1378
1409
  }
@@ -1384,65 +1415,65 @@ function Ne(e, t, i, n) {
1384
1415
  }
1385
1416
  });
1386
1417
  }
1387
- async function ln(e, t, i, n, s) {
1388
- var w, p, u, _;
1418
+ async function gn(e, t, i, n, s) {
1419
+ var h, w, u, C;
1389
1420
  const o = async () => {
1390
- if (Ie(e.presenter.type)) {
1391
- const $ = await Ne(e, t, n), A = `${nn}_${$.sessionId}`, C = (/* @__PURE__ */ new Date()).toISOString();
1421
+ if (Pe(e.presenter.type)) {
1422
+ const x = await Ue(e, t, n), j = `${on}_${x.sessionId}`, S = (/* @__PURE__ */ new Date()).toISOString();
1392
1423
  return { chatResult: {
1393
- chatMode: F.Functional,
1424
+ chatMode: B.Functional,
1394
1425
  chat: {
1395
- id: A,
1426
+ id: j,
1396
1427
  agent_id: e.id,
1397
1428
  owner_id: e.owner_id ?? "",
1398
- created: C,
1399
- modified: C,
1400
- agent_id__created_at: C,
1401
- agent_id__modified_at: C,
1402
- chat_mode: F.Functional,
1429
+ created: S,
1430
+ modified: S,
1431
+ agent_id__created_at: S,
1432
+ agent_id__modified_at: S,
1433
+ chat_mode: B.Functional,
1403
1434
  messages: []
1404
1435
  }
1405
- }, streamingManager: $ };
1436
+ }, streamingManager: x };
1406
1437
  } else {
1407
- const $ = new AbortController(), A = $.signal;
1408
- let C;
1438
+ const x = new AbortController(), j = x.signal;
1439
+ let S;
1409
1440
  try {
1410
- const B = He(
1441
+ const R = Xe(
1411
1442
  e,
1412
1443
  i,
1413
1444
  n,
1414
1445
  t.mode,
1415
1446
  t.persistentChat,
1416
1447
  s
1417
- ), z = Ne(e, t, n, A).then((te) => (C = te, te)), [I, U] = await Promise.all([B, z]);
1418
- return { chatResult: I, streamingManager: U };
1419
- } catch (B) {
1420
- throw $.abort(), C && await C.disconnect().catch(() => {
1421
- }), B;
1448
+ ), F = Ue(e, t, n, j).then((N) => (S = N, N)), [k, q] = await Promise.all([R, F]);
1449
+ return { chatResult: k, streamingManager: q };
1450
+ } catch (R) {
1451
+ throw x.abort(), S && await S.disconnect().catch(() => {
1452
+ }), R;
1422
1453
  }
1423
1454
  }
1424
1455
  }, { chatResult: c, streamingManager: a } = await o(), { chat: r, chatMode: d } = c;
1425
- return d && t.mode !== void 0 && d !== t.mode && (t.mode = d, (p = (w = t.callbacks).onModeChange) == null || p.call(w, d), d !== F.Functional) ? ((_ = (u = t.callbacks).onError) == null || _.call(u, new Ge(d)), a == null || a.disconnect(), { chat: r }) : { chat: r, streamingManager: a };
1456
+ return d && t.mode !== void 0 && d !== t.mode && (t.mode = d, (w = (h = t.callbacks).onModeChange) == null || w.call(h, d), d !== B.Functional) ? ((C = (u = t.callbacks).onError) == null || C.call(u, new nt(d)), a == null || a.disconnect(), { chat: r }) : { chat: r, streamingManager: a };
1426
1457
  }
1427
- async function gn(e, t) {
1428
- var M, Y, Z;
1458
+ async function wn(e, t) {
1459
+ var Q, z, te;
1429
1460
  let i = !0, n = null;
1430
- const s = t.mixpanelKey || ft, o = t.wsURL || lt, c = t.baseURL || ge, a = t.mode || F.Functional, r = {
1461
+ const s = t.mixpanelKey || pt, o = t.wsURL || ht, c = t.baseURL || he, a = t.mode || B.Functional, r = {
1431
1462
  messages: [],
1432
1463
  chatMode: a
1433
- }, d = Rt({
1464
+ }, d = Tt({
1434
1465
  token: s,
1435
1466
  agentId: e,
1436
1467
  isEnabled: t.enableAnalitics,
1437
1468
  externalId: t.externalId,
1438
1469
  mixpanelAdditionalProperties: t.mixpanelAdditionalProperties
1439
- }), w = Date.now();
1440
- Be(() => {
1441
- d.track("agent-sdk", { event: "init" }, w);
1470
+ }), h = Date.now();
1471
+ Fe(() => {
1472
+ d.track("agent-sdk", { event: "init" }, h);
1442
1473
  });
1443
- const p = wt(t.auth, c, t.callbacks.onError, t.externalId), u = await p.getById(e), _ = Ie(u.presenter.type);
1444
- d.enrich(vt(u));
1445
- const { onMessage: $, clearQueue: A } = zt(
1474
+ const w = kt(t.auth, c, t.callbacks.onError, t.externalId), u = await w.getById(e), C = Pe(u.presenter.type);
1475
+ d.enrich(Dt(u));
1476
+ const { onMessage: x, clearQueue: j } = Yt(
1446
1477
  d,
1447
1478
  r,
1448
1479
  t,
@@ -1452,70 +1483,70 @@ async function gn(e, t) {
1452
1483
  return (f = r.socketManager) == null ? void 0 : f.disconnect();
1453
1484
  }
1454
1485
  );
1455
- r.messages = Mt(t.initialMessages), (Y = (M = t.callbacks).onNewMessage) == null || Y.call(M, [...r.messages], "answer");
1456
- const C = (f) => {
1486
+ r.messages = At(t.initialMessages), (z = (Q = t.callbacks).onNewMessage) == null || z.call(Q, [...r.messages], "answer");
1487
+ const S = (f) => {
1457
1488
  n = f;
1458
- }, B = ({ type: f }) => {
1459
- var k, T, P;
1489
+ }, R = ({ type: f }) => {
1490
+ var E, A, P;
1460
1491
  const y = r.messages[r.messages.length - 1];
1461
1492
  d.track("agent-video-interrupt", {
1462
1493
  type: f || "click",
1463
- video_duration_to_interrupt: Re.get(!0),
1464
- message_duration_to_interrupt: ee.get(!0)
1465
- }), y.interrupted = !0, (T = (k = t.callbacks).onNewMessage) == null || T.call(k, [...r.messages], "answer"), _ ? Lt(r.streamingManager) : (At(r.streamingManager, (P = r.streamingManager) == null ? void 0 : P.streamType, n), Pt(r.streamingManager, n));
1466
- }, z = Date.now();
1467
- Be(() => {
1468
- d.track("agent-sdk", { event: "loaded", ...yt(u) }, z);
1494
+ video_duration_to_interrupt: Te.get(!0),
1495
+ message_duration_to_interrupt: ne.get(!0)
1496
+ }), y.interrupted = !0, (A = (E = t.callbacks).onNewMessage) == null || A.call(E, [...r.messages], "answer"), C ? Vt(r.streamingManager) : (Jt(r.streamingManager, (P = r.streamingManager) == null ? void 0 : P.streamType, n), Ut(r.streamingManager, n));
1497
+ }, F = Date.now();
1498
+ Fe(() => {
1499
+ d.track("agent-sdk", { event: "loaded", ...Rt(u) }, F);
1469
1500
  });
1470
- async function I(f) {
1471
- var J, O, H, L, N, q, oe;
1472
- (O = (J = t.callbacks).onConnectionStateChange) == null || O.call(J, R.Connecting), ee.reset(), f && !i && (delete r.chat, (L = (H = t.callbacks).onNewMessage) == null || L.call(H, [...r.messages], "answer"));
1473
- const y = a === F.DirectPlayback || _ ? Promise.resolve(void 0) : $t(
1501
+ async function k(f) {
1502
+ var J, O, X, L, K, Y, oe;
1503
+ (O = (J = t.callbacks).onConnectionStateChange) == null || O.call(J, M.Connecting), ne.reset(), f && !i && (delete r.chat, (L = (X = t.callbacks).onNewMessage) == null || L.call(X, [...r.messages], "answer"));
1504
+ const y = a === B.DirectPlayback || C ? Promise.resolve(void 0) : Ht(
1474
1505
  t.auth,
1475
1506
  o,
1476
- { onMessage: $, onError: t.callbacks.onError },
1507
+ { onMessage: x, onError: t.callbacks.onError },
1477
1508
  t.externalId
1478
- ), k = Se(
1479
- () => ln(
1509
+ ), E = Me(
1510
+ () => gn(
1480
1511
  u,
1481
1512
  {
1482
1513
  ...t,
1483
1514
  mode: a,
1484
1515
  callbacks: {
1485
1516
  ...t.callbacks,
1486
- onVideoIdChange: C,
1487
- onMessage: $,
1488
- onInterruptDetected: B
1517
+ onVideoIdChange: S,
1518
+ onMessage: x,
1519
+ onInterruptDetected: R
1489
1520
  }
1490
1521
  },
1491
- p,
1522
+ w,
1492
1523
  d,
1493
1524
  r.chat
1494
1525
  ),
1495
1526
  {
1496
1527
  limit: 3,
1497
- timeout: dt,
1528
+ timeout: mt,
1498
1529
  timeoutErrorMessage: "Timeout initializing the stream",
1499
- shouldRetryFn: (V) => (V == null ? void 0 : V.message) !== "Could not connect" && V.status !== 429 && (V == null ? void 0 : V.message) !== "InsufficientCreditsError",
1530
+ shouldRetryFn: (U) => (U == null ? void 0 : U.message) !== "Could not connect" && U.status !== 429 && (U == null ? void 0 : U.message) !== "InsufficientCreditsError",
1500
1531
  delayMs: 1e3
1501
1532
  }
1502
- ).catch((V) => {
1503
- var m, h;
1504
- throw te(F.Maintenance), (h = (m = t.callbacks).onConnectionStateChange) == null || h.call(m, R.Fail), V;
1505
- }), [T, { streamingManager: P, chat: E }] = await Promise.all([y, k]);
1506
- E && E.id !== ((N = r.chat) == null ? void 0 : N.id) && ((oe = (q = t.callbacks).onNewChat) == null || oe.call(q, E.id)), r.streamingManager = P, r.socketManager = T, r.chat = E, i = !1, d.enrich({
1507
- chatId: E == null ? void 0 : E.id,
1533
+ ).catch((U) => {
1534
+ var m, p;
1535
+ throw N(B.Maintenance), (p = (m = t.callbacks).onConnectionStateChange) == null || p.call(m, M.Fail), U;
1536
+ }), [A, { streamingManager: P, chat: T }] = await Promise.all([y, E]);
1537
+ T && T.id !== ((K = r.chat) == null ? void 0 : K.id) && ((oe = (Y = t.callbacks).onNewChat) == null || oe.call(Y, T.id)), r.streamingManager = P, r.socketManager = A, r.chat = T, i = !1, d.enrich({
1538
+ chatId: T == null ? void 0 : T.id,
1508
1539
  streamId: P == null ? void 0 : P.streamId,
1509
1540
  mode: r.chatMode
1510
- }), te((E == null ? void 0 : E.chat_mode) ?? a);
1541
+ }), N((T == null ? void 0 : T.chat_mode) ?? a);
1511
1542
  }
1512
- async function U() {
1513
- var f, y, k, T;
1514
- (f = r.socketManager) == null || f.disconnect(), await ((y = r.streamingManager) == null ? void 0 : y.disconnect()), delete r.streamingManager, delete r.socketManager, (T = (k = t.callbacks).onConnectionStateChange) == null || T.call(k, R.Disconnected);
1543
+ async function q() {
1544
+ var f, y, E, A;
1545
+ (f = r.socketManager) == null || f.disconnect(), await ((y = r.streamingManager) == null ? void 0 : y.disconnect()), delete r.streamingManager, delete r.socketManager, (A = (E = t.callbacks).onConnectionStateChange) == null || A.call(E, M.Disconnected);
1515
1546
  }
1516
- async function te(f) {
1517
- var y, k;
1518
- f !== r.chatMode && (d.track("agent-mode-change", { mode: f }), r.chatMode = f, r.chatMode !== F.Functional && await U(), (k = (y = t.callbacks).onModeChange) == null || k.call(y, f));
1547
+ async function N(f) {
1548
+ var y, E;
1549
+ f !== r.chatMode && (d.track("agent-mode-change", { mode: f }), r.chatMode = f, r.chatMode !== B.Functional && await q(), (E = (y = t.callbacks).onModeChange) == null || E.call(y, f));
1519
1550
  }
1520
1551
  return {
1521
1552
  agent: u,
@@ -1531,36 +1562,36 @@ async function gn(e, t) {
1531
1562
  var f;
1532
1563
  return ((f = r.streamingManager) == null ? void 0 : f.triggersAvailable) ?? !1;
1533
1564
  },
1534
- starterMessages: ((Z = u.knowledge) == null ? void 0 : Z.starter_message) || [],
1535
- getSTTToken: () => p.getSTTToken(u.id),
1536
- changeMode: te,
1565
+ starterMessages: ((te = u.knowledge) == null ? void 0 : te.starter_message) || [],
1566
+ getSTTToken: () => w.getSTTToken(u.id),
1567
+ changeMode: N,
1537
1568
  enrichAnalytics: d.enrich,
1538
1569
  async connect() {
1539
- await I(!0), d.track("agent-chat", {
1570
+ await k(!0), d.track("agent-chat", {
1540
1571
  event: "connect",
1541
1572
  mode: r.chatMode
1542
1573
  });
1543
1574
  },
1544
1575
  async reconnect() {
1545
1576
  const f = r.streamingManager;
1546
- if (_ && (f != null && f.reconnect)) {
1577
+ if (C && (f != null && f.reconnect)) {
1547
1578
  try {
1548
1579
  await f.reconnect(), d.track("agent-chat", {
1549
1580
  event: "reconnect",
1550
1581
  mode: r.chatMode
1551
1582
  });
1552
1583
  } catch {
1553
- await U(), await I(!1);
1584
+ await q(), await k(!1);
1554
1585
  }
1555
1586
  return;
1556
1587
  }
1557
- await U(), await I(!1), d.track("agent-chat", {
1588
+ await q(), await k(!1), d.track("agent-chat", {
1558
1589
  event: "reconnect",
1559
1590
  mode: r.chatMode
1560
1591
  });
1561
1592
  },
1562
1593
  async disconnect() {
1563
- await U(), d.track("agent-chat", {
1594
+ await q(), d.track("agent-chat", {
1564
1595
  event: "disconnect",
1565
1596
  mode: r.chatMode
1566
1597
  });
@@ -1578,120 +1609,120 @@ async function gn(e, t) {
1578
1609
  return r.streamingManager.unpublishMicrophoneStream();
1579
1610
  },
1580
1611
  async chat(f) {
1581
- var P, E, J, O, H;
1612
+ var P, T, J, O, X;
1582
1613
  const y = () => {
1583
- if (Ve(a))
1614
+ if (He(a))
1584
1615
  throw new ue(`${a} is enabled, chat is disabled`);
1585
1616
  if (f.length >= 800)
1586
1617
  throw new ue("Message cannot be more than 800 characters");
1587
1618
  if (f.length === 0)
1588
1619
  throw new ue("Message cannot be empty");
1589
- if (r.chatMode === F.Maintenance)
1620
+ if (r.chatMode === B.Maintenance)
1590
1621
  throw new ue("Chat is in maintenance mode");
1591
- if (![F.TextOnly, F.Playground].includes(r.chatMode)) {
1622
+ if (![B.TextOnly, B.Playground].includes(r.chatMode)) {
1592
1623
  if (!r.streamingManager)
1593
1624
  throw new ue("Streaming manager is not initialized");
1594
1625
  if (!r.chat)
1595
1626
  throw new ue("Chat is not initialized");
1596
1627
  }
1597
- }, k = async () => {
1598
- var L, N;
1628
+ }, E = async () => {
1629
+ var L, K;
1599
1630
  if (!r.chat) {
1600
- const q = await He(
1631
+ const Y = await Xe(
1601
1632
  u,
1602
- p,
1633
+ w,
1603
1634
  d,
1604
1635
  r.chatMode,
1605
1636
  t.persistentChat
1606
1637
  );
1607
- if (!q.chat)
1608
- throw new Ye(r.chatMode, !!t.persistentChat);
1609
- r.chat = q.chat, (N = (L = t.callbacks).onNewChat) == null || N.call(L, r.chat.id);
1638
+ if (!Y.chat)
1639
+ throw new tt(r.chatMode, !!t.persistentChat);
1640
+ r.chat = Y.chat, (K = (L = t.callbacks).onNewChat) == null || K.call(L, r.chat.id);
1610
1641
  }
1611
1642
  return r.chat.id;
1612
- }, T = async (L, N) => {
1613
- const q = r.chatMode === F.Playground;
1614
- return Se(_ && !q ? async () => {
1615
- var m, h;
1616
- return await ((h = (m = r.streamingManager) == null ? void 0 : m.sendTextMessage) == null ? void 0 : h.call(m, f)), Promise.resolve({});
1643
+ }, A = async (L, K) => {
1644
+ const Y = r.chatMode === B.Playground;
1645
+ return Me(C && !Y ? async () => {
1646
+ var m, p;
1647
+ return await ((p = (m = r.streamingManager) == null ? void 0 : m.sendTextMessage) == null ? void 0 : p.call(m, f)), Promise.resolve({});
1617
1648
  } : async () => {
1618
- var m, h;
1619
- return p.chat(
1649
+ var m, p;
1650
+ return w.chat(
1620
1651
  u.id,
1621
- N,
1652
+ K,
1622
1653
  {
1623
1654
  chatMode: r.chatMode,
1624
1655
  streamId: (m = r.streamingManager) == null ? void 0 : m.streamId,
1625
- sessionId: (h = r.streamingManager) == null ? void 0 : h.sessionId,
1626
- messages: L.map(({ matches: x, ...K }) => K)
1656
+ sessionId: (p = r.streamingManager) == null ? void 0 : p.sessionId,
1657
+ messages: L.map(({ matches: $, ...V }) => V)
1627
1658
  },
1628
1659
  {
1629
- ...Oe(r.chatMode),
1660
+ ...Qe(r.chatMode),
1630
1661
  skipErrorHandler: !0
1631
1662
  }
1632
1663
  );
1633
1664
  }, {
1634
1665
  limit: 2,
1635
1666
  shouldRetryFn: (m) => {
1636
- var K, he, pe, ce;
1637
- const h = (K = m == null ? void 0 : m.message) == null ? void 0 : K.includes("missing or invalid session_id");
1638
- return !((he = m == null ? void 0 : m.message) == null ? void 0 : he.includes("Stream Error")) && !h ? ((ce = (pe = t.callbacks).onError) == null || ce.call(pe, m), !1) : !0;
1667
+ var V, ce, pe, we;
1668
+ const p = (V = m == null ? void 0 : m.message) == null ? void 0 : V.includes("missing or invalid session_id");
1669
+ return !((ce = m == null ? void 0 : m.message) == null ? void 0 : ce.includes("Stream Error")) && !p ? ((we = (pe = t.callbacks).onError) == null || we.call(pe, m), !1) : !0;
1639
1670
  },
1640
1671
  onRetry: async () => {
1641
- await U(), await I(!1);
1672
+ await q(), await k(!1);
1642
1673
  }
1643
1674
  });
1644
1675
  };
1645
1676
  try {
1646
- A(), y(), r.messages.push({
1677
+ j(), y(), r.messages.push({
1647
1678
  id: le(),
1648
1679
  role: "user",
1649
1680
  content: f,
1650
- created_at: new Date(ee.update()).toISOString()
1651
- }), (E = (P = t.callbacks).onNewMessage) == null || E.call(P, [...r.messages], "user");
1652
- const L = await k(), N = await T([...r.messages], L);
1681
+ created_at: new Date(ne.update()).toISOString()
1682
+ }), (T = (P = t.callbacks).onNewMessage) == null || T.call(P, [...r.messages], "user");
1683
+ const L = await E(), K = await A([...r.messages], L);
1653
1684
  return r.messages.push({
1654
1685
  id: le(),
1655
1686
  role: "assistant",
1656
- content: N.result || "",
1687
+ content: K.result || "",
1657
1688
  created_at: (/* @__PURE__ */ new Date()).toISOString(),
1658
- context: N.context,
1659
- matches: N.matches
1689
+ context: K.context,
1690
+ matches: K.matches
1660
1691
  }), d.track("agent-message-send", {
1661
1692
  event: "success",
1662
1693
  messages: r.messages.length + 1
1663
- }), N.result && ((O = (J = t.callbacks).onNewMessage) == null || O.call(J, [...r.messages], "answer"), d.track("agent-message-received", {
1664
- latency: ee.get(!0),
1694
+ }), K.result && ((O = (J = t.callbacks).onNewMessage) == null || O.call(J, [...r.messages], "answer"), d.track("agent-message-received", {
1695
+ latency: ne.get(!0),
1665
1696
  messages: r.messages.length
1666
- })), N;
1697
+ })), K;
1667
1698
  } catch (L) {
1668
- throw ((H = r.messages[r.messages.length - 1]) == null ? void 0 : H.role) === "assistant" && r.messages.pop(), d.track("agent-message-send", {
1699
+ throw ((X = r.messages[r.messages.length - 1]) == null ? void 0 : X.role) === "assistant" && r.messages.pop(), d.track("agent-message-send", {
1669
1700
  event: "error",
1670
1701
  messages: r.messages.length
1671
1702
  }), L;
1672
1703
  }
1673
1704
  },
1674
- rate(f, y, k) {
1675
- var E, J, O, H;
1676
- const T = r.messages.find((L) => L.id === f);
1705
+ rate(f, y, E) {
1706
+ var T, J, O, X;
1707
+ const A = r.messages.find((L) => L.id === f);
1677
1708
  if (r.chat) {
1678
- if (!T)
1709
+ if (!A)
1679
1710
  throw new Error("Message not found");
1680
1711
  } else throw new Error("Chat is not initialized");
1681
- const P = ((E = T.matches) == null ? void 0 : E.map((L) => [L.document_id, L.id])) ?? [];
1712
+ const P = ((T = A.matches) == null ? void 0 : T.map((L) => [L.document_id, L.id])) ?? [];
1682
1713
  return d.track("agent-rate", {
1683
- event: k ? "update" : "create",
1714
+ event: E ? "update" : "create",
1684
1715
  thumb: y === 1 ? "up" : "down",
1685
1716
  knowledge_id: ((J = u.knowledge) == null ? void 0 : J.id) ?? "",
1686
1717
  matches: P,
1687
1718
  score: y
1688
- }), k ? p.updateRating(u.id, r.chat.id, k, {
1719
+ }), E ? w.updateRating(u.id, r.chat.id, E, {
1689
1720
  knowledge_id: ((O = u.knowledge) == null ? void 0 : O.id) ?? "",
1690
1721
  message_id: f,
1691
1722
  matches: P,
1692
1723
  score: y
1693
- }) : p.createRating(u.id, r.chat.id, {
1694
- knowledge_id: ((H = u.knowledge) == null ? void 0 : H.id) ?? "",
1724
+ }) : w.createRating(u.id, r.chat.id, {
1725
+ knowledge_id: ((X = u.knowledge) == null ? void 0 : X.id) ?? "",
1695
1726
  message_id: f,
1696
1727
  matches: P,
1697
1728
  score: y
@@ -1700,10 +1731,10 @@ async function gn(e, t) {
1700
1731
  deleteRate(f) {
1701
1732
  if (!r.chat)
1702
1733
  throw new Error("Chat is not initialized");
1703
- return d.track("agent-rate-delete", { type: "text" }), p.deleteRating(u.id, r.chat.id, f);
1734
+ return d.track("agent-rate-delete", { type: "text" }), w.deleteRating(u.id, r.chat.id, f);
1704
1735
  },
1705
1736
  async speak(f) {
1706
- var P, E, J;
1737
+ var P, T, J;
1707
1738
  function y() {
1708
1739
  if (typeof f == "string") {
1709
1740
  if (!u.presenter.voice)
@@ -1727,13 +1758,13 @@ async function gn(e, t) {
1727
1758
  }
1728
1759
  return f;
1729
1760
  }
1730
- const k = y();
1731
- if (d.track("agent-speak", k), ee.update(), r.messages && k.type === "text" && (r.messages.push({
1761
+ const E = y();
1762
+ if (d.track("agent-speak", E), ne.update(), r.messages && E.type === "text" && (r.messages.push({
1732
1763
  id: le(),
1733
1764
  role: "assistant",
1734
- content: k.input,
1765
+ content: E.input,
1735
1766
  created_at: (/* @__PURE__ */ new Date()).toISOString()
1736
- }), (E = (P = t.callbacks).onNewMessage) == null || E.call(P, [...r.messages], "answer")), mt(r.chatMode))
1767
+ }), (T = (P = t.callbacks).onNewMessage) == null || T.call(P, [...r.messages], "answer")), yt(r.chatMode))
1737
1768
  return {
1738
1769
  duration: 0,
1739
1770
  video_id: "",
@@ -1742,37 +1773,37 @@ async function gn(e, t) {
1742
1773
  if (!r.streamingManager)
1743
1774
  throw new Error("Please connect to the agent first");
1744
1775
  return r.streamingManager.speak({
1745
- script: k,
1776
+ script: E,
1746
1777
  metadata: { chat_id: (J = r.chat) == null ? void 0 : J.id, agent_id: u.id }
1747
1778
  });
1748
1779
  },
1749
- interrupt: B
1780
+ interrupt: R
1750
1781
  };
1751
1782
  }
1752
1783
  export {
1753
- W as AgentActivityState,
1754
- nt as AgentStatus,
1755
- Ye as ChatCreationFailed,
1756
- F as ChatMode,
1757
- Ge as ChatModeDowngraded,
1758
- X as ChatProgress,
1759
- R as ConnectionState,
1760
- ie as ConnectivityState,
1761
- ot as DocumentType,
1762
- at as KnowledgeType,
1763
- tt as PlanGroup,
1764
- st as Providers,
1765
- rt as RateState,
1766
- j as StreamEvents,
1767
- ae as StreamType,
1768
- S as StreamingState,
1769
- it as Subject,
1770
- ye as TransportProvider,
1771
- et as UserPlan,
1784
+ H as AgentActivityState,
1785
+ ot as AgentStatus,
1786
+ tt as ChatCreationFailed,
1787
+ B as ChatMode,
1788
+ nt as ChatModeDowngraded,
1789
+ Z as ChatProgress,
1790
+ M as ConnectionState,
1791
+ re as ConnectivityState,
1792
+ ut as DocumentType,
1793
+ dt as KnowledgeType,
1794
+ at as PlanGroup,
1795
+ lt as Providers,
1796
+ st as RateState,
1797
+ b as StreamEvents,
1798
+ ie as StreamType,
1799
+ D as StreamingState,
1800
+ ct as Subject,
1801
+ Se as TransportProvider,
1802
+ it as UserPlan,
1772
1803
  ue as ValidationError,
1773
- Ke as VideoType,
1774
- ct as VoiceAccess,
1775
- Ze as WsError,
1776
- gn as createAgentManager,
1777
- mn as mapVideoType
1804
+ Ve as VideoType,
1805
+ ft as VoiceAccess,
1806
+ rt as WsError,
1807
+ wn as createAgentManager,
1808
+ pn as mapVideoType
1778
1809
  };