@guava-ai/guava-sdk 0.38.0 → 0.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -57,8 +57,8 @@ You can also load a prebuilt bundle from a CDN.
57
57
 
58
58
  ```html
59
59
  <script
60
- src="https://cdn.jsdelivr.net/npm/@guava-ai/guava-sdk@0.38.0/dist/guava-webrtc.min.js"
61
- integrity="sha384-6MN0aBFnGuNmEmoeTtbYR4z0Irx2sJ26LYWjUOUyrKlAnAugcZRQOIpnpNz3zAo5"
60
+ src="https://cdn.jsdelivr.net/npm/@guava-ai/guava-sdk@0.39.0/dist/guava-webrtc.min.js"
61
+ integrity="sha384-1Y1pGUhd4tRQdEQaEUk/2nx9w2Mvl2EEXIZiTw4YFGfUSgp5o9hMJS8UibgJXrNp"
62
62
  crossorigin="anonymous"
63
63
  ></script>
64
64
  <script>
@@ -73,12 +73,12 @@ Or as an ES module:
73
73
  <script type="importmap">
74
74
  {
75
75
  "integrity": {
76
- "https://cdn.jsdelivr.net/npm/@guava-ai/guava-sdk@0.38.0/dist/guava-webrtc.esm.min.js": "sha384-qnseWbldkzYVfnszRGXZuEDW2mg62vnYWLJpAk8wjfFdt+g0qBhvU4b1wnYVvkMJ"
76
+ "https://cdn.jsdelivr.net/npm/@guava-ai/guava-sdk@0.39.0/dist/guava-webrtc.esm.min.js": "sha384-jtuG1DKqz8m5JdMoUZp4jTbGTZTrqJqWqf3tVxsWHqG08UPs7lU9EC4AZkdIzzp0"
77
77
  }
78
78
  }
79
79
  </script>
80
80
  <script type="module">
81
- import * as Guava from "https://cdn.jsdelivr.net/npm/@guava-ai/guava-sdk@0.38.0/dist/guava-webrtc.esm.min.js";
81
+ import * as Guava from "https://cdn.jsdelivr.net/npm/@guava-ai/guava-sdk@0.39.0/dist/guava-webrtc.esm.min.js";
82
82
  const call = new Guava.Call({ webrtcCode: "..." });
83
83
  await call.start();
84
84
  </script>
@@ -1,2 +1,2 @@
1
- var m=Object.defineProperty;var v=(s,e,t)=>e in s?m(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var n=(s,e,t)=>v(s,typeof e!="symbol"?e+"":e,t);function l(s,e){return s*(60/e)}var h=class{constructor(){n(this,"context",null);n(this,"intervalId",null);n(this,"volume",.15);n(this,"onSeconds",2);n(this,"periodSeconds",6);n(this,"startDelay",.3)}start(){if(this.context)return;let e=new AudioContext,t=e.createGain();t.gain.value=0,t.connect(e.destination);for(let a of[440,480]){let c=e.createOscillator();c.frequency.value=a,c.connect(t),c.start()}let i=e.currentTime+this.startDelay,r=()=>{t.gain.setValueAtTime(this.volume,i),t.gain.setValueAtTime(0,i+this.onSeconds),i+=this.periodSeconds};r(),this.intervalId=setInterval(r,this.periodSeconds*1e3),this.context=e}stop(){this.context&&(this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null),this.context.close(),this.context=null)}},d=class{constructor(){n(this,"context",null);n(this,"masterGain",null);n(this,"intervalId",null);n(this,"running",!1);n(this,"pulseCount",0);n(this,"nextBarStart",0);n(this,"volume",.25);n(this,"bpm",120);n(this,"beatsPerBar",4);n(this,"startDelay",.3)}start(){if(this.running)return;this.running=!0,this.pulseCount=0,this.context=new AudioContext,this.masterGain=this.context.createGain(),this.masterGain.gain.value=this.volume,this.masterGain.connect(this.context.destination),this.nextBarStart=this.context.currentTime+this.startDelay,this.playPulse(),this.playPulse();let e=l(this.beatsPerBar,this.bpm);this.intervalId=setInterval(()=>{this.playPulse()},e*1e3)}noteToFrequency(e){let t=e.match(/^([A-G])(#|b)?(-?\d+)$/);if(!t)throw new Error(`Invalid note: ${e}`);let[,i,r,a]=t,c={C:0,D:2,E:4,F:5,G:7,A:9,B:11},u=r==="#"?1:r==="b"?-1:0,_=(Number(a)+1)*12+c[i]+u;return 440*Math.pow(2,(_-69)/12)}stop(){if(!this.running)return;this.running=!1,this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null);let e=this.context,t=this.masterGain;if(!e||!t)return;let i=e.currentTime;t.gain.setValueAtTime(t.gain.value,i),t.gain.linearRampToValueAtTime(0,i+.15),setTimeout(()=>{e.close()},200),this.context=null,this.masterGain=null}playPulse(){if(!this.context||!this.masterGain)return;let e=this.nextBarStart,t=r=>e+l(r,this.bpm),i=l(1,this.bpm);this.pulseCount%4===0||this.pulseCount%4===2?(this.playNote("C5",t(0),i),this.playNote("E5",t(.5),i),this.playNote("G4",t(1),i)):this.pulseCount%4===3?(this.playNote("E5",t(0),i),this.playNote("G5",t(.5),i),this.playNote("C5",t(1),i)):(this.playNote("E5",t(0),i),this.playNote("G4",t(.5),i),this.playNote("C5",t(1),i)),this.pulseCount++,this.nextBarStart=t(this.beatsPerBar)}playNote(e,t,i){if(!this.context||!this.masterGain)return;let r=this.context.createOscillator(),a=this.context.createGain();r.type="sine",r.frequency.value=this.noteToFrequency(e),a.gain.setValueAtTime(0,t),a.gain.linearRampToValueAtTime(.7,t+.05),a.gain.exponentialRampToValueAtTime(.001,t+i),r.connect(a),a.connect(this.masterGain),r.start(t),r.stop(t+i+.01),r.onended=()=>{r.disconnect(),a.disconnect()}}};var o=class extends Error{constructor(t,i,r){super(i,r);n(this,"code");this.name="CallError",this.code=t}},C=[{urls:["stun:stun.l.google.com:19302","stun:stun.sipgate.net:10000"]}],f={"http:":"ws:","https:":"wss:","ws:":"ws:","wss:":"wss:"},p=class{constructor({webrtcCode:e,baseUrl:t="https://app.goguava.ai/",connectingTone:i=new d}){n(this,"_state","not-started");n(this,"_error");n(this,"_stateHandlers",new Set);n(this,"_remoteStreamHandlers",new Set);n(this,"_webrtcCode");n(this,"_sockUrl");n(this,"_remoteAudio");n(this,"_connectingTone");n(this,"_sock");n(this,"_localStream");n(this,"_peerConnection");n(this,"_remoteStream");n(this,"_iceServers");n(this,"_pendingStart");this._webrtcCode=e,i!=="disabled"&&(this._connectingTone=i),this._sockUrl=new URL("/webrtc/",t);let r=f[this._sockUrl.protocol];if(r===void 0)throw new Error(`Unsupported baseUrl protocol: ${this._sockUrl.protocol}`);this._sockUrl.protocol=r,this._remoteAudio=document.createElement("audio"),this._remoteAudio.autoplay=!0}get state(){return this._state}get error(){return this._error}get remoteStream(){return this._remoteStream}onStateChange(e){return this._stateHandlers.add(e),()=>{this._stateHandlers.delete(e)}}onRemoteStream(e){return this._remoteStreamHandlers.add(e),()=>{this._remoteStreamHandlers.delete(e)}}_setState(e){if(e!==this._state){this._state=e,e==="connecting"?this._connectingTone?.start():this._connectingTone?.stop();for(let t of this._stateHandlers)t(e)}}async start(){if(this._state!=="not-started")throw new Error("start() was already called.");this._setState("starting"),document.body.appendChild(this._remoteAudio);let e=setTimeout(()=>{this._setState("waiting-for-permission")},500);try{this._localStream=await navigator.mediaDevices.getUserMedia({audio:!0,video:!1})}catch(i){let r=new o("MICROPHONE_ACCESS_FAILED","Failed to access microphone.",{cause:i});throw this._end(r),r}finally{clearTimeout(e)}if(this.state==="ended"){this._teardown();return}this._setState("connecting");let t=new Promise((i,r)=>{this._pendingStart={resolve:i,reject:r}});this._sock=new WebSocket(this._sockUrl),this._sock.onopen=()=>{this._send({type:"auth",body:this._webrtcCode})},this._sock.onmessage=i=>{this._onMessage(JSON.parse(i.data))},this._sock.onclose=()=>{this._end(new o("CONNECTION_FAILED","Signaling connection closed."))},await t}async hangup(){if(this._state==="not-started")throw new Error("hangup() called before start().");this._state!=="ended"&&(this._send({type:"hangup"}),this._end())}_send(e){this._sock!==void 0&&this._sock.readyState===WebSocket.OPEN&&this._sock.send(JSON.stringify(e))}_onMessage(e){switch(e.type){case"auth":if(e.body!=="OK"){this._end(new o("AUTHENTICATION_FAILED",e.reason||"Authentication failed."));return}this._iceServers=e.ice_servers,this._send({type:"connect",body:{}}),this._connectPeer();break;case"desc":if(!this._peerConnection){this._end(new o("PROTOCOL_ERROR","Received desc before the peer connection was created."));return}this._peerConnection.setRemoteDescription(e.body).catch(t=>{this._end(new o("CONNECTION_FAILED","Failed to set remote description.",{cause:t}))});break;case"candidate":if(!this._peerConnection){this._end(new o("PROTOCOL_ERROR","Received candidate before the peer connection was created."));return}this._peerConnection.addIceCandidate(e.body).catch(t=>{console.warn("Failed to add ICE candidate:",t)});break;case"hangup":this._state==="connected"?this._end():this._end(new o("REMOTE_HANGUP","The remote hung up before the call connected."));break;case"error":this._end(new o("SERVER_ERROR",e.body||"Unknown server error."));break;case"transfer":this._end(new o("PROTOCOL_ERROR","Transfers are not supported."));break;default:console.warn("Unknown signaling message:",e)}}async _connectPeer(){if(this._localStream){this._peerConnection=new RTCPeerConnection({iceServers:this._iceServers??C}),this._peerConnection.onicecandidate=e=>{e.candidate&&this._send({type:"candidate",body:e.candidate})},this._peerConnection.ontrack=e=>{this._remoteStream=new MediaStream([e.track]),this._remoteAudio.srcObject=this._remoteStream;for(let t of this._remoteStreamHandlers)t(this._remoteStream)},this._peerConnection.onconnectionstatechange=()=>{this._peerConnection!==void 0&&(this._peerConnection.connectionState==="connected"?(this._setState("connected"),this._pendingStart!==void 0&&(this._pendingStart.resolve(),this._pendingStart=void 0)):this._peerConnection.connectionState==="failed"&&this._end(new o("CONNECTION_FAILED","Peer connection failed.")))};for(let e of this._localStream.getAudioTracks())this._peerConnection.addTrack(e);try{await this._peerConnection.setLocalDescription()}catch(e){this._end(new o("CONNECTION_FAILED","Failed to create offer.",{cause:e}));return}this.state!=="ended"&&this._send({type:"desc",body:this._peerConnection.localDescription})}}_end(e){this._state!=="ended"&&(this._error=e,this._teardown(),this._setState("ended"),this._pendingStart!==void 0&&(e?this._pendingStart.reject(e):this._pendingStart.resolve(),this._pendingStart=void 0))}_teardown(){this._sock&&(this._sock.onclose=null,this._sock.close(),this._sock=void 0),this._peerConnection!==void 0&&(this._peerConnection.close(),this._peerConnection=void 0);for(let e of this._localStream?.getTracks()??[])e.stop();this._localStream=void 0,this._remoteStream=void 0,this._remoteAudio.srcObject=null,this._remoteAudio.remove()}};export{p as Call,o as CallError,d as JingleTone,h as RingbackTone};
1
+ var f=Object.defineProperty;var S=(a,e,t)=>e in a?f(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var i=(a,e,t)=>S(a,typeof e!="symbol"?e+"":e,t);function _(a,e){return a*(60/e)}var u=class{constructor(){i(this,"context",null);i(this,"intervalId",null);i(this,"volume",.15);i(this,"onSeconds",2);i(this,"periodSeconds",6);i(this,"startDelay",.3)}start(){if(this.context)return;let e=new AudioContext,t=e.createGain();t.gain.value=0,t.connect(e.destination);for(let o of[440,480]){let d=e.createOscillator();d.frequency.value=o,d.connect(t),d.start()}let n=e.currentTime+this.startDelay,s=()=>{t.gain.setValueAtTime(this.volume,n),t.gain.setValueAtTime(0,n+this.onSeconds),n+=this.periodSeconds};s(),this.intervalId=setInterval(s,this.periodSeconds*1e3),this.context=e}stop(){this.context&&(this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null),this.context.close(),this.context=null)}},h=class{constructor(){i(this,"context",null);i(this,"masterGain",null);i(this,"intervalId",null);i(this,"running",!1);i(this,"pulseCount",0);i(this,"nextBarStart",0);i(this,"volume",.3);i(this,"bpm",120);i(this,"beatsPerBar",4);i(this,"startDelay",.3)}start(){if(this.running)return;this.running=!0,this.pulseCount=0,this.context=new AudioContext,this.masterGain=this.context.createGain(),this.masterGain.gain.value=this.volume,this.masterGain.connect(this.context.destination),this.nextBarStart=this.context.currentTime+this.startDelay,this.playPulse(),this.playPulse();let e=_(this.beatsPerBar,this.bpm);this.intervalId=setInterval(()=>{this.playPulse()},e*1e3)}noteToFrequency(e){let t=e.match(/^([A-G])(#|b)?(-?\d+)$/);if(!t)throw new Error(`Invalid note: ${e}`);let[,n,s,o]=t,d={C:0,D:2,E:4,F:5,G:7,A:9,B:11},c=s==="#"?1:s==="b"?-1:0,l=(Number(o)+1)*12+d[n]+c;return 440*Math.pow(2,(l-69)/12)}stop(){if(!this.running)return;this.running=!1,this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null);let e=this.context,t=this.masterGain;if(!e||!t)return;let n=e.currentTime;t.gain.setValueAtTime(t.gain.value,n),t.gain.linearRampToValueAtTime(0,n+.15),setTimeout(()=>{e.close()},200),this.context=null,this.masterGain=null}playPulse(){if(!this.context||!this.masterGain)return;let e=this.nextBarStart,t=s=>e+_(s,this.bpm),n=_(1,this.bpm);this.pulseCount%4===0||this.pulseCount%4===2?(this.playNote("C5",t(0),n),this.playNote("E5",t(.5),n),this.playNote("G4",t(1),n)):this.pulseCount%4===3?(this.playNote("E5",t(0),n),this.playNote("G5",t(.5),n),this.playNote("C5",t(1),n)):(this.playNote("E5",t(0),n),this.playNote("G4",t(.5),n),this.playNote("C5",t(1),n)),this.pulseCount++,this.nextBarStart=t(this.beatsPerBar)}playNote(e,t,n){if(!this.context||!this.masterGain)return;let s=this.context.createOscillator(),o=this.context.createGain();s.type="sine",s.frequency.value=this.noteToFrequency(e),o.gain.setValueAtTime(0,t),o.gain.linearRampToValueAtTime(.7,t+.05),o.gain.exponentialRampToValueAtTime(.001,t+n),s.connect(o),o.connect(this.masterGain),s.start(t),s.stop(t+n+.01),s.onended=()=>{s.disconnect(),o.disconnect()}}},p=class{play(){let e=new AudioContext;e.state==="suspended"&&e.resume();let t=e.currentTime,n=this.playNote(e,{frequency:293.665,startTime:t,attack:.065,decay:.085,volume:.2}),s=this.playNote(e,{frequency:220,startTime:t+.139,attack:.018,decay:.075,volume:.2});Promise.all([n,s]).then(()=>e.close())}playNote(e,{frequency:t,startTime:n,attack:s,decay:o,volume:d}){let c=e.createOscillator(),l=e.createGain();c.type="sine",c.frequency.setValueAtTime(t,n),l.gain.setValueAtTime(0,n),l.gain.linearRampToValueAtTime(d,n+s);let m=n+s,v=m+o*5;return l.gain.setTargetAtTime(1e-5,m,o),c.connect(l),l.connect(e.destination),c.start(n),c.stop(v),new Promise(y=>{c.onended=()=>{c.disconnect(),l.disconnect(),y()}})}};var r=class extends Error{constructor(t,n,s){super(n,s);i(this,"code");this.name="CallError",this.code=t}},C=[{urls:["stun:stun.l.google.com:19302","stun:stun.sipgate.net:10000"]}],T={"http:":"ws:","https:":"wss:","ws:":"ws:","wss:":"wss:"},g=class{constructor({webrtcCode:e,baseUrl:t="https://app.goguava.ai/",connectingTone:n=new h,leavingTone:s=new p,connectionTimeoutMs:o=3e4,customPayload:d={}}){i(this,"_state","not-started");i(this,"_error");i(this,"_stateHandlers",new Set);i(this,"_remoteStreamHandlers",new Set);i(this,"_messageHandlers",new Set);i(this,"_webrtcCode");i(this,"_sockUrl");i(this,"_remoteAudio");i(this,"_connectingTone");i(this,"_leavingTone");i(this,"_connectionTimeoutMs");i(this,"_customPayload");i(this,"_connectionTimer");i(this,"_sock");i(this,"_chatSockUrl");i(this,"_chatSessionId",crypto.randomUUID());i(this,"_chatSock");i(this,"_chatReady",!1);i(this,"_pendingChatMessages",[]);i(this,"_localStream");i(this,"_peerConnection");i(this,"_remoteStream");i(this,"_iceServers");i(this,"_pendingStart");this._webrtcCode=e,this._customPayload=d,n!=="disabled"&&(this._connectingTone=n),s!=="disabled"&&(this._leavingTone=s),o!=="disabled"&&(this._connectionTimeoutMs=o),this._sockUrl=new URL("/webrtc/",t);let c=T[this._sockUrl.protocol];if(c===void 0)throw new Error(`Unsupported baseUrl protocol: ${this._sockUrl.protocol}`);this._sockUrl.protocol=c,this._chatSockUrl=new URL("/webrtc-chat/",this._sockUrl),this._remoteAudio=document.createElement("audio"),this._remoteAudio.autoplay=!0}get state(){return this._state}get error(){return this._error}get remoteStream(){return this._remoteStream}onStateChange(e){return this._stateHandlers.add(e),()=>{this._stateHandlers.delete(e)}}onRemoteStream(e){return this._remoteStreamHandlers.add(e),()=>{this._remoteStreamHandlers.delete(e)}}onMessage(e){return this._messageHandlers.add(e),()=>{this._messageHandlers.delete(e)}}_setState(e){if(e!==this._state){e==="ended"&&this._leavingTone?.play(),this._state=e,e==="connecting"?(this._connectingTone?.start(),this._connectionTimeoutMs!==void 0&&(this._connectionTimer=setTimeout(()=>{this._send({type:"hangup"}),this._end(new r("CONNECTION_TIMEOUT","Timed out connecting the call."))},this._connectionTimeoutMs))):(this._connectingTone?.stop(),clearTimeout(this._connectionTimer),this._connectionTimer=void 0);for(let t of this._stateHandlers)t(e)}}async start(){if(this._state!=="not-started")throw new Error("start() was already called.");this._setState("starting"),document.body.appendChild(this._remoteAudio);let e=setTimeout(()=>{this._setState("waiting-for-permission")},500);try{this._localStream=await navigator.mediaDevices.getUserMedia({audio:!0,video:!1})}catch(n){let s=new r("MICROPHONE_ACCESS_FAILED","Failed to access microphone.",{cause:n});throw this._end(s),s}finally{clearTimeout(e)}if(this.state==="ended"){this._teardown();return}this._setState("connecting");let t=new Promise((n,s)=>{this._pendingStart={resolve:n,reject:s}});this._sock=new WebSocket(this._sockUrl),this._sock.onopen=()=>{this._send({type:"auth",body:JSON.stringify({webrtc_code:this._webrtcCode,chat_session_id:this._chatSessionId})})},this._sock.onmessage=n=>{this._onMessage(JSON.parse(n.data))},this._sock.onclose=()=>{this._end(new r("CONNECTION_FAILED","Signaling connection closed."))},await t}sendMessage(e){if(this._state==="not-started"||this._state==="ended")throw new Error(`sendMessage() called while the call is ${this._state}.`);let t=JSON.stringify({type:"webrtc-message",payload:e});this._chatReady&&this._chatSock!==void 0?this._chatSock.send(t):this._pendingChatMessages.push(t)}_openChatSocket(){this._chatSock=new WebSocket(this._chatSockUrl),this._chatSock.onopen=()=>{this._chatSock?.send(JSON.stringify({webrtc_code:this._webrtcCode,chat_session_id:this._chatSessionId}))},this._chatSock.onmessage=e=>{let t=JSON.parse(e.data);if(t.type==="ready"&&!this._chatReady){this._chatReady=!0;for(let n of this._pendingChatMessages)this._chatSock?.send(n);this._pendingChatMessages=[]}else if(t.command_type==="send-webrtc-message")for(let n of this._messageHandlers)n(t.payload??{})}}async hangup(){if(this._state==="not-started")throw new Error("hangup() called before start().");this._state!=="ended"&&(this._send({type:"hangup"}),this._end())}_send(e){this._sock!==void 0&&this._sock.readyState===WebSocket.OPEN&&this._sock.send(JSON.stringify(e))}_onMessage(e){switch(e.type){case"auth":if(e.body!=="OK"){this._end(new r("AUTHENTICATION_FAILED",e.reason||"Authentication failed."));return}this._iceServers=e.ice_servers,this._openChatSocket(),this._send({type:"connect",body:{custom_payload:this._customPayload}}),this._connectPeer();break;case"desc":if(!this._peerConnection){this._end(new r("PROTOCOL_ERROR","Received desc before the peer connection was created."));return}this._peerConnection.setRemoteDescription(e.body).catch(t=>{this._end(new r("CONNECTION_FAILED","Failed to set remote description.",{cause:t}))});break;case"candidate":if(!this._peerConnection){this._end(new r("PROTOCOL_ERROR","Received candidate before the peer connection was created."));return}this._peerConnection.addIceCandidate(e.body).catch(t=>{console.warn("Failed to add ICE candidate:",t)});break;case"hangup":this._state==="connected"?this._end():this._end(new r("REMOTE_HANGUP","The remote hung up before the call connected."));break;case"error":this._end(new r("SERVER_ERROR",e.body||"Unknown server error."));break;case"transfer":this._end(new r("PROTOCOL_ERROR","Transfers are not supported."));break;default:console.warn("Unknown signaling message:",e)}}async _connectPeer(){if(this._localStream){this._peerConnection=new RTCPeerConnection({iceServers:this._iceServers??C}),this._peerConnection.onicecandidate=e=>{e.candidate&&this._send({type:"candidate",body:e.candidate})},this._peerConnection.ontrack=e=>{this._remoteStream=new MediaStream([e.track]),this._remoteAudio.srcObject=this._remoteStream;for(let t of this._remoteStreamHandlers)t(this._remoteStream)},this._peerConnection.onconnectionstatechange=()=>{this._peerConnection!==void 0&&(this._peerConnection.connectionState==="connected"?(this._setState("connected"),this._pendingStart!==void 0&&(this._pendingStart.resolve(),this._pendingStart=void 0)):this._peerConnection.connectionState==="failed"&&this._end(new r("CONNECTION_FAILED","Peer connection failed.")))};for(let e of this._localStream.getAudioTracks())this._peerConnection.addTrack(e);try{await this._peerConnection.setLocalDescription()}catch(e){this._end(new r("CONNECTION_FAILED","Failed to create offer.",{cause:e}));return}this.state!=="ended"&&this._send({type:"desc",body:this._peerConnection.localDescription})}}_end(e){this._state!=="ended"&&(this._error=e,this._teardown(),this._setState("ended"),this._pendingStart!==void 0&&(e?this._pendingStart.reject(e):this._pendingStart.resolve(),this._pendingStart=void 0))}_teardown(){this._sock&&(this._sock.onclose=null,this._sock.close(),this._sock=void 0),this._chatSock&&(this._chatSock.close(),this._chatSock=void 0),this._chatReady=!1,this._pendingChatMessages=[],this._peerConnection!==void 0&&(this._peerConnection.close(),this._peerConnection=void 0);for(let e of this._localStream?.getTracks()??[])e.stop();this._localStream=void 0,this._remoteStream=void 0,this._remoteAudio.srcObject=null,this._remoteAudio.remove()}};export{g as Call,r as CallError,h as JingleTone,p as LeavingTone,u as RingbackTone};
2
2
  //# sourceMappingURL=guava-webrtc.esm.min.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/webrtc/tones.ts", "../src/webrtc/index.ts"],
4
- "sourcesContent": ["// Seconds from the start of bar 0 to the given 0-indexed beat.\nfunction beatTime(beat: number, bpm: number): number {\n return beat * (60 / bpm);\n}\n\nexport interface ConnectingTone {\n start(): void;\n stop(): void;\n}\n\n// North American ringback: 440 + 480 Hz, 2s on, 4s off.\nexport class RingbackTone implements ConnectingTone {\n private context: AudioContext | null = null;\n private intervalId: ReturnType<typeof setInterval> | null = null;\n\n private readonly volume = 0.15;\n private readonly onSeconds = 2;\n private readonly periodSeconds = 6;\n // Delay before the first ring, so near-instant connections stay silent.\n private readonly startDelay = 0.3;\n\n start(): void {\n if (this.context) return;\n\n const context = new AudioContext();\n const gain = context.createGain();\n gain.gain.value = 0;\n gain.connect(context.destination);\n\n for (const frequency of [440, 480]) {\n const oscillator = context.createOscillator();\n oscillator.frequency.value = frequency;\n oscillator.connect(gain);\n oscillator.start();\n }\n\n let nextRing = context.currentTime + this.startDelay;\n const ring = () => {\n gain.gain.setValueAtTime(this.volume, nextRing);\n gain.gain.setValueAtTime(0, nextRing + this.onSeconds);\n nextRing += this.periodSeconds;\n };\n ring();\n this.intervalId = setInterval(ring, this.periodSeconds * 1000);\n this.context = context;\n }\n\n stop(): void {\n if (!this.context) return;\n\n if (this.intervalId) {\n clearInterval(this.intervalId);\n this.intervalId = null;\n }\n // Closing the context stops the oscillators.\n void this.context.close();\n this.context = null;\n }\n}\n\nexport class JingleTone implements ConnectingTone {\n private context: AudioContext | null = null;\n private masterGain: GainNode | null = null;\n private intervalId: ReturnType<typeof setInterval> | null = null;\n private running = false;\n private pulseCount = 0;\n // Audio-clock time of the next bar to schedule.\n private nextBarStart = 0;\n\n // Configuration\n private readonly volume = 0.25;\n private readonly bpm = 120;\n private readonly beatsPerBar = 4;\n // Delay before the first note, so near-instant connections stay silent.\n private readonly startDelay = 0.3;\n\n start(): void {\n if (this.running) return;\n\n this.running = true;\n this.pulseCount = 0;\n this.context = new AudioContext();\n this.masterGain = this.context.createGain();\n this.masterGain.gain.value = this.volume;\n this.masterGain.connect(this.context.destination);\n this.nextBarStart = this.context.currentTime + this.startDelay;\n\n // Stay one bar ahead so timer jitter doesn't shift the beat.\n this.playPulse();\n this.playPulse();\n\n const barLength = beatTime(this.beatsPerBar, this.bpm);\n this.intervalId = setInterval(() => {\n this.playPulse();\n }, barLength * 1000);\n }\n\n private noteToFrequency(note: string): number {\n const match = note.match(/^([A-G])(#|b)?(-?\\d+)$/);\n\n if (!match) {\n throw new Error(`Invalid note: ${note}`);\n }\n\n const [, name, accidental, octave] = match;\n\n const semitones: Record<string, number> = {\n C: 0,\n D: 2,\n E: 4,\n F: 5,\n G: 7,\n A: 9,\n B: 11,\n };\n\n const offset = accidental === \"#\" ? 1 : accidental === \"b\" ? -1 : 0;\n const midi = (Number(octave) + 1) * 12 + semitones[name] + offset;\n\n return 440 * Math.pow(2, (midi - 69) / 12);\n }\n\n stop(): void {\n if (!this.running) return;\n\n this.running = false;\n\n if (this.intervalId) {\n clearInterval(this.intervalId);\n this.intervalId = null;\n }\n\n const context = this.context;\n const gain = this.masterGain;\n\n if (!context || !gain) return;\n\n // Smooth fade-out\n const now = context.currentTime;\n gain.gain.setValueAtTime(gain.gain.value, now);\n gain.gain.linearRampToValueAtTime(0, now + 0.15);\n\n setTimeout(() => {\n void context.close();\n }, 200);\n\n this.context = null;\n this.masterGain = null;\n }\n\n private playPulse(): void {\n if (!this.context || !this.masterGain) return;\n\n const bar = this.nextBarStart;\n const beat = (n: number) => bar + beatTime(n, this.bpm);\n const beatLength = beatTime(1, this.bpm);\n\n // Notes on beats 1-3, silence on beat 4.\n if (this.pulseCount % 4 === 0 || this.pulseCount % 4 === 2) {\n this.playNote(\"C5\", beat(0), beatLength);\n this.playNote(\"E5\", beat(0.5), beatLength);\n this.playNote(\"G4\", beat(1), beatLength);\n } else if (this.pulseCount % 4 === 3) {\n this.playNote(\"E5\", beat(0), beatLength);\n this.playNote(\"G5\", beat(0.5), beatLength);\n this.playNote(\"C5\", beat(1), beatLength);\n } else {\n this.playNote(\"E5\", beat(0), beatLength);\n this.playNote(\"G4\", beat(0.5), beatLength);\n this.playNote(\"C5\", beat(1), beatLength);\n }\n this.pulseCount++;\n this.nextBarStart = beat(this.beatsPerBar);\n }\n\n private playNote(note: string, startTime: number, duration: number): void {\n if (!this.context || !this.masterGain) return;\n\n const oscillator = this.context.createOscillator();\n const envelope = this.context.createGain();\n\n oscillator.type = \"sine\";\n oscillator.frequency.value = this.noteToFrequency(note);\n\n envelope.gain.setValueAtTime(0, startTime);\n envelope.gain.linearRampToValueAtTime(0.7, startTime + 0.05);\n envelope.gain.exponentialRampToValueAtTime(0.001, startTime + duration);\n\n oscillator.connect(envelope);\n envelope.connect(this.masterGain);\n\n oscillator.start(startTime);\n oscillator.stop(startTime + duration + 0.01);\n\n oscillator.onended = () => {\n oscillator.disconnect();\n envelope.disconnect();\n };\n }\n}\n", "import { type ConnectingTone, JingleTone } from \"./tones.ts\";\n\nexport { type ConnectingTone, JingleTone, RingbackTone } from \"./tones.ts\";\n\nexport type CallState =\n | \"not-started\" // start() hasn't been called yet.\n | \"starting\" // Starting the call. Do not play a Ringback yet.\n | \"waiting-for-permission\" // We're waiting for the Microphone permission prompt. You may show a reminder UI here.\n | \"connecting\" // The call is now connection to the remote. This is where you can play a ringback or other connecting tone.\n | \"connected\" // The call is now connected and the audio channel is established.\n | \"ended\"; // The call has ended.\n\nexport type CallErrorCode =\n | \"MICROPHONE_ACCESS_FAILED\"\n | \"CONNECTION_FAILED\"\n | \"AUTHENTICATION_FAILED\"\n | \"PROTOCOL_ERROR\" // The server sent an unexpected message.\n | \"SERVER_ERROR\" // The server sent an error message.\n | \"REMOTE_HANGUP\"; // The remote hung up before the call connected.\n\nexport class CallError extends Error {\n readonly code: CallErrorCode;\n\n constructor(code: CallErrorCode, message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"CallError\";\n this.code = code;\n }\n}\n\n// Used if the server doesn't send ICE servers with the auth reply.\nconst DEFAULT_ICE_SERVERS: RTCIceServer[] = [\n { urls: [\"stun:stun.l.google.com:19302\", \"stun:stun.sipgate.net:10000\"] },\n];\n\nconst SOCKET_PROTOCOLS: Record<string, string> = {\n \"http:\": \"ws:\",\n \"https:\": \"wss:\",\n \"ws:\": \"ws:\",\n \"wss:\": \"wss:\",\n};\n\n// Messages received on the signaling socket. See junkswitch/junkswitch/webrtcsocket.py.\ntype SignalingMessage =\n | {\n type: \"auth\";\n body: \"OK\" | \"FAIL\";\n reason?: string;\n ice_servers?: RTCIceServer[];\n }\n | { type: \"desc\"; body: RTCSessionDescriptionInit }\n | { type: \"candidate\"; body: RTCIceCandidateInit }\n | { type: \"hangup\" }\n | { type: \"error\"; body?: string }\n | { type: \"transfer\" };\n\nexport class Call {\n private _state: CallState = \"not-started\";\n private _error?: CallError;\n private readonly _stateHandlers = new Set<(state: CallState) => void>();\n private readonly _remoteStreamHandlers = new Set<(stream: MediaStream) => void>();\n private readonly _webrtcCode: string;\n private readonly _sockUrl: URL;\n private readonly _remoteAudio: HTMLAudioElement;\n private readonly _connectingTone?: ConnectingTone;\n private _sock?: WebSocket;\n private _localStream?: MediaStream;\n private _peerConnection?: RTCPeerConnection;\n private _remoteStream?: MediaStream;\n private _iceServers?: RTCIceServer[];\n // Settles the promise returned by start().\n private _pendingStart?: { resolve: () => void; reject: (error: CallError) => void };\n\n constructor({\n webrtcCode,\n baseUrl = \"https://app.goguava.ai/\",\n connectingTone = new JingleTone(),\n }: {\n webrtcCode: string;\n baseUrl?: string;\n // Played while the call is in the \"connecting\" state.\n connectingTone?: ConnectingTone | \"disabled\";\n }) {\n this._webrtcCode = webrtcCode;\n if (connectingTone !== \"disabled\") {\n this._connectingTone = connectingTone;\n }\n\n this._sockUrl = new URL(\"/webrtc/\", baseUrl);\n const sockProtocol = SOCKET_PROTOCOLS[this._sockUrl.protocol];\n if (sockProtocol === undefined) {\n throw new Error(`Unsupported baseUrl protocol: ${this._sockUrl.protocol}`);\n }\n this._sockUrl.protocol = sockProtocol;\n\n this._remoteAudio = document.createElement(\"audio\");\n this._remoteAudio.autoplay = true;\n }\n\n get state(): CallState {\n return this._state;\n }\n\n // Set if the call ended due to a failure.\n get error(): CallError | undefined {\n return this._error;\n }\n\n // Set once the remote audio track arrives, which happens before the call reaches \"connected\".\n get remoteStream(): MediaStream | undefined {\n return this._remoteStream;\n }\n\n // Returns a function that unregisters the handler.\n onStateChange(handler: (state: CallState) => void): () => void {\n this._stateHandlers.add(handler);\n return () => {\n this._stateHandlers.delete(handler);\n };\n }\n\n // Fires whenever the remote stream is replaced, including the first time it arrives.\n // Returns a function that unregisters the handler.\n onRemoteStream(handler: (stream: MediaStream) => void): () => void {\n this._remoteStreamHandlers.add(handler);\n return () => {\n this._remoteStreamHandlers.delete(handler);\n };\n }\n\n private _setState(state: CallState) {\n if (state === this._state) {\n return;\n }\n this._state = state;\n if (state === \"connecting\") {\n this._connectingTone?.start();\n } else {\n this._connectingTone?.stop();\n }\n for (const handler of this._stateHandlers) {\n handler(state);\n }\n }\n\n async start() {\n if (this._state !== \"not-started\") {\n throw new Error(`start() was already called.`);\n }\n this._setState(\"starting\");\n\n // Add the audio element.\n document.body.appendChild(this._remoteAudio);\n\n // We start by requesting device permissions first. The idea is that, after permission has been granted,\n // it makes sence to play a ringback, but it doesn't make sense to do so during the permission prompt.\n\n const permissionTimer = setTimeout(() => {\n this._setState(\"waiting-for-permission\");\n }, 500);\n\n try {\n this._localStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });\n } catch (error) {\n const callError = new CallError(\"MICROPHONE_ACCESS_FAILED\", \"Failed to access microphone.\", {\n cause: error,\n });\n this._end(callError);\n throw callError;\n } finally {\n clearTimeout(permissionTimer);\n }\n\n // hangup() was called during the permission prompt.\n if (this.state === \"ended\") {\n this._teardown();\n return;\n }\n\n // Now open and authenticate the signaling socket.\n this._setState(\"connecting\"); // UI can start playing rinback tones here.\n const started = new Promise<void>((resolve, reject) => {\n this._pendingStart = { resolve, reject };\n });\n\n this._sock = new WebSocket(this._sockUrl);\n this._sock.onopen = () => {\n this._send({ type: \"auth\", body: this._webrtcCode });\n };\n this._sock.onmessage = (event) => {\n this._onMessage(JSON.parse(event.data) as SignalingMessage);\n };\n this._sock.onclose = () => {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Signaling connection closed.\"));\n };\n\n await started;\n }\n\n async hangup() {\n if (this._state === \"not-started\") {\n throw new Error(`hangup() called before start().`);\n }\n if (this._state === \"ended\") {\n return;\n }\n this._send({ type: \"hangup\" });\n this._end();\n }\n\n private _send(message: { type: string; body?: unknown }) {\n if (this._sock !== undefined && this._sock.readyState === WebSocket.OPEN) {\n this._sock.send(JSON.stringify(message));\n }\n }\n\n private _onMessage(message: SignalingMessage) {\n switch (message.type) {\n case \"auth\":\n if (message.body !== \"OK\") {\n this._end(\n new CallError(\"AUTHENTICATION_FAILED\", message.reason || \"Authentication failed.\"),\n );\n return;\n }\n this._iceServers = message.ice_servers;\n this._send({ type: \"connect\", body: {} });\n void this._connectPeer();\n break;\n case \"desc\":\n if (!this._peerConnection) {\n this._end(\n new CallError(\n \"PROTOCOL_ERROR\",\n \"Received desc before the peer connection was created.\",\n ),\n );\n return;\n }\n this._peerConnection.setRemoteDescription(message.body).catch((error) => {\n this._end(\n new CallError(\"CONNECTION_FAILED\", \"Failed to set remote description.\", {\n cause: error,\n }),\n );\n });\n break;\n case \"candidate\":\n if (!this._peerConnection) {\n this._end(\n new CallError(\n \"PROTOCOL_ERROR\",\n \"Received candidate before the peer connection was created.\",\n ),\n );\n return;\n }\n this._peerConnection.addIceCandidate(message.body).catch((error) => {\n console.warn(\"Failed to add ICE candidate:\", error);\n });\n break;\n case \"hangup\":\n if (this._state === \"connected\") {\n this._end();\n } else {\n this._end(\n new CallError(\"REMOTE_HANGUP\", \"The remote hung up before the call connected.\"),\n );\n }\n break;\n case \"error\":\n this._end(new CallError(\"SERVER_ERROR\", message.body || \"Unknown server error.\"));\n break;\n case \"transfer\":\n this._end(new CallError(\"PROTOCOL_ERROR\", \"Transfers are not supported.\"));\n break;\n default:\n console.warn(\"Unknown signaling message:\", message);\n }\n }\n\n private async _connectPeer() {\n if (!this._localStream) {\n return;\n }\n this._peerConnection = new RTCPeerConnection({\n iceServers: this._iceServers ?? DEFAULT_ICE_SERVERS,\n });\n this._peerConnection.onicecandidate = (event) => {\n if (event.candidate) {\n this._send({ type: \"candidate\", body: event.candidate });\n }\n };\n this._peerConnection.ontrack = (event) => {\n this._remoteStream = new MediaStream([event.track]);\n this._remoteAudio.srcObject = this._remoteStream;\n for (const handler of this._remoteStreamHandlers) {\n handler(this._remoteStream);\n }\n };\n this._peerConnection.onconnectionstatechange = () => {\n if (this._peerConnection === undefined) {\n return;\n }\n // \"disconnected\" is often transient, so wait for \"failed\" before ending the call.\n if (this._peerConnection.connectionState === \"connected\") {\n this._setState(\"connected\");\n if (this._pendingStart !== undefined) {\n this._pendingStart.resolve();\n this._pendingStart = undefined;\n }\n } else if (this._peerConnection.connectionState === \"failed\") {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Peer connection failed.\"));\n }\n };\n for (const track of this._localStream.getAudioTracks()) {\n this._peerConnection.addTrack(track);\n }\n\n try {\n await this._peerConnection.setLocalDescription();\n } catch (error) {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Failed to create offer.\", { cause: error }));\n return;\n }\n\n // The call may have ended while creating the offer.\n if (this.state === \"ended\") {\n return;\n }\n this._send({ type: \"desc\", body: this._peerConnection.localDescription });\n }\n\n private _end(error?: CallError) {\n if (this._state === \"ended\") {\n return;\n }\n this._error = error;\n this._teardown();\n this._setState(\"ended\");\n\n // If start() is still pending, it rejects on failure and resolves on hangup().\n if (this._pendingStart !== undefined) {\n if (error) {\n this._pendingStart.reject(error);\n } else {\n this._pendingStart.resolve();\n }\n this._pendingStart = undefined;\n }\n }\n\n private _teardown() {\n if (this._sock) {\n // Don't fire close handlers for a socket we closed ourselves.\n this._sock.onclose = null;\n this._sock.close();\n this._sock = undefined;\n }\n if (this._peerConnection !== undefined) {\n this._peerConnection.close();\n this._peerConnection = undefined;\n }\n for (const track of this._localStream?.getTracks() ?? []) {\n track.stop();\n }\n this._localStream = undefined;\n this._remoteStream = undefined;\n this._remoteAudio.srcObject = null;\n this._remoteAudio.remove();\n }\n}\n"],
5
- "mappings": "oKACA,SAASA,EAASC,EAAcC,EAAqB,CACnD,OAAOD,GAAQ,GAAKC,EACtB,CAQO,IAAMC,EAAN,KAA6C,CAA7C,cACLC,EAAA,KAAQ,UAA+B,MACvCA,EAAA,KAAQ,aAAoD,MAE5DA,EAAA,KAAiB,SAAS,KAC1BA,EAAA,KAAiB,YAAY,GAC7BA,EAAA,KAAiB,gBAAgB,GAEjCA,EAAA,KAAiB,aAAa,IAE9B,OAAc,CACZ,GAAI,KAAK,QAAS,OAElB,IAAMC,EAAU,IAAI,aACdC,EAAOD,EAAQ,WAAW,EAChCC,EAAK,KAAK,MAAQ,EAClBA,EAAK,QAAQD,EAAQ,WAAW,EAEhC,QAAWE,IAAa,CAAC,IAAK,GAAG,EAAG,CAClC,IAAMC,EAAaH,EAAQ,iBAAiB,EAC5CG,EAAW,UAAU,MAAQD,EAC7BC,EAAW,QAAQF,CAAI,EACvBE,EAAW,MAAM,CACnB,CAEA,IAAIC,EAAWJ,EAAQ,YAAc,KAAK,WACpCK,EAAO,IAAM,CAC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3B,KAAK,gBAAkB,QAEzB,QAAWM,KAAS,KAAK,cAAc,UAAU,GAAK,CAAC,EACrDA,EAAM,KAAK,EAEb,KAAK,aAAe,OACpB,KAAK,cAAgB,OACrB,KAAK,aAAa,UAAY,KAC9B,KAAK,aAAa,OAAO,CAC3B,CACF",
6
- "names": ["beatTime", "beat", "bpm", "RingbackTone", "__publicField", "context", "gain", "frequency", "oscillator", "nextRing", "ring", "JingleTone", "barLength", "note", "match", "name", "accidental", "octave", "semitones", "offset", "midi", "now", "bar", "n", "beatLength", "startTime", "duration", "envelope", "CallError", "code", "message", "options", "__publicField", "DEFAULT_ICE_SERVERS", "SOCKET_PROTOCOLS", "Call", "webrtcCode", "baseUrl", "connectingTone", "JingleTone", "sockProtocol", "handler", "state", "permissionTimer", "error", "callError", "started", "resolve", "reject", "event", "track"]
4
+ "sourcesContent": ["// Seconds from the start of bar 0 to the given 0-indexed beat.\nfunction beatTime(beat: number, bpm: number): number {\n return beat * (60 / bpm);\n}\n\nexport interface ConnectingTone {\n start(): void;\n stop(): void;\n}\n\n// North American ringback: 440 + 480 Hz, 2s on, 4s off.\nexport class RingbackTone implements ConnectingTone {\n private context: AudioContext | null = null;\n private intervalId: ReturnType<typeof setInterval> | null = null;\n\n private readonly volume = 0.15;\n private readonly onSeconds = 2;\n private readonly periodSeconds = 6;\n // Delay before the first ring, so near-instant connections stay silent.\n private readonly startDelay = 0.3;\n\n start(): void {\n if (this.context) return;\n\n const context = new AudioContext();\n const gain = context.createGain();\n gain.gain.value = 0;\n gain.connect(context.destination);\n\n for (const frequency of [440, 480]) {\n const oscillator = context.createOscillator();\n oscillator.frequency.value = frequency;\n oscillator.connect(gain);\n oscillator.start();\n }\n\n let nextRing = context.currentTime + this.startDelay;\n const ring = () => {\n gain.gain.setValueAtTime(this.volume, nextRing);\n gain.gain.setValueAtTime(0, nextRing + this.onSeconds);\n nextRing += this.periodSeconds;\n };\n ring();\n this.intervalId = setInterval(ring, this.periodSeconds * 1000);\n this.context = context;\n }\n\n stop(): void {\n if (!this.context) return;\n\n if (this.intervalId) {\n clearInterval(this.intervalId);\n this.intervalId = null;\n }\n // Closing the context stops the oscillators.\n void this.context.close();\n this.context = null;\n }\n}\n\nexport class JingleTone implements ConnectingTone {\n private context: AudioContext | null = null;\n private masterGain: GainNode | null = null;\n private intervalId: ReturnType<typeof setInterval> | null = null;\n private running = false;\n private pulseCount = 0;\n // Audio-clock time of the next bar to schedule.\n private nextBarStart = 0;\n\n // Configuration\n private readonly volume = 0.3;\n private readonly bpm = 120;\n private readonly beatsPerBar = 4;\n // Delay before the first note, so near-instant connections stay silent.\n private readonly startDelay = 0.3;\n\n start(): void {\n if (this.running) return;\n\n this.running = true;\n this.pulseCount = 0;\n this.context = new AudioContext();\n this.masterGain = this.context.createGain();\n this.masterGain.gain.value = this.volume;\n this.masterGain.connect(this.context.destination);\n this.nextBarStart = this.context.currentTime + this.startDelay;\n\n // Stay one bar ahead so timer jitter doesn't shift the beat.\n this.playPulse();\n this.playPulse();\n\n const barLength = beatTime(this.beatsPerBar, this.bpm);\n this.intervalId = setInterval(() => {\n this.playPulse();\n }, barLength * 1000);\n }\n\n private noteToFrequency(note: string): number {\n const match = note.match(/^([A-G])(#|b)?(-?\\d+)$/);\n\n if (!match) {\n throw new Error(`Invalid note: ${note}`);\n }\n\n const [, name, accidental, octave] = match;\n\n const semitones: Record<string, number> = {\n C: 0,\n D: 2,\n E: 4,\n F: 5,\n G: 7,\n A: 9,\n B: 11,\n };\n\n const offset = accidental === \"#\" ? 1 : accidental === \"b\" ? -1 : 0;\n const midi = (Number(octave) + 1) * 12 + semitones[name] + offset;\n\n return 440 * Math.pow(2, (midi - 69) / 12);\n }\n\n stop(): void {\n if (!this.running) return;\n\n this.running = false;\n\n if (this.intervalId) {\n clearInterval(this.intervalId);\n this.intervalId = null;\n }\n\n const context = this.context;\n const gain = this.masterGain;\n\n if (!context || !gain) return;\n\n // Smooth fade-out\n const now = context.currentTime;\n gain.gain.setValueAtTime(gain.gain.value, now);\n gain.gain.linearRampToValueAtTime(0, now + 0.15);\n\n setTimeout(() => {\n void context.close();\n }, 200);\n\n this.context = null;\n this.masterGain = null;\n }\n\n private playPulse(): void {\n if (!this.context || !this.masterGain) return;\n\n const bar = this.nextBarStart;\n const beat = (n: number) => bar + beatTime(n, this.bpm);\n const beatLength = beatTime(1, this.bpm);\n\n // Notes on beats 1-3, silence on beat 4.\n if (this.pulseCount % 4 === 0 || this.pulseCount % 4 === 2) {\n this.playNote(\"C5\", beat(0), beatLength);\n this.playNote(\"E5\", beat(0.5), beatLength);\n this.playNote(\"G4\", beat(1), beatLength);\n } else if (this.pulseCount % 4 === 3) {\n this.playNote(\"E5\", beat(0), beatLength);\n this.playNote(\"G5\", beat(0.5), beatLength);\n this.playNote(\"C5\", beat(1), beatLength);\n } else {\n this.playNote(\"E5\", beat(0), beatLength);\n this.playNote(\"G4\", beat(0.5), beatLength);\n this.playNote(\"C5\", beat(1), beatLength);\n }\n this.pulseCount++;\n this.nextBarStart = beat(this.beatsPerBar);\n }\n\n private playNote(note: string, startTime: number, duration: number): void {\n if (!this.context || !this.masterGain) return;\n\n const oscillator = this.context.createOscillator();\n const envelope = this.context.createGain();\n\n oscillator.type = \"sine\";\n oscillator.frequency.value = this.noteToFrequency(note);\n\n envelope.gain.setValueAtTime(0, startTime);\n envelope.gain.linearRampToValueAtTime(0.7, startTime + 0.05);\n envelope.gain.exponentialRampToValueAtTime(0.001, startTime + duration);\n\n oscillator.connect(envelope);\n envelope.connect(this.masterGain);\n\n oscillator.start(startTime);\n oscillator.stop(startTime + duration + 0.01);\n\n oscillator.onended = () => {\n oscillator.disconnect();\n envelope.disconnect();\n };\n }\n}\n\nexport interface Tone {\n play(): void;\n}\n\nexport class LeavingTone implements Tone {\n public play(): void {\n const ctx = new AudioContext();\n\n if (ctx.state === \"suspended\") {\n void ctx.resume();\n }\n\n const now = ctx.currentTime;\n\n // First note: D4\n const first = this.playNote(ctx, {\n frequency: 293.665,\n startTime: now,\n attack: 0.065,\n decay: 0.085,\n volume: 0.2,\n });\n\n // Second note: A3, starts 139ms later\n const second = this.playNote(ctx, {\n frequency: 220.0,\n startTime: now + 0.139,\n attack: 0.018,\n decay: 0.075,\n volume: 0.2,\n });\n\n void Promise.all([first, second]).then(() => ctx.close());\n }\n\n // Resolves once the note has finished playing.\n private playNote(\n ctx: AudioContext,\n {\n frequency,\n startTime,\n attack,\n decay,\n volume,\n }: {\n frequency: number;\n startTime: number;\n attack: number;\n decay: number;\n volume: number;\n },\n ): Promise<void> {\n const oscillator = ctx.createOscillator();\n const gain = ctx.createGain();\n\n oscillator.type = \"sine\";\n oscillator.frequency.setValueAtTime(frequency, startTime);\n\n // Smooth onset\n gain.gain.setValueAtTime(0, startTime);\n gain.gain.linearRampToValueAtTime(volume, startTime + attack);\n\n // Exponential decay\n const peakTime = startTime + attack;\n const endTime = peakTime + decay * 5;\n\n gain.gain.setTargetAtTime(0.00001, peakTime, decay);\n\n oscillator.connect(gain);\n gain.connect(ctx.destination);\n\n oscillator.start(startTime);\n oscillator.stop(endTime);\n\n return new Promise((resolve) => {\n oscillator.onended = () => {\n oscillator.disconnect();\n gain.disconnect();\n resolve();\n };\n });\n }\n}\n", "import { type ConnectingTone, JingleTone, LeavingTone, type Tone } from \"./tones.ts\";\n\nexport { type ConnectingTone, JingleTone, RingbackTone, LeavingTone, type Tone } from \"./tones.ts\";\n\nexport type CallState =\n | \"not-started\" // start() hasn't been called yet.\n | \"starting\" // Starting the call. Do not play a Ringback yet.\n | \"waiting-for-permission\" // We're waiting for the Microphone permission prompt. You may show a reminder UI here.\n | \"connecting\" // The call is now connection to the remote. This is where you can play a ringback or other connecting tone.\n | \"connected\" // The call is now connected and the audio channel is established.\n | \"ended\"; // The call has ended.\n\nexport type CallErrorCode =\n | \"MICROPHONE_ACCESS_FAILED\"\n | \"CONNECTION_FAILED\"\n | \"AUTHENTICATION_FAILED\"\n | \"PROTOCOL_ERROR\" // The server sent an unexpected message.\n | \"SERVER_ERROR\" // The server sent an error message.\n | \"REMOTE_HANGUP\" // The remote hung up before the call connected.\n | \"CONNECTION_TIMEOUT\"; // The call didn't connect within connectionTimeoutMs.\n\nexport class CallError extends Error {\n readonly code: CallErrorCode;\n\n constructor(code: CallErrorCode, message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"CallError\";\n this.code = code;\n }\n}\n\n// Used if the server doesn't send ICE servers with the auth reply.\nconst DEFAULT_ICE_SERVERS: RTCIceServer[] = [\n { urls: [\"stun:stun.l.google.com:19302\", \"stun:stun.sipgate.net:10000\"] },\n];\n\nconst SOCKET_PROTOCOLS: Record<string, string> = {\n \"http:\": \"ws:\",\n \"https:\": \"wss:\",\n \"ws:\": \"ws:\",\n \"wss:\": \"wss:\",\n};\n\n// Messages received on the signaling socket. See junkswitch/junkswitch/webrtcsocket.py.\ntype SignalingMessage =\n | {\n type: \"auth\";\n body: \"OK\" | \"FAIL\";\n reason?: string;\n ice_servers?: RTCIceServer[];\n }\n | { type: \"desc\"; body: RTCSessionDescriptionInit }\n | { type: \"candidate\"; body: RTCIceCandidateInit }\n | { type: \"hangup\" }\n | { type: \"error\"; body?: string }\n | { type: \"transfer\" };\n\nexport class Call {\n private _state: CallState = \"not-started\";\n private _error?: CallError;\n private readonly _stateHandlers = new Set<(state: CallState) => void>();\n private readonly _remoteStreamHandlers = new Set<(stream: MediaStream) => void>();\n private readonly _messageHandlers = new Set<(payload: Record<string, unknown>) => void>();\n private readonly _webrtcCode: string;\n private readonly _sockUrl: URL;\n\n private readonly _remoteAudio: HTMLAudioElement;\n private readonly _connectingTone?: ConnectingTone;\n private readonly _leavingTone?: Tone;\n private readonly _connectionTimeoutMs?: number;\n private readonly _customPayload: Record<string, unknown>;\n private _connectionTimer?: ReturnType<typeof setTimeout>;\n private _sock?: WebSocket;\n\n private readonly _chatSockUrl: URL;\n // Links the chat socket to this call's session on the server.\n private readonly _chatSessionId = crypto.randomUUID();\n private _chatSock?: WebSocket;\n private _chatReady = false;\n // Messages sent before the chat socket is ready.\n private _pendingChatMessages: string[] = [];\n\n private _localStream?: MediaStream;\n private _peerConnection?: RTCPeerConnection;\n private _remoteStream?: MediaStream;\n private _iceServers?: RTCIceServer[];\n // Settles the promise returned by start().\n private _pendingStart?: { resolve: () => void; reject: (error: CallError) => void };\n\n constructor({\n webrtcCode,\n baseUrl = \"https://app.goguava.ai/\",\n connectingTone = new JingleTone(),\n leavingTone = new LeavingTone(),\n connectionTimeoutMs = 30_000,\n customPayload = {},\n }: {\n /** Code used to authenticate with the signaling server. */\n webrtcCode: string;\n /** Base URL of the Guava server. Defaults to `https://app.goguava.ai/`. */\n baseUrl?: string;\n /** Played while the call is in the \"connecting\" state. Defaults to {@link JingleTone}. */\n connectingTone?: ConnectingTone | \"disabled\";\n /** Played when the call ends. Defaults to {@link LeavingTone}. */\n leavingTone?: Tone | \"disabled\";\n /** Max time in the \"connecting\" state before the call fails with CONNECTION_TIMEOUT. Defaults to 30 seconds. */\n connectionTimeoutMs?: number | \"disabled\";\n /** A custom payload that is forwarded to the agent at the start of the call. */\n customPayload?: Record<string, unknown>;\n }) {\n this._webrtcCode = webrtcCode;\n this._customPayload = customPayload;\n if (connectingTone !== \"disabled\") {\n this._connectingTone = connectingTone;\n }\n if (leavingTone !== \"disabled\") {\n this._leavingTone = leavingTone;\n }\n if (connectionTimeoutMs !== \"disabled\") {\n this._connectionTimeoutMs = connectionTimeoutMs;\n }\n\n this._sockUrl = new URL(\"/webrtc/\", baseUrl);\n const sockProtocol = SOCKET_PROTOCOLS[this._sockUrl.protocol];\n if (sockProtocol === undefined) {\n throw new Error(`Unsupported baseUrl protocol: ${this._sockUrl.protocol}`);\n }\n this._sockUrl.protocol = sockProtocol;\n this._chatSockUrl = new URL(\"/webrtc-chat/\", this._sockUrl);\n\n this._remoteAudio = document.createElement(\"audio\");\n this._remoteAudio.autoplay = true;\n }\n\n get state(): CallState {\n return this._state;\n }\n\n // Set if the call ended due to a failure.\n get error(): CallError | undefined {\n return this._error;\n }\n\n // Set once the remote audio track arrives, which happens before the call reaches \"connected\".\n get remoteStream(): MediaStream | undefined {\n return this._remoteStream;\n }\n\n // Returns a function that unregisters the handler.\n onStateChange(handler: (state: CallState) => void): () => void {\n this._stateHandlers.add(handler);\n return () => {\n this._stateHandlers.delete(handler);\n };\n }\n\n // Fires whenever the remote stream is replaced, including the first time it arrives.\n // Returns a function that unregisters the handler.\n onRemoteStream(handler: (stream: MediaStream) => void): () => void {\n this._remoteStreamHandlers.add(handler);\n return () => {\n this._remoteStreamHandlers.delete(handler);\n };\n }\n\n /**\n * Registers a handler for messages the agent sends with `call.send_webrtc_message()`.\n * @param handler - Called with each message's payload.\n * @returns A function that unregisters the handler.\n */\n onMessage(handler: (payload: Record<string, unknown>) => void): () => void {\n this._messageHandlers.add(handler);\n return () => {\n this._messageHandlers.delete(handler);\n };\n }\n\n private _setState(state: CallState) {\n if (state === this._state) {\n return;\n }\n\n if (state === \"ended\") {\n this._leavingTone?.play();\n }\n\n this._state = state;\n if (state === \"connecting\") {\n this._connectingTone?.start();\n if (this._connectionTimeoutMs !== undefined) {\n this._connectionTimer = setTimeout(() => {\n this._send({ type: \"hangup\" });\n this._end(new CallError(\"CONNECTION_TIMEOUT\", \"Timed out connecting the call.\"));\n }, this._connectionTimeoutMs);\n }\n } else {\n this._connectingTone?.stop();\n clearTimeout(this._connectionTimer);\n this._connectionTimer = undefined;\n }\n for (const handler of this._stateHandlers) {\n handler(state);\n }\n }\n\n async start() {\n if (this._state !== \"not-started\") {\n throw new Error(`start() was already called.`);\n }\n this._setState(\"starting\");\n\n // Add the audio element.\n document.body.appendChild(this._remoteAudio);\n\n // We start by requesting device permissions first. The idea is that, after permission has been granted,\n // it makes sence to play a ringback, but it doesn't make sense to do so during the permission prompt.\n\n const permissionTimer = setTimeout(() => {\n this._setState(\"waiting-for-permission\");\n }, 500);\n\n try {\n this._localStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });\n } catch (error) {\n const callError = new CallError(\"MICROPHONE_ACCESS_FAILED\", \"Failed to access microphone.\", {\n cause: error,\n });\n this._end(callError);\n throw callError;\n } finally {\n clearTimeout(permissionTimer);\n }\n\n // hangup() was called during the permission prompt.\n if (this.state === \"ended\") {\n this._teardown();\n return;\n }\n\n // Now open and authenticate the signaling socket.\n this._setState(\"connecting\"); // UI can start playing rinback tones here.\n const started = new Promise<void>((resolve, reject) => {\n this._pendingStart = { resolve, reject };\n });\n\n this._sock = new WebSocket(this._sockUrl);\n this._sock.onopen = () => {\n this._send({\n type: \"auth\",\n body: JSON.stringify({\n webrtc_code: this._webrtcCode,\n chat_session_id: this._chatSessionId,\n }),\n });\n };\n this._sock.onmessage = (event) => {\n this._onMessage(JSON.parse(event.data) as SignalingMessage);\n };\n this._sock.onclose = () => {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Signaling connection closed.\"));\n };\n\n await started;\n }\n\n /**\n * Sends a message to the agent, delivered to its `on_webrtc_message` handler.\n * Messages sent before the message channel is ready are queued.\n * @param payload - A JSON-serializable object.\n */\n sendMessage(payload: Record<string, unknown>) {\n if (this._state === \"not-started\" || this._state === \"ended\") {\n throw new Error(`sendMessage() called while the call is ${this._state}.`);\n }\n const message = JSON.stringify({ type: \"webrtc-message\", payload });\n if (this._chatReady && this._chatSock !== undefined) {\n this._chatSock.send(message);\n } else {\n this._pendingChatMessages.push(message);\n }\n }\n\n private _openChatSocket() {\n this._chatSock = new WebSocket(this._chatSockUrl);\n this._chatSock.onopen = () => {\n this._chatSock?.send(\n JSON.stringify({ webrtc_code: this._webrtcCode, chat_session_id: this._chatSessionId }),\n );\n };\n this._chatSock.onmessage = (event) => {\n const message = JSON.parse(event.data) as {\n type?: string;\n command_type?: string;\n payload?: Record<string, unknown>;\n };\n // The server sends \"ready\" once the call session is found.\n if (message.type === \"ready\" && !this._chatReady) {\n this._chatReady = true;\n for (const pending of this._pendingChatMessages) {\n this._chatSock?.send(pending);\n }\n this._pendingChatMessages = [];\n } else if (message.command_type === \"send-webrtc-message\") {\n for (const handler of this._messageHandlers) {\n handler(message.payload ?? {});\n }\n }\n };\n }\n\n async hangup() {\n if (this._state === \"not-started\") {\n throw new Error(`hangup() called before start().`);\n }\n if (this._state === \"ended\") {\n return;\n }\n this._send({ type: \"hangup\" });\n this._end();\n }\n\n private _send(message: { type: string; body?: unknown }) {\n if (this._sock !== undefined && this._sock.readyState === WebSocket.OPEN) {\n this._sock.send(JSON.stringify(message));\n }\n }\n\n private _onMessage(message: SignalingMessage) {\n switch (message.type) {\n case \"auth\":\n if (message.body !== \"OK\") {\n this._end(\n new CallError(\"AUTHENTICATION_FAILED\", message.reason || \"Authentication failed.\"),\n );\n return;\n }\n this._iceServers = message.ice_servers;\n this._openChatSocket();\n this._send({ type: \"connect\", body: { custom_payload: this._customPayload } });\n void this._connectPeer();\n break;\n case \"desc\":\n if (!this._peerConnection) {\n this._end(\n new CallError(\n \"PROTOCOL_ERROR\",\n \"Received desc before the peer connection was created.\",\n ),\n );\n return;\n }\n this._peerConnection.setRemoteDescription(message.body).catch((error) => {\n this._end(\n new CallError(\"CONNECTION_FAILED\", \"Failed to set remote description.\", {\n cause: error,\n }),\n );\n });\n break;\n case \"candidate\":\n if (!this._peerConnection) {\n this._end(\n new CallError(\n \"PROTOCOL_ERROR\",\n \"Received candidate before the peer connection was created.\",\n ),\n );\n return;\n }\n this._peerConnection.addIceCandidate(message.body).catch((error) => {\n console.warn(\"Failed to add ICE candidate:\", error);\n });\n break;\n case \"hangup\":\n if (this._state === \"connected\") {\n this._end();\n } else {\n this._end(\n new CallError(\"REMOTE_HANGUP\", \"The remote hung up before the call connected.\"),\n );\n }\n break;\n case \"error\":\n this._end(new CallError(\"SERVER_ERROR\", message.body || \"Unknown server error.\"));\n break;\n case \"transfer\":\n this._end(new CallError(\"PROTOCOL_ERROR\", \"Transfers are not supported.\"));\n break;\n default:\n console.warn(\"Unknown signaling message:\", message);\n }\n }\n\n private async _connectPeer() {\n if (!this._localStream) {\n return;\n }\n this._peerConnection = new RTCPeerConnection({\n iceServers: this._iceServers ?? DEFAULT_ICE_SERVERS,\n });\n this._peerConnection.onicecandidate = (event) => {\n if (event.candidate) {\n this._send({ type: \"candidate\", body: event.candidate });\n }\n };\n this._peerConnection.ontrack = (event) => {\n this._remoteStream = new MediaStream([event.track]);\n this._remoteAudio.srcObject = this._remoteStream;\n for (const handler of this._remoteStreamHandlers) {\n handler(this._remoteStream);\n }\n };\n this._peerConnection.onconnectionstatechange = () => {\n if (this._peerConnection === undefined) {\n return;\n }\n // \"disconnected\" is often transient, so wait for \"failed\" before ending the call.\n if (this._peerConnection.connectionState === \"connected\") {\n this._setState(\"connected\");\n if (this._pendingStart !== undefined) {\n this._pendingStart.resolve();\n this._pendingStart = undefined;\n }\n } else if (this._peerConnection.connectionState === \"failed\") {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Peer connection failed.\"));\n }\n };\n for (const track of this._localStream.getAudioTracks()) {\n this._peerConnection.addTrack(track);\n }\n\n try {\n await this._peerConnection.setLocalDescription();\n } catch (error) {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Failed to create offer.\", { cause: error }));\n return;\n }\n\n // The call may have ended while creating the offer.\n if (this.state === \"ended\") {\n return;\n }\n this._send({ type: \"desc\", body: this._peerConnection.localDescription });\n }\n\n private _end(error?: CallError) {\n if (this._state === \"ended\") {\n return;\n }\n this._error = error;\n this._teardown();\n this._setState(\"ended\");\n\n // If start() is still pending, it rejects on failure and resolves on hangup().\n if (this._pendingStart !== undefined) {\n if (error) {\n this._pendingStart.reject(error);\n } else {\n this._pendingStart.resolve();\n }\n this._pendingStart = undefined;\n }\n }\n\n private _teardown() {\n // Close the signaling socket.\n if (this._sock) {\n // Don't fire close handlers for a socket we closed ourselves.\n this._sock.onclose = null;\n this._sock.close();\n this._sock = undefined;\n }\n\n // Close the chat socket.\n if (this._chatSock) {\n this._chatSock.close();\n this._chatSock = undefined;\n }\n this._chatReady = false;\n this._pendingChatMessages = [];\n\n // Close the peer connection.\n if (this._peerConnection !== undefined) {\n this._peerConnection.close();\n this._peerConnection = undefined;\n }\n for (const track of this._localStream?.getTracks() ?? []) {\n track.stop();\n }\n this._localStream = undefined;\n this._remoteStream = undefined;\n\n // Remove the audio element.\n this._remoteAudio.srcObject = null;\n this._remoteAudio.remove();\n }\n}\n"],
5
+ "mappings": "oKACA,SAASA,EAASC,EAAcC,EAAqB,CACnD,OAAOD,GAAQ,GAAKC,EACtB,CAQO,IAAMC,EAAN,KAA6C,CAA7C,cACLC,EAAA,KAAQ,UAA+B,MACvCA,EAAA,KAAQ,aAAoD,MAE5DA,EAAA,KAAiB,SAAS,KAC1BA,EAAA,KAAiB,YAAY,GAC7BA,EAAA,KAAiB,gBAAgB,GAEjCA,EAAA,KAAiB,aAAa,IAE9B,OAAc,CACZ,GAAI,KAAK,QAAS,OAElB,IAAMC,EAAU,IAAI,aACdC,EAAOD,EAAQ,WAAW,EAChCC,EAAK,KAAK,MAAQ,EAClBA,EAAK,QAAQD,EAAQ,WAAW,EAEhC,QAAWE,IAAa,CAAC,IAAK,GAAG,EAAG,CAClC,IAAMC,EAAaH,EAAQ,iBAAiB,EAC5CG,EAAW,UAAU,MAAQD,EAC7BC,EAAW,QAAQF,CAAI,EACvBE,EAAW,MAAM,CACnB,CAEA,IAAIC,EAAWJ,EAAQ,YAAc,KAAK,WACpCK,EAAO,IAAM,CACjBJ,EAAK,KAAK,eAAe,KAAK,OAAQG,CAAQ,EAC9CH,EAAK,KAAK,eAAe,EAAGG,EAAW,KAAK,SAAS,EACrDA,GAAY,KAAK,aACnB,EACAC,EAAK,EACL,KAAK,WAAa,YAAYA,EAAM,KAAK,cAAgB,GAAI,EAC7D,KAAK,QAAUL,CACjB,CAEA,MAAa,CACN,KAAK,UAEN,KAAK,aACP,cAAc,KAAK,UAAU,EAC7B,KAAK,WAAa,MAGf,KAAK,QAAQ,MAAM,EACxB,KAAK,QAAU,KACjB,CACF,EAEaM,EAAN,KAA2C,CAA3C,cACLP,EAAA,KAAQ,UAA+B,MACvCA,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UAAU,MAAM,EACrB,KAAK,UAAY,QAEnB,KAAK,WAAa,GAClB,KAAK,qBAAuB,CAAC,EAGzB,KAAK,kBAAoB,SAC3B,KAAK,gBAAgB,MAAM,EAC3B,KAAK,gBAAkB,QAEzB,QAAWQ,KAAS,KAAK,cAAc,UAAU,GAAK,CAAC,EACrDA,EAAM,KAAK,EAEb,KAAK,aAAe,OACpB,KAAK,cAAgB,OAGrB,KAAK,aAAa,UAAY,KAC9B,KAAK,aAAa,OAAO,CAC3B,CACF",
6
+ "names": ["beatTime", "beat", "bpm", "RingbackTone", "__publicField", "context", "gain", "frequency", "oscillator", "nextRing", "ring", "JingleTone", "barLength", "note", "match", "name", "accidental", "octave", "semitones", "offset", "midi", "now", "bar", "n", "beatLength", "startTime", "duration", "envelope", "LeavingTone", "ctx", "first", "second", "attack", "decay", "volume", "peakTime", "endTime", "resolve", "CallError", "code", "message", "options", "__publicField", "DEFAULT_ICE_SERVERS", "SOCKET_PROTOCOLS", "Call", "webrtcCode", "baseUrl", "connectingTone", "JingleTone", "leavingTone", "LeavingTone", "connectionTimeoutMs", "customPayload", "sockProtocol", "handler", "state", "permissionTimer", "error", "callError", "started", "resolve", "reject", "event", "payload", "pending", "track"]
7
7
  }
