@d-id/client-sdk 3.0.0-staging.415 → 3.0.0-staging.417

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