@streamoji/avatar-widget 0.4.6 → 0.4.7
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 +5 -5
- package/dist/avatar-widget.js +254 -254
- package/dist/avatar-widget.umd.js +44 -44
- package/dist/avatar-widget.umd.js.map +1 -1
- 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)=>{},Gt=({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 Ye=()=>{Y=requestAnimationFrame(Ye),(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 Ye(),()=>{cancelAnimationFrame(Y)}},[f]),l.jsx("canvas",{ref:w,style:{width:"100%",height:"100%",display:"block"}})},Ln="https://pub-48df6f7d60d6440bbd01676ea5d90e55.r2.dev",Jt="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:Jt)}).catch(()=>{S||b(Jt)}),()=>{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,qt=3,dt=.55,Tt=.12,zt=2,Yt=2,mt=4,Kt=8,Fn=1,Qt=.38,Xt=.32,Zt="__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:Ye,agentResponse:Fe,isSpeaking:C,nextStartTimeRef:Ze,stopPlayback:Se,setIsSpeaking:Ne,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),Pe=t.useRef(0),Ee=t.useRef(!1),ge=t.useRef(null),Te=t.useRef(qt),st=t.useRef(-1),Me=t.useRef("left"),je=t.useRef(mt+Math.random()*(Kt-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,Pe.current=o),Ke.current){const y=C?.2:.3,z=(o-Pe.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,Be=I.browOuterUpRight??0,te=o*Cn,ot=Et*Math.sin(te),Ie=Et*.7*Math.sin(te+.7),We=Et*.7*Math.sin(te+1.3),v=y=>Math.max(0,Math.min(1,y)),it=v(ee+ot),Ve=v(_t+Ie),$e=v(Be+We),at=v(it+d);u(E.current,"browInnerUp",at),u(A.current,"browInnerUp",at),u(F.current,"browInnerUp",at),u(E.current,"browOuterUpLeft",Ve),u(A.current,"browOuterUpLeft",Ve),u(F.current,"browOuterUpLeft",Ve),u(E.current,"browOuterUpRight",$e),u(A.current,"browOuterUpRight",$e),u(F.current,"browOuterUpRight",$e);const He=Te.current,De=He+dt,he=De+Tt;o>he&&(Te.current+=dt+Tt+qt);const be=!C&&o>=He&&o<De,Oe=!C&&o>=De&&o<he;(be||Oe)&&st.current!==He&&(st.current=He,Me.current=Math.random()<.7?"left":"up");const ct=be?o-He:Oe?dt:0,re=be||Oe?Math.min(1,ct/dt):0,Ge=re<=0?0:Math.sin(re*Math.PI),ut=Oe?1-(o-De)/Tt:1,wt=be?Ge:Oe?Ge*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>=je.current){fe.current=o,Ae.current=Math.random()<.5?"pucker":"smile";const y=Ae.current==="pucker"?Yt:zt;je.current=o+y+mt+Math.random()*(Kt-mt)}const ie=Ae.current==="pucker"?Yt:zt,ae=!C&&o>=fe.current&&o<fe.current+ie,Je=ae?(o-fe.current)/ie:0,Re=Je<=0?0:Je<.5?Je*2:(()=>{const y=(Je-.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")+Qt*Re),z=v(de("mouthSmileRight")+Qt*Re),N=v(de("mouthDimpleLeft")+Xt*Re),Le=v(de("mouthDimpleRight")+Xt*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(),Ne(!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 en(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,Ye]=t.useState("male"),[Fe,C]=t.useState(""),[Ze,Se]=t.useState(!1),Ne=V,$=X;t.useEffect(()=>{if(!w){Se(!0);return}let e=!1;return(async()=>{console.log(">> fetchConfig RUNNING for",w);try{const r=`agent_config_${w}`,i=sessionStorage.getItem(r);if(i)try{const a=JSON.parse(i);e||(a.encryptedAgentToken&&Y(a.encryptedAgentToken),a.avatarGender&&Ye(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(r)}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(r,JSON.stringify(L)),L})();At.set(w,a),n=await a}if(e)return;n&&(n.encryptedAgentToken&&Y(n.encryptedAgentToken),n.avatarGender&&Ye(n.avatarGender),n.agent_id&&C(n.agent_id))}catch(r){console.error("Failed to fetch avatar config:",r)}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)"),s=()=>Z(e.matches);return e.addEventListener("change",s),()=>e.removeEventListener("change",s)},[]);const H=bt?80:600,j=t.useRef(!1),ke=t.useRef([]),Ke=t.useRef(0),Pe=t.useRef(!1),Ee=t.useRef([]),ge=t.useRef(null),Te=t.useRef([]);t.useRef([]);const st=t.useRef([]),Me=t.useRef(0),je=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,Be]=t.useState("Chat with us"),[te,ot]=t.useState(null),[Ie,We]=t.useState("hidden"),[v,it]=t.useState(""),Ve=t.useRef(null),$e=t.useRef(Ie);$e.current=Ie;const[at,He]=t.useState(null),De=t.useRef(null),he=t.useRef(null),[be,Oe]=t.useState(!1),ct=t.useRef(null),[re,Ge]=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(""),Je=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:Zt,[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=$e.current;if(!(e==="exiting"||e==="entering")){if(e==="hidden"){lt&&(it(ue??""),We("entering"));return}e==="visible"&&(!lt||ue!==v)&&(Ve.current=lt?ue:null,We("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 tn=t.useCallback(()=>{const e=$e.current;if(e==="exiting"){We("hidden");const s=Ve.current;Ve.current=null,s!=null&&s!==""&&(it(s),We("entering"))}else e==="entering"&&We("visible")},[]),nn=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 s=e.ask_for||e.lead_capture?.ask_for,r=s?String(s).trim().toLowerCase():"",i=r==="none"||r==="<none>";if(s&&!i){const n=r;if(b&&(n==="email"&&b.email||n==="name"&&b.name||n==="phone"&&b.phone))return;ot(n||null),Be(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"&&Be("Chat with us"));e.collected,e.valid},rn=e=>{const s=e.trim();if(!s)return null;if(s.startsWith("{"))try{return JSON.parse(s)}catch{return null}if(s.includes(":")){const r=s.split(":"),i=r[0].trim().toLowerCase(),n=r.slice(1).join(":").trim();return{[i]:n}}return null},sn=t.useCallback(()=>{},[]),Ue="§",St=()=>{for(;x.current.includes(`
|
|
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",F=(...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 Ne=S.width,C=S.height;if(Ne===0||C===0)return;const Ze=C/2;V.clearRect(0,0,Ne,C),V.fillStyle="#1e293b";const Se=2,$=Se+2,xe=Ne*.95,G=Math.floor(xe/$);(!b.current||b.current.length!==G)&&(b.current=new Float32Array(G).fill(2));const oe=G*$,M=(Ne-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*_),N=Math.floor((p+1)*_);for(let q=A;q<N;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*$,N=Ze-E/2;V.beginPath(),V.roundRect?V.roundRect(A,N,Se,E,4):V.fillRect(A,N,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,Fn=.5,zt=3,dt=.55,Tt=.12,Yt=2,Kt=2,mt=4,Qt=8,Nn=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}},Un=[{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:Ne,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),N=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),Ue=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,F(`[ANIMATION] Found head mesh: ${c.name}`)),o.includes("teeth")&&(A.current=c,F(`[ANIMATION] Found teeth mesh: ${c.name}`)),o.includes("beard")&&(N.current=c,F(`[ANIMATION] Found beard mesh: ${c.name}`)),o.includes("eyeleft")&&(q.current=c,F(`[ANIMATION] Found eye left mesh: ${c.name}`)),o.includes("eyeright")&&(K.current=c,F(`[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&&F("[ANIMATION] Meshes with brow morphs:",h.length)},[M]);const et=(g,h=1)=>{const c=`viseme_${g}`.toLowerCase(),o=Un.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(N.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(N.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(N.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(U=>{U.name.toLowerCase().includes("hips")&&(y=U)});const z=y?`Hips Y: ${y.position.y.toFixed(4)}`:"Hips not found";F(`[ANIMATION] Mixer Time: ${Z.time.toFixed(2)}, ${z}`)}if(Z.update(h),o>ke.current&&!Ke.current&&(Ke.current=!0,Ue.current=o),Ke.current){const y=C?.2:.3,z=(o-Ue.current)/y;if(z>=1)if(Ke.current=!1,Ee.current){const U=C?1:2.5;ke.current=o+U,Ee.current=!1}else ke.current=o+.12,Ee.current=!0;else{const U=z<.5?z*2:(1-z)*2;u(E.current,"eyeBlinkLeft",U),u(E.current,"eyeBlinkRight",U),u(A.current,"eyeBlinkLeft",U),u(A.current,"eyeBlinkRight",U),u(N.current,"eyeBlinkLeft",U),u(N.current,"eyeBlinkRight",U),d=U*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(N.current,"browInnerUp",at),u(E.current,"browOuterUpLeft",We),u(A.current,"browOuterUpLeft",We),u(N.current,"browOuterUpLeft",We),u(E.current,"browOuterUpRight",Ve),u(A.current,"browOuterUpRight",Ve),u(N.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=Fn*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")+Nn*Re);u(E.current,"mouthPucker",y),u(A.current,"mouthPucker",y),u(N.current,"mouthPucker",y)}else{const y=v(de("mouthSmileLeft")+Xt*Re),z=v(de("mouthSmileRight")+Xt*Re),U=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",U),u(E.current,"mouthDimpleRight",Le),u(A.current,"mouthSmileLeft",y),u(A.current,"mouthSmileRight",z),u(A.current,"mouthDimpleLeft",U),u(A.current,"mouthDimpleRight",Le),u(N.current,"mouthSmileLeft",y),u(N.current,"mouthSmileRight",z),u(N.current,"mouthDimpleLeft",U),u(N.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"),[Ne,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??Ne),[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([]),[N,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),Ue=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),[U,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"||!U?null:en,[W,te,be,re,U]),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());F(`[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(()=>{},[]),Fe="§",St=()=>{for(;x.current.includes(`
|
|
2
2
|
`);){const e=x.current.indexOf(`
|
|
3
|
-
`),
|
|
3
|
+
`),r=x.current.slice(0,e).trim();x.current=x.current.slice(e+1);const s=rn(r);s&&Ot(s)}},Ct=e=>{if(ie.current)if(e.includes(Fe)){const r=e.indexOf(Fe),s=e.slice(0,r),i=e.slice(r+Fe.length);s&&(ae.current+=s,le(n=>n+s)),ie.current=!1,i&&(x.current+=i,St())}else e===Fe?ie.current=!1:(ae.current+=e,le(r=>r+e));else if(e.includes(Fe)){const r=e.indexOf(Fe),s=e.slice(0,r),i=e.slice(r+Fe.length);x.current+=s,St(),ie.current=!0,i&&(ae.current+=i,le(n=>n+i))}else if(e===Fe){ie.current=!0;return}else x.current+=e,St()};t.useEffect(()=>{(async()=>{if(!sessionStorage.getItem("STREAMOJI_LEADS_SESSION_LEAD_ID")){const n="secret",a=Math.floor(Date.now()/1e3).toString();try{const m=new TextEncoder,R=await crypto.subtle.importKey("raw",m.encode(n),{name:"HMAC",hash:"SHA-256"},!1,["sign"]),L=await crypto.subtle.sign("HMAC",R,m.encode(a)),k=Array.from(new Uint8Array(L)).map(O=>O.toString(16).padStart(2,"0")).join("");sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",k),F("[SESSION] New HMAC UID generated and saved:",k)}catch(m){console.error("[SESSION] HMAC generation failed:",m);const R=Math.random().toString(36)+Date.now().toString();sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",R)}}const s=sessionStorage.getItem("STREAMOJI_LEADS_SESSION_MESSAGES");let i=[];if(s)try{i=JSON.parse(s)}catch{i=[]}i.length===0&&b&&(b.name||b.email)?(i=[{role:"user",content:`PRESET_USER_DETAILS: Name is "${b.name||""}" , Email is "${b.email||""}"`}],sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify(i))):s||sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify([]))})()},[b]),t.useEffect(()=>{const e=()=>{Ge.current=Date.now(),de.current&&(de.current=!1,z(!1),A([]),q(""),le(""),J(!1))};window.addEventListener("mousemove",e),window.addEventListener("keydown",e),window.addEventListener("mousedown",e),window.addEventListener("touchstart",e);const r=setInterval(async()=>{if(Date.now()-Ge.current>=3e4&&!D&&!ne&&W==="Ready"&&!de.current)if(de.current=!0,Re.current.length>0)z(!0),A(Re.current);else try{const n=`${Xe}/nudgeUser`,a={navigationUrl:window.location.href,agentId:w},m=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)});if(m.ok){const L=(await m.json()).nudge_questions;L&&L.length>0&&(F("[NUDGE] Received nudge questions from API:",L),Re.current=L,z(!0),A(L))}}catch(n){console.error("[NUDGE] Error calling /nudgeUser:",n),de.current=!1}},1e3);return()=>{window.removeEventListener("mousemove",e),window.removeEventListener("keydown",e),window.removeEventListener("mousedown",e),window.removeEventListener("touchstart",e),clearInterval(r)}},[Ye,D,ne,W]),t.useEffect(()=>{if(E.length===0)return;let e=0,r=0,s=!1,i=100;const n=()=>{const a=E[e];if(s){const m=a.substring(0,r-1);q(m),r--,i=50}else{const m=a.substring(0,r+1);q(m),r++,i=100}!s&&r===a.length?(s=!0,i=3e3):s&&r===0&&(s=!1,e=(e+1)%E.length,i=500),K.current=setTimeout(n,i)};return n(),()=>{K.current&&clearTimeout(K.current)}},[E]);const Ft=()=>{try{return JSON.parse(sessionStorage.getItem("STREAMOJI_LEADS_SESSION_MESSAGES")||"[]")}catch{return[]}},on=e=>{sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify(e))},[yt,Rt]=t.useState(!1),[Nt,Lt]=t.useState(0),_e=t.useRef(null),ft=t.useRef([]),Ut=t.useRef(0),[Pt,an]=t.useState(null),[cn,vt]=t.useState(null),ve=t.useRef(null),Je=t.useRef(null),nt=t.useCallback((e=!1)=>{e&&(ye.current=!0,p(!1),_("Ready")),Ee.current=[],ke.current=[],Ue.current=!1,J(!1),Me.current=0,Pe.current=0,fe.current=0,Te.current.forEach(r=>{try{r.stop()}catch{}}),De.current&&(clearTimeout(De.current),De.current=null),$e(null),ee(""),Te.current=[]},[]),un=async()=>{try{const e=await navigator.mediaDevices.getUserMedia({audio:!0}),r=window.AudioContext||window.webkitAudioContext,s=new r,i=s.createMediaStreamSource(e),n=s.createAnalyser();n.fftSize=64,i.connect(n),ve.current=s,Je.current=i,vt(n);const a=new MediaRecorder(e);_e.current=a,ft.current=[],a.ondataavailable=m=>{m.data.size>0&&ft.current.push(m.data)},a.onstop=async()=>{const m=Date.now()-Ut.current;if(vt(null),Je.current&&(Je.current.disconnect(),Je.current=null),ve.current&&ve.current.state!=="closed"&&(ve.current.close(),ve.current=null),m<1e3){_("Recording too short. Hold or click longer."),p(!1);return}const R=new Blob(ft.current,{type:"audio/wav"});await dn(R)},Ut.current=Date.now(),a.start(100),Rt(!0),_("Listening...")}catch(e){console.error("Error accessing microphone:",e),_("Mic Access Error")}},ln=()=>{_e.current&&_e.current.state!=="inactive"&&(_e.current.stop(),_e.current.stream.getTracks().forEach(e=>e.stop()),Rt(!1))},fn=()=>{_e.current&&_e.current.state!=="inactive"&&(_e.current.onstop=null,_e.current.stop(),_e.current.stream.getTracks().forEach(e=>e.stop()),vt(null),Je.current&&(Je.current.disconnect(),Je.current=null),ve.current&&ve.current.state!=="closed"&&(ve.current.close(),ve.current=null),Rt(!1),ft.current=[],_("Ready"))};t.useEffect(()=>{if(!D)return;const e=()=>{const r=fe.current;if(r<=0)return;const s=ge.current,i=Me.current;if(!s)return;const n=s.currentTime-i,a=Math.min(Math.max(0,n),r),R=tn(M).trim().length;if(R<=0)return;const L=Math.min(Math.round(a/r*R),R);tt(L)};return clearInterval(g.current??void 0),g.current=setInterval(e,90),()=>clearInterval(g.current??void 0)},[D,M,fe.current]),t.useEffect(()=>{let e;return yt?(Lt(0),e=window.setInterval(()=>{Lt(r=>r+1)},1e3)):Lt(0),()=>clearInterval(e)},[yt]);const hn=e=>{const r=e.numberOfChannels,s=e.length*r*2+44,i=new ArrayBuffer(s),n=new DataView(i);let a=0;const m=k=>{n.setUint16(a,k,!0),a+=2},R=k=>{n.setUint32(a,k,!0),a+=4};R(1179011410),R(s-8),R(1163280727),R(544501094),R(16),m(1),m(r),R(e.sampleRate),R(e.sampleRate*2*r),m(r*2),m(16),R(1635017060),R(s-a-4);const L=[];for(let k=0;k<r;k++)L.push(e.getChannelData(k));let T=0;for(;a<s;){for(let k=0;k<r;k++){let O=Math.max(-1,Math.min(1,L[k][T]));O=O<0?O*32768:O*32767,n.setInt16(a,O,!0),a+=2}T++}return new Blob([i],{type:"audio/wav"})},xt=async(e,r,s=!1)=>{if(!ye.current){if(j.current){ke.current.push({audio:e,visemes:r,isNewSegment:s});return}j.current=!0;try{const i=window.AudioContext||window.webkitAudioContext,n=ge.current??new i;n.state==="suspended"&&await n.resume(),ge.current=n;const a=window.atob(e),m=new Uint8Array(a.length);for(let B=0;B<a.length;B++)m[B]=a.charCodeAt(B);const R=await n.decodeAudioData(m.buffer.slice(0));fe.current+=R.duration;const L=n.currentTime;let T=Pe.current;const k=!Ue.current;T<L&&(T=L+.1),Pe.current=T+R.duration;const O=n.createBufferSource();O.buffer=R;let Q=Pt;if((!Q||Q.context!==n)&&(Q=n.createAnalyser(),Q.fftSize=64,Q.connect(n.destination),an(Q)),O.connect(Q),Te.current.push(O),ye.current){Te.current=Te.current.filter(B=>B!==O);return}if(k){Ue.current=!0,J(!0),F(`[AUDIO] setIsSpeaking(true) - First chunk starting at ${T.toFixed(3)}`),Me.current=T;const B=(T-L)*1e3;Ke.current=performance.now()+B,F(`[AUDIO] Response started. Initial startTime: ${T.toFixed(3)}, CT: ${L.toFixed(3)}`)}O.start(T);const se=(T-Me.current)*1e3;s&&(Ae.current=se,F(`[AUDIO] New segment detected at +${se.toFixed(0)}ms. Resetting segment offset.`)),r.forEach((B,me)=>{const P=B.symbol??"";if(P){const pe=jn(P),we=Math.round(B.start*1e3),qe=Math.round((B.duration??0)*1e3),Ht=Ae.current+we;me<3&&F(`[AUDIO] Viseme "${P}": segment_relative=${we}ms, segment_offset=${Ae.current.toFixed(0)}ms => vtime=${Ht}ms`),pe.forEach(Gt=>{Ee.current.push({viseme:Gt.v,weight:Gt.w,vtime:Ht,vduration:qe})})}}),_("Speaking...")}finally{if(j.current=!1,ke.current.length>0){const i=ke.current.shift();i&&xt(i.audio,i.visemes,i.isNewSegment)}}}},dn=async e=>{try{p(!0),z(!1),ae.current="",le(""),_("Processing Voice...");const r=await e.arrayBuffer(),i=await new(window.AudioContext||window.webkitAudioContext)().decodeAudioData(r),n=hn(i),a=new FileReader;a.readAsDataURL(n),a.onloadend=async()=>{const m=a.result.split(",")[1];nt(),oe(""),x.current="",ie.current=!1;const R=`${Xe}/stt`,L=await fetch(R,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({audio_base64:m,audio_format:"wav",token:Ye})});if(L.status===429){try{const me=await L.text(),P=JSON.parse(me);F("[STT] 429 agent at capacity:",P?.detail)}catch{}oe(""),_("Busy"),p(!1);return}if(!L.ok){const me=await L.text();let P="STT Failed";try{P=JSON.parse(me).error||P}catch{me&&(P=me.slice(0,200))}throw new Error(P)}const T=L.body;if(F("this is body"+T),!T){_("STT Failed"),p(!1);return}const k=T.getReader();ye.current=!1;const O=new TextDecoder;let Q="",se=!1;const B=async(me,P)=>{switch(me){case"transcript":P.transcript!=null&&oe(String(P.transcript));break;case"text":{const pe=P.delta??P.text??"";pe&&Ct(pe);break}case"audio":{const pe=P.chunk,we=P.visemes??[],qe=!!P.is_new_segment;pe&&await xt(pe,we,qe);break}case"done":{se=!0,_("Ready"),p(!1);break}case"error":{se=!0,_("STT Failed"),p(!1);break}default:break}};for(;;){const{done:me,value:P}=await k.read();P&&(Q+=O.decode(P,{stream:!0}));const pe=Q.split(`
|
|
4
4
|
|
|
5
|
-
`);Q=pe.pop()??"";for(const we of pe){const
|
|
5
|
+
`);Q=pe.pop()??"";for(const we of pe){const qe=Qe(we);qe&&await B(qe.event,qe.data)}if(me){if(Q.trim()){const we=Qe(Q.trim());we&&await B(we.event,we.data)}se||(_("Ready"),p(!1));break}}}}catch(r){console.error("Audio Submission Error:",r),_("STT Failed"),p(!1)}},mn=async e=>{e&&e.preventDefault(),z(!1),ae.current="",le(""),!(!G||ne)&&await pn(G)},Qe=e=>{const r=e.split(/\r?\n/);let s="",i="";for(const a of r)a.startsWith("event:")?s=a.slice(6).trim():a.startsWith("data:")&&(i=a.slice(5).trim());if(!s)return null;let n={};if(i)try{n=JSON.parse(i)}catch{n={raw:i}}return{event:s,data:n}},ht=(e,r)=>{switch(e){case"connected":x.current="",ie.current=!1;break;case"text":{const s=r.delta??"";s&&Ct(s);break}case"metadata":{Ot(r);break}case"audio":{const s=r.chunk,i=r.visemes??[],n=r.is_new_segment??!1;s&&xt(s,i,n);break}case"done":{const s=Ft(),i=ae.current.trim(),n=[...s,{role:"user",content:G||"..."},{role:"assistant",content:i}];on(n),st.current=[...Ee.current],_("Ready"),p(!1),oe("");break}case"error":{const s=r.message??"Unknown error";ae.current=s,le(s),_("Agent Failed"),p(!1);break}}},jt=async(e,r)=>{p(!0),_("Thinking..."),ae.current="",x.current="",ie.current=!1,nt(),fe.current=0,tt(0);try{const s=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});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 n=i.getReader();ye.current=!1;const a=new TextDecoder;let m="";for(;;){const{done:R,value:L}=await n.read();L&&(m+=a.decode(L,{stream:!0}));const T=m.split(`
|
|
6
6
|
|
|
7
|
-
`);m=T.pop()??"";for(const k of T){const O=Qe(k);O&&ht(O.event,O.data)}if(R){if(m.trim()){const k=Qe(m.trim());k&&ht(k.event,k.data)}_("Ready"),p(!1);break}}}catch(
|
|
7
|
+
`);m=T.pop()??"";for(const k of T){const O=Qe(k);O&&ht(O.event,O.data)}if(R){if(m.trim()){const k=Qe(m.trim());k&&ht(k.event,k.data)}_("Ready"),p(!1);break}}}catch(s){console.error("Streaming Error:",s),_("Failed"),p(!1)}},Bt=t.useCallback(async(e,r)=>{const s=`${Xe}/avatar_tts`;await jt(s,{user_query:e,llm_prompt:r})},[]),Wt=t.useCallback(async(e,r,s,i)=>{const n=`${Xe}/avatar_ttsWithKnowledge`;await jt(n,{user_query:e,llm_prompt:r,knowledge:s,history:i})},[]),Vt=t.useRef(!1);t.useEffect(()=>{S&&!Vt.current&&(S({avatarSpeak:Bt,avatarRespond:Wt}),Vt.current=!0)},[S,Bt,Wt]);const pn=async e=>{p(!0),_("Thinking..."),ae.current="",x.current="",ie.current=!1,nt(),fe.current=0,tt(0);const r=`${Xe}/agent/chat`;try{const s=Ft();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 n={history:s,question:e,lead_id:i,token:Ye};F("[CHAT] Sending payload:",n);const a=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n),cache:"default"});if(a.status===429){try{const k=await a.json();F("[CHAT] 429 agent at capacity:",k?.detail)}catch{}oe(""),_("Busy"),p(!1);return}if(!a.ok)throw new Error("Agent request failed");const m=a.body;if(!m){_("Agent Failed"),p(!1);return}const R=m.getReader();ye.current=!1;const L=new TextDecoder;let T="";for(;;){const{done:k,value:O}=await R.read();F(`[SSE] Chunk received. done=${k}, length=${O?.length||0}`),O&&(T+=L.decode(O,{stream:!0}));const Q=T.split(`
|
|
8
8
|
|
|
9
|
-
`);T=Q.pop()??"";for(const se of Q){
|
|
9
|
+
`);T=Q.pop()??"";for(const se of Q){F(`[SSE] Processing block: ${se.slice(0,50)}...`);const B=Qe(se);B&&(F(`[SSE] Event: ${B.event}`),ht(B.event,B.data))}if(k){if(F("[SSE] Stream finished"),T.trim()){const se=Qe(T.trim());se&&ht(se.event,se.data)}_("Ready"),p(!1),oe("");break}}}catch(s){console.error("Chat Error:",s),_("Agent Failed"),p(!1)}},gn=M.trim(),$t=tn(gn),kt=$t&&D?$t.slice(0,et!=null&&et>0?et:0):"";t.useEffect(()=>{const e=Ce.current;e!=="exiting"&&(kt?(wt(kt),e==="hidden"&&He("entering")):(e==="visible"||e==="entering")&&He("exiting"))},[kt,re]);const bn=t.useCallback(()=>{const e=Ce.current;e==="entering"?He("visible"):e==="exiting"&&He("hidden")},[]);t.useLayoutEffect(()=>{const e=ct.current;e&&(e.scrollTop=e.scrollHeight)},[ut]);const _n=$==="female"?Dn:In;return Ze?l.jsxs("div",{className:"avatar-widget-container",children:[l.jsxs("div",{className:"avatar-input-area",children:[Ie!=="hidden"?l.jsx("div",{className:`avatar-thinking-tab${Ie==="exiting"?" avatar-thinking-tab--exiting":Ie==="entering"?" avatar-thinking-tab--entering":""}`,onAnimationEnd:nn,children:v===en?l.jsx("a",{href:"https://leads.streamoji.com",target:"_blank",rel:"noopener noreferrer",children:"Made by Streamoji Leads"}):v}):null,l.jsx("div",{className:"avatar-input-container",children:l.jsx("div",{style:{display:"flex",alignItems:"center",width:"100%",height:"100%"},children:yt?l.jsxs("div",{className:"avatar-input-recording",children:[l.jsx("button",{type:"button",className:"avatar-recording-cancel",onClick:fn,title:"Cancel",children:l.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:[l.jsx("line",{x1:"18",y1:"6",x2:"6",y2:"18"}),l.jsx("line",{x1:"6",y1:"6",x2:"18",y2:"18"})]})}),l.jsxs("div",{style:{flex:1,height:"100%",position:"relative",display:"flex",alignItems:"center",minWidth:0},children:[l.jsx("div",{style:{flex:1,height:"100%"},children:l.jsx(Jt,{analyser:cn})}),l.jsxs("span",{style:{fontSize:"0.75rem",color:"#64748b",fontWeight:500,marginLeft:"4px",minWidth:"32px",textAlign:"right",fontVariantNumeric:"tabular-nums"},children:[Math.floor(Nt/60),":",String(Nt%60).padStart(2,"0")]})]}),l.jsx("button",{type:"button",className:"avatar-recording-confirm",onClick:ln,title:"Send",children:l.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:l.jsx("polyline",{points:"20 6 9 17 4 12"})})})]}):D&&!y?l.jsxs("div",{className:"avatar-input-speaking",children:[l.jsx("div",{style:{flex:1,height:"100%",display:"flex",alignItems:"center",paddingRight:"8px"},children:l.jsx(Jt,{analyser:Pt})}),l.jsx("button",{type:"button",className:"avatar-speaking-stop",onClick:()=>nt(!0),title:"Stop",children:l.jsx("span",{className:"avatar-speaking-stop__icon","aria-hidden":!0})})]}):ne?l.jsx("div",{style:{flex:1,height:"100%",display:"flex",alignItems:"center",justifyContent:"center"},children:l.jsx("div",{className:"avatar-input-loader"})}):l.jsxs("form",{onSubmit:mn,style:{flex:1,display:"flex",height:"100%",alignItems:"center"},children:[l.jsx("input",{id:"avatar-text-input",type:"text",value:G,onChange:e=>oe(e.target.value),placeholder:W==="Busy"?"Assisting another user":N||"Ask me anything",disabled:ne||W==="Busy",autoComplete:"off",style:{width:"100%",height:"100%"}}),W==="Busy"?l.jsx("button",{type:"button",className:"mic-button",disabled:!0,style:{backgroundColor:"#1e4a5e"},title:"Agent at capacity",children:l.jsx("svg",{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",children:l.jsx("path",{d:"M4 2L20 2L12 10L4 2z M12 14L4 22L20 22L12 14z",fill:"white"})})}):G.trim()===""?l.jsx("button",{type:"button",className:"mic-button",onClick:un,disabled:ne,style:{backgroundColor:"#1e4a5e"},children:l.jsxs("svg",{width:"28",height:"28",viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:[l.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"}),l.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"})]})}):l.jsx("button",{type:"submit",className:"mic-button",disabled:ne,style:{backgroundColor:"#1e4a5e"},title:"Send",children:l.jsx("svg",{width:24,height:24,viewBox:"0 0 24 24",fill:"none","aria-hidden":"true",children:l.jsx("path",{d:"M19 12H5M19 12L14 17M19 12L14 7",stroke:"white",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})]})})})]}),l.jsx("div",{className:"avatar-wrapper",children:l.jsxs("div",{className:"avatar-scene-wrapper",children:[re!=="hidden"&&l.jsx("div",{className:`avatar-bubble${re==="entering"?" avatar-bubble--entering":re==="exiting"?" avatar-bubble--exiting":""}`,onAnimationEnd:bn,children:l.jsx("div",{ref:ct,className:`avatar-bubble__content${y?" avatar-bubble__content--nudge":""}`,children:ut})}),l.jsx("div",{className:"avatar-canvas-layer",style:{width:H,height:H},children:l.jsxs(It.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:[l.jsx(Bn,{target:_n}),l.jsx("ambientLight",{intensity:.7}),l.jsx("directionalLight",{position:[0,2,2],intensity:1}),l.jsx(gt.Environment,{preset:"city"}),l.jsx(t.Suspense,{fallback:null,children:xe!==null&&l.jsx(Wn,{avatarUrl:xe,isPlayingRef:Ue,visemeQueueRef:Ee,audioContextRef:ge,responseAudioStartTimeRef:Me,adjustments:An,mood:h,expression:o,agentResponse:M,isSpeaking:D,nextStartTimeRef:Pe,stopPlayback:nt,setIsSpeaking:J,expressionUrl:I,onExpressionFinished:sn,isNudgeResponse:y,avatarGender:$})})]})})]})})]}):null},Hn=({agentId:f,onNavigationRequested:w,presetUserDetails:b,onAvatarReady:S})=>l.jsx($n,{agentId:f,onNavigationRequested:w,presetUserDetails:b,onAvatarReady:S});exports.AvatarWidget=Hn;
|