File size: 2,122 Bytes
6b3d6fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$(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
});