@d-id/client-sdk 1.1.47 → 1.1.48-staging.214

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