Skip to content

Commit a68d474

Browse files
committed
New Codriver (3)
1 parent 9b265ef commit a68d474

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

codriver/frontend/script.js

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ function changeTheme() {
77
document.body.className = userPreferences.theme;
88
localStorage.setItem("preferences", JSON.stringify(userPreferences));
99
}
10-
// HUGGING FACE API
11-
const response = await fetch("https://new-codriver.onrender.com", {
12-
method: "POST",
13-
headers: {"Content-Type": "application/json"},
14-
body: JSON.stringify({ message: input })
15-
});
16-
1710

1811
// DOM Elements
1912
const userInput = document.getElementById("userInput");
@@ -23,6 +16,7 @@ const micStatus = document.getElementById("micStatus");
2316
// Constants
2417
const WEATHER_API_KEY = "5b72dabcdd9d4b67a75170510250304";
2518
const NYT_API_KEY = "N8j8mb3TSiexCbefuDeT1Ap5MgKPGwmw";
19+
const BACKEND_URL = "https://new-codriver.onrender.com/api/chat"; // your Render backend
2620

2721
let gameActive = false;
2822
let secretNumber = null;
@@ -105,10 +99,7 @@ function getRandomQuote() {
10599

106100
function getRandomFunFact() {
107101
return [
108-
"Honey never spoils!",
109-
"Bananas are berries, but strawberries aren't!",
110-
"Sharks existed before trees.",
111-
"Octopuses have three hearts."
102+
"Honey never spoils!","Bananas are berries, but strawberries aren't!","Sharks existed before trees.","Octopuses have three hearts."
112103
][Math.floor(Math.random() * 4)];
113104
}
114105

@@ -142,11 +133,15 @@ async function sendMessage() {
142133
const input = userInput.value.trim();
143134
if (!input) return;
144135

136+
// Show user's message
145137
const userMessage = document.createElement("p");
146138
userMessage.textContent = "You: " + input;
147139
userMessage.classList.add("user-message");
148140
messageArea.appendChild(userMessage);
149141

142+
let response = "";
143+
144+
// Handle games
150145
if (gameActive) {
151146
if (/exit game/i.test(input)) {
152147
gameActive = false;
@@ -168,39 +163,36 @@ async function sendMessage() {
168163
return;
169164
}
170165

171-
let response = "";
172-
173-
if (/hello|hi|hey|greetings/i.test(input)) {
174-
response = "Hello! How can I assist you today?";
175-
} else if (/quote/i.test(input)) {
176-
response = getRandomQuote();
177-
} else if (/weather/i.test(input)) {
166+
// Local responses
167+
if (/hello|hi|hey|greetings/i.test(input)) response = "Hello! How can I assist you today?";
168+
else if (/quote/i.test(input)) response = getRandomQuote();
169+
else if (/weather/i.test(input)) {
178170
const city = input.split(" ").pop();
179171
response = await fetchWeather(city);
180-
} else if (/news/i.test(input)) {
181-
response = await fetchNews();
182-
} else if (/fun fact/i.test(input)) {
183-
response = getRandomFunFact();
184-
} else if (/joke/i.test(input)) {
185-
response = getRandomJoke();
186-
} else if (/riddle/i.test(input)) {
187-
response = getRandomRiddle();
188-
} else if (/time/i.test(input)) {
189-
response = getCurrentTime();
190-
} else if (/game/i.test(input)) {
172+
}
173+
else if (/news/i.test(input)) response = await fetchNews();
174+
else if (/fun fact/i.test(input)) response = getRandomFunFact();
175+
else if (/joke/i.test(input)) response = getRandomJoke();
176+
else if (/riddle/i.test(input)) response = getRandomRiddle();
177+
else if (/time/i.test(input)) response = getCurrentTime();
178+
else if (/game/i.test(input)) {
191179
gameActive = true;
192180
secretNumber = Math.floor(Math.random() * 10) + 1;
193181
response = "Let's play a game! I'm thinking of a number between 1 and 10. What's your guess?";
194-
} else if (/calculate|what'?s|what is|^[0-9+\-*/().\s]+$/i.test(input)) {
182+
}
183+
else {
184+
// Fallback: call backend for AI response
195185
try {
196-
const expression = input.replace(/[^0-9+\-*/().]/g, "");
197-
const result = Function("'use strict'; return (" + expression + ")")();
198-
response = `The result is ${result}`;
186+
const apiResponse = await fetch(BACKEND_URL, {
187+
method: "POST",
188+
headers: { "Content-Type": "application/json" },
189+
body: JSON.stringify({ message: input })
190+
});
191+
const data = await apiResponse.json();
192+
response = data.reply || "Sorry, I didn't understand that.";
199193
} catch {
200-
response = "I couldn't calculate that. Please check your expression.";
194+
response = "Sorry, I couldn't connect to the server.";
201195
}
202-
} else {
203-
response = "Sorry, I didn't understand that.";
204196
}
205197

206198
addAssistantMessage(response);

0 commit comments

Comments
 (0)