Spaces:
Running
Running
Update main.js
Browse files
main.js
CHANGED
@@ -113,23 +113,79 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
113 |
}
|
114 |
|
115 |
function handleSearch() {
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
const results = poisData.filter(poi =>
|
122 |
poi.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
123 |
(poi.description && poi.description.toLowerCase().includes(searchTerm.toLowerCase()))
|
124 |
);
|
|
|
125 |
if (results.length > 0) {
|
126 |
-
let
|
127 |
-
results.forEach(poi => {
|
128 |
-
|
129 |
-
addAIMessage(
|
130 |
} else {
|
131 |
-
addAIMessage(`Sorry, I couldn't find
|
132 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
}
|
134 |
|
135 |
// --- EVENT LISTENERS ---
|
|
|
113 |
}
|
114 |
|
115 |
function handleSearch() {
|
116 |
+
const searchTerm = userInput.value.trim();
|
117 |
+
if (!searchTerm) return;
|
118 |
+
|
119 |
+
addUserMessage(searchTerm);
|
120 |
+
userInput.value = '';
|
121 |
+
|
122 |
+
// Keywords to help decide if this is a local search
|
123 |
+
const searchKeywords = ['search', 'find', 'show', 'where is', 'landmark', 'park', 'beach', 'hollywood', 'map', 'navigate'];
|
124 |
+
|
125 |
+
// Check if the user's message contains any of our local search keywords
|
126 |
+
const isLocalSearch = searchKeywords.some(keyword => searchTerm.toLowerCase().includes(keyword));
|
127 |
+
|
128 |
+
if (isLocalSearch) {
|
129 |
+
// --- LOGIC FOR LOCAL SEARCH (same as before) ---
|
130 |
+
if (poisData.length === 0) {
|
131 |
+
return addAIMessage("Location data is still loading. Please try again in a moment.");
|
132 |
+
}
|
133 |
const results = poisData.filter(poi =>
|
134 |
poi.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
135 |
(poi.description && poi.description.toLowerCase().includes(searchTerm.toLowerCase()))
|
136 |
);
|
137 |
+
|
138 |
if (results.length > 0) {
|
139 |
+
let responseMessage = `I found ${results.length} local result(s):<ul>`;
|
140 |
+
results.forEach(poi => { responseMessage += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
|
141 |
+
responseMessage += "</ul>";
|
142 |
+
addAIMessage(responseMessage);
|
143 |
} else {
|
144 |
+
addAIMessage(`Sorry, I couldn't find any local places matching "${searchTerm}".`);
|
145 |
}
|
146 |
+
|
147 |
+
} else {
|
148 |
+
// --- NEW LOGIC: ASK THE GEMINI AI ---
|
149 |
+
// 1. Show a temporary "thinking" message for better UX
|
150 |
+
addAIMessage("AI is thinking...");
|
151 |
+
|
152 |
+
// 2. Call your backend's new "/api/ask" endpoint
|
153 |
+
fetch(`${config.backendUrl}/api/ask`, {
|
154 |
+
method: 'POST',
|
155 |
+
headers: {
|
156 |
+
'Content-Type': 'application/json'
|
157 |
+
},
|
158 |
+
body: JSON.stringify({ prompt: searchTerm }) // Send the user's message
|
159 |
+
})
|
160 |
+
.then(response => {
|
161 |
+
if (!response.ok) {
|
162 |
+
throw new Error('Network response was not ok');
|
163 |
+
}
|
164 |
+
return response.json();
|
165 |
+
})
|
166 |
+
.then(data => {
|
167 |
+
// 3. Find the "AI is thinking..." message and replace its content with the real response
|
168 |
+
const chatContainer = aiAssistant.querySelector('.flex-col');
|
169 |
+
const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
|
170 |
+
|
171 |
+
if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
|
172 |
+
thinkingMessage.querySelector('p').innerHTML = data.response.replace(/\n/g, '<br>'); // Update the message and format newlines
|
173 |
+
} else {
|
174 |
+
addAIMessage(data.response.replace(/\n/g, '<br>')); // Fallback: just add a new message
|
175 |
+
}
|
176 |
+
})
|
177 |
+
.catch(error => {
|
178 |
+
console.error('Error asking AI:', error);
|
179 |
+
// 4. If there was an error, replace the "thinking" message with an error message
|
180 |
+
const chatContainer = aiAssistant.querySelector('.flex-col');
|
181 |
+
const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
|
182 |
+
|
183 |
+
if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
|
184 |
+
thinkingMessage.querySelector('p').innerHTML = "Sorry, I had trouble connecting to the AI. Please try again.";
|
185 |
+
} else {
|
186 |
+
addAIMessage("Sorry, I had trouble connecting to the AI. Please try again.");
|
187 |
+
}
|
188 |
+
});
|
189 |
}
|
190 |
|
191 |
// --- EVENT LISTENERS ---
|