@streamoji/aitwin 0.4.7 → 0.4.8
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 Xt=require("react/jsx-runtime"),v=require("react"),Xn=require("@pipecat-ai/client-js"),dr=require("@pipecat-ai/websocket-transport"),se=require("pixi.js");var Jt=typeof document<"u"?document.currentScript:null;const Jn="https://aitwin.bubu.social",cn={publicBase:Jn},Qn="0.4.7",Zn={version:Qn},es={},vt="https://ai.aitwin.me",mr=`${cn.publicBase}/custom-faces`,ts=`${cn.publicBase}/aitwin-workers/v${Zn.version}/visemeDiffPreview.worker.js`;function rs(e){if(e)return e;const t=typeof{url:typeof document>"u"?require("url").pathToFileURL(__filename).href:Jt&&Jt.tagName.toUpperCase()==="SCRIPT"&&Jt.src||new URL("aitwin.cjs",document.baseURI).href}<"u"?es?.VITE_AITWIN_WORKER_URL:void 0;return t?.trim()?t.trim():ts}const ns="https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin",Qt=3e3,_t=16,Nr=3e3,ss=4e3,is=.85,as=8,hr=400,pr="eng_a7f2b9c1",Ht="eng_d4e8f3a2",Ye="eng_c9b1e6d4",ze=Ye,ln=new Set([Ht,Ye]),fn={[pr]:"oculus_direct",[Ye]:"oculus_direct",[Ht]:"inworld_word"},Ur={google:pr,inworld:Ht,cartesia:Ye};function os(e){return fn[e]}function us(e){return e===Ye}function cs(e){if(e.ttsEngineId&&e.ttsEngineId in fn)return e.ttsEngineId;const t=e.tts?.toLowerCase();return t&&t in Ur?Ur[t]:ze}class Lt extends Error{constructor(t="AI twin not found"){super(t),this.name="AiTwinNotFoundError"}}async function dn(e,t=ns){const r=new URL(t);r.searchParams.set("id",e);const n=await fetch(r.toString());if(n.status===404)throw new Lt;if(!n.ok)throw new Error(`getAiTwin failed (${n.status})`);const s=await n.json();if(!s.success)throw new Lt(s.error??"AI twin not found");const{tts:i,token:o,...a}=s.data,l=String(a.authToken??o??"").trim()||void 0;return{...a,ttsEngineId:cs(s.data),authToken:l}}const ls="https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken",fs="client_6TNvp3SCs4Og0a1ijm9TommXLql1",ds="kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1",ms="6TNvp3SCs4Og0a1ijm9TommXLql1",hs="Swaraj Mali";async function Ft(){const e=await fetch(ls,{method:"POST",headers:{"Content-Type":"application/json","Client-Id":fs,"Client-Secret":ds},body:JSON.stringify({userId:ms,userName:hs})});if(!e.ok)throw new Error(`getAuthToken failed (${e.status})`);const t=await e.json(),r=t.authToken??t.token??"";if(!r.trim())throw new Error("getAuthToken returned no token");return r}async function ps(e,t=vt){const r=await fetch(`${t.replace(/\/$/,"")}/api/session-value`,{headers:{Authorization:`Bearer ${e}`}});if(!r.ok)throw new Error(`/api/session-value failed (${r.status})`);const n=await r.json();if(!n.value)throw new Error("Session value response missing value");return n}function ws(e){const t=e.trim();if(t.length%2!==0)throw new Error("Invalid hex key");const r=new Uint8Array(t.length/2);for(let n=0;n<t.length;n+=2)r[n/2]=Number.parseInt(t.slice(n,n+2),16);return r}function Mt(e){return Uint8Array.from(e)}function gs(e,t){const r=new Uint8Array(e.length+t.length);return r.set(e,0),r.set(t,e.length),r}async function ys(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 r=new Uint8Array(e),n=Mt(r.subarray(0,12)),s=Mt(r.subarray(12,28)),i=Mt(r.subarray(28)),o=Mt(gs(i,s)),a=await crypto.subtle.importKey("raw",ws(t),{name:"AES-GCM"},!1,["decrypt"]);return crypto.subtle.decrypt({name:"AES-GCM",iv:n,tagLength:128},a,o)}let nr=null;const sr=new Set;function Rt(e){return e.includes("/custom-faces/")}function Ot(e){const t=e.binBase.trim().replace(/\/$/,""),r=e.twinBase.trim().replace(/\/$/,"");if(!(e.encrypted||Rt(t)||Rt(r)))return{twinBase:r,binBase:t,encrypted:!1};const s=Rt(t)?t:Rt(r)?r:t;return{twinBase:s,binBase:s,encrypted:!0}}function Xe(e){return Ot(e).encrypted}function ir(e,t=mr){const r=`${t.replace(/\/$/,"")}/${e}`;return{twinBase:r,binBase:r,encrypted:!0}}function mn(e,t){return(e??t)?.trim()||void 0}function hn(e){nr=Ot(e);for(const t of sr)t()}function _s(e,t){const r=typeof globalThis.location<"u"?globalThis.location.href:void 0;if(!r)return Ot(e);const n=s=>{try{return new URL(s,r).href.replace(/\/$/,"")}catch{return s}};return Ot({twinBase:n(e.twinBase),binBase:n(e.binBase),encrypted:e.encrypted})}function Je(){if(!nr)throw new Error("Twin assets not configured; call setActiveTwinAssets first");return nr}function pn(e){return sr.add(e),()=>sr.delete(e)}function Nt(){const e=Je();return Xe(e)?`${e.binBase}/sil.bin`:`${e.twinBase}/sil.png`}function Ss(){const e=Je();return Xe(e)?`${e.binBase}/idle.bin`:`${e.twinBase}/idle.mp4`}function wn(){const e=Je();return Xe(e)?e.binBase:e.twinBase}function Tt(){return Xe(Je())}function gn(){const e=Je();return Xe(e)?`${e.binBase}/atlas.bin`:`${e.twinBase}/atlas.json`}function yn(){const e=Je();return Xe(e)?`${e.binBase}/expression_atlas.bin`:`${e.twinBase}/expression_atlas.json`}function _n(e){const t=e.split(/\r?\n/);let r="",n="";for(const i of t)i.startsWith("event:")?r=i.slice(6).trim():i.startsWith("data:")&&(n=i.slice(5).trim());if(!r)return null;let s={};if(n)try{s=JSON.parse(n)}catch{s={raw:n}}return{event:r,data:s}}const Sn=["aa","CH","DD","E","FF","I","O","PP","RR","SS","TH","U","kk","nn","sil"],vs=new Map(Sn.map(e=>[e.toLowerCase(),e]));function vn(e){const t=(e??"").trim();return t?vs.get(t.toLowerCase())??"sil":"sil"}const Ts={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 bs(e){if(!e)return["sil"];const t=e.toLowerCase();return Ts[t]??["sil"]}function ks(e,t){const r=e.word.length;if(r<=0)return 0;const n=e.wduration;if(n<=0)return 0;const s=Math.max(0,Math.min(1,(t-e.wtime)/n));return Math.min(r-1,Math.floor(s*r))}function at(e,t){const r=Math.max(0,Math.min(e.length-1,t));return e[r]??""}function wr(e,t){const r=e.toLowerCase().replace(/[^a-z']/g,"");return!r||at(r,t)!=="w"||t>0&&at(r,t-1)==="q"?!1:t===0&&/^w[aeiouy]/.test(r)}function Es(e,t){const r=e.toLowerCase(),n=at(r,t),s=r.slice(Math.max(0,t-1),t+2);return n==="i"||n==="y"?"I":s.includes("ee")||n==="e"||r.includes("ea")?"E":"aa"}function Is(e,t){const r=e.toLowerCase(),n=r.slice(Math.max(0,t-1),Math.min(r.length,t+3));if(/oo|ou|uw/.test(n))return"U";if(wr(r,t))return"FF";const s=at(r,t);return s==="u"||s==="w"?"U":"O"}function As(e,t){const r=e.toLowerCase();if(wr(r,t))return"FF";const n=r.slice(Math.max(0,t-2),t+1);if(/qu/.test(n))return"U";const s=r.slice(Math.max(0,t-1),Math.min(r.length,t+3));return/wo|wh/.test(s)||at(r,t)==="o"?"O":"U"}const Ct={d:"DD",t:"DD",n:"DD",s:"SS",z:"SS",k:"kk",g:"kk",c:"CH",x:"CH",j:"CH"};function Ps(e,t){const r=e.toLowerCase(),n=at(r,t);if(Ct[n])return Ct[n];for(let s=0;s<r.length;s++){const i=r[s];if(Ct[i])return Ct[i]}return"DD"}function xs(e,t,r){const n=bs(e);if(n.length===1)return n[0];const s=t.trim();if(!s)return n[0];const i=(e??"").toLowerCase(),o=Math.max(0,r);switch(i){case"aei":return Es(s,o);case"o":return Is(s,o);case"qw":return As(s,o);case"cdgknstxyz":return Ps(s,o);default:return n[0]}}const Ms=32,Vr=48;function Tn(e){let t=0;for(const r of e)t=Math.max(t,r.vtime+r.vduration);return t}function bn(e){let t=null,r=-1;for(const n of e){if(n.viseme==="sil")continue;const s=n.vtime+n.vduration;s>r&&(t=n,r=s)}return t}function Rs(e){return kn(e).find(r=>r.viseme!=="sil")??null}function Ke(e){const t=Tn(e);return bn(e)?t+hr:t}function Cs(e){const t=[],r=Rs(e);r&&r.vtime>0&&t.push({from:"sil",to:r.viseme,fromVtime:0,toVtime:r.vtime});const n=bn(e),s=Tn(e);return n&&t.push({from:n.viseme,to:"sil",fromVtime:s,toVtime:s+hr}),t}function kn(e){const t=new Map;for(const r of e)t.has(r.vtime)||t.set(r.vtime,r);return[...t.values()].sort((r,n)=>r.vtime-n.vtime)}function Ds(e){const t=kn(e),r=[];for(let n=0;n<t.length-1;n++){const s=t[n].viseme,i=t[n+1].viseme,o=t[n].vtime,a=t[n+1].vtime;s===i||a<=o||r.push({from:s,to:i,fromVtime:o,toVtime:a})}return r}function Bs(e){return e.length===0?[]:[...Cs(e),...Ds(e)]}function Fs(e,t){let r=null;for(const n of e){const{transStart:s,transEnd:i}=En(n.fromVtime,n.toVtime,n.from,n.to);t>=s&&t<i&&(r=n)}return r}function En(e,t,r,n){const s=Math.max(0,t-e);if((r==="PP"||n==="PP")&&s>Vr){const f=Vr,c=t,u=c-f;return{gapMs:s,effectiveGapMs:f,holdEnd:u,transStart:u,transEnd:c}}const o=s>0?Math.max(s,Ms):0,a=e,l=e+o;return{gapMs:s,effectiveGapMs:o,holdEnd:e,transStart:a,transEnd:l}}const gr=16e3;function In(e){const t=Number(e);return Number.isFinite(t)&&t>0?t:gr}function Ls(e){let t=e.trim();const r=t.indexOf(",");t.startsWith("data:")&&r>=0&&(t=t.slice(r+1)),t=t.replace(/\s+/g,"").replace(/-/g,"+").replace(/_/g,"/");const n=t.length%4;return n!==0&&(t+="=".repeat(4-n)),t}function Os(e){return e.length>=4&&e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70}function Wr(e,t,r=gr){const n=t.byteOffset%2===0?t:t.slice(),s=new Int16Array(n.buffer,n.byteOffset,n.byteLength/2),i=new Float32Array(s.length);for(let a=0;a<s.length;a++)i[a]=s[a]>=32768?-(65536-s[a])/32768:s[a]/32767;const o=e.createBuffer(1,i.length,r);return o.copyToChannel(i,0),o}function Ns(e){const t=Ls(e),r=window.atob(t),n=new Uint8Array(r.length);for(let s=0;s<r.length;s++)n[s]=r.charCodeAt(s);return n}async function Hr(e,t,r=gr){const n=Ns(t),s=In(r);if(Os(n)){const i=n.buffer.slice(n.byteOffset,n.byteOffset+n.byteLength);return e.decodeAudioData(i)}if(n.length>=2&&n.length%2===0)return Wr(e,n,s);try{const i=n.buffer.slice(n.byteOffset,n.byteOffset+n.byteLength);return await e.decodeAudioData(i)}catch{return Wr(e,n,s)}}function Us(e,t){for(const r of e)if(t>=r.wtime&&t<r.wtime+r.wduration)return r;return null}function An(e,t){const r=[],n=[];let s=null,i=!1,o=!1,a=!1,l=!1,f=0,c=0,u=0,d=null,w=1,T=t,h=!1,I=null,b=null,x=!1,p=0,A=!1,C=!1,L=0,K=null,P=null,k=null;const D=()=>({visemes:[],words:[],segmentOffsetMs:0}),O=()=>{C=!1,L=0},M=()=>{d!=null&&(clearTimeout(d),d=null)},re=()=>{for(const g of n){try{g.onended=null,g.stop(0)}catch{}try{g.disconnect()}catch{}}n.length=0},N=()=>{for(const g of n)g.playbackRate.value=w},J=()=>{if(!(h||!l||i||C||n.length>0||r.length>0)){if(a&&s&&c>0){const g=(s.currentTime-c)*1e3;if(g<p){M();const B=w>0?(p-g)/w:p-g;d=setTimeout(()=>{h||(o=!1,d=null,e.onSpeakingDone(),e.onNotify())},Math.max(0,B));return}}o=!1,M(),e.onSpeakingDone(),e.onNotify()}},X=g=>{const B=Ke(g);if(B<=0){e.onSpeakingDone(),e.onNotify();return}o=!0,f=performance.now(),e.onSpeakingStart(),e.onNotify(),M();const R=w>0?B/w:B;d=setTimeout(()=>{h||(o=!1,d=null,e.onSpeakingDone(),e.onNotify())},R)},Q=(g,B,R,y)=>{B.length!==0&&(u=R,B.forEach(S=>{const G=String(S.word??"").trim();if(!G)return;const W=Math.round((S.start??0)*1e3),j=S.duration!=null?Math.round(S.duration*1e3):S.end!=null?Math.max(1,Math.round((S.end-(S.start??0))*1e3)):0;g.push({word:G,wtime:u+W,wduration:j,queueIndex:g.length,gesture:S.gesture??null,emphasis:S.emphasis===!0})}))},_e=(g,B,R,y,S,F)=>{R.length!==0&&(u=y,R.forEach(G=>{const W=String(G.symbol??G.viseme??"").trim();if(!W)return;const j=Math.round((G.start??0)*1e3),ve=Math.max(1,Math.round((G.duration??0)*1e3)),Ae=u+j,Te=Us(B,Ae),m=Te?ks(Te,Ae):0;let E;os(F)==="oculus_direct"?(E=vn(W),E==="U"&&Te&&wr(Te.word,m)&&(E="FF")):E=xs(W,Te?.word??"",m),g.push({viseme:E,weight:1,vtime:Ae,vduration:ve})}),e.onNotify())},Z=(g,B,R,y,S,F,G)=>{Q(B,y,S),_e(g,B,R,S,F,G)},ue=(g,B,R)=>{k===g&&g.visemes===B||(B.length=0,R.length=0,g.visemes=B,g.words=R,g.segmentOffsetMs=0,k=g,u=0,p=0)},ce=g=>(b||(b=g.createGain(),b.gain.value=x?0:1,b.connect(g.destination)),b),pe=async()=>{const g=window.AudioContext||window.webkitAudioContext;return s||(s=new g),s.state==="suspended"&&await s.resume(),ce(s),I||(I=s.createMediaStreamDestination()),s},ae=(g,B)=>{if(!s)return 0;const R=s,y=R.currentTime,S=y+.02;if(B.isFirstAudio){c<=0&&(c=S);const G=(S-y)*1e3;f=performance.now()+G}B.onBeforeStart?.(S);const F=R.createBufferSource();return F.buffer=g,F.playbackRate.value=w,F.connect(ce(R)),I&&F.connect(I),n.push(F),h?(n.pop(),S):(B.isFirstAudio&&(o=!0,e.onSpeakingStart()),F.onended=()=>{const G=n.indexOf(F);G>=0&&n.splice(G,1),e.onNotify(),!h&&(K&&P?z(K,P):J())},F.start(S),S)},z=async(g,B)=>{if(h||i||n.length>0)return;if(r.length===0){J();return}i=!0;let R=!1;try{const y=await pe();if(h)return;const S=r[0];if(!S)return;const F=!o;if(A&&(e.shouldHoldForWarmup?.()??!1)&&(C||F&&S.newStream)||C){ue(S.utterance,g,B);for(const j of r){if(h)return;j.audioBuffer||(j.audioBuffer=await Hr(y,j.audio,j.sampleRate)),a=!0,C||(C=!0,L=0,j.newStream&&j.onNewStream?.()),j.visemesQueued||(Z(g,B,j.visemes,j.words,L,!0,T),j.visemesQueued=!0,L+=Math.round(j.audioBuffer.duration*1e3),p=Math.max(p,Ke(g)))}e.onNotify();return}const W=r.shift();if(!W||(W.audioBuffer||(W.audioBuffer=await Hr(y,W.audio,W.sampleRate)),h))return;a=!0,M(),ue(W.utterance,g,B),W.visemesQueued=!1,W.newStream&&W.onNewStream?.(),ae(W.audioBuffer,{isFirstAudio:!o,isFirstOfStream:!1,onBeforeStart:j=>{c=j,u=0,f=performance.now()+(j-y.currentTime)*1e3,Z(g,B,W.visemes,W.words,0,!0,T),W.visemesQueued=!0,W.utterance.segmentOffsetMs=0}}),R=n.length>0,p=Ke(g),e.onNotify()}catch(y){console.error("[avatarChunkPlayback] audio chunk failed:",y)}finally{if(i=!1,h)return;if(C){r.some(S=>!S.audioBuffer||!S.visemesQueued)&&z(g,B);return}!R&&n.length===0&&r.length>0?z(g,B):n.length===0&&r.length===0&&J()}};return{setStreamTtsEngineId:g=>{T=g},setPlaybackSpeed:g=>{w=Math.max(.1,Math.min(1,g)),N()},getPlaybackSpeed:()=>w,setLipsyncWarmupHoldEnabled:g=>{A=g,g||O()},isWarmingUp:()=>C,releaseWarmupHold:()=>{if(C){C=!1,L=0;for(const g of r)g.newStream=!1;K&&P&&z(K,P),e.onNotify()}},resetForNewStream:g=>{h=!1,l=!1,T=g,p=0},resumeBotStream:()=>{h=!1,l=!1},clearAborted:()=>{h=!1},primeAudioContext:async()=>{const g=window.AudioContext||window.webkitAudioContext;s||(s=new g),s.state==="suspended"&&await s.resume(),s&&(ce(s),I||(I=s.createMediaStreamDestination()))},stop:()=>{h=!0,M(),re(),r.length=0,i=!1,o=!1,a=!1,l=!1,u=0,c=0,f=0,p=0,K=null,P=null,k=null,O()},markStreamComplete:g=>{if(p=Math.max(p,Ke(g)),l=!0,!a&&g.length>0){X(g);return}J()},playAudioChunk:async(g,B,R,y,S,F,G)=>{if(h)return;K=g,P=B;const W=In(G?.sampleRate),j=G?.newStream===!0,ve=D();if(!R?.trim()){if(y.length===0&&S.length===0)return;ue(ve,g,B),j&&G?.onNewStream?.(),Z(g,B,y,S,0,!0,T),p=Ke(g),!o&&p>0&&X(g),e.onNotify();return}r.push({audio:R,audioBuffer:null,utterance:ve,visemes:y,words:S,isNewSegment:F,sampleRate:W,newStream:j,onNewStream:G?.onNewStream,visemesQueued:!1}),await z(g,B)},queueTimingsOnly:(g,B,R,y,S)=>{if(h)return;const F=D();ue(F,g,B),Z(g,B,R,y,0,!0,T),F.segmentOffsetMs=0,p=Ke(g)},getPlaybackElapsedMs:()=>a&&s&&c>0?Math.max(0,(s.currentTime-c)*1e3):o?Math.max(0,(performance.now()-f)*w):0,isSpeaking:()=>o?!0:!a||!s||c<=0?!1:(s.currentTime-c)*1e3<p,getVoiceStream:()=>I?I.stream:null,setMuted:g=>{x=g,b&&(b.gain.value=g?0:1)},isMuted:()=>x,getPlaybackStartPerf:()=>f,bargeIn:()=>{h=!0,l=!1,M(),re(),r.length=0,i=!1,o=!1,a=!1,u=0,c=0,f=0,p=0,K=null,P=null,k=null,O()},interrupt:()=>{h=!0,l=!1,M(),re(),r.length=0,i=!1,o=!1,a=!1,u=0,c=0,f=0,p=0,K=null,P=null,k=null,O()}}}function bt(e,t){let r=null;for(const n of e)t<n.vtime||t>=n.vtime+n.vduration||(!r||n.vtime>=r.vtime)&&(r=n);return r?r.viseme:"sil"}function Pn(e,t,r=120){const n=bt(e,t);if(n!=="sil")return n;let s=null;for(const i of e){if(i.vtime>t)break;const o=i.vtime+i.vduration;o<=t&&t-o<=r&&(s=i)}return s?.viseme??"sil"}function Vs(e,t,r=400){const n=t-r;let s=0;for(const i of e)if(i.vtime+i.vduration<n)s+=1;else break;s>0&&e.splice(0,s)}function Ws(e,t){for(const r of e)if(t>=r.wtime&&t<r.wtime+r.wduration)return r;return null}function Hs(e,t){const r=e.trim();if(!r)return null;try{const n=r.replace(/\s+/g,""),s=atob(n).length,i=Number.isFinite(t)&&(t??0)>0?Number(t):16e3;return s/2/i*1e3}catch{return null}}const $t=.5,qt=1.5,yr="Olivia",_r="f786b574-daa5-4673-aa0c-cbe3e8534c02",kt=.85;function ar(e,t=vt,r={}){const n=`${t.replace(/\/$/,"")}/avatar_ttsWithPoses`,s=[],i=[],o=[],a=[];let l="",f="neutral",c="",u=ze;const d=new Set;let w=0,T=null;const h=()=>d.forEach(P=>P());let I=()=>!1;const b=An({onSpeakingStart:()=>e("speaking"),onSpeakingDone:()=>e("done"),onNotify:h,shouldHoldForWarmup:()=>I()},ze),x=()=>b.getPlaybackElapsedMs(),p=(P,k=l)=>{if(!r.onLipsyncDebug)return;const D=x(),O=s;r.onLipsyncDebug({event:P,spokenText:k,mood:f,playbackAnchorPerf:b.getPlaybackStartPerf(),playbackElapsedMs:D,queueLength:O.length,activeViseme:bt(O,D),segments:a.map(M=>({...M,visemes:[...M.visemes]}))})},A=(P,k)=>{r.onLipsyncDebug&&(T={event:P,spokenText:k},w===0&&(w=requestAnimationFrame(()=>{w=0;const D=T;T=null,D&&p(D.event,D.spokenText)})))},C=(P,k,D,O,M)=>{if(!r.onLipsyncDebug)return;const re=s.slice(k),N=D.filter(J=>String(J.symbol??J.viseme??"").trim());a.push({spokenText:P,audioOffsetMs:re[0]?.vtime??0,isNewSegment:O,chunkDurationMs:M,receivedAtMs:performance.now(),visemes:re.map((J,X)=>{const Q=N[X];return{rawSymbol:String(Q?.symbol??Q?.viseme??J.viseme).trim(),viseme:J.viseme,startSec:Number(Q?.start??0),durationSec:Number(Q?.duration??0),vtimeMs:J.vtime,vdurationMs:J.vduration}})})},L=()=>{b.stop(),s.length=0,i.length=0,o.length=0,f="neutral",a.length=0,l="",e("idle"),A("reset"),h()};return{setDeveloperToken:P=>{c=P},setTtsEngineId:P=>{u=P},getTtsEngineId:()=>u,speak:async(P,k)=>{L(),e("loading"),l=P,a.length=0;const D=c.trim();if(!D)throw new Error("Developer token required for avatar_ttsWithPoses");const O=k?.ttsEngineId??u??ze;b.resetForNewStream(O),b.setStreamTtsEngineId(O);const M=k?.voiceId??yr,re=k?.speakingRate??kt,N=Math.max($t,Math.min(qt,re)),J={user_query:P,ttsEngineId:O,speakingRate:N,tenant:"aiTwin"};ln.has(O)&&(J.voice_id=M);try{const X=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${D}`},body:JSON.stringify(J)});if(!X.ok)throw new Error(`avatar_ttsWithPoses failed (${X.status})`);const Q=X.body;if(!Q)throw new Error("No response body");const _e=Q.getReader(),Z=new TextDecoder;let ue="",ce=!0;const pe=async ae=>{const z=_n(ae);if(z){if(z.event==="audio"){const ee=z.data.chunk,te=z.data.visemes??[],we=z.data.words??[],g=z.data.is_new_segment??!1,B=z.data.sample_rate,R=s.length;if(ee){const y=ce;ce=!1,await b.playAudioChunk(s,i,ee,te,we,g,{sampleRate:B,newStream:y}),C(P,R,te,g,Hs(ee,B)),A("audio_chunk",P)}else(te.length>0||we.length>0)&&(b.queueTimingsOnly(s,i,te,we,g),C(P,R,te,g,null),A("audio_chunk",P))}else if(z.event==="metadata"){const ee=z.data.mood;typeof ee=="string"&&ee.trim()&&(f=ee.trim());const te=z.data.sentence_emotions;if(Array.isArray(te)){o.length=0;for(const we of te){if(!we||typeof we!="object")continue;const g=we;o.push({sentence_index:Number(g.sentence_index??0),text:String(g.text??""),sentiment:typeof g.sentiment=="string"?g.sentiment:void 0,emotion:String(g.emotion??f),start_word:Number(g.start_word??0),end_word:Number(g.end_word??0)})}}h(),A("metadata",P)}else if(z.event==="error")throw new Error(String(z.data.message??"avatar_ttsWithPoses stream error"))}};for(;;){const{done:ae,value:z}=await _e.read();z&&(ue+=Z.decode(z,{stream:!0}));const ee=ue.split(`
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const nr=require("react/jsx-runtime"),T=require("react"),es=require("@pipecat-ai/client-js"),_r=require("@pipecat-ai/websocket-transport"),ne=require("pixi.js");var sr=typeof document<"u"?document.currentScript:null;const ts="https://aitwin.bubu.social",dn={publicBase:ts},rs="0.4.8",ns={version:rs},ss={},Et="https://ai.aitwin.me",Sr=`${dn.publicBase}/custom-faces`,is=`${dn.publicBase}/aitwin-workers/v${ns.version}/visemeDiffPreview.worker.js`;function as(e){if(e)return e;const t=typeof{url:typeof document>"u"?require("url").pathToFileURL(__filename).href:sr&&sr.tagName.toUpperCase()==="SCRIPT"&&sr.src||new URL("aitwin.cjs",document.baseURI).href}<"u"?ss?.VITE_AITWIN_WORKER_URL:void 0;return t?.trim()?t.trim():is}const os="https://us-central1-streamoji-265f4.cloudfunctions.net/getAiTwin",ir=3e3,vt=16,$r=3e3,cs=4e3,us=.85,ls=8,vr=400,Tr="eng_a7f2b9c1",jt="eng_d4e8f3a2",tt="eng_c9b1e6d4",Ze=tt,mn=new Set([jt,tt]),hn={[Tr]:"oculus_direct",[tt]:"oculus_direct",[jt]:"inworld_word"},Hr={google:Tr,inworld:jt,cartesia:tt};function fs(e){return hn[e]}function ds(e){return e===tt}function ms(e){if(e.ttsEngineId&&e.ttsEngineId in hn)return e.ttsEngineId;const t=e.tts?.toLowerCase();return t&&t in Hr?Hr[t]:Ze}class Wt extends Error{constructor(t="AI twin not found"){super(t),this.name="AiTwinNotFoundError"}}async function pn(e,t=os){const r=new URL(t);r.searchParams.set("id",e);const n=await fetch(r.toString());if(n.status===404)throw new Wt;if(!n.ok)throw new Error(`getAiTwin failed (${n.status})`);const s=await n.json();if(!s.success)throw new Wt(s.error??"AI twin not found");const{tts:i,token:o,...a}=s.data,l=String(a.authToken??o??"").trim()||void 0;return{...a,ttsEngineId:ms(s.data),authToken:l}}const hs="https://us-central1-streamoji-265f4.cloudfunctions.net/getAuthToken",ps="client_6TNvp3SCs4Og0a1ijm9TommXLql1",ws="kk8Fq8EmexzP10jMIEwY3R44M5RKUEm1",gs="6TNvp3SCs4Og0a1ijm9TommXLql1",ys="Swaraj Mali";async function Vt(){const e=await fetch(hs,{method:"POST",headers:{"Content-Type":"application/json","Client-Id":ps,"Client-Secret":ws},body:JSON.stringify({userId:gs,userName:ys})});if(!e.ok)throw new Error(`getAuthToken failed (${e.status})`);const t=await e.json(),r=t.authToken??t.token??"";if(!r.trim())throw new Error("getAuthToken returned no token");return r}async function _s(e,t=Et){const r=await fetch(`${t.replace(/\/$/,"")}/api/session-value`,{headers:{Authorization:`Bearer ${e}`}});if(!r.ok)throw new Error(`/api/session-value failed (${r.status})`);const n=await r.json();if(!n.value)throw new Error("Session value response missing value");return n}function Ss(e){const t=e.trim();if(t.length%2!==0)throw new Error("Invalid hex key");const r=new Uint8Array(t.length/2);for(let n=0;n<t.length;n+=2)r[n/2]=Number.parseInt(t.slice(n,n+2),16);return r}function Ft(e){return Uint8Array.from(e)}function vs(e,t){const r=new Uint8Array(e.length+t.length);return r.set(e,0),r.set(t,e.length),r}async function Ts(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 r=new Uint8Array(e),n=Ft(r.subarray(0,12)),s=Ft(r.subarray(12,28)),i=Ft(r.subarray(28)),o=Ft(vs(i,s)),a=await crypto.subtle.importKey("raw",Ss(t),{name:"AES-GCM"},!1,["decrypt"]);return crypto.subtle.decrypt({name:"AES-GCM",iv:n,tagLength:128},a,o)}let fr=null;const dr=new Set;function Lt(e){return e.includes("/custom-faces/")}function $t(e){const t=e.binBase.trim().replace(/\/$/,""),r=e.twinBase.trim().replace(/\/$/,"");if(!(e.encrypted||Lt(t)||Lt(r)))return{twinBase:r,binBase:t,encrypted:!1};const s=Lt(t)?t:Lt(r)?r:t;return{twinBase:s,binBase:s,encrypted:!0}}function rt(e){return $t(e).encrypted}function mr(e,t=Sr){const r=`${t.replace(/\/$/,"")}/${e}`;return{twinBase:r,binBase:r,encrypted:!0}}function wn(e,t){return(e??t)?.trim()||void 0}function gn(e){fr=$t(e);for(const t of dr)t()}function bs(e,t){const r=typeof globalThis.location<"u"?globalThis.location.href:void 0;if(!r)return $t(e);const n=s=>{try{return new URL(s,r).href.replace(/\/$/,"")}catch{return s}};return $t({twinBase:n(e.twinBase),binBase:n(e.binBase),encrypted:e.encrypted})}function nt(){if(!fr)throw new Error("Twin assets not configured; call setActiveTwinAssets first");return fr}function yn(e){return dr.add(e),()=>dr.delete(e)}function Ht(){const e=nt();return rt(e)?`${e.binBase}/sil.bin`:`${e.twinBase}/sil.png`}function Es(){const e=nt();return rt(e)?`${e.binBase}/idle.bin`:`${e.twinBase}/idle.mp4`}function _n(){const e=nt();return rt(e)?e.binBase:e.twinBase}function kt(){return rt(nt())}function Sn(){const e=nt();return rt(e)?`${e.binBase}/atlas.bin`:`${e.twinBase}/atlas.json`}function vn(){const e=nt();return rt(e)?`${e.binBase}/expression_atlas.bin`:`${e.twinBase}/expression_atlas.json`}function Tn(e){const t=e.split(/\r?\n/);let r="",n="";for(const i of t)i.startsWith("event:")?r=i.slice(6).trim():i.startsWith("data:")&&(n=i.slice(5).trim());if(!r)return null;let s={};if(n)try{s=JSON.parse(n)}catch{s={raw:n}}return{event:r,data:s}}const bn=["aa","CH","DD","E","FF","I","O","PP","RR","SS","TH","U","kk","nn","sil"],ks=new Map(bn.map(e=>[e.toLowerCase(),e]));function En(e){const t=(e??"").trim();return t?ks.get(t.toLowerCase())??"sil":"sil"}const Is={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 As(e){if(!e)return["sil"];const t=e.toLowerCase();return Is[t]??["sil"]}function xs(e,t){const r=e.word.length;if(r<=0)return 0;const n=e.wduration;if(n<=0)return 0;const s=Math.max(0,Math.min(1,(t-e.wtime)/n));return Math.min(r-1,Math.floor(s*r))}function dt(e,t){const r=Math.max(0,Math.min(e.length-1,t));return e[r]??""}function br(e,t){const r=e.toLowerCase().replace(/[^a-z']/g,"");return!r||dt(r,t)!=="w"||t>0&&dt(r,t-1)==="q"?!1:t===0&&/^w[aeiouy]/.test(r)}function Ps(e,t){const r=e.toLowerCase(),n=dt(r,t),s=r.slice(Math.max(0,t-1),t+2);return n==="i"||n==="y"?"I":s.includes("ee")||n==="e"||r.includes("ea")?"E":"aa"}function Rs(e,t){const r=e.toLowerCase(),n=r.slice(Math.max(0,t-1),Math.min(r.length,t+3));if(/oo|ou|uw/.test(n))return"U";if(br(r,t))return"FF";const s=dt(r,t);return s==="u"||s==="w"?"U":"O"}function Ms(e,t){const r=e.toLowerCase();if(br(r,t))return"FF";const n=r.slice(Math.max(0,t-2),t+1);if(/qu/.test(n))return"U";const s=r.slice(Math.max(0,t-1),Math.min(r.length,t+3));return/wo|wh/.test(s)||dt(r,t)==="o"?"O":"U"}const Ot={d:"DD",t:"DD",n:"DD",s:"SS",z:"SS",k:"kk",g:"kk",c:"CH",x:"CH",j:"CH"};function Cs(e,t){const r=e.toLowerCase(),n=dt(r,t);if(Ot[n])return Ot[n];for(let s=0;s<r.length;s++){const i=r[s];if(Ot[i])return Ot[i]}return"DD"}function Ds(e,t,r){const n=As(e);if(n.length===1)return n[0];const s=t.trim();if(!s)return n[0];const i=(e??"").toLowerCase(),o=Math.max(0,r);switch(i){case"aei":return Ps(s,o);case"o":return Rs(s,o);case"qw":return Ms(s,o);case"cdgknstxyz":return Cs(s,o);default:return n[0]}}const Bs=32,qr=48;function kn(e){let t=0;for(const r of e)t=Math.max(t,r.vtime+r.vduration);return t}function In(e){let t=null,r=-1;for(const n of e){if(n.viseme==="sil")continue;const s=n.vtime+n.vduration;s>r&&(t=n,r=s)}return t}function Fs(e){return An(e).find(r=>r.viseme!=="sil")??null}function Je(e){const t=kn(e);return In(e)?t+vr:t}function Ls(e){const t=[],r=Fs(e);r&&r.vtime>0&&t.push({from:"sil",to:r.viseme,fromVtime:0,toVtime:r.vtime});const n=In(e),s=kn(e);return n&&t.push({from:n.viseme,to:"sil",fromVtime:s,toVtime:s+vr}),t}function An(e){const t=new Map;for(const r of e)t.has(r.vtime)||t.set(r.vtime,r);return[...t.values()].sort((r,n)=>r.vtime-n.vtime)}function Os(e){const t=An(e),r=[];for(let n=0;n<t.length-1;n++){const s=t[n].viseme,i=t[n+1].viseme,o=t[n].vtime,a=t[n+1].vtime;s===i||a<=o||r.push({from:s,to:i,fromVtime:o,toVtime:a})}return r}function Ns(e){return e.length===0?[]:[...Ls(e),...Os(e)]}function Us(e,t){let r=null;for(const n of e){const{transStart:s,transEnd:i}=xn(n.fromVtime,n.toVtime,n.from,n.to);t>=s&&t<i&&(r=n)}return r}function xn(e,t,r,n){const s=Math.max(0,t-e);if((r==="PP"||n==="PP")&&s>qr){const f=qr,u=t,c=u-f;return{gapMs:s,effectiveGapMs:f,holdEnd:c,transStart:c,transEnd:u}}const o=s>0?Math.max(s,Bs):0,a=e,l=e+o;return{gapMs:s,effectiveGapMs:o,holdEnd:e,transStart:a,transEnd:l}}const Er=16e3;function Pn(e){const t=Number(e);return Number.isFinite(t)&&t>0?t:Er}function Vs(e){let t=e.trim();const r=t.indexOf(",");t.startsWith("data:")&&r>=0&&(t=t.slice(r+1)),t=t.replace(/\s+/g,"").replace(/-/g,"+").replace(/_/g,"/");const n=t.length%4;return n!==0&&(t+="=".repeat(4-n)),t}function Ws(e){return e.length>=4&&e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70}function Kr(e,t,r=Er){const n=t.byteOffset%2===0?t:t.slice(),s=new Int16Array(n.buffer,n.byteOffset,n.byteLength/2),i=new Float32Array(s.length);for(let a=0;a<s.length;a++)i[a]=s[a]>=32768?-(65536-s[a])/32768:s[a]/32767;const o=e.createBuffer(1,i.length,r);return o.copyToChannel(i,0),o}function $s(e){const t=Vs(e),r=window.atob(t),n=new Uint8Array(r.length);for(let s=0;s<r.length;s++)n[s]=r.charCodeAt(s);return n}async function Gr(e,t,r=Er){const n=$s(t),s=Pn(r);if(Ws(n)){const i=n.buffer.slice(n.byteOffset,n.byteOffset+n.byteLength);return e.decodeAudioData(i)}if(n.length>=2&&n.length%2===0)return Kr(e,n,s);try{const i=n.buffer.slice(n.byteOffset,n.byteOffset+n.byteLength);return await e.decodeAudioData(i)}catch{return Kr(e,n,s)}}function Hs(e,t){for(const r of e)if(t>=r.wtime&&t<r.wtime+r.wduration)return r;return null}function Rn(e,t){const r=[],n=[];let s=null,i=!1,o=!1,a=!1,l=!1,f=0,u=0,c=0,d=null,w=1,b=t,h=!1,A=null,v=0,R=!1,g=!1,E=0,F=null,D=null,Y=null;const O=()=>({visemes:[],words:[],segmentOffsetMs:0}),k=()=>{g=!1,E=0},x=()=>{d!=null&&(clearTimeout(d),d=null)},M=()=>{for(const y of n){try{y.onended=null,y.stop(0)}catch{}try{y.disconnect()}catch{}}n.length=0},B=()=>{for(const y of n)y.playbackRate.value=w},ee=()=>{if(!(h||!l||i||g||n.length>0||r.length>0)){if(a&&s&&u>0){const y=(s.currentTime-u)*1e3;if(y<v){x();const I=w>0?(v-y)/w:v-y;d=setTimeout(()=>{h||(o=!1,d=null,e.onSpeakingDone(),e.onNotify())},Math.max(0,I));return}}o=!1,x(),e.onSpeakingDone(),e.onNotify()}},V=y=>{const I=Je(y);if(I<=0){e.onSpeakingDone(),e.onNotify();return}o=!0,f=performance.now(),e.onSpeakingStart(),e.onNotify(),x();const U=w>0?I/w:I;d=setTimeout(()=>{h||(o=!1,d=null,e.onSpeakingDone(),e.onNotify())},U)},te=(y,I,U,G)=>{I.length!==0&&(c=U,I.forEach($=>{const p=String($.word??"").trim();if(!p)return;const S=Math.round(($.start??0)*1e3),C=$.duration!=null?Math.round($.duration*1e3):$.end!=null?Math.max(1,Math.round(($.end-($.start??0))*1e3)):0;y.push({word:p,wtime:c+S,wduration:C,queueIndex:y.length,gesture:$.gesture??null,emphasis:$.emphasis===!0})}))},X=(y,I,U,G,$,H)=>{U.length!==0&&(c=G,U.forEach(p=>{const S=String(p.symbol??p.viseme??"").trim();if(!S)return;const C=Math.round((p.start??0)*1e3),le=Math.max(1,Math.round((p.duration??0)*1e3)),oe=c+C,pe=Hs(I,oe),ye=pe?xs(pe,oe):0;let ve;fs(H)==="oculus_direct"?(ve=En(S),ve==="U"&&pe&&br(pe.word,ye)&&(ve="FF")):ve=Ds(S,pe?.word??"",ye),y.push({viseme:ve,weight:1,vtime:oe,vduration:le})}),e.onNotify())},z=(y,I,U,G,$,H,p)=>{te(I,G,$),X(y,I,U,$,H,p)},me=(y,I,U)=>{Y===y&&y.visemes===I||(I.length=0,U.length=0,y.visemes=I,y.words=U,y.segmentOffsetMs=0,Y=y,c=0,v=0)},ae=async()=>{const y=window.AudioContext||window.webkitAudioContext;return s||(s=new y),s.state==="suspended"&&await s.resume(),A||(A=s.createMediaStreamDestination()),s},Q=(y,I)=>{if(!s)return 0;const U=s,G=U.currentTime,$=G+.02;if(I.isFirstAudio){u<=0&&(u=$);const p=($-G)*1e3;f=performance.now()+p}I.onBeforeStart?.($);const H=U.createBufferSource();return H.buffer=y,H.playbackRate.value=w,H.connect(U.destination),A&&H.connect(A),n.push(H),h?(n.pop(),$):(I.isFirstAudio&&(o=!0,e.onSpeakingStart()),H.onended=()=>{const p=n.indexOf(H);p>=0&&n.splice(p,1),e.onNotify(),!h&&(F&&D?de(F,D):ee())},H.start($),$)},de=async(y,I)=>{if(h||i||n.length>0)return;if(r.length===0){ee();return}i=!0;let U=!1;try{const G=await ae();if(h)return;const $=r[0];if(!$)return;const H=!o;if(R&&(e.shouldHoldForWarmup?.()??!1)&&(g||H&&$.newStream)||g){me($.utterance,y,I);for(const C of r){if(h)return;C.audioBuffer||(C.audioBuffer=await Gr(G,C.audio,C.sampleRate)),a=!0,g||(g=!0,E=0,C.newStream&&C.onNewStream?.()),C.visemesQueued||(z(y,I,C.visemes,C.words,E,!0,b),C.visemesQueued=!0,E+=Math.round(C.audioBuffer.duration*1e3),v=Math.max(v,Je(y)))}e.onNotify();return}const S=r.shift();if(!S||(S.audioBuffer||(S.audioBuffer=await Gr(G,S.audio,S.sampleRate)),h))return;a=!0,x(),me(S.utterance,y,I),S.visemesQueued=!1,S.newStream&&S.onNewStream?.(),Q(S.audioBuffer,{isFirstAudio:!o,isFirstOfStream:!1,onBeforeStart:C=>{u=C,c=0,f=performance.now()+(C-G.currentTime)*1e3,z(y,I,S.visemes,S.words,0,!0,b),S.visemesQueued=!0,S.utterance.segmentOffsetMs=0}}),U=n.length>0,v=Je(y),e.onNotify()}catch(G){console.error("[avatarChunkPlayback] audio chunk failed:",G)}finally{if(i=!1,h)return;if(g){r.some($=>!$.audioBuffer||!$.visemesQueued)&&de(y,I);return}!U&&n.length===0&&r.length>0?de(y,I):n.length===0&&r.length===0&&ee()}};return{setStreamTtsEngineId:y=>{b=y},setPlaybackSpeed:y=>{w=Math.max(.1,Math.min(1,y)),B()},getPlaybackSpeed:()=>w,setLipsyncWarmupHoldEnabled:y=>{R=y,y||k()},isWarmingUp:()=>g,releaseWarmupHold:()=>{if(g){g=!1,E=0;for(const y of r)y.newStream=!1;F&&D&&de(F,D),e.onNotify()}},resetForNewStream:y=>{h=!1,l=!1,b=y,v=0},resumeBotStream:()=>{h=!1,l=!1},clearAborted:()=>{h=!1},primeAudioContext:async()=>{const y=window.AudioContext||window.webkitAudioContext;s||(s=new y),s.state==="suspended"&&await s.resume(),s&&!A&&(A=s.createMediaStreamDestination())},stop:()=>{h=!0,x(),M(),r.length=0,i=!1,o=!1,a=!1,l=!1,c=0,u=0,f=0,v=0,F=null,D=null,Y=null,k()},markStreamComplete:y=>{if(v=Math.max(v,Je(y)),l=!0,!a&&y.length>0){V(y);return}ee()},playAudioChunk:async(y,I,U,G,$,H,p)=>{if(h)return;F=y,D=I;const S=Pn(p?.sampleRate),C=p?.newStream===!0,le=O();if(!U?.trim()){if(G.length===0&&$.length===0)return;me(le,y,I),C&&p?.onNewStream?.(),z(y,I,G,$,0,!0,b),v=Je(y),!o&&v>0&&V(y),e.onNotify();return}r.push({audio:U,audioBuffer:null,utterance:le,visemes:G,words:$,isNewSegment:H,sampleRate:S,newStream:C,onNewStream:p?.onNewStream,visemesQueued:!1}),await de(y,I)},queueTimingsOnly:(y,I,U,G,$)=>{if(h)return;const H=O();me(H,y,I),z(y,I,U,G,0,!0,b),H.segmentOffsetMs=0,v=Je(y)},getPlaybackElapsedMs:()=>a&&s&&u>0?Math.max(0,(s.currentTime-u)*1e3):o?Math.max(0,(performance.now()-f)*w):0,isSpeaking:()=>o?!0:!a||!s||u<=0?!1:(s.currentTime-u)*1e3<v,getVoiceStream:()=>A?A.stream:null,getPlaybackStartPerf:()=>f,bargeIn:()=>{h=!0,l=!1,x(),M(),r.length=0,i=!1,o=!1,a=!1,c=0,u=0,f=0,v=0,F=null,D=null,Y=null,k()},interrupt:()=>{h=!0,l=!1,x(),M(),r.length=0,i=!1,o=!1,a=!1,c=0,u=0,f=0,v=0,F=null,D=null,Y=null,k()}}}function It(e,t){let r=null;for(const n of e)t<n.vtime||t>=n.vtime+n.vduration||(!r||n.vtime>=r.vtime)&&(r=n);return r?r.viseme:"sil"}function Mn(e,t,r=120){const n=It(e,t);if(n!=="sil")return n;let s=null;for(const i of e){if(i.vtime>t)break;const o=i.vtime+i.vduration;o<=t&&t-o<=r&&(s=i)}return s?.viseme??"sil"}function qs(e,t,r=400){const n=t-r;let s=0;for(const i of e)if(i.vtime+i.vduration<n)s+=1;else break;s>0&&e.splice(0,s)}function Ks(e,t){for(const r of e)if(t>=r.wtime&&t<r.wtime+r.wduration)return r;return null}function Gs(e,t){const r=e.trim();if(!r)return null;try{const n=r.replace(/\s+/g,""),s=atob(n).length,i=Number.isFinite(t)&&(t??0)>0?Number(t):16e3;return s/2/i*1e3}catch{return null}}const Yt=.5,Xt=1.5,kr="Olivia",Ir="f786b574-daa5-4673-aa0c-cbe3e8534c02",At=.85;function hr(e,t=Et,r={}){const n=`${t.replace(/\/$/,"")}/avatar_ttsWithPoses`,s=[],i=[],o=[],a=[];let l="",f="neutral",u="",c=Ze;const d=new Set;let w=0,b=null;const h=()=>d.forEach(O=>O());let A=()=>!1;const v=Rn({onSpeakingStart:()=>e("speaking"),onSpeakingDone:()=>e("done"),onNotify:h,shouldHoldForWarmup:()=>A()},Ze),R=()=>v.getPlaybackElapsedMs(),g=(O,k=l)=>{if(!r.onLipsyncDebug)return;const x=R(),M=s;r.onLipsyncDebug({event:O,spokenText:k,mood:f,playbackAnchorPerf:v.getPlaybackStartPerf(),playbackElapsedMs:x,queueLength:M.length,activeViseme:It(M,x),segments:a.map(B=>({...B,visemes:[...B.visemes]}))})},E=(O,k)=>{r.onLipsyncDebug&&(b={event:O,spokenText:k},w===0&&(w=requestAnimationFrame(()=>{w=0;const x=b;b=null,x&&g(x.event,x.spokenText)})))},F=(O,k,x,M,B)=>{if(!r.onLipsyncDebug)return;const ee=s.slice(k),V=x.filter(te=>String(te.symbol??te.viseme??"").trim());a.push({spokenText:O,audioOffsetMs:ee[0]?.vtime??0,isNewSegment:M,chunkDurationMs:B,receivedAtMs:performance.now(),visemes:ee.map((te,X)=>{const z=V[X];return{rawSymbol:String(z?.symbol??z?.viseme??te.viseme).trim(),viseme:te.viseme,startSec:Number(z?.start??0),durationSec:Number(z?.duration??0),vtimeMs:te.vtime,vdurationMs:te.vduration}})})},D=()=>{v.stop(),s.length=0,i.length=0,o.length=0,f="neutral",a.length=0,l="",e("idle"),E("reset"),h()};return{setDeveloperToken:O=>{u=O},setTtsEngineId:O=>{c=O},getTtsEngineId:()=>c,speak:async(O,k)=>{D(),e("loading"),l=O,a.length=0;const x=u.trim();if(!x)throw new Error("Developer token required for avatar_ttsWithPoses");const M=k?.ttsEngineId??c??Ze;v.resetForNewStream(M),v.setStreamTtsEngineId(M);const B=k?.voiceId??kr,ee=k?.speakingRate??At,V=Math.max(Yt,Math.min(Xt,ee)),te={user_query:O,ttsEngineId:M,speakingRate:V,tenant:"aiTwin"};mn.has(M)&&(te.voice_id=B);try{const X=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${x}`},body:JSON.stringify(te)});if(!X.ok)throw new Error(`avatar_ttsWithPoses failed (${X.status})`);const z=X.body;if(!z)throw new Error("No response body");const me=z.getReader(),ae=new TextDecoder;let Q="",de=!0;const he=async Se=>{const j=Tn(Se);if(j){if(j.event==="audio"){const y=j.data.chunk,I=j.data.visemes??[],U=j.data.words??[],G=j.data.is_new_segment??!1,$=j.data.sample_rate,H=s.length;if(y){const p=de;de=!1,await v.playAudioChunk(s,i,y,I,U,G,{sampleRate:$,newStream:p}),F(O,H,I,G,Gs(y,$)),E("audio_chunk",O)}else(I.length>0||U.length>0)&&(v.queueTimingsOnly(s,i,I,U,G),F(O,H,I,G,null),E("audio_chunk",O))}else if(j.event==="metadata"){const y=j.data.mood;typeof y=="string"&&y.trim()&&(f=y.trim());const I=j.data.sentence_emotions;if(Array.isArray(I)){o.length=0;for(const U of I){if(!U||typeof U!="object")continue;const G=U;o.push({sentence_index:Number(G.sentence_index??0),text:String(G.text??""),sentiment:typeof G.sentiment=="string"?G.sentiment:void 0,emotion:String(G.emotion??f),start_word:Number(G.start_word??0),end_word:Number(G.end_word??0)})}}h(),E("metadata",O)}else if(j.event==="error")throw new Error(String(j.data.message??"avatar_ttsWithPoses stream error"))}};for(;;){const{done:Se,value:j}=await me.read();j&&(Q+=ae.decode(j,{stream:!0}));const y=Q.split(`
|
|
2
2
|
|
|
3
|
-
`);
|
|
3
|
+
`);Q=y.pop()??"";for(const I of y)await he(I);if(Se){Q.trim()&&await he(Q.trim()),v.markStreamComplete(s),E("bot_stopped",O);break}}}catch(X){throw console.error("[avatarTtsLipsync]",X),D(),e("error"),X}},stop:D,getVisemeQueue:()=>s,getWordQueue:()=>i,getSentenceEmotions:()=>o,getStreamMood:()=>f,getPlaybackElapsedMs:()=>v.getPlaybackElapsedMs(),isSpeaking:()=>v.isSpeaking(),isWarmingUp:()=>v.isWarmingUp(),releaseWarmupHold:()=>v.releaseWarmupHold(),setLipsyncWarmupHold:(O,k)=>{k&&(A=k),v.setLipsyncWarmupHoldEnabled(O)},getVoiceStream:()=>v.getVoiceStream(),setPlaybackSpeed:O=>{v.setPlaybackSpeed(O)},getPlaybackSpeed:()=>v.getPlaybackSpeed(),subscribe:O=>(d.add(O),()=>d.delete(O))}}const Ar=16e3,xr=16e3;class zs extends _r.ProtobufFrameSerializer{async deserialize(t){try{return await super.deserialize(t)}catch(r){if(r instanceof Error&&r.message==="Unknown frame kind")return{type:"raw",message:null};throw r}}}class js extends _r.DailyMediaManager{constructor(){super(!0,!0,void 0,void 0,512,Ar,xr)}bufferBotAudio(t,r){}}const Ys=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Xs(e){return Ys.test(e.trim())}function Js(e){const t=e.replace(/\/$/,"");return t.startsWith("https://")?`wss://${t.slice(8)}`:t.startsWith("http://")?`ws://${t.slice(7)}`:t}function Cn(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 r=e.apiBase??Et,n=new URL(`${Js(r)}/ws/voice`);n.searchParams.set("authToken",t),n.searchParams.set("tenant","aiTwin");const s=e.voiceId?.trim();s&&Xs(s)&&n.searchParams.set("voiceId",s);const i=e.personaId?.trim();i&&n.searchParams.set("personaId",i);const o=e.speakingRate??At;return n.searchParams.set("speaking_rate",String(Math.max(Yt,Math.min(Xt,o)))),e.ttsStream===!1?n.searchParams.set("tts_stream","false"):n.searchParams.set("tts_stream","true"),n.toString()}function ar(e,t){if(!Array.isArray(e))return[];const r=[];for(const n of e)!n||typeof n!="object"||r.push({sentence_index:Number(n.sentence_index??0),text:String(n.text??""),sentiment:typeof n.sentiment=="string"?n.sentiment:void 0,emotion:String(n.emotion??t),start_word:Number(n.start_word??0),end_word:Number(n.end_word??0)});return r}function Qs(e){if(!e||typeof e!="object")return null;const t=e,n=(t.data&&typeof t.data=="object"&&!Array.isArray(t.data)?t.data:null)||t;return{...n,type:String(n.type??t.type??"server-message")}}function or(e){return e.is_new_segment===!0||Number(e.audio_offset_ms??-1)===0?!0:Number(e.chunk_index??-1)===0}function Zs(e){return e?.transport?._mediaManager?._wavStreamPlayer??null}function Dn(e,t={}){const r=[],n=[],s=[],i=[];let o="",a="neutral",l=tt,f=!1,u=null,c=null,d={...t},w=0,b=null;const h=new Set,A=[];let v=!1,R=null,g=!0,E="default";const F=new Set;let D=null,Y=0,O=!1,k=null,x=!1;const M=()=>{k!=null&&(clearTimeout(k),k=null)},B=()=>h.forEach(m=>m());let ee=()=>!1;const V=Rn({onSpeakingStart:()=>e("speaking"),onSpeakingDone:()=>{x=!1,e("done")},onNotify:B,shouldHoldForWarmup:()=>ee()},l),te=()=>{if(R)return R;const m=new MessageChannel;return m.port1.onmessage=()=>{const P=A.shift();P!==void 0&&C(P),A.length>0?m.port2.postMessage(null):v=!1},R=m,m},X=m=>{A.push(m),!v&&(v=!0,te().port2.postMessage(null))},z=()=>V.getPlaybackElapsedMs(),me=(m,P=o)=>{if(!d.onLipsyncDebug)return;const J=z(),ue=r;d.onLipsyncDebug({event:m,spokenText:P,mood:a,playbackAnchorPerf:V.getPlaybackStartPerf(),playbackElapsedMs:J,queueLength:ue.length,activeViseme:It(ue,J),segments:i.map(_e=>({..._e,visemes:[..._e.visemes]}))})},ae=(m,P)=>{d.onLipsyncDebug&&(b={event:m,spokenText:P},w===0&&(w=requestAnimationFrame(()=>{w=0;const J=b;b=null,J&&me(J.event,J.spokenText)})))},Q=()=>{g=!0,F.clear(),V.clearAborted()},de=()=>{const m=Zs(u);if(m){E=`bot-${Date.now()}`,m.interruptedTrackIds={[E]:!0};try{m.interrupt?.()}catch{}}const P=c;try{P?.interrupt?.()}catch{}},he=m=>{if(x&&V.isSpeaking())return!0;const P=m.context_id?.trim();return P&&F.has(P)?!0:g?!1:!(m.type==="avatar_metadata"||or(m))},Se=m=>{g=!1,x=!1;const P=m.context_id?.trim();P&&F.add(P),M(),O=!1,A.length=0,v=!1,D?.abort(),D=null,Y+=1,de(),V.bargeIn(),j(),e("idle"),ae("interrupt"),B()},j=()=>{r.length=0,n.length=0,i.length=0,o="",a="neutral",s.length=0},y=()=>{V.stop(),j(),ae("reset"),B()},I=()=>{y(),e("idle"),B()},U=(m,P)=>{typeof m.mood=="string"&&m.mood.trim()&&(a=m.mood.trim()),Array.isArray(m.sentence_emotions)&&m.sentence_emotions.length>0&&(P&&(s.length=0),s.push(...ar(m.sentence_emotions,a)))},G=m=>{if(he({...m,type:"avatar_metadata"}))return;Q(),O=!0;const P=String(m.spoken_text??m.text??"").trim();P&&(o=P),V.resetForNewStream(l),typeof m.mood=="string"&&m.mood.trim()&&(a=m.mood.trim()),s.length=0,s.push(...ar(m.sentence_emotions,a)),Array.isArray(m.gestures)&&m.gestures,ae("metadata",P),B()},$=(m,P,J,ue)=>{if(!d.onLipsyncDebug)return;const _e=r.slice(J),Ge=ue.filter(fe=>String(fe.symbol??fe.viseme??"").trim());i.push({spokenText:P,audioOffsetMs:_e[0]?.vtime??0,isNewSegment:!!m.is_new_segment,chunkDurationMs:m.chunk_duration_ms!=null?Number(m.chunk_duration_ms):null,receivedAtMs:performance.now(),visemes:_e.map((fe,ke)=>{const Ce=Ge[ke];return{rawSymbol:String(Ce?.symbol??Ce?.viseme??fe.viseme).trim(),viseme:fe.viseme,startSec:Number(Ce?.start??0),durationSec:Number(Ce?.duration??0),vtimeMs:fe.vtime,vdurationMs:fe.vduration}})})},H=m=>{if(he(m))return;M(),or(m)&&Q();const P=String(m.spoken_text??m.text??"").trim(),J=or(m),ue=m.visemes??[],_e=m.words??[],Ge=String(m.chunk??"").trim(),fe=O;fe&&(O=!1),P&&(o=P),U(m,J),fe?V.resetForNewStream(l):J&&V.resumeBotStream();const ke=r.length;V.playAudioChunk(r,n,Ge,ue,_e,J,{sampleRate:m.sample_rate,newStream:fe,onNewStream:()=>{i.length=0}}).then(()=>{$(m,P,ke,ue),ae("audio_chunk",P)})},p=m=>{if(he(m))return;(m.is_new_segment||Number(m.audio_offset_ms??-1)===0)&&Q(),console.warn("[realtimeVoice] avatar_lipsync is deprecated — expect avatar_audio_chunk");const P=String(m.spoken_text??m.text??"").trim();P&&(o=P),U(m,!!m.is_new_segment);const J=r.length;V.queueTimingsOnly(r,n,m.visemes??[],m.words??[],!!m.is_new_segment),$(m,P,J,m.visemes??[]),ae("lipsync",P),B()},S=async m=>{const P=String(m.text??"").trim();if(!P)return;const J=d.authToken?.trim();if(!J){console.error("[realtimeVoice] avatar_speak requires authToken");return}D?.abort();const ue=new AbortController;D=ue;const _e=++Y;Q(),V.resetForNewStream(l),o=P;const fe=`${(d.apiBase??Et).replace(/\/$/,"")}/avatar_ttsWithPoses`,ke=d.voiceId?.trim()||Ir,Ce=d.speakingRate??At,Te=Math.max(Yt,Math.min(Xt,Ce)),Ne={user_query:P,ttsEngineId:l,speakingRate:Te,tenant:"aiTwin"};mn.has(l)&&(Ne.voice_id=ke);try{const Be=await fetch(fe,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${J}`},body:JSON.stringify(Ne),signal:ue.signal});if(!Be.ok)throw new Error(`avatar_ttsWithPoses failed (${Be.status})`);const Ue=Be.body;if(!Ue)throw new Error("No response body");const xt=Ue.getReader(),st=new TextDecoder;let Ve="",gt=!0;const ce=async it=>{if(_e!==Y||ue.signal.aborted||!g)return;const we=Tn(it);if(we){if(we.event==="audio"){const Ae=we.data.chunk,Ie=we.data.visemes??[],at=we.data.words??[],We=we.data.is_new_segment??!1,Pt=we.data.sample_rate;if(Ae){const Fe=gt;gt=!1,await V.playAudioChunk(r,n,Ae,Ie,at,We,{sampleRate:Pt,newStream:Fe})}else(Ie.length>0||at.length>0)&&V.queueTimingsOnly(r,n,Ie,at,We)}else if(we.event==="metadata"){const Ae=we.data.mood;typeof Ae=="string"&&Ae.trim()&&(a=Ae.trim());const Ie=we.data.sentence_emotions;Array.isArray(Ie)&&(s.length=0,s.push(...ar(Ie,a))),B()}else if(we.event==="error")throw new Error(String(we.data.message??"avatar_ttsWithPoses stream error"))}};for(;!(_e!==Y||ue.signal.aborted||!g);){const{done:it,value:we}=await xt.read();we&&(Ve+=st.decode(we,{stream:!0}));const Ae=Ve.split(`
|
|
4
4
|
|
|
5
|
-
`);Ue=Se.pop()??"";for(const Pe of Se)await dt(Pe);if(Ze){Ue.trim()&&await dt(Ue.trim()),ye===K&&!le.signal.aborted&&p&&N.markStreamComplete(r);break}}}catch(Ee){if(le.signal.aborted||!p)return;console.error("[realtimeVoice] avatar_speak failed:",Ee),e("error"),M()}finally{L===le&&(L=null)}},F=m=>{const E=js(m);if(!E){console.warn("[realtimeVoice] server message with no payload",m);return}const Y=String(E.type??"unknown");switch(Y!=="avatar_metadata"&&Y!=="avatar_audio_chunk"&&Y!=="avatar_lipsync"&&Y!=="avatar_interrupt"&&Y!=="error"&&console.debug("[realtimeVoice] server message",Y,E),Y){case"avatar_metadata":g(E);break;case"avatar_audio_chunk":R(E);break;case"avatar_lipsync":y(E);break;case"avatar_interrupt":ae(E);break;case"error":{const le=String(E.message??"Voice session error");console.error("[realtimeVoice] server error:",le),e("error"),M();break}case"avatar_speak":S(E);break}},G=()=>c||(u=new qs,c=new Xn.PipecatClient({transport:new dr.WebSocketTransport({mediaManager:u,serializer:new $s,recorderSampleRate:Sr,playerSampleRate:vr}),enableCam:!1,enableMic:!0,callbacks:{onBotReady:()=>{f=!0,e("idle"),M()},onDisconnected:()=>{f=!1,te()},onError:m=>{const E=typeof m=="object"&&m!==null&&"data"in m?String(m.data??m):String(m);console.error("[realtimeVoice]",E,m),e("error"),M()},onTransportStateChanged:m=>{m==="error"&&(e("error"),M())},onServerMessage:m=>{X(m)},onUserStartedSpeaking:()=>{},onBotStartedSpeaking:()=>{ue(),Z("bot_started")},onBotStoppedSpeaking:()=>{p&&(O(),k=setTimeout(()=>{k=null,p&&(N.markStreamComplete(r),Z("bot_stopped"))},450))},onUserTranscript:m=>{String(m.text??""),d.onUserTranscript?.(m.text,!!m.final)},onBotOutput:m=>{d.onBotOutput?.(m.text)}}}),c);return{setDeveloperToken:m=>{},setTtsEngineId:m=>{l=m,N.setStreamTtsEngineId(m)},getTtsEngineId:()=>l,speak:async(m,E)=>{const Y=m.trim();if(Y){if(!c||!f)throw new Error("Realtime voice is not connected. Call connect() first.");ee(),await c.sendText(Y)}},stop:()=>{L?.abort(),L=null,K+=1,ue(),ee(),e("idle"),M()},connect:async m=>{if(m&&(d={...d,...m}),!d.authToken?.trim())throw new Error("Voice connect requires authToken (same JWT as SSE Authorization Bearer).");e("loading");const E=G();await E.initDevices();const Y=xn(d);console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):",Y);try{await E.connect({wsUrl:Y})}catch(le){f=!1,e("error"),M();const ye=le instanceof Error?le.message:"Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";throw new Error(`${ye} — ${Y}`)}f=!0,e("idle"),M()},disconnect:async()=>{if(O(),P=!1,I.length=0,b=!1,L?.abort(),L=null,K+=1,ue(),c)try{await c.disconnect()}catch(m){console.error("[realtimeVoice] disconnect failed:",m)}f=!1,te()},isConnected:()=>f,unlockAudio:async()=>{await N.primeAudioContext()},getVisemeQueue:()=>r,getWordQueue:()=>n,getSentenceEmotions:()=>s,getStreamMood:()=>a,getPlaybackElapsedMs:Q,isSpeaking:()=>N.isSpeaking(),isWarmingUp:()=>N.isWarmingUp(),releaseWarmupHold:()=>N.releaseWarmupHold(),setLipsyncWarmupHold:(m,E)=>{E&&(re=E),N.setLipsyncWarmupHoldEnabled(m)},getVoiceStream:()=>N.getVoiceStream(),setMuted:m=>{N.setMuted(m)},isMuted:()=>N.isMuted(),setPlaybackSpeed:m=>{N.setPlaybackSpeed(m)},getPlaybackSpeed:()=>N.getPlaybackSpeed(),subscribe:m=>(h.add(m),()=>h.delete(m))}}function Rn(e,t,r){return{width:e.crop?.source_width??t,height:e.crop?.source_height??r}}function Cn(e,t){const r=e.crop;return{x:t?.source_x??r?.x??0,y:t?.source_y??r?.y??0,width:t?.source_w??r?.width??e.frame_width??0,height:t?.source_h??r?.height??e.frame_height??0}}function Dn(e){return e?(e.w??0)>0&&(e.h??0)>0:!1}async function ct(e,t){const r=await fetch(e);if(!r.ok)throw new Error(`Failed to load encrypted asset: ${e}`);const n=await r.arrayBuffer();return ys(n,t)}async function Bn(e,t){if(!t)throw new Error("Missing asset key for encrypted JSON");const r=await ct(e,t),n=new TextDecoder().decode(r);return JSON.parse(n)}async function Xs(e){const t=new Blob([e],{type:"image/webp"});return createImageBitmap(t)}async function Js(e){const t=new Blob([e],{type:"image/webp"}),r=URL.createObjectURL(t);return new Promise((n,s)=>{const i=new Image;i.decoding="async",i.onload=()=>{URL.revokeObjectURL(r),n(i)},i.onerror=()=>{URL.revokeObjectURL(r),s(new Error("Failed to decode decrypted image"))},i.src=r})}async function Qs(e){const t=new Blob([e],{type:"image/png"});return createImageBitmap(t)}async function Zs(e){const t=new Blob([e],{type:"image/png"}),r=URL.createObjectURL(t);return new Promise((n,s)=>{const i=new Image;i.decoding="async",i.onload=()=>{URL.revokeObjectURL(r),n(i)},i.onerror=()=>{URL.revokeObjectURL(r),s(new Error("Failed to decode decrypted PNG"))},i.src=r})}function ei(e){const t=new Blob([e],{type:"video/mp4"});return URL.createObjectURL(t)}async function ti(e){const t=Ss();if(!Tt())return t;if(!e)throw new Error("Missing asset key for encrypted idle video");const r=await ct(t,e);return ei(r)}function Tr(e,t){return`${e}/${t}_minus_sil.png`}const Fn=5;function Kt(e){const t=e?.composite_threshold;if(typeof t=="number"&&t>0)return t;const r=e?.crop?.content_threshold;return typeof r=="number"&&r>0?r:Fn}const ri=["aa","E","I","O","U","PP","FF","DD","SS","TH","CH","RR","kk","nn"],or=["t01_0.25","t02_0.50","t03_0.75"],ni=or.length,$r=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 qr(e,t){return`${e}__${t}`}function Kr(e,t){return`pairs/${e}/${t}_minus_sil.png`}function St(e){return`${e}_minus_sil.png`}function br(e,t){if(t.has(e))return e;const r=/^pairs\/([^/]+)\//.exec(e);if(!r)return null;const n=r[1].split("__")[1];if(!n||n==="sil")return null;const s=St(n);return t.has(s)?s:null}function Ln(e,t){return e.filter(r=>t.has(r))}function si(e,t,r){const n=kr(e,t);return Ln(n,r).length>0}function kr(e,t){if(e===t)return[];const r=[],n=qr(e,t);if($r.has(n)){for(const i of or)r.push(Kr(n,i));return r}const s=qr(t,e);if($r.has(s)){for(let i=ni-1;i>=0;i--)r.push(Kr(s,or[i]));return r}return[]}function ii(e){return e<16?[1]:e<24?[1]:e<35?[0,1,2]:[0,1,2]}function ai(e,t){return ii(t).map(n=>e[n]).filter(n=>n!=null)}function oi(e,t,r,n){if(n<=0)return 0;const i=Math.max(1,r-t)/n,o=e-t,a=Math.floor(o/i);return Math.min(n-1,Math.max(0,a))}function On(e,t,r){if(!r)return`${e}/${t}`;const n=t.split("/").pop()??t,s=n.includes(".")?n.slice(0,n.lastIndexOf(".")):n;return`${e}/${s}.bin`}const ui=[.32,.24,.16,.08,0],ci=ui.map(e=>`eyes_${e.toFixed(2)}`),li=["eyes_0.32","eyes_0.16","eyes_0.00"],fi=["eyes_0.32","eyes_0.16","eyes_0.08","eyes_0.00"],Gr=2,di=-30,ur=20,mi={fear:-30,anger:-20,sad:-10,neutral:0,love:10,happy:0,disgust:20};function Er(e){return`brow_${e}`}function Ir(e){return mi[e]}function je(e){return Math.max(di,Math.min(ur,e))}function hi(e,t){if(e===t)return[];const r=e<t?Gr:-Gr,n=[];let s=e;for(;s!==t;)s+=r,n.push(s);return n}const pi={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 r=String(e??"").trim().toLowerCase();return r?pi[r]??t:t}function Nn(e){return e===0?null:Tr(Er(e),"eyes_standard")}function Un(e){return Nn(Ir(e))}function zr(e,t){const r=Er(Ir(e));return!t||t==="eyes_standard"?Un(e):e==="neutral"&&t==="eyes_standard"?null:Tr(r,t)}function wi(e,t){const r=[];for(const n of t)r.push(zr(e,n));for(let n=t.length-2;n>=0;n--)r.push(zr(e,t[n]));return r.push(Un(e)),r}function gi(e){return Tr(Er(e),"eyes_standard")}function yi(e,t){return hi(e,t).map(gi)}const _i=6;function Si(e,t){for(const r of e)if(t>=r.wtime&&t<r.wtime+r.wduration)return r;return null}function vi(e){const t=je(e);return t>=ur?ur:je(t+_i)}function Ti(e,t,r){const n=je(r);return Si(t,e)?.emphasis?vi(n):n}function jr(e){if(!e)return 0;const t=e.match(/^brow_(-?\d+)\//);if(!t?.[1])return null;const r=Number.parseInt(t[1],10);return Number.isFinite(r)?je(r):null}function yt(e,t){return e+Math.random()*(t-e)}function Dt(){return yt(14,32)}function bi(e={}){const{initialDelayMs:t=800+Math.random()*1200,intervalMinMs:r=2200,intervalMaxMs:n=5800,doubleBlinkChance:s=.18,doubleBlinkGapMinMs:i=120,doubleBlinkGapMaxMs:o=220,fullBlinkChance:a=.75,postEmotionChangeDelayMs:l=380,minDwellMs:f=550}=e;let c="neutral",u="neutral",d=0,w=0,T=0,h={kind:"idle",nextBlinkAt:performance.now()+t},I=null,b="__eyes_open__|neutral|brow:0";function x(k=c){b=`${I??"__eyes_open__"}|${k}|brow:${w}`}function p(k,D=c){I=k;const O=jr(k);O!=null?w=O:k==null&&(w=0),x(D)}function A(k){return Nn(k)}function C(k,D,O){const M=je(D),re=je(O);if(d=re,M===re){p(A(re));return}const N=[...yi(M,re),A(re)];h={kind:"browBlend",paths:N,index:0,holdUntil:k+Dt(),targetBrow:re},p(N[0]??null)}function L(k,D){h={kind:"idle",nextBlinkAt:k+yt(r,n)},u=D,p(A(d),D)}function K(k,D,O){const M=O?li:Math.random()<a?ci:fi;h={kind:"playing",paths:wi(D,M),index:0,holdUntil:k+Dt(),after:"idle",blinkEmotion:D},u=D,p(h.paths[0]??null,D)}function P(k){if(h.kind==="browBlend"){for(;k>=h.holdUntil&&h.index<h.paths.length-1;)h.index+=1,h.holdUntil=k+Dt();const D=h.paths[h.index]??null;p(D),k>=h.holdUntil&&h.index>=h.paths.length-1&&(w=h.targetBrow,d=h.targetBrow,p(A(h.targetBrow)),h={kind:"idle",nextBlinkAt:k+l});return}if(h.kind==="idle"){p(A(d),u),k>=h.nextBlinkAt&&K(k,u,!1);return}if(h.kind==="doublePause"){p(A(d),h.blinkEmotion),k>=h.resumeAt&&K(k,h.blinkEmotion,!0);return}if(h.kind==="playing"){for(;k>=h.holdUntil&&h.index<h.paths.length-1;)h.index+=1,h.holdUntil=k+Dt();if(p(h.paths[h.index]??null,h.blinkEmotion),k>=h.holdUntil&&h.index>=h.paths.length-1){if(h.after==="resumeIdle"){L(k+l,c);return}if(h.after==="doublePause"){h={kind:"doublePause",resumeAt:k+yt(i,o),blinkEmotion:h.blinkEmotion},p(A(d),h.blinkEmotion);return}if(h.after==="idle"&&Math.random()<s){h={kind:"doublePause",resumeAt:k+yt(i,o),blinkEmotion:h.blinkEmotion},p(A(d),h.blinkEmotion);return}L(k,h.blinkEmotion)}}}return{advance:P,setTargetEmotion(k){const D=it(k,c);if(D===c)return;const O=performance.now();f>0&&D!==c&&!(c==="neutral"&&D!=="neutral")&&O-T<f||(c=D,u=D,T=O)},setTargetEyebrow(k){const D=je(k);if(D===d)return;const O=performance.now(),M=h.kind==="browBlend"?jr(I)??w:w;d=D,!(D===M&&h.kind!=="browBlend")&&(h.kind==="playing"||h.kind==="doublePause"||C(O,M,D))},getTargetEmotion:()=>c,getTargetEyebrow:()=>d,getExpressionPath:()=>I,getDrawKey:()=>b,reset(){c="neutral",u="neutral",d=0,w=0,T=0,L(performance.now()+yt(r,n),"neutral")}}}function ki(){const e=document.createElement("video");e.muted=!0,e.playsInline=!0,e.preload="auto",e.setAttribute("playsinline",""),e.loop=!0;let t=!1,r=null,n="";const s=()=>{r&&(URL.revokeObjectURL(r),r=null)};return{async load(i){t&&n===i||(s(),t=!1,n=i,i.startsWith("blob:")&&(r=i),await new Promise((o,a)=>{const l=()=>{const f=e.duration;if(!Number.isFinite(f)||f<=0){a(new Error(`Idle video has invalid duration: ${i}`));return}t=!0,e.currentTime=0,o()};e.addEventListener("loadeddata",l,{once:!0}),e.addEventListener("error",()=>a(new Error(`Failed to load idle video: ${i}`)),{once:!0}),e.src=i,e.load()}))},isReady(){return t},getVideo(){return e},setActive(i){t&&(i?e.paused&&e.play().catch(()=>{}):e.paused||e.pause())},restart(){t&&(e.currentTime=0)},dispose(){e.pause(),e.removeAttribute("src"),e.load(),s(),t=!1,n=""}}}const Ei=50,Vn=400;function Ii(e,t,r){return e.filter(n=>{const s=n.queueIndex??-1;return s>=t&&s<=r})}function Ai(e,t){const r=[];for(const n of e){const s=Ii(t,n.start_word,n.end_word);if(s.length===0)continue;const i=s[0],o=s[s.length-1];r.push({sentence:n,startMs:Math.max(0,i.wtime-Ei),endMs:o.wtime+o.wduration+Vn})}return r.sort((n,s)=>n.startMs-s.startMs)}function Pi(e,t,r,n="neutral"){const s=it(n);if(t.length===0)return s;if(r.length===0||e<r[0].wtime)return it(t[0]?.emotion,s);const i=Ai(t,r);let o=null;for(const f of i)e>=f.startMs&&e<=f.endMs&&(o=f);if(o)return it(o.sentence.emotion,s);const a=r[r.length-1],l=a.wtime+a.wduration;return e>l+Vn?it(t[t.length-1]?.emotion,s):s}function Ar(e,t,r,n,s=Fn){const i=new OffscreenCanvas(r,n),o=i.getContext("2d",{willReadFrequently:!0});o.drawImage(e,0,0,r,n);const a=o.getImageData(0,0,r,n),l=o.createImageData(r,n);l.data.set(a.data);const c=new OffscreenCanvas(r,n).getContext("2d",{willReadFrequently:!0});for(const u of t){c.clearRect(0,0,r,n),c.drawImage(u,0,0,r,n);const d=c.getImageData(0,0,r,n);for(let w=0;w<l.data.length;w+=4){const T=d.data[w],h=d.data[w+1],I=d.data[w+2];Math.max(T,h,I)>s&&(l.data[w]=T,l.data[w+1]=h,l.data[w+2]=I,l.data[w+3]=255)}}return o.putImageData(l,0,0),i}let Ge=null,ot=null;const Ut=new Map;function xi(){Ge=null,ot=null,Ut.clear()}pn(xi);function Mi(e){return e??ot??void 0}function Pr(e){return new Promise((t,r)=>{const n=new Image;n.decoding="async",n.onload=()=>t(n),n.onerror=()=>r(new Error(`Failed to load image: ${e}`)),n.src=e})}async function Ri(e){return Tt()?Zs(await ct(Nt(),e??"")):Pr(Nt())}async function Yr(e,t,r,n){const s=r?await Bn(e,n):await fetch(e).then(f=>{if(!f.ok)throw new Error(`Failed to load ${e}`);return f.json()}),i=s.sheets?.[0];if(!i?.path)throw new Error(`${e} has no sheets[0].path`);const o=r?await Js(await ct(On(t,i.path,r),n??"")):await Pr(`${t}/${i.path}`),a=new Map;for(const f of s.cells??[])f.path&&a.set(f.path,f);const l=new Map;for(const f of s.sheets??[])l.set(f.index,f);return{atlas:o,atlasMeta:s,cellByPath:a,sheetByIndex:l,diffBase:t}}async function cr(e){const t=Mi(e),r=Tt();if(r&&!t)throw new Error("Encrypted assets enabled but no key provided");if(Ge&&(!r||ot===(t??null))){const i=await Ge;return{...ut(i),visemePaths:[...i.viseme.cellByPath.keys()]}}ot=t??null,Ut.clear();const n=wn();Ge=(async()=>{const i=await Ri(t),o=await Yr(gn(),n,r,t);let a=null;try{a=await Yr(yn(),n,r,t)}catch{a=null}return{sil:i,viseme:o,expression:a}})();const s=await Ge;return{...ut(s),visemePaths:[...s.viseme.cellByPath.keys()]}}async function xr(e){if(await cr(e),!Ge)throw new Error("Assets not loaded");return Ge}function ut(e){return Rn(e.viseme.atlasMeta,e.sil.naturalWidth,e.sil.naturalHeight)}function Ci(e,t){const r=document.createElement("canvas");return r.width=t.w,r.height=t.h,r.getContext("2d",{willReadFrequently:!0}).drawImage(e.atlas,t.x,t.y,t.w,t.h,0,0,t.w,t.h),r}async function Vt(e,t,r){const n=`${t.diffBase}::${r}`,s=Ut.get(n);if(s)return s;const i=(async()=>{const o=t.cellByPath.get(r);if(!o)throw new Error(`No atlas cell for path: ${r}`);const a=Cn(t.atlasMeta,o),l=ut(e);if(Dn(o)){const u=Ci(t,o),d=document.createElement("canvas");d.width=l.width,d.height=l.height;const w=d.getContext("2d");return w.fillStyle="#000",w.fillRect(0,0,l.width,l.height),w.drawImage(u,a.x,a.y),d}const f=`${t.diffBase}/${r}`,c=await Pr(f);if(c.naturalWidth===l.width&&c.naturalHeight===l.height)return c;throw new Error(`Diff PNG wrong size for ${r}`)})();return Ut.set(n,i),i}async function tr(e,t,r,n,s,i){if(s){const{width:w,height:T}=s;if(i&&!i()||t.readyState<HTMLMediaElement.HAVE_CURRENT_DATA)return;const h=document.createElement("canvas");h.width=w,h.height=T;const I=h.getContext("2d");if(I.clearRect(0,0,w,T),I.drawImage(t,0,0,w,T),i&&!i())return;(e.width!==w||e.height!==T)&&(e.width=w,e.height=T);const b=e.getContext("2d");b.clearRect(0,0,w,T),b.drawImage(h,0,0);return}const o=await xr(),a=Kt(o.viseme.atlasMeta),{width:l,height:f}=ut(o);(e.width!==l||e.height!==f)&&(e.width=l,e.height=f);const c=[],u=e.getContext("2d");if(u.clearRect(0,0,l,f),t.readyState<HTMLMediaElement.HAVE_CURRENT_DATA){const w=o.sil;u.drawImage(w,0,0,l,f);return}if(c.length===0){u.drawImage(t,0,0,l,f);return}const d=Ar(t,c,l,f,a);u.drawImage(d,0,0)}async function lr(e,t,r){const n=await xr(ot??void 0),s=Kt(n.viseme.atlasMeta),{width:i,height:o}=ut(n);(e.width!==i||e.height!==o)&&(e.width=i,e.height=o);const a=[];if(t.mouthPath){const c=new Set(n.viseme.cellByPath.keys()),u=br(t.mouthPath,c)??(c.has(t.mouthPath)?t.mouthPath:null);u&&a.push(await Vt(n,n.viseme,u))}t.expressionPath&&n.expression&&a.push(await Vt(n,n.expression,t.expressionPath));const l=e.getContext("2d");if(l.clearRect(0,0,i,o),a.length===0){l.drawImage(n.sil,0,0,i,o);return}const f=Ar(n.sil,a,i,o,s);l.drawImage(f,0,0)}async function Di(e,t){const r=await xr(ot??void 0),n=Kt(r.viseme.atlasMeta),{width:s,height:i}=ut(r),o=[];if(e.mouthPath){const c=new Set(r.viseme.cellByPath.keys()),u=br(e.mouthPath,c)??(c.has(e.mouthPath)?e.mouthPath:null);u&&o.push(await Vt(r,r.viseme,u))}e.expressionPath&&r.expression&&o.push(await Vt(r,r.expression,e.expressionPath));const a=new OffscreenCanvas(s,i),l=a.getContext("2d");if(o.length===0)return l.drawImage(r.sil,0,0,s,i),a.transferToImageBitmap();const f=Ar(r.sil,o,s,i,n);return l.drawImage(f,0,0),a.transferToImageBitmap()}async function Bi(e){await lr(e,{mouthPath:null,expressionPath:null})}let Wn=null;function Xr(e){Wn=e}function Ve(){return Wn}function Fi(e){return`${e.from}→${e.to}@${e.fromVtime}`}function Jr(){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 Li(){let e=Jr();const t=new Set,r=new Set,n=new Set;let s="",i=-1,o="";const a=()=>{e=Jr(),t.clear(),r.clear(),n.clear(),s="",i=-1,o=""},l=c=>{t.add(c)},f=c=>{c&&n.add(c)};return{reset:a,onRafTickSpeaking(){e.rafTicksSpeaking+=1},onDrawDeduped(){e.rafTicksDeduped+=1},onDrawRequested(){e.drawsRequested+=1},onQueueSuperseded(){e.queueSuperseded+=1},onWorkerStale(){e.workerStale+=1},onTransitionSample(c,u){const d=Fi(c);e.transitionFramesSampled+=1,d!==s&&(s=d,i=-1,o!==d&&(o=d,e.rifeFramesOmittedByGap+=c.omittedByGap)),u&&r.add(u),i>=0&&c.frameIdx>i+1&&(e.transitionFramesMissed+=c.frameIdx-i-1),i=c.frameIdx},onBufferTick(){e.bufferTicks+=1},onBufferHit(){e.bufferHits+=1},onBufferUnderrun(){e.bufferUnderruns+=1},onBufferPrefetched(){e.bufferPrefetched+=1},onBufferReset(){},onDrawDisplayed(c,u){if(e.drawsDisplayed+=1,c.transitionMeta){e.transitionFramesDisplayed+=1;const w=c.transitionMeta;l(w.fromVtime),l(w.toVtime),f(c.mouthPath);return}if(c.label.includes("→")){e.transitionFramesDisplayed+=1;return}const d=c.label.trim();if(!(!d||d==="sil")){for(const w of u)if(w.viseme===d&&c.elapsedMs>=w.vtime&&c.elapsedMs<w.vtime+w.vduration){l(w.vtime);break}}},snapshot(c,u){let d=0,w=0;for(const x of c)if(x.viseme!=="sil"){if(x.vtime>=u)break;d+=1,t.has(x.vtime)&&(w+=1)}const T=d>0?Math.round(w/d*100):100,h=e.queueSuperseded+e.workerStale+Math.max(0,e.drawsRequested-e.drawsDisplayed),I=d-w,b=e.bufferTicks>0?Math.round(e.bufferHits/e.bufferTicks*100):100;return{...e,bufferHitRate:b,renderLoss:h,visemeSlotsTotal:d,visemeSlotsCovered:w,visemeCoveragePct:T,transitionFramesUniqueSampled:r.size,transitionFramesUniquePainted:n.size,drawsDeduped:e.rafTicksDeduped,renderSkips:e.rafTicksDeduped+h,visemesShown:w,visemesSkipped:I,rifeTransitionFramesSkipped:e.transitionFramesMissed}}}}const Hn=hr,Oi=200,Qr=300,Ni=100,Ui=600,$n="__sil__";function Zr(e){return e.lastDrawnKey?e.lastDrawnKey.split("|")[0]===$n:!1}function Mr(){return{lastDrawnKey:""}}function Oe(e){e.lastDrawnKey="",e.transitionCache=void 0}function Vi(e,t){if(t?.transitionCache&&t.transitionCache.queueLength===e.length)return t.transitionCache.transitions;const r=Bs(e);return t&&(t.transitionCache={queueLength:e.length,transitions:r}),r}function Wi(e,t){let r="sil";for(const n of e)n.viseme!=="sil"&&(t!=null&&n.vtime>t||(r=n.viseme));return r}function Hi(e,t,r,n=120){if(r!=="sil")return r;const s=Pn(e,t,n);return s!=="sil"?s:Wi(e,t)}function $i(e,t){return e==="sil"?!1:t?si(e,"sil",t):kr(e,"sil").length>0}function qi(){return Hn+Ui}function Wt(e){return e??$n}function Rr(e,t,r,n,s=Ni){if(e.length===0)return{diffPath:null,label:"sil"};if(t>=Ke(e))return{diffPath:null,label:"sil"};const i=Vi(e,n),o=Fs(i,t);if(o){const l=En(o.fromVtime,o.toVtime,o.from,o.to);if(t>=l.transStart&&t<l.transEnd){let f=kr(o.from,o.to);r&&(f=Ln(f,r));const c=ai(f,l.effectiveGapMs);if(c.length>0){const u=oi(t,l.transStart,l.transEnd,c.length);return{diffPath:c[u]??c[c.length-1],label:`${o.from}→${o.to}`,transitionMeta:{from:o.from,to:o.to,fromVtime:o.fromVtime,toVtime:o.toVtime,frameIdx:u,frameCount:c.length,fullFrameCount:f.length,omittedByGap:Math.max(0,f.length-c.length)}}}return{diffPath:St(o.to),label:o.to}}}const a=Pn(e,t,s);return a==="sil"?{diffPath:null,label:"sil"}:{diffPath:St(a),label:a}}function Cr(e,t){const r=t??Wt(e.expressionPath);return`${Wt(e.mouthPath)}|${r}`}function en(e,t,r,n,s,i,o,a,l,f){const c=Rr(t,r,l,i),u={mouthPath:c.diffPath,expressionPath:n},d=Cr(u,s),w=Ve();if(c.transitionMeta&&w?.onTransitionSample(c.transitionMeta,c.diffPath),d===i.lastDrawnKey){w?.onDrawDeduped();return}w?.onDrawRequested(),a(c),i.lastDrawnKey=d,o(e,u,d,T=>{T===d&&(i.lastDrawnKey=d)})}function tn(e,t,r,n,s,i,o=null){const a={mouthPath:null,expressionPath:t},l=o!=null?`|idle@${Math.round(o*1e3)}`:"",f=Cr(a,r)+l;!(o!=null)&&f===n.lastDrawnKey||(i(o!=null?"idle":"sil"),s(e,a,f,u=>{u===f&&(n.lastDrawnKey=f)}))}function qn(e,t,r,n,s=_t,i){if(r<=t||e.length===0)return[];const o=i??Mr(),a=[];let l="";for(let f=t;f<r;f+=s){const c=Rr(e,f,n,o),u=Wt(c.diffPath);u!==l&&(l=u,a.push({displayAtMs:f,mouthPath:c.diffPath,mouthKey:u,label:c.label}))}return a}const Ki=3;function Dr(e){let t=0;const r=[];let n=!1,s=!1;const i=async()=>{if(!n){n=!0;try{for(;r.length>0;){const a=r.shift(),l=++t;await e(a.diffs,l,a)&&l===t&&a.onDisplayed?.(a.drawKey)}}finally{n=!1,r.length>0&&i()}}},o=()=>{s||(s=!0,queueMicrotask(()=>{s=!1,i()}))};return{enqueue(a,l,f){const c=r.findIndex(u=>u.drawKey===l);c>=0?r[c]={diffs:a,drawKey:l,onDisplayed:f}:(r.length>=Ki&&(r.shift(),Ve()?.onQueueSuperseded()),r.push({diffs:a,drawKey:l,onDisplayed:f})),o()},reset(){t+=1,r.length=0},isLatestSeq(a){return a===t},getLatestSeq(){return t}}}function Gi(e,t=2){const r=[],n=new Set;let s=0,i=!1,o=0;const a=(f,c)=>`${f}|${c}`,l=async()=>{if(!i){i=!0;try{for(;s<t&&r.length>0;){const f=r.shift(),c=a(f.displayAtMs,f.drawKey);if(n.has(c))continue;n.add(c),s+=1;const u=o;try{await e(f)}finally{if(n.delete(c),s-=1,u!==o)return}}}finally{i=!1,r.length>0&&s<t&&l()}}};return{enqueue(f){const c=a(f.displayAtMs,f.drawKey);n.has(c)||r.some(u=>a(u.displayAtMs,u.drawKey)===c)||(r.push(f),l())},isQueuedOrInflight(f,c){const u=a(f,c);return n.has(u)?!0:r.some(d=>a(d.displayAtMs,d.drawKey)===u)},reset(){o+=1,r.length=0,n.clear(),s=0},pendingCount(){return r.length+s}}}function zi(){const e=new Map,t=r=>{r.bitmap.close()};return{has(r,n){return e.has(n)},markRendered(r,n,s,i,o){const a=e.get(n);a&&t(a),e.set(n,{displayAtMs:r,mouthKey:n,label:s,mouthPath:i,bitmap:o})},findForMouthKey(r){return e.get(r)??null},removeThrough(r){},pruneBefore(r){},reset(){for(const r of e.values())t(r);e.clear()},size(){return e.size}}}function ji(e,t,r,n,s,i){const o=qn(t,r,n,s,_t,i);if(o.length===0)return{ready:0,total:0};let a=0;for(const l of o)e.has(l.displayAtMs,l.mouthKey)&&(a+=1);return{ready:a,total:o.length}}function Yi(e){const t=zi(),r=Mr(),n=new Set;let s="",i=null;const o=Gi(async u=>{try{if(!e.supportsBufferedPlayback)return;const d=await e.compositePrefetchFrame(u.diffs,u.mouthKey);if(!d)return;t.markRendered(u.displayAtMs,u.mouthKey,u.label,u.mouthPath,d),Ve()?.onBufferPrefetched()}finally{n.delete(u.mouthKey)}},as),a=u=>{for(const d of u)t.has(d.displayAtMs,d.mouthKey)||n.has(d.mouthKey)||o.isQueuedOrInflight(0,d.mouthKey)||(n.add(d.mouthKey),o.enqueue({...d,displayAtMs:0,drawKey:d.mouthKey,diffs:{mouthPath:d.mouthPath,expressionPath:null}}))},l=(u,d,w,T=Qt)=>{if(!e.supportsBufferedPlayback)return;const h=Math.max(0,d-_t*4),I=qn(u,h,d+T,w,_t,r);a(I)},f=(u,d)=>{s=u,i=d},c=(u,d)=>!(s!==u||i!==d);return{reset(){t.reset(),o.reset(),n.clear(),Oe(r),s="",i=null,Ve()?.onBufferReset()},markExternallyDisplayed(u,d){f(u.split("|")[0]??u,d)},warmupTick(u){e.supportsBufferedPlayback&&l(u.queue,0,u.availablePaths,Math.max(Nr,Qt))},isWarmupReady(u,d=Nr){if(!e.supportsBufferedPlayback)return!0;const{ready:w,total:T}=ji(t,u.queue,0,d,u.availablePaths,r);return T===0?!1:w/T>=is},getBufferedFrameCount:()=>t.size(),tick(u){const d=Ve();d?.onBufferTick(),l(u.queue,u.elapsedMs,u.availablePaths),t.pruneBefore(u.elapsedMs-Qt);const w=Rr(u.queue,u.elapsedMs,u.availablePaths,r),T=Wt(w.diffPath),h=Cr({mouthPath:w.diffPath,expressionPath:u.expressionPath},u.expressionDrawKey),I=()=>(f(T,u.expressionPath),d?.onBufferHit(),{usedBuffer:!0,underrun:!1,held:!0});if(c(T,u.expressionPath)||u.playbackState.lastDrawnKey===h)return I();const b=t.findForMouthKey(T,u.elapsedMs,_t);return b&&u.expressionPath!=null?(d?.onBufferHit(),{usedBuffer:!1,underrun:!1,held:!1}):b?(e.blitBitmap(u.canvas,b.bitmap),f(b.mouthKey,u.expressionPath),u.playbackState.lastDrawnKey=h,u.onStatus({diffPath:b.mouthPath,label:b.label}),u.onDisplayed(h,b.mouthPath,b.label),d?.onBufferHit(),{usedBuffer:!0,underrun:!1,held:!1}):(d?.onBufferUnderrun(),{usedBuffer:!1,underrun:!0,held:!1})}}}const Xi=`
|
|
5
|
+
`);Ve=Ae.pop()??"";for(const Ie of Ae)await ce(Ie);if(it){Ve.trim()&&await ce(Ve.trim()),_e===Y&&!ue.signal.aborted&&g&&V.markStreamComplete(r);break}}}catch(Be){if(ue.signal.aborted||!g)return;console.error("[realtimeVoice] avatar_speak failed:",Be),e("error"),B()}finally{D===ue&&(D=null)}},C=m=>{const P=Qs(m);if(!P){console.warn("[realtimeVoice] server message with no payload",m);return}const J=String(P.type??"unknown");switch(J!=="avatar_metadata"&&J!=="avatar_audio_chunk"&&J!=="avatar_lipsync"&&J!=="avatar_interrupt"&&J!=="error"&&console.debug("[realtimeVoice] server message",J,P),J){case"avatar_metadata":G(P);break;case"avatar_audio_chunk":H(P);break;case"avatar_lipsync":p(P);break;case"avatar_interrupt":Se(P);break;case"error":{const ue=String(P.message??"Voice session error");console.error("[realtimeVoice] server error:",ue),e("error"),B();break}case"avatar_speak":S(P);break}},le=()=>u||(c=new js,u=new es.PipecatClient({transport:new _r.WebSocketTransport({mediaManager:c,serializer:new zs,recorderSampleRate:Ar,playerSampleRate:xr}),enableCam:!1,enableMic:!0,callbacks:{onBotReady:()=>{f=!0,e("idle"),B()},onDisconnected:()=>{f=!1,I()},onError:m=>{const P=typeof m=="object"&&m!==null&&"data"in m?String(m.data??m):String(m);console.error("[realtimeVoice]",P,m),e("error"),B()},onTransportStateChanged:m=>{m==="error"&&(e("error"),B())},onServerMessage:m=>{X(m)},onUserStartedSpeaking:()=>{},onBotStartedSpeaking:()=>{Q(),ae("bot_started")},onBotStoppedSpeaking:()=>{g&&(M(),k=setTimeout(()=>{k=null,g&&(V.markStreamComplete(r),ae("bot_stopped"))},450))},onUserTranscript:m=>{String(m.text??""),d.onUserTranscript?.(m.text,!!m.final)},onBotOutput:m=>{d.onBotOutput?.(m.text)}}}),u);return{setDeveloperToken:m=>{},setTtsEngineId:m=>{l=m,V.setStreamTtsEngineId(m)},getTtsEngineId:()=>l,speak:async(m,P)=>{const J=m.trim();if(J){if(!u||!f)throw new Error("Realtime voice is not connected. Call connect() first.");y(),await u.sendText(J)}},stop:()=>{D?.abort(),D=null,Y+=1,Q(),y(),e("idle"),B()},connect:async m=>{if(m&&(d={...d,...m}),!d.authToken?.trim())throw new Error("Voice connect requires authToken (same JWT as SSE Authorization Bearer).");e("loading");const P=le();await P.initDevices();const J=Cn(d);console.info("[realtimeVoice] connecting (avatar_audio_chunk RTVI):",J);try{await P.connect({wsUrl:J})}catch(ue){f=!1,e("error"),B();const _e=ue instanceof Error?ue.message:"Realtime voice connect failed (WebSocket 101 only means upgrade OK — check authToken/origin/RTVI handshake)";throw new Error(`${_e} — ${J}`)}f=!0,e("idle"),B()},disconnect:async()=>{if(M(),O=!1,A.length=0,v=!1,D?.abort(),D=null,Y+=1,Q(),u)try{await u.disconnect()}catch(m){console.error("[realtimeVoice] disconnect failed:",m)}f=!1,I()},isConnected:()=>f,unlockAudio:async()=>{await V.primeAudioContext()},getVisemeQueue:()=>r,getWordQueue:()=>n,getSentenceEmotions:()=>s,getStreamMood:()=>a,getPlaybackElapsedMs:z,isSpeaking:()=>V.isSpeaking(),isWarmingUp:()=>V.isWarmingUp(),releaseWarmupHold:()=>V.releaseWarmupHold(),setLipsyncWarmupHold:(m,P)=>{P&&(ee=P),V.setLipsyncWarmupHoldEnabled(m)},getVoiceStream:()=>V.getVoiceStream(),setPlaybackSpeed:m=>{V.setPlaybackSpeed(m)},getPlaybackSpeed:()=>V.getPlaybackSpeed(),subscribe:m=>(h.add(m),()=>h.delete(m))}}function Bn(e,t,r){return{width:e.crop?.source_width??t,height:e.crop?.source_height??r}}function Fn(e,t){const r=e.crop;return{x:t?.source_x??r?.x??0,y:t?.source_y??r?.y??0,width:t?.source_w??r?.width??e.frame_width??0,height:t?.source_h??r?.height??e.frame_height??0}}function Ln(e){return e?(e.w??0)>0&&(e.h??0)>0:!1}async function pt(e,t){const r=await fetch(e);if(!r.ok)throw new Error(`Failed to load encrypted asset: ${e}`);const n=await r.arrayBuffer();return Ts(n,t)}async function On(e,t){if(!t)throw new Error("Missing asset key for encrypted JSON");const r=await pt(e,t),n=new TextDecoder().decode(r);return JSON.parse(n)}async function ei(e){const t=new Blob([e],{type:"image/webp"});return createImageBitmap(t)}async function ti(e){const t=new Blob([e],{type:"image/webp"}),r=URL.createObjectURL(t);return new Promise((n,s)=>{const i=new Image;i.decoding="async",i.onload=()=>{URL.revokeObjectURL(r),n(i)},i.onerror=()=>{URL.revokeObjectURL(r),s(new Error("Failed to decode decrypted image"))},i.src=r})}async function ri(e){const t=new Blob([e],{type:"image/png"});return createImageBitmap(t)}async function ni(e){const t=new Blob([e],{type:"image/png"}),r=URL.createObjectURL(t);return new Promise((n,s)=>{const i=new Image;i.decoding="async",i.onload=()=>{URL.revokeObjectURL(r),n(i)},i.onerror=()=>{URL.revokeObjectURL(r),s(new Error("Failed to decode decrypted PNG"))},i.src=r})}function si(e){const t=new Blob([e],{type:"video/mp4"});return URL.createObjectURL(t)}async function ii(e){const t=Es();if(!kt())return t;if(!e)throw new Error("Missing asset key for encrypted idle video");const r=await pt(t,e);return si(r)}function ft(e,t){return`${e}/${t}_minus_sil.png`}const Nn=5;function Jt(e){const t=e?.composite_threshold;if(typeof t=="number"&&t>0)return t;const r=e?.crop?.content_threshold;return typeof r=="number"&&r>0?r:Nn}const ai=["aa","E","I","O","U","PP","FF","DD","SS","TH","CH","RR","kk","nn"],pr=["t01_0.25","t02_0.50","t03_0.75"],oi=pr.length,zr=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 jr(e,t){return`${e}__${t}`}function Yr(e,t){return`pairs/${e}/${t}_minus_sil.png`}function Tt(e){return`${e}_minus_sil.png`}function Pr(e,t){if(t.has(e))return e;const r=/^pairs\/([^/]+)\//.exec(e);if(!r)return null;const n=r[1].split("__")[1];if(!n||n==="sil")return null;const s=Tt(n);return t.has(s)?s:null}function Un(e,t){return e.filter(r=>t.has(r))}function ci(e,t,r){const n=Rr(e,t);return Un(n,r).length>0}function Rr(e,t){if(e===t)return[];const r=[],n=jr(e,t);if(zr.has(n)){for(const i of pr)r.push(Yr(n,i));return r}const s=jr(t,e);if(zr.has(s)){for(let i=oi-1;i>=0;i--)r.push(Yr(s,pr[i]));return r}return[]}function ui(e){return e<16?[1]:e<24?[1]:e<35?[0,1,2]:[0,1,2]}function li(e,t){return ui(t).map(n=>e[n]).filter(n=>n!=null)}function fi(e,t,r,n){if(n<=0)return 0;const i=Math.max(1,r-t)/n,o=e-t,a=Math.floor(o/i);return Math.min(n-1,Math.max(0,a))}function Vn(e,t,r){if(!r)return`${e}/${t}`;const n=t.split("/").pop()??t,s=n.includes(".")?n.slice(0,n.lastIndexOf(".")):n;return`${e}/${s}.bin`}const di=[.32,.24,.16,.08,0],mi=di.map(e=>`eyes_${e.toFixed(2)}`),hi=["eyes_0.32","eyes_0.16","eyes_0.00"],pi=["eyes_0.32","eyes_0.16","eyes_0.08","eyes_0.00"],De=2,wi=-30,qt=20,Wn={fear:-20,anger:-20,sad:-10,neutral:0,love:0,happy:0,disgust:10};function bt(e){return`brow_${e}`}function et(e){return Wn[e]}function Oe(e){return Math.max(wi,Math.min(qt,e))}function gi(e,t){if(e===t)return[];const r=e<t,n=r?De:-De,s=[];let i=e;if(i%De!==0){const o=r?Math.ceil(i/De)*De:Math.floor(i/De)*De,a=Oe(o);if(r?a>=t:a<=t)return[t];i=a,s.push(i)}for(;;){const o=i+n;if(r?o>=t:o<=t){t!==i&&s.push(t);break}i=o,s.push(i)}return s}function Xr(e){const t=Oe(e);if(t%De===0)return[t];const r=Math.floor(t/De)*De,n=Math.min(qt,r+De),s=Oe(r);return t-s<=n-t?s===n?[s]:[s,n]:s===n?[n]:[n,s]}const yi=["fear","anger","sad","neutral","love","happy","disgust"],_i={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 Ke(e,t="neutral"){const r=String(e??"").trim().toLowerCase();return r?_i[r]??t:t}function $n(e){return e===0?null:ft(bt(e),"eyes_standard")}function Hn(e){return $n(et(e))}const Si={[-20]:"anger",[-10]:"sad",0:"neutral",10:"disgust"};function Mr(e,t){if(!e)return null;if(t(e))return e;const r=e.match(/^brow_(-?\d+)\/(eyes_[^/]+)_minus_sil\.png$/);if(r){const s=Number(r[1]),i=r[2];for(const a of Xr(s)){if(a===s)continue;const l=ft(bt(a),i);if(t(l))return l}const o=Si[s];if(o){const a=ft(o,i);if(t(a))return a}return null}const n=e.match(/^(fear|anger|sad|neutral|love|happy|disgust)\/(eyes_[^/]+)_minus_sil\.png$/);if(n){const s=n[1],i=n[2],o=et(s);for(const a of[o,...Xr(o)]){const l=ft(bt(a),i);if(t(l))return l}}return null}function Jr(e,t){const r=bt(et(e));return!t||t==="eyes_standard"?Hn(e):e==="neutral"&&t==="eyes_standard"?null:ft(r,t)}function vi(e,t){const r=[];for(const n of t)r.push(Jr(e,n));for(let n=t.length-2;n>=0;n--)r.push(Jr(e,t[n]));return r.push(Hn(e)),r}function Ti(e){return ft(bt(e),"eyes_standard")}function bi(e,t){return gi(e,t).map(Ti)}const Ei=6;function ki(e,t){for(const r of e)if(t>=r.wtime&&t<r.wtime+r.wduration)return r;return null}function Ii(e){const t=Oe(e);return t>=qt?qt:Oe(t+Ei)}function Ai(e,t,r){const n=Oe(r);return ki(t,e)?.emphasis?Ii(n):n}function Qr(e){if(!e)return 0;const t=e.match(/^brow_(-?\d+)\//);if(!t?.[1])return null;const r=Number.parseInt(t[1],10);return Number.isFinite(r)?Oe(r):null}function lt(e,t){return e+Math.random()*(t-e)}function Nt(){return lt(14,32)}function xi(e={}){const{initialDelayMs:t=800+Math.random()*1200,intervalMinMs:r=2200,intervalMaxMs:n=5800,doubleBlinkChance:s=.18,doubleBlinkGapMinMs:i=120,doubleBlinkGapMaxMs:o=220,fullBlinkChance:a=.75,postEmotionChangeDelayMs:l=380,minDwellMs:f=550}=e;let u="neutral",c="neutral",d=0,w=0,b=0,h={kind:"idle",nextBlinkAt:performance.now()+t},A=null,v="__eyes_open__|neutral|brow:0";function R(k=u){v=`${A??"__eyes_open__"}|${k}|brow:${w}`}function g(k,x=u){A=k;const M=Qr(k);M!=null?w=M:k==null&&(w=0),R(x)}function E(k){return $n(k)}function F(k,x,M){const B=Oe(x),ee=Oe(M);if(d=ee,B===ee){g(E(ee));return}const V=[...bi(B,ee),E(ee)];h={kind:"browBlend",paths:V,index:0,holdUntil:k+Nt(),targetBrow:ee},g(V[0]??null)}function D(k,x){h={kind:"idle",nextBlinkAt:k+lt(r,n)},c=x,g(E(d),x)}function Y(k,x,M){const B=M?hi:Math.random()<a?mi:pi;h={kind:"playing",paths:vi(x,B),index:0,holdUntil:k+Nt(),after:"idle",blinkEmotion:x},c=x,g(h.paths[0]??null,x)}function O(k){if(h.kind==="browBlend"){for(;k>=h.holdUntil&&h.index<h.paths.length-1;)h.index+=1,h.holdUntil=k+Nt();const x=h.paths[h.index]??null;g(x),k>=h.holdUntil&&h.index>=h.paths.length-1&&(w=h.targetBrow,d=h.targetBrow,g(E(h.targetBrow)),h={kind:"idle",nextBlinkAt:k+l});return}if(h.kind==="idle"){g(E(d),c),k>=h.nextBlinkAt&&Y(k,c,!1);return}if(h.kind==="doublePause"){g(E(d),h.blinkEmotion),k>=h.resumeAt&&Y(k,h.blinkEmotion,!0);return}if(h.kind==="playing"){for(;k>=h.holdUntil&&h.index<h.paths.length-1;)h.index+=1,h.holdUntil=k+Nt();if(g(h.paths[h.index]??null,h.blinkEmotion),k>=h.holdUntil&&h.index>=h.paths.length-1){if(h.after==="resumeIdle"){D(k+l,u);return}if(h.after==="doublePause"){h={kind:"doublePause",resumeAt:k+lt(i,o),blinkEmotion:h.blinkEmotion},g(E(d),h.blinkEmotion);return}if(h.after==="idle"&&Math.random()<s){h={kind:"doublePause",resumeAt:k+lt(i,o),blinkEmotion:h.blinkEmotion},g(E(d),h.blinkEmotion);return}D(k,h.blinkEmotion)}}}return{advance:O,setTargetEmotion(k,x){const M=Ke(k,u);if(M===u)return;const B=performance.now();!x?.force&&f>0&&!(u==="neutral"&&M!=="neutral")&&B-b<f||(u=M,c=M,b=B)},setTargetEyebrow(k){const x=Oe(k);if(x===d)return;const M=performance.now(),B=h.kind==="browBlend"?Qr(A)??w:w;d=x,!(x===B&&h.kind!=="browBlend")&&(h.kind==="playing"||h.kind==="doublePause"||F(M,B,x))},snapToEmotion(k){const x=Ke(k),M=et(x);u=x,c=x,d=M,w=M,b=performance.now(),h={kind:"idle",nextBlinkAt:performance.now()+lt(4e3,7e3)},g(E(M),x)},getTargetEmotion:()=>u,getTargetEyebrow:()=>d,getExpressionPath:()=>A,getDrawKey:()=>v,reset(){u="neutral",c="neutral",d=0,w=0,b=0,D(performance.now()+lt(r,n),"neutral")}}}function Pi(){const e=document.createElement("video");e.muted=!0,e.playsInline=!0,e.preload="auto",e.setAttribute("playsinline",""),e.loop=!0;let t=!1,r=null,n="";const s=()=>{r&&(URL.revokeObjectURL(r),r=null)};return{async load(i){t&&n===i||(s(),t=!1,n=i,i.startsWith("blob:")&&(r=i),await new Promise((o,a)=>{const l=()=>{const f=e.duration;if(!Number.isFinite(f)||f<=0){a(new Error(`Idle video has invalid duration: ${i}`));return}t=!0,e.currentTime=0,o()};e.addEventListener("loadeddata",l,{once:!0}),e.addEventListener("error",()=>a(new Error(`Failed to load idle video: ${i}`)),{once:!0}),e.src=i,e.load()}))},isReady(){return t},getVideo(){return e},setActive(i){t&&(i?e.paused&&e.play().catch(()=>{}):e.paused||e.pause())},restart(){t&&(e.currentTime=0)},dispose(){e.pause(),e.removeAttribute("src"),e.load(),s(),t=!1,n=""}}}const Ri=50,qn=400;function Mi(e,t,r){return e.filter(n=>{const s=n.queueIndex??-1;return s>=t&&s<=r})}function Ci(e,t){const r=[];for(const n of e){const s=Mi(t,n.start_word,n.end_word);if(s.length===0)continue;const i=s[0],o=s[s.length-1];r.push({sentence:n,startMs:Math.max(0,i.wtime-Ri),endMs:o.wtime+o.wduration+qn})}return r.sort((n,s)=>n.startMs-s.startMs)}function Di(e,t,r,n="neutral"){const s=Ke(n);if(t.length===0)return s;if(r.length===0||e<r[0].wtime)return Ke(t[0]?.emotion,s);const i=Ci(t,r);let o=null;for(const f of i)e>=f.startMs&&e<=f.endMs&&(o=f);if(o)return Ke(o.sentence.emotion,s);const a=r[r.length-1],l=a.wtime+a.wduration;return e>l+qn?Ke(t[t.length-1]?.emotion,s):s}function Cr(e,t,r,n,s=Nn){const i=new OffscreenCanvas(r,n),o=i.getContext("2d",{willReadFrequently:!0});o.drawImage(e,0,0,r,n);const a=o.getImageData(0,0,r,n),l=o.createImageData(r,n);l.data.set(a.data);const u=new OffscreenCanvas(r,n).getContext("2d",{willReadFrequently:!0});for(const c of t){u.clearRect(0,0,r,n),u.drawImage(c,0,0,r,n);const d=u.getImageData(0,0,r,n);for(let w=0;w<l.data.length;w+=4){const b=d.data[w],h=d.data[w+1],A=d.data[w+2];Math.max(b,h,A)>s&&(l.data[w]=b,l.data[w+1]=h,l.data[w+2]=A,l.data[w+3]=255)}}return o.putImageData(l,0,0),i}let Qe=null,mt=null;const Kt=new Map;function Bi(){Qe=null,mt=null,Kt.clear()}yn(Bi);function Fi(e){return e??mt??void 0}function Dr(e){return new Promise((t,r)=>{const n=new Image;n.decoding="async",n.onload=()=>t(n),n.onerror=()=>r(new Error(`Failed to load image: ${e}`)),n.src=e})}async function Li(e){return kt()?ni(await pt(Ht(),e??"")):Dr(Ht())}async function Zr(e,t,r,n){const s=r?await On(e,n):await fetch(e).then(f=>{if(!f.ok)throw new Error(`Failed to load ${e}`);return f.json()}),i=s.sheets?.[0];if(!i?.path)throw new Error(`${e} has no sheets[0].path`);const o=r?await ti(await pt(Vn(t,i.path,r),n??"")):await Dr(`${t}/${i.path}`),a=new Map;for(const f of s.cells??[])f.path&&a.set(f.path,f);const l=new Map;for(const f of s.sheets??[])l.set(f.index,f);return{atlas:o,atlasMeta:s,cellByPath:a,sheetByIndex:l,diffBase:t}}async function wr(e){const t=Fi(e),r=kt();if(r&&!t)throw new Error("Encrypted assets enabled but no key provided");if(Qe&&(!r||mt===(t??null))){const i=await Qe;return{...ht(i),visemePaths:[...i.viseme.cellByPath.keys()]}}mt=t??null,Kt.clear();const n=_n();Qe=(async()=>{const i=await Li(t),o=await Zr(Sn(),n,r,t);let a=null;try{a=await Zr(vn(),n,r,t)}catch{a=null}return{sil:i,viseme:o,expression:a}})();const s=await Qe;return{...ht(s),visemePaths:[...s.viseme.cellByPath.keys()]}}async function Br(e){if(await wr(e),!Qe)throw new Error("Assets not loaded");return Qe}function ht(e){return Bn(e.viseme.atlasMeta,e.sil.naturalWidth,e.sil.naturalHeight)}function Oi(e,t){const r=document.createElement("canvas");return r.width=t.w,r.height=t.h,r.getContext("2d",{willReadFrequently:!0}).drawImage(e.atlas,t.x,t.y,t.w,t.h,0,0,t.w,t.h),r}async function Gt(e,t,r){const n=`${t.diffBase}::${r}`,s=Kt.get(n);if(s)return s;const i=(async()=>{const o=t.cellByPath.get(r);if(!o)throw new Error(`No atlas cell for path: ${r}`);const a=Fn(t.atlasMeta,o),l=ht(e);if(Ln(o)){const c=Oi(t,o),d=document.createElement("canvas");d.width=l.width,d.height=l.height;const w=d.getContext("2d");return w.fillStyle="#000",w.fillRect(0,0,l.width,l.height),w.drawImage(c,a.x,a.y),d}const f=`${t.diffBase}/${r}`,u=await Dr(f);if(u.naturalWidth===l.width&&u.naturalHeight===l.height)return u;throw new Error(`Diff PNG wrong size for ${r}`)})();return Kt.set(n,i),i}async function cr(e,t,r,n,s,i){if(s){const{width:w,height:b}=s;if(i&&!i()||t.readyState<HTMLMediaElement.HAVE_CURRENT_DATA)return;const h=document.createElement("canvas");h.width=w,h.height=b;const A=h.getContext("2d");if(A.clearRect(0,0,w,b),A.drawImage(t,0,0,w,b),i&&!i())return;(e.width!==w||e.height!==b)&&(e.width=w,e.height=b);const v=e.getContext("2d");v.clearRect(0,0,w,b),v.drawImage(h,0,0);return}const o=await Br(),a=Jt(o.viseme.atlasMeta),{width:l,height:f}=ht(o);(e.width!==l||e.height!==f)&&(e.width=l,e.height=f);const u=[],c=e.getContext("2d");if(c.clearRect(0,0,l,f),t.readyState<HTMLMediaElement.HAVE_CURRENT_DATA){const w=o.sil;c.drawImage(w,0,0,l,f);return}if(u.length===0){c.drawImage(t,0,0,l,f);return}const d=Cr(t,u,l,f,a);c.drawImage(d,0,0)}async function gr(e,t,r){const n=await Br(mt??void 0),s=Jt(n.viseme.atlasMeta),{width:i,height:o}=ht(n);(e.width!==i||e.height!==o)&&(e.width=i,e.height=o);const a=[];if(t.mouthPath){const u=new Set(n.viseme.cellByPath.keys()),c=Pr(t.mouthPath,u)??(u.has(t.mouthPath)?t.mouthPath:null);c&&a.push(await Gt(n,n.viseme,c))}if(t.expressionPath&&n.expression){const u=Mr(t.expressionPath,c=>n.expression.cellByPath.has(c));u&&a.push(await Gt(n,n.expression,u))}const l=e.getContext("2d");if(l.clearRect(0,0,i,o),a.length===0){l.drawImage(n.sil,0,0,i,o);return}const f=Cr(n.sil,a,i,o,s);l.drawImage(f,0,0)}async function Ni(e,t){const r=await Br(mt??void 0),n=Jt(r.viseme.atlasMeta),{width:s,height:i}=ht(r),o=[];if(e.mouthPath){const u=new Set(r.viseme.cellByPath.keys()),c=Pr(e.mouthPath,u)??(u.has(e.mouthPath)?e.mouthPath:null);c&&o.push(await Gt(r,r.viseme,c))}if(e.expressionPath&&r.expression){const u=Mr(e.expressionPath,c=>r.expression.cellByPath.has(c));u&&o.push(await Gt(r,r.expression,u))}const a=new OffscreenCanvas(s,i),l=a.getContext("2d");if(o.length===0)return l.drawImage(r.sil,0,0,s,i),a.transferToImageBitmap();const f=Cr(r.sil,o,s,i,n);return l.drawImage(f,0,0),a.transferToImageBitmap()}async function Ui(e){await gr(e,{mouthPath:null,expressionPath:null})}let Kn=null;function en(e){Kn=e}function Ye(){return Kn}function Vi(e){return`${e.from}→${e.to}@${e.fromVtime}`}function tn(){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 Wi(){let e=tn();const t=new Set,r=new Set,n=new Set;let s="",i=-1,o="";const a=()=>{e=tn(),t.clear(),r.clear(),n.clear(),s="",i=-1,o=""},l=u=>{t.add(u)},f=u=>{u&&n.add(u)};return{reset:a,onRafTickSpeaking(){e.rafTicksSpeaking+=1},onDrawDeduped(){e.rafTicksDeduped+=1},onDrawRequested(){e.drawsRequested+=1},onQueueSuperseded(){e.queueSuperseded+=1},onWorkerStale(){e.workerStale+=1},onTransitionSample(u,c){const d=Vi(u);e.transitionFramesSampled+=1,d!==s&&(s=d,i=-1,o!==d&&(o=d,e.rifeFramesOmittedByGap+=u.omittedByGap)),c&&r.add(c),i>=0&&u.frameIdx>i+1&&(e.transitionFramesMissed+=u.frameIdx-i-1),i=u.frameIdx},onBufferTick(){e.bufferTicks+=1},onBufferHit(){e.bufferHits+=1},onBufferUnderrun(){e.bufferUnderruns+=1},onBufferPrefetched(){e.bufferPrefetched+=1},onBufferReset(){},onDrawDisplayed(u,c){if(e.drawsDisplayed+=1,u.transitionMeta){e.transitionFramesDisplayed+=1;const w=u.transitionMeta;l(w.fromVtime),l(w.toVtime),f(u.mouthPath);return}if(u.label.includes("→")){e.transitionFramesDisplayed+=1;return}const d=u.label.trim();if(!(!d||d==="sil")){for(const w of c)if(w.viseme===d&&u.elapsedMs>=w.vtime&&u.elapsedMs<w.vtime+w.vduration){l(w.vtime);break}}},snapshot(u,c){let d=0,w=0;for(const R of u)if(R.viseme!=="sil"){if(R.vtime>=c)break;d+=1,t.has(R.vtime)&&(w+=1)}const b=d>0?Math.round(w/d*100):100,h=e.queueSuperseded+e.workerStale+Math.max(0,e.drawsRequested-e.drawsDisplayed),A=d-w,v=e.bufferTicks>0?Math.round(e.bufferHits/e.bufferTicks*100):100;return{...e,bufferHitRate:v,renderLoss:h,visemeSlotsTotal:d,visemeSlotsCovered:w,visemeCoveragePct:b,transitionFramesUniqueSampled:r.size,transitionFramesUniquePainted:n.size,drawsDeduped:e.rafTicksDeduped,renderSkips:e.rafTicksDeduped+h,visemesShown:w,visemesSkipped:A,rifeTransitionFramesSkipped:e.transitionFramesMissed}}}}const Gn=vr,$i=200,rn=300,Hi=100,qi=600,zn="__sil__";function nn(e){return e.lastDrawnKey?e.lastDrawnKey.split("|")[0]===zn:!1}function Fr(){return{lastDrawnKey:""}}function Me(e){e.lastDrawnKey="",e.transitionCache=void 0}function Ki(e,t){if(t?.transitionCache&&t.transitionCache.queueLength===e.length)return t.transitionCache.transitions;const r=Ns(e);return t&&(t.transitionCache={queueLength:e.length,transitions:r}),r}function Gi(e,t){let r="sil";for(const n of e)n.viseme!=="sil"&&(t!=null&&n.vtime>t||(r=n.viseme));return r}function zi(e,t,r,n=120){if(r!=="sil")return r;const s=Mn(e,t,n);return s!=="sil"?s:Gi(e,t)}function ji(e,t){return e==="sil"?!1:t?ci(e,"sil",t):Rr(e,"sil").length>0}function Yi(){return Gn+qi}function zt(e){return e??zn}function Lr(e,t,r,n,s=Hi){if(e.length===0)return{diffPath:null,label:"sil"};if(t>=Je(e))return{diffPath:null,label:"sil"};const i=Ki(e,n),o=Us(i,t);if(o){const l=xn(o.fromVtime,o.toVtime,o.from,o.to);if(t>=l.transStart&&t<l.transEnd){let f=Rr(o.from,o.to);r&&(f=Un(f,r));const u=li(f,l.effectiveGapMs);if(u.length>0){const c=fi(t,l.transStart,l.transEnd,u.length);return{diffPath:u[c]??u[u.length-1],label:`${o.from}→${o.to}`,transitionMeta:{from:o.from,to:o.to,fromVtime:o.fromVtime,toVtime:o.toVtime,frameIdx:c,frameCount:u.length,fullFrameCount:f.length,omittedByGap:Math.max(0,f.length-u.length)}}}return{diffPath:Tt(o.to),label:o.to}}}const a=Mn(e,t,s);return a==="sil"?{diffPath:null,label:"sil"}:{diffPath:Tt(a),label:a}}function Qt(e,t){const r=t??zt(e.expressionPath);return`${zt(e.mouthPath)}|${r}`}function sn(e,t,r,n,s,i,o,a,l,f){const u=Lr(t,r,l,i),c={mouthPath:u.diffPath,expressionPath:n},d=Qt(c,s),w=Ye();if(u.transitionMeta&&w?.onTransitionSample(u.transitionMeta,u.diffPath),d===i.lastDrawnKey){w?.onDrawDeduped();return}w?.onDrawRequested(),a(u),i.lastDrawnKey=d,o(e,c,d,b=>{b===d&&(i.lastDrawnKey=d)})}function ur(e,t,r,n,s,i,o=null){const a={mouthPath:null,expressionPath:t},l=o!=null?`|idle@${Math.round(o*1e3)}`:"",f=Qt(a,r)+l;!(o!=null)&&f===n.lastDrawnKey||(i(o!=null?t?"idle · blink":"idle":t?"blink":"sil"),s(e,a,f,c=>{c===f&&(n.lastDrawnKey=f)}))}function jn(e,t,r,n,s=vt,i){if(r<=t||e.length===0)return[];const o=i??Fr(),a=[];let l="";for(let f=t;f<r;f+=s){const u=Lr(e,f,n,o),c=zt(u.diffPath);c!==l&&(l=c,a.push({displayAtMs:f,mouthPath:u.diffPath,mouthKey:c,label:u.label}))}return a}const Xi=3;function Or(e){let t=0;const r=[];let n=!1,s=!1;const i=async()=>{if(!n){n=!0;try{for(;r.length>0;){const a=r.shift(),l=++t;await e(a.diffs,l,a)&&l===t&&a.onDisplayed?.(a.drawKey)}}finally{n=!1,r.length>0&&i()}}},o=()=>{s||(s=!0,queueMicrotask(()=>{s=!1,i()}))};return{enqueue(a,l,f){const u=r.findIndex(c=>c.drawKey===l);u>=0?r[u]={diffs:a,drawKey:l,onDisplayed:f}:(r.length>=Xi&&(r.shift(),Ye()?.onQueueSuperseded()),r.push({diffs:a,drawKey:l,onDisplayed:f})),o()},reset(){t+=1,r.length=0},isLatestSeq(a){return a===t},getLatestSeq(){return t}}}function Ji(e,t=2){const r=[],n=new Set;let s=0,i=!1,o=0;const a=(f,u)=>`${f}|${u}`,l=async()=>{if(!i){i=!0;try{for(;s<t&&r.length>0;){const f=r.shift(),u=a(f.displayAtMs,f.drawKey);if(n.has(u))continue;n.add(u),s+=1;const c=o;try{await e(f)}finally{if(n.delete(u),s-=1,c!==o)return}}}finally{i=!1,r.length>0&&s<t&&l()}}};return{enqueue(f){const u=a(f.displayAtMs,f.drawKey);n.has(u)||r.some(c=>a(c.displayAtMs,c.drawKey)===u)||(r.push(f),l())},isQueuedOrInflight(f,u){const c=a(f,u);return n.has(c)?!0:r.some(d=>a(d.displayAtMs,d.drawKey)===c)},reset(){o+=1,r.length=0,n.clear(),s=0},pendingCount(){return r.length+s}}}function Qi(){const e=new Map,t=r=>{r.bitmap.close()};return{has(r,n){return e.has(n)},markRendered(r,n,s,i,o){const a=e.get(n);a&&t(a),e.set(n,{displayAtMs:r,mouthKey:n,label:s,mouthPath:i,bitmap:o})},findForMouthKey(r){return e.get(r)??null},removeThrough(r){},pruneBefore(r){},reset(){for(const r of e.values())t(r);e.clear()},size(){return e.size}}}function Zi(e,t,r,n,s,i){const o=jn(t,r,n,s,vt,i);if(o.length===0)return{ready:0,total:0};let a=0;for(const l of o)e.has(l.displayAtMs,l.mouthKey)&&(a+=1);return{ready:a,total:o.length}}function ea(e){const t=Qi(),r=Fr(),n=new Set;let s="",i=null;const o=Ji(async c=>{try{if(!e.supportsBufferedPlayback)return;const d=await e.compositePrefetchFrame(c.diffs,c.mouthKey);if(!d)return;t.markRendered(c.displayAtMs,c.mouthKey,c.label,c.mouthPath,d),Ye()?.onBufferPrefetched()}finally{n.delete(c.mouthKey)}},ls),a=c=>{for(const d of c)t.has(d.displayAtMs,d.mouthKey)||n.has(d.mouthKey)||o.isQueuedOrInflight(0,d.mouthKey)||(n.add(d.mouthKey),o.enqueue({...d,displayAtMs:0,drawKey:d.mouthKey,diffs:{mouthPath:d.mouthPath,expressionPath:null}}))},l=(c,d,w,b=ir)=>{if(!e.supportsBufferedPlayback)return;const h=Math.max(0,d-vt*4),A=jn(c,h,d+b,w,vt,r);a(A)},f=(c,d)=>{s=c,i=d},u=(c,d)=>!(s!==c||i!==d);return{reset(){t.reset(),o.reset(),n.clear(),Me(r),s="",i=null,Ye()?.onBufferReset()},markExternallyDisplayed(c,d){f(c.split("|")[0]??c,d)},warmupTick(c){e.supportsBufferedPlayback&&l(c.queue,0,c.availablePaths,Math.max($r,ir))},isWarmupReady(c,d=$r){if(!e.supportsBufferedPlayback)return!0;const{ready:w,total:b}=Zi(t,c.queue,0,d,c.availablePaths,r);return b===0?!1:w/b>=us},getBufferedFrameCount:()=>t.size(),tick(c){const d=Ye();d?.onBufferTick(),l(c.queue,c.elapsedMs,c.availablePaths),t.pruneBefore(c.elapsedMs-ir);const w=Lr(c.queue,c.elapsedMs,c.availablePaths,r),b=zt(w.diffPath),h=Qt({mouthPath:w.diffPath,expressionPath:c.expressionPath},c.expressionDrawKey),A=()=>(f(b,c.expressionPath),d?.onBufferHit(),{usedBuffer:!0,underrun:!1,held:!0});if(u(b,c.expressionPath)||c.playbackState.lastDrawnKey===h)return A();const v=t.findForMouthKey(b,c.elapsedMs,vt);return v&&c.expressionPath!=null?(d?.onBufferHit(),{usedBuffer:!1,underrun:!1,held:!1}):v?(e.blitBitmap(c.canvas,v.bitmap),f(v.mouthKey,c.expressionPath),c.playbackState.lastDrawnKey=h,c.onStatus({diffPath:v.mouthPath,label:v.label}),c.onDisplayed(h,v.mouthPath,v.label),d?.onBufferHit(),{usedBuffer:!0,underrun:!1,held:!1}):(d?.onBufferUnderrun(),{usedBuffer:!1,underrun:!0,held:!1})}}}const ta=`
|
|
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
|
-
`,
|
|
33
|
+
`,ra=`
|
|
34
34
|
in vec2 vTextureCoord;
|
|
35
35
|
out vec4 finalColor;
|
|
36
36
|
|
|
@@ -44,4 +44,4 @@ void main(void)
|
|
|
44
44
|
float keep = step(uThreshold + 1e-6, peak);
|
|
45
45
|
finalColor = vec4(color.rgb, keep);
|
|
46
46
|
}
|
|
47
|
-
`;function rn(e=5){const t=new se.Filter({glProgram:se.GlProgram.from({vertex:Xi,fragment:Ji,name:"threshold-key-filter"}),resources:{thresholdUniforms:{uThreshold:{value:e/255,type:"f32"}}}});return t.setThreshold=r=>{t.resources.thresholdUniforms.uniforms.uThreshold=r/255},t.getThreshold=()=>Math.round(t.resources.thresholdUniforms.uniforms.uThreshold*255),t}function Qi(e,t,r){return e>r||t>r}function Zi(e){return e?e.getParameter(e.MAX_TEXTURE_SIZE):4096}function ea(e,t){return t!=null&&t>0?Math.floor(t):Zi(e)}function ta(e,t){const r=document.createElement("canvas");r.width=t.w,r.height=t.h;const n=r.getContext("2d");if(!n)throw new Error("Failed to acquire 2d context for atlas cell crop");return n.drawImage(e,t.x,t.y,t.w,t.h,0,0,t.w,t.h),r}function Kn(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 nn(e,t,r,n,s){const i=r?await Bn(e,s):await fetch(e).then(u=>{if(!u.ok)throw new Error(`Failed to load ${e}`);return u.json()}),o=i.sheets?.[0];if(!o?.path)throw new Error(`${e} has no sheets[0].path`);const a=new Map,l=async(u,d)=>{const w=On(t,d,r),T=r?await Xs(await ct(w,s??"")):await Kn(w);if(Qi(T.width,T.height,n))return console.info(`[PixiVisemeCompositor] Sheet ${d} (${T.width}x${T.height}) exceeds MAX_TEXTURE_SIZE=${n}; using per-cell crops.`),{mode:"cells",bitmap:T};const h=se.Texture.from(T);return h.source.scaleMode="nearest",{mode:"full",texture:h}};a.set(o.index,await l(o.index,o.path));for(const u of i.sheets??[])u.index!==o.index&&u.path&&a.set(u.index,await l(u.index,u.path));const f=new Map;for(const u of i.cells??[])u.path&&f.set(u.path,u);const c=new Map;for(const u of i.sheets??[])c.set(u.index,u);return{atlasMeta:i,cellByPath:f,sheetByIndex:c,diffBase:t,sheets:a}}async function ra(e,t={}){const r=new se.Application;try{await r.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(y){throw r.destroy({removeView:!1}),y}if(r.renderer.type!==se.RendererType.WEBGL)throw r.destroy({removeView:!1}),new Error("Pixi did not initialize a WebGL renderer");const n=ea(r.renderer.gl,t.maxTextureSize);let s=!1,i=!1,o=!1;const a=r.canvas,l=()=>{s=!0,!i&&!o&&(i=!0,t.onWebGlContextLost?.())};a.addEventListener("webglcontextlost",l);const f=new se.Sprite(se.Texture.EMPTY),c=new se.Sprite(se.Texture.EMPTY);c.visible=!1,c.alpha=0;const u=new se.Sprite(se.Texture.EMPTY),d=new se.Sprite(se.Texture.EMPTY),w=new se.Sprite(se.Texture.EMPTY);w.visible=!1;const T=rn(5),h=rn(5);u.filters=[T],d.filters=[h],r.stage.addChild(f),r.stage.addChild(c),r.stage.addChild(u),r.stage.addChild(d),r.stage.addChild(w);let I=null,b=null,x=null,p=null,A=null,C=null,L=null,K=842,P=1264,k=null,D=5;const O=new Map;let M=null,re=null,N=null;const J=pn(()=>{O.clear(),C=null,L=null,I=null,k=null}),X=(y,S)=>{K=y,P=S,r.renderer.resize(y,S),(e.width!==y||e.height!==S)&&(e.width=y,e.height=S)},Q=()=>{u.visible=!1,d.visible=!1,u.texture=se.Texture.EMPTY,d.texture=se.Texture.EMPTY,M=null,re=null},_e=()=>{A=null,c.visible=!1,c.alpha=0},Z=()=>{f.visible=!0,f.width=K,f.height=P,f.x=0,f.y=0},ue=()=>{c.width=K,c.height=P,c.x=0,c.y=0},ce=()=>{w.visible=!1,_e(),I&&(f.texture=I,Z())},pe=()=>{!A&&!c.visible||ce()},ae=y=>{if(o||s||!I){ce();return}const S=Math.max(0,y);if(!b||S<=0){ce();return}Q(),w.visible=!1,f.texture=b,Z(),c.texture=I,ue(),c.alpha=0,c.visible=!0,A={startMs:performance.now(),durationMs:S},R()},z=(y=performance.now())=>{if(!A)return!1;if(o||s)return pe(),!1;const S=(y-A.startMs)/Math.max(1,A.durationMs);if(S>=1)return ce(),R(),!1;const F=Math.min(1,Math.max(0,S)),G=F*F*(3-2*F);return c.alpha=G,c.visible=!0,R(),!0},ee=()=>{if(x){try{x.resource=null}catch{}x.destroy(),x=null}b&&(b.destroy(!1),b=null),p=null},te=(y,S)=>{const F=y.cellByPath.get(S);if(!F||!Dn(F))return null;const G=`${y.diffBase}::${S}`;let W=O.get(G);if(!W){const j=F.sheet??0,ve=y.sheets.get(j);if(!ve)return null;let Ae,Te=!1;if(ve.mode==="full")Ae=new se.Texture({source:ve.texture.source,frame:new se.Rectangle(F.x,F.y,F.w,F.h)});else{const m=ta(ve.bitmap,F);Ae=se.Texture.from(m),Ae.source.scaleMode="nearest",Te=!0}W={texture:Ae,ownsSource:Te},O.set(G,W)}return{texture:W.texture,cell:F}},we=(y,S,F,G)=>{if(!G||!F)return y.visible=!1,y.texture=se.Texture.EMPTY,!1;const W=te(F,G);if(!W)return y.visible=!1,y.texture=se.Texture.EMPTY,!1;const j=Cn(F.atlasMeta,W.cell);return S.setThreshold(D),y.texture=W.texture,y.x=j.x,y.y=j.y,y.width=W.cell.w,y.height=W.cell.h,y.visible=!0,!0},g=y=>{if(!y||!C)return null;const S=k??new Set(C.cellByPath.keys());return br(y,S)??(S.has(y)?y:null)},B=y=>{w.visible=!1,ce();const S=g(y.mouthPath),F=y.expressionPath&&L&&L.cellByPath.has(y.expressionPath)?y.expressionPath:null;S!==M?(we(u,T,C,S),M=S):S?u.visible=!0:u.visible=!1,F!==re?(we(d,h,L,F),re=F):F?d.visible=!0:d.visible=!1},R=()=>{if(!(o||s||document.hidden))try{r.render()}catch{}};return{async loadAssets(y){if(o)return{width:K,height:P,visemePaths:[]};const S=Tt();if(S&&!y)throw new Error("Encrypted assets enabled but no key provided");const F=S?await Qs(await ct(Nt(),y??"")):await Kn(Nt());I&&I.destroy(!0),I=se.Texture.from(F),I.source.scaleMode="linear";const G=wn();C=await nn(gn(),G,S,n,y);try{L=await nn(yn(),G,S,n,y)}catch{L=null}const W=Rn(C.atlasMeta,F.width,F.height);return X(W.width,W.height),D=Kt(C.atlasMeta),T.setThreshold(D),h.setThreshold(D),k=new Set(C.cellByPath.keys()),O.clear(),ce(),Q(),R(),{width:W.width,height:W.height,visemePaths:[...C.cellByPath.keys()]}},getVisemeAtlasPaths(){return k},drawLiveFrame(y){o||s||!I||(pe(),B(y),R())},drawIdleVideo(y,S){o||s||document.hidden||S&&!S()||y.readyState<HTMLMediaElement.HAVE_CURRENT_DATA||(_e(),Q(),w.visible=!1,!b||p!==y?(ee(),x=new se.VideoSource({resource:y,autoPlay:!1,updateFPS:0}),x.autoUpdate=!1,b=new se.Texture({source:x}),p=y):b.source.update(),f.texture=b,Z(),R())},beginIdleToSilCrossfade:ae,tickIdleToSilCrossfade:z,finishIdleToSilCrossfade:pe,isIdleToSilCrossfading(){return A!=null},renderSilOnly(){o||s||!I||(Q(),ce(),R())},blitBitmap(y){o||s||(pe(),Q(),N&&(N.destroy(!0),N=null),N=se.Texture.from(y),w.texture=N,w.visible=!0,w.x=0,w.y=0,w.width=K,w.height=P,f.visible=!1,R())},async compositePrefetchFrame(y){return null},renderFrame:R,isContextLost(){return s},dispose(){if(o)return;o=!0,a.removeEventListener("webglcontextlost",l),J(),ee(),N&&(N.destroy(!0),N=null);for(const{texture:S,ownsSource:F}of O.values())S.destroy(F);O.clear(),I&&(I.destroy(!0),I=null);const y=S=>{S.mode==="full"?S.texture.destroy(!0):S.bitmap.close()};if(C)for(const S of C.sheets.values())y(S);if(L)for(const S of L.sheets.values())y(S);T.destroy(!0),h.destroy(!0),r.destroy({removeView:!1},{children:!0,texture:!0})}}}const sn=-1,Bt=1e5,an=new Map;async function na(e){let t=an.get(e);if(!t){const r=await fetch(e);if(!r.ok)throw new Error(`Failed to fetch worker script (${r.status}): ${e}`);const n=await r.blob();t=URL.createObjectURL(n),an.set(e,t)}return new Worker(t,{name:"visemeDiffPreview"})}function sa(){return typeof Worker<"u"&&typeof OffscreenCanvas<"u"}function fr(e,t){(e.width!==t.width||e.height!==t.height)&&(e.width=t.width,e.height=t.height);const r=e.getContext("2d");r&&r.drawImage(t,0,0)}function on(e,t){let r=0,n=null;const s=Dr(async(i,o)=>(await cr(),await lr(e,i),s.isLatestSeq(o)));return{usesWorker:!1,usesPixi:!1,async loadAssets(i){const o=await cr(i);return n=o.visemePaths?new Set(o.visemePaths):null,o},getVisemeAtlasPaths(){return n},async renderSilOnly(){r+=1,s.reset(),await Bi(e)},drawLiveFrame(i,o,a,l){s.enqueue(o,a,l)},async compositePrefetchFrame(i,o){try{return await Di(i)}catch{return null}},blitBitmap(i,o){fr(i,o)},drawIdleVideo(){},beginIdleToSilCrossfade(){},tickIdleToSilCrossfade(){return!1},finishIdleToSilCrossfade(){},isIdleToSilCrossfading(){return!1},supportsBufferedPlayback:!0,async renderViseme(i,o){r+=1;const a=r;t.onStatus?.(o),await lr(i,{mouthPath:St(o),expressionPath:null})},dispose(){r+=1,s.reset()}}}function ia(e,t,r){let n=null,s=1,i=0,o=!1,a=!1,l=null;const f=new Map,c=new Map;let u=Bt;const d=Dr(async(x,p,A)=>(f.set(p,{drawKey:A.drawKey,onDisplayed:A.onDisplayed}),await I({type:"drawFrame",diffPath:x.mouthPath,expressionDiffPath:x.expressionPath,liveSeq:p},void 0,!0),!1)),w=x=>{if(x===sn){let C=-1,L;for(const[K,P]of f)K>=C&&(C=K,L=P);C>=0&&f.delete(C),L?.onDisplayed?.(L.drawKey);return}const p=-x,A=f.get(p);f.delete(p),A?.onDisplayed?.(A.drawKey)},T=new Map,h=(x,p)=>{a||!n||n.postMessage(x,[])},I=(x,p,A=!1)=>{if(a)return Promise.resolve();const C=s++;return new Promise((L,K)=>{T.set(C,{resolve:P=>L(P),reject:K,generation:typeof x.generation=="number"?x.generation:void 0,expectRenderDone:A}),h({...x,requestId:C})})},b=(async()=>{n=await na(r),n.onmessage=x=>{if(a)return;const p=x.data;if(p.type==="status"){t.onStatus?.(p.label);return}if(p.type==="frame"){if(p.generation<=-Bt){const L=-p.generation,K=c.get(L);c.delete(L),K?.resolve(p.bitmap);return}const C=p.generation<0;if(!C&&p.generation!==i){p.bitmap.close();return}if(C&&!(p.generation===sn)){const K=-p.generation;if(!d.isLatestSeq(K)){Ve()?.onWorkerStale(),p.bitmap.close();return}}if(C&&t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()){Ve()?.onWorkerStale(),p.bitmap.close();return}fr(e,p.bitmap),p.bitmap.close(),C&&w(p.generation);return}const A=T.get(p.requestId);if(A)switch(p.type){case"ready":T.delete(p.requestId),A.resolve();break;case"loadAssetsDone":T.delete(p.requestId),l=p.visemePaths?.length?new Set(p.visemePaths):null,A.resolve({width:p.width,height:p.height,visemePaths:p.visemePaths});break;case"renderDone":if(A.expectRenderDone&&A.generation!=null&&p.generation!==A.generation)return;T.delete(p.requestId),A.resolve();break;case"renderAborted":T.delete(p.requestId),A.resolve();break;case"error":T.delete(p.requestId),t.onError?.(p.message),A.reject(new Error(p.message));break}},n.onerror=x=>{if(a)return;const p=x.message||"Worker error";t.onError?.(p);for(const[,A]of T)A.reject(new Error(p));T.clear()},await I({type:"init"}),o=!0})();return{usesWorker:!0,usesPixi:!1,async loadAssets(x){if(a)return{width:842,height:1264};if(await b,a)return{width:842,height:1264};if(!o)throw new Error("Worker init failed");return await I({type:"loadAssets",keyHex:x,urls:_s(Je())})??{width:842,height:1264}},getVisemeAtlasPaths(){return l},async renderSilOnly(){if(a||(await b,a))return;i+=1,d.reset(),f.clear(),c.forEach(({resolve:p})=>p(null)),c.clear(),u=Bt;const x=i;await I({type:"cancelLiveDraws"}),await I({type:"renderSil",generation:x},void 0,!0)},drawLiveFrame(x,p,A,C){a||b.then(()=>{a||d.enqueue(p,A,C)})},compositePrefetchFrame(x,p){return a?Promise.resolve(null):b.then(()=>{if(a)return null;const A=++u;return new Promise(C=>{c.set(A,{resolve:C}),I({type:"drawFrame",diffPath:x.mouthPath,expressionDiffPath:x.expressionPath,liveSeq:A},void 0,!0).catch(()=>{c.delete(A),C(null)})})})},blitBitmap(x,p){fr(x,p)},drawIdleVideo(){},beginIdleToSilCrossfade(){},tickIdleToSilCrossfade(){return!1},finishIdleToSilCrossfade(){},isIdleToSilCrossfading(){return!1},supportsBufferedPlayback:!0,async renderViseme(x,p,A={}){if(a||(await b,a))return;i+=1,d.reset(),f.clear();const C=i;try{await I({type:"cancelLiveDraws"}),await I({type:"renderViseme",generation:C,to:p,from:A.from,transitionDurationMs:A.transitionDurationMs,gapMs:A.gapMs,threshold:A.threshold},void 0,!0)}catch(L){if(L?.name==="AbortError")return;throw L}},dispose(){if(!a){a=!0,i+=1,d.reset(),f.clear(),c.forEach(({resolve:x})=>x(null)),c.clear(),u=Bt;for(const[,x]of T)x.resolve();T.clear(),n?.terminate(),n=null}}}}async function aa(e,t){const r=await ra(e,{onWebGlContextLost:t.onWebGlContextLost,maxTextureSize:t.pixiMaxTextureSize});let n=0,s=null,i=!1;const o=Dr(async(a,l)=>i?!1:t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()?(Ve()?.onWorkerStale(),!1):(r.drawLiveFrame(a),o.isLatestSeq(l)));return{usesWorker:!1,usesPixi:!0,async loadAssets(a){if(i)return{width:842,height:1264};const l=await r.loadAssets(a);return s=l.visemePaths?.length?new Set(l.visemePaths):null,l},getVisemeAtlasPaths(){return s},async renderSilOnly(){i||(n+=1,o.reset(),r.renderSilOnly())},drawLiveFrame(a,l,f,c){i||o.enqueue(l,f,c)},async compositePrefetchFrame(a,l){if(i)return null;try{return await r.compositePrefetchFrame(a)}catch{return null}},blitBitmap(a,l){i||t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()||r.blitBitmap(l)},drawIdleVideo(a,l){i||r.drawIdleVideo(a,l)},beginIdleToSilCrossfade(a){i||r.beginIdleToSilCrossfade(a)},tickIdleToSilCrossfade(a){return i?!1:r.tickIdleToSilCrossfade(a)},finishIdleToSilCrossfade(){i||r.finishIdleToSilCrossfade()},isIdleToSilCrossfading(){return i?!1:r.isIdleToSilCrossfading()},supportsBufferedPlayback:!1,async renderViseme(a,l){if(i)return;n+=1;const f=n;o.reset(),t.onStatus?.(l),r.drawLiveFrame({mouthPath:St(l),expressionPath:null})},dispose(){i||(i=!0,n+=1,o.reset(),r.dispose())}}}function un(e,t){if(!sa())return on(e,t);const r=rs(t.workerScriptUrl);try{return ia(e,t,r)}catch{return on(e,t)}}function oa(e,t={}){if(t.preferLegacyRenderer)return un(e,t);let r=null,n=!1,s=null,i=!1;const o=()=>r?Promise.resolve(r):(s||(s=(async()=>{try{const a=await aa(e,t);if(i)throw a.dispose(),new Error("Renderer disposed during Pixi init");return n=!0,r=a,a}catch(a){if(i)throw a instanceof Error?a:new Error(String(a));return console.warn("[AiTwin] Pixi WebGL renderer unavailable, using Canvas2D fallback:",a),r=un(e,t),n=!1,r}})()),s);return{get usesWorker(){return r?.usesWorker??!1},get usesPixi(){return n},async loadAssets(a){return(await o()).loadAssets(a)},getVisemeAtlasPaths(){return r?.getVisemeAtlasPaths()??null},async renderSilOnly(){return(await o()).renderSilOnly()},drawLiveFrame(a,l,f,c){o().then(u=>{i||u.drawLiveFrame(a,l,f,c)})},async compositePrefetchFrame(a,l){return(await o()).compositePrefetchFrame(a,l)},blitBitmap(a,l){r&&r.blitBitmap(a,l)},drawIdleVideo(a,l){r?.usesPixi&&r.drawIdleVideo(a,l)},beginIdleToSilCrossfade(a){r?.beginIdleToSilCrossfade(a)},tickIdleToSilCrossfade(a){return r?.tickIdleToSilCrossfade(a)??!1},finishIdleToSilCrossfade(){r?.finishIdleToSilCrossfade()},isIdleToSilCrossfading(){return r?.isIdleToSilCrossfading()??!1},get supportsBufferedPlayback(){return r?.supportsBufferedPlayback??!1},async renderViseme(a,l,f){return(await o()).renderViseme(a,l,f)},dispose(){i=!0,s=null,r?.dispose(),r=null}}}class ua extends Error{constructor(t,r){super(`${t} timed out after ${Math.round(r/1e3)}s`),this.name="LoadTimeoutError"}}function rr(e,t,r){return new Promise((n,s)=>{const i=globalThis.setTimeout(()=>{s(new ua(r,t))},t);e.then(o=>{globalThis.clearTimeout(i),n(o)},o=>{globalThis.clearTimeout(i),s(o)})})}const ca={position:"relative",display:"inline-block",lineHeight:0},la={display:"block",maxWidth:"100%",height:"auto"},fa={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 gt(e){return us(e)?_r:yr}function Ne(e,t){return t?.isSpeaking()?t:e.isSpeaking()?e:t?.isConnected()?t:e}const da=v.forwardRef(function({id:t,avatarId:r,faceId:n,assets:s,authToken:i,apiBase:o,ttsEngineId:a=ze,voiceId:l,speakingRate:f=kt,className:c,style:u,canvasStyle:d,onStatusChange:w,onDisplayStatus:T,onError:h,onReady:I,showErrorOverlay:b=!0,onUserTranscript:x,onBotOutput:p,onRealtimeLipsyncDebug:A,onFrameRenderStats:C,lipsyncBufferedPlayback:L=!0,lipsyncPlaybackWarmupMs:K=1500,pixiMaxTextureSize:P},k){const D=v.useRef(null),O=v.useRef(null),M=v.useRef(null),re=v.useRef(L);re.current=L;const N=v.useRef(K);N.current=K;const J=v.useRef(0),X=v.useRef(Mr()),Q=v.useRef(bi()),_e=v.useRef(0),Z=v.useRef(0),ue=v.useRef(0),ce=v.useRef(null),pe=v.useRef(null),ae=v.useRef(!1),z=v.useRef(!1),ee=v.useRef(!1),te=v.useRef(!1),we=v.useRef("sil"),g=v.useRef(0),B=v.useRef(!1),R=v.useRef(null),y=v.useRef(!1),[S,F]=v.useState("idle"),[G,W]=v.useState(null),[j,ve]=v.useState(!1),[Ae,Te]=v.useState(!1),[m,E]=v.useState({width:842,height:1264}),[Y,le]=v.useState(0),ye=v.useRef(!1),ke=v.useRef(i??null),de=o?.trim()||vt,Ce=v.useRef(null),me=v.useRef(null),We=v.useRef(!1),He=v.useRef({width:842,height:1264}),Ee=v.useRef(null),lt=v.useRef("sil"),Qe=v.useRef(void 0),ft=v.useRef(new Map),Ue=v.useRef(I),he=v.useRef(T),dt=v.useRef(h),Ze=v.useRef(x),ge=v.useRef(p),Se=v.useRef(A),Pe=v.useRef(C),De=v.useRef(Li());Ue.current=I,he.current=T,dt.current=h,Ze.current=x,ge.current=p,Se.current=A,Pe.current=C,v.useEffect(()=>{const _=De.current;return Xr(_),()=>Xr(null)},[]);const mt=v.useCallback(_=>{const U=_?.ttsEngineId??a??me.current?.ttsEngineId??ze,H=_?.voiceId??l??me.current?.voiceId??gt(U);return{ttsEngineId:U,voiceId:H}},[a,l]),Be=v.useCallback(_=>{W(_),dt.current?.(_)},[]),$e=v.useCallback(_=>{F(_),w?.(_)},[w]),ne=v.useRef(ar($e,de,{onLipsyncDebug:_=>Se.current?.(_)})),ie=v.useRef(null);v.useEffect(()=>(ne.current=ar($e,de,{onLipsyncDebug:_=>Se.current?.(_)}),i&&ne.current.setDeveloperToken(i),ne.current.setTtsEngineId(a),()=>{ne.current.stop();const _=ie.current;_&&(_.disconnect(),ie.current=null)}),[$e,i,a,de]);const Br=v.useCallback(()=>(ie.current||(ie.current=Mn($e,{speakingRate:f,onUserTranscript:(_,U)=>Ze.current?.(_,U),onBotOutput:_=>ge.current?.(_),onLipsyncDebug:_=>Se.current?.(_)}),ie.current.setTtsEngineId(a)),ie.current),[$e,f,a]),xe=v.useCallback(()=>{ce.current!=null&&(clearTimeout(ce.current),ce.current=null)},[]),Me=v.useCallback(()=>{pe.current!=null&&(clearTimeout(pe.current),pe.current=null),ee.current=!1},[]),Et=v.useCallback(()=>{if(te.current)return;const _=O.current;if(te.current=!0,Me(),ae.current=!1,z.current=!1,!y.current||!R.current){_?.renderSilOnly();return}R.current.setActive(!1);const U=R.current.getVideo();if(_?.usesPixi){_.drawIdleVideo(U),_.beginIdleToSilCrossfade(Qr);return}(async()=>{const H=D.current;H&&await tr(H,U,null,void 0,He.current).catch(()=>{}),await new Promise(V=>setTimeout(V,Qr)),te.current&&await _?.renderSilOnly()})()},[Me]),Fr=v.useCallback(()=>{!y.current||!R.current||(te.current=!1,O.current?.finishIdleToSilCrossfade(),Me(),ee.current=!0,pe.current=setTimeout(()=>{if(pe.current=null,Ne(ne.current,ie.current).isSpeaking()){ee.current=!1;return}if(ae.current){ee.current=!1;return}const _=R.current;if(!_){ee.current=!1;return}_.restart(),_.setActive(!0);const U=_.getVideo();let H=!1;const V=()=>{H||(H=!0,U.removeEventListener("playing",q),ee.current=!1)},q=()=>{V()};if(U.addEventListener("playing",q,{once:!0}),U.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA&&!U.paused){V();return}setTimeout(V,300)},Oi))},[Me]),et=v.useCallback(()=>{if(xe(),he.current?.("sil"),ae.current=!1,te.current=!1,!y.current||!R.current){ee.current=!1;return}Fr()},[xe,Fr]),Gt=v.useCallback(async _=>{const U=Z.current,H=O.current,V=D.current,q=()=>{_?.fallbackOnly||(ae.current=!1)};if(!H||!V||!j){q();return}if(Ne(ne.current,ie.current).isSpeaking()){q();return}if(_?.fallbackOnly){if(!ae.current||(Oe(X.current),await H.renderSilOnly(),U!==Z.current)||Ne(ne.current,ie.current).isSpeaking())return;et();return}if(Zr(X.current)){Oe(X.current),et();return}if(re.current&&H.supportsBufferedPlayback&&!_?.fallbackOnly){if(Oe(X.current),M.current?.reset(),await H.renderSilOnly(),U!==Z.current||Ne(ne.current,ie.current).isSpeaking())return;et();return}const Re=Hi(Ne(ne.current,ie.current).getVisemeQueue(),g.current,we.current),ht=$i(Re,Ee.current);if(Oe(X.current),!ht){if(await H.renderSilOnly(),U!==Z.current||Ne(ne.current,ie.current).isSpeaking())return;et();return}try{await H.renderViseme(V,"sil",{from:Re,transitionDurationMs:Hn})}catch(rt){if(rt?.name!=="AbortError")throw rt}if(U!==Z.current){q();return}if(Ne(ne.current,ie.current).isSpeaking()){q();return}et()},[j,et]),tt=v.useCallback(()=>{if(z.current||ae.current)return;z.current=!0,ae.current=!0,xe();const _=Z.current;Gt().finally(()=>{z.current=!1}),ce.current=setTimeout(()=>{ce.current=null,_===Z.current&&Gt({fallbackOnly:!0})},qi())},[xe,Gt]),Lr=v.useCallback(async()=>{const _=Math.floor(Date.now()/1e3);if(Ce.current&&_<Ce.current.expiresAt-60)return Ce.current.key;const U=ke.current??await Ft();ke.current=U,ne.current.setDeveloperToken(U);const H=await ps(U,de);return Ce.current={key:H.value,expiresAt:H.expiresAt},H.value},[de]);v.useEffect(()=>{i&&(ke.current=i,ne.current.setDeveloperToken(i))},[i]),v.useEffect(()=>{ne.current.setTtsEngineId(a),ie.current?.setTtsEngineId(a),me.current&&(me.current.ttsEngineId=a)},[a]),v.useEffect(()=>{const _=l??gt(a);me.current&&(me.current.voiceId=_)},[l,a]);const qe=v.useMemo(()=>mn(r,n),[r,n]),jn=v.useMemo(()=>s?JSON.stringify(s):qe?`face:${qe}`:`id:${t??""}`,[s,qe,t]);return v.useEffect(()=>{if(!t&&!s&&!qe){Be("AiTwin requires `assets`, `avatarId`/`faceId`, or `id`");return}let _=!1;We.current=!1,ve(!1),Te(!1),W(null),me.current=null,R.current=null,y.current=!1;const U=D.current;if(!U)return;let H=null;return(async()=>{try{let V,q,Ie,Re;if(s)V=s,q=a,Ie=l??gt(q);else if(qe)he.current?.("loadFace"),V=ir(qe),q=a,Ie=l??gt(q);else{he.current?.("getAiTwin");const $=await rr(dn(t),3e4,"getAiTwin");if(_)return;if(!$.faceId?.trim())throw new Error("getAiTwin returned no faceId");q=$.ttsEngineId,Ie=l??$.voiceId??gt(q),Re=$.knowledgeContextId?.trim()||void 0,V=ir($.faceId)}me.current={ttsEngineId:q,voiceId:Ie,personaId:Re},hn(V),R.current=ki();const ht=!!(s&&!Xe(s));H=oa(U,{preferLegacyRenderer:ht,pixiMaxTextureSize:P,onStatus:$=>{_||he.current?.($)},onError:$=>{_||Be($)},shouldAcceptLiveFrame:()=>{const $=Ne(ne.current,ie.current);return($.isSpeaking()||$.isWarmingUp())&&!ae.current},onWebGlContextLost:()=>{_||ye.current||(ye.current=!0,queueMicrotask(()=>{_||le($=>$+1)}))}}),O.current=H,M.current=Yi(H),ye.current=!1,($=>{const fe=()=>{if(!$||N.current<=0)return!1;const be=O.current;return be?re.current&&be.supportsBufferedPlayback?!0:be.usesPixi:!1};ne.current.setLipsyncWarmupHold($,fe),ie.current?.setLipsyncWarmupHold($,fe)})(!0),he.current?.("auth");const pt=ke.current??i??await rr(Ft(),3e4,"getAuthToken");if(_)return;ke.current=pt,ne.current.setDeveloperToken(pt),ne.current.setTtsEngineId(q);const It=Tt()?await Lr():void 0;if(_)return;he.current?.("loadAssets");const oe=await rr(H.loadAssets(It),18e4,"load face assets");if(!_&&oe.width>0&&oe.height>0&&(He.current=oe,E(oe)),_)return;Ee.current=H.getVisemeAtlasPaths(),We.current=!0,ve(!0),Ue.current?.(),H.renderSilOnly().then(()=>{_||he.current?.("sil")}),(async()=>{try{const $=await ti(It);if(await R.current.load($),_)return;y.current=!0,Te(!0),Oe(X.current),R.current.setActive(!0);const fe=R.current.getVideo();H.usesPixi?H.drawIdleVideo(fe):await tr(U,fe,null,void 0,He.current),_||he.current?.("idle")}catch($){if(_)return;y.current=!1,Te(!1),console.warn("[AiTwin] Idle video unavailable:",$)}})()}catch(V){if(_)return;if(V instanceof Lt){Be(V.message);return}Be(V instanceof Error?V.message:"Failed to load AI twin")}})(),()=>{_=!0,R.current?.dispose(),H?.dispose(),O.current=null}},[jn,s,qe,t,a,l,i,P,Lr,Be,Y]),v.useEffect(()=>{const _={current:!1},U=()=>{const H=D.current,V=O.current,q=Ne(ne.current,ie.current);if(!H||!V||!j){_e.current=requestAnimationFrame(U);return}if(document.hidden){R.current?.setActive(!1),_e.current=requestAnimationFrame(U);return}const Ie=q.isSpeaking(),Re=q.isWarmingUp(),ht=Ie&&!B.current,rt=B.current&&!Ie;ht&&(xe(),Me(),ae.current=!1,z.current=!1,Oe(X.current),M.current?.reset(),De.current.reset(),Qe.current=void 0,ue.current=0,we.current="sil",g.current=0,Et()),rt&&(Q.current.reset(),R.current?.setActive(!1),M.current?.reset(),V.finishIdleToSilCrossfade(),tt());const pt=rt||ae.current||ee.current||te.current,It=L&&V.supportsBufferedPlayback;if(Re){if(Et(),y.current&&R.current?.setActive(!1),V.usesPixi&&V.isIdleToSilCrossfading()){V.tickIdleToSilCrossfade(performance.now()),q.getVisemeQueue().length>0&&J.current===0&&(J.current=performance.now()),B.current=q.isSpeaking(),_e.current=requestAnimationFrame(U);return}const oe=q.getVisemeQueue();oe.length>0&&J.current===0&&(J.current=performance.now()),Q.current.advance(performance.now());const $=Q.current.getExpressionPath(),fe=Q.current.getDrawKey(),be={queue:oe,elapsedMs:0,expressionPath:$,expressionDrawKey:fe,availablePaths:Ee.current};let Fe=!1;It&&M.current?(M.current.warmupTick(be),Fe=M.current.isWarmupReady(be,N.current)):V.usesPixi&&en(H,oe,0,$,fe,X.current,(st,At,zt,Pt)=>{V.drawLiveFrame(st,At,zt,Le=>{Pt?.(Le),q.isWarmingUp()&&Le!==""&&!Zr({lastDrawnKey:Le})&&(J.current=0,q.releaseWarmupHold())})},st=>{he.current?.(st.label)},Ee.current);const nt=J.current>0&&performance.now()-J.current>ss;he.current?.(Fe?"buffer ready":"buffering lipsync…"),(Fe||nt)&&(J.current=0,q.releaseWarmupHold()),B.current=q.isSpeaking(),_e.current=requestAnimationFrame(U);return}if(Ie){Et(),y.current&&R.current?.setActive(!1),V.usesPixi&&V.isIdleToSilCrossfading()&&V.finishIdleToSilCrossfade();const oe=q.getVisemeQueue(),$=q.getPlaybackElapsedMs();Vs(oe,$),g.current=$;const fe=oe.length>0?bt(oe,$):"sil";fe!=="sil"&&(we.current=fe);const be=Pi($,q.getSentenceEmotions(),q.getWordQueue(),q.getStreamMood());Q.current.setTargetEmotion(be),Q.current.setTargetEyebrow(Ti($,q.getWordQueue(),Ir(be))),Q.current.advance(performance.now());const Fe=Q.current.getExpressionPath(),nt=Q.current.getDrawKey(),st=(Le,jt,wt,Yt)=>{const Or={label:lt.current,elapsedMs:$,mouthPath:jt.mouthPath,transitionMeta:Qe.current};ft.current.set(wt,Or),M.current?.markExternallyDisplayed(wt,Fe),V.drawLiveFrame(Le,jt,wt,xt=>{const Yn=ft.current.get(xt)??Or;ft.current.delete(xt),De.current.onDrawDisplayed(Yn,oe),M.current?.markExternallyDisplayed(xt,Fe),Yt?.(xt)})},At=Le=>{lt.current=Le.label,Qe.current=Le.transitionMeta,he.current?.(Le.label)},zt=L&&V.supportsBufferedPlayback;let Pt=!1;zt&&M.current&&(Pt=M.current.tick({canvas:H,queue:oe,elapsedMs:$,expressionPath:Fe,expressionDrawKey:nt,playbackState:X.current,availablePaths:Ee.current,onStatus:At,onDisplayed:(jt,wt,Yt)=>{De.current.onDrawDisplayed({label:Yt,elapsedMs:$,mouthPath:wt,transitionMeta:Qe.current},oe)}}).usedBuffer),Pt||en(H,oe,$,Fe,nt,X.current,st,At,Ee.current),De.current.onRafTickSpeaking(),Pe.current?.(De.current.snapshot(oe,$))}else if(!pt&&y.current&&R.current){const oe=R.current,$=oe.getVideo();if($.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA){oe.setActive(!0);const fe=()=>!q.isSpeaking()&&!ae.current&&!ee.current;V.usesPixi?(V.drawIdleVideo($,fe),fe()&&he.current?.("idle")):_.current||(_.current=!0,tr(H,$,null,void 0,He.current,fe).then(()=>he.current?.("idle")).finally(()=>{_.current=!1}))}else tn(H,null,"__sil__",X.current,(fe,be,Fe,nt)=>V.drawLiveFrame(fe,be,Fe,nt),fe=>he.current?.(fe))}else pt||tn(H,null,"__sil__",X.current,(oe,$,fe,be)=>V.drawLiveFrame(oe,$,fe,be),oe=>he.current?.(oe));B.current=Ie,_e.current=requestAnimationFrame(U)};return _e.current=requestAnimationFrame(U),()=>{cancelAnimationFrame(_e.current),xe(),Me(),ne.current.stop(),ie.current?.stop()}},[j,Ae,tt,xe,Me,Et]),v.useImperativeHandle(k,()=>({speakText:async(_,U)=>{const H=_.trim();if(!H)return;const V=O.current;if(!V||!We.current&&!j)throw new Error("AI twin is not ready");const{ttsEngineId:q,voiceId:Ie}=mt(U);W(null),Z.current+=1,xe(),Me(),ae.current=!1,z.current=!1,we.current="sil",g.current=0,Oe(X.current),Q.current.reset(),B.current=!1,te.current=!1,V.finishIdleToSilCrossfade();try{await ne.current.speak(H,{voiceId:Ie,speakingRate:U?.speakingRate??f,ttsEngineId:q})}catch(Re){throw te.current=!1,V.finishIdleToSilCrossfade(),tt(),Be(Re instanceof Error?Re.message:"TTS failed"),Re}},stop:()=>{Z.current+=1,xe(),Me(),ae.current=!1,z.current=!1,te.current=!1,O.current?.finishIdleToSilCrossfade(),ne.current.stop(),ie.current?.stop(),Oe(X.current),M.current?.reset(),J.current=0,B.current=!1,tt()},setTtsEngineId:_=>{ne.current.setTtsEngineId(_),ie.current?.setTtsEngineId(_),me.current&&(me.current.ttsEngineId=_)},renderViseme:async(_,U)=>{const H=O.current,V=D.current;if(!H||!V||!We.current)throw new Error("AI twin is not ready");await H.renderViseme(V,_,{from:U?.from,transitionDurationMs:U?.transitionDurationMs,gapMs:U?.gapMs,onStatus:q=>he.current?.(q)})},isReady:()=>We.current,getStatus:()=>S,connect:async(_={})=>{W(null);try{let U=_.authToken?.trim();U||(U=ke.current??i??await Ft(),ke.current=U);const H=_.voiceId?.trim()??l??me.current?.voiceId,V=_.personaId?.trim()??me.current?.personaId,q=Br();await q.unlockAudio(),await q.connect({..._,apiBase:_.apiBase?.trim()||de,authToken:U,voiceId:H,personaId:V,speakingRate:_.speakingRate??f})}catch(U){throw Be(U instanceof Error?U.message:"Voice connect failed"),U}},disconnect:async()=>{const _=ie.current;_&&(Z.current+=1,xe(),Me(),ae.current=!1,z.current=!1,te.current=!1,O.current?.finishIdleToSilCrossfade(),Oe(X.current),M.current?.reset(),J.current=0,B.current=!1,await _.disconnect(),tt())},isConnected:()=>ie.current?.isConnected()??!1,getVoiceStream:()=>ie.current?.getVoiceStream()??ne.current?.getVoiceStream()??null,setMuted:_=>{ne.current.setMuted(_),ie.current?.setMuted(_)},isMuted:()=>ie.current?.isMuted()??ne.current.isMuted(),getPersonaId:()=>me.current?.personaId?.trim()||null}),[j,f,i,l,xe,Me,tt,Be,mt,Br,S,de]),Xt.jsxs("div",{className:c,style:{...ca,...u},children:[Xt.jsx("canvas",{ref:D,width:m.width,height:m.height,style:{...la,...d},"aria-label":t?`AI twin ${t}`:"AI twin face"}),b&&G?Xt.jsx("div",{style:fa,children:G}):null]})}),Gn="thumbnail.webp";function ma(e,t=mr){const r=e.trim();if(!r)throw new Error("faceId is required");return`${t.replace(/\/$/,"")}/${r}/${Gn}`}const ha=new Set([".png",".jpg",".jpeg",".webp"]);function zn(e=".png"){const t=e.toLowerCase();return ha.has(t)?`source${t}`:"source.png"}function pa(e,t=mr,r=".png"){const n=e.trim();if(!n)throw new Error("faceId is required");return`${t.replace(/\/$/,"")}/${n}/${zn(r)}`}exports.AI_TWIN_THUMBNAIL_FILENAME=Gn;exports.AiTwin=da;exports.AiTwinNotFoundError=Lt;exports.DEFAULT_TTS_ENGINE_ID=ze;exports.MIC_SAMPLE_RATE=Sr;exports.OCULUS_VISEME_IDS=Sn;exports.PLAYBACK_SAMPLE_RATE=vr;exports.SPEAKING_RATE_MAX=qt;exports.SPEAKING_RATE_MIN=$t;exports.TTS_ENGINE_CARTESIA=Ye;exports.TTS_ENGINE_GOOGLE=pr;exports.TTS_ENGINE_INWORLD=Ht;exports.VISEME_IDS=ri;exports.VISEME_TEST_CARTESIA_VOICE_ID=_r;exports.VISEME_TEST_SPEAKING_RATE=kt;exports.VISEME_TEST_VOICE_ID=yr;exports.aiTwinSourceFilename=zn;exports.buildVoiceWebSocketUrl=xn;exports.createAvatarTtsLipsyncController=ar;exports.createRealtimeVoiceLipsyncController=Mn;exports.faceAssetUrls=ir;exports.fetchAiTwin=dn;exports.fetchDevAuthToken=Ft;exports.getAiTwinSourceUrl=pa;exports.getAiTwinThumbnailUrl=ma;exports.normalizeOculusViseme=vn;exports.resolveDirectFaceId=mn;exports.resolveVisemeAtTime=bt;exports.resolveWordAtTime=Ws;exports.setActiveTwinAssets=hn;
|
|
47
|
+
`;function an(e=5){const t=new ne.Filter({glProgram:ne.GlProgram.from({vertex:ta,fragment:ra,name:"threshold-key-filter"}),resources:{thresholdUniforms:{uThreshold:{value:e/255,type:"f32"}}}});return t.setThreshold=r=>{t.resources.thresholdUniforms.uniforms.uThreshold=r/255},t.getThreshold=()=>Math.round(t.resources.thresholdUniforms.uniforms.uThreshold*255),t}function na(e,t,r){return e>r||t>r}function sa(e){return e?e.getParameter(e.MAX_TEXTURE_SIZE):4096}function ia(e,t){return t!=null&&t>0?Math.floor(t):sa(e)}function aa(e,t){const r=document.createElement("canvas");r.width=t.w,r.height=t.h;const n=r.getContext("2d");if(!n)throw new Error("Failed to acquire 2d context for atlas cell crop");return n.drawImage(e,t.x,t.y,t.w,t.h,0,0,t.w,t.h),r}function Yn(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 on(e,t,r,n,s){const i=r?await On(e,s):await fetch(e).then(c=>{if(!c.ok)throw new Error(`Failed to load ${e}`);return c.json()}),o=i.sheets?.[0];if(!o?.path)throw new Error(`${e} has no sheets[0].path`);const a=new Map,l=async(c,d)=>{const w=Vn(t,d,r),b=r?await ei(await pt(w,s??"")):await Yn(w);if(na(b.width,b.height,n))return console.info(`[PixiVisemeCompositor] Sheet ${d} (${b.width}x${b.height}) exceeds MAX_TEXTURE_SIZE=${n}; using per-cell crops.`),{mode:"cells",bitmap:b};const h=ne.Texture.from(b);return h.source.scaleMode="nearest",{mode:"full",texture:h}};a.set(o.index,await l(o.index,o.path));for(const c of i.sheets??[])c.index!==o.index&&c.path&&a.set(c.index,await l(c.index,c.path));const f=new Map;for(const c of i.cells??[])c.path&&f.set(c.path,c);const u=new Map;for(const c of i.sheets??[])u.set(c.index,c);return{atlasMeta:i,cellByPath:f,sheetByIndex:u,diffBase:t,sheets:a}}async function oa(e,t={}){const r=new ne.Application;try{await r.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(p){throw r.destroy({removeView:!1}),p}if(r.renderer.type!==ne.RendererType.WEBGL)throw r.destroy({removeView:!1}),new Error("Pixi did not initialize a WebGL renderer");const n=ia(r.renderer.gl,t.maxTextureSize);let s=!1,i=!1,o=!1;const a=r.canvas,l=()=>{s=!0,!i&&!o&&(i=!0,t.onWebGlContextLost?.())};a.addEventListener("webglcontextlost",l);const f=new ne.Sprite(ne.Texture.EMPTY),u=new ne.Sprite(ne.Texture.EMPTY);u.visible=!1,u.alpha=0;const c=new ne.Sprite(ne.Texture.EMPTY),d=new ne.Sprite(ne.Texture.EMPTY),w=new ne.Sprite(ne.Texture.EMPTY);w.visible=!1;const b=an(5),h=an(5);c.filters=[b],d.filters=[h],r.stage.addChild(f),r.stage.addChild(u),r.stage.addChild(c),r.stage.addChild(d),r.stage.addChild(w);let A=null,v=null,R=null,g=null,E=null,F=null,D=null,Y=842,O=1264,k=null,x=5;const M=new Map;let B=null,ee=null,V=null;const te=yn(()=>{M.clear(),F=null,D=null,A=null,k=null}),X=(p,S)=>{Y=p,O=S,r.renderer.resize(p,S),(e.width!==p||e.height!==S)&&(e.width=p,e.height=S)},z=()=>{c.visible=!1,d.visible=!1,c.texture=ne.Texture.EMPTY,d.texture=ne.Texture.EMPTY,B=null,ee=null},me=()=>{E=null,u.visible=!1,u.alpha=0},ae=()=>{f.visible=!0,f.width=Y,f.height=O,f.x=0,f.y=0},Q=()=>{u.width=Y,u.height=O,u.x=0,u.y=0},de=()=>{w.visible=!1,me(),A&&(f.texture=A,ae())},he=()=>{!E&&!u.visible||de()},Se=p=>{if(o||s||!A){de();return}const S=Math.max(0,p);if(!v||S<=0){de();return}z(),w.visible=!1,f.texture=v,ae(),u.texture=A,Q(),u.alpha=0,u.visible=!0,E={startMs:performance.now(),durationMs:S},H()},j=(p=performance.now())=>{if(!E)return!1;if(o||s)return he(),!1;const S=(p-E.startMs)/Math.max(1,E.durationMs);if(S>=1)return de(),H(),!1;const C=Math.min(1,Math.max(0,S)),le=C*C*(3-2*C);return u.alpha=le,u.visible=!0,H(),!0},y=()=>{if(R){try{R.resource=null}catch{}R.destroy(),R=null}v&&(v.destroy(!1),v=null),g=null},I=(p,S)=>{const C=p.cellByPath.get(S);if(!C||!Ln(C))return null;const le=`${p.diffBase}::${S}`;let oe=M.get(le);if(!oe){const pe=C.sheet??0,ye=p.sheets.get(pe);if(!ye)return null;let ve,wt=!1;if(ye.mode==="full")ve=new ne.Texture({source:ye.texture.source,frame:new ne.Rectangle(C.x,C.y,C.w,C.h)});else{const m=aa(ye.bitmap,C);ve=ne.Texture.from(m),ve.source.scaleMode="nearest",wt=!0}oe={texture:ve,ownsSource:wt},M.set(le,oe)}return{texture:oe.texture,cell:C}},U=(p,S,C,le)=>{if(!le||!C)return p.visible=!1,p.texture=ne.Texture.EMPTY,!1;const oe=I(C,le);if(!oe)return p.visible=!1,p.texture=ne.Texture.EMPTY,!1;const pe=Fn(C.atlasMeta,oe.cell);return S.setThreshold(x),p.texture=oe.texture,p.x=pe.x,p.y=pe.y,p.width=oe.cell.w,p.height=oe.cell.h,p.visible=!0,!0},G=p=>{if(!p||!F)return null;const S=k??new Set(F.cellByPath.keys());return Pr(p,S)??(S.has(p)?p:null)},$=p=>{w.visible=!1,de();const S=G(p.mouthPath),C=p.expressionPath&&D?Mr(p.expressionPath,le=>D.cellByPath.has(le)):null;S!==B?(U(c,b,F,S),B=S):S?c.visible=!0:c.visible=!1,C!==ee?(U(d,h,D,C),ee=C):C?d.visible=!0:d.visible=!1},H=()=>{if(!(o||s||document.hidden))try{r.render()}catch{}};return{async loadAssets(p){if(o)return{width:Y,height:O,visemePaths:[]};const S=kt();if(S&&!p)throw new Error("Encrypted assets enabled but no key provided");const C=S?await ri(await pt(Ht(),p??"")):await Yn(Ht());A&&A.destroy(!0),A=ne.Texture.from(C),A.source.scaleMode="linear";const le=_n();F=await on(Sn(),le,S,n,p);try{D=await on(vn(),le,S,n,p)}catch(pe){console.warn("[PixiVisemeCompositor] expression atlas unavailable:",pe),D=null}const oe=Bn(F.atlasMeta,C.width,C.height);return X(oe.width,oe.height),x=Jt(F.atlasMeta),b.setThreshold(x),h.setThreshold(x),k=new Set(F.cellByPath.keys()),M.clear(),de(),z(),H(),{width:oe.width,height:oe.height,visemePaths:[...F.cellByPath.keys()]}},getVisemeAtlasPaths(){return k},drawLiveFrame(p){o||s||!A||(he(),$(p),H())},drawIdleVideo(p,S){o||s||document.hidden||S&&!S()||p.readyState<HTMLMediaElement.HAVE_CURRENT_DATA||(me(),z(),w.visible=!1,!v||g!==p?(y(),R=new ne.VideoSource({resource:p,autoPlay:!1,updateFPS:0}),R.autoUpdate=!1,v=new ne.Texture({source:R}),g=p):v.source.update(),f.texture=v,ae(),H())},beginIdleToSilCrossfade:Se,tickIdleToSilCrossfade:j,finishIdleToSilCrossfade:he,isIdleToSilCrossfading(){return E!=null},renderSilOnly(){o||s||!A||(z(),de(),H())},blitBitmap(p){o||s||(he(),z(),V&&(V.destroy(!0),V=null),V=ne.Texture.from(p),w.texture=V,w.visible=!0,w.x=0,w.y=0,w.width=Y,w.height=O,f.visible=!1,H())},async compositePrefetchFrame(p){return null},renderFrame:H,isContextLost(){return s},dispose(){if(o)return;o=!0,a.removeEventListener("webglcontextlost",l),te(),y(),V&&(V.destroy(!0),V=null);for(const{texture:S,ownsSource:C}of M.values())S.destroy(C);M.clear(),A&&(A.destroy(!0),A=null);const p=S=>{S.mode==="full"?S.texture.destroy(!0):S.bitmap.close()};if(F)for(const S of F.sheets.values())p(S);if(D)for(const S of D.sheets.values())p(S);b.destroy(!0),h.destroy(!0),r.destroy({removeView:!1},{children:!0,texture:!0})}}}const cn=-1,Ut=1e5,un=new Map;async function ca(e){let t=un.get(e);if(!t){const r=await fetch(e);if(!r.ok)throw new Error(`Failed to fetch worker script (${r.status}): ${e}`);const n=await r.blob();t=URL.createObjectURL(n),un.set(e,t)}return new Worker(t,{name:"visemeDiffPreview"})}function ua(){return typeof Worker<"u"&&typeof OffscreenCanvas<"u"}function yr(e,t){(e.width!==t.width||e.height!==t.height)&&(e.width=t.width,e.height=t.height);const r=e.getContext("2d");r&&r.drawImage(t,0,0)}function ln(e,t){let r=0,n=null;const s=Or(async(i,o)=>(await wr(),await gr(e,i),s.isLatestSeq(o)));return{usesWorker:!1,usesPixi:!1,async loadAssets(i){const o=await wr(i);return n=o.visemePaths?new Set(o.visemePaths):null,o},getVisemeAtlasPaths(){return n},async renderSilOnly(){r+=1,s.reset(),await Ui(e)},drawLiveFrame(i,o,a,l){s.enqueue(o,a,l)},async compositePrefetchFrame(i,o){try{return await Ni(i)}catch{return null}},blitBitmap(i,o){yr(i,o)},drawIdleVideo(){},beginIdleToSilCrossfade(){},tickIdleToSilCrossfade(){return!1},finishIdleToSilCrossfade(){},isIdleToSilCrossfading(){return!1},supportsBufferedPlayback:!0,async renderViseme(i,o){r+=1;const a=r;t.onStatus?.(o),await gr(i,{mouthPath:Tt(o),expressionPath:null})},dispose(){r+=1,s.reset()}}}function la(e,t,r){let n=null,s=1,i=0,o=!1,a=!1,l=null;const f=new Map,u=new Map;let c=Ut;const d=Or(async(R,g,E)=>(f.set(g,{drawKey:E.drawKey,onDisplayed:E.onDisplayed}),await A({type:"drawFrame",diffPath:R.mouthPath,expressionDiffPath:R.expressionPath,liveSeq:g},void 0,!0),!1)),w=R=>{if(R===cn){let F=-1,D;for(const[Y,O]of f)Y>=F&&(F=Y,D=O);F>=0&&f.delete(F),D?.onDisplayed?.(D.drawKey);return}const g=-R,E=f.get(g);f.delete(g),E?.onDisplayed?.(E.drawKey)},b=new Map,h=(R,g)=>{a||!n||n.postMessage(R,[])},A=(R,g,E=!1)=>{if(a)return Promise.resolve();const F=s++;return new Promise((D,Y)=>{b.set(F,{resolve:O=>D(O),reject:Y,generation:typeof R.generation=="number"?R.generation:void 0,expectRenderDone:E}),h({...R,requestId:F})})},v=(async()=>{n=await ca(r),n.onmessage=R=>{if(a)return;const g=R.data;if(g.type==="status"){t.onStatus?.(g.label);return}if(g.type==="frame"){if(g.generation<=-Ut){const D=-g.generation,Y=u.get(D);u.delete(D),Y?.resolve(g.bitmap);return}const F=g.generation<0;if(!F&&g.generation!==i){g.bitmap.close();return}if(F&&!(g.generation===cn)){const Y=-g.generation;if(!d.isLatestSeq(Y)){Ye()?.onWorkerStale(),g.bitmap.close();return}}if(F&&t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()){Ye()?.onWorkerStale(),g.bitmap.close();return}yr(e,g.bitmap),g.bitmap.close(),F&&w(g.generation);return}const E=b.get(g.requestId);if(E)switch(g.type){case"ready":b.delete(g.requestId),E.resolve();break;case"loadAssetsDone":b.delete(g.requestId),l=g.visemePaths?.length?new Set(g.visemePaths):null,E.resolve({width:g.width,height:g.height,visemePaths:g.visemePaths});break;case"renderDone":if(E.expectRenderDone&&E.generation!=null&&g.generation!==E.generation)return;b.delete(g.requestId),E.resolve();break;case"renderAborted":b.delete(g.requestId),E.resolve();break;case"error":b.delete(g.requestId),t.onError?.(g.message),E.reject(new Error(g.message));break}},n.onerror=R=>{if(a)return;const g=R.message||"Worker error";t.onError?.(g);for(const[,E]of b)E.reject(new Error(g));b.clear()},await A({type:"init"}),o=!0})();return{usesWorker:!0,usesPixi:!1,async loadAssets(R){if(a)return{width:842,height:1264};if(await v,a)return{width:842,height:1264};if(!o)throw new Error("Worker init failed");return await A({type:"loadAssets",keyHex:R,urls:bs(nt())})??{width:842,height:1264}},getVisemeAtlasPaths(){return l},async renderSilOnly(){if(a||(await v,a))return;i+=1,d.reset(),f.clear(),u.forEach(({resolve:g})=>g(null)),u.clear(),c=Ut;const R=i;await A({type:"cancelLiveDraws"}),await A({type:"renderSil",generation:R},void 0,!0)},drawLiveFrame(R,g,E,F){a||v.then(()=>{a||d.enqueue(g,E,F)})},compositePrefetchFrame(R,g){return a?Promise.resolve(null):v.then(()=>{if(a)return null;const E=++c;return new Promise(F=>{u.set(E,{resolve:F}),A({type:"drawFrame",diffPath:R.mouthPath,expressionDiffPath:R.expressionPath,liveSeq:E},void 0,!0).catch(()=>{u.delete(E),F(null)})})})},blitBitmap(R,g){yr(R,g)},drawIdleVideo(){},beginIdleToSilCrossfade(){},tickIdleToSilCrossfade(){return!1},finishIdleToSilCrossfade(){},isIdleToSilCrossfading(){return!1},supportsBufferedPlayback:!0,async renderViseme(R,g,E={}){if(a||(await v,a))return;i+=1,d.reset(),f.clear();const F=i;try{await A({type:"cancelLiveDraws"}),await A({type:"renderViseme",generation:F,to:g,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,i+=1,d.reset(),f.clear(),u.forEach(({resolve:R})=>R(null)),u.clear(),c=Ut;for(const[,R]of b)R.resolve();b.clear(),n?.terminate(),n=null}}}}async function fa(e,t){const r=await oa(e,{onWebGlContextLost:t.onWebGlContextLost,maxTextureSize:t.pixiMaxTextureSize});let n=0,s=null,i=!1;const o=Or(async(a,l)=>i?!1:t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()?(Ye()?.onWorkerStale(),!1):(r.drawLiveFrame(a),o.isLatestSeq(l)));return{usesWorker:!1,usesPixi:!0,async loadAssets(a){if(i)return{width:842,height:1264};const l=await r.loadAssets(a);return s=l.visemePaths?.length?new Set(l.visemePaths):null,l},getVisemeAtlasPaths(){return s},async renderSilOnly(){i||(n+=1,o.reset(),r.renderSilOnly())},drawLiveFrame(a,l,f,u){i||o.enqueue(l,f,u)},async compositePrefetchFrame(a,l){if(i)return null;try{return await r.compositePrefetchFrame(a)}catch{return null}},blitBitmap(a,l){i||t.shouldAcceptLiveFrame&&!t.shouldAcceptLiveFrame()||r.blitBitmap(l)},drawIdleVideo(a,l){i||r.drawIdleVideo(a,l)},beginIdleToSilCrossfade(a){i||r.beginIdleToSilCrossfade(a)},tickIdleToSilCrossfade(a){return i?!1:r.tickIdleToSilCrossfade(a)},finishIdleToSilCrossfade(){i||r.finishIdleToSilCrossfade()},isIdleToSilCrossfading(){return i?!1:r.isIdleToSilCrossfading()},supportsBufferedPlayback:!1,async renderViseme(a,l){if(i)return;n+=1;const f=n;o.reset(),t.onStatus?.(l),r.drawLiveFrame({mouthPath:Tt(l),expressionPath:null})},dispose(){i||(i=!0,n+=1,o.reset(),r.dispose())}}}function fn(e,t){if(!ua())return ln(e,t);const r=as(t.workerScriptUrl);try{return la(e,t,r)}catch{return ln(e,t)}}function da(e,t={}){if(t.preferLegacyRenderer)return fn(e,t);let r=null,n=!1,s=null,i=!1;const o=()=>r?Promise.resolve(r):(s||(s=(async()=>{try{const a=await fa(e,t);if(i)throw a.dispose(),new Error("Renderer disposed during Pixi init");return n=!0,r=a,a}catch(a){if(i)throw a instanceof Error?a:new Error(String(a));return console.warn("[AiTwin] Pixi WebGL renderer unavailable, using Canvas2D fallback:",a),r=fn(e,t),n=!1,r}})()),s);return{get usesWorker(){return r?.usesWorker??!1},get usesPixi(){return n},async loadAssets(a){return(await o()).loadAssets(a)},getVisemeAtlasPaths(){return r?.getVisemeAtlasPaths()??null},async renderSilOnly(){return(await o()).renderSilOnly()},drawLiveFrame(a,l,f,u){o().then(c=>{i||c.drawLiveFrame(a,l,f,u)})},async compositePrefetchFrame(a,l){return(await o()).compositePrefetchFrame(a,l)},blitBitmap(a,l){r&&r.blitBitmap(a,l)},drawIdleVideo(a,l){r?.usesPixi&&r.drawIdleVideo(a,l)},beginIdleToSilCrossfade(a){r?.beginIdleToSilCrossfade(a)},tickIdleToSilCrossfade(a){return r?.tickIdleToSilCrossfade(a)??!1},finishIdleToSilCrossfade(){r?.finishIdleToSilCrossfade()},isIdleToSilCrossfading(){return r?.isIdleToSilCrossfading()??!1},get supportsBufferedPlayback(){return r?.supportsBufferedPlayback??!1},async renderViseme(a,l,f){return(await o()).renderViseme(a,l,f)},dispose(){i=!0,s=null,r?.dispose(),r=null}}}class ma extends Error{constructor(t,r){super(`${t} timed out after ${Math.round(r/1e3)}s`),this.name="LoadTimeoutError"}}function lr(e,t,r){return new Promise((n,s)=>{const i=globalThis.setTimeout(()=>{s(new ma(r,t))},t);e.then(o=>{globalThis.clearTimeout(i),n(o)},o=>{globalThis.clearTimeout(i),s(o)})})}const ha={position:"relative",display:"inline-block",lineHeight:0},pa={display:"block",maxWidth:"100%",height:"auto"},wa={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 St(e){return ds(e)?Ir:kr}function qe(e,t){return t?.isSpeaking()?t:e.isSpeaking()?e:t?.isConnected()?t:e}const ga=T.forwardRef(function({id:t,avatarId:r,faceId:n,assets:s,authToken:i,apiBase:o,ttsEngineId:a=Ze,voiceId:l,speakingRate:f=At,className:u,style:c,canvasStyle:d,onStatusChange:w,onDisplayStatus:b,onError:h,onReady:A,showErrorOverlay:v=!0,onUserTranscript:R,onBotOutput:g,onRealtimeLipsyncDebug:E,onFrameRenderStats:F,lipsyncBufferedPlayback:D=!0,lipsyncPlaybackWarmupMs:Y=1500,pixiMaxTextureSize:O},k){const x=T.useRef(null),M=T.useRef(null),B=T.useRef(null),ee=T.useRef(D);ee.current=D;const V=T.useRef(Y);V.current=Y;const te=T.useRef(0),X=T.useRef(Fr()),z=T.useRef(xi()),me=T.useRef(!1),ae=T.useRef(0),Q=T.useRef(0),de=T.useRef(0),he=T.useRef(null),Se=T.useRef(null),j=T.useRef(!1),y=T.useRef(!1),I=T.useRef(!1),U=T.useRef(!1),G=T.useRef("sil"),$=T.useRef(0),H=T.useRef(!1),p=T.useRef(null),S=T.useRef(!1),[C,le]=T.useState("idle"),[oe,pe]=T.useState(null),[ye,ve]=T.useState(!1),[wt,m]=T.useState(!1),[P,J]=T.useState({width:842,height:1264}),[ue,_e]=T.useState(0),Ge=T.useRef(!1),fe=T.useRef(i??null),ke=o?.trim()||Et,Ce=T.useRef(null),Te=T.useRef(null),Ne=T.useRef(!1),Be=T.useRef({width:842,height:1264}),Ue=T.useRef(null),xt=T.useRef("sil"),st=T.useRef(void 0),Ve=T.useRef(new Map),gt=T.useRef(A),ce=T.useRef(b),it=T.useRef(h),we=T.useRef(R),Ae=T.useRef(g),Ie=T.useRef(E),at=T.useRef(F),We=T.useRef(Wi());gt.current=A,ce.current=b,it.current=h,we.current=R,Ae.current=g,Ie.current=E,at.current=F,T.useEffect(()=>{const _=We.current;return en(_),()=>en(null)},[]);const Pt=T.useCallback(_=>{const L=_?.ttsEngineId??a??Te.current?.ttsEngineId??Ze,W=_?.voiceId??l??Te.current?.voiceId??St(L);return{ttsEngineId:L,voiceId:W}},[a,l]),Fe=T.useCallback(_=>{pe(_),it.current?.(_)},[]),yt=T.useCallback(_=>{le(_),w?.(_)},[w]),se=T.useRef(hr(yt,ke,{onLipsyncDebug:_=>Ie.current?.(_)})),ie=T.useRef(null);T.useEffect(()=>(se.current=hr(yt,ke,{onLipsyncDebug:_=>Ie.current?.(_)}),i&&se.current.setDeveloperToken(i),se.current.setTtsEngineId(a),()=>{se.current.stop();const _=ie.current;_&&(_.disconnect(),ie.current=null)}),[yt,i,a,ke]);const Nr=T.useCallback(()=>(ie.current||(ie.current=Dn(yt,{speakingRate:f,onUserTranscript:(_,L)=>we.current?.(_,L),onBotOutput:_=>Ae.current?.(_),onLipsyncDebug:_=>Ie.current?.(_)}),ie.current.setTtsEngineId(a)),ie.current),[yt,f,a]),xe=T.useCallback(()=>{he.current!=null&&(clearTimeout(he.current),he.current=null)},[]),Pe=T.useCallback(()=>{Se.current!=null&&(clearTimeout(Se.current),Se.current=null),I.current=!1},[]),Rt=T.useCallback(()=>{if(U.current)return;const _=M.current;if(U.current=!0,Pe(),j.current=!1,y.current=!1,!S.current||!p.current){_?.renderSilOnly();return}p.current.setActive(!1);const L=p.current.getVideo();if(_?.usesPixi){_.drawIdleVideo(L),_.beginIdleToSilCrossfade(rn);return}(async()=>{const W=x.current;W&&await cr(W,L,null,void 0,Be.current).catch(()=>{}),await new Promise(N=>setTimeout(N,rn)),U.current&&await _?.renderSilOnly()})()},[Pe]),Ur=T.useCallback(()=>{!S.current||!p.current||(U.current=!1,M.current?.finishIdleToSilCrossfade(),Pe(),I.current=!0,Se.current=setTimeout(()=>{if(Se.current=null,qe(se.current,ie.current).isSpeaking()){I.current=!1;return}if(j.current){I.current=!1;return}const _=p.current;if(!_){I.current=!1;return}_.restart(),_.setActive(!0);const L=_.getVideo();let W=!1;const N=()=>{W||(W=!0,L.removeEventListener("playing",K),I.current=!1)},K=()=>{N()};if(L.addEventListener("playing",K,{once:!0}),L.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA&&!L.paused){N();return}setTimeout(N,300)},$i))},[Pe]),ot=T.useCallback(()=>{if(xe(),ce.current?.("sil"),j.current=!1,U.current=!1,!S.current||!p.current){I.current=!1;return}Ur()},[xe,Ur]),Zt=T.useCallback(async _=>{const L=Q.current,W=M.current,N=x.current,K=()=>{_?.fallbackOnly||(j.current=!1)};if(!W||!N||!ye){K();return}if(qe(se.current,ie.current).isSpeaking()){K();return}if(_?.fallbackOnly){if(!j.current||(Me(X.current),await W.renderSilOnly(),L!==Q.current)||qe(se.current,ie.current).isSpeaking())return;ot();return}if(nn(X.current)){Me(X.current),ot();return}if(ee.current&&W.supportsBufferedPlayback&&!_?.fallbackOnly){if(Me(X.current),B.current?.reset(),await W.renderSilOnly(),L!==Q.current||qe(se.current,ie.current).isSpeaking())return;ot();return}const Ee=zi(qe(se.current,ie.current).getVisemeQueue(),$.current,G.current),$e=ji(Ee,Ue.current);if(Me(X.current),!$e){if(await W.renderSilOnly(),L!==Q.current||qe(se.current,ie.current).isSpeaking())return;ot();return}try{await W.renderViseme(N,"sil",{from:Ee,transitionDurationMs:Gn})}catch(He){if(He?.name!=="AbortError")throw He}if(L!==Q.current){K();return}if(qe(se.current,ie.current).isSpeaking()){K();return}ot()},[ye,ot]),ct=T.useCallback(()=>{if(y.current||j.current)return;y.current=!0,j.current=!0,xe();const _=Q.current;Zt().finally(()=>{y.current=!1}),he.current=setTimeout(()=>{he.current=null,_===Q.current&&Zt({fallbackOnly:!0})},Yi())},[xe,Zt]),Vr=T.useCallback(async()=>{const _=Math.floor(Date.now()/1e3);if(Ce.current&&_<Ce.current.expiresAt-60)return Ce.current.key;const L=fe.current??await Vt();fe.current=L,se.current.setDeveloperToken(L);const W=await _s(L,ke);return Ce.current={key:W.value,expiresAt:W.expiresAt},W.value},[ke]);T.useEffect(()=>{i&&(fe.current=i,se.current.setDeveloperToken(i))},[i]),T.useEffect(()=>{se.current.setTtsEngineId(a),ie.current?.setTtsEngineId(a),Te.current&&(Te.current.ttsEngineId=a)},[a]),T.useEffect(()=>{const _=l??St(a);Te.current&&(Te.current.voiceId=_)},[l,a]);const Xe=T.useMemo(()=>wn(r,n),[r,n]),Qn=T.useMemo(()=>s?JSON.stringify(s):Xe?`face:${Xe}`:`id:${t??""}`,[s,Xe,t]);return T.useEffect(()=>{if(!t&&!s&&!Xe){Fe("AiTwin requires `assets`, `avatarId`/`faceId`, or `id`");return}let _=!1;Ne.current=!1,ve(!1),m(!1),pe(null),Te.current=null,p.current=null,S.current=!1;const L=x.current;if(!L)return;let W=null;return(async()=>{try{let N,K,be,Ee;if(s)N=s,K=a,be=l??St(K);else if(Xe)ce.current?.("loadFace"),N=mr(Xe),K=a,be=l??St(K);else{ce.current?.("getAiTwin");const q=await lr(pn(t),3e4,"getAiTwin");if(_)return;if(!q.faceId?.trim())throw new Error("getAiTwin returned no faceId");K=q.ttsEngineId,be=l??q.voiceId??St(K),Ee=q.knowledgeContextId?.trim()||void 0,N=mr(q.faceId)}Te.current={ttsEngineId:K,voiceId:be,personaId:Ee},gn(N),p.current=Pi();const $e=!!(s&&!rt(s));W=da(L,{preferLegacyRenderer:$e,pixiMaxTextureSize:O,onStatus:q=>{_||ce.current?.(q)},onError:q=>{_||Fe(q)},shouldAcceptLiveFrame:()=>{if(me.current)return!0;if(j.current)return!1;const q=qe(se.current,ie.current);return q.isSpeaking()||q.isWarmingUp()},onWebGlContextLost:()=>{_||Ge.current||(Ge.current=!0,queueMicrotask(()=>{_||_e(q=>q+1)}))}}),M.current=W,B.current=ea(W),Ge.current=!1,(q=>{const Z=()=>{if(!q||V.current<=0)return!1;const ge=M.current;return ge?ee.current&&ge.supportsBufferedPlayback?!0:ge.usesPixi:!1};se.current.setLipsyncWarmupHold(q,Z),ie.current?.setLipsyncWarmupHold(q,Z)})(!0),ce.current?.("auth");const ze=fe.current??i??await lr(Vt(),3e4,"getAuthToken");if(_)return;fe.current=ze,se.current.setDeveloperToken(ze),se.current.setTtsEngineId(K);const Mt=kt()?await Vr():void 0;if(_)return;ce.current?.("loadAssets");const re=await lr(W.loadAssets(Mt),18e4,"load face assets");if(!_&&re.width>0&&re.height>0&&(Be.current=re,J(re)),_)return;Ue.current=W.getVisemeAtlasPaths(),Ne.current=!0,ve(!0),gt.current?.(),W.renderSilOnly().then(()=>{_||ce.current?.("sil")}),(async()=>{try{const q=await ii(Mt);if(await p.current.load(q),_)return;S.current=!0,m(!0),Me(X.current),p.current.setActive(!0);const Z=p.current.getVideo();W.usesPixi?W.drawIdleVideo(Z):await cr(L,Z,null,void 0,Be.current),_||ce.current?.("idle")}catch(q){if(_)return;S.current=!1,m(!1),console.warn("[AiTwin] Idle video unavailable:",q)}})()}catch(N){if(_)return;if(N instanceof Wt){Fe(N.message);return}Fe(N instanceof Error?N.message:"Failed to load AI twin")}})(),()=>{_=!0,p.current?.dispose(),W?.dispose(),M.current=null}},[Qn,s,Xe,t,a,l,i,O,Vr,Fe,ue]),T.useEffect(()=>{const _={current:!1},L=()=>{const W=x.current,N=M.current,K=qe(se.current,ie.current);if(!W||!N||!ye){ae.current=requestAnimationFrame(L);return}if(document.hidden){p.current?.setActive(!1),ae.current=requestAnimationFrame(L);return}const be=K.isSpeaking(),Ee=K.isWarmingUp(),$e=be&&!H.current,He=H.current&&!be;$e&&(xe(),Pe(),j.current=!1,y.current=!1,Me(X.current),B.current?.reset(),We.current.reset(),st.current=void 0,de.current=0,G.current="sil",$.current=0,Rt()),He&&(z.current.reset(),p.current?.setActive(!1),B.current?.reset(),N.finishIdleToSilCrossfade(),ct());const ze=He||j.current||I.current||U.current,Mt=D&&N.supportsBufferedPlayback;if(Ee){if(Rt(),S.current&&p.current?.setActive(!1),N.usesPixi&&N.isIdleToSilCrossfading()){N.tickIdleToSilCrossfade(performance.now()),K.getVisemeQueue().length>0&&te.current===0&&(te.current=performance.now()),H.current=K.isSpeaking(),ae.current=requestAnimationFrame(L);return}const re=K.getVisemeQueue();re.length>0&&te.current===0&&(te.current=performance.now()),z.current.advance(performance.now());const q=z.current.getExpressionPath(),Z=z.current.getDrawKey(),ge={queue:re,elapsedMs:0,expressionPath:q,expressionDrawKey:Z,availablePaths:Ue.current};let Re=!1;Mt&&B.current?(B.current.warmupTick(ge),Re=B.current.isWarmupReady(ge,V.current)):N.usesPixi&&sn(W,re,0,q,Z,X.current,(ut,Ct,er,Dt)=>{N.drawLiveFrame(ut,Ct,er,Le=>{Dt?.(Le),K.isWarmingUp()&&Le!==""&&!nn({lastDrawnKey:Le})&&(te.current=0,K.releaseWarmupHold())})},ut=>{ce.current?.(ut.label)},Ue.current);const je=te.current>0&&performance.now()-te.current>cs;ce.current?.(Re?"buffer ready":"buffering lipsync…"),(Re||je)&&(te.current=0,K.releaseWarmupHold()),H.current=K.isSpeaking(),ae.current=requestAnimationFrame(L);return}if(be){Rt(),S.current&&p.current?.setActive(!1),N.usesPixi&&N.isIdleToSilCrossfading()&&N.finishIdleToSilCrossfade();const re=K.getVisemeQueue(),q=K.getPlaybackElapsedMs();qs(re,q),$.current=q;const Z=re.length>0?It(re,q):"sil";Z!=="sil"&&(G.current=Z);const ge=Di(q,K.getSentenceEmotions(),K.getWordQueue(),K.getStreamMood());z.current.setTargetEmotion(ge),z.current.setTargetEyebrow(Ai(q,K.getWordQueue(),et(ge))),z.current.advance(performance.now());const Re=z.current.getExpressionPath(),je=z.current.getDrawKey(),ut=(Le,tr,_t,rr)=>{const Wr={label:xt.current,elapsedMs:q,mouthPath:tr.mouthPath,transitionMeta:st.current};Ve.current.set(_t,Wr),B.current?.markExternallyDisplayed(_t,Re),N.drawLiveFrame(Le,tr,_t,Bt=>{const Zn=Ve.current.get(Bt)??Wr;Ve.current.delete(Bt),We.current.onDrawDisplayed(Zn,re),B.current?.markExternallyDisplayed(Bt,Re),rr?.(Bt)})},Ct=Le=>{xt.current=Le.label,st.current=Le.transitionMeta,ce.current?.(Le.label)},er=D&&N.supportsBufferedPlayback;let Dt=!1;er&&B.current&&(Dt=B.current.tick({canvas:W,queue:re,elapsedMs:q,expressionPath:Re,expressionDrawKey:je,playbackState:X.current,availablePaths:Ue.current,onStatus:Ct,onDisplayed:(tr,_t,rr)=>{We.current.onDrawDisplayed({label:rr,elapsedMs:q,mouthPath:_t,transitionMeta:st.current},re)}}).usedBuffer),Dt||sn(W,re,q,Re,je,X.current,ut,Ct,Ue.current),We.current.onRafTickSpeaking(),at.current?.(We.current.snapshot(re,q))}else if(!ze&&me.current){p.current?.setActive(!1),z.current.advance(performance.now());const re=z.current.getExpressionPath(),q=z.current.getDrawKey();ur(W,re,q,X.current,(Z,ge,Re,je)=>N.drawLiveFrame(Z,ge,Re,je),Z=>{const ge=z.current.getTargetEmotion();ce.current?.(Z==="sil"||Z==="blink"?`${ge}${Z==="blink"?" · blink":""}`:Z)})}else if(!ze&&S.current&&p.current){const re=p.current,q=re.getVideo();if(q.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA){re.setActive(!0);const Z=()=>!K.isSpeaking()&&!j.current&&!I.current&&!me.current;N.usesPixi?(N.drawIdleVideo(q,Z),Z()&&ce.current?.("idle")):_.current||(_.current=!0,cr(W,q,null,void 0,Be.current,Z).then(()=>ce.current?.("idle")).finally(()=>{_.current=!1}))}else ur(W,null,"__sil__",X.current,(Z,ge,Re,je)=>N.drawLiveFrame(Z,ge,Re,je),Z=>ce.current?.(Z))}else ze||ur(W,null,"__sil__",X.current,(re,q,Z,ge)=>N.drawLiveFrame(re,q,Z,ge),re=>ce.current?.(re));H.current=be,ae.current=requestAnimationFrame(L)};return ae.current=requestAnimationFrame(L),()=>{cancelAnimationFrame(ae.current),xe(),Pe(),se.current.stop(),ie.current?.stop()}},[ye,wt,ct,xe,Pe,Rt]),T.useImperativeHandle(k,()=>({speakText:async(_,L)=>{const W=_.trim();if(!W)return;const N=M.current;if(!N||!Ne.current&&!ye)throw new Error("AI twin is not ready");const{ttsEngineId:K,voiceId:be}=Pt(L);pe(null),Q.current+=1,xe(),Pe(),j.current=!1,y.current=!1,G.current="sil",$.current=0,Me(X.current),me.current=!1,z.current.reset(),H.current=!1,U.current=!1,N.finishIdleToSilCrossfade();try{await se.current.speak(W,{voiceId:be,speakingRate:L?.speakingRate??f,ttsEngineId:K})}catch(Ee){throw U.current=!1,N.finishIdleToSilCrossfade(),ct(),Fe(Ee instanceof Error?Ee.message:"TTS failed"),Ee}},stop:()=>{Q.current+=1,xe(),Pe(),j.current=!1,y.current=!1,U.current=!1,M.current?.finishIdleToSilCrossfade(),se.current.stop(),ie.current?.stop(),Me(X.current),B.current?.reset(),te.current=0,H.current=!1,ct()},setTtsEngineId:_=>{se.current.setTtsEngineId(_),ie.current?.setTtsEngineId(_),Te.current&&(Te.current.ttsEngineId=_)},renderViseme:async(_,L)=>{const W=M.current,N=x.current;if(!W||!N||!Ne.current)throw new Error("AI twin is not ready");await W.renderViseme(N,_,{from:L?.from,transitionDurationMs:L?.transitionDurationMs,gapMs:L?.gapMs,onStatus:K=>ce.current?.(K)})},setEmotion:_=>{if(!Ne.current)throw new Error("AI twin is not ready");const L=M.current,W=x.current;if(!L||!W)throw new Error("AI twin is not ready");Q.current+=1,xe(),Pe(),j.current=!1,y.current=!1,I.current=!1,U.current=!1,se.current.stop(),ie.current?.stop(),Me(X.current),B.current?.reset(),te.current=0,H.current=!1,L.finishIdleToSilCrossfade(),p.current?.setActive(!1);const N=Ke(_);me.current=!0,z.current.snapToEmotion(N);const K=z.current.getExpressionPath(),be=z.current.getDrawKey(),Ee={mouthPath:null,expressionPath:K},$e=Qt(Ee,be);X.current.lastDrawnKey="",L.drawLiveFrame(W,Ee,$e,ze=>{ze===$e&&(X.current.lastDrawnKey=$e)});const He=et(N);ce.current?.(K?`${N} · brow ${He>0?"+":""}${He}`:`${N} · brow 0 (sil base)`)},getEmotion:()=>me.current?z.current.getTargetEmotion():null,clearEmotionPreview:()=>{me.current=!1,z.current.reset(),Me(X.current),ce.current?.("idle")},isReady:()=>Ne.current,getStatus:()=>C,connect:async(_={})=>{pe(null);try{let L=_.authToken?.trim();L||(L=fe.current??i??await Vt(),fe.current=L);const W=_.voiceId?.trim()??l??Te.current?.voiceId,N=_.personaId?.trim()??Te.current?.personaId,K=Nr();await K.unlockAudio(),await K.connect({..._,apiBase:_.apiBase?.trim()||ke,authToken:L,voiceId:W,personaId:N,speakingRate:_.speakingRate??f})}catch(L){throw Fe(L instanceof Error?L.message:"Voice connect failed"),L}},disconnect:async()=>{const _=ie.current;_&&(Q.current+=1,xe(),Pe(),j.current=!1,y.current=!1,U.current=!1,M.current?.finishIdleToSilCrossfade(),Me(X.current),B.current?.reset(),te.current=0,H.current=!1,await _.disconnect(),ct())},isConnected:()=>ie.current?.isConnected()??!1,getVoiceStream:()=>ie.current?.getVoiceStream()??se.current?.getVoiceStream()??null}),[ye,f,i,l,xe,Pe,ct,Fe,Pt,Nr,C,ke]),nr.jsxs("div",{className:u,style:{...ha,...c},children:[nr.jsx("canvas",{ref:x,width:P.width,height:P.height,style:{...pa,...d},"aria-label":t?`AI twin ${t}`:"AI twin face"}),v&&oe?nr.jsx("div",{style:wa,children:oe}):null]})}),Xn="thumbnail.webp";function ya(e,t=Sr){const r=e.trim();if(!r)throw new Error("faceId is required");return`${t.replace(/\/$/,"")}/${r}/${Xn}`}const _a=new Set([".png",".jpg",".jpeg",".webp"]);function Jn(e=".png"){const t=e.toLowerCase();return _a.has(t)?`source${t}`:"source.png"}function Sa(e,t=Sr,r=".png"){const n=e.trim();if(!n)throw new Error("faceId is required");return`${t.replace(/\/$/,"")}/${n}/${Jn(r)}`}exports.AI_TWIN_THUMBNAIL_FILENAME=Xn;exports.AiTwin=ga;exports.AiTwinNotFoundError=Wt;exports.DEFAULT_TTS_ENGINE_ID=Ze;exports.EMOTION_TO_EYEBROW=Wn;exports.EXPRESSION_ATLAS_EMOTIONS=yi;exports.MIC_SAMPLE_RATE=Ar;exports.OCULUS_VISEME_IDS=bn;exports.PLAYBACK_SAMPLE_RATE=xr;exports.SPEAKING_RATE_MAX=Xt;exports.SPEAKING_RATE_MIN=Yt;exports.TTS_ENGINE_CARTESIA=tt;exports.TTS_ENGINE_GOOGLE=Tr;exports.TTS_ENGINE_INWORLD=jt;exports.VISEME_IDS=ai;exports.VISEME_TEST_CARTESIA_VOICE_ID=Ir;exports.VISEME_TEST_SPEAKING_RATE=At;exports.VISEME_TEST_VOICE_ID=kr;exports.aiTwinSourceFilename=Jn;exports.buildVoiceWebSocketUrl=Cn;exports.createAvatarTtsLipsyncController=hr;exports.createRealtimeVoiceLipsyncController=Dn;exports.emotionToEyebrow=et;exports.faceAssetUrls=mr;exports.fetchAiTwin=pn;exports.fetchDevAuthToken=Vt;exports.getAiTwinSourceUrl=Sa;exports.getAiTwinThumbnailUrl=ya;exports.normalizeOculusViseme=En;exports.normalizeStreamEmotion=Ke;exports.resolveDirectFaceId=wn;exports.resolveVisemeAtTime=It;exports.resolveWordAtTime=Ks;exports.setActiveTwinAssets=gn;
|