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

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