@d-id/client-sdk 1.1.15 → 1.1.16

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