File size: 1,991 Bytes
6e6dab9
 
 
 
 
 
 
 
 
6ed2c8f
 
 
 
6e6dab9
6ed2c8f
 
 
 
 
 
 
6e6dab9
 
 
6ed2c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e6dab9
451c088
 
 
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
import { ChatCompletionsRequester } from "./llm_requester.js";

export class ButtonsBinder {
    constructor() {
        this.requester = null;
    }
    bind_send_user_input() {
        const button = $("#send-user-input");
        button.click(async () => {
            await this.handle_user_input(button);
        });

        $("#user-input").keypress(async (event) => {
            let status = button.text().trim();
            if (
                event.key === "Enter" &&
                !event.shiftKey &&
                (status === "Send" || status === "Regenerate")
            ) {
                event.preventDefault();
                await this.handle_user_input(button);
            }
        });
    }
    async handle_user_input(button) {
        let status = button.text().trim();
        if (status === "Send" || status === "Regenerate") {
            console.log("Send");
            button
                .text("Stop")
                .removeClass("btn-primary btn-success")
                .addClass("btn-warning");
            await this.post_user_input();
            button
                .text("Regenerate")
                .removeClass("btn-warning")
                .addClass("btn-success")
                .attr("disabled", false);
        } else if (status === "Stop") {
            console.log("Stop");
            this.requester.stop();
            button
                .attr("disabled", true)
                .removeClass("btn-warning")
                .addClass("btn-success")
                .text("Regenerate")
                .attr("disabled", false);
            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);
        await this.requester.post();
    }
    bind() {
        this.bind_send_user_input();
    }
}