@d-id/client-sdk 1.1.11-staging.40 → 1.1.11-staging.42

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