@vanira/sdk 0.0.25 → 0.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- (function(u,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(u=typeof globalThis<"u"?globalThis:u||self,f(u.VaniraAI={}))})(this,function(u){"use strict";var we=Object.defineProperty;var me=(u,f,v)=>f in u?we(u,f,{enumerable:!0,configurable:!0,writable:!0,value:v}):u[f]=v;var o=(u,f,v)=>me(u,typeof f!="symbol"?f+"":f,v);var f=typeof document<"u"?document.currentScript:null;class v{constructor(t){o(this,"serverUrl");o(this,"agentId");o(this,"callId");o(this,"apiKey");o(this,"backendUrl");o(this,"onConnected");o(this,"onDisconnected");o(this,"onError");o(this,"onTranscription");o(this,"onRemoteTrack");o(this,"onClientToolCall");o(this,"pc",null);o(this,"dataChannel",null);o(this,"audioElement",null);o(this,"connected",!1);if(!t.agentId)throw new Error("agentId is required");if(!t.serverUrl&&!t.apiKey)throw new Error("Provide either serverUrl or apiKey (to use createCall())");this.agentId=t.agentId,this.serverUrl=t.serverUrl?t.serverUrl.replace(/\/$/,""):"",this.callId=t.callId||this.generateCallId(),this.apiKey=t.apiKey,this.backendUrl=(t.backendUrl||"https://api.vanira.io").replace(/\/$/,""),this.onConnected=t.onConnected||(()=>{}),this.onDisconnected=t.onDisconnected||(()=>{}),this.onError=t.onError||(e=>console.error("[WebRTC]",e)),this.onTranscription=t.onTranscription||(()=>{}),this.onRemoteTrack=t.onRemoteTrack||(()=>{}),this.onClientToolCall=t.onClientToolCall||(()=>{})}async createCall(){var s;if(!this.apiKey)throw new Error("[VaniraAI] apiKey is required to use createCall()");const t={"Content-Type":"application/json","X-API-Key":this.apiKey},e=await fetch(`${this.backendUrl}/calls/create`,{method:"POST",headers:t,body:JSON.stringify({agent_id:this.agentId,type:"web"})});if(!e.ok){const n=await e.json().catch(()=>({}));throw new Error(`[VaniraAI] createCall failed (${e.status}): ${((s=n==null?void 0:n.detail)==null?void 0:s.message)||(n==null?void 0:n.message)||e.statusText}`)}const i=await e.json();if(!i.call_id||!i.worker_url)throw new Error("[VaniraAI] createCall response missing call_id or worker_url");this.callId=i.call_id,this.serverUrl=i.worker_url.replace(/\/$/,""),await this.connect()}async connect(){if(this.apiKey&&!this.serverUrl){await this.createCall();return}if(!this.serverUrl)throw new Error("[VaniraAI] serverUrl is missing. Provide apiKey or serverUrl.");console.log("đŸ”ĩ [WebRTC] Starting connection...");try{this.pc=new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},{urls:"stun:global.relay.metered.ca:80"},{urls:["turns:global.relay.metered.ca:443?transport=tcp","turn:global.relay.metered.ca:443?transport=tcp","turn:global.relay.metered.ca:80?transport=tcp"],username:"fa97658be3343d21da3b65e6",credential:"HXHDoqeHbvZrmCuf"}],iceTransportPolicy:"all"}),console.log("using ice servers:",[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"}]);const t=await navigator.mediaDevices.getUserMedia({audio:{echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0,sampleRate:{ideal:16e3},channelCount:1}});if(console.log("🎤 [WebRTC] Microphone access granted"),!this.pc){console.log("[WebRTC] Connection aborted: peer connection closed during setup"),t.getTracks().forEach(a=>a.stop());return}if(t.getTracks().forEach(a=>{var r;(r=this.pc)==null||r.addTrack(a,t)}),!this.pc)throw new Error("RTCPeerConnection was closed unexpectedly");this.dataChannel=this.pc.createDataChannel("control"),this.dataChannel.onopen=()=>console.log("📡 [WebRTC] DataChannel opened"),this.dataChannel.onmessage=a=>{if(typeof a.data=="string")try{this.handleControlEvent(JSON.parse(a.data))}catch{console.warn("[WebRTC] Failed to parse message:",a.data)}else if(a.data instanceof ArrayBuffer)try{const r=new TextDecoder().decode(a.data);try{const A=JSON.parse(r);A&&typeof A=="object"&&A.event==="client_tool_call"?(console.log("[VaniraAI] Safely decoding binary tool_call to JSON:",A),this.handleControlEvent(A)):console.log("[VaniraAI] Decoded JSON from binary (inspect only):",A)}catch{console.log("[VaniraAI] Decoded String from binary:",r)}}catch{console.log("[VaniraAI] Received binary data:",a.data.byteLength,"bytes (not decodable)")}else a.data instanceof Blob&&a.data.text().then(r=>{try{this.handleControlEvent(JSON.parse(r))}catch{console.warn("[WebRTC] Failed to parse blob data:",r)}})},this.dataChannel.onerror=a=>console.error("❌ [WebRTC] DataChannel error:",a),this.pc.ontrack=a=>{const r=a.track,A=a.streams[0];console.log(`đŸ“Ĩ [WebRTC] Received ${r.kind} track`),r.kind==="audio"?(console.log("🔊 [WebRTC] Received audio track from server"),this.audioElement=new Audio,this.audioElement.srcObject=A,this.audioElement.play().catch(c=>console.warn("Audio autoplay blocked:",c)),this.audioElement.onended=()=>{this.sendEvent("playedStream"),console.log("✅ [WebRTC] TTS playback complete")}):r.kind==="video"&&(console.log("📹 [WebRTC] Video track received"),this.onRemoteTrack(r,A))};const e=()=>{var A,c,l,d,g,w,p,I,x,Q,E;console.log("🔄 [WebRTC] State:",(A=this.pc)==null?void 0:A.connectionState,"| ICE:",(c=this.pc)==null?void 0:c.iceConnectionState);const a=((l=this.pc)==null?void 0:l.connectionState)==="connected"||((d=this.pc)==null?void 0:d.iceConnectionState)==="connected"||((g=this.pc)==null?void 0:g.iceConnectionState)==="completed",r=((w=this.pc)==null?void 0:w.connectionState)==="failed"||((p=this.pc)==null?void 0:p.iceConnectionState)==="failed"||((I=this.pc)==null?void 0:I.connectionState)==="closed"||((x=this.pc)==null?void 0:x.iceConnectionState)==="closed"||((Q=this.pc)==null?void 0:Q.connectionState)==="disconnected"||((E=this.pc)==null?void 0:E.iceConnectionState)==="disconnected";a&&!this.connected?(this.connected=!0,this.onConnected()):r&&this.connected&&(this.connected=!1,this.onDisconnected())};this.pc.onconnectionstatechange=e,this.pc.oniceconnectionstatechange=e;const i=await this.pc.createOffer();await this.pc.setLocalDescription(i),console.log("📝 [WebRTC] Created offer, waiting for ICE gathering..."),await this.waitForIceGathering(),console.log("🧊 [WebRTC] ICE gathering complete"),console.log("📤 [WebRTC] Sending offer via HTTP...");const s=await fetch(`${this.serverUrl}/webrtc?agent=${this.agentId}_${this.callId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({offer:this.pc.localDescription,agentId:this.agentId,callId:this.callId})});if(!s.ok){const a=await s.json();throw new Error(a.error||`HTTP ${s.status}`)}const{answer:n}=await s.json();console.log("đŸ“Ĩ [WebRTC] Received answer from server"),await this.pc.setRemoteDescription(n),console.log("✅ [WebRTC] Connection established!")}catch(t){throw console.error("❌ [WebRTC] Connection failed:",t),this.disconnect(),this.onError(t.message||t),t}}waitForIceGathering(){return new Promise(t=>{if(!this.pc)return t();if(this.pc.iceGatheringState==="complete")t();else{const e=()=>{var i,s;((i=this.pc)==null?void 0:i.iceGatheringState)==="complete"&&((s=this.pc)==null||s.removeEventListener("icegatheringstatechange",e),t())};this.pc.addEventListener("icegatheringstatechange",e),setTimeout(()=>{var i;(i=this.pc)==null||i.removeEventListener("icegatheringstatechange",e),console.warn("âš ī¸ [WebRTC] ICE gathering timeout, proceeding anyway"),t()},5e3)}})}sendEvent(t,e={}){var i;((i=this.dataChannel)==null?void 0:i.readyState)==="open"&&this.dataChannel.send(JSON.stringify({event:t,...e}))}handleControlEvent(t){var e;switch(t.event){case"clearAudio":console.log("🛑 [WebRTC] Interrupt: clearAudio received (leaving stream unpaused)");break;case"transcription":console.log("📝 [WebRTC] Transcription:",t.text),this.onTranscription(t.text,t.isFinal);break;case"mark":console.log("đŸˇī¸ [WebRTC] Mark:",t.name);break;case"client_tool_call":{const i=t.tool_call||t.data||t,s=(i==null?void 0:i.tool_call_id)||(i==null?void 0:i.call_id)||t.tool_call_id||"";s&&((e=this.dataChannel)==null?void 0:e.readyState)==="open"?(this.dataChannel.send(JSON.stringify({event:"client_tool_ack",data:{tool_call_id:s}})),console.log("✅ [VaniraAI] Sent client_tool_ack for:",s)):s||console.warn("âš ī¸ [VaniraAI] client_tool_call received without tool_call_id — ack skipped"),console.log("đŸ› ī¸ [VaniraAI] Client Tool Call:",t),this.onClientToolCall(i);break}default:console.log("â„šī¸ [WebRTC] Unknown event:",t.event)}}disconnect(){console.log("🔴 [WebRTC] Disconnecting..."),this.audioElement&&(this.audioElement.pause(),this.audioElement.srcObject=null),this.dataChannel&&(this.dataChannel.close(),this.dataChannel=null),this.pc&&(this.pc.getSenders().forEach(t=>{t.track&&t.track.stop()}),this.pc.close(),this.pc=null),this.connected=!1,this.onDisconnected()}generateCallId(){return"web_"+Date.now()+"_"+Math.random().toString(36).substr(2,8)}sendToolResult(t,e){this.sendEvent("client_tool_result",{call_id:t,result:e})}sendToolAck(t){var e;((e=this.dataChannel)==null?void 0:e.readyState)==="open"?(this.dataChannel.send(JSON.stringify({event:"client_tool_ack",data:{tool_call_id:t}})),console.log("✅ [VaniraAI] Sent client_tool_ack for:",t)):console.warn("âš ī¸ [VaniraAI] Cannot send tool_ack: DataChannel not open")}sendContextUpdate(t){this.sendEvent("client_context_update",{data:{context:t}})}sendActionTrigger(t,e={}){this.sendEvent("client_action_trigger",{data:{action_name:t,data:e}})}triggerActionInterrupt(){this.sendEvent("action_interrupt"),console.log("🛑 [VaniraAI] Triggered client-side action interrupt")}static async fetchIceServers(t,e="https://coredb.travelr.club/v1/graphql"){var i;try{const n=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",Authorization:t},body:JSON.stringify({query:`
1
+ (function(u,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(u=typeof globalThis<"u"?globalThis:u||self,f(u.VaniraAI={}))})(this,function(u){"use strict";var we=Object.defineProperty;var me=(u,f,v)=>f in u?we(u,f,{enumerable:!0,configurable:!0,writable:!0,value:v}):u[f]=v;var o=(u,f,v)=>me(u,typeof f!="symbol"?f+"":f,v);var f=typeof document<"u"?document.currentScript:null;class v{constructor(t){o(this,"serverUrl");o(this,"agentId");o(this,"callId");o(this,"apiKey");o(this,"backendUrl");o(this,"onConnected");o(this,"onDisconnected");o(this,"onError");o(this,"onTranscription");o(this,"onRemoteTrack");o(this,"onClientToolCall");o(this,"pc",null);o(this,"dataChannel",null);o(this,"audioElement",null);o(this,"connected",!1);if(!t.agentId)throw new Error("agentId is required");if(!t.serverUrl&&!t.apiKey)throw new Error("Provide either serverUrl or apiKey (to use createCall())");this.agentId=t.agentId,this.serverUrl=t.serverUrl?t.serverUrl.replace(/\/$/,""):"",this.callId=t.callId||this.generateCallId(),this.apiKey=t.apiKey,this.backendUrl=(t.backendUrl||"https://api.vanira.io").replace(/\/$/,""),this.onConnected=t.onConnected||(()=>{}),this.onDisconnected=t.onDisconnected||(()=>{}),this.onError=t.onError||(e=>console.error("[WebRTC]",e)),this.onTranscription=t.onTranscription||(()=>{}),this.onRemoteTrack=t.onRemoteTrack||(()=>{}),this.onClientToolCall=t.onClientToolCall||(()=>{})}async createCall(){var s;if(!this.apiKey)throw new Error("[VaniraAI] apiKey is required to use createCall()");const t={"Content-Type":"application/json","X-API-Key":this.apiKey},e=await fetch(`${this.backendUrl}/calls/create`,{method:"POST",headers:t,body:JSON.stringify({agent_id:this.agentId,type:"web"})});if(!e.ok){const n=await e.json().catch(()=>({}));throw new Error(`[VaniraAI] createCall failed (${e.status}): ${((s=n==null?void 0:n.detail)==null?void 0:s.message)||(n==null?void 0:n.message)||e.statusText}`)}const i=await e.json();if(!i.call_id||!i.worker_url)throw new Error("[VaniraAI] createCall response missing call_id or worker_url");this.callId=i.call_id,this.serverUrl=new URL(i.worker_url).origin,await this.connect()}async connect(){if(this.apiKey&&!this.serverUrl){await this.createCall();return}if(!this.serverUrl)throw new Error("[VaniraAI] serverUrl is missing. Provide apiKey or serverUrl.");console.log("đŸ”ĩ [WebRTC] Starting connection...");try{this.pc=new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},{urls:"stun:global.relay.metered.ca:80"},{urls:["turns:global.relay.metered.ca:443?transport=tcp","turn:global.relay.metered.ca:443?transport=tcp","turn:global.relay.metered.ca:80?transport=tcp"],username:"fa97658be3343d21da3b65e6",credential:"HXHDoqeHbvZrmCuf"}],iceTransportPolicy:"all"}),console.log("using ice servers:",[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"}]);const t=await navigator.mediaDevices.getUserMedia({audio:{echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0,sampleRate:{ideal:16e3},channelCount:1}});if(console.log("🎤 [WebRTC] Microphone access granted"),!this.pc){console.log("[WebRTC] Connection aborted: peer connection closed during setup"),t.getTracks().forEach(a=>a.stop());return}if(t.getTracks().forEach(a=>{var r;(r=this.pc)==null||r.addTrack(a,t)}),!this.pc)throw new Error("RTCPeerConnection was closed unexpectedly");this.dataChannel=this.pc.createDataChannel("control"),this.dataChannel.onopen=()=>console.log("📡 [WebRTC] DataChannel opened"),this.dataChannel.onmessage=a=>{if(typeof a.data=="string")try{this.handleControlEvent(JSON.parse(a.data))}catch{console.warn("[WebRTC] Failed to parse message:",a.data)}else if(a.data instanceof ArrayBuffer)try{const r=new TextDecoder().decode(a.data);try{const A=JSON.parse(r);A&&typeof A=="object"&&A.event==="client_tool_call"?(console.log("[VaniraAI] Safely decoding binary tool_call to JSON:",A),this.handleControlEvent(A)):console.log("[VaniraAI] Decoded JSON from binary (inspect only):",A)}catch{console.log("[VaniraAI] Decoded String from binary:",r)}}catch{console.log("[VaniraAI] Received binary data:",a.data.byteLength,"bytes (not decodable)")}else a.data instanceof Blob&&a.data.text().then(r=>{try{this.handleControlEvent(JSON.parse(r))}catch{console.warn("[WebRTC] Failed to parse blob data:",r)}})},this.dataChannel.onerror=a=>console.error("❌ [WebRTC] DataChannel error:",a),this.pc.ontrack=a=>{const r=a.track,A=a.streams[0];console.log(`đŸ“Ĩ [WebRTC] Received ${r.kind} track`),r.kind==="audio"?(console.log("🔊 [WebRTC] Received audio track from server"),this.audioElement=new Audio,this.audioElement.srcObject=A,this.audioElement.play().catch(c=>console.warn("Audio autoplay blocked:",c)),this.audioElement.onended=()=>{this.sendEvent("playedStream"),console.log("✅ [WebRTC] TTS playback complete")}):r.kind==="video"&&(console.log("📹 [WebRTC] Video track received"),this.onRemoteTrack(r,A))};const e=()=>{var A,c,l,d,g,w,p,I,x,Q,E;console.log("🔄 [WebRTC] State:",(A=this.pc)==null?void 0:A.connectionState,"| ICE:",(c=this.pc)==null?void 0:c.iceConnectionState);const a=((l=this.pc)==null?void 0:l.connectionState)==="connected"||((d=this.pc)==null?void 0:d.iceConnectionState)==="connected"||((g=this.pc)==null?void 0:g.iceConnectionState)==="completed",r=((w=this.pc)==null?void 0:w.connectionState)==="failed"||((p=this.pc)==null?void 0:p.iceConnectionState)==="failed"||((I=this.pc)==null?void 0:I.connectionState)==="closed"||((x=this.pc)==null?void 0:x.iceConnectionState)==="closed"||((Q=this.pc)==null?void 0:Q.connectionState)==="disconnected"||((E=this.pc)==null?void 0:E.iceConnectionState)==="disconnected";a&&!this.connected?(this.connected=!0,this.onConnected()):r&&this.connected&&(this.connected=!1,this.onDisconnected())};this.pc.onconnectionstatechange=e,this.pc.oniceconnectionstatechange=e;const i=await this.pc.createOffer();await this.pc.setLocalDescription(i),console.log("📝 [WebRTC] Created offer, waiting for ICE gathering..."),await this.waitForIceGathering(),console.log("🧊 [WebRTC] ICE gathering complete"),console.log("📤 [WebRTC] Sending offer via HTTP...");const s=await fetch(`${this.serverUrl}/webrtc?agent=${this.agentId}_${this.callId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({offer:this.pc.localDescription,agentId:this.agentId,callId:this.callId})});if(!s.ok){const a=await s.json();throw new Error(a.error||`HTTP ${s.status}`)}const{answer:n}=await s.json();console.log("đŸ“Ĩ [WebRTC] Received answer from server"),await this.pc.setRemoteDescription(n),console.log("✅ [WebRTC] Connection established!")}catch(t){throw console.error("❌ [WebRTC] Connection failed:",t),this.disconnect(),this.onError(t.message||t),t}}waitForIceGathering(){return new Promise(t=>{if(!this.pc)return t();if(this.pc.iceGatheringState==="complete")t();else{const e=()=>{var i,s;((i=this.pc)==null?void 0:i.iceGatheringState)==="complete"&&((s=this.pc)==null||s.removeEventListener("icegatheringstatechange",e),t())};this.pc.addEventListener("icegatheringstatechange",e),setTimeout(()=>{var i;(i=this.pc)==null||i.removeEventListener("icegatheringstatechange",e),console.warn("âš ī¸ [WebRTC] ICE gathering timeout, proceeding anyway"),t()},5e3)}})}sendEvent(t,e={}){var i;((i=this.dataChannel)==null?void 0:i.readyState)==="open"&&this.dataChannel.send(JSON.stringify({event:t,...e}))}handleControlEvent(t){var e;switch(t.event){case"clearAudio":console.log("🛑 [WebRTC] Interrupt: clearAudio received (leaving stream unpaused)");break;case"transcription":console.log("📝 [WebRTC] Transcription:",t.text),this.onTranscription(t.text,t.isFinal);break;case"mark":console.log("đŸˇī¸ [WebRTC] Mark:",t.name);break;case"client_tool_call":{const i=t.tool_call||t.data||t,s=(i==null?void 0:i.tool_call_id)||(i==null?void 0:i.call_id)||t.tool_call_id||"";s&&((e=this.dataChannel)==null?void 0:e.readyState)==="open"?(this.dataChannel.send(JSON.stringify({event:"client_tool_ack",data:{tool_call_id:s}})),console.log("✅ [VaniraAI] Sent client_tool_ack for:",s)):s||console.warn("âš ī¸ [VaniraAI] client_tool_call received without tool_call_id — ack skipped"),console.log("đŸ› ī¸ [VaniraAI] Client Tool Call:",t),this.onClientToolCall(i);break}default:console.log("â„šī¸ [WebRTC] Unknown event:",t.event)}}disconnect(){console.log("🔴 [WebRTC] Disconnecting..."),this.audioElement&&(this.audioElement.pause(),this.audioElement.srcObject=null),this.dataChannel&&(this.dataChannel.close(),this.dataChannel=null),this.pc&&(this.pc.getSenders().forEach(t=>{t.track&&t.track.stop()}),this.pc.close(),this.pc=null),this.connected=!1,this.onDisconnected()}generateCallId(){return"web_"+Date.now()+"_"+Math.random().toString(36).substr(2,8)}sendToolResult(t,e){this.sendEvent("client_tool_result",{call_id:t,result:e})}sendToolAck(t){var e;((e=this.dataChannel)==null?void 0:e.readyState)==="open"?(this.dataChannel.send(JSON.stringify({event:"client_tool_ack",data:{tool_call_id:t}})),console.log("✅ [VaniraAI] Sent client_tool_ack for:",t)):console.warn("âš ī¸ [VaniraAI] Cannot send tool_ack: DataChannel not open")}sendContextUpdate(t){this.sendEvent("client_context_update",{data:{context:t}})}sendActionTrigger(t,e={}){this.sendEvent("client_action_trigger",{data:{action_name:t,data:e}})}triggerActionInterrupt(){this.sendEvent("action_interrupt"),console.log("🛑 [VaniraAI] Triggered client-side action interrupt")}static async fetchIceServers(t,e="https://coredb.travelr.club/v1/graphql"){var i;try{const n=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",Authorization:t},body:JSON.stringify({query:`
2
2
  query GetIceServers {
3
3
  ice_servers(where: {enabled: {_eq: true}}) {
4
4
  urls
@@ -5,7 +5,7 @@ var we=Object.defineProperty;var ue=(f,C,b)=>C in f?we(f,C,{enumerable:!0,config
5
5
  }
6
6
  }
7
7
  `;try{const a=await(await fetch(q,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:e,variables:{prospectGroupId:t,name:`Widget Guest ${new Date().toLocaleString()}`}})})).json();return a.errors?(console.warn("[VaniraAI] Prospect creation failed, using anonymous ID",a.errors),`anon_${Date.now()}_${Math.random().toString(36).substr(2,9)}`):((s=(i=a.data)==null?void 0:i.insert_prospects_one)==null?void 0:s.id)||`anon_${Date.now()}`}catch(n){return console.error("[VaniraAI] Failed to create prospect:",n),`anon_${Date.now()}`}}static async fetchWelcomeMessage(t,e,i){try{const s=await fetch(`${k}/widget/chat`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({agent_id:t,...i?{widget_id:i}:{},message:"",prospect_id:e,stream:!1})});if(!s.ok)throw new Error("Failed to fetch welcome message");const n=await s.json();let a=n.response||"Hey! how can I help you ?",r;const l=n.chat_id||n.inbox_id;return n.widget&&(r=n.widget),{role:"assistant",content:a,widget:r,chatId:l}}catch(s){return console.error("[VaniraAI] Failed to fetch welcome message:",s),{role:"assistant",content:"Hey! how can I help you ?"}}}static async sendChatMessage(t,e,i,s,n,a,r,l){var c,A,d,g;try{const w={agent_id:t,message:i,prospect_id:e,stream:!0};s&&(w.inbox_id=s),l&&(w.widget_id=l);const p=await fetch(`${k}/widget/chat`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(w)});if(!p.ok)throw new Error("Chat request failed");const E=(c=p.body)==null?void 0:c.getReader(),x=new TextDecoder;if(!E)throw new Error("No reader");let M="",Q="",D=null;for(;;){const{done:he,value:ge}=await E.read();if(he)break;Q+=x.decode(ge,{stream:!0});const K=Q.split(`
8
- `);Q=K.pop()||"";for(const pe of K){const Y=pe.trim();if(Y&&Y.startsWith("data: ")){const F=Y.slice(6);if(F==="[DONE]"){r(D);return}try{const y=JSON.parse(F);if(y.type==="metadata"&&y.chat_id){D=y.chat_id;continue}const Z=(g=(d=(A=y.choices)==null?void 0:A[0])==null?void 0:d.delta)==null?void 0:g.content;Z&&(M+=Z,n(M)),y.widget&&a(y.widget),y.chat_id&&!D&&(D=y.chat_id)}catch{}}}}r(D)}catch(w){console.error("[VaniraAI] Chat error:",w),n("Sorry, I encountered an error. Please try again."),r(null)}}static listenForAdminReplies(t,e,i){const s=`${k}/inbox/stream?inbox_id=${t}&sender=${encodeURIComponent(e)}`;let n=null,a=0,r=!1;const l=()=>{r||(n=new EventSource(s),console.log(`[VaniraAI] Subscribed to SSE EventSource at ${n.url}`),n.onopen=()=>{console.log("[VaniraAI] SSE connection opened successfully."),a=0},n.onmessage=c=>{console.log("[VaniraAI] 📡 Raw SSE Event triggered:",c.data);try{const A=JSON.parse(c.data);console.log("[VaniraAI] SSE stream JSON parsed:",A);const d=A.direction==="outgoing",g=!!A.content,w=A.source!=="ai";d&&g&&w&&(console.log("[VaniraAI] đŸŽ¯ Displaying admin message in chat UI:",A.content),i(A.content))}catch(A){console.error("[VaniraAI] SSE parse error",A)}},n.onerror=c=>{if(console.error("[VaniraAI] SSE connection error or disconnect",c),n&&(n.close(),n=null),r)return;const A=Math.min(2e3*Math.pow(2,a),3e4);console.log(`[VaniraAI] Reconnecting SSE in ${A}ms (Attempt ${a+1})...`),a++,setTimeout(l,A)})};return l(),{close:()=>{r=!0,n&&(n.close(),n=null,console.log("[VaniraAI] SSE connection manually closed."))}}}static async createCall(t,e,i,s,n){try{const a="https://api.vanira.io",r={"Content-Type":"application/json"};n&&(r["X-API-Key"]=n);const l=await fetch(`${a}/calls/create`,{method:"POST",headers:r,body:JSON.stringify({agent_id:t,prospect_id:i||void 0,type:"web"})}),c=await l.json();if(!l.ok)throw new Error(`[VaniraAI] Call creation failed HTTP ${l.status}`);if(!c.worker_url)throw new Error("[VaniraAI] Worker URL missing from response. Call cannot proceed.");let A;try{A=new URL(c.worker_url).origin}catch{A=c.worker_url}return{callId:c.call_id||c.id||`web_${Date.now()}`,workerUrl:A}}catch(a){throw console.error("[VaniraAI] Failed to create call:",a),a}}}class _{constructor(t){o(this,"config");o(this,"root",null);this.config=t}async initialize(t){this.config=t}}class ${constructor(t){o(this,"serverUrl");o(this,"agentId");o(this,"callId");o(this,"apiKey");o(this,"backendUrl");o(this,"onConnected");o(this,"onDisconnected");o(this,"onError");o(this,"onTranscription");o(this,"onRemoteTrack");o(this,"onClientToolCall");o(this,"pc",null);o(this,"dataChannel",null);o(this,"audioElement",null);o(this,"connected",!1);if(!t.agentId)throw new Error("agentId is required");if(!t.serverUrl&&!t.apiKey)throw new Error("Provide either serverUrl or apiKey (to use createCall())");this.agentId=t.agentId,this.serverUrl=t.serverUrl?t.serverUrl.replace(/\/$/,""):"",this.callId=t.callId||this.generateCallId(),this.apiKey=t.apiKey,this.backendUrl=(t.backendUrl||"https://api.vanira.io").replace(/\/$/,""),this.onConnected=t.onConnected||(()=>{}),this.onDisconnected=t.onDisconnected||(()=>{}),this.onError=t.onError||(e=>console.error("[WebRTC]",e)),this.onTranscription=t.onTranscription||(()=>{}),this.onRemoteTrack=t.onRemoteTrack||(()=>{}),this.onClientToolCall=t.onClientToolCall||(()=>{})}async createCall(){var s;if(!this.apiKey)throw new Error("[VaniraAI] apiKey is required to use createCall()");const t={"Content-Type":"application/json","X-API-Key":this.apiKey},e=await fetch(`${this.backendUrl}/calls/create`,{method:"POST",headers:t,body:JSON.stringify({agent_id:this.agentId,type:"web"})});if(!e.ok){const n=await e.json().catch(()=>({}));throw new Error(`[VaniraAI] createCall failed (${e.status}): ${((s=n==null?void 0:n.detail)==null?void 0:s.message)||(n==null?void 0:n.message)||e.statusText}`)}const i=await e.json();if(!i.call_id||!i.worker_url)throw new Error("[VaniraAI] createCall response missing call_id or worker_url");this.callId=i.call_id,this.serverUrl=i.worker_url.replace(/\/$/,""),await this.connect()}async connect(){if(this.apiKey&&!this.serverUrl){await this.createCall();return}if(!this.serverUrl)throw new Error("[VaniraAI] serverUrl is missing. Provide apiKey or serverUrl.");console.log("đŸ”ĩ [WebRTC] Starting connection...");try{this.pc=new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},{urls:"stun:global.relay.metered.ca:80"},{urls:["turns:global.relay.metered.ca:443?transport=tcp","turn:global.relay.metered.ca:443?transport=tcp","turn:global.relay.metered.ca:80?transport=tcp"],username:"fa97658be3343d21da3b65e6",credential:"HXHDoqeHbvZrmCuf"}],iceTransportPolicy:"all"}),console.log("using ice servers:",[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"}]);const t=await navigator.mediaDevices.getUserMedia({audio:{echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0,sampleRate:{ideal:16e3},channelCount:1}});if(console.log("🎤 [WebRTC] Microphone access granted"),!this.pc){console.log("[WebRTC] Connection aborted: peer connection closed during setup"),t.getTracks().forEach(a=>a.stop());return}if(t.getTracks().forEach(a=>{var r;(r=this.pc)==null||r.addTrack(a,t)}),!this.pc)throw new Error("RTCPeerConnection was closed unexpectedly");this.dataChannel=this.pc.createDataChannel("control"),this.dataChannel.onopen=()=>console.log("📡 [WebRTC] DataChannel opened"),this.dataChannel.onmessage=a=>{if(typeof a.data=="string")try{this.handleControlEvent(JSON.parse(a.data))}catch{console.warn("[WebRTC] Failed to parse message:",a.data)}else if(a.data instanceof ArrayBuffer)try{const r=new TextDecoder().decode(a.data);try{const l=JSON.parse(r);l&&typeof l=="object"&&l.event==="client_tool_call"?(console.log("[VaniraAI] Safely decoding binary tool_call to JSON:",l),this.handleControlEvent(l)):console.log("[VaniraAI] Decoded JSON from binary (inspect only):",l)}catch{console.log("[VaniraAI] Decoded String from binary:",r)}}catch{console.log("[VaniraAI] Received binary data:",a.data.byteLength,"bytes (not decodable)")}else a.data instanceof Blob&&a.data.text().then(r=>{try{this.handleControlEvent(JSON.parse(r))}catch{console.warn("[WebRTC] Failed to parse blob data:",r)}})},this.dataChannel.onerror=a=>console.error("❌ [WebRTC] DataChannel error:",a),this.pc.ontrack=a=>{const r=a.track,l=a.streams[0];console.log(`đŸ“Ĩ [WebRTC] Received ${r.kind} track`),r.kind==="audio"?(console.log("🔊 [WebRTC] Received audio track from server"),this.audioElement=new Audio,this.audioElement.srcObject=l,this.audioElement.play().catch(c=>console.warn("Audio autoplay blocked:",c)),this.audioElement.onended=()=>{this.sendEvent("playedStream"),console.log("✅ [WebRTC] TTS playback complete")}):r.kind==="video"&&(console.log("📹 [WebRTC] Video track received"),this.onRemoteTrack(r,l))};const e=()=>{var l,c,A,d,g,w,p,E,x,M,Q;console.log("🔄 [WebRTC] State:",(l=this.pc)==null?void 0:l.connectionState,"| ICE:",(c=this.pc)==null?void 0:c.iceConnectionState);const a=((A=this.pc)==null?void 0:A.connectionState)==="connected"||((d=this.pc)==null?void 0:d.iceConnectionState)==="connected"||((g=this.pc)==null?void 0:g.iceConnectionState)==="completed",r=((w=this.pc)==null?void 0:w.connectionState)==="failed"||((p=this.pc)==null?void 0:p.iceConnectionState)==="failed"||((E=this.pc)==null?void 0:E.connectionState)==="closed"||((x=this.pc)==null?void 0:x.iceConnectionState)==="closed"||((M=this.pc)==null?void 0:M.connectionState)==="disconnected"||((Q=this.pc)==null?void 0:Q.iceConnectionState)==="disconnected";a&&!this.connected?(this.connected=!0,this.onConnected()):r&&this.connected&&(this.connected=!1,this.onDisconnected())};this.pc.onconnectionstatechange=e,this.pc.oniceconnectionstatechange=e;const i=await this.pc.createOffer();await this.pc.setLocalDescription(i),console.log("📝 [WebRTC] Created offer, waiting for ICE gathering..."),await this.waitForIceGathering(),console.log("🧊 [WebRTC] ICE gathering complete"),console.log("📤 [WebRTC] Sending offer via HTTP...");const s=await fetch(`${this.serverUrl}/webrtc?agent=${this.agentId}_${this.callId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({offer:this.pc.localDescription,agentId:this.agentId,callId:this.callId})});if(!s.ok){const a=await s.json();throw new Error(a.error||`HTTP ${s.status}`)}const{answer:n}=await s.json();console.log("đŸ“Ĩ [WebRTC] Received answer from server"),await this.pc.setRemoteDescription(n),console.log("✅ [WebRTC] Connection established!")}catch(t){throw console.error("❌ [WebRTC] Connection failed:",t),this.disconnect(),this.onError(t.message||t),t}}waitForIceGathering(){return new Promise(t=>{if(!this.pc)return t();if(this.pc.iceGatheringState==="complete")t();else{const e=()=>{var i,s;((i=this.pc)==null?void 0:i.iceGatheringState)==="complete"&&((s=this.pc)==null||s.removeEventListener("icegatheringstatechange",e),t())};this.pc.addEventListener("icegatheringstatechange",e),setTimeout(()=>{var i;(i=this.pc)==null||i.removeEventListener("icegatheringstatechange",e),console.warn("âš ī¸ [WebRTC] ICE gathering timeout, proceeding anyway"),t()},5e3)}})}sendEvent(t,e={}){var i;((i=this.dataChannel)==null?void 0:i.readyState)==="open"&&this.dataChannel.send(JSON.stringify({event:t,...e}))}handleControlEvent(t){var e;switch(t.event){case"clearAudio":console.log("🛑 [WebRTC] Interrupt: clearAudio received (leaving stream unpaused)");break;case"transcription":console.log("📝 [WebRTC] Transcription:",t.text),this.onTranscription(t.text,t.isFinal);break;case"mark":console.log("đŸˇī¸ [WebRTC] Mark:",t.name);break;case"client_tool_call":{const i=t.tool_call||t.data||t,s=(i==null?void 0:i.tool_call_id)||(i==null?void 0:i.call_id)||t.tool_call_id||"";s&&((e=this.dataChannel)==null?void 0:e.readyState)==="open"?(this.dataChannel.send(JSON.stringify({event:"client_tool_ack",data:{tool_call_id:s}})),console.log("✅ [VaniraAI] Sent client_tool_ack for:",s)):s||console.warn("âš ī¸ [VaniraAI] client_tool_call received without tool_call_id — ack skipped"),console.log("đŸ› ī¸ [VaniraAI] Client Tool Call:",t),this.onClientToolCall(i);break}default:console.log("â„šī¸ [WebRTC] Unknown event:",t.event)}}disconnect(){console.log("🔴 [WebRTC] Disconnecting..."),this.audioElement&&(this.audioElement.pause(),this.audioElement.srcObject=null),this.dataChannel&&(this.dataChannel.close(),this.dataChannel=null),this.pc&&(this.pc.getSenders().forEach(t=>{t.track&&t.track.stop()}),this.pc.close(),this.pc=null),this.connected=!1,this.onDisconnected()}generateCallId(){return"web_"+Date.now()+"_"+Math.random().toString(36).substr(2,8)}sendToolResult(t,e){this.sendEvent("client_tool_result",{call_id:t,result:e})}sendToolAck(t){var e;((e=this.dataChannel)==null?void 0:e.readyState)==="open"?(this.dataChannel.send(JSON.stringify({event:"client_tool_ack",data:{tool_call_id:t}})),console.log("✅ [VaniraAI] Sent client_tool_ack for:",t)):console.warn("âš ī¸ [VaniraAI] Cannot send tool_ack: DataChannel not open")}sendContextUpdate(t){this.sendEvent("client_context_update",{data:{context:t}})}sendActionTrigger(t,e={}){this.sendEvent("client_action_trigger",{data:{action_name:t,data:e}})}triggerActionInterrupt(){this.sendEvent("action_interrupt"),console.log("🛑 [VaniraAI] Triggered client-side action interrupt")}static async fetchIceServers(t,e="https://coredb.travelr.club/v1/graphql"){var i;try{const n=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",Authorization:t},body:JSON.stringify({query:`
8
+ `);Q=K.pop()||"";for(const pe of K){const Y=pe.trim();if(Y&&Y.startsWith("data: ")){const F=Y.slice(6);if(F==="[DONE]"){r(D);return}try{const y=JSON.parse(F);if(y.type==="metadata"&&y.chat_id){D=y.chat_id;continue}const Z=(g=(d=(A=y.choices)==null?void 0:A[0])==null?void 0:d.delta)==null?void 0:g.content;Z&&(M+=Z,n(M)),y.widget&&a(y.widget),y.chat_id&&!D&&(D=y.chat_id)}catch{}}}}r(D)}catch(w){console.error("[VaniraAI] Chat error:",w),n("Sorry, I encountered an error. Please try again."),r(null)}}static listenForAdminReplies(t,e,i){const s=`${k}/inbox/stream?inbox_id=${t}&sender=${encodeURIComponent(e)}`;let n=null,a=0,r=!1;const l=()=>{r||(n=new EventSource(s),console.log(`[VaniraAI] Subscribed to SSE EventSource at ${n.url}`),n.onopen=()=>{console.log("[VaniraAI] SSE connection opened successfully."),a=0},n.onmessage=c=>{console.log("[VaniraAI] 📡 Raw SSE Event triggered:",c.data);try{const A=JSON.parse(c.data);console.log("[VaniraAI] SSE stream JSON parsed:",A);const d=A.direction==="outgoing",g=!!A.content,w=A.source!=="ai";d&&g&&w&&(console.log("[VaniraAI] đŸŽ¯ Displaying admin message in chat UI:",A.content),i(A.content))}catch(A){console.error("[VaniraAI] SSE parse error",A)}},n.onerror=c=>{if(console.error("[VaniraAI] SSE connection error or disconnect",c),n&&(n.close(),n=null),r)return;const A=Math.min(2e3*Math.pow(2,a),3e4);console.log(`[VaniraAI] Reconnecting SSE in ${A}ms (Attempt ${a+1})...`),a++,setTimeout(l,A)})};return l(),{close:()=>{r=!0,n&&(n.close(),n=null,console.log("[VaniraAI] SSE connection manually closed."))}}}static async createCall(t,e,i,s,n){try{const a="https://api.vanira.io",r={"Content-Type":"application/json"};n&&(r["X-API-Key"]=n);const l=await fetch(`${a}/calls/create`,{method:"POST",headers:r,body:JSON.stringify({agent_id:t,prospect_id:i||void 0,type:"web"})}),c=await l.json();if(!l.ok)throw new Error(`[VaniraAI] Call creation failed HTTP ${l.status}`);if(!c.worker_url)throw new Error("[VaniraAI] Worker URL missing from response. Call cannot proceed.");let A;try{A=new URL(c.worker_url).origin}catch{A=c.worker_url}return{callId:c.call_id||c.id||`web_${Date.now()}`,workerUrl:A}}catch(a){throw console.error("[VaniraAI] Failed to create call:",a),a}}}class _{constructor(t){o(this,"config");o(this,"root",null);this.config=t}async initialize(t){this.config=t}}class ${constructor(t){o(this,"serverUrl");o(this,"agentId");o(this,"callId");o(this,"apiKey");o(this,"backendUrl");o(this,"onConnected");o(this,"onDisconnected");o(this,"onError");o(this,"onTranscription");o(this,"onRemoteTrack");o(this,"onClientToolCall");o(this,"pc",null);o(this,"dataChannel",null);o(this,"audioElement",null);o(this,"connected",!1);if(!t.agentId)throw new Error("agentId is required");if(!t.serverUrl&&!t.apiKey)throw new Error("Provide either serverUrl or apiKey (to use createCall())");this.agentId=t.agentId,this.serverUrl=t.serverUrl?t.serverUrl.replace(/\/$/,""):"",this.callId=t.callId||this.generateCallId(),this.apiKey=t.apiKey,this.backendUrl=(t.backendUrl||"https://api.vanira.io").replace(/\/$/,""),this.onConnected=t.onConnected||(()=>{}),this.onDisconnected=t.onDisconnected||(()=>{}),this.onError=t.onError||(e=>console.error("[WebRTC]",e)),this.onTranscription=t.onTranscription||(()=>{}),this.onRemoteTrack=t.onRemoteTrack||(()=>{}),this.onClientToolCall=t.onClientToolCall||(()=>{})}async createCall(){var s;if(!this.apiKey)throw new Error("[VaniraAI] apiKey is required to use createCall()");const t={"Content-Type":"application/json","X-API-Key":this.apiKey},e=await fetch(`${this.backendUrl}/calls/create`,{method:"POST",headers:t,body:JSON.stringify({agent_id:this.agentId,type:"web"})});if(!e.ok){const n=await e.json().catch(()=>({}));throw new Error(`[VaniraAI] createCall failed (${e.status}): ${((s=n==null?void 0:n.detail)==null?void 0:s.message)||(n==null?void 0:n.message)||e.statusText}`)}const i=await e.json();if(!i.call_id||!i.worker_url)throw new Error("[VaniraAI] createCall response missing call_id or worker_url");this.callId=i.call_id,this.serverUrl=new URL(i.worker_url).origin,await this.connect()}async connect(){if(this.apiKey&&!this.serverUrl){await this.createCall();return}if(!this.serverUrl)throw new Error("[VaniraAI] serverUrl is missing. Provide apiKey or serverUrl.");console.log("đŸ”ĩ [WebRTC] Starting connection...");try{this.pc=new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},{urls:"stun:global.relay.metered.ca:80"},{urls:["turns:global.relay.metered.ca:443?transport=tcp","turn:global.relay.metered.ca:443?transport=tcp","turn:global.relay.metered.ca:80?transport=tcp"],username:"fa97658be3343d21da3b65e6",credential:"HXHDoqeHbvZrmCuf"}],iceTransportPolicy:"all"}),console.log("using ice servers:",[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"}]);const t=await navigator.mediaDevices.getUserMedia({audio:{echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0,sampleRate:{ideal:16e3},channelCount:1}});if(console.log("🎤 [WebRTC] Microphone access granted"),!this.pc){console.log("[WebRTC] Connection aborted: peer connection closed during setup"),t.getTracks().forEach(a=>a.stop());return}if(t.getTracks().forEach(a=>{var r;(r=this.pc)==null||r.addTrack(a,t)}),!this.pc)throw new Error("RTCPeerConnection was closed unexpectedly");this.dataChannel=this.pc.createDataChannel("control"),this.dataChannel.onopen=()=>console.log("📡 [WebRTC] DataChannel opened"),this.dataChannel.onmessage=a=>{if(typeof a.data=="string")try{this.handleControlEvent(JSON.parse(a.data))}catch{console.warn("[WebRTC] Failed to parse message:",a.data)}else if(a.data instanceof ArrayBuffer)try{const r=new TextDecoder().decode(a.data);try{const l=JSON.parse(r);l&&typeof l=="object"&&l.event==="client_tool_call"?(console.log("[VaniraAI] Safely decoding binary tool_call to JSON:",l),this.handleControlEvent(l)):console.log("[VaniraAI] Decoded JSON from binary (inspect only):",l)}catch{console.log("[VaniraAI] Decoded String from binary:",r)}}catch{console.log("[VaniraAI] Received binary data:",a.data.byteLength,"bytes (not decodable)")}else a.data instanceof Blob&&a.data.text().then(r=>{try{this.handleControlEvent(JSON.parse(r))}catch{console.warn("[WebRTC] Failed to parse blob data:",r)}})},this.dataChannel.onerror=a=>console.error("❌ [WebRTC] DataChannel error:",a),this.pc.ontrack=a=>{const r=a.track,l=a.streams[0];console.log(`đŸ“Ĩ [WebRTC] Received ${r.kind} track`),r.kind==="audio"?(console.log("🔊 [WebRTC] Received audio track from server"),this.audioElement=new Audio,this.audioElement.srcObject=l,this.audioElement.play().catch(c=>console.warn("Audio autoplay blocked:",c)),this.audioElement.onended=()=>{this.sendEvent("playedStream"),console.log("✅ [WebRTC] TTS playback complete")}):r.kind==="video"&&(console.log("📹 [WebRTC] Video track received"),this.onRemoteTrack(r,l))};const e=()=>{var l,c,A,d,g,w,p,E,x,M,Q;console.log("🔄 [WebRTC] State:",(l=this.pc)==null?void 0:l.connectionState,"| ICE:",(c=this.pc)==null?void 0:c.iceConnectionState);const a=((A=this.pc)==null?void 0:A.connectionState)==="connected"||((d=this.pc)==null?void 0:d.iceConnectionState)==="connected"||((g=this.pc)==null?void 0:g.iceConnectionState)==="completed",r=((w=this.pc)==null?void 0:w.connectionState)==="failed"||((p=this.pc)==null?void 0:p.iceConnectionState)==="failed"||((E=this.pc)==null?void 0:E.connectionState)==="closed"||((x=this.pc)==null?void 0:x.iceConnectionState)==="closed"||((M=this.pc)==null?void 0:M.connectionState)==="disconnected"||((Q=this.pc)==null?void 0:Q.iceConnectionState)==="disconnected";a&&!this.connected?(this.connected=!0,this.onConnected()):r&&this.connected&&(this.connected=!1,this.onDisconnected())};this.pc.onconnectionstatechange=e,this.pc.oniceconnectionstatechange=e;const i=await this.pc.createOffer();await this.pc.setLocalDescription(i),console.log("📝 [WebRTC] Created offer, waiting for ICE gathering..."),await this.waitForIceGathering(),console.log("🧊 [WebRTC] ICE gathering complete"),console.log("📤 [WebRTC] Sending offer via HTTP...");const s=await fetch(`${this.serverUrl}/webrtc?agent=${this.agentId}_${this.callId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({offer:this.pc.localDescription,agentId:this.agentId,callId:this.callId})});if(!s.ok){const a=await s.json();throw new Error(a.error||`HTTP ${s.status}`)}const{answer:n}=await s.json();console.log("đŸ“Ĩ [WebRTC] Received answer from server"),await this.pc.setRemoteDescription(n),console.log("✅ [WebRTC] Connection established!")}catch(t){throw console.error("❌ [WebRTC] Connection failed:",t),this.disconnect(),this.onError(t.message||t),t}}waitForIceGathering(){return new Promise(t=>{if(!this.pc)return t();if(this.pc.iceGatheringState==="complete")t();else{const e=()=>{var i,s;((i=this.pc)==null?void 0:i.iceGatheringState)==="complete"&&((s=this.pc)==null||s.removeEventListener("icegatheringstatechange",e),t())};this.pc.addEventListener("icegatheringstatechange",e),setTimeout(()=>{var i;(i=this.pc)==null||i.removeEventListener("icegatheringstatechange",e),console.warn("âš ī¸ [WebRTC] ICE gathering timeout, proceeding anyway"),t()},5e3)}})}sendEvent(t,e={}){var i;((i=this.dataChannel)==null?void 0:i.readyState)==="open"&&this.dataChannel.send(JSON.stringify({event:t,...e}))}handleControlEvent(t){var e;switch(t.event){case"clearAudio":console.log("🛑 [WebRTC] Interrupt: clearAudio received (leaving stream unpaused)");break;case"transcription":console.log("📝 [WebRTC] Transcription:",t.text),this.onTranscription(t.text,t.isFinal);break;case"mark":console.log("đŸˇī¸ [WebRTC] Mark:",t.name);break;case"client_tool_call":{const i=t.tool_call||t.data||t,s=(i==null?void 0:i.tool_call_id)||(i==null?void 0:i.call_id)||t.tool_call_id||"";s&&((e=this.dataChannel)==null?void 0:e.readyState)==="open"?(this.dataChannel.send(JSON.stringify({event:"client_tool_ack",data:{tool_call_id:s}})),console.log("✅ [VaniraAI] Sent client_tool_ack for:",s)):s||console.warn("âš ī¸ [VaniraAI] client_tool_call received without tool_call_id — ack skipped"),console.log("đŸ› ī¸ [VaniraAI] Client Tool Call:",t),this.onClientToolCall(i);break}default:console.log("â„šī¸ [WebRTC] Unknown event:",t.event)}}disconnect(){console.log("🔴 [WebRTC] Disconnecting..."),this.audioElement&&(this.audioElement.pause(),this.audioElement.srcObject=null),this.dataChannel&&(this.dataChannel.close(),this.dataChannel=null),this.pc&&(this.pc.getSenders().forEach(t=>{t.track&&t.track.stop()}),this.pc.close(),this.pc=null),this.connected=!1,this.onDisconnected()}generateCallId(){return"web_"+Date.now()+"_"+Math.random().toString(36).substr(2,8)}sendToolResult(t,e){this.sendEvent("client_tool_result",{call_id:t,result:e})}sendToolAck(t){var e;((e=this.dataChannel)==null?void 0:e.readyState)==="open"?(this.dataChannel.send(JSON.stringify({event:"client_tool_ack",data:{tool_call_id:t}})),console.log("✅ [VaniraAI] Sent client_tool_ack for:",t)):console.warn("âš ī¸ [VaniraAI] Cannot send tool_ack: DataChannel not open")}sendContextUpdate(t){this.sendEvent("client_context_update",{data:{context:t}})}sendActionTrigger(t,e={}){this.sendEvent("client_action_trigger",{data:{action_name:t,data:e}})}triggerActionInterrupt(){this.sendEvent("action_interrupt"),console.log("🛑 [VaniraAI] Triggered client-side action interrupt")}static async fetchIceServers(t,e="https://coredb.travelr.club/v1/graphql"){var i;try{const n=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json",Authorization:t},body:JSON.stringify({query:`
9
9
  query GetIceServers {
10
10
  ice_servers(where: {enabled: {_eq: true}}) {
11
11
  urls