jisaacso219 commited on
Commit
cfb4d9b
·
verified ·
1 Parent(s): e7fe0b1

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +58 -55
script.js CHANGED
@@ -1,66 +1,69 @@
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
- });
 
1
+ const form = document.getElementById('chat-form');
2
+ const input = form.querySelector('input');
3
+ const chatbox = document.getElementById('chatbox');
4
+ const avatar = document.getElementById('avatar');
5
+ const overlay = document.getElementById('overlay');
6
 
7
+ function addBubble(text, sender = 'user') {
8
+ const bubble = document.createElement('div');
9
+ bubble.className = 'bubble ' + sender;
10
+ bubble.innerText = text;
11
+ chatbox.appendChild(bubble);
12
+ chatbox.scrollTop = chatbox.scrollHeight;
13
  }
14
 
15
+ function streamText(text, onDone) {
16
+ const bubble = document.createElement('div');
17
+ bubble.className = 'bubble ai';
18
+ chatbox.appendChild(bubble);
19
+ chatbox.scrollTop = chatbox.scrollHeight;
20
+
21
+ let i = 0;
22
+ function stream() {
23
+ if (i < text.length) {
24
+ bubble.innerText += text[i++];
25
+ chatbox.scrollTop = chatbox.scrollHeight;
26
+ setTimeout(stream, 20);
27
+ } else {
28
+ onDone && onDone();
29
+ }
30
+ }
31
+ stream();
32
  }
33
 
34
+ function playAudio(url) {
35
+ avatar.classList.add('speaking');
36
+ const audio = new Audio(url);
37
+ audio.play();
38
 
39
+ audio.onended = () => {
40
+ avatar.classList.remove('speaking');
41
+ fetch(`/delete_audio?path=${encodeURIComponent(url)}`);
42
+ };
43
+ }
44
 
45
+ form.onsubmit = async (e) => {
46
+ e.preventDefault();
47
+ const msg = input.value.trim();
48
+ if (!msg) return;
 
 
49
 
50
+ addBubble(msg, 'user');
51
+ input.value = '';
 
 
 
 
 
52
 
53
+ try {
54
+ const response = await fetch('/ask', {
55
+ method: 'POST',
56
+ headers: { 'Content-Type': 'application/json' },
57
+ body: JSON.stringify({ message: msg })
58
+ });
59
+ const { text, audio_url } = await response.json();
60
 
61
+ streamText(text, () => {
62
+ if (audio_url) playAudio(audio_url);
63
+ });
64
+ } catch (err) {
65
+ streamText("❌ SHODAN encountered an error.", null);
66
+ console.error(err);
67
+ }
68
+ };
69