@d-id/client-sdk 1.1.0-beta.2 → 1.1.0-beta.3

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.
@@ -1 +1,1789 @@
1
- (function(h,_){typeof exports=="object"&&typeof module<"u"?_(exports):typeof define=="function"&&define.amd?define(["exports"],_):(h=typeof globalThis<"u"?globalThis:h||self,_(h.index={}))})(this,function(h){"use strict";var ot=Object.defineProperty;var ct=(h,_,z)=>_ in h?ot(h,_,{enumerable:!0,configurable:!0,writable:!0,value:z}):h[_]=z;var J=(h,_,z)=>(ct(h,typeof _!="symbol"?_+"":_,z),z);class _ extends Error{constructor({kind:a,description:r,error:s}){super(JSON.stringify({kind:a,description:r}));J(this,"kind");J(this,"description");J(this,"error");this.kind=a,this.description=r,this.error=s}}class z extends _{constructor(t,a){super({kind:"ChatCreationFailed",description:`Failed to create ${a?"persistent":""} chat, mode: ${t}`})}}class te extends _{constructor(t){super({kind:"ChatModeDowngraded",description:`Chat mode downgraded to ${t}`})}}class B extends _{constructor(a,r){super({kind:"ValidationError",description:a});J(this,"key");this.key=r}}class ne extends _{constructor(t){super({kind:"WSError",description:t})}}var re=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(re||{}),ae=(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))(ae||{}),ie=(e=>(e.Created="created",e.Started="started",e.Done="done",e.Error="error",e.Rejected="rejected",e.Ready="ready",e))(ie||{}),se=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(se||{}),P=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))(P||{}),W=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(W||{}),oe=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(oe||{}),ce=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(ce||{}),de=(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))(de||{}),Y=(e=>(e.Clip="clip",e.Talk="talk",e))(Y||{});const le=e=>{switch(e){case"clip":return"clip";case"talk":return"talk";default:throw new Error(`Unknown video type: ${e}`)}};var R=(e=>(e.Start="START",e.Stop="STOP",e))(R||{}),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||{}),S=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(S||{}),me=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(me||{}),ue=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(ue||{});const Me=45*1e3,_e="X-Playground-Chat",x="https://api.d-id.com",Ee="wss://notifications.d-id.com",be="79f81a83a67430be2bc0fd61042b8faa",fe=e=>new Promise(t=>setTimeout(t,e)),X=()=>Math.random().toString(16).slice(2);function Te(e,t){let a;return{promise:new Promise((s,i)=>{a=setTimeout(()=>i(new Error(t)),e)}),clear:()=>clearTimeout(a)}}async function Q(e,t){const a={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)??(()=>!0),onRetry:(t==null?void 0:t.onRetry)??(()=>{})};let r;for(let s=1;s<=a.limit;s++)try{if(!a.timeout)return await e();const{promise:i,clear:n}=Te(a.timeout,a.timeoutErrorMessage),o=e().finally(n);return await Promise.race([o,i])}catch(i){if(r=i,!a.shouldRetryFn(i)||s>=a.limit)throw i;await fe(a.delayMs),a.onRetry(i)}throw r}function ge(){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 he(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}.${ge()}_${Pe}`;throw new Error(`Unknown auth type: ${e}`)}const Ie=e=>Q(e,{limit:3,delayMs:1e3,timeout:0,shouldRetryFn:t=>t.status===429});function Z(e,t=x,a){const r=async(s,i)=>{const{skipErrorHandler:n,...o}=i||{},c=await Ie(()=>fetch(t+(s!=null&&s.startsWith("/")?s:`/${s}`),{...o,headers:{...o.headers,Authorization:he(e),"Content-Type":"application/json"}}));if(!c.ok){let u=await c.text().catch(()=>`Failed to fetch with status ${c.status}`);const d=new Error(u);throw a&&!n&&a(d,{url:s,options:o,headers:c.headers}),d}return c.json()};return{get(s,i){return r(s,{...i,method:"GET"})},post(s,i,n){return r(s,{...n,body:JSON.stringify(i),method:"POST"})},delete(s,i,n){return r(s,{...n,body:JSON.stringify(i),method:"DELETE"})},patch(s,i,n){return r(s,{...n,body:JSON.stringify(i),method:"PATCH"})}}}function we(e,t=x,a){const r=Z(e,`${t}/agents`,a);return{create(s,i){return r.post("/",s,i)},getAgents(s,i){return r.get(`/${s?`?tag=${s}`:""}`,i).then(n=>n??[])},getById(s,i){return r.get(`/${s}`,i)},delete(s,i){return r.delete(`/${s}`,void 0,i)},update(s,i,n){return r.patch(`/${s}`,i,n)},newChat(s,i,n){return r.post(`/${s}/chat`,i,n)},chat(s,i,n,o){return r.post(`/${s}/chat/${i}`,n,o)},createRating(s,i,n,o){return r.post(`/${s}/chat/${i}/ratings`,n,o)},updateRating(s,i,n,o,c){return r.patch(`/${s}/chat/${i}/ratings/${n}`,o,c)},deleteRating(s,i,n,o){return r.delete(`/${s}/chat/${i}/ratings/${n}`,o)},getSTTToken(s,i){return r.get(`/${s}/stt-token`,i)}}}const G=e=>e.type==="clip"&&e.presenter_id.startsWith("v2_")?"clip_v2":e.type;function Ae(e){var s,i,n,o;const t=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",a=()=>{const c=navigator.platform;return c.toLowerCase().includes("win")?"Windows":c.toLowerCase().includes("mac")?"Mac OS X":c.toLowerCase().includes("linux")?"Linux":"Unknown"},r=e.presenter;return{$os:`${a()}`,isMobile:`${t()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:G(r),agentVoice:{voiceId:(i=(s=e.presenter)==null?void 0:s.voice)==null?void 0:i.voice_id,provider:(o=(n=e.presenter)==null?void 0:n.voice)==null?void 0:o.type}}}function $e(e,t,a){var c,u,d;const{event:r,...s}=e,{template:i}=(t==null?void 0:t.llm)||{},{language:n}=((c=t==null?void 0:t.presenter)==null?void 0:c.voice)||{};return{...s,llm:{...s.llm,template:i},script:{...s.script,provider:{...(u=s==null?void 0:s.script)==null?void 0:u.provider,language:n}},stitch:(t==null?void 0:t.presenter.type)==="talk"?(d=t==null?void 0:t.presenter)==null?void 0:d.stitch:void 0,...a}}let O={};const je="https://api-js.mixpanel.com/track/?verbose=1&ip=1";function ze(e){var i,n,o,c,u,d;const t=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",a=e.agent.presenter,r=(i=e.agent.llm)==null?void 0:i.prompt_customization,s={token:e.token||"testKey",distinct_id:e.distinctId||ge(),agentId:e.agent.id,agentType:G(a),owner_id:e.agent.owner_id??"",promptVersion:(n=e.agent.llm)==null?void 0:n.prompt_version,behavior:{role:r==null?void 0:r.role,personality:r==null?void 0:r.personality,instructions:(o=e.agent.llm)==null?void 0:o.instructions},temperature:(c=e.agent.llm)==null?void 0:c.temperature,knowledgeSource:r==null?void 0:r.knowledge_source,starterQuestionsCount:(d=(u=e.agent.knowledge)==null?void 0:u.starter_message)==null?void 0:d.length,topicsToAvoid:r==null?void 0:r.topics_to_avoid,maxResponseLength:r==null?void 0:r.max_response_length};return{...s,additionalProperties:{},isEnabled:e.isEnabled??!0,getRandom:()=>Math.random().toString(16).slice(2),enrich(g){const D={};if(g&&typeof g!="object")throw new Error("properties must be a flat json object");for(let w in g)(typeof g[w]=="string"||typeof g[w]=="number")&&(D[w]=g[w]);this.additionalProperties={...this.additionalProperties,...D}},async track(g,D){if(!this.isEnabled)return Promise.resolve();const{audioPath:w,...v}=D||{},k={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:g,properties:{...this.additionalProperties,...v,...s,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}}])})};try{return await fetch(je,k).then(M=>M.json())}catch(M){return console.error(M)}},linkTrack(g,D,w,v){O[g]||(O[g]={events:{},resolvedDependencies:[]}),v.includes(w)||v.push(w);const k=O[g];if(k.events[w]={props:D},k.resolvedDependencies.push(w),v.every(N=>k.resolvedDependencies.includes(N))){const N=v.reduce((A,l)=>k.events[l]?{...A,...k.events[l].props}:A,{});this.track(g,N),k.resolvedDependencies=k.resolvedDependencies.filter(A=>!v.includes(A)),v.forEach(A=>{delete k.events[A]})}}}}function Le(){let e=0;return{reset:()=>e=0,update:()=>e=Date.now(),get:(t=!1)=>t?Date.now()-e:e}}const H=Le();function pe(e){return e===P.Playground?{headers:{[_e]:"true"}}:{}}async function ye(e,t,a,r,s=!1,i){try{return!i&&r!==P.DirectPlayback&&(i=await t.newChat(e.id,{persist:s},pe(r)),a.track("agent-chat",{event:"created",chat_id:i.id,agent_id:e.id,mode:r})),{chat:i,chatMode:(i==null?void 0:i.chat_mode)??r}}catch(n){try{const o=JSON.parse(n.message);if((o==null?void 0:o.kind)==="InsufficientCreditsError")throw new Error("InsufficientCreditsError")}catch(o){console.error("Error parsing the error message:",o)}throw new Error("Cannot create new chat")}}function Ne(e){var a;const t=((a=e.greetings)==null?void 0:a.filter(r=>r.length>0))??[];return t.length>0?t[Math.floor(Math.random()*t.length)]:`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`}function ve(e,t){return t&&t.length>0?t:[{content:e,id:X(),role:"assistant",created_at:new Date().toISOString()}]}function Fe(e){return new Promise((t,a)=>{const{callbacks:r,host:s,auth:i}=e,{onMessage:n=null,onOpen:o=null,onClose:c=null,onError:u=null}=r||{},d=new WebSocket(`${s}?authorization=${he(i)}`);d.onmessage=n,d.onclose=c,d.onerror=g=>{console.error(g),u==null||u("Websocket failed to connect",g),a(g)},d.onopen=g=>{o==null||o(g),t(d)}})}async function Be(e){const{retries:t=1}=e;let a=null;for(let r=0;(a==null?void 0:a.readyState)!==WebSocket.OPEN;r++)try{a=await Fe(e)}catch(s){if(r===t)throw s;await fe(r*500)}return a}async function We(e,t,a){const r=a!=null&&a.onMessage?[a.onMessage]:[],s=await Be({auth:e,host:t,callbacks:{onError:i=>{var n;return(n=a.onError)==null?void 0:n.call(a,new ne(i))},onMessage(i){const n=JSON.parse(i.data);r.forEach(o=>o(n.event,n))}}});return{socket:s,disconnect:()=>s.close(),subscribeToEvents:i=>r.push(i)}}function He(e){if(e.answer!==void 0)return e.answer;let t=0,a="";for(;t in e;)a+=e[t++];return a}function Ke(e,t,a,r,s){const i=r.messages[r.messages.length-1];if(!(e===W.Partial||e===W.Answer)||(i==null?void 0:i.role)!=="assistant")return;const{content:n,sequence:o}=t;e===W.Partial?a[o]=n:a.answer=n;const c=He(a);(i.content!==c||e===W.Answer)&&(i.content=c,s==null||s([...r.messages],e))}function Ue(e,t,a,r,s){let i={};return{clearQueue:()=>i={},onMessage:(n,o)=>{var c,u;if("content"in o)Ke(n,o,i,t,a.callbacks.onNewMessage),n===W.Answer&&e.track("agent-message-received",{messages:t.messages.length,mode:t.chatMode});else{const d=V,g=[d.StreamVideoDone,d.StreamVideoError,d.StreamVideoRejected],D=[d.StreamFailed,d.StreamVideoError,d.StreamVideoRejected],w=$e(o,r,{mode:t.chatMode});if(n=n,n===d.StreamVideoCreated)e.linkTrack("agent-video",w,d.StreamVideoCreated,["start"]);else if(g.includes(n)){const v=n.split("/")[1];D.includes(n)?e.track("agent-video",{...w,event:v}):e.linkTrack("agent-video",{...w,event:v},n,["done"])}D.includes(n)&&((u=(c=a.callbacks).onError)==null||u.call(c,new Error(`Stream failed with event ${n}`),{data:o})),o.event===d.StreamDone&&s()}}}}function Ve(e,t,a,r){const s=Z(e,`${t}/agents/${a}`,r);return{createStream(i){return s.post("/streams",{output_resolution:i.output_resolution,compatibility_mode:i.compatibility_mode,stream_warmup:i.stream_warmup,session_timeout:i.session_timeout,stream_greeting:i.stream_greeting})},startConnection(i,n,o){return s.post(`/streams/${i}/sdp`,{session_id:o,answer:n})},addIceCandidate(i,n,o){return s.post(`/streams/${i}/ice`,{session_id:o,...n})},sendStreamRequest(i,n,o){return s.post(`/streams/${i}`,{session_id:n,...o})},close(i,n){return s.delete(`/streams/${i}`,{session_id:n})}}}function xe(e,t,a,r){const s=Z(e,`${t}/agents/${a}`,r);return{createStream(i,n){return s.post("/streams",{driver_url:i.driver_url,face:i.face,config:i.config,output_resolution:i.output_resolution,compatibility_mode:i.compatibility_mode,stream_warmup:i.stream_warmup,session_timeout:i.session_timeout,stream_greeting:i.stream_greeting},n)},startConnection(i,n,o,c){return s.post(`/streams/${i}/sdp`,{session_id:o,answer:n},c)},addIceCandidate(i,n,o,c){return s.post(`/streams/${i}/ice`,{session_id:o,...n},c)},sendStreamRequest(i,n,o,c){return s.post(`/streams/${i}`,{session_id:n,...o},c)},close(i,n,o){return s.delete(`/streams/${i}`,{session_id:n},o)}}}function qe(e,t,a){const r=(t.timestamp-e.timestamp)/1e3;return{duration:r,bytesReceived:t.bytesReceived-e.bytesReceived,bitrate:Math.round((t.bytesReceived-e.bytesReceived)*8/r),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)/r,framesPerSecond:t.framesPerSecond,freezeCount:t.freezeCount-e.freezeCount,freezeDuration:t.freezeDuration-e.freezeDuration,lowFpsCount:a}}function Je(e){return e.filter(t=>t.freezeCount>0||t.framesPerSecond<21||t.framesDropped>0||t.packetsLost>0).map(t=>{const{timestamp:a,...r}=t,s=[];return t.freezeCount>0&&s.push("freeze"),t.framesPerSecond<21&&s.push("low fps"),t.framesDropped>0&&s.push("frames dropped"),t.packetsLost>0&&s.push("packet loss"),{...r,causes:s}})}function Xe(e){let t="";for(const a of e.values())if(a&&a.type==="codec"&&a.mimeType.startsWith("video")&&(t=a.mimeType.split("/")[1]),a&&a.type==="inbound-rtp"&&a.kind==="video")return{codec:t,timestamp:a.timestamp,bytesReceived:a.bytesReceived,packetsReceived:a.packetsReceived,packetsLost:a.packetsLost,framesDropped:a.framesDropped,framesDecoded:a.framesDecoded,jitter:a.jitter,jitterBufferDelay:a.jitterBufferDelay,frameWidth:a.frameWidth,frameHeight:a.frameHeight,framesPerSecond:a.framesPerSecond,freezeCount:a.freezeCount,freezeDuration:a.totalFreezesDuration};return{}}function Ye(e,t,a){const r=e.map((n,o)=>o===0?a?{timestamp:n.timestamp,duration:0,bytesReceived:n.bytesReceived-a.bytesReceived,bitrate:(n.bytesReceived-a.bytesReceived)*8/(t/1e3),packetsReceived:n.packetsReceived-a.packetsReceived,packetsLost:n.packetsLost-a.packetsLost,framesDropped:n.framesDropped-a.framesDropped,framesDecoded:n.framesDecoded-a.framesDecoded,jitter:n.jitter,jitterBufferDelay:n.jitterBufferDelay-a.jitterBufferDelay,framesPerSecond:n.framesPerSecond,freezeCount:n.freezeCount-a.freezeCount,freezeDuration:n.freezeDuration-a.freezeDuration}:{timestamp:n.timestamp,duration:0,bytesReceived:n.bytesReceived,bitrate:n.bytesReceived*8/(t/1e3),packetsReceived:n.packetsReceived,packetsLost:n.packetsLost,framesDropped:n.framesDropped,framesDecoded:n.framesDecoded,jitter:n.jitter,jitterBufferDelay:n.jitterBufferDelay,framesPerSecond:n.framesPerSecond,freezeCount:n.freezeCount,freezeDuration:n.freezeDuration}:{timestamp:n.timestamp,duration:t*o/1e3,bytesReceived:n.bytesReceived-e[o-1].bytesReceived,bitrate:(n.bytesReceived-e[o-1].bytesReceived)*8/(t/1e3),packetsReceived:n.packetsReceived-e[o-1].packetsReceived,packetsLost:n.packetsLost-e[o-1].packetsLost,framesDropped:n.framesDropped-e[o-1].framesDropped,framesDecoded:n.framesDecoded-e[o-1].framesDecoded,jitter:n.jitter,jitterBufferDelay:n.jitterBufferDelay-e[o-1].jitterBufferDelay,framesPerSecond:n.framesPerSecond,freezeCount:n.freezeCount-e[o-1].freezeCount,freezeDuration:n.freezeDuration-e[o-1].freezeDuration}),s=Je(r),i=s.reduce((n,o)=>n+(o.causes.includes("low fps")?1:0),0);return{webRTCStats:{anomalies:s,aggregateReport:qe(e[0],e[e.length-1],i)},codec:e[0].codec,resolution:`${e[0].frameWidth}x${e[0].frameHeight}`}}const ee=100,Qe=Math.max(Math.ceil(400/ee),1);function Ze(){let e=0;return t=>{for(const a of t.values())if(a&&a.type==="inbound-rtp"&&a.kind==="video"){const r=a.framesDecoded,s=r-e>0;return e=r,s}return!1}}function Ge(e,t,a,r,s=!1,i=!1){const n=s?1:0;let o=[],c,u=0,d=!1,g=0;const D=Ze();return setInterval(async()=>{const w=await e.getStats(),v=D(w),k=Xe(w);if(v)u=0,d||(r==null||r(R.Start),i&&g>=n&&!t()&&a(),c=o[o.length-1],o=[],g++,d=!0),o.push(k);else if(d&&(u++,u>=Qe)){const M=Ye(o,ee,c);r==null||r(R.Stop,M),!i&&!t()&&a(),d=!1}},ee)}let ke=!1;const L=(e,t)=>ke&&console.log(e,t),Oe=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function Ce(e){switch(e){case"connected":return S.Connected;case"checking":return S.Connecting;case"failed":return S.Fail;case"new":return S.New;case"closed":return S.Closed;case"disconnected":return S.Disconnected;case"completed":return S.Completed;default:return S.New}}function Re(e,t,a,r){e===R.Start&&t===R.Start?a==null||a(R.Start):e===R.Stop&&t===R.Stop&&(a==null||a(R.Stop,r))}async function et(e,t,{debug:a=!1,callbacks:r,auth:s,baseURL:i=x,warmup:n}){ke=a;let o=!1,c=!1,u=R.Stop,d=R.Stop;const{startConnection:g,sendStreamRequest:D,close:w,createStream:v,addIceCandidate:k}=t.videoType===Y.Clip?Ve(s,i,e,r.onError):xe(s,i,e,r.onError),{id:M,offer:N,ice_servers:A,session_id:l}=await v(t),m=new Oe({iceServers:A}),C=m.createDataChannel("JanusDataChannel");if(!l)throw new Error("Could not create session_id");const I=()=>o,$=()=>{var f;o=!0,c&&((f=r.onConnectionStateChange)==null||f.call(r,S.Connected))},E=Ge(m,I,$,(f,p)=>Re(d=f,u,r.onVideoStateChange,p),n,!!t.stream_greeting);m.onicecandidate=f=>{var p;L("peerConnection.onicecandidate",f);try{f.candidate&&f.candidate.sdpMid&&f.candidate.sdpMLineIndex!==null?k(M,{candidate:f.candidate.candidate,sdpMid:f.candidate.sdpMid,sdpMLineIndex:f.candidate.sdpMLineIndex},l):k(M,{candidate:null},l)}catch(y){(p=r.onError)==null||p.call(r,y,{streamId:M})}},C.onopen=()=>{c=!0,(!t.stream_warmup&&!t.stream_greeting||o)&&$()},C.onmessage=f=>{f.data==="stream/started"?u=R.Start:f.data==="stream/done"&&(u=R.Stop),Re(d,u,r.onVideoStateChange)},m.oniceconnectionstatechange=()=>{var p;L("peerConnection.oniceconnectionstatechange => "+m.iceConnectionState);const f=Ce(m.iceConnectionState);f!==S.Connected&&((p=r.onConnectionStateChange)==null||p.call(r,f))},m.ontrack=f=>{var p;L("peerConnection.ontrack",f),(p=r.onSrcObjectReady)==null||p.call(r,f.streams[0])},await m.setRemoteDescription(N),L("set remote description OK");const j=await m.createAnswer();return L("create answer OK"),await m.setLocalDescription(j),L("set local description OK"),await g(M,j,l),L("start connection OK"),{speak(f){return D(M,l,f)},async disconnect(){var f,p;if(M){const y=Ce(m.iceConnectionState);if(m){if(y===S.New){(f=r.onVideoStateChange)==null||f.call(r,R.Stop),clearInterval(E);return}m.close(),m.oniceconnectionstatechange=null,m.onnegotiationneeded=null,m.onicecandidate=null,m.ontrack=null}try{y===S.Connected&&await w(M,l).catch(b=>{})}catch(b){L("Error on close stream connection",b)}(p=r.onVideoStateChange)==null||p.call(r,R.Stop),clearInterval(E)}},sessionId:l,streamId:M}}function tt(e,t,a){var s;const{streamOptions:r}=t??{};return{videoType:le(e.presenter.type),output_resolution:r==null?void 0:r.outputResolution,session_timeout:r==null?void 0:r.sessionTimeout,stream_warmup:r==null?void 0:r.streamWarmup,compatibility_mode:r==null?void 0:r.compatibilityMode,stream_greeting:G(e.presenter)!=="clip"&&((s=t==null?void 0:t.streamOptions)!=null&&s.streamGreeting)?a:void 0}}function nt(e,t,a,r){H.get()>0&&(e===R.Start?r.linkTrack("agent-video",{event:"start",latency:H.get(!0)},"start",[V.StreamVideoCreated]):e===R.Stop&&r.linkTrack("agent-video",{event:"stop",is_greenscreen:t.presenter.type==="clip"&&t.presenter.is_greenscreen,background:t.presenter.type==="clip"&&t.presenter.background,...a},"done",[V.StreamVideoDone]))}function rt(e,t,a,r){return H.reset(),new Promise(async(s,i)=>{var n;try{const o=await et(e.id,tt(e,t,r),{...t,analytics:a,warmup:(n=t.streamOptions)==null?void 0:n.streamWarmup,callbacks:{...t.callbacks,onConnectionStateChange:c=>{var u,d;(d=(u=t.callbacks).onConnectionStateChange)==null||d.call(u,c),c===S.Connected&&s(o)},onVideoStateChange:(c,u)=>{var d,g;(g=(d=t.callbacks).onVideoStateChange)==null||g.call(d,c),nt(c,e,u,a)}}})}catch(o){i(o)}})}async function at(e,t,a,r,s,i){var u,d,g,D;const{chat:n,chatMode:o}=await ye(e,a,r,t.mode,t.persistentChat,s);if(o&&o!==t.mode&&(t.mode=o,(d=(u=t.callbacks).onModeChange)==null||d.call(u,o),o===P.TextOnly))return(D=(g=t.callbacks).onError)==null||D.call(g,new te(o)),{chat:n};const c=await rt(e,t,r,i);return{chat:n,streamingManager:c}}async function it(e,t){var M,N,A;let a=!0;const r=t.mixpanelKey||be,s=t.wsURL||Ee,i=t.baseURL||x,n={messages:[],chatMode:t.mode||P.Functional},o=we(t.auth,i,t.callbacks.onError),c=await o.getById(e),u=Ne(c),d=ze({token:r,agent:c,isEnabled:t.enableAnalitics,distinctId:t.distinctId}),{onMessage:g,clearQueue:D}=Ue(d,n,t,c,()=>{var l;return(l=n.socketManager)==null?void 0:l.disconnect()});n.messages=ve(u,t.initialMessages),(N=(M=t.callbacks).onNewMessage)==null||N.call(M,[...n.messages],"answer"),d.track("agent-sdk",{event:"loaded",...Ae(c)});async function w(l){var j,f,p,y,b,T,K;(f=(j=t.callbacks).onConnectionStateChange)==null||f.call(j,S.Connecting),H.reset(),l&&!a&&(delete n.chat,n.messages=ve(u),(y=(p=t.callbacks).onNewMessage)==null||y.call(p,[...n.messages],"answer"));const m=t.mode===P.DirectPlayback?Promise.resolve(void 0):We(t.auth,s,{onMessage:g,onError:t.callbacks.onError}),C=Q(()=>at(c,t,o,d,n.chat,l?u:void 0),{limit:3,timeout:Me,timeoutErrorMessage:"Timeout initializing the stream",shouldRetryFn:F=>(F==null?void 0:F.message)!=="Could not connect"&&F.status!==429,delayMs:1e3}).catch(F=>{var U,q;throw k(P.Maintenance),(q=(U=t.callbacks).onConnectionStateChange)==null||q.call(U,S.Fail),F}),[I,{streamingManager:$,chat:E}]=await Promise.all([m,C]);E&&E.id!==((b=n.chat)==null?void 0:b.id)&&((K=(T=t.callbacks).onNewChat)==null||K.call(T,E.id)),n.streamingManager=$,n.socketManager=I,n.chat=E,a=!1,k((E==null?void 0:E.chat_mode)??t.mode??P.Functional)}async function v(){var l,m,C,I;(l=n.socketManager)==null||l.disconnect(),await((m=n.streamingManager)==null?void 0:m.disconnect()),delete n.streamingManager,delete n.socketManager,(I=(C=t.callbacks).onConnectionStateChange)==null||I.call(C,S.Disconnected)}async function k(l){var m,C;l!==n.chatMode&&(d.track("agent-mode-change",{mode:l}),n.chatMode=l,n.chatMode!==P.Functional&&await v(),(C=(m=t.callbacks).onModeChange)==null||C.call(m,l))}return{agent:c,starterMessages:((A=c.knowledge)==null?void 0:A.starter_message)||[],getSTTToken:()=>o.getSTTToken(c.id),changeMode:k,enrichAnalytics:d.enrich,async connect(){var l;await w(!0),d.track("agent-chat",{event:"connect",chatId:(l=n.chat)==null?void 0:l.id,agentId:c.id,mode:n.chatMode})},async reconnect(){var l;await v(),await w(!1),d.track("agent-chat",{event:"reconnect",chatId:(l=n.chat)==null?void 0:l.id,agentId:c.id,mode:n.chatMode})},async disconnect(){var l;await v(),d.track("agent-chat",{event:"disconnect",chatId:(l=n.chat)==null?void 0:l.id,agentId:c.id,mode:n.chatMode})},async chat(l){var $,E,j,f,p;const m=()=>{if(t.mode===P.DirectPlayback)throw new B("Direct playback is enabled, chat is disabled");if(l.length>=800)throw new B("Message cannot be more than 800 characters");if(l.length===0)throw new B("Message cannot be empty");if(n.chatMode===P.Maintenance)throw new B("Chat is in maintenance mode");if(![P.TextOnly,P.Playground].includes(n.chatMode)){if(!n.streamingManager)throw new B("Streaming manager is not initialized");if(!n.chat)throw new B("Chat is not initialized")}},C=async()=>{var y,b;if(!n.chat){const T=await ye(c,o,d,n.chatMode,t.persistentChat);if(!T.chat)throw new z(n.chatMode,!!t.persistentChat);n.chat=T.chat,(b=(y=t.callbacks).onNewChat)==null||b.call(y,n.chat.id)}return n.chat.id},I=async(y,b)=>Q(()=>{var T,K;return o.chat(c.id,b,{chatMode:n.chatMode,streamId:(T=n.streamingManager)==null?void 0:T.streamId,sessionId:(K=n.streamingManager)==null?void 0:K.sessionId,messages:y.map(({matches:F,...U})=>U)},{...pe(n.chatMode),skipErrorHandler:!0})},{limit:2,shouldRetryFn:T=>{var U,q,Se,De;const K=(U=T==null?void 0:T.message)==null?void 0:U.includes("missing or invalid session_id");return!((q=T==null?void 0:T.message)==null?void 0:q.includes("Stream Error"))&&!K?((De=(Se=t.callbacks).onError)==null||De.call(Se,T),!1):!0},onRetry:async()=>{await v(),await w(!1)}});try{D(),m(),n.messages.push({id:X(),role:"user",content:l,created_at:new Date(H.update()).toISOString()}),(E=($=t.callbacks).onNewMessage)==null||E.call($,[...n.messages],"user");const y=await C(),b=await I([...n.messages],y);return n.messages.push({id:X(),role:"assistant",content:b.result||"",created_at:new Date().toISOString(),context:b.context,matches:b.matches}),d.track("agent-message-send",{event:"success",mode:n.chatMode,messages:n.messages.length+1}),b.result&&((f=(j=t.callbacks).onNewMessage)==null||f.call(j,[...n.messages],"answer"),d.track("agent-message-received",{latency:H.get(!0),mode:n.chatMode,messages:n.messages.length})),b}catch(y){throw((p=n.messages[n.messages.length-1])==null?void 0:p.role)==="assistant"&&n.messages.pop(),d.track("agent-message-send",{event:"error",mode:n.chatMode,messages:n.messages.length}),y}},rate(l,m,C){var E,j,f,p;const I=n.messages.find(y=>y.id===l);if(n.chat){if(!I)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const $=((E=I.matches)==null?void 0:E.map(y=>[y.document_id,y.id]))??[];return d.track("agent-rate",{event:C?"update":"create",thumb:m===1?"up":"down",knowledge_id:((j=c.knowledge)==null?void 0:j.id)??"",mode:n.chatMode,matches:$,score:m}),C?o.updateRating(c.id,n.chat.id,C,{knowledge_id:((f=c.knowledge)==null?void 0:f.id)??"",message_id:l,matches:$,score:m}):o.createRating(c.id,n.chat.id,{knowledge_id:((p=c.knowledge)==null?void 0:p.id)??"",message_id:l,matches:$,score:m})},deleteRate(l){var m;if(!n.chat)throw new Error("Chat is not initialized");return d.track("agent-rate-delete",{type:"text",chat_id:(m=n.chat)==null?void 0:m.id,id:l,mode:n.chatMode}),o.deleteRating(c.id,n.chat.id,l)},speak(l){var I;if(!n.streamingManager)throw new Error("Please connect to the agent first");function m(){if(typeof l=="string"){if(!c.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:c.presenter.voice,input:l,ssml:!1}}if(l.type==="text"&&!l.provider){if(!c.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:c.presenter.voice,input:l.input,ssml:l.ssml}}return l}const C=m();return d.track("agent-speak",C),H.update(),n.streamingManager.speak({script:C,metadata:{chat_id:(I=n.chat)==null?void 0:I.id,agent_id:c.id}})}}}function st(e,t,a){const{getById:r}=we(t,a||x);return r(e)}h.AgentStatus=ie,h.ChatCreationFailed=z,h.ChatMode=P,h.ChatModeDowngraded=te,h.ChatProgress=W,h.ConnectionState=S,h.DocumentType=de,h.KnowledgeType=ce,h.PlanGroup=ae,h.Providers=me,h.RateState=se,h.StreamEvents=V,h.StreamingState=R,h.Subject=oe,h.UserPlan=re,h.ValidationError=B,h.VideoType=Y,h.VoiceAccess=ue,h.WsError=ne,h.createAgentManager=it,h.getAgent=st,h.mapVideoType=le,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
1
+ (function(global, factory) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.index = {}));
3
+ })(this, function(exports2) {
4
+ "use strict";var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => {
7
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
+ return value;
9
+ };
10
+
11
+ class BaseError extends Error {
12
+ constructor({
13
+ kind,
14
+ description,
15
+ error
16
+ }) {
17
+ super(JSON.stringify({
18
+ kind,
19
+ description
20
+ }));
21
+ __publicField(this, "kind");
22
+ __publicField(this, "description");
23
+ __publicField(this, "error");
24
+ this.kind = kind;
25
+ this.description = description;
26
+ this.error = error;
27
+ }
28
+ }
29
+ class ChatCreationFailed extends BaseError {
30
+ constructor(mode, persistent) {
31
+ super({
32
+ kind: "ChatCreationFailed",
33
+ description: `Failed to create ${persistent ? "persistent" : ""} chat, mode: ${mode}`
34
+ });
35
+ }
36
+ }
37
+ class ChatModeDowngraded extends BaseError {
38
+ constructor(mode) {
39
+ super({
40
+ kind: "ChatModeDowngraded",
41
+ description: `Chat mode downgraded to ${mode}`
42
+ });
43
+ }
44
+ }
45
+ class ValidationError extends BaseError {
46
+ constructor(message, key) {
47
+ super({
48
+ kind: "ValidationError",
49
+ description: message
50
+ });
51
+ __publicField(this, "key");
52
+ this.key = key;
53
+ }
54
+ }
55
+ class WsError extends BaseError {
56
+ constructor(message) {
57
+ super({
58
+ kind: "WSError",
59
+ description: message
60
+ });
61
+ }
62
+ }
63
+ var UserPlan = /* @__PURE__ */ ((UserPlan2) => {
64
+ UserPlan2["TRIAL"] = "trial";
65
+ UserPlan2["BASIC"] = "basic";
66
+ UserPlan2["ENTERPRISE"] = "enterprise";
67
+ UserPlan2["LITE"] = "lite";
68
+ UserPlan2["ADVANCED"] = "advanced";
69
+ return UserPlan2;
70
+ })(UserPlan || {});
71
+ var PlanGroup = /* @__PURE__ */ ((PlanGroup2) => {
72
+ PlanGroup2["TRIAL"] = "deid-trial";
73
+ PlanGroup2["PRO"] = "deid-pro";
74
+ PlanGroup2["ENTERPRISE"] = "deid-enterprise";
75
+ PlanGroup2["LITE"] = "deid-lite";
76
+ PlanGroup2["ADVANCED"] = "deid-advanced";
77
+ PlanGroup2["BUILD"] = "deid-api-build";
78
+ PlanGroup2["LAUNCH"] = "deid-api-launch";
79
+ PlanGroup2["SCALE"] = "deid-api-scale";
80
+ return PlanGroup2;
81
+ })(PlanGroup || {});
82
+ var AgentStatus = /* @__PURE__ */ ((AgentStatus2) => {
83
+ AgentStatus2["Created"] = "created";
84
+ AgentStatus2["Started"] = "started";
85
+ AgentStatus2["Done"] = "done";
86
+ AgentStatus2["Error"] = "error";
87
+ AgentStatus2["Rejected"] = "rejected";
88
+ AgentStatus2["Ready"] = "ready";
89
+ return AgentStatus2;
90
+ })(AgentStatus || {});
91
+ var RateState = /* @__PURE__ */ ((RateState2) => {
92
+ RateState2["Unrated"] = "Unrated";
93
+ RateState2["Positive"] = "Positive";
94
+ RateState2["Negative"] = "Negative";
95
+ return RateState2;
96
+ })(RateState || {});
97
+ var ChatMode = /* @__PURE__ */ ((ChatMode2) => {
98
+ ChatMode2["Functional"] = "Functional";
99
+ ChatMode2["TextOnly"] = "TextOnly";
100
+ ChatMode2["Maintenance"] = "Maintenance";
101
+ ChatMode2["Playground"] = "Playground";
102
+ ChatMode2["DirectPlayback"] = "DirectPlayback";
103
+ return ChatMode2;
104
+ })(ChatMode || {});
105
+ var ChatProgress = /* @__PURE__ */ ((ChatProgress2) => {
106
+ ChatProgress2["Embed"] = "embed";
107
+ ChatProgress2["Query"] = "query";
108
+ ChatProgress2["Partial"] = "partial";
109
+ ChatProgress2["Answer"] = "answer";
110
+ ChatProgress2["Complete"] = "done";
111
+ return ChatProgress2;
112
+ })(ChatProgress || {});
113
+ var Subject = /* @__PURE__ */ ((Subject2) => {
114
+ Subject2["KnowledgeProcessing"] = "knowledge/processing";
115
+ Subject2["KnowledgeIndexing"] = "knowledge/indexing";
116
+ Subject2["KnowledgeFailed"] = "knowledge/error";
117
+ Subject2["KnowledgeDone"] = "knowledge/done";
118
+ return Subject2;
119
+ })(Subject || {});
120
+ var KnowledgeType = /* @__PURE__ */ ((KnowledgeType2) => {
121
+ KnowledgeType2["Knowledge"] = "knowledge";
122
+ KnowledgeType2["Document"] = "document";
123
+ KnowledgeType2["Record"] = "record";
124
+ return KnowledgeType2;
125
+ })(KnowledgeType || {});
126
+ var DocumentType = /* @__PURE__ */ ((DocumentType2) => {
127
+ DocumentType2["Pdf"] = "pdf";
128
+ DocumentType2["Text"] = "text";
129
+ DocumentType2["Html"] = "html";
130
+ DocumentType2["Word"] = "word";
131
+ DocumentType2["Json"] = "json";
132
+ DocumentType2["Markdown"] = "markdown";
133
+ DocumentType2["Csv"] = "csv";
134
+ DocumentType2["Excel"] = "excel";
135
+ DocumentType2["Powerpoint"] = "powerpoint";
136
+ DocumentType2["Archive"] = "archive";
137
+ DocumentType2["Image"] = "image";
138
+ DocumentType2["Audio"] = "audio";
139
+ DocumentType2["Video"] = "video";
140
+ return DocumentType2;
141
+ })(DocumentType || {});
142
+ var VideoType = /* @__PURE__ */ ((VideoType2) => {
143
+ VideoType2["Clip"] = "clip";
144
+ VideoType2["Talk"] = "talk";
145
+ return VideoType2;
146
+ })(VideoType || {});
147
+ const mapVideoType = (type) => {
148
+ switch (type) {
149
+ case "clip":
150
+ return "clip";
151
+ case "talk":
152
+ return "talk";
153
+ default:
154
+ throw new Error(`Unknown video type: ${type}`);
155
+ }
156
+ };
157
+ var StreamingState = /* @__PURE__ */ ((StreamingState2) => {
158
+ StreamingState2["Start"] = "START";
159
+ StreamingState2["Stop"] = "STOP";
160
+ return StreamingState2;
161
+ })(StreamingState || {});
162
+ var ConnectivityState = /* @__PURE__ */ ((ConnectivityState2) => {
163
+ ConnectivityState2["Strong"] = "STRONG";
164
+ ConnectivityState2["Weak"] = "WEAK";
165
+ ConnectivityState2["Unknown"] = "UNKNOWN";
166
+ return ConnectivityState2;
167
+ })(ConnectivityState || {});
168
+ var AgentActivityState = /* @__PURE__ */ ((AgentActivityState2) => {
169
+ AgentActivityState2["Idle"] = "IDLE";
170
+ AgentActivityState2["Talking"] = "TALKING";
171
+ return AgentActivityState2;
172
+ })(AgentActivityState || {});
173
+ const DataChannelSignalMap = {
174
+ "stream/started": "START",
175
+ "stream/done": "STOP"
176
+ /* Stop */
177
+ };
178
+ var StreamEvents = /* @__PURE__ */ ((StreamEvents2) => {
179
+ StreamEvents2["ChatAnswer"] = "chat/answer";
180
+ StreamEvents2["ChatPartial"] = "chat/partial";
181
+ StreamEvents2["StreamDone"] = "stream/done";
182
+ StreamEvents2["StreamStarted"] = "stream/started";
183
+ StreamEvents2["StreamFailed"] = "stream/error";
184
+ StreamEvents2["StreamReady"] = "stream/ready";
185
+ StreamEvents2["StreamCreated"] = "stream/created";
186
+ StreamEvents2["StreamVideoCreated"] = "stream-video/started";
187
+ StreamEvents2["StreamVideoDone"] = "stream-video/done";
188
+ StreamEvents2["StreamVideoError"] = "stream-video/error";
189
+ StreamEvents2["StreamVideoRejected"] = "stream-video/rejected";
190
+ return StreamEvents2;
191
+ })(StreamEvents || {});
192
+ var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
193
+ ConnectionState2["New"] = "new";
194
+ ConnectionState2["Fail"] = "fail";
195
+ ConnectionState2["Connected"] = "connected";
196
+ ConnectionState2["Connecting"] = "connecting";
197
+ ConnectionState2["Closed"] = "closed";
198
+ ConnectionState2["Completed"] = "completed";
199
+ ConnectionState2["Disconnected"] = "disconnected";
200
+ return ConnectionState2;
201
+ })(ConnectionState || {});
202
+ var StreamType = /* @__PURE__ */ ((StreamType2) => {
203
+ StreamType2["Legacy"] = "legacy";
204
+ StreamType2["Fluent"] = "fluent";
205
+ return StreamType2;
206
+ })(StreamType || {});
207
+ var Providers = /* @__PURE__ */ ((Providers2) => {
208
+ Providers2["Amazon"] = "amazon";
209
+ Providers2["Microsoft"] = "microsoft";
210
+ Providers2["Afflorithmics"] = "afflorithmics";
211
+ Providers2["Elevenlabs"] = "elevenlabs";
212
+ return Providers2;
213
+ })(Providers || {});
214
+ var VoiceAccess = /* @__PURE__ */ ((VoiceAccess2) => {
215
+ VoiceAccess2["Public"] = "public";
216
+ VoiceAccess2["Premium"] = "premium";
217
+ VoiceAccess2["Private"] = "private";
218
+ return VoiceAccess2;
219
+ })(VoiceAccess || {});
220
+ const CONNECTION_RETRY_TIMEOUT_MS = 45 * 1e3;
221
+ const PLAYGROUND_HEADER = "X-Playground-Chat";
222
+ const didApiUrl = "https://api-dev.d-id.com";
223
+ const didSocketApiUrl = "wss://notifications-dev.d-id.com";
224
+ const mixpanelKey = "79f81a83a67430be2bc0fd61042b8faa";
225
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
226
+ const getRandom = () => Math.random().toString(16).slice(2);
227
+ function createRacePromise(timeout, timeoutErrorMessage) {
228
+ let timeId;
229
+ const promise = new Promise((_, reject) => {
230
+ timeId = setTimeout(() => reject(new Error(timeoutErrorMessage)), timeout);
231
+ });
232
+ return {
233
+ promise,
234
+ clear: () => clearTimeout(timeId)
235
+ };
236
+ }
237
+ async function retryOperation(operation, userOptions) {
238
+ const options = {
239
+ limit: (userOptions == null ? void 0 : userOptions.limit) ?? 3,
240
+ delayMs: (userOptions == null ? void 0 : userOptions.delayMs) ?? 0,
241
+ timeout: (userOptions == null ? void 0 : userOptions.timeout) ?? 3e4,
242
+ timeoutErrorMessage: (userOptions == null ? void 0 : userOptions.timeoutErrorMessage) || "Timeout error",
243
+ shouldRetryFn: (userOptions == null ? void 0 : userOptions.shouldRetryFn) ?? (() => true),
244
+ onRetry: (userOptions == null ? void 0 : userOptions.onRetry) ?? (() => {
245
+ })
246
+ };
247
+ let lastError;
248
+ for (let attempt = 1; attempt <= options.limit; attempt++) {
249
+ try {
250
+ if (!options.timeout) {
251
+ return await operation();
252
+ }
253
+ const {
254
+ promise,
255
+ clear
256
+ } = createRacePromise(options.timeout, options.timeoutErrorMessage);
257
+ const operationPromise = operation().finally(clear);
258
+ return await Promise.race([operationPromise, promise]);
259
+ } catch (error) {
260
+ lastError = error;
261
+ if (!options.shouldRetryFn(error) || attempt >= options.limit) {
262
+ throw error;
263
+ }
264
+ await sleep(options.delayMs);
265
+ options.onRetry(error);
266
+ }
267
+ }
268
+ throw lastError;
269
+ }
270
+ function getExternalId() {
271
+ let key = window.localStorage.getItem("did_external_key_id");
272
+ if (!key) {
273
+ key = Math.random().toString(16).slice(2);
274
+ window.localStorage.setItem("did_external_key_id", key);
275
+ }
276
+ return key;
277
+ }
278
+ let sessionKey = getRandom();
279
+ function getAuthHeader(auth) {
280
+ if (auth.type === "bearer") {
281
+ return `Bearer ${auth.token}`;
282
+ } else if (auth.type === "basic") {
283
+ return `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
284
+ } else if (auth.type === "key") {
285
+ return `Client-Key ${auth.clientKey}.${getExternalId()}_${sessionKey}`;
286
+ } else {
287
+ throw new Error(`Unknown auth type: ${auth}`);
288
+ }
289
+ }
290
+ const retryHttpTooManyRequests = (operation) => retryOperation(operation, {
291
+ limit: 3,
292
+ delayMs: 1e3,
293
+ timeout: 0,
294
+ shouldRetryFn: (error) => error.status === 429
295
+ });
296
+ function createClient(auth, host = didApiUrl, onError) {
297
+ const client = async (url, options) => {
298
+ const {
299
+ skipErrorHandler,
300
+ ...fetchOptions
301
+ } = options || {};
302
+ const request = await retryHttpTooManyRequests(() => fetch(host + ((url == null ? void 0 : url.startsWith("/")) ? url : `/${url}`), {
303
+ ...fetchOptions,
304
+ headers: {
305
+ ...fetchOptions.headers,
306
+ Authorization: getAuthHeader(auth),
307
+ "Content-Type": "application/json"
308
+ }
309
+ }));
310
+ if (!request.ok) {
311
+ let errorText = await request.text().catch(() => `Failed to fetch with status ${request.status}`);
312
+ const error = new Error(errorText);
313
+ if (onError && !skipErrorHandler) {
314
+ onError(error, {
315
+ url,
316
+ options: fetchOptions,
317
+ headers: request.headers
318
+ });
319
+ }
320
+ throw error;
321
+ }
322
+ return request.json();
323
+ };
324
+ return {
325
+ get(url, options) {
326
+ return client(url, {
327
+ ...options,
328
+ method: "GET"
329
+ });
330
+ },
331
+ post(url, body, options) {
332
+ return client(url, {
333
+ ...options,
334
+ body: JSON.stringify(body),
335
+ method: "POST"
336
+ });
337
+ },
338
+ delete(url, body, options) {
339
+ return client(url, {
340
+ ...options,
341
+ body: JSON.stringify(body),
342
+ method: "DELETE"
343
+ });
344
+ },
345
+ patch(url, body, options) {
346
+ return client(url, {
347
+ ...options,
348
+ body: JSON.stringify(body),
349
+ method: "PATCH"
350
+ });
351
+ }
352
+ };
353
+ }
354
+ function createAgentsApi(auth, host = didApiUrl, onError) {
355
+ const client = createClient(auth, `${host}/agents`, onError);
356
+ return {
357
+ create(payload, options) {
358
+ return client.post(`/`, payload, options);
359
+ },
360
+ getAgents(tag, options) {
361
+ return client.get(`/${tag ? `?tag=${tag}` : ""}`, options).then((agents) => agents ?? []);
362
+ },
363
+ getById(id, options) {
364
+ return client.get(`/${id}`, options);
365
+ },
366
+ delete(id, options) {
367
+ return client.delete(`/${id}`, void 0, options);
368
+ },
369
+ update(id, payload, options) {
370
+ return client.patch(`/${id}`, payload, options);
371
+ },
372
+ newChat(agentId, payload, options) {
373
+ return client.post(`/${agentId}/chat`, payload, options);
374
+ },
375
+ chat(agentId, chatId, payload, options) {
376
+ return client.post(`/${agentId}/chat/${chatId}`, payload, options);
377
+ },
378
+ createRating(agentId, chatId, payload, options) {
379
+ return client.post(`/${agentId}/chat/${chatId}/ratings`, payload, options);
380
+ },
381
+ updateRating(agentId, chatId, ratingId, payload, options) {
382
+ return client.patch(`/${agentId}/chat/${chatId}/ratings/${ratingId}`, payload, options);
383
+ },
384
+ deleteRating(agentId, chatId, ratingId, options) {
385
+ return client.delete(`/${agentId}/chat/${chatId}/ratings/${ratingId}`, options);
386
+ },
387
+ getSTTToken(agentId, options) {
388
+ return client.get(`/${agentId}/stt-token`, options);
389
+ }
390
+ };
391
+ }
392
+ const getAgentType = (presenter) => presenter.type === "clip" && presenter.presenter_id.startsWith("v2_") ? "clip_v2" : presenter.type;
393
+ function getAnalyticsInfo(agent) {
394
+ var _a, _b, _c, _d;
395
+ const mobileOrDesktop = () => {
396
+ return /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
397
+ };
398
+ const getUserOS = () => {
399
+ const platform = navigator.platform;
400
+ if (platform.toLowerCase().includes("win")) {
401
+ return "Windows";
402
+ } else if (platform.toLowerCase().includes("mac")) {
403
+ return "Mac OS X";
404
+ } else if (platform.toLowerCase().includes("linux")) {
405
+ return "Linux";
406
+ } else {
407
+ return "Unknown";
408
+ }
409
+ };
410
+ const presenter = agent.presenter;
411
+ return {
412
+ $os: `${getUserOS()}`,
413
+ isMobile: `${mobileOrDesktop() == "Mobile"}`,
414
+ browser: navigator.userAgent,
415
+ origin: window.location.origin,
416
+ agentType: getAgentType(presenter),
417
+ agentVoice: {
418
+ voiceId: (_b = (_a = agent.presenter) == null ? void 0 : _a.voice) == null ? void 0 : _b.voice_id,
419
+ provider: (_d = (_c = agent.presenter) == null ? void 0 : _c.voice) == null ? void 0 : _d.type
420
+ }
421
+ };
422
+ }
423
+ const sumFunc = (numbers) => numbers.reduce((total, aNumber) => total + aNumber, 0);
424
+ const average = (numbers) => sumFunc(numbers) / numbers.length;
425
+ function getStreamAnalyticsProps(data, agent, additionalProps) {
426
+ var _a, _b, _c;
427
+ const {
428
+ event,
429
+ ...baseProps
430
+ } = data;
431
+ const {
432
+ template
433
+ } = (agent == null ? void 0 : agent.llm) || {};
434
+ const {
435
+ language
436
+ } = ((_a = agent == null ? void 0 : agent.presenter) == null ? void 0 : _a.voice) || {};
437
+ const props = {
438
+ ...baseProps,
439
+ llm: {
440
+ ...baseProps.llm,
441
+ template
442
+ },
443
+ script: {
444
+ ...baseProps.script,
445
+ provider: {
446
+ ...(_b = baseProps == null ? void 0 : baseProps.script) == null ? void 0 : _b.provider,
447
+ language
448
+ }
449
+ },
450
+ stitch: (agent == null ? void 0 : agent.presenter.type) === "talk" ? (_c = agent == null ? void 0 : agent.presenter) == null ? void 0 : _c.stitch : void 0,
451
+ ...additionalProps
452
+ };
453
+ return props;
454
+ }
455
+ let mixpanelEvents = {};
456
+ const mixpanelUrl = "https://api-js.mixpanel.com/track/?verbose=1&ip=1";
457
+ function initializeAnalytics(config) {
458
+ var _a, _b, _c, _d, _e, _f;
459
+ const source = (window == null ? void 0 : window.hasOwnProperty("DID_AGENTS_API")) ? "agents-ui" : "agents-sdk";
460
+ const presenter = config.agent.presenter;
461
+ const promptCustomization = (_a = config.agent.llm) == null ? void 0 : _a.prompt_customization;
462
+ const analyticProps = {
463
+ token: config.token || "testKey",
464
+ distinct_id: config.distinctId || getExternalId(),
465
+ agentId: config.agent.id,
466
+ agentType: getAgentType(presenter),
467
+ owner_id: config.agent.owner_id ?? "",
468
+ promptVersion: (_b = config.agent.llm) == null ? void 0 : _b.prompt_version,
469
+ behavior: {
470
+ role: promptCustomization == null ? void 0 : promptCustomization.role,
471
+ personality: promptCustomization == null ? void 0 : promptCustomization.personality,
472
+ instructions: (_c = config.agent.llm) == null ? void 0 : _c.instructions
473
+ },
474
+ temperature: (_d = config.agent.llm) == null ? void 0 : _d.temperature,
475
+ knowledgeSource: promptCustomization == null ? void 0 : promptCustomization.knowledge_source,
476
+ starterQuestionsCount: (_f = (_e = config.agent.knowledge) == null ? void 0 : _e.starter_message) == null ? void 0 : _f.length,
477
+ topicsToAvoid: promptCustomization == null ? void 0 : promptCustomization.topics_to_avoid,
478
+ maxResponseLength: promptCustomization == null ? void 0 : promptCustomization.max_response_length
479
+ };
480
+ return {
481
+ ...analyticProps,
482
+ additionalProperties: {},
483
+ isEnabled: config.isEnabled ?? true,
484
+ getRandom: () => Math.random().toString(16).slice(2),
485
+ enrich(properties) {
486
+ const props = {};
487
+ if (properties && typeof properties !== "object") {
488
+ throw new Error("properties must be a flat json object");
489
+ }
490
+ for (let prop in properties) {
491
+ if (typeof properties[prop] === "string" || typeof properties[prop] === "number") {
492
+ props[prop] = properties[prop];
493
+ }
494
+ }
495
+ this.additionalProperties = {
496
+ ...this.additionalProperties,
497
+ ...props
498
+ };
499
+ },
500
+ async track(event, props) {
501
+ if (!this.isEnabled) {
502
+ return Promise.resolve();
503
+ }
504
+ const {
505
+ audioPath,
506
+ ...sendProps
507
+ } = props || {};
508
+ const options = {
509
+ method: "POST",
510
+ headers: {
511
+ "Content-Type": "application/x-www-form-urlencoded"
512
+ },
513
+ body: new URLSearchParams({
514
+ data: JSON.stringify([{
515
+ event,
516
+ properties: {
517
+ ...this.additionalProperties,
518
+ ...sendProps,
519
+ ...analyticProps,
520
+ source,
521
+ time: Date.now(),
522
+ $insert_id: this.getRandom(),
523
+ origin: window.location.href,
524
+ "Screen Height": window.screen.height || window.innerWidth,
525
+ "Screen Width": window.screen.width || window.innerHeight,
526
+ "User Agent": navigator.userAgent
527
+ }
528
+ }])
529
+ })
530
+ };
531
+ try {
532
+ return await fetch(mixpanelUrl, options).then((res) => res.json());
533
+ } catch (err) {
534
+ return console.error(err);
535
+ }
536
+ },
537
+ linkTrack(mixpanelEvent, props, event, dependencies) {
538
+ if (!mixpanelEvents[mixpanelEvent]) {
539
+ mixpanelEvents[mixpanelEvent] = {
540
+ events: {},
541
+ resolvedDependencies: []
542
+ };
543
+ }
544
+ if (!dependencies.includes(event)) {
545
+ dependencies.push(event);
546
+ }
547
+ const linkedEvent = mixpanelEvents[mixpanelEvent];
548
+ linkedEvent.events[event] = {
549
+ props
550
+ };
551
+ linkedEvent.resolvedDependencies.push(event);
552
+ const allDependenciesResolved = dependencies.every((value) => linkedEvent.resolvedDependencies.includes(value));
553
+ if (allDependenciesResolved) {
554
+ const aggregatedProps = dependencies.reduce((acc, curr) => {
555
+ if (linkedEvent.events[curr]) {
556
+ return {
557
+ ...acc,
558
+ ...linkedEvent.events[curr].props
559
+ };
560
+ }
561
+ return acc;
562
+ }, {});
563
+ this.track(mixpanelEvent, aggregatedProps);
564
+ linkedEvent.resolvedDependencies = linkedEvent.resolvedDependencies.filter((event2) => !dependencies.includes(event2));
565
+ dependencies.forEach((event2) => {
566
+ delete linkedEvent.events[event2];
567
+ });
568
+ }
569
+ }
570
+ };
571
+ }
572
+ function createTimestampTracker() {
573
+ let timestamp = 0;
574
+ return {
575
+ reset: () => timestamp = 0,
576
+ update: () => timestamp = Date.now(),
577
+ get: (delta = false) => delta ? Date.now() - timestamp : timestamp
578
+ };
579
+ }
580
+ const timestampTracker = createTimestampTracker();
581
+ function getRequestHeaders(chatMode) {
582
+ return chatMode === ChatMode.Playground ? {
583
+ headers: {
584
+ [PLAYGROUND_HEADER]: "true"
585
+ }
586
+ } : {};
587
+ }
588
+ async function createChat(agent, agentsApi, analytics, chatMode, persist = false, chat) {
589
+ try {
590
+ if (!chat && chatMode !== ChatMode.DirectPlayback) {
591
+ chat = await agentsApi.newChat(agent.id, {
592
+ persist
593
+ }, getRequestHeaders(chatMode));
594
+ analytics.track("agent-chat", {
595
+ event: "created",
596
+ chat_id: chat.id,
597
+ agent_id: agent.id,
598
+ mode: chatMode
599
+ });
600
+ }
601
+ return {
602
+ chat,
603
+ chatMode: (chat == null ? void 0 : chat.chat_mode) ?? chatMode
604
+ };
605
+ } catch (error) {
606
+ try {
607
+ const parsedError = JSON.parse(error.message);
608
+ if ((parsedError == null ? void 0 : parsedError.kind) === "InsufficientCreditsError") {
609
+ throw new Error("InsufficientCreditsError");
610
+ }
611
+ } catch (e) {
612
+ console.error("Error parsing the error message:", e);
613
+ }
614
+ throw new Error("Cannot create new chat");
615
+ }
616
+ }
617
+ function getGreetings(agent) {
618
+ var _a;
619
+ const greetings = ((_a = agent.greetings) == null ? void 0 : _a.filter((greeting) => greeting.length > 0)) ?? [];
620
+ if (greetings.length > 0) {
621
+ return greetings[Math.floor(Math.random() * greetings.length)];
622
+ }
623
+ return `Hi! I'm ${agent.preview_name || "My Agent"}. How can I help you?`;
624
+ }
625
+ function getInitialMessages(content, initialMessages) {
626
+ if (initialMessages && initialMessages.length > 0) {
627
+ return initialMessages;
628
+ }
629
+ return [{
630
+ content,
631
+ id: getRandom(),
632
+ role: "assistant",
633
+ created_at: (/* @__PURE__ */ new Date()).toISOString()
634
+ }];
635
+ }
636
+ function connect(options) {
637
+ return new Promise((resolve, reject) => {
638
+ const {
639
+ callbacks,
640
+ host,
641
+ auth
642
+ } = options;
643
+ const {
644
+ onMessage = null,
645
+ onOpen = null,
646
+ onClose = null,
647
+ onError = null
648
+ } = callbacks || {};
649
+ const socket = new WebSocket(`${host}?authorization=${getAuthHeader(auth)}`);
650
+ socket.onmessage = onMessage;
651
+ socket.onclose = onClose;
652
+ socket.onerror = (e) => {
653
+ console.error(e);
654
+ onError == null ? void 0 : onError("Websocket failed to connect", e);
655
+ reject(e);
656
+ };
657
+ socket.onopen = (e) => {
658
+ onOpen == null ? void 0 : onOpen(e);
659
+ resolve(socket);
660
+ };
661
+ });
662
+ }
663
+ async function connectWithRetries(options) {
664
+ const {
665
+ retries = 1
666
+ } = options;
667
+ let socket = null;
668
+ for (let attempt = 0; (socket == null ? void 0 : socket.readyState) !== WebSocket.OPEN; attempt++) {
669
+ try {
670
+ socket = await connect(options);
671
+ } catch (e) {
672
+ if (attempt === retries) {
673
+ throw e;
674
+ }
675
+ await sleep(attempt * 500);
676
+ }
677
+ }
678
+ return socket;
679
+ }
680
+ async function createSocketManager(auth, host, callbacks) {
681
+ const messageCallbacks = (callbacks == null ? void 0 : callbacks.onMessage) ? [callbacks.onMessage] : [];
682
+ const socket = await connectWithRetries({
683
+ auth,
684
+ host,
685
+ callbacks: {
686
+ onError: (error) => {
687
+ var _a;
688
+ return (_a = callbacks.onError) == null ? void 0 : _a.call(callbacks, new WsError(error));
689
+ },
690
+ onMessage(event) {
691
+ const parsedData = JSON.parse(event.data);
692
+ messageCallbacks.forEach((callback) => callback(parsedData.event, parsedData));
693
+ }
694
+ }
695
+ });
696
+ return {
697
+ socket,
698
+ disconnect: () => socket.close(),
699
+ subscribeToEvents: (callback) => messageCallbacks.push(callback)
700
+ };
701
+ }
702
+ function getMessageContent(chatEventQueue) {
703
+ if (chatEventQueue["answer"] !== void 0) {
704
+ return chatEventQueue["answer"];
705
+ }
706
+ let currentSequence = 0;
707
+ let content = "";
708
+ while (currentSequence in chatEventQueue) {
709
+ content += chatEventQueue[currentSequence++];
710
+ }
711
+ return content;
712
+ }
713
+ function processChatEvent(event, data, chatEventQueue, items, onNewMessage) {
714
+ const lastMessage = items.messages[items.messages.length - 1];
715
+ if (!(event === ChatProgress.Partial || event === ChatProgress.Answer) || (lastMessage == null ? void 0 : lastMessage.role) !== "assistant") {
716
+ return;
717
+ }
718
+ const {
719
+ content,
720
+ sequence
721
+ } = data;
722
+ if (event === ChatProgress.Partial) {
723
+ chatEventQueue[sequence] = content;
724
+ } else {
725
+ chatEventQueue["answer"] = content;
726
+ }
727
+ const messageContent = getMessageContent(chatEventQueue);
728
+ if (lastMessage.content !== messageContent || event === ChatProgress.Answer) {
729
+ lastMessage.content = messageContent;
730
+ onNewMessage == null ? void 0 : onNewMessage([...items.messages], event);
731
+ }
732
+ }
733
+ function createMessageEventQueue(analytics, items, options, agentEntity, onStreamDone) {
734
+ let chatEventQueue = {};
735
+ return {
736
+ clearQueue: () => chatEventQueue = {},
737
+ onMessage: (event, data) => {
738
+ var _a, _b;
739
+ if ("content" in data) {
740
+ processChatEvent(event, data, chatEventQueue, items, options.callbacks.onNewMessage);
741
+ if (event === ChatProgress.Answer) {
742
+ analytics.track("agent-message-received", {
743
+ messages: items.messages.length,
744
+ mode: items.chatMode
745
+ });
746
+ }
747
+ } else {
748
+ const SEvent = StreamEvents;
749
+ const completedEvents = [SEvent.StreamVideoDone, SEvent.StreamVideoError, SEvent.StreamVideoRejected];
750
+ const failedEvents = [SEvent.StreamFailed, SEvent.StreamVideoError, SEvent.StreamVideoRejected];
751
+ const props = getStreamAnalyticsProps(data, agentEntity, {
752
+ mode: items.chatMode
753
+ });
754
+ event = event;
755
+ if (event === SEvent.StreamVideoCreated) {
756
+ analytics.linkTrack("agent-video", props, SEvent.StreamVideoCreated, ["start"]);
757
+ } else if (completedEvents.includes(event)) {
758
+ const streamEvent = event.split("/")[1];
759
+ if (failedEvents.includes(event)) {
760
+ analytics.track("agent-video", {
761
+ ...props,
762
+ event: streamEvent
763
+ });
764
+ } else {
765
+ analytics.linkTrack("agent-video", {
766
+ ...props,
767
+ event: streamEvent
768
+ }, event, ["done"]);
769
+ }
770
+ }
771
+ if (failedEvents.includes(event)) {
772
+ (_b = (_a = options.callbacks).onError) == null ? void 0 : _b.call(_a, new Error(`Stream failed with event ${event}`), {
773
+ data
774
+ });
775
+ }
776
+ if (data.event === SEvent.StreamDone) {
777
+ onStreamDone();
778
+ }
779
+ }
780
+ }
781
+ };
782
+ }
783
+ function createClipApi(auth, host, agentId, onError) {
784
+ const client = createClient(auth, `${host}/agents/${agentId}`, onError);
785
+ return {
786
+ createStream(options) {
787
+ return client.post("/streams", {
788
+ output_resolution: options.output_resolution,
789
+ compatibility_mode: options.compatibility_mode,
790
+ stream_warmup: options.stream_warmup,
791
+ session_timeout: options.session_timeout,
792
+ stream_greeting: options.stream_greeting
793
+ });
794
+ },
795
+ startConnection(streamId, answer, sessionId) {
796
+ return client.post(`/streams/${streamId}/sdp`, {
797
+ session_id: sessionId,
798
+ answer
799
+ });
800
+ },
801
+ addIceCandidate(streamId, candidate, sessionId) {
802
+ return client.post(`/streams/${streamId}/ice`, {
803
+ session_id: sessionId,
804
+ ...candidate
805
+ });
806
+ },
807
+ sendStreamRequest(streamId, sessionId, payload) {
808
+ return client.post(`/streams/${streamId}`, {
809
+ session_id: sessionId,
810
+ ...payload
811
+ });
812
+ },
813
+ close(streamId, sessionId) {
814
+ return client.delete(`/streams/${streamId}`, {
815
+ session_id: sessionId
816
+ });
817
+ }
818
+ };
819
+ }
820
+ function createTalkApi(auth, host, agentId, onError) {
821
+ const client = createClient(auth, `${host}/agents/${agentId}`, onError);
822
+ return {
823
+ createStream(streamOptions, options) {
824
+ return client.post("/streams", {
825
+ driver_url: streamOptions.driver_url,
826
+ face: streamOptions.face,
827
+ config: streamOptions.config,
828
+ output_resolution: streamOptions.output_resolution,
829
+ compatibility_mode: streamOptions.compatibility_mode,
830
+ stream_warmup: streamOptions.stream_warmup,
831
+ session_timeout: streamOptions.session_timeout,
832
+ stream_greeting: streamOptions.stream_greeting
833
+ }, options);
834
+ },
835
+ startConnection(streamId, answer, sessionId, options) {
836
+ return client.post(`/streams/${streamId}/sdp`, {
837
+ session_id: sessionId,
838
+ answer
839
+ }, options);
840
+ },
841
+ addIceCandidate(streamId, candidate, sessionId, options) {
842
+ return client.post(`/streams/${streamId}/ice`, {
843
+ session_id: sessionId,
844
+ ...candidate
845
+ }, options);
846
+ },
847
+ sendStreamRequest(streamId, sessionId, payload, options) {
848
+ return client.post(`/streams/${streamId}`, {
849
+ session_id: sessionId,
850
+ ...payload
851
+ }, options);
852
+ },
853
+ close(streamId, sessionId, options) {
854
+ return client.delete(`/streams/${streamId}`, {
855
+ session_id: sessionId
856
+ }, options);
857
+ }
858
+ };
859
+ }
860
+ function createAggregateReport(start, end, lowFpsCount) {
861
+ const duration = (end.timestamp - start.timestamp) / 1e3;
862
+ return {
863
+ duration,
864
+ bytesReceived: end.bytesReceived - start.bytesReceived,
865
+ bitrate: Math.round((end.bytesReceived - start.bytesReceived) * 8 / duration),
866
+ packetsReceived: end.packetsReceived - start.packetsReceived,
867
+ packetsLost: end.packetsLost - start.packetsLost,
868
+ framesDropped: end.framesDropped - start.framesDropped,
869
+ framesDecoded: end.framesDecoded - start.framesDecoded,
870
+ jitter: end.jitter,
871
+ avgJitterDelayInInterval: (end.jitterBufferDelay - start.jitterBufferDelay) / (end.jitterBufferEmittedCount - start.jitterBufferEmittedCount),
872
+ jitterBufferEmittedCount: end.jitterBufferEmittedCount - start.jitterBufferEmittedCount,
873
+ jitterBufferDelay: (end.jitterBufferDelay - start.jitterBufferDelay) / duration,
874
+ framesPerSecond: end.framesPerSecond,
875
+ freezeCount: end.freezeCount - start.freezeCount,
876
+ freezeDuration: end.freezeDuration - start.freezeDuration,
877
+ lowFpsCount
878
+ };
879
+ }
880
+ function extractAnomalies(stats) {
881
+ return stats.filter((report) => report.freezeCount > 0 || report.framesPerSecond < 21 || report.framesDropped > 0 || report.packetsLost > 0).map((report) => {
882
+ const {
883
+ timestamp,
884
+ ...updatedReport
885
+ } = report;
886
+ const causes = [];
887
+ if (report.freezeCount > 0) {
888
+ causes.push("freeze");
889
+ }
890
+ if (report.framesPerSecond < 21) {
891
+ causes.push("low fps");
892
+ }
893
+ if (report.framesDropped > 0) {
894
+ causes.push("frames dropped");
895
+ }
896
+ if (report.packetsLost > 0) {
897
+ causes.push("packet loss");
898
+ }
899
+ return {
900
+ ...updatedReport,
901
+ causes
902
+ };
903
+ });
904
+ }
905
+ function formatStats(stats) {
906
+ let codec = "";
907
+ for (const report of stats.values()) {
908
+ if (report && report.type === "codec" && report.mimeType.startsWith("video")) {
909
+ codec = report.mimeType.split("/")[1];
910
+ }
911
+ if (report && report.type === "inbound-rtp" && report.kind === "video") {
912
+ return {
913
+ codec,
914
+ timestamp: report.timestamp,
915
+ bytesReceived: report.bytesReceived,
916
+ packetsReceived: report.packetsReceived,
917
+ packetsLost: report.packetsLost,
918
+ framesDropped: report.framesDropped,
919
+ framesDecoded: report.framesDecoded,
920
+ jitter: report.jitter,
921
+ jitterBufferDelay: report.jitterBufferDelay,
922
+ jitterBufferEmittedCount: report.jitterBufferEmittedCount,
923
+ avgJitterDelayInInterval: report.jitterBufferDelay / report.jitterBufferEmittedCount,
924
+ frameWidth: report.frameWidth,
925
+ frameHeight: report.frameHeight,
926
+ framesPerSecond: report.framesPerSecond,
927
+ freezeCount: report.freezeCount,
928
+ freezeDuration: report.totalFreezesDuration
929
+ };
930
+ }
931
+ }
932
+ return {};
933
+ }
934
+ function createVideoStatsReport(stats, interval2, previousStats) {
935
+ const differentialReport = stats.map((report, index) => {
936
+ if (index === 0) {
937
+ if (!previousStats) {
938
+ return {
939
+ timestamp: report.timestamp,
940
+ duration: 0,
941
+ bytesReceived: report.bytesReceived,
942
+ bitrate: report.bytesReceived * 8 / (interval2 / 1e3),
943
+ packetsReceived: report.packetsReceived,
944
+ packetsLost: report.packetsLost,
945
+ framesDropped: report.framesDropped,
946
+ framesDecoded: report.framesDecoded,
947
+ jitter: report.jitter,
948
+ jitterBufferDelay: report.jitterBufferDelay,
949
+ jitterBufferEmittedCount: report.jitterBufferEmittedCount,
950
+ avgJitterDelayInInterval: report.jitterBufferDelay / report.jitterBufferEmittedCount,
951
+ framesPerSecond: report.framesPerSecond,
952
+ freezeCount: report.freezeCount,
953
+ freezeDuration: report.freezeDuration
954
+ };
955
+ }
956
+ return {
957
+ timestamp: report.timestamp,
958
+ duration: 0,
959
+ bytesReceived: report.bytesReceived - previousStats.bytesReceived,
960
+ bitrate: (report.bytesReceived - previousStats.bytesReceived) * 8 / (interval2 / 1e3),
961
+ packetsReceived: report.packetsReceived - previousStats.packetsReceived,
962
+ packetsLost: report.packetsLost - previousStats.packetsLost,
963
+ framesDropped: report.framesDropped - previousStats.framesDropped,
964
+ framesDecoded: report.framesDecoded - previousStats.framesDecoded,
965
+ jitter: report.jitter,
966
+ jitterBufferDelay: report.jitterBufferDelay - previousStats.jitterBufferDelay,
967
+ jitterBufferEmittedCount: report.jitterBufferEmittedCount - previousStats.jitterBufferEmittedCount,
968
+ avgJitterDelayInInterval: (report.jitterBufferDelay - previousStats.jitterBufferDelay) / (report.jitterBufferEmittedCount - previousStats.jitterBufferEmittedCount),
969
+ framesPerSecond: report.framesPerSecond,
970
+ freezeCount: report.freezeCount - previousStats.freezeCount,
971
+ freezeDuration: report.freezeDuration - previousStats.freezeDuration
972
+ };
973
+ }
974
+ return {
975
+ timestamp: report.timestamp,
976
+ duration: interval2 * index / 1e3,
977
+ bytesReceived: report.bytesReceived - stats[index - 1].bytesReceived,
978
+ bitrate: (report.bytesReceived - stats[index - 1].bytesReceived) * 8 / (interval2 / 1e3),
979
+ packetsReceived: report.packetsReceived - stats[index - 1].packetsReceived,
980
+ packetsLost: report.packetsLost - stats[index - 1].packetsLost,
981
+ framesDropped: report.framesDropped - stats[index - 1].framesDropped,
982
+ framesDecoded: report.framesDecoded - stats[index - 1].framesDecoded,
983
+ jitter: report.jitter,
984
+ jitterBufferDelay: report.jitterBufferDelay - stats[index - 1].jitterBufferDelay,
985
+ jitterBufferEmittedCount: report.jitterBufferEmittedCount - stats[index - 1].jitterBufferEmittedCount,
986
+ avgJitterDelayInInterval: (report.jitterBufferDelay - stats[index - 1].jitterBufferDelay) / (report.jitterBufferEmittedCount - stats[index - 1].jitterBufferEmittedCount),
987
+ framesPerSecond: report.framesPerSecond,
988
+ freezeCount: report.freezeCount - stats[index - 1].freezeCount,
989
+ freezeDuration: report.freezeDuration - stats[index - 1].freezeDuration
990
+ };
991
+ });
992
+ const anomalies = extractAnomalies(differentialReport);
993
+ const lowFpsCount = anomalies.reduce((acc, report) => acc + (report.causes.includes("low fps") ? 1 : 0), 0);
994
+ const avgJittersSamples = differentialReport.map((stat) => stat.avgJitterDelayInInterval);
995
+ return {
996
+ webRTCStats: {
997
+ anomalies,
998
+ aggregateReport: createAggregateReport(stats[0], stats[stats.length - 1], lowFpsCount),
999
+ minJitterDelayInInterval: Math.min(...avgJittersSamples),
1000
+ maxJitterDelayInInterval: Math.max(...avgJittersSamples),
1001
+ avgJitterDelayInInterval: average(avgJittersSamples)
1002
+ },
1003
+ codec: stats[0].codec,
1004
+ resolution: `${stats[0].frameWidth}x${stats[0].frameHeight}`
1005
+ };
1006
+ }
1007
+ const interval = 100;
1008
+ const notReceivingIntervalsThreshold = Math.max(Math.ceil(400 / interval), 1);
1009
+ const LOW_JITTER_TRESHOLD = 0.25;
1010
+ const HIGH_JITTER_TRESHOLD = 0.28;
1011
+ function createVideoStatsAnalyzer() {
1012
+ let lastFramesReceived = 0;
1013
+ let prevDelay;
1014
+ let prevCount;
1015
+ let avgJitterDelayInInterval = 0;
1016
+ return (stats) => {
1017
+ for (const report of stats.values()) {
1018
+ if (report && report.type === "inbound-rtp" && report.kind === "video") {
1019
+ const delay = report.jitterBufferDelay;
1020
+ const count = report.jitterBufferEmittedCount;
1021
+ if (prevCount && count > prevCount) {
1022
+ const deltaDelay = delay - prevDelay;
1023
+ const deltaCount = count - prevCount;
1024
+ avgJitterDelayInInterval = deltaDelay / deltaCount;
1025
+ }
1026
+ prevDelay = delay;
1027
+ prevCount = count;
1028
+ const currFramesReceived = report.framesDecoded;
1029
+ const isReceiving = currFramesReceived - lastFramesReceived > 0;
1030
+ lastFramesReceived = currFramesReceived;
1031
+ return {
1032
+ isReceiving,
1033
+ avgJitterDelayInInterval,
1034
+ freezeCount: report.freezeCount
1035
+ };
1036
+ }
1037
+ }
1038
+ return {
1039
+ isReceiving: false,
1040
+ avgJitterDelayInInterval
1041
+ };
1042
+ };
1043
+ }
1044
+ function pollStats(peerConnection, getIsConnected, onConnected, onVideoStateChange, onConnectivityStateChange, warmup = false, shouldWaitForGreeting = false) {
1045
+ const streamsBeforeReady = warmup ? 1 : 0;
1046
+ let allStats = [];
1047
+ let previousStats;
1048
+ let notReceivingNumIntervals = 0;
1049
+ let isStreaming = false;
1050
+ let streamsCount = 0;
1051
+ let prevLowConnState = ConnectivityState.Unknown;
1052
+ let currLowConnState = ConnectivityState.Unknown;
1053
+ let currFreezeCount = 0;
1054
+ let prevFreezeCount = 0;
1055
+ const isReceivingVideoBytes = createVideoStatsAnalyzer();
1056
+ return setInterval(async () => {
1057
+ const stats = await peerConnection.getStats();
1058
+ const {
1059
+ isReceiving,
1060
+ avgJitterDelayInInterval,
1061
+ freezeCount
1062
+ } = isReceivingVideoBytes(stats);
1063
+ const slimStats = formatStats(stats);
1064
+ if (isReceiving) {
1065
+ notReceivingNumIntervals = 0;
1066
+ currFreezeCount = freezeCount - prevFreezeCount;
1067
+ currLowConnState = avgJitterDelayInInterval < LOW_JITTER_TRESHOLD ? ConnectivityState.Strong : avgJitterDelayInInterval > HIGH_JITTER_TRESHOLD && currFreezeCount > 1 ? ConnectivityState.Weak : prevLowConnState;
1068
+ if (currLowConnState !== prevLowConnState) {
1069
+ onConnectivityStateChange == null ? void 0 : onConnectivityStateChange(currLowConnState);
1070
+ prevLowConnState = currLowConnState;
1071
+ prevFreezeCount += currFreezeCount;
1072
+ currFreezeCount = 0;
1073
+ }
1074
+ if (!isStreaming) {
1075
+ onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Start);
1076
+ if (shouldWaitForGreeting && streamsCount >= streamsBeforeReady && !getIsConnected()) {
1077
+ onConnected();
1078
+ }
1079
+ previousStats = allStats[allStats.length - 1];
1080
+ allStats = [];
1081
+ streamsCount++;
1082
+ isStreaming = true;
1083
+ }
1084
+ allStats.push(slimStats);
1085
+ } else if (isStreaming) {
1086
+ notReceivingNumIntervals++;
1087
+ if (notReceivingNumIntervals >= notReceivingIntervalsThreshold) {
1088
+ const statsReport = createVideoStatsReport(allStats, interval, previousStats);
1089
+ onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Stop, statsReport);
1090
+ if (!shouldWaitForGreeting && !getIsConnected()) {
1091
+ onConnected();
1092
+ }
1093
+ isStreaming = false;
1094
+ }
1095
+ }
1096
+ }, interval);
1097
+ }
1098
+ let _debug = false;
1099
+ const log = (message, extra) => _debug && console.log(message, extra);
1100
+ const actualRTCPC = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
1101
+ function mapConnectionState(state) {
1102
+ switch (state) {
1103
+ case "connected":
1104
+ return ConnectionState.Connected;
1105
+ case "checking":
1106
+ return ConnectionState.Connecting;
1107
+ case "failed":
1108
+ return ConnectionState.Fail;
1109
+ case "new":
1110
+ return ConnectionState.New;
1111
+ case "closed":
1112
+ return ConnectionState.Closed;
1113
+ case "disconnected":
1114
+ return ConnectionState.Disconnected;
1115
+ case "completed":
1116
+ return ConnectionState.Completed;
1117
+ default:
1118
+ return ConnectionState.New;
1119
+ }
1120
+ }
1121
+ function handleLegacyStreamState({
1122
+ statsSignal,
1123
+ dataChannelSignal,
1124
+ onVideoStateChange,
1125
+ report
1126
+ }) {
1127
+ if (statsSignal === StreamingState.Start && dataChannelSignal === StreamingState.Start) {
1128
+ onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Start);
1129
+ } else if (statsSignal === StreamingState.Stop && dataChannelSignal === StreamingState.Stop) {
1130
+ onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Stop, report);
1131
+ }
1132
+ }
1133
+ function handleFluentStreamState({
1134
+ statsSignal,
1135
+ dataChannelSignal,
1136
+ onVideoStateChange,
1137
+ onAgentActivityStateChange,
1138
+ report
1139
+ }) {
1140
+ if (statsSignal === StreamingState.Start) {
1141
+ onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Start);
1142
+ } else if (statsSignal === StreamingState.Stop) {
1143
+ onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Stop, report);
1144
+ }
1145
+ if (dataChannelSignal === StreamingState.Start) {
1146
+ onAgentActivityStateChange == null ? void 0 : onAgentActivityStateChange(AgentActivityState.Talking);
1147
+ } else if (dataChannelSignal === StreamingState.Stop) {
1148
+ onAgentActivityStateChange == null ? void 0 : onAgentActivityStateChange(AgentActivityState.Idle);
1149
+ }
1150
+ }
1151
+ function handleStreamState({
1152
+ statsSignal,
1153
+ dataChannelSignal,
1154
+ onVideoStateChange,
1155
+ onAgentActivityStateChange,
1156
+ streamType,
1157
+ report
1158
+ }) {
1159
+ if (streamType === StreamType.Legacy) {
1160
+ handleLegacyStreamState({
1161
+ statsSignal,
1162
+ dataChannelSignal,
1163
+ onVideoStateChange,
1164
+ report
1165
+ });
1166
+ } else if (streamType === StreamType.Fluent) {
1167
+ handleFluentStreamState({
1168
+ statsSignal,
1169
+ dataChannelSignal,
1170
+ onVideoStateChange,
1171
+ onAgentActivityStateChange,
1172
+ report
1173
+ });
1174
+ }
1175
+ }
1176
+ async function createStreamingManager(agentId, agent, {
1177
+ debug = false,
1178
+ callbacks,
1179
+ auth,
1180
+ baseURL = didApiUrl
1181
+ }) {
1182
+ _debug = debug;
1183
+ let isConnected = false;
1184
+ let isDatachannelOpen = false;
1185
+ let dataChannelSignal = StreamingState.Stop;
1186
+ let statsSignal = StreamingState.Stop;
1187
+ let connectivityState = ConnectivityState.Unknown;
1188
+ const {
1189
+ startConnection,
1190
+ sendStreamRequest,
1191
+ close,
1192
+ createStream,
1193
+ addIceCandidate
1194
+ } = agent.videoType === VideoType.Clip ? createClipApi(auth, baseURL, agentId, callbacks.onError) : createTalkApi(auth, baseURL, agentId, callbacks.onError);
1195
+ const {
1196
+ id: streamIdFromServer,
1197
+ offer,
1198
+ ice_servers,
1199
+ session_id,
1200
+ fluent
1201
+ } = await createStream(agent);
1202
+ const peerConnection = new actualRTCPC({
1203
+ iceServers: ice_servers
1204
+ });
1205
+ const pcDataChannel = peerConnection.createDataChannel("JanusDataChannel");
1206
+ if (!session_id) {
1207
+ throw new Error("Could not create session_id");
1208
+ }
1209
+ const streamType = fluent ? StreamType.Fluent : StreamType.Legacy;
1210
+ const warmup = agent.stream_warmup && !fluent;
1211
+ const getIsConnected = () => isConnected;
1212
+ const onConnected = () => {
1213
+ var _a;
1214
+ isConnected = true;
1215
+ if (isDatachannelOpen) {
1216
+ (_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, ConnectionState.Connected);
1217
+ }
1218
+ };
1219
+ const videoStatsInterval = pollStats(peerConnection, getIsConnected, onConnected, (state, report) => handleStreamState({
1220
+ statsSignal: state,
1221
+ dataChannelSignal: streamType === StreamType.Legacy ? dataChannelSignal : void 0,
1222
+ onVideoStateChange: callbacks.onVideoStateChange,
1223
+ onAgentActivityStateChange: callbacks.onAgentActivityStateChange,
1224
+ report,
1225
+ streamType
1226
+ }), (state) => {
1227
+ var _a;
1228
+ return (_a = callbacks.onConnectivityStateChange) == null ? void 0 : _a.call(callbacks, connectivityState);
1229
+ }, warmup, !!agent.stream_greeting);
1230
+ peerConnection.onicecandidate = (event) => {
1231
+ var _a;
1232
+ log("peerConnection.onicecandidate", event);
1233
+ try {
1234
+ if (event.candidate && event.candidate.sdpMid && event.candidate.sdpMLineIndex !== null) {
1235
+ addIceCandidate(streamIdFromServer, {
1236
+ candidate: event.candidate.candidate,
1237
+ sdpMid: event.candidate.sdpMid,
1238
+ sdpMLineIndex: event.candidate.sdpMLineIndex
1239
+ }, session_id);
1240
+ } else {
1241
+ addIceCandidate(streamIdFromServer, {
1242
+ candidate: null
1243
+ }, session_id);
1244
+ }
1245
+ } catch (e) {
1246
+ (_a = callbacks.onError) == null ? void 0 : _a.call(callbacks, e, {
1247
+ streamId: streamIdFromServer
1248
+ });
1249
+ }
1250
+ };
1251
+ pcDataChannel.onopen = () => {
1252
+ isDatachannelOpen = true;
1253
+ if (!warmup && !agent.stream_greeting || isConnected) {
1254
+ onConnected();
1255
+ }
1256
+ };
1257
+ pcDataChannel.onmessage = (event) => {
1258
+ if (event.data in DataChannelSignalMap) {
1259
+ dataChannelSignal = DataChannelSignalMap[event.data];
1260
+ handleStreamState({
1261
+ statsSignal: streamType === StreamType.Legacy ? statsSignal : void 0,
1262
+ dataChannelSignal,
1263
+ onVideoStateChange: callbacks.onVideoStateChange,
1264
+ onAgentActivityStateChange: callbacks.onAgentActivityStateChange,
1265
+ streamType
1266
+ });
1267
+ }
1268
+ };
1269
+ peerConnection.oniceconnectionstatechange = () => {
1270
+ var _a;
1271
+ log("peerConnection.oniceconnectionstatechange => " + peerConnection.iceConnectionState);
1272
+ const newState = mapConnectionState(peerConnection.iceConnectionState);
1273
+ if (newState !== ConnectionState.Connected) {
1274
+ (_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, newState);
1275
+ }
1276
+ };
1277
+ peerConnection.ontrack = (event) => {
1278
+ var _a;
1279
+ log("peerConnection.ontrack", event);
1280
+ (_a = callbacks.onSrcObjectReady) == null ? void 0 : _a.call(callbacks, event.streams[0]);
1281
+ };
1282
+ await peerConnection.setRemoteDescription(offer);
1283
+ log("set remote description OK");
1284
+ const sessionClientAnswer = await peerConnection.createAnswer();
1285
+ log("create answer OK");
1286
+ await peerConnection.setLocalDescription(sessionClientAnswer);
1287
+ log("set local description OK");
1288
+ await startConnection(streamIdFromServer, sessionClientAnswer, session_id);
1289
+ log("start connection OK");
1290
+ return {
1291
+ /**
1292
+ * Method to send request to server to get clip or talk depend on you payload
1293
+ * @param payload
1294
+ */
1295
+ speak(payload) {
1296
+ return sendStreamRequest(streamIdFromServer, session_id, payload);
1297
+ },
1298
+ /**
1299
+ * Method to close RTC connection
1300
+ */
1301
+ async disconnect() {
1302
+ var _a, _b, _c;
1303
+ if (streamIdFromServer) {
1304
+ const state = mapConnectionState(peerConnection.iceConnectionState);
1305
+ if (peerConnection) {
1306
+ if (state === ConnectionState.New) {
1307
+ (_a = callbacks.onVideoStateChange) == null ? void 0 : _a.call(callbacks, StreamingState.Stop);
1308
+ clearInterval(videoStatsInterval);
1309
+ return;
1310
+ }
1311
+ peerConnection.close();
1312
+ peerConnection.oniceconnectionstatechange = null;
1313
+ peerConnection.onnegotiationneeded = null;
1314
+ peerConnection.onicecandidate = null;
1315
+ peerConnection.ontrack = null;
1316
+ }
1317
+ try {
1318
+ if (state === ConnectionState.Connected) {
1319
+ await close(streamIdFromServer, session_id).catch((_) => {
1320
+ });
1321
+ }
1322
+ } catch (e) {
1323
+ log("Error on close stream connection", e);
1324
+ }
1325
+ (_b = callbacks.onVideoStateChange) == null ? void 0 : _b.call(callbacks, StreamingState.Stop);
1326
+ (_c = callbacks.onAgentActivityStateChange) == null ? void 0 : _c.call(callbacks, AgentActivityState.Idle);
1327
+ clearInterval(videoStatsInterval);
1328
+ }
1329
+ },
1330
+ /**
1331
+ * Session identifier information, should be returned in the body of all streaming requests
1332
+ */
1333
+ sessionId: session_id,
1334
+ /**
1335
+ * Id of current RTC stream
1336
+ */
1337
+ streamId: streamIdFromServer,
1338
+ streamType
1339
+ };
1340
+ }
1341
+ function getAgentStreamArgs(agent, options, greeting) {
1342
+ var _a;
1343
+ const {
1344
+ streamOptions
1345
+ } = options ?? {};
1346
+ return {
1347
+ videoType: mapVideoType(agent.presenter.type),
1348
+ output_resolution: streamOptions == null ? void 0 : streamOptions.outputResolution,
1349
+ session_timeout: streamOptions == null ? void 0 : streamOptions.sessionTimeout,
1350
+ stream_warmup: streamOptions == null ? void 0 : streamOptions.streamWarmup,
1351
+ compatibility_mode: streamOptions == null ? void 0 : streamOptions.compatibilityMode,
1352
+ stream_greeting: getAgentType(agent.presenter) !== "clip" && ((_a = options == null ? void 0 : options.streamOptions) == null ? void 0 : _a.streamGreeting) ? greeting : void 0
1353
+ };
1354
+ }
1355
+ function handleStateChange(state, agent, statsReport, analytics) {
1356
+ if (timestampTracker.get() > 0) {
1357
+ if (state === StreamingState.Start) {
1358
+ analytics.linkTrack("agent-video", {
1359
+ event: "start",
1360
+ latency: timestampTracker.get(true)
1361
+ }, "start", [StreamEvents.StreamVideoCreated]);
1362
+ } else if (state === StreamingState.Stop) {
1363
+ analytics.linkTrack("agent-video", {
1364
+ event: "stop",
1365
+ is_greenscreen: agent.presenter.type === "clip" && agent.presenter.is_greenscreen,
1366
+ background: agent.presenter.type === "clip" && agent.presenter.background,
1367
+ ...statsReport
1368
+ }, "done", [StreamEvents.StreamVideoDone]);
1369
+ }
1370
+ }
1371
+ }
1372
+ function connectToManager(agent, options, analytics, greeting) {
1373
+ timestampTracker.reset();
1374
+ return new Promise(async (resolve, reject) => {
1375
+ try {
1376
+ const streamingManager = await createStreamingManager(agent.id, getAgentStreamArgs(agent, options, greeting), {
1377
+ ...options,
1378
+ analytics,
1379
+ callbacks: {
1380
+ ...options.callbacks,
1381
+ onConnectionStateChange: (state) => {
1382
+ var _a, _b;
1383
+ (_b = (_a = options.callbacks).onConnectionStateChange) == null ? void 0 : _b.call(_a, state);
1384
+ if (state === ConnectionState.Connected) {
1385
+ resolve(streamingManager);
1386
+ }
1387
+ },
1388
+ onVideoStateChange: (state, statsReport) => {
1389
+ var _a, _b;
1390
+ (_b = (_a = options.callbacks).onVideoStateChange) == null ? void 0 : _b.call(_a, state);
1391
+ handleStateChange(state, agent, statsReport, analytics);
1392
+ }
1393
+ }
1394
+ });
1395
+ } catch (error) {
1396
+ reject(error);
1397
+ }
1398
+ });
1399
+ }
1400
+ async function initializeStreamAndChat(agent, options, agentsApi, analytics, chat, greeting) {
1401
+ var _a, _b, _c, _d;
1402
+ const {
1403
+ chat: newChat,
1404
+ chatMode
1405
+ } = await createChat(agent, agentsApi, analytics, options.mode, options.persistentChat, chat);
1406
+ if (chatMode && chatMode !== options.mode) {
1407
+ options.mode = chatMode;
1408
+ (_b = (_a = options.callbacks).onModeChange) == null ? void 0 : _b.call(_a, chatMode);
1409
+ if (chatMode === ChatMode.TextOnly) {
1410
+ (_d = (_c = options.callbacks).onError) == null ? void 0 : _d.call(_c, new ChatModeDowngraded(chatMode));
1411
+ return {
1412
+ chat: newChat
1413
+ };
1414
+ }
1415
+ }
1416
+ const streamingManager = await connectToManager(agent, options, analytics, greeting);
1417
+ return {
1418
+ chat: newChat,
1419
+ streamingManager
1420
+ };
1421
+ }
1422
+ async function createAgentManager(agent, options) {
1423
+ var _a, _b, _c;
1424
+ let firstConnection = true;
1425
+ const mxKey = options.mixpanelKey || mixpanelKey;
1426
+ const wsURL = options.wsURL || didSocketApiUrl;
1427
+ const baseURL = options.baseURL || didApiUrl;
1428
+ const items = {
1429
+ messages: [],
1430
+ chatMode: options.mode || ChatMode.Functional
1431
+ };
1432
+ const agentsApi = createAgentsApi(options.auth, baseURL, options.callbacks.onError);
1433
+ const agentEntity = await agentsApi.getById(agent);
1434
+ const greeting = getGreetings(agentEntity);
1435
+ const analytics = initializeAnalytics({
1436
+ token: mxKey,
1437
+ agent: agentEntity,
1438
+ isEnabled: options.enableAnalitics,
1439
+ distinctId: options.distinctId
1440
+ });
1441
+ const {
1442
+ onMessage,
1443
+ clearQueue
1444
+ } = createMessageEventQueue(analytics, items, options, agentEntity, () => {
1445
+ var _a2;
1446
+ return (_a2 = items.socketManager) == null ? void 0 : _a2.disconnect();
1447
+ });
1448
+ items.messages = getInitialMessages(greeting, options.initialMessages);
1449
+ (_b = (_a = options.callbacks).onNewMessage) == null ? void 0 : _b.call(_a, [...items.messages], "answer");
1450
+ analytics.track("agent-sdk", {
1451
+ event: "loaded",
1452
+ ...getAnalyticsInfo(agentEntity)
1453
+ });
1454
+ async function connect2(newChat) {
1455
+ var _a2, _b2, _c2, _d, _e, _f, _g;
1456
+ (_b2 = (_a2 = options.callbacks).onConnectionStateChange) == null ? void 0 : _b2.call(_a2, ConnectionState.Connecting);
1457
+ timestampTracker.reset();
1458
+ if (newChat && !firstConnection) {
1459
+ delete items.chat;
1460
+ items.messages = getInitialMessages(greeting);
1461
+ (_d = (_c2 = options.callbacks).onNewMessage) == null ? void 0 : _d.call(_c2, [...items.messages], "answer");
1462
+ }
1463
+ const websocketPromise = options.mode === ChatMode.DirectPlayback ? Promise.resolve(void 0) : createSocketManager(options.auth, wsURL, {
1464
+ onMessage,
1465
+ onError: options.callbacks.onError
1466
+ });
1467
+ const initPromise = retryOperation(() => {
1468
+ return initializeStreamAndChat(agentEntity, options, agentsApi, analytics, items.chat, newChat ? greeting : void 0);
1469
+ }, {
1470
+ limit: 3,
1471
+ timeout: CONNECTION_RETRY_TIMEOUT_MS,
1472
+ timeoutErrorMessage: "Timeout initializing the stream",
1473
+ // Retry on all errors except for connection errors and rate limit errors, these are already handled in client level.
1474
+ shouldRetryFn: (error) => (error == null ? void 0 : error.message) !== "Could not connect" && error.status !== 429,
1475
+ delayMs: 1e3
1476
+ }).catch((e) => {
1477
+ var _a3, _b3;
1478
+ changeMode(ChatMode.Maintenance);
1479
+ (_b3 = (_a3 = options.callbacks).onConnectionStateChange) == null ? void 0 : _b3.call(_a3, ConnectionState.Fail);
1480
+ throw e;
1481
+ });
1482
+ const [socketManager, {
1483
+ streamingManager,
1484
+ chat
1485
+ }] = await Promise.all([websocketPromise, initPromise]);
1486
+ if (chat && chat.id !== ((_e = items.chat) == null ? void 0 : _e.id)) {
1487
+ (_g = (_f = options.callbacks).onNewChat) == null ? void 0 : _g.call(_f, chat.id);
1488
+ }
1489
+ items.streamingManager = streamingManager;
1490
+ items.socketManager = socketManager;
1491
+ items.chat = chat;
1492
+ firstConnection = false;
1493
+ changeMode((chat == null ? void 0 : chat.chat_mode) ?? options.mode ?? ChatMode.Functional);
1494
+ }
1495
+ async function disconnect() {
1496
+ var _a2, _b2, _c2, _d;
1497
+ (_a2 = items.socketManager) == null ? void 0 : _a2.disconnect();
1498
+ await ((_b2 = items.streamingManager) == null ? void 0 : _b2.disconnect());
1499
+ delete items.streamingManager;
1500
+ delete items.socketManager;
1501
+ (_d = (_c2 = options.callbacks).onConnectionStateChange) == null ? void 0 : _d.call(_c2, ConnectionState.Disconnected);
1502
+ }
1503
+ async function changeMode(mode) {
1504
+ var _a2, _b2;
1505
+ if (mode !== items.chatMode) {
1506
+ analytics.track("agent-mode-change", {
1507
+ mode
1508
+ });
1509
+ items.chatMode = mode;
1510
+ if (items.chatMode !== ChatMode.Functional) {
1511
+ await disconnect();
1512
+ }
1513
+ (_b2 = (_a2 = options.callbacks).onModeChange) == null ? void 0 : _b2.call(_a2, mode);
1514
+ }
1515
+ }
1516
+ return {
1517
+ agent: agentEntity,
1518
+ getStreamType: () => {
1519
+ var _a2;
1520
+ return ((_a2 = items.streamingManager) == null ? void 0 : _a2.streamType) || StreamType.Legacy;
1521
+ },
1522
+ starterMessages: ((_c = agentEntity.knowledge) == null ? void 0 : _c.starter_message) || [],
1523
+ getSTTToken: () => agentsApi.getSTTToken(agentEntity.id),
1524
+ changeMode,
1525
+ enrichAnalytics: analytics.enrich,
1526
+ async connect() {
1527
+ var _a2;
1528
+ await connect2(true);
1529
+ analytics.track("agent-chat", {
1530
+ event: "connect",
1531
+ chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
1532
+ agentId: agentEntity.id,
1533
+ mode: items.chatMode
1534
+ });
1535
+ },
1536
+ async reconnect() {
1537
+ var _a2;
1538
+ await disconnect();
1539
+ await connect2(false);
1540
+ analytics.track("agent-chat", {
1541
+ event: "reconnect",
1542
+ chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
1543
+ agentId: agentEntity.id,
1544
+ mode: items.chatMode
1545
+ });
1546
+ },
1547
+ async disconnect() {
1548
+ var _a2;
1549
+ await disconnect();
1550
+ analytics.track("agent-chat", {
1551
+ event: "disconnect",
1552
+ chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
1553
+ agentId: agentEntity.id,
1554
+ mode: items.chatMode
1555
+ });
1556
+ },
1557
+ async chat(userMessage) {
1558
+ var _a2, _b2, _c2, _d, _e;
1559
+ const validateChatRequest = () => {
1560
+ if (options.mode === ChatMode.DirectPlayback) {
1561
+ throw new ValidationError("Direct playback is enabled, chat is disabled");
1562
+ } else if (userMessage.length >= 800) {
1563
+ throw new ValidationError("Message cannot be more than 800 characters");
1564
+ } else if (userMessage.length === 0) {
1565
+ throw new ValidationError("Message cannot be empty");
1566
+ } else if (items.chatMode === ChatMode.Maintenance) {
1567
+ throw new ValidationError("Chat is in maintenance mode");
1568
+ } else if (![ChatMode.TextOnly, ChatMode.Playground].includes(items.chatMode)) {
1569
+ if (!items.streamingManager) {
1570
+ throw new ValidationError("Streaming manager is not initialized");
1571
+ }
1572
+ if (!items.chat) {
1573
+ throw new ValidationError("Chat is not initialized");
1574
+ }
1575
+ }
1576
+ };
1577
+ const initializeChat = async () => {
1578
+ var _a3, _b3;
1579
+ if (!items.chat) {
1580
+ const newChat = await createChat(agentEntity, agentsApi, analytics, items.chatMode, options.persistentChat);
1581
+ if (!newChat.chat) {
1582
+ throw new ChatCreationFailed(items.chatMode, !!options.persistentChat);
1583
+ }
1584
+ items.chat = newChat.chat;
1585
+ (_b3 = (_a3 = options.callbacks).onNewChat) == null ? void 0 : _b3.call(_a3, items.chat.id);
1586
+ }
1587
+ return items.chat.id;
1588
+ };
1589
+ const sendChatRequest = async (messages, chatId) => {
1590
+ return retryOperation(() => {
1591
+ var _a3, _b3;
1592
+ return agentsApi.chat(agentEntity.id, chatId, {
1593
+ chatMode: items.chatMode,
1594
+ streamId: (_a3 = items.streamingManager) == null ? void 0 : _a3.streamId,
1595
+ sessionId: (_b3 = items.streamingManager) == null ? void 0 : _b3.sessionId,
1596
+ messages: messages.map(({
1597
+ matches,
1598
+ ...message
1599
+ }) => message)
1600
+ }, {
1601
+ ...getRequestHeaders(items.chatMode),
1602
+ skipErrorHandler: true
1603
+ });
1604
+ }, {
1605
+ limit: 2,
1606
+ shouldRetryFn: (error) => {
1607
+ var _a3, _b3, _c3, _d2;
1608
+ const isInvalidSessionId = (_a3 = error == null ? void 0 : error.message) == null ? void 0 : _a3.includes("missing or invalid session_id");
1609
+ const isStreamError = (_b3 = error == null ? void 0 : error.message) == null ? void 0 : _b3.includes("Stream Error");
1610
+ if (!isStreamError && !isInvalidSessionId) {
1611
+ (_d2 = (_c3 = options.callbacks).onError) == null ? void 0 : _d2.call(_c3, error);
1612
+ return false;
1613
+ }
1614
+ return true;
1615
+ },
1616
+ onRetry: async () => {
1617
+ await disconnect();
1618
+ await connect2(false);
1619
+ }
1620
+ });
1621
+ };
1622
+ try {
1623
+ clearQueue();
1624
+ validateChatRequest();
1625
+ items.messages.push({
1626
+ id: getRandom(),
1627
+ role: "user",
1628
+ content: userMessage,
1629
+ created_at: new Date(timestampTracker.update()).toISOString()
1630
+ });
1631
+ (_b2 = (_a2 = options.callbacks).onNewMessage) == null ? void 0 : _b2.call(_a2, [...items.messages], "user");
1632
+ const chatId = await initializeChat();
1633
+ const response = await sendChatRequest([...items.messages], chatId);
1634
+ items.messages.push({
1635
+ id: getRandom(),
1636
+ role: "assistant",
1637
+ content: response.result || "",
1638
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
1639
+ context: response.context,
1640
+ matches: response.matches
1641
+ });
1642
+ analytics.track("agent-message-send", {
1643
+ event: "success",
1644
+ mode: items.chatMode,
1645
+ messages: items.messages.length + 1
1646
+ });
1647
+ if (response.result) {
1648
+ (_d = (_c2 = options.callbacks).onNewMessage) == null ? void 0 : _d.call(_c2, [...items.messages], "answer");
1649
+ analytics.track("agent-message-received", {
1650
+ latency: timestampTracker.get(true),
1651
+ mode: items.chatMode,
1652
+ messages: items.messages.length
1653
+ });
1654
+ }
1655
+ return response;
1656
+ } catch (e) {
1657
+ if (((_e = items.messages[items.messages.length - 1]) == null ? void 0 : _e.role) === "assistant") {
1658
+ items.messages.pop();
1659
+ }
1660
+ analytics.track("agent-message-send", {
1661
+ event: "error",
1662
+ mode: items.chatMode,
1663
+ messages: items.messages.length
1664
+ });
1665
+ throw e;
1666
+ }
1667
+ },
1668
+ rate(messageId, score, rateId) {
1669
+ var _a2, _b2, _c2, _d;
1670
+ const message = items.messages.find((message2) => message2.id === messageId);
1671
+ if (!items.chat) {
1672
+ throw new Error("Chat is not initialized");
1673
+ } else if (!message) {
1674
+ throw new Error("Message not found");
1675
+ }
1676
+ const matches = ((_a2 = message.matches) == null ? void 0 : _a2.map((match) => [match.document_id, match.id])) ?? [];
1677
+ analytics.track("agent-rate", {
1678
+ event: rateId ? "update" : "create",
1679
+ thumb: score === 1 ? "up" : "down",
1680
+ knowledge_id: ((_b2 = agentEntity.knowledge) == null ? void 0 : _b2.id) ?? "",
1681
+ mode: items.chatMode,
1682
+ matches,
1683
+ score
1684
+ });
1685
+ if (rateId) {
1686
+ return agentsApi.updateRating(agentEntity.id, items.chat.id, rateId, {
1687
+ knowledge_id: ((_c2 = agentEntity.knowledge) == null ? void 0 : _c2.id) ?? "",
1688
+ message_id: messageId,
1689
+ matches,
1690
+ score
1691
+ });
1692
+ }
1693
+ return agentsApi.createRating(agentEntity.id, items.chat.id, {
1694
+ knowledge_id: ((_d = agentEntity.knowledge) == null ? void 0 : _d.id) ?? "",
1695
+ message_id: messageId,
1696
+ matches,
1697
+ score
1698
+ });
1699
+ },
1700
+ deleteRate(id) {
1701
+ var _a2;
1702
+ if (!items.chat) {
1703
+ throw new Error("Chat is not initialized");
1704
+ }
1705
+ analytics.track("agent-rate-delete", {
1706
+ type: "text",
1707
+ chat_id: (_a2 = items.chat) == null ? void 0 : _a2.id,
1708
+ id,
1709
+ mode: items.chatMode
1710
+ });
1711
+ return agentsApi.deleteRating(agentEntity.id, items.chat.id, id);
1712
+ },
1713
+ speak(payload) {
1714
+ var _a2;
1715
+ if (!items.streamingManager) {
1716
+ throw new Error("Please connect to the agent first");
1717
+ }
1718
+ function getScript() {
1719
+ if (typeof payload === "string") {
1720
+ if (!agentEntity.presenter.voice) {
1721
+ throw new Error("Presenter voice is not initialized");
1722
+ }
1723
+ return {
1724
+ type: "text",
1725
+ provider: agentEntity.presenter.voice,
1726
+ input: payload,
1727
+ ssml: false
1728
+ };
1729
+ }
1730
+ if (payload.type === "text" && !payload.provider) {
1731
+ if (!agentEntity.presenter.voice) {
1732
+ throw new Error("Presenter voice is not initialized");
1733
+ }
1734
+ return {
1735
+ type: "text",
1736
+ provider: agentEntity.presenter.voice,
1737
+ input: payload.input,
1738
+ ssml: payload.ssml
1739
+ };
1740
+ }
1741
+ return payload;
1742
+ }
1743
+ const script = getScript();
1744
+ analytics.track("agent-speak", script);
1745
+ timestampTracker.update();
1746
+ return items.streamingManager.speak({
1747
+ script,
1748
+ metadata: {
1749
+ chat_id: (_a2 = items.chat) == null ? void 0 : _a2.id,
1750
+ agent_id: agentEntity.id
1751
+ }
1752
+ });
1753
+ }
1754
+ };
1755
+ }
1756
+ function getAgent(agentId, auth, baseURL) {
1757
+ const {
1758
+ getById
1759
+ } = createAgentsApi(auth, baseURL || didApiUrl);
1760
+ return getById(agentId);
1761
+ }
1762
+ exports2.AgentActivityState = AgentActivityState;
1763
+ exports2.AgentStatus = AgentStatus;
1764
+ exports2.ChatCreationFailed = ChatCreationFailed;
1765
+ exports2.ChatMode = ChatMode;
1766
+ exports2.ChatModeDowngraded = ChatModeDowngraded;
1767
+ exports2.ChatProgress = ChatProgress;
1768
+ exports2.ConnectionState = ConnectionState;
1769
+ exports2.ConnectivityState = ConnectivityState;
1770
+ exports2.DataChannelSignalMap = DataChannelSignalMap;
1771
+ exports2.DocumentType = DocumentType;
1772
+ exports2.KnowledgeType = KnowledgeType;
1773
+ exports2.PlanGroup = PlanGroup;
1774
+ exports2.Providers = Providers;
1775
+ exports2.RateState = RateState;
1776
+ exports2.StreamEvents = StreamEvents;
1777
+ exports2.StreamType = StreamType;
1778
+ exports2.StreamingState = StreamingState;
1779
+ exports2.Subject = Subject;
1780
+ exports2.UserPlan = UserPlan;
1781
+ exports2.ValidationError = ValidationError;
1782
+ exports2.VideoType = VideoType;
1783
+ exports2.VoiceAccess = VoiceAccess;
1784
+ exports2.WsError = WsError;
1785
+ exports2.createAgentManager = createAgentManager;
1786
+ exports2.getAgent = getAgent;
1787
+ exports2.mapVideoType = mapVideoType;
1788
+ Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
1789
+ });