File size: 3,178 Bytes
bdd842c
 
 
 
af38c19
6e6dab9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53b13af
 
 
 
 
 
 
 
af38c19
6e6dab9
bdd842c
8e9147b
af38c19
8e9147b
 
 
6e6dab9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02acd51
 
 
2e70541
02acd51
4006506
02acd51
 
 
6e6dab9
53b13af
af38c19
6e6dab9
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
    setup_available_models_on_select,
    setup_temperature_on_select,
} from "./llm_models_loader.js";
import { bind_chat_buttons } from "./buttons_binder.js";
var user_input_history = [];
var user_input_history_idx = 0;

$("#user-input").on("enter", function () {
    console.log("enter");
});

function auto_resize_user_input() {
    // https://stackoverflow.com/questions/37629860/automatically-resizing-textarea-in-bootstrap
    document.getElementById("user-input").addEventListener(
        "input",
        function () {
            this.style.height = 0;
            this.style.height = this.scrollHeight + 3 + "px";
        },
        false
    );
}

function load_live_js() {
    if (window.location.protocol !== "https:") {
        var script = document.createElement("script");
        script.src = "./js/common/live.js";
        document.head.appendChild(script);
    }
}

function setup_interactive_components() {
    setup_available_models_on_select();
    setup_temperature_on_select();
    auto_resize_user_input();
    bind_chat_buttons();
    register_user_input_history_buttons_callbacks();
    adjust_messagers_container_max_height();
    $(window).on("resize", adjust_messagers_container_max_height);
}

function register_user_input_history_buttons_callbacks() {
    set_user_input_history_buttons_state();
    $("#prev-user-input").click(function (event) {
        console.log("prev");
        user_input_history_idx = Math.max(0, user_input_history_idx - 1);
        user_input_history_value = user_input_history[user_input_history_idx];
        $("#user-input").val(user_input_history_value);
        set_user_input_history_buttons_state();
        event.preventDefault();
    });
    $("#next-user-input").click(function (event) {
        console.log("next");
        user_input_history_idx = Math.min(
            user_input_history.length - 1,
            user_input_history_idx + 1
        );
        user_input_history_value = user_input_history[user_input_history_idx];
        $("#user-input").val(user_input_history_value);
        set_user_input_history_buttons_state();
        event.preventDefault();
    });
}

function set_user_input_history_buttons_state() {
    let prev_user_input = $("#prev-user-input");
    let next_user_input = $("#next-user-input");
    if (user_input_history.length === 0) {
        prev_user_input.attr("disabled", true);
        next_user_input.attr("disabled", true);
    }
    if (user_input_history_idx >= user_input_history.length - 1) {
        next_user_input.attr("disabled", true);
    } else {
        next_user_input.attr("disabled", false);
    }

    if (user_input_history_idx <= 0) {
        prev_user_input.attr("disabled", true);
    } else {
        prev_user_input.attr("disabled", false);
    }
}

function adjust_messagers_container_max_height() {
    var user_interaction_height = $("#user-interactions").outerHeight(true);
    var page_height = $(window).height();
    $("#chat-session-container").css(
        "max-height",
        page_height - user_interaction_height + "px"
    );
}

$(document).ready(function () {
    load_live_js();
    setup_interactive_components();
});