zann-voice 0.1.40 → 0.1.42
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 +0 -3
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +1 -3
package/README.md
CHANGED
|
@@ -26,7 +26,6 @@ const voice = new ZannVoice({
|
|
|
26
26
|
//在需要呼叫地方调用 必须在click事件中调用,一次new 可以多次调用
|
|
27
27
|
voice.connect({
|
|
28
28
|
onOpen: () => console.log("Connected!"),
|
|
29
|
-
onTrack: (e) => console.log("Track received", e),
|
|
30
29
|
onError: (e) => console.error(e),
|
|
31
30
|
onAuth:(e) => console.log(e),
|
|
32
31
|
onMessage: (msg) => console.log("WS Message", msg)
|
|
@@ -66,7 +65,6 @@ const voice = new ZannVoice({
|
|
|
66
65
|
voice.connect({
|
|
67
66
|
media:{ audio:true, video:false},
|
|
68
67
|
onOpen: () => console.log("Connected!"),
|
|
69
|
-
onTrack: (e) => console.log("Track received", e),
|
|
70
68
|
onAuth:(e) => console.log(e),
|
|
71
69
|
onError: (e) => console.error(e),
|
|
72
70
|
onMessage: (msg) => console.log("WS Message", msg)
|
|
@@ -78,7 +76,6 @@ voice.connect({
|
|
|
78
76
|
| -------------| :-----------: | :-----------: | :-----------: |
|
|
79
77
|
| media | media |否 | 媒体模式, 默认开启麦克风,默认关闭摄像头 |
|
|
80
78
|
| onOpen | function |是 | 监听到连接成功后发生 |
|
|
81
|
-
| onTrack | function |是 | 监听媒体流[请参考](https://developer.mozilla.org/en-US/docs/Web/API/RTCTrackEvent#example) |
|
|
82
79
|
| onError | function |是 | 监听错误问题 |
|
|
83
80
|
| onMessage | function |是 | 监听消息字幕 |
|
|
84
81
|
| onAuth | function | 是 | 监听到需要用户授权麦克风或者摄像头,返回值参考 <code>PermissionState.state 授权状态</code>|
|
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var j=require("@fingerprintjs/fingerprintjs");function t(o,e,i,s){if(i==="a"&&!s)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!s:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return i==="m"?s:i==="a"?s.call(o):s?s.value:e.get(o)}function a(o,e,i,s,n){if(s==="m")throw new TypeError("Private method is not writable");if(s==="a"&&!n)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!n:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return s==="a"?n.call(o,i):n?n.value=i:e.set(o,i),i}typeof SuppressedError=="function"&&SuppressedError;var f,r,l,h,T,W,g,
|
|
1
|
+
"use strict";var j=require("@fingerprintjs/fingerprintjs");function t(o,e,i,s){if(i==="a"&&!s)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!s:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return i==="m"?s:i==="a"?s.call(o):s?s.value:e.get(o)}function a(o,e,i,s,n){if(s==="m")throw new TypeError("Private method is not writable");if(s==="a"&&!n)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!n:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return s==="a"?n.call(o,i):n?n.value=i:e.set(o,i),i}typeof SuppressedError=="function"&&SuppressedError;var f,r,l,h,T,W,y,g,S,b,v,A,V,m,F,C,k,E,_,p,Z,D,N,z,q,O,I,R,P,J,x,U,$;class B{constructor({wss:e,TURN:i,maxReconnect:s=5}){f.add(this),r.set(this,null),l.set(this,null),h.set(this,null),T.set(this,!1),W.set(this,!1),y.set(this,0),g.set(this,5),S.set(this,!1),b.set(this,!0),v.set(this,""),A.set(this,{urls:""}),V.set(this,null),m.set(this,null),F.set(this,void 0),C.set(this,void 0),k.set(this,void 0),E.set(this,void 0),_.set(this,void 0),p.set(this,void 0),Z.set(this,""),a(this,v,e,"f"),a(this,A,i,"f"),a(this,g,s,"f")}async connect({media:e={audio:!0,video:!1},onOpen:i,onError:s,onMessage:n,onAuth:d,onMicrophoneChange:c,onCameraChange:w}){if(t(this,T,"f")){console.warn("\u26A0\uFE0F \u5DF2\u7ECF\u5B58\u5728 RTC/WS\uFF0C\u4E0D\u91CD\u590D\u521B\u5EFA");return}a(this,W,!1,"f"),a(this,S,!1,"f"),a(this,y,0,"f"),a(this,F,i,"f"),a(this,C,s,"f"),a(this,k,n,"f"),a(this,E,d,"f"),a(this,_,c,"f"),a(this,p,w,"f"),a(this,r,new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},t(this,A,"f")]}),"f"),t(this,r,"f").onicecandidate=u=>{u.candidate&&t(this,l,"f")?.readyState===WebSocket.OPEN&&t(this,l,"f").send(JSON.stringify({type:"ice",data:u.candidate}))},t(this,r,"f").ontrack=u=>{if(u.track.kind=="audio"){const M=document.createElement("audio");M.srcObject=u.streams[0],M.autoplay=!0,M.muted=!1,document.body.appendChild(M)}},e.audio&&await this.startMicrophone(),e.video&&await this.startCamera(),await t(this,f,"m",z).call(this),t(this,r,"f").onconnectionstatechange=()=>{const u=t(this,r,"f")?.connectionState;console.log("RTC connection state:",u),!t(this,W,"f")&&(u==="disconnected"||u==="failed")&&(console.warn("RTC disconnected, retrying offer..."),t(this,f,"m",x).call(this))}}async startMicrophone(){if(!t(this,r,"f")){console.warn("\u274C startMicrophone \u9700\u8981\u5148\u8C03\u7528 connect()");return}if(t(this,h,"f")?.getAudioTracks().length){t(this,h,"f").getAudioTracks().forEach(e=>e.enabled=!0),t(this,_,"f")&&t(this,_,"f").call(this,!0,t(this,h,"f"));return}try{const e=await navigator.mediaDevices.getUserMedia({audio:!0});e.getAudioTracks().forEach(i=>t(this,r,"f").addTrack(i,e)),t(this,h,"f")||a(this,h,new MediaStream,"f"),e.getAudioTracks().forEach(i=>t(this,h,"f").addTrack(i)),t(this,_,"f")&&t(this,_,"f").call(this,!0,e)}catch(e){t(this,E,"f").call(this,e),t(this,_,"f")&&t(this,_,"f").call(this,!1,null),t(this,f,"m",P).call(this)}}analyzeFrequency({stream:e,fftSize:i=2048,callback:s}){a(this,m,new AudioContext,"f");const n=t(this,m,"f").createAnalyser();n.fftSize=t(this,f,"m",J).call(this,i),n.smoothingTimeConstant=.8,t(this,m,"f").createMediaStreamSource(e).connect(n);const c=new Uint8Array(n.fftSize),w=new Uint8Array(n.frequencyBinCount),u=this;function M(){t(u,m,"f")&&(n.getByteTimeDomainData(c),n.getByteFrequencyData(w),t(u,T,"f")&&s(c,w),requestAnimationFrame(M))}M()}stopMicrophone(){t(this,h,"f")&&(t(this,h,"f").getAudioTracks().forEach(e=>{e.enabled=!1}),t(this,_,"f")&&t(this,_,"f").call(this,!1,t(this,h,"f")),t(this,f,"m",P).call(this))}async startCamera(){if(!t(this,r,"f")){console.warn("\u274C startCamera \u9700\u8981\u5148\u8C03\u7528 connect()");return}if(t(this,h,"f")?.getVideoTracks().length){t(this,h,"f").getVideoTracks().forEach(e=>e.enabled=!0),t(this,p,"f")&&t(this,p,"f").call(this,!0,t(this,h,"f"));return}try{const e=await navigator.mediaDevices.getUserMedia({video:!0});e.getVideoTracks().forEach(i=>t(this,r,"f").addTrack(i,e)),t(this,h,"f")||a(this,h,new MediaStream,"f"),e.getVideoTracks().forEach(i=>t(this,h,"f").addTrack(i)),t(this,p,"f")&&t(this,p,"f").call(this,!0,e)}catch(e){t(this,E,"f").call(this,e),t(this,p,"f")&&t(this,p,"f").call(this,!1,null)}}stopCamera(){t(this,h,"f")&&(t(this,h,"f").getVideoTracks().forEach(e=>{e.enabled=!1}),t(this,p,"f")&&t(this,p,"f").call(this,!1,t(this,h,"f")))}destroy(){console.log("\u{1F6D1} Destroying ZannVoice..."),a(this,W,!0,"f"),this.stopMicrophone(),this.stopCamera(),t(this,f,"m",U).call(this),t(this,f,"m",$).call(this),a(this,T,!1,"f"),clearInterval(t(this,V,"f")),a(this,V,null,"f"),a(this,S,!1,"f")}async authorize(){if(!navigator.permissions)return{microphone:"unknown",camera:"unknown"};const e=async n=>{try{return(await navigator.permissions.query({name:n})).state}catch{return"unknown"}},i=await e("microphone"),s=await e("camera");return{microphone:i,camera:s}}}r=new WeakMap,l=new WeakMap,h=new WeakMap,T=new WeakMap,W=new WeakMap,y=new WeakMap,g=new WeakMap,S=new WeakMap,b=new WeakMap,v=new WeakMap,A=new WeakMap,V=new WeakMap,m=new WeakMap,F=new WeakMap,C=new WeakMap,k=new WeakMap,E=new WeakMap,_=new WeakMap,p=new WeakMap,Z=new WeakMap,f=new WeakSet,D=function(e,i){const s=new URL(e);return Object.entries(i).forEach(([n,d])=>s.searchParams.set(n,String(d))),s.toString()},N=function(e,i,s=1e4){let n=0,d=e;for(let c=0;c<i;c++)n+=d,d=Math.min(d*2,s);return n},z=async function o(e=1e3){if(!t(this,v,"f"))return;const s=await(await j.load()).get(),n=t(this,f,"m",N).call(this,1e3,t(this,g,"f")),d=t(this,f,"m",D).call(this,t(this,v,"f"),{fingerprint:s.visitorId,maxRetryTotalTime:n});a(this,l,new WebSocket(d),"f"),t(this,l,"f").onopen=()=>{console.log("\u2705 WS connected"),a(this,S,!0,"f"),a(this,y,0,"f"),t(this,r,"f")?.connectionState!=="connected"&&t(this,f,"m",x).call(this)},t(this,l,"f").onmessage=c=>t(this,f,"m",I).call(this,c),t(this,l,"f").onerror=c=>{t(this,S,"f")&&t(this,C,"f").call(this,c)},t(this,l,"f").onclose=()=>{var c;if(!t(this,W,"f")){if(t(this,y,"f")>=t(this,g,"f")){console.error(`\u274C WS \u8D85\u8FC7\u6700\u5927\u91CD\u8FDE\u6B21\u6570 ${t(this,g,"f")}`),t(this,C,"f")?.call(this,new Error(`WS exceeded max reconnects ${t(this,g,"f")}`)),a(this,S,!1,"f"),this.stopMicrophone();return}a(this,y,(c=t(this,y,"f"),c++,c),"f"),console.warn(`\u{1F504} WS disconnected, retrying in ${e}ms... [${t(this,y,"f")}/${t(this,g,"f")}]`),t(this,f,"m",O).call(this),setTimeout(()=>t(this,f,"m",o).call(this,Math.min(e*2,1e4)),e)}}},q=function(){t(this,f,"m",O).call(this),a(this,V,setInterval(()=>{t(this,l,"f")?.send(JSON.stringify({type:"heartbeat",data:"1"}))},1e4),"f")},O=function(){t(this,V,"f")!=null&&(clearInterval(t(this,V,"f")),a(this,V,null,"f"))},I=async function(e){const i=JSON.parse(e.data);switch(i.type){case"answer":await t(this,f,"m",R).call(this,i.data),t(this,F,"f").call(this),t(this,f,"m",q).call(this),a(this,T,!0,"f");break;case"ice":await t(this,r,"f")?.addIceCandidate(i.data);break;case"offer":await t(this,f,"m",R).call(this,i.data);const s=await t(this,r,"f")?.createAnswer();s&&(await t(this,r,"f")?.setLocalDescription(s),t(this,l,"f")?.send(JSON.stringify({type:"answer",data:s})));break;case"start_talking":a(this,Z,"","f"),a(this,b,!0,"f"),t(this,k,"f").call(this,{type:"start",data:null});break;case"asr_text":t(this,k,"f").call(this,{type:"user",data:{content:i.data}});break;case"llm_text":i.finish_reason==="content_filter"?a(this,Z,i.content,"f"):a(this,Z,t(this,Z,"f")+i.content,"f"),t(this,b,"f")&&(t(this,k,"f").call(this,{type:"before",data:null}),a(this,b,!1,"f")),t(this,k,"f").call(this,{type:"assistant",data:{content:t(this,Z,"f")}});break;case"llm_end":t(this,k,"f").call(this,{type:"end",data:null}),a(this,Z,"","f"),a(this,b,!0,"f");break}},R=async function(e){const i=t(this,r,"f")?.signalingState;if(i!=="have-local-offer"){console.warn("\u26A0 \u5FFD\u7565\u91CD\u590D answer\uFF0C\u5F53\u524D\u72B6\u6001:",i);return}else await t(this,r,"f")?.setRemoteDescription(e)},P=function(){t(this,m,"f")&&(t(this,m,"f").close(),a(this,m,null,"f"))},J=function(e){const n=w=>(w&w-1)===0,d=w=>{let u=1;for(;u*2<=w;)u*=2;return u};let c=Math.min(Math.max(e,32),32768);return n(c)||(c=d(c)),c},x=async function(){if(!t(this,r,"f")||!t(this,l,"f")||t(this,l,"f").readyState!==WebSocket.OPEN)return;const e=await t(this,r,"f").createOffer();await t(this,r,"f").setLocalDescription(e),t(this,l,"f").send(JSON.stringify({type:"offer",data:e}))},U=function(){t(this,r,"f")?.getSenders().forEach(e=>e.track?.stop()),t(this,r,"f")?.close(),a(this,r,null,"f")},$=function(){t(this,l,"f")&&t(this,l,"f").readyState===WebSocket.OPEN&&t(this,l,"f").close(),a(this,l,null,"f")},module.exports=B;
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import j from"@fingerprintjs/fingerprintjs";function t(o,e,i,s){if(i==="a"&&!s)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!s:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return i==="m"?s:i==="a"?s.call(o):s?s.value:e.get(o)}function a(o,e,i,s,n){if(s==="m")throw new TypeError("Private method is not writable");if(s==="a"&&!n)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!n:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return s==="a"?n.call(o,i):n?n.value=i:e.set(o,i),i}typeof SuppressedError=="function"&&SuppressedError;var f,r,l,h,T,W,g,
|
|
1
|
+
import j from"@fingerprintjs/fingerprintjs";function t(o,e,i,s){if(i==="a"&&!s)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!s:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return i==="m"?s:i==="a"?s.call(o):s?s.value:e.get(o)}function a(o,e,i,s,n){if(s==="m")throw new TypeError("Private method is not writable");if(s==="a"&&!n)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!n:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return s==="a"?n.call(o,i):n?n.value=i:e.set(o,i),i}typeof SuppressedError=="function"&&SuppressedError;var f,r,l,h,T,W,y,g,S,b,C,A,V,m,F,E,k,v,_,p,Z,D,N,z,I,O,q,R,P,J,x,U,$;class B{constructor({wss:e,TURN:i,maxReconnect:s=5}){f.add(this),r.set(this,null),l.set(this,null),h.set(this,null),T.set(this,!1),W.set(this,!1),y.set(this,0),g.set(this,5),S.set(this,!1),b.set(this,!0),C.set(this,""),A.set(this,{urls:""}),V.set(this,null),m.set(this,null),F.set(this,void 0),E.set(this,void 0),k.set(this,void 0),v.set(this,void 0),_.set(this,void 0),p.set(this,void 0),Z.set(this,""),a(this,C,e,"f"),a(this,A,i,"f"),a(this,g,s,"f")}async connect({media:e={audio:!0,video:!1},onOpen:i,onError:s,onMessage:n,onAuth:d,onMicrophoneChange:c,onCameraChange:w}){if(t(this,T,"f")){console.warn("\u26A0\uFE0F \u5DF2\u7ECF\u5B58\u5728 RTC/WS\uFF0C\u4E0D\u91CD\u590D\u521B\u5EFA");return}a(this,W,!1,"f"),a(this,S,!1,"f"),a(this,y,0,"f"),a(this,F,i,"f"),a(this,E,s,"f"),a(this,k,n,"f"),a(this,v,d,"f"),a(this,_,c,"f"),a(this,p,w,"f"),a(this,r,new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},t(this,A,"f")]}),"f"),t(this,r,"f").onicecandidate=u=>{u.candidate&&t(this,l,"f")?.readyState===WebSocket.OPEN&&t(this,l,"f").send(JSON.stringify({type:"ice",data:u.candidate}))},t(this,r,"f").ontrack=u=>{if(u.track.kind=="audio"){const M=document.createElement("audio");M.srcObject=u.streams[0],M.autoplay=!0,M.muted=!1,document.body.appendChild(M)}},e.audio&&await this.startMicrophone(),e.video&&await this.startCamera(),await t(this,f,"m",z).call(this),t(this,r,"f").onconnectionstatechange=()=>{const u=t(this,r,"f")?.connectionState;console.log("RTC connection state:",u),!t(this,W,"f")&&(u==="disconnected"||u==="failed")&&(console.warn("RTC disconnected, retrying offer..."),t(this,f,"m",x).call(this))}}async startMicrophone(){if(!t(this,r,"f")){console.warn("\u274C startMicrophone \u9700\u8981\u5148\u8C03\u7528 connect()");return}if(t(this,h,"f")?.getAudioTracks().length){t(this,h,"f").getAudioTracks().forEach(e=>e.enabled=!0),t(this,_,"f")&&t(this,_,"f").call(this,!0,t(this,h,"f"));return}try{const e=await navigator.mediaDevices.getUserMedia({audio:!0});e.getAudioTracks().forEach(i=>t(this,r,"f").addTrack(i,e)),t(this,h,"f")||a(this,h,new MediaStream,"f"),e.getAudioTracks().forEach(i=>t(this,h,"f").addTrack(i)),t(this,_,"f")&&t(this,_,"f").call(this,!0,e)}catch(e){t(this,v,"f").call(this,e),t(this,_,"f")&&t(this,_,"f").call(this,!1,null),t(this,f,"m",P).call(this)}}analyzeFrequency({stream:e,fftSize:i=2048,callback:s}){a(this,m,new AudioContext,"f");const n=t(this,m,"f").createAnalyser();n.fftSize=t(this,f,"m",J).call(this,i),n.smoothingTimeConstant=.8,t(this,m,"f").createMediaStreamSource(e).connect(n);const c=new Uint8Array(n.fftSize),w=new Uint8Array(n.frequencyBinCount),u=this;function M(){t(u,m,"f")&&(n.getByteTimeDomainData(c),n.getByteFrequencyData(w),t(u,T,"f")&&s(c,w),requestAnimationFrame(M))}M()}stopMicrophone(){t(this,h,"f")&&(t(this,h,"f").getAudioTracks().forEach(e=>{e.enabled=!1}),t(this,_,"f")&&t(this,_,"f").call(this,!1,t(this,h,"f")),t(this,f,"m",P).call(this))}async startCamera(){if(!t(this,r,"f")){console.warn("\u274C startCamera \u9700\u8981\u5148\u8C03\u7528 connect()");return}if(t(this,h,"f")?.getVideoTracks().length){t(this,h,"f").getVideoTracks().forEach(e=>e.enabled=!0),t(this,p,"f")&&t(this,p,"f").call(this,!0,t(this,h,"f"));return}try{const e=await navigator.mediaDevices.getUserMedia({video:!0});e.getVideoTracks().forEach(i=>t(this,r,"f").addTrack(i,e)),t(this,h,"f")||a(this,h,new MediaStream,"f"),e.getVideoTracks().forEach(i=>t(this,h,"f").addTrack(i)),t(this,p,"f")&&t(this,p,"f").call(this,!0,e)}catch(e){t(this,v,"f").call(this,e),t(this,p,"f")&&t(this,p,"f").call(this,!1,null)}}stopCamera(){t(this,h,"f")&&(t(this,h,"f").getVideoTracks().forEach(e=>{e.enabled=!1}),t(this,p,"f")&&t(this,p,"f").call(this,!1,t(this,h,"f")))}destroy(){console.log("\u{1F6D1} Destroying ZannVoice..."),a(this,W,!0,"f"),this.stopMicrophone(),this.stopCamera(),t(this,f,"m",U).call(this),t(this,f,"m",$).call(this),a(this,T,!1,"f"),clearInterval(t(this,V,"f")),a(this,V,null,"f"),a(this,S,!1,"f")}async authorize(){if(!navigator.permissions)return{microphone:"unknown",camera:"unknown"};const e=async n=>{try{return(await navigator.permissions.query({name:n})).state}catch{return"unknown"}},i=await e("microphone"),s=await e("camera");return{microphone:i,camera:s}}}r=new WeakMap,l=new WeakMap,h=new WeakMap,T=new WeakMap,W=new WeakMap,y=new WeakMap,g=new WeakMap,S=new WeakMap,b=new WeakMap,C=new WeakMap,A=new WeakMap,V=new WeakMap,m=new WeakMap,F=new WeakMap,E=new WeakMap,k=new WeakMap,v=new WeakMap,_=new WeakMap,p=new WeakMap,Z=new WeakMap,f=new WeakSet,D=function(e,i){const s=new URL(e);return Object.entries(i).forEach(([n,d])=>s.searchParams.set(n,String(d))),s.toString()},N=function(e,i,s=1e4){let n=0,d=e;for(let c=0;c<i;c++)n+=d,d=Math.min(d*2,s);return n},z=async function o(e=1e3){if(!t(this,C,"f"))return;const s=await(await j.load()).get(),n=t(this,f,"m",N).call(this,1e3,t(this,g,"f")),d=t(this,f,"m",D).call(this,t(this,C,"f"),{fingerprint:s.visitorId,maxRetryTotalTime:n});a(this,l,new WebSocket(d),"f"),t(this,l,"f").onopen=()=>{console.log("\u2705 WS connected"),a(this,S,!0,"f"),a(this,y,0,"f"),t(this,r,"f")?.connectionState!=="connected"&&t(this,f,"m",x).call(this)},t(this,l,"f").onmessage=c=>t(this,f,"m",q).call(this,c),t(this,l,"f").onerror=c=>{t(this,S,"f")&&t(this,E,"f").call(this,c)},t(this,l,"f").onclose=()=>{var c;if(!t(this,W,"f")){if(t(this,y,"f")>=t(this,g,"f")){console.error(`\u274C WS \u8D85\u8FC7\u6700\u5927\u91CD\u8FDE\u6B21\u6570 ${t(this,g,"f")}`),t(this,E,"f")?.call(this,new Error(`WS exceeded max reconnects ${t(this,g,"f")}`)),a(this,S,!1,"f"),this.stopMicrophone();return}a(this,y,(c=t(this,y,"f"),c++,c),"f"),console.warn(`\u{1F504} WS disconnected, retrying in ${e}ms... [${t(this,y,"f")}/${t(this,g,"f")}]`),t(this,f,"m",O).call(this),setTimeout(()=>t(this,f,"m",o).call(this,Math.min(e*2,1e4)),e)}}},I=function(){t(this,f,"m",O).call(this),a(this,V,setInterval(()=>{t(this,l,"f")?.send(JSON.stringify({type:"heartbeat",data:"1"}))},1e4),"f")},O=function(){t(this,V,"f")!=null&&(clearInterval(t(this,V,"f")),a(this,V,null,"f"))},q=async function(e){const i=JSON.parse(e.data);switch(i.type){case"answer":await t(this,f,"m",R).call(this,i.data),t(this,F,"f").call(this),t(this,f,"m",I).call(this),a(this,T,!0,"f");break;case"ice":await t(this,r,"f")?.addIceCandidate(i.data);break;case"offer":await t(this,f,"m",R).call(this,i.data);const s=await t(this,r,"f")?.createAnswer();s&&(await t(this,r,"f")?.setLocalDescription(s),t(this,l,"f")?.send(JSON.stringify({type:"answer",data:s})));break;case"start_talking":a(this,Z,"","f"),a(this,b,!0,"f"),t(this,k,"f").call(this,{type:"start",data:null});break;case"asr_text":t(this,k,"f").call(this,{type:"user",data:{content:i.data}});break;case"llm_text":i.finish_reason==="content_filter"?a(this,Z,i.content,"f"):a(this,Z,t(this,Z,"f")+i.content,"f"),t(this,b,"f")&&(t(this,k,"f").call(this,{type:"before",data:null}),a(this,b,!1,"f")),t(this,k,"f").call(this,{type:"assistant",data:{content:t(this,Z,"f")}});break;case"llm_end":t(this,k,"f").call(this,{type:"end",data:null}),a(this,Z,"","f"),a(this,b,!0,"f");break}},R=async function(e){const i=t(this,r,"f")?.signalingState;if(i!=="have-local-offer"){console.warn("\u26A0 \u5FFD\u7565\u91CD\u590D answer\uFF0C\u5F53\u524D\u72B6\u6001:",i);return}else await t(this,r,"f")?.setRemoteDescription(e)},P=function(){t(this,m,"f")&&(t(this,m,"f").close(),a(this,m,null,"f"))},J=function(e){const n=w=>(w&w-1)===0,d=w=>{let u=1;for(;u*2<=w;)u*=2;return u};let c=Math.min(Math.max(e,32),32768);return n(c)||(c=d(c)),c},x=async function(){if(!t(this,r,"f")||!t(this,l,"f")||t(this,l,"f").readyState!==WebSocket.OPEN)return;const e=await t(this,r,"f").createOffer();await t(this,r,"f").setLocalDescription(e),t(this,l,"f").send(JSON.stringify({type:"offer",data:e}))},U=function(){t(this,r,"f")?.getSenders().forEach(e=>e.track?.stop()),t(this,r,"f")?.close(),a(this,r,null,"f")},$=function(){t(this,l,"f")&&t(this,l,"f").readyState===WebSocket.OPEN&&t(this,l,"f").close(),a(this,l,null,"f")};export{B as default};
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ export type voice = {
|
|
|
4
4
|
video?: boolean;
|
|
5
5
|
};
|
|
6
6
|
onOpen: () => void;
|
|
7
|
-
onTrack: (event: any) => void;
|
|
8
7
|
onError: (event: any) => void;
|
|
9
8
|
onMessage: (event: any) => void;
|
|
10
9
|
onAuth: (event: any) => void;
|
|
@@ -36,7 +35,6 @@ declare class ZannVoice {
|
|
|
36
35
|
* video: false 是否开启摄像头
|
|
37
36
|
* }
|
|
38
37
|
* @param onOpen 监听到连接成功后发生
|
|
39
|
-
* @param onTrack 监听到媒体流
|
|
40
38
|
* @param onError 创建出现了问题
|
|
41
39
|
* @param onMessage 监听websocket 发来的内容
|
|
42
40
|
* @param onAuth 当需要授权时候发生
|
|
@@ -44,7 +42,7 @@ declare class ZannVoice {
|
|
|
44
42
|
* @param onCameraChange 监听摄像头变化 state 是否开启麦克风
|
|
45
43
|
* @returns
|
|
46
44
|
*/
|
|
47
|
-
connect({ media, onOpen,
|
|
45
|
+
connect({ media, onOpen, onError, onMessage, onAuth, onMicrophoneChange, onCameraChange }: voice): Promise<void>;
|
|
48
46
|
/**
|
|
49
47
|
* 开启麦克风
|
|
50
48
|
* @returns
|