-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathchat.js
More file actions
582 lines (496 loc) · 21.5 KB
/
Copy pathchat.js
File metadata and controls
582 lines (496 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
242
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
317
318
319
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
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
462
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// AgriTech Assistant - Chat Interface & AI Vision Integration
const USE_AI_FALLBACK = true;
const CHAT_HISTORY_STORAGE_KEY = 'agritech_chat_history';
// Local storage prediction helper
function savePrediction(input, output) {
try {
const history = JSON.parse(localStorage.getItem("predictions")) || [];
const newEntry = { input, output, timestamp: new Date().toLocaleString() };
history.unshift(newEntry);
localStorage.setItem("predictions", JSON.stringify(history.slice(0, 5)));
} catch (err) {
console.warn("Prediction saving failed:", err);
}
}
// Rule-based fallback responses (offline mode)
const RULE_BASED_FALLBACKS = {
soil: "Healthy soil is essential for good yields. Add organic compost, avoid excessive chemical use, and test soil regularly.",
crop: "Crop management involves selecting suitable crops for the season, timely sowing, and monitoring pests and diseases.",
crops: "Proper crop care includes crop rotation, pest control, balanced fertilization, and timely irrigation.",
water: "Efficient water management includes drip or sprinkler irrigation and avoiding overwatering.",
irrigation: "Irrigation should be scheduled based on crop growth stage and soil moisture levels.",
fertilizer: "Fertilizers should be applied based on soil test results. Overuse can damage crops and soil health.",
disease: "For crop diseases, early detection is critical. Remove infected leaves, apply recommended organic or chemical fungicides, and use our Disease Detector tool.",
pest: "Manage pests using integrated pest management (IPM), neem oil sprays, pheromone traps, and bio-pesticides."
};
const DEFAULT_FALLBACK_MESSAGE =
"I'm currently running in offline mode. Here's some general advice: focus on soil health, proper irrigation, balanced fertilization, and timely crop monitoring.";
const FALLBACK_MESSAGES = [
"I did not fully catch that, but I can still help. Try asking about crop diseases, irrigation planning, or soil health.",
"I could not find an exact answer yet. Ask me about crop recommendation, weather impact, pest control, or fertilizers.",
"Let us try a more specific question. You can ask: best crops for your state, disease prevention, or water-saving techniques."
];
const MIN_TYPING_DELAY_MS = 600;
const MAX_TYPING_DELAY_MS = 2000;
document.addEventListener('DOMContentLoaded', () => {
// Update copyright year dynamically
const yearElement = document.getElementById('current-year');
if (yearElement) {
yearElement.textContent = new Date().getFullYear();
}
const chatContainer = document.getElementById('chat-container');
const chatWindow = document.getElementById('chat-window');
const chatForm = document.getElementById('chat-form');
const chatInput = document.getElementById('chat-input');
const sendBtn = document.getElementById('send-button');
const clearChatBtn = document.getElementById('clear-chat-btn');
// Photo upload elements
const imageInput = document.getElementById('image-input');
const uploadPhotoBtn = document.getElementById('upload-photo-btn');
const imagePreviewContainer = document.getElementById('image-preview-container');
const imagePreviewImg = document.getElementById('image-preview');
const previewFilename = document.getElementById('preview-filename');
const removeImageBtn = document.getElementById('remove-image-btn');
const dropOverlay = document.getElementById('drop-overlay');
// Lightbox modal elements
const lightboxModal = document.getElementById('image-lightbox-modal');
const lightboxImg = document.getElementById('lightbox-img');
const lightboxCaption = document.getElementById('lightbox-caption');
const lightboxCloseBtn = document.getElementById('lightbox-close-btn');
const lightboxBackdrop = document.getElementById('lightbox-backdrop');
// Active attached image state
let currentImageDataUrl = null;
let currentImageBase64 = null;
let currentImageName = "";
// Initialize JSON-based chatbot if available
let jsonChatbot = null;
if (typeof JSONChatbot === 'function') {
jsonChatbot = new JSONChatbot();
}
let chatHistory = [];
const persistChatHistory = () => {
try {
// Keep up to 30 recent messages in localStorage
const historyToSave = chatHistory.slice(-30);
localStorage.setItem(CHAT_HISTORY_STORAGE_KEY, JSON.stringify(historyToSave));
} catch (error) {
console.warn('Unable to persist chat history:', error);
}
};
const loadChatHistory = () => {
try {
const rawHistory = localStorage.getItem(CHAT_HISTORY_STORAGE_KEY);
const parsedHistory = rawHistory ? JSON.parse(rawHistory) : [];
return Array.isArray(parsedHistory)
? parsedHistory.filter((entry) => entry && (typeof entry.messageContent === 'string' || entry.imageSrc) && (entry.sender === 'user' || entry.sender === 'bot'))
: [];
} catch (error) {
console.warn('Unable to load chat history:', error);
return [];
}
};
const saveMessageToHistory = (messageContent, sender, time, imageSrc = null) => {
chatHistory.push({
messageContent,
sender,
time: time || new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }),
imageSrc: imageSrc || null
});
persistChatHistory();
};
const renderSavedHistory = () => {
if (!chatHistory.length) return false;
const welcomeMessage = chatWindow.querySelector('.welcome-message');
if (welcomeMessage) {
welcomeMessage.remove();
}
chatHistory.forEach(({ messageContent, sender, time, imageSrc }) => {
displayMessage(messageContent, sender, time, false, imageSrc);
});
return true;
};
const clearChatHistory = () => {
chatHistory = [];
persistChatHistory();
chatWindow.innerHTML = `
<div class="welcome-message">
<h3><i class="fas fa-leaf"></i> Welcome to AgriTech Assistant!</h3>
<p>
I'm powered by Google Gemini AI to help with all your farming
questions and guide you through AgriTech platform features.
</p>
<div class="suggestions">
<div class="suggestion">What can I do on AgriTech?</div>
<div class="suggestion">How to use crop recommendation?</div>
<div class="suggestion">Best crops for this season?</div>
<div class="suggestion">Organic pest control methods</div>
<div class="suggestion">Water conservation techniques</div>
<div class="suggestion">Soil health improvement</div>
<div class="suggestion">How to use crop disease detection?</div>
<div class="suggestion">How does labour scheduling work?</div>
<div class="suggestion">How to access farmer forum?</div>
<div class="suggestion">How to check weather updates?</div>
<div class="suggestion">Government schemes for farmers</div>
<div class="suggestion">Fertilizer recommendations</div>
</div>
</div>
`;
};
// HTML escaping function to prevent XSS
function escapeHtml(text) {
if (!text) return '';
return String(text)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// Format message text (support markdown links, bold, italics, code)
function format(txt) {
if (!txt) return '';
return txt
.replace(/\n/g, '<br>')
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.*?)\*/g, '<em>$1</em>')
.replace(/`(.*?)`/g, '<code>$1</code>')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank" rel="noopener noreferrer" style="color:#22c55e;text-decoration:underline;">$1</a>');
}
// Lightbox controls
const openLightbox = (imageSrc, captionText) => {
if (!lightboxModal || !lightboxImg) return;
lightboxImg.src = imageSrc;
if (lightboxCaption) {
lightboxCaption.textContent = captionText || "Crop diagnosis photo";
}
lightboxModal.style.display = 'flex';
lightboxModal.setAttribute('aria-hidden', 'false');
};
const closeLightbox = () => {
if (!lightboxModal) return;
lightboxModal.style.display = 'none';
lightboxModal.setAttribute('aria-hidden', 'true');
if (lightboxImg) lightboxImg.src = '';
};
if (lightboxCloseBtn) lightboxCloseBtn.addEventListener('click', closeLightbox);
if (lightboxBackdrop) lightboxBackdrop.addEventListener('click', closeLightbox);
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && lightboxModal && lightboxModal.style.display === 'flex') {
closeLightbox();
}
});
// Secure message rendering with optional image thumbnail
function displayMessage(messageContent, sender, timeOverride, shouldPersist = true, imageSrc = null) {
const welcomeMessage = chatWindow.querySelector('.welcome-message');
if (welcomeMessage) {
welcomeMessage.remove();
}
const messageElement = document.createElement('div');
messageElement.className = `message ${sender}`;
const time = timeOverride || new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const name = sender === 'user' ? 'You' : 'AgriBot';
const headerDiv = document.createElement('div');
headerDiv.className = 'message-header';
const icon = document.createElement('i');
icon.className = `fas fa-${sender === 'user' ? 'user' : 'robot'}`;
headerDiv.appendChild(icon);
headerDiv.appendChild(document.createTextNode(` ${name}`));
messageElement.appendChild(headerDiv);
// If an image is associated with this message, render preview thumbnail
if (imageSrc) {
const imgContainer = document.createElement('div');
imgContainer.className = 'message-image-container';
const img = document.createElement('img');
img.src = imageSrc;
img.alt = 'Uploaded crop photo';
img.className = 'message-image';
img.addEventListener('click', () => openLightbox(imageSrc, messageContent || "Uploaded crop photo"));
const zoomBadge = document.createElement('span');
zoomBadge.className = 'image-zoom-badge';
zoomBadge.innerHTML = '<i class="fas fa-search-plus"></i> View';
imgContainer.appendChild(img);
imgContainer.appendChild(zoomBadge);
messageElement.appendChild(imgContainer);
}
if (messageContent) {
const textDiv = document.createElement('div');
textDiv.className = 'message-text';
textDiv.innerHTML = format(escapeHtml(messageContent));
messageElement.appendChild(textDiv);
}
const timeDiv = document.createElement('div');
timeDiv.className = 'timestamp';
timeDiv.textContent = time;
messageElement.appendChild(timeDiv);
chatWindow.appendChild(messageElement);
chatWindow.scrollTop = chatWindow.scrollHeight;
if (shouldPersist && !timeOverride) {
saveMessageToHistory(messageContent, sender, time, imageSrc);
}
}
// Handle image selection & preview
const handleImageFile = (file) => {
if (!file) return;
if (!file.type.startsWith('image/')) {
alert('Please select a valid image file (PNG, JPEG, WEBP).');
return;
}
// Limit image size to 10MB
if (file.size > 10 * 1024 * 1024) {
alert('Image is too large. Please upload an image under 10MB.');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
currentImageDataUrl = e.target.result;
currentImageBase64 = e.target.result.split(',')[1];
currentImageName = file.name || "crop_photo.jpg";
if (imagePreviewImg) {
imagePreviewImg.src = currentImageDataUrl;
}
if (previewFilename) {
const sizeKb = Math.round(file.size / 1024);
previewFilename.innerHTML = `<i class="fas fa-image"></i> ${escapeHtml(currentImageName)} <small>(${sizeKb} KB)</small>`;
}
if (imagePreviewContainer) {
imagePreviewContainer.style.display = 'flex';
}
chatInput.focus();
};
reader.readAsDataURL(file);
};
const clearImage = () => {
currentImageDataUrl = null;
currentImageBase64 = null;
currentImageName = "";
if (imageInput) imageInput.value = '';
if (imagePreviewContainer) imagePreviewContainer.style.display = 'none';
if (imagePreviewImg) imagePreviewImg.src = '';
};
// Attach event listeners for image upload button and input
if (uploadPhotoBtn && imageInput) {
uploadPhotoBtn.addEventListener('click', () => {
imageInput.click();
});
}
if (imageInput) {
imageInput.addEventListener('change', (e) => {
const file = e.target.files && e.target.files[0];
if (file) handleImageFile(file);
});
}
if (removeImageBtn) {
removeImageBtn.addEventListener('click', clearImage);
}
// Drag and Drop Handling on Chat Container
if (chatContainer) {
let dragCounter = 0;
chatContainer.addEventListener('dragenter', (e) => {
e.preventDefault();
e.stopPropagation();
dragCounter++;
chatContainer.classList.add('drag-active');
});
chatContainer.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
});
chatContainer.addEventListener('dragleave', (e) => {
e.preventDefault();
e.stopPropagation();
dragCounter--;
if (dragCounter <= 0) {
dragCounter = 0;
chatContainer.classList.remove('drag-active');
}
});
chatContainer.addEventListener('drop', (e) => {
e.preventDefault();
e.stopPropagation();
dragCounter = 0;
chatContainer.classList.remove('drag-active');
const dt = e.dataTransfer;
if (dt && dt.files && dt.files.length > 0) {
const file = dt.files[0];
if (file.type.startsWith('image/')) {
handleImageFile(file);
} else {
alert('Please drop an image file (PNG, JPG, WEBP).');
}
}
});
}
// Clipboard Paste Support (Ctrl+V)
document.addEventListener('paste', (e) => {
const items = (e.clipboardData || e.originalEvent.clipboardData)?.items;
if (!items) return;
for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
const file = items[i].getAsFile();
if (file) {
handleImageFile(file);
break;
}
}
}
});
// Suggestion pill clicks
document.addEventListener('click', (e) => {
if (e.target.classList.contains('suggestion')) {
chatInput.value = e.target.textContent;
chatForm.dispatchEvent(new Event('submit'));
}
});
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const getRandomFallbackMessage = () => {
const randomIndex = Math.floor(Math.random() * FALLBACK_MESSAGES.length);
return FALLBACK_MESSAGES[randomIndex] || DEFAULT_FALLBACK_MESSAGE;
};
const getRuleBasedFallback = (input) => {
const lowerInput = (input || '').toLowerCase();
for (const keyword in RULE_BASED_FALLBACKS) {
if (lowerInput.includes(keyword)) {
return RULE_BASED_FALLBACKS[keyword];
}
}
return getRandomFallbackMessage();
};
const resolveLocalResponse = async (input, hasImage = false) => {
if (hasImage) {
return "🌾 **Crop Image Received for Analysis**\n\nI have received your crop image! To get full live AI vision diagnostics with custom treatment steps, make sure our backend server is active with Gemini API configured.\n\n🔍 **Quick Diagnostic Guidelines**:\n- **Leaf Spot / Blight**: Remove diseased foliage and spray Copper Oxychloride or Mancozeb.\n- **Powdery Mildew / Rust**: Spray Neem oil (10,000 ppm) or wettable sulfur in early morning.\n- **Pest Infestation**: Look under leaves for aphids, thrips, or whiteflies.\n\n👉 **Recommended Tools**:\n- Try our specialized **[Crop Disease Detector](Disease prediction/template/index.html)**\n- Use our **[AI Disease Scanner](ai_disease.html)** for automated leaf pathology!";
}
if (jsonChatbot) {
try {
const details = await jsonChatbot.getResponseDetails(input);
if (details && details.response) {
return details.response;
}
} catch (error) {
console.warn('Local response matching failed:', error);
}
}
return getRuleBasedFallback(input);
};
const computeTypingDelay = (userInput, botReply, hasImage) => {
const inputLength = (userInput || '').length;
const replyLength = (botReply || '').length;
const base = hasImage ? 900 : 600;
const dynamic = Math.min(800, Math.floor((inputLength + replyLength) * 1.8));
return Math.max(MIN_TYPING_DELAY_MS, Math.min(MAX_TYPING_DELAY_MS, base + dynamic));
};
const showTyping = (labelText) => {
const typing = document.createElement('div');
typing.className = 'typing-indicator';
typing.innerHTML = `<div class="typing-text">${escapeHtml(labelText || 'AgriBot is typing')}</div><span></span><span></span><span></span>`;
chatWindow.appendChild(typing);
chatWindow.scrollTop = chatWindow.scrollHeight;
return typing;
};
const setTypingText = (typingElement, text) => {
if (!typingElement) return;
const textElement = typingElement.querySelector('.typing-text');
if (textElement) {
textElement.textContent = text;
}
};
const toggleInput = (disable) => {
if (sendBtn) sendBtn.disabled = disable;
if (chatInput) chatInput.disabled = disable;
if (uploadPhotoBtn) uploadPhotoBtn.disabled = disable;
if (!disable && chatInput) chatInput.focus();
};
// Chat Form Submission
chatForm.addEventListener('submit', async (e) => {
e.preventDefault();
const input = chatInput.value.trim();
const attachedImage = currentImageDataUrl;
const attachedImageBase64 = currentImageBase64;
// Must have either text or image
if (!input && !attachedImageBase64) return;
if (input.length > 1000) {
alert('Message too long. Please keep messages under 1000 characters.');
return;
}
// Render user message with attached image preview
const userDisplayCaption = input || (attachedImage ? "Uploaded crop image for diagnosis" : "");
displayMessage(userDisplayCaption, 'user', undefined, true, attachedImage);
// Reset input fields and clear preview
chatInput.value = '';
chatInput.style.height = 'auto';
clearImage();
const hasImage = Boolean(attachedImageBase64);
const typing = showTyping(hasImage ? 'AgriBot is analyzing your crop photo...' : 'AgriBot is typing...');
toggleInput(true);
const startedAt = Date.now();
try {
let reply = "";
if (USE_AI_FALLBACK) {
setTypingText(typing, hasImage ? 'Scanning plant pathology and symptoms...' : 'Consulting agricultural database...');
try {
const res = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
message: input || "Please identify this crop and diagnose any disease, pest, or deficiency shown in this image, and provide recommendations.",
image: attachedImageBase64 || null
})
});
if (res.ok) {
const data = await res.json();
reply = data.reply || "";
if (reply) {
savePrediction(input || "Crop photo diagnosis", reply);
}
}
} catch (fetchErr) {
console.info("Backend /api/chat not reachable, using offline assistant:", fetchErr.message);
}
}
if (!reply) {
reply = await resolveLocalResponse(input, hasImage);
}
setTypingText(typing, 'Finalizing response...');
const typingDelay = computeTypingDelay(input, reply, hasImage);
const elapsed = Date.now() - startedAt;
if (elapsed < typingDelay) {
await delay(typingDelay - elapsed);
}
displayMessage(reply || DEFAULT_FALLBACK_MESSAGE, 'bot');
} catch (error) {
console.error('Chatbot error:', error);
const fallbackReply = await resolveLocalResponse(input, hasImage);
displayMessage(fallbackReply, 'bot');
} finally {
if (typing) typing.remove();
toggleInput(false);
}
});
// Auto expand textarea on input
chatInput.addEventListener('input', function () {
this.style.height = 'auto';
this.style.height = Math.min(this.scrollHeight, 150) + 'px';
});
// Enter to send, Shift+Enter for new line
chatInput.addEventListener('keydown', function (e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
chatForm.dispatchEvent(new Event('submit'));
}
});
if (clearChatBtn) {
clearChatBtn.addEventListener('click', clearChatHistory);
}
// Load chat history or show welcome message
chatHistory = loadChatHistory();
setTimeout(() => {
if (!renderSavedHistory()) {
displayMessage(
"Hello! 🌱 I'm AgriBot, your AI farming assistant. You can ask me agriculture questions, seek crop recommendations, or **upload photos of your plants/crops** for disease diagnosis and treatment advice. How can I help you today?",
'bot',
undefined,
false
);
}
}, 400);
chatInput.focus();
});