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 / hive-wall-adapter.js
| 1 | /** |
| 2 | * Sovereign Hive Honeycomb Wall Adapter |
| 3 | * Integrates Drake's Hexagonal Honeycomb Message Wall into the Native Agent Hive Cockpit. |
| 4 | */ |
| 5 | |
| 6 | (function () { |
| 7 | const DEFAULT_RADIUS = 88; |
| 8 | let wallController = null; |
| 9 | |
| 10 | function formatCombForWall(comb) { |
| 11 | if (!comb) return null; |
| 12 | return { |
| 13 | id: String(comb.id), |
| 14 | q: Number(comb.q), |
| 15 | r: Number(comb.r), |
| 16 | author: comb.author || "AIEN", |
| 17 | role: comb.role || "agent", |
| 18 | body: comb.content || comb.body || "", |
| 19 | content: comb.content || comb.body || "", |
| 20 | status: comb.status || "visible", |
| 21 | membership: comb.role === "operator" ? "guest" : "alumni", |
| 22 | conversationId: comb.parent_id || comb.id, |
| 23 | growthParentId: comb.parent_id || null, |
| 24 | createdAt: typeof comb.created_at === "string" ? new Date(comb.created_at).getTime() : (comb.createdAt || Date.now()), |
| 25 | created_at: comb.created_at || new Date().toISOString(), |
| 26 | intent: comb.intent || "independent", |
| 27 | origin: comb.intent ? { |
| 28 | kind: comb.intent, |
| 29 | anchors: comb.parent_id ? [{ cellId: comb.parent_id }] : [] |
| 30 | } : null, |
| 31 | isMine: comb.role === "operator" || comb.author === "Drake" || comb.author === "AIEN", |
| 32 | neighbors: Array.isArray(comb.neighbors) ? comb.neighbors : [] |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | const cockpitHiveBackend = { |
| 37 | async wallBounds() { |
| 38 | try { |
| 39 | const res = await fetch("/api/hive/cells"); |
| 40 | if (!res.ok) throw new Error("HTTP " + res.status); |
| 41 | const data = await res.json(); |
| 42 | return data.bounds || { |
| 43 | revision: data.combs ? data.combs.length : 1, |
| 44 | viewerRevision: data.combs ? data.combs.length : 1, |
| 45 | radius: 5, |
| 46 | messageCount: data.combs ? data.combs.length : 1, |
| 47 | readOnly: false, |
| 48 | updatedAt: Date.now() |
| 49 | }; |
| 50 | } catch (err) { |
| 51 | console.warn("[Hive Wall] wallBounds fallback:", err); |
| 52 | return { |
| 53 | revision: 1, |
| 54 | viewerRevision: 1, |
| 55 | radius: 5, |
| 56 | messageCount: 1, |
| 57 | readOnly: false, |
| 58 | updatedAt: Date.now() |
| 59 | }; |
| 60 | } |
| 61 | }, |
| 62 | |
| 63 | async wallRegion(region) { |
| 64 | try { |
| 65 | const res = await fetch("/api/hive/cells"); |
| 66 | if (!res.ok) throw new Error("HTTP " + res.status); |
| 67 | const data = await res.json(); |
| 68 | const rawCombs = data.combs || data.cells || []; |
| 69 | const filtered = rawCombs.filter(c => |
| 70 | c.q >= region.qMin && c.q <= region.qMax && |
| 71 | c.r >= region.rMin && c.r <= region.rMax |
| 72 | ); |
| 73 | const cells = filtered.map(formatCombForWall); |
| 74 | return { |
| 75 | bounds: data.bounds, |
| 76 | region: { |
| 77 | qMin: region.qMin, |
| 78 | qMax: region.qMax, |
| 79 | rMin: region.rMin, |
| 80 | rMax: region.rMax |
| 81 | }, |
| 82 | cells, |
| 83 | conversations: [], |
| 84 | truncated: false |
| 85 | }; |
| 86 | } catch (err) { |
| 87 | console.error("[Hive Wall] wallRegion error:", err); |
| 88 | return { |
| 89 | bounds: { revision: 1, radius: 5, messageCount: 0, readOnly: false, updatedAt: Date.now() }, |
| 90 | region, |
| 91 | cells: [], |
| 92 | conversations: [], |
| 93 | truncated: false |
| 94 | }; |
| 95 | } |
| 96 | }, |
| 97 | |
| 98 | async wallPlaceCell(entry) { |
| 99 | const payload = { |
| 100 | q: entry.q, |
| 101 | r: entry.r, |
| 102 | author: "Drake", |
| 103 | role: "operator", |
| 104 | content: entry.body, |
| 105 | intent: entry.intent || "independent", |
| 106 | parent_id: entry.replyToId || (entry.anchorCellIds && entry.anchorCellIds[0]) || null |
| 107 | }; |
| 108 | |
| 109 | const res = await fetch("/api/hive/comb", { |
| 110 | method: "POST", |
| 111 | headers: { "Content-Type": "application/json" }, |
| 112 | body: JSON.stringify(payload) |
| 113 | }); |
| 114 | |
| 115 | if (!res.ok) { |
| 116 | const err = await res.json().catch(() => ({ error: "Failed to place comb" })); |
| 117 | throw new Error(err.error || "Placement rejected"); |
| 118 | } |
| 119 | |
| 120 | const data = await res.json(); |
| 121 | return { |
| 122 | bounds: data.bounds, |
| 123 | cell: formatCombForWall(data.comb || data.cell) |
| 124 | }; |
| 125 | }, |
| 126 | |
| 127 | async wallCell(cellId) { |
| 128 | const res = await fetch("/api/hive/cells"); |
| 129 | const data = await res.json(); |
| 130 | const rawCombs = data.combs || data.cells || []; |
| 131 | const found = rawCombs.find(c => String(c.id) === String(cellId)); |
| 132 | return { |
| 133 | bounds: data.bounds, |
| 134 | cell: found ? formatCombForWall(found) : null |
| 135 | }; |
| 136 | }, |
| 137 | |
| 138 | subscribeWallChanges(callback) { |
| 139 | let lastRevision = 0; |
| 140 | const timer = setInterval(async () => { |
| 141 | try { |
| 142 | const res = await fetch("/api/hive/bounds"); |
| 143 | if (!res.ok) return; |
| 144 | const data = await res.json(); |
| 145 | const rev = data.bounds ? data.bounds.revision : (data.count || 0); |
| 146 | if (rev !== lastRevision) { |
| 147 | lastRevision = rev; |
| 148 | callback({ |
| 149 | type: "bounds", |
| 150 | revision: rev, |
| 151 | bounds: data.bounds |
| 152 | }); |
| 153 | updateWallHeaderBadge(data.bounds); |
| 154 | } |
| 155 | } catch (e) {} |
| 156 | }, 4000); |
| 157 | |
| 158 | return () => clearInterval(timer); |
| 159 | }, |
| 160 | |
| 161 | async wallIdentityKey() { |
| 162 | return "hive:operator"; |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | function updateWallHeaderBadge(bounds) { |
| 167 | if (!bounds) return; |
| 168 | const countEl = document.getElementById("hive-wall-count"); |
| 169 | const radiusEl = document.getElementById("hive-wall-radius"); |
| 170 | if (countEl) countEl.textContent = bounds.count ?? bounds.messageCount ?? 0; |
| 171 | if (radiusEl) radiusEl.textContent = bounds.radius ?? 0; |
| 172 | } |
| 173 | |
| 174 | function initHiveWall() { |
| 175 | const mount = document.getElementById("hive-wall-container"); |
| 176 | if (!mount) return; |
| 177 | |
| 178 | if (!wallController && window.HiveWall && typeof window.HiveWall.createHiveWall === "function") { |
| 179 | try { |
| 180 | wallController = window.HiveWall.createHiveWall({ |
| 181 | root: mount, |
| 182 | backend: cockpitHiveBackend, |
| 183 | getUser: () => ({ tag: "Drake", membership: "alumni" }), |
| 184 | toast: (msg) => { |
| 185 | console.log("[Hive Wall Toast]", msg); |
| 186 | }, |
| 187 | hexRadius: DEFAULT_RADIUS, |
| 188 | }); |
| 189 | wallController.activate(); |
| 190 | console.log("✓ Hexagonal Honeycomb Message Wall active in Hive Pulse"); |
| 191 | } catch (err) { |
| 192 | console.error("Failed to initialize HiveWallController:", err); |
| 193 | } |
| 194 | } else if (wallController) { |
| 195 | // Refresh visible region on re-tab |
| 196 | if (typeof wallController.refreshVisible === "function") { |
| 197 | wallController.refreshVisible(true); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Update telemetry badge |
| 202 | fetch("/api/hive/bounds") |
| 203 | .then(r => r.json()) |
| 204 | .then(d => { |
| 205 | if (d.bounds) updateWallHeaderBadge(d.bounds); |
| 206 | }) |
| 207 | .catch(() => {}); |
| 208 | } |
| 209 | |
| 210 | async function emitSocraticInquiryModal() { |
| 211 | const question = prompt("Enter Socratic inquiry to emit onto the hexagonal honeycomb lattice:"); |
| 212 | if (!question || !question.trim()) return; |
| 213 | |
| 214 | try { |
| 215 | const res = await fetch("/api/hive/comb", { |
| 216 | method: "POST", |
| 217 | headers: { "Content-Type": "application/json" }, |
| 218 | body: JSON.stringify({ |
| 219 | author: "AIEN (Socratic)", |
| 220 | role: "socratic", |
| 221 | content: question.trim(), |
| 222 | intent: "branch" |
| 223 | }) |
| 224 | }); |
| 225 | |
| 226 | if (!res.ok) { |
| 227 | const err = await res.json().catch(() => ({ error: "Emission failed" })); |
| 228 | alert("Failed to emit comb: " + (err.error || "Unknown error")); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | const data = await res.json(); |
| 233 | console.log("✓ Socratic Comb placed:", data); |
| 234 | if (wallController && typeof wallController.refreshVisible === "function") { |
| 235 | wallController.refreshVisible(true); |
| 236 | } |
| 237 | } catch (e) { |
| 238 | alert("Error emitting Socratic inquiry: " + e.message); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // Expose globally |
| 243 | window.initHiveWall = initHiveWall; |
| 244 | window.emitSocraticInquiryModal = emitSocraticInquiryModal; |
| 245 | |
| 246 | // Wire up manual refresh and socratic buttons if present |
| 247 | document.addEventListener("DOMContentLoaded", () => { |
| 248 | const refreshBtn = document.getElementById("btn-hive-wall-refresh"); |
| 249 | if (refreshBtn) { |
| 250 | refreshBtn.addEventListener("click", () => { |
| 251 | if (wallController && typeof wallController.refreshVisible === "function") { |
| 252 | wallController.refreshVisible(true); |
| 253 | } |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | const socraticBtn = document.getElementById("btn-hive-wall-socratic"); |
| 258 | if (socraticBtn) { |
| 259 | socraticBtn.addEventListener("click", emitSocraticInquiryModal); |
| 260 | } |
| 261 | }); |
| 262 | })(); |