Spaces:
Runtime error
Runtime error
Update frontend/script.js
Browse files- frontend/script.js +508 -523
frontend/script.js
CHANGED
|
@@ -1,524 +1,509 @@
|
|
| 1 |
-
// API Configuration -
|
| 2 |
-
const API_BASE_URL = window.location.origin;
|
| 3 |
-
|
| 4 |
-
// Session Management
|
| 5 |
-
let currentSessionId = null;
|
| 6 |
-
let currentAnswer = null;
|
| 7 |
-
let currentSources = null;
|
| 8 |
-
let currentQuery = null;
|
| 9 |
-
let queryHistory = [];
|
| 10 |
-
|
| 11 |
-
// DOM Elements
|
| 12 |
-
const queryInput = document.getElementById('queryInput');
|
| 13 |
-
const submitBtn = document.getElementById('submitBtn');
|
| 14 |
-
const btnText = submitBtn.querySelector('.btn-text');
|
| 15 |
-
const loader = submitBtn.querySelector('.loader');
|
| 16 |
-
const responseSection = document.getElementById('responseSection');
|
| 17 |
-
const initialState = document.getElementById('initialState');
|
| 18 |
-
const loadingState = document.getElementById('loadingState');
|
| 19 |
-
const answerDiv = document.getElementById('answer');
|
| 20 |
-
const sourcesDiv = document.getElementById('sources');
|
| 21 |
-
const errorToast = document.getElementById('errorToast');
|
| 22 |
-
const errorMessage = document.getElementById('errorMessage');
|
| 23 |
-
const expandedQueriesDiv = document.getElementById('expandedQueries');
|
| 24 |
-
const queriesList = document.getElementById('queriesList');
|
| 25 |
-
const exportBtn = document.getElementById('exportBtn');
|
| 26 |
-
const githubBtn = document.getElementById('githubBtn');
|
| 27 |
-
const historySelect = document.getElementById('historySelect');
|
| 28 |
-
|
| 29 |
-
// Initialize
|
| 30 |
-
function init() {
|
| 31 |
-
currentSessionId = sessionStorage.getItem('sessionId') || generateSessionId();
|
| 32 |
-
sessionStorage.setItem('sessionId', currentSessionId);
|
| 33 |
-
|
| 34 |
-
// Load history
|
| 35 |
-
const saved = sessionStorage.getItem('queryHistory');
|
| 36 |
-
if (saved) {
|
| 37 |
-
try {
|
| 38 |
-
queryHistory = JSON.parse(saved);
|
| 39 |
-
updateHistoryDropdown();
|
| 40 |
-
} catch (e) {
|
| 41 |
-
queryHistory = [];
|
| 42 |
-
}
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
setupEventListeners();
|
| 46 |
-
checkHealth();
|
| 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 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
}
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
</
|
| 235 |
-
|
| 236 |
-
}
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
}
|
| 240 |
-
|
| 241 |
-
// Display
|
| 242 |
-
if (
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
<div
|
| 317 |
-
|
| 318 |
-
${escapeHtml(source.
|
| 319 |
-
</div>
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
doc.
|
| 365 |
-
doc.
|
| 366 |
-
doc.text('
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
doc.setFontSize(10);
|
| 370 |
-
doc.
|
| 371 |
-
doc.
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
doc.setFontSize(12);
|
| 376 |
-
doc.setFont(undefined, 'bold');
|
| 377 |
-
doc.
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
doc.setFont(undefined, 'normal');
|
| 381 |
-
doc.setFontSize(10);
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
doc.
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
.
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
doc.
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
yPos
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
}
|
| 458 |
-
}
|
| 459 |
-
|
| 460 |
-
//
|
| 461 |
-
function
|
| 462 |
-
if (!
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
}
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
console.warn('API health check failed');
|
| 510 |
-
} else {
|
| 511 |
-
console.log('API health check passed');
|
| 512 |
-
}
|
| 513 |
-
} catch (error) {
|
| 514 |
-
console.error('Cannot connect to API:', error);
|
| 515 |
-
// Don't show error on page load for Hugging Face
|
| 516 |
-
// The space might still be starting up
|
| 517 |
-
}
|
| 518 |
-
}
|
| 519 |
-
|
| 520 |
-
// Make toggleSource available globally
|
| 521 |
-
window.toggleSource = toggleSource;
|
| 522 |
-
|
| 523 |
-
// Initialize on load
|
| 524 |
init();
|
|
|
|
| 1 |
+
// API Configuration - Use relative URL for HuggingFace
|
| 2 |
+
const API_BASE_URL = window.location.origin;
|
| 3 |
+
|
| 4 |
+
// Session Management
|
| 5 |
+
let currentSessionId = null;
|
| 6 |
+
let currentAnswer = null;
|
| 7 |
+
let currentSources = null;
|
| 8 |
+
let currentQuery = null;
|
| 9 |
+
let queryHistory = [];
|
| 10 |
+
|
| 11 |
+
// DOM Elements
|
| 12 |
+
const queryInput = document.getElementById('queryInput');
|
| 13 |
+
const submitBtn = document.getElementById('submitBtn');
|
| 14 |
+
const btnText = submitBtn.querySelector('.btn-text');
|
| 15 |
+
const loader = submitBtn.querySelector('.loader');
|
| 16 |
+
const responseSection = document.getElementById('responseSection');
|
| 17 |
+
const initialState = document.getElementById('initialState');
|
| 18 |
+
const loadingState = document.getElementById('loadingState');
|
| 19 |
+
const answerDiv = document.getElementById('answer');
|
| 20 |
+
const sourcesDiv = document.getElementById('sources');
|
| 21 |
+
const errorToast = document.getElementById('errorToast');
|
| 22 |
+
const errorMessage = document.getElementById('errorMessage');
|
| 23 |
+
const expandedQueriesDiv = document.getElementById('expandedQueries');
|
| 24 |
+
const queriesList = document.getElementById('queriesList');
|
| 25 |
+
const exportBtn = document.getElementById('exportBtn');
|
| 26 |
+
const githubBtn = document.getElementById('githubBtn');
|
| 27 |
+
const historySelect = document.getElementById('historySelect');
|
| 28 |
+
|
| 29 |
+
// Initialize
|
| 30 |
+
function init() {
|
| 31 |
+
currentSessionId = sessionStorage.getItem('sessionId') || generateSessionId();
|
| 32 |
+
sessionStorage.setItem('sessionId', currentSessionId);
|
| 33 |
+
|
| 34 |
+
// Load history
|
| 35 |
+
const saved = sessionStorage.getItem('queryHistory');
|
| 36 |
+
if (saved) {
|
| 37 |
+
try {
|
| 38 |
+
queryHistory = JSON.parse(saved);
|
| 39 |
+
updateHistoryDropdown();
|
| 40 |
+
} catch (e) {
|
| 41 |
+
queryHistory = [];
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
setupEventListeners();
|
| 46 |
+
checkHealth();
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
function generateSessionId() {
|
| 50 |
+
return `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
function setupEventListeners() {
|
| 54 |
+
// Submit button
|
| 55 |
+
if (submitBtn) {
|
| 56 |
+
submitBtn.addEventListener('click', handleSubmit);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// Enter key (Ctrl+Enter)
|
| 60 |
+
if (queryInput) {
|
| 61 |
+
queryInput.addEventListener('keydown', (e) => {
|
| 62 |
+
if (e.key === 'Enter' && e.ctrlKey) {
|
| 63 |
+
handleSubmit();
|
| 64 |
+
}
|
| 65 |
+
});
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
// Example queries
|
| 69 |
+
document.querySelectorAll('.example-item').forEach(item => {
|
| 70 |
+
item.addEventListener('click', () => {
|
| 71 |
+
const query = item.getAttribute('data-query');
|
| 72 |
+
if (query && queryInput) {
|
| 73 |
+
queryInput.value = query;
|
| 74 |
+
handleSubmit();
|
| 75 |
+
}
|
| 76 |
+
});
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
// Export button
|
| 80 |
+
if (exportBtn) {
|
| 81 |
+
exportBtn.addEventListener('click', exportToPDF);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// GitHub button
|
| 85 |
+
if (githubBtn) {
|
| 86 |
+
githubBtn.addEventListener('click', () => {
|
| 87 |
+
window.open('https://github.com/Adeyemi0/FinSight-RAG-Application-', '_blank');
|
| 88 |
+
});
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
// History dropdown
|
| 92 |
+
if (historySelect) {
|
| 93 |
+
historySelect.addEventListener('change', (e) => {
|
| 94 |
+
const query = e.target.value;
|
| 95 |
+
if (query && queryInput) {
|
| 96 |
+
queryInput.value = query;
|
| 97 |
+
}
|
| 98 |
+
});
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// Update history dropdown
|
| 103 |
+
function updateHistoryDropdown() {
|
| 104 |
+
if (!historySelect) return;
|
| 105 |
+
|
| 106 |
+
historySelect.innerHTML = '<option value="">Select a recent query</option>';
|
| 107 |
+
|
| 108 |
+
queryHistory.slice().reverse().forEach((item, index) => {
|
| 109 |
+
const option = document.createElement('option');
|
| 110 |
+
option.value = item.query;
|
| 111 |
+
option.textContent = item.query.substring(0, 60) + (item.query.length > 60 ? '...' : '');
|
| 112 |
+
historySelect.appendChild(option);
|
| 113 |
+
});
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// Save to history
|
| 117 |
+
function saveToHistory(query, answer) {
|
| 118 |
+
queryHistory.push({
|
| 119 |
+
query,
|
| 120 |
+
answer: answer.substring(0, 500),
|
| 121 |
+
timestamp: new Date().toISOString()
|
| 122 |
+
});
|
| 123 |
+
|
| 124 |
+
// Keep last 20
|
| 125 |
+
if (queryHistory.length > 20) {
|
| 126 |
+
queryHistory = queryHistory.slice(-20);
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
try {
|
| 130 |
+
sessionStorage.setItem('queryHistory', JSON.stringify(queryHistory));
|
| 131 |
+
updateHistoryDropdown();
|
| 132 |
+
} catch (e) {
|
| 133 |
+
console.error('Failed to save history:', e);
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
// Main submit handler
|
| 138 |
+
async function handleSubmit() {
|
| 139 |
+
if (!queryInput) return;
|
| 140 |
+
|
| 141 |
+
const query = queryInput.value.trim();
|
| 142 |
+
|
| 143 |
+
if (!query) {
|
| 144 |
+
showError('Please enter a question');
|
| 145 |
+
return;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
// Show loading
|
| 149 |
+
setLoading(true);
|
| 150 |
+
hideError();
|
| 151 |
+
|
| 152 |
+
// Hide initial state, show loading
|
| 153 |
+
if (initialState) initialState.style.display = 'none';
|
| 154 |
+
if (loadingState) loadingState.style.display = 'flex';
|
| 155 |
+
if (responseSection) responseSection.style.display = 'none';
|
| 156 |
+
|
| 157 |
+
try {
|
| 158 |
+
// Extract ticker from query if mentioned (e.g., "What is AAPL's revenue?")
|
| 159 |
+
const tickerMatch = query.match(/\b([A-Z]{1,5})\b/);
|
| 160 |
+
const detectedTicker = tickerMatch ? tickerMatch[1] : null;
|
| 161 |
+
|
| 162 |
+
const requestData = {
|
| 163 |
+
query,
|
| 164 |
+
ticker: detectedTicker, // Auto-detect ticker or null for all companies
|
| 165 |
+
doc_types: null, // No filter
|
| 166 |
+
top_k: 10,
|
| 167 |
+
session_id: currentSessionId
|
| 168 |
+
};
|
| 169 |
+
|
| 170 |
+
console.log('Request data:', requestData);
|
| 171 |
+
|
| 172 |
+
const response = await fetch(`${API_BASE_URL}/query`, {
|
| 173 |
+
method: 'POST',
|
| 174 |
+
headers: { 'Content-Type': 'application/json' },
|
| 175 |
+
body: JSON.stringify(requestData)
|
| 176 |
+
});
|
| 177 |
+
|
| 178 |
+
if (!response.ok) {
|
| 179 |
+
const errorData = await response.json().catch(() => ({}));
|
| 180 |
+
throw new Error(errorData.detail || `HTTP ${response.status}`);
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
const data = await response.json();
|
| 184 |
+
|
| 185 |
+
if (!data) {
|
| 186 |
+
throw new Error('Empty response from server');
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
// Store current data for export
|
| 190 |
+
currentQuery = query;
|
| 191 |
+
currentAnswer = data.answer;
|
| 192 |
+
currentSources = data.sources;
|
| 193 |
+
|
| 194 |
+
// Save to history
|
| 195 |
+
saveToHistory(query, data.answer || '');
|
| 196 |
+
|
| 197 |
+
// Display results
|
| 198 |
+
displayResults(data);
|
| 199 |
+
|
| 200 |
+
// Clear input
|
| 201 |
+
queryInput.value = '';
|
| 202 |
+
|
| 203 |
+
} catch (error) {
|
| 204 |
+
console.error('Error:', error);
|
| 205 |
+
showError(error.message || 'Failed to process query');
|
| 206 |
+
|
| 207 |
+
// Show initial state again
|
| 208 |
+
if (loadingState) loadingState.style.display = 'none';
|
| 209 |
+
if (initialState) initialState.style.display = 'flex';
|
| 210 |
+
|
| 211 |
+
} finally {
|
| 212 |
+
setLoading(false);
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// Display results
|
| 217 |
+
function displayResults(data) {
|
| 218 |
+
if (!data) return;
|
| 219 |
+
|
| 220 |
+
// Hide loading, show response
|
| 221 |
+
if (loadingState) loadingState.style.display = 'none';
|
| 222 |
+
if (responseSection) responseSection.style.display = 'block';
|
| 223 |
+
|
| 224 |
+
// Display answer
|
| 225 |
+
if (answerDiv) {
|
| 226 |
+
answerDiv.innerHTML = formatAnswer(data.answer || 'No answer available');
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
// Display expanded queries
|
| 230 |
+
if (expandedQueriesDiv && queriesList) {
|
| 231 |
+
if (data.expanded_queries && data.expanded_queries.length > 1) {
|
| 232 |
+
expandedQueriesDiv.style.display = 'block';
|
| 233 |
+
queriesList.innerHTML = data.expanded_queries
|
| 234 |
+
.map(q => `<li>${escapeHtml(q)}</li>`)
|
| 235 |
+
.join('');
|
| 236 |
+
} else {
|
| 237 |
+
expandedQueriesDiv.style.display = 'none';
|
| 238 |
+
}
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
// Display sources
|
| 242 |
+
if (sourcesDiv) {
|
| 243 |
+
displaySources(data.sources || []);
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
// Scroll to top of right panel
|
| 247 |
+
const rightPanel = document.querySelector('.right-panel');
|
| 248 |
+
if (rightPanel) {
|
| 249 |
+
rightPanel.scrollTop = 0;
|
| 250 |
+
}
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
// Format answer with enhanced styling
|
| 254 |
+
function formatAnswer(answer) {
|
| 255 |
+
if (!answer) return '';
|
| 256 |
+
|
| 257 |
+
let formatted = escapeHtml(answer);
|
| 258 |
+
|
| 259 |
+
// Line breaks
|
| 260 |
+
formatted = formatted.replace(/\n/g, '<br>');
|
| 261 |
+
|
| 262 |
+
// Citations
|
| 263 |
+
formatted = formatted.replace(/\[Source (\d+)\]/g,
|
| 264 |
+
'<span class="citation">[Source $1]</span>');
|
| 265 |
+
|
| 266 |
+
// Highlight numbers (currency, percentages, ratios)
|
| 267 |
+
formatted = formatted.replace(/\$[\d,]+\.?\d*[BM]?/g,
|
| 268 |
+
match => `<span class="highlight-number">${match}</span>`);
|
| 269 |
+
formatted = formatted.replace(/\d+\.?\d*%/g,
|
| 270 |
+
match => `<span class="highlight-number">${match}</span>`);
|
| 271 |
+
|
| 272 |
+
// Color-code metrics
|
| 273 |
+
formatted = formatted.replace(/(\d+\.?\d+)(x|:1)/g,
|
| 274 |
+
'<span class="metric-green">$1$2</span>');
|
| 275 |
+
|
| 276 |
+
// Bold headers (lines ending with:)
|
| 277 |
+
formatted = formatted.replace(/^(.+:)$/gm, '<strong>$1</strong>');
|
| 278 |
+
|
| 279 |
+
// Create calculation boxes for formulas
|
| 280 |
+
formatted = formatted.replace(/Formula: ([^\n]+)/g,
|
| 281 |
+
'<div class="calculation-box"><h4>Formula</h4><div class="calculation-step">$1</div></div>');
|
| 282 |
+
|
| 283 |
+
return formatted;
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
// Display sources with collapsible cards
|
| 287 |
+
function displaySources(sources) {
|
| 288 |
+
if (!sourcesDiv) return;
|
| 289 |
+
|
| 290 |
+
if (!sources || sources.length === 0) {
|
| 291 |
+
sourcesDiv.innerHTML = '<p style="color: var(--text-muted);">No sources available</p>';
|
| 292 |
+
return;
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
sourcesDiv.innerHTML = sources.map((source, index) => {
|
| 296 |
+
if (!source) return '';
|
| 297 |
+
|
| 298 |
+
const docTypeLabel = source.doc_type === '10k' ? '10-K Report' :
|
| 299 |
+
source.doc_type.replace('_', ' ').replace(/\b\w/g, l => l.toUpperCase());
|
| 300 |
+
|
| 301 |
+
return `
|
| 302 |
+
<div class="source-card" id="source-${index}">
|
| 303 |
+
<div class="source-header" onclick="toggleSource(${index})">
|
| 304 |
+
<div class="source-title">
|
| 305 |
+
<span class="source-badge">${docTypeLabel}</span>
|
| 306 |
+
${escapeHtml(source.filename || 'Unknown')}
|
| 307 |
+
</div>
|
| 308 |
+
<div class="source-similarity">
|
| 309 |
+
Similarity Score: <strong>${source.similarity_score ? (source.similarity_score * 100).toFixed(0) + '%' : 'N/A'}</strong>
|
| 310 |
+
</div>
|
| 311 |
+
</div>
|
| 312 |
+
<div class="source-content">
|
| 313 |
+
<div class="source-details">
|
| 314 |
+
${source.ticker ? `<span class="source-detail"><strong>Ticker:</strong> ${escapeHtml(source.ticker)}</span>` : ''}
|
| 315 |
+
${source.chunk_id ? `<span class="source-detail"><strong>Chunk:</strong> ${escapeHtml(source.chunk_id)}</span>` : ''}
|
| 316 |
+
</div>
|
| 317 |
+
<div class="source-preview">
|
| 318 |
+
"${escapeHtml(source.text_preview || 'No preview available')}"
|
| 319 |
+
</div>
|
| 320 |
+
</div>
|
| 321 |
+
</div>
|
| 322 |
+
`;
|
| 323 |
+
}).filter(Boolean).join('');
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
// Toggle source expansion
|
| 327 |
+
function toggleSource(index) {
|
| 328 |
+
const card = document.getElementById(`source-${index}`);
|
| 329 |
+
if (card) {
|
| 330 |
+
card.classList.toggle('expanded');
|
| 331 |
+
}
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
// Export to PDF function
|
| 335 |
+
function exportToPDF() {
|
| 336 |
+
if (!currentAnswer) {
|
| 337 |
+
showError('No answer to export. Please ask a question first.');
|
| 338 |
+
return;
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
try {
|
| 342 |
+
// Check if jsPDF is loaded
|
| 343 |
+
if (typeof window.jspdf === 'undefined') {
|
| 344 |
+
showError('PDF library not loaded. Please refresh the page.');
|
| 345 |
+
return;
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
const { jsPDF } = window.jspdf;
|
| 349 |
+
const doc = new jsPDF();
|
| 350 |
+
|
| 351 |
+
// Set title
|
| 352 |
+
doc.setFontSize(18);
|
| 353 |
+
doc.setFont(undefined, 'bold');
|
| 354 |
+
doc.text('FinSage Analytics Report', 20, 20);
|
| 355 |
+
|
| 356 |
+
// Set subtitle
|
| 357 |
+
doc.setFontSize(10);
|
| 358 |
+
doc.setFont(undefined, 'normal');
|
| 359 |
+
doc.setTextColor(100);
|
| 360 |
+
doc.text(`Generated on ${new Date().toLocaleString()}`, 20, 28);
|
| 361 |
+
|
| 362 |
+
// Query section
|
| 363 |
+
doc.setFontSize(12);
|
| 364 |
+
doc.setFont(undefined, 'bold');
|
| 365 |
+
doc.setTextColor(0);
|
| 366 |
+
doc.text('Query:', 20, 40);
|
| 367 |
+
|
| 368 |
+
doc.setFont(undefined, 'normal');
|
| 369 |
+
doc.setFontSize(10);
|
| 370 |
+
const queryLines = doc.splitTextToSize(currentQuery || '', 170);
|
| 371 |
+
doc.text(queryLines, 20, 48);
|
| 372 |
+
|
| 373 |
+
// Answer section
|
| 374 |
+
let yPos = 48 + (queryLines.length * 7) + 10;
|
| 375 |
+
doc.setFontSize(12);
|
| 376 |
+
doc.setFont(undefined, 'bold');
|
| 377 |
+
doc.text('Answer:', 20, yPos);
|
| 378 |
+
|
| 379 |
+
yPos += 8;
|
| 380 |
+
doc.setFont(undefined, 'normal');
|
| 381 |
+
doc.setFontSize(10);
|
| 382 |
+
|
| 383 |
+
// Clean answer text (remove HTML tags and format)
|
| 384 |
+
let cleanAnswer = currentAnswer
|
| 385 |
+
.replace(/<[^>]*>/g, '') // Remove HTML tags
|
| 386 |
+
.replace(/\[Source \d+\]/g, '') // Remove citation markers
|
| 387 |
+
.replace(/ /g, ' ')
|
| 388 |
+
.replace(/</g, '<')
|
| 389 |
+
.replace(/>/g, '>')
|
| 390 |
+
.replace(/&/g, '&');
|
| 391 |
+
|
| 392 |
+
const answerLines = doc.splitTextToSize(cleanAnswer, 170);
|
| 393 |
+
|
| 394 |
+
// Add answer with page breaks if needed
|
| 395 |
+
answerLines.forEach((line, index) => {
|
| 396 |
+
if (yPos > 270) {
|
| 397 |
+
doc.addPage();
|
| 398 |
+
yPos = 20;
|
| 399 |
+
}
|
| 400 |
+
doc.text(line, 20, yPos);
|
| 401 |
+
yPos += 7;
|
| 402 |
+
});
|
| 403 |
+
|
| 404 |
+
// Sources section
|
| 405 |
+
if (currentSources && currentSources.length > 0) {
|
| 406 |
+
yPos += 10;
|
| 407 |
+
if (yPos > 250) {
|
| 408 |
+
doc.addPage();
|
| 409 |
+
yPos = 20;
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
doc.setFontSize(12);
|
| 413 |
+
doc.setFont(undefined, 'bold');
|
| 414 |
+
doc.text('Sources:', 20, yPos);
|
| 415 |
+
yPos += 8;
|
| 416 |
+
|
| 417 |
+
doc.setFontSize(9);
|
| 418 |
+
doc.setFont(undefined, 'normal');
|
| 419 |
+
|
| 420 |
+
currentSources.forEach((source, index) => {
|
| 421 |
+
if (yPos > 270) {
|
| 422 |
+
doc.addPage();
|
| 423 |
+
yPos = 20;
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
doc.setFont(undefined, 'bold');
|
| 427 |
+
doc.text(`[${index + 1}] ${source.filename || 'Unknown'}`, 20, yPos);
|
| 428 |
+
yPos += 5;
|
| 429 |
+
|
| 430 |
+
doc.setFont(undefined, 'normal');
|
| 431 |
+
doc.setTextColor(100);
|
| 432 |
+
doc.text(`Type: ${source.doc_type || 'N/A'} | Similarity: ${source.similarity_score ? (source.similarity_score * 100).toFixed(0) + '%' : 'N/A'}`, 20, yPos);
|
| 433 |
+
yPos += 8;
|
| 434 |
+
doc.setTextColor(0);
|
| 435 |
+
});
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
// Save PDF
|
| 439 |
+
const filename = `FinSage_Analysis_${Date.now()}.pdf`;
|
| 440 |
+
doc.save(filename);
|
| 441 |
+
|
| 442 |
+
} catch (error) {
|
| 443 |
+
console.error('PDF Export Error:', error);
|
| 444 |
+
showError('Failed to export PDF. Please try again.');
|
| 445 |
+
}
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
// Loading state
|
| 449 |
+
function setLoading(isLoading) {
|
| 450 |
+
if (!submitBtn) return;
|
| 451 |
+
|
| 452 |
+
submitBtn.disabled = isLoading;
|
| 453 |
+
|
| 454 |
+
if (btnText && loader) {
|
| 455 |
+
btnText.style.display = isLoading ? 'none' : 'inline';
|
| 456 |
+
loader.style.display = isLoading ? 'inline-block' : 'none';
|
| 457 |
+
}
|
| 458 |
+
}
|
| 459 |
+
|
| 460 |
+
// Error handling
|
| 461 |
+
function showError(message) {
|
| 462 |
+
if (!errorToast || !errorMessage) {
|
| 463 |
+
alert(message);
|
| 464 |
+
return;
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
errorMessage.textContent = message;
|
| 468 |
+
errorToast.style.display = 'block';
|
| 469 |
+
|
| 470 |
+
setTimeout(() => {
|
| 471 |
+
errorToast.style.display = 'none';
|
| 472 |
+
}, 5000);
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
function hideError() {
|
| 476 |
+
if (errorToast) {
|
| 477 |
+
errorToast.style.display = 'none';
|
| 478 |
+
}
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
// Utility: Escape HTML
|
| 482 |
+
function escapeHtml(text) {
|
| 483 |
+
if (!text) return '';
|
| 484 |
+
const div = document.createElement('div');
|
| 485 |
+
div.textContent = text;
|
| 486 |
+
return div.innerHTML;
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
// Health check
|
| 490 |
+
async function checkHealth() {
|
| 491 |
+
try {
|
| 492 |
+
const response = await fetch(`${API_BASE_URL}/health`, {
|
| 493 |
+
signal: AbortSignal.timeout(5000)
|
| 494 |
+
});
|
| 495 |
+
|
| 496 |
+
if (!response.ok) {
|
| 497 |
+
console.warn('API health check failed');
|
| 498 |
+
}
|
| 499 |
+
} catch (error) {
|
| 500 |
+
console.error('Cannot connect to API:', error);
|
| 501 |
+
showError('Backend API is not responding. Please start the server.');
|
| 502 |
+
}
|
| 503 |
+
}
|
| 504 |
+
|
| 505 |
+
// Make toggleSource available globally
|
| 506 |
+
window.toggleSource = toggleSource;
|
| 507 |
+
|
| 508 |
+
// Initialize on load
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
init();
|