@lunora/solid 1.0.0-alpha.81 → 1.0.0-alpha.82

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
@@ -934,8 +934,24 @@ interface MicrophoneConfig {
934
934
  interruptChunks: number;
935
935
  /** RMS above which the user is considered to be barging in while the agent speaks. */
936
936
  interruptThreshold: number;
937
- /** `true` while `status === "speaking"` — gates barge-in detection. */
938
- isSpeaking: () => boolean;
937
+ /**
938
+ * `true` from the moment a turn is committed until it completes — the whole
939
+ * `thinking` + `speaking` window, not just the audible half.
940
+ *
941
+ * It gates BOTH branches below, and the wider span is the point. Gated only
942
+ * on "audibly speaking", turn detection kept running through the entire
943
+ * STT+LLM window after a `commit`: room noise at the (deliberately low)
944
+ * `silenceThreshold` re-armed `sawSpeech`, another quiet gap fired a SECOND
945
+ * `commit`, and the DO refused it with "a turn is already in progress" —
946
+ * a refusal that returns before draining the audio buffer, so the PCM
947
+ * captured since the first commit leaked into the next utterance.
948
+ *
949
+ * A genuine barge-in still works in that window: it routes through the
950
+ * `onInterrupt` branch, which needs `interruptChunks` consecutive chunks at
951
+ * `interruptThreshold` — an order of magnitude above `silenceThreshold` —
952
+ * and `interrupt` is exactly what the DO tells the client to send.
953
+ */
954
+ isTurnActive: () => boolean;
939
955
  /** One 16 kHz mono 16-bit little-endian PCM frame captured from the mic. */
940
956
  onAudio: (pcm: Uint8Array) => void;
941
957
  /** A barge-in was detected (RMS spike while the agent is speaking). */
package/dist/index.d.ts CHANGED
@@ -934,8 +934,24 @@ interface MicrophoneConfig {
934
934
  interruptChunks: number;
935
935
  /** RMS above which the user is considered to be barging in while the agent speaks. */
936
936
  interruptThreshold: number;
937
- /** `true` while `status === "speaking"` — gates barge-in detection. */
938
- isSpeaking: () => boolean;
937
+ /**
938
+ * `true` from the moment a turn is committed until it completes — the whole
939
+ * `thinking` + `speaking` window, not just the audible half.
940
+ *
941
+ * It gates BOTH branches below, and the wider span is the point. Gated only
942
+ * on "audibly speaking", turn detection kept running through the entire
943
+ * STT+LLM window after a `commit`: room noise at the (deliberately low)
944
+ * `silenceThreshold` re-armed `sawSpeech`, another quiet gap fired a SECOND
945
+ * `commit`, and the DO refused it with "a turn is already in progress" —
946
+ * a refusal that returns before draining the audio buffer, so the PCM
947
+ * captured since the first commit leaked into the next utterance.
948
+ *
949
+ * A genuine barge-in still works in that window: it routes through the
950
+ * `onInterrupt` branch, which needs `interruptChunks` consecutive chunks at
951
+ * `interruptThreshold` — an order of magnitude above `silenceThreshold` —
952
+ * and `interrupt` is exactly what the DO tells the client to send.
953
+ */
954
+ isTurnActive: () => boolean;
939
955
  /** One 16 kHz mono 16-bit little-endian PCM frame captured from the mic. */
940
956
  onAudio: (pcm: Uint8Array) => void;
941
957
  /** A barge-in was detected (RMS spike while the agent is speaking). */
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{LunoraContext as r,useLunora as o}from"./packem_shared/LunoraContext-dPcjtn6R.mjs";import{createAction as c,createActionForClient as n}from"./packem_shared/createAction-M4s8XBXV.mjs";import{createAgent as m}from"./packem_shared/createAgent-hhL-sw_X.mjs";import{createAgentChat as x}from"./packem_shared/createAgentChat-CK0_VzMa.mjs";import{createAgentState as u}from"./packem_shared/createAgentState-D3UsBxBI.mjs";import{createAgentToolEvents as A}from"./packem_shared/createAgentToolEvents-BoC23C-n.mjs";import{AuthLoading as l,Authenticated as s,Unauthenticated as h,createAuth as C}from"./packem_shared/AuthLoading-6vueEnV0.mjs";import{default as y}from"./packem_shared/createConnectionStatus-BnlXYWeE.mjs";import{createFlag as P,createFlags as S}from"./packem_shared/createFlag-BmHiFrD-.mjs";import{createMutation as Q,createMutationForClient as v}from"./packem_shared/createMutation-DcSsIbGF.mjs";import{createMutator as E}from"./packem_shared/createMutator-q9cMjGKv.mjs";import{createInfiniteQuery as R,createPaginatedQuery as T}from"./packem_shared/createInfiniteQuery-B_UTdD9R.mjs";import{createPresence as V}from"./packem_shared/createPresence-eSor6sjx.mjs";import{createQuery as k}from"./packem_shared/createQuery-CdWTlKJM.mjs";import{createRateLimit as w}from"./packem_shared/createRateLimit-CIVWrD5H.mjs";import{createStream as B}from"./packem_shared/createStream-Dx7SnivS.mjs";import{createSubscription as G}from"./packem_shared/createSubscription-1glD6fxF.mjs";import{createVoiceAgent as J}from"./packem_shared/createVoiceAgent-DOJL_HAh.mjs";import{default as N}from"./packem_shared/hydratePreloaded-Bhxdstm1.mjs";import{LunoraProvider as W}from"./packem_shared/LunoraProvider-BKWckcDR.mjs";export{l as AuthLoading,s as Authenticated,r as LunoraContext,W as LunoraProvider,h as Unauthenticated,c as createAction,n as createActionForClient,m as createAgent,x as createAgentChat,u as createAgentState,A as createAgentToolEvents,C as createAuth,y as createConnectionStatus,P as createFlag,S as createFlags,R as createInfiniteQuery,Q as createMutation,v as createMutationForClient,E as createMutator,T as createPaginatedQuery,V as createPresence,k as createQuery,w as createRateLimit,B as createStream,G as createSubscription,J as createVoiceAgent,N as hydratePreloaded,o as useLunora};
1
+ import{LunoraContext as r,useLunora as o}from"./packem_shared/LunoraContext-dPcjtn6R.mjs";import{createAction as c,createActionForClient as n}from"./packem_shared/createAction-M4s8XBXV.mjs";import{createAgent as m}from"./packem_shared/createAgent-hhL-sw_X.mjs";import{createAgentChat as x}from"./packem_shared/createAgentChat-CK0_VzMa.mjs";import{createAgentState as u}from"./packem_shared/createAgentState-D3UsBxBI.mjs";import{createAgentToolEvents as A}from"./packem_shared/createAgentToolEvents-BoC23C-n.mjs";import{AuthLoading as l,Authenticated as s,Unauthenticated as h,createAuth as C}from"./packem_shared/AuthLoading-6vueEnV0.mjs";import{default as y}from"./packem_shared/createConnectionStatus-BnlXYWeE.mjs";import{createFlag as P,createFlags as S}from"./packem_shared/createFlag-BmHiFrD-.mjs";import{createMutation as Q,createMutationForClient as v}from"./packem_shared/createMutation-DcSsIbGF.mjs";import{createMutator as E}from"./packem_shared/createMutator-q9cMjGKv.mjs";import{createInfiniteQuery as R,createPaginatedQuery as T}from"./packem_shared/createInfiniteQuery-B_UTdD9R.mjs";import{createPresence as V}from"./packem_shared/createPresence-eSor6sjx.mjs";import{createQuery as k}from"./packem_shared/createQuery-CdWTlKJM.mjs";import{createRateLimit as w}from"./packem_shared/createRateLimit-CIVWrD5H.mjs";import{createStream as B}from"./packem_shared/createStream-Dx7SnivS.mjs";import{createSubscription as G}from"./packem_shared/createSubscription-1glD6fxF.mjs";import{createVoiceAgent as J}from"./packem_shared/createVoiceAgent-BM9H7p7P.mjs";import{default as N}from"./packem_shared/hydratePreloaded-Bhxdstm1.mjs";import{LunoraProvider as W}from"./packem_shared/LunoraProvider-BKWckcDR.mjs";export{l as AuthLoading,s as Authenticated,r as LunoraContext,W as LunoraProvider,h as Unauthenticated,c as createAction,n as createActionForClient,m as createAgent,x as createAgentChat,u as createAgentState,A as createAgentToolEvents,C as createAuth,y as createConnectionStatus,P as createFlag,S as createFlags,R as createInfiniteQuery,Q as createMutation,v as createMutationForClient,E as createMutator,T as createPaginatedQuery,V as createPresence,k as createQuery,w as createRateLimit,B as createStream,G as createSubscription,J as createVoiceAgent,N as hydratePreloaded,o as useLunora};
@@ -0,0 +1 @@
1
+ import{createSignal as y,onCleanup as J}from"solid-js";import{useLunora as G}from"./LunoraContext-dPcjtn6R.mjs";import{resolveMaybe as X}from"./createAgent-hhL-sw_X.mjs";const D="/_lunora/ws",j="/_lunora/voice/",z=4001,F=e=>e.startsWith("https://")?`wss://${e.slice(8)}`:e.startsWith("http://")?`ws://${e.slice(7)}`:e,Q=e=>{const r=e.startsWith("agents:")?e.slice(7):e;return r.endsWith("Voice")?r.slice(0,-5):r},Y=(e,r)=>{if(r===void 0||r==="")return F(e);if(r.endsWith(D))return r.slice(0,-D.length);try{return new URL(r).origin}catch{return F(e)}},Z=e=>{const r=Y(e.httpUrl,e.wsUrl),s=r.endsWith("/")?r.slice(0,-1):r,i=new URLSearchParams({threadKey:e.threadKey});return`${s}${j}${encodeURIComponent(e.agent)}?${i.toString()}`},ee=(e,r)=>{if(r?.code===z)return new Error(`${e}: authentication token expired — refresh the credential and start a new call`)},te=(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`))})},ne=16e3,re=e=>{if(e.length===0)return 0;let r=0;for(const s of e)r+=s*s;return Math.sqrt(r/e.length)},oe=(e,r)=>{const s=r/ne,i=s>1?Math.floor(e.length/s):e.length,f=new ArrayBuffer(i*2),l=new DataView(f);for(let u=0;u<i;u+=1){const k=e[Math.floor(u*s)]??0,g=Math.max(-1,Math.min(1,k));l.setInt16(u*2,g<0?g*32768:g*32767,!0)}return new Uint8Array(f)},se=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("createVoiceAgent: microphone capture requires getUserMedia + AudioContext (no browser audio available)");const f=await s({audio:{channelCount:1,echoCancellation:!0,noiseSuppression:!0}}),l=new i,u=l.createMediaStreamSource(f),k=l.createScriptProcessor(4096,1,1);let g=!1,w=!1,c=0,p=0;return k.onaudioprocess=o=>{const h=o.inputBuffer.getChannelData(0),m=g?0:re(h);if(e.onLevel(m),g)return;if(e.onAudio(oe(h,l.sampleRate)),e.isTurnActive()){p=m>=e.interruptThreshold?p+1:0,p>=e.interruptChunks&&(p=0,e.onInterrupt());return}p=0;const b=h.length/l.sampleRate*1e3;if(m>=e.silenceThreshold){w=!0,c=0;return}w&&(c+=b,c>=e.silenceDurationMs&&(w=!1,c=0,e.onSilence()))},u.connect(k),k.connect(l.destination),{setMuted:o=>{g=o},stop:()=>{k.disconnect(),u.disconnect();for(const o of f.getTracks())o.stop();l.close()}}},ae=()=>{const e=globalThis,r=e.AudioContext??e.webkitAudioContext;if(!r)throw new Error("createVoiceAgent: audio playback requires AudioContext (no browser audio available)");const s=new r,i=new Set;let f=0,l=Promise.resolve(),u=0;const k=async(c,p)=>{if(p!==u)return;let o;try{o=await s.decodeAudioData(c.buffer)}catch{return}if(p!==u)return;const h=s.createBufferSource();h.buffer=o,h.connect(s.destination);const m=Math.max(s.currentTime,f);h.start(m),f=m+o.duration,i.add(h),h.onended=()=>{i.delete(h)}},g=c=>{const p=Uint8Array.from(c),o=u;l=l.then(()=>k(p,o))},w=()=>{u+=1;for(const c of i)try{c.stop()}catch{}i.clear(),f=s.currentTime};return{enqueue:g,interrupt:w,stop:()=>{w(),s.close()}}},V=1,ie=.01,ce=1200,ue=.15,le=3,fe=e=>{const{createMicrophone:r=se,createSpeaker:s=ae,createSocket:i,interruptChunks:f=le,interruptThreshold:l=ue,silenceDurationMs:u=ce,silenceThreshold:k=ie,threadKey:g,voice:w}=e,c=G(),[p,o]=y("idle"),[h,m]=y(!1),[b,M]=y(""),[W,v]=y(""),[N,x]=y(0),[I,U]=y(!1),[O,S]=y(void 0);let a,C=!1;const _=t=>{const n=a?.socket;return n?.readyState===V?(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{}}C=!1,m(!1),o("idle"),x(0)},P=T,H=t=>{const n=a;switch(t.type){case"assistant_delta":{n&&(n.speaking=!0),o("speaking"),v(R=>R+t.text);break}case"assistant_done":{n&&(n.awaitingTurn=!1,n.speaking=!1),v(t.text),o("listening");break}case"error":{n&&(n.awaitingTurn=!1,n.speaking=!1),S(new Error(t.message)),o("listening");break}case"interrupted":{n&&(n.awaitingTurn=!1,n.speaking=!1,n.suppressAudio=!1),n?.speaker?.interrupt(),o("listening");break}case"ready":{n&&(n.audioFormat=t.audioFormat,n.suppressAudio=!1),m(!0),o("listening");break}case"user_transcript":{n&&(n.suppressAudio=!1),M(t.text),v(""),o("thinking");break}}},$=t=>{const n=a;!n||n.suppressAudio||(n.speaker??=s({audioFormat:n.audioFormat}),n.speaking=!0,o("speaking"),n.speaker.enqueue(t))},B=async()=>{if(a||C)return;C=!0,S(void 0),M(""),v("");let t;try{const n=Z({agent:Q(w.__lunoraRef),httpUrl:c.url,threadKey:X(g),wsUrl:c.wsUrl}),A=(i??(d=>{const E=c.getWebSocketImpl();if(!E)throw new Error("createVoiceAgent: no WebSocket implementation available (pass createSocket explicitly)");return new E(d)}))(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=te(c,"createVoiceAgent",d=>{a===t&&(S(d),T())}),A.onmessage=d=>{if(typeof d.data=="string"){try{H(JSON.parse(d.data))}catch{}return}$(new Uint8Array(d.data))},A.onerror=()=>{S(new Error("createVoiceAgent: voice socket error"))},A.onclose=d=>{if(a!==t)return;const E=ee("createVoiceAgent",d);E&&S(E),T()};const L=await r({interruptChunks:f,interruptThreshold:l,isTurnActive:()=>(a?.speaking??!1)||(a?.awaitingTurn??!1),onAudio:d=>{A.readyState===V&&A.send(d)},onInterrupt:()=>{_({type:"interrupt"}),a?.speaker?.interrupt(),a&&(a.awaitingTurn=!1,a.speaking=!1,a.suppressAudio=!0),o("listening")},onLevel:d=>{x(d)},onSilence:()=>{_({type:"commit"}),a&&(a.awaitingTurn=!0),o("thinking")},silenceDurationMs:u,silenceThreshold:k});a===t?(t.microphone=L,U(!1),t.speaking||o("listening")):L.stop()}catch(n){a===t&&(S(n instanceof Error?n:new Error(String(n))),T())}finally{C=!1}},K=()=>{const t=!I();return a?.microphone?.setMuted(t),U(t),t},q=t=>{_({text:t,type:"text"})&&(a&&(a.awaitingTurn=!0),o("thinking"))};return J(T),{audioLevel:N,connected:h,endCall:P,error:O,interimTranscript:W,isMuted:I,sendText:q,startCall:B,status:p,toggleMute:K,transcript:b}};export{fe as createVoiceAgent};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/solid",
3
- "version": "1.0.0-alpha.81",
3
+ "version": "1.0.0-alpha.82",
4
4
  "description": "SolidJS adapter for Lunora — live queries, 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.78",
58
- "@lunora/errors": "1.0.0-alpha.31",
59
- "@lunora/ratelimit": "1.0.0-alpha.38",
57
+ "@lunora/client": "1.0.0-alpha.79",
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{createSignal as w,onCleanup as J}from"solid-js";import{useLunora as G}from"./LunoraContext-dPcjtn6R.mjs";import{resolveMaybe as X}from"./createAgent-hhL-sw_X.mjs";const I="/_lunora/ws",j="/_lunora/voice/",z=4001,F=e=>e.startsWith("https://")?`wss://${e.slice(8)}`:e.startsWith("http://")?`ws://${e.slice(7)}`:e,Q=e=>{const r=e.startsWith("agents:")?e.slice(7):e;return r.endsWith("Voice")?r.slice(0,-5):r},Y=(e,r)=>{if(r===void 0||r==="")return F(e);if(r.endsWith(I))return r.slice(0,-I.length);try{return new URL(r).origin}catch{return F(e)}},Z=e=>{const r=Y(e.httpUrl,e.wsUrl),s=r.endsWith("/")?r.slice(0,-1):r,i=new URLSearchParams({threadKey:e.threadKey});return`${s}${j}${encodeURIComponent(e.agent)}?${i.toString()}`},ee=(e,r)=>{if(r?.code===z)return new Error(`${e}: authentication token expired — refresh the credential and start a new call`)},te=16e3,ne=e=>{if(e.length===0)return 0;let r=0;for(const s of e)r+=s*s;return Math.sqrt(r/e.length)},re=(e,r)=>{const s=r/te,i=s>1?Math.floor(e.length/s):e.length,h=new ArrayBuffer(i*2),l=new DataView(h);for(let c=0;c<i;c+=1){const g=e[Math.floor(c*s)]??0,f=Math.max(-1,Math.min(1,g));l.setInt16(c*2,f<0?f*32768:f*32767,!0)}return new Uint8Array(h)},oe=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("createVoiceAgent: microphone capture requires getUserMedia + AudioContext (no browser audio available)");const h=await s({audio:{channelCount:1,echoCancellation:!0,noiseSuppression:!0}}),l=new i,c=l.createMediaStreamSource(h),g=l.createScriptProcessor(4096,1,1);let f=!1,S=!1,u=0,d=0;return g.onaudioprocess=o=>{const p=o.inputBuffer.getChannelData(0),m=f?0:ne(p);if(e.onLevel(m),f)return;if(e.onAudio(re(p,l.sampleRate)),e.isSpeaking()){d=m>=e.interruptThreshold?d+1:0,d>=e.interruptChunks&&(d=0,e.onInterrupt());return}d=0;const v=p.length/l.sampleRate*1e3;if(m>=e.silenceThreshold){S=!0,u=0;return}S&&(u+=v,u>=e.silenceDurationMs&&(S=!1,u=0,e.onSilence()))},c.connect(g),g.connect(l.destination),{setMuted:o=>{f=o},stop:()=>{g.disconnect(),c.disconnect();for(const o of h.getTracks())o.stop();l.close()}}},se=()=>{const e=globalThis,r=e.AudioContext??e.webkitAudioContext;if(!r)throw new Error("createVoiceAgent: audio playback requires AudioContext (no browser audio available)");const s=new r,i=new Set;let h=0,l=Promise.resolve(),c=0;const g=async(u,d)=>{if(d!==c)return;let o;try{o=await s.decodeAudioData(u.buffer)}catch{return}if(d!==c)return;const p=s.createBufferSource();p.buffer=o,p.connect(s.destination);const m=Math.max(s.currentTime,h);p.start(m),h=m+o.duration,i.add(p),p.onended=()=>{i.delete(p)}},f=u=>{const d=Uint8Array.from(u),o=c;l=l.then(()=>g(d,o))},S=()=>{c+=1;for(const u of i)try{u.stop()}catch{}i.clear(),h=s.currentTime};return{enqueue:f,interrupt:S,stop:()=>{S(),s.close()}}},W=1,ae=.01,ie=1200,ce=.15,ue=3,he=e=>{const{createMicrophone:r=oe,createSpeaker:s=se,createSocket:i,interruptChunks:h=ue,interruptThreshold:l=ce,silenceDurationMs:c=ie,silenceThreshold:g=ae,threadKey:f,voice:S}=e,u=G(),[d,o]=w("idle"),[p,m]=w(!1),[v,M]=w(""),[N,C]=w(""),[O,x]=w(0),[U,R]=w(!1),[P,y]=w(void 0);let a,T=!1;const _=t=>{const n=a?.socket;return n?.readyState===W?(n.send(JSON.stringify(t)),!0):!1},b=()=>{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{}}T=!1,m(!1),o("idle"),x(0)},V=b,H=t=>{const n=a;switch(t.type){case"assistant_delta":{n&&(n.speaking=!0),o("speaking"),C(L=>L+t.text);break}case"assistant_done":{n&&(n.speaking=!1),C(t.text),o("listening");break}case"error":{n&&(n.speaking=!1),y(new Error(t.message)),o("listening");break}case"interrupted":{n&&(n.speaking=!1,n.suppressAudio=!1),n?.speaker?.interrupt(),o("listening");break}case"ready":{n&&(n.audioFormat=t.audioFormat,n.suppressAudio=!1),m(!0),o("listening");break}case"user_transcript":{n&&(n.suppressAudio=!1),M(t.text),C(""),o("thinking");break}}},$=t=>{const n=a;!n||n.suppressAudio||(n.speaker??=s({audioFormat:n.audioFormat}),n.speaking=!0,o("speaking"),n.speaker.enqueue(t))},B=async()=>{if(a||T)return;T=!0,y(void 0),M(""),C("");let t;try{const n=Z({agent:Q(S.__lunoraRef),httpUrl:u.url,threadKey:X(f),wsUrl:u.wsUrl}),A=(i??(k=>{const E=u.getWebSocketImpl();if(!E)throw new Error("createVoiceAgent: no WebSocket implementation available (pass createSocket explicitly)");return new E(k)}))(n);A.binaryType="arraybuffer",t={audioFormat:"mp3",microphone:void 0,socket:A,speaker:void 0,speaking:!1,suppressAudio:!1},a=t,A.onmessage=k=>{if(typeof k.data=="string"){try{H(JSON.parse(k.data))}catch{}return}$(new Uint8Array(k.data))},A.onerror=()=>{y(new Error("createVoiceAgent: voice socket error"))},A.onclose=k=>{if(a!==t)return;const E=ee("createVoiceAgent",k);E&&y(E),b()};const D=await r({interruptChunks:h,interruptThreshold:l,isSpeaking:()=>a?.speaking??!1,onAudio:k=>{A.readyState===W&&A.send(k)},onInterrupt:()=>{_({type:"interrupt"}),a?.speaker?.interrupt(),a&&(a.speaking=!1,a.suppressAudio=!0),o("listening")},onLevel:k=>{x(k)},onSilence:()=>{_({type:"commit"}),o("thinking")},silenceDurationMs:c,silenceThreshold:g});a===t?(t.microphone=D,R(!1),t.speaking||o("listening")):D.stop()}catch(n){a===t&&(y(n instanceof Error?n:new Error(String(n))),b())}finally{T=!1}},K=()=>{const t=!U();return a?.microphone?.setMuted(t),R(t),t},q=t=>{_({text:t,type:"text"})&&o("thinking")};return J(b),{audioLevel:O,connected:p,endCall:V,error:P,interimTranscript:N,isMuted:U,sendText:q,startCall:B,status:d,toggleMute:K,transcript:v}};export{he as createVoiceAgent};