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

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