@d-id/client-sdk 1.0.19-beta.33 → 1.0.19-beta.35
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,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const S = "https://api.d-id.com", Q = "wss://notifications.d-id.com";
|
|
2
2
|
function U(e) {
|
|
3
3
|
if (e.type === "bearer")
|
|
4
4
|
return `Bearer ${e.token}`;
|
|
@@ -8,7 +8,7 @@ function U(e) {
|
|
|
8
8
|
return `Client-Key ${e.clientKey}.${e.externalId}`;
|
|
9
9
|
throw new Error(`Unknown auth type: ${e}`);
|
|
10
10
|
}
|
|
11
|
-
function b(e, s =
|
|
11
|
+
function b(e, s = S) {
|
|
12
12
|
const n = async (t, r) => {
|
|
13
13
|
const a = await fetch(s + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
14
14
|
...r,
|
|
@@ -19,8 +19,8 @@ function b(e, s = y) {
|
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
if (!a.ok) {
|
|
22
|
-
let
|
|
23
|
-
throw new Error(
|
|
22
|
+
let d = await a.text().catch(() => "Failed to fetch");
|
|
23
|
+
throw new Error(d);
|
|
24
24
|
}
|
|
25
25
|
return a.json();
|
|
26
26
|
};
|
|
@@ -54,7 +54,7 @@ function b(e, s = y) {
|
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
function F(e, s =
|
|
57
|
+
function F(e, s = S) {
|
|
58
58
|
const n = b(e, `${s}/agents`);
|
|
59
59
|
return {
|
|
60
60
|
create(t, r) {
|
|
@@ -75,12 +75,12 @@ function F(e, s = y) {
|
|
|
75
75
|
newChat(t, r) {
|
|
76
76
|
return n.post(`/${t}/chat`, void 0, r);
|
|
77
77
|
},
|
|
78
|
-
chat(t, r, a,
|
|
79
|
-
return n.post(`/${t}/chat/${r}`, a,
|
|
78
|
+
chat(t, r, a, d) {
|
|
79
|
+
return n.post(`/${t}/chat/${r}`, a, d);
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
|
-
function X(e, s =
|
|
83
|
+
function X(e, s = S) {
|
|
84
84
|
const n = b(e, `${s}/knowledge`);
|
|
85
85
|
return {
|
|
86
86
|
createKnowledge(t, r) {
|
|
@@ -117,7 +117,7 @@ function X(e, s = y) {
|
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
119
|
}
|
|
120
|
-
function Y(e, s =
|
|
120
|
+
function Y(e, s = S) {
|
|
121
121
|
const n = b(e, `${s}/chats/ratings`);
|
|
122
122
|
return {
|
|
123
123
|
create(t, r) {
|
|
@@ -142,15 +142,15 @@ function O(e) {
|
|
|
142
142
|
host: r,
|
|
143
143
|
auth: a
|
|
144
144
|
} = e, {
|
|
145
|
-
onMessage:
|
|
146
|
-
onOpen:
|
|
147
|
-
onClose:
|
|
145
|
+
onMessage: d = null,
|
|
146
|
+
onOpen: u = null,
|
|
147
|
+
onClose: g = null,
|
|
148
148
|
onError: f = null
|
|
149
|
-
} = t || {},
|
|
150
|
-
|
|
151
|
-
console.log(
|
|
152
|
-
},
|
|
153
|
-
|
|
149
|
+
} = t || {}, m = new WebSocket(`${r}?authorization=${U(a)}`);
|
|
150
|
+
m.onmessage = d, m.onclose = g, m.onerror = (l) => {
|
|
151
|
+
console.log(l), f == null || f(l), n(l);
|
|
152
|
+
}, m.onopen = (l) => {
|
|
153
|
+
u == null || u(l), s(m);
|
|
154
154
|
};
|
|
155
155
|
});
|
|
156
156
|
}
|
|
@@ -175,8 +175,8 @@ async function ee(e, s, n) {
|
|
|
175
175
|
host: s,
|
|
176
176
|
callbacks: {
|
|
177
177
|
onMessage: (a) => {
|
|
178
|
-
const
|
|
179
|
-
t.forEach((
|
|
178
|
+
const d = JSON.parse(a.data);
|
|
179
|
+
t.forEach((u) => u(d.event, d));
|
|
180
180
|
}
|
|
181
181
|
}
|
|
182
182
|
});
|
|
@@ -186,35 +186,43 @@ async function ee(e, s, n) {
|
|
|
186
186
|
subscribeToEvents: (a) => t.push(a)
|
|
187
187
|
};
|
|
188
188
|
}
|
|
189
|
-
var te = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(te || {}), ne = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(ne || {}),
|
|
189
|
+
var te = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(te || {}), ne = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(ne || {}), A = /* @__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.StreamFailed = "stream/error", e))(_ || {}), re = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(re || {}), ae = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e))(ae || {}), K = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(K || {}), se = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(se || {}), ie = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(ie || {}), oe = /* @__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))(oe || {}), I = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(I || {});
|
|
190
190
|
function ce(e) {
|
|
191
|
-
return e.presenter.type ===
|
|
192
|
-
videoType:
|
|
191
|
+
return e.presenter.type === I.Clip ? {
|
|
192
|
+
videoType: I.Clip,
|
|
193
193
|
driver_id: e.presenter.driver_id,
|
|
194
194
|
presenter_id: e.presenter.presenter_id
|
|
195
195
|
} : {
|
|
196
|
-
videoType:
|
|
196
|
+
videoType: I.Talk,
|
|
197
197
|
source_url: e.presenter.source_url
|
|
198
198
|
};
|
|
199
199
|
}
|
|
200
200
|
function H(e, s, n, t) {
|
|
201
201
|
return new Promise(async (r, a) => {
|
|
202
|
-
const
|
|
202
|
+
const d = await me(ce(e), {
|
|
203
203
|
...s,
|
|
204
204
|
callbacks: {
|
|
205
205
|
...s.callbacks,
|
|
206
|
-
onConnectionStateChange: async (
|
|
207
|
-
var
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
206
|
+
onConnectionStateChange: async (u) => {
|
|
207
|
+
var g, f;
|
|
208
|
+
if (u === "connected")
|
|
209
|
+
try {
|
|
210
|
+
t || (t = await n.newChat(e.id)), r({
|
|
211
|
+
chat: t,
|
|
212
|
+
streamingManager: d
|
|
213
|
+
});
|
|
214
|
+
} catch (m) {
|
|
215
|
+
console.error(m), a(new Error("Cannot create new chat"));
|
|
216
|
+
}
|
|
217
|
+
else
|
|
218
|
+
u === "failed" && a(new Error("Cannot create connection"));
|
|
219
|
+
(f = (g = s.callbacks).onConnectionStateChange) == null || f.call(g, u);
|
|
212
220
|
},
|
|
213
221
|
// TODO remove when webscoket will return partial
|
|
214
|
-
onMessage: (
|
|
215
|
-
var f,
|
|
216
|
-
|
|
217
|
-
content:
|
|
222
|
+
onMessage: (u, g) => {
|
|
223
|
+
var f, m;
|
|
224
|
+
u === _.ChatAnswer && (console.log("ChatAnswer", u, g), (m = (f = s.callbacks).onChatEvents) == null || m.call(f, K.Answer, {
|
|
225
|
+
content: g,
|
|
218
226
|
event: K.Answer
|
|
219
227
|
}));
|
|
220
228
|
}
|
|
@@ -222,61 +230,68 @@ function H(e, s, n, t) {
|
|
|
222
230
|
});
|
|
223
231
|
});
|
|
224
232
|
}
|
|
225
|
-
function
|
|
226
|
-
return F(s, n ||
|
|
233
|
+
function we(e, s, n) {
|
|
234
|
+
return F(s, n || S).getById(e);
|
|
227
235
|
}
|
|
228
|
-
async function
|
|
229
|
-
|
|
236
|
+
async function pe(e, s) {
|
|
237
|
+
var k, M;
|
|
238
|
+
const n = s.baseURL || S, t = s.wsURL || Q, r = new AbortController(), a = F(s.auth, n), d = Y(s.auth, n), u = X(s.auth, n), g = await a.getById(e);
|
|
239
|
+
(M = (k = s.callbacks) == null ? void 0 : k.onAgentReady) == null || M.call(k, g);
|
|
240
|
+
const f = await ee(s.auth, t, s.callbacks.onChatEvents);
|
|
230
241
|
let {
|
|
231
|
-
chat:
|
|
232
|
-
streamingManager:
|
|
233
|
-
} = await H(
|
|
242
|
+
chat: m,
|
|
243
|
+
streamingManager: l
|
|
244
|
+
} = await H(g, s, a);
|
|
234
245
|
return {
|
|
235
|
-
agent:
|
|
246
|
+
agent: g,
|
|
247
|
+
chatId: m.id,
|
|
236
248
|
async reconnectToChat() {
|
|
237
249
|
const {
|
|
238
|
-
streamingManager:
|
|
239
|
-
} = await H(
|
|
240
|
-
|
|
250
|
+
streamingManager: i
|
|
251
|
+
} = await H(g, s, a, m);
|
|
252
|
+
l = i;
|
|
241
253
|
},
|
|
242
254
|
terminate() {
|
|
243
|
-
return r.abort(), f.terminate(),
|
|
244
|
-
},
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
messages: c
|
|
255
|
+
return r.abort(), f.terminate(), l.terminate();
|
|
256
|
+
},
|
|
257
|
+
chat(i) {
|
|
258
|
+
return a.chat(e, m.id, {
|
|
259
|
+
sessionId: l.sessionId,
|
|
260
|
+
streamId: l.streamId,
|
|
261
|
+
messages: i
|
|
251
262
|
}, {
|
|
252
263
|
signal: r.signal
|
|
253
264
|
});
|
|
254
265
|
},
|
|
255
|
-
rate(
|
|
256
|
-
return
|
|
266
|
+
rate(i, o) {
|
|
267
|
+
return o ? d.update(o, i) : d.create(i);
|
|
257
268
|
},
|
|
258
|
-
deleteRate(
|
|
259
|
-
return
|
|
269
|
+
deleteRate(i) {
|
|
270
|
+
return d.delete(i);
|
|
260
271
|
},
|
|
261
|
-
speak(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
272
|
+
speak(i) {
|
|
273
|
+
function o() {
|
|
274
|
+
if (i.type === "text")
|
|
275
|
+
return {
|
|
276
|
+
type: "text",
|
|
277
|
+
provider: i.provider,
|
|
278
|
+
input: i.input,
|
|
279
|
+
ssml: i.ssml || !1
|
|
280
|
+
};
|
|
281
|
+
if (i.type === "audio")
|
|
282
|
+
return {
|
|
283
|
+
type: "audio",
|
|
284
|
+
audio_url: i.audio_url
|
|
285
|
+
};
|
|
286
|
+
throw new Error("Invalid payload");
|
|
287
|
+
}
|
|
288
|
+
return l.speak({
|
|
289
|
+
script: o()
|
|
290
|
+
});
|
|
276
291
|
},
|
|
277
|
-
getStarterMessages() {
|
|
278
|
-
var
|
|
279
|
-
return (
|
|
292
|
+
async getStarterMessages() {
|
|
293
|
+
var i;
|
|
294
|
+
return (i = g.knowledge) != null && i.id ? u.getKnowledge(g.knowledge.id).then((o) => (o == null ? void 0 : o.starter_message) || []) : [];
|
|
280
295
|
}
|
|
281
296
|
};
|
|
282
297
|
}
|
|
@@ -326,23 +341,23 @@ function le(e, s) {
|
|
|
326
341
|
config: t.config
|
|
327
342
|
}, r);
|
|
328
343
|
},
|
|
329
|
-
startConnection(t, r, a,
|
|
344
|
+
startConnection(t, r, a, d) {
|
|
330
345
|
return n.post(`/talks/streams/${t}/sdp`, {
|
|
331
346
|
session_id: a,
|
|
332
347
|
answer: r
|
|
333
|
-
},
|
|
348
|
+
}, d);
|
|
334
349
|
},
|
|
335
|
-
addIceCandidate(t, r, a,
|
|
350
|
+
addIceCandidate(t, r, a, d) {
|
|
336
351
|
return n.post(`/talks/streams/${t}/ice`, {
|
|
337
352
|
session_id: a,
|
|
338
353
|
...r
|
|
339
|
-
},
|
|
354
|
+
}, d);
|
|
340
355
|
},
|
|
341
|
-
sendStreamRequest(t, r, a,
|
|
356
|
+
sendStreamRequest(t, r, a, d) {
|
|
342
357
|
return n.post(`/talks/streams/${t}`, {
|
|
343
358
|
session_id: r,
|
|
344
359
|
...a
|
|
345
|
-
},
|
|
360
|
+
}, d);
|
|
346
361
|
},
|
|
347
362
|
close(t, r, a) {
|
|
348
363
|
return n.delete(`/talks/streams/${t}`, {
|
|
@@ -385,117 +400,117 @@ function ue(e, s) {
|
|
|
385
400
|
});
|
|
386
401
|
}
|
|
387
402
|
let T = !1;
|
|
388
|
-
const
|
|
389
|
-
async function
|
|
403
|
+
const C = (e, s) => T && console.log(e, s), ge = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
404
|
+
async function me(e, {
|
|
390
405
|
debug: s = !1,
|
|
391
406
|
callbacks: n,
|
|
392
407
|
auth: t,
|
|
393
|
-
baseURL: r =
|
|
408
|
+
baseURL: r = S
|
|
394
409
|
}) {
|
|
395
410
|
T = s;
|
|
396
411
|
const a = {
|
|
397
412
|
...n
|
|
398
413
|
}, {
|
|
399
|
-
startConnection:
|
|
400
|
-
sendStreamRequest:
|
|
401
|
-
close:
|
|
414
|
+
startConnection: d,
|
|
415
|
+
sendStreamRequest: u,
|
|
416
|
+
close: g,
|
|
402
417
|
createStream: f,
|
|
403
|
-
addIceCandidate:
|
|
404
|
-
} = e.videoType ===
|
|
405
|
-
id:
|
|
406
|
-
offer:
|
|
407
|
-
ice_servers:
|
|
408
|
-
session_id:
|
|
409
|
-
} = await f(e),
|
|
410
|
-
iceServers:
|
|
411
|
-
}),
|
|
412
|
-
let
|
|
413
|
-
if (!
|
|
418
|
+
addIceCandidate: m
|
|
419
|
+
} = e.videoType === I.Clip ? de(t, r) : le(t, r), {
|
|
420
|
+
id: l,
|
|
421
|
+
offer: k,
|
|
422
|
+
ice_servers: M,
|
|
423
|
+
session_id: i
|
|
424
|
+
} = await f(e), o = new ge({
|
|
425
|
+
iceServers: M
|
|
426
|
+
}), E = o.createDataChannel("JanusDataChannel");
|
|
427
|
+
let v = [], R = 0, P = 0, y, x, W;
|
|
428
|
+
if (!i)
|
|
414
429
|
throw new Error("Could not create session_id");
|
|
415
430
|
(() => {
|
|
416
431
|
W = setInterval(() => {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
var
|
|
420
|
-
if (
|
|
421
|
-
if (
|
|
422
|
-
const
|
|
423
|
-
let z =
|
|
424
|
-
|
|
432
|
+
o.getStats().then((w) => {
|
|
433
|
+
w.forEach((p) => {
|
|
434
|
+
var h;
|
|
435
|
+
if (p.type === "inbound-rtp" && p.kind === "video") {
|
|
436
|
+
if (P = v.length - 1, p && v[P]) {
|
|
437
|
+
const $ = p.bytesReceived, N = v[P].bytesReceived;
|
|
438
|
+
let z = y;
|
|
439
|
+
y = $ - N > 0;
|
|
425
440
|
let L;
|
|
426
|
-
if (z !==
|
|
427
|
-
if (
|
|
428
|
-
|
|
441
|
+
if (z !== y) {
|
|
442
|
+
if (y)
|
|
443
|
+
R = v.length;
|
|
429
444
|
else {
|
|
430
|
-
const J =
|
|
445
|
+
const J = v.slice(R), D = R === 0 ? void 0 : v[R - 1];
|
|
431
446
|
L = ue(J, D), L = L.sort((V, G) => G.packetsLost - V.packetsLost).slice(0, 5);
|
|
432
447
|
}
|
|
433
|
-
(
|
|
448
|
+
(h = a.onVideoStateChange) == null || h.call(a, y ? A.Start : A.Stop, L);
|
|
434
449
|
}
|
|
435
450
|
}
|
|
436
|
-
|
|
451
|
+
v.push(p);
|
|
437
452
|
}
|
|
438
453
|
});
|
|
439
454
|
});
|
|
440
455
|
}, 500);
|
|
441
456
|
})();
|
|
442
457
|
const q = () => {
|
|
443
|
-
clearInterval(
|
|
458
|
+
clearInterval(x);
|
|
444
459
|
};
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
candidate:
|
|
448
|
-
sdpMid:
|
|
449
|
-
sdpMLineIndex:
|
|
450
|
-
},
|
|
451
|
-
},
|
|
452
|
-
var
|
|
453
|
-
|
|
454
|
-
},
|
|
455
|
-
var
|
|
456
|
-
|
|
457
|
-
},
|
|
458
|
-
var
|
|
459
|
-
if (
|
|
460
|
-
const [
|
|
461
|
-
|
|
462
|
-
event:
|
|
463
|
-
data:
|
|
464
|
-
}),
|
|
460
|
+
o.onicecandidate = (c) => {
|
|
461
|
+
C("peerConnection.onicecandidate", c), c.candidate && c.candidate.sdpMid && c.candidate.sdpMLineIndex !== null && m(l, {
|
|
462
|
+
candidate: c.candidate.candidate,
|
|
463
|
+
sdpMid: c.candidate.sdpMid,
|
|
464
|
+
sdpMLineIndex: c.candidate.sdpMLineIndex
|
|
465
|
+
}, i);
|
|
466
|
+
}, o.oniceconnectionstatechange = () => {
|
|
467
|
+
var c;
|
|
468
|
+
C("peerConnection.oniceconnectionstatechange => " + o.iceConnectionState), (c = a.onConnectionStateChange) == null || c.call(a, o.iceConnectionState);
|
|
469
|
+
}, o.ontrack = (c) => {
|
|
470
|
+
var w;
|
|
471
|
+
C("peerConnection.ontrack", c), (w = a.onSrcObjectReady) == null || w.call(a, c.streams[0]);
|
|
472
|
+
}, E.onmessage = (c) => {
|
|
473
|
+
var w, p;
|
|
474
|
+
if (E.readyState === "open") {
|
|
475
|
+
const [h, $] = c.data.split(":");
|
|
476
|
+
h === _.StreamStarted ? console.log("StreamStarted", h, $) : h === _.StreamDone ? console.log("StreamDone") : h === _.StreamFailed ? ((w = a.onVideoStateChange) == null || w.call(a, A.Stop, {
|
|
477
|
+
event: h,
|
|
478
|
+
data: $
|
|
479
|
+
}), v = [], R = 0, P = 0, y = !1, console.log("StreamFailed")) : (p = a.onMessage) == null || p.call(a, h, decodeURIComponent($));
|
|
465
480
|
}
|
|
466
|
-
}, await
|
|
467
|
-
const B = await
|
|
468
|
-
return
|
|
481
|
+
}, await o.setRemoteDescription(k), C("set remote description OK");
|
|
482
|
+
const B = await o.createAnswer();
|
|
483
|
+
return C("create answer OK"), await o.setLocalDescription(B), C("set local description OK"), await d(l, B, i), C("start connection OK"), {
|
|
469
484
|
/**
|
|
470
485
|
* Method to send request to server to get clip or talk depend on you payload
|
|
471
486
|
* @param payload
|
|
472
487
|
*/
|
|
473
|
-
speak(
|
|
474
|
-
return l
|
|
488
|
+
speak(c) {
|
|
489
|
+
return u(l, i, c);
|
|
475
490
|
},
|
|
476
491
|
/**
|
|
477
492
|
* Method to close RTC connection
|
|
478
493
|
*/
|
|
479
494
|
async terminate() {
|
|
480
|
-
var
|
|
481
|
-
|
|
482
|
-
}), (
|
|
495
|
+
var c, w;
|
|
496
|
+
l && (o && (o.close(), o.oniceconnectionstatechange = null, o.onnegotiationneeded = null, o.onicecandidate = null, o.ontrack = null, clearInterval(x)), await g(l, i).catch((p) => {
|
|
497
|
+
}), (c = a.onConnectionStateChange) == null || c.call(a, "closed"), (w = a.onVideoStateChange) == null || w.call(a, A.Stop), q(), clearInterval(W));
|
|
483
498
|
},
|
|
484
499
|
/**
|
|
485
500
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
486
501
|
*/
|
|
487
|
-
sessionId:
|
|
502
|
+
sessionId: i,
|
|
488
503
|
/**
|
|
489
504
|
* Id of current RTC stream
|
|
490
505
|
*/
|
|
491
|
-
streamId:
|
|
506
|
+
streamId: l,
|
|
492
507
|
/**
|
|
493
508
|
* Method to add callback that will be trigered on supported events
|
|
494
509
|
* @param eventName
|
|
495
510
|
* @param callback
|
|
496
511
|
*/
|
|
497
|
-
onCallback(
|
|
498
|
-
a[
|
|
512
|
+
onCallback(c, w) {
|
|
513
|
+
a[c] = w;
|
|
499
514
|
}
|
|
500
515
|
};
|
|
501
516
|
}
|
|
@@ -507,15 +522,14 @@ export {
|
|
|
507
522
|
te as Providers,
|
|
508
523
|
re as RateState,
|
|
509
524
|
ee as SocketManager,
|
|
510
|
-
|
|
525
|
+
_ as StreamEvents,
|
|
511
526
|
se as Subject,
|
|
512
527
|
ne as VoiceAccess,
|
|
513
|
-
|
|
528
|
+
pe as createAgentManager,
|
|
514
529
|
F as createAgentsApi,
|
|
515
530
|
b as createClient,
|
|
516
531
|
X as createKnowledgeApi,
|
|
517
532
|
Y as createRatingsApi,
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
ce as getAgentStreamArgs
|
|
533
|
+
me as createStreamingManager,
|
|
534
|
+
we as getAgent
|
|
521
535
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(s=typeof globalThis<"u"?globalThis:s||self,p(s.index={}))})(this,function(s){"use strict";const p="https://api.d-id.com",ee="wss://notifications.d-id.com";function W(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}.${e.externalId}`;throw new Error(`Unknown auth type: ${e}`)}function $(e,i=p){const n=async(t,a)=>{const r=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...a,headers:{...a==null?void 0:a.headers,Authorization:W(e),"Content-Type":"application/json"}});if(!r.ok){let c=await r.text().catch(()=>"Failed to fetch");throw new Error(c)}return r.json()};return{get(t,a){return n(t,{...a,method:"GET"})},post(t,a,r){return n(t,{...r,body:JSON.stringify(a),method:"POST"})},delete(t,a,r){return n(t,{...r,body:JSON.stringify(a),method:"DELETE"})},patch(t,a,r){return n(t,{...r,body:JSON.stringify(a),method:"PATCH"})}}}function T(e,i=p){const n=$(e,`${i}/agents`);return{create(t,a){return n.post("/",t,a)},getAgents(t,a){return n.get(`/${t?`?tag=${t}`:""}`,a).then(r=>r??[])},getById(t,a){return n.get(`/${t}`,a)},delete(t,a){return n.delete(`/${t}`,void 0,a)},update(t,a,r){return n.patch(`/${t}`,a,r)},newChat(t,a){return n.post(`/${t}/chat`,void 0,a)},chat(t,a,r,c){return n.post(`/${t}/chat/${a}`,r,c)}}}function B(e,i=p){const n=$(e,`${i}/knowledge`);return{createKnowledge(t,a){return n.post("/",t,a)},getKnowledgeBase(t){return n.get("/",t)},getKnowledge(t,a){return n.get(`/${t}`,a)},deleteKnowledge(t,a){return n.delete(`/${t}`,void 0,a)},createDocument(t,a,r){return n.post(`/${t}/documents`,a,r)},deleteDocument(t,a,r){return n.delete(`/${t}/documents/${a}`,void 0,r)},getDocuments(t,a){return n.get(`/${t}/documents`,a)},getDocument(t,a,r){return n.get(`/${t}/documents/${a}`,r)},getRecords(t,a,r){return n.get(`/${t}/documents/${a}/records`,r)},query(t,a,r){return n.post(`/${t}/query`,{query:a},r)}}}function H(e,i=p){const n=$(e,`${i}/chats/ratings`);return{create(t,a){return n.post("/",t,a)},getByKnowledge(t,a){return n.get(`/${t}`,a).then(r=>r??[])},update(t,a,r){return n.patch(`/${t}`,a,r)},delete(t,a){return n.delete(`/${t}`,a)}}}const te=e=>new Promise(i=>setTimeout(i,e));function ne(e){return new Promise((i,n)=>{const{callbacks:t,host:a,auth:r}=e,{onMessage:c=null,onOpen:u=null,onClose:m=null,onError:w=null}=t||{},f=new WebSocket(`${a}?authorization=${W(r)}`);f.onmessage=c,f.onclose=m,f.onerror=l=>{console.log(l),w==null||w(l),n(l)},f.onopen=l=>{u==null||u(l),i(f)}})}async function ae(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 ne(e)}catch(a){if(t===i)throw a;await te(t*500)}return n}async function U(e,i,n){const t=n?[n]:[],a=await ae({auth:e,host:i,callbacks:{onMessage:r=>{const c=JSON.parse(r.data);t.forEach(u=>u(c.event,c))}}});return{socket:a,terminate:()=>a.close(),subscribeToEvents:r=>t.push(r)}}var F=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(F||{}),q=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(q||{}),I=(e=>(e.Start="START",e.Stop="STOP",e))(I||{}),A=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e))(A||{}),N=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(N||{}),z=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(z||{}),L=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(L||{}),J=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(J||{}),D=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(D||{}),V=(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))(V||{}),M=(e=>(e.Clip="clip",e.Talk="talk",e))(M||{});function G(e){return e.presenter.type===M.Clip?{videoType:M.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:M.Talk,source_url:e.presenter.source_url}}function Q(e,i,n,t){return new Promise(async(a,r)=>{const c=await Y(G(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async u=>{var m,w;u==="connected"?(t||(t=await n.newChat(e.id)),a({chat:t,streamingManager:c})):u==="failed"&&r(new Error("Cannot create connection")),(w=(m=i.callbacks).onConnectionStateChange)==null||w.call(m,u)},onMessage:(u,m)=>{var w,f;u===A.ChatAnswer&&(console.log("ChatAnswer",u,m),(f=(w=i.callbacks).onChatEvents)==null||f.call(w,L.Answer,{content:m,event:L.Answer}))}}})})}function re(e,i,n){return T(i,n||p).getById(e)}async function ie(e,i){const n=i.baseURL||p,t=i.wsURL||ee,a=new AbortController,r=T(i.auth,n),c=H(i.auth,n),u=B(i.auth,n),m=await r.getById(e),w=await U(i.auth,t,i.callbacks.onChatEvents);let{chat:f,streamingManager:l}=await Q(m,i,r);return{agent:m,async reconnectToChat(){const{streamingManager:d}=await Q(m,i,r,f);l=d},terminate(){return a.abort(),w.terminate(),l.terminate()},chatId:f.id,chat(d){return r.chat(e,f.id,{sessionId:l.sessionId,streamId:l.streamId,messages:d},{signal:a.signal})},rate(d,v){return v?c.update(v,d):c.create(d)},deleteRate(d){return c.delete(d)},speak(d){let v;return d.type==="text"?v={script:{type:"text",provider:d.provider,input:d.input,ssml:d.ssml||!1}}:d.type==="audio"&&(v={script:{type:"audio",audio_url:d.audio_url}}),l.speak(v)},getStarterMessages(){var d,v;return(d=m.knowledge)!=null&&d.id?u.getKnowledge((v=m.knowledge)==null?void 0:v.id).then(C=>(C==null?void 0:C.starter_message)||[]):Promise.resolve([])}}}function se(e,i){const n=$(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,a,r){return n.post(`/clips/streams/${t}/sdp`,{session_id:r,answer:a})},addIceCandidate(t,a,r){return n.post(`/clips/streams/${t}/ice`,{session_id:r,...a})},sendStreamRequest(t,a,r){return n.post(`/clips/streams/${t}`,{session_id:a,...r})},close(t,a){return n.delete(`/clips/streams/${t}`,{session_id:a})}}}function oe(e,i){const n=$(e,i);return{createStream(t,a){return n.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},a)},startConnection(t,a,r,c){return n.post(`/talks/streams/${t}/sdp`,{session_id:r,answer:a},c)},addIceCandidate(t,a,r,c){return n.post(`/talks/streams/${t}/ice`,{session_id:r,...a},c)},sendStreamRequest(t,a,r,c){return n.post(`/talks/streams/${t}`,{session_id:a,...r},c)},close(t,a,r){return n.delete(`/talks/streams/${t}`,{session_id:a},r)}}}function ce(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 X=!1;const R=(e,i)=>X&&console.log(e,i),de=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function Y(e,{debug:i=!1,callbacks:n,auth:t,baseURL:a=p}){X=i;const r={...n},{startConnection:c,sendStreamRequest:u,close:m,createStream:w,addIceCandidate:f}=e.videoType===M.Clip?se(t,a):oe(t,a),{id:l,offer:d,ice_servers:v,session_id:C}=await w(e),g=new de({iceServers:v}),Z=g.createDataChannel("JanusDataChannel");let k=[],P=0,K=0,_,O,j;if(!C)throw new Error("Could not create session_id");(()=>{j=setInterval(()=>{g.getStats().then(h=>{h.forEach(S=>{var y;if(S.type==="inbound-rtp"&&S.kind==="video"){if(K=k.length-1,S&&k[K]){const b=S.bytesReceived,ue=k[K].bytesReceived;let ge=_;_=b-ue>0;let E;if(ge!==_){if(_)P=k.length;else{const me=k.slice(P),fe=P===0?void 0:k[P-1];E=ce(me,fe),E=E.sort((we,he)=>he.packetsLost-we.packetsLost).slice(0,5)}(y=r.onVideoStateChange)==null||y.call(r,_?I.Start:I.Stop,E)}}k.push(S)}})})},500)})();const le=()=>{clearInterval(O)};g.onicecandidate=o=>{R("peerConnection.onicecandidate",o),o.candidate&&o.candidate.sdpMid&&o.candidate.sdpMLineIndex!==null&&f(l,{candidate:o.candidate.candidate,sdpMid:o.candidate.sdpMid,sdpMLineIndex:o.candidate.sdpMLineIndex},C)},g.oniceconnectionstatechange=()=>{var o;R("peerConnection.oniceconnectionstatechange => "+g.iceConnectionState),(o=r.onConnectionStateChange)==null||o.call(r,g.iceConnectionState)},g.ontrack=o=>{var h;R("peerConnection.ontrack",o),(h=r.onSrcObjectReady)==null||h.call(r,o.streams[0])},Z.onmessage=o=>{var h,S;if(Z.readyState==="open"){const[y,b]=o.data.split(":");y===A.StreamStarted?console.log("StreamStarted",y,b):y===A.StreamDone?console.log("StreamDone"):y===A.StreamFailed?((h=r.onVideoStateChange)==null||h.call(r,I.Stop,{event:y,data:b}),k=[],P=0,K=0,_=!1,console.log("StreamFailed")):(S=r.onMessage)==null||S.call(r,y,decodeURIComponent(b))}},await g.setRemoteDescription(d),R("set remote description OK");const x=await g.createAnswer();return R("create answer OK"),await g.setLocalDescription(x),R("set local description OK"),await c(l,x,C),R("start connection OK"),{speak(o){return u(l,C,o)},async terminate(){var o,h;l&&(g&&(g.close(),g.oniceconnectionstatechange=null,g.onnegotiationneeded=null,g.onicecandidate=null,g.ontrack=null,clearInterval(O)),await m(l,C).catch(S=>{}),(o=r.onConnectionStateChange)==null||o.call(r,"closed"),(h=r.onVideoStateChange)==null||h.call(r,I.Stop),le(),clearInterval(j))},sessionId:C,streamId:l,onCallback(o,h){r[o]=h}}}s.ChatMode=z,s.ChatProgress=L,s.DocumentType=V,s.KnowledgeType=D,s.Providers=F,s.RateState=N,s.SocketManager=U,s.StreamEvents=A,s.Subject=J,s.VoiceAccess=q,s.createAgentManager=ie,s.createAgentsApi=T,s.createClient=$,s.createKnowledgeApi=B,s.createRatingsApi=H,s.createStreamingManager=Y,s.getAgent=re,s.getAgentStreamArgs=G,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(d,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(d=typeof globalThis<"u"?globalThis:d||self,p(d.index={}))})(this,function(d){"use strict";const p="https://api.d-id.com",j="wss://notifications.d-id.com";function W(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}.${e.externalId}`;throw new Error(`Unknown auth type: ${e}`)}function k(e,i=p){const n=async(t,a)=>{const r=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...a,headers:{...a==null?void 0:a.headers,Authorization:W(e),"Content-Type":"application/json"}});if(!r.ok){let l=await r.text().catch(()=>"Failed to fetch");throw new Error(l)}return r.json()};return{get(t,a){return n(t,{...a,method:"GET"})},post(t,a,r){return n(t,{...r,body:JSON.stringify(a),method:"POST"})},delete(t,a,r){return n(t,{...r,body:JSON.stringify(a),method:"DELETE"})},patch(t,a,r){return n(t,{...r,body:JSON.stringify(a),method:"PATCH"})}}}function T(e,i=p){const n=k(e,`${i}/agents`);return{create(t,a){return n.post("/",t,a)},getAgents(t,a){return n.get(`/${t?`?tag=${t}`:""}`,a).then(r=>r??[])},getById(t,a){return n.get(`/${t}`,a)},delete(t,a){return n.delete(`/${t}`,void 0,a)},update(t,a,r){return n.patch(`/${t}`,a,r)},newChat(t,a){return n.post(`/${t}/chat`,void 0,a)},chat(t,a,r,l){return n.post(`/${t}/chat/${a}`,r,l)}}}function B(e,i=p){const n=k(e,`${i}/knowledge`);return{createKnowledge(t,a){return n.post("/",t,a)},getKnowledgeBase(t){return n.get("/",t)},getKnowledge(t,a){return n.get(`/${t}`,a)},deleteKnowledge(t,a){return n.delete(`/${t}`,void 0,a)},createDocument(t,a,r){return n.post(`/${t}/documents`,a,r)},deleteDocument(t,a,r){return n.delete(`/${t}/documents/${a}`,void 0,r)},getDocuments(t,a){return n.get(`/${t}/documents`,a)},getDocument(t,a,r){return n.get(`/${t}/documents/${a}`,r)},getRecords(t,a,r){return n.get(`/${t}/documents/${a}/records`,r)},query(t,a,r){return n.post(`/${t}/query`,{query:a},r)}}}function H(e,i=p){const n=k(e,`${i}/chats/ratings`);return{create(t,a){return n.post("/",t,a)},getByKnowledge(t,a){return n.get(`/${t}`,a).then(r=>r??[])},update(t,a,r){return n.patch(`/${t}`,a,r)},delete(t,a){return n.delete(`/${t}`,a)}}}const ee=e=>new Promise(i=>setTimeout(i,e));function te(e){return new Promise((i,n)=>{const{callbacks:t,host:a,auth:r}=e,{onMessage:l=null,onOpen:g=null,onClose:m=null,onError:w=null}=t||{},f=new WebSocket(`${a}?authorization=${W(r)}`);f.onmessage=l,f.onclose=m,f.onerror=u=>{console.log(u),w==null||w(u),n(u)},f.onopen=u=>{g==null||g(u),i(f)}})}async function ne(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 te(e)}catch(a){if(t===i)throw a;await ee(t*500)}return n}async function U(e,i,n){const t=n?[n]:[],a=await ne({auth:e,host:i,callbacks:{onMessage:r=>{const l=JSON.parse(r.data);t.forEach(g=>g(l.event,l))}}});return{socket:a,terminate:()=>a.close(),subscribeToEvents:r=>t.push(r)}}var F=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(F||{}),q=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(q||{}),_=(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))(R||{}),N=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(N||{}),z=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(z||{}),P=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(P||{}),J=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(J||{}),D=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(D||{}),V=(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))(V||{}),I=(e=>(e.Clip="clip",e.Talk="talk",e))(I||{});function ae(e){return e.presenter.type===I.Clip?{videoType:I.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:I.Talk,source_url:e.presenter.source_url}}function G(e,i,n,t){return new Promise(async(a,r)=>{const l=await X(ae(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async g=>{var m,w;if(g==="connected")try{t||(t=await n.newChat(e.id)),a({chat:t,streamingManager:l})}catch(f){console.error(f),r(new Error("Cannot create new chat"))}else g==="failed"&&r(new Error("Cannot create connection"));(w=(m=i.callbacks).onConnectionStateChange)==null||w.call(m,g)},onMessage:(g,m)=>{var w,f;g===R.ChatAnswer&&(console.log("ChatAnswer",g,m),(f=(w=i.callbacks).onChatEvents)==null||f.call(w,P.Answer,{content:m,event:P.Answer}))}}})})}function re(e,i,n){return T(i,n||p).getById(e)}async function ie(e,i){var $,L;const n=i.baseURL||p,t=i.wsURL||j,a=new AbortController,r=T(i.auth,n),l=H(i.auth,n),g=B(i.auth,n),m=await r.getById(e);(L=($=i.callbacks)==null?void 0:$.onAgentReady)==null||L.call($,m);const w=await U(i.auth,t,i.callbacks.onChatEvents);let{chat:f,streamingManager:u}=await G(m,i,r);return{agent:m,chatId:f.id,async reconnectToChat(){const{streamingManager:s}=await G(m,i,r,f);u=s},terminate(){return a.abort(),w.terminate(),u.terminate()},chat(s){return r.chat(e,f.id,{sessionId:u.sessionId,streamId:u.streamId,messages:s},{signal:a.signal})},rate(s,c){return c?l.update(c,s):l.create(s)},deleteRate(s){return l.delete(s)},speak(s){function c(){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 u.speak({script:c()})},async getStarterMessages(){var s;return(s=m.knowledge)!=null&&s.id?g.getKnowledge(m.knowledge.id).then(c=>(c==null?void 0:c.starter_message)||[]):[]}}}function se(e,i){const n=k(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,a,r){return n.post(`/clips/streams/${t}/sdp`,{session_id:r,answer:a})},addIceCandidate(t,a,r){return n.post(`/clips/streams/${t}/ice`,{session_id:r,...a})},sendStreamRequest(t,a,r){return n.post(`/clips/streams/${t}`,{session_id:a,...r})},close(t,a){return n.delete(`/clips/streams/${t}`,{session_id:a})}}}function ce(e,i){const n=k(e,i);return{createStream(t,a){return n.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},a)},startConnection(t,a,r,l){return n.post(`/talks/streams/${t}/sdp`,{session_id:r,answer:a},l)},addIceCandidate(t,a,r,l){return n.post(`/talks/streams/${t}/ice`,{session_id:r,...a},l)},sendStreamRequest(t,a,r,l){return n.post(`/talks/streams/${t}`,{session_id:a,...r},l)},close(t,a,r){return n.delete(`/talks/streams/${t}`,{session_id:a},r)}}}function oe(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 Q=!1;const y=(e,i)=>Q&&console.log(e,i),de=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function X(e,{debug:i=!1,callbacks:n,auth:t,baseURL:a=p}){Q=i;const r={...n},{startConnection:l,sendStreamRequest:g,close:m,createStream:w,addIceCandidate:f}=e.videoType===I.Clip?se(t,a):ce(t,a),{id:u,offer:$,ice_servers:L,session_id:s}=await w(e),c=new de({iceServers:L}),Y=c.createDataChannel("JanusDataChannel");let S=[],b=0,K=0,A,Z,x;if(!s)throw new Error("Could not create session_id");(()=>{x=setInterval(()=>{c.getStats().then(h=>{h.forEach(v=>{var C;if(v.type==="inbound-rtp"&&v.kind==="video"){if(K=S.length-1,v&&S[K]){const M=v.bytesReceived,ue=S[K].bytesReceived;let ge=A;A=M-ue>0;let E;if(ge!==A){if(A)b=S.length;else{const me=S.slice(b),fe=b===0?void 0:S[b-1];E=oe(me,fe),E=E.sort((we,he)=>he.packetsLost-we.packetsLost).slice(0,5)}(C=r.onVideoStateChange)==null||C.call(r,A?_.Start:_.Stop,E)}}S.push(v)}})})},500)})();const le=()=>{clearInterval(Z)};c.onicecandidate=o=>{y("peerConnection.onicecandidate",o),o.candidate&&o.candidate.sdpMid&&o.candidate.sdpMLineIndex!==null&&f(u,{candidate:o.candidate.candidate,sdpMid:o.candidate.sdpMid,sdpMLineIndex:o.candidate.sdpMLineIndex},s)},c.oniceconnectionstatechange=()=>{var o;y("peerConnection.oniceconnectionstatechange => "+c.iceConnectionState),(o=r.onConnectionStateChange)==null||o.call(r,c.iceConnectionState)},c.ontrack=o=>{var h;y("peerConnection.ontrack",o),(h=r.onSrcObjectReady)==null||h.call(r,o.streams[0])},Y.onmessage=o=>{var h,v;if(Y.readyState==="open"){const[C,M]=o.data.split(":");C===R.StreamStarted?console.log("StreamStarted",C,M):C===R.StreamDone?console.log("StreamDone"):C===R.StreamFailed?((h=r.onVideoStateChange)==null||h.call(r,_.Stop,{event:C,data:M}),S=[],b=0,K=0,A=!1,console.log("StreamFailed")):(v=r.onMessage)==null||v.call(r,C,decodeURIComponent(M))}},await c.setRemoteDescription($),y("set remote description OK");const O=await c.createAnswer();return y("create answer OK"),await c.setLocalDescription(O),y("set local description OK"),await l(u,O,s),y("start connection OK"),{speak(o){return g(u,s,o)},async terminate(){var o,h;u&&(c&&(c.close(),c.oniceconnectionstatechange=null,c.onnegotiationneeded=null,c.onicecandidate=null,c.ontrack=null,clearInterval(Z)),await m(u,s).catch(v=>{}),(o=r.onConnectionStateChange)==null||o.call(r,"closed"),(h=r.onVideoStateChange)==null||h.call(r,_.Stop),le(),clearInterval(x))},sessionId:s,streamId:u,onCallback(o,h){r[o]=h}}}d.ChatMode=z,d.ChatProgress=P,d.DocumentType=V,d.KnowledgeType=D,d.Providers=F,d.RateState=N,d.SocketManager=U,d.StreamEvents=R,d.Subject=J,d.VoiceAccess=q,d.createAgentManager=ie,d.createAgentsApi=T,d.createClient=k,d.createKnowledgeApi=B,d.createRatingsApi=H,d.createStreamingManager=X,d.getAgent=re,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Agent, AgentManager, AgentManagerOptions
|
|
1
|
+
import { Agent, AgentManager, AgentManagerOptions } from '../types/index';
|
|
2
2
|
import { Auth } from '..';
|
|
3
|
-
export declare function getAgentStreamArgs(agent: Agent): CreateStreamOptions;
|
|
4
3
|
export declare function getAgent(agentId: string, auth: Auth, baseURL?: string): Promise<Agent>;
|
|
5
4
|
/**
|
|
6
5
|
* Creates a new Agent Manager instance for interacting with an agent, chat, and related connections.
|
|
@@ -56,6 +56,11 @@ interface ManagerCallbacks {
|
|
|
56
56
|
* @param progress
|
|
57
57
|
*/
|
|
58
58
|
onChatEvents?(progress: ChatProgress, data: any): void;
|
|
59
|
+
/**
|
|
60
|
+
* Optional callback function that will be triggered when the agent is ready
|
|
61
|
+
* @param agent - Agent instance you are working with
|
|
62
|
+
*/
|
|
63
|
+
onAgentReady?(agent: Agent): void;
|
|
59
64
|
}
|
|
60
65
|
export interface AgentManagerOptions {
|
|
61
66
|
callbacks: ManagerCallbacks;
|