@d-id/client-sdk 1.0.19-beta.32 → 1.0.19-beta.34
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 +70 -72
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const y = "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
|
|
11
|
+
function b(e, s = y) {
|
|
12
12
|
const n = async (t, r) => {
|
|
13
13
|
const a = await fetch(s + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
14
14
|
...r,
|
|
@@ -54,8 +54,8 @@ function M(e, s = R) {
|
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
function F(e, s =
|
|
58
|
-
const n =
|
|
57
|
+
function F(e, s = y) {
|
|
58
|
+
const n = b(e, `${s}/agents`);
|
|
59
59
|
return {
|
|
60
60
|
create(t, r) {
|
|
61
61
|
return n.post("/", t, r);
|
|
@@ -80,8 +80,8 @@ function F(e, s = R) {
|
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
|
-
function
|
|
84
|
-
const n =
|
|
83
|
+
function X(e, s = y) {
|
|
84
|
+
const n = b(e, `${s}/knowledge`);
|
|
85
85
|
return {
|
|
86
86
|
createKnowledge(t, r) {
|
|
87
87
|
return n.post("/", t, r);
|
|
@@ -117,8 +117,8 @@ function Y(e, s = R) {
|
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
119
|
}
|
|
120
|
-
function
|
|
121
|
-
const n =
|
|
120
|
+
function Y(e, s = y) {
|
|
121
|
+
const n = b(e, `${s}/chats/ratings`);
|
|
122
122
|
return {
|
|
123
123
|
create(t, r) {
|
|
124
124
|
return n.post("/", t, r);
|
|
@@ -134,8 +134,8 @@ function Z(e, s = R) {
|
|
|
134
134
|
}
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
|
-
const
|
|
138
|
-
function
|
|
137
|
+
const Z = (e) => new Promise((s) => setTimeout(s, e));
|
|
138
|
+
function O(e) {
|
|
139
139
|
return new Promise((s, n) => {
|
|
140
140
|
const {
|
|
141
141
|
callbacks: t,
|
|
@@ -154,23 +154,23 @@ function j(e) {
|
|
|
154
154
|
};
|
|
155
155
|
});
|
|
156
156
|
}
|
|
157
|
-
async function
|
|
157
|
+
async function j(e) {
|
|
158
158
|
const {
|
|
159
159
|
retries: s = 1
|
|
160
160
|
} = e;
|
|
161
161
|
let n = null;
|
|
162
162
|
for (let t = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; t++)
|
|
163
163
|
try {
|
|
164
|
-
n = await
|
|
164
|
+
n = await O(e);
|
|
165
165
|
} catch (r) {
|
|
166
166
|
if (t === s)
|
|
167
167
|
throw r;
|
|
168
|
-
await
|
|
168
|
+
await Z(t * 500);
|
|
169
169
|
}
|
|
170
170
|
return n;
|
|
171
171
|
}
|
|
172
|
-
async function
|
|
173
|
-
const t = n ? [n] : [], r = await
|
|
172
|
+
async function ee(e, s, n) {
|
|
173
|
+
const t = n ? [n] : [], r = await j({
|
|
174
174
|
auth: e,
|
|
175
175
|
host: s,
|
|
176
176
|
callbacks: {
|
|
@@ -186,8 +186,8 @@ async function te(e, s, n) {
|
|
|
186
186
|
subscribeToEvents: (a) => t.push(a)
|
|
187
187
|
};
|
|
188
188
|
}
|
|
189
|
-
var
|
|
190
|
-
function
|
|
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 || {}), _ = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(_ || {}), I = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e))(I || {}), 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 || {}), P = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(P || {});
|
|
190
|
+
function ce(e) {
|
|
191
191
|
return e.presenter.type === P.Clip ? {
|
|
192
192
|
videoType: P.Clip,
|
|
193
193
|
driver_id: e.presenter.driver_id,
|
|
@@ -199,7 +199,7 @@ function de(e) {
|
|
|
199
199
|
}
|
|
200
200
|
function H(e, s, n, t) {
|
|
201
201
|
return new Promise(async (r, a) => {
|
|
202
|
-
const o = await
|
|
202
|
+
const o = await ge(ce(e), {
|
|
203
203
|
...s,
|
|
204
204
|
callbacks: {
|
|
205
205
|
...s.callbacks,
|
|
@@ -222,11 +222,11 @@ function H(e, s, n, t) {
|
|
|
222
222
|
});
|
|
223
223
|
});
|
|
224
224
|
}
|
|
225
|
-
function
|
|
226
|
-
return F(s, n ||
|
|
225
|
+
function pe(e, s, n) {
|
|
226
|
+
return F(s, n || y).getById(e);
|
|
227
227
|
}
|
|
228
|
-
async function
|
|
229
|
-
const n = s.baseURL ||
|
|
228
|
+
async function we(e, s) {
|
|
229
|
+
const n = s.baseURL || y, t = s.wsURL || Q, r = new AbortController(), a = F(s.auth, n), o = Y(s.auth, n), l = X(s.auth, n), m = await a.getById(e), f = await ee(s.auth, t, s.callbacks.onChatEvents);
|
|
230
230
|
let {
|
|
231
231
|
chat: g,
|
|
232
232
|
streamingManager: d
|
|
@@ -280,8 +280,8 @@ async function he(e, s) {
|
|
|
280
280
|
}
|
|
281
281
|
};
|
|
282
282
|
}
|
|
283
|
-
function
|
|
284
|
-
const n =
|
|
283
|
+
function de(e, s) {
|
|
284
|
+
const n = b(e, s);
|
|
285
285
|
return {
|
|
286
286
|
createStream(t) {
|
|
287
287
|
return n.post("/clips/streams", {
|
|
@@ -315,8 +315,8 @@ function le(e, s) {
|
|
|
315
315
|
}
|
|
316
316
|
};
|
|
317
317
|
}
|
|
318
|
-
function
|
|
319
|
-
const n =
|
|
318
|
+
function le(e, s) {
|
|
319
|
+
const n = b(e, s);
|
|
320
320
|
return {
|
|
321
321
|
createStream(t, r) {
|
|
322
322
|
return n.post("/talks/streams", {
|
|
@@ -351,7 +351,7 @@ function ue(e, s) {
|
|
|
351
351
|
}
|
|
352
352
|
};
|
|
353
353
|
}
|
|
354
|
-
function
|
|
354
|
+
function ue(e, s) {
|
|
355
355
|
return e.map((n, t) => t === 0 ? s ? {
|
|
356
356
|
index: t,
|
|
357
357
|
timestamp: n.timestamp,
|
|
@@ -385,12 +385,12 @@ function me(e, s) {
|
|
|
385
385
|
});
|
|
386
386
|
}
|
|
387
387
|
let T = !1;
|
|
388
|
-
const
|
|
389
|
-
async function
|
|
388
|
+
const k = (e, s) => T && console.log(e, s), me = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
389
|
+
async function ge(e, {
|
|
390
390
|
debug: s = !1,
|
|
391
391
|
callbacks: n,
|
|
392
392
|
auth: t,
|
|
393
|
-
baseURL: r =
|
|
393
|
+
baseURL: r = y
|
|
394
394
|
}) {
|
|
395
395
|
T = s;
|
|
396
396
|
const a = {
|
|
@@ -401,38 +401,36 @@ async function fe(e, {
|
|
|
401
401
|
close: m,
|
|
402
402
|
createStream: f,
|
|
403
403
|
addIceCandidate: g
|
|
404
|
-
} = e.videoType === P.Clip ?
|
|
404
|
+
} = e.videoType === P.Clip ? de(t, r) : le(t, r), {
|
|
405
405
|
id: d,
|
|
406
406
|
offer: c,
|
|
407
407
|
ice_servers: w,
|
|
408
408
|
session_id: h
|
|
409
|
-
} = await f(e), u = new
|
|
409
|
+
} = await f(e), u = new me({
|
|
410
410
|
iceServers: w
|
|
411
411
|
}), x = u.createDataChannel("JanusDataChannel");
|
|
412
|
-
let S = [],
|
|
412
|
+
let S = [], $ = 0, M = 0, R, E, W;
|
|
413
413
|
if (!h)
|
|
414
414
|
throw new Error("Could not create session_id");
|
|
415
415
|
(() => {
|
|
416
416
|
W = setInterval(() => {
|
|
417
417
|
u.getStats().then((p) => {
|
|
418
418
|
p.forEach((v) => {
|
|
419
|
-
var C
|
|
419
|
+
var C;
|
|
420
420
|
if (v.type === "inbound-rtp" && v.kind === "video") {
|
|
421
|
-
if (
|
|
422
|
-
const
|
|
423
|
-
let
|
|
424
|
-
|
|
425
|
-
let
|
|
426
|
-
if (
|
|
427
|
-
if (
|
|
428
|
-
|
|
429
|
-
}), k)
|
|
430
|
-
_ = S.length;
|
|
421
|
+
if (M = S.length - 1, v && S[M]) {
|
|
422
|
+
const A = v.bytesReceived, N = S[M].bytesReceived;
|
|
423
|
+
let z = R;
|
|
424
|
+
R = A - N > 0;
|
|
425
|
+
let L;
|
|
426
|
+
if (z !== R) {
|
|
427
|
+
if (R)
|
|
428
|
+
$ = S.length;
|
|
431
429
|
else {
|
|
432
|
-
const
|
|
433
|
-
|
|
430
|
+
const J = S.slice($), D = $ === 0 ? void 0 : S[$ - 1];
|
|
431
|
+
L = ue(J, D), L = L.sort((V, G) => G.packetsLost - V.packetsLost).slice(0, 5);
|
|
434
432
|
}
|
|
435
|
-
(
|
|
433
|
+
(C = a.onVideoStateChange) == null || C.call(a, R ? _.Start : _.Stop, L);
|
|
436
434
|
}
|
|
437
435
|
}
|
|
438
436
|
S.push(v);
|
|
@@ -445,29 +443,29 @@ async function fe(e, {
|
|
|
445
443
|
clearInterval(E);
|
|
446
444
|
};
|
|
447
445
|
u.onicecandidate = (i) => {
|
|
448
|
-
|
|
446
|
+
k("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && g(d, {
|
|
449
447
|
candidate: i.candidate.candidate,
|
|
450
448
|
sdpMid: i.candidate.sdpMid,
|
|
451
449
|
sdpMLineIndex: i.candidate.sdpMLineIndex
|
|
452
450
|
}, h);
|
|
453
451
|
}, u.oniceconnectionstatechange = () => {
|
|
454
452
|
var i;
|
|
455
|
-
|
|
453
|
+
k("peerConnection.oniceconnectionstatechange => " + u.iceConnectionState), (i = a.onConnectionStateChange) == null || i.call(a, u.iceConnectionState);
|
|
456
454
|
}, u.ontrack = (i) => {
|
|
457
455
|
var p;
|
|
458
|
-
|
|
456
|
+
k("peerConnection.ontrack", i), (p = a.onSrcObjectReady) == null || p.call(a, i.streams[0]);
|
|
459
457
|
}, x.onmessage = (i) => {
|
|
460
458
|
var p, v;
|
|
461
459
|
if (x.readyState === "open") {
|
|
462
|
-
const [C,
|
|
463
|
-
C === I.StreamStarted ? console.log("StreamStarted", C,
|
|
460
|
+
const [C, A] = i.data.split(":");
|
|
461
|
+
C === I.StreamStarted ? console.log("StreamStarted", C, A) : C === I.StreamDone ? console.log("StreamDone") : C === I.StreamFailed ? ((p = a.onVideoStateChange) == null || p.call(a, _.Stop, {
|
|
464
462
|
event: C,
|
|
465
|
-
data:
|
|
466
|
-
}), S = [],
|
|
463
|
+
data: A
|
|
464
|
+
}), S = [], $ = 0, M = 0, R = !1, console.log("StreamFailed")) : (v = a.onMessage) == null || v.call(a, C, decodeURIComponent(A));
|
|
467
465
|
}
|
|
468
|
-
}, await u.setRemoteDescription(c),
|
|
466
|
+
}, await u.setRemoteDescription(c), k("set remote description OK");
|
|
469
467
|
const B = await u.createAnswer();
|
|
470
|
-
return
|
|
468
|
+
return k("create answer OK"), await u.setLocalDescription(B), k("set local description OK"), await o(d, B, h), k("start connection OK"), {
|
|
471
469
|
/**
|
|
472
470
|
* Method to send request to server to get clip or talk depend on you payload
|
|
473
471
|
* @param payload
|
|
@@ -481,7 +479,7 @@ async function fe(e, {
|
|
|
481
479
|
async terminate() {
|
|
482
480
|
var i, p;
|
|
483
481
|
d && (u && (u.close(), u.oniceconnectionstatechange = null, u.onnegotiationneeded = null, u.onicecandidate = null, u.ontrack = null, clearInterval(E)), await m(d, h).catch((v) => {
|
|
484
|
-
}), (i = a.onConnectionStateChange) == null || i.call(a, "closed"), (p = a.onVideoStateChange) == null || p.call(a,
|
|
482
|
+
}), (i = a.onConnectionStateChange) == null || i.call(a, "closed"), (p = a.onVideoStateChange) == null || p.call(a, _.Stop), q(), clearInterval(W));
|
|
485
483
|
},
|
|
486
484
|
/**
|
|
487
485
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
@@ -502,22 +500,22 @@ async function fe(e, {
|
|
|
502
500
|
};
|
|
503
501
|
}
|
|
504
502
|
export {
|
|
505
|
-
|
|
503
|
+
ae as ChatMode,
|
|
506
504
|
K as ChatProgress,
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
505
|
+
oe as DocumentType,
|
|
506
|
+
ie as KnowledgeType,
|
|
507
|
+
te as Providers,
|
|
508
|
+
re as RateState,
|
|
509
|
+
ee as SocketManager,
|
|
512
510
|
I as StreamEvents,
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
511
|
+
se as Subject,
|
|
512
|
+
ne as VoiceAccess,
|
|
513
|
+
we as createAgentManager,
|
|
516
514
|
F as createAgentsApi,
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
515
|
+
b as createClient,
|
|
516
|
+
X as createKnowledgeApi,
|
|
517
|
+
Y as createRatingsApi,
|
|
518
|
+
ge as createStreamingManager,
|
|
519
|
+
pe as getAgent,
|
|
520
|
+
ce as getAgentStreamArgs
|
|
523
521
|
};
|
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 A(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=A(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=A(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=A(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||{}),_=(e=>(e.Start="START",e.Stop="STOP",e))(_||{}),I=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e))(I||{}),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||{}),P=(e=>(e.Clip="clip",e.Talk="talk",e))(P||{});function G(e){return e.presenter.type===P.Clip?{videoType:P.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:P.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===I.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(S=>(S==null?void 0:S.starter_message)||[]):Promise.resolve([])}}}function se(e,i){const n=A(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=A(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 k=(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===P.Clip?se(t,a):oe(t,a),{id:l,offer:d,ice_servers:v,session_id:S}=await w(e),g=new de({iceServers:v}),Z=g.createDataChannel("JanusDataChannel");let R=[],b=0,K=0,$,x,O;if(!S)throw new Error("Could not create session_id");(()=>{O=setInterval(()=>{g.getStats().then(h=>{h.forEach(C=>{var y,M;if(C.type==="inbound-rtp"&&C.kind==="video"){if(K=R.length-1,C&&R[K]){const ue=C.bytesReceived,ge=R[K].bytesReceived;let me=$;$=ue-ge>0;let E;if(me!==$){if((y=r.onVideoStateChange)==null||y.call(r,$?_.Start:_.Stop,{isPlaying:$}),$)b=R.length;else{const fe=R.slice(b),we=b===0?void 0:R[b-1];E=ce(fe,we),E=E.sort((he,pe)=>pe.packetsLost-he.packetsLost).slice(0,5)}(M=r.onVideoStateChange)==null||M.call(r,_.Stop,E)}}R.push(C)}})})},500)})();const le=()=>{clearInterval(x)};g.onicecandidate=o=>{k("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},S)},g.oniceconnectionstatechange=()=>{var o;k("peerConnection.oniceconnectionstatechange => "+g.iceConnectionState),(o=r.onConnectionStateChange)==null||o.call(r,g.iceConnectionState)},g.ontrack=o=>{var h;k("peerConnection.ontrack",o),(h=r.onSrcObjectReady)==null||h.call(r,o.streams[0])},Z.onmessage=o=>{var h,C;if(Z.readyState==="open"){const[y,M]=o.data.split(":");y===I.StreamStarted?console.log("StreamStarted",y,M):y===I.StreamDone?console.log("StreamDone"):y===I.StreamFailed?((h=r.onVideoStateChange)==null||h.call(r,_.Stop,{event:y,data:M}),R=[],b=0,K=0,$=!1,console.log("StreamFailed")):(C=r.onMessage)==null||C.call(r,y,decodeURIComponent(M))}},await g.setRemoteDescription(d),k("set remote description OK");const j=await g.createAnswer();return k("create answer OK"),await g.setLocalDescription(j),k("set local description OK"),await c(l,j,S),k("start connection OK"),{speak(o){return u(l,S,o)},async terminate(){var o,h;l&&(g&&(g.close(),g.oniceconnectionstatechange=null,g.onnegotiationneeded=null,g.onicecandidate=null,g.ontrack=null,clearInterval(x)),await m(l,S).catch(C=>{}),(o=r.onConnectionStateChange)==null||o.call(r,"closed"),(h=r.onVideoStateChange)==null||h.call(r,_.Stop),le(),clearInterval(O))},sessionId:S,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=I,s.Subject=J,s.VoiceAccess=q,s.createAgentManager=ie,s.createAgentsApi=T,s.createClient=A,s.createKnowledgeApi=B,s.createRatingsApi=H,s.createStreamingManager=Y,s.getAgent=re,s.getAgentStreamArgs=G,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
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"})});
|