@d-id/client-sdk 1.1.61 → 1.1.63-staging.289

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