@@ -97,7 +97,6 @@ function startMessage(speaker) {
9797 if ( speaker === "bot" && loadingIndicator ) {
9898 loadingIndicator . remove ( ) ;
9999 console . log ( "loadingremoved" ) ;
100-
101100 }
102101}
103102// Function to add content to the current message
@@ -166,42 +165,107 @@ function set_model() {
166165 const modelSelected = document . getElementById ( "select_model_btn" ) . value ;
167166
168167}
169- // Function to handle user input and send it to the server
170- function sendUserInput ( ) {
171- const inputField = document . getElementById ( "userInput" ) ;
172- const userPrompt = inputField . value . trim ( ) ;
173- const modelSelected = localStorage . getItem ( "selectedModel" ) ;
174-
175- if ( ! modelSelected ) {
176- alert ( "Please select a model" ) ;
177- return ;
168+
169+ function processQueue ( ) {
170+ if ( ! isSpeaking && textQueue . length > 0 ) {
171+ const sentence = textQueue . shift ( ) ; // Get the next sentence from the queue
172+ cleanText ( sentence ) ; // Prepare the text and start TTS
173+ isSpeaking = true ; // Set speaking status to true while TTS is active
174+ }
175+ }
176+
177+ // Function to end the current message and optionally show a loading indicator
178+ function endMessage ( ) {
179+ highlightAll ( ) ;
180+ cleanText ( text ) ;
181+ text = "" ;
182+ response_content = "" ;
183+ flow = false ;
184+ if ( currentSpeaker === "user" ) {
185+ console . log ( "user" ) ;
186+ showLoadingIndicator ( "Thinking..." )
187+ }
188+ currentSpeaker = null ;
178189 }
179190
180- if ( userPrompt ) {
181- fetch ( "/get_response" , {
191+ // Function to show a loading indicator
192+ function showLoadingIndicator ( text ) {
193+ // Create the loading indicator element
194+ loadingIndicator = document . createElement ( "div" ) ;
195+ loadingIndicator . className = "loading" ;
196+ loadingIndicator . innerHTML = text ;
197+ // Ensure messageContent is defined and append the loading indicator
198+ if ( messageContent ) {
199+ messageDiv . appendChild ( loadingIndicator ) ;
200+ console . log ( messageDiv , loadingIndicator ) ;
201+
202+ } else {
203+ console . error ( "messageContent is not defined" ) ;
204+ }
205+ }
206+
207+
208+ // Function to handle user input and send it to the server
209+ function sendUserInput ( ) {
210+ const inputField = document . getElementById ( "userInput" ) ;
211+ const userPrompt = inputField . value . trim ( ) ;
212+ const modelSelected = localStorage . getItem ( "selectedModel" ) ;
213+ if ( ! modelSelected ) {
214+ alert ( "Please select a model" ) ;
215+ return ;
216+ }
217+
218+ if ( userPrompt ) {
219+ fetch ( "/get_response" , {
220+ method : "POST" ,
221+ headers : { "Content-Type" : "application/json" } ,
222+ body : JSON . stringify ( { prompt : userPrompt , model_need : modelSelected } ) ,
223+ } )
224+ . then ( ( response ) => response . json ( ) )
225+ . then ( ( data ) => {
226+ console . log ( "Response received:" , data . response ) ;
227+ } )
228+ . catch ( ( error ) => console . error ( "Error:" , error ) ) ;
229+
230+ inputField . value = "" ; // Clear input field
231+ }
232+ }
233+
234+ // Function to create options for the model selection dropdown
235+ function createModelOption ( name ) {
236+ const selectElement = document . getElementById ( "select_model_btn" ) ;
237+ const option = document . createElement ( "option" ) ;
238+ option . value = name ;
239+ option . textContent = name ;
240+ selectElement . appendChild ( option ) ;
241+ }
242+
243+ // Fetch the list of models from the server and populate the dropdown
244+ function get_list ( ) {
245+ fetch ( "/list_request" , {
182246 method : "POST" ,
183247 headers : { "Content-Type" : "application/json" } ,
184248 body : JSON . stringify ( { prompt : userPrompt , model_need : modelSelected , new_chat : new_chat } ) ,
185249 } )
186250 . then ( ( response ) => response . json ( ) )
187251 . then ( ( data ) => {
188- console . log ( "Response received:" , data . response ) ;
252+ localStorage . setItem ( "list" , JSON . stringify ( data ) ) ;
253+ data . list . models . forEach ( ( model ) => createModelOption ( model . name ) ) ;
254+ data . list . models . forEach ( ( model ) => createModalList ( model . name ) ) ;
255+ showNotification ( "List fetched successfully" ) ;
256+ save_log ( "List fetched successfully" ) ;
189257 } )
190258 . catch ( ( error ) => console . error ( "Error:" , error ) ) ;
191259
192260 inputField . value = "" ; // Clear input field
193261 new_chat = false ;
194262 }
195- }
196263
197- // Function to create options for the model selection dropdown
198- function createModelOption ( name ) {
199- const selectElement = document . getElementById ( "select_model_btn" ) ;
200- const option = document . createElement ( "option" ) ;
201- option . value = name ;
202- option . textContent = name ;
203- selectElement . appendChild ( option ) ;
204- }
264+ // Event listeners for submitting input
265+ document . getElementById ( "submitButton" ) . onclick = sendUserInput ;
266+ document . getElementById ( "userInput" ) . addEventListener ( "keypress" , ( event ) => {
267+ if ( ! flow && event . key === "Enter" ) sendUserInput ( ) ;
268+ } ) ;
205269
206270// Fetch the list of models from the server and populate the dropdown
207271function get_list ( ) {
@@ -309,8 +373,7 @@ eventSource.onmessage = function (event) {
309373 addContent ( response ) ;
310374 break ;
311375 }
312- } ;
313-
376+ }
314377// Function to copy text to clipboard
315378function copyToClipboard ( text ) {
316379 const textArea = document . createElement ( "textarea" ) ;
@@ -323,7 +386,3 @@ function copyToClipboard(text) {
323386 document . body . removeChild ( textArea ) ;
324387 // alert("Copied to clipboard!");
325388}
326- const lastMessage = messageDiv . lastElementChild ;
327- if ( lastMessage ) {
328- lastMessage . scrollIntoView ( { behavior : "smooth" } ) ;
329- }
0 commit comments