File size: 4,575 Bytes
3a17e83
58f9352
7bb4d19
 
fb77726
bdd842c
af38c19
 
 
a53437c
d1f0f2d
fb77726
7dd081a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af38c19
 
 
 
4b68ad4
 
 
 
7bb4d19
 
 
 
 
 
 
 
 
 
 
 
 
af38c19
7dd081a
7bb4d19
 
fb77726
 
 
 
bdd842c
68f11c9
bdd842c
 
ac03bb3
af38c19
ac03bb3
4a8deab
ac03bb3
 
 
c98264e
 
 
 
65e47ec
 
 
 
ac03bb3
65e47ec
c98264e
 
65e47ec
ac03bb3
c98264e
ac03bb3
c98264e
7bb4d19
ac03bb3
 
65e47ec
c98264e
 
65e47ec
ac03bb3
c98264e
ac03bb3
c98264e
ac03bb3
 
7bb4d19
af38c19
7bb4d19
 
077080c
af38c19
077080c
 
4a8deab
 
 
7bb4d19
 
 
 
 
 
 
 
 
 
 
7445f95
 
 
a53437c
4b68ad4
 
a53437c
 
d1f0f2d
a53437c
 
d1f0f2d
 
 
7dd081a
7bb4d19
 
 
 
 
 
 
 
af38c19
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Messager, MessagerList } from "./messager.js";
import { transform_footnote } from "../converters/stream_jsonizer.js";

let messagers_container = $("#messagers-container");
let available_models_select = $("#available-models-select");
let temperature_select = $("#temperature-select");

let messager_list = new MessagerList(messagers_container);
let chat_history = [messager_list];
let md_to_html_converter = new showdown.Converter();
md_to_html_converter.setFlavor("github");

let is_user_scrolling = false;

export function set_user_scroll_status(val = true) {
    is_user_scrolling = val;
}

export function scroll_to_bottom(animate = false) {
    if (is_user_scrolling) {
        return;
    }
    console.log("scroll_to_bottom");
    if (animate) {
        $("#chat-session-container").animate(
            {
                scrollTop: $("#chat-session-container").prop("scrollHeight"),
            },
            500
        );
    } else {
        $("#chat-session-container").prop(
            "scrollTop",
            $("#chat-session-container").prop("scrollHeight")
        );
    }
}

export function get_active_messager_list() {
    return chat_history[chat_history.length - 1];
}

export function get_latest_messager() {
    return get_active_messager_list().messagers.slice(-1)[0];
}

export function create_messager(
    role,
    content = "",
    model = "",
    temperature = ""
) {
    let message = {
        role: role,
        content: content,
        model: model,
        temperature: temperature,
    };
    let messager = new Messager(message);
    get_active_messager_list().push(messager);
    scroll_to_bottom();
}

export function get_selected_llm_model() {
    return available_models_select.val();
}

export function get_selected_temperature() {
    return Number(temperature_select.val());
}

export function get_latest_messager_container() {
    return get_active_messager_list().messagers_container.children().last();
}
export function get_latest_message_content_displayer() {
    return get_latest_messager_container().find(".content-displayer");
}

export function get_latest_user_messager() {
    return $(".message-user").last();
}

export function get_latest_assistant_messager() {
    return $(".message-assistant").last();
}

export function start_latest_message_animation() {
    get_latest_assistant_messager()
        .find(".content-displayer")
        .addClass("blinking");
    get_latest_assistant_messager()
        .find(".button-group")
        .find(".regenerate-button")
        .find("i")
        .addClass("fa-spin-fast");
}

export function stop_latest_message_animation() {
    get_latest_assistant_messager()
        .find(".content-displayer")
        .removeClass("blinking");
    get_latest_assistant_messager()
        .find(".button-group")
        .find(".regenerate-button")
        .find("i")
        .removeClass("fa-spin-fast");
}

export function get_request_messages() {
    return get_active_messager_list().get_request_messages();
}

export function pop_messager(n = 2) {
    return get_active_messager_list().pop(n);
}

export function update_message(json_chunks, content_displayer = null) {
    if (content_displayer === null) {
        content_displayer = get_latest_message_content_displayer();
    }
    json_chunks.forEach(function (item) {
        let choice = item.choices[0];
        let content = choice.delta.content;
        let role = choice.delta.role;
        let finish_reason = choice.finish_reason;
        if (role) {
            console.log("role: " + role);
        }
        if (content) {
            console.log(content);
            content_displayer.data(
                "raw_content",
                content_displayer.data("raw_content") + content
            );
            get_active_messager_list().messagers.slice(-1)[0].message.content +=
                content;
            content_displayer.html(
                md_to_html_converter.makeHtml(
                    transform_footnote(content_displayer.data("raw_content"))
                )
            );
            content_displayer
                .find("table")
                .addClass("table table-bordered table-hover");
            scroll_to_bottom();
        }
        if (finish_reason === "stop") {
            console.log("[STOP]");
        }
        console.log(item);
    });
    return json_chunks;
}

export function create_new_chat_session() {
    let new_messager_list = new MessagerList(messagers_container);
    chat_history.push(new_messager_list);
    messagers_container.empty();
}