@d-id/client-sdk 1.1.13 → 1.1.14-staging.55

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,1284 @@
1
+ var ke = Object.defineProperty;
2
+ var De = (e, t, n) => t in e ? ke(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var Z = (e, t, n) => De(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 Ce 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 Re 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 Se extends te {
34
+ constructor(t) {
35
+ super({ kind: "WSError", description: t });
36
+ }
37
+ }
38
+ var Ie = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(Ie || {}), Ee = /* @__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))(Ee || {}), Me = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(Me || {}), _e = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(_e || {}), 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 || {}), Te = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(Te || {}), je = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(je || {}), Ae = /* @__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))(Ae || {}), de = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e.Expressive = "expressive", e))(de || {});
39
+ const jt = (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 || {}), 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 || {}), 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 || {}), J = /* @__PURE__ */ ((e) => (e.Legacy = "legacy", e.Fluent = "fluent", e))(J || {}), be = /* @__PURE__ */ ((e) => (e.Livekit = "livekit", e))(be || {}), Pe = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(Pe || {}), Le = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(Le || {});
50
+ const Be = 45 * 1e3, $e = "X-Playground-Chat", ee = "https://api.d-id.com", xe = "wss://notifications.d-id.com", ze = "79f81a83a67430be2bc0fd61042b8faa", le = (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
+ }, Fe = (e) => [E.TextOnly, E.Playground, E.Maintenance].includes(e), ue = (e) => e && [E.DirectPlayback, E.Off].includes(e);
54
+ function Ne(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 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: o, clear: c } = Ne(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 le(n.delayMs), n.onRetry(o);
84
+ }
85
+ throw r;
86
+ }
87
+ function fe() {
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 Je = V();
96
+ function me(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}.${fe()}_${Je}`;
103
+ throw new Error(`Unknown auth type: ${e}`);
104
+ }
105
+ const We = (e) => ne(e, {
106
+ limit: 3,
107
+ delayMs: 1e3,
108
+ timeout: 0,
109
+ shouldRetryFn: (t) => t.status === 429
110
+ });
111
+ function ge(e, t = ee, n) {
112
+ const r = async (i, o) => {
113
+ const { skipErrorHandler: c, ...a } = o || {}, s = await We(
114
+ () => fetch(t + (i != null && i.startsWith("/") ? i : `/${i}`), {
115
+ ...a,
116
+ headers: {
117
+ ...a.headers,
118
+ Authorization: me(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 he(e, t = ee, n) {
146
+ const r = ge(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 we = (e) => e.type === "clip" && e.presenter_id.startsWith("v2_") ? "clip_v2" : e.type;
184
+ function Ke(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: we(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 Ue(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: we(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 He = (e) => e.reduce((t, n) => t + n, 0), se = (e) => He(e) / e.length;
226
+ function qe(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 re = {};
238
+ const Ve = "https://api-js.mixpanel.com/track/?verbose=1&ip=1";
239
+ function Oe(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 || fe(),
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(Ve, c).then((a) => a.json());
285
+ } catch (a) {
286
+ return console.error(a);
287
+ }
288
+ },
289
+ linkTrack(n, r, i, o) {
290
+ re[n] || (re[n] = { events: {}, resolvedDependencies: [] }), o.includes(i) || o.push(i);
291
+ const c = re[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 pe() {
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 $ = pe(), ae = pe();
314
+ function ye(e) {
315
+ return e === E.Playground ? { headers: { [$e]: "true" } } : {};
316
+ }
317
+ async function ve(e, t, n, r, i = !1, o) {
318
+ try {
319
+ return !o && !ue(r) && (o = await t.newChat(e.id, { persist: i }, ye(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 Xe(e) {
336
+ return e && e.length > 0 ? e : [];
337
+ }
338
+ function Ye(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 !== J.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 Qe(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 Ze(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=${me(o)}`);
359
+ l.onmessage = c, l.onclose = s, l.onerror = (y) => {
360
+ console.error(y), d == null || d("Websocket failed to connect", y), n(y);
361
+ }, l.onopen = (y) => {
362
+ a == null || a(y), t(l);
363
+ };
364
+ });
365
+ }
366
+ async function Ge(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 Ze(e);
372
+ } catch (i) {
373
+ if (r === t)
374
+ throw i;
375
+ await le(r * 500);
376
+ }
377
+ return n;
378
+ }
379
+ async function et(e, t, n) {
380
+ const r = n != null && n.onMessage ? [n.onMessage] : [], i = await Ge({
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 Se(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 tt(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 rt(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 = tt(n);
415
+ (o.content !== s || e === q.Answer) && (o.content = s, i == null || i([...r.messages], e));
416
+ }
417
+ function nt(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
+ rt(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, y = [l.StreamVideoDone, l.StreamVideoError, l.StreamVideoRejected], M = [l.StreamFailed, l.StreamVideoError, l.StreamVideoRejected], j = qe(a, r, { mode: t.chatMode });
430
+ if (c = c, c === l.StreamVideoCreated)
431
+ e.linkTrack("agent-video", j, l.StreamVideoCreated, ["start"]);
432
+ else if (y.includes(c)) {
433
+ const b = c.split("/")[1];
434
+ M.includes(c) ? e.track("agent-video", { ...j, event: b }) : e.linkTrack("agent-video", { ...j, event: b }, 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 at(e, t, n, r) {
442
+ const i = ge(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 it = (e, t) => (n, r) => e && console.log(`[${t}] ${n}`, r ?? "");
471
+ function st(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 ot(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 ct(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 dt(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 = ot(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: se(a),
585
+ maxRtt: Math.max(...a),
586
+ aggregateReport: st(e[0], e[e.length - 1], o),
587
+ minJitterDelayInInterval: Math.min(...c),
588
+ maxJitterDelayInInterval: Math.max(...c),
589
+ avgJitterDelayInInterval: se(c)
590
+ },
591
+ codec: e[0].codec,
592
+ resolution: `${e[0].frameWidth}x${e[0].frameHeight}`
593
+ };
594
+ }
595
+ const ie = 100, lt = Math.max(Math.ceil(400 / ie), 1), ut = 0.25, ft = 0.28;
596
+ function mt() {
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, y = a - n;
604
+ r = l / y;
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 gt(e, t, n, r, i, o = !1) {
614
+ let c = [], a, s = 0, d = !1, l = G.Unknown, y = G.Unknown, M = 0, j = 0;
615
+ const b = mt();
616
+ return setInterval(async () => {
617
+ const z = await e.getStats(), { isReceiving: K, avgJitterDelayInInterval: W, freezeCount: C } = b(z), U = ct(z);
618
+ if (K)
619
+ s = 0, M = C - j, y = W < ut ? G.Strong : W > ft && M > 1 ? G.Weak : l, y !== l && (i == null || i(y), l = y, j += 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 >= lt)) {
621
+ const u = dt(c, ie, a);
622
+ r == null || r(w.Stop, u), t() || n(), j = C, d = !1;
623
+ }
624
+ }, ie);
625
+ }
626
+ const ht = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
627
+ function oe(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 wt = (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 pt({
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 yt({
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 ce({
674
+ statsSignal: e,
675
+ dataChannelSignal: t,
676
+ onVideoStateChange: n,
677
+ onAgentActivityStateChange: r,
678
+ streamType: i,
679
+ report: o
680
+ }) {
681
+ i === J.Legacy ? pt({ statsSignal: e, dataChannelSignal: t, onVideoStateChange: n, report: o }) : i === J.Fluent && yt({
682
+ statsSignal: e,
683
+ dataChannelSignal: t,
684
+ onVideoStateChange: n,
685
+ onAgentActivityStateChange: r,
686
+ report: o
687
+ });
688
+ }
689
+ async function vt(e, t, { debug: n = !1, callbacks: r, auth: i, baseURL: o = ee, analytics: c }) {
690
+ var Y;
691
+ const a = it(n, "WebRTCStreamingManager"), s = wt(a);
692
+ let d = !1, l = !1, y = w.Stop, M = w.Stop;
693
+ const { startConnection: j, sendStreamRequest: b, close: z, createStream: K, addIceCandidate: W } = at(
694
+ i,
695
+ o,
696
+ e,
697
+ r.onError
698
+ ), {
699
+ id: C,
700
+ offer: U,
701
+ ice_servers: u,
702
+ session_id: h,
703
+ fluent: p,
704
+ interrupt_enabled: _,
705
+ triggers_enabled: v
706
+ } = await K(t);
707
+ (Y = r.onStreamCreated) == null || Y.call(r, { stream_id: C, session_id: h, agent_id: e });
708
+ const f = new ht({ iceServers: u }), S = f.createDataChannel("JanusDataChannel");
709
+ if (!h)
710
+ throw new Error("Could not create session_id");
711
+ const T = p ? J.Fluent : J.Legacy;
712
+ c.enrich({
713
+ "stream-type": T
714
+ });
715
+ const L = t.stream_warmup && !p, D = () => d, I = () => {
716
+ var m;
717
+ d = !0, l && ((m = r.onConnectionStateChange) == null || m.call(r, R.Connected));
718
+ }, k = gt(
719
+ f,
720
+ D,
721
+ I,
722
+ (m, g) => ce({
723
+ statsSignal: M = m,
724
+ dataChannelSignal: T === J.Legacy ? y : 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
+ L
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 ? W(
741
+ C,
742
+ {
743
+ candidate: m.candidate.candidate,
744
+ sdpMid: m.candidate.sdpMid,
745
+ sdpMLineIndex: m.candidate.sdpMLineIndex
746
+ },
747
+ h
748
+ ) : W(C, { candidate: null }, h);
749
+ } catch (A) {
750
+ (g = r.onError) == null || g.call(r, A, { streamId: C });
751
+ }
752
+ }, S.onopen = () => {
753
+ l = !0, (!L || d) && I();
754
+ };
755
+ const x = (m) => {
756
+ var g;
757
+ (g = r.onVideoIdChange) == null || g.call(r, m);
758
+ };
759
+ function B(m, g) {
760
+ if (m === P.StreamStarted && typeof g == "object" && "metadata" in g) {
761
+ const A = g.metadata;
762
+ x(A.videoId);
763
+ }
764
+ m === P.StreamDone && x(null), y = m === P.StreamStarted ? w.Start : w.Stop, ce({
765
+ statsSignal: T === J.Legacy ? M : void 0,
766
+ dataChannelSignal: y,
767
+ onVideoStateChange: r.onVideoStateChange,
768
+ onAgentActivityStateChange: r.onAgentActivityStateChange,
769
+ streamType: T
770
+ });
771
+ }
772
+ function F(m, g) {
773
+ const A = typeof g == "string" ? g : g == null ? void 0 : g.metadata;
774
+ A && c.enrich({ streamMetadata: A }), c.track("agent-chat", { event: "ready" });
775
+ }
776
+ const N = {
777
+ [P.StreamStarted]: B,
778
+ [P.StreamDone]: B,
779
+ [P.StreamReady]: F
780
+ };
781
+ S.onmessage = (m) => {
782
+ var Q;
783
+ const { subject: g, data: A } = s(m.data);
784
+ (Q = N[g]) == null || Q.call(N, g, A);
785
+ }, f.oniceconnectionstatechange = () => {
786
+ var g;
787
+ a("peerConnection.oniceconnectionstatechange => " + f.iceConnectionState);
788
+ const m = oe(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(U), 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 j(C, 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(C, h, m);
802
+ },
803
+ /**
804
+ * Method to close RTC connection
805
+ */
806
+ async disconnect() {
807
+ var m;
808
+ if (C) {
809
+ const g = oe(f.iceConnectionState);
810
+ if (f) {
811
+ if (g === R.New) {
812
+ clearInterval(k);
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 z(C, h).catch((A) => {
819
+ });
820
+ } catch (A) {
821
+ a("Error on close stream connection", A);
822
+ }
823
+ (m = r.onAgentActivityStateChange) == null || m.call(r, O.Idle), clearInterval(k);
824
+ }
825
+ },
826
+ /**
827
+ * Method to send data channel messages to the server
828
+ */
829
+ sendDataChannelMessage(m) {
830
+ var g, A;
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: C
834
+ });
835
+ return;
836
+ }
837
+ try {
838
+ S.send(m);
839
+ } catch (Q) {
840
+ a("Error sending data channel message", Q), (A = r.onError) == null || A.call(r, Q, { streamId: C });
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: C,
851
+ streamType: T,
852
+ interruptAvailable: _ ?? !1,
853
+ triggersAvailable: v ?? !1
854
+ };
855
+ }
856
+ const kt = (e) => e.presenter.type === de.Expressive;
857
+ async function Dt(e, t, n) {
858
+ const r = e.id;
859
+ if (kt(e)) {
860
+ const { createLiveKitStreamingManager: i } = await import("./livekit-manager-DZ_BAT5P.js");
861
+ return i(r, t, n);
862
+ } else
863
+ return vt(r, t, n);
864
+ }
865
+ function Ct(e) {
866
+ const { streamOptions: t } = e ?? {};
867
+ return {
868
+ output_resolution: t == null ? void 0 : t.outputResolution,
869
+ session_timeout: t == null ? void 0 : t.sessionTimeout,
870
+ stream_warmup: t == null ? void 0 : t.streamWarmup,
871
+ compatibility_mode: t == null ? void 0 : t.compatibilityMode,
872
+ fluent: t == null ? void 0 : t.fluent
873
+ };
874
+ }
875
+ function Rt(e, t, n, r, i) {
876
+ i === J.Fluent ? St(e, t, n, r, i) : Et(e, t, n, r, i);
877
+ }
878
+ function St(e, t, n, r, i) {
879
+ e === w.Start ? r.track("stream-session", { event: "start", "stream-type": i }) : e === w.Stop && r.track("stream-session", {
880
+ event: "stop",
881
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
882
+ background: t.presenter.type === "clip" && t.presenter.background,
883
+ "stream-type": i,
884
+ ...n
885
+ });
886
+ }
887
+ function It(e, t, n, r) {
888
+ $.get() <= 0 || (e === w.Start ? n.linkTrack(
889
+ "agent-video",
890
+ { event: "start", latency: $.get(!0), "stream-type": r },
891
+ "start",
892
+ [P.StreamVideoCreated]
893
+ ) : e === w.Stop && n.linkTrack(
894
+ "agent-video",
895
+ {
896
+ event: "stop",
897
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
898
+ background: t.presenter.type === "clip" && t.presenter.background,
899
+ "stream-type": r
900
+ },
901
+ "done",
902
+ [P.StreamVideoDone]
903
+ ));
904
+ }
905
+ function Et(e, t, n, r, i) {
906
+ $.get() <= 0 || (e === w.Start ? r.linkTrack(
907
+ "agent-video",
908
+ { event: "start", latency: $.get(!0), "stream-type": i },
909
+ "start",
910
+ [P.StreamVideoCreated]
911
+ ) : e === w.Stop && r.linkTrack(
912
+ "agent-video",
913
+ {
914
+ event: "stop",
915
+ is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
916
+ background: t.presenter.type === "clip" && t.presenter.background,
917
+ "stream-type": i,
918
+ ...n
919
+ },
920
+ "done",
921
+ [P.StreamVideoDone]
922
+ ));
923
+ }
924
+ function Mt(e, t, n) {
925
+ return $.reset(), new Promise(async (r, i) => {
926
+ try {
927
+ let o;
928
+ o = await Dt(e, Ct(t), {
929
+ ...t,
930
+ analytics: n,
931
+ callbacks: {
932
+ ...t.callbacks,
933
+ onConnectionStateChange: (c) => {
934
+ var a, s;
935
+ (s = (a = t.callbacks).onConnectionStateChange) == null || s.call(a, c), c === R.Connected && r(o);
936
+ },
937
+ onVideoStateChange: (c, a) => {
938
+ var s, d;
939
+ (d = (s = t.callbacks).onVideoStateChange) == null || d.call(s, c), Rt(
940
+ c,
941
+ e,
942
+ a,
943
+ n,
944
+ o.streamType
945
+ );
946
+ },
947
+ onAgentActivityStateChange: (c) => {
948
+ var a, s;
949
+ (s = (a = t.callbacks).onAgentActivityStateChange) == null || s.call(a, c), c === O.Talking ? ae.update() : ae.reset(), It(
950
+ c === O.Talking ? w.Start : w.Stop,
951
+ e,
952
+ n,
953
+ o.streamType
954
+ );
955
+ }
956
+ }
957
+ });
958
+ } catch (o) {
959
+ i(o);
960
+ }
961
+ });
962
+ }
963
+ async function _t(e, t, n, r, i) {
964
+ var y, M, j, b;
965
+ const o = ve(e, n, r, t.mode, t.persistentChat, i), c = Mt(e, t, r), [a, s] = await Promise.all([o, c]), { chat: d, chatMode: l } = a;
966
+ return l && l !== t.mode && (t.mode = l, (M = (y = t.callbacks).onModeChange) == null || M.call(y, l), l !== E.Functional) ? ((b = (j = t.callbacks).onError) == null || b.call(j, new Re(l)), s == null || s.disconnect(), { chat: d }) : { chat: d, streamingManager: s };
967
+ }
968
+ async function At(e, t) {
969
+ var W, C, U;
970
+ let n = !0, r = null;
971
+ const i = t.mixpanelKey || ze, o = t.wsURL || xe, c = t.baseURL || ee, a = {
972
+ messages: [],
973
+ chatMode: t.mode || E.Functional
974
+ }, s = Oe({
975
+ token: i,
976
+ agentId: e,
977
+ isEnabled: t.enableAnalitics,
978
+ distinctId: t.distinctId,
979
+ mixpanelAdditionalProperties: t.mixpanelAdditionalProperties
980
+ });
981
+ s.track("agent-sdk", { event: "init" });
982
+ const d = he(t.auth, c, t.callbacks.onError), l = await d.getById(e);
983
+ s.enrich(Ue(l));
984
+ const { onMessage: y, clearQueue: M } = nt(
985
+ s,
986
+ a,
987
+ t,
988
+ l,
989
+ () => {
990
+ var u;
991
+ return (u = a.socketManager) == null ? void 0 : u.disconnect();
992
+ }
993
+ );
994
+ a.messages = Xe(t.initialMessages), (C = (W = t.callbacks).onNewMessage) == null || C.call(W, [...a.messages], "answer");
995
+ const j = (u) => {
996
+ r = u;
997
+ };
998
+ s.track("agent-sdk", { event: "loaded", ...Ke(l) });
999
+ async function b(u) {
1000
+ var S, T, L, D, I, k, x;
1001
+ (T = (S = t.callbacks).onConnectionStateChange) == null || T.call(S, R.Connecting), $.reset(), u && !n && (delete a.chat, (D = (L = t.callbacks).onNewMessage) == null || D.call(L, [...a.messages], "answer"));
1002
+ const h = t.mode === E.DirectPlayback ? Promise.resolve(void 0) : et(t.auth, o, { onMessage: y, onError: t.callbacks.onError }), p = ne(
1003
+ () => _t(
1004
+ l,
1005
+ { ...t, callbacks: { ...t.callbacks, onVideoIdChange: j } },
1006
+ d,
1007
+ s,
1008
+ a.chat
1009
+ ),
1010
+ {
1011
+ limit: 3,
1012
+ timeout: Be,
1013
+ timeoutErrorMessage: "Timeout initializing the stream",
1014
+ // Retry on all errors except for connection errors and rate limit errors, these are already handled in client level.
1015
+ shouldRetryFn: (B) => (B == null ? void 0 : B.message) !== "Could not connect" && B.status !== 429,
1016
+ delayMs: 1e3
1017
+ }
1018
+ ).catch((B) => {
1019
+ var F, N;
1020
+ throw K(E.Maintenance), (N = (F = t.callbacks).onConnectionStateChange) == null || N.call(F, R.Fail), B;
1021
+ }), [_, { streamingManager: v, chat: f }] = await Promise.all([h, p]);
1022
+ f && f.id !== ((I = a.chat) == null ? void 0 : I.id) && ((x = (k = t.callbacks).onNewChat) == null || x.call(k, f.id)), a.streamingManager = v, a.socketManager = _, a.chat = f, n = !1, s.enrich({
1023
+ chatId: f == null ? void 0 : f.id,
1024
+ streamId: v == null ? void 0 : v.streamId,
1025
+ mode: a.chatMode
1026
+ }), K((f == null ? void 0 : f.chat_mode) ?? t.mode ?? E.Functional);
1027
+ }
1028
+ async function z() {
1029
+ var u, h, p, _;
1030
+ (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);
1031
+ }
1032
+ async function K(u) {
1033
+ var h, p;
1034
+ u !== a.chatMode && (s.track("agent-mode-change", { mode: u }), a.chatMode = u, a.chatMode !== E.Functional && await z(), (p = (h = t.callbacks).onModeChange) == null || p.call(h, u));
1035
+ }
1036
+ return {
1037
+ agent: l,
1038
+ getStreamType: () => {
1039
+ var u;
1040
+ return (u = a.streamingManager) == null ? void 0 : u.streamType;
1041
+ },
1042
+ getIsInterruptAvailable: () => {
1043
+ var u;
1044
+ return ((u = a.streamingManager) == null ? void 0 : u.interruptAvailable) ?? !1;
1045
+ },
1046
+ getIsTriggersAvailable: () => {
1047
+ var u;
1048
+ return ((u = a.streamingManager) == null ? void 0 : u.triggersAvailable) ?? !1;
1049
+ },
1050
+ starterMessages: ((U = l.knowledge) == null ? void 0 : U.starter_message) || [],
1051
+ getSTTToken: () => d.getSTTToken(l.id),
1052
+ changeMode: K,
1053
+ enrichAnalytics: s.enrich,
1054
+ async connect() {
1055
+ await b(!0), s.track("agent-chat", {
1056
+ event: "connect",
1057
+ mode: a.chatMode
1058
+ });
1059
+ },
1060
+ async reconnect() {
1061
+ await z(), await b(!1), s.track("agent-chat", {
1062
+ event: "reconnect",
1063
+ mode: a.chatMode
1064
+ });
1065
+ },
1066
+ async disconnect() {
1067
+ await z(), s.track("agent-chat", {
1068
+ event: "disconnect",
1069
+ mode: a.chatMode
1070
+ });
1071
+ },
1072
+ async chat(u) {
1073
+ var v, f, S, T, L;
1074
+ const h = () => {
1075
+ if (ue(t.mode))
1076
+ throw new H(`${t.mode} is enabled, chat is disabled`);
1077
+ if (u.length >= 800)
1078
+ throw new H("Message cannot be more than 800 characters");
1079
+ if (u.length === 0)
1080
+ throw new H("Message cannot be empty");
1081
+ if (a.chatMode === E.Maintenance)
1082
+ throw new H("Chat is in maintenance mode");
1083
+ if (![E.TextOnly, E.Playground].includes(a.chatMode)) {
1084
+ if (!a.streamingManager)
1085
+ throw new H("Streaming manager is not initialized");
1086
+ if (!a.chat)
1087
+ throw new H("Chat is not initialized");
1088
+ }
1089
+ }, p = async () => {
1090
+ var D, I;
1091
+ if (!a.chat) {
1092
+ const k = await ve(
1093
+ l,
1094
+ d,
1095
+ s,
1096
+ a.chatMode,
1097
+ t.persistentChat
1098
+ );
1099
+ if (!k.chat)
1100
+ throw new Ce(a.chatMode, !!t.persistentChat);
1101
+ a.chat = k.chat, (I = (D = t.callbacks).onNewChat) == null || I.call(D, a.chat.id);
1102
+ }
1103
+ return a.chat.id;
1104
+ }, _ = async (D, I) => ne(
1105
+ () => {
1106
+ var k, x;
1107
+ return d.chat(
1108
+ l.id,
1109
+ I,
1110
+ {
1111
+ chatMode: a.chatMode,
1112
+ streamId: (k = a.streamingManager) == null ? void 0 : k.streamId,
1113
+ sessionId: (x = a.streamingManager) == null ? void 0 : x.sessionId,
1114
+ messages: D.map(({ matches: B, ...F }) => F)
1115
+ },
1116
+ {
1117
+ ...ye(a.chatMode),
1118
+ skipErrorHandler: !0
1119
+ }
1120
+ );
1121
+ },
1122
+ {
1123
+ limit: 2,
1124
+ shouldRetryFn: (k) => {
1125
+ var F, N, X, Y;
1126
+ const x = (F = k == null ? void 0 : k.message) == null ? void 0 : F.includes("missing or invalid session_id");
1127
+ return !((N = k == null ? void 0 : k.message) == null ? void 0 : N.includes("Stream Error")) && !x ? ((Y = (X = t.callbacks).onError) == null || Y.call(X, k), !1) : !0;
1128
+ },
1129
+ onRetry: async () => {
1130
+ await z(), await b(!1);
1131
+ }
1132
+ }
1133
+ );
1134
+ try {
1135
+ M(), h(), a.messages.push({
1136
+ id: V(),
1137
+ role: "user",
1138
+ content: u,
1139
+ created_at: new Date($.update()).toISOString()
1140
+ }), (f = (v = t.callbacks).onNewMessage) == null || f.call(v, [...a.messages], "user");
1141
+ const D = await p(), I = await _([...a.messages], D);
1142
+ return a.messages.push({
1143
+ id: V(),
1144
+ role: "assistant",
1145
+ content: I.result || "",
1146
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
1147
+ context: I.context,
1148
+ matches: I.matches
1149
+ }), s.track("agent-message-send", {
1150
+ event: "success",
1151
+ messages: a.messages.length + 1
1152
+ }), I.result && ((T = (S = t.callbacks).onNewMessage) == null || T.call(S, [...a.messages], "answer"), s.track("agent-message-received", {
1153
+ latency: $.get(!0),
1154
+ messages: a.messages.length
1155
+ })), I;
1156
+ } catch (D) {
1157
+ throw ((L = a.messages[a.messages.length - 1]) == null ? void 0 : L.role) === "assistant" && a.messages.pop(), s.track("agent-message-send", {
1158
+ event: "error",
1159
+ messages: a.messages.length
1160
+ }), D;
1161
+ }
1162
+ },
1163
+ rate(u, h, p) {
1164
+ var f, S, T, L;
1165
+ const _ = a.messages.find((D) => D.id === u);
1166
+ if (a.chat) {
1167
+ if (!_)
1168
+ throw new Error("Message not found");
1169
+ } else throw new Error("Chat is not initialized");
1170
+ const v = ((f = _.matches) == null ? void 0 : f.map((D) => [D.document_id, D.id])) ?? [];
1171
+ return s.track("agent-rate", {
1172
+ event: p ? "update" : "create",
1173
+ thumb: h === 1 ? "up" : "down",
1174
+ knowledge_id: ((S = l.knowledge) == null ? void 0 : S.id) ?? "",
1175
+ matches: v,
1176
+ score: h
1177
+ }), p ? d.updateRating(l.id, a.chat.id, p, {
1178
+ knowledge_id: ((T = l.knowledge) == null ? void 0 : T.id) ?? "",
1179
+ message_id: u,
1180
+ matches: v,
1181
+ score: h
1182
+ }) : d.createRating(l.id, a.chat.id, {
1183
+ knowledge_id: ((L = l.knowledge) == null ? void 0 : L.id) ?? "",
1184
+ message_id: u,
1185
+ matches: v,
1186
+ score: h
1187
+ });
1188
+ },
1189
+ deleteRate(u) {
1190
+ if (!a.chat)
1191
+ throw new Error("Chat is not initialized");
1192
+ return s.track("agent-rate-delete", { type: "text" }), d.deleteRating(l.id, a.chat.id, u);
1193
+ },
1194
+ async speak(u) {
1195
+ var v, f, S;
1196
+ function h() {
1197
+ if (typeof u == "string") {
1198
+ if (!l.presenter.voice)
1199
+ throw new Error("Presenter voice is not initialized");
1200
+ return {
1201
+ type: "text",
1202
+ provider: l.presenter.voice,
1203
+ input: u,
1204
+ ssml: !1
1205
+ };
1206
+ }
1207
+ if (u.type === "text" && !u.provider) {
1208
+ if (!l.presenter.voice)
1209
+ throw new Error("Presenter voice is not initialized");
1210
+ return {
1211
+ type: "text",
1212
+ provider: l.presenter.voice,
1213
+ input: u.input,
1214
+ ssml: u.ssml
1215
+ };
1216
+ }
1217
+ return u;
1218
+ }
1219
+ const p = h();
1220
+ if (s.track("agent-speak", p), $.update(), a.messages && p.type === "text" && (a.messages.push({
1221
+ id: V(),
1222
+ role: "assistant",
1223
+ content: p.input,
1224
+ created_at: new Date($.get(!0)).toISOString()
1225
+ }), (f = (v = t.callbacks).onNewMessage) == null || f.call(v, [...a.messages], "answer")), Fe(a.chatMode))
1226
+ return {
1227
+ duration: 0,
1228
+ video_id: "",
1229
+ status: "success"
1230
+ };
1231
+ if (!a.streamingManager)
1232
+ throw new Error("Please connect to the agent first");
1233
+ return a.streamingManager.speak({
1234
+ script: p,
1235
+ metadata: { chat_id: (S = a.chat) == null ? void 0 : S.id, agent_id: l.id }
1236
+ });
1237
+ },
1238
+ async interrupt({ type: u }) {
1239
+ var p, _, v;
1240
+ Ye(a.streamingManager, (p = a.streamingManager) == null ? void 0 : p.streamType, r);
1241
+ const h = a.messages[a.messages.length - 1];
1242
+ s.track("agent-video-interrupt", {
1243
+ type: u || "click",
1244
+ video_duration_to_interrupt: ae.get(!0),
1245
+ message_duration_to_interrupt: $.get(!0)
1246
+ }), h.interrupted = !0, (v = (_ = t.callbacks).onNewMessage) == null || v.call(_, [...a.messages], "answer"), Qe(a.streamingManager, r);
1247
+ }
1248
+ };
1249
+ }
1250
+ function bt(e, t, n) {
1251
+ const { getById: r } = he(t, n || ee);
1252
+ return r(e);
1253
+ }
1254
+ export {
1255
+ O as A,
1256
+ R as C,
1257
+ Ae as D,
1258
+ je as K,
1259
+ Ee as P,
1260
+ _e as R,
1261
+ J as S,
1262
+ be as T,
1263
+ Ie as U,
1264
+ H as V,
1265
+ Se as W,
1266
+ it as a,
1267
+ G as b,
1268
+ ge as c,
1269
+ w as d,
1270
+ P as e,
1271
+ ee as f,
1272
+ Ce as g,
1273
+ Re as h,
1274
+ At as i,
1275
+ bt as j,
1276
+ Me as k,
1277
+ E as l,
1278
+ q as m,
1279
+ Te as n,
1280
+ de as o,
1281
+ jt as p,
1282
+ Pe as q,
1283
+ Le as r
1284
+ };