@d-id/client-sdk 1.1.52-staging.233 → 1.1.52-staging.236

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