@sage-rsc/talking-head-react 1.0.55 → 1.0.56
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 +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/components/CurriculumLearning.jsx +5 -0
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(),l=await this.audioCtx.decodeAudioData(o);console.log("Analyzing audio for precise lip-sync...");const u=await this.audioAnalyzer.analyzeAudio(l,e);console.log("Azure TTS Audio Analysis:",{text:e,audioDuration:l.duration,visemeCount:u.visemes.length,wordCount:u.words.length,features:{onsets:u.features.onsets.length,boundaries:u.features.phonemeBoundaries.length}});const a=[];for(let r=0;r<u.visemes.length;r++){const c=u.visemes[r],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 h=[...t.anim,...a];this.audioPlaylist.push({anim:h,audio:l}),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,l)=>{l>0&&(e+=" <mark name='"+o.mark+"'/>"),e+=o.word.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',""").replaceAll("'","'").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),l=await this.audioCtx.decodeAudioData(o);this.speakWithHands();const u=[0];let a=0;t.text.forEach((c,d)=>{if(d>0){let g=u[u.length-1];s.timepoints[a]&&(g=s.timepoints[a].timeSeconds*1e3,s.timepoints[a].markName===""+c.mark&&a++),u.push(g)}});const h=[{mark:0,time:0}];u.forEach((c,d)=>{if(d>0){let g=c-u[d-1];h[d-1].duration=g,h.push({mark:d,time:c})}});let r=1e3*l.duration;r>this.opt.ttsTrimEnd&&(r=r-this.opt.ttsTrimEnd),h[h.length-1].duration=r-h[h.length-1].time,t.anim.forEach(c=>{const d=h[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:l}),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 l=t.sampleRate;typeof l=="number"&&l>=8e3&&l<=96e3?l!==this.audioCtx.sampleRate&&this.initAudioGraph(l):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 l=this.audioCtx.audioWorklet.addModule(Je.href),u=new Promise((a,h)=>setTimeout(()=>h(new Error("Worklet loading timed out")),5e3));await Promise.race([l,u]),this.workletLoaded=!0}catch(l){throw console.error("Failed to load audio worklet:",l),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=l=>{if(l.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(u){console.error(u)}if(l.data.type==="playback-ended"&&(this._streamPause(),this.onAudioEnd))try{this.onAudioEnd()}catch{}if(this.onMetrics&&l.data.type==="metrics")try{this.onMetrics(l.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 l=this.audioCtx.resume(),u=new Promise((a,h)=>setTimeout(()=>h("p2"),1e3));try{await Promise.race([l,u])}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],l={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(l)}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 l=this.streamLipsyncLang||this.avatar.lipsyncLang||this.opt.lipsyncLang,u=this.lipsyncPreProcessText(n,l),a=this.lipsyncWordsToVisemes(u,l);if(a&&a.visemes&&a.visemes.length){const h=a.times[a.visemes.length-1]+a.durations[a.visemes.length-1],r=Math.min(o,Math.max(0,o-a.visemes.length*150));let c=.6+this.convertRange(r,[0,o],[0,.4]);if(o=Math.min(o,a.visemes.length*200),h>0)for(let d=0;d<a.visemes.length;d++){const g=e+s+a.times[d]/h*o,y=a.durations[d]/h*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),te.setFromMatrixPosition(this.speakTo.objectLeftEye.matrixWorld),ie.setFromMatrixPosition(this.speakTo.objectRightEye.matrixWorld),e.addVectors(te,ie).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),te.setFromMatrixPosition(this.objectLeftEye.matrixWorld),ie.setFromMatrixPosition(this.objectRightEye.matrixWorld),te.add(ie).divideScalar(2),W.copy(this.armature.quaternion),W.multiply(this.poseTarget.props["Hips.quaternion"]),W.multiply(this.poseTarget.props["Spine.quaternion"]),W.multiply(this.poseTarget.props["Spine1.quaternion"]),W.multiply(this.poseTarget.props["Spine2.quaternion"]),W.multiply(this.poseTarget.props["Neck.quaternion"]),W.multiply(this.poseTarget.props["Head.quaternion"]);const i=new f.Vector3().subVectors(e,te).normalize(),n=Math.atan2(i.x,i.z),s=Math.asin(-i.y);F.set(s,n,0,"YXZ");const l=new f.Quaternion().setFromEuler(F),u=new f.Quaternion().copy(l).multiply(W.clone().invert());F.setFromQuaternion(u,"YXZ");let a=F.x/(40/24)+.2,h=F.y/(9/4),r=Math.min(.6,Math.max(-.3,a)),c=Math.min(.8,Math.max(-.8,h)),d=(Math.random()-.5)/4,g=(Math.random()-.5)/4;if(t){let y=this.animQueue.findIndex(L=>L.template.name==="lookat");y!==-1&&this.animQueue.splice(y,1);const x={name:"lookat",dt:[750,t],vs:{bodyRotateX:[r+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),l=new f.Vector3().addVectors(s,o).divideScalar(2);l.project(this.camera);let u=(l.x+1)/2*n.width+n.left,a=-(l.y-1)/2*n.height+n.top;t===null&&(t=u),e===null&&(e=a),W.copy(this.armature.quaternion),W.multiply(this.poseTarget.props["Hips.quaternion"]),W.multiply(this.poseTarget.props["Spine.quaternion"]),W.multiply(this.poseTarget.props["Spine1.quaternion"]),W.multiply(this.poseTarget.props["Spine2.quaternion"]),W.multiply(this.poseTarget.props["Neck.quaternion"]),W.multiply(this.poseTarget.props["Head.quaternion"]),F.setFromQuaternion(W);let h=F.x/(40/24),r=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-u,u),y=Math.max(window.innerHeight-a,a),x=this.convertRange(e,[a-y,a+y],[-.3,.6])-h+c,L=this.convertRange(t,[u-g,u+g],[-.8,.8])-r+d;x=Math.min(.6,Math.max(-.3,x)),L=Math.min(.8,Math.max(-.8,L));let P=(Math.random()-.5)/4,p=(Math.random()-.5)/4;if(i){let T=this.animQueue.findIndex(I=>I.template.name==="lookat");T!==-1&&this.animQueue.splice(T,1);const M={name:"lookat",dt:[750,i],vs:{bodyRotateX:[x+P],bodyRotateY:[L+p],eyesRotateX:[-3*P+.1],eyesRotateY:[-5*p],browInnerUp:[[0,.7]],mouthLeft:[[0,.7]],mouthRight:[[0,.7]],eyeContact:[0],headMove:[0]}};this.animQueue.push(this.animFactory(M))}}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 l=o[0].point,u=new f.Vector3,a=new f.Vector3;this.objectLeftArm.getWorldPosition(u),this.objectRightArm.getWorldPosition(a);const h=u.distanceToSquared(l),r=a.distanceToSquared(l);h<r?(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}]},l,!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}]},l,!1,1e3),this.setValue("handFistRight",0))}else["LeftArm","LeftForeArm","LeftHand","RightArm","RightForeArm","RightHand"].forEach(l=>{let u=l+".quaternion";this.poseTarget.props[u].copy(this.getPoseTemplateProp(u)),this.poseTarget.props[u].t=this.animClock,this.poseTarget.props[u].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 l=this.animClips.find(u=>u.url===t+"-"+n);if(l){let u=this.animQueue.find(r=>r.template.name==="pose");u&&(u.ts[0]=1/0),Object.entries(l.pose.props).forEach(r=>{this.poseBase.props[r[0]]=r[1].clone(),this.poseTarget.props[r[0]]=r[1].clone(),this.poseTarget.props[r[0]].t=0,this.poseTarget.props[r[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/l.clip.duration),h=this.mixer.clipAction(l.clip);h.setLoop(f.LoopRepeat,a),h.clampWhenFinished=!0,this.currentFBXAction=h;try{h.fadeIn(.5).play(),console.log("FBX animation started successfully:",t)}catch(r){console.warn("FBX animation failed to start:",r),this.stopAnimation();return}if(h.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 h=new be.FBXLoader;let r;try{r=await h.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(r&&r.animations&&r.animations[n]){let c=r.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 L=0;L<y.values.length;L++)y.values[L]=y.values[L]*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),r&&r.animations?console.error(`FBX file loaded but has ${r.animations.length} animation(s), requested index ${n}`):console.error(r?"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 l=this.animPoses.find(u=>u.url===t+"-"+n);l&&(o=l.pose)}if(o){this.poseName=t,this.mixer=null;let l=this.animQueue.find(u=>u.template.name==="pose");l&&(l.ts[0]=this.animClock+i*1e3+2e3),this.setPoseFromTemplate(o)}else{let u=await new be.FBXLoader().loadAsync(t,e);if(u&&u.animations&&u.animations[n]){let a=u.animations[n];const h={};a.tracks.forEach(c=>{c.name=c.name.replaceAll("mixamorig","");const d=c.name.split(".");d[1]==="position"?h[c.name]=new f.Vector3(c.values[0]*s,c.values[1]*s,c.values[2]*s):d[1]==="quaternion"?h[c.name]=new f.Quaternion(c.values[0],c.values[1],c.values[2],c.values[3]):d[1]==="rotation"&&(h[d[0]+".quaternion"]=new f.Quaternion().setFromEuler(new f.Euler(c.values[0],c.values[1],c.values[2],"XYZ")).normalize())});const r={props:h};h["Hips.position"]&&(h["Hips.position"].y<.5?r.lying=!0:r.standing=!0),this.animPoses.push({url:t+"-"+n,pose:r}),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 l=this.animQueue.findIndex(u=>u.template.name==="talkinghands");l!==-1&&(this.animQueue[l].ts=this.animQueue[l].ts.map(u=>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[u,a]of Object.entries(this.gesture))a.t=this.animClock,a.d=n,this.poseTarget.props.hasOwnProperty(u)&&(this.poseTarget.props[u].copy(a),this.poseTarget.props[u].t=this.animClock,this.poseTarget.props[u].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 l=this.animFactory(o);if(l.gesture=!0,e&&Number.isFinite(e)){const u=l.ts[0],h=l.ts[l.ts.length-1]-u;if(e*1e3-h>0){const c=[];for(let y=1;y<l.ts.length;y++)c.push(l.ts[y]-l.ts[y-1]);const d=o.template?.rescale||c.map(y=>y/h),g=e*1e3-h;l.ts=l.ts.map((y,x,L)=>x===0?u:L[x-1]+c[x-1]+d[x-1]*g)}else{const c=e*1e3/h;l.ts=l.ts.map(d=>u+c*(d-u))}}this.animQueue.push(l)}}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,l=new f.Vector3,u=new f.Vector3,a=new f.Quaternion,h=new f.Vector3,r=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(L=>{L.bone=this.ikMesh.getObjectByName(L.link),L.bone.quaternion.copy(this.getPoseTemplateProp(L.link+".quaternion"))}),d.updateMatrixWorld(!0);const x=t.iterations||10;if(e)for(let L=0;L<x;L++){let P=!1;for(let p=0,T=y.length;p<T;p++){const M=y[p].bone;M.matrixWorld.decompose(u,a,h),a.invert(),o.setFromMatrixPosition(g.matrixWorld),l.subVectors(o,u),l.applyQuaternion(a),l.normalize(),s.subVectors(e,u),s.applyQuaternion(a),s.normalize();let I=s.dot(l);I>1?I=1:I<-1&&(I=-1),I=Math.acos(I),!(I<1e-5)&&(y[p].minAngle!==void 0&&I<y[p].minAngle&&(I=y[p].minAngle),y[p].maxAngle!==void 0&&I>y[p].maxAngle&&(I=y[p].maxAngle),r.crossVectors(l,s),r.normalize(),W.setFromAxisAngle(r,I),M.quaternion.multiply(W),M.rotation.setFromVector3(c.setFromEuler(M.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))),M.updateMatrixWorld(!0),P=!0)}if(!P)break}n&&y.forEach(L=>{this.poseTarget.props[L.link+".quaternion"].copy(L.bone.quaternion),this.poseTarget.props[L.link+".quaternion"].t=this.animClock,this.poseTarget.props[L.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"}},Le={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 ce(){return{service:"elevenlabs",endpoint:se.endpoint,apiKey:se.apiKey,defaultVoice:se.defaultVoice,voices:se.voices}}function et(){const E=ce(),t=[];return Object.entries(E.voices).forEach(([e,i])=>{t.push({value:i,label:`${e.charAt(0).toUpperCase()+e.slice(1)} (${E.service})`})}),t}const ye=A.forwardRef(({avatarUrl:E="/avatars/brunette.glb",avatarBody:t="F",mood:e="neutral",ttsLang:i="en",ttsService:n=null,ttsVoice:s=null,ttsApiKey:o=null,bodyMovement:l="idle",movementIntensity:u=.5,showFullAvatar:a=!0,cameraView:h="upper",onReady:r=()=>{},onLoading:c=()=>{},onError:d=()=>{},className:g="",style:y={},animations:x={}},L)=>{const P=A.useRef(null),p=A.useRef(null),T=A.useRef(a),[M,I]=A.useState(!0),[D,O]=A.useState(null),[X,ne]=A.useState(!1);A.useEffect(()=>{T.current=a},[a]);const S=ce(),B=n||S.service;let N;B==="browser"?N={service:"browser",endpoint:"",apiKey:null,defaultVoice:"Google US English"}:B==="elevenlabs"?N={service:"elevenlabs",endpoint:"https://api.elevenlabs.io/v1/text-to-speech",apiKey:o||S.apiKey,defaultVoice:s||S.defaultVoice||se.defaultVoice,voices:S.voices||se.voices}:B==="deepgram"?N={service:"deepgram",endpoint:"https://api.deepgram.com/v1/speak",apiKey:o||S.apiKey,defaultVoice:s||S.defaultVoice||Le.defaultVoice,voices:S.voices||Le.voices}:N={...S,apiKey:o!==null?o:S.apiKey};const V={url:E,body:t,avatarMood:e,ttsLang:B==="browser"?"en-US":i,ttsVoice:s||N.defaultVoice,lipsyncLang:"en",showFullAvatar:a,bodyMovement:l,movementIntensity:u},q={ttsEndpoint:N.endpoint,ttsApikey:N.apiKey,ttsService:B,lipsyncModules:["en"],cameraView:h},_=A.useCallback(async()=>{if(!(!P.current||p.current))try{if(I(!0),O(null),p.current=new Ae(P.current,q),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(V,z=>{if(z.lengthComputable){const H=Math.min(100,Math.round(z.loaded/z.total*100));c(H)}}),await new Promise(z=>{const H=()=>{p.current.lipsync&&Object.keys(p.current.lipsync).length>0?z():setTimeout(H,100)};H()}),p.current&&p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(a)}catch(z){console.warn("Error setting full body mode on initialization:",z)}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()),I(!1),ne(!0),r(p.current);const w=()=>{document.visibilityState==="visible"?p.current?.start():p.current?.stop()};return document.addEventListener("visibilitychange",w),()=>{document.removeEventListener("visibilitychange",w)}}catch(R){console.error("Error initializing TalkingHead:",R),O(R.message||"Failed to initialize avatar"),I(!1),d(R)}},[E,t,e,i,n,s,o,a,l,u,h]);A.useEffect(()=>(_(),()=>{p.current&&(p.current.stop(),p.current.dispose(),p.current=null)}),[_]),A.useEffect(()=>{if(!P.current||!p.current)return;const R=new ResizeObserver(z=>{for(const H of z)p.current&&p.current.onResize&&p.current.onResize()});R.observe(P.current);const w=()=>{p.current&&p.current.onResize&&p.current.onResize()};return window.addEventListener("resize",w),()=>{R.disconnect(),window.removeEventListener("resize",w)}},[X]);const oe=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(R){console.warn("Failed to resume audio context:",R)}},[]),de=A.useCallback(async(R,w={})=>{if(p.current&&X)try{await oe();const z={...w,lipsyncLang:w.lipsyncLang||V.lipsyncLang||"en"};if(w.onSpeechEnd&&p.current){const H=p.current;let U=null,K=0;const pe=1200;let ae=!1;U=setInterval(()=>{if(K++,K>pe){if(U&&(clearInterval(U),U=null),!ae){ae=!0;try{w.onSpeechEnd()}catch(xe){console.error("Error in onSpeechEnd callback (timeout):",xe)}}return}const we=!H.speechQueue||H.speechQueue.length===0,Ce=!H.audioPlaylist||H.audioPlaylist.length===0;H&&H.isSpeaking===!1&&we&&Ce&&H.isAudioPlaying===!1&&!ae&&setTimeout(()=>{if(H&&H.isSpeaking===!1&&(!H.speechQueue||H.speechQueue.length===0)&&(!H.audioPlaylist||H.audioPlaylist.length===0)&&H.isAudioPlaying===!1&&!ae){ae=!0,U&&(clearInterval(U),U=null);try{w.onSpeechEnd()}catch(ze){console.error("Error in onSpeechEnd callback:",ze)}}},100)},100)}p.current.lipsync&&Object.keys(p.current.lipsync).length>0?(p.current.setSlowdownRate&&p.current.setSlowdownRate(1.05),p.current.speakText(R,z)):setTimeout(async()=>{await oe(),p.current&&p.current.lipsync&&(p.current.setSlowdownRate&&p.current.setSlowdownRate(1.05),p.current.speakText(R,z))},100)}catch(z){console.error("Error speaking text:",z),O(z.message||"Failed to speak text")}},[X,oe,V.lipsyncLang]),re=A.useCallback(()=>{p.current&&(p.current.stopSpeaking(),p.current.setSlowdownRate&&p.current.setSlowdownRate(1))},[]),me=A.useCallback(R=>{p.current&&p.current.setMood(R)},[]),j=A.useCallback(R=>{p.current&&p.current.setSlowdownRate&&p.current.setSlowdownRate(R)},[]),b=A.useCallback((R,w=!1)=>{if(p.current&&p.current.playAnimation){if(x&&x[R]&&(R=x[R]),p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(T.current)}catch(H){console.warn("Error setting full body mode:",H)}if(R.includes("."))try{p.current.playAnimation(R,null,10,0,.01,w)}catch(H){console.warn(`Failed to play ${R}:`,H);try{p.current.setBodyMovement("idle")}catch(U){console.warn("Fallback animation also failed:",U)}}else{const H=[".fbx",".glb",".gltf"];let U=!1;for(const K of H)try{p.current.playAnimation(R+K,null,10,0,.01,w),U=!0;break}catch{}if(!U){console.warn("Animation not found:",R);try{p.current.setBodyMovement("idle")}catch(K){console.warn("Fallback animation also failed:",K)}}}}},[x]),v=A.useCallback(()=>{p.current&&p.current.onResize&&p.current.onResize()},[]);return A.useImperativeHandle(L,()=>({speakText:de,stopSpeaking:re,resumeAudioContext:oe,setMood:me,setTimingAdjustment:j,playAnimation:b,isReady:X,talkingHead:p.current,handleResize:v,setBodyMovement:R=>{if(p.current&&p.current.setShowFullAvatar&&p.current.setBodyMovement)try{p.current.setShowFullAvatar(T.current),p.current.setBodyMovement(R)}catch(w){console.warn("Error setting body movement:",w)}},setMovementIntensity:R=>p.current?.setMovementIntensity(R),playRandomDance:()=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playRandomDance)try{p.current.setShowFullAvatar(T.current),p.current.playRandomDance()}catch(R){console.warn("Error playing random dance:",R)}},playReaction:R=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playReaction)try{p.current.setShowFullAvatar(T.current),p.current.playReaction(R)}catch(w){console.warn("Error playing reaction:",w)}},playCelebration:()=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playCelebration)try{p.current.setShowFullAvatar(T.current),p.current.playCelebration()}catch(R){console.warn("Error playing celebration:",R)}},setShowFullAvatar:R=>{if(p.current&&p.current.setShowFullAvatar)try{T.current=R,p.current.setShowFullAvatar(R)}catch(w){console.warn("Error setting showFullAvatar:",w)}},lockAvatarPosition:()=>{if(p.current&&p.current.lockAvatarPosition)try{p.current.lockAvatarPosition()}catch(R){console.warn("Error locking avatar position:",R)}},unlockAvatarPosition:()=>{if(p.current&&p.current.unlockAvatarPosition)try{p.current.unlockAvatarPosition()}catch(R){console.warn("Error unlocking avatar position:",R)}}})),ee.jsxs("div",{className:`talking-head-avatar ${g}`,style:{width:"100%",height:"100%",position:"relative",...y},children:[ee.jsx("div",{ref:P,className:"talking-head-viewer",style:{width:"100%",height:"100%",minHeight:"400px"}}),M&&ee.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..."}),D&&ee.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:D})]})});ye.displayName="TalkingHeadAvatar";const ke=A.forwardRef(({text:E="Hello! I'm a talking avatar. How are you today?",onLoading:t=()=>{},onError:e=()=>{},onReady:i=()=>{},className:n="",style:s={},avatarConfig:o={}},l)=>{const u=A.useRef(null),a=A.useRef(null),[h,r]=A.useState(!0),[c,d]=A.useState(null),[g,y]=A.useState(!1),x=ce(),L=o.ttsService||x.service,P=L==="browser"?{endpoint:"",apiKey:null,defaultVoice:"Google US English"}:{...x,apiKey:o.ttsApiKey!==void 0&&o.ttsApiKey!==null?o.ttsApiKey:x.apiKey,endpoint:L==="elevenlabs"&&o.ttsApiKey?"https://api.elevenlabs.io/v1/text-to-speech":x.endpoint},p={url:"/avatars/brunette.glb",body:"F",avatarMood:"neutral",ttsLang:L==="browser"?"en-US":"en",ttsVoice:o.ttsVoice||P.defaultVoice,lipsyncLang:"en",showFullAvatar:!0,bodyMovement:"idle",movementIntensity:.5,...o},T={ttsEndpoint:P.endpoint,ttsApikey:P.apiKey,ttsService:L,lipsyncModules:["en"],cameraView:"upper"},M=A.useCallback(async()=>{if(!(!u.current||a.current))try{if(r(!0),d(null),a.current=new Ae(u.current,T),await a.current.showAvatar(p,N=>{if(N.lengthComputable){const V=Math.min(100,Math.round(N.loaded/N.total*100));t(V)}}),a.current.morphs&&a.current.morphs.length>0){const N=a.current.morphs[0].morphTargetDictionary;console.log("Available morph targets:",Object.keys(N));const V=Object.keys(N).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(N=>{const V=()=>{a.current.lipsync&&Object.keys(a.current.lipsync).length>0?(console.log("Lip-sync modules loaded:",Object.keys(a.current.lipsync)),N()):(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(N){console.warn("Error setting full body mode on initialization:",N)}r(!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(S){console.error("Error initializing TalkingHead:",S),d(S.message||"Failed to initialize avatar"),r(!1),e(S)}},[]);A.useEffect(()=>(M(),()=>{a.current&&(a.current.stop(),a.current.dispose(),a.current=null)}),[M]);const I=A.useCallback(S=>{if(a.current&&g)try{console.log("Speaking text:",S),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(S)):(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(S)):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]),D=A.useCallback(()=>{a.current&&(a.current.stopSpeaking(),a.current.setSlowdownRate&&(a.current.setSlowdownRate(1),console.log("Reset timing to normal")))},[]),O=A.useCallback(S=>{a.current&&a.current.setMood(S)},[]),X=A.useCallback(S=>{a.current&&a.current.setSlowdownRate&&(a.current.setSlowdownRate(S),console.log("Timing adjustment set to:",S))},[]),ne=A.useCallback((S,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(S.includes("."))try{a.current.playAnimation(S,null,10,0,.01,B),console.log("Playing animation:",S)}catch(V){console.log(`Failed to play ${S}:`,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 _ of V)try{a.current.playAnimation(S+_,null,10,0,.01,B),console.log("Playing animation:",S+_),q=!0;break}catch{console.log(`Failed to play ${S}${_}, trying next format...`)}if(!q){console.warn("Animation system not available or animation not found:",S);try{a.current.setBodyMovement("idle"),console.log("Fallback to idle animation")}catch(_){console.warn("Fallback animation also failed:",_)}}}}else console.warn("Animation system not available or animation not found:",S)},[]);return A.useImperativeHandle(l,()=>({speakText:I,stopSpeaking:D,setMood:O,setTimingAdjustment:X,playAnimation:ne,isReady:g,talkingHead:a.current,setBodyMovement:S=>{if(a.current&&a.current.setShowFullAvatar&&a.current.setBodyMovement)try{a.current.setShowFullAvatar(!0),a.current.setBodyMovement(S),console.log("Body movement set with full body mode:",S)}catch(B){console.warn("Error setting body movement:",B)}},setMovementIntensity:S=>a.current?.setMovementIntensity(S),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(S){console.warn("Error playing random dance:",S)}},playReaction:S=>{if(a.current&&a.current.setShowFullAvatar&&a.current.playReaction)try{a.current.setShowFullAvatar(!0),a.current.playReaction(S),console.log("Reaction played with full body mode:",S)}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(S){console.warn("Error playing celebration:",S)}},setShowFullAvatar:S=>{if(a.current&&a.current.setShowFullAvatar)try{a.current.setShowFullAvatar(S),console.log("Show full avatar set to:",S)}catch(B){console.warn("Error setting showFullAvatar:",B)}},lockAvatarPosition:()=>{if(a.current&&a.current.lockAvatarPosition)try{a.current.lockAvatarPosition()}catch(S){console.warn("Error locking avatar position:",S)}},unlockAvatarPosition:()=>{if(a.current&&a.current.unlockAvatarPosition)try{a.current.unlockAvatarPosition()}catch(S){console.warn("Error unlocking avatar position:",S)}}})),ee.jsxs("div",{className:`talking-head-container ${n}`,style:s,children:[ee.jsx("div",{ref:u,className:"talking-head-viewer",style:{width:"100%",height:"100%",minHeight:"400px"}}),h&&ee.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&&ee.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})]})});ke.displayName="TalkingHeadComponent";const Se=A.forwardRef(({curriculumData:E=null,avatarConfig:t={},animations:e={},onLessonStart:i=()=>{},onLessonComplete:n=()=>{},onQuestionAnswer:s=()=>{},onCurriculumComplete:o=()=>{},onCustomAction:l=()=>{},autoStart:u=!1},a)=>{const h=A.useRef(null),r=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:l}),d=A.useRef(null),g=A.useRef(null),y=A.useRef(null),x=A.useRef(null),L=A.useRef(null),P=A.useRef(null),p=A.useRef(null),T=A.useRef(E?.curriculum||{title:"Default Curriculum",description:"No curriculum data provided",language:"en",modules:[]}),M=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:l}},[i,n,s,o,l]),A.useEffect(()=>{T.current=E?.curriculum||{title:"Default Curriculum",description:"No curriculum data provided",language:"en",modules:[]},M.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"}},[E,t,e]);const I=A.useCallback(()=>(T.current||{modules:[]}).modules[r.current.currentModuleIndex]?.lessons[r.current.currentLessonIndex],[]),D=A.useCallback(()=>I()?.questions[r.current.currentQuestionIndex],[I]),O=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,[]),X=A.useCallback(()=>{r.current.lessonCompleted=!0,r.current.isQuestionMode=!1;const b=r.current.totalQuestions>0?Math.round(r.current.score/r.current.totalQuestions*100):100;let v="Congratulations! You've completed this lesson";if(r.current.totalQuestions>0?v+=` with a score of ${r.current.score} out of ${r.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:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,score:r.current.score,totalQuestions:r.current.totalQuestions,percentage:b}),c.current.onCustomAction({type:"lessonComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,score:r.current.score,totalQuestions:r.current.totalQuestions,percentage:b}),h.current){if(h.current.setMood("happy"),e.lessonComplete)try{h.current.playAnimation(e.lessonComplete,!0)}catch{h.current.playCelebration()}const R=T.current||{modules:[]},w=R.modules[r.current.currentModuleIndex],z=r.current.currentLessonIndex<(w?.lessons?.length||0)-1,H=r.current.currentModuleIndex<(R.modules?.length||0)-1,U=z||H,K=M.current||{lipsyncLang:"en"};h.current.speakText(v,{lipsyncLang:K.lipsyncLang,onSpeechEnd:()=>{c.current.onCustomAction({type:"lessonCompleteFeedbackDone",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,score:r.current.score,totalQuestions:r.current.totalQuestions,percentage:b,hasNextLesson:U})}})}},[e.lessonComplete]),ne=A.useCallback(()=>{r.current.curriculumCompleted=!0;const b=T.current||{modules:[]};if(c.current.onCurriculumComplete({modules:b.modules.length,totalLessons:b.modules.reduce((v,R)=>v+R.lessons.length,0)}),h.current){if(h.current.setMood("celebrating"),e.curriculumComplete)try{h.current.playAnimation(e.curriculumComplete,!0)}catch{h.current.playCelebration()}const v=M.current||{lipsyncLang:"en"};h.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]),S=A.useCallback(()=>{const b=I();r.current.isQuestionMode=!0,r.current.currentQuestionIndex=0,r.current.totalQuestions=b?.questions?.length||0,r.current.score=0;const v=D();v&&c.current.onCustomAction({type:"questionStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,totalQuestions:r.current.totalQuestions,question:v,score:r.current.score});const R=()=>{if(!h.current||!v)return;if(h.current.setMood("happy"),e.questionStart)try{h.current.playAnimation(e.questionStart,!0)}catch(z){console.warn("Failed to play questionStart animation:",z)}const w=M.current||{lipsyncLang:"en"};v.type==="code_test"?h.current.speakText(`Let's test your coding skills! Here's your first challenge: ${v.question}`,{lipsyncLang:w.lipsyncLang}):v.type==="multiple_choice"?h.current.speakText(`Now let me ask you some questions. Here's the first one: ${v.question}`,{lipsyncLang:w.lipsyncLang}):v.type==="true_false"?h.current.speakText(`Let's start with some true or false questions. First question: ${v.question}`,{lipsyncLang:w.lipsyncLang}):h.current.speakText(`Now let me ask you some questions. Here's the first one: ${v.question}`,{lipsyncLang:w.lipsyncLang})};if(h.current&&h.current.isReady&&v)R();else if(h.current&&h.current.isReady){const w=M.current||{lipsyncLang:"en"};h.current.speakText("Now let me ask you some questions to test your understanding.",{lipsyncLang:w.lipsyncLang})}else{const w=setInterval(()=>{h.current&&h.current.isReady&&(clearInterval(w),v&&R())},100);setTimeout(()=>{clearInterval(w)},5e3)}},[e.questionStart,I,D]),B=A.useCallback(()=>{const b=I();if(r.current.currentQuestionIndex<(b?.questions?.length||0)-1){r.current.currentQuestionIndex+=1;const v=D();v&&c.current.onCustomAction({type:"nextQuestion",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,totalQuestions:r.current.totalQuestions,question:v,score:r.current.score});const R=()=>{if(!h.current||!v)return;if(h.current.setMood("happy"),h.current.setBodyMovement("idle"),e.nextQuestion)try{h.current.playAnimation(e.nextQuestion,!0)}catch(z){console.warn("Failed to play nextQuestion animation:",z)}const w=M.current||{lipsyncLang:"en"};v.type==="code_test"?h.current.speakText(`Great! Now let's move on to your next coding challenge: ${v.question}`,{lipsyncLang:w.lipsyncLang}):v.type==="multiple_choice"?h.current.speakText(`Alright! Here's your next question: ${v.question}`,{lipsyncLang:w.lipsyncLang}):v.type==="true_false"?h.current.speakText(`Now let's try this one: ${v.question}`,{lipsyncLang:w.lipsyncLang}):h.current.speakText(`Here's the next question: ${v.question}`,{lipsyncLang:w.lipsyncLang})};if(h.current&&h.current.isReady&&v)R();else if(v){const w=setInterval(()=>{h.current&&h.current.isReady&&(clearInterval(w),R())},100);setTimeout(()=>{clearInterval(w)},5e3)}}else c.current.onCustomAction({type:"allQuestionsComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,totalQuestions:r.current.totalQuestions,score:r.current.score})},[e.nextQuestion,I,D]),N=A.useCallback(()=>{const b=T.current||{modules:[]},v=b.modules[r.current.currentModuleIndex];if(r.current.currentLessonIndex<(v?.lessons?.length||0)-1){r.current.currentLessonIndex+=1,r.current.currentQuestionIndex=0,r.current.lessonCompleted=!1,r.current.isQuestionMode=!1,r.current.isTeaching=!1,r.current.score=0,r.current.totalQuestions=0;const w=b.modules[r.current.currentModuleIndex],z=r.current.currentLessonIndex<(w?.lessons?.length||0)-1,H=r.current.currentModuleIndex<(b.modules?.length||0)-1,U=z||H;c.current.onCustomAction({type:"lessonStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,hasNextLesson:U}),c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:I()}),h.current&&(h.current.setMood("happy"),h.current.setBodyMovement("idle"))}else if(r.current.currentModuleIndex<(b.modules?.length||0)-1){r.current.currentModuleIndex+=1,r.current.currentLessonIndex=0,r.current.currentQuestionIndex=0,r.current.lessonCompleted=!1,r.current.isQuestionMode=!1,r.current.isTeaching=!1,r.current.score=0,r.current.totalQuestions=0;const z=b.modules[r.current.currentModuleIndex],H=r.current.currentLessonIndex<(z?.lessons?.length||0)-1,U=r.current.currentModuleIndex<(b.modules?.length||0)-1,K=H||U;c.current.onCustomAction({type:"lessonStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,hasNextLesson:K}),c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:I()}),h.current&&(h.current.setMood("happy"),h.current.setBodyMovement("idle"))}else L.current&&L.current()},[]),V=A.useCallback(()=>{const b=I();let v=null;if(b?.avatar_script&&b?.body){const R=b.avatar_script.trim(),w=b.body.trim(),z=R.match(/[.!?]$/)?" ":". ";v=`${R}${z}${w}`}else v=b?.avatar_script||b?.body||null;if(h.current&&h.current.isReady&&v){r.current.isTeaching=!0,r.current.isQuestionMode=!1,r.current.score=0,r.current.totalQuestions=0,h.current.setMood("happy");let R=!1;if(e.teaching)try{h.current.playAnimation(e.teaching,!0),R=!0}catch(z){console.warn("Failed to play teaching animation:",z)}R||h.current.setBodyMovement("gesturing");const w=M.current||{lipsyncLang:"en"};c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:b}),c.current.onCustomAction({type:"teachingStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:b}),h.current.speakText(v,{lipsyncLang:w.lipsyncLang,onSpeechEnd:()=>{r.current.isTeaching=!1,c.current.onCustomAction({type:"teachingComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:b,hasQuestions:b.questions&&b.questions.length>0})}})}},[e.teaching,I]),q=A.useCallback(b=>{const v=D(),R=O(b,v);if(R&&(r.current.score+=1),c.current.onQuestionAnswer({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,answer:b,isCorrect:R,question:v}),h.current)if(R){if(h.current.setMood("happy"),e.correct)try{h.current.playReaction("happy")}catch{h.current.setBodyMovement("happy")}h.current.setBodyMovement("gesturing");const w=v.type==="code_test"?`Great job! Your code passed all the tests! ${v.explanation||""}`:`Excellent! That's correct! ${v.explanation||""}`,z=M.current||{lipsyncLang:"en"};h.current.speakText(w,{lipsyncLang:z.lipsyncLang,onSpeechEnd:()=>{const U=I()?.questions?.length||0;c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,isCorrect:!0,hasNextQuestion:r.current.currentQuestionIndex<U-1,score:r.current.score,totalQuestions:r.current.totalQuestions})}})}else{if(h.current.setMood("sad"),e.incorrect)try{h.current.playAnimation(e.incorrect,!0)}catch{h.current.setBodyMovement("idle")}h.current.setBodyMovement("gesturing");const w=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.`,z=M.current||{lipsyncLang:"en"};h.current.speakText(w,{lipsyncLang:z.lipsyncLang,onSpeechEnd:()=>{const U=I()?.questions?.length||0;c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,isCorrect:!1,hasNextQuestion:r.current.currentQuestionIndex<U-1,score:r.current.score,totalQuestions:r.current.totalQuestions})}})}else{const z=I()?.questions?.length||0;c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,isCorrect:R,hasNextQuestion:r.current.currentQuestionIndex<z-1,score:r.current.score,totalQuestions:r.current.totalQuestions,avatarNotReady:!0})}},[e.correct,e.incorrect,D,I,O]),_=A.useCallback(b=>{const v=D();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 R={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:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,testResult:R,question:v}),p.current&&p.current(R)},[D,O]),oe=A.useCallback(()=>{if(r.current.currentQuestionIndex>0){r.current.currentQuestionIndex-=1;const b=D();b&&c.current.onCustomAction({type:"questionStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,totalQuestions:r.current.totalQuestions,question:b,score:r.current.score});const v=()=>{if(!h.current||!b)return;h.current.setMood("happy"),h.current.setBodyMovement("idle");const R=M.current||{lipsyncLang:"en"};b.type==="code_test"?h.current.speakText(`Let's go back to this coding challenge: ${b.question}`,{lipsyncLang:R.lipsyncLang}):h.current.speakText(`Going back to: ${b.question}`,{lipsyncLang:R.lipsyncLang})};if(h.current&&h.current.isReady&&b)v();else if(b){const R=setInterval(()=>{h.current&&h.current.isReady&&(clearInterval(R),v())},100);setTimeout(()=>{clearInterval(R)},5e3)}}},[D]),de=A.useCallback(()=>{const b=T.current||{modules:[]};if(b.modules[r.current.currentModuleIndex],r.current.currentLessonIndex>0)r.current.currentLessonIndex-=1,r.current.currentQuestionIndex=0,r.current.lessonCompleted=!1,r.current.isQuestionMode=!1,r.current.isTeaching=!1,r.current.score=0,r.current.totalQuestions=0,c.current.onCustomAction({type:"lessonStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex}),c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:I()}),h.current&&(h.current.setMood("happy"),h.current.setBodyMovement("idle"));else if(r.current.currentModuleIndex>0){const w=b.modules[r.current.currentModuleIndex-1];r.current.currentModuleIndex-=1,r.current.currentLessonIndex=(w?.lessons?.length||1)-1,r.current.currentQuestionIndex=0,r.current.lessonCompleted=!1,r.current.isQuestionMode=!1,r.current.isTeaching=!1,r.current.score=0,r.current.totalQuestions=0,c.current.onCustomAction({type:"lessonStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex}),c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:I()}),h.current&&(h.current.setMood("happy"),h.current.setBodyMovement("idle"))}},[I]),re=A.useCallback(()=>{r.current.currentModuleIndex=0,r.current.currentLessonIndex=0,r.current.currentQuestionIndex=0,r.current.isTeaching=!1,r.current.isQuestionMode=!1,r.current.lessonCompleted=!1,r.current.curriculumCompleted=!1,r.current.score=0,r.current.totalQuestions=0},[]),me=A.useCallback(b=>{console.log("Avatar is ready!",b);const v=I(),R=v?.avatar_script||v?.body;u&&R&&setTimeout(()=>{d.current&&d.current()},10)},[u,I]);A.useLayoutEffect(()=>{d.current=V,g.current=N,y.current=X,x.current=B,L.current=ne,P.current=S,p.current=q}),A.useImperativeHandle(a,()=>({startTeaching:V,startQuestions:S,handleAnswerSelect:q,handleCodeTestResult:_,nextQuestion:B,previousQuestion:oe,nextLesson:N,previousLesson:de,completeLesson:X,completeCurriculum:ne,resetCurriculum:re,getState:()=>({...r.current}),getCurrentQuestion:()=>D(),getCurrentLesson:()=>I(),getAvatarRef:()=>h.current,speakText:async(b,v={})=>{await h.current?.resumeAudioContext?.();const R=M.current||{lipsyncLang:"en"};h.current?.speakText(b,{...v,lipsyncLang:v.lipsyncLang||R.lipsyncLang})},resumeAudioContext:async()=>{if(h.current?.resumeAudioContext)return await h.current.resumeAudioContext();const b=h.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(R){console.warn("Failed to resume audio context:",R)}}else console.warn("Audio context not available yet")},stopSpeaking:()=>h.current?.stopSpeaking(),setMood:b=>h.current?.setMood(b),playAnimation:(b,v)=>h.current?.playAnimation(b,v),setBodyMovement:b=>h.current?.setBodyMovement(b),setMovementIntensity:b=>h.current?.setMovementIntensity(b),playRandomDance:()=>h.current?.playRandomDance(),playReaction:b=>h.current?.playReaction(b),playCelebration:()=>h.current?.playCelebration(),setShowFullAvatar:b=>h.current?.setShowFullAvatar(b),setTimingAdjustment:b=>h.current?.setTimingAdjustment(b),lockAvatarPosition:()=>h.current?.lockAvatarPosition(),unlockAvatarPosition:()=>h.current?.unlockAvatarPosition(),triggerCustomAction:(b,v)=>{c.current.onCustomAction({type:b,...v,state:{...r.current}})},handleResize:()=>h.current?.handleResize(),isAvatarReady:()=>h.current?.isReady||!1}),[V,S,q,_,B,N,X,ne,re,D,I]);const j=M.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 ee.jsx("div",{style:{width:"100%",height:"100%"},children:ee.jsx(ye,{ref:h,avatarUrl:j.avatarUrl,avatarBody:j.avatarBody,mood:j.mood,ttsLang:j.ttsLang,ttsService:j.ttsService,ttsVoice:j.ttsVoice,ttsApiKey:j.ttsApiKey,bodyMovement:j.bodyMovement,movementIntensity:j.movementIntensity,showFullAvatar:j.showFullAvatar,cameraView:"upper",animations:j.animations,onReady:me,onLoading:()=>{},onError:b=>{console.error("Avatar error:",b)}})})});Se.displayName="CurriculumLearning";const fe={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"}},tt=E=>fe[E]||null,it=E=>fe.hasOwnProperty(E);exports.CurriculumLearning=Se;exports.TalkingHeadAvatar=ye;exports.TalkingHeadComponent=ke;exports.animations=fe;exports.getActiveTTSConfig=ce;exports.getAnimation=tt;exports.getVoiceOptions=et;exports.hasAnimation=it;
|
|
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(),l=await this.audioCtx.decodeAudioData(o);console.log("Analyzing audio for precise lip-sync...");const u=await this.audioAnalyzer.analyzeAudio(l,e);console.log("Azure TTS Audio Analysis:",{text:e,audioDuration:l.duration,visemeCount:u.visemes.length,wordCount:u.words.length,features:{onsets:u.features.onsets.length,boundaries:u.features.phonemeBoundaries.length}});const a=[];for(let r=0;r<u.visemes.length;r++){const c=u.visemes[r],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 h=[...t.anim,...a];this.audioPlaylist.push({anim:h,audio:l}),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,l)=>{l>0&&(e+=" <mark name='"+o.mark+"'/>"),e+=o.word.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',""").replaceAll("'","'").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),l=await this.audioCtx.decodeAudioData(o);this.speakWithHands();const u=[0];let a=0;t.text.forEach((c,d)=>{if(d>0){let g=u[u.length-1];s.timepoints[a]&&(g=s.timepoints[a].timeSeconds*1e3,s.timepoints[a].markName===""+c.mark&&a++),u.push(g)}});const h=[{mark:0,time:0}];u.forEach((c,d)=>{if(d>0){let g=c-u[d-1];h[d-1].duration=g,h.push({mark:d,time:c})}});let r=1e3*l.duration;r>this.opt.ttsTrimEnd&&(r=r-this.opt.ttsTrimEnd),h[h.length-1].duration=r-h[h.length-1].time,t.anim.forEach(c=>{const d=h[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:l}),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 l=t.sampleRate;typeof l=="number"&&l>=8e3&&l<=96e3?l!==this.audioCtx.sampleRate&&this.initAudioGraph(l):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 l=this.audioCtx.audioWorklet.addModule(Je.href),u=new Promise((a,h)=>setTimeout(()=>h(new Error("Worklet loading timed out")),5e3));await Promise.race([l,u]),this.workletLoaded=!0}catch(l){throw console.error("Failed to load audio worklet:",l),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=l=>{if(l.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(u){console.error(u)}if(l.data.type==="playback-ended"&&(this._streamPause(),this.onAudioEnd))try{this.onAudioEnd()}catch{}if(this.onMetrics&&l.data.type==="metrics")try{this.onMetrics(l.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 l=this.audioCtx.resume(),u=new Promise((a,h)=>setTimeout(()=>h("p2"),1e3));try{await Promise.race([l,u])}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],l={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(l)}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 l=this.streamLipsyncLang||this.avatar.lipsyncLang||this.opt.lipsyncLang,u=this.lipsyncPreProcessText(n,l),a=this.lipsyncWordsToVisemes(u,l);if(a&&a.visemes&&a.visemes.length){const h=a.times[a.visemes.length-1]+a.durations[a.visemes.length-1],r=Math.min(o,Math.max(0,o-a.visemes.length*150));let c=.6+this.convertRange(r,[0,o],[0,.4]);if(o=Math.min(o,a.visemes.length*200),h>0)for(let d=0;d<a.visemes.length;d++){const g=e+s+a.times[d]/h*o,y=a.durations[d]/h*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),te.setFromMatrixPosition(this.speakTo.objectLeftEye.matrixWorld),ie.setFromMatrixPosition(this.speakTo.objectRightEye.matrixWorld),e.addVectors(te,ie).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),te.setFromMatrixPosition(this.objectLeftEye.matrixWorld),ie.setFromMatrixPosition(this.objectRightEye.matrixWorld),te.add(ie).divideScalar(2),W.copy(this.armature.quaternion),W.multiply(this.poseTarget.props["Hips.quaternion"]),W.multiply(this.poseTarget.props["Spine.quaternion"]),W.multiply(this.poseTarget.props["Spine1.quaternion"]),W.multiply(this.poseTarget.props["Spine2.quaternion"]),W.multiply(this.poseTarget.props["Neck.quaternion"]),W.multiply(this.poseTarget.props["Head.quaternion"]);const i=new f.Vector3().subVectors(e,te).normalize(),n=Math.atan2(i.x,i.z),s=Math.asin(-i.y);F.set(s,n,0,"YXZ");const l=new f.Quaternion().setFromEuler(F),u=new f.Quaternion().copy(l).multiply(W.clone().invert());F.setFromQuaternion(u,"YXZ");let a=F.x/(40/24)+.2,h=F.y/(9/4),r=Math.min(.6,Math.max(-.3,a)),c=Math.min(.8,Math.max(-.8,h)),d=(Math.random()-.5)/4,g=(Math.random()-.5)/4;if(t){let y=this.animQueue.findIndex(L=>L.template.name==="lookat");y!==-1&&this.animQueue.splice(y,1);const x={name:"lookat",dt:[750,t],vs:{bodyRotateX:[r+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),l=new f.Vector3().addVectors(s,o).divideScalar(2);l.project(this.camera);let u=(l.x+1)/2*n.width+n.left,a=-(l.y-1)/2*n.height+n.top;t===null&&(t=u),e===null&&(e=a),W.copy(this.armature.quaternion),W.multiply(this.poseTarget.props["Hips.quaternion"]),W.multiply(this.poseTarget.props["Spine.quaternion"]),W.multiply(this.poseTarget.props["Spine1.quaternion"]),W.multiply(this.poseTarget.props["Spine2.quaternion"]),W.multiply(this.poseTarget.props["Neck.quaternion"]),W.multiply(this.poseTarget.props["Head.quaternion"]),F.setFromQuaternion(W);let h=F.x/(40/24),r=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-u,u),y=Math.max(window.innerHeight-a,a),x=this.convertRange(e,[a-y,a+y],[-.3,.6])-h+c,L=this.convertRange(t,[u-g,u+g],[-.8,.8])-r+d;x=Math.min(.6,Math.max(-.3,x)),L=Math.min(.8,Math.max(-.8,L));let P=(Math.random()-.5)/4,p=(Math.random()-.5)/4;if(i){let T=this.animQueue.findIndex(I=>I.template.name==="lookat");T!==-1&&this.animQueue.splice(T,1);const M={name:"lookat",dt:[750,i],vs:{bodyRotateX:[x+P],bodyRotateY:[L+p],eyesRotateX:[-3*P+.1],eyesRotateY:[-5*p],browInnerUp:[[0,.7]],mouthLeft:[[0,.7]],mouthRight:[[0,.7]],eyeContact:[0],headMove:[0]}};this.animQueue.push(this.animFactory(M))}}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 l=o[0].point,u=new f.Vector3,a=new f.Vector3;this.objectLeftArm.getWorldPosition(u),this.objectRightArm.getWorldPosition(a);const h=u.distanceToSquared(l),r=a.distanceToSquared(l);h<r?(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}]},l,!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}]},l,!1,1e3),this.setValue("handFistRight",0))}else["LeftArm","LeftForeArm","LeftHand","RightArm","RightForeArm","RightHand"].forEach(l=>{let u=l+".quaternion";this.poseTarget.props[u].copy(this.getPoseTemplateProp(u)),this.poseTarget.props[u].t=this.animClock,this.poseTarget.props[u].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 l=this.animClips.find(u=>u.url===t+"-"+n);if(l){let u=this.animQueue.find(r=>r.template.name==="pose");u&&(u.ts[0]=1/0),Object.entries(l.pose.props).forEach(r=>{this.poseBase.props[r[0]]=r[1].clone(),this.poseTarget.props[r[0]]=r[1].clone(),this.poseTarget.props[r[0]].t=0,this.poseTarget.props[r[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/l.clip.duration),h=this.mixer.clipAction(l.clip);h.setLoop(f.LoopRepeat,a),h.clampWhenFinished=!0,this.currentFBXAction=h;try{h.fadeIn(.5).play(),console.log("FBX animation started successfully:",t)}catch(r){console.warn("FBX animation failed to start:",r),this.stopAnimation();return}if(h.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 h=new be.FBXLoader;let r;try{r=await h.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(r&&r.animations&&r.animations[n]){let c=r.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 L=0;L<y.values.length;L++)y.values[L]=y.values[L]*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),r&&r.animations?console.error(`FBX file loaded but has ${r.animations.length} animation(s), requested index ${n}`):console.error(r?"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 l=this.animPoses.find(u=>u.url===t+"-"+n);l&&(o=l.pose)}if(o){this.poseName=t,this.mixer=null;let l=this.animQueue.find(u=>u.template.name==="pose");l&&(l.ts[0]=this.animClock+i*1e3+2e3),this.setPoseFromTemplate(o)}else{let u=await new be.FBXLoader().loadAsync(t,e);if(u&&u.animations&&u.animations[n]){let a=u.animations[n];const h={};a.tracks.forEach(c=>{c.name=c.name.replaceAll("mixamorig","");const d=c.name.split(".");d[1]==="position"?h[c.name]=new f.Vector3(c.values[0]*s,c.values[1]*s,c.values[2]*s):d[1]==="quaternion"?h[c.name]=new f.Quaternion(c.values[0],c.values[1],c.values[2],c.values[3]):d[1]==="rotation"&&(h[d[0]+".quaternion"]=new f.Quaternion().setFromEuler(new f.Euler(c.values[0],c.values[1],c.values[2],"XYZ")).normalize())});const r={props:h};h["Hips.position"]&&(h["Hips.position"].y<.5?r.lying=!0:r.standing=!0),this.animPoses.push({url:t+"-"+n,pose:r}),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 l=this.animQueue.findIndex(u=>u.template.name==="talkinghands");l!==-1&&(this.animQueue[l].ts=this.animQueue[l].ts.map(u=>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[u,a]of Object.entries(this.gesture))a.t=this.animClock,a.d=n,this.poseTarget.props.hasOwnProperty(u)&&(this.poseTarget.props[u].copy(a),this.poseTarget.props[u].t=this.animClock,this.poseTarget.props[u].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 l=this.animFactory(o);if(l.gesture=!0,e&&Number.isFinite(e)){const u=l.ts[0],h=l.ts[l.ts.length-1]-u;if(e*1e3-h>0){const c=[];for(let y=1;y<l.ts.length;y++)c.push(l.ts[y]-l.ts[y-1]);const d=o.template?.rescale||c.map(y=>y/h),g=e*1e3-h;l.ts=l.ts.map((y,x,L)=>x===0?u:L[x-1]+c[x-1]+d[x-1]*g)}else{const c=e*1e3/h;l.ts=l.ts.map(d=>u+c*(d-u))}}this.animQueue.push(l)}}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,l=new f.Vector3,u=new f.Vector3,a=new f.Quaternion,h=new f.Vector3,r=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(L=>{L.bone=this.ikMesh.getObjectByName(L.link),L.bone.quaternion.copy(this.getPoseTemplateProp(L.link+".quaternion"))}),d.updateMatrixWorld(!0);const x=t.iterations||10;if(e)for(let L=0;L<x;L++){let P=!1;for(let p=0,T=y.length;p<T;p++){const M=y[p].bone;M.matrixWorld.decompose(u,a,h),a.invert(),o.setFromMatrixPosition(g.matrixWorld),l.subVectors(o,u),l.applyQuaternion(a),l.normalize(),s.subVectors(e,u),s.applyQuaternion(a),s.normalize();let I=s.dot(l);I>1?I=1:I<-1&&(I=-1),I=Math.acos(I),!(I<1e-5)&&(y[p].minAngle!==void 0&&I<y[p].minAngle&&(I=y[p].minAngle),y[p].maxAngle!==void 0&&I>y[p].maxAngle&&(I=y[p].maxAngle),r.crossVectors(l,s),r.normalize(),W.setFromAxisAngle(r,I),M.quaternion.multiply(W),M.rotation.setFromVector3(c.setFromEuler(M.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))),M.updateMatrixWorld(!0),P=!0)}if(!P)break}n&&y.forEach(L=>{this.poseTarget.props[L.link+".quaternion"].copy(L.bone.quaternion),this.poseTarget.props[L.link+".quaternion"].t=this.animClock,this.poseTarget.props[L.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"}},Le={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 ce(){return{service:"elevenlabs",endpoint:se.endpoint,apiKey:se.apiKey,defaultVoice:se.defaultVoice,voices:se.voices}}function et(){const E=ce(),t=[];return Object.entries(E.voices).forEach(([e,i])=>{t.push({value:i,label:`${e.charAt(0).toUpperCase()+e.slice(1)} (${E.service})`})}),t}const ye=A.forwardRef(({avatarUrl:E="/avatars/brunette.glb",avatarBody:t="F",mood:e="neutral",ttsLang:i="en",ttsService:n=null,ttsVoice:s=null,ttsApiKey:o=null,bodyMovement:l="idle",movementIntensity:u=.5,showFullAvatar:a=!0,cameraView:h="upper",onReady:r=()=>{},onLoading:c=()=>{},onError:d=()=>{},className:g="",style:y={},animations:x={}},L)=>{const P=A.useRef(null),p=A.useRef(null),T=A.useRef(a),[M,I]=A.useState(!0),[D,O]=A.useState(null),[X,ne]=A.useState(!1);A.useEffect(()=>{T.current=a},[a]);const S=ce(),B=n||S.service;let N;B==="browser"?N={service:"browser",endpoint:"",apiKey:null,defaultVoice:"Google US English"}:B==="elevenlabs"?N={service:"elevenlabs",endpoint:"https://api.elevenlabs.io/v1/text-to-speech",apiKey:o||S.apiKey,defaultVoice:s||S.defaultVoice||se.defaultVoice,voices:S.voices||se.voices}:B==="deepgram"?N={service:"deepgram",endpoint:"https://api.deepgram.com/v1/speak",apiKey:o||S.apiKey,defaultVoice:s||S.defaultVoice||Le.defaultVoice,voices:S.voices||Le.voices}:N={...S,apiKey:o!==null?o:S.apiKey};const V={url:E,body:t,avatarMood:e,ttsLang:B==="browser"?"en-US":i,ttsVoice:s||N.defaultVoice,lipsyncLang:"en",showFullAvatar:a,bodyMovement:l,movementIntensity:u},q={ttsEndpoint:N.endpoint,ttsApikey:N.apiKey,ttsService:B,lipsyncModules:["en"],cameraView:h},_=A.useCallback(async()=>{if(!(!P.current||p.current))try{if(I(!0),O(null),p.current=new Ae(P.current,q),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(V,z=>{if(z.lengthComputable){const H=Math.min(100,Math.round(z.loaded/z.total*100));c(H)}}),await new Promise(z=>{const H=()=>{p.current.lipsync&&Object.keys(p.current.lipsync).length>0?z():setTimeout(H,100)};H()}),p.current&&p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(a)}catch(z){console.warn("Error setting full body mode on initialization:",z)}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()),I(!1),ne(!0),r(p.current);const w=()=>{document.visibilityState==="visible"?p.current?.start():p.current?.stop()};return document.addEventListener("visibilitychange",w),()=>{document.removeEventListener("visibilitychange",w)}}catch(R){console.error("Error initializing TalkingHead:",R),O(R.message||"Failed to initialize avatar"),I(!1),d(R)}},[E,t,e,i,n,s,o,a,l,u,h]);A.useEffect(()=>(_(),()=>{p.current&&(p.current.stop(),p.current.dispose(),p.current=null)}),[_]),A.useEffect(()=>{if(!P.current||!p.current)return;const R=new ResizeObserver(z=>{for(const H of z)p.current&&p.current.onResize&&p.current.onResize()});R.observe(P.current);const w=()=>{p.current&&p.current.onResize&&p.current.onResize()};return window.addEventListener("resize",w),()=>{R.disconnect(),window.removeEventListener("resize",w)}},[X]);const oe=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(R){console.warn("Failed to resume audio context:",R)}},[]),de=A.useCallback(async(R,w={})=>{if(p.current&&X)try{await oe();const z={...w,lipsyncLang:w.lipsyncLang||V.lipsyncLang||"en"};if(w.onSpeechEnd&&p.current){const H=p.current;let U=null,K=0;const pe=1200;let ae=!1;U=setInterval(()=>{if(K++,K>pe){if(U&&(clearInterval(U),U=null),!ae){ae=!0;try{w.onSpeechEnd()}catch(xe){console.error("Error in onSpeechEnd callback (timeout):",xe)}}return}const we=!H.speechQueue||H.speechQueue.length===0,Ce=!H.audioPlaylist||H.audioPlaylist.length===0;H&&H.isSpeaking===!1&&we&&Ce&&H.isAudioPlaying===!1&&!ae&&setTimeout(()=>{if(H&&H.isSpeaking===!1&&(!H.speechQueue||H.speechQueue.length===0)&&(!H.audioPlaylist||H.audioPlaylist.length===0)&&H.isAudioPlaying===!1&&!ae){ae=!0,U&&(clearInterval(U),U=null);try{w.onSpeechEnd()}catch(ze){console.error("Error in onSpeechEnd callback:",ze)}}},100)},100)}p.current.lipsync&&Object.keys(p.current.lipsync).length>0?(p.current.setSlowdownRate&&p.current.setSlowdownRate(1.05),p.current.speakText(R,z)):setTimeout(async()=>{await oe(),p.current&&p.current.lipsync&&(p.current.setSlowdownRate&&p.current.setSlowdownRate(1.05),p.current.speakText(R,z))},100)}catch(z){console.error("Error speaking text:",z),O(z.message||"Failed to speak text")}},[X,oe,V.lipsyncLang]),re=A.useCallback(()=>{p.current&&(p.current.stopSpeaking(),p.current.setSlowdownRate&&p.current.setSlowdownRate(1))},[]),me=A.useCallback(R=>{p.current&&p.current.setMood(R)},[]),j=A.useCallback(R=>{p.current&&p.current.setSlowdownRate&&p.current.setSlowdownRate(R)},[]),b=A.useCallback((R,w=!1)=>{if(p.current&&p.current.playAnimation){if(x&&x[R]&&(R=x[R]),p.current.setShowFullAvatar)try{p.current.setShowFullAvatar(T.current)}catch(H){console.warn("Error setting full body mode:",H)}if(R.includes("."))try{p.current.playAnimation(R,null,10,0,.01,w)}catch(H){console.warn(`Failed to play ${R}:`,H);try{p.current.setBodyMovement("idle")}catch(U){console.warn("Fallback animation also failed:",U)}}else{const H=[".fbx",".glb",".gltf"];let U=!1;for(const K of H)try{p.current.playAnimation(R+K,null,10,0,.01,w),U=!0;break}catch{}if(!U){console.warn("Animation not found:",R);try{p.current.setBodyMovement("idle")}catch(K){console.warn("Fallback animation also failed:",K)}}}}},[x]),v=A.useCallback(()=>{p.current&&p.current.onResize&&p.current.onResize()},[]);return A.useImperativeHandle(L,()=>({speakText:de,stopSpeaking:re,resumeAudioContext:oe,setMood:me,setTimingAdjustment:j,playAnimation:b,isReady:X,talkingHead:p.current,handleResize:v,setBodyMovement:R=>{if(p.current&&p.current.setShowFullAvatar&&p.current.setBodyMovement)try{p.current.setShowFullAvatar(T.current),p.current.setBodyMovement(R)}catch(w){console.warn("Error setting body movement:",w)}},setMovementIntensity:R=>p.current?.setMovementIntensity(R),playRandomDance:()=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playRandomDance)try{p.current.setShowFullAvatar(T.current),p.current.playRandomDance()}catch(R){console.warn("Error playing random dance:",R)}},playReaction:R=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playReaction)try{p.current.setShowFullAvatar(T.current),p.current.playReaction(R)}catch(w){console.warn("Error playing reaction:",w)}},playCelebration:()=>{if(p.current&&p.current.setShowFullAvatar&&p.current.playCelebration)try{p.current.setShowFullAvatar(T.current),p.current.playCelebration()}catch(R){console.warn("Error playing celebration:",R)}},setShowFullAvatar:R=>{if(p.current&&p.current.setShowFullAvatar)try{T.current=R,p.current.setShowFullAvatar(R)}catch(w){console.warn("Error setting showFullAvatar:",w)}},lockAvatarPosition:()=>{if(p.current&&p.current.lockAvatarPosition)try{p.current.lockAvatarPosition()}catch(R){console.warn("Error locking avatar position:",R)}},unlockAvatarPosition:()=>{if(p.current&&p.current.unlockAvatarPosition)try{p.current.unlockAvatarPosition()}catch(R){console.warn("Error unlocking avatar position:",R)}}})),ee.jsxs("div",{className:`talking-head-avatar ${g}`,style:{width:"100%",height:"100%",position:"relative",...y},children:[ee.jsx("div",{ref:P,className:"talking-head-viewer",style:{width:"100%",height:"100%",minHeight:"400px"}}),M&&ee.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..."}),D&&ee.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:D})]})});ye.displayName="TalkingHeadAvatar";const ke=A.forwardRef(({text:E="Hello! I'm a talking avatar. How are you today?",onLoading:t=()=>{},onError:e=()=>{},onReady:i=()=>{},className:n="",style:s={},avatarConfig:o={}},l)=>{const u=A.useRef(null),a=A.useRef(null),[h,r]=A.useState(!0),[c,d]=A.useState(null),[g,y]=A.useState(!1),x=ce(),L=o.ttsService||x.service,P=L==="browser"?{endpoint:"",apiKey:null,defaultVoice:"Google US English"}:{...x,apiKey:o.ttsApiKey!==void 0&&o.ttsApiKey!==null?o.ttsApiKey:x.apiKey,endpoint:L==="elevenlabs"&&o.ttsApiKey?"https://api.elevenlabs.io/v1/text-to-speech":x.endpoint},p={url:"/avatars/brunette.glb",body:"F",avatarMood:"neutral",ttsLang:L==="browser"?"en-US":"en",ttsVoice:o.ttsVoice||P.defaultVoice,lipsyncLang:"en",showFullAvatar:!0,bodyMovement:"idle",movementIntensity:.5,...o},T={ttsEndpoint:P.endpoint,ttsApikey:P.apiKey,ttsService:L,lipsyncModules:["en"],cameraView:"upper"},M=A.useCallback(async()=>{if(!(!u.current||a.current))try{if(r(!0),d(null),a.current=new Ae(u.current,T),await a.current.showAvatar(p,N=>{if(N.lengthComputable){const V=Math.min(100,Math.round(N.loaded/N.total*100));t(V)}}),a.current.morphs&&a.current.morphs.length>0){const N=a.current.morphs[0].morphTargetDictionary;console.log("Available morph targets:",Object.keys(N));const V=Object.keys(N).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(N=>{const V=()=>{a.current.lipsync&&Object.keys(a.current.lipsync).length>0?(console.log("Lip-sync modules loaded:",Object.keys(a.current.lipsync)),N()):(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(N){console.warn("Error setting full body mode on initialization:",N)}r(!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(S){console.error("Error initializing TalkingHead:",S),d(S.message||"Failed to initialize avatar"),r(!1),e(S)}},[]);A.useEffect(()=>(M(),()=>{a.current&&(a.current.stop(),a.current.dispose(),a.current=null)}),[M]);const I=A.useCallback(S=>{if(a.current&&g)try{console.log("Speaking text:",S),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(S)):(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(S)):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]),D=A.useCallback(()=>{a.current&&(a.current.stopSpeaking(),a.current.setSlowdownRate&&(a.current.setSlowdownRate(1),console.log("Reset timing to normal")))},[]),O=A.useCallback(S=>{a.current&&a.current.setMood(S)},[]),X=A.useCallback(S=>{a.current&&a.current.setSlowdownRate&&(a.current.setSlowdownRate(S),console.log("Timing adjustment set to:",S))},[]),ne=A.useCallback((S,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(S.includes("."))try{a.current.playAnimation(S,null,10,0,.01,B),console.log("Playing animation:",S)}catch(V){console.log(`Failed to play ${S}:`,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 _ of V)try{a.current.playAnimation(S+_,null,10,0,.01,B),console.log("Playing animation:",S+_),q=!0;break}catch{console.log(`Failed to play ${S}${_}, trying next format...`)}if(!q){console.warn("Animation system not available or animation not found:",S);try{a.current.setBodyMovement("idle"),console.log("Fallback to idle animation")}catch(_){console.warn("Fallback animation also failed:",_)}}}}else console.warn("Animation system not available or animation not found:",S)},[]);return A.useImperativeHandle(l,()=>({speakText:I,stopSpeaking:D,setMood:O,setTimingAdjustment:X,playAnimation:ne,isReady:g,talkingHead:a.current,setBodyMovement:S=>{if(a.current&&a.current.setShowFullAvatar&&a.current.setBodyMovement)try{a.current.setShowFullAvatar(!0),a.current.setBodyMovement(S),console.log("Body movement set with full body mode:",S)}catch(B){console.warn("Error setting body movement:",B)}},setMovementIntensity:S=>a.current?.setMovementIntensity(S),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(S){console.warn("Error playing random dance:",S)}},playReaction:S=>{if(a.current&&a.current.setShowFullAvatar&&a.current.playReaction)try{a.current.setShowFullAvatar(!0),a.current.playReaction(S),console.log("Reaction played with full body mode:",S)}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(S){console.warn("Error playing celebration:",S)}},setShowFullAvatar:S=>{if(a.current&&a.current.setShowFullAvatar)try{a.current.setShowFullAvatar(S),console.log("Show full avatar set to:",S)}catch(B){console.warn("Error setting showFullAvatar:",B)}},lockAvatarPosition:()=>{if(a.current&&a.current.lockAvatarPosition)try{a.current.lockAvatarPosition()}catch(S){console.warn("Error locking avatar position:",S)}},unlockAvatarPosition:()=>{if(a.current&&a.current.unlockAvatarPosition)try{a.current.unlockAvatarPosition()}catch(S){console.warn("Error unlocking avatar position:",S)}}})),ee.jsxs("div",{className:`talking-head-container ${n}`,style:s,children:[ee.jsx("div",{ref:u,className:"talking-head-viewer",style:{width:"100%",height:"100%",minHeight:"400px"}}),h&&ee.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&&ee.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})]})});ke.displayName="TalkingHeadComponent";const Se=A.forwardRef(({curriculumData:E=null,avatarConfig:t={},animations:e={},onLessonStart:i=()=>{},onLessonComplete:n=()=>{},onQuestionAnswer:s=()=>{},onCurriculumComplete:o=()=>{},onCustomAction:l=()=>{},autoStart:u=!1},a)=>{const h=A.useRef(null),r=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:l}),d=A.useRef(null),g=A.useRef(null),y=A.useRef(null),x=A.useRef(null),L=A.useRef(null),P=A.useRef(null),p=A.useRef(null),T=A.useRef(E?.curriculum||{title:"Default Curriculum",description:"No curriculum data provided",language:"en",modules:[]}),M=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:l}},[i,n,s,o,l]),A.useEffect(()=>{T.current=E?.curriculum||{title:"Default Curriculum",description:"No curriculum data provided",language:"en",modules:[]},M.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"}},[E,t,e]);const I=A.useCallback(()=>(T.current||{modules:[]}).modules[r.current.currentModuleIndex]?.lessons[r.current.currentLessonIndex],[]),D=A.useCallback(()=>I()?.questions[r.current.currentQuestionIndex],[I]),O=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,[]),X=A.useCallback(()=>{r.current.lessonCompleted=!0,r.current.isQuestionMode=!1;const b=r.current.totalQuestions>0?Math.round(r.current.score/r.current.totalQuestions*100):100;let v="Congratulations! You've completed this lesson";if(r.current.totalQuestions>0?v+=` with a score of ${r.current.score} out of ${r.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:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,score:r.current.score,totalQuestions:r.current.totalQuestions,percentage:b}),c.current.onCustomAction({type:"lessonComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,score:r.current.score,totalQuestions:r.current.totalQuestions,percentage:b}),h.current){if(h.current.setMood("happy"),e.lessonComplete)try{h.current.playAnimation(e.lessonComplete,!0)}catch{h.current.playCelebration()}const R=T.current||{modules:[]},w=R.modules[r.current.currentModuleIndex],z=r.current.currentLessonIndex<(w?.lessons?.length||0)-1,H=r.current.currentModuleIndex<(R.modules?.length||0)-1,U=z||H,K=M.current||{lipsyncLang:"en"};h.current.speakText(v,{lipsyncLang:K.lipsyncLang,onSpeechEnd:()=>{c.current.onCustomAction({type:"lessonCompleteFeedbackDone",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,score:r.current.score,totalQuestions:r.current.totalQuestions,percentage:b,hasNextLesson:U})}})}},[e.lessonComplete]),ne=A.useCallback(()=>{r.current.curriculumCompleted=!0;const b=T.current||{modules:[]};if(c.current.onCurriculumComplete({modules:b.modules.length,totalLessons:b.modules.reduce((v,R)=>v+R.lessons.length,0)}),h.current){if(h.current.setMood("celebrating"),e.curriculumComplete)try{h.current.playAnimation(e.curriculumComplete,!0)}catch{h.current.playCelebration()}const v=M.current||{lipsyncLang:"en"};h.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]),S=A.useCallback(()=>{const b=I();r.current.isQuestionMode=!0,r.current.currentQuestionIndex=0,r.current.totalQuestions=b?.questions?.length||0,r.current.score=0;const v=D();v&&c.current.onCustomAction({type:"questionStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,totalQuestions:r.current.totalQuestions,question:v,score:r.current.score});const R=()=>{if(!h.current||!v)return;if(h.current.setMood("happy"),e.questionStart)try{h.current.playAnimation(e.questionStart,!0)}catch(z){console.warn("Failed to play questionStart animation:",z)}const w=M.current||{lipsyncLang:"en"};v.type==="code_test"?h.current.speakText(`Let's test your coding skills! Here's your first challenge: ${v.question}`,{lipsyncLang:w.lipsyncLang}):v.type==="multiple_choice"?h.current.speakText(`Now let me ask you some questions. Here's the first one: ${v.question}`,{lipsyncLang:w.lipsyncLang}):v.type==="true_false"?h.current.speakText(`Let's start with some true or false questions. First question: ${v.question}`,{lipsyncLang:w.lipsyncLang}):h.current.speakText(`Now let me ask you some questions. Here's the first one: ${v.question}`,{lipsyncLang:w.lipsyncLang})};if(h.current&&h.current.isReady&&v)R();else if(h.current&&h.current.isReady){const w=M.current||{lipsyncLang:"en"};h.current.speakText("Now let me ask you some questions to test your understanding.",{lipsyncLang:w.lipsyncLang})}else{const w=setInterval(()=>{h.current&&h.current.isReady&&(clearInterval(w),v&&R())},100);setTimeout(()=>{clearInterval(w)},5e3)}},[e.questionStart,I,D]),B=A.useCallback(()=>{const b=I();if(r.current.currentQuestionIndex<(b?.questions?.length||0)-1){h.current&&h.current.stopSpeaking&&h.current.stopSpeaking(),r.current.currentQuestionIndex+=1;const v=D();v&&c.current.onCustomAction({type:"nextQuestion",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,totalQuestions:r.current.totalQuestions,question:v,score:r.current.score});const R=()=>{if(!h.current||!v)return;if(h.current.setMood("happy"),h.current.setBodyMovement("idle"),e.nextQuestion)try{h.current.playAnimation(e.nextQuestion,!0)}catch(z){console.warn("Failed to play nextQuestion animation:",z)}const w=M.current||{lipsyncLang:"en"};v.type==="code_test"?h.current.speakText(`Great! Now let's move on to your next coding challenge: ${v.question}`,{lipsyncLang:w.lipsyncLang}):v.type==="multiple_choice"?h.current.speakText(`Alright! Here's your next question: ${v.question}`,{lipsyncLang:w.lipsyncLang}):v.type==="true_false"?h.current.speakText(`Now let's try this one: ${v.question}`,{lipsyncLang:w.lipsyncLang}):h.current.speakText(`Here's the next question: ${v.question}`,{lipsyncLang:w.lipsyncLang})};if(h.current&&h.current.isReady&&v)R();else if(v){const w=setInterval(()=>{h.current&&h.current.isReady&&(clearInterval(w),R())},100);setTimeout(()=>{clearInterval(w)},5e3)}}else c.current.onCustomAction({type:"allQuestionsComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,totalQuestions:r.current.totalQuestions,score:r.current.score})},[e.nextQuestion,I,D]),N=A.useCallback(()=>{const b=T.current||{modules:[]},v=b.modules[r.current.currentModuleIndex];if(r.current.currentLessonIndex<(v?.lessons?.length||0)-1){r.current.currentLessonIndex+=1,r.current.currentQuestionIndex=0,r.current.lessonCompleted=!1,r.current.isQuestionMode=!1,r.current.isTeaching=!1,r.current.score=0,r.current.totalQuestions=0;const w=b.modules[r.current.currentModuleIndex],z=r.current.currentLessonIndex<(w?.lessons?.length||0)-1,H=r.current.currentModuleIndex<(b.modules?.length||0)-1,U=z||H;c.current.onCustomAction({type:"lessonStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,hasNextLesson:U}),c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:I()}),h.current&&(h.current.setMood("happy"),h.current.setBodyMovement("idle"))}else if(r.current.currentModuleIndex<(b.modules?.length||0)-1){r.current.currentModuleIndex+=1,r.current.currentLessonIndex=0,r.current.currentQuestionIndex=0,r.current.lessonCompleted=!1,r.current.isQuestionMode=!1,r.current.isTeaching=!1,r.current.score=0,r.current.totalQuestions=0;const z=b.modules[r.current.currentModuleIndex],H=r.current.currentLessonIndex<(z?.lessons?.length||0)-1,U=r.current.currentModuleIndex<(b.modules?.length||0)-1,K=H||U;c.current.onCustomAction({type:"lessonStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,hasNextLesson:K}),c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:I()}),h.current&&(h.current.setMood("happy"),h.current.setBodyMovement("idle"))}else L.current&&L.current()},[]),V=A.useCallback(()=>{const b=I();let v=null;if(b?.avatar_script&&b?.body){const R=b.avatar_script.trim(),w=b.body.trim(),z=R.match(/[.!?]$/)?" ":". ";v=`${R}${z}${w}`}else v=b?.avatar_script||b?.body||null;if(h.current&&h.current.isReady&&v){r.current.isTeaching=!0,r.current.isQuestionMode=!1,r.current.score=0,r.current.totalQuestions=0,h.current.setMood("happy");let R=!1;if(e.teaching)try{h.current.playAnimation(e.teaching,!0),R=!0}catch(z){console.warn("Failed to play teaching animation:",z)}R||h.current.setBodyMovement("gesturing");const w=M.current||{lipsyncLang:"en"};c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:b}),c.current.onCustomAction({type:"teachingStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:b}),h.current.speakText(v,{lipsyncLang:w.lipsyncLang,onSpeechEnd:()=>{r.current.isTeaching=!1,c.current.onCustomAction({type:"teachingComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:b,hasQuestions:b.questions&&b.questions.length>0})}})}},[e.teaching,I]),q=A.useCallback(b=>{const v=D(),R=O(b,v);if(R&&(r.current.score+=1),c.current.onQuestionAnswer({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,answer:b,isCorrect:R,question:v}),h.current)if(R){if(h.current.setMood("happy"),e.correct)try{h.current.playReaction("happy")}catch{h.current.setBodyMovement("happy")}h.current.setBodyMovement("gesturing");const w=v.type==="code_test"?`Great job! Your code passed all the tests! ${v.explanation||""}`:`Excellent! That's correct! ${v.explanation||""}`,z=M.current||{lipsyncLang:"en"};h.current.speakText(w,{lipsyncLang:z.lipsyncLang,onSpeechEnd:()=>{const U=I()?.questions?.length||0;c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,isCorrect:!0,hasNextQuestion:r.current.currentQuestionIndex<U-1,score:r.current.score,totalQuestions:r.current.totalQuestions})}})}else{if(h.current.setMood("sad"),e.incorrect)try{h.current.playAnimation(e.incorrect,!0)}catch{h.current.setBodyMovement("idle")}h.current.setBodyMovement("gesturing");const w=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.`,z=M.current||{lipsyncLang:"en"};h.current.speakText(w,{lipsyncLang:z.lipsyncLang,onSpeechEnd:()=>{const U=I()?.questions?.length||0;c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,isCorrect:!1,hasNextQuestion:r.current.currentQuestionIndex<U-1,score:r.current.score,totalQuestions:r.current.totalQuestions})}})}else{const z=I()?.questions?.length||0;c.current.onCustomAction({type:"answerFeedbackComplete",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,isCorrect:R,hasNextQuestion:r.current.currentQuestionIndex<z-1,score:r.current.score,totalQuestions:r.current.totalQuestions,avatarNotReady:!0})}},[e.correct,e.incorrect,D,I,O]),_=A.useCallback(b=>{const v=D();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 R={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:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,testResult:R,question:v}),p.current&&p.current(R)},[D,O]),oe=A.useCallback(()=>{if(r.current.currentQuestionIndex>0){r.current.currentQuestionIndex-=1;const b=D();b&&c.current.onCustomAction({type:"questionStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,questionIndex:r.current.currentQuestionIndex,totalQuestions:r.current.totalQuestions,question:b,score:r.current.score});const v=()=>{if(!h.current||!b)return;h.current.setMood("happy"),h.current.setBodyMovement("idle");const R=M.current||{lipsyncLang:"en"};b.type==="code_test"?h.current.speakText(`Let's go back to this coding challenge: ${b.question}`,{lipsyncLang:R.lipsyncLang}):h.current.speakText(`Going back to: ${b.question}`,{lipsyncLang:R.lipsyncLang})};if(h.current&&h.current.isReady&&b)v();else if(b){const R=setInterval(()=>{h.current&&h.current.isReady&&(clearInterval(R),v())},100);setTimeout(()=>{clearInterval(R)},5e3)}}},[D]),de=A.useCallback(()=>{const b=T.current||{modules:[]};if(b.modules[r.current.currentModuleIndex],r.current.currentLessonIndex>0)r.current.currentLessonIndex-=1,r.current.currentQuestionIndex=0,r.current.lessonCompleted=!1,r.current.isQuestionMode=!1,r.current.isTeaching=!1,r.current.score=0,r.current.totalQuestions=0,c.current.onCustomAction({type:"lessonStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex}),c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:I()}),h.current&&(h.current.setMood("happy"),h.current.setBodyMovement("idle"));else if(r.current.currentModuleIndex>0){const w=b.modules[r.current.currentModuleIndex-1];r.current.currentModuleIndex-=1,r.current.currentLessonIndex=(w?.lessons?.length||1)-1,r.current.currentQuestionIndex=0,r.current.lessonCompleted=!1,r.current.isQuestionMode=!1,r.current.isTeaching=!1,r.current.score=0,r.current.totalQuestions=0,c.current.onCustomAction({type:"lessonStart",moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex}),c.current.onLessonStart({moduleIndex:r.current.currentModuleIndex,lessonIndex:r.current.currentLessonIndex,lesson:I()}),h.current&&(h.current.setMood("happy"),h.current.setBodyMovement("idle"))}},[I]),re=A.useCallback(()=>{r.current.currentModuleIndex=0,r.current.currentLessonIndex=0,r.current.currentQuestionIndex=0,r.current.isTeaching=!1,r.current.isQuestionMode=!1,r.current.lessonCompleted=!1,r.current.curriculumCompleted=!1,r.current.score=0,r.current.totalQuestions=0},[]),me=A.useCallback(b=>{console.log("Avatar is ready!",b);const v=I(),R=v?.avatar_script||v?.body;u&&R&&setTimeout(()=>{d.current&&d.current()},10)},[u,I]);A.useLayoutEffect(()=>{d.current=V,g.current=N,y.current=X,x.current=B,L.current=ne,P.current=S,p.current=q}),A.useImperativeHandle(a,()=>({startTeaching:V,startQuestions:S,handleAnswerSelect:q,handleCodeTestResult:_,nextQuestion:B,previousQuestion:oe,nextLesson:N,previousLesson:de,completeLesson:X,completeCurriculum:ne,resetCurriculum:re,getState:()=>({...r.current}),getCurrentQuestion:()=>D(),getCurrentLesson:()=>I(),getAvatarRef:()=>h.current,speakText:async(b,v={})=>{await h.current?.resumeAudioContext?.();const R=M.current||{lipsyncLang:"en"};h.current?.speakText(b,{...v,lipsyncLang:v.lipsyncLang||R.lipsyncLang})},resumeAudioContext:async()=>{if(h.current?.resumeAudioContext)return await h.current.resumeAudioContext();const b=h.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(R){console.warn("Failed to resume audio context:",R)}}else console.warn("Audio context not available yet")},stopSpeaking:()=>h.current?.stopSpeaking(),setMood:b=>h.current?.setMood(b),playAnimation:(b,v)=>h.current?.playAnimation(b,v),setBodyMovement:b=>h.current?.setBodyMovement(b),setMovementIntensity:b=>h.current?.setMovementIntensity(b),playRandomDance:()=>h.current?.playRandomDance(),playReaction:b=>h.current?.playReaction(b),playCelebration:()=>h.current?.playCelebration(),setShowFullAvatar:b=>h.current?.setShowFullAvatar(b),setTimingAdjustment:b=>h.current?.setTimingAdjustment(b),lockAvatarPosition:()=>h.current?.lockAvatarPosition(),unlockAvatarPosition:()=>h.current?.unlockAvatarPosition(),triggerCustomAction:(b,v)=>{c.current.onCustomAction({type:b,...v,state:{...r.current}})},handleResize:()=>h.current?.handleResize(),isAvatarReady:()=>h.current?.isReady||!1}),[V,S,q,_,B,N,X,ne,re,D,I]);const j=M.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 ee.jsx("div",{style:{width:"100%",height:"100%"},children:ee.jsx(ye,{ref:h,avatarUrl:j.avatarUrl,avatarBody:j.avatarBody,mood:j.mood,ttsLang:j.ttsLang,ttsService:j.ttsService,ttsVoice:j.ttsVoice,ttsApiKey:j.ttsApiKey,bodyMovement:j.bodyMovement,movementIntensity:j.movementIntensity,showFullAvatar:j.showFullAvatar,cameraView:"upper",animations:j.animations,onReady:me,onLoading:()=>{},onError:b=>{console.error("Avatar error:",b)}})})});Se.displayName="CurriculumLearning";const fe={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"}},tt=E=>fe[E]||null,it=E=>fe.hasOwnProperty(E);exports.CurriculumLearning=Se;exports.TalkingHeadAvatar=ye;exports.TalkingHeadComponent=ke;exports.animations=fe;exports.getActiveTTSConfig=ce;exports.getAnimation=tt;exports.getVoiceOptions=et;exports.hasAnimation=it;
|
package/dist/index.js
CHANGED
|
@@ -7406,7 +7406,7 @@ const nt = fe(({
|
|
|
7406
7406
|
}, [e.questionStart, I, B]), P = M(() => {
|
|
7407
7407
|
const b = I();
|
|
7408
7408
|
if (r.current.currentQuestionIndex < (b?.questions?.length || 0) - 1) {
|
|
7409
|
-
r.current.currentQuestionIndex += 1;
|
|
7409
|
+
h.current && h.current.stopSpeaking && h.current.stopSpeaking(), r.current.currentQuestionIndex += 1;
|
|
7410
7410
|
const R = B();
|
|
7411
7411
|
R && d.current.onCustomAction({
|
|
7412
7412
|
type: "nextQuestion",
|
package/package.json
CHANGED
|
@@ -360,6 +360,11 @@ const CurriculumLearning = forwardRef(({
|
|
|
360
360
|
const nextQuestion = useCallback(() => {
|
|
361
361
|
const currentLesson = getCurrentLesson();
|
|
362
362
|
if (stateRef.current.currentQuestionIndex < (currentLesson?.questions?.length || 0) - 1) {
|
|
363
|
+
// Stop any current speech/feedback before skipping to next question
|
|
364
|
+
if (avatarRef.current && avatarRef.current.stopSpeaking) {
|
|
365
|
+
avatarRef.current.stopSpeaking();
|
|
366
|
+
}
|
|
367
|
+
|
|
363
368
|
stateRef.current.currentQuestionIndex += 1;
|
|
364
369
|
|
|
365
370
|
// Trigger custom action IMMEDIATELY for UI update (before avatar speaks)
|