@streamoji/avatar-widget 0.5.2 → 0.5.6
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/avatar-widget.cjs +6 -6
- package/dist/avatar-widget.js +871 -856
- package/dist/avatar-widget.umd.js +158 -158
- package/package.json +1 -1
package/dist/avatar-widget.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react/jsx-runtime"),gt=require("@react-three/drei"),It=require("@react-three/fiber"),t=require("react"),wn=require("three"),Sn=require("three/examples/jsm/loaders/GLTFLoader.js");function yn(f){const w=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(f){for(const b in f)if(b!=="default"){const S=Object.getOwnPropertyDescriptor(f,b);Object.defineProperty(w,b,S.get?S:{enumerable:!0,get:()=>f[b]})}}return w.default=f,Object.freeze(w)}const rt=yn(wn),Xe="https://ai.streamoji.com",U=(...f)=>{},Rn=(...f)=>{},Jt=({analyser:f})=>{const w=t.useRef(null),b=t.useRef(null);return t.useEffect(()=>{const S=w.current;if(!S)return;const V=S.getContext("2d",{alpha:!0});if(!V)return;let Y,X=null;f&&(f.fftSize=128,X=new Uint8Array(f.frequencyBinCount));const ze=()=>{Y=requestAnimationFrame(ze),(S.width!==S.offsetWidth||S.height!==S.offsetHeight)&&(S.width=S.offsetWidth,S.height=S.offsetHeight);const Fe=S.width,C=S.height;if(Fe===0||C===0)return;const Ze=C/2;V.clearRect(0,0,Fe,C),V.fillStyle="#1e293b";const Se=2,$=Se+2,xe=Fe*.95,G=Math.floor(xe/$);(!b.current||b.current.length!==G)&&(b.current=new Float32Array(G).fill(2));const oe=G*$,M=(Fe-oe)/2;f&&X&&f.getByteFrequencyData(X);const le=X?X.length:0,_=Math.floor(le*.7)/G,ne=new Float32Array(G);for(let p=0;p<G;p++){let D=0;if(X&&_>0){const A=Math.floor(p*_),F=Math.floor((p+1)*_);for(let q=A;q<F;q++){const K=X[q]||0;K>D&&(D=K)}}D<10&&(D=0);const J=D/255,E=D>0?Math.max(2,Math.pow(J,1.4)*C*.25):2;ne[p]=E}for(let p=0;p<G;p++){const D=G-1-p,J=Math.max(ne[p],ne[D]),E=b.current[p]+(J-b.current[p])*.3;b.current[p]=E;const A=M+p*$,F=Ze-E/2;V.beginPath(),V.roundRect?V.roundRect(A,F,Se,E,4):V.fillRect(A,F,Se,E),V.fill()}};return ze(),()=>{cancelAnimationFrame(Y)}},[f]),l.jsx("canvas",{ref:w,style:{width:"100%",height:"100%",display:"block"}})},Ln="https://pub-48df6f7d60d6440bbd01676ea5d90e55.r2.dev",qt="https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/default-models/avatar-blue-suit.glb";function vn(f){const[w,b]=t.useState(null);return t.useEffect(()=>{if(!f){b(null);return}let S=!1;const V=`${Ln}/${f}.glb`;return fetch(V,{method:"HEAD"}).then(Y=>{S||b(Y.ok?V:qt)}).catch(()=>{S||b(qt)}),()=>{S=!0}},[f]),w}const xn=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/M_Standing_Idle_Variations_001.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/M_Standing_Idle_Variations_002.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/M_Standing_Idle_002.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/M_Standing_Idle_Variations_005.glb"],kn=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/F_Standing_Idle_Variations_001.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/F_Standing_Idle_Variations_003.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/F_Standing_Idle_Variations_003.glb"],En=[{id:"m_expr_01",name:"Friendly Wave",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_001.glb"},{id:"m_expr_02",name:"You There",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_002.glb"},{id:"m_expr_04",name:"Awkward Agreement",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_004.glb"},{id:"m_expr_05",name:"What's Going On?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_005.glb"},{id:"m_expr_06",name:"Tired Stretch",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_006.glb"},{id:"m_expr_07",name:"Conceilied Laughter",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_007.glb"},{id:"m_expr_08",name:"You Come Here",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_008.glb"},{id:"m_expr_09",name:"Come Here Kid",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_009.glb"},{id:"m_expr_10",name:"Come Here Everyone",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_010.glb"},{id:"m_expr_11",name:"No Freaking Way",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_011.glb"},{id:"m_expr_12",name:"Cheerful Approval",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_012.glb"},{id:"m_expr_13",name:"Waving Hello",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_013.glb"},{id:"m_expr_14",name:"Checking Surroundings",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_014.glb"},{id:"m_expr_15",name:"Referee Warning",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_015.glb"},{id:"m_expr_16",name:"You Thumbs Down",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_016.glb"},{id:"m_expr_17",name:"Side Thumbs Down",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_017.glb"},{id:"m_expr_18",name:"You're Finished",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_018.glb"},{id:"m_talk_01",name:"Oh God, Why Me?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_001.glb"},{id:"m_talk_02",name:"What Are You Doing?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_002.glb"},{id:"m_talk_03",name:"What Am I doing?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_003.glb"},{id:"m_talk_04",name:"No Way",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_004.glb"},{id:"m_talk_05",name:"What's Going On?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_005.glb"},{id:"m_talk_06",name:"I have no idea",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_006.glb"},{id:"m_talk_07",name:"What's going on here?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_007.glb"},{id:"m_talk_08",name:"Let's stop",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_008.glb"},{id:"m_talk_09",name:"Fed Up Moment",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_009.glb"},{id:"m_talk_10",name:"What's This? Hold On",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_010.glb"},{id:"f_talk_01",name:"Great Job Clap",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_001.glb"},{id:"f_talk_02",name:"Chill Stretch",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_002.glb"},{id:"f_talk_03",name:"This Is Me",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_003.glb"},{id:"f_talk_04",name:"Empathize",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_004.glb"},{id:"f_talk_05",name:"Loose Hands Stretch",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_005.glb"},{id:"f_talk_06",name:"Take It Easy",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_006.glb"}],Tn=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_005.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_007.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_006.glb"],Mn=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_004.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_005.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_006.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_002.glb"],An={zoom:.85,position:[.15,-.8,0],scale:1.5,rotation:[.15,.02,0]},In=[-.45,1.9,.1],Dn=[-.45,1.75,.1],On={browInnerUp:.2},Et=.18,Cn=1,Un=.5,zt=3,dt=.55,Tt=.12,Yt=2,Kt=2,mt=4,Qt=8,Fn=1,Xt=.38,Zt=.32,en="__branding__",pt={neutral:{eyeLookDownLeft:.1,eyeLookDownRight:.1},happy:{mouthSmileLeft:.2,mouthSmileRight:.2,eyeLookDownLeft:.1,eyeLookDownRight:.1},sad:{eyeLookDownLeft:.2,eyeLookDownRight:.2,browDownRight:.1,browInnerUp:.6,browOuterUpRight:.2,eyeSquintLeft:.7,eyeSquintRight:.7,mouthFrownLeft:.8,mouthFrownRight:.8,mouthLeft:.2,mouthPucker:.5,mouthRollLower:.2,mouthRollUpper:.2,mouthShrugLower:.2,mouthShrugUpper:.2,mouthStretchLeft:.4},angry:{eyeLookDownLeft:.1,eyeLookDownRight:.1,browDownLeft:.6,browDownRight:.6,jawForward:.3,mouthFrownLeft:.7,mouthFrownRight:.7,mouthRollLower:.2,mouthShrugLower:.3},fear:{browInnerUp:.7,eyeSquintLeft:.5,eyeSquintRight:.5,eyeWideLeft:.6,eyeWideRight:.6,mouthClose:.1,mouthFunnel:.3,mouthShrugLower:.5,mouthShrugUpper:.5},disgust:{browDownLeft:.7,browDownRight:.1,browInnerUp:.3,eyeSquintLeft:1,eyeSquintRight:1,eyeWideLeft:.5,eyeWideRight:.5,mouthLeft:.4,mouthPressLeft:.3,mouthRollLower:.3,mouthShrugLower:.3,mouthShrugUpper:.8,mouthUpperUpLeft:.3,noseSneerLeft:1,noseSneerRight:.7},love:{browInnerUp:.4,browOuterUpLeft:.2,browOuterUpRight:.2,mouthSmileLeft:.2,mouthSmileRight:.2,eyeBlinkLeft:.6,eyeBlinkRight:.6,eyeWideLeft:.7,eyeWideRight:.7,mouthDimpleLeft:.1,mouthDimpleRight:.1,mouthPressLeft:.2,mouthShrugUpper:.2,mouthUpperUpLeft:.1,mouthUpperUpRight:.1}},Nn=[{key:"viseme_aa",mix:{jawOpen:.4,mouthSmileLeft:.15,mouthSmileRight:.15},teethMix:{jawOpen:.2,mouthSmileLeft:.15,mouthSmileRight:.15}},{key:"viseme_E",mix:{mouthPressLeft:.65,mouthPressRight:.65,mouthDimpleLeft:1,mouthDimpleRight:1,jawOpen:.25,mouthFunnel:.1},teethMix:{jawOpen:.15,mouthPressLeft:.65,mouthPressRight:.65,mouthDimpleLeft:1,mouthDimpleRight:1,mouthFunnel:.1}},{key:"viseme_I",mix:{mouthPressLeft:.55,mouthPressRight:.55,mouthDimpleLeft:1,mouthDimpleRight:1,jawOpen:.15,mouthFunnel:.05},teethMix:{mouthPressLeft:.55,mouthPressRight:.55,mouthDimpleLeft:1,mouthDimpleRight:1,jawOpen:.15,mouthFunnel:.05}},{key:"viseme_O",mix:{mouthPucker:.7,jawOpen:.4,mouthFunnel:.25},teethMix:{jawForward:.6,jawOpen:.2,mouthPucker:.7,mouthFunnel:.25}},{key:"viseme_U",mix:{mouthFunnel:1,mouthClose:.2},teethMix:{mouthFunnel:1,mouthClose:.2}},{key:"viseme_PP",mix:{mouthDimpleLeft:.5,mouthDimpleRight:.5,mouthStretchLeft:.3,mouthStretchRight:.3,mouthRollLower:.3,mouthRollUpper:.3,mouthPressLeft:.5,mouthPressRight:.5},teethMix:{mouthDimpleLeft:.5,mouthDimpleRight:.5,mouthStretchLeft:.3,mouthStretchRight:.3,mouthRollLower:.3,mouthRollUpper:.3,mouthPressLeft:.5,mouthPressRight:.5}},{key:"viseme_FF",mix:{mouthPucker:1,mouthShrugUpper:1,mouthDimpleLeft:1,mouthDimpleRight:1,mouthStretchLeft:.5,mouthStretchRight:.5,mouthPressLeft:.3,mouthPressRight:.3,mouthRollLower:.1},teethMix:{mouthPucker:1,mouthShrugUpper:1,mouthDimpleLeft:1,mouthDimpleRight:1,mouthStretchLeft:.5,mouthStretchRight:.5,mouthPressLeft:.3,mouthPressRight:.3,mouthRollLower:.1}},{key:"viseme_DD",mix:{mouthShrugUpper:.25,mouthShrugLower:.25,mouthPressLeft:.4,mouthPressRight:.4,mouthFunnel:.7,jawOpen:.1},teethMix:{mouthShrugUpper:.25,mouthShrugLower:.25,mouthPressLeft:.4,mouthPressRight:.4,mouthFunnel:.7,jawOpen:.1}},{key:"viseme_SS",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthLowerDownLeft:.3,mouthLowerDownRight:.3,jawOpen:.1},teethMix:{mouthPressLeft:.8,mouthPressRight:.8,mouthLowerDownLeft:.3,mouthLowerDownRight:.3,jawOpen:.1}},{key:"viseme_TH",mix:{mouthStretchLeft:.4,mouthStretchRight:.4,mouthDimpleLeft:1,mouthDimpleRight:1,mouthFunnel:.4,jawOpen:.15,mouthShrugLower:.45,mouthShrugUpper:.45},teethMix:{mouthStretchLeft:.4,mouthStretchRight:.4,mouthDimpleLeft:1,mouthDimpleRight:1,mouthFunnel:.4,jawOpen:.15,mouthShrugLower:.45,mouthShrugUpper:.45}},{key:"viseme_CH",mix:{mouthPucker:.8,mouthShrugLower:.7,mouthShrugUpper:.2,mouthLowerDownLeft:.6,mouthLowerDownRight:.6,mouthDimpleLeft:.5,mouthDimpleRight:.5,mouthUpperUpLeft:.8,mouthUpperUpRight:.8,jawOpen:.2},teethMix:{mouthPucker:.8,mouthShrugLower:.7,mouthShrugUpper:.2,mouthLowerDownLeft:.6,mouthLowerDownRight:.6,mouthDimpleLeft:.5,mouthDimpleRight:.5,mouthUpperUpLeft:.8,mouthUpperUpRight:.8,jawOpen:.2}},{key:"viseme_RR",mix:{mouthPucker:.4,jawOpen:.2,mouthShrugUpper:.5,mouthFunnel:.15,mouthDimpleLeft:.5,mouthDimpleRight:.5},teethMix:{mouthPucker:.4,jawOpen:.2,mouthShrugUpper:.5,mouthFunnel:.15,mouthDimpleLeft:.5,mouthDimpleRight:.5}},{key:"viseme_kk",mix:{mouthPucker:.5,jawOpen:.2,mouthShrugUpper:.5,mouthFunnel:.2,mouthDimpleLeft:.5,mouthDimpleRight:.5},teethMix:{mouthPucker:.5,jawOpen:.2,mouthShrugUpper:.5,mouthFunnel:.2,mouthDimpleLeft:.5,mouthDimpleRight:.5}},{key:"viseme_nn",mix:{mouthPucker:.6,jawOpen:.2,mouthShrugUpper:.7,mouthShrugLower:.1,mouthFunnel:.1,mouthDimpleLeft:.8,mouthDimpleRight:.8},teethMix:{mouthPucker:.6,jawOpen:.2,mouthShrugUpper:.7,mouthShrugLower:.1,mouthFunnel:.1,mouthDimpleLeft:.8,mouthDimpleRight:.8}},{key:"viseme_sil",mix:{}}],Pn={aei:[{v:"E",w:.8},{v:"I",w:.2}],ee:[{v:"I",w:1}],oo:[{v:"O",w:1}],u:[{v:"U",w:1}],aa:[{v:"aa",w:1}],ah:[{v:"aa",w:.7},{v:"O",w:.3}],bmp:[{v:"PP",w:1}],fv:[{v:"FF",w:1}],th:[{v:"TH",w:1}],l:[{v:"nn",w:1}],r:[{v:"RR",w:1}],qw:[{v:"U",w:.6},{v:"O",w:.4}],chjsh:[{v:"CH",w:1}],cdgknstxyz:[{v:"DD",w:.6},{v:"SS",w:.4}],sil:[{v:"sil",w:1}]};function jn(f){if(!f)return[{v:"sil",w:1}];const w=f.toLowerCase();return Pn[w]??[{v:"sil",w:1}]}function Bn({target:f}){const{camera:w}=It.useThree();return t.useEffect(()=>{w.lookAt(...f)},[w,f]),null}function u(f,w,b){if(!f||!f.morphTargetDictionary)return;const S=f,V=S.morphTargetDictionary,Y=S.morphTargetInfluences;if(Y)for(const X in V)X.toLowerCase()===w.toLowerCase()&&(Y[V[X]]=b)}function Mt(f,w=.97){if(!f)return;const b=f;if(b.morphTargetInfluences)for(let S=0;S<b.morphTargetInfluences.length;S++)b.morphTargetInfluences[S]*=w}const Wn=t.memo(({avatarUrl:f,isPlayingRef:w,visemeQueueRef:b,audioContextRef:S,responseAudioStartTimeRef:V,adjustments:Y,mood:X,expression:ze,agentResponse:Fe,isSpeaking:C,nextStartTimeRef:Ze,stopPlayback:Se,setIsSpeaking:Ye,expressionUrl:$,onExpressionFinished:xe,isNudgeResponse:G,avatarGender:oe})=>{const{scene:M}=gt.useGLTF(f),le=oe==="female"?kn:xn,W=gt.useGLTF(le),_=t.useMemo(()=>W.flatMap(g=>g.animations),[W]),ne=oe==="female"?Mn:Tn,p=gt.useGLTF(ne),D=t.useMemo(()=>p.flatMap(g=>g.animations),[p]),J=t.useRef(null),E=t.useRef(null),A=t.useRef(null),F=t.useRef(null),q=t.useRef(null),K=t.useRef(null),bt=t.useRef([]),[Z]=t.useState(()=>new rt.AnimationMixer(M)),H=t.useRef({}),j=t.useRef(null),ke=t.useRef(0),Ke=t.useRef(!1),Ne=t.useRef(0),Ee=t.useRef(!1),ge=t.useRef(null),Te=t.useRef(zt),st=t.useRef(-1),Me=t.useRef("left"),Pe=t.useRef(mt+Math.random()*(Qt-mt)),fe=t.useRef(0),Ae=t.useRef("smile");t.useEffect(()=>{if(!(!_||!M)){if(_.forEach((g,h)=>{const c=`idle_${h}`;if(!H.current[c]){const o=Z.clipAction(g,M);o.name=c,o.setLoop(rt.LoopOnce,1),o.clampWhenFinished=!0,H.current[c]=o}}),D.forEach((g,h)=>{const c=`talk_${h}`;if(!H.current[c]){const o=Z.clipAction(g,M);o.name=c,o.setLoop(rt.LoopOnce,1),o.clampWhenFinished=!0,H.current[c]=o}}),_.length>0){const g=H.current.idle_0,h=j.current&&Z.existingAction(j.current.getClip());g&&!h&&(g.reset().fadeIn(.5).play(),j.current=g)}return()=>{Z.stopAllAction(),H.current={},j.current=null}}},[_,D,M,Z]);const ye=t.useRef("");t.useEffect(()=>{if(!$||!M||$===ye.current)return;ye.current=$,ge.current=$,new Sn.GLTFLoader().load($,h=>{if(h.animations&&h.animations.length>0){const c=h.animations[0],o=Z.clipAction(c,M);if(o.name=`EXPR_${$}`,o.setLoop(rt.LoopOnce,1),o.clampWhenFinished=!0,H.current[`EXPR_${$}`]=o,C&&ge.current===$){const d=j.current;o.reset().fadeIn(.3).play(),d&&d!==o&&d.crossFadeTo(o,.3,!0),j.current=o,ge.current=null}}},void 0,h=>{console.error(`[ANIMATION] Failed to load ${$}`,h)})},[$,M,Z,C]),t.useEffect(()=>{const g=h=>{const c=h.action,o=c.name||"";if(o.startsWith("idle_")){const I=(parseInt(o.split("_")[1])+1)%_.length,ee=H.current[`idle_${I}`];ee&&(ee.reset().fadeIn(.5).play(),c.crossFadeTo(ee,.5,!0),j.current=ee)}else if(o.startsWith("EXPR_")){if(C){const d=Math.floor(Math.random()*D.length),I=H.current[`talk_${d}`];I&&(I.reset().fadeIn(.5).play(),c.crossFadeTo(I,.5,!0),j.current=I)}else{const d=H.current.idle_0;d&&(d.reset().fadeIn(.5).play(),c.crossFadeTo(d,.5,!0),j.current=d)}xe&&xe()}else if(o.startsWith("talk_"))if(C&&!G){const I=(parseInt(o.split("_")[1])+1)%D.length,ee=H.current[`talk_${I}`];ee&&(ee.reset().fadeIn(.3).play(),c.crossFadeTo(ee,.3,!0),j.current=ee)}else{const d=H.current.idle_0;d&&(d.reset().fadeIn(.5).play(),c.crossFadeTo(d,.5,!0),j.current=d)}};return Z.addEventListener("finished",g),()=>Z.removeEventListener("finished",g)},[Z,_,D,C,G,xe]),t.useEffect(()=>{if(C&&M){const g=j.current,h=g?.name||"";if(h.startsWith("idle_")||h.startsWith("talk_")||h===""){const c=ge.current;if(c){const o=H.current[`EXPR_${c}`];if(o){o.reset().fadeIn(.3).play(),g&&g!==o&&g.crossFadeTo(o,.3,!0),j.current=o,ge.current=null;return}}if(h.startsWith("idle_")||h===""){const o=Math.floor(Math.random()*D.length),d=H.current[`talk_${o}`];d&&(d.reset().fadeIn(.5).play(),g&&g.crossFadeTo(d,.5,!0),j.current=d)}}}else if(!C&&M){const g=j.current,h=g?.name||"";if(h.startsWith("talk_")||h.startsWith("EXPR_")){const c=H.current.idle_0;c&&(c.reset().fadeIn(.5).play(),g&&g.crossFadeTo(c,.5,!0),j.current=c)}}},[C,M,$]),t.useEffect(()=>{if(!M)return;M.traverse(c=>{if(c.isMesh&&c.morphTargetDictionary){const o=c.name.toLowerCase();(o.includes("head")||o.includes("avatar"))&&(E.current=c,U(`[ANIMATION] Found head mesh: ${c.name}`)),o.includes("teeth")&&(A.current=c,U(`[ANIMATION] Found teeth mesh: ${c.name}`)),o.includes("beard")&&(F.current=c,U(`[ANIMATION] Found beard mesh: ${c.name}`)),o.includes("eyeleft")&&(q.current=c,U(`[ANIMATION] Found eye left mesh: ${c.name}`)),o.includes("eyeright")&&(K.current=c,U(`[ANIMATION] Found eye right mesh: ${c.name}`))}});const g=E.current?.morphTargetDictionary;g&&Object.keys(g).filter(c=>c.toLowerCase().includes("brow"));const h=[];M.traverse(c=>{if(c.isMesh){const d=c.morphTargetDictionary;d&&Object.keys(d).some(I=>I.toLowerCase().includes("brow"))&&h.push(c)}}),bt.current=h,h.length>0&&U("[ANIMATION] Meshes with brow morphs:",h.length)},[M]);const et=(g,h=1)=>{const c=`viseme_${g}`.toLowerCase(),o=Nn.find(d=>d.key.toLowerCase()===c);if(o){for(const d in o.mix){const I=o.mix[d]*h;u(E.current,d,I),u(F.current,d,I),(!o.teethMix||!(d in o.teethMix))&&u(A.current,d,I)}if(o.teethMix)for(const d in o.teethMix){const I=o.teethMix[d]*h;u(A.current,d,I)}}},tt=g=>{const h=pt[g]??pt.neutral;for(const c in h)u(E.current,c,h[c]),u(A.current,c,h[c]),u(F.current,c,h[c])};return It.useFrame((g,h)=>{const c=Math.pow(.88,60*h);Mt(E.current,c),Mt(A.current,c),Mt(F.current,c),tt(X);const o=g.clock.elapsedTime;let d=0;if(Math.floor(o)%5===0&&Math.floor((o-h)%5)!==0){let y=null;M.traverse(N=>{N.name.toLowerCase().includes("hips")&&(y=N)});const z=y?`Hips Y: ${y.position.y.toFixed(4)}`:"Hips not found";U(`[ANIMATION] Mixer Time: ${Z.time.toFixed(2)}, ${z}`)}if(Z.update(h),o>ke.current&&!Ke.current&&(Ke.current=!0,Ne.current=o),Ke.current){const y=C?.2:.3,z=(o-Ne.current)/y;if(z>=1)if(Ke.current=!1,Ee.current){const N=C?1:2.5;ke.current=o+N,Ee.current=!1}else ke.current=o+.12,Ee.current=!0;else{const N=z<.5?z*2:(1-z)*2;u(E.current,"eyeBlinkLeft",N),u(E.current,"eyeBlinkRight",N),u(A.current,"eyeBlinkLeft",N),u(A.current,"eyeBlinkRight",N),u(F.current,"eyeBlinkLeft",N),u(F.current,"eyeBlinkRight",N),d=N*On.browInnerUp}}const I=pt[X]??pt.neutral,ee=I.browInnerUp??0,_t=I.browOuterUpLeft??0,je=I.browOuterUpRight??0,te=o*Cn,ot=Et*Math.sin(te),Ie=Et*.7*Math.sin(te+.7),Be=Et*.7*Math.sin(te+1.3),v=y=>Math.max(0,Math.min(1,y)),it=v(ee+ot),We=v(_t+Ie),Ve=v(je+Be),at=v(it+d);u(E.current,"browInnerUp",at),u(A.current,"browInnerUp",at),u(F.current,"browInnerUp",at),u(E.current,"browOuterUpLeft",We),u(A.current,"browOuterUpLeft",We),u(F.current,"browOuterUpLeft",We),u(E.current,"browOuterUpRight",Ve),u(A.current,"browOuterUpRight",Ve),u(F.current,"browOuterUpRight",Ve);const $e=Te.current,De=$e+dt,he=De+Tt;o>he&&(Te.current+=dt+Tt+zt);const be=!C&&o>=$e&&o<De,Oe=!C&&o>=De&&o<he;(be||Oe)&&st.current!==$e&&(st.current=$e,Me.current=Math.random()<.7?"left":"up");const ct=be?o-$e:Oe?dt:0,re=be||Oe?Math.min(1,ct/dt):0,He=re<=0?0:Math.sin(re*Math.PI),ut=Oe?1-(o-De)/Tt:1,wt=be?He:Oe?He*ut:0,Ce=Un*wt,x=y=>I[y]??0;if(Me.current==="up"?(u(q.current,"eyeLookUpLeft",v(x("eyeLookUpLeft")+Ce)),u(q.current,"eyeLookDownLeft",v(x("eyeLookDownLeft"))),u(q.current,"eyeLookOutLeft",v(x("eyeLookOutLeft"))),u(q.current,"eyeLookInLeft",v(x("eyeLookInLeft"))),u(K.current,"eyeLookUpRight",v(x("eyeLookUpRight")+Ce)),u(K.current,"eyeLookDownRight",v(x("eyeLookDownRight"))),u(K.current,"eyeLookOutRight",v(x("eyeLookOutRight"))),u(K.current,"eyeLookInRight",v(x("eyeLookInRight")))):(u(q.current,"eyeLookUpLeft",v(x("eyeLookUpLeft"))),u(q.current,"eyeLookDownLeft",v(x("eyeLookDownLeft"))),u(q.current,"eyeLookOutLeft",v(x("eyeLookOutLeft"))),u(q.current,"eyeLookInLeft",v(x("eyeLookInLeft")+Ce)),u(K.current,"eyeLookUpRight",v(x("eyeLookUpRight"))),u(K.current,"eyeLookDownRight",v(x("eyeLookDownRight"))),u(K.current,"eyeLookOutRight",v(x("eyeLookOutRight")+Ce)),u(K.current,"eyeLookInRight",v(x("eyeLookInRight")))),!C&&o>=Pe.current){fe.current=o,Ae.current=Math.random()<.5?"pucker":"smile";const y=Ae.current==="pucker"?Kt:Yt;Pe.current=o+y+mt+Math.random()*(Qt-mt)}const ie=Ae.current==="pucker"?Kt:Yt,ae=!C&&o>=fe.current&&o<fe.current+ie,Ge=ae?(o-fe.current)/ie:0,Re=Ge<=0?0:Ge<.5?Ge*2:(()=>{const y=(Ge-.5)*2;return 1-y*y*(3-2*y)})(),de=y=>I[y]??0;if(ae)if(Ae.current==="pucker"){const y=v(de("mouthPucker")+Fn*Re);u(E.current,"mouthPucker",y),u(A.current,"mouthPucker",y),u(F.current,"mouthPucker",y)}else{const y=v(de("mouthSmileLeft")+Xt*Re),z=v(de("mouthSmileRight")+Xt*Re),N=v(de("mouthDimpleLeft")+Zt*Re),Le=v(de("mouthDimpleRight")+Zt*Re);u(E.current,"mouthSmileLeft",y),u(E.current,"mouthSmileRight",z),u(E.current,"mouthDimpleLeft",N),u(E.current,"mouthDimpleRight",Le),u(A.current,"mouthSmileLeft",y),u(A.current,"mouthSmileRight",z),u(A.current,"mouthDimpleLeft",N),u(A.current,"mouthDimpleRight",Le),u(F.current,"mouthSmileLeft",y),u(F.current,"mouthSmileRight",z),u(F.current,"mouthDimpleLeft",N),u(F.current,"mouthDimpleRight",Le)}if(J.current){const y=w.current?0:Y.rotation[1];J.current.rotation.y=rt.MathUtils.lerp(J.current.rotation.y,y,.1),J.current.position.set(...Y.position),J.current.scale.setScalar(Y.scale),J.current.rotation.x=Y.rotation[0],J.current.rotation.z=Y.rotation[2]}if(w.current&&S.current){const y=S.current.currentTime,Le=(y-V.current)*1e3- -150;for(let ce=0;ce<b.current.length;ce++){const ue=b.current[ce];Le>=ue.vtime&&Le<ue.vtime+ue.vduration&&et(ue.viseme,ue.weight??1)}y>Ze.current+.5&&(Se(),Ye(!1))}}),l.jsx("group",{ref:J,children:l.jsx("primitive",{object:M})})});function Vn(f){return f?f.charAt(0).toUpperCase()+f.slice(1).toLowerCase():""}function tn(f){return f.replace(/\*(.*?)\*/g," $1 ").replace(/\[.*?\]/g," ").replace(/<.*?\/>/g," ").replace(/<.*?>.*?<\/.*?>/g," ").replace(/\s+/g," ").trim()}const At=new Map,$n=({onNavigationRequested:f,agentId:w,presetUserDetails:b,onAvatarReady:S})=>{const[V,Y]=t.useState(""),[X,ze]=t.useState("male"),[Fe,C]=t.useState(""),[Ze,Se]=t.useState(!1),Ye=V,$=X;t.useEffect(()=>{if(!w){Se(!0);return}let e=!1;return(async()=>{console.log(">> fetchConfig RUNNING for",w);try{const s=`agent_config_${w}`,i=sessionStorage.getItem(s);if(i)try{const a=JSON.parse(i);e||(a.encryptedAgentToken&&Y(a.encryptedAgentToken),a.avatarGender&&ze(a.avatarGender),a.agent_id&&C(a.agent_id),Se(!0));return}catch(a){console.error("Failed to parse cached avatar config:",a),sessionStorage.removeItem(s)}let n;if(At.has(w))console.log(">> REUSING in-flight fetch for",w),n=await At.get(w);else{const a=(async()=>{console.log(">> INITIATING visitedWebsite fetch for",w);const m={agentId:w},R=await fetch(`${Xe}/visitedWebsite`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(m)});if(!R.ok)throw new Error(`Fetch failed: ${R.status}`);const L=await R.json();return sessionStorage.setItem(s,JSON.stringify(L)),L})();At.set(w,a),n=await a}if(e)return;n&&(n.encryptedAgentToken&&Y(n.encryptedAgentToken),n.avatarGender&&ze(n.avatarGender),n.agent_id&&C(n.agent_id))}catch(s){console.error("Failed to fetch avatar config:",s)}finally{e||Se(!0)}})(),()=>{e=!0}},[w]);const xe=vn(w??Fe),[G,oe]=t.useState(""),[M,le]=t.useState(""),[W,_]=t.useState("Ready"),[ne,p]=t.useState(!1),[D,J]=t.useState(!1),[E,A]=t.useState([]),[F,q]=t.useState(""),K=t.useRef(null),[bt,Z]=t.useState(()=>typeof window<"u"?window.matchMedia("(max-width: 480px)").matches:!1);t.useEffect(()=>{const e=window.matchMedia("(max-width: 480px)"),r=()=>Z(e.matches);return e.addEventListener("change",r),()=>e.removeEventListener("change",r)},[]);const H=bt?80:600,j=t.useRef(!1),ke=t.useRef([]),Ke=t.useRef(0),Ne=t.useRef(!1),Ee=t.useRef([]),ge=t.useRef(null),Te=t.useRef([]);t.useRef([]);const st=t.useRef([]),Me=t.useRef(0),Pe=t.useRef(0),fe=t.useRef(0),Ae=t.useRef(0),ye=t.useRef(!1),[et,tt]=t.useState(null),g=t.useRef(null),[h,c]=t.useState("neutral"),[o,d]=t.useState(""),[I,ee]=t.useState(""),[_t,je]=t.useState("Chat with us"),[te,ot]=t.useState(null),[Ie,Be]=t.useState("hidden"),[v,it]=t.useState(""),We=t.useRef(null),Ve=t.useRef(Ie);Ve.current=Ie;const[at,$e]=t.useState(null),De=t.useRef(null),he=t.useRef(null),[be,Oe]=t.useState(!1),ct=t.useRef(null),[re,He]=t.useState("hidden"),[ut,wt]=t.useState(""),Ce=t.useRef(re);Ce.current=re;const x=t.useRef(""),ie=t.useRef(!1),ae=t.useRef(""),Ge=t.useRef(Date.now()),Re=t.useRef([]),de=t.useRef(!1),[y,z]=t.useState(!1),[N,Le]=t.useState(!1),ce=t.useRef(null),ue=t.useMemo(()=>be?"Try again":W==="Busy"?"Busy":W==="Thinking..."||W==="Processing Voice..."?W:te!=null&&te!==""&&te!=="none"&&te!=="<none>"?`Enter ${Vn(te)}`:re!=="hidden"||!N?null:en,[W,te,be,re,N]),Dt=!be&&W!=="Busy"&&W!=="Thinking..."&&W!=="Processing Voice..."&&(te==null||te===""||te==="none"||te==="<none>");t.useEffect(()=>{if(re!=="hidden"||!Dt){Le(!1),ce.current!=null&&(clearTimeout(ce.current),ce.current=null);return}return ce.current=setTimeout(()=>{ce.current=null,Le(!0)},400),()=>{ce.current!=null&&(clearTimeout(ce.current),ce.current=null)}},[re,Dt]);const lt=ue!=null&&ue!=="";t.useEffect(()=>{const e=Ve.current;if(!(e==="exiting"||e==="entering")){if(e==="hidden"){lt&&(it(ue??""),Be("entering"));return}e==="visible"&&(!lt||ue!==v)&&(We.current=lt?ue:null,Be("exiting"))}},[lt,ue,Ie,v]),t.useEffect(()=>{if(W!=="Busy"){he.current!=null&&(clearTimeout(he.current),he.current=null);return}return he.current=setTimeout(()=>{he.current=null,_("Ready"),Oe(!0)},12e3),()=>{he.current!=null&&(clearTimeout(he.current),he.current=null)}},[W]),t.useEffect(()=>{if(!be)return;const e=setTimeout(()=>Oe(!1),2500);return()=>clearTimeout(e)},[be]);const nn=t.useCallback(()=>{const e=Ve.current;if(e==="exiting"){Be("hidden");const r=We.current;We.current=null,r!=null&&r!==""&&(it(r),Be("entering"))}else e==="entering"&&Be("visible")},[]),Ot=e=>{if(!e)return;if(e.mood!=null){const n=String(e.mood).toLowerCase();c(n)}if(e.expression!=null){const n=String(e.expression).trim();d(n);const a=En.find(m=>m.name.toLowerCase()===n.toLowerCase());U(`[STREAM] Animation match for "${n}": ${a?a.name:"NONE"}`),ee(a?.url??"")}if(e.navigation!=null){const n=String(e.navigation).trim();n!==""&&(f?f(n):window.open(n,"_blank"))}const r=e.ask_for||e.lead_capture?.ask_for,s=r?String(r).trim().toLowerCase():"",i=s==="none"||s==="<none>";if(r&&!i){const n=s;if(b&&(n==="email"&&b.email||n==="name"&&b.name||n==="phone"&&b.phone))return;ot(n||null),je(n==="email"?"Enter your email":n==="name"?"Enter your name":n==="phone"?"Enter your phone number":"Chat with us")}else(i||e.ask_for===null||e.lead_capture&&e.lead_capture.ask_for===null||e.ask_for==="none")&&(ot(null),_t!=="Chat with us"&&je("Chat with us"));e.collected,e.valid},rn=e=>{const r=e.trim();if(!r)return null;if(r.startsWith("{"))try{return JSON.parse(r)}catch{return null}if(r.includes(":")){const s=r.split(":"),i=s[0].trim().toLowerCase(),n=s.slice(1).join(":").trim();return{[i]:n}}return null},sn=t.useCallback(()=>{},[]),Ue="§",St=()=>{for(;x.current.includes(`
|
|
2
|
-
`);){const e=
|
|
3
|
-
`),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("react/jsx-runtime"),yt=require("@react-three/drei"),Ct=require("@react-three/fiber"),t=require("react"),wn=require("three"),yn=require("three/examples/jsm/loaders/GLTFLoader.js");function Sn(l){const m=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(l){for(const S in l)if(S!=="default"){const g=Object.getOwnPropertyDescriptor(l,S);Object.defineProperty(m,S,g.get?g:{enumerable:!0,get:()=>l[S]})}}return m.default=l,Object.freeze(m)}const dt=Sn(wn),st="https://ai.streamoji.com",C=(...l)=>{},Rn=(...l)=>{},qt=({analyser:l})=>{const m=t.useRef(null),S=t.useRef(null);return t.useEffect(()=>{const g=m.current;if(!g)return;const $=g.getContext("2d",{alpha:!0});if(!$)return;let Y,X=null;l&&(l.fftSize=128,X=new Uint8Array(l.frequencyBinCount));const Ke=()=>{Y=requestAnimationFrame(Ke),(g.width!==g.offsetWidth||g.height!==g.offsetHeight)&&(g.width=g.offsetWidth,g.height=g.offsetHeight);const Pe=g.width,N=g.height;if(Pe===0||N===0)return;const ot=N/2;$.clearRect(0,0,Pe,N),$.fillStyle="#1e293b";const Se=2,V=Se+2,ke=Pe*.95,G=Math.floor(ke/V);(!S.current||S.current.length!==G)&&(S.current=new Float32Array(G).fill(2));const ie=G*V,I=(Pe-ie)/2;l&&X&&l.getByteFrequencyData(X);const fe=X?X.length:0,_=Math.floor(fe*.7)/G,ne=new Float32Array(G);for(let p=0;p<G;p++){let O=0;if(X&&_>0){const U=Math.floor(p*_),j=Math.floor((p+1)*_);for(let q=U;q<j;q++){const K=X[q]||0;K>O&&(O=K)}}O<10&&(O=0);const J=O/255,T=O>0?Math.max(2,Math.pow(J,1.4)*N*.25):2;ne[p]=T}for(let p=0;p<G;p++){const O=G-1-p,J=Math.max(ne[p],ne[O]),T=S.current[p]+(J-S.current[p])*.3;S.current[p]=T;const U=I+p*V,j=ot-T/2;$.beginPath(),$.roundRect?$.roundRect(U,j,Se,T,4):$.fillRect(U,j,Se,T),$.fill()}};return Ke(),()=>{cancelAnimationFrame(Y)}},[l]),c.jsx("canvas",{ref:m,style:{width:"100%",height:"100%",display:"block"}})},vn="https://pub-48df6f7d60d6440bbd01676ea5d90e55.r2.dev",zt="https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/default-models/avatar-blue-suit.glb";function xn(l){const[m,S]=t.useState(null);return t.useEffect(()=>{if(!l){S(null);return}let g=!1;const $=`${vn}/${l}.glb`;return fetch($,{method:"HEAD"}).then(Y=>{g||S(Y.ok?$:zt)}).catch(()=>{g||S(zt)}),()=>{g=!0}},[l]),m}const Ln=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/M_Standing_Idle_Variations_001.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/M_Standing_Idle_Variations_002.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/M_Standing_Idle_002.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/M_Standing_Idle_Variations_005.glb"],kn=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/F_Standing_Idle_Variations_001.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/F_Standing_Idle_Variations_003.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/idle/F_Standing_Idle_Variations_003.glb"],En=[{id:"m_expr_01",name:"Friendly Wave",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_001.glb"},{id:"m_expr_02",name:"You There",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_002.glb"},{id:"m_expr_04",name:"Awkward Agreement",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_004.glb"},{id:"m_expr_05",name:"What's Going On?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_005.glb"},{id:"m_expr_06",name:"Tired Stretch",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_006.glb"},{id:"m_expr_07",name:"Conceilied Laughter",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_007.glb"},{id:"m_expr_08",name:"You Come Here",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_008.glb"},{id:"m_expr_09",name:"Come Here Kid",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_009.glb"},{id:"m_expr_10",name:"Come Here Everyone",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_010.glb"},{id:"m_expr_11",name:"No Freaking Way",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_011.glb"},{id:"m_expr_12",name:"Cheerful Approval",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_012.glb"},{id:"m_expr_13",name:"Waving Hello",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_013.glb"},{id:"m_expr_14",name:"Checking Surroundings",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_014.glb"},{id:"m_expr_15",name:"Referee Warning",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_015.glb"},{id:"m_expr_16",name:"You Thumbs Down",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_016.glb"},{id:"m_expr_17",name:"Side Thumbs Down",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_017.glb"},{id:"m_expr_18",name:"You're Finished",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Standing_Expressions_018.glb"},{id:"m_talk_01",name:"Oh God, Why Me?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_001.glb"},{id:"m_talk_02",name:"What Are You Doing?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_002.glb"},{id:"m_talk_03",name:"What Am I doing?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_003.glb"},{id:"m_talk_04",name:"No Way",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_004.glb"},{id:"m_talk_05",name:"What's Going On?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_005.glb"},{id:"m_talk_06",name:"I have no idea",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_006.glb"},{id:"m_talk_07",name:"What's going on here?",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_007.glb"},{id:"m_talk_08",name:"Let's stop",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_008.glb"},{id:"m_talk_09",name:"Fed Up Moment",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_009.glb"},{id:"m_talk_10",name:"What's This? Hold On",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_010.glb"},{id:"f_talk_01",name:"Great Job Clap",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_001.glb"},{id:"f_talk_02",name:"Chill Stretch",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_002.glb"},{id:"f_talk_03",name:"This Is Me",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_003.glb"},{id:"f_talk_04",name:"Empathize",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_004.glb"},{id:"f_talk_05",name:"Loose Hands Stretch",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_005.glb"},{id:"f_talk_06",name:"Take It Easy",url:"https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_006.glb"}],Tn=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_005.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_007.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_006.glb"],Mn=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_004.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_005.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_006.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/F_Talking_Variations_002.glb"],An={zoom:.85,position:[.15,-.8,0],scale:1.5,rotation:[.15,.02,0]},In=[-.45,1.9,.1],On=[-.45,1.75,.1],Dn={browInnerUp:.2},At=.18,Cn=1,Nn=.5,Yt=3,bt=.55,It=.12,Kt=2,Qt=2,_t=4,Xt=8,Un=1,Zt=.38,en=.32,tn="__branding__",wt={neutral:{eyeLookDownLeft:.1,eyeLookDownRight:.1},happy:{mouthSmileLeft:.2,mouthSmileRight:.2,eyeLookDownLeft:.1,eyeLookDownRight:.1},sad:{eyeLookDownLeft:.2,eyeLookDownRight:.2,browDownRight:.1,browInnerUp:.6,browOuterUpRight:.2,eyeSquintLeft:.7,eyeSquintRight:.7,mouthFrownLeft:.8,mouthFrownRight:.8,mouthLeft:.2,mouthPucker:.5,mouthRollLower:.2,mouthRollUpper:.2,mouthShrugLower:.2,mouthShrugUpper:.2,mouthStretchLeft:.4},angry:{eyeLookDownLeft:.1,eyeLookDownRight:.1,browDownLeft:.6,browDownRight:.6,jawForward:.3,mouthFrownLeft:.7,mouthFrownRight:.7,mouthRollLower:.2,mouthShrugLower:.3},fear:{browInnerUp:.7,eyeSquintLeft:.5,eyeSquintRight:.5,eyeWideLeft:.6,eyeWideRight:.6,mouthClose:.1,mouthFunnel:.3,mouthShrugLower:.5,mouthShrugUpper:.5},disgust:{browDownLeft:.7,browDownRight:.1,browInnerUp:.3,eyeSquintLeft:1,eyeSquintRight:1,eyeWideLeft:.5,eyeWideRight:.5,mouthLeft:.4,mouthPressLeft:.3,mouthRollLower:.3,mouthShrugLower:.3,mouthShrugUpper:.8,mouthUpperUpLeft:.3,noseSneerLeft:1,noseSneerRight:.7},love:{browInnerUp:.4,browOuterUpLeft:.2,browOuterUpRight:.2,mouthSmileLeft:.2,mouthSmileRight:.2,eyeBlinkLeft:.6,eyeBlinkRight:.6,eyeWideLeft:.7,eyeWideRight:.7,mouthDimpleLeft:.1,mouthDimpleRight:.1,mouthPressLeft:.2,mouthShrugUpper:.2,mouthUpperUpLeft:.1,mouthUpperUpRight:.1}},sn=[{key:"viseme_aa",mix:{jawOpen:.6},teethMix:{jawOpen:.6}},{key:"viseme_E",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthDimpleLeft:1,mouthDimpleRight:1,jawOpen:.3},teethMix:{mouthPressLeft:.8,mouthPressRight:.8,mouthDimpleLeft:1,mouthDimpleRight:1,jawOpen:.3}},{key:"viseme_I",mix:{mouthPressLeft:.6,mouthPressRight:.6,mouthDimpleLeft:.6,mouthDimpleRight:.6,jawOpen:.2},teethMix:{mouthPressLeft:.6,mouthPressRight:.6,mouthDimpleLeft:.6,mouthDimpleRight:.6,jawOpen:.2}},{key:"viseme_O",mix:{mouthPucker:1,jawForward:.6,jawOpen:.2},teethMix:{mouthPucker:1,jawForward:.6,jawOpen:.2}},{key:"viseme_U",mix:{mouthFunnel:1},teethMix:{mouthFunnel:1}},{key:"viseme_PP",mix:{mouthRollLower:.8,mouthRollUpper:.8,mouthUpperUpLeft:.3,mouthUpperUpRight:.3},teethMix:{mouthRollLower:.8,mouthRollUpper:.8,mouthUpperUpLeft:.3,mouthUpperUpRight:.3}},{key:"viseme_FF",mix:{mouthPucker:1,mouthShrugUpper:1,mouthLowerDownLeft:.2,mouthLowerDownRight:.2,mouthDimpleLeft:1,mouthDimpleRight:1,mouthRollLower:1},teethMix:{mouthPucker:1,mouthShrugUpper:1,mouthLowerDownLeft:.2,mouthLowerDownRight:.2,mouthDimpleLeft:1,mouthDimpleRight:1,mouthRollLower:1}},{key:"viseme_DD",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthFunnel:.5,jawOpen:.2},teethMix:{mouthPressLeft:.8,mouthPressRight:.8,mouthFunnel:.5,jawOpen:.2}},{key:"viseme_SS",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthLowerDownLeft:.5,mouthLowerDownRight:.5,jawOpen:.1},teethMix:{mouthPressLeft:.8,mouthPressRight:.8,mouthLowerDownLeft:.5,mouthLowerDownRight:.5,jawOpen:.1}},{key:"viseme_TH",mix:{mouthRollUpper:.6,jawOpen:.2,tongueOut:.4},teethMix:{mouthRollUpper:.6,jawOpen:.2,tongueOut:.4}},{key:"viseme_CH",mix:{mouthPucker:.5,jawOpen:.2},teethMix:{mouthPucker:.5,jawOpen:.2}},{key:"viseme_RR",mix:{mouthPucker:.5,jawOpen:.2},teethMix:{mouthPucker:.5,jawOpen:.2}},{key:"viseme_kk",mix:{mouthLowerDownLeft:.4,mouthLowerDownRight:.4,mouthDimpleLeft:.3,mouthDimpleRight:.3,mouthFunnel:.3,mouthPucker:.3,jawOpen:.15},teethMix:{mouthLowerDownLeft:.4,mouthLowerDownRight:.4,mouthDimpleLeft:.3,mouthDimpleRight:.3,mouthFunnel:.3,mouthPucker:.3,jawOpen:.15}},{key:"viseme_nn",mix:{mouthLowerDownLeft:.4,mouthLowerDownRight:.4,mouthDimpleLeft:.3,mouthDimpleRight:.3,mouthFunnel:.3,mouthPucker:.3,jawOpen:.15,tongueOut:.2},teethMix:{mouthLowerDownLeft:.4,mouthLowerDownRight:.4,mouthDimpleLeft:.3,mouthDimpleRight:.3,mouthFunnel:.3,mouthPucker:.3,jawOpen:.15,tongueOut:.2}},{key:"viseme_sil",mix:{}}],Fn={aei:[{v:"E",w:.8},{v:"I",w:.2}],ee:[{v:"I",w:1}],oo:[{v:"O",w:1}],u:[{v:"U",w:1}],aa:[{v:"aa",w:1}],ah:[{v:"aa",w:.7},{v:"O",w:.3}],bmp:[{v:"PP",w:1}],fv:[{v:"FF",w:1}],th:[{v:"TH",w:1}],l:[{v:"nn",w:1}],r:[{v:"RR",w:1}],qw:[{v:"U",w:.6},{v:"O",w:.4}],chjsh:[{v:"CH",w:1}],cdgknstxyz:[{v:"DD",w:.6},{v:"SS",w:.4}],sil:[{v:"sil",w:1}]},Ot=new Set(sn.flatMap(l=>[...Object.keys(l.mix),...l.teethMix?Object.keys(l.teethMix):[]]));function nn(l){const m=Math.max(0,Math.min(1,l));return m*m*(3-2*m)}function jn(l,m,S){if(S<=0)return 0;const g=(l-m)/S;return g<0||g>1?0:g<.25?nn(g/.25):g>.75?nn((1-g)/.25):1}function Pn(l){if(!l)return[{v:"sil",w:1}];const m=l.toLowerCase();return Fn[m]??[{v:"sil",w:1}]}function Bn({target:l}){const{camera:m}=Ct.useThree();return t.useEffect(()=>{m.lookAt(...l)},[m,l]),null}function u(l,m,S){if(!l||!l.morphTargetDictionary)return;const g=l,$=g.morphTargetDictionary,Y=g.morphTargetInfluences;if(Y)for(const X in $)X.toLowerCase()===m.toLowerCase()&&(Y[$[X]]=S)}const Wn=t.memo(({avatarUrl:l,isPlayingRef:m,visemeQueueRef:S,audioContextRef:g,responseAudioStartTimeRef:$,adjustments:Y,mood:X,expression:Ke,agentResponse:Pe,isSpeaking:N,nextStartTimeRef:ot,stopPlayback:Se,setIsSpeaking:Qe,expressionUrl:V,onExpressionFinished:ke,isNudgeResponse:G,avatarGender:ie})=>{const{scene:I}=yt.useGLTF(l),fe=ie==="female"?kn:Ln,W=yt.useGLTF(fe),_=t.useMemo(()=>W.flatMap(w=>w.animations),[W]),ne=ie==="female"?Mn:Tn,p=yt.useGLTF(ne),O=t.useMemo(()=>p.flatMap(w=>w.animations),[p]),J=t.useRef(null),T=t.useRef(null),U=t.useRef(null),j=t.useRef(null),q=t.useRef(null),K=t.useRef(null),St=t.useRef([]),[Z]=t.useState(()=>new dt.AnimationMixer(I)),H=t.useRef({}),P=t.useRef(null),Ee=t.useRef(0),Xe=t.useRef(!1),Be=t.useRef(0),Te=t.useRef(!1),be=t.useRef(null),Me=t.useRef(Yt),ht=t.useRef(-1),Ae=t.useRef("left"),We=t.useRef(_t+Math.random()*(Xt-_t)),de=t.useRef(0),Ie=t.useRef("smile"),Oe=t.useRef({});t.useEffect(()=>{if(!(!_||!I)){if(_.forEach((w,b)=>{const o=`idle_${b}`;if(!H.current[o]){const f=Z.clipAction(w,I);f.name=o,f.setLoop(dt.LoopOnce,1),f.clampWhenFinished=!0,H.current[o]=f}}),O.forEach((w,b)=>{const o=`talk_${b}`;if(!H.current[o]){const f=Z.clipAction(w,I);f.name=o,f.setLoop(dt.LoopOnce,1),f.clampWhenFinished=!0,H.current[o]=f}}),_.length>0){const w=H.current.idle_0,b=P.current&&Z.existingAction(P.current.getClip());w&&!b&&(w.reset().fadeIn(.5).play(),P.current=w)}return()=>{Z.stopAllAction(),H.current={},P.current=null}}},[_,O,I,Z]);const Ze=t.useRef("");t.useEffect(()=>{if(!V||!I||V===Ze.current)return;Ze.current=V,be.current=V,new yn.GLTFLoader().load(V,b=>{if(b.animations&&b.animations.length>0){const o=b.animations[0],f=Z.clipAction(o,I);if(f.name=`EXPR_${V}`,f.setLoop(dt.LoopOnce,1),f.clampWhenFinished=!0,H.current[`EXPR_${V}`]=f,N&&be.current===V){const R=P.current;f.reset().fadeIn(.3).play(),R&&R!==f&&R.crossFadeTo(f,.3,!0),P.current=f,be.current=null}}},void 0,b=>{console.error(`[ANIMATION] Failed to load ${V}`,b)})},[V,I,Z,N]),t.useEffect(()=>{const w=b=>{const o=b.action,f=o.name||"";if(f.startsWith("idle_")){const ee=(parseInt(f.split("_")[1])+1)%_.length,te=H.current[`idle_${ee}`];te&&(te.reset().fadeIn(.5).play(),o.crossFadeTo(te,.5,!0),P.current=te)}else if(f.startsWith("EXPR_")){if(N){const R=Math.floor(Math.random()*O.length),ee=H.current[`talk_${R}`];ee&&(ee.reset().fadeIn(.5).play(),o.crossFadeTo(ee,.5,!0),P.current=ee)}else{const R=H.current.idle_0;R&&(R.reset().fadeIn(.5).play(),o.crossFadeTo(R,.5,!0),P.current=R)}ke&&ke()}else if(f.startsWith("talk_"))if(N&&!G){const ee=(parseInt(f.split("_")[1])+1)%O.length,te=H.current[`talk_${ee}`];te&&(te.reset().fadeIn(.3).play(),o.crossFadeTo(te,.3,!0),P.current=te)}else{const R=H.current.idle_0;R&&(R.reset().fadeIn(.5).play(),o.crossFadeTo(R,.5,!0),P.current=R)}};return Z.addEventListener("finished",w),()=>Z.removeEventListener("finished",w)},[Z,_,O,N,G,ke]),t.useEffect(()=>{if(N&&I){const w=P.current,b=w?.name||"";if(b.startsWith("idle_")||b.startsWith("talk_")||b===""){const o=be.current;if(o){const f=H.current[`EXPR_${o}`];if(f){f.reset().fadeIn(.3).play(),w&&w!==f&&w.crossFadeTo(f,.3,!0),P.current=f,be.current=null;return}}if(b.startsWith("idle_")||b===""){const f=Math.floor(Math.random()*O.length),R=H.current[`talk_${f}`];R&&(R.reset().fadeIn(.5).play(),w&&w.crossFadeTo(R,.5,!0),P.current=R)}}}else if(!N&&I){const w=P.current,b=w?.name||"";if(b.startsWith("talk_")||b.startsWith("EXPR_")){const o=H.current.idle_0;o&&(o.reset().fadeIn(.5).play(),w&&w.crossFadeTo(o,.5,!0),P.current=o)}}},[N,I,V]),t.useEffect(()=>{if(!I)return;I.traverse(o=>{if(o.isMesh&&o.morphTargetDictionary){const f=o.name.toLowerCase();(f.includes("head")||f.includes("avatar"))&&(T.current=o,C(`[ANIMATION] Found head mesh: ${o.name}`)),f.includes("teeth")&&(U.current=o,C(`[ANIMATION] Found teeth mesh: ${o.name}`)),f.includes("beard")&&(j.current=o,C(`[ANIMATION] Found beard mesh: ${o.name}`)),f.includes("eyeleft")&&(q.current=o,C(`[ANIMATION] Found eye left mesh: ${o.name}`)),f.includes("eyeright")&&(K.current=o,C(`[ANIMATION] Found eye right mesh: ${o.name}`))}});const w=T.current?.morphTargetDictionary;w&&Object.keys(w).filter(o=>o.toLowerCase().includes("brow"));const b=[];I.traverse(o=>{if(o.isMesh){const R=o.morphTargetDictionary;R&&Object.keys(R).some(ee=>ee.toLowerCase().includes("brow"))&&b.push(o)}}),St.current=b,b.length>0&&C("[ANIMATION] Meshes with brow morphs:",b.length)},[I]);const it=w=>{const b=wt[w]??wt.neutral;for(const o in b)u(T.current,o,b[o]),u(U.current,o,b[o]),u(j.current,o,b[o])};return Ct.useFrame((w,b)=>{it(X);const o=w.clock.elapsedTime;let f=0;if(Math.floor(o)%5===0&&Math.floor((o-b)%5)!==0){let h=null;I.traverse(E=>{E.name.toLowerCase().includes("hips")&&(h=E)});const A=h?`Hips Y: ${h.position.y.toFixed(4)}`:"Hips not found";C(`[ANIMATION] Mixer Time: ${Z.time.toFixed(2)}, ${A}`)}if(Z.update(b),o>Ee.current&&!Xe.current&&(Xe.current=!0,Be.current=o),Xe.current){const h=N?.2:.3,A=(o-Be.current)/h;if(A>=1)if(Xe.current=!1,Te.current){const E=N?1:2.5;Ee.current=o+E,Te.current=!1}else Ee.current=o+.12,Te.current=!0;else{const E=A<.5?A*2:(1-A)*2;u(T.current,"eyeBlinkLeft",E),u(T.current,"eyeBlinkRight",E),u(U.current,"eyeBlinkLeft",E),u(U.current,"eyeBlinkRight",E),u(j.current,"eyeBlinkLeft",E),u(j.current,"eyeBlinkRight",E),f=E*Dn.browInnerUp}}const R=wt[X]??wt.neutral,ee=R.browInnerUp??0,te=R.browOuterUpLeft??0,ae=R.browOuterUpRight??0,et=o*Cn,De=At*Math.sin(et),$e=At*.7*Math.sin(et+.7),tt=At*.7*Math.sin(et+1.3),k=h=>Math.max(0,Math.min(1,h)),at=k(ee+De),Ve=k(te+$e),He=k(ae+tt),Ge=k(at+f);u(T.current,"browInnerUp",Ge),u(U.current,"browInnerUp",Ge),u(j.current,"browInnerUp",Ge),u(T.current,"browOuterUpLeft",Ve),u(U.current,"browOuterUpLeft",Ve),u(j.current,"browOuterUpLeft",Ve),u(T.current,"browOuterUpRight",He),u(U.current,"browOuterUpRight",He),u(j.current,"browOuterUpRight",He);const Re=Me.current,re=Re+bt,Ce=re+It;o>Ce&&(Me.current+=bt+It+Yt);const Je=!N&&o>=Re&&o<re,Ne=!N&&o>=re&&o<Ce;(Je||Ne)&&ht.current!==Re&&(ht.current=Re,Ae.current=Math.random()<.7?"left":"up");const ce=Je?o-Re:Ne?bt:0,qe=Je||Ne?Math.min(1,ce/bt):0,ct=qe<=0?0:Math.sin(qe*Math.PI),Rt=Ne?1-(o-re)/It:1,ut=Je?ct:Ne?ct*Rt:0,z=Nn*ut,M=h=>R[h]??0;if(Ae.current==="up"?(u(q.current,"eyeLookUpLeft",k(M("eyeLookUpLeft")+z)),u(q.current,"eyeLookDownLeft",k(M("eyeLookDownLeft"))),u(q.current,"eyeLookOutLeft",k(M("eyeLookOutLeft"))),u(q.current,"eyeLookInLeft",k(M("eyeLookInLeft"))),u(K.current,"eyeLookUpRight",k(M("eyeLookUpRight")+z)),u(K.current,"eyeLookDownRight",k(M("eyeLookDownRight"))),u(K.current,"eyeLookOutRight",k(M("eyeLookOutRight"))),u(K.current,"eyeLookInRight",k(M("eyeLookInRight")))):(u(q.current,"eyeLookUpLeft",k(M("eyeLookUpLeft"))),u(q.current,"eyeLookDownLeft",k(M("eyeLookDownLeft"))),u(q.current,"eyeLookOutLeft",k(M("eyeLookOutLeft"))),u(q.current,"eyeLookInLeft",k(M("eyeLookInLeft")+z)),u(K.current,"eyeLookUpRight",k(M("eyeLookUpRight"))),u(K.current,"eyeLookDownRight",k(M("eyeLookDownRight"))),u(K.current,"eyeLookOutRight",k(M("eyeLookOutRight")+z)),u(K.current,"eyeLookInRight",k(M("eyeLookInRight")))),!N&&o>=We.current){de.current=o,Ie.current=Math.random()<.5?"pucker":"smile";const h=Ie.current==="pucker"?Qt:Kt;We.current=o+h+_t+Math.random()*(Xt-_t)}const ue=Ie.current==="pucker"?Qt:Kt,lt=!N&&o>=de.current&&o<de.current+ue,Ue=lt?(o-de.current)/ue:0,he=Ue<=0?0:Ue<.5?Ue*2:(()=>{const h=(Ue-.5)*2;return 1-h*h*(3-2*h)})(),ve=h=>R[h]??0;if(lt)if(Ie.current==="pucker"){const h=k(ve("mouthPucker")+Un*he);u(T.current,"mouthPucker",h),u(U.current,"mouthPucker",h),u(j.current,"mouthPucker",h)}else{const h=k(ve("mouthSmileLeft")+Zt*he),A=k(ve("mouthSmileRight")+Zt*he),E=k(ve("mouthDimpleLeft")+en*he),_e=k(ve("mouthDimpleRight")+en*he);u(T.current,"mouthSmileLeft",h),u(T.current,"mouthSmileRight",A),u(T.current,"mouthDimpleLeft",E),u(T.current,"mouthDimpleRight",_e),u(U.current,"mouthSmileLeft",h),u(U.current,"mouthSmileRight",A),u(U.current,"mouthDimpleLeft",E),u(U.current,"mouthDimpleRight",_e),u(j.current,"mouthSmileLeft",h),u(j.current,"mouthSmileRight",A),u(j.current,"mouthDimpleLeft",E),u(j.current,"mouthDimpleRight",_e)}if(J.current){const h=m.current?0:Y.rotation[1];J.current.rotation.y=dt.MathUtils.lerp(J.current.rotation.y,h,.1),J.current.position.set(...Y.position),J.current.scale.setScalar(Y.scale),J.current.rotation.x=Y.rotation[0],J.current.rotation.z=Y.rotation[2]}const le={};if(Ot.forEach(h=>{le[h]=0}),m.current&&g.current){const h=g.current.currentTime,_e=(h-$.current)*1e3- -150;for(let Fe=0;Fe<S.current.length;Fe++){const xe=S.current[Fe];if(_e<xe.vtime||_e>=xe.vtime+xe.vduration)continue;const mt=jn(_e,xe.vtime,xe.vduration),vt=`viseme_${xe.viseme}`.toLowerCase(),je=sn.find(se=>se.key.toLowerCase()===vt);if(!je)continue;const me=(xe.weight??1)*mt;for(const se in je.mix)le[se]=(le[se]??0)+je.mix[se]*me;if(je.teethMix)for(const se in je.teethMix)le[se]=(le[se]??0)+je.teethMix[se]*me}h>ot.current+.5&&(Se(),Qe(!1))}const nt=Oe.current;Ot.forEach(h=>{const A=le[h]??0,E=nt[h]??0;nt[h]=Math.max(0,Math.min(1,E+(A-E)*(1-Math.exp(-12*b))))}),Ot.forEach(h=>{const A=nt[h]??0;u(T.current,h,A),u(j.current,h,A),u(U.current,h,A)})}),c.jsx("group",{ref:J,children:c.jsx("primitive",{object:I})})});function $n(l){return l?l.charAt(0).toUpperCase()+l.slice(1).toLowerCase():""}function rn(l){return l.replace(/\*(.*?)\*/g," $1 ").replace(/\[.*?\]/g," ").replace(/<.*?\/>/g," ").replace(/<.*?>.*?<\/.*?>/g," ").replace(/\s+/g," ").trim()}const Dt=new Map,Vn=({onNavigationRequested:l,agentId:m,presetUserDetails:S,onAvatarReady:g})=>{const[$,Y]=t.useState(""),[X,Ke]=t.useState("male"),[Pe,N]=t.useState(""),[ot,Se]=t.useState(!1),Qe=$,V=X;t.useEffect(()=>{if(!m){Se(!0);return}let e=!1;return(async()=>{console.log(">> fetchConfig RUNNING for",m);try{const s=`agent_config_${m}`,i=sessionStorage.getItem(s);if(i)try{const a=JSON.parse(i);e||(a.encryptedAgentToken&&Y(a.encryptedAgentToken),a.avatarGender&&Ke(a.avatarGender),a.agent_id&&N(a.agent_id),Se(!0));return}catch(a){console.error("Failed to parse cached avatar config:",a),sessionStorage.removeItem(s)}let r;if(Dt.has(m))console.log(">> REUSING in-flight fetch for",m),r=await Dt.get(m);else{const a=(async()=>{console.log(">> INITIATING visitedWebsite fetch for",m);const d={agentId:m},y=await fetch(`${st}/visitedWebsite`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(d)});if(!y.ok)throw new Error(`Fetch failed: ${y.status}`);const v=await y.json();return sessionStorage.setItem(s,JSON.stringify(v)),v})();Dt.set(m,a),r=await a}if(e)return;r&&(r.encryptedAgentToken&&Y(r.encryptedAgentToken),r.avatarGender&&Ke(r.avatarGender),r.agent_id&&N(r.agent_id))}catch(s){console.error("Failed to fetch avatar config:",s)}finally{e||Se(!0)}})(),()=>{e=!0}},[m]);const ke=xn(m??Pe),[G,ie]=t.useState(""),[I,fe]=t.useState(""),[W,_]=t.useState("Ready"),[ne,p]=t.useState(!1),[O,J]=t.useState(!1),[T,U]=t.useState([]),[j,q]=t.useState(""),K=t.useRef(null),[St,Z]=t.useState(()=>typeof window<"u"?window.matchMedia("(max-width: 480px)").matches:!1);t.useEffect(()=>{const e=window.matchMedia("(max-width: 480px)"),n=()=>Z(e.matches);return e.addEventListener("change",n),()=>e.removeEventListener("change",n)},[]);const H=St?80:600,P=t.useRef(!1),Ee=t.useRef([]),Xe=t.useRef(0),Be=t.useRef(!1),Te=t.useRef([]),be=t.useRef(null),Me=t.useRef([]);t.useRef([]);const ht=t.useRef([]),Ae=t.useRef(0),We=t.useRef(0),de=t.useRef(0),Ie=t.useRef(0),Oe=t.useRef(!1),[Ze,it]=t.useState(null),w=t.useRef(null),[b,o]=t.useState("neutral"),[f,R]=t.useState(""),[ee,te]=t.useState(""),[ae,et]=t.useState(()=>{if(typeof window>"u")return null;try{const e=window.sessionStorage.getItem("leadCaptureAskFor");return e&&e!==""?e:null}catch{return null}}),[De,$e]=t.useState("hidden"),[tt,k]=t.useState(""),at=t.useRef(null),Ve=t.useRef(De);Ve.current=De;const[He,Ge]=t.useState(null),Re=t.useRef(null),re=t.useRef(null),[Ce,Je]=t.useState(!1),Ne=t.useRef(null),[ce,qe]=t.useState("hidden"),[ct,Rt]=t.useState(""),ut=t.useRef(ce);ut.current=ce;const z=t.useRef(""),M=t.useRef(!1),ue=t.useRef(""),lt=t.useRef(Date.now()),Ue=t.useRef([]),he=t.useRef(!1),[ve,le]=t.useState(!1),[nt,h]=t.useState(!1),A=t.useRef(null),E=t.useMemo(()=>Ce?"Try again":W==="Busy"?"Busy":W==="Thinking..."||W==="Processing Voice..."?W:ae!=null&&ae!==""&&ae!=="none"&&ae!=="<none>"?`Enter ${$n(ae)}`:ce!=="hidden"||!nt?null:tn,[W,ae,Ce,ce,nt]),_e=!Ce&&W!=="Busy"&&W!=="Thinking..."&&W!=="Processing Voice..."&&(ae==null||ae===""||ae==="none"||ae==="<none>");t.useEffect(()=>{if(ce!=="hidden"||!_e){h(!1),A.current!=null&&(clearTimeout(A.current),A.current=null);return}return A.current=setTimeout(()=>{A.current=null,h(!0)},400),()=>{A.current!=null&&(clearTimeout(A.current),A.current=null)}},[ce,_e]);const Fe=E!=null&&E!=="";t.useEffect(()=>{const e=Ve.current;if(!(e==="exiting"||e==="entering")){if(e==="hidden"){Fe&&(k(E??""),$e("entering"));return}e==="visible"&&(!Fe||E!==tt)&&(at.current=Fe?E:null,$e("exiting"))}},[Fe,E,De,tt]),t.useEffect(()=>{if(W!=="Busy"){re.current!=null&&(clearTimeout(re.current),re.current=null);return}return re.current=setTimeout(()=>{re.current=null,_("Ready"),Je(!0)},12e3),()=>{re.current!=null&&(clearTimeout(re.current),re.current=null)}},[W]),t.useEffect(()=>{if(!Ce)return;const e=setTimeout(()=>Je(!1),2500);return()=>clearTimeout(e)},[Ce]);const xe=t.useCallback(()=>{const e=Ve.current;if(e==="exiting"){$e("hidden");const n=at.current;at.current=null,n!=null&&n!==""&&(k(n),$e("entering"))}else e==="entering"&&$e("visible")},[]),mt=e=>{if(!e)return;if(e.mood!=null){const r=String(e.mood).toLowerCase();o(r)}if(e.expression!=null){const r=String(e.expression).trim();R(r);const a=En.find(d=>d.name.toLowerCase()===r.toLowerCase());C(`[STREAM] Animation match for "${r}": ${a?a.name:"NONE"}`),te(a?.url??"")}if(e.navigation!=null){const r=String(e.navigation).trim();if(r!==""){let a=!1;if(typeof window<"u")try{const d=new URL(window.location.href),y=new URL(r,window.location.href),v=d.hostname==="leads.streamoji.com"||d.hostname.includes("localhost");if(y.origin!==d.origin&&!v){C("[STREAM] Navigation rejected: different origin");return}const L=x=>x.replace(/\/+$/,"")||"/";a=L(d.pathname)===L(y.pathname)}catch{}if(a)return;l?l(r):Ge(r)}}const n=e.ask_for||e.lead_capture?.ask_for,s=n?String(n).trim().toLowerCase():"",i=s==="none"||s==="<none>";if(n&&!i){const r=s;if(S&&(r==="email"&&S.email||r==="name"&&S.name||r==="phone"&&S.phone))return;if(typeof window<"u")try{window.sessionStorage.setItem("leadCaptureAskFor",r)}catch{}et(r||null)}else if(i||e.ask_for===null||e.lead_capture&&e.lead_capture.ask_for===null||e.ask_for==="none"){if(typeof window<"u")try{window.sessionStorage.removeItem("leadCaptureAskFor")}catch{}et(null)}e.collected,e.valid},vt=e=>{const n=e.trim();if(!n)return null;if(n.startsWith("{"))try{return JSON.parse(n)}catch{return null}if(n.includes(":")){const s=n.split(":"),i=s[0].trim().toLowerCase(),r=s.slice(1).join(":").trim();return{[i]:r}}return null},je=t.useCallback(()=>{},[]),me="§",se=()=>{for(;z.current.includes(`
|
|
2
|
+
`);){const e=z.current.indexOf(`
|
|
3
|
+
`),n=z.current.slice(0,e).trim();z.current=z.current.slice(e+1);const s=vt(n);s&&mt(s)}},Nt=e=>{if(M.current)if(e.includes(me)){const n=e.indexOf(me),s=e.slice(0,n),i=e.slice(n+me.length);s&&(ue.current+=s,fe(r=>r+s)),M.current=!1,i&&(z.current+=i,se())}else e===me?M.current=!1:(ue.current+=e,fe(n=>n+e));else if(e.includes(me)){const n=e.indexOf(me),s=e.slice(0,n),i=e.slice(n+me.length);z.current+=s,se(),M.current=!0,i&&(ue.current+=i,fe(r=>r+i))}else if(e===me){M.current=!0;return}else z.current+=e,se()};t.useEffect(()=>{(async()=>{if(!sessionStorage.getItem("STREAMOJI_LEADS_SESSION_LEAD_ID")){const r="secret",a=Math.floor(Date.now()/1e3).toString();try{const d=new TextEncoder,y=await globalThis.crypto.subtle.importKey("raw",d.encode(r),{name:"HMAC",hash:"SHA-256"},!1,["sign"]),v=await globalThis.crypto.subtle.sign("HMAC",y,d.encode(a)),x=Array.from(new Uint8Array(v)).map(D=>D.toString(16).padStart(2,"0")).join("");sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",x),C("[SESSION] New HMAC UID generated and saved:",x)}catch(d){console.error("[SESSION] HMAC generation failed:",d);const y=Math.random().toString(36)+Date.now().toString();sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",y)}}const s=sessionStorage.getItem("STREAMOJI_LEADS_SESSION_MESSAGES");let i=[];if(s)try{i=JSON.parse(s)}catch{i=[]}i.length===0&&S&&(S.name||S.email)?(i=[{role:"user",content:`PRESET_USER_DETAILS: Name is "${S.name||""}" , Email is "${S.email||""}"`}],sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify(i))):s||sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify([]))})()},[S]),t.useEffect(()=>{const e=()=>{lt.current=Date.now(),he.current&&(he.current=!1,le(!1),U([]),q(""),fe(""),J(!1))};window.addEventListener("mousemove",e),window.addEventListener("keydown",e),window.addEventListener("mousedown",e),window.addEventListener("touchstart",e);const n=setInterval(async()=>{if(Date.now()-lt.current>=3e4&&!O&&!ne&&W==="Ready"&&!he.current)if(he.current=!0,Ue.current.length>0)le(!0),U(Ue.current);else try{const r=`${st}/nudgeUser`,a={navigationUrl:window.location.href,agentId:m},d=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)});if(d.ok){const v=(await d.json()).nudge_questions;v&&v.length>0&&(C("[NUDGE] Received nudge questions from API:",v),Ue.current=v,le(!0),U(v))}}catch(r){console.error("[NUDGE] Error calling /nudgeUser:",r),he.current=!1}},1e3);return()=>{window.removeEventListener("mousemove",e),window.removeEventListener("keydown",e),window.removeEventListener("mousedown",e),window.removeEventListener("touchstart",e),clearInterval(n)}},[Qe,O,ne,W]),t.useEffect(()=>{if(T.length===0)return;let e=0,n=0,s=!1,i=100;const r=()=>{const a=T[e];if(s){const d=a.substring(0,n-1);q(d),n--,i=50}else{const d=a.substring(0,n+1);q(d),n++,i=100}!s&&n===a.length?(s=!0,i=3e3):s&&n===0&&(s=!1,e=(e+1)%T.length,i=500),K.current=setTimeout(r,i)};return r(),()=>{K.current&&clearTimeout(K.current)}},[T]);const Ut=()=>{try{return JSON.parse(sessionStorage.getItem("STREAMOJI_LEADS_SESSION_MESSAGES")||"[]")}catch{return[]}},on=e=>{sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify(e))},[xt,Lt]=t.useState(!1),[Ft,kt]=t.useState(0),we=t.useRef(null),pt=t.useRef([]),jt=t.useRef(0),[Pt,an]=t.useState(null),[cn,Et]=t.useState(null),Le=t.useRef(null),ze=t.useRef(null),ft=t.useCallback((e=!1)=>{e&&(Oe.current=!0,p(!1),_("Ready")),Te.current=[],Ee.current=[],Be.current=!1,J(!1),Ae.current=0,We.current=0,de.current=0,Me.current.forEach(n=>{try{n.stop()}catch{}}),Re.current&&(clearTimeout(Re.current),Re.current=null),setTimeout(()=>{Ge(null),te("")},10),Me.current=[]},[]),un=async()=>{try{const e=await navigator.mediaDevices.getUserMedia({audio:!0}),n=window.AudioContext||window.webkitAudioContext,s=new n,i=s.createMediaStreamSource(e),r=s.createAnalyser();r.fftSize=64,i.connect(r),Le.current=s,ze.current=i,Et(r);const a=new MediaRecorder(e);we.current=a,pt.current=[],a.ondataavailable=d=>{d.data.size>0&&pt.current.push(d.data)},a.onstop=async()=>{const d=Date.now()-jt.current;if(Et(null),ze.current&&(ze.current.disconnect(),ze.current=null),Le.current&&Le.current.state!=="closed"&&(Le.current.close(),Le.current=null),d<1e3){_("Recording too short. Hold or click longer."),p(!1);return}const y=new Blob(pt.current,{type:"audio/wav"});await hn(y)},jt.current=Date.now(),a.start(100),Lt(!0),_("Listening...")}catch(e){console.error("Error accessing microphone:",e),_("Mic Access Error")}},ln=()=>{we.current&&we.current.state!=="inactive"&&(we.current.stop(),we.current.stream.getTracks().forEach(e=>e.stop()),Lt(!1))},fn=()=>{we.current&&we.current.state!=="inactive"&&(we.current.onstop=null,we.current.stop(),we.current.stream.getTracks().forEach(e=>e.stop()),Et(null),ze.current&&(ze.current.disconnect(),ze.current=null),Le.current&&Le.current.state!=="closed"&&(Le.current.close(),Le.current=null),Lt(!1),pt.current=[],_("Ready"))};t.useEffect(()=>{if(!O)return;const e=()=>{const n=de.current;if(n<=0)return;const s=be.current,i=Ae.current;if(!s)return;const r=s.currentTime-i,a=Math.min(Math.max(0,r),n),y=rn(I).trim().length;if(y<=0)return;const v=Math.min(Math.round(a/n*y),y);it(v)};return clearInterval(w.current??void 0),w.current=setInterval(e,90),()=>clearInterval(w.current??void 0)},[O,I,de.current]),t.useEffect(()=>{let e;return xt?(kt(0),e=window.setInterval(()=>{kt(n=>n+1)},1e3)):kt(0),()=>clearInterval(e)},[xt]);const dn=e=>{const n=e.numberOfChannels,s=e.length*n*2+44,i=new ArrayBuffer(s),r=new DataView(i);let a=0;const d=x=>{r.setUint16(a,x,!0),a+=2},y=x=>{r.setUint32(a,x,!0),a+=4};y(1179011410),y(s-8),y(1163280727),y(544501094),y(16),d(1),d(n),y(e.sampleRate),y(e.sampleRate*2*n),d(n*2),d(16),y(1635017060),y(s-a-4);const v=[];for(let x=0;x<n;x++)v.push(e.getChannelData(x));let L=0;for(;a<s;){for(let x=0;x<n;x++){let D=Math.max(-1,Math.min(1,v[x][L]));D=D<0?D*32768:D*32767,r.setInt16(a,D,!0),a+=2}L++}return new Blob([i],{type:"audio/wav"})},Tt=async(e,n,s=!1)=>{if(!Oe.current){if(P.current){Ee.current.push({audio:e,visemes:n,isNewSegment:s});return}P.current=!0;try{const i=window.AudioContext||window.webkitAudioContext,r=be.current??new i;r.state==="suspended"&&await r.resume(),be.current=r;const a=window.atob(e),d=new Uint8Array(a.length);for(let B=0;B<a.length;B++)d[B]=a.charCodeAt(B);const y=await r.decodeAudioData(d.buffer.slice(0));de.current+=y.duration;const v=r.currentTime;let L=We.current;const x=!Be.current;L<v&&(L=v+.1),We.current=L+y.duration;const D=r.createBufferSource();D.buffer=y;let Q=Pt;if((!Q||Q.context!==r)&&(Q=r.createAnalyser(),Q.fftSize=64,Q.connect(r.destination),an(Q)),D.connect(Q),Me.current.push(D),Oe.current){Me.current=Me.current.filter(B=>B!==D);return}if(x){Be.current=!0,J(!0),C(`[AUDIO] setIsSpeaking(true) - First chunk starting at ${L.toFixed(3)}`),Ae.current=L;const B=(L-v)*1e3;Xe.current=performance.now()+B,C(`[AUDIO] Response started. Initial startTime: ${L.toFixed(3)}, CT: ${v.toFixed(3)}`)}D.start(L);const oe=(L-Ae.current)*1e3;s&&(Ie.current=oe,C(`[AUDIO] New segment detected at +${oe.toFixed(0)}ms. Resetting segment offset.`)),n.forEach((B,pe)=>{const F=B.symbol??"";if(F){const ge=Pn(F),ye=Math.round(B.start*1e3),Ye=Math.round((B.duration??0)*1e3),Gt=Ie.current+ye;pe<3&&C(`[AUDIO] Viseme "${F}": segment_relative=${ye}ms, segment_offset=${Ie.current.toFixed(0)}ms => vtime=${Gt}ms`),ge.forEach(Jt=>{Te.current.push({viseme:Jt.v,weight:Jt.w,vtime:Gt,vduration:Ye})})}}),_("Speaking...")}finally{if(P.current=!1,Ee.current.length>0){const i=Ee.current.shift();i&&Tt(i.audio,i.visemes,i.isNewSegment)}}}},hn=async e=>{try{p(!0),le(!1),ue.current="",fe(""),_("Processing Voice...");const n=await e.arrayBuffer(),i=await new(window.AudioContext||window.webkitAudioContext)().decodeAudioData(n),r=dn(i),a=new FileReader;a.readAsDataURL(r),a.onloadend=async()=>{const d=a.result.split(",")[1];ft(),ie(""),z.current="",M.current=!1;const y=`${st}/stt?token=${encodeURIComponent(Qe)}`,v=await fetch(y,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({audio_base64:d,audio_format:"wav"})});if(v.status===429){try{const pe=await v.text(),F=JSON.parse(pe);C("[STT] 429 agent at capacity:",F?.detail)}catch{}ie(""),_("Busy"),p(!1);return}if(!v.ok){const pe=await v.text();let F="STT Failed";try{F=JSON.parse(pe).error||F}catch{pe&&(F=pe.slice(0,200))}throw new Error(F)}const L=v.body;if(C("this is body"+L),!L){_("STT Failed"),p(!1);return}const x=L.getReader();Oe.current=!1;const D=new TextDecoder;let Q="",oe=!1;const B=async(pe,F)=>{switch(pe){case"transcript":F.transcript!=null&&ie(String(F.transcript));break;case"text":{const ge=F.delta??F.text??"";ge&&Nt(ge);break}case"audio":{const ge=F.chunk,ye=F.visemes??[],Ye=!!F.is_new_segment;ge&&await Tt(ge,ye,Ye);break}case"done":{oe=!0,_("Ready"),p(!1);break}case"error":{oe=!0,_("STT Failed"),p(!1);break}default:break}};for(;;){const{done:pe,value:F}=await x.read();F&&(Q+=D.decode(F,{stream:!0}));const ge=Q.split(`
|
|
4
4
|
|
|
5
|
-
`);Q=
|
|
5
|
+
`);Q=ge.pop()??"";for(const ye of ge){const Ye=rt(ye);Ye&&await B(Ye.event,Ye.data)}if(pe){if(Q.trim()){const ye=rt(Q.trim());ye&&await B(ye.event,ye.data)}oe||(_("Ready"),p(!1));break}}}}catch(n){console.error("Audio Submission Error:",n),_("STT Failed"),p(!1)}},mn=async e=>{e&&e.preventDefault(),le(!1),ue.current="",fe(""),!(!G||ne)&&await pn(G)},rt=e=>{const n=e.split(/\r?\n/);let s="",i="";for(const a of n)a.startsWith("event:")?s=a.slice(6).trim():a.startsWith("data:")&&(i=a.slice(5).trim());if(!s)return null;let r={};if(i)try{r=JSON.parse(i)}catch{r={raw:i}}return{event:s,data:r}},gt=(e,n)=>{switch(e){case"connected":z.current="",M.current=!1;break;case"text":{const s=n.delta??"";s&&Nt(s);break}case"metadata":{mt(n);break}case"audio":{const s=n.chunk,i=n.visemes??[],r=n.is_new_segment??!1;s&&Tt(s,i,r);break}case"done":{const s=Ut(),i=ue.current.trim(),r=[...s,{role:"user",content:G||"..."},{role:"assistant",content:i}];on(r),ht.current=[...Te.current],_("Ready"),p(!1),ie("");break}case"error":{const s=n.message??"Unknown error";ue.current=s,fe(s),_("Agent Failed"),p(!1);break}}},Bt=async(e,n)=>{p(!0),_("Thinking..."),ue.current="",z.current="",M.current=!1,ft(),de.current=0,it(0);try{const s=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});if(s.status===429){_("Busy"),p(!1);return}if(!s.ok)throw new Error("Request failed");const i=s.body;if(!i){_("Failed"),p(!1);return}const r=i.getReader();Oe.current=!1;const a=new TextDecoder;let d="";for(;;){const{done:y,value:v}=await r.read();v&&(d+=a.decode(v,{stream:!0}));const L=d.split(`
|
|
6
6
|
|
|
7
|
-
`);
|
|
7
|
+
`);d=L.pop()??"";for(const x of L){const D=rt(x);D&>(D.event,D.data)}if(y){if(d.trim()){const x=rt(d.trim());x&>(x.event,x.data)}_("Ready"),p(!1);break}}}catch(s){console.error("Streaming Error:",s),_("Failed"),p(!1)}},Wt=t.useCallback(async(e,n)=>{const s=`${st}/avatar_tts`;await Bt(s,{user_query:e,llm_prompt:n})},[]),$t=t.useCallback(async(e,n,s,i)=>{const r=`${st}/avatar_ttsWithKnowledge`;await Bt(r,{user_query:e,llm_prompt:n,knowledge:s,history:i})},[]),Vt=t.useRef(!1);t.useEffect(()=>{g&&!Vt.current&&(g({avatarSpeak:Wt,avatarRespond:$t}),Vt.current=!0)},[g,Wt,$t]);const pn=async e=>{p(!0),_("Thinking..."),ue.current="",z.current="",M.current=!1,ft(),de.current=0,it(0);const n=`${st}/agent/chat?token=${encodeURIComponent(Qe)}`;try{const s=Ut();let i=sessionStorage.getItem("STREAMOJI_LEADS_SESSION_LEAD_ID");i||(Rn("[CHAT] Session UID missing at send time! Generating emergency backup."),i="emergency-"+Math.random().toString(36).substring(7),sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",i));const r={history:s,question:e,lead_id:i};C("[CHAT] Sending payload:",r);const a=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r),cache:"default"});if(a.status===429){try{const x=await a.json();C("[CHAT] 429 agent at capacity:",x?.detail)}catch{}ie(""),_("Busy"),p(!1);return}if(!a.ok)throw new Error("Agent request failed");const d=a.body;if(!d){_("Agent Failed"),p(!1);return}const y=d.getReader();Oe.current=!1;const v=new TextDecoder;let L="";for(;;){const{done:x,value:D}=await y.read();C(`[SSE] Chunk received. done=${x}, length=${D?.length||0}`),D&&(L+=v.decode(D,{stream:!0}));const Q=L.split(`
|
|
8
8
|
|
|
9
|
-
`);
|
|
9
|
+
`);L=Q.pop()??"";for(const oe of Q){C(`[SSE] Processing block: ${oe.slice(0,50)}...`);const B=rt(oe);B&&(C(`[SSE] Event: ${B.event}`),gt(B.event,B.data))}if(x){if(C("[SSE] Stream finished"),L.trim()){const oe=rt(L.trim());oe&>(oe.event,oe.data)}_("Ready"),p(!1),ie("");break}}}catch(s){console.error("Chat Error:",s),_("Agent Failed"),p(!1)}},gn=I.trim(),Ht=rn(gn),Mt=Ht&&O?Ht.slice(0,Ze!=null&&Ze>0?Ze:0):"";t.useEffect(()=>{const e=ut.current;if(e!=="exiting"){if(Mt)Rt(Mt),e==="hidden"&&qe("entering");else if(e==="visible"||e==="entering"){if(!l&&He){const n=He;try{window.location.href=n}catch{window.location.assign(n)}Ge(null)}qe("exiting")}}},[Mt,ce,l,He]);const bn=t.useCallback(()=>{const e=ut.current;e==="entering"?qe("visible"):e==="exiting"&&qe("hidden")},[]);t.useLayoutEffect(()=>{const e=Ne.current;e&&(e.scrollTop=e.scrollHeight)},[ct]);const _n=V==="female"?On:In;return ot?c.jsxs("div",{className:"avatar-widget-container",children:[c.jsxs("div",{className:"avatar-input-area",children:[De!=="hidden"?c.jsx("div",{className:`avatar-thinking-tab${De==="exiting"?" avatar-thinking-tab--exiting":De==="entering"?" avatar-thinking-tab--entering":""}`,onAnimationEnd:xe,children:tt===tn?c.jsx("a",{href:"https://leads.streamoji.com",target:"_blank",rel:"noopener noreferrer",children:"Made by Streamoji Leads"}):tt}):null,c.jsx("div",{className:"avatar-input-container",children:c.jsx("div",{style:{display:"flex",alignItems:"center",width:"100%",height:"100%"},children:xt?c.jsxs("div",{className:"avatar-input-recording",children:[c.jsx("button",{type:"button",className:"avatar-recording-cancel",onClick:fn,title:"Cancel",children:c.jsxs("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2.5",strokeLinecap:"round",strokeLinejoin:"round",style:{display:"block"},children:[c.jsx("line",{x1:"18",y1:"6",x2:"6",y2:"18"}),c.jsx("line",{x1:"6",y1:"6",x2:"18",y2:"18"})]})}),c.jsxs("div",{style:{flex:1,height:"100%",position:"relative",display:"flex",alignItems:"center",minWidth:0},children:[c.jsx("div",{style:{flex:1,height:"100%"},children:c.jsx(qt,{analyser:cn})}),c.jsxs("span",{style:{fontSize:"0.75rem",color:"#64748b",fontWeight:500,marginLeft:"4px",minWidth:"32px",textAlign:"right",fontVariantNumeric:"tabular-nums"},children:[Math.floor(Ft/60),":",String(Ft%60).padStart(2,"0")]})]}),c.jsx("button",{type:"button",className:"avatar-recording-confirm",onClick:ln,title:"Send",children:c.jsx("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2.5",strokeLinecap:"round",strokeLinejoin:"round",style:{display:"block"},children:c.jsx("polyline",{points:"20 6 9 17 4 12"})})})]}):O&&!ve?c.jsxs("div",{className:"avatar-input-speaking",children:[c.jsx("div",{style:{flex:1,height:"100%",display:"flex",alignItems:"center",paddingRight:"8px"},children:c.jsx(qt,{analyser:Pt})}),c.jsx("button",{type:"button",className:"avatar-speaking-stop",onClick:()=>ft(!0),title:"Stop",children:c.jsx("span",{className:"avatar-speaking-stop__icon","aria-hidden":!0})})]}):ne?c.jsx("div",{style:{flex:1,height:"100%",display:"flex",alignItems:"center",justifyContent:"center"},children:c.jsx("div",{className:"avatar-input-loader"})}):c.jsxs("form",{onSubmit:mn,style:{flex:1,display:"flex",height:"100%",alignItems:"center"},children:[c.jsx("input",{id:"avatar-text-input",type:"text",value:G,onChange:e=>ie(e.target.value),placeholder:W==="Busy"?"Assisting another user":j||"Ask me anything",disabled:ne||W==="Busy",autoComplete:"off",style:{width:"100%",height:"100%"}}),W==="Busy"?c.jsx("button",{type:"button",className:"mic-button",disabled:!0,style:{backgroundColor:"#1e4a5e"},title:"Agent at capacity",children:c.jsx("svg",{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",children:c.jsx("path",{d:"M4 2L20 2L12 10L4 2z M12 14L4 22L20 22L12 14z",fill:"white"})})}):G.trim()===""?c.jsx("button",{type:"button",className:"mic-button",onClick:un,disabled:ne,style:{backgroundColor:"#1e4a5e"},children:c.jsxs("svg",{width:"28",height:"28",viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:[c.jsx("path",{d:"M12 14C13.66 14 15 12.66 15 11V5C15 3.34 13.66 2 12 2C10.34 2 9 3.34 9 5V11C9 12.66 10.34 14 12 14Z",fill:"white"}),c.jsx("path",{d:"M17 11C17 13.76 14.76 16 12 16C9.24 16 7 13.76 7 11H5C5 14.53 7.61 17.43 11 17.93V21H13V17.93C16.39 17.43 19 14.53 19 11H17Z",fill:"white"})]})}):c.jsx("button",{type:"submit",className:"mic-button",disabled:ne,style:{backgroundColor:"#1e4a5e"},title:"Send",children:c.jsx("svg",{width:24,height:24,viewBox:"0 0 24 24",fill:"none","aria-hidden":"true",children:c.jsx("path",{d:"M19 12H5M19 12L14 17M19 12L14 7",stroke:"white",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})]})})})]}),c.jsx("div",{className:"avatar-wrapper",children:c.jsxs("div",{className:"avatar-scene-wrapper",children:[ce!=="hidden"&&c.jsx("div",{className:`avatar-bubble${ce==="entering"?" avatar-bubble--entering":ce==="exiting"?" avatar-bubble--exiting":""}`,onAnimationEnd:bn,children:c.jsx("div",{ref:Ne,className:`avatar-bubble__content${ve?" avatar-bubble__content--nudge":""}`,children:ct})}),c.jsx("div",{className:"avatar-canvas-layer",style:{width:H,height:H},children:c.jsxs(Ct.Canvas,{shadows:!0,camera:{position:[.2,1.4,3],fov:42},gl:{alpha:!0},dpr:1.8,style:{pointerEvents:"none",width:"100%",height:"100%"},children:[c.jsx(Bn,{target:_n}),c.jsx("ambientLight",{intensity:.7}),c.jsx("directionalLight",{position:[0,2,2],intensity:1}),c.jsx(yt.Environment,{preset:"city"}),c.jsx(t.Suspense,{fallback:null,children:ke!==null&&c.jsx(Wn,{avatarUrl:ke,isPlayingRef:Be,visemeQueueRef:Te,audioContextRef:be,responseAudioStartTimeRef:Ae,adjustments:An,mood:b,expression:f,agentResponse:I,isSpeaking:O,nextStartTimeRef:We,stopPlayback:ft,setIsSpeaking:J,expressionUrl:ee,onExpressionFinished:je,isNudgeResponse:ve,avatarGender:V})})]})})]})})]}):null},Hn=({agentId:l,onNavigationRequested:m,presetUserDetails:S,onAvatarReady:g})=>c.jsx(Vn,{agentId:l,onNavigationRequested:m,presetUserDetails:S,onAvatarReady:g});exports.AvatarWidget=Hn;
|