@supernal/tts-widget 1.0.3 → 1.0.5

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
- {"version":3,"file":"widget.d.ts","sourceRoot":"","sources":["../src/widget.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,cAAc,CAAC,EAAE,UAAU,GAAG,aAAa,GAAG,UAAU,CAAC;IACzD,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,OAAO,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAA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eAAe;IASvB,OAAO,CAAC,gBAAgB;IAaxB,OAAO,CAAC,gBAAgB;IAkCxB,OAAO,CAAC,mBAAmB;IA+B3B;;;;OAIG;IACH,OAAO,CAAC,qBAAqB;YA0If,eAAe;YAyEf,aAAa;IA8D3B;;;OAGG;YACW,wBAAwB;YA4HxB,SAAS;IA2GvB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAqBzB,OAAO,CAAC,UAAU;IAUlB,OAAO,CAAC,WAAW;IAkBnB,OAAO,CAAC,SAAS;IAqBjB;;;OAGG;IACH,OAAO,CAAC,cAAc;IAoCtB;;OAEG;IACH,OAAO,CAAC,0BAA0B;IAyBlC;;OAEG;YACW,2BAA2B;IA4CzC;;OAEG;YACW,qBAAqB;IAsHnC,OAAO,CAAC,cAAc;IA2CtB,OAAO,CAAC,SAAS;IAqBjB,OAAO,CAAC,YAAY;IAapB,OAAO,CAAC,iBAAiB;IAiBzB,OAAO,CAAC,aAAa;IAWrB,OAAO,CAAC,oBAAoB;IAY5B,OAAO,CAAC,kBAAkB;WAUZ,IAAI,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,WAAW;WAU/C,WAAW,IAAI,WAAW,GAAG,SAAS;IAI7C,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,GAAQ,GAAG,IAAI;CAsBvF;AA6CD,eAAe,WAAW,CAAC"}
package/dist/widget.js CHANGED
@@ -1,4 +1,4 @@
1
- var L=class w{constructor(e={}){this.currentAudio=null;this.currentButton=null;this.isProcessing=!1;this.lastClickTime=0;this.debounceDelay=300;this.progressUpdateInterval=null;this.audioQueue=[];this.apiUrl=e.apiUrl||"https://www.tts.supernal.ai",this.defaultVoice=e.voice||"default",this.defaultProvider=e.provider||"openai",this.defaultSpeed=e.speed||1,this.autoHash=e.autoHash!==!1,this.cacheExpiry=e.cacheExpiry||7*24*60*60*1e3,this.apiKey=e.apiKey||this.detectApiKey(),this.showBranding=e.showBranding!==!1,this.version=e.version||"1.0.0",this.devMode=e.devMode===!0,this.clientSideSpeed=e.clientSideSpeed!==!1,this.generationMode=e.generationMode||"standard",this.progressiveThreshold=e.progressiveThreshold||2e3,this.cache=new Map,this.loadCacheFromStorage(),this.init()}detectApiKey(){if(typeof window>"u")return;let e=["NEXT_PUBLIC_TTS_API_KEY","VITE_TTS_API_KEY","PUBLIC_TTS_API_KEY","REACT_APP_TTS_API_KEY"];for(let t of e)try{let s=globalThis.process?.env;if(s?.[t])return console.log(`[TTS Widget] Auto-detected API key from process.env.${t}`),s[t];let r=globalThis.import?.meta?.env;if(r?.[t])return console.log(`[TTS Widget] Auto-detected API key from import.meta.env.${t}`),r[t]}catch{continue}}init(){document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>this.initializeWidgets()):this.initializeWidgets(),this.observeDOM()}observeDOM(){new MutationObserver(t=>{t.forEach(s=>{s.addedNodes.forEach(r=>{if(r.nodeType===Node.ELEMENT_NODE){let o=r;o.classList?.contains("supernal-tts-widget")?this.setupWidget(o):o.querySelectorAll?.(".supernal-tts-widget")?.forEach(i=>this.setupWidget(i))}})})}).observe(document.body,{childList:!0,subtree:!0})}initializeWidgets(){let e=document.querySelectorAll(".supernal-tts-widget");e.forEach(t=>this.setupWidget(t)),this.devMode&&e.length>0&&this.addDevModeClearButton()}setupWidget(e){if(e.classList.contains("supernal-tts-widget-initialized"))return;let t=this.parseWidgetConfig(e);if(!t.text){console.warn("TTS Widget: No text found for widget",e);return}let s=this.generateHash(t.text,{voice:t.voice,provider:t.provider,speed:t.speed}),r=e.dataset.controls;if(r==="advanced"||r==="true"){this.setupAdvancedWidget(e,t.text,{voice:t.voice,provider:t.provider,speed:t.speed,hash:s,apiKey:t.apiKey});return}else if(r==="compact"){this.setupCompactWidget(e,t.text,{voice:t.voice,provider:t.provider,speed:t.speed,hash:s,apiKey:t.apiKey});return}this.setupModularWidget(e,t,s)}scrubText(e){if(!e)return"";let t=e;return t=t.replace(/\[([^\]]+)\]\(([^)]+)\)/g,"$1"),t=t.replace(/https?:\/\/[^\s]+/g,"link"),t=t.replace(/www\.[^\s]+/g,"link"),t}parseWidgetConfig(e){let t=e.dataset.text||e.textContent?.trim()||"",s=this.scrubText(t),r=e.dataset.voice||this.defaultVoice,o=e.dataset.provider||this.defaultProvider,n=e.dataset.speed?parseFloat(e.dataset.speed):this.defaultSpeed,i=e.dataset.voices||"",a=i?i.split(",").map(v=>v.trim()).filter(Boolean):[],l=e.dataset.enableSpeed==="true",d=e.dataset.enableProgress==="true",h=e.dataset.apiKey||this.apiKey;return{text:s,voices:a,enableSpeed:l,enableProgress:d,speed:n,provider:o,voice:r,apiKey:h}}setupBasicWidget(e,t,s){let{voice:r,provider:o,speed:n,hash:i,apiKey:a}=s;if(e.classList.contains("supernal-tts-widget-initialized"))return;let l=document.createElement("div");l.className="supernal-tts-button-container";let d=e.querySelector(".supernal-tts-play");if(d||(d=this.createPlayButton()),l.appendChild(d),this.showBranding&&!e.querySelector(".supernal-badge")){let h=this.createBrandingBadge();l.appendChild(h)}e.appendChild(l),e.classList.add("supernal-tts-widget-initialized"),d.addEventListener("click",h=>{h.preventDefault(),this.handlePlayClick(d,t,{voice:r,provider:o,speed:n,hash:i,apiKey:a})}),e._ttsWidget={text:t,voice:r,provider:o,speed:n,hash:i,playButton:d}}setupCompactWidget(e,t,s){let{hash:r,apiKey:o}=s,{voice:n,provider:i,speed:a}=s;if(e.classList.contains("supernal-tts-widget-initialized"))return;let l=document.createElement("div");l.className="supernal-tts-compact-widget";let d=document.createElement("div");d.className="supernal-tts-top-row";let h=this.createPlayButton(),v=document.createElement("select");if(v.className="supernal-tts-voice-select",v.innerHTML=`
1
+ var w=class L{constructor(e={}){this.currentAudio=null;this.currentButton=null;this.isProcessing=!1;this.lastClickTime=0;this.debounceDelay=300;this.progressUpdateInterval=null;this.audioQueue=[];this.apiUrl=e.apiUrl||"https://www.tts.supernal.ai",this.defaultVoice=e.voice||"default",this.defaultProvider=e.provider||"openai",this.defaultSpeed=e.speed||1,this.autoHash=e.autoHash!==!1,this.cacheExpiry=e.cacheExpiry||7*24*60*60*1e3,this.apiKey=e.apiKey||this.detectApiKey(),this.showBranding=e.showBranding!==!1,this.version=e.version||"1.0.0",this.devMode=e.devMode===!0,this.clientSideSpeed=e.clientSideSpeed!==!1,this.generationMode=e.generationMode||"standard",this.progressiveThreshold=e.progressiveThreshold||2e3,this.cache=new Map,this.loadCacheFromStorage(),this.init()}detectApiKey(){if(typeof window>"u")return;let e=["NEXT_PUBLIC_TTS_API_KEY","VITE_TTS_API_KEY","PUBLIC_TTS_API_KEY","REACT_APP_TTS_API_KEY"];for(let t of e)try{let s=globalThis.process?.env;if(s?.[t])return console.log(`[TTS Widget] Auto-detected API key from process.env.${t}`),s[t];let r=globalThis.import?.meta?.env;if(r?.[t])return console.log(`[TTS Widget] Auto-detected API key from import.meta.env.${t}`),r[t]}catch{continue}}init(){document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>this.initializeWidgets()):this.initializeWidgets(),this.observeDOM()}observeDOM(){new MutationObserver(t=>{t.forEach(s=>{s.addedNodes.forEach(r=>{if(r.nodeType===Node.ELEMENT_NODE){let o=r;o.classList?.contains("supernal-tts-widget")?this.setupWidget(o):o.querySelectorAll?.(".supernal-tts-widget")?.forEach(i=>this.setupWidget(i))}})})}).observe(document.body,{childList:!0,subtree:!0})}initializeWidgets(){let e=document.querySelectorAll(".supernal-tts-widget");e.forEach(t=>this.setupWidget(t)),this.devMode&&e.length>0&&this.addDevModeClearButton()}setupWidget(e){if(e.classList.contains("supernal-tts-widget-initialized"))return;let t=this.parseWidgetConfig(e);if(!t.text){console.warn("TTS Widget: No text found for widget",e);return}let s=this.generateHash(t.text,{voice:t.voice,provider:t.provider,speed:t.speed}),r=e.dataset.controls;if(r==="advanced"||r==="true"){this.setupAdvancedWidget(e,t.text,{voice:t.voice,provider:t.provider,speed:t.speed,hash:s,apiKey:t.apiKey});return}else if(r==="compact"){this.setupCompactWidget(e,t.text,{voice:t.voice,provider:t.provider,speed:t.speed,hash:s,apiKey:t.apiKey});return}this.setupModularWidget(e,t,s)}scrubText(e){if(!e)return"";let t=e;return t=t.replace(/\[([^\]]+)\]\(([^)]+)\)/g,"$1"),t=t.replace(/https?:\/\/[^\s]+/g,"link"),t=t.replace(/www\.[^\s]+/g,"link"),t}parseWidgetConfig(e){let t=e.dataset.text||e.textContent?.trim()||"",s=this.scrubText(t),r=e.dataset.voice||this.defaultVoice,o=e.dataset.provider||this.defaultProvider,n=e.dataset.speed?parseFloat(e.dataset.speed):this.defaultSpeed,i=e.dataset.voices||"",a=i?i.split(",").map(v=>v.trim()).filter(Boolean):[],l=e.dataset.enableSpeed==="true",d=e.dataset.enableProgress==="true",h=e.dataset.apiKey||this.apiKey;return{text:s,voices:a,enableSpeed:l,enableProgress:d,speed:n,provider:o,voice:r,apiKey:h}}setupBasicWidget(e,t,s){let{voice:r,provider:o,speed:n,hash:i,apiKey:a}=s;if(e.classList.contains("supernal-tts-widget-initialized"))return;let l=document.createElement("div");l.className="supernal-tts-button-container";let d=e.querySelector(".supernal-tts-play");if(d||(d=this.createPlayButton()),l.appendChild(d),this.showBranding&&!e.querySelector(".supernal-badge")){let h=this.createBrandingBadge();l.appendChild(h)}e.appendChild(l),e.classList.add("supernal-tts-widget-initialized"),d.addEventListener("click",h=>{h.preventDefault(),this.handlePlayClick(d,t,{voice:r,provider:o,speed:n,hash:i,apiKey:a})}),e._ttsWidget={text:t,voice:r,provider:o,speed:n,hash:i,playButton:d}}setupCompactWidget(e,t,s){let{hash:r,apiKey:o}=s,{voice:n,provider:i,speed:a}=s;if(e.classList.contains("supernal-tts-widget-initialized"))return;let l=document.createElement("div");l.className="supernal-tts-compact-widget";let d=document.createElement("div");d.className="supernal-tts-top-row";let h=this.createPlayButton(),v=document.createElement("select");if(v.className="supernal-tts-voice-select",v.innerHTML=`
2
2
  <option value="alloy" ${n==="alloy"?"selected":""}>Alloy</option>
3
3
  <option value="echo" ${n==="echo"?"selected":""}>Echo</option>
4
4
  <option value="fable" ${n==="fable"?"selected":""}>Fable</option>
@@ -92,5 +92,5 @@ var L=class w{constructor(e={}){this.currentAudio=null;this.currentButton=null;t
92
92
  <span style="opacity: 0.7; font-size: 10px;">\xD7</span>
93
93
  `;let r=i=>{i.addEventListener("mouseenter",()=>{i.style.transform="scale(1.05)",i.style.boxShadow="0 4px 12px rgba(0,0,0,0.4)"}),i.addEventListener("mouseleave",()=>{i.style.transform="scale(1)",i.style.boxShadow="0 2px 8px rgba(0,0,0,0.3)"})};r(s),r(t);let o=()=>{let i=this.cache.size;this.cache.clear(),localStorage.removeItem("supernal-tts-cache"),s.innerHTML=`<span>\u2713</span><span>Cleared ${i}</span><span style="opacity: 0.7; font-size: 10px;">\xD7</span>`,s.style.background="#28a745",setTimeout(()=>{s.innerHTML='<span>\u{1F5D1}\uFE0F</span><span>Clear Cache</span><span style="opacity: 0.7; font-size: 10px;">\xD7</span>',s.style.background="#dc3545"},2e3),console.log(`[Supernal TTS Dev] Cleared ${i} cached items`)};s.addEventListener("click",i=>{i.preventDefault();let a=i.clientX-i.target.getBoundingClientRect().left,l=s.getBoundingClientRect().width;a>l*.8?(s.style.display="none",t.style.display="block",localStorage.setItem("supernal-tts-dev-minimized","true")):o()}),t.addEventListener("click",i=>{i.preventDefault(),t.style.display="none",s.style.display="flex",localStorage.setItem("supernal-tts-dev-minimized","false")}),localStorage.getItem("supernal-tts-dev-minimized")==="true"&&(s.style.display="none",t.style.display="block"),e.appendChild(s),e.appendChild(t),document.body?document.body.appendChild(e):document.addEventListener("DOMContentLoaded",()=>{document.body.appendChild(e)})}async handlePlayClick(e,t,s){let{hash:r,progressBar:o}=s;if(e.classList.contains("supernal-tts-playing")&&this.currentButton===e){this.pauseAudio();return}if(e.classList.contains("supernal-tts-paused")&&this.currentButton===e){this.resumeAudio();return}if(!this.isProcessing){this.currentAudio&&this.currentButton&&this.currentButton!==e&&this.stopAudio();try{this.isProcessing=!0,this.setButtonState(e,"loading");let n=this.getCachedAudioUrl(r);if(!n)if(this.generationMode==="progressive"||this.generationMode==="standard"&&t.length>this.progressiveThreshold){await this.generateAudioProgressive(t,s,e,o);return}else{let a=await this.generateAudio(t,s);n=a.audioUrl.startsWith("http")?a.audioUrl:`${this.apiUrl}${a.audioUrl}`,this.cacheAudioUrl(r,n,a)}await this.playAudio(n,e,o)}catch(n){console.error("TTS Error:",n),this.setButtonState(e,"error"),this.showError(e,n.message),setTimeout(()=>this.setButtonState(e,"ready"),3e3),this.isProcessing=!1}}}async generateAudio(e,t){let s={text:e,options:{provider:t.provider,voice:t.voice,speed:t.speed||1}},r={"Content-Type":"application/json"},o=t.apiKey||this.apiKey;console.log("[TTS Widget] Generate Audio - API Key present:",!!o),console.log("[TTS Widget] Generate Audio - this.apiKey:",!!this.apiKey),console.log("[TTS Widget] Generate Audio - options.apiKey:",!!t.apiKey),o?(r.Authorization=`Bearer ${o}`,console.log("[TTS Widget] Generate Audio - Authorization header added")):console.warn("[TTS Widget] Generate Audio - NO API KEY, Authorization header NOT added");try{let n=await fetch(`${this.apiUrl}/api/v1/generate`,{method:"POST",headers:r,body:JSON.stringify(s)});if(!n.ok){let a=await n.json().catch(()=>({}));throw new Error(a.error||`HTTP ${n.status}: ${n.statusText}`)}let i=await n.json();return{audioUrl:`${this.apiUrl}${i.audioUrl}`,hash:i.hash,cached:i.cached,duration:i.duration,cost:i.cost}}catch(n){if(console.error("TTS generation failed:",n),n.message.includes("Failed to fetch")||n.message.includes("NetworkError")){let i=n.message;throw new Error(`TTS API unreachable: ${i}. Check if ${this.apiUrl} is accessible.`)}throw n}}async generateAudioProgressive(e,t,s,r){let o={text:e,options:{provider:t.provider,voice:t.voice,speed:t.speed||1}},n={"Content-Type":"application/json",Accept:"text/event-stream"},i=t.apiKey||this.apiKey;i&&(n.Authorization=`Bearer ${i}`);try{let a=await fetch(`${this.apiUrl}/api/v1/generate-progressive`,{method:"POST",headers:n,body:JSON.stringify(o)});if(!a.ok){let c=await a.json().catch(()=>({}));throw new Error(c.error||`HTTP ${a.status}: ${a.statusText}`)}if(!a.body)throw new Error("No response body for SSE stream");let l=a.body.getReader(),d=new TextDecoder,h="",v=0,g=0,f=(c,m)=>{if(r){let T=c/m*100,y=r.querySelector(".supernal-tts-progress-slider");y&&(y.value=T.toString());let u=r.querySelector(".current");u&&(u.textContent=`Chunk ${c}/${m}`)}};for(;;){let{done:c,value:m}=await l.read();if(c)break;h+=d.decode(m,{stream:!0});let T=h.split(`
94
94
 
95
- `);h=T.pop()||"";for(let y of T)if(y.startsWith("data: "))try{let u=JSON.parse(y.substring(6));switch(u.type){case"started":v=u.totalChunks,console.log(`[Progressive TTS] Started: ${v} chunks`),this.setButtonState(s,"loading");break;case"progress":g=u.completed,console.log(`[Progressive TTS] Progress: ${g}/${v}`),f(g,v);break;case"complete":console.log("[Progressive TTS] Complete:",u);let p=u.audioUrl.startsWith("http")?u.audioUrl:`${this.apiUrl}${u.audioUrl}`;this.cacheAudioUrl(t.hash,p,{hash:u.hash,cached:!1,duration:u.duration,cost:u.cost}),await this.playAudio(p,s,r);break;case"error":throw new Error(u.message||"Progressive generation failed")}}catch(u){console.warn("[Progressive TTS] Failed to parse event:",y,u)}}}catch(a){throw console.error("Progressive TTS generation failed:",a),this.isProcessing=!1,a}}async playAudio(e,t,s,r){return new Promise((o,n)=>{let i=new Audio(e);this.currentAudio=i,this.currentButton=t,this.clientSideSpeed&&(i.preservesPitch=!0,i.mozPreservesPitch=!0,i.webkitPreservesPitch=!0),r!==void 0&&this.clientSideSpeed&&(i.playbackRate=r),this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),i.addEventListener("loadstart",()=>{this.currentAudio===i&&this.setButtonState(t,"loading")}),i.addEventListener("canplay",()=>{if(this.currentAudio===i){this.setButtonState(t,"playing"),this.isProcessing=!1;let a=i.play();a!==void 0&&a.catch(l=>{console.error("Audio play failed:",l),this.currentAudio===i&&(this.setButtonState(t,"error"),this.currentAudio=null,this.currentButton=null,this.isProcessing=!1,n(new Error("Audio playback failed")))}),s&&(s.classList.add("playing"),this.progressUpdateInterval=window.setInterval(()=>{this.updateProgressBar(i,s)},100))}}),i.addEventListener("ended",()=>{if(this.currentAudio===i){if(this.setButtonState(t,"ready"),this.currentAudio=null,this.currentButton=null,this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),s){s.classList.remove("playing");let a=s.querySelector(".supernal-tts-progress-slider");a&&(a.value="0");let l=s.querySelector(".supernal-tts-time-display");l&&(l.innerHTML='<span class="current">0:00</span> / <span class="duration">0:00</span>')}o()}}),i.addEventListener("error",a=>{this.currentAudio===i&&(this.setButtonState(t,"error"),this.currentAudio=null,this.currentButton=null,this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),s&&s.classList.remove("playing"),n(new Error("Audio playback failed")))}),i.load()})}updateProgressBar(e,t){if(!e||!t)return;let s=t.querySelector(".supernal-tts-progress-slider"),r=t.querySelector(".current"),o=t.querySelector(".duration");if(s&&!isNaN(e.duration)){let n=e.currentTime/e.duration*100;s.value=n.toString()}r&&(r.textContent=this.formatTime(e.currentTime)),o&&!isNaN(e.duration)&&(o.textContent=this.formatTime(e.duration))}pauseAudio(){this.currentAudio&&!this.currentAudio.paused&&this.currentAudio.pause(),this.currentButton&&this.setButtonState(this.currentButton,"paused")}resumeAudio(){if(this.currentAudio&&this.currentAudio.paused){let e=this.currentAudio.play();e!==void 0&&e.then(()=>{this.currentButton&&this.setButtonState(this.currentButton,"playing")}).catch(t=>{console.error("Audio resume failed:",t),this.currentButton&&this.setButtonState(this.currentButton,"error")})}}stopAudio(){this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),this.currentAudio&&(this.currentAudio.pause(),this.currentAudio.currentTime=0,this.currentAudio=null),this.currentButton&&(this.setButtonState(this.currentButton,"ready"),this.currentButton=null),this.isProcessing=!1}cleanStopAudio(e){if(this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),this.currentAudio&&(this.currentAudio.pause(),this.currentAudio.currentTime=0,this.currentAudio=null),this.currentButton&&(this.setButtonState(this.currentButton,"ready"),this.currentButton=null),e){e.classList.remove("playing");let t=e.querySelector(".supernal-tts-progress-slider");t&&(t.value="0");let s=e.querySelector(".supernal-tts-time-display");s&&(s.innerHTML='<span class="current">0:00</span> / <span class="duration">0:00</span>')}this.isProcessing=!1}cleanStopAudioKeepProgress(e){this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),this.currentAudio&&(this.currentAudio.pause(),this.currentAudio.currentTime=0,this.currentAudio=null),this.currentButton&&this.setButtonState(this.currentButton,"loading"),this.isProcessing=!1}async handlePlayClickFromPosition(e,t,s,r){let{hash:o,progressBar:n}=s;if(!this.isProcessing)try{this.isProcessing=!0,this.setButtonState(e,"loading");let i=this.getCachedAudioUrl(o);if(!i){let a=await this.generateAudio(t,s);i=a.audioUrl.startsWith("http")?a.audioUrl:`${this.apiUrl}${a.audioUrl}`,this.cacheAudioUrl(o,i,a)}await this.playAudioFromPosition(i,e,n,r)}catch(i){console.error("TTS Error:",i),this.setButtonState(e,"error"),this.showError(e,i.message),setTimeout(()=>this.setButtonState(e,"ready"),3e3),this.isProcessing=!1}}async playAudioFromPosition(e,t,s,r){return new Promise((o,n)=>{let i=new Audio(e);this.currentAudio=i,this.currentButton=t,this.clientSideSpeed&&(i.preservesPitch=!0,i.mozPreservesPitch=!0,i.webkitPreservesPitch=!0),this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null);let a=!1;i.addEventListener("loadstart",()=>{this.currentAudio===i&&this.setButtonState(t,"loading")}),i.addEventListener("loadedmetadata",()=>{this.currentAudio===i&&!a&&r>0&&(i.currentTime=i.duration*r,a=!0)}),i.addEventListener("canplay",()=>{if(this.currentAudio===i){!a&&r>0&&i.duration>0&&(i.currentTime=i.duration*r,a=!0),this.setButtonState(t,"playing"),this.isProcessing=!1;let l=i.play();l!==void 0&&l.catch(d=>{console.error("Audio play failed:",d),this.currentAudio===i&&(this.setButtonState(t,"error"),this.currentAudio=null,this.currentButton=null,this.isProcessing=!1,n(new Error("Audio playback failed")))}),s&&(s.classList.add("playing"),this.progressUpdateInterval=window.setInterval(()=>{this.updateProgressBar(i,s)},100))}}),i.addEventListener("ended",()=>{if(this.currentAudio===i){if(this.setButtonState(t,"ready"),this.currentAudio=null,this.currentButton=null,this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),s){s.classList.remove("playing");let l=s.querySelector(".supernal-tts-progress-slider");l&&(l.value="0");let d=s.querySelector(".supernal-tts-time-display");d&&(d.innerHTML='<span class="current">0:00</span> / <span class="duration">0:00</span>')}o()}}),i.addEventListener("error",l=>{this.currentAudio===i&&(this.setButtonState(t,"error"),this.currentAudio=null,this.currentButton=null,this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),s&&s.classList.remove("playing"),n(new Error("Audio playback failed")))}),i.load()})}setButtonState(e,t){e.classList.remove("supernal-tts-loading","supernal-tts-playing","supernal-tts-paused","supernal-tts-error");let s=e.querySelector(".supernal-tts-play-icon"),r=e.querySelector(".supernal-tts-text");switch(t){case"loading":e.classList.add("supernal-tts-loading"),e.disabled=!0,r&&(r.textContent="Loading...");break;case"playing":e.classList.add("supernal-tts-playing"),e.disabled=!1,s&&(s.innerHTML='<rect x="6" y="4" width="4" height="16"/><rect x="14" y="4" width="4" height="16"/>'),r&&(r.textContent="Pause");break;case"paused":e.classList.add("supernal-tts-paused"),e.disabled=!1,s&&(s.innerHTML='<path d="M8 5v14l11-7z"/>'),r&&(r.textContent="Resume");break;case"error":e.classList.add("supernal-tts-error"),e.disabled=!1,s&&(s.innerHTML='<path d="M12 2L2 7v10c0 5.55 3.84 9.95 9 11 5.16-1.05 9-5.45 9-11V7l-10-5z"/>'),r&&(r.textContent="Error");break;default:e.disabled=!1,s&&(s.innerHTML='<path d="M8 5v14l11-7z"/>'),r&&(r.textContent="Listen");break}}showError(e,t){let s=document.createElement("div");s.className="supernal-tts-error-tooltip",t.includes("development mode")?s.textContent=t:t.includes("Failed to fetch")?s.textContent="Service unavailable (dev mode)":s.textContent="Audio generation failed",e.parentNode?.appendChild(s),setTimeout(()=>{s.parentNode&&s.parentNode.removeChild(s)},3e3)}generateHash(e,t={}){let s=e+JSON.stringify(t),r=0;for(let o=0;o<s.length;o++){let n=s.charCodeAt(o);r=(r<<5)-r+n,r=r&r}return Math.abs(r).toString(36)}getCachedAudioUrl(e){let t=this.cache.get(e);if(!t)return null;if(Date.now()>t.expiry)return this.cache.delete(e),this.saveCacheToStorage(),null;let s=t.audioUrl;return s.startsWith("http")?s:`${this.apiUrl}${s}`}cacheAudioUrl(e,t,s={}){this.cache.set(e,{audioUrl:t,metadata:s,expiry:Date.now()+this.cacheExpiry,timestamp:Date.now()}),this.saveCacheToStorage()}loadCacheFromStorage(){try{let e=localStorage.getItem("supernal-tts-cache");if(e){let t=JSON.parse(e);this.cache=new Map(Object.entries(t))}}catch(e){console.warn("Failed to load TTS cache:",e)}}saveCacheToStorage(){try{let e=Object.fromEntries(this.cache);localStorage.setItem("supernal-tts-cache",JSON.stringify(e))}catch(e){console.warn("Failed to save TTS cache:",e)}}static init(e){return window.SupernalTTSInstance?(console.debug("SupernalTTS already initialized, returning existing instance"),window.SupernalTTSInstance):(window.SupernalTTSInstance=new w(e),window.SupernalTTSInstance)}static getInstance(){return window.SupernalTTSInstance}addWidget(e,t,s={}){let r=null;if(typeof e=="string"?r=document.querySelector(e):r=e,!r){console.error("TTS Widget: Element not found");return}r.classList.add("supernal-tts-widget"),r.dataset.text=this.scrubText(t),s.voice&&(r.dataset.voice=s.voice),s.provider&&(r.dataset.provider=s.provider),this.setupWidget(r)}};if(typeof window<"u")if(window.SupernalTTS)console.debug("SupernalTTS already loaded, skipping redeclaration");else{window.SupernalTTS=L;let w=document.querySelector("script[data-supernal-tts-auto-init]");if(w&&w.dataset){let e=JSON.parse(w.dataset.supernalTtsAutoInit||"{}");L.init(e)}}var A=L;export{L as SupernalTTS,A as default};
95
+ `);h=T.pop()||"";for(let y of T)if(y.startsWith("data: "))try{let u=JSON.parse(y.substring(6));switch(u.type){case"started":v=u.totalChunks,console.log(`[Progressive TTS] Started: ${v} chunks`),this.setButtonState(s,"loading");break;case"progress":g=u.completed,console.log(`[Progressive TTS] Progress: ${g}/${v}`),f(g,v);break;case"complete":console.log("[Progressive TTS] Complete:",u);let p=u.audioUrl.startsWith("http")?u.audioUrl:`${this.apiUrl}${u.audioUrl}`;this.cacheAudioUrl(t.hash,p,{hash:u.hash,cached:!1,duration:u.duration,cost:u.cost}),await this.playAudio(p,s,r);break;case"error":throw new Error(u.message||"Progressive generation failed")}}catch(u){console.warn("[Progressive TTS] Failed to parse event:",y,u)}}}catch(a){throw console.error("Progressive TTS generation failed:",a),this.isProcessing=!1,a}}async playAudio(e,t,s,r){return new Promise((o,n)=>{let i=new Audio(e);this.currentAudio=i,this.currentButton=t,this.clientSideSpeed&&(i.preservesPitch=!0,i.mozPreservesPitch=!0,i.webkitPreservesPitch=!0),r!==void 0&&this.clientSideSpeed&&(i.playbackRate=r),this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),i.addEventListener("loadstart",()=>{this.currentAudio===i&&this.setButtonState(t,"loading")}),i.addEventListener("canplay",()=>{if(this.currentAudio===i){this.setButtonState(t,"playing"),this.isProcessing=!1;let a=i.play();a!==void 0&&a.catch(l=>{console.error("Audio play failed:",l),this.currentAudio===i&&(this.setButtonState(t,"error"),this.currentAudio=null,this.currentButton=null,this.isProcessing=!1,n(new Error("Audio playback failed")))}),s&&(s.classList.add("playing"),this.progressUpdateInterval=window.setInterval(()=>{this.updateProgressBar(i,s)},100))}}),i.addEventListener("ended",()=>{if(this.currentAudio===i){if(this.setButtonState(t,"ready"),this.currentAudio=null,this.currentButton=null,this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),s){s.classList.remove("playing");let a=s.querySelector(".supernal-tts-progress-slider");a&&(a.value="0");let l=s.querySelector(".supernal-tts-time-display");l&&(l.innerHTML='<span class="current">0:00</span> / <span class="duration">0:00</span>')}o()}}),i.addEventListener("error",a=>{this.currentAudio===i&&(this.setButtonState(t,"error"),this.currentAudio=null,this.currentButton=null,this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),s&&s.classList.remove("playing"),n(new Error("Audio playback failed")))}),i.load()})}updateProgressBar(e,t){if(!e||!t)return;let s=t.querySelector(".supernal-tts-progress-slider"),r=t.querySelector(".current"),o=t.querySelector(".duration");if(s&&!isNaN(e.duration)){let n=e.currentTime/e.duration*100;s.value=n.toString()}r&&(r.textContent=this.formatTime(e.currentTime)),o&&!isNaN(e.duration)&&(o.textContent=this.formatTime(e.duration))}pauseAudio(){this.currentAudio&&!this.currentAudio.paused&&this.currentAudio.pause(),this.currentButton&&this.setButtonState(this.currentButton,"paused")}resumeAudio(){if(this.currentAudio&&this.currentAudio.paused){let e=this.currentAudio.play();e!==void 0&&e.then(()=>{this.currentButton&&this.setButtonState(this.currentButton,"playing")}).catch(t=>{console.error("Audio resume failed:",t),this.currentButton&&this.setButtonState(this.currentButton,"error")})}}stopAudio(){this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),this.currentAudio&&(this.currentAudio.pause(),this.currentAudio.currentTime=0,this.currentAudio=null),this.currentButton&&(this.setButtonState(this.currentButton,"ready"),this.currentButton=null),this.isProcessing=!1}cleanStopAudio(e){if(this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),this.currentAudio&&(this.currentAudio.pause(),this.currentAudio.currentTime=0,this.currentAudio=null),this.currentButton&&(this.setButtonState(this.currentButton,"ready"),this.currentButton=null),e){e.classList.remove("playing");let t=e.querySelector(".supernal-tts-progress-slider");t&&(t.value="0");let s=e.querySelector(".supernal-tts-time-display");s&&(s.innerHTML='<span class="current">0:00</span> / <span class="duration">0:00</span>')}this.isProcessing=!1}cleanStopAudioKeepProgress(e){this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),this.currentAudio&&(this.currentAudio.pause(),this.currentAudio.currentTime=0,this.currentAudio=null),this.currentButton&&this.setButtonState(this.currentButton,"loading"),this.isProcessing=!1}async handlePlayClickFromPosition(e,t,s,r){let{hash:o,progressBar:n}=s;if(!this.isProcessing)try{this.isProcessing=!0,this.setButtonState(e,"loading");let i=this.getCachedAudioUrl(o);if(!i){let a=await this.generateAudio(t,s);i=a.audioUrl.startsWith("http")?a.audioUrl:`${this.apiUrl}${a.audioUrl}`,this.cacheAudioUrl(o,i,a)}await this.playAudioFromPosition(i,e,n,r)}catch(i){console.error("TTS Error:",i),this.setButtonState(e,"error"),this.showError(e,i.message),setTimeout(()=>this.setButtonState(e,"ready"),3e3),this.isProcessing=!1}}async playAudioFromPosition(e,t,s,r){return new Promise((o,n)=>{let i=new Audio(e);this.currentAudio=i,this.currentButton=t,this.clientSideSpeed&&(i.preservesPitch=!0,i.mozPreservesPitch=!0,i.webkitPreservesPitch=!0),this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null);let a=!1;i.addEventListener("loadstart",()=>{this.currentAudio===i&&this.setButtonState(t,"loading")}),i.addEventListener("loadedmetadata",()=>{this.currentAudio===i&&!a&&r>0&&(i.currentTime=i.duration*r,a=!0)}),i.addEventListener("canplay",()=>{if(this.currentAudio===i){!a&&r>0&&i.duration>0&&(i.currentTime=i.duration*r,a=!0),this.setButtonState(t,"playing"),this.isProcessing=!1;let l=i.play();l!==void 0&&l.catch(d=>{console.error("Audio play failed:",d),this.currentAudio===i&&(this.setButtonState(t,"error"),this.currentAudio=null,this.currentButton=null,this.isProcessing=!1,n(new Error("Audio playback failed")))}),s&&(s.classList.add("playing"),this.progressUpdateInterval=window.setInterval(()=>{this.updateProgressBar(i,s)},100))}}),i.addEventListener("ended",()=>{if(this.currentAudio===i){if(this.setButtonState(t,"ready"),this.currentAudio=null,this.currentButton=null,this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),s){s.classList.remove("playing");let l=s.querySelector(".supernal-tts-progress-slider");l&&(l.value="0");let d=s.querySelector(".supernal-tts-time-display");d&&(d.innerHTML='<span class="current">0:00</span> / <span class="duration">0:00</span>')}o()}}),i.addEventListener("error",l=>{this.currentAudio===i&&(this.setButtonState(t,"error"),this.currentAudio=null,this.currentButton=null,this.progressUpdateInterval&&(clearInterval(this.progressUpdateInterval),this.progressUpdateInterval=null),s&&s.classList.remove("playing"),n(new Error("Audio playback failed")))}),i.load()})}setButtonState(e,t){e.classList.remove("supernal-tts-loading","supernal-tts-playing","supernal-tts-paused","supernal-tts-error");let s=e.querySelector(".supernal-tts-play-icon"),r=e.querySelector(".supernal-tts-text");switch(t){case"loading":e.classList.add("supernal-tts-loading"),e.disabled=!0,r&&(r.textContent="Loading...");break;case"playing":e.classList.add("supernal-tts-playing"),e.disabled=!1,s&&(s.innerHTML='<rect x="6" y="4" width="4" height="16"/><rect x="14" y="4" width="4" height="16"/>'),r&&(r.textContent="Pause");break;case"paused":e.classList.add("supernal-tts-paused"),e.disabled=!1,s&&(s.innerHTML='<path d="M8 5v14l11-7z"/>'),r&&(r.textContent="Resume");break;case"error":e.classList.add("supernal-tts-error"),e.disabled=!1,s&&(s.innerHTML='<path d="M12 2L2 7v10c0 5.55 3.84 9.95 9 11 5.16-1.05 9-5.45 9-11V7l-10-5z"/>'),r&&(r.textContent="Error");break;default:e.disabled=!1,s&&(s.innerHTML='<path d="M8 5v14l11-7z"/>'),r&&(r.textContent="Listen");break}}showError(e,t){let s=document.createElement("div");s.className="supernal-tts-error-tooltip",t.includes("development mode")?s.textContent=t:t.includes("Failed to fetch")?s.textContent="Service unavailable (dev mode)":s.textContent="Audio generation failed",e.parentNode?.appendChild(s),setTimeout(()=>{s.parentNode&&s.parentNode.removeChild(s)},3e3)}generateHash(e,t={}){let s=e+JSON.stringify(t),r=0;for(let o=0;o<s.length;o++){let n=s.charCodeAt(o);r=(r<<5)-r+n,r=r&r}return Math.abs(r).toString(36)}getCachedAudioUrl(e){let t=this.cache.get(e);if(!t)return null;if(Date.now()>t.expiry)return this.cache.delete(e),this.saveCacheToStorage(),null;let s=t.audioUrl;return s.startsWith("http")?s:`${this.apiUrl}${s}`}cacheAudioUrl(e,t,s={}){this.cache.set(e,{audioUrl:t,metadata:s,expiry:Date.now()+this.cacheExpiry,timestamp:Date.now()}),this.saveCacheToStorage()}loadCacheFromStorage(){try{let e=localStorage.getItem("supernal-tts-cache");if(e){let t=JSON.parse(e);this.cache=new Map(Object.entries(t))}}catch(e){console.warn("Failed to load TTS cache:",e)}}saveCacheToStorage(){try{let e=Object.fromEntries(this.cache);localStorage.setItem("supernal-tts-cache",JSON.stringify(e))}catch(e){console.warn("Failed to save TTS cache:",e)}}static init(e){return window.SupernalTTSInstance?(console.debug("SupernalTTS already initialized, returning existing instance"),window.SupernalTTSInstance):(window.SupernalTTSInstance=new L(e),window.SupernalTTSInstance)}static getInstance(){return window.SupernalTTSInstance}addWidget(e,t,s={}){let r=null;if(typeof e=="string"?r=document.querySelector(e):r=e,!r){console.error("TTS Widget: Element not found");return}r.classList.add("supernal-tts-widget"),r.dataset.text=this.scrubText(t),s.voice&&(r.dataset.voice=s.voice),s.provider&&(r.dataset.provider=s.provider),this.setupWidget(r)}};if(typeof window<"u")if(window.SupernalTTS)console.debug("SupernalTTS already loaded, skipping redeclaration");else{window.SupernalTTS=w;let L=()=>{if(!window.SupernalTTSInstance){let s=document.querySelector("script[data-supernal-tts-auto-init]");if(s&&s.dataset){let r=JSON.parse(s.dataset.supernalTtsAutoInit||"{}");w.init(r)}}let t=document.querySelectorAll(".supernal-tts-widget");if(t.length>0&&window.SupernalTTSInstance){let s=window.SupernalTTSInstance;t.forEach(r=>{r.classList.contains("supernal-tts-widget-initialized")||s.setupWidget(r)})}};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",L):L()}var A=w;export{w as SupernalTTS,A as default};
96
96
  //# sourceMappingURL=widget.js.map