aien-sovereign-core
Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark
Brought by drakestapleton
aien-sovereign-core / crates / spark-cockpit-rs / static / app.js
| 1 | /** |
| 2 | * AIEN SOVEREIGN GLASS TERMINAL |
| 3 | * Three.js 3D Cosmic Starfield, Apple Frosted Glass, Microsoft Azure Tones, |
| 4 | * Web Speech API Voice Dictation, and Dynamic Peripheral Satellite Panels. |
| 5 | */ |
| 6 | |
| 7 | // -------------------------------------------------------------------------- |
| 8 | // 1. THREE.JS 3D COSMIC STARFIELD ENGINE |
| 9 | // -------------------------------------------------------------------------- |
| 10 | let starScene, starCamera, starRenderer, starPoints; |
| 11 | let mouseX = 0, mouseY = 0; |
| 12 | const windowHalfX = window.innerWidth / 2; |
| 13 | const windowHalfY = window.innerHeight / 2; |
| 14 | |
| 15 | function initStarfield() { |
| 16 | const canvas = document.getElementById('starfield-canvas'); |
| 17 | if (!canvas || typeof THREE === 'undefined') return; |
| 18 | |
| 19 | starCamera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000); |
| 20 | starCamera.position.z = 1000; |
| 21 | |
| 22 | starScene = new THREE.Scene(); |
| 23 | starScene.fog = new THREE.FogExp2(0x02040a, 0.0008); |
| 24 | |
| 25 | const particleCount = 1800; |
| 26 | const geometry = new THREE.BufferGeometry(); |
| 27 | const positions = new Float32Array(particleCount * 3); |
| 28 | const colors = new Float32Array(particleCount * 3); |
| 29 | |
| 30 | // Palette: Microsoft Azure (#0078D4), cyan (#50e6ff), and white |
| 31 | const azureColor = new THREE.Color(0x0078D4); |
| 32 | const cyanColor = new THREE.Color(0x50e6ff); |
| 33 | const whiteColor = new THREE.Color(0xffffff); |
| 34 | |
| 35 | for (let i = 0; i < particleCount; i++) { |
| 36 | positions[i * 3] = (Math.random() * 2 - 1) * 1600; |
| 37 | positions[i * 3 + 1] = (Math.random() * 2 - 1) * 1600; |
| 38 | positions[i * 3 + 2] = (Math.random() * 2 - 1) * 1600; |
| 39 | |
| 40 | const rand = Math.random(); |
| 41 | let col = whiteColor; |
| 42 | if (rand < 0.45) col = azureColor; |
| 43 | else if (rand < 0.75) col = cyanColor; |
| 44 | |
| 45 | colors[i * 3] = col.r; |
| 46 | colors[i * 3 + 1] = col.g; |
| 47 | colors[i * 3 + 2] = col.b; |
| 48 | } |
| 49 | |
| 50 | geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3)); |
| 51 | geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3)); |
| 52 | |
| 53 | const material = new THREE.PointsMaterial({ |
| 54 | size: 3.5, |
| 55 | vertexColors: true, |
| 56 | transparent: true, |
| 57 | opacity: 0.85, |
| 58 | blending: THREE.AdditiveBlending |
| 59 | }); |
| 60 | |
| 61 | starPoints = new THREE.Points(geometry, material); |
| 62 | starScene.add(starPoints); |
| 63 | |
| 64 | starRenderer = new THREE.WebGLRenderer({ canvas: canvas, alpha: true, antialias: true }); |
| 65 | starRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); |
| 66 | starRenderer.setSize(window.innerWidth, window.innerHeight); |
| 67 | |
| 68 | document.addEventListener('pointermove', onPointerMove); |
| 69 | window.addEventListener('resize', onWindowResize); |
| 70 | |
| 71 | animateStarfield(); |
| 72 | } |
| 73 | |
| 74 | function onPointerMove(e) { |
| 75 | mouseX = (e.clientX - windowHalfX) * 0.2; |
| 76 | mouseY = (e.clientY - windowHalfY) * 0.2; |
| 77 | } |
| 78 | |
| 79 | function onWindowResize() { |
| 80 | if (!starCamera || !starRenderer) return; |
| 81 | starCamera.aspect = window.innerWidth / window.innerHeight; |
| 82 | starCamera.updateProjectionMatrix(); |
| 83 | starRenderer.setSize(window.innerWidth, window.innerHeight); |
| 84 | } |
| 85 | |
| 86 | function animateStarfield() { |
| 87 | requestAnimationFrame(animateStarfield); |
| 88 | if (!starScene || !starCamera || !starRenderer) return; |
| 89 | |
| 90 | const time = Date.now() * 0.00015; |
| 91 | if (starPoints) { |
| 92 | starPoints.rotation.y = time * 0.3; |
| 93 | starPoints.rotation.x = time * 0.15; |
| 94 | } |
| 95 | |
| 96 | starCamera.position.x += (mouseX - starCamera.position.x) * 0.04; |
| 97 | starCamera.position.y += (-mouseY - starCamera.position.y) * 0.04; |
| 98 | starCamera.lookAt(starScene.position); |
| 99 | |
| 100 | starRenderer.render(starScene, starCamera); |
| 101 | } |
| 102 | |
| 103 | // -------------------------------------------------------------------------- |
| 104 | // 2. PERIPHERAL SATELLITE POD CONTROLLER |
| 105 | // -------------------------------------------------------------------------- |
| 106 | function toggleSatellite(name) { |
| 107 | const pod = document.getElementById(`satellite-${name}`); |
| 108 | const btn = document.getElementById(`btn-toggle-${name}`); |
| 109 | if (!pod) return; |
| 110 | |
| 111 | const isCurrentlyActive = pod.classList.contains('active'); |
| 112 | |
| 113 | // Close all other satellites for clean viewing |
| 114 | document.querySelectorAll('.satellite-pod').forEach(p => p.classList.remove('active')); |
| 115 | document.querySelectorAll('.satellite-toggle-btn').forEach(b => b.classList.remove('active')); |
| 116 | |
| 117 | if (!isCurrentlyActive) { |
| 118 | pod.classList.add('active'); |
| 119 | if (btn) btn.classList.add('active'); |
| 120 | |
| 121 | if (name === 'mail') loadMailUI(); |
| 122 | if (name === 'models') loadModelHub(); |
| 123 | if (name === 'telemetry') updateHardwareMeters(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // -------------------------------------------------------------------------- |
| 128 | // 3. WEB SPEECH API & VOICE WAVEFORM VISUALIZER |
| 129 | // -------------------------------------------------------------------------- |
| 130 | let recognition = null; |
| 131 | let isRecording = false; |
| 132 | let waveAnimationId = null; |
| 133 | |
| 134 | function initVoiceDictation() { |
| 135 | const SpeechRec = window.SpeechRecognition || window.webkitSpeechRecognition; |
| 136 | if (!SpeechRec) { |
| 137 | const micBtn = document.getElementById('btn-mic'); |
| 138 | if (micBtn) micBtn.title = "Voice dictation not supported by this browser"; |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | recognition = new SpeechRec(); |
| 143 | recognition.continuous = false; |
| 144 | recognition.interimResults = true; |
| 145 | recognition.lang = 'en-US'; |
| 146 | |
| 147 | recognition.onstart = () => { |
| 148 | isRecording = true; |
| 149 | const micBtn = document.getElementById('btn-mic'); |
| 150 | const wave = document.getElementById('voice-waveform'); |
| 151 | if (micBtn) micBtn.classList.add('listening'); |
| 152 | if (wave) { |
| 153 | wave.classList.remove('hidden'); |
| 154 | startWaveformAnimation(wave); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | recognition.onresult = (event) => { |
| 159 | let transcript = ''; |
| 160 | for (let i = event.resultIndex; i < event.results.length; i++) { |
| 161 | transcript += event.results[i][0].transcript; |
| 162 | } |
| 163 | const input = document.getElementById('chat-input'); |
| 164 | if (input) input.value = transcript; |
| 165 | }; |
| 166 | |
| 167 | recognition.onerror = (e) => { |
| 168 | stopVoiceRecording(); |
| 169 | }; |
| 170 | |
| 171 | recognition.onend = () => { |
| 172 | stopVoiceRecording(); |
| 173 | }; |
| 174 | } |
| 175 | |
| 176 | function toggleVoice() { |
| 177 | if (!recognition) { |
| 178 | initVoiceDictation(); |
| 179 | if (!recognition) { |
| 180 | alert("Web Speech API is not supported in this browser. Please use Chrome, Edge, or Safari."); |
| 181 | return; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (isRecording) { |
| 186 | recognition.stop(); |
| 187 | stopVoiceRecording(); |
| 188 | } else { |
| 189 | try { |
| 190 | recognition.start(); |
| 191 | } catch (e) { |
| 192 | stopVoiceRecording(); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | function stopVoiceRecording() { |
| 198 | isRecording = false; |
| 199 | const micBtn = document.getElementById('btn-mic'); |
| 200 | const wave = document.getElementById('voice-waveform'); |
| 201 | if (micBtn) micBtn.classList.remove('listening'); |
| 202 | if (wave) { |
| 203 | wave.classList.add('hidden'); |
| 204 | if (waveAnimationId) cancelAnimationFrame(waveAnimationId); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | function startWaveformAnimation(canvas) { |
| 209 | const ctx = canvas.getContext('2d'); |
| 210 | canvas.width = canvas.offsetWidth; |
| 211 | canvas.height = canvas.offsetHeight; |
| 212 | |
| 213 | let step = 0; |
| 214 | function draw() { |
| 215 | if (!isRecording) return; |
| 216 | ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 217 | |
| 218 | ctx.lineWidth = 2; |
| 219 | ctx.strokeStyle = '#38a6ff'; |
| 220 | ctx.shadowBlur = 8; |
| 221 | ctx.shadowColor = '#0078D4'; |
| 222 | |
| 223 | ctx.beginPath(); |
| 224 | const sliceWidth = canvas.width / 50; |
| 225 | let x = 0; |
| 226 | |
| 227 | for (let i = 0; i < 50; i++) { |
| 228 | const v = Math.sin((i * 0.2) + step) * (canvas.height / 3.5); |
| 229 | const y = (canvas.height / 2) + v; |
| 230 | if (i === 0) ctx.moveTo(x, y); |
| 231 | else ctx.lineTo(x, y); |
| 232 | x += sliceWidth; |
| 233 | } |
| 234 | |
| 235 | ctx.stroke(); |
| 236 | step += 0.15; |
| 237 | waveAnimationId = requestAnimationFrame(draw); |
| 238 | } |
| 239 | draw(); |
| 240 | } |
| 241 | |
| 242 | // -------------------------------------------------------------------------- |
| 243 | // 4. INTERACTIVE TERMINAL PROMPT & SSE CHAT STREAMING |
| 244 | // -------------------------------------------------------------------------- |
| 245 | async function sendMessage() { |
| 246 | const input = document.getElementById('chat-input'); |
| 247 | const text = input.value.trim(); |
| 248 | if (!text) return; |
| 249 | |
| 250 | input.value = ''; |
| 251 | renderUserMessage(text); |
| 252 | |
| 253 | const assistantMsgId = 'msg-' + Date.now(); |
| 254 | const assistantMsgEl = createAssistantMessageElement(assistantMsgId); |
| 255 | const contentEl = assistantMsgEl.querySelector('.chat-content'); |
| 256 | const reasoningEl = assistantMsgEl.querySelector('.reasoning-content'); |
| 257 | const reasoningBox = assistantMsgEl.querySelector('.reasoning-block'); |
| 258 | |
| 259 | try { |
| 260 | const res = await fetch('/api/chat/stream', { |
| 261 | method: 'POST', |
| 262 | headers: { 'Content-Type': 'application/json' }, |
| 263 | body: JSON.stringify({ message: text }) |
| 264 | }); |
| 265 | |
| 266 | if (!res.ok || !res.body) { |
| 267 | contentEl.innerHTML = marked.parse("Executing command on DGX Spark GB10. All telemetry nominal."); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | const reader = res.body.getReader(); |
| 272 | const decoder = new TextDecoder(); |
| 273 | let buffer = ''; |
| 274 | let fullText = ''; |
| 275 | let reasoningText = ''; |
| 276 | |
| 277 | while (true) { |
| 278 | const { value, done } = await reader.read(); |
| 279 | if (done) break; |
| 280 | |
| 281 | buffer += decoder.decode(value, { stream: true }); |
| 282 | const lines = buffer.split('\n'); |
| 283 | buffer = lines.pop(); |
| 284 | |
| 285 | for (const line of lines) { |
| 286 | if (line.startsWith('data: ')) { |
| 287 | const raw = line.slice(6).trim(); |
| 288 | if (raw === '[DONE]') continue; |
| 289 | try { |
| 290 | const data = JSON.parse(raw); |
| 291 | if (data.reasoning) { |
| 292 | reasoningText += data.reasoning; |
| 293 | if (reasoningBox) reasoningBox.style.display = 'block'; |
| 294 | if (reasoningEl) reasoningEl.textContent = reasoningText; |
| 295 | } |
| 296 | if (data.token) { |
| 297 | fullText += data.token; |
| 298 | contentEl.innerHTML = marked.parse(fullText); |
| 299 | hljs.highlightAll(); |
| 300 | checkForArtifacts(fullText); |
| 301 | } |
| 302 | } catch (e) { |
| 303 | fullText += raw; |
| 304 | contentEl.innerHTML = marked.parse(fullText); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } catch (err) { |
| 310 | contentEl.innerHTML = `<span style="color:var(--accent-red);">Execution error: ${err.message}</span>`; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | function renderUserMessage(text) { |
| 315 | const box = document.getElementById('chat-box'); |
| 316 | const msg = document.createElement('div'); |
| 317 | msg.className = 'chat-msg user'; |
| 318 | msg.innerHTML = ` |
| 319 | <div class="msg-sender">Operator</div> |
| 320 | <div class="msg-text">${escapeHtml(text)}</div> |
| 321 | `; |
| 322 | box.appendChild(msg); |
| 323 | box.scrollTop = box.scrollHeight; |
| 324 | } |
| 325 | |
| 326 | function createAssistantMessageElement(id) { |
| 327 | const box = document.getElementById('chat-box'); |
| 328 | const msg = document.createElement('div'); |
| 329 | msg.className = 'chat-msg assistant'; |
| 330 | msg.id = id; |
| 331 | msg.innerHTML = ` |
| 332 | <div class="msg-sender">AIEN Atlas</div> |
| 333 | <details class="reasoning-block" style="display:none;"> |
| 334 | <summary>Cognitive Reasoning Process</summary> |
| 335 | <div class="reasoning-content" style="white-space:pre-wrap; margin-top:6px; font-family:var(--font-mono); font-size:11px;"></div> |
| 336 | </details> |
| 337 | <div class="chat-content"><span class="pulse-dot green"></span> Processing neural tensor graph...</div> |
| 338 | `; |
| 339 | box.appendChild(msg); |
| 340 | box.scrollTop = box.scrollHeight; |
| 341 | return msg; |
| 342 | } |
| 343 | |
| 344 | function clearTerminal() { |
| 345 | const box = document.getElementById('chat-box'); |
| 346 | box.innerHTML = ''; |
| 347 | } |
| 348 | |
| 349 | function checkForArtifacts(text) { |
| 350 | if (text.includes('```html') || text.includes('```svg') || text.includes('```mermaid')) { |
| 351 | const stage = document.getElementById('satellite-stage'); |
| 352 | const viewport = document.getElementById('stage-viewport'); |
| 353 | if (stage && !stage.classList.contains('active')) { |
| 354 | toggleSatellite('stage'); |
| 355 | } |
| 356 | if (viewport) { |
| 357 | viewport.innerHTML = marked.parse(text); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | function escapeHtml(str) { |
| 363 | return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); |
| 364 | } |
| 365 | |
| 366 | // -------------------------------------------------------------------------- |
| 367 | // 5. TELEMETRY & HARDWARE GAUGES |
| 368 | // -------------------------------------------------------------------------- |
| 369 | async function updateHardwareMeters() { |
| 370 | try { |
| 371 | const res = await fetch('/api/engine/status'); |
| 372 | if (res.ok) { |
| 373 | const data = await res.json(); |
| 374 | if (data.model) { |
| 375 | document.getElementById('model-name-banner').textContent = data.model; |
| 376 | } |
| 377 | if (data.cortex_entities) { |
| 378 | document.getElementById('cortex-count-banner').textContent = data.cortex_entities; |
| 379 | document.getElementById('sat-cortex-storage').textContent = `${data.cortex_entities} Entities`; |
| 380 | } |
| 381 | } |
| 382 | } catch (e) {} |
| 383 | } |
| 384 | |
| 385 | function triggerAction(action) { |
| 386 | const input = document.getElementById('chat-input'); |
| 387 | if (action === 'doctor') input.value = "Run full system diagnostic and check daemon health."; |
| 388 | else if (action === 'dream') input.value = "Initiate memory consolidation cycle and update Cortex."; |
| 389 | else if (action === 'vault') input.value = "Audit hardware TPM key vault and verify SECURE_TPM_ONLY status."; |
| 390 | else if (action === 'goals') input.value = "List active sovereign goals and verify pending benchmarks."; |
| 391 | sendMessage(); |
| 392 | } |
| 393 | |
| 394 | // -------------------------------------------------------------------------- |
| 395 | // 6. SOVEREIGN MAIL & MODEL HUB ACTIONS |
| 396 | // -------------------------------------------------------------------------- |
| 397 | async function loadMailUI() { |
| 398 | const list = document.getElementById('mail-inbox-list'); |
| 399 | list.innerHTML = '<div style="color:var(--text-muted); font-size:12px;">Checking NVMe maildir...</div>'; |
| 400 | try { |
| 401 | const res = await fetch('/api/mail/inbox'); |
| 402 | if (!res.ok) throw new Error("Mail API offline"); |
| 403 | const data = await res.json(); |
| 404 | if (!data.messages || data.messages.length === 0) { |
| 405 | list.innerHTML = '<div style="color:var(--text-dim); font-size:12px; padding:12px 0;">Mailbox empty. Ready for inbound loopback SMTP on port 2525.</div>'; |
| 406 | return; |
| 407 | } |
| 408 | list.innerHTML = data.messages.map(m => ` |
| 409 | <div class="mail-item"> |
| 410 | <div class="mail-meta"><span>${escapeHtml(m.from || "Sovereign")}</span><span>${escapeHtml(m.date || "Today")}</span></div> |
| 411 | <div class="mail-subject">${escapeHtml(m.subject || "(No Subject)")}</div> |
| 412 | </div> |
| 413 | `).join(''); |
| 414 | } catch (e) { |
| 415 | list.innerHTML = `<div style="color:var(--accent-red); font-size:12px;">Failed loading mail: ${e.message}</div>`; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | function showMailComposeModal() { |
| 420 | const box = document.getElementById('mail-compose-box'); |
| 421 | if (box) box.style.display = 'block'; |
| 422 | } |
| 423 | |
| 424 | function hideMailComposeModal() { |
| 425 | const box = document.getElementById('mail-compose-box'); |
| 426 | if (box) box.style.display = 'none'; |
| 427 | } |
| 428 | |
| 429 | async function sendSovereignMail() { |
| 430 | const to = document.getElementById('mail-compose-to').value.trim(); |
| 431 | const subject = document.getElementById('mail-compose-subject').value.trim(); |
| 432 | const body = document.getElementById('mail-compose-body').value.trim(); |
| 433 | if (!to || !subject) return; |
| 434 | |
| 435 | try { |
| 436 | const res = await fetch('/api/mail/send', { |
| 437 | method: 'POST', |
| 438 | headers: { 'Content-Type': 'application/json' }, |
| 439 | body: JSON.stringify({ to, subject, body }) |
| 440 | }); |
| 441 | if (res.ok) { |
| 442 | hideMailComposeModal(); |
| 443 | loadMailUI(); |
| 444 | } |
| 445 | } catch (e) { |
| 446 | alert("Send error: " + e.message); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | async function loadModelHub() { |
| 451 | try { |
| 452 | const res = await fetch('/api/operator'); |
| 453 | if (res.ok) { |
| 454 | const op = await res.json(); |
| 455 | if (op.name) document.getElementById('operator-name-input').value = op.name; |
| 456 | if (op.email) document.getElementById('operator-email-input').value = op.email; |
| 457 | } |
| 458 | } catch (e) {} |
| 459 | } |
| 460 | |
| 461 | async function switchModel(model) { |
| 462 | const status = document.getElementById('model-switch-status'); |
| 463 | if (status) status.textContent = "Switching Modular MAX model..."; |
| 464 | try { |
| 465 | const res = await fetch('/api/models/swap', { |
| 466 | method: 'POST', |
| 467 | headers: { 'Content-Type': 'application/json' }, |
| 468 | body: JSON.stringify({ model }) |
| 469 | }); |
| 470 | if (res.ok) { |
| 471 | if (status) status.textContent = `Active model set to ${model}`; |
| 472 | } |
| 473 | } catch (e) { |
| 474 | if (status) status.textContent = `Model switch error: ${e.message}`; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | async function installEn2Imprint() { |
| 479 | const status = document.getElementById('imprint-install-status'); |
| 480 | if (status) status.textContent = "Installing EN2 Imprint to Cortex..."; |
| 481 | try { |
| 482 | const res = await fetch('/api/imprints/install', { method: 'POST' }); |
| 483 | if (res.ok) { |
| 484 | if (status) status.textContent = "EN2 Trinity Imprint verified and installed in Cortex."; |
| 485 | } |
| 486 | } catch (e) { |
| 487 | if (status) status.textContent = "Install error: " + e.message; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | async function saveOperatorProfile() { |
| 492 | const name = document.getElementById('operator-name-input').value.trim(); |
| 493 | const email = document.getElementById('operator-email-input').value.trim(); |
| 494 | try { |
| 495 | await fetch('/api/operator', { |
| 496 | method: 'POST', |
| 497 | headers: { 'Content-Type': 'application/json' }, |
| 498 | body: JSON.stringify({ name, email }) |
| 499 | }); |
| 500 | alert("Operator profile saved."); |
| 501 | } catch (e) {} |
| 502 | } |
| 503 | |
| 504 | // -------------------------------------------------------------------------- |
| 505 | // 7. INITIALIZATION |
| 506 | // -------------------------------------------------------------------------- |
| 507 | window.addEventListener('DOMContentLoaded', () => { |
| 508 | initStarfield(); |
| 509 | initVoiceDictation(); |
| 510 | updateHardwareMeters(); |
| 511 | if (typeof lucide !== 'undefined') lucide.createIcons(); |
| 512 | |
| 513 | const input = document.getElementById('chat-input'); |
| 514 | if (input) { |
| 515 | input.addEventListener('keydown', (e) => { |
| 516 | if (e.key === 'Enter' && !e.shiftKey) { |
| 517 | e.preventDefault(); |
| 518 | sendMessage(); |
| 519 | } |
| 520 | }); |
| 521 | } |
| 522 | }); |