@d-id/client-sdk 1.1.22-staging.104 → 1.1.22-staging.106

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