// 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(); // REMOVED - rely on request.js console.error(`Error fetching conversation: ${response.status}`); // Simplified 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(); // REMOVED - rely on request.js console.error(`Error fetching conversations: ${response.status}`); // Simplified 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(); // REMOVED - rely on request.js console.error(`Error fetching models: ${response.status}`); // Simplified 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) { // No need to call response.text() here. The error is already being thrown by request.js // const errorText = await response.text(); // THIS LINE IS REMOVED throw new Error(`Error creating conversation: ${response.status}`); // Simplified error } 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