-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.html
More file actions
339 lines (290 loc) · 9.89 KB
/
Copy pathIndex.html
File metadata and controls
339 lines (290 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>AI Voice Assistant</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: system-ui, sans-serif;
background: #0f0f1a;
color: #e2e8f0;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 32px 16px;
}
h1 {
font-size: 1.6rem;
font-weight: 700;
letter-spacing: -0.5px;
margin-bottom: 4px;
}
.subtitle {
font-size: 0.85rem;
color: #64748b;
margin-bottom: 28px;
}
/* ── Chat window ── */
#chat {
width: 100%;
max-width: 680px;
background: #16162a;
border: 1px solid #2d2d50;
border-radius: 16px;
padding: 20px;
min-height: 300px;
max-height: 55vh;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 12px;
margin-bottom: 20px;
}
.msg {
display: flex;
flex-direction: column;
max-width: 80%;
}
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.bot { align-self: flex-start; align-items: flex-start; }
.bubble {
padding: 10px 14px;
border-radius: 14px;
line-height: 1.55;
font-size: 0.92rem;
}
.msg.user .bubble { background: #4f46e5; color: #fff; border-bottom-right-radius: 4px; }
.msg.bot .bubble { background: #1e293b; color: #cbd5e1; border-bottom-left-radius: 4px; }
.label {
font-size: 0.72rem;
color: #475569;
margin-bottom: 4px;
padding: 0 4px;
}
/* typing indicator */
.typing span {
display: inline-block;
width: 6px; height: 6px;
background: #64748b;
border-radius: 50%;
margin: 0 2px;
animation: bounce 1.1s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
0%, 80%, 100% { transform: translateY(0); }
40% { transform: translateY(-6px); }
}
/* ── Controls ── */
.controls {
display: flex;
align-items: center;
gap: 12px;
width: 100%;
max-width: 680px;
}
#textInput {
flex: 1;
background: #16162a;
border: 1px solid #2d2d50;
border-radius: 12px;
padding: 12px 16px;
color: #e2e8f0;
font-size: 0.9rem;
outline: none;
transition: border-color 0.2s;
}
#textInput:focus { border-color: #4f46e5; }
button {
padding: 11px 18px;
border: none;
border-radius: 12px;
font-size: 0.9rem;
font-weight: 600;
cursor: pointer;
transition: opacity 0.15s, transform 0.1s;
}
button:active { transform: scale(0.96); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
#sendBtn { background: #4f46e5; color: #fff; }
#micBtn { background: #1e293b; color: #94a3b8; font-size: 1.2rem; }
#micBtn.recording { background: #dc2626; color: #fff; animation: pulse 1s infinite; }
@keyframes pulse {
0%, 100% { box-shadow: 0 0 0 0 rgba(220,38,38,0.4); }
50% { box-shadow: 0 0 0 8px rgba(220,38,38,0); }
}
#status {
font-size: 0.78rem;
color: #475569;
margin-top: 10px;
min-height: 18px;
}
</style>
</head>
<body>
<h1>🎙 AI Voice Assistant</h1>
<p class="subtitle">Powered by OpenAI · Can search the internet</p>
<div id="chat"></div>
<div class="controls">
<button id="micBtn" title="Hold to record">🎤</button>
<input id="textInput" type="text" placeholder="Type a message or use the mic…" />
<button id="sendBtn">Send</button>
</div>
<div id="status"></div>
<script>
// ── State ──────────────────────────────────────────────────────────────────
let threadId = null;
let recognition = null;
let isRecording = false;
const chat = document.getElementById("chat");
const textInput = document.getElementById("textInput");
const sendBtn = document.getElementById("sendBtn");
const micBtn = document.getElementById("micBtn");
const statusEl = document.getElementById("status");
// ── Initialise thread on load ──────────────────────────────────────────────
async function initThread() {
setStatus("Connecting…");
try {
const res = await fetch("/thread");
const data = await res.json();
threadId = data.threadId;
setStatus("Ready. Say something or type below.");
} catch {
setStatus("⚠️ Could not connect to server. Is it running?");
}
}
// ── Send message ───────────────────────────────────────────────────────────
async function sendMessage(text) {
text = text.trim();
if (!text || !threadId) return;
appendMsg("user", text);
textInput.value = "";
setDisabled(true);
setStatus("Thinking…");
const typingEl = appendTyping();
try {
const res = await fetch("/message", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ threadId, message: text }),
});
const data = await res.json();
typingEl.remove();
if (!res.ok) throw new Error(data.error ?? "Server error");
appendMsg("bot", data.message);
speak(data.message);
setStatus("Done.");
} catch (err) {
typingEl.remove();
appendMsg("bot", `⚠️ ${err.message}`);
setStatus("Error — see message above.");
} finally {
setDisabled(false);
textInput.focus();
}
}
// ── UI helpers ─────────────────────────────────────────────────────────────
function appendMsg(role, text) {
const wrapper = document.createElement("div");
wrapper.className = `msg ${role === "user" ? "user" : "bot"}`;
const label = document.createElement("span");
label.className = "label";
label.textContent = role === "user" ? "You" : "Assistant";
const bubble = document.createElement("div");
bubble.className = "bubble";
bubble.textContent = text;
wrapper.appendChild(label);
wrapper.appendChild(bubble);
chat.appendChild(wrapper);
chat.scrollTop = chat.scrollHeight;
return wrapper;
}
function appendTyping() {
const wrapper = document.createElement("div");
wrapper.className = "msg bot";
const label = document.createElement("span");
label.className = "label";
label.textContent = "Assistant";
const bubble = document.createElement("div");
bubble.className = "bubble typing";
bubble.innerHTML = "<span></span><span></span><span></span>";
wrapper.appendChild(label);
wrapper.appendChild(bubble);
chat.appendChild(wrapper);
chat.scrollTop = chat.scrollHeight;
return wrapper;
}
function setDisabled(disabled) {
sendBtn.disabled = disabled;
micBtn.disabled = disabled;
textInput.disabled = disabled;
}
function setStatus(msg) {
statusEl.textContent = msg;
}
// ── Text-to-speech (Web Speech API) ───────────────────────────────────────
function speak(text) {
if (!("speechSynthesis" in window)) return;
window.speechSynthesis.cancel();
const utter = new SpeechSynthesisUtterance(text);
utter.rate = 1.0;
window.speechSynthesis.speak(utter);
}
// ── Speech-to-text (Web Speech API) ───────────────────────────────────────
function setupSpeechRecognition() {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition) {
micBtn.title = "Speech recognition not supported in this browser";
micBtn.disabled = true;
return;
}
recognition = new SpeechRecognition();
recognition.lang = "en-US";
recognition.interimResults = false;
recognition.onresult = (e) => {
const transcript = e.results[0][0].transcript;
textInput.value = transcript;
sendMessage(transcript);
};
recognition.onerror = (e) => {
setStatus(`Mic error: ${e.error}`);
stopRecording();
};
recognition.onend = stopRecording;
}
function startRecording() {
if (!recognition) return;
isRecording = true;
micBtn.classList.add("recording");
micBtn.textContent = "⏹";
setStatus("Listening…");
recognition.start();
}
function stopRecording() {
isRecording = false;
micBtn.classList.remove("recording");
micBtn.textContent = "🎤";
if (statusEl.textContent === "Listening…") setStatus("");
}
// ── Event listeners ────────────────────────────────────────────────────────
sendBtn.addEventListener("click", () => sendMessage(textInput.value));
textInput.addEventListener("keydown", (e) => {
if (e.key === "Enter" && !e.shiftKey) sendMessage(textInput.value);
});
micBtn.addEventListener("click", () => {
if (isRecording) {
recognition?.stop();
} else {
startRecording();
}
});
// ── Boot ───────────────────────────────────────────────────────────────────
setupSpeechRecognition();
initThread();
</script>
</body>
</html>