@d-id/client-sdk 1.1.14 → 1.1.15-staging.63

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.
@@ -0,0 +1,1337 @@
1
+ var Se = Object.defineProperty;
2
+ var Ie = (e, t, n) => t in e ? Se(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var Z = (e, t, n) => Ie(e, typeof t != "symbol" ? t + "" : t, n);
4
+ class te extends Error {
5
+ constructor({ kind: n, description: r, error: i }) {
6
+ super(JSON.stringify({ kind: n, description: r }));
7
+ Z(this, "kind");
8
+ Z(this, "description");
9
+ Z(this, "error");
10
+ this.kind = n, this.description = r, this.error = i;
11
+ }
12
+ }
13
+ class Ee extends te {
14
+ constructor(t, n) {
15
+ super({
16
+ kind: "ChatCreationFailed",
17
+ description: `Failed to create ${n ? "persistent" : ""} chat, mode: ${t}`
18
+ });
19
+ }
20
+ }
21
+ class Me extends te {
22
+ constructor(t) {
23
+ super({ kind: "ChatModeDowngraded", description: `Chat mode downgraded to ${t}` });
24
+ }
25
+ }
26
+ class H extends te {
27
+ constructor(n, r) {
28
+ super({ kind: "ValidationError", description: n });
29
+ Z(this, "key");
30
+ this.key = r;
31
+ }
32
+ }
33
+ class _e extends te {
34
+ constructor(t) {
35
+ super({ kind: "WSError", description: t });
36
+ }
37
+ }
38
+ var Te = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(Te || {}), Ae = /* @__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))(Ae || {}), je = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(je || {}), Pe = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(Pe || {}), E = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e.Off = "Off", e))(E || {}), V = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(V || {}), be = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(be || {}), Le = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(Le || {}), Be = /* @__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))(Be || {}), me = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e.Expressive = "expressive", e))(me || {});
39
+ const bt = (e) => {
40
+ switch (e) {
41
+ case "clip":
42
+ return "clip";
43
+ case "talk":
44
+ return "talk";
45
+ default:
46
+ throw new Error(`Unknown video type: ${e}`);
47
+ }
48
+ };
49
+ var w = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(w || {}), G = /* @__PURE__ */ ((e) => (e.Strong = "STRONG", e.Weak = "WEAK", e.Unknown = "UNKNOWN", e))(G || {}), O = /* @__PURE__ */ ((e) => (e.Idle = "IDLE", e.Talking = "TALKING", e))(O || {}), L = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", 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))(L || {}), R = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(R || {}), U = /* @__PURE__ */ ((e) => (e.Legacy = "legacy", e.Fluent = "fluent", e))(U || {}), ce = /* @__PURE__ */ ((e) => (e.Livekit = "livekit", e))(ce || {}), $e = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))($e || {}), xe = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(xe || {});
50
+ const ze = 45 * 1e3, Fe = "X-Playground-Chat", ee = "https://api.d-id.com", Ne = "wss://notifications.d-id.com", Je = "79f81a83a67430be2bc0fd61042b8faa", ge = (e) => new Promise((t) => setTimeout(t, e)), q = (e = 16) => {
51
+ const t = new Uint8Array(e);
52
+ return window.crypto.getRandomValues(t), Array.from(t, (n) => n.toString(16).padStart(2, "0")).join("").slice(0, 13);
53
+ }, We = (e) => [E.TextOnly, E.Playground, E.Maintenance].includes(e), he = (e) => e && [E.DirectPlayback, E.Off].includes(e);
54
+ function Ue(e, t) {
55
+ let n;
56
+ return {
57
+ promise: new Promise((i, s) => {
58
+ n = setTimeout(() => s(new Error(t)), e);
59
+ }),
60
+ clear: () => clearTimeout(n)
61
+ };
62
+ }
63
+ async function ne(e, t) {
64
+ const n = {
65
+ limit: (t == null ? void 0 : t.limit) ?? 3,
66
+ delayMs: (t == null ? void 0 : t.delayMs) ?? 0,
67
+ timeout: (t == null ? void 0 : t.timeout) ?? 3e4,
68
+ timeoutErrorMessage: (t == null ? void 0 : t.timeoutErrorMessage) || "Timeout error",
69
+ shouldRetryFn: (t == null ? void 0 : t.shouldRetryFn) ?? (() => !0),
70
+ onRetry: (t == null ? void 0 : t.onRetry) ?? (() => {
71
+ })
72
+ };
73
+ let r;
74
+ for (let i = 1; i <= n.limit; i++)
75
+ try {
76
+ if (!n.timeout)
77
+ return await e();
78
+ const { promise: s, clear: c } = Ue(n.timeout, n.timeoutErrorMessage), a = e().finally(c);
79
+ return await Promise.race([a, s]);
80
+ } catch (s) {
81
+ if (r = s, !n.shouldRetryFn(s) || i >= n.limit)
82
+ throw s;
83
+ await ge(n.delayMs), n.onRetry(s);
84
+ }
85
+ throw r;
86
+ }
87
+ function we() {
88
+ let e = window.localStorage.getItem("did_external_key_id");
89
+ if (!e) {
90
+ let t = q();
91
+ window.localStorage.setItem("did_external_key_id", t), e = t;
92
+ }
93
+ return e;
94
+ }
95
+ let Ke = q();
96
+ function pe(e) {
97
+ if (e.type === "bearer")
98
+ return `Bearer ${e.token}`;
99
+ if (e.type === "basic")
100
+ return `Basic ${btoa(`${e.username}:${e.password}`)}`;
101
+ if (e.type === "key")
102
+ return `Client-Key ${e.clientKey}.${we()}_${Ke}`;
103
+ throw new Error(`Unknown auth type: ${e}`);
104
+ }
105
+ const He = (e) => ne(e, {
106
+ limit: 3,
107
+ delayMs: 1e3,
108
+ timeout: 0,
109
+ shouldRetryFn: (t) => t.status === 429
110
+ });
111
+ function ve(e, t = ee, n) {
112
+ const r = async (i, s) => {
113
+ const { skipErrorHandler: c, ...a } = s || {}, o = await He(
114
+ () => fetch(t + (i != null && i.startsWith("/") ? i : `/${i}`), {
115
+ ...a,
116
+ headers: {
117
+ ...a.headers,
118
+ Authorization: pe(e),
119
+ "Content-Type": "application/json"
120
+ }
121
+ })
122
+ );
123
+ if (!o.ok) {
124
+ let d = await o.text().catch(() => `Failed to fetch with status ${o.status}`);
125
+ const l = new Error(d);
126
+ throw n && !c && n(l, { url: i, options: a, headers: o.headers }), l;
127
+ }
128
+ return o.json();
129
+ };
130
+ return {
131
+ get(i, s) {
132
+ return r(i, { ...s, method: "GET" });
133
+ },
134
+ post(i, s, c) {
135
+ return r(i, { ...c, body: JSON.stringify(s), method: "POST" });
136
+ },
137
+ delete(i, s, c) {
138
+ return r(i, { ...c, body: JSON.stringify(s), method: "DELETE" });
139
+ },
140
+ patch(i, s, c) {
141
+ return r(i, { ...c, body: JSON.stringify(s), method: "PATCH" });
142
+ }
143
+ };
144
+ }
145
+ function ye(e, t = ee, n) {
146
+ const r = ve(e, `${t}/agents`, n);
147
+ return {
148
+ create(i, s) {
149
+ return r.post("/", i, s);
150
+ },
151
+ getAgents(i, s) {
152
+ return r.get(`/${i ? `?tag=${i}` : ""}`, s).then((c) => c ?? []);
153
+ },
154
+ getById(i, s) {
155
+ return r.get(`/${i}`, s);
156
+ },
157
+ delete(i, s) {
158
+ return r.delete(`/${i}`, void 0, s);
159
+ },
160
+ update(i, s, c) {
161
+ return r.patch(`/${i}`, s, c);
162
+ },
163
+ newChat(i, s, c) {
164
+ return r.post(`/${i}/chat`, s, c);
165
+ },
166
+ chat(i, s, c, a) {
167
+ return r.post(`/${i}/chat/${s}`, c, a);
168
+ },
169
+ createRating(i, s, c, a) {
170
+ return r.post(`/${i}/chat/${s}/ratings`, c, a);
171
+ },
172
+ updateRating(i, s, c, a, o) {
173
+ return r.patch(`/${i}/chat/${s}/ratings/${c}`, a, o);
174
+ },
175
+ deleteRating(i, s, c, a) {
176
+ return r.delete(`/${i}/chat/${s}/ratings/${c}`, a);
177
+ },
178
+ getSTTToken(i, s) {
179
+ return r.get(`/${i}/stt-token`, s);
180
+ }
181
+ };
182
+ }
183
+ const ke = (e) => e.type === "clip" && e.presenter_id.startsWith("v2_") ? "clip_v2" : e.type, De = (e) => e === me.Expressive;
184
+ function Ve(e) {
185
+ var i, s, c, a;
186
+ const t = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop", n = () => {
187
+ const o = navigator.platform;
188
+ return o.toLowerCase().includes("win") ? "Windows" : o.toLowerCase().includes("mac") ? "Mac OS X" : o.toLowerCase().includes("linux") ? "Linux" : "Unknown";
189
+ }, r = e.presenter;
190
+ return {
191
+ $os: `${n()}`,
192
+ isMobile: `${t() == "Mobile"}`,
193
+ browser: navigator.userAgent,
194
+ origin: window.location.origin,
195
+ agentType: ke(r),
196
+ agentVoice: {
197
+ voiceId: (s = (i = e.presenter) == null ? void 0 : i.voice) == null ? void 0 : s.voice_id,
198
+ provider: (a = (c = e.presenter) == null ? void 0 : c.voice) == null ? void 0 : a.type
199
+ }
200
+ };
201
+ }
202
+ function qe(e) {
203
+ var n, r, i, s, c, a;
204
+ const t = (n = e.llm) == null ? void 0 : n.prompt_customization;
205
+ return {
206
+ agentType: ke(e.presenter),
207
+ owner_id: e.owner_id ?? "",
208
+ promptVersion: (r = e.llm) == null ? void 0 : r.prompt_version,
209
+ behavior: {
210
+ role: t == null ? void 0 : t.role,
211
+ personality: t == null ? void 0 : t.personality,
212
+ instructions: (i = e.llm) == null ? void 0 : i.instructions
213
+ },
214
+ temperature: (s = e.llm) == null ? void 0 : s.temperature,
215
+ knowledgeSource: t == null ? void 0 : t.knowledge_source,
216
+ starterQuestionsCount: (a = (c = e.knowledge) == null ? void 0 : c.starter_message) == null ? void 0 : a.length,
217
+ topicsToAvoid: t == null ? void 0 : t.topics_to_avoid,
218
+ maxResponseLength: t == null ? void 0 : t.max_response_length,
219
+ agentId: e.id,
220
+ access: e.access,
221
+ name: e.preview_name,
222
+ ...e.access === "public" ? { from: "agent-template" } : {}
223
+ };
224
+ }
225
+ const Oe = (e) => e.reduce((t, n) => t + n, 0), de = (e) => Oe(e) / e.length;
226
+ function Xe(e, t, n) {
227
+ var o, d, l;
228
+ const { event: r, ...i } = e, { template: s } = (t == null ? void 0 : t.llm) || {}, { language: c } = ((o = t == null ? void 0 : t.presenter) == null ? void 0 : o.voice) || {};
229
+ return {
230
+ ...i,
231
+ llm: { ...i.llm, template: s },
232
+ script: { ...i.script, provider: { ...(d = i == null ? void 0 : i.script) == null ? void 0 : d.provider, language: c } },
233
+ stitch: (t == null ? void 0 : t.presenter.type) === "talk" ? (l = t == null ? void 0 : t.presenter) == null ? void 0 : l.stitch : void 0,
234
+ ...n
235
+ };
236
+ }
237
+ let re = {};
238
+ const Ye = "https://api-js.mixpanel.com/track/?verbose=1&ip=1";
239
+ function Qe(e) {
240
+ const t = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk";
241
+ return {
242
+ token: e.token || "testKey",
243
+ distinct_id: e.distinctId || we(),
244
+ agentId: e.agentId,
245
+ additionalProperties: {
246
+ id: e.distinctId,
247
+ ...e.mixpanelAdditionalProperties || {}
248
+ },
249
+ isEnabled: e.isEnabled ?? !0,
250
+ getRandom: q,
251
+ enrich(n) {
252
+ this.additionalProperties = { ...this.additionalProperties, ...n };
253
+ },
254
+ async track(n, r) {
255
+ if (!this.isEnabled)
256
+ return Promise.resolve();
257
+ const { audioPath: i, ...s } = r || {}, c = {
258
+ method: "POST",
259
+ headers: {
260
+ "Content-Type": "application/x-www-form-urlencoded"
261
+ },
262
+ body: new URLSearchParams({
263
+ data: JSON.stringify([
264
+ {
265
+ event: n,
266
+ properties: {
267
+ ...this.additionalProperties,
268
+ ...s,
269
+ agentId: this.agentId,
270
+ source: t,
271
+ token: this.token,
272
+ time: Date.now(),
273
+ $insert_id: this.getRandom(),
274
+ origin: window.location.href,
275
+ "Screen Height": window.screen.height || window.innerWidth,
276
+ "Screen Width": window.screen.width || window.innerHeight,
277
+ "User Agent": navigator.userAgent
278
+ }
279
+ }
280
+ ])
281
+ })
282
+ };
283
+ try {
284
+ return await fetch(Ye, c).then((a) => a.json());
285
+ } catch (a) {
286
+ return console.error(a);
287
+ }
288
+ },
289
+ linkTrack(n, r, i, s) {
290
+ re[n] || (re[n] = { events: {}, resolvedDependencies: [] }), s.includes(i) || s.push(i);
291
+ const c = re[n];
292
+ if (c.events[i] = { props: r }, c.resolvedDependencies.push(i), s.every(
293
+ (o) => c.resolvedDependencies.includes(o)
294
+ )) {
295
+ const o = s.reduce((d, l) => c.events[l] ? { ...d, ...c.events[l].props } : d, {});
296
+ this.track(n, o), c.resolvedDependencies = c.resolvedDependencies.filter(
297
+ (d) => !s.includes(d)
298
+ ), s.forEach((d) => {
299
+ delete c.events[d];
300
+ });
301
+ }
302
+ }
303
+ };
304
+ }
305
+ function Ce() {
306
+ let e = 0;
307
+ return {
308
+ reset: () => e = 0,
309
+ update: () => e = Date.now(),
310
+ get: (t = !1) => t ? Date.now() - e : e
311
+ };
312
+ }
313
+ const N = Ce(), ae = Ce();
314
+ function Re(e) {
315
+ return e === E.Playground ? { headers: { [Fe]: "true" } } : {};
316
+ }
317
+ async function ie(e, t, n, r, i = !1, s) {
318
+ try {
319
+ return !s && !he(r) && (s = await t.newChat(e.id, { persist: i }, Re(r)), n.track("agent-chat", {
320
+ event: "created",
321
+ chatId: s.id,
322
+ mode: r
323
+ })), { chat: s, chatMode: (s == null ? void 0 : s.chat_mode) ?? r };
324
+ } catch (c) {
325
+ try {
326
+ const a = JSON.parse(c.message);
327
+ if ((a == null ? void 0 : a.kind) === "InsufficientCreditsError")
328
+ throw new Error("InsufficientCreditsError");
329
+ } catch (a) {
330
+ console.error("Error parsing the error message:", a);
331
+ }
332
+ throw new Error("Cannot create new chat");
333
+ }
334
+ }
335
+ function Ze(e) {
336
+ return e && e.length > 0 ? e : [];
337
+ }
338
+ function Ge(e, t, n) {
339
+ if (!e)
340
+ throw new Error("Please connect to the agent first");
341
+ if (!e.interruptAvailable)
342
+ throw new Error("Interrupt is not enabled for this stream");
343
+ if (t !== U.Fluent)
344
+ throw new Error("Interrupt only available for Fluent streams");
345
+ if (!n)
346
+ throw new Error("No active video to interrupt");
347
+ }
348
+ async function et(e, t) {
349
+ const n = {
350
+ type: L.StreamInterrupt,
351
+ videoId: t,
352
+ timestamp: Date.now()
353
+ };
354
+ e.sendDataChannelMessage(JSON.stringify(n));
355
+ }
356
+ function tt(e) {
357
+ return new Promise((t, n) => {
358
+ const { callbacks: r, host: i, auth: s } = e, { onMessage: c = null, onOpen: a = null, onClose: o = null, onError: d = null } = r || {}, l = new WebSocket(`${i}?authorization=${pe(s)}`);
359
+ l.onmessage = c, l.onclose = o, l.onerror = (v) => {
360
+ console.error(v), d == null || d("Websocket failed to connect", v), n(v);
361
+ }, l.onopen = (v) => {
362
+ a == null || a(v), t(l);
363
+ };
364
+ });
365
+ }
366
+ async function rt(e) {
367
+ const { retries: t = 1 } = e;
368
+ let n = null;
369
+ for (let r = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; r++)
370
+ try {
371
+ n = await tt(e);
372
+ } catch (i) {
373
+ if (r === t)
374
+ throw i;
375
+ await ge(r * 500);
376
+ }
377
+ return n;
378
+ }
379
+ async function nt(e, t, n) {
380
+ const r = n != null && n.onMessage ? [n.onMessage] : [], i = await rt({
381
+ auth: e,
382
+ host: t,
383
+ callbacks: {
384
+ onError: (s) => {
385
+ var c;
386
+ return (c = n.onError) == null ? void 0 : c.call(n, new _e(s));
387
+ },
388
+ onMessage(s) {
389
+ const c = JSON.parse(s.data);
390
+ r.forEach((a) => a(c.event, c));
391
+ }
392
+ }
393
+ });
394
+ return {
395
+ socket: i,
396
+ disconnect: () => i.close(),
397
+ subscribeToEvents: (s) => r.push(s)
398
+ };
399
+ }
400
+ function at(e) {
401
+ if (e.answer !== void 0)
402
+ return e.answer;
403
+ let t = 0, n = "";
404
+ for (; t in e; )
405
+ n += e[t++];
406
+ return n;
407
+ }
408
+ function it(e, t, n, r, i) {
409
+ const s = r.messages[r.messages.length - 1];
410
+ if (!(e === V.Partial || e === V.Answer) || (s == null ? void 0 : s.role) !== "assistant")
411
+ return;
412
+ const { content: c, sequence: a } = t;
413
+ e === V.Partial ? n[a] = c : n.answer = c;
414
+ const o = at(n);
415
+ (s.content !== o || e === V.Answer) && (s.content = o, i == null || i([...r.messages], e));
416
+ }
417
+ function st(e, t, n, r, i) {
418
+ let s = {};
419
+ return {
420
+ clearQueue: () => s = {},
421
+ onMessage: (c, a) => {
422
+ var o, d;
423
+ if ("content" in a)
424
+ it(c, a, s, t, n.callbacks.onNewMessage), c === V.Answer && e.track("agent-message-received", {
425
+ messages: t.messages.length,
426
+ mode: t.chatMode
427
+ });
428
+ else {
429
+ const l = L, v = [l.StreamVideoDone, l.StreamVideoError, l.StreamVideoRejected], M = [l.StreamFailed, l.StreamVideoError, l.StreamVideoRejected], A = Xe(a, r, { mode: t.chatMode });
430
+ if (c = c, c === l.StreamVideoCreated)
431
+ e.linkTrack("agent-video", A, l.StreamVideoCreated, ["start"]);
432
+ else if (v.includes(c)) {
433
+ const b = c.split("/")[1];
434
+ M.includes(c) ? e.track("agent-video", { ...A, event: b }) : e.linkTrack("agent-video", { ...A, event: b }, c, ["done"]);
435
+ }
436
+ M.includes(c) && ((d = (o = n.callbacks).onError) == null || d.call(o, new Error(`Stream failed with event ${c}`), { data: a })), a.event === l.StreamDone && i();
437
+ }
438
+ }
439
+ };
440
+ }
441
+ function ot(e, t, n, r) {
442
+ const i = ve(e, `${t}/agents/${n}`, r);
443
+ return {
444
+ createStream(s) {
445
+ return i.post("/streams", s);
446
+ },
447
+ startConnection(s, c, a) {
448
+ return i.post(`/streams/${s}/sdp`, {
449
+ session_id: a,
450
+ answer: c
451
+ });
452
+ },
453
+ addIceCandidate(s, c, a) {
454
+ return i.post(`/streams/${s}/ice`, {
455
+ session_id: a,
456
+ ...c
457
+ });
458
+ },
459
+ sendStreamRequest(s, c, a) {
460
+ return i.post(`/streams/${s}`, {
461
+ session_id: c,
462
+ ...a
463
+ });
464
+ },
465
+ close(s, c) {
466
+ return i.delete(`/streams/${s}`, { session_id: c });
467
+ }
468
+ };
469
+ }
470
+ const ct = (e, t) => (n, r) => e && console.log(`[${t}] ${n}`, r ?? "");
471
+ function dt(e, t, n) {
472
+ const r = (t.timestamp - e.timestamp) / 1e3;
473
+ return {
474
+ duration: r,
475
+ bytesReceived: t.bytesReceived - e.bytesReceived,
476
+ bitrate: Math.round((t.bytesReceived - e.bytesReceived) * 8 / r),
477
+ packetsReceived: t.packetsReceived - e.packetsReceived,
478
+ packetsLost: t.packetsLost - e.packetsLost,
479
+ framesDropped: t.framesDropped - e.framesDropped,
480
+ framesDecoded: t.framesDecoded - e.framesDecoded,
481
+ jitter: t.jitter,
482
+ avgJitterDelayInInterval: (t.jitterBufferDelay - e.jitterBufferDelay) / (t.jitterBufferEmittedCount - e.jitterBufferEmittedCount),
483
+ jitterBufferEmittedCount: t.jitterBufferEmittedCount - e.jitterBufferEmittedCount,
484
+ jitterBufferDelay: (t.jitterBufferDelay - e.jitterBufferDelay) / r,
485
+ framesPerSecond: t.framesPerSecond,
486
+ freezeCount: t.freezeCount - e.freezeCount,
487
+ freezeDuration: t.freezeDuration - e.freezeDuration,
488
+ lowFpsCount: n
489
+ };
490
+ }
491
+ function lt(e) {
492
+ return e.filter(
493
+ (t) => t.freezeCount > 0 || t.framesPerSecond < 21 || t.framesDropped > 0 || t.packetsLost > 0
494
+ ).map((t) => {
495
+ const { timestamp: n, ...r } = t, i = [];
496
+ return t.freezeCount > 0 && i.push("freeze"), t.framesPerSecond < 21 && i.push("low fps"), t.framesDropped > 0 && i.push("frames dropped"), t.packetsLost > 0 && i.push("packet loss"), {
497
+ ...r,
498
+ causes: i
499
+ };
500
+ });
501
+ }
502
+ function ut(e) {
503
+ let t = "", n = 0;
504
+ for (const r of e.values())
505
+ if (r && r.type === "codec" && r.mimeType.startsWith("video") && (t = r.mimeType.split("/")[1]), r && r.type === "candidate-pair" && (n = r.currentRoundTripTime), r && r.type === "inbound-rtp" && r.kind === "video")
506
+ return {
507
+ codec: t,
508
+ rtt: n,
509
+ timestamp: r.timestamp,
510
+ bytesReceived: r.bytesReceived,
511
+ packetsReceived: r.packetsReceived,
512
+ packetsLost: r.packetsLost,
513
+ framesDropped: r.framesDropped,
514
+ framesDecoded: r.framesDecoded,
515
+ jitter: r.jitter,
516
+ jitterBufferDelay: r.jitterBufferDelay,
517
+ jitterBufferEmittedCount: r.jitterBufferEmittedCount,
518
+ avgJitterDelayInInterval: r.jitterBufferDelay / r.jitterBufferEmittedCount,
519
+ frameWidth: r.frameWidth,
520
+ frameHeight: r.frameHeight,
521
+ framesPerSecond: r.framesPerSecond,
522
+ freezeCount: r.freezeCount,
523
+ freezeDuration: r.totalFreezesDuration
524
+ };
525
+ return {};
526
+ }
527
+ function ft(e, t, n) {
528
+ const r = e.map((o, d) => d === 0 ? n ? {
529
+ timestamp: o.timestamp,
530
+ duration: 0,
531
+ rtt: o.rtt,
532
+ bytesReceived: o.bytesReceived - n.bytesReceived,
533
+ bitrate: (o.bytesReceived - n.bytesReceived) * 8 / (t / 1e3),
534
+ packetsReceived: o.packetsReceived - n.packetsReceived,
535
+ packetsLost: o.packetsLost - n.packetsLost,
536
+ framesDropped: o.framesDropped - n.framesDropped,
537
+ framesDecoded: o.framesDecoded - n.framesDecoded,
538
+ jitter: o.jitter,
539
+ jitterBufferDelay: o.jitterBufferDelay - n.jitterBufferDelay,
540
+ jitterBufferEmittedCount: o.jitterBufferEmittedCount - n.jitterBufferEmittedCount,
541
+ avgJitterDelayInInterval: (o.jitterBufferDelay - n.jitterBufferDelay) / (o.jitterBufferEmittedCount - n.jitterBufferEmittedCount),
542
+ framesPerSecond: o.framesPerSecond,
543
+ freezeCount: o.freezeCount - n.freezeCount,
544
+ freezeDuration: o.freezeDuration - n.freezeDuration
545
+ } : {
546
+ timestamp: o.timestamp,
547
+ rtt: o.rtt,
548
+ duration: 0,
549
+ bytesReceived: o.bytesReceived,
550
+ bitrate: o.bytesReceived * 8 / (t / 1e3),
551
+ packetsReceived: o.packetsReceived,
552
+ packetsLost: o.packetsLost,
553
+ framesDropped: o.framesDropped,
554
+ framesDecoded: o.framesDecoded,
555
+ jitter: o.jitter,
556
+ jitterBufferDelay: o.jitterBufferDelay,
557
+ jitterBufferEmittedCount: o.jitterBufferEmittedCount,
558
+ avgJitterDelayInInterval: o.jitterBufferDelay / o.jitterBufferEmittedCount,
559
+ framesPerSecond: o.framesPerSecond,
560
+ freezeCount: o.freezeCount,
561
+ freezeDuration: o.freezeDuration
562
+ } : {
563
+ timestamp: o.timestamp,
564
+ duration: t * d / 1e3,
565
+ rtt: o.rtt,
566
+ bytesReceived: o.bytesReceived - e[d - 1].bytesReceived,
567
+ bitrate: (o.bytesReceived - e[d - 1].bytesReceived) * 8 / (t / 1e3),
568
+ packetsReceived: o.packetsReceived - e[d - 1].packetsReceived,
569
+ packetsLost: o.packetsLost - e[d - 1].packetsLost,
570
+ framesDropped: o.framesDropped - e[d - 1].framesDropped,
571
+ framesDecoded: o.framesDecoded - e[d - 1].framesDecoded,
572
+ jitter: o.jitter,
573
+ jitterBufferDelay: o.jitterBufferDelay - e[d - 1].jitterBufferDelay,
574
+ jitterBufferEmittedCount: o.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount,
575
+ avgJitterDelayInInterval: (o.jitterBufferDelay - e[d - 1].jitterBufferDelay) / (o.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount),
576
+ framesPerSecond: o.framesPerSecond,
577
+ freezeCount: o.freezeCount - e[d - 1].freezeCount,
578
+ freezeDuration: o.freezeDuration - e[d - 1].freezeDuration
579
+ }), i = lt(r), s = i.reduce((o, d) => o + (d.causes.includes("low fps") ? 1 : 0), 0), c = r.filter((o) => !!o.avgJitterDelayInInterval).map((o) => o.avgJitterDelayInInterval), a = r.filter((o) => !!o.rtt).map((o) => o.rtt);
580
+ return {
581
+ webRTCStats: {
582
+ anomalies: i,
583
+ minRtt: Math.min(...a),
584
+ avgRtt: de(a),
585
+ maxRtt: Math.max(...a),
586
+ aggregateReport: dt(e[0], e[e.length - 1], s),
587
+ minJitterDelayInInterval: Math.min(...c),
588
+ maxJitterDelayInInterval: Math.max(...c),
589
+ avgJitterDelayInInterval: de(c)
590
+ },
591
+ codec: e[0].codec,
592
+ resolution: `${e[0].frameWidth}x${e[0].frameHeight}`
593
+ };
594
+ }
595
+ const se = 100, mt = Math.max(Math.ceil(400 / se), 1), gt = 0.25, ht = 0.28;
596
+ function wt() {
597
+ let e = 0, t, n, r = 0;
598
+ return (i) => {
599
+ for (const s of i.values())
600
+ if (s && s.type === "inbound-rtp" && s.kind === "video") {
601
+ const c = s.jitterBufferDelay, a = s.jitterBufferEmittedCount;
602
+ if (n && a > n) {
603
+ const l = c - t, v = a - n;
604
+ r = l / v;
605
+ }
606
+ t = c, n = a;
607
+ const o = s.framesDecoded, d = o - e > 0;
608
+ return e = o, { isReceiving: d, avgJitterDelayInInterval: r, freezeCount: s.freezeCount };
609
+ }
610
+ return { isReceiving: !1, avgJitterDelayInInterval: r };
611
+ };
612
+ }
613
+ function pt(e, t, n, r, i, s = !1) {
614
+ let c = [], a, o = 0, d = !1, l = G.Unknown, v = G.Unknown, M = 0, A = 0;
615
+ const b = wt();
616
+ return setInterval(async () => {
617
+ const j = await e.getStats(), { isReceiving: B, avgJitterDelayInInterval: z, freezeCount: y } = b(j), K = ut(j);
618
+ if (B)
619
+ o = 0, M = y - A, v = z < gt ? G.Strong : z > ht && M > 1 ? G.Weak : l, v !== l && (i == null || i(v), l = v, A += M, M = 0), d || (r == null || r(w.Start), a = c[c.length - 1], c = [], d = !0), c.push(K);
620
+ else if (d && (o++, o >= mt)) {
621
+ const u = ft(c, se, a);
622
+ r == null || r(w.Stop, u), t() || n(), A = y, d = !1;
623
+ }
624
+ }, se);
625
+ }
626
+ const vt = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
627
+ function le(e) {
628
+ switch (e) {
629
+ case "connected":
630
+ return R.Connected;
631
+ case "checking":
632
+ return R.Connecting;
633
+ case "failed":
634
+ return R.Fail;
635
+ case "new":
636
+ return R.New;
637
+ case "closed":
638
+ return R.Closed;
639
+ case "disconnected":
640
+ return R.Disconnected;
641
+ case "completed":
642
+ return R.Completed;
643
+ default:
644
+ return R.New;
645
+ }
646
+ }
647
+ const yt = (e) => (t) => {
648
+ const [n, r = ""] = t.split(/:(.+)/);
649
+ try {
650
+ const i = JSON.parse(r);
651
+ return e("parsed data channel message", { subject: n, data: i }), { subject: n, data: i };
652
+ } catch (i) {
653
+ return e("Failed to parse data channel message, returning data as string", { subject: n, rawData: r, error: i }), { subject: n, data: r };
654
+ }
655
+ };
656
+ function kt({
657
+ statsSignal: e,
658
+ dataChannelSignal: t,
659
+ onVideoStateChange: n,
660
+ report: r
661
+ }) {
662
+ e === w.Start && t === w.Start ? n == null || n(w.Start) : e === w.Stop && t === w.Stop && (n == null || n(w.Stop, r));
663
+ }
664
+ function Dt({
665
+ statsSignal: e,
666
+ dataChannelSignal: t,
667
+ onVideoStateChange: n,
668
+ onAgentActivityStateChange: r,
669
+ report: i
670
+ }) {
671
+ e === w.Start ? n == null || n(w.Start) : e === w.Stop && (n == null || n(w.Stop, i)), t === w.Start ? r == null || r(O.Talking) : t === w.Stop && (r == null || r(O.Idle));
672
+ }
673
+ function ue({
674
+ statsSignal: e,
675
+ dataChannelSignal: t,
676
+ onVideoStateChange: n,
677
+ onAgentActivityStateChange: r,
678
+ streamType: i,
679
+ report: s
680
+ }) {
681
+ i === U.Legacy ? kt({ statsSignal: e, dataChannelSignal: t, onVideoStateChange: n, report: s }) : i === U.Fluent && Dt({
682
+ statsSignal: e,
683
+ dataChannelSignal: t,
684
+ onVideoStateChange: n,
685
+ onAgentActivityStateChange: r,
686
+ report: s
687
+ });
688
+ }
689
+ async function Ct(e, t, { debug: n = !1, callbacks: r, auth: i, baseURL: s = ee, analytics: c }) {
690
+ var Y;
691
+ const a = ct(n, "WebRTCStreamingManager"), o = yt(a);
692
+ let d = !1, l = !1, v = w.Stop, M = w.Stop;
693
+ const { startConnection: A, sendStreamRequest: b, close: j, createStream: B, addIceCandidate: z } = ot(
694
+ i,
695
+ s,
696
+ e,
697
+ r.onError
698
+ ), {
699
+ id: y,
700
+ offer: K,
701
+ ice_servers: u,
702
+ session_id: h,
703
+ fluent: p,
704
+ interrupt_enabled: _,
705
+ triggers_enabled: k
706
+ } = await B(t);
707
+ (Y = r.onStreamCreated) == null || Y.call(r, { stream_id: y, session_id: h, agent_id: e });
708
+ const f = new vt({ iceServers: u }), S = f.createDataChannel("JanusDataChannel");
709
+ if (!h)
710
+ throw new Error("Could not create session_id");
711
+ const T = p ? U.Fluent : U.Legacy;
712
+ c.enrich({
713
+ "stream-type": T
714
+ });
715
+ const $ = t.stream_warmup && !p, C = () => d, I = () => {
716
+ var m;
717
+ d = !0, l && ((m = r.onConnectionStateChange) == null || m.call(r, R.Connected));
718
+ }, D = pt(
719
+ f,
720
+ C,
721
+ I,
722
+ (m, g) => ue({
723
+ statsSignal: M = m,
724
+ dataChannelSignal: T === U.Legacy ? v : void 0,
725
+ onVideoStateChange: r.onVideoStateChange,
726
+ onAgentActivityStateChange: r.onAgentActivityStateChange,
727
+ report: g,
728
+ streamType: T
729
+ }),
730
+ (m) => {
731
+ var g;
732
+ return (g = r.onConnectivityStateChange) == null ? void 0 : g.call(r, m);
733
+ },
734
+ $
735
+ );
736
+ f.onicecandidate = (m) => {
737
+ var g;
738
+ a("peerConnection.onicecandidate", m);
739
+ try {
740
+ m.candidate && m.candidate.sdpMid && m.candidate.sdpMLineIndex !== null ? z(
741
+ y,
742
+ {
743
+ candidate: m.candidate.candidate,
744
+ sdpMid: m.candidate.sdpMid,
745
+ sdpMLineIndex: m.candidate.sdpMLineIndex
746
+ },
747
+ h
748
+ ) : z(y, { candidate: null }, h);
749
+ } catch (P) {
750
+ (g = r.onError) == null || g.call(r, P, { streamId: y });
751
+ }
752
+ }, S.onopen = () => {
753
+ l = !0, (!$ || d) && I();
754
+ };
755
+ const F = (m) => {
756
+ var g;
757
+ (g = r.onVideoIdChange) == null || g.call(r, m);
758
+ };
759
+ function x(m, g) {
760
+ if (m === L.StreamStarted && typeof g == "object" && "metadata" in g) {
761
+ const P = g.metadata;
762
+ F(P.videoId);
763
+ }
764
+ m === L.StreamDone && F(null), v = m === L.StreamStarted ? w.Start : w.Stop, ue({
765
+ statsSignal: T === U.Legacy ? M : void 0,
766
+ dataChannelSignal: v,
767
+ onVideoStateChange: r.onVideoStateChange,
768
+ onAgentActivityStateChange: r.onAgentActivityStateChange,
769
+ streamType: T
770
+ });
771
+ }
772
+ function J(m, g) {
773
+ const P = typeof g == "string" ? g : g == null ? void 0 : g.metadata;
774
+ P && c.enrich({ streamMetadata: P }), c.track("agent-chat", { event: "ready" });
775
+ }
776
+ const W = {
777
+ [L.StreamStarted]: x,
778
+ [L.StreamDone]: x,
779
+ [L.StreamReady]: J
780
+ };
781
+ S.onmessage = (m) => {
782
+ var Q;
783
+ const { subject: g, data: P } = o(m.data);
784
+ (Q = W[g]) == null || Q.call(W, g, P);
785
+ }, f.oniceconnectionstatechange = () => {
786
+ var g;
787
+ a("peerConnection.oniceconnectionstatechange => " + f.iceConnectionState);
788
+ const m = le(f.iceConnectionState);
789
+ m !== R.Connected && ((g = r.onConnectionStateChange) == null || g.call(r, m));
790
+ }, f.ontrack = (m) => {
791
+ var g;
792
+ a("peerConnection.ontrack", m), (g = r.onSrcObjectReady) == null || g.call(r, m.streams[0]);
793
+ }, await f.setRemoteDescription(K), a("set remote description OK");
794
+ const X = await f.createAnswer();
795
+ return a("create answer OK"), await f.setLocalDescription(X), a("set local description OK"), await A(y, X, h), a("start connection OK"), {
796
+ /**
797
+ * Method to send request to server to get clip or talk depend on you payload
798
+ * @param payload
799
+ */
800
+ speak(m) {
801
+ return b(y, h, m);
802
+ },
803
+ /**
804
+ * Method to close RTC connection
805
+ */
806
+ async disconnect() {
807
+ var m;
808
+ if (y) {
809
+ const g = le(f.iceConnectionState);
810
+ if (f) {
811
+ if (g === R.New) {
812
+ clearInterval(D);
813
+ return;
814
+ }
815
+ f.close(), f.oniceconnectionstatechange = null, f.onnegotiationneeded = null, f.onicecandidate = null, f.ontrack = null;
816
+ }
817
+ try {
818
+ g === R.Connected && await j(y, h).catch((P) => {
819
+ });
820
+ } catch (P) {
821
+ a("Error on close stream connection", P);
822
+ }
823
+ (m = r.onAgentActivityStateChange) == null || m.call(r, O.Idle), clearInterval(D);
824
+ }
825
+ },
826
+ /**
827
+ * Method to send data channel messages to the server
828
+ */
829
+ sendDataChannelMessage(m) {
830
+ var g, P;
831
+ if (!d || S.readyState !== "open") {
832
+ a("Data channel is not ready for sending messages"), (g = r.onError) == null || g.call(r, new Error("Data channel is not ready for sending messages"), {
833
+ streamId: y
834
+ });
835
+ return;
836
+ }
837
+ try {
838
+ S.send(m);
839
+ } catch (Q) {
840
+ a("Error sending data channel message", Q), (P = r.onError) == null || P.call(r, Q, { streamId: y });
841
+ }
842
+ },
843
+ /**
844
+ * Session identifier information, should be returned in the body of all streaming requests
845
+ */
846
+ sessionId: h,
847
+ /**
848
+ * Id of current RTC stream
849
+ */
850
+ streamId: y,
851
+ streamType: T,
852
+ interruptAvailable: _ ?? !1,
853
+ triggersAvailable: k ?? !1
854
+ };
855
+ }
856
+ var oe = /* @__PURE__ */ ((e) => (e.V1 = "v1", e.V2 = "v2", e))(oe || {});
857
+ async function Rt(e, t, n) {
858
+ const r = e.id;
859
+ switch (t.version) {
860
+ case "v1": {
861
+ const { version: i, ...s } = t;
862
+ return Ct(r, s, n);
863
+ }
864
+ case "v2": {
865
+ const { version: i, ...s } = t;
866
+ switch (s.transport_provider) {
867
+ case ce.Livekit:
868
+ const { createLiveKitStreamingManager: c } = await import("./livekit-manager-tsiAM1XU.js");
869
+ return c(r, s, n);
870
+ default:
871
+ throw new Error(`Unsupported transport provider: ${s.transport_provider}`);
872
+ }
873
+ }
874
+ default:
875
+ throw new Error(`Invalid stream version: ${t.version}`);
876
+ }
877
+ }
878
+ function St(e) {
879
+ return {
880
+ transport_provider: ce.Livekit,
881
+ chat_id: e == null ? void 0 : e.chatId
882
+ };
883
+ }
884
+ function It(e) {
885
+ var i, s, c;
886
+ const { streamOptions: t } = e ?? {}, n = e != null && e.distinctId || ((i = e == null ? void 0 : e.mixpanelAdditionalProperties) == null ? void 0 : i.plan) !== void 0 ? {
887
+ ...e != null && e.distinctId ? { distinct_id: e.distinctId } : {},
888
+ ...((s = e == null ? void 0 : e.mixpanelAdditionalProperties) == null ? void 0 : s.plan) !== void 0 ? { plan: (c = e.mixpanelAdditionalProperties) == null ? void 0 : c.plan } : {}
889
+ } : void 0;
890
+ return { ...{
891
+ output_resolution: t == null ? void 0 : t.outputResolution,
892
+ session_timeout: t == null ? void 0 : t.sessionTimeout,
893
+ stream_warmup: t == null ? void 0 : t.streamWarmup,
894
+ compatibility_mode: t == null ? void 0 : t.compatibilityMode,
895
+ fluent: t == null ? void 0 : t.fluent
896
+ }, ...n && { end_user_data: n } };
897
+ }
898
+ function Et(e, t) {
899
+ return De(e.presenter.type) ? { version: oe.V2, ...St(t) } : { version: oe.V1, ...It(t) };
900
+ }
901
+ function Mt(e, t, n, r, i) {
902
+ i === U.Fluent ? _t(e, t, n, r, i) : At(e, t, n, r, i);
903
+ }
904
+ function _t(e, t, n, r, i) {
905
+ e === w.Start ? r.track("stream-session", { event: "start", "stream-type": i }) : e === w.Stop && r.track("stream-session", {
906
+ event: "stop",
907
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
908
+ background: t.presenter.type === "clip" && t.presenter.background,
909
+ "stream-type": i,
910
+ ...n
911
+ });
912
+ }
913
+ function Tt(e, t, n, r) {
914
+ N.get() <= 0 || (e === w.Start ? n.linkTrack(
915
+ "agent-video",
916
+ { event: "start", latency: N.get(!0), "stream-type": r },
917
+ "start",
918
+ [L.StreamVideoCreated]
919
+ ) : e === w.Stop && n.linkTrack(
920
+ "agent-video",
921
+ {
922
+ event: "stop",
923
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
924
+ background: t.presenter.type === "clip" && t.presenter.background,
925
+ "stream-type": r
926
+ },
927
+ "done",
928
+ [L.StreamVideoDone]
929
+ ));
930
+ }
931
+ function At(e, t, n, r, i) {
932
+ N.get() <= 0 || (e === w.Start ? r.linkTrack(
933
+ "agent-video",
934
+ { event: "start", latency: N.get(!0), "stream-type": i },
935
+ "start",
936
+ [L.StreamVideoCreated]
937
+ ) : e === w.Stop && r.linkTrack(
938
+ "agent-video",
939
+ {
940
+ event: "stop",
941
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
942
+ background: t.presenter.type === "clip" && t.presenter.background,
943
+ "stream-type": i,
944
+ ...n
945
+ },
946
+ "done",
947
+ [L.StreamVideoDone]
948
+ ));
949
+ }
950
+ function fe(e, t, n) {
951
+ return N.reset(), new Promise(async (r, i) => {
952
+ try {
953
+ let s;
954
+ s = await Rt(e, Et(e, t), {
955
+ ...t,
956
+ analytics: n,
957
+ callbacks: {
958
+ ...t.callbacks,
959
+ onConnectionStateChange: (c) => {
960
+ var a, o;
961
+ (o = (a = t.callbacks).onConnectionStateChange) == null || o.call(a, c), c === R.Connected && r(s);
962
+ },
963
+ onVideoStateChange: (c, a) => {
964
+ var o, d;
965
+ (d = (o = t.callbacks).onVideoStateChange) == null || d.call(o, c), Mt(
966
+ c,
967
+ e,
968
+ a,
969
+ n,
970
+ s.streamType
971
+ );
972
+ },
973
+ onAgentActivityStateChange: (c) => {
974
+ var a, o;
975
+ (o = (a = t.callbacks).onAgentActivityStateChange) == null || o.call(a, c), c === O.Talking ? ae.update() : ae.reset(), Tt(
976
+ c === O.Talking ? w.Start : w.Stop,
977
+ e,
978
+ n,
979
+ s.streamType
980
+ );
981
+ }
982
+ }
983
+ });
984
+ } catch (s) {
985
+ i(s);
986
+ }
987
+ });
988
+ }
989
+ async function jt(e, t, n, r, i) {
990
+ var l, v, M, A;
991
+ const s = async () => {
992
+ var b;
993
+ if (De(e.presenter.type)) {
994
+ const j = await ie(
995
+ e,
996
+ n,
997
+ r,
998
+ t.mode,
999
+ t.persistentChat,
1000
+ i
1001
+ ), B = await fe(
1002
+ e,
1003
+ { ...t, chatId: (b = j.chat) == null ? void 0 : b.id },
1004
+ r
1005
+ );
1006
+ return { chatResult: j, streamingManager: B };
1007
+ } else {
1008
+ const j = ie(
1009
+ e,
1010
+ n,
1011
+ r,
1012
+ t.mode,
1013
+ t.persistentChat,
1014
+ i
1015
+ ), B = fe(e, t, r), [z, y] = await Promise.all([j, B]);
1016
+ return { chatResult: z, streamingManager: y };
1017
+ }
1018
+ }, { chatResult: c, streamingManager: a } = await s(), { chat: o, chatMode: d } = c;
1019
+ return d && d !== t.mode && (t.mode = d, (v = (l = t.callbacks).onModeChange) == null || v.call(l, d), d !== E.Functional) ? ((A = (M = t.callbacks).onError) == null || A.call(M, new Me(d)), a == null || a.disconnect(), { chat: o }) : { chat: o, streamingManager: a };
1020
+ }
1021
+ async function Lt(e, t) {
1022
+ var z, y, K;
1023
+ let n = !0, r = null;
1024
+ const i = t.mixpanelKey || Je, s = t.wsURL || Ne, c = t.baseURL || ee, a = {
1025
+ messages: [],
1026
+ chatMode: t.mode || E.Functional
1027
+ }, o = Qe({
1028
+ token: i,
1029
+ agentId: e,
1030
+ isEnabled: t.enableAnalitics,
1031
+ distinctId: t.distinctId,
1032
+ mixpanelAdditionalProperties: t.mixpanelAdditionalProperties
1033
+ });
1034
+ o.track("agent-sdk", { event: "init" });
1035
+ const d = ye(t.auth, c, t.callbacks.onError), l = await d.getById(e);
1036
+ o.enrich(qe(l));
1037
+ const { onMessage: v, clearQueue: M } = st(
1038
+ o,
1039
+ a,
1040
+ t,
1041
+ l,
1042
+ () => {
1043
+ var u;
1044
+ return (u = a.socketManager) == null ? void 0 : u.disconnect();
1045
+ }
1046
+ );
1047
+ a.messages = Ze(t.initialMessages), (y = (z = t.callbacks).onNewMessage) == null || y.call(z, [...a.messages], "answer");
1048
+ const A = (u) => {
1049
+ r = u;
1050
+ };
1051
+ o.track("agent-sdk", { event: "loaded", ...Ve(l) });
1052
+ async function b(u) {
1053
+ var S, T, $, C, I, D, F;
1054
+ (T = (S = t.callbacks).onConnectionStateChange) == null || T.call(S, R.Connecting), N.reset(), u && !n && (delete a.chat, (C = ($ = t.callbacks).onNewMessage) == null || C.call($, [...a.messages], "answer"));
1055
+ const h = t.mode === E.DirectPlayback ? Promise.resolve(void 0) : nt(t.auth, s, { onMessage: v, onError: t.callbacks.onError }), p = ne(
1056
+ () => jt(
1057
+ l,
1058
+ { ...t, callbacks: { ...t.callbacks, onVideoIdChange: A } },
1059
+ d,
1060
+ o,
1061
+ a.chat
1062
+ ),
1063
+ {
1064
+ limit: 3,
1065
+ timeout: ze,
1066
+ timeoutErrorMessage: "Timeout initializing the stream",
1067
+ // Retry on all errors except for connection errors and rate limit errors, these are already handled in client level.
1068
+ shouldRetryFn: (x) => (x == null ? void 0 : x.message) !== "Could not connect" && x.status !== 429,
1069
+ delayMs: 1e3
1070
+ }
1071
+ ).catch((x) => {
1072
+ var J, W;
1073
+ throw B(E.Maintenance), (W = (J = t.callbacks).onConnectionStateChange) == null || W.call(J, R.Fail), x;
1074
+ }), [_, { streamingManager: k, chat: f }] = await Promise.all([h, p]);
1075
+ f && f.id !== ((I = a.chat) == null ? void 0 : I.id) && ((F = (D = t.callbacks).onNewChat) == null || F.call(D, f.id)), a.streamingManager = k, a.socketManager = _, a.chat = f, n = !1, o.enrich({
1076
+ chatId: f == null ? void 0 : f.id,
1077
+ streamId: k == null ? void 0 : k.streamId,
1078
+ mode: a.chatMode
1079
+ }), B((f == null ? void 0 : f.chat_mode) ?? t.mode ?? E.Functional);
1080
+ }
1081
+ async function j() {
1082
+ var u, h, p, _;
1083
+ (u = a.socketManager) == null || u.disconnect(), await ((h = a.streamingManager) == null ? void 0 : h.disconnect()), delete a.streamingManager, delete a.socketManager, (_ = (p = t.callbacks).onConnectionStateChange) == null || _.call(p, R.Disconnected);
1084
+ }
1085
+ async function B(u) {
1086
+ var h, p;
1087
+ u !== a.chatMode && (o.track("agent-mode-change", { mode: u }), a.chatMode = u, a.chatMode !== E.Functional && await j(), (p = (h = t.callbacks).onModeChange) == null || p.call(h, u));
1088
+ }
1089
+ return {
1090
+ agent: l,
1091
+ getStreamType: () => {
1092
+ var u;
1093
+ return (u = a.streamingManager) == null ? void 0 : u.streamType;
1094
+ },
1095
+ getIsInterruptAvailable: () => {
1096
+ var u;
1097
+ return ((u = a.streamingManager) == null ? void 0 : u.interruptAvailable) ?? !1;
1098
+ },
1099
+ getIsTriggersAvailable: () => {
1100
+ var u;
1101
+ return ((u = a.streamingManager) == null ? void 0 : u.triggersAvailable) ?? !1;
1102
+ },
1103
+ starterMessages: ((K = l.knowledge) == null ? void 0 : K.starter_message) || [],
1104
+ getSTTToken: () => d.getSTTToken(l.id),
1105
+ changeMode: B,
1106
+ enrichAnalytics: o.enrich,
1107
+ async connect() {
1108
+ await b(!0), o.track("agent-chat", {
1109
+ event: "connect",
1110
+ mode: a.chatMode
1111
+ });
1112
+ },
1113
+ async reconnect() {
1114
+ await j(), await b(!1), o.track("agent-chat", {
1115
+ event: "reconnect",
1116
+ mode: a.chatMode
1117
+ });
1118
+ },
1119
+ async disconnect() {
1120
+ await j(), o.track("agent-chat", {
1121
+ event: "disconnect",
1122
+ mode: a.chatMode
1123
+ });
1124
+ },
1125
+ async chat(u) {
1126
+ var k, f, S, T, $;
1127
+ const h = () => {
1128
+ if (he(t.mode))
1129
+ throw new H(`${t.mode} is enabled, chat is disabled`);
1130
+ if (u.length >= 800)
1131
+ throw new H("Message cannot be more than 800 characters");
1132
+ if (u.length === 0)
1133
+ throw new H("Message cannot be empty");
1134
+ if (a.chatMode === E.Maintenance)
1135
+ throw new H("Chat is in maintenance mode");
1136
+ if (![E.TextOnly, E.Playground].includes(a.chatMode)) {
1137
+ if (!a.streamingManager)
1138
+ throw new H("Streaming manager is not initialized");
1139
+ if (!a.chat)
1140
+ throw new H("Chat is not initialized");
1141
+ }
1142
+ }, p = async () => {
1143
+ var C, I;
1144
+ if (!a.chat) {
1145
+ const D = await ie(
1146
+ l,
1147
+ d,
1148
+ o,
1149
+ a.chatMode,
1150
+ t.persistentChat
1151
+ );
1152
+ if (!D.chat)
1153
+ throw new Ee(a.chatMode, !!t.persistentChat);
1154
+ a.chat = D.chat, (I = (C = t.callbacks).onNewChat) == null || I.call(C, a.chat.id);
1155
+ }
1156
+ return a.chat.id;
1157
+ }, _ = async (C, I) => ne(
1158
+ () => {
1159
+ var D, F;
1160
+ return d.chat(
1161
+ l.id,
1162
+ I,
1163
+ {
1164
+ chatMode: a.chatMode,
1165
+ streamId: (D = a.streamingManager) == null ? void 0 : D.streamId,
1166
+ sessionId: (F = a.streamingManager) == null ? void 0 : F.sessionId,
1167
+ messages: C.map(({ matches: x, ...J }) => J)
1168
+ },
1169
+ {
1170
+ ...Re(a.chatMode),
1171
+ skipErrorHandler: !0
1172
+ }
1173
+ );
1174
+ },
1175
+ {
1176
+ limit: 2,
1177
+ shouldRetryFn: (D) => {
1178
+ var J, W, X, Y;
1179
+ const F = (J = D == null ? void 0 : D.message) == null ? void 0 : J.includes("missing or invalid session_id");
1180
+ return !((W = D == null ? void 0 : D.message) == null ? void 0 : W.includes("Stream Error")) && !F ? ((Y = (X = t.callbacks).onError) == null || Y.call(X, D), !1) : !0;
1181
+ },
1182
+ onRetry: async () => {
1183
+ await j(), await b(!1);
1184
+ }
1185
+ }
1186
+ );
1187
+ try {
1188
+ M(), h(), a.messages.push({
1189
+ id: q(),
1190
+ role: "user",
1191
+ content: u,
1192
+ created_at: new Date(N.update()).toISOString()
1193
+ }), (f = (k = t.callbacks).onNewMessage) == null || f.call(k, [...a.messages], "user");
1194
+ const C = await p(), I = await _([...a.messages], C);
1195
+ return a.messages.push({
1196
+ id: q(),
1197
+ role: "assistant",
1198
+ content: I.result || "",
1199
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
1200
+ context: I.context,
1201
+ matches: I.matches
1202
+ }), o.track("agent-message-send", {
1203
+ event: "success",
1204
+ messages: a.messages.length + 1
1205
+ }), I.result && ((T = (S = t.callbacks).onNewMessage) == null || T.call(S, [...a.messages], "answer"), o.track("agent-message-received", {
1206
+ latency: N.get(!0),
1207
+ messages: a.messages.length
1208
+ })), I;
1209
+ } catch (C) {
1210
+ throw (($ = a.messages[a.messages.length - 1]) == null ? void 0 : $.role) === "assistant" && a.messages.pop(), o.track("agent-message-send", {
1211
+ event: "error",
1212
+ messages: a.messages.length
1213
+ }), C;
1214
+ }
1215
+ },
1216
+ rate(u, h, p) {
1217
+ var f, S, T, $;
1218
+ const _ = a.messages.find((C) => C.id === u);
1219
+ if (a.chat) {
1220
+ if (!_)
1221
+ throw new Error("Message not found");
1222
+ } else throw new Error("Chat is not initialized");
1223
+ const k = ((f = _.matches) == null ? void 0 : f.map((C) => [C.document_id, C.id])) ?? [];
1224
+ return o.track("agent-rate", {
1225
+ event: p ? "update" : "create",
1226
+ thumb: h === 1 ? "up" : "down",
1227
+ knowledge_id: ((S = l.knowledge) == null ? void 0 : S.id) ?? "",
1228
+ matches: k,
1229
+ score: h
1230
+ }), p ? d.updateRating(l.id, a.chat.id, p, {
1231
+ knowledge_id: ((T = l.knowledge) == null ? void 0 : T.id) ?? "",
1232
+ message_id: u,
1233
+ matches: k,
1234
+ score: h
1235
+ }) : d.createRating(l.id, a.chat.id, {
1236
+ knowledge_id: (($ = l.knowledge) == null ? void 0 : $.id) ?? "",
1237
+ message_id: u,
1238
+ matches: k,
1239
+ score: h
1240
+ });
1241
+ },
1242
+ deleteRate(u) {
1243
+ if (!a.chat)
1244
+ throw new Error("Chat is not initialized");
1245
+ return o.track("agent-rate-delete", { type: "text" }), d.deleteRating(l.id, a.chat.id, u);
1246
+ },
1247
+ async speak(u) {
1248
+ var k, f, S;
1249
+ function h() {
1250
+ if (typeof u == "string") {
1251
+ if (!l.presenter.voice)
1252
+ throw new Error("Presenter voice is not initialized");
1253
+ return {
1254
+ type: "text",
1255
+ provider: l.presenter.voice,
1256
+ input: u,
1257
+ ssml: !1
1258
+ };
1259
+ }
1260
+ if (u.type === "text" && !u.provider) {
1261
+ if (!l.presenter.voice)
1262
+ throw new Error("Presenter voice is not initialized");
1263
+ return {
1264
+ type: "text",
1265
+ provider: l.presenter.voice,
1266
+ input: u.input,
1267
+ ssml: u.ssml
1268
+ };
1269
+ }
1270
+ return u;
1271
+ }
1272
+ const p = h();
1273
+ if (o.track("agent-speak", p), N.update(), a.messages && p.type === "text" && (a.messages.push({
1274
+ id: q(),
1275
+ role: "assistant",
1276
+ content: p.input,
1277
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
1278
+ }), (f = (k = t.callbacks).onNewMessage) == null || f.call(k, [...a.messages], "answer")), We(a.chatMode))
1279
+ return {
1280
+ duration: 0,
1281
+ video_id: "",
1282
+ status: "success"
1283
+ };
1284
+ if (!a.streamingManager)
1285
+ throw new Error("Please connect to the agent first");
1286
+ return a.streamingManager.speak({
1287
+ script: p,
1288
+ metadata: { chat_id: (S = a.chat) == null ? void 0 : S.id, agent_id: l.id }
1289
+ });
1290
+ },
1291
+ async interrupt({ type: u }) {
1292
+ var p, _, k;
1293
+ Ge(a.streamingManager, (p = a.streamingManager) == null ? void 0 : p.streamType, r);
1294
+ const h = a.messages[a.messages.length - 1];
1295
+ o.track("agent-video-interrupt", {
1296
+ type: u || "click",
1297
+ video_duration_to_interrupt: ae.get(!0),
1298
+ message_duration_to_interrupt: N.get(!0)
1299
+ }), h.interrupted = !0, (k = (_ = t.callbacks).onNewMessage) == null || k.call(_, [...a.messages], "answer"), et(a.streamingManager, r);
1300
+ }
1301
+ };
1302
+ }
1303
+ function Bt(e, t, n) {
1304
+ const { getById: r } = ye(t, n || ee);
1305
+ return r(e);
1306
+ }
1307
+ export {
1308
+ O as A,
1309
+ R as C,
1310
+ Be as D,
1311
+ Le as K,
1312
+ Ae as P,
1313
+ Pe as R,
1314
+ U as S,
1315
+ ce as T,
1316
+ Te as U,
1317
+ H as V,
1318
+ _e as W,
1319
+ ct as a,
1320
+ G as b,
1321
+ ve as c,
1322
+ w as d,
1323
+ L as e,
1324
+ ee as f,
1325
+ Ee as g,
1326
+ Me as h,
1327
+ Lt as i,
1328
+ Bt as j,
1329
+ je as k,
1330
+ E as l,
1331
+ V as m,
1332
+ be as n,
1333
+ me as o,
1334
+ bt as p,
1335
+ $e as q,
1336
+ xe as r
1337
+ };