@d-id/client-sdk 1.1.52-staging.237 → 1.1.52-staging.240

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