@d-id/client-sdk 1.1.62 → 1.1.63-staging.290

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,2032 +1,29 @@
1
- var Et = Object.defineProperty;
2
- var kt = (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) => kt(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 Mt 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 || {}), k = /* @__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))(k || {}), 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
- [k.ChatAnswer]: X.Answer,
403
- [k.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 Me = 100, fn = Math.max(Math.ceil(400 / Me), 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: M } = P(B), Y = un(B);
605
- if (F)
606
- t = 0, l = M - 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, Me, c);
609
- r == null || r(E.Stop, Q), n() || i(), v = M, d = !1;
610
- }
611
- }
612
- return {
613
- start: () => {
614
- a || (a = setInterval(N, Me));
615
- },
616
- stop: () => {
617
- a && (clearInterval(a), a = null);
618
- },
619
- getReport: () => rt(o, Me, 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, M = 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
- m != null && m.isLocal && (oe.update(), te === W.Talking && (te = W.Idle));
677
- }
678
- try {
679
- await l.connect(re, H), r("LiveKit room joined successfully"), U = setTimeout(() => {
680
- var u;
681
- r(
682
- `Track subscription timeout - no track subscribed within ${it / 1e3} seconds after connect`
683
- ), U = null, g.track("connectivity-error", {
684
- error: "Track subscription timeout",
685
- sessionId: K
686
- }), (u = t.onError) == null || u.call(t, new Error("Track subscription timeout"), { sessionId: K }), Le("internal:track-subscription-timeout");
687
- }, it);
688
- } catch (u) {
689
- Be(u, r, t);
690
- }
691
- g.enrich({
692
- "stream-type": P
693
- });
694
- function f(u) {
695
- var m, p, S, _;
696
- switch (r("Connection state changed:", u), u) {
697
- case o.Connecting:
698
- r("CALLBACK: onConnectionStateChange(Connecting)"), (m = t.onConnectionStateChange) == null || m.call(t, x.Connecting, "livekit:connecting");
699
- break;
700
- case o.Connected:
701
- r("LiveKit room connected successfully"), v = !0;
702
- break;
703
- case o.Disconnected:
704
- 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");
705
- break;
706
- case o.Reconnecting:
707
- r("LiveKit room reconnecting..."), (S = t.onConnectionStateChange) == null || S.call(t, x.Connecting, "livekit:reconnecting");
708
- break;
709
- case o.SignalReconnecting:
710
- r("LiveKit room signal reconnecting..."), (_ = t.onConnectionStateChange) == null || _.call(t, x.Connecting, "livekit:signal-reconnecting");
711
- break;
712
- }
713
- }
714
- function C(u, m) {
715
- var p;
716
- r("Connection quality:", u), m != null && m.isLocal && ((p = t.onConnectivityStateChange) == null || p.call(t, wn[u]));
717
- }
718
- function R(u) {
719
- r("Participant connected:", u.identity);
720
- }
721
- function A(u) {
722
- r("Participant disconnected:", u.identity), Le("livekit:participant-disconnected");
723
- }
724
- function D() {
725
- var u;
726
- Y !== E.Start && (r("CALLBACK: onVideoStateChange(Start)"), Y = E.Start, (u = t.onVideoStateChange) == null || u.call(t, E.Start));
727
- }
728
- function b(u) {
729
- var m;
730
- Y !== E.Stop && (r("CALLBACK: onVideoStateChange(Stop)"), Y = E.Stop, (m = t.onVideoStateChange) == null || m.call(t, E.Stop, u));
731
- }
732
- function J(u, m, p) {
733
- var _, L, q;
734
- r(`Track subscribed: ${u.kind} from ${p.identity}`);
735
- const S = u.mediaStreamTrack;
736
- if (!S) {
737
- r(`No mediaStreamTrack available for ${u.kind}`);
738
- return;
739
- }
740
- 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" && (M = ln(
741
- () => u.getRTCStatsReport(),
742
- ({ sttLatency: V, serviceLatency: ie }) => {
743
- var Oe, We, He;
744
- const ye = oe.get(!0);
745
- let ve = 0;
746
- if (V) {
747
- const qe = ((We = (Oe = I == null ? void 0 : I.getReport()) == null ? void 0 : Oe.webRTCStats) == null ? void 0 : We.avgRtt) ?? 0;
748
- ve = qe > 0 ? Math.round(qe * 1e3) : 0;
749
- }
750
- const fe = ye > 0 ? ye + (V ?? 0) + ve : void 0, ce = fe !== void 0 && ie !== void 0 ? fe - ie : void 0;
751
- (He = t.onFirstAudioDetected) == null || He.call(t, { latency: fe, networkLatency: ce });
752
- }
753
- )), 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(
754
- () => u.getRTCStatsReport(),
755
- () => v,
756
- Vt,
757
- (V, ie) => {
758
- r(`Video state change: ${V}`), V === E.Start ? (U && (clearTimeout(U), U = null, r("Track subscription timeout cleared")), D()) : V === E.Stop && b(ie);
759
- }
760
- ), I.start());
761
- }
762
- function w(u, m, p) {
763
- r(`Track unsubscribed: ${u.kind} from ${p.identity}`), u.kind === "audio" && (M == null || M.destroy(), M = null), u.kind === "video" && (b(I == null ? void 0 : I.getReport()), I == null || I.stop(), I = null);
764
- }
765
- function y(u, m) {
766
- var S;
767
- const p = sn[u];
768
- p && ((S = t.onMessage) == null || S.call(t, p, { event: p, ...m }));
769
- }
770
- function T(u, m) {
771
- var p, S, _, L;
772
- if (u === k.ToolCallStarted) {
773
- te = W.ToolActive, (p = t.onAgentActivityStateChange) == null || p.call(t, W.ToolActive), (S = t.onToolEvent) == null || S.call(t, k.ToolCallStarted, m);
774
- return;
775
- }
776
- if (u === k.ToolCallDone) {
777
- (_ = t.onToolEvent) == null || _.call(t, k.ToolCallDone, m);
778
- return;
779
- }
780
- u === k.ToolCallError && ((L = t.onToolEvent) == null || L.call(t, k.ToolCallError, m));
781
- }
782
- function j(u, m) {
783
- var p, S, _, L, q;
784
- if (ne = ((p = m.metadata) == null ? void 0 : p.interruptible) !== !1, (S = t.onInterruptibleChange) == null || S.call(t, ne), u === k.StreamVideoCreated) {
785
- te = W.Talking, (_ = t.onAgentActivityStateChange) == null || _.call(t, W.Talking), M == null || M.arm({
786
- sttLatency: (L = m == null ? void 0 : m.stt) == null ? void 0 : L.latency,
787
- serviceLatency: m == null ? void 0 : m.serviceLatency
788
- });
789
- return;
790
- }
791
- ne && (te = W.Idle, (q = t.onAgentActivityStateChange) == null || q.call(t, W.Idle));
792
- }
793
- function G(u, m) {
794
- var L, q, V, ie;
795
- 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 };
796
- i.debug && ((V = m == null ? void 0 : m.metadata) != null && V.sentiment) && (_.sentiment = {
797
- id: m.metadata.sentiment.id,
798
- name: m.metadata.sentiment.sentiment
799
- }), (ie = t.onMessage) == null || ie.call(t, u, _), j(u, m);
800
- }
801
- function he(u, m) {
802
- var p;
803
- (p = t.onMessage) == null || p.call(t, X.Transcribe, { event: X.Transcribe, ...m }), queueMicrotask(() => {
804
- var S;
805
- (S = t.onAgentActivityStateChange) == null || S.call(t, W.Loading);
806
- });
807
- }
808
- const Z = {
809
- [k.ChatAnswer]: y,
810
- [k.ChatPartial]: y,
811
- [k.ToolCallStarted]: T,
812
- [k.ToolCallDone]: T,
813
- [k.ToolCallError]: T,
814
- [k.StreamVideoCreated]: G,
815
- [k.StreamVideoDone]: G,
816
- [k.StreamVideoError]: G,
817
- [k.StreamVideoRejected]: G,
818
- [k.ChatAudioTranscribed]: he
819
- };
820
- function z(u, m, p, S) {
821
- const _ = new TextDecoder().decode(u);
822
- try {
823
- const L = JSON.parse(_), q = S || L.subject;
824
- if (r("Data received:", { subject: q, data: L }), !q) return;
825
- const V = Z[q];
826
- V == null || V(q, L);
827
- } catch (L) {
828
- r("Failed to parse data channel message:", L);
829
- }
830
- }
831
- function ee(u) {
832
- var m;
833
- r("Media devices error:", u), (m = t.onError) == null || m.call(t, new Error(Re), { sessionId: K });
834
- }
835
- function De(u) {
836
- var m;
837
- r("Encryption error:", u), (m = t.onError) == null || m.call(t, new Error(Re), { sessionId: K });
838
- }
839
- function le(u, m, p) {
840
- r("Track subscription failed:", { trackSid: u, participant: m, reason: p });
841
- }
842
- function ke(u, m, p) {
843
- for (const [S, _] of p)
844
- if (_.source === m && _.track) {
845
- const L = _.track.mediaStreamTrack;
846
- if (L === u || (L == null ? void 0 : L.id) === u.id)
847
- return _;
848
- }
849
- return null;
850
- }
851
- async function pe(u, m, p, S, _, L) {
852
- var ye, ve, fe;
853
- if (!v || !l)
854
- throw r(`Room is not connected, cannot publish ${S} stream`), new Error("Room is not connected");
855
- if (u.isPublishing) {
856
- r(`${S} publish already in progress, skipping`);
857
- return;
858
- }
859
- const q = p(m);
860
- if (q.length === 0)
861
- throw new Error(`No ${S} track found in the provided MediaStream`);
862
- const V = q[0], ie = ke(V, S, _());
863
- if (ie) {
864
- r(`${S} track is already published, skipping`, {
865
- trackId: V.id,
866
- publishedTrackId: (ve = (ye = ie.track) == null ? void 0 : ye.mediaStreamTrack) == null ? void 0 : ve.id
867
- }), u.publication = ie;
868
- return;
869
- }
870
- if ((fe = u.publication) != null && fe.track) {
871
- const ce = u.publication.track.mediaStreamTrack;
872
- ce !== V && (ce == null ? void 0 : ce.id) !== V.id && (r(`Unpublishing existing ${S} track before publishing new one`), await L());
873
- }
874
- r(`Publishing ${S} track from provided MediaStream`, { trackId: V.id }), u.isPublishing = !0;
875
- try {
876
- u.publication = await l.localParticipant.publishTrack(V, { source: S }), r(`${S} track published successfully`, { trackSid: u.publication.trackSid });
877
- } catch (ce) {
878
- throw r(`Failed to publish ${S} track:`, ce), ce;
879
- } finally {
880
- u.isPublishing = !1;
881
- }
882
- }
883
- async function we(u, m) {
884
- if (!(!u.publication || !u.publication.track))
885
- try {
886
- l && (await l.localParticipant.unpublishTrack(u.publication.track, !1), r(`${m} track unpublished`));
887
- } catch (p) {
888
- r(`Error unpublishing ${m} track:`, p);
889
- } finally {
890
- u.publication = null;
891
- }
892
- }
893
- async function vt(u) {
894
- return pe(
895
- B,
896
- u,
897
- (m) => m.getAudioTracks(),
898
- c.Source.Microphone,
899
- () => l.localParticipant.audioTrackPublications,
900
- Ae
901
- );
902
- }
903
- async function Ae() {
904
- return we(B, "Microphone");
905
- }
906
- async function Ct(u) {
907
- return pe(
908
- F,
909
- u,
910
- (m) => m.getVideoTracks(),
911
- c.Source.Camera,
912
- () => l.localParticipant.videoTrackPublications,
913
- _e
914
- );
915
- }
916
- async function _e() {
917
- return we(F, "Camera");
918
- }
919
- function St() {
920
- N && (N.getTracks().forEach((u) => u.stop()), N = null);
921
- }
922
- async function be(u, m) {
923
- var p, S;
924
- if (!v || !l) {
925
- r("Room is not connected for sending messages"), (p = t.onError) == null || p.call(t, new Error(Re), {
926
- sessionId: K
927
- });
928
- return;
929
- }
930
- try {
931
- await l.localParticipant.sendText(u, { topic: m }), r("Message sent successfully:", u);
932
- } catch (_) {
933
- r("Failed to send message:", _), (S = t.onError) == null || S.call(t, new Error(Re), { sessionId: K });
934
- }
935
- }
936
- async function Rt(u) {
937
- var m;
938
- try {
939
- const S = JSON.parse(u).topic;
940
- return be("", S);
941
- } catch (p) {
942
- r("Failed to send data channel message:", p), (m = t.onError) == null || m.call(t, new Error(Re), { sessionId: K });
943
- }
944
- }
945
- function Tt(u) {
946
- return be(
947
- u,
948
- "lk.chat"
949
- /* Chat */
950
- );
951
- }
952
- async function Le(u) {
953
- var m, p;
954
- U && (clearTimeout(U), U = null), M == null || M.destroy(), M = 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;
955
- }
956
- return {
957
- speak(u) {
958
- const m = typeof u == "string" ? u : JSON.stringify(u);
959
- return be(
960
- m,
961
- "did.speak"
962
- /* Speak */
963
- );
964
- },
965
- disconnect: () => Le("user:disconnect"),
966
- async reconnect() {
967
- var u, m;
968
- if ((l == null ? void 0 : l.state) === o.Connected) {
969
- r("Room is already connected");
970
- return;
971
- }
972
- if (!l || !re || !H)
973
- throw r("Cannot reconnect: missing room, URL or token"), new Error("Cannot reconnect: session not available");
974
- r("Reconnecting to LiveKit room, state:", l.state), Q = !1, (u = t.onConnectionStateChange) == null || u.call(t, x.Connecting, "user:reconnect");
975
- try {
976
- if (await l.connect(re, H), r("Room reconnected"), v = !0, l.remoteParticipants.size === 0) {
977
- if (r("Waiting for agent to join..."), !await new Promise((S) => {
978
- const _ = setTimeout(() => {
979
- l == null || l.off(a.ParticipantConnected, L), S(!1);
980
- }, 5e3), L = () => {
981
- clearTimeout(_), l == null || l.off(a.ParticipantConnected, L), S(!0);
982
- };
983
- l == null || l.on(a.ParticipantConnected, L);
984
- }))
985
- throw r("Agent did not join within timeout"), await l.disconnect(), new Error("Agent did not rejoin the room");
986
- r("Agent joined, reconnection successful");
987
- }
988
- } catch (p) {
989
- throw r("Failed to reconnect:", p), (m = t.onConnectionStateChange) == null || m.call(t, x.Fail, "user:reconnect-failed"), p;
990
- }
991
- },
992
- sendDataChannelMessage: Rt,
993
- sendTextMessage: Tt,
994
- publishMicrophoneStream: vt,
995
- unpublishMicrophoneStream: Ae,
996
- publishCameraStream: Ct,
997
- unpublishCameraStream: _e,
998
- registerRpcMethod(u, m) {
999
- l == null || l.registerRpcMethod(u, m);
1000
- },
1001
- unregisterRpcMethod(u) {
1002
- l == null || l.unregisterRpcMethod(u);
1003
- },
1004
- sessionId: K,
1005
- streamId: K,
1006
- streamType: P,
1007
- interruptAvailable: de,
1008
- isInterruptible: ne
1009
- };
1010
- }
1011
- const vn = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1012
- __proto__: null,
1013
- DataChannelTopic: Ue,
1014
- createLiveKitStreamingManager: yn,
1015
- handleInitError: Be
1016
- }, Symbol.toStringTag, { value: "Module" }));
1017
- function Cn(e, n, i) {
1018
- if (!e)
1019
- throw new Error("Please connect to the agent first");
1020
- if (!e.interruptAvailable)
1021
- throw new Error("Interrupt is not enabled for this stream");
1022
- if (n !== se.Fluent)
1023
- throw new Error("Interrupt only available for Fluent streams");
1024
- if (!i)
1025
- throw new Error("No active video to interrupt");
1026
- }
1027
- async function Sn(e, n) {
1028
- const i = {
1029
- type: k.StreamInterrupt,
1030
- videoId: n,
1031
- timestamp: Date.now()
1032
- };
1033
- e.sendDataChannelMessage(JSON.stringify(i));
1034
- }
1035
- async function Rn(e) {
1036
- const n = {
1037
- topic: Ue.Interrupt
1038
- };
1039
- e.sendDataChannelMessage(JSON.stringify(n));
1040
- }
1041
- function Tn(e) {
1042
- return new Promise((n, i) => {
1043
- 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))}`);
1044
- g.onmessage = c, g.onclose = d, g.onerror = (l) => {
1045
- console.error(l), h == null || h("Websocket failed to connect", l), i(l);
1046
- }, g.onopen = (l) => {
1047
- t == null || t(l), n(g);
1048
- };
1049
- });
1050
- }
1051
- async function En(e) {
1052
- const { retries: n = 1 } = e;
1053
- let i = null;
1054
- for (let r = 0; (i == null ? void 0 : i.readyState) !== WebSocket.OPEN; r++)
1055
- try {
1056
- i = await Tn(e);
1057
- } catch (s) {
1058
- if (r === n)
1059
- throw s;
1060
- await ut(r * 500);
1061
- }
1062
- return i;
1063
- }
1064
- async function kn(e, n, i, r) {
1065
- const s = i != null && i.onMessage ? [i.onMessage] : [], a = await En({
1066
- auth: e,
1067
- host: n,
1068
- externalId: r,
1069
- callbacks: {
1070
- onError: (o) => {
1071
- var c;
1072
- return (c = i.onError) == null ? void 0 : c.call(i, new Dt(o));
1073
- },
1074
- onMessage(o) {
1075
- const c = JSON.parse(o.data);
1076
- s.forEach((t) => t(c.event, c));
1077
- }
1078
- }
1079
- });
1080
- return {
1081
- socket: a,
1082
- disconnect: () => a.close(),
1083
- subscribeToEvents: (o) => s.push(o)
1084
- };
1085
- }
1086
- function Mn(e) {
1087
- if (e.answer !== void 0)
1088
- return e.answer;
1089
- let n = 0, i = "";
1090
- for (; n in e; )
1091
- i += e[n++];
1092
- return i;
1093
- }
1094
- function In(e, n, i) {
1095
- if (!e.content)
1096
- return;
1097
- const r = n.messages[n.messages.length - 1];
1098
- (r == null ? void 0 : r.role) === "assistant" && !r.interrupted && (r.interrupted = !0);
1099
- const s = {
1100
- id: e.id || `user-${Date.now()}`,
1101
- role: e.role,
1102
- content: e.content,
1103
- parts: Te(e.content),
1104
- created_at: e.created_at || (/* @__PURE__ */ new Date()).toISOString(),
1105
- transcribed: !0
1106
- };
1107
- n.messages.push(s), i == null || i([...n.messages], "user");
1108
- }
1109
- function Dn(e, n, i, r, s, a) {
1110
- if (e === X.Transcribe && n.content) {
1111
- In(n, r, s);
1112
- return;
1113
- }
1114
- if (!(e === X.Partial || e === X.Answer))
1115
- return;
1116
- const o = r.messages[r.messages.length - 1], c = n.id && (o == null ? void 0 : o.role) === "assistant" && o.id !== n.id;
1117
- let t;
1118
- if ((o == null ? void 0 : o.role) === "assistant" && !c)
1119
- t = o;
1120
- else if (!o || o.transcribed && o.role === "user" || c)
1121
- c && a(), t = {
1122
- id: n.id || `assistant-${Date.now()}`,
1123
- role: n.role || "assistant",
1124
- content: n.content || "",
1125
- parts: [],
1126
- created_at: n.created_at || (/* @__PURE__ */ new Date()).toISOString()
1127
- }, r.messages.push(t);
1128
- else
1129
- return;
1130
- const { content: d, sequence: h } = n;
1131
- e === X.Partial ? i[h] = d : i.answer = d;
1132
- const g = Mn(i);
1133
- (t.content !== g || e === X.Answer) && (t.content = g, t.parts = Te(g), s == null || s([...r.messages], e));
1134
- }
1135
- function An(e, n, i, r, s) {
1136
- let a = {};
1137
- const o = () => a = {};
1138
- let c = "answer";
1139
- const t = (d, h) => {
1140
- var g, l;
1141
- h === "user" && o(), c = h, (l = (g = i.callbacks).onNewMessage) == null || l.call(g, d, h);
1142
- };
1143
- return {
1144
- clearQueue: o,
1145
- onMessage: (d, h) => {
1146
- var g, l;
1147
- if ("content" in h) {
1148
- const v = d === k.ChatAnswer ? X.Answer : d === k.ChatAudioTranscribed ? X.Transcribe : d;
1149
- Dn(v, h, a, n, t, o), v === X.Answer && e.track("agent-message-received", {
1150
- content: h.content,
1151
- messages: n.messages.length,
1152
- mode: n.chatMode
1153
- });
1154
- } else {
1155
- const v = k, P = [v.StreamVideoDone, v.StreamVideoError, v.StreamVideoRejected], N = [v.StreamFailed, v.StreamVideoError, v.StreamVideoRejected], B = en(h, r, { mode: n.chatMode });
1156
- if (d = d, d === v.StreamVideoCreated && (e.linkTrack("agent-video", B, v.StreamVideoCreated, ["start"]), h.sentiment)) {
1157
- const F = n.messages[n.messages.length - 1];
1158
- if ((F == null ? void 0 : F.role) === "assistant") {
1159
- const I = { ...F, sentiment: h.sentiment };
1160
- n.messages[n.messages.length - 1] = I, t == null || t([...n.messages], c);
1161
- }
1162
- }
1163
- if (P.includes(d)) {
1164
- const F = d.split("/")[1];
1165
- N.includes(d) ? e.track("agent-video", { ...B, event: F }) : e.linkTrack("agent-video", { ...B, event: F }, d, ["done"]);
1166
- }
1167
- 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();
1168
- }
1169
- }
1170
- };
1171
- }
1172
- function _n(e, n, i, r) {
1173
- const s = ze(e, `${n}/agents/${i}`, r);
1174
- return {
1175
- createStream(a, o) {
1176
- return s.post("/streams", a, { signal: o });
1177
- },
1178
- startConnection(a, o, c, t) {
1179
- return s.post(
1180
- `/streams/${a}/sdp`,
1181
- {
1182
- session_id: c,
1183
- answer: o
1184
- },
1185
- { signal: t }
1186
- );
1187
- },
1188
- addIceCandidate(a, o, c, t) {
1189
- return s.post(
1190
- `/streams/${a}/ice`,
1191
- {
1192
- session_id: c,
1193
- ...o
1194
- },
1195
- { signal: t }
1196
- );
1197
- },
1198
- sendStreamRequest(a, o, c) {
1199
- return s.post(`/streams/${a}`, {
1200
- session_id: o,
1201
- ...c
1202
- });
1203
- },
1204
- close(a, o) {
1205
- return s.delete(`/streams/${a}`, { session_id: o });
1206
- }
1207
- };
1208
- }
1209
- const bn = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
1210
- function at(e) {
1211
- switch (e) {
1212
- case "connected":
1213
- return x.Connected;
1214
- case "checking":
1215
- return x.Connecting;
1216
- case "failed":
1217
- return x.Fail;
1218
- case "new":
1219
- return x.New;
1220
- case "closed":
1221
- return x.Closed;
1222
- case "disconnected":
1223
- return x.Disconnected;
1224
- case "completed":
1225
- return x.Completed;
1226
- default:
1227
- return x.New;
1228
- }
1229
- }
1230
- const Ln = (e) => (n) => {
1231
- const [i, r = ""] = n.split(/:(.+)/);
1232
- try {
1233
- const s = JSON.parse(r);
1234
- return e("parsed data channel message", { subject: i, data: s }), { subject: i, data: s };
1235
- } catch (s) {
1236
- return e("Failed to parse data channel message, returning data as string", { subject: i, rawData: r, error: s }), { subject: i, data: r };
1237
- }
1238
- };
1239
- function Pn({
1240
- statsSignal: e,
1241
- dataChannelSignal: n,
1242
- onVideoStateChange: i,
1243
- report: r,
1244
- log: s
1245
- }) {
1246
- 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));
1247
- }
1248
- function xn({
1249
- statsSignal: e,
1250
- dataChannelSignal: n,
1251
- onVideoStateChange: i,
1252
- onAgentActivityStateChange: r,
1253
- report: s,
1254
- log: a
1255
- }) {
1256
- 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));
1257
- }
1258
- function ot({
1259
- statsSignal: e,
1260
- dataChannelSignal: n,
1261
- onVideoStateChange: i,
1262
- onAgentActivityStateChange: r,
1263
- streamType: s,
1264
- report: a,
1265
- log: o
1266
- }) {
1267
- s === se.Legacy ? Pn({ statsSignal: e, dataChannelSignal: n, onVideoStateChange: i, report: a, log: o }) : s === se.Fluent && xn({
1268
- statsSignal: e,
1269
- dataChannelSignal: n,
1270
- onVideoStateChange: i,
1271
- onAgentActivityStateChange: r,
1272
- report: a,
1273
- log: o
1274
- });
1275
- }
1276
- async function $n(e, n, { debug: i = !1, callbacks: r, auth: s, baseURL: a = Ee, analytics: o }, c) {
1277
- var J;
1278
- const t = pt(i, "WebRTCStreamingManager"), d = Ln(t);
1279
- let h = !1, g = !1, l = E.Stop, v = E.Stop;
1280
- const { startConnection: P, sendStreamRequest: N, close: B, createStream: F, addIceCandidate: I } = _n(
1281
- s,
1282
- a,
1283
- e,
1284
- r.onError
1285
- ), {
1286
- id: M,
1287
- offer: Y,
1288
- ice_servers: Q,
1289
- session_id: U,
1290
- fluent: te,
1291
- interrupt_enabled: ne
1292
- } = await F(n, c);
1293
- (J = r.onStreamCreated) == null || J.call(r, { stream_id: M, session_id: U, agent_id: e });
1294
- const $ = new bn({ iceServers: Q }), K = $.createDataChannel("JanusDataChannel");
1295
- if (!U)
1296
- throw new Error("Could not create session_id");
1297
- const H = te ? se.Fluent : se.Legacy;
1298
- o.enrich({
1299
- "stream-type": H
1300
- });
1301
- const re = n.stream_warmup && !te, de = () => h, ue = () => {
1302
- var w;
1303
- h = !0, g && (t("CALLBACK: onConnectionStateChange(Connected)"), (w = r.onConnectionStateChange) == null || w.call(r, x.Connected));
1304
- }, f = yt(
1305
- () => $.getStats(),
1306
- de,
1307
- ue,
1308
- (w, y) => ot({
1309
- statsSignal: v = w,
1310
- dataChannelSignal: H === se.Legacy ? l : void 0,
1311
- onVideoStateChange: r.onVideoStateChange,
1312
- onAgentActivityStateChange: r.onAgentActivityStateChange,
1313
- report: y,
1314
- streamType: H,
1315
- log: t
1316
- }),
1317
- (w) => {
1318
- var y;
1319
- return (y = r.onConnectivityStateChange) == null ? void 0 : y.call(r, w);
1320
- }
1321
- );
1322
- f.start(), $.onicecandidate = (w) => {
1323
- var y;
1324
- t("peerConnection.onicecandidate", w);
1325
- try {
1326
- w.candidate && w.candidate.sdpMid && w.candidate.sdpMLineIndex !== null ? I(
1327
- M,
1328
- {
1329
- candidate: w.candidate.candidate,
1330
- sdpMid: w.candidate.sdpMid,
1331
- sdpMLineIndex: w.candidate.sdpMLineIndex
1332
- },
1333
- U,
1334
- c
1335
- ) : I(M, { candidate: null }, U, c);
1336
- } catch (T) {
1337
- (y = r.onError) == null || y.call(r, T, { streamId: M });
1338
- }
1339
- }, K.onopen = () => {
1340
- g = !0, (!re || h) && ue();
1341
- };
1342
- const C = (w) => {
1343
- var y;
1344
- (y = r.onVideoIdChange) == null || y.call(r, w);
1345
- };
1346
- function R(w, y) {
1347
- if (w === k.StreamStarted && typeof y == "object" && "metadata" in y) {
1348
- const T = y.metadata;
1349
- C(T.videoId);
1350
- }
1351
- w === k.StreamDone && C(null), l = w === k.StreamStarted ? E.Start : E.Stop, ot({
1352
- statsSignal: H === se.Legacy ? v : void 0,
1353
- dataChannelSignal: l,
1354
- onVideoStateChange: r.onVideoStateChange,
1355
- onAgentActivityStateChange: r.onAgentActivityStateChange,
1356
- streamType: H,
1357
- log: t
1358
- });
1359
- }
1360
- function A(w, y) {
1361
- var j;
1362
- const T = typeof y == "string" ? y : y == null ? void 0 : y.metadata;
1363
- T && o.enrich({ streamMetadata: T }), (j = r.onStreamReady) == null || j.call(r);
1364
- }
1365
- const D = {
1366
- [k.StreamStarted]: R,
1367
- [k.StreamDone]: R,
1368
- [k.StreamReady]: A
1369
- };
1370
- K.onmessage = (w) => {
1371
- var j;
1372
- const { subject: y, data: T } = d(w.data);
1373
- (j = D[y]) == null || j.call(D, y, T);
1374
- }, $.oniceconnectionstatechange = () => {
1375
- var y;
1376
- t("peerConnection.oniceconnectionstatechange => " + $.iceConnectionState);
1377
- const w = at($.iceConnectionState);
1378
- w !== x.Connected && ((y = r.onConnectionStateChange) == null || y.call(r, w));
1379
- }, $.ontrack = (w) => {
1380
- var y;
1381
- t("peerConnection.ontrack", w), t("CALLBACK: onSrcObjectReady"), (y = r.onSrcObjectReady) == null || y.call(r, w.streams[0]);
1382
- }, await $.setRemoteDescription(Y), t("set remote description OK");
1383
- const b = await $.createAnswer();
1384
- return t("create answer OK"), await $.setLocalDescription(b), t("set local description OK"), await P(M, b, U, c), t("start connection OK"), {
1385
- /**
1386
- * Method to send request to server to get clip or talk depend on you payload
1387
- * @param payload
1388
- */
1389
- speak(w) {
1390
- return N(M, U, w);
1391
- },
1392
- /**
1393
- * Method to close RTC connection
1394
- */
1395
- async disconnect() {
1396
- var w;
1397
- if (M) {
1398
- const y = at($.iceConnectionState);
1399
- if ($) {
1400
- if (y === x.New) {
1401
- f.stop();
1402
- return;
1403
- }
1404
- $.close(), $.oniceconnectionstatechange = null, $.onnegotiationneeded = null, $.onicecandidate = null, $.ontrack = null;
1405
- }
1406
- try {
1407
- y === x.Connected && await B(M, U).catch((T) => {
1408
- });
1409
- } catch (T) {
1410
- t("Error on close stream connection", T);
1411
- }
1412
- (w = r.onAgentActivityStateChange) == null || w.call(r, W.Idle), f.stop();
1413
- }
1414
- },
1415
- /**
1416
- * Method to send data channel messages to the server
1417
- */
1418
- sendDataChannelMessage(w) {
1419
- var y, T;
1420
- if (!h || K.readyState !== "open") {
1421
- 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"), {
1422
- streamId: M
1423
- });
1424
- return;
1425
- }
1426
- try {
1427
- K.send(w);
1428
- } catch (j) {
1429
- t("Error sending data channel message", j), (T = r.onError) == null || T.call(r, j, { streamId: M });
1430
- }
1431
- },
1432
- /**
1433
- * Session identifier information, should be returned in the body of all streaming requests
1434
- */
1435
- sessionId: U,
1436
- /**
1437
- * Id of current RTC stream
1438
- */
1439
- streamId: M,
1440
- streamType: H,
1441
- interruptAvailable: ne ?? !1,
1442
- isInterruptible: !0
1443
- };
1444
- }
1445
- var Fe = /* @__PURE__ */ ((e) => (e.V1 = "v1", e.V2 = "v2", e))(Fe || {});
1446
- async function jn(e, n, i, r) {
1447
- const s = e.id;
1448
- switch (n.version) {
1449
- case "v1": {
1450
- const { version: a, ...o } = n;
1451
- return $n(s, o, i, r);
1452
- }
1453
- case "v2": {
1454
- const { version: a, ...o } = n;
1455
- switch (o.transport.provider) {
1456
- case Ne.Livekit:
1457
- const { createLiveKitStreamingManager: c } = await Promise.resolve().then(() => vn);
1458
- return c(s, o, i);
1459
- default:
1460
- throw new Error(`Unsupported transport provider: ${o.transport.provider}`);
1461
- }
1462
- }
1463
- default:
1464
- throw new Error(`Invalid stream version: ${n.version}`);
1465
- }
1466
- }
1467
- const Bn = "cht";
1468
- function Fn() {
1469
- return {
1470
- transport: {
1471
- provider: Ne.Livekit
1472
- }
1473
- };
1474
- }
1475
- function Nn(e) {
1476
- var s, a;
1477
- const { streamOptions: n } = e ?? {}, i = ((s = e == null ? void 0 : e.mixpanelAdditionalProperties) == null ? void 0 : s.plan) !== void 0 ? {
1478
- plan: (a = e.mixpanelAdditionalProperties) == null ? void 0 : a.plan
1479
- } : void 0;
1480
- return { ...{
1481
- output_resolution: n == null ? void 0 : n.outputResolution,
1482
- session_timeout: n == null ? void 0 : n.sessionTimeout,
1483
- stream_warmup: n == null ? void 0 : n.streamWarmup,
1484
- compatibility_mode: n == null ? void 0 : n.compatibilityMode,
1485
- fluent: n == null ? void 0 : n.fluent
1486
- }, ...i && { end_user_data: i } };
1487
- }
1488
- function Kn(e, n) {
1489
- return Ke(e.presenter.type) ? { version: Fe.V2, ...Fn() } : { version: Fe.V1, ...Nn(n) };
1490
- }
1491
- function zn(e, n, i) {
1492
- i.track("agent-connection-state-change", { state: e, ...n && { reason: n } });
1493
- }
1494
- function Vn(e, n, i, r, s) {
1495
- s === se.Fluent ? Un(e, n, i, r, s) : Jn(e, n, i, r, s);
1496
- }
1497
- function Un(e, n, i, r, s) {
1498
- e === E.Start ? r.track("stream-session", { event: "start", "stream-type": s }) : e === E.Stop && r.track("stream-session", {
1499
- event: "stop",
1500
- is_greenscreen: n.presenter.type === "clip" && n.presenter.is_greenscreen,
1501
- background: n.presenter.type === "clip" && n.presenter.background,
1502
- "stream-type": s,
1503
- ...i
1504
- });
1505
- }
1506
- function st(e, n, i, r, s) {
1507
- e === E.Start ? i.linkTrack("agent-video", { event: "start", ...s, "stream-type": r }, "start", [
1508
- k.StreamVideoCreated
1509
- ]) : e === E.Stop && i.linkTrack(
1510
- "agent-video",
1511
- {
1512
- event: "stop",
1513
- is_greenscreen: n.presenter.type === "clip" && n.presenter.is_greenscreen,
1514
- background: n.presenter.type === "clip" && n.presenter.background,
1515
- "stream-type": r
1516
- },
1517
- "done",
1518
- [k.StreamVideoDone]
1519
- );
1520
- }
1521
- function Jn(e, n, i, r, s) {
1522
- e === E.Start ? r.linkTrack(
1523
- "agent-video",
1524
- { event: "start", latency: oe.get(!0), "stream-type": s },
1525
- "start",
1526
- [k.StreamVideoCreated]
1527
- ) : e === E.Stop && r.linkTrack(
1528
- "agent-video",
1529
- {
1530
- event: "stop",
1531
- is_greenscreen: n.presenter.type === "clip" && n.presenter.is_greenscreen,
1532
- background: n.presenter.type === "clip" && n.presenter.background,
1533
- "stream-type": s,
1534
- ...i
1535
- },
1536
- "done",
1537
- [k.StreamVideoDone]
1538
- );
1539
- }
1540
- function ct(e, n, i, r) {
1541
- return oe.reset(), nt.update(), new Promise(async (s, a) => {
1542
- try {
1543
- let o, c = !1;
1544
- const t = Kn(e, n);
1545
- i.enrich({
1546
- "stream-version": t.version.toString()
1547
- });
1548
- let d = null;
1549
- const h = e.presenter.type === "expressive";
1550
- o = await jn(
1551
- e,
1552
- t,
1553
- {
1554
- ...n,
1555
- analytics: i,
1556
- callbacks: {
1557
- ...n.callbacks,
1558
- onConnectionStateChange: (g, l) => {
1559
- var v, P;
1560
- (P = (v = n.callbacks).onConnectionStateChange) == null || P.call(v, g), zn(g, l, i), g === x.Connected && (o ? s(o) : c = !0);
1561
- },
1562
- onVideoStateChange: (g, l) => {
1563
- var v, P;
1564
- (P = (v = n.callbacks).onVideoStateChange) == null || P.call(v, g), Vn(
1565
- g,
1566
- e,
1567
- l,
1568
- i,
1569
- o.streamType
1570
- );
1571
- },
1572
- onAgentActivityStateChange: (g) => {
1573
- var l, v;
1574
- (v = (l = n.callbacks).onAgentActivityStateChange) == null || v.call(l, g), g === W.Talking ? (je.update(), d = (P) => {
1575
- st(
1576
- E.Start,
1577
- e,
1578
- i,
1579
- o.streamType,
1580
- P
1581
- ), d = null;
1582
- }, h || d({ latency: oe.get(!0) })) : (je.reset(), d = null, st(
1583
- E.Stop,
1584
- e,
1585
- i,
1586
- o.streamType
1587
- ));
1588
- },
1589
- onFirstAudioDetected: (g) => {
1590
- d == null || d(g);
1591
- },
1592
- onStreamReady: () => {
1593
- const g = nt.get(!0);
1594
- i.track("agent-chat", { event: "ready", latency: g });
1595
- }
1596
- }
1597
- },
1598
- r
1599
- ), c && s(o);
1600
- } catch (o) {
1601
- a(o);
1602
- }
1603
- });
1604
- }
1605
- async function On(e, n, i, r, s) {
1606
- var h, g, l, v;
1607
- const a = async () => {
1608
- if (Ke(e.presenter.type)) {
1609
- const P = await ct(e, n, r), N = `${Bn}_${P.sessionId}`, B = (/* @__PURE__ */ new Date()).toISOString();
1610
- return { chatResult: {
1611
- chatMode: O.Functional,
1612
- chat: {
1613
- id: N,
1614
- agent_id: e.id,
1615
- owner_id: e.owner_id ?? "",
1616
- created: B,
1617
- modified: B,
1618
- agent_id__created_at: B,
1619
- agent_id__modified_at: B,
1620
- chat_mode: O.Functional,
1621
- messages: []
1622
- }
1623
- }, streamingManager: P };
1624
- } else {
1625
- const P = new AbortController(), N = P.signal;
1626
- let B;
1627
- try {
1628
- const F = ht(
1629
- e,
1630
- i,
1631
- r,
1632
- n.mode,
1633
- n.persistentChat,
1634
- s
1635
- ), I = ct(e, n, r, N).then((Q) => (B = Q, Q)), [M, Y] = await Promise.all([F, I]);
1636
- return { chatResult: M, streamingManager: Y };
1637
- } catch (F) {
1638
- throw P.abort(), B && await B.disconnect().catch(() => {
1639
- }), F;
1640
- }
1641
- }
1642
- }, { chatResult: o, streamingManager: c } = await a(), { chat: t, chatMode: d } = o;
1643
- 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 };
1644
- }
1645
- async function qn(e, n) {
1646
- var H, re, de, ue;
1647
- let i = !0, r = null;
1648
- const s = n.mixpanelKey || zt, a = n.wsURL || Kt, o = n.baseURL || Ee, c = n.mode || O.Functional, t = {
1649
- messages: [],
1650
- chatMode: c
1651
- }, d = nn({
1652
- token: s,
1653
- agentId: e,
1654
- isEnabled: n.enableAnalitics,
1655
- externalId: n.externalId,
1656
- mixpanelAdditionalProperties: n.mixpanelAdditionalProperties
1657
- }), h = Date.now();
1658
- tt(() => {
1659
- d.track("agent-sdk", { event: "init" }, h);
1660
- });
1661
- const g = Xt(n.auth, o, n.callbacks.onError, n.externalId), l = await g.getById(e);
1662
- n.debug = n.debug || ((H = l == null ? void 0 : l.advanced_settings) == null ? void 0 : H.ui_debug_mode);
1663
- const v = Ke(l.presenter.type);
1664
- d.enrich(Gt(l));
1665
- const { onMessage: P, clearQueue: N } = An(d, t, n, l, () => {
1666
- var f, C, R;
1667
- (f = t.socketManager) == null || f.disconnect(), (R = (C = n.callbacks).onConnectionStateChange) == null || R.call(C, x.Disconnected);
1668
- });
1669
- t.messages = an(n.initialMessages), (de = (re = n.callbacks).onNewMessage) == null || de.call(re, [...t.messages], "answer");
1670
- const B = (f) => {
1671
- r = f;
1672
- }, F = ({ type: f }) => {
1673
- var R, A, D, b, J;
1674
- if (!((R = t.streamingManager) != null && R.interruptAvailable) || !((A = t.streamingManager) != null && A.isInterruptible)) return;
1675
- const C = t.messages[t.messages.length - 1];
1676
- d.track("agent-video-interrupt", {
1677
- type: f || "click",
1678
- video_duration_to_interrupt: je.get(!0),
1679
- message_duration_to_interrupt: oe.get(!0)
1680
- }), 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));
1681
- }, I = /* @__PURE__ */ new Map();
1682
- function M(f) {
1683
- return async (C) => {
1684
- const R = I.get(f);
1685
- if (!R)
1686
- throw new Error(`No handler registered for client tool: ${f}`);
1687
- try {
1688
- const A = JSON.parse(C.payload);
1689
- return await R(A);
1690
- } catch (A) {
1691
- throw new Error(`Client tool "${f}" failed: ${A.message}`);
1692
- }
1693
- };
1694
- }
1695
- function Y() {
1696
- var f, C, R, A;
1697
- for (const [D] of I)
1698
- (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, M(D));
1699
- }
1700
- function Q(f, C) {
1701
- var A, D;
1702
- const R = !I.has(f);
1703
- I.set(f, C), R && ((D = (A = t.streamingManager) == null ? void 0 : A.registerRpcMethod) == null || D.call(A, f, M(f)));
1704
- }
1705
- function U(f) {
1706
- var C, R;
1707
- I.delete(f), (R = (C = t.streamingManager) == null ? void 0 : C.unregisterRpcMethod) == null || R.call(C, f);
1708
- }
1709
- const te = Date.now();
1710
- tt(() => {
1711
- d.track("agent-sdk", { event: "loaded", ...Yt(l) }, te);
1712
- });
1713
- async function ne(f) {
1714
- var J, w, y, T, j, G, he;
1715
- (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"));
1716
- const C = c === O.DirectPlayback || v ? Promise.resolve(void 0) : kn(
1717
- n.auth,
1718
- a,
1719
- { onMessage: P, onError: n.callbacks.onError },
1720
- n.externalId
1721
- ), R = xe(
1722
- () => On(
1723
- l,
1724
- {
1725
- ...n,
1726
- mode: c,
1727
- callbacks: {
1728
- ...n.callbacks,
1729
- onVideoIdChange: B,
1730
- onMessage: P
1731
- }
1732
- },
1733
- g,
1734
- d,
1735
- t.chat
1736
- ),
1737
- {
1738
- limit: 3,
1739
- timeout: Ft,
1740
- timeoutErrorMessage: "Timeout initializing the stream",
1741
- shouldRetryFn: (Z) => (Z == null ? void 0 : Z.message) !== "Could not connect" && Z.status !== 429 && (Z == null ? void 0 : Z.message) !== "InsufficientCreditsError",
1742
- delayMs: 1e3
1743
- }
1744
- ).catch((Z) => {
1745
- var z, ee;
1746
- throw K(O.Maintenance), (ee = (z = n.callbacks).onConnectionStateChange) == null || ee.call(z, x.Fail), Z;
1747
- }), [A, { streamingManager: D, chat: b }] = await Promise.all([C, R]);
1748
- 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({
1749
- chatId: b == null ? void 0 : b.id,
1750
- streamId: D == null ? void 0 : D.streamId,
1751
- mode: t.chatMode
1752
- }), K((b == null ? void 0 : b.chat_mode) ?? c);
1753
- }
1754
- async function $() {
1755
- var f, C, R, A;
1756
- (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);
1757
- }
1758
- async function K(f) {
1759
- var C, R;
1760
- 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));
1761
- }
1762
- return {
1763
- agent: l,
1764
- getStreamType: () => {
1765
- var f;
1766
- return (f = t.streamingManager) == null ? void 0 : f.streamType;
1767
- },
1768
- getIsInterruptAvailable: () => {
1769
- var f;
1770
- return ((f = t.streamingManager) == null ? void 0 : f.interruptAvailable) ?? !1;
1771
- },
1772
- starterMessages: ((ue = l.knowledge) == null ? void 0 : ue.starter_message) || [],
1773
- getSTTToken: () => g.getSTTToken(l.id),
1774
- changeMode: K,
1775
- enrichAnalytics: d.enrich,
1776
- async connect() {
1777
- await ne(!0), d.track("agent-chat", {
1778
- event: "connect",
1779
- mode: t.chatMode
1780
- });
1781
- },
1782
- async reconnect() {
1783
- const f = t.streamingManager;
1784
- if (v && (f != null && f.reconnect)) {
1785
- try {
1786
- await f.reconnect(), d.track("agent-chat", {
1787
- event: "reconnect",
1788
- mode: t.chatMode
1789
- });
1790
- } catch {
1791
- await $(), await ne(!1);
1792
- }
1793
- return;
1794
- }
1795
- await $(), await ne(!1), d.track("agent-chat", {
1796
- event: "reconnect",
1797
- mode: t.chatMode
1798
- });
1799
- },
1800
- async disconnect() {
1801
- await $(), d.track("agent-chat", {
1802
- event: "disconnect",
1803
- mode: t.chatMode
1804
- });
1805
- },
1806
- async publishMicrophoneStream(f) {
1807
- var C;
1808
- if (!((C = t.streamingManager) != null && C.publishMicrophoneStream))
1809
- throw new Error("publishMicrophoneStream is not available for this streaming manager");
1810
- return t.streamingManager.publishMicrophoneStream(f);
1811
- },
1812
- async unpublishMicrophoneStream() {
1813
- var f;
1814
- if ((f = t.streamingManager) != null && f.unpublishMicrophoneStream)
1815
- return t.streamingManager.unpublishMicrophoneStream();
1816
- },
1817
- async publishCameraStream(f) {
1818
- var C;
1819
- if (!((C = t.streamingManager) != null && C.publishCameraStream))
1820
- throw new Error("publishCameraStream is not available for this streaming manager");
1821
- return t.streamingManager.publishCameraStream(f);
1822
- },
1823
- async unpublishCameraStream() {
1824
- var f;
1825
- if ((f = t.streamingManager) != null && f.unpublishCameraStream)
1826
- return t.streamingManager.unpublishCameraStream();
1827
- },
1828
- async chat(f) {
1829
- var D, b, J, w, y;
1830
- const C = () => {
1831
- if (ft(c))
1832
- throw new me(`${c} is enabled, chat is disabled`);
1833
- if (f.length >= 800)
1834
- throw new me("Message cannot be more than 800 characters");
1835
- if (f.length === 0)
1836
- throw new me("Message cannot be empty");
1837
- if (t.chatMode === O.Maintenance)
1838
- throw new me("Chat is in maintenance mode");
1839
- if (![O.TextOnly, O.Playground].includes(t.chatMode)) {
1840
- if (!t.streamingManager)
1841
- throw new me("Streaming manager is not initialized");
1842
- if (!t.chat)
1843
- throw new me("Chat is not initialized");
1844
- }
1845
- }, R = async () => {
1846
- var T, j;
1847
- if (!t.chat) {
1848
- const G = await ht(
1849
- l,
1850
- g,
1851
- d,
1852
- t.chatMode,
1853
- n.persistentChat
1854
- );
1855
- if (!G.chat)
1856
- throw new Mt(t.chatMode, !!n.persistentChat);
1857
- t.chat = G.chat, (j = (T = n.callbacks).onNewChat) == null || j.call(T, t.chat.id);
1858
- }
1859
- return t.chat.id;
1860
- }, A = async (T, j) => {
1861
- const G = t.chatMode === O.Playground;
1862
- return xe(v && !G ? async () => {
1863
- var z, ee;
1864
- return await ((ee = (z = t.streamingManager) == null ? void 0 : z.sendTextMessage) == null ? void 0 : ee.call(z, f)), Promise.resolve({});
1865
- } : async () => {
1866
- var z, ee;
1867
- return g.chat(
1868
- l.id,
1869
- j,
1870
- {
1871
- chatMode: t.chatMode,
1872
- streamId: (z = t.streamingManager) == null ? void 0 : z.streamId,
1873
- sessionId: (ee = t.streamingManager) == null ? void 0 : ee.sessionId,
1874
- messages: T.map(({ matches: De, ...le }) => le)
1875
- },
1876
- {
1877
- ...gt(t.chatMode),
1878
- skipErrorHandler: !0
1879
- }
1880
- );
1881
- }, {
1882
- limit: 2,
1883
- shouldRetryFn: (z) => {
1884
- var le, ke, pe, we;
1885
- const ee = (le = z == null ? void 0 : z.message) == null ? void 0 : le.includes("missing or invalid session_id");
1886
- return !((ke = z == null ? void 0 : z.message) == null ? void 0 : ke.includes("Stream Error")) && !ee ? ((we = (pe = n.callbacks).onError) == null || we.call(pe, z), !1) : !0;
1887
- },
1888
- onRetry: async () => {
1889
- await $(), await ne(!1);
1890
- }
1891
- });
1892
- };
1893
- try {
1894
- N(), C(), t.messages.push({
1895
- id: ge(),
1896
- role: "user",
1897
- content: f,
1898
- parts: Te(f),
1899
- created_at: new Date(oe.update()).toISOString()
1900
- }), (b = (D = n.callbacks).onNewMessage) == null || b.call(D, [...t.messages], "user");
1901
- const T = await R(), j = await A([...t.messages], T);
1902
- return t.messages.push({
1903
- id: ge(),
1904
- role: "assistant",
1905
- content: j.result || "",
1906
- parts: Te(j.result || ""),
1907
- created_at: (/* @__PURE__ */ new Date()).toISOString(),
1908
- context: j.context,
1909
- matches: j.matches
1910
- }), d.track("agent-message-send", {
1911
- event: "success",
1912
- messages: t.messages.length + 1
1913
- }), j.result && ((w = (J = n.callbacks).onNewMessage) == null || w.call(J, [...t.messages], "answer"), d.track("agent-message-received", {
1914
- latency: oe.get(!0),
1915
- messages: t.messages.length
1916
- })), j;
1917
- } catch (T) {
1918
- throw ((y = t.messages[t.messages.length - 1]) == null ? void 0 : y.role) === "assistant" && t.messages.pop(), d.track("agent-message-send", {
1919
- event: "error",
1920
- messages: t.messages.length
1921
- }), T;
1922
- }
1923
- },
1924
- rate(f, C, R) {
1925
- var b, J, w, y;
1926
- const A = t.messages.find((T) => T.id === f);
1927
- if (t.chat) {
1928
- if (!A)
1929
- throw new Error("Message not found");
1930
- } else throw new Error("Chat is not initialized");
1931
- const D = ((b = A.matches) == null ? void 0 : b.map((T) => [T.document_id, T.id])) ?? [];
1932
- return d.track("agent-rate", {
1933
- event: R ? "update" : "create",
1934
- thumb: C === 1 ? "up" : "down",
1935
- knowledge_id: ((J = l.knowledge) == null ? void 0 : J.id) ?? "",
1936
- matches: D,
1937
- score: C
1938
- }), R ? g.updateRating(l.id, t.chat.id, R, {
1939
- knowledge_id: ((w = l.knowledge) == null ? void 0 : w.id) ?? "",
1940
- message_id: f,
1941
- matches: D,
1942
- score: C
1943
- }) : g.createRating(l.id, t.chat.id, {
1944
- knowledge_id: ((y = l.knowledge) == null ? void 0 : y.id) ?? "",
1945
- message_id: f,
1946
- matches: D,
1947
- score: C
1948
- });
1949
- },
1950
- deleteRate(f) {
1951
- if (!t.chat)
1952
- throw new Error("Chat is not initialized");
1953
- return d.track("agent-rate-delete", { type: "text" }), g.deleteRating(l.id, t.chat.id, f);
1954
- },
1955
- async speak(f) {
1956
- var D, b, J;
1957
- function C() {
1958
- if (typeof f == "string") {
1959
- if (!l.presenter.voice)
1960
- throw new Error("Presenter voice is not initialized");
1961
- return {
1962
- type: "text",
1963
- provider: l.presenter.voice,
1964
- input: f,
1965
- ssml: !1
1966
- };
1967
- }
1968
- if (f.type === "text" && !f.provider) {
1969
- if (!l.presenter.voice)
1970
- throw new Error("Presenter voice is not initialized");
1971
- return {
1972
- type: "text",
1973
- provider: l.presenter.voice,
1974
- input: f.input,
1975
- ssml: f.ssml
1976
- };
1977
- }
1978
- return f;
1979
- }
1980
- const R = C();
1981
- if (d.track("agent-speak", R), oe.update(), t.messages && R.type === "text" && (t.messages.push({
1982
- id: ge(),
1983
- role: "assistant",
1984
- content: R.input,
1985
- parts: Te(R.input),
1986
- created_at: (/* @__PURE__ */ new Date()).toISOString()
1987
- }), (b = (D = n.callbacks).onNewMessage) == null || b.call(D, [...t.messages], "answer")), Ot(t.chatMode))
1988
- return {
1989
- duration: 0,
1990
- video_id: "",
1991
- status: "success"
1992
- };
1993
- if (!t.streamingManager)
1994
- throw new Error("Please connect to the agent first");
1995
- return t.streamingManager.speak({
1996
- script: R,
1997
- metadata: { chat_id: (J = t.chat) == null ? void 0 : J.id, agent_id: l.id }
1998
- });
1999
- },
2000
- interrupt: F,
2001
- registerClientTool: Q,
2002
- unregisterClientTool: U
2003
- };
2004
- }
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";
2005
2
  export {
2006
- W as AgentActivityState,
2007
- bt as AgentStatus,
2008
- Mt as ChatCreationFailed,
2009
- O as ChatMode,
2010
- It as ChatModeDowngraded,
2011
- X as ChatProgress,
2012
- x as ConnectionState,
2013
- ae as ConnectivityState,
2014
- $t as DocumentType,
2015
- xt as KnowledgeType,
2016
- _t as PlanGroup,
2017
- jt as Providers,
2018
- Lt as RateState,
2019
- k as StreamEvents,
2020
- se as StreamType,
2021
- E as StreamingState,
2022
- Pt as Subject,
2023
- Ne as TransportProvider,
2024
- At as UserPlan,
2025
- me as ValidationError,
2026
- dt as VideoType,
2027
- Bt as VoiceAccess,
2028
- Dt as WsError,
2029
- qn as createAgentManager,
2030
- Hn as mapVideoType,
2031
- 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
2032
29
  };