@d-id/client-sdk 1.0.19 → 1.0.29-beta.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,499 +1,536 @@
1
- const v = "https://api.d-id.com", J = "wss://notifications.d-id.com";
2
- function q(e) {
1
+ var b = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(b || {}), x = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(x || {}), A = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(A || {}), E = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e))(E || {}), T = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(T || {}), N = /* @__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))(N || {}), O = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(O || {}), B = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e))(B || {}), W = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(W || {}), D = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(D || {}), H = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(H || {}), F = /* @__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))(F || {}), I = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(I || {});
2
+ const y = "https://api.d-id.com", q = "wss://notifications.d-id.com", U = () => Math.random().toString(16).slice(2);
3
+ function j() {
4
+ let e = window.localStorage.getItem("did_external_key_id");
5
+ return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
6
+ }
7
+ let z = U();
8
+ function M(e) {
3
9
  if (e.type === "bearer")
4
10
  return `Bearer ${e.token}`;
5
11
  if (e.type === "basic")
6
12
  return `Basic ${btoa(`${e.username}:${e.password}`)}`;
7
13
  if (e.type === "key")
8
- return `Client-Key ${e.clientKey}`;
14
+ return `Client-Key ${e.clientKey}.${j()}_${z}`;
9
15
  throw new Error(`Unknown auth type: ${e}`);
10
16
  }
11
- function y(e, i = v) {
12
- const r = async (t, n) => {
13
- const a = await fetch(i + (t != null && t.startsWith("/") ? t : `/${t}`), {
14
- ...n,
17
+ function k(e, a = y) {
18
+ const n = async (t, r) => {
19
+ const i = await fetch(a + (t != null && t.startsWith("/") ? t : `/${t}`), {
20
+ ...r,
15
21
  headers: {
16
- ...n == null ? void 0 : n.headers,
17
- Authorization: q(e),
22
+ ...r == null ? void 0 : r.headers,
23
+ Authorization: M(e),
18
24
  "Content-Type": "application/json"
19
25
  }
20
26
  });
21
- if (!a.ok) {
22
- let c = await a.text().catch(() => "Failed to fetch");
23
- throw new Error(c);
27
+ if (!i.ok) {
28
+ let d = await i.text().catch(() => "Failed to fetch");
29
+ throw new Error(d);
24
30
  }
25
- return a.json();
31
+ return i.json();
26
32
  };
27
33
  return {
28
- get(t, n) {
29
- return r(t, {
30
- ...n,
34
+ get(t, r) {
35
+ return n(t, {
36
+ ...r,
31
37
  method: "GET"
32
38
  });
33
39
  },
34
- post(t, n, a) {
35
- return r(t, {
36
- ...a,
37
- body: JSON.stringify(n),
40
+ post(t, r, i) {
41
+ return n(t, {
42
+ ...i,
43
+ body: JSON.stringify(r),
38
44
  method: "POST"
39
45
  });
40
46
  },
41
- delete(t, n, a) {
42
- return r(t, {
43
- ...a,
44
- body: JSON.stringify(n),
47
+ delete(t, r, i) {
48
+ return n(t, {
49
+ ...i,
50
+ body: JSON.stringify(r),
45
51
  method: "DELETE"
46
52
  });
47
53
  },
48
- patch(t, n, a) {
49
- return r(t, {
50
- ...a,
51
- body: JSON.stringify(n),
54
+ patch(t, r, i) {
55
+ return n(t, {
56
+ ...i,
57
+ body: JSON.stringify(r),
52
58
  method: "PATCH"
53
59
  });
54
60
  }
55
61
  };
56
62
  }
57
- function T(e, i = v) {
58
- const r = y(e, `${i}/agents`);
63
+ function K(e, a = y) {
64
+ const n = k(e, `${a}/agents`);
59
65
  return {
60
- create(t, n) {
61
- return r.post("/", t, n);
66
+ create(t, r) {
67
+ return n.post("/", t, r);
62
68
  },
63
- getAgents(t, n) {
64
- return r.get(`/${t ? `?tag=${t}` : ""}`, n).then((a) => a ?? []);
69
+ getAgents(t, r) {
70
+ return n.get(`/${t ? `?tag=${t}` : ""}`, r).then((i) => i ?? []);
65
71
  },
66
- getById(t, n) {
67
- return r.get(`/${t}`, n);
72
+ getById(t, r) {
73
+ return n.get(`/${t}`, r);
68
74
  },
69
- delete(t, n) {
70
- return r.delete(`/${t}`, void 0, n);
75
+ delete(t, r) {
76
+ return n.delete(`/${t}`, void 0, r);
71
77
  },
72
- update(t, n, a) {
73
- return r.patch(`/${t}`, n, a);
78
+ update(t, r, i) {
79
+ return n.patch(`/${t}`, r, i);
74
80
  },
75
- newChat(t, n) {
76
- return r.post(`/${t}/chat`, void 0, n);
81
+ newChat(t, r) {
82
+ return n.post(`/${t}/chat`, void 0, r);
77
83
  },
78
- chat(t, n, a, c) {
79
- return r.post(`/${t}/chat/${n}`, a, c);
84
+ chat(t, r, i, d) {
85
+ return n.post(`/${t}/chat/${r}`, i, d);
80
86
  }
81
87
  };
82
88
  }
83
- function D(e, i = v) {
84
- const r = y(e, `${i}/knowledge`);
89
+ function J(e, a = y) {
90
+ const n = k(e, `${a}/knowledge`);
85
91
  return {
86
- createKnowledge(t, n) {
87
- return r.post("/", t, n);
92
+ createKnowledge(t, r) {
93
+ return n.post("/", t, r);
88
94
  },
89
95
  getKnowledgeBase(t) {
90
- return r.get("/", t);
96
+ return n.get("/", t);
91
97
  },
92
- getKnowledge(t, n) {
93
- return r.get(`/${t}`, n);
98
+ getKnowledge(t, r) {
99
+ return n.get(`/${t}`, r);
94
100
  },
95
- deleteKnowledge(t, n) {
96
- return r.delete(`/${t}`, void 0, n);
101
+ deleteKnowledge(t, r) {
102
+ return n.delete(`/${t}`, void 0, r);
97
103
  },
98
- createDocument(t, n, a) {
99
- return r.post(`/${t}/documents`, n, a);
104
+ createDocument(t, r, i) {
105
+ return n.post(`/${t}/documents`, r, i);
100
106
  },
101
- deleteDocument(t, n, a) {
102
- return r.delete(`/${t}/documents/${n}`, void 0, a);
107
+ deleteDocument(t, r, i) {
108
+ return n.delete(`/${t}/documents/${r}`, void 0, i);
103
109
  },
104
- getDocuments(t, n) {
105
- return r.get(`/${t}/documents`, n);
110
+ getDocuments(t, r) {
111
+ return n.get(`/${t}/documents`, r);
106
112
  },
107
- getDocument(t, n, a) {
108
- return r.get(`/${t}/documents/${n}`, a);
113
+ getDocument(t, r, i) {
114
+ return n.get(`/${t}/documents/${r}`, i);
109
115
  },
110
- getRecords(t, n, a) {
111
- return r.get(`/${t}/documents/${n}/records`, a);
116
+ getRecords(t, r, i) {
117
+ return n.get(`/${t}/documents/${r}/records`, i);
112
118
  },
113
- query(t, n, a) {
114
- return r.post(`/${t}/query`, {
115
- query: n
116
- }, a);
119
+ query(t, r, i) {
120
+ return n.post(`/${t}/query`, {
121
+ query: r
122
+ }, i);
117
123
  }
118
124
  };
119
125
  }
120
- function F(e, i = v) {
121
- const r = y(e, `${i}/chats/ratings`);
126
+ function Q(e, a = y) {
127
+ const n = k(e, `${a}/chats/ratings`);
122
128
  return {
123
- create(t, n) {
124
- return r.post("/", t, n);
129
+ create(t, r) {
130
+ return n.post("/", t, r);
125
131
  },
126
- getByKnowledge(t, n) {
127
- return r.get(`/${t}`, n).then((a) => a ?? []);
132
+ getByKnowledge(t, r) {
133
+ return n.get(`/${t}`, r).then((i) => i ?? []);
128
134
  },
129
- update(t, n, a) {
130
- return r.patch(`/${t}`, n, a);
135
+ update(t, r, i) {
136
+ return n.patch(`/${t}`, r, i);
131
137
  },
132
- delete(t, n) {
133
- return r.delete(`/${t}`, n);
138
+ delete(t, r) {
139
+ return n.delete(`/${t}`, r);
134
140
  }
135
141
  };
136
142
  }
137
- const V = (e) => new Promise((i) => setTimeout(i, e));
138
- function G(e) {
139
- return new Promise((i, r) => {
143
+ const V = (e) => new Promise((a) => setTimeout(a, e));
144
+ function X(e) {
145
+ return new Promise((a, n) => {
140
146
  const {
141
147
  callbacks: t,
142
- host: n,
143
- auth: a
148
+ host: r,
149
+ auth: i
144
150
  } = e, {
145
- onMessage: c = null,
146
- onOpen: d = null,
147
- onClose: f = null,
148
- onError: m = null
149
- } = t || {}, l = new WebSocket(`${n}?authorization=${q(a)}`);
150
- l.onmessage = c, l.onclose = f, l.onerror = (s) => {
151
- console.log(s), m == null || m(s), r(s);
152
- }, l.onopen = (s) => {
153
- d == null || d(s), i(l);
151
+ onMessage: d = null,
152
+ onOpen: g = null,
153
+ onClose: o = null,
154
+ onError: w = null
155
+ } = t || {}, l = new WebSocket(`${r}?authorization=${M(i)}`);
156
+ l.onmessage = d, l.onclose = o, l.onerror = (m) => {
157
+ console.error(m), w == null || w(m), n(m);
158
+ }, l.onopen = (m) => {
159
+ g == null || g(m), a(l);
154
160
  };
155
161
  });
156
162
  }
157
- async function Q(e) {
163
+ async function Y(e) {
158
164
  const {
159
- retries: i = 1
165
+ retries: a = 1
160
166
  } = e;
161
- let r = null;
162
- for (let t = 0; (r == null ? void 0 : r.readyState) !== WebSocket.OPEN; t++)
167
+ let n = null;
168
+ for (let t = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; t++)
163
169
  try {
164
- r = await G(e);
165
- } catch (n) {
166
- if (t === i)
167
- throw n;
170
+ n = await X(e);
171
+ } catch (r) {
172
+ if (t === a)
173
+ throw r;
168
174
  await V(t * 500);
169
175
  }
170
- return r;
176
+ return n;
171
177
  }
172
- async function X(e, i, r = J) {
173
- const t = i ? [i] : [], n = await Q({
178
+ async function Z(e, a, n) {
179
+ const t = n ? [n] : [], r = await Y({
174
180
  auth: e,
175
- host: r,
181
+ host: a,
176
182
  callbacks: {
177
- onMessage: (a) => {
178
- const c = JSON.parse(a.data);
179
- t.forEach((d) => d(c.event, c));
183
+ onMessage: (i) => {
184
+ const d = JSON.parse(i.data);
185
+ t.forEach((g) => g(d.event, d));
180
186
  }
181
187
  }
182
188
  });
183
189
  return {
184
- socket: n,
185
- terminate: () => n.close(),
186
- subscribeToEvents: (a) => t.push(a)
187
- };
188
- }
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 || {}), L = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(L || {}), j = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(j || {}), ee = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(ee || {}), 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 || {}), R = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(R || {});
190
- function ne(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
190
+ socket: r,
191
+ disconnect: () => r.close(),
192
+ subscribeToEvents: (i) => t.push(i)
198
193
  };
199
194
  }
200
- function x(e, i, r, t) {
201
- return new Promise(async (n, a) => {
202
- const c = await oe(ne(e), {
203
- ...i,
204
- callbacks: {
205
- ...i.callbacks,
206
- onConnectionStateChange: async (d) => {
207
- var f, m;
208
- d === "connected" ? (t || (t = await r.newChat(e.id)), n({
209
- chat: t,
210
- streamingManager: c
211
- })) : d === "failed" && a(new Error("Cannot create connection")), (m = (f = i.callbacks).onConnectionStateChange) == null || m.call(f, d);
212
- },
213
- // TODO remove when webscoket will return partial
214
- onMessage: (d, f) => {
215
- var m, l;
216
- d === A.ChatPartial && ((l = (m = i.callbacks).onChatEvents) == null || l.call(m, L.Partial, {
217
- content: f,
218
- event: L.Partial
219
- }));
220
- }
221
- }
222
- });
223
- });
224
- }
225
- async function ce(e, i) {
226
- const r = i.baseURL || v, t = new AbortController(), n = T(i.auth, r), a = F(i.auth, r), c = D(i.auth, r), d = await n.getById(e), f = await X(i.auth, i.callbacks.onChatEvents);
227
- let {
228
- chat: m,
229
- streamingManager: l
230
- } = await x(d, i, n);
231
- return {
232
- agent: d,
233
- async reconnectToChat() {
234
- const {
235
- streamingManager: s
236
- } = await x(d, i, n, m);
237
- l = s;
238
- },
239
- terminate() {
240
- return t.abort(), f.terminate(), l.terminate();
241
- },
242
- chatId: m.id,
243
- chat(s) {
244
- return n.chat(e, m.id, {
245
- sessionId: l.sessionId,
246
- streamId: l.streamId,
247
- messages: s
248
- }, {
249
- signal: t.signal
250
- });
251
- },
252
- rate(s, g) {
253
- return g ? a.update(g, s) : a.create(s);
254
- },
255
- deleteRate(s) {
256
- return a.delete(s);
257
- },
258
- speak(s) {
259
- let g;
260
- return s.type === "text" ? g = {
261
- script: {
262
- type: "text",
263
- provider: s.provider,
264
- input: s.input,
265
- ssml: s.ssml || !1
266
- }
267
- } : s.type === "audio" && (g = {
268
- script: {
269
- type: "audio",
270
- audio_url: s.audio_url
271
- }
272
- }), l.speak(g);
273
- },
274
- getStarterMessages() {
275
- var s, g;
276
- return (s = d.knowledge) != null && s.id ? c.getKnowledge((g = d.knowledge) == null ? void 0 : g.id).then((C) => (C == null ? void 0 : C.starter_message) || []) : Promise.resolve([]);
277
- }
278
- };
279
- }
280
- function re(e, i) {
281
- const r = y(e, i);
195
+ function G(e, a) {
196
+ const n = k(e, a);
282
197
  return {
283
198
  createStream(t) {
284
- return r.post("/clips/streams", {
199
+ return n.post("/clips/streams", {
285
200
  driver_id: t.driver_id,
286
201
  presenter_id: t.presenter_id,
287
- compatibility_mode: t.compatibility_mode
202
+ compatibility_mode: t.compatibility_mode,
203
+ idle_video_url: t == null ? void 0 : t.idle_video_url
288
204
  });
289
205
  },
290
- startConnection(t, n, a) {
291
- return r.post(`/clips/streams/${t}/sdp`, {
292
- session_id: a,
293
- answer: n
206
+ startConnection(t, r, i) {
207
+ return n.post(`/clips/streams/${t}/sdp`, {
208
+ session_id: i,
209
+ answer: r
294
210
  });
295
211
  },
296
- addIceCandidate(t, n, a) {
297
- return r.post(`/clips/streams/${t}/ice`, {
298
- session_id: a,
299
- ...n
212
+ addIceCandidate(t, r, i) {
213
+ return n.post(`/clips/streams/${t}/ice`, {
214
+ session_id: i,
215
+ ...r
300
216
  });
301
217
  },
302
- sendStreamRequest(t, n, a) {
303
- return r.post(`/clips/streams/${t}`, {
304
- session_id: n,
305
- ...a
218
+ sendStreamRequest(t, r, i) {
219
+ return n.post(`/clips/streams/${t}`, {
220
+ session_id: r,
221
+ ...i
306
222
  });
307
223
  },
308
- close(t, n) {
309
- return r.delete(`/clips/streams/${t}`, {
310
- session_id: n
224
+ close(t, r) {
225
+ return n.delete(`/clips/streams/${t}`, {
226
+ session_id: r
311
227
  });
312
228
  }
313
229
  };
314
230
  }
315
- function ae(e, i) {
316
- const r = y(e, i);
231
+ function ee(e, a) {
232
+ const n = k(e, a);
317
233
  return {
318
- createStream(t, n) {
319
- return r.post("/talks/streams", {
234
+ createStream(t, r) {
235
+ return n.post("/talks/streams", {
320
236
  source_url: t.source_url,
321
237
  driver_url: t.driver_url,
322
238
  face: t.face,
323
239
  config: t.config
324
- }, n);
325
- },
326
- startConnection(t, n, a, c) {
327
- return r.post(`/talks/streams/${t}/sdp`, {
328
- session_id: a,
329
- answer: n
330
- }, c);
331
- },
332
- addIceCandidate(t, n, a, c) {
333
- return r.post(`/talks/streams/${t}/ice`, {
334
- session_id: a,
335
- ...n
336
- }, c);
337
- },
338
- sendStreamRequest(t, n, a, c) {
339
- return r.post(`/talks/streams/${t}`, {
340
- session_id: n,
341
- ...a
342
- }, c);
343
- },
344
- close(t, n, a) {
345
- return r.delete(`/talks/streams/${t}`, {
346
- session_id: n
347
- }, a);
240
+ }, r);
241
+ },
242
+ startConnection(t, r, i, d) {
243
+ return n.post(`/talks/streams/${t}/sdp`, {
244
+ session_id: i,
245
+ answer: r
246
+ }, d);
247
+ },
248
+ addIceCandidate(t, r, i, d) {
249
+ return n.post(`/talks/streams/${t}/ice`, {
250
+ session_id: i,
251
+ ...r
252
+ }, d);
253
+ },
254
+ sendStreamRequest(t, r, i, d) {
255
+ return n.post(`/talks/streams/${t}`, {
256
+ session_id: r,
257
+ ...i
258
+ }, d);
259
+ },
260
+ close(t, r, i) {
261
+ return n.delete(`/talks/streams/${t}`, {
262
+ session_id: r
263
+ }, i);
348
264
  }
349
265
  };
350
266
  }
351
- function ie(e, i) {
352
- return e.map((r, t) => t === 0 ? i ? {
267
+ function te(e, a) {
268
+ return e.map((n, t) => t === 0 ? a ? {
353
269
  index: t,
354
- timestamp: r.timestamp,
355
- bytesReceived: r.bytesReceived - i.bytesReceived,
356
- packetsReceived: r.packetsReceived - i.packetsReceived,
357
- packetsLost: r.packetsLost - i.packetsLost,
358
- jitter: r.jitter,
359
- frameWidth: r.frameWidth,
360
- frameHeight: r.frameHeight,
361
- frameRate: r.frameRate
270
+ timestamp: n.timestamp,
271
+ bytesReceived: n.bytesReceived - a.bytesReceived,
272
+ packetsReceived: n.packetsReceived - a.packetsReceived,
273
+ packetsLost: n.packetsLost - a.packetsLost,
274
+ jitter: n.jitter,
275
+ frameWidth: n.frameWidth,
276
+ frameHeight: n.frameHeight,
277
+ framesPerSecond: n.framesPerSecond
362
278
  } : {
363
279
  index: t,
364
- timestamp: r.timestamp,
365
- bytesReceived: r.bytesReceived,
366
- packetsReceived: r.packetsReceived,
367
- packetsLost: r.packetsLost,
368
- jitter: r.jitter,
369
- frameWidth: r.frameWidth,
370
- frameHeight: r.frameHeight,
371
- frameRate: r.frameRate
280
+ timestamp: n.timestamp,
281
+ bytesReceived: n.bytesReceived,
282
+ packetsReceived: n.packetsReceived,
283
+ packetsLost: n.packetsLost,
284
+ jitter: n.jitter,
285
+ frameWidth: n.frameWidth,
286
+ frameHeight: n.frameHeight,
287
+ framesPerSecond: n.framesPerSecond
372
288
  } : {
373
289
  index: t,
374
- 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,
378
- jitter: r.jitter,
379
- frameWidth: r.frameWidth,
380
- frameHeight: r.frameHeight,
381
- frameRate: r.frameRate
290
+ timestamp: n.timestamp,
291
+ bytesReceived: n.bytesReceived - e[t - 1].bytesReceived,
292
+ packetsReceived: n.packetsReceived - e[t - 1].packetsReceived,
293
+ packetsLost: n.packetsLost - e[t - 1].packetsLost,
294
+ jitter: n.jitter,
295
+ frameWidth: n.frameWidth,
296
+ frameHeight: n.frameHeight,
297
+ framesPerSecond: n.framesPerSecond
382
298
  });
383
299
  }
384
- let N = !1;
385
- const h = (e, i) => N && console.log(e, i), se = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
386
- async function oe(e, {
387
- debug: i = !1,
388
- callbacks: r,
300
+ let P = !1;
301
+ const _ = (e, a) => P && console.log(e, a), ne = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
302
+ function re(e, a) {
303
+ let n = [], t = 0, r = 0, i;
304
+ return setInterval(() => {
305
+ e.getStats().then((g) => {
306
+ g.forEach((o) => {
307
+ if (o.type === "inbound-rtp" && o.kind === "video") {
308
+ if (r = n.length - 1, o && n[r]) {
309
+ const w = o.bytesReceived, l = n[r].bytesReceived;
310
+ let m = i;
311
+ i = w - l > 0;
312
+ let v;
313
+ if (m !== i) {
314
+ if (i)
315
+ t = n.length;
316
+ else {
317
+ const f = n.slice(t), u = t === 0 ? void 0 : n[t - 1];
318
+ v = te(f, u), v = v.sort((s, S) => S.packetsLost - s.packetsLost).slice(0, 5);
319
+ }
320
+ a == null || a(i ? A.Start : A.Stop, v);
321
+ }
322
+ }
323
+ n.push(o);
324
+ }
325
+ });
326
+ });
327
+ }, 500);
328
+ }
329
+ async function ie(e, {
330
+ debug: a = !1,
331
+ callbacks: n,
389
332
  auth: t,
390
- baseURL: n = v
333
+ baseURL: r = y
391
334
  }) {
392
- N = i;
393
- const a = {
394
- ...r
395
- }, {
396
- startConnection: c,
335
+ P = a;
336
+ const {
337
+ startConnection: i,
397
338
  sendStreamRequest: d,
398
- close: f,
399
- createStream: m,
400
- addIceCandidate: l
401
- } = e.videoType === R.Clip ? re(t, n) : ae(t, n), {
402
- id: s,
403
- offer: g,
404
- ice_servers: C,
405
- session_id: w
406
- } = await m(e), u = new se({
407
- iceServers: C
408
- }), b = u.createDataChannel("JanusDataChannel"), k = [];
409
- let $ = 0, E;
410
- if (!w)
339
+ close: g,
340
+ createStream: o,
341
+ addIceCandidate: w
342
+ } = e.videoType === I.Clip ? G(t, r) : ee(t, r), {
343
+ id: l,
344
+ offer: m,
345
+ ice_servers: v,
346
+ session_id: f
347
+ } = await o(e), u = new ne({
348
+ iceServers: v
349
+ }), s = u.createDataChannel("JanusDataChannel");
350
+ if (!f)
411
351
  throw new Error("Could not create session_id");
412
- u.onicecandidate = (o) => {
413
- h("peerConnection.onicecandidate", o), o.candidate && o.candidate.sdpMid && o.candidate.sdpMLineIndex !== null && l(s, {
414
- candidate: o.candidate.candidate,
415
- sdpMid: o.candidate.sdpMid,
416
- sdpMLineIndex: o.candidate.sdpMLineIndex
417
- }, w);
352
+ const S = re(u, n.onVideoStateChange);
353
+ u.onicecandidate = (c) => {
354
+ _("peerConnection.onicecandidate", c), c.candidate && c.candidate.sdpMid && c.candidate.sdpMLineIndex !== null && w(l, {
355
+ candidate: c.candidate.candidate,
356
+ sdpMid: c.candidate.sdpMid,
357
+ sdpMLineIndex: c.candidate.sdpMLineIndex
358
+ }, f);
418
359
  }, u.oniceconnectionstatechange = () => {
419
- var o;
420
- h("peerConnection.oniceconnectionstatechange => " + u.iceConnectionState), (o = a.onConnectionStateChange) == null || o.call(a, u.iceConnectionState);
421
- }, u.ontrack = (o) => {
360
+ var c;
361
+ _("peerConnection.oniceconnectionstatechange => " + u.iceConnectionState), (c = n.onConnectionStateChange) == null || c.call(n, u.iceConnectionState);
362
+ }, u.ontrack = (c) => {
422
363
  var p;
423
- h("peerConnection.ontrack", o), (p = a.onSrcObjectReady) == null || p.call(a, o.streams[0]);
424
- }, b.onmessage = (o) => {
425
- var p, I, H;
426
- if (b.readyState === "open") {
427
- const [M, U] = o.data.split(":");
428
- if (M === A.StreamStarted)
429
- $ = k.length, E = setInterval(() => {
430
- u.getStats().then((K) => {
431
- K.forEach((S) => {
432
- S.type === "inbound-rtp" && S.kind === "video" && k.push(S);
433
- });
434
- });
435
- }, 1e3), (p = a.onVideoStateChange) == null || p.call(a, _.Start);
436
- else if (M === A.StreamDone) {
437
- clearInterval(E);
438
- const P = k.slice($);
439
- if (P) {
440
- const K = $ === 0 ? void 0 : k[$ - 1], S = ie(P, K);
441
- $ = k.length, (I = a.onVideoStateChange) == null || I.call(a, _.Stop, S.sort((z, B) => B.packetsLost - z.packetsLost).slice(0, 5));
442
- }
443
- } else
444
- (H = a.onMessage) == null || H.call(a, M, decodeURIComponent(U));
364
+ _("peerConnection.ontrack", c), (p = n.onSrcObjectReady) == null || p.call(n, c.streams[0]);
365
+ }, s.onmessage = (c) => {
366
+ var p, R;
367
+ if (s.readyState === "open") {
368
+ const [h, $] = c.data.split(":");
369
+ h === E.StreamStarted ? console.log("StreamStarted", h, $) : h === E.StreamDone ? console.log("StreamDone") : h === E.StreamFailed ? ((p = n.onVideoStateChange) == null || p.call(n, A.Stop, {
370
+ event: h,
371
+ data: $
372
+ }), clearInterval(S), console.log("StreamFailed")) : (R = n.onMessage) == null || R.call(n, h, decodeURIComponent($));
445
373
  }
446
- }, await u.setRemoteDescription(g), h("set remote description OK");
447
- const W = await u.createAnswer();
448
- return h("create answer OK"), await u.setLocalDescription(W), h("set local description OK"), await c(s, W, w), h("start connection OK"), {
374
+ }, await u.setRemoteDescription(m), _("set remote description OK");
375
+ const C = await u.createAnswer();
376
+ return _("create answer OK"), await u.setLocalDescription(C), _("set local description OK"), await i(l, C, f), _("start connection OK"), {
449
377
  /**
450
378
  * Method to send request to server to get clip or talk depend on you payload
451
- * @param payload
379
+ * @param payload
452
380
  */
