@d-id/client-sdk 1.1.11 → 1.1.12

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