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

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

Browse files
application/static/js/components/initialize.js CHANGED
@@ -8,43 +8,47 @@ class Initialize {
8
  this.uiManager = uiManager;
9
  this.systemPrompt = `your response syntax should be: ###for heading### \n ** for sub heading ** \n *** for text highlight/bold text ***`;
10
  this.model = null;
11
- this.image_captioning = this.uiManager.image_captioning; // Get from uiManager
 
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
 
40
  async fetchConv(id) {
 
41
  try {
42
  const response = await requests.request('POST', '/fetch', { "Content-Type": "application/json" }, JSON.stringify({ "convId": id }), false);
43
  if (!response.ok) {
44
  const errorText = await response.text();
45
- throw new Error(`Error fetching conversation: ${response.status} - ${errorText}`);
 
46
  }
47
  const data = await response.json();
 
48
  this.convTitle = data['title'];
49
  const arr = data['messages'];
50
  for (let i = 0; i < arr.length; i++) {
@@ -58,20 +62,22 @@ class Initialize {
58
  }
59
  }
60
  } catch (error) {
61
- alert(`An error occurred: ${error}`);
62
- console.error(error);
63
  }
64
  }
65
 
66
  async fetchConvs() {
 
67
  try {
68
  const response = await requests.request('GET', '/convs', { "Content-Type": "application/json" }, null, false);
69
  if (!response.ok) {
70
  const errorText = await response.text();
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++) {
@@ -82,47 +88,47 @@ class Initialize {
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
  });
89
  }
90
  } catch (error) {
91
- alert(`An error occurred: ${error}`);
92
- console.error(error);
93
  }
94
  }
95
 
96
-
97
  async fetchModels(initialModel) {
 
98
  try {
99
  const response = await requests.request('GET', '/models', { "Content-Type": "application/json" }, null, false);
100
  if (!response.ok) {
101
  const errorText = await response.text();
102
- throw new Error(`Error fetching models: ${response.status} - ${errorText}`);
 
103
  }
104
  const data = await response.json();
105
- this.uiManager.models.innerHTML = ''; // Clear existing options
 
106
 
107
  for (let i = 0; i < data.length; i++) {
108
  const opt = document.createElement('option');
109
  opt.innerText = data[i];
110
- opt.value = data[i]; // Set the value attribute
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) {
124
- alert(`An error occurred: ${error}`);
125
- console.error(error);
126
  }
127
  }
128
  }
 
8
  this.uiManager = uiManager;
9
  this.systemPrompt = `your response syntax should be: ###for heading### \n ** for sub heading ** \n *** for text highlight/bold text ***`;
10
  this.model = null;
11
+ this.image_captioning = this.uiManager.image_captioning;
12
+ console.log("Initialize constructor called"); // Initial log
13
  }
14
 
15
  async initialize(model = null) {
16
+ console.log("initialize called with model:", model);
17
  this.convTitle = null;
18
  this.convId = null;
19
  this.uiManager.messagesDiv.innerHTML = '';
20
+ this.uiManager.prevChatsCont.innerHTML = '';
21
+ console.log("initialize: UI reset");
22
 
 
23
  await this.fetchModels(model);
24
+ console.log("initialize: fetchModels completed");
25
 
 
26
  await this.fetchConvs();
27
+ console.log("initialize: fetchConvs completed");
 
28
  }
29
 
30
  async reInitialize(id) {
31
+ console.log("reInitialize called with id:", id);
32
  this.convTitle = null;
33
  this.convId = id;
34
  this.uiManager.messagesDiv.innerHTML = '';
35
+ console.log("reInitialize: UI reset");
36
 
 
37
  await this.fetchConv(id);
38
+ console.log("reInitialize: fetchConv completed");
39
  }
40
 
41
  async fetchConv(id) {
42
+ console.log("fetchConv called with id:", id);
43
  try {
44
  const response = await requests.request('POST', '/fetch', { "Content-Type": "application/json" }, JSON.stringify({ "convId": id }), false);
45
  if (!response.ok) {
46
  const errorText = await response.text();
47
+ console.error(`Error fetching conversation: ${response.status} - ${errorText}`);
48
+ return; // Early return on error
49
  }
50
  const data = await response.json();
51
+ console.log("fetchConv: data received:", data);
52
  this.convTitle = data['title'];
53
  const arr = data['messages'];
54
  for (let i = 0; i < arr.length; i++) {
 
62
  }
63
  }
64
  } catch (error) {
65
+ console.error("fetchConv: Error:", error);
 
66
  }
67
  }
68
 
69
  async fetchConvs() {
70
+ console.log("fetchConvs called");
71
  try {
72
  const response = await requests.request('GET', '/convs', { "Content-Type": "application/json" }, null, false);
73
  if (!response.ok) {
74
  const errorText = await response.text();
75
+ console.error(`Error fetching conversations: ${response.status} - ${errorText}`);
76
+ return; // Early return on error
77
+
78
  }
79
  const data = await response.json();
80
+ console.log("fetchConvs: data received:", data);
81
  this.uiManager.prevChatsCont.innerHTML = '';
82
 
83
  for (let i = 0; i < data.length; i++) {
 
88
  prevChat.innerText = dict['title'];
89
  this.uiManager.prevChatsCont.appendChild(prevChat);
90
 
 
91
  prevChat.addEventListener('click', () => {
92
+ console.log("prevChat clicked, id:", prevChat.id); // Log click
93
  this.reInitialize(prevChat.id);
94
  });
95
  }
96
  } catch (error) {
97
+ console.error("fetchConvs: Error:", error);
 
98
  }
99
  }
100
 
 
101
  async fetchModels(initialModel) {
102
+ console.log("fetchModels called with initialModel:", initialModel);
103
  try {
104
  const response = await requests.request('GET', '/models', { "Content-Type": "application/json" }, null, false);
105
  if (!response.ok) {
106
  const errorText = await response.text();
107
+ console.error(`Error fetching models: ${response.status} - ${errorText}`);
108
+ return; // Early return on error
109
  }
110
  const data = await response.json();
111
+ console.log("fetchModels: data received:", data);
112
+ this.uiManager.models.innerHTML = '';
113
 
114
  for (let i = 0; i < data.length; i++) {
115
  const opt = document.createElement('option');
116
  opt.innerText = data[i];
117
+ opt.value = data[i];
118
  this.uiManager.models.appendChild(opt);
119
  }
120
 
 
121
  this.model = initialModel || data[0];
122
+ this.uiManager.models.value = this.model;
123
+ console.log("fetchModels: model set to:", this.model);
124
 
 
125
  this.uiManager.models.addEventListener('change', (e) => {
126
+ console.log("Model changed to:", e.target.value);
127
  this.model = e.target.value;
128
  });
129
 
130
  } catch (error) {
131
+ console.error("fetchModels: Error:", error);
 
132
  }
133
  }
134
  }