@lunora/vue 1.0.0-alpha.113 → 1.0.0-alpha.115
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/dist/index.d.mts
CHANGED
|
@@ -977,8 +977,24 @@ interface MicrophoneConfig {
|
|
|
977
977
|
interruptChunks: number;
|
|
978
978
|
/** RMS above which the user is considered to be barging in while the agent speaks. */
|
|
979
979
|
interruptThreshold: number;
|
|
980
|
-
/**
|
|
981
|
-
|
|
980
|
+
/**
|
|
981
|
+
* `true` from the moment a turn is committed until it completes — the whole
|
|
982
|
+
* `thinking` + `speaking` window, not just the audible half.
|
|
983
|
+
*
|
|
984
|
+
* It gates BOTH branches below, and the wider span is the point. Gated only
|
|
985
|
+
* on "audibly speaking", turn detection kept running through the entire
|
|
986
|
+
* STT+LLM window after a `commit`: room noise at the (deliberately low)
|
|
987
|
+
* `silenceThreshold` re-armed `sawSpeech`, another quiet gap fired a SECOND
|
|
988
|
+
* `commit`, and the DO refused it with "a turn is already in progress" —
|
|
989
|
+
* a refusal that returns before draining the audio buffer, so the PCM
|
|
990
|
+
* captured since the first commit leaked into the next utterance.
|
|
991
|
+
*
|
|
992
|
+
* A genuine barge-in still works in that window: it routes through the
|
|
993
|
+
* `onInterrupt` branch, which needs `interruptChunks` consecutive chunks at
|
|
994
|
+
* `interruptThreshold` — an order of magnitude above `silenceThreshold` —
|
|
995
|
+
* and `interrupt` is exactly what the DO tells the client to send.
|
|
996
|
+
*/
|
|
997
|
+
isTurnActive: () => boolean;
|
|
982
998
|
/** One 16 kHz mono 16-bit little-endian PCM frame captured from the mic. */
|
|
983
999
|
onAudio: (pcm: Uint8Array) => void;
|
|
984
1000
|
/** A barge-in was detected (RMS spike while the agent is speaking). */
|
package/dist/index.d.ts
CHANGED
|
@@ -977,8 +977,24 @@ interface MicrophoneConfig {
|
|
|
977
977
|
interruptChunks: number;
|
|
978
978
|
/** RMS above which the user is considered to be barging in while the agent speaks. */
|
|
979
979
|
interruptThreshold: number;
|
|
980
|
-
/**
|
|
981
|
-
|
|
980
|
+
/**
|
|
981
|
+
* `true` from the moment a turn is committed until it completes — the whole
|
|
982
|
+
* `thinking` + `speaking` window, not just the audible half.
|
|
983
|
+
*
|
|
984
|
+
* It gates BOTH branches below, and the wider span is the point. Gated only
|
|
985
|
+
* on "audibly speaking", turn detection kept running through the entire
|
|
986
|
+
* STT+LLM window after a `commit`: room noise at the (deliberately low)
|
|
987
|
+
* `silenceThreshold` re-armed `sawSpeech`, another quiet gap fired a SECOND
|
|
988
|
+
* `commit`, and the DO refused it with "a turn is already in progress" —
|
|
989
|
+
* a refusal that returns before draining the audio buffer, so the PCM
|
|
990
|
+
* captured since the first commit leaked into the next utterance.
|
|
991
|
+
*
|
|
992
|
+
* A genuine barge-in still works in that window: it routes through the
|
|
993
|
+
* `onInterrupt` branch, which needs `interruptChunks` consecutive chunks at
|
|
994
|
+
* `interruptThreshold` — an order of magnitude above `silenceThreshold` —
|
|
995
|
+
* and `interrupt` is exactly what the DO tells the client to send.
|
|
996
|
+
*/
|
|
997
|
+
isTurnActive: () => boolean;
|
|
982
998
|
/** One 16 kHz mono 16-bit little-endian PCM frame captured from the mic. */
|
|
983
999
|
onAudio: (pcm: Uint8Array) => void;
|
|
984
1000
|
/** A barge-in was detected (RMS spike while the agent is speaking). */
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{AuthLoading as r,Authenticated as t,Unauthenticated as u}from"./packem_shared/AuthLoading-nfI43dvD.mjs";import{hydratePreloaded as a}from"./packem_shared/hydratePreloaded-BpzZkPH4.mjs";import{LUNORA_INJECTION_KEY as f,createLunora as m,provideLunora as p,useLunora as x}from"./packem_shared/LUNORA_INJECTION_KEY-Bct9tKCj.mjs";import{useAction as A}from"./packem_shared/useAction-C4VLsh4M.mjs";import{useAgent as d}from"./packem_shared/useAgent-By6FSLfF.mjs";import{useAgentChat as h}from"./packem_shared/useAgentChat-Dv_y3vQH.mjs";import{useAgentState as l}from"./packem_shared/useAgentState-BYx1l9vy.mjs";import{useAgentToolEvents as Q}from"./packem_shared/useAgentToolEvents-BMwcZ2G9.mjs";import{useAuth as b}from"./packem_shared/useAuth-DJ9902_4.mjs";import{default as E}from"./packem_shared/useConnectionStatus-BSFJBnmi.mjs";import{useFlag as N,useFlags as P}from"./packem_shared/useFlag-xVfWC_c6.mjs";import{useMutation as v}from"./packem_shared/useMutation-D6PkaNaE.mjs";import{useMutator as M}from"./packem_shared/useMutator-CO7nXlRf.mjs";import{useInfiniteQuery as R,usePaginatedQuery as U}from"./packem_shared/useInfiniteQuery-2KHGc4Ss.mjs";import{usePresence as J}from"./packem_shared/usePresence-BOs1lDlC.mjs";import{subscribeToQuery as V,useQuery as Y}from"./packem_shared/subscribeToQuery-G5w094pU.mjs";import{useRateLimit as k}from"./packem_shared/useRateLimit-zoPsflSG.mjs";import{useStream as w}from"./packem_shared/useStream-Ca4Rkw4p.mjs";import{useSubscription as B}from"./packem_shared/useSubscription-yGJRK0w-.mjs";import{useVoiceAgent as G}from"./packem_shared/useVoiceAgent-
|
|
1
|
+
import{AuthLoading as r,Authenticated as t,Unauthenticated as u}from"./packem_shared/AuthLoading-nfI43dvD.mjs";import{hydratePreloaded as a}from"./packem_shared/hydratePreloaded-BpzZkPH4.mjs";import{LUNORA_INJECTION_KEY as f,createLunora as m,provideLunora as p,useLunora as x}from"./packem_shared/LUNORA_INJECTION_KEY-Bct9tKCj.mjs";import{useAction as A}from"./packem_shared/useAction-C4VLsh4M.mjs";import{useAgent as d}from"./packem_shared/useAgent-By6FSLfF.mjs";import{useAgentChat as h}from"./packem_shared/useAgentChat-Dv_y3vQH.mjs";import{useAgentState as l}from"./packem_shared/useAgentState-BYx1l9vy.mjs";import{useAgentToolEvents as Q}from"./packem_shared/useAgentToolEvents-BMwcZ2G9.mjs";import{useAuth as b}from"./packem_shared/useAuth-DJ9902_4.mjs";import{default as E}from"./packem_shared/useConnectionStatus-BSFJBnmi.mjs";import{useFlag as N,useFlags as P}from"./packem_shared/useFlag-xVfWC_c6.mjs";import{useMutation as v}from"./packem_shared/useMutation-D6PkaNaE.mjs";import{useMutator as M}from"./packem_shared/useMutator-CO7nXlRf.mjs";import{useInfiniteQuery as R,usePaginatedQuery as U}from"./packem_shared/useInfiniteQuery-2KHGc4Ss.mjs";import{usePresence as J}from"./packem_shared/usePresence-BOs1lDlC.mjs";import{subscribeToQuery as V,useQuery as Y}from"./packem_shared/subscribeToQuery-G5w094pU.mjs";import{useRateLimit as k}from"./packem_shared/useRateLimit-zoPsflSG.mjs";import{useStream as w}from"./packem_shared/useStream-Ca4Rkw4p.mjs";import{useSubscription as B}from"./packem_shared/useSubscription-yGJRK0w-.mjs";import{useVoiceAgent as G}from"./packem_shared/useVoiceAgent-BEGHhbmX.mjs";export{r as AuthLoading,t as Authenticated,f as LUNORA_INJECTION_KEY,u as Unauthenticated,m as createLunora,a as hydratePreloaded,p as provideLunora,V as subscribeToQuery,A as useAction,d as useAgent,h as useAgentChat,l as useAgentState,Q as useAgentToolEvents,b as useAuth,E as useConnectionStatus,N as useFlag,P as useFlags,R as useInfiniteQuery,x as useLunora,v as useMutation,M as useMutator,U as usePaginatedQuery,J as usePresence,Y as useQuery,k as useRateLimit,w as useStream,B as useSubscription,G as useVoiceAgent};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ref as y,onScopeDispose as N,toValue as O}from"vue";import{useLunora as P}from"./LUNORA_INJECTION_KEY-Bct9tKCj.mjs";const M="/_lunora/ws",H="/_lunora/voice/",$=4001,I=e=>e.startsWith("https://")?`wss://${e.slice(8)}`:e.startsWith("http://")?`ws://${e.slice(7)}`:e,B=e=>{const r=e.startsWith("agents:")?e.slice(7):e;return r.endsWith("Voice")?r.slice(0,-5):r},K=(e,r)=>{if(r===void 0||r==="")return I(e);if(r.endsWith(M))return r.slice(0,-M.length);try{return new URL(r).origin}catch{return I(e)}},q=e=>{const r=K(e.httpUrl,e.wsUrl),s=r.endsWith("/")?r.slice(0,-1):r,i=new URLSearchParams({threadKey:e.threadKey});return`${s}${H}${encodeURIComponent(e.agent)}?${i.toString()}`},J=(e,r)=>{if(r?.code===$)return new Error(`${e}: authentication token expired — refresh the credential and start a new call`)},G=(e,r,s)=>{const i=e.currentIdentity();return e.onAuthTokenChange(()=>{e.currentIdentity()!==i&&s(new Error(`${r}: the signed-in identity changed during the call — the session was ended; start a new call`))})},X=16e3,j=e=>{if(e.length===0)return 0;let r=0;for(const s of e)r+=s*s;return Math.sqrt(r/e.length)},z=(e,r)=>{const s=r/X,i=s>1?Math.floor(e.length/s):e.length,g=new ArrayBuffer(i*2),p=new DataView(g);for(let l=0;l<i;l+=1){const k=e[Math.floor(l*s)]??0,v=Math.max(-1,Math.min(1,k));p.setInt16(l*2,v<0?v*32768:v*32767,!0)}return new Uint8Array(g)},Q=async e=>{const r=globalThis,s=r.navigator?.mediaDevices?.getUserMedia.bind(r.navigator.mediaDevices),i=r.AudioContext??r.webkitAudioContext;if(!s||!i)throw new Error("useVoiceAgent: microphone capture requires getUserMedia + AudioContext (no browser audio available)");const g=await s({audio:{channelCount:1,echoCancellation:!0,noiseSuppression:!0}}),p=new i,l=p.createMediaStreamSource(g),k=p.createScriptProcessor(4096,1,1);let v=!1,w=!1,c=0,o=0;return k.onaudioprocess=u=>{const d=u.inputBuffer.getChannelData(0),f=v?0:j(d);if(e.onLevel(f),v)return;if(e.onAudio(z(d,p.sampleRate)),e.isTurnActive()){o=f>=e.interruptThreshold?o+1:0,o>=e.interruptChunks&&(o=0,e.onInterrupt());return}o=0;const S=d.length/p.sampleRate*1e3;if(f>=e.silenceThreshold){w=!0,c=0;return}w&&(c+=S,c>=e.silenceDurationMs&&(w=!1,c=0,e.onSilence()))},l.connect(k),k.connect(p.destination),{setMuted:u=>{v=u},stop:()=>{k.disconnect(),l.disconnect();for(const u of g.getTracks())u.stop();p.close()}}},Y=()=>{const e=globalThis,r=e.AudioContext??e.webkitAudioContext;if(!r)throw new Error("useVoiceAgent: audio playback requires AudioContext (no browser audio available)");const s=new r,i=new Set;let g=0,p=Promise.resolve(),l=0;const k=async(c,o)=>{if(o!==l)return;let u;try{u=await s.decodeAudioData(c.buffer)}catch{return}if(o!==l)return;const d=s.createBufferSource();d.buffer=u,d.connect(s.destination);const f=Math.max(s.currentTime,g);d.start(f),g=f+u.duration,i.add(d),d.onended=()=>{i.delete(d)}},v=c=>{const o=Uint8Array.from(c),u=l;p=p.then(()=>k(o,u))},w=()=>{l+=1;for(const c of i)try{c.stop()}catch{}i.clear(),g=s.currentTime};return{enqueue:v,interrupt:w,stop:()=>{w(),s.close()}}},U=1,Z=.01,ee=1200,te=.15,ne=3,ae=e=>{const{createMicrophone:r=Q,createSpeaker:s=Y,createSocket:i,interruptChunks:g=ne,interruptThreshold:p=te,silenceDurationMs:l=ee,silenceThreshold:k=Z,threadKey:v,voice:w}=e,c=P(),o=y("idle"),u=y(!1),d=y(""),f=y(""),S=y(0),C=y(!1),m=y(void 0);let a,b=!1;const _=t=>{const n=a?.socket;return n?.readyState===U?(n.send(JSON.stringify(t)),!0):!1},T=()=>{const t=a;if(a=void 0,t){t.unwatchIdentity?.(),t.microphone?.stop(),t.speaker?.stop(),t.socket.onmessage=null,t.socket.onerror=null,t.socket.onclose=null;try{t.socket.close()}catch{}}b=!1,u.value=!1,o.value="idle",S.value=0},R=T,D=t=>{const n=a;switch(t.type){case"assistant_delta":{n&&(n.speaking=!0),o.value="speaking",f.value+=t.text;break}case"assistant_done":{n&&(n.awaitingTurn=!1,n.speaking=!1),f.value=t.text,o.value="listening";break}case"error":{n&&(n.awaitingTurn=!1,n.speaking=!1),m.value=new Error(t.message),o.value="listening";break}case"interrupted":{n&&(n.awaitingTurn=!1,n.speaking=!1,n.suppressAudio=!1),n?.speaker?.interrupt(),o.value="listening";break}case"ready":{n&&(n.audioFormat=t.audioFormat,n.suppressAudio=!1),u.value=!0,o.value="listening";break}case"user_transcript":{n&&(n.suppressAudio=!1),d.value=t.text,f.value="",o.value="thinking";break}}},L=t=>{const n=a;!n||n.suppressAudio||(n.speaker??=s({audioFormat:n.audioFormat}),n.speaking=!0,o.value="speaking",n.speaker.enqueue(t))},V=async()=>{if(a||b)return;b=!0,m.value=void 0,d.value="",f.value="";let t;try{const n=q({agent:B(w.__lunoraRef),httpUrl:c.url,threadKey:O(v),wsUrl:c.wsUrl}),A=(i??(h=>{const E=c.getWebSocketImpl();if(!E)throw new Error("useVoiceAgent: no WebSocket implementation available (pass createSocket explicitly)");return new E(h)}))(n);A.binaryType="arraybuffer",t={audioFormat:"mp3",microphone:void 0,socket:A,speaker:void 0,speaking:!1,awaitingTurn:!1,suppressAudio:!1,unwatchIdentity:void 0},a=t,t.unwatchIdentity=G(c,"useVoiceAgent",h=>{a===t&&(m.value=h,T())}),A.onmessage=h=>{if(typeof h.data=="string"){try{D(JSON.parse(h.data))}catch{}return}L(new Uint8Array(h.data))},A.onerror=()=>{m.value=new Error("useVoiceAgent: voice socket error")},A.onclose=h=>{if(a!==t)return;const E=J("useVoiceAgent",h);E&&(m.value=E),T()};const x=await r({interruptChunks:g,interruptThreshold:p,isTurnActive:()=>(a?.speaking??!1)||(a?.awaitingTurn??!1),onAudio:h=>{A.readyState===U&&A.send(h)},onInterrupt:()=>{_({type:"interrupt"}),a?.speaker?.interrupt(),a&&(a.awaitingTurn=!1,a.speaking=!1,a.suppressAudio=!0),o.value="listening"},onLevel:h=>{S.value=h},onSilence:()=>{_({type:"commit"}),a&&(a.awaitingTurn=!0),o.value="thinking"},silenceDurationMs:l,silenceThreshold:k});a===t?(t.microphone=x,C.value=!1,t.speaking||(o.value="listening")):x.stop()}catch(n){a===t&&(m.value=n instanceof Error?n:new Error(String(n)),T())}finally{b=!1}},F=()=>{const t=!C.value;return a?.microphone?.setMuted(t),C.value=t,t},W=t=>{_({text:t,type:"text"})&&(a&&(a.awaitingTurn=!0),o.value="thinking")};return N(T),{audioLevel:S,connected:u,endCall:R,error:m,interimTranscript:f,isMuted:C,sendText:W,startCall:V,status:o,toggleMute:F,transcript:d}};export{ae as useVoiceAgent};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/vue",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.115",
|
|
4
4
|
"description": "Vue adapter for Lunora — live composables, optimistic mutations, and reactive loaders",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -54,9 +54,9 @@
|
|
|
54
54
|
"access": "public"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@lunora/client": "1.0.0-alpha.
|
|
58
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
59
|
-
"@lunora/ratelimit": "1.0.0-alpha.
|
|
57
|
+
"@lunora/client": "1.0.0-alpha.80",
|
|
58
|
+
"@lunora/errors": "1.0.0-alpha.32",
|
|
59
|
+
"@lunora/ratelimit": "1.0.0-alpha.39",
|
|
60
60
|
"@visulima/storage-client": "1.0.2"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{ref as A,onScopeDispose as V,toValue as O}from"vue";import{useLunora as P}from"./LUNORA_INJECTION_KEY-Bct9tKCj.mjs";const M="/_lunora/ws",H="/_lunora/voice/",$=4001,U=e=>e.startsWith("https://")?`wss://${e.slice(8)}`:e.startsWith("http://")?`ws://${e.slice(7)}`:e,B=e=>{const o=e.startsWith("agents:")?e.slice(7):e;return o.endsWith("Voice")?o.slice(0,-5):o},K=(e,o)=>{if(o===void 0||o==="")return U(e);if(o.endsWith(M))return o.slice(0,-M.length);try{return new URL(o).origin}catch{return U(e)}},q=e=>{const o=K(e.httpUrl,e.wsUrl),s=o.endsWith("/")?o.slice(0,-1):o,c=new URLSearchParams({threadKey:e.threadKey});return`${s}${H}${encodeURIComponent(e.agent)}?${c.toString()}`},J=(e,o)=>{if(o?.code===$)return new Error(`${e}: authentication token expired — refresh the credential and start a new call`)},G=16e3,X=e=>{if(e.length===0)return 0;let o=0;for(const s of e)o+=s*s;return Math.sqrt(o/e.length)},j=(e,o)=>{const s=o/G,c=s>1?Math.floor(e.length/s):e.length,f=new ArrayBuffer(c*2),p=new DataView(f);for(let u=0;u<c;u+=1){const v=e[Math.floor(u*s)]??0,k=Math.max(-1,Math.min(1,v));p.setInt16(u*2,k<0?k*32768:k*32767,!0)}return new Uint8Array(f)},z=async e=>{const o=globalThis,s=o.navigator?.mediaDevices?.getUserMedia.bind(o.navigator.mediaDevices),c=o.AudioContext??o.webkitAudioContext;if(!s||!c)throw new Error("useVoiceAgent: microphone capture requires getUserMedia + AudioContext (no browser audio available)");const f=await s({audio:{channelCount:1,echoCancellation:!0,noiseSuppression:!0}}),p=new c,u=p.createMediaStreamSource(f),v=p.createScriptProcessor(4096,1,1);let k=!1,m=!1,l=0,r=0;return v.onaudioprocess=i=>{const d=i.inputBuffer.getChannelData(0),h=k?0:X(d);if(e.onLevel(h),k)return;if(e.onAudio(j(d,p.sampleRate)),e.isSpeaking()){r=h>=e.interruptThreshold?r+1:0,r>=e.interruptChunks&&(r=0,e.onInterrupt());return}r=0;const y=d.length/p.sampleRate*1e3;if(h>=e.silenceThreshold){m=!0,l=0;return}m&&(l+=y,l>=e.silenceDurationMs&&(m=!1,l=0,e.onSilence()))},u.connect(v),v.connect(p.destination),{setMuted:i=>{k=i},stop:()=>{v.disconnect(),u.disconnect();for(const i of f.getTracks())i.stop();p.close()}}},Q=()=>{const e=globalThis,o=e.AudioContext??e.webkitAudioContext;if(!o)throw new Error("useVoiceAgent: audio playback requires AudioContext (no browser audio available)");const s=new o,c=new Set;let f=0,p=Promise.resolve(),u=0;const v=async(l,r)=>{if(r!==u)return;let i;try{i=await s.decodeAudioData(l.buffer)}catch{return}if(r!==u)return;const d=s.createBufferSource();d.buffer=i,d.connect(s.destination);const h=Math.max(s.currentTime,f);d.start(h),f=h+i.duration,c.add(d),d.onended=()=>{c.delete(d)}},k=l=>{const r=Uint8Array.from(l),i=u;p=p.then(()=>v(r,i))},m=()=>{u+=1;for(const l of c)try{l.stop()}catch{}c.clear(),f=s.currentTime};return{enqueue:k,interrupt:m,stop:()=>{m(),s.close()}}},R=1,Y=.01,Z=1200,ee=.15,te=3,se=e=>{const{createMicrophone:o=z,createSpeaker:s=Q,createSocket:c,interruptChunks:f=te,interruptThreshold:p=ee,silenceDurationMs:u=Z,silenceThreshold:v=Y,threadKey:k,voice:m}=e,l=P(),r=A("idle"),i=A(!1),d=A(""),h=A(""),y=A(0),b=A(!1),w=A(void 0);let a,C=!1;const _=t=>{const n=a?.socket;return n?.readyState===R?(n.send(JSON.stringify(t)),!0):!1},T=()=>{const t=a;if(a=void 0,t){t.microphone?.stop(),t.speaker?.stop(),t.socket.onmessage=null,t.socket.onerror=null,t.socket.onclose=null;try{t.socket.close()}catch{}}C=!1,i.value=!1,r.value="idle",y.value=0},D=T,L=t=>{const n=a;switch(t.type){case"assistant_delta":{n&&(n.speaking=!0),r.value="speaking",h.value+=t.text;break}case"assistant_done":{n&&(n.speaking=!1),h.value=t.text,r.value="listening";break}case"error":{n&&(n.speaking=!1),w.value=new Error(t.message),r.value="listening";break}case"interrupted":{n&&(n.speaking=!1,n.suppressAudio=!1),n?.speaker?.interrupt(),r.value="listening";break}case"ready":{n&&(n.audioFormat=t.audioFormat,n.suppressAudio=!1),i.value=!0,r.value="listening";break}case"user_transcript":{n&&(n.suppressAudio=!1),d.value=t.text,h.value="",r.value="thinking";break}}},I=t=>{const n=a;!n||n.suppressAudio||(n.speaker??=s({audioFormat:n.audioFormat}),n.speaking=!0,r.value="speaking",n.speaker.enqueue(t))},F=async()=>{if(a||C)return;C=!0,w.value=void 0,d.value="",h.value="";let t;try{const n=q({agent:B(m.__lunoraRef),httpUrl:l.url,threadKey:O(k),wsUrl:l.wsUrl}),S=(c??(g=>{const E=l.getWebSocketImpl();if(!E)throw new Error("useVoiceAgent: no WebSocket implementation available (pass createSocket explicitly)");return new E(g)}))(n);S.binaryType="arraybuffer",t={audioFormat:"mp3",microphone:void 0,socket:S,speaker:void 0,speaking:!1,suppressAudio:!1},a=t,S.onmessage=g=>{if(typeof g.data=="string"){try{L(JSON.parse(g.data))}catch{}return}I(new Uint8Array(g.data))},S.onerror=()=>{w.value=new Error("useVoiceAgent: voice socket error")},S.onclose=g=>{if(a!==t)return;const E=J("useVoiceAgent",g);E&&(w.value=E),T()};const x=await o({interruptChunks:f,interruptThreshold:p,isSpeaking:()=>a?.speaking??!1,onAudio:g=>{S.readyState===R&&S.send(g)},onInterrupt:()=>{_({type:"interrupt"}),a?.speaker?.interrupt(),a&&(a.speaking=!1,a.suppressAudio=!0),r.value="listening"},onLevel:g=>{y.value=g},onSilence:()=>{_({type:"commit"}),r.value="thinking"},silenceDurationMs:u,silenceThreshold:v});a===t?(t.microphone=x,b.value=!1,t.speaking||(r.value="listening")):x.stop()}catch(n){a===t&&(w.value=n instanceof Error?n:new Error(String(n)),T())}finally{C=!1}},W=()=>{const t=!b.value;return a?.microphone?.setMuted(t),b.value=t,t},N=t=>{_({text:t,type:"text"})&&(r.value="thinking")};return V(T),{audioLevel:y,connected:i,endCall:D,error:w,interimTranscript:h,isMuted:b,sendText:N,startCall:F,status:r,toggleMute:W,transcript:d}};export{se as useVoiceAgent};
|