UNPKG

@anam-ai/js-sdk

Version:

Client side JavaScript SDK for Anam AI

1 lines 37.1 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.anam=t():e.anam=t()}(self,(()=>(()=>{"use strict";var e={816:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,r){function o(e){try{l(i.next(e))}catch(e){r(e)}}function a(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const s=n(71),r=n(958),o=n(170);t.default=class{constructor(e,t,n){this.inputAudioState={isMuted:!1},this.sessionId=null,this.streamingClient=null,this._isStreaming=!1;const i=this.validateClientConfig(e,t,n);if(i)throw new Error(i);this.personaConfig=t,this.clientOptions=n,this.publicEventEmitter=new r.PublicEventEmitter,this.internalEventEmitter=new r.InternalEventEmitter,this.apiClient=new r.CoreApiRestClient(e,null==n?void 0:n.apiKey,null==n?void 0:n.api),this.messageHistoryClient=new r.MessageHistoryClient(this.publicEventEmitter,this.internalEventEmitter)}decodeJwt(e){try{const t=e.split(".")[1];return JSON.parse(atob(t))}catch(e){throw new Error("Invalid session token format")}}validateClientConfig(e,t,n){var i;if(!e&&!(null==n?void 0:n.apiKey))return"Either sessionToken or apiKey must be provided";if((null==n?void 0:n.apiKey)&&e)return"Only one of sessionToken or apiKey should be used";if(e){const n=null===(i=this.decodeJwt(e).type)||void 0===i?void 0:i.toLowerCase();if("legacy"===n){if(!t||!("personaId"in t))return"Both session token and client are missing a persona configuration. Please provide a persona ID of a saved persona in the personaConfig parameter."}else if(("ephemeral"===n||"stateful"===n)&&t)return"This session token already contains a persona configuration. Please remove the personaConfig parameter."}else if(!t)return"Missing persona config. Persona configuration must be provided when using apiKey";if((null==n?void 0:n.voiceDetection)&&void 0!==n.voiceDetection.endOfSpeechSensitivity){if("number"!=typeof n.voiceDetection.endOfSpeechSensitivity)return"End of speech sensitivity must be a number";if(n.voiceDetection.endOfSpeechSensitivity<0||n.voiceDetection.endOfSpeechSensitivity>1)return"End of speech sensitivity must be between 0 and 1"}}buildStartSessionOptionsForClient(){var e;const t={};if((null===(e=this.clientOptions)||void 0===e?void 0:e.voiceDetection)&&(t.voiceDetection=this.clientOptions.voiceDetection),0!==Object.keys(t).length)return t}startSession(e){return i(this,void 0,void 0,(function*(){var t;const n=this.personaConfig,i=this.buildStartSessionOptionsForClient(),o=yield this.apiClient.startSession(n,i),{sessionId:a,clientConfig:l,engineHost:c,engineProtocol:d,signallingEndpoint:E}=o,{heartbeatIntervalSeconds:u,maxWsReconnectionAttempts:h,iceServers:C}=l;try{this.streamingClient=new r.StreamingClient(a,{engine:{baseUrl:`${d}://${c}`},signalling:{heartbeatIntervalSeconds:u,maxWsReconnectionAttempts:h,url:{baseUrl:c,protocol:d,signallingPath:E}},iceServers:C,inputAudio:{inputAudioState:this.inputAudioState,userProvidedMediaStream:e,audioDeviceId:null===(t=this.clientOptions)||void 0===t?void 0:t.audioDeviceId}},this.publicEventEmitter,this.internalEventEmitter)}catch(e){throw new s.ClientError("Failed to initialize streaming client",s.ErrorCode.CLIENT_ERROR_CODE_SERVER_ERROR,500,{cause:e instanceof Error?e.message:String(e)})}return this.sessionId=a,a}))}startSessionIfNeeded(e){return i(this,void 0,void 0,(function*(){if(!(this.sessionId&&this.streamingClient||(yield this.startSession(e),this.sessionId&&this.streamingClient)))throw new s.ClientError("Session ID or streaming client is not available after starting session",s.ErrorCode.CLIENT_ERROR_CODE_SERVER_ERROR,500,{cause:"Failed to initialize session properly"})}))}stream(e){return i(this,void 0,void 0,(function*(){if(yield this.startSessionIfNeeded(e),this._isStreaming)throw new Error("Already streaming");return this._isStreaming=!0,new Promise((e=>{var t;const n=[];let i=!1,s=!1;this.publicEventEmitter.addListener(o.AnamEvent.VIDEO_STREAM_STARTED,(t=>{n.push(t),i=!0,s&&e(n)})),this.publicEventEmitter.addListener(o.AnamEvent.AUDIO_STREAM_STARTED,(t=>{n.push(t),s=!0,i&&e(n)})),null===(t=this.streamingClient)||void 0===t||t.startConnection()}))}))}streamToVideoAndAudioElements(e,t,n){return i(this,void 0,void 0,(function*(){try{yield this.startSessionIfNeeded(n)}catch(e){if(e instanceof s.ClientError)throw e;throw new s.ClientError("Failed to start session",s.ErrorCode.CLIENT_ERROR_CODE_SERVER_ERROR,500,{cause:e instanceof Error?e.message:String(e)})}if(this._isStreaming)throw new Error("Already streaming");if(this._isStreaming=!0,!this.streamingClient)throw new Error("Failed to stream: streaming client is not available");this.streamingClient.setMediaStreamTargetsById(e,t),this.streamingClient.startConnection()}))}talk(e){return i(this,void 0,void 0,(function*(){if(!this.streamingClient)throw new Error("Failed to send talk command: session is not started. Have you called startSession?");if(!this._isStreaming)throw new Error("Failed to send talk command: not currently streaming. Have you called stream?");yield this.streamingClient.sendTalkCommand(e)}))}sendDataMessage(e){if(!this.streamingClient)throw new Error("Failed to send message: session is not started.");this.streamingClient.sendDataMessage(e)}stopStreaming(){return i(this,void 0,void 0,(function*(){this.streamingClient&&(this.streamingClient.stopConnection(),this.streamingClient=null,this.sessionId=null,this._isStreaming=!1)}))}isStreaming(){return this._isStreaming}setPersonaConfig(e){this.personaConfig=e}getPersonaConfig(){return this.personaConfig}getInputAudioState(){return this.streamingClient&&(this.inputAudioState=this.streamingClient.getInputAudioState()),this.inputAudioState}muteInputAudio(){return this.streamingClient?this.inputAudioState=this.streamingClient.muteInputAudio():this.inputAudioState=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!0}),this.inputAudioState}unmuteInputAudio(){return this.streamingClient?this.inputAudioState=this.streamingClient.unmuteInputAudio():this.inputAudioState=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!1}),this.inputAudioState}createTalkMessageStream(e){if(!this.streamingClient)throw new Error("Failed to start talk message stream: session is not started.");if(e&&""===e.trim())throw new Error("Failed to start talk message stream: correlationId is empty");return this.streamingClient.startTalkMessageStream(e)}addListener(e,t){this.publicEventEmitter.addListener(e,t)}removeListener(e,t){this.publicEventEmitter.removeListener(e,t)}getActiveSessionId(){return this.sessionId}}},440:function(e,t,n){var i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.unsafe_createClientWithApiKey=t.createClient=void 0;const s=i(n(816));t.createClient=(e,t,n)=>new s.default(e,t,n),t.unsafe_createClientWithApiKey=(e,t,n)=>new s.default(void 0,t,Object.assign(Object.assign({},n),{apiKey:e}))},71:function(e,t){var n,i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,r){function o(e){try{l(i.next(e))}catch(e){r(e)}}function a(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.ClientError=t.sendErrorMetric=t.ErrorCode=void 0,function(e){e.CLIENT_ERROR_CODE_USAGE_LIMIT_REACHED="CLIENT_ERROR_CODE_USAGE_LIMIT_REACHED",e.CLIENT_ERROR_CODE_VALIDATION_ERROR="CLIENT_ERROR_CODE_VALIDATION_ERROR",e.CLIENT_ERROR_CODE_AUTHENTICATION_ERROR="CLIENT_ERROR_CODE_AUTHENTICATION_ERROR",e.CLIENT_ERROR_CODE_SERVER_ERROR="CLIENT_ERROR_CODE_SERVER_ERROR",e.CLIENT_ERROR_CODE_MAX_CONCURRENT_SESSIONS_REACHED="CLIENT_ERROR_CODE_MAX_CONCURRENT_SESSIONS_REACHED",e.CLIENT_ERROR_CODE_SERVICE_BUSY="CLIENT_ERROR_CODE_SERVICE_BUSY",e.CLIENT_ERROR_CODE_NO_PLAN_FOUND="CLIENT_ERROR_CODE_NO_PLAN_FOUND",e.CLIENT_ERROR_CODE_UNKNOWN_ERROR="CLIENT_ERROR_CODE_UNKNOWN_ERROR"}(n||(t.ErrorCode=n={})),t.sendErrorMetric=(e,t,n)=>i(void 0,void 0,void 0,(function*(){try{yield fetch("https://api.anam.ai/v1/metrics/client",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:e,value:t,tags:n})})}catch(e){console.error("Failed to send error metric:",e)}}));class s extends Error{constructor(e,n,i=500,r){super(e),this.name="ClientError",this.code=n,this.statusCode=i,this.details=r,Object.setPrototypeOf(this,s.prototype),(0,t.sendErrorMetric)("client_error",n,{details:r,statusCode:i})}}t.ClientError=s},985:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.CONNECTION_CLOSED_CODE_WEBRTC_FAILURE=t.CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE=t.CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED=t.CONNECTION_CLOSED_CODE_NORMAL=t.DEFAULT_API_VERSION=t.DEFAULT_API_BASE_URL=t.DEFAULT_HEADERS=void 0,t.DEFAULT_HEADERS={"Content-Type":"application/json"},t.DEFAULT_API_BASE_URL="https://api.anam.ai",t.DEFAULT_API_VERSION="/v1",t.CONNECTION_CLOSED_CODE_NORMAL="CONNECTION_CLOSED_CODE_NORMAL",t.CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED="CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED",t.CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE="CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE",t.CONNECTION_CLOSED_CODE_WEBRTC_FAILURE="CONNECTION_CLOSED_CODE_WEBRTC_FAILURE"},18:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,r){function o(e){try{l(i.next(e))}catch(e){r(e)}}function a(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.CoreApiRestClient=void 0;const s=n(71),r=n(985),o=n(672);t.CoreApiRestClient=class{constructor(e,t,n){if(!e&&!t)throw new Error("Either sessionToken or apiKey must be provided");this.sessionToken=e||null,this.apiKey=t||null,this.baseUrl=(null==n?void 0:n.baseUrl)||r.DEFAULT_API_BASE_URL,this.apiVersion=(null==n?void 0:n.apiVersion)||r.DEFAULT_API_VERSION}startSession(e,t){return i(this,void 0,void 0,(function*(){if(!this.sessionToken){if(!e)throw new s.ClientError("Persona configuration must be provided when using apiKey",s.ErrorCode.CLIENT_ERROR_CODE_VALIDATION_ERROR,400);this.sessionToken=yield this.unsafe_getSessionToken(e)}try{const n=yield fetch(`${this.getApiUrl()}/engine/session`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.sessionToken}`},body:JSON.stringify({personaConfig:e,sessionOptions:t})}),i=yield n.json();switch(n.status){case 200:case 201:return i;case 400:throw new s.ClientError("Invalid request to start session",s.ErrorCode.CLIENT_ERROR_CODE_VALIDATION_ERROR,400,{cause:i.message});case 401:throw new s.ClientError("Authentication failed when starting session",s.ErrorCode.CLIENT_ERROR_CODE_AUTHENTICATION_ERROR,401,{cause:i.message});case 402:throw new s.ClientError("Please sign up for a plan to start a session",s.ErrorCode.CLIENT_ERROR_CODE_NO_PLAN_FOUND,402,{cause:i.message});case 403:throw new s.ClientError("Authentication failed when starting session",s.ErrorCode.CLIENT_ERROR_CODE_AUTHENTICATION_ERROR,403,{cause:i.message});case 429:throw new s.ClientError("Out of credits, please upgrade your plan",s.ErrorCode.CLIENT_ERROR_CODE_USAGE_LIMIT_REACHED,429,{cause:i.message});case 503:throw new s.ClientError("There are no available personas, please try again later",s.ErrorCode.CLIENT_ERROR_CODE_SERVICE_BUSY,503,{cause:i.message});default:throw new s.ClientError("Unknown error when starting session",s.ErrorCode.CLIENT_ERROR_CODE_SERVER_ERROR,500,{cause:i.message})}}catch(e){if(e instanceof s.ClientError)throw e;throw new s.ClientError("Failed to start session",s.ErrorCode.CLIENT_ERROR_CODE_SERVER_ERROR,500,{cause:e instanceof Error?e.message:String(e)})}}))}unsafe_getSessionToken(e){return i(this,void 0,void 0,(function*(){if(console.warn("Using unsecure method. This method should not be used in production."),!this.apiKey)throw new Error("No apiKey provided");let t={clientLabel:"js-sdk-api-key"};(0,o.isCustomPersonaConfig)(e)&&(t=Object.assign(Object.assign({},t),{personaConfig:e}));try{const e=yield fetch(`${this.getApiUrl()}/auth/session-token`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:JSON.stringify(t)});return(yield e.json()).sessionToken}catch(e){throw new Error("Failed to get session token")}}))}getApiUrl(){return`${this.baseUrl}${this.apiVersion}`}}},529:function(e,t){var n=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,r){function o(e){try{l(i.next(e))}catch(e){r(e)}}function a(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.EngineApiRestClient=void 0,t.EngineApiRestClient=class{constructor(e,t){this.baseUrl=e,this.sessionId=t}sendTalkCommand(e){return n(this,void 0,void 0,(function*(){try{const t=yield fetch(`${this.baseUrl}/talk?session_id=${this.sessionId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:e})});if(!t.ok)throw new Error(`Failed to send talk command: ${t.status} ${t.statusText}`)}catch(e){throw console.error(e),new Error("EngineApiRestClient - sendTalkCommand: Failed to send talk command")}}))}}},259:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEventEmitter=void 0,t.InternalEventEmitter=class{constructor(){this.listeners={}}addListener(e,t){this.listeners[e]||(this.listeners[e]=new Set),this.listeners[e].add(t)}removeListener(e,t){this.listeners[e]&&this.listeners[e].delete(t)}emit(e,...t){this.listeners[e]&&this.listeners[e].forEach((e=>{e(...t)}))}}},462:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MessageHistoryClient=void 0;const i=n(170);t.MessageHistoryClient=class{constructor(e,t){this.messages=[],this.publicEventEmitter=e,this.internalEventEmitter=t,this.internalEventEmitter.addListener(i.InternalEvent.WEBRTC_CHAT_MESSAGE_RECEIVED,this.processWebRtcTextMessageEvent.bind(this))}webRtcTextMessageEventToMessageStreamEvent(e){return{id:`${e.role}::${e.message_id}`,content:e.content,role:e.role,endOfSpeech:e.end_of_speech,interrupted:e.interrupted}}processUserMessage(e){const t={id:e.id,content:e.content,role:e.role};this.messages.push(t)}processPersonaMessage(e){const t={id:e.id,content:e.content,role:e.role,interrupted:e.interrupted},n=this.messages.findIndex((e=>e.id===t.id));if(-1!==n){const e=this.messages[n];this.messages[n]=Object.assign(Object.assign({},e),{content:e.content+t.content,interrupted:e.interrupted||t.interrupted})}else this.messages.push(t)}processWebRtcTextMessageEvent(e){const t=this.webRtcTextMessageEventToMessageStreamEvent(e);switch(this.publicEventEmitter.emit(i.AnamEvent.MESSAGE_STREAM_EVENT_RECEIVED,t),t.role){case i.MessageRole.USER:this.processUserMessage(t);break;case i.MessageRole.PERSONA:this.processPersonaMessage(t)}t.endOfSpeech&&this.publicEventEmitter.emit(i.AnamEvent.MESSAGE_HISTORY_UPDATED,this.messages)}}},995:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PublicEventEmitter=void 0;const i=n(71),s=n(170);t.PublicEventEmitter=class{constructor(){this.listeners={}}addListener(e,t){this.listeners[e]||(this.listeners[e]=new Set),this.listeners[e].add(t)}removeListener(e,t){this.listeners[e]&&this.listeners[e].delete(t)}emit(e,...t){this.listeners[e]&&(e===s.AnamEvent.CONNECTION_CLOSED&&(0,i.sendErrorMetric)("client_connection_closed",t[0]),this.listeners[e].forEach((e=>{e(...t)})))}}},279:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,r){function o(e){try{l(i.next(e))}catch(e){r(e)}}function a(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.SignallingClient=void 0;const s=n(985),r=n(170);t.SignallingClient=class{constructor(e,t,n,i){var s;if(this.stopSignal=!1,this.sendingBuffer=[],this.wsConnectionAttempts=0,this.socket=null,this.heartBeatIntervalRef=null,this.publicEventEmitter=n,this.internalEventEmitter=i,!e)throw new Error("Signalling Client: sessionId is required");this.sessionId=e;const{heartbeatIntervalSeconds:r,maxWsReconnectionAttempts:o,url:a}=t;if(this.heartbeatIntervalSeconds=r||5,this.maxWsReconnectionAttempts=o||5,!a.baseUrl)throw new Error("Signalling Client: baseUrl is required");const l=`${a.protocol||"https"}://${a.baseUrl}`;this.url=new URL(l),this.url.protocol="http"===a.protocol?"ws:":"wss:",a.port&&(this.url.port=a.port),this.url.pathname=null!==(s=a.signallingPath)&&void 0!==s?s:"/ws",this.url.searchParams.append("session_id",e)}stop(){this.stopSignal=!0,this.closeSocket()}connect(){return this.socket=new WebSocket(this.url.href),this.socket.onopen=this.onOpen.bind(this),this.socket.onclose=this.onClose.bind(this),this.socket.onerror=this.onError.bind(this),this.socket}sendOffer(e){return i(this,void 0,void 0,(function*(){const t={connectionDescription:e,userUid:this.sessionId},n={actionType:r.SignalMessageAction.OFFER,sessionId:this.sessionId,payload:t};this.sendSignalMessage(n)}))}sendIceCandidate(e){return i(this,void 0,void 0,(function*(){const t={actionType:r.SignalMessageAction.ICE_CANDIDATE,sessionId:this.sessionId,payload:e.toJSON()};this.sendSignalMessage(t)}))}sendSignalMessage(e){var t;if((null===(t=this.socket)||void 0===t?void 0:t.readyState)===WebSocket.OPEN)try{this.socket.send(JSON.stringify(e))}catch(e){console.error("SignallingClient - sendSignalMessage: error sending message",e)}else this.sendingBuffer.push(e)}sendTalkMessage(e){return i(this,void 0,void 0,(function*(){const t={actionType:r.SignalMessageAction.TALK_STREAM_INPUT,sessionId:this.sessionId,payload:e};this.sendSignalMessage(t)}))}closeSocket(){this.socket&&(this.socket.close(),this.socket=null),this.heartBeatIntervalRef&&(clearInterval(this.heartBeatIntervalRef),this.heartBeatIntervalRef=null)}onOpen(){return i(this,void 0,void 0,(function*(){if(!this.socket)throw new Error("SignallingClient - onOpen: socket is null");try{this.wsConnectionAttempts=0,this.flushSendingBuffer(),this.socket.onmessage=this.onMessage.bind(this),this.startSendingHeartBeats(),this.internalEventEmitter.emit(r.InternalEvent.WEB_SOCKET_OPEN)}catch(e){console.error("SignallingClient - onOpen: error in onOpen",e),this.publicEventEmitter.emit(r.AnamEvent.CONNECTION_CLOSED,s.CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE)}}))}onClose(){return i(this,void 0,void 0,(function*(){this.wsConnectionAttempts+=1,this.stopSignal||(this.wsConnectionAttempts<=this.maxWsReconnectionAttempts?(this.socket=null,setTimeout((()=>{this.connect()}),100*this.wsConnectionAttempts)):(this.heartBeatIntervalRef&&(clearInterval(this.heartBeatIntervalRef),this.heartBeatIntervalRef=null),this.publicEventEmitter.emit(r.AnamEvent.CONNECTION_CLOSED,s.CONNECTION_CLOSED_CODE_SIGNALLING_CLIENT_CONNECTION_FAILURE)))}))}onError(e){this.stopSignal||console.error("SignallingClient - onError: ",e)}flushSendingBuffer(){const e=[];this.sendingBuffer.length>0&&this.sendingBuffer.forEach((t=>{var n;(null===(n=this.socket)||void 0===n?void 0:n.readyState)===WebSocket.OPEN?this.socket.send(JSON.stringify(t)):e.push(t)})),this.sendingBuffer=e}onMessage(e){return i(this,void 0,void 0,(function*(){const t=JSON.parse(e.data);this.internalEventEmitter.emit(r.InternalEvent.SIGNAL_MESSAGE_RECEIVED,t)}))}startSendingHeartBeats(){if(!this.socket)throw new Error("SignallingClient - startSendingHeartBeats: socket is null");this.heartBeatIntervalRef&&console.warn("SignallingClient - startSendingHeartBeats: heartbeat interval already set");const e=1e3*this.heartbeatIntervalSeconds,t={actionType:r.SignalMessageAction.HEARTBEAT,sessionId:this.sessionId,payload:""},n=JSON.stringify(t);this.heartBeatIntervalRef=setInterval((()=>{var e;this.stopSignal||(null===(e=this.socket)||void 0===e?void 0:e.readyState)===WebSocket.OPEN&&this.socket.send(n)}),e)}}},577:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,r){function o(e){try{l(i.next(e))}catch(e){r(e)}}function a(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.StreamingClient=void 0;const s=n(985),r=n(958),o=n(170),a=n(711);t.StreamingClient=class{constructor(e,t,n,i){this.peerConnection=null,this.connectionReceivedAnswer=!1,this.remoteIceCandidateBuffer=[],this.inputAudioStream=null,this.dataChannel=null,this.videoElement=null,this.videoStream=null,this.audioElement=null,this.audioStream=null,this.inputAudioState={isMuted:!1},this.publicEventEmitter=n,this.internalEventEmitter=i;const{inputAudio:s}=t;this.inputAudioState=s.inputAudioState,t.inputAudio.userProvidedMediaStream&&(this.inputAudioStream=t.inputAudio.userProvidedMediaStream),this.internalEventEmitter.addListener(o.InternalEvent.WEB_SOCKET_OPEN,this.onSignallingClientConnected.bind(this)),this.internalEventEmitter.addListener(o.InternalEvent.SIGNAL_MESSAGE_RECEIVED,this.onSignalMessage.bind(this)),this.iceServers=t.iceServers,this.signallingClient=new r.SignallingClient(e,t.signalling,this.publicEventEmitter,this.internalEventEmitter),this.engineApiRestClient=new r.EngineApiRestClient(t.engine.baseUrl,e),this.audioDeviceId=t.inputAudio.audioDeviceId}onInputAudioStateChange(e,t){e.isMuted!==t.isMuted&&(t.isMuted?this.muteAllAudioTracks():this.unmuteAllAudioTracks())}muteAllAudioTracks(){var e;null===(e=this.inputAudioStream)||void 0===e||e.getAudioTracks().forEach((e=>{e.enabled=!1}))}unmuteAllAudioTracks(){var e;null===(e=this.inputAudioStream)||void 0===e||e.getAudioTracks().forEach((e=>{e.enabled=!0}))}muteInputAudio(){const e=this.inputAudioState,t=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!0});return this.inputAudioState=t,this.onInputAudioStateChange(e,t),this.inputAudioState}unmuteInputAudio(){const e=this.inputAudioState,t=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!1});return this.inputAudioState=t,this.onInputAudioStateChange(e,t),this.inputAudioState}getInputAudioState(){return this.inputAudioState}getPeerConnection(){return this.peerConnection}getInputAudioStream(){return this.inputAudioStream}getVideoStream(){return this.videoStream}getAudioStream(){return this.audioStream}sendDataMessage(e){this.dataChannel&&"open"===this.dataChannel.readyState&&this.dataChannel.send(e)}setMediaStreamTargetsById(e,t){if(e){const t=document.getElementById(e);if(!t)throw new Error(`StreamingClient: video element with id ${e} not found`);this.videoElement=t}if(t){const e=document.getElementById(t);if(!e)throw new Error(`StreamingClient: audio element with id ${t} not found`);this.audioElement=e}}startConnection(){try{if(this.peerConnection)return void console.error("StreamingClient - startConnection: peer connection already exists");this.signallingClient.connect()}catch(e){console.log("StreamingClient - startConnection: error",e),this.handleWebrtcFailure(e)}}stopConnection(){this.shutdown()}sendTalkCommand(e){return i(this,void 0,void 0,(function*(){if(!this.peerConnection)throw new Error("StreamingClient - sendTalkCommand: peer connection is null");yield this.engineApiRestClient.sendTalkCommand(e)}))}startTalkMessageStream(e){return e||(e=Math.random().toString(36).substring(2,15)),new a.TalkMessageStream(e,this.internalEventEmitter,this.signallingClient)}initPeerConnection(){return i(this,void 0,void 0,(function*(){this.peerConnection=new RTCPeerConnection({iceServers:this.iceServers}),this.peerConnection.onicecandidate=this.onIceCandidate.bind(this),this.peerConnection.oniceconnectionstatechange=this.onIceConnectionStateChange.bind(this),this.peerConnection.onconnectionstatechange=this.onConnectionStateChange.bind(this),this.peerConnection.addEventListener("track",this.onTrackEventHandler.bind(this)),yield this.setupDataChannels(),this.peerConnection.addTransceiver("video",{direction:"recvonly"}),this.peerConnection.addTransceiver("audio",{direction:"sendrecv"})}))}onSignalMessage(e){return i(this,void 0,void 0,(function*(){if(this.peerConnection)switch(e.actionType){case o.SignalMessageAction.ANSWER:const t=e.payload;yield this.peerConnection.setRemoteDescription(t),this.connectionReceivedAnswer=!0,this.flushRemoteIceCandidateBuffer();break;case o.SignalMessageAction.ICE_CANDIDATE:const n=e.payload,i=new RTCIceCandidate(n);this.connectionReceivedAnswer?yield this.peerConnection.addIceCandidate(i):this.remoteIceCandidateBuffer.push(i);break;case o.SignalMessageAction.END_SESSION:const r=e.payload;console.log("StreamingClient - onSignalMessage: reason",r),this.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s.CONNECTION_CLOSED_CODE_NORMAL),this.shutdown();break;case o.SignalMessageAction.WARNING:const a=e.payload;console.warn("Warning received from server: "+a);break;case o.SignalMessageAction.TALK_STREAM_INTERRUPTED:const l=e.payload;this.publicEventEmitter.emit(o.AnamEvent.TALK_STREAM_INTERRUPTED,l.correlationId);break;default:console.error("StreamingClient - onSignalMessage: unknown signal message action type",e)}else console.error("StreamingClient - onSignalMessage: peerConnection is not initialized")}))}onSignallingClientConnected(){return i(this,void 0,void 0,(function*(){if(!this.peerConnection)try{yield this.initPeerConnectionAndSendOffer()}catch(e){console.error("StreamingClient - onSignallingClientConnected: Error initializing peer connection",e),this.handleWebrtcFailure(e)}}))}flushRemoteIceCandidateBuffer(){this.remoteIceCandidateBuffer.forEach((e=>{var t;null===(t=this.peerConnection)||void 0===t||t.addIceCandidate(e)})),this.remoteIceCandidateBuffer=[]}onIceCandidate(e){e.candidate&&this.signallingClient.sendIceCandidate(e.candidate)}onIceConnectionStateChange(){var e,t;"connected"!==(null===(e=this.peerConnection)||void 0===e?void 0:e.iceConnectionState)&&"completed"!==(null===(t=this.peerConnection)||void 0===t?void 0:t.iceConnectionState)||this.publicEventEmitter.emit(o.AnamEvent.CONNECTION_ESTABLISHED)}onConnectionStateChange(){var e;"closed"===(null===(e=this.peerConnection)||void 0===e?void 0:e.connectionState)&&(console.error("StreamingClient - onConnectionStateChange: Connection closed"),this.handleWebrtcFailure("The connection to our servers was lost. Please try again."))}handleWebrtcFailure(e){console.error({message:"StreamingClient - handleWebrtcFailure: ",err:e}),"NotAllowedError"===e.name&&"Permission denied"===e.message?this.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s.CONNECTION_CLOSED_CODE_MICROPHONE_PERMISSION_DENIED):this.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s.CONNECTION_CLOSED_CODE_WEBRTC_FAILURE);try{this.stopConnection()}catch(e){console.error("StreamingClient - handleWebrtcFailure: error stopping connection",e)}}onTrackEventHandler(e){if("video"===e.track.kind){if(this.videoStream=e.streams[0],this.publicEventEmitter.emit(o.AnamEvent.VIDEO_STREAM_STARTED,this.videoStream),this.videoElement){this.videoElement.srcObject=this.videoStream;const e=this.videoElement.requestVideoFrameCallback((()=>{var t;null===(t=this.videoElement)||void 0===t||t.cancelVideoFrameCallback(e),this.publicEventEmitter.emit(o.AnamEvent.VIDEO_PLAY_STARTED)}))}}else"audio"===e.track.kind&&(this.audioStream=e.streams[0],this.publicEventEmitter.emit(o.AnamEvent.AUDIO_STREAM_STARTED,this.audioStream),this.audioElement&&(this.audioElement.srcObject=this.audioStream))}setupDataChannels(){return i(this,void 0,void 0,(function*(){if(!this.peerConnection)return void console.error("StreamingClient - setupDataChannels: peer connection is not initialized");if(this.inputAudioStream){if(!this.inputAudioStream.getAudioTracks().length)throw new Error("StreamingClient - setupDataChannels: user provided stream does not have audio tracks")}else{const e={echoCancellation:!0};this.audioDeviceId&&(e.deviceId={exact:this.audioDeviceId}),this.inputAudioStream=yield navigator.mediaDevices.getUserMedia({audio:e})}this.inputAudioState.isMuted&&this.muteAllAudioTracks();const e=this.inputAudioStream.getAudioTracks()[0];this.peerConnection.addTrack(e,this.inputAudioStream),this.publicEventEmitter.emit(o.AnamEvent.INPUT_AUDIO_STREAM_STARTED,this.inputAudioStream);const t=this.peerConnection.createDataChannel("chat",{ordered:!0});t.onopen=()=>{this.dataChannel=null!=t?t:null},t.onclose=()=>{},t.onmessage=e=>{const t=JSON.parse(e.data);this.internalEventEmitter.emit(o.InternalEvent.WEBRTC_CHAT_MESSAGE_RECEIVED,t)}}))}initPeerConnectionAndSendOffer(){return i(this,void 0,void 0,(function*(){if(yield this.initPeerConnection(),this.peerConnection){try{const e=yield this.peerConnection.createOffer();yield this.peerConnection.setLocalDescription(e)}catch(e){console.error("StreamingClient - initPeerConnectionAndSendOffer: error creating offer",e)}if(!this.peerConnection.localDescription)throw new Error("StreamingClient - initPeerConnectionAndSendOffer: local description is null");yield this.signallingClient.sendOffer(this.peerConnection.localDescription)}else console.error("StreamingClient - initPeerConnectionAndSendOffer: peer connection is not initialized")}))}shutdown(){try{this.inputAudioStream&&this.inputAudioStream.getTracks().forEach((e=>{e.stop()})),this.inputAudioStream=null}catch(e){console.error("StreamingClient - shutdown: error stopping input audio stream",e)}try{this.signallingClient.stop()}catch(e){console.error("StreamingClient - shutdown: error stopping signallilng",e)}try{this.peerConnection&&"closed"!==this.peerConnection.connectionState&&(this.peerConnection.onconnectionstatechange=null,this.peerConnection.close(),this.peerConnection=null)}catch(e){console.error("StreamingClient - shutdown: error closing peer connection",e)}}}},958:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.StreamingClient=t.PublicEventEmitter=t.MessageHistoryClient=t.InternalEventEmitter=t.EngineApiRestClient=t.CoreApiRestClient=t.SignallingClient=void 0;var i=n(279);Object.defineProperty(t,"SignallingClient",{enumerable:!0,get:function(){return i.SignallingClient}});var s=n(18);Object.defineProperty(t,"CoreApiRestClient",{enumerable:!0,get:function(){return s.CoreApiRestClient}});var r=n(529);Object.defineProperty(t,"EngineApiRestClient",{enumerable:!0,get:function(){return r.EngineApiRestClient}});var o=n(259);Object.defineProperty(t,"InternalEventEmitter",{enumerable:!0,get:function(){return o.InternalEventEmitter}});var a=n(462);Object.defineProperty(t,"MessageHistoryClient",{enumerable:!0,get:function(){return a.MessageHistoryClient}});var l=n(995);Object.defineProperty(t,"PublicEventEmitter",{enumerable:!0,get:function(){return l.PublicEventEmitter}});var c=n(577);Object.defineProperty(t,"StreamingClient",{enumerable:!0,get:function(){return c.StreamingClient}})},672:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.isCustomPersonaConfig=t.isSavedPersonaConfig=void 0,t.isSavedPersonaConfig=function(e){return"personaId"in e},t.isCustomPersonaConfig=function(e){return"brainType"in e}},711:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,r){function o(e){try{l(i.next(e))}catch(e){r(e)}}function a(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.TalkMessageStream=void 0;const s=n(170),r=n(322);t.TalkMessageStream=class{constructor(e,t,n){this.state=r.TalkMessageStreamState.UNSTARTED,this.correlationId=e,this.internalEventEmitter=t,this.signallingClient=n,this.internalEventEmitter.addListener(s.InternalEvent.SIGNAL_MESSAGE_RECEIVED,this.onSignalMessage.bind(this))}onDeactivate(){this.internalEventEmitter.removeListener(s.InternalEvent.SIGNAL_MESSAGE_RECEIVED,this.onSignalMessage.bind(this))}onSignalMessage(e){return i(this,void 0,void 0,(function*(){e.actionType===s.SignalMessageAction.TALK_STREAM_INTERRUPTED&&e.payload.correlationId===this.correlationId&&(this.state=r.TalkMessageStreamState.INTERRUPTED,this.onDeactivate())}))}endMessage(){return i(this,void 0,void 0,(function*(){if(this.state===r.TalkMessageStreamState.ENDED)return void console.warn("Talk stream is already ended via end of speech. No need to call endMessage.");if(this.state!==r.TalkMessageStreamState.STREAMING)return void console.warn("Talk stream is not active state: "+this.state);const e={content:"",startOfSpeech:!1,endOfSpeech:!0,correlationId:this.correlationId};yield this.signallingClient.sendTalkMessage(e),this.state=r.TalkMessageStreamState.ENDED,this.onDeactivate()}))}streamMessageChunk(e,t){return i(this,void 0,void 0,(function*(){if(this.state!==r.TalkMessageStreamState.STREAMING&&this.state!==r.TalkMessageStreamState.UNSTARTED)throw new Error("Talk stream is not in an active state: "+this.state);const n={content:e,startOfSpeech:this.state===r.TalkMessageStreamState.UNSTARTED,endOfSpeech:t,correlationId:this.correlationId};this.state=t?r.TalkMessageStreamState.ENDED:r.TalkMessageStreamState.STREAMING,this.state===r.TalkMessageStreamState.ENDED&&this.onDeactivate(),yield this.signallingClient.sendTalkMessage(n)}))}getCorrelationId(){return this.correlationId}isActive(){return this.state===r.TalkMessageStreamState.STREAMING||this.state===r.TalkMessageStreamState.UNSTARTED}getState(){return this.state}}},322:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.TalkMessageStreamState=void 0,function(e){e[e.UNSTARTED=0]="UNSTARTED",e[e.STREAMING=1]="STREAMING",e[e.INTERRUPTED=2]="INTERRUPTED",e[e.ENDED=3]="ENDED"}(n||(t.TalkMessageStreamState=n={}))},878:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEvent=t.AnamEvent=void 0;var i=n(21);Object.defineProperty(t,"AnamEvent",{enumerable:!0,get:function(){return i.AnamEvent}});var s=n(645);Object.defineProperty(t,"InternalEvent",{enumerable:!0,get:function(){return s.InternalEvent}})},645:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEvent=void 0,function(e){e.WEB_SOCKET_OPEN="WEB_SOCKET_OPEN",e.SIGNAL_MESSAGE_RECEIVED="SIGNAL_MESSAGE_RECEIVED",e.WEBRTC_CHAT_MESSAGE_RECEIVED="WEBRTC_CHAT_MESSAGE_RECEIVED"}(n||(t.InternalEvent=n={}))},21:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.AnamEvent=void 0,function(e){e.MESSAGE_HISTORY_UPDATED="MESSAGE_HISTORY_UPDATED",e.MESSAGE_STREAM_EVENT_RECEIVED="MESSAGE_STREAM_EVENT_RECEIVED",e.CONNECTION_ESTABLISHED="CONNECTION_ESTABLISHED",e.CONNECTION_CLOSED="CONNECTION_CLOSED",e.INPUT_AUDIO_STREAM_STARTED="INPUT_AUDIO_STREAM_STARTED",e.VIDEO_STREAM_STARTED="VIDEO_STREAM_STARTED",e.VIDEO_PLAY_STARTED="VIDEO_PLAY_STARTED",e.AUDIO_STREAM_STARTED="AUDIO_STREAM_STARTED",e.TALK_STREAM_INTERRUPTED="TALK_STREAM_INTERRUPTED"}(n||(t.AnamEvent=n={}))},170:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEvent=t.AnamEvent=t.MessageRole=t.SignalMessageAction=void 0;var i=n(295);Object.defineProperty(t,"SignalMessageAction",{enumerable:!0,get:function(){return i.SignalMessageAction}});var s=n(822);Object.defineProperty(t,"MessageRole",{enumerable:!0,get:function(){return s.MessageRole}});var r=n(878);Object.defineProperty(t,"AnamEvent",{enumerable:!0,get:function(){return r.AnamEvent}});var o=n(878);Object.defineProperty(t,"InternalEvent",{enumerable:!0,get:function(){return o.InternalEvent}})},273:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.MessageRole=void 0,function(e){e.USER="user",e.PERSONA="persona"}(n||(t.MessageRole=n={}))},822:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MessageRole=void 0;var i=n(273);Object.defineProperty(t,"MessageRole",{enumerable:!0,get:function(){return i.MessageRole}})},656:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.SignalMessageAction=void 0,function(e){e.OFFER="offer",e.ANSWER="answer",e.ICE_CANDIDATE="icecandidate",e.END_SESSION="endsession",e.HEARTBEAT="heartbeat",e.WARNING="warning",e.TALK_STREAM_INTERRUPTED="talkinputstreaminterrupted",e.TALK_STREAM_INPUT="talkstream"}(n||(t.SignalMessageAction=n={}))},295:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.SignalMessageAction=void 0;var i=n(656);Object.defineProperty(t,"SignalMessageAction",{enumerable:!0,get:function(){return i.SignalMessageAction}})}},t={};return function n(i){var s=t[i];if(void 0!==s)return s.exports;var r=t[i]={exports:{}};return e[i].call(r.exports,r,r.exports,n),r.exports}(440)})()));