@streamoji/aitwin 0.6.9 → 0.7.0

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/aitwin.cjs CHANGED
@@ -1,8 +1,8 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const F=require("react/jsx-runtime"),h=require("react"),gs=require("@pipecat-ai/client-js"),Qn=require("@pipecat-ai/websocket-transport"),ye=require("pixi.js");var Rn=typeof document<"u"?document.currentScript:null;const ws="https://aitwin.bubu.social",ui={publicBase:ws},ys="0.6.9",vs={version:ys},_s={},Ct="https://ai.aitwin.me",Xn=`${ui.publicBase}/custom-faces`,bs=`${ui.publicBase}/aitwin-workers/v${vs.version}/visemeDiffPreview.worker.js`;function Ss(e){if(e)return e;const t=typeof{url:typeof document>"u"?require("url").pathToFileURL(__filename).href:Rn&&Rn.tagName.toUpperCase()==="SCRIPT"&&Rn.src||new URL("aitwin.cjs",document.baseURI).href}<"u"?_s?.VITE_AITWIN_WORKER_URL:void 0;return t?.trim()?t.trim():bs}const Ts="https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin",Cn=3e3,Ot=16,Cr=3e3,ks=4e3,Es=.85,xs=8,Jn=400,Zn="eng_a7f2b9c1",dn="eng_d4e8f3a2",st="eng_c9b1e6d4",at="visemetts",ht=st,fn="eve",Un=new Set([dn,st,at]),er={[Zn]:"oculus_direct",[st]:"oculus_direct",[at]:"oculus_direct",[dn]:"inworld_word"},Vn={google:Zn,inworld:dn,cartesia:st,visemetts:at};function Is(e){const t=e.trim();if(t)return t in er?t:Vn[t.toLowerCase()]}function di(e){const t=e?.trim();if(!t)return{};const n=t.indexOf(":");if(n<=0)return{voiceId:t};const r=Is(t.slice(0,n));if(!r)return{voiceId:t};const i=t.slice(n+1).trim();return{ttsEngineId:r,voiceId:i||void 0}}function it(e,t){const n=di(t);return{ttsEngineId:n.ttsEngineId??e,voiceId:n.voiceId}}function fi(e){return er[e]}function mi(e){return e===st}function hi(e){return e===at}function As(e){if(e.ttsEngineId&&e.ttsEngineId in er)return e.ttsEngineId;const t=e.tts?.toLowerCase();return t&&t in Vn?Vn[t]:ht}class rn extends Error{constructor(t="AI twin not found"){super(t),this.name="AiTwinNotFoundError"}}function tr(e,...t){const n=typeof e=="string"?e:e?.brainId?.trim()||e?.knowledgeContextId?.trim();for(const r of[n,...t]){const i=r?.trim();if(i)return i}}function nr(e,...t){const n=typeof e=="string"?e:e?.brainUrl?.trim();for(const r of[n,...t]){const i=r?.trim();if(i)return i}}function Ms(e){const t=(e??"").trim();if(!t)return;let n;try{n=new URL(t)}catch{throw new Error("brainUrl must be an http or https URL")}if(n.protocol!=="http:"&&n.protocol!=="https:")throw new Error("brainUrl must be an http or https URL");if(!n.host)throw new Error("brainUrl must be an http or https URL");if(n.hash)throw new Error("brainUrl must not include a fragment");return t}async function pi(e,t=Ts){const n=new URL(t);n.searchParams.set("id",e);const r=await fetch(n.toString());if(r.status===404)throw new rn;if(!r.ok)throw new Error(`getAiTwin failed (${r.status})`);const i=await r.json();if(!i.success)throw new rn(i.error??"AI twin not found");const{tts:s,token:o,...a}=i.data,u=String(a.authToken??o??"").trim()||void 0,d=tr(a),l=nr(a);return{...a,ttsEngineId:As(i.data),authToken:u,brainId:d,knowledgeContextId:d,brainUrl:l}}const rr="thumbnail.webp";function gi(e,t=Xn){const n=e.trim();if(!n)throw new Error("faceId is required");return`${t.replace(/\/$/,"")}/${n}/${rr}`}function Dr(e){const t=e?.trim();if(!t)return null;try{return gi(t)}catch{return null}}function Ps(e){if(!e)return null;const t=(e.binBase||e.twinBase).trim().replace(/\/$/,"");return t?`${t}/${rr}`:null}const Rs="https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken",Cs="client_6TNvp3SCs4Og0a1ijm9TommXLql1",Ds="kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1",Bs="6TNvp3SCs4Og0a1ijm9TommXLql1",Fs="Swaraj Mali";async function nn(){const e=await fetch(Rs,{method:"POST",headers:{"Content-Type":"application/json","Client-Id":Cs,"Client-Secret":Ds},body:JSON.stringify({userId:Bs,userName:Fs})});if(!e.ok)throw new Error(`getAuthToken failed (${e.status})`);const t=await e.json(),n=t.authToken??t.token??"";if(!n.trim())throw new Error("getAuthToken returned no token");return n}async function Ls(e,t=Ct){const n=await fetch(`${t.replace(/\/$/,"")}/api/session-value`,{headers:{Authorization:`Bearer ${e}`}});if(!n.ok)throw new Error(`/api/session-value failed (${n.status})`);const r=await n.json();if(!r.value)throw new Error("Session value response missing value");return r}function Ns(e){const t=e.trim();if(t.length%2!==0)throw new Error("Invalid hex key");const n=new Uint8Array(t.length/2);for(let r=0;r<t.length;r+=2)n[r/2]=Number.parseInt(t.slice(r,r+2),16);return n}function Xt(e){return Uint8Array.from(e)}function Os(e,t){const n=new Uint8Array(e.length+t.length);return n.set(e,0),n.set(t,e.length),n}async function Us(e,t){if(!t)throw new Error("Asset key is required for encrypted assets");if(e.byteLength<28)throw new Error("Encrypted payload too small");const n=new Uint8Array(e),r=Xt(n.subarray(0,12)),i=Xt(n.subarray(12,28)),s=Xt(n.subarray(28)),o=Xt(Os(s,i)),a=await crypto.subtle.importKey("raw",Ns(t),{name:"AES-GCM"},!1,["decrypt"]);return crypto.subtle.decrypt({name:"AES-GCM",iv:r,tagLength:128},a,o)}let Wn=null;const zn=new Set;function Jt(e){return e.includes("/custom-faces/")}function sn(e){const t=e.binBase.trim().replace(/\/$/,""),n=e.twinBase.trim().replace(/\/$/,"");if(!(e.encrypted||Jt(t)||Jt(n)))return{twinBase:n,binBase:t,encrypted:!1};const i=Jt(t)?t:Jt(n)?n:t;return{twinBase:i,binBase:i,encrypted:!0}}function wt(e){return sn(e).encrypted}function Hn(e,t=Xn){const n=`${t.replace(/\/$/,"")}/${e}`;return{twinBase:n,binBase:n,encrypted:!0}}function wi(e,t){return(e??t)?.trim()||void 0}function yi(e){Wn=sn(e);for(const t of zn)t()}function Vs(e,t){const n=typeof globalThis.location<"u"?globalThis.location.href:void 0;if(!n)return sn(e);const r=i=>{try{return new URL(i,n).href.replace(/\/$/,"")}catch{return i}};return sn({twinBase:r(e.twinBase),binBase:r(e.binBase),encrypted:e.encrypted})}function yt(){if(!Wn)throw new Error("Twin assets not configured; call setActiveTwinAssets first");return Wn}function vi(e){return zn.add(e),()=>zn.delete(e)}function an(){const e=yt();return wt(e)?`${e.binBase}/sil.bin`:`${e.twinBase}/sil.png`}function Ws(){const e=yt();return wt(e)?`${e.binBase}/idle.bin`:`${e.twinBase}/idle.mp4`}function _i(){const e=yt();return wt(e)?e.binBase:e.twinBase}function Wt(){return wt(yt())}function bi(){const e=yt();return wt(e)?`${e.binBase}/atlas.bin`:`${e.twinBase}/atlas.json`}function Si(){const e=yt();return wt(e)?`${e.binBase}/expression_atlas.bin`:`${e.twinBase}/expression_atlas.json`}const pt=[{code:"en",label:"English",visemetts:!0,cartesia:!0},{code:"es",label:"Spanish",visemetts:!0,cartesia:!0},{code:"es-MX",label:"Spanish (Mexico)",visemetts:!0,cartesia:!0},{code:"es-ES",label:"Spanish (Spain)",visemetts:!0,cartesia:!0},{code:"pt",label:"Portuguese",visemetts:!0,cartesia:!0},{code:"pt-BR",label:"Portuguese (Brazil)",visemetts:!0,cartesia:!0},{code:"pt-PT",label:"Portuguese (Portugal)",visemetts:!0,cartesia:!0},{code:"fr",label:"French",visemetts:!0,cartesia:!0},{code:"de",label:"German",visemetts:!0,cartesia:!0},{code:"it",label:"Italian",visemetts:!0,cartesia:!0},{code:"zh",label:"Chinese",visemetts:!0,cartesia:!0},{code:"ja",label:"Japanese",visemetts:!0,cartesia:!0},{code:"hi",label:"Hindi",visemetts:!0,cartesia:!0},{code:"ko",label:"Korean",visemetts:!0,cartesia:!0},{code:"ru",label:"Russian",visemetts:!0,cartesia:!0},{code:"tr",label:"Turkish",visemetts:!0,cartesia:!0},{code:"id",label:"Indonesian",visemetts:!0,cartesia:!0},{code:"ar",label:"Arabic",visemetts:!0,cartesia:!0},{code:"ar-EG",label:"Arabic (Egypt)",visemetts:!0,cartesia:!0},{code:"ar-AE",label:"Arabic (UAE)",visemetts:!0,cartesia:!0},{code:"vi",label:"Vietnamese",visemetts:!0,cartesia:!0},{code:"bn",label:"Bengali",visemetts:!0,cartesia:!0},{code:"auto",label:"Auto-detect",visemetts:!0,cartesia:!1},{code:"nl",label:"Dutch",visemetts:!1,cartesia:!0},{code:"pl",label:"Polish",visemetts:!1,cartesia:!0},{code:"sv",label:"Swedish",visemetts:!1,cartesia:!0},{code:"tl",label:"Tagalog",visemetts:!1,cartesia:!0},{code:"bg",label:"Bulgarian",visemetts:!1,cartesia:!0},{code:"ro",label:"Romanian",visemetts:!1,cartesia:!0},{code:"cs",label:"Czech",visemetts:!1,cartesia:!0},{code:"el",label:"Greek",visemetts:!1,cartesia:!0},{code:"fi",label:"Finnish",visemetts:!1,cartesia:!0},{code:"hr",label:"Croatian",visemetts:!1,cartesia:!0},{code:"ms",label:"Malay",visemetts:!1,cartesia:!0},{code:"sk",label:"Slovak",visemetts:!1,cartesia:!0},{code:"da",label:"Danish",visemetts:!1,cartesia:!0},{code:"ta",label:"Tamil",visemetts:!1,cartesia:!0},{code:"uk",label:"Ukrainian",visemetts:!1,cartesia:!0},{code:"hu",label:"Hungarian",visemetts:!1,cartesia:!0},{code:"no",label:"Norwegian",visemetts:!1,cartesia:!0},{code:"th",label:"Thai",visemetts:!1,cartesia:!0},{code:"he",label:"Hebrew",visemetts:!1,cartesia:!0},{code:"ka",label:"Georgian",visemetts:!1,cartesia:!0},{code:"te",label:"Telugu",visemetts:!1,cartesia:!0},{code:"gu",label:"Gujarati",visemetts:!1,cartesia:!0},{code:"kn",label:"Kannada",visemetts:!1,cartesia:!0},{code:"ml",label:"Malayalam",visemetts:!1,cartesia:!0},{code:"mr",label:"Marathi",visemetts:!1,cartesia:!0},{code:"pa",label:"Punjabi",visemetts:!1,cartesia:!0}],Ti=pt.map(e=>e.code),zs=new Set(Ti),Hs=new Set(pt.filter(e=>e.visemetts).map(e=>e.code)),$s=new Set(pt.filter(e=>e.cartesia).map(e=>e.code)),js="en";function ki(e){return zs.has(e)}function mn(e){const t=e?.trim();if(t)return t}function Ks(e){return e===at?pt.filter(t=>t.visemetts).map(t=>t.code):e===st?pt.filter(t=>t.cartesia).map(t=>t.code):pt.filter(t=>t.visemetts&&t.cartesia).map(t=>t.code)}function Gs(e,t){const n=t.trim();return n?e===at?Hs.has(n):e===st?$s.has(n):ki(n):!1}function Ei(e){const t=e.split(/\r?\n/);let n="",r="";for(const s of t)s.startsWith("event:")?n=s.slice(6).trim():s.startsWith("data:")&&(r=s.slice(5).trim());if(!n)return null;let i={};if(r)try{i=JSON.parse(r)}catch{i={raw:r}}return{event:n,data:i}}const xi=["aa","CH","DD","E","FF","I","O","PP","RR","SS","TH","U","kk","nn","sil"],qs=new Map(xi.map(e=>[e.toLowerCase(),e]));function hn(e){const t=(e??"").trim();return t?qs.get(t.toLowerCase())??"sil":"sil"}const Ys={aei:["aa","E","I"],o:["O","U"],ee:["I"],bmp:["PP"],fv:["FF"],l:["nn"],r:["RR"],th:["TH"],qw:["U","O"],cdgknstxyz:["DD","SS","kk","CH"]};function Qs(e){if(!e)return["sil"];const t=e.toLowerCase();return Ys[t]??["sil"]}function Ii(e,t){const n=e.word.length;if(n<=0)return 0;const r=e.wduration;if(r<=0)return 0;const i=Math.max(0,Math.min(1,(t-e.wtime)/r));return Math.min(n-1,Math.floor(i*n))}function At(e,t){const n=Math.max(0,Math.min(e.length-1,t));return e[n]??""}function pn(e,t){const n=e.toLowerCase().replace(/[^a-z']/g,"");return!n||At(n,t)!=="w"||t>0&&At(n,t-1)==="q"?!1:t===0&&/^w[aeiouy]/.test(n)}function Xs(e,t){const n=e.toLowerCase(),r=At(n,t),i=n.slice(Math.max(0,t-1),t+2);return r==="i"||r==="y"?"I":i.includes("ee")||r==="e"||n.includes("ea")?"E":"aa"}function Js(e,t){const n=e.toLowerCase(),r=n.slice(Math.max(0,t-1),Math.min(n.length,t+3));if(/oo|ou|uw/.test(r))return"U";if(pn(n,t))return"FF";const i=At(n,t);return i==="u"||i==="w"?"U":"O"}function Zs(e,t){const n=e.toLowerCase();if(pn(n,t))return"FF";const r=n.slice(Math.max(0,t-2),t+1);if(/qu/.test(r))return"U";const i=n.slice(Math.max(0,t-1),Math.min(n.length,t+3));return/wo|wh/.test(i)||At(n,t)==="o"?"O":"U"}const Zt={d:"DD",t:"DD",n:"DD",s:"SS",z:"SS",k:"kk",g:"kk",c:"CH",x:"CH",j:"CH"};function ea(e,t){const n=e.toLowerCase(),r=At(n,t);if(Zt[r])return Zt[r];for(let i=0;i<n.length;i++){const s=n[i];if(Zt[s])return Zt[s]}return"DD"}function Ai(e,t,n){const r=Qs(e);if(r.length===1)return r[0];const i=t.trim();if(!i)return r[0];const s=(e??"").toLowerCase(),o=Math.max(0,n);switch(s){case"aei":return Xs(i,o);case"o":return Js(i,o);case"qw":return Zs(i,o);case"cdgknstxyz":return ea(i,o);default:return r[0]}}const ir=16,$n=47,Mi=new Set(["aa","E","I"]),Pi=new Set(["DD","SS","kk","CH"]),ta=[Mi,Pi];function na(e){for(const t of ta)if(t.has(e))return t;return null}function ra(e){return na(e)!=null}function Ri(e,t=ir){return t<=0?e:Math.round(e/t)*t}function sr(e,t=ir){return e.map(n=>({...n,vtime:Ri(n.vtime,t)}))}function Ci(e){const t=new Map;for(const n of e)t.has(n.vtime)||t.set(n.vtime,n);return[...t.values()].sort((n,r)=>n.vtime-r.vtime)}function ar(e){if(e.length<=2)return[...e];const t=[e[0]];for(let n=1;n<e.length-1;n++){const r=e[n],i=e[n+1],s=t[t.length-1];if(!ra(r.viseme)){t.push(r);continue}const o=r.vtime-s.vtime,a=i.vtime-r.vtime;o<$n||a<$n||t.push(r)}return t.push(e[e.length-1]),t}function gn(e){return ar(Ci(sr(e)))}function Di(e,t){let n="sil";for(const r of e){if(r.vtime>t)break;r.viseme!=="sil"&&(n=r.viseme)}return n}function wn(e){const t=Ci(sr(e)),n=ar(t),r=new Set(n.map(s=>s.vtime)),i=[];for(const s of t)r.has(s.vtime)||i.push({viseme:s.viseme,vtime:s.vtime});return i}function ia(e){return wn(e).map(t=>t.viseme)}const sa=32,Br=48;function Bi(e){let t=0;for(const n of e)t=Math.max(t,n.vtime+n.vduration);return t}function Fi(e){let t=null,n=-1;for(const r of e){if(r.viseme==="sil")continue;const i=r.vtime+r.vduration;i>n&&(t=r,n=i)}return t}function aa(e){return gn(e).find(n=>n.viseme!=="sil")??null}function ft(e){const t=Bi(e);return Fi(e)?t+Jn:t}function oa(e){const t=[],n=aa(e);n&&n.vtime>0&&t.push({from:"sil",to:n.viseme,fromVtime:0,toVtime:n.vtime});const r=Fi(e),i=Bi(e);return r&&t.push({from:r.viseme,to:"sil",fromVtime:i,toVtime:i+Jn}),t}function Li(e){const t=gn(e),n=[];for(let r=0;r<t.length-1;r++){const i=t[r].viseme,s=t[r+1].viseme,o=t[r].vtime,a=t[r+1].vtime;i===s||a<=o||n.push({from:i,to:s,fromVtime:o,toVtime:a})}return n}function Ni(e){return e.length===0?[]:[...oa(e),...Li(e)]}function ca(e,t,n=!1){let r=null;for(const i of e){const{transStart:s,transEnd:o}=Oi(i.fromVtime,i.toVtime,i.from,i.to,n);t>=s&&t<o&&(r=i)}return r}function Oi(e,t,n,r,i=!1){const s=Math.max(0,t-e);if(i&&(n==="PP"||r==="PP")&&s>Br){const l=Br,c=t,f=c-l;return{gapMs:s,effectiveGapMs:l,holdEnd:f,transStart:f,transEnd:c}}const a=s>0?Math.max(s,sa):0,u=e,d=e+a;return{gapMs:s,effectiveGapMs:a,holdEnd:e,transStart:u,transEnd:d}}function Ui(e,t,n,r,i,s){r.forEach(o=>{const u=String(o.word??"").trim();if(!u)return;const d=Math.round((o.start??0)*1e3),l=o.duration!=null?Math.round(o.duration*1e3):o.end!=null?Math.max(1,Math.round((o.end-(o.start??0))*1e3)):0;t.push({word:u,wtime:i+d,wduration:l,queueIndex:t.length,gesture:o.gesture??null,emphasis:o.emphasis===!0})}),n.forEach(o=>{const a=String(o.symbol??o.viseme??"").trim();if(!a)return;const u=Math.round((o.start??0)*1e3),d=Math.max(1,Math.round((o.duration??0)*1e3)),l=i+u,c=zi(t,l),f=c?Ii(c,l):0;let g;fi(s)==="oculus_direct"?(g=hn(a),g==="U"&&c&&pn(c.word,f)&&(g="FF")):g=Ai(a,c?.word??"",f),e.push({viseme:g,weight:1,vtime:l,vduration:d})})}function Vi(e,t){let n=t;for(const r of e){const i=Math.round((r.start??0)*1e3),s=Math.max(1,Math.round((r.duration??0)*1e3));n=Math.max(n,t+i+s)}return n}const or=16e3;function Wi(e){const t=Number(e);return Number.isFinite(t)&&t>0?t:or}function la(e){let t=e.trim();const n=t.indexOf(",");t.startsWith("data:")&&n>=0&&(t=t.slice(n+1)),t=t.replace(/\s+/g,"").replace(/-/g,"+").replace(/_/g,"/");const r=t.length%4;return r!==0&&(t+="=".repeat(4-r)),t}function ua(e){return e.length>=4&&e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70}function Fr(e,t,n=or){const r=t.byteOffset%2===0?t:t.slice(),i=new Int16Array(r.buffer,r.byteOffset,r.byteLength/2),s=new Float32Array(i.length);for(let a=0;a<i.length;a++)s[a]=i[a]>=32768?-(65536-i[a])/32768:i[a]/32767;const o=e.createBuffer(1,s.length,n);return o.copyToChannel(s,0),o}function da(e){const t=la(e),n=window.atob(t),r=new Uint8Array(n.length);for(let i=0;i<n.length;i++)r[i]=n.charCodeAt(i);return r}async function Lr(e,t,n=or){const r=da(t),i=Wi(n);if(ua(r)){const s=r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength);return e.decodeAudioData(s)}if(r.length>=2&&r.length%2===0)return Fr(e,r,i);try{const s=r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength);return await e.decodeAudioData(s)}catch{return Fr(e,r,i)}}function zi(e,t){for(const n of e)if(t>=n.wtime&&t<n.wtime+n.wduration)return n;return null}function Hi(e,t){const n=[],r=[];let i=null,s=!1,o=!1,a=!1,u=!1,d=0,l=0,c=0,f=null,g=1,T=t,m=!1,I=null,L=0,O=!1,b=!1,E=0,V=null,D=null,oe=null;const pe=()=>({visemes:[],words:[],segmentOffsetMs:0}),P=()=>{b=!1,E=0},z=()=>{f!=null&&(clearTimeout(f),f=null)},Q=()=>{for(const v of r){try{v.onended=null,v.stop(0)}catch{}try{v.disconnect()}catch{}}r.length=0},le=()=>{for(const v of r)v.playbackRate.value=g},ce=()=>{if(!(m||!u||s||b||r.length>0||n.length>0)){if(a&&i&&l>0){const v=(i.currentTime-l)*1e3;if(v<L){z();const N=g>0?(L-v)/g:L-v;f=setTimeout(()=>{m||(o=!1,f=null,e.onSpeakingDone(),e.onNotify())},Math.max(0,N));return}}o=!1,z(),e.onSpeakingDone(),e.onNotify()}},re=v=>{const N=ft(v);if(N<=0){e.onSpeakingDone(),e.onNotify();return}o=!0,d=performance.now(),e.onSpeakingStart(),e.onNotify(),z();const U=g>0?N/g:N;f=setTimeout(()=>{m||(o=!1,f=null,e.onSpeakingDone(),e.onNotify())},U)},ue=(v,N,U,Y)=>{N.length!==0&&(c=U,N.forEach(G=>{const _=String(G.word??"").trim();if(!_)return;const y=Math.round((G.start??0)*1e3),S=G.duration!=null?Math.round(G.duration*1e3):G.end!=null?Math.max(1,Math.round((G.end-(G.start??0))*1e3)):0;v.push({word:_,wtime:c+y,wduration:S,queueIndex:v.length,gesture:G.gesture??null,emphasis:G.emphasis===!0})}))},we=(v,N,U,Y,G,Z)=>{U.length!==0&&(c=Y,U.forEach(_=>{const y=String(_.symbol??_.viseme??"").trim();if(!y)return;const S=Math.round((_.start??0)*1e3),K=Math.max(1,Math.round((_.duration??0)*1e3)),ee=c+S,x=zi(N,ee),J=x?Ii(x,ee):0;let ne;fi(Z)==="oculus_direct"?(ne=hn(y),ne==="U"&&x&&pn(x.word,J)&&(ne="FF")):ne=Ai(y,x?.word??"",J),v.push({viseme:ne,weight:1,vtime:ee,vduration:K})}),e.onNotify())},se=(v,N,U,Y,G,Z,_)=>{ue(N,Y,G),we(v,N,U,G,Z,_)},W=(v,N,U)=>{oe===v&&v.visemes===N||(N.length=0,U.length=0,v.visemes=N,v.words=U,v.segmentOffsetMs=0,oe=v,c=0,L=0)},M=async()=>{const v=window.AudioContext||window.webkitAudioContext;return i||(i=new v),i.state==="suspended"&&await i.resume(),I||(I=i.createMediaStreamDestination()),i},q=(v,N)=>{if(!i)return 0;const U=i,Y=U.currentTime,G=Y+.02;if(N.isFirstAudio){l<=0&&(l=G);const _=(G-Y)*1e3;d=performance.now()+_}N.onBeforeStart?.(G);const Z=U.createBufferSource();return Z.buffer=v,Z.playbackRate.value=g,Z.connect(U.destination),I&&Z.connect(I),r.push(Z),m?(r.pop(),G):(N.isFirstAudio&&(o=!0,e.onSpeakingStart()),Z.onended=()=>{const _=r.indexOf(Z);_>=0&&r.splice(_,1),e.onNotify(),!m&&(V&&D?R(V,D):ce())},Z.start(G),G)},R=async(v,N)=>{if(m||s||r.length>0)return;if(n.length===0){ce();return}s=!0;let U=!1;try{const Y=await M();if(m)return;const G=n[0];if(!G)return;const Z=!o;if(O&&(e.shouldHoldForWarmup?.()??!1)&&(b||Z&&G.newStream)||b){W(G.utterance,v,N);for(const S of n){if(m)return;S.audioBuffer||(S.audioBuffer=await Lr(Y,S.audio,S.sampleRate)),a=!0,b||(b=!0,E=0,S.newStream&&S.onNewStream?.()),S.visemesQueued||(se(v,N,S.visemes,S.words,E,!0,T),S.visemesQueued=!0,E+=Math.round(S.audioBuffer.duration*1e3),L=Math.max(L,ft(v)))}e.onNotify();return}const y=n.shift();if(!y||(y.audioBuffer||(y.audioBuffer=await Lr(Y,y.audio,y.sampleRate)),m))return;a=!0,z(),W(y.utterance,v,N),y.visemesQueued=!1,y.newStream&&y.onNewStream?.(),q(y.audioBuffer,{isFirstAudio:!o,isFirstOfStream:!1,onBeforeStart:S=>{l=S,c=0,d=performance.now()+(S-Y.currentTime)*1e3,v.length=0,N.length=0,se(v,N,y.visemes,y.words,0,!0,T),y.visemesQueued=!0,y.utterance.segmentOffsetMs=0}}),U=r.length>0,L=ft(v),e.onNotify()}catch(Y){console.error("[avatarChunkPlayback] audio chunk failed:",Y)}finally{if(s=!1,m)return;if(b){n.some(G=>!G.audioBuffer||!G.visemesQueued)&&R(v,N);return}!U&&r.length===0&&n.length>0?R(v,N):r.length===0&&n.length===0&&ce()}};return{setStreamTtsEngineId:v=>{T=v},setPlaybackSpeed:v=>{g=Math.max(.1,Math.min(1,v)),le()},getPlaybackSpeed:()=>g,setLipsyncWarmupHoldEnabled:v=>{O=v,v||P()},isWarmingUp:()=>b,releaseWarmupHold:()=>{if(b){b=!1,E=0;for(const v of n)v.newStream=!1;V&&D&&R(V,D),e.onNotify()}},resetForNewStream:v=>{m=!1,u=!1,T=v,L=0},resumeBotStream:()=>{m=!1,u=!1},clearAborted:()=>{m=!1},primeAudioContext:async()=>{const v=window.AudioContext||window.webkitAudioContext;i||(i=new v),i.state==="suspended"&&await i.resume(),i&&!I&&(I=i.createMediaStreamDestination())},stop:()=>{m=!0,z(),Q(),n.length=0,s=!1,o=!1,a=!1,u=!1,c=0,l=0,d=0,L=0,V=null,D=null,oe=null,P()},markStreamComplete:v=>{if(L=Math.max(L,ft(v)),u=!0,!a&&v.length>0){re(v);return}ce()},playAudioChunk:async(v,N,U,Y,G,Z,_)=>{if(m)return;V=v,D=N;const y=Wi(_?.sampleRate),S=_?.newStream===!0,K=pe();if(!U?.trim()){if(Y.length===0&&G.length===0)return;W(K,v,N),S&&_?.onNewStream?.(),se(v,N,Y,G,0,!0,T),L=ft(v),!o&&L>0&&re(v),e.onNotify();return}n.push({audio:U,audioBuffer:null,utterance:K,visemes:Y,words:G,isNewSegment:Z,sampleRate:y,newStream:S,onNewStream:_?.onNewStream,visemesQueued:!1}),await R(v,N)},queueTimingsOnly:(v,N,U,Y,G,Z)=>{if(m)return;const _=pe();W(_,v,N),se(v,N,U,Y,0,!0,T),_.segmentOffsetMs=0,L=ft(v)},getPlaybackElapsedMs:()=>a&&i&&l>0?Math.max(0,(i.currentTime-l)*1e3*g):o?Math.max(0,(performance.now()-d)*g):0,isSpeaking:()=>o?!0:!a||!i||l<=0?!1:(i.currentTime-l)*1e3<L,getVoiceStream:()=>I?I.stream:null,getPlaybackStartPerf:()=>d,bargeIn:()=>{m=!0,u=!1,z(),Q(),n.length=0,s=!1,o=!1,a=!1,c=0,l=0,d=0,L=0,V=null,D=null,oe=null,P()},interrupt:()=>{m=!0,u=!1,z(),Q(),n.length=0,s=!1,o=!1,a=!1,c=0,l=0,d=0,L=0,V=null,D=null,oe=null,P()}}}function zt(e,t){let n=null;for(const r of e)t<r.vtime||t>=r.vtime+r.vduration||(!n||r.vtime>=n.vtime)&&(n=r);return n?n.viseme:"sil"}function $i(e,t,n=120){const r=zt(e,t);if(r!=="sil")return r;let i=null;for(const s of e){if(s.vtime>t)break;const o=s.vtime+s.vduration;o<=t&&t-o<=n&&(i=s)}return i?.viseme??"sil"}function fa(e,t,n=400){const r=t-n;let i=0;for(const s of e)if(s.vtime+s.vduration<r)i+=1;else break;i>0&&e.splice(0,i)}function ma(e,t){for(const n of e)if(t>=n.wtime&&t<n.wtime+n.wduration)return n;return null}function Nr(e,t){const n=e.trim();if(!n)return null;try{const r=n.replace(/\s+/g,""),i=atob(r).length,s=Number.isFinite(t)&&(t??0)>0?Number(t):16e3;return i/2/s*1e3}catch{return null}}const yn=.5,vn=1.5,cr="Olivia",lr="f786b574-daa5-4673-aa0c-cbe3e8534c02",Ht=.85;function jn(e,t=Ct,n={}){const r=`${t.replace(/\/$/,"")}/avatar_ttsWithPoses`,i=[],s=[],o=[],a=[];let u=0;const d=[],l=[];let c="",f="neutral",g="",T=ht;const m=new Set;let I=0,L=null,O=0,b=null;const E=()=>m.forEach(W=>W());let V=()=>!1;const D=Hi({onSpeakingStart:()=>e("speaking"),onSpeakingDone:()=>e("done"),onNotify:E,shouldHoldForWarmup:()=>V()},ht),oe=()=>D.getPlaybackElapsedMs(),pe=(W,M=c)=>{if(!n.onLipsyncDebug)return;const q=oe(),R=i,H=wn(o).map(X=>({viseme:X.viseme,vtimeMs:X.vtime}));n.onLipsyncDebug({event:W,spokenText:M,mood:f,playbackAnchorPerf:D.getPlaybackStartPerf(),playbackElapsedMs:q,queueLength:R.length,activeViseme:zt(R,q),segments:l.map(X=>({...X,visemes:[...X.visemes]})),fullQueueLength:o.length,compactionSkipped:H})},P=(W,M)=>{n.onLipsyncDebug&&(L={event:W,spokenText:M},I===0&&(I=requestAnimationFrame(()=>{I=0;const q=L;L=null,q&&pe(q.event,q.spokenText)})))},z=(W,M,q,R,H)=>{if(!n.onLipsyncDebug)return;const X=i.slice(M),ae=q.filter(v=>String(v.symbol??v.viseme??"").trim());l.push({spokenText:W,audioOffsetMs:X[0]?.vtime??0,isNewSegment:R,chunkDurationMs:H,receivedAtMs:performance.now(),visemes:X.map((v,N)=>{const U=ae[N];return{rawSymbol:String(U?.symbol??U?.viseme??v.viseme).trim(),viseme:v.viseme,startSec:Number(U?.start??0),durationSec:Number(U?.duration??0),vtimeMs:v.vtime,vdurationMs:v.vduration}})})},Q=()=>{o.length=0,a.length=0,u=0},le=(W,M,q,R,H)=>{if(W.length===0&&M.length===0)return;Ui(o,a,W,M,u,H);const X=Vi(W,u),ae=q?Nr(q,R):null;u=Math.max(X,ae!=null?u+ae:u)},ce=()=>{O+=1,b?.abort(),b=null,D.stop(),i.length=0,s.length=0,Q(),d.length=0,f="neutral",l.length=0,c="",e("idle"),P("reset"),E()},re=W=>{const M=it(W?.ttsEngineId,W?.voiceId),q=M.ttsEngineId??T??ht,R=M.voiceId??(q===at?fn:cr),H=W?.speakingRate??Ht,X=Math.max(yn,Math.min(vn,H));return{engineId:q,voiceId:R,speakingRate:X,language:mn(W?.language)}},ue=async W=>{ce();const M=O;b=new AbortController,e("loading"),c=W.debugSpokenFallback,l.length=0,Q();const q=g.trim();if(!q)throw new Error("Developer token required for avatar_ttsWithPoses");D.resetForNewStream(W.engineId),D.setStreamTtsEngineId(W.engineId);let R="",H="";try{const X=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${q}`,Accept:"text/event-stream"},body:JSON.stringify(W.requestBody),signal:b.signal});if(!X.ok)throw new Error(`avatar_ttsWithPoses failed (${X.status})`);const ae=X.body;if(!ae)throw new Error("No response body");const v=ae.getReader(),N=new TextDecoder;let U="",Y=!0;const G=()=>R||W.debugSpokenFallback,Z=async _=>{if(M!==O)return;const y=Ei(_);if(y){if(y.event==="spokenText"){const S=typeof y.data.text=="string"?y.data.text.trim():"";S&&(R=S,c=S,W.onSpokenText?.(S),E());return}if(y.event==="displayText"){const S=typeof y.data.text=="string"?y.data.text.trim():"";S&&(H=S,W.onDisplayText?.(S),E());return}if(y.event!=="done"){if(y.event==="audio"){const S=y.data.chunk,K=y.data.visemes??[],ee=y.data.words??[],x=y.data.is_new_segment??!1,J=y.data.sample_rate,ne=Number(y.data.audio_offset_ms??0),te=i.length,Se=G();if(le(K,ee,S,J,W.engineId),S){const ut=Y;Y=!1,await D.playAudioChunk(i,s,S,K,ee,x,{sampleRate:J,newStream:ut,audioOffsetMs:ne}),z(Se,te,K,x,Nr(S,J)),P("audio_chunk",Se)}else(K.length>0||ee.length>0)&&(D.queueTimingsOnly(i,s,K,ee,x,{audioOffsetMs:ne,newStream:Y}),z(Se,te,K,x,null),P("audio_chunk",Se))}else if(y.event==="metadata"){const S=y.data.mood;typeof S=="string"&&S.trim()&&(f=S.trim());const K=y.data.spoken_text??y.data.text;typeof K=="string"&&K.trim()&&!R&&(R=K.trim(),c=R);const ee=y.data.sentence_emotions;if(Array.isArray(ee)){d.length=0;for(const x of ee){if(!x||typeof x!="object")continue;const J=x;d.push({sentence_index:Number(J.sentence_index??0),text:String(J.text??""),sentiment:typeof J.sentiment=="string"?J.sentiment:void 0,emotion:String(J.emotion??f),start_word:Number(J.start_word??0),end_word:Number(J.end_word??0)})}}E(),P("metadata",G())}else if(y.event==="error")throw new Error(String(y.data.message??"avatar_ttsWithPoses stream error"))}}};for(;;){if(M!==O)return{displayText:H,spokenText:R,aborted:!0};const{done:_,value:y}=await v.read();y&&(U+=N.decode(y,{stream:!0}));const S=U.split(`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("react/jsx-runtime"),m=require("react"),gs=require("@pipecat-ai/client-js"),Jn=require("@pipecat-ai/websocket-transport"),ve=require("pixi.js");var Dn=typeof document<"u"?document.currentScript:null;const ws="https://aitwin.bubu.social",di={publicBase:ws},ys="0.7.0",vs={version:ys},_s={},Dt="https://ai.aitwin.me",Zn=`${di.publicBase}/custom-faces`,bs=`${di.publicBase}/aitwin-workers/v${vs.version}/visemeDiffPreview.worker.js`;function Ss(e){if(e)return e;const t=typeof{url:typeof document>"u"?require("url").pathToFileURL(__filename).href:Dn&&Dn.tagName.toUpperCase()==="SCRIPT"&&Dn.src||new URL("aitwin.cjs",document.baseURI).href}<"u"?_s?.VITE_AITWIN_WORKER_URL:void 0;return t?.trim()?t.trim():bs}const Ts="https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin",Bn=3e3,Vt=16,Dr=3e3,ks=4e3,Es=.85,xs=8,er=400,tr="eng_a7f2b9c1",mn="eng_d4e8f3a2",at="eng_c9b1e6d4",ot="visemetts",ht=at,hn="eve",Wn=new Set([mn,at,ot]),nr={[tr]:"oculus_direct",[at]:"oculus_direct",[ot]:"oculus_direct",[mn]:"inworld_word"},Hn={google:tr,inworld:mn,cartesia:at,visemetts:ot};function Is(e){const t=e.trim();if(t)return t in nr?t:Hn[t.toLowerCase()]}function fi(e){const t=e?.trim();if(!t)return{};const n=t.indexOf(":");if(n<=0)return{voiceId:t};const r=Is(t.slice(0,n));if(!r)return{voiceId:t};const i=t.slice(n+1).trim();return{ttsEngineId:r,voiceId:i||void 0}}function st(e,t){const n=fi(t);return{ttsEngineId:n.ttsEngineId??e,voiceId:n.voiceId}}function mi(e){return nr[e]}function hi(e){return e===at}function pi(e){return e===ot}function As(e){if(e.ttsEngineId&&e.ttsEngineId in nr)return e.ttsEngineId;const t=e.tts?.toLowerCase();return t&&t in Hn?Hn[t]:ht}class an extends Error{constructor(t="AI twin not found"){super(t),this.name="AiTwinNotFoundError"}}function rr(e,...t){const n=typeof e=="string"?e:e?.brainId?.trim()||e?.knowledgeContextId?.trim();for(const r of[n,...t]){const i=r?.trim();if(i)return i}}function ir(e,...t){const n=typeof e=="string"?e:e?.brainUrl?.trim();for(const r of[n,...t]){const i=r?.trim();if(i)return i}}function Ms(e){const t=(e??"").trim();if(!t)return;let n;try{n=new URL(t)}catch{throw new Error("brainUrl must be an http or https URL")}if(n.protocol!=="http:"&&n.protocol!=="https:")throw new Error("brainUrl must be an http or https URL");if(!n.host)throw new Error("brainUrl must be an http or https URL");if(n.hash)throw new Error("brainUrl must not include a fragment");return t}async function gi(e,t=Ts){const n=new URL(t);n.searchParams.set("id",e);const r=await fetch(n.toString());if(r.status===404)throw new an;if(!r.ok)throw new Error(`getAiTwin failed (${r.status})`);const i=await r.json();if(!i.success)throw new an(i.error??"AI twin not found");const{tts:s,token:o,...a}=i.data,u=String(a.authToken??o??"").trim()||void 0,d=rr(a),l=ir(a);return{...a,ttsEngineId:As(i.data),authToken:u,brainId:d,knowledgeContextId:d,brainUrl:l}}const sr="thumbnail.webp";function wi(e,t=Zn){const n=e.trim();if(!n)throw new Error("faceId is required");return`${t.replace(/\/$/,"")}/${n}/${sr}`}function Br(e){const t=e?.trim();if(!t)return null;try{return wi(t)}catch{return null}}function Ps(e){if(!e)return null;const t=(e.binBase||e.twinBase).trim().replace(/\/$/,"");return t?`${t}/${sr}`:null}const Rs="https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken",Cs="client_6TNvp3SCs4Og0a1ijm9TommXLql1",Ds="kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1",Bs="6TNvp3SCs4Og0a1ijm9TommXLql1",Fs="Swaraj Mali";async function sn(){const e=await fetch(Rs,{method:"POST",headers:{"Content-Type":"application/json","Client-Id":Cs,"Client-Secret":Ds},body:JSON.stringify({userId:Bs,userName:Fs})});if(!e.ok)throw new Error(`getAuthToken failed (${e.status})`);const t=await e.json(),n=t.authToken??t.token??"";if(!n.trim())throw new Error("getAuthToken returned no token");return n}async function Ls(e,t=Dt){const n=await fetch(`${t.replace(/\/$/,"")}/api/session-value`,{headers:{Authorization:`Bearer ${e}`}});if(!n.ok)throw new Error(`/api/session-value failed (${n.status})`);const r=await n.json();if(!r.value)throw new Error("Session value response missing value");return r}function Ns(e){const t=e.trim();if(t.length%2!==0)throw new Error("Invalid hex key");const n=new Uint8Array(t.length/2);for(let r=0;r<t.length;r+=2)n[r/2]=Number.parseInt(t.slice(r,r+2),16);return n}function Zt(e){return Uint8Array.from(e)}function Os(e,t){const n=new Uint8Array(e.length+t.length);return n.set(e,0),n.set(t,e.length),n}async function Us(e,t){if(!t)throw new Error("Asset key is required for encrypted assets");if(e.byteLength<28)throw new Error("Encrypted payload too small");const n=new Uint8Array(e),r=Zt(n.subarray(0,12)),i=Zt(n.subarray(12,28)),s=Zt(n.subarray(28)),o=Zt(Os(s,i)),a=await crypto.subtle.importKey("raw",Ns(t),{name:"AES-GCM"},!1,["decrypt"]);return crypto.subtle.decrypt({name:"AES-GCM",iv:r,tagLength:128},a,o)}let $n=null;const zn=new Set;function en(e){return e.includes("/custom-faces/")}function on(e){const t=e.binBase.trim().replace(/\/$/,""),n=e.twinBase.trim().replace(/\/$/,"");if(!(e.encrypted||en(t)||en(n)))return{twinBase:n,binBase:t,encrypted:!1};const i=en(t)?t:en(n)?n:t;return{twinBase:i,binBase:i,encrypted:!0}}function wt(e){return on(e).encrypted}function jn(e,t=Zn){const n=`${t.replace(/\/$/,"")}/${e}`;return{twinBase:n,binBase:n,encrypted:!0}}function yi(e,t){return(e??t)?.trim()||void 0}function vi(e){$n=on(e);for(const t of zn)t()}function Vs(e,t){const n=typeof globalThis.location<"u"?globalThis.location.href:void 0;if(!n)return on(e);const r=i=>{try{return new URL(i,n).href.replace(/\/$/,"")}catch{return i}};return on({twinBase:r(e.twinBase),binBase:r(e.binBase),encrypted:e.encrypted})}function yt(){if(!$n)throw new Error("Twin assets not configured; call setActiveTwinAssets first");return $n}function _i(e){return zn.add(e),()=>zn.delete(e)}function cn(){const e=yt();return wt(e)?`${e.binBase}/sil.bin`:`${e.twinBase}/sil.png`}function Ws(){const e=yt();return wt(e)?`${e.binBase}/idle.bin`:`${e.twinBase}/idle.mp4`}function bi(){const e=yt();return wt(e)?e.binBase:e.twinBase}function zt(){return wt(yt())}function Si(){const e=yt();return wt(e)?`${e.binBase}/atlas.bin`:`${e.twinBase}/atlas.json`}function Ti(){const e=yt();return wt(e)?`${e.binBase}/expression_atlas.bin`:`${e.twinBase}/expression_atlas.json`}const pt=[{code:"en",label:"English",visemetts:!0,cartesia:!0},{code:"es",label:"Spanish",visemetts:!0,cartesia:!0},{code:"es-MX",label:"Spanish (Mexico)",visemetts:!0,cartesia:!0},{code:"es-ES",label:"Spanish (Spain)",visemetts:!0,cartesia:!0},{code:"pt",label:"Portuguese",visemetts:!0,cartesia:!0},{code:"pt-BR",label:"Portuguese (Brazil)",visemetts:!0,cartesia:!0},{code:"pt-PT",label:"Portuguese (Portugal)",visemetts:!0,cartesia:!0},{code:"fr",label:"French",visemetts:!0,cartesia:!0},{code:"de",label:"German",visemetts:!0,cartesia:!0},{code:"it",label:"Italian",visemetts:!0,cartesia:!0},{code:"zh",label:"Chinese",visemetts:!0,cartesia:!0},{code:"ja",label:"Japanese",visemetts:!0,cartesia:!0},{code:"hi",label:"Hindi",visemetts:!0,cartesia:!0},{code:"ko",label:"Korean",visemetts:!0,cartesia:!0},{code:"ru",label:"Russian",visemetts:!0,cartesia:!0},{code:"tr",label:"Turkish",visemetts:!0,cartesia:!0},{code:"id",label:"Indonesian",visemetts:!0,cartesia:!0},{code:"ar",label:"Arabic",visemetts:!0,cartesia:!0},{code:"ar-EG",label:"Arabic (Egypt)",visemetts:!0,cartesia:!0},{code:"ar-AE",label:"Arabic (UAE)",visemetts:!0,cartesia:!0},{code:"vi",label:"Vietnamese",visemetts:!0,cartesia:!0},{code:"bn",label:"Bengali",visemetts:!0,cartesia:!0},{code:"auto",label:"Auto-detect",visemetts:!0,cartesia:!1},{code:"nl",label:"Dutch",visemetts:!1,cartesia:!0},{code:"pl",label:"Polish",visemetts:!1,cartesia:!0},{code:"sv",label:"Swedish",visemetts:!1,cartesia:!0},{code:"tl",label:"Tagalog",visemetts:!1,cartesia:!0},{code:"bg",label:"Bulgarian",visemetts:!1,cartesia:!0},{code:"ro",label:"Romanian",visemetts:!1,cartesia:!0},{code:"cs",label:"Czech",visemetts:!1,cartesia:!0},{code:"el",label:"Greek",visemetts:!1,cartesia:!0},{code:"fi",label:"Finnish",visemetts:!1,cartesia:!0},{code:"hr",label:"Croatian",visemetts:!1,cartesia:!0},{code:"ms",label:"Malay",visemetts:!1,cartesia:!0},{code:"sk",label:"Slovak",visemetts:!1,cartesia:!0},{code:"da",label:"Danish",visemetts:!1,cartesia:!0},{code:"ta",label:"Tamil",visemetts:!1,cartesia:!0},{code:"uk",label:"Ukrainian",visemetts:!1,cartesia:!0},{code:"hu",label:"Hungarian",visemetts:!1,cartesia:!0},{code:"no",label:"Norwegian",visemetts:!1,cartesia:!0},{code:"th",label:"Thai",visemetts:!1,cartesia:!0},{code:"he",label:"Hebrew",visemetts:!1,cartesia:!0},{code:"ka",label:"Georgian",visemetts:!1,cartesia:!0},{code:"te",label:"Telugu",visemetts:!1,cartesia:!0},{code:"gu",label:"Gujarati",visemetts:!1,cartesia:!0},{code:"kn",label:"Kannada",visemetts:!1,cartesia:!0},{code:"ml",label:"Malayalam",visemetts:!1,cartesia:!0},{code:"mr",label:"Marathi",visemetts:!1,cartesia:!0},{code:"pa",label:"Punjabi",visemetts:!1,cartesia:!0}],ki=pt.map(e=>e.code),Hs=new Set(ki),$s=new Set(pt.filter(e=>e.visemetts).map(e=>e.code)),zs=new Set(pt.filter(e=>e.cartesia).map(e=>e.code)),js="en";function Ei(e){return Hs.has(e)}function pn(e){const t=e?.trim();if(t)return t}function Ks(e){return e===ot?pt.filter(t=>t.visemetts).map(t=>t.code):e===at?pt.filter(t=>t.cartesia).map(t=>t.code):pt.filter(t=>t.visemetts&&t.cartesia).map(t=>t.code)}function Gs(e,t){const n=t.trim();return n?e===ot?$s.has(n):e===at?zs.has(n):Ei(n):!1}function xi(e){const t=e.split(/\r?\n/);let n="",r="";for(const s of t)s.startsWith("event:")?n=s.slice(6).trim():s.startsWith("data:")&&(r=s.slice(5).trim());if(!n)return null;let i={};if(r)try{i=JSON.parse(r)}catch{i={raw:r}}return{event:n,data:i}}const Ii=["aa","CH","DD","E","FF","I","O","PP","RR","SS","TH","U","kk","nn","sil"],qs=new Map(Ii.map(e=>[e.toLowerCase(),e]));function gn(e){const t=(e??"").trim();return t?qs.get(t.toLowerCase())??"sil":"sil"}const Ys={aei:["aa","E","I"],o:["O","U"],ee:["I"],bmp:["PP"],fv:["FF"],l:["nn"],r:["RR"],th:["TH"],qw:["U","O"],cdgknstxyz:["DD","SS","kk","CH"]};function Qs(e){if(!e)return["sil"];const t=e.toLowerCase();return Ys[t]??["sil"]}function Ai(e,t){const n=e.word.length;if(n<=0)return 0;const r=e.wduration;if(r<=0)return 0;const i=Math.max(0,Math.min(1,(t-e.wtime)/r));return Math.min(n-1,Math.floor(i*n))}function Mt(e,t){const n=Math.max(0,Math.min(e.length-1,t));return e[n]??""}function wn(e,t){const n=e.toLowerCase().replace(/[^a-z']/g,"");return!n||Mt(n,t)!=="w"||t>0&&Mt(n,t-1)==="q"?!1:t===0&&/^w[aeiouy]/.test(n)}function Xs(e,t){const n=e.toLowerCase(),r=Mt(n,t),i=n.slice(Math.max(0,t-1),t+2);return r==="i"||r==="y"?"I":i.includes("ee")||r==="e"||n.includes("ea")?"E":"aa"}function Js(e,t){const n=e.toLowerCase(),r=n.slice(Math.max(0,t-1),Math.min(n.length,t+3));if(/oo|ou|uw/.test(r))return"U";if(wn(n,t))return"FF";const i=Mt(n,t);return i==="u"||i==="w"?"U":"O"}function Zs(e,t){const n=e.toLowerCase();if(wn(n,t))return"FF";const r=n.slice(Math.max(0,t-2),t+1);if(/qu/.test(r))return"U";const i=n.slice(Math.max(0,t-1),Math.min(n.length,t+3));return/wo|wh/.test(i)||Mt(n,t)==="o"?"O":"U"}const tn={d:"DD",t:"DD",n:"DD",s:"SS",z:"SS",k:"kk",g:"kk",c:"CH",x:"CH",j:"CH"};function ea(e,t){const n=e.toLowerCase(),r=Mt(n,t);if(tn[r])return tn[r];for(let i=0;i<n.length;i++){const s=n[i];if(tn[s])return tn[s]}return"DD"}function Mi(e,t,n){const r=Qs(e);if(r.length===1)return r[0];const i=t.trim();if(!i)return r[0];const s=(e??"").toLowerCase(),o=Math.max(0,n);switch(s){case"aei":return Xs(i,o);case"o":return Js(i,o);case"qw":return Zs(i,o);case"cdgknstxyz":return ea(i,o);default:return r[0]}}const ar=16,Kn=47,Pi=new Set(["aa","E","I"]),Ri=new Set(["DD","SS","kk","CH"]),ta=[Pi,Ri];function na(e){for(const t of ta)if(t.has(e))return t;return null}function ra(e){return na(e)!=null}function Ci(e,t=ar){return t<=0?e:Math.round(e/t)*t}function or(e,t=ar){return e.map(n=>({...n,vtime:Ci(n.vtime,t)}))}function Di(e){const t=new Map;for(const n of e)t.has(n.vtime)||t.set(n.vtime,n);return[...t.values()].sort((n,r)=>n.vtime-r.vtime)}function cr(e){if(e.length<=2)return[...e];const t=[e[0]];for(let n=1;n<e.length-1;n++){const r=e[n],i=e[n+1],s=t[t.length-1];if(!ra(r.viseme)){t.push(r);continue}const o=r.vtime-s.vtime,a=i.vtime-r.vtime;o<Kn||a<Kn||t.push(r)}return t.push(e[e.length-1]),t}function yn(e){return cr(Di(or(e)))}function Bi(e,t){let n="sil";for(const r of e){if(r.vtime>t)break;r.viseme!=="sil"&&(n=r.viseme)}return n}function vn(e){const t=Di(or(e)),n=cr(t),r=new Set(n.map(s=>s.vtime)),i=[];for(const s of t)r.has(s.vtime)||i.push({viseme:s.viseme,vtime:s.vtime});return i}function ia(e){return vn(e).map(t=>t.viseme)}const sa=32,Fr=48;function Fi(e){let t=0;for(const n of e)t=Math.max(t,n.vtime+n.vduration);return t}function Li(e){let t=null,n=-1;for(const r of e){if(r.viseme==="sil")continue;const i=r.vtime+r.vduration;i>n&&(t=r,n=i)}return t}function aa(e){return yn(e).find(n=>n.viseme!=="sil")??null}function ft(e){const t=Fi(e);return Li(e)?t+er:t}function oa(e){const t=[],n=aa(e);n&&n.vtime>0&&t.push({from:"sil",to:n.viseme,fromVtime:0,toVtime:n.vtime});const r=Li(e),i=Fi(e);return r&&t.push({from:r.viseme,to:"sil",fromVtime:i,toVtime:i+er}),t}function Ni(e){const t=yn(e),n=[];for(let r=0;r<t.length-1;r++){const i=t[r].viseme,s=t[r+1].viseme,o=t[r].vtime,a=t[r+1].vtime;i===s||a<=o||n.push({from:i,to:s,fromVtime:o,toVtime:a})}return n}function Oi(e){return e.length===0?[]:[...oa(e),...Ni(e)]}function ca(e,t,n=!1){let r=null;for(const i of e){const{transStart:s,transEnd:o}=Ui(i.fromVtime,i.toVtime,i.from,i.to,n);t>=s&&t<o&&(r=i)}return r}function Ui(e,t,n,r,i=!1){const s=Math.max(0,t-e);if(i&&(n==="PP"||r==="PP")&&s>Fr){const l=Fr,c=t,f=c-l;return{gapMs:s,effectiveGapMs:l,holdEnd:f,transStart:f,transEnd:c}}const a=s>0?Math.max(s,sa):0,u=e,d=e+a;return{gapMs:s,effectiveGapMs:a,holdEnd:e,transStart:u,transEnd:d}}function Vi(e,t,n,r,i,s){r.forEach(o=>{const u=String(o.word??"").trim();if(!u)return;const d=Math.round((o.start??0)*1e3),l=o.duration!=null?Math.round(o.duration*1e3):o.end!=null?Math.max(1,Math.round((o.end-(o.start??0))*1e3)):0;t.push({word:u,wtime:i+d,wduration:l,queueIndex:t.length,gesture:o.gesture??null,emphasis:o.emphasis===!0})}),n.forEach(o=>{const a=String(o.symbol??o.viseme??"").trim();if(!a)return;const u=Math.round((o.start??0)*1e3),d=Math.max(1,Math.round((o.duration??0)*1e3)),l=i+u,c=$i(t,l),f=c?Ai(c,l):0;let g;mi(s)==="oculus_direct"?(g=gn(a),g==="U"&&c&&wn(c.word,f)&&(g="FF")):g=Mi(a,c?.word??"",f),e.push({viseme:g,weight:1,vtime:l,vduration:d})})}function Wi(e,t){let n=t;for(const r of e){const i=Math.round((r.start??0)*1e3),s=Math.max(1,Math.round((r.duration??0)*1e3));n=Math.max(n,t+i+s)}return n}const lr=16e3;function Hi(e){const t=Number(e);return Number.isFinite(t)&&t>0?t:lr}function la(e){let t=e.trim();const n=t.indexOf(",");t.startsWith("data:")&&n>=0&&(t=t.slice(n+1)),t=t.replace(/\s+/g,"").replace(/-/g,"+").replace(/_/g,"/");const r=t.length%4;return r!==0&&(t+="=".repeat(4-r)),t}function ua(e){return e.length>=4&&e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70}function Lr(e,t,n=lr){const r=t.byteOffset%2===0?t:t.slice(),i=new Int16Array(r.buffer,r.byteOffset,r.byteLength/2),s=new Float32Array(i.length);for(let a=0;a<i.length;a++)s[a]=i[a]>=32768?-(65536-i[a])/32768:i[a]/32767;const o=e.createBuffer(1,s.length,n);return o.copyToChannel(s,0),o}function da(e){const t=la(e),n=window.atob(t),r=new Uint8Array(n.length);for(let i=0;i<n.length;i++)r[i]=n.charCodeAt(i);return r}async function Nr(e,t,n=lr){const r=da(t),i=Hi(n);if(ua(r)){const s=r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength);return e.decodeAudioData(s)}if(r.length>=2&&r.length%2===0)return Lr(e,r,i);try{const s=r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength);return await e.decodeAudioData(s)}catch{return Lr(e,r,i)}}function $i(e,t){for(const n of e)if(t>=n.wtime&&t<n.wtime+n.wduration)return n;return null}function zi(e,t){const n=[],r=[];let i=null,s=!1,o=!1,a=!1,u=!1,d=0,l=0,c=0,f=null,g=1,T=t,h=!1,I=null,F=0,N=!1,b=!1,k=0,V=null,D=null,se=null;const le=()=>({visemes:[],words:[],segmentOffsetMs:0}),P=()=>{b=!1,k=0},H=()=>{f!=null&&(clearTimeout(f),f=null)},Q=()=>{for(const v of r){try{v.onended=null,v.stop(0)}catch{}try{v.disconnect()}catch{}}r.length=0},oe=()=>{for(const v of r)v.playbackRate.value=g},ae=()=>{if(!(h||!u||s||b||r.length>0||n.length>0)){if(a&&i&&l>0){const v=(i.currentTime-l)*1e3;if(v<F){H();const U=g>0?(F-v)/g:F-v;f=setTimeout(()=>{h||(o=!1,f=null,e.onSpeakingDone(),e.onNotify())},Math.max(0,U));return}}o=!1,H(),e.onSpeakingDone(),e.onNotify()}},te=v=>{const U=ft(v);if(U<=0){e.onSpeakingDone(),e.onNotify();return}o=!0,d=performance.now(),e.onSpeakingStart(),e.onNotify(),H();const L=g>0?U/g:U;f=setTimeout(()=>{h||(o=!1,f=null,e.onSpeakingDone(),e.onNotify())},L)},ce=(v,U,L,Z)=>{U.length!==0&&(c=L,U.forEach($=>{const _=String($.word??"").trim();if(!_)return;const y=Math.round(($.start??0)*1e3),S=$.duration!=null?Math.round($.duration*1e3):$.end!=null?Math.max(1,Math.round(($.end-($.start??0))*1e3)):0;v.push({word:_,wtime:c+y,wduration:S,queueIndex:v.length,gesture:$.gesture??null,emphasis:$.emphasis===!0})}))},ye=(v,U,L,Z,$,J)=>{L.length!==0&&(c=Z,L.forEach(_=>{const y=String(_.symbol??_.viseme??"").trim();if(!y)return;const S=Math.round((_.start??0)*1e3),G=Math.max(1,Math.round((_.duration??0)*1e3)),ee=c+S,ue=$i(U,ee),me=ue?Ai(ue,ee):0;let x;mi(J)==="oculus_direct"?(x=gn(y),x==="U"&&ue&&wn(ue.word,me)&&(x="FF")):x=Mi(y,ue?.word??"",me),v.push({viseme:x,weight:1,vtime:ee,vduration:G})}),e.onNotify())},ne=(v,U,L,Z,$,J,_)=>{ce(U,Z,$),ye(v,U,L,$,J,_)},W=(v,U,L)=>{se===v&&v.visemes===U||(U.length=0,L.length=0,v.visemes=U,v.words=L,v.segmentOffsetMs=0,se=v,c=0,F=0)},M=async()=>{const v=window.AudioContext||window.webkitAudioContext;return i||(i=new v),i.state==="suspended"&&await i.resume(),I||(I=i.createMediaStreamDestination()),i},Y=(v,U)=>{if(!i)return 0;const L=i,Z=L.currentTime,$=Z+.02;if(U.isFirstAudio){l<=0&&(l=$);const _=($-Z)*1e3;d=performance.now()+_}U.onBeforeStart?.($);const J=L.createBufferSource();return J.buffer=v,J.playbackRate.value=g,J.connect(L.destination),I&&J.connect(I),r.push(J),h?(r.pop(),$):(U.isFirstAudio&&(o=!0,e.onSpeakingStart()),J.onended=()=>{const _=r.indexOf(J);_>=0&&r.splice(_,1),e.onNotify(),!h&&(V&&D?R(V,D):ae())},J.start($),$)},R=async(v,U)=>{if(h||s||r.length>0)return;if(n.length===0){ae();return}s=!0;let L=!1;try{const Z=await M();if(h)return;const $=n[0];if(!$)return;const J=!o;if(N&&(e.shouldHoldForWarmup?.()??!1)&&(b||J&&$.newStream)||b){W($.utterance,v,U);for(const S of n){if(h)return;S.audioBuffer||(S.audioBuffer=await Nr(Z,S.audio,S.sampleRate)),a=!0,b||(b=!0,k=0,S.newStream&&S.onNewStream?.()),S.visemesQueued||(ne(v,U,S.visemes,S.words,k,!0,T),S.visemesQueued=!0,k+=Math.round(S.audioBuffer.duration*1e3),F=Math.max(F,ft(v)))}e.onNotify();return}const y=n.shift();if(!y||(y.audioBuffer||(y.audioBuffer=await Nr(Z,y.audio,y.sampleRate)),h))return;a=!0,H(),W(y.utterance,v,U),y.visemesQueued=!1,y.newStream&&y.onNewStream?.(),Y(y.audioBuffer,{isFirstAudio:!o,isFirstOfStream:!1,onBeforeStart:S=>{l=S,c=0,d=performance.now()+(S-Z.currentTime)*1e3,v.length=0,U.length=0,ne(v,U,y.visemes,y.words,0,!0,T),y.visemesQueued=!0,y.utterance.segmentOffsetMs=0}}),L=r.length>0,F=ft(v),e.onNotify()}catch(Z){console.error("[avatarChunkPlayback] audio chunk failed:",Z)}finally{if(s=!1,h)return;if(b){n.some($=>!$.audioBuffer||!$.visemesQueued)&&R(v,U);return}!L&&r.length===0&&n.length>0?R(v,U):r.length===0&&n.length===0&&ae()}};return{setStreamTtsEngineId:v=>{T=v},setPlaybackSpeed:v=>{g=Math.max(.1,Math.min(1,v)),oe()},getPlaybackSpeed:()=>g,setLipsyncWarmupHoldEnabled:v=>{N=v,v||P()},isWarmingUp:()=>b,releaseWarmupHold:()=>{if(b){b=!1,k=0;for(const v of n)v.newStream=!1;V&&D&&R(V,D),e.onNotify()}},resetForNewStream:v=>{h=!1,u=!1,T=v,F=0},resumeBotStream:()=>{h=!1,u=!1},clearAborted:()=>{h=!1},primeAudioContext:async()=>{const v=window.AudioContext||window.webkitAudioContext;i||(i=new v),i.state==="suspended"&&await i.resume(),i&&!I&&(I=i.createMediaStreamDestination())},stop:()=>{h=!0,H(),Q(),n.length=0,s=!1,o=!1,a=!1,u=!1,c=0,l=0,d=0,F=0,V=null,D=null,se=null,P()},markStreamComplete:v=>{if(F=Math.max(F,ft(v)),u=!0,!a&&v.length>0){te(v);return}ae()},playAudioChunk:async(v,U,L,Z,$,J,_)=>{if(h)return;V=v,D=U;const y=Hi(_?.sampleRate),S=_?.newStream===!0,G=le();if(!L?.trim()){if(Z.length===0&&$.length===0)return;W(G,v,U),S&&_?.onNewStream?.(),ne(v,U,Z,$,0,!0,T),F=ft(v),!o&&F>0&&te(v),e.onNotify();return}n.push({audio:L,audioBuffer:null,utterance:G,visemes:Z,words:$,isNewSegment:J,sampleRate:y,newStream:S,onNewStream:_?.onNewStream,visemesQueued:!1}),await R(v,U)},queueTimingsOnly:(v,U,L,Z,$,J)=>{if(h)return;const _=le();W(_,v,U),ne(v,U,L,Z,0,!0,T),_.segmentOffsetMs=0,F=ft(v)},getPlaybackElapsedMs:()=>a&&i&&l>0?Math.max(0,(i.currentTime-l)*1e3*g):o?Math.max(0,(performance.now()-d)*g):0,isSpeaking:()=>o?!0:!a||!i||l<=0?!1:(i.currentTime-l)*1e3<F,getVoiceStream:()=>I?I.stream:null,getPlaybackStartPerf:()=>d,bargeIn:()=>{h=!0,u=!1,H(),Q(),n.length=0,s=!1,o=!1,a=!1,c=0,l=0,d=0,F=0,V=null,D=null,se=null,P()},interrupt:()=>{h=!0,u=!1,H(),Q(),n.length=0,s=!1,o=!1,a=!1,c=0,l=0,d=0,F=0,V=null,D=null,se=null,P()}}}function jt(e,t){let n=null;for(const r of e)t<r.vtime||t>=r.vtime+r.vduration||(!n||r.vtime>=n.vtime)&&(n=r);return n?n.viseme:"sil"}function ji(e,t,n=120){const r=jt(e,t);if(r!=="sil")return r;let i=null;for(const s of e){if(s.vtime>t)break;const o=s.vtime+s.vduration;o<=t&&t-o<=n&&(i=s)}return i?.viseme??"sil"}function fa(e,t,n=400){const r=t-n;let i=0;for(const s of e)if(s.vtime+s.vduration<r)i+=1;else break;i>0&&e.splice(0,i)}function ma(e,t){for(const n of e)if(t>=n.wtime&&t<n.wtime+n.wduration)return n;return null}function Or(e,t){const n=e.trim();if(!n)return null;try{const r=n.replace(/\s+/g,""),i=atob(r).length,s=Number.isFinite(t)&&(t??0)>0?Number(t):16e3;return i/2/s*1e3}catch{return null}}const _n=.5,bn=1.5,ur="Olivia",dr="f786b574-daa5-4673-aa0c-cbe3e8534c02",Kt=.85;function Gn(e,t=Dt,n={}){const r=`${t.replace(/\/$/,"")}/avatar_ttsWithPoses`,i=[],s=[],o=[],a=[];let u=0;const d=[],l=[];let c="",f="neutral",g="",T=ht;const h=new Set;let I=0,F=null,N=0,b=null;const k=()=>h.forEach(W=>W());let V=()=>!1;const D=zi({onSpeakingStart:()=>e("speaking"),onSpeakingDone:()=>e("done"),onNotify:k,shouldHoldForWarmup:()=>V()},ht),se=()=>D.getPlaybackElapsedMs(),le=(W,M=c)=>{if(!n.onLipsyncDebug)return;const Y=se(),R=i,z=vn(o).map(X=>({viseme:X.viseme,vtimeMs:X.vtime}));n.onLipsyncDebug({event:W,spokenText:M,mood:f,playbackAnchorPerf:D.getPlaybackStartPerf(),playbackElapsedMs:Y,queueLength:R.length,activeViseme:jt(R,Y),segments:l.map(X=>({...X,visemes:[...X.visemes]})),fullQueueLength:o.length,compactionSkipped:z})},P=(W,M)=>{n.onLipsyncDebug&&(F={event:W,spokenText:M},I===0&&(I=requestAnimationFrame(()=>{I=0;const Y=F;F=null,Y&&le(Y.event,Y.spokenText)})))},H=(W,M,Y,R,z)=>{if(!n.onLipsyncDebug)return;const X=i.slice(M),ie=Y.filter(v=>String(v.symbol??v.viseme??"").trim());l.push({spokenText:W,audioOffsetMs:X[0]?.vtime??0,isNewSegment:R,chunkDurationMs:z,receivedAtMs:performance.now(),visemes:X.map((v,U)=>{const L=ie[U];return{rawSymbol:String(L?.symbol??L?.viseme??v.viseme).trim(),viseme:v.viseme,startSec:Number(L?.start??0),durationSec:Number(L?.duration??0),vtimeMs:v.vtime,vdurationMs:v.vduration}})})},Q=()=>{o.length=0,a.length=0,u=0},oe=(W,M,Y,R,z)=>{if(W.length===0&&M.length===0)return;Vi(o,a,W,M,u,z);const X=Wi(W,u),ie=Y?Or(Y,R):null;u=Math.max(X,ie!=null?u+ie:u)},ae=()=>{N+=1,b?.abort(),b=null,D.stop(),i.length=0,s.length=0,Q(),d.length=0,f="neutral",l.length=0,c="",e("idle"),P("reset"),k()},te=W=>{const M=st(W?.ttsEngineId,W?.voiceId),Y=M.ttsEngineId??T??ht,R=M.voiceId??(Y===ot?hn:ur),z=W?.speakingRate??Kt,X=Math.max(_n,Math.min(bn,z));return{engineId:Y,voiceId:R,speakingRate:X,language:pn(W?.language)}},ce=async W=>{ae();const M=N;b=new AbortController,e("loading"),c=W.debugSpokenFallback,l.length=0,Q();const Y=g.trim();if(!Y)throw new Error("Developer token required for avatar_ttsWithPoses");D.resetForNewStream(W.engineId),D.setStreamTtsEngineId(W.engineId);let R="",z="";try{const X=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${Y}`,Accept:"text/event-stream"},body:JSON.stringify(W.requestBody),signal:b.signal});if(!X.ok)throw new Error(`avatar_ttsWithPoses failed (${X.status})`);const ie=X.body;if(!ie)throw new Error("No response body");const v=ie.getReader(),U=new TextDecoder;let L="",Z=!0;const $=()=>R||W.debugSpokenFallback,J=async _=>{if(M!==N)return;const y=xi(_);if(y){if(y.event==="spokenText"){const S=typeof y.data.text=="string"?y.data.text.trim():"";S&&(R=S,c=S,W.onSpokenText?.(S),k());return}if(y.event==="displayText"){const S=typeof y.data.text=="string"?y.data.text.trim():"";S&&(z=S,W.onDisplayText?.(S),k());return}if(y.event!=="done"){if(y.event==="audio"){const S=y.data.chunk,G=y.data.visemes??[],ee=y.data.words??[],ue=y.data.is_new_segment??!1,me=y.data.sample_rate,x=Number(y.data.audio_offset_ms??0),q=i.length,ge=$();if(oe(G,ee,S,me,W.engineId),S){const Ne=Z;Z=!1,await D.playAudioChunk(i,s,S,G,ee,ue,{sampleRate:me,newStream:Ne,audioOffsetMs:x}),H(ge,q,G,ue,Or(S,me)),P("audio_chunk",ge)}else(G.length>0||ee.length>0)&&(D.queueTimingsOnly(i,s,G,ee,ue,{audioOffsetMs:x,newStream:Z}),H(ge,q,G,ue,null),P("audio_chunk",ge))}else if(y.event==="metadata"){const S=y.data.mood;typeof S=="string"&&S.trim()&&(f=S.trim());const G=y.data.spoken_text??y.data.text;typeof G=="string"&&G.trim()&&!R&&(R=G.trim(),c=R);const ee=y.data.sentence_emotions;if(Array.isArray(ee)){d.length=0;for(const ue of ee){if(!ue||typeof ue!="object")continue;const me=ue;d.push({sentence_index:Number(me.sentence_index??0),text:String(me.text??""),sentiment:typeof me.sentiment=="string"?me.sentiment:void 0,emotion:String(me.emotion??f),start_word:Number(me.start_word??0),end_word:Number(me.end_word??0)})}}k(),P("metadata",$())}else if(y.event==="error")throw new Error(String(y.data.message??"avatar_ttsWithPoses stream error"))}}};for(;;){if(M!==N)return{displayText:z,spokenText:R,aborted:!0};const{done:_,value:y}=await v.read();y&&(L+=U.decode(y,{stream:!0}));const S=L.split(`
2
2
 
3
- `);U=S.pop()??"";for(const K of S)await Z(K);if(_){U.trim()&&await Z(U.trim()),D.markStreamComplete(i),P("bot_stopped",G());break}}}catch(X){if(M!==O)return{displayText:H,spokenText:R,aborted:!0};if(X instanceof DOMException&&X.name==="AbortError")return{displayText:H,spokenText:R,aborted:!0};throw console.error("[avatarTtsLipsync]",X),ce(),e("error"),X}return{displayText:H,spokenText:R}};return{setDeveloperToken:W=>{g=W},setTtsEngineId:W=>{T=W},getTtsEngineId:()=>T,speak:async(W,M)=>{const{engineId:q,voiceId:R,speakingRate:H,language:X}=re(M),ae={user_query:W,ttsEngineId:q,speakingRate:H,tenant:"aiTwin"};Un.has(q)&&(ae.voice_id=R),X&&(ae.language=X),await ue({requestBody:ae,engineId:q,debugSpokenFallback:W})},speakWithReply:async(W,M)=>{const q=W.trim();if(!q)throw new Error("Message is required");const{engineId:R,voiceId:H,speakingRate:X,language:ae}=re(M),v={generateReply:!0,message:q,ttsEngineId:R,speakingRate:X,tenant:"aiTwin",history:M?.history??[]};Un.has(R)&&(v.voice_id=H),ae&&(v.language=ae);const N=M?.brainId?.trim();N&&(v.brainId=N);const U=M?.brainUrl?.trim();U&&(v.brainUrl=U);const Y=await ue({requestBody:v,engineId:R,debugSpokenFallback:q,onSpokenText:M?.onSpokenText,onDisplayText:M?.onDisplayText});if(Y.aborted)return{spokenText:Y.spokenText,displayText:Y.displayText};if(!Y.spokenText&&!Y.displayText)throw new Error("avatar_ttsWithPoses generateReply stream missing spokenText or displayText");return{spokenText:Y.spokenText||Y.displayText,displayText:Y.displayText||Y.spokenText}},stop:ce,getVisemeQueue:()=>i,getWordQueue:()=>s,getFullVisemeQueue:()=>o,getFullWordQueue:()=>a,getSentenceEmotions:()=>d,getStreamMood:()=>f,getPlaybackElapsedMs:()=>D.getPlaybackElapsedMs(),isSpeaking:()=>D.isSpeaking(),isWarmingUp:()=>D.isWarmingUp(),releaseWarmupHold:()=>D.releaseWarmupHold(),setLipsyncWarmupHold:(W,M)=>{M&&(V=M),D.setLipsyncWarmupHoldEnabled(W)},getVoiceStream:()=>D.getVoiceStream(),setPlaybackSpeed:W=>{D.setPlaybackSpeed(W)},getPlaybackSpeed:()=>D.getPlaybackSpeed(),subscribe:W=>(m.add(W),()=>m.delete(W))}}const ur=16e3,dr=16e3;class ha extends Qn.ProtobufFrameSerializer{async deserialize(t){try{return await super.deserialize(t)}catch(n){if(n instanceof Error&&n.message==="Unknown frame kind")return{type:"raw",message:null};throw n}}}class pa extends Qn.DailyMediaManager{constructor(){super(!0,!0,void 0,void 0,512,ur,dr)}bufferBotAudio(t,n){}}const ga=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Or(e){return ga.test(e.trim())}function wa(e){const t=e.replace(/\/$/,"");return t.startsWith("https://")?`wss://${t.slice(8)}`:t.startsWith("http://")?`ws://${t.slice(7)}`:t}function ji(e){if(e.wsUrl?.trim())return e.wsUrl.trim();const t=e.authToken?.trim();if(!t)throw new Error("Voice connect requires authToken (same JWT as SSE Authorization Bearer).");const n=e.apiBase??Ct,r=new URL(`${wa(n)}/ws/voice`);r.searchParams.set("authToken",t),r.searchParams.set("tenant","aiTwin");const i=it(e.ttsEngineId,e.voiceId),s=i.ttsEngineId;s&&r.searchParams.set("ttsEngineId",s);const o=i.voiceId?.trim();o&&(s&&hi(s)?Or(o)||r.searchParams.set("voiceId",o):Or(o)&&r.searchParams.set("voiceId",o));const a=mn(e.language);a&&r.searchParams.set("language",a);const u=e.brainId?.trim()||e.personaId?.trim();u&&r.searchParams.set("brainId",u);const d=e.brainUrl?.trim();d&&r.searchParams.set("brainUrl",d);const l=e.speakingRate??Ht;r.searchParams.set("speaking_rate",String(Math.max(yn,Math.min(vn,l)))),e.ttsStream===!1?r.searchParams.set("tts_stream","false"):r.searchParams.set("tts_stream","true"),e.speakOnlyWhenSpokenTo&&r.searchParams.set("speakOnlyWhenSpokenTo","true");const c=e.avatarName?.trim();return c&&r.searchParams.set("avatarName",c),r.toString()}function Dn(e,t){if(!Array.isArray(e))return[];const n=[];for(const r of e)!r||typeof r!="object"||n.push({sentence_index:Number(r.sentence_index??0),text:String(r.text??""),sentiment:typeof r.sentiment=="string"?r.sentiment:void 0,emotion:String(r.emotion??t),start_word:Number(r.start_word??0),end_word:Number(r.end_word??0)});return n}function ya(e){if(!e||typeof e!="object")return null;const t=e,r=(t.data&&typeof t.data=="object"&&!Array.isArray(t.data)?t.data:null)||t;return{...r,type:String(r.type??t.type??"server-message")}}function Ur(e){return e.is_new_segment===!0||Number(e.audio_offset_ms??-1)===0?!0:Number(e.chunk_index??-1)===0}function va(e){return e?.transport?._mediaManager?._wavStreamPlayer??null}function Ki(e,t={}){const n=[],r=[],i=[],s=[];let o=0;const a=[],u=[];let d="",l="neutral",c=st,f=!1,g=null,T=null,m={...t},I=0,L=null;const O=new Set,b=[];let E=!1,V=null,D=!0,oe="default";const pe=new Set;let P=null,z=0,Q=!1,le=!1,ce=null,re=!1;const ue=()=>{ce!=null&&(clearTimeout(ce),ce=null)},we=()=>{ue(),ce=setTimeout(()=>{ce=null,D&&(M.markStreamComplete(n),ae("bot_stopped"))},450)},se=()=>O.forEach(p=>p());let W=()=>!1;const M=Hi({onSpeakingStart:()=>e("speaking"),onSpeakingDone:()=>{re=!1,le=!1,e("done")},onNotify:se,shouldHoldForWarmup:()=>W()},c),q=()=>{if(V)return V;const p=new MessageChannel;return p.port1.onmessage=()=>{const B=b.shift();B!==void 0&&te(B),b.length>0?p.port2.postMessage(null):E=!1},V=p,p},R=p=>{b.push(p),!E&&(E=!0,q().port2.postMessage(null))},H=()=>M.getPlaybackElapsedMs(),X=(p,B=d)=>{if(!m.onLipsyncDebug)return;const ie=H(),de=n,Me=wn(i).map(Ee=>({viseme:Ee.viseme,vtimeMs:Ee.vtime}));m.onLipsyncDebug({event:p,spokenText:B,mood:l,playbackAnchorPerf:M.getPlaybackStartPerf(),playbackElapsedMs:ie,queueLength:de.length,activeViseme:zt(de,ie),segments:u.map(Ee=>({...Ee,visemes:[...Ee.visemes]})),fullQueueLength:i.length,compactionSkipped:Me})},ae=(p,B)=>{m.onLipsyncDebug&&(L={event:p,spokenText:B},I===0&&(I=requestAnimationFrame(()=>{I=0;const ie=L;L=null,ie&&X(ie.event,ie.spokenText)})))},v=()=>{D=!0,pe.clear(),M.clearAborted()},N=()=>{const p=va(g);if(p){oe=`bot-${Date.now()}`,p.interruptedTrackIds={[oe]:!0};try{p.interrupt?.()}catch{}}const B=T;try{B?.interrupt?.()}catch{}},U=p=>{if(re&&M.isSpeaking())return!0;const B=p.context_id?.trim();return B&&pe.has(B)?!0:D?!1:!(p.type==="avatar_metadata"||Ur(p))},Y=p=>{D=!1,re=!1;const B=p.context_id?.trim();B&&pe.add(B),ue(),Q=!1,le=!1,b.length=0,E=!1,P?.abort(),P=null,z+=1,N(),M.bargeIn(),G(),e("idle"),ae("interrupt"),se()},G=()=>{n.length=0,r.length=0,i.length=0,s.length=0,o=0,u.length=0,d="",l="neutral",a.length=0},Z=()=>{ue(),le=!1,Q=!1,M.stop(),G(),ae("reset"),se()},_=()=>{Z(),e("idle"),se()},y=(p,B)=>{typeof p.mood=="string"&&p.mood.trim()&&(l=p.mood.trim()),Array.isArray(p.sentence_emotions)&&p.sentence_emotions.length>0&&(B&&(a.length=0),a.push(...Dn(p.sentence_emotions,l)))},S=p=>{if(U({...p,type:"avatar_metadata"}))return;v(),Q=!0;const B=String(p.spoken_text??p.text??"").trim();B&&(d=B),M.resetForNewStream(c),typeof p.mood=="string"&&p.mood.trim()&&(l=p.mood.trim()),a.length=0,a.push(...Dn(p.sentence_emotions,l)),Array.isArray(p.gestures)&&p.gestures,ae("metadata",B),se()},K=(p,B,ie,de)=>{if(!m.onLipsyncDebug)return;const Me=n.slice(ie),Ee=de.filter(Pe=>String(Pe.symbol??Pe.viseme??"").trim());u.push({spokenText:B,audioOffsetMs:Me[0]?.vtime??0,isNewSegment:!!p.is_new_segment,chunkDurationMs:p.chunk_duration_ms!=null?Number(p.chunk_duration_ms):null,receivedAtMs:performance.now(),visemes:Me.map((Pe,ze)=>{const Oe=Ee[ze];return{rawSymbol:String(Oe?.symbol??Oe?.viseme??Pe.viseme).trim(),viseme:Pe.viseme,startSec:Number(Oe?.start??0),durationSec:Number(Oe?.duration??0),vtimeMs:Pe.vtime,vdurationMs:Pe.vduration}})})},ee=(p,B,ie,de)=>{if(p.length===0&&B.length===0)return;Ui(i,s,p,B,o,c);const Me=Vi(p,o);let Ee=null;const Pe=ie.trim();if(Pe)try{const ze=atob(Pe.replace(/\s+/g,"")).length,Oe=Number.isFinite(de)&&(de??0)>0?Number(de):16e3;Ee=ze/2/Oe*1e3}catch{Ee=null}o=Math.max(Me,Ee!=null?o+Ee:o)},x=p=>{if(U(p))return;const B=Ur(p);B&&(ue(),v());const ie=String(p.spoken_text??p.text??"").trim(),de=p.visemes??[],Me=p.words??[],Ee=String(p.chunk??"").trim(),Pe=Q||B||!le;Q&&(Q=!1),Pe&&(le=!0),ie&&(d=ie),y(p,B),Pe?(M.resetForNewStream(c),i.length=0,s.length=0,o=0):B&&M.resumeBotStream(),ee(de,Me,Ee,p.sample_rate);const ze=n.length;M.playAudioChunk(n,r,Ee,de,Me,B,{sampleRate:p.sample_rate,newStream:Pe,audioOffsetMs:Number(p.audio_offset_ms??0),onNewStream:()=>{u.length=0}}).then(()=>{K(p,ie,ze,de),ae("audio_chunk",ie),we()})},J=p=>{if(U(p))return;(p.is_new_segment||Number(p.audio_offset_ms??-1)===0)&&v(),console.warn("[realtimeVoice] avatar_lipsync is deprecated — expect avatar_audio_chunk");const B=String(p.spoken_text??p.text??"").trim();B&&(d=B),y(p,!!p.is_new_segment),ee(p.visemes??[],p.words??[],"",void 0);const ie=n.length;M.queueTimingsOnly(n,r,p.visemes??[],p.words??[],!!p.is_new_segment,{audioOffsetMs:Number(p.audio_offset_ms??0)}),K(p,B,ie,p.visemes??[]),ae("lipsync",B),se()},ne=async p=>{const B=String(p.text??"").trim();if(!B)return;const ie=m.authToken?.trim();if(!ie){console.error("[realtimeVoice] avatar_speak requires authToken");return}P?.abort();const de=new AbortController;P=de;const Me=++z;v(),M.resetForNewStream(c),d=B;const Pe=`${(m.apiBase??Ct).replace(/\/$/,"")}/avatar_ttsWithPoses`,ze=it(c,m.voiceId);ze.ttsEngineId&&(c=ze.ttsEngineId,M.setStreamTtsEngineId(c));const Oe=ze.voiceId?.trim()||(c===at?fn:lr),jt=m.speakingRate??Ht,Sn=Math.max(yn,Math.min(vn,jt)),Bt={user_query:B,ttsEngineId:c,speakingRate:Sn,tenant:"aiTwin"};Un.has(c)&&(Bt.voice_id=Oe);const _t=mn(m.language);_t&&(Bt.language=_t);try{const Ne=await fetch(Pe,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${ie}`},body:JSON.stringify(Bt),signal:de.signal});if(!Ne.ok)throw new Error(`avatar_ttsWithPoses failed (${Ne.status})`);const Ge=Ne.body;if(!Ge)throw new Error("No response body");const bt=Ge.getReader(),xe=new TextDecoder;let Re="",qe=!0;const Ue=async Ve=>{if(Me!==z||de.signal.aborted||!D)return;const Ie=Ei(Ve);if(Ie){if(Ie.event==="audio"){const De=Ie.data.chunk,He=Ie.data.visemes??[],et=Ie.data.words??[],$e=Ie.data.is_new_segment??!1,We=Ie.data.sample_rate,St=Number(Ie.data.audio_offset_ms??0);if(De){const Tt=qe;qe=!1,await M.playAudioChunk(n,r,De,He,et,$e,{sampleRate:We,newStream:Tt,audioOffsetMs:St})}else(He.length>0||et.length>0)&&M.queueTimingsOnly(n,r,He,et,$e,{audioOffsetMs:St,newStream:qe})}else if(Ie.event==="metadata"){const De=Ie.data.mood;typeof De=="string"&&De.trim()&&(l=De.trim());const He=Ie.data.sentence_emotions;Array.isArray(He)&&(a.length=0,a.push(...Dn(He,l))),se()}else if(Ie.event==="error")throw new Error(String(Ie.data.message??"avatar_ttsWithPoses stream error"))}};for(;!(Me!==z||de.signal.aborted||!D);){const{done:Ve,value:Ie}=await bt.read();Ie&&(Re+=xe.decode(Ie,{stream:!0}));const De=Re.split(`
3
+ `);L=S.pop()??"";for(const G of S)await J(G);if(_){L.trim()&&await J(L.trim()),D.markStreamComplete(i),P("bot_stopped",$());break}}}catch(X){if(M!==N)return{displayText:z,spokenText:R,aborted:!0};if(X instanceof DOMException&&X.name==="AbortError")return{displayText:z,spokenText:R,aborted:!0};throw console.error("[avatarTtsLipsync]",X),ae(),e("error"),X}return{displayText:z,spokenText:R}};return{setDeveloperToken:W=>{g=W},setTtsEngineId:W=>{T=W},getTtsEngineId:()=>T,speak:async(W,M)=>{const{engineId:Y,voiceId:R,speakingRate:z,language:X}=te(M),ie={user_query:W,ttsEngineId:Y,speakingRate:z,tenant:"aiTwin"};Wn.has(Y)&&(ie.voice_id=R),X&&(ie.language=X),await ce({requestBody:ie,engineId:Y,debugSpokenFallback:W})},speakWithReply:async(W,M)=>{const Y=W.trim();if(!Y)throw new Error("Message is required");const{engineId:R,voiceId:z,speakingRate:X,language:ie}=te(M),v={generateReply:!0,message:Y,ttsEngineId:R,speakingRate:X,tenant:"aiTwin",history:M?.history??[]};Wn.has(R)&&(v.voice_id=z),ie&&(v.language=ie);const U=M?.brainId?.trim();U&&(v.brainId=U);const L=M?.brainUrl?.trim();L&&(v.brainUrl=L);const Z=await ce({requestBody:v,engineId:R,debugSpokenFallback:Y,onSpokenText:M?.onSpokenText,onDisplayText:M?.onDisplayText});if(Z.aborted)return{spokenText:Z.spokenText,displayText:Z.displayText};if(!Z.spokenText&&!Z.displayText)throw new Error("avatar_ttsWithPoses generateReply stream missing spokenText or displayText");return{spokenText:Z.spokenText||Z.displayText,displayText:Z.displayText||Z.spokenText}},stop:ae,getVisemeQueue:()=>i,getWordQueue:()=>s,getFullVisemeQueue:()=>o,getFullWordQueue:()=>a,getSentenceEmotions:()=>d,getStreamMood:()=>f,getPlaybackElapsedMs:()=>D.getPlaybackElapsedMs(),isSpeaking:()=>D.isSpeaking(),isWarmingUp:()=>D.isWarmingUp(),releaseWarmupHold:()=>D.releaseWarmupHold(),setLipsyncWarmupHold:(W,M)=>{M&&(V=M),D.setLipsyncWarmupHoldEnabled(W)},getVoiceStream:()=>D.getVoiceStream(),setPlaybackSpeed:W=>{D.setPlaybackSpeed(W)},getPlaybackSpeed:()=>D.getPlaybackSpeed(),subscribe:W=>(h.add(W),()=>h.delete(W))}}function Wt(e){if(e==null)return!1;if(typeof e=="object"){const r=e;if(r.type==="permissions"||r.name==="NotAllowedError"||r.name==="PermissionDeniedError")return!0}const n=(e instanceof Error?`${e.name} ${e.message}`:typeof e=="string"?e:String(e)).toLowerCase();return!!(n.includes("notallowederror")||n.includes("permissiondeniederror")||n.includes("permission denied")||n.includes("permissions denied")||n.includes("microphone access denied")||(n.includes("microphone")||n.includes(" mic"))&&(n.includes("denied")||n.includes("blocked")||n.includes("not allowed")||n.includes("permission")))}async function ha(){if(typeof navigator>"u"||!navigator.permissions?.query)return!1;try{return(await navigator.permissions.query({name:"microphone"})).state==="denied"}catch{return!1}}const fr=16e3,mr=16e3;class pa extends Jn.ProtobufFrameSerializer{async deserialize(t){try{return await super.deserialize(t)}catch(n){if(n instanceof Error&&n.message==="Unknown frame kind")return{type:"raw",message:null};throw n}}}class ga extends Jn.DailyMediaManager{constructor(){super(!0,!0,void 0,void 0,512,fr,mr)}bufferBotAudio(t,n){}}const wa=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Ur(e){return wa.test(e.trim())}function ya(e){const t=e.replace(/\/$/,"");return t.startsWith("https://")?`wss://${t.slice(8)}`:t.startsWith("http://")?`ws://${t.slice(7)}`:t}function Ki(e){if(e.wsUrl?.trim())return e.wsUrl.trim();const t=e.authToken?.trim();if(!t)throw new Error("Voice connect requires authToken (same JWT as SSE Authorization Bearer).");const n=e.apiBase??Dt,r=new URL(`${ya(n)}/ws/voice`);r.searchParams.set("authToken",t),r.searchParams.set("tenant","aiTwin");const i=st(e.ttsEngineId,e.voiceId),s=i.ttsEngineId;s&&r.searchParams.set("ttsEngineId",s);const o=i.voiceId?.trim();o&&(s&&pi(s)?Ur(o)||r.searchParams.set("voiceId",o):Ur(o)&&r.searchParams.set("voiceId",o));const a=pn(e.language);a&&r.searchParams.set("language",a);const u=e.brainId?.trim()||e.personaId?.trim();u&&r.searchParams.set("brainId",u);const d=e.brainUrl?.trim();d&&r.searchParams.set("brainUrl",d);const l=e.speakingRate??Kt;r.searchParams.set("speaking_rate",String(Math.max(_n,Math.min(bn,l)))),e.ttsStream===!1?r.searchParams.set("tts_stream","false"):r.searchParams.set("tts_stream","true"),e.speakOnlyWhenSpokenTo&&r.searchParams.set("speakOnlyWhenSpokenTo","true");const c=e.avatarName?.trim();return c&&r.searchParams.set("avatarName",c),r.toString()}function Fn(e,t){if(!Array.isArray(e))return[];const n=[];for(const r of e)!r||typeof r!="object"||n.push({sentence_index:Number(r.sentence_index??0),text:String(r.text??""),sentiment:typeof r.sentiment=="string"?r.sentiment:void 0,emotion:String(r.emotion??t),start_word:Number(r.start_word??0),end_word:Number(r.end_word??0)});return n}function va(e){if(!e||typeof e!="object")return null;const t=e,r=(t.data&&typeof t.data=="object"&&!Array.isArray(t.data)?t.data:null)||t;return{...r,type:String(r.type??t.type??"server-message")}}function Vr(e){return e.is_new_segment===!0||Number(e.audio_offset_ms??-1)===0?!0:Number(e.chunk_index??-1)===0}function _a(e){return e?.transport?._mediaManager?._wavStreamPlayer??null}function Gi(e,t={}){const n=[],r=[],i=[],s=[];let o=0;const a=[],u=[];let d="",l="neutral",c=at,f=!1,g=null,T=null,h={...t},I=0,F=null;const N=new Set,b=[];let k=!1,V=null,D=!0,se="default";const le=new Set;let P=null,H=0,Q=!1,oe=!1,ae=null,te=!1;const ce=()=>{ae!=null&&(clearTimeout(ae),ae=null)},ye=()=>{ce(),ae=setTimeout(()=>{ae=null,D&&(M.markStreamComplete(n),ie("bot_stopped"))},450)},ne=()=>N.forEach(p=>p());let W=()=>!1;const M=zi({onSpeakingStart:()=>e("speaking"),onSpeakingDone:()=>{te=!1,oe=!1,e("done")},onNotify:ne,shouldHoldForWarmup:()=>W()},c),Y=()=>{if(V)return V;const p=new MessageChannel;return p.port1.onmessage=()=>{const B=b.shift();B!==void 0&&q(B),b.length>0?p.port2.postMessage(null):k=!1},V=p,p},R=p=>{b.push(p),!k&&(k=!0,Y().port2.postMessage(null))},z=()=>M.getPlaybackElapsedMs(),X=(p,B=d)=>{if(!h.onLipsyncDebug)return;const re=z(),de=n,be=vn(i).map(xe=>({viseme:xe.viseme,vtimeMs:xe.vtime}));h.onLipsyncDebug({event:p,spokenText:B,mood:l,playbackAnchorPerf:M.getPlaybackStartPerf(),playbackElapsedMs:re,queueLength:de.length,activeViseme:jt(de,re),segments:u.map(xe=>({...xe,visemes:[...xe.visemes]})),fullQueueLength:i.length,compactionSkipped:be})},ie=(p,B)=>{h.onLipsyncDebug&&(F={event:p,spokenText:B},I===0&&(I=requestAnimationFrame(()=>{I=0;const re=F;F=null,re&&X(re.event,re.spokenText)})))},v=()=>{D=!0,le.clear(),M.clearAborted()},U=()=>{const p=_a(g);if(p){se=`bot-${Date.now()}`,p.interruptedTrackIds={[se]:!0};try{p.interrupt?.()}catch{}}const B=T;try{B?.interrupt?.()}catch{}},L=p=>{if(te&&M.isSpeaking())return!0;const B=p.context_id?.trim();return B&&le.has(B)?!0:D?!1:!(p.type==="avatar_metadata"||Vr(p))},Z=p=>{D=!1,te=!1;const B=p.context_id?.trim();B&&le.add(B),ce(),Q=!1,oe=!1,b.length=0,k=!1,P?.abort(),P=null,H+=1,U(),M.bargeIn(),$(),e("idle"),ie("interrupt"),ne()},$=()=>{n.length=0,r.length=0,i.length=0,s.length=0,o=0,u.length=0,d="",l="neutral",a.length=0},J=()=>{ce(),oe=!1,Q=!1,M.stop(),$(),ie("reset"),ne()},_=()=>{J(),e("idle"),ne()},y=(p,B)=>{typeof p.mood=="string"&&p.mood.trim()&&(l=p.mood.trim()),Array.isArray(p.sentence_emotions)&&p.sentence_emotions.length>0&&(B&&(a.length=0),a.push(...Fn(p.sentence_emotions,l)))},S=p=>{if(L({...p,type:"avatar_metadata"}))return;v(),Q=!0;const B=String(p.spoken_text??p.text??"").trim();B&&(d=B),M.resetForNewStream(c),typeof p.mood=="string"&&p.mood.trim()&&(l=p.mood.trim()),a.length=0,a.push(...Fn(p.sentence_emotions,l)),Array.isArray(p.gestures)&&p.gestures,ie("metadata",B),ne()},G=(p,B,re,de)=>{if(!h.onLipsyncDebug)return;const be=n.slice(re),xe=de.filter(Pe=>String(Pe.symbol??Pe.viseme??"").trim());u.push({spokenText:B,audioOffsetMs:be[0]?.vtime??0,isNewSegment:!!p.is_new_segment,chunkDurationMs:p.chunk_duration_ms!=null?Number(p.chunk_duration_ms):null,receivedAtMs:performance.now(),visemes:be.map((Pe,$e)=>{const Ue=xe[$e];return{rawSymbol:String(Ue?.symbol??Ue?.viseme??Pe.viseme).trim(),viseme:Pe.viseme,startSec:Number(Ue?.start??0),durationSec:Number(Ue?.duration??0),vtimeMs:Pe.vtime,vdurationMs:Pe.vduration}})})},ee=(p,B,re,de)=>{if(p.length===0&&B.length===0)return;Vi(i,s,p,B,o,c);const be=Wi(p,o);let xe=null;const Pe=re.trim();if(Pe)try{const $e=atob(Pe.replace(/\s+/g,"")).length,Ue=Number.isFinite(de)&&(de??0)>0?Number(de):16e3;xe=$e/2/Ue*1e3}catch{xe=null}o=Math.max(be,xe!=null?o+xe:o)},ue=p=>{if(L(p))return;const B=Vr(p);B&&(ce(),v());const re=String(p.spoken_text??p.text??"").trim(),de=p.visemes??[],be=p.words??[],xe=String(p.chunk??"").trim(),Pe=Q||B||!oe;Q&&(Q=!1),Pe&&(oe=!0),re&&(d=re),y(p,B),Pe?(M.resetForNewStream(c),i.length=0,s.length=0,o=0):B&&M.resumeBotStream(),ee(de,be,xe,p.sample_rate);const $e=n.length;M.playAudioChunk(n,r,xe,de,be,B,{sampleRate:p.sample_rate,newStream:Pe,audioOffsetMs:Number(p.audio_offset_ms??0),onNewStream:()=>{u.length=0}}).then(()=>{G(p,re,$e,de),ie("audio_chunk",re),ye()})},me=p=>{if(L(p))return;(p.is_new_segment||Number(p.audio_offset_ms??-1)===0)&&v(),console.warn("[realtimeVoice] avatar_lipsync is deprecated — expect avatar_audio_chunk");const B=String(p.spoken_text??p.text??"").trim();B&&(d=B),y(p,!!p.is_new_segment),ee(p.visemes??[],p.words??[],"",void 0);const re=n.length;M.queueTimingsOnly(n,r,p.visemes??[],p.words??[],!!p.is_new_segment,{audioOffsetMs:Number(p.audio_offset_ms??0)}),G(p,B,re,p.visemes??[]),ie("lipsync",B),ne()},x=async p=>{const B=String(p.text??"").trim();if(!B)return;const re=h.authToken?.trim();if(!re){console.error("[realtimeVoice] avatar_speak requires authToken");return}P?.abort();const de=new AbortController;P=de;const be=++H;v(),M.resetForNewStream(c),d=B;const Pe=`${(h.apiBase??Dt).replace(/\/$/,"")}/avatar_ttsWithPoses`,$e=st(c,h.voiceId);$e.ttsEngineId&&(c=$e.ttsEngineId,M.setStreamTtsEngineId(c));const Ue=$e.voiceId?.trim()||(c===ot?hn:dr),Gt=h.speakingRate??Kt,kn=Math.max(_n,Math.min(bn,Gt)),Lt={user_query:B,ttsEngineId:c,speakingRate:kn,tenant:"aiTwin"};Wn.has(c)&&(Lt.voice_id=Ue);const bt=pn(h.language);bt&&(Lt.language=bt);try{const Oe=await fetch(Pe,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${re}`},body:JSON.stringify(Lt),signal:de.signal});if(!Oe.ok)throw new Error(`avatar_ttsWithPoses failed (${Oe.status})`);const qe=Oe.body;if(!qe)throw new Error("No response body");const St=qe.getReader(),Ie=new TextDecoder;let Re="",Ye=!0;const Ve=async We=>{if(be!==H||de.signal.aborted||!D)return;const Ae=xi(We);if(Ae){if(Ae.event==="audio"){const De=Ae.data.chunk,ze=Ae.data.visemes??[],tt=Ae.data.words??[],je=Ae.data.is_new_segment??!1,He=Ae.data.sample_rate,Tt=Number(Ae.data.audio_offset_ms??0);if(De){const kt=Ye;Ye=!1,await M.playAudioChunk(n,r,De,ze,tt,je,{sampleRate:He,newStream:kt,audioOffsetMs:Tt})}else(ze.length>0||tt.length>0)&&M.queueTimingsOnly(n,r,ze,tt,je,{audioOffsetMs:Tt,newStream:Ye})}else if(Ae.event==="metadata"){const De=Ae.data.mood;typeof De=="string"&&De.trim()&&(l=De.trim());const ze=Ae.data.sentence_emotions;Array.isArray(ze)&&(a.length=0,a.push(...Fn(ze,l))),ne()}else if(Ae.event==="error")throw new Error(String(Ae.data.message??"avatar_ttsWithPoses stream error"))}};for(;!(be!==H||de.signal.aborted||!D);){const{done:We,value:Ae}=await St.read();Ae&&(Re+=Ie.decode(Ae,{stream:!0}));const De=Re.split(`
4
4
 
5
- `);Re=De.pop()??"";for(const He of De)await Ue(He);if(Ve){Re.trim()&&await Ue(Re.trim()),Me===z&&!de.signal.aborted&&D&&M.markStreamComplete(n);break}}}catch(Ne){if(de.signal.aborted||!D)return;console.error("[realtimeVoice] avatar_speak failed:",Ne),e("error"),se()}finally{P===de&&(P=null)}},te=p=>{const B=ya(p);if(!B){console.warn("[realtimeVoice] server message with no payload",p);return}const ie=String(B.type??"unknown");switch(ie!=="avatar_metadata"&&ie!=="avatar_audio_chunk"&&ie!=="avatar_lipsync"&&ie!=="avatar_interrupt"&&ie!=="error"&&console.debug("[realtimeVoice] server message",ie,B),ie){case"avatar_metadata":S(B);break;case"avatar_audio_chunk":x(B);break;case"avatar_lipsync":J(B);break;case"avatar_interrupt":Y(B);break;case"error":{const de=String(B.message??"Voice session error");console.error("[realtimeVoice] server error:",de),e("error"),se();break}case"avatar_speak":ne(B);break}},Se=()=>g||(T=new pa,g=new gs.PipecatClient({transport:new Qn.WebSocketTransport({mediaManager:T,serializer:new ha,recorderSampleRate:ur,playerSampleRate:dr}),enableCam:!1,enableMic:!0,callbacks:{onBotReady:()=>{f=!0,e("idle"),se()},onDisconnected:()=>{f=!1,_()},onError:p=>{const B=typeof p=="object"&&p!==null&&"data"in p?String(p.data??p):String(p);console.error("[realtimeVoice]",B,p),e("error"),se()},onTransportStateChanged:p=>{p==="error"&&(e("error"),se())},onServerMessage:p=>{R(p)},onUserStartedSpeaking:()=>{},onBotStartedSpeaking:()=>{v(),ae("bot_started")},onBotStoppedSpeaking:()=>{D&&we()},onUserTranscript:p=>{String(p.text??""),m.onUserTranscript?.(p.text,!!p.final)},onBotOutput:p=>{m.onBotOutput?.(p.text)}}}),g);return{setDeveloperToken:p=>{},setTtsEngineId:p=>{c=p,M.setStreamTtsEngineId(p)},getTtsEngineId:()=>c,speak:async(p,B)=>{const ie=p.trim();if(ie){if(!g||!f)throw new Error("Realtime voice is not connected. Call connect() first.");Z(),await g.sendText(ie)}},speakWithReply:async(p,B)=>{throw new Error("speakWithReply is not available on the voice session; use AiTwin.speakWithReply()")},stop:()=>{P?.abort(),P=null,z+=1,v(),Z(),e("idle"),se()},connect:async p=>{p&&(m={...m,...p});const B=it(m.ttsEngineId,m.voiceId);if(B.ttsEngineId&&(c=B.ttsEngineId,M.setStreamTtsEngineId(c)),m={...m,ttsEngineId:c,...B.voiceId?{voiceId:B.voiceId}:{}},!m.authToken?.trim())throw new Error("Voice connect requires authToken (same JWT as SSE Authorization Bearer).");e("loading");const ie=Se();await ie.initDevices();const de=ji({...m,ttsEngineId:c});console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):",de);try{await ie.connect({wsUrl:de})}catch(Me){f=!1,e("error"),se();const Ee=Me instanceof Error?Me.message:"Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";throw new Error(`${Ee} — ${de}`)}f=!0,e("idle"),se()},disconnect:async()=>{if(ue(),Q=!1,le=!1,b.length=0,E=!1,P?.abort(),P=null,z+=1,v(),g)try{await g.disconnect()}catch(p){console.error("[realtimeVoice] disconnect failed:",p)}f=!1,_()},isConnected:()=>f,unlockAudio:async()=>{await M.primeAudioContext()},getVisemeQueue:()=>n,getWordQueue:()=>r,getFullVisemeQueue:()=>i,getFullWordQueue:()=>s,getSentenceEmotions:()=>a,getStreamMood:()=>l,getPlaybackElapsedMs:H,isSpeaking:()=>M.isSpeaking(),isWarmingUp:()=>M.isWarmingUp(),releaseWarmupHold:()=>M.releaseWarmupHold(),setLipsyncWarmupHold:(p,B)=>{B&&(W=B),M.setLipsyncWarmupHoldEnabled(p)},getVoiceStream:()=>M.getVoiceStream(),setPlaybackSpeed:p=>{M.setPlaybackSpeed(p)},getPlaybackSpeed:()=>M.getPlaybackSpeed(),subscribe:p=>(O.add(p),()=>O.delete(p))}}function Gi(e,t,n){return{width:e.crop?.source_width??t,height:e.crop?.source_height??n}}function qi(e,t){const n=e.crop;return{x:t?.source_x??n?.x??0,y:t?.source_y??n?.y??0,width:t?.source_w??n?.width??e.frame_width??0,height:t?.source_h??n?.height??e.frame_height??0}}function Yi(e){return e?(e.w??0)>0&&(e.h??0)>0:!1}async function Dt(e,t){const n=await fetch(e);if(!n.ok)throw new Error(`Failed to load encrypted asset: ${e}`);const r=await n.arrayBuffer();return Us(r,t)}async function Qi(e,t){if(!t)throw new Error("Missing asset key for encrypted JSON");const n=await Dt(e,t),r=new TextDecoder().decode(n);return JSON.parse(r)}async function _a(e){const t=new Blob([e],{type:"image/webp"});return createImageBitmap(t)}async function ba(e){const t=new Blob([e],{type:"image/webp"}),n=URL.createObjectURL(t);return new Promise((r,i)=>{const s=new Image;s.decoding="async",s.onload=()=>{URL.revokeObjectURL(n),r(s)},s.onerror=()=>{URL.revokeObjectURL(n),i(new Error("Failed to decode decrypted image"))},s.src=n})}async function Sa(e){const t=new Blob([e],{type:"image/png"});return createImageBitmap(t)}async function Ta(e){const t=new Blob([e],{type:"image/png"}),n=URL.createObjectURL(t);return new Promise((r,i)=>{const s=new Image;s.decoding="async",s.onload=()=>{URL.revokeObjectURL(n),r(s)},s.onerror=()=>{URL.revokeObjectURL(n),i(new Error("Failed to decode decrypted PNG"))},s.src=n})}function ka(e){const t=new Blob([e],{type:"video/mp4"});return URL.createObjectURL(t)}async function Ea(e){const t=Ws();if(!Wt())return t;if(!e)throw new Error("Missing asset key for encrypted idle video");const n=await Dt(t,e);return ka(n)}function It(e,t){return`${e}/${t}_minus_sil.png`}const Xi=5;function _n(e){const t=e?.composite_threshold;if(typeof t=="number"&&t>0)return t;const n=e?.crop?.content_threshold;return typeof n=="number"&&n>0?n:Xi}const xa=["aa","E","I","O","U","PP","FF","DD","SS","TH","CH","RR","kk","nn"],Kn=["t01_0.25","t02_0.50","t03_0.75"],Ia=Kn.length,Vr=new Set(["aa__kk","aa__nn","aa__sil","CH__aa","CH__DD","CH__E","CH__FF","CH__I","CH__kk","CH__nn","CH__O","CH__PP","CH__RR","CH__sil","CH__SS","CH__TH","CH__U","DD__aa","DD__E","DD__FF","DD__I","DD__kk","DD__nn","DD__O","DD__PP","DD__RR","DD__sil","DD__SS","DD__TH","DD__U","E__aa","E__FF","E__I","E__kk","E__nn","E__O","E__PP","E__RR","E__sil","E__SS","E__TH","E__U","FF__aa","FF__I","FF__kk","FF__nn","FF__O","FF__PP","FF__RR","FF__sil","FF__SS","FF__TH","FF__U","I__aa","I__kk","I__nn","I__O","I__PP","I__RR","I__sil","I__SS","I__TH","I__U","kk__nn","kk__sil","nn__sil","O__aa","O__kk","O__nn","O__PP","O__RR","O__sil","O__SS","O__TH","O__U","PP__aa","PP__kk","PP__nn","PP__RR","PP__sil","PP__SS","PP__TH","PP__U","RR__aa","RR__kk","RR__nn","RR__sil","RR__SS","RR__TH","RR__U","SS__aa","SS__kk","SS__nn","SS__sil","SS__TH","SS__U","TH__aa","TH__kk","TH__nn","TH__sil","TH__U","U__aa","U__kk","U__nn","U__sil"]);function Wr(e,t){return`${e}__${t}`}function zr(e,t){return`pairs/${e}/${t}_minus_sil.png`}function Mt(e){return`${e}_minus_sil.png`}function fr(e,t){if(t.has(e))return e;const n=/^pairs\/([^/]+)\//.exec(e);if(!n)return null;const r=n[1].split("__")[1];if(!r||r==="sil")return null;const i=Mt(r);return t.has(i)?i:null}function Ji(e,t){return e.filter(n=>t.has(n))}function Aa(e,t,n){const r=mr(e,t);return Ji(r,n).length>0}function mr(e,t){if(e===t)return[];const n=[],r=Wr(e,t);if(Vr.has(r)){for(const s of Kn)n.push(zr(r,s));return n}const i=Wr(t,e);if(Vr.has(i)){for(let s=Ia-1;s>=0;s--)n.push(zr(i,Kn[s]));return n}return[]}function Ma(e){return e<16?[1]:e<24?[1]:e<35?[0,1,2]:[0,1,2]}function Pa(e,t){return Ma(t).map(r=>e[r]).filter(r=>r!=null)}function Ra(e,t,n,r){if(r<=0)return 0;const s=Math.max(1,n-t)/r,o=e-t,a=Math.floor(o/s);return Math.min(r-1,Math.max(0,a))}function Zi(e,t,n){if(!n)return`${e}/${t}`;const r=t.split("/").pop()??t,i=r.includes(".")?r.slice(0,r.lastIndexOf(".")):r;return`${e}/${i}.bin`}const Ca=[.32,.24,.16,.08,0],Da=Ca.map(e=>`eyes_${e.toFixed(2)}`),Ba=["eyes_0.32","eyes_0.16","eyes_0.00"],Fa=["eyes_0.32","eyes_0.16","eyes_0.08","eyes_0.00"],Ke=2,La=-20,on=16,es={fear:-20,anger:-20,sad:-10,neutral:0,love:0,happy:0,disgust:10};function Ut(e){return`brow_${e}`}function gt(e){return es[e]}function Ze(e){return Math.max(La,Math.min(on,e))}function Na(e,t){if(e===t)return[];const n=e<t,r=n?Ke:-Ke,i=[];let s=e;if(s%Ke!==0){const o=n?Math.ceil(s/Ke)*Ke:Math.floor(s/Ke)*Ke,a=Ze(o);if(n?a>=t:a<=t)return[t];s=a,i.push(s)}for(;;){const o=s+r;if(n?o>=t:o<=t){t!==s&&i.push(t);break}s=o,i.push(s)}return i}function Hr(e){const t=Ze(e);if(t%Ke===0)return[t];const n=Math.floor(t/Ke)*Ke,r=Math.min(on,n+Ke),i=Ze(n);return t-i<=r-t?i===r?[i]:[i,r]:i===r?[r]:[r,i]}const Oa=["fear","anger","sad","neutral","love","happy","disgust"],Ua={fear:"fear",afraid:"fear",anxiety:"fear",anger:"anger",angry:"anger",mad:"anger",sad:"sad",sadness:"sad",sorrow:"sad",neutral:"neutral",calm:"neutral",love:"love",loving:"love",affection:"love",happy:"happy",happiness:"happy",joy:"happy",joyful:"happy",excited:"happy",disgust:"disgust",disgusted:"disgust"};function rt(e,t="neutral"){const n=String(e??"").trim().toLowerCase();return n?Ua[n]??t:t}function ts(e){return e===0?null:It(Ut(e),"eyes_standard")}function ns(e){return ts(gt(e))}const Va={[-20]:"anger",[-10]:"sad",0:"neutral",10:"disgust"};function hr(e,t){if(!e)return null;if(t(e))return e;const n=e.match(/^brow_(-?\d+)\/(eyes_[^/]+)_minus_sil\.png$/);if(n){const i=Number(n[1]),s=n[2];for(const a of Hr(i)){if(a===i)continue;const u=It(Ut(a),s);if(t(u))return u}const o=Va[i];if(o){const a=It(o,s);if(t(a))return a}return null}const r=e.match(/^(fear|anger|sad|neutral|love|happy|disgust)\/(eyes_[^/]+)_minus_sil\.png$/);if(r){const i=r[1],s=r[2],o=gt(i);for(const a of[o,...Hr(o)]){const u=It(Ut(a),s);if(t(u))return u}}return null}function $r(e,t){const n=Ut(gt(e));return!t||t==="eyes_standard"?ns(e):e==="neutral"&&t==="eyes_standard"?null:It(n,t)}function Wa(e,t){const n=[];for(const r of t)n.push($r(e,r));for(let r=t.length-2;r>=0;r--)n.push($r(e,t[r]));return n.push(ns(e)),n}function za(e){return It(Ut(e),"eyes_standard")}function Ha(e,t){return Na(e,t).map(za)}const $a=6;function ja(e,t){for(const n of e)if(t>=n.wtime&&t<n.wtime+n.wduration)return n;return null}function Ka(e){const t=Ze(e);return t>=on?on:Ze(t+$a)}function Ga(e,t,n){const r=Ze(n);return ja(t,e)?.emphasis?Ka(r):r}function jr(e){if(!e)return 0;const t=e.match(/^brow_(-?\d+)\//);if(!t?.[1])return null;const n=Number.parseInt(t[1],10);return Number.isFinite(n)?Ze(n):null}function Je(e,t){return e+Math.random()*(t-e)}function Kr(){return Je(14,32)}function qa(e){if(e)return{holdMinMs:10,holdMaxMs:18,closedExtraMs:0};const t=Math.random();return t<.18?{holdMinMs:38,holdMaxMs:62,closedExtraMs:Je(40,90)}:t<.42?{holdMinMs:26,holdMaxMs:44,closedExtraMs:Je(12,36)}:t<.82?{holdMinMs:14,holdMaxMs:28,closedExtraMs:Math.random()<.35?Je(8,22):0}:{holdMinMs:10,holdMaxMs:18,closedExtraMs:0}}function Gr(e,t){const n=Je(e.holdMinMs,e.holdMaxMs);return t?n+e.closedExtraMs:n}function Ya(e={}){const{initialDelayMs:t=800+Math.random()*1200,intervalMinMs:n=2200,intervalMaxMs:r=5800,doubleBlinkChance:i=.18,doubleBlinkGapMinMs:s=120,doubleBlinkGapMaxMs:o=220,fullBlinkChance:a=.75,postEmotionChangeDelayMs:u=380,minDwellMs:d=550}=e;let l="neutral",c="neutral",f=0,g=0,T=0,m={kind:"idle",nextBlinkAt:performance.now()+t},I=null,L="__eyes_open__|neutral|brow:0";function O(P=l){L=`${I??"__eyes_open__"}|${P}|brow:${g}`}function b(P,z=l){I=P;const Q=jr(P);Q!=null?g=Q:P==null&&(g=0),O(z)}function E(P){return ts(P)}function V(P,z,Q){const le=Ze(z),ce=Ze(Q);if(f=ce,le===ce){b(E(ce));return}const re=[...Ha(le,ce),E(ce)];m={kind:"browBlend",paths:re,index:0,holdUntil:P+Kr(),targetBrow:ce},b(re[0]??null)}function D(P,z){m={kind:"idle",nextBlinkAt:P+Je(n,r)},c=z,b(E(f),z)}function oe(P,z,Q){const le=Q?Ba:Math.random()<a?Da:Fa,ce=qa(Q),re=le.length-1;m={kind:"playing",paths:Wa(z,le),index:0,holdUntil:P+Gr(ce,re===0),after:"idle",blinkEmotion:z,pace:ce,closedFrameIndex:re},c=z,b(m.paths[0]??null,z)}function pe(P){if(m.kind==="browBlend"){for(;P>=m.holdUntil&&m.index<m.paths.length-1;)m.index+=1,m.holdUntil=P+Kr();const z=m.paths[m.index]??null;b(z),P>=m.holdUntil&&m.index>=m.paths.length-1&&(g=m.targetBrow,f=m.targetBrow,b(E(m.targetBrow)),m={kind:"idle",nextBlinkAt:P+u});return}if(m.kind==="idle"){b(E(f),c),P>=m.nextBlinkAt&&oe(P,c,!1);return}if(m.kind==="doublePause"){b(E(f),m.blinkEmotion),P>=m.resumeAt&&oe(P,m.blinkEmotion,!0);return}if(m.kind==="playing"){for(;P>=m.holdUntil&&m.index<m.paths.length-1;)m.index+=1,m.holdUntil=P+Gr(m.pace,m.index===m.closedFrameIndex);if(b(m.paths[m.index]??null,m.blinkEmotion),P>=m.holdUntil&&m.index>=m.paths.length-1){if(m.after==="resumeIdle"){D(P+u,l);return}if(m.after==="doublePause"){m={kind:"doublePause",resumeAt:P+Je(s,o),blinkEmotion:m.blinkEmotion},b(E(f),m.blinkEmotion);return}if(m.after==="idle"&&Math.random()<i){m={kind:"doublePause",resumeAt:P+Je(s,o),blinkEmotion:m.blinkEmotion},b(E(f),m.blinkEmotion);return}D(P,m.blinkEmotion)}}}return{advance:pe,setTargetEmotion(P,z){const Q=rt(P,l);if(Q===l)return;const le=performance.now();!z?.force&&d>0&&!(l==="neutral"&&Q!=="neutral")&&le-T<d||(l=Q,c=Q,T=le)},setTargetEyebrow(P){const z=Ze(P);if(z===f)return;const Q=performance.now(),le=m.kind==="browBlend"?jr(I)??g:g;f=z,!(z===le&&m.kind!=="browBlend")&&(m.kind==="playing"||m.kind==="doublePause"||V(Q,le,z))},snapToEmotion(P){const z=rt(P),Q=gt(z);l=z,c=z,f=Q,g=Q,T=performance.now(),m={kind:"idle",nextBlinkAt:performance.now()+Je(4e3,7e3)},b(E(Q),z)},getTargetEmotion:()=>l,getTargetEyebrow:()=>f,getExpressionPath:()=>I,getDrawKey:()=>L,reset(){l="neutral",c="neutral",f=0,g=0,T=0,D(performance.now()+Je(n,r),"neutral")}}}function Qa(){const e=document.createElement("video");e.muted=!0,e.playsInline=!0,e.preload="auto",e.setAttribute("playsinline",""),e.loop=!0;let t=!1,n=null,r="";const i=()=>{n&&(URL.revokeObjectURL(n),n=null)};return{async load(s){t&&r===s||(i(),t=!1,r=s,s.startsWith("blob:")&&(n=s),await new Promise((o,a)=>{const u=()=>{const d=e.duration;if(!Number.isFinite(d)||d<=0){a(new Error(`Idle video has invalid duration: ${s}`));return}t=!0,e.currentTime=0,o()};e.addEventListener("loadeddata",u,{once:!0}),e.addEventListener("error",()=>a(new Error(`Failed to load idle video: ${s}`)),{once:!0}),e.src=s,e.load()}))},isReady(){return t},getVideo(){return e},setActive(s){t&&(s?e.paused&&e.play().catch(()=>{}):e.paused||e.pause())},setLooping(s){e.loop=s},restart(){t&&(e.currentTime=0)},dispose(){e.pause(),e.removeAttribute("src"),e.load(),i(),t=!1,r=""}}}const Xa=50,rs=400;function Ja(e,t,n){return e.filter(r=>{const i=r.queueIndex??-1;return i>=t&&i<=n})}function Za(e,t){const n=[];for(const r of e){const i=Ja(t,r.start_word,r.end_word);if(i.length===0)continue;const s=i[0],o=i[i.length-1];n.push({sentence:r,startMs:Math.max(0,s.wtime-Xa),endMs:o.wtime+o.wduration+rs})}return n.sort((r,i)=>r.startMs-i.startMs)}function eo(e,t,n,r="neutral"){const i=rt(r);if(t.length===0)return i;if(n.length===0||e<n[0].wtime)return rt(t[0]?.emotion,i);const s=Za(t,n);let o=null;for(const d of s)e>=d.startMs&&e<=d.endMs&&(o=d);if(o)return rt(o.sentence.emotion,i);const a=n[n.length-1],u=a.wtime+a.wduration;return e>u+rs?rt(t[t.length-1]?.emotion,i):i}function pr(e,t,n,r,i=Xi){const s=new OffscreenCanvas(n,r),o=s.getContext("2d",{willReadFrequently:!0});o.drawImage(e,0,0,n,r);const a=o.getImageData(0,0,n,r),u=o.createImageData(n,r);u.data.set(a.data);const l=new OffscreenCanvas(n,r).getContext("2d",{willReadFrequently:!0});for(const c of t){l.clearRect(0,0,n,r),l.drawImage(c,0,0,n,r);const f=l.getImageData(0,0,n,r);for(let g=0;g<u.data.length;g+=4){const T=f.data[g],m=f.data[g+1],I=f.data[g+2];f.data[g+3]>0&&Math.max(T,m,I)>i&&(u.data[g]=T,u.data[g+1]=m,u.data[g+2]=I,u.data[g+3]=255)}}return o.putImageData(u,0,0),s}let mt=null,Pt=null;const cn=new Map;function to(){mt=null,Pt=null,cn.clear()}vi(to);function no(e){return e??Pt??void 0}function gr(e){return new Promise((t,n)=>{const r=new Image;r.decoding="async",r.onload=()=>t(r),r.onerror=()=>n(new Error(`Failed to load image: ${e}`)),r.src=e})}async function ro(e){return Wt()?Ta(await Dt(an(),e??"")):gr(an())}async function qr(e,t,n,r){const i=n?await Qi(e,r):await fetch(e).then(d=>{if(!d.ok)throw new Error(`Failed to load ${e}`);return d.json()}),s=i.sheets?.[0];if(!s?.path)throw new Error(`${e} has no sheets[0].path`);const o=n?await ba(await Dt(Zi(t,s.path,n),r??"")):await gr(`${t}/${s.path}`),a=new Map;for(const d of i.cells??[])d.path&&a.set(d.path,d);const u=new Map;for(const d of i.sheets??[])u.set(d.index,d);return{atlas:o,atlasMeta:i,cellByPath:a,sheetByIndex:u,diffBase:t}}async function Gn(e){const t=no(e),n=Wt();if(n&&!t)throw new Error("Encrypted assets enabled but no key provided");if(mt&&(!n||Pt===(t??null))){const s=await mt;return{...Rt(s),visemePaths:[...s.viseme.cellByPath.keys()]}}Pt=t??null,cn.clear();const r=_i();mt=(async()=>{const s=await ro(t),o=await qr(bi(),r,n,t);let a=null;try{a=await qr(Si(),r,n,t)}catch{a=null}return{sil:s,viseme:o,expression:a}})();const i=await mt;return{...Rt(i),visemePaths:[...i.viseme.cellByPath.keys()]}}async function wr(e){if(await Gn(e),!mt)throw new Error("Assets not loaded");return mt}function Rt(e){return Gi(e.viseme.atlasMeta,e.sil.naturalWidth,e.sil.naturalHeight)}function io(e,t){const n=document.createElement("canvas");return n.width=t.w,n.height=t.h,n.getContext("2d",{willReadFrequently:!0}).drawImage(e.atlas,t.x,t.y,t.w,t.h,0,0,t.w,t.h),n}async function ln(e,t,n){const r=`${t.diffBase}::${n}`,i=cn.get(r);if(i)return i;const s=(async()=>{const o=t.cellByPath.get(n);if(!o)throw new Error(`No atlas cell for path: ${n}`);const a=qi(t.atlasMeta,o),u=Rt(e);if(Yi(o)){const c=io(t,o),f=document.createElement("canvas");f.width=u.width,f.height=u.height;const g=f.getContext("2d");return g.fillStyle="#000",g.fillRect(0,0,u.width,u.height),g.drawImage(c,a.x,a.y),f}const d=`${t.diffBase}/${n}`,l=await gr(d);if(l.naturalWidth===u.width&&l.naturalHeight===u.height)return l;throw new Error(`Diff PNG wrong size for ${n}`)})();return cn.set(r,s),s}async function Bn(e,t,n,r,i,s){if(i){const{width:g,height:T}=i;if(s&&!s()||t.readyState<HTMLMediaElement.HAVE_CURRENT_DATA)return;const m=document.createElement("canvas");m.width=g,m.height=T;const I=m.getContext("2d");if(I.clearRect(0,0,g,T),I.drawImage(t,0,0,g,T),s&&!s())return;(e.width!==g||e.height!==T)&&(e.width=g,e.height=T);const L=e.getContext("2d");L.clearRect(0,0,g,T),L.drawImage(m,0,0);return}const o=await wr(),a=_n(o.viseme.atlasMeta),{width:u,height:d}=Rt(o);(e.width!==u||e.height!==d)&&(e.width=u,e.height=d);const l=[],c=e.getContext("2d");if(c.clearRect(0,0,u,d),t.readyState<HTMLMediaElement.HAVE_CURRENT_DATA){const g=o.sil;c.drawImage(g,0,0,u,d);return}if(l.length===0){c.drawImage(t,0,0,u,d);return}const f=pr(t,l,u,d,a);c.drawImage(f,0,0)}async function qn(e,t,n){const r=await wr(Pt??void 0),i=_n(r.viseme.atlasMeta),{width:s,height:o}=Rt(r);(e.width!==s||e.height!==o)&&(e.width=s,e.height=o);const a=[];if(t.mouthPath){const l=new Set(r.viseme.cellByPath.keys()),c=fr(t.mouthPath,l)??(l.has(t.mouthPath)?t.mouthPath:null);c&&a.push(await ln(r,r.viseme,c))}if(t.expressionPath&&r.expression){const l=hr(t.expressionPath,c=>r.expression.cellByPath.has(c));l&&a.push(await ln(r,r.expression,l))}const u=e.getContext("2d");if(u.clearRect(0,0,s,o),a.length===0){u.drawImage(r.sil,0,0,s,o);return}const d=pr(r.sil,a,s,o,i);u.drawImage(d,0,0)}async function so(e,t){const n=await wr(Pt??void 0),r=_n(n.viseme.atlasMeta),{width:i,height:s}=Rt(n),o=[];if(e.mouthPath){const l=new Set(n.viseme.cellByPath.keys()),c=fr(e.mouthPath,l)??(l.has(e.mouthPath)?e.mouthPath:null);c&&o.push(await ln(n,n.viseme,c))}if(e.expressionPath&&n.expression){const l=hr(e.expressionPath,c=>n.expression.cellByPath.has(c));l&&o.push(await ln(n,n.expression,l))}const a=new OffscreenCanvas(i,s),u=a.getContext("2d");if(o.length===0)return u.drawImage(n.sil,0,0,i,s),a.transferToImageBitmap();const d=pr(n.sil,o,i,s,r);return u.drawImage(d,0,0),a.transferToImageBitmap()}async function ao(e){await qn(e,{mouthPath:null,expressionPath:null})}const en={fontFamily:"system-ui, -apple-system, sans-serif",fontSize:"clamp(0.875rem, 2.5vw, 1.125rem)",color:"rgba(255,255,255,0.85)"};function oo(e){return e.length===0?"":e.map(t=>t.word).join(" ")}function co(e){if(!e)return{enabled:!1,style:en};const t=e===!0?{}:e;return{enabled:!0,style:{fontFamily:t.fontFamily??en.fontFamily,fontSize:t.fontSize??en.fontSize,color:t.color??en.color}}}const lo={position:"absolute",bottom:0,left:0,right:0,display:"flex",justifyContent:"center",padding:"0.75rem 1rem",pointerEvents:"none"},uo={display:"inline-block",maxWidth:"100%",padding:"0.45rem 0.85rem",borderRadius:"0.75rem",background:"rgba(0,0,0,0.55)",lineHeight:1.4,textAlign:"center"},fo={display:"-webkit-box",WebkitLineClamp:2,WebkitBoxOrient:"vertical",overflow:"hidden",maxWidth:"100%",margin:0};function mo({text:e,style:t}){return F.jsx("div",{style:lo,"aria-live":"polite","aria-atomic":"true",children:F.jsx("div",{style:{...uo,fontFamily:t.fontFamily,fontSize:t.fontSize,color:t.color},children:F.jsx("p",{style:fo,children:e})})})}const ho={inset:0,overflow:"hidden",pointerEvents:"none",position:"absolute",zIndex:3},po={filter:"blur(22px)",height:"100%",inset:0,objectFit:"cover",objectPosition:"top",position:"absolute",transform:"scale(1.08)",width:"100%"},go={backdropFilter:"blur(10px)",WebkitBackdropFilter:"blur(10px)",inset:0,position:"absolute"};function wo({thumbnailUrl:e}){const[t,n]=h.useState(!1);h.useEffect(()=>{n(!1)},[e]);const r=!!e&&t;return F.jsxs("div",{style:ho,"aria-hidden":!0,children:[e?F.jsx("img",{src:e,alt:"",style:{...po,opacity:r?1:0,transition:"opacity 0.45s ease"},onLoad:()=>n(!0),onError:()=>n(!1)}):null,F.jsx("div",{style:{...go,background:r?"rgba(0, 0, 0, 0.28)":"rgba(10, 10, 11, 0.92)"}})]})}let is=null;function Yr(e){is=e}function lt(){return is}function yo(e){return`${e.from}→${e.to}@${e.fromVtime}`}function Qr(){return{rafTicksSpeaking:0,rafTicksDeduped:0,drawsRequested:0,drawsDisplayed:0,queueSuperseded:0,workerStale:0,rifeFramesOmittedByGap:0,transitionFramesSampled:0,transitionFramesDisplayed:0,transitionFramesMissed:0,bufferTicks:0,bufferHits:0,bufferUnderruns:0,bufferPrefetched:0}}function vo(){let e=Qr();const t=new Set,n=new Set,r=new Set;let i="",s=-1,o="";const a=()=>{e=Qr(),t.clear(),n.clear(),r.clear(),i="",s=-1,o=""},u=l=>{t.add(l)},d=l=>{l&&r.add(l)};return{reset:a,onRafTickSpeaking(){e.rafTicksSpeaking+=1},onDrawDeduped(){e.rafTicksDeduped+=1},onDrawRequested(){e.drawsRequested+=1},onQueueSuperseded(){e.queueSuperseded+=1},onWorkerStale(){e.workerStale+=1},onTransitionSample(l,c){const f=yo(l);e.transitionFramesSampled+=1,f!==i&&(i=f,s=-1,o!==f&&(o=f,e.rifeFramesOmittedByGap+=l.omittedByGap)),c&&n.add(c),s>=0&&l.frameIdx>s+1&&(e.transitionFramesMissed+=l.frameIdx-s-1),s=l.frameIdx},onBufferTick(){e.bufferTicks+=1},onBufferHit(){e.bufferHits+=1},onBufferUnderrun(){e.bufferUnderruns+=1},onBufferPrefetched(){e.bufferPrefetched+=1},onBufferReset(){},onDrawDisplayed(l,c){if(e.drawsDisplayed+=1,l.transitionMeta){e.transitionFramesDisplayed+=1;const g=l.transitionMeta;u(g.fromVtime),u(g.toVtime),d(l.mouthPath);return}if(l.label.includes("→")){e.transitionFramesDisplayed+=1;return}const f=l.label.trim();if(!(!f||f==="sil")){for(const g of c)if(g.viseme===f&&l.elapsedMs>=g.vtime&&l.elapsedMs<g.vtime+g.vduration){u(g.vtime);break}}},snapshot(l,c){let f=0,g=0;for(const O of l)if(O.viseme!=="sil"){if(O.vtime>=c)break;f+=1,t.has(O.vtime)&&(g+=1)}const T=f>0?Math.round(g/f*100):100,m=e.queueSuperseded+e.workerStale+Math.max(0,e.drawsRequested-e.drawsDisplayed),I=f-g,L=e.bufferTicks>0?Math.round(e.bufferHits/e.bufferTicks*100):100;return{...e,bufferHitRate:L,renderLoss:m,visemeSlotsTotal:f,visemeSlotsCovered:g,visemeCoveragePct:T,transitionFramesUniqueSampled:n.size,transitionFramesUniquePainted:r.size,drawsDeduped:e.rafTicksDeduped,renderSkips:e.rafTicksDeduped+m,visemesShown:g,visemesSkipped:I,rifeTransitionFramesSkipped:e.transitionFramesMissed}}}}const ss=Jn,_o=500,Xr=500,bo=450,Fn=.05,So=100,To=600,as="__sil__";function Jr(e){return e.lastDrawnKey?e.lastDrawnKey.split("|")[0]===as:!1}function yr(){return{lastDrawnKey:""}}function Le(e){e.lastDrawnKey="",e.transitionCache=void 0}function ko(e,t){if(t?.transitionCache&&t.transitionCache.queueLength===e.length)return t.transitionCache.transitions;const n=Ni(e);return t&&(t.transitionCache={queueLength:e.length,transitions:n}),n}function Eo(e,t){let n="sil";for(const r of e)r.viseme!=="sil"&&(t!=null&&r.vtime>t||(n=r.viseme));return n}function xo(e,t,n,r=120){if(n!=="sil")return n;const i=$i(e,t,r);return i!=="sil"?i:Eo(e,t)}function Io(e,t){return e==="sil"?!1:t?Aa(e,"sil",t):mr(e,"sil").length>0}function Ao(){return ss+To}function un(e){return e??as}function vr(e,t,n,r,i=So){if(e.length===0)return{diffPath:null,label:"sil"};if(t>=ft(e))return{diffPath:null,label:"sil"};const s=r?.holdPp===!0,o=ko(e,r),a=ca(o,t,s);if(a){const l=Oi(a.fromVtime,a.toVtime,a.from,a.to,s);if(t>=l.transStart&&t<l.transEnd){let c=mr(a.from,a.to);n&&(c=Ji(c,n));const f=Pa(c,l.effectiveGapMs);if(f.length>0){const g=Ra(t,l.transStart,l.transEnd,f.length);return{diffPath:f[g]??f[f.length-1],label:`${a.from}→${a.to}`,transitionMeta:{from:a.from,to:a.to,fromVtime:a.fromVtime,toVtime:a.toVtime,frameIdx:g,frameCount:f.length,fullFrameCount:c.length,omittedByGap:Math.max(0,c.length-f.length)}}}return{diffPath:Mt(a.to),label:a.to}}}const u=gn(e);let d=Di(u,t);return d==="sil"&&(d=$i(e,t,i)),d==="sil"?{diffPath:null,label:"sil"}:{diffPath:Mt(d),label:d}}function Vt(e,t){const n=t??un(e.expressionPath);return`${un(e.mouthPath)}|${n}`}function Zr(e,t,n,r,i,s,o,a,u,d){const l=vr(t,n,u,s),c={mouthPath:l.diffPath,expressionPath:r},f=Vt(c,i),g=lt();if(l.transitionMeta&&g?.onTransitionSample(l.transitionMeta,l.diffPath),f===s.lastDrawnKey){g?.onDrawDeduped();return}g?.onDrawRequested(),a(l),s.lastDrawnKey=f,o(e,c,f,T=>{T===f&&(s.lastDrawnKey=f)})}function Ln(e,t,n,r,i,s,o=null,a=null){const u={mouthPath:a,expressionPath:t},d=o!=null?`|idle@${Math.round(o*1e3)}`:"",l=Vt(u,n)+d;!(o!=null)&&l===r.lastDrawnKey||(s(o!=null?t?"idle · blink":"idle":t?"blink":"sil"),i(e,u,l,f=>{f===l&&(r.lastDrawnKey=l)}))}function os(e,t,n,r,i=Ot,s){if(n<=t||e.length===0)return[];const o=s??yr(),a=[];let u="";for(let d=t;d<n;d+=i){const l=vr(e,d,r,o),c=un(l.diffPath);c!==u&&(u=c,a.push({displayAtMs:d,mouthPath:l.diffPath,mouthKey:c,label:l.label}))}return a}const Mo=3;function _r(e){let t=0;const n=[];let r=!1,i=!1;const s=async()=>{if(!r){r=!0;try{for(;n.length>0;){const a=n.shift(),u=++t;await e(a.diffs,u,a)&&u===t&&a.onDisplayed?.(a.drawKey)}}finally{r=!1,n.length>0&&s()}}},o=()=>{i||(i=!0,queueMicrotask(()=>{i=!1,s()}))};return{enqueue(a,u,d){const l=n.findIndex(c=>c.drawKey===u);l>=0?n[l]={diffs:a,drawKey:u,onDisplayed:d}:(n.length>=Mo&&(n.shift(),lt()?.onQueueSuperseded()),n.push({diffs:a,drawKey:u,onDisplayed:d})),o()},reset(){t+=1,n.length=0},isLatestSeq(a){return a===t},getLatestSeq(){return t}}}function Po(e,t=2){const n=[],r=new Set;let i=0,s=!1,o=0;const a=(d,l)=>`${d}|${l}`,u=async()=>{if(!s){s=!0;try{for(;i<t&&n.length>0;){const d=n.shift(),l=a(d.displayAtMs,d.drawKey);if(r.has(l))continue;r.add(l),i+=1;const c=o;try{await e(d)}finally{if(r.delete(l),i-=1,c!==o)return}}}finally{s=!1,n.length>0&&i<t&&u()}}};return{enqueue(d){const l=a(d.displayAtMs,d.drawKey);r.has(l)||n.some(c=>a(c.displayAtMs,c.drawKey)===l)||(n.push(d),u())},isQueuedOrInflight(d,l){const c=a(d,l);return r.has(c)?!0:n.some(f=>a(f.displayAtMs,f.drawKey)===c)},reset(){o+=1,n.length=0,r.clear(),i=0},pendingCount(){return n.length+i}}}function Ro(){const e=new Map,t=n=>{n.bitmap.close()};return{has(n,r){return e.has(r)},markRendered(n,r,i,s,o){const a=e.get(r);a&&t(a),e.set(r,{displayAtMs:n,mouthKey:r,label:i,mouthPath:s,bitmap:o})},findForMouthKey(n){return e.get(n)??null},removeThrough(n){},pruneBefore(n){},reset(){for(const n of e.values())t(n);e.clear()},size(){return e.size}}}function Co(e,t,n,r,i,s){const o=os(t,n,r,i,Ot,s);if(o.length===0)return{ready:0,total:0};let a=0;for(const u of o)e.has(u.displayAtMs,u.mouthKey)&&(a+=1);return{ready:a,total:o.length}}function Do(e){const t=Ro(),n=yr(),r=new Set;let i="",s=null;const o=Po(async c=>{try{if(!e.supportsBufferedPlayback)return;const f=await e.compositePrefetchFrame(c.diffs,c.mouthKey);if(!f)return;t.markRendered(c.displayAtMs,c.mouthKey,c.label,c.mouthPath,f),lt()?.onBufferPrefetched()}finally{r.delete(c.mouthKey)}},xs),a=c=>{for(const f of c)t.has(f.displayAtMs,f.mouthKey)||r.has(f.mouthKey)||o.isQueuedOrInflight(0,f.mouthKey)||(r.add(f.mouthKey),o.enqueue({...f,displayAtMs:0,drawKey:f.mouthKey,diffs:{mouthPath:f.mouthPath,expressionPath:null}}))},u=(c,f,g,T=Cn)=>{if(!e.supportsBufferedPlayback)return;const m=Math.max(0,f-Ot*4),I=os(c,m,f+T,g,Ot,n);a(I)},d=(c,f)=>{i=c,s=f},l=(c,f)=>!(i!==c||s!==f);return{reset(){t.reset(),o.reset(),r.clear(),Le(n),i="",s=null,lt()?.onBufferReset()},setHoldPp(c){n.holdPp=c},markExternallyDisplayed(c,f){d(c.split("|")[0]??c,f)},warmupTick(c){e.supportsBufferedPlayback&&u(c.queue,0,c.availablePaths,Math.max(Cr,Cn))},isWarmupReady(c,f=Cr){if(!e.supportsBufferedPlayback)return!0;const{ready:g,total:T}=Co(t,c.queue,0,f,c.availablePaths,n);return T===0?!1:g/T>=Es},getBufferedFrameCount:()=>t.size(),tick(c){n.holdPp=c.playbackState.holdPp===!0;const f=lt();f?.onBufferTick(),u(c.queue,c.elapsedMs,c.availablePaths),t.pruneBefore(c.elapsedMs-Cn);const g=vr(c.queue,c.elapsedMs,c.availablePaths,n),T=un(g.diffPath),m=Vt({mouthPath:g.diffPath,expressionPath:c.expressionPath},c.expressionDrawKey),I=()=>(d(T,c.expressionPath),f?.onBufferHit(),{usedBuffer:!0,underrun:!1,held:!0});if(l(T,c.expressionPath)||c.playbackState.lastDrawnKey===m)return I();const L=t.findForMouthKey(T,c.elapsedMs,Ot);return L&&c.expressionPath!=null?(f?.onBufferHit(),{usedBuffer:!1,underrun:!1,held:!1}):L?(e.blitBitmap(c.canvas,L.bitmap),d(L.mouthKey,c.expressionPath),c.playbackState.lastDrawnKey=m,c.onStatus({diffPath:L.mouthPath,label:L.label}),c.onDisplayed(m,L.mouthPath,L.label),f?.onBufferHit(),{usedBuffer:!0,underrun:!1,held:!1}):(f?.onBufferUnderrun(),{usedBuffer:!1,underrun:!0,held:!1})}}}const Bo=`
5
+ `);Re=De.pop()??"";for(const ze of De)await Ve(ze);if(We){Re.trim()&&await Ve(Re.trim()),be===H&&!de.signal.aborted&&D&&M.markStreamComplete(n);break}}}catch(Oe){if(de.signal.aborted||!D)return;console.error("[realtimeVoice] avatar_speak failed:",Oe),e("error"),ne()}finally{P===de&&(P=null)}},q=p=>{const B=va(p);if(!B){console.warn("[realtimeVoice] server message with no payload",p);return}const re=String(B.type??"unknown");switch(re!=="avatar_metadata"&&re!=="avatar_audio_chunk"&&re!=="avatar_lipsync"&&re!=="avatar_interrupt"&&re!=="error"&&console.debug("[realtimeVoice] server message",re,B),re){case"avatar_metadata":S(B);break;case"avatar_audio_chunk":ue(B);break;case"avatar_lipsync":me(B);break;case"avatar_interrupt":Z(B);break;case"error":{const de=String(B.message??"Voice session error");console.error("[realtimeVoice] server error:",de),e("error"),ne();break}case"avatar_speak":x(B);break}},ge=()=>g||(T=new ga,g=new gs.PipecatClient({transport:new Jn.WebSocketTransport({mediaManager:T,serializer:new pa,recorderSampleRate:fr,playerSampleRate:mr}),enableCam:!1,enableMic:!0,callbacks:{onBotReady:()=>{f=!0,e("idle"),ne()},onDisconnected:()=>{f=!1,_()},onError:p=>{const B=typeof p=="object"&&p!==null&&"data"in p?String(p.data??p):String(p);console.error("[realtimeVoice]",B,p),e("error"),ne()},onTransportStateChanged:p=>{p==="error"&&(e("error"),ne())},onServerMessage:p=>{R(p)},onUserStartedSpeaking:()=>{},onBotStartedSpeaking:()=>{v(),ie("bot_started")},onBotStoppedSpeaking:()=>{D&&ye()},onUserTranscript:p=>{String(p.text??""),h.onUserTranscript?.(p.text,!!p.final)},onBotOutput:p=>{h.onBotOutput?.(p.text)}}}),g);return{setDeveloperToken:p=>{},setTtsEngineId:p=>{c=p,M.setStreamTtsEngineId(p)},getTtsEngineId:()=>c,speak:async(p,B)=>{const re=p.trim();if(re){if(!g||!f)throw new Error("Realtime voice is not connected. Call connect() first.");J(),await g.sendText(re)}},speakWithReply:async(p,B)=>{throw new Error("speakWithReply is not available on the voice session; use AiTwin.speakWithReply()")},stop:()=>{P?.abort(),P=null,H+=1,v(),J(),e("idle"),ne()},connect:async p=>{p&&(h={...h,...p});const B=st(h.ttsEngineId,h.voiceId);if(B.ttsEngineId&&(c=B.ttsEngineId,M.setStreamTtsEngineId(c)),h={...h,ttsEngineId:c,...B.voiceId?{voiceId:B.voiceId}:{}},!h.authToken?.trim())throw new Error("Voice connect requires authToken (same JWT as SSE Authorization Bearer).");e("loading");const re=ge();try{await re.initDevices()}catch(be){throw f=!1,e("idle"),ne(),Wt(be)?new Error("Microphone access denied"):be instanceof Error?be:new Error("Failed to initialize microphone")}const de=Ki({...h,ttsEngineId:c});console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):",de);try{await re.connect({wsUrl:de})}catch(be){if(f=!1,e("error"),ne(),Wt(be))throw e("idle"),new Error("Microphone access denied");const xe=be instanceof Error?be.message:"Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";throw new Error(`${xe} — ${de}`)}f=!0,e("idle"),ne()},disconnect:async()=>{if(ce(),Q=!1,oe=!1,b.length=0,k=!1,P?.abort(),P=null,H+=1,v(),g)try{await g.disconnect()}catch(p){console.error("[realtimeVoice] disconnect failed:",p)}f=!1,_()},isConnected:()=>f,unlockAudio:async()=>{await M.primeAudioContext()},getVisemeQueue:()=>n,getWordQueue:()=>r,getFullVisemeQueue:()=>i,getFullWordQueue:()=>s,getSentenceEmotions:()=>a,getStreamMood:()=>l,getPlaybackElapsedMs:z,isSpeaking:()=>M.isSpeaking(),isWarmingUp:()=>M.isWarmingUp(),releaseWarmupHold:()=>M.releaseWarmupHold(),setLipsyncWarmupHold:(p,B)=>{B&&(W=B),M.setLipsyncWarmupHoldEnabled(p)},getVoiceStream:()=>M.getVoiceStream(),setPlaybackSpeed:p=>{M.setPlaybackSpeed(p)},getPlaybackSpeed:()=>M.getPlaybackSpeed(),subscribe:p=>(N.add(p),()=>N.delete(p))}}function qi(e,t,n){return{width:e.crop?.source_width??t,height:e.crop?.source_height??n}}function Yi(e,t){const n=e.crop;return{x:t?.source_x??n?.x??0,y:t?.source_y??n?.y??0,width:t?.source_w??n?.width??e.frame_width??0,height:t?.source_h??n?.height??e.frame_height??0}}function Qi(e){return e?(e.w??0)>0&&(e.h??0)>0:!1}async function Bt(e,t){const n=await fetch(e);if(!n.ok)throw new Error(`Failed to load encrypted asset: ${e}`);const r=await n.arrayBuffer();return Us(r,t)}async function Xi(e,t){if(!t)throw new Error("Missing asset key for encrypted JSON");const n=await Bt(e,t),r=new TextDecoder().decode(n);return JSON.parse(r)}async function ba(e){const t=new Blob([e],{type:"image/webp"});return createImageBitmap(t)}async function Sa(e){const t=new Blob([e],{type:"image/webp"}),n=URL.createObjectURL(t);return new Promise((r,i)=>{const s=new Image;s.decoding="async",s.onload=()=>{URL.revokeObjectURL(n),r(s)},s.onerror=()=>{URL.revokeObjectURL(n),i(new Error("Failed to decode decrypted image"))},s.src=n})}async function Ta(e){const t=new Blob([e],{type:"image/png"});return createImageBitmap(t)}async function ka(e){const t=new Blob([e],{type:"image/png"}),n=URL.createObjectURL(t);return new Promise((r,i)=>{const s=new Image;s.decoding="async",s.onload=()=>{URL.revokeObjectURL(n),r(s)},s.onerror=()=>{URL.revokeObjectURL(n),i(new Error("Failed to decode decrypted PNG"))},s.src=n})}function Ea(e){const t=new Blob([e],{type:"video/mp4"});return URL.createObjectURL(t)}async function xa(e){const t=Ws();if(!zt())return t;if(!e)throw new Error("Missing asset key for encrypted idle video");const n=await Bt(t,e);return Ea(n)}function At(e,t){return`${e}/${t}_minus_sil.png`}const Ji=5;function Sn(e){const t=e?.composite_threshold;if(typeof t=="number"&&t>0)return t;const n=e?.crop?.content_threshold;return typeof n=="number"&&n>0?n:Ji}const Ia=["aa","E","I","O","U","PP","FF","DD","SS","TH","CH","RR","kk","nn"],qn=["t01_0.25","t02_0.50","t03_0.75"],Aa=qn.length,Wr=new Set(["aa__kk","aa__nn","aa__sil","CH__aa","CH__DD","CH__E","CH__FF","CH__I","CH__kk","CH__nn","CH__O","CH__PP","CH__RR","CH__sil","CH__SS","CH__TH","CH__U","DD__aa","DD__E","DD__FF","DD__I","DD__kk","DD__nn","DD__O","DD__PP","DD__RR","DD__sil","DD__SS","DD__TH","DD__U","E__aa","E__FF","E__I","E__kk","E__nn","E__O","E__PP","E__RR","E__sil","E__SS","E__TH","E__U","FF__aa","FF__I","FF__kk","FF__nn","FF__O","FF__PP","FF__RR","FF__sil","FF__SS","FF__TH","FF__U","I__aa","I__kk","I__nn","I__O","I__PP","I__RR","I__sil","I__SS","I__TH","I__U","kk__nn","kk__sil","nn__sil","O__aa","O__kk","O__nn","O__PP","O__RR","O__sil","O__SS","O__TH","O__U","PP__aa","PP__kk","PP__nn","PP__RR","PP__sil","PP__SS","PP__TH","PP__U","RR__aa","RR__kk","RR__nn","RR__sil","RR__SS","RR__TH","RR__U","SS__aa","SS__kk","SS__nn","SS__sil","SS__TH","SS__U","TH__aa","TH__kk","TH__nn","TH__sil","TH__U","U__aa","U__kk","U__nn","U__sil"]);function Hr(e,t){return`${e}__${t}`}function $r(e,t){return`pairs/${e}/${t}_minus_sil.png`}function Pt(e){return`${e}_minus_sil.png`}function hr(e,t){if(t.has(e))return e;const n=/^pairs\/([^/]+)\//.exec(e);if(!n)return null;const r=n[1].split("__")[1];if(!r||r==="sil")return null;const i=Pt(r);return t.has(i)?i:null}function Zi(e,t){return e.filter(n=>t.has(n))}function Ma(e,t,n){const r=pr(e,t);return Zi(r,n).length>0}function pr(e,t){if(e===t)return[];const n=[],r=Hr(e,t);if(Wr.has(r)){for(const s of qn)n.push($r(r,s));return n}const i=Hr(t,e);if(Wr.has(i)){for(let s=Aa-1;s>=0;s--)n.push($r(i,qn[s]));return n}return[]}function Pa(e){return e<16?[1]:e<24?[1]:e<35?[0,1,2]:[0,1,2]}function Ra(e,t){return Pa(t).map(r=>e[r]).filter(r=>r!=null)}function Ca(e,t,n,r){if(r<=0)return 0;const s=Math.max(1,n-t)/r,o=e-t,a=Math.floor(o/s);return Math.min(r-1,Math.max(0,a))}function es(e,t,n){if(!n)return`${e}/${t}`;const r=t.split("/").pop()??t,i=r.includes(".")?r.slice(0,r.lastIndexOf(".")):r;return`${e}/${i}.bin`}const Da=[.32,.24,.16,.08,0],Ba=Da.map(e=>`eyes_${e.toFixed(2)}`),Fa=["eyes_0.32","eyes_0.16","eyes_0.00"],La=["eyes_0.32","eyes_0.16","eyes_0.08","eyes_0.00"],Ge=2,Na=-20,ln=16,ts={fear:-20,anger:-20,sad:-10,neutral:0,love:0,happy:0,disgust:10};function Ht(e){return`brow_${e}`}function gt(e){return ts[e]}function et(e){return Math.max(Na,Math.min(ln,e))}function Oa(e,t){if(e===t)return[];const n=e<t,r=n?Ge:-Ge,i=[];let s=e;if(s%Ge!==0){const o=n?Math.ceil(s/Ge)*Ge:Math.floor(s/Ge)*Ge,a=et(o);if(n?a>=t:a<=t)return[t];s=a,i.push(s)}for(;;){const o=s+r;if(n?o>=t:o<=t){t!==s&&i.push(t);break}s=o,i.push(s)}return i}function zr(e){const t=et(e);if(t%Ge===0)return[t];const n=Math.floor(t/Ge)*Ge,r=Math.min(ln,n+Ge),i=et(n);return t-i<=r-t?i===r?[i]:[i,r]:i===r?[r]:[r,i]}const Ua=["fear","anger","sad","neutral","love","happy","disgust"],Va={fear:"fear",afraid:"fear",anxiety:"fear",anger:"anger",angry:"anger",mad:"anger",sad:"sad",sadness:"sad",sorrow:"sad",neutral:"neutral",calm:"neutral",love:"love",loving:"love",affection:"love",happy:"happy",happiness:"happy",joy:"happy",joyful:"happy",excited:"happy",disgust:"disgust",disgusted:"disgust"};function it(e,t="neutral"){const n=String(e??"").trim().toLowerCase();return n?Va[n]??t:t}function ns(e){return e===0?null:At(Ht(e),"eyes_standard")}function rs(e){return ns(gt(e))}const Wa={[-20]:"anger",[-10]:"sad",0:"neutral",10:"disgust"};function gr(e,t){if(!e)return null;if(t(e))return e;const n=e.match(/^brow_(-?\d+)\/(eyes_[^/]+)_minus_sil\.png$/);if(n){const i=Number(n[1]),s=n[2];for(const a of zr(i)){if(a===i)continue;const u=At(Ht(a),s);if(t(u))return u}const o=Wa[i];if(o){const a=At(o,s);if(t(a))return a}return null}const r=e.match(/^(fear|anger|sad|neutral|love|happy|disgust)\/(eyes_[^/]+)_minus_sil\.png$/);if(r){const i=r[1],s=r[2],o=gt(i);for(const a of[o,...zr(o)]){const u=At(Ht(a),s);if(t(u))return u}}return null}function jr(e,t){const n=Ht(gt(e));return!t||t==="eyes_standard"?rs(e):e==="neutral"&&t==="eyes_standard"?null:At(n,t)}function Ha(e,t){const n=[];for(const r of t)n.push(jr(e,r));for(let r=t.length-2;r>=0;r--)n.push(jr(e,t[r]));return n.push(rs(e)),n}function $a(e){return At(Ht(e),"eyes_standard")}function za(e,t){return Oa(e,t).map($a)}const ja=6;function Ka(e,t){for(const n of e)if(t>=n.wtime&&t<n.wtime+n.wduration)return n;return null}function Ga(e){const t=et(e);return t>=ln?ln:et(t+ja)}function qa(e,t,n){const r=et(n);return Ka(t,e)?.emphasis?Ga(r):r}function Kr(e){if(!e)return 0;const t=e.match(/^brow_(-?\d+)\//);if(!t?.[1])return null;const n=Number.parseInt(t[1],10);return Number.isFinite(n)?et(n):null}function Ze(e,t){return e+Math.random()*(t-e)}function Gr(){return Ze(14,32)}function Ya(e){if(e)return{holdMinMs:10,holdMaxMs:18,closedExtraMs:0};const t=Math.random();return t<.18?{holdMinMs:38,holdMaxMs:62,closedExtraMs:Ze(40,90)}:t<.42?{holdMinMs:26,holdMaxMs:44,closedExtraMs:Ze(12,36)}:t<.82?{holdMinMs:14,holdMaxMs:28,closedExtraMs:Math.random()<.35?Ze(8,22):0}:{holdMinMs:10,holdMaxMs:18,closedExtraMs:0}}function qr(e,t){const n=Ze(e.holdMinMs,e.holdMaxMs);return t?n+e.closedExtraMs:n}function Qa(e={}){const{initialDelayMs:t=800+Math.random()*1200,intervalMinMs:n=2200,intervalMaxMs:r=5800,doubleBlinkChance:i=.18,doubleBlinkGapMinMs:s=120,doubleBlinkGapMaxMs:o=220,fullBlinkChance:a=.75,postEmotionChangeDelayMs:u=380,minDwellMs:d=550}=e;let l="neutral",c="neutral",f=0,g=0,T=0,h={kind:"idle",nextBlinkAt:performance.now()+t},I=null,F="__eyes_open__|neutral|brow:0";function N(P=l){F=`${I??"__eyes_open__"}|${P}|brow:${g}`}function b(P,H=l){I=P;const Q=Kr(P);Q!=null?g=Q:P==null&&(g=0),N(H)}function k(P){return ns(P)}function V(P,H,Q){const oe=et(H),ae=et(Q);if(f=ae,oe===ae){b(k(ae));return}const te=[...za(oe,ae),k(ae)];h={kind:"browBlend",paths:te,index:0,holdUntil:P+Gr(),targetBrow:ae},b(te[0]??null)}function D(P,H){h={kind:"idle",nextBlinkAt:P+Ze(n,r)},c=H,b(k(f),H)}function se(P,H,Q){const oe=Q?Fa:Math.random()<a?Ba:La,ae=Ya(Q),te=oe.length-1;h={kind:"playing",paths:Ha(H,oe),index:0,holdUntil:P+qr(ae,te===0),after:"idle",blinkEmotion:H,pace:ae,closedFrameIndex:te},c=H,b(h.paths[0]??null,H)}function le(P){if(h.kind==="browBlend"){for(;P>=h.holdUntil&&h.index<h.paths.length-1;)h.index+=1,h.holdUntil=P+Gr();const H=h.paths[h.index]??null;b(H),P>=h.holdUntil&&h.index>=h.paths.length-1&&(g=h.targetBrow,f=h.targetBrow,b(k(h.targetBrow)),h={kind:"idle",nextBlinkAt:P+u});return}if(h.kind==="idle"){b(k(f),c),P>=h.nextBlinkAt&&se(P,c,!1);return}if(h.kind==="doublePause"){b(k(f),h.blinkEmotion),P>=h.resumeAt&&se(P,h.blinkEmotion,!0);return}if(h.kind==="playing"){for(;P>=h.holdUntil&&h.index<h.paths.length-1;)h.index+=1,h.holdUntil=P+qr(h.pace,h.index===h.closedFrameIndex);if(b(h.paths[h.index]??null,h.blinkEmotion),P>=h.holdUntil&&h.index>=h.paths.length-1){if(h.after==="resumeIdle"){D(P+u,l);return}if(h.after==="doublePause"){h={kind:"doublePause",resumeAt:P+Ze(s,o),blinkEmotion:h.blinkEmotion},b(k(f),h.blinkEmotion);return}if(h.after==="idle"&&Math.random()<i){h={kind:"doublePause",resumeAt:P+Ze(s,o),blinkEmotion:h.blinkEmotion},b(k(f),h.blinkEmotion);return}D(P,h.blinkEmotion)}}}return{advance:le,setTargetEmotion(P,H){const Q=it(P,l);if(Q===l)return;const oe=performance.now();!H?.force&&d>0&&!(l==="neutral"&&Q!=="neutral")&&oe-T<d||(l=Q,c=Q,T=oe)},setTargetEyebrow(P){const H=et(P);if(H===f)return;const Q=performance.now(),oe=h.kind==="browBlend"?Kr(I)??g:g;f=H,!(H===oe&&h.kind!=="browBlend")&&(h.kind==="playing"||h.kind==="doublePause"||V(Q,oe,H))},snapToEmotion(P){const H=it(P),Q=gt(H);l=H,c=H,f=Q,g=Q,T=performance.now(),h={kind:"idle",nextBlinkAt:performance.now()+Ze(4e3,7e3)},b(k(Q),H)},getTargetEmotion:()=>l,getTargetEyebrow:()=>f,getExpressionPath:()=>I,getDrawKey:()=>F,reset(){l="neutral",c="neutral",f=0,g=0,T=0,D(performance.now()+Ze(n,r),"neutral")}}}function Xa(){const e=document.createElement("video");e.muted=!0,e.playsInline=!0,e.preload="auto",e.setAttribute("playsinline",""),e.loop=!0;let t=!1,n=null,r="";const i=()=>{n&&(URL.revokeObjectURL(n),n=null)};return{async load(s){t&&r===s||(i(),t=!1,r=s,s.startsWith("blob:")&&(n=s),await new Promise((o,a)=>{const u=()=>{const d=e.duration;if(!Number.isFinite(d)||d<=0){a(new Error(`Idle video has invalid duration: ${s}`));return}t=!0,e.currentTime=0,o()};e.addEventListener("loadeddata",u,{once:!0}),e.addEventListener("error",()=>a(new Error(`Failed to load idle video: ${s}`)),{once:!0}),e.src=s,e.load()}))},isReady(){return t},getVideo(){return e},setActive(s){t&&(s?e.paused&&e.play().catch(()=>{}):e.paused||e.pause())},setLooping(s){e.loop=s},restart(){t&&(e.currentTime=0)},dispose(){e.pause(),e.removeAttribute("src"),e.load(),i(),t=!1,r=""}}}const Ja=50,is=400;function Za(e,t,n){return e.filter(r=>{const i=r.queueIndex??-1;return i>=t&&i<=n})}function eo(e,t){const n=[];for(const r of e){const i=Za(t,r.start_word,r.end_word);if(i.length===0)continue;const s=i[0],o=i[i.length-1];n.push({sentence:r,startMs:Math.max(0,s.wtime-Ja),endMs:o.wtime+o.wduration+is})}return n.sort((r,i)=>r.startMs-i.startMs)}function to(e,t,n,r="neutral"){const i=it(r);if(t.length===0)return i;if(n.length===0||e<n[0].wtime)return it(t[0]?.emotion,i);const s=eo(t,n);let o=null;for(const d of s)e>=d.startMs&&e<=d.endMs&&(o=d);if(o)return it(o.sentence.emotion,i);const a=n[n.length-1],u=a.wtime+a.wduration;return e>u+is?it(t[t.length-1]?.emotion,i):i}function wr(e,t,n,r,i=Ji){const s=new OffscreenCanvas(n,r),o=s.getContext("2d",{willReadFrequently:!0});o.drawImage(e,0,0,n,r);const a=o.getImageData(0,0,n,r),u=o.createImageData(n,r);u.data.set(a.data);const l=new OffscreenCanvas(n,r).getContext("2d",{willReadFrequently:!0});for(const c of t){l.clearRect(0,0,n,r),l.drawImage(c,0,0,n,r);const f=l.getImageData(0,0,n,r);for(let g=0;g<u.data.length;g+=4){const T=f.data[g],h=f.data[g+1],I=f.data[g+2];f.data[g+3]>0&&Math.max(T,h,I)>i&&(u.data[g]=T,u.data[g+1]=h,u.data[g+2]=I,u.data[g+3]=255)}}return o.putImageData(u,0,0),s}let mt=null,Rt=null;const un=new Map;function no(){mt=null,Rt=null,un.clear()}_i(no);function ro(e){return e??Rt??void 0}function yr(e){return new Promise((t,n)=>{const r=new Image;r.decoding="async",r.onload=()=>t(r),r.onerror=()=>n(new Error(`Failed to load image: ${e}`)),r.src=e})}async function io(e){return zt()?ka(await Bt(cn(),e??"")):yr(cn())}async function Yr(e,t,n,r){const i=n?await Xi(e,r):await fetch(e).then(d=>{if(!d.ok)throw new Error(`Failed to load ${e}`);return d.json()}),s=i.sheets?.[0];if(!s?.path)throw new Error(`${e} has no sheets[0].path`);const o=n?await Sa(await Bt(es(t,s.path,n),r??"")):await yr(`${t}/${s.path}`),a=new Map;for(const d of i.cells??[])d.path&&a.set(d.path,d);const u=new Map;for(const d of i.sheets??[])u.set(d.index,d);return{atlas:o,atlasMeta:i,cellByPath:a,sheetByIndex:u,diffBase:t}}async function Yn(e){const t=ro(e),n=zt();if(n&&!t)throw new Error("Encrypted assets enabled but no key provided");if(mt&&(!n||Rt===(t??null))){const s=await mt;return{...Ct(s),visemePaths:[...s.viseme.cellByPath.keys()]}}Rt=t??null,un.clear();const r=bi();mt=(async()=>{const s=await io(t),o=await Yr(Si(),r,n,t);let a=null;try{a=await Yr(Ti(),r,n,t)}catch{a=null}return{sil:s,viseme:o,expression:a}})();const i=await mt;return{...Ct(i),visemePaths:[...i.viseme.cellByPath.keys()]}}async function vr(e){if(await Yn(e),!mt)throw new Error("Assets not loaded");return mt}function Ct(e){return qi(e.viseme.atlasMeta,e.sil.naturalWidth,e.sil.naturalHeight)}function so(e,t){const n=document.createElement("canvas");return n.width=t.w,n.height=t.h,n.getContext("2d",{willReadFrequently:!0}).drawImage(e.atlas,t.x,t.y,t.w,t.h,0,0,t.w,t.h),n}async function dn(e,t,n){const r=`${t.diffBase}::${n}`,i=un.get(r);if(i)return i;const s=(async()=>{const o=t.cellByPath.get(n);if(!o)throw new Error(`No atlas cell for path: ${n}`);const a=Yi(t.atlasMeta,o),u=Ct(e);if(Qi(o)){const c=so(t,o),f=document.createElement("canvas");f.width=u.width,f.height=u.height;const g=f.getContext("2d");return g.fillStyle="#000",g.fillRect(0,0,u.width,u.height),g.drawImage(c,a.x,a.y),f}const d=`${t.diffBase}/${n}`,l=await yr(d);if(l.naturalWidth===u.width&&l.naturalHeight===u.height)return l;throw new Error(`Diff PNG wrong size for ${n}`)})();return un.set(r,s),s}async function Ln(e,t,n,r,i,s){if(i){const{width:g,height:T}=i;if(s&&!s()||t.readyState<HTMLMediaElement.HAVE_CURRENT_DATA)return;const h=document.createElement("canvas");h.width=g,h.height=T;const I=h.getContext("2d");if(I.clearRect(0,0,g,T),I.drawImage(t,0,0,g,T),s&&!s())return;(e.width!==g||e.height!==T)&&(e.width=g,e.height=T);const F=e.getContext("2d");F.clearRect(0,0,g,T),F.drawImage(h,0,0);return}const o=await vr(),a=Sn(o.viseme.atlasMeta),{width:u,height:d}=Ct(o);(e.width!==u||e.height!==d)&&(e.width=u,e.height=d);const l=[],c=e.getContext("2d");if(c.clearRect(0,0,u,d),t.readyState<HTMLMediaElement.HAVE_CURRENT_DATA){const g=o.sil;c.drawImage(g,0,0,u,d);return}if(l.length===0){c.drawImage(t,0,0,u,d);return}const f=wr(t,l,u,d,a);c.drawImage(f,0,0)}async function Qn(e,t,n){const r=await vr(Rt??void 0),i=Sn(r.viseme.atlasMeta),{width:s,height:o}=Ct(r);(e.width!==s||e.height!==o)&&(e.width=s,e.height=o);const a=[];if(t.mouthPath){const l=new Set(r.viseme.cellByPath.keys()),c=hr(t.mouthPath,l)??(l.has(t.mouthPath)?t.mouthPath:null);c&&a.push(await dn(r,r.viseme,c))}if(t.expressionPath&&r.expression){const l=gr(t.expressionPath,c=>r.expression.cellByPath.has(c));l&&a.push(await dn(r,r.expression,l))}const u=e.getContext("2d");if(u.clearRect(0,0,s,o),a.length===0){u.drawImage(r.sil,0,0,s,o);return}const d=wr(r.sil,a,s,o,i);u.drawImage(d,0,0)}async function ao(e,t){const n=await vr(Rt??void 0),r=Sn(n.viseme.atlasMeta),{width:i,height:s}=Ct(n),o=[];if(e.mouthPath){const l=new Set(n.viseme.cellByPath.keys()),c=hr(e.mouthPath,l)??(l.has(e.mouthPath)?e.mouthPath:null);c&&o.push(await dn(n,n.viseme,c))}if(e.expressionPath&&n.expression){const l=gr(e.expressionPath,c=>n.expression.cellByPath.has(c));l&&o.push(await dn(n,n.expression,l))}const a=new OffscreenCanvas(i,s),u=a.getContext("2d");if(o.length===0)return u.drawImage(n.sil,0,0,i,s),a.transferToImageBitmap();const d=wr(n.sil,o,i,s,r);return u.drawImage(d,0,0),a.transferToImageBitmap()}async function oo(e){await Qn(e,{mouthPath:null,expressionPath:null})}const nn={fontFamily:"system-ui, -apple-system, sans-serif",fontSize:"clamp(0.875rem, 2.5vw, 1.125rem)",color:"rgba(255,255,255,0.85)"};function co(e){return e.length===0?"":e.map(t=>t.word).join(" ")}function lo(e){if(!e)return{enabled:!1,style:nn};const t=e===!0?{}:e;return{enabled:!0,style:{fontFamily:t.fontFamily??nn.fontFamily,fontSize:t.fontSize??nn.fontSize,color:t.color??nn.color}}}const uo={position:"absolute",bottom:0,left:0,right:0,display:"flex",justifyContent:"center",padding:"0.75rem 1rem",pointerEvents:"none"},fo={display:"inline-block",maxWidth:"100%",padding:"0.45rem 0.85rem",borderRadius:"0.75rem",background:"rgba(0,0,0,0.55)",lineHeight:1.4,textAlign:"center"},mo={display:"-webkit-box",WebkitLineClamp:2,WebkitBoxOrient:"vertical",overflow:"hidden",maxWidth:"100%",margin:0};function ho({text:e,style:t}){return O.jsx("div",{style:uo,"aria-live":"polite","aria-atomic":"true",children:O.jsx("div",{style:{...fo,fontFamily:t.fontFamily,fontSize:t.fontSize,color:t.color},children:O.jsx("p",{style:mo,children:e})})})}const po={inset:0,overflow:"hidden",pointerEvents:"none",position:"absolute",zIndex:3},go={filter:"blur(22px)",height:"100%",inset:0,objectFit:"cover",objectPosition:"top",position:"absolute",transform:"scale(1.08)",width:"100%"},wo={backdropFilter:"blur(10px)",WebkitBackdropFilter:"blur(10px)",inset:0,position:"absolute"};function yo({thumbnailUrl:e}){const[t,n]=m.useState(!1);m.useEffect(()=>{n(!1)},[e]);const r=!!e&&t;return O.jsxs("div",{style:po,"aria-hidden":!0,children:[e?O.jsx("img",{src:e,alt:"",style:{...go,opacity:r?1:0,transition:"opacity 0.45s ease"},onLoad:()=>n(!0),onError:()=>n(!1)}):null,O.jsx("div",{style:{...wo,background:r?"rgba(0, 0, 0, 0.28)":"rgba(10, 10, 11, 0.92)"}})]})}let ss=null;function Qr(e){ss=e}function ut(){return ss}function vo(e){return`${e.from}→${e.to}@${e.fromVtime}`}function Xr(){return{rafTicksSpeaking:0,rafTicksDeduped:0,drawsRequested:0,drawsDisplayed:0,queueSuperseded:0,workerStale:0,rifeFramesOmittedByGap:0,transitionFramesSampled:0,transitionFramesDisplayed:0,transitionFramesMissed:0,bufferTicks:0,bufferHits:0,bufferUnderruns:0,bufferPrefetched:0}}function _o(){let e=Xr();const t=new Set,n=new Set,r=new Set;let i="",s=-1,o="";const a=()=>{e=Xr(),t.clear(),n.clear(),r.clear(),i="",s=-1,o=""},u=l=>{t.add(l)},d=l=>{l&&r.add(l)};return{reset:a,onRafTickSpeaking(){e.rafTicksSpeaking+=1},onDrawDeduped(){e.rafTicksDeduped+=1},onDrawRequested(){e.drawsRequested+=1},onQueueSuperseded(){e.queueSuperseded+=1},onWorkerStale(){e.workerStale+=1},onTransitionSample(l,c){const f=vo(l);e.transitionFramesSampled+=1,f!==i&&(i=f,s=-1,o!==f&&(o=f,e.rifeFramesOmittedByGap+=l.omittedByGap)),c&&n.add(c),s>=0&&l.frameIdx>s+1&&(e.transitionFramesMissed+=l.frameIdx-s-1),s=l.frameIdx},onBufferTick(){e.bufferTicks+=1},onBufferHit(){e.bufferHits+=1},onBufferUnderrun(){e.bufferUnderruns+=1},onBufferPrefetched(){e.bufferPrefetched+=1},onBufferReset(){},onDrawDisplayed(l,c){if(e.drawsDisplayed+=1,l.transitionMeta){e.transitionFramesDisplayed+=1;const g=l.transitionMeta;u(g.fromVtime),u(g.toVtime),d(l.mouthPath);return}if(l.label.includes("→")){e.transitionFramesDisplayed+=1;return}const f=l.label.trim();if(!(!f||f==="sil")){for(const g of c)if(g.viseme===f&&l.elapsedMs>=g.vtime&&l.elapsedMs<g.vtime+g.vduration){u(g.vtime);break}}},snapshot(l,c){let f=0,g=0;for(const N of l)if(N.viseme!=="sil"){if(N.vtime>=c)break;f+=1,t.has(N.vtime)&&(g+=1)}const T=f>0?Math.round(g/f*100):100,h=e.queueSuperseded+e.workerStale+Math.max(0,e.drawsRequested-e.drawsDisplayed),I=f-g,F=e.bufferTicks>0?Math.round(e.bufferHits/e.bufferTicks*100):100;return{...e,bufferHitRate:F,renderLoss:h,visemeSlotsTotal:f,visemeSlotsCovered:g,visemeCoveragePct:T,transitionFramesUniqueSampled:n.size,transitionFramesUniquePainted:r.size,drawsDeduped:e.rafTicksDeduped,renderSkips:e.rafTicksDeduped+h,visemesShown:g,visemesSkipped:I,rifeTransitionFramesSkipped:e.transitionFramesMissed}}}}const as=er,bo=500,Jr=500,So=450,Nn=.05,To=100,ko=600,os="__sil__";function Zr(e){return e.lastDrawnKey?e.lastDrawnKey.split("|")[0]===os:!1}function _r(){return{lastDrawnKey:""}}function Le(e){e.lastDrawnKey="",e.transitionCache=void 0}function Eo(e,t){if(t?.transitionCache&&t.transitionCache.queueLength===e.length)return t.transitionCache.transitions;const n=Oi(e);return t&&(t.transitionCache={queueLength:e.length,transitions:n}),n}function xo(e,t){let n="sil";for(const r of e)r.viseme!=="sil"&&(t!=null&&r.vtime>t||(n=r.viseme));return n}function Io(e,t,n,r=120){if(n!=="sil")return n;const i=ji(e,t,r);return i!=="sil"?i:xo(e,t)}function Ao(e,t){return e==="sil"?!1:t?Ma(e,"sil",t):pr(e,"sil").length>0}function Mo(){return as+ko}function fn(e){return e??os}function br(e,t,n,r,i=To){if(e.length===0)return{diffPath:null,label:"sil"};if(t>=ft(e))return{diffPath:null,label:"sil"};const s=r?.holdPp===!0,o=Eo(e,r),a=ca(o,t,s);if(a){const l=Ui(a.fromVtime,a.toVtime,a.from,a.to,s);if(t>=l.transStart&&t<l.transEnd){let c=pr(a.from,a.to);n&&(c=Zi(c,n));const f=Ra(c,l.effectiveGapMs);if(f.length>0){const g=Ca(t,l.transStart,l.transEnd,f.length);return{diffPath:f[g]??f[f.length-1],label:`${a.from}→${a.to}`,transitionMeta:{from:a.from,to:a.to,fromVtime:a.fromVtime,toVtime:a.toVtime,frameIdx:g,frameCount:f.length,fullFrameCount:c.length,omittedByGap:Math.max(0,c.length-f.length)}}}return{diffPath:Pt(a.to),label:a.to}}}const u=yn(e);let d=Bi(u,t);return d==="sil"&&(d=ji(e,t,i)),d==="sil"?{diffPath:null,label:"sil"}:{diffPath:Pt(d),label:d}}function $t(e,t){const n=t??fn(e.expressionPath);return`${fn(e.mouthPath)}|${n}`}function ei(e,t,n,r,i,s,o,a,u,d){const l=br(t,n,u,s),c={mouthPath:l.diffPath,expressionPath:r},f=$t(c,i),g=ut();if(l.transitionMeta&&g?.onTransitionSample(l.transitionMeta,l.diffPath),f===s.lastDrawnKey){g?.onDrawDeduped();return}g?.onDrawRequested(),a(l),s.lastDrawnKey=f,o(e,c,f,T=>{T===f&&(s.lastDrawnKey=f)})}function On(e,t,n,r,i,s,o=null,a=null){const u={mouthPath:a,expressionPath:t},d=o!=null?`|idle@${Math.round(o*1e3)}`:"",l=$t(u,n)+d;!(o!=null)&&l===r.lastDrawnKey||(s(o!=null?t?"idle · blink":"idle":t?"blink":"sil"),i(e,u,l,f=>{f===l&&(r.lastDrawnKey=l)}))}function cs(e,t,n,r,i=Vt,s){if(n<=t||e.length===0)return[];const o=s??_r(),a=[];let u="";for(let d=t;d<n;d+=i){const l=br(e,d,r,o),c=fn(l.diffPath);c!==u&&(u=c,a.push({displayAtMs:d,mouthPath:l.diffPath,mouthKey:c,label:l.label}))}return a}const Po=3;function Sr(e){let t=0;const n=[];let r=!1,i=!1;const s=async()=>{if(!r){r=!0;try{for(;n.length>0;){const a=n.shift(),u=++t;await e(a.diffs,u,a)&&u===t&&a.onDisplayed?.(a.drawKey)}}finally{r=!1,n.length>0&&s()}}},o=()=>{i||(i=!0,queueMicrotask(()=>{i=!1,s()}))};return{enqueue(a,u,d){const l=n.findIndex(c=>c.drawKey===u);l>=0?n[l]={diffs:a,drawKey:u,onDisplayed:d}:(n.length>=Po&&(n.shift(),ut()?.onQueueSuperseded()),n.push({diffs:a,drawKey:u,onDisplayed:d})),o()},reset(){t+=1,n.length=0},isLatestSeq(a){return a===t},getLatestSeq(){return t}}}function Ro(e,t=2){const n=[],r=new Set;let i=0,s=!1,o=0;const a=(d,l)=>`${d}|${l}`,u=async()=>{if(!s){s=!0;try{for(;i<t&&n.length>0;){const d=n.shift(),l=a(d.displayAtMs,d.drawKey);if(r.has(l))continue;r.add(l),i+=1;const c=o;try{await e(d)}finally{if(r.delete(l),i-=1,c!==o)return}}}finally{s=!1,n.length>0&&i<t&&u()}}};return{enqueue(d){const l=a(d.displayAtMs,d.drawKey);r.has(l)||n.some(c=>a(c.displayAtMs,c.drawKey)===l)||(n.push(d),u())},isQueuedOrInflight(d,l){const c=a(d,l);return r.has(c)?!0:n.some(f=>a(f.displayAtMs,f.drawKey)===c)},reset(){o+=1,n.length=0,r.clear(),i=0},pendingCount(){return n.length+i}}}function Co(){const e=new Map,t=n=>{n.bitmap.close()};return{has(n,r){return e.has(r)},markRendered(n,r,i,s,o){const a=e.get(r);a&&t(a),e.set(r,{displayAtMs:n,mouthKey:r,label:i,mouthPath:s,bitmap:o})},findForMouthKey(n){return e.get(n)??null},removeThrough(n){},pruneBefore(n){},reset(){for(const n of e.values())t(n);e.clear()},size(){return e.size}}}function Do(e,t,n,r,i,s){const o=cs(t,n,r,i,Vt,s);if(o.length===0)return{ready:0,total:0};let a=0;for(const u of o)e.has(u.displayAtMs,u.mouthKey)&&(a+=1);return{ready:a,total:o.length}}function Bo(e){const t=Co(),n=_r(),r=new Set;let i="",s=null;const o=Ro(async c=>{try{if(!e.supportsBufferedPlayback)return;const f=await e.compositePrefetchFrame(c.diffs,c.mouthKey);if(!f)return;t.markRendered(c.displayAtMs,c.mouthKey,c.label,c.mouthPath,f),ut()?.onBufferPrefetched()}finally{r.delete(c.mouthKey)}},xs),a=c=>{for(const f of c)t.has(f.displayAtMs,f.mouthKey)||r.has(f.mouthKey)||o.isQueuedOrInflight(0,f.mouthKey)||(r.add(f.mouthKey),o.enqueue({...f,displayAtMs:0,drawKey:f.mouthKey,diffs:{mouthPath:f.mouthPath,expressionPath:null}}))},u=(c,f,g,T=Bn)=>{if(!e.supportsBufferedPlayback)return;const h=Math.max(0,f-Vt*4),I=cs(c,h,f+T,g,Vt,n);a(I)},d=(c,f)=>{i=c,s=f},l=(c,f)=>!(i!==c||s!==f);return{reset(){t.reset(),o.reset(),r.clear(),Le(n),i="",s=null,ut()?.onBufferReset()},setHoldPp(c){n.holdPp=c},markExternallyDisplayed(c,f){d(c.split("|")[0]??c,f)},warmupTick(c){e.supportsBufferedPlayback&&u(c.queue,0,c.availablePaths,Math.max(Dr,Bn))},isWarmupReady(c,f=Dr){if(!e.supportsBufferedPlayback)return!0;const{ready:g,total:T}=Do(t,c.queue,0,f,c.availablePaths,n);return T===0?!1:g/T>=Es},getBufferedFrameCount:()=>t.size(),tick(c){n.holdPp=c.playbackState.holdPp===!0;const f=ut();f?.onBufferTick(),u(c.queue,c.elapsedMs,c.availablePaths),t.pruneBefore(c.elapsedMs-Bn);const g=br(c.queue,c.elapsedMs,c.availablePaths,n),T=fn(g.diffPath),h=$t({mouthPath:g.diffPath,expressionPath:c.expressionPath},c.expressionDrawKey),I=()=>(d(T,c.expressionPath),f?.onBufferHit(),{usedBuffer:!0,underrun:!1,held:!0});if(l(T,c.expressionPath)||c.playbackState.lastDrawnKey===h)return I();const F=t.findForMouthKey(T,c.elapsedMs,Vt);return F&&c.expressionPath!=null?(f?.onBufferHit(),{usedBuffer:!1,underrun:!1,held:!1}):F?(e.blitBitmap(c.canvas,F.bitmap),d(F.mouthKey,c.expressionPath),c.playbackState.lastDrawnKey=h,c.onStatus({diffPath:F.mouthPath,label:F.label}),c.onDisplayed(h,F.mouthPath,F.label),f?.onBufferHit(),{usedBuffer:!0,underrun:!1,held:!1}):(f?.onBufferUnderrun(),{usedBuffer:!1,underrun:!0,held:!1})}}}const Fo=`
6
6
  in vec2 aPosition;
7
7
  out vec2 vTextureCoord;
8
8
 
@@ -30,7 +30,7 @@ void main(void)
30
30
  gl_Position = filterVertexPosition();
31
31
  vTextureCoord = filterTextureCoord();
32
32
  }
33
- `,Fo=`
33
+ `,Lo=`
34
34
  in vec2 vTextureCoord;
35
35
  out vec4 finalColor;
36
36
 
@@ -44,5 +44,5 @@ void main(void)
44
44
  float keep = step(uThreshold + 1e-6, peak) * color.a;
45
45
  finalColor = vec4(color.rgb, keep);
46
46
  }
47
- `;function ei(e=5){const t=new ye.Filter({glProgram:ye.GlProgram.from({vertex:Bo,fragment:Fo,name:"threshold-key-filter"}),resources:{thresholdUniforms:{uThreshold:{value:e/255,type:"f32"}}}});return t.setThreshold=n=>{t.resources.thresholdUniforms.uniforms.uThreshold=n/255},t.getThreshold=()=>Math.round(t.resources.thresholdUniforms.uniforms.uThreshold*255),t}function Lo(e,t,n){return e>n||t>n}function No(e){return e?e.getParameter(e.MAX_TEXTURE_SIZE):4096}function Oo(e,t){return t!=null&&t>0?Math.floor(t):No(e)}function Uo(e,t){const n=document.createElement("canvas");n.width=t.w,n.height=t.h;const r=n.getContext("2d");if(!r)throw new Error("Failed to acquire 2d context for atlas cell crop");return r.drawImage(e,t.x,t.y,t.w,t.h,0,0,t.w,t.h),n}function cs(e){return fetch(e).then(t=>{if(!t.ok)throw new Error(`Failed to load image: ${e}`);return t.blob()}).then(t=>createImageBitmap(t))}async function ti(e,t,n,r,i){const s=n?await Qi(e,i):await fetch(e).then(c=>{if(!c.ok)throw new Error(`Failed to load ${e}`);return c.json()}),o=s.sheets?.[0];if(!o?.path)throw new Error(`${e} has no sheets[0].path`);const a=new Map,u=async(c,f)=>{const g=Zi(t,f,n),T=n?await _a(await Dt(g,i??"")):await cs(g);if(Lo(T.width,T.height,r))return console.info(`[PixiVisemeCompositor] Sheet ${f} (${T.width}x${T.height}) exceeds MAX_TEXTURE_SIZE=${r}; using per-cell crops.`),{mode:"cells",bitmap:T};const m=ye.Texture.from(T);return m.source.scaleMode="nearest",{mode:"full",texture:m}};a.set(o.index,await u(o.index,o.path));for(const c of s.sheets??[])c.index!==o.index&&c.path&&a.set(c.index,await u(c.index,c.path));const d=new Map;for(const c of s.cells??[])c.path&&d.set(c.path,c);const l=new Map;for(const c of s.sheets??[])l.set(c.index,c);return{atlasMeta:s,cellByPath:d,sheetByIndex:l,diffBase:t,sheets:a}}async function Vo(e,t={}){const n=new ye.Application;try{await n.init({canvas:e,width:842,height:1264,preference:"webgl",autoStart:!1,antialias:!1,resolution:1,autoDensity:!1,backgroundAlpha:1,backgroundColor:0,powerPreference:"high-performance"})}catch(_){throw n.destroy({removeView:!1}),_}if(n.renderer.type!==ye.RendererType.WEBGL)throw n.destroy({removeView:!1}),new Error("Pixi did not initialize a WebGL renderer");const r=Oo(n.renderer.gl,t.maxTextureSize);let i=!1,s=!1,o=!1;const a=n.canvas,u=()=>{i=!0,!s&&!o&&(s=!0,t.onWebGlContextLost?.())};a.addEventListener("webglcontextlost",u);const d=new ye.Sprite(ye.Texture.EMPTY),l=new ye.Sprite(ye.Texture.EMPTY);l.visible=!1,l.alpha=0;const c=new ye.Sprite(ye.Texture.EMPTY),f=new ye.Sprite(ye.Texture.EMPTY),g=new ye.Sprite(ye.Texture.EMPTY);g.visible=!1;const T=ei(5),m=ei(5);c.filters=[T],f.filters=[m],n.stage.addChild(d),n.stage.addChild(l),n.stage.addChild(c),n.stage.addChild(f),n.stage.addChild(g);let I=null,L=null,O=null,b=null,E=null,V=null,D=null,oe=842,pe=1264,P=null,z=5;const Q=new Map;let le=null,ce=null,re=null;const ue=vi(()=>{Q.clear(),V=null,D=null,I=null,P=null}),we=(_,y)=>{oe=_,pe=y,n.renderer.resize(_,y),(e.width!==_||e.height!==y)&&(e.width=_,e.height=y)},se=()=>{c.visible=!1,f.visible=!1,c.texture=ye.Texture.EMPTY,f.texture=ye.Texture.EMPTY,le=null,ce=null},W=()=>{E=null,l.visible=!1,l.alpha=0},M=()=>{d.visible=!0,d.width=oe,d.height=pe,d.x=0,d.y=0},q=()=>{l.width=oe,l.height=pe,l.x=0,l.y=0},R=()=>{g.visible=!1,W(),I&&(d.texture=I,M())},H=()=>{E&&R()},X=_=>{if(o||i||!I){R();return}const y=Math.max(0,_);if(!L||y<=0){R();return}se(),g.visible=!1,d.texture=I,M(),l.texture=L,q(),l.alpha=1,l.visible=!0,E={startMs:performance.now(),durationMs:y},Z()},ae=(_=performance.now())=>{if(!E)return!1;if(o||i)return H(),!1;const y=(_-E.startMs)/Math.max(1,E.durationMs);if(y>=1)return R(),Z(),!1;const S=Math.min(1,Math.max(0,y)),K=S*S*(3-2*S);return l.alpha=1-K,l.visible=!0,Z(),!0},v=()=>{if(O){try{O.resource=null}catch{}O.destroy(),O=null}L&&(L.destroy(!1),L=null),b=null},N=(_,y)=>{const S=_.cellByPath.get(y);if(!S||!Yi(S))return null;const K=`${_.diffBase}::${y}`;let ee=Q.get(K);if(!ee){const x=S.sheet??0,J=_.sheets.get(x);if(!J)return null;let ne,te=!1;if(J.mode==="full")ne=new ye.Texture({source:J.texture.source,frame:new ye.Rectangle(S.x,S.y,S.w,S.h)});else{const Se=Uo(J.bitmap,S);ne=ye.Texture.from(Se),ne.source.scaleMode="nearest",te=!0}ee={texture:ne,ownsSource:te},Q.set(K,ee)}return{texture:ee.texture,cell:S}},U=(_,y,S,K)=>{if(!K||!S)return _.visible=!1,_.texture=ye.Texture.EMPTY,!1;const ee=N(S,K);if(!ee)return _.visible=!1,_.texture=ye.Texture.EMPTY,!1;const x=qi(S.atlasMeta,ee.cell);return y.setThreshold(z),_.texture=ee.texture,_.x=x.x,_.y=x.y,_.width=ee.cell.w,_.height=ee.cell.h,_.visible=!0,!0},Y=_=>{if(!_||!V)return null;const y=P??new Set(V.cellByPath.keys());return fr(_,y)??(y.has(_)?_:null)},G=_=>{g.visible=!1,R();const y=Y(_.mouthPath),S=_.expressionPath&&D?hr(_.expressionPath,K=>D.cellByPath.has(K)):null;y!==le?(U(c,T,V,y),le=y):y?c.visible=!0:c.visible=!1,S!==ce?(U(f,m,D,S),ce=S):S?f.visible=!0:f.visible=!1},Z=()=>{if(!(o||i||document.hidden))try{n.render()}catch{}};return{async loadAssets(_){if(o)return{width:oe,height:pe,visemePaths:[]};const y=Wt();if(y&&!_)throw new Error("Encrypted assets enabled but no key provided");const S=y?await Sa(await Dt(an(),_??"")):await cs(an());I&&I.destroy(!0),I=ye.Texture.from(S),I.source.scaleMode="linear";const K=_i();V=await ti(bi(),K,y,r,_);try{D=await ti(Si(),K,y,r,_)}catch(x){console.warn("[PixiVisemeCompositor] expression atlas unavailable:",x),D=null}const ee=Gi(V.atlasMeta,S.width,S.height);return we(ee.width,ee.height),z=_n(V.atlasMeta),T.setThreshold(z),m.setThreshold(z),P=new Set(V.cellByPath.keys()),Q.clear(),R(),se(),Z(),{width:ee.width,height:ee.height,visemePaths:[...V.cellByPath.keys()]}},getVisemeAtlasPaths(){return P},drawLiveFrame(_){o||i||!I||(H(),G(_),Z())},drawIdleVideo(_,y){o||i||document.hidden||y&&!y()||_.readyState<HTMLMediaElement.HAVE_CURRENT_DATA||(E=null,se(),g.visible=!1,I&&(d.texture=I,M()),!L||b!==_?(v(),O=new ye.VideoSource({resource:_,autoPlay:!1,updateFPS:0}),O.autoUpdate=!1,L=new ye.Texture({source:O}),b=_):L.source.update(),l.texture=L,q(),l.alpha=1,l.visible=!0,Z())},beginIdleToSilCrossfade:X,tickIdleToSilCrossfade:ae,finishIdleToSilCrossfade:H,isIdleToSilCrossfading(){return E!=null},renderSilOnly(){o||i||!I||(se(),R(),Z())},blitBitmap(_){o||i||(H(),se(),re&&(re.destroy(!0),re=null),re=ye.Texture.from(_),g.texture=re,g.visible=!0,g.x=0,g.y=0,g.width=oe,g.height=pe,d.visible=!1,Z())},async compositePrefetchFrame(_){return null},renderFrame:Z,isContextLost(){return i},dispose(){if(o)return;o=!0,a.removeEventListener("webglcontextlost",u),ue(),v(),re&&(re.destroy(!0),re=null);for(const{texture:y,ownsSource:S}of Q.values())y.destroy(S);Q.clear(),I&&(I.destroy(!0),I=null);const _=y=>{y.mode==="full"?y.texture.destroy(!0):y.bitmap.close()};if(V)for(const y of V.sheets.values())_(y);if(D)for(const y of D.sheets.values())_(y);T.destroy(!0),m.destroy(!0),n.destroy({removeView:!1},{children:!0,texture:!0})}}}const ni=-1,tn=1e5,ri=new Map;async function Wo(e){let t=ri.get(e);if(!t){const n=await fetch(e);if(!n.ok)throw new Error(`Failed to fetch worker script (${n.status}): ${e}`);const r=await n.blob();t=URL.createObjectURL(r),ri.set(e,t)}return new Worker(t,{name:"visemeDiffPreview"})}function zo(){return typeof Worker<"u"&&typeof OffscreenCanvas<"u"}function Yn(e,t){(e.width!==t.width||e.height!==t.height)&&(e.width=t.width,e.height=t.height);const n=e.getContext("2d");n&&n.drawImage(t,0,0)}function ii(e,t){let n=0,r=null;const i=_r(async(s,o)=>(await Gn(),await qn(e,s),i.isLatestSeq(o)));return{usesWorker:!1,usesPixi:!1,async loadAssets(s){const o=await Gn(s);return r=o.visemePaths?new Set(o.visemePaths):null,o},getVisemeAtlasPaths(){return r},async renderSilOnly(){n+=1,i.reset(),await ao(e)},drawLiveFrame(s,o,a,u){i.enqueue(o,a,u)},async compositePrefetchFrame(s,o){try{return await so(s)}catch{return null}},blitBitmap(s,o){Yn(s,o)},drawIdleVideo(){},beginIdleToSilCrossfade(){},tickIdleToSilCrossfade(){return!1},finishIdleToSilCrossfade(){},isIdleToSilCrossfading(){return!1},supportsBufferedPlayback:!0,async renderViseme(s,o){n+=1;const a=n;t.onStatus?.(o),await qn(s,{mouthPath:Mt(o),expressionPath:null})},dispose(){n+=1,i.reset()}}}function Ho(e,t,n){let r=null,i=1,s=0,o=!1,a=!1,u=null;const d=new Map,l=new Map;let c=tn;const f=_r(async(O,b,E)=>(d.set(b,{drawKey:E.drawKey,onDisplayed:E.onDisplayed}),await I({type:"drawFrame",diffPath:O.mouthPath,expressionDiffPath:O.expressionPath,liveSeq:b},void 0,!0),!1)),g=O=>{if(O===ni){let V=-1,D;for(const[oe,pe]of d)oe>=V&&(V=oe,D=pe);V>=0&&d.delete(V),D?.onDisplayed?.(D.drawKey);return}const b=-O,E=d.get(b);d.delete(b),E?.onDisplayed?.(E.drawKey)},T=new Map,m=(O,b)=>{a||!r||r.postMessage(O,[])},I=(O,b,E=!1)=>{if(a)return Promise.resolve();const V=i++;return new Promise((D,oe)=>{T.set(V,{resolve:pe=>D(pe),reject:oe,generation:typeof O.generation=="number"?O.generation:void 0,expectRenderDone:E}),m({...O,requestId:V})})},L=(async()=>{r=await Wo(n),r.onmessage=O=>{if(a)return;const b=O.data;if(b.type==="status"){t.onStatus?.(b.label);return}if(b.type==="frame"){if(b.generation<=-tn){const D=-b.generation,oe=l.get(D);l.delete(D),oe?.resolve(b.bitmap);return}const V=b.generation<0;if(!V&&b.generation!==s){b.bitmap.close();return}if(V&&!(b.generation===ni)){const oe=-b.generation;if(!f.isLatestSeq(oe)){lt()?.onWorkerStale(),b.bitmap.close();return}}if(V&&t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()){lt()?.onWorkerStale(),b.bitmap.close();return}Yn(e,b.bitmap),b.bitmap.close(),V&&g(b.generation);return}const E=T.get(b.requestId);if(E)switch(b.type){case"ready":T.delete(b.requestId),E.resolve();break;case"loadAssetsDone":T.delete(b.requestId),u=b.visemePaths?.length?new Set(b.visemePaths):null,E.resolve({width:b.width,height:b.height,visemePaths:b.visemePaths});break;case"renderDone":if(E.expectRenderDone&&E.generation!=null&&b.generation!==E.generation)return;T.delete(b.requestId),E.resolve();break;case"renderAborted":T.delete(b.requestId),E.resolve();break;case"error":T.delete(b.requestId),t.onError?.(b.message),E.reject(new Error(b.message));break}},r.onerror=O=>{if(a)return;const b=O.message||"Worker error";t.onError?.(b);for(const[,E]of T)E.reject(new Error(b));T.clear()},await I({type:"init"}),o=!0})();return{usesWorker:!0,usesPixi:!1,async loadAssets(O){if(a)return{width:842,height:1264};if(await L,a)return{width:842,height:1264};if(!o)throw new Error("Worker init failed");return await I({type:"loadAssets",keyHex:O,urls:Vs(yt())})??{width:842,height:1264}},getVisemeAtlasPaths(){return u},async renderSilOnly(){if(a||(await L,a))return;s+=1,f.reset(),d.clear(),l.forEach(({resolve:b})=>b(null)),l.clear(),c=tn;const O=s;await I({type:"cancelLiveDraws"}),await I({type:"renderSil",generation:O},void 0,!0)},drawLiveFrame(O,b,E,V){a||L.then(()=>{a||f.enqueue(b,E,V)})},compositePrefetchFrame(O,b){return a?Promise.resolve(null):L.then(()=>{if(a)return null;const E=++c;return new Promise(V=>{l.set(E,{resolve:V}),I({type:"drawFrame",diffPath:O.mouthPath,expressionDiffPath:O.expressionPath,liveSeq:E},void 0,!0).catch(()=>{l.delete(E),V(null)})})})},blitBitmap(O,b){Yn(O,b)},drawIdleVideo(){},beginIdleToSilCrossfade(){},tickIdleToSilCrossfade(){return!1},finishIdleToSilCrossfade(){},isIdleToSilCrossfading(){return!1},supportsBufferedPlayback:!0,async renderViseme(O,b,E={}){if(a||(await L,a))return;s+=1,f.reset(),d.clear();const V=s;try{await I({type:"cancelLiveDraws"}),await I({type:"renderViseme",generation:V,to:b,from:E.from,transitionDurationMs:E.transitionDurationMs,gapMs:E.gapMs,threshold:E.threshold},void 0,!0)}catch(D){if(D?.name==="AbortError")return;throw D}},dispose(){if(!a){a=!0,s+=1,f.reset(),d.clear(),l.forEach(({resolve:O})=>O(null)),l.clear(),c=tn;for(const[,O]of T)O.resolve();T.clear(),r?.terminate(),r=null}}}}async function $o(e,t){const n=await Vo(e,{onWebGlContextLost:t.onWebGlContextLost,maxTextureSize:t.pixiMaxTextureSize});let r=0,i=null,s=!1;const o=_r(async(a,u)=>s?!1:t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()?(lt()?.onWorkerStale(),!1):(n.drawLiveFrame(a),o.isLatestSeq(u)));return{usesWorker:!1,usesPixi:!0,async loadAssets(a){if(s)return{width:842,height:1264};const u=await n.loadAssets(a);return i=u.visemePaths?.length?new Set(u.visemePaths):null,u},getVisemeAtlasPaths(){return i},async renderSilOnly(){s||(r+=1,o.reset(),n.renderSilOnly())},drawLiveFrame(a,u,d,l){s||o.enqueue(u,d,l)},async compositePrefetchFrame(a,u){if(s)return null;try{return await n.compositePrefetchFrame(a)}catch{return null}},blitBitmap(a,u){s||t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()||n.blitBitmap(u)},drawIdleVideo(a,u){s||n.drawIdleVideo(a,u)},beginIdleToSilCrossfade(a){s||n.beginIdleToSilCrossfade(a)},tickIdleToSilCrossfade(a){return s?!1:n.tickIdleToSilCrossfade(a)},finishIdleToSilCrossfade(){s||n.finishIdleToSilCrossfade()},isIdleToSilCrossfading(){return s?!1:n.isIdleToSilCrossfading()},supportsBufferedPlayback:!1,async renderViseme(a,u){if(s)return;r+=1;const d=r;o.reset(),t.onStatus?.(u),n.drawLiveFrame({mouthPath:Mt(u),expressionPath:null})},dispose(){s||(s=!0,r+=1,o.reset(),n.dispose())}}}function si(e,t){if(!zo())return ii(e,t);const n=Ss(t.workerScriptUrl);try{return Ho(e,t,n)}catch{return ii(e,t)}}function jo(e,t={}){if(t.preferLegacyRenderer)return si(e,t);let n=null,r=!1,i=null,s=!1;const o=()=>n?Promise.resolve(n):(i||(i=(async()=>{try{const a=await $o(e,t);if(s)throw a.dispose(),new Error("Renderer disposed during Pixi init");return r=!0,n=a,a}catch(a){if(s)throw a instanceof Error?a:new Error(String(a));return console.warn("[AiTwin] Pixi WebGL renderer unavailable, using Canvas2D fallback:",a),n=si(e,t),r=!1,n}})()),i);return{get usesWorker(){return n?.usesWorker??!1},get usesPixi(){return r},async loadAssets(a){return(await o()).loadAssets(a)},getVisemeAtlasPaths(){return n?.getVisemeAtlasPaths()??null},async renderSilOnly(){return(await o()).renderSilOnly()},drawLiveFrame(a,u,d,l){o().then(c=>{s||c.drawLiveFrame(a,u,d,l)})},async compositePrefetchFrame(a,u){return(await o()).compositePrefetchFrame(a,u)},blitBitmap(a,u){n&&n.blitBitmap(a,u)},drawIdleVideo(a,u){n?.usesPixi&&n.drawIdleVideo(a,u)},beginIdleToSilCrossfade(a){n?.beginIdleToSilCrossfade(a)},tickIdleToSilCrossfade(a){return n?.tickIdleToSilCrossfade(a)??!1},finishIdleToSilCrossfade(){n?.finishIdleToSilCrossfade()},isIdleToSilCrossfading(){return n?.isIdleToSilCrossfading()??!1},get supportsBufferedPlayback(){return n?.supportsBufferedPlayback??!1},async renderViseme(a,u,d){return(await o()).renderViseme(a,u,d)},dispose(){s=!0,i=null,n?.dispose(),n=null}}}class Ko extends Error{constructor(t,n){super(`${t} timed out after ${Math.round(n/1e3)}s`),this.name="LoadTimeoutError"}}function Nn(e,t,n){return new Promise((r,i)=>{const s=globalThis.setTimeout(()=>{i(new Ko(n,t))},t);e.then(o=>{globalThis.clearTimeout(s),r(o)},o=>{globalThis.clearTimeout(s),i(o)})})}const Go={overflow:"hidden",position:"relative",display:"inline-block",lineHeight:0},qo={display:"block",maxWidth:"100%",height:"auto"},Yo={position:"absolute",inset:0,display:"flex",alignItems:"center",justifyContent:"center",padding:"1rem",background:"rgba(0,0,0,0.55)",color:"#fff",fontSize:"0.875rem",textAlign:"center"};function On(e){return mi(e)?lr:hi(e)?fn:cr}function nt(e,t){return t?.isSpeaking()||t?.isWarmingUp()?t:e.isSpeaking()||e.isWarmingUp()?e:t?.isConnected()?t:e}const ls=h.forwardRef(function({id:t,avatarId:n,faceId:r,assets:i,authToken:s,apiBase:o,ttsEngineId:a,voiceId:u,language:d,speakingRate:l=Ht,className:c,style:f,canvasStyle:g,onStatusChange:T,onDisplayStatus:m,onError:I,onReady:L,showErrorOverlay:O=!0,loadingThumbnailUrl:b,onUserTranscript:E,onBotOutput:V,onRealtimeLipsyncDebug:D,onFrameRenderStats:oe,lipsyncBufferedPlayback:pe=!0,lipsyncPlaybackWarmupMs:P=1500,idleHandoffHoldMs:z=_o,pixiMaxTextureSize:Q,captions:le},ce){const re=h.useRef(null),ue=h.useRef(null),we=h.useRef(null),se=h.useRef(pe);se.current=pe;const W=h.useRef(P);W.current=P;const M=h.useRef(z);M.current=z;const q=h.useRef(0),R=h.useRef(yr()),H=h.useRef(Ya()),X=h.useRef(!1),ae=h.useRef(null),v=h.useRef(null),N=h.useRef(0),U=h.useRef(0),Y=h.useRef(0),G=h.useRef(null),Z=h.useRef(null),_=h.useRef(!1),y=h.useRef(!1),S=h.useRef(!1),K=h.useRef(!1),ee=h.useRef(!1),x=h.useRef("sil"),J=h.useRef(0),ne=h.useRef(!1),te=h.useRef(null),Se=h.useRef(!1),[ut,br]=h.useState("idle"),[$t,vt]=h.useState(null),[ot,bn]=h.useState(!1),[p,B]=h.useState(!1),[ie,de]=h.useState(!1),[Me,Ee]=h.useState(),[Pe,ze]=h.useState({width:842,height:1264}),[Oe,jt]=h.useState(null),[Sn,Bt]=h.useState(0),_t=h.useRef(!1),Ne=h.useRef(s??null),Ge=o?.trim()||Ct,bt=h.useRef(null),xe=h.useRef(null),{ttsEngineId:Re,voiceId:qe}=h.useMemo(()=>{const w=it(a,u),k=w.ttsEngineId??ht;return{ttsEngineId:k,voiceId:w.voiceId??On(k)}},[a,u]),Ue=h.useCallback(w=>{const k=mi(w);R.current.holdPp=k,we.current?.setHoldPp(k)},[]),Ve=h.useRef(!1),Ie=h.useRef({width:842,height:1264}),De=h.useRef(null),He=h.useRef("sil"),et=h.useRef(""),$e=h.useMemo(()=>co(le),[le]),We=h.useCallback(()=>{et.current="",jt(null)},[]);h.useEffect(()=>{$e.enabled||We()},[$e.enabled,We]);const St=h.useCallback(w=>{if(!$e.enabled)return;const k=w.isSpeaking(),C=w.isWarmingUp();if(!k||C){et.current&&We();return}const A=oo(w.getWordQueue());if(!A){et.current&&We();return}A!==et.current&&(et.current=A,jt(A))},[$e.enabled,We]),Tt=h.useRef(void 0),Tn=h.useRef(new Map),Sr=h.useRef(L),Tr=h.useRef(T),Te=h.useRef(m),kr=h.useRef(I),Er=h.useRef(E),kn=h.useRef(V),Kt=h.useRef(D),xr=h.useRef(oe),kt=h.useRef(vo());Sr.current=L,Tr.current=T,Te.current=m,kr.current=I,Er.current=E,kn.current=V,Kt.current=D,xr.current=oe,h.useEffect(()=>{const w=kt.current;return Yr(w),()=>Yr(null)},[]);const En=h.useCallback(w=>{const k=it(w?.ttsEngineId??a,w?.voiceId??u),C=k.ttsEngineId??xe.current?.ttsEngineId??ht,A=k.voiceId??xe.current?.voiceId??On(C);return{ttsEngineId:C,voiceId:A}},[a,u]),tt=h.useCallback(w=>{vt(w),de(!0),kr.current?.(w)},[]),Ft=h.useCallback(w=>{br(w),Tr.current?.(w)},[]),me=h.useRef(jn(Ft,Ge,{onLipsyncDebug:w=>Kt.current?.(w)})),ge=h.useRef(null),Ir=h.useRef(()=>{});h.useEffect(()=>(me.current=jn(Ft,Ge,{onLipsyncDebug:w=>Kt.current?.(w)}),s&&me.current.setDeveloperToken(s),me.current.setTtsEngineId(Re),()=>{me.current.stop();const w=ge.current;w&&(w.disconnect(),ge.current=null)}),[Ft,Ge]);const Ar=h.useCallback(()=>(ge.current||(ge.current=Ki(Ft,{speakingRate:l,language:d,onUserTranscript:(w,k)=>Er.current?.(w,k),onBotOutput:w=>kn.current?.(w),onLipsyncDebug:w=>Kt.current?.(w)}),ge.current.setTtsEngineId(Re),Ir.current(!0)),ge.current),[Ft,l,d,Re]),Fe=h.useCallback(()=>{G.current!=null&&(clearTimeout(G.current),G.current=null)},[]),Be=h.useCallback(()=>{Z.current!=null&&(clearTimeout(Z.current),Z.current=null),S.current=!1},[]),xn=h.useCallback(()=>{ee.current=!0,Be(),te.current?.setLooping(!1)},[Be]),je=h.useCallback(()=>{ee.current=!1,te.current?.setLooping(!0)},[]),Lt=h.useCallback(()=>{if(K.current)return;const w=ue.current;if(K.current=!0,ee.current=!0,Be(),_.current=!1,y.current=!1,!Se.current||!te.current){w?.renderSilOnly();return}const k=te.current;k.setLooping(!1),k.setActive(!1);const C=k.getVideo();if(C.ended||Number.isFinite(C.currentTime)&&C.currentTime<=Fn){try{(C.ended||C.currentTime>Fn)&&(C.currentTime=0)}catch{}w?.renderSilOnly(),w?.finishIdleToSilCrossfade();return}if(w?.usesPixi){w.drawIdleVideo(C),w.beginIdleToSilCrossfade(Xr);return}(async()=>{const $=re.current;$&&await Bn($,C,null,void 0,Ie.current).catch(()=>{}),await new Promise(ve=>setTimeout(ve,Xr)),K.current&&await w?.renderSilOnly()})()},[Be]),Mr=h.useCallback(()=>{if(!(!Se.current||!te.current)){if(K.current=!1,ue.current?.finishIdleToSilCrossfade(),ue.current?.renderSilOnly(),Be(),ee.current){S.current=!1;return}S.current=!0,Z.current=setTimeout(()=>{if(Z.current=null,nt(me.current,ge.current).isSpeaking()){S.current=!1;return}if(_.current){S.current=!1;return}if(ee.current){S.current=!1;return}const w=te.current;if(!w){S.current=!1;return}je(),w.setLooping(!0),w.restart(),w.setActive(!0);const k=w.getVideo();let C=!1;const A=()=>{C||(C=!0,k.removeEventListener("playing",$),S.current=!1)},$=()=>{A()};if(k.addEventListener("playing",$,{once:!0}),k.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA&&!k.paused){A();return}setTimeout(A,300)},M.current)}},[Be,je]),Et=h.useCallback(()=>{if(Fe(),Te.current?.("sil"),_.current=!1,K.current=!1,!Se.current||!te.current){S.current=!1,je();return}Mr()},[Fe,Mr,je]),In=h.useCallback(async w=>{const k=U.current,C=ue.current,A=re.current,$=()=>{w?.fallbackOnly||(_.current=!1)};if(!C||!A||!ot){$();return}if(nt(me.current,ge.current).isSpeaking()){$();return}if(w?.fallbackOnly){if(!_.current||(Le(R.current),await C.renderSilOnly(),k!==U.current)||nt(me.current,ge.current).isSpeaking())return;Et();return}if(Jr(R.current)){Le(R.current),Et();return}if(se.current&&C.supportsBufferedPlayback&&!w?.fallbackOnly){if(Le(R.current),we.current?.reset(),await C.renderSilOnly(),k!==U.current||nt(me.current,ge.current).isSpeaking())return;Et();return}const _e=xo(nt(me.current,ge.current).getVisemeQueue(),J.current,x.current),Ce=Io(_e,De.current);if(Le(R.current),!Ce){if(await C.renderSilOnly(),k!==U.current||nt(me.current,ge.current).isSpeaking())return;Et();return}try{await C.renderViseme(A,"sil",{from:_e,transitionDurationMs:ss})}catch(ke){if(ke?.name!=="AbortError")throw ke}if(k!==U.current){$();return}if(nt(me.current,ge.current).isSpeaking()){$();return}Et()},[ot,Et]),dt=h.useCallback(()=>{if(y.current||_.current)return;y.current=!0,_.current=!0,Fe();const w=U.current;In().finally(()=>{y.current=!1}),G.current=setTimeout(()=>{G.current=null,w===U.current&&In({fallbackOnly:!0})},Ao())},[Fe,In]),Pr=h.useCallback(async()=>{const w=Math.floor(Date.now()/1e3);if(bt.current&&w<bt.current.expiresAt-60)return bt.current.key;const k=Ne.current??await nn();Ne.current=k,me.current.setDeveloperToken(k);const C=await Ls(k,Ge);return bt.current={key:C.value,expiresAt:C.expiresAt},C.value},[Ge]);h.useEffect(()=>{s&&(Ne.current=s,me.current.setDeveloperToken(s))},[s]),h.useEffect(()=>{me.current.setTtsEngineId(Re),ge.current?.setTtsEngineId(Re),xe.current&&(xe.current.ttsEngineId=Re),Ue(Re)},[Re,Ue]),h.useEffect(()=>{xe.current&&(xe.current.voiceId=qe)},[qe]);const Ye=h.useMemo(()=>wi(n,r),[n,r]),ms=h.useMemo(()=>i?JSON.stringify(i):Ye?`face:${Ye}`:`id:${t??""}`,[i,Ye,t]),hs=h.useMemo(()=>{const w=b?.trim();return w||(Dr(Ye)??Dr(Me)??Ps(i))},[b,Ye,Me,i]);return h.useEffect(()=>{if(!t&&!i&&!Ye){tt("AiTwin requires `assets`, `avatarId`/`faceId`, or `id`");return}let w=!1;Ve.current=!1,bn(!1),B(!1),de(!1),Ee(Ye),vt(null),xe.current=null,te.current=null,Se.current=!1;const k=re.current;if(!k)return;let C=null;return(async()=>{try{let A,$,ve,_e,Ce;if(i)A=i,$=Re,ve=qe;else if(Ye)Te.current?.("loadFace"),A=Hn(Ye),$=Re,ve=qe;else{Te.current?.("getAiTwin");const j=await Nn(pi(t),3e4,"getAiTwin");if(w)return;if(!j.faceId?.trim())throw new Error("getAiTwin returned no faceId");Ee(j.faceId.trim());const be=it(a,u);$=be.ttsEngineId??j.ttsEngineId,ve=be.voiceId??j.voiceId??On($),_e=tr(j),Ce=nr(j),A=Hn(j.faceId)}xe.current={ttsEngineId:$,voiceId:ve,brainId:_e,brainUrl:Ce},yi(A),te.current=Qa();const ke=!!(i&&!wt(i));C=jo(k,{preferLegacyRenderer:ke,pixiMaxTextureSize:Q,onStatus:j=>{w||Te.current?.(j)},onError:j=>{w||tt(j)},shouldAcceptLiveFrame:()=>{if(X.current)return!0;if(_.current)return!1;const j=nt(me.current,ge.current);return j.isSpeaking()||j.isWarmingUp()},onWebGlContextLost:()=>{w||_t.current||(_t.current=!0,queueMicrotask(()=>{w||Bt(j=>j+1)}))}}),ue.current=C,we.current=Do(C),Ue($),_t.current=!1;const Qe=j=>{const be=()=>{if(!j||W.current<=0)return!1;const Ae=ue.current;return Ae?se.current&&Ae.supportsBufferedPlayback?!0:Ae.usesPixi:!1};me.current.setLipsyncWarmupHold(j,be),ge.current?.setLipsyncWarmupHold(j,be)};Ir.current=Qe,Qe(!0),Te.current?.("auth");const Gt=Ne.current??s??await Nn(nn(),3e4,"getAuthToken");if(w)return;Ne.current=Gt,me.current.setDeveloperToken(Gt),me.current.setTtsEngineId($);const he=Wt()?await Pr():void 0;if(w)return;Te.current?.("loadAssets");const fe=await Nn(C.loadAssets(he),18e4,"load face assets");if(!w&&fe.width>0&&fe.height>0&&(Ie.current=fe,ze(fe)),w)return;De.current=C.getVisemeAtlasPaths(),Ve.current=!0,bn(!0),Sr.current?.(),C.renderSilOnly().then(()=>{w||Te.current?.("sil")}),(async()=>{try{const j=await Ea(he);if(await te.current.load(j),w||(Se.current=!0,B(!0),Le(R.current),await C.renderSilOnly(),w)||(await new Promise(Ae=>setTimeout(Ae,M.current)),w))return;te.current.setActive(!0);const be=te.current.getVideo();C.usesPixi?C.drawIdleVideo(be):await Bn(k,be,null,void 0,Ie.current)}catch(j){if(w)return;Se.current=!1,B(!1),console.warn("[AiTwin] Idle video unavailable:",j)}finally{w||(de(!0),Te.current?.("idle"))}})()}catch(A){if(w)return;if(A instanceof rn){tt(A.message);return}tt(A instanceof Error?A.message:"Failed to load AI twin")}})(),()=>{w=!0,te.current?.dispose(),C?.dispose(),ue.current=null}},[ms,i,Ye,t,Re,qe,a,u,s,Q,Pr,tt,Sn,Ue]),h.useEffect(()=>{const w={current:!1},k=()=>{const C=re.current,A=ue.current,$=nt(me.current,ge.current);if(!C||!A||!ot){N.current=requestAnimationFrame(k);return}if(document.hidden){te.current?.setActive(!1),N.current=requestAnimationFrame(k);return}const ve=$.isSpeaking(),_e=$.isWarmingUp(),Ce=ve&&!ne.current,ke=ne.current&&!ve;Ce&&(Fe(),Be(),_.current=!1,y.current=!1,Le(R.current),we.current?.reset(),kt.current.reset(),Tt.current=void 0,Y.current=0,x.current="sil",J.current=0,Lt()),ke&&(H.current.reset(),te.current?.setActive(!1),we.current?.reset(),A.finishIdleToSilCrossfade(),We(),ee.current=!1,dt());const Qe=ke||_.current||S.current||K.current,Gt=pe&&A.supportsBufferedPlayback;if(ee.current&&!ve&&!_e&&!ke&&!_.current&&!S.current){if(K.current){A.usesPixi&&A.isIdleToSilCrossfading()&&A.tickIdleToSilCrossfade(performance.now()),ne.current=ve,N.current=requestAnimationFrame(k);return}if(Se.current&&te.current){const he=te.current.getVideo();if(he.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA){const fe=he.duration,j=he.currentTime,be=he.ended||Number.isFinite(j)&&j<=Fn,Ae=Number.isFinite(fe)&&fe>0&&Number.isFinite(j)?Math.max(0,(fe-j)*1e3):Number.POSITIVE_INFINITY;if(be||Ae<=bo){Lt(),A.usesPixi&&A.isIdleToSilCrossfading()&&A.tickIdleToSilCrossfade(performance.now()),ne.current=ve,N.current=requestAnimationFrame(k);return}}}}if(_e){if(Lt(),Se.current&&te.current?.setActive(!1),A.usesPixi&&A.isIdleToSilCrossfading()){A.tickIdleToSilCrossfade(performance.now()),$.getVisemeQueue().length>0&&q.current===0&&(q.current=performance.now()),ne.current=$.isSpeaking(),N.current=requestAnimationFrame(k);return}const he=$.getVisemeQueue();he.length>0&&q.current===0&&(q.current=performance.now()),H.current.advance(performance.now());const fe=H.current.getExpressionPath(),j=H.current.getDrawKey(),be={queue:he,elapsedMs:0,expressionPath:fe,expressionDrawKey:j,availablePaths:De.current};let Ae=!1;Gt&&we.current?(we.current.warmupTick(be),Ae=we.current.isWarmupReady(be,W.current)):A.usesPixi&&Zr(C,he,0,fe,j,R.current,(xt,qt,An,Yt)=>{A.drawLiveFrame(xt,qt,An,Xe=>{Yt?.(Xe),$.isWarmingUp()&&Xe!==""&&!Jr({lastDrawnKey:Xe})&&(q.current=0,$.releaseWarmupHold())})},xt=>{Te.current?.(xt.label)},De.current);const ct=q.current>0&&performance.now()-q.current>ks;Te.current?.(Ae?"buffer ready":"buffering lipsync…"),(Ae||ct)&&(q.current=0,$.releaseWarmupHold()),St($),ne.current=$.isSpeaking(),N.current=requestAnimationFrame(k);return}if(ve){Lt(),Se.current&&te.current?.setActive(!1),A.usesPixi&&A.isIdleToSilCrossfading()&&A.finishIdleToSilCrossfade();const he=$.getVisemeQueue(),fe=$.getPlaybackElapsedMs();fa(he,fe),J.current=fe;const j=he.length>0?zt(he,fe):"sil";j!=="sil"&&(x.current=j);const be=eo(fe,$.getSentenceEmotions(),$.getWordQueue(),$.getStreamMood());H.current.setTargetEmotion(be),H.current.setTargetEyebrow(Ga(fe,$.getWordQueue(),gt(be))),H.current.advance(performance.now());const Ae=H.current.getExpressionPath(),ct=H.current.getDrawKey(),xt=(Xe,Mn,Nt,Pn)=>{const Rr={label:He.current,elapsedMs:fe,mouthPath:Mn.mouthPath,transitionMeta:Tt.current};Tn.current.set(Nt,Rr),we.current?.markExternallyDisplayed(Nt,Ae),A.drawLiveFrame(Xe,Mn,Nt,Qt=>{const ps=Tn.current.get(Qt)??Rr;Tn.current.delete(Qt),kt.current.onDrawDisplayed(ps,he),we.current?.markExternallyDisplayed(Qt,Ae),Pn?.(Qt)})},qt=Xe=>{He.current=Xe.label,Tt.current=Xe.transitionMeta,Te.current?.(Xe.label)},An=pe&&A.supportsBufferedPlayback;let Yt=!1;An&&we.current&&(Yt=we.current.tick({canvas:C,queue:he,elapsedMs:fe,expressionPath:Ae,expressionDrawKey:ct,playbackState:R.current,availablePaths:De.current,onStatus:qt,onDisplayed:(Mn,Nt,Pn)=>{kt.current.onDrawDisplayed({label:Pn,elapsedMs:fe,mouthPath:Nt,transitionMeta:Tt.current},he)}}).usedBuffer),Yt||Zr(C,he,fe,Ae,ct,R.current,xt,qt,De.current),kt.current.onRafTickSpeaking(),xr.current?.(kt.current.snapshot(he,fe))}else if(!Qe&&X.current){te.current?.setActive(!1),H.current.advance(performance.now());const he=H.current.getExpressionPath(),fe=H.current.getDrawKey();Ln(C,he,fe,R.current,(j,be,Ae,ct)=>A.drawLiveFrame(j,be,Ae,ct),j=>{if(v.current){Te.current?.(v.current);return}const be=H.current.getTargetEmotion();Te.current?.(j==="sil"||j==="blink"?`${be}${j==="blink"?" · blink":""}`:j)},null,ae.current)}else if(!Qe&&Se.current&&te.current){const he=te.current,fe=he.getVideo();if(fe.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA){he.setActive(!0);const j=()=>!$.isSpeaking()&&!_.current&&!S.current&&!X.current;A.usesPixi?(A.drawIdleVideo(fe,j),j()&&Te.current?.("idle")):w.current||(w.current=!0,Bn(C,fe,null,void 0,Ie.current,j).then(()=>Te.current?.("idle")).finally(()=>{w.current=!1}))}else Ln(C,null,"__sil__",R.current,(j,be,Ae,ct)=>A.drawLiveFrame(j,be,Ae,ct),j=>Te.current?.(j))}else Qe||Ln(C,null,"__sil__",R.current,(he,fe,j,be)=>A.drawLiveFrame(he,fe,j,be),he=>Te.current?.(he));$e.enabled&&St($),ne.current=ve,N.current=requestAnimationFrame(k)};return N.current=requestAnimationFrame(k),()=>{cancelAnimationFrame(N.current),Fe(),Be(),me.current.stop(),ge.current?.stop()}},[ot,p,dt,Fe,Be,Lt,St,We,$e.enabled]),h.useImperativeHandle(ce,()=>({speakText:async(w,k)=>{const C=w.trim();if(!C)return;const A=ue.current;if(!A||!Ve.current&&!ot)throw new Error("AI twin is not ready");const{ttsEngineId:$,voiceId:ve}=En(k);vt(null),We(),U.current+=1,Fe(),Be(),_.current=!1,y.current=!1,x.current="sil",J.current=0,Le(R.current),Ue($),X.current=!1,ae.current=null,v.current=null,H.current.reset(),ne.current=!1,xn(),K.current=!1,A.finishIdleToSilCrossfade();try{await me.current.speak(C,{voiceId:ve,speakingRate:k?.speakingRate??l,ttsEngineId:$,language:k?.language??d})}catch(_e){throw je(),K.current=!1,A.finishIdleToSilCrossfade(),dt(),tt(_e instanceof Error?_e.message:"TTS failed"),_e}},speakWithReply:async(w,k)=>{const C=w.trim();if(!C)return{displayText:"",spokenText:""};const A=ue.current;if(!A||!Ve.current&&!ot)throw new Error("AI twin is not ready");const{ttsEngineId:$,voiceId:ve}=En(k);vt(null),We(),U.current+=1,Fe(),Be(),_.current=!1,y.current=!1,x.current="sil",J.current=0,Le(R.current),Ue($),X.current=!1,ae.current=null,v.current=null,H.current.reset(),ne.current=!1,xn(),K.current=!1,A.finishIdleToSilCrossfade();const _e=k?.brainId?.trim()||xe.current?.brainId?.trim()||void 0,Ce=k?.brainUrl?.trim()||xe.current?.brainUrl?.trim()||void 0;try{return await me.current.speakWithReply(C,{...k,voiceId:ve,speakingRate:k?.speakingRate??l,ttsEngineId:$,language:k?.language??d,brainId:_e,brainUrl:Ce,onDisplayText:ke=>{k?.onDisplayText?.(ke),kn.current?.(ke)}})}catch(ke){throw je(),K.current=!1,A.finishIdleToSilCrossfade(),dt(),tt(ke instanceof Error?ke.message:"speakWithReply failed"),ke}},stop:()=>{U.current+=1,Fe(),Be(),_.current=!1,y.current=!1,K.current=!1,je(),ue.current?.finishIdleToSilCrossfade(),me.current.stop(),ge.current?.stop(),Le(R.current),we.current?.reset(),q.current=0,ne.current=!1,We(),dt()},setTtsEngineId:w=>{me.current.setTtsEngineId(w),ge.current?.setTtsEngineId(w),xe.current&&(xe.current.ttsEngineId=w),Ue(w)},renderViseme:async(w,k)=>{const C=ue.current,A=re.current;if(!C||!A||!Ve.current)throw new Error("AI twin is not ready");await C.renderViseme(A,w,{from:k?.from,transitionDurationMs:k?.transitionDurationMs,gapMs:k?.gapMs,onStatus:$=>Te.current?.($)})},setEmotion:w=>{if(!Ve.current)throw new Error("AI twin is not ready");const k=ue.current,C=re.current;if(!k||!C)throw new Error("AI twin is not ready");U.current+=1,Fe(),Be(),_.current=!1,y.current=!1,S.current=!1,K.current=!1,je(),me.current.stop(),ge.current?.stop(),Le(R.current),we.current?.reset(),q.current=0,ne.current=!1,k.finishIdleToSilCrossfade(),te.current?.setActive(!1);const A=rt(w);X.current=!0,ae.current=null,v.current=null,H.current.snapToEmotion(A);const $=H.current.getExpressionPath(),ve=H.current.getDrawKey(),_e={mouthPath:null,expressionPath:$},Ce=Vt(_e,ve);R.current.lastDrawnKey="",k.drawLiveFrame(C,_e,Ce,Qe=>{Qe===Ce&&(R.current.lastDrawnKey=Ce)});const ke=gt(A);Te.current?.($?`${A} · brow ${ke>0?"+":""}${ke}`:`${A} · brow 0 (sil base)`)},getEmotion:()=>X.current?H.current.getTargetEmotion():null,clearEmotionPreview:()=>{X.current=!1,ae.current=null,v.current=null,H.current.reset(),Le(R.current),Te.current?.("idle")},setViseme:w=>{if(!Ve.current)throw new Error("AI twin is not ready");const k=ue.current,C=re.current;if(!k||!C)throw new Error("AI twin is not ready");U.current+=1,Fe(),Be(),_.current=!1,y.current=!1,S.current=!1,K.current=!1,je(),me.current.stop(),ge.current?.stop(),Le(R.current),we.current?.reset(),q.current=0,ne.current=!1,k.finishIdleToSilCrossfade(),te.current?.setActive(!1);const A=hn(w);X.current=!0,v.current=A,ae.current=A==="sil"?null:Mt(A),H.current.snapToEmotion("neutral");const $=H.current.getExpressionPath(),ve=H.current.getDrawKey(),Ce={mouthPath:ae.current,expressionPath:$},ke=Vt(Ce,ve);R.current.lastDrawnKey="",k.drawLiveFrame(C,Ce,ke,Qe=>{Qe===ke&&(R.current.lastDrawnKey=ke)}),Te.current?.(A)},getViseme:()=>X.current?v.current:null,isReady:()=>Ve.current,getStatus:()=>ut,connect:async(w={})=>{vt(null);try{let k=w.authToken?.trim();k||(k=Ne.current??s??await nn(),Ne.current=k);const C=it(w.ttsEngineId??a,w.voiceId??u),A=C.voiceId??xe.current?.voiceId,$=w.brainId?.trim()||w.personaId?.trim()||xe.current?.brainId,ve=w.brainUrl?.trim()||xe.current?.brainUrl,_e=C.ttsEngineId??xe.current?.ttsEngineId??Re,Ce=Ar();_e&&Ce.setTtsEngineId(_e),_e&&Ue(_e),await Ce.unlockAudio(),await Ce.connect({...w,apiBase:w.apiBase?.trim()||Ge,authToken:k,voiceId:A,brainId:$,brainUrl:ve,ttsEngineId:_e,speakingRate:w.speakingRate??l,language:w.language??d})}catch(k){throw tt(k instanceof Error?k.message:"Voice connect failed"),k}},disconnect:async()=>{const w=ge.current;w&&(U.current+=1,Fe(),Be(),_.current=!1,y.current=!1,K.current=!1,je(),ue.current?.finishIdleToSilCrossfade(),Le(R.current),we.current?.reset(),q.current=0,ne.current=!1,await w.disconnect(),dt())},isConnected:()=>ge.current?.isConnected()??!1,getVoiceStream:()=>ge.current?.getVoiceStream()??me.current?.getVoiceStream()??null,setMuted:w=>{const k=me.current,C=ge.current;k?.setMuted?.(w),C?.setMuted?.(w)},isMuted:()=>{const w=ge.current,k=me.current;return w?.isMuted?.()??k?.isMuted?.()??!1},getBrainId:()=>xe.current?.brainId?.trim()||null,getPersonaId:()=>xe.current?.brainId?.trim()||null,getBrainUrl:()=>xe.current?.brainUrl?.trim()||null}),[ot,l,d,s,Re,qe,Fe,Be,dt,xn,je,tt,En,Ar,Ue,ut,Ge]),F.jsxs("div",{className:c,style:{...Go,...f},children:[F.jsx("canvas",{ref:re,width:Pe.width,height:Pe.height,style:{...qo,...g},"aria-label":t?`AI twin ${t}`:"AI twin face"}),!ie&&!$t?F.jsx(wo,{thumbnailUrl:hs}):null,O&&$t?F.jsx("div",{style:Yo,children:$t}):null,$e.enabled&&Oe?F.jsx(mo,{text:Oe,style:$e.style},Oe):null]})});function Qo({muted:e}){return e?F.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none","aria-hidden":!0,children:[F.jsx("path",{d:"M11 5 6.5 9H3v6h3.5L11 19V5Z",fill:"currentColor"}),F.jsx("path",{d:"m16 9.5 5 5m0-5-5 5",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})]}):F.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none","aria-hidden":!0,children:[F.jsx("path",{d:"M11 5 6.5 9H3v6h3.5L11 19V5Z",fill:"currentColor"}),F.jsx("path",{d:"M15.5 8.5a4.5 4.5 0 0 1 0 7M18.5 6a8 8 0 0 1 0 12",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})]})}function Xo(){return F.jsx("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":!0,children:F.jsx("path",{d:"M8 5.5v13l11-6.5L8 5.5Z"})})}function Jo(){return F.jsx("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":!0,children:F.jsx("rect",{x:"6",y:"6",width:"12",height:"12",rx:"1.5"})})}function Zo(){return F.jsx("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none","aria-hidden":!0,children:F.jsx("path",{d:"M5 6.5A2.5 2.5 0 0 1 7.5 4h9A2.5 2.5 0 0 1 19 6.5v7A2.5 2.5 0 0 1 16.5 16H10l-4 3.5V16H7.5A2.5 2.5 0 0 1 5 13.5v-7Z",stroke:"currentColor",strokeWidth:"2",strokeLinejoin:"round"})})}function us({className:e}){return F.jsx("span",{className:e??"aitwin-widget__spinner","aria-hidden":!0,children:F.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:F.jsx("circle",{cx:"12",cy:"12",r:"9",stroke:"currentColor",strokeWidth:"2.5",strokeLinecap:"round",strokeDasharray:"40 20"})})})}function ec({muted:e,onToggle:t}){return F.jsx("button",{type:"button",className:"aitwin-widget__mute",onClick:t,"aria-label":e?"Unmute":"Mute","aria-pressed":e,children:F.jsx(Qo,{muted:e})})}function tc(){return F.jsxs("div",{className:"aitwin-widget__connecting",role:"status","aria-live":"polite",children:[F.jsx(us,{className:"aitwin-widget__spinner aitwin-widget__spinner--accent"}),F.jsx("span",{children:"Connecting..."})]})}function nc(e){const t=e.connecting||e.textSending||e.status==="loading"||e.status==="speaking",n=e.connecting?"Connecting...":e.connected?"End Talk":"Talk",r=["aitwin-widget__action",e.textMode&&!e.connecting?"":"aitwin-widget__action--primary",e.connecting?"aitwin-widget__action--connecting":""].filter(Boolean).join(" ");return F.jsxs("div",{className:"aitwin-widget__controls",children:[F.jsxs("div",{className:"aitwin-widget__actions",children:[F.jsxs("button",{type:"button",className:r,onClick:e.onTalk,disabled:!e.ready||e.connecting||e.textSending,"aria-busy":e.connecting,children:[e.connecting?F.jsx(us,{className:"aitwin-widget__spinner aitwin-widget__spinner--on-accent"}):e.connected?F.jsx(Jo,{}):F.jsx(Xo,{}),F.jsx("span",{children:n})]}),F.jsxs("button",{type:"button",className:e.textMode?"aitwin-widget__action aitwin-widget__action--primary":"aitwin-widget__action",onClick:e.onTextMode,disabled:!e.ready||e.connecting||e.textSending,"aria-pressed":e.textMode,children:[F.jsx(Zo,{}),F.jsx("span",{children:"Text"})]})]}),e.textMode?F.jsxs("form",{className:"aitwin-widget__composer",onSubmit:i=>{i.preventDefault(),e.onSendText()},children:[F.jsx("input",{type:"text",className:"aitwin-widget__composer-input",value:e.textDraft,onChange:i=>e.onTextDraftChange(i.target.value),placeholder:e.textSending?"Getting reply...":"Type a message…",autoComplete:"off",disabled:!e.ready||t}),F.jsx("button",{type:"submit",className:"aitwin-widget__composer-send",disabled:!e.ready||t||!e.textDraft.trim()&&!e.textSending,"aria-busy":e.textSending,children:e.textSending?"…":"Send"})]}):null,e.error?F.jsx("p",{className:"aitwin-widget__controls-error",role:"alert",children:e.error}):null]})}const ai=97,rc=3500;function ic(e,t){const n=Math.max(0,e),r=1-Math.exp(-n/rc);return Math.min(ai,Math.round(r*ai))}function sc(e){const[t,n]=h.useState(null),[r,i]=h.useState(()=>Date.now());return h.useEffect(()=>{if(!e){n(null);return}const s=Date.now();n(s),i(s);let o=0;const a=()=>{i(Date.now()),o=window.requestAnimationFrame(a)};return o=window.requestAnimationFrame(a),()=>{window.cancelAnimationFrame(o)}},[e]),e?t==null?0:ic(r-t):100}const ac="M259.01,593.87v-101.49c.33-6.31,4.94-11.26,10.21-11.88,6.42-.75,13.18,4.99,13.13,12.92-.42,45.09-.83,90.17-1.25,135.26.03,1.56.56,15.43,11.95,23.2,6.18,4.21,12.57,4.6,15.42,4.58-.42-112.77-.83-225.54-1.25-338.31-.08-2.55-1.03-46.36,21.17-55.34,2.05-.83,4.42-1.41,7.17-1.56v453.42c.07,12.06,1.98,37.84,19.03,62.73,7.01,10.23,14.79,17.71,21.4,22.99V222.42c-.01-3.72.48-11.67,5.35-19.87,7-11.77,18.13-15.82,21.4-16.95,10.42-3.61,19.49-2.06,23.48-1.11v684.96c.59,4.03,1.78,9.72,4.45,16.12,1.93,4.63,8.84,20.57,26.47,31.26,13.48,8.18,26.56,9.23,32.86,9.31-1.3-256.11-2.59-512.22-3.89-768.33.76-16.43,14.57-28.94,29.73-28.34,13.53.53,25.39,11.39,27.23,25.84-1.1,264.68-2.2,529.35-3.3,794.03,4.33.92,22.38,4.25,38.94-6.53,10.26-6.68,15.24-15.72,17.09-19.17,5.55-10.36,6.42-19.98,6.53-25.01-.72-246.99-1.43-493.97-2.15-740.96,2.27-.06,34.75-.44,48.29,22.68,3.41,5.83,4.87,11.86,5.34,17.19v674.68c5.92-2.16,39.19-14.94,50.16-47.79,3.12-9.34,3.68-18.03,3.33-25.01.03-193.22.06-386.43.09-579.65,4.12,1.18,28.28,8.48,36.17,30.08,1.64,4.49,2.32,8.88,2.43,12.92.02,146.42.05,292.84.07,439.25,3.82-1.71,19.51-9.22,25.56-26.47,2.15-6.12,2.48-11.73,2.29-15.89.01-114.91.03-229.81.04-344.72,2.36-.1,7.51.02,12.74,3.13,9.1,5.4,10.87,15.35,11.12,16.88.35,11.49.69,22.97,1.04,34.46.46,37.05.93,74.1,1.39,111.15v152.69c3.39-.72,9.77-2.67,14.31-8.34,3.62-4.52,4.51-9.46,5.56-15.28.91-5.04.95-9.31.83-12.23.02-38.81.03-77.62.05-116.43";function oc({className:e="aitwin-widget__logo",strokeWidth:t=15,durationMs:n=2600,delayMs:r=0,loop:i=!1}){return F.jsx("svg",{viewBox:"0 0 1080 1080",className:e,role:"img","aria-label":"AITWIN logo",children:F.jsx("path",{className:i?"aitwin-widget__logo-draw aitwin-widget__logo-draw--loop":"aitwin-widget__logo-draw",d:ac,fill:"none",stroke:"currentColor",strokeWidth:t,strokeMiterlimit:10,pathLength:1,style:{animationDuration:`${n}ms`,animationDelay:`${r}ms`}})})}function cc({progress:e}){const t=Math.max(0,Math.min(100,Math.round(e)));return F.jsx("div",{className:"aitwin-widget__face-loading","aria-live":"polite","aria-label":`Loading ${t} percent`,children:F.jsxs("div",{className:"aitwin-widget__face-loading-content",children:[F.jsx(oc,{className:"aitwin-widget__logo",loop:!0,durationMs:2600}),F.jsxs("div",{className:"aitwin-widget__face-loading-bar-wrap",children:[F.jsx("div",{role:"progressbar","aria-valuenow":t,"aria-valuemin":0,"aria-valuemax":100,className:"aitwin-widget__face-loading-track",children:F.jsx("div",{className:"aitwin-widget__face-loading-fill",style:{width:`${t}%`}})}),F.jsxs("p",{className:"aitwin-widget__face-loading-pct",children:[t,"%"]})]})]})})}function oi(e){const t=e.id?.trim()||void 0,n=e.faceId?.trim()||e.avatarId?.trim()||void 0;return{id:n?void 0:t,faceId:n,authToken:e.authToken?.trim()||void 0,apiBase:e.apiBase?.trim()||void 0,voiceId:e.voiceId?.trim()||void 0,speakingRate:e.speakingRate,language:e.language}}function lc(e){return[e.id??"",e.faceId??"",e.authToken?.slice(0,16)??"",e.voiceId??"",e.apiBase??"",String(e.speakingRate??"")].join("|")}const ci={display:"flex",alignItems:"center",justifyContent:"center",width:"100%",height:"100%",lineHeight:0},uc={display:"block",height:"100%",width:"100%",objectFit:"cover",objectPosition:"top"},dc=h.forwardRef(function({options:t},n){const r=h.useRef(null),i=h.useRef(t);i.current={...i.current,onReady:t.onReady,onStatusChange:t.onStatusChange,onError:t.onError,onUserTranscript:t.onUserTranscript,onBotOutput:t.onBotOutput};const[s,o]=h.useState(!!t.showControls),[a,u]=h.useState(()=>oi(t)),[d,l]=h.useState(!1),[c,f]=h.useState(!1),[g,T]=h.useState(!1),[m,I]=h.useState(!1),[L,O]=h.useState("idle"),[b,E]=h.useState(null),[V,D]=h.useState(!1),[oe,pe]=h.useState(!1),[P,z]=h.useState(""),[Q,le]=h.useState(!1),[ce,re]=h.useState([]),[ue,we]=h.useState(t.brainId?.trim()||t.personaId?.trim()||void 0),[se,W]=h.useState(t.brainUrl?.trim()||void 0),M=h.useCallback(x=>{i.current={...i.current,...x},x.showControls!==void 0&&o(!!x.showControls),(x.brainId!==void 0||x.personaId!==void 0)&&we((x.brainId??x.personaId)?.trim()||void 0),x.brainUrl!==void 0&&W(x.brainUrl.trim()||void 0),u(J=>oi({id:x.id??J.id,faceId:x.faceId??x.avatarId??J.faceId,authToken:x.authToken??J.authToken,apiBase:x.apiBase??J.apiBase,voiceId:x.voiceId??J.voiceId,speakingRate:x.speakingRate??J.speakingRate,language:x.language??J.language}))},[]);h.useImperativeHandle(n,()=>({getTwin:()=>r.current,applyOptions:M}),[M]),h.useEffect(()=>{M(t)},[M,t.id,t.faceId,t.avatarId,t.authToken,t.apiBase,t.voiceId,t.speakingRate,t.showControls,t.brainId,t.personaId,t.brainUrl]);const q=h.useCallback(()=>{l(!0),E(null)},[]),R=h.useCallback(x=>{x==="idle"&&f(!0)},[]),H=h.useCallback(x=>{O(x),i.current.onStatusChange?.(x)},[]),X=h.useCallback(x=>{E(x),l(!1),f(!1),I(!1),i.current.onError?.(x)},[]),ae=h.useCallback((x,J)=>{i.current.onUserTranscript?.(x,J)},[]),v=h.useCallback(x=>{i.current.onBotOutput?.(x)},[]);h.useEffect(()=>{if(!s)return;const x=window.setInterval(()=>{T(!!r.current?.isConnected())},400);return()=>window.clearInterval(x)},[s,a]);const N=lc(a),U=!!(a.id||a.faceId),Y=d&&c,G=U&&!Y&&!b,Z=sc(G),_=h.useRef(N);h.useEffect(()=>{Y&&i.current.onReady?.()},[Y]),h.useEffect(()=>{Y&&r.current?.setMuted(V)},[Y,V,a]),h.useEffect(()=>{re([])},[a.id,a.faceId]),h.useEffect(()=>{_.current!==N&&(_.current=N,l(!1),f(!1),E(null),I(!1),T(!1),O("idle"))},[N]);const y=()=>{const x=r.current;if(!x?.isReady())return;if(E(null),pe(!1),x.isConnected()){I(!1),x.disconnect().finally(()=>{T(!1)});return}I(!0);const J={authToken:a.authToken,voiceId:a.voiceId,speakingRate:a.speakingRate,language:a.language,apiBase:a.apiBase,...ue?{brainId:ue}:{},...se?{brainUrl:se}:{}};x.connect(J).then(()=>{T(!!x.isConnected())}).catch(ne=>{const te=ne instanceof Error?ne.message:"Failed to connect voice.";E(te),i.current.onError?.(te)}).finally(()=>{I(!1)})},S=()=>{pe(x=>!x),E(null)},K=()=>{const x=P.trim(),J=r.current;!x||!J?.isReady()||Q||(E(null),z(""),le(!0),i.current.onUserTranscript?.(x,!0),(async()=>{try{const ne=ue||J.getBrainId()?.trim()||void 0,te=se||J.getBrainUrl()?.trim()||void 0,Se=await J.speakWithReply(x,{brainId:ne,brainUrl:te,history:ce,voiceId:a.voiceId,speakingRate:a.speakingRate,language:a.language});Se.displayText&&re(ut=>[...ut,{role:"user",content:x},{role:"assistant",content:Se.displayText}])}catch(ne){const te=ne instanceof Error?ne.message:"Failed to get reply.";E(te),i.current.onError?.(te)}finally{le(!1)}})())},ee=()=>{const x=!V;D(x),r.current?.setMuted(x)};return F.jsxs("div",{className:"aitwin-widget__layout",children:[F.jsxs("div",{className:"aitwin-widget__stage",children:[U?F.jsx("div",{style:ci,children:F.jsx(ls,{ref:r,id:a.id,faceId:a.faceId,authToken:a.authToken,apiBase:a.apiBase,voiceId:a.voiceId,speakingRate:a.speakingRate,language:a.language,style:ci,canvasStyle:uc,onReady:q,onDisplayStatus:R,onStatusChange:H,onError:X,onUserTranscript:ae,onBotOutput:v,loadingThumbnailUrl:t.loadingThumbnailUrl},N)}):F.jsx("p",{className:"aitwin-widget__hint",style:{padding:16},children:"Pass id or faceId to load a twin."}),G?F.jsx(cc,{progress:Z}):null,s?F.jsx(ec,{muted:V,onToggle:ee}):null,s&&m?F.jsx(tc,{}):null]}),s?F.jsx(nc,{ready:Y,connected:g,connecting:m,status:L,textMode:oe,textDraft:P,textSending:Q,error:b,onTalk:y,onTextMode:S,onTextDraftChange:z,onSendText:K}):null]})}),fc=".aitwin-widget{--aitwin-border: rgba(255, 255, 255, .12);--aitwin-text: #f4f4f5;--aitwin-muted: #a1a1aa;--aitwin-accent: #f97316;--aitwin-accent-hover: #fb923c;--aitwin-font: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;--aitwin-size: 1;--aitwin-stage-width: calc(200px * var(--aitwin-size));--aitwin-stage-height: calc(267px * var(--aitwin-size));--aitwin-action-height: calc(40px * var(--aitwin-size));--aitwin-action-font: calc(13px * var(--aitwin-size));--aitwin-action-radius: calc(10px * var(--aitwin-size));--aitwin-action-gap: calc(6px * var(--aitwin-size));--aitwin-mute-size: calc(28px * var(--aitwin-size));--aitwin-layout-gap: calc(8px * var(--aitwin-size));box-sizing:border-box;color:var(--aitwin-text);font-family:var(--aitwin-font);font-size:calc(12px * var(--aitwin-size));line-height:1.45;width:fit-content;max-width:100%}.aitwin-widget *,.aitwin-widget *:before,.aitwin-widget *:after{box-sizing:border-box}.aitwin-widget__layout{display:flex;flex-direction:column;gap:var(--aitwin-layout-gap);width:fit-content;max-width:100%}.aitwin-widget__stage{align-items:center;background:#0a0a0b;border:1px solid var(--aitwin-border);border-radius:calc(10px * var(--aitwin-size));display:flex;flex-shrink:0;height:var(--aitwin-stage-height);justify-content:center;line-height:0;overflow:hidden;position:relative;width:var(--aitwin-stage-width)}.aitwin-widget__stage .aitwin-root,.aitwin-widget__stage canvas{display:block;height:100%;object-fit:cover;object-position:top;width:100%}.aitwin-widget__hint{color:var(--aitwin-muted);font-size:calc(11px * var(--aitwin-size));margin:0}.aitwin-widget__mute{align-items:center;background:#28282cb8;border:none;border-radius:999px;color:#fff;cursor:pointer;display:inline-flex;height:var(--aitwin-mute-size);justify-content:center;padding:0;position:absolute;right:calc(8px * var(--aitwin-size));top:calc(8px * var(--aitwin-size));width:var(--aitwin-mute-size);z-index:2}.aitwin-widget__mute:hover{background:#37373cd9}.aitwin-widget__mute svg{height:calc(15px * var(--aitwin-size));width:calc(15px * var(--aitwin-size))}.aitwin-widget__controls{display:flex;flex-direction:column;gap:var(--aitwin-action-gap);width:var(--aitwin-stage-width)}.aitwin-widget__actions{display:grid;gap:var(--aitwin-action-gap);grid-template-columns:1fr 1fr;width:100%}.aitwin-widget__action{align-items:center;background:transparent;border:1.5px solid var(--aitwin-accent);border-radius:999px;color:var(--aitwin-accent);cursor:pointer;display:inline-flex;font-family:inherit;font-size:var(--aitwin-action-font);font-weight:600;gap:calc(6px * var(--aitwin-size));height:var(--aitwin-action-height);justify-content:center;padding:0 calc(10px * var(--aitwin-size))}.aitwin-widget__action svg{flex-shrink:0;height:calc(12px * var(--aitwin-size));width:calc(12px * var(--aitwin-size))}.aitwin-widget__action:hover:not(:disabled){background:#f973161a}.aitwin-widget__action:disabled{cursor:not-allowed;opacity:.45}.aitwin-widget__action--primary{background:var(--aitwin-accent);border-color:transparent;color:#fff}.aitwin-widget__action--primary:hover:not(:disabled){background:var(--aitwin-accent-hover)}.aitwin-widget__action--connecting:disabled{cursor:progress;opacity:1}.aitwin-widget__spinner{display:inline-flex;flex-shrink:0;height:calc(14px * var(--aitwin-size));width:calc(14px * var(--aitwin-size))}.aitwin-widget__spinner svg{animation:aitwin-widget-spin .8s linear infinite;display:block;height:100%;width:100%}.aitwin-widget__spinner--accent{color:var(--aitwin-accent);height:calc(16px * var(--aitwin-size));width:calc(16px * var(--aitwin-size))}.aitwin-widget__spinner--on-accent{color:#fff}@keyframes aitwin-widget-spin{to{transform:rotate(360deg)}}.aitwin-widget__connecting{align-items:center;backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);background:#ffffffe0;border-radius:999px;bottom:calc(14px * var(--aitwin-size));box-shadow:0 4px 16px #0000001f;color:#3f3f46;display:inline-flex;font-size:calc(12px * var(--aitwin-size));font-weight:600;gap:calc(8px * var(--aitwin-size));left:50%;line-height:1;padding:calc(8px * var(--aitwin-size)) calc(14px * var(--aitwin-size));pointer-events:none;position:absolute;transform:translate(-50%);z-index:3}.aitwin-widget__face-loading{background:transparent;inset:0;overflow:hidden;pointer-events:none;position:absolute;z-index:4}.aitwin-widget__face-loading-content{align-items:center;display:flex;flex-direction:column;gap:calc(12px * var(--aitwin-size));inset:0;justify-content:center;padding:calc(24px * var(--aitwin-size));position:absolute;z-index:1}.aitwin-widget__logo{color:#fff;display:block;flex-shrink:0;height:calc(64px * var(--aitwin-size));margin-bottom:calc(4px * var(--aitwin-size));width:calc(64px * var(--aitwin-size))}.aitwin-widget__logo-draw{stroke-dasharray:1;stroke-dashoffset:1;animation:aitwin-widget-logo-draw 2.6s ease-in-out forwards}.aitwin-widget__logo-draw--loop{animation:aitwin-widget-logo-draw-loop 2.6s ease-in-out infinite}@keyframes aitwin-widget-logo-draw{to{stroke-dashoffset:0}}@keyframes aitwin-widget-logo-draw-loop{0%{stroke-dashoffset:1}45%{stroke-dashoffset:0}55%{stroke-dashoffset:0}to{stroke-dashoffset:-1}}@media(prefers-reduced-motion:reduce){.aitwin-widget__logo-draw,.aitwin-widget__logo-draw--loop{animation:none;stroke-dashoffset:0}}.aitwin-widget__face-loading-bar-wrap{max-width:calc(220px * var(--aitwin-size));width:100%}.aitwin-widget__face-loading-track{background:#fff3;border-radius:999px;height:calc(4px * var(--aitwin-size));overflow:hidden;width:100%}.aitwin-widget__face-loading-fill{background:var(--aitwin-accent);border-radius:999px;height:100%;transition:width .3s ease-out}.aitwin-widget__face-loading-pct{color:#ffffffbf;font-size:calc(12px * var(--aitwin-size));margin:calc(8px * var(--aitwin-size)) 0 0;text-align:center}.aitwin-widget__composer{display:grid;gap:var(--aitwin-action-gap);grid-template-columns:1fr auto;width:100%}.aitwin-widget__composer-input{background:#ffffff0a;border:1px solid var(--aitwin-border);border-radius:var(--aitwin-action-radius);color:var(--aitwin-text);font-family:inherit;font-size:calc(12px * var(--aitwin-size));min-width:0;outline:none;padding:calc(8px * var(--aitwin-size)) calc(10px * var(--aitwin-size))}.aitwin-widget__composer-input:focus{border-color:#f973168c}.aitwin-widget__composer-send{background:var(--aitwin-accent);border:none;border-radius:var(--aitwin-action-radius);color:#fff;cursor:pointer;font-family:inherit;font-size:calc(12px * var(--aitwin-size));font-weight:600;padding:0 calc(12px * var(--aitwin-size))}.aitwin-widget__composer-send:disabled{cursor:not-allowed;opacity:.45}.aitwin-widget__controls-error{color:#fecaca;font-size:calc(11px * var(--aitwin-size));margin:0}.aitwin-widget--float{bottom:20px;filter:drop-shadow(0 12px 32px rgba(0,0,0,.45));height:fit-content;margin:0;max-width:calc(100vw - 40px);position:fixed;right:20px;width:fit-content;z-index:2147483000}",li="data-aitwin-widget-style";function ds(){if(typeof document>"u")return;const e=fc.trim();if(!e){console.warn("[AiTwinWidget] styles.css?inline resolved empty — chrome will be unstyled");return}const t=document.querySelector(`style[${li}]`);if(t){if((t.textContent??"").trim().length>0)return;t.textContent=e;return}const n=document.createElement("style");n.setAttribute(li,"1"),n.textContent=e,(document.head??document.documentElement).appendChild(n)}typeof document<"u"&&ds();const mc=h.forwardRef(function(t,n){ds();const r=h.useRef(null);h.useImperativeHandle(n,()=>({speakText:async(s,o)=>{const a=r.current?.getTwin();if(!a)throw new Error("AiTwinWidget: twin is not mounted");return a.speakText(s,o)},speakWithReply:async(s,o)=>{const a=r.current?.getTwin();if(!a)throw new Error("AiTwinWidget: twin is not mounted");return a.speakWithReply(s,o)},connect:async s=>{const o=r.current?.getTwin();if(!o)throw new Error("AiTwinWidget: twin is not mounted");return o.connect(s)},disconnect:async()=>{const s=r.current?.getTwin();if(s)return s.disconnect()},stop:()=>{r.current?.getTwin()?.stop()},isReady:()=>!!r.current?.getTwin()?.isReady(),isConnected:()=>!!r.current?.getTwin()?.isConnected(),getTwin:()=>r.current?.getTwin()??null}),[]);const i=!!t.float;return F.jsx("div",{className:i?"aitwin-widget aitwin-widget--float":"aitwin-widget",children:F.jsx(dc,{ref:r,options:t})})}),hc=new Set([".png",".jpg",".jpeg",".webp"]);function fs(e=".png"){const t=e.toLowerCase();return hc.has(t)?`source${t}`:"source.png"}function pc(e,t=Xn,n=".png"){const r=e.trim();if(!r)throw new Error("faceId is required");return`${t.replace(/\/$/,"")}/${r}/${fs(n)}`}async function gc(e){try{const t=await e.json();if(typeof t.detail=="string"&&t.detail.trim())return t.detail;if(typeof t.error=="string"&&t.error.trim())return t.error}catch{}return`avatarReply failed (${e.status})`}async function wc(e){const t=e.message.trim();if(!t)throw new Error("Message is required");const n=e.authToken.trim();if(!n)throw new Error("Auth token is required");const r=(e.apiBase?.trim()||Ct).replace(/\/$/,""),i=await fetch(`${r}/avatarReply`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`,Accept:"text/event-stream"},body:JSON.stringify({message:t,history:e.history??[],brainId:e.brainId?.trim()||e.personaId?.trim()||"",tenant:"aiTwin"})});if(!i.ok)throw new Error(await gc(i));if(!i.body)throw new Error("avatarReply response body is empty");const s=i.body.getReader(),o=new TextDecoder;let a="",u="",d="",l="";for(;;){const{done:c,value:f}=await s.read();if(c)break;a+=o.decode(f,{stream:!0});const g=a.split(`
48
- `);a=g.pop()||"";for(const T of g){if(T.startsWith("event: ")){l=T.slice(7).trim();continue}if(!T.startsWith("data: "))continue;const m=T.slice(6).trim();if(!m)continue;let I;try{I=JSON.parse(m)}catch{continue}if(l==="spokenText")u=I.text||"",e.onSpokenText?.(u);else if(l==="displayText")d=I.text||"";else if(l==="error")throw new Error(I.error||"avatarReply stream error")}}if(!u.trim()||!d.trim())throw new Error("avatarReply stream missing displayText or spokenText");return{displayText:d.trim(),spokenText:u.trim()}}exports.AI_TWIN_THUMBNAIL_FILENAME=rr;exports.AiTwin=ls;exports.AiTwinNotFoundError=rn;exports.AiTwinWidget=mc;exports.CONSONANT_SKIP_GROUP=Pi;exports.DEFAULT_TTS_ENGINE_ID=ht;exports.DEFAULT_TTS_LANGUAGE=js;exports.EMOTION_TO_EYEBROW=es;exports.EXPRESSION_ATLAS_EMOTIONS=Oa;exports.MIC_SAMPLE_RATE=ur;exports.OCULUS_VISEME_IDS=xi;exports.PLAYBACK_SAMPLE_RATE=dr;exports.SPEAKING_RATE_MAX=vn;exports.SPEAKING_RATE_MIN=yn;exports.TTS_ENGINE_CARTESIA=st;exports.TTS_ENGINE_GOOGLE=Zn;exports.TTS_ENGINE_INWORLD=dn;exports.TTS_ENGINE_VISEMETTS=at;exports.TTS_LANGUAGE_CATALOG=pt;exports.TTS_LANGUAGE_CODES=Ti;exports.VISEME_IDS=xa;exports.VISEME_SKIP_GAP_MS=$n;exports.VISEME_TEST_CARTESIA_VOICE_ID=lr;exports.VISEME_TEST_SPEAKING_RATE=Ht;exports.VISEME_TEST_VISEMETTS_VOICE_ID=fn;exports.VISEME_TEST_VOICE_ID=cr;exports.VISEME_VTIME_GRID_MS=ir;exports.VOWEL_SKIP_GROUP=Mi;exports.aiTwinSourceFilename=fs;exports.buildPlaybackTransitions=Ni;exports.buildPlaybackVisemeSeries=gn;exports.buildRawVisemeTransitions=Li;exports.buildVoiceWebSocketUrl=ji;exports.compactSkippableVisemes=ar;exports.createAvatarTtsLipsyncController=jn;exports.createRealtimeVoiceLipsyncController=Ki;exports.emotionToEyebrow=gt;exports.faceAssetUrls=Hn;exports.fetchAiTwin=pi;exports.fetchAvatarReply=wc;exports.fetchDevAuthToken=nn;exports.getAiTwinSourceUrl=pc;exports.getAiTwinThumbnailUrl=gi;exports.isTtsLanguageCode=ki;exports.isTtsLanguageSupported=Gs;exports.listSkippedVisemeEntries=wn;exports.listSkippedVisemes=ia;exports.normalizeOculusViseme=hn;exports.normalizeStreamEmotion=rt;exports.normalizeTtsLanguage=mn;exports.parsePackedVoiceId=di;exports.parseTwinBrainUrl=Ms;exports.resolveDirectFaceId=wi;exports.resolveTwinBrainId=tr;exports.resolveTwinBrainUrl=nr;exports.resolveVisemeAtTime=zt;exports.resolveVisemeFromPlaybackSeries=Di;exports.resolveWordAtTime=ma;exports.setActiveTwinAssets=yi;exports.snapVisemeQueueToGrid=sr;exports.snapVisemeVtimeToGrid=Ri;exports.splitVoiceIdSpec=it;exports.ttsLanguagesForEngine=Ks;
47
+ `;function ti(e=5){const t=new ve.Filter({glProgram:ve.GlProgram.from({vertex:Fo,fragment:Lo,name:"threshold-key-filter"}),resources:{thresholdUniforms:{uThreshold:{value:e/255,type:"f32"}}}});return t.setThreshold=n=>{t.resources.thresholdUniforms.uniforms.uThreshold=n/255},t.getThreshold=()=>Math.round(t.resources.thresholdUniforms.uniforms.uThreshold*255),t}function No(e,t,n){return e>n||t>n}function Oo(e){return e?e.getParameter(e.MAX_TEXTURE_SIZE):4096}function Uo(e,t){return t!=null&&t>0?Math.floor(t):Oo(e)}function Vo(e,t){const n=document.createElement("canvas");n.width=t.w,n.height=t.h;const r=n.getContext("2d");if(!r)throw new Error("Failed to acquire 2d context for atlas cell crop");return r.drawImage(e,t.x,t.y,t.w,t.h,0,0,t.w,t.h),n}function ls(e){return fetch(e).then(t=>{if(!t.ok)throw new Error(`Failed to load image: ${e}`);return t.blob()}).then(t=>createImageBitmap(t))}async function ni(e,t,n,r,i){const s=n?await Xi(e,i):await fetch(e).then(c=>{if(!c.ok)throw new Error(`Failed to load ${e}`);return c.json()}),o=s.sheets?.[0];if(!o?.path)throw new Error(`${e} has no sheets[0].path`);const a=new Map,u=async(c,f)=>{const g=es(t,f,n),T=n?await ba(await Bt(g,i??"")):await ls(g);if(No(T.width,T.height,r))return console.info(`[PixiVisemeCompositor] Sheet ${f} (${T.width}x${T.height}) exceeds MAX_TEXTURE_SIZE=${r}; using per-cell crops.`),{mode:"cells",bitmap:T};const h=ve.Texture.from(T);return h.source.scaleMode="nearest",{mode:"full",texture:h}};a.set(o.index,await u(o.index,o.path));for(const c of s.sheets??[])c.index!==o.index&&c.path&&a.set(c.index,await u(c.index,c.path));const d=new Map;for(const c of s.cells??[])c.path&&d.set(c.path,c);const l=new Map;for(const c of s.sheets??[])l.set(c.index,c);return{atlasMeta:s,cellByPath:d,sheetByIndex:l,diffBase:t,sheets:a}}async function Wo(e,t={}){const n=new ve.Application;try{await n.init({canvas:e,width:842,height:1264,preference:"webgl",autoStart:!1,antialias:!1,resolution:1,autoDensity:!1,backgroundAlpha:1,backgroundColor:0,powerPreference:"high-performance"})}catch(_){throw n.destroy({removeView:!1}),_}if(n.renderer.type!==ve.RendererType.WEBGL)throw n.destroy({removeView:!1}),new Error("Pixi did not initialize a WebGL renderer");const r=Uo(n.renderer.gl,t.maxTextureSize);let i=!1,s=!1,o=!1;const a=n.canvas,u=()=>{i=!0,!s&&!o&&(s=!0,t.onWebGlContextLost?.())};a.addEventListener("webglcontextlost",u);const d=new ve.Sprite(ve.Texture.EMPTY),l=new ve.Sprite(ve.Texture.EMPTY);l.visible=!1,l.alpha=0;const c=new ve.Sprite(ve.Texture.EMPTY),f=new ve.Sprite(ve.Texture.EMPTY),g=new ve.Sprite(ve.Texture.EMPTY);g.visible=!1;const T=ti(5),h=ti(5);c.filters=[T],f.filters=[h],n.stage.addChild(d),n.stage.addChild(l),n.stage.addChild(c),n.stage.addChild(f),n.stage.addChild(g);let I=null,F=null,N=null,b=null,k=null,V=null,D=null,se=842,le=1264,P=null,H=5;const Q=new Map;let oe=null,ae=null,te=null;const ce=_i(()=>{Q.clear(),V=null,D=null,I=null,P=null}),ye=(_,y)=>{se=_,le=y,n.renderer.resize(_,y),(e.width!==_||e.height!==y)&&(e.width=_,e.height=y)},ne=()=>{c.visible=!1,f.visible=!1,c.texture=ve.Texture.EMPTY,f.texture=ve.Texture.EMPTY,oe=null,ae=null},W=()=>{k=null,l.visible=!1,l.alpha=0},M=()=>{d.visible=!0,d.width=se,d.height=le,d.x=0,d.y=0},Y=()=>{l.width=se,l.height=le,l.x=0,l.y=0},R=()=>{g.visible=!1,W(),I&&(d.texture=I,M())},z=()=>{k&&R()},X=_=>{if(o||i||!I){R();return}const y=Math.max(0,_);if(!F||y<=0){R();return}ne(),g.visible=!1,d.texture=I,M(),l.texture=F,Y(),l.alpha=1,l.visible=!0,k={startMs:performance.now(),durationMs:y},J()},ie=(_=performance.now())=>{if(!k)return!1;if(o||i)return z(),!1;const y=(_-k.startMs)/Math.max(1,k.durationMs);if(y>=1)return R(),J(),!1;const S=Math.min(1,Math.max(0,y)),G=S*S*(3-2*S);return l.alpha=1-G,l.visible=!0,J(),!0},v=()=>{if(N){try{N.resource=null}catch{}N.destroy(),N=null}F&&(F.destroy(!1),F=null),b=null},U=(_,y)=>{const S=_.cellByPath.get(y);if(!S||!Qi(S))return null;const G=`${_.diffBase}::${y}`;let ee=Q.get(G);if(!ee){const ue=S.sheet??0,me=_.sheets.get(ue);if(!me)return null;let x,q=!1;if(me.mode==="full")x=new ve.Texture({source:me.texture.source,frame:new ve.Rectangle(S.x,S.y,S.w,S.h)});else{const ge=Vo(me.bitmap,S);x=ve.Texture.from(ge),x.source.scaleMode="nearest",q=!0}ee={texture:x,ownsSource:q},Q.set(G,ee)}return{texture:ee.texture,cell:S}},L=(_,y,S,G)=>{if(!G||!S)return _.visible=!1,_.texture=ve.Texture.EMPTY,!1;const ee=U(S,G);if(!ee)return _.visible=!1,_.texture=ve.Texture.EMPTY,!1;const ue=Yi(S.atlasMeta,ee.cell);return y.setThreshold(H),_.texture=ee.texture,_.x=ue.x,_.y=ue.y,_.width=ee.cell.w,_.height=ee.cell.h,_.visible=!0,!0},Z=_=>{if(!_||!V)return null;const y=P??new Set(V.cellByPath.keys());return hr(_,y)??(y.has(_)?_:null)},$=_=>{g.visible=!1,R();const y=Z(_.mouthPath),S=_.expressionPath&&D?gr(_.expressionPath,G=>D.cellByPath.has(G)):null;y!==oe?(L(c,T,V,y),oe=y):y?c.visible=!0:c.visible=!1,S!==ae?(L(f,h,D,S),ae=S):S?f.visible=!0:f.visible=!1},J=()=>{if(!(o||i||document.hidden))try{n.render()}catch{}};return{async loadAssets(_){if(o)return{width:se,height:le,visemePaths:[]};const y=zt();if(y&&!_)throw new Error("Encrypted assets enabled but no key provided");const S=y?await Ta(await Bt(cn(),_??"")):await ls(cn());I&&I.destroy(!0),I=ve.Texture.from(S),I.source.scaleMode="linear";const G=bi();V=await ni(Si(),G,y,r,_);try{D=await ni(Ti(),G,y,r,_)}catch(ue){console.warn("[PixiVisemeCompositor] expression atlas unavailable:",ue),D=null}const ee=qi(V.atlasMeta,S.width,S.height);return ye(ee.width,ee.height),H=Sn(V.atlasMeta),T.setThreshold(H),h.setThreshold(H),P=new Set(V.cellByPath.keys()),Q.clear(),R(),ne(),J(),{width:ee.width,height:ee.height,visemePaths:[...V.cellByPath.keys()]}},getVisemeAtlasPaths(){return P},drawLiveFrame(_){o||i||!I||(z(),$(_),J())},drawIdleVideo(_,y){o||i||document.hidden||y&&!y()||_.readyState<HTMLMediaElement.HAVE_CURRENT_DATA||(k=null,ne(),g.visible=!1,I&&(d.texture=I,M()),!F||b!==_?(v(),N=new ve.VideoSource({resource:_,autoPlay:!1,updateFPS:0}),N.autoUpdate=!1,F=new ve.Texture({source:N}),b=_):F.source.update(),l.texture=F,Y(),l.alpha=1,l.visible=!0,J())},beginIdleToSilCrossfade:X,tickIdleToSilCrossfade:ie,finishIdleToSilCrossfade:z,isIdleToSilCrossfading(){return k!=null},renderSilOnly(){o||i||!I||(ne(),R(),J())},blitBitmap(_){o||i||(z(),ne(),te&&(te.destroy(!0),te=null),te=ve.Texture.from(_),g.texture=te,g.visible=!0,g.x=0,g.y=0,g.width=se,g.height=le,d.visible=!1,J())},async compositePrefetchFrame(_){return null},renderFrame:J,isContextLost(){return i},dispose(){if(o)return;o=!0,a.removeEventListener("webglcontextlost",u),ce(),v(),te&&(te.destroy(!0),te=null);for(const{texture:y,ownsSource:S}of Q.values())y.destroy(S);Q.clear(),I&&(I.destroy(!0),I=null);const _=y=>{y.mode==="full"?y.texture.destroy(!0):y.bitmap.close()};if(V)for(const y of V.sheets.values())_(y);if(D)for(const y of D.sheets.values())_(y);T.destroy(!0),h.destroy(!0),n.destroy({removeView:!1},{children:!0,texture:!0})}}}const ri=-1,rn=1e5,ii=new Map;async function Ho(e){let t=ii.get(e);if(!t){const n=await fetch(e);if(!n.ok)throw new Error(`Failed to fetch worker script (${n.status}): ${e}`);const r=await n.blob();t=URL.createObjectURL(r),ii.set(e,t)}return new Worker(t,{name:"visemeDiffPreview"})}function $o(){return typeof Worker<"u"&&typeof OffscreenCanvas<"u"}function Xn(e,t){(e.width!==t.width||e.height!==t.height)&&(e.width=t.width,e.height=t.height);const n=e.getContext("2d");n&&n.drawImage(t,0,0)}function si(e,t){let n=0,r=null;const i=Sr(async(s,o)=>(await Yn(),await Qn(e,s),i.isLatestSeq(o)));return{usesWorker:!1,usesPixi:!1,async loadAssets(s){const o=await Yn(s);return r=o.visemePaths?new Set(o.visemePaths):null,o},getVisemeAtlasPaths(){return r},async renderSilOnly(){n+=1,i.reset(),await oo(e)},drawLiveFrame(s,o,a,u){i.enqueue(o,a,u)},async compositePrefetchFrame(s,o){try{return await ao(s)}catch{return null}},blitBitmap(s,o){Xn(s,o)},drawIdleVideo(){},beginIdleToSilCrossfade(){},tickIdleToSilCrossfade(){return!1},finishIdleToSilCrossfade(){},isIdleToSilCrossfading(){return!1},supportsBufferedPlayback:!0,async renderViseme(s,o){n+=1;const a=n;t.onStatus?.(o),await Qn(s,{mouthPath:Pt(o),expressionPath:null})},dispose(){n+=1,i.reset()}}}function zo(e,t,n){let r=null,i=1,s=0,o=!1,a=!1,u=null;const d=new Map,l=new Map;let c=rn;const f=Sr(async(N,b,k)=>(d.set(b,{drawKey:k.drawKey,onDisplayed:k.onDisplayed}),await I({type:"drawFrame",diffPath:N.mouthPath,expressionDiffPath:N.expressionPath,liveSeq:b},void 0,!0),!1)),g=N=>{if(N===ri){let V=-1,D;for(const[se,le]of d)se>=V&&(V=se,D=le);V>=0&&d.delete(V),D?.onDisplayed?.(D.drawKey);return}const b=-N,k=d.get(b);d.delete(b),k?.onDisplayed?.(k.drawKey)},T=new Map,h=(N,b)=>{a||!r||r.postMessage(N,[])},I=(N,b,k=!1)=>{if(a)return Promise.resolve();const V=i++;return new Promise((D,se)=>{T.set(V,{resolve:le=>D(le),reject:se,generation:typeof N.generation=="number"?N.generation:void 0,expectRenderDone:k}),h({...N,requestId:V})})},F=(async()=>{r=await Ho(n),r.onmessage=N=>{if(a)return;const b=N.data;if(b.type==="status"){t.onStatus?.(b.label);return}if(b.type==="frame"){if(b.generation<=-rn){const D=-b.generation,se=l.get(D);l.delete(D),se?.resolve(b.bitmap);return}const V=b.generation<0;if(!V&&b.generation!==s){b.bitmap.close();return}if(V&&!(b.generation===ri)){const se=-b.generation;if(!f.isLatestSeq(se)){ut()?.onWorkerStale(),b.bitmap.close();return}}if(V&&t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()){ut()?.onWorkerStale(),b.bitmap.close();return}Xn(e,b.bitmap),b.bitmap.close(),V&&g(b.generation);return}const k=T.get(b.requestId);if(k)switch(b.type){case"ready":T.delete(b.requestId),k.resolve();break;case"loadAssetsDone":T.delete(b.requestId),u=b.visemePaths?.length?new Set(b.visemePaths):null,k.resolve({width:b.width,height:b.height,visemePaths:b.visemePaths});break;case"renderDone":if(k.expectRenderDone&&k.generation!=null&&b.generation!==k.generation)return;T.delete(b.requestId),k.resolve();break;case"renderAborted":T.delete(b.requestId),k.resolve();break;case"error":T.delete(b.requestId),t.onError?.(b.message),k.reject(new Error(b.message));break}},r.onerror=N=>{if(a)return;const b=N.message||"Worker error";t.onError?.(b);for(const[,k]of T)k.reject(new Error(b));T.clear()},await I({type:"init"}),o=!0})();return{usesWorker:!0,usesPixi:!1,async loadAssets(N){if(a)return{width:842,height:1264};if(await F,a)return{width:842,height:1264};if(!o)throw new Error("Worker init failed");return await I({type:"loadAssets",keyHex:N,urls:Vs(yt())})??{width:842,height:1264}},getVisemeAtlasPaths(){return u},async renderSilOnly(){if(a||(await F,a))return;s+=1,f.reset(),d.clear(),l.forEach(({resolve:b})=>b(null)),l.clear(),c=rn;const N=s;await I({type:"cancelLiveDraws"}),await I({type:"renderSil",generation:N},void 0,!0)},drawLiveFrame(N,b,k,V){a||F.then(()=>{a||f.enqueue(b,k,V)})},compositePrefetchFrame(N,b){return a?Promise.resolve(null):F.then(()=>{if(a)return null;const k=++c;return new Promise(V=>{l.set(k,{resolve:V}),I({type:"drawFrame",diffPath:N.mouthPath,expressionDiffPath:N.expressionPath,liveSeq:k},void 0,!0).catch(()=>{l.delete(k),V(null)})})})},blitBitmap(N,b){Xn(N,b)},drawIdleVideo(){},beginIdleToSilCrossfade(){},tickIdleToSilCrossfade(){return!1},finishIdleToSilCrossfade(){},isIdleToSilCrossfading(){return!1},supportsBufferedPlayback:!0,async renderViseme(N,b,k={}){if(a||(await F,a))return;s+=1,f.reset(),d.clear();const V=s;try{await I({type:"cancelLiveDraws"}),await I({type:"renderViseme",generation:V,to:b,from:k.from,transitionDurationMs:k.transitionDurationMs,gapMs:k.gapMs,threshold:k.threshold},void 0,!0)}catch(D){if(D?.name==="AbortError")return;throw D}},dispose(){if(!a){a=!0,s+=1,f.reset(),d.clear(),l.forEach(({resolve:N})=>N(null)),l.clear(),c=rn;for(const[,N]of T)N.resolve();T.clear(),r?.terminate(),r=null}}}}async function jo(e,t){const n=await Wo(e,{onWebGlContextLost:t.onWebGlContextLost,maxTextureSize:t.pixiMaxTextureSize});let r=0,i=null,s=!1;const o=Sr(async(a,u)=>s?!1:t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()?(ut()?.onWorkerStale(),!1):(n.drawLiveFrame(a),o.isLatestSeq(u)));return{usesWorker:!1,usesPixi:!0,async loadAssets(a){if(s)return{width:842,height:1264};const u=await n.loadAssets(a);return i=u.visemePaths?.length?new Set(u.visemePaths):null,u},getVisemeAtlasPaths(){return i},async renderSilOnly(){s||(r+=1,o.reset(),n.renderSilOnly())},drawLiveFrame(a,u,d,l){s||o.enqueue(u,d,l)},async compositePrefetchFrame(a,u){if(s)return null;try{return await n.compositePrefetchFrame(a)}catch{return null}},blitBitmap(a,u){s||t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()||n.blitBitmap(u)},drawIdleVideo(a,u){s||n.drawIdleVideo(a,u)},beginIdleToSilCrossfade(a){s||n.beginIdleToSilCrossfade(a)},tickIdleToSilCrossfade(a){return s?!1:n.tickIdleToSilCrossfade(a)},finishIdleToSilCrossfade(){s||n.finishIdleToSilCrossfade()},isIdleToSilCrossfading(){return s?!1:n.isIdleToSilCrossfading()},supportsBufferedPlayback:!1,async renderViseme(a,u){if(s)return;r+=1;const d=r;o.reset(),t.onStatus?.(u),n.drawLiveFrame({mouthPath:Pt(u),expressionPath:null})},dispose(){s||(s=!0,r+=1,o.reset(),n.dispose())}}}function ai(e,t){if(!$o())return si(e,t);const n=Ss(t.workerScriptUrl);try{return zo(e,t,n)}catch{return si(e,t)}}function Ko(e,t={}){if(t.preferLegacyRenderer)return ai(e,t);let n=null,r=!1,i=null,s=!1;const o=()=>n?Promise.resolve(n):(i||(i=(async()=>{try{const a=await jo(e,t);if(s)throw a.dispose(),new Error("Renderer disposed during Pixi init");return r=!0,n=a,a}catch(a){if(s)throw a instanceof Error?a:new Error(String(a));return console.warn("[AiTwin] Pixi WebGL renderer unavailable, using Canvas2D fallback:",a),n=ai(e,t),r=!1,n}})()),i);return{get usesWorker(){return n?.usesWorker??!1},get usesPixi(){return r},async loadAssets(a){return(await o()).loadAssets(a)},getVisemeAtlasPaths(){return n?.getVisemeAtlasPaths()??null},async renderSilOnly(){return(await o()).renderSilOnly()},drawLiveFrame(a,u,d,l){o().then(c=>{s||c.drawLiveFrame(a,u,d,l)})},async compositePrefetchFrame(a,u){return(await o()).compositePrefetchFrame(a,u)},blitBitmap(a,u){n&&n.blitBitmap(a,u)},drawIdleVideo(a,u){n?.usesPixi&&n.drawIdleVideo(a,u)},beginIdleToSilCrossfade(a){n?.beginIdleToSilCrossfade(a)},tickIdleToSilCrossfade(a){return n?.tickIdleToSilCrossfade(a)??!1},finishIdleToSilCrossfade(){n?.finishIdleToSilCrossfade()},isIdleToSilCrossfading(){return n?.isIdleToSilCrossfading()??!1},get supportsBufferedPlayback(){return n?.supportsBufferedPlayback??!1},async renderViseme(a,u,d){return(await o()).renderViseme(a,u,d)},dispose(){s=!0,i=null,n?.dispose(),n=null}}}class Go extends Error{constructor(t,n){super(`${t} timed out after ${Math.round(n/1e3)}s`),this.name="LoadTimeoutError"}}function Un(e,t,n){return new Promise((r,i)=>{const s=globalThis.setTimeout(()=>{i(new Go(n,t))},t);e.then(o=>{globalThis.clearTimeout(s),r(o)},o=>{globalThis.clearTimeout(s),i(o)})})}const qo={overflow:"hidden",position:"relative",display:"inline-block",lineHeight:0},Yo={display:"block",maxWidth:"100%",height:"auto"},Qo={position:"absolute",inset:0,display:"flex",alignItems:"center",justifyContent:"center",padding:"1rem",background:"rgba(0,0,0,0.55)",color:"#fff",fontSize:"0.875rem",textAlign:"center"};function Vn(e){return hi(e)?dr:pi(e)?hn:ur}function rt(e,t){return t?.isSpeaking()||t?.isWarmingUp()?t:e.isSpeaking()||e.isWarmingUp()?e:t?.isConnected()?t:e}const us=m.forwardRef(function({id:t,avatarId:n,faceId:r,assets:i,authToken:s,apiBase:o,ttsEngineId:a,voiceId:u,language:d,speakingRate:l=Kt,className:c,style:f,canvasStyle:g,onStatusChange:T,onDisplayStatus:h,onError:I,onReady:F,showErrorOverlay:N=!0,loadingThumbnailUrl:b,onUserTranscript:k,onBotOutput:V,onRealtimeLipsyncDebug:D,onFrameRenderStats:se,lipsyncBufferedPlayback:le=!0,lipsyncPlaybackWarmupMs:P=1500,idleHandoffHoldMs:H=bo,pixiMaxTextureSize:Q,captions:oe},ae){const te=m.useRef(null),ce=m.useRef(null),ye=m.useRef(null),ne=m.useRef(le);ne.current=le;const W=m.useRef(P);W.current=P;const M=m.useRef(H);M.current=H;const Y=m.useRef(0),R=m.useRef(_r()),z=m.useRef(Qa()),X=m.useRef(!1),ie=m.useRef(null),v=m.useRef(null),U=m.useRef(0),L=m.useRef(0),Z=m.useRef(0),$=m.useRef(null),J=m.useRef(null),_=m.useRef(!1),y=m.useRef(!1),S=m.useRef(!1),G=m.useRef(!1),ee=m.useRef(!1),ue=m.useRef("sil"),me=m.useRef(0),x=m.useRef(!1),q=m.useRef(null),ge=m.useRef(!1),[Ne,Ft]=m.useState("idle"),[vt,_t]=m.useState(null),[ct,Tn]=m.useState(!1),[p,B]=m.useState(!1),[re,de]=m.useState(!1),[be,xe]=m.useState(),[Pe,$e]=m.useState({width:842,height:1264}),[Ue,Gt]=m.useState(null),[kn,Lt]=m.useState(0),bt=m.useRef(!1),Oe=m.useRef(s??null),qe=o?.trim()||Dt,St=m.useRef(null),Ie=m.useRef(null),{ttsEngineId:Re,voiceId:Ye}=m.useMemo(()=>{const w=st(a,u),E=w.ttsEngineId??ht;return{ttsEngineId:E,voiceId:w.voiceId??Vn(E)}},[a,u]),Ve=m.useCallback(w=>{const E=hi(w);R.current.holdPp=E,ye.current?.setHoldPp(E)},[]),We=m.useRef(!1),Ae=m.useRef({width:842,height:1264}),De=m.useRef(null),ze=m.useRef("sil"),tt=m.useRef(""),je=m.useMemo(()=>lo(oe),[oe]),He=m.useCallback(()=>{tt.current="",Gt(null)},[]);m.useEffect(()=>{je.enabled||He()},[je.enabled,He]);const Tt=m.useCallback(w=>{if(!je.enabled)return;const E=w.isSpeaking(),C=w.isWarmingUp();if(!E||C){tt.current&&He();return}const A=co(w.getWordQueue());if(!A){tt.current&&He();return}A!==tt.current&&(tt.current=A,Gt(A))},[je.enabled,He]),kt=m.useRef(void 0),En=m.useRef(new Map),Tr=m.useRef(F),kr=m.useRef(T),ke=m.useRef(h),Er=m.useRef(I),xr=m.useRef(k),xn=m.useRef(V),qt=m.useRef(D),Ir=m.useRef(se),Et=m.useRef(_o());Tr.current=F,kr.current=T,ke.current=h,Er.current=I,xr.current=k,xn.current=V,qt.current=D,Ir.current=se,m.useEffect(()=>{const w=Et.current;return Qr(w),()=>Qr(null)},[]);const In=m.useCallback(w=>{const E=st(w?.ttsEngineId??a,w?.voiceId??u),C=E.ttsEngineId??Ie.current?.ttsEngineId??ht,A=E.voiceId??Ie.current?.voiceId??Vn(C);return{ttsEngineId:C,voiceId:A}},[a,u]),nt=m.useCallback(w=>{_t(w),de(!0),Er.current?.(w)},[]),Nt=m.useCallback(w=>{Ft(w),kr.current?.(w)},[]),he=m.useRef(Gn(Nt,qe,{onLipsyncDebug:w=>qt.current?.(w)})),we=m.useRef(null),Ar=m.useRef(()=>{});m.useEffect(()=>(he.current=Gn(Nt,qe,{onLipsyncDebug:w=>qt.current?.(w)}),s&&he.current.setDeveloperToken(s),he.current.setTtsEngineId(Re),()=>{he.current.stop();const w=we.current;w&&(w.disconnect(),we.current=null)}),[Nt,qe]);const Mr=m.useCallback(()=>(we.current||(we.current=Gi(Nt,{speakingRate:l,language:d,onUserTranscript:(w,E)=>xr.current?.(w,E),onBotOutput:w=>xn.current?.(w),onLipsyncDebug:w=>qt.current?.(w)}),we.current.setTtsEngineId(Re),Ar.current(!0)),we.current),[Nt,l,d,Re]),Fe=m.useCallback(()=>{$.current!=null&&(clearTimeout($.current),$.current=null)},[]),Be=m.useCallback(()=>{J.current!=null&&(clearTimeout(J.current),J.current=null),S.current=!1},[]),An=m.useCallback(()=>{ee.current=!0,Be(),q.current?.setLooping(!1)},[Be]),Ke=m.useCallback(()=>{ee.current=!1,q.current?.setLooping(!0)},[]),Ot=m.useCallback(()=>{if(G.current)return;const w=ce.current;if(G.current=!0,ee.current=!0,Be(),_.current=!1,y.current=!1,!ge.current||!q.current){w?.renderSilOnly();return}const E=q.current;E.setLooping(!1),E.setActive(!1);const C=E.getVideo();if(C.ended||Number.isFinite(C.currentTime)&&C.currentTime<=Nn){try{(C.ended||C.currentTime>Nn)&&(C.currentTime=0)}catch{}w?.renderSilOnly(),w?.finishIdleToSilCrossfade();return}if(w?.usesPixi){w.drawIdleVideo(C),w.beginIdleToSilCrossfade(Jr);return}(async()=>{const j=te.current;j&&await Ln(j,C,null,void 0,Ae.current).catch(()=>{}),await new Promise(_e=>setTimeout(_e,Jr)),G.current&&await w?.renderSilOnly()})()},[Be]),Pr=m.useCallback(()=>{if(!(!ge.current||!q.current)){if(G.current=!1,ce.current?.finishIdleToSilCrossfade(),ce.current?.renderSilOnly(),Be(),ee.current){S.current=!1;return}S.current=!0,J.current=setTimeout(()=>{if(J.current=null,rt(he.current,we.current).isSpeaking()){S.current=!1;return}if(_.current){S.current=!1;return}if(ee.current){S.current=!1;return}const w=q.current;if(!w){S.current=!1;return}Ke(),w.setLooping(!0),w.restart(),w.setActive(!0);const E=w.getVideo();let C=!1;const A=()=>{C||(C=!0,E.removeEventListener("playing",j),S.current=!1)},j=()=>{A()};if(E.addEventListener("playing",j,{once:!0}),E.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA&&!E.paused){A();return}setTimeout(A,300)},M.current)}},[Be,Ke]),xt=m.useCallback(()=>{if(Fe(),ke.current?.("sil"),_.current=!1,G.current=!1,!ge.current||!q.current){S.current=!1,Ke();return}Pr()},[Fe,Pr,Ke]),Mn=m.useCallback(async w=>{const E=L.current,C=ce.current,A=te.current,j=()=>{w?.fallbackOnly||(_.current=!1)};if(!C||!A||!ct){j();return}if(rt(he.current,we.current).isSpeaking()){j();return}if(w?.fallbackOnly){if(!_.current||(Le(R.current),await C.renderSilOnly(),E!==L.current)||rt(he.current,we.current).isSpeaking())return;xt();return}if(Zr(R.current)){Le(R.current),xt();return}if(ne.current&&C.supportsBufferedPlayback&&!w?.fallbackOnly){if(Le(R.current),ye.current?.reset(),await C.renderSilOnly(),E!==L.current||rt(he.current,we.current).isSpeaking())return;xt();return}const Se=Io(rt(he.current,we.current).getVisemeQueue(),me.current,ue.current),Ce=Ao(Se,De.current);if(Le(R.current),!Ce){if(await C.renderSilOnly(),E!==L.current||rt(he.current,we.current).isSpeaking())return;xt();return}try{await C.renderViseme(A,"sil",{from:Se,transitionDurationMs:as})}catch(Ee){if(Ee?.name!=="AbortError")throw Ee}if(E!==L.current){j();return}if(rt(he.current,we.current).isSpeaking()){j();return}xt()},[ct,xt]),dt=m.useCallback(()=>{if(y.current||_.current)return;y.current=!0,_.current=!0,Fe();const w=L.current;Mn().finally(()=>{y.current=!1}),$.current=setTimeout(()=>{$.current=null,w===L.current&&Mn({fallbackOnly:!0})},Mo())},[Fe,Mn]),Rr=m.useCallback(async()=>{const w=Math.floor(Date.now()/1e3);if(St.current&&w<St.current.expiresAt-60)return St.current.key;const E=Oe.current??await sn();Oe.current=E,he.current.setDeveloperToken(E);const C=await Ls(E,qe);return St.current={key:C.value,expiresAt:C.expiresAt},C.value},[qe]);m.useEffect(()=>{s&&(Oe.current=s,he.current.setDeveloperToken(s))},[s]),m.useEffect(()=>{he.current.setTtsEngineId(Re),we.current?.setTtsEngineId(Re),Ie.current&&(Ie.current.ttsEngineId=Re),Ve(Re)},[Re,Ve]),m.useEffect(()=>{Ie.current&&(Ie.current.voiceId=Ye)},[Ye]);const Qe=m.useMemo(()=>yi(n,r),[n,r]),ms=m.useMemo(()=>i?JSON.stringify(i):Qe?`face:${Qe}`:`id:${t??""}`,[i,Qe,t]),hs=m.useMemo(()=>{const w=b?.trim();return w||(Br(Qe)??Br(be)??Ps(i))},[b,Qe,be,i]);return m.useEffect(()=>{if(!t&&!i&&!Qe){nt("AiTwin requires `assets`, `avatarId`/`faceId`, or `id`");return}let w=!1;We.current=!1,Tn(!1),B(!1),de(!1),xe(Qe),_t(null),Ie.current=null,q.current=null,ge.current=!1;const E=te.current;if(!E)return;let C=null;return(async()=>{try{let A,j,_e,Se,Ce;if(i)A=i,j=Re,_e=Ye;else if(Qe)ke.current?.("loadFace"),A=jn(Qe),j=Re,_e=Ye;else{ke.current?.("getAiTwin");const K=await Un(gi(t),3e4,"getAiTwin");if(w)return;if(!K.faceId?.trim())throw new Error("getAiTwin returned no faceId");xe(K.faceId.trim());const Te=st(a,u);j=Te.ttsEngineId??K.ttsEngineId,_e=Te.voiceId??K.voiceId??Vn(j),Se=rr(K),Ce=ir(K),A=jn(K.faceId)}Ie.current={ttsEngineId:j,voiceId:_e,brainId:Se,brainUrl:Ce},vi(A),q.current=Xa();const Ee=!!(i&&!wt(i));C=Ko(E,{preferLegacyRenderer:Ee,pixiMaxTextureSize:Q,onStatus:K=>{w||ke.current?.(K)},onError:K=>{w||nt(K)},shouldAcceptLiveFrame:()=>{if(X.current)return!0;if(_.current)return!1;const K=rt(he.current,we.current);return K.isSpeaking()||K.isWarmingUp()},onWebGlContextLost:()=>{w||bt.current||(bt.current=!0,queueMicrotask(()=>{w||Lt(K=>K+1)}))}}),ce.current=C,ye.current=Bo(C),Ve(j),bt.current=!1;const Xe=K=>{const Te=()=>{if(!K||W.current<=0)return!1;const Me=ce.current;return Me?ne.current&&Me.supportsBufferedPlayback?!0:Me.usesPixi:!1};he.current.setLipsyncWarmupHold(K,Te),we.current?.setLipsyncWarmupHold(K,Te)};Ar.current=Xe,Xe(!0),ke.current?.("auth");const Yt=Oe.current??s??await Un(sn(),3e4,"getAuthToken");if(w)return;Oe.current=Yt,he.current.setDeveloperToken(Yt),he.current.setTtsEngineId(j);const pe=zt()?await Rr():void 0;if(w)return;ke.current?.("loadAssets");const fe=await Un(C.loadAssets(pe),18e4,"load face assets");if(!w&&fe.width>0&&fe.height>0&&(Ae.current=fe,$e(fe)),w)return;De.current=C.getVisemeAtlasPaths(),We.current=!0,Tn(!0),Tr.current?.(),C.renderSilOnly().then(()=>{w||ke.current?.("sil")}),(async()=>{try{const K=await xa(pe);if(await q.current.load(K),w||(ge.current=!0,B(!0),Le(R.current),await C.renderSilOnly(),w)||(await new Promise(Me=>setTimeout(Me,M.current)),w))return;q.current.setActive(!0);const Te=q.current.getVideo();C.usesPixi?C.drawIdleVideo(Te):await Ln(E,Te,null,void 0,Ae.current)}catch(K){if(w)return;ge.current=!1,B(!1),console.warn("[AiTwin] Idle video unavailable:",K)}finally{w||(de(!0),ke.current?.("idle"))}})()}catch(A){if(w)return;if(A instanceof an){nt(A.message);return}nt(A instanceof Error?A.message:"Failed to load AI twin")}})(),()=>{w=!0,q.current?.dispose(),C?.dispose(),ce.current=null}},[ms,i,Qe,t,Re,Ye,a,u,s,Q,Rr,nt,kn,Ve]),m.useEffect(()=>{const w={current:!1},E=()=>{const C=te.current,A=ce.current,j=rt(he.current,we.current);if(!C||!A||!ct){U.current=requestAnimationFrame(E);return}if(document.hidden){q.current?.setActive(!1),U.current=requestAnimationFrame(E);return}const _e=j.isSpeaking(),Se=j.isWarmingUp(),Ce=_e&&!x.current,Ee=x.current&&!_e;Ce&&(Fe(),Be(),_.current=!1,y.current=!1,Le(R.current),ye.current?.reset(),Et.current.reset(),kt.current=void 0,Z.current=0,ue.current="sil",me.current=0,Ot()),Ee&&(z.current.reset(),q.current?.setActive(!1),ye.current?.reset(),A.finishIdleToSilCrossfade(),He(),ee.current=!1,dt());const Xe=Ee||_.current||S.current||G.current,Yt=le&&A.supportsBufferedPlayback;if(ee.current&&!_e&&!Se&&!Ee&&!_.current&&!S.current){if(G.current){A.usesPixi&&A.isIdleToSilCrossfading()&&A.tickIdleToSilCrossfade(performance.now()),x.current=_e,U.current=requestAnimationFrame(E);return}if(ge.current&&q.current){const pe=q.current.getVideo();if(pe.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA){const fe=pe.duration,K=pe.currentTime,Te=pe.ended||Number.isFinite(K)&&K<=Nn,Me=Number.isFinite(fe)&&fe>0&&Number.isFinite(K)?Math.max(0,(fe-K)*1e3):Number.POSITIVE_INFINITY;if(Te||Me<=So){Ot(),A.usesPixi&&A.isIdleToSilCrossfading()&&A.tickIdleToSilCrossfade(performance.now()),x.current=_e,U.current=requestAnimationFrame(E);return}}}}if(Se){if(Ot(),ge.current&&q.current?.setActive(!1),A.usesPixi&&A.isIdleToSilCrossfading()){A.tickIdleToSilCrossfade(performance.now()),j.getVisemeQueue().length>0&&Y.current===0&&(Y.current=performance.now()),x.current=j.isSpeaking(),U.current=requestAnimationFrame(E);return}const pe=j.getVisemeQueue();pe.length>0&&Y.current===0&&(Y.current=performance.now()),z.current.advance(performance.now());const fe=z.current.getExpressionPath(),K=z.current.getDrawKey(),Te={queue:pe,elapsedMs:0,expressionPath:fe,expressionDrawKey:K,availablePaths:De.current};let Me=!1;Yt&&ye.current?(ye.current.warmupTick(Te),Me=ye.current.isWarmupReady(Te,W.current)):A.usesPixi&&ei(C,pe,0,fe,K,R.current,(It,Qt,Pn,Xt)=>{A.drawLiveFrame(It,Qt,Pn,Je=>{Xt?.(Je),j.isWarmingUp()&&Je!==""&&!Zr({lastDrawnKey:Je})&&(Y.current=0,j.releaseWarmupHold())})},It=>{ke.current?.(It.label)},De.current);const lt=Y.current>0&&performance.now()-Y.current>ks;ke.current?.(Me?"buffer ready":"buffering lipsync…"),(Me||lt)&&(Y.current=0,j.releaseWarmupHold()),Tt(j),x.current=j.isSpeaking(),U.current=requestAnimationFrame(E);return}if(_e){Ot(),ge.current&&q.current?.setActive(!1),A.usesPixi&&A.isIdleToSilCrossfading()&&A.finishIdleToSilCrossfade();const pe=j.getVisemeQueue(),fe=j.getPlaybackElapsedMs();fa(pe,fe),me.current=fe;const K=pe.length>0?jt(pe,fe):"sil";K!=="sil"&&(ue.current=K);const Te=to(fe,j.getSentenceEmotions(),j.getWordQueue(),j.getStreamMood());z.current.setTargetEmotion(Te),z.current.setTargetEyebrow(qa(fe,j.getWordQueue(),gt(Te))),z.current.advance(performance.now());const Me=z.current.getExpressionPath(),lt=z.current.getDrawKey(),It=(Je,Rn,Ut,Cn)=>{const Cr={label:ze.current,elapsedMs:fe,mouthPath:Rn.mouthPath,transitionMeta:kt.current};En.current.set(Ut,Cr),ye.current?.markExternallyDisplayed(Ut,Me),A.drawLiveFrame(Je,Rn,Ut,Jt=>{const ps=En.current.get(Jt)??Cr;En.current.delete(Jt),Et.current.onDrawDisplayed(ps,pe),ye.current?.markExternallyDisplayed(Jt,Me),Cn?.(Jt)})},Qt=Je=>{ze.current=Je.label,kt.current=Je.transitionMeta,ke.current?.(Je.label)},Pn=le&&A.supportsBufferedPlayback;let Xt=!1;Pn&&ye.current&&(Xt=ye.current.tick({canvas:C,queue:pe,elapsedMs:fe,expressionPath:Me,expressionDrawKey:lt,playbackState:R.current,availablePaths:De.current,onStatus:Qt,onDisplayed:(Rn,Ut,Cn)=>{Et.current.onDrawDisplayed({label:Cn,elapsedMs:fe,mouthPath:Ut,transitionMeta:kt.current},pe)}}).usedBuffer),Xt||ei(C,pe,fe,Me,lt,R.current,It,Qt,De.current),Et.current.onRafTickSpeaking(),Ir.current?.(Et.current.snapshot(pe,fe))}else if(!Xe&&X.current){q.current?.setActive(!1),z.current.advance(performance.now());const pe=z.current.getExpressionPath(),fe=z.current.getDrawKey();On(C,pe,fe,R.current,(K,Te,Me,lt)=>A.drawLiveFrame(K,Te,Me,lt),K=>{if(v.current){ke.current?.(v.current);return}const Te=z.current.getTargetEmotion();ke.current?.(K==="sil"||K==="blink"?`${Te}${K==="blink"?" · blink":""}`:K)},null,ie.current)}else if(!Xe&&ge.current&&q.current){const pe=q.current,fe=pe.getVideo();if(fe.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA){pe.setActive(!0);const K=()=>!j.isSpeaking()&&!_.current&&!S.current&&!X.current;A.usesPixi?(A.drawIdleVideo(fe,K),K()&&ke.current?.("idle")):w.current||(w.current=!0,Ln(C,fe,null,void 0,Ae.current,K).then(()=>ke.current?.("idle")).finally(()=>{w.current=!1}))}else On(C,null,"__sil__",R.current,(K,Te,Me,lt)=>A.drawLiveFrame(K,Te,Me,lt),K=>ke.current?.(K))}else Xe||On(C,null,"__sil__",R.current,(pe,fe,K,Te)=>A.drawLiveFrame(pe,fe,K,Te),pe=>ke.current?.(pe));je.enabled&&Tt(j),x.current=_e,U.current=requestAnimationFrame(E)};return U.current=requestAnimationFrame(E),()=>{cancelAnimationFrame(U.current),Fe(),Be(),he.current.stop(),we.current?.stop()}},[ct,p,dt,Fe,Be,Ot,Tt,He,je.enabled]),m.useImperativeHandle(ae,()=>({speakText:async(w,E)=>{const C=w.trim();if(!C)return;const A=ce.current;if(!A||!We.current&&!ct)throw new Error("AI twin is not ready");const{ttsEngineId:j,voiceId:_e}=In(E);_t(null),He(),L.current+=1,Fe(),Be(),_.current=!1,y.current=!1,ue.current="sil",me.current=0,Le(R.current),Ve(j),X.current=!1,ie.current=null,v.current=null,z.current.reset(),x.current=!1,An(),G.current=!1,A.finishIdleToSilCrossfade();try{await he.current.speak(C,{voiceId:_e,speakingRate:E?.speakingRate??l,ttsEngineId:j,language:E?.language??d})}catch(Se){throw Ke(),G.current=!1,A.finishIdleToSilCrossfade(),dt(),nt(Se instanceof Error?Se.message:"TTS failed"),Se}},speakWithReply:async(w,E)=>{const C=w.trim();if(!C)return{displayText:"",spokenText:""};const A=ce.current;if(!A||!We.current&&!ct)throw new Error("AI twin is not ready");const{ttsEngineId:j,voiceId:_e}=In(E);_t(null),He(),L.current+=1,Fe(),Be(),_.current=!1,y.current=!1,ue.current="sil",me.current=0,Le(R.current),Ve(j),X.current=!1,ie.current=null,v.current=null,z.current.reset(),x.current=!1,An(),G.current=!1,A.finishIdleToSilCrossfade();const Se=E?.brainId?.trim()||Ie.current?.brainId?.trim()||void 0,Ce=E?.brainUrl?.trim()||Ie.current?.brainUrl?.trim()||void 0;try{return await he.current.speakWithReply(C,{...E,voiceId:_e,speakingRate:E?.speakingRate??l,ttsEngineId:j,language:E?.language??d,brainId:Se,brainUrl:Ce,onDisplayText:Ee=>{E?.onDisplayText?.(Ee),xn.current?.(Ee)}})}catch(Ee){throw Ke(),G.current=!1,A.finishIdleToSilCrossfade(),dt(),nt(Ee instanceof Error?Ee.message:"speakWithReply failed"),Ee}},stop:()=>{L.current+=1,Fe(),Be(),_.current=!1,y.current=!1,G.current=!1,Ke(),ce.current?.finishIdleToSilCrossfade(),he.current.stop(),we.current?.stop(),Le(R.current),ye.current?.reset(),Y.current=0,x.current=!1,He(),dt()},setTtsEngineId:w=>{he.current.setTtsEngineId(w),we.current?.setTtsEngineId(w),Ie.current&&(Ie.current.ttsEngineId=w),Ve(w)},renderViseme:async(w,E)=>{const C=ce.current,A=te.current;if(!C||!A||!We.current)throw new Error("AI twin is not ready");await C.renderViseme(A,w,{from:E?.from,transitionDurationMs:E?.transitionDurationMs,gapMs:E?.gapMs,onStatus:j=>ke.current?.(j)})},setEmotion:w=>{if(!We.current)throw new Error("AI twin is not ready");const E=ce.current,C=te.current;if(!E||!C)throw new Error("AI twin is not ready");L.current+=1,Fe(),Be(),_.current=!1,y.current=!1,S.current=!1,G.current=!1,Ke(),he.current.stop(),we.current?.stop(),Le(R.current),ye.current?.reset(),Y.current=0,x.current=!1,E.finishIdleToSilCrossfade(),q.current?.setActive(!1);const A=it(w);X.current=!0,ie.current=null,v.current=null,z.current.snapToEmotion(A);const j=z.current.getExpressionPath(),_e=z.current.getDrawKey(),Se={mouthPath:null,expressionPath:j},Ce=$t(Se,_e);R.current.lastDrawnKey="",E.drawLiveFrame(C,Se,Ce,Xe=>{Xe===Ce&&(R.current.lastDrawnKey=Ce)});const Ee=gt(A);ke.current?.(j?`${A} · brow ${Ee>0?"+":""}${Ee}`:`${A} · brow 0 (sil base)`)},getEmotion:()=>X.current?z.current.getTargetEmotion():null,clearEmotionPreview:()=>{X.current=!1,ie.current=null,v.current=null,z.current.reset(),Le(R.current),ke.current?.("idle")},setViseme:w=>{if(!We.current)throw new Error("AI twin is not ready");const E=ce.current,C=te.current;if(!E||!C)throw new Error("AI twin is not ready");L.current+=1,Fe(),Be(),_.current=!1,y.current=!1,S.current=!1,G.current=!1,Ke(),he.current.stop(),we.current?.stop(),Le(R.current),ye.current?.reset(),Y.current=0,x.current=!1,E.finishIdleToSilCrossfade(),q.current?.setActive(!1);const A=gn(w);X.current=!0,v.current=A,ie.current=A==="sil"?null:Pt(A),z.current.snapToEmotion("neutral");const j=z.current.getExpressionPath(),_e=z.current.getDrawKey(),Ce={mouthPath:ie.current,expressionPath:j},Ee=$t(Ce,_e);R.current.lastDrawnKey="",E.drawLiveFrame(C,Ce,Ee,Xe=>{Xe===Ee&&(R.current.lastDrawnKey=Ee)}),ke.current?.(A)},getViseme:()=>X.current?v.current:null,isReady:()=>We.current,getStatus:()=>Ne,connect:async(w={})=>{_t(null);try{let E=w.authToken?.trim();E||(E=Oe.current??s??await sn(),Oe.current=E);const C=st(w.ttsEngineId??a,w.voiceId??u),A=C.voiceId??Ie.current?.voiceId,j=w.brainId?.trim()||w.personaId?.trim()||Ie.current?.brainId,_e=w.brainUrl?.trim()||Ie.current?.brainUrl,Se=C.ttsEngineId??Ie.current?.ttsEngineId??Re,Ce=Mr();Se&&Ce.setTtsEngineId(Se),Se&&Ve(Se),await Ce.unlockAudio(),await Ce.connect({...w,apiBase:w.apiBase?.trim()||qe,authToken:E,voiceId:A,brainId:j,brainUrl:_e,ttsEngineId:Se,speakingRate:w.speakingRate??l,language:w.language??d})}catch(E){throw Wt(E)||nt(E instanceof Error?E.message:"Voice connect failed"),E}},disconnect:async()=>{const w=we.current;w&&(L.current+=1,Fe(),Be(),_.current=!1,y.current=!1,G.current=!1,Ke(),ce.current?.finishIdleToSilCrossfade(),Le(R.current),ye.current?.reset(),Y.current=0,x.current=!1,await w.disconnect(),dt())},isConnected:()=>we.current?.isConnected()??!1,getVoiceStream:()=>we.current?.getVoiceStream()??he.current?.getVoiceStream()??null,setMuted:w=>{const E=he.current,C=we.current;E?.setMuted?.(w),C?.setMuted?.(w)},isMuted:()=>{const w=we.current,E=he.current;return w?.isMuted?.()??E?.isMuted?.()??!1},getBrainId:()=>Ie.current?.brainId?.trim()||null,getPersonaId:()=>Ie.current?.brainId?.trim()||null,getBrainUrl:()=>Ie.current?.brainUrl?.trim()||null}),[ct,l,d,s,Re,Ye,Fe,Be,dt,An,Ke,nt,In,Mr,Ve,Ne,qe]),O.jsxs("div",{className:c,style:{...qo,...f},children:[O.jsx("canvas",{ref:te,width:Pe.width,height:Pe.height,style:{...Yo,...g},"aria-label":t?`AI twin ${t}`:"AI twin face"}),!re&&!vt?O.jsx(yo,{thumbnailUrl:hs}):null,N&&vt?O.jsx("div",{style:Qo,children:vt}):null,je.enabled&&Ue?O.jsx(ho,{text:Ue,style:je.style},Ue):null]})});function Xo({muted:e}){return e?O.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none","aria-hidden":!0,children:[O.jsx("path",{d:"M11 5 6.5 9H3v6h3.5L11 19V5Z",fill:"currentColor"}),O.jsx("path",{d:"m16 9.5 5 5m0-5-5 5",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})]}):O.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none","aria-hidden":!0,children:[O.jsx("path",{d:"M11 5 6.5 9H3v6h3.5L11 19V5Z",fill:"currentColor"}),O.jsx("path",{d:"M15.5 8.5a4.5 4.5 0 0 1 0 7M18.5 6a8 8 0 0 1 0 12",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})]})}function Jo(){return O.jsx("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":!0,children:O.jsx("path",{d:"M8 5.5v13l11-6.5L8 5.5Z"})})}function Zo(){return O.jsx("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"currentColor","aria-hidden":!0,children:O.jsx("rect",{x:"6",y:"6",width:"12",height:"12",rx:"1.5"})})}function ec(){return O.jsx("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none","aria-hidden":!0,children:O.jsx("path",{d:"M5 6.5A2.5 2.5 0 0 1 7.5 4h9A2.5 2.5 0 0 1 19 6.5v7A2.5 2.5 0 0 1 16.5 16H10l-4 3.5V16H7.5A2.5 2.5 0 0 1 5 13.5v-7Z",stroke:"currentColor",strokeWidth:"2",strokeLinejoin:"round"})})}function tc({className:e}){return O.jsx("span",{className:e??"aitwin-widget__spinner","aria-hidden":!0,children:O.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:O.jsx("circle",{cx:"12",cy:"12",r:"9",stroke:"currentColor",strokeWidth:"2.5",strokeLinecap:"round",strokeDasharray:"40 20"})})})}function nc({muted:e,onToggle:t}){return O.jsx("button",{type:"button",className:"aitwin-widget__mute",onClick:t,"aria-label":e?"Unmute":"Mute","aria-pressed":e,children:O.jsx(Xo,{muted:e})})}function rc(e){const t=e.connecting||e.textSending||e.status==="loading"||e.status==="speaking",n=e.connecting?"Connecting...":e.connected?"End":"Talk",r=["aitwin-widget__action",e.textMode&&!e.connecting?"":"aitwin-widget__action--primary",e.connecting?"aitwin-widget__action--connecting":""].filter(Boolean).join(" ");return O.jsxs("div",{className:"aitwin-widget__controls",children:[O.jsxs("div",{className:"aitwin-widget__actions",children:[O.jsxs("button",{type:"button",className:r,onClick:e.onTalk,disabled:!e.ready||e.connecting||e.textSending,"aria-busy":e.connecting,children:[e.connecting?O.jsx(tc,{className:"aitwin-widget__spinner aitwin-widget__spinner--on-accent"}):e.connected?O.jsx(Zo,{}):O.jsx(Jo,{}),O.jsx("span",{children:n})]}),O.jsxs("button",{type:"button",className:e.textMode?"aitwin-widget__action aitwin-widget__action--primary":"aitwin-widget__action",onClick:e.onTextMode,disabled:!e.ready||e.connecting||e.textSending,"aria-pressed":e.textMode,children:[O.jsx(ec,{}),O.jsx("span",{children:"Text"})]})]}),e.textMode?O.jsxs("form",{className:"aitwin-widget__composer",onSubmit:i=>{i.preventDefault(),e.onSendText()},children:[O.jsx("input",{type:"text",className:"aitwin-widget__composer-input",value:e.textDraft,onChange:i=>e.onTextDraftChange(i.target.value),placeholder:e.textSending?"Getting reply...":"Type a message…",autoComplete:"off",disabled:!e.ready||t}),O.jsx("button",{type:"submit",className:"aitwin-widget__composer-send",disabled:!e.ready||t||!e.textDraft.trim()&&!e.textSending,"aria-busy":e.textSending,children:e.textSending?"…":"Send"})]}):null,e.error?O.jsx("p",{className:"aitwin-widget__controls-error",role:"alert",children:e.error}):null]})}const oi=97,ic=3500;function sc(e,t){const n=Math.max(0,e),r=1-Math.exp(-n/ic);return Math.min(oi,Math.round(r*oi))}function ac(e){const[t,n]=m.useState(null),[r,i]=m.useState(()=>Date.now());return m.useEffect(()=>{if(!e){n(null);return}const s=Date.now();n(s),i(s);let o=0;const a=()=>{i(Date.now()),o=window.requestAnimationFrame(a)};return o=window.requestAnimationFrame(a),()=>{window.cancelAnimationFrame(o)}},[e]),e?t==null?0:sc(r-t):100}const oc="M259.01,593.87v-101.49c.33-6.31,4.94-11.26,10.21-11.88,6.42-.75,13.18,4.99,13.13,12.92-.42,45.09-.83,90.17-1.25,135.26.03,1.56.56,15.43,11.95,23.2,6.18,4.21,12.57,4.6,15.42,4.58-.42-112.77-.83-225.54-1.25-338.31-.08-2.55-1.03-46.36,21.17-55.34,2.05-.83,4.42-1.41,7.17-1.56v453.42c.07,12.06,1.98,37.84,19.03,62.73,7.01,10.23,14.79,17.71,21.4,22.99V222.42c-.01-3.72.48-11.67,5.35-19.87,7-11.77,18.13-15.82,21.4-16.95,10.42-3.61,19.49-2.06,23.48-1.11v684.96c.59,4.03,1.78,9.72,4.45,16.12,1.93,4.63,8.84,20.57,26.47,31.26,13.48,8.18,26.56,9.23,32.86,9.31-1.3-256.11-2.59-512.22-3.89-768.33.76-16.43,14.57-28.94,29.73-28.34,13.53.53,25.39,11.39,27.23,25.84-1.1,264.68-2.2,529.35-3.3,794.03,4.33.92,22.38,4.25,38.94-6.53,10.26-6.68,15.24-15.72,17.09-19.17,5.55-10.36,6.42-19.98,6.53-25.01-.72-246.99-1.43-493.97-2.15-740.96,2.27-.06,34.75-.44,48.29,22.68,3.41,5.83,4.87,11.86,5.34,17.19v674.68c5.92-2.16,39.19-14.94,50.16-47.79,3.12-9.34,3.68-18.03,3.33-25.01.03-193.22.06-386.43.09-579.65,4.12,1.18,28.28,8.48,36.17,30.08,1.64,4.49,2.32,8.88,2.43,12.92.02,146.42.05,292.84.07,439.25,3.82-1.71,19.51-9.22,25.56-26.47,2.15-6.12,2.48-11.73,2.29-15.89.01-114.91.03-229.81.04-344.72,2.36-.1,7.51.02,12.74,3.13,9.1,5.4,10.87,15.35,11.12,16.88.35,11.49.69,22.97,1.04,34.46.46,37.05.93,74.1,1.39,111.15v152.69c3.39-.72,9.77-2.67,14.31-8.34,3.62-4.52,4.51-9.46,5.56-15.28.91-5.04.95-9.31.83-12.23.02-38.81.03-77.62.05-116.43";function cc({className:e="aitwin-widget__logo",strokeWidth:t=15,durationMs:n=2600,delayMs:r=0,loop:i=!1}){return O.jsx("svg",{viewBox:"0 0 1080 1080",className:e,role:"img","aria-label":"AITWIN logo",children:O.jsx("path",{className:i?"aitwin-widget__logo-draw aitwin-widget__logo-draw--loop":"aitwin-widget__logo-draw",d:oc,fill:"none",stroke:"currentColor",strokeWidth:t,strokeMiterlimit:10,pathLength:1,style:{animationDuration:`${n}ms`,animationDelay:`${r}ms`}})})}function lc({progress:e}){const t=Math.max(0,Math.min(100,Math.round(e)));return O.jsx("div",{className:"aitwin-widget__face-loading","aria-live":"polite","aria-label":`Loading ${t} percent`,children:O.jsxs("div",{className:"aitwin-widget__face-loading-content",children:[O.jsx(cc,{className:"aitwin-widget__logo",loop:!0,durationMs:2600}),O.jsxs("div",{className:"aitwin-widget__face-loading-bar-wrap",children:[O.jsx("div",{role:"progressbar","aria-valuenow":t,"aria-valuemin":0,"aria-valuemax":100,className:"aitwin-widget__face-loading-track",children:O.jsx("div",{className:"aitwin-widget__face-loading-fill",style:{width:`${t}%`}})}),O.jsxs("p",{className:"aitwin-widget__face-loading-pct",children:[t,"%"]})]})]})})}function ci(e){const t=e.id?.trim()||void 0,n=e.faceId?.trim()||e.avatarId?.trim()||void 0;return{id:n?void 0:t,faceId:n,authToken:e.authToken?.trim()||void 0,apiBase:e.apiBase?.trim()||void 0,voiceId:e.voiceId?.trim()||void 0,speakingRate:e.speakingRate,language:e.language}}function uc(e){return[e.id??"",e.faceId??"",e.authToken?.slice(0,16)??"",e.voiceId??"",e.apiBase??"",String(e.speakingRate??"")].join("|")}const li={display:"flex",alignItems:"center",justifyContent:"center",width:"100%",height:"100%",lineHeight:0},dc={display:"block",height:"100%",width:"100%",objectFit:"cover",objectPosition:"top"},fc=m.forwardRef(function({options:t},n){const r=m.useRef(null),i=m.useRef(t);i.current={...i.current,onReady:t.onReady,onStatusChange:t.onStatusChange,onError:t.onError,onUserTranscript:t.onUserTranscript,onBotOutput:t.onBotOutput};const[s,o]=m.useState(!!t.showControls),[a,u]=m.useState(()=>ci(t)),[d,l]=m.useState(!1),[c,f]=m.useState(!1),[g,T]=m.useState(!1),[h,I]=m.useState(!1),[F,N]=m.useState("idle"),[b,k]=m.useState(null),[V,D]=m.useState(!1),[se,le]=m.useState(!1),[P,H]=m.useState(""),[Q,oe]=m.useState(!1),[ae,te]=m.useState([]),[ce,ye]=m.useState(t.brainId?.trim()||t.personaId?.trim()||void 0),[ne,W]=m.useState(t.brainUrl?.trim()||void 0),M=m.useCallback(x=>{i.current={...i.current,...x},x.showControls!==void 0&&o(!!x.showControls),(x.brainId!==void 0||x.personaId!==void 0)&&ye((x.brainId??x.personaId)?.trim()||void 0),x.brainUrl!==void 0&&W(x.brainUrl.trim()||void 0),u(q=>ci({id:x.id??q.id,faceId:x.faceId??x.avatarId??q.faceId,authToken:x.authToken??q.authToken,apiBase:x.apiBase??q.apiBase,voiceId:x.voiceId??q.voiceId,speakingRate:x.speakingRate??q.speakingRate,language:x.language??q.language}))},[]);m.useImperativeHandle(n,()=>({getTwin:()=>r.current,applyOptions:M}),[M]),m.useEffect(()=>{M(t)},[M,t.id,t.faceId,t.avatarId,t.authToken,t.apiBase,t.voiceId,t.speakingRate,t.showControls,t.brainId,t.personaId,t.brainUrl]);const Y=m.useCallback(()=>{l(!0),k(null)},[]),R=m.useCallback(x=>{x==="idle"&&f(!0)},[]),z=m.useCallback(x=>{N(x),i.current.onStatusChange?.(x)},[]),X=m.useRef(!1);X.current=d&&c;const ie=m.useCallback(x=>{if(I(!1),Wt(x)){le(!0),k(null);return}k(x),X.current||(l(!1),f(!1)),i.current.onError?.(x)},[]),v=m.useCallback((x,q)=>{i.current.onUserTranscript?.(x,q)},[]),U=m.useCallback(x=>{i.current.onBotOutput?.(x)},[]);m.useEffect(()=>{if(!s)return;const x=window.setInterval(()=>{T(!!r.current?.isConnected())},400);return()=>window.clearInterval(x)},[s,a]);const L=uc(a),Z=!!(a.id||a.faceId),$=d&&c,J=Z&&!$&&!b,_=ac(J),y=m.useRef(L);m.useEffect(()=>{$&&i.current.onReady?.()},[$]),m.useEffect(()=>{$&&r.current?.setMuted(V)},[$,V,a]),m.useEffect(()=>{te([])},[a.id,a.faceId]),m.useEffect(()=>{y.current!==L&&(y.current=L,l(!1),f(!1),k(null),I(!1),T(!1),N("idle"))},[L]);const S=m.useCallback(()=>{le(!0),k(null),I(!1),T(!1)},[]),G=()=>{const x=r.current;if(!x?.isReady())return;if(k(null),x.isConnected()){I(!1),le(!1),x.disconnect().finally(()=>{T(!1)});return}I(!0);const q={authToken:a.authToken,voiceId:a.voiceId,speakingRate:a.speakingRate,language:a.language,apiBase:a.apiBase,...ce?{brainId:ce}:{},...ne?{brainUrl:ne}:{}};(async()=>{if(await ha()){S();return}le(!1);try{await x.connect(q),T(!!x.isConnected())}catch(ge){if(Wt(ge)){S();return}const Ne=ge instanceof Error?ge.message:"Failed to connect voice.";k(Ne),i.current.onError?.(Ne)}finally{I(!1)}})()},ee=()=>{le(x=>!x),k(null)},ue=()=>{const x=P.trim(),q=r.current;!x||!q?.isReady()||Q||(k(null),H(""),oe(!0),i.current.onUserTranscript?.(x,!0),(async()=>{try{const ge=ce||q.getBrainId()?.trim()||void 0,Ne=ne||q.getBrainUrl()?.trim()||void 0,Ft=await q.speakWithReply(x,{brainId:ge,brainUrl:Ne,history:ae,voiceId:a.voiceId,speakingRate:a.speakingRate,language:a.language});Ft.displayText&&te(vt=>[...vt,{role:"user",content:x},{role:"assistant",content:Ft.displayText}])}catch(ge){const Ne=ge instanceof Error?ge.message:"Failed to get reply.";k(Ne),i.current.onError?.(Ne)}finally{oe(!1)}})())},me=()=>{const x=!V;D(x),r.current?.setMuted(x)};return O.jsxs("div",{className:"aitwin-widget__layout",children:[O.jsxs("div",{className:"aitwin-widget__stage",children:[Z?O.jsx("div",{style:li,children:O.jsx(us,{ref:r,id:a.id,faceId:a.faceId,authToken:a.authToken,apiBase:a.apiBase,voiceId:a.voiceId,speakingRate:a.speakingRate,language:a.language,style:li,canvasStyle:dc,onReady:Y,onDisplayStatus:R,onStatusChange:z,onError:ie,onUserTranscript:v,onBotOutput:U,loadingThumbnailUrl:t.loadingThumbnailUrl},L)}):O.jsx("p",{className:"aitwin-widget__hint",style:{padding:16},children:"Pass id or faceId to load a twin."}),J?O.jsx(lc,{progress:_}):null,s?O.jsx(nc,{muted:V,onToggle:me}):null]}),s?O.jsx(rc,{ready:$,connected:g,connecting:h,status:F,textMode:se,textDraft:P,textSending:Q,error:b,onTalk:G,onTextMode:ee,onTextDraftChange:H,onSendText:ue}):null]})}),mc=".aitwin-widget{--aitwin-border: rgba(255, 255, 255, .12);--aitwin-text: #f4f4f5;--aitwin-muted: #a1a1aa;--aitwin-accent: #f97316;--aitwin-accent-hover: #fb923c;--aitwin-font: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;--aitwin-size: 1;--aitwin-stage-width: calc(200px * var(--aitwin-size));--aitwin-stage-height: calc(267px * var(--aitwin-size));--aitwin-action-height: calc(40px * var(--aitwin-size));--aitwin-action-font: calc(13px * var(--aitwin-size));--aitwin-action-radius: calc(10px * var(--aitwin-size));--aitwin-action-gap: calc(6px * var(--aitwin-size));--aitwin-mute-size: calc(28px * var(--aitwin-size));--aitwin-layout-gap: calc(8px * var(--aitwin-size));box-sizing:border-box;color:var(--aitwin-text);font-family:var(--aitwin-font);font-size:calc(12px * var(--aitwin-size));line-height:1.45;width:fit-content;max-width:100%}.aitwin-widget *,.aitwin-widget *:before,.aitwin-widget *:after{box-sizing:border-box}.aitwin-widget__layout{display:flex;flex-direction:column;gap:var(--aitwin-layout-gap);width:fit-content;max-width:100%}.aitwin-widget__stage{align-items:center;background:#0a0a0b;border:1px solid var(--aitwin-border);border-radius:calc(10px * var(--aitwin-size));display:flex;flex-shrink:0;height:var(--aitwin-stage-height);justify-content:center;line-height:0;overflow:hidden;position:relative;width:var(--aitwin-stage-width)}.aitwin-widget__stage .aitwin-root,.aitwin-widget__stage canvas{display:block;height:100%;object-fit:cover;object-position:top;width:100%}.aitwin-widget__hint{color:var(--aitwin-muted);font-size:calc(11px * var(--aitwin-size));margin:0}.aitwin-widget__mute{align-items:center;background:#28282cb8;border:none;border-radius:999px;color:#fff;cursor:pointer;display:inline-flex;height:var(--aitwin-mute-size);justify-content:center;padding:0;position:absolute;right:calc(8px * var(--aitwin-size));top:calc(8px * var(--aitwin-size));width:var(--aitwin-mute-size);z-index:2}.aitwin-widget__mute:hover{background:#37373cd9}.aitwin-widget__mute svg{height:calc(15px * var(--aitwin-size));width:calc(15px * var(--aitwin-size))}.aitwin-widget__controls{display:flex;flex-direction:column;gap:var(--aitwin-action-gap);width:var(--aitwin-stage-width)}.aitwin-widget__actions{display:grid;gap:var(--aitwin-action-gap);grid-template-columns:1fr 1fr;width:100%}.aitwin-widget__action{align-items:center;background:transparent;border:1.5px solid var(--aitwin-accent);border-radius:999px;color:var(--aitwin-accent);cursor:pointer;display:inline-flex;font-family:inherit;font-size:var(--aitwin-action-font);font-weight:600;gap:calc(6px * var(--aitwin-size));height:var(--aitwin-action-height);justify-content:center;padding:0 calc(10px * var(--aitwin-size))}.aitwin-widget__action svg{flex-shrink:0;height:calc(12px * var(--aitwin-size));width:calc(12px * var(--aitwin-size))}.aitwin-widget__action:hover:not(:disabled){background:#f973161a}.aitwin-widget__action:disabled{cursor:not-allowed;opacity:.45}.aitwin-widget__action--primary{background:var(--aitwin-accent);border-color:transparent;color:#fff}.aitwin-widget__action--primary:hover:not(:disabled){background:var(--aitwin-accent-hover)}.aitwin-widget__action--connecting:disabled{cursor:progress;opacity:1}.aitwin-widget__spinner{display:inline-flex;flex-shrink:0;height:calc(14px * var(--aitwin-size));width:calc(14px * var(--aitwin-size))}.aitwin-widget__spinner svg{animation:aitwin-widget-spin .8s linear infinite;display:block;height:100%;width:100%}.aitwin-widget__spinner--on-accent{color:#fff}@keyframes aitwin-widget-spin{to{transform:rotate(360deg)}}.aitwin-widget__face-loading{background:transparent;inset:0;overflow:hidden;pointer-events:none;position:absolute;z-index:4}.aitwin-widget__face-loading-content{align-items:center;display:flex;flex-direction:column;gap:calc(12px * var(--aitwin-size));inset:0;justify-content:center;padding:calc(24px * var(--aitwin-size));position:absolute;z-index:1}.aitwin-widget__logo{color:#fff;display:block;flex-shrink:0;height:calc(64px * var(--aitwin-size));margin-bottom:calc(4px * var(--aitwin-size));width:calc(64px * var(--aitwin-size))}.aitwin-widget__logo-draw{stroke-dasharray:1;stroke-dashoffset:1;animation:aitwin-widget-logo-draw 2.6s ease-in-out forwards}.aitwin-widget__logo-draw--loop{animation:aitwin-widget-logo-draw-loop 2.6s ease-in-out infinite}@keyframes aitwin-widget-logo-draw{to{stroke-dashoffset:0}}@keyframes aitwin-widget-logo-draw-loop{0%{stroke-dashoffset:1}45%{stroke-dashoffset:0}55%{stroke-dashoffset:0}to{stroke-dashoffset:-1}}@media(prefers-reduced-motion:reduce){.aitwin-widget__logo-draw,.aitwin-widget__logo-draw--loop{animation:none;stroke-dashoffset:0}}.aitwin-widget__face-loading-bar-wrap{max-width:calc(220px * var(--aitwin-size));width:100%}.aitwin-widget__face-loading-track{background:#fff3;border-radius:999px;height:calc(4px * var(--aitwin-size));overflow:hidden;width:100%}.aitwin-widget__face-loading-fill{background:var(--aitwin-accent);border-radius:999px;height:100%;transition:width .3s ease-out}.aitwin-widget__face-loading-pct{color:#ffffffbf;font-size:calc(12px * var(--aitwin-size));margin:calc(8px * var(--aitwin-size)) 0 0;text-align:center}.aitwin-widget__composer{display:grid;gap:var(--aitwin-action-gap);grid-template-columns:1fr auto;width:100%}.aitwin-widget__composer-input{background:#ffffff0a;border:1px solid var(--aitwin-border);border-radius:var(--aitwin-action-radius);color:var(--aitwin-text);font-family:inherit;font-size:calc(12px * var(--aitwin-size));min-width:0;outline:none;padding:calc(8px * var(--aitwin-size)) calc(10px * var(--aitwin-size))}.aitwin-widget__composer-input:focus{border-color:#f973168c}.aitwin-widget__composer-send{background:var(--aitwin-accent);border:none;border-radius:var(--aitwin-action-radius);color:#fff;cursor:pointer;font-family:inherit;font-size:calc(12px * var(--aitwin-size));font-weight:600;padding:0 calc(12px * var(--aitwin-size))}.aitwin-widget__composer-send:disabled{cursor:not-allowed;opacity:.45}.aitwin-widget__controls-error{color:#fecaca;font-size:calc(11px * var(--aitwin-size));margin:0}.aitwin-widget--float{bottom:20px;filter:drop-shadow(0 12px 32px rgba(0,0,0,.45));height:fit-content;margin:0;max-width:calc(100vw - 40px);position:fixed;right:20px;width:fit-content;z-index:2147483000}",ui="data-aitwin-widget-style";function ds(){if(typeof document>"u")return;const e=mc.trim();if(!e){console.warn("[AiTwinWidget] styles.css?inline resolved empty — chrome will be unstyled");return}const t=document.querySelector(`style[${ui}]`);if(t){if((t.textContent??"").trim().length>0)return;t.textContent=e;return}const n=document.createElement("style");n.setAttribute(ui,"1"),n.textContent=e,(document.head??document.documentElement).appendChild(n)}typeof document<"u"&&ds();const hc=m.forwardRef(function(t,n){ds();const r=m.useRef(null);m.useImperativeHandle(n,()=>({speakText:async(s,o)=>{const a=r.current?.getTwin();if(!a)throw new Error("AiTwinWidget: twin is not mounted");return a.speakText(s,o)},speakWithReply:async(s,o)=>{const a=r.current?.getTwin();if(!a)throw new Error("AiTwinWidget: twin is not mounted");return a.speakWithReply(s,o)},connect:async s=>{const o=r.current?.getTwin();if(!o)throw new Error("AiTwinWidget: twin is not mounted");return o.connect(s)},disconnect:async()=>{const s=r.current?.getTwin();if(s)return s.disconnect()},stop:()=>{r.current?.getTwin()?.stop()},isReady:()=>!!r.current?.getTwin()?.isReady(),isConnected:()=>!!r.current?.getTwin()?.isConnected(),getTwin:()=>r.current?.getTwin()??null}),[]);const i=!!t.float;return O.jsx("div",{className:i?"aitwin-widget aitwin-widget--float":"aitwin-widget",children:O.jsx(fc,{ref:r,options:t})})}),pc=new Set([".png",".jpg",".jpeg",".webp"]);function fs(e=".png"){const t=e.toLowerCase();return pc.has(t)?`source${t}`:"source.png"}function gc(e,t=Zn,n=".png"){const r=e.trim();if(!r)throw new Error("faceId is required");return`${t.replace(/\/$/,"")}/${r}/${fs(n)}`}async function wc(e){try{const t=await e.json();if(typeof t.detail=="string"&&t.detail.trim())return t.detail;if(typeof t.error=="string"&&t.error.trim())return t.error}catch{}return`avatarReply failed (${e.status})`}async function yc(e){const t=e.message.trim();if(!t)throw new Error("Message is required");const n=e.authToken.trim();if(!n)throw new Error("Auth token is required");const r=(e.apiBase?.trim()||Dt).replace(/\/$/,""),i=await fetch(`${r}/avatarReply`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`,Accept:"text/event-stream"},body:JSON.stringify({message:t,history:e.history??[],brainId:e.brainId?.trim()||e.personaId?.trim()||"",tenant:"aiTwin"})});if(!i.ok)throw new Error(await wc(i));if(!i.body)throw new Error("avatarReply response body is empty");const s=i.body.getReader(),o=new TextDecoder;let a="",u="",d="",l="";for(;;){const{done:c,value:f}=await s.read();if(c)break;a+=o.decode(f,{stream:!0});const g=a.split(`
48
+ `);a=g.pop()||"";for(const T of g){if(T.startsWith("event: ")){l=T.slice(7).trim();continue}if(!T.startsWith("data: "))continue;const h=T.slice(6).trim();if(!h)continue;let I;try{I=JSON.parse(h)}catch{continue}if(l==="spokenText")u=I.text||"",e.onSpokenText?.(u);else if(l==="displayText")d=I.text||"";else if(l==="error")throw new Error(I.error||"avatarReply stream error")}}if(!u.trim()||!d.trim())throw new Error("avatarReply stream missing displayText or spokenText");return{displayText:d.trim(),spokenText:u.trim()}}exports.AI_TWIN_THUMBNAIL_FILENAME=sr;exports.AiTwin=us;exports.AiTwinNotFoundError=an;exports.AiTwinWidget=hc;exports.CONSONANT_SKIP_GROUP=Ri;exports.DEFAULT_TTS_ENGINE_ID=ht;exports.DEFAULT_TTS_LANGUAGE=js;exports.EMOTION_TO_EYEBROW=ts;exports.EXPRESSION_ATLAS_EMOTIONS=Ua;exports.MIC_SAMPLE_RATE=fr;exports.OCULUS_VISEME_IDS=Ii;exports.PLAYBACK_SAMPLE_RATE=mr;exports.SPEAKING_RATE_MAX=bn;exports.SPEAKING_RATE_MIN=_n;exports.TTS_ENGINE_CARTESIA=at;exports.TTS_ENGINE_GOOGLE=tr;exports.TTS_ENGINE_INWORLD=mn;exports.TTS_ENGINE_VISEMETTS=ot;exports.TTS_LANGUAGE_CATALOG=pt;exports.TTS_LANGUAGE_CODES=ki;exports.VISEME_IDS=Ia;exports.VISEME_SKIP_GAP_MS=Kn;exports.VISEME_TEST_CARTESIA_VOICE_ID=dr;exports.VISEME_TEST_SPEAKING_RATE=Kt;exports.VISEME_TEST_VISEMETTS_VOICE_ID=hn;exports.VISEME_TEST_VOICE_ID=ur;exports.VISEME_VTIME_GRID_MS=ar;exports.VOWEL_SKIP_GROUP=Pi;exports.aiTwinSourceFilename=fs;exports.buildPlaybackTransitions=Oi;exports.buildPlaybackVisemeSeries=yn;exports.buildRawVisemeTransitions=Ni;exports.buildVoiceWebSocketUrl=Ki;exports.compactSkippableVisemes=cr;exports.createAvatarTtsLipsyncController=Gn;exports.createRealtimeVoiceLipsyncController=Gi;exports.emotionToEyebrow=gt;exports.faceAssetUrls=jn;exports.fetchAiTwin=gi;exports.fetchAvatarReply=yc;exports.fetchDevAuthToken=sn;exports.getAiTwinSourceUrl=gc;exports.getAiTwinThumbnailUrl=wi;exports.isTtsLanguageCode=Ei;exports.isTtsLanguageSupported=Gs;exports.listSkippedVisemeEntries=vn;exports.listSkippedVisemes=ia;exports.normalizeOculusViseme=gn;exports.normalizeStreamEmotion=it;exports.normalizeTtsLanguage=pn;exports.parsePackedVoiceId=fi;exports.parseTwinBrainUrl=Ms;exports.resolveDirectFaceId=yi;exports.resolveTwinBrainId=rr;exports.resolveTwinBrainUrl=ir;exports.resolveVisemeAtTime=jt;exports.resolveVisemeFromPlaybackSeries=Bi;exports.resolveWordAtTime=ma;exports.setActiveTwinAssets=vi;exports.snapVisemeQueueToGrid=or;exports.snapVisemeVtimeToGrid=Ci;exports.splitVoiceIdSpec=st;exports.ttsLanguagesForEngine=Ks;