Update templates/index.html
Browse files- templates/index.html +23 -29
templates/index.html
CHANGED
|
@@ -289,6 +289,8 @@
|
|
| 289 |
messageDiv.innerHTML = marked.parse(content); // Render Markdown for text
|
| 290 |
} else if (typeof content === 'object' && content.type === 'image') {
|
| 291 |
// Display image in message bubble
|
|
|
|
|
|
|
| 292 |
const img = document.createElement('img');
|
| 293 |
img.src = content.src;
|
| 294 |
img.alt = "Uploaded image";
|
|
@@ -301,6 +303,7 @@
|
|
| 301 |
}
|
| 302 |
} else if (typeof content === 'object' && content.type === 'document') {
|
| 303 |
// Display document info in message bubble
|
|
|
|
| 304 |
const docInfoDiv = document.createElement('div');
|
| 305 |
docInfoDiv.innerHTML = `<strong>Document Uploaded:</strong> ${content.name}<br>`;
|
| 306 |
if (content.textPreview) {
|
|
@@ -341,7 +344,6 @@
|
|
| 341 |
console.log("DEBUG: Image input change event triggered.");
|
| 342 |
console.log("DEBUG: event.target.files:", event.target.files);
|
| 343 |
|
| 344 |
-
// IMPORTANT: Do NOT call clearFileSelection() here. It clears the file before it's read.
|
| 345 |
const file = event.target.files[0];
|
| 346 |
|
| 347 |
if (!file) {
|
|
@@ -366,19 +368,17 @@
|
|
| 366 |
|
| 367 |
uploadedFile = file;
|
| 368 |
uploadedFileType = 'image';
|
| 369 |
-
|
|
|
|
| 370 |
clearFileButton.classList.remove('hidden');
|
| 371 |
|
| 372 |
const reader = new FileReader();
|
| 373 |
reader.onload = (e) => {
|
| 374 |
uploadedFileBase64 = e.target.result.split(',')[1]; // Get base64 part
|
| 375 |
console.log("DEBUG: Image FileReader loaded. Base64 length:", uploadedFileBase64.length);
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
filePreview.innerHTML = '';
|
| 380 |
-
filePreview.appendChild(img);
|
| 381 |
-
filePreview.classList.remove('hidden');
|
| 382 |
};
|
| 383 |
reader.onerror = (e) => {
|
| 384 |
console.error("DEBUG: FileReader error for image:", e);
|
|
@@ -393,7 +393,6 @@
|
|
| 393 |
console.log("DEBUG: Document input change event triggered.");
|
| 394 |
console.log("DEBUG: event.target.files:", event.target.files);
|
| 395 |
|
| 396 |
-
// IMPORTANT: Do NOT call clearFileSelection() here. It clears the file before it's read.
|
| 397 |
const file = event.target.files[0];
|
| 398 |
|
| 399 |
if (!file) {
|
|
@@ -405,7 +404,8 @@
|
|
| 405 |
console.log("DEBUG: Selected document file:", file);
|
| 406 |
uploadedFile = file;
|
| 407 |
uploadedFileType = 'document';
|
| 408 |
-
|
|
|
|
| 409 |
clearFileButton.classList.remove('hidden');
|
| 410 |
|
| 411 |
// Basic text extraction for .txt files on frontend
|
|
@@ -414,12 +414,9 @@
|
|
| 414 |
reader.onload = (e) => {
|
| 415 |
uploadedFileText = e.target.result;
|
| 416 |
console.log("DEBUG: Document FileReader loaded. Text length:", uploadedFileText.length);
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
filePreview.innerHTML = '';
|
| 421 |
-
filePreview.appendChild(textPreview);
|
| 422 |
-
filePreview.classList.remove('hidden');
|
| 423 |
};
|
| 424 |
reader.onerror = (e) => {
|
| 425 |
console.error("DEBUG: FileReader error for document:", e);
|
|
@@ -429,15 +426,12 @@
|
|
| 429 |
reader.readAsText(file);
|
| 430 |
} else {
|
| 431 |
// For other document types, inform the user about limitations
|
| 432 |
-
|
| 433 |
-
unsupportedMessage.classList.add('p-3', 'bg-red-800', 'text-white', 'rounded-lg');
|
| 434 |
-
unsupportedMessage.innerHTML = `<strong>Unsupported Document Type:</strong> Only plain text (.txt) files are directly processed on the frontend. For PDF, DOCX, etc., advanced backend parsing is required. The file name will be sent, but content won't be extracted here.`;
|
| 435 |
-
filePreview.innerHTML = '';
|
| 436 |
-
filePreview.appendChild(unsupportedMessage);
|
| 437 |
-
filePreview.classList.remove('hidden');
|
| 438 |
-
uploadedFileText = null; // Ensure no text is sent for unsupported types
|
| 439 |
showMessage("Unsupported Document", "Only plain text (.txt) files are directly processed on the frontend. For PDF, DOCX, etc., advanced backend parsing is required.");
|
| 440 |
console.warn("DEBUG: Unsupported document type selected:", file.type);
|
|
|
|
|
|
|
|
|
|
| 441 |
}
|
| 442 |
});
|
| 443 |
|
|
@@ -466,17 +460,19 @@
|
|
| 466 |
mimeType: uploadedFile.type,
|
| 467 |
data: uploadedFileBase64
|
| 468 |
};
|
| 469 |
-
|
|
|
|
| 470 |
console.log("DEBUG: Prepared image data for requestBody. MimeType:", requestBody.image_data.mimeType, "Data length:", requestBody.image_data.data.length);
|
| 471 |
} else if (uploadedFileType === 'document' && uploadedFileText) {
|
| 472 |
requestBody.document_text = uploadedFileText;
|
| 473 |
-
|
|
|
|
| 474 |
console.log("DEBUG: Prepared document text for requestBody. Text length:", requestBody.document_text.length);
|
| 475 |
} else if (uploadedFileType === 'document' && uploadedFile && !uploadedFileText) {
|
| 476 |
// For unsupported document types, send filename and a note
|
| 477 |
-
// This will be handled as a text query on the backend
|
| 478 |
requestBody.query = `${userQuery}\n\nUser uploaded a document named "${uploadedFile.name}". Please note: Content extraction for this file type is not supported in this demo.`;
|
| 479 |
-
|
|
|
|
| 480 |
console.log("DEBUG: Prepared unsupported document type as text query:", requestBody.query);
|
| 481 |
}
|
| 482 |
|
|
@@ -488,8 +484,6 @@
|
|
| 488 |
messageInput.style.height = 'auto'; // Reset textarea height
|
| 489 |
|
| 490 |
// Clear file selection after adding to chat
|
| 491 |
-
// IMPORTANT: This is the correct place to clear the file selection
|
| 492 |
-
// after the data has been used to construct the request.
|
| 493 |
clearFileSelection();
|
| 494 |
|
| 495 |
// Show loading indicator
|
|
|
|
| 289 |
messageDiv.innerHTML = marked.parse(content); // Render Markdown for text
|
| 290 |
} else if (typeof content === 'object' && content.type === 'image') {
|
| 291 |
// Display image in message bubble
|
| 292 |
+
// This block is now effectively unused for user messages as displayContent will be a string
|
| 293 |
+
// But keeping it here for potential future use or if AI sends image back
|
| 294 |
const img = document.createElement('img');
|
| 295 |
img.src = content.src;
|
| 296 |
img.alt = "Uploaded image";
|
|
|
|
| 303 |
}
|
| 304 |
} else if (typeof content === 'object' && content.type === 'document') {
|
| 305 |
// Display document info in message bubble
|
| 306 |
+
// This block is now effectively unused for user messages as displayContent will be a string
|
| 307 |
const docInfoDiv = document.createElement('div');
|
| 308 |
docInfoDiv.innerHTML = `<strong>Document Uploaded:</strong> ${content.name}<br>`;
|
| 309 |
if (content.textPreview) {
|
|
|
|
| 344 |
console.log("DEBUG: Image input change event triggered.");
|
| 345 |
console.log("DEBUG: event.target.files:", event.target.files);
|
| 346 |
|
|
|
|
| 347 |
const file = event.target.files[0];
|
| 348 |
|
| 349 |
if (!file) {
|
|
|
|
| 368 |
|
| 369 |
uploadedFile = file;
|
| 370 |
uploadedFileType = 'image';
|
| 371 |
+
// Update this line to show success message and file name
|
| 372 |
+
fileNameDisplay.textContent = `Image: ${file.name} (Uploaded successfully!)`;
|
| 373 |
clearFileButton.classList.remove('hidden');
|
| 374 |
|
| 375 |
const reader = new FileReader();
|
| 376 |
reader.onload = (e) => {
|
| 377 |
uploadedFileBase64 = e.target.result.split(',')[1]; // Get base64 part
|
| 378 |
console.log("DEBUG: Image FileReader loaded. Base64 length:", uploadedFileBase64.length);
|
| 379 |
+
// Removed image preview code here
|
| 380 |
+
filePreview.innerHTML = ''; // Ensure no old preview remains
|
| 381 |
+
filePreview.classList.add('hidden'); // Ensure preview container remains hidden
|
|
|
|
|
|
|
|
|
|
| 382 |
};
|
| 383 |
reader.onerror = (e) => {
|
| 384 |
console.error("DEBUG: FileReader error for image:", e);
|
|
|
|
| 393 |
console.log("DEBUG: Document input change event triggered.");
|
| 394 |
console.log("DEBUG: event.target.files:", event.target.files);
|
| 395 |
|
|
|
|
| 396 |
const file = event.target.files[0];
|
| 397 |
|
| 398 |
if (!file) {
|
|
|
|
| 404 |
console.log("DEBUG: Selected document file:", file);
|
| 405 |
uploadedFile = file;
|
| 406 |
uploadedFileType = 'document';
|
| 407 |
+
// Update this line to show success message and file name
|
| 408 |
+
fileNameDisplay.textContent = `Document: ${file.name} (Uploaded successfully!)`;
|
| 409 |
clearFileButton.classList.remove('hidden');
|
| 410 |
|
| 411 |
// Basic text extraction for .txt files on frontend
|
|
|
|
| 414 |
reader.onload = (e) => {
|
| 415 |
uploadedFileText = e.target.result;
|
| 416 |
console.log("DEBUG: Document FileReader loaded. Text length:", uploadedFileText.length);
|
| 417 |
+
// Removed document preview code here
|
| 418 |
+
filePreview.innerHTML = ''; // Ensure no old preview remains
|
| 419 |
+
filePreview.classList.add('hidden'); // Ensure preview container remains hidden
|
|
|
|
|
|
|
|
|
|
| 420 |
};
|
| 421 |
reader.onerror = (e) => {
|
| 422 |
console.error("DEBUG: FileReader error for document:", e);
|
|
|
|
| 426 |
reader.readAsText(file);
|
| 427 |
} else {
|
| 428 |
// For other document types, inform the user about limitations
|
| 429 |
+
// The filename will still be displayed via fileNameDisplay.textContent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
showMessage("Unsupported Document", "Only plain text (.txt) files are directly processed on the frontend. For PDF, DOCX, etc., advanced backend parsing is required.");
|
| 431 |
console.warn("DEBUG: Unsupported document type selected:", file.type);
|
| 432 |
+
uploadedFileText = null; // Ensure no text is sent for unsupported types
|
| 433 |
+
filePreview.innerHTML = ''; // Ensure no old preview remains
|
| 434 |
+
filePreview.classList.add('hidden'); // Ensure preview container remains hidden
|
| 435 |
}
|
| 436 |
});
|
| 437 |
|
|
|
|
| 460 |
mimeType: uploadedFile.type,
|
| 461 |
data: uploadedFileBase64
|
| 462 |
};
|
| 463 |
+
// Change displayContent to a text message for the chat bubble
|
| 464 |
+
displayContent = `Image uploaded: ${uploadedFile.name}. ${userQuery ? `\n\n${userQuery}` : ''}`;
|
| 465 |
console.log("DEBUG: Prepared image data for requestBody. MimeType:", requestBody.image_data.mimeType, "Data length:", requestBody.image_data.data.length);
|
| 466 |
} else if (uploadedFileType === 'document' && uploadedFileText) {
|
| 467 |
requestBody.document_text = uploadedFileText;
|
| 468 |
+
// Change displayContent to a text message for the chat bubble
|
| 469 |
+
displayContent = `Document uploaded: ${uploadedFile.name}. ${userQuery ? `\n\n${userQuery}` : ''}`;
|
| 470 |
console.log("DEBUG: Prepared document text for requestBody. Text length:", requestBody.document_text.length);
|
| 471 |
} else if (uploadedFileType === 'document' && uploadedFile && !uploadedFileText) {
|
| 472 |
// For unsupported document types, send filename and a note
|
|
|
|
| 473 |
requestBody.query = `${userQuery}\n\nUser uploaded a document named "${uploadedFile.name}". Please note: Content extraction for this file type is not supported in this demo.`;
|
| 474 |
+
// Change displayContent to a text message for the chat bubble
|
| 475 |
+
displayContent = `Document uploaded: ${uploadedFile.name} (unsupported type). ${userQuery ? `\n\n${userQuery}` : ''}`;
|
| 476 |
console.log("DEBUG: Prepared unsupported document type as text query:", requestBody.query);
|
| 477 |
}
|
| 478 |
|
|
|
|
| 484 |
messageInput.style.height = 'auto'; // Reset textarea height
|
| 485 |
|
| 486 |
// Clear file selection after adding to chat
|
|
|
|
|
|
|
| 487 |
clearFileSelection();
|
| 488 |
|
| 489 |
// Show loading indicator
|