import{textToSpeech}from'./voicevox.js';constrecognition=newwebkitSpeechRecognition();recognition.continuous=false;recognition.interimResults=true;conststartButton=document.getElementById('start-recording');conststopButton=document.getElementById('stop-recording');constlanguageSelect=document.getElementById('language-select');consttranscriptDiv=document.getElementById('transcript');constresponseDiv=document.getElementById('response');consthistoryDiv=document.getElementById('history');letmessages=[];letisSpaceKeyDown=false;startButton.addEventListener('click',startRecording);stopButton.addEventListener('click',stopRecording);languageSelect.addEventListener('change',changeLanguage);functionstartRecording(){recognition.start();startButton.disabled=true;stopButton.disabled=false;}functionstopRecording(){recognition.stop();startButton.disabled=false;stopButton.disabled=true;}functionchangeLanguage(){recognition.lang=languageSelect.value;}recognition.addEventListener('result',(event)=>{letfinalTranscript='';for(leti=event.resultIndex;i<event.results.length;i++){consttranscript=event.results[i][0].transcript;if(event.results[i].isFinal){finalTranscript+=transcript;}}// transcriptDiv.textContent = finalTranscript;if(finalTranscript){getChatGPTResponse(finalTranscript);}});recognition.addEventListener('end',()=>{if(isSpaceKeyDown){startRecording();}});recognition.lang=recognition.lang==''?'ja-JP':languageSelect.value;asyncfunctiongetChatGPTResponse(text){responseDiv.textContent='Thinking...';constuserMessage={role:'user',content:text};messages.push(userMessage);// 最新のメッセージを保持constMAX_MESSAGE_LENGTH=6;if(messages.length>MAX_MESSAGE_LENGTH){messages.shift();}constheaders={'Content-Type':'application/json','Authorization':`Bearer ${API_KEY}`};constpayload={model:'gpt-3.5-turbo',messages:[...messages,],temperature:0.8,};try{constresponse=awaitfetch('https://api.openai.com/v1/chat/completions',{method:'POST',headers:headers,body:JSON.stringify(payload),});constdata=awaitresponse.json();if(data.choices[0].message.content){constchatGPTResponse=data.choices[0].message.content;textToSpeech(chatGPTResponse);messages.push({role:'assistant',content:chatGPTResponse});displayHistory();}else{console.error('Error: No choices or text in API response');responseDiv.textContent='Error: ChatGPT did not provide a response';}}catch(error){console.error('Error:',error);responseDiv.textContent='Error: Could not get response from ChatGPT';}}functiondisplayHistory(){historyDiv.innerHTML='';messages.forEach((message)=>{constrole=message.role==='user'?'User':'AI';constpElement=document.createElement('p');pElement.textContent=`${role}: ${message.content}`;pElement.classList.add('message');pElement.classList.add(message.role);historyDiv.appendChild(pElement);});historyDiv.scrollTop=historyDiv.scrollHeight;}// spaceを押している間だけ、音声を受け付けるfunctionhandleKeyDown(event){if(event.code==='Space'&&!isRecognitionActive()){isSpaceKeyDown=true;startRecording();}}functionisRecognitionActive(){returnstartButton.disabled&&!stopButton.disabled;}functionhandleKeyUp(event){if(event.code==='Space'){isSpaceKeyDown=false;stopRecording();}}functioninit(){document.body.addEventListener('keydown',handleKeyDown);document.body.addEventListener('keyup',handleKeyUp);}init();