@@ -1,2 +1,2 @@
1
- var Guava=(()=>{var l=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var g=(o,e,t)=>e in o?l(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var S=(o,e)=>{for(var t in e)l(o,t,{get:e[t],enumerable:!0})},y=(o,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of C(e))!f.call(o,r)&&r!==t&&l(o,r,{get:()=>e[r],enumerable:!(n=v(e,r))||n.enumerable});return o};var T=o=>y(l({},"__esModule",{value:!0}),o);var i=(o,e,t)=>g(o,typeof e!="symbol"?e+"":e,t);var w={};S(w,{Call:()=>u,CallError:()=>s,JingleTone:()=>c,RingbackTone:()=>h});function p(o,e){return o*(60/e)}var h=class{constructor(){i(this,"context",null);i(this,"intervalId",null);i(this,"volume",.15);i(this,"onSeconds",2);i(this,"periodSeconds",6);i(this,"startDelay",.3)}start(){if(this.context)return;let e=new AudioContext,t=e.createGain();t.gain.value=0,t.connect(e.destination);for(let a of[440,480]){let d=e.createOscillator();d.frequency.value=a,d.connect(t),d.start()}let n=e.currentTime+this.startDelay,r=()=>{t.gain.setValueAtTime(this.volume,n),t.gain.setValueAtTime(0,n+this.onSeconds),n+=this.periodSeconds};r(),this.intervalId=setInterval(r,this.periodSeconds*1e3),this.context=e}stop(){this.context&&(this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null),this.context.close(),this.context=null)}},c=class{constructor(){i(this,"context",null);i(this,"masterGain",null);i(this,"intervalId",null);i(this,"running",!1);i(this,"pulseCount",0);i(this,"nextBarStart",0);i(this,"volume",.25);i(this,"bpm",120);i(this,"beatsPerBar",4);i(this,"startDelay",.3)}start(){if(this.running)return;this.running=!0,this.pulseCount=0,this.context=new AudioContext,this.masterGain=this.context.createGain(),this.masterGain.gain.value=this.volume,this.masterGain.connect(this.context.destination),this.nextBarStart=this.context.currentTime+this.startDelay,this.playPulse(),this.playPulse();let e=p(this.beatsPerBar,this.bpm);this.intervalId=setInterval(()=>{this.playPulse()},e*1e3)}noteToFrequency(e){let t=e.match(/^([A-G])(#|b)?(-?\d+)$/);if(!t)throw new Error(`Invalid note: ${e}`);let[,n,r,a]=t,d={C:0,D:2,E:4,F:5,G:7,A:9,B:11},_=r==="#"?1:r==="b"?-1:0,m=(Number(a)+1)*12+d[n]+_;return 440*Math.pow(2,(m-69)/12)}stop(){if(!this.running)return;this.running=!1,this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null);let e=this.context,t=this.masterGain;if(!e||!t)return;let n=e.currentTime;t.gain.setValueAtTime(t.gain.value,n),t.gain.linearRampToValueAtTime(0,n+.15),setTimeout(()=>{e.close()},200),this.context=null,this.masterGain=null}playPulse(){if(!this.context||!this.masterGain)return;let e=this.nextBarStart,t=r=>e+p(r,this.bpm),n=p(1,this.bpm);this.pulseCount%4===0||this.pulseCount%4===2?(this.playNote("C5",t(0),n),this.playNote("E5",t(.5),n),this.playNote("G4",t(1),n)):this.pulseCount%4===3?(this.playNote("E5",t(0),n),this.playNote("G5",t(.5),n),this.playNote("C5",t(1),n)):(this.playNote("E5",t(0),n),this.playNote("G4",t(.5),n),this.playNote("C5",t(1),n)),this.pulseCount++,this.nextBarStart=t(this.beatsPerBar)}playNote(e,t,n){if(!this.context||!this.masterGain)return;let r=this.context.createOscillator(),a=this.context.createGain();r.type="sine",r.frequency.value=this.noteToFrequency(e),a.gain.setValueAtTime(0,t),a.gain.linearRampToValueAtTime(.7,t+.05),a.gain.exponentialRampToValueAtTime(.001,t+n),r.connect(a),a.connect(this.masterGain),r.start(t),r.stop(t+n+.01),r.onended=()=>{r.disconnect(),a.disconnect()}}};var s=class extends Error{constructor(t,n,r){super(n,r);i(this,"code");this.name="CallError",this.code=t}},b=[{urls:["stun:stun.l.google.com:19302","stun:stun.sipgate.net:10000"]}],E={"http:":"ws:","https:":"wss:","ws:":"ws:","wss:":"wss:"},u=class{constructor({webrtcCode:e,baseUrl:t="https://app.goguava.ai/",connectingTone:n=new c}){i(this,"_state","not-started");i(this,"_error");i(this,"_stateHandlers",new Set);i(this,"_remoteStreamHandlers",new Set);i(this,"_webrtcCode");i(this,"_sockUrl");i(this,"_remoteAudio");i(this,"_connectingTone");i(this,"_sock");i(this,"_localStream");i(this,"_peerConnection");i(this,"_remoteStream");i(this,"_iceServers");i(this,"_pendingStart");this._webrtcCode=e,n!=="disabled"&&(this._connectingTone=n),this._sockUrl=new URL("/webrtc/",t);let r=E[this._sockUrl.protocol];if(r===void 0)throw new Error(`Unsupported baseUrl protocol: ${this._sockUrl.protocol}`);this._sockUrl.protocol=r,this._remoteAudio=document.createElement("audio"),this._remoteAudio.autoplay=!0}get state(){return this._state}get error(){return this._error}get remoteStream(){return this._remoteStream}onStateChange(e){return this._stateHandlers.add(e),()=>{this._stateHandlers.delete(e)}}onRemoteStream(e){return this._remoteStreamHandlers.add(e),()=>{this._remoteStreamHandlers.delete(e)}}_setState(e){if(e!==this._state){this._state=e,e==="connecting"?this._connectingTone?.start():this._connectingTone?.stop();for(let t of this._stateHandlers)t(e)}}async start(){if(this._state!=="not-started")throw new Error("start() was already called.");this._setState("starting"),document.body.appendChild(this._remoteAudio);let e=setTimeout(()=>{this._setState("waiting-for-permission")},500);try{this._localStream=await navigator.mediaDevices.getUserMedia({audio:!0,video:!1})}catch(n){let r=new s("MICROPHONE_ACCESS_FAILED","Failed to access microphone.",{cause:n});throw this._end(r),r}finally{clearTimeout(e)}if(this.state==="ended"){this._teardown();return}this._setState("connecting");let t=new Promise((n,r)=>{this._pendingStart={resolve:n,reject:r}});this._sock=new WebSocket(this._sockUrl),this._sock.onopen=()=>{this._send({type:"auth",body:this._webrtcCode})},this._sock.onmessage=n=>{this._onMessage(JSON.parse(n.data))},this._sock.onclose=()=>{this._end(new s("CONNECTION_FAILED","Signaling connection closed."))},await t}async hangup(){if(this._state==="not-started")throw new Error("hangup() called before start().");this._state!=="ended"&&(this._send({type:"hangup"}),this._end())}_send(e){this._sock!==void 0&&this._sock.readyState===WebSocket.OPEN&&this._sock.send(JSON.stringify(e))}_onMessage(e){switch(e.type){case"auth":if(e.body!=="OK"){this._end(new s("AUTHENTICATION_FAILED",e.reason||"Authentication failed."));return}this._iceServers=e.ice_servers,this._send({type:"connect",body:{}}),this._connectPeer();break;case"desc":if(!this._peerConnection){this._end(new s("PROTOCOL_ERROR","Received desc before the peer connection was created."));return}this._peerConnection.setRemoteDescription(e.body).catch(t=>{this._end(new s("CONNECTION_FAILED","Failed to set remote description.",{cause:t}))});break;case"candidate":if(!this._peerConnection){this._end(new s("PROTOCOL_ERROR","Received candidate before the peer connection was created."));return}this._peerConnection.addIceCandidate(e.body).catch(t=>{console.warn("Failed to add ICE candidate:",t)});break;case"hangup":this._state==="connected"?this._end():this._end(new s("REMOTE_HANGUP","The remote hung up before the call connected."));break;case"error":this._end(new s("SERVER_ERROR",e.body||"Unknown server error."));break;case"transfer":this._end(new s("PROTOCOL_ERROR","Transfers are not supported."));break;default:console.warn("Unknown signaling message:",e)}}async _connectPeer(){if(this._localStream){this._peerConnection=new RTCPeerConnection({iceServers:this._iceServers??b}),this._peerConnection.onicecandidate=e=>{e.candidate&&this._send({type:"candidate",body:e.candidate})},this._peerConnection.ontrack=e=>{this._remoteStream=new MediaStream([e.track]),this._remoteAudio.srcObject=this._remoteStream;for(let t of this._remoteStreamHandlers)t(this._remoteStream)},this._peerConnection.onconnectionstatechange=()=>{this._peerConnection!==void 0&&(this._peerConnection.connectionState==="connected"?(this._setState("connected"),this._pendingStart!==void 0&&(this._pendingStart.resolve(),this._pendingStart=void 0)):this._peerConnection.connectionState==="failed"&&this._end(new s("CONNECTION_FAILED","Peer connection failed.")))};for(let e of this._localStream.getAudioTracks())this._peerConnection.addTrack(e);try{await this._peerConnection.setLocalDescription()}catch(e){this._end(new s("CONNECTION_FAILED","Failed to create offer.",{cause:e}));return}this.state!=="ended"&&this._send({type:"desc",body:this._peerConnection.localDescription})}}_end(e){this._state!=="ended"&&(this._error=e,this._teardown(),this._setState("ended"),this._pendingStart!==void 0&&(e?this._pendingStart.reject(e):this._pendingStart.resolve(),this._pendingStart=void 0))}_teardown(){this._sock&&(this._sock.onclose=null,this._sock.close(),this._sock=void 0),this._peerConnection!==void 0&&(this._peerConnection.close(),this._peerConnection=void 0);for(let e of this._localStream?.getTracks()??[])e.stop();this._localStream=void 0,this._remoteStream=void 0,this._remoteAudio.srcObject=null,this._remoteAudio.remove()}};return T(w);})();
1
+ var Guava=(()=>{var _=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var w=(o,e,t)=>e in o?_(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var b=(o,e)=>{for(var t in e)_(o,t,{get:e[t],enumerable:!0})},R=(o,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of C(e))!T.call(o,s)&&s!==t&&_(o,s,{get:()=>e[s],enumerable:!(n=S(e,s))||n.enumerable});return o};var k=o=>R(_({},"__esModule",{value:!0}),o);var i=(o,e,t)=>w(o,typeof e!="symbol"?e+"":e,t);var I={};b(I,{Call:()=>g,CallError:()=>a,JingleTone:()=>h,LeavingTone:()=>p,RingbackTone:()=>u});function m(o,e){return o*(60/e)}var u=class{constructor(){i(this,"context",null);i(this,"intervalId",null);i(this,"volume",.15);i(this,"onSeconds",2);i(this,"periodSeconds",6);i(this,"startDelay",.3)}start(){if(this.context)return;let e=new AudioContext,t=e.createGain();t.gain.value=0,t.connect(e.destination);for(let r of[440,480]){let d=e.createOscillator();d.frequency.value=r,d.connect(t),d.start()}let n=e.currentTime+this.startDelay,s=()=>{t.gain.setValueAtTime(this.volume,n),t.gain.setValueAtTime(0,n+this.onSeconds),n+=this.periodSeconds};s(),this.intervalId=setInterval(s,this.periodSeconds*1e3),this.context=e}stop(){this.context&&(this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null),this.context.close(),this.context=null)}},h=class{constructor(){i(this,"context",null);i(this,"masterGain",null);i(this,"intervalId",null);i(this,"running",!1);i(this,"pulseCount",0);i(this,"nextBarStart",0);i(this,"volume",.3);i(this,"bpm",120);i(this,"beatsPerBar",4);i(this,"startDelay",.3)}start(){if(this.running)return;this.running=!0,this.pulseCount=0,this.context=new AudioContext,this.masterGain=this.context.createGain(),this.masterGain.gain.value=this.volume,this.masterGain.connect(this.context.destination),this.nextBarStart=this.context.currentTime+this.startDelay,this.playPulse(),this.playPulse();let e=m(this.beatsPerBar,this.bpm);this.intervalId=setInterval(()=>{this.playPulse()},e*1e3)}noteToFrequency(e){let t=e.match(/^([A-G])(#|b)?(-?\d+)$/);if(!t)throw new Error(`Invalid note: ${e}`);let[,n,s,r]=t,d={C:0,D:2,E:4,F:5,G:7,A:9,B:11},c=s==="#"?1:s==="b"?-1:0,l=(Number(r)+1)*12+d[n]+c;return 440*Math.pow(2,(l-69)/12)}stop(){if(!this.running)return;this.running=!1,this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null);let e=this.context,t=this.masterGain;if(!e||!t)return;let n=e.currentTime;t.gain.setValueAtTime(t.gain.value,n),t.gain.linearRampToValueAtTime(0,n+.15),setTimeout(()=>{e.close()},200),this.context=null,this.masterGain=null}playPulse(){if(!this.context||!this.masterGain)return;let e=this.nextBarStart,t=s=>e+m(s,this.bpm),n=m(1,this.bpm);this.pulseCount%4===0||this.pulseCount%4===2?(this.playNote("C5",t(0),n),this.playNote("E5",t(.5),n),this.playNote("G4",t(1),n)):this.pulseCount%4===3?(this.playNote("E5",t(0),n),this.playNote("G5",t(.5),n),this.playNote("C5",t(1),n)):(this.playNote("E5",t(0),n),this.playNote("G4",t(.5),n),this.playNote("C5",t(1),n)),this.pulseCount++,this.nextBarStart=t(this.beatsPerBar)}playNote(e,t,n){if(!this.context||!this.masterGain)return;let s=this.context.createOscillator(),r=this.context.createGain();s.type="sine",s.frequency.value=this.noteToFrequency(e),r.gain.setValueAtTime(0,t),r.gain.linearRampToValueAtTime(.7,t+.05),r.gain.exponentialRampToValueAtTime(.001,t+n),s.connect(r),r.connect(this.masterGain),s.start(t),s.stop(t+n+.01),s.onended=()=>{s.disconnect(),r.disconnect()}}},p=class{play(){let e=new AudioContext;e.state==="suspended"&&e.resume();let t=e.currentTime,n=this.playNote(e,{frequency:293.665,startTime:t,attack:.065,decay:.085,volume:.2}),s=this.playNote(e,{frequency:220,startTime:t+.139,attack:.018,decay:.075,volume:.2});Promise.all([n,s]).then(()=>e.close())}playNote(e,{frequency:t,startTime:n,attack:s,decay:r,volume:d}){let c=e.createOscillator(),l=e.createGain();c.type="sine",c.frequency.setValueAtTime(t,n),l.gain.setValueAtTime(0,n),l.gain.linearRampToValueAtTime(d,n+s);let v=n+s,y=v+r*5;return l.gain.setTargetAtTime(1e-5,v,r),c.connect(l),l.connect(e.destination),c.start(n),c.stop(y),new Promise(f=>{c.onended=()=>{c.disconnect(),l.disconnect(),f()}})}};var a=class extends Error{constructor(t,n,s){super(n,s);i(this,"code");this.name="CallError",this.code=t}},E=[{urls:["stun:stun.l.google.com:19302","stun:stun.sipgate.net:10000"]}],O={"http:":"ws:","https:":"wss:","ws:":"ws:","wss:":"wss:"},g=class{constructor({webrtcCode:e,baseUrl:t="https://app.goguava.ai/",connectingTone:n=new h,leavingTone:s=new p,connectionTimeoutMs:r=3e4,customPayload:d={}}){i(this,"_state","not-started");i(this,"_error");i(this,"_stateHandlers",new Set);i(this,"_remoteStreamHandlers",new Set);i(this,"_messageHandlers",new Set);i(this,"_webrtcCode");i(this,"_sockUrl");i(this,"_remoteAudio");i(this,"_connectingTone");i(this,"_leavingTone");i(this,"_connectionTimeoutMs");i(this,"_customPayload");i(this,"_connectionTimer");i(this,"_sock");i(this,"_chatSockUrl");i(this,"_chatSessionId",crypto.randomUUID());i(this,"_chatSock");i(this,"_chatReady",!1);i(this,"_pendingChatMessages",[]);i(this,"_localStream");i(this,"_peerConnection");i(this,"_remoteStream");i(this,"_iceServers");i(this,"_pendingStart");this._webrtcCode=e,this._customPayload=d,n!=="disabled"&&(this._connectingTone=n),s!=="disabled"&&(this._leavingTone=s),r!=="disabled"&&(this._connectionTimeoutMs=r),this._sockUrl=new URL("/webrtc/",t);let c=O[this._sockUrl.protocol];if(c===void 0)throw new Error(`Unsupported baseUrl protocol: ${this._sockUrl.protocol}`);this._sockUrl.protocol=c,this._chatSockUrl=new URL("/webrtc-chat/",this._sockUrl),this._remoteAudio=document.createElement("audio"),this._remoteAudio.autoplay=!0}get state(){return this._state}get error(){return this._error}get remoteStream(){return this._remoteStream}onStateChange(e){return this._stateHandlers.add(e),()=>{this._stateHandlers.delete(e)}}onRemoteStream(e){return this._remoteStreamHandlers.add(e),()=>{this._remoteStreamHandlers.delete(e)}}onMessage(e){return this._messageHandlers.add(e),()=>{this._messageHandlers.delete(e)}}_setState(e){if(e!==this._state){e==="ended"&&this._leavingTone?.play(),this._state=e,e==="connecting"?(this._connectingTone?.start(),this._connectionTimeoutMs!==void 0&&(this._connectionTimer=setTimeout(()=>{this._send({type:"hangup"}),this._end(new a("CONNECTION_TIMEOUT","Timed out connecting the call."))},this._connectionTimeoutMs))):(this._connectingTone?.stop(),clearTimeout(this._connectionTimer),this._connectionTimer=void 0);for(let t of this._stateHandlers)t(e)}}async start(){if(this._state!=="not-started")throw new Error("start() was already called.");this._setState("starting"),document.body.appendChild(this._remoteAudio);let e=setTimeout(()=>{this._setState("waiting-for-permission")},500);try{this._localStream=await navigator.mediaDevices.getUserMedia({audio:!0,video:!1})}catch(n){let s=new a("MICROPHONE_ACCESS_FAILED","Failed to access microphone.",{cause:n});throw this._end(s),s}finally{clearTimeout(e)}if(this.state==="ended"){this._teardown();return}this._setState("connecting");let t=new Promise((n,s)=>{this._pendingStart={resolve:n,reject:s}});this._sock=new WebSocket(this._sockUrl),this._sock.onopen=()=>{this._send({type:"auth",body:JSON.stringify({webrtc_code:this._webrtcCode,chat_session_id:this._chatSessionId})})},this._sock.onmessage=n=>{this._onMessage(JSON.parse(n.data))},this._sock.onclose=()=>{this._end(new a("CONNECTION_FAILED","Signaling connection closed."))},await t}sendMessage(e){if(this._state==="not-started"||this._state==="ended")throw new Error(`sendMessage() called while the call is ${this._state}.`);let t=JSON.stringify({type:"webrtc-message",payload:e});this._chatReady&&this._chatSock!==void 0?this._chatSock.send(t):this._pendingChatMessages.push(t)}_openChatSocket(){this._chatSock=new WebSocket(this._chatSockUrl),this._chatSock.onopen=()=>{this._chatSock?.send(JSON.stringify({webrtc_code:this._webrtcCode,chat_session_id:this._chatSessionId}))},this._chatSock.onmessage=e=>{let t=JSON.parse(e.data);if(t.type==="ready"&&!this._chatReady){this._chatReady=!0;for(let n of this._pendingChatMessages)this._chatSock?.send(n);this._pendingChatMessages=[]}else if(t.command_type==="send-webrtc-message")for(let n of this._messageHandlers)n(t.payload??{})}}async hangup(){if(this._state==="not-started")throw new Error("hangup() called before start().");this._state!=="ended"&&(this._send({type:"hangup"}),this._end())}_send(e){this._sock!==void 0&&this._sock.readyState===WebSocket.OPEN&&this._sock.send(JSON.stringify(e))}_onMessage(e){switch(e.type){case"auth":if(e.body!=="OK"){this._end(new a("AUTHENTICATION_FAILED",e.reason||"Authentication failed."));return}this._iceServers=e.ice_servers,this._openChatSocket(),this._send({type:"connect",body:{custom_payload:this._customPayload}}),this._connectPeer();break;case"desc":if(!this._peerConnection){this._end(new a("PROTOCOL_ERROR","Received desc before the peer connection was created."));return}this._peerConnection.setRemoteDescription(e.body).catch(t=>{this._end(new a("CONNECTION_FAILED","Failed to set remote description.",{cause:t}))});break;case"candidate":if(!this._peerConnection){this._end(new a("PROTOCOL_ERROR","Received candidate before the peer connection was created."));return}this._peerConnection.addIceCandidate(e.body).catch(t=>{console.warn("Failed to add ICE candidate:",t)});break;case"hangup":this._state==="connected"?this._end():this._end(new a("REMOTE_HANGUP","The remote hung up before the call connected."));break;case"error":this._end(new a("SERVER_ERROR",e.body||"Unknown server error."));break;case"transfer":this._end(new a("PROTOCOL_ERROR","Transfers are not supported."));break;default:console.warn("Unknown signaling message:",e)}}async _connectPeer(){if(this._localStream){this._peerConnection=new RTCPeerConnection({iceServers:this._iceServers??E}),this._peerConnection.onicecandidate=e=>{e.candidate&&this._send({type:"candidate",body:e.candidate})},this._peerConnection.ontrack=e=>{this._remoteStream=new MediaStream([e.track]),this._remoteAudio.srcObject=this._remoteStream;for(let t of this._remoteStreamHandlers)t(this._remoteStream)},this._peerConnection.onconnectionstatechange=()=>{this._peerConnection!==void 0&&(this._peerConnection.connectionState==="connected"?(this._setState("connected"),this._pendingStart!==void 0&&(this._pendingStart.resolve(),this._pendingStart=void 0)):this._peerConnection.connectionState==="failed"&&this._end(new a("CONNECTION_FAILED","Peer connection failed.")))};for(let e of this._localStream.getAudioTracks())this._peerConnection.addTrack(e);try{await this._peerConnection.setLocalDescription()}catch(e){this._end(new a("CONNECTION_FAILED","Failed to create offer.",{cause:e}));return}this.state!=="ended"&&this._send({type:"desc",body:this._peerConnection.localDescription})}}_end(e){this._state!=="ended"&&(this._error=e,this._teardown(),this._setState("ended"),this._pendingStart!==void 0&&(e?this._pendingStart.reject(e):this._pendingStart.resolve(),this._pendingStart=void 0))}_teardown(){this._sock&&(this._sock.onclose=null,this._sock.close(),this._sock=void 0),this._chatSock&&(this._chatSock.close(),this._chatSock=void 0),this._chatReady=!1,this._pendingChatMessages=[],this._peerConnection!==void 0&&(this._peerConnection.close(),this._peerConnection=void 0);for(let e of this._localStream?.getTracks()??[])e.stop();this._localStream=void 0,this._remoteStream=void 0,this._remoteAudio.srcObject=null,this._remoteAudio.remove()}};return k(I);})();
2
2
  //# sourceMappingURL=guava-webrtc.min.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/webrtc/index.ts", "../src/webrtc/tones.ts"],
4
- "sourcesContent": ["import { type ConnectingTone, JingleTone } from \"./tones.ts\";\n\nexport { type ConnectingTone, JingleTone, RingbackTone } from \"./tones.ts\";\n\nexport type CallState =\n | \"not-started\" // start() hasn't been called yet.\n | \"starting\" // Starting the call. Do not play a Ringback yet.\n | \"waiting-for-permission\" // We're waiting for the Microphone permission prompt. You may show a reminder UI here.\n | \"connecting\" // The call is now connection to the remote. This is where you can play a ringback or other connecting tone.\n | \"connected\" // The call is now connected and the audio channel is established.\n | \"ended\"; // The call has ended.\n\nexport type CallErrorCode =\n | \"MICROPHONE_ACCESS_FAILED\"\n | \"CONNECTION_FAILED\"\n | \"AUTHENTICATION_FAILED\"\n | \"PROTOCOL_ERROR\" // The server sent an unexpected message.\n | \"SERVER_ERROR\" // The server sent an error message.\n | \"REMOTE_HANGUP\"; // The remote hung up before the call connected.\n\nexport class CallError extends Error {\n readonly code: CallErrorCode;\n\n constructor(code: CallErrorCode, message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"CallError\";\n this.code = code;\n }\n}\n\n// Used if the server doesn't send ICE servers with the auth reply.\nconst DEFAULT_ICE_SERVERS: RTCIceServer[] = [\n { urls: [\"stun:stun.l.google.com:19302\", \"stun:stun.sipgate.net:10000\"] },\n];\n\nconst SOCKET_PROTOCOLS: Record<string, string> = {\n \"http:\": \"ws:\",\n \"https:\": \"wss:\",\n \"ws:\": \"ws:\",\n \"wss:\": \"wss:\",\n};\n\n// Messages received on the signaling socket. See junkswitch/junkswitch/webrtcsocket.py.\ntype SignalingMessage =\n | {\n type: \"auth\";\n body: \"OK\" | \"FAIL\";\n reason?: string;\n ice_servers?: RTCIceServer[];\n }\n | { type: \"desc\"; body: RTCSessionDescriptionInit }\n | { type: \"candidate\"; body: RTCIceCandidateInit }\n | { type: \"hangup\" }\n | { type: \"error\"; body?: string }\n | { type: \"transfer\" };\n\nexport class Call {\n private _state: CallState = \"not-started\";\n private _error?: CallError;\n private readonly _stateHandlers = new Set<(state: CallState) => void>();\n private readonly _remoteStreamHandlers = new Set<(stream: MediaStream) => void>();\n private readonly _webrtcCode: string;\n private readonly _sockUrl: URL;\n private readonly _remoteAudio: HTMLAudioElement;\n private readonly _connectingTone?: ConnectingTone;\n private _sock?: WebSocket;\n private _localStream?: MediaStream;\n private _peerConnection?: RTCPeerConnection;\n private _remoteStream?: MediaStream;\n private _iceServers?: RTCIceServer[];\n // Settles the promise returned by start().\n private _pendingStart?: { resolve: () => void; reject: (error: CallError) => void };\n\n constructor({\n webrtcCode,\n baseUrl = \"https://app.goguava.ai/\",\n connectingTone = new JingleTone(),\n }: {\n webrtcCode: string;\n baseUrl?: string;\n // Played while the call is in the \"connecting\" state.\n connectingTone?: ConnectingTone | \"disabled\";\n }) {\n this._webrtcCode = webrtcCode;\n if (connectingTone !== \"disabled\") {\n this._connectingTone = connectingTone;\n }\n\n this._sockUrl = new URL(\"/webrtc/\", baseUrl);\n const sockProtocol = SOCKET_PROTOCOLS[this._sockUrl.protocol];\n if (sockProtocol === undefined) {\n throw new Error(`Unsupported baseUrl protocol: ${this._sockUrl.protocol}`);\n }\n this._sockUrl.protocol = sockProtocol;\n\n this._remoteAudio = document.createElement(\"audio\");\n this._remoteAudio.autoplay = true;\n }\n\n get state(): CallState {\n return this._state;\n }\n\n // Set if the call ended due to a failure.\n get error(): CallError | undefined {\n return this._error;\n }\n\n // Set once the remote audio track arrives, which happens before the call reaches \"connected\".\n get remoteStream(): MediaStream | undefined {\n return this._remoteStream;\n }\n\n // Returns a function that unregisters the handler.\n onStateChange(handler: (state: CallState) => void): () => void {\n this._stateHandlers.add(handler);\n return () => {\n this._stateHandlers.delete(handler);\n };\n }\n\n // Fires whenever the remote stream is replaced, including the first time it arrives.\n // Returns a function that unregisters the handler.\n onRemoteStream(handler: (stream: MediaStream) => void): () => void {\n this._remoteStreamHandlers.add(handler);\n return () => {\n this._remoteStreamHandlers.delete(handler);\n };\n }\n\n private _setState(state: CallState) {\n if (state === this._state) {\n return;\n }\n this._state = state;\n if (state === \"connecting\") {\n this._connectingTone?.start();\n } else {\n this._connectingTone?.stop();\n }\n for (const handler of this._stateHandlers) {\n handler(state);\n }\n }\n\n async start() {\n if (this._state !== \"not-started\") {\n throw new Error(`start() was already called.`);\n }\n this._setState(\"starting\");\n\n // Add the audio element.\n document.body.appendChild(this._remoteAudio);\n\n // We start by requesting device permissions first. The idea is that, after permission has been granted,\n // it makes sence to play a ringback, but it doesn't make sense to do so during the permission prompt.\n\n const permissionTimer = setTimeout(() => {\n this._setState(\"waiting-for-permission\");\n }, 500);\n\n try {\n this._localStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });\n } catch (error) {\n const callError = new CallError(\"MICROPHONE_ACCESS_FAILED\", \"Failed to access microphone.\", {\n cause: error,\n });\n this._end(callError);\n throw callError;\n } finally {\n clearTimeout(permissionTimer);\n }\n\n // hangup() was called during the permission prompt.\n if (this.state === \"ended\") {\n this._teardown();\n return;\n }\n\n // Now open and authenticate the signaling socket.\n this._setState(\"connecting\"); // UI can start playing rinback tones here.\n const started = new Promise<void>((resolve, reject) => {\n this._pendingStart = { resolve, reject };\n });\n\n this._sock = new WebSocket(this._sockUrl);\n this._sock.onopen = () => {\n this._send({ type: \"auth\", body: this._webrtcCode });\n };\n this._sock.onmessage = (event) => {\n this._onMessage(JSON.parse(event.data) as SignalingMessage);\n };\n this._sock.onclose = () => {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Signaling connection closed.\"));\n };\n\n await started;\n }\n\n async hangup() {\n if (this._state === \"not-started\") {\n throw new Error(`hangup() called before start().`);\n }\n if (this._state === \"ended\") {\n return;\n }\n this._send({ type: \"hangup\" });\n this._end();\n }\n\n private _send(message: { type: string; body?: unknown }) {\n if (this._sock !== undefined && this._sock.readyState === WebSocket.OPEN) {\n this._sock.send(JSON.stringify(message));\n }\n }\n\n private _onMessage(message: SignalingMessage) {\n switch (message.type) {\n case \"auth\":\n if (message.body !== \"OK\") {\n this._end(\n new CallError(\"AUTHENTICATION_FAILED\", message.reason || \"Authentication failed.\"),\n );\n return;\n }\n this._iceServers = message.ice_servers;\n this._send({ type: \"connect\", body: {} });\n void this._connectPeer();\n break;\n case \"desc\":\n if (!this._peerConnection) {\n this._end(\n new CallError(\n \"PROTOCOL_ERROR\",\n \"Received desc before the peer connection was created.\",\n ),\n );\n return;\n }\n this._peerConnection.setRemoteDescription(message.body).catch((error) => {\n this._end(\n new CallError(\"CONNECTION_FAILED\", \"Failed to set remote description.\", {\n cause: error,\n }),\n );\n });\n break;\n case \"candidate\":\n if (!this._peerConnection) {\n this._end(\n new CallError(\n \"PROTOCOL_ERROR\",\n \"Received candidate before the peer connection was created.\",\n ),\n );\n return;\n }\n this._peerConnection.addIceCandidate(message.body).catch((error) => {\n console.warn(\"Failed to add ICE candidate:\", error);\n });\n break;\n case \"hangup\":\n if (this._state === \"connected\") {\n this._end();\n } else {\n this._end(\n new CallError(\"REMOTE_HANGUP\", \"The remote hung up before the call connected.\"),\n );\n }\n break;\n case \"error\":\n this._end(new CallError(\"SERVER_ERROR\", message.body || \"Unknown server error.\"));\n break;\n case \"transfer\":\n this._end(new CallError(\"PROTOCOL_ERROR\", \"Transfers are not supported.\"));\n break;\n default:\n console.warn(\"Unknown signaling message:\", message);\n }\n }\n\n private async _connectPeer() {\n if (!this._localStream) {\n return;\n }\n this._peerConnection = new RTCPeerConnection({\n iceServers: this._iceServers ?? DEFAULT_ICE_SERVERS,\n });\n this._peerConnection.onicecandidate = (event) => {\n if (event.candidate) {\n this._send({ type: \"candidate\", body: event.candidate });\n }\n };\n this._peerConnection.ontrack = (event) => {\n this._remoteStream = new MediaStream([event.track]);\n this._remoteAudio.srcObject = this._remoteStream;\n for (const handler of this._remoteStreamHandlers) {\n handler(this._remoteStream);\n }\n };\n this._peerConnection.onconnectionstatechange = () => {\n if (this._peerConnection === undefined) {\n return;\n }\n // \"disconnected\" is often transient, so wait for \"failed\" before ending the call.\n if (this._peerConnection.connectionState === \"connected\") {\n this._setState(\"connected\");\n if (this._pendingStart !== undefined) {\n this._pendingStart.resolve();\n this._pendingStart = undefined;\n }\n } else if (this._peerConnection.connectionState === \"failed\") {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Peer connection failed.\"));\n }\n };\n for (const track of this._localStream.getAudioTracks()) {\n this._peerConnection.addTrack(track);\n }\n\n try {\n await this._peerConnection.setLocalDescription();\n } catch (error) {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Failed to create offer.\", { cause: error }));\n return;\n }\n\n // The call may have ended while creating the offer.\n if (this.state === \"ended\") {\n return;\n }\n this._send({ type: \"desc\", body: this._peerConnection.localDescription });\n }\n\n private _end(error?: CallError) {\n if (this._state === \"ended\") {\n return;\n }\n this._error = error;\n this._teardown();\n this._setState(\"ended\");\n\n // If start() is still pending, it rejects on failure and resolves on hangup().\n if (this._pendingStart !== undefined) {\n if (error) {\n this._pendingStart.reject(error);\n } else {\n this._pendingStart.resolve();\n }\n this._pendingStart = undefined;\n }\n }\n\n private _teardown() {\n if (this._sock) {\n // Don't fire close handlers for a socket we closed ourselves.\n this._sock.onclose = null;\n this._sock.close();\n this._sock = undefined;\n }\n if (this._peerConnection !== undefined) {\n this._peerConnection.close();\n this._peerConnection = undefined;\n }\n for (const track of this._localStream?.getTracks() ?? []) {\n track.stop();\n }\n this._localStream = undefined;\n this._remoteStream = undefined;\n this._remoteAudio.srcObject = null;\n this._remoteAudio.remove();\n }\n}\n", "// Seconds from the start of bar 0 to the given 0-indexed beat.\nfunction beatTime(beat: number, bpm: number): number {\n return beat * (60 / bpm);\n}\n\nexport interface ConnectingTone {\n start(): void;\n stop(): void;\n}\n\n// North American ringback: 440 + 480 Hz, 2s on, 4s off.\nexport class RingbackTone implements ConnectingTone {\n private context: AudioContext | null = null;\n private intervalId: ReturnType<typeof setInterval> | null = null;\n\n private readonly volume = 0.15;\n private readonly onSeconds = 2;\n private readonly periodSeconds = 6;\n // Delay before the first ring, so near-instant connections stay silent.\n private readonly startDelay = 0.3;\n\n start(): void {\n if (this.context) return;\n\n const context = new AudioContext();\n const gain = context.createGain();\n gain.gain.value = 0;\n gain.connect(context.destination);\n\n for (const frequency of [440, 480]) {\n const oscillator = context.createOscillator();\n oscillator.frequency.value = frequency;\n oscillator.connect(gain);\n oscillator.start();\n }\n\n let nextRing = context.currentTime + this.startDelay;\n const ring = () => {\n gain.gain.setValueAtTime(this.volume, nextRing);\n gain.gain.setValueAtTime(0, nextRing + this.onSeconds);\n nextRing += this.periodSeconds;\n };\n ring();\n this.intervalId = setInterval(ring, this.periodSeconds * 1000);\n this.context = context;\n }\n\n stop(): void {\n if (!this.context) return;\n\n if (this.intervalId) {\n clearInterval(this.intervalId);\n this.intervalId = null;\n }\n // Closing the context stops the oscillators.\n void this.context.close();\n this.context = null;\n }\n}\n\nexport class JingleTone implements ConnectingTone {\n private context: AudioContext | null = null;\n private masterGain: GainNode | null = null;\n private intervalId: ReturnType<typeof setInterval> | null = null;\n private running = false;\n private pulseCount = 0;\n // Audio-clock time of the next bar to schedule.\n private nextBarStart = 0;\n\n // Configuration\n private readonly volume = 0.25;\n private readonly bpm = 120;\n private readonly beatsPerBar = 4;\n // Delay before the first note, so near-instant connections stay silent.\n private readonly startDelay = 0.3;\n\n start(): void {\n if (this.running) return;\n\n this.running = true;\n this.pulseCount = 0;\n this.context = new AudioContext();\n this.masterGain = this.context.createGain();\n this.masterGain.gain.value = this.volume;\n this.masterGain.connect(this.context.destination);\n this.nextBarStart = this.context.currentTime + this.startDelay;\n\n // Stay one bar ahead so timer jitter doesn't shift the beat.\n this.playPulse();\n this.playPulse();\n\n const barLength = beatTime(this.beatsPerBar, this.bpm);\n this.intervalId = setInterval(() => {\n this.playPulse();\n }, barLength * 1000);\n }\n\n private noteToFrequency(note: string): number {\n const match = note.match(/^([A-G])(#|b)?(-?\\d+)$/);\n\n if (!match) {\n throw new Error(`Invalid note: ${note}`);\n }\n\n const [, name, accidental, octave] = match;\n\n const semitones: Record<string, number> = {\n C: 0,\n D: 2,\n E: 4,\n F: 5,\n G: 7,\n A: 9,\n B: 11,\n };\n\n const offset = accidental === \"#\" ? 1 : accidental === \"b\" ? -1 : 0;\n const midi = (Number(octave) + 1) * 12 + semitones[name] + offset;\n\n return 440 * Math.pow(2, (midi - 69) / 12);\n }\n\n stop(): void {\n if (!this.running) return;\n\n this.running = false;\n\n if (this.intervalId) {\n clearInterval(this.intervalId);\n this.intervalId = null;\n }\n\n const context = this.context;\n const gain = this.masterGain;\n\n if (!context || !gain) return;\n\n // Smooth fade-out\n const now = context.currentTime;\n gain.gain.setValueAtTime(gain.gain.value, now);\n gain.gain.linearRampToValueAtTime(0, now + 0.15);\n\n setTimeout(() => {\n void context.close();\n }, 200);\n\n this.context = null;\n this.masterGain = null;\n }\n\n private playPulse(): void {\n if (!this.context || !this.masterGain) return;\n\n const bar = this.nextBarStart;\n const beat = (n: number) => bar + beatTime(n, this.bpm);\n const beatLength = beatTime(1, this.bpm);\n\n // Notes on beats 1-3, silence on beat 4.\n if (this.pulseCount % 4 === 0 || this.pulseCount % 4 === 2) {\n this.playNote(\"C5\", beat(0), beatLength);\n this.playNote(\"E5\", beat(0.5), beatLength);\n this.playNote(\"G4\", beat(1), beatLength);\n } else if (this.pulseCount % 4 === 3) {\n this.playNote(\"E5\", beat(0), beatLength);\n this.playNote(\"G5\", beat(0.5), beatLength);\n this.playNote(\"C5\", beat(1), beatLength);\n } else {\n this.playNote(\"E5\", beat(0), beatLength);\n this.playNote(\"G4\", beat(0.5), beatLength);\n this.playNote(\"C5\", beat(1), beatLength);\n }\n this.pulseCount++;\n this.nextBarStart = beat(this.beatsPerBar);\n }\n\n private playNote(note: string, startTime: number, duration: number): void {\n if (!this.context || !this.masterGain) return;\n\n const oscillator = this.context.createOscillator();\n const envelope = this.context.createGain();\n\n oscillator.type = \"sine\";\n oscillator.frequency.value = this.noteToFrequency(note);\n\n envelope.gain.setValueAtTime(0, startTime);\n envelope.gain.linearRampToValueAtTime(0.7, startTime + 0.05);\n envelope.gain.exponentialRampToValueAtTime(0.001, startTime + duration);\n\n oscillator.connect(envelope);\n envelope.connect(this.masterGain);\n\n oscillator.start(startTime);\n oscillator.stop(startTime + duration + 0.01);\n\n oscillator.onended = () => {\n oscillator.disconnect();\n envelope.disconnect();\n };\n }\n}\n"],
5
- "mappings": "ojBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,UAAAE,EAAA,cAAAC,EAAA,eAAAC,EAAA,iBAAAC,ICCA,SAASC,EAASC,EAAcC,EAAqB,CACnD,OAAOD,GAAQ,GAAKC,EACtB,CAQO,IAAMC,EAAN,KAA6C,CAA7C,cACLC,EAAA,KAAQ,UAA+B,MACvCA,EAAA,KAAQ,aAAoD,MAE5DA,EAAA,KAAiB,SAAS,KAC1BA,EAAA,KAAiB,YAAY,GAC7BA,EAAA,KAAiB,gBAAgB,GAEjCA,EAAA,KAAiB,aAAa,IAE9B,OAAc,CACZ,GAAI,KAAK,QAAS,OAElB,IAAMC,EAAU,IAAI,aACdC,EAAOD,EAAQ,WAAW,EAChCC,EAAK,KAAK,M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,OAAO,EAGlB,KAAK,gBAAkB,SACrBA,EACF,KAAK,cAAc,OAAOA,CAAK,EAE/B,KAAK,cAAc,QAAQ,EAE7B,KAAK,cAAgB,QAEzB,CAEQ,WAAY,CACd,KAAK,QAEP,KAAK,MAAM,QAAU,KACrB,KAAK,MAAM,MAAM,EACjB,KAAK,MAAQ,QAEX,KAAK,kBAAoB,SAC3B,KAAK,gBAAgB,MAAM,EAC3B,KAAK,gBAAkB,QAEzB,QAAWM,KAAS,KAAK,cAAc,UAAU,GAAK,CAAC,EACrDA,EAAM,KAAK,EAEb,KAAK,aAAe,OACpB,KAAK,cAAgB,OACrB,KAAK,aAAa,UAAY,KAC9B,KAAK,aAAa,OAAO,CAC3B,CACF",
6
- "names": ["index_exports", "__export", "Call", "CallError", "JingleTone", "RingbackTone", "beatTime", "beat", "bpm", "RingbackTone", "__publicField", "context", "gain", "frequency", "oscillator", "nextRing", "ring", "JingleTone", "barLength", "note", "match", "name", "accidental", "octave", "semitones", "offset", "midi", "now", "bar", "n", "beatLength", "startTime", "duration", "envelope", "CallError", "code", "message", "options", "__publicField", "DEFAULT_ICE_SERVERS", "SOCKET_PROTOCOLS", "Call", "webrtcCode", "baseUrl", "connectingTone", "JingleTone", "sockProtocol", "handler", "state", "permissionTimer", "error", "callError", "started", "resolve", "reject", "event", "track"]
4
+ "sourcesContent": ["import { type ConnectingTone, JingleTone, LeavingTone, type Tone } from \"./tones.ts\";\n\nexport { type ConnectingTone, JingleTone, RingbackTone, LeavingTone, type Tone } from \"./tones.ts\";\n\nexport type CallState =\n | \"not-started\" // start() hasn't been called yet.\n | \"starting\" // Starting the call. Do not play a Ringback yet.\n | \"waiting-for-permission\" // We're waiting for the Microphone permission prompt. You may show a reminder UI here.\n | \"connecting\" // The call is now connection to the remote. This is where you can play a ringback or other connecting tone.\n | \"connected\" // The call is now connected and the audio channel is established.\n | \"ended\"; // The call has ended.\n\nexport type CallErrorCode =\n | \"MICROPHONE_ACCESS_FAILED\"\n | \"CONNECTION_FAILED\"\n | \"AUTHENTICATION_FAILED\"\n | \"PROTOCOL_ERROR\" // The server sent an unexpected message.\n | \"SERVER_ERROR\" // The server sent an error message.\n | \"REMOTE_HANGUP\" // The remote hung up before the call connected.\n | \"CONNECTION_TIMEOUT\"; // The call didn't connect within connectionTimeoutMs.\n\nexport class CallError extends Error {\n readonly code: CallErrorCode;\n\n constructor(code: CallErrorCode, message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"CallError\";\n this.code = code;\n }\n}\n\n// Used if the server doesn't send ICE servers with the auth reply.\nconst DEFAULT_ICE_SERVERS: RTCIceServer[] = [\n { urls: [\"stun:stun.l.google.com:19302\", \"stun:stun.sipgate.net:10000\"] },\n];\n\nconst SOCKET_PROTOCOLS: Record<string, string> = {\n \"http:\": \"ws:\",\n \"https:\": \"wss:\",\n \"ws:\": \"ws:\",\n \"wss:\": \"wss:\",\n};\n\n// Messages received on the signaling socket. See junkswitch/junkswitch/webrtcsocket.py.\ntype SignalingMessage =\n | {\n type: \"auth\";\n body: \"OK\" | \"FAIL\";\n reason?: string;\n ice_servers?: RTCIceServer[];\n }\n | { type: \"desc\"; body: RTCSessionDescriptionInit }\n | { type: \"candidate\"; body: RTCIceCandidateInit }\n | { type: \"hangup\" }\n | { type: \"error\"; body?: string }\n | { type: \"transfer\" };\n\nexport class Call {\n private _state: CallState = \"not-started\";\n private _error?: CallError;\n private readonly _stateHandlers = new Set<(state: CallState) => void>();\n private readonly _remoteStreamHandlers = new Set<(stream: MediaStream) => void>();\n private readonly _messageHandlers = new Set<(payload: Record<string, unknown>) => void>();\n private readonly _webrtcCode: string;\n private readonly _sockUrl: URL;\n\n private readonly _remoteAudio: HTMLAudioElement;\n private readonly _connectingTone?: ConnectingTone;\n private readonly _leavingTone?: Tone;\n private readonly _connectionTimeoutMs?: number;\n private readonly _customPayload: Record<string, unknown>;\n private _connectionTimer?: ReturnType<typeof setTimeout>;\n private _sock?: WebSocket;\n\n private readonly _chatSockUrl: URL;\n // Links the chat socket to this call's session on the server.\n private readonly _chatSessionId = crypto.randomUUID();\n private _chatSock?: WebSocket;\n private _chatReady = false;\n // Messages sent before the chat socket is ready.\n private _pendingChatMessages: string[] = [];\n\n private _localStream?: MediaStream;\n private _peerConnection?: RTCPeerConnection;\n private _remoteStream?: MediaStream;\n private _iceServers?: RTCIceServer[];\n // Settles the promise returned by start().\n private _pendingStart?: { resolve: () => void; reject: (error: CallError) => void };\n\n constructor({\n webrtcCode,\n baseUrl = \"https://app.goguava.ai/\",\n connectingTone = new JingleTone(),\n leavingTone = new LeavingTone(),\n connectionTimeoutMs = 30_000,\n customPayload = {},\n }: {\n /** Code used to authenticate with the signaling server. */\n webrtcCode: string;\n /** Base URL of the Guava server. Defaults to `https://app.goguava.ai/`. */\n baseUrl?: string;\n /** Played while the call is in the \"connecting\" state. Defaults to {@link JingleTone}. */\n connectingTone?: ConnectingTone | \"disabled\";\n /** Played when the call ends. Defaults to {@link LeavingTone}. */\n leavingTone?: Tone | \"disabled\";\n /** Max time in the \"connecting\" state before the call fails with CONNECTION_TIMEOUT. Defaults to 30 seconds. */\n connectionTimeoutMs?: number | \"disabled\";\n /** A custom payload that is forwarded to the agent at the start of the call. */\n customPayload?: Record<string, unknown>;\n }) {\n this._webrtcCode = webrtcCode;\n this._customPayload = customPayload;\n if (connectingTone !== \"disabled\") {\n this._connectingTone = connectingTone;\n }\n if (leavingTone !== \"disabled\") {\n this._leavingTone = leavingTone;\n }\n if (connectionTimeoutMs !== \"disabled\") {\n this._connectionTimeoutMs = connectionTimeoutMs;\n }\n\n this._sockUrl = new URL(\"/webrtc/\", baseUrl);\n const sockProtocol = SOCKET_PROTOCOLS[this._sockUrl.protocol];\n if (sockProtocol === undefined) {\n throw new Error(`Unsupported baseUrl protocol: ${this._sockUrl.protocol}`);\n }\n this._sockUrl.protocol = sockProtocol;\n this._chatSockUrl = new URL(\"/webrtc-chat/\", this._sockUrl);\n\n this._remoteAudio = document.createElement(\"audio\");\n this._remoteAudio.autoplay = true;\n }\n\n get state(): CallState {\n return this._state;\n }\n\n // Set if the call ended due to a failure.\n get error(): CallError | undefined {\n return this._error;\n }\n\n // Set once the remote audio track arrives, which happens before the call reaches \"connected\".\n get remoteStream(): MediaStream | undefined {\n return this._remoteStream;\n }\n\n // Returns a function that unregisters the handler.\n onStateChange(handler: (state: CallState) => void): () => void {\n this._stateHandlers.add(handler);\n return () => {\n this._stateHandlers.delete(handler);\n };\n }\n\n // Fires whenever the remote stream is replaced, including the first time it arrives.\n // Returns a function that unregisters the handler.\n onRemoteStream(handler: (stream: MediaStream) => void): () => void {\n this._remoteStreamHandlers.add(handler);\n return () => {\n this._remoteStreamHandlers.delete(handler);\n };\n }\n\n /**\n * Registers a handler for messages the agent sends with `call.send_webrtc_message()`.\n * @param handler - Called with each message's payload.\n * @returns A function that unregisters the handler.\n */\n onMessage(handler: (payload: Record<string, unknown>) => void): () => void {\n this._messageHandlers.add(handler);\n return () => {\n this._messageHandlers.delete(handler);\n };\n }\n\n private _setState(state: CallState) {\n if (state === this._state) {\n return;\n }\n\n if (state === \"ended\") {\n this._leavingTone?.play();\n }\n\n this._state = state;\n if (state === \"connecting\") {\n this._connectingTone?.start();\n if (this._connectionTimeoutMs !== undefined) {\n this._connectionTimer = setTimeout(() => {\n this._send({ type: \"hangup\" });\n this._end(new CallError(\"CONNECTION_TIMEOUT\", \"Timed out connecting the call.\"));\n }, this._connectionTimeoutMs);\n }\n } else {\n this._connectingTone?.stop();\n clearTimeout(this._connectionTimer);\n this._connectionTimer = undefined;\n }\n for (const handler of this._stateHandlers) {\n handler(state);\n }\n }\n\n async start() {\n if (this._state !== \"not-started\") {\n throw new Error(`start() was already called.`);\n }\n this._setState(\"starting\");\n\n // Add the audio element.\n document.body.appendChild(this._remoteAudio);\n\n // We start by requesting device permissions first. The idea is that, after permission has been granted,\n // it makes sence to play a ringback, but it doesn't make sense to do so during the permission prompt.\n\n const permissionTimer = setTimeout(() => {\n this._setState(\"waiting-for-permission\");\n }, 500);\n\n try {\n this._localStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });\n } catch (error) {\n const callError = new CallError(\"MICROPHONE_ACCESS_FAILED\", \"Failed to access microphone.\", {\n cause: error,\n });\n this._end(callError);\n throw callError;\n } finally {\n clearTimeout(permissionTimer);\n }\n\n // hangup() was called during the permission prompt.\n if (this.state === \"ended\") {\n this._teardown();\n return;\n }\n\n // Now open and authenticate the signaling socket.\n this._setState(\"connecting\"); // UI can start playing rinback tones here.\n const started = new Promise<void>((resolve, reject) => {\n this._pendingStart = { resolve, reject };\n });\n\n this._sock = new WebSocket(this._sockUrl);\n this._sock.onopen = () => {\n this._send({\n type: \"auth\",\n body: JSON.stringify({\n webrtc_code: this._webrtcCode,\n chat_session_id: this._chatSessionId,\n }),\n });\n };\n this._sock.onmessage = (event) => {\n this._onMessage(JSON.parse(event.data) as SignalingMessage);\n };\n this._sock.onclose = () => {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Signaling connection closed.\"));\n };\n\n await started;\n }\n\n /**\n * Sends a message to the agent, delivered to its `on_webrtc_message` handler.\n * Messages sent before the message channel is ready are queued.\n * @param payload - A JSON-serializable object.\n */\n sendMessage(payload: Record<string, unknown>) {\n if (this._state === \"not-started\" || this._state === \"ended\") {\n throw new Error(`sendMessage() called while the call is ${this._state}.`);\n }\n const message = JSON.stringify({ type: \"webrtc-message\", payload });\n if (this._chatReady && this._chatSock !== undefined) {\n this._chatSock.send(message);\n } else {\n this._pendingChatMessages.push(message);\n }\n }\n\n private _openChatSocket() {\n this._chatSock = new WebSocket(this._chatSockUrl);\n this._chatSock.onopen = () => {\n this._chatSock?.send(\n JSON.stringify({ webrtc_code: this._webrtcCode, chat_session_id: this._chatSessionId }),\n );\n };\n this._chatSock.onmessage = (event) => {\n const message = JSON.parse(event.data) as {\n type?: string;\n command_type?: string;\n payload?: Record<string, unknown>;\n };\n // The server sends \"ready\" once the call session is found.\n if (message.type === \"ready\" && !this._chatReady) {\n this._chatReady = true;\n for (const pending of this._pendingChatMessages) {\n this._chatSock?.send(pending);\n }\n this._pendingChatMessages = [];\n } else if (message.command_type === \"send-webrtc-message\") {\n for (const handler of this._messageHandlers) {\n handler(message.payload ?? {});\n }\n }\n };\n }\n\n async hangup() {\n if (this._state === \"not-started\") {\n throw new Error(`hangup() called before start().`);\n }\n if (this._state === \"ended\") {\n return;\n }\n this._send({ type: \"hangup\" });\n this._end();\n }\n\n private _send(message: { type: string; body?: unknown }) {\n if (this._sock !== undefined && this._sock.readyState === WebSocket.OPEN) {\n this._sock.send(JSON.stringify(message));\n }\n }\n\n private _onMessage(message: SignalingMessage) {\n switch (message.type) {\n case \"auth\":\n if (message.body !== \"OK\") {\n this._end(\n new CallError(\"AUTHENTICATION_FAILED\", message.reason || \"Authentication failed.\"),\n );\n return;\n }\n this._iceServers = message.ice_servers;\n this._openChatSocket();\n this._send({ type: \"connect\", body: { custom_payload: this._customPayload } });\n void this._connectPeer();\n break;\n case \"desc\":\n if (!this._peerConnection) {\n this._end(\n new CallError(\n \"PROTOCOL_ERROR\",\n \"Received desc before the peer connection was created.\",\n ),\n );\n return;\n }\n this._peerConnection.setRemoteDescription(message.body).catch((error) => {\n this._end(\n new CallError(\"CONNECTION_FAILED\", \"Failed to set remote description.\", {\n cause: error,\n }),\n );\n });\n break;\n case \"candidate\":\n if (!this._peerConnection) {\n this._end(\n new CallError(\n \"PROTOCOL_ERROR\",\n \"Received candidate before the peer connection was created.\",\n ),\n );\n return;\n }\n this._peerConnection.addIceCandidate(message.body).catch((error) => {\n console.warn(\"Failed to add ICE candidate:\", error);\n });\n break;\n case \"hangup\":\n if (this._state === \"connected\") {\n this._end();\n } else {\n this._end(\n new CallError(\"REMOTE_HANGUP\", \"The remote hung up before the call connected.\"),\n );\n }\n break;\n case \"error\":\n this._end(new CallError(\"SERVER_ERROR\", message.body || \"Unknown server error.\"));\n break;\n case \"transfer\":\n this._end(new CallError(\"PROTOCOL_ERROR\", \"Transfers are not supported.\"));\n break;\n default:\n console.warn(\"Unknown signaling message:\", message);\n }\n }\n\n private async _connectPeer() {\n if (!this._localStream) {\n return;\n }\n this._peerConnection = new RTCPeerConnection({\n iceServers: this._iceServers ?? DEFAULT_ICE_SERVERS,\n });\n this._peerConnection.onicecandidate = (event) => {\n if (event.candidate) {\n this._send({ type: \"candidate\", body: event.candidate });\n }\n };\n this._peerConnection.ontrack = (event) => {\n this._remoteStream = new MediaStream([event.track]);\n this._remoteAudio.srcObject = this._remoteStream;\n for (const handler of this._remoteStreamHandlers) {\n handler(this._remoteStream);\n }\n };\n this._peerConnection.onconnectionstatechange = () => {\n if (this._peerConnection === undefined) {\n return;\n }\n // \"disconnected\" is often transient, so wait for \"failed\" before ending the call.\n if (this._peerConnection.connectionState === \"connected\") {\n this._setState(\"connected\");\n if (this._pendingStart !== undefined) {\n this._pendingStart.resolve();\n this._pendingStart = undefined;\n }\n } else if (this._peerConnection.connectionState === \"failed\") {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Peer connection failed.\"));\n }\n };\n for (const track of this._localStream.getAudioTracks()) {\n this._peerConnection.addTrack(track);\n }\n\n try {\n await this._peerConnection.setLocalDescription();\n } catch (error) {\n this._end(new CallError(\"CONNECTION_FAILED\", \"Failed to create offer.\", { cause: error }));\n return;\n }\n\n // The call may have ended while creating the offer.\n if (this.state === \"ended\") {\n return;\n }\n this._send({ type: \"desc\", body: this._peerConnection.localDescription });\n }\n\n private _end(error?: CallError) {\n if (this._state === \"ended\") {\n return;\n }\n this._error = error;\n this._teardown();\n this._setState(\"ended\");\n\n // If start() is still pending, it rejects on failure and resolves on hangup().\n if (this._pendingStart !== undefined) {\n if (error) {\n this._pendingStart.reject(error);\n } else {\n this._pendingStart.resolve();\n }\n this._pendingStart = undefined;\n }\n }\n\n private _teardown() {\n // Close the signaling socket.\n if (this._sock) {\n // Don't fire close handlers for a socket we closed ourselves.\n this._sock.onclose = null;\n this._sock.close();\n this._sock = undefined;\n }\n\n // Close the chat socket.\n if (this._chatSock) {\n this._chatSock.close();\n this._chatSock = undefined;\n }\n this._chatReady = false;\n this._pendingChatMessages = [];\n\n // Close the peer connection.\n if (this._peerConnection !== undefined) {\n this._peerConnection.close();\n this._peerConnection = undefined;\n }\n for (const track of this._localStream?.getTracks() ?? []) {\n track.stop();\n }\n this._localStream = undefined;\n this._remoteStream = undefined;\n\n // Remove the audio element.\n this._remoteAudio.srcObject = null;\n this._remoteAudio.remove();\n }\n}\n", "// Seconds from the start of bar 0 to the given 0-indexed beat.\nfunction beatTime(beat: number, bpm: number): number {\n return beat * (60 / bpm);\n}\n\nexport interface ConnectingTone {\n start(): void;\n stop(): void;\n}\n\n// North American ringback: 440 + 480 Hz, 2s on, 4s off.\nexport class RingbackTone implements ConnectingTone {\n private context: AudioContext | null = null;\n private intervalId: ReturnType<typeof setInterval> | null = null;\n\n private readonly volume = 0.15;\n private readonly onSeconds = 2;\n private readonly periodSeconds = 6;\n // Delay before the first ring, so near-instant connections stay silent.\n private readonly startDelay = 0.3;\n\n start(): void {\n if (this.context) return;\n\n const context = new AudioContext();\n const gain = context.createGain();\n gain.gain.value = 0;\n gain.connect(context.destination);\n\n for (const frequency of [440, 480]) {\n const oscillator = context.createOscillator();\n oscillator.frequency.value = frequency;\n oscillator.connect(gain);\n oscillator.start();\n }\n\n let nextRing = context.currentTime + this.startDelay;\n const ring = () => {\n gain.gain.setValueAtTime(this.volume, nextRing);\n gain.gain.setValueAtTime(0, nextRing + this.onSeconds);\n nextRing += this.periodSeconds;\n };\n ring();\n this.intervalId = setInterval(ring, this.periodSeconds * 1000);\n this.context = context;\n }\n\n stop(): void {\n if (!this.context) return;\n\n if (this.intervalId) {\n clearInterval(this.intervalId);\n this.intervalId = null;\n }\n // Closing the context stops the oscillators.\n void this.context.close();\n this.context = null;\n }\n}\n\nexport class JingleTone implements ConnectingTone {\n private context: AudioContext | null = null;\n private masterGain: GainNode | null = null;\n private intervalId: ReturnType<typeof setInterval> | null = null;\n private running = false;\n private pulseCount = 0;\n // Audio-clock time of the next bar to schedule.\n private nextBarStart = 0;\n\n // Configuration\n private readonly volume = 0.3;\n private readonly bpm = 120;\n private readonly beatsPerBar = 4;\n // Delay before the first note, so near-instant connections stay silent.\n private readonly startDelay = 0.3;\n\n start(): void {\n if (this.running) return;\n\n this.running = true;\n this.pulseCount = 0;\n this.context = new AudioContext();\n this.masterGain = this.context.createGain();\n this.masterGain.gain.value = this.volume;\n this.masterGain.connect(this.context.destination);\n this.nextBarStart = this.context.currentTime + this.startDelay;\n\n // Stay one bar ahead so timer jitter doesn't shift the beat.\n this.playPulse();\n this.playPulse();\n\n const barLength = beatTime(this.beatsPerBar, this.bpm);\n this.intervalId = setInterval(() => {\n this.playPulse();\n }, barLength * 1000);\n }\n\n private noteToFrequency(note: string): number {\n const match = note.match(/^([A-G])(#|b)?(-?\\d+)$/);\n\n if (!match) {\n throw new Error(`Invalid note: ${note}`);\n }\n\n const [, name, accidental, octave] = match;\n\n const semitones: Record<string, number> = {\n C: 0,\n D: 2,\n E: 4,\n F: 5,\n G: 7,\n A: 9,\n B: 11,\n };\n\n const offset = accidental === \"#\" ? 1 : accidental === \"b\" ? -1 : 0;\n const midi = (Number(octave) + 1) * 12 + semitones[name] + offset;\n\n return 440 * Math.pow(2, (midi - 69) / 12);\n }\n\n stop(): void {\n if (!this.running) return;\n\n this.running = false;\n\n if (this.intervalId) {\n clearInterval(this.intervalId);\n this.intervalId = null;\n }\n\n const context = this.context;\n const gain = this.masterGain;\n\n if (!context || !gain) return;\n\n // Smooth fade-out\n const now = context.currentTime;\n gain.gain.setValueAtTime(gain.gain.value, now);\n gain.gain.linearRampToValueAtTime(0, now + 0.15);\n\n setTimeout(() => {\n void context.close();\n }, 200);\n\n this.context = null;\n this.masterGain = null;\n }\n\n private playPulse(): void {\n if (!this.context || !this.masterGain) return;\n\n const bar = this.nextBarStart;\n const beat = (n: number) => bar + beatTime(n, this.bpm);\n const beatLength = beatTime(1, this.bpm);\n\n // Notes on beats 1-3, silence on beat 4.\n if (this.pulseCount % 4 === 0 || this.pulseCount % 4 === 2) {\n this.playNote(\"C5\", beat(0), beatLength);\n this.playNote(\"E5\", beat(0.5), beatLength);\n this.playNote(\"G4\", beat(1), beatLength);\n } else if (this.pulseCount % 4 === 3) {\n this.playNote(\"E5\", beat(0), beatLength);\n this.playNote(\"G5\", beat(0.5), beatLength);\n this.playNote(\"C5\", beat(1), beatLength);\n } else {\n this.playNote(\"E5\", beat(0), beatLength);\n this.playNote(\"G4\", beat(0.5), beatLength);\n this.playNote(\"C5\", beat(1), beatLength);\n }\n this.pulseCount++;\n this.nextBarStart = beat(this.beatsPerBar);\n }\n\n private playNote(note: string, startTime: number, duration: number): void {\n if (!this.context || !this.masterGain) return;\n\n const oscillator = this.context.createOscillator();\n const envelope = this.context.createGain();\n\n oscillator.type = \"sine\";\n oscillator.frequency.value = this.noteToFrequency(note);\n\n envelope.gain.setValueAtTime(0, startTime);\n envelope.gain.linearRampToValueAtTime(0.7, startTime + 0.05);\n envelope.gain.exponentialRampToValueAtTime(0.001, startTime + duration);\n\n oscillator.connect(envelope);\n envelope.connect(this.masterGain);\n\n oscillator.start(startTime);\n oscillator.stop(startTime + duration + 0.01);\n\n oscillator.onended = () => {\n oscillator.disconnect();\n envelope.disconnect();\n };\n }\n}\n\nexport interface Tone {\n play(): void;\n}\n\nexport class LeavingTone implements Tone {\n public play(): void {\n const ctx = new AudioContext();\n\n if (ctx.state === \"suspended\") {\n void ctx.resume();\n }\n\n const now = ctx.currentTime;\n\n // First note: D4\n const first = this.playNote(ctx, {\n frequency: 293.665,\n startTime: now,\n attack: 0.065,\n decay: 0.085,\n volume: 0.2,\n });\n\n // Second note: A3, starts 139ms later\n const second = this.playNote(ctx, {\n frequency: 220.0,\n startTime: now + 0.139,\n attack: 0.018,\n decay: 0.075,\n volume: 0.2,\n });\n\n void Promise.all([first, second]).then(() => ctx.close());\n }\n\n // Resolves once the note has finished playing.\n private playNote(\n ctx: AudioContext,\n {\n frequency,\n startTime,\n attack,\n decay,\n volume,\n }: {\n frequency: number;\n startTime: number;\n attack: number;\n decay: number;\n volume: number;\n },\n ): Promise<void> {\n const oscillator = ctx.createOscillator();\n const gain = ctx.createGain();\n\n oscillator.type = \"sine\";\n oscillator.frequency.setValueAtTime(frequency, startTime);\n\n // Smooth onset\n gain.gain.setValueAtTime(0, startTime);\n gain.gain.linearRampToValueAtTime(volume, startTime + attack);\n\n // Exponential decay\n const peakTime = startTime + attack;\n const endTime = peakTime + decay * 5;\n\n gain.gain.setTargetAtTime(0.00001, peakTime, decay);\n\n oscillator.connect(gain);\n gain.connect(ctx.destination);\n\n oscillator.start(startTime);\n oscillator.stop(endTime);\n\n return new Promise((resolve) => {\n oscillator.onended = () => {\n oscillator.disconnect();\n gain.disconnect();\n resolve();\n };\n });\n }\n}\n"],
5
+ "mappings": "ojBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,UAAAE,EAAA,cAAAC,EAAA,eAAAC,EAAA,gBAAAC,EAAA,iBAAAC,ICCA,SAASC,EAASC,EAAcC,EAAqB,CACnD,OAAOD,GAAQ,GAAKC,EACtB,CAQO,IAAMC,EAAN,KAA6C,CAA7C,cACLC,EAAA,KAAQ,UAA+B,MACvCA,EAAA,KAAQ,aAAoD,MAE5DA,EAAA,KAAiB,SAAS,KAC1BA,EAAA,KAAiB,YAAY,GAC7BA,EAAA,KAAiB,gBAAgB,GAEjCA,EAAA,KAAiB,aAAa,IAE9B,OAAc,CACZ,GAAI,KAAK,QAAS,OAElB,IAAMC,EAAU,IAAI,aACdC,EAAOD,EAAQ,WAAW,EAChCC,EAAK,KAAK,MAAQ,EAClBA,EAAK,QAAQD,EAAQ,WAAW,EAEhC,QAAWE,IAAa,CAAC,IAAK,GAAG,EAAG,CAClC,IAAMC,EAAaH,EAAQ,iBAAiB,EAC5CG,EAAW,UAAU,MAAQD,EAC7BC,EAAW,QAAQF,CAAI,EACvBE,EAAW,MAAM,CACnB,CAEA,IAAIC,EAAWJ,EAAQ,YAAc,KAAK,WACpCK,EAAO,I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SAGnB,KAAK,MAAM,CAAE,KAAM,OAAQ,KAAM,KAAK,gBAAgB,gBAAiB,CAAC,EAC1E,CAEQ,KAAKA,EAAmB,CAC1B,KAAK,SAAW,UAGpB,KAAK,OAASA,EACd,KAAK,UAAU,EACf,KAAK,UAAU,OAAO,EAGlB,KAAK,gBAAkB,SACrBA,EACF,KAAK,cAAc,OAAOA,CAAK,EAE/B,KAAK,cAAc,QAAQ,EAE7B,KAAK,cAAgB,QAEzB,CAEQ,WAAY,CAEd,KAAK,QAEP,KAAK,MAAM,QAAU,KACrB,KAAK,MAAM,MAAM,EACjB,KAAK,MAAQ,QAIX,KAAK,YACP,KAAK,UAAU,MAAM,EACrB,KAAK,UAAY,QAEnB,KAAK,WAAa,GAClB,KAAK,qBAAuB,CAAC,EAGzB,KAAK,kBAAoB,SAC3B,KAAK,gBAAgB,MAAM,EAC3B,KAAK,gBAAkB,QAEzB,QAAWQ,KAAS,KAAK,cAAc,UAAU,GAAK,CAAC,EACrDA,EAAM,KAAK,EAEb,KAAK,aAAe,OACpB,KAAK,cAAgB,OAGrB,KAAK,aAAa,UAAY,KAC9B,KAAK,aAAa,OAAO,CAC3B,CACF",
6
+ "names": ["index_exports", "__export", "Call", "CallError", "JingleTone", "LeavingTone", "RingbackTone", "beatTime", "beat", "bpm", "RingbackTone", "__publicField", "context", "gain", "frequency", "oscillator", "nextRing", "ring", "JingleTone", "barLength", "note", "match", "name", "accidental", "octave", "semitones", "offset", "midi", "now", "bar", "n", "beatLength", "startTime", "duration", "envelope", "LeavingTone", "ctx", "first", "second", "attack", "decay", "volume", "peakTime", "endTime", "resolve", "CallError", "code", "message", "options", "__publicField", "DEFAULT_ICE_SERVERS", "SOCKET_PROTOCOLS", "Call", "webrtcCode", "baseUrl", "connectingTone", "JingleTone", "leavingTone", "LeavingTone", "connectionTimeoutMs", "customPayload", "sockProtocol", "handler", "state", "permissionTimer", "error", "callError", "started", "resolve", "reject", "event", "payload", "pending", "track"]
7
7
  }
@@ -9,6 +9,7 @@ export type PSTNCallInfo = z.infer<typeof PSTNCallInfo>;
9
9
  export declare const WebRTCCallInfo: z.ZodObject<{
10
10
  call_type: z.ZodLiteral<"webrtc">;
11
11
  webrtc_code: z.ZodString;
12
+ custom_payload: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
12
13
  }, z.core.$strip>;
13
14
  export type WebRTCCallInfo = z.infer<typeof WebRTCCallInfo>;
14
15
  export declare class SIPCallInfo {
@@ -28,6 +29,7 @@ export declare const CallInfo: z.ZodDiscriminatedUnion<[z.ZodObject<{
28
29
  }, z.core.$strip>, z.ZodObject<{
29
30
  call_type: z.ZodLiteral<"webrtc">;
30
31
  webrtc_code: z.ZodString;
32
+ custom_payload: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
31
33
  }, z.core.$strip>, z.ZodPipe<z.ZodObject<{
32
34
  call_type: z.ZodLiteral<"sip">;
33
35
  from_aor: z.ZodString;
@@ -44,6 +44,7 @@ exports.PSTNCallInfo = z.object({
44
44
  exports.WebRTCCallInfo = z.object({
45
45
  call_type: z.literal("webrtc"),
46
46
  webrtc_code: z.string(),
47
+ custom_payload: z.record(z.string(), z.unknown()).default({}),
47
48
  });
48
49
  const SIPCallInfoSchema = z
49
50
  .object({
@@ -1 +1 @@
1
- {"version":3,"file":"call-info.js","sourceRoot":"","sources":["../../../src/socket/call-info.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAAyB;AAEZ,QAAA,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC;IACnC,SAAS,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC;IAC5B,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAClC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;IACrB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CACjC,CAAC,CAAC;AAGU,QAAA,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Y;IACZ,sBAAc;IACd,iBAAiB;CAClB,CAAC,CAAC"}
@@ -21,6 +21,7 @@ export declare const AssignCall: z.ZodObject<{
21
21
  }, z.core.$strip>, z.ZodObject<{
22
22
  call_type: z.ZodLiteral<"webrtc">;
23
23
  webrtc_code: z.ZodString;
24
+ custom_payload: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
24
25
  }, z.core.$strip>, z.ZodPipe<z.ZodObject<{
25
26
  call_type: z.ZodLiteral<"sip">;
26
27
  from_aor: z.ZodString;
@@ -51,6 +52,7 @@ export declare const ServerMessage: z.ZodDiscriminatedUnion<[z.ZodObject<{
51
52
  }, z.core.$strip>, z.ZodObject<{
52
53
  call_type: z.ZodLiteral<"webrtc">;
53
54
  webrtc_code: z.ZodString;
55
+ custom_payload: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
54
56
  }, z.core.$strip>, z.ZodPipe<z.ZodObject<{
55
57
  call_type: z.ZodLiteral<"sip">;
56
58
  from_aor: z.ZodString;
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.38.0";
1
+ export declare const SDK_VERSION = "0.39.0";
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = "0.38.0";
4
+ exports.SDK_VERSION = "0.39.0";
5
5
  //# sourceMappingURL=version.js.map