@d-id/client-sdk 1.1.51 → 1.1.52

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