Reality123b commited on
Commit
e3f1fd9
·
verified ·
1 Parent(s): dfc6217

Update application/static/js/components/initialize.js

Browse files
application/static/js/components/initialize.js CHANGED
@@ -3,7 +3,7 @@ import requests from "./request.js";
3
 
4
  class Initialize {
5
  constructor(uiManager) {
6
- this.convId = null; // Initialize to null
7
  this.convTitle = null;
8
  this.uiManager = uiManager;
9
  this.systemPrompt = `your response syntax should be: ###for heading### \n ** for sub heading ** \n *** for text highlight/bold text ***`;
@@ -12,18 +12,28 @@ class Initialize {
12
  }
13
 
14
  async initialize(model = null) {
 
15
  this.convTitle = null;
16
  this.convId = null;
17
  this.uiManager.messagesDiv.innerHTML = '';
18
- // No need to clear prevChatsCont here; it's done in fetchConvs
19
- await this.fetchModels(model); // Fetch models *first*
20
- await this.fetchConvs(); // *Then* fetch conversations
 
 
 
 
 
 
21
  }
22
 
23
  async reInitialize(id) {
 
24
  this.convTitle = null;
25
  this.convId = id;
26
  this.uiManager.messagesDiv.innerHTML = '';
 
 
27
  await this.fetchConv(id);
28
  }
29
 
@@ -61,7 +71,9 @@ class Initialize {
61
  throw new Error(`Error fetching conversations: ${response.status} - ${errorText}`);
62
  }
63
  const data = await response.json();
64
- this.uiManager.prevChatsCont.innerHTML = ''; // Clear here
 
 
65
  for (let i = 0; i < data.length; i++) {
66
  const prevChat = document.createElement('div');
67
  const dict = data[i];
@@ -70,7 +82,7 @@ class Initialize {
70
  prevChat.innerText = dict['title'];
71
  this.uiManager.prevChatsCont.appendChild(prevChat);
72
 
73
- // Add event listener *inside* the loop, after appending
74
  prevChat.addEventListener('click', () => {
75
  this.reInitialize(prevChat.id);
76
  });
@@ -99,15 +111,13 @@ class Initialize {
99
  this.uiManager.models.appendChild(opt);
100
  }
101
 
102
- // Set the initial model, or the first one if none provided
103
  this.model = initialModel || data[0];
104
  this.uiManager.models.value = this.model; // Set selected option
105
 
 
106
  this.uiManager.models.addEventListener('change', (e) => {
107
- const selected = e.target.value;
108
- this.model = selected; // Update the model
109
- // this.initialize(selected); // Don't re-initialize the entire UI.
110
- // Just update this.model. Re-initializing is too disruptive.
111
  });
112
 
113
  } catch (error) {
 
3
 
4
  class Initialize {
5
  constructor(uiManager) {
6
+ this.convId = null;
7
  this.convTitle = null;
8
  this.uiManager = uiManager;
9
  this.systemPrompt = `your response syntax should be: ###for heading### \n ** for sub heading ** \n *** for text highlight/bold text ***`;
 
12
  }
13
 
14
  async initialize(model = null) {
15
+ // 1. Reset UI state *before* any async calls. This is CRUCIAL.
16
  this.convTitle = null;
17
  this.convId = null;
18
  this.uiManager.messagesDiv.innerHTML = '';
19
+ this.uiManager.prevChatsCont.innerHTML = ''; // Clear conversations here
20
+
21
+ // 2. Fetch models. Await the result.
22
+ await this.fetchModels(model);
23
+
24
+ // 3. Fetch conversations. Await the result.
25
+ await this.fetchConvs();
26
+
27
+ // 4. The UI is now fully initialized and consistent.
28
  }
29
 
30
  async reInitialize(id) {
31
+ // 1. Reset UI state *before* any async calls.
32
  this.convTitle = null;
33
  this.convId = id;
34
  this.uiManager.messagesDiv.innerHTML = '';
35
+
36
+ // 2. Fetch the specific conversation. Await the result.
37
  await this.fetchConv(id);
38
  }
39
 
 
71
  throw new Error(`Error fetching conversations: ${response.status} - ${errorText}`);
72
  }
73
  const data = await response.json();
74
+ // Clear conversations *inside* fetchConvs, before adding new ones
75
+ this.uiManager.prevChatsCont.innerHTML = '';
76
+
77
  for (let i = 0; i < data.length; i++) {
78
  const prevChat = document.createElement('div');
79
  const dict = data[i];
 
82
  prevChat.innerText = dict['title'];
83
  this.uiManager.prevChatsCont.appendChild(prevChat);
84
 
85
+ // Attach event listener *after* appending to the DOM.
86
  prevChat.addEventListener('click', () => {
87
  this.reInitialize(prevChat.id);
88
  });
 
111
  this.uiManager.models.appendChild(opt);
112
  }
113
 
114
+ // Set the initial model. Use initialModel if provided, otherwise default to the first model.
115
  this.model = initialModel || data[0];
116
  this.uiManager.models.value = this.model; // Set selected option
117
 
118
+ // Only update this.model on change. Do NOT re-initialize.
119
  this.uiManager.models.addEventListener('change', (e) => {
120
+ this.model = e.target.value;
 
 
 
121
  });
122
 
123
  } catch (error) {