Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- index.html +653 -493
index.html
CHANGED
|
@@ -1,542 +1,702 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
|
|
|
| 3 |
<head>
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
* GLOBAL VARIABLES & RESET
|
| 10 |
-
* Using CSS Variables for consistent theming and easy maintenance.
|
| 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 |
* MAIN CHAT AREA
|
| 90 |
-
* Mimics the ListView behavior but with better UX.
|
| 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 |
* INPUT AREA
|
| 190 |
-
* Fixed at bottom, mimics the GUICtrlCreateInput/Button combo.
|
| 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 |
* ANIMATIONS
|
| 249 |
*/
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
|
| 260 |
-
|
| 261 |
* MODAL / TOAST
|
| 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 |
</head>
|
|
|
|
| 291 |
<body>
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
</header>
|
| 300 |
|
| 301 |
-
<
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
</div>
|
| 308 |
</div>
|
|
|
|
| 309 |
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
</
|
| 317 |
|
| 318 |
<footer>
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
</footer>
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
<
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
-
//
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
const statusBadge = document.getElementById('connection-status');
|
| 344 |
-
const toast = document.getElementById('toast');
|
| 345 |
-
|
| 346 |
-
// --- State ---
|
| 347 |
-
let isGenerating = false;
|
| 348 |
-
let messages = []; // In-memory history
|
| 349 |
-
|
| 350 |
-
// --- Initialization ---
|
| 351 |
-
window.addEventListener('DOMContentLoaded', () => {
|
| 352 |
-
checkConnection();
|
| 353 |
-
userInput.focus();
|
| 354 |
-
|
| 355 |
-
// Auto-resize input (optional modern touch)
|
| 356 |
-
userInput.addEventListener('input', function() {
|
| 357 |
-
this.style.height = 'auto';
|
| 358 |
-
this.style.height = (this.scrollHeight) + 'px';
|
| 359 |
-
if(this.value === '') this.style.height = 'auto';
|
| 360 |
-
});
|
| 361 |
});
|
| 362 |
|
| 363 |
-
//
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
userInput.addEventListener('keydown', (e) => {
|
| 367 |
-
if (e.key === 'Enter' && !e.shiftKey) {
|
| 368 |
-
e.preventDefault();
|
| 369 |
-
handleSend();
|
| 370 |
-
}
|
| 371 |
});
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
}
|
| 385 |
-
}
|
| 386 |
-
|
| 387 |
-
statusBadge.style.backgroundColor = '#c62828';
|
| 388 |
-
showToast('Ollama is not running. Please start it via terminal.');
|
| 389 |
}
|
|
|
|
|
|
|
|
|
|
| 390 |
}
|
|
|
|
| 391 |
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
const
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
try {
|
| 409 |
-
// 3. API Call
|
| 410 |
-
const response = await fetch(OLLAMA_API_URL, {
|
| 411 |
-
method: 'POST',
|
| 412 |
-
headers: { 'Content-Type': 'application/json' },
|
| 413 |
-
body: JSON.stringify({
|
| 414 |
-
model: DEFAULT_MODEL,
|
| 415 |
-
messages: messages,
|
| 416 |
-
stream: true // Enable streaming for real-time feel
|
| 417 |
-
})
|
| 418 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
} catch (e) {
|
| 451 |
-
console.error('Error parsing stream line', e);
|
| 452 |
-
}
|
| 453 |
}
|
| 454 |
}
|
| 455 |
}
|
| 456 |
-
|
| 457 |
-
// 5. Finalize
|
| 458 |
-
messages.push({ role: 'assistant', content: aiResponseText });
|
| 459 |
-
toggleLoading(false);
|
| 460 |
-
|
| 461 |
-
} catch (error) {
|
| 462 |
-
console.error(error);
|
| 463 |
-
addMessageToUI('ai', 'Error: Could not connect to Ollama. Is it running?');
|
| 464 |
-
toggleLoading(false);
|
| 465 |
-
isGenerating = false;
|
| 466 |
-
}
|
| 467 |
-
}
|
| 468 |
-
|
| 469 |
-
// --- UI Helpers ---
|
| 470 |
-
|
| 471 |
-
function addMessageToUI(role, text, isPlaceholder = false) {
|
| 472 |
-
const row = document.createElement('div');
|
| 473 |
-
row.className = `message-row ${role}`;
|
| 474 |
-
|
| 475 |
-
const bubble = document.createElement('div');
|
| 476 |
-
bubble.className = 'message-bubble';
|
| 477 |
-
|
| 478 |
-
// Add timestamp
|
| 479 |
-
const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
| 480 |
-
const timestamp = document.createElement('span');
|
| 481 |
-
timestamp.className = 'timestamp';
|
| 482 |
-
timestamp.textContent = time;
|
| 483 |
-
bubble.appendChild(timestamp);
|
| 484 |
-
|
| 485 |
-
// Add Content
|
| 486 |
-
if (isPlaceholder) {
|
| 487 |
-
const contentSpan = document.createElement('span');
|
| 488 |
-
contentSpan.className = 'content-text';
|
| 489 |
-
bubble.appendChild(contentSpan);
|
| 490 |
-
} else {
|
| 491 |
-
const contentSpan = document.createElement('span');
|
| 492 |
-
contentSpan.className = 'content-text';
|
| 493 |
-
contentSpan.innerHTML = escapeHtml(text);
|
| 494 |
-
bubble.appendChild(contentSpan);
|
| 495 |
}
|
| 496 |
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
chatContainer.insertBefore(row, typingIndicator);
|
| 501 |
-
|
| 502 |
-
// Scroll to bottom
|
| 503 |
-
chatContainer.scrollTop = chatContainer.scrollHeight;
|
| 504 |
|
| 505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
}
|
|
|
|
| 507 |
|
| 508 |
-
|
| 509 |
-
if (show) {
|
| 510 |
-
typingIndicator.style.display = 'flex';
|
| 511 |
-
sendBtn.disabled = true;
|
| 512 |
-
sendBtn.innerHTML = '<span>...</span>';
|
| 513 |
-
} else {
|
| 514 |
-
typingIndicator.style.display = 'none';
|
| 515 |
-
sendBtn.disabled = false;
|
| 516 |
-
sendBtn.innerHTML = '<span>Send</span><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg>';
|
| 517 |
-
userInput.focus();
|
| 518 |
-
}
|
| 519 |
-
}
|
| 520 |
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
}
|
|
|
|
| 539 |
|
| 540 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 541 |
</body>
|
| 542 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
+
|
| 4 |
<head>
|
| 5 |
+
<meta charset="UTF-8">
|
| 6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 7 |
+
<title>Ollama Chat Interface</title>
|
| 8 |
+
<style>
|
| 9 |
+
/*
|
| 10 |
* GLOBAL VARIABLES & RESET
|
|
|
|
| 11 |
*/
|
| 12 |
+
:root {
|
| 13 |
+
--bg-body: #121212;
|
| 14 |
+
--bg-panel: #1e1e1e;
|
| 15 |
+
--bg-input: #2c2c2c;
|
| 16 |
+
--text-main: #e0e0e0;
|
| 17 |
+
--text-muted: #a0a0a0;
|
| 18 |
+
--accent-color: #008080;
|
| 19 |
+
--accent-hover: #009999;
|
| 20 |
+
--border-color: #333;
|
| 21 |
+
--msg-user-bg: #008080;
|
| 22 |
+
--msg-ai-bg: #2c2c2c;
|
| 23 |
+
--font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 24 |
+
--shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.3);
|
| 25 |
+
--shadow-md: 0 4px 8px rgba(0, 0, 0, 0.4);
|
| 26 |
+
--sidebar-width: 280px;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
* {
|
| 30 |
+
box-sizing: border-box;
|
| 31 |
+
margin: 0;
|
| 32 |
+
padding: 0;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
body {
|
| 36 |
+
font-family: var(--font-family);
|
| 37 |
+
background-color: var(--bg-body);
|
| 38 |
+
color: var(--text-main);
|
| 39 |
+
height: 100vh;
|
| 40 |
+
display: flex;
|
| 41 |
+
overflow: hidden;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
/*
|
| 45 |
+
* SIDEBAR
|
| 46 |
+
* Settings and Model Selection
|
| 47 |
*/
|
| 48 |
+
aside {
|
| 49 |
+
width: var(--sidebar-width);
|
| 50 |
+
background-color: var(--bg-panel);
|
| 51 |
+
border-right: 1px solid var(--border-color);
|
| 52 |
+
display: flex;
|
| 53 |
+
flex-direction: column;
|
| 54 |
+
padding: 1.5rem;
|
| 55 |
+
gap: 2rem;
|
| 56 |
+
z-index: 10;
|
| 57 |
+
transition: transform 0.3s ease;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
aside h2 {
|
| 61 |
+
font-size: 1rem;
|
| 62 |
+
text-transform: uppercase;
|
| 63 |
+
letter-spacing: 1px;
|
| 64 |
+
color: var(--text-muted);
|
| 65 |
+
margin-bottom: 0.5rem;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
.control-group {
|
| 69 |
+
display: flex;
|
| 70 |
+
flex-direction: column;
|
| 71 |
+
gap: 0.5rem;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
label {
|
| 75 |
+
font-size: 0.85rem;
|
| 76 |
+
color: var(--text-muted);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
select, textarea {
|
| 80 |
+
background-color: var(--bg-input);
|
| 81 |
+
border: 1px solid var(--border-color);
|
| 82 |
+
color: var(--text-main);
|
| 83 |
+
padding: 0.75rem;
|
| 84 |
+
border-radius: 6px;
|
| 85 |
+
font-family: inherit;
|
| 86 |
+
font-size: 0.9rem;
|
| 87 |
+
outline: none;
|
| 88 |
+
resize: none;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
select:focus, textarea:focus {
|
| 92 |
+
border-color: var(--accent-color);
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
.status-indicator {
|
| 96 |
+
display: flex;
|
| 97 |
+
align-items: center;
|
| 98 |
+
gap: 8px;
|
| 99 |
+
font-size: 0.85rem;
|
| 100 |
+
padding: 0.5rem;
|
| 101 |
+
background: rgba(255, 255, 255, 0.05);
|
| 102 |
+
border-radius: 6px;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
.status-dot {
|
| 106 |
+
width: 8px;
|
| 107 |
+
height: 8px;
|
| 108 |
+
border-radius: 50%;
|
| 109 |
+
background-color: #555;
|
| 110 |
+
transition: background-color 0.3s;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
.status-dot.online { background-color: #2e7d32; box-shadow: 0 0 5px #2e7d32; }
|
| 114 |
+
.status-dot.offline { background-color: #c62828; }
|
| 115 |
+
|
| 116 |
+
/*
|
| 117 |
* MAIN CHAT AREA
|
|
|
|
| 118 |
*/
|
| 119 |
+
main {
|
| 120 |
+
flex: 1;
|
| 121 |
+
display: flex;
|
| 122 |
+
flex-direction: column;
|
| 123 |
+
position: relative;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
#chat-container {
|
| 127 |
+
flex: 1;
|
| 128 |
+
overflow-y: auto;
|
| 129 |
+
padding: 2rem;
|
| 130 |
+
display: flex;
|
| 131 |
+
flex-direction: column;
|
| 132 |
+
gap: 1.5rem;
|
| 133 |
+
scroll-behavior: smooth;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
/* Scrollbar Styling */
|
| 137 |
+
#chat-container::-webkit-scrollbar {
|
| 138 |
+
width: 8px;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
#chat-container::-webkit-scrollbar-track {
|
| 142 |
+
background: var(--bg-body);
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
#chat-container::-webkit-scrollbar-thumb {
|
| 146 |
+
background: #444;
|
| 147 |
+
border-radius: 4px;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
/* Message Bubbles */
|
| 151 |
+
.message-row {
|
| 152 |
+
display: flex;
|
| 153 |
+
width: 100%;
|
| 154 |
+
opacity: 0;
|
| 155 |
+
animation: fadeIn 0.3s forwards;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
.message-row.user {
|
| 159 |
+
justify-content: flex-end;
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
.message-row.ai {
|
| 163 |
+
justify-content: flex-start;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
.message-bubble {
|
| 167 |
+
max-width: 80%;
|
| 168 |
+
padding: 1rem 1.25rem;
|
| 169 |
+
border-radius: 12px;
|
| 170 |
+
font-size: 1rem;
|
| 171 |
+
line-height: 1.6;
|
| 172 |
+
position: relative;
|
| 173 |
+
word-wrap: break-word;
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
.message-row.user .message-bubble {
|
| 177 |
+
background-color: var(--msg-user-bg);
|
| 178 |
+
color: white;
|
| 179 |
+
border-bottom-right-radius: 2px;
|
| 180 |
+
box-shadow: var(--shadow-sm);
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
.message-row.ai .message-bubble {
|
| 184 |
+
background-color: var(--msg-ai-bg);
|
| 185 |
+
color: var(--text-main);
|
| 186 |
+
border-bottom-left-radius: 2px;
|
| 187 |
+
border: 1px solid var(--border-color);
|
| 188 |
+
box-shadow: var(--shadow-sm);
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
.timestamp {
|
| 192 |
+
font-size: 0.7rem;
|
| 193 |
+
opacity: 0.7;
|
| 194 |
+
margin-bottom: 4px;
|
| 195 |
+
display: block;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
/* Typing Indicator */
|
| 199 |
+
.typing-indicator {
|
| 200 |
+
display: flex;
|
| 201 |
+
gap: 4px;
|
| 202 |
+
padding: 12px 16px;
|
| 203 |
+
background-color: var(--msg-ai-bg);
|
| 204 |
+
border-radius: 12px;
|
| 205 |
+
width: fit-content;
|
| 206 |
+
margin-left: 1rem;
|
| 207 |
+
display: none;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
.typing-indicator span {
|
| 211 |
+
width: 8px;
|
| 212 |
+
height: 8px;
|
| 213 |
+
background-color: var(--text-muted);
|
| 214 |
+
border-radius: 50%;
|
| 215 |
+
animation: bounce 1.4s infinite ease-in-out both;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
|
| 219 |
+
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }
|
| 220 |
+
|
| 221 |
+
/*
|
| 222 |
* INPUT AREA
|
|
|
|
| 223 |
*/
|
| 224 |
+
footer {
|
| 225 |
+
background-color: var(--bg-panel);
|
| 226 |
+
padding: 1.5rem;
|
| 227 |
+
border-top: 1px solid var(--border-color);
|
| 228 |
+
display: flex;
|
| 229 |
+
gap: 10px;
|
| 230 |
+
align-items: flex-end;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
#user-input {
|
| 234 |
+
flex: 1;
|
| 235 |
+
background-color: var(--bg-input);
|
| 236 |
+
border: 1px solid var(--border-color);
|
| 237 |
+
color: var(--text-main);
|
| 238 |
+
padding: 12px 16px;
|
| 239 |
+
border-radius: 8px;
|
| 240 |
+
font-size: 1rem;
|
| 241 |
+
outline: none;
|
| 242 |
+
transition: border-color 0.2s, box-shadow 0.2s;
|
| 243 |
+
min-height: 50px;
|
| 244 |
+
max-height: 150px;
|
| 245 |
+
overflow-y: auto;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
#user-input:focus {
|
| 249 |
+
border-color: var(--accent-color);
|
| 250 |
+
box-shadow: 0 0 0 2px rgba(0, 128, 128, 0.2);
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
#send-btn {
|
| 254 |
+
background-color: var(--accent-color);
|
| 255 |
+
color: white;
|
| 256 |
+
border: none;
|
| 257 |
+
padding: 0 24px;
|
| 258 |
+
border-radius: 8px;
|
| 259 |
+
font-size: 1rem;
|
| 260 |
+
font-weight: 600;
|
| 261 |
+
cursor: pointer;
|
| 262 |
+
transition: background-color 0.2s, transform 0.1s;
|
| 263 |
+
height: 50px;
|
| 264 |
+
display: flex;
|
| 265 |
+
align-items: center;
|
| 266 |
+
gap: 8px;
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
#send-btn:hover {
|
| 270 |
+
background-color: var(--accent-hover);
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
#send-btn:active {
|
| 274 |
+
transform: scale(0.98);
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
#send-btn:disabled {
|
| 278 |
+
background-color: #444;
|
| 279 |
+
cursor: not-allowed;
|
| 280 |
+
opacity: 0.7;
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
/*
|
| 284 |
* ANIMATIONS
|
| 285 |
*/
|
| 286 |
+
@keyframes fadeIn {
|
| 287 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 288 |
+
to { opacity: 1; transform: translateY(0); }
|
| 289 |
+
}
|
| 290 |
|
| 291 |
+
@keyframes bounce {
|
| 292 |
+
0%, 80%, 100% { transform: scale(0); }
|
| 293 |
+
40% { transform: scale(1); }
|
| 294 |
+
}
|
| 295 |
|
| 296 |
+
/*
|
| 297 |
* MODAL / TOAST
|
| 298 |
*/
|
| 299 |
+
.toast {
|
| 300 |
+
position: fixed;
|
| 301 |
+
top: 20px;
|
| 302 |
+
left: 50%;
|
| 303 |
+
transform: translateX(-50%) translateY(-100px);
|
| 304 |
+
background-color: #333;
|
| 305 |
+
color: white;
|
| 306 |
+
padding: 12px 24px;
|
| 307 |
+
border-radius: 8px;
|
| 308 |
+
box-shadow: var(--shadow-md);
|
| 309 |
+
opacity: 0;
|
| 310 |
+
transition: all 0.4s ease;
|
| 311 |
+
z-index: 100;
|
| 312 |
+
border-left: 4px solid var(--accent-color);
|
| 313 |
+
display: flex;
|
| 314 |
+
align-items: center;
|
| 315 |
+
gap: 10px;
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
.toast.show {
|
| 319 |
+
transform: translateX(-50%) translateY(0);
|
| 320 |
+
opacity: 1;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
/* Responsive */
|
| 324 |
+
@media (max-width: 768px) {
|
| 325 |
+
aside {
|
| 326 |
+
position: fixed;
|
| 327 |
+
height: 100%;
|
| 328 |
+
left: -280px;
|
| 329 |
+
box-shadow: 2px 0 10px rgba(0,0,0,0.5);
|
| 330 |
+
}
|
| 331 |
+
aside.open {
|
| 332 |
+
left: 0;
|
| 333 |
+
}
|
| 334 |
+
.mobile-menu-btn {
|
| 335 |
+
display: block !important;
|
| 336 |
+
}
|
| 337 |
+
.message-bubble { max-width: 90%; }
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
.mobile-menu-btn {
|
| 341 |
+
display: none;
|
| 342 |
+
position: absolute;
|
| 343 |
+
top: 1rem;
|
| 344 |
+
left: 1rem;
|
| 345 |
+
z-index: 20;
|
| 346 |
+
background: var(--bg-panel);
|
| 347 |
+
border: 1px solid var(--border-color);
|
| 348 |
+
color: var(--text-main);
|
| 349 |
+
padding: 0.5rem;
|
| 350 |
+
border-radius: 4px;
|
| 351 |
+
}
|
| 352 |
+
</style>
|
| 353 |
</head>
|
| 354 |
+
|
| 355 |
<body>
|
| 356 |
|
| 357 |
+
<!-- Mobile Menu Toggle -->
|
| 358 |
+
<button class="mobile-menu-btn" id="menu-toggle">⚙️</button>
|
| 359 |
+
|
| 360 |
+
<!-- Sidebar -->
|
| 361 |
+
<aside id="sidebar">
|
| 362 |
+
<div class="status-indicator">
|
| 363 |
+
<div class="status-dot" id="status-dot"></div>
|
| 364 |
+
<span id="status-text">Checking...</span>
|
| 365 |
+
</div>
|
| 366 |
+
|
| 367 |
+
<div class="control-group">
|
| 368 |
+
<h2>Settings</h2>
|
| 369 |
+
<label for="model-select">Ollama Model</label>
|
| 370 |
+
<select id="model-select">
|
| 371 |
+
<option value="llama3.2">llama3.2 (Default)</option>
|
| 372 |
+
<option value="codellama">CodeLlama</option>
|
| 373 |
+
<option value="mistral">Mistral</option>
|
| 374 |
+
<option value="llava">LLaVA (Vision)</option>
|
| 375 |
+
</select>
|
| 376 |
+
</div>
|
| 377 |
+
|
| 378 |
+
<div class="control-group">
|
| 379 |
+
<label for="system-prompt">System Prompt</label>
|
| 380 |
+
<textarea id="system-prompt" rows="4">You are a helpful AI assistant.</textarea>
|
| 381 |
+
</div>
|
| 382 |
+
|
| 383 |
+
<div class="control-group" style="margin-top: auto;">
|
| 384 |
+
<button id="refresh-models-btn" style="background: var(--bg-input); border: 1px solid var(--border-color); color: var(--text-main); padding: 8px; border-radius: 4px; cursor: pointer;">Refresh Models</button>
|
| 385 |
+
</div>
|
| 386 |
+
</aside>
|
| 387 |
+
|
| 388 |
+
<!-- Main Chat -->
|
| 389 |
+
<main>
|
| 390 |
+
<header style="padding: 1rem 1.5rem; border-bottom: 1px solid var(--border-color); display: flex; align-items: center;">
|
| 391 |
+
<h1 style="font-size: 1.2rem; font-weight: 600; display: flex; align-items: center; gap: 10px;">
|
| 392 |
+
<span>🤖</span> Ollama Chat
|
| 393 |
+
</h1>
|
| 394 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="margin-left: auto; font-size: 0.8rem; color: var(--text-muted); text-decoration: none;">Built with anycoder</a>
|
| 395 |
</header>
|
| 396 |
|
| 397 |
+
<div id="chat-container">
|
| 398 |
+
<!-- Initial Welcome Message -->
|
| 399 |
+
<div class="message-row ai">
|
| 400 |
+
<div class="message-bubble">
|
| 401 |
+
<span class="timestamp">System</span>
|
| 402 |
+
Welcome! Ensure Ollama is running locally on port 11434. I am connected and ready to chat.
|
|
|
|
| 403 |
</div>
|
| 404 |
+
</div>
|
| 405 |
|
| 406 |
+
<!-- Typing Indicator -->
|
| 407 |
+
<div class="typing-indicator" id="typing-indicator">
|
| 408 |
+
<span></span>
|
| 409 |
+
<span></span>
|
| 410 |
+
<span></span>
|
| 411 |
+
</div>
|
| 412 |
+
</div>
|
| 413 |
|
| 414 |
<footer>
|
| 415 |
+
<textarea id="user-input" placeholder="Type a message... (Shift+Enter for new line)" rows="1"></textarea>
|
| 416 |
+
<button id="send-btn">
|
| 417 |
+
<span>Send</span>
|
| 418 |
+
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg>
|
| 419 |
+
</button>
|
| 420 |
</footer>
|
| 421 |
+
</main>
|
| 422 |
+
|
| 423 |
+
<div id="toast" class="toast">
|
| 424 |
+
<span id="toast-icon">ℹ️</span>
|
| 425 |
+
<span id="toast-message">Notification</span>
|
| 426 |
+
</div>
|
| 427 |
+
|
| 428 |
+
<script>
|
| 429 |
+
/**
|
| 430 |
+
* OLLAMA CHAT LOGIC
|
| 431 |
+
* Handles API communication, state management, and UI updates.
|
| 432 |
+
*/
|
| 433 |
+
|
| 434 |
+
// --- Configuration ---
|
| 435 |
+
const OLLAMA_API_URL = 'http://localhost:11434/api/chat';
|
| 436 |
+
const OLLAMA_TAGS_URL = 'http://localhost:11434/api/tags';
|
| 437 |
+
|
| 438 |
+
// --- DOM Elements ---
|
| 439 |
+
const chatContainer = document.getElementById('chat-container');
|
| 440 |
+
const userInput = document.getElementById('user-input');
|
| 441 |
+
const sendBtn = document.getElementById('send-btn');
|
| 442 |
+
const typingIndicator = document.getElementById('typing-indicator');
|
| 443 |
+
const statusDot = document.getElementById('status-dot');
|
| 444 |
+
const statusText = document.getElementById('status-text');
|
| 445 |
+
const toast = document.getElementById('toast');
|
| 446 |
+
const modelSelect = document.getElementById('model-select');
|
| 447 |
+
const systemPrompt = document.getElementById('system-prompt');
|
| 448 |
+
const refreshModelsBtn = document.getElementById('refresh-models-btn');
|
| 449 |
+
const menuToggle = document.getElementById('menu-toggle');
|
| 450 |
+
const sidebar = document.getElementById('sidebar');
|
| 451 |
+
|
| 452 |
+
// --- State ---
|
| 453 |
+
let isGenerating = false;
|
| 454 |
+
let messages = []; // Conversation history
|
| 455 |
+
|
| 456 |
+
// --- Initialization ---
|
| 457 |
+
window.addEventListener('DOMContentLoaded', () => {
|
| 458 |
+
checkConnection();
|
| 459 |
+
userInput.focus();
|
| 460 |
|
| 461 |
+
// Auto-resize textarea
|
| 462 |
+
userInput.addEventListener('input', function() {
|
| 463 |
+
this.style.height = 'auto';
|
| 464 |
+
this.style.height = (this.scrollHeight) + 'px';
|
| 465 |
+
if(this.value === '') this.style.height = 'auto';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
});
|
| 467 |
|
| 468 |
+
// Sidebar toggle for mobile
|
| 469 |
+
menuToggle.addEventListener('click', () => {
|
| 470 |
+
sidebar.classList.toggle('open');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
});
|
| 472 |
+
});
|
| 473 |
+
|
| 474 |
+
// --- Event Listeners ---
|
| 475 |
+
sendBtn.addEventListener('click', handleSend);
|
| 476 |
+
|
| 477 |
+
userInput.addEventListener('keydown', (e) => {
|
| 478 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
| 479 |
+
e.preventDefault();
|
| 480 |
+
handleSend();
|
| 481 |
+
}
|
| 482 |
+
});
|
| 483 |
+
|
| 484 |
+
refreshModelsBtn.addEventListener('click', fetchModels);
|
| 485 |
+
|
| 486 |
+
// --- Core Functions ---
|
| 487 |
+
|
| 488 |
+
async function checkConnection() {
|
| 489 |
+
try {
|
| 490 |
+
const response = await fetch(OLLAMA_TAGS_URL);
|
| 491 |
+
if (response.ok) {
|
| 492 |
+
updateStatus(true);
|
| 493 |
+
showToast('Connected to Ollama');
|
| 494 |
+
// Only fetch models if we haven't populated them yet
|
| 495 |
+
if(modelSelect.options.length === 1) {
|
| 496 |
+
fetchModels();
|
| 497 |
}
|
| 498 |
+
} else {
|
| 499 |
+
throw new Error('Not connected');
|
|
|
|
|
|
|
| 500 |
}
|
| 501 |
+
} catch (error) {
|
| 502 |
+
updateStatus(false);
|
| 503 |
+
showToast('Ollama is not running.', true);
|
| 504 |
}
|
| 505 |
+
}
|
| 506 |
|
| 507 |
+
async function fetchModels() {
|
| 508 |
+
try {
|
| 509 |
+
const response = await fetch(OLLAMA_TAGS_URL);
|
| 510 |
+
const data = await response.json();
|
| 511 |
+
|
| 512 |
+
// Clear existing options except default
|
| 513 |
+
modelSelect.innerHTML = '<option value="">Loading...</option>';
|
| 514 |
+
|
| 515 |
+
if (data.models && data.models.length > 0) {
|
| 516 |
+
modelSelect.innerHTML = '';
|
| 517 |
+
data.models.forEach(model => {
|
| 518 |
+
const option = document.createElement('option');
|
| 519 |
+
option.value = model.name;
|
| 520 |
+
option.textContent = model.name;
|
| 521 |
+
modelSelect.appendChild(option);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
});
|
| 523 |
+
showToast(`Loaded ${data.models.length} models`);
|
| 524 |
+
} else {
|
| 525 |
+
modelSelect.innerHTML = '<option value="">No models found</option>';
|
| 526 |
+
}
|
| 527 |
+
} catch (error) {
|
| 528 |
+
console.error(error);
|
| 529 |
+
showToast('Failed to fetch models', true);
|
| 530 |
+
}
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
async function handleSend() {
|
| 534 |
+
if (isGenerating) return;
|
| 535 |
+
|
| 536 |
+
const text = userInput.value.trim();
|
| 537 |
+
if (!text) return;
|
| 538 |
+
|
| 539 |
+
// 1. UI Updates
|
| 540 |
+
addMessageToUI('user', text);
|
| 541 |
+
userInput.value = '';
|
| 542 |
+
userInput.style.height = 'auto'; // Reset height
|
| 543 |
+
isGenerating = true;
|
| 544 |
+
toggleLoading(true);
|
| 545 |
+
|
| 546 |
+
// 2. Prepare Payload
|
| 547 |
+
// We prepend the system prompt to the history for context
|
| 548 |
+
const history = [
|
| 549 |
+
{ role: 'system', content: systemPrompt.value },
|
| 550 |
+
...messages
|
| 551 |
+
];
|
| 552 |
+
|
| 553 |
+
try {
|
| 554 |
+
const response = await fetch(OLLAMA_API_URL, {
|
| 555 |
+
method: 'POST',
|
| 556 |
+
headers: { 'Content-Type': 'application/json' },
|
| 557 |
+
body: JSON.stringify({
|
| 558 |
+
model: modelSelect.value || 'llama3.2',
|
| 559 |
+
messages: history,
|
| 560 |
+
stream: true
|
| 561 |
+
})
|
| 562 |
+
});
|
| 563 |
|
| 564 |
+
if (!response.ok) throw new Error('API request failed');
|
| 565 |
+
|
| 566 |
+
// 3. Handle Streaming
|
| 567 |
+
const reader = response.body.getReader();
|
| 568 |
+
const decoder = new TextDecoder();
|
| 569 |
+
let aiResponseText = '';
|
| 570 |
+
|
| 571 |
+
// Create a placeholder AI message
|
| 572 |
+
const aiBubble = addMessageToUI('ai', '', true);
|
| 573 |
+
|
| 574 |
+
while (true) {
|
| 575 |
+
const { done, value } = await reader.read();
|
| 576 |
+
if (done) break;
|
| 577 |
+
|
| 578 |
+
const chunk = decoder.decode(value, { stream: true });
|
| 579 |
+
const lines = chunk.split('\n');
|
| 580 |
+
|
| 581 |
+
for (const line of lines) {
|
| 582 |
+
if (line.startsWith('data: ')) {
|
| 583 |
+
const jsonStr = line.slice(6);
|
| 584 |
+
if (jsonStr === '[DONE]') break;
|
| 585 |
+
|
| 586 |
+
try {
|
| 587 |
+
const data = JSON.parse(jsonStr);
|
| 588 |
+
const content = data.message?.content || '';
|
| 589 |
+
aiResponseText += content;
|
| 590 |
+
aiBubble.querySelector('.content-text').innerHTML = escapeHtml(aiResponseText);
|
| 591 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
| 592 |
+
} catch (e) {
|
| 593 |
+
console.error('Error parsing stream', e);
|
|
|
|
|
|
|
|
|
|
| 594 |
}
|
| 595 |
}
|
| 596 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 597 |
}
|
| 598 |
|
| 599 |
+
// 4. Save to history
|
| 600 |
+
messages.push({ role: 'user', content: text });
|
| 601 |
+
messages.push({ role: 'assistant', content: aiResponseText });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
|
| 603 |
+
} catch (error) {
|
| 604 |
+
console.error(error);
|
| 605 |
+
addMessageToUI('ai', 'Error: Could not connect to Ollama. Is it running on port 11434?');
|
| 606 |
+
toggleLoading(false);
|
| 607 |
+
isGenerating = false;
|
| 608 |
}
|
| 609 |
+
}
|
| 610 |
|
| 611 |
+
// --- UI Helpers ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 612 |
|
| 613 |
+
function addMessageToUI(role, text, isPlaceholder = false) {
|
| 614 |
+
const row = document.createElement('div');
|
| 615 |
+
row.className = `message-row ${role}`;
|
| 616 |
+
|
| 617 |
+
const bubble = document.createElement('div');
|
| 618 |
+
bubble.className = 'message-bubble';
|
| 619 |
+
|
| 620 |
+
const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
| 621 |
+
const timestamp = document.createElement('span');
|
| 622 |
+
timestamp.className = 'timestamp';
|
| 623 |
+
timestamp.textContent = time;
|
| 624 |
+
bubble.appendChild(timestamp);
|
| 625 |
+
|
| 626 |
+
if (isPlaceholder) {
|
| 627 |
+
const contentSpan = document.createElement('span');
|
| 628 |
+
contentSpan.className = 'content-text';
|
| 629 |
+
bubble.appendChild(contentSpan);
|
| 630 |
+
} else {
|
| 631 |
+
const contentSpan = document.createElement('span');
|
| 632 |
+
contentSpan.className = 'content-text';
|
| 633 |
+
contentSpan.innerHTML = escapeHtml(text);
|
| 634 |
+
bubble.appendChild(contentSpan);
|
| 635 |
+
}
|
| 636 |
+
|
| 637 |
+
row.appendChild(bubble);
|
| 638 |
+
|
| 639 |
+
// Insert before the typing indicator
|
| 640 |
+
chatContainer.insertBefore(row, typingIndicator);
|
| 641 |
+
|
| 642 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
| 643 |
+
|
| 644 |
+
return bubble;
|
| 645 |
+
}
|
| 646 |
+
|
| 647 |
+
function toggleLoading(show) {
|
| 648 |
+
if (show) {
|
| 649 |
+
typingIndicator.style.display = 'flex';
|
| 650 |
+
sendBtn.disabled = true;
|
| 651 |
+
sendBtn.innerHTML = '<span>...</span>';
|
| 652 |
+
sidebar.style.opacity = '0.5';
|
| 653 |
+
sidebar.style.pointerEvents = 'none';
|
| 654 |
+
} else {
|
| 655 |
+
typingIndicator.style.display = 'none';
|
| 656 |
+
sendBtn.disabled = false;
|
| 657 |
+
sendBtn.innerHTML = '<span>Send</span><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg>';
|
| 658 |
+
userInput.focus();
|
| 659 |
+
sidebar.style.opacity = '1';
|
| 660 |
+
sidebar.style.pointerEvents = 'auto';
|
| 661 |
}
|
| 662 |
+
}
|
| 663 |
|
| 664 |
+
function showToast(msg, isError = false) {
|
| 665 |
+
const toastEl = document.getElementById('toast');
|
| 666 |
+
const toastMsg = document.getElementById('toast-message');
|
| 667 |
+
const toastIcon = document.getElementById('toast-icon');
|
| 668 |
+
|
| 669 |
+
toastMsg.textContent = msg;
|
| 670 |
+
toastIcon.textContent = isError ? '⚠️' : '✅';
|
| 671 |
+
toastEl.style.borderLeftColor = isError ? '#c62828' : 'var(--accent-color)';
|
| 672 |
+
|
| 673 |
+
toastEl.classList.add('show');
|
| 674 |
+
setTimeout(() => {
|
| 675 |
+
toastEl.classList.remove('show');
|
| 676 |
+
}, 3000);
|
| 677 |
+
}
|
| 678 |
+
|
| 679 |
+
function updateStatus(isOnline) {
|
| 680 |
+
if (isOnline) {
|
| 681 |
+
statusDot.className = 'status-dot online';
|
| 682 |
+
statusText.textContent = 'Online';
|
| 683 |
+
statusText.style.color = '#2e7d32';
|
| 684 |
+
} else {
|
| 685 |
+
statusDot.className = 'status-dot offline';
|
| 686 |
+
statusText.textContent = 'Offline';
|
| 687 |
+
statusText.style.color = '#c62828';
|
| 688 |
+
}
|
| 689 |
+
}
|
| 690 |
+
|
| 691 |
+
function escapeHtml(text) {
|
| 692 |
+
if (!text) return text;
|
| 693 |
+
return text
|
| 694 |
+
.replace(/&/g, "&")
|
| 695 |
+
.replace(/</g, "<")
|
| 696 |
+
.replace(/>/g, ">")
|
| 697 |
+
.replace(/"/g, """)
|
| 698 |
+
.replace(/'/g, "'");
|
| 699 |
+
}
|
| 700 |
+
</script>
|
| 701 |
</body>
|
| 702 |
</html>
|