aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-cockpit-rs / static / app.js
| 1 | // ========================================================================= // |
| 2 | // ALLEN HARNESS FRONT END (SPARK COCKPIT) // |
| 3 | // Every figure on the page is read from the server; nothing is typed in. // |
| 4 | // ========================================================================= // |
| 5 | |
| 6 | let currentThreadId = "main"; |
| 7 | let activeView = "chat"; |
| 8 | let isStreaming = false; |
| 9 | let termWs = null; |
| 10 | let activeArtifact = null; |
| 11 | |
| 12 | // Initialize on DOMContentLoaded |
| 13 | document.addEventListener("DOMContentLoaded", () => { |
| 14 | initTheme(); |
| 15 | initNavigation(); |
| 16 | initChat(); |
| 17 | initApprovals(); |
| 18 | initTelemetry(); |
| 19 | initLive(); |
| 20 | initDrawer(); |
| 21 | initGoals(); |
| 22 | initMemory(); |
| 23 | }); |
| 24 | |
| 25 | // ========================================================================= // |
| 26 | // THEME & NAVIGATION // |
| 27 | // ========================================================================= // |
| 28 | function initTheme() { |
| 29 | // A link can choose the theme (?theme=light); otherwise the last choice on this browser, else dark. |
| 30 | const fromLink = new URLSearchParams(window.location.search).get("theme"); |
| 31 | const savedTheme = fromLink || localStorage.getItem("allen_theme") || localStorage.getItem("muse_theme") || "dark"; |
| 32 | document.body.className = savedTheme === "light" ? "light-theme" : "dark-theme"; |
| 33 | |
| 34 | const btn = document.getElementById("btn-theme-toggle"); |
| 35 | if (btn) { |
| 36 | btn.addEventListener("click", () => { |
| 37 | const isLight = document.body.classList.contains("light-theme"); |
| 38 | const nextTheme = isLight ? "dark" : "light"; |
| 39 | document.body.className = nextTheme + "-theme"; |
| 40 | localStorage.setItem("allen_theme", nextTheme); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | // Wide screens: the menu button narrows the rail to icons. Phones (under 820 px): the rail |
| 45 | // sits off canvas and the same button slides it in over a backdrop. |
| 46 | const sidebarToggle = document.getElementById("btn-sidebar-toggle"); |
| 47 | const sidebar = document.getElementById("muse-sidebar"); |
| 48 | const backdrop = document.getElementById("sidebar-backdrop"); |
| 49 | const phone = window.matchMedia("(max-width: 820px)"); |
| 50 | const closeRail = () => { |
| 51 | sidebar.classList.remove("open"); |
| 52 | if (backdrop) backdrop.classList.remove("show"); |
| 53 | }; |
| 54 | if (sidebarToggle && sidebar) { |
| 55 | sidebarToggle.addEventListener("click", () => { |
| 56 | if (phone.matches) { |
| 57 | const open = sidebar.classList.toggle("open"); |
| 58 | if (backdrop) backdrop.classList.toggle("show", open); |
| 59 | } else { |
| 60 | sidebar.classList.toggle("collapsed"); |
| 61 | } |
| 62 | }); |
| 63 | if (backdrop) backdrop.addEventListener("click", closeRail); |
| 64 | sidebar.querySelectorAll(".nav-item, .thread-item").forEach(el => { |
| 65 | el.addEventListener("click", () => { if (phone.matches) closeRail(); }); |
| 66 | }); |
| 67 | phone.addEventListener("change", closeRail); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | function initNavigation() { |
| 72 | const navItems = document.querySelectorAll(".nav-item"); |
| 73 | navItems.forEach(item => { |
| 74 | item.addEventListener("click", () => { |
| 75 | const viewName = item.getAttribute("data-view"); |
| 76 | switchView(viewName); |
| 77 | }); |
| 78 | }); |
| 79 | |
| 80 | const btnNewChat = document.getElementById("btn-new-chat"); |
| 81 | if (btnNewChat) { |
| 82 | btnNewChat.addEventListener("click", () => { |
| 83 | switchView("chat"); |
| 84 | clearChat(); |
| 85 | }); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | function switchView(viewName) { |
| 90 | activeView = viewName; |
| 91 | document.querySelectorAll(".nav-item").forEach(el => { |
| 92 | el.classList.toggle("active", el.getAttribute("data-view") === viewName); |
| 93 | }); |
| 94 | |
| 95 | document.querySelectorAll(".muse-view").forEach(el => { |
| 96 | el.classList.toggle("active", el.id === `view-${viewName}`); |
| 97 | }); |
| 98 | |
| 99 | if (viewName === "feed") loadFeed(); |
| 100 | if (viewName === "goals") loadGoals(); |
| 101 | if (viewName === "artifacts") loadArtifacts(); |
| 102 | if (viewName === "memory") loadMemory(); |
| 103 | if (viewName === "terminal") initTerminal(); |
| 104 | } |
| 105 | |
| 106 | // ========================================================================= // |
| 107 | // REAL-TIME TELEMETRY // |
| 108 | // ========================================================================= // |
| 109 | async function initTelemetry() { |
| 110 | async function fetchTelemetry() { |
| 111 | try { |
| 112 | const res = await fetch("/api/telemetry/live"); |
| 113 | if (!res.ok) return; |
| 114 | const data = await res.json(); |
| 115 | |
| 116 | // /api/telemetry/live answers {gpu: {temperature_c}, memory: {used_kb, total_kb}}. |
| 117 | // The pills stay hidden until a real reading arrives. |
| 118 | const temp = data.gpu && data.gpu.temperature_c; |
| 119 | const usedKb = data.memory && data.memory.used_kb; |
| 120 | const totalKb = data.memory && data.memory.total_kb; |
| 121 | let shown = false; |
| 122 | |
| 123 | const gpuTempEl = document.getElementById("gpu-temp"); |
| 124 | const gpuPill = document.getElementById("pill-gpu"); |
| 125 | if (gpuTempEl && typeof temp === "number" && temp > 0) { |
| 126 | gpuTempEl.textContent = `GB10 ${Math.round(temp)}°C`; |
| 127 | if (gpuPill) gpuPill.hidden = false; |
| 128 | shown = true; |
| 129 | } else if (gpuPill) { |
| 130 | gpuPill.hidden = true; |
| 131 | } |
| 132 | |
| 133 | const ramUsedEl = document.getElementById("ram-used"); |
| 134 | const ramPill = document.getElementById("pill-ram"); |
| 135 | if (ramUsedEl && typeof usedKb === "number" && typeof totalKb === "number" && totalKb > 0) { |
| 136 | const usedGb = usedKb / 1048576; |
| 137 | const totalGb = Math.round(totalKb / 1048576); |
| 138 | ramUsedEl.textContent = `${usedGb.toFixed(1)} of ${totalGb} GB`; |
| 139 | if (ramPill) ramPill.hidden = false; |
| 140 | shown = true; |
| 141 | } else if (ramPill) { |
| 142 | ramPill.hidden = true; |
| 143 | } |
| 144 | |
| 145 | const pills = document.getElementById("system-pills"); |
| 146 | if (pills) pills.hidden = !shown; |
| 147 | } catch (_) {} |
| 148 | } |
| 149 | |
| 150 | fetchTelemetry(); |
| 151 | setInterval(fetchTelemetry, 3000); |
| 152 | } |
| 153 | |
| 154 | // ========================================================================= // |
| 155 | // LIVE STATE ON THE WELCOME (seat, approvals, goals, Spark judge) // |
| 156 | // ========================================================================= // |
| 157 | function setText(id, text) { |
| 158 | const el = document.getElementById(id); |
| 159 | if (el) el.textContent = text; |
| 160 | } |
| 161 | |
| 162 | // Every API or model string placed into innerHTML goes through esc(). |
| 163 | function esc(value) { |
| 164 | return String(value ?? "") |
| 165 | .replace(/&/g, "&") |
| 166 | .replace(/</g, "<") |
| 167 | .replace(/>/g, ">") |
| 168 | .replace(/"/g, """) |
| 169 | .replace(/'/g, "'"); |
| 170 | } |
| 171 | |
| 172 | function greetingForHour(hour) { |
| 173 | if (hour < 5) return "Good night"; |
| 174 | if (hour < 12) return "Good morning"; |
| 175 | if (hour < 18) return "Good afternoon"; |
| 176 | return "Good evening"; |
| 177 | } |
| 178 | |
| 179 | async function readJson(url) { |
| 180 | const res = await fetch(url); |
| 181 | if (!res.ok) throw new Error(`${url}: ${res.status}`); |
| 182 | return res.json(); |
| 183 | } |
| 184 | |
| 185 | async function refreshSeat() { |
| 186 | const badge = document.getElementById("seat-model-badge"); |
| 187 | try { |
| 188 | const data = await readJson("/api/models"); |
| 189 | const first = (data.active_models || [])[0] || {}; |
| 190 | const online = first.status === "ONLINE" && first.id; |
| 191 | const text = online ? first.id : "seat offline"; |
| 192 | if (badge) { |
| 193 | badge.textContent = text; |
| 194 | badge.classList.toggle("is-offline", !online); |
| 195 | } |
| 196 | setText("live-seat", text); |
| 197 | const card = document.getElementById("live-card-seat"); |
| 198 | if (card) card.classList.toggle("is-bad", !online); |
| 199 | } catch (_) { |
| 200 | if (badge) badge.textContent = "seat unknown"; |
| 201 | setText("live-seat", "not readable"); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | async function refreshCounts() { |
| 206 | try { |
| 207 | const data = await readJson("/api/approvals"); |
| 208 | const pending = (data.approvals || []).filter(a => a.status === "pending").length; |
| 209 | setText("live-approvals", String(pending)); |
| 210 | const card = document.getElementById("live-card-approvals"); |
| 211 | if (card) card.classList.toggle("is-attention", pending > 0); |
| 212 | } catch (_) { |
| 213 | setText("live-approvals", "not readable"); |
| 214 | } |
| 215 | try { |
| 216 | const data = await readJson("/api/goals"); |
| 217 | setText("live-goals", String((data.goals || []).length)); |
| 218 | } catch (_) { |
| 219 | setText("live-goals", "not readable"); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | function judgeWord(v) { |
| 224 | if (v === "PASS") return "PASS"; |
| 225 | if (v === "FAIL") return "FAIL"; |
| 226 | if (!v || v === "NO VERDICT") return "NO VERDICT"; |
| 227 | return "BLOCKED"; |
| 228 | } |
| 229 | |
| 230 | async function refreshJudge() { |
| 231 | const value = document.getElementById("live-judge"); |
| 232 | const note = document.getElementById("live-judge-note"); |
| 233 | const card = document.getElementById("live-card-judge"); |
| 234 | try { |
| 235 | const j = await readJson("/api/judge"); |
| 236 | const word = judgeWord(j.verdict); |
| 237 | if (value) { |
| 238 | value.textContent = word; |
| 239 | value.className = `live-value verdict verdict-${word.toLowerCase().replace(" ", "-")}`; |
| 240 | } |
| 241 | if (card) card.classList.toggle("is-bad", word === "FAIL"); |
| 242 | if (note) { |
| 243 | if (j.commit) { |
| 244 | const when = (j.finished_utc || "").replace("T", " ").replace("Z", " UTC"); |
| 245 | note.textContent = `commit ${j.commit.slice(0, 7)}: ${j.checks_pass} of ${j.checks_total} checks PASS, ${j.checks_fail} FAIL, ${j.checks_blocked} blocked. Judged ${when}.`; |
| 246 | } else { |
| 247 | note.textContent = j.note || "No Spark judge bundle could be read on this machine."; |
| 248 | } |
| 249 | } |
| 250 | } catch (_) { |
| 251 | if (value) value.textContent = "not readable"; |
| 252 | if (note) note.textContent = "The judge's bundle store could not be read."; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | function initLive() { |
| 257 | setText("hero-greeting", `${greetingForHour(new Date().getHours())}. ALLEN is ready.`); |
| 258 | refreshSeat(); |
| 259 | refreshCounts(); |
| 260 | refreshJudge(); |
| 261 | setInterval(refreshSeat, 15000); |
| 262 | setInterval(refreshCounts, 15000); |
| 263 | setInterval(refreshJudge, 60000); |
| 264 | } |
| 265 | |
| 266 | // ========================================================================= // |
| 267 | // CHAT & STREAMING (SSE) // |
| 268 | // ========================================================================= // |
| 269 | function initChat() { |
| 270 | const input = document.getElementById("chat-input"); |
| 271 | const btnSend = document.getElementById("btn-send"); |
| 272 | const btnClear = document.getElementById("btn-clear-chat"); |
| 273 | |
| 274 | if (input) { |
| 275 | input.addEventListener("keydown", (e) => { |
| 276 | if (e.key === "Enter" && !e.shiftKey) { |
| 277 | e.preventDefault(); |
| 278 | sendMessage(); |
| 279 | } |
| 280 | }); |
| 281 | |
| 282 | input.addEventListener("input", () => { |
| 283 | input.style.height = "auto"; |
| 284 | input.style.height = Math.min(input.scrollHeight, 160) + "px"; |
| 285 | }); |
| 286 | } |
| 287 | |
| 288 | if (btnSend) { |
| 289 | btnSend.addEventListener("click", () => sendMessage()); |
| 290 | } |
| 291 | |
| 292 | if (btnClear) { |
| 293 | btnClear.addEventListener("click", () => clearChat()); |
| 294 | } |
| 295 | |
| 296 | // Suggestion chips in hero |
| 297 | document.querySelectorAll(".suggestion-chip").forEach(chip => { |
| 298 | chip.addEventListener("click", () => { |
| 299 | const prompt = chip.getAttribute("data-prompt"); |
| 300 | if (input && prompt) { |
| 301 | input.value = prompt; |
| 302 | sendMessage(); |
| 303 | } |
| 304 | }); |
| 305 | }); |
| 306 | } |
| 307 | |
| 308 | function clearChat() { |
| 309 | const list = document.getElementById("messages-list"); |
| 310 | if (list) list.innerHTML = ""; |
| 311 | const hero = document.getElementById("chat-hero"); |
| 312 | if (hero) hero.style.display = "flex"; |
| 313 | setAgentStatus("Ready"); |
| 314 | } |
| 315 | |
| 316 | function setAgentStatus(statusText) { |
| 317 | const phrase = document.getElementById("status-phrase"); |
| 318 | if (phrase) phrase.textContent = statusText; |
| 319 | } |
| 320 | |
| 321 | async function sendMessage() { |
| 322 | const input = document.getElementById("chat-input"); |
| 323 | if (!input || isStreaming) return; |
| 324 | const text = input.value.trim(); |
| 325 | if (!text) return; |
| 326 | |
| 327 | // Hide hero |
| 328 | const hero = document.getElementById("chat-hero"); |
| 329 | if (hero) hero.style.display = "none"; |
| 330 | |
| 331 | // Append user bubble |
| 332 | appendUserMessage(text); |
| 333 | input.value = ""; |
| 334 | input.style.height = "auto"; |
| 335 | |
| 336 | // Append agent placeholder |
| 337 | const { row, bubble, thoughtContent, textContent } = createAgentMessagePlaceholder(); |
| 338 | isStreaming = true; |
| 339 | setAgentStatus("Reasoning..."); |
| 340 | |
| 341 | try { |
| 342 | const res = await fetch("/api/chat/stream", { |
| 343 | method: "POST", |
| 344 | headers: { "Content-Type": "application/json" }, |
| 345 | body: JSON.stringify({ |
| 346 | model: "atlas-lightning-omni", |
| 347 | messages: [{ role: "user", content: text }] |
| 348 | }) |
| 349 | }); |
| 350 | |
| 351 | if (!res.ok) { |
| 352 | textContent.textContent = `Error connecting to model seat: HTTP ${res.status}`; |
| 353 | isStreaming = false; |
| 354 | setAgentStatus("Ready"); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | const reader = res.body.getReader(); |
| 359 | const decoder = new TextDecoder(); |
| 360 | let accumulatedContent = ""; |
| 361 | let accumulatedThought = ""; |
| 362 | let buffer = ""; |
| 363 | |
| 364 | while (true) { |
| 365 | const { value, done } = await reader.read(); |
| 366 | if (done) break; |
| 367 | |
| 368 | buffer += decoder.decode(value, { stream: true }); |
| 369 | const lines = buffer.split("\n"); |
| 370 | buffer = lines.pop() || ""; |
| 371 | |
| 372 | for (const line of lines) { |
| 373 | const trimmed = line.trim(); |
| 374 | if (!trimmed.startsWith("data:")) continue; |
| 375 | const dataStr = trimmed.slice(5).trim(); |
| 376 | if (dataStr === "[DONE]") break; |
| 377 | |
| 378 | try { |
| 379 | const parsed = JSON.parse(dataStr); |
| 380 | if (parsed.reasoning) { |
| 381 | accumulatedThought += parsed.reasoning; |
| 382 | thoughtContent.textContent = accumulatedThought; |
| 383 | const thoughtBox = row.querySelector(".thought-box"); |
| 384 | if (thoughtBox) thoughtBox.style.display = "block"; |
| 385 | } |
| 386 | if (parsed.content) { |
| 387 | accumulatedContent += parsed.content; |
| 388 | renderMarkdown(textContent, accumulatedContent); |
| 389 | } |
| 390 | } catch (_) {} |
| 391 | } |
| 392 | scrollToBottom(); |
| 393 | } |
| 394 | } catch (err) { |
| 395 | textContent.textContent = `Network exception during stream: ${err.message}`; |
| 396 | } finally { |
| 397 | isStreaming = false; |
| 398 | setAgentStatus("Ready"); |
| 399 | scrollToBottom(); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | function appendUserMessage(text) { |
| 404 | const list = document.getElementById("messages-list"); |
| 405 | if (!list) return; |
| 406 | |
| 407 | const row = document.createElement("div"); |
| 408 | row.className = "message-row user-row"; |
| 409 | |
| 410 | const bubble = document.createElement("div"); |
| 411 | bubble.className = "message-bubble"; |
| 412 | bubble.textContent = text; |
| 413 | |
| 414 | row.appendChild(bubble); |
| 415 | list.appendChild(row); |
| 416 | scrollToBottom(); |
| 417 | } |
| 418 | |
| 419 | function createAgentMessagePlaceholder() { |
| 420 | const list = document.getElementById("messages-list"); |
| 421 | const row = document.createElement("div"); |
| 422 | row.className = "message-row agent-row"; |
| 423 | |
| 424 | const avatar = document.createElement("img"); |
| 425 | avatar.className = "msg-avatar"; |
| 426 | avatar.src = "/avatar.jpg"; |
| 427 | avatar.onerror = () => { avatar.src = "/icon-192.png"; }; |
| 428 | |
| 429 | const bubble = document.createElement("div"); |
| 430 | bubble.className = "message-bubble"; |
| 431 | |
| 432 | const thoughtBox = document.createElement("div"); |
| 433 | thoughtBox.className = "thought-box"; |
| 434 | thoughtBox.style.display = "none"; |
| 435 | thoughtBox.innerHTML = ` |
| 436 | <div class="thought-header"> |
| 437 | <span>⚡ Reasoning Process</span> |
| 438 | <span>▼</span> |
| 439 | </div> |
| 440 | <div class="thought-content"></div> |
| 441 | `; |
| 442 | const thoughtHeader = thoughtBox.querySelector(".thought-header"); |
| 443 | const thoughtContent = thoughtBox.querySelector(".thought-content"); |
| 444 | thoughtHeader.addEventListener("click", () => { |
| 445 | const isVisible = thoughtContent.style.display !== "none"; |
| 446 | thoughtContent.style.display = isVisible ? "none" : "block"; |
| 447 | thoughtHeader.querySelector("span:last-child").textContent = isVisible ? "▶" : "▼"; |
| 448 | }); |
| 449 | |
| 450 | const textContent = document.createElement("div"); |
| 451 | textContent.className = "agent-markdown-body"; |
| 452 | textContent.textContent = "Thinking..."; |
| 453 | |
| 454 | bubble.appendChild(thoughtBox); |
| 455 | bubble.appendChild(textContent); |
| 456 | row.appendChild(avatar); |
| 457 | row.appendChild(bubble); |
| 458 | list.appendChild(row); |
| 459 | |
| 460 | scrollToBottom(); |
| 461 | return { row, bubble, thoughtContent, textContent }; |
| 462 | } |
| 463 | |
| 464 | function renderMarkdown(container, rawText) { |
| 465 | // Simple, high-speed compiled markdown formatter without external runtime bloat |
| 466 | let formatted = rawText |
| 467 | .replace(/&/g, "&") |
| 468 | .replace(/</g, "<") |
| 469 | .replace(/>/g, ">"); |
| 470 | |
| 471 | // Code blocks: ```lang ... ``` |
| 472 | formatted = formatted.replace(/```([a-zA-Z0-9_-]*)\n([\s\S]*?)```/g, (match, lang, code) => { |
| 473 | return `<pre class="code-canvas"><code class="language-${lang}">${code}</code></pre>`; |
| 474 | }); |
| 475 | |
| 476 | // Inline code: `code` |
| 477 | formatted = formatted.replace(/`([^`]+)`/g, "<code>$1</code>"); |
| 478 | |
| 479 | // Bold: **text** |
| 480 | formatted = formatted.replace(/\*\*([^\*]+)\*\*/g, "<strong>$1</strong>"); |
| 481 | |
| 482 | // Headers: ### Head |
| 483 | formatted = formatted.replace(/^### (.*$)/gim, "<h3>$1</h3>"); |
| 484 | formatted = formatted.replace(/^## (.*$)/gim, "<h2>$1</h2>"); |
| 485 | formatted = formatted.replace(/^# (.*$)/gim, "<h1>$1</h1>"); |
| 486 | |
| 487 | // Lists: - item |
| 488 | formatted = formatted.replace(/^\s*-\s*(.*$)/gim, "<li>$1</li>"); |
| 489 | formatted = formatted.replace(/(<li>.*<\/li>)/s, "<ul>$1</ul>"); |
| 490 | |
| 491 | // Paragraphs |
| 492 | formatted = formatted.replace(/\n\n/g, "<br><br>"); |
| 493 | |
| 494 | container.innerHTML = formatted; |
| 495 | } |
| 496 | |
| 497 | function scrollToBottom() { |
| 498 | const area = document.getElementById("chat-scroll-area"); |
| 499 | if (area) { |
| 500 | area.scrollTop = area.scrollHeight; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // ========================================================================= // |
| 505 | // PROACTIVE FEED VIEW // |
| 506 | // ========================================================================= // |
| 507 | async function loadFeed() { |
| 508 | const grid = document.getElementById("feed-grid"); |
| 509 | if (!grid) return; |
| 510 | grid.innerHTML = "<div style='color:var(--text-muted);'>Loading proactive feed updates...</div>"; |
| 511 | |
| 512 | try { |
| 513 | const res = await fetch("/api/feed"); |
| 514 | if (!res.ok) throw new Error("Feed request failed"); |
| 515 | const data = await res.json(); |
| 516 | grid.innerHTML = ""; |
| 517 | |
| 518 | (data.feed || []).forEach(item => { |
| 519 | const card = document.createElement("div"); |
| 520 | card.className = "feed-card"; |
| 521 | card.innerHTML = ` |
| 522 | <div> |
| 523 | <div class="feed-card-header"> |
| 524 | <span class="feed-card-badge ${esc(item.badge.toLowerCase())}">${esc(item.badge)}</span> |
| 525 | <span style="font-size:11px; color:var(--text-muted);">${esc(item.category.toUpperCase())}</span> |
| 526 | </div> |
| 527 | <h3 class="feed-card-title" style="margin-top:8px;">${esc(item.title)}</h3> |
| 528 | <p class="feed-card-summary" style="margin-top:6px;">${esc(item.summary)}</p> |
| 529 | </div> |
| 530 | <div style="display:flex; justify-content:flex-end;"> |
| 531 | <button class="stage-btn" data-target="${esc(item.action_target)}">${esc(item.action_label)}</button> |
| 532 | </div> |
| 533 | `; |
| 534 | const btn = card.querySelector("button"); |
| 535 | btn.addEventListener("click", () => { |
| 536 | switchView(item.action_target); |
| 537 | }); |
| 538 | grid.appendChild(card); |
| 539 | }); |
| 540 | } catch (err) { |
| 541 | grid.innerHTML = `<div style="color:var(--status-crimson);">Failed to load feed: ${esc(err.message)}</div>`; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // ========================================================================= // |
| 546 | // GOALS DECK // |
| 547 | // ========================================================================= // |
| 548 | function initGoals() { |
| 549 | const btnNewGoal = document.getElementById("btn-open-new-goal-modal"); |
| 550 | const modal = document.getElementById("modal-new-goal"); |
| 551 | const btnSubmit = document.getElementById("btn-submit-goal"); |
| 552 | |
| 553 | if (btnNewGoal && modal) { |
| 554 | btnNewGoal.addEventListener("click", () => { |
| 555 | modal.style.display = "flex"; |
| 556 | }); |
| 557 | } |
| 558 | |
| 559 | if (btnSubmit) { |
| 560 | btnSubmit.addEventListener("click", async () => { |
| 561 | const titleInput = document.getElementById("goal-input-title"); |
| 562 | const msInput = document.getElementById("goal-input-milestones"); |
| 563 | const title = titleInput.value.trim(); |
| 564 | if (!title) return; |
| 565 | |
| 566 | const milestones = msInput.value |
| 567 | .split(",") |
| 568 | .map(s => s.trim()) |
| 569 | .filter(s => s.length > 0); |
| 570 | |
| 571 | try { |
| 572 | await fetch("/api/goals", { |
| 573 | method: "POST", |
| 574 | headers: { "Content-Type": "application/json" }, |
| 575 | body: JSON.stringify({ title, milestones }) |
| 576 | }); |
| 577 | modal.style.display = "none"; |
| 578 | titleInput.value = ""; |
| 579 | msInput.value = ""; |
| 580 | loadGoals(); |
| 581 | } catch (err) { |
| 582 | alert("Failed to create goal: " + err.message); |
| 583 | } |
| 584 | }); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | async function loadGoals() { |
| 589 | const container = document.getElementById("goals-container"); |
| 590 | if (!container) return; |
| 591 | container.innerHTML = "<div style='color:var(--text-muted);'>Retrieving objectives...</div>"; |
| 592 | |
| 593 | try { |
| 594 | const res = await fetch("/api/goals"); |
| 595 | if (!res.ok) throw new Error("Failed to load goals"); |
| 596 | const data = await res.json(); |
| 597 | container.innerHTML = ""; |
| 598 | |
| 599 | const goals = data.goals || []; |
| 600 | const badge = document.getElementById("goals-badge"); |
| 601 | if (badge) badge.textContent = goals.length; |
| 602 | |
| 603 | if (goals.length === 0) { |
| 604 | container.innerHTML = ` |
| 605 | <div class="goal-card" style="grid-column: 1 / -1; text-align:center; padding: 40px;"> |
| 606 | <h3 style="font-size:16px; margin-bottom:8px;">No Active Objectives Tracked</h3> |
| 607 | <p style="color:var(--text-secondary); margin-bottom:16px;">Deploy your first autonomous engineering or systems objective.</p> |
| 608 | <div><button class="stage-btn primary" onclick="document.getElementById('modal-new-goal').style.display='flex'">+ New Goal</button></div> |
| 609 | </div> |
| 610 | `; |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | goals.forEach(goal => { |
| 615 | const card = document.createElement("div"); |
| 616 | card.className = "goal-card"; |
| 617 | |
| 618 | const milestones = goal.milestones || []; |
| 619 | const completedCount = milestones.filter(m => m.completed).length; |
| 620 | const pct = milestones.length > 0 ? Math.round((completedCount / milestones.length) * 100) : 0; |
| 621 | |
| 622 | card.innerHTML = ` |
| 623 | <div class="goal-card-top"> |
| 624 | <span class="goal-title">${esc(goal.title)}</span> |
| 625 | <span class="feed-card-badge ${pct === 100 ? "online" : "pending"}">${pct}%</span> |
| 626 | </div> |
| 627 | <div class="goal-progress-wrap"> |
| 628 | <div class="goal-progress-bar-track"> |
| 629 | <div class="goal-progress-bar-fill" style="width: ${pct}%;"></div> |
| 630 | </div> |
| 631 | <div style="font-size:11px; color:var(--text-muted); display:flex; justify-content:space-between;"> |
| 632 | <span>${completedCount} of ${milestones.length} milestones complete</span> |
| 633 | </div> |
| 634 | </div> |
| 635 | <div class="goal-milestones-list"> |
| 636 | ${milestones.map(m => ` |
| 637 | <label class="milestone-row"> |
| 638 | <input type="checkbox" class="milestone-check" ${m.completed ? "checked" : ""}> |
| 639 | <span style="${m.completed ? "text-decoration:line-through; opacity:0.6;" : ""}">${esc(m.name || m.title || m)}</span> |
| 640 | </label> |
| 641 | `).join("")} |
| 642 | </div> |
| 643 | `; |
| 644 | container.appendChild(card); |
| 645 | }); |
| 646 | } catch (err) { |
| 647 | container.innerHTML = `<div style="color:var(--status-crimson);">Goals error: ${esc(err.message)}</div>`; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // ========================================================================= // |
| 652 | // ARTIFACTS LIBRARY // |
| 653 | // ========================================================================= // |
| 654 | async function loadArtifacts() { |
| 655 | const grid = document.getElementById("artifacts-grid"); |
| 656 | if (!grid) return; |
| 657 | grid.innerHTML = "<div style='color:var(--text-muted);'>Inspecting artifacts catalog...</div>"; |
| 658 | |
| 659 | try { |
| 660 | const res = await fetch("/api/artifacts"); |
| 661 | if (!res.ok) throw new Error("Artifacts fetch failed"); |
| 662 | const data = await res.json(); |
| 663 | grid.innerHTML = ""; |
| 664 | |
| 665 | const items = data.artifacts || []; |
| 666 | const badge = document.getElementById("artifacts-badge"); |
| 667 | if (badge) badge.textContent = items.length; |
| 668 | |
| 669 | if (items.length === 0) { |
| 670 | grid.innerHTML = "<div style='color:var(--text-muted); padding:20px;'>No artifacts generated yet. Output files will appear here automatically.</div>"; |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | items.forEach(item => { |
| 675 | const card = document.createElement("div"); |
| 676 | card.className = "artifact-card"; |
| 677 | card.innerHTML = ` |
| 678 | <div class="artifact-card-icon"> |
| 679 | <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:18px;height:18px;"><polygon points="12 2 2 7 12 12 22 7 12 2"/><polyline points="2 17 12 22 22 17"/><polyline points="2 12 12 17 22 12"/></svg> |
| 680 | </div> |
| 681 | <div class="artifact-card-title">${esc(item.filename)}</div> |
| 682 | <div style="font-size:11px; color:var(--text-muted); display:flex; justify-content:space-between;"> |
| 683 | <span>${esc(item.ext.toUpperCase())}</span> |
| 684 | <span>${(item.size / 1024).toFixed(1)} KB</span> |
| 685 | </div> |
| 686 | `; |
| 687 | card.addEventListener("click", () => { |
| 688 | openArtifactInDrawer(item.filename); |
| 689 | }); |
| 690 | grid.appendChild(card); |
| 691 | }); |
| 692 | } catch (err) { |
| 693 | grid.innerHTML = `<div style="color:var(--status-crimson);">Failed to load artifacts: ${esc(err.message)}</div>`; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | async function openArtifactInDrawer(filename) { |
| 698 | const drawer = document.getElementById("muse-drawer"); |
| 699 | const emptyState = document.getElementById("artifact-empty-state"); |
| 700 | const viewer = document.getElementById("artifact-content-viewer"); |
| 701 | const titleEl = document.getElementById("artifact-active-filename"); |
| 702 | const codeEl = document.getElementById("artifact-active-code"); |
| 703 | |
| 704 | if (drawer) drawer.classList.add("open"); |
| 705 | switchDrawerTab("artifact"); |
| 706 | |
| 707 | try { |
| 708 | const res = await fetch(`/api/artifacts/${encodeURIComponent(filename)}`); |
| 709 | if (!res.ok) throw new Error("Could not retrieve artifact file"); |
| 710 | const text = await res.text(); |
| 711 | |
| 712 | activeArtifact = { filename, content: text }; |
| 713 | if (titleEl) titleEl.textContent = filename; |
| 714 | if (codeEl) codeEl.textContent = text; |
| 715 | if (emptyState) emptyState.style.display = "none"; |
| 716 | if (viewer) viewer.style.display = "block"; |
| 717 | } catch (err) { |
| 718 | alert("Error opening artifact: " + err.message); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | // ========================================================================= // |
| 723 | // APPROVALS QUEUE // |
| 724 | // ========================================================================= // |
| 725 | function initApprovals() { |
| 726 | const btnStageApprovals = document.getElementById("btn-approvals-queue"); |
| 727 | if (btnStageApprovals) { |
| 728 | btnStageApprovals.addEventListener("click", () => { |
| 729 | const drawer = document.getElementById("muse-drawer"); |
| 730 | if (drawer) drawer.classList.add("open"); |
| 731 | switchDrawerTab("approvals"); |
| 732 | loadApprovals(); |
| 733 | }); |
| 734 | } |
| 735 | loadApprovals(); |
| 736 | } |
| 737 | |
| 738 | async function loadApprovals() { |
| 739 | try { |
| 740 | const res = await fetch("/api/approvals"); |
| 741 | if (!res.ok) return; |
| 742 | const data = await res.json(); |
| 743 | const approvals = data.approvals || []; |
| 744 | |
| 745 | const badgeTop = document.getElementById("approvals-count-badge"); |
| 746 | const badgeDrawer = document.getElementById("drawer-appr-badge"); |
| 747 | const pendingCount = approvals.filter(a => a.status === "pending").length; |
| 748 | |
| 749 | if (badgeTop) badgeTop.textContent = pendingCount; |
| 750 | if (badgeDrawer) badgeDrawer.textContent = pendingCount; |
| 751 | |
| 752 | const list = document.getElementById("drawer-approvals-list"); |
| 753 | if (!list) return; |
| 754 | |
| 755 | if (approvals.length === 0) { |
| 756 | list.innerHTML = "<div style='color:var(--text-muted); text-align:center; padding:30px;'>The cockpit holds no approvals. The approval desk issues them (aien compose authorize).</div>"; |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | list.innerHTML = ""; |
| 761 | approvals.forEach(appr => { |
| 762 | const card = document.createElement("div"); |
| 763 | card.className = "approval-card"; |
| 764 | card.innerHTML = ` |
| 765 | <div class="approval-header"> |
| 766 | <span class="approval-title"> |
| 767 | <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width:16px;height:16px;color:var(--status-amber);"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg> |
| 768 | ${esc(appr.title)} |
| 769 | </span> |
| 770 | <span class="approval-badge">${esc(appr.status.toUpperCase())}</span> |
| 771 | </div> |
| 772 | <p class="approval-desc">${esc(appr.description)}</p> |
| 773 | ${appr.command_or_diff ? `<pre class="approval-cmd">${esc(appr.command_or_diff)}</pre>` : ""} |
| 774 | ${appr.status === "pending" ? ` |
| 775 | <div class="approval-actions"> |
| 776 | <button class="btn-approve" data-id="${esc(appr.id)}">Approve</button> |
| 777 | <button class="btn-reject" data-id="${esc(appr.id)}">Reject</button> |
| 778 | </div> |
| 779 | ` : ""} |
| 780 | `; |
| 781 | |
| 782 | const btnApprove = card.querySelector(".btn-approve"); |
| 783 | const btnReject = card.querySelector(".btn-reject"); |
| 784 | |
| 785 | if (btnApprove) { |
| 786 | btnApprove.addEventListener("click", () => resolveApproval(appr.id, "approve")); |
| 787 | } |
| 788 | if (btnReject) { |
| 789 | btnReject.addEventListener("click", () => resolveApproval(appr.id, "reject")); |
| 790 | } |
| 791 | |
| 792 | list.appendChild(card); |
| 793 | }); |
| 794 | } catch (_) {} |
| 795 | } |
| 796 | |
| 797 | async function resolveApproval(id, action) { |
| 798 | try { |
| 799 | const res = await fetch(`/api/approvals/${encodeURIComponent(id)}/resolve`, { |
| 800 | method: "POST", |
| 801 | headers: { "Content-Type": "application/json" }, |
| 802 | body: JSON.stringify({ action }) |
| 803 | }); |
| 804 | if (res.ok) { |
| 805 | loadApprovals(); |
| 806 | } |
| 807 | } catch (err) { |
| 808 | alert("Approval error: " + err.message); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | // ========================================================================= // |
| 813 | // MEMORY INSPECTOR (SPARK CORTEX) // |
| 814 | // ========================================================================= // |
| 815 | function initMemory() { |
| 816 | const btnSearch = document.getElementById("btn-search-memory"); |
| 817 | const input = document.getElementById("memory-search-input"); |
| 818 | |
| 819 | if (btnSearch && input) { |
| 820 | btnSearch.addEventListener("click", () => { |
| 821 | loadMemory(input.value.trim()); |
| 822 | }); |
| 823 | input.addEventListener("keydown", (e) => { |
| 824 | if (e.key === "Enter") loadMemory(input.value.trim()); |
| 825 | }); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | async function loadMemory(query = "") { |
| 830 | const grid = document.getElementById("memory-grid"); |
| 831 | if (!grid) return; |
| 832 | grid.innerHTML = "<div style='color:var(--text-muted);'>Querying Spark Cortex space atlas-memory...</div>"; |
| 833 | |
| 834 | try { |
| 835 | const url = query ? `/api/cortex?q=${encodeURIComponent(query)}` : "/api/cortex"; |
| 836 | const res = await fetch(url); |
| 837 | if (!res.ok) throw new Error("Cortex query failed"); |
| 838 | const data = await res.json(); |
| 839 | grid.innerHTML = ""; |
| 840 | |
| 841 | const results = data.results || []; |
| 842 | if (results.length === 0) { |
| 843 | grid.innerHTML = "<div style='color:var(--text-muted); padding:20px;'>No memory records found matching query in space atlas-memory.</div>"; |
| 844 | return; |
| 845 | } |
| 846 | |
| 847 | results.forEach(item => { |
| 848 | const card = document.createElement("div"); |
| 849 | card.className = "memory-card"; |
| 850 | card.innerHTML = ` |
| 851 | <div style="display:flex; justify-content:space-between; align-items:center;"> |
| 852 | <span class="memory-name">${esc(item.canonical_name || item.name || "Entity")}</span> |
| 853 | <span class="feed-card-badge online">${item.confidence ? Math.round(item.confidence * 100) + "%" : "STORED"}</span> |
| 854 | </div> |
| 855 | <p class="memory-content">${esc(item.content || item.summary || "")}</p> |
| 856 | `; |
| 857 | grid.appendChild(card); |
| 858 | }); |
| 859 | } catch (err) { |
| 860 | grid.innerHTML = `<div style="color:var(--status-crimson);">Cortex memory error: ${esc(err.message)}</div>`; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // ========================================================================= // |
| 865 | // WORKSPACE DRAWER TABS // |
| 866 | // ========================================================================= // |
| 867 | function initDrawer() { |
| 868 | const drawer = document.getElementById("muse-drawer"); |
| 869 | const btnClose = document.getElementById("btn-close-drawer"); |
| 870 | const btnToggle = document.getElementById("btn-toggle-artifacts-drawer"); |
| 871 | |
| 872 | if (btnClose && drawer) { |
| 873 | btnClose.addEventListener("click", () => drawer.classList.remove("open")); |
| 874 | } |
| 875 | |
| 876 | if (btnToggle && drawer) { |
| 877 | btnToggle.addEventListener("click", () => drawer.classList.toggle("open")); |
| 878 | } |
| 879 | |
| 880 | document.querySelectorAll(".drawer-tab").forEach(tab => { |
| 881 | tab.addEventListener("click", () => { |
| 882 | const tabName = tab.getAttribute("data-drawer-tab"); |
| 883 | switchDrawerTab(tabName); |
| 884 | }); |
| 885 | }); |
| 886 | |
| 887 | const btnCopy = document.getElementById("btn-copy-artifact"); |
| 888 | if (btnCopy) { |
| 889 | btnCopy.addEventListener("click", () => { |
| 890 | if (activeArtifact && activeArtifact.content) { |
| 891 | navigator.clipboard.writeText(activeArtifact.content); |
| 892 | btnCopy.textContent = "Copied!"; |
| 893 | setTimeout(() => { btnCopy.textContent = "Copy"; }, 2000); |
| 894 | } |
| 895 | }); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | function switchDrawerTab(tabName) { |
| 900 | document.querySelectorAll(".drawer-tab").forEach(t => { |
| 901 | t.classList.toggle("active", t.getAttribute("data-drawer-tab") === tabName); |
| 902 | }); |
| 903 | document.querySelectorAll(".drawer-pane").forEach(p => { |
| 904 | p.classList.toggle("active", p.id === `drawer-pane-${tabName}`); |
| 905 | }); |
| 906 | } |
| 907 | |
| 908 | // ========================================================================= // |
| 909 | // INTERACTIVE PTY TERMINAL (BASH ON SPARK) // |
| 910 | // ========================================================================= // |
| 911 | function initTerminal() { |
| 912 | const output = document.getElementById("term-output"); |
| 913 | const input = document.getElementById("term-input"); |
| 914 | const status = document.getElementById("term-status-indicator"); |
| 915 | const btnClear = document.getElementById("btn-term-clear"); |
| 916 | |
| 917 | if (btnClear && output) { |
| 918 | btnClear.addEventListener("click", () => { output.textContent = ""; }); |
| 919 | } |
| 920 | |
| 921 | if (termWs && termWs.readyState === WebSocket.OPEN) return; |
| 922 | |
| 923 | const protocol = location.protocol === "https:" ? "wss:" : "ws:"; |
| 924 | const wsUrl = `${protocol}//${location.host}/ws/terminal`; |
| 925 | termWs = new WebSocket(wsUrl); |
| 926 | |
| 927 | termWs.onopen = () => { |
| 928 | if (status) { |
| 929 | status.textContent = "Connected"; |
| 930 | status.style.color = "var(--status-emerald)"; |
| 931 | } |
| 932 | }; |
| 933 | |
| 934 | termWs.onmessage = (event) => { |
| 935 | if (output) { |
| 936 | output.textContent += event.data; |
| 937 | output.scrollTop = output.scrollHeight; |
| 938 | } |
| 939 | }; |
| 940 | |
| 941 | termWs.onclose = () => { |
| 942 | if (status) { |
| 943 | status.textContent = "Disconnected"; |
| 944 | status.style.color = "var(--status-crimson)"; |
| 945 | } |
| 946 | }; |
| 947 | |
| 948 | if (input) { |
| 949 | input.addEventListener("keydown", (e) => { |
| 950 | if (e.key === "Enter") { |
| 951 | const cmd = input.value + "\n"; |
| 952 | if (termWs && termWs.readyState === WebSocket.OPEN) { |
| 953 | termWs.send(cmd); |
| 954 | } |
| 955 | input.value = ""; |
| 956 | } |
| 957 | }); |
| 958 | } |
| 959 | } |