/* Brain of Reese — chat shell. * * Renders suggestions (onboarding chips + "Maybe try" deflection chips — * one shared .suggestion-chip component, renderChips below), shows KB * health, and runs chat turns against POST /api/chat (SSE, PLAN §4): * deltas render live into the Brain bubble, the done event appends source * chips (and "Maybe try" chips when the turn was deflected — honesty gate, * phase 04), errors surface as a red banner. The full feedback state * machine lands with the loading-feedback story; this keeps the "never * stale" contract: the button is busy for the whole turn and is always * re-enabled at the end. All DOM ids match frontend/index.html. */ const messagesEl = document.querySelector("#messages"); const emptyState = document.querySelector("#empty-state"); const suggestionsEl = document.querySelector("#suggestions"); const composer = document.querySelector("#composer"); const input = document.querySelector("#message-input"); const sendBtn = document.querySelector("#send-btn"); const sendLabel = document.querySelector("#send-label"); const sendStatus = document.querySelector("#send-status"); const banner = document.querySelector("#kb-banner"); const bannerText = document.querySelector("#kb-banner-text"); const versionEl = document.querySelector("#app-version"); /* ---------- tiny, safe markdown renderer (no external libs, no CDN) ---------- */ export function escapeHtml(s) { return s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'", }[c])); } export function renderMarkdown(md) { // 1. Protect fenced code blocks. const codeBlocks = []; let text = md.replace(/```(\w*)\n([\s\S]*?)```/g, (_m, _lang, code) => { codeBlocks.push(`
${escapeHtml(code.replace(/\n$/, ""))}
`); return `\u0000CODE${codeBlocks.length - 1}\u0000`; }); // 2. Escape everything else, then apply inline + block transforms. text = escapeHtml(text) .replace(/`([^`\n]+)`/g, "$1") .replace(/\*\*([^*]+)\*\*/g, "$1") .replace(/(^|[\s(])\*([^*\n]+)\*/g, "$1$2") .replace(/^### (.*)$/gm, "

$1

") .replace(/^## (.*)$/gm, "

$1

") .replace(/^# (.*)$/gm, "

$1

") .replace(/^\s*[-*] (.*)$/gm, "
  • $1
  • ") .replace(/(
  • [\s\S]*?<\/li>)(?!\s*
  • )/g, "") .replace(/^\d+\. (.*)$/gm, "
  • $1
  • "); // 3. Paragraphs (double newline separated). text = text .split(/\n{2,}/) .map((block) => { const b = block.trim(); if (!b) return ""; if (/^<(h\d|ul|ol|pre|li)/.test(b)) return b; return `

    ${b.replace(/\n/g, "
    ")}

    `; }) .join(""); // 4. Restore code blocks. return text.replace(/\u0000CODE(\d+)\u0000/g, (_m, i) => codeBlocks[Number(i)]); } /* ---------- messages ---------- */ function addMessage(who, html) { if (emptyState) emptyState.hidden = true; const wrap = document.createElement("div"); wrap.className = `msg ${who}`; wrap.innerHTML = `
    ${html}
    `; messagesEl.appendChild(wrap); wrap.scrollIntoView({ behavior: "smooth", block: "end" }); return wrap; } function addTyping() { if (emptyState) emptyState.hidden = true; const wrap = document.createElement("div"); wrap.className = "msg brain"; wrap.id = "typing-indicator"; wrap.innerHTML = `
    `; messagesEl.appendChild(wrap); wrap.scrollIntoView({ behavior: "smooth", block: "end" }); } function removeTyping() { document.querySelector("#typing-indicator")?.remove(); } /* ---------- suggestions (shared chip component, phase 05) ---------- * * One component, two homes: the onboarding row in the empty state and the * "Maybe try" row under a deflected answer. The container must be * role="list" with an accessible name ("Suggested questions" / "Maybe * try"); each chip is a real