|
320 | 320 | // Add the user's input to the chat history, and spawn a new thinking chatbot response placeholder |
321 | 321 | function addUserMessage_andChatbotPlaceholder(user_input) { |
322 | 322 | const all_messages_div = document.getElementById('all-messages-div'); |
323 | | - |
| 323 | + const text_area = document.querySelector('#user-input-for-demo'); |
| 324 | + |
324 | 325 | const user_div = document.createElement('div'); |
325 | 326 | user_div.classList.add('user-message'); |
326 | 327 |
|
327 | 328 | const user_span_for_message = document.createElement('span'); |
328 | 329 | user_div.appendChild(user_span_for_message); |
329 | | - |
330 | 330 | user_span_for_message.textContent = user_input; |
| 331 | + |
| 332 | + if (Array.from(user_input).length >= text_area.maxLength) { |
| 333 | + const user_text_limit_update = document.createElement('div'); |
| 334 | + user_text_limit_update.classList.add('char-limit-mesg'); |
| 335 | + user_div.appendChild(user_text_limit_update); |
| 336 | + user_text_limit_update.textContent = "Char limit reached - truncated to 10,000."; |
| 337 | + } |
| 338 | + |
| 339 | + |
331 | 340 | all_messages_div.insertBefore(user_div, all_messages_div.firstChild); |
332 | 341 |
|
| 342 | + |
| 343 | + |
| 344 | + |
333 | 345 | const chatbot_div = document.createElement('div'); |
334 | 346 | chatbot_div.classList.add('chatbot-message'); |
335 | 347 |
|
|
397 | 409 | } |
398 | 410 |
|
399 | 411 |
|
| 412 | +function tellUserServerIsThinking() { |
| 413 | + document.getElementById('all_messages_div') |
| 414 | + const current_chatbot_message = document.querySelector("#all-messages-div .chatbot-message"); |
| 415 | + if (current_chatbot_message) { |
| 416 | + const ads_loader_element = current_chatbot_message.querySelector("span ads-loader"); |
| 417 | + |
| 418 | + ads_loader_element.setAttribute('label','Still thinking, one moment...') |
| 419 | + } |
| 420 | +} |
| 421 | + |
400 | 422 |
|
401 | 423 | // Event listener additions to key demo components |
402 | 424 | document.addEventListener("DOMContentLoaded", function() { |
|
442 | 464 |
|
443 | 465 | // User enters any text in textinput |
444 | 466 | text_area.addEventListener('input', function() { |
445 | | - //this.style.height = 'auto'; |
446 | | - //this.style.height = (this.scrollHeight > this.clientHeight ? this.scrollHeight : this.clientHeight) + 'px'; |
447 | 467 | adjustInputHeight(); |
448 | 468 | toggleButton(); // enable input button |
449 | 469 | }); |
| 470 | + |
450 | 471 | // User hits enter in textinput |
451 | 472 | text_area.addEventListener('keydown', function(event) { |
452 | 473 | if (event.key === 'Enter') { |
|
541 | 562 |
|
542 | 563 | // Set timeout |
543 | 564 | const controller = new AbortController(); |
544 | | - let timeout; |
545 | | - function resetTimeout() { |
| 565 | + let timeout; // for server timeout |
| 566 | + let tell_user_timeout; // to inform user the UI is thinking |
| 567 | + const timeout_duration = 5000; // 5 seconds by default |
| 568 | + const tell_user_timeout_duration = 6000; // 6 seconds by default |
| 569 | + function resetTimeout(duration = timeout_duration) { |
546 | 570 | clearTimeout(timeout); |
547 | | - timeout = setTimeout(() => controller.abort(), 5000); // Adjust the timeout duration as needed |
| 571 | + clearTimeout(tell_user_timeout); |
| 572 | + |
| 573 | + |
| 574 | + timeout = setTimeout(() => controller.abort(), duration); |
| 575 | + |
| 576 | + if (duration > 5000) { |
| 577 | + tell_user_timeout = setTimeout(() => { |
| 578 | + tellUserServerIsThinking(); |
| 579 | + }, tell_user_timeout_duration); // trigger at 6 seconds |
| 580 | + } |
| 581 | + |
548 | 582 | } |
549 | 583 | resetTimeout(); // Start the timeout right before sending |
550 | 584 |
|
|
592 | 626 | } |
593 | 627 | else if (return_json.value == 'STREAM_STARTING') { |
594 | 628 | hidePopup(); |
| 629 | + resetTimeout(15000); // Set timeout to 15 seconds |
595 | 630 | } |
596 | 631 | } |
597 | 632 | else if (return_json.message_type === 'chunk') { |
|
0 commit comments