@d-id/client-sdk 1.1.32 → 1.1.33-staging.151

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