PMBeta / static /index.js
Sergidev's picture
Upload static v1
6b3d6fc verified
raw
history blame
2.12 kB
$(document).ready(function() {
var memoryMode = 'full';
$('#memory-toggle').change(function() {
memoryMode = $(this).is(':checked') ? 'smart' : 'full';
});
$('#chat-form').submit(function(event) {
event.preventDefault();
var userInput = $('#user-input').val();
$('#chat-container').append('<div class="message user-message"><i class="fas fa-user icon"></i>' + userInput + '</div>');
$('#user-input').val('');
$('#send-button').prop('disabled', true);
$('#loading-message').show();
var $botMessage = $('<div class="message bot-message"><i class="fas fa-robot icon"></i></div>');
$('#chat-container').append($botMessage);
var botResponse = '';
$.ajax({
url: '/chat',
method: 'POST',
data: JSON.stringify({ user_input: userInput, mode: memoryMode }),
contentType: 'application/json',
dataType: 'text', // Add this line to handle the response as text
xhrFields: {
onprogress: function(e) {
var chunk = e.currentTarget.response.slice(botResponse.length);
botResponse += chunk;
$botMessage.html('<i class="fas fa-robot icon"></i>' + botResponse.replace(/\n/g, '<br>'));
$('#chat-container').scrollTop($('#chat-container')[0].scrollHeight);
}
},
success: function() {
$('#send-button').prop('disabled', false);
$('#loading-message').hide();
},
error: function(xhr, status, error) {
$('#send-button').prop('disabled', false);
$('#loading-message').hide();
var errorMessage = '<div class="message error-message"><i class="fas fa-exclamation-triangle icon"></i>Error: ' + error + '</div>';
$('#chat-container').append(errorMessage);
}
});
});
setInterval(function() {
$.post('/sleep');
}, 20000); // set to 50 seconds, usually 2 minutes in milliseconds
});