@d-id/client-sdk 1.1.27 → 1.1.28-staging.129

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