@sage-rsc/talking-head-react 1.0.47 → 1.0.48

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/index.cjs CHANGED
@@ -4,4 +4,4 @@
4
4
  ${e}
5
5
  </voice>
6
6
  </speak>
7
- `,s=await fetch(this.opt.ttsEndpoint,{method:"POST",headers:{"Ocp-Apim-Subscription-Key":this.opt.ttsApikey,"Content-Type":"application/ssml+xml","X-Microsoft-OutputFormat":"audio-16khz-128kbitrate-mono-mp3"},body:n});if(!s.ok)throw new Error(`Azure TTS error: ${s.status} ${s.statusText}`);const o=await s.arrayBuffer(),r=await this.audioCtx.decodeAudioData(o);console.log("Analyzing audio for precise lip-sync...");const h=await this.audioAnalyzer.analyzeAudio(r,e);console.log("Azure TTS Audio Analysis:",{text:e,audioDuration:r.duration,visemeCount:h.visemes.length,wordCount:h.words.length,features:{onsets:h.features.onsets.length,boundaries:h.features.phonemeBoundaries.length}});const a=[];for(let l=0;l<h.visemes.length;l++){const c=h.visemes[l],d=c.startTime*1e3,g=c.duration*1e3,y=c.intensity;a.push({template:{name:"viseme"},ts:[d-Math.min(60,2*g/3),d+Math.min(25,g/2),d+g+Math.min(60,g/2)],vs:{["viseme_"+c.viseme]:[null,y,0]}})}const u=[...t.anim,...a];this.audioPlaylist.push({anim:u,audio:r}),this.onSubtitles=t.onSubtitles||null,this.resetLips(),t.mood&&this.setMood(t.mood),this.playAudio()}async synthesizeWithExternalTTS(t){let e="<speak>";t.text.forEach((o,r)=>{r>0&&(e+=" <mark name='"+o.mark+"'/>"),e+=o.word.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll('"',"&quot;").replaceAll("'","&apos;").replace(new RegExp("^\\p{Dash_Punctuation}$","ug"),'<break time="750ms"/>')}),e+="</speak>";const i={method:"POST",headers:{"Content-Type":"application/json; charset=utf-8"},body:JSON.stringify({input:{ssml:e},voice:{languageCode:t.lang||this.avatar.ttsLang||this.opt.ttsLang,name:t.voice||this.avatar.ttsVoice||this.opt.ttsVoice},audioConfig:{audioEncoding:this.ttsAudioEncoding,speakingRate:(t.rate||this.avatar.ttsRate||this.opt.ttsRate)+this.mood.speech.deltaRate,pitch:(t.pitch||this.avatar.ttsPitch||this.opt.ttsPitch)+this.mood.speech.deltaPitch,volumeGainDb:(t.volume||this.avatar.ttsVolume||this.opt.ttsVolume)+this.mood.speech.deltaVolume},enableTimePointing:[1]})};this.opt.jwtGet&&typeof this.opt.jwtGet=="function"&&(i.headers.Authorization="Bearer "+await this.opt.jwtGet());const n=await fetch(this.opt.ttsEndpoint+(this.opt.ttsApikey?"?key="+this.opt.ttsApikey:""),i),s=await n.json();if(n.status===200&&s&&s.audioContent){const o=this.b64ToArrayBuffer(s.audioContent),r=await this.audioCtx.decodeAudioData(o);this.speakWithHands();const h=[0];let a=0;t.text.forEach((c,d)=>{if(d>0){let g=h[h.length-1];s.timepoints[a]&&(g=s.timepoints[a].timeSeconds*1e3,s.timepoints[a].markName===""+c.mark&&a++),h.push(g)}});const u=[{mark:0,time:0}];h.forEach((c,d)=>{if(d>0){let g=c-h[d-1];u[d-1].duration=g,u.push({mark:d,time:c})}});let l=1e3*r.duration;l>this.opt.ttsTrimEnd&&(l=l-this.opt.ttsTrimEnd),u[u.length-1].duration=l-u[u.length-1].time,t.anim.forEach(c=>{const d=u[c.mark];if(d)for(let g=0;g<c.ts.length;g++)c.ts[g]=d.time+c.ts[g]*d.duration+this.opt.ttsTrimStart}),this.audioPlaylist.push({anim:t.anim,audio:r}),this.onSubtitles=t.onSubtitles||null,this.resetLips(),t.mood&&this.setMood(t.mood),this.playAudio()}else this.startSpeaking(!0)}async startSpeaking(t=!1){if(!(!this.armature||this.isSpeaking&&!t))if(this.stateName="speaking",this.isSpeaking=!0,this.speechQueue.length){let e=this.speechQueue.shift();if(e.emoji){this.lookAtCamera(500);let i=e.emoji.dt.reduce((n,s)=>n+s,0);this.animQueue.push(this.animFactory(e.emoji)),setTimeout(this.startSpeaking.bind(this),i,!0)}else if(e.break)setTimeout(this.startSpeaking.bind(this),e.break,!0);else if(e.audio)e.isRaw||(this.lookAtCamera(500),this.speakWithHands(),this.resetLips()),this.audioPlaylist.push({anim:e.anim,audio:e.audio,isRaw:e.isRaw}),this.onSubtitles=e.onSubtitles||null,e.mood&&this.setMood(e.mood),this.playAudio();else if(e.text){this.lookAtCamera(500);try{!this.opt.ttsEndpoint||this.opt.ttsEndpoint===""?await this.synthesizeWithBrowserTTS(e):this.opt.ttsService==="elevenlabs"?await this.synthesizeWithElevenLabsTTS(e):this.opt.ttsService==="deepgram"?await this.synthesizeWithDeepgramTTS(e):this.opt.ttsService==="azure"?await this.synthesizeWithAzureTTS(e):await this.synthesizeWithExternalTTS(e)}catch(i){console.error("Error:",i),this.startSpeaking(!0)}}else e.anim?(this.onSubtitles=e.onSubtitles||null,this.resetLips(),e.mood&&this.setMood(e.mood),e.anim.forEach((i,n)=>{for(let s=0;s<i.ts.length;s++)i.ts[s]=this.animClock+10*n;this.animQueue.push(i)}),setTimeout(this.startSpeaking.bind(this),10*e.anim.length,!0)):e.marker?(typeof e.marker=="function"&&e.marker(),this.startSpeaking(!0)):this.startSpeaking(!0)}else this.stateName="idle",this.isSpeaking=!1}pauseSpeaking(){try{this.audioSpeechSource.stop()}catch{}this.audioPlaylist.length=0,this.stateName="idle",this.isSpeaking=!1,this.isAudioPlaying=!1,this.animQueue=this.animQueue.filter(t=>t.template.name!=="viseme"&&t.template.name!=="subtitles"&&t.template.name!=="blendshapes"),this.armature&&(this.resetLips(),this.render())}stopSpeaking(){try{this.audioSpeechSource.stop()}catch{}this.audioPlaylist.length=0,this.speechQueue.length=0,this.animQueue=this.animQueue.filter(t=>t.template.name!=="viseme"&&t.template.name!=="subtitles"&&t.template.name!=="blendshapes"),this.stateName="idle",this.isSpeaking=!1,this.isAudioPlaying=!1,this.armature&&(this.resetLips(),this.render())}async streamStart(t={},e=null,i=null,n=null,s=null){if(this.stopSpeaking(),this.isStreaming=!0,t.waitForAudioChunks!==void 0&&(this.streamWaitForAudioChunks=t.waitForAudioChunks),this.streamWaitForAudioChunks||(this.streamAudioStartTime=this.animClock),this.streamLipsyncQueue=[],this.streamLipsyncType=t.lipsyncType||this.streamLipsyncType||"visemes",this.streamLipsyncLang=t.lipsyncLang||this.streamLipsyncLang||this.avatar.lipsyncLang||this.opt.lipsyncLang,this.onAudioStart=e,this.onAudioEnd=i,this.onMetrics=s,t.sampleRate!==void 0){const r=t.sampleRate;typeof r=="number"&&r>=8e3&&r<=96e3?r!==this.audioCtx.sampleRate&&this.initAudioGraph(r):console.warn("Invalid sampleRate provided. It must be a number between 8000 and 96000 Hz.")}if(t.gain!==void 0&&(this.audioStreamGainNode.gain.value=t.gain),!this.streamWorkletNode||!this.streamWorkletNode.port||this.streamWorkletNode.numberOfOutputs===0||this.streamWorkletNode.context!==this.audioCtx){if(this.streamWorkletNode)try{this.streamWorkletNode.disconnect(),this.streamWorkletNode=null}catch{}if(!this.workletLoaded)try{const r=this.audioCtx.audioWorklet.addModule(et.href),h=new Promise((a,u)=>setTimeout(()=>u(new Error("Worklet loading timed out")),5e3));await Promise.race([r,h]),this.workletLoaded=!0}catch(r){throw console.error("Failed to load audio worklet:",r),new Error("Failed to initialize streaming speech")}this.streamWorkletNode=new AudioWorkletNode(this.audioCtx,"playback-worklet",{processorOptions:{sampleRate:this.audioCtx.sampleRate,metrics:t.metrics||{enabled:!1}}}),this.streamWorkletNode.connect(this.audioStreamGainNode),this.streamWorkletNode.connect(this.audioAnalyzerNode),this.streamWorkletNode.port.onmessage=r=>{if(r.data.type==="playback-started"&&(this.isSpeaking=!0,this.stateName="speaking",this.streamWaitForAudioChunks&&(this.streamAudioStartTime=this.animClock),this._processStreamLipsyncQueue(),this.speakWithHands(),this.onAudioStart))try{this.onAudioStart?.()}catch(h){console.error(h)}if(r.data.type==="playback-ended"&&(this._streamPause(),this.onAudioEnd))try{this.onAudioEnd()}catch{}if(this.onMetrics&&r.data.type==="metrics")try{this.onMetrics(r.data)}catch{}}}if(t.metrics)try{this.streamWorkletNode.port.postMessage({type:"config-metrics",data:t.metrics})}catch{}if(this.resetLips(),this.lookAtCamera(500),t.mood&&this.setMood(t.mood),this.onSubtitles=n||null,this.audioCtx.state==="suspended"||this.audioCtx.state==="interrupted"){const r=this.audioCtx.resume(),h=new Promise((a,u)=>setTimeout(()=>u("p2"),1e3));try{await Promise.race([r,h])}catch{console.warn("Can't play audio. Web Audio API suspended. This is often due to calling some speak method before the first user action, which is typically prevented by the browser.");return}}}streamNotifyEnd(){!this.isStreaming||!this.streamWorkletNode||this.streamWorkletNode.port.postMessage({type:"no-more-data"})}streamInterrupt(){if(!this.isStreaming)return;const t=this.isSpeaking;if(this.streamWorkletNode)try{this.streamWorkletNode.port.postMessage({type:"stop"})}catch{}if(this._streamPause(!0),t&&this.onAudioEnd)try{this.onAudioEnd()}catch{}}streamStop(){if(this.isStreaming){if(this.streamInterrupt(),this.streamWorkletNode){try{this.streamWorkletNode.disconnect()}catch{}this.streamWorkletNode=null}this.isStreaming=!1}}_streamPause(t=!1){this.isSpeaking=!1,this.stateName="idle",t&&(this.streamWaitForAudioChunks&&(this.streamAudioStartTime=null),this.streamLipsyncQueue=[],this.animQueue=this.animQueue.filter(e=>e.template.name!=="viseme"&&e.template.name!=="subtitles"&&e.template.name!=="blendshapes"),this.armature&&(this.resetLips(),this.render()))}_processStreamLipsyncQueue(){if(this.isStreaming)for(;this.streamLipsyncQueue.length>0;){const t=this.streamLipsyncQueue.shift();this._processLipsyncData(t,this.streamAudioStartTime)}}_processLipsyncData(t,e){if(this.isStreaming){if(t.visemes&&this.streamLipsyncType=="visemes")for(let i=0;i<t.visemes.length;i++){const n=t.visemes[i],s=e+t.vtimes[i],o=t.vdurations[i],r={template:{name:"viseme"},ts:[s-2*o/3,s+o/2,s+o+o/2],vs:{["viseme_"+n]:[null,n==="PP"||n==="FF"?.9:.6,0]}};this.animQueue.push(r)}if(t.words&&(this.onSubtitles||this.streamLipsyncType=="words"))for(let i=0;i<t.words.length;i++){const n=t.words[i],s=t.wtimes[i];let o=t.wdurations[i];if(n.length&&(this.onSubtitles&&this.animQueue.push({template:{name:"subtitles"},ts:[e+s],vs:{subtitles:[" "+n]}}),this.streamLipsyncType=="words")){const r=this.streamLipsyncLang||this.avatar.lipsyncLang||this.opt.lipsyncLang,h=this.lipsyncPreProcessText(n,r),a=this.lipsyncWordsToVisemes(h,r);if(a&&a.visemes&&a.visemes.length){const u=a.times[a.visemes.length-1]+a.durations[a.visemes.length-1],l=Math.min(o,Math.max(0,o-a.visemes.length*150));let c=.6+this.convertRange(l,[0,o],[0,.4]);if(o=Math.min(o,a.visemes.length*200),u>0)for(let d=0;d<a.visemes.length;d++){const g=e+s+a.times[d]/u*o,y=a.durations[d]/u*o;this.animQueue.push({template:{name:"viseme"},ts:[g-Math.min(60,2*y/3),g+Math.min(25,y/2),g+y+Math.min(60,y/2)],vs:{["viseme_"+a.visemes[d]]:[null,a.visemes[d]==="PP"||a.visemes[d]==="FF"?.9:c,0]}})}}}}if(t.anims&&this.streamLipsyncType=="blendshapes")for(let i=0;i<t.anims.length;i++){let n=t.anims[i];n.delay+=e;let s=this.animFactory(n,!1,1,1,!0);this.animQueue.push(s)}}}streamAudio(t){if(!(!this.isStreaming||!this.streamWorkletNode)){if(this.isSpeaking||(this.streamLipsyncQueue=[],this.streamAudioStartTime=null),this.isSpeaking=!0,this.stateName="speaking",t.audio!==void 0){const e={type:"audioData",data:null};if(t.audio instanceof ArrayBuffer)e.data=t.audio,this.streamWorkletNode.port.postMessage(e,[e.data]);else if(t.audio instanceof Int16Array||t.audio instanceof Uint8Array){const i=t.audio.buffer.slice(t.audio.byteOffset,t.audio.byteOffset+t.audio.byteLength);e.data=i,this.streamWorkletNode.port.postMessage(e,[e.data])}else if(t.audio instanceof Float32Array){const i=new Int16Array(t.audio.length);for(let n=0;n<t.audio.length;n++){let s=Math.max(-1,Math.min(1,t.audio[n]));i[n]=s<0?s*32768:s*32767}e.data=i.buffer,this.streamWorkletNode.port.postMessage(e,[e.data])}else console.error("r.audio is not a supported type. Must be ArrayBuffer, Int16Array, Uint8Array, or Float32Array:",t.audio)}if(t.visemes||t.anims||t.words){if(this.streamWaitForAudioChunks&&!this.streamAudioStartTime){this.streamLipsyncQueue.length>=200&&this.streamLipsyncQueue.shift(),this.streamLipsyncQueue.push(t);return}else!this.streamWaitForAudioChunks&&!this.streamAudioStartTime&&(this.streamAudioStartTime=this.animClock);this._processLipsyncData(t,this.streamAudioStartTime)}}}makeEyeContact(t){this.animQueue.push(this.animFactory({name:"eyecontact",dt:[0,t],vs:{eyeContact:[1]}}))}lookAhead(t){if(t){let e=(Math.random()-.5)/4,i=(Math.random()-.5)/4,n=this.animQueue.findIndex(o=>o.template.name==="lookat");n!==-1&&this.animQueue.splice(n,1);const s={name:"lookat",dt:[750,t],vs:{bodyRotateX:[e],bodyRotateY:[i],eyesRotateX:[-3*e+.1],eyesRotateY:[-5*i],browInnerUp:[[0,.7]],mouthLeft:[[0,.7]],mouthRight:[[0,.7]],eyeContact:[0],headMove:[0]}};this.animQueue.push(this.animFactory(s))}}lookAtCamera(t){let e;if(this.speakTo&&(e=new f.Vector3,this.speakTo.objectLeftEye?.isObject3D?(this.speakTo.armature.objectHead,this.speakTo.objectLeftEye.updateMatrixWorld(!0),this.speakTo.objectRightEye.updateMatrixWorld(!0),ie.setFromMatrixPosition(this.speakTo.objectLeftEye.matrixWorld),ne.setFromMatrixPosition(this.speakTo.objectRightEye.matrixWorld),e.addVectors(ie,ne).divideScalar(2)):this.speakTo.isObject3D?this.speakTo.getWorldPosition(e):this.speakTo.isVector3?e.set(this.speakTo):this.speakTo.x&&this.speakTo.y&&this.speakTo.z&&e.set(this.speakTo.x,this.speakTo.y,this.speakTo.z)),!e){if(this.avatar.hasOwnProperty("avatarIgnoreCamera")){if(this.avatar.avatarIgnoreCamera){this.lookAhead(t);return}}else if(this.opt.avatarIgnoreCamera){this.lookAhead(t);return}this.lookAt(null,null,t);return}this.objectLeftEye.updateMatrixWorld(!0),this.objectRightEye.updateMatrixWorld(!0),ie.setFromMatrixPosition(this.objectLeftEye.matrixWorld),ne.setFromMatrixPosition(this.objectRightEye.matrixWorld),ie.add(ne).divideScalar(2),N.copy(this.armature.quaternion),N.multiply(this.poseTarget.props["Hips.quaternion"]),N.multiply(this.poseTarget.props["Spine.quaternion"]),N.multiply(this.poseTarget.props["Spine1.quaternion"]),N.multiply(this.poseTarget.props["Spine2.quaternion"]),N.multiply(this.poseTarget.props["Neck.quaternion"]),N.multiply(this.poseTarget.props["Head.quaternion"]);const i=new f.Vector3().subVectors(e,ie).normalize(),n=Math.atan2(i.x,i.z),s=Math.asin(-i.y);F.set(s,n,0,"YXZ");const r=new f.Quaternion().setFromEuler(F),h=new f.Quaternion().copy(r).multiply(N.clone().invert());F.setFromQuaternion(h,"YXZ");let a=F.x/(40/24)+.2,u=F.y/(9/4),l=Math.min(.6,Math.max(-.3,a)),c=Math.min(.8,Math.max(-.8,u)),d=(Math.random()-.5)/4,g=(Math.random()-.5)/4;if(t){let y=this.animQueue.findIndex(I=>I.template.name==="lookat");y!==-1&&this.animQueue.splice(y,1);const x={name:"lookat",dt:[750,t],vs:{bodyRotateX:[l+d],bodyRotateY:[c+g],eyesRotateX:[-3*d+.1],eyesRotateY:[-5*g],browInnerUp:[[0,.7]],mouthLeft:[[0,.7]],mouthRight:[[0,.7]],eyeContact:[0],headMove:[0]}};this.animQueue.push(this.animFactory(x))}}lookAt(t,e,i){if(!this.camera)return;const n=this.nodeAvatar.getBoundingClientRect();this.objectLeftEye.updateMatrixWorld(!0),this.objectRightEye.updateMatrixWorld(!0);const s=new f.Vector3().setFromMatrixPosition(this.objectLeftEye.matrixWorld),o=new f.Vector3().setFromMatrixPosition(this.objectRightEye.matrixWorld),r=new f.Vector3().addVectors(s,o).divideScalar(2);r.project(this.camera);let h=(r.x+1)/2*n.width+n.left,a=-(r.y-1)/2*n.height+n.top;t===null&&(t=h),e===null&&(e=a),N.copy(this.armature.quaternion),N.multiply(this.poseTarget.props["Hips.quaternion"]),N.multiply(this.poseTarget.props["Spine.quaternion"]),N.multiply(this.poseTarget.props["Spine1.quaternion"]),N.multiply(this.poseTarget.props["Spine2.quaternion"]),N.multiply(this.poseTarget.props["Neck.quaternion"]),N.multiply(this.poseTarget.props["Head.quaternion"]),F.setFromQuaternion(N);let u=F.x/(40/24),l=F.y/(9/4),c=Math.min(.4,Math.max(-.4,this.camera.rotation.x)),d=Math.min(.4,Math.max(-.4,this.camera.rotation.y)),g=Math.max(window.innerWidth-h,h),y=Math.max(window.innerHeight-a,a),x=this.convertRange(e,[a-y,a+y],[-.3,.6])-u+c,I=this.convertRange(t,[h-g,h+g],[-.8,.8])-l+d;x=Math.min(.6,Math.max(-.3,x)),I=Math.min(.8,Math.max(-.8,I));let E=(Math.random()-.5)/4,p=(Math.random()-.5)/4;if(i){let P=this.animQueue.findIndex(R=>R.template.name==="lookat");P!==-1&&this.animQueue.splice(P,1);const H={name:"lookat",dt:[750,i],vs:{bodyRotateX:[x+E],bodyRotateY:[I+p],eyesRotateX:[-3*E+.1],eyesRotateY:[-5*p],browInnerUp:[[0,.7]],mouthLeft:[[0,.7]],mouthRight:[[0,.7]],eyeContact:[0],headMove:[0]}};this.animQueue.push(this.animFactory(H))}}touchAt(t,e){if(!this.camera)return;const i=this.nodeAvatar.getBoundingClientRect(),n=new f.Vector2((t-i.left)/i.width*2-1,-((e-i.top)/i.height)*2+1),s=new f.Raycaster;s.setFromCamera(n,this.camera);const o=s.intersectObject(this.armature);if(o.length>0){const r=o[0].point,h=new f.Vector3,a=new f.Vector3;this.objectLeftArm.getWorldPosition(h),this.objectRightArm.getWorldPosition(a);const u=h.distanceToSquared(r),l=a.distanceToSquared(r);u<l?(this.ikSolve({iterations:20,root:"LeftShoulder",effector:"LeftHandMiddle1",links:[{link:"LeftHand",minx:-.5,maxx:.5,miny:-1,maxy:1,minz:-.5,maxz:.5,maxAngle:.1},{link:"LeftForeArm",minx:-.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-.5,maxz:3,maxAngle:.2},{link:"LeftArm",minx:-1.5,maxx:1.5,miny:0,maxy:0,minz:-1,maxz:3}]},r,!1,1e3),this.setValue("handFistLeft",0)):(this.ikSolve({iterations:20,root:"RightShoulder",effector:"RightHandMiddle1",links:[{link:"RightHand",minx:-.5,maxx:.5,miny:-1,maxy:1,minz:-.5,maxz:.5,maxAngle:.1},{link:"RightForeArm",minx:-.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-3,maxz:.5,maxAngle:.2},{link:"RightArm",minx:-1.5,maxx:1.5,miny:0,maxy:0,minz:-1,maxz:3}]},r,!1,1e3),this.setValue("handFistRight",0))}else["LeftArm","LeftForeArm","LeftHand","RightArm","RightForeArm","RightHand"].forEach(r=>{let h=r+".quaternion";this.poseTarget.props[h].copy(this.getPoseTemplateProp(h)),this.poseTarget.props[h].t=this.animClock,this.poseTarget.props[h].d=1e3});return o.length>0}speakWithHands(t=0,e=.5){if(this.mixer||this.gesture||!this.poseTarget.template.standing||this.poseTarget.template.bend||Math.random()>e)return;this.ikSolve({root:"LeftShoulder",effector:"LeftHandMiddle1",links:[{link:"LeftHand",minx:-.5,maxx:.5,miny:-1,maxy:1,minz:-.5,maxz:.5},{link:"LeftForeArm",minx:-.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-.5,maxz:3},{link:"LeftArm",minx:-1.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-1,maxz:3}]},new f.Vector3(this.gaussianRandom(0,.5),this.gaussianRandom(-.8,-.2),this.gaussianRandom(0,.5)),!0),this.ikSolve({root:"RightShoulder",effector:"RightHandMiddle1",links:[{link:"RightHand",minx:-.5,maxx:.5,miny:-1,maxy:1,minz:-.5,maxz:.5},{link:"RightForeArm",minx:-.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-3,maxz:.5},{link:"RightArm"}]},new f.Vector3(this.gaussianRandom(-.5,0),this.gaussianRandom(-.8,-.2),this.gaussianRandom(0,.5)),!0);const i=[],n=[];i.push(100+Math.round(Math.random()*500)),n.push({duration:1e3,props:{"LeftHand.quaternion":new f.Quaternion().setFromEuler(new f.Euler(0,-1-Math.random(),0)),"RightHand.quaternion":new f.Quaternion().setFromEuler(new f.Euler(0,1+Math.random(),0))}}),["LeftArm","LeftForeArm","RightArm","RightForeArm"].forEach(o=>{n[0].props[o+".quaternion"]=this.ikMesh.getObjectByName(o).quaternion.clone()}),i.push(1e3+Math.round(Math.random()*500)),n.push({duration:2e3,props:{}}),["LeftArm","LeftForeArm","RightArm","RightForeArm","LeftHand","RightHand"].forEach(o=>{n[1].props[o+".quaternion"]=null});const s=this.animFactory({name:"talkinghands",delay:t,dt:i,vs:{moveto:n}});this.animQueue.push(s)}getSlowdownRate(t){return this.animSlowdownRate}setSlowdownRate(t){this.animSlowdownRate=t,this.audioSpeechSource.playbackRate.value=1/this.animSlowdownRate,this.audioBackgroundSource.playbackRate.value=1/this.animSlowdownRate}getAutoRotateSpeed(t){return this.controls.autoRotateSpeed}setAutoRotateSpeed(t){this.controls.autoRotateSpeed=t,this.controls.autoRotate=t>0}start(){this.armature&&this.isRunning===!1&&(this.audioCtx.resume(),this.animTimeLast=performance.now(),this.isRunning=!0,this.isAvatarOnly||requestAnimationFrame(this.animate.bind(this)))}stop(){this.isRunning=!1,this.audioCtx.suspend()}startListening(t,e={},i=null){this.listeningAnalyzer=t,this.listeningAnalyzer.fftSize=256,this.listeningAnalyzer.smoothingTimeConstant=.1,this.listeningAnalyzer.minDecibels=-70,this.listeningAnalyzer.maxDecibels=-10,this.listeningOnchange=i&&typeof i=="function"?i:null,this.listeningSilenceThresholdLevel=e?.hasOwnProperty("listeningSilenceThresholdLevel")?e.listeningSilenceThresholdLevel:this.opt.listeningSilenceThresholdLevel,this.listeningSilenceThresholdMs=e?.hasOwnProperty("listeningSilenceThresholdMs")?e.listeningSilenceThresholdMs:this.opt.listeningSilenceThresholdMs,this.listeningSilenceDurationMax=e?.hasOwnProperty("listeningSilenceDurationMax")?e.listeningSilenceDurationMax:this.opt.listeningSilenceDurationMax,this.listeningActiveThresholdLevel=e?.hasOwnProperty("listeningActiveThresholdLevel")?e.listeningActiveThresholdLevel:this.opt.listeningActiveThresholdLevel,this.listeningActiveThresholdMs=e?.hasOwnProperty("listeningActiveThresholdMs")?e.listeningActiveThresholdMs:this.opt.listeningActiveThresholdMs,this.listeningActiveDurationMax=e?.hasOwnProperty("listeningActiveDurationMax")?e.listeningActiveDurationMax:this.opt.listeningActiveDurationMax,this.listeningActive=!1,this.listeningVolume=0,this.listeningTimer=0,this.listeningTimerTotal=0,this.isListening=!0}stopListening(){this.isListening=!1}async playAnimation(t,e=null,i=10,n=0,s=.01,o=!1){if(!this.armature)return;this.positionWasLocked=!o,o?console.log("Position locking disabled for FBX animation:",t):(this.lockAvatarPosition(),console.log("Position locked immediately before FBX animation:",t));let r=this.animClips.find(h=>h.url===t+"-"+n);if(r){let h=this.animQueue.find(l=>l.template.name==="pose");h&&(h.ts[0]=1/0),Object.entries(r.pose.props).forEach(l=>{this.poseBase.props[l[0]]=l[1].clone(),this.poseTarget.props[l[0]]=l[1].clone(),this.poseTarget.props[l[0]].t=0,this.poseTarget.props[l[0]].d=1e3}),this.mixer?console.log("Using existing mixer for FBX animation, preserving morph targets"):(this.mixer=new f.AnimationMixer(this.armature),console.log("Created new mixer for FBX animation")),this.mixer.addEventListener("finished",this.stopAnimation.bind(this),{once:!0});const a=Math.ceil(i/r.clip.duration),u=this.mixer.clipAction(r.clip);u.setLoop(f.LoopRepeat,a),u.clampWhenFinished=!0,this.currentFBXAction=u;try{u.fadeIn(.5).play(),console.log("FBX animation started successfully:",t)}catch(l){console.warn("FBX animation failed to start:",l),this.stopAnimation();return}if(u.getClip().tracks.length===0){console.warn("FBX animation has no valid tracks, stopping"),this.stopAnimation();return}}else{if(t.split(".").pop().toLowerCase()!=="fbx"){console.error(`Invalid file type for FBX animation: ${t}. Expected .fbx file.`);return}let a=!1;try{const c=await fetch(t,{method:"HEAD"});if(a=c.ok,!a){console.error(`FBX file not found at ${t}. Status: ${c.status}`),console.error("Please check:"),console.error("1. File path is correct (note: path is case-sensitive)"),console.error("2. File exists in your public folder"),console.error("3. File is accessible (not blocked by server)");return}}catch(c){console.warn(`Could not verify file existence for ${t}, attempting to load anyway:`,c)}const u=new Ie.FBXLoader;let l;try{l=await u.loadAsync(t,e)}catch(c){console.error(`Failed to load FBX animation from ${t}:`,c),console.error("Error details:",{message:c.message,url:t,suggestion:"Make sure the file is a valid FBX file and the path is correct"}),c.message&&c.message.includes("version number")&&(console.error("FBX Loader Error: Cannot find version number"),console.error("This error usually means:"),console.error("1. The file is not a valid FBX file (might be GLB, corrupted, or wrong format)"),console.error("2. The file might be corrupted"),console.error("3. The file path might be incorrect"),console.error("4. The server returned an HTML error page instead of the FBX file"),console.error("5. The file might not exist at that path"),console.error(""),console.error("Solution: Please verify:"),console.error(` - File exists at: ${t}`),console.error(" - File is a valid FBX binary file"),console.error(" - File path matches your public folder structure"),console.error(" - File is not corrupted"));try{const d=await fetch(t),g=d.headers.get("content-type"),y=await d.text();console.error("Response details:",{status:d.status,contentType:g,firstBytes:y.substring(0,100),isHTML:y.trim().startsWith("<!DOCTYPE")||y.trim().startsWith("<html")}),(y.trim().startsWith("<!DOCTYPE")||y.trim().startsWith("<html"))&&console.error("The server returned an HTML page instead of an FBX file. The file path is likely incorrect.")}catch(d){console.error("Could not fetch file for debugging:",d)}return}if(l&&l.animations&&l.animations[n]){let c=l.animations[n];const d={};c.tracks.forEach(y=>{y.name=y.name.replaceAll("mixamorig","");const x=y.name.split(".");if(x[1]==="position"){for(let I=0;I<y.values.length;I++)y.values[I]=y.values[I]*s;d[y.name]=new f.Vector3(y.values[0],y.values[1],y.values[2])}else x[1]==="quaternion"?d[y.name]=new f.Quaternion(y.values[0],y.values[1],y.values[2],y.values[3]):x[1]==="rotation"&&(d[x[0]+".quaternion"]=new f.Quaternion().setFromEuler(new f.Euler(y.values[0],y.values[1],y.values[2],"XYZ")).normalize())});const g={props:d};d["Hips.position"]&&(d["Hips.position"].y<.5?g.lying=!0:g.standing=!0),this.animClips.push({url:t+"-"+n,clip:c,pose:g}),this.playAnimation(t,e,i,n,s)}else{const c="Animation "+t+" (ndx="+n+") not found";console.error(c),l&&l.animations?console.error(`FBX file loaded but has ${l.animations.length} animation(s), requested index ${n}`):console.error(l?"FBX file loaded but contains no animations":"FBX file failed to load or is invalid")}}}stopAnimation(){if(this.currentFBXAction&&(this.currentFBXAction.stop(),this.currentFBXAction=null,console.log("FBX animation action stopped, mixer preserved for lip-sync")),this.mixer&&this.mixer._actions.length===0&&(this.mixer=null,console.log("Mixer destroyed as no actions remain")),this.positionWasLocked?(this.unlockAvatarPosition(),console.log("Position unlocked after FBX animation stopped")):console.log("Position was not locked, no unlock needed"),this.gesture)for(let[e,i]of Object.entries(this.gesture))i.t=this.animClock,i.d=1e3,this.poseTarget.props.hasOwnProperty(e)&&(this.poseTarget.props[e].copy(i),this.poseTarget.props[e].t=this.animClock,this.poseTarget.props[e].d=1e3);let t=this.animQueue.find(e=>e.template.name==="pose");t&&(t.ts[0]=this.animClock),this.setPoseFromTemplate(null)}async playPose(t,e=null,i=5,n=0,s=.01){if(!this.armature)return;let o=this.poseTemplates[t];if(!o){const r=this.animPoses.find(h=>h.url===t+"-"+n);r&&(o=r.pose)}if(o){this.poseName=t,this.mixer=null;let r=this.animQueue.find(h=>h.template.name==="pose");r&&(r.ts[0]=this.animClock+i*1e3+2e3),this.setPoseFromTemplate(o)}else{let h=await new Ie.FBXLoader().loadAsync(t,e);if(h&&h.animations&&h.animations[n]){let a=h.animations[n];const u={};a.tracks.forEach(c=>{c.name=c.name.replaceAll("mixamorig","");const d=c.name.split(".");d[1]==="position"?u[c.name]=new f.Vector3(c.values[0]*s,c.values[1]*s,c.values[2]*s):d[1]==="quaternion"?u[c.name]=new f.Quaternion(c.values[0],c.values[1],c.values[2],c.values[3]):d[1]==="rotation"&&(u[d[0]+".quaternion"]=new f.Quaternion().setFromEuler(new f.Euler(c.values[0],c.values[1],c.values[2],"XYZ")).normalize())});const l={props:u};u["Hips.position"]&&(u["Hips.position"].y<.5?l.lying=!0:l.standing=!0),this.animPoses.push({url:t+"-"+n,pose:l}),this.playPose(t,e,i,n,s)}else{const a="Pose "+t+" (ndx="+n+") not found";console.error(a)}}}stopPose(){this.stopAnimation()}playGesture(t,e=3,i=!1,n=1e3){if(!this.armature)return;let s=this.gestureTemplates[t];if(s){this.gestureTimeout&&(clearTimeout(this.gestureTimeout),this.gestureTimeout=null);let r=this.animQueue.findIndex(h=>h.template.name==="talkinghands");r!==-1&&(this.animQueue[r].ts=this.animQueue[r].ts.map(h=>0)),this.gesture=this.propsToThreeObjects(s),i&&(this.gesture=this.mirrorPose(this.gesture)),t==="namaste"&&this.avatar.body==="M"&&(this.gesture["RightArm.quaternion"].rotateTowards(new f.Quaternion(0,1,0,0),-.25),this.gesture["LeftArm.quaternion"].rotateTowards(new f.Quaternion(0,1,0,0),-.25));for(let[h,a]of Object.entries(this.gesture))a.t=this.animClock,a.d=n,this.poseTarget.props.hasOwnProperty(h)&&(this.poseTarget.props[h].copy(a),this.poseTarget.props[h].t=this.animClock,this.poseTarget.props[h].d=n);e&&Number.isFinite(e)&&(this.gestureTimeout=setTimeout(this.stopGesture.bind(this,n),1e3*e))}let o=this.animEmojis[t];if(o&&(o&&o.link&&(o=this.animEmojis[o.link]),o)){this.lookAtCamera(500);const r=this.animFactory(o);if(r.gesture=!0,e&&Number.isFinite(e)){const h=r.ts[0],u=r.ts[r.ts.length-1]-h;if(e*1e3-u>0){const c=[];for(let y=1;y<r.ts.length;y++)c.push(r.ts[y]-r.ts[y-1]);const d=o.template?.rescale||c.map(y=>y/u),g=e*1e3-u;r.ts=r.ts.map((y,x,I)=>x===0?h:I[x-1]+c[x-1]+d[x-1]*g)}else{const c=e*1e3/u;r.ts=r.ts.map(d=>h+c*(d-h))}}this.animQueue.push(r)}}stopGesture(t=1e3){if(this.gestureTimeout&&(clearTimeout(this.gestureTimeout),this.gestureTimeout=null),this.gesture){const i=Object.entries(this.gesture);this.gesture=null;for(const[n,s]of i)this.poseTarget.props.hasOwnProperty(n)&&(this.poseTarget.props[n].copy(this.getPoseTemplateProp(n)),this.poseTarget.props[n].t=this.animClock,this.poseTarget.props[n].d=t)}let e=this.animQueue.findIndex(i=>i.gesture);e!==-1&&this.animQueue.splice(e,1)}ikSolve(t,e=null,i=!1,n=null){const s=new f.Vector3,o=new f.Vector3,r=new f.Vector3,h=new f.Vector3,a=new f.Quaternion,u=new f.Vector3,l=new f.Vector3,c=new f.Vector3,d=this.ikMesh.getObjectByName(t.root);d.position.setFromMatrixPosition(this.armature.getObjectByName(t.root).matrixWorld),d.quaternion.setFromRotationMatrix(this.armature.getObjectByName(t.root).matrixWorld),e&&i&&e.applyQuaternion(this.armature.quaternion).add(d.position);const g=this.ikMesh.getObjectByName(t.effector),y=t.links;y.forEach(I=>{I.bone=this.ikMesh.getObjectByName(I.link),I.bone.quaternion.copy(this.getPoseTemplateProp(I.link+".quaternion"))}),d.updateMatrixWorld(!0);const x=t.iterations||10;if(e)for(let I=0;I<x;I++){let E=!1;for(let p=0,P=y.length;p<P;p++){const H=y[p].bone;H.matrixWorld.decompose(h,a,u),a.invert(),o.setFromMatrixPosition(g.matrixWorld),r.subVectors(o,h),r.applyQuaternion(a),r.normalize(),s.subVectors(e,h),s.applyQuaternion(a),s.normalize();let R=s.dot(r);R>1?R=1:R<-1&&(R=-1),R=Math.acos(R),!(R<1e-5)&&(y[p].minAngle!==void 0&&R<y[p].minAngle&&(R=y[p].minAngle),y[p].maxAngle!==void 0&&R>y[p].maxAngle&&(R=y[p].maxAngle),l.crossVectors(r,s),l.normalize(),N.setFromAxisAngle(l,R),H.quaternion.multiply(N),H.rotation.setFromVector3(c.setFromEuler(H.rotation).clamp(new f.Vector3(y[p].minx!==void 0?y[p].minx:-1/0,y[p].miny!==void 0?y[p].miny:-1/0,y[p].minz!==void 0?y[p].minz:-1/0),new f.Vector3(y[p].maxx!==void 0?y[p].maxx:1/0,y[p].maxy!==void 0?y[p].maxy:1/0,y[p].maxz!==void 0?y[p].maxz:1/0))),H.updateMatrixWorld(!0),E=!0)}if(!E)break}n&&y.forEach(I=>{this.poseTarget.props[I.link+".quaternion"].copy(I.bone.quaternion),this.poseTarget.props[I.link+".quaternion"].t=this.animClock,this.poseTarget.props[I.link+".quaternion"].d=n})}dispose(){this.isRunning=!1,this.stop(),this.stopSpeaking(),this.streamStop(),this.isAvatarOnly?this.armature&&(this.armature.parent&&this.armature.parent.remove(this.armature),this.clearThree(this.armature)):(this.clearThree(this.scene),this.resizeobserver.disconnect(),this.renderer&&(this.renderer.dispose(),this.renderer.domElement&&this.renderer.domElement.parentNode&&this.renderer.domElement.parentNode.removeChild(this.renderer.domElement),this.renderer=null)),this.clearThree(this.ikMesh),this.dynamicbones.dispose()}}const se={apiKey:"sk_ace57ef3ef65a92b9d3bee2a00183b78ca790bc3e10964f2",endpoint:"https://api.elevenlabs.io/v1/text-to-speech",defaultVoice:"21m00Tcm4TlvDq8ikWAM",voices:{rachel:"21m00Tcm4TlvDq8ikWAM",drew:"29vD33N1CtxCmqQRPOHJ",bella:"EXAVITQu4vr4xnSDxMaL",antoni:"ErXwobaYiN019PkySvjV",elli:"MF3mGyEYCl7XYWbV9V6O",josh:"VR6AewLTigWG4xSOukaG"}},ke={defaultVoice:"aura-2-thalia-en",voices:{thalia:"aura-2-thalia-en",asteria:"aura-2-asteria-en",orion:"aura-2-orion-en",stella:"aura-2-stella-en",athena:"aura-2-athena-en",hera:"aura-2-hera-en",zeus:"aura-2-zeus-en"}};function me(){return{service:"elevenlabs",endpoint:se.endpoint,apiKey:se.apiKey,defaultVoice:se.defaultVoice,voices:se.voices}}function it(){const M=me(),t=[];return Object.entries(M.voices).forEach(([e,i])=>{t.push({value:i,label:`${e.charAt(0).toUpperCase()+e.slice(1)} (${M.service})`})}),t}const xe=A.forwardRef(({avatarUrl:M="/avatars/brunette.glb",avatarBody:t="F",mood:e="neutral",ttsLang:i="en",ttsService:n=null,ttsVoice:s=null,ttsApiKey:o=null,bodyMovement:r="idle",movementIntensity:h=.5,showFullAvatar:a=!0,cameraView:u="upper",onReady:l=()=>{},onLoading:c=()=>{},onError:d=()=>{},className:g="",style:y={},animations:x={}},I)=>{const E=A.useRef(null),p=A.useRef(null),[P,H]=A.useState(!0),[R,O]=A.useState(null),[D,J]=A.useState(!1),X=me(),k=n||X.service;let B;k==="browser"?B={service:"browser",endpoint:"",apiKey:null,defaultVoice:"Google US English"}:k==="elevenlabs"?B={service:"elevenlabs",endpoint:"https://api.elevenlabs.io/v1/text-to-speech",apiKey:o||X.apiKey,defaultVoice:s||X.defaultVoice||se.defaultVoice,voices:X.voices||se.voices}:k==="deepgram"?B={service:"deepgram",endpoint:"https://api.deepgram.com/v1/speak",apiKey:o||X.apiKey,defaultVoice:s||X.defaultVoice||ke.defaultVoice,voices:X.voices||ke.voices}:B={...X,apiKey:o!==null?o:X.apiKey};const W={url:M,body:t,avatarMood:e,ttsLang:k==="browser"?"en-US":i,ttsVoice:s||B.defaultVoice,lipsyncLang:"en",showFullAvatar:a,bodyMovement:r,movementIntensity:h},V={ttsEndpoint:B.endpoint,ttsApikey:B.apiKey,ttsService:k,lipsyncModules:["en"],cameraView:u},Q=A.useCallback(async()=>{if(!(!E.current||p.current))try{if(H(!0),O(null),p.current=new we(E.current,V),p.current.controls&&(p.current.controls.enableRotate=!1,p.current.controls.enableZoom=!1,p.current.controls.enablePan=!1,p.current.controls.enableDamping=!1),x&&Object.keys(x).length>0&&(p.current.customAnimations=x),await p.current.showAvatar(W,U=>{if(U.lengthComputable){const z=Math.min(100,Math.round(U.loaded/U.total*100));c(z)}}),await new Promise(U=>{const z=()=>{p.current.lipsync&&Object.keys(p.current.lipsync).length>0?U():setTimeout(z,100)};z()}),p.current&&p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(a)}catch(U){console.warn("Error setting full body mode on initialization:",U)}p.current&&p.current.controls&&(p.current.controls.enableRotate=!1,p.current.controls.enableZoom=!1,p.current.controls.enablePan=!1,p.current.controls.enableDamping=!1,p.current.controls.update()),H(!1),J(!0),l(p.current);const T=()=>{document.visibilityState==="visible"?p.current?.start():p.current?.stop()};return document.addEventListener("visibilitychange",T),()=>{document.removeEventListener("visibilitychange",T)}}catch(S){console.error("Error initializing TalkingHead:",S),O(S.message||"Failed to initialize avatar"),H(!1),d(S)}},[M,t,e,i,n,s,o,a,r,h,u]);A.useEffect(()=>(Q(),()=>{p.current&&(p.current.stop(),p.current.dispose(),p.current=null)}),[Q]),A.useEffect(()=>{if(!E.current||!p.current)return;const S=new ResizeObserver(U=>{for(const z of U)p.current&&p.current.onResize&&p.current.onResize()});S.observe(E.current);const T=()=>{p.current&&p.current.onResize&&p.current.onResize()};return window.addEventListener("resize",T),()=>{S.disconnect(),window.removeEventListener("resize",T)}},[D]);const q=A.useCallback(async()=>{if(p.current&&p.current.audioCtx)try{(p.current.audioCtx.state==="suspended"||p.current.audioCtx.state==="interrupted")&&(await p.current.audioCtx.resume(),console.log("Audio context resumed"))}catch(S){console.warn("Failed to resume audio context:",S)}},[]),ae=A.useCallback(async(S,T={})=>{if(p.current&&D)try{await q();const U={...T,lipsyncLang:T.lipsyncLang||W.lipsyncLang||"en"};if(T.onSpeechEnd&&p.current){const z=p.current;let Y=null,oe=0,re=!1;const He=1200,Te=1e4;let Re=0;const ge=setInterval(()=>{Re++,z&&z.isSpeaking&&(z.audioPlaylist&&z.audioPlaylist.length>0||z.isAudioPlaying===!0)&&(re=!0,clearInterval(ge),Y=setInterval(ve,50));const le=!z.speechQueue||z.speechQueue.length===0;if(z&&!z.isSpeaking&&le&&(!z.audioPlaylist||z.audioPlaylist.length===0)&&(!z.isAudioPlaying||z.isAudioPlaying===!1)){clearInterval(ge);try{T.onSpeechEnd()}catch(he){console.error("Error in onSpeechEnd callback:",he)}return}if(Re*50>Te){if(clearInterval(ge),z&&z.isSpeaking)re=!0,Y=setInterval(ve,50);else if(z&&!z.isSpeaking&&le&&(!z.audioPlaylist||z.audioPlaylist.length===0)&&(!z.isAudioPlaying||z.isAudioPlaying===!1))try{T.onSpeechEnd()}catch(he){console.error("Error in onSpeechEnd callback:",he)}}},50),ve=()=>{if(oe++,oe>He){Y&&(clearInterval(Y),Y=null);try{T.onSpeechEnd()}catch(ye){console.error("Error in onSpeechEnd callback:",ye)}return}if(!re)return;const le=!z.speechQueue||z.speechQueue.length===0;z&&(!z.isSpeaking||z.isSpeaking===!1)&&(!z.audioPlaylist||z.audioPlaylist.length===0)&&(!z.isAudioPlaying||z.isAudioPlaying===!1)&&le&&(Y&&(clearInterval(Y),Y=null),setTimeout(()=>{try{T.onSpeechEnd()}catch(ye){console.error("Error in onSpeechEnd callback:",ye)}},50))}}p.current.lipsync&&Object.keys(p.current.lipsync).length>0?(p.current.setSlowdownRate&&p.current.setSlowdownRate(1.05),p.current.speakText(S,U)):setTimeout(async()=>{await q(),p.current&&p.current.lipsync&&(p.current.setSlowdownRate&&p.current.setSlowdownRate(1.05),p.current.speakText(S,U))},100)}catch(U){console.error("Error speaking text:",U),O(U.message||"Failed to speak text")}},[D,q,W.lipsyncLang]),pe=A.useCallback(()=>{p.current&&(p.current.stopSpeaking(),p.current.setSlowdownRate&&p.current.setSlowdownRate(1))},[]),_=A.useCallback(S=>{p.current&&p.current.setMood(S)},[]),b=A.useCallback(S=>{p.current&&p.current.setSlowdownRate&&p.current.setSlowdownRate(S)},[]),v=A.useCallback((S,T=!1)=>{if(p.current&&p.current.playAnimation){if(x&&x[S]&&(S=x[S]),p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(!0)}catch(z){console.warn("Error setting full body mode:",z)}if(S.includes("."))try{p.current.playAnimation(S,null,10,0,.01,T)}catch(z){console.warn(`Failed to play ${S}:`,z);try{p.current.setBodyMovement("idle")}catch(Y){console.warn("Fallback animation also failed:",Y)}}else{const z=[".fbx",".glb",".gltf"];let Y=!1;for(const oe of z)try{p.current.playAnimation(S+oe,null,10,0,.01,T),Y=!0;break}catch{}if(!Y){console.warn("Animation not found:",S);try{p.current.setBodyMovement("idle")}catch(oe){console.warn("Fallback animation also failed:",oe)}}}}},[x]),C=A.useCallback(()=>{p.current&&p.current.onResize&&p.current.onResize()},[]);return A.useImperativeHandle(I,()=>({speakText:ae,stopSpeaking:pe,resumeAudioContext:q,setMood:_,setTimingAdjustment:b,playAnimation:v,isReady:D,talkingHead:p.current,handleResize:C,setBodyMovement:S=>{if(p.current&&p.current.setShowFullAvatar&&p.current.setBodyMovement)try{p.current.setShowFullAvatar(!0),p.current.setBodyMovement(S)}catch(T){console.warn("Error setting body movement:",T)}},setMovementIntensity:S=>p.current?.setMovementIntensity(S),playRandomDance:()=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playRandomDance)try{p.current.setShowFullAvatar(!0),p.current.playRandomDance()}catch(S){console.warn("Error playing random dance:",S)}},playReaction:S=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playReaction)try{p.current.setShowFullAvatar(!0),p.current.playReaction(S)}catch(T){console.warn("Error playing reaction:",T)}},playCelebration:()=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playCelebration)try{p.current.setShowFullAvatar(!0),p.current.playCelebration()}catch(S){console.warn("Error playing celebration:",S)}},setShowFullAvatar:S=>{if(p.current&&p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(S)}catch(T){console.warn("Error setting showFullAvatar:",T)}},lockAvatarPosition:()=>{if(p.current&&p.current.lockAvatarPosition)try{p.current.lockAvatarPosition()}catch(S){console.warn("Error locking avatar position:",S)}},unlockAvatarPosition:()=>{if(p.current&&p.current.unlockAvatarPosition)try{p.current.unlockAvatarPosition()}catch(S){console.warn("Error unlocking avatar position:",S)}}})),te.jsxs("div",{className:`talking-head-avatar ${g}`,style:{width:"100%",height:"100%",position:"relative",...y},children:[te.jsx("div",{ref:E,className:"talking-head-viewer",style:{width:"100%",height:"100%",minHeight:"400px"}}),P&&te.jsx("div",{className:"loading-overlay",style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",color:"white",fontSize:"18px",zIndex:10},children:"Loading avatar..."}),R&&te.jsx("div",{className:"error-overlay",style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",color:"#ff6b6b",fontSize:"16px",textAlign:"center",zIndex:10,padding:"20px",borderRadius:"8px"},children:R})]})});xe.displayName="TalkingHeadAvatar";const ze=A.forwardRef(({text:M="Hello! I'm a talking avatar. How are you today?",onLoading:t=()=>{},onError:e=()=>{},onReady:i=()=>{},className:n="",style:s={},avatarConfig:o={}},r)=>{const h=A.useRef(null),a=A.useRef(null),[u,l]=A.useState(!0),[c,d]=A.useState(null),[g,y]=A.useState(!1),x=me(),I=o.ttsService||x.service,E=I==="browser"?{endpoint:"",apiKey:null,defaultVoice:"Google US English"}:{...x,apiKey:o.ttsApiKey!==void 0&&o.ttsApiKey!==null?o.ttsApiKey:x.apiKey,endpoint:I==="elevenlabs"&&o.ttsApiKey?"https://api.elevenlabs.io/v1/text-to-speech":x.endpoint},p={url:"/avatars/brunette.glb",body:"F",avatarMood:"neutral",ttsLang:I==="browser"?"en-US":"en",ttsVoice:o.ttsVoice||E.defaultVoice,lipsyncLang:"en",showFullAvatar:!0,bodyMovement:"idle",movementIntensity:.5,...o},P={ttsEndpoint:E.endpoint,ttsApikey:E.apiKey,ttsService:I,lipsyncModules:["en"],cameraView:"upper"},H=A.useCallback(async()=>{if(!(!h.current||a.current))try{if(l(!0),d(null),a.current=new we(h.current,P),await a.current.showAvatar(p,W=>{if(W.lengthComputable){const V=Math.min(100,Math.round(W.loaded/W.total*100));t(V)}}),a.current.morphs&&a.current.morphs.length>0){const W=a.current.morphs[0].morphTargetDictionary;console.log("Available morph targets:",Object.keys(W));const V=Object.keys(W).filter(Q=>Q.startsWith("viseme_"));console.log("Viseme morph targets found:",V),V.length===0&&(console.warn("No viseme morph targets found! Lip-sync will not work properly."),console.log("Expected viseme targets: viseme_aa, viseme_E, viseme_I, viseme_O, viseme_U, viseme_PP, viseme_SS, viseme_TH, viseme_DD, viseme_FF, viseme_kk, viseme_nn, viseme_RR, viseme_CH, viseme_sil"))}if(await new Promise(W=>{const V=()=>{a.current.lipsync&&Object.keys(a.current.lipsync).length>0?(console.log("Lip-sync modules loaded:",Object.keys(a.current.lipsync)),W()):(console.log("Waiting for lip-sync modules to load..."),setTimeout(V,100))};V()}),a.current&&a.current.setShowFullAvatar)try{a.current.setShowFullAvatar(!0),console.log("Avatar initialized in full body mode")}catch(W){console.warn("Error setting full body mode on initialization:",W)}l(!1),y(!0),i(a.current);const B=()=>{document.visibilityState==="visible"?a.current?.start():a.current?.stop()};return document.addEventListener("visibilitychange",B),()=>{document.removeEventListener("visibilitychange",B)}}catch(k){console.error("Error initializing TalkingHead:",k),d(k.message||"Failed to initialize avatar"),l(!1),e(k)}},[]);A.useEffect(()=>(H(),()=>{a.current&&(a.current.stop(),a.current.dispose(),a.current=null)}),[H]);const R=A.useCallback(k=>{if(a.current&&g)try{console.log("Speaking text:",k),console.log("Avatar config:",p),console.log("TalkingHead instance:",a.current),a.current.lipsync&&Object.keys(a.current.lipsync).length>0?(console.log("Lip-sync modules loaded:",Object.keys(a.current.lipsync)),a.current.setSlowdownRate&&(a.current.setSlowdownRate(1.05),console.log("Applied timing adjustment for better lip-sync")),a.current.speakText(k)):(console.warn("Lip-sync modules not ready, waiting..."),setTimeout(()=>{a.current&&a.current.lipsync?(console.log("Lip-sync now ready, speaking..."),a.current.setSlowdownRate&&(a.current.setSlowdownRate(1.05),console.log("Applied timing adjustment for better lip-sync")),a.current.speakText(k)):console.error("Lip-sync still not ready after waiting")},500))}catch(B){console.error("Error speaking text:",B),d(B.message||"Failed to speak text")}else console.warn("Avatar not ready for speaking. isReady:",g,"talkingHeadRef:",!!a.current)},[g,p]),O=A.useCallback(()=>{a.current&&(a.current.stopSpeaking(),a.current.setSlowdownRate&&(a.current.setSlowdownRate(1),console.log("Reset timing to normal")))},[]),D=A.useCallback(k=>{a.current&&a.current.setMood(k)},[]),J=A.useCallback(k=>{a.current&&a.current.setSlowdownRate&&(a.current.setSlowdownRate(k),console.log("Timing adjustment set to:",k))},[]),X=A.useCallback((k,B=!1)=>{if(a.current&&a.current.playAnimation){if(a.current.setShowFullAvatar)try{a.current.setShowFullAvatar(!0)}catch(V){console.warn("Error setting full body mode:",V)}if(k.includes("."))try{a.current.playAnimation(k,null,10,0,.01,B),console.log("Playing animation:",k)}catch(V){console.log(`Failed to play ${k}:`,V);try{a.current.setBodyMovement("idle"),console.log("Fallback to idle animation")}catch(Q){console.warn("Fallback animation also failed:",Q)}}else{const V=[".fbx",".glb",".gltf"];let Q=!1;for(const q of V)try{a.current.playAnimation(k+q,null,10,0,.01,B),console.log("Playing animation:",k+q),Q=!0;break}catch{console.log(`Failed to play ${k}${q}, trying next format...`)}if(!Q){console.warn("Animation system not available or animation not found:",k);try{a.current.setBodyMovement("idle"),console.log("Fallback to idle animation")}catch(q){console.warn("Fallback animation also failed:",q)}}}}else console.warn("Animation system not available or animation not found:",k)},[]);return A.useImperativeHandle(r,()=>({speakText:R,stopSpeaking:O,setMood:D,setTimingAdjustment:J,playAnimation:X,isReady:g,talkingHead:a.current,setBodyMovement:k=>{if(a.current&&a.current.setShowFullAvatar&&a.current.setBodyMovement)try{a.current.setShowFullAvatar(!0),a.current.setBodyMovement(k),console.log("Body movement set with full body mode:",k)}catch(B){console.warn("Error setting body movement:",B)}},setMovementIntensity:k=>a.current?.setMovementIntensity(k),playRandomDance:()=>{if(a.current&&a.current.setShowFullAvatar&&a.current.playRandomDance)try{a.current.setShowFullAvatar(!0),a.current.playRandomDance(),console.log("Random dance played with full body mode")}catch(k){console.warn("Error playing random dance:",k)}},playReaction:k=>{if(a.current&&a.current.setShowFullAvatar&&a.current.playReaction)try{a.current.setShowFullAvatar(!0),a.current.playReaction(k),console.log("Reaction played with full body mode:",k)}catch(B){console.warn("Error playing reaction:",B)}},playCelebration:()=>{if(a.current&&a.current.setShowFullAvatar&&a.current.playCelebration)try{a.current.setShowFullAvatar(!0),a.current.playCelebration(),console.log("Celebration played with full body mode")}catch(k){console.warn("Error playing celebration:",k)}},setShowFullAvatar:k=>{if(a.current&&a.current.setShowFullAvatar)try{a.current.setShowFullAvatar(k),console.log("Show full avatar set to:",k)}catch(B){console.warn("Error setting showFullAvatar:",B)}},lockAvatarPosition:()=>{if(a.current&&a.current.lockAvatarPosition)try{a.current.lockAvatarPosition()}catch(k){console.warn("Error locking avatar position:",k)}},unlockAvatarPosition:()=>{if(a.current&&a.current.unlockAvatarPosition)try{a.current.unlockAvatarPosition()}catch(k){console.warn("Error unlocking avatar position:",k)}}})),te.jsxs("div",{className:`talking-head-container ${n}`,style:s,children:[te.jsx("div",{ref:h,className:"talking-head-viewer",style:{width:"100%",height:"100%",minHeight:"400px"}}),u&&te.jsx("div",{className:"loading-overlay",style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",color:"white",fontSize:"18px",zIndex:10},children:"Loading avatar..."}),c&&te.jsx("div",{className:"error-overlay",style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",color:"#ff6b6b",fontSize:"16px",textAlign:"center",zIndex:10,padding:"20px",borderRadius:"8px"},children:c})]})});ze.displayName="TalkingHeadComponent";const Ce=A.forwardRef(({curriculumData:M=null,avatarConfig:t={},animations:e={},onLessonStart:i=()=>{},onLessonComplete:n=()=>{},onQuestionAnswer:s=()=>{},onCurriculumComplete:o=()=>{},onCustomAction:r=()=>{},autoStart:h=!1},a)=>{const u=A.useRef(null),l=A.useRef({currentModuleIndex:0,currentLessonIndex:0,currentQuestionIndex:0,isTeaching:!1,isQuestionMode:!1,lessonCompleted:!1,curriculumCompleted:!1,score:0,totalQuestions:0}),c=A.useRef({onLessonStart:i,onLessonComplete:n,onQuestionAnswer:s,onCurriculumComplete:o,onCustomAction:r}),d=A.useRef(null),g=A.useRef(null),y=A.useRef(null),x=A.useRef(null),I=A.useRef(null),E=A.useRef(null),p=A.useRef(null),P=A.useRef(M?.curriculum||{title:"Default Curriculum",description:"No curriculum data provided",language:"en",modules:[]}),H=A.useRef({avatarUrl:t.avatarUrl||"/avatars/brunette.glb",avatarBody:t.avatarBody||"F",mood:t.mood||"happy",ttsLang:t.ttsLang||"en",ttsService:t.ttsService||null,ttsVoice:t.ttsVoice||null,ttsApiKey:t.ttsApiKey||null,bodyMovement:t.bodyMovement||"gesturing",movementIntensity:t.movementIntensity||.7,showFullAvatar:t.showFullAvatar!==void 0?t.showFullAvatar:!0,animations:e,lipsyncLang:"en"});A.useEffect(()=>{c.current={onLessonStart:i,onLessonComplete:n,onQuestionAnswer:s,onCurriculumComplete:o,onCustomAction:r}},[i,n,s,o,r]),A.useEffect(()=>{P.current=M?.curriculum||{title:"Default Curriculum",description:"No curriculum data provided",language:"en",modules:[]},H.current={avatarUrl:t.avatarUrl||"/avatars/brunette.glb",avatarBody:t.avatarBody||"F",mood:t.mood||"happy",ttsLang:t.ttsLang||"en",ttsService:t.ttsService||null,ttsVoice:t.ttsVoice||null,ttsApiKey:t.ttsApiKey||null,bodyMovement:t.bodyMovement||"gesturing",movementIntensity:t.movementIntensity||.7,showFullAvatar:t.showFullAvatar!==void 0?t.showFullAvatar:!0,animations:e,lipsyncLang:"en"}},[M,t,e]);const R=A.useCallback(()=>(P.current||{modules:[]}).modules[l.current.currentModuleIndex]?.lessons[l.current.currentLessonIndex],[]),O=A.useCallback(()=>R()?.questions[l.current.currentQuestionIndex],[R]),D=A.useCallback((b,v)=>v.type==="multiple_choice"||v.type==="true_false"?b===v.answer:v.type==="code_test"&&typeof b=="object"&&b!==null?b.passed===!0:!1,[]),J=A.useCallback(()=>{l.current.lessonCompleted=!0,l.current.isQuestionMode=!1;const b=l.current.totalQuestions>0?Math.round(l.current.score/l.current.totalQuestions*100):100;let v="Congratulations! You've completed this lesson";if(l.current.totalQuestions>0?v+=` with a score of ${l.current.score} out of ${l.current.totalQuestions} (${b}%). `:v+="! ",b>=80?v+="Excellent work! You have a great understanding of this topic.":b>=60?v+="Good job! You understand most of the concepts.":v+="Keep practicing! You're making progress.",c.current.onLessonComplete({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,score:l.current.score,totalQuestions:l.current.totalQuestions,percentage:b}),c.current.onCustomAction({type:"lessonComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,score:l.current.score,totalQuestions:l.current.totalQuestions,percentage:b}),u.current){if(u.current.setMood("happy"),e.lessonComplete)try{u.current.playAnimation(e.lessonComplete,!0)}catch{u.current.playCelebration()}const C=P.current||{modules:[]},S=C.modules[l.current.currentModuleIndex],T=l.current.currentLessonIndex<(S?.lessons?.length||0)-1,U=l.current.currentModuleIndex<(C.modules?.length||0)-1,z=T||U,Y=H.current||{lipsyncLang:"en"};u.current.speakText(v,{lipsyncLang:Y.lipsyncLang,onSpeechEnd:()=>{c.current.onCustomAction({type:"lessonCompleteFeedbackDone",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,score:l.current.score,totalQuestions:l.current.totalQuestions,percentage:b,hasNextLesson:z})}})}},[e.lessonComplete]),X=A.useCallback(()=>{l.current.curriculumCompleted=!0;const b=P.current||{modules:[]};if(c.current.onCurriculumComplete({modules:b.modules.length,totalLessons:b.modules.reduce((v,C)=>v+C.lessons.length,0)}),u.current){if(u.current.setMood("celebrating"),e.curriculumComplete)try{u.current.playAnimation(e.curriculumComplete,!0)}catch{u.current.playCelebration()}const v=H.current||{lipsyncLang:"en"};u.current.speakText("Amazing! You've completed the entire curriculum! You're now ready to move on to more advanced topics. Well done!",{lipsyncLang:v.lipsyncLang})}},[e.curriculumComplete]),k=A.useCallback(()=>{const b=R();l.current.isQuestionMode=!0,l.current.currentQuestionIndex=0,l.current.totalQuestions=b?.questions?.length||0;const v=O();if(v&&c.current.onCustomAction({type:"questionStart",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,totalQuestions:l.current.totalQuestions,question:v}),u.current&&u.current.isReady&&v){if(u.current.setMood("curious"),e.questionStart)try{u.current.playAnimation(e.questionStart,!0)}catch(S){console.warn("Failed to play questionStart animation:",S)}const C=H.current||{lipsyncLang:"en"};v.type==="code_test"?u.current.speakText(`Let's test your coding skills! Here's your first challenge: ${v.question}`,{lipsyncLang:C.lipsyncLang}):v.type==="multiple_choice"?u.current.speakText(`Now let me ask you some questions. Here's the first one: ${v.question}`,{lipsyncLang:C.lipsyncLang}):v.type==="true_false"?u.current.speakText(`Let's start with some true or false questions. First question: ${v.question}`,{lipsyncLang:C.lipsyncLang}):u.current.speakText(`Now let me ask you some questions. Here's the first one: ${v.question}`,{lipsyncLang:C.lipsyncLang})}else if(u.current&&u.current.isReady){const C=H.current||{lipsyncLang:"en"};u.current.speakText("Now let me ask you some questions to test your understanding.",{lipsyncLang:C.lipsyncLang})}else setTimeout(()=>{E.current&&E.current()},100)},[e.questionStart,R,O]),B=A.useCallback(()=>{const b=R();if(l.current.currentQuestionIndex<(b?.questions?.length||0)-1){l.current.currentQuestionIndex+=1;const v=O();if(v&&c.current.onCustomAction({type:"nextQuestion",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,totalQuestions:l.current.totalQuestions,question:v}),u.current&&v){if(u.current.setMood("happy"),u.current.setBodyMovement("idle"),e.nextQuestion)try{u.current.playAnimation(e.nextQuestion,!0)}catch(S){console.warn("Failed to play nextQuestion animation:",S)}const C=H.current||{lipsyncLang:"en"};v.type==="code_test"?u.current.speakText(`Great! Now let's move on to your next coding challenge: ${v.question}`,{lipsyncLang:C.lipsyncLang}):v.type==="multiple_choice"?u.current.speakText(`Alright! Here's your next question: ${v.question}`,{lipsyncLang:C.lipsyncLang}):v.type==="true_false"?u.current.speakText(`Now let's try this one: ${v.question}`,{lipsyncLang:C.lipsyncLang}):u.current.speakText(`Here's the next question: ${v.question}`,{lipsyncLang:C.lipsyncLang})}}else c.current.onCustomAction({type:"allQuestionsComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,totalQuestions:l.current.totalQuestions,score:l.current.score})},[e.nextQuestion,R,O]),W=A.useCallback(()=>{const b=P.current||{modules:[]},v=b.modules[l.current.currentModuleIndex];l.current.currentLessonIndex<(v?.lessons?.length||0)-1?(l.current.currentLessonIndex+=1,l.current.currentQuestionIndex=0,l.current.lessonCompleted=!1,l.current.isQuestionMode=!1,l.current.isTeaching=!1,l.current.score=0,l.current.totalQuestions=0,c.current.onCustomAction({type:"lessonStart",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex}),c.current.onLessonStart({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:R()}),u.current&&(u.current.setMood("happy"),u.current.setBodyMovement("idle"))):l.current.currentModuleIndex<(b.modules?.length||0)-1?(l.current.currentModuleIndex+=1,l.current.currentLessonIndex=0,l.current.currentQuestionIndex=0,l.current.lessonCompleted=!1,l.current.isQuestionMode=!1,l.current.isTeaching=!1,l.current.score=0,l.current.totalQuestions=0,c.current.onCustomAction({type:"lessonStart",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex}),c.current.onLessonStart({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:R()}),u.current&&(u.current.setMood("happy"),u.current.setBodyMovement("idle"))):I.current&&I.current()},[]),V=A.useCallback(()=>{const b=R();let v=null;if(b?.avatar_script&&b?.body){const C=b.avatar_script.trim(),S=b.body.trim(),T=C.match(/[.!?]$/)?" ":". ";v=`${C}${T}${S}`}else v=b?.avatar_script||b?.body||null;if(u.current&&u.current.isReady&&v){l.current.isTeaching=!0,l.current.isQuestionMode=!1,u.current.setMood("happy");let C=!1;if(e.teaching)try{u.current.playAnimation(e.teaching,!0),C=!0}catch(T){console.warn("Failed to play teaching animation:",T)}C||u.current.setBodyMovement("gesturing");const S=H.current||{lipsyncLang:"en"};c.current.onLessonStart({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:b}),c.current.onCustomAction({type:"teachingStart",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:b}),u.current.speakText(v,{lipsyncLang:S.lipsyncLang,onSpeechEnd:()=>{l.current.isTeaching=!1,c.current.onCustomAction({type:"teachingComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:b,hasQuestions:b.questions&&b.questions.length>0})}})}},[e.teaching,R]),Q=A.useCallback(b=>{const v=O(),C=D(b,v);if(C&&(l.current.score+=1),c.current.onQuestionAnswer({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,answer:b,isCorrect:C,question:v}),u.current)if(C){if(u.current.setMood("happy"),e.correct)try{u.current.playReaction("happy")}catch{u.current.setBodyMovement("happy")}u.current.setBodyMovement("gesturing");const S=v.type==="code_test"?`Great job! Your code passed all the tests! ${v.explanation||""}`:`Excellent! That's correct! ${v.explanation||""}`,T=H.current||{lipsyncLang:"en"};u.current.speakText(S,{lipsyncLang:T.lipsyncLang,onSpeechEnd:()=>{c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,isCorrect:!0,hasNextQuestion:l.current.currentQuestionIndex<(R()?.questions?.length||0)-1})}})}else{if(u.current.setMood("sad"),e.incorrect)try{u.current.playAnimation(e.incorrect,!0)}catch{u.current.setBodyMovement("idle")}u.current.setBodyMovement("gesturing");const S=v.type==="code_test"?`Your code didn't pass all the tests. ${v.explanation||"Try again!"}`:`Not quite right, but don't worry! ${v.explanation||""} Let's move on to the next question.`,T=H.current||{lipsyncLang:"en"};u.current.speakText(S,{lipsyncLang:T.lipsyncLang,onSpeechEnd:()=>{c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,isCorrect:!1,hasNextQuestion:l.current.currentQuestionIndex<(R()?.questions?.length||0)-1})}})}else c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,isCorrect:C,hasNextQuestion:l.current.currentQuestionIndex<(R()?.questions?.length||0)-1,avatarNotReady:!0})},[e.correct,e.incorrect,O,R,D]),q=A.useCallback(b=>{const v=O();if(!b||typeof b!="object"){console.error("Invalid code test result format. Expected object with {passed: boolean, ...}");return}if(v?.type!=="code_test"){console.warn("Current question is not a code test. Use handleAnswerSelect for other question types.");return}const C={passed:b.passed===!0,results:b.results||[],output:b.output||"",error:b.error||null,executionTime:b.executionTime||null,testCount:b.testCount||0,passedCount:b.passedCount||0,failedCount:b.failedCount||0};c.current.onCustomAction({type:"codeTestSubmitted",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,testResult:C,question:v}),p.current&&p.current(C)},[O,D]),ae=A.useCallback(()=>{l.current.currentModuleIndex=0,l.current.currentLessonIndex=0,l.current.currentQuestionIndex=0,l.current.isTeaching=!1,l.current.isQuestionMode=!1,l.current.lessonCompleted=!1,l.current.curriculumCompleted=!1,l.current.score=0,l.current.totalQuestions=0},[]),pe=A.useCallback(b=>{console.log("Avatar is ready!",b);const v=R(),C=v?.avatar_script||v?.body;h&&C&&setTimeout(()=>{d.current&&d.current()},10)},[h,R]);A.useLayoutEffect(()=>{d.current=V,g.current=W,y.current=J,x.current=B,I.current=X,E.current=k,p.current=Q}),A.useImperativeHandle(a,()=>({startTeaching:V,startQuestions:k,handleAnswerSelect:Q,handleCodeTestResult:q,nextQuestion:B,nextLesson:W,completeLesson:J,completeCurriculum:X,resetCurriculum:ae,getState:()=>({...l.current}),getCurrentQuestion:()=>O(),getCurrentLesson:()=>R(),getAvatarRef:()=>u.current,speakText:async(b,v={})=>{await u.current?.resumeAudioContext?.();const C=H.current||{lipsyncLang:"en"};u.current?.speakText(b,{...v,lipsyncLang:v.lipsyncLang||C.lipsyncLang})},resumeAudioContext:async()=>{if(u.current?.resumeAudioContext)return await u.current.resumeAudioContext();const b=u.current?.talkingHead;if(b?.audioCtx){const v=b.audioCtx;if(v.state==="suspended"||v.state==="interrupted")try{await v.resume(),console.log("Audio context resumed via talkingHead")}catch(C){console.warn("Failed to resume audio context:",C)}}else console.warn("Audio context not available yet")},stopSpeaking:()=>u.current?.stopSpeaking(),setMood:b=>u.current?.setMood(b),playAnimation:(b,v)=>u.current?.playAnimation(b,v),setBodyMovement:b=>u.current?.setBodyMovement(b),setMovementIntensity:b=>u.current?.setMovementIntensity(b),playRandomDance:()=>u.current?.playRandomDance(),playReaction:b=>u.current?.playReaction(b),playCelebration:()=>u.current?.playCelebration(),setShowFullAvatar:b=>u.current?.setShowFullAvatar(b),setTimingAdjustment:b=>u.current?.setTimingAdjustment(b),lockAvatarPosition:()=>u.current?.lockAvatarPosition(),unlockAvatarPosition:()=>u.current?.unlockAvatarPosition(),triggerCustomAction:(b,v)=>{c.current.onCustomAction({type:b,...v,state:{...l.current}})},handleResize:()=>u.current?.handleResize(),isAvatarReady:()=>u.current?.isReady||!1}),[V,k,Q,q,B,W,J,X,ae,O,R]);const _=H.current||{avatarUrl:"/avatars/brunette.glb",avatarBody:"F",mood:"happy",ttsLang:"en",ttsService:null,ttsVoice:null,ttsApiKey:null,bodyMovement:"gesturing",movementIntensity:.7,showFullAvatar:!0,animations:e};return te.jsx("div",{style:{width:"100%",height:"100%"},children:te.jsx(xe,{ref:u,avatarUrl:_.avatarUrl,avatarBody:_.avatarBody,mood:_.mood,ttsLang:_.ttsLang,ttsService:_.ttsService,ttsVoice:_.ttsVoice,ttsApiKey:_.ttsApiKey,bodyMovement:_.bodyMovement,movementIntensity:_.movementIntensity,showFullAvatar:_.showFullAvatar,cameraView:"upper",animations:_.animations,onReady:pe,onLoading:()=>{},onError:b=>{console.error("Avatar error:",b)}})})});Ce.displayName="CurriculumLearning";const be={dance:{name:"dance",type:"code-based",variations:["dancing","dancing2","dancing3"],loop:!0,duration:1e4,description:"Celebration dance animation with multiple variations"},happy:{name:"happy",type:"code-based",loop:!0,duration:5e3,description:"Happy, upbeat body movement"},surprised:{name:"surprised",type:"code-based",loop:!1,duration:3e3,description:"Surprised reaction with quick movements"},thinking:{name:"thinking",type:"code-based",loop:!0,duration:8e3,description:"Thoughtful, contemplative movement"},nodding:{name:"nodding",type:"code-based",loop:!1,duration:2e3,description:"Nodding agreement gesture"},shaking:{name:"shaking",type:"code-based",loop:!1,duration:2e3,description:"Shaking head disagreement gesture"},celebration:{name:"celebration",type:"code-based",loop:!1,duration:3e3,description:"Celebration animation for achievements"},energetic:{name:"energetic",type:"code-based",loop:!0,duration:6e3,description:"High-energy, enthusiastic movement"},swaying:{name:"swaying",type:"code-based",loop:!0,duration:8e3,description:"Gentle swaying motion"},bouncing:{name:"bouncing",type:"code-based",loop:!0,duration:4e3,description:"Bouncy, playful movement"},gesturing:{name:"gesturing",type:"code-based",loop:!0,duration:5e3,description:"Teaching gesture animation"},walking:{name:"walking",type:"code-based",loop:!0,duration:6e3,description:"Walking motion"},prancing:{name:"prancing",type:"code-based",loop:!0,duration:4e3,description:"Playful prancing movement"},excited:{name:"excited",type:"code-based",loop:!0,duration:5e3,description:"Excited, energetic movement"}},nt=M=>be[M]||null,ot=M=>be.hasOwnProperty(M);exports.CurriculumLearning=Ce;exports.TalkingHeadAvatar=xe;exports.TalkingHeadComponent=ze;exports.animations=be;exports.getActiveTTSConfig=me;exports.getAnimation=nt;exports.getVoiceOptions=it;exports.hasAnimation=ot;
7
+ `,s=await fetch(this.opt.ttsEndpoint,{method:"POST",headers:{"Ocp-Apim-Subscription-Key":this.opt.ttsApikey,"Content-Type":"application/ssml+xml","X-Microsoft-OutputFormat":"audio-16khz-128kbitrate-mono-mp3"},body:n});if(!s.ok)throw new Error(`Azure TTS error: ${s.status} ${s.statusText}`);const o=await s.arrayBuffer(),r=await this.audioCtx.decodeAudioData(o);console.log("Analyzing audio for precise lip-sync...");const h=await this.audioAnalyzer.analyzeAudio(r,e);console.log("Azure TTS Audio Analysis:",{text:e,audioDuration:r.duration,visemeCount:h.visemes.length,wordCount:h.words.length,features:{onsets:h.features.onsets.length,boundaries:h.features.phonemeBoundaries.length}});const a=[];for(let l=0;l<h.visemes.length;l++){const c=h.visemes[l],d=c.startTime*1e3,g=c.duration*1e3,y=c.intensity;a.push({template:{name:"viseme"},ts:[d-Math.min(60,2*g/3),d+Math.min(25,g/2),d+g+Math.min(60,g/2)],vs:{["viseme_"+c.viseme]:[null,y,0]}})}const u=[...t.anim,...a];this.audioPlaylist.push({anim:u,audio:r}),this.onSubtitles=t.onSubtitles||null,this.resetLips(),t.mood&&this.setMood(t.mood),this.playAudio()}async synthesizeWithExternalTTS(t){let e="<speak>";t.text.forEach((o,r)=>{r>0&&(e+=" <mark name='"+o.mark+"'/>"),e+=o.word.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll('"',"&quot;").replaceAll("'","&apos;").replace(new RegExp("^\\p{Dash_Punctuation}$","ug"),'<break time="750ms"/>')}),e+="</speak>";const i={method:"POST",headers:{"Content-Type":"application/json; charset=utf-8"},body:JSON.stringify({input:{ssml:e},voice:{languageCode:t.lang||this.avatar.ttsLang||this.opt.ttsLang,name:t.voice||this.avatar.ttsVoice||this.opt.ttsVoice},audioConfig:{audioEncoding:this.ttsAudioEncoding,speakingRate:(t.rate||this.avatar.ttsRate||this.opt.ttsRate)+this.mood.speech.deltaRate,pitch:(t.pitch||this.avatar.ttsPitch||this.opt.ttsPitch)+this.mood.speech.deltaPitch,volumeGainDb:(t.volume||this.avatar.ttsVolume||this.opt.ttsVolume)+this.mood.speech.deltaVolume},enableTimePointing:[1]})};this.opt.jwtGet&&typeof this.opt.jwtGet=="function"&&(i.headers.Authorization="Bearer "+await this.opt.jwtGet());const n=await fetch(this.opt.ttsEndpoint+(this.opt.ttsApikey?"?key="+this.opt.ttsApikey:""),i),s=await n.json();if(n.status===200&&s&&s.audioContent){const o=this.b64ToArrayBuffer(s.audioContent),r=await this.audioCtx.decodeAudioData(o);this.speakWithHands();const h=[0];let a=0;t.text.forEach((c,d)=>{if(d>0){let g=h[h.length-1];s.timepoints[a]&&(g=s.timepoints[a].timeSeconds*1e3,s.timepoints[a].markName===""+c.mark&&a++),h.push(g)}});const u=[{mark:0,time:0}];h.forEach((c,d)=>{if(d>0){let g=c-h[d-1];u[d-1].duration=g,u.push({mark:d,time:c})}});let l=1e3*r.duration;l>this.opt.ttsTrimEnd&&(l=l-this.opt.ttsTrimEnd),u[u.length-1].duration=l-u[u.length-1].time,t.anim.forEach(c=>{const d=u[c.mark];if(d)for(let g=0;g<c.ts.length;g++)c.ts[g]=d.time+c.ts[g]*d.duration+this.opt.ttsTrimStart}),this.audioPlaylist.push({anim:t.anim,audio:r}),this.onSubtitles=t.onSubtitles||null,this.resetLips(),t.mood&&this.setMood(t.mood),this.playAudio()}else this.startSpeaking(!0)}async startSpeaking(t=!1){if(!(!this.armature||this.isSpeaking&&!t))if(this.stateName="speaking",this.isSpeaking=!0,this.speechQueue.length){let e=this.speechQueue.shift();if(e.emoji){this.lookAtCamera(500);let i=e.emoji.dt.reduce((n,s)=>n+s,0);this.animQueue.push(this.animFactory(e.emoji)),setTimeout(this.startSpeaking.bind(this),i,!0)}else if(e.break)setTimeout(this.startSpeaking.bind(this),e.break,!0);else if(e.audio)e.isRaw||(this.lookAtCamera(500),this.speakWithHands(),this.resetLips()),this.audioPlaylist.push({anim:e.anim,audio:e.audio,isRaw:e.isRaw}),this.onSubtitles=e.onSubtitles||null,e.mood&&this.setMood(e.mood),this.playAudio();else if(e.text){this.lookAtCamera(500);try{!this.opt.ttsEndpoint||this.opt.ttsEndpoint===""?await this.synthesizeWithBrowserTTS(e):this.opt.ttsService==="elevenlabs"?await this.synthesizeWithElevenLabsTTS(e):this.opt.ttsService==="deepgram"?await this.synthesizeWithDeepgramTTS(e):this.opt.ttsService==="azure"?await this.synthesizeWithAzureTTS(e):await this.synthesizeWithExternalTTS(e)}catch(i){console.error("Error:",i),this.startSpeaking(!0)}}else e.anim?(this.onSubtitles=e.onSubtitles||null,this.resetLips(),e.mood&&this.setMood(e.mood),e.anim.forEach((i,n)=>{for(let s=0;s<i.ts.length;s++)i.ts[s]=this.animClock+10*n;this.animQueue.push(i)}),setTimeout(this.startSpeaking.bind(this),10*e.anim.length,!0)):e.marker?(typeof e.marker=="function"&&e.marker(),this.startSpeaking(!0)):this.startSpeaking(!0)}else this.stateName="idle",this.isSpeaking=!1}pauseSpeaking(){try{this.audioSpeechSource.stop()}catch{}this.audioPlaylist.length=0,this.stateName="idle",this.isSpeaking=!1,this.isAudioPlaying=!1,this.animQueue=this.animQueue.filter(t=>t.template.name!=="viseme"&&t.template.name!=="subtitles"&&t.template.name!=="blendshapes"),this.armature&&(this.resetLips(),this.render())}stopSpeaking(){try{this.audioSpeechSource.stop()}catch{}this.audioPlaylist.length=0,this.speechQueue.length=0,this.animQueue=this.animQueue.filter(t=>t.template.name!=="viseme"&&t.template.name!=="subtitles"&&t.template.name!=="blendshapes"),this.stateName="idle",this.isSpeaking=!1,this.isAudioPlaying=!1,this.armature&&(this.resetLips(),this.render())}async streamStart(t={},e=null,i=null,n=null,s=null){if(this.stopSpeaking(),this.isStreaming=!0,t.waitForAudioChunks!==void 0&&(this.streamWaitForAudioChunks=t.waitForAudioChunks),this.streamWaitForAudioChunks||(this.streamAudioStartTime=this.animClock),this.streamLipsyncQueue=[],this.streamLipsyncType=t.lipsyncType||this.streamLipsyncType||"visemes",this.streamLipsyncLang=t.lipsyncLang||this.streamLipsyncLang||this.avatar.lipsyncLang||this.opt.lipsyncLang,this.onAudioStart=e,this.onAudioEnd=i,this.onMetrics=s,t.sampleRate!==void 0){const r=t.sampleRate;typeof r=="number"&&r>=8e3&&r<=96e3?r!==this.audioCtx.sampleRate&&this.initAudioGraph(r):console.warn("Invalid sampleRate provided. It must be a number between 8000 and 96000 Hz.")}if(t.gain!==void 0&&(this.audioStreamGainNode.gain.value=t.gain),!this.streamWorkletNode||!this.streamWorkletNode.port||this.streamWorkletNode.numberOfOutputs===0||this.streamWorkletNode.context!==this.audioCtx){if(this.streamWorkletNode)try{this.streamWorkletNode.disconnect(),this.streamWorkletNode=null}catch{}if(!this.workletLoaded)try{const r=this.audioCtx.audioWorklet.addModule(et.href),h=new Promise((a,u)=>setTimeout(()=>u(new Error("Worklet loading timed out")),5e3));await Promise.race([r,h]),this.workletLoaded=!0}catch(r){throw console.error("Failed to load audio worklet:",r),new Error("Failed to initialize streaming speech")}this.streamWorkletNode=new AudioWorkletNode(this.audioCtx,"playback-worklet",{processorOptions:{sampleRate:this.audioCtx.sampleRate,metrics:t.metrics||{enabled:!1}}}),this.streamWorkletNode.connect(this.audioStreamGainNode),this.streamWorkletNode.connect(this.audioAnalyzerNode),this.streamWorkletNode.port.onmessage=r=>{if(r.data.type==="playback-started"&&(this.isSpeaking=!0,this.stateName="speaking",this.streamWaitForAudioChunks&&(this.streamAudioStartTime=this.animClock),this._processStreamLipsyncQueue(),this.speakWithHands(),this.onAudioStart))try{this.onAudioStart?.()}catch(h){console.error(h)}if(r.data.type==="playback-ended"&&(this._streamPause(),this.onAudioEnd))try{this.onAudioEnd()}catch{}if(this.onMetrics&&r.data.type==="metrics")try{this.onMetrics(r.data)}catch{}}}if(t.metrics)try{this.streamWorkletNode.port.postMessage({type:"config-metrics",data:t.metrics})}catch{}if(this.resetLips(),this.lookAtCamera(500),t.mood&&this.setMood(t.mood),this.onSubtitles=n||null,this.audioCtx.state==="suspended"||this.audioCtx.state==="interrupted"){const r=this.audioCtx.resume(),h=new Promise((a,u)=>setTimeout(()=>u("p2"),1e3));try{await Promise.race([r,h])}catch{console.warn("Can't play audio. Web Audio API suspended. This is often due to calling some speak method before the first user action, which is typically prevented by the browser.");return}}}streamNotifyEnd(){!this.isStreaming||!this.streamWorkletNode||this.streamWorkletNode.port.postMessage({type:"no-more-data"})}streamInterrupt(){if(!this.isStreaming)return;const t=this.isSpeaking;if(this.streamWorkletNode)try{this.streamWorkletNode.port.postMessage({type:"stop"})}catch{}if(this._streamPause(!0),t&&this.onAudioEnd)try{this.onAudioEnd()}catch{}}streamStop(){if(this.isStreaming){if(this.streamInterrupt(),this.streamWorkletNode){try{this.streamWorkletNode.disconnect()}catch{}this.streamWorkletNode=null}this.isStreaming=!1}}_streamPause(t=!1){this.isSpeaking=!1,this.stateName="idle",t&&(this.streamWaitForAudioChunks&&(this.streamAudioStartTime=null),this.streamLipsyncQueue=[],this.animQueue=this.animQueue.filter(e=>e.template.name!=="viseme"&&e.template.name!=="subtitles"&&e.template.name!=="blendshapes"),this.armature&&(this.resetLips(),this.render()))}_processStreamLipsyncQueue(){if(this.isStreaming)for(;this.streamLipsyncQueue.length>0;){const t=this.streamLipsyncQueue.shift();this._processLipsyncData(t,this.streamAudioStartTime)}}_processLipsyncData(t,e){if(this.isStreaming){if(t.visemes&&this.streamLipsyncType=="visemes")for(let i=0;i<t.visemes.length;i++){const n=t.visemes[i],s=e+t.vtimes[i],o=t.vdurations[i],r={template:{name:"viseme"},ts:[s-2*o/3,s+o/2,s+o+o/2],vs:{["viseme_"+n]:[null,n==="PP"||n==="FF"?.9:.6,0]}};this.animQueue.push(r)}if(t.words&&(this.onSubtitles||this.streamLipsyncType=="words"))for(let i=0;i<t.words.length;i++){const n=t.words[i],s=t.wtimes[i];let o=t.wdurations[i];if(n.length&&(this.onSubtitles&&this.animQueue.push({template:{name:"subtitles"},ts:[e+s],vs:{subtitles:[" "+n]}}),this.streamLipsyncType=="words")){const r=this.streamLipsyncLang||this.avatar.lipsyncLang||this.opt.lipsyncLang,h=this.lipsyncPreProcessText(n,r),a=this.lipsyncWordsToVisemes(h,r);if(a&&a.visemes&&a.visemes.length){const u=a.times[a.visemes.length-1]+a.durations[a.visemes.length-1],l=Math.min(o,Math.max(0,o-a.visemes.length*150));let c=.6+this.convertRange(l,[0,o],[0,.4]);if(o=Math.min(o,a.visemes.length*200),u>0)for(let d=0;d<a.visemes.length;d++){const g=e+s+a.times[d]/u*o,y=a.durations[d]/u*o;this.animQueue.push({template:{name:"viseme"},ts:[g-Math.min(60,2*y/3),g+Math.min(25,y/2),g+y+Math.min(60,y/2)],vs:{["viseme_"+a.visemes[d]]:[null,a.visemes[d]==="PP"||a.visemes[d]==="FF"?.9:c,0]}})}}}}if(t.anims&&this.streamLipsyncType=="blendshapes")for(let i=0;i<t.anims.length;i++){let n=t.anims[i];n.delay+=e;let s=this.animFactory(n,!1,1,1,!0);this.animQueue.push(s)}}}streamAudio(t){if(!(!this.isStreaming||!this.streamWorkletNode)){if(this.isSpeaking||(this.streamLipsyncQueue=[],this.streamAudioStartTime=null),this.isSpeaking=!0,this.stateName="speaking",t.audio!==void 0){const e={type:"audioData",data:null};if(t.audio instanceof ArrayBuffer)e.data=t.audio,this.streamWorkletNode.port.postMessage(e,[e.data]);else if(t.audio instanceof Int16Array||t.audio instanceof Uint8Array){const i=t.audio.buffer.slice(t.audio.byteOffset,t.audio.byteOffset+t.audio.byteLength);e.data=i,this.streamWorkletNode.port.postMessage(e,[e.data])}else if(t.audio instanceof Float32Array){const i=new Int16Array(t.audio.length);for(let n=0;n<t.audio.length;n++){let s=Math.max(-1,Math.min(1,t.audio[n]));i[n]=s<0?s*32768:s*32767}e.data=i.buffer,this.streamWorkletNode.port.postMessage(e,[e.data])}else console.error("r.audio is not a supported type. Must be ArrayBuffer, Int16Array, Uint8Array, or Float32Array:",t.audio)}if(t.visemes||t.anims||t.words){if(this.streamWaitForAudioChunks&&!this.streamAudioStartTime){this.streamLipsyncQueue.length>=200&&this.streamLipsyncQueue.shift(),this.streamLipsyncQueue.push(t);return}else!this.streamWaitForAudioChunks&&!this.streamAudioStartTime&&(this.streamAudioStartTime=this.animClock);this._processLipsyncData(t,this.streamAudioStartTime)}}}makeEyeContact(t){this.animQueue.push(this.animFactory({name:"eyecontact",dt:[0,t],vs:{eyeContact:[1]}}))}lookAhead(t){if(t){let e=(Math.random()-.5)/4,i=(Math.random()-.5)/4,n=this.animQueue.findIndex(o=>o.template.name==="lookat");n!==-1&&this.animQueue.splice(n,1);const s={name:"lookat",dt:[750,t],vs:{bodyRotateX:[e],bodyRotateY:[i],eyesRotateX:[-3*e+.1],eyesRotateY:[-5*i],browInnerUp:[[0,.7]],mouthLeft:[[0,.7]],mouthRight:[[0,.7]],eyeContact:[0],headMove:[0]}};this.animQueue.push(this.animFactory(s))}}lookAtCamera(t){let e;if(this.speakTo&&(e=new f.Vector3,this.speakTo.objectLeftEye?.isObject3D?(this.speakTo.armature.objectHead,this.speakTo.objectLeftEye.updateMatrixWorld(!0),this.speakTo.objectRightEye.updateMatrixWorld(!0),ie.setFromMatrixPosition(this.speakTo.objectLeftEye.matrixWorld),ne.setFromMatrixPosition(this.speakTo.objectRightEye.matrixWorld),e.addVectors(ie,ne).divideScalar(2)):this.speakTo.isObject3D?this.speakTo.getWorldPosition(e):this.speakTo.isVector3?e.set(this.speakTo):this.speakTo.x&&this.speakTo.y&&this.speakTo.z&&e.set(this.speakTo.x,this.speakTo.y,this.speakTo.z)),!e){if(this.avatar.hasOwnProperty("avatarIgnoreCamera")){if(this.avatar.avatarIgnoreCamera){this.lookAhead(t);return}}else if(this.opt.avatarIgnoreCamera){this.lookAhead(t);return}this.lookAt(null,null,t);return}this.objectLeftEye.updateMatrixWorld(!0),this.objectRightEye.updateMatrixWorld(!0),ie.setFromMatrixPosition(this.objectLeftEye.matrixWorld),ne.setFromMatrixPosition(this.objectRightEye.matrixWorld),ie.add(ne).divideScalar(2),N.copy(this.armature.quaternion),N.multiply(this.poseTarget.props["Hips.quaternion"]),N.multiply(this.poseTarget.props["Spine.quaternion"]),N.multiply(this.poseTarget.props["Spine1.quaternion"]),N.multiply(this.poseTarget.props["Spine2.quaternion"]),N.multiply(this.poseTarget.props["Neck.quaternion"]),N.multiply(this.poseTarget.props["Head.quaternion"]);const i=new f.Vector3().subVectors(e,ie).normalize(),n=Math.atan2(i.x,i.z),s=Math.asin(-i.y);F.set(s,n,0,"YXZ");const r=new f.Quaternion().setFromEuler(F),h=new f.Quaternion().copy(r).multiply(N.clone().invert());F.setFromQuaternion(h,"YXZ");let a=F.x/(40/24)+.2,u=F.y/(9/4),l=Math.min(.6,Math.max(-.3,a)),c=Math.min(.8,Math.max(-.8,u)),d=(Math.random()-.5)/4,g=(Math.random()-.5)/4;if(t){let y=this.animQueue.findIndex(I=>I.template.name==="lookat");y!==-1&&this.animQueue.splice(y,1);const x={name:"lookat",dt:[750,t],vs:{bodyRotateX:[l+d],bodyRotateY:[c+g],eyesRotateX:[-3*d+.1],eyesRotateY:[-5*g],browInnerUp:[[0,.7]],mouthLeft:[[0,.7]],mouthRight:[[0,.7]],eyeContact:[0],headMove:[0]}};this.animQueue.push(this.animFactory(x))}}lookAt(t,e,i){if(!this.camera)return;const n=this.nodeAvatar.getBoundingClientRect();this.objectLeftEye.updateMatrixWorld(!0),this.objectRightEye.updateMatrixWorld(!0);const s=new f.Vector3().setFromMatrixPosition(this.objectLeftEye.matrixWorld),o=new f.Vector3().setFromMatrixPosition(this.objectRightEye.matrixWorld),r=new f.Vector3().addVectors(s,o).divideScalar(2);r.project(this.camera);let h=(r.x+1)/2*n.width+n.left,a=-(r.y-1)/2*n.height+n.top;t===null&&(t=h),e===null&&(e=a),N.copy(this.armature.quaternion),N.multiply(this.poseTarget.props["Hips.quaternion"]),N.multiply(this.poseTarget.props["Spine.quaternion"]),N.multiply(this.poseTarget.props["Spine1.quaternion"]),N.multiply(this.poseTarget.props["Spine2.quaternion"]),N.multiply(this.poseTarget.props["Neck.quaternion"]),N.multiply(this.poseTarget.props["Head.quaternion"]),F.setFromQuaternion(N);let u=F.x/(40/24),l=F.y/(9/4),c=Math.min(.4,Math.max(-.4,this.camera.rotation.x)),d=Math.min(.4,Math.max(-.4,this.camera.rotation.y)),g=Math.max(window.innerWidth-h,h),y=Math.max(window.innerHeight-a,a),x=this.convertRange(e,[a-y,a+y],[-.3,.6])-u+c,I=this.convertRange(t,[h-g,h+g],[-.8,.8])-l+d;x=Math.min(.6,Math.max(-.3,x)),I=Math.min(.8,Math.max(-.8,I));let E=(Math.random()-.5)/4,p=(Math.random()-.5)/4;if(i){let P=this.animQueue.findIndex(R=>R.template.name==="lookat");P!==-1&&this.animQueue.splice(P,1);const H={name:"lookat",dt:[750,i],vs:{bodyRotateX:[x+E],bodyRotateY:[I+p],eyesRotateX:[-3*E+.1],eyesRotateY:[-5*p],browInnerUp:[[0,.7]],mouthLeft:[[0,.7]],mouthRight:[[0,.7]],eyeContact:[0],headMove:[0]}};this.animQueue.push(this.animFactory(H))}}touchAt(t,e){if(!this.camera)return;const i=this.nodeAvatar.getBoundingClientRect(),n=new f.Vector2((t-i.left)/i.width*2-1,-((e-i.top)/i.height)*2+1),s=new f.Raycaster;s.setFromCamera(n,this.camera);const o=s.intersectObject(this.armature);if(o.length>0){const r=o[0].point,h=new f.Vector3,a=new f.Vector3;this.objectLeftArm.getWorldPosition(h),this.objectRightArm.getWorldPosition(a);const u=h.distanceToSquared(r),l=a.distanceToSquared(r);u<l?(this.ikSolve({iterations:20,root:"LeftShoulder",effector:"LeftHandMiddle1",links:[{link:"LeftHand",minx:-.5,maxx:.5,miny:-1,maxy:1,minz:-.5,maxz:.5,maxAngle:.1},{link:"LeftForeArm",minx:-.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-.5,maxz:3,maxAngle:.2},{link:"LeftArm",minx:-1.5,maxx:1.5,miny:0,maxy:0,minz:-1,maxz:3}]},r,!1,1e3),this.setValue("handFistLeft",0)):(this.ikSolve({iterations:20,root:"RightShoulder",effector:"RightHandMiddle1",links:[{link:"RightHand",minx:-.5,maxx:.5,miny:-1,maxy:1,minz:-.5,maxz:.5,maxAngle:.1},{link:"RightForeArm",minx:-.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-3,maxz:.5,maxAngle:.2},{link:"RightArm",minx:-1.5,maxx:1.5,miny:0,maxy:0,minz:-1,maxz:3}]},r,!1,1e3),this.setValue("handFistRight",0))}else["LeftArm","LeftForeArm","LeftHand","RightArm","RightForeArm","RightHand"].forEach(r=>{let h=r+".quaternion";this.poseTarget.props[h].copy(this.getPoseTemplateProp(h)),this.poseTarget.props[h].t=this.animClock,this.poseTarget.props[h].d=1e3});return o.length>0}speakWithHands(t=0,e=.5){if(this.mixer||this.gesture||!this.poseTarget.template.standing||this.poseTarget.template.bend||Math.random()>e)return;this.ikSolve({root:"LeftShoulder",effector:"LeftHandMiddle1",links:[{link:"LeftHand",minx:-.5,maxx:.5,miny:-1,maxy:1,minz:-.5,maxz:.5},{link:"LeftForeArm",minx:-.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-.5,maxz:3},{link:"LeftArm",minx:-1.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-1,maxz:3}]},new f.Vector3(this.gaussianRandom(0,.5),this.gaussianRandom(-.8,-.2),this.gaussianRandom(0,.5)),!0),this.ikSolve({root:"RightShoulder",effector:"RightHandMiddle1",links:[{link:"RightHand",minx:-.5,maxx:.5,miny:-1,maxy:1,minz:-.5,maxz:.5},{link:"RightForeArm",minx:-.5,maxx:1.5,miny:-1.5,maxy:1.5,minz:-3,maxz:.5},{link:"RightArm"}]},new f.Vector3(this.gaussianRandom(-.5,0),this.gaussianRandom(-.8,-.2),this.gaussianRandom(0,.5)),!0);const i=[],n=[];i.push(100+Math.round(Math.random()*500)),n.push({duration:1e3,props:{"LeftHand.quaternion":new f.Quaternion().setFromEuler(new f.Euler(0,-1-Math.random(),0)),"RightHand.quaternion":new f.Quaternion().setFromEuler(new f.Euler(0,1+Math.random(),0))}}),["LeftArm","LeftForeArm","RightArm","RightForeArm"].forEach(o=>{n[0].props[o+".quaternion"]=this.ikMesh.getObjectByName(o).quaternion.clone()}),i.push(1e3+Math.round(Math.random()*500)),n.push({duration:2e3,props:{}}),["LeftArm","LeftForeArm","RightArm","RightForeArm","LeftHand","RightHand"].forEach(o=>{n[1].props[o+".quaternion"]=null});const s=this.animFactory({name:"talkinghands",delay:t,dt:i,vs:{moveto:n}});this.animQueue.push(s)}getSlowdownRate(t){return this.animSlowdownRate}setSlowdownRate(t){this.animSlowdownRate=t,this.audioSpeechSource.playbackRate.value=1/this.animSlowdownRate,this.audioBackgroundSource.playbackRate.value=1/this.animSlowdownRate}getAutoRotateSpeed(t){return this.controls.autoRotateSpeed}setAutoRotateSpeed(t){this.controls.autoRotateSpeed=t,this.controls.autoRotate=t>0}start(){this.armature&&this.isRunning===!1&&(this.audioCtx.resume(),this.animTimeLast=performance.now(),this.isRunning=!0,this.isAvatarOnly||requestAnimationFrame(this.animate.bind(this)))}stop(){this.isRunning=!1,this.audioCtx.suspend()}startListening(t,e={},i=null){this.listeningAnalyzer=t,this.listeningAnalyzer.fftSize=256,this.listeningAnalyzer.smoothingTimeConstant=.1,this.listeningAnalyzer.minDecibels=-70,this.listeningAnalyzer.maxDecibels=-10,this.listeningOnchange=i&&typeof i=="function"?i:null,this.listeningSilenceThresholdLevel=e?.hasOwnProperty("listeningSilenceThresholdLevel")?e.listeningSilenceThresholdLevel:this.opt.listeningSilenceThresholdLevel,this.listeningSilenceThresholdMs=e?.hasOwnProperty("listeningSilenceThresholdMs")?e.listeningSilenceThresholdMs:this.opt.listeningSilenceThresholdMs,this.listeningSilenceDurationMax=e?.hasOwnProperty("listeningSilenceDurationMax")?e.listeningSilenceDurationMax:this.opt.listeningSilenceDurationMax,this.listeningActiveThresholdLevel=e?.hasOwnProperty("listeningActiveThresholdLevel")?e.listeningActiveThresholdLevel:this.opt.listeningActiveThresholdLevel,this.listeningActiveThresholdMs=e?.hasOwnProperty("listeningActiveThresholdMs")?e.listeningActiveThresholdMs:this.opt.listeningActiveThresholdMs,this.listeningActiveDurationMax=e?.hasOwnProperty("listeningActiveDurationMax")?e.listeningActiveDurationMax:this.opt.listeningActiveDurationMax,this.listeningActive=!1,this.listeningVolume=0,this.listeningTimer=0,this.listeningTimerTotal=0,this.isListening=!0}stopListening(){this.isListening=!1}async playAnimation(t,e=null,i=10,n=0,s=.01,o=!1){if(!this.armature)return;this.positionWasLocked=!o,o?console.log("Position locking disabled for FBX animation:",t):(this.lockAvatarPosition(),console.log("Position locked immediately before FBX animation:",t));let r=this.animClips.find(h=>h.url===t+"-"+n);if(r){let h=this.animQueue.find(l=>l.template.name==="pose");h&&(h.ts[0]=1/0),Object.entries(r.pose.props).forEach(l=>{this.poseBase.props[l[0]]=l[1].clone(),this.poseTarget.props[l[0]]=l[1].clone(),this.poseTarget.props[l[0]].t=0,this.poseTarget.props[l[0]].d=1e3}),this.mixer?console.log("Using existing mixer for FBX animation, preserving morph targets"):(this.mixer=new f.AnimationMixer(this.armature),console.log("Created new mixer for FBX animation")),this.mixer.addEventListener("finished",this.stopAnimation.bind(this),{once:!0});const a=Math.ceil(i/r.clip.duration),u=this.mixer.clipAction(r.clip);u.setLoop(f.LoopRepeat,a),u.clampWhenFinished=!0,this.currentFBXAction=u;try{u.fadeIn(.5).play(),console.log("FBX animation started successfully:",t)}catch(l){console.warn("FBX animation failed to start:",l),this.stopAnimation();return}if(u.getClip().tracks.length===0){console.warn("FBX animation has no valid tracks, stopping"),this.stopAnimation();return}}else{if(t.split(".").pop().toLowerCase()!=="fbx"){console.error(`Invalid file type for FBX animation: ${t}. Expected .fbx file.`);return}let a=!1;try{const c=await fetch(t,{method:"HEAD"});if(a=c.ok,!a){console.error(`FBX file not found at ${t}. Status: ${c.status}`),console.error("Please check:"),console.error("1. File path is correct (note: path is case-sensitive)"),console.error("2. File exists in your public folder"),console.error("3. File is accessible (not blocked by server)");return}}catch(c){console.warn(`Could not verify file existence for ${t}, attempting to load anyway:`,c)}const u=new Ie.FBXLoader;let l;try{l=await u.loadAsync(t,e)}catch(c){console.error(`Failed to load FBX animation from ${t}:`,c),console.error("Error details:",{message:c.message,url:t,suggestion:"Make sure the file is a valid FBX file and the path is correct"}),c.message&&c.message.includes("version number")&&(console.error("FBX Loader Error: Cannot find version number"),console.error("This error usually means:"),console.error("1. The file is not a valid FBX file (might be GLB, corrupted, or wrong format)"),console.error("2. The file might be corrupted"),console.error("3. The file path might be incorrect"),console.error("4. The server returned an HTML error page instead of the FBX file"),console.error("5. The file might not exist at that path"),console.error(""),console.error("Solution: Please verify:"),console.error(` - File exists at: ${t}`),console.error(" - File is a valid FBX binary file"),console.error(" - File path matches your public folder structure"),console.error(" - File is not corrupted"));try{const d=await fetch(t),g=d.headers.get("content-type"),y=await d.text();console.error("Response details:",{status:d.status,contentType:g,firstBytes:y.substring(0,100),isHTML:y.trim().startsWith("<!DOCTYPE")||y.trim().startsWith("<html")}),(y.trim().startsWith("<!DOCTYPE")||y.trim().startsWith("<html"))&&console.error("The server returned an HTML page instead of an FBX file. The file path is likely incorrect.")}catch(d){console.error("Could not fetch file for debugging:",d)}return}if(l&&l.animations&&l.animations[n]){let c=l.animations[n];const d={};c.tracks.forEach(y=>{y.name=y.name.replaceAll("mixamorig","");const x=y.name.split(".");if(x[1]==="position"){for(let I=0;I<y.values.length;I++)y.values[I]=y.values[I]*s;d[y.name]=new f.Vector3(y.values[0],y.values[1],y.values[2])}else x[1]==="quaternion"?d[y.name]=new f.Quaternion(y.values[0],y.values[1],y.values[2],y.values[3]):x[1]==="rotation"&&(d[x[0]+".quaternion"]=new f.Quaternion().setFromEuler(new f.Euler(y.values[0],y.values[1],y.values[2],"XYZ")).normalize())});const g={props:d};d["Hips.position"]&&(d["Hips.position"].y<.5?g.lying=!0:g.standing=!0),this.animClips.push({url:t+"-"+n,clip:c,pose:g}),this.playAnimation(t,e,i,n,s)}else{const c="Animation "+t+" (ndx="+n+") not found";console.error(c),l&&l.animations?console.error(`FBX file loaded but has ${l.animations.length} animation(s), requested index ${n}`):console.error(l?"FBX file loaded but contains no animations":"FBX file failed to load or is invalid")}}}stopAnimation(){if(this.currentFBXAction&&(this.currentFBXAction.stop(),this.currentFBXAction=null,console.log("FBX animation action stopped, mixer preserved for lip-sync")),this.mixer&&this.mixer._actions.length===0&&(this.mixer=null,console.log("Mixer destroyed as no actions remain")),this.positionWasLocked?(this.unlockAvatarPosition(),console.log("Position unlocked after FBX animation stopped")):console.log("Position was not locked, no unlock needed"),this.gesture)for(let[e,i]of Object.entries(this.gesture))i.t=this.animClock,i.d=1e3,this.poseTarget.props.hasOwnProperty(e)&&(this.poseTarget.props[e].copy(i),this.poseTarget.props[e].t=this.animClock,this.poseTarget.props[e].d=1e3);let t=this.animQueue.find(e=>e.template.name==="pose");t&&(t.ts[0]=this.animClock),this.setPoseFromTemplate(null)}async playPose(t,e=null,i=5,n=0,s=.01){if(!this.armature)return;let o=this.poseTemplates[t];if(!o){const r=this.animPoses.find(h=>h.url===t+"-"+n);r&&(o=r.pose)}if(o){this.poseName=t,this.mixer=null;let r=this.animQueue.find(h=>h.template.name==="pose");r&&(r.ts[0]=this.animClock+i*1e3+2e3),this.setPoseFromTemplate(o)}else{let h=await new Ie.FBXLoader().loadAsync(t,e);if(h&&h.animations&&h.animations[n]){let a=h.animations[n];const u={};a.tracks.forEach(c=>{c.name=c.name.replaceAll("mixamorig","");const d=c.name.split(".");d[1]==="position"?u[c.name]=new f.Vector3(c.values[0]*s,c.values[1]*s,c.values[2]*s):d[1]==="quaternion"?u[c.name]=new f.Quaternion(c.values[0],c.values[1],c.values[2],c.values[3]):d[1]==="rotation"&&(u[d[0]+".quaternion"]=new f.Quaternion().setFromEuler(new f.Euler(c.values[0],c.values[1],c.values[2],"XYZ")).normalize())});const l={props:u};u["Hips.position"]&&(u["Hips.position"].y<.5?l.lying=!0:l.standing=!0),this.animPoses.push({url:t+"-"+n,pose:l}),this.playPose(t,e,i,n,s)}else{const a="Pose "+t+" (ndx="+n+") not found";console.error(a)}}}stopPose(){this.stopAnimation()}playGesture(t,e=3,i=!1,n=1e3){if(!this.armature)return;let s=this.gestureTemplates[t];if(s){this.gestureTimeout&&(clearTimeout(this.gestureTimeout),this.gestureTimeout=null);let r=this.animQueue.findIndex(h=>h.template.name==="talkinghands");r!==-1&&(this.animQueue[r].ts=this.animQueue[r].ts.map(h=>0)),this.gesture=this.propsToThreeObjects(s),i&&(this.gesture=this.mirrorPose(this.gesture)),t==="namaste"&&this.avatar.body==="M"&&(this.gesture["RightArm.quaternion"].rotateTowards(new f.Quaternion(0,1,0,0),-.25),this.gesture["LeftArm.quaternion"].rotateTowards(new f.Quaternion(0,1,0,0),-.25));for(let[h,a]of Object.entries(this.gesture))a.t=this.animClock,a.d=n,this.poseTarget.props.hasOwnProperty(h)&&(this.poseTarget.props[h].copy(a),this.poseTarget.props[h].t=this.animClock,this.poseTarget.props[h].d=n);e&&Number.isFinite(e)&&(this.gestureTimeout=setTimeout(this.stopGesture.bind(this,n),1e3*e))}let o=this.animEmojis[t];if(o&&(o&&o.link&&(o=this.animEmojis[o.link]),o)){this.lookAtCamera(500);const r=this.animFactory(o);if(r.gesture=!0,e&&Number.isFinite(e)){const h=r.ts[0],u=r.ts[r.ts.length-1]-h;if(e*1e3-u>0){const c=[];for(let y=1;y<r.ts.length;y++)c.push(r.ts[y]-r.ts[y-1]);const d=o.template?.rescale||c.map(y=>y/u),g=e*1e3-u;r.ts=r.ts.map((y,x,I)=>x===0?h:I[x-1]+c[x-1]+d[x-1]*g)}else{const c=e*1e3/u;r.ts=r.ts.map(d=>h+c*(d-h))}}this.animQueue.push(r)}}stopGesture(t=1e3){if(this.gestureTimeout&&(clearTimeout(this.gestureTimeout),this.gestureTimeout=null),this.gesture){const i=Object.entries(this.gesture);this.gesture=null;for(const[n,s]of i)this.poseTarget.props.hasOwnProperty(n)&&(this.poseTarget.props[n].copy(this.getPoseTemplateProp(n)),this.poseTarget.props[n].t=this.animClock,this.poseTarget.props[n].d=t)}let e=this.animQueue.findIndex(i=>i.gesture);e!==-1&&this.animQueue.splice(e,1)}ikSolve(t,e=null,i=!1,n=null){const s=new f.Vector3,o=new f.Vector3,r=new f.Vector3,h=new f.Vector3,a=new f.Quaternion,u=new f.Vector3,l=new f.Vector3,c=new f.Vector3,d=this.ikMesh.getObjectByName(t.root);d.position.setFromMatrixPosition(this.armature.getObjectByName(t.root).matrixWorld),d.quaternion.setFromRotationMatrix(this.armature.getObjectByName(t.root).matrixWorld),e&&i&&e.applyQuaternion(this.armature.quaternion).add(d.position);const g=this.ikMesh.getObjectByName(t.effector),y=t.links;y.forEach(I=>{I.bone=this.ikMesh.getObjectByName(I.link),I.bone.quaternion.copy(this.getPoseTemplateProp(I.link+".quaternion"))}),d.updateMatrixWorld(!0);const x=t.iterations||10;if(e)for(let I=0;I<x;I++){let E=!1;for(let p=0,P=y.length;p<P;p++){const H=y[p].bone;H.matrixWorld.decompose(h,a,u),a.invert(),o.setFromMatrixPosition(g.matrixWorld),r.subVectors(o,h),r.applyQuaternion(a),r.normalize(),s.subVectors(e,h),s.applyQuaternion(a),s.normalize();let R=s.dot(r);R>1?R=1:R<-1&&(R=-1),R=Math.acos(R),!(R<1e-5)&&(y[p].minAngle!==void 0&&R<y[p].minAngle&&(R=y[p].minAngle),y[p].maxAngle!==void 0&&R>y[p].maxAngle&&(R=y[p].maxAngle),l.crossVectors(r,s),l.normalize(),N.setFromAxisAngle(l,R),H.quaternion.multiply(N),H.rotation.setFromVector3(c.setFromEuler(H.rotation).clamp(new f.Vector3(y[p].minx!==void 0?y[p].minx:-1/0,y[p].miny!==void 0?y[p].miny:-1/0,y[p].minz!==void 0?y[p].minz:-1/0),new f.Vector3(y[p].maxx!==void 0?y[p].maxx:1/0,y[p].maxy!==void 0?y[p].maxy:1/0,y[p].maxz!==void 0?y[p].maxz:1/0))),H.updateMatrixWorld(!0),E=!0)}if(!E)break}n&&y.forEach(I=>{this.poseTarget.props[I.link+".quaternion"].copy(I.bone.quaternion),this.poseTarget.props[I.link+".quaternion"].t=this.animClock,this.poseTarget.props[I.link+".quaternion"].d=n})}dispose(){this.isRunning=!1,this.stop(),this.stopSpeaking(),this.streamStop(),this.isAvatarOnly?this.armature&&(this.armature.parent&&this.armature.parent.remove(this.armature),this.clearThree(this.armature)):(this.clearThree(this.scene),this.resizeobserver.disconnect(),this.renderer&&(this.renderer.dispose(),this.renderer.domElement&&this.renderer.domElement.parentNode&&this.renderer.domElement.parentNode.removeChild(this.renderer.domElement),this.renderer=null)),this.clearThree(this.ikMesh),this.dynamicbones.dispose()}}const se={apiKey:"sk_ace57ef3ef65a92b9d3bee2a00183b78ca790bc3e10964f2",endpoint:"https://api.elevenlabs.io/v1/text-to-speech",defaultVoice:"21m00Tcm4TlvDq8ikWAM",voices:{rachel:"21m00Tcm4TlvDq8ikWAM",drew:"29vD33N1CtxCmqQRPOHJ",bella:"EXAVITQu4vr4xnSDxMaL",antoni:"ErXwobaYiN019PkySvjV",elli:"MF3mGyEYCl7XYWbV9V6O",josh:"VR6AewLTigWG4xSOukaG"}},ke={defaultVoice:"aura-2-thalia-en",voices:{thalia:"aura-2-thalia-en",asteria:"aura-2-asteria-en",orion:"aura-2-orion-en",stella:"aura-2-stella-en",athena:"aura-2-athena-en",hera:"aura-2-hera-en",zeus:"aura-2-zeus-en"}};function me(){return{service:"elevenlabs",endpoint:se.endpoint,apiKey:se.apiKey,defaultVoice:se.defaultVoice,voices:se.voices}}function it(){const M=me(),t=[];return Object.entries(M.voices).forEach(([e,i])=>{t.push({value:i,label:`${e.charAt(0).toUpperCase()+e.slice(1)} (${M.service})`})}),t}const xe=A.forwardRef(({avatarUrl:M="/avatars/brunette.glb",avatarBody:t="F",mood:e="neutral",ttsLang:i="en",ttsService:n=null,ttsVoice:s=null,ttsApiKey:o=null,bodyMovement:r="idle",movementIntensity:h=.5,showFullAvatar:a=!0,cameraView:u="upper",onReady:l=()=>{},onLoading:c=()=>{},onError:d=()=>{},className:g="",style:y={},animations:x={}},I)=>{const E=A.useRef(null),p=A.useRef(null),[P,H]=A.useState(!0),[R,O]=A.useState(null),[D,J]=A.useState(!1),X=me(),k=n||X.service;let B;k==="browser"?B={service:"browser",endpoint:"",apiKey:null,defaultVoice:"Google US English"}:k==="elevenlabs"?B={service:"elevenlabs",endpoint:"https://api.elevenlabs.io/v1/text-to-speech",apiKey:o||X.apiKey,defaultVoice:s||X.defaultVoice||se.defaultVoice,voices:X.voices||se.voices}:k==="deepgram"?B={service:"deepgram",endpoint:"https://api.deepgram.com/v1/speak",apiKey:o||X.apiKey,defaultVoice:s||X.defaultVoice||ke.defaultVoice,voices:X.voices||ke.voices}:B={...X,apiKey:o!==null?o:X.apiKey};const W={url:M,body:t,avatarMood:e,ttsLang:k==="browser"?"en-US":i,ttsVoice:s||B.defaultVoice,lipsyncLang:"en",showFullAvatar:a,bodyMovement:r,movementIntensity:h},V={ttsEndpoint:B.endpoint,ttsApikey:B.apiKey,ttsService:k,lipsyncModules:["en"],cameraView:u},Q=A.useCallback(async()=>{if(!(!E.current||p.current))try{if(H(!0),O(null),p.current=new we(E.current,V),p.current.controls&&(p.current.controls.enableRotate=!1,p.current.controls.enableZoom=!1,p.current.controls.enablePan=!1,p.current.controls.enableDamping=!1),x&&Object.keys(x).length>0&&(p.current.customAnimations=x),await p.current.showAvatar(W,U=>{if(U.lengthComputable){const z=Math.min(100,Math.round(U.loaded/U.total*100));c(z)}}),await new Promise(U=>{const z=()=>{p.current.lipsync&&Object.keys(p.current.lipsync).length>0?U():setTimeout(z,100)};z()}),p.current&&p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(a)}catch(U){console.warn("Error setting full body mode on initialization:",U)}p.current&&p.current.controls&&(p.current.controls.enableRotate=!1,p.current.controls.enableZoom=!1,p.current.controls.enablePan=!1,p.current.controls.enableDamping=!1,p.current.controls.update()),H(!1),J(!0),l(p.current);const T=()=>{document.visibilityState==="visible"?p.current?.start():p.current?.stop()};return document.addEventListener("visibilitychange",T),()=>{document.removeEventListener("visibilitychange",T)}}catch(S){console.error("Error initializing TalkingHead:",S),O(S.message||"Failed to initialize avatar"),H(!1),d(S)}},[M,t,e,i,n,s,o,a,r,h,u]);A.useEffect(()=>(Q(),()=>{p.current&&(p.current.stop(),p.current.dispose(),p.current=null)}),[Q]),A.useEffect(()=>{if(!E.current||!p.current)return;const S=new ResizeObserver(U=>{for(const z of U)p.current&&p.current.onResize&&p.current.onResize()});S.observe(E.current);const T=()=>{p.current&&p.current.onResize&&p.current.onResize()};return window.addEventListener("resize",T),()=>{S.disconnect(),window.removeEventListener("resize",T)}},[D]);const q=A.useCallback(async()=>{if(p.current&&p.current.audioCtx)try{(p.current.audioCtx.state==="suspended"||p.current.audioCtx.state==="interrupted")&&(await p.current.audioCtx.resume(),console.log("Audio context resumed"))}catch(S){console.warn("Failed to resume audio context:",S)}},[]),ae=A.useCallback(async(S,T={})=>{if(p.current&&D)try{await q();const U={...T,lipsyncLang:T.lipsyncLang||W.lipsyncLang||"en"};if(T.onSpeechEnd&&p.current){const z=p.current;let Y=null,oe=0,re=!1;const He=1200,Te=1e4;let Re=0;const ge=setInterval(()=>{Re++,z&&z.isSpeaking&&(z.audioPlaylist&&z.audioPlaylist.length>0||z.isAudioPlaying===!0)&&(re=!0,clearInterval(ge),Y=setInterval(ve,50));const le=!z.speechQueue||z.speechQueue.length===0;if(z&&!z.isSpeaking&&le&&(!z.audioPlaylist||z.audioPlaylist.length===0)&&(!z.isAudioPlaying||z.isAudioPlaying===!1)){clearInterval(ge);try{T.onSpeechEnd()}catch(he){console.error("Error in onSpeechEnd callback:",he)}return}if(Re*50>Te){if(clearInterval(ge),z&&z.isSpeaking)re=!0,Y=setInterval(ve,50);else if(z&&!z.isSpeaking&&le&&(!z.audioPlaylist||z.audioPlaylist.length===0)&&(!z.isAudioPlaying||z.isAudioPlaying===!1))try{T.onSpeechEnd()}catch(he){console.error("Error in onSpeechEnd callback:",he)}}},50),ve=()=>{if(oe++,oe>He){Y&&(clearInterval(Y),Y=null);try{T.onSpeechEnd()}catch(ye){console.error("Error in onSpeechEnd callback:",ye)}return}if(!re)return;const le=!z.speechQueue||z.speechQueue.length===0;z&&(!z.isSpeaking||z.isSpeaking===!1)&&(!z.audioPlaylist||z.audioPlaylist.length===0)&&(!z.isAudioPlaying||z.isAudioPlaying===!1)&&le&&(Y&&(clearInterval(Y),Y=null),setTimeout(()=>{try{T.onSpeechEnd()}catch(ye){console.error("Error in onSpeechEnd callback:",ye)}},50))}}p.current.lipsync&&Object.keys(p.current.lipsync).length>0?(p.current.setSlowdownRate&&p.current.setSlowdownRate(1.05),p.current.speakText(S,U)):setTimeout(async()=>{await q(),p.current&&p.current.lipsync&&(p.current.setSlowdownRate&&p.current.setSlowdownRate(1.05),p.current.speakText(S,U))},100)}catch(U){console.error("Error speaking text:",U),O(U.message||"Failed to speak text")}},[D,q,W.lipsyncLang]),pe=A.useCallback(()=>{p.current&&(p.current.stopSpeaking(),p.current.setSlowdownRate&&p.current.setSlowdownRate(1))},[]),_=A.useCallback(S=>{p.current&&p.current.setMood(S)},[]),b=A.useCallback(S=>{p.current&&p.current.setSlowdownRate&&p.current.setSlowdownRate(S)},[]),v=A.useCallback((S,T=!1)=>{if(p.current&&p.current.playAnimation){if(x&&x[S]&&(S=x[S]),p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(!0)}catch(z){console.warn("Error setting full body mode:",z)}if(S.includes("."))try{p.current.playAnimation(S,null,10,0,.01,T)}catch(z){console.warn(`Failed to play ${S}:`,z);try{p.current.setBodyMovement("idle")}catch(Y){console.warn("Fallback animation also failed:",Y)}}else{const z=[".fbx",".glb",".gltf"];let Y=!1;for(const oe of z)try{p.current.playAnimation(S+oe,null,10,0,.01,T),Y=!0;break}catch{}if(!Y){console.warn("Animation not found:",S);try{p.current.setBodyMovement("idle")}catch(oe){console.warn("Fallback animation also failed:",oe)}}}}},[x]),C=A.useCallback(()=>{p.current&&p.current.onResize&&p.current.onResize()},[]);return A.useImperativeHandle(I,()=>({speakText:ae,stopSpeaking:pe,resumeAudioContext:q,setMood:_,setTimingAdjustment:b,playAnimation:v,isReady:D,talkingHead:p.current,handleResize:C,setBodyMovement:S=>{if(p.current&&p.current.setShowFullAvatar&&p.current.setBodyMovement)try{p.current.setShowFullAvatar(!0),p.current.setBodyMovement(S)}catch(T){console.warn("Error setting body movement:",T)}},setMovementIntensity:S=>p.current?.setMovementIntensity(S),playRandomDance:()=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playRandomDance)try{p.current.setShowFullAvatar(!0),p.current.playRandomDance()}catch(S){console.warn("Error playing random dance:",S)}},playReaction:S=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playReaction)try{p.current.setShowFullAvatar(!0),p.current.playReaction(S)}catch(T){console.warn("Error playing reaction:",T)}},playCelebration:()=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playCelebration)try{p.current.setShowFullAvatar(!0),p.current.playCelebration()}catch(S){console.warn("Error playing celebration:",S)}},setShowFullAvatar:S=>{if(p.current&&p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(S)}catch(T){console.warn("Error setting showFullAvatar:",T)}},lockAvatarPosition:()=>{if(p.current&&p.current.lockAvatarPosition)try{p.current.lockAvatarPosition()}catch(S){console.warn("Error locking avatar position:",S)}},unlockAvatarPosition:()=>{if(p.current&&p.current.unlockAvatarPosition)try{p.current.unlockAvatarPosition()}catch(S){console.warn("Error unlocking avatar position:",S)}}})),te.jsxs("div",{className:`talking-head-avatar ${g}`,style:{width:"100%",height:"100%",position:"relative",...y},children:[te.jsx("div",{ref:E,className:"talking-head-viewer",style:{width:"100%",height:"100%",minHeight:"400px"}}),P&&te.jsx("div",{className:"loading-overlay",style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",color:"white",fontSize:"18px",zIndex:10},children:"Loading avatar..."}),R&&te.jsx("div",{className:"error-overlay",style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",color:"#ff6b6b",fontSize:"16px",textAlign:"center",zIndex:10,padding:"20px",borderRadius:"8px"},children:R})]})});xe.displayName="TalkingHeadAvatar";const ze=A.forwardRef(({text:M="Hello! I'm a talking avatar. How are you today?",onLoading:t=()=>{},onError:e=()=>{},onReady:i=()=>{},className:n="",style:s={},avatarConfig:o={}},r)=>{const h=A.useRef(null),a=A.useRef(null),[u,l]=A.useState(!0),[c,d]=A.useState(null),[g,y]=A.useState(!1),x=me(),I=o.ttsService||x.service,E=I==="browser"?{endpoint:"",apiKey:null,defaultVoice:"Google US English"}:{...x,apiKey:o.ttsApiKey!==void 0&&o.ttsApiKey!==null?o.ttsApiKey:x.apiKey,endpoint:I==="elevenlabs"&&o.ttsApiKey?"https://api.elevenlabs.io/v1/text-to-speech":x.endpoint},p={url:"/avatars/brunette.glb",body:"F",avatarMood:"neutral",ttsLang:I==="browser"?"en-US":"en",ttsVoice:o.ttsVoice||E.defaultVoice,lipsyncLang:"en",showFullAvatar:!0,bodyMovement:"idle",movementIntensity:.5,...o},P={ttsEndpoint:E.endpoint,ttsApikey:E.apiKey,ttsService:I,lipsyncModules:["en"],cameraView:"upper"},H=A.useCallback(async()=>{if(!(!h.current||a.current))try{if(l(!0),d(null),a.current=new we(h.current,P),await a.current.showAvatar(p,W=>{if(W.lengthComputable){const V=Math.min(100,Math.round(W.loaded/W.total*100));t(V)}}),a.current.morphs&&a.current.morphs.length>0){const W=a.current.morphs[0].morphTargetDictionary;console.log("Available morph targets:",Object.keys(W));const V=Object.keys(W).filter(Q=>Q.startsWith("viseme_"));console.log("Viseme morph targets found:",V),V.length===0&&(console.warn("No viseme morph targets found! Lip-sync will not work properly."),console.log("Expected viseme targets: viseme_aa, viseme_E, viseme_I, viseme_O, viseme_U, viseme_PP, viseme_SS, viseme_TH, viseme_DD, viseme_FF, viseme_kk, viseme_nn, viseme_RR, viseme_CH, viseme_sil"))}if(await new Promise(W=>{const V=()=>{a.current.lipsync&&Object.keys(a.current.lipsync).length>0?(console.log("Lip-sync modules loaded:",Object.keys(a.current.lipsync)),W()):(console.log("Waiting for lip-sync modules to load..."),setTimeout(V,100))};V()}),a.current&&a.current.setShowFullAvatar)try{a.current.setShowFullAvatar(!0),console.log("Avatar initialized in full body mode")}catch(W){console.warn("Error setting full body mode on initialization:",W)}l(!1),y(!0),i(a.current);const B=()=>{document.visibilityState==="visible"?a.current?.start():a.current?.stop()};return document.addEventListener("visibilitychange",B),()=>{document.removeEventListener("visibilitychange",B)}}catch(k){console.error("Error initializing TalkingHead:",k),d(k.message||"Failed to initialize avatar"),l(!1),e(k)}},[]);A.useEffect(()=>(H(),()=>{a.current&&(a.current.stop(),a.current.dispose(),a.current=null)}),[H]);const R=A.useCallback(k=>{if(a.current&&g)try{console.log("Speaking text:",k),console.log("Avatar config:",p),console.log("TalkingHead instance:",a.current),a.current.lipsync&&Object.keys(a.current.lipsync).length>0?(console.log("Lip-sync modules loaded:",Object.keys(a.current.lipsync)),a.current.setSlowdownRate&&(a.current.setSlowdownRate(1.05),console.log("Applied timing adjustment for better lip-sync")),a.current.speakText(k)):(console.warn("Lip-sync modules not ready, waiting..."),setTimeout(()=>{a.current&&a.current.lipsync?(console.log("Lip-sync now ready, speaking..."),a.current.setSlowdownRate&&(a.current.setSlowdownRate(1.05),console.log("Applied timing adjustment for better lip-sync")),a.current.speakText(k)):console.error("Lip-sync still not ready after waiting")},500))}catch(B){console.error("Error speaking text:",B),d(B.message||"Failed to speak text")}else console.warn("Avatar not ready for speaking. isReady:",g,"talkingHeadRef:",!!a.current)},[g,p]),O=A.useCallback(()=>{a.current&&(a.current.stopSpeaking(),a.current.setSlowdownRate&&(a.current.setSlowdownRate(1),console.log("Reset timing to normal")))},[]),D=A.useCallback(k=>{a.current&&a.current.setMood(k)},[]),J=A.useCallback(k=>{a.current&&a.current.setSlowdownRate&&(a.current.setSlowdownRate(k),console.log("Timing adjustment set to:",k))},[]),X=A.useCallback((k,B=!1)=>{if(a.current&&a.current.playAnimation){if(a.current.setShowFullAvatar)try{a.current.setShowFullAvatar(!0)}catch(V){console.warn("Error setting full body mode:",V)}if(k.includes("."))try{a.current.playAnimation(k,null,10,0,.01,B),console.log("Playing animation:",k)}catch(V){console.log(`Failed to play ${k}:`,V);try{a.current.setBodyMovement("idle"),console.log("Fallback to idle animation")}catch(Q){console.warn("Fallback animation also failed:",Q)}}else{const V=[".fbx",".glb",".gltf"];let Q=!1;for(const q of V)try{a.current.playAnimation(k+q,null,10,0,.01,B),console.log("Playing animation:",k+q),Q=!0;break}catch{console.log(`Failed to play ${k}${q}, trying next format...`)}if(!Q){console.warn("Animation system not available or animation not found:",k);try{a.current.setBodyMovement("idle"),console.log("Fallback to idle animation")}catch(q){console.warn("Fallback animation also failed:",q)}}}}else console.warn("Animation system not available or animation not found:",k)},[]);return A.useImperativeHandle(r,()=>({speakText:R,stopSpeaking:O,setMood:D,setTimingAdjustment:J,playAnimation:X,isReady:g,talkingHead:a.current,setBodyMovement:k=>{if(a.current&&a.current.setShowFullAvatar&&a.current.setBodyMovement)try{a.current.setShowFullAvatar(!0),a.current.setBodyMovement(k),console.log("Body movement set with full body mode:",k)}catch(B){console.warn("Error setting body movement:",B)}},setMovementIntensity:k=>a.current?.setMovementIntensity(k),playRandomDance:()=>{if(a.current&&a.current.setShowFullAvatar&&a.current.playRandomDance)try{a.current.setShowFullAvatar(!0),a.current.playRandomDance(),console.log("Random dance played with full body mode")}catch(k){console.warn("Error playing random dance:",k)}},playReaction:k=>{if(a.current&&a.current.setShowFullAvatar&&a.current.playReaction)try{a.current.setShowFullAvatar(!0),a.current.playReaction(k),console.log("Reaction played with full body mode:",k)}catch(B){console.warn("Error playing reaction:",B)}},playCelebration:()=>{if(a.current&&a.current.setShowFullAvatar&&a.current.playCelebration)try{a.current.setShowFullAvatar(!0),a.current.playCelebration(),console.log("Celebration played with full body mode")}catch(k){console.warn("Error playing celebration:",k)}},setShowFullAvatar:k=>{if(a.current&&a.current.setShowFullAvatar)try{a.current.setShowFullAvatar(k),console.log("Show full avatar set to:",k)}catch(B){console.warn("Error setting showFullAvatar:",B)}},lockAvatarPosition:()=>{if(a.current&&a.current.lockAvatarPosition)try{a.current.lockAvatarPosition()}catch(k){console.warn("Error locking avatar position:",k)}},unlockAvatarPosition:()=>{if(a.current&&a.current.unlockAvatarPosition)try{a.current.unlockAvatarPosition()}catch(k){console.warn("Error unlocking avatar position:",k)}}})),te.jsxs("div",{className:`talking-head-container ${n}`,style:s,children:[te.jsx("div",{ref:h,className:"talking-head-viewer",style:{width:"100%",height:"100%",minHeight:"400px"}}),u&&te.jsx("div",{className:"loading-overlay",style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",color:"white",fontSize:"18px",zIndex:10},children:"Loading avatar..."}),c&&te.jsx("div",{className:"error-overlay",style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",color:"#ff6b6b",fontSize:"16px",textAlign:"center",zIndex:10,padding:"20px",borderRadius:"8px"},children:c})]})});ze.displayName="TalkingHeadComponent";const Ce=A.forwardRef(({curriculumData:M=null,avatarConfig:t={},animations:e={},onLessonStart:i=()=>{},onLessonComplete:n=()=>{},onQuestionAnswer:s=()=>{},onCurriculumComplete:o=()=>{},onCustomAction:r=()=>{},autoStart:h=!1},a)=>{const u=A.useRef(null),l=A.useRef({currentModuleIndex:0,currentLessonIndex:0,currentQuestionIndex:0,isTeaching:!1,isQuestionMode:!1,lessonCompleted:!1,curriculumCompleted:!1,score:0,totalQuestions:0}),c=A.useRef({onLessonStart:i,onLessonComplete:n,onQuestionAnswer:s,onCurriculumComplete:o,onCustomAction:r}),d=A.useRef(null),g=A.useRef(null),y=A.useRef(null),x=A.useRef(null),I=A.useRef(null),E=A.useRef(null),p=A.useRef(null),P=A.useRef(M?.curriculum||{title:"Default Curriculum",description:"No curriculum data provided",language:"en",modules:[]}),H=A.useRef({avatarUrl:t.avatarUrl||"/avatars/brunette.glb",avatarBody:t.avatarBody||"F",mood:t.mood||"happy",ttsLang:t.ttsLang||"en",ttsService:t.ttsService||null,ttsVoice:t.ttsVoice||null,ttsApiKey:t.ttsApiKey||null,bodyMovement:t.bodyMovement||"gesturing",movementIntensity:t.movementIntensity||.7,showFullAvatar:t.showFullAvatar!==void 0?t.showFullAvatar:!1,animations:e,lipsyncLang:"en"});A.useEffect(()=>{c.current={onLessonStart:i,onLessonComplete:n,onQuestionAnswer:s,onCurriculumComplete:o,onCustomAction:r}},[i,n,s,o,r]),A.useEffect(()=>{P.current=M?.curriculum||{title:"Default Curriculum",description:"No curriculum data provided",language:"en",modules:[]},H.current={avatarUrl:t.avatarUrl||"/avatars/brunette.glb",avatarBody:t.avatarBody||"F",mood:t.mood||"happy",ttsLang:t.ttsLang||"en",ttsService:t.ttsService||null,ttsVoice:t.ttsVoice||null,ttsApiKey:t.ttsApiKey||null,bodyMovement:t.bodyMovement||"gesturing",movementIntensity:t.movementIntensity||.7,showFullAvatar:t.showFullAvatar!==void 0?t.showFullAvatar:!1,animations:e,lipsyncLang:"en"}},[M,t,e]);const R=A.useCallback(()=>(P.current||{modules:[]}).modules[l.current.currentModuleIndex]?.lessons[l.current.currentLessonIndex],[]),O=A.useCallback(()=>R()?.questions[l.current.currentQuestionIndex],[R]),D=A.useCallback((b,v)=>v.type==="multiple_choice"||v.type==="true_false"?b===v.answer:v.type==="code_test"&&typeof b=="object"&&b!==null?b.passed===!0:!1,[]),J=A.useCallback(()=>{l.current.lessonCompleted=!0,l.current.isQuestionMode=!1;const b=l.current.totalQuestions>0?Math.round(l.current.score/l.current.totalQuestions*100):100;let v="Congratulations! You've completed this lesson";if(l.current.totalQuestions>0?v+=` with a score of ${l.current.score} out of ${l.current.totalQuestions} (${b}%). `:v+="! ",b>=80?v+="Excellent work! You have a great understanding of this topic.":b>=60?v+="Good job! You understand most of the concepts.":v+="Keep practicing! You're making progress.",c.current.onLessonComplete({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,score:l.current.score,totalQuestions:l.current.totalQuestions,percentage:b}),c.current.onCustomAction({type:"lessonComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,score:l.current.score,totalQuestions:l.current.totalQuestions,percentage:b}),u.current){if(u.current.setMood("happy"),e.lessonComplete)try{u.current.playAnimation(e.lessonComplete,!0)}catch{u.current.playCelebration()}const C=P.current||{modules:[]},S=C.modules[l.current.currentModuleIndex],T=l.current.currentLessonIndex<(S?.lessons?.length||0)-1,U=l.current.currentModuleIndex<(C.modules?.length||0)-1,z=T||U,Y=H.current||{lipsyncLang:"en"};u.current.speakText(v,{lipsyncLang:Y.lipsyncLang,onSpeechEnd:()=>{c.current.onCustomAction({type:"lessonCompleteFeedbackDone",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,score:l.current.score,totalQuestions:l.current.totalQuestions,percentage:b,hasNextLesson:z})}})}},[e.lessonComplete]),X=A.useCallback(()=>{l.current.curriculumCompleted=!0;const b=P.current||{modules:[]};if(c.current.onCurriculumComplete({modules:b.modules.length,totalLessons:b.modules.reduce((v,C)=>v+C.lessons.length,0)}),u.current){if(u.current.setMood("celebrating"),e.curriculumComplete)try{u.current.playAnimation(e.curriculumComplete,!0)}catch{u.current.playCelebration()}const v=H.current||{lipsyncLang:"en"};u.current.speakText("Amazing! You've completed the entire curriculum! You're now ready to move on to more advanced topics. Well done!",{lipsyncLang:v.lipsyncLang})}},[e.curriculumComplete]),k=A.useCallback(()=>{const b=R();l.current.isQuestionMode=!0,l.current.currentQuestionIndex=0,l.current.totalQuestions=b?.questions?.length||0;const v=O();if(v&&c.current.onCustomAction({type:"questionStart",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,totalQuestions:l.current.totalQuestions,question:v}),u.current&&u.current.isReady&&v){if(u.current.setMood("curious"),e.questionStart)try{u.current.playAnimation(e.questionStart,!0)}catch(S){console.warn("Failed to play questionStart animation:",S)}const C=H.current||{lipsyncLang:"en"};v.type==="code_test"?u.current.speakText(`Let's test your coding skills! Here's your first challenge: ${v.question}`,{lipsyncLang:C.lipsyncLang}):v.type==="multiple_choice"?u.current.speakText(`Now let me ask you some questions. Here's the first one: ${v.question}`,{lipsyncLang:C.lipsyncLang}):v.type==="true_false"?u.current.speakText(`Let's start with some true or false questions. First question: ${v.question}`,{lipsyncLang:C.lipsyncLang}):u.current.speakText(`Now let me ask you some questions. Here's the first one: ${v.question}`,{lipsyncLang:C.lipsyncLang})}else if(u.current&&u.current.isReady){const C=H.current||{lipsyncLang:"en"};u.current.speakText("Now let me ask you some questions to test your understanding.",{lipsyncLang:C.lipsyncLang})}else setTimeout(()=>{E.current&&E.current()},100)},[e.questionStart,R,O]),B=A.useCallback(()=>{const b=R();if(l.current.currentQuestionIndex<(b?.questions?.length||0)-1){l.current.currentQuestionIndex+=1;const v=O();if(v&&c.current.onCustomAction({type:"nextQuestion",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,totalQuestions:l.current.totalQuestions,question:v}),u.current&&v){if(u.current.setMood("happy"),u.current.setBodyMovement("idle"),e.nextQuestion)try{u.current.playAnimation(e.nextQuestion,!0)}catch(S){console.warn("Failed to play nextQuestion animation:",S)}const C=H.current||{lipsyncLang:"en"};v.type==="code_test"?u.current.speakText(`Great! Now let's move on to your next coding challenge: ${v.question}`,{lipsyncLang:C.lipsyncLang}):v.type==="multiple_choice"?u.current.speakText(`Alright! Here's your next question: ${v.question}`,{lipsyncLang:C.lipsyncLang}):v.type==="true_false"?u.current.speakText(`Now let's try this one: ${v.question}`,{lipsyncLang:C.lipsyncLang}):u.current.speakText(`Here's the next question: ${v.question}`,{lipsyncLang:C.lipsyncLang})}}else c.current.onCustomAction({type:"allQuestionsComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,totalQuestions:l.current.totalQuestions,score:l.current.score})},[e.nextQuestion,R,O]),W=A.useCallback(()=>{const b=P.current||{modules:[]},v=b.modules[l.current.currentModuleIndex];l.current.currentLessonIndex<(v?.lessons?.length||0)-1?(l.current.currentLessonIndex+=1,l.current.currentQuestionIndex=0,l.current.lessonCompleted=!1,l.current.isQuestionMode=!1,l.current.isTeaching=!1,l.current.score=0,l.current.totalQuestions=0,c.current.onCustomAction({type:"lessonStart",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex}),c.current.onLessonStart({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:R()}),u.current&&(u.current.setMood("happy"),u.current.setBodyMovement("idle"))):l.current.currentModuleIndex<(b.modules?.length||0)-1?(l.current.currentModuleIndex+=1,l.current.currentLessonIndex=0,l.current.currentQuestionIndex=0,l.current.lessonCompleted=!1,l.current.isQuestionMode=!1,l.current.isTeaching=!1,l.current.score=0,l.current.totalQuestions=0,c.current.onCustomAction({type:"lessonStart",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex}),c.current.onLessonStart({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:R()}),u.current&&(u.current.setMood("happy"),u.current.setBodyMovement("idle"))):I.current&&I.current()},[]),V=A.useCallback(()=>{const b=R();let v=null;if(b?.avatar_script&&b?.body){const C=b.avatar_script.trim(),S=b.body.trim(),T=C.match(/[.!?]$/)?" ":". ";v=`${C}${T}${S}`}else v=b?.avatar_script||b?.body||null;if(u.current&&u.current.isReady&&v){l.current.isTeaching=!0,l.current.isQuestionMode=!1,u.current.setMood("happy");let C=!1;if(e.teaching)try{u.current.playAnimation(e.teaching,!0),C=!0}catch(T){console.warn("Failed to play teaching animation:",T)}C||u.current.setBodyMovement("gesturing");const S=H.current||{lipsyncLang:"en"};c.current.onLessonStart({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:b}),c.current.onCustomAction({type:"teachingStart",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:b}),u.current.speakText(v,{lipsyncLang:S.lipsyncLang,onSpeechEnd:()=>{l.current.isTeaching=!1,c.current.onCustomAction({type:"teachingComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,lesson:b,hasQuestions:b.questions&&b.questions.length>0})}})}},[e.teaching,R]),Q=A.useCallback(b=>{const v=O(),C=D(b,v);if(C&&(l.current.score+=1),c.current.onQuestionAnswer({moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,answer:b,isCorrect:C,question:v}),u.current)if(C){if(u.current.setMood("happy"),e.correct)try{u.current.playReaction("happy")}catch{u.current.setBodyMovement("happy")}u.current.setBodyMovement("gesturing");const S=v.type==="code_test"?`Great job! Your code passed all the tests! ${v.explanation||""}`:`Excellent! That's correct! ${v.explanation||""}`,T=H.current||{lipsyncLang:"en"};u.current.speakText(S,{lipsyncLang:T.lipsyncLang,onSpeechEnd:()=>{c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,isCorrect:!0,hasNextQuestion:l.current.currentQuestionIndex<(R()?.questions?.length||0)-1})}})}else{if(u.current.setMood("sad"),e.incorrect)try{u.current.playAnimation(e.incorrect,!0)}catch{u.current.setBodyMovement("idle")}u.current.setBodyMovement("gesturing");const S=v.type==="code_test"?`Your code didn't pass all the tests. ${v.explanation||"Try again!"}`:`Not quite right, but don't worry! ${v.explanation||""} Let's move on to the next question.`,T=H.current||{lipsyncLang:"en"};u.current.speakText(S,{lipsyncLang:T.lipsyncLang,onSpeechEnd:()=>{c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,isCorrect:!1,hasNextQuestion:l.current.currentQuestionIndex<(R()?.questions?.length||0)-1})}})}else c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,isCorrect:C,hasNextQuestion:l.current.currentQuestionIndex<(R()?.questions?.length||0)-1,avatarNotReady:!0})},[e.correct,e.incorrect,O,R,D]),q=A.useCallback(b=>{const v=O();if(!b||typeof b!="object"){console.error("Invalid code test result format. Expected object with {passed: boolean, ...}");return}if(v?.type!=="code_test"){console.warn("Current question is not a code test. Use handleAnswerSelect for other question types.");return}const C={passed:b.passed===!0,results:b.results||[],output:b.output||"",error:b.error||null,executionTime:b.executionTime||null,testCount:b.testCount||0,passedCount:b.passedCount||0,failedCount:b.failedCount||0};c.current.onCustomAction({type:"codeTestSubmitted",moduleIndex:l.current.currentModuleIndex,lessonIndex:l.current.currentLessonIndex,questionIndex:l.current.currentQuestionIndex,testResult:C,question:v}),p.current&&p.current(C)},[O,D]),ae=A.useCallback(()=>{l.current.currentModuleIndex=0,l.current.currentLessonIndex=0,l.current.currentQuestionIndex=0,l.current.isTeaching=!1,l.current.isQuestionMode=!1,l.current.lessonCompleted=!1,l.current.curriculumCompleted=!1,l.current.score=0,l.current.totalQuestions=0},[]),pe=A.useCallback(b=>{console.log("Avatar is ready!",b);const v=R(),C=v?.avatar_script||v?.body;h&&C&&setTimeout(()=>{d.current&&d.current()},10)},[h,R]);A.useLayoutEffect(()=>{d.current=V,g.current=W,y.current=J,x.current=B,I.current=X,E.current=k,p.current=Q}),A.useImperativeHandle(a,()=>({startTeaching:V,startQuestions:k,handleAnswerSelect:Q,handleCodeTestResult:q,nextQuestion:B,nextLesson:W,completeLesson:J,completeCurriculum:X,resetCurriculum:ae,getState:()=>({...l.current}),getCurrentQuestion:()=>O(),getCurrentLesson:()=>R(),getAvatarRef:()=>u.current,speakText:async(b,v={})=>{await u.current?.resumeAudioContext?.();const C=H.current||{lipsyncLang:"en"};u.current?.speakText(b,{...v,lipsyncLang:v.lipsyncLang||C.lipsyncLang})},resumeAudioContext:async()=>{if(u.current?.resumeAudioContext)return await u.current.resumeAudioContext();const b=u.current?.talkingHead;if(b?.audioCtx){const v=b.audioCtx;if(v.state==="suspended"||v.state==="interrupted")try{await v.resume(),console.log("Audio context resumed via talkingHead")}catch(C){console.warn("Failed to resume audio context:",C)}}else console.warn("Audio context not available yet")},stopSpeaking:()=>u.current?.stopSpeaking(),setMood:b=>u.current?.setMood(b),playAnimation:(b,v)=>u.current?.playAnimation(b,v),setBodyMovement:b=>u.current?.setBodyMovement(b),setMovementIntensity:b=>u.current?.setMovementIntensity(b),playRandomDance:()=>u.current?.playRandomDance(),playReaction:b=>u.current?.playReaction(b),playCelebration:()=>u.current?.playCelebration(),setShowFullAvatar:b=>u.current?.setShowFullAvatar(b),setTimingAdjustment:b=>u.current?.setTimingAdjustment(b),lockAvatarPosition:()=>u.current?.lockAvatarPosition(),unlockAvatarPosition:()=>u.current?.unlockAvatarPosition(),triggerCustomAction:(b,v)=>{c.current.onCustomAction({type:b,...v,state:{...l.current}})},handleResize:()=>u.current?.handleResize(),isAvatarReady:()=>u.current?.isReady||!1}),[V,k,Q,q,B,W,J,X,ae,O,R]);const _=H.current||{avatarUrl:"/avatars/brunette.glb",avatarBody:"F",mood:"happy",ttsLang:"en",ttsService:null,ttsVoice:null,ttsApiKey:null,bodyMovement:"gesturing",movementIntensity:.7,showFullAvatar:!1,animations:e};return te.jsx("div",{style:{width:"100%",height:"100%"},children:te.jsx(xe,{ref:u,avatarUrl:_.avatarUrl,avatarBody:_.avatarBody,mood:_.mood,ttsLang:_.ttsLang,ttsService:_.ttsService,ttsVoice:_.ttsVoice,ttsApiKey:_.ttsApiKey,bodyMovement:_.bodyMovement,movementIntensity:_.movementIntensity,showFullAvatar:_.showFullAvatar,cameraView:"upper",animations:_.animations,onReady:pe,onLoading:()=>{},onError:b=>{console.error("Avatar error:",b)}})})});Ce.displayName="CurriculumLearning";const be={dance:{name:"dance",type:"code-based",variations:["dancing","dancing2","dancing3"],loop:!0,duration:1e4,description:"Celebration dance animation with multiple variations"},happy:{name:"happy",type:"code-based",loop:!0,duration:5e3,description:"Happy, upbeat body movement"},surprised:{name:"surprised",type:"code-based",loop:!1,duration:3e3,description:"Surprised reaction with quick movements"},thinking:{name:"thinking",type:"code-based",loop:!0,duration:8e3,description:"Thoughtful, contemplative movement"},nodding:{name:"nodding",type:"code-based",loop:!1,duration:2e3,description:"Nodding agreement gesture"},shaking:{name:"shaking",type:"code-based",loop:!1,duration:2e3,description:"Shaking head disagreement gesture"},celebration:{name:"celebration",type:"code-based",loop:!1,duration:3e3,description:"Celebration animation for achievements"},energetic:{name:"energetic",type:"code-based",loop:!0,duration:6e3,description:"High-energy, enthusiastic movement"},swaying:{name:"swaying",type:"code-based",loop:!0,duration:8e3,description:"Gentle swaying motion"},bouncing:{name:"bouncing",type:"code-based",loop:!0,duration:4e3,description:"Bouncy, playful movement"},gesturing:{name:"gesturing",type:"code-based",loop:!0,duration:5e3,description:"Teaching gesture animation"},walking:{name:"walking",type:"code-based",loop:!0,duration:6e3,description:"Walking motion"},prancing:{name:"prancing",type:"code-based",loop:!0,duration:4e3,description:"Playful prancing movement"},excited:{name:"excited",type:"code-based",loop:!0,duration:5e3,description:"Excited, energetic movement"}},nt=M=>be[M]||null,ot=M=>be.hasOwnProperty(M);exports.CurriculumLearning=Ce;exports.TalkingHeadAvatar=xe;exports.TalkingHeadComponent=ze;exports.animations=be;exports.getActiveTTSConfig=me;exports.getAnimation=nt;exports.getVoiceOptions=it;exports.hasAnimation=ot;
package/dist/index.js CHANGED
@@ -7292,7 +7292,7 @@ const st = be(({
7292
7292
  ttsApiKey: t.ttsApiKey || null,
7293
7293
  bodyMovement: t.bodyMovement || "gesturing",
7294
7294
  movementIntensity: t.movementIntensity || 0.7,
7295
- showFullAvatar: t.showFullAvatar !== void 0 ? t.showFullAvatar : !0,
7295
+ showFullAvatar: t.showFullAvatar !== void 0 ? t.showFullAvatar : !1,
7296
7296
  animations: e,
7297
7297
  lipsyncLang: "en"
7298
7298
  });
@@ -7320,7 +7320,7 @@ const st = be(({
7320
7320
  ttsApiKey: t.ttsApiKey || null,
7321
7321
  bodyMovement: t.bodyMovement || "gesturing",
7322
7322
  movementIntensity: t.movementIntensity || 0.7,
7323
- showFullAvatar: t.showFullAvatar !== void 0 ? t.showFullAvatar : !0,
7323
+ showFullAvatar: t.showFullAvatar !== void 0 ? t.showFullAvatar : !1,
7324
7324
  animations: e,
7325
7325
  lipsyncLang: "en"
7326
7326
  };
@@ -7682,7 +7682,7 @@ const st = be(({
7682
7682
  ttsApiKey: null,
7683
7683
  bodyMovement: "gesturing",
7684
7684
  movementIntensity: 0.7,
7685
- showFullAvatar: !0,
7685
+ showFullAvatar: !1,
7686
7686
  animations: e
7687
7687
  };
7688
7688
  return /* @__PURE__ */ oe("div", { style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ oe(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sage-rsc/talking-head-react",
3
- "version": "1.0.47",
3
+ "version": "1.0.48",
4
4
  "description": "A reusable React component for 3D talking avatars with lip-sync and text-to-speech",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -106,7 +106,7 @@ const CurriculumLearning = forwardRef(({
106
106
  ttsApiKey: avatarConfig.ttsApiKey || null,
107
107
  bodyMovement: avatarConfig.bodyMovement || "gesturing",
108
108
  movementIntensity: avatarConfig.movementIntensity || 0.7,
109
- showFullAvatar: avatarConfig.showFullAvatar !== undefined ? avatarConfig.showFullAvatar : true,
109
+ showFullAvatar: avatarConfig.showFullAvatar !== undefined ? avatarConfig.showFullAvatar : false,
110
110
  animations: animations,
111
111
  lipsyncLang: 'en'
112
112
  });
@@ -141,7 +141,7 @@ const CurriculumLearning = forwardRef(({
141
141
  ttsApiKey: avatarConfig.ttsApiKey || null,
142
142
  bodyMovement: avatarConfig.bodyMovement || "gesturing",
143
143
  movementIntensity: avatarConfig.movementIntensity || 0.7,
144
- showFullAvatar: avatarConfig.showFullAvatar !== undefined ? avatarConfig.showFullAvatar : true,
144
+ showFullAvatar: avatarConfig.showFullAvatar !== undefined ? avatarConfig.showFullAvatar : false,
145
145
  animations: animations,
146
146
  lipsyncLang: 'en'
147
147
  };
@@ -186,7 +186,7 @@ const CurriculumLearning = forwardRef(({
186
186
  let feedbackMessage = `Congratulations! You've completed this lesson`;
187
187
  if (stateRef.current.totalQuestions > 0) {
188
188
  feedbackMessage += ` with a score of ${stateRef.current.score} out of ${stateRef.current.totalQuestions} (${percentage}%). `;
189
- } else {
189
+ } else {
190
190
  feedbackMessage += `! `;
191
191
  }
192
192
 
@@ -321,7 +321,7 @@ const CurriculumLearning = forwardRef(({
321
321
  avatarRef.current.speakText(`Now let me ask you some questions. Here's the first one: ${firstQuestion.question}`, { lipsyncLang: config.lipsyncLang });
322
322
  } else if (firstQuestion.type === 'true_false') {
323
323
  avatarRef.current.speakText(`Let's start with some true or false questions. First question: ${firstQuestion.question}`, { lipsyncLang: config.lipsyncLang });
324
- } else {
324
+ } else {
325
325
  avatarRef.current.speakText(`Now let me ask you some questions. Here's the first one: ${firstQuestion.question}`, { lipsyncLang: config.lipsyncLang });
326
326
  }
327
327
  } else if (avatarRef.current && avatarRef.current.isReady) {
@@ -358,7 +358,7 @@ const CurriculumLearning = forwardRef(({
358
358
  }
359
359
 
360
360
  if (avatarRef.current && nextQuestionObj) {
361
- avatarRef.current.setMood("happy");
361
+ avatarRef.current.setMood("happy");
362
362
  avatarRef.current.setBodyMovement("idle");
363
363
 
364
364
  // Play custom animation if available
@@ -435,9 +435,9 @@ const CurriculumLearning = forwardRef(({
435
435
  lessonIndex: stateRef.current.currentLessonIndex,
436
436
  lesson: getCurrentLesson()
437
437
  });
438
-
439
- if (avatarRef.current) {
440
- avatarRef.current.setMood("happy");
438
+
439
+ if (avatarRef.current) {
440
+ avatarRef.current.setMood("happy");
441
441
  avatarRef.current.setBodyMovement("idle");
442
442
  }
443
443
  } else {
@@ -468,11 +468,11 @@ const CurriculumLearning = forwardRef(({
468
468
  lessonIndex: stateRef.current.currentLessonIndex,
469
469
  lesson: getCurrentLesson()
470
470
  });
471
-
472
- if (avatarRef.current) {
471
+
472
+ if (avatarRef.current) {
473
473
  avatarRef.current.setMood("happy");
474
474
  avatarRef.current.setBodyMovement("idle");
475
- }
475
+ }
476
476
  } else {
477
477
  // No more modules or lessons - complete curriculum
478
478
  if (completeCurriculumRef.current) {
@@ -823,7 +823,7 @@ const CurriculumLearning = forwardRef(({
823
823
  ttsApiKey: null,
824
824
  bodyMovement: "gesturing",
825
825
  movementIntensity: 0.7,
826
- showFullAvatar: true,
826
+ showFullAvatar: false,
827
827
  animations: animations,
828
828
  lipsyncLang: 'en'
829
829
  };