@d-id/client-sdk 1.0.19-beta.27 → 1.0.19-beta.28
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 +21 -21
- 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", N = "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 M(e, i =
|
|
11
|
+
function M(e, i = y) {
|
|
12
12
|
const n = async (t, r) => {
|
|
13
13
|
const a = await fetch(i + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
14
14
|
...r,
|
|
@@ -54,7 +54,7 @@ function M(e, i = R) {
|
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
function T(e, i =
|
|
57
|
+
function T(e, i = y) {
|
|
58
58
|
const n = M(e, `${i}/agents`);
|
|
59
59
|
return {
|
|
60
60
|
create(t, r) {
|
|
@@ -80,7 +80,7 @@ function T(e, i = R) {
|
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
|
-
function z(e, i =
|
|
83
|
+
function z(e, i = y) {
|
|
84
84
|
const n = M(e, `${i}/knowledge`);
|
|
85
85
|
return {
|
|
86
86
|
createKnowledge(t, r) {
|
|
@@ -117,7 +117,7 @@ function z(e, i = R) {
|
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
119
|
}
|
|
120
|
-
function J(e, i =
|
|
120
|
+
function J(e, i = y) {
|
|
121
121
|
const n = M(e, `${i}/chats/ratings`);
|
|
122
122
|
return {
|
|
123
123
|
create(t, r) {
|
|
@@ -223,10 +223,10 @@ function H(e, i, n, t) {
|
|
|
223
223
|
});
|
|
224
224
|
}
|
|
225
225
|
function de(e, i, n) {
|
|
226
|
-
return T(i, n ||
|
|
226
|
+
return T(i, n || y).getById(e);
|
|
227
227
|
}
|
|
228
228
|
async function le(e, i) {
|
|
229
|
-
const n = i.baseURL ||
|
|
229
|
+
const n = i.baseURL || y, t = i.wsURL || N, r = new AbortController(), a = T(i.auth, n), o = J(i.auth, n), l = z(i.auth, n), g = await a.getById(e), f = await Q(i.auth, t, i.callbacks.onChatEvents);
|
|
230
230
|
let {
|
|
231
231
|
chat: m,
|
|
232
232
|
streamingManager: d
|
|
@@ -385,12 +385,12 @@ function ie(e, i) {
|
|
|
385
385
|
});
|
|
386
386
|
}
|
|
387
387
|
let q = !1;
|
|
388
|
-
const
|
|
388
|
+
const k = (e, i) => q && console.log(e, i), se = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
389
389
|
async function oe(e, {
|
|
390
390
|
debug: i = !1,
|
|
391
391
|
callbacks: n,
|
|
392
392
|
auth: t,
|
|
393
|
-
baseURL: r =
|
|
393
|
+
baseURL: r = y
|
|
394
394
|
}) {
|
|
395
395
|
q = i;
|
|
396
396
|
const a = {
|
|
@@ -408,7 +408,7 @@ async function oe(e, {
|
|
|
408
408
|
session_id: h
|
|
409
409
|
} = await f(e), u = new se({
|
|
410
410
|
iceServers: w
|
|
411
|
-
}), E = u.createDataChannel("JanusDataChannel"),
|
|
411
|
+
}), E = u.createDataChannel("JanusDataChannel"), R = [];
|
|
412
412
|
let S = 0, $, x, W;
|
|
413
413
|
if (!h)
|
|
414
414
|
throw new Error("Could not create session_id");
|
|
@@ -418,15 +418,15 @@ async function oe(e, {
|
|
|
418
418
|
p.forEach((v) => {
|
|
419
419
|
var C;
|
|
420
420
|
if (v.type === "inbound-rtp" && v.kind === "video") {
|
|
421
|
-
if (S =
|
|
422
|
-
const A = v.bytesReceived, _ =
|
|
421
|
+
if (S = R.length - 1, console.log("report ", S), v && R[S]) {
|
|
422
|
+
const A = v.bytesReceived, _ = R[S].bytesReceived;
|
|
423
423
|
console.log("ontrack interval i <=", A, _);
|
|
424
424
|
let K = $;
|
|
425
425
|
$ = A - _ > 0, K !== $ && ((C = a.onVideoStateChange) == null || C.call(a, $ ? I.Start : I.Stop, {
|
|
426
426
|
isPlaying: $
|
|
427
427
|
})), console.log(" ontrack interval isPlaying?", $);
|
|
428
428
|
}
|
|
429
|
-
|
|
429
|
+
R.push(v);
|
|
430
430
|
}
|
|
431
431
|
});
|
|
432
432
|
});
|
|
@@ -436,17 +436,17 @@ async function oe(e, {
|
|
|
436
436
|
clearInterval(x), console.log("clearInterval palying?");
|
|
437
437
|
};
|
|
438
438
|
u.onicecandidate = (s) => {
|
|
439
|
-
|
|
439
|
+
k("peerConnection.onicecandidate", s), s.candidate && s.candidate.sdpMid && s.candidate.sdpMLineIndex !== null && m(d, {
|
|
440
440
|
candidate: s.candidate.candidate,
|
|
441
441
|
sdpMid: s.candidate.sdpMid,
|
|
442
442
|
sdpMLineIndex: s.candidate.sdpMLineIndex
|
|
443
443
|
}, h);
|
|
444
444
|
}, u.oniceconnectionstatechange = () => {
|
|
445
445
|
var s;
|
|
446
|
-
|
|
446
|
+
k("peerConnection.oniceconnectionstatechange => " + u.iceConnectionState), (s = a.onConnectionStateChange) == null || s.call(a, u.iceConnectionState);
|
|
447
447
|
}, u.ontrack = (s) => {
|
|
448
448
|
var p;
|
|
449
|
-
|
|
449
|
+
k("peerConnection.ontrack", s), (p = a.onSrcObjectReady) == null || p.call(a, s.streams[0]);
|
|
450
450
|
}, E.onmessage = (s) => {
|
|
451
451
|
var p, v;
|
|
452
452
|
if (E.readyState === "open") {
|
|
@@ -455,10 +455,10 @@ async function oe(e, {
|
|
|
455
455
|
console.log("StreamStarted", C, A);
|
|
456
456
|
else if (C === P.StreamDone) {
|
|
457
457
|
console.log("StreamDone");
|
|
458
|
-
const _ =
|
|
458
|
+
const _ = R.slice(S);
|
|
459
459
|
if (_) {
|
|
460
|
-
const K = S === 0 ? void 0 :
|
|
461
|
-
ie(_, K)
|
|
460
|
+
const K = S === 0 ? void 0 : R[S - 1];
|
|
461
|
+
ie(_, K);
|
|
462
462
|
}
|
|
463
463
|
} else
|
|
464
464
|
C === P.StreamFailed ? (p = a.onVideoStateChange) == null || p.call(a, I.Stop, {
|
|
@@ -466,9 +466,9 @@ async function oe(e, {
|
|
|
466
466
|
data: A
|
|
467
467
|
}) : (v = a.onMessage) == null || v.call(a, C, decodeURIComponent(A));
|
|
468
468
|
}
|
|
469
|
-
}, await u.setRemoteDescription(c),
|
|
469
|
+
}, await u.setRemoteDescription(c), k("set remote description OK");
|
|
470
470
|
const B = await u.createAnswer();
|
|
471
|
-
return
|
|
471
|
+
return k("create answer OK"), await u.setLocalDescription(B), k("set local description OK"), await o(d, B, h), k("start connection OK"), {
|
|
472
472
|
/**
|
|
473
473
|
* Method to send request to server to get clip or talk depend on you payload
|
|
474
474
|
* @param payload
|
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 E(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 q=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(q||{}),F=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(F||{}),M=(e=>(e.Start="START",e.Stop="STOP",e))(M||{}),_=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e))(_||{}),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||{}),b=(e=>(e.Clip="clip",e.Talk="talk",e))(b||{});function G(e){return e.presenter.type===b.Clip?{videoType:b.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:b.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===_.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 E(i,n||p).getById(e)}async function ie(e,i){const n=i.baseURL||p,t=i.wsURL||ee,a=new AbortController,r=E(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=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 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===b.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=0,I,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=$.length-1,console.log("report ",k),S&&$[k]){const P=S.bytesReceived,K=$[k].bytesReceived;console.log("ontrack interval i <=",P,K);let T=I;I=P-K>0,T!==I&&((y=r.onVideoStateChange)==null||y.call(r,I?M.Start:M.Stop,{isPlaying:I})),console.log(" ontrack interval isPlaying?",I)}$.push(S)}})})},500)})();const le=()=>{clearInterval(O),console.log("clearInterval palying?")};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,P]=o.data.split(":");if(y===_.StreamStarted)console.log("StreamStarted",y,P);else if(y===_.StreamDone){console.log("StreamDone");const K=$.slice(k);if(K){const T=k===0?void 0:$[k-1];ce(K,T),k=$.length}}else y===_.StreamFailed?(h=r.onVideoStateChange)==null||h.call(r,M.Stop,{event:y,data:P}):(S=r.onMessage)==null||S.call(r,y,decodeURIComponent(P))}},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,M.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=q,s.RateState=N,s.SocketManager=U,s.StreamEvents=_,s.Subject=J,s.VoiceAccess=F,s.createAgentManager=ie,s.createAgentsApi=E,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 E(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 q=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(q||{}),F=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(F||{}),M=(e=>(e.Start="START",e.Stop="STOP",e))(M||{}),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||{}),b=(e=>(e.Clip="clip",e.Talk="talk",e))(b||{});function G(e){return e.presenter.type===b.Clip?{videoType:b.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:b.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 E(i,n||p).getById(e)}async function ie(e,i){const n=i.baseURL||p,t=i.wsURL||ee,a=new AbortController,r=E(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 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===b.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 R=0,I,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(R=_.length-1,console.log("report ",R),S&&_[R]){const P=S.bytesReceived,K=_[R].bytesReceived;console.log("ontrack interval i <=",P,K);let T=I;I=P-K>0,T!==I&&((y=r.onVideoStateChange)==null||y.call(r,I?M.Start:M.Stop,{isPlaying:I})),console.log(" ontrack interval isPlaying?",I)}_.push(S)}})})},500)})();const le=()=>{clearInterval(O),console.log("clearInterval palying?")};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},C)},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,S;if(Z.readyState==="open"){const[y,P]=o.data.split(":");if(y===A.StreamStarted)console.log("StreamStarted",y,P);else if(y===A.StreamDone){console.log("StreamDone");const K=_.slice(R);if(K){const T=R===0?void 0:_[R-1];ce(K,T)}}else y===A.StreamFailed?(h=r.onVideoStateChange)==null||h.call(r,M.Stop,{event:y,data:P}):(S=r.onMessage)==null||S.call(r,y,decodeURIComponent(P))}},await g.setRemoteDescription(d),k("set remote description OK");const x=await g.createAnswer();return k("create answer OK"),await g.setLocalDescription(x),k("set local description OK"),await c(l,x,C),k("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,M.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=q,s.RateState=N,s.SocketManager=U,s.StreamEvents=A,s.Subject=J,s.VoiceAccess=F,s.createAgentManager=ie,s.createAgentsApi=E,s.createClient=$,s.createKnowledgeApi=B,s.createRatingsApi=H,s.createStreamingManager=Y,s.getAgent=re,s.getAgentStreamArgs=G,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|