File size: 3,987 Bytes
e608215
 
 
 
 
 
d763513
e608215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests from "./request.js";
class Initialize{
    constructor(uiManager){
        this.convId;
        this.convTitle;
        this.uiManager = uiManager;
        this.systemPrompt = `your response syntax should be: ###for heading### \n ** for sub heading ** \n *** for text highlight/bold text ***`;
        this.model = null;
    }

    async initialize(model=null){
        this.convTitle=null;
        this.convId=null;
        this.uiManager.messagesDiv.innerHTML = '';
        this.uiManager.prevChatsCont.innerHTML = '';
        await this.fetchConvs();
        document.querySelectorAll('.prevChat').forEach((elem)=>{
            elem.addEventListener('click', ()=>{
                this.reInitialize(elem.id)
            })
        })
        if(model!=null){
            this.model = model;
        } else{
            await this.fetchModels()
        }
    }

    async reInitialize(id){
        this.convTitle=null;
        this.convId=id;
        this.uiManager.messagesDiv.innerHTML = '';
        await this.fetchConv(id);
    }

    async fetchConv(id){
        try {
            const response = await requests.request('POST','/fetch',{"Content-Type": "application/json"},JSON.stringify({"convId": id}),false);
            if(!response.ok){
                alert('error while fetching conversations')
                return
            }
            const data = await response.json();
            this.convTitle = data['title'];
            const arr = data['messages'];
            for(let i=0;i<arr.length;i++){
                const dict = arr[i];
                if(dict['role']=='user'){
                    this.uiManager.appendUserMsg(dict['content'])
                }
                else if(dict['role']=='assistant'){
                    this.uiManager.appendAiMsg(dict['content']);
                    this.uiManager.renderSymbols.renderAll(this.uiManager.aiP);
                }
            }
        } catch (error) {
            alert(`an error occured ${error}`)
            console.log(error)
        }
    }
    async fetchConvs(){
        try {
            const response = await requests.request('GET','/convs',{"Content-Type": "application/json"},null,false);
            if(!response.ok){
                alert('error while fetching conversations')
                return
            }
            const data = await response.json();
            for(let i=0;i<data.length;i++){
                const prevChat = document.createElement('div');
                const dict = data[i];
                prevChat.id = dict['convId'];
                prevChat.className = 'prevChat';
                prevChat.innerText = dict['title'];
                this.uiManager.prevChatsCont.appendChild(prevChat)

            }
        } catch (error) {
            alert(`an error ocuured ${error}`)
        }
    }
    async createConv(){
        const response = await requests.request('POST', '/create', {"Content-Type": "application/json"},JSON.stringify({"system_prompt": this.systemPrompt}),false)
        if(!response.ok){
            alert('error while creating new Conversation')
            return
        }
        const data = await response.json()
        this.convId = data['convId']
    }
    async fetchModels(){
        const response = await requests.request('GET', '/models', {"Content-Type": "application/json"},null,false)
        if(!response.ok){
            alert('error while fetching models')
            return
        }
        const data = await response.json();
        this.model = data[0];
        this.uiManager.models.innerHTML = '';
        for(let i=0; i<data.length;i++){
            const opt = document.createElement('option')
            opt.innerText = data[i];
            this.uiManager.models.appendChild(opt)
        }
        this.uiManager.models.addEventListener('change', (e)=>{
            const selected = e.target.value;
            this.initialize(selected)
        })

    }
}
export default Initialize