@d-id/client-sdk 1.1.23-staging.109 → 1.1.23-staging.110

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