@d-id/client-sdk 1.0.19-beta.147 → 1.0.19-beta.148
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 +18 -18
- package/dist/index.umd.cjs +1 -1
- package/dist/src/utils/SdkError.d.ts +6 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -676,9 +676,21 @@ function je(e, t, i) {
|
|
|
676
676
|
...i
|
|
677
677
|
};
|
|
678
678
|
}
|
|
679
|
+
class xe extends Error {
|
|
680
|
+
constructor({
|
|
681
|
+
kind: t,
|
|
682
|
+
description: i
|
|
683
|
+
}) {
|
|
684
|
+
const a = JSON.stringify({
|
|
685
|
+
kind: t,
|
|
686
|
+
description: i
|
|
687
|
+
});
|
|
688
|
+
super(a);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
679
691
|
let L = 0;
|
|
680
|
-
const
|
|
681
|
-
function
|
|
692
|
+
const Ne = 45 * 1e3;
|
|
693
|
+
function Be(e, t, i) {
|
|
682
694
|
var a, n, s, r, o;
|
|
683
695
|
return {
|
|
684
696
|
videoType: ue(e.presenter.type),
|
|
@@ -717,18 +729,6 @@ async function ae(e, t, i, a, n) {
|
|
|
717
729
|
throw (r == null ? void 0 : r.kind) === "InsufficientCreditsError" ? new Error("InsufficientCreditsError") : new Error("Cannot create new chat");
|
|
718
730
|
}
|
|
719
731
|
}
|
|
720
|
-
class Be extends Error {
|
|
721
|
-
constructor({
|
|
722
|
-
kind: t,
|
|
723
|
-
description: i
|
|
724
|
-
}) {
|
|
725
|
-
const a = JSON.stringify({
|
|
726
|
-
kind: t,
|
|
727
|
-
description: i
|
|
728
|
-
});
|
|
729
|
-
super(a);
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
732
|
function Fe(e, t, i, a, n, s) {
|
|
733
733
|
return new Promise(async (r, o) => {
|
|
734
734
|
var m, p, k, y, C;
|
|
@@ -741,14 +741,14 @@ function Fe(e, t, i, a, n, s) {
|
|
|
741
741
|
return o(g);
|
|
742
742
|
}
|
|
743
743
|
const d = (n == null ? void 0 : n.chat_mode) || l;
|
|
744
|
-
if (d !== l && (t.mode = d, (p = (m = t.callbacks).onModeChange) == null || p.call(m, d), d === I.TextOnly && ((y = (k = t.callbacks) == null ? void 0 : k.onError) == null || y.call(k, new
|
|
745
|
-
kind: "
|
|
744
|
+
if (d !== l && (t.mode = d, (p = (m = t.callbacks).onModeChange) == null || p.call(m, d), d === I.TextOnly && ((y = (k = t.callbacks) == null ? void 0 : k.onError) == null || y.call(k, new xe({
|
|
745
|
+
kind: "ChatModeDowngraded",
|
|
746
746
|
description: `Chat mode changed from ${l} to ${d} when creating the chat`
|
|
747
747
|
}), {}))), d === I.TextOnly)
|
|
748
748
|
return r({
|
|
749
749
|
chat: n
|
|
750
750
|
});
|
|
751
|
-
const f = await Te(e.id,
|
|
751
|
+
const f = await Te(e.id, Be(e, t, s), {
|
|
752
752
|
...t,
|
|
753
753
|
analytics: a,
|
|
754
754
|
warmup: (C = t.streamOptions) == null ? void 0 : C.streamWarmup,
|
|
@@ -869,7 +869,7 @@ async function Ve(e, t) {
|
|
|
869
869
|
(b = (S = t.callbacks).onConnectionStateChange) == null || b.call(S, D.Connecting), L = 0, c && !a && (delete n.chat, n.messages = Y(f), (A = (E = t.callbacks).onNewMessage) == null || A.call(E, [...n.messages], "answer"));
|
|
870
870
|
const h = t.mode === I.DirectPlayback ? Promise.resolve(void 0) : Re(t.auth, r, p), v = G(() => Fe(d, t, l, m, n.chat, c ? f : void 0), {
|
|
871
871
|
limit: 3,
|
|
872
|
-
timeout:
|
|
872
|
+
timeout: Ne,
|
|
873
873
|
timeoutErrorMessage: "Timeout initializing the stream",
|
|
874
874
|
// Retry on all errors except for connection errors and rate limit errors, these are already handled in client level.
|
|
875
875
|
shouldRetryFn: ($) => ($ == null ? void 0 : $.message) !== "Could not connect" && $.status !== 429,
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(v,F){typeof exports=="object"&&typeof module<"u"?F(exports):typeof define=="function"&&define.amd?define(["exports"],F):(v=typeof globalThis<"u"?globalThis:v||self,F(v.index={}))})(this,function(v){"use strict";var F=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(F||{}),Y=(e=>(e.TRIAL="deid-trial",e.PRO="deid-pro",e.ENTERPRISE="deid-enterprise",e.LITE="deid-lite",e.ADVANCED="deid-advanced",e.BUILD="deid-api-build",e.LAUNCH="deid-api-launch",e.SCALE="deid-api-scale",e))(Y||{}),Z=(e=>(e.Created="created",e.Started="started",e.Done="done",e.Error="error",e.Rejected="rejected",e.Ready="ready",e))(Z||{}),G=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(G||{}),b=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))(b||{}),B=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(B||{}),Q=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(Q||{}),ee=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(ee||{}),te=(e=>(e.Pdf="pdf",e.Text="text",e.Html="html",e.Word="word",e.Json="json",e.Markdown="markdown",e.Csv="csv",e.Excel="excel",e.Powerpoint="powerpoint",e.Archive="archive",e.Image="image",e.Audio="audio",e.Video="video",e))(te||{}),J=(e=>(e.Clip="clip",e.Talk="talk",e))(J||{});const ne=e=>{switch(e){case"clip":return"clip";case"talk":return"talk";default:throw new Error(`Unknown video type: ${e}`)}};var j=(e=>(e.Start="START",e.Stop="STOP",e))(j||{}),V=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e.StreamReady="stream/ready",e.StreamCreated="stream/created",e.StreamVideoCreated="stream-video/started",e.StreamVideoDone="stream-video/done",e.StreamVideoError="stream-video/error",e.StreamVideoRejected="stream-video/rejected",e))(V||{}),D=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(D||{}),re=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(re||{}),ae=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(ae||{});const K="https://api.d-id.com",we="wss://notifications.d-id.com",ye="79f81a83a67430be2bc0fd61042b8faa";async function ie(e,t){const i={limit:(t==null?void 0:t.limit)??3,delayMs:(t==null?void 0:t.delayMs)??0,timeout:(t==null?void 0:t.timeout)??3e4,timeoutErrorMessage:(t==null?void 0:t.timeoutErrorMessage)||"Timeout error",shouldRetryFn:(t==null?void 0:t.shouldRetryFn)??function(){return!0}};let a;for(let n=1;n<=i.limit;n++)try{if(!i.timeout)return await e();let s;const r=new Promise((d,f)=>{e().then(m=>{clearTimeout(s),d(m)}).catch(m=>{clearTimeout(s),f(m)})}),o=new Promise((d,f)=>{s=setTimeout(()=>f(new Error(i.timeoutErrorMessage)),i.timeout)});return await Promise.race([r,o])}catch(s){if(a=s,!i.shouldRetryFn(s)||n>=i.limit)throw s;i.delayMs>0&&await new Promise(r=>setTimeout(r,i.delayMs))}throw a}const x=()=>Math.random().toString(16).slice(2);function se(){let e=window.localStorage.getItem("did_external_key_id");return e||(e=Math.random().toString(16).slice(2),window.localStorage.setItem("did_external_key_id",e)),e}let pe=x();function oe(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}.${se()}_${pe}`;throw new Error(`Unknown auth type: ${e}`)}const ve=e=>ie(e,{limit:3,delayMs:1e3,timeout:0,shouldRetryFn:t=>t.status===429});function O(e,t=K,i){const a=async(n,s)=>{const{skipErrorHandler:r,...o}=s||{},l=await ve(()=>fetch(t+(n!=null&&n.startsWith("/")?n:`/${n}`),{...o,headers:{...o.headers,Authorization:oe(e),"Content-Type":"application/json"}}));if(!l.ok){let d=await l.text().catch(()=>"Failed to fetch");throw i&&!r&&i(new Error(d),{url:n,options:o,headers:l.headers}),new Error(d)}return l.json()};return{get(n,s){return a(n,{...s,method:"GET"})},post(n,s,r){return a(n,{...r,body:JSON.stringify(s),method:"POST"})},delete(n,s,r){return a(n,{...r,body:JSON.stringify(s),method:"DELETE"})},patch(n,s,r){return a(n,{...r,body:JSON.stringify(s),method:"PATCH"})}}}function ce(e,t=K,i){const a=O(e,`${t}/agents`,i);return{create(n,s){return a.post("/",n,s)},getAgents(n,s){return a.get(`/${n?`?tag=${n}`:""}`,s).then(r=>r??[])},getById(n,s){return a.get(`/${n}`,s)},delete(n,s){return a.delete(`/${n}`,void 0,s)},update(n,s,r){return a.patch(`/${n}`,s,r)},newChat(n,s,r){return a.post(`/${n}/chat`,s,r)},chat(n,s,r,o){return a.post(`/${n}/chat/${s}`,r,o)},createRating(n,s,r,o){return a.post(`/${n}/chat/${s}/ratings`,r,o)},updateRating(n,s,r,o,l){return a.patch(`/${n}/chat/${s}/ratings/${r}`,o,l)},deleteRating(n,s,r,o){return a.delete(`/${n}/chat/${s}/ratings/${r}`,o)},getSTTToken(n,s){return a.get(`/${n}/stt-token`,s)}}}const ke=e=>new Promise(t=>setTimeout(t,e));function Ce(e){return new Promise((t,i)=>{const{callbacks:a,host:n,auth:s}=e,{onMessage:r=null,onOpen:o=null,onClose:l=null,onError:d=null}=a||{},f=new WebSocket(`${n}?authorization=${oe(s)}`);f.onmessage=r,f.onclose=l,f.onerror=m=>{console.error(m),d==null||d("Websocket failed to connect",m),i(m)},f.onopen=m=>{o==null||o(m),t(f)}})}async function Se(e){const{retries:t=1}=e;let i=null;for(let a=0;(i==null?void 0:i.readyState)!==WebSocket.OPEN;a++)try{i=await Ce(e)}catch(n){if(a===t)throw n;await ke(a*500)}return i}async function Re(e,t,i){const a=i!=null&&i.onMessage?[i.onMessage]:[],n=await Se({auth:e,host:t,callbacks:{onError:i==null?void 0:i.onError,onMessage:s=>{const r=JSON.parse(s.data);a.forEach(o=>o(r.event,r))}}});return{socket:n,disconnect:()=>n.close(),subscribeToEvents:s=>a.push(s)}}const De="X-Playground-Chat";function Me(e,t,i,a){const n=O(e,`${t}/agents/${i}`,a);return{createStream(s){return n.post("/streams",{output_resolution:s.output_resolution,compatibility_mode:s.compatibility_mode,stream_warmup:s.stream_warmup,session_timeout:s.session_timeout,stream_greeting:s.stream_greeting})},startConnection(s,r,o){return n.post(`/streams/${s}/sdp`,{session_id:o,answer:r})},addIceCandidate(s,r,o){return n.post(`/streams/${s}/ice`,{session_id:o,...r})},sendStreamRequest(s,r,o){return n.post(`/streams/${s}`,{session_id:r,...o})},close(s,r){return n.delete(`/streams/${s}`,{session_id:r})}}}function _e(e,t,i,a){const n=O(e,`${t}/agents/${i}`,a);return{createStream(s,r){return n.post("/streams",{driver_url:s.driver_url,face:s.face,config:s.config,output_resolution:s.output_resolution,compatibility_mode:s.compatibility_mode,stream_warmup:s.stream_warmup,session_timeout:s.session_timeout,stream_greeting:s.stream_greeting},r)},startConnection(s,r,o,l){return n.post(`/streams/${s}/sdp`,{session_id:o,answer:r},l)},addIceCandidate(s,r,o,l){return n.post(`/streams/${s}/ice`,{session_id:o,...r},l)},sendStreamRequest(s,r,o,l){return n.post(`/streams/${s}`,{session_id:r,...o},l)},close(s,r,o){return n.delete(`/streams/${s}`,{session_id:r},o)}}}function be(e,t,i){const a=(t.timestamp-e.timestamp)/1e3;return{duration:a,bytesReceived:t.bytesReceived-e.bytesReceived,bitrate:Math.round((t.bytesReceived-e.bytesReceived)*8/a),packetsReceived:t.packetsReceived-e.packetsReceived,packetsLost:t.packetsLost-e.packetsLost,framesDropped:t.framesDropped-e.framesDropped,framesDecoded:t.framesDecoded-e.framesDecoded,jitter:t.jitter,jitterBufferDelay:(t.jitterBufferDelay-e.jitterBufferDelay)/a,framesPerSecond:t.framesPerSecond,freezeCount:t.freezeCount-e.freezeCount,freezeDuration:t.freezeDuration-e.freezeDuration,lowFpsCount:i}}function Ee(e){return e.filter(t=>t.freezeCount>0||t.framesPerSecond<21||t.framesDropped>0||t.packetsLost>0).map(t=>{const{timestamp:i,...a}=t,n=[];return t.freezeCount>0&&n.push("freeze"),t.framesPerSecond<21&&n.push("low fps"),t.framesDropped>0&&n.push("frames dropped"),t.packetsLost>0&&n.push("packet loss"),{...a,causes:n}})}function Pe(e){let t="";for(const i of e.values())if(i&&i.type==="codec"&&i.mimeType.startsWith("video")&&(t=i.mimeType.split("/")[1]),i&&i.type==="inbound-rtp"&&i.kind==="video")return{codec:t,timestamp:i.timestamp,bytesReceived:i.bytesReceived,packetsReceived:i.packetsReceived,packetsLost:i.packetsLost,framesDropped:i.framesDropped,framesDecoded:i.framesDecoded,jitter:i.jitter,jitterBufferDelay:i.jitterBufferDelay,frameWidth:i.frameWidth,frameHeight:i.frameHeight,framesPerSecond:i.framesPerSecond,freezeCount:i.freezeCount,freezeDuration:i.totalFreezesDuration};return{}}function Ie(e,t,i){const a=e.map((r,o)=>o===0?i?{timestamp:r.timestamp,duration:0,bytesReceived:r.bytesReceived-i.bytesReceived,bitrate:(r.bytesReceived-i.bytesReceived)*8/(t/1e3),packetsReceived:r.packetsReceived-i.packetsReceived,packetsLost:r.packetsLost-i.packetsLost,framesDropped:r.framesDropped-i.framesDropped,framesDecoded:r.framesDecoded-i.framesDecoded,jitter:r.jitter,jitterBufferDelay:r.jitterBufferDelay-i.jitterBufferDelay,framesPerSecond:r.framesPerSecond,freezeCount:r.freezeCount-i.freezeCount,freezeDuration:r.freezeDuration-i.freezeDuration}:{timestamp:r.timestamp,duration:0,bytesReceived:r.bytesReceived,bitrate:r.bytesReceived*8/(t/1e3),packetsReceived:r.packetsReceived,packetsLost:r.packetsLost,framesDropped:r.framesDropped,framesDecoded:r.framesDecoded,jitter:r.jitter,jitterBufferDelay:r.jitterBufferDelay,framesPerSecond:r.framesPerSecond,freezeCount:r.freezeCount,freezeDuration:r.freezeDuration}:{timestamp:r.timestamp,duration:t*o/1e3,bytesReceived:r.bytesReceived-e[o-1].bytesReceived,bitrate:(r.bytesReceived-e[o-1].bytesReceived)*8/(t/1e3),packetsReceived:r.packetsReceived-e[o-1].packetsReceived,packetsLost:r.packetsLost-e[o-1].packetsLost,framesDropped:r.framesDropped-e[o-1].framesDropped,framesDecoded:r.framesDecoded-e[o-1].framesDecoded,jitter:r.jitter,jitterBufferDelay:r.jitterBufferDelay-e[o-1].jitterBufferDelay,framesPerSecond:r.framesPerSecond,freezeCount:r.freezeCount-e[o-1].freezeCount,freezeDuration:r.freezeDuration-e[o-1].freezeDuration}),n=Ee(a),s=n.reduce((r,o)=>r+(o.causes.includes("low fps")?1:0),0);return{webRTCStats:{anomalies:n,aggregateReport:be(e[0],e[e.length-1],s)},codec:e[0].codec,resolution:`${e[0].frameWidth}x${e[0].frameHeight}`}}let de=!1;const z=(e,t)=>de&&console.log(e,t),Te=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function le(e){switch(e){case"connected":return D.Connected;case"checking":return D.Connecting;case"failed":return D.Fail;case"new":return D.New;case"closed":return D.Closed;case"disconnected":return D.Disconnected;case"completed":return D.Completed;default:return D.New}}function Ae(){let e=0;return t=>{for(const i of t.values())if(i&&i.type==="inbound-rtp"&&i.kind==="video"){const a=i.bytesReceived,n=a-e>0;return e=a,n}return!1}}function $e(e,t,i=!1,a,n){const r=Math.max(Math.ceil(10),1);let o=[],l,d=0,f=!1;const m=i?1:0;let y=0;const C=Ae();return setInterval(async()=>{const p=await e.getStats(),S=C(p),g=Pe(p);if(S)d=0,f||(t==null||t(j.Start),y>=m&&!a()&&n(),l=o[o.length-1],o=[],y++,f=!0),o.push(g);else if(f&&(d++,d>=r)){const R=Ie(o,100,l);t==null||t(j.Stop,R),f=!1}},100)}async function je(e,t,{debug:i=!1,callbacks:a,auth:n,baseURL:s=K,warmup:r}){de=i;const{startConnection:o,sendStreamRequest:l,close:d,createStream:f,addIceCandidate:m}=t.videoType===J.Clip?Me(n,s,e,a.onError):_e(n,s,e,a.onError),{id:y,offer:C,ice_servers:p,session_id:S}=await f(t),g=new Te({iceServers:p}),R=g.createDataChannel("JanusDataChannel");if(!S)throw new Error("Could not create session_id");let E=!1;const c=()=>E,h=()=>{var u;E=!0,(u=a.onConnectionStateChange)==null||u.call(a,D.Connected)},k=$e(g,a.onVideoStateChange,r,c,h);g.onicecandidate=u=>{var w;z("peerConnection.onicecandidate",u);try{u.candidate&&u.candidate.sdpMid&&u.candidate.sdpMLineIndex!==null?m(y,{candidate:u.candidate.candidate,sdpMid:u.candidate.sdpMid,sdpMLineIndex:u.candidate.sdpMLineIndex},S):m(y,{candidate:null},S)}catch(M){(w=a.onError)==null||w.call(a,M,{streamId:y})}},R.onmessage=u=>{if(R.readyState==="open"){const[w,M]=u.data.split(":");w===V.StreamReady&&!E&&h()}},g.oniceconnectionstatechange=()=>{var w;z("peerConnection.oniceconnectionstatechange => "+g.iceConnectionState);const u=le(g.iceConnectionState);u!==D.Connected&&((w=a.onConnectionStateChange)==null||w.call(a,u))},g.ontrack=u=>{var w;z("peerConnection.ontrack",u),(w=a.onSrcObjectReady)==null||w.call(a,u.streams[0])},await g.setRemoteDescription(C),z("set remote description OK");const _=await g.createAnswer();return z("create answer OK"),await g.setLocalDescription(_),z("set local description OK"),await o(y,_,S),z("start connection OK"),{speak(u){return l(y,S,u)},async disconnect(){var u,w;if(y){const M=le(g.iceConnectionState);if(g){if(M===D.New){(u=a.onVideoStateChange)==null||u.call(a,j.Stop),clearInterval(k);return}g.close(),g.oniceconnectionstatechange=null,g.onnegotiationneeded=null,g.onicecandidate=null,g.ontrack=null}try{M===D.Connected&&await d(y,S).catch(P=>{})}catch(P){z("Error on close stream connection",P)}(w=a.onVideoStateChange)==null||w.call(a,j.Stop),clearInterval(k)}},sessionId:S,streamId:y}}let X={};function ze(e){var s,r,o,l,d;const t=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",i=e.agent.presenter,a=(s=e.agent.llm)==null?void 0:s.prompt_customization,n={token:e.token||"testKey",distinct_id:e.distinctId||se(),agentId:e.agent.id,agentType:i.type==="clip"&&i.presenter_id.startsWith("v2_")?"clip_v2":i.type,owner_id:e.agent.owner_id??"",behavior:{role:a==null?void 0:a.role,personality:a==null?void 0:a.personality,instructions:(r=e.agent.llm)==null?void 0:r.instructions},temperature:(o=e.agent.llm)==null?void 0:o.temperature,knowledgeSource:a==null?void 0:a.knowledge_source,starterQuestionsCount:(d=(l=e.agent.knowledge)==null?void 0:l.starter_message)==null?void 0:d.length,topicsToAvoid:a==null?void 0:a.topics_to_avoid,maxResponseLength:a==null?void 0:a.max_response_length};return{...n,additionalProperties:{},isEnabled:e.isEnabled??!0,getRandom:()=>Math.random().toString(16).slice(2),enrich(f){const m={};if(f&&typeof f!="object")throw new Error("properties must be a flat json object");for(let y in f)(typeof f[y]=="string"||typeof f[y]=="number")&&(m[y]=f[y]);this.additionalProperties={...this.additionalProperties,...m}},track(f,m){if(!this.isEnabled)return Promise.resolve();const{audioPath:y,...C}=m||{},p={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:f,properties:{...this.additionalProperties,...C,...n,source:t,time:Date.now(),$insert_id:this.getRandom(),origin:window.location.href,"Screen Height":window.screen.height||window.innerWidth,"Screen Width":window.screen.width||window.innerHeight,"User Agent":navigator.userAgent}}])})};return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",p).then(S=>S.json()).catch(S=>console.error(S))},linkTrack(f,m,y,C){X[f]||(X[f]={events:{},resolvedDependencies:[]}),C.includes(y)||C.push(y);const p=X[f];if(p.events[y]={props:m},p.resolvedDependencies.push(y),C.every(g=>p.resolvedDependencies.includes(g))){const g=C.reduce((R,E)=>p.events[E]?{...R,...p.events[E].props}:R,{});this.track(f,g),p.resolvedDependencies=p.resolvedDependencies.filter(R=>!C.includes(R)),C.forEach(R=>{delete p.events[R]})}}}}function Le(e){var n,s,r,o;const t=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",i=()=>{const l=navigator.platform;return l.toLowerCase().includes("win")?"Windows":l.toLowerCase().includes("mac")?"Mac OS X":l.toLowerCase().includes("linux")?"Linux":"Unknown"},a=e.presenter;return{$os:`${i()}`,isMobile:`${t()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:a.type==="clip"&&a.presenter_id.startsWith("v2_")?"clip_v2":a.type,agentVoice:{voiceId:(s=(n=e.presenter)==null?void 0:n.voice)==null?void 0:s.voice_id,provider:(o=(r=e.presenter)==null?void 0:r.voice)==null?void 0:o.type}}}function Ne(e,t,i){var d,f;const{event:a,...n}=e,{template:s}=(t==null?void 0:t.llm)||{},{language:r}=((d=t==null?void 0:t.presenter)==null?void 0:d.voice)||{},{stitch:o}=(t==null?void 0:t.presenter)||{};return{...n,llm:{...n.llm,template:s},script:{...n.script,provider:{...(f=n==null?void 0:n.script)==null?void 0:f.provider,language:r}},stitch:o,...i}}let L=0;const Be=45*1e3;function Fe(e,t,i){var a,n,s,r,o;return{videoType:ne(e.presenter.type),output_resolution:(a=t==null?void 0:t.streamOptions)==null?void 0:a.outputResolution,session_timeout:(n=t==null?void 0:t.streamOptions)==null?void 0:n.sessionTimeout,stream_warmup:(s=t==null?void 0:t.streamOptions)==null?void 0:s.streamWarmup,compatibility_mode:(r=t==null?void 0:t.streamOptions)==null?void 0:r.compatibilityMode,stream_greeting:(o=t==null?void 0:t.streamOptions)!=null&&o.streamGreeting?i:void 0}}function me(e){return e===b.Playground?{headers:{[De]:"true"}}:{}}async function ue(e,t,i,a,n){try{const s=await t.newChat(e,{persist:n??!1},me(a));return i.track("agent-chat",{event:"created",chat_id:s.id,agent_id:e,mode:a}),s}catch(s){let r;try{r=JSON.parse(s.message)}catch(o){console.error("Error parsing the error message:",o)}throw(r==null?void 0:r.kind)==="InsufficientCreditsError"?new Error("InsufficientCreditsError"):new Error("Cannot create new chat")}}class Ve extends Error{constructor({kind:t,description:i}){const a=JSON.stringify({kind:t,description:i});super(a)}}function We(e,t,i,a,n,s){return new Promise(async(r,o)=>{var m,y,C,p,S;L=0;const l=String(t.mode);if(!n&&t.mode!==b.DirectPlayback)try{n=await ue(e.id,i,a,t.mode,t.persistentChat)}catch(g){return o(g)}const d=(n==null?void 0:n.chat_mode)||l;if(d!==l&&(t.mode=d,(y=(m=t.callbacks).onModeChange)==null||y.call(m,d),d===b.TextOnly&&((p=(C=t.callbacks)==null?void 0:C.onError)==null||p.call(C,new Ve({kind:"ModeUnavailable",description:`Chat mode changed from ${l} to ${d} when creating the chat`}),{}))),d===b.TextOnly)return r({chat:n});const f=await je(e.id,Fe(e,t,s),{...t,analytics:a,warmup:(S=t.streamOptions)==null?void 0:S.streamWarmup,callbacks:{...t.callbacks,onConnectionStateChange:async g=>{var R,E,c,h;g===D.Connected?f?((E=(R=t.callbacks).onConnectionStateChange)==null||E.call(R,g),r({chat:n,streamingManager:f})):n&&o(new Error("Something went wrong while initializing the manager")):(h=(c=t.callbacks).onConnectionStateChange)==null||h.call(c,g)},onVideoStateChange(g,R){var E,c;(c=(E=t.callbacks).onVideoStateChange)==null||c.call(E,g),L>0&&(g===j.Start?a.linkTrack("agent-video",{event:"start",latency:Date.now()-L},"start",[V.StreamVideoCreated]):g===j.Stop&&a.linkTrack("agent-video",{event:"stop",is_greenscreen:e.presenter.type==="clip"&&e.presenter.is_greenscreen,background:e.presenter.type==="clip"&&e.presenter.background,...R},"done",[V.StreamVideoDone]))}}}).catch(o)})}function He(e){var i;const t=(i=e.greetings)==null?void 0:i.filter(a=>a.length>0);if(t&&t.length>0){const a=Math.floor(Math.random()*t.length);return t[a]}else return`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`}function fe(e,t){return t&&t.length>0?t:[{content:e,id:x(),role:"assistant",created_at:new Date().toISOString()}]}function Ke(e){if(e.answer!==void 0)return e.answer;let t=0,i="";for(;t in e;)i+=e[t],t++;return i}function Ue(e,t,i,a,n){if(!(e===B.Partial||e===B.Answer))return;const s=a.messages[a.messages.length-1];if((s==null?void 0:s.role)!=="assistant")return;const{content:r,sequence:o}=t;e===B.Partial?i[o]=r:i.answer=r;const l=Ke(i);(s.content!==l||e===B.Answer)&&(s.content=l,n==null||n([...a.messages],e))}async function xe(e,t){var g,R,E;let i={},a=!0;const n={messages:[],chatMode:t.mode||b.Functional},s=t.baseURL||K,r=t.wsURL||we,o=t.mixpanelKey||ye,l=ce(t.auth,s,t.callbacks.onError),d=await l.getById(e),f=He(d);n.messages=fe(f,t.initialMessages),(R=(g=t.callbacks).onNewMessage)==null||R.call(g,[...n.messages],"answer");const m=ze({token:o,agent:d,isEnabled:t.enableAnalitics,distinctId:t.distinctId});m.track("agent-sdk",{event:"loaded",...Le(d)});const y={onMessage:(c,h)=>{var k,_;if("content"in h)Ue(c,h,i,n,t.callbacks.onNewMessage),c===B.Answer&&m.track("agent-message-received",{messages:n.messages.length,mode:n.chatMode});else{const u=V,w=[u.StreamVideoDone,u.StreamVideoError,u.StreamVideoRejected],M=[u.StreamFailed,u.StreamVideoError,u.StreamVideoRejected],P=Ne(h,d,{mode:n.chatMode});if(c=c,c===u.StreamVideoCreated)m.linkTrack("agent-video",P,u.StreamVideoCreated,["start"]);else if(w.includes(c)){const I=c.split("/")[1];M.includes(c)?m.track("agent-video",{...P,event:I}):m.linkTrack("agent-video",{...P,event:I},c,["done"])}M.includes(c)&&((_=(k=t.callbacks).onError)==null||_.call(k,new Error(`Stream failed with event ${c}`),{data:h})),h.event===u.StreamDone&&p()}}};async function C(c){var M,P,I,$,U,N,T;(P=(M=t.callbacks).onConnectionStateChange)==null||P.call(M,D.Connecting),L=0,c&&!a&&(delete n.chat,n.messages=fe(f),($=(I=t.callbacks).onNewMessage)==null||$.call(I,[...n.messages],"answer"));const h=t.mode===b.DirectPlayback?Promise.resolve(void 0):Re(t.auth,r,y),k=ie(()=>We(d,t,l,m,n.chat,c?f:void 0),{limit:3,timeout:Be,timeoutErrorMessage:"Timeout initializing the stream",shouldRetryFn:A=>(A==null?void 0:A.message)!=="Could not connect"&&A.status!==429,delayMs:1e3}).catch(A=>{var W,H;throw S(b.Maintenance),(H=(W=t.callbacks).onConnectionStateChange)==null||H.call(W,D.Fail),A}),[_,{streamingManager:u,chat:w}]=await Promise.all([h,k]);w&&w.id!==((U=n.chat)==null?void 0:U.id)&&((T=(N=t.callbacks).onNewChat)==null||T.call(N,w.id)),n.streamingManager=u,n.socketManager=_,n.chat=w,a=!1,S((w==null?void 0:w.chat_mode)??t.mode??b.Functional)}async function p(){var c,h,k,_;(c=n.socketManager)==null||c.disconnect(),await((h=n.streamingManager)==null?void 0:h.disconnect()),delete n.streamingManager,delete n.socketManager,(_=(k=t.callbacks).onConnectionStateChange)==null||_.call(k,D.Disconnected)}async function S(c){var h,k;c!==n.chatMode&&(m.track("agent-mode-change",{mode:c}),n.chatMode=c,n.chatMode!==b.Functional&&await p(),(k=(h=t.callbacks).onModeChange)==null||k.call(h,c))}return{agent:d,starterMessages:((E=d.knowledge)==null?void 0:E.starter_message)||[],getSTTToken:()=>l.getSTTToken(d.id),changeMode:S,enrichAnalytics:m.enrich,async connect(){var c;await C(!0),m.track("agent-chat",{event:"connect",chatId:(c=n.chat)==null?void 0:c.id,agentId:d.id,mode:n.chatMode})},async reconnect(){var c;await p(),await C(!1),m.track("agent-chat",{event:"reconnect",chatId:(c=n.chat)==null?void 0:c.id,agentId:d.id,mode:n.chatMode})},async disconnect(){var c;await p(),m.track("agent-chat",{event:"disconnect",chatId:(c=n.chat)==null?void 0:c.id,agentId:d.id,mode:n.chatMode})},async chat(c){var k,_,u,w,M,P;const h=x();i={};try{if(L=Date.now(),t.mode===b.DirectPlayback)throw new Error("Direct playback is enabled, chat is disabled");if(c.length>=800)throw new Error("Message cannot be more than 800 characters");if(c.length===0)throw new Error("Message cannot be empty");if(n.chatMode===b.Maintenance)throw new Error("Chat is in maintenance mode");if(![b.TextOnly,b.Playground].includes(n.chatMode))if(n.streamingManager){if(!n.chat)throw new Error("Chat is not initialized")}else throw new Error("Streaming manager is not initialized");n.messages.push({id:x(),role:"user",content:c,created_at:new Date(L).toISOString()}),(_=(k=t.callbacks).onNewMessage)==null||_.call(k,[...n.messages],"user"),n.chat||(n.chat=await ue(d.id,l,m,n.chatMode,t.persistentChat),(w=(u=t.callbacks).onNewChat)==null||w.call(u,n.chat.id));const I={id:h,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},$=[...n.messages];n.messages.push(I);const U=T=>{var A,W;return l.chat(d.id,T,{sessionId:(A=n.streamingManager)==null?void 0:A.sessionId,streamId:(W=n.streamingManager)==null?void 0:W.streamId,chatMode:n.chatMode,messages:$.map(({matches:H,...q})=>q)},{...me(n.chatMode),skipErrorHandler:!0})},N=await U(n.chat.id).catch(async T=>{var H,q,ge,he;const A=(H=T==null?void 0:T.message)==null?void 0:H.includes("missing or invalid session_id");if(!((q=T==null?void 0:T.message)==null?void 0:q.includes("Stream Error"))&&!A)throw(he=(ge=t.callbacks).onError)==null||he.call(ge,T,{}),T;return await p(),await C(!1),U(n.chat.id)});return m.track("agent-message-send",{event:"success",mode:n.chatMode,messages:n.messages.length+1}),I.context=N.context,I.matches=N.matches,N.result&&(I.content=N.result,m.track("agent-message-received",{latency:Date.now()-L,mode:n.chatMode,messages:n.messages.length}),(P=(M=t.callbacks).onNewMessage)==null||P.call(M,[...n.messages],"answer")),N}catch(I){throw n.messages[n.messages.length-1].id===h&&n.messages.pop(),m.track("agent-message-send",{event:"error",mode:n.chatMode,messages:n.messages.length}),I}},rate(c,h,k){var w,M,P,I;const _=n.messages.find($=>$.id===c);if(n.chat){if(!_)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const u=((w=_.matches)==null?void 0:w.map($=>[$.document_id,$.id]))??[];return m.track("agent-rate",{event:k?"update":"create",thumb:h===1?"up":"down",knowledge_id:((M=d.knowledge)==null?void 0:M.id)??"",mode:n.chatMode,matches:u,score:h}),k?l.updateRating(d.id,n.chat.id,k,{knowledge_id:((P=d.knowledge)==null?void 0:P.id)??"",message_id:c,matches:u,score:h}):l.createRating(d.id,n.chat.id,{knowledge_id:((I=d.knowledge)==null?void 0:I.id)??"",message_id:c,matches:u,score:h})},deleteRate(c){var h;if(!n.chat)throw new Error("Chat is not initialized");return m.track("agent-rate-delete",{type:"text",chat_id:(h=n.chat)==null?void 0:h.id,id:c,mode:n.chatMode}),l.deleteRating(d.id,n.chat.id,c)},speak(c){if(!n.streamingManager)throw new Error("Please connect to the agent first");L=Date.now();function h(){if(typeof c=="string"){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c,ssml:!1}}if(c.type==="text"&&!c.provider){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c.input,ssml:c.ssml}}return c}const k=h();return m.track("agent-speak",k),n.streamingManager.speak({script:k})}}}function qe(e,t,i){const{getById:a}=ce(t,i||K);return a(e)}v.AgentStatus=Z,v.ChatMode=b,v.ChatProgress=B,v.ConnectionState=D,v.DocumentType=te,v.KnowledgeType=ee,v.PlanGroup=Y,v.Providers=re,v.RateState=G,v.StreamEvents=V,v.StreamingState=j,v.Subject=Q,v.UserPlan=F,v.VideoType=J,v.VoiceAccess=ae,v.createAgentManager=xe,v.getAgent=qe,v.mapVideoType=ne,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(v,F){typeof exports=="object"&&typeof module<"u"?F(exports):typeof define=="function"&&define.amd?define(["exports"],F):(v=typeof globalThis<"u"?globalThis:v||self,F(v.index={}))})(this,function(v){"use strict";var F=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(F||{}),Y=(e=>(e.TRIAL="deid-trial",e.PRO="deid-pro",e.ENTERPRISE="deid-enterprise",e.LITE="deid-lite",e.ADVANCED="deid-advanced",e.BUILD="deid-api-build",e.LAUNCH="deid-api-launch",e.SCALE="deid-api-scale",e))(Y||{}),Z=(e=>(e.Created="created",e.Started="started",e.Done="done",e.Error="error",e.Rejected="rejected",e.Ready="ready",e))(Z||{}),G=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(G||{}),b=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))(b||{}),B=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(B||{}),Q=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(Q||{}),ee=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(ee||{}),te=(e=>(e.Pdf="pdf",e.Text="text",e.Html="html",e.Word="word",e.Json="json",e.Markdown="markdown",e.Csv="csv",e.Excel="excel",e.Powerpoint="powerpoint",e.Archive="archive",e.Image="image",e.Audio="audio",e.Video="video",e))(te||{}),J=(e=>(e.Clip="clip",e.Talk="talk",e))(J||{});const ne=e=>{switch(e){case"clip":return"clip";case"talk":return"talk";default:throw new Error(`Unknown video type: ${e}`)}};var j=(e=>(e.Start="START",e.Stop="STOP",e))(j||{}),V=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e.StreamReady="stream/ready",e.StreamCreated="stream/created",e.StreamVideoCreated="stream-video/started",e.StreamVideoDone="stream-video/done",e.StreamVideoError="stream-video/error",e.StreamVideoRejected="stream-video/rejected",e))(V||{}),D=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(D||{}),re=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(re||{}),ae=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(ae||{});const K="https://api.d-id.com",we="wss://notifications.d-id.com",ye="79f81a83a67430be2bc0fd61042b8faa";async function ie(e,t){const i={limit:(t==null?void 0:t.limit)??3,delayMs:(t==null?void 0:t.delayMs)??0,timeout:(t==null?void 0:t.timeout)??3e4,timeoutErrorMessage:(t==null?void 0:t.timeoutErrorMessage)||"Timeout error",shouldRetryFn:(t==null?void 0:t.shouldRetryFn)??function(){return!0}};let a;for(let n=1;n<=i.limit;n++)try{if(!i.timeout)return await e();let s;const r=new Promise((d,f)=>{e().then(m=>{clearTimeout(s),d(m)}).catch(m=>{clearTimeout(s),f(m)})}),o=new Promise((d,f)=>{s=setTimeout(()=>f(new Error(i.timeoutErrorMessage)),i.timeout)});return await Promise.race([r,o])}catch(s){if(a=s,!i.shouldRetryFn(s)||n>=i.limit)throw s;i.delayMs>0&&await new Promise(r=>setTimeout(r,i.delayMs))}throw a}const x=()=>Math.random().toString(16).slice(2);function se(){let e=window.localStorage.getItem("did_external_key_id");return e||(e=Math.random().toString(16).slice(2),window.localStorage.setItem("did_external_key_id",e)),e}let pe=x();function oe(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}.${se()}_${pe}`;throw new Error(`Unknown auth type: ${e}`)}const ve=e=>ie(e,{limit:3,delayMs:1e3,timeout:0,shouldRetryFn:t=>t.status===429});function O(e,t=K,i){const a=async(n,s)=>{const{skipErrorHandler:r,...o}=s||{},l=await ve(()=>fetch(t+(n!=null&&n.startsWith("/")?n:`/${n}`),{...o,headers:{...o.headers,Authorization:oe(e),"Content-Type":"application/json"}}));if(!l.ok){let d=await l.text().catch(()=>"Failed to fetch");throw i&&!r&&i(new Error(d),{url:n,options:o,headers:l.headers}),new Error(d)}return l.json()};return{get(n,s){return a(n,{...s,method:"GET"})},post(n,s,r){return a(n,{...r,body:JSON.stringify(s),method:"POST"})},delete(n,s,r){return a(n,{...r,body:JSON.stringify(s),method:"DELETE"})},patch(n,s,r){return a(n,{...r,body:JSON.stringify(s),method:"PATCH"})}}}function ce(e,t=K,i){const a=O(e,`${t}/agents`,i);return{create(n,s){return a.post("/",n,s)},getAgents(n,s){return a.get(`/${n?`?tag=${n}`:""}`,s).then(r=>r??[])},getById(n,s){return a.get(`/${n}`,s)},delete(n,s){return a.delete(`/${n}`,void 0,s)},update(n,s,r){return a.patch(`/${n}`,s,r)},newChat(n,s,r){return a.post(`/${n}/chat`,s,r)},chat(n,s,r,o){return a.post(`/${n}/chat/${s}`,r,o)},createRating(n,s,r,o){return a.post(`/${n}/chat/${s}/ratings`,r,o)},updateRating(n,s,r,o,l){return a.patch(`/${n}/chat/${s}/ratings/${r}`,o,l)},deleteRating(n,s,r,o){return a.delete(`/${n}/chat/${s}/ratings/${r}`,o)},getSTTToken(n,s){return a.get(`/${n}/stt-token`,s)}}}const ke=e=>new Promise(t=>setTimeout(t,e));function Ce(e){return new Promise((t,i)=>{const{callbacks:a,host:n,auth:s}=e,{onMessage:r=null,onOpen:o=null,onClose:l=null,onError:d=null}=a||{},f=new WebSocket(`${n}?authorization=${oe(s)}`);f.onmessage=r,f.onclose=l,f.onerror=m=>{console.error(m),d==null||d("Websocket failed to connect",m),i(m)},f.onopen=m=>{o==null||o(m),t(f)}})}async function Se(e){const{retries:t=1}=e;let i=null;for(let a=0;(i==null?void 0:i.readyState)!==WebSocket.OPEN;a++)try{i=await Ce(e)}catch(n){if(a===t)throw n;await ke(a*500)}return i}async function Re(e,t,i){const a=i!=null&&i.onMessage?[i.onMessage]:[],n=await Se({auth:e,host:t,callbacks:{onError:i==null?void 0:i.onError,onMessage:s=>{const r=JSON.parse(s.data);a.forEach(o=>o(r.event,r))}}});return{socket:n,disconnect:()=>n.close(),subscribeToEvents:s=>a.push(s)}}const De="X-Playground-Chat";function Me(e,t,i,a){const n=O(e,`${t}/agents/${i}`,a);return{createStream(s){return n.post("/streams",{output_resolution:s.output_resolution,compatibility_mode:s.compatibility_mode,stream_warmup:s.stream_warmup,session_timeout:s.session_timeout,stream_greeting:s.stream_greeting})},startConnection(s,r,o){return n.post(`/streams/${s}/sdp`,{session_id:o,answer:r})},addIceCandidate(s,r,o){return n.post(`/streams/${s}/ice`,{session_id:o,...r})},sendStreamRequest(s,r,o){return n.post(`/streams/${s}`,{session_id:r,...o})},close(s,r){return n.delete(`/streams/${s}`,{session_id:r})}}}function _e(e,t,i,a){const n=O(e,`${t}/agents/${i}`,a);return{createStream(s,r){return n.post("/streams",{driver_url:s.driver_url,face:s.face,config:s.config,output_resolution:s.output_resolution,compatibility_mode:s.compatibility_mode,stream_warmup:s.stream_warmup,session_timeout:s.session_timeout,stream_greeting:s.stream_greeting},r)},startConnection(s,r,o,l){return n.post(`/streams/${s}/sdp`,{session_id:o,answer:r},l)},addIceCandidate(s,r,o,l){return n.post(`/streams/${s}/ice`,{session_id:o,...r},l)},sendStreamRequest(s,r,o,l){return n.post(`/streams/${s}`,{session_id:r,...o},l)},close(s,r,o){return n.delete(`/streams/${s}`,{session_id:r},o)}}}function be(e,t,i){const a=(t.timestamp-e.timestamp)/1e3;return{duration:a,bytesReceived:t.bytesReceived-e.bytesReceived,bitrate:Math.round((t.bytesReceived-e.bytesReceived)*8/a),packetsReceived:t.packetsReceived-e.packetsReceived,packetsLost:t.packetsLost-e.packetsLost,framesDropped:t.framesDropped-e.framesDropped,framesDecoded:t.framesDecoded-e.framesDecoded,jitter:t.jitter,jitterBufferDelay:(t.jitterBufferDelay-e.jitterBufferDelay)/a,framesPerSecond:t.framesPerSecond,freezeCount:t.freezeCount-e.freezeCount,freezeDuration:t.freezeDuration-e.freezeDuration,lowFpsCount:i}}function Ee(e){return e.filter(t=>t.freezeCount>0||t.framesPerSecond<21||t.framesDropped>0||t.packetsLost>0).map(t=>{const{timestamp:i,...a}=t,n=[];return t.freezeCount>0&&n.push("freeze"),t.framesPerSecond<21&&n.push("low fps"),t.framesDropped>0&&n.push("frames dropped"),t.packetsLost>0&&n.push("packet loss"),{...a,causes:n}})}function Pe(e){let t="";for(const i of e.values())if(i&&i.type==="codec"&&i.mimeType.startsWith("video")&&(t=i.mimeType.split("/")[1]),i&&i.type==="inbound-rtp"&&i.kind==="video")return{codec:t,timestamp:i.timestamp,bytesReceived:i.bytesReceived,packetsReceived:i.packetsReceived,packetsLost:i.packetsLost,framesDropped:i.framesDropped,framesDecoded:i.framesDecoded,jitter:i.jitter,jitterBufferDelay:i.jitterBufferDelay,frameWidth:i.frameWidth,frameHeight:i.frameHeight,framesPerSecond:i.framesPerSecond,freezeCount:i.freezeCount,freezeDuration:i.totalFreezesDuration};return{}}function Ie(e,t,i){const a=e.map((r,o)=>o===0?i?{timestamp:r.timestamp,duration:0,bytesReceived:r.bytesReceived-i.bytesReceived,bitrate:(r.bytesReceived-i.bytesReceived)*8/(t/1e3),packetsReceived:r.packetsReceived-i.packetsReceived,packetsLost:r.packetsLost-i.packetsLost,framesDropped:r.framesDropped-i.framesDropped,framesDecoded:r.framesDecoded-i.framesDecoded,jitter:r.jitter,jitterBufferDelay:r.jitterBufferDelay-i.jitterBufferDelay,framesPerSecond:r.framesPerSecond,freezeCount:r.freezeCount-i.freezeCount,freezeDuration:r.freezeDuration-i.freezeDuration}:{timestamp:r.timestamp,duration:0,bytesReceived:r.bytesReceived,bitrate:r.bytesReceived*8/(t/1e3),packetsReceived:r.packetsReceived,packetsLost:r.packetsLost,framesDropped:r.framesDropped,framesDecoded:r.framesDecoded,jitter:r.jitter,jitterBufferDelay:r.jitterBufferDelay,framesPerSecond:r.framesPerSecond,freezeCount:r.freezeCount,freezeDuration:r.freezeDuration}:{timestamp:r.timestamp,duration:t*o/1e3,bytesReceived:r.bytesReceived-e[o-1].bytesReceived,bitrate:(r.bytesReceived-e[o-1].bytesReceived)*8/(t/1e3),packetsReceived:r.packetsReceived-e[o-1].packetsReceived,packetsLost:r.packetsLost-e[o-1].packetsLost,framesDropped:r.framesDropped-e[o-1].framesDropped,framesDecoded:r.framesDecoded-e[o-1].framesDecoded,jitter:r.jitter,jitterBufferDelay:r.jitterBufferDelay-e[o-1].jitterBufferDelay,framesPerSecond:r.framesPerSecond,freezeCount:r.freezeCount-e[o-1].freezeCount,freezeDuration:r.freezeDuration-e[o-1].freezeDuration}),n=Ee(a),s=n.reduce((r,o)=>r+(o.causes.includes("low fps")?1:0),0);return{webRTCStats:{anomalies:n,aggregateReport:be(e[0],e[e.length-1],s)},codec:e[0].codec,resolution:`${e[0].frameWidth}x${e[0].frameHeight}`}}let de=!1;const z=(e,t)=>de&&console.log(e,t),Te=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function le(e){switch(e){case"connected":return D.Connected;case"checking":return D.Connecting;case"failed":return D.Fail;case"new":return D.New;case"closed":return D.Closed;case"disconnected":return D.Disconnected;case"completed":return D.Completed;default:return D.New}}function Ae(){let e=0;return t=>{for(const i of t.values())if(i&&i.type==="inbound-rtp"&&i.kind==="video"){const a=i.bytesReceived,n=a-e>0;return e=a,n}return!1}}function $e(e,t,i=!1,a,n){const r=Math.max(Math.ceil(10),1);let o=[],l,d=0,f=!1;const m=i?1:0;let y=0;const C=Ae();return setInterval(async()=>{const p=await e.getStats(),S=C(p),g=Pe(p);if(S)d=0,f||(t==null||t(j.Start),y>=m&&!a()&&n(),l=o[o.length-1],o=[],y++,f=!0),o.push(g);else if(f&&(d++,d>=r)){const R=Ie(o,100,l);t==null||t(j.Stop,R),f=!1}},100)}async function je(e,t,{debug:i=!1,callbacks:a,auth:n,baseURL:s=K,warmup:r}){de=i;const{startConnection:o,sendStreamRequest:l,close:d,createStream:f,addIceCandidate:m}=t.videoType===J.Clip?Me(n,s,e,a.onError):_e(n,s,e,a.onError),{id:y,offer:C,ice_servers:p,session_id:S}=await f(t),g=new Te({iceServers:p}),R=g.createDataChannel("JanusDataChannel");if(!S)throw new Error("Could not create session_id");let E=!1;const c=()=>E,h=()=>{var u;E=!0,(u=a.onConnectionStateChange)==null||u.call(a,D.Connected)},k=$e(g,a.onVideoStateChange,r,c,h);g.onicecandidate=u=>{var w;z("peerConnection.onicecandidate",u);try{u.candidate&&u.candidate.sdpMid&&u.candidate.sdpMLineIndex!==null?m(y,{candidate:u.candidate.candidate,sdpMid:u.candidate.sdpMid,sdpMLineIndex:u.candidate.sdpMLineIndex},S):m(y,{candidate:null},S)}catch(M){(w=a.onError)==null||w.call(a,M,{streamId:y})}},R.onmessage=u=>{if(R.readyState==="open"){const[w,M]=u.data.split(":");w===V.StreamReady&&!E&&h()}},g.oniceconnectionstatechange=()=>{var w;z("peerConnection.oniceconnectionstatechange => "+g.iceConnectionState);const u=le(g.iceConnectionState);u!==D.Connected&&((w=a.onConnectionStateChange)==null||w.call(a,u))},g.ontrack=u=>{var w;z("peerConnection.ontrack",u),(w=a.onSrcObjectReady)==null||w.call(a,u.streams[0])},await g.setRemoteDescription(C),z("set remote description OK");const _=await g.createAnswer();return z("create answer OK"),await g.setLocalDescription(_),z("set local description OK"),await o(y,_,S),z("start connection OK"),{speak(u){return l(y,S,u)},async disconnect(){var u,w;if(y){const M=le(g.iceConnectionState);if(g){if(M===D.New){(u=a.onVideoStateChange)==null||u.call(a,j.Stop),clearInterval(k);return}g.close(),g.oniceconnectionstatechange=null,g.onnegotiationneeded=null,g.onicecandidate=null,g.ontrack=null}try{M===D.Connected&&await d(y,S).catch(P=>{})}catch(P){z("Error on close stream connection",P)}(w=a.onVideoStateChange)==null||w.call(a,j.Stop),clearInterval(k)}},sessionId:S,streamId:y}}let X={};function ze(e){var s,r,o,l,d;const t=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",i=e.agent.presenter,a=(s=e.agent.llm)==null?void 0:s.prompt_customization,n={token:e.token||"testKey",distinct_id:e.distinctId||se(),agentId:e.agent.id,agentType:i.type==="clip"&&i.presenter_id.startsWith("v2_")?"clip_v2":i.type,owner_id:e.agent.owner_id??"",behavior:{role:a==null?void 0:a.role,personality:a==null?void 0:a.personality,instructions:(r=e.agent.llm)==null?void 0:r.instructions},temperature:(o=e.agent.llm)==null?void 0:o.temperature,knowledgeSource:a==null?void 0:a.knowledge_source,starterQuestionsCount:(d=(l=e.agent.knowledge)==null?void 0:l.starter_message)==null?void 0:d.length,topicsToAvoid:a==null?void 0:a.topics_to_avoid,maxResponseLength:a==null?void 0:a.max_response_length};return{...n,additionalProperties:{},isEnabled:e.isEnabled??!0,getRandom:()=>Math.random().toString(16).slice(2),enrich(f){const m={};if(f&&typeof f!="object")throw new Error("properties must be a flat json object");for(let y in f)(typeof f[y]=="string"||typeof f[y]=="number")&&(m[y]=f[y]);this.additionalProperties={...this.additionalProperties,...m}},track(f,m){if(!this.isEnabled)return Promise.resolve();const{audioPath:y,...C}=m||{},p={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:f,properties:{...this.additionalProperties,...C,...n,source:t,time:Date.now(),$insert_id:this.getRandom(),origin:window.location.href,"Screen Height":window.screen.height||window.innerWidth,"Screen Width":window.screen.width||window.innerHeight,"User Agent":navigator.userAgent}}])})};return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",p).then(S=>S.json()).catch(S=>console.error(S))},linkTrack(f,m,y,C){X[f]||(X[f]={events:{},resolvedDependencies:[]}),C.includes(y)||C.push(y);const p=X[f];if(p.events[y]={props:m},p.resolvedDependencies.push(y),C.every(g=>p.resolvedDependencies.includes(g))){const g=C.reduce((R,E)=>p.events[E]?{...R,...p.events[E].props}:R,{});this.track(f,g),p.resolvedDependencies=p.resolvedDependencies.filter(R=>!C.includes(R)),C.forEach(R=>{delete p.events[R]})}}}}function Le(e){var n,s,r,o;const t=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",i=()=>{const l=navigator.platform;return l.toLowerCase().includes("win")?"Windows":l.toLowerCase().includes("mac")?"Mac OS X":l.toLowerCase().includes("linux")?"Linux":"Unknown"},a=e.presenter;return{$os:`${i()}`,isMobile:`${t()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:a.type==="clip"&&a.presenter_id.startsWith("v2_")?"clip_v2":a.type,agentVoice:{voiceId:(s=(n=e.presenter)==null?void 0:n.voice)==null?void 0:s.voice_id,provider:(o=(r=e.presenter)==null?void 0:r.voice)==null?void 0:o.type}}}function Ne(e,t,i){var d,f;const{event:a,...n}=e,{template:s}=(t==null?void 0:t.llm)||{},{language:r}=((d=t==null?void 0:t.presenter)==null?void 0:d.voice)||{},{stitch:o}=(t==null?void 0:t.presenter)||{};return{...n,llm:{...n.llm,template:s},script:{...n.script,provider:{...(f=n==null?void 0:n.script)==null?void 0:f.provider,language:r}},stitch:o,...i}}class Be extends Error{constructor({kind:t,description:i}){const a=JSON.stringify({kind:t,description:i});super(a)}}let L=0;const Fe=45*1e3;function Ve(e,t,i){var a,n,s,r,o;return{videoType:ne(e.presenter.type),output_resolution:(a=t==null?void 0:t.streamOptions)==null?void 0:a.outputResolution,session_timeout:(n=t==null?void 0:t.streamOptions)==null?void 0:n.sessionTimeout,stream_warmup:(s=t==null?void 0:t.streamOptions)==null?void 0:s.streamWarmup,compatibility_mode:(r=t==null?void 0:t.streamOptions)==null?void 0:r.compatibilityMode,stream_greeting:(o=t==null?void 0:t.streamOptions)!=null&&o.streamGreeting?i:void 0}}function me(e){return e===b.Playground?{headers:{[De]:"true"}}:{}}async function ue(e,t,i,a,n){try{const s=await t.newChat(e,{persist:n??!1},me(a));return i.track("agent-chat",{event:"created",chat_id:s.id,agent_id:e,mode:a}),s}catch(s){let r;try{r=JSON.parse(s.message)}catch(o){console.error("Error parsing the error message:",o)}throw(r==null?void 0:r.kind)==="InsufficientCreditsError"?new Error("InsufficientCreditsError"):new Error("Cannot create new chat")}}function We(e,t,i,a,n,s){return new Promise(async(r,o)=>{var m,y,C,p,S;L=0;const l=String(t.mode);if(!n&&t.mode!==b.DirectPlayback)try{n=await ue(e.id,i,a,t.mode,t.persistentChat)}catch(g){return o(g)}const d=(n==null?void 0:n.chat_mode)||l;if(d!==l&&(t.mode=d,(y=(m=t.callbacks).onModeChange)==null||y.call(m,d),d===b.TextOnly&&((p=(C=t.callbacks)==null?void 0:C.onError)==null||p.call(C,new Be({kind:"ChatModeDowngraded",description:`Chat mode changed from ${l} to ${d} when creating the chat`}),{}))),d===b.TextOnly)return r({chat:n});const f=await je(e.id,Ve(e,t,s),{...t,analytics:a,warmup:(S=t.streamOptions)==null?void 0:S.streamWarmup,callbacks:{...t.callbacks,onConnectionStateChange:async g=>{var R,E,c,h;g===D.Connected?f?((E=(R=t.callbacks).onConnectionStateChange)==null||E.call(R,g),r({chat:n,streamingManager:f})):n&&o(new Error("Something went wrong while initializing the manager")):(h=(c=t.callbacks).onConnectionStateChange)==null||h.call(c,g)},onVideoStateChange(g,R){var E,c;(c=(E=t.callbacks).onVideoStateChange)==null||c.call(E,g),L>0&&(g===j.Start?a.linkTrack("agent-video",{event:"start",latency:Date.now()-L},"start",[V.StreamVideoCreated]):g===j.Stop&&a.linkTrack("agent-video",{event:"stop",is_greenscreen:e.presenter.type==="clip"&&e.presenter.is_greenscreen,background:e.presenter.type==="clip"&&e.presenter.background,...R},"done",[V.StreamVideoDone]))}}}).catch(o)})}function He(e){var i;const t=(i=e.greetings)==null?void 0:i.filter(a=>a.length>0);if(t&&t.length>0){const a=Math.floor(Math.random()*t.length);return t[a]}else return`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`}function fe(e,t){return t&&t.length>0?t:[{content:e,id:x(),role:"assistant",created_at:new Date().toISOString()}]}function Ke(e){if(e.answer!==void 0)return e.answer;let t=0,i="";for(;t in e;)i+=e[t],t++;return i}function Ue(e,t,i,a,n){if(!(e===B.Partial||e===B.Answer))return;const s=a.messages[a.messages.length-1];if((s==null?void 0:s.role)!=="assistant")return;const{content:r,sequence:o}=t;e===B.Partial?i[o]=r:i.answer=r;const l=Ke(i);(s.content!==l||e===B.Answer)&&(s.content=l,n==null||n([...a.messages],e))}async function xe(e,t){var g,R,E;let i={},a=!0;const n={messages:[],chatMode:t.mode||b.Functional},s=t.baseURL||K,r=t.wsURL||we,o=t.mixpanelKey||ye,l=ce(t.auth,s,t.callbacks.onError),d=await l.getById(e),f=He(d);n.messages=fe(f,t.initialMessages),(R=(g=t.callbacks).onNewMessage)==null||R.call(g,[...n.messages],"answer");const m=ze({token:o,agent:d,isEnabled:t.enableAnalitics,distinctId:t.distinctId});m.track("agent-sdk",{event:"loaded",...Le(d)});const y={onMessage:(c,h)=>{var k,_;if("content"in h)Ue(c,h,i,n,t.callbacks.onNewMessage),c===B.Answer&&m.track("agent-message-received",{messages:n.messages.length,mode:n.chatMode});else{const u=V,w=[u.StreamVideoDone,u.StreamVideoError,u.StreamVideoRejected],M=[u.StreamFailed,u.StreamVideoError,u.StreamVideoRejected],P=Ne(h,d,{mode:n.chatMode});if(c=c,c===u.StreamVideoCreated)m.linkTrack("agent-video",P,u.StreamVideoCreated,["start"]);else if(w.includes(c)){const I=c.split("/")[1];M.includes(c)?m.track("agent-video",{...P,event:I}):m.linkTrack("agent-video",{...P,event:I},c,["done"])}M.includes(c)&&((_=(k=t.callbacks).onError)==null||_.call(k,new Error(`Stream failed with event ${c}`),{data:h})),h.event===u.StreamDone&&p()}}};async function C(c){var M,P,I,$,U,N,T;(P=(M=t.callbacks).onConnectionStateChange)==null||P.call(M,D.Connecting),L=0,c&&!a&&(delete n.chat,n.messages=fe(f),($=(I=t.callbacks).onNewMessage)==null||$.call(I,[...n.messages],"answer"));const h=t.mode===b.DirectPlayback?Promise.resolve(void 0):Re(t.auth,r,y),k=ie(()=>We(d,t,l,m,n.chat,c?f:void 0),{limit:3,timeout:Fe,timeoutErrorMessage:"Timeout initializing the stream",shouldRetryFn:A=>(A==null?void 0:A.message)!=="Could not connect"&&A.status!==429,delayMs:1e3}).catch(A=>{var W,H;throw S(b.Maintenance),(H=(W=t.callbacks).onConnectionStateChange)==null||H.call(W,D.Fail),A}),[_,{streamingManager:u,chat:w}]=await Promise.all([h,k]);w&&w.id!==((U=n.chat)==null?void 0:U.id)&&((T=(N=t.callbacks).onNewChat)==null||T.call(N,w.id)),n.streamingManager=u,n.socketManager=_,n.chat=w,a=!1,S((w==null?void 0:w.chat_mode)??t.mode??b.Functional)}async function p(){var c,h,k,_;(c=n.socketManager)==null||c.disconnect(),await((h=n.streamingManager)==null?void 0:h.disconnect()),delete n.streamingManager,delete n.socketManager,(_=(k=t.callbacks).onConnectionStateChange)==null||_.call(k,D.Disconnected)}async function S(c){var h,k;c!==n.chatMode&&(m.track("agent-mode-change",{mode:c}),n.chatMode=c,n.chatMode!==b.Functional&&await p(),(k=(h=t.callbacks).onModeChange)==null||k.call(h,c))}return{agent:d,starterMessages:((E=d.knowledge)==null?void 0:E.starter_message)||[],getSTTToken:()=>l.getSTTToken(d.id),changeMode:S,enrichAnalytics:m.enrich,async connect(){var c;await C(!0),m.track("agent-chat",{event:"connect",chatId:(c=n.chat)==null?void 0:c.id,agentId:d.id,mode:n.chatMode})},async reconnect(){var c;await p(),await C(!1),m.track("agent-chat",{event:"reconnect",chatId:(c=n.chat)==null?void 0:c.id,agentId:d.id,mode:n.chatMode})},async disconnect(){var c;await p(),m.track("agent-chat",{event:"disconnect",chatId:(c=n.chat)==null?void 0:c.id,agentId:d.id,mode:n.chatMode})},async chat(c){var k,_,u,w,M,P;const h=x();i={};try{if(L=Date.now(),t.mode===b.DirectPlayback)throw new Error("Direct playback is enabled, chat is disabled");if(c.length>=800)throw new Error("Message cannot be more than 800 characters");if(c.length===0)throw new Error("Message cannot be empty");if(n.chatMode===b.Maintenance)throw new Error("Chat is in maintenance mode");if(![b.TextOnly,b.Playground].includes(n.chatMode))if(n.streamingManager){if(!n.chat)throw new Error("Chat is not initialized")}else throw new Error("Streaming manager is not initialized");n.messages.push({id:x(),role:"user",content:c,created_at:new Date(L).toISOString()}),(_=(k=t.callbacks).onNewMessage)==null||_.call(k,[...n.messages],"user"),n.chat||(n.chat=await ue(d.id,l,m,n.chatMode,t.persistentChat),(w=(u=t.callbacks).onNewChat)==null||w.call(u,n.chat.id));const I={id:h,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},$=[...n.messages];n.messages.push(I);const U=T=>{var A,W;return l.chat(d.id,T,{sessionId:(A=n.streamingManager)==null?void 0:A.sessionId,streamId:(W=n.streamingManager)==null?void 0:W.streamId,chatMode:n.chatMode,messages:$.map(({matches:H,...q})=>q)},{...me(n.chatMode),skipErrorHandler:!0})},N=await U(n.chat.id).catch(async T=>{var H,q,ge,he;const A=(H=T==null?void 0:T.message)==null?void 0:H.includes("missing or invalid session_id");if(!((q=T==null?void 0:T.message)==null?void 0:q.includes("Stream Error"))&&!A)throw(he=(ge=t.callbacks).onError)==null||he.call(ge,T,{}),T;return await p(),await C(!1),U(n.chat.id)});return m.track("agent-message-send",{event:"success",mode:n.chatMode,messages:n.messages.length+1}),I.context=N.context,I.matches=N.matches,N.result&&(I.content=N.result,m.track("agent-message-received",{latency:Date.now()-L,mode:n.chatMode,messages:n.messages.length}),(P=(M=t.callbacks).onNewMessage)==null||P.call(M,[...n.messages],"answer")),N}catch(I){throw n.messages[n.messages.length-1].id===h&&n.messages.pop(),m.track("agent-message-send",{event:"error",mode:n.chatMode,messages:n.messages.length}),I}},rate(c,h,k){var w,M,P,I;const _=n.messages.find($=>$.id===c);if(n.chat){if(!_)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const u=((w=_.matches)==null?void 0:w.map($=>[$.document_id,$.id]))??[];return m.track("agent-rate",{event:k?"update":"create",thumb:h===1?"up":"down",knowledge_id:((M=d.knowledge)==null?void 0:M.id)??"",mode:n.chatMode,matches:u,score:h}),k?l.updateRating(d.id,n.chat.id,k,{knowledge_id:((P=d.knowledge)==null?void 0:P.id)??"",message_id:c,matches:u,score:h}):l.createRating(d.id,n.chat.id,{knowledge_id:((I=d.knowledge)==null?void 0:I.id)??"",message_id:c,matches:u,score:h})},deleteRate(c){var h;if(!n.chat)throw new Error("Chat is not initialized");return m.track("agent-rate-delete",{type:"text",chat_id:(h=n.chat)==null?void 0:h.id,id:c,mode:n.chatMode}),l.deleteRating(d.id,n.chat.id,c)},speak(c){if(!n.streamingManager)throw new Error("Please connect to the agent first");L=Date.now();function h(){if(typeof c=="string"){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c,ssml:!1}}if(c.type==="text"&&!c.provider){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c.input,ssml:c.ssml}}return c}const k=h();return m.track("agent-speak",k),n.streamingManager.speak({script:k})}}}function qe(e,t,i){const{getById:a}=ce(t,i||K);return a(e)}v.AgentStatus=Z,v.ChatMode=b,v.ChatProgress=B,v.ConnectionState=D,v.DocumentType=te,v.KnowledgeType=ee,v.PlanGroup=Y,v.Providers=re,v.RateState=G,v.StreamEvents=V,v.StreamingState=j,v.Subject=Q,v.UserPlan=F,v.VideoType=J,v.VoiceAccess=ae,v.createAgentManager=xe,v.getAgent=qe,v.mapVideoType=ne,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
|