Spaces:
Running
Running
Update main.js
Browse files
main.js
CHANGED
@@ -107,82 +107,65 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
107 |
}
|
108 |
|
109 |
function handleSearch() {
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
const stopWords = ['show', 'me', 'find', 'is', 'a', 'the', 'for', 'where', 'search', 'of', 'at'];
|
122 |
-
const searchTokens = searchTerm.toLowerCase().split(' ').filter(word => !stopWords.includes(word) && word.length > 2);
|
123 |
-
|
124 |
-
if (searchTokens.length === 0) {
|
125 |
-
return addAIMessage("Please be more specific in your local search.");
|
126 |
-
}
|
127 |
-
|
128 |
-
const results = poisData.filter(poi => {
|
129 |
-
const poiText = (poi.name + ' ' + (poi.description || '')).toLowerCase();
|
130 |
-
|
131 |
-
// Check if the POI text contains any of the search keywords
|
132 |
-
return searchTokens.some(token => {
|
133 |
-
// First, check for a direct match
|
134 |
-
if (poiText.includes(token)) return true;
|
135 |
-
|
136 |
-
// NEW: If no direct match and the word is plural,
|
137 |
-
// check for the singular version too (e.g., "landmarks" matches "landmark")
|
138 |
-
if (token.endsWith('s')) {
|
139 |
-
return poiText.includes(token.slice(0, -1));
|
140 |
-
}
|
141 |
-
|
142 |
-
return false;
|
143 |
-
});
|
144 |
-
});
|
145 |
-
|
146 |
-
if (results.length > 0) {
|
147 |
-
let responseMessage = `I found ${results.length} local result(s):<ul>`;
|
148 |
-
results.forEach(poi => { responseMessage += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
|
149 |
-
responseMessage += "</ul>";
|
150 |
-
addAIMessage(responseMessage);
|
151 |
-
} else {
|
152 |
-
addAIMessage(`Sorry, I couldn't find any local places matching "${searchTerm}".`);
|
153 |
-
}
|
154 |
-
|
155 |
-
} else {
|
156 |
-
// --- ASK THE GEMINI AI (Unchanged) ---
|
157 |
-
addAIMessage("AI is thinking...");
|
158 |
-
fetch(`${config.backendUrl}/api/ask`, {
|
159 |
-
method: 'POST',
|
160 |
-
headers: { 'Content-Type': 'application/json' },
|
161 |
-
body: JSON.stringify({ prompt: searchTerm })
|
162 |
-
})
|
163 |
-
.then(response => { if (!response.ok) throw new Error('Network error'); return response.json(); })
|
164 |
-
.then(data => {
|
165 |
-
const chatContainer = aiAssistant.querySelector('.flex-col');
|
166 |
-
const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
|
167 |
-
if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
|
168 |
-
thinkingMessage.querySelector('p').innerHTML = data.response.replace(/\n/g, '<br>');
|
169 |
-
} else {
|
170 |
-
addAIMessage(data.response.replace(/\n/g, '<br>'));
|
171 |
}
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
} else {
|
180 |
-
addAIMessage(
|
181 |
}
|
182 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
}
|
184 |
-
}
|
185 |
-
} // <-- THIS CLOSING BRACE WAS MISSING
|
186 |
|
187 |
// --- EVENT LISTENERS ---
|
188 |
|
@@ -229,5 +212,4 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
229 |
|
230 |
// --- INITIALIZATION ---
|
231 |
fetchPoisAndCreateAREntities();
|
232 |
-
|
233 |
-
}); // <-- THIS CLOSING BRACE AND PARENTHESIS WERE MISSING
|
|
|
107 |
}
|
108 |
|
109 |
function handleSearch() {
|
110 |
+
const searchTerm = userInput.value.trim();
|
111 |
+
if (!searchTerm) return;
|
112 |
+
addUserMessage(searchTerm);
|
113 |
+
userInput.value = '';
|
114 |
+
const searchKeywords = ['search', 'find', 'show', 'where is', 'landmark', 'park', 'beach', 'hollywood', 'map', 'navigate'];
|
115 |
+
const isLocalSearch = searchKeywords.some(keyword => searchTerm.toLowerCase().includes(keyword));
|
116 |
+
if (isLocalSearch) {
|
117 |
+
const stopWords = ['show', 'me', 'find', 'is', 'a', 'the', 'for', 'where', 'search', 'of', 'at'];
|
118 |
+
const searchTokens = searchTerm.toLowerCase().split(' ').filter(word => !stopWords.includes(word) && word.length > 2);
|
119 |
+
if (searchTokens.length === 0) {
|
120 |
+
return addAIMessage("Please be more specific in your local search.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
}
|
122 |
+
const results = poisData.filter(poi => {
|
123 |
+
const poiText = (poi.name + ' ' + (poi.description || '')).toLowerCase();
|
124 |
+
return searchTokens.some(token => {
|
125 |
+
if (poiText.includes(token)) return true;
|
126 |
+
if (token.endsWith('s')) {
|
127 |
+
return poiText.includes(token.slice(0, -1));
|
128 |
+
}
|
129 |
+
return false;
|
130 |
+
});
|
131 |
+
});
|
132 |
+
if (results.length > 0) {
|
133 |
+
let responseMessage = `I found ${results.length} local result(s):<ul>`;
|
134 |
+
results.forEach(poi => { responseMessage += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
|
135 |
+
responseMessage += "</ul>";
|
136 |
+
addAIMessage(responseMessage);
|
137 |
} else {
|
138 |
+
addAIMessage(`Sorry, I couldn't find any local places matching "${searchTerm}".`);
|
139 |
}
|
140 |
+
} else {
|
141 |
+
addAIMessage("AI is thinking...");
|
142 |
+
fetch(`${config.backendUrl}/api/ask`, {
|
143 |
+
method: 'POST',
|
144 |
+
headers: { 'Content-Type': 'application/json' },
|
145 |
+
body: JSON.stringify({ prompt: searchTerm })
|
146 |
+
})
|
147 |
+
.then(response => { if (!response.ok) throw new Error('Network error'); return response.json(); })
|
148 |
+
.then(data => {
|
149 |
+
const chatContainer = aiAssistant.querySelector('.flex-col');
|
150 |
+
const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
|
151 |
+
if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
|
152 |
+
thinkingMessage.querySelector('p').innerHTML = data.response.replace(/\n/g, '<br>');
|
153 |
+
} else {
|
154 |
+
addAIMessage(data.response.replace(/\n/g, '<br>'));
|
155 |
+
}
|
156 |
+
})
|
157 |
+
.catch(error => {
|
158 |
+
console.error('Error asking AI:', error);
|
159 |
+
const chatContainer = aiAssistant.querySelector('.flex-col');
|
160 |
+
const thinkingMessage = Array.from(chatContainer.querySelectorAll('.assistant-message')).pop();
|
161 |
+
if (thinkingMessage && thinkingMessage.textContent.includes("AI is thinking...")) {
|
162 |
+
thinkingMessage.querySelector('p').innerHTML = "Sorry, I had trouble connecting to the AI.";
|
163 |
+
} else {
|
164 |
+
addAIMessage("Sorry, I had trouble connecting to the AI.");
|
165 |
+
}
|
166 |
+
});
|
167 |
+
}
|
168 |
}
|
|
|
|
|
169 |
|
170 |
// --- EVENT LISTENERS ---
|
171 |
|
|
|
212 |
|
213 |
// --- INITIALIZATION ---
|
214 |
fetchPoisAndCreateAREntities();
|
215 |
+
});
|
|