@d-id/client-sdk 1.1.22 → 1.1.23-staging.109

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