Spaces:
Sleeping
Sleeping
Update index.html
Browse filesSwitch voice to Whisper + SSE fallback
- index.html +335 -392
index.html
CHANGED
|
@@ -8,198 +8,101 @@
|
|
| 8 |
<meta name="apple-mobile-web-app-title" content="DE Assistant" />
|
| 9 |
<meta name="theme-color" content="#1a1a2e" />
|
| 10 |
<link rel="manifest" href="/manifest.json" />
|
| 11 |
-
<link rel="apple-touch-icon" href="/icon-192.png" />
|
| 12 |
<title>DE Knowledge Assistant</title>
|
| 13 |
<style>
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
--
|
| 19 |
-
--
|
| 20 |
-
--
|
| 21 |
-
--
|
| 22 |
-
--accent2: #0f3460;
|
| 23 |
-
--user-bg: #0f3460;
|
| 24 |
-
--bot-bg: #1a1a2e;
|
| 25 |
-
--text: #e0e0e0;
|
| 26 |
-
--text-dim: #888;
|
| 27 |
-
--border: #2a2a4a;
|
| 28 |
-
--green: #00c896;
|
| 29 |
-
--radius: 18px;
|
| 30 |
-
--safe-top: env(safe-area-inset-top, 0px);
|
| 31 |
-
--safe-bot: env(safe-area-inset-bottom, 0px);
|
| 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 |
-
.sources { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; }
|
| 105 |
-
.source-chip { font-size: 10px; background: rgba(15,52,96,.6);
|
| 106 |
-
border: 1px solid var(--accent2); border-radius: 20px;
|
| 107 |
-
padding: 3px 9px; color: #a0b4d0; }
|
| 108 |
-
|
| 109 |
-
/* typing indicator */
|
| 110 |
-
.typing-dots span { display: inline-block; width: 6px; height: 6px;
|
| 111 |
-
border-radius: 50%; background: var(--text-dim);
|
| 112 |
-
margin: 0 2px; animation: bounce .9s infinite; }
|
| 113 |
-
.typing-dots span:nth-child(2) { animation-delay: .15s; }
|
| 114 |
-
.typing-dots span:nth-child(3) { animation-delay: .3s; }
|
| 115 |
-
@keyframes bounce { 0%,60%,100%{transform:translateY(0)} 30%{transform:translateY(-6px)} }
|
| 116 |
-
|
| 117 |
-
/* ββ Welcome card βββββββββββββββββββββββββββββββββββββββββ */
|
| 118 |
-
#welcome { text-align: center; padding: 40px 20px; }
|
| 119 |
-
#welcome .icon { font-size: 52px; margin-bottom: 12px; }
|
| 120 |
-
#welcome h2 { font-size: 20px; margin-bottom: 8px; }
|
| 121 |
-
#welcome p { color: var(--text-dim); font-size: 13px; max-width: 280px; margin: 0 auto 20px; }
|
| 122 |
-
.starters { display: flex; flex-direction: column; gap: 8px; max-width: 340px; margin: 0 auto; }
|
| 123 |
-
.starter-btn { background: var(--surface); border: 1px solid var(--border);
|
| 124 |
-
border-radius: 12px; padding: 11px 16px; color: var(--text);
|
| 125 |
-
font-size: 13px; cursor: pointer; text-align: left; transition: border-color .2s, background .2s; }
|
| 126 |
-
.starter-btn:hover { border-color: var(--accent); background: var(--surface2); }
|
| 127 |
-
|
| 128 |
-
/* ββ Input bar ββββββββββββββββββββββββββββββββββββββββββββ */
|
| 129 |
-
#input-bar {
|
| 130 |
-
display: flex; align-items: flex-end; gap: 8px;
|
| 131 |
-
padding: 10px 14px calc(10px + var(--safe-bot));
|
| 132 |
-
background: var(--surface);
|
| 133 |
-
border-top: 1px solid var(--border);
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
#msg-input {
|
| 137 |
-
flex: 1; background: var(--surface2); border: 1px solid var(--border);
|
| 138 |
-
border-radius: 22px; padding: 10px 16px; color: var(--text);
|
| 139 |
-
font-size: 15px; resize: none; min-height: 44px; max-height: 120px;
|
| 140 |
-
line-height: 1.4; outline: none; font-family: inherit;
|
| 141 |
-
transition: border-color .2s;
|
| 142 |
-
}
|
| 143 |
-
#msg-input:focus { border-color: var(--accent); }
|
| 144 |
-
#msg-input::placeholder { color: var(--text-dim); }
|
| 145 |
-
|
| 146 |
-
.icon-btn {
|
| 147 |
-
width: 44px; height: 44px; border-radius: 50%; border: none;
|
| 148 |
-
cursor: pointer; display: grid; place-items: center;
|
| 149 |
-
font-size: 18px; flex-shrink: 0; transition: transform .15s, background .2s;
|
| 150 |
-
}
|
| 151 |
-
.icon-btn:active { transform: scale(.9); }
|
| 152 |
-
|
| 153 |
-
#send-btn { background: var(--accent); }
|
| 154 |
-
#send-btn:disabled { background: #555; cursor: not-allowed; }
|
| 155 |
-
|
| 156 |
-
#voice-btn { background: var(--surface2); border: 1px solid var(--border); }
|
| 157 |
-
#voice-btn.recording {
|
| 158 |
-
background: var(--accent) !important;
|
| 159 |
-
animation: ripple 1.2s ease-out infinite;
|
| 160 |
-
}
|
| 161 |
-
@keyframes ripple {
|
| 162 |
-
0% { box-shadow: 0 0 0 0 rgba(233,69,96,.6); }
|
| 163 |
-
70% { box-shadow: 0 0 0 16px rgba(233,69,96,0); }
|
| 164 |
-
100%{ box-shadow: 0 0 0 0 rgba(233,69,96,0); }
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
/* ββ Voice overlay ββββββββββββββββββββββββββββββββββββββββ */
|
| 168 |
-
#voice-overlay {
|
| 169 |
-
display: none; position: fixed; inset: 0; background: rgba(0,0,0,.8);
|
| 170 |
-
backdrop-filter: blur(10px); flex-direction: column;
|
| 171 |
-
align-items: center; justify-content: center; gap: 20px; z-index: 100;
|
| 172 |
-
}
|
| 173 |
-
#voice-overlay.active { display: flex; }
|
| 174 |
-
#voice-wave { font-size: 64px; animation: pulse 1s infinite; }
|
| 175 |
-
#voice-transcript { color: #ddd; font-size: 16px; max-width: 300px;
|
| 176 |
-
text-align: center; min-height: 40px; }
|
| 177 |
-
#voice-cancel { background: var(--surface); border: 1px solid var(--border);
|
| 178 |
-
color: var(--text); border-radius: 50px; padding: 12px 28px;
|
| 179 |
-
font-size: 15px; cursor: pointer; }
|
| 180 |
-
|
| 181 |
-
/* ββ Toast ββββββββββββββββββββββββββββββββββββββββββββββββ */
|
| 182 |
-
#toast { position: fixed; bottom: calc(90px + var(--safe-bot)); left: 50%;
|
| 183 |
-
transform: translateX(-50%) translateY(20px);
|
| 184 |
-
background: #222; border: 1px solid var(--border); border-radius: 20px;
|
| 185 |
-
padding: 8px 18px; font-size: 13px; opacity: 0; pointer-events: none;
|
| 186 |
-
transition: opacity .3s, transform .3s; white-space: nowrap; z-index: 50; }
|
| 187 |
-
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
|
| 188 |
</style>
|
| 189 |
</head>
|
| 190 |
<body>
|
| 191 |
<div id="app">
|
| 192 |
-
<!-- Header -->
|
| 193 |
<header>
|
| 194 |
<div class="logo">ποΈ</div>
|
| 195 |
<div class="header-text">
|
| 196 |
<h1>DE Knowledge Assistant</h1>
|
| 197 |
<p id="header-sub">Connectingβ¦</p>
|
| 198 |
</div>
|
| 199 |
-
<div id="status-dot"
|
| 200 |
</header>
|
| 201 |
-
|
| 202 |
-
<!-- Messages -->
|
| 203 |
<div id="messages">
|
| 204 |
<div id="welcome">
|
| 205 |
<div class="icon">β‘</div>
|
|
@@ -213,118 +116,113 @@
|
|
| 213 |
</div>
|
| 214 |
</div>
|
| 215 |
</div>
|
| 216 |
-
|
| 217 |
-
<!-- Input bar -->
|
| 218 |
<div id="input-bar">
|
| 219 |
-
<button id="voice-btn" class="icon-btn" onclick="
|
| 220 |
<textarea id="msg-input" rows="1" placeholder="Ask a data engineering questionβ¦"
|
| 221 |
onkeydown="handleKey(event)" oninput="autoResize(this)"></textarea>
|
| 222 |
<button id="send-btn" class="icon-btn" onclick="sendMessage()" title="Send">β€</button>
|
| 223 |
</div>
|
| 224 |
</div>
|
| 225 |
-
|
| 226 |
-
<!-- Voice overlay -->
|
| 227 |
<div id="voice-overlay">
|
| 228 |
<div id="voice-wave">ποΈ</div>
|
| 229 |
-
<div id="voice-
|
| 230 |
-
<
|
|
|
|
|
|
|
| 231 |
</div>
|
| 232 |
-
|
| 233 |
-
<!-- Toast -->
|
| 234 |
<div id="toast"></div>
|
| 235 |
-
|
| 236 |
<script>
|
| 237 |
-
//
|
|
|
|
|
|
|
|
|
|
| 238 |
const state = {
|
| 239 |
history: [],
|
| 240 |
isLoading: false,
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
| 242 |
synthesis: window.speechSynthesis || null,
|
| 243 |
-
|
| 244 |
-
currentUtterance: null,
|
| 245 |
};
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
//
|
| 250 |
const $ = id => document.getElementById(id);
|
| 251 |
-
const msgInput
|
| 252 |
-
const messages
|
| 253 |
-
const sendBtn
|
| 254 |
-
const voiceBtn
|
| 255 |
-
const statusDot
|
| 256 |
-
const headerSub
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
// ββ Health check βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 262 |
async function checkHealth() {
|
| 263 |
try {
|
| 264 |
const r = await fetch(`${BASE_URL}/api/health`);
|
| 265 |
const data = await r.json();
|
| 266 |
-
statusDot.className = 'ready';
|
| 267 |
-
headerSub.textContent =
|
|
|
|
|
|
|
| 268 |
} catch {
|
| 269 |
statusDot.className = 'loading';
|
| 270 |
-
headerSub.textContent = '
|
| 271 |
setTimeout(checkHealth, 3000);
|
| 272 |
}
|
| 273 |
}
|
| 274 |
checkHealth();
|
| 275 |
-
|
| 276 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
function renderMarkdown(text) {
|
| 278 |
return text
|
| 279 |
-
// code
|
| 280 |
-
.replace(/```(\w*)\n?([\s\S]*?)```/g, (_, lang, code) =>
|
| 281 |
`<pre><code class="lang-${lang}">${escHtml(code.trim())}</code></pre>`)
|
| 282 |
-
//
|
| 283 |
-
.replace(/`([^`]+)`/g, (_, c) => `<code>${escHtml(c)}</code>`)
|
| 284 |
-
// bold
|
| 285 |
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
|
| 286 |
-
// italic
|
| 287 |
.replace(/\*(.+?)\*/g, '<em>$1</em>')
|
| 288 |
-
// headings
|
| 289 |
.replace(/^### (.+)$/gm, '<p><strong>$1</strong></p>')
|
| 290 |
.replace(/^## (.+)$/gm, '<p><strong>$1</strong></p>')
|
| 291 |
-
// bullets
|
| 292 |
.replace(/^[-β’] (.+)$/gm, '<li>$1</li>')
|
| 293 |
.replace(/(<li>[\s\S]+?<\/li>)/g, '<ul>$1</ul>')
|
| 294 |
-
// numbered lists
|
| 295 |
.replace(/^\d+\. (.+)$/gm, '<li>$1</li>')
|
| 296 |
-
// emoji-prefixed tips
|
| 297 |
.replace(/^(π‘.+)$/gm, '<blockquote>$1</blockquote>')
|
| 298 |
-
// newlines β paragraphs
|
| 299 |
.replace(/\n\n+/g, '</p><p>')
|
| 300 |
.replace(/^(?!<)(.+)/gm, '$1')
|
| 301 |
-
.replace(/^<p><\/p>$|^<\/p><p>$/gm, '')
|
| 302 |
.trim();
|
| 303 |
}
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
if (welcome.style.display !== 'none' || document.getElementById('welcome')) {
|
| 311 |
-
if ($('welcome')) $('welcome').remove();
|
| 312 |
-
}
|
| 313 |
-
|
| 314 |
const wrap = document.createElement('div');
|
| 315 |
wrap.className = `msg ${role}`;
|
| 316 |
wrap.innerHTML = `
|
| 317 |
-
<div class="avatar">${role
|
| 318 |
-
<div class="bubble"
|
| 319 |
-
role === 'user' ? escHtml(content) : renderMarkdown(content)
|
| 320 |
-
}</div>`;
|
| 321 |
messages.appendChild(wrap);
|
| 322 |
messages.scrollTop = messages.scrollHeight;
|
| 323 |
return wrap.querySelector('.bubble');
|
| 324 |
}
|
| 325 |
-
|
| 326 |
function showTyping() {
|
| 327 |
-
|
| 328 |
const wrap = document.createElement('div');
|
| 329 |
wrap.className = 'msg bot';
|
| 330 |
wrap.id = 'typing-indicator';
|
|
@@ -334,222 +232,267 @@ function showTyping() {
|
|
| 334 |
messages.appendChild(wrap);
|
| 335 |
messages.scrollTop = messages.scrollHeight;
|
| 336 |
}
|
| 337 |
-
|
| 338 |
function removeTyping() { $('typing-indicator')?.remove(); }
|
| 339 |
-
|
| 340 |
-
//
|
|
|
|
|
|
|
| 341 |
async function sendMessage(text) {
|
| 342 |
const msg = (text || msgInput.value).trim();
|
| 343 |
if (!msg || state.isLoading) return;
|
| 344 |
-
|
| 345 |
msgInput.value = '';
|
| 346 |
autoResize(msgInput);
|
| 347 |
state.isLoading = true;
|
| 348 |
sendBtn.disabled = true;
|
| 349 |
statusDot.className = 'loading';
|
| 350 |
-
|
| 351 |
appendMessage('user', msg);
|
| 352 |
showTyping();
|
| 353 |
-
|
| 354 |
-
// Stop any ongoing TTS
|
| 355 |
state.synthesis?.cancel();
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
try {
|
| 358 |
const res = await fetch(`${BASE_URL}/api/chat`, {
|
| 359 |
method: 'POST',
|
| 360 |
headers: { 'Content-Type': 'application/json' },
|
| 361 |
body: JSON.stringify({ message: msg, history: state.history, stream: true }),
|
| 362 |
});
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
if (!res.ok) throw new Error(`Server error ${res.status}`);
|
| 367 |
-
|
| 368 |
-
const bubble = appendMessage('bot', '');
|
| 369 |
-
let full = '';
|
| 370 |
-
|
| 371 |
-
// Server-Sent Events streaming
|
| 372 |
const reader = res.body.getReader();
|
| 373 |
const decoder = new TextDecoder();
|
| 374 |
-
let buffer = '';
|
| 375 |
-
|
|
|
|
|
|
|
|
|
|
| 376 |
while (true) {
|
| 377 |
const { done, value } = await reader.read();
|
| 378 |
if (done) break;
|
| 379 |
buffer += decoder.decode(value, { stream: true });
|
| 380 |
const lines = buffer.split('\n');
|
| 381 |
buffer = lines.pop();
|
| 382 |
-
|
| 383 |
for (const line of lines) {
|
|
|
|
| 384 |
if (!line.startsWith('data: ')) continue;
|
| 385 |
const payload = line.slice(6).trim();
|
| 386 |
-
if (payload === '[DONE]')
|
| 387 |
try {
|
| 388 |
const { chunk } = JSON.parse(payload);
|
| 389 |
if (chunk) {
|
|
|
|
| 390 |
full += chunk;
|
| 391 |
bubble.innerHTML = renderMarkdown(full);
|
| 392 |
messages.scrollTop = messages.scrollHeight;
|
| 393 |
}
|
| 394 |
-
} catch { /*
|
| 395 |
}
|
| 396 |
}
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
}
|
| 407 |
-
|
| 408 |
-
} catch (err) {
|
| 409 |
removeTyping();
|
| 410 |
-
appendMessage('bot',
|
| 411 |
-
|
| 412 |
-
state.isLoading = false;
|
| 413 |
-
sendBtn.disabled = false;
|
| 414 |
-
statusDot.className = 'ready';
|
| 415 |
}
|
| 416 |
}
|
| 417 |
-
|
| 418 |
function sendStarter(btn) { sendMessage(btn.textContent.trim()); }
|
| 419 |
-
|
| 420 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
function speakText(text) {
|
| 422 |
if (!state.synthesis) return;
|
| 423 |
state.synthesis.cancel();
|
| 424 |
-
const utt = new SpeechSynthesisUtterance(text.slice(0, 800));
|
| 425 |
utt.rate = 1.05;
|
| 426 |
-
utt.pitch = 1;
|
| 427 |
-
// Prefer a natural English voice
|
| 428 |
const voices = state.synthesis.getVoices();
|
| 429 |
-
const
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
if (
|
| 433 |
state.synthesis.speak(utt);
|
| 434 |
}
|
| 435 |
-
|
| 436 |
-
function stripMarkdown(
|
| 437 |
-
return
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
}
|
| 445 |
-
|
| 446 |
-
// ββ Voice Input βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 447 |
-
function setupSpeechRecognition() {
|
| 448 |
-
const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 449 |
-
if (!SR) return null;
|
| 450 |
-
|
| 451 |
-
const rec = new SR();
|
| 452 |
-
rec.continuous = false;
|
| 453 |
-
rec.interimResults = true;
|
| 454 |
-
rec.lang = 'en-US';
|
| 455 |
-
|
| 456 |
-
rec.onstart = () => {
|
| 457 |
-
voiceOverlay.classList.add('active');
|
| 458 |
-
voiceBtn.classList.add('recording');
|
| 459 |
-
voiceTranscript.textContent = 'Listeningβ¦';
|
| 460 |
-
};
|
| 461 |
-
|
| 462 |
-
rec.onresult = e => {
|
| 463 |
-
let interim = '', final = '';
|
| 464 |
-
for (let r of e.results) {
|
| 465 |
-
if (r.isFinal) final += r[0].transcript;
|
| 466 |
-
else interim += r[0].transcript;
|
| 467 |
-
}
|
| 468 |
-
voiceTranscript.textContent = final || interim || 'Listeningβ¦';
|
| 469 |
-
if (final) msgInput.value = final;
|
| 470 |
-
};
|
| 471 |
-
|
| 472 |
-
rec.onerror = err => {
|
| 473 |
-
stopVoice();
|
| 474 |
-
if (err.error === 'not-allowed') toast('π€ Microphone permission denied');
|
| 475 |
-
else toast(`Voice error: ${err.error}`);
|
| 476 |
-
};
|
| 477 |
-
|
| 478 |
-
rec.onend = () => {
|
| 479 |
-
voiceOverlay.classList.remove('active');
|
| 480 |
-
voiceBtn.classList.remove('recording');
|
| 481 |
-
state.voiceActive = false;
|
| 482 |
-
const text = msgInput.value.trim();
|
| 483 |
-
if (text) sendMessage(text);
|
| 484 |
-
};
|
| 485 |
-
|
| 486 |
-
return rec;
|
| 487 |
-
}
|
| 488 |
-
|
| 489 |
-
function toggleVoice() {
|
| 490 |
-
if (state.voiceActive) { stopVoice(); return; }
|
| 491 |
-
|
| 492 |
-
if (!state.recognition) {
|
| 493 |
-
state.recognition = setupSpeechRecognition();
|
| 494 |
-
}
|
| 495 |
-
|
| 496 |
-
if (!state.recognition) {
|
| 497 |
-
toast('π€ Voice not supported in this browser. Try Chrome on Android or Safari on iOS 14.5+.');
|
| 498 |
-
return;
|
| 499 |
-
}
|
| 500 |
-
|
| 501 |
-
state.voiceActive = true;
|
| 502 |
-
try { state.recognition.start(); }
|
| 503 |
-
catch { state.voiceActive = false; toast('Could not start microphone'); }
|
| 504 |
}
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
voiceOverlay.classList.remove('active');
|
| 510 |
-
voiceBtn.classList.remove('recording');
|
| 511 |
-
}
|
| 512 |
-
|
| 513 |
-
// ββ UI helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 514 |
function autoResize(el) {
|
| 515 |
el.style.height = 'auto';
|
| 516 |
el.style.height = Math.min(el.scrollHeight, 120) + 'px';
|
| 517 |
}
|
| 518 |
-
|
| 519 |
function handleKey(e) {
|
| 520 |
-
if (e.key === 'Enter' && !e.shiftKey) {
|
| 521 |
-
e.preventDefault();
|
| 522 |
-
sendMessage();
|
| 523 |
-
}
|
| 524 |
}
|
| 525 |
-
|
| 526 |
-
function toast(msg, duration = 3000) {
|
| 527 |
const t = $('toast');
|
| 528 |
t.textContent = msg;
|
| 529 |
t.classList.add('show');
|
| 530 |
-
setTimeout(() => t.classList.remove('show'),
|
| 531 |
}
|
| 532 |
-
|
| 533 |
-
//
|
| 534 |
-
let deferredPrompt;
|
| 535 |
-
window.addEventListener('beforeinstallprompt', e => {
|
| 536 |
-
e.preventDefault();
|
| 537 |
-
deferredPrompt = e;
|
| 538 |
-
setTimeout(() => {
|
| 539 |
-
toast('π² Add to Home Screen for the best experience!', 5000);
|
| 540 |
-
}, 3000);
|
| 541 |
-
});
|
| 542 |
-
|
| 543 |
-
// ββ Service worker ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 544 |
-
if ('serviceWorker' in navigator) {
|
| 545 |
-
navigator.serviceWorker.register('/sw.js').catch(() => {});
|
| 546 |
-
}
|
| 547 |
-
|
| 548 |
-
// Load voices async (required for some browsers)
|
| 549 |
if (state.synthesis) {
|
| 550 |
state.synthesis.onvoiceschanged = () => state.synthesis.getVoices();
|
| 551 |
state.synthesis.getVoices();
|
| 552 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
</script>
|
| 554 |
</body>
|
| 555 |
-
</html>
|
|
|
|
| 8 |
<meta name="apple-mobile-web-app-title" content="DE Assistant" />
|
| 9 |
<meta name="theme-color" content="#1a1a2e" />
|
| 10 |
<link rel="manifest" href="/manifest.json" />
|
|
|
|
| 11 |
<title>DE Knowledge Assistant</title>
|
| 12 |
<style>
|
| 13 |
+
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
|
| 14 |
+
:root{
|
| 15 |
+
--bg:#0d0d1a; --surface:#1a1a2e; --surface2:#16213e;
|
| 16 |
+
--accent:#e94560; --accent2:#0f3460; --user-bg:#0f3460; --bot-bg:#1a1a2e;
|
| 17 |
+
--text:#e0e0e0; --text-dim:#888; --border:#2a2a4a;
|
| 18 |
+
--green:#00c896; --red:#ff4655;
|
| 19 |
+
--radius:18px; --safe-top:env(safe-area-inset-top,0px);
|
| 20 |
+
--safe-bot:env(safe-area-inset-bottom,0px);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
+
html,body{height:100%;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;background:var(--bg);color:var(--text)}
|
| 23 |
+
#app{display:flex;flex-direction:column;height:100dvh;padding-top:var(--safe-top)}
|
| 24 |
+
|
| 25 |
+
header{display:flex;align-items:center;gap:12px;padding:14px 18px;background:var(--surface);border-bottom:1px solid var(--border);backdrop-filter:blur(20px);-webkit-backdrop-filter:blur(20px)}
|
| 26 |
+
.logo{width:36px;height:36px;border-radius:10px;background:linear-gradient(135deg,var(--accent),var(--accent2));display:grid;place-items:center;font-size:18px;flex-shrink:0}
|
| 27 |
+
.header-text h1{font-size:15px;font-weight:700}
|
| 28 |
+
.header-text p{font-size:11px;color:var(--text-dim)}
|
| 29 |
+
#status-dot{width:8px;height:8px;border-radius:50%;background:#555;margin-left:auto;flex-shrink:0;transition:background .3s}
|
| 30 |
+
#status-dot.ready{background:var(--green);box-shadow:0 0 6px var(--green)}
|
| 31 |
+
#status-dot.loading{background:#f5a623;animation:pulse 1s infinite}
|
| 32 |
+
#status-dot.error{background:var(--red);box-shadow:0 0 6px var(--red)}
|
| 33 |
+
@keyframes pulse{0%,100%{opacity:1}50%{opacity:.4}}
|
| 34 |
+
|
| 35 |
+
#messages{flex:1;overflow-y:auto;padding:16px 14px;scroll-behavior:smooth}
|
| 36 |
+
#messages::-webkit-scrollbar{width:4px}
|
| 37 |
+
#messages::-webkit-scrollbar-thumb{background:var(--border);border-radius:2px}
|
| 38 |
+
|
| 39 |
+
.msg{display:flex;gap:10px;margin-bottom:16px;animation:fadeIn .25s ease}
|
| 40 |
+
@keyframes fadeIn{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:none}}
|
| 41 |
+
.msg.user{flex-direction:row-reverse}
|
| 42 |
+
.avatar{width:32px;height:32px;border-radius:50%;display:grid;place-items:center;font-size:14px;flex-shrink:0;align-self:flex-end}
|
| 43 |
+
.msg.user .avatar{background:var(--accent)}
|
| 44 |
+
.msg.bot .avatar{background:var(--accent2)}
|
| 45 |
+
.bubble{max-width:min(75vw,520px);padding:12px 16px;border-radius:var(--radius);line-height:1.6;font-size:14px;word-wrap:break-word}
|
| 46 |
+
.msg.user .bubble{background:var(--user-bg);border-bottom-right-radius:4px}
|
| 47 |
+
.msg.bot .bubble{background:var(--bot-bg);border:1px solid var(--border);border-bottom-left-radius:4px}
|
| 48 |
+
.bubble code{background:rgba(255,255,255,.08);padding:2px 6px;border-radius:4px;font-family:"SF Mono",Menlo,monospace;font-size:12px}
|
| 49 |
+
.bubble pre{background:#0a0a1a;border:1px solid var(--border);border-radius:10px;padding:12px;overflow-x:auto;margin:8px 0}
|
| 50 |
+
.bubble pre code{background:none;padding:0;font-size:12px}
|
| 51 |
+
.bubble strong{color:#fff}
|
| 52 |
+
.bubble p{margin-bottom:8px}
|
| 53 |
+
.bubble p:last-child{margin-bottom:0}
|
| 54 |
+
.bubble ul{padding-left:18px}
|
| 55 |
+
.bubble blockquote{border-left:3px solid var(--accent);padding-left:12px;color:var(--text-dim);margin:8px 0}
|
| 56 |
+
|
| 57 |
+
.typing-dots span{display:inline-block;width:6px;height:6px;border-radius:50%;background:var(--text-dim);margin:0 2px;animation:bounce .9s infinite}
|
| 58 |
+
.typing-dots span:nth-child(2){animation-delay:.15s}
|
| 59 |
+
.typing-dots span:nth-child(3){animation-delay:.3s}
|
| 60 |
+
@keyframes bounce{0%,60%,100%{transform:translateY(0)}30%{transform:translateY(-6px)}}
|
| 61 |
+
|
| 62 |
+
#welcome{text-align:center;padding:40px 20px}
|
| 63 |
+
#welcome .icon{font-size:52px;margin-bottom:12px}
|
| 64 |
+
#welcome h2{font-size:20px;margin-bottom:8px}
|
| 65 |
+
#welcome p{color:var(--text-dim);font-size:13px;max-width:280px;margin:0 auto 20px}
|
| 66 |
+
.starters{display:flex;flex-direction:column;gap:8px;max-width:340px;margin:0 auto}
|
| 67 |
+
.starter-btn{background:var(--surface);border:1px solid var(--border);border-radius:12px;padding:11px 16px;color:var(--text);font-size:13px;cursor:pointer;text-align:left;transition:border-color .2s,background .2s}
|
| 68 |
+
.starter-btn:hover{border-color:var(--accent);background:var(--surface2)}
|
| 69 |
+
|
| 70 |
+
#input-bar{display:flex;align-items:flex-end;gap:8px;padding:10px 14px calc(10px + var(--safe-bot));background:var(--surface);border-top:1px solid var(--border)}
|
| 71 |
+
#msg-input{flex:1;background:var(--surface2);border:1px solid var(--border);border-radius:22px;padding:10px 16px;color:var(--text);font-size:15px;resize:none;min-height:44px;max-height:120px;line-height:1.4;outline:none;font-family:inherit;transition:border-color .2s}
|
| 72 |
+
#msg-input:focus{border-color:var(--accent)}
|
| 73 |
+
#msg-input::placeholder{color:var(--text-dim)}
|
| 74 |
+
|
| 75 |
+
.icon-btn{width:44px;height:44px;border-radius:50%;border:none;cursor:pointer;display:grid;place-items:center;font-size:18px;flex-shrink:0;transition:transform .15s,background .2s}
|
| 76 |
+
.icon-btn:active{transform:scale(.9)}
|
| 77 |
+
#send-btn{background:var(--accent)}
|
| 78 |
+
#send-btn:disabled{background:#555;cursor:not-allowed}
|
| 79 |
+
#voice-btn{background:var(--surface2);border:1px solid var(--border)}
|
| 80 |
+
#voice-btn.recording{background:var(--accent)!important;animation:ripple 1.2s ease-out infinite}
|
| 81 |
+
@keyframes ripple{0%{box-shadow:0 0 0 0 rgba(233,69,96,.6)}70%{box-shadow:0 0 0 16px rgba(233,69,96,0)}100%{box-shadow:0 0 0 0 rgba(233,69,96,0)}}
|
| 82 |
+
|
| 83 |
+
#voice-overlay{display:none;position:fixed;inset:0;background:rgba(0,0,0,.88);backdrop-filter:blur(10px);flex-direction:column;align-items:center;justify-content:center;gap:20px;z-index:100}
|
| 84 |
+
#voice-overlay.active{display:flex}
|
| 85 |
+
#voice-wave{font-size:64px;animation:pulse 1s infinite}
|
| 86 |
+
#voice-status{color:#ddd;font-size:16px;max-width:300px;text-align:center;min-height:40px}
|
| 87 |
+
#voice-timer{color:var(--text-dim);font-size:13px;font-variant-numeric:tabular-nums}
|
| 88 |
+
#voice-stop{background:var(--accent);border:none;color:#fff;border-radius:50px;padding:12px 28px;font-size:15px;cursor:pointer;font-weight:600}
|
| 89 |
+
#voice-cancel{background:transparent;border:1px solid var(--border);color:var(--text);border-radius:50px;padding:10px 24px;font-size:14px;cursor:pointer}
|
| 90 |
+
|
| 91 |
+
#toast{position:fixed;bottom:calc(90px + var(--safe-bot));left:50%;transform:translateX(-50%) translateY(20px);background:#222;border:1px solid var(--border);border-radius:20px;padding:8px 18px;font-size:13px;opacity:0;pointer-events:none;transition:opacity .3s,transform .3s;max-width:90vw;text-align:center;z-index:50}
|
| 92 |
+
#toast.show{opacity:1;transform:translateX(-50%) translateY(0)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
</style>
|
| 94 |
</head>
|
| 95 |
<body>
|
| 96 |
<div id="app">
|
|
|
|
| 97 |
<header>
|
| 98 |
<div class="logo">ποΈ</div>
|
| 99 |
<div class="header-text">
|
| 100 |
<h1>DE Knowledge Assistant</h1>
|
| 101 |
<p id="header-sub">Connectingβ¦</p>
|
| 102 |
</div>
|
| 103 |
+
<div id="status-dot"></div>
|
| 104 |
</header>
|
| 105 |
+
|
|
|
|
| 106 |
<div id="messages">
|
| 107 |
<div id="welcome">
|
| 108 |
<div class="icon">β‘</div>
|
|
|
|
| 116 |
</div>
|
| 117 |
</div>
|
| 118 |
</div>
|
| 119 |
+
|
|
|
|
| 120 |
<div id="input-bar">
|
| 121 |
+
<button id="voice-btn" class="icon-btn" onclick="startVoice()" title="Voice input">π€</button>
|
| 122 |
<textarea id="msg-input" rows="1" placeholder="Ask a data engineering questionβ¦"
|
| 123 |
onkeydown="handleKey(event)" oninput="autoResize(this)"></textarea>
|
| 124 |
<button id="send-btn" class="icon-btn" onclick="sendMessage()" title="Send">β€</button>
|
| 125 |
</div>
|
| 126 |
</div>
|
| 127 |
+
|
|
|
|
| 128 |
<div id="voice-overlay">
|
| 129 |
<div id="voice-wave">ποΈ</div>
|
| 130 |
+
<div id="voice-status">Recordingβ¦</div>
|
| 131 |
+
<div id="voice-timer">0:00</div>
|
| 132 |
+
<button id="voice-stop" onclick="stopVoice(true)">β Done β Transcribe</button>
|
| 133 |
+
<button id="voice-cancel" onclick="stopVoice(false)">β Cancel</button>
|
| 134 |
</div>
|
| 135 |
+
|
|
|
|
| 136 |
<div id="toast"></div>
|
| 137 |
+
|
| 138 |
<script>
|
| 139 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 140 |
+
// State & globals
|
| 141 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 142 |
+
const BASE_URL = window.location.origin;
|
| 143 |
const state = {
|
| 144 |
history: [],
|
| 145 |
isLoading: false,
|
| 146 |
+
mediaRecorder: null,
|
| 147 |
+
audioChunks: [],
|
| 148 |
+
recordStart: 0,
|
| 149 |
+
timerInterval: null,
|
| 150 |
synthesis: window.speechSynthesis || null,
|
| 151 |
+
voiceInputUsed: false,
|
|
|
|
| 152 |
};
|
| 153 |
+
|
| 154 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 155 |
+
// DOM helpers
|
| 156 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 157 |
const $ = id => document.getElementById(id);
|
| 158 |
+
const msgInput = $('msg-input');
|
| 159 |
+
const messages = $('messages');
|
| 160 |
+
const sendBtn = $('send-btn');
|
| 161 |
+
const voiceBtn = $('voice-btn');
|
| 162 |
+
const statusDot = $('status-dot');
|
| 163 |
+
const headerSub = $('header-sub');
|
| 164 |
+
|
| 165 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 166 |
+
// Health check
|
| 167 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 168 |
async function checkHealth() {
|
| 169 |
try {
|
| 170 |
const r = await fetch(`${BASE_URL}/api/health`);
|
| 171 |
const data = await r.json();
|
| 172 |
+
statusDot.className = data.groq_key_set ? 'ready' : 'error';
|
| 173 |
+
headerSub.textContent = data.groq_key_set
|
| 174 |
+
? `${data.vectorstore_docs.toLocaleString()} chunks Β· Groq`
|
| 175 |
+
: 'β οΈ GROQ_API_KEY missing β check Space secrets';
|
| 176 |
} catch {
|
| 177 |
statusDot.className = 'loading';
|
| 178 |
+
headerSub.textContent = 'Server startingβ¦';
|
| 179 |
setTimeout(checkHealth, 3000);
|
| 180 |
}
|
| 181 |
}
|
| 182 |
checkHealth();
|
| 183 |
+
|
| 184 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 185 |
+
// Markdown rendering (dependency-free)
|
| 186 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 187 |
+
function escHtml(s) {
|
| 188 |
+
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
function renderMarkdown(text) {
|
| 192 |
return text
|
| 193 |
+
.replace(/```(\w*)\n?([\s\S]*?)```/g, (_,lang,code) =>
|
|
|
|
| 194 |
`<pre><code class="lang-${lang}">${escHtml(code.trim())}</code></pre>`)
|
| 195 |
+
.replace(/`([^`]+)`/g, (_,c) => `<code>${escHtml(c)}</code>`)
|
|
|
|
|
|
|
| 196 |
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
|
|
|
|
| 197 |
.replace(/\*(.+?)\*/g, '<em>$1</em>')
|
|
|
|
| 198 |
.replace(/^### (.+)$/gm, '<p><strong>$1</strong></p>')
|
| 199 |
.replace(/^## (.+)$/gm, '<p><strong>$1</strong></p>')
|
|
|
|
| 200 |
.replace(/^[-β’] (.+)$/gm, '<li>$1</li>')
|
| 201 |
.replace(/(<li>[\s\S]+?<\/li>)/g, '<ul>$1</ul>')
|
|
|
|
| 202 |
.replace(/^\d+\. (.+)$/gm, '<li>$1</li>')
|
|
|
|
| 203 |
.replace(/^(π‘.+)$/gm, '<blockquote>$1</blockquote>')
|
|
|
|
| 204 |
.replace(/\n\n+/g, '</p><p>')
|
| 205 |
.replace(/^(?!<)(.+)/gm, '$1')
|
|
|
|
| 206 |
.trim();
|
| 207 |
}
|
| 208 |
+
|
| 209 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 210 |
+
// Message rendering
|
| 211 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 212 |
+
function appendMessage(role, content) {
|
| 213 |
+
$('welcome')?.remove();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
const wrap = document.createElement('div');
|
| 215 |
wrap.className = `msg ${role}`;
|
| 216 |
wrap.innerHTML = `
|
| 217 |
+
<div class="avatar">${role==='user'?'π§':'π€'}</div>
|
| 218 |
+
<div class="bubble">${role==='user' ? escHtml(content) : renderMarkdown(content)}</div>`;
|
|
|
|
|
|
|
| 219 |
messages.appendChild(wrap);
|
| 220 |
messages.scrollTop = messages.scrollHeight;
|
| 221 |
return wrap.querySelector('.bubble');
|
| 222 |
}
|
| 223 |
+
|
| 224 |
function showTyping() {
|
| 225 |
+
$('welcome')?.remove();
|
| 226 |
const wrap = document.createElement('div');
|
| 227 |
wrap.className = 'msg bot';
|
| 228 |
wrap.id = 'typing-indicator';
|
|
|
|
| 232 |
messages.appendChild(wrap);
|
| 233 |
messages.scrollTop = messages.scrollHeight;
|
| 234 |
}
|
| 235 |
+
|
| 236 |
function removeTyping() { $('typing-indicator')?.remove(); }
|
| 237 |
+
|
| 238 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 239 |
+
// Send flow β tries streaming first, falls back to /api/chat-simple
|
| 240 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 241 |
async function sendMessage(text) {
|
| 242 |
const msg = (text || msgInput.value).trim();
|
| 243 |
if (!msg || state.isLoading) return;
|
| 244 |
+
|
| 245 |
msgInput.value = '';
|
| 246 |
autoResize(msgInput);
|
| 247 |
state.isLoading = true;
|
| 248 |
sendBtn.disabled = true;
|
| 249 |
statusDot.className = 'loading';
|
| 250 |
+
|
| 251 |
appendMessage('user', msg);
|
| 252 |
showTyping();
|
|
|
|
|
|
|
| 253 |
state.synthesis?.cancel();
|
| 254 |
+
|
| 255 |
+
try {
|
| 256 |
+
const full = await streamWithFallback(msg);
|
| 257 |
+
|
| 258 |
+
state.history.push({ role:'user', content:msg });
|
| 259 |
+
state.history.push({ role:'assistant', content:full });
|
| 260 |
+
if (state.history.length > 12) state.history = state.history.slice(-12);
|
| 261 |
+
|
| 262 |
+
if (state.voiceInputUsed && state.synthesis && full) {
|
| 263 |
+
speakText(stripMarkdown(full));
|
| 264 |
+
state.voiceInputUsed = false;
|
| 265 |
+
}
|
| 266 |
+
} catch (err) {
|
| 267 |
+
removeTyping();
|
| 268 |
+
appendMessage('bot', `β οΈ **Error:** ${err.message}`);
|
| 269 |
+
} finally {
|
| 270 |
+
state.isLoading = false;
|
| 271 |
+
sendBtn.disabled = false;
|
| 272 |
+
statusDot.className = 'ready';
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
async function streamWithFallback(msg) {
|
| 277 |
+
// ββ Try SSE streaming first ββββββββββββββββββββββββββββββββββββββββββββ
|
| 278 |
try {
|
| 279 |
const res = await fetch(`${BASE_URL}/api/chat`, {
|
| 280 |
method: 'POST',
|
| 281 |
headers: { 'Content-Type': 'application/json' },
|
| 282 |
body: JSON.stringify({ message: msg, history: state.history, stream: true }),
|
| 283 |
});
|
| 284 |
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
| 285 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
const reader = res.body.getReader();
|
| 287 |
const decoder = new TextDecoder();
|
| 288 |
+
let buffer = '', full = '', bubble = null, firstChunkAt = Date.now();
|
| 289 |
+
|
| 290 |
+
// If no real data arrives in 25s, abort and fall back
|
| 291 |
+
const watchdog = setTimeout(() => reader.cancel('timeout'), 25000);
|
| 292 |
+
|
| 293 |
while (true) {
|
| 294 |
const { done, value } = await reader.read();
|
| 295 |
if (done) break;
|
| 296 |
buffer += decoder.decode(value, { stream: true });
|
| 297 |
const lines = buffer.split('\n');
|
| 298 |
buffer = lines.pop();
|
| 299 |
+
|
| 300 |
for (const line of lines) {
|
| 301 |
+
if (line.startsWith(':')) continue; // comment / heartbeat
|
| 302 |
if (!line.startsWith('data: ')) continue;
|
| 303 |
const payload = line.slice(6).trim();
|
| 304 |
+
if (payload === '[DONE]') { clearTimeout(watchdog); return full; }
|
| 305 |
try {
|
| 306 |
const { chunk } = JSON.parse(payload);
|
| 307 |
if (chunk) {
|
| 308 |
+
if (!bubble) { removeTyping(); bubble = appendMessage('bot',''); }
|
| 309 |
full += chunk;
|
| 310 |
bubble.innerHTML = renderMarkdown(full);
|
| 311 |
messages.scrollTop = messages.scrollHeight;
|
| 312 |
}
|
| 313 |
+
} catch { /* ignore malformed */ }
|
| 314 |
}
|
| 315 |
}
|
| 316 |
+
clearTimeout(watchdog);
|
| 317 |
+
if (full) return full;
|
| 318 |
+
throw new Error('Empty stream');
|
| 319 |
+
} catch (streamErr) {
|
| 320 |
+
// ββ Fall back to the non-streaming endpoint ββββββββββββββββββββββββ
|
| 321 |
+
console.warn('SSE failed, falling back to /api/chat-simple:', streamErr);
|
| 322 |
+
toast('Streaming blocked β using fallback', 2000);
|
| 323 |
+
|
| 324 |
+
const res = await fetch(`${BASE_URL}/api/chat-simple`, {
|
| 325 |
+
method: 'POST',
|
| 326 |
+
headers: { 'Content-Type': 'application/json' },
|
| 327 |
+
body: JSON.stringify({ message: msg, history: state.history, stream: false }),
|
| 328 |
+
});
|
| 329 |
+
if (!res.ok) {
|
| 330 |
+
const errText = await res.text();
|
| 331 |
+
throw new Error(`Server returned ${res.status}: ${errText}`);
|
| 332 |
}
|
| 333 |
+
const data = await res.json();
|
|
|
|
| 334 |
removeTyping();
|
| 335 |
+
appendMessage('bot', data.response);
|
| 336 |
+
return data.response;
|
|
|
|
|
|
|
|
|
|
| 337 |
}
|
| 338 |
}
|
| 339 |
+
|
| 340 |
function sendStarter(btn) { sendMessage(btn.textContent.trim()); }
|
| 341 |
+
|
| 342 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 343 |
+
// Voice INPUT β MediaRecorder + server-side Groq Whisper
|
| 344 |
+
// (No more `webkitSpeechRecognition` β no more "network" errors)
|
| 345 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 346 |
+
async function startVoice() {
|
| 347 |
+
if (state.mediaRecorder) { stopVoice(false); return; }
|
| 348 |
+
|
| 349 |
+
if (!navigator.mediaDevices?.getUserMedia) {
|
| 350 |
+
toast('π€ This browser does not support audio recording');
|
| 351 |
+
return;
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
let stream;
|
| 355 |
+
try {
|
| 356 |
+
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 357 |
+
} catch (err) {
|
| 358 |
+
toast(err.name === 'NotAllowedError'
|
| 359 |
+
? 'π€ Microphone permission denied'
|
| 360 |
+
: `π€ Mic error: ${err.name}`);
|
| 361 |
+
return;
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
// Pick the best-supported mime type
|
| 365 |
+
const mimeCandidates = ['audio/webm;codecs=opus','audio/webm','audio/mp4','audio/ogg'];
|
| 366 |
+
const mimeType = mimeCandidates.find(t => MediaRecorder.isTypeSupported(t)) || '';
|
| 367 |
+
|
| 368 |
+
state.audioChunks = [];
|
| 369 |
+
state.mediaRecorder = new MediaRecorder(stream, mimeType ? { mimeType } : undefined);
|
| 370 |
+
state.voiceInputUsed = true;
|
| 371 |
+
|
| 372 |
+
state.mediaRecorder.ondataavailable = e => {
|
| 373 |
+
if (e.data.size > 0) state.audioChunks.push(e.data);
|
| 374 |
+
};
|
| 375 |
+
|
| 376 |
+
state.mediaRecorder.onstop = async () => {
|
| 377 |
+
stream.getTracks().forEach(t => t.stop());
|
| 378 |
+
const submitted = state.mediaRecorder._submit !== false;
|
| 379 |
+
const chunks = state.audioChunks;
|
| 380 |
+
state.mediaRecorder = null;
|
| 381 |
+
state.audioChunks = [];
|
| 382 |
+
|
| 383 |
+
if (!submitted || !chunks.length) return;
|
| 384 |
+
|
| 385 |
+
$('voice-status').textContent = 'Transcribingβ¦';
|
| 386 |
+
$('voice-wave').textContent = 'β³';
|
| 387 |
+
|
| 388 |
+
const blob = new Blob(chunks, { type: mimeType || 'audio/webm' });
|
| 389 |
+
const ext = (mimeType.includes('mp4') ? 'm4a'
|
| 390 |
+
: mimeType.includes('ogg') ? 'ogg'
|
| 391 |
+
: 'webm');
|
| 392 |
+
|
| 393 |
+
const fd = new FormData();
|
| 394 |
+
fd.append('audio', blob, `recording.${ext}`);
|
| 395 |
+
|
| 396 |
+
try {
|
| 397 |
+
const res = await fetch(`${BASE_URL}/api/transcribe`, { method:'POST', body: fd });
|
| 398 |
+
const data = await res.json();
|
| 399 |
+
closeVoiceOverlay();
|
| 400 |
+
|
| 401 |
+
if (!res.ok) throw new Error(data.detail || 'Transcription failed');
|
| 402 |
+
const text = (data.text || '').trim();
|
| 403 |
+
if (!text) { toast('No speech detected'); return; }
|
| 404 |
+
|
| 405 |
+
msgInput.value = text;
|
| 406 |
+
autoResize(msgInput);
|
| 407 |
+
sendMessage(text);
|
| 408 |
+
} catch (err) {
|
| 409 |
+
closeVoiceOverlay();
|
| 410 |
+
toast(`Voice error: ${err.message}`, 5000);
|
| 411 |
+
}
|
| 412 |
+
};
|
| 413 |
+
|
| 414 |
+
// ββ Open overlay, start timer ββββββββββββββββββββββββββββββββββββββββββ
|
| 415 |
+
state.recordStart = Date.now();
|
| 416 |
+
$('voice-status').textContent = 'Recording⦠speak your question';
|
| 417 |
+
$('voice-wave').textContent = 'ποΈ';
|
| 418 |
+
$('voice-timer').textContent = '0:00';
|
| 419 |
+
$('voice-overlay').classList.add('active');
|
| 420 |
+
voiceBtn.classList.add('recording');
|
| 421 |
+
|
| 422 |
+
state.timerInterval = setInterval(() => {
|
| 423 |
+
const s = Math.floor((Date.now() - state.recordStart) / 1000);
|
| 424 |
+
$('voice-timer').textContent = `${Math.floor(s/60)}:${String(s%60).padStart(2,'0')}`;
|
| 425 |
+
if (s >= 60) stopVoice(true); // auto-stop at 60s
|
| 426 |
+
}, 200);
|
| 427 |
+
|
| 428 |
+
state.mediaRecorder.start();
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
function stopVoice(submit) {
|
| 432 |
+
clearInterval(state.timerInterval);
|
| 433 |
+
if (!state.mediaRecorder) { closeVoiceOverlay(); return; }
|
| 434 |
+
state.mediaRecorder._submit = submit;
|
| 435 |
+
try { state.mediaRecorder.stop(); } catch { closeVoiceOverlay(); }
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
function closeVoiceOverlay() {
|
| 439 |
+
$('voice-overlay').classList.remove('active');
|
| 440 |
+
voiceBtn.classList.remove('recording');
|
| 441 |
+
clearInterval(state.timerInterval);
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 445 |
+
// Voice OUTPUT β browser SpeechSynthesis (free, no API)
|
| 446 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 447 |
function speakText(text) {
|
| 448 |
if (!state.synthesis) return;
|
| 449 |
state.synthesis.cancel();
|
| 450 |
+
const utt = new SpeechSynthesisUtterance(text.slice(0, 800));
|
| 451 |
utt.rate = 1.05;
|
|
|
|
|
|
|
| 452 |
const voices = state.synthesis.getVoices();
|
| 453 |
+
const pref = voices.find(v => v.lang.startsWith('en') && v.localService)
|
| 454 |
+
|| voices.find(v => v.lang.startsWith('en'))
|
| 455 |
+
|| voices[0];
|
| 456 |
+
if (pref) utt.voice = pref;
|
| 457 |
state.synthesis.speak(utt);
|
| 458 |
}
|
| 459 |
+
|
| 460 |
+
function stripMarkdown(t) {
|
| 461 |
+
return t.replace(/```[\s\S]*?```/g,'code block')
|
| 462 |
+
.replace(/`([^`]+)`/g,'$1')
|
| 463 |
+
.replace(/\*\*(.+?)\*\*/g,'$1')
|
| 464 |
+
.replace(/\*(.+?)\*/g,'$1')
|
| 465 |
+
.replace(/^#+\s/gm,'')
|
| 466 |
+
.replace(/^[-β’]\s/gm,'')
|
| 467 |
+
.replace(/π‘/g,'Pro tip:');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
}
|
| 469 |
+
|
| 470 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 471 |
+
// UI helpers
|
| 472 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
function autoResize(el) {
|
| 474 |
el.style.height = 'auto';
|
| 475 |
el.style.height = Math.min(el.scrollHeight, 120) + 'px';
|
| 476 |
}
|
|
|
|
| 477 |
function handleKey(e) {
|
| 478 |
+
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); }
|
|
|
|
|
|
|
|
|
|
| 479 |
}
|
| 480 |
+
function toast(msg, dur = 3000) {
|
|
|
|
| 481 |
const t = $('toast');
|
| 482 |
t.textContent = msg;
|
| 483 |
t.classList.add('show');
|
| 484 |
+
setTimeout(() => t.classList.remove('show'), dur);
|
| 485 |
}
|
| 486 |
+
|
| 487 |
+
// Load voices async (iOS fix)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
if (state.synthesis) {
|
| 489 |
state.synthesis.onvoiceschanged = () => state.synthesis.getVoices();
|
| 490 |
state.synthesis.getVoices();
|
| 491 |
}
|
| 492 |
+
|
| 493 |
+
if ('serviceWorker' in navigator) {
|
| 494 |
+
navigator.serviceWorker.register('/sw.js').catch(() => {});
|
| 495 |
+
}
|
| 496 |
</script>
|
| 497 |
</body>
|
| 498 |
+
</html>
|