@d-id/client-sdk 2.0.2 → 2.0.3-staging.385

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,1763 +0,0 @@
1
- var at = Object.defineProperty;
2
- var it = (e, t, n) => t in e ? at(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var O = (e, t, n) => it(e, typeof t != "symbol" ? t + "" : t, n);
4
- class X extends Error {
5
- constructor(t, n = "Error", i) {
6
- super(t), this.kind = n, this.originalError = i, Object.setPrototypeOf(this, new.target.prototype);
7
- }
8
- toJson() {
9
- const t = this.originalError instanceof Error ? this.originalError.message.slice(0, 256) : void 0;
10
- return {
11
- kind: this.kind,
12
- message: this.message,
13
- ...t && t !== this.message ? { cause: t } : {}
14
- };
15
- }
16
- }
17
- function Dn(e) {
18
- return e instanceof Error && typeof e.kind == "string";
19
- }
20
- class st extends X {
21
- constructor(t, n) {
22
- super(`Failed to create ${n ? "persistent" : ""} chat, mode: ${t}`, "ChatCreationFailed");
23
- }
24
- }
25
- class ot extends X {
26
- constructor(t) {
27
- super(`Chat mode downgraded to ${t}`, "ChatModeDowngraded");
28
- }
29
- }
30
- function ct(e) {
31
- try {
32
- const t = JSON.parse(e);
33
- if (t && typeof t == "object" && typeof t.kind == "string")
34
- return t;
35
- } catch {
36
- }
37
- }
38
- class Ke extends X {
39
- constructor(n, i, c = {}) {
40
- const o = ct(i);
41
- super(((o == null ? void 0 : o.description) ?? i).slice(0, 256), (o == null ? void 0 : o.kind) ?? "HttpError");
42
- O(this, "status");
43
- O(this, "url");
44
- O(this, "method");
45
- this.status = n, this.url = c.url, this.method = c.method;
46
- }
47
- toJson() {
48
- return {
49
- ...super.toJson(),
50
- httpStatus: this.status,
51
- ...this.url ? { endpoint: this.url } : {},
52
- ...this.method ? { method: this.method } : {}
53
- };
54
- }
55
- }
56
- class dt extends X {
57
- constructor(n, i = {}) {
58
- super("Network request failed", "NetworkError", n);
59
- O(this, "endpoint");
60
- O(this, "method");
61
- O(this, "durationMs");
62
- O(this, "online");
63
- O(this, "visibility");
64
- this.endpoint = i.url, this.method = i.method, this.durationMs = i.durationMs, this.online = i.online, this.visibility = i.visibility;
65
- }
66
- toJson() {
67
- return {
68
- ...super.toJson(),
69
- ...this.endpoint ? { endpoint: this.endpoint } : {},
70
- ...this.method ? { method: this.method } : {},
71
- ...this.durationMs !== void 0 ? { durationMs: this.durationMs } : {},
72
- ...this.online !== void 0 ? { online: this.online } : {},
73
- ...this.visibility ? { visibility: this.visibility } : {}
74
- };
75
- }
76
- }
77
- class Oe extends X {
78
- constructor(t, n) {
79
- super(t, "StreamError", n);
80
- }
81
- }
82
- class U extends X {
83
- constructor(t, n) {
84
- super(t, "ValidationError"), this.key = n;
85
- }
86
- }
87
- class lt extends X {
88
- constructor(t) {
89
- super(t, "WSError");
90
- }
91
- }
92
- var ut = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(ut || {}), x = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e.Off = "Off", e))(x || {}), z = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Transcribe = "transcribe", e.Complete = "done", e))(z || {}), ft = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(ft || {}), mt = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(mt || {}), gt = /* @__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))(gt || {}), We = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e.Expressive = "expressive", e))(We || {});
93
- const In = (e) => {
94
- switch (e) {
95
- case "clip":
96
- return "clip";
97
- case "talk":
98
- return "talk";
99
- case "expressive":
100
- return "expressive";
101
- default:
102
- throw new Error(`Unknown video type: ${e}`);
103
- }
104
- };
105
- var k = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(k || {}), ne = /* @__PURE__ */ ((e) => (e.Strong = "STRONG", e.Weak = "WEAK", e.Unknown = "UNKNOWN", e))(ne || {}), ae = /* @__PURE__ */ ((e) => (e.Idle = "IDLE", e.Loading = "LOADING", e.Talking = "TALKING", e.ToolActive = "TOOL_ACTIVE", e))(ae || {}), L = /* @__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.ToolCallStarted = "tool-call/started", e.ToolCallDone = "tool-call/done", e.ToolCallError = "tool-call/error", e.TurnStarted = "turn/started", e.TurnEnded = "turn/ended", e))(L || {}), P = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnecting = "disconnecting", e.Disconnected = "disconnected", e))(P || {}), W = /* @__PURE__ */ ((e) => (e.Legacy = "legacy", e.Fluent = "fluent", e))(W || {}), Se = /* @__PURE__ */ ((e) => (e.Livekit = "livekit", e))(Se || {}), ht = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.AzureOpenAi = "azure-openai", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(ht || {}), pt = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(pt || {});
106
- const Tn = (...e) => {
107
- }, He = (e) => new Promise((t) => setTimeout(t, e)), ee = (e = 16) => {
108
- const t = new Uint8Array(e);
109
- return window.crypto.getRandomValues(t), Array.from(t, (n) => n.toString(16).padStart(2, "0")).join("").slice(0, 13);
110
- };
111
- function pe(e) {
112
- if (e !== void 0)
113
- return window.localStorage.setItem("did_external_key_id", e), e;
114
- let t = window.localStorage.getItem("did_external_key_id");
115
- if (!t) {
116
- let n = ee();
117
- window.localStorage.setItem("did_external_key_id", n), t = n;
118
- }
119
- return t;
120
- }
121
- let le = ee();
122
- function wt() {
123
- le = ee();
124
- }
125
- function qe(e, t) {
126
- if (e.type === "bearer")
127
- return `Bearer ${e.token}~${le}`;
128
- if (e.type === "basic")
129
- return `Basic ${"token" in e ? e.token : btoa(`${e.username}:${e.password}`)}~${le}`;
130
- if (e.type === "key")
131
- return `Client-Key ${e.clientKey}.${pe(t)}_${le}`;
132
- throw new Error(`Unknown auth type: ${e}`);
133
- }
134
- const yt = 45 * 1e3, vt = "X-Playground-Chat", Te = 2e3, fe = "https://api.d-id.com", Ct = "wss://notifications.d-id.com", St = "79f81a83a67430be2bc0fd61042b8faa", Ge = (e) => e.type, kt = (e) => {
135
- switch (e.type) {
136
- case "expressive":
137
- return "v4";
138
- case "clip":
139
- return "v3-pro";
140
- case "talk":
141
- return "v2";
142
- }
143
- }, ke = (e) => e === We.Expressive, Mt = (e) => [x.TextOnly, x.Playground, x.Maintenance].includes(e), Xe = (e) => e && [x.DirectPlayback, x.Off].includes(e), be = /\[!\[([^\[\]]*)\]\(([^)\s]+)\)\]\(([^)\s]+)\)/g, Ee = /!\[([^\[\]]*)\]\(([^)\s]+)\)/g, Rt = [".mp4", ".webm", ".mkv", ".mov", ".m4v", ".ogv"];
144
- function Dt(e) {
145
- const t = e.split("?")[0].split("#")[0].toLowerCase();
146
- return Rt.some((n) => t.endsWith(n));
147
- }
148
- const _e = new RegExp("(?<!!)\\[([^\\[\\]]+)\\]\\(([^)\\s]+)\\)", "g"), Ae = /<a\s+href="([^"]*)"[^>]*?>([^<]*)<\/a>/gi;
149
- function It(e) {
150
- if (e.length === 0)
151
- return [];
152
- const t = [];
153
- let n;
154
- for (be.lastIndex = 0; (n = be.exec(e)) !== null; )
155
- t.push({
156
- index: n.index,
157
- length: n[0].length,
158
- part: { type: "video", src: n[3], alt: n[1], thumbnail: n[2] }
159
- });
160
- for (Ee.lastIndex = 0; (n = Ee.exec(e)) !== null; )
161
- if (!t.some((s) => n.index >= s.index && n.index < s.index + s.length)) {
162
- const s = n[2], r = n[1];
163
- let a;
164
- Dt(s) ? a = { type: "video", src: s, alt: r } : (a = { type: "image", src: s, alt: r }, s.toLowerCase().endsWith(".gif") && (a.mimeType = "image/gif")), t.push({ index: n.index, length: n[0].length, part: a });
165
- }
166
- for (_e.lastIndex = 0; (n = _e.exec(e)) !== null; )
167
- t.some((s) => n.index >= s.index && n.index < s.index + s.length) || t.push({
168
- index: n.index,
169
- length: n[0].length,
170
- part: { type: "link", href: n[2], label: n[1] }
171
- });
172
- for (Ae.lastIndex = 0; (n = Ae.exec(e)) !== null; )
173
- t.some((s) => n.index >= s.index && n.index < s.index + s.length) || t.push({
174
- index: n.index,
175
- length: n[0].length,
176
- part: { type: "link", href: n[1], label: n[2] }
177
- });
178
- if (t.length === 0)
179
- return [{ type: "text", text: e }];
180
- t.sort((o, s) => o.index - s.index);
181
- const i = [];
182
- let c = 0;
183
- for (const o of t)
184
- o.index > c && i.push({ type: "text", text: e.slice(c, o.index) }), i.push(o.part), c = o.index + o.length;
185
- return c < e.length && i.push({ type: "text", text: e.slice(c) }), i;
186
- }
187
- let xe = "", me = [];
188
- function re(e) {
189
- return e === xe || (xe = e, me = It(e)), me;
190
- }
191
- function Tt(e, t) {
192
- let n;
193
- return {
194
- promise: new Promise((c, o) => {
195
- n = setTimeout(() => o(new Error(t)), e);
196
- }),
197
- clear: () => clearTimeout(n)
198
- };
199
- }
200
- async function we(e, t) {
201
- const n = {
202
- limit: (t == null ? void 0 : t.limit) ?? 3,
203
- delayMs: (t == null ? void 0 : t.delayMs) ?? 0,
204
- timeout: (t == null ? void 0 : t.timeout) ?? 3e4,
205
- timeoutErrorMessage: (t == null ? void 0 : t.timeoutErrorMessage) || "Timeout error",
206
- shouldRetryFn: (t == null ? void 0 : t.shouldRetryFn) ?? (() => !0),
207
- onRetry: (t == null ? void 0 : t.onRetry) ?? (() => {
208
- })
209
- };
210
- let i;
211
- for (let c = 1; c <= n.limit; c++)
212
- try {
213
- if (!n.timeout)
214
- return await e();
215
- const { promise: o, clear: s } = Tt(n.timeout, n.timeoutErrorMessage), r = e().finally(s);
216
- return await Promise.race([r, o]);
217
- } catch (o) {
218
- if (i = o, !n.shouldRetryFn(o) || c >= n.limit)
219
- throw o;
220
- await He(n.delayMs), n.onRetry(o);
221
- }
222
- throw i;
223
- }
224
- const bt = (e) => we(e, {
225
- limit: 3,
226
- delayMs: 1e3,
227
- timeout: 0,
228
- shouldRetryFn: (t) => t.status === 429
229
- });
230
- function Qe(e, t = fe, n, i) {
231
- const c = async (o, s) => {
232
- const { skipErrorHandler: r, ...a } = s || {}, d = a.method ?? "GET", m = performance.now();
233
- let u;
234
- try {
235
- u = await bt(
236
- () => fetch(t + (o != null && o.startsWith("/") ? o : `/${o}`), {
237
- ...a,
238
- headers: {
239
- ...a.headers,
240
- Authorization: qe(e, i),
241
- "Content-Type": "application/json"
242
- }
243
- })
244
- );
245
- } catch (f) {
246
- if ((f == null ? void 0 : f.name) === "AbortError")
247
- throw f;
248
- const v = new dt(f, {
249
- url: o,
250
- method: d,
251
- durationMs: Math.round(performance.now() - m),
252
- online: typeof navigator < "u" ? navigator.onLine : void 0,
253
- visibility: typeof document < "u" ? document.visibilityState : void 0
254
- });
255
- throw r || n == null || n(v, { url: o, options: a }), v;
256
- }
257
- if (!u.ok) {
258
- const f = await u.text().catch(() => `Failed to fetch with status ${u.status}`), p = new Ke(u.status, f, { url: o, method: d });
259
- throw r || n == null || n(p, { url: o, options: a, headers: u.headers }), p;
260
- }
261
- return u.json();
262
- };
263
- return {
264
- get(o, s) {
265
- return c(o, { ...s, method: "GET" });
266
- },
267
- post(o, s, r) {
268
- return c(o, { ...r, body: JSON.stringify(s), method: "POST" });
269
- },
270
- delete(o, s, r) {
271
- return c(o, { ...r, body: JSON.stringify(s), method: "DELETE" });
272
- },
273
- patch(o, s, r) {
274
- return c(o, { ...r, body: JSON.stringify(s), method: "PATCH" });
275
- }
276
- };
277
- }
278
- function Et(e, t = fe, n, i) {
279
- const c = Qe(e, `${t}/agents`, n, i);
280
- return {
281
- getRuntimeById(o, s) {
282
- return c.get(`/${o}/runtime`, s);
283
- },
284
- newChat(o, s, r) {
285
- return c.post(`/${o}/chat`, s, r);
286
- },
287
- chat(o, s, r, a) {
288
- return c.post(`/${o}/chat/${s}`, r, a);
289
- },
290
- createRating(o, s, r, a) {
291
- return c.post(`/${o}/chat/${s}/ratings`, r, a);
292
- },
293
- updateRating(o, s, r, a, d) {
294
- return c.patch(`/${o}/chat/${s}/ratings/${r}`, a, d);
295
- },
296
- deleteRating(o, s, r, a) {
297
- return c.delete(`/${o}/chat/${s}/ratings/${r}`, a);
298
- },
299
- submitFeedback(o, s, r, a) {
300
- return c.post(`/${o}/chat/${s}/feedback`, r, a);
301
- },
302
- getSTTToken(o, s) {
303
- return c.get(`/${o}/stt-token`, s);
304
- }
305
- };
306
- }
307
- function _t(e) {
308
- var c, o;
309
- const t = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop", n = () => {
310
- const s = navigator.platform;
311
- return s.toLowerCase().includes("win") ? "Windows" : s.toLowerCase().includes("mac") ? "Mac OS X" : s.toLowerCase().includes("linux") ? "Linux" : "Unknown";
312
- }, i = e.avatar;
313
- return {
314
- $os: `${n()}`,
315
- isMobile: `${t() == "Mobile"}`,
316
- browser: navigator.userAgent,
317
- origin: window.location.origin,
318
- agentType: Ge(i),
319
- agentVoice: {
320
- language: (o = (c = e.avatar) == null ? void 0 : c.voice) == null ? void 0 : o.language
321
- }
322
- };
323
- }
324
- function At(e) {
325
- var t;
326
- return {
327
- agentType: Ge(e.avatar),
328
- presenterType: kt(e.avatar),
329
- owner_id: e.owner_id ?? "",
330
- starterQuestionsCount: (t = e.starter_message) == null ? void 0 : t.length,
331
- agentId: e.id,
332
- agentName: e.name
333
- };
334
- }
335
- const xt = (e) => {
336
- try {
337
- return String((e == null ? void 0 : e.message) ?? e ?? "").slice(0, 256);
338
- } catch {
339
- return "Unknown error";
340
- }
341
- }, Pt = (e) => e.reduce((t, n) => t + n, 0), Pe = (e) => Pt(e) / e.length, Le = (e) => Math.min(...e), je = (e) => Math.max(...e), de = (e, t = 0) => {
342
- const n = 10 ** t;
343
- return Math.round(e * n) / n;
344
- }, Lt = (e, t) => {
345
- const n = [...e].sort((i, c) => i - c);
346
- return n[Math.min(n.length - 1, Math.floor(t * (n.length - 1)))];
347
- }, jt = (e) => Lt(e, 0.5), $t = (e, t) => {
348
- try {
349
- return e();
350
- } catch {
351
- return t;
352
- }
353
- };
354
- function Bt(e, t, n) {
355
- var r, a;
356
- const { event: i, ...c } = e, { language: o } = ((r = t == null ? void 0 : t.avatar) == null ? void 0 : r.voice) || {};
357
- return {
358
- ...c,
359
- script: { ...c.script, provider: { ...(a = c == null ? void 0 : c.script) == null ? void 0 : a.provider, language: o } },
360
- ...n
361
- };
362
- }
363
- function $e(e) {
364
- "requestIdleCallback" in window ? requestIdleCallback(e, { timeout: 2e3 }) : setTimeout(e, 0);
365
- }
366
- function ge(e) {
367
- return e instanceof X ? e.toJson() : { kind: "UnknownError", message: xt(e) || "UnknownError" };
368
- }
369
- const zt = "2.0.2", Ft = "https://api-js.mixpanel.com/track/?verbose=1&ip=1", Ye = 50, Z = [];
370
- let he = !1;
371
- async function Ze(e) {
372
- const t = await fetch(Ft, {
373
- method: "POST",
374
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
375
- body: new URLSearchParams({ data: JSON.stringify(e) })
376
- });
377
- if (!t.ok)
378
- throw new Error(`Mixpanel responded with ${t.status}`);
379
- }
380
- function Nt(e) {
381
- Z.push(e), Z.splice(Ye);
382
- }
383
- function ye() {
384
- if (he || !Z.length)
385
- return;
386
- he = !0;
387
- const e = Z.splice(0, Z.length);
388
- Ze(e).catch(() => {
389
- Z.unshift(...e), Z.splice(Ye);
390
- }).finally(() => {
391
- he = !1;
392
- });
393
- }
394
- typeof window < "u" && typeof window.addEventListener == "function" && (window.addEventListener("online", ye), typeof document < "u" && document.addEventListener("visibilitychange", () => {
395
- document.visibilityState === "visible" && ye();
396
- }));
397
- function Ut(e) {
398
- const t = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", n = {};
399
- return {
400
- token: e.token || "testKey",
401
- distinct_id: pe(e.externalId),
402
- agentId: e.agentId,
403
- additionalProperties: {
404
- id: pe(e.externalId),
405
- ...e.mixpanelAdditionalProperties || {}
406
- },
407
- isEnabled: e.isEnabled ?? !0,
408
- getRandom: ee,
409
- enrich(i) {
410
- this.additionalProperties = { ...this.additionalProperties, ...i };
411
- },
412
- async track(i, c, o) {
413
- if (!this.isEnabled)
414
- return Promise.resolve();
415
- const { audioPath: s, ...r } = c || {}, a = o || Date.now(), d = {
416
- event: i,
417
- properties: {
418
- ...this.additionalProperties,
419
- ...r,
420
- agentId: this.agentId,
421
- source: t,
422
- emittedBy: "agents-sdk",
423
- sdkVersion: zt,
424
- token: this.token,
425
- time: a,
426
- $insert_id: this.getRandom(),
427
- origin: window.location.href,
428
- "Screen Height": window.screen.height || window.innerHeight,
429
- "Screen Width": window.screen.width || window.innerWidth,
430
- "User Agent": navigator.userAgent
431
- }
432
- };
433
- return ye(), Ze([d]).catch(() => Nt(d)), Promise.resolve();
434
- },
435
- linkTrack(i, c, o, s) {
436
- n[i] || (n[i] = { events: {}, resolvedDependencies: [] }), s.includes(o) || s.push(o);
437
- const r = n[i];
438
- if (r.events[o] = { props: c }, r.resolvedDependencies.push(o), s.every(
439
- (d) => r.resolvedDependencies.includes(d)
440
- )) {
441
- const d = s.reduce((m, u) => r.events[u] ? { ...m, ...r.events[u].props } : m, {});
442
- this.track(i, d), r.resolvedDependencies = r.resolvedDependencies.filter(
443
- (m) => !s.includes(m)
444
- ), s.forEach((m) => {
445
- delete r.events[m];
446
- });
447
- }
448
- }
449
- };
450
- }
451
- function Me() {
452
- let e = 0;
453
- return {
454
- reset: () => e = 0,
455
- update: () => e = Date.now(),
456
- get: (t = !1) => t ? Date.now() - e : e
457
- };
458
- }
459
- const G = Me(), ve = Me(), Be = Me();
460
- function et(e) {
461
- return e === x.Playground ? { headers: { [vt]: "true" } } : {};
462
- }
463
- async function tt(e, t, n, i, c = !1, o) {
464
- return !o && !Xe(i) && (o = await t.newChat(e.id, { persist: c }, et(i)), n.track("agent-chat", {
465
- event: "created",
466
- chatId: o.id,
467
- mode: i
468
- })), { chat: o, chatMode: (o == null ? void 0 : o.chat_mode) ?? i };
469
- }
470
- function Jt(e) {
471
- return e && e.length > 0 ? e : [];
472
- }
473
- function Vt(e) {
474
- return new Promise((t, n) => {
475
- const { callbacks: i, host: c, auth: o, externalId: s } = e, { onMessage: r = null, onOpen: a = null, onClose: d = null, onError: m = null } = i || {}, u = new WebSocket(`${c}?authorization=${encodeURIComponent(qe(o, s))}`);
476
- u.onmessage = r, u.onclose = d, u.onerror = (f) => {
477
- console.error(f), m == null || m("Websocket failed to connect", f), n(f);
478
- }, u.onopen = (f) => {
479
- a == null || a(f), t(u);
480
- };
481
- });
482
- }
483
- async function Kt(e) {
484
- const { retries: t = 1 } = e;
485
- let n = null;
486
- for (let i = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; i++)
487
- try {
488
- n = await Vt(e);
489
- } catch (c) {
490
- if (i === t)
491
- throw c;
492
- await He(i * 500);
493
- }
494
- return n;
495
- }
496
- async function Ot(e, t, n, i) {
497
- const c = n != null && n.onMessage ? [n.onMessage] : [], o = await Kt({
498
- auth: e,
499
- host: t,
500
- externalId: i,
501
- callbacks: {
502
- onError: (s) => {
503
- var r;
504
- return (r = n.onError) == null ? void 0 : r.call(n, new lt(s));
505
- },
506
- onMessage(s) {
507
- const r = JSON.parse(s.data);
508
- c.forEach((a) => a(r.event, r));
509
- }
510
- }
511
- });
512
- return {
513
- socket: o,
514
- disconnect: () => o.close(),
515
- subscribeToEvents: (s) => c.push(s)
516
- };
517
- }
518
- function ze(e) {
519
- if (e.answer !== void 0)
520
- return e.answer;
521
- let t = 0, n = "";
522
- for (; t in e; )
523
- n += e[t++];
524
- return n;
525
- }
526
- function Wt(e, t, n) {
527
- if (!e.content)
528
- return;
529
- const i = {
530
- id: e.id || `user-${Date.now()}`,
531
- role: e.role,
532
- content: e.content,
533
- parts: re(e.content),
534
- created_at: e.created_at || (/* @__PURE__ */ new Date()).toISOString(),
535
- transcribed: !0
536
- };
537
- t.messages.push(i), n == null || n([...t.messages], "user");
538
- }
539
- function Ht(e, t, n, i, c, o, s) {
540
- if (e === z.Transcribe && t.content) {
541
- Wt(t, i, c);
542
- return;
543
- }
544
- if (!(e === z.Partial || e === z.Answer))
545
- return;
546
- const r = i.messages[i.messages.length - 1];
547
- if (e === z.Answer && (r == null ? void 0 : r.role) === "user" && s === "partial")
548
- return;
549
- const d = s === "answer";
550
- let m;
551
- if ((r == null ? void 0 : r.role) === "assistant" && !d)
552
- m = r;
553
- else if (!r || r.role === "user" || d)
554
- d && o(), m = {
555
- id: t.id || `assistant-${Date.now()}`,
556
- role: t.role || "assistant",
557
- content: t.content || "",
558
- parts: [],
559
- created_at: t.created_at || (/* @__PURE__ */ new Date()).toISOString()
560
- }, i.messages.push(m);
561
- else
562
- return;
563
- const { content: u, sequence: f } = t;
564
- if (e === z.Partial)
565
- n[f] = u;
566
- else {
567
- const v = ze(n);
568
- !!(u && u.length < v.length) && (m.interrupted = !0), n.answer = u;
569
- }
570
- const p = ze(n);
571
- (m.content !== p || e === z.Answer) && (m.content = p, m.parts = re(p), c == null || c([...i.messages], e));
572
- }
573
- function qt(e, t, n, i, c) {
574
- const o = {}, s = () => {
575
- for (const d of Object.keys(o))
576
- delete o[d];
577
- };
578
- let r = null;
579
- const a = (d, m) => {
580
- var u, f;
581
- m === "user" && s(), (f = (u = n.callbacks).onNewMessage) == null || f.call(u, d, m);
582
- };
583
- return {
584
- clearQueue: s,
585
- onMessage: (d, m) => {
586
- var u, f;
587
- if ("content" in m) {
588
- const p = d === L.ChatAnswer ? z.Answer : d === L.ChatAudioTranscribed ? z.Transcribe : d;
589
- Ht(
590
- p,
591
- m,
592
- o,
593
- t,
594
- a,
595
- s,
596
- r
597
- ), p === z.Partial ? r = "partial" : p === z.Answer && (r = "answer", s()), p === z.Answer && e.track("agent-message-received", {
598
- content: m.content,
599
- messages: t.messages.length,
600
- mode: t.chatMode
601
- });
602
- } else {
603
- const p = L, v = [p.StreamVideoDone, p.StreamVideoError, p.StreamVideoRejected], B = [p.StreamFailed, p.StreamVideoError, p.StreamVideoRejected], D = Bt(m, i, { mode: t.chatMode });
604
- if (d = d, d === p.StreamVideoCreated && (e.linkTrack("agent-video", D, p.StreamVideoCreated, ["start"]), m.sentiment)) {
605
- const b = t.messages[t.messages.length - 1];
606
- if ((b == null ? void 0 : b.role) === "assistant") {
607
- const j = { ...b, sentiment: m.sentiment };
608
- t.messages[t.messages.length - 1] = j, a == null || a([...t.messages], r ?? "answer");
609
- }
610
- }
611
- if (v.includes(d)) {
612
- const b = d.split("/")[1];
613
- B.includes(d) ? e.track("agent-video", { ...D, event: b }) : e.linkTrack("agent-video", { ...D, event: b }, d, ["done"]);
614
- }
615
- B.includes(d) && ((f = (u = n.callbacks).onError) == null || f.call(u, new Oe(`Stream failed with event ${d}`), { data: m })), m.event === p.StreamDone && c();
616
- }
617
- }
618
- };
619
- }
620
- function Gt(e, t, n, i) {
621
- const c = Qe(e, `${t}/agents/${n}`, i);
622
- return {
623
- createStream(o, s) {
624
- return c.post("/streams", o, { signal: s });
625
- },
626
- startConnection(o, s, r, a) {
627
- return c.post(
628
- `/streams/${o}/sdp`,
629
- {
630
- session_id: r,
631
- answer: s
632
- },
633
- { signal: a }
634
- );
635
- },
636
- addIceCandidate(o, s, r, a) {
637
- return c.post(
638
- `/streams/${o}/ice`,
639
- {
640
- session_id: r,
641
- ...s
642
- },
643
- { signal: a }
644
- );
645
- },
646
- sendStreamRequest(o, s, r) {
647
- return c.post(`/streams/${o}`, {
648
- session_id: s,
649
- ...r
650
- });
651
- },
652
- close(o, s) {
653
- return c.delete(`/streams/${o}`, { session_id: s });
654
- }
655
- };
656
- }
657
- const Xt = (e, t) => (n, i) => e && console.log(`[${t}] ${n}`, i ?? ""), Qt = 45;
658
- function Yt(e) {
659
- const t = e.map((a) => a.av).filter((a) => !!a && a.audioPlayout > 0 && a.videoPlayout > 0);
660
- if (t.length < 2)
661
- return null;
662
- const n = t[0].localTs, i = t[t.length - 1].localTs - n, c = t.map((a) => a.audioPlayout - a.videoPlayout), o = c.filter(
663
- (a) => a > Qt || a < -100
664
- ), s = t.filter((a) => a.localTs - n >= Math.min(5e3, i / 2)), r = (s.length ? s : t).map((a) => a.audioPlayout - a.videoPlayout);
665
- return {
666
- sampleCount: t.length,
667
- durationMs: de(i),
668
- desyncDurationMs: de(i * (o.length / c.length)),
669
- maxOffsetMs: de(c.reduce((a, d) => Math.abs(d) > Math.abs(a) ? d : a)),
670
- residualOffsetMs: de(jt(r))
671
- };
672
- }
673
- function Zt(e, t, n) {
674
- const i = (t.timestamp - e.timestamp) / 1e3;
675
- return {
676
- duration: i,
677
- bytesReceived: t.bytesReceived - e.bytesReceived,
678
- bitrate: Math.round((t.bytesReceived - e.bytesReceived) * 8 / i),
679
- packetsReceived: t.packetsReceived - e.packetsReceived,
680
- packetsLost: t.packetsLost - e.packetsLost,
681
- framesDropped: t.framesDropped - e.framesDropped,
682
- framesDecoded: t.framesDecoded - e.framesDecoded,
683
- jitter: t.jitter,
684
- avgJitterDelayInInterval: (t.jitterBufferDelay - e.jitterBufferDelay) / (t.jitterBufferEmittedCount - e.jitterBufferEmittedCount),
685
- jitterBufferEmittedCount: t.jitterBufferEmittedCount - e.jitterBufferEmittedCount,
686
- jitterBufferDelay: (t.jitterBufferDelay - e.jitterBufferDelay) / i,
687
- framesPerSecond: t.framesPerSecond,
688
- freezeCount: t.freezeCount - e.freezeCount,
689
- freezeDuration: t.freezeDuration - e.freezeDuration,
690
- lowFpsCount: n
691
- };
692
- }
693
- function en(e) {
694
- return e.filter(
695
- (t) => t.freezeCount > 0 || t.framesPerSecond < 21 || t.framesDropped > 0 || t.packetsLost > 0
696
- ).map((t) => {
697
- const { timestamp: n, ...i } = t, c = [];
698
- 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"), {
699
- ...i,
700
- causes: c
701
- };
702
- });
703
- }
704
- function tn(e) {
705
- var a;
706
- let t = "", n = 0, i = null, c = null;
707
- const o = /* @__PURE__ */ new Map();
708
- for (const d of e.values())
709
- if (d)
710
- if (d.type === "codec" && ((a = d.mimeType) != null && a.startsWith("video")))
711
- o.set(d.id, d.mimeType.split("/")[1]);
712
- else if (d.type === "candidate-pair") {
713
- const m = d, u = m.currentRoundTripTime ?? 0;
714
- u > 0 && (m.nominated === !0 || n === 0) && (n = u);
715
- } else d.type === "inbound-rtp" && d.kind === "video" ? i = d : d.type === "inbound-rtp" && d.kind === "audio" && (c = d);
716
- if (!i)
717
- return {};
718
- const s = i;
719
- s.codecId && o.has(s.codecId) ? t = o.get(s.codecId) : o.size > 0 && (t = o.values().next().value ?? "");
720
- const r = {
721
- codec: t,
722
- rtt: n,
723
- timestamp: s.timestamp,
724
- bytesReceived: s.bytesReceived,
725
- packetsReceived: s.packetsReceived,
726
- packetsLost: s.packetsLost,
727
- framesDropped: s.framesDropped,
728
- framesDecoded: s.framesDecoded,
729
- jitter: s.jitter,
730
- jitterBufferDelay: s.jitterBufferDelay,
731
- jitterBufferEmittedCount: s.jitterBufferEmittedCount,
732
- avgJitterDelayInInterval: s.jitterBufferDelay / s.jitterBufferEmittedCount,
733
- frameWidth: s.frameWidth,
734
- frameHeight: s.frameHeight,
735
- framesPerSecond: s.framesPerSecond,
736
- freezeCount: s.freezeCount,
737
- freezeDuration: s.totalFreezesDuration
738
- };
739
- return c && (r.av = {
740
- audioPlayout: c.estimatedPlayoutTimestamp ?? 0,
741
- videoPlayout: s.estimatedPlayoutTimestamp ?? 0,
742
- localTs: s.timestamp
743
- }), r;
744
- }
745
- function Fe(e, t, n) {
746
- const i = e.map((a, d) => d === 0 ? n ? {
747
- timestamp: a.timestamp,
748
- duration: 0,
749
- rtt: a.rtt,
750
- bytesReceived: a.bytesReceived - n.bytesReceived,
751
- bitrate: (a.bytesReceived - n.bytesReceived) * 8 / (t / 1e3),
752
- packetsReceived: a.packetsReceived - n.packetsReceived,
753
- packetsLost: a.packetsLost - n.packetsLost,
754
- framesDropped: a.framesDropped - n.framesDropped,
755
- framesDecoded: a.framesDecoded - n.framesDecoded,
756
- jitter: a.jitter,
757
- jitterBufferDelay: a.jitterBufferDelay - n.jitterBufferDelay,
758
- jitterBufferEmittedCount: a.jitterBufferEmittedCount - n.jitterBufferEmittedCount,
759
- avgJitterDelayInInterval: (a.jitterBufferDelay - n.jitterBufferDelay) / (a.jitterBufferEmittedCount - n.jitterBufferEmittedCount),
760
- framesPerSecond: a.framesPerSecond,
761
- freezeCount: a.freezeCount - n.freezeCount,
762
- freezeDuration: a.freezeDuration - n.freezeDuration
763
- } : {
764
- timestamp: a.timestamp,
765
- rtt: a.rtt,
766
- duration: 0,
767
- bytesReceived: a.bytesReceived,
768
- bitrate: a.bytesReceived * 8 / (t / 1e3),
769
- packetsReceived: a.packetsReceived,
770
- packetsLost: a.packetsLost,
771
- framesDropped: a.framesDropped,
772
- framesDecoded: a.framesDecoded,
773
- jitter: a.jitter,
774
- jitterBufferDelay: a.jitterBufferDelay,
775
- jitterBufferEmittedCount: a.jitterBufferEmittedCount,
776
- avgJitterDelayInInterval: a.jitterBufferDelay / a.jitterBufferEmittedCount,
777
- framesPerSecond: a.framesPerSecond,
778
- freezeCount: a.freezeCount,
779
- freezeDuration: a.freezeDuration
780
- } : {
781
- timestamp: a.timestamp,
782
- duration: t * d / 1e3,
783
- rtt: a.rtt,
784
- bytesReceived: a.bytesReceived - e[d - 1].bytesReceived,
785
- bitrate: (a.bytesReceived - e[d - 1].bytesReceived) * 8 / (t / 1e3),
786
- packetsReceived: a.packetsReceived - e[d - 1].packetsReceived,
787
- packetsLost: a.packetsLost - e[d - 1].packetsLost,
788
- framesDropped: a.framesDropped - e[d - 1].framesDropped,
789
- framesDecoded: a.framesDecoded - e[d - 1].framesDecoded,
790
- jitter: a.jitter,
791
- jitterBufferDelay: a.jitterBufferDelay - e[d - 1].jitterBufferDelay,
792
- jitterBufferEmittedCount: a.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount,
793
- avgJitterDelayInInterval: (a.jitterBufferDelay - e[d - 1].jitterBufferDelay) / (a.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount),
794
- framesPerSecond: a.framesPerSecond,
795
- freezeCount: a.freezeCount - e[d - 1].freezeCount,
796
- freezeDuration: a.freezeDuration - e[d - 1].freezeDuration
797
- }), c = en(i), o = c.reduce((a, d) => a + (d.causes.includes("low fps") ? 1 : 0), 0), s = i.filter((a) => !!a.avgJitterDelayInInterval).map((a) => a.avgJitterDelayInInterval), r = i.filter((a) => !!a.rtt).map((a) => a.rtt);
798
- return {
799
- webRTCStats: {
800
- anomalies: c,
801
- avSync: $t(() => Yt(e), null),
802
- minRtt: Le(r),
803
- avgRtt: Pe(r),
804
- maxRtt: je(r),
805
- aggregateReport: Zt(e[0], e[e.length - 1], o),
806
- minJitterDelayInInterval: Le(s),
807
- maxJitterDelayInInterval: je(s),
808
- avgJitterDelayInInterval: Pe(s)
809
- },
810
- codec: e[0].codec,
811
- resolution: `${e[0].frameWidth}x${e[0].frameHeight}`
812
- };
813
- }
814
- function nt(e, t) {
815
- for (const n of e.values())
816
- if ((n == null ? void 0 : n.type) === "inbound-rtp" && n.kind === t)
817
- return n;
818
- return null;
819
- }
820
- const te = 10;
821
- function bn(e, t) {
822
- let n = !1, i = !1, c = null, o = 0, s = 0, r = {};
823
- async function a() {
824
- if (n) {
825
- try {
826
- const d = await e();
827
- if (!d) {
828
- c = setTimeout(a, te);
829
- return;
830
- }
831
- const m = nt(d, "audio");
832
- if (!m) {
833
- c = setTimeout(a, te);
834
- return;
835
- }
836
- const u = m.totalAudioEnergy ?? 0, f = m.totalSamplesReceived ?? 0;
837
- if (!i) {
838
- o = u, s = f, i = !0, c = setTimeout(a, te);
839
- return;
840
- }
841
- const p = u - o, v = f - s;
842
- if (o = u, s = f, v > 0 && p > 0) {
843
- n = !1, t(r);
844
- return;
845
- }
846
- } catch {
847
- }
848
- n && (c = setTimeout(a, te));
849
- }
850
- }
851
- return {
852
- arm(d = {}) {
853
- r = d, n = !0, i = !1, performance.now(), c !== null && clearTimeout(c), c = setTimeout(a, te);
854
- },
855
- destroy() {
856
- n = !1, c !== null && (clearTimeout(c), c = null);
857
- }
858
- };
859
- }
860
- const ue = 100, nn = Math.max(Math.ceil(400 / ue), 1), rn = 0.25, an = 0.28;
861
- function sn() {
862
- let e = 0, t, n, i = 0;
863
- return (c) => {
864
- const o = nt(c, "video");
865
- if (!o)
866
- return { isReceiving: !1, avgJitterDelayInInterval: i };
867
- const s = o.jitterBufferDelay, r = o.jitterBufferEmittedCount;
868
- if (n && r > n) {
869
- const m = s - t, u = r - n;
870
- i = m / u;
871
- }
872
- t = s, n = r;
873
- const a = o.framesDecoded, d = a - e > 0;
874
- return e = a, { isReceiving: d, avgJitterDelayInInterval: i, freezeCount: o.freezeCount };
875
- };
876
- }
877
- function on(e, t, n, i, c) {
878
- let o = null, s = [], r, a = 0, d = !1, m = ne.Unknown, u = ne.Unknown, f = 0, p = 0;
879
- const v = sn();
880
- async function B() {
881
- const D = await e();
882
- if (!D)
883
- return;
884
- const { isReceiving: b, avgJitterDelayInInterval: j, freezeCount: T } = v(D), Q = tn(D);
885
- if (b)
886
- a = 0, f = T - p, u = j < rn ? ne.Strong : j > an && f > 1 ? ne.Weak : m, u !== m && (c == null || c(u), m = u, p += f, f = 0), d || (i == null || i(k.Start), r = s[s.length - 1], s = [], d = !0), s.push(Q);
887
- else if (d && (a++, a >= nn)) {
888
- const H = Fe(s, ue, r);
889
- i == null || i(k.Stop, H), t() || n(), p = T, d = !1;
890
- }
891
- }
892
- return {
893
- start: () => {
894
- o || (o = setInterval(B, ue));
895
- },
896
- stop: () => {
897
- o && (clearInterval(o), o = null);
898
- },
899
- getReport: () => Fe(s, ue, r)
900
- };
901
- }
902
- const cn = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
903
- function Ne(e) {
904
- switch (e) {
905
- case "connected":
906
- return P.Connected;
907
- case "checking":
908
- return P.Connecting;
909
- case "failed":
910
- return P.Fail;
911
- case "new":
912
- return P.New;
913
- case "closed":
914
- return P.Closed;
915
- case "disconnected":
916
- return P.Disconnected;
917
- case "completed":
918
- return P.Completed;
919
- default:
920
- return P.New;
921
- }
922
- }
923
- const dn = (e) => (t) => {
924
- const [n, i = ""] = t.split(/:(.+)/);
925
- try {
926
- const c = JSON.parse(i);
927
- return e("parsed data channel message", { subject: n, data: c }), { subject: n, data: c };
928
- } catch (c) {
929
- return e("Failed to parse data channel message, returning data as string", { subject: n, rawData: i, error: c }), { subject: n, data: i };
930
- }
931
- };
932
- function ln({
933
- statsSignal: e,
934
- dataChannelSignal: t,
935
- onVideoStateChange: n,
936
- report: i,
937
- log: c
938
- }) {
939
- e === k.Start && t === k.Start ? (c("CALLBACK: onVideoStateChange(Start)"), n == null || n(k.Start)) : e === k.Stop && t === k.Stop && (c("CALLBACK: onVideoStateChange(Stop)"), n == null || n(k.Stop, i));
940
- }
941
- function un({
942
- statsSignal: e,
943
- dataChannelSignal: t,
944
- onVideoStateChange: n,
945
- onAgentActivityStateChange: i,
946
- report: c,
947
- log: o
948
- }) {
949
- e === k.Start ? (o("CALLBACK: onVideoStateChange(Start)"), n == null || n(k.Start)) : e === k.Stop && (o("CALLBACK: onVideoStateChange(Stop)"), n == null || n(k.Stop, c)), t === k.Start ? i == null || i(ae.Talking) : t === k.Stop && (i == null || i(ae.Idle));
950
- }
951
- function Ue({
952
- statsSignal: e,
953
- dataChannelSignal: t,
954
- onVideoStateChange: n,
955
- onAgentActivityStateChange: i,
956
- streamType: c,
957
- report: o,
958
- log: s
959
- }) {
960
- c === W.Legacy ? ln({ statsSignal: e, dataChannelSignal: t, onVideoStateChange: n, report: o, log: s }) : c === W.Fluent && un({
961
- statsSignal: e,
962
- dataChannelSignal: t,
963
- onVideoStateChange: n,
964
- onAgentActivityStateChange: i,
965
- report: o,
966
- log: s
967
- });
968
- }
969
- async function fn(e, t, { debug: n = !1, callbacks: i, auth: c, baseURL: o = fe, analytics: s }, r) {
970
- var I;
971
- const a = Xt(n, "WebRTCStreamingManager"), d = dn(a);
972
- let m = !1, u = !1, f = k.Stop, p = k.Stop;
973
- const { startConnection: v, sendStreamRequest: B, close: D, createStream: b, addIceCandidate: j } = Gt(
974
- c,
975
- o,
976
- e,
977
- i.onError
978
- ), {
979
- id: T,
980
- offer: Q,
981
- ice_servers: H,
982
- session_id: J,
983
- fluent: ie,
984
- interrupt_enabled: Y
985
- } = await b(t, r);
986
- (I = i.onStreamCreated) == null || I.call(i, { stream_id: T, session_id: J, agent_id: e });
987
- const M = new cn({ iceServers: H }), q = M.createDataChannel("JanusDataChannel");
988
- if (!J)
989
- throw new Error("Could not create session_id");
990
- const V = ie ? W.Fluent : W.Legacy;
991
- s.enrich({
992
- "stream-type": V
993
- });
994
- const se = t.stream_warmup && !ie, oe = () => m, l = () => {
995
- var h;
996
- m = !0, u && (a("CALLBACK: onConnectionStateChange(Connected)"), (h = i.onConnectionStateChange) == null || h.call(i, P.Connected));
997
- }, g = on(
998
- () => M.getStats(),
999
- oe,
1000
- l,
1001
- (h, w) => Ue({
1002
- statsSignal: p = h,
1003
- dataChannelSignal: V === W.Legacy ? f : void 0,
1004
- onVideoStateChange: i.onVideoStateChange,
1005
- onAgentActivityStateChange: i.onAgentActivityStateChange,
1006
- report: w,
1007
- streamType: V,
1008
- log: a
1009
- }),
1010
- (h) => {
1011
- var w;
1012
- return (w = i.onConnectivityStateChange) == null ? void 0 : w.call(i, h);
1013
- }
1014
- );
1015
- g.start(), M.onicecandidate = (h) => {
1016
- var w;
1017
- a("peerConnection.onicecandidate", h);
1018
- try {
1019
- h.candidate && h.candidate.sdpMid && h.candidate.sdpMLineIndex !== null ? j(
1020
- T,
1021
- {
1022
- candidate: h.candidate.candidate,
1023
- sdpMid: h.candidate.sdpMid,
1024
- sdpMLineIndex: h.candidate.sdpMLineIndex
1025
- },
1026
- J,
1027
- r
1028
- ) : j(T, { candidate: null }, J, r);
1029
- } catch (E) {
1030
- (w = i.onError) == null || w.call(i, E, { streamId: T });
1031
- }
1032
- }, q.onopen = () => {
1033
- u = !0, (!se || m) && l();
1034
- };
1035
- let y = null;
1036
- const C = (h) => {
1037
- var w;
1038
- y = h, (w = i.onVideoIdChange) == null || w.call(i, h);
1039
- };
1040
- function S(h, w) {
1041
- if (h === L.StreamStarted && typeof w == "object" && "metadata" in w) {
1042
- const E = w.metadata;
1043
- C(E.videoId);
1044
- }
1045
- h === L.StreamDone && C(null), f = h === L.StreamStarted ? k.Start : k.Stop, Ue({
1046
- statsSignal: V === W.Legacy ? p : void 0,
1047
- dataChannelSignal: f,
1048
- onVideoStateChange: i.onVideoStateChange,
1049
- onAgentActivityStateChange: i.onAgentActivityStateChange,
1050
- streamType: V,
1051
- log: a
1052
- });
1053
- }
1054
- function R(h, w) {
1055
- var _;
1056
- const E = typeof w == "string" ? w : w == null ? void 0 : w.metadata;
1057
- E && s.enrich({ streamMetadata: E }), (_ = i.onStreamReady) == null || _.call(i);
1058
- }
1059
- const $ = {
1060
- [L.StreamStarted]: S,
1061
- [L.StreamDone]: S,
1062
- [L.StreamReady]: R
1063
- };
1064
- q.onmessage = (h) => {
1065
- var _;
1066
- const { subject: w, data: E } = d(h.data);
1067
- (_ = $[w]) == null || _.call($, w, E);
1068
- }, M.oniceconnectionstatechange = () => {
1069
- var w;
1070
- a("peerConnection.oniceconnectionstatechange => " + M.iceConnectionState);
1071
- const h = Ne(M.iceConnectionState);
1072
- h !== P.Connected && ((w = i.onConnectionStateChange) == null || w.call(i, h, `webrtc:ice-${M.iceConnectionState}`));
1073
- }, M.ontrack = (h) => {
1074
- var w;
1075
- a("peerConnection.ontrack", h), a("CALLBACK: onSrcObjectReady"), (w = i.onSrcObjectReady) == null || w.call(i, h.streams[0]);
1076
- }, await M.setRemoteDescription(Q), a("set remote description OK");
1077
- const F = await M.createAnswer();
1078
- a("create answer OK"), await M.setLocalDescription(F), a("set local description OK"), await v(T, F, J, r), a("start connection OK");
1079
- function N(h) {
1080
- var w, E;
1081
- if (!m || q.readyState !== "open") {
1082
- a("Data channel is not ready for sending messages"), (w = i.onError) == null || w.call(i, new Oe("Data channel is not ready for sending messages"), {
1083
- streamId: T
1084
- });
1085
- return;
1086
- }
1087
- try {
1088
- q.send(h);
1089
- } catch (_) {
1090
- a("Error sending data channel message", _), (E = i.onError) == null || E.call(i, _, { streamId: T });
1091
- }
1092
- }
1093
- return {
1094
- /**
1095
- * Method to send request to server to get clip or talk depend on you payload
1096
- * @param payload
1097
- */
1098
- speak(h) {
1099
- return B(T, J, h);
1100
- },
1101
- /**
1102
- * Method to close RTC connection
1103
- */
1104
- async disconnect() {
1105
- var h;
1106
- if (T) {
1107
- const w = Ne(M.iceConnectionState);
1108
- if (M) {
1109
- if (w === P.New) {
1110
- g.stop();
1111
- return;
1112
- }
1113
- M.close(), M.oniceconnectionstatechange = null, M.onnegotiationneeded = null, M.onicecandidate = null, M.ontrack = null;
1114
- }
1115
- try {
1116
- w === P.Connected && await D(T, J).catch((E) => {
1117
- });
1118
- } catch (E) {
1119
- a("Error on close stream connection", E);
1120
- }
1121
- (h = i.onAgentActivityStateChange) == null || h.call(i, ae.Idle), g.stop();
1122
- }
1123
- },
1124
- sendDataChannelMessage: N,
1125
- /**
1126
- * Session identifier information, should be returned in the body of all streaming requests
1127
- */
1128
- sessionId: J,
1129
- /**
1130
- * Id of current RTC stream
1131
- */
1132
- streamId: T,
1133
- streamType: V,
1134
- interruptAvailable: Y ?? !1,
1135
- isInterruptible: !0,
1136
- interrupt(h) {
1137
- if (!Y)
1138
- throw new Error("Interrupt is not enabled for this stream");
1139
- if (V !== W.Fluent)
1140
- throw new Error("Interrupt only available for Fluent streams");
1141
- if (!y)
1142
- throw new Error("No active video to interrupt");
1143
- const w = {
1144
- type: L.StreamInterrupt,
1145
- videoId: y,
1146
- timestamp: Date.now()
1147
- };
1148
- N(JSON.stringify(w));
1149
- }
1150
- };
1151
- }
1152
- var Ce = /* @__PURE__ */ ((e) => (e.V1 = "v1", e.V2 = "v2", e))(Ce || {});
1153
- async function mn(e, t, n, i) {
1154
- const c = e.id;
1155
- switch (t.version) {
1156
- case "v1": {
1157
- const { version: o, ...s } = t;
1158
- return fn(c, s, n, i);
1159
- }
1160
- case "v2": {
1161
- const { version: o, ...s } = t;
1162
- switch (s.transport.provider) {
1163
- case Se.Livekit:
1164
- const { createLiveKitStreamingManager: r } = await import("./livekit-manager-C8H6vzgE.js");
1165
- return r(c, s, n);
1166
- default:
1167
- throw new Error(`Unsupported transport provider: ${s.transport.provider}`);
1168
- }
1169
- }
1170
- default:
1171
- throw new Error(`Invalid stream version: ${t.version}`);
1172
- }
1173
- }
1174
- const gn = "cht";
1175
- function hn() {
1176
- return {
1177
- transport: {
1178
- provider: Se.Livekit
1179
- }
1180
- };
1181
- }
1182
- function pn(e) {
1183
- var c, o;
1184
- const { streamOptions: t } = e ?? {}, n = ((c = e == null ? void 0 : e.mixpanelAdditionalProperties) == null ? void 0 : c.plan) !== void 0 ? {
1185
- plan: (o = e.mixpanelAdditionalProperties) == null ? void 0 : o.plan
1186
- } : void 0;
1187
- return { ...{
1188
- output_resolution: t == null ? void 0 : t.outputResolution,
1189
- session_timeout: t == null ? void 0 : t.sessionTimeout,
1190
- stream_warmup: t == null ? void 0 : t.streamWarmup,
1191
- compatibility_mode: t == null ? void 0 : t.compatibilityMode,
1192
- fluent: t == null ? void 0 : t.fluent
1193
- }, ...n && { end_user_data: n } };
1194
- }
1195
- function wn(e, t) {
1196
- return ke(e.avatar.type) ? { version: Ce.V2, ...hn() } : { version: Ce.V1, ...pn(t) };
1197
- }
1198
- function yn(e, t, n) {
1199
- n.track("agent-connection-state-change", { state: e, ...t && { reason: t } });
1200
- }
1201
- function vn(e, t, n, i, c) {
1202
- c === W.Fluent ? Cn(e, t, n, i, c) : Sn(e, t, n, i, c);
1203
- }
1204
- function Cn(e, t, n, i, c) {
1205
- e === k.Start ? i.track("stream-session", { event: "start", "stream-type": c }) : e === k.Stop && i.track("stream-session", {
1206
- event: "stop",
1207
- "stream-type": c,
1208
- ...n
1209
- });
1210
- }
1211
- function Je(e, t, n, i, c) {
1212
- e === k.Start ? n.linkTrack("agent-video", { event: "start", ...c, "stream-type": i }, "start", [
1213
- L.StreamVideoCreated
1214
- ]) : e === k.Stop && n.linkTrack(
1215
- "agent-video",
1216
- {
1217
- event: "stop",
1218
- "stream-type": i
1219
- },
1220
- "done",
1221
- [L.StreamVideoDone]
1222
- );
1223
- }
1224
- function Sn(e, t, n, i, c) {
1225
- e === k.Start ? i.linkTrack(
1226
- "agent-video",
1227
- { event: "start", latency: G.get(!0), "stream-type": c },
1228
- "start",
1229
- [L.StreamVideoCreated]
1230
- ) : e === k.Stop && i.linkTrack(
1231
- "agent-video",
1232
- {
1233
- event: "stop",
1234
- "stream-type": c,
1235
- ...n
1236
- },
1237
- "done",
1238
- [L.StreamVideoDone]
1239
- );
1240
- }
1241
- function kn(e, t, n) {
1242
- const i = {
1243
- call_id: t.call_id,
1244
- name: t.name
1245
- };
1246
- if (e === L.ToolCallStarted) {
1247
- n.track("agent-tool-call", { ...i, event: "started" });
1248
- return;
1249
- }
1250
- const c = t;
1251
- n.track("agent-tool-call", {
1252
- ...i,
1253
- event: e === L.ToolCallDone ? "done" : "error",
1254
- duration_ms: c.duration_ms,
1255
- extra_keys: c.extra ? Object.keys(c.extra).length : 0
1256
- });
1257
- }
1258
- function Ve(e, t, n, i) {
1259
- return G.reset(), Be.update(), new Promise(async (c, o) => {
1260
- try {
1261
- let s, r = !1;
1262
- const a = wn(e, t);
1263
- n.enrich({
1264
- "stream-version": a.version.toString()
1265
- });
1266
- let d = null;
1267
- const m = e.avatar.type === "expressive";
1268
- s = await mn(
1269
- e,
1270
- a,
1271
- {
1272
- ...t,
1273
- analytics: n,
1274
- callbacks: {
1275
- ...t.callbacks,
1276
- onConnectionStateChange: (u, f) => {
1277
- var p, v;
1278
- (v = (p = t.callbacks).onConnectionStateChange) == null || v.call(p, u), yn(u, f, n), u === P.Connected && (s ? c(s) : r = !0);
1279
- },
1280
- onVideoStateChange: (u, f) => {
1281
- var p, v;
1282
- (v = (p = t.callbacks).onVideoStateChange) == null || v.call(p, u), vn(
1283
- u,
1284
- e,
1285
- f,
1286
- n,
1287
- s.streamType
1288
- );
1289
- },
1290
- onAgentActivityStateChange: (u) => {
1291
- var f, p;
1292
- (p = (f = t.callbacks).onAgentActivityStateChange) == null || p.call(f, u), u === ae.Talking ? (ve.update(), d = (v) => {
1293
- Je(
1294
- k.Start,
1295
- e,
1296
- n,
1297
- s.streamType,
1298
- v
1299
- ), d = null;
1300
- }, m || d({ latency: G.get(!0) })) : (ve.reset(), d = null, Je(
1301
- k.Stop,
1302
- e,
1303
- n,
1304
- s.streamType
1305
- ));
1306
- },
1307
- onFirstAudioDetected: (u) => {
1308
- d == null || d(u);
1309
- },
1310
- onStreamReady: () => {
1311
- const u = Be.get(!0);
1312
- n.track("agent-chat", { event: "ready", latency: u });
1313
- },
1314
- onToolEvent: ((u, f) => {
1315
- var p, v;
1316
- (v = (p = t.callbacks).onToolEvent) == null || v.call(p, u, f), kn(u, f, n);
1317
- })
1318
- }
1319
- },
1320
- i
1321
- ), r && c(s);
1322
- } catch (s) {
1323
- o(s);
1324
- }
1325
- });
1326
- }
1327
- async function Mn(e, t, n, i, c) {
1328
- var m, u, f, p;
1329
- const o = async () => {
1330
- if (ke(e.avatar.type)) {
1331
- const v = await Ve(e, t, i), B = `${gn}_${v.sessionId}`, D = (/* @__PURE__ */ new Date()).toISOString();
1332
- return { chatResult: {
1333
- chatMode: x.Functional,
1334
- chat: {
1335
- id: B,
1336
- agent_id: e.id,
1337
- owner_id: e.owner_id ?? "",
1338
- created: D,
1339
- modified: D,
1340
- agent_id__created_at: D,
1341
- agent_id__modified_at: D,
1342
- chat_mode: x.Functional,
1343
- messages: []
1344
- }
1345
- }, streamingManager: v };
1346
- } else {
1347
- const v = new AbortController(), B = v.signal;
1348
- let D;
1349
- try {
1350
- const b = tt(
1351
- e,
1352
- n,
1353
- i,
1354
- t.mode,
1355
- t.persistentChat,
1356
- c
1357
- ), j = Ve(e, t, i, B).then((H) => (D = H, H)), [T, Q] = await Promise.all([b, j]);
1358
- return { chatResult: T, streamingManager: Q };
1359
- } catch (b) {
1360
- throw v.abort(), D && await D.disconnect().catch(() => {
1361
- }), b;
1362
- }
1363
- }
1364
- }, { chatResult: s, streamingManager: r } = await o(), { chat: a, chatMode: d } = s;
1365
- return d && t.mode !== void 0 && d !== t.mode && (t.mode = d, (u = (m = t.callbacks).onModeChange) == null || u.call(m, d), d !== x.Functional) ? ((p = (f = t.callbacks).onError) == null || p.call(f, new ot(d)), r == null || r.disconnect(), { chat: a }) : { chat: a, streamingManager: r };
1366
- }
1367
- async function En(e, t) {
1368
- var V, se, oe;
1369
- let n = !0;
1370
- const i = t.mixpanelKey || St, c = t.wsURL || Ct, o = t.baseURL || fe, s = t.mode || x.Functional, r = {
1371
- messages: [],
1372
- chatMode: s
1373
- }, a = Ut({
1374
- token: i,
1375
- agentId: e,
1376
- isEnabled: t.enableAnalytics ?? t.enableAnalitics,
1377
- externalId: t.externalId,
1378
- mixpanelAdditionalProperties: t.mixpanelAdditionalProperties
1379
- }), d = Date.now();
1380
- $e(() => {
1381
- a.track("agent-sdk", { event: "init" }, d);
1382
- });
1383
- const m = t.callbacks.onError;
1384
- t.callbacks.onError = (l, g) => {
1385
- a.track("agent-error", { error: ge(l) }), m == null || m(l, g);
1386
- };
1387
- const u = Et(t.auth, o, t.callbacks.onError, t.externalId), f = await u.getRuntimeById(e);
1388
- t.debug = t.debug || ((V = f == null ? void 0 : f.advanced_settings) == null ? void 0 : V.ui_debug_mode);
1389
- const p = ke(f.avatar.type);
1390
- a.enrich(At(f));
1391
- const { onMessage: v, clearQueue: B } = qt(a, r, t, f, () => {
1392
- var l, g, y;
1393
- (l = r.socketManager) == null || l.disconnect(), (y = (g = t.callbacks).onConnectionStateChange) == null || y.call(g, P.Disconnected);
1394
- });
1395
- r.messages = Jt(t.initialMessages), (oe = (se = t.callbacks).onNewMessage) == null || oe.call(se, [...r.messages], "answer");
1396
- const D = (l) => {
1397
- a.enrich({ videoId: l });
1398
- }, b = ({ type: l }) => {
1399
- var y, C, S, R;
1400
- if (!((y = r.streamingManager) != null && y.interruptAvailable) || !((C = r.streamingManager) != null && C.isInterruptible)) return;
1401
- const g = r.messages[r.messages.length - 1];
1402
- a.track("agent-video-interrupt", {
1403
- type: l || "click",
1404
- video_duration_to_interrupt: ve.get(!0),
1405
- message_duration_to_interrupt: G.get(!0)
1406
- }), g.interrupted = !0, (R = (S = t.callbacks).onNewMessage) == null || R.call(S, [...r.messages], "answer"), r.streamingManager.interrupt(l);
1407
- }, j = /* @__PURE__ */ new Map();
1408
- function T(l) {
1409
- return async (g) => {
1410
- const y = j.get(l);
1411
- if (!y)
1412
- throw new Error(`No handler registered for client tool: ${l}`);
1413
- try {
1414
- const C = JSON.parse(g.payload);
1415
- return await y(C);
1416
- } catch (C) {
1417
- throw new Error(`Client tool "${l}" failed: ${C.message}`);
1418
- }
1419
- };
1420
- }
1421
- function Q() {
1422
- var l, g, y, C;
1423
- for (const [S] of j)
1424
- (g = (l = r.streamingManager) == null ? void 0 : l.unregisterRpcMethod) == null || g.call(l, S), (C = (y = r.streamingManager) == null ? void 0 : y.registerRpcMethod) == null || C.call(y, S, T(S));
1425
- }
1426
- function H(l, g) {
1427
- var C, S;
1428
- const y = !j.has(l);
1429
- j.set(l, g), y && ((S = (C = r.streamingManager) == null ? void 0 : C.registerRpcMethod) == null || S.call(C, l, T(l)));
1430
- }
1431
- function J(l) {
1432
- var g, y;
1433
- j.delete(l), (y = (g = r.streamingManager) == null ? void 0 : g.unregisterRpcMethod) == null || y.call(g, l);
1434
- }
1435
- const ie = Date.now();
1436
- $e(() => {
1437
- a.track("agent-sdk", { event: "loaded", ..._t(f) }, ie);
1438
- });
1439
- async function Y(l) {
1440
- var $, F, N, I, h, w, E;
1441
- wt(), (F = ($ = t.callbacks).onConnectionStateChange) == null || F.call($, P.Connecting), G.reset(), l && !n && (delete r.chat, (I = (N = t.callbacks).onNewMessage) == null || I.call(N, [...r.messages], "answer"));
1442
- const g = s === x.DirectPlayback || p ? Promise.resolve(void 0) : Ot(
1443
- t.auth,
1444
- c,
1445
- { onMessage: v, onError: t.callbacks.onError },
1446
- t.externalId
1447
- ), y = we(
1448
- () => Mn(
1449
- f,
1450
- {
1451
- ...t,
1452
- mode: s,
1453
- callbacks: {
1454
- ...t.callbacks,
1455
- onVideoIdChange: D,
1456
- onMessage: v
1457
- }
1458
- },
1459
- u,
1460
- a,
1461
- r.chat
1462
- ),
1463
- {
1464
- limit: 3,
1465
- timeout: yt,
1466
- timeoutErrorMessage: "Timeout initializing the stream",
1467
- shouldRetryFn: (_) => (_ == null ? void 0 : _.message) !== "Could not connect" && !(_ instanceof Ke && (_.status === 429 || _.kind === "InsufficientCreditsError")),
1468
- delayMs: 1e3
1469
- }
1470
- ).catch((_) => {
1471
- var A, K;
1472
- throw q(x.Maintenance), (K = (A = t.callbacks).onConnectionStateChange) == null || K.call(A, P.Fail), _;
1473
- }), [C, { streamingManager: S, chat: R }] = await Promise.all([g, y]);
1474
- R && R.id !== ((h = r.chat) == null ? void 0 : h.id) && ((E = (w = t.callbacks).onNewChat) == null || E.call(w, R.id)), r.streamingManager = S, r.socketManager = C, r.chat = R, Q(), n = !1, a.enrich({
1475
- chatId: R == null ? void 0 : R.id,
1476
- streamId: S == null ? void 0 : S.streamId,
1477
- mode: r.chatMode
1478
- }), q((R == null ? void 0 : R.chat_mode) ?? s);
1479
- }
1480
- async function M() {
1481
- var l, g, y, C;
1482
- (l = r.socketManager) == null || l.disconnect(), await ((g = r.streamingManager) == null ? void 0 : g.disconnect()), delete r.streamingManager, delete r.socketManager, (C = (y = t.callbacks).onConnectionStateChange) == null || C.call(y, P.Disconnected);
1483
- }
1484
- async function q(l) {
1485
- var g, y;
1486
- l !== r.chatMode && (a.track("agent-mode-change", { mode: l }), r.chatMode = l, r.chatMode !== x.Functional && await M(), (y = (g = t.callbacks).onModeChange) == null || y.call(g, l));
1487
- }
1488
- return {
1489
- agent: f,
1490
- getStreamType: () => {
1491
- var l;
1492
- return (l = r.streamingManager) == null ? void 0 : l.streamType;
1493
- },
1494
- getIsInterruptAvailable: () => {
1495
- var l;
1496
- return ((l = r.streamingManager) == null ? void 0 : l.interruptAvailable) ?? !1;
1497
- },
1498
- starterMessages: f.starter_message || [],
1499
- getSTTToken: () => u.getSTTToken(f.id),
1500
- changeMode: q,
1501
- enrichAnalytics: a.enrich,
1502
- async connect() {
1503
- await Y(!0), a.track("agent-chat", {
1504
- event: "connect",
1505
- mode: r.chatMode
1506
- });
1507
- },
1508
- async reconnect() {
1509
- const l = r.streamingManager;
1510
- let g;
1511
- if (p && (l != null && l.reconnect))
1512
- try {
1513
- await l.reconnect();
1514
- } catch (y) {
1515
- g = ge(y).kind, await M(), await Y(!1);
1516
- }
1517
- else
1518
- await M(), await Y(!1);
1519
- a.track("agent-chat", {
1520
- event: "reconnect",
1521
- mode: r.chatMode,
1522
- success: !0,
1523
- ...g && { fallbackReason: g }
1524
- });
1525
- },
1526
- async disconnect() {
1527
- await M(), a.track("agent-chat", {
1528
- event: "disconnect",
1529
- mode: r.chatMode
1530
- });
1531
- },
1532
- publishMicrophoneStream(l) {
1533
- var g;
1534
- return (g = r.streamingManager) != null && g.publishMicrophoneStream ? r.streamingManager.publishMicrophoneStream(l) : Promise.reject(new Error("publishMicrophoneStream is not available for this streaming manager"));
1535
- },
1536
- unpublishMicrophoneStream() {
1537
- var l;
1538
- return (l = r.streamingManager) != null && l.unpublishMicrophoneStream ? r.streamingManager.unpublishMicrophoneStream() : Promise.resolve();
1539
- },
1540
- replaceMicrophoneTrack(l) {
1541
- var g;
1542
- return (g = r.streamingManager) != null && g.replaceMicrophoneTrack ? r.streamingManager.replaceMicrophoneTrack(l) : Promise.reject(new Error("replaceMicrophoneTrack is not available for this streaming manager"));
1543
- },
1544
- publishCameraStream(l) {
1545
- var g;
1546
- return (g = r.streamingManager) != null && g.publishCameraStream ? r.streamingManager.publishCameraStream(l) : Promise.reject(new Error("publishCameraStream is not available for this streaming manager"));
1547
- },
1548
- unpublishCameraStream() {
1549
- var l;
1550
- return (l = r.streamingManager) != null && l.unpublishCameraStream ? r.streamingManager.unpublishCameraStream() : Promise.resolve();
1551
- },
1552
- async chat(l) {
1553
- var S, R, $, F, N;
1554
- const g = () => {
1555
- if (Xe(s))
1556
- throw new U(`${s} is enabled, chat is disabled`);
1557
- if (l.length >= Te)
1558
- throw new U(`Message cannot be more than ${Te} characters`);
1559
- if (l.length === 0)
1560
- throw new U("Message cannot be empty");
1561
- if (r.chatMode === x.Maintenance)
1562
- throw new U("Chat is in maintenance mode");
1563
- if (![x.TextOnly, x.Playground].includes(r.chatMode)) {
1564
- if (!r.streamingManager)
1565
- throw new U("Streaming manager is not initialized");
1566
- if (!r.chat)
1567
- throw new U("Chat is not initialized");
1568
- }
1569
- }, y = async () => {
1570
- var I, h;
1571
- if (!r.chat) {
1572
- const w = await tt(
1573
- f,
1574
- u,
1575
- a,
1576
- r.chatMode,
1577
- t.persistentChat
1578
- );
1579
- if (!w.chat)
1580
- throw new st(r.chatMode, !!t.persistentChat);
1581
- r.chat = w.chat, (h = (I = t.callbacks).onNewChat) == null || h.call(I, r.chat.id);
1582
- }
1583
- return r.chat.id;
1584
- }, C = async (I, h) => {
1585
- const w = r.chatMode === x.Playground;
1586
- return we(p && !w ? async () => {
1587
- var A, K;
1588
- return await ((K = (A = r.streamingManager) == null ? void 0 : A.sendTextMessage) == null ? void 0 : K.call(A, l)), Promise.resolve({});
1589
- } : async () => {
1590
- var A, K;
1591
- return u.chat(
1592
- f.id,
1593
- h,
1594
- {
1595
- chatMode: r.chatMode,
1596
- streamId: (A = r.streamingManager) == null ? void 0 : A.streamId,
1597
- sessionId: (K = r.streamingManager) == null ? void 0 : K.sessionId,
1598
- messages: I.map(({ matches: rt, ...ce }) => ce)
1599
- },
1600
- {
1601
- ...et(r.chatMode),
1602
- skipErrorHandler: !0
1603
- }
1604
- );
1605
- }, {
1606
- limit: 2,
1607
- shouldRetryFn: (A) => {
1608
- var ce, Re, De, Ie;
1609
- const K = (ce = A == null ? void 0 : A.message) == null ? void 0 : ce.includes("missing or invalid session_id");
1610
- return !((Re = A == null ? void 0 : A.message) == null ? void 0 : Re.includes("Stream Error")) && !K ? ((Ie = (De = t.callbacks).onError) == null || Ie.call(De, A), !1) : !0;
1611
- },
1612
- onRetry: async () => {
1613
- await M(), await Y(!1);
1614
- }
1615
- });
1616
- };
1617
- try {
1618
- B(), g(), r.messages.push({
1619
- id: ee(),
1620
- role: "user",
1621
- content: l,
1622
- parts: re(l),
1623
- created_at: new Date(G.update()).toISOString()
1624
- }), (R = (S = t.callbacks).onNewMessage) == null || R.call(S, [...r.messages], "user");
1625
- const I = await y(), h = await C([...r.messages], I);
1626
- return h.result && r.messages.push({
1627
- id: ee(),
1628
- role: "assistant",
1629
- content: h.result,
1630
- parts: re(h.result),
1631
- created_at: (/* @__PURE__ */ new Date()).toISOString(),
1632
- context: h.context,
1633
- matches: h.matches
1634
- }), a.track("agent-message-send", {
1635
- event: "success",
1636
- messages: r.messages.length + 1
1637
- }), h.result && ((F = ($ = t.callbacks).onNewMessage) == null || F.call($, [...r.messages], "answer"), a.track("agent-message-received", {
1638
- latency: G.get(!0),
1639
- messages: r.messages.length
1640
- })), h;
1641
- } catch (I) {
1642
- throw ((N = r.messages[r.messages.length - 1]) == null ? void 0 : N.role) === "assistant" && r.messages.pop(), a.track("agent-message-send", {
1643
- event: "error",
1644
- messages: r.messages.length,
1645
- error: ge(I)
1646
- }), I;
1647
- }
1648
- },
1649
- rate(l, g, y) {
1650
- var R, $, F, N;
1651
- const C = r.messages.find((I) => I.id === l);
1652
- if (r.chat) {
1653
- if (!C)
1654
- throw new U("Message not found");
1655
- } else throw new U("Chat is not initialized");
1656
- const S = ((R = C.matches) == null ? void 0 : R.map((I) => [I.document_id, I.id])) ?? [];
1657
- return a.track("agent-rate", {
1658
- event: y ? "update" : "create",
1659
- thumb: g === 1 ? "up" : "down",
1660
- knowledge_id: (($ = f.knowledge) == null ? void 0 : $.id) ?? "",
1661
- matches: S,
1662
- score: g
1663
- }), y ? u.updateRating(f.id, r.chat.id, y, {
1664
- knowledge_id: ((F = f.knowledge) == null ? void 0 : F.id) ?? "",
1665
- message_id: l,
1666
- matches: S,
1667
- score: g
1668
- }) : u.createRating(f.id, r.chat.id, {
1669
- knowledge_id: ((N = f.knowledge) == null ? void 0 : N.id) ?? "",
1670
- message_id: l,
1671
- matches: S,
1672
- score: g
1673
- });
1674
- },
1675
- deleteRate(l) {
1676
- if (!r.chat)
1677
- throw new U("Chat is not initialized");
1678
- return a.track("agent-rate-delete", { type: "text" }), u.deleteRating(f.id, r.chat.id, l);
1679
- },
1680
- submitFeedback(l, g) {
1681
- if (!r.chat)
1682
- throw new U("Chat is not initialized");
1683
- return a.track("agent-feedback", { rating: l, hasAnswer: !!g }), u.submitFeedback(f.id, r.chat.id, { rating: l, answer: g });
1684
- },
1685
- async speak(l) {
1686
- var S, R, $;
1687
- function g() {
1688
- return typeof l == "string" ? {
1689
- type: "text",
1690
- input: l,
1691
- ssml: !1
1692
- } : l.type === "text" && !l.provider ? {
1693
- type: "text",
1694
- input: l.input,
1695
- ssml: l.ssml,
1696
- should_queue_speaks: l.should_queue_speaks,
1697
- sentiment: l.sentiment
1698
- } : l;
1699
- }
1700
- const y = g();
1701
- if (a.track("agent-speak", y), G.update(), r.messages && y.type === "text" && (r.messages.push({
1702
- id: ee(),
1703
- role: "assistant",
1704
- content: y.input,
1705
- parts: re(y.input),
1706
- created_at: (/* @__PURE__ */ new Date()).toISOString()
1707
- }), (R = (S = t.callbacks).onNewMessage) == null || R.call(S, [...r.messages], "answer")), Mt(r.chatMode))
1708
- return {
1709
- duration: 0,
1710
- video_id: "",
1711
- status: "success"
1712
- };
1713
- if (!r.streamingManager)
1714
- throw new U("Please connect to the agent first");
1715
- return r.streamingManager.speak({
1716
- script: y,
1717
- metadata: { chat_id: ($ = r.chat) == null ? void 0 : $.id, agent_id: f.id }
1718
- });
1719
- },
1720
- interrupt: b,
1721
- registerClientTool: H,
1722
- unregisterClientTool: J
1723
- };
1724
- }
1725
- export {
1726
- ae as A,
1727
- X as B,
1728
- z as C,
1729
- gt as D,
1730
- Ke as H,
1731
- mt as K,
1732
- dt as N,
1733
- ht as P,
1734
- ut as R,
1735
- L as S,
1736
- Se as T,
1737
- U as V,
1738
- lt as W,
1739
- Xt as a,
1740
- W as b,
1741
- Qe as c,
1742
- fe as d,
1743
- Oe as e,
1744
- P as f,
1745
- ne as g,
1746
- on as h,
1747
- bn as i,
1748
- k as j,
1749
- st as k,
1750
- G as l,
1751
- x as m,
1752
- Tn as n,
1753
- ot as o,
1754
- zt as p,
1755
- ft as q,
1756
- We as r,
1757
- pt as s,
1758
- ge as t,
1759
- En as u,
1760
- Dn as v,
1761
- In as w,
1762
- It as x
1763
- };