@streamoji/avatar-widget 0.2.5 → 0.2.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.cjs.map +1 -1
- package/dist/avatar-widget.js +480 -466
- package/dist/avatar-widget.js.map +1 -1
- package/dist/avatar-widget.umd.js +155 -155
- package/dist/avatar-widget.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/avatar-widget.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react/jsx-runtime"),Je=require("@react-three/drei"),at=require("@react-three/fiber"),t=require("react"),Wt=require("three"),Bt=require("three/examples/jsm/loaders/GLTFLoader.js");function Vt(l){const w=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(l){for(const b in l)if(b!=="default"){const p=Object.getOwnPropertyDescriptor(l,b);Object.defineProperty(w,b,p.get?p:{enumerable:!0,get:()=>l[b]})}}return w.default=l,Object.freeze(w)}const je=Vt(Wt),st="https://ai.streamoji.com",N=(...l)=>{},Ht=(...l)=>{},St=({analyser:l})=>{const w=t.useRef(null),b=t.useRef(null);return t.useEffect(()=>{const p=w.current;if(!p)return;const A=p.getContext("2d",{alpha:!0});if(!A)return;let j,E=null;l&&(l.fftSize=128,E=new Uint8Array(l.frequencyBinCount));const X=()=>{j=requestAnimationFrame(X),(p.width!==p.offsetWidth||p.height!==p.offsetHeight)&&(p.width=p.offsetWidth,p.height=p.offsetHeight);const ne=p.width,F=p.height;if(ne===0||F===0)return;const W=F/2;A.clearRect(0,0,ne,F),A.fillStyle="#1e293b";const _=2,m=_+2,J=ne*.95,B=Math.floor(J/m);(!b.current||b.current.length!==B)&&(b.current=new Float32Array(B).fill(2));const le=B*m,R=(ne-le)/2;l&&E&&l.getByteFrequencyData(E);const Ce=E?E.length:0,$=Math.floor(Ce*.7)/B,de=new Float32Array(B);for(let T=0;T<B;T++){let C=0;if(E&&$>0){const fe=Math.floor(T*$),L=Math.floor((T+1)*$);for(let v=fe;v<L;v++){const S=E[v]||0;S>C&&(C=S)}}C<10&&(C=0);const U=C/255,M=C>0?Math.max(2,Math.pow(U,1.4)*F*.25):2;de[T]=M}for(let T=0;T<B;T++){const C=B-1-T,U=Math.max(de[T],de[C]),M=b.current[T]+(U-b.current[T])*.3;b.current[T]=M;const fe=R+T*m,L=W-M/2;A.beginPath(),A.roundRect?A.roundRect(fe,L,_,M,4):A.fillRect(fe,L,_,M),A.fill()}};return X(),()=>{cancelAnimationFrame(j)}},[l]),a.jsx("canvas",{ref:w,style:{width:"100%",height:"100%",display:"block"}})},$t="https://pub-48df6f7d60d6440bbd01676ea5d90e55.r2.dev",xt="https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/default-models/fullbodyavatarmale.glb";async function qt(l){const w=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(l));return Array.from(new Uint8Array(w)).map(b=>b.toString(16).padStart(2,"0")).join("")}function Gt(l){const[w,b]=t.useState(null);return t.useEffect(()=>{if(!l){b(null);return}let p=!1;return qt(l).then(A=>{if(p)return;const j=`${$t}/${A}.glb`;fetch(j,{method:"HEAD"}).then(E=>{p||b(E.ok?j:xt)}).catch(()=>{p||b(xt)})}),()=>{p=!0}},[l]),w}const Jt=["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"],zt=["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"],Yt=[{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"}],Qt=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_005.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_007.glb"],Xt={zoom:.85,position:[.15,-.8,0],scale:1.5,rotation:[.15,.02,0]},Kt=[-.45,1.9,.1],Zt={browInnerUp:.2},it=.18,en=1,Ge={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}},tn=[{key:"viseme_aa",mix:{jawOpen:.6}},{key:"viseme_E",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthDimpleLeft:1,mouthDimpleRight:1,jawOpen:.3}},{key:"viseme_I",mix:{mouthPressLeft:.6,mouthPressRight:.6,mouthDimpleLeft:.6,mouthDimpleRight:.6,jawOpen:.2}},{key:"viseme_O",mix:{mouthPucker:1,jawForward:.6,jawOpen:.2}},{key:"viseme_U",mix:{mouthFunnel:1}},{key:"viseme_PP",mix:{mouthRollLower:.3,mouthRollUpper:.3,mouthUpperUpLeft:.3,mouthUpperUpRight:.3}},{key:"viseme_FF",mix:{mouthPucker:1,mouthShrugUpper:1,mouthLowerDownLeft:.2,mouthLowerDownRight:.2,mouthDimpleLeft:1,mouthDimpleRight:1,mouthRollLower:.3}},{key:"viseme_DD",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthFunnel:.5,jawOpen:.2}},{key:"viseme_SS",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthLowerDownLeft:.5,mouthLowerDownRight:.5,jawOpen:.1}},{key:"viseme_TH",mix:{mouthRollUpper:.3,jawOpen:.2,tongueOut:.4}},{key:"viseme_CH",mix:{mouthPucker:.5,jawOpen:.2}},{key:"viseme_RR",mix:{mouthPucker:.5,jawOpen:.2}},{key:"viseme_kk",mix:{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}},{key:"viseme_sil",mix:{}}],nn={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 rn(l){if(!l)return[{v:"sil",w:1}];const w=l.toLowerCase();return nn[w]??[{v:"sil",w:1}]}function sn({target:l}){const{camera:w}=at.useThree();return t.useEffect(()=>{w.lookAt(...l)},[w,l]),null}function G(l,w,b){if(!l||!l.morphTargetDictionary)return;const p=l,A=p.morphTargetDictionary,j=p.morphTargetInfluences;if(j)for(const E in A)E.toLowerCase()===w.toLowerCase()&&(j[A[E]]=b)}function yt(l,w=.97){if(!l)return;const b=l;if(b.morphTargetInfluences)for(let p=0;p<b.morphTargetInfluences.length;p++)b.morphTargetInfluences[p]*=w}const an=t.memo(({avatarUrl:l,isPlayingRef:w,visemeQueueRef:b,audioContextRef:p,responseAudioStartTimeRef:A,adjustments:j,mood:E,expression:X,agentResponse:ne,isSpeaking:F,nextStartTimeRef:W,stopPlayback:_,setIsSpeaking:re,expressionUrl:m,onExpressionFinished:J,isNudgeResponse:B,avatarGender:le})=>{const{scene:R}=Je.useGLTF(l),Ce=le==="female"?zt:Jt,_e=Je.useGLTF(Ce),$=t.useMemo(()=>_e.flatMap(f=>f.animations),[_e]),de=Je.useGLTF(Qt),T=t.useMemo(()=>de.flatMap(f=>f.animations),[de]),C=t.useRef(null),U=t.useRef(null),M=t.useRef(null),fe=t.useRef([]),[L]=t.useState(()=>new je.AnimationMixer(R)),v=t.useRef({}),S=t.useRef(null),he=t.useRef(0),ke=t.useRef(!1),pe=t.useRef(0),se=t.useRef(null);t.useEffect(()=>{if(!(!$||!R)){if($.forEach((f,d)=>{const o=`idle_${d}`;if(!v.current[o]){const s=L.clipAction(f,R);s.name=o,s.setLoop(je.LoopOnce,1),s.clampWhenFinished=!0,v.current[o]=s}}),T.forEach((f,d)=>{const o=`talk_${d}`;if(!v.current[o]){const s=L.clipAction(f,R);s.name=o,s.setLoop(je.LoopOnce,1),s.clampWhenFinished=!0,v.current[o]=s}}),$.length>0){const f=v.current.idle_0,d=S.current&&L.existingAction(S.current.getClip());f&&!d&&(f.reset().fadeIn(.5).play(),S.current=f)}return()=>{L.stopAllAction(),v.current={},S.current=null}}},[$,T,R,L]);const me=t.useRef("");t.useEffect(()=>{if(!m||!R||m===me.current)return;me.current=m,se.current=m,new Bt.GLTFLoader().load(m,d=>{if(d.animations&&d.animations.length>0){const o=d.animations[0],s=L.clipAction(o,R);if(s.name=`EXPR_${m}`,s.setLoop(je.LoopOnce,1),s.clampWhenFinished=!0,v.current[`EXPR_${m}`]=s,F&&se.current===m){const h=S.current;s.reset().fadeIn(.3).play(),h&&h!==s&&h.crossFadeTo(s,.3,!0),S.current=s,se.current=null}}},void 0,d=>{console.error(`[ANIMATION] Failed to load ${m}`,d)})},[m,R,L,F]),t.useEffect(()=>{const f=d=>{const o=d.action,s=o.name||"";if(s.startsWith("idle_")){const q=(parseInt(s.split("_")[1])+1)%$.length,z=v.current[`idle_${q}`];z&&(z.reset().fadeIn(.5).play(),o.crossFadeTo(z,.5,!0),S.current=z)}else if(s.startsWith("EXPR_")){if(F){const h=v.current.talk_0;h&&(h.reset().fadeIn(.5).play(),o.crossFadeTo(h,.5,!0),S.current=h)}else{const h=v.current.idle_0;h&&(h.reset().fadeIn(.5).play(),o.crossFadeTo(h,.5,!0),S.current=h)}J&&J()}else if(s.startsWith("talk_"))if(F&&!B){const q=(parseInt(s.split("_")[1])+1)%T.length,z=v.current[`talk_${q}`];z&&(z.reset().fadeIn(.3).play(),o.crossFadeTo(z,.3,!0),S.current=z)}else{const h=v.current.idle_0;h&&(h.reset().fadeIn(.5).play(),o.crossFadeTo(h,.5,!0),S.current=h)}};return L.addEventListener("finished",f),()=>L.removeEventListener("finished",f)},[L,$,T,F,B,J]),t.useEffect(()=>{if(F&&R){const f=S.current,d=f?.name||"";if(d.startsWith("idle_")||d.startsWith("talk_")||d===""){const o=se.current;if(o){const s=v.current[`EXPR_${o}`];if(s){s.reset().fadeIn(.3).play(),f&&f!==s&&f.crossFadeTo(s,.3,!0),S.current=s,se.current=null;return}}if(d.startsWith("idle_")||d===""){const s=v.current.talk_0;s&&(s.reset().fadeIn(.5).play(),f&&f.crossFadeTo(s,.5,!0),S.current=s)}}}else if(!F&&R){const f=S.current,d=f?.name||"";if(d.startsWith("talk_")||d.startsWith("EXPR_")){const o=v.current.idle_0;o&&(o.reset().fadeIn(.5).play(),f&&f.crossFadeTo(o,.5,!0),S.current=o)}}},[F,R,m]),t.useEffect(()=>{if(!R)return;R.traverse(o=>{if(o.isMesh&&o.morphTargetDictionary){const s=o.name.toLowerCase();(s.includes("head")||s.includes("avatar"))&&(U.current=o,N(`[ANIMATION] Found head mesh: ${o.name}`)),s.includes("teeth")&&(M.current=o,N(`[ANIMATION] Found teeth mesh: ${o.name}`))}});const f=U.current?.morphTargetDictionary;f&&Object.keys(f).filter(o=>o.toLowerCase().includes("brow"));const d=[];R.traverse(o=>{if(o.isMesh){const h=o.morphTargetDictionary;h&&Object.keys(h).some(q=>q.toLowerCase().includes("brow"))&&d.push(o)}}),fe.current=d,d.length>0&&N("[ANIMATION] Meshes with brow morphs:",d.length)},[R]);const De=(f,d=1)=>{const o=`viseme_${f}`.toLowerCase(),s=tn.find(h=>h.key.toLowerCase()===o);if(s)for(const h in s.mix){const q=s.mix[h]*d;G(U.current,h,q),G(M.current,h,q)}},we=f=>{const d=Ge[f]??Ge.neutral;for(const o in d)G(U.current,o,d[o]),G(M.current,o,d[o])};return at.useFrame((f,d)=>{const o=Math.pow(.88,60*d);yt(U.current,o),yt(M.current,o),we(E);const s=f.clock.elapsedTime;let h=0;if(Math.floor(s)%5===0&&Math.floor((s-d)%5)!==0){let H=null;R.traverse(Z=>{Z.name.toLowerCase().includes("hips")&&(H=Z)});const be=H?`Hips Y: ${H.position.y.toFixed(4)}`:"Hips not found";N(`[ANIMATION] Mixer Time: ${L.time.toFixed(2)}, ${be}`)}if(L.update(d),s>he.current&&!ke.current&&(ke.current=!0,pe.current=s),ke.current){const be=(s-pe.current)/.3;if(be>=1)ke.current=!1,he.current=s+2+Math.random()*5;else{const Z=be<.5?be*2:(1-be)*2;G(U.current,"eyeBlinkLeft",Z),G(U.current,"eyeBlinkRight",Z),G(M.current,"eyeBlinkLeft",Z),G(M.current,"eyeBlinkRight",Z),h=Z*Zt.browInnerUp}}const q=Ge[E]??Ge.neutral,z=q.browInnerUp??0,ze=q.browOuterUpLeft??0,Pe=q.browOuterUpRight??0,Oe=s*en,ve=it*Math.sin(Oe),ge=it*.7*Math.sin(Oe+.7),We=it*.7*Math.sin(Oe+1.3),K=H=>Math.max(0,Math.min(1,H)),Se=K(z+ve),xe=K(ze+ge),Fe=K(Pe+We),Le=K(Se+h);if(G(U.current,"browInnerUp",Le),G(M.current,"browInnerUp",Le),G(U.current,"browOuterUpLeft",xe),G(M.current,"browOuterUpLeft",xe),G(U.current,"browOuterUpRight",Fe),G(M.current,"browOuterUpRight",Fe),C.current){const H=w.current?0:j.rotation[1];C.current.rotation.y=je.MathUtils.lerp(C.current.rotation.y,H,.1),C.current.position.set(...j.position),C.current.scale.setScalar(j.scale),C.current.rotation.x=j.rotation[0],C.current.rotation.z=j.rotation[2]}if(w.current&&p.current){const H=p.current.currentTime,Ae=(H-A.current)*1e3- -150;for(let Q=0;Q<b.current.length;Q++){const ie=b.current[Q];Ae>=ie.vtime&&Ae<ie.vtime+ie.vduration&&De(ie.viseme,ie.weight??1)}H>W.current+.5&&(_(),re(!1))}}),a.jsx("group",{ref:C,children:a.jsx("primitive",{object:R})})});function on(l){return l?l.charAt(0).toUpperCase()+l.slice(1).toLowerCase():""}const cn=({token:l,agentToken:w,onNavigationRequested:b,avatarGender:p}={})=>{const A=l??w??"",j=Gt(A||void 0),[E,X]=t.useState(""),[ne,F]=t.useState(""),[W,_]=t.useState("Ready"),[re,m]=t.useState(!1),[J,B]=t.useState(!1),[le,R]=t.useState([]),[Ce,_e]=t.useState(""),$=t.useRef(null),[de,T]=t.useState(()=>typeof window<"u"?window.matchMedia("(max-width: 480px)").matches:!1);t.useEffect(()=>{const e=window.matchMedia("(max-width: 480px)"),n=()=>T(e.matches);return e.addEventListener("change",n),()=>e.removeEventListener("change",n)},[]);const C=de?80:600,U=t.useRef(!1),M=t.useRef([]),fe=t.useRef(0),L=t.useRef(!1),v=t.useRef([]),S=t.useRef(null),he=t.useRef([]);t.useRef([]);const ke=t.useRef([]),pe=t.useRef(0),se=t.useRef(0),me=t.useRef(0),De=t.useRef(0),we=t.useRef(!1),[f,d]=t.useState(null),o=t.useRef(null),[s,h]=t.useState("neutral"),[q,z]=t.useState(""),[ze,Pe]=t.useState(""),[Oe,ve]=t.useState("Chat with us"),[ge,We]=t.useState(null),[K,Se]=t.useState("hidden"),[xe,Fe]=t.useState(""),Le=t.useRef(null),H=t.useRef(K);H.current=K;const[be,Z]=t.useState(null),Ae=t.useRef(null),Q=t.useRef(null),[ie,ot]=t.useState(!1),ct=t.useRef(null),[Ee,Be]=t.useState("hidden"),[ut,Rt]=t.useState(""),Ye=t.useRef(Ee);Ye.current=Ee;const ce=t.useRef(""),Te=t.useRef(!1),Me=t.useRef(""),lt=t.useRef(Date.now()),Qe=t.useRef([]),Ne=t.useRef(!1),[Xe,Ue]=t.useState(!1),dt="__branding__",Ie=t.useMemo(()=>ie?"Try again":W==="Busy"?"Busy":W==="Thinking..."||W==="Processing Voice..."?W:ge!=null&&ge!==""&&ge!=="none"&&ge!=="<none>"?`Enter ${on(ge)}`:dt,[W,ge,ie]),Ve=Ie!=null&&Ie!=="";t.useEffect(()=>{const e=H.current;if(!(e==="exiting"||e==="entering")){if(e==="hidden"){Ve&&(Fe(Ie??""),Se("entering"));return}e==="visible"&&(!Ve||Ie!==xe)&&(Le.current=Ve?Ie:null,Se("exiting"))}},[Ve,Ie,K,xe]),t.useEffect(()=>{if(W!=="Busy"){Q.current!=null&&(clearTimeout(Q.current),Q.current=null);return}return Q.current=setTimeout(()=>{Q.current=null,_("Ready"),ot(!0)},12e3),()=>{Q.current!=null&&(clearTimeout(Q.current),Q.current=null)}},[W]),t.useEffect(()=>{if(!ie)return;const e=setTimeout(()=>ot(!1),2500);return()=>clearTimeout(e)},[ie]);const kt=t.useCallback(()=>{const e=H.current;if(e==="exiting"){Se("hidden");const n=Le.current;Le.current=null,n!=null&&n!==""&&(Fe(n),Se("entering"))}else e==="entering"&&Se("visible")},[]),Lt=e=>{if(!e)return;if(e.mood!=null){const r=String(e.mood).toLowerCase();h(r)}if(e.expression!=null){const r=String(e.expression).trim();z(r);const u=Yt.find(g=>g.name.toLowerCase()===r.toLowerCase());N(`[STREAM] Animation match for "${r}": ${u?u.name:"NONE"}`),Pe(u?.url??"")}if(e.navigation!=null){const r=String(e.navigation).trim();r!==""&&(b?b(r):window.open(r,"_blank"))}const n=e.ask_for||e.lead_capture?.ask_for,i=n?String(n).trim().toLowerCase():"",c=i==="none"||i==="<none>";if(n&&!c){const r=i;We(r||null),ve(r==="email"?"Enter your email":r==="name"?"Enter your name":r==="phone"?"Enter your phone number":"Chat with us")}else(c||e.ask_for===null||e.lead_capture&&e.lead_capture.ask_for===null||e.ask_for==="none")&&(We(null),Oe!=="Chat with us"&&ve("Chat with us"));e.collected,e.valid},At=e=>{const n=e.trim();if(!n)return null;if(n.startsWith("{"))try{return JSON.parse(n)}catch{return null}if(n.includes(":")){const i=n.split(":"),c=i[0].trim().toLowerCase(),r=i.slice(1).join(":").trim();return{[c]:r}}return null},Et=t.useCallback(()=>{},[]),ft=e=>{if(Te.current)e==="§"?Te.current=!1:(Me.current+=e,F(n=>n+e));else if(e==="§"){Te.current=!0;return}else for(ce.current+=e;ce.current.includes(`
|
|
2
|
-
`);){const n=
|
|
3
|
-
`),i=
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react/jsx-runtime"),ze=require("@react-three/drei"),ot=require("@react-three/fiber"),e=require("react"),$t=require("three"),qt=require("three/examples/jsm/loaders/GLTFLoader.js");function Gt(l){const w=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(l){for(const b in l)if(b!=="default"){const p=Object.getOwnPropertyDescriptor(l,b);Object.defineProperty(w,b,p.get?p:{enumerable:!0,get:()=>l[b]})}}return w.default=l,Object.freeze(w)}const Be=Gt($t),it="https://ai.streamoji.com",U=(...l)=>{},Jt=(...l)=>{},Rt=({analyser:l})=>{const w=e.useRef(null),b=e.useRef(null);return e.useEffect(()=>{const p=w.current;if(!p)return;const T=p.getContext("2d",{alpha:!0});if(!T)return;let B,A=null;l&&(l.fftSize=128,A=new Uint8Array(l.frequencyBinCount));const Z=()=>{B=requestAnimationFrame(Z),(p.width!==p.offsetWidth||p.height!==p.offsetHeight)&&(p.width=p.offsetWidth,p.height=p.offsetHeight);const se=p.width,E=p.height;if(se===0||E===0)return;const N=E/2;T.clearRect(0,0,se,E),T.fillStyle="#1e293b";const _=2,m=_+2,J=se*.95,W=Math.floor(J/m);(!b.current||b.current.length!==W)&&(b.current=new Float32Array(W).fill(2));const fe=W*m,R=(se-fe)/2;l&&A&&l.getByteFrequencyData(A);const Ce=A?A.length:0,$=Math.floor(Ce*.7)/W,he=new Float32Array(W);for(let I=0;I<W;I++){let C=0;if(A&&$>0){const pe=Math.floor(I*$),L=Math.floor((I+1)*$);for(let v=pe;v<L;v++){const S=A[v]||0;S>C&&(C=S)}}C<10&&(C=0);const j=C/255,M=C>0?Math.max(2,Math.pow(j,1.4)*E*.25):2;he[I]=M}for(let I=0;I<W;I++){const C=W-1-I,j=Math.max(he[I],he[C]),M=b.current[I]+(j-b.current[I])*.3;b.current[I]=M;const pe=R+I*m,L=N-M/2;T.beginPath(),T.roundRect?T.roundRect(pe,L,_,M,4):T.fillRect(pe,L,_,M),T.fill()}};return Z(),()=>{cancelAnimationFrame(B)}},[l]),a.jsx("canvas",{ref:w,style:{width:"100%",height:"100%",display:"block"}})},zt="https://pub-48df6f7d60d6440bbd01676ea5d90e55.r2.dev",kt="https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/default-models/fullbodyavatarmale.glb";async function Yt(l){const w=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(l));return Array.from(new Uint8Array(w)).map(b=>b.toString(16).padStart(2,"0")).join("")}function Qt(l){const[w,b]=e.useState(null);return e.useEffect(()=>{if(!l){b(null);return}let p=!1;return Yt(l).then(T=>{if(p)return;const B=`${zt}/${T}.glb`;fetch(B,{method:"HEAD"}).then(A=>{p||b(A.ok?B:kt)}).catch(()=>{p||b(kt)})}),()=>{p=!0}},[l]),w}const Xt=["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"],Kt=["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"],Zt=[{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"}],en=["https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_005.glb","https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/masculine/expression/M_Talking_Variations_007.glb"],tn={zoom:.85,position:[.15,-.8,0],scale:1.5,rotation:[.15,.02,0]},nn=[-.45,1.9,.1],rn={browInnerUp:.2},at=.18,sn=1,Lt="__branding__",Je={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}},an=[{key:"viseme_aa",mix:{jawOpen:.6}},{key:"viseme_E",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthDimpleLeft:1,mouthDimpleRight:1,jawOpen:.3}},{key:"viseme_I",mix:{mouthPressLeft:.6,mouthPressRight:.6,mouthDimpleLeft:.6,mouthDimpleRight:.6,jawOpen:.2}},{key:"viseme_O",mix:{mouthPucker:1,jawForward:.6,jawOpen:.2}},{key:"viseme_U",mix:{mouthFunnel:1}},{key:"viseme_PP",mix:{mouthRollLower:.3,mouthRollUpper:.3,mouthUpperUpLeft:.3,mouthUpperUpRight:.3}},{key:"viseme_FF",mix:{mouthPucker:1,mouthShrugUpper:1,mouthLowerDownLeft:.2,mouthLowerDownRight:.2,mouthDimpleLeft:1,mouthDimpleRight:1,mouthRollLower:.3}},{key:"viseme_DD",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthFunnel:.5,jawOpen:.2}},{key:"viseme_SS",mix:{mouthPressLeft:.8,mouthPressRight:.8,mouthLowerDownLeft:.5,mouthLowerDownRight:.5,jawOpen:.1}},{key:"viseme_TH",mix:{mouthRollUpper:.3,jawOpen:.2,tongueOut:.4}},{key:"viseme_CH",mix:{mouthPucker:.5,jawOpen:.2}},{key:"viseme_RR",mix:{mouthPucker:.5,jawOpen:.2}},{key:"viseme_kk",mix:{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}},{key:"viseme_sil",mix:{}}],on={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 cn(l){if(!l)return[{v:"sil",w:1}];const w=l.toLowerCase();return on[w]??[{v:"sil",w:1}]}function un({target:l}){const{camera:w}=ot.useThree();return e.useEffect(()=>{w.lookAt(...l)},[w,l]),null}function G(l,w,b){if(!l||!l.morphTargetDictionary)return;const p=l,T=p.morphTargetDictionary,B=p.morphTargetInfluences;if(B)for(const A in T)A.toLowerCase()===w.toLowerCase()&&(B[T[A]]=b)}function Tt(l,w=.97){if(!l)return;const b=l;if(b.morphTargetInfluences)for(let p=0;p<b.morphTargetInfluences.length;p++)b.morphTargetInfluences[p]*=w}const ln=e.memo(({avatarUrl:l,isPlayingRef:w,visemeQueueRef:b,audioContextRef:p,responseAudioStartTimeRef:T,adjustments:B,mood:A,expression:Z,agentResponse:se,isSpeaking:E,nextStartTimeRef:N,stopPlayback:_,setIsSpeaking:ie,expressionUrl:m,onExpressionFinished:J,isNudgeResponse:W,avatarGender:fe})=>{const{scene:R}=ze.useGLTF(l),Ce=fe==="female"?Kt:Xt,ve=ze.useGLTF(Ce),$=e.useMemo(()=>ve.flatMap(f=>f.animations),[ve]),he=ze.useGLTF(en),I=e.useMemo(()=>he.flatMap(f=>f.animations),[he]),C=e.useRef(null),j=e.useRef(null),M=e.useRef(null),pe=e.useRef([]),[L]=e.useState(()=>new Be.AnimationMixer(R)),v=e.useRef({}),S=e.useRef(null),me=e.useRef(0),Te=e.useRef(!1),ge=e.useRef(0),ae=e.useRef(null);e.useEffect(()=>{if(!(!$||!R)){if($.forEach((f,d)=>{const o=`idle_${d}`;if(!v.current[o]){const s=L.clipAction(f,R);s.name=o,s.setLoop(Be.LoopOnce,1),s.clampWhenFinished=!0,v.current[o]=s}}),I.forEach((f,d)=>{const o=`talk_${d}`;if(!v.current[o]){const s=L.clipAction(f,R);s.name=o,s.setLoop(Be.LoopOnce,1),s.clampWhenFinished=!0,v.current[o]=s}}),$.length>0){const f=v.current.idle_0,d=S.current&&L.existingAction(S.current.getClip());f&&!d&&(f.reset().fadeIn(.5).play(),S.current=f)}return()=>{L.stopAllAction(),v.current={},S.current=null}}},[$,I,R,L]);const be=e.useRef("");e.useEffect(()=>{if(!m||!R||m===be.current)return;be.current=m,ae.current=m,new qt.GLTFLoader().load(m,d=>{if(d.animations&&d.animations.length>0){const o=d.animations[0],s=L.clipAction(o,R);if(s.name=`EXPR_${m}`,s.setLoop(Be.LoopOnce,1),s.clampWhenFinished=!0,v.current[`EXPR_${m}`]=s,E&&ae.current===m){const h=S.current;s.reset().fadeIn(.3).play(),h&&h!==s&&h.crossFadeTo(s,.3,!0),S.current=s,ae.current=null}}},void 0,d=>{console.error(`[ANIMATION] Failed to load ${m}`,d)})},[m,R,L,E]),e.useEffect(()=>{const f=d=>{const o=d.action,s=o.name||"";if(s.startsWith("idle_")){const q=(parseInt(s.split("_")[1])+1)%$.length,z=v.current[`idle_${q}`];z&&(z.reset().fadeIn(.5).play(),o.crossFadeTo(z,.5,!0),S.current=z)}else if(s.startsWith("EXPR_")){if(E){const h=v.current.talk_0;h&&(h.reset().fadeIn(.5).play(),o.crossFadeTo(h,.5,!0),S.current=h)}else{const h=v.current.idle_0;h&&(h.reset().fadeIn(.5).play(),o.crossFadeTo(h,.5,!0),S.current=h)}J&&J()}else if(s.startsWith("talk_"))if(E&&!W){const q=(parseInt(s.split("_")[1])+1)%I.length,z=v.current[`talk_${q}`];z&&(z.reset().fadeIn(.3).play(),o.crossFadeTo(z,.3,!0),S.current=z)}else{const h=v.current.idle_0;h&&(h.reset().fadeIn(.5).play(),o.crossFadeTo(h,.5,!0),S.current=h)}};return L.addEventListener("finished",f),()=>L.removeEventListener("finished",f)},[L,$,I,E,W,J]),e.useEffect(()=>{if(E&&R){const f=S.current,d=f?.name||"";if(d.startsWith("idle_")||d.startsWith("talk_")||d===""){const o=ae.current;if(o){const s=v.current[`EXPR_${o}`];if(s){s.reset().fadeIn(.3).play(),f&&f!==s&&f.crossFadeTo(s,.3,!0),S.current=s,ae.current=null;return}}if(d.startsWith("idle_")||d===""){const s=v.current.talk_0;s&&(s.reset().fadeIn(.5).play(),f&&f.crossFadeTo(s,.5,!0),S.current=s)}}}else if(!E&&R){const f=S.current,d=f?.name||"";if(d.startsWith("talk_")||d.startsWith("EXPR_")){const o=v.current.idle_0;o&&(o.reset().fadeIn(.5).play(),f&&f.crossFadeTo(o,.5,!0),S.current=o)}}},[E,R,m]),e.useEffect(()=>{if(!R)return;R.traverse(o=>{if(o.isMesh&&o.morphTargetDictionary){const s=o.name.toLowerCase();(s.includes("head")||s.includes("avatar"))&&(j.current=o,U(`[ANIMATION] Found head mesh: ${o.name}`)),s.includes("teeth")&&(M.current=o,U(`[ANIMATION] Found teeth mesh: ${o.name}`))}});const f=j.current?.morphTargetDictionary;f&&Object.keys(f).filter(o=>o.toLowerCase().includes("brow"));const d=[];R.traverse(o=>{if(o.isMesh){const h=o.morphTargetDictionary;h&&Object.keys(h).some(q=>q.toLowerCase().includes("brow"))&&d.push(o)}}),pe.current=d,d.length>0&&U("[ANIMATION] Meshes with brow morphs:",d.length)},[R]);const Oe=(f,d=1)=>{const o=`viseme_${f}`.toLowerCase(),s=an.find(h=>h.key.toLowerCase()===o);if(s)for(const h in s.mix){const q=s.mix[h]*d;G(j.current,h,q),G(M.current,h,q)}},Se=f=>{const d=Je[f]??Je.neutral;for(const o in d)G(j.current,o,d[o]),G(M.current,o,d[o])};return ot.useFrame((f,d)=>{const o=Math.pow(.88,60*d);Tt(j.current,o),Tt(M.current,o),Se(A);const s=f.clock.elapsedTime;let h=0;if(Math.floor(s)%5===0&&Math.floor((s-d)%5)!==0){let V=null;R.traverse(Y=>{Y.name.toLowerCase().includes("hips")&&(V=Y)});const _e=V?`Hips Y: ${V.position.y.toFixed(4)}`:"Hips not found";U(`[ANIMATION] Mixer Time: ${L.time.toFixed(2)}, ${_e}`)}if(L.update(d),s>me.current&&!Te.current&&(Te.current=!0,ge.current=s),Te.current){const V=E?.2:.3,_e=(s-ge.current)/V;if(_e>=1){Te.current=!1;const Y=E?1:2.5;me.current=s+Y}else{const Y=_e<.5?_e*2:(1-_e)*2;G(j.current,"eyeBlinkLeft",Y),G(j.current,"eyeBlinkRight",Y),G(M.current,"eyeBlinkLeft",Y),G(M.current,"eyeBlinkRight",Y),h=Y*rn.browInnerUp}}const q=Je[A]??Je.neutral,z=q.browInnerUp??0,Ye=q.browOuterUpLeft??0,Pe=q.browOuterUpRight??0,Fe=s*sn,xe=at*Math.sin(Fe),X=at*.7*Math.sin(Fe+.7),We=at*.7*Math.sin(Fe+1.3),ee=V=>Math.max(0,Math.min(1,V)),ye=ee(z+xe),Re=ee(Ye+X),Ne=ee(Pe+We),Ae=ee(ye+h);if(G(j.current,"browInnerUp",Ae),G(M.current,"browInnerUp",Ae),G(j.current,"browOuterUpLeft",Re),G(M.current,"browOuterUpLeft",Re),G(j.current,"browOuterUpRight",Ne),G(M.current,"browOuterUpRight",Ne),C.current){const V=w.current?0:B.rotation[1];C.current.rotation.y=Be.MathUtils.lerp(C.current.rotation.y,V,.1),C.current.position.set(...B.position),C.current.scale.setScalar(B.scale),C.current.rotation.x=B.rotation[0],C.current.rotation.z=B.rotation[2]}if(w.current&&p.current){const V=p.current.currentTime,Ee=(V-T.current)*1e3- -150;for(let K=0;K<b.current.length;K++){const te=b.current[K];Ee>=te.vtime&&Ee<te.vtime+te.vduration&&Oe(te.viseme,te.weight??1)}V>N.current+.5&&(_(),ie(!1))}}),a.jsx("group",{ref:C,children:a.jsx("primitive",{object:R})})});function dn(l){return l?l.charAt(0).toUpperCase()+l.slice(1).toLowerCase():""}const fn=({token:l,agentToken:w,onNavigationRequested:b,avatarGender:p}={})=>{const T=l??w??"",B=Qt(T||void 0),[A,Z]=e.useState(""),[se,E]=e.useState(""),[N,_]=e.useState("Ready"),[ie,m]=e.useState(!1),[J,W]=e.useState(!1),[fe,R]=e.useState([]),[Ce,ve]=e.useState(""),$=e.useRef(null),[he,I]=e.useState(()=>typeof window<"u"?window.matchMedia("(max-width: 480px)").matches:!1);e.useEffect(()=>{const t=window.matchMedia("(max-width: 480px)"),n=()=>I(t.matches);return t.addEventListener("change",n),()=>t.removeEventListener("change",n)},[]);const C=he?80:600,j=e.useRef(!1),M=e.useRef([]),pe=e.useRef(0),L=e.useRef(!1),v=e.useRef([]),S=e.useRef(null),me=e.useRef([]);e.useRef([]);const Te=e.useRef([]),ge=e.useRef(0),ae=e.useRef(0),be=e.useRef(0),Oe=e.useRef(0),Se=e.useRef(!1),[f,d]=e.useState(null),o=e.useRef(null),[s,h]=e.useState("neutral"),[q,z]=e.useState(""),[Ye,Pe]=e.useState(""),[Fe,xe]=e.useState("Chat with us"),[X,We]=e.useState(null),[ee,ye]=e.useState("hidden"),[Re,Ne]=e.useState(""),Ae=e.useRef(null),V=e.useRef(ee);V.current=ee;const[_e,Y]=e.useState(null),Ee=e.useRef(null),K=e.useRef(null),[te,ct]=e.useState(!1),ut=e.useRef(null),[oe,Ve]=e.useState("hidden"),[lt,At]=e.useState(""),Qe=e.useRef(oe);Qe.current=oe;const le=e.useRef(""),Ie=e.useRef(!1),Me=e.useRef(""),dt=e.useRef(Date.now()),Xe=e.useRef([]),Ue=e.useRef(!1),[Ke,je]=e.useState(!1),[ft,ht]=e.useState(!1),we=e.useRef(null),De=e.useMemo(()=>te?"Try again":N==="Busy"?"Busy":N==="Thinking..."||N==="Processing Voice..."?N:X!=null&&X!==""&&X!=="none"&&X!=="<none>"?`Enter ${dn(X)}`:oe!=="hidden"||!ft?null:Lt,[N,X,te,oe,ft]),pt=!te&&N!=="Busy"&&N!=="Thinking..."&&N!=="Processing Voice..."&&(X==null||X===""||X==="none"||X==="<none>");e.useEffect(()=>{if(oe!=="hidden"||!pt){ht(!1),we.current!=null&&(clearTimeout(we.current),we.current=null);return}return we.current=setTimeout(()=>{we.current=null,ht(!0)},400),()=>{we.current!=null&&(clearTimeout(we.current),we.current=null)}},[oe,pt]);const He=De!=null&&De!=="";e.useEffect(()=>{const t=V.current;if(!(t==="exiting"||t==="entering")){if(t==="hidden"){He&&(Ne(De??""),ye("entering"));return}t==="visible"&&(!He||De!==Re)&&(Ae.current=He?De:null,ye("exiting"))}},[He,De,ee,Re]),e.useEffect(()=>{if(N!=="Busy"){K.current!=null&&(clearTimeout(K.current),K.current=null);return}return K.current=setTimeout(()=>{K.current=null,_("Ready"),ct(!0)},12e3),()=>{K.current!=null&&(clearTimeout(K.current),K.current=null)}},[N]),e.useEffect(()=>{if(!te)return;const t=setTimeout(()=>ct(!1),2500);return()=>clearTimeout(t)},[te]);const Et=e.useCallback(()=>{const t=V.current;if(t==="exiting"){ye("hidden");const n=Ae.current;Ae.current=null,n!=null&&n!==""&&(Ne(n),ye("entering"))}else t==="entering"&&ye("visible")},[]),It=t=>{if(!t)return;if(t.mood!=null){const r=String(t.mood).toLowerCase();h(r)}if(t.expression!=null){const r=String(t.expression).trim();z(r);const u=Zt.find(g=>g.name.toLowerCase()===r.toLowerCase());U(`[STREAM] Animation match for "${r}": ${u?u.name:"NONE"}`),Pe(u?.url??"")}if(t.navigation!=null){const r=String(t.navigation).trim();r!==""&&(b?b(r):window.open(r,"_blank"))}const n=t.ask_for||t.lead_capture?.ask_for,i=n?String(n).trim().toLowerCase():"",c=i==="none"||i==="<none>";if(n&&!c){const r=i;We(r||null),xe(r==="email"?"Enter your email":r==="name"?"Enter your name":r==="phone"?"Enter your phone number":"Chat with us")}else(c||t.ask_for===null||t.lead_capture&&t.lead_capture.ask_for===null||t.ask_for==="none")&&(We(null),Fe!=="Chat with us"&&xe("Chat with us"));t.collected,t.valid},Mt=t=>{const n=t.trim();if(!n)return null;if(n.startsWith("{"))try{return JSON.parse(n)}catch{return null}if(n.includes(":")){const i=n.split(":"),c=i[0].trim().toLowerCase(),r=i.slice(1).join(":").trim();return{[c]:r}}return null},Dt=e.useCallback(()=>{},[]),mt=t=>{if(Ie.current)t==="§"?Ie.current=!1:(Me.current+=t,E(n=>n+t));else if(t==="§"){Ie.current=!0;return}else for(le.current+=t;le.current.includes(`
|
|
2
|
+
`);){const n=le.current.indexOf(`
|
|
3
|
+
`),i=le.current.slice(0,n).trim();le.current=le.current.slice(n+1);const c=Mt(i);c&&It(c)}};e.useEffect(()=>{(async()=>{if(!sessionStorage.getItem("STREAMOJI_LEADS_SESSION_LEAD_ID")){const i="secret",c=Math.floor(Date.now()/1e3).toString();try{const r=new TextEncoder,u=await crypto.subtle.importKey("raw",r.encode(i),{name:"HMAC",hash:"SHA-256"},!1,["sign"]),g=await crypto.subtle.sign("HMAC",u,r.encode(c)),x=Array.from(new Uint8Array(g)).map(y=>y.toString(16).padStart(2,"0")).join("");sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",x),U("[SESSION] New HMAC UID generated and saved:",x)}catch(r){console.error("[SESSION] HMAC generation failed:",r);const u=Math.random().toString(36)+Date.now().toString();sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",u)}}sessionStorage.getItem("STREAMOJI_LEADS_SESSION_MESSAGES")||sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify([]))})()},[]),e.useEffect(()=>{const t=()=>{dt.current=Date.now(),Ue.current&&(Ue.current=!1,je(!1),R([]),ve(""),E(""),W(!1))};window.addEventListener("mousemove",t),window.addEventListener("keydown",t),window.addEventListener("mousedown",t),window.addEventListener("touchstart",t);const n=setInterval(async()=>{if(Date.now()-dt.current>=3e4&&!J&&!ie&&N==="Ready"&&!Ue.current)if(Ue.current=!0,Xe.current.length>0)je(!0),R(Xe.current);else try{const r=`${it}/nudgeUser`,u={navigationUrl:window.location.href,token:T},g=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)});if(g.ok){const x=(await g.json()).nudge_questions;x&&x.length>0&&(U("[NUDGE] Received nudge questions from API:",x),Xe.current=x,je(!0),R(x))}}catch(r){console.error("[NUDGE] Error calling /nudgeUser:",r),Ue.current=!1}},1e3);return()=>{window.removeEventListener("mousemove",t),window.removeEventListener("keydown",t),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t),clearInterval(n)}},[T,J,ie,N]),e.useEffect(()=>{if(fe.length===0)return;let t=0,n=0,i=!1,c=100;const r=()=>{const u=fe[t];if(i){const g=u.substring(0,n-1);ve(g),n--,c=50}else{const g=u.substring(0,n+1);ve(g),n++,c=100}!i&&n===u.length?(i=!0,c=3e3):i&&n===0&&(i=!1,t=(t+1)%fe.length,c=500),$.current=setTimeout(r,c)};return r(),()=>{$.current&&clearTimeout($.current)}},[fe]);const gt=()=>{try{return JSON.parse(sessionStorage.getItem("STREAMOJI_LEADS_SESSION_MESSAGES")||"[]")}catch{return[]}},Ct=t=>{sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify(t))},[Ze,et]=e.useState(!1),[bt,tt]=e.useState(0),ce=e.useRef(null),$e=e.useRef([]),_t=e.useRef(0),[wt,Ot]=e.useState(null),[Ft,nt]=e.useState(null),de=e.useRef(null),ke=e.useRef(null),qe=e.useCallback((t=!1)=>{t&&(Se.current=!0,m(!1),_("Ready")),v.current=[],M.current=[],L.current=!1,W(!1),ge.current=0,ae.current=0,be.current=0,me.current.forEach(n=>{try{n.stop()}catch{}}),Ee.current&&(clearTimeout(Ee.current),Ee.current=null),Y(null),Pe(""),me.current=[]},[]),Nt=async()=>{try{const t=await navigator.mediaDevices.getUserMedia({audio:!0}),n=window.AudioContext||window.webkitAudioContext,i=new n,c=i.createMediaStreamSource(t),r=i.createAnalyser();r.fftSize=64,c.connect(r),de.current=i,ke.current=c,nt(r);const u=new MediaRecorder(t);ce.current=u,$e.current=[],u.ondataavailable=g=>{g.data.size>0&&$e.current.push(g.data)},u.onstop=async()=>{const g=Date.now()-_t.current;if(nt(null),ke.current&&(ke.current.disconnect(),ke.current=null),de.current&&de.current.state!=="closed"&&(de.current.close(),de.current=null),g<1e3){_("Recording too short. Hold or click longer."),m(!1);return}const k=new Blob($e.current,{type:"audio/wav"});await Pt(k)},_t.current=Date.now(),u.start(100),et(!0),_("Listening...")}catch(t){console.error("Error accessing microphone:",t),_("Mic Access Error")}},Ut=()=>{ce.current&&ce.current.state!=="inactive"&&(ce.current.stop(),ce.current.stream.getTracks().forEach(t=>t.stop()),et(!1))},jt=()=>{ce.current&&ce.current.state!=="inactive"&&(ce.current.onstop=null,ce.current.stop(),ce.current.stream.getTracks().forEach(t=>t.stop()),nt(null),ke.current&&(ke.current.disconnect(),ke.current=null),de.current&&de.current.state!=="closed"&&(de.current.close(),de.current=null),et(!1),$e.current=[],_("Ready"))};e.useEffect(()=>{if(!J)return;const t=()=>{const n=be.current;if(n<=0)return;const i=S.current,c=ge.current;if(!i)return;const r=i.currentTime-c,u=Math.min(Math.max(0,r),n),g=se.trim().length;if(g<=0)return;const k=Math.min(Math.round(u/n*g),g);d(k)};return clearInterval(o.current??void 0),o.current=setInterval(t,90),()=>clearInterval(o.current??void 0)},[J,se,be.current]),e.useEffect(()=>{let t;return Ze?(tt(0),t=window.setInterval(()=>{tt(n=>n+1)},1e3)):tt(0),()=>clearInterval(t)},[Ze]);const Bt=t=>{const n=t.numberOfChannels,i=t.length*n*2+44,c=new ArrayBuffer(i),r=new DataView(c);let u=0;const g=O=>{r.setUint16(u,O,!0),u+=2},k=O=>{r.setUint32(u,O,!0),u+=4};k(1179011410),k(i-8),k(1163280727),k(544501094),k(16),g(1),g(n),k(t.sampleRate),k(t.sampleRate*2*n),g(n*2),g(16),k(1635017060),k(i-u-4);const x=[];for(let O=0;O<n;O++)x.push(t.getChannelData(O));let y=0;for(;u<i;){for(let O=0;O<n;O++){let P=Math.max(-1,Math.min(1,x[O][y]));P=P<0?P*32768:P*32767,r.setInt16(u,P,!0),u+=2}y++}return new Blob([c],{type:"audio/wav"})},rt=async(t,n,i=!1)=>{if(!Se.current){if(j.current){M.current.push({audio:t,visemes:n,isNewSegment:i});return}j.current=!0;try{const c=window.AudioContext||window.webkitAudioContext,r=S.current??new c;r.state==="suspended"&&await r.resume(),S.current=r;const u=window.atob(t),g=new Uint8Array(u.length);for(let F=0;F<u.length;F++)g[F]=u.charCodeAt(F);const k=await r.decodeAudioData(g.buffer.slice(0));be.current+=k.duration;const x=r.currentTime;let y=ae.current;const O=!L.current;y<x&&(y=x+.1),ae.current=y+k.duration;const P=r.createBufferSource();P.buffer=k;let H=wt;if((!H||H.context!==r)&&(H=r.createAnalyser(),H.fftSize=64,H.connect(r.destination),Ot(H)),P.connect(H),me.current.push(P),Se.current){me.current=me.current.filter(F=>F!==P);return}if(O){L.current=!0,W(!0),U(`[AUDIO] setIsSpeaking(true) - First chunk starting at ${y.toFixed(3)}`),ge.current=y;const F=(y-x)*1e3;pe.current=performance.now()+F,U(`[AUDIO] Response started. Initial startTime: ${y.toFixed(3)}, CT: ${x.toFixed(3)}`)}P.start(y);const Q=(y-ge.current)*1e3;i&&(Oe.current=Q,U(`[AUDIO] New segment detected at +${Q.toFixed(0)}ms. Resetting segment offset.`)),n.forEach((F,ne)=>{const D=F.symbol??"";if(D){const re=cn(D),ue=Math.round(F.start*1e3),Le=Math.round((F.duration??0)*1e3),xt=Oe.current+ue;ne<3&&U(`[AUDIO] Viseme "${D}": segment_relative=${ue}ms, segment_offset=${Oe.current.toFixed(0)}ms => vtime=${xt}ms`),re.forEach(yt=>{v.current.push({viseme:yt.v,weight:yt.w,vtime:xt,vduration:Le})})}}),_("Speaking...")}finally{if(j.current=!1,M.current.length>0){const c=M.current.shift();c&&rt(c.audio,c.visemes,c.isNewSegment)}}}},Pt=async t=>{try{m(!0),je(!1),Me.current="",E(""),_("Processing Voice...");const n=await t.arrayBuffer(),c=await new(window.AudioContext||window.webkitAudioContext)().decodeAudioData(n),r=Bt(c),u=new FileReader;u.readAsDataURL(r),u.onloadend=async()=>{const g=u.result.split(",")[1];qe(),Z(""),le.current="",Ie.current=!1;const k=`${it}/stt?token=${encodeURIComponent(T)}`,x=await fetch(k,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({audio_base64:g,audio_format:"wav"})});if(x.status===429){try{const ne=await x.text(),D=JSON.parse(ne);U("[STT] 429 agent at capacity:",D?.detail)}catch{}Z(""),_("Busy"),m(!1);return}if(!x.ok){const ne=await x.text();let D="STT Failed";try{D=JSON.parse(ne).error||D}catch{ne&&(D=ne.slice(0,200))}throw new Error(D)}const y=x.body;if(U("this is body"+y),!y){_("STT Failed"),m(!1);return}const O=y.getReader();Se.current=!1;const P=new TextDecoder;let H="",Q=!1;const F=async(ne,D)=>{switch(ne){case"transcript":D.transcript!=null&&Z(String(D.transcript));break;case"text":{const re=D.delta??D.text??"";re&&mt(re);break}case"audio":{const re=D.chunk,ue=D.visemes??[],Le=!!D.is_new_segment;re&&await rt(re,ue,Le);break}case"done":{Q=!0,_("Ready"),m(!1);break}case"error":{Q=!0,_("STT Failed"),m(!1);break}default:break}};for(;;){const{done:ne,value:D}=await O.read();D&&(H+=P.decode(D,{stream:!0}));const re=H.split(`
|
|
4
4
|
|
|
5
|
-
`);
|
|
5
|
+
`);H=re.pop()??"";for(const ue of re){const Le=Ge(ue);Le&&await F(Le.event,Le.data)}if(ne){if(H.trim()){const ue=Ge(H.trim());ue&&await F(ue.event,ue.data)}Q||(_("Ready"),m(!1));break}}}}catch(n){console.error("Audio Submission Error:",n),_("STT Failed"),m(!1)}},Wt=async t=>{t&&t.preventDefault(),je(!1),Me.current="",E(""),!(!A||ie)&&await Vt(A)},Ge=t=>{const n=t.split(/\r?\n/);let i="",c="";for(const u of n)u.startsWith("event:")?i=u.slice(6).trim():u.startsWith("data:")&&(c=u.slice(5).trim());if(!i)return null;let r={};if(c)try{r=JSON.parse(c)}catch{r={raw:c}}return{event:i,data:r}},vt=(t,n)=>{switch(t){case"connected":le.current="",Ie.current=!1;break;case"text":{const i=n.delta??"";i&&mt(i);break}case"audio":{const i=n.chunk,c=n.visemes??[];i&&rt(i,c);break}case"done":{const i=gt(),c=Me.current.trim(),r=[...i,{role:"user",content:A||"..."},{role:"assistant",content:c}];Ct(r),Te.current=[...v.current],_("Ready"),m(!1),Z("");break}case"error":{const i=n.message??"Unknown error";Me.current=i,E(i),_("Agent Failed"),m(!1);break}}},Vt=async t=>{m(!0),_("Thinking..."),Me.current="",le.current="",Ie.current=!1,qe(),be.current=0,d(0);const n=`${it}/agent/chat?token=${encodeURIComponent(T)}`;try{const i=gt();let c=sessionStorage.getItem("STREAMOJI_LEADS_SESSION_LEAD_ID");c||(Jt("[CHAT] Session UID missing at send time! Generating emergency backup."),c="emergency-"+Math.random().toString(36).substring(7),sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",c));const r={history:i,question:t,lead_id:c};U("[CHAT] Sending payload:",r);const u=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r),cache:"default"});if(u.status===429){try{const O=await u.json();U("[CHAT] 429 agent at capacity:",O?.detail)}catch{}Z(""),_("Busy"),m(!1);return}if(!u.ok)throw new Error("Agent request failed");const g=u.body;if(!g){_("Agent Failed"),m(!1);return}const k=g.getReader();Se.current=!1;const x=new TextDecoder;let y="";for(;;){const{done:O,value:P}=await k.read();U(`[SSE] Chunk received. done=${O}, length=${P?.length||0}`),P&&(y+=x.decode(P,{stream:!0}));const H=y.split(`
|
|
6
6
|
|
|
7
|
-
`);y=
|
|
7
|
+
`);y=H.pop()??"";for(const Q of H){U(`[SSE] Processing block: ${Q.slice(0,50)}...`);const F=Ge(Q);F&&(U(`[SSE] Event: ${F.event}`),vt(F.event,F.data))}if(O){if(U("[SSE] Stream finished"),y.trim()){const Q=Ge(y.trim());Q&&vt(Q.event,Q.data)}_("Ready"),m(!1),Z("");break}}}catch(i){console.error("Chat Error:",i),_("Agent Failed"),m(!1)}},St=se.trim(),st=St&&J?St.slice(0,f!=null&&f>0?f:0):"";e.useEffect(()=>{const t=Qe.current;t!=="exiting"&&(st?(At(st),t==="hidden"&&Ve("entering")):(t==="visible"||t==="entering")&&Ve("exiting"))},[st,oe]);const Ht=e.useCallback(()=>{const t=Qe.current;t==="entering"?Ve("visible"):t==="exiting"&&Ve("hidden")},[]);return e.useLayoutEffect(()=>{const t=ut.current;t&&(t.scrollTop=t.scrollHeight)},[lt]),a.jsxs("div",{className:"avatar-widget-container",children:[a.jsxs("div",{className:"avatar-input-area",children:[ee!=="hidden"?a.jsx("div",{className:`avatar-thinking-tab${ee==="exiting"?" avatar-thinking-tab--exiting":ee==="entering"?" avatar-thinking-tab--entering":""}`,onAnimationEnd:Et,children:Re===Lt?a.jsx("a",{href:"https://leads.streamoji.com",target:"_blank",rel:"noopener noreferrer",children:"Made by Streamoji Leads"}):Re}):null,a.jsx("div",{className:"avatar-input-container",children:a.jsx("div",{style:{display:"flex",alignItems:"center",width:"100%",height:"100%"},children:Ze?a.jsxs("div",{className:"avatar-input-recording",children:[a.jsx("button",{type:"button",className:"avatar-recording-cancel",onClick:jt,title:"Cancel",children:a.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:[a.jsx("line",{x1:"18",y1:"6",x2:"6",y2:"18"}),a.jsx("line",{x1:"6",y1:"6",x2:"18",y2:"18"})]})}),a.jsxs("div",{style:{flex:1,height:"100%",position:"relative",display:"flex",alignItems:"center",minWidth:0},children:[a.jsx("div",{style:{flex:1,height:"100%"},children:a.jsx(Rt,{analyser:Ft})}),a.jsxs("span",{style:{fontSize:"0.75rem",color:"#64748b",fontWeight:500,marginLeft:"4px",minWidth:"32px",textAlign:"right",fontVariantNumeric:"tabular-nums"},children:[Math.floor(bt/60),":",String(bt%60).padStart(2,"0")]})]}),a.jsx("button",{type:"button",className:"avatar-recording-confirm",onClick:Ut,title:"Send",children:a.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:a.jsx("polyline",{points:"20 6 9 17 4 12"})})})]}):J&&!Ke?a.jsxs("div",{className:"avatar-input-speaking",children:[a.jsx("div",{style:{flex:1,height:"100%",display:"flex",alignItems:"center",paddingRight:"8px"},children:a.jsx(Rt,{analyser:wt})}),a.jsx("button",{type:"button",className:"avatar-speaking-stop",onClick:()=>qe(!0),title:"Stop",children:a.jsx("span",{className:"avatar-speaking-stop__icon","aria-hidden":!0})})]}):ie?a.jsx("div",{style:{flex:1,height:"100%",display:"flex",alignItems:"center",justifyContent:"center"},children:a.jsx("div",{className:"avatar-input-loader"})}):a.jsxs("form",{onSubmit:Wt,style:{flex:1,display:"flex",height:"100%",alignItems:"center"},children:[a.jsx("input",{id:"avatar-text-input",type:"text",value:A,onChange:t=>Z(t.target.value),placeholder:N==="Busy"?"Assisting another user":Ce||"Ask me anything",disabled:ie||N==="Busy",autoComplete:"off",style:{width:"100%",height:"100%"}}),N==="Busy"?a.jsx("button",{type:"button",className:"mic-button",disabled:!0,style:{backgroundColor:"#1e4a5e"},title:"Agent at capacity",children:a.jsx("svg",{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",children:a.jsx("path",{d:"M4 2L20 2L12 10L4 2z M12 14L4 22L20 22L12 14z",fill:"white"})})}):A.trim()===""?a.jsx("button",{type:"button",className:"mic-button",onClick:Nt,disabled:ie,style:{backgroundColor:"#1e4a5e"},children:a.jsxs("svg",{width:"28",height:"28",viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:[a.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"}),a.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"})]})}):a.jsx("button",{type:"submit",className:"mic-button",disabled:ie,style:{backgroundColor:"#1e4a5e"},title:"Send",children:a.jsx("svg",{width:24,height:24,viewBox:"0 0 24 24",fill:"none","aria-hidden":"true",children:a.jsx("path",{d:"M19 12H5M19 12L14 17M19 12L14 7",stroke:"white",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})]})})})]}),a.jsx("div",{className:"avatar-wrapper",children:a.jsxs("div",{className:"avatar-scene-wrapper",children:[oe!=="hidden"&&a.jsx("div",{className:`avatar-bubble${oe==="entering"?" avatar-bubble--entering":oe==="exiting"?" avatar-bubble--exiting":""}`,onAnimationEnd:Ht,children:a.jsx("div",{ref:ut,className:`avatar-bubble__content${Ke?" avatar-bubble__content--nudge":""}`,children:lt})}),a.jsx("div",{className:"avatar-canvas-layer",style:{width:C,height:C},children:a.jsxs(ot.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:[a.jsx(un,{target:nn}),a.jsx("ambientLight",{intensity:.7}),a.jsx("directionalLight",{position:[0,2,2],intensity:1}),a.jsx(ze.Environment,{preset:"city"}),a.jsx(e.Suspense,{fallback:null,children:B!==null&&a.jsx(ln,{avatarUrl:B,isPlayingRef:L,visemeQueueRef:v,audioContextRef:S,responseAudioStartTimeRef:ge,adjustments:tn,mood:s,expression:q,agentResponse:se,isSpeaking:J,nextStartTimeRef:ae,stopPlayback:qe,setIsSpeaking:W,expressionUrl:Ye,onExpressionFinished:Dt,isNudgeResponse:Ke,avatarGender:p})})]})})]})})]})},hn=({token:l,onNavigationRequested:w})=>a.jsx(fn,{token:l,onNavigationRequested:w});exports.AvatarWidget=hn;
|
|
8
8
|
//# sourceMappingURL=avatar-widget.cjs.map
|