@pickaxeproject/react 6.14.0 → 6.14.1

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 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../_virtual/_tslib.js"),n=require("../../common/api/pickaxe.js"),r=require("react");function t(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var t=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(n,r,t.get?t:{enumerable:!0,get:function(){return e[r]}})}})),n.default=e,Object.freeze(n)}const o={ar:"ar-SA",ca:"ca-ES",cs:"cs-CZ",da:"da-DK",de:"de-DE",en:"en-US",es:"es-ES",fi:"fi-FI",fr:"fr-FR",he:"he-IL",hi:"hi-IN",it:"it-IT",ja:"ja-JP",ko:"ko-KR",nb:"nb-NO",nl:"nl-NL",pl:"pl-PL",pt:"pt-PT",ro:"ro-RO",ru:"ru-RU",sv:"sv-SE",th:"th-TH",tr:"tr-TR",vi:"vi-VN",zh:"zh-CN",zu:"zu-ZA"};let i=null;const s=new Map,l=e=>e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&apos;"),a=({text:r,language:a,provider:c,pickaxeId:u,preferredVoiceName:d,browserVoices:p=[],onLoading:h,onPlay:f,onEnd:v})=>{var w;null==i||i.cancel(),null===(w=window.speechSynthesis)||void 0===w||w.cancel();let y=null,g=null,S=null,b=null,m=null,k=!1,L=!1,E=!1;const R=()=>{L||(L=!0,null==y||y.close(),y=null,null==m||m.abort(),m=null,null==S||S.pause(),S=null,b&&URL.revokeObjectURL(b),b=null,i===O&&(i=null),v())},A=()=>{k||L||f()},O={cancel:()=>{var e;k||L||(k=!0,null==g||g.close(),g=null,null==y||y.pause(),null==y||y.close(),y=null,null==m||m.abort(),m=null,null==S||S.pause(),S=null,b&&URL.revokeObjectURL(b),b=null,null===(e=window.speechSynthesis)||void 0===e||e.cancel(),R())}};i=O,h();const x=()=>{if(k||L||E)return;if(E=!0,!("speechSynthesis"in window)||!("SpeechSynthesisUtterance"in window))return void R();const e=new SpeechSynthesisUtterance(r),n=a||"en",t=p.find((e=>e.lang.startsWith(n)&&e.name.includes("Google")))||p.find((e=>e.lang.startsWith(n)));t?(e.voice=t,e.lang=t.lang):e.lang=n,e.onstart=A,e.onend=R,e.onerror=R,window.speechSynthesis.speak(e)},j=()=>e.__awaiter(void 0,void 0,void 0,(function*(){var e;try{const[{data:i},c]=yield Promise.all([n.pickaxeApi2.post("/speech"),Promise.resolve().then((function(){return t(require("microsoft-cognitiveservices-speech-sdk"))}))]);if(!i.success||!i.token)throw new Error(i.error||"Azure Speech token was not returned");if(k||L)return;const u=(e=>{if(!e)return"en-US";const n=e.toLowerCase();return"pt-br"===n?"pt-BR":o[n.split("-")[0]]||e})(a),p=c.SpeechConfig.fromAuthorizationToken(i.token,"eastus");p.speechSynthesisLanguage=u,p.speechSynthesisOutputFormat=c.SpeechSynthesisOutputFormat.Audio24Khz48KBitRateMonoMp3;const h=new c.SpeakerAudioDestination;h.onAudioStart=()=>{const e=h.internalAudio;e?e.addEventListener("playing",A,{once:!0}):A()},h.onAudioEnd=R,y=h;const f=c.AudioConfig.fromSpeakerOutput(h),v=new c.SpeechSynthesizer(p,f);g=v;let w=d||s.get(u);if(!w){const n=yield v.getVoicesAsync(u);if(k||L)return;if(w=null===(e=n.voices.find((e=>e.locale===u)))||void 0===e?void 0:e.shortName,!w)throw new Error(`No Azure voice is available for ${u}`);s.set(u,w)}const S=w,b=yield new Promise(((e,n)=>{const t=`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="${u}"><voice name="${l(S)}"><break time="500ms"/>${l(r)}</voice></speak>`;v.speakSsmlAsync(t,e,n)}));if(v.close(),g=null,k||L)return;if(b.reason!==c.ResultReason.SynthesizingAudioCompleted)throw new Error(b.errorDetails||"Azure speech synthesis failed")}catch(e){if(null==g||g.close(),g=null,null==y||y.close(),y=null,k||L)return;console.warn("Azure text-to-speech failed; using browser speech",e),x()}}));return"elevenlabs"===c?e.__awaiter(void 0,void 0,void 0,(function*(){try{if(!u)throw new Error("A Pickaxe ID is required");m=new AbortController;const e=yield fetch(`${n.pickaxeApi2.defaults.baseURL}/speech/elevenlabs/stream`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({text:r,pickaxeId:u}),signal:m.signal});if(!e.ok)throw new Error("ElevenLabs speech synthesis failed");if(k||L)return;if(S=new Audio,S.onplaying=A,S.onended=R,S.onerror=R,!("MediaSource"in window)||!MediaSource.isTypeSupported("audio/mpeg")||!e.body)return b=URL.createObjectURL(yield e.blob()),S.src=b,void(yield S.play());const t=new MediaSource;b=URL.createObjectURL(t),S.src=b;const o=yield new Promise(((e,n)=>{t.addEventListener("sourceopen",(()=>{try{e(t.addSourceBuffer("audio/mpeg"))}catch(e){n(e)}}),{once:!0})})),i=e.body.getReader();let s=!1;for(;!k&&!L;){const{done:e,value:n}=yield i.read();if(e)break;(null==n?void 0:n.byteLength)&&(yield new Promise(((e,r)=>{const t=()=>{o.removeEventListener("updateend",i),o.removeEventListener("error",s)},i=()=>{t(),e()},s=()=>{t(),r(new Error("Audio buffer failed"))};o.addEventListener("updateend",i),o.addEventListener("error",s),o.appendBuffer(n)})),s||(s=!0,yield S.play()))}k||"open"!==t.readyState||t.endOfStream()}catch(e){if(k||L)return;console.warn("ElevenLabs text-to-speech failed; using browser speech",e),null==m||m.abort(),m=null,null==S||S.pause(),S=null,b&&URL.revokeObjectURL(b),b=null,x()}})):j(),O};exports.useTextToSpeech=(e=!0,n={})=>{const[t,o]=r.useState(!1),[i,s]=r.useState(!1),[l,c]=r.useState([]),u=r.useRef(null),d=r.useRef(!0);r.useEffect((()=>(d.current=!0,()=>{var e;d.current=!1,null===(e=u.current)||void 0===e||e.cancel(),u.current=null})),[]),r.useEffect((()=>{if(!e||!("speechSynthesis"in window))return;const n=()=>{const e=window.speechSynthesis.getVoices();e.length>0&&c(e)};return n(),window.speechSynthesis.addEventListener("voiceschanged",n),()=>{window.speechSynthesis.removeEventListener("voiceschanged",n)}}),[e]);const p=r.useCallback((()=>{var e;null===(e=u.current)||void 0===e||e.cancel(),u.current=null}),[]);return{isLoading:t,isSpeaking:i,speak:r.useCallback(((e,r)=>{if(!e)return;const t=a({text:e,language:r,provider:n.provider,pickaxeId:n.pickaxeId,preferredVoiceName:n.voiceName,browserVoices:l,onLoading:()=>{d.current&&(o(!0),s(!1))},onPlay:()=>{d.current&&(o(!1),s(!0))},onEnd:()=>{u.current===t&&(u.current=null),d.current&&(o(!1),s(!1))}});u.current=t}),[l,n]),stop:p}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../_virtual/_tslib.js"),n=require("../../common/api/pickaxe.js"),r=require("react");function t(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var t=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(n,r,t.get?t:{enumerable:!0,get:function(){return e[r]}})}})),n.default=e,Object.freeze(n)}const o={ar:"ar-SA",ca:"ca-ES",cs:"cs-CZ",da:"da-DK",de:"de-DE",en:"en-US",es:"es-ES",fi:"fi-FI",fr:"fr-FR",he:"he-IL",hi:"hi-IN",it:"it-IT",ja:"ja-JP",ko:"ko-KR",nb:"nb-NO",nl:"nl-NL",pl:"pl-PL",pt:"pt-PT",ro:"ro-RO",ru:"ru-RU",sv:"sv-SE",th:"th-TH",tr:"tr-TR",vi:"vi-VN",zh:"zh-CN",zu:"zu-ZA"};let i=null;const s=new Map,l=e=>e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&apos;"),a=({text:r,language:a,provider:c,pickaxeId:u,preferredVoiceName:d,browserVoices:p=[],onLoading:h,onPlay:f,onEnd:v})=>{var w;null==i||i.cancel(),null===(w=window.speechSynthesis)||void 0===w||w.cancel();let y=null,g=null,S=null,b=null,m=null,k=!1,L=!1,E=!1;const A=()=>{L||(L=!0,null==y||y.close(),y=null,null==m||m.abort(),m=null,null==S||S.pause(),S=null,b&&URL.revokeObjectURL(b),b=null,i===O&&(i=null),v())},R=()=>{k||L||f()},O={cancel:()=>{var e;k||L||(k=!0,null==g||g.close(),g=null,null==y||y.pause(),null==y||y.close(),y=null,null==m||m.abort(),m=null,null==S||S.pause(),S=null,b&&URL.revokeObjectURL(b),b=null,null===(e=window.speechSynthesis)||void 0===e||e.cancel(),A())}};i=O,h();const x=(i=!0)=>e.__awaiter(void 0,void 0,void 0,(function*(){var e;try{const[{data:c},u]=yield Promise.all([n.pickaxeApi2.post("/speech"),Promise.resolve().then((function(){return t(require("microsoft-cognitiveservices-speech-sdk"))}))]);if(!c.success||!c.token)throw new Error(c.error||"Azure Speech token was not returned");if(k||L)return;const p=(e=>{if(!e)return"en-US";const n=e.toLowerCase();return"pt-br"===n?"pt-BR":o[n.split("-")[0]]||e})(a),h=u.SpeechConfig.fromAuthorizationToken(c.token,"eastus");h.speechSynthesisLanguage=p,h.speechSynthesisOutputFormat=u.SpeechSynthesisOutputFormat.Audio24Khz48KBitRateMonoMp3;const f=new u.SpeakerAudioDestination;f.onAudioStart=()=>{const e=f.internalAudio;e?e.addEventListener("playing",R,{once:!0}):R()},f.onAudioEnd=A,y=f;const v=u.AudioConfig.fromSpeakerOutput(f),w=new u.SpeechSynthesizer(h,v);g=w;let S=(i?d:void 0)||s.get(p);if(!S){const n=yield w.getVoicesAsync(p);if(k||L)return;if(S=null===(e=n.voices.find((e=>e.locale===p)))||void 0===e?void 0:e.shortName,!S)throw new Error(`No Azure voice is available for ${p}`);s.set(p,S)}const b=S,m=yield new Promise(((e,n)=>{const t=`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="${p}"><voice name="${l(b)}"><break time="500ms"/>${l(r)}</voice></speak>`;w.speakSsmlAsync(t,e,n)}));if(w.close(),g=null,k||L)return;if(m.reason!==u.ResultReason.SynthesizingAudioCompleted)throw new Error(m.errorDetails||"Azure speech synthesis failed")}catch(e){if(null==g||g.close(),g=null,null==y||y.close(),y=null,k||L)return;console.warn("Azure text-to-speech failed; using browser speech",e),(()=>{if(k||L||E)return;if(E=!0,!("speechSynthesis"in window)||!("SpeechSynthesisUtterance"in window))return void A();const e=new SpeechSynthesisUtterance(r),n=a||"en",t=p.find((e=>e.lang.startsWith(n)&&e.name.includes("Google")))||p.find((e=>e.lang.startsWith(n)));t?(e.voice=t,e.lang=t.lang):e.lang=n,e.onstart=R,e.onend=A,e.onerror=A,window.speechSynthesis.speak(e)})()}}));return"elevenlabs"===c?e.__awaiter(void 0,void 0,void 0,(function*(){try{if(!u)throw new Error("A Pickaxe ID is required");m=new AbortController;const e=yield fetch(`${n.pickaxeApi2.defaults.baseURL}/speech/elevenlabs/stream`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({text:r,pickaxeId:u}),signal:m.signal});if(!e.ok)throw new Error("ElevenLabs speech synthesis failed");if(k||L)return;if(S=new Audio,S.onplaying=R,S.onended=A,S.onerror=A,!("MediaSource"in window)||!MediaSource.isTypeSupported("audio/mpeg")||!e.body)return b=URL.createObjectURL(yield e.blob()),S.src=b,void(yield S.play());const t=new MediaSource;b=URL.createObjectURL(t),S.src=b;const o=yield new Promise(((e,n)=>{t.addEventListener("sourceopen",(()=>{try{e(t.addSourceBuffer("audio/mpeg"))}catch(e){n(e)}}),{once:!0})})),i=e.body.getReader();let s,l=!1,a=null;for(;!k&&!L;){const{done:e,value:n}=yield i.read();if(e)break;(null==n?void 0:n.byteLength)&&(yield new Promise(((e,r)=>{const t=()=>{o.removeEventListener("updateend",i),o.removeEventListener("error",s)},i=()=>{t(),e()},s=()=>{t(),r(new Error("Audio buffer failed"))};o.addEventListener("updateend",i),o.addEventListener("error",s),o.appendBuffer(n)})),l||(l=!0,a=S.play().catch((e=>{s=e}))))}if(k||"open"!==t.readyState||t.endOfStream(),!a)throw new Error("ElevenLabs returned no audio");if(yield a,s)throw s}catch(e){if(k||L)return;console.warn("ElevenLabs text-to-speech failed; using Azure speech",e),null==m||m.abort(),m=null,null==S||S.pause(),S=null,b&&URL.revokeObjectURL(b),b=null,x(!1)}})):x(),O};exports.useTextToSpeech=(e=!0,n={})=>{const[t,o]=r.useState(!1),[i,s]=r.useState(!1),[l,c]=r.useState([]),u=r.useRef(null),d=r.useRef(!0);r.useEffect((()=>(d.current=!0,()=>{var e;d.current=!1,null===(e=u.current)||void 0===e||e.cancel(),u.current=null})),[]),r.useEffect((()=>{if(!e||!("speechSynthesis"in window))return;const n=()=>{const e=window.speechSynthesis.getVoices();e.length>0&&c(e)};return n(),window.speechSynthesis.addEventListener("voiceschanged",n),()=>{window.speechSynthesis.removeEventListener("voiceschanged",n)}}),[e]);const p=r.useCallback((()=>{var e;null===(e=u.current)||void 0===e||e.cancel(),u.current=null}),[]);return{isLoading:t,isSpeaking:i,speak:r.useCallback(((e,r)=>{if(!e)return;const t=a({text:e,language:r,provider:n.provider,pickaxeId:n.pickaxeId,preferredVoiceName:n.voiceName,browserVoices:l,onLoading:()=>{d.current&&(o(!0),s(!1))},onPlay:()=>{d.current&&(o(!1),s(!0))},onEnd:()=>{u.current===t&&(u.current=null),d.current&&(o(!1),s(!1))}});u.current=t}),[l,n]),stop:p}};
@@ -1 +1 @@
1
- import{__awaiter as e}from"../../_virtual/_tslib.js";import{pickaxeApi2 as n}from"../../common/api/pickaxe.js";import{useState as r,useRef as t,useEffect as o,useCallback as i}from"react";const s={ar:"ar-SA",ca:"ca-ES",cs:"cs-CZ",da:"da-DK",de:"de-DE",en:"en-US",es:"es-ES",fi:"fi-FI",fr:"fr-FR",he:"he-IL",hi:"hi-IN",it:"it-IT",ja:"ja-JP",ko:"ko-KR",nb:"nb-NO",nl:"nl-NL",pl:"pl-PL",pt:"pt-PT",ro:"ro-RO",ru:"ru-RU",sv:"sv-SE",th:"th-TH",tr:"tr-TR",vi:"vi-VN",zh:"zh-CN",zu:"zu-ZA"};let l=null;const a=new Map,c=e=>e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&apos;"),u=({text:r,language:t,provider:o,pickaxeId:i,preferredVoiceName:u,browserVoices:d=[],onLoading:p,onPlay:h,onEnd:v})=>{var f;null==l||l.cancel(),null===(f=window.speechSynthesis)||void 0===f||f.cancel();let w=null,y=null,g=null,S=null,m=null,b=!1,L=!1,k=!1;const E=()=>{L||(L=!0,null==w||w.close(),w=null,null==m||m.abort(),m=null,null==g||g.pause(),g=null,S&&URL.revokeObjectURL(S),S=null,l===A&&(l=null),v())},R=()=>{b||L||h()},A={cancel:()=>{var e;b||L||(b=!0,null==y||y.close(),y=null,null==w||w.pause(),null==w||w.close(),w=null,null==m||m.abort(),m=null,null==g||g.pause(),g=null,S&&URL.revokeObjectURL(S),S=null,null===(e=window.speechSynthesis)||void 0===e||e.cancel(),E())}};l=A,p();const U=()=>{if(b||L||k)return;if(k=!0,!("speechSynthesis"in window)||!("SpeechSynthesisUtterance"in window))return void E();const e=new SpeechSynthesisUtterance(r),n=t||"en",o=d.find((e=>e.lang.startsWith(n)&&e.name.includes("Google")))||d.find((e=>e.lang.startsWith(n)));o?(e.voice=o,e.lang=o.lang):e.lang=n,e.onstart=R,e.onend=E,e.onerror=E,window.speechSynthesis.speak(e)},x=()=>e(void 0,void 0,void 0,(function*(){var e;try{const[{data:o},i]=yield Promise.all([n.post("/speech"),import("microsoft-cognitiveservices-speech-sdk")]);if(!o.success||!o.token)throw new Error(o.error||"Azure Speech token was not returned");if(b||L)return;const l=(e=>{if(!e)return"en-US";const n=e.toLowerCase();return"pt-br"===n?"pt-BR":s[n.split("-")[0]]||e})(t),d=i.SpeechConfig.fromAuthorizationToken(o.token,"eastus");d.speechSynthesisLanguage=l,d.speechSynthesisOutputFormat=i.SpeechSynthesisOutputFormat.Audio24Khz48KBitRateMonoMp3;const p=new i.SpeakerAudioDestination;p.onAudioStart=()=>{const e=p.internalAudio;e?e.addEventListener("playing",R,{once:!0}):R()},p.onAudioEnd=E,w=p;const h=i.AudioConfig.fromSpeakerOutput(p),v=new i.SpeechSynthesizer(d,h);y=v;let f=u||a.get(l);if(!f){const n=yield v.getVoicesAsync(l);if(b||L)return;if(f=null===(e=n.voices.find((e=>e.locale===l)))||void 0===e?void 0:e.shortName,!f)throw new Error(`No Azure voice is available for ${l}`);a.set(l,f)}const g=f,S=yield new Promise(((e,n)=>{const t=`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="${l}"><voice name="${c(g)}"><break time="500ms"/>${c(r)}</voice></speak>`;v.speakSsmlAsync(t,e,n)}));if(v.close(),y=null,b||L)return;if(S.reason!==i.ResultReason.SynthesizingAudioCompleted)throw new Error(S.errorDetails||"Azure speech synthesis failed")}catch(e){if(null==y||y.close(),y=null,null==w||w.close(),w=null,b||L)return;console.warn("Azure text-to-speech failed; using browser speech",e),U()}}));return"elevenlabs"===o?e(void 0,void 0,void 0,(function*(){try{if(!i)throw new Error("A Pickaxe ID is required");m=new AbortController;const e=yield fetch(`${n.defaults.baseURL}/speech/elevenlabs/stream`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({text:r,pickaxeId:i}),signal:m.signal});if(!e.ok)throw new Error("ElevenLabs speech synthesis failed");if(b||L)return;if(g=new Audio,g.onplaying=R,g.onended=E,g.onerror=E,!("MediaSource"in window)||!MediaSource.isTypeSupported("audio/mpeg")||!e.body)return S=URL.createObjectURL(yield e.blob()),g.src=S,void(yield g.play());const t=new MediaSource;S=URL.createObjectURL(t),g.src=S;const o=yield new Promise(((e,n)=>{t.addEventListener("sourceopen",(()=>{try{e(t.addSourceBuffer("audio/mpeg"))}catch(e){n(e)}}),{once:!0})})),s=e.body.getReader();let l=!1;for(;!b&&!L;){const{done:e,value:n}=yield s.read();if(e)break;(null==n?void 0:n.byteLength)&&(yield new Promise(((e,r)=>{const t=()=>{o.removeEventListener("updateend",i),o.removeEventListener("error",s)},i=()=>{t(),e()},s=()=>{t(),r(new Error("Audio buffer failed"))};o.addEventListener("updateend",i),o.addEventListener("error",s),o.appendBuffer(n)})),l||(l=!0,yield g.play()))}b||"open"!==t.readyState||t.endOfStream()}catch(e){if(b||L)return;console.warn("ElevenLabs text-to-speech failed; using browser speech",e),null==m||m.abort(),m=null,null==g||g.pause(),g=null,S&&URL.revokeObjectURL(S),S=null,U()}})):x(),A},d=(e=!0,n={})=>{const[s,l]=r(!1),[a,c]=r(!1),[d,p]=r([]),h=t(null),v=t(!0);o((()=>(v.current=!0,()=>{var e;v.current=!1,null===(e=h.current)||void 0===e||e.cancel(),h.current=null})),[]),o((()=>{if(!e||!("speechSynthesis"in window))return;const n=()=>{const e=window.speechSynthesis.getVoices();e.length>0&&p(e)};return n(),window.speechSynthesis.addEventListener("voiceschanged",n),()=>{window.speechSynthesis.removeEventListener("voiceschanged",n)}}),[e]);const f=i((()=>{var e;null===(e=h.current)||void 0===e||e.cancel(),h.current=null}),[]);return{isLoading:s,isSpeaking:a,speak:i(((e,r)=>{if(!e)return;const t=u({text:e,language:r,provider:n.provider,pickaxeId:n.pickaxeId,preferredVoiceName:n.voiceName,browserVoices:d,onLoading:()=>{v.current&&(l(!0),c(!1))},onPlay:()=>{v.current&&(l(!1),c(!0))},onEnd:()=>{h.current===t&&(h.current=null),v.current&&(l(!1),c(!1))}});h.current=t}),[d,n]),stop:f}};export{d as useTextToSpeech};
1
+ import{__awaiter as e}from"../../_virtual/_tslib.js";import{pickaxeApi2 as n}from"../../common/api/pickaxe.js";import{useState as r,useRef as t,useEffect as o,useCallback as i}from"react";const s={ar:"ar-SA",ca:"ca-ES",cs:"cs-CZ",da:"da-DK",de:"de-DE",en:"en-US",es:"es-ES",fi:"fi-FI",fr:"fr-FR",he:"he-IL",hi:"hi-IN",it:"it-IT",ja:"ja-JP",ko:"ko-KR",nb:"nb-NO",nl:"nl-NL",pl:"pl-PL",pt:"pt-PT",ro:"ro-RO",ru:"ru-RU",sv:"sv-SE",th:"th-TH",tr:"tr-TR",vi:"vi-VN",zh:"zh-CN",zu:"zu-ZA"};let l=null;const a=new Map,c=e=>e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&apos;"),u=({text:r,language:t,provider:o,pickaxeId:i,preferredVoiceName:u,browserVoices:d=[],onLoading:p,onPlay:h,onEnd:v})=>{var f;null==l||l.cancel(),null===(f=window.speechSynthesis)||void 0===f||f.cancel();let w=null,y=null,g=null,S=null,m=null,b=!1,L=!1,k=!1;const E=()=>{L||(L=!0,null==w||w.close(),w=null,null==m||m.abort(),m=null,null==g||g.pause(),g=null,S&&URL.revokeObjectURL(S),S=null,l===R&&(l=null),v())},A=()=>{b||L||h()},R={cancel:()=>{var e;b||L||(b=!0,null==y||y.close(),y=null,null==w||w.pause(),null==w||w.close(),w=null,null==m||m.abort(),m=null,null==g||g.pause(),g=null,S&&URL.revokeObjectURL(S),S=null,null===(e=window.speechSynthesis)||void 0===e||e.cancel(),E())}};l=R,p();const U=(o=!0)=>e(void 0,void 0,void 0,(function*(){var e;try{const[{data:i},l]=yield Promise.all([n.post("/speech"),import("microsoft-cognitiveservices-speech-sdk")]);if(!i.success||!i.token)throw new Error(i.error||"Azure Speech token was not returned");if(b||L)return;const d=(e=>{if(!e)return"en-US";const n=e.toLowerCase();return"pt-br"===n?"pt-BR":s[n.split("-")[0]]||e})(t),p=l.SpeechConfig.fromAuthorizationToken(i.token,"eastus");p.speechSynthesisLanguage=d,p.speechSynthesisOutputFormat=l.SpeechSynthesisOutputFormat.Audio24Khz48KBitRateMonoMp3;const h=new l.SpeakerAudioDestination;h.onAudioStart=()=>{const e=h.internalAudio;e?e.addEventListener("playing",A,{once:!0}):A()},h.onAudioEnd=E,w=h;const v=l.AudioConfig.fromSpeakerOutput(h),f=new l.SpeechSynthesizer(p,v);y=f;let g=(o?u:void 0)||a.get(d);if(!g){const n=yield f.getVoicesAsync(d);if(b||L)return;if(g=null===(e=n.voices.find((e=>e.locale===d)))||void 0===e?void 0:e.shortName,!g)throw new Error(`No Azure voice is available for ${d}`);a.set(d,g)}const S=g,m=yield new Promise(((e,n)=>{const t=`<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="${d}"><voice name="${c(S)}"><break time="500ms"/>${c(r)}</voice></speak>`;f.speakSsmlAsync(t,e,n)}));if(f.close(),y=null,b||L)return;if(m.reason!==l.ResultReason.SynthesizingAudioCompleted)throw new Error(m.errorDetails||"Azure speech synthesis failed")}catch(e){if(null==y||y.close(),y=null,null==w||w.close(),w=null,b||L)return;console.warn("Azure text-to-speech failed; using browser speech",e),(()=>{if(b||L||k)return;if(k=!0,!("speechSynthesis"in window)||!("SpeechSynthesisUtterance"in window))return void E();const e=new SpeechSynthesisUtterance(r),n=t||"en",o=d.find((e=>e.lang.startsWith(n)&&e.name.includes("Google")))||d.find((e=>e.lang.startsWith(n)));o?(e.voice=o,e.lang=o.lang):e.lang=n,e.onstart=A,e.onend=E,e.onerror=E,window.speechSynthesis.speak(e)})()}}));return"elevenlabs"===o?e(void 0,void 0,void 0,(function*(){try{if(!i)throw new Error("A Pickaxe ID is required");m=new AbortController;const e=yield fetch(`${n.defaults.baseURL}/speech/elevenlabs/stream`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({text:r,pickaxeId:i}),signal:m.signal});if(!e.ok)throw new Error("ElevenLabs speech synthesis failed");if(b||L)return;if(g=new Audio,g.onplaying=A,g.onended=E,g.onerror=E,!("MediaSource"in window)||!MediaSource.isTypeSupported("audio/mpeg")||!e.body)return S=URL.createObjectURL(yield e.blob()),g.src=S,void(yield g.play());const t=new MediaSource;S=URL.createObjectURL(t),g.src=S;const o=yield new Promise(((e,n)=>{t.addEventListener("sourceopen",(()=>{try{e(t.addSourceBuffer("audio/mpeg"))}catch(e){n(e)}}),{once:!0})})),s=e.body.getReader();let l,a=!1,c=null;for(;!b&&!L;){const{done:e,value:n}=yield s.read();if(e)break;(null==n?void 0:n.byteLength)&&(yield new Promise(((e,r)=>{const t=()=>{o.removeEventListener("updateend",i),o.removeEventListener("error",s)},i=()=>{t(),e()},s=()=>{t(),r(new Error("Audio buffer failed"))};o.addEventListener("updateend",i),o.addEventListener("error",s),o.appendBuffer(n)})),a||(a=!0,c=g.play().catch((e=>{l=e}))))}if(b||"open"!==t.readyState||t.endOfStream(),!c)throw new Error("ElevenLabs returned no audio");if(yield c,l)throw l}catch(e){if(b||L)return;console.warn("ElevenLabs text-to-speech failed; using Azure speech",e),null==m||m.abort(),m=null,null==g||g.pause(),g=null,S&&URL.revokeObjectURL(S),S=null,U(!1)}})):U(),R},d=(e=!0,n={})=>{const[s,l]=r(!1),[a,c]=r(!1),[d,p]=r([]),h=t(null),v=t(!0);o((()=>(v.current=!0,()=>{var e;v.current=!1,null===(e=h.current)||void 0===e||e.cancel(),h.current=null})),[]),o((()=>{if(!e||!("speechSynthesis"in window))return;const n=()=>{const e=window.speechSynthesis.getVoices();e.length>0&&p(e)};return n(),window.speechSynthesis.addEventListener("voiceschanged",n),()=>{window.speechSynthesis.removeEventListener("voiceschanged",n)}}),[e]);const f=i((()=>{var e;null===(e=h.current)||void 0===e||e.cancel(),h.current=null}),[]);return{isLoading:s,isSpeaking:a,speak:i(((e,r)=>{if(!e)return;const t=u({text:e,language:r,provider:n.provider,pickaxeId:n.pickaxeId,preferredVoiceName:n.voiceName,browserVoices:d,onLoading:()=>{v.current&&(l(!0),c(!1))},onPlay:()=>{v.current&&(l(!1),c(!0))},onEnd:()=>{h.current===t&&(h.current=null),v.current&&(l(!1),c(!1))}});h.current=t}),[d,n]),stop:f}};export{d as useTextToSpeech};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pickaxeproject/react",
3
- "version": "6.14.0",
3
+ "version": "6.14.1",
4
4
  "description": "Client utility library for Pickaxe",
5
5
  "repository": {
6
6
  "type": "git",