@d-id/client-sdk 1.1.61 → 1.1.63-staging.289

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