CaffeinatedCoding commited on
Commit
1569f04
·
verified ·
1 Parent(s): bf73d3b

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. frontend/app.js +57 -9
frontend/app.js CHANGED
@@ -26,8 +26,15 @@ textarea.addEventListener("keydown", e => {
26
  });
27
 
28
  function showScreen(name) {
 
29
  document.querySelectorAll(".screen").forEach(s => s.classList.remove("active"));
30
- document.getElementById("screen-" + name).classList.add("active");
 
 
 
 
 
 
31
  }
32
 
33
  function toggleSidebar() {
@@ -98,24 +105,29 @@ function renderMessages() {
98
  async function submitQuery() {
99
  const query = textarea.value.trim();
100
  if (!query || isLoading) return;
101
- if (query.length < 5) { alert("Query too short (minimum 5 characters)"); return; }
102
  if (query.length > 1000) { alert("Query too long"); return; }
103
 
104
  setLoading(true);
 
 
105
 
106
  // Create session if needed and switch to chat view
107
  if (!activeSessionId) {
108
  createSession(query);
 
109
  }
110
 
111
  // Ensure chat screen is visible
112
  showScreen("chat");
 
113
 
114
  const session = getActiveSession();
115
 
116
  // Add user message to session and display
117
  session.messages.push({ role: "user", text: query });
118
  appendUserBubble(query);
 
119
 
120
  // Clear input
121
  textarea.value = "";
@@ -123,38 +135,52 @@ async function submitQuery() {
123
 
124
  // Show loading state
125
  const loaderId = appendLoader();
 
126
 
127
  try {
 
 
 
128
  const res = await fetch(`${API_BASE}/query`, {
129
  method: "POST",
130
  headers: { "Content-Type": "application/json" },
131
  body: JSON.stringify({
132
  query,
133
- session_id: activeSessionId ? String(activeSessionId) : "default"
134
  })
135
  });
136
 
 
 
 
137
  const data = await res.json();
 
 
138
  removeLoader(loaderId);
 
139
 
140
  if (!res.ok) {
141
- const msg = data.detail || "Error: please try again";
 
142
  session.messages.push({ role: "error", text: msg });
143
  appendErrorBubble(msg);
144
  } else {
 
145
  session.messages.push({ role: "ai", data });
146
  appendAIBubble(data);
 
147
  }
148
  } catch (err) {
149
  removeLoader(loaderId);
150
- console.error("Query error:", err);
151
- const msg = "Could not reach server - check console for details";
152
  session.messages.push({ role: "error", text: msg });
153
  appendErrorBubble(msg);
154
  }
155
 
156
  setLoading(false);
157
  scrollBottom();
 
158
  }
159
 
160
  function usesuggestion(queryText) {
@@ -165,6 +191,7 @@ function usesuggestion(queryText) {
165
  }
166
 
167
  function appendUserBubble(text, scroll = true) {
 
168
  const div = document.createElement("div");
169
  div.className = "flex items-start gap-4 ml-12 flex-row-reverse";
170
  div.innerHTML = `
@@ -177,12 +204,18 @@ function appendUserBubble(text, scroll = true) {
177
  </div>
178
  `;
179
  messagesList.appendChild(div);
 
180
  if (scroll) scrollBottom();
181
  }
182
 
183
  function appendAIBubble(data, scroll = true) {
 
 
 
 
184
  const verified = data.verification_status === true || data.verification_status === "verified";
185
- const sourceCount = (data.sources || []).length;
 
186
  const sourcesBtn = sourceCount > 0 ? `
187
  <button class="text-[11px] font-bold text-secondary uppercase tracking-widest flex items-center gap-1 hover:underline">
188
  <span class="material-symbols-outlined text-sm">description</span> ${sourceCount} Citation${sourceCount > 1 ? "s" : ""}
@@ -197,7 +230,7 @@ function appendAIBubble(data, scroll = true) {
197
  <div class="clay-card p-6 bg-white text-primary leading-relaxed shadow-sm max-w-2xl">
198
  <p class="text-sm font-medium mb-4">Registry Assistant</p>
199
  <div class="font-serif text-lg leading-relaxed text-primary/90">
200
- ${formatAnswer(data.answer)}
201
  </div>
202
  <div class="mt-6 pt-4 border-t border-primary/5 flex gap-4">
203
  ${sourcesBtn}
@@ -208,10 +241,12 @@ function appendAIBubble(data, scroll = true) {
208
  </div>
209
  `;
210
  messagesList.appendChild(div);
 
211
  if (scroll) scrollBottom();
212
  }
213
 
214
  function appendErrorBubble(text, scroll = true) {
 
215
  const div = document.createElement("div");
216
  div.className = "flex items-start gap-4 mr-12";
217
  div.innerHTML = `
@@ -224,6 +259,7 @@ function appendErrorBubble(text, scroll = true) {
224
  </div>
225
  `;
226
  messagesList.appendChild(div);
 
227
  if (scroll) scrollBottom();
228
  }
229
 
@@ -245,7 +281,12 @@ function appendLoader() {
245
  </div>
246
  </div>
247
  `;
 
 
 
 
248
  messagesList.appendChild(div);
 
249
  scrollBottom();
250
  return id;
251
  }
@@ -277,7 +318,14 @@ function scrollBottom() {
277
  }
278
 
279
  function formatAnswer(text) {
280
- if (!text) return "";
 
 
 
 
 
 
 
281
  return text.replace(/\n/g, "<br>");
282
  }
283
 
 
26
  });
27
 
28
  function showScreen(name) {
29
+ console.log("showScreen called with name:", name);
30
  document.querySelectorAll(".screen").forEach(s => s.classList.remove("active"));
31
+ const screenEl = document.getElementById("screen-" + name);
32
+ if (!screenEl) {
33
+ console.error("Screen element not found:", "screen-" + name);
34
+ return;
35
+ }
36
+ screenEl.classList.add("active");
37
+ console.log("Screen switched to:", name);
38
  }
39
 
40
  function toggleSidebar() {
 
105
  async function submitQuery() {
106
  const query = textarea.value.trim();
107
  if (!query || isLoading) return;
108
+ if (query.length < 10) { alert("Query too short (minimum 10 characters)"); return; }
109
  if (query.length > 1000) { alert("Query too long"); return; }
110
 
111
  setLoading(true);
112
+ console.log("=== Submitting Query ===");
113
+ console.log("Query:", query);
114
 
115
  // Create session if needed and switch to chat view
116
  if (!activeSessionId) {
117
  createSession(query);
118
+ console.log("Created new session:", activeSessionId);
119
  }
120
 
121
  // Ensure chat screen is visible
122
  showScreen("chat");
123
+ console.log("Switched to chat screen");
124
 
125
  const session = getActiveSession();
126
 
127
  // Add user message to session and display
128
  session.messages.push({ role: "user", text: query });
129
  appendUserBubble(query);
130
+ console.log("Added user message to session");
131
 
132
  // Clear input
133
  textarea.value = "";
 
135
 
136
  // Show loading state
137
  const loaderId = appendLoader();
138
+ console.log("Showing loader:", loaderId);
139
 
140
  try {
141
+ const sessionId = activeSessionId ? String(activeSessionId) : "default";
142
+ console.log("Posting to /query with session_id:", sessionId);
143
+
144
  const res = await fetch(`${API_BASE}/query`, {
145
  method: "POST",
146
  headers: { "Content-Type": "application/json" },
147
  body: JSON.stringify({
148
  query,
149
+ session_id: sessionId
150
  })
151
  });
152
 
153
+ console.log("Response status:", res.status);
154
+ console.log("Response ok:", res.ok);
155
+
156
  const data = await res.json();
157
+ console.log("Response data:", data);
158
+
159
  removeLoader(loaderId);
160
+ console.log("Removed loader");
161
 
162
  if (!res.ok) {
163
+ const msg = data.detail || data.error || JSON.stringify(data) || "Error: please try again";
164
+ console.error("API Error:", msg);
165
  session.messages.push({ role: "error", text: msg });
166
  appendErrorBubble(msg);
167
  } else {
168
+ console.log("API Success - Adding AI message");
169
  session.messages.push({ role: "ai", data });
170
  appendAIBubble(data);
171
+ console.log("AI message added");
172
  }
173
  } catch (err) {
174
  removeLoader(loaderId);
175
+ console.error("Fetch error:", err);
176
+ const msg = `Connection error: ${err.message}`;
177
  session.messages.push({ role: "error", text: msg });
178
  appendErrorBubble(msg);
179
  }
180
 
181
  setLoading(false);
182
  scrollBottom();
183
+ console.log("=== Query Complete ===");
184
  }
185
 
186
  function usesuggestion(queryText) {
 
191
  }
192
 
193
  function appendUserBubble(text, scroll = true) {
194
+ console.log("appendUserBubble called with text:", text);
195
  const div = document.createElement("div");
196
  div.className = "flex items-start gap-4 ml-12 flex-row-reverse";
197
  div.innerHTML = `
 
204
  </div>
205
  `;
206
  messagesList.appendChild(div);
207
+ console.log("User message appended to DOM");
208
  if (scroll) scrollBottom();
209
  }
210
 
211
  function appendAIBubble(data, scroll = true) {
212
+ console.log("appendAIBubble called with data:", data);
213
+
214
+ // Handle different response structures
215
+ const answer = data.answer || data.response || data.text || JSON.stringify(data);
216
  const verified = data.verification_status === true || data.verification_status === "verified";
217
+ const sourceCount = (data.sources || data.source_judgments || []).length;
218
+
219
  const sourcesBtn = sourceCount > 0 ? `
220
  <button class="text-[11px] font-bold text-secondary uppercase tracking-widest flex items-center gap-1 hover:underline">
221
  <span class="material-symbols-outlined text-sm">description</span> ${sourceCount} Citation${sourceCount > 1 ? "s" : ""}
 
230
  <div class="clay-card p-6 bg-white text-primary leading-relaxed shadow-sm max-w-2xl">
231
  <p class="text-sm font-medium mb-4">Registry Assistant</p>
232
  <div class="font-serif text-lg leading-relaxed text-primary/90">
233
+ ${formatAnswer(answer)}
234
  </div>
235
  <div class="mt-6 pt-4 border-t border-primary/5 flex gap-4">
236
  ${sourcesBtn}
 
241
  </div>
242
  `;
243
  messagesList.appendChild(div);
244
+ console.log("AI message appended to DOM");
245
  if (scroll) scrollBottom();
246
  }
247
 
248
  function appendErrorBubble(text, scroll = true) {
249
+ console.error("appendErrorBubble called with text:", text);
250
  const div = document.createElement("div");
251
  div.className = "flex items-start gap-4 mr-12";
252
  div.innerHTML = `
 
259
  </div>
260
  `;
261
  messagesList.appendChild(div);
262
+ console.log("Error message appended to DOM");
263
  if (scroll) scrollBottom();
264
  }
265
 
 
281
  </div>
282
  </div>
283
  `;
284
+ if (!messagesList) {
285
+ console.error("messagesList element not found!");
286
+ return id;
287
+ }
288
  messagesList.appendChild(div);
289
+ console.log("Loader appended with id:", id, "messagesList children:", messagesList.children.length);
290
  scrollBottom();
291
  return id;
292
  }
 
318
  }
319
 
320
  function formatAnswer(text) {
321
+ if (!text) {
322
+ console.warn("formatAnswer received empty text");
323
+ return "<em>No response received</em>";
324
+ }
325
+ if (typeof text !== "string") {
326
+ console.log("formatAnswer received non-string:", typeof text);
327
+ return "<em>Invalid response format</em>";
328
+ }
329
  return text.replace(/\n/g, "<br>");
330
  }
331