@d-id/client-sdk 1.0.19-beta.45 → 1.0.19-beta.47

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