453
- speak(o) {
454
- return d(s, w, o);
381
+ speak(c) {
382
+ return d(l, f, c);
455
383
  },
456
384
  /**
457
385
  * Method to close RTC connection
458
386
  */
459
- async terminate() {
460
- var o, p;
461
- s && (u && (u.close(), u.oniceconnectionstatechange = null, u.onnegotiationneeded = null, u.onicecandidate = null, u.ontrack = null), await f(s, w).catch((I) => {
462
- }), (o = a.onConnectionStateChange) == null || o.call(a, "closed"), (p = a.onVideoStateChange) == null || p.call(a, _.Stop));
387
+ async disconnect() {
388
+ var c, p;
389
+ l && (u && (u.close(), u.oniceconnectionstatechange = null, u.onnegotiationneeded = null, u.onicecandidate = null, u.ontrack = null), await g(l, f).catch((R) => {
390
+ }), (c = n.onConnectionStateChange) == null || c.call(n, "closed"), (p = n.onVideoStateChange) == null || p.call(n, A.Stop), clearInterval(S));
463
391
  },
464
392
  /**
465
393
  * Session identifier information, should be returned in the body of all streaming requests
466
394
  */
467
- sessionId: w,
395
+ sessionId: f,
468
396
  /**
469
397
  * Id of current RTC stream
470
398
  */
471
- streamId: s,
472
- /**
473
- * Method to add callback that will be trigered on supported events
474
- * @param eventName
475
- * @param callback
476
- */
477
- onCallback(o, p) {
478
- a[o] = p;
399
+ streamId: l
400
+ };
401
+ }
402
+ function ae(e, a) {
403
+ var n;
404
+ return (n = e.knowledge) != null && n.id ? a.getKnowledge(e.knowledge.id).then((t) => (t == null ? void 0 : t.starter_message) || []) : [];
405
+ }
406
+ function se(e) {
407
+ var a;
408
+ return e.presenter.type === I.Clip ? {
409
+ videoType: I.Clip,
410
+ driver_id: e.presenter.driver_id,
411
+ presenter_id: e.presenter.presenter_id,
412
+ idle_video_url: (a = e.presenter) == null ? void 0 : a.idle_video
413
+ } : {
414
+ videoType: I.Talk,
415
+ source_url: e.presenter.source_url
416
+ };
417
+ }
418
+ function L(e, a, n, t) {
419
+ return new Promise(async (r, i) => {
420
+ const d = await ie(se(e), {
421
+ ...a,
422
+ callbacks: {
423
+ ...a.callbacks,
424
+ onConnectionStateChange: async (g) => {
425
+ var o, w;
426
+ if (g === "connected")
427
+ try {
428
+ t || (t = await n.newChat(e.id)), r({
429
+ chat: t,
430
+ streamingManager: d
431
+ });
432
+ } catch (l) {
433
+ console.error(l), i(new Error("Cannot create new chat"));
434
+ }
435
+ else
436
+ g === "failed" && i(new Error("Cannot create connection"));
437
+ (w = (o = a.callbacks).onConnectionStateChange) == null || w.call(o, g);
438
+ }
439
+ }
440
+ });
441
+ });
442
+ }
443
+ function oe(e, a, n) {
444
+ return K(a, n || y).getById(e);
445
+ }
446
+ async function de(e, a) {
447
+ var f, u;
448
+ const n = a.baseURL || y, t = a.wsURL || q, r = new AbortController(), i = K(a.auth, n), d = Q(a.auth, n), g = J(a.auth, n), o = typeof e == "string" ? await i.getById(e) : e;
449
+ (u = (f = a.callbacks) == null ? void 0 : f.onAgentReady) == null || u.call(f, o);
450
+ const w = await Z(a.auth, t, a.callbacks.onChatEvents);
451
+ let {
452
+ chat: l,
453
+ streamingManager: m
454
+ } = await L(o, a, i);
455
+ const v = await ae(o, g);
456
+ return {
457
+ agent: o,
458
+ chatId: l.id,
459
+ starterMessages: v,
460
+ async reconnect() {
461
+ const {
462
+ streamingManager: s
463
+ } = await L(o, a, i, l);
464
+ m = s;
465
+ },
466
+ disconnect() {
467
+ return r.abort(), w.disconnect(), m.disconnect();
468
+ },
469
+ chat(s) {
470
+ if (s.length === 0)
471
+ throw new Error("Messages cannot be empty");
472
+ return s[s.length - 1].created_at = (/* @__PURE__ */ new Date()).toISOString(), s[s.length - 1].role || (s[s.length - 1].role = "user"), i.chat(o.id, l.id, {
473
+ sessionId: m.sessionId,
474
+ streamId: m.streamId,
475
+ messages: s
476
+ }, {
477
+ signal: r.signal
478
+ });
479
+ },
480
+ rate(s, S, C) {
481
+ var p, R, h;
482
+ const c = ((p = S.matches) == null ? void 0 : p.map(($) => [$.document_id, $.id])) ?? [];
483
+ return C ? d.update(C, {
484
+ agent_id: o.id,
485
+ knowledge_id: ((R = o.knowledge) == null ? void 0 : R.id) ?? "",
486
+ chat_id: l.id,
487
+ score: s,
488
+ matches: c
489
+ }) : d.create({
490
+ agent_id: o.id,
491
+ knowledge_id: ((h = o.knowledge) == null ? void 0 : h.id) ?? "",
492
+ chat_id: l.id,
493
+ score: s,
494
+ matches: c
495
+ });
496
+ },
497
+ deleteRate(s) {
498
+ return d.delete(s);
499
+ },
500
+ speak(s) {
501
+ function S() {
502
+ if (s.type === "text") {
503
+ let C = o.presenter.voice;
504
+ return s.provider && (C = s.provider), {
505
+ type: "text",
506
+ provider: C,
507
+ input: s.input,
508
+ ssml: s.ssml || !1
509
+ };
510
+ } else if (s.type === "audio")
511
+ return {
512
+ type: "audio",
513
+ audio_url: s.audio_url
514
+ };
515
+ throw new Error("Invalid payload");
516
+ }
517
+ return m.speak({
518
+ script: S()
519
+ });
479
520
  }
480
521
  };
481
522
  }
482
523
  export {
483
- L as ChatProgress,
484
- te as DocumentType,
485
- ee as KnowledgeType,
486
- Y as Providers,
524
+ B as ChatMode,
525
+ W as ChatProgress,
526
+ F as DocumentType,
527
+ H as KnowledgeType,
528
+ N as PlanGroup,
529
+ b as Providers,
487
530
  O as RateState,
488
- X as SocketManager,
489
- A as StreamEvents,
490
- j as Subject,
491
- Z as VoiceAccess,
492
- ce as createAgentManager,
493
- T as createAgentsApi,
494
- y as createClient,
495
- D as createKnowledgeApi,
496
- F as createRatingsApi,
497
- oe as createStreamingManager,
498
- ne as getAgentStreamArgs
531
+ D as Subject,
532
+ T as UserPlan,
533
+ x as VoiceAccess,
534
+ de as createAgentManager,
535
+ oe as getAgent
499
536
  };