@d-id/client-sdk 1.1.51 → 1.1.52-staging.234

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