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

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