@d-id/client-sdk 1.0.20 → 1.1.0-beta.1

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