@streamoji/avatar-widget 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/avatar-widget.cjs +5 -5
- package/dist/avatar-widget.cjs.map +1 -1
- package/dist/avatar-widget.css +1 -1
- package/dist/avatar-widget.js +635 -621
- package/dist/avatar-widget.js.map +1 -1
- package/dist/avatar-widget.umd.js +41 -41
- 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"),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=
|
|
3
|
-
`),i=
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react/jsx-runtime"),Ge=require("@react-three/drei"),ct=require("@react-three/fiber"),e=require("react"),qt=require("three"),Jt=require("three/examples/jsm/loaders/GLTFLoader.js");function Gt(l){const b=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(l){for(const g in l)if(g!=="default"){const p=Object.getOwnPropertyDescriptor(l,g);Object.defineProperty(b,g,p.get?p:{enumerable:!0,get:()=>l[g]})}}return b.default=l,Object.freeze(b)}const Ue=Gt(qt),at="https://ai.streamoji.com",B=(...l)=>{},zt=(...l)=>{},kt=({analyser:l})=>{const b=e.useRef(null),g=e.useRef(null);return e.useEffect(()=>{const p=b.current;if(!p)return;const _=p.getContext("2d",{alpha:!0});if(!_)return;let C,W=null;l&&(l.fftSize=128,W=new Uint8Array(l.frequencyBinCount));const le=()=>{C=requestAnimationFrame(le),(p.width!==p.offsetWidth||p.height!==p.offsetHeight)&&(p.width=p.offsetWidth,p.height=p.offsetHeight);const Y=p.width,O=p.height;if(Y===0||O===0)return;const de=O/2;_.clearRect(0,0,Y,O),_.fillStyle="#1e293b";const T=2,R=T+2,A=Y*.95,F=Math.floor(A/R);(!g.current||g.current.length!==F)&&(g.current=new Float32Array(F).fill(2));const me=F*R,v=(Y-me)/2;l&&W&&l.getByteFrequencyData(W);const _e=W?W.length:0,q=Math.floor(_e*.7)/F,ie=new Float32Array(F);for(let I=0;I<F;I++){let j=0;if(W&&q>0){const ee=Math.floor(I*q),U=Math.floor((I+1)*q);for(let y=ee;y<U;y++){const x=W[y]||0;x>j&&(j=x)}}j<10&&(j=0);const V=j/255,D=j>0?Math.max(2,Math.pow(V,1.4)*O*.25):2;ie[I]=D}for(let I=0;I<F;I++){const j=F-1-I,V=Math.max(ie[I],ie[j]),D=g.current[I]+(V-g.current[I])*.3;g.current[I]=D;const ee=v+I*R,U=de-D/2;_.beginPath(),_.roundRect?_.roundRect(ee,U,T,D,4):_.fillRect(ee,U,T,D),_.fill()}};return le(),()=>{cancelAnimationFrame(C)}},[l]),a.jsx("canvas",{ref:b,style:{width:"100%",height:"100%",display:"block"}})},Yt="https://pub-48df6f7d60d6440bbd01676ea5d90e55.r2.dev",Et="https://pub-be53cae7bd99457a8c1f11b4d38f1672.r2.dev/default-models/fullbodyavatarmale.glb";async function Qt(l){const b=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(l));return Array.from(new Uint8Array(b)).map(g=>g.toString(16).padStart(2,"0")).join("")}function Xt(l){const[b,g]=e.useState(null);return e.useEffect(()=>{if(!l){g(null);return}let p=!1;return Qt(l).then(_=>{if(p)return;const C=`${Yt}/${_}.glb`;fetch(C,{method:"HEAD"}).then(W=>{p||g(W.ok?C:Et)}).catch(()=>{p||g(Et)})}),()=>{p=!0}},[l]),b}const Kt=["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"],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"],nn={zoom:.85,position:[.15,-.8,0],scale:1.5,rotation:[.15,.02,0]},rn=[-.45,1.9,.1],sn={browInnerUp:.2},ot=.18,an=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}},on=[{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:{}}],cn={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 un(l){if(!l)return[{v:"sil",w:1}];const b=l.toLowerCase();return cn[b]??[{v:"sil",w:1}]}function ln({target:l}){const{camera:b}=ct.useThree();return e.useEffect(()=>{b.lookAt(...l)},[b,l]),null}function z(l,b,g){if(!l||!l.morphTargetDictionary)return;const p=l,_=p.morphTargetDictionary,C=p.morphTargetInfluences;if(C)for(const W in _)W.toLowerCase()===b.toLowerCase()&&(C[_[W]]=g)}function At(l,b=.97){if(!l)return;const g=l;if(g.morphTargetInfluences)for(let p=0;p<g.morphTargetInfluences.length;p++)g.morphTargetInfluences[p]*=b}const dn=e.memo(({avatarUrl:l,isPlayingRef:b,visemeQueueRef:g,audioContextRef:p,responseAudioStartTimeRef:_,adjustments:C,mood:W,expression:le,agentResponse:Y,isSpeaking:O,nextStartTimeRef:de,stopPlayback:T,setIsSpeaking:S,expressionUrl:R,onExpressionFinished:A,isNudgeResponse:F,avatarGender:me})=>{const{scene:v}=Ge.useGLTF(l),_e=me==="female"?Zt:Kt,Ce=Ge.useGLTF(_e),q=e.useMemo(()=>Ce.flatMap(d=>d.animations),[Ce]),ie=Ge.useGLTF(tn),I=e.useMemo(()=>ie.flatMap(d=>d.animations),[ie]),j=e.useRef(null),V=e.useRef(null),D=e.useRef(null),ee=e.useRef([]),[U]=e.useState(()=>new Ue.AnimationMixer(v)),y=e.useRef({}),x=e.useRef(null),we=e.useRef(0),ae=e.useRef(!1),Pe=e.useRef(0),te=e.useRef(null);e.useEffect(()=>{if(!(!q||!v)){if(q.forEach((d,f)=>{const o=`idle_${f}`;if(!y.current[o]){const s=U.clipAction(d,v);s.name=o,s.setLoop(Ue.LoopOnce,1),s.clampWhenFinished=!0,y.current[o]=s}}),I.forEach((d,f)=>{const o=`talk_${f}`;if(!y.current[o]){const s=U.clipAction(d,v);s.name=o,s.setLoop(Ue.LoopOnce,1),s.clampWhenFinished=!0,y.current[o]=s}}),q.length>0){const d=y.current.idle_0,f=x.current&&U.existingAction(x.current.getClip());d&&!f&&(d.reset().fadeIn(.5).play(),x.current=d)}return()=>{U.stopAllAction(),y.current={},x.current=null}}},[q,I,v,U]);const Se=e.useRef("");e.useEffect(()=>{if(!R||!v||R===Se.current)return;Se.current=R,te.current=R,new Jt.GLTFLoader().load(R,f=>{if(f.animations&&f.animations.length>0){const o=f.animations[0],s=U.clipAction(o,v);if(s.name=`EXPR_${R}`,s.setLoop(Ue.LoopOnce,1),s.clampWhenFinished=!0,y.current[`EXPR_${R}`]=s,O&&te.current===R){const m=x.current;s.reset().fadeIn(.3).play(),m&&m!==s&&m.crossFadeTo(s,.3,!0),x.current=s,te.current=null}}},void 0,f=>{console.error(`[ANIMATION] Failed to load ${R}`,f)})},[R,v,U,O]),e.useEffect(()=>{const d=f=>{const o=f.action,s=o.name||"";if(s.startsWith("idle_")){const J=(parseInt(s.split("_")[1])+1)%q.length,Q=y.current[`idle_${J}`];Q&&(Q.reset().fadeIn(.5).play(),o.crossFadeTo(Q,.5,!0),x.current=Q)}else if(s.startsWith("EXPR_")){if(O){const m=y.current.talk_0;m&&(m.reset().fadeIn(.5).play(),o.crossFadeTo(m,.5,!0),x.current=m)}else{const m=y.current.idle_0;m&&(m.reset().fadeIn(.5).play(),o.crossFadeTo(m,.5,!0),x.current=m)}A&&A()}else if(s.startsWith("talk_"))if(O&&!F){const J=(parseInt(s.split("_")[1])+1)%I.length,Q=y.current[`talk_${J}`];Q&&(Q.reset().fadeIn(.3).play(),o.crossFadeTo(Q,.3,!0),x.current=Q)}else{const m=y.current.idle_0;m&&(m.reset().fadeIn(.5).play(),o.crossFadeTo(m,.5,!0),x.current=m)}};return U.addEventListener("finished",d),()=>U.removeEventListener("finished",d)},[U,q,I,O,F,A]),e.useEffect(()=>{if(O&&v){const d=x.current,f=d?.name||"";if(f.startsWith("idle_")||f.startsWith("talk_")||f===""){const o=te.current;if(o){const s=y.current[`EXPR_${o}`];if(s){s.reset().fadeIn(.3).play(),d&&d!==s&&d.crossFadeTo(s,.3,!0),x.current=s,te.current=null;return}}if(f.startsWith("idle_")||f===""){const s=y.current.talk_0;s&&(s.reset().fadeIn(.5).play(),d&&d.crossFadeTo(s,.5,!0),x.current=s)}}}else if(!O&&v){const d=x.current,f=d?.name||"";if(f.startsWith("talk_")||f.startsWith("EXPR_")){const o=y.current.idle_0;o&&(o.reset().fadeIn(.5).play(),d&&d.crossFadeTo(o,.5,!0),x.current=o)}}},[O,v,R]),e.useEffect(()=>{if(!v)return;v.traverse(o=>{if(o.isMesh&&o.morphTargetDictionary){const s=o.name.toLowerCase();(s.includes("head")||s.includes("avatar"))&&(V.current=o,B(`[ANIMATION] Found head mesh: ${o.name}`)),s.includes("teeth")&&(D.current=o,B(`[ANIMATION] Found teeth mesh: ${o.name}`))}});const d=V.current?.morphTargetDictionary;d&&Object.keys(d).filter(o=>o.toLowerCase().includes("brow"));const f=[];v.traverse(o=>{if(o.isMesh){const m=o.morphTargetDictionary;m&&Object.keys(m).some(J=>J.toLowerCase().includes("brow"))&&f.push(o)}}),ee.current=f,f.length>0&&B("[ANIMATION] Meshes with brow morphs:",f.length)},[v]);const ve=(d,f=1)=>{const o=`viseme_${d}`.toLowerCase(),s=on.find(m=>m.key.toLowerCase()===o);if(s)for(const m in s.mix){const J=s.mix[m]*f;z(V.current,m,J),z(D.current,m,J)}},Oe=d=>{const f=Je[d]??Je.neutral;for(const o in f)z(V.current,o,f[o]),z(D.current,o,f[o])};return ct.useFrame((d,f)=>{const o=Math.pow(.88,60*f);At(V.current,o),At(D.current,o),Oe(W);const s=d.clock.elapsedTime;let m=0;if(Math.floor(s)%5===0&&Math.floor((s-f)%5)!==0){let H=null;v.traverse(Z=>{Z.name.toLowerCase().includes("hips")&&(H=Z)});const ne=H?`Hips Y: ${H.position.y.toFixed(4)}`:"Hips not found";B(`[ANIMATION] Mixer Time: ${U.time.toFixed(2)}, ${ne}`)}if(U.update(f),s>we.current&&!ae.current&&(ae.current=!0,Pe.current=s),ae.current){const H=O?.2:.3,ne=(s-Pe.current)/H;if(ne>=1){ae.current=!1;const Z=O?1:2.5;we.current=s+Z}else{const Z=ne<.5?ne*2:(1-ne)*2;z(V.current,"eyeBlinkLeft",Z),z(V.current,"eyeBlinkRight",Z),z(D.current,"eyeBlinkLeft",Z),z(D.current,"eyeBlinkRight",Z),m=Z*sn.browInnerUp}}const J=Je[W]??Je.neutral,Q=J.browInnerUp??0,ze=J.browOuterUpLeft??0,Ye=J.browOuterUpRight??0,Ae=s*an,Qe=ot*Math.sin(Ae),ye=ot*.7*Math.sin(Ae+.7),K=ot*.7*Math.sin(Ae+1.3),xe=H=>Math.max(0,Math.min(1,H)),pe=xe(Q+Qe),ge=xe(ze+ye),Re=xe(Ye+K),De=xe(pe+m);if(z(V.current,"browInnerUp",De),z(D.current,"browInnerUp",De),z(V.current,"browOuterUpLeft",ge),z(D.current,"browOuterUpLeft",ge),z(V.current,"browOuterUpRight",Re),z(D.current,"browOuterUpRight",Re),j.current){const H=b.current?0:C.rotation[1];j.current.rotation.y=Ue.MathUtils.lerp(j.current.rotation.y,H,.1),j.current.position.set(...C.position),j.current.scale.setScalar(C.scale),j.current.rotation.x=C.rotation[0],j.current.rotation.z=C.rotation[2]}if(b.current&&p.current){const H=p.current.currentTime,Be=(H-_.current)*1e3- -150;for(let ke=0;ke<g.current.length;ke++){const G=g.current[ke];Be>=G.vtime&&Be<G.vtime+G.vduration&&ve(G.viseme,G.weight??1)}H>de.current+.5&&(T(),S(!1))}}),a.jsx("group",{ref:j,children:a.jsx("primitive",{object:v})})});function fn(l){return l?l.charAt(0).toUpperCase()+l.slice(1).toLowerCase():""}const hn=({token:l,agentToken:b,onNavigationRequested:g,avatarGender:p,presetUserDetails:_}={})=>{const C=l??b??"",W=Xt(C||void 0),[le,Y]=e.useState(""),[O,de]=e.useState(""),[T,S]=e.useState("Ready"),[R,A]=e.useState(!1),[F,me]=e.useState(!1),[v,_e]=e.useState([]),[Ce,q]=e.useState(""),ie=e.useRef(null),[I,j]=e.useState(()=>typeof window<"u"?window.matchMedia("(max-width: 480px)").matches:!1);e.useEffect(()=>{const t=window.matchMedia("(max-width: 480px)"),n=()=>j(t.matches);return t.addEventListener("change",n),()=>t.removeEventListener("change",n)},[]);const V=I?80:600,D=e.useRef(!1),ee=e.useRef([]),U=e.useRef(0),y=e.useRef(!1),x=e.useRef([]),we=e.useRef(null),ae=e.useRef([]);e.useRef([]);const Pe=e.useRef([]),te=e.useRef(0),Se=e.useRef(0),ve=e.useRef(0),Oe=e.useRef(0),d=e.useRef(!1),[f,o]=e.useState(null),s=e.useRef(null),[m,J]=e.useState("neutral"),[Q,ze]=e.useState(""),[Ye,Ae]=e.useState(""),[Qe,ye]=e.useState("Chat with us"),[K,xe]=e.useState(null),[pe,ge]=e.useState("hidden"),[Re,De]=e.useState(""),H=e.useRef(null),ne=e.useRef(pe);ne.current=pe;const[Z,Be]=e.useState(null),ke=e.useRef(null),G=e.useRef(null),[Ne,ut]=e.useState(!1),lt=e.useRef(null),[oe,We]=e.useState("hidden"),[dt,Tt]=e.useState(""),Xe=e.useRef(oe);Xe.current=oe;const fe=e.useRef(""),Te=e.useRef(!1),Ie=e.useRef(""),ft=e.useRef(Date.now()),Ke=e.useRef([]),Fe=e.useRef(!1),[Ze,je]=e.useState(!1),[ht,mt]=e.useState(!1),be=e.useRef(null),Me=e.useMemo(()=>Ne?"Try again":T==="Busy"?"Busy":T==="Thinking..."||T==="Processing Voice..."?T:K!=null&&K!==""&&K!=="none"&&K!=="<none>"?`Enter ${fn(K)}`:oe!=="hidden"||!ht?null:Lt,[T,K,Ne,oe,ht]),pt=!Ne&&T!=="Busy"&&T!=="Thinking..."&&T!=="Processing Voice..."&&(K==null||K===""||K==="none"||K==="<none>");e.useEffect(()=>{if(oe!=="hidden"||!pt){mt(!1),be.current!=null&&(clearTimeout(be.current),be.current=null);return}return be.current=setTimeout(()=>{be.current=null,mt(!0)},400),()=>{be.current!=null&&(clearTimeout(be.current),be.current=null)}},[oe,pt]);const Ve=Me!=null&&Me!=="";e.useEffect(()=>{const t=ne.current;if(!(t==="exiting"||t==="entering")){if(t==="hidden"){Ve&&(De(Me??""),ge("entering"));return}t==="visible"&&(!Ve||Me!==Re)&&(H.current=Ve?Me:null,ge("exiting"))}},[Ve,Me,pe,Re]),e.useEffect(()=>{if(T!=="Busy"){G.current!=null&&(clearTimeout(G.current),G.current=null);return}return G.current=setTimeout(()=>{G.current=null,S("Ready"),ut(!0)},12e3),()=>{G.current!=null&&(clearTimeout(G.current),G.current=null)}},[T]),e.useEffect(()=>{if(!Ne)return;const t=setTimeout(()=>ut(!1),2500);return()=>clearTimeout(t)},[Ne]);const It=e.useCallback(()=>{const t=ne.current;if(t==="exiting"){ge("hidden");const n=H.current;H.current=null,n!=null&&n!==""&&(De(n),ge("entering"))}else t==="entering"&&ge("visible")},[]),Mt=t=>{if(!t)return;if(t.mood!=null){const r=String(t.mood).toLowerCase();J(r)}if(t.expression!=null){const r=String(t.expression).trim();ze(r);const u=en.find(h=>h.name.toLowerCase()===r.toLowerCase());B(`[STREAM] Animation match for "${r}": ${u?u.name:"NONE"}`),Ae(u?.url??"")}if(t.navigation!=null){const r=String(t.navigation).trim();r!==""&&(g?g(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;if(_&&(r==="email"&&_.email||r==="name"&&_.name||r==="phone"&&_.phone))return;xe(r||null),ye(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")&&(xe(null),Qe!=="Chat with us"&&ye("Chat with us"));t.collected,t.valid},Ct=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},Ot=e.useCallback(()=>{},[]),gt=t=>{if(Te.current)t==="§"?Te.current=!1:(Ie.current+=t,de(n=>n+t));else if(t==="§"){Te.current=!0;return}else for(fe.current+=t;fe.current.includes(`
|
|
2
|
+
`);){const n=fe.current.indexOf(`
|
|
3
|
+
`),i=fe.current.slice(0,n).trim();fe.current=fe.current.slice(n+1);const c=Ct(i);c&&Mt(c)}};e.useEffect(()=>{(async()=>{if(!sessionStorage.getItem("STREAMOJI_LEADS_SESSION_LEAD_ID")){const r="secret",u=Math.floor(Date.now()/1e3).toString();try{const h=new TextEncoder,w=await crypto.subtle.importKey("raw",h.encode(r),{name:"HMAC",hash:"SHA-256"},!1,["sign"]),k=await crypto.subtle.sign("HMAC",w,h.encode(u)),L=Array.from(new Uint8Array(k)).map(P=>P.toString(16).padStart(2,"0")).join("");sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",L),B("[SESSION] New HMAC UID generated and saved:",L)}catch(h){console.error("[SESSION] HMAC generation failed:",h);const w=Math.random().toString(36)+Date.now().toString();sessionStorage.setItem("STREAMOJI_LEADS_SESSION_LEAD_ID",w)}}const i=sessionStorage.getItem("STREAMOJI_LEADS_SESSION_MESSAGES");let c=[];if(i)try{c=JSON.parse(i)}catch{c=[]}c.length===0&&_&&(_.name||_.email)?(c=[{role:"user",content:`PRESET_USER_DETAILS: Name is "${_.name||""}" , Email is "${_.email||""}"`}],sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify(c))):i||sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify([]))})()},[_]),e.useEffect(()=>{const t=()=>{ft.current=Date.now(),Fe.current&&(Fe.current=!1,je(!1),_e([]),q(""),de(""),me(!1))};window.addEventListener("mousemove",t),window.addEventListener("keydown",t),window.addEventListener("mousedown",t),window.addEventListener("touchstart",t);const n=setInterval(async()=>{if(Date.now()-ft.current>=3e4&&!F&&!R&&T==="Ready"&&!Fe.current)if(Fe.current=!0,Ke.current.length>0)je(!0),_e(Ke.current);else try{const r=`${at}/nudgeUser`,u={navigationUrl:window.location.href,token:C},h=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)});if(h.ok){const k=(await h.json()).nudge_questions;k&&k.length>0&&(B("[NUDGE] Received nudge questions from API:",k),Ke.current=k,je(!0),_e(k))}}catch(r){console.error("[NUDGE] Error calling /nudgeUser:",r),Fe.current=!1}},1e3);return()=>{window.removeEventListener("mousemove",t),window.removeEventListener("keydown",t),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t),clearInterval(n)}},[C,F,R,T]),e.useEffect(()=>{if(v.length===0)return;let t=0,n=0,i=!1,c=100;const r=()=>{const u=v[t];if(i){const h=u.substring(0,n-1);q(h),n--,c=50}else{const h=u.substring(0,n+1);q(h),n++,c=100}!i&&n===u.length?(i=!0,c=3e3):i&&n===0&&(i=!1,t=(t+1)%v.length,c=500),ie.current=setTimeout(r,c)};return r(),()=>{ie.current&&clearTimeout(ie.current)}},[v]);const bt=()=>{try{return JSON.parse(sessionStorage.getItem("STREAMOJI_LEADS_SESSION_MESSAGES")||"[]")}catch{return[]}},Dt=t=>{sessionStorage.setItem("STREAMOJI_LEADS_SESSION_MESSAGES",JSON.stringify(t))},[et,tt]=e.useState(!1),[_t,nt]=e.useState(0),ce=e.useRef(null),He=e.useRef([]),wt=e.useRef(0),[St,Nt]=e.useState(null),[Ft,rt]=e.useState(null),he=e.useRef(null),Ee=e.useRef(null),$e=e.useCallback((t=!1)=>{t&&(d.current=!0,A(!1),S("Ready")),x.current=[],ee.current=[],y.current=!1,me(!1),te.current=0,Se.current=0,ve.current=0,ae.current.forEach(n=>{try{n.stop()}catch{}}),ke.current&&(clearTimeout(ke.current),ke.current=null),Be(null),Ae(""),ae.current=[]},[]),jt=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),he.current=i,Ee.current=c,rt(r);const u=new MediaRecorder(t);ce.current=u,He.current=[],u.ondataavailable=h=>{h.data.size>0&&He.current.push(h.data)},u.onstop=async()=>{const h=Date.now()-wt.current;if(rt(null),Ee.current&&(Ee.current.disconnect(),Ee.current=null),he.current&&he.current.state!=="closed"&&(he.current.close(),he.current=null),h<1e3){S("Recording too short. Hold or click longer."),A(!1);return}const w=new Blob(He.current,{type:"audio/wav"});await Wt(w)},wt.current=Date.now(),u.start(100),tt(!0),S("Listening...")}catch(t){console.error("Error accessing microphone:",t),S("Mic Access Error")}},Ut=()=>{ce.current&&ce.current.state!=="inactive"&&(ce.current.stop(),ce.current.stream.getTracks().forEach(t=>t.stop()),tt(!1))},Pt=()=>{ce.current&&ce.current.state!=="inactive"&&(ce.current.onstop=null,ce.current.stop(),ce.current.stream.getTracks().forEach(t=>t.stop()),rt(null),Ee.current&&(Ee.current.disconnect(),Ee.current=null),he.current&&he.current.state!=="closed"&&(he.current.close(),he.current=null),tt(!1),He.current=[],S("Ready"))};e.useEffect(()=>{if(!F)return;const t=()=>{const n=ve.current;if(n<=0)return;const i=we.current,c=te.current;if(!i)return;const r=i.currentTime-c,u=Math.min(Math.max(0,r),n),h=O.trim().length;if(h<=0)return;const w=Math.min(Math.round(u/n*h),h);o(w)};return clearInterval(s.current??void 0),s.current=setInterval(t,90),()=>clearInterval(s.current??void 0)},[F,O,ve.current]),e.useEffect(()=>{let t;return et?(nt(0),t=window.setInterval(()=>{nt(n=>n+1)},1e3)):nt(0),()=>clearInterval(t)},[et]);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 h=L=>{r.setUint16(u,L,!0),u+=2},w=L=>{r.setUint32(u,L,!0),u+=4};w(1179011410),w(i-8),w(1163280727),w(544501094),w(16),h(1),h(n),w(t.sampleRate),w(t.sampleRate*2*n),h(n*2),h(16),w(1635017060),w(i-u-4);const k=[];for(let L=0;L<n;L++)k.push(t.getChannelData(L));let E=0;for(;u<i;){for(let L=0;L<n;L++){let P=Math.max(-1,Math.min(1,k[L][E]));P=P<0?P*32768:P*32767,r.setInt16(u,P,!0),u+=2}E++}return new Blob([c],{type:"audio/wav"})},st=async(t,n,i=!1)=>{if(!d.current){if(D.current){ee.current.push({audio:t,visemes:n,isNewSegment:i});return}D.current=!0;try{const c=window.AudioContext||window.webkitAudioContext,r=we.current??new c;r.state==="suspended"&&await r.resume(),we.current=r;const u=window.atob(t),h=new Uint8Array(u.length);for(let N=0;N<u.length;N++)h[N]=u.charCodeAt(N);const w=await r.decodeAudioData(h.buffer.slice(0));ve.current+=w.duration;const k=r.currentTime;let E=Se.current;const L=!y.current;E<k&&(E=k+.1),Se.current=E+w.duration;const P=r.createBufferSource();P.buffer=w;let $=St;if((!$||$.context!==r)&&($=r.createAnalyser(),$.fftSize=64,$.connect(r.destination),Nt($)),P.connect($),ae.current.push(P),d.current){ae.current=ae.current.filter(N=>N!==P);return}if(L){y.current=!0,me(!0),B(`[AUDIO] setIsSpeaking(true) - First chunk starting at ${E.toFixed(3)}`),te.current=E;const N=(E-k)*1e3;U.current=performance.now()+N,B(`[AUDIO] Response started. Initial startTime: ${E.toFixed(3)}, CT: ${k.toFixed(3)}`)}P.start(E);const X=(E-te.current)*1e3;i&&(Oe.current=X,B(`[AUDIO] New segment detected at +${X.toFixed(0)}ms. Resetting segment offset.`)),n.forEach((N,re)=>{const M=N.symbol??"";if(M){const se=un(M),ue=Math.round(N.start*1e3),Le=Math.round((N.duration??0)*1e3),xt=Oe.current+ue;re<3&&B(`[AUDIO] Viseme "${M}": segment_relative=${ue}ms, segment_offset=${Oe.current.toFixed(0)}ms => vtime=${xt}ms`),se.forEach(Rt=>{x.current.push({viseme:Rt.v,weight:Rt.w,vtime:xt,vduration:Le})})}}),S("Speaking...")}finally{if(D.current=!1,ee.current.length>0){const c=ee.current.shift();c&&st(c.audio,c.visemes,c.isNewSegment)}}}},Wt=async t=>{try{A(!0),je(!1),Ie.current="",de(""),S("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 h=u.result.split(",")[1];$e(),Y(""),fe.current="",Te.current=!1;const w=`${at}/stt?token=${encodeURIComponent(C)}`,k=await fetch(w,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({audio_base64:h,audio_format:"wav"})});if(k.status===429){try{const re=await k.text(),M=JSON.parse(re);B("[STT] 429 agent at capacity:",M?.detail)}catch{}Y(""),S("Busy"),A(!1);return}if(!k.ok){const re=await k.text();let M="STT Failed";try{M=JSON.parse(re).error||M}catch{re&&(M=re.slice(0,200))}throw new Error(M)}const E=k.body;if(B("this is body"+E),!E){S("STT Failed"),A(!1);return}const L=E.getReader();d.current=!1;const P=new TextDecoder;let $="",X=!1;const N=async(re,M)=>{switch(re){case"transcript":M.transcript!=null&&Y(String(M.transcript));break;case"text":{const se=M.delta??M.text??"";se&>(se);break}case"audio":{const se=M.chunk,ue=M.visemes??[],Le=!!M.is_new_segment;se&&await st(se,ue,Le);break}case"done":{X=!0,S("Ready"),A(!1);break}case"error":{X=!0,S("STT Failed"),A(!1);break}default:break}};for(;;){const{done:re,value:M}=await L.read();M&&($+=P.decode(M,{stream:!0}));const se=$.split(`
|
|
4
4
|
|
|
5
|
-
`)
|
|
5
|
+
`);$=se.pop()??"";for(const ue of se){const Le=qe(ue);Le&&await N(Le.event,Le.data)}if(re){if($.trim()){const ue=qe($.trim());ue&&await N(ue.event,ue.data)}X||(S("Ready"),A(!1));break}}}}catch(n){console.error("Audio Submission Error:",n),S("STT Failed"),A(!1)}},Vt=async t=>{t&&t.preventDefault(),je(!1),Ie.current="",de(""),!(!le||R)&&await Ht(le)},qe=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":fe.current="",Te.current=!1;break;case"text":{const i=n.delta??"";i&>(i);break}case"audio":{const i=n.chunk,c=n.visemes??[];i&&st(i,c);break}case"done":{const i=bt(),c=Ie.current.trim(),r=[...i,{role:"user",content:le||"..."},{role:"assistant",content:c}];Dt(r),Pe.current=[...x.current],S("Ready"),A(!1),Y("");break}case"error":{const i=n.message??"Unknown error";Ie.current=i,de(i),S("Agent Failed"),A(!1);break}}},Ht=async t=>{A(!0),S("Thinking..."),Ie.current="",fe.current="",Te.current=!1,$e(),ve.current=0,o(0);const n=`${at}/agent/chat?token=${encodeURIComponent(C)}`;try{const i=bt();let c=sessionStorage.getItem("STREAMOJI_LEADS_SESSION_LEAD_ID");c||(zt("[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};B("[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 L=await u.json();B("[CHAT] 429 agent at capacity:",L?.detail)}catch{}Y(""),S("Busy"),A(!1);return}if(!u.ok)throw new Error("Agent request failed");const h=u.body;if(!h){S("Agent Failed"),A(!1);return}const w=h.getReader();d.current=!1;const k=new TextDecoder;let E="";for(;;){const{done:L,value:P}=await w.read();B(`[SSE] Chunk received. done=${L}, length=${P?.length||0}`),P&&(E+=k.decode(P,{stream:!0}));const $=E.split(`
|
|
6
6
|
|
|
7
|
-
`);
|
|
7
|
+
`);E=$.pop()??"";for(const X of $){B(`[SSE] Processing block: ${X.slice(0,50)}...`);const N=qe(X);N&&(B(`[SSE] Event: ${N.event}`),vt(N.event,N.data))}if(L){if(B("[SSE] Stream finished"),E.trim()){const X=qe(E.trim());X&&vt(X.event,X.data)}S("Ready"),A(!1),Y("");break}}}catch(i){console.error("Chat Error:",i),S("Agent Failed"),A(!1)}},yt=O.trim(),it=yt&&F?yt.slice(0,f!=null&&f>0?f:0):"";e.useEffect(()=>{const t=Xe.current;t!=="exiting"&&(it?(Tt(it),t==="hidden"&&We("entering")):(t==="visible"||t==="entering")&&We("exiting"))},[it,oe]);const $t=e.useCallback(()=>{const t=Xe.current;t==="entering"?We("visible"):t==="exiting"&&We("hidden")},[]);return e.useLayoutEffect(()=>{const t=lt.current;t&&(t.scrollTop=t.scrollHeight)},[dt]),a.jsxs("div",{className:"avatar-widget-container",children:[a.jsxs("div",{className:"avatar-input-area",children:[pe!=="hidden"?a.jsx("div",{className:`avatar-thinking-tab${pe==="exiting"?" avatar-thinking-tab--exiting":pe==="entering"?" avatar-thinking-tab--entering":""}`,onAnimationEnd:It,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:et?a.jsxs("div",{className:"avatar-input-recording",children:[a.jsx("button",{type:"button",className:"avatar-recording-cancel",onClick:Pt,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(kt,{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(_t/60),":",String(_t%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"})})})]}):F&&!Ze?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(kt,{analyser:St})}),a.jsx("button",{type:"button",className:"avatar-speaking-stop",onClick:()=>$e(!0),title:"Stop",children:a.jsx("span",{className:"avatar-speaking-stop__icon","aria-hidden":!0})})]}):R?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:Vt,style:{flex:1,display:"flex",height:"100%",alignItems:"center"},children:[a.jsx("input",{id:"avatar-text-input",type:"text",value:le,onChange:t=>Y(t.target.value),placeholder:T==="Busy"?"Assisting another user":Ce||"Ask me anything",disabled:R||T==="Busy",autoComplete:"off",style:{width:"100%",height:"100%"}}),T==="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"})})}):le.trim()===""?a.jsx("button",{type:"button",className:"mic-button",onClick:jt,disabled:R,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:R,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:$t,children:a.jsx("div",{ref:lt,className:`avatar-bubble__content${Ze?" avatar-bubble__content--nudge":""}`,children:dt})}),a.jsx("div",{className:"avatar-canvas-layer",style:{width:V,height:V},children:a.jsxs(ct.Canvas,{shadows:!0,camera:{position:[.2,1.4,3],fov:42},gl:{alpha:!0},dpr:1.8,style:{pointerEvents:"none",width:"100%",height:"100%"},children:[a.jsx(ln,{target:rn}),a.jsx("ambientLight",{intensity:.7}),a.jsx("directionalLight",{position:[0,2,2],intensity:1}),a.jsx(Ge.Environment,{preset:"city"}),a.jsx(e.Suspense,{fallback:null,children:W!==null&&a.jsx(dn,{avatarUrl:W,isPlayingRef:y,visemeQueueRef:x,audioContextRef:we,responseAudioStartTimeRef:te,adjustments:nn,mood:m,expression:Q,agentResponse:O,isSpeaking:F,nextStartTimeRef:Se,stopPlayback:$e,setIsSpeaking:me,expressionUrl:Ye,onExpressionFinished:Ot,isNudgeResponse:Ze,avatarGender:p})})]})})]})})]})},mn=({token:l,onNavigationRequested:b})=>a.jsx(hn,{token:l,onNavigationRequested:b});exports.AvatarWidget=mn;
|
|
8
8
|
//# sourceMappingURL=avatar-widget.cjs.map
|