Spaces:
Sleeping
Sleeping
File size: 6,212 Bytes
064fb37 e608215 dfc6217 e3f1fd9 dfc6217 e608215 d763513 e608215 6bd3f87 e608215 dfc6217 6bd3f87 dfc6217 e608215 6bd3f87 e3f1fd9 6bd3f87 e3f1fd9 6bd3f87 e608215 dfc6217 6bd3f87 dfc6217 e608215 6bd3f87 e3f1fd9 e608215 6bd3f87 e608215 dfc6217 6bd3f87 e608215 dfc6217 6bd3f87 e608215 6bd3f87 e608215 dfc6217 e608215 dfc6217 e608215 dfc6217 e608215 6bd3f87 e608215 dfc6217 6bd3f87 e608215 dfc6217 6bd3f87 e608215 6bd3f87 e3f1fd9 dfc6217 e608215 dfc6217 e608215 dfc6217 6bd3f87 dfc6217 e608215 6bd3f87 e608215 dfc6217 6bd3f87 dfc6217 6bd3f87 dfc6217 6bd3f87 dfc6217 6bd3f87 dfc6217 6bd3f87 dfc6217 6bd3f87 e3f1fd9 dfc6217 6bd3f87 dfc6217 e608215 aa217cb e608215 dfc6217 |
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 |
// application/static/js/components/initialize.js
import requests from "./request.js";
class Initialize {
constructor(uiManager) {
this.convId = null;
this.convTitle = null;
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;
console.log("Initialize constructor called"); // Initial log
}
async initialize(model = null) {
console.log("initialize called with model:", model);
this.convTitle = null;
this.convId = null;
this.uiManager.messagesDiv.innerHTML = '';
this.uiManager.prevChatsCont.innerHTML = '';
console.log("initialize: UI reset");
await this.fetchModels(model);
console.log("initialize: fetchModels completed");
await this.fetchConvs();
console.log("initialize: fetchConvs completed");
}
async reInitialize(id) {
console.log("reInitialize called with id:", id);
this.convTitle = null;
this.convId = id;
this.uiManager.messagesDiv.innerHTML = '';
console.log("reInitialize: UI reset");
await this.fetchConv(id);
console.log("reInitialize: fetchConv completed");
}
async fetchConv(id) {
console.log("fetchConv called with id:", id);
try {
const response = await requests.request('POST', '/fetch', { "Content-Type": "application/json" }, JSON.stringify({ "convId": id }), false);
if (!response.ok) {
const errorText = await response.text();
console.error(`Error fetching conversation: ${response.status} - ${errorText}`);
return; // Early return on error
}
const data = await response.json();
console.log("fetchConv: data received:", data);
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) {
console.error("fetchConv: Error:", error);
}
}
async fetchConvs() {
console.log("fetchConvs called");
try {
const response = await requests.request('GET', '/convs', { "Content-Type": "application/json" }, null, false);
if (!response.ok) {
const errorText = await response.text();
console.error(`Error fetching conversations: ${response.status} - ${errorText}`);
return; // Early return on error
}
const data = await response.json();
console.log("fetchConvs: data received:", data);
this.uiManager.prevChatsCont.innerHTML = '';
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);
prevChat.addEventListener('click', () => {
console.log("prevChat clicked, id:", prevChat.id); // Log click
this.reInitialize(prevChat.id);
});
}
} catch (error) {
console.error("fetchConvs: Error:", error);
}
}
async fetchModels(initialModel) {
console.log("fetchModels called with initialModel:", initialModel);
try {
const response = await requests.request('GET', '/models', { "Content-Type": "application/json" }, null, false);
if (!response.ok) {
const errorText = await response.text();
console.error(`Error fetching models: ${response.status} - ${errorText}`);
return; // Early return on error
}
const data = await response.json();
console.log("fetchModels: data received:", data);
this.uiManager.models.innerHTML = '';
for (let i = 0; i < data.length; i++) {
const opt = document.createElement('option');
opt.innerText = data[i];
opt.value = data[i];
this.uiManager.models.appendChild(opt);
}
this.model = initialModel || data[0];
this.uiManager.models.value = this.model;
console.log("fetchModels: model set to:", this.model);
this.uiManager.models.addEventListener('change', (e) => {
console.log("Model changed to:", e.target.value);
this.model = e.target.value;
});
} catch (error) {
console.error("fetchModels: Error:", error);
}
}
// ADD THIS NEW METHOD
async createConv() {
console.log("createConv called");
try {
const response = await requests.request('POST', '/create', { "Content-Type": "application/json" }, JSON.stringify({ "system_prompt": this.systemPrompt }), false);
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Error creating conversation: ${response.status} - ${errorText}`);
}
const data = await response.json();
this.convId = data['convId']; // Set the convId
console.log("createConv: New convId:", this.convId);
return this.convId; // Return the new convId
} catch (error) {
alert(`An error occurred creating a conversation: ${error}`);
console.error(error);
return null; // Return null on error
}
}
}
export default Initialize |