File size: 2,797 Bytes
6e6dab9
ac03bb3
 
 
 
af38c19
ac03bb3
6e6dab9
af38c19
 
 
 
 
 
 
 
6e6dab9
 
 
af38c19
6e6dab9
ac03bb3
6e6dab9
6ed2c8f
 
 
 
4a1d039
 
 
 
 
6ed2c8f
077080c
6e6dab9
 
 
6ed2c8f
077080c
 
 
 
4a1d039
 
077080c
4a1d039
077080c
6ed2c8f
 
 
 
 
077080c
6e6dab9
451c088
 
 
ed18d7d
 
6e6dab9
 
077080c
 
 
4a1d039
ac03bb3
c98264e
077080c
ac03bb3
077080c
 
 
4a1d039
077080c
ac03bb3
 
4a1d039
 
 
c5c7ecf
1be3dd1
077080c
af38c19
 
 
 
6e6dab9
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
import { ChatCompletionsRequester } from "./llm_requester.js";
import {
    pop_messager,
    stop_latest_message_animation,
    start_latest_message_animation,
    create_new_chat_session,
} from "./chat_operator.js";

export function bind_chat_buttons() {
    let send_binder = new SendUserInputButtonBinder();
    send_binder.bind();
    let new_binder = new NewChatButtonBinder();
    new_binder.bind();
}

class SendUserInputButtonBinder {
    constructor() {
        this.requester = null;
    }
    bind() {
        const button = $("#send-user-input");
        button.attr("status", "send").attr("title", "Send");
        button.click(async () => {
            await this.handle_user_input(button);
        });

        $("#user-input").keypress(async (event) => {
            if (
                event.key === "Enter" &&
                !event.shiftKey &&
                button.attr("status") === "send"
            ) {
                event.preventDefault();
                await this.send(button);
            }
        });
    }
    async handle_user_input(button) {
        let user_input_content = $("#user-input").val();
        if (user_input_content === "") {
            return;
        }
        let status = button.attr("status");
        if (status === "send") {
            this.send(button);
        } else if (status === "stop") {
            this.stop(button);
            return;
        } else {
            console.log("No action");
        }
    }

    async post_user_input() {
        let user_input_content = $("#user-input").val();
        console.log(user_input_content);
        this.requester = new ChatCompletionsRequester(user_input_content);
        this.requester.create_messager_components();
        start_latest_message_animation();
        await this.requester.post();
    }

    async send(button) {
        console.log("Send");
        let button_icon = button.find("i");
        button.attr("status", "stop").attr("title", "Stop");
        button_icon.removeClass().addClass("fa fa-circle-pause fa-fade-fast");
        await this.post_user_input();
        await this.stop(button);
    }
    async stop(button) {
        console.log("Stop");
        let button_icon = button.find("i");
        this.requester.stop();
        stop_latest_message_animation();
        button.attr("status", "send").attr("title", "Send");
        button_icon
            .removeClass()
            .addClass("fa fa-paper-plane")
            .addClass("icon-success");
        hljs.highlightAll();
    }
}

class NewChatButtonBinder {
    constructor() {}
    bind() {
        const button = $("#new-chat-session");
        button.attr("status", "new").attr("title", "New Chat");
        button.click(() => {
            create_new_chat_session();
        });
    }
}