(() => { "use strict"; const WORKER_ACCESS_TOKEN_SESSION_KEY = "aureon.workerAccessToken"; const state = { language: "en", messages: [], observers: [ { id: "gemma", label: "Gemma Context Observer", status: "idle", notes: "Conversation context" }, { id: "aureon", label: "Aureon Queen Observer", status: "linked", notes: "Supervisor and organism state" }, ], lastSupervisor: null, flameballPlan: null, }; const providerModels = { gemini: ["gemini-1.5-flash", "gemini-2.0-flash-exp"], openrouter: ["openrouter/free", "google/gemma-4-31b-it:free", "meta-llama/llama-3.1-8b-instruct:free"], aureon: ["aureon-brain", "aureon-queen", "aureon-council", "aureon-architect", "aureon-vault"], huggingface: ["Qwen/Qwen2.5-7B-Instruct", "HuggingFaceH4/zephyr-7b-beta", "microsoft/Phi-3-mini-4k-instruct"], grok: ["grok-2-latest"], openai: ["gpt-4o-mini", "gpt-4o"], }; const $ = (id) => document.getElementById(id); function escapeHtml(value) { return String(value ?? "") .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } function asList(value) { if (Array.isArray(value)) return value; if (value && typeof value === "object") return Object.values(value); return []; } function workerAccessToken() { try { return String(window.sessionStorage.getItem(WORKER_ACCESS_TOKEN_SESSION_KEY) || "").trim(); } catch { return ""; } } function saveWorkerAccessTokenForSession() { const input = $("workerAccessToken"); const token = String(input?.value || "").trim(); try { if (token) { window.sessionStorage.setItem(WORKER_ACCESS_TOKEN_SESSION_KEY, token); } else { window.sessionStorage.removeItem(WORKER_ACCESS_TOKEN_SESSION_KEY); } } catch { setStatus("Session storage is unavailable; the access token was not retained.", "warn"); return; } finally { if (input) input.value = ""; } setStatus(token ? "Worker access token is active for this tab." : "Worker access token cleared.", token ? "ok" : "info"); } async function fetchJson(url, options = {}) { const target = new URL(url, window.location.href); const requestOptions = { ...options }; if (target.origin === window.location.origin && (target.pathname === "/api" || target.pathname.startsWith("/api/"))) { const token = workerAccessToken(); if (!token) throw new Error("Worker access token is required for API routes."); const headers = new Headers(options.headers || {}); headers.set("Authorization", `Bearer ${token}`); requestOptions.headers = headers; } const response = await fetch(url, requestOptions); const text = await response.text(); const data = text ? JSON.parse(text) : {}; if (!response.ok) { throw new Error(data?.error?.message || `${response.status} ${response.statusText}`); } return data; } async function postJson(url, body) { return fetchJson(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); } function setText(id, text) { const node = $(id); if (node) node.textContent = text; } function setHtml(id, html) { const node = $(id); if (node) node.innerHTML = html; } function setStatus(text, tone = "info") { const node = $("status"); if (!node) return; node.textContent = text; node.dataset.tone = tone; } function selectedValue(id, fallback = "") { return $(id)?.value || fallback; } function updateModels() { const provider = selectedValue("provider", "aureon"); const modelSelect = $("model"); if (!modelSelect) return; modelSelect.innerHTML = ""; for (const model of providerModels[provider] || ["default"]) { const option = document.createElement("option"); option.value = model; option.textContent = model; modelSelect.appendChild(option); } updateActiveLabels(); } function updateActiveLabels() { const provider = selectedValue("provider", "aureon"); const model = selectedValue("model", "aureon-brain"); const engine = selectedValue("assistantEngine", "standard"); setText("activeModelLabel", `${provider} / ${model} / ${engine}`); const hint = { standard: "Standard chat through the selected provider.", codex: "Codex persona for software and repo work; provider still controls the chat route.", aureon: "Aureon local bridge through Phi, supervisor, and vault context.", flameball: "Aureon flAmeBall mode for local planning and guarded execution.", terminal: "Local terminal is guarded by MURGE_HOST_TERMINAL_ENABLED.", sandbox: "Docker sandbox is guarded by MURGE_SANDBOX_ENABLED.", }[engine] || "Choose an agent persona for this PC."; setText("assistantEngineHint", hint); } function appendMessage(role, text, extraClass = "") { const container = $("chatMessages"); if (!container) return null; const node = document.createElement("div"); node.className = `message ${role}${extraClass ? ` ${extraClass}` : ""}`; node.textContent = text; container.appendChild(node); container.scrollTop = container.scrollHeight; state.messages.push({ role, text, createdAt: new Date().toISOString() }); return node; } function removeNode(node) { if (node?.parentNode) node.parentNode.removeChild(node); } function rolePrompt() { const role = selectedValue("role", "assistant"); const route = selectedValue("swarmRoute", "direct"); const supervisor = state.lastSupervisor; const gates = supervisor?.liveGates || {}; return [ `You are ${role} inside the Flameborn UI connected to Aureon.`, `Swarm route: ${route}.`, `Aureon supervisor: ${supervisor?.status || "unknown"}.`, `Trade path: ${gates.tradePathState || "unknown"}.`, "Do not create manual trading controls or bypass runtime gates.", ].join(" "); } async function sendChat() { const input = $("userInput"); const message = input?.value.trim() || ""; if (!message) return; const engine = selectedValue("assistantEngine", "standard"); const provider = engine === "aureon" || engine === "flameball" || $("localAureonAssistant")?.checked ? "aureon" : selectedValue("provider", "aureon"); const model = selectedValue("model", providerModels[provider]?.[0] || "aureon-brain"); appendMessage("user", message); input.value = ""; const loading = appendMessage("loading", "Aureon is routing the request..."); setStatus("Sending through the selected local bridge...", "info"); try { // Route through Conversation Engine if toggle is on if ($("conversationModeToggle")?.checked && provider === "aureon") { const response = await postJson("/api/conversation", { action: "respond", message, voice: "queen" }); removeNode(loading); const reply = response.result?.text || JSON.stringify(response.result, null, 2); appendMessage("assistant", reply); setStatus("Queen responded via Conversation Engine", "ok"); if ($("classroomMode")?.checked) { mirrorConversation(message, reply); } return; } const endpoint = provider === "aureon" ? "/api/message" : "/api/chat"; const response = await postJson(endpoint, { provider, model, message, rolePrompt: rolePrompt(), accessMode: selectedValue("accessMode", "free"), temperature: Number(selectedValue("temperature", "0.7")), responseStyle: selectedValue("aureonResponseStyle", "safe"), swarmRoute: selectedValue("swarmRoute", "direct"), }); removeNode(loading); appendMessage("assistant", response.reply || response.text || JSON.stringify(response, null, 2)); setStatus(response.bridgeConnected === false ? "Local vault fallback used." : "Response received.", response.bridgeConnected === false ? "warn" : "ok"); if ($("classroomMode")?.checked) { mirrorConversation(message, response.reply || ""); } } catch (error) { removeNode(loading); appendMessage("assistant", `Route blocked or unavailable: ${error.message}`); setStatus(error.message, "warn"); } } async function mirrorConversation(userText, assistantText) { try { await postJson("/api/classroom/observe", { depth: selectedValue("observerDepth", "standard"), conversation: { user: userText, assistant: assistantText, provider: selectedValue("provider", "aureon"), model: selectedValue("model", "aureon-brain"), }, }); await loadClassroomState(); } catch { setText("streamStatus", "observer blocked"); } } function renderSystems(payload) { const capabilities = payload?.capabilities || {}; const systems = asList(capabilities.systems); const liveRows = asList(payload?.liveCapabilityRows); const artifactRows = asList(payload?.artifactRows); const detected = Number(capabilities.detected || 0); const total = Number(capabilities.total || systems.length || 0); const liveGate = payload?.liveGates?.tradePathState || "unknown"; const liveHtml = liveRows.map((row) => `
${escapeHtml(row.label || row.id)} ${escapeHtml(row.status || "visible")}

${escapeHtml(row.endpoint || row.mode || row.id || "")}

`).join(""); const artifactHtml = artifactRows.map((row) => `
${escapeHtml(row.label || row.id)} ${escapeHtml(row.status || (row.exists ? "present" : "missing"))}

${escapeHtml(row.updatedAt || row.path || "")}

`).join(""); const systemHtml = systems.slice(0, 14).map((system) => `
${escapeHtml(system.label)} ${system.exists ? "wired" : "missing"}

${escapeHtml(system.purpose || system.relativePath)}

`).join(""); setHtml("aureonSystemsCard", `
${detected}/${total} repo capabilities trade path ${escapeHtml(liveGate)}
${liveHtml}${artifactHtml}${systemHtml}
`); } function renderFullCapabilityStress(report) { const summary = report?.summary || {}; const rows = asList(report?.status_rows); const actions = asList(report?.next_repair_actions); const required = `${summary.required_status_pass_count ?? 0}/${summary.required_status_count ?? 0}`; const status = report?.status || "artifact_missing"; const rowHtml = rows.slice(0, 14).map((row) => `
${escapeHtml(row.label || row.id)} ${escapeHtml(row.status || (row.passed ? "passing" : "attention"))}

${escapeHtml(row.passed ? row.id : row.next_action || row.blocker_id || row.id)}

`).join(""); const actionHtml = actions.slice(0, 4).map((action) => `
${escapeHtml(action.id || action.status_row || "repair")} ${escapeHtml(action.severity || "attention")}

${escapeHtml(action.action || "")}

`).join(""); setHtml("fullCapabilityStressCard", `
${escapeHtml(status)} ${escapeHtml(required)} proof rows
No trading bypass${summary.no_trading_gate_bypass ? "visible" : "attention"}

Live gate visibility ${summary.live_trade_gate_visibility ? "passing" : "needs proof"}; broker mutation remains runtime-owned.

ThoughtBus + Mycelium${summary.thoughtbus_receiving && summary.mycelium_receiving ? "receiving" : "attention"}

ThoughtBus ${summary.thoughtbus_receiving ? "yes" : "no"}; Mycelium ${summary.mycelium_receiving ? "yes" : "no"}.

${rowHtml || `
Full capability stress artifact is not ready yet.
`} ${actionHtml}
`); } function renderSupervisor(supervisor) { state.lastSupervisor = supervisor; const gates = supervisor.liveGates || {}; const gateSummary = [ `intent ${gates.orderIntentPublishEnabled ? "on" : "off"}`, `executor ${gates.executorEnabled ? "on" : "off"}`, `live ${gates.liveEnabled ? "on" : "off"}`, `real orders ${gates.realOrdersDisabled ? "disabled" : "enabled"}`, `mutations ${gates.exchangeMutationsDisabled ? "disabled" : "enabled"}`, ].join(" | "); setText("providerStatus", `Supervisor ${supervisor.status}; ${gateSummary}`); const badge = document.querySelector(".system-badge strong"); if (badge) badge.textContent = supervisor.supervisorConnected ? "Aureon linked" : "Aureon attention"; const badgeStatus = document.querySelector(".system-badge span"); if (badgeStatus) badgeStatus.textContent = supervisor.phiBridgeConnected ? "Phi bridge online" : "Core checking"; } async function refreshAureon() { try { const [status, systems, supervisor, fullCapabilityStress] = await Promise.all([ fetchJson("/api/aureon/status"), fetchJson("/api/aureon/systems"), fetchJson("/api/aureon/supervisor"), fetchJson("/api/aureon/full-capability-stress").catch((error) => ({ status: "artifact_missing", summary: {}, status_rows: [], next_repair_actions: [{ id: "artifact_missing", severity: "attention", action: error.message }], })), ]); renderSupervisor(supervisor); renderSystems(systems); renderFullCapabilityStress(fullCapabilityStress); renderDesktopStatus(status, supervisor); setStatus("Flameborn is connected to Aureon supervisor surfaces.", "ok"); } catch (error) { setHtml("aureonSystemsCard", `
Aureon supervisor unavailable: ${escapeHtml(error.message)}
`); setHtml("fullCapabilityStressCard", `
Full launch proof unavailable: ${escapeHtml(error.message)}
`); setStatus(`Aureon supervisor unavailable: ${error.message}`, "warn"); } } async function refreshCapabilities() { try { setText("capabilitiesStatus", "Scanning..."); const data = await fetchJson("/api/aureon/capabilities"); renderCapabilities(data); setText("capabilitiesStatus", `${data.import_ok}/${data.total_modules} modules OK`); } catch (error) { setHtml("capabilitiesCard", `
Capability scan failed: ${escapeHtml(error.message)}
`); setText("capabilitiesStatus", "Scan failed"); } } function renderCapabilities(data) { const categories = data.categories || {}; const catOrder = ["coding", "llm", "integrations", "queen", "vault", "autonomous"]; const html = catOrder.map((cat) => { const info = categories[cat]; if (!info) return ""; const ok = info.import_ok || 0; const total = info.total || 0; const statusClass = ok === total ? "system-live" : ok > 0 ? "system-live" : "system-missing"; const items = (info.modules || []).slice(0, 6).map((m) => { const cls = m.import_ok ? "system-live" : "system-missing"; return `
${escapeHtml(m.module_path.split(".").pop())}${m.import_ok ? "OK" : "FAIL"}
`; }).join(""); return `
${escapeHtml(cat.toUpperCase())} ${ok}/${total}
${items}
`; }).join(""); setHtml("capabilitiesCard", html || `
No capability data.
`); } function renderDesktopStatus(status, supervisor) { const activation = status.activation || {}; const blockers = asList(supervisor.blockers).slice(0, 6); setText("desktopModeLabel", activation.localOnly ? "Local-only mode" : "Remote bind attention"); setHtml("desktopStatusCard", `
${escapeHtml(supervisor.status || "checking")} ${activation.noTradingGateBypass ? "no trading bypass" : "attention"}
Host terminal${activation.hostTerminalEnabled ? "enabled" : "guarded off"}

MURGE_HOST_TERMINAL_ENABLED

Docker sandbox${activation.sandboxEnabled ? "enabled" : "guarded off"}

MURGE_SANDBOX_ENABLED

Blockers${blockers.length}

${escapeHtml(blockers.join(", ") || "none visible")}

`); } async function loadClassroomState() { try { const [classroom, meta] = await Promise.all([ fetchJson("/api/classroom/state"), fetchJson("/api/metacognition"), ]); renderObservers(classroom); renderMetacognition(meta.monitor || {}); } catch { renderObservers(null); } } function renderObservers(classroom) { const observerRows = asList(classroom?.observers).length ? asList(classroom.observers) : state.observers; setHtml("observerCards", observerRows.map((observer) => `
${escapeHtml(observer.label || observer.id)}${escapeHtml(observer.status || "idle")}

${escapeHtml(observer.notes || observer.lastRunAt || "ready")}

`).join("")); setText("classroomState", `${observerRows.length} observer lane(s) visible`); setHtml("contextStream", state.messages.slice(-6).map((message) => `
${escapeHtml(message.role)}${escapeHtml(message.createdAt.slice(11, 19))}

${escapeHtml(message.text.slice(0, 180))}

`).join("") || `
No context events yet.
`); setHtml("memoryViz", `
${state.messages.length} turns ${$("classroomMode")?.checked ? "observing" : "standby"}
`); } function renderMetacognition(monitor) { setHtml("metacognitionCard", `
${escapeHtml(monitor.stabilityIndex ?? "held")}% stability ${escapeHtml(monitor.dominantProvider || "none")}
${(asList(monitor.guidance).slice(0, 3).map((item) => `

${escapeHtml(item)}

`).join("")) || `
Waiting for conversation evidence.
`}
`); } async function runTerminalCommand() { const command = $("terminalCommand")?.value.trim() || ""; if (!command) return; setText("terminalState", "running"); try { const result = await postJson("/api/terminal/run", { command, approved: Boolean($("terminalApproval")?.checked), }); setText("terminalState", result.blocked ? "blocked" : result.ok ? "ok" : "error"); setText("terminalOutput", [ `> ${command}`, result.safety?.reason ? `guard: ${result.safety.reason}` : "", result.stdout || "", result.stderr || "", ].filter(Boolean).join("\n")); } catch (error) { setText("terminalState", "blocked"); setText("terminalOutput", error.message); } } async function checkTerminalAndSandbox() { try { const terminal = await fetchJson("/api/terminal/status"); setText("terminalState", terminal.enabled ? "ready" : "guarded off"); setText("terminalCwd", terminal.cwd || "local terminal"); setText("hostTerminalStatus", terminal.enabled ? "Host terminal API enabled" : "Host terminal guarded off"); setText("hostTerminalState", terminal.enabled ? "ready" : "guarded"); } catch (error) { setText("terminalState", "unavailable"); setText("hostTerminalStatus", error.message); } try { const sandbox = await fetchJson("/api/sandbox/status"); setText("sandboxState", sandbox.enabled ? "ready" : "guarded off"); setText("sandboxStatus", sandbox.enabled ? `Docker ${sandbox.dockerAvailable ? "available" : "checking"}` : "Sandbox guarded off"); } catch (error) { setText("sandboxState", "unavailable"); setText("sandboxStatus", error.message); } } async function flameballPlan() { const task = $("flameballTask")?.value.trim() || ""; if (!task) return; setText("flameballState", "planning"); setText("flameballOutput", "Asking local Aureon planner..."); try { const response = await postJson("/api/assistant/cli", { task, mode: "plan", model: selectedValue("model", "aureon-architect"), responseStyle: selectedValue("aureonResponseStyle", "safe"), swarmRoute: selectedValue("swarmRoute", "direct"), }); state.flameballPlan = response; setText("flameballState", "planned"); setHtml("flameballQueue", `
${escapeHtml(response.reply || "No plan returned.")}
`); setText("flameballOutput", response.stderr || "Plan loaded. Review before running any guarded command."); } catch (error) { setText("flameballState", "blocked"); setHtml("flameballQueue", `
${escapeHtml(error.message)}
`); setText("flameballOutput", error.message); } } async function loadResearchFeed() { const query = $("researchQuery")?.value.trim() || "Aureon local runtime organism integration"; setHtml("researchFeedCard", `
Loading research feed...
`); try { const response = await fetchJson(`/api/research/feed?q=${encodeURIComponent(query)}&limit=3`); const items = asList(response.feed?.items || response.feed?.results); setHtml("researchFeedCard", items.map((item) => `
${escapeHtml(item.title || item.label || "research item")}${escapeHtml(item.source || "source")}

${escapeHtml(item.summary || item.url || JSON.stringify(item).slice(0, 180))}

`).join("") || `
No feed rows returned.
`); } catch (error) { setHtml("researchFeedCard", `
Research feed blocked: ${escapeHtml(error.message)}
`); } } function bindEvents() { $("saveWorkerAccessToken")?.addEventListener("click", saveWorkerAccessTokenForSession); $("workerAccessToken")?.addEventListener("keydown", (event) => { if (event.key === "Enter") { event.preventDefault(); saveWorkerAccessTokenForSession(); } }); $("provider")?.addEventListener("change", updateModels); $("model")?.addEventListener("change", updateActiveLabels); $("assistantEngine")?.addEventListener("change", updateActiveLabels); $("temperature")?.addEventListener("input", () => setText("tempValue", selectedValue("temperature", "0.7"))); $("sendBtn")?.addEventListener("click", sendChat); $("userInput")?.addEventListener("keydown", (event) => { if (event.key === "Enter" && !event.shiftKey) { event.preventDefault(); sendChat(); } }); $("clearHistory")?.addEventListener("click", () => { state.messages = []; setHtml("chatMessages", ""); renderObservers(null); }); $("exportBtn")?.addEventListener("click", async () => { const text = JSON.stringify({ exportedAt: new Date().toISOString(), messages: state.messages }, null, 2); await navigator.clipboard?.writeText(text).catch(() => {}); setStatus("Conversation export copied to clipboard.", "ok"); }); $("desktopRefreshBtn")?.addEventListener("click", refreshAureon); $("vaultToggleBtn")?.addEventListener("click", openVaultIframe); $("vaultCloseBtn")?.addEventListener("click", closeVaultIframe); $("loopStartBtn")?.addEventListener("click", () => aureonPost("/api/aureon/vault/api/loop/start", {}, "Loop started")); $("loopStopBtn")?.addEventListener("click", () => aureonPost("/api/aureon/vault/api/loop/stop", {}, "Loop stopped")); $("loopTickBtn")?.addEventListener("click", () => aureonPost("/api/aureon/vault/api/tick", {}, "Tick triggered")); $("queenConverseBtn")?.addEventListener("click", () => aureonPost("/api/aureon/vault/api/converse", {}, "Queen conversing")); $("queenMemoryBtn")?.addEventListener("click", () => aureonGet("/api/aureon/vault/api/queen/memory", "Queen memory")); $("queenSpeakBtn")?.addEventListener("click", () => { const voice = selectedValue("aureonVoiceSelect", "queen"); aureonPost("/api/aureon/vault/api/speak", { voice }, `${voice} speaking`); }); $("capabilitiesRefreshBtn")?.addEventListener("click", refreshCapabilities); $("coderSkillsBtn")?.addEventListener("click", () => aureonGet("/api/coder/skills", "Coder skills")); $("llmModelsBtn")?.addEventListener("click", () => aureonGet("/api/llm/models", "LLM models")); $("integrationsBtn")?.addEventListener("click", () => aureonGet("/api/integrations/status", "Integrations")); $("runnerExecuteBtn")?.addEventListener("click", executeRunner); $("tradingRefreshBtn")?.addEventListener("click", refreshTrading); $("worldDataIngestBtn")?.addEventListener("click", ingestWorldData); $("selfEnhanceBtn")?.addEventListener("click", triggerSelfEnhance); $("auditRefreshBtn")?.addEventListener("click", refreshAudit); $("orchestratorRefreshBtn")?.addEventListener("click", refreshOrchestrator); $("orchestratorSpinBtn")?.addEventListener("click", spinOrchestrator); $("orchestratorStartBtn")?.addEventListener("click", () => commandOrchestrator("start")); $("orchestratorStopBtn")?.addEventListener("click", () => commandOrchestrator("stop")); $("neuralMapRefreshBtn")?.addEventListener("click", refreshNeuralMap); $("executionPaperBtn")?.addEventListener("click", executePaperTrade); $("stressTestRunBtn")?.addEventListener("click", runStressTest); $("validatorRefreshBtn")?.addEventListener("click", runValidatorAll); $("deepScanBtn")?.addEventListener("click", runDeepScan); $("codeAgentRunBtn")?.addEventListener("click", runCodeAgent); $("conversationRunBtn")?.addEventListener("click", runConversation); $("agentTeamRefreshBtn")?.addEventListener("click", refreshAgentTeam); $("agentTeamCreateBtn")?.addEventListener("click", createAgentTeam); $("agentTeamRunBtn")?.addEventListener("click", runAgentTeam); $("agentTeamRunAgentBtn")?.addEventListener("click", runSingleAgent); $("fileEditorListBtn")?.addEventListener("click", listFileEditor); $("fileEditorReadBtn")?.addEventListener("click", readFileEditor); $("fileEditorSaveBtn")?.addEventListener("click", saveFileEditor); $("desktopRestartWebBtn")?.addEventListener("click", () => setStatus("Restart is handled by the PowerShell launcher, not the browser UI.", "warn")); $("desktopRestartRuntimeBtn")?.addEventListener("click", () => setStatus("Runtime restart stays in the reviewed local launcher.", "warn")); $("desktopRestartAureonBtn")?.addEventListener("click", () => setStatus("Aureon core remains owned by AUREON_PRODUCTION_LIVE.cmd.", "warn")); $("desktopOpenLogsBtn")?.addEventListener("click", () => setStatus("Logs are in integrations/aureon_murge/logs/activation.", "info")); $("terminalRunBtn")?.addEventListener("click", runTerminalCommand); $("terminalCopyBtn")?.addEventListener("click", async () => { await navigator.clipboard?.writeText($("terminalCommand")?.value || "").catch(() => {}); setStatus("Terminal command copied.", "ok"); }); document.querySelectorAll("#terminalSuggestions button").forEach((button) => { button.addEventListener("click", () => { const command = button.getAttribute("data-command") || ""; if ($("terminalCommand")) $("terminalCommand").value = command; }); }); $("sandboxStatusBtn")?.addEventListener("click", checkTerminalAndSandbox); $("sandboxConnectBtn")?.addEventListener("click", () => setText("sandboxStatus", "PTY connect remains guarded by sandbox activation.")); $("sandboxNewSessionBtn")?.addEventListener("click", () => setText("sandboxSessionLabel", `sandbox-${Date.now().toString().slice(-6)}`)); $("hostTerminalConnectBtn")?.addEventListener("click", () => setText("hostTerminalStatus", "Host PTY connect remains guarded by host terminal activation.")); $("hostTerminalNewSessionBtn")?.addEventListener("click", () => setText("hostSessionLabel", `host-${Date.now().toString().slice(-6)}`)); $("flameballPlanBtn")?.addEventListener("click", flameballPlan); $("flameballRunBtn")?.addEventListener("click", () => setText("flameballOutput", "Safe execution is routed through the local CLI/terminal guards. Generate a plan first, then use reviewed terminal commands.")); $("flameballCopyBtn")?.addEventListener("click", async () => { await navigator.clipboard?.writeText(state.flameballPlan?.reply || $("flameballOutput")?.textContent || "").catch(() => {}); setStatus("flAmeBall output copied.", "ok"); }); $("flameballClearBtn")?.addEventListener("click", () => { state.flameballPlan = null; setHtml("flameballQueue", `
No execution plan loaded.
`); setText("flameballOutput", "Aureon Executor ready. Host runtime: http://127.0.0.1:7331"); }); $("researchSearchBtn")?.addEventListener("click", loadResearchFeed); $("addGemmaObserverBtn")?.addEventListener("click", () => addObserver("gemma", "Gemma Context Observer")); $("addObserverBtn")?.addEventListener("click", () => addObserver(selectedValue("observerPreset", "logic"), $("observerPreset")?.selectedOptions?.[0]?.textContent || "Observer")); $("classroomMode")?.addEventListener("change", () => renderObservers(null)); $("langEn")?.addEventListener("click", () => setLanguage("en")); $("langPl")?.addEventListener("click", () => setLanguage("pl")); } function addObserver(id, label) { if (!state.observers.some((observer) => observer.id === id)) { state.observers.push({ id, label, status: "idle", notes: "Added locally" }); } renderObservers(null); } function setLanguage(language) { state.language = language; $("langEn")?.classList.toggle("active", language === "en"); $("langPl")?.classList.toggle("active", language === "pl"); } function openVaultIframe() { const iframe = $("vaultIframe"); const dock = $("vaultIframeDock"); const chat = $("mainChatPanel"); if (!iframe || !dock || !chat) return; iframe.src = "http://127.0.0.1:5566"; dock.style.display = "flex"; chat.style.display = "none"; } function closeVaultIframe() { const iframe = $("vaultIframe"); const dock = $("vaultIframeDock"); const chat = $("mainChatPanel"); if (!iframe || !dock || !chat) return; iframe.src = "about:blank"; dock.style.display = "none"; chat.style.display = "flex"; } async function aureonPost(endpoint, body, okMessage) { try { await postJson(endpoint, body); setStatus(okMessage, "ok"); } catch (error) { setStatus(`Aureon action failed: ${error.message}`, "warn"); } } async function aureonGet(endpoint, label) { try { const data = await fetchJson(endpoint); setStatus(`${label}: ${JSON.stringify(data).slice(0, 120)}`, "ok"); } catch (error) { setStatus(`${label} failed: ${error.message}`, "warn"); } } async function executeRunner() { const script = $("runnerScript")?.value.trim() || ""; const args = ($("runnerArgs")?.value.trim() || "").split(/\s+/).filter(Boolean); if (!script) { setText("runnerOutput", "Error: script path is required."); return; } setText("runnerOutput", "Executing..."); try { const result = await postJson("/api/aureon/run", { script, args, timeout: 30_000 }); const out = [ `Exit code: ${result.exitCode}`, result.stdout ? `--- stdout ---\n${result.stdout}` : "", result.stderr ? `--- stderr ---\n${result.stderr}` : "", ].filter(Boolean).join("\n"); setText("runnerOutput", out.slice(0, 3000)); setStatus("Runner completed", result.ok ? "ok" : "warn"); } catch (error) { setText("runnerOutput", `Execution failed: ${error.message}`); setStatus("Runner failed", "warn"); } } function connectSseBridge() { try { const source = new EventSource("/api/aureon/sse"); source.addEventListener("message", (event) => { try { const payload = JSON.parse(event.data); if (payload.type === "aureon-status") { const lambda = payload.data?.lambda_state?.lambda?.toFixed(2) ?? "—"; const level = payload.data?.lambda_state?.level ?? "—"; setText("vaultStatusLabel", `Vault: ${level} | Λ=${lambda}`); } } catch { // ignore malformed SSE } }); source.addEventListener("error", () => { // reconnect handled automatically by EventSource }); } catch { // SSE not supported } } async function refreshTrading() { try { const data = await fetchJson("/api/trading/status"); const exchanges = (data.exchanges || []).map((ex) => `
${escapeHtml(ex.label)}${ex.enabled ? (ex.hasKeys ? "keys set" : "no keys") : "missing"}
`).join(""); const bots = (data.bots || []).map((b) => `
${escapeHtml(b.label)}${b.enabled ? "available" : "missing"}
`).join(""); setHtml("tradingCard", `
${exchanges}${bots}
`); setText("tradingStatusLabel", `Mode: ${escapeHtml(data.mode || "unknown")}`); } catch (error) { setHtml("tradingCard", `
Trading status unavailable: ${escapeHtml(error.message)}
`); } } async function ingestWorldData() { const query = $("worldDataQuery")?.value.trim() || "bitcoin market news"; setText("worldDataOutput", "Ingesting..."); try { const result = await postJson("/api/world-data/ingest", { query }); const items = (result.items || []).map((i) => `[${escapeHtml(i.source)}] ${escapeHtml(i.title || i.topic)}`).join("\n"); setText("worldDataOutput", `Query: ${escapeHtml(result.query)}\nCount: ${result.count || 0}\n\n${items}`.slice(0, 2000)); setStatus("World data ingested", "ok"); } catch (error) { setText("worldDataOutput", `Ingest failed: ${error.message}`); setStatus("World data ingest failed", "warn"); } } async function triggerSelfEnhance() { setText("enhanceOutput", "Triggering self-enhancement cycle..."); try { const result = await postJson("/api/self-enhance/trigger", {}); setText("enhanceOutput", `Status: ${escapeHtml(result.status || "unknown")}\n${JSON.stringify(result, null, 2)}`.slice(0, 2000)); setStatus("Self-enhancement triggered", "ok"); } catch (error) { setText("enhanceOutput", `Trigger failed: ${error.message}`); setStatus("Self-enhancement failed", "warn"); } } async function refreshAudit() { try { const data = await fetchJson("/api/audit/trail"); const entries = (data.entries || []).slice(-10).map((e) => `
${escapeHtml(e.event || e.type || "event")}${escapeHtml(e.timestamp || "")}

${escapeHtml(JSON.stringify(e).slice(0, 120))}

`).join(""); setHtml("auditCard", entries || `
No audit entries found.
`); } catch (error) { setHtml("auditCard", `
Audit unavailable: ${escapeHtml(error.message)}
`); } } // ═══════════════════════════════════════════════════════════════════════════ // ORCHESTRATOR CONTROL PANEL // ═══════════════════════════════════════════════════════════════════════════ async function refreshOrchestrator() { try { const data = await fetchJson("/api/orchestrator/status"); const items = (data.orchestrators || []).map((o) => `
${escapeHtml(o.class)}${o.import_ok ? "ready" : "blocked"}

${escapeHtml(o.module)} · ${o.methods.length} methods

`).join(""); setHtml("orchestratorCard", `
${items}
`); setText("orchestratorStatusLabel", `${data.orchestrators?.length || 0} orchestrators scanned`); } catch (error) { setHtml("orchestratorCard", `
Orchestrator status unavailable: ${escapeHtml(error.message)}
`); } } async function spinOrchestrator() { const symbol = $("orchestratorSymbol")?.value.trim() || "BTCUSD"; setText("orchestratorStatusLabel", `Spinning ${symbol}...`); try { const result = await postJson("/api/orchestrator/spin", { symbol }); setText("orchestratorStatusLabel", `Spin complete: ${escapeHtml(result.signal?.direction || "?")} (${(result.signal?.confidence || 0).toFixed(2)})`); setStatus(`Orchestrator spin: ${result.signal?.direction || "unknown"}`, "ok"); } catch (error) { setText("orchestratorStatusLabel", `Spin failed: ${error.message}`); setStatus("Orchestrator spin failed", "warn"); } } async function commandOrchestrator(action) { const target = "global"; setText("orchestratorStatusLabel", `${action} ${target}...`); try { const result = await postJson("/api/orchestrator/command", { action, target }); setText("orchestratorStatusLabel", `${action}: ${escapeHtml(result.status || result.started || result.stopped || "ok")}`); setStatus(`Orchestrator ${action}: OK`, "ok"); } catch (error) { setText("orchestratorStatusLabel", `${action} failed: ${error.message}`); setStatus(`Orchestrator ${action} failed`, "warn"); } } // ═══════════════════════════════════════════════════════════════════════════ // NEURAL PATHWAY MAPPER // ═══════════════════════════════════════════════════════════════════════════ async function refreshNeuralMap() { setHtml("neuralMapCard", `
Building neural map...
`); try { const data = await fetchJson("/api/neural-map"); const categories = {}; for (const node of data.nodes || []) { const cat = node.category || "unknown"; if (!categories[cat]) categories[cat] = []; categories[cat].push(node); } const catHtml = Object.entries(categories).map(([cat, nodes]) => `
${escapeHtml(cat)} ${nodes.length} nodes
${nodes.slice(0, 8).map((n) => `${escapeHtml(n.label)}`).join("")} ${nodes.length > 8 ? `+${nodes.length - 8} more` : ""}
`).join(""); const edgeSummary = `
${data.edgeCount} import edges across ${data.nodeCount} modules
`; setHtml("neuralMapCard", `${edgeSummary}${catHtml}`); } catch (error) { setHtml("neuralMapCard", `
Neural map unavailable: ${escapeHtml(error.message)}
`); } } // ═══════════════════════════════════════════════════════════════════════════ // LIVE TRADING EXECUTION // ═══════════════════════════════════════════════════════════════════════════ async function executePaperTrade() { const symbol = $("executionSymbol")?.value.trim() || "BTCUSD"; const exchange = $("executionExchange")?.value || "kraken"; setText("executionOutput", `Executing paper trade for ${symbol} on ${exchange}...`); try { const result = await postJson("/api/trading/execute", { symbol, exchange, mode: "paper" }); const trade = result.trade || {}; setText("executionOutput", [ `Symbol: ${trade.symbol}`, `Exchange: ${trade.exchange}`, `Direction: ${trade.direction}`, `Confidence: ${trade.confidence}`, `Entry: ${trade.entry_price}`, `Status: ${trade.status}`, `Time: ${trade.timestamp}`, ].join("\n")); setStatus(`Paper trade executed: ${trade.direction}`, "ok"); } catch (error) { setText("executionOutput", `Execution failed: ${error.message}`); setStatus("Paper trade failed", "warn"); } } // ═══════════════════════════════════════════════════════════════════════════ // STRESS TEST SUITE // ═══════════════════════════════════════════════════════════════════════════ async function runStressTest() { const type = $("stressTestType")?.value || "smoke"; setText("stressTestOutput", `Running ${type} test... this may take a while.`); try { const result = await postJson("/api/stress-test/run", { type }); const reports = result.reports || []; const summary = reports.map((r) => { const status = r.slo_passed ? "✅" : "❌"; return `${status} ${r.test_type}: ${r.total_requests} req, ${r.rps} rps, ${(r.availability * 100).toFixed(1)}% avail`; }).join("\n"); setText("stressTestOutput", summary || JSON.stringify(result, null, 2).slice(0, 2000)); setStatus(`Stress test complete: ${result.all_passed ? "ALL PASSED" : "FAILURES"}`, result.all_passed ? "ok" : "warn"); } catch (error) { setText("stressTestOutput", `Stress test failed: ${error.message}`); setStatus("Stress test failed", "warn"); } } // ═══════════════════════════════════════════════════════════════════════════ // ORGANISM VALIDATOR — executes each capability and reports results // ═══════════════════════════════════════════════════════════════════════════ async function loadCapabilityList() { try { const data = await fetchJson("/api/capability/list"); const caps = data.capabilities || []; const byCat = {}; for (const c of caps) { if (!byCat[c.category]) byCat[c.category] = []; byCat[c.category].push(c); } let html = ""; for (const [cat, items] of Object.entries(byCat)) { html += `
${escapeHtml(cat.toUpperCase())}`; for (const item of items) { html += ``; } html += `
`; } setHtml("validatorCard", html); document.querySelectorAll(".validator-btn").forEach((btn) => { btn.addEventListener("click", () => runSingleCapability(btn.getAttribute("data-capability"))); }); setText("validatorStatusLabel", `${caps.length} capabilities ready`); } catch (error) { setHtml("validatorCard", `
Validator unavailable: ${escapeHtml(error.message)}
`); } } async function runSingleCapability(key) { const btn = document.querySelector(`.validator-btn[data-capability="${key}"]`); if (btn) btn.textContent = "Running..."; try { const result = await postJson("/api/capability/execute", { capability: key }); const status = result.result ? "✅" : "⚠️"; if (btn) btn.textContent = `${status} ${result.label}`; setStatus(`Capability ${result.label}: OK`, "ok"); } catch (error) { if (btn) btn.textContent = `❌ ${key}`; setStatus(`Capability ${key} failed: ${error.message}`, "warn"); } } async function runValidatorAll() { setText("validatorStatusLabel", "Running all capabilities..."); try { const data = await fetchJson("/api/capability/list"); const caps = data.capabilities || []; let pass = 0; let fail = 0; for (const cap of caps) { try { await postJson("/api/capability/execute", { capability: cap.key }); pass++; } catch { fail++; } } setText("validatorStatusLabel", `${pass}/${caps.length} passed, ${fail} failed`); setStatus(`Organism validation complete: ${pass}/${caps.length}`, fail === 0 ? "ok" : "warn"); } catch (error) { setText("validatorStatusLabel", `Validator failed: ${error.message}`); } } async function runDeepScan() { const out = $("deepScanOutput"); if (out) out.textContent = "Deep scanning all 794 modules... this may take 60-120s."; try { const result = await fetchJson("/api/validator/deep-scan"); const data = result.result || {}; const summary = `[Deep Scan Result] Total: ${data.total} | Passed: ${data.passed} | Failed: ${data.failed} | Rate: ${(data.pass_rate * 100).toFixed(1)}%`; if (out) out.textContent = summary + "\n\nFirst 20 failures:\n" + (data.results || []).filter(r => !r.ok).slice(0, 20).map(r => ` ${r.module}: ${r.error}`).join("\n"); setStatus(`Deep scan: ${data.passed}/${data.total} passed`, "ok"); } catch (error) { if (out) out.textContent = "Deep scan failed: " + error.message; setStatus("Deep scan failed", "warn"); } } // ═══════════════════════════════════════════════════════════════════════════ // CODE AGENT ENGINE // ═══════════════════════════════════════════════════════════════════════════ async function runCodeAgent() { const action = $("codeAgentAction")?.value || "capabilities"; const input = $("codeAgentInput")?.value.trim() || ""; setText("codeAgentOutput", `Running ${action}...`); try { if (action === "execute-plan") { const result = await postJson("/api/code-agent/execute-plan", { goal: input || "Analyze the codebase" }); const out = JSON.stringify(result.result || result.raw, null, 2).slice(0, 3000); setText("codeAgentOutput", out); setStatus(`Goal plan executed`, "ok"); return; } const body = { action }; if (input) { if (action === "explore") body.pattern = input; else if (action === "plan") body.goal = input; else if (action === "execute-python") body.code = input; else if (action === "execute-shell") body.command = input; else if (action === "read-file") body.path = input; else if (action === "web-search") body.query = input; else if (action === "propose-code") { body.description = input; body.path = "test.py"; } } const result = await postJson("/api/code-agent", body); const out = JSON.stringify(result.result || result.raw, null, 2).slice(0, 2000); setText("codeAgentOutput", out); setStatus(`Code agent ${action}: OK`, "ok"); } catch (error) { setText("codeAgentOutput", `Error: ${error.message}`); setStatus(`Code agent ${action} failed`, "warn"); } } // ═══════════════════════════════════════════════════════════════════════════ // AGENT TEAM VISUALIZER // ═══════════════════════════════════════════════════════════════════════════ async function refreshAgentTeam() { setText("agentTeamStatusLabel", "Loading..."); try { const result = await postJson("/api/agent-team", { action: "status" }); const data = result.result || {}; const agents = (data.agents || []).map((a) => `${escapeHtml(a)}`).join(""); const teams = (data.teams || []).map((t) => `${escapeHtml(t)}`).join(""); setHtml("agentTeamCard", `
Agents
${agents || "No agents"}
Teams
${teams || "No teams"}
`); setText("agentTeamStatusLabel", `${data.agents?.length || 0} agents, ${data.teams?.length || 0} teams`); } catch (error) { setHtml("agentTeamCard", `
Team status unavailable: ${escapeHtml(error.message)}
`); } } async function createAgentTeam() { const name = $("agentTeamName")?.value.trim() || "default"; const agents = $("agentTeamAgents")?.value.split(",").map((s) => s.trim()).filter(Boolean) || ["analyzer"]; setText("agentTeamOutput", `Creating team ${name}...`); try { const result = await postJson("/api/agent-team", { action: "create-team", name, agents }); setText("agentTeamOutput", JSON.stringify(result.result, null, 2).slice(0, 2000)); setStatus(`Team ${name} created`, "ok"); await refreshAgentTeam(); } catch (error) { setText("agentTeamOutput", `Error: ${error.message}`); setStatus("Team creation failed", "warn"); } } async function runAgentTeam() { const name = $("agentTeamName")?.value.trim() || "default"; const task = $("agentTeamTask")?.value.trim() || "Analyze the codebase"; setText("agentTeamOutput", `Running team ${name}...`); try { const result = await postJson("/api/agent-team", { action: "run-team", name, task }); setText("agentTeamOutput", JSON.stringify(result.result, null, 2).slice(0, 2000)); setStatus(`Team ${name} executed`, "ok"); } catch (error) { setText("agentTeamOutput", `Error: ${error.message}`); setStatus("Team run failed", "warn"); } } async function runSingleAgent() { const name = $("agentTeamName")?.value.trim() || "analyzer"; const task = $("agentTeamTask")?.value.trim() || "Analyze the codebase"; setText("agentTeamOutput", `Running agent ${name}...`); try { const result = await postJson("/api/agent-team", { action: "run-agent", name, task }); setText("agentTeamOutput", JSON.stringify(result.result, null, 2).slice(0, 2000)); setStatus(`Agent ${name} executed`, "ok"); } catch (error) { setText("agentTeamOutput", `Error: ${error.message}`); setStatus("Agent run failed", "warn"); } } // ═══════════════════════════════════════════════════════════════════════════ // FILE EDITOR // ═══════════════════════════════════════════════════════════════════════════ async function listFileEditor() { const dir = $("fileEditorDir")?.value.trim() || "."; setText("fileEditorStatusLabel", `Listing ${dir}...`); try { const result = await postJson("/api/file-editor", { action: "list", directory: dir }); const items = (result.result?.items || []).map((item) => { const isDir = item.is_dir || false; return `
${escapeHtml(item.name || item.path || "unknown")}
`; }).join(""); setHtml("fileEditorBrowser", items || `
No items in ${escapeHtml(dir)}
`); document.querySelectorAll(".file-item").forEach((el) => { el.addEventListener("click", () => { const path = el.getAttribute("data-path"); if (path && $("fileEditorPath")) $("fileEditorPath").value = path; }); }); setText("fileEditorStatusLabel", `${result.result?.items?.length || 0} items in ${dir}`); } catch (error) { setHtml("fileEditorBrowser", `
List failed: ${escapeHtml(error.message)}
`); } } async function readFileEditor() { const path = $("fileEditorPath")?.value.trim() || "README.md"; setText("fileEditorStatusLabel", `Reading ${path}...`); try { const result = await postJson("/api/file-editor", { action: "read", path }); const content = result.result?.content || ""; $("fileEditorContent").value = content; setText("fileEditorStatusLabel", `Read ${path} (${content.length} chars)`); setStatus(`File read: ${path}`, "ok"); } catch (error) { setText("fileEditorStatusLabel", `Read failed: ${error.message}`); setStatus(`File read failed`, "warn"); } } async function saveFileEditor() { const path = $("fileEditorPath")?.value.trim() || "test.txt"; const content = $("fileEditorContent")?.value || ""; setText("fileEditorStatusLabel", `Saving ${path}...`); try { const result = await postJson("/api/file-editor", { action: "write", path, content }); setText("fileEditorStatusLabel", `Saved ${path}`); setStatus(`File saved: ${path}`, "ok"); } catch (error) { setText("fileEditorStatusLabel", `Save failed: ${error.message}`); setStatus(`File save failed`, "warn"); } } // ═══════════════════════════════════════════════════════════════════════════ // CONVERSATION ENGINE // ═══════════════════════════════════════════════════════════════════════════ async function runConversation() { const action = $("conversationAction")?.value || "respond"; const input = $("conversationInput")?.value.trim() || ""; setText("conversationOutput", `Running ${action}...`); try { const body = { action }; if (input) { if (action === "respond") body.message = input; else if (action === "memory-remember") body.text = input; else if (action === "agent-run" || action === "team-run") body.task = input; } const result = await postJson("/api/conversation", body); const out = JSON.stringify(result.result || result.raw, null, 2).slice(0, 2000); setText("conversationOutput", out); setStatus(`Conversation ${action}: OK`, "ok"); } catch (error) { setText("conversationOutput", `Error: ${error.message}`); setStatus(`Conversation ${action} failed`, "warn"); } } // ═══════════════════════════════════════════════════════════════════════════ // REAL-TIME WEBSOCKET BRIDGE // ═══════════════════════════════════════════════════════════════════════════ function connectRealtimeBridge() { try { const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; const ws = new WebSocket(`${protocol}//${window.location.host}/ws/realtime`); ws.addEventListener("open", () => { setText("realtimeStatusLabel", "WebSocket connected"); ws.send(JSON.stringify({ type: "subscribe", channel: "all" })); }); ws.addEventListener("message", (event) => { try { const payload = JSON.parse(event.data); if (payload.type === "trading-tick") { const d = payload.data || {}; setHtml("realtimeTickCard", `
${escapeHtml(d.symbol)} $${d.price} ${d.direction} ${d.confidence} ${new Date(d.timestamp).toLocaleTimeString()}
`); } if (payload.type === "aureon-status") { const lambda = payload.data?.lambda_state?.lambda?.toFixed(2) ?? "—"; const level = payload.data?.lambda_state?.level ?? "—"; setText("vaultStatusLabel", `Vault: ${level} | Λ=${lambda}`); } } catch { // ignore malformed messages } }); ws.addEventListener("close", () => { setText("realtimeStatusLabel", "WebSocket disconnected — reconnecting..."); setTimeout(connectRealtimeBridge, 5000); }); ws.addEventListener("error", () => { setText("realtimeStatusLabel", "WebSocket error"); }); } catch { setText("realtimeStatusLabel", "WebSocket not supported"); } } async function init() { updateModels(); updateActiveLabels(); setLanguage("en"); appendMessage("assistant", "Flameborn UI is online. Aureon supervisor, Phi bridge, capability inventory, terminal guards, orchestrators, neural map, and classroom observers are loading."); bindEvents(); await Promise.allSettled([ refreshAureon(), refreshCapabilities(), checkTerminalAndSandbox(), loadClassroomState(), refreshOrchestrator(), refreshTrading(), loadCapabilityList(), refreshAgentTeam(), ]); setInterval(refreshAureon, 15000); connectSseBridge(); connectRealtimeBridge(); setInterval(checkTerminalAndSandbox, 30000); setInterval(refreshOrchestrator, 60000); } if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", init); } else { init(); } })();