@d-id/client-sdk 1.2.2-staging.347 → 1.2.3-staging.349

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