Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
@@ -1,87 +1,66 @@
|
|
1 |
-
const
|
2 |
-
const
|
3 |
-
const
|
4 |
-
const
|
5 |
-
const glitchOverlay = document.getElementById("glitchOverlay");
|
6 |
-
const resetButton = document.getElementById("reset");
|
7 |
|
8 |
-
function
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
const target = div.querySelector(".message-text");
|
16 |
-
streamText(text, target);
|
17 |
-
|
18 |
-
if (speaker === "SHODAN") {
|
19 |
-
if (shouldGlitch(text)) {
|
20 |
-
triggerGlitch();
|
21 |
-
}
|
22 |
-
fetch("/voice", {
|
23 |
-
method: "POST",
|
24 |
-
headers: { "Content-Type": "application/json" },
|
25 |
-
body: JSON.stringify({ text })
|
26 |
-
})
|
27 |
-
.then(res => res.json())
|
28 |
-
.then(data => {
|
29 |
-
const audio = new Audio(data.audio_url);
|
30 |
-
audio.play();
|
31 |
-
});
|
32 |
-
}
|
33 |
}
|
34 |
|
35 |
-
function streamText(
|
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 |
-
thinking.style.display = "block";
|
67 |
-
messageInput.value = "";
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
headers: { "Content-Type": "application/json" },
|
72 |
-
body: JSON.stringify({ message: msg })
|
73 |
-
})
|
74 |
-
.then(res => res.json())
|
75 |
-
.then(data => {
|
76 |
-
thinking.style.display = "none";
|
77 |
-
addMessage("SHODAN", data.response);
|
78 |
-
});
|
79 |
-
});
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
});
|
|
|
1 |
+
const form = document.querySelector("form");
|
2 |
+
const input = document.querySelector("input");
|
3 |
+
const chatbox = document.getElementById("chatbox");
|
4 |
+
const avatar = document.getElementById("avatar");
|
|
|
|
|
5 |
|
6 |
+
function appendBubble(text, sender) {
|
7 |
+
const bubble = document.createElement("div");
|
8 |
+
bubble.classList.add("bubble", sender);
|
9 |
+
bubble.textContent = text;
|
10 |
+
chatbox.appendChild(bubble);
|
11 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
|
14 |
+
function streamText(element, text) {
|
15 |
+
element.textContent = "";
|
16 |
+
let i = 0;
|
17 |
+
const interval = setInterval(() => {
|
18 |
+
element.textContent += text[i];
|
19 |
+
i++;
|
20 |
+
if (i >= text.length) clearInterval(interval);
|
21 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
22 |
+
}, 25);
|
23 |
}
|
24 |
|
25 |
+
form.addEventListener("submit", async (e) => {
|
26 |
+
e.preventDefault();
|
27 |
+
const message = input.value.trim();
|
28 |
+
if (!message) return;
|
29 |
|
30 |
+
// User message
|
31 |
+
appendBubble(message, "user");
|
32 |
+
input.value = "";
|
|
|
33 |
|
34 |
+
// Placeholder AI message
|
35 |
+
const aiBubble = document.createElement("div");
|
36 |
+
aiBubble.classList.add("bubble", "ai");
|
37 |
+
aiBubble.textContent = "⏳ SHODAN is thinking...";
|
38 |
+
chatbox.appendChild(aiBubble);
|
39 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
40 |
|
41 |
+
// Send to backend
|
42 |
+
try {
|
43 |
+
const res = await fetch("/api/chat", {
|
44 |
+
method: "POST",
|
45 |
+
headers: { "Content-Type": "application/json" },
|
46 |
+
body: JSON.stringify({ message })
|
47 |
+
});
|
48 |
|
49 |
+
const data = await res.json();
|
|
|
|
|
50 |
|
51 |
+
if (data.reply) {
|
52 |
+
streamText(aiBubble, data.reply);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
// Audio playback if available
|
55 |
+
if (data.audio) {
|
56 |
+
const audio = new Audio(data.audio);
|
57 |
+
audio.play();
|
58 |
+
}
|
59 |
+
} else {
|
60 |
+
aiBubble.textContent = "⚠️ SHODAN is silent. Something went wrong.";
|
61 |
+
}
|
62 |
+
} catch (err) {
|
63 |
+
aiBubble.textContent = "❌ ERROR contacting SHODAN backend.";
|
64 |
+
console.error(err);
|
65 |
+
}
|
66 |
});
|