@d-id/client-sdk 1.1.21-staging.98 → 1.1.22